blob: 9d330b85cd3abd6c08e070b8370d70dbc1468bab [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 *
Xin Li28c53d32017-03-07 00:33:02 +00001047 * 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 *
Xin Li28c53d32017-03-07 00:33:02 +00001060 * 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 *
Xin Li28c53d32017-03-07 00:33:02 +00001077 * 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
1404 ent->children = xmlStringGetNodeList(doc,
1405 (const xmlChar*)node->content);
1406 ent->owner = 1;
1407 temp = ent->children;
1408 while (temp) {
1409 temp->parent = (xmlNodePtr)ent;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001410 ent->last = temp;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001411 temp = temp->next;
1412 }
1413 }
1414 if (last == NULL) {
1415 last = ret = node;
1416 } else {
1417 last = xmlAddNextSibling(last, node);
1418 }
1419 }
1420 xmlFree(val);
1421 }
1422 cur++;
1423 q = cur;
1424 }
1425 if (charval != 0) {
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001426 xmlChar buffer[10];
Daniel Veillard07cb8222003-09-10 10:51:05 +00001427 int l;
1428
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001429 l = xmlCopyCharMultiByte(buffer, charval);
1430 buffer[l] = 0;
Conrad Irwin7d553f82012-05-10 20:17:25 -07001431
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001432 if (xmlBufCat(buf, buffer))
Conrad Irwin7d553f82012-05-10 20:17:25 -07001433 goto out;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001434 charval = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001435 }
Daniel Veillard07cb8222003-09-10 10:51:05 +00001436 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001437 cur++;
1438 }
Conrad Irwin7d553f82012-05-10 20:17:25 -07001439
1440 if (cur != q) {
Owen Taylor3473f882001-02-23 17:55:21 +00001441 /*
1442 * Handle the last piece of text.
1443 */
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001444 if (xmlBufAdd(buf, q, cur - q))
Conrad Irwin7d553f82012-05-10 20:17:25 -07001445 goto out;
Owen Taylor3473f882001-02-23 17:55:21 +00001446 }
Conrad Irwin7d553f82012-05-10 20:17:25 -07001447
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001448 if (!xmlBufIsEmpty(buf)) {
Conrad Irwin7d553f82012-05-10 20:17:25 -07001449 node = xmlNewDocText(doc, NULL);
1450 if (node == NULL) goto out;
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001451 node->content = xmlBufDetach(buf);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001452
1453 if (last == NULL) {
Philip Withnall57941042014-10-26 18:08:04 +00001454 ret = node;
Conrad Irwin7d553f82012-05-10 20:17:25 -07001455 } else {
Philip Withnall57941042014-10-26 18:08:04 +00001456 xmlAddNextSibling(last, node);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001457 }
1458 } else if (ret == NULL) {
1459 ret = xmlNewDocText(doc, BAD_CAST "");
1460 }
1461
1462out:
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001463 xmlBufFree(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001464 return(ret);
1465}
1466
1467/**
1468 * xmlStringGetNodeList:
1469 * @doc: the document
1470 * @value: the value of the attribute
1471 *
1472 * Parse the value string and build the node list associated. Should
1473 * produce a flat tree with only TEXTs and ENTITY_REFs.
1474 * Returns a pointer to the first child
1475 */
1476xmlNodePtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +08001477xmlStringGetNodeList(const xmlDoc *doc, const xmlChar *value) {
Owen Taylor3473f882001-02-23 17:55:21 +00001478 xmlNodePtr ret = NULL, last = NULL;
1479 xmlNodePtr node;
1480 xmlChar *val;
1481 const xmlChar *cur = value;
1482 const xmlChar *q;
1483 xmlEntityPtr ent;
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001484 xmlBufPtr buf;
Owen Taylor3473f882001-02-23 17:55:21 +00001485
1486 if (value == NULL) return(NULL);
1487
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001488 buf = xmlBufCreateSize(0);
1489 if (buf == NULL) return(NULL);
1490 xmlBufSetAllocationScheme(buf, XML_BUFFER_ALLOC_HYBRID);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001491
Owen Taylor3473f882001-02-23 17:55:21 +00001492 q = cur;
1493 while (*cur != 0) {
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001494 if (cur[0] == '&') {
1495 int charval = 0;
1496 xmlChar tmp;
1497
Owen Taylor3473f882001-02-23 17:55:21 +00001498 /*
1499 * Save the current text.
1500 */
1501 if (cur != q) {
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001502 if (xmlBufAdd(buf, q, cur - q))
Conrad Irwin7d553f82012-05-10 20:17:25 -07001503 goto out;
Owen Taylor3473f882001-02-23 17:55:21 +00001504 }
Owen Taylor3473f882001-02-23 17:55:21 +00001505 q = cur;
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001506 if ((cur[1] == '#') && (cur[2] == 'x')) {
1507 cur += 3;
1508 tmp = *cur;
1509 while (tmp != ';') { /* Non input consuming loop */
Daniel Veillardaa6de472008-08-25 14:53:31 +00001510 if ((tmp >= '0') && (tmp <= '9'))
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001511 charval = charval * 16 + (tmp - '0');
1512 else if ((tmp >= 'a') && (tmp <= 'f'))
1513 charval = charval * 16 + (tmp - 'a') + 10;
1514 else if ((tmp >= 'A') && (tmp <= 'F'))
1515 charval = charval * 16 + (tmp - 'A') + 10;
Owen Taylor3473f882001-02-23 17:55:21 +00001516 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001517 xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
1518 NULL);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001519 charval = 0;
1520 break;
1521 }
1522 cur++;
1523 tmp = *cur;
1524 }
1525 if (tmp == ';')
1526 cur++;
1527 q = cur;
1528 } else if (cur[1] == '#') {
1529 cur += 2;
1530 tmp = *cur;
1531 while (tmp != ';') { /* Non input consuming loops */
Daniel Veillardaa6de472008-08-25 14:53:31 +00001532 if ((tmp >= '0') && (tmp <= '9'))
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001533 charval = charval * 10 + (tmp - '0');
1534 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001535 xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
1536 NULL);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001537 charval = 0;
1538 break;
1539 }
1540 cur++;
1541 tmp = *cur;
1542 }
1543 if (tmp == ';')
1544 cur++;
1545 q = cur;
1546 } else {
1547 /*
1548 * Read the entity string
1549 */
1550 cur++;
1551 q = cur;
1552 while ((*cur != 0) && (*cur != ';')) cur++;
1553 if (*cur == 0) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001554 xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY,
1555 (xmlNodePtr) doc, (const char *) q);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001556 goto out;
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001557 }
1558 if (cur != q) {
1559 /*
1560 * Predefined entities don't generate nodes
1561 */
1562 val = xmlStrndup(q, cur - q);
1563 ent = xmlGetDocEntity(doc, val);
1564 if ((ent != NULL) &&
1565 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
Conrad Irwin7d553f82012-05-10 20:17:25 -07001566
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001567 if (xmlBufCat(buf, ent->content))
Conrad Irwin7d553f82012-05-10 20:17:25 -07001568 goto out;
Daniel Veillardaa6de472008-08-25 14:53:31 +00001569
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001570 } else {
1571 /*
Conrad Irwin7d553f82012-05-10 20:17:25 -07001572 * Flush buffer so far
1573 */
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001574 if (!xmlBufIsEmpty(buf)) {
Conrad Irwin7d553f82012-05-10 20:17:25 -07001575 node = xmlNewDocText(doc, NULL);
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001576 node->content = xmlBufDetach(buf);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001577
1578 if (last == NULL) {
1579 last = ret = node;
1580 } else {
1581 last = xmlAddNextSibling(last, node);
1582 }
1583 }
1584
1585 /*
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001586 * Create a new REFERENCE_REF node
1587 */
1588 node = xmlNewReference(doc, val);
1589 if (node == NULL) {
1590 if (val != NULL) xmlFree(val);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001591 goto out;
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001592 }
Daniel Veillardbf8dae82002-04-18 16:39:10 +00001593 else if ((ent != NULL) && (ent->children == NULL)) {
1594 xmlNodePtr temp;
1595
Xin Li28c53d32017-03-07 00:33:02 +00001596 ent->children = (xmlNodePtr) -1;
Daniel Veillardbf8dae82002-04-18 16:39:10 +00001597 ent->children = xmlStringGetNodeList(doc,
1598 (const xmlChar*)node->content);
Daniel Veillard2d84a892002-12-30 00:01:08 +00001599 ent->owner = 1;
Daniel Veillardbf8dae82002-04-18 16:39:10 +00001600 temp = ent->children;
1601 while (temp) {
1602 temp->parent = (xmlNodePtr)ent;
1603 temp = temp->next;
1604 }
1605 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001606 if (last == NULL) {
1607 last = ret = node;
1608 } else {
1609 last = xmlAddNextSibling(last, node);
1610 }
1611 }
1612 xmlFree(val);
1613 }
1614 cur++;
1615 q = cur;
1616 }
1617 if (charval != 0) {
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001618 xmlChar buffer[10];
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001619 int len;
1620
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001621 len = xmlCopyCharMultiByte(buffer, charval);
1622 buffer[len] = 0;
Conrad Irwin7d553f82012-05-10 20:17:25 -07001623
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001624 if (xmlBufCat(buf, buffer))
Conrad Irwin7d553f82012-05-10 20:17:25 -07001625 goto out;
1626 charval = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001627 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001628 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001629 cur++;
1630 }
Daniel Veillard75bea542001-05-11 17:41:21 +00001631 if ((cur != q) || (ret == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001632 /*
1633 * Handle the last piece of text.
1634 */
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001635 xmlBufAdd(buf, q, cur - q);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001636 }
1637
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001638 if (!xmlBufIsEmpty(buf)) {
Conrad Irwin7d553f82012-05-10 20:17:25 -07001639 node = xmlNewDocText(doc, NULL);
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001640 node->content = xmlBufDetach(buf);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001641
1642 if (last == NULL) {
Philip Withnall57941042014-10-26 18:08:04 +00001643 ret = node;
Owen Taylor3473f882001-02-23 17:55:21 +00001644 } else {
Philip Withnall57941042014-10-26 18:08:04 +00001645 xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001646 }
1647 }
Conrad Irwin7d553f82012-05-10 20:17:25 -07001648
1649out:
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001650 xmlBufFree(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001651 return(ret);
1652}
1653
1654/**
1655 * xmlNodeListGetString:
1656 * @doc: the document
1657 * @list: a Node list
1658 * @inLine: should we replace entity contents or show their external form
1659 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001660 * Build the string equivalent to the text contained in the Node list
Owen Taylor3473f882001-02-23 17:55:21 +00001661 * made of TEXTs and ENTITY_REFs
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001662 *
Daniel Veillardbd9afb52002-09-25 22:25:35 +00001663 * Returns a pointer to the string copy, the caller must free it with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00001664 */
1665xmlChar *
Kurt Roeckx95ebe532014-10-13 16:06:21 +08001666xmlNodeListGetString(xmlDocPtr doc, const xmlNode *list, int inLine)
Daniel Veillard7646b182002-04-20 06:41:40 +00001667{
Kurt Roeckx95ebe532014-10-13 16:06:21 +08001668 const xmlNode *node = list;
Owen Taylor3473f882001-02-23 17:55:21 +00001669 xmlChar *ret = NULL;
1670 xmlEntityPtr ent;
Daniel Veillard7d4c5292012-09-05 11:45:32 +08001671 int attr;
Owen Taylor3473f882001-02-23 17:55:21 +00001672
Daniel Veillard7646b182002-04-20 06:41:40 +00001673 if (list == NULL)
1674 return (NULL);
Daniel Veillard7d4c5292012-09-05 11:45:32 +08001675 if ((list->parent != NULL) && (list->parent->type == XML_ATTRIBUTE_NODE))
1676 attr = 1;
1677 else
1678 attr = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001679
1680 while (node != NULL) {
1681 if ((node->type == XML_TEXT_NODE) ||
Daniel Veillard7646b182002-04-20 06:41:40 +00001682 (node->type == XML_CDATA_SECTION_NODE)) {
1683 if (inLine) {
1684 ret = xmlStrcat(ret, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001685 } else {
Daniel Veillard7646b182002-04-20 06:41:40 +00001686 xmlChar *buffer;
Owen Taylor3473f882001-02-23 17:55:21 +00001687
Daniel Veillard7d4c5292012-09-05 11:45:32 +08001688 if (attr)
1689 buffer = xmlEncodeAttributeEntities(doc, node->content);
1690 else
1691 buffer = xmlEncodeEntitiesReentrant(doc, node->content);
Daniel Veillard7646b182002-04-20 06:41:40 +00001692 if (buffer != NULL) {
1693 ret = xmlStrcat(ret, buffer);
1694 xmlFree(buffer);
1695 }
1696 }
1697 } else if (node->type == XML_ENTITY_REF_NODE) {
1698 if (inLine) {
1699 ent = xmlGetDocEntity(doc, node->name);
1700 if (ent != NULL) {
1701 xmlChar *buffer;
1702
1703 /* an entity content can be any "well balanced chunk",
1704 * i.e. the result of the content [43] production:
1705 * http://www.w3.org/TR/REC-xml#NT-content.
1706 * So it can contain text, CDATA section or nested
1707 * entity reference nodes (among others).
1708 * -> we recursive call xmlNodeListGetString()
1709 * which handles these types */
1710 buffer = xmlNodeListGetString(doc, ent->children, 1);
1711 if (buffer != NULL) {
1712 ret = xmlStrcat(ret, buffer);
1713 xmlFree(buffer);
1714 }
1715 } else {
1716 ret = xmlStrcat(ret, node->content);
1717 }
1718 } else {
1719 xmlChar buf[2];
1720
1721 buf[0] = '&';
1722 buf[1] = 0;
1723 ret = xmlStrncat(ret, buf, 1);
1724 ret = xmlStrcat(ret, node->name);
1725 buf[0] = ';';
1726 buf[1] = 0;
1727 ret = xmlStrncat(ret, buf, 1);
1728 }
1729 }
1730#if 0
1731 else {
1732 xmlGenericError(xmlGenericErrorContext,
1733 "xmlGetNodeListString : invalid node type %d\n",
1734 node->type);
1735 }
1736#endif
1737 node = node->next;
1738 }
1739 return (ret);
1740}
Daniel Veillard652327a2003-09-29 18:02:38 +00001741
1742#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001743/**
1744 * xmlNodeListGetRawString:
1745 * @doc: the document
1746 * @list: a Node list
1747 * @inLine: should we replace entity contents or show their external form
1748 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001749 * Builds the string equivalent to the text contained in the Node list
Owen Taylor3473f882001-02-23 17:55:21 +00001750 * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString()
1751 * this function doesn't do any character encoding handling.
1752 *
Daniel Veillardbd9afb52002-09-25 22:25:35 +00001753 * Returns a pointer to the string copy, the caller must free it with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00001754 */
1755xmlChar *
Kurt Roeckx95ebe532014-10-13 16:06:21 +08001756xmlNodeListGetRawString(const xmlDoc *doc, const xmlNode *list, int inLine)
Daniel Veillard7646b182002-04-20 06:41:40 +00001757{
Kurt Roeckx95ebe532014-10-13 16:06:21 +08001758 const xmlNode *node = list;
Owen Taylor3473f882001-02-23 17:55:21 +00001759 xmlChar *ret = NULL;
1760 xmlEntityPtr ent;
1761
Daniel Veillard7646b182002-04-20 06:41:40 +00001762 if (list == NULL)
1763 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001764
1765 while (node != NULL) {
Daniel Veillard7db37732001-07-12 01:20:08 +00001766 if ((node->type == XML_TEXT_NODE) ||
Daniel Veillard7646b182002-04-20 06:41:40 +00001767 (node->type == XML_CDATA_SECTION_NODE)) {
1768 if (inLine) {
1769 ret = xmlStrcat(ret, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001770 } else {
Daniel Veillard7646b182002-04-20 06:41:40 +00001771 xmlChar *buffer;
1772
1773 buffer = xmlEncodeSpecialChars(doc, node->content);
1774 if (buffer != NULL) {
1775 ret = xmlStrcat(ret, buffer);
1776 xmlFree(buffer);
1777 }
1778 }
1779 } else if (node->type == XML_ENTITY_REF_NODE) {
1780 if (inLine) {
1781 ent = xmlGetDocEntity(doc, node->name);
1782 if (ent != NULL) {
1783 xmlChar *buffer;
1784
1785 /* an entity content can be any "well balanced chunk",
1786 * i.e. the result of the content [43] production:
1787 * http://www.w3.org/TR/REC-xml#NT-content.
1788 * So it can contain text, CDATA section or nested
1789 * entity reference nodes (among others).
1790 * -> we recursive call xmlNodeListGetRawString()
1791 * which handles these types */
1792 buffer =
1793 xmlNodeListGetRawString(doc, ent->children, 1);
1794 if (buffer != NULL) {
1795 ret = xmlStrcat(ret, buffer);
1796 xmlFree(buffer);
1797 }
1798 } else {
1799 ret = xmlStrcat(ret, node->content);
1800 }
1801 } else {
1802 xmlChar buf[2];
1803
1804 buf[0] = '&';
1805 buf[1] = 0;
1806 ret = xmlStrncat(ret, buf, 1);
1807 ret = xmlStrcat(ret, node->name);
1808 buf[0] = ';';
1809 buf[1] = 0;
1810 ret = xmlStrncat(ret, buf, 1);
1811 }
1812 }
Owen Taylor3473f882001-02-23 17:55:21 +00001813#if 0
Daniel Veillard7646b182002-04-20 06:41:40 +00001814 else {
1815 xmlGenericError(xmlGenericErrorContext,
1816 "xmlGetNodeListString : invalid node type %d\n",
1817 node->type);
1818 }
Owen Taylor3473f882001-02-23 17:55:21 +00001819#endif
Daniel Veillard7646b182002-04-20 06:41:40 +00001820 node = node->next;
Owen Taylor3473f882001-02-23 17:55:21 +00001821 }
Daniel Veillard7646b182002-04-20 06:41:40 +00001822 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00001823}
Daniel Veillard652327a2003-09-29 18:02:38 +00001824#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001825
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001826static xmlAttrPtr
1827xmlNewPropInternal(xmlNodePtr node, xmlNsPtr ns,
1828 const xmlChar * name, const xmlChar * value,
1829 int eatname)
1830{
Owen Taylor3473f882001-02-23 17:55:21 +00001831 xmlAttrPtr cur;
1832 xmlDocPtr doc = NULL;
1833
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001834 if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) {
Daniel Veillard8f6c2b12008-04-03 11:17:21 +00001835 if ((eatname == 1) &&
1836 ((node->doc == NULL) ||
Daniel Veillarded939f82008-04-08 08:20:08 +00001837 (!(xmlDictOwns(node->doc->dict, name)))))
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001838 xmlFree((xmlChar *) name);
1839 return (NULL);
1840 }
Owen Taylor3473f882001-02-23 17:55:21 +00001841
1842 /*
1843 * Allocate a new property and fill the fields.
1844 */
1845 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1846 if (cur == NULL) {
Daniel Veillard8f6c2b12008-04-03 11:17:21 +00001847 if ((eatname == 1) &&
Daniel Veillard76d36452009-09-07 11:19:33 +02001848 ((node == NULL) || (node->doc == NULL) ||
Daniel Veillarded939f82008-04-08 08:20:08 +00001849 (!(xmlDictOwns(node->doc->dict, name)))))
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001850 xmlFree((xmlChar *) name);
1851 xmlTreeErrMemory("building attribute");
1852 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001853 }
1854 memset(cur, 0, sizeof(xmlAttr));
1855 cur->type = XML_ATTRIBUTE_NODE;
1856
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001857 cur->parent = node;
Owen Taylor3473f882001-02-23 17:55:21 +00001858 if (node != NULL) {
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001859 doc = node->doc;
1860 cur->doc = doc;
Owen Taylor3473f882001-02-23 17:55:21 +00001861 }
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001862 cur->ns = ns;
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001863
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001864 if (eatname == 0) {
1865 if ((doc != NULL) && (doc->dict != NULL))
1866 cur->name = (xmlChar *) xmlDictLookup(doc->dict, name, -1);
1867 else
1868 cur->name = xmlStrdup(name);
1869 } else
1870 cur->name = name;
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001871
Owen Taylor3473f882001-02-23 17:55:21 +00001872 if (value != NULL) {
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001873 xmlNodePtr tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00001874
Daniel Veillard6f8611f2008-02-15 08:33:21 +00001875 if(!xmlCheckUTF8(value)) {
1876 xmlTreeErr(XML_TREE_NOT_UTF8, (xmlNodePtr) doc,
1877 NULL);
1878 if (doc != NULL)
1879 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
1880 }
1881 cur->children = xmlNewDocText(doc, value);
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001882 cur->last = NULL;
1883 tmp = cur->children;
1884 while (tmp != NULL) {
1885 tmp->parent = (xmlNodePtr) cur;
1886 if (tmp->next == NULL)
1887 cur->last = tmp;
1888 tmp = tmp->next;
1889 }
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001890 }
Owen Taylor3473f882001-02-23 17:55:21 +00001891
1892 /*
1893 * Add it at the end to preserve parsing order ...
1894 */
1895 if (node != NULL) {
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001896 if (node->properties == NULL) {
1897 node->properties = cur;
1898 } else {
1899 xmlAttrPtr prev = node->properties;
Owen Taylor3473f882001-02-23 17:55:21 +00001900
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001901 while (prev->next != NULL)
1902 prev = prev->next;
1903 prev->next = cur;
1904 cur->prev = prev;
1905 }
Owen Taylor3473f882001-02-23 17:55:21 +00001906 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00001907
Daniel Veillard76d36452009-09-07 11:19:33 +02001908 if ((value != NULL) && (node != NULL) &&
1909 (xmlIsID(node->doc, node, cur) == 1))
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001910 xmlAddID(NULL, node->doc, value, cur);
1911
Daniel Veillarda880b122003-04-21 21:36:41 +00001912 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001913 xmlRegisterNodeDefaultValue((xmlNodePtr) cur);
1914 return (cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001915}
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001916
1917#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
1918 defined(LIBXML_SCHEMAS_ENABLED)
1919/**
1920 * xmlNewProp:
1921 * @node: the holding node
1922 * @name: the name of the attribute
1923 * @value: the value of the attribute
1924 *
1925 * Create a new property carried by a node.
1926 * Returns a pointer to the attribute
1927 */
1928xmlAttrPtr
1929xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
1930
1931 if (name == NULL) {
1932#ifdef DEBUG_TREE
1933 xmlGenericError(xmlGenericErrorContext,
1934 "xmlNewProp : name == NULL\n");
1935#endif
1936 return(NULL);
1937 }
1938
1939 return xmlNewPropInternal(node, NULL, name, value, 0);
1940}
Daniel Veillard652327a2003-09-29 18:02:38 +00001941#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001942
1943/**
1944 * xmlNewNsProp:
1945 * @node: the holding node
1946 * @ns: the namespace
1947 * @name: the name of the attribute
1948 * @value: the value of the attribute
1949 *
1950 * Create a new property tagged with a namespace and carried by a node.
1951 * Returns a pointer to the attribute
1952 */
1953xmlAttrPtr
1954xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
1955 const xmlChar *value) {
Owen Taylor3473f882001-02-23 17:55:21 +00001956
1957 if (name == NULL) {
1958#ifdef DEBUG_TREE
1959 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00001960 "xmlNewNsProp : name == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001961#endif
1962 return(NULL);
1963 }
1964
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001965 return xmlNewPropInternal(node, ns, name, value, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001966}
1967
1968/**
Daniel Veillard46de64e2002-05-29 08:21:33 +00001969 * xmlNewNsPropEatName:
1970 * @node: the holding node
1971 * @ns: the namespace
1972 * @name: the name of the attribute
1973 * @value: the value of the attribute
1974 *
1975 * Create a new property tagged with a namespace and carried by a node.
1976 * Returns a pointer to the attribute
1977 */
1978xmlAttrPtr
1979xmlNewNsPropEatName(xmlNodePtr node, xmlNsPtr ns, xmlChar *name,
1980 const xmlChar *value) {
Daniel Veillard46de64e2002-05-29 08:21:33 +00001981
1982 if (name == NULL) {
1983#ifdef DEBUG_TREE
1984 xmlGenericError(xmlGenericErrorContext,
1985 "xmlNewNsPropEatName : name == NULL\n");
1986#endif
1987 return(NULL);
1988 }
1989
Daniel Veillard8f6c2b12008-04-03 11:17:21 +00001990 return xmlNewPropInternal(node, ns, name, value, 1);
Daniel Veillard46de64e2002-05-29 08:21:33 +00001991}
1992
1993/**
Owen Taylor3473f882001-02-23 17:55:21 +00001994 * xmlNewDocProp:
1995 * @doc: the document
1996 * @name: the name of the attribute
1997 * @value: the value of the attribute
1998 *
1999 * Create a new property carried by a document.
2000 * Returns a pointer to the attribute
2001 */
2002xmlAttrPtr
2003xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) {
2004 xmlAttrPtr cur;
2005
2006 if (name == NULL) {
2007#ifdef DEBUG_TREE
2008 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00002009 "xmlNewDocProp : name == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002010#endif
2011 return(NULL);
2012 }
2013
2014 /*
2015 * Allocate a new property and fill the fields.
2016 */
2017 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
2018 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002019 xmlTreeErrMemory("building attribute");
Owen Taylor3473f882001-02-23 17:55:21 +00002020 return(NULL);
2021 }
2022 memset(cur, 0, sizeof(xmlAttr));
2023 cur->type = XML_ATTRIBUTE_NODE;
2024
Daniel Veillard03a53c32004-10-26 16:06:51 +00002025 if ((doc != NULL) && (doc->dict != NULL))
2026 cur->name = xmlDictLookup(doc->dict, name, -1);
2027 else
2028 cur->name = xmlStrdup(name);
Daniel Veillardaa6de472008-08-25 14:53:31 +00002029 cur->doc = doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002030 if (value != NULL) {
2031 xmlNodePtr tmp;
2032
2033 cur->children = xmlStringGetNodeList(doc, value);
2034 cur->last = NULL;
2035
2036 tmp = cur->children;
2037 while (tmp != NULL) {
2038 tmp->parent = (xmlNodePtr) cur;
2039 if (tmp->next == NULL)
2040 cur->last = tmp;
2041 tmp = tmp->next;
2042 }
2043 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002044
Daniel Veillarda880b122003-04-21 21:36:41 +00002045 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002046 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002047 return(cur);
2048}
2049
2050/**
2051 * xmlFreePropList:
2052 * @cur: the first property in the list
2053 *
2054 * Free a property and all its siblings, all the children are freed too.
2055 */
2056void
2057xmlFreePropList(xmlAttrPtr cur) {
2058 xmlAttrPtr next;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002059 if (cur == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +00002060 while (cur != NULL) {
2061 next = cur->next;
2062 xmlFreeProp(cur);
2063 cur = next;
2064 }
2065}
2066
2067/**
2068 * xmlFreeProp:
2069 * @cur: an attribute
2070 *
2071 * Free one attribute, all the content is freed too
2072 */
2073void
2074xmlFreeProp(xmlAttrPtr cur) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002075 xmlDictPtr dict = NULL;
2076 if (cur == NULL) return;
2077
2078 if (cur->doc != NULL) dict = cur->doc->dict;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002079
Daniel Veillarda880b122003-04-21 21:36:41 +00002080 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002081 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
2082
Owen Taylor3473f882001-02-23 17:55:21 +00002083 /* Check for ID removal -> leading to invalid references ! */
Daniel Veillardda6f4af2005-06-20 17:17:54 +00002084 if ((cur->doc != NULL) && (cur->atype == XML_ATTRIBUTE_ID)) {
2085 xmlRemoveID(cur->doc, cur);
Daniel Veillard76d66f42001-05-16 21:05:17 +00002086 }
Owen Taylor3473f882001-02-23 17:55:21 +00002087 if (cur->children != NULL) xmlFreeNodeList(cur->children);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002088 DICT_FREE(cur->name)
Owen Taylor3473f882001-02-23 17:55:21 +00002089 xmlFree(cur);
2090}
2091
2092/**
2093 * xmlRemoveProp:
2094 * @cur: an attribute
2095 *
2096 * Unlink and free one attribute, all the content is freed too
2097 * Note this doesn't work for namespace definition attributes
2098 *
2099 * Returns 0 if success and -1 in case of error.
2100 */
2101int
2102xmlRemoveProp(xmlAttrPtr cur) {
2103 xmlAttrPtr tmp;
2104 if (cur == NULL) {
2105#ifdef DEBUG_TREE
2106 xmlGenericError(xmlGenericErrorContext,
2107 "xmlRemoveProp : cur == NULL\n");
2108#endif
2109 return(-1);
2110 }
2111 if (cur->parent == NULL) {
2112#ifdef DEBUG_TREE
2113 xmlGenericError(xmlGenericErrorContext,
2114 "xmlRemoveProp : cur->parent == NULL\n");
2115#endif
2116 return(-1);
2117 }
2118 tmp = cur->parent->properties;
2119 if (tmp == cur) {
2120 cur->parent->properties = cur->next;
Rob Richards19dc9612005-10-28 16:15:16 +00002121 if (cur->next != NULL)
2122 cur->next->prev = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002123 xmlFreeProp(cur);
2124 return(0);
2125 }
2126 while (tmp != NULL) {
2127 if (tmp->next == cur) {
2128 tmp->next = cur->next;
2129 if (tmp->next != NULL)
2130 tmp->next->prev = tmp;
2131 xmlFreeProp(cur);
2132 return(0);
2133 }
2134 tmp = tmp->next;
2135 }
2136#ifdef DEBUG_TREE
2137 xmlGenericError(xmlGenericErrorContext,
2138 "xmlRemoveProp : attribute not owned by its node\n");
2139#endif
2140 return(-1);
2141}
2142
2143/**
Daniel Veillard03a53c32004-10-26 16:06:51 +00002144 * xmlNewDocPI:
2145 * @doc: the target document
Owen Taylor3473f882001-02-23 17:55:21 +00002146 * @name: the processing instruction name
2147 * @content: the PI content
2148 *
2149 * Creation of a processing instruction element.
2150 * Returns a pointer to the new node object.
2151 */
2152xmlNodePtr
Daniel Veillard03a53c32004-10-26 16:06:51 +00002153xmlNewDocPI(xmlDocPtr doc, const xmlChar *name, const xmlChar *content) {
Owen Taylor3473f882001-02-23 17:55:21 +00002154 xmlNodePtr cur;
2155
2156 if (name == NULL) {
2157#ifdef DEBUG_TREE
2158 xmlGenericError(xmlGenericErrorContext,
2159 "xmlNewPI : name == NULL\n");
2160#endif
2161 return(NULL);
2162 }
2163
2164 /*
2165 * Allocate a new node and fill the fields.
2166 */
2167 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2168 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002169 xmlTreeErrMemory("building PI");
Owen Taylor3473f882001-02-23 17:55:21 +00002170 return(NULL);
2171 }
2172 memset(cur, 0, sizeof(xmlNode));
2173 cur->type = XML_PI_NODE;
2174
Daniel Veillard03a53c32004-10-26 16:06:51 +00002175 if ((doc != NULL) && (doc->dict != NULL))
2176 cur->name = xmlDictLookup(doc->dict, name, -1);
2177 else
2178 cur->name = xmlStrdup(name);
Owen Taylor3473f882001-02-23 17:55:21 +00002179 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002180 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00002181 }
Daniel Veillarda03e3652004-11-02 18:45:30 +00002182 cur->doc = doc;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002183
Daniel Veillarda880b122003-04-21 21:36:41 +00002184 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002185 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002186 return(cur);
2187}
2188
2189/**
Daniel Veillard03a53c32004-10-26 16:06:51 +00002190 * xmlNewPI:
2191 * @name: the processing instruction name
2192 * @content: the PI content
2193 *
2194 * Creation of a processing instruction element.
2195 * Use xmlDocNewPI preferably to get string interning
2196 *
2197 * Returns a pointer to the new node object.
2198 */
2199xmlNodePtr
2200xmlNewPI(const xmlChar *name, const xmlChar *content) {
2201 return(xmlNewDocPI(NULL, name, content));
2202}
2203
2204/**
Owen Taylor3473f882001-02-23 17:55:21 +00002205 * xmlNewNode:
2206 * @ns: namespace if any
2207 * @name: the node name
2208 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002209 * Creation of a new node element. @ns is optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00002210 *
William M. Brackd7cf7f82003-11-14 07:13:16 +00002211 * Returns a pointer to the new node object. Uses xmlStrdup() to make
2212 * copy of @name.
Owen Taylor3473f882001-02-23 17:55:21 +00002213 */
2214xmlNodePtr
2215xmlNewNode(xmlNsPtr ns, const xmlChar *name) {
2216 xmlNodePtr cur;
2217
2218 if (name == NULL) {
2219#ifdef DEBUG_TREE
2220 xmlGenericError(xmlGenericErrorContext,
2221 "xmlNewNode : name == NULL\n");
2222#endif
2223 return(NULL);
2224 }
2225
2226 /*
2227 * Allocate a new node and fill the fields.
2228 */
2229 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2230 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002231 xmlTreeErrMemory("building node");
Owen Taylor3473f882001-02-23 17:55:21 +00002232 return(NULL);
2233 }
2234 memset(cur, 0, sizeof(xmlNode));
2235 cur->type = XML_ELEMENT_NODE;
Daniel Veillardaa6de472008-08-25 14:53:31 +00002236
Owen Taylor3473f882001-02-23 17:55:21 +00002237 cur->name = xmlStrdup(name);
2238 cur->ns = ns;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002239
Daniel Veillarda880b122003-04-21 21:36:41 +00002240 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002241 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002242 return(cur);
2243}
2244
2245/**
Daniel Veillard46de64e2002-05-29 08:21:33 +00002246 * xmlNewNodeEatName:
2247 * @ns: namespace if any
2248 * @name: the node name
2249 *
2250 * Creation of a new node element. @ns is optional (NULL).
2251 *
William M. Brackd7cf7f82003-11-14 07:13:16 +00002252 * Returns a pointer to the new node object, with pointer @name as
2253 * new node's name. Use xmlNewNode() if a copy of @name string is
2254 * is needed as new node's name.
Daniel Veillard46de64e2002-05-29 08:21:33 +00002255 */
2256xmlNodePtr
2257xmlNewNodeEatName(xmlNsPtr ns, xmlChar *name) {
2258 xmlNodePtr cur;
2259
2260 if (name == NULL) {
2261#ifdef DEBUG_TREE
2262 xmlGenericError(xmlGenericErrorContext,
2263 "xmlNewNode : name == NULL\n");
2264#endif
2265 return(NULL);
2266 }
2267
2268 /*
2269 * Allocate a new node and fill the fields.
2270 */
2271 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2272 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002273 xmlTreeErrMemory("building node");
Xin Li28c53d32017-03-07 00:33:02 +00002274 /* we can't check here that name comes from the doc dictionary */
Daniel Veillard46de64e2002-05-29 08:21:33 +00002275 return(NULL);
2276 }
2277 memset(cur, 0, sizeof(xmlNode));
2278 cur->type = XML_ELEMENT_NODE;
Daniel Veillardaa6de472008-08-25 14:53:31 +00002279
Daniel Veillard46de64e2002-05-29 08:21:33 +00002280 cur->name = name;
2281 cur->ns = ns;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002282
Daniel Veillarda880b122003-04-21 21:36:41 +00002283 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002284 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Daniel Veillard46de64e2002-05-29 08:21:33 +00002285 return(cur);
2286}
2287
2288/**
Owen Taylor3473f882001-02-23 17:55:21 +00002289 * xmlNewDocNode:
2290 * @doc: the document
2291 * @ns: namespace if any
2292 * @name: the node name
2293 * @content: the XML text content if any
2294 *
2295 * Creation of a new node element within a document. @ns and @content
Daniel Veillardd1640922001-12-17 15:30:10 +00002296 * are optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00002297 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2298 * references, but XML special chars need to be escaped first by using
2299 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2300 * need entities support.
2301 *
2302 * Returns a pointer to the new node object.
2303 */
2304xmlNodePtr
2305xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns,
2306 const xmlChar *name, const xmlChar *content) {
2307 xmlNodePtr cur;
2308
Daniel Veillard95ddcd32004-10-26 21:53:55 +00002309 if ((doc != NULL) && (doc->dict != NULL))
Daniel Veillard03a53c32004-10-26 16:06:51 +00002310 cur = xmlNewNodeEatName(ns, (xmlChar *)
2311 xmlDictLookup(doc->dict, name, -1));
2312 else
2313 cur = xmlNewNode(ns, name);
Owen Taylor3473f882001-02-23 17:55:21 +00002314 if (cur != NULL) {
2315 cur->doc = doc;
2316 if (content != NULL) {
2317 cur->children = xmlStringGetNodeList(doc, content);
2318 UPDATE_LAST_CHILD_AND_PARENT(cur)
2319 }
2320 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002321
Owen Taylor3473f882001-02-23 17:55:21 +00002322 return(cur);
2323}
2324
Daniel Veillard46de64e2002-05-29 08:21:33 +00002325/**
2326 * xmlNewDocNodeEatName:
2327 * @doc: the document
2328 * @ns: namespace if any
2329 * @name: the node name
2330 * @content: the XML text content if any
2331 *
2332 * Creation of a new node element within a document. @ns and @content
2333 * are optional (NULL).
2334 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2335 * references, but XML special chars need to be escaped first by using
2336 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2337 * need entities support.
2338 *
2339 * Returns a pointer to the new node object.
2340 */
2341xmlNodePtr
2342xmlNewDocNodeEatName(xmlDocPtr doc, xmlNsPtr ns,
2343 xmlChar *name, const xmlChar *content) {
2344 xmlNodePtr cur;
2345
2346 cur = xmlNewNodeEatName(ns, name);
2347 if (cur != NULL) {
2348 cur->doc = doc;
2349 if (content != NULL) {
2350 cur->children = xmlStringGetNodeList(doc, content);
2351 UPDATE_LAST_CHILD_AND_PARENT(cur)
2352 }
Daniel Veillard8f6c2b12008-04-03 11:17:21 +00002353 } else {
Xin Li28c53d32017-03-07 00:33:02 +00002354 /* if name don't come from the doc dictionary free it here */
Daniel Veillard8f6c2b12008-04-03 11:17:21 +00002355 if ((name != NULL) && (doc != NULL) &&
2356 (!(xmlDictOwns(doc->dict, name))))
2357 xmlFree(name);
Daniel Veillard46de64e2002-05-29 08:21:33 +00002358 }
2359 return(cur);
2360}
2361
Daniel Veillard652327a2003-09-29 18:02:38 +00002362#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002363/**
2364 * xmlNewDocRawNode:
2365 * @doc: the document
2366 * @ns: namespace if any
2367 * @name: the node name
2368 * @content: the text content if any
2369 *
2370 * Creation of a new node element within a document. @ns and @content
Daniel Veillardd1640922001-12-17 15:30:10 +00002371 * are optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00002372 *
2373 * Returns a pointer to the new node object.
2374 */
2375xmlNodePtr
2376xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns,
2377 const xmlChar *name, const xmlChar *content) {
2378 xmlNodePtr cur;
2379
Daniel Veillard95ddcd32004-10-26 21:53:55 +00002380 cur = xmlNewDocNode(doc, ns, name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002381 if (cur != NULL) {
2382 cur->doc = doc;
2383 if (content != NULL) {
2384 cur->children = xmlNewDocText(doc, content);
2385 UPDATE_LAST_CHILD_AND_PARENT(cur)
2386 }
2387 }
2388 return(cur);
2389}
2390
2391/**
2392 * xmlNewDocFragment:
2393 * @doc: the document owning the fragment
2394 *
2395 * Creation of a new Fragment node.
2396 * Returns a pointer to the new node object.
2397 */
2398xmlNodePtr
2399xmlNewDocFragment(xmlDocPtr doc) {
2400 xmlNodePtr cur;
2401
2402 /*
2403 * Allocate a new DocumentFragment node and fill the fields.
2404 */
2405 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2406 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002407 xmlTreeErrMemory("building fragment");
Owen Taylor3473f882001-02-23 17:55:21 +00002408 return(NULL);
2409 }
2410 memset(cur, 0, sizeof(xmlNode));
2411 cur->type = XML_DOCUMENT_FRAG_NODE;
2412
2413 cur->doc = doc;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002414
Daniel Veillarda880b122003-04-21 21:36:41 +00002415 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002416 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002417 return(cur);
2418}
Daniel Veillard652327a2003-09-29 18:02:38 +00002419#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002420
2421/**
2422 * xmlNewText:
2423 * @content: the text content
2424 *
2425 * Creation of a new text node.
2426 * Returns a pointer to the new node object.
2427 */
2428xmlNodePtr
2429xmlNewText(const xmlChar *content) {
2430 xmlNodePtr cur;
2431
2432 /*
2433 * Allocate a new node and fill the fields.
2434 */
2435 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2436 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002437 xmlTreeErrMemory("building text");
Owen Taylor3473f882001-02-23 17:55:21 +00002438 return(NULL);
2439 }
2440 memset(cur, 0, sizeof(xmlNode));
2441 cur->type = XML_TEXT_NODE;
2442
2443 cur->name = xmlStringText;
2444 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002445 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00002446 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002447
Daniel Veillarda880b122003-04-21 21:36:41 +00002448 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002449 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002450 return(cur);
2451}
2452
Daniel Veillard652327a2003-09-29 18:02:38 +00002453#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002454/**
2455 * xmlNewTextChild:
2456 * @parent: the parent node
2457 * @ns: a namespace if any
2458 * @name: the name of the child
2459 * @content: the text content of the child if any.
2460 *
2461 * Creation of a new child element, added at the end of @parent children list.
MST 2003 John Flecke1f70492003-12-20 23:43:28 +00002462 * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
2463 * created element inherits the namespace of @parent. If @content is non NULL,
William M. Brackd7cf7f82003-11-14 07:13:16 +00002464 * a child TEXT node will be created containing the string @content.
MST 2003 John Fleck8b03bc52003-12-17 03:45:01 +00002465 * NOTE: Use xmlNewChild() if @content will contain entities that need to be
2466 * preserved. Use this function, xmlNewTextChild(), if you need to ensure that
Daniel Veillardaa6de472008-08-25 14:53:31 +00002467 * reserved XML chars that might appear in @content, such as the ampersand,
2468 * greater-than or less-than signs, are automatically replaced by their XML
2469 * escaped entity representations.
Owen Taylor3473f882001-02-23 17:55:21 +00002470 *
2471 * Returns a pointer to the new node object.
2472 */
2473xmlNodePtr
2474xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns,
2475 const xmlChar *name, const xmlChar *content) {
2476 xmlNodePtr cur, prev;
2477
2478 if (parent == NULL) {
2479#ifdef DEBUG_TREE
2480 xmlGenericError(xmlGenericErrorContext,
2481 "xmlNewTextChild : parent == NULL\n");
2482#endif
2483 return(NULL);
2484 }
2485
2486 if (name == NULL) {
2487#ifdef DEBUG_TREE
2488 xmlGenericError(xmlGenericErrorContext,
2489 "xmlNewTextChild : name == NULL\n");
2490#endif
2491 return(NULL);
2492 }
2493
2494 /*
2495 * Allocate a new node
2496 */
Daniel Veillard254b1262003-11-01 17:04:58 +00002497 if (parent->type == XML_ELEMENT_NODE) {
2498 if (ns == NULL)
2499 cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content);
2500 else
2501 cur = xmlNewDocRawNode(parent->doc, ns, name, content);
2502 } else if ((parent->type == XML_DOCUMENT_NODE) ||
2503 (parent->type == XML_HTML_DOCUMENT_NODE)) {
2504 if (ns == NULL)
2505 cur = xmlNewDocRawNode((xmlDocPtr) parent, NULL, name, content);
2506 else
2507 cur = xmlNewDocRawNode((xmlDocPtr) parent, ns, name, content);
2508 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2509 cur = xmlNewDocRawNode( parent->doc, ns, name, content);
2510 } else {
2511 return(NULL);
2512 }
Owen Taylor3473f882001-02-23 17:55:21 +00002513 if (cur == NULL) return(NULL);
2514
2515 /*
2516 * add the new element at the end of the children list.
2517 */
2518 cur->type = XML_ELEMENT_NODE;
2519 cur->parent = parent;
2520 cur->doc = parent->doc;
2521 if (parent->children == NULL) {
2522 parent->children = cur;
2523 parent->last = cur;
2524 } else {
2525 prev = parent->last;
2526 prev->next = cur;
2527 cur->prev = prev;
2528 parent->last = cur;
2529 }
2530
2531 return(cur);
2532}
Daniel Veillard652327a2003-09-29 18:02:38 +00002533#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002534
2535/**
2536 * xmlNewCharRef:
2537 * @doc: the document
2538 * @name: the char ref string, starting with # or "&# ... ;"
2539 *
2540 * Creation of a new character reference node.
2541 * Returns a pointer to the new node object.
2542 */
2543xmlNodePtr
2544xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) {
2545 xmlNodePtr cur;
2546
Daniel Veillard36e5cd52004-11-02 14:52:23 +00002547 if (name == NULL)
2548 return(NULL);
2549
Owen Taylor3473f882001-02-23 17:55:21 +00002550 /*
2551 * Allocate a new node and fill the fields.
2552 */
2553 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2554 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002555 xmlTreeErrMemory("building character reference");
Owen Taylor3473f882001-02-23 17:55:21 +00002556 return(NULL);
2557 }
2558 memset(cur, 0, sizeof(xmlNode));
2559 cur->type = XML_ENTITY_REF_NODE;
2560
2561 cur->doc = doc;
2562 if (name[0] == '&') {
2563 int len;
2564 name++;
2565 len = xmlStrlen(name);
2566 if (name[len - 1] == ';')
2567 cur->name = xmlStrndup(name, len - 1);
2568 else
2569 cur->name = xmlStrndup(name, len);
2570 } else
2571 cur->name = xmlStrdup(name);
Daniel Veillard5335dc52003-01-01 20:59:38 +00002572
Daniel Veillarda880b122003-04-21 21:36:41 +00002573 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002574 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002575 return(cur);
2576}
2577
2578/**
2579 * xmlNewReference:
2580 * @doc: the document
2581 * @name: the reference name, or the reference string with & and ;
2582 *
2583 * Creation of a new reference node.
2584 * Returns a pointer to the new node object.
2585 */
2586xmlNodePtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +08002587xmlNewReference(const xmlDoc *doc, const xmlChar *name) {
Owen Taylor3473f882001-02-23 17:55:21 +00002588 xmlNodePtr cur;
2589 xmlEntityPtr ent;
2590
Daniel Veillard36e5cd52004-11-02 14:52:23 +00002591 if (name == NULL)
2592 return(NULL);
2593
Owen Taylor3473f882001-02-23 17:55:21 +00002594 /*
2595 * Allocate a new node and fill the fields.
2596 */
2597 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2598 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002599 xmlTreeErrMemory("building reference");
Owen Taylor3473f882001-02-23 17:55:21 +00002600 return(NULL);
2601 }
2602 memset(cur, 0, sizeof(xmlNode));
2603 cur->type = XML_ENTITY_REF_NODE;
2604
Kurt Roeckx95ebe532014-10-13 16:06:21 +08002605 cur->doc = (xmlDoc *)doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002606 if (name[0] == '&') {
2607 int len;
2608 name++;
2609 len = xmlStrlen(name);
2610 if (name[len - 1] == ';')
2611 cur->name = xmlStrndup(name, len - 1);
2612 else
2613 cur->name = xmlStrndup(name, len);
2614 } else
2615 cur->name = xmlStrdup(name);
2616
2617 ent = xmlGetDocEntity(doc, cur->name);
2618 if (ent != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002619 cur->content = ent->content;
Owen Taylor3473f882001-02-23 17:55:21 +00002620 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002621 * The parent pointer in entity is a DTD pointer and thus is NOT
Owen Taylor3473f882001-02-23 17:55:21 +00002622 * updated. Not sure if this is 100% correct.
2623 * -George
2624 */
2625 cur->children = (xmlNodePtr) ent;
2626 cur->last = (xmlNodePtr) ent;
2627 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002628
Daniel Veillarda880b122003-04-21 21:36:41 +00002629 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002630 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002631 return(cur);
2632}
2633
2634/**
2635 * xmlNewDocText:
2636 * @doc: the document
2637 * @content: the text content
2638 *
2639 * Creation of a new text node within a document.
2640 * Returns a pointer to the new node object.
2641 */
2642xmlNodePtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +08002643xmlNewDocText(const xmlDoc *doc, const xmlChar *content) {
Owen Taylor3473f882001-02-23 17:55:21 +00002644 xmlNodePtr cur;
2645
2646 cur = xmlNewText(content);
Kurt Roeckx95ebe532014-10-13 16:06:21 +08002647 if (cur != NULL) cur->doc = (xmlDoc *)doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002648 return(cur);
2649}
2650
2651/**
2652 * xmlNewTextLen:
2653 * @content: the text content
2654 * @len: the text len.
2655 *
Daniel Veillard60087f32001-10-10 09:45:09 +00002656 * Creation of a new text node with an extra parameter for the content's length
Owen Taylor3473f882001-02-23 17:55:21 +00002657 * Returns a pointer to the new node object.
2658 */
2659xmlNodePtr
2660xmlNewTextLen(const xmlChar *content, int len) {
2661 xmlNodePtr cur;
2662
2663 /*
2664 * Allocate a new node and fill the fields.
2665 */
2666 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2667 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002668 xmlTreeErrMemory("building text");
Owen Taylor3473f882001-02-23 17:55:21 +00002669 return(NULL);
2670 }
2671 memset(cur, 0, sizeof(xmlNode));
2672 cur->type = XML_TEXT_NODE;
2673
2674 cur->name = xmlStringText;
2675 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002676 cur->content = xmlStrndup(content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00002677 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002678
Daniel Veillarda880b122003-04-21 21:36:41 +00002679 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002680 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002681 return(cur);
2682}
2683
2684/**
2685 * xmlNewDocTextLen:
2686 * @doc: the document
2687 * @content: the text content
2688 * @len: the text len.
2689 *
Daniel Veillard60087f32001-10-10 09:45:09 +00002690 * Creation of a new text node with an extra content length parameter. The
Owen Taylor3473f882001-02-23 17:55:21 +00002691 * text node pertain to a given document.
2692 * Returns a pointer to the new node object.
2693 */
2694xmlNodePtr
2695xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) {
2696 xmlNodePtr cur;
2697
2698 cur = xmlNewTextLen(content, len);
2699 if (cur != NULL) cur->doc = doc;
2700 return(cur);
2701}
2702
2703/**
2704 * xmlNewComment:
2705 * @content: the comment content
2706 *
2707 * Creation of a new node containing a comment.
2708 * Returns a pointer to the new node object.
2709 */
2710xmlNodePtr
2711xmlNewComment(const xmlChar *content) {
2712 xmlNodePtr cur;
2713
2714 /*
2715 * Allocate a new node and fill the fields.
2716 */
2717 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2718 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002719 xmlTreeErrMemory("building comment");
Owen Taylor3473f882001-02-23 17:55:21 +00002720 return(NULL);
2721 }
2722 memset(cur, 0, sizeof(xmlNode));
2723 cur->type = XML_COMMENT_NODE;
2724
2725 cur->name = xmlStringComment;
2726 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002727 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00002728 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002729
Daniel Veillarda880b122003-04-21 21:36:41 +00002730 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002731 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002732 return(cur);
2733}
2734
2735/**
2736 * xmlNewCDataBlock:
2737 * @doc: the document
Daniel Veillardd1640922001-12-17 15:30:10 +00002738 * @content: the CDATA block content content
Owen Taylor3473f882001-02-23 17:55:21 +00002739 * @len: the length of the block
2740 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002741 * Creation of a new node containing a CDATA block.
Owen Taylor3473f882001-02-23 17:55:21 +00002742 * Returns a pointer to the new node object.
2743 */
2744xmlNodePtr
2745xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) {
2746 xmlNodePtr cur;
2747
2748 /*
2749 * Allocate a new node and fill the fields.
2750 */
2751 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2752 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002753 xmlTreeErrMemory("building CDATA");
Owen Taylor3473f882001-02-23 17:55:21 +00002754 return(NULL);
2755 }
2756 memset(cur, 0, sizeof(xmlNode));
2757 cur->type = XML_CDATA_SECTION_NODE;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002758 cur->doc = doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002759
2760 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002761 cur->content = xmlStrndup(content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00002762 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002763
Daniel Veillarda880b122003-04-21 21:36:41 +00002764 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002765 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002766 return(cur);
2767}
2768
2769/**
2770 * xmlNewDocComment:
2771 * @doc: the document
2772 * @content: the comment content
2773 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002774 * Creation of a new node containing a comment within a document.
Owen Taylor3473f882001-02-23 17:55:21 +00002775 * Returns a pointer to the new node object.
2776 */
2777xmlNodePtr
2778xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) {
2779 xmlNodePtr cur;
2780
2781 cur = xmlNewComment(content);
2782 if (cur != NULL) cur->doc = doc;
2783 return(cur);
2784}
2785
2786/**
2787 * xmlSetTreeDoc:
2788 * @tree: the top element
2789 * @doc: the document
2790 *
2791 * update all nodes under the tree to point to the right document
2792 */
2793void
2794xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) {
Daniel Veillard19e96c32001-07-09 10:32:59 +00002795 xmlAttrPtr prop;
2796
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002797 if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +00002798 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002799 if (tree->doc != doc) {
Daniel Veillard36065812002-01-24 15:02:46 +00002800 if(tree->type == XML_ELEMENT_NODE) {
2801 prop = tree->properties;
2802 while (prop != NULL) {
Xin Li28c53d32017-03-07 00:33:02 +00002803 if (prop->atype == XML_ATTRIBUTE_ID) {
2804 xmlRemoveID(tree->doc, prop);
2805 }
2806
Daniel Veillard36065812002-01-24 15:02:46 +00002807 prop->doc = doc;
2808 xmlSetListDoc(prop->children, doc);
Xin Li28c53d32017-03-07 00:33:02 +00002809
2810 /*
2811 * TODO: ID attributes should be also added to the new
2812 * document, but this breaks things like xmlReplaceNode.
2813 * The underlying problem is that xmlRemoveID is only called
2814 * if a node is destroyed, not if it's unlinked.
2815 */
2816#if 0
2817 if (xmlIsID(doc, tree, prop)) {
2818 xmlChar *idVal = xmlNodeListGetString(doc, prop->children,
2819 1);
2820 xmlAddID(NULL, doc, idVal, prop);
2821 }
2822#endif
2823
Daniel Veillard36065812002-01-24 15:02:46 +00002824 prop = prop->next;
2825 }
Daniel Veillard19e96c32001-07-09 10:32:59 +00002826 }
Owen Taylor3473f882001-02-23 17:55:21 +00002827 if (tree->children != NULL)
2828 xmlSetListDoc(tree->children, doc);
2829 tree->doc = doc;
2830 }
2831}
2832
2833/**
2834 * xmlSetListDoc:
Daniel Veillardd1640922001-12-17 15:30:10 +00002835 * @list: the first element
Owen Taylor3473f882001-02-23 17:55:21 +00002836 * @doc: the document
2837 *
2838 * update all nodes in the list to point to the right document
2839 */
2840void
2841xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) {
2842 xmlNodePtr cur;
2843
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002844 if ((list == NULL) || (list->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +00002845 return;
2846 cur = list;
2847 while (cur != NULL) {
2848 if (cur->doc != doc)
2849 xmlSetTreeDoc(cur, doc);
2850 cur = cur->next;
2851 }
2852}
2853
Daniel Veillard2156d432004-03-04 15:59:36 +00002854#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00002855/**
2856 * xmlNewChild:
2857 * @parent: the parent node
2858 * @ns: a namespace if any
2859 * @name: the name of the child
2860 * @content: the XML content of the child if any.
2861 *
2862 * Creation of a new child element, added at the end of @parent children list.
MST 2003 John Flecke1f70492003-12-20 23:43:28 +00002863 * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
2864 * created element inherits the namespace of @parent. If @content is non NULL,
Owen Taylor3473f882001-02-23 17:55:21 +00002865 * a child list containing the TEXTs and ENTITY_REFs node will be created.
MST 2003 John Fleck8b03bc52003-12-17 03:45:01 +00002866 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
2867 * references. XML special chars must be escaped first by using
2868 * xmlEncodeEntitiesReentrant(), or xmlNewTextChild() should be used.
Owen Taylor3473f882001-02-23 17:55:21 +00002869 *
2870 * Returns a pointer to the new node object.
2871 */
2872xmlNodePtr
2873xmlNewChild(xmlNodePtr parent, xmlNsPtr ns,
2874 const xmlChar *name, const xmlChar *content) {
2875 xmlNodePtr cur, prev;
2876
2877 if (parent == NULL) {
2878#ifdef DEBUG_TREE
2879 xmlGenericError(xmlGenericErrorContext,
2880 "xmlNewChild : parent == NULL\n");
2881#endif
2882 return(NULL);
2883 }
2884
2885 if (name == NULL) {
2886#ifdef DEBUG_TREE
2887 xmlGenericError(xmlGenericErrorContext,
2888 "xmlNewChild : name == NULL\n");
2889#endif
2890 return(NULL);
2891 }
2892
2893 /*
2894 * Allocate a new node
2895 */
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002896 if (parent->type == XML_ELEMENT_NODE) {
2897 if (ns == NULL)
2898 cur = xmlNewDocNode(parent->doc, parent->ns, name, content);
2899 else
2900 cur = xmlNewDocNode(parent->doc, ns, name, content);
2901 } else if ((parent->type == XML_DOCUMENT_NODE) ||
2902 (parent->type == XML_HTML_DOCUMENT_NODE)) {
2903 if (ns == NULL)
2904 cur = xmlNewDocNode((xmlDocPtr) parent, NULL, name, content);
2905 else
2906 cur = xmlNewDocNode((xmlDocPtr) parent, ns, name, content);
Daniel Veillard7e3f1402002-10-28 18:52:57 +00002907 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2908 cur = xmlNewDocNode( parent->doc, ns, name, content);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002909 } else {
2910 return(NULL);
2911 }
Owen Taylor3473f882001-02-23 17:55:21 +00002912 if (cur == NULL) return(NULL);
2913
2914 /*
2915 * add the new element at the end of the children list.
2916 */
2917 cur->type = XML_ELEMENT_NODE;
2918 cur->parent = parent;
2919 cur->doc = parent->doc;
2920 if (parent->children == NULL) {
2921 parent->children = cur;
2922 parent->last = cur;
2923 } else {
2924 prev = parent->last;
2925 prev->next = cur;
2926 cur->prev = prev;
2927 parent->last = cur;
2928 }
2929
2930 return(cur);
2931}
Daniel Veillard652327a2003-09-29 18:02:38 +00002932#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002933
2934/**
Rob Richards65815122006-02-25 17:13:33 +00002935 * xmlAddPropSibling:
Daniel Veillardaa6de472008-08-25 14:53:31 +00002936 * @prev: the attribute to which @prop is added after
Rob Richards65815122006-02-25 17:13:33 +00002937 * @cur: the base attribute passed to calling function
2938 * @prop: the new attribute
2939 *
2940 * Add a new attribute after @prev using @cur as base attribute.
2941 * When inserting before @cur, @prev is passed as @cur->prev.
2942 * When inserting after @cur, @prev is passed as @cur.
Daniel Veillardaa6de472008-08-25 14:53:31 +00002943 * If an existing attribute is found it is detroyed prior to adding @prop.
Rob Richards65815122006-02-25 17:13:33 +00002944 *
2945 * Returns the attribute being inserted or NULL in case of error.
2946 */
2947static xmlNodePtr
2948xmlAddPropSibling(xmlNodePtr prev, xmlNodePtr cur, xmlNodePtr prop) {
2949 xmlAttrPtr attr;
2950
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002951 if ((cur == NULL) || (cur->type != XML_ATTRIBUTE_NODE) ||
2952 (prop == NULL) || (prop->type != XML_ATTRIBUTE_NODE) ||
2953 ((prev != NULL) && (prev->type != XML_ATTRIBUTE_NODE)))
Rob Richards65815122006-02-25 17:13:33 +00002954 return(NULL);
2955
2956 /* check if an attribute with the same name exists */
2957 if (prop->ns == NULL)
2958 attr = xmlHasNsProp(cur->parent, prop->name, NULL);
2959 else
2960 attr = xmlHasNsProp(cur->parent, prop->name, prop->ns->href);
2961
2962 if (prop->doc != cur->doc) {
2963 xmlSetTreeDoc(prop, cur->doc);
2964 }
2965 prop->parent = cur->parent;
2966 prop->prev = prev;
2967 if (prev != NULL) {
2968 prop->next = prev->next;
2969 prev->next = prop;
2970 if (prop->next)
2971 prop->next->prev = prop;
2972 } else {
2973 prop->next = cur;
2974 cur->prev = prop;
2975 }
2976 if (prop->prev == NULL && prop->parent != NULL)
2977 prop->parent->properties = (xmlAttrPtr) prop;
2978 if ((attr != NULL) && (attr->type != XML_ATTRIBUTE_DECL)) {
2979 /* different instance, destroy it (attributes must be unique) */
2980 xmlRemoveProp((xmlAttrPtr) attr);
2981 }
2982 return prop;
2983}
2984
2985/**
Owen Taylor3473f882001-02-23 17:55:21 +00002986 * xmlAddNextSibling:
2987 * @cur: the child node
2988 * @elem: the new node
2989 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002990 * Add a new node @elem as the next sibling of @cur
2991 * If the new node was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00002992 * first unlinked from its existing context.
2993 * As a result of text merging @elem may be freed.
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002994 * If the new node is ATTRIBUTE, it is added into properties instead of children.
Daniel Veillardaa6de472008-08-25 14:53:31 +00002995 * If there is an attribute with equal name, it is first destroyed.
Owen Taylor3473f882001-02-23 17:55:21 +00002996 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002997 * Returns the new node or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002998 */
2999xmlNodePtr
3000xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) {
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003001 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003002#ifdef DEBUG_TREE
3003 xmlGenericError(xmlGenericErrorContext,
3004 "xmlAddNextSibling : cur == NULL\n");
3005#endif
3006 return(NULL);
3007 }
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003008 if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003009#ifdef DEBUG_TREE
3010 xmlGenericError(xmlGenericErrorContext,
3011 "xmlAddNextSibling : elem == NULL\n");
3012#endif
3013 return(NULL);
3014 }
3015
Rob Richards19dc9612005-10-28 16:15:16 +00003016 if (cur == elem) {
3017#ifdef DEBUG_TREE
3018 xmlGenericError(xmlGenericErrorContext,
3019 "xmlAddNextSibling : cur == elem\n");
3020#endif
3021 return(NULL);
3022 }
3023
Owen Taylor3473f882001-02-23 17:55:21 +00003024 xmlUnlinkNode(elem);
3025
3026 if (elem->type == XML_TEXT_NODE) {
3027 if (cur->type == XML_TEXT_NODE) {
Owen Taylor3473f882001-02-23 17:55:21 +00003028 xmlNodeAddContent(cur, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003029 xmlFreeNode(elem);
3030 return(cur);
3031 }
Daniel Veillard9e1c72d2001-08-31 20:03:19 +00003032 if ((cur->next != NULL) && (cur->next->type == XML_TEXT_NODE) &&
3033 (cur->name == cur->next->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003034 xmlChar *tmp;
3035
3036 tmp = xmlStrdup(elem->content);
3037 tmp = xmlStrcat(tmp, cur->next->content);
3038 xmlNodeSetContent(cur->next, tmp);
3039 xmlFree(tmp);
Owen Taylor3473f882001-02-23 17:55:21 +00003040 xmlFreeNode(elem);
3041 return(cur->next);
3042 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003043 } else if (elem->type == XML_ATTRIBUTE_NODE) {
Rob Richards65815122006-02-25 17:13:33 +00003044 return xmlAddPropSibling(cur, cur, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003045 }
3046
3047 if (elem->doc != cur->doc) {
3048 xmlSetTreeDoc(elem, cur->doc);
3049 }
3050 elem->parent = cur->parent;
3051 elem->prev = cur;
3052 elem->next = cur->next;
3053 cur->next = elem;
3054 if (elem->next != NULL)
3055 elem->next->prev = elem;
Rob Richards65815122006-02-25 17:13:33 +00003056 if ((elem->parent != NULL) && (elem->parent->last == cur))
Owen Taylor3473f882001-02-23 17:55:21 +00003057 elem->parent->last = elem;
3058 return(elem);
3059}
3060
William M. Brack21e4ef22005-01-02 09:53:13 +00003061#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
Nicolas Le Cam41586ca2013-06-17 13:01:33 +02003062 defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00003063/**
3064 * xmlAddPrevSibling:
3065 * @cur: the child node
3066 * @elem: the new node
3067 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003068 * Add a new node @elem as the previous sibling of @cur
Owen Taylor3473f882001-02-23 17:55:21 +00003069 * merging adjacent TEXT nodes (@elem may be freed)
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003070 * If the new node was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00003071 * first unlinked from its existing context.
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003072 * If the new node is ATTRIBUTE, it is added into properties instead of children.
Daniel Veillardaa6de472008-08-25 14:53:31 +00003073 * If there is an attribute with equal name, it is first destroyed.
Owen Taylor3473f882001-02-23 17:55:21 +00003074 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003075 * Returns the new node or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003076 */
3077xmlNodePtr
3078xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) {
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003079 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003080#ifdef DEBUG_TREE
3081 xmlGenericError(xmlGenericErrorContext,
3082 "xmlAddPrevSibling : cur == NULL\n");
3083#endif
3084 return(NULL);
3085 }
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003086 if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003087#ifdef DEBUG_TREE
3088 xmlGenericError(xmlGenericErrorContext,
3089 "xmlAddPrevSibling : elem == NULL\n");
3090#endif
3091 return(NULL);
3092 }
3093
Rob Richards19dc9612005-10-28 16:15:16 +00003094 if (cur == elem) {
3095#ifdef DEBUG_TREE
3096 xmlGenericError(xmlGenericErrorContext,
3097 "xmlAddPrevSibling : cur == elem\n");
3098#endif
3099 return(NULL);
3100 }
3101
Owen Taylor3473f882001-02-23 17:55:21 +00003102 xmlUnlinkNode(elem);
3103
3104 if (elem->type == XML_TEXT_NODE) {
3105 if (cur->type == XML_TEXT_NODE) {
Owen Taylor3473f882001-02-23 17:55:21 +00003106 xmlChar *tmp;
3107
3108 tmp = xmlStrdup(elem->content);
3109 tmp = xmlStrcat(tmp, cur->content);
3110 xmlNodeSetContent(cur, tmp);
3111 xmlFree(tmp);
Owen Taylor3473f882001-02-23 17:55:21 +00003112 xmlFreeNode(elem);
3113 return(cur);
3114 }
Daniel Veillard9e1c72d2001-08-31 20:03:19 +00003115 if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE) &&
3116 (cur->name == cur->prev->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003117 xmlNodeAddContent(cur->prev, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003118 xmlFreeNode(elem);
3119 return(cur->prev);
3120 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003121 } else if (elem->type == XML_ATTRIBUTE_NODE) {
Rob Richards65815122006-02-25 17:13:33 +00003122 return xmlAddPropSibling(cur->prev, cur, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003123 }
3124
3125 if (elem->doc != cur->doc) {
3126 xmlSetTreeDoc(elem, cur->doc);
3127 }
3128 elem->parent = cur->parent;
3129 elem->next = cur;
3130 elem->prev = cur->prev;
3131 cur->prev = elem;
3132 if (elem->prev != NULL)
3133 elem->prev->next = elem;
Rob Richards65815122006-02-25 17:13:33 +00003134 if ((elem->parent != NULL) && (elem->parent->children == cur)) {
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003135 elem->parent->children = elem;
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003136 }
Owen Taylor3473f882001-02-23 17:55:21 +00003137 return(elem);
3138}
Daniel Veillard652327a2003-09-29 18:02:38 +00003139#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00003140
3141/**
3142 * xmlAddSibling:
3143 * @cur: the child node
3144 * @elem: the new node
3145 *
3146 * Add a new element @elem to the list of siblings of @cur
3147 * merging adjacent TEXT nodes (@elem may be freed)
3148 * If the new element was already inserted in a document it is
3149 * first unlinked from its existing context.
3150 *
3151 * Returns the new element or NULL in case of error.
3152 */
3153xmlNodePtr
3154xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) {
3155 xmlNodePtr parent;
3156
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003157 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003158#ifdef DEBUG_TREE
3159 xmlGenericError(xmlGenericErrorContext,
3160 "xmlAddSibling : cur == NULL\n");
3161#endif
3162 return(NULL);
3163 }
3164
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003165 if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003166#ifdef DEBUG_TREE
3167 xmlGenericError(xmlGenericErrorContext,
3168 "xmlAddSibling : elem == NULL\n");
3169#endif
3170 return(NULL);
3171 }
3172
Daniel Veillard43bc89c2009-03-23 19:32:04 +00003173 if (cur == elem) {
3174#ifdef DEBUG_TREE
3175 xmlGenericError(xmlGenericErrorContext,
3176 "xmlAddSibling : cur == elem\n");
3177#endif
3178 return(NULL);
3179 }
3180
Owen Taylor3473f882001-02-23 17:55:21 +00003181 /*
3182 * Constant time is we can rely on the ->parent->last to find
3183 * the last sibling.
3184 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00003185 if ((cur->type != XML_ATTRIBUTE_NODE) && (cur->parent != NULL) &&
Owen Taylor3473f882001-02-23 17:55:21 +00003186 (cur->parent->children != NULL) &&
3187 (cur->parent->last != NULL) &&
3188 (cur->parent->last->next == NULL)) {
3189 cur = cur->parent->last;
3190 } else {
3191 while (cur->next != NULL) cur = cur->next;
3192 }
3193
3194 xmlUnlinkNode(elem);
3195
Daniel Veillarde22dd5c2003-10-29 12:53:27 +00003196 if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE) &&
3197 (cur->name == elem->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003198 xmlNodeAddContent(cur, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003199 xmlFreeNode(elem);
3200 return(cur);
Rob Richards65815122006-02-25 17:13:33 +00003201 } else if (elem->type == XML_ATTRIBUTE_NODE) {
3202 return xmlAddPropSibling(cur, cur, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003203 }
3204
3205 if (elem->doc != cur->doc) {
3206 xmlSetTreeDoc(elem, cur->doc);
3207 }
3208 parent = cur->parent;
3209 elem->prev = cur;
3210 elem->next = NULL;
3211 elem->parent = parent;
3212 cur->next = elem;
3213 if (parent != NULL)
3214 parent->last = elem;
3215
3216 return(elem);
3217}
3218
3219/**
3220 * xmlAddChildList:
3221 * @parent: the parent node
3222 * @cur: the first node in the list
3223 *
3224 * Add a list of node at the end of the child list of the parent
3225 * merging adjacent TEXT nodes (@cur may be freed)
3226 *
3227 * Returns the last child or NULL in case of error.
3228 */
3229xmlNodePtr
3230xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) {
3231 xmlNodePtr prev;
3232
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003233 if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003234#ifdef DEBUG_TREE
3235 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00003236 "xmlAddChildList : parent == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003237#endif
3238 return(NULL);
3239 }
3240
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003241 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003242#ifdef DEBUG_TREE
3243 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00003244 "xmlAddChildList : child == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003245#endif
3246 return(NULL);
3247 }
3248
3249 if ((cur->doc != NULL) && (parent->doc != NULL) &&
3250 (cur->doc != parent->doc)) {
3251#ifdef DEBUG_TREE
3252 xmlGenericError(xmlGenericErrorContext,
3253 "Elements moved to a different document\n");
3254#endif
3255 }
3256
3257 /*
3258 * add the first element at the end of the children list.
3259 */
Daniel Veillard5335dc52003-01-01 20:59:38 +00003260
Owen Taylor3473f882001-02-23 17:55:21 +00003261 if (parent->children == NULL) {
3262 parent->children = cur;
3263 } else {
3264 /*
3265 * If cur and parent->last both are TEXT nodes, then merge them.
3266 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00003267 if ((cur->type == XML_TEXT_NODE) &&
Owen Taylor3473f882001-02-23 17:55:21 +00003268 (parent->last->type == XML_TEXT_NODE) &&
3269 (cur->name == parent->last->name)) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00003270 xmlNodeAddContent(parent->last, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003271 /*
3272 * if it's the only child, nothing more to be done.
3273 */
3274 if (cur->next == NULL) {
3275 xmlFreeNode(cur);
3276 return(parent->last);
3277 }
3278 prev = cur;
3279 cur = cur->next;
3280 xmlFreeNode(prev);
3281 }
3282 prev = parent->last;
3283 prev->next = cur;
3284 cur->prev = prev;
3285 }
3286 while (cur->next != NULL) {
3287 cur->parent = parent;
3288 if (cur->doc != parent->doc) {
3289 xmlSetTreeDoc(cur, parent->doc);
3290 }
3291 cur = cur->next;
3292 }
3293 cur->parent = parent;
Rob Richards810a78b2008-12-31 22:13:57 +00003294 /* the parent may not be linked to a doc ! */
3295 if (cur->doc != parent->doc) {
3296 xmlSetTreeDoc(cur, parent->doc);
3297 }
Owen Taylor3473f882001-02-23 17:55:21 +00003298 parent->last = cur;
3299
3300 return(cur);
3301}
3302
3303/**
3304 * xmlAddChild:
3305 * @parent: the parent node
3306 * @cur: the child node
3307 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003308 * Add a new node to @parent, at the end of the child (or property) list
Owen Taylor3473f882001-02-23 17:55:21 +00003309 * merging adjacent TEXT nodes (in which case @cur is freed)
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003310 * If the new node is ATTRIBUTE, it is added into properties instead of children.
Daniel Veillardaa6de472008-08-25 14:53:31 +00003311 * If there is an attribute with equal name, it is first destroyed.
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003312 *
Owen Taylor3473f882001-02-23 17:55:21 +00003313 * Returns the child or NULL in case of error.
3314 */
3315xmlNodePtr
3316xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) {
3317 xmlNodePtr prev;
3318
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003319 if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003320#ifdef DEBUG_TREE
3321 xmlGenericError(xmlGenericErrorContext,
3322 "xmlAddChild : parent == NULL\n");
3323#endif
3324 return(NULL);
3325 }
3326
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003327 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003328#ifdef DEBUG_TREE
3329 xmlGenericError(xmlGenericErrorContext,
3330 "xmlAddChild : child == NULL\n");
3331#endif
3332 return(NULL);
3333 }
3334
Rob Richards19dc9612005-10-28 16:15:16 +00003335 if (parent == cur) {
3336#ifdef DEBUG_TREE
3337 xmlGenericError(xmlGenericErrorContext,
3338 "xmlAddChild : parent == cur\n");
3339#endif
3340 return(NULL);
3341 }
Owen Taylor3473f882001-02-23 17:55:21 +00003342 /*
3343 * If cur is a TEXT node, merge its content with adjacent TEXT nodes
Owen Taylor3473f882001-02-23 17:55:21 +00003344 * cur is then freed.
3345 */
3346 if (cur->type == XML_TEXT_NODE) {
Daniel Veillard7db37732001-07-12 01:20:08 +00003347 if ((parent->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003348 (parent->content != NULL) &&
Rob Richards19dc9612005-10-28 16:15:16 +00003349 (parent->name == cur->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003350 xmlNodeAddContent(parent, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003351 xmlFreeNode(cur);
3352 return(parent);
3353 }
3354 if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003355 (parent->last->name == cur->name) &&
3356 (parent->last != cur)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003357 xmlNodeAddContent(parent->last, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003358 xmlFreeNode(cur);
3359 return(parent->last);
3360 }
3361 }
3362
3363 /*
3364 * add the new element at the end of the children list.
3365 */
Daniel Veillard5335dc52003-01-01 20:59:38 +00003366 prev = cur->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00003367 cur->parent = parent;
3368 if (cur->doc != parent->doc) {
3369 xmlSetTreeDoc(cur, parent->doc);
3370 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00003371 /* this check prevents a loop on tree-traversions if a developer
3372 * tries to add a node to its parent multiple times
3373 */
3374 if (prev == parent)
3375 return(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00003376
3377 /*
Daniel Veillard7db37732001-07-12 01:20:08 +00003378 * Coalescing
Owen Taylor3473f882001-02-23 17:55:21 +00003379 */
Daniel Veillard7db37732001-07-12 01:20:08 +00003380 if ((parent->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003381 (parent->content != NULL) &&
3382 (parent != cur)) {
Daniel Veillard7db37732001-07-12 01:20:08 +00003383 xmlNodeAddContent(parent, cur->content);
Daniel Veillard7db37732001-07-12 01:20:08 +00003384 xmlFreeNode(cur);
3385 return(parent);
Owen Taylor3473f882001-02-23 17:55:21 +00003386 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003387 if (cur->type == XML_ATTRIBUTE_NODE) {
Rob Richards19dc9612005-10-28 16:15:16 +00003388 if (parent->type != XML_ELEMENT_NODE)
3389 return(NULL);
Rob Richards810a78b2008-12-31 22:13:57 +00003390 if (parent->properties != NULL) {
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003391 /* check if an attribute with the same name exists */
3392 xmlAttrPtr lastattr;
Owen Taylor3473f882001-02-23 17:55:21 +00003393
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003394 if (cur->ns == NULL)
Rob Richardsc342ec62005-10-25 00:10:12 +00003395 lastattr = xmlHasNsProp(parent, cur->name, NULL);
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003396 else
3397 lastattr = xmlHasNsProp(parent, cur->name, cur->ns->href);
Rob Richardsc342ec62005-10-25 00:10:12 +00003398 if ((lastattr != NULL) && (lastattr != (xmlAttrPtr) cur) && (lastattr->type != XML_ATTRIBUTE_DECL)) {
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003399 /* different instance, destroy it (attributes must be unique) */
Rob Richards19dc9612005-10-28 16:15:16 +00003400 xmlUnlinkNode((xmlNodePtr) lastattr);
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003401 xmlFreeProp(lastattr);
3402 }
Rob Richards19dc9612005-10-28 16:15:16 +00003403 if (lastattr == (xmlAttrPtr) cur)
3404 return(cur);
Rob Richards810a78b2008-12-31 22:13:57 +00003405
3406 }
3407 if (parent->properties == NULL) {
3408 parent->properties = (xmlAttrPtr) cur;
3409 } else {
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003410 /* find the end */
Rob Richards810a78b2008-12-31 22:13:57 +00003411 xmlAttrPtr lastattr = parent->properties;
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003412 while (lastattr->next != NULL) {
3413 lastattr = lastattr->next;
3414 }
3415 lastattr->next = (xmlAttrPtr) cur;
3416 ((xmlAttrPtr) cur)->prev = lastattr;
3417 }
3418 } else {
3419 if (parent->children == NULL) {
3420 parent->children = cur;
3421 parent->last = cur;
3422 } else {
3423 prev = parent->last;
3424 prev->next = cur;
3425 cur->prev = prev;
3426 parent->last = cur;
3427 }
3428 }
Owen Taylor3473f882001-02-23 17:55:21 +00003429 return(cur);
3430}
3431
3432/**
3433 * xmlGetLastChild:
3434 * @parent: the parent node
3435 *
3436 * Search the last child of a node.
3437 * Returns the last child or NULL if none.
3438 */
3439xmlNodePtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +08003440xmlGetLastChild(const xmlNode *parent) {
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003441 if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003442#ifdef DEBUG_TREE
3443 xmlGenericError(xmlGenericErrorContext,
3444 "xmlGetLastChild : parent == NULL\n");
3445#endif
3446 return(NULL);
3447 }
3448 return(parent->last);
3449}
3450
Daniel Veillardbe2bd6a2008-11-27 15:26:28 +00003451#ifdef LIBXML_TREE_ENABLED
3452/*
3453 * 5 interfaces from DOM ElementTraversal
3454 */
3455
3456/**
3457 * xmlChildElementCount:
3458 * @parent: the parent node
3459 *
3460 * Finds the current number of child nodes of that element which are
3461 * element nodes.
3462 * Note the handling of entities references is different than in
3463 * the W3C DOM element traversal spec since we don't have back reference
3464 * from entities content to entities references.
3465 *
3466 * Returns the count of element child or 0 if not available
3467 */
3468unsigned long
3469xmlChildElementCount(xmlNodePtr parent) {
3470 unsigned long ret = 0;
3471 xmlNodePtr cur = NULL;
3472
3473 if (parent == NULL)
3474 return(0);
3475 switch (parent->type) {
3476 case XML_ELEMENT_NODE:
3477 case XML_ENTITY_NODE:
3478 case XML_DOCUMENT_NODE:
Kyle VanderBeek1db99692014-07-29 00:32:15 -07003479 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardbe2bd6a2008-11-27 15:26:28 +00003480 case XML_HTML_DOCUMENT_NODE:
3481 cur = parent->children;
3482 break;
3483 default:
3484 return(0);
3485 }
3486 while (cur != NULL) {
3487 if (cur->type == XML_ELEMENT_NODE)
3488 ret++;
3489 cur = cur->next;
3490 }
3491 return(ret);
3492}
3493
3494/**
3495 * xmlFirstElementChild:
3496 * @parent: the parent node
3497 *
3498 * Finds the first child node of that element which is a Element node
3499 * Note the handling of entities references is different than in
3500 * the W3C DOM element traversal spec since we don't have back reference
3501 * from entities content to entities references.
3502 *
3503 * Returns the first element child or NULL if not available
3504 */
3505xmlNodePtr
3506xmlFirstElementChild(xmlNodePtr parent) {
3507 xmlNodePtr cur = NULL;
3508
3509 if (parent == NULL)
3510 return(NULL);
3511 switch (parent->type) {
3512 case XML_ELEMENT_NODE:
3513 case XML_ENTITY_NODE:
3514 case XML_DOCUMENT_NODE:
Kyle VanderBeek1db99692014-07-29 00:32:15 -07003515 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardbe2bd6a2008-11-27 15:26:28 +00003516 case XML_HTML_DOCUMENT_NODE:
3517 cur = parent->children;
3518 break;
3519 default:
3520 return(NULL);
3521 }
3522 while (cur != NULL) {
3523 if (cur->type == XML_ELEMENT_NODE)
3524 return(cur);
3525 cur = cur->next;
3526 }
3527 return(NULL);
3528}
3529
3530/**
3531 * xmlLastElementChild:
3532 * @parent: the parent node
3533 *
3534 * Finds the last child node of that element which is a Element node
3535 * Note the handling of entities references is different than in
3536 * the W3C DOM element traversal spec since we don't have back reference
3537 * from entities content to entities references.
3538 *
3539 * Returns the last element child or NULL if not available
3540 */
3541xmlNodePtr
3542xmlLastElementChild(xmlNodePtr parent) {
3543 xmlNodePtr cur = NULL;
3544
3545 if (parent == NULL)
3546 return(NULL);
3547 switch (parent->type) {
3548 case XML_ELEMENT_NODE:
3549 case XML_ENTITY_NODE:
3550 case XML_DOCUMENT_NODE:
Kyle VanderBeek1db99692014-07-29 00:32:15 -07003551 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardbe2bd6a2008-11-27 15:26:28 +00003552 case XML_HTML_DOCUMENT_NODE:
3553 cur = parent->last;
3554 break;
3555 default:
3556 return(NULL);
3557 }
3558 while (cur != NULL) {
3559 if (cur->type == XML_ELEMENT_NODE)
3560 return(cur);
3561 cur = cur->prev;
3562 }
3563 return(NULL);
3564}
3565
3566/**
3567 * xmlPreviousElementSibling:
3568 * @node: the current node
3569 *
3570 * Finds the first closest previous sibling of the node which is an
3571 * element node.
3572 * Note the handling of entities references is different than in
3573 * the W3C DOM element traversal spec since we don't have back reference
3574 * from entities content to entities references.
3575 *
3576 * Returns the previous element sibling or NULL if not available
3577 */
3578xmlNodePtr
3579xmlPreviousElementSibling(xmlNodePtr node) {
3580 if (node == NULL)
3581 return(NULL);
3582 switch (node->type) {
3583 case XML_ELEMENT_NODE:
3584 case XML_TEXT_NODE:
3585 case XML_CDATA_SECTION_NODE:
3586 case XML_ENTITY_REF_NODE:
3587 case XML_ENTITY_NODE:
3588 case XML_PI_NODE:
3589 case XML_COMMENT_NODE:
3590 case XML_XINCLUDE_START:
3591 case XML_XINCLUDE_END:
3592 node = node->prev;
3593 break;
3594 default:
3595 return(NULL);
3596 }
3597 while (node != NULL) {
3598 if (node->type == XML_ELEMENT_NODE)
3599 return(node);
François Delyon2f700902010-02-03 17:32:37 +01003600 node = node->prev;
Daniel Veillardbe2bd6a2008-11-27 15:26:28 +00003601 }
3602 return(NULL);
3603}
3604
3605/**
3606 * xmlNextElementSibling:
3607 * @node: the current node
3608 *
3609 * Finds the first closest next sibling of the node which is an
3610 * element node.
3611 * Note the handling of entities references is different than in
3612 * the W3C DOM element traversal spec since we don't have back reference
3613 * from entities content to entities references.
3614 *
3615 * Returns the next element sibling or NULL if not available
3616 */
3617xmlNodePtr
3618xmlNextElementSibling(xmlNodePtr node) {
3619 if (node == NULL)
3620 return(NULL);
3621 switch (node->type) {
3622 case XML_ELEMENT_NODE:
3623 case XML_TEXT_NODE:
3624 case XML_CDATA_SECTION_NODE:
3625 case XML_ENTITY_REF_NODE:
3626 case XML_ENTITY_NODE:
3627 case XML_PI_NODE:
3628 case XML_COMMENT_NODE:
3629 case XML_DTD_NODE:
3630 case XML_XINCLUDE_START:
3631 case XML_XINCLUDE_END:
3632 node = node->next;
3633 break;
3634 default:
3635 return(NULL);
3636 }
3637 while (node != NULL) {
3638 if (node->type == XML_ELEMENT_NODE)
3639 return(node);
3640 node = node->next;
3641 }
3642 return(NULL);
3643}
3644
3645#endif /* LIBXML_TREE_ENABLED */
3646
Owen Taylor3473f882001-02-23 17:55:21 +00003647/**
3648 * xmlFreeNodeList:
3649 * @cur: the first node in the list
3650 *
3651 * Free a node and all its siblings, this is a recursive behaviour, all
3652 * the children are freed too.
3653 */
3654void
3655xmlFreeNodeList(xmlNodePtr cur) {
3656 xmlNodePtr next;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003657 xmlDictPtr dict = NULL;
3658
3659 if (cur == NULL) return;
Daniel Veillarde6a55192002-01-14 17:11:53 +00003660 if (cur->type == XML_NAMESPACE_DECL) {
3661 xmlFreeNsList((xmlNsPtr) cur);
3662 return;
3663 }
Daniel Veillard9adc0462003-03-24 18:39:54 +00003664 if ((cur->type == XML_DOCUMENT_NODE) ||
3665#ifdef LIBXML_DOCB_ENABLED
3666 (cur->type == XML_DOCB_DOCUMENT_NODE) ||
Daniel Veillard9adc0462003-03-24 18:39:54 +00003667#endif
Daniel Veillard6560a422003-03-27 21:25:38 +00003668 (cur->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard9adc0462003-03-24 18:39:54 +00003669 xmlFreeDoc((xmlDocPtr) cur);
3670 return;
3671 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003672 if (cur->doc != NULL) dict = cur->doc->dict;
Owen Taylor3473f882001-02-23 17:55:21 +00003673 while (cur != NULL) {
3674 next = cur->next;
Daniel Veillard02141ea2001-04-30 11:46:40 +00003675 if (cur->type != XML_DTD_NODE) {
Daniel Veillard5335dc52003-01-01 20:59:38 +00003676
Daniel Veillarda880b122003-04-21 21:36:41 +00003677 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00003678 xmlDeregisterNodeDefaultValue(cur);
3679
Daniel Veillard02141ea2001-04-30 11:46:40 +00003680 if ((cur->children != NULL) &&
3681 (cur->type != XML_ENTITY_REF_NODE))
3682 xmlFreeNodeList(cur->children);
Daniel Veillard01c13b52002-12-10 15:19:08 +00003683 if (((cur->type == XML_ELEMENT_NODE) ||
3684 (cur->type == XML_XINCLUDE_START) ||
3685 (cur->type == XML_XINCLUDE_END)) &&
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003686 (cur->properties != NULL))
Daniel Veillard02141ea2001-04-30 11:46:40 +00003687 xmlFreePropList(cur->properties);
Daniel Veillard7db37732001-07-12 01:20:08 +00003688 if ((cur->type != XML_ELEMENT_NODE) &&
3689 (cur->type != XML_XINCLUDE_START) &&
3690 (cur->type != XML_XINCLUDE_END) &&
Daniel Veillard8874b942005-08-25 13:19:21 +00003691 (cur->type != XML_ENTITY_REF_NODE) &&
3692 (cur->content != (xmlChar *) &(cur->properties))) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003693 DICT_FREE(cur->content)
Daniel Veillard7db37732001-07-12 01:20:08 +00003694 }
3695 if (((cur->type == XML_ELEMENT_NODE) ||
3696 (cur->type == XML_XINCLUDE_START) ||
3697 (cur->type == XML_XINCLUDE_END)) &&
3698 (cur->nsDef != NULL))
3699 xmlFreeNsList(cur->nsDef);
3700
Daniel Veillard9cc6dc62001-06-11 08:09:20 +00003701 /*
3702 * When a node is a text node or a comment, it uses a global static
3703 * variable for the name of the node.
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003704 * Otherwise the node name might come from the document's
Xin Li28c53d32017-03-07 00:33:02 +00003705 * dictionary
Daniel Veillard9cc6dc62001-06-11 08:09:20 +00003706 */
Daniel Veillard02141ea2001-04-30 11:46:40 +00003707 if ((cur->name != NULL) &&
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003708 (cur->type != XML_TEXT_NODE) &&
3709 (cur->type != XML_COMMENT_NODE))
3710 DICT_FREE(cur->name)
Daniel Veillard02141ea2001-04-30 11:46:40 +00003711 xmlFree(cur);
3712 }
Owen Taylor3473f882001-02-23 17:55:21 +00003713 cur = next;
3714 }
3715}
3716
3717/**
3718 * xmlFreeNode:
3719 * @cur: the node
3720 *
3721 * Free a node, this is a recursive behaviour, all the children are freed too.
3722 * This doesn't unlink the child from the list, use xmlUnlinkNode() first.
3723 */
3724void
3725xmlFreeNode(xmlNodePtr cur) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003726 xmlDictPtr dict = NULL;
3727
3728 if (cur == NULL) return;
Daniel Veillard5335dc52003-01-01 20:59:38 +00003729
Daniel Veillard02141ea2001-04-30 11:46:40 +00003730 /* use xmlFreeDtd for DTD nodes */
Daniel Veillarde6a55192002-01-14 17:11:53 +00003731 if (cur->type == XML_DTD_NODE) {
3732 xmlFreeDtd((xmlDtdPtr) cur);
Owen Taylor3473f882001-02-23 17:55:21 +00003733 return;
Daniel Veillarde6a55192002-01-14 17:11:53 +00003734 }
3735 if (cur->type == XML_NAMESPACE_DECL) {
3736 xmlFreeNs((xmlNsPtr) cur);
3737 return;
3738 }
Daniel Veillarda70d62f2002-11-07 14:18:03 +00003739 if (cur->type == XML_ATTRIBUTE_NODE) {
3740 xmlFreeProp((xmlAttrPtr) cur);
3741 return;
3742 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00003743
Daniel Veillarda880b122003-04-21 21:36:41 +00003744 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00003745 xmlDeregisterNodeDefaultValue(cur);
3746
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003747 if (cur->doc != NULL) dict = cur->doc->dict;
3748
Daniel Veillard2cba4152008-08-27 11:45:41 +00003749 if (cur->type == XML_ENTITY_DECL) {
3750 xmlEntityPtr ent = (xmlEntityPtr) cur;
3751 DICT_FREE(ent->SystemID);
3752 DICT_FREE(ent->ExternalID);
3753 }
Owen Taylor3473f882001-02-23 17:55:21 +00003754 if ((cur->children != NULL) &&
3755 (cur->type != XML_ENTITY_REF_NODE))
3756 xmlFreeNodeList(cur->children);
Daniel Veillard01c13b52002-12-10 15:19:08 +00003757 if (((cur->type == XML_ELEMENT_NODE) ||
3758 (cur->type == XML_XINCLUDE_START) ||
3759 (cur->type == XML_XINCLUDE_END)) &&
3760 (cur->properties != NULL))
Daniel Veillard02141ea2001-04-30 11:46:40 +00003761 xmlFreePropList(cur->properties);
Daniel Veillard7db37732001-07-12 01:20:08 +00003762 if ((cur->type != XML_ELEMENT_NODE) &&
3763 (cur->content != NULL) &&
3764 (cur->type != XML_ENTITY_REF_NODE) &&
3765 (cur->type != XML_XINCLUDE_END) &&
Daniel Veillard8874b942005-08-25 13:19:21 +00003766 (cur->type != XML_XINCLUDE_START) &&
3767 (cur->content != (xmlChar *) &(cur->properties))) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003768 DICT_FREE(cur->content)
Daniel Veillard7db37732001-07-12 01:20:08 +00003769 }
3770
Daniel Veillardacd370f2001-06-09 17:17:51 +00003771 /*
3772 * When a node is a text node or a comment, it uses a global static
3773 * variable for the name of the node.
Xin Li28c53d32017-03-07 00:33:02 +00003774 * Otherwise the node name might come from the document's dictionary
Daniel Veillardacd370f2001-06-09 17:17:51 +00003775 */
Owen Taylor3473f882001-02-23 17:55:21 +00003776 if ((cur->name != NULL) &&
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003777 (cur->type != XML_TEXT_NODE) &&
3778 (cur->type != XML_COMMENT_NODE))
3779 DICT_FREE(cur->name)
Daniel Veillardacd370f2001-06-09 17:17:51 +00003780
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003781 if (((cur->type == XML_ELEMENT_NODE) ||
3782 (cur->type == XML_XINCLUDE_START) ||
3783 (cur->type == XML_XINCLUDE_END)) &&
3784 (cur->nsDef != NULL))
3785 xmlFreeNsList(cur->nsDef);
Owen Taylor3473f882001-02-23 17:55:21 +00003786 xmlFree(cur);
3787}
3788
3789/**
3790 * xmlUnlinkNode:
3791 * @cur: the node
3792 *
3793 * Unlink a node from it's current context, the node is not freed
Daniel Veillard39d027c2012-05-11 12:38:23 +08003794 * If one need to free the node, use xmlFreeNode() routine after the
3795 * unlink to discard it.
Daniel Veillard6ca24a32012-08-08 15:31:55 +08003796 * Note that namespace nodes can't be unlinked as they do not have
3797 * pointer to their parent.
Owen Taylor3473f882001-02-23 17:55:21 +00003798 */
3799void
3800xmlUnlinkNode(xmlNodePtr cur) {
3801 if (cur == NULL) {
3802#ifdef DEBUG_TREE
3803 xmlGenericError(xmlGenericErrorContext,
3804 "xmlUnlinkNode : node == NULL\n");
3805#endif
3806 return;
3807 }
Daniel Veillard6ca24a32012-08-08 15:31:55 +08003808 if (cur->type == XML_NAMESPACE_DECL)
3809 return;
Daniel Veillard29e43992001-12-13 22:21:58 +00003810 if (cur->type == XML_DTD_NODE) {
3811 xmlDocPtr doc;
3812 doc = cur->doc;
Daniel Veillarda067e652003-05-01 08:03:46 +00003813 if (doc != NULL) {
3814 if (doc->intSubset == (xmlDtdPtr) cur)
3815 doc->intSubset = NULL;
3816 if (doc->extSubset == (xmlDtdPtr) cur)
3817 doc->extSubset = NULL;
3818 }
Daniel Veillard29e43992001-12-13 22:21:58 +00003819 }
Daniel Veillard2cba4152008-08-27 11:45:41 +00003820 if (cur->type == XML_ENTITY_DECL) {
3821 xmlDocPtr doc;
3822 doc = cur->doc;
3823 if (doc != NULL) {
3824 if (doc->intSubset != NULL) {
3825 if (xmlHashLookup(doc->intSubset->entities, cur->name) == cur)
3826 xmlHashRemoveEntry(doc->intSubset->entities, cur->name,
3827 NULL);
3828 if (xmlHashLookup(doc->intSubset->pentities, cur->name) == cur)
3829 xmlHashRemoveEntry(doc->intSubset->pentities, cur->name,
3830 NULL);
3831 }
3832 if (doc->extSubset != NULL) {
3833 if (xmlHashLookup(doc->extSubset->entities, cur->name) == cur)
3834 xmlHashRemoveEntry(doc->extSubset->entities, cur->name,
3835 NULL);
3836 if (xmlHashLookup(doc->extSubset->pentities, cur->name) == cur)
3837 xmlHashRemoveEntry(doc->extSubset->pentities, cur->name,
3838 NULL);
3839 }
3840 }
3841 }
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003842 if (cur->parent != NULL) {
3843 xmlNodePtr parent;
3844 parent = cur->parent;
3845 if (cur->type == XML_ATTRIBUTE_NODE) {
3846 if (parent->properties == (xmlAttrPtr) cur)
3847 parent->properties = ((xmlAttrPtr) cur)->next;
3848 } else {
3849 if (parent->children == cur)
3850 parent->children = cur->next;
3851 if (parent->last == cur)
3852 parent->last = cur->prev;
3853 }
3854 cur->parent = NULL;
3855 }
Owen Taylor3473f882001-02-23 17:55:21 +00003856 if (cur->next != NULL)
3857 cur->next->prev = cur->prev;
3858 if (cur->prev != NULL)
3859 cur->prev->next = cur->next;
3860 cur->next = cur->prev = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00003861}
3862
Daniel Veillard2156d432004-03-04 15:59:36 +00003863#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00003864/**
3865 * xmlReplaceNode:
3866 * @old: the old node
3867 * @cur: the node
3868 *
William M. Brackd7cf7f82003-11-14 07:13:16 +00003869 * Unlink the old node from its current context, prune the new one
Daniel Veillardd1640922001-12-17 15:30:10 +00003870 * at the same place. If @cur was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00003871 * first unlinked from its existing context.
3872 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003873 * Returns the @old node
Owen Taylor3473f882001-02-23 17:55:21 +00003874 */
3875xmlNodePtr
3876xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) {
Daniel Veillardce244ad2004-11-05 10:03:46 +00003877 if (old == cur) return(NULL);
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003878 if ((old == NULL) || (old->type == XML_NAMESPACE_DECL) ||
3879 (old->parent == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003880#ifdef DEBUG_TREE
3881 xmlGenericError(xmlGenericErrorContext,
Daniel Veillarda03e3652004-11-02 18:45:30 +00003882 "xmlReplaceNode : old == NULL or without parent\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003883#endif
3884 return(NULL);
3885 }
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003886 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003887 xmlUnlinkNode(old);
3888 return(old);
3889 }
3890 if (cur == old) {
3891 return(old);
3892 }
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003893 if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) {
3894#ifdef DEBUG_TREE
3895 xmlGenericError(xmlGenericErrorContext,
3896 "xmlReplaceNode : Trying to replace attribute node with other node type\n");
3897#endif
3898 return(old);
3899 }
3900 if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) {
3901#ifdef DEBUG_TREE
3902 xmlGenericError(xmlGenericErrorContext,
3903 "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n");
3904#endif
3905 return(old);
3906 }
Owen Taylor3473f882001-02-23 17:55:21 +00003907 xmlUnlinkNode(cur);
Daniel Veillard64d7d122005-05-11 18:03:42 +00003908 xmlSetTreeDoc(cur, old->doc);
Owen Taylor3473f882001-02-23 17:55:21 +00003909 cur->parent = old->parent;
3910 cur->next = old->next;
3911 if (cur->next != NULL)
3912 cur->next->prev = cur;
3913 cur->prev = old->prev;
3914 if (cur->prev != NULL)
3915 cur->prev->next = cur;
3916 if (cur->parent != NULL) {
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003917 if (cur->type == XML_ATTRIBUTE_NODE) {
3918 if (cur->parent->properties == (xmlAttrPtr)old)
3919 cur->parent->properties = ((xmlAttrPtr) cur);
3920 } else {
3921 if (cur->parent->children == old)
3922 cur->parent->children = cur;
3923 if (cur->parent->last == old)
3924 cur->parent->last = cur;
3925 }
Owen Taylor3473f882001-02-23 17:55:21 +00003926 }
3927 old->next = old->prev = NULL;
3928 old->parent = NULL;
3929 return(old);
3930}
Daniel Veillard652327a2003-09-29 18:02:38 +00003931#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00003932
3933/************************************************************************
3934 * *
3935 * Copy operations *
3936 * *
3937 ************************************************************************/
Daniel Veillardaa6de472008-08-25 14:53:31 +00003938
Owen Taylor3473f882001-02-23 17:55:21 +00003939/**
3940 * xmlCopyNamespace:
3941 * @cur: the namespace
3942 *
3943 * Do a copy of the namespace.
3944 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003945 * Returns: a new #xmlNsPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003946 */
3947xmlNsPtr
3948xmlCopyNamespace(xmlNsPtr cur) {
3949 xmlNsPtr ret;
3950
3951 if (cur == NULL) return(NULL);
3952 switch (cur->type) {
3953 case XML_LOCAL_NAMESPACE:
3954 ret = xmlNewNs(NULL, cur->href, cur->prefix);
3955 break;
3956 default:
3957#ifdef DEBUG_TREE
3958 xmlGenericError(xmlGenericErrorContext,
3959 "xmlCopyNamespace: invalid type %d\n", cur->type);
3960#endif
3961 return(NULL);
3962 }
3963 return(ret);
3964}
3965
3966/**
3967 * xmlCopyNamespaceList:
3968 * @cur: the first namespace
3969 *
3970 * Do a copy of an namespace list.
3971 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003972 * Returns: a new #xmlNsPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003973 */
3974xmlNsPtr
3975xmlCopyNamespaceList(xmlNsPtr cur) {
3976 xmlNsPtr ret = NULL;
3977 xmlNsPtr p = NULL,q;
3978
3979 while (cur != NULL) {
3980 q = xmlCopyNamespace(cur);
3981 if (p == NULL) {
3982 ret = p = q;
3983 } else {
3984 p->next = q;
3985 p = q;
3986 }
3987 cur = cur->next;
3988 }
3989 return(ret);
3990}
3991
3992static xmlNodePtr
3993xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent);
Rob Richards19dc9612005-10-28 16:15:16 +00003994
3995static xmlAttrPtr
3996xmlCopyPropInternal(xmlDocPtr doc, xmlNodePtr target, xmlAttrPtr cur) {
Owen Taylor3473f882001-02-23 17:55:21 +00003997 xmlAttrPtr ret;
3998
3999 if (cur == NULL) return(NULL);
Daniel Veillard3e62adb2012-08-09 14:24:02 +08004000 if ((target != NULL) && (target->type != XML_ELEMENT_NODE))
4001 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004002 if (target != NULL)
4003 ret = xmlNewDocProp(target->doc, cur->name, NULL);
Rob Richards19dc9612005-10-28 16:15:16 +00004004 else if (doc != NULL)
4005 ret = xmlNewDocProp(doc, cur->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004006 else if (cur->parent != NULL)
4007 ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL);
4008 else if (cur->children != NULL)
4009 ret = xmlNewDocProp(cur->children->doc, cur->name, NULL);
4010 else
4011 ret = xmlNewDocProp(NULL, cur->name, NULL);
4012 if (ret == NULL) return(NULL);
4013 ret->parent = target;
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00004014
Owen Taylor3473f882001-02-23 17:55:21 +00004015 if ((cur->ns != NULL) && (target != NULL)) {
Daniel Veillard8107a222002-01-13 14:10:10 +00004016 xmlNsPtr ns;
Daniel Veillard652327a2003-09-29 18:02:38 +00004017
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00004018 ns = xmlSearchNs(target->doc, target, cur->ns->prefix);
4019 if (ns == NULL) {
4020 /*
4021 * Humm, we are copying an element whose namespace is defined
4022 * out of the new tree scope. Search it in the original tree
4023 * and add it at the top of the new tree
4024 */
4025 ns = xmlSearchNs(cur->doc, cur->parent, cur->ns->prefix);
4026 if (ns != NULL) {
4027 xmlNodePtr root = target;
4028 xmlNodePtr pred = NULL;
4029
4030 while (root->parent != NULL) {
4031 pred = root;
4032 root = root->parent;
4033 }
4034 if (root == (xmlNodePtr) target->doc) {
4035 /* correct possibly cycling above the document elt */
4036 root = pred;
4037 }
4038 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
4039 }
4040 } else {
4041 /*
4042 * we have to find something appropriate here since
4043 * we cant be sure, that the namespce we found is identified
4044 * by the prefix
4045 */
Daniel Veillard044fc6b2002-03-04 17:09:44 +00004046 if (xmlStrEqual(ns->href, cur->ns->href)) {
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00004047 /* this is the nice case */
4048 ret->ns = ns;
4049 } else {
4050 /*
4051 * we are in trouble: we need a new reconcilied namespace.
4052 * This is expensive
4053 */
4054 ret->ns = xmlNewReconciliedNs(target->doc, target, cur->ns);
4055 }
4056 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00004057
Owen Taylor3473f882001-02-23 17:55:21 +00004058 } else
4059 ret->ns = NULL;
4060
4061 if (cur->children != NULL) {
4062 xmlNodePtr tmp;
4063
4064 ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret);
4065 ret->last = NULL;
4066 tmp = ret->children;
4067 while (tmp != NULL) {
4068 /* tmp->parent = (xmlNodePtr)ret; */
4069 if (tmp->next == NULL)
4070 ret->last = tmp;
4071 tmp = tmp->next;
4072 }
4073 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00004074 /*
4075 * Try to handle IDs
4076 */
Daniel Veillarda3db2e32002-03-08 15:46:57 +00004077 if ((target!= NULL) && (cur!= NULL) &&
4078 (target->doc != NULL) && (cur->doc != NULL) &&
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00004079 (cur->doc->ids != NULL) && (cur->parent != NULL)) {
4080 if (xmlIsID(cur->doc, cur->parent, cur)) {
4081 xmlChar *id;
4082
4083 id = xmlNodeListGetString(cur->doc, cur->children, 1);
4084 if (id != NULL) {
4085 xmlAddID(NULL, target->doc, id, ret);
4086 xmlFree(id);
4087 }
4088 }
4089 }
Owen Taylor3473f882001-02-23 17:55:21 +00004090 return(ret);
4091}
4092
4093/**
Rob Richards19dc9612005-10-28 16:15:16 +00004094 * xmlCopyProp:
4095 * @target: the element where the attribute will be grafted
4096 * @cur: the attribute
4097 *
4098 * Do a copy of the attribute.
4099 *
4100 * Returns: a new #xmlAttrPtr, or NULL in case of error.
4101 */
4102xmlAttrPtr
4103xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) {
4104 return xmlCopyPropInternal(NULL, target, cur);
4105}
4106
4107/**
Owen Taylor3473f882001-02-23 17:55:21 +00004108 * xmlCopyPropList:
4109 * @target: the element where the attributes will be grafted
4110 * @cur: the first attribute
4111 *
4112 * Do a copy of an attribute list.
4113 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004114 * Returns: a new #xmlAttrPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004115 */
4116xmlAttrPtr
4117xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) {
4118 xmlAttrPtr ret = NULL;
4119 xmlAttrPtr p = NULL,q;
4120
Daniel Veillard3e62adb2012-08-09 14:24:02 +08004121 if ((target != NULL) && (target->type != XML_ELEMENT_NODE))
4122 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004123 while (cur != NULL) {
4124 q = xmlCopyProp(target, cur);
William M. Brack13dfa872004-09-18 04:52:08 +00004125 if (q == NULL)
4126 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004127 if (p == NULL) {
4128 ret = p = q;
4129 } else {
4130 p->next = q;
4131 q->prev = p;
4132 p = q;
4133 }
4134 cur = cur->next;
4135 }
4136 return(ret);
4137}
4138
4139/*
Daniel Veillardd1640922001-12-17 15:30:10 +00004140 * NOTE about the CopyNode operations !
Owen Taylor3473f882001-02-23 17:55:21 +00004141 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004142 * They are split into external and internal parts for one
Owen Taylor3473f882001-02-23 17:55:21 +00004143 * tricky reason: namespaces. Doing a direct copy of a node
4144 * say RPM:Copyright without changing the namespace pointer to
4145 * something else can produce stale links. One way to do it is
4146 * to keep a reference counter but this doesn't work as soon
4147 * as one move the element or the subtree out of the scope of
4148 * the existing namespace. The actual solution seems to add
4149 * a copy of the namespace at the top of the copied tree if
4150 * not available in the subtree.
4151 * Hence two functions, the public front-end call the inner ones
William M. Brack57e9e912004-03-09 16:19:02 +00004152 * The argument "recursive" normally indicates a recursive copy
4153 * of the node with values 0 (no) and 1 (yes). For XInclude,
4154 * however, we allow a value of 2 to indicate copy properties and
4155 * namespace info, but don't recurse on children.
Owen Taylor3473f882001-02-23 17:55:21 +00004156 */
4157
4158static xmlNodePtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +08004159xmlStaticCopyNode(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent,
William M. Brack57e9e912004-03-09 16:19:02 +00004160 int extended) {
Owen Taylor3473f882001-02-23 17:55:21 +00004161 xmlNodePtr ret;
4162
4163 if (node == NULL) return(NULL);
Daniel Veillard39196eb2001-06-19 18:09:42 +00004164 switch (node->type) {
4165 case XML_TEXT_NODE:
4166 case XML_CDATA_SECTION_NODE:
4167 case XML_ELEMENT_NODE:
Daniel Veillardec6725e2002-09-05 11:12:45 +00004168 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00004169 case XML_ENTITY_REF_NODE:
4170 case XML_ENTITY_NODE:
4171 case XML_PI_NODE:
4172 case XML_COMMENT_NODE:
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00004173 case XML_XINCLUDE_START:
4174 case XML_XINCLUDE_END:
4175 break;
4176 case XML_ATTRIBUTE_NODE:
Rob Richards19dc9612005-10-28 16:15:16 +00004177 return((xmlNodePtr) xmlCopyPropInternal(doc, parent, (xmlAttrPtr) node));
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00004178 case XML_NAMESPACE_DECL:
4179 return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node));
Daniel Veillardaa6de472008-08-25 14:53:31 +00004180
Daniel Veillard39196eb2001-06-19 18:09:42 +00004181 case XML_DOCUMENT_NODE:
4182 case XML_HTML_DOCUMENT_NODE:
4183#ifdef LIBXML_DOCB_ENABLED
4184 case XML_DOCB_DOCUMENT_NODE:
4185#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00004186#ifdef LIBXML_TREE_ENABLED
William M. Brack57e9e912004-03-09 16:19:02 +00004187 return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, extended));
Daniel Veillard652327a2003-09-29 18:02:38 +00004188#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard39196eb2001-06-19 18:09:42 +00004189 case XML_DOCUMENT_TYPE_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00004190 case XML_NOTATION_NODE:
4191 case XML_DTD_NODE:
4192 case XML_ELEMENT_DECL:
4193 case XML_ATTRIBUTE_DECL:
4194 case XML_ENTITY_DECL:
4195 return(NULL);
4196 }
Daniel Veillardb33c2012001-04-25 12:59:04 +00004197
Owen Taylor3473f882001-02-23 17:55:21 +00004198 /*
4199 * Allocate a new node and fill the fields.
4200 */
4201 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
4202 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004203 xmlTreeErrMemory("copying node");
Owen Taylor3473f882001-02-23 17:55:21 +00004204 return(NULL);
4205 }
4206 memset(ret, 0, sizeof(xmlNode));
4207 ret->type = node->type;
4208
4209 ret->doc = doc;
Daniel Veillardaa6de472008-08-25 14:53:31 +00004210 ret->parent = parent;
Owen Taylor3473f882001-02-23 17:55:21 +00004211 if (node->name == xmlStringText)
4212 ret->name = xmlStringText;
4213 else if (node->name == xmlStringTextNoenc)
4214 ret->name = xmlStringTextNoenc;
4215 else if (node->name == xmlStringComment)
4216 ret->name = xmlStringComment;
Daniel Veillard03a53c32004-10-26 16:06:51 +00004217 else if (node->name != NULL) {
4218 if ((doc != NULL) && (doc->dict != NULL))
4219 ret->name = xmlDictLookup(doc->dict, node->name, -1);
4220 else
4221 ret->name = xmlStrdup(node->name);
4222 }
Daniel Veillard7db37732001-07-12 01:20:08 +00004223 if ((node->type != XML_ELEMENT_NODE) &&
4224 (node->content != NULL) &&
4225 (node->type != XML_ENTITY_REF_NODE) &&
4226 (node->type != XML_XINCLUDE_END) &&
4227 (node->type != XML_XINCLUDE_START)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004228 ret->content = xmlStrdup(node->content);
Daniel Veillard8107a222002-01-13 14:10:10 +00004229 }else{
4230 if (node->type == XML_ELEMENT_NODE)
Daniel Veillard3e35f8e2003-10-21 00:05:38 +00004231 ret->line = node->line;
Owen Taylor3473f882001-02-23 17:55:21 +00004232 }
Daniel Veillardacb2bda2002-01-13 16:15:43 +00004233 if (parent != NULL) {
4234 xmlNodePtr tmp;
4235
Daniel Veillard8a1b1852003-01-05 22:37:17 +00004236 /*
4237 * this is a tricky part for the node register thing:
4238 * in case ret does get coalesced in xmlAddChild
4239 * the deregister-node callback is called; so we register ret now already
4240 */
Daniel Veillarda880b122003-04-21 21:36:41 +00004241 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00004242 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
4243
Daniel Veillardacb2bda2002-01-13 16:15:43 +00004244 tmp = xmlAddChild(parent, ret);
4245 /* node could have coalesced */
4246 if (tmp != ret)
4247 return(tmp);
4248 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00004249
William M. Brack57e9e912004-03-09 16:19:02 +00004250 if (!extended)
Daniel Veillard8a1b1852003-01-05 22:37:17 +00004251 goto out;
Petr Pajas2afca4a2009-07-30 17:47:32 +02004252 if (((node->type == XML_ELEMENT_NODE) ||
4253 (node->type == XML_XINCLUDE_START)) && (node->nsDef != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00004254 ret->nsDef = xmlCopyNamespaceList(node->nsDef);
4255
4256 if (node->ns != NULL) {
4257 xmlNsPtr ns;
4258
4259 ns = xmlSearchNs(doc, ret, node->ns->prefix);
4260 if (ns == NULL) {
4261 /*
4262 * Humm, we are copying an element whose namespace is defined
4263 * out of the new tree scope. Search it in the original tree
4264 * and add it at the top of the new tree
4265 */
4266 ns = xmlSearchNs(node->doc, node, node->ns->prefix);
4267 if (ns != NULL) {
4268 xmlNodePtr root = ret;
4269
4270 while (root->parent != NULL) root = root->parent;
Daniel Veillarde82a9922001-04-22 12:12:58 +00004271 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
Rob Richardsddb01cb2010-01-29 13:32:12 -05004272 } else {
4273 ret->ns = xmlNewReconciliedNs(doc, ret, node->ns);
Owen Taylor3473f882001-02-23 17:55:21 +00004274 }
4275 } else {
4276 /*
4277 * reference the existing namespace definition in our own tree.
4278 */
4279 ret->ns = ns;
4280 }
4281 }
Petr Pajas2afca4a2009-07-30 17:47:32 +02004282 if (((node->type == XML_ELEMENT_NODE) ||
4283 (node->type == XML_XINCLUDE_START)) && (node->properties != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00004284 ret->properties = xmlCopyPropList(ret, node->properties);
Daniel Veillardb33c2012001-04-25 12:59:04 +00004285 if (node->type == XML_ENTITY_REF_NODE) {
4286 if ((doc == NULL) || (node->doc != doc)) {
4287 /*
4288 * The copied node will go into a separate document, so
Daniel Veillardd1640922001-12-17 15:30:10 +00004289 * to avoid dangling references to the ENTITY_DECL node
Daniel Veillardb33c2012001-04-25 12:59:04 +00004290 * we cannot keep the reference. Try to find it in the
4291 * target document.
4292 */
4293 ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name);
4294 } else {
4295 ret->children = node->children;
4296 }
Daniel Veillard0ec98632001-11-14 15:04:32 +00004297 ret->last = ret->children;
William M. Brack57e9e912004-03-09 16:19:02 +00004298 } else if ((node->children != NULL) && (extended != 2)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004299 ret->children = xmlStaticCopyNodeList(node->children, doc, ret);
Daniel Veillard0ec98632001-11-14 15:04:32 +00004300 UPDATE_LAST_CHILD_AND_PARENT(ret)
4301 }
Daniel Veillard8a1b1852003-01-05 22:37:17 +00004302
4303out:
4304 /* if parent != NULL we already registered the node above */
Daniel Veillardac996a12004-07-30 12:02:58 +00004305 if ((parent == NULL) &&
4306 ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00004307 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
Owen Taylor3473f882001-02-23 17:55:21 +00004308 return(ret);
4309}
4310
4311static xmlNodePtr
4312xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) {
4313 xmlNodePtr ret = NULL;
4314 xmlNodePtr p = NULL,q;
4315
4316 while (node != NULL) {
Daniel Veillard652327a2003-09-29 18:02:38 +00004317#ifdef LIBXML_TREE_ENABLED
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00004318 if (node->type == XML_DTD_NODE ) {
Daniel Veillard4497e692001-06-09 14:19:02 +00004319 if (doc == NULL) {
4320 node = node->next;
4321 continue;
4322 }
Daniel Veillardb33c2012001-04-25 12:59:04 +00004323 if (doc->intSubset == NULL) {
4324 q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node );
Gaurav98a4e712013-11-29 23:28:21 +08004325 if (q == NULL) return(NULL);
Daniel Veillardb33c2012001-04-25 12:59:04 +00004326 q->doc = doc;
4327 q->parent = parent;
4328 doc->intSubset = (xmlDtdPtr) q;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004329 xmlAddChild(parent, q);
Daniel Veillardb33c2012001-04-25 12:59:04 +00004330 } else {
4331 q = (xmlNodePtr) doc->intSubset;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004332 xmlAddChild(parent, q);
Daniel Veillardb33c2012001-04-25 12:59:04 +00004333 }
4334 } else
Daniel Veillard652327a2003-09-29 18:02:38 +00004335#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardb33c2012001-04-25 12:59:04 +00004336 q = xmlStaticCopyNode(node, doc, parent, 1);
Gaurav98a4e712013-11-29 23:28:21 +08004337 if (q == NULL) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004338 if (ret == NULL) {
4339 q->prev = NULL;
4340 ret = p = q;
Daniel Veillardacb2bda2002-01-13 16:15:43 +00004341 } else if (p != q) {
4342 /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */
Owen Taylor3473f882001-02-23 17:55:21 +00004343 p->next = q;
4344 q->prev = p;
4345 p = q;
4346 }
4347 node = node->next;
4348 }
4349 return(ret);
4350}
4351
4352/**
4353 * xmlCopyNode:
4354 * @node: the node
William M. Brack57e9e912004-03-09 16:19:02 +00004355 * @extended: if 1 do a recursive copy (properties, namespaces and children
4356 * when applicable)
4357 * if 2 copy properties and namespaces (when applicable)
Owen Taylor3473f882001-02-23 17:55:21 +00004358 *
4359 * Do a copy of the node.
4360 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004361 * Returns: a new #xmlNodePtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004362 */
4363xmlNodePtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +08004364xmlCopyNode(xmlNodePtr node, int extended) {
Owen Taylor3473f882001-02-23 17:55:21 +00004365 xmlNodePtr ret;
4366
William M. Brack57e9e912004-03-09 16:19:02 +00004367 ret = xmlStaticCopyNode(node, NULL, NULL, extended);
Owen Taylor3473f882001-02-23 17:55:21 +00004368 return(ret);
4369}
4370
4371/**
Daniel Veillard82daa812001-04-12 08:55:36 +00004372 * xmlDocCopyNode:
4373 * @node: the node
Daniel Veillardd1640922001-12-17 15:30:10 +00004374 * @doc: the document
William M. Brack57e9e912004-03-09 16:19:02 +00004375 * @extended: if 1 do a recursive copy (properties, namespaces and children
4376 * when applicable)
4377 * if 2 copy properties and namespaces (when applicable)
Daniel Veillard82daa812001-04-12 08:55:36 +00004378 *
4379 * Do a copy of the node to a given document.
4380 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004381 * Returns: a new #xmlNodePtr, or NULL in case of error.
Daniel Veillard82daa812001-04-12 08:55:36 +00004382 */
4383xmlNodePtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +08004384xmlDocCopyNode(xmlNodePtr node, xmlDocPtr doc, int extended) {
Daniel Veillard82daa812001-04-12 08:55:36 +00004385 xmlNodePtr ret;
4386
William M. Brack57e9e912004-03-09 16:19:02 +00004387 ret = xmlStaticCopyNode(node, doc, NULL, extended);
Daniel Veillard82daa812001-04-12 08:55:36 +00004388 return(ret);
4389}
4390
4391/**
Daniel Veillard03a53c32004-10-26 16:06:51 +00004392 * xmlDocCopyNodeList:
4393 * @doc: the target document
4394 * @node: the first node in the list.
4395 *
4396 * Do a recursive copy of the node list.
4397 *
4398 * Returns: a new #xmlNodePtr, or NULL in case of error.
4399 */
Kurt Roeckx95ebe532014-10-13 16:06:21 +08004400xmlNodePtr xmlDocCopyNodeList(xmlDocPtr doc, xmlNodePtr node) {
Daniel Veillard03a53c32004-10-26 16:06:51 +00004401 xmlNodePtr ret = xmlStaticCopyNodeList(node, doc, NULL);
4402 return(ret);
4403}
4404
4405/**
Owen Taylor3473f882001-02-23 17:55:21 +00004406 * xmlCopyNodeList:
4407 * @node: the first node in the list.
4408 *
4409 * Do a recursive copy of the node list.
Daniel Veillard03a53c32004-10-26 16:06:51 +00004410 * Use xmlDocCopyNodeList() if possible to ensure string interning.
Owen Taylor3473f882001-02-23 17:55:21 +00004411 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004412 * Returns: a new #xmlNodePtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004413 */
Kurt Roeckx95ebe532014-10-13 16:06:21 +08004414xmlNodePtr xmlCopyNodeList(xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00004415 xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL);
4416 return(ret);
4417}
4418
Daniel Veillard2156d432004-03-04 15:59:36 +00004419#if defined(LIBXML_TREE_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004420/**
Owen Taylor3473f882001-02-23 17:55:21 +00004421 * xmlCopyDtd:
4422 * @dtd: the dtd
4423 *
4424 * Do a copy of the dtd.
4425 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004426 * Returns: a new #xmlDtdPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004427 */
4428xmlDtdPtr
4429xmlCopyDtd(xmlDtdPtr dtd) {
4430 xmlDtdPtr ret;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004431 xmlNodePtr cur, p = NULL, q;
Owen Taylor3473f882001-02-23 17:55:21 +00004432
4433 if (dtd == NULL) return(NULL);
4434 ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID);
4435 if (ret == NULL) return(NULL);
4436 if (dtd->entities != NULL)
4437 ret->entities = (void *) xmlCopyEntitiesTable(
4438 (xmlEntitiesTablePtr) dtd->entities);
4439 if (dtd->notations != NULL)
4440 ret->notations = (void *) xmlCopyNotationTable(
4441 (xmlNotationTablePtr) dtd->notations);
4442 if (dtd->elements != NULL)
4443 ret->elements = (void *) xmlCopyElementTable(
4444 (xmlElementTablePtr) dtd->elements);
4445 if (dtd->attributes != NULL)
4446 ret->attributes = (void *) xmlCopyAttributeTable(
4447 (xmlAttributeTablePtr) dtd->attributes);
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004448 if (dtd->pentities != NULL)
4449 ret->pentities = (void *) xmlCopyEntitiesTable(
4450 (xmlEntitiesTablePtr) dtd->pentities);
Daniel Veillardaa6de472008-08-25 14:53:31 +00004451
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004452 cur = dtd->children;
4453 while (cur != NULL) {
4454 q = NULL;
4455
4456 if (cur->type == XML_ENTITY_DECL) {
4457 xmlEntityPtr tmp = (xmlEntityPtr) cur;
4458 switch (tmp->etype) {
4459 case XML_INTERNAL_GENERAL_ENTITY:
4460 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
4461 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
4462 q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name);
4463 break;
4464 case XML_INTERNAL_PARAMETER_ENTITY:
4465 case XML_EXTERNAL_PARAMETER_ENTITY:
Daniel Veillardaa6de472008-08-25 14:53:31 +00004466 q = (xmlNodePtr)
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004467 xmlGetParameterEntityFromDtd(ret, tmp->name);
4468 break;
4469 case XML_INTERNAL_PREDEFINED_ENTITY:
4470 break;
4471 }
4472 } else if (cur->type == XML_ELEMENT_DECL) {
4473 xmlElementPtr tmp = (xmlElementPtr) cur;
4474 q = (xmlNodePtr)
4475 xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix);
4476 } else if (cur->type == XML_ATTRIBUTE_DECL) {
4477 xmlAttributePtr tmp = (xmlAttributePtr) cur;
Daniel Veillardaa6de472008-08-25 14:53:31 +00004478 q = (xmlNodePtr)
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004479 xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix);
4480 } else if (cur->type == XML_COMMENT_NODE) {
4481 q = xmlCopyNode(cur, 0);
4482 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00004483
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004484 if (q == NULL) {
4485 cur = cur->next;
4486 continue;
4487 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00004488
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004489 if (p == NULL)
4490 ret->children = q;
4491 else
Daniel Veillardaa6de472008-08-25 14:53:31 +00004492 p->next = q;
4493
4494 q->prev = p;
4495 q->parent = (xmlNodePtr) ret;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004496 q->next = NULL;
4497 ret->last = q;
Daniel Veillardaa6de472008-08-25 14:53:31 +00004498 p = q;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004499 cur = cur->next;
4500 }
4501
Owen Taylor3473f882001-02-23 17:55:21 +00004502 return(ret);
4503}
Daniel Veillard2156d432004-03-04 15:59:36 +00004504#endif
Owen Taylor3473f882001-02-23 17:55:21 +00004505
Daniel Veillard2156d432004-03-04 15:59:36 +00004506#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004507/**
4508 * xmlCopyDoc:
4509 * @doc: the document
William M. Brack57e9e912004-03-09 16:19:02 +00004510 * @recursive: if not zero do a recursive copy.
Owen Taylor3473f882001-02-23 17:55:21 +00004511 *
4512 * Do a copy of the document info. If recursive, the content tree will
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004513 * be copied too as well as DTD, namespaces and entities.
Owen Taylor3473f882001-02-23 17:55:21 +00004514 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004515 * Returns: a new #xmlDocPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004516 */
4517xmlDocPtr
4518xmlCopyDoc(xmlDocPtr doc, int recursive) {
4519 xmlDocPtr ret;
4520
4521 if (doc == NULL) return(NULL);
4522 ret = xmlNewDoc(doc->version);
4523 if (ret == NULL) return(NULL);
4524 if (doc->name != NULL)
4525 ret->name = xmlMemStrdup(doc->name);
4526 if (doc->encoding != NULL)
4527 ret->encoding = xmlStrdup(doc->encoding);
Daniel Veillardf59507d2005-01-27 17:26:49 +00004528 if (doc->URL != NULL)
4529 ret->URL = xmlStrdup(doc->URL);
Owen Taylor3473f882001-02-23 17:55:21 +00004530 ret->charset = doc->charset;
4531 ret->compression = doc->compression;
4532 ret->standalone = doc->standalone;
4533 if (!recursive) return(ret);
4534
Daniel Veillardb33c2012001-04-25 12:59:04 +00004535 ret->last = NULL;
4536 ret->children = NULL;
Daniel Veillard2156d432004-03-04 15:59:36 +00004537#ifdef LIBXML_TREE_ENABLED
Daniel Veillardb33c2012001-04-25 12:59:04 +00004538 if (doc->intSubset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004539 ret->intSubset = xmlCopyDtd(doc->intSubset);
Daniel Veillard42870f42014-07-26 21:04:54 +08004540 if (ret->intSubset == NULL) {
4541 xmlFreeDoc(ret);
4542 return(NULL);
4543 }
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004544 xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret);
Daniel Veillardb33c2012001-04-25 12:59:04 +00004545 ret->intSubset->parent = ret;
4546 }
Daniel Veillard2156d432004-03-04 15:59:36 +00004547#endif
Owen Taylor3473f882001-02-23 17:55:21 +00004548 if (doc->oldNs != NULL)
4549 ret->oldNs = xmlCopyNamespaceList(doc->oldNs);
4550 if (doc->children != NULL) {
4551 xmlNodePtr tmp;
Daniel Veillardaa6de472008-08-25 14:53:31 +00004552
Daniel Veillardb33c2012001-04-25 12:59:04 +00004553 ret->children = xmlStaticCopyNodeList(doc->children, ret,
4554 (xmlNodePtr)ret);
Owen Taylor3473f882001-02-23 17:55:21 +00004555 ret->last = NULL;
4556 tmp = ret->children;
4557 while (tmp != NULL) {
4558 if (tmp->next == NULL)
4559 ret->last = tmp;
4560 tmp = tmp->next;
4561 }
4562 }
4563 return(ret);
4564}
Daniel Veillard652327a2003-09-29 18:02:38 +00004565#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004566
4567/************************************************************************
4568 * *
4569 * Content access functions *
4570 * *
4571 ************************************************************************/
Daniel Veillardaa6de472008-08-25 14:53:31 +00004572
Owen Taylor3473f882001-02-23 17:55:21 +00004573/**
Daniel Veillard968a03a2012-08-13 12:41:33 +08004574 * xmlGetLineNoInternal:
Daniel Veillard01c13b52002-12-10 15:19:08 +00004575 * @node: valid node
Daniel Veillard968a03a2012-08-13 12:41:33 +08004576 * @depth: used to limit any risk of recursion
Daniel Veillard8faa7832001-11-26 15:58:08 +00004577 *
Daniel Veillard968a03a2012-08-13 12:41:33 +08004578 * Get line number of @node.
4579 * Try to override the limitation of lines being store in 16 bits ints
Daniel Veillard8faa7832001-11-26 15:58:08 +00004580 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004581 * Returns the line number if successful, -1 otherwise
Daniel Veillard8faa7832001-11-26 15:58:08 +00004582 */
Daniel Veillard968a03a2012-08-13 12:41:33 +08004583static long
Kurt Roeckx95ebe532014-10-13 16:06:21 +08004584xmlGetLineNoInternal(const xmlNode *node, int depth)
Daniel Veillard8faa7832001-11-26 15:58:08 +00004585{
4586 long result = -1;
4587
Daniel Veillard968a03a2012-08-13 12:41:33 +08004588 if (depth >= 5)
4589 return(-1);
4590
Daniel Veillard8faa7832001-11-26 15:58:08 +00004591 if (!node)
4592 return result;
Daniel Veillard73da77e2005-08-24 14:05:37 +00004593 if ((node->type == XML_ELEMENT_NODE) ||
4594 (node->type == XML_TEXT_NODE) ||
4595 (node->type == XML_COMMENT_NODE) ||
Daniel Veillard968a03a2012-08-13 12:41:33 +08004596 (node->type == XML_PI_NODE)) {
4597 if (node->line == 65535) {
4598 if ((node->type == XML_TEXT_NODE) && (node->psvi != NULL))
4599 result = (long) node->psvi;
4600 else if ((node->type == XML_ELEMENT_NODE) &&
4601 (node->children != NULL))
4602 result = xmlGetLineNoInternal(node->children, depth + 1);
4603 else if (node->next != NULL)
4604 result = xmlGetLineNoInternal(node->next, depth + 1);
4605 else if (node->prev != NULL)
4606 result = xmlGetLineNoInternal(node->prev, depth + 1);
4607 }
4608 if ((result == -1) || (result == 65535))
4609 result = (long) node->line;
4610 } else if ((node->prev != NULL) &&
Daniel Veillard8faa7832001-11-26 15:58:08 +00004611 ((node->prev->type == XML_ELEMENT_NODE) ||
Daniel Veillard73da77e2005-08-24 14:05:37 +00004612 (node->prev->type == XML_TEXT_NODE) ||
4613 (node->prev->type == XML_COMMENT_NODE) ||
4614 (node->prev->type == XML_PI_NODE)))
Daniel Veillard968a03a2012-08-13 12:41:33 +08004615 result = xmlGetLineNoInternal(node->prev, depth + 1);
Daniel Veillard8faa7832001-11-26 15:58:08 +00004616 else if ((node->parent != NULL) &&
Daniel Veillard73da77e2005-08-24 14:05:37 +00004617 (node->parent->type == XML_ELEMENT_NODE))
Daniel Veillard968a03a2012-08-13 12:41:33 +08004618 result = xmlGetLineNoInternal(node->parent, depth + 1);
Daniel Veillard8faa7832001-11-26 15:58:08 +00004619
4620 return result;
4621}
4622
Daniel Veillard968a03a2012-08-13 12:41:33 +08004623/**
4624 * xmlGetLineNo:
4625 * @node: valid node
4626 *
4627 * Get line number of @node.
4628 * Try to override the limitation of lines being store in 16 bits ints
4629 * if XML_PARSE_BIG_LINES parser option was used
4630 *
4631 * Returns the line number if successful, -1 otherwise
4632 */
4633long
Kurt Roeckx95ebe532014-10-13 16:06:21 +08004634xmlGetLineNo(const xmlNode *node)
Daniel Veillard968a03a2012-08-13 12:41:33 +08004635{
4636 return(xmlGetLineNoInternal(node, 0));
4637}
4638
Daniel Veillard2156d432004-03-04 15:59:36 +00004639#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED)
Daniel Veillard8faa7832001-11-26 15:58:08 +00004640/**
4641 * xmlGetNodePath:
4642 * @node: a node
4643 *
4644 * Build a structure based Path for the given node
4645 *
4646 * Returns the new path or NULL in case of error. The caller must free
4647 * the returned string
4648 */
4649xmlChar *
Kurt Roeckx95ebe532014-10-13 16:06:21 +08004650xmlGetNodePath(const xmlNode *node)
Daniel Veillard8faa7832001-11-26 15:58:08 +00004651{
Kurt Roeckx95ebe532014-10-13 16:06:21 +08004652 const xmlNode *cur, *tmp, *next;
Daniel Veillard8faa7832001-11-26 15:58:08 +00004653 xmlChar *buffer = NULL, *temp;
4654 size_t buf_len;
4655 xmlChar *buf;
Daniel Veillard96c3a3b2002-10-14 15:39:04 +00004656 const char *sep;
Daniel Veillard8faa7832001-11-26 15:58:08 +00004657 const char *name;
4658 char nametemp[100];
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004659 int occur = 0, generic;
Daniel Veillard8faa7832001-11-26 15:58:08 +00004660
Daniel Veillard3e62adb2012-08-09 14:24:02 +08004661 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004662 return (NULL);
4663
4664 buf_len = 500;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004665 buffer = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004666 if (buffer == NULL) {
4667 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004668 return (NULL);
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004669 }
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004670 buf = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
Daniel Veillard8faa7832001-11-26 15:58:08 +00004671 if (buf == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004672 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004673 xmlFree(buffer);
4674 return (NULL);
4675 }
4676
4677 buffer[0] = 0;
4678 cur = node;
4679 do {
4680 name = "";
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004681 sep = "?";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004682 occur = 0;
4683 if ((cur->type == XML_DOCUMENT_NODE) ||
4684 (cur->type == XML_HTML_DOCUMENT_NODE)) {
4685 if (buffer[0] == '/')
4686 break;
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004687 sep = "/";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004688 next = NULL;
4689 } else if (cur->type == XML_ELEMENT_NODE) {
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004690 generic = 0;
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004691 sep = "/";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004692 name = (const char *) cur->name;
4693 if (cur->ns) {
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004694 if (cur->ns->prefix != NULL) {
William M. Brack13dfa872004-09-18 04:52:08 +00004695 snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
Daniel Veillardaa6de472008-08-25 14:53:31 +00004696 (char *)cur->ns->prefix, (char *)cur->name);
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004697 nametemp[sizeof(nametemp) - 1] = 0;
4698 name = nametemp;
4699 } else {
4700 /*
4701 * We cannot express named elements in the default
4702 * namespace, so use "*".
4703 */
4704 generic = 1;
4705 name = "*";
Daniel Veillardaa6de472008-08-25 14:53:31 +00004706 }
Daniel Veillard8faa7832001-11-26 15:58:08 +00004707 }
4708 next = cur->parent;
4709
4710 /*
4711 * Thumbler index computation
Daniel Veillardc00cda82003-04-07 10:22:39 +00004712 * TODO: the ocurence test seems bogus for namespaced names
Daniel Veillard8faa7832001-11-26 15:58:08 +00004713 */
4714 tmp = cur->prev;
4715 while (tmp != NULL) {
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00004716 if ((tmp->type == XML_ELEMENT_NODE) &&
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004717 (generic ||
Kasimier T. Buchcik43ceb1e2006-06-12 11:08:18 +00004718 (xmlStrEqual(cur->name, tmp->name) &&
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004719 ((tmp->ns == cur->ns) ||
4720 ((tmp->ns != NULL) && (cur->ns != NULL) &&
Kasimier T. Buchcik43ceb1e2006-06-12 11:08:18 +00004721 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004722 occur++;
4723 tmp = tmp->prev;
4724 }
4725 if (occur == 0) {
4726 tmp = cur->next;
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004727 while (tmp != NULL && occur == 0) {
4728 if ((tmp->type == XML_ELEMENT_NODE) &&
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004729 (generic ||
Kasimier T. Buchcik43ceb1e2006-06-12 11:08:18 +00004730 (xmlStrEqual(cur->name, tmp->name) &&
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004731 ((tmp->ns == cur->ns) ||
4732 ((tmp->ns != NULL) && (cur->ns != NULL) &&
Kasimier T. Buchcik43ceb1e2006-06-12 11:08:18 +00004733 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004734 occur++;
4735 tmp = tmp->next;
4736 }
4737 if (occur != 0)
4738 occur = 1;
4739 } else
4740 occur++;
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004741 } else if (cur->type == XML_COMMENT_NODE) {
4742 sep = "/";
4743 name = "comment()";
4744 next = cur->parent;
4745
4746 /*
4747 * Thumbler index computation
4748 */
4749 tmp = cur->prev;
4750 while (tmp != NULL) {
4751 if (tmp->type == XML_COMMENT_NODE)
4752 occur++;
4753 tmp = tmp->prev;
4754 }
4755 if (occur == 0) {
4756 tmp = cur->next;
4757 while (tmp != NULL && occur == 0) {
4758 if (tmp->type == XML_COMMENT_NODE)
4759 occur++;
4760 tmp = tmp->next;
4761 }
4762 if (occur != 0)
4763 occur = 1;
4764 } else
4765 occur++;
4766 } else if ((cur->type == XML_TEXT_NODE) ||
4767 (cur->type == XML_CDATA_SECTION_NODE)) {
4768 sep = "/";
4769 name = "text()";
4770 next = cur->parent;
4771
4772 /*
4773 * Thumbler index computation
4774 */
4775 tmp = cur->prev;
4776 while (tmp != NULL) {
Kasimier T. Buchcikeb468702006-02-15 10:57:50 +00004777 if ((tmp->type == XML_TEXT_NODE) ||
4778 (tmp->type == XML_CDATA_SECTION_NODE))
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004779 occur++;
4780 tmp = tmp->prev;
4781 }
Kasimier T. Buchcikeb468702006-02-15 10:57:50 +00004782 /*
4783 * Evaluate if this is the only text- or CDATA-section-node;
4784 * if yes, then we'll get "text()", otherwise "text()[1]".
4785 */
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004786 if (occur == 0) {
4787 tmp = cur->next;
Kasimier T. Buchcikeb468702006-02-15 10:57:50 +00004788 while (tmp != NULL) {
4789 if ((tmp->type == XML_TEXT_NODE) ||
4790 (tmp->type == XML_CDATA_SECTION_NODE))
4791 {
4792 occur = 1;
4793 break;
Daniel Veillardaa6de472008-08-25 14:53:31 +00004794 }
Kasimier T. Buchcikeb468702006-02-15 10:57:50 +00004795 tmp = tmp->next;
4796 }
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004797 } else
4798 occur++;
4799 } else if (cur->type == XML_PI_NODE) {
4800 sep = "/";
4801 snprintf(nametemp, sizeof(nametemp) - 1,
William M. Brack13dfa872004-09-18 04:52:08 +00004802 "processing-instruction('%s')", (char *)cur->name);
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004803 nametemp[sizeof(nametemp) - 1] = 0;
4804 name = nametemp;
4805
4806 next = cur->parent;
4807
4808 /*
4809 * Thumbler index computation
4810 */
4811 tmp = cur->prev;
4812 while (tmp != NULL) {
4813 if ((tmp->type == XML_PI_NODE) &&
4814 (xmlStrEqual(cur->name, tmp->name)))
4815 occur++;
4816 tmp = tmp->prev;
4817 }
4818 if (occur == 0) {
4819 tmp = cur->next;
4820 while (tmp != NULL && occur == 0) {
4821 if ((tmp->type == XML_PI_NODE) &&
4822 (xmlStrEqual(cur->name, tmp->name)))
4823 occur++;
4824 tmp = tmp->next;
4825 }
4826 if (occur != 0)
4827 occur = 1;
4828 } else
4829 occur++;
4830
Daniel Veillard8faa7832001-11-26 15:58:08 +00004831 } else if (cur->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004832 sep = "/@";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004833 name = (const char *) (((xmlAttrPtr) cur)->name);
Daniel Veillard365c8062005-07-19 11:31:55 +00004834 if (cur->ns) {
4835 if (cur->ns->prefix != NULL)
4836 snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
Daniel Veillardaa6de472008-08-25 14:53:31 +00004837 (char *)cur->ns->prefix, (char *)cur->name);
Daniel Veillard365c8062005-07-19 11:31:55 +00004838 else
4839 snprintf(nametemp, sizeof(nametemp) - 1, "%s",
Daniel Veillardaa6de472008-08-25 14:53:31 +00004840 (char *)cur->name);
Daniel Veillard365c8062005-07-19 11:31:55 +00004841 nametemp[sizeof(nametemp) - 1] = 0;
4842 name = nametemp;
4843 }
Daniel Veillard8faa7832001-11-26 15:58:08 +00004844 next = ((xmlAttrPtr) cur)->parent;
4845 } else {
4846 next = cur->parent;
4847 }
4848
4849 /*
4850 * Make sure there is enough room
4851 */
4852 if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) {
4853 buf_len =
4854 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20;
4855 temp = (xmlChar *) xmlRealloc(buffer, buf_len);
4856 if (temp == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004857 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004858 xmlFree(buf);
4859 xmlFree(buffer);
4860 return (NULL);
4861 }
4862 buffer = temp;
4863 temp = (xmlChar *) xmlRealloc(buf, buf_len);
4864 if (temp == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004865 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004866 xmlFree(buf);
4867 xmlFree(buffer);
4868 return (NULL);
4869 }
4870 buf = temp;
4871 }
4872 if (occur == 0)
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004873 snprintf((char *) buf, buf_len, "%s%s%s",
Daniel Veillard8faa7832001-11-26 15:58:08 +00004874 sep, name, (char *) buffer);
4875 else
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004876 snprintf((char *) buf, buf_len, "%s%s[%d]%s",
Daniel Veillard8faa7832001-11-26 15:58:08 +00004877 sep, name, occur, (char *) buffer);
William M. Brack13dfa872004-09-18 04:52:08 +00004878 snprintf((char *) buffer, buf_len, "%s", (char *)buf);
Daniel Veillard8faa7832001-11-26 15:58:08 +00004879 cur = next;
4880 } while (cur != NULL);
4881 xmlFree(buf);
4882 return (buffer);
4883}
Daniel Veillard652327a2003-09-29 18:02:38 +00004884#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard8faa7832001-11-26 15:58:08 +00004885
4886/**
Owen Taylor3473f882001-02-23 17:55:21 +00004887 * xmlDocGetRootElement:
4888 * @doc: the document
4889 *
4890 * Get the root element of the document (doc->children is a list
4891 * containing possibly comments, PIs, etc ...).
4892 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004893 * Returns the #xmlNodePtr for the root or NULL
Owen Taylor3473f882001-02-23 17:55:21 +00004894 */
4895xmlNodePtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +08004896xmlDocGetRootElement(const xmlDoc *doc) {
Owen Taylor3473f882001-02-23 17:55:21 +00004897 xmlNodePtr ret;
4898
4899 if (doc == NULL) return(NULL);
4900 ret = doc->children;
4901 while (ret != NULL) {
4902 if (ret->type == XML_ELEMENT_NODE)
4903 return(ret);
4904 ret = ret->next;
4905 }
4906 return(ret);
4907}
Daniel Veillardaa6de472008-08-25 14:53:31 +00004908
Daniel Veillard2156d432004-03-04 15:59:36 +00004909#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004910/**
4911 * xmlDocSetRootElement:
4912 * @doc: the document
Daniel Veillard26a45c82006-10-20 12:55:34 +00004913 * @root: the new document root element, if root is NULL no action is taken,
4914 * to remove a node from a document use xmlUnlinkNode(root) instead.
Owen Taylor3473f882001-02-23 17:55:21 +00004915 *
4916 * Set the root element of the document (doc->children is a list
4917 * containing possibly comments, PIs, etc ...).
4918 *
Daniel Veillard26a45c82006-10-20 12:55:34 +00004919 * Returns the old root element if any was found, NULL if root was NULL
Owen Taylor3473f882001-02-23 17:55:21 +00004920 */
4921xmlNodePtr
4922xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) {
4923 xmlNodePtr old = NULL;
4924
4925 if (doc == NULL) return(NULL);
Daniel Veillard3e62adb2012-08-09 14:24:02 +08004926 if ((root == NULL) || (root->type == XML_NAMESPACE_DECL))
Daniel Veillardc575b992002-02-08 13:28:40 +00004927 return(NULL);
4928 xmlUnlinkNode(root);
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00004929 xmlSetTreeDoc(root, doc);
Daniel Veillardc575b992002-02-08 13:28:40 +00004930 root->parent = (xmlNodePtr) doc;
Owen Taylor3473f882001-02-23 17:55:21 +00004931 old = doc->children;
4932 while (old != NULL) {
4933 if (old->type == XML_ELEMENT_NODE)
4934 break;
4935 old = old->next;
4936 }
4937 if (old == NULL) {
4938 if (doc->children == NULL) {
4939 doc->children = root;
4940 doc->last = root;
4941 } else {
4942 xmlAddSibling(doc->children, root);
4943 }
4944 } else {
4945 xmlReplaceNode(old, root);
4946 }
4947 return(old);
4948}
Daniel Veillard2156d432004-03-04 15:59:36 +00004949#endif
Daniel Veillardaa6de472008-08-25 14:53:31 +00004950
Daniel Veillard2156d432004-03-04 15:59:36 +00004951#if defined(LIBXML_TREE_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004952/**
4953 * xmlNodeSetLang:
4954 * @cur: the node being changed
Daniel Veillardd1640922001-12-17 15:30:10 +00004955 * @lang: the language description
Owen Taylor3473f882001-02-23 17:55:21 +00004956 *
4957 * Set the language of a node, i.e. the values of the xml:lang
4958 * attribute.
4959 */
4960void
4961xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004962 xmlNsPtr ns;
4963
Owen Taylor3473f882001-02-23 17:55:21 +00004964 if (cur == NULL) return;
4965 switch(cur->type) {
4966 case XML_TEXT_NODE:
4967 case XML_CDATA_SECTION_NODE:
4968 case XML_COMMENT_NODE:
4969 case XML_DOCUMENT_NODE:
4970 case XML_DOCUMENT_TYPE_NODE:
4971 case XML_DOCUMENT_FRAG_NODE:
4972 case XML_NOTATION_NODE:
4973 case XML_HTML_DOCUMENT_NODE:
4974 case XML_DTD_NODE:
4975 case XML_ELEMENT_DECL:
4976 case XML_ATTRIBUTE_DECL:
4977 case XML_ENTITY_DECL:
4978 case XML_PI_NODE:
4979 case XML_ENTITY_REF_NODE:
4980 case XML_ENTITY_NODE:
4981 case XML_NAMESPACE_DECL:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004982#ifdef LIBXML_DOCB_ENABLED
4983 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004984#endif
4985 case XML_XINCLUDE_START:
4986 case XML_XINCLUDE_END:
4987 return;
4988 case XML_ELEMENT_NODE:
4989 case XML_ATTRIBUTE_NODE:
4990 break;
4991 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004992 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4993 if (ns == NULL)
4994 return;
4995 xmlSetNsProp(cur, ns, BAD_CAST "lang", lang);
Owen Taylor3473f882001-02-23 17:55:21 +00004996}
Daniel Veillard652327a2003-09-29 18:02:38 +00004997#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardaa6de472008-08-25 14:53:31 +00004998
Owen Taylor3473f882001-02-23 17:55:21 +00004999/**
5000 * xmlNodeGetLang:
5001 * @cur: the node being checked
5002 *
5003 * Searches the language of a node, i.e. the values of the xml:lang
5004 * attribute or the one carried by the nearest ancestor.
5005 *
5006 * Returns a pointer to the lang value, or NULL if not found
Daniel Veillardbd9afb52002-09-25 22:25:35 +00005007 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00005008 */
5009xmlChar *
Kurt Roeckx95ebe532014-10-13 16:06:21 +08005010xmlNodeGetLang(const xmlNode *cur) {
Owen Taylor3473f882001-02-23 17:55:21 +00005011 xmlChar *lang;
5012
Daniel Veillard3e62adb2012-08-09 14:24:02 +08005013 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
5014 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005015 while (cur != NULL) {
Daniel Veillardc17337c2001-05-09 10:51:31 +00005016 lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE);
Owen Taylor3473f882001-02-23 17:55:21 +00005017 if (lang != NULL)
5018 return(lang);
5019 cur = cur->parent;
5020 }
5021 return(NULL);
5022}
Daniel Veillardaa6de472008-08-25 14:53:31 +00005023
Owen Taylor3473f882001-02-23 17:55:21 +00005024
Daniel Veillard652327a2003-09-29 18:02:38 +00005025#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00005026/**
5027 * xmlNodeSetSpacePreserve:
5028 * @cur: the node being changed
5029 * @val: the xml:space value ("0": default, 1: "preserve")
5030 *
5031 * Set (or reset) the space preserving behaviour of a node, i.e. the
5032 * value of the xml:space attribute.
5033 */
5034void
5035xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005036 xmlNsPtr ns;
5037
Owen Taylor3473f882001-02-23 17:55:21 +00005038 if (cur == NULL) return;
5039 switch(cur->type) {
5040 case XML_TEXT_NODE:
5041 case XML_CDATA_SECTION_NODE:
5042 case XML_COMMENT_NODE:
5043 case XML_DOCUMENT_NODE:
5044 case XML_DOCUMENT_TYPE_NODE:
5045 case XML_DOCUMENT_FRAG_NODE:
5046 case XML_NOTATION_NODE:
5047 case XML_HTML_DOCUMENT_NODE:
5048 case XML_DTD_NODE:
5049 case XML_ELEMENT_DECL:
5050 case XML_ATTRIBUTE_DECL:
5051 case XML_ENTITY_DECL:
5052 case XML_PI_NODE:
5053 case XML_ENTITY_REF_NODE:
5054 case XML_ENTITY_NODE:
5055 case XML_NAMESPACE_DECL:
5056 case XML_XINCLUDE_START:
5057 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005058#ifdef LIBXML_DOCB_ENABLED
5059 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005060#endif
5061 return;
5062 case XML_ELEMENT_NODE:
5063 case XML_ATTRIBUTE_NODE:
5064 break;
5065 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005066 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
5067 if (ns == NULL)
5068 return;
Owen Taylor3473f882001-02-23 17:55:21 +00005069 switch (val) {
5070 case 0:
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005071 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "default");
Owen Taylor3473f882001-02-23 17:55:21 +00005072 break;
5073 case 1:
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005074 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "preserve");
Owen Taylor3473f882001-02-23 17:55:21 +00005075 break;
5076 }
5077}
Daniel Veillard652327a2003-09-29 18:02:38 +00005078#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005079
5080/**
5081 * xmlNodeGetSpacePreserve:
5082 * @cur: the node being checked
5083 *
5084 * Searches the space preserving behaviour of a node, i.e. the values
5085 * of the xml:space attribute or the one carried by the nearest
5086 * ancestor.
5087 *
Daniel Veillardd1640922001-12-17 15:30:10 +00005088 * Returns -1 if xml:space is not inherited, 0 if "default", 1 if "preserve"
Owen Taylor3473f882001-02-23 17:55:21 +00005089 */
5090int
Kurt Roeckx95ebe532014-10-13 16:06:21 +08005091xmlNodeGetSpacePreserve(const xmlNode *cur) {
Owen Taylor3473f882001-02-23 17:55:21 +00005092 xmlChar *space;
5093
Daniel Veillard3e62adb2012-08-09 14:24:02 +08005094 if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE))
5095 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00005096 while (cur != NULL) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005097 space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE);
Owen Taylor3473f882001-02-23 17:55:21 +00005098 if (space != NULL) {
5099 if (xmlStrEqual(space, BAD_CAST "preserve")) {
5100 xmlFree(space);
5101 return(1);
5102 }
5103 if (xmlStrEqual(space, BAD_CAST "default")) {
5104 xmlFree(space);
5105 return(0);
5106 }
5107 xmlFree(space);
5108 }
5109 cur = cur->parent;
5110 }
5111 return(-1);
5112}
Daniel Veillardaa6de472008-08-25 14:53:31 +00005113
Daniel Veillard652327a2003-09-29 18:02:38 +00005114#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00005115/**
5116 * xmlNodeSetName:
5117 * @cur: the node being changed
5118 * @name: the new tag name
5119 *
5120 * Set (or reset) the name of a node.
5121 */
5122void
5123xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) {
Daniel Veillardce244ad2004-11-05 10:03:46 +00005124 xmlDocPtr doc;
5125 xmlDictPtr dict;
Tristan Van Berkomf0dd6e12014-04-22 21:15:05 -04005126 const xmlChar *freeme = NULL;
Daniel Veillardce244ad2004-11-05 10:03:46 +00005127
Owen Taylor3473f882001-02-23 17:55:21 +00005128 if (cur == NULL) return;
5129 if (name == NULL) return;
5130 switch(cur->type) {
5131 case XML_TEXT_NODE:
5132 case XML_CDATA_SECTION_NODE:
5133 case XML_COMMENT_NODE:
5134 case XML_DOCUMENT_TYPE_NODE:
5135 case XML_DOCUMENT_FRAG_NODE:
5136 case XML_NOTATION_NODE:
5137 case XML_HTML_DOCUMENT_NODE:
5138 case XML_NAMESPACE_DECL:
5139 case XML_XINCLUDE_START:
5140 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005141#ifdef LIBXML_DOCB_ENABLED
5142 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005143#endif
5144 return;
5145 case XML_ELEMENT_NODE:
5146 case XML_ATTRIBUTE_NODE:
5147 case XML_PI_NODE:
5148 case XML_ENTITY_REF_NODE:
5149 case XML_ENTITY_NODE:
5150 case XML_DTD_NODE:
5151 case XML_DOCUMENT_NODE:
5152 case XML_ELEMENT_DECL:
5153 case XML_ATTRIBUTE_DECL:
5154 case XML_ENTITY_DECL:
5155 break;
5156 }
Daniel Veillardce244ad2004-11-05 10:03:46 +00005157 doc = cur->doc;
5158 if (doc != NULL)
5159 dict = doc->dict;
5160 else
5161 dict = NULL;
5162 if (dict != NULL) {
5163 if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name)))
Tristan Van Berkomf0dd6e12014-04-22 21:15:05 -04005164 freeme = cur->name;
Daniel Veillardce244ad2004-11-05 10:03:46 +00005165 cur->name = xmlDictLookup(dict, name, -1);
5166 } else {
Tristan Van Berkomf0dd6e12014-04-22 21:15:05 -04005167 if (cur->name != NULL)
5168 freeme = cur->name;
Daniel Veillardce244ad2004-11-05 10:03:46 +00005169 cur->name = xmlStrdup(name);
5170 }
Tristan Van Berkomf0dd6e12014-04-22 21:15:05 -04005171
5172 if (freeme)
5173 xmlFree((xmlChar *) freeme);
Owen Taylor3473f882001-02-23 17:55:21 +00005174}
Daniel Veillard2156d432004-03-04 15:59:36 +00005175#endif
Daniel Veillardaa6de472008-08-25 14:53:31 +00005176
Daniel Veillard2156d432004-03-04 15:59:36 +00005177#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00005178/**
5179 * xmlNodeSetBase:
5180 * @cur: the node being changed
5181 * @uri: the new base URI
5182 *
5183 * Set (or reset) the base URI of a node, i.e. the value of the
5184 * xml:base attribute.
5185 */
5186void
Daniel Veillardf85ce8e2003-09-22 10:24:45 +00005187xmlNodeSetBase(xmlNodePtr cur, const xmlChar* uri) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005188 xmlNsPtr ns;
Martin Trappelf3703102010-01-22 12:08:00 +01005189 xmlChar* fixed;
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005190
Owen Taylor3473f882001-02-23 17:55:21 +00005191 if (cur == NULL) return;
5192 switch(cur->type) {
5193 case XML_TEXT_NODE:
5194 case XML_CDATA_SECTION_NODE:
5195 case XML_COMMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005196 case XML_DOCUMENT_TYPE_NODE:
5197 case XML_DOCUMENT_FRAG_NODE:
5198 case XML_NOTATION_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005199 case XML_DTD_NODE:
5200 case XML_ELEMENT_DECL:
5201 case XML_ATTRIBUTE_DECL:
5202 case XML_ENTITY_DECL:
5203 case XML_PI_NODE:
5204 case XML_ENTITY_REF_NODE:
5205 case XML_ENTITY_NODE:
5206 case XML_NAMESPACE_DECL:
5207 case XML_XINCLUDE_START:
5208 case XML_XINCLUDE_END:
Owen Taylor3473f882001-02-23 17:55:21 +00005209 return;
5210 case XML_ELEMENT_NODE:
5211 case XML_ATTRIBUTE_NODE:
5212 break;
Daniel Veillard4cbe4702002-05-05 06:57:27 +00005213 case XML_DOCUMENT_NODE:
5214#ifdef LIBXML_DOCB_ENABLED
5215 case XML_DOCB_DOCUMENT_NODE:
5216#endif
5217 case XML_HTML_DOCUMENT_NODE: {
5218 xmlDocPtr doc = (xmlDocPtr) cur;
5219
5220 if (doc->URL != NULL)
5221 xmlFree((xmlChar *) doc->URL);
5222 if (uri == NULL)
5223 doc->URL = NULL;
5224 else
Daniel Veillardb8efdda2006-10-10 12:37:14 +00005225 doc->URL = xmlPathToURI(uri);
Daniel Veillard4cbe4702002-05-05 06:57:27 +00005226 return;
5227 }
Owen Taylor3473f882001-02-23 17:55:21 +00005228 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00005229
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005230 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
5231 if (ns == NULL)
5232 return;
Daniel Veillardb8efdda2006-10-10 12:37:14 +00005233 fixed = xmlPathToURI(uri);
5234 if (fixed != NULL) {
5235 xmlSetNsProp(cur, ns, BAD_CAST "base", fixed);
Martin Trappelf3703102010-01-22 12:08:00 +01005236 xmlFree(fixed);
Daniel Veillardb8efdda2006-10-10 12:37:14 +00005237 } else {
5238 xmlSetNsProp(cur, ns, BAD_CAST "base", uri);
5239 }
Owen Taylor3473f882001-02-23 17:55:21 +00005240}
Daniel Veillard652327a2003-09-29 18:02:38 +00005241#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005242
5243/**
Owen Taylor3473f882001-02-23 17:55:21 +00005244 * xmlNodeGetBase:
5245 * @doc: the document the node pertains to
5246 * @cur: the node being checked
5247 *
5248 * Searches for the BASE URL. The code should work on both XML
5249 * and HTML document even if base mechanisms are completely different.
5250 * It returns the base as defined in RFC 2396 sections
5251 * 5.1.1. Base URI within Document Content
5252 * and
5253 * 5.1.2. Base URI from the Encapsulating Entity
5254 * However it does not return the document base (5.1.3), use
Daniel Veillarde4d18492010-03-09 11:12:30 +01005255 * doc->URL in this case
Owen Taylor3473f882001-02-23 17:55:21 +00005256 *
5257 * Returns a pointer to the base URL, or NULL if not found
Daniel Veillardbd9afb52002-09-25 22:25:35 +00005258 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00005259 */
5260xmlChar *
Kurt Roeckx95ebe532014-10-13 16:06:21 +08005261xmlNodeGetBase(const xmlDoc *doc, const xmlNode *cur) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00005262 xmlChar *oldbase = NULL;
5263 xmlChar *base, *newbase;
Owen Taylor3473f882001-02-23 17:55:21 +00005264
Daniel Veillardaa6de472008-08-25 14:53:31 +00005265 if ((cur == NULL) && (doc == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00005266 return(NULL);
Daniel Veillard3e62adb2012-08-09 14:24:02 +08005267 if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
5268 return(NULL);
Daniel Veillardaa6de472008-08-25 14:53:31 +00005269 if (doc == NULL) doc = cur->doc;
Owen Taylor3473f882001-02-23 17:55:21 +00005270 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
5271 cur = doc->children;
5272 while ((cur != NULL) && (cur->name != NULL)) {
5273 if (cur->type != XML_ELEMENT_NODE) {
5274 cur = cur->next;
5275 continue;
5276 }
5277 if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) {
5278 cur = cur->children;
5279 continue;
5280 }
5281 if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) {
5282 cur = cur->children;
5283 continue;
5284 }
5285 if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) {
5286 return(xmlGetProp(cur, BAD_CAST "href"));
5287 }
5288 cur = cur->next;
5289 }
5290 return(NULL);
5291 }
5292 while (cur != NULL) {
5293 if (cur->type == XML_ENTITY_DECL) {
5294 xmlEntityPtr ent = (xmlEntityPtr) cur;
5295 return(xmlStrdup(ent->URI));
5296 }
Daniel Veillard42596ad2001-05-22 16:57:14 +00005297 if (cur->type == XML_ELEMENT_NODE) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00005298 base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE);
Daniel Veillard42596ad2001-05-22 16:57:14 +00005299 if (base != NULL) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00005300 if (oldbase != NULL) {
5301 newbase = xmlBuildURI(oldbase, base);
5302 if (newbase != NULL) {
5303 xmlFree(oldbase);
5304 xmlFree(base);
5305 oldbase = newbase;
5306 } else {
5307 xmlFree(oldbase);
5308 xmlFree(base);
5309 return(NULL);
5310 }
5311 } else {
5312 oldbase = base;
5313 }
5314 if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) ||
5315 (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) ||
5316 (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4)))
5317 return(oldbase);
Daniel Veillard42596ad2001-05-22 16:57:14 +00005318 }
5319 }
Owen Taylor3473f882001-02-23 17:55:21 +00005320 cur = cur->parent;
5321 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00005322 if ((doc != NULL) && (doc->URL != NULL)) {
5323 if (oldbase == NULL)
5324 return(xmlStrdup(doc->URL));
5325 newbase = xmlBuildURI(oldbase, doc->URL);
5326 xmlFree(oldbase);
5327 return(newbase);
5328 }
5329 return(oldbase);
Owen Taylor3473f882001-02-23 17:55:21 +00005330}
Daniel Veillardaa6de472008-08-25 14:53:31 +00005331
Owen Taylor3473f882001-02-23 17:55:21 +00005332/**
Daniel Veillard78697292003-10-19 20:44:43 +00005333 * xmlNodeBufGetContent:
5334 * @buffer: a buffer
5335 * @cur: the node being read
5336 *
5337 * Read the value of a node @cur, this can be either the text carried
5338 * directly by this node if it's a TEXT node or the aggregate string
5339 * of the values carried by this node child's (TEXT and ENTITY_REF).
5340 * Entity references are substituted.
5341 * Fills up the buffer @buffer with this value
Daniel Veillardaa6de472008-08-25 14:53:31 +00005342 *
Daniel Veillard78697292003-10-19 20:44:43 +00005343 * Returns 0 in case of success and -1 in case of error.
5344 */
5345int
Kurt Roeckx95ebe532014-10-13 16:06:21 +08005346xmlNodeBufGetContent(xmlBufferPtr buffer, const xmlNode *cur)
Daniel Veillard78697292003-10-19 20:44:43 +00005347{
Daniel Veillarddddeede2012-07-16 14:44:26 +08005348 xmlBufPtr buf;
5349 int ret;
5350
Daniel Veillard78697292003-10-19 20:44:43 +00005351 if ((cur == NULL) || (buffer == NULL)) return(-1);
Daniel Veillarddddeede2012-07-16 14:44:26 +08005352 buf = xmlBufFromBuffer(buffer);
5353 ret = xmlBufGetNodeContent(buf, cur);
5354 buffer = xmlBufBackToBuffer(buf);
5355 if ((ret < 0) || (buffer == NULL))
5356 return(-1);
5357 return(0);
5358}
5359
5360/**
5361 * xmlBufGetNodeContent:
Daniel Veillard28cc42d2012-08-10 10:00:18 +08005362 * @buf: a buffer xmlBufPtr
Daniel Veillarddddeede2012-07-16 14:44:26 +08005363 * @cur: the node being read
5364 *
5365 * Read the value of a node @cur, this can be either the text carried
5366 * directly by this node if it's a TEXT node or the aggregate string
5367 * of the values carried by this node child's (TEXT and ENTITY_REF).
5368 * Entity references are substituted.
Kurt Roeckx95ebe532014-10-13 16:06:21 +08005369 * Fills up the buffer @buf with this value
Daniel Veillarddddeede2012-07-16 14:44:26 +08005370 *
5371 * Returns 0 in case of success and -1 in case of error.
5372 */
5373int
Kurt Roeckx95ebe532014-10-13 16:06:21 +08005374xmlBufGetNodeContent(xmlBufPtr buf, const xmlNode *cur)
Daniel Veillarddddeede2012-07-16 14:44:26 +08005375{
5376 if ((cur == NULL) || (buf == NULL)) return(-1);
Daniel Veillard78697292003-10-19 20:44:43 +00005377 switch (cur->type) {
5378 case XML_CDATA_SECTION_NODE:
5379 case XML_TEXT_NODE:
Daniel Veillarddddeede2012-07-16 14:44:26 +08005380 xmlBufCat(buf, cur->content);
Daniel Veillard78697292003-10-19 20:44:43 +00005381 break;
5382 case XML_DOCUMENT_FRAG_NODE:
5383 case XML_ELEMENT_NODE:{
Kurt Roeckx95ebe532014-10-13 16:06:21 +08005384 const xmlNode *tmp = cur;
Daniel Veillard78697292003-10-19 20:44:43 +00005385
5386 while (tmp != NULL) {
5387 switch (tmp->type) {
5388 case XML_CDATA_SECTION_NODE:
5389 case XML_TEXT_NODE:
5390 if (tmp->content != NULL)
Daniel Veillarddddeede2012-07-16 14:44:26 +08005391 xmlBufCat(buf, tmp->content);
Daniel Veillard78697292003-10-19 20:44:43 +00005392 break;
5393 case XML_ENTITY_REF_NODE:
Daniel Veillarddddeede2012-07-16 14:44:26 +08005394 xmlBufGetNodeContent(buf, tmp);
Rob Richards77b92ff2005-12-20 15:55:14 +00005395 break;
Daniel Veillard78697292003-10-19 20:44:43 +00005396 default:
5397 break;
5398 }
5399 /*
5400 * Skip to next node
5401 */
5402 if (tmp->children != NULL) {
5403 if (tmp->children->type != XML_ENTITY_DECL) {
5404 tmp = tmp->children;
5405 continue;
5406 }
5407 }
5408 if (tmp == cur)
5409 break;
5410
5411 if (tmp->next != NULL) {
5412 tmp = tmp->next;
5413 continue;
5414 }
5415
5416 do {
5417 tmp = tmp->parent;
5418 if (tmp == NULL)
5419 break;
5420 if (tmp == cur) {
5421 tmp = NULL;
5422 break;
5423 }
5424 if (tmp->next != NULL) {
5425 tmp = tmp->next;
5426 break;
5427 }
5428 } while (tmp != NULL);
5429 }
5430 break;
5431 }
5432 case XML_ATTRIBUTE_NODE:{
5433 xmlAttrPtr attr = (xmlAttrPtr) cur;
5434 xmlNodePtr tmp = attr->children;
5435
5436 while (tmp != NULL) {
5437 if (tmp->type == XML_TEXT_NODE)
Daniel Veillarddddeede2012-07-16 14:44:26 +08005438 xmlBufCat(buf, tmp->content);
Daniel Veillard78697292003-10-19 20:44:43 +00005439 else
Daniel Veillarddddeede2012-07-16 14:44:26 +08005440 xmlBufGetNodeContent(buf, tmp);
Daniel Veillard78697292003-10-19 20:44:43 +00005441 tmp = tmp->next;
5442 }
5443 break;
5444 }
5445 case XML_COMMENT_NODE:
5446 case XML_PI_NODE:
Daniel Veillarddddeede2012-07-16 14:44:26 +08005447 xmlBufCat(buf, cur->content);
Daniel Veillard78697292003-10-19 20:44:43 +00005448 break;
5449 case XML_ENTITY_REF_NODE:{
5450 xmlEntityPtr ent;
5451 xmlNodePtr tmp;
5452
5453 /* lookup entity declaration */
5454 ent = xmlGetDocEntity(cur->doc, cur->name);
5455 if (ent == NULL)
5456 return(-1);
5457
5458 /* an entity content can be any "well balanced chunk",
5459 * i.e. the result of the content [43] production:
5460 * http://www.w3.org/TR/REC-xml#NT-content
5461 * -> we iterate through child nodes and recursive call
5462 * xmlNodeGetContent() which handles all possible node types */
5463 tmp = ent->children;
5464 while (tmp) {
Daniel Veillarddddeede2012-07-16 14:44:26 +08005465 xmlBufGetNodeContent(buf, tmp);
Daniel Veillard78697292003-10-19 20:44:43 +00005466 tmp = tmp->next;
5467 }
5468 break;
5469 }
5470 case XML_ENTITY_NODE:
5471 case XML_DOCUMENT_TYPE_NODE:
5472 case XML_NOTATION_NODE:
5473 case XML_DTD_NODE:
5474 case XML_XINCLUDE_START:
5475 case XML_XINCLUDE_END:
5476 break;
5477 case XML_DOCUMENT_NODE:
5478#ifdef LIBXML_DOCB_ENABLED
5479 case XML_DOCB_DOCUMENT_NODE:
5480#endif
5481 case XML_HTML_DOCUMENT_NODE:
5482 cur = cur->children;
5483 while (cur!= NULL) {
5484 if ((cur->type == XML_ELEMENT_NODE) ||
5485 (cur->type == XML_TEXT_NODE) ||
5486 (cur->type == XML_CDATA_SECTION_NODE)) {
Daniel Veillarddddeede2012-07-16 14:44:26 +08005487 xmlBufGetNodeContent(buf, cur);
Daniel Veillard78697292003-10-19 20:44:43 +00005488 }
5489 cur = cur->next;
5490 }
5491 break;
5492 case XML_NAMESPACE_DECL:
Daniel Veillarddddeede2012-07-16 14:44:26 +08005493 xmlBufCat(buf, ((xmlNsPtr) cur)->href);
Daniel Veillard78697292003-10-19 20:44:43 +00005494 break;
5495 case XML_ELEMENT_DECL:
5496 case XML_ATTRIBUTE_DECL:
5497 case XML_ENTITY_DECL:
5498 break;
5499 }
5500 return(0);
5501}
Daniel Veillarddddeede2012-07-16 14:44:26 +08005502
Daniel Veillard78697292003-10-19 20:44:43 +00005503/**
Owen Taylor3473f882001-02-23 17:55:21 +00005504 * xmlNodeGetContent:
5505 * @cur: the node being read
5506 *
5507 * Read the value of a node, this can be either the text carried
5508 * directly by this node if it's a TEXT node or the aggregate string
5509 * of the values carried by this node child's (TEXT and ENTITY_REF).
Daniel Veillardd1640922001-12-17 15:30:10 +00005510 * Entity references are substituted.
5511 * Returns a new #xmlChar * or NULL if no content is available.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00005512 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00005513 */
5514xmlChar *
Kurt Roeckx95ebe532014-10-13 16:06:21 +08005515xmlNodeGetContent(const xmlNode *cur)
Daniel Veillard7646b182002-04-20 06:41:40 +00005516{
5517 if (cur == NULL)
5518 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005519 switch (cur->type) {
5520 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005521 case XML_ELEMENT_NODE:{
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005522 xmlBufPtr buf;
Daniel Veillard7646b182002-04-20 06:41:40 +00005523 xmlChar *ret;
Owen Taylor3473f882001-02-23 17:55:21 +00005524
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005525 buf = xmlBufCreateSize(64);
5526 if (buf == NULL)
Daniel Veillard7646b182002-04-20 06:41:40 +00005527 return (NULL);
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005528 xmlBufGetNodeContent(buf, cur);
5529 ret = xmlBufDetach(buf);
5530 xmlBufFree(buf);
Daniel Veillard7646b182002-04-20 06:41:40 +00005531 return (ret);
5532 }
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005533 case XML_ATTRIBUTE_NODE:
5534 return(xmlGetPropNodeValueInternal((xmlAttrPtr) cur));
Owen Taylor3473f882001-02-23 17:55:21 +00005535 case XML_COMMENT_NODE:
5536 case XML_PI_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005537 if (cur->content != NULL)
5538 return (xmlStrdup(cur->content));
5539 return (NULL);
5540 case XML_ENTITY_REF_NODE:{
5541 xmlEntityPtr ent;
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005542 xmlBufPtr buf;
Daniel Veillard7646b182002-04-20 06:41:40 +00005543 xmlChar *ret;
5544
5545 /* lookup entity declaration */
5546 ent = xmlGetDocEntity(cur->doc, cur->name);
5547 if (ent == NULL)
5548 return (NULL);
5549
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005550 buf = xmlBufCreate();
5551 if (buf == NULL)
Daniel Veillard7646b182002-04-20 06:41:40 +00005552 return (NULL);
5553
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005554 xmlBufGetNodeContent(buf, cur);
Daniel Veillard7646b182002-04-20 06:41:40 +00005555
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005556 ret = xmlBufDetach(buf);
5557 xmlBufFree(buf);
Daniel Veillard7646b182002-04-20 06:41:40 +00005558 return (ret);
5559 }
Owen Taylor3473f882001-02-23 17:55:21 +00005560 case XML_ENTITY_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005561 case XML_DOCUMENT_TYPE_NODE:
5562 case XML_NOTATION_NODE:
5563 case XML_DTD_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005564 case XML_XINCLUDE_START:
5565 case XML_XINCLUDE_END:
Daniel Veillard9adc0462003-03-24 18:39:54 +00005566 return (NULL);
5567 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005568#ifdef LIBXML_DOCB_ENABLED
Daniel Veillard7646b182002-04-20 06:41:40 +00005569 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005570#endif
Daniel Veillard9adc0462003-03-24 18:39:54 +00005571 case XML_HTML_DOCUMENT_NODE: {
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005572 xmlBufPtr buf;
Daniel Veillardc4696922003-10-19 21:47:14 +00005573 xmlChar *ret;
Daniel Veillard9adc0462003-03-24 18:39:54 +00005574
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005575 buf = xmlBufCreate();
5576 if (buf == NULL)
Daniel Veillardc4696922003-10-19 21:47:14 +00005577 return (NULL);
5578
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005579 xmlBufGetNodeContent(buf, (xmlNodePtr) cur);
Daniel Veillardc4696922003-10-19 21:47:14 +00005580
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005581 ret = xmlBufDetach(buf);
5582 xmlBufFree(buf);
Daniel Veillardc4696922003-10-19 21:47:14 +00005583 return (ret);
Daniel Veillard9adc0462003-03-24 18:39:54 +00005584 }
Daniel Veillard96c3a3b2002-10-14 15:39:04 +00005585 case XML_NAMESPACE_DECL: {
5586 xmlChar *tmp;
5587
5588 tmp = xmlStrdup(((xmlNsPtr) cur)->href);
5589 return (tmp);
5590 }
Owen Taylor3473f882001-02-23 17:55:21 +00005591 case XML_ELEMENT_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00005592 /* TODO !!! */
5593 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005594 case XML_ATTRIBUTE_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00005595 /* TODO !!! */
5596 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005597 case XML_ENTITY_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00005598 /* TODO !!! */
5599 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005600 case XML_CDATA_SECTION_NODE:
5601 case XML_TEXT_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005602 if (cur->content != NULL)
5603 return (xmlStrdup(cur->content));
5604 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005605 }
Daniel Veillard7646b182002-04-20 06:41:40 +00005606 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005607}
Daniel Veillard652327a2003-09-29 18:02:38 +00005608
Owen Taylor3473f882001-02-23 17:55:21 +00005609/**
5610 * xmlNodeSetContent:
5611 * @cur: the node being modified
5612 * @content: the new value of the content
5613 *
5614 * Replace the content of a node.
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00005615 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
5616 * references, but XML special chars need to be escaped first by using
5617 * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars().
Owen Taylor3473f882001-02-23 17:55:21 +00005618 */
5619void
5620xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) {
5621 if (cur == NULL) {
5622#ifdef DEBUG_TREE
5623 xmlGenericError(xmlGenericErrorContext,
5624 "xmlNodeSetContent : node == NULL\n");
5625#endif
5626 return;
5627 }
5628 switch (cur->type) {
5629 case XML_DOCUMENT_FRAG_NODE:
5630 case XML_ELEMENT_NODE:
Daniel Veillard2c748c62002-01-16 15:37:50 +00005631 case XML_ATTRIBUTE_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005632 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5633 cur->children = xmlStringGetNodeList(cur->doc, content);
5634 UPDATE_LAST_CHILD_AND_PARENT(cur)
5635 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005636 case XML_TEXT_NODE:
5637 case XML_CDATA_SECTION_NODE:
5638 case XML_ENTITY_REF_NODE:
5639 case XML_ENTITY_NODE:
5640 case XML_PI_NODE:
5641 case XML_COMMENT_NODE:
Daniel Veillard8874b942005-08-25 13:19:21 +00005642 if ((cur->content != NULL) &&
5643 (cur->content != (xmlChar *) &(cur->properties))) {
William M. Brack7762bb12004-01-04 14:49:01 +00005644 if (!((cur->doc != NULL) && (cur->doc->dict != NULL) &&
Daniel Veillardc587bce2005-05-10 15:28:08 +00005645 (xmlDictOwns(cur->doc->dict, cur->content))))
William M. Brack7762bb12004-01-04 14:49:01 +00005646 xmlFree(cur->content);
Daniel Veillardaa6de472008-08-25 14:53:31 +00005647 }
Owen Taylor3473f882001-02-23 17:55:21 +00005648 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5649 cur->last = cur->children = NULL;
5650 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005651 cur->content = xmlStrdup(content);
Daniel Veillardaa6de472008-08-25 14:53:31 +00005652 } else
Owen Taylor3473f882001-02-23 17:55:21 +00005653 cur->content = NULL;
Daniel Veillard8874b942005-08-25 13:19:21 +00005654 cur->properties = NULL;
5655 cur->nsDef = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00005656 break;
5657 case XML_DOCUMENT_NODE:
5658 case XML_HTML_DOCUMENT_NODE:
5659 case XML_DOCUMENT_TYPE_NODE:
5660 case XML_XINCLUDE_START:
5661 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005662#ifdef LIBXML_DOCB_ENABLED
5663 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005664#endif
5665 break;
5666 case XML_NOTATION_NODE:
5667 break;
5668 case XML_DTD_NODE:
5669 break;
5670 case XML_NAMESPACE_DECL:
5671 break;
5672 case XML_ELEMENT_DECL:
5673 /* TODO !!! */
5674 break;
5675 case XML_ATTRIBUTE_DECL:
5676 /* TODO !!! */
5677 break;
5678 case XML_ENTITY_DECL:
5679 /* TODO !!! */
5680 break;
5681 }
5682}
5683
Daniel Veillard652327a2003-09-29 18:02:38 +00005684#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00005685/**
5686 * xmlNodeSetContentLen:
5687 * @cur: the node being modified
5688 * @content: the new value of the content
5689 * @len: the size of @content
5690 *
5691 * Replace the content of a node.
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00005692 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
5693 * references, but XML special chars need to be escaped first by using
5694 * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars().
Owen Taylor3473f882001-02-23 17:55:21 +00005695 */
5696void
5697xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5698 if (cur == NULL) {
5699#ifdef DEBUG_TREE
5700 xmlGenericError(xmlGenericErrorContext,
5701 "xmlNodeSetContentLen : node == NULL\n");
5702#endif
5703 return;
5704 }
5705 switch (cur->type) {
5706 case XML_DOCUMENT_FRAG_NODE:
5707 case XML_ELEMENT_NODE:
Daniel Veillard2c748c62002-01-16 15:37:50 +00005708 case XML_ATTRIBUTE_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005709 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5710 cur->children = xmlStringLenGetNodeList(cur->doc, content, len);
5711 UPDATE_LAST_CHILD_AND_PARENT(cur)
5712 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005713 case XML_TEXT_NODE:
5714 case XML_CDATA_SECTION_NODE:
5715 case XML_ENTITY_REF_NODE:
5716 case XML_ENTITY_NODE:
5717 case XML_PI_NODE:
5718 case XML_COMMENT_NODE:
5719 case XML_NOTATION_NODE:
Daniel Veillard8874b942005-08-25 13:19:21 +00005720 if ((cur->content != NULL) &&
5721 (cur->content != (xmlChar *) &(cur->properties))) {
5722 if (!((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5723 (xmlDictOwns(cur->doc->dict, cur->content))))
5724 xmlFree(cur->content);
Daniel Veillardaa6de472008-08-25 14:53:31 +00005725 }
Owen Taylor3473f882001-02-23 17:55:21 +00005726 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5727 cur->children = cur->last = NULL;
5728 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005729 cur->content = xmlStrndup(content, len);
Daniel Veillardaa6de472008-08-25 14:53:31 +00005730 } else
Owen Taylor3473f882001-02-23 17:55:21 +00005731 cur->content = NULL;
Daniel Veillard8874b942005-08-25 13:19:21 +00005732 cur->properties = NULL;
5733 cur->nsDef = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00005734 break;
5735 case XML_DOCUMENT_NODE:
5736 case XML_DTD_NODE:
5737 case XML_HTML_DOCUMENT_NODE:
5738 case XML_DOCUMENT_TYPE_NODE:
5739 case XML_NAMESPACE_DECL:
5740 case XML_XINCLUDE_START:
5741 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005742#ifdef LIBXML_DOCB_ENABLED
5743 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005744#endif
5745 break;
5746 case XML_ELEMENT_DECL:
5747 /* TODO !!! */
5748 break;
5749 case XML_ATTRIBUTE_DECL:
5750 /* TODO !!! */
5751 break;
5752 case XML_ENTITY_DECL:
5753 /* TODO !!! */
5754 break;
5755 }
5756}
Daniel Veillard652327a2003-09-29 18:02:38 +00005757#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005758
5759/**
5760 * xmlNodeAddContentLen:
5761 * @cur: the node being modified
5762 * @content: extra content
5763 * @len: the size of @content
Daniel Veillardaa6de472008-08-25 14:53:31 +00005764 *
Owen Taylor3473f882001-02-23 17:55:21 +00005765 * Append the extra substring to the node content.
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00005766 * NOTE: In contrast to xmlNodeSetContentLen(), @content is supposed to be
5767 * raw text, so unescaped XML special chars are allowed, entity
5768 * references are not supported.
Owen Taylor3473f882001-02-23 17:55:21 +00005769 */
5770void
5771xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5772 if (cur == NULL) {
5773#ifdef DEBUG_TREE
5774 xmlGenericError(xmlGenericErrorContext,
5775 "xmlNodeAddContentLen : node == NULL\n");
5776#endif
5777 return;
5778 }
5779 if (len <= 0) return;
5780 switch (cur->type) {
5781 case XML_DOCUMENT_FRAG_NODE:
5782 case XML_ELEMENT_NODE: {
Daniel Veillardacb2bda2002-01-13 16:15:43 +00005783 xmlNodePtr last, newNode, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00005784
Daniel Veillard7db37732001-07-12 01:20:08 +00005785 last = cur->last;
Owen Taylor3473f882001-02-23 17:55:21 +00005786 newNode = xmlNewTextLen(content, len);
5787 if (newNode != NULL) {
Daniel Veillardacb2bda2002-01-13 16:15:43 +00005788 tmp = xmlAddChild(cur, newNode);
5789 if (tmp != newNode)
5790 return;
Owen Taylor3473f882001-02-23 17:55:21 +00005791 if ((last != NULL) && (last->next == newNode)) {
5792 xmlTextMerge(last, newNode);
5793 }
5794 }
5795 break;
5796 }
5797 case XML_ATTRIBUTE_NODE:
5798 break;
5799 case XML_TEXT_NODE:
5800 case XML_CDATA_SECTION_NODE:
5801 case XML_ENTITY_REF_NODE:
5802 case XML_ENTITY_NODE:
5803 case XML_PI_NODE:
5804 case XML_COMMENT_NODE:
5805 case XML_NOTATION_NODE:
5806 if (content != NULL) {
Daniel Veillard8874b942005-08-25 13:19:21 +00005807 if ((cur->content == (xmlChar *) &(cur->properties)) ||
5808 ((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5809 xmlDictOwns(cur->doc->dict, cur->content))) {
5810 cur->content = xmlStrncatNew(cur->content, content, len);
5811 cur->properties = NULL;
5812 cur->nsDef = NULL;
William M. Brack7762bb12004-01-04 14:49:01 +00005813 break;
5814 }
Owen Taylor3473f882001-02-23 17:55:21 +00005815 cur->content = xmlStrncat(cur->content, content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00005816 }
5817 case XML_DOCUMENT_NODE:
5818 case XML_DTD_NODE:
5819 case XML_HTML_DOCUMENT_NODE:
5820 case XML_DOCUMENT_TYPE_NODE:
5821 case XML_NAMESPACE_DECL:
5822 case XML_XINCLUDE_START:
5823 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005824#ifdef LIBXML_DOCB_ENABLED
5825 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005826#endif
5827 break;
5828 case XML_ELEMENT_DECL:
5829 case XML_ATTRIBUTE_DECL:
5830 case XML_ENTITY_DECL:
5831 break;
5832 }
5833}
5834
5835/**
5836 * xmlNodeAddContent:
5837 * @cur: the node being modified
5838 * @content: extra content
Daniel Veillardaa6de472008-08-25 14:53:31 +00005839 *
Owen Taylor3473f882001-02-23 17:55:21 +00005840 * Append the extra substring to the node content.
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00005841 * NOTE: In contrast to xmlNodeSetContent(), @content is supposed to be
5842 * raw text, so unescaped XML special chars are allowed, entity
5843 * references are not supported.
Owen Taylor3473f882001-02-23 17:55:21 +00005844 */
5845void
5846xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) {
5847 int len;
5848
5849 if (cur == NULL) {
5850#ifdef DEBUG_TREE
5851 xmlGenericError(xmlGenericErrorContext,
5852 "xmlNodeAddContent : node == NULL\n");
5853#endif
5854 return;
5855 }
5856 if (content == NULL) return;
5857 len = xmlStrlen(content);
5858 xmlNodeAddContentLen(cur, content, len);
5859}
5860
5861/**
5862 * xmlTextMerge:
5863 * @first: the first text node
5864 * @second: the second text node being merged
Daniel Veillardaa6de472008-08-25 14:53:31 +00005865 *
Owen Taylor3473f882001-02-23 17:55:21 +00005866 * Merge two text nodes into one
5867 * Returns the first text node augmented
5868 */
5869xmlNodePtr
5870xmlTextMerge(xmlNodePtr first, xmlNodePtr second) {
5871 if (first == NULL) return(second);
5872 if (second == NULL) return(first);
5873 if (first->type != XML_TEXT_NODE) return(first);
5874 if (second->type != XML_TEXT_NODE) return(first);
5875 if (second->name != first->name)
5876 return(first);
Owen Taylor3473f882001-02-23 17:55:21 +00005877 xmlNodeAddContent(first, second->content);
Owen Taylor3473f882001-02-23 17:55:21 +00005878 xmlUnlinkNode(second);
5879 xmlFreeNode(second);
5880 return(first);
5881}
5882
Daniel Veillardf1a27c62006-10-13 22:33:03 +00005883#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00005884/**
5885 * xmlGetNsList:
5886 * @doc: the document
5887 * @node: the current node
5888 *
5889 * Search all the namespace applying to a given element.
Daniel Veillardd1640922001-12-17 15:30:10 +00005890 * Returns an NULL terminated array of all the #xmlNsPtr found
Owen Taylor3473f882001-02-23 17:55:21 +00005891 * that need to be freed by the caller or NULL if no
5892 * namespace if defined
5893 */
5894xmlNsPtr *
Kurt Roeckx95ebe532014-10-13 16:06:21 +08005895xmlGetNsList(const xmlDoc *doc ATTRIBUTE_UNUSED, const xmlNode *node)
Daniel Veillard77044732001-06-29 21:31:07 +00005896{
Owen Taylor3473f882001-02-23 17:55:21 +00005897 xmlNsPtr cur;
5898 xmlNsPtr *ret = NULL;
5899 int nbns = 0;
5900 int maxns = 10;
5901 int i;
5902
Daniel Veillard3e62adb2012-08-09 14:24:02 +08005903 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
5904 return(NULL);
5905
Owen Taylor3473f882001-02-23 17:55:21 +00005906 while (node != NULL) {
Daniel Veillard77044732001-06-29 21:31:07 +00005907 if (node->type == XML_ELEMENT_NODE) {
5908 cur = node->nsDef;
5909 while (cur != NULL) {
5910 if (ret == NULL) {
5911 ret =
5912 (xmlNsPtr *) xmlMalloc((maxns + 1) *
5913 sizeof(xmlNsPtr));
5914 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005915 xmlTreeErrMemory("getting namespace list");
Daniel Veillard77044732001-06-29 21:31:07 +00005916 return (NULL);
5917 }
5918 ret[nbns] = NULL;
5919 }
5920 for (i = 0; i < nbns; i++) {
5921 if ((cur->prefix == ret[i]->prefix) ||
5922 (xmlStrEqual(cur->prefix, ret[i]->prefix)))
5923 break;
5924 }
5925 if (i >= nbns) {
5926 if (nbns >= maxns) {
5927 maxns *= 2;
5928 ret = (xmlNsPtr *) xmlRealloc(ret,
5929 (maxns +
5930 1) *
5931 sizeof(xmlNsPtr));
5932 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005933 xmlTreeErrMemory("getting namespace list");
Daniel Veillard77044732001-06-29 21:31:07 +00005934 return (NULL);
5935 }
5936 }
5937 ret[nbns++] = cur;
5938 ret[nbns] = NULL;
5939 }
Owen Taylor3473f882001-02-23 17:55:21 +00005940
Daniel Veillard77044732001-06-29 21:31:07 +00005941 cur = cur->next;
5942 }
5943 }
5944 node = node->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00005945 }
Daniel Veillard77044732001-06-29 21:31:07 +00005946 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00005947}
Daniel Veillard652327a2003-09-29 18:02:38 +00005948#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005949
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005950/*
5951* xmlTreeEnsureXMLDecl:
5952* @doc: the doc
Daniel Veillardaa6de472008-08-25 14:53:31 +00005953*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005954* Ensures that there is an XML namespace declaration on the doc.
Daniel Veillardaa6de472008-08-25 14:53:31 +00005955*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005956* Returns the XML ns-struct or NULL on API and internal errors.
5957*/
5958static xmlNsPtr
5959xmlTreeEnsureXMLDecl(xmlDocPtr doc)
5960{
5961 if (doc == NULL)
5962 return (NULL);
5963 if (doc->oldNs != NULL)
5964 return (doc->oldNs);
5965 {
5966 xmlNsPtr ns;
5967 ns = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5968 if (ns == NULL) {
5969 xmlTreeErrMemory(
5970 "allocating the XML namespace");
5971 return (NULL);
5972 }
5973 memset(ns, 0, sizeof(xmlNs));
5974 ns->type = XML_LOCAL_NAMESPACE;
Daniel Veillardaa6de472008-08-25 14:53:31 +00005975 ns->href = xmlStrdup(XML_XML_NAMESPACE);
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005976 ns->prefix = xmlStrdup((const xmlChar *)"xml");
5977 doc->oldNs = ns;
5978 return (ns);
5979 }
5980}
5981
Owen Taylor3473f882001-02-23 17:55:21 +00005982/**
5983 * xmlSearchNs:
5984 * @doc: the document
5985 * @node: the current node
Daniel Veillard77851712001-02-27 21:54:07 +00005986 * @nameSpace: the namespace prefix
Owen Taylor3473f882001-02-23 17:55:21 +00005987 *
5988 * Search a Ns registered under a given name space for a document.
5989 * recurse on the parents until it finds the defined namespace
5990 * or return NULL otherwise.
5991 * @nameSpace can be NULL, this is a search for the default namespace.
5992 * We don't allow to cross entities boundaries. If you don't declare
5993 * the namespace within those you will be in troubles !!! A warning
5994 * is generated to cover this case.
5995 *
5996 * Returns the namespace pointer or NULL.
5997 */
5998xmlNsPtr
5999xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00006000
Owen Taylor3473f882001-02-23 17:55:21 +00006001 xmlNsPtr cur;
Kurt Roeckx95ebe532014-10-13 16:06:21 +08006002 const xmlNode *orig = node;
Owen Taylor3473f882001-02-23 17:55:21 +00006003
Daniel Veillard3e62adb2012-08-09 14:24:02 +08006004 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006005 if ((nameSpace != NULL) &&
6006 (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) {
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00006007 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
6008 /*
6009 * The XML-1.0 namespace is normally held on the root
6010 * element. In this case exceptionally create it on the
6011 * node element.
6012 */
6013 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
6014 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006015 xmlTreeErrMemory("searching namespace");
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00006016 return(NULL);
6017 }
6018 memset(cur, 0, sizeof(xmlNs));
6019 cur->type = XML_LOCAL_NAMESPACE;
Daniel Veillardaa6de472008-08-25 14:53:31 +00006020 cur->href = xmlStrdup(XML_XML_NAMESPACE);
6021 cur->prefix = xmlStrdup((const xmlChar *)"xml");
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00006022 cur->next = node->nsDef;
6023 node->nsDef = cur;
6024 return(cur);
6025 }
Daniel Veillard11ce4002006-03-10 00:36:23 +00006026 if (doc == NULL) {
6027 doc = node->doc;
6028 if (doc == NULL)
6029 return(NULL);
6030 }
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00006031 /*
6032 * Return the XML namespace declaration held by the doc.
6033 */
6034 if (doc->oldNs == NULL)
6035 return(xmlTreeEnsureXMLDecl(doc));
6036 else
6037 return(doc->oldNs);
Owen Taylor3473f882001-02-23 17:55:21 +00006038 }
6039 while (node != NULL) {
6040 if ((node->type == XML_ENTITY_REF_NODE) ||
6041 (node->type == XML_ENTITY_NODE) ||
6042 (node->type == XML_ENTITY_DECL))
6043 return(NULL);
6044 if (node->type == XML_ELEMENT_NODE) {
6045 cur = node->nsDef;
6046 while (cur != NULL) {
6047 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
6048 (cur->href != NULL))
6049 return(cur);
6050 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
6051 (cur->href != NULL) &&
6052 (xmlStrEqual(cur->prefix, nameSpace)))
6053 return(cur);
6054 cur = cur->next;
6055 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00006056 if (orig != node) {
Daniel Veillardf4e56292003-10-28 14:27:41 +00006057 cur = node->ns;
6058 if (cur != NULL) {
6059 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
6060 (cur->href != NULL))
6061 return(cur);
6062 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
6063 (cur->href != NULL) &&
6064 (xmlStrEqual(cur->prefix, nameSpace)))
6065 return(cur);
6066 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00006067 }
Owen Taylor3473f882001-02-23 17:55:21 +00006068 }
6069 node = node->parent;
6070 }
6071 return(NULL);
6072}
6073
6074/**
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006075 * xmlNsInScope:
6076 * @doc: the document
6077 * @node: the current node
6078 * @ancestor: the ancestor carrying the namespace
6079 * @prefix: the namespace prefix
6080 *
6081 * Verify that the given namespace held on @ancestor is still in scope
6082 * on node.
Daniel Veillardaa6de472008-08-25 14:53:31 +00006083 *
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006084 * Returns 1 if true, 0 if false and -1 in case of error.
6085 */
6086static int
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006087xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node,
6088 xmlNodePtr ancestor, const xmlChar * prefix)
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006089{
6090 xmlNsPtr tst;
6091
6092 while ((node != NULL) && (node != ancestor)) {
6093 if ((node->type == XML_ENTITY_REF_NODE) ||
6094 (node->type == XML_ENTITY_NODE) ||
6095 (node->type == XML_ENTITY_DECL))
6096 return (-1);
6097 if (node->type == XML_ELEMENT_NODE) {
6098 tst = node->nsDef;
6099 while (tst != NULL) {
6100 if ((tst->prefix == NULL)
6101 && (prefix == NULL))
6102 return (0);
6103 if ((tst->prefix != NULL)
6104 && (prefix != NULL)
6105 && (xmlStrEqual(tst->prefix, prefix)))
6106 return (0);
6107 tst = tst->next;
6108 }
6109 }
6110 node = node->parent;
6111 }
6112 if (node != ancestor)
6113 return (-1);
6114 return (1);
6115}
Daniel Veillardaa6de472008-08-25 14:53:31 +00006116
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006117/**
Owen Taylor3473f882001-02-23 17:55:21 +00006118 * xmlSearchNsByHref:
6119 * @doc: the document
6120 * @node: the current node
6121 * @href: the namespace value
6122 *
6123 * Search a Ns aliasing a given URI. Recurse on the parents until it finds
6124 * the defined namespace or return NULL otherwise.
6125 * Returns the namespace pointer or NULL.
6126 */
6127xmlNsPtr
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006128xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href)
6129{
Owen Taylor3473f882001-02-23 17:55:21 +00006130 xmlNsPtr cur;
6131 xmlNodePtr orig = node;
Daniel Veillard62040be2004-05-17 03:17:26 +00006132 int is_attr;
Owen Taylor3473f882001-02-23 17:55:21 +00006133
Daniel Veillard3e62adb2012-08-09 14:24:02 +08006134 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) || (href == NULL))
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006135 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006136 if (xmlStrEqual(href, XML_XML_NAMESPACE)) {
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006137 /*
6138 * Only the document can hold the XML spec namespace.
6139 */
6140 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
6141 /*
6142 * The XML-1.0 namespace is normally held on the root
6143 * element. In this case exceptionally create it on the
6144 * node element.
6145 */
6146 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
6147 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006148 xmlTreeErrMemory("searching namespace");
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006149 return (NULL);
6150 }
6151 memset(cur, 0, sizeof(xmlNs));
6152 cur->type = XML_LOCAL_NAMESPACE;
6153 cur->href = xmlStrdup(XML_XML_NAMESPACE);
6154 cur->prefix = xmlStrdup((const xmlChar *) "xml");
6155 cur->next = node->nsDef;
6156 node->nsDef = cur;
6157 return (cur);
6158 }
Daniel Veillard11ce4002006-03-10 00:36:23 +00006159 if (doc == NULL) {
6160 doc = node->doc;
6161 if (doc == NULL)
6162 return(NULL);
6163 }
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00006164 /*
6165 * Return the XML namespace declaration held by the doc.
6166 */
6167 if (doc->oldNs == NULL)
6168 return(xmlTreeEnsureXMLDecl(doc));
6169 else
Daniel Veillardaa6de472008-08-25 14:53:31 +00006170 return(doc->oldNs);
Owen Taylor3473f882001-02-23 17:55:21 +00006171 }
Daniel Veillard62040be2004-05-17 03:17:26 +00006172 is_attr = (node->type == XML_ATTRIBUTE_NODE);
Owen Taylor3473f882001-02-23 17:55:21 +00006173 while (node != NULL) {
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006174 if ((node->type == XML_ENTITY_REF_NODE) ||
6175 (node->type == XML_ENTITY_NODE) ||
6176 (node->type == XML_ENTITY_DECL))
6177 return (NULL);
6178 if (node->type == XML_ELEMENT_NODE) {
6179 cur = node->nsDef;
6180 while (cur != NULL) {
6181 if ((cur->href != NULL) && (href != NULL) &&
6182 (xmlStrEqual(cur->href, href))) {
Daniel Veillard62040be2004-05-17 03:17:26 +00006183 if (((!is_attr) || (cur->prefix != NULL)) &&
Kasimier T. Buchcikba70cc02005-02-28 10:28:21 +00006184 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006185 return (cur);
6186 }
6187 cur = cur->next;
6188 }
Daniel Veillardf4e56292003-10-28 14:27:41 +00006189 if (orig != node) {
6190 cur = node->ns;
6191 if (cur != NULL) {
6192 if ((cur->href != NULL) && (href != NULL) &&
6193 (xmlStrEqual(cur->href, href))) {
Daniel Veillard62040be2004-05-17 03:17:26 +00006194 if (((!is_attr) || (cur->prefix != NULL)) &&
Kasimier T. Buchcikba70cc02005-02-28 10:28:21 +00006195 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
Daniel Veillardf4e56292003-10-28 14:27:41 +00006196 return (cur);
6197 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006198 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00006199 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006200 }
6201 node = node->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00006202 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006203 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006204}
6205
6206/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00006207 * xmlNewReconciliedNs:
Owen Taylor3473f882001-02-23 17:55:21 +00006208 * @doc: the document
6209 * @tree: a node expected to hold the new namespace
6210 * @ns: the original namespace
6211 *
6212 * This function tries to locate a namespace definition in a tree
6213 * ancestors, or create a new namespace definition node similar to
6214 * @ns trying to reuse the same prefix. However if the given prefix is
6215 * null (default namespace) or reused within the subtree defined by
6216 * @tree or on one of its ancestors then a new prefix is generated.
6217 * Returns the (new) namespace definition or NULL in case of error
6218 */
Daniel Veillard8ed10722009-08-20 19:17:36 +02006219static xmlNsPtr
Owen Taylor3473f882001-02-23 17:55:21 +00006220xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) {
6221 xmlNsPtr def;
6222 xmlChar prefix[50];
6223 int counter = 1;
6224
Daniel Veillard3e62adb2012-08-09 14:24:02 +08006225 if ((tree == NULL) || (tree->type != XML_ELEMENT_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006226#ifdef DEBUG_TREE
6227 xmlGenericError(xmlGenericErrorContext,
6228 "xmlNewReconciliedNs : tree == NULL\n");
6229#endif
6230 return(NULL);
6231 }
Daniel Veillardce244ad2004-11-05 10:03:46 +00006232 if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006233#ifdef DEBUG_TREE
6234 xmlGenericError(xmlGenericErrorContext,
6235 "xmlNewReconciliedNs : ns == NULL\n");
6236#endif
6237 return(NULL);
6238 }
6239 /*
6240 * Search an existing namespace definition inherited.
6241 */
6242 def = xmlSearchNsByHref(doc, tree, ns->href);
6243 if (def != NULL)
6244 return(def);
6245
6246 /*
6247 * Find a close prefix which is not already in use.
6248 * Let's strip namespace prefixes longer than 20 chars !
6249 */
Daniel Veillardf742d342002-03-07 00:05:35 +00006250 if (ns->prefix == NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00006251 snprintf((char *) prefix, sizeof(prefix), "default");
Daniel Veillardf742d342002-03-07 00:05:35 +00006252 else
William M. Brack13dfa872004-09-18 04:52:08 +00006253 snprintf((char *) prefix, sizeof(prefix), "%.20s", (char *)ns->prefix);
Daniel Veillardf742d342002-03-07 00:05:35 +00006254
Owen Taylor3473f882001-02-23 17:55:21 +00006255 def = xmlSearchNs(doc, tree, prefix);
6256 while (def != NULL) {
6257 if (counter > 1000) return(NULL);
Daniel Veillardf742d342002-03-07 00:05:35 +00006258 if (ns->prefix == NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00006259 snprintf((char *) prefix, sizeof(prefix), "default%d", counter++);
Daniel Veillardf742d342002-03-07 00:05:35 +00006260 else
William M. Brack13dfa872004-09-18 04:52:08 +00006261 snprintf((char *) prefix, sizeof(prefix), "%.20s%d",
Daniel Veillardaa6de472008-08-25 14:53:31 +00006262 (char *)ns->prefix, counter++);
Owen Taylor3473f882001-02-23 17:55:21 +00006263 def = xmlSearchNs(doc, tree, prefix);
6264 }
6265
6266 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00006267 * OK, now we are ready to create a new one.
Owen Taylor3473f882001-02-23 17:55:21 +00006268 */
6269 def = xmlNewNs(tree, ns->href, prefix);
6270 return(def);
6271}
6272
Daniel Veillard652327a2003-09-29 18:02:38 +00006273#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00006274/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00006275 * xmlReconciliateNs:
Owen Taylor3473f882001-02-23 17:55:21 +00006276 * @doc: the document
6277 * @tree: a node defining the subtree to reconciliate
6278 *
6279 * This function checks that all the namespaces declared within the given
6280 * tree are properly declared. This is needed for example after Copy or Cut
6281 * and then paste operations. The subtree may still hold pointers to
6282 * namespace declarations outside the subtree or invalid/masked. As much
Daniel Veillardd1640922001-12-17 15:30:10 +00006283 * as possible the function try to reuse the existing namespaces found in
Owen Taylor3473f882001-02-23 17:55:21 +00006284 * the new environment. If not possible the new namespaces are redeclared
6285 * on @tree at the top of the given subtree.
6286 * Returns the number of namespace declarations created or -1 in case of error.
6287 */
6288int
6289xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) {
6290 xmlNsPtr *oldNs = NULL;
6291 xmlNsPtr *newNs = NULL;
6292 int sizeCache = 0;
6293 int nbCache = 0;
6294
6295 xmlNsPtr n;
6296 xmlNodePtr node = tree;
6297 xmlAttrPtr attr;
6298 int ret = 0, i;
6299
Daniel Veillardce244ad2004-11-05 10:03:46 +00006300 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) return(-1);
6301 if ((doc == NULL) || (doc->type != XML_DOCUMENT_NODE)) return(-1);
6302 if (node->doc != doc) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006303 while (node != NULL) {
6304 /*
6305 * Reconciliate the node namespace
6306 */
6307 if (node->ns != NULL) {
6308 /*
6309 * initialize the cache if needed
6310 */
6311 if (sizeCache == 0) {
6312 sizeCache = 10;
6313 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6314 sizeof(xmlNsPtr));
6315 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006316 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00006317 return(-1);
6318 }
6319 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6320 sizeof(xmlNsPtr));
6321 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006322 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00006323 xmlFree(oldNs);
6324 return(-1);
6325 }
6326 }
6327 for (i = 0;i < nbCache;i++) {
6328 if (oldNs[i] == node->ns) {
6329 node->ns = newNs[i];
6330 break;
6331 }
6332 }
6333 if (i == nbCache) {
6334 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00006335 * OK we need to recreate a new namespace definition
Owen Taylor3473f882001-02-23 17:55:21 +00006336 */
6337 n = xmlNewReconciliedNs(doc, tree, node->ns);
6338 if (n != NULL) { /* :-( what if else ??? */
6339 /*
6340 * check if we need to grow the cache buffers.
6341 */
6342 if (sizeCache <= nbCache) {
6343 sizeCache *= 2;
6344 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
6345 sizeof(xmlNsPtr));
6346 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006347 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00006348 xmlFree(newNs);
6349 return(-1);
6350 }
6351 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
6352 sizeof(xmlNsPtr));
6353 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006354 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00006355 xmlFree(oldNs);
6356 return(-1);
6357 }
6358 }
6359 newNs[nbCache] = n;
6360 oldNs[nbCache++] = node->ns;
6361 node->ns = n;
6362 }
6363 }
6364 }
6365 /*
6366 * now check for namespace hold by attributes on the node.
6367 */
Daniel Veillardb5f11972006-10-14 08:46:40 +00006368 if (node->type == XML_ELEMENT_NODE) {
6369 attr = node->properties;
6370 while (attr != NULL) {
6371 if (attr->ns != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00006372 /*
Daniel Veillardb5f11972006-10-14 08:46:40 +00006373 * initialize the cache if needed
Owen Taylor3473f882001-02-23 17:55:21 +00006374 */
Daniel Veillardb5f11972006-10-14 08:46:40 +00006375 if (sizeCache == 0) {
6376 sizeCache = 10;
6377 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6378 sizeof(xmlNsPtr));
6379 if (oldNs == NULL) {
6380 xmlTreeErrMemory("fixing namespaces");
6381 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006382 }
Daniel Veillardb5f11972006-10-14 08:46:40 +00006383 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6384 sizeof(xmlNsPtr));
6385 if (newNs == NULL) {
6386 xmlTreeErrMemory("fixing namespaces");
6387 xmlFree(oldNs);
6388 return(-1);
6389 }
6390 }
6391 for (i = 0;i < nbCache;i++) {
6392 if (oldNs[i] == attr->ns) {
6393 attr->ns = newNs[i];
6394 break;
6395 }
6396 }
6397 if (i == nbCache) {
6398 /*
6399 * OK we need to recreate a new namespace definition
6400 */
6401 n = xmlNewReconciliedNs(doc, tree, attr->ns);
6402 if (n != NULL) { /* :-( what if else ??? */
6403 /*
6404 * check if we need to grow the cache buffers.
6405 */
6406 if (sizeCache <= nbCache) {
6407 sizeCache *= 2;
6408 oldNs = (xmlNsPtr *) xmlRealloc(oldNs,
6409 sizeCache * sizeof(xmlNsPtr));
6410 if (oldNs == NULL) {
6411 xmlTreeErrMemory("fixing namespaces");
6412 xmlFree(newNs);
6413 return(-1);
6414 }
6415 newNs = (xmlNsPtr *) xmlRealloc(newNs,
6416 sizeCache * sizeof(xmlNsPtr));
6417 if (newNs == NULL) {
6418 xmlTreeErrMemory("fixing namespaces");
6419 xmlFree(oldNs);
6420 return(-1);
6421 }
6422 }
6423 newNs[nbCache] = n;
6424 oldNs[nbCache++] = attr->ns;
6425 attr->ns = n;
6426 }
Owen Taylor3473f882001-02-23 17:55:21 +00006427 }
6428 }
Daniel Veillardb5f11972006-10-14 08:46:40 +00006429 attr = attr->next;
Owen Taylor3473f882001-02-23 17:55:21 +00006430 }
Owen Taylor3473f882001-02-23 17:55:21 +00006431 }
6432
6433 /*
6434 * Browse the full subtree, deep first
6435 */
Daniel Veillardb5f11972006-10-14 08:46:40 +00006436 if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006437 /* deep first */
6438 node = node->children;
6439 } else if ((node != tree) && (node->next != NULL)) {
6440 /* then siblings */
6441 node = node->next;
6442 } else if (node != tree) {
6443 /* go up to parents->next if needed */
6444 while (node != tree) {
6445 if (node->parent != NULL)
6446 node = node->parent;
6447 if ((node != tree) && (node->next != NULL)) {
6448 node = node->next;
6449 break;
6450 }
6451 if (node->parent == NULL) {
6452 node = NULL;
6453 break;
6454 }
6455 }
6456 /* exit condition */
Daniel Veillardaa6de472008-08-25 14:53:31 +00006457 if (node == tree)
Owen Taylor3473f882001-02-23 17:55:21 +00006458 node = NULL;
Daniel Veillard1e774382002-03-06 17:35:40 +00006459 } else
6460 break;
Owen Taylor3473f882001-02-23 17:55:21 +00006461 }
Daniel Veillardf742d342002-03-07 00:05:35 +00006462 if (oldNs != NULL)
6463 xmlFree(oldNs);
6464 if (newNs != NULL)
6465 xmlFree(newNs);
Owen Taylor3473f882001-02-23 17:55:21 +00006466 return(ret);
6467}
Daniel Veillard652327a2003-09-29 18:02:38 +00006468#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00006469
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006470static xmlAttrPtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +08006471xmlGetPropNodeInternal(const xmlNode *node, const xmlChar *name,
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006472 const xmlChar *nsName, int useDTD)
6473{
6474 xmlAttrPtr prop;
6475
6476 if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL))
6477 return(NULL);
6478
6479 if (node->properties != NULL) {
6480 prop = node->properties;
6481 if (nsName == NULL) {
6482 /*
6483 * We want the attr to be in no namespace.
6484 */
6485 do {
6486 if ((prop->ns == NULL) && xmlStrEqual(prop->name, name)) {
6487 return(prop);
6488 }
6489 prop = prop->next;
6490 } while (prop != NULL);
6491 } else {
6492 /*
6493 * We want the attr to be in the specified namespace.
6494 */
6495 do {
6496 if ((prop->ns != NULL) && xmlStrEqual(prop->name, name) &&
6497 ((prop->ns->href == nsName) ||
6498 xmlStrEqual(prop->ns->href, nsName)))
6499 {
6500 return(prop);
6501 }
6502 prop = prop->next;
6503 } while (prop != NULL);
6504 }
6505 }
6506
6507#ifdef LIBXML_TREE_ENABLED
6508 if (! useDTD)
6509 return(NULL);
6510 /*
6511 * Check if there is a default/fixed attribute declaration in
6512 * the internal or external subset.
Daniel Veillardaa6de472008-08-25 14:53:31 +00006513 */
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006514 if ((node->doc != NULL) && (node->doc->intSubset != NULL)) {
6515 xmlDocPtr doc = node->doc;
6516 xmlAttributePtr attrDecl = NULL;
6517 xmlChar *elemQName, *tmpstr = NULL;
6518
6519 /*
Daniel Veillardaa6de472008-08-25 14:53:31 +00006520 * We need the QName of the element for the DTD-lookup.
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006521 */
6522 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
6523 tmpstr = xmlStrdup(node->ns->prefix);
6524 tmpstr = xmlStrcat(tmpstr, BAD_CAST ":");
6525 tmpstr = xmlStrcat(tmpstr, node->name);
6526 if (tmpstr == NULL)
6527 return(NULL);
6528 elemQName = tmpstr;
6529 } else
6530 elemQName = (xmlChar *) node->name;
6531 if (nsName == NULL) {
6532 /*
6533 * The common and nice case: Attr in no namespace.
6534 */
6535 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset,
6536 elemQName, name, NULL);
6537 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
6538 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset,
6539 elemQName, name, NULL);
6540 }
6541 } else {
6542 xmlNsPtr *nsList, *cur;
6543
6544 /*
6545 * The ugly case: Search using the prefixes of in-scope
6546 * ns-decls corresponding to @nsName.
6547 */
6548 nsList = xmlGetNsList(node->doc, node);
6549 if (nsList == NULL) {
6550 if (tmpstr != NULL)
6551 xmlFree(tmpstr);
6552 return(NULL);
6553 }
6554 cur = nsList;
6555 while (*cur != NULL) {
6556 if (xmlStrEqual((*cur)->href, nsName)) {
6557 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elemQName,
6558 name, (*cur)->prefix);
6559 if (attrDecl)
6560 break;
6561 if (doc->extSubset != NULL) {
6562 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elemQName,
6563 name, (*cur)->prefix);
6564 if (attrDecl)
6565 break;
6566 }
6567 }
6568 cur++;
6569 }
6570 xmlFree(nsList);
Daniel Veillardaa6de472008-08-25 14:53:31 +00006571 }
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006572 if (tmpstr != NULL)
6573 xmlFree(tmpstr);
6574 /*
6575 * Only default/fixed attrs are relevant.
6576 */
6577 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6578 return((xmlAttrPtr) attrDecl);
6579 }
6580#endif /* LIBXML_TREE_ENABLED */
6581 return(NULL);
6582}
6583
6584static xmlChar*
Kurt Roeckx95ebe532014-10-13 16:06:21 +08006585xmlGetPropNodeValueInternal(const xmlAttr *prop)
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006586{
6587 if (prop == NULL)
6588 return(NULL);
6589 if (prop->type == XML_ATTRIBUTE_NODE) {
6590 /*
6591 * Note that we return at least the empty string.
6592 * TODO: Do we really always want that?
6593 */
6594 if (prop->children != NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00006595 if ((prop->children->next == NULL) &&
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006596 ((prop->children->type == XML_TEXT_NODE) ||
6597 (prop->children->type == XML_CDATA_SECTION_NODE)))
6598 {
6599 /*
6600 * Optimization for the common case: only 1 text node.
6601 */
6602 return(xmlStrdup(prop->children->content));
6603 } else {
6604 xmlChar *ret;
6605
6606 ret = xmlNodeListGetString(prop->doc, prop->children, 1);
6607 if (ret != NULL)
6608 return(ret);
6609 }
6610 }
6611 return(xmlStrdup((xmlChar *)""));
6612 } else if (prop->type == XML_ATTRIBUTE_DECL) {
6613 return(xmlStrdup(((xmlAttributePtr)prop)->defaultValue));
6614 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00006615 return(NULL);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006616}
6617
Owen Taylor3473f882001-02-23 17:55:21 +00006618/**
6619 * xmlHasProp:
6620 * @node: the node
6621 * @name: the attribute name
6622 *
6623 * Search an attribute associated to a node
6624 * This function also looks in DTD attribute declaration for #FIXED or
6625 * default declaration values unless DTD use has been turned off.
6626 *
Daniel Veillardaa6de472008-08-25 14:53:31 +00006627 * Returns the attribute or the attribute declaration or NULL if
Owen Taylor3473f882001-02-23 17:55:21 +00006628 * neither was found.
6629 */
6630xmlAttrPtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +08006631xmlHasProp(const xmlNode *node, const xmlChar *name) {
Owen Taylor3473f882001-02-23 17:55:21 +00006632 xmlAttrPtr prop;
6633 xmlDocPtr doc;
6634
Daniel Veillard8874b942005-08-25 13:19:21 +00006635 if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL))
6636 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006637 /*
6638 * Check on the properties attached to the node
6639 */
6640 prop = node->properties;
6641 while (prop != NULL) {
6642 if (xmlStrEqual(prop->name, name)) {
6643 return(prop);
6644 }
6645 prop = prop->next;
6646 }
6647 if (!xmlCheckDTD) return(NULL);
6648
6649 /*
6650 * Check if there is a default declaration in the internal
6651 * or external subsets
6652 */
6653 doc = node->doc;
6654 if (doc != NULL) {
6655 xmlAttributePtr attrDecl;
6656 if (doc->intSubset != NULL) {
6657 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
6658 if ((attrDecl == NULL) && (doc->extSubset != NULL))
6659 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
Daniel Veillard75eb1ad2003-07-07 14:42:44 +00006660 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6661 /* return attribute declaration only if a default value is given
6662 (that includes #FIXED declarations) */
Owen Taylor3473f882001-02-23 17:55:21 +00006663 return((xmlAttrPtr) attrDecl);
6664 }
6665 }
6666 return(NULL);
6667}
6668
6669/**
Daniel Veillarde95e2392001-06-06 10:46:28 +00006670 * xmlHasNsProp:
6671 * @node: the node
6672 * @name: the attribute name
Daniel Veillardca2366a2001-06-11 12:09:01 +00006673 * @nameSpace: the URI of the namespace
Daniel Veillarde95e2392001-06-06 10:46:28 +00006674 *
6675 * Search for an attribute associated to a node
6676 * This attribute has to be anchored in the namespace specified.
6677 * This does the entity substitution.
6678 * This function looks in DTD attribute declaration for #FIXED or
6679 * default declaration values unless DTD use has been turned off.
William M. Brack2c228442004-10-03 04:10:00 +00006680 * Note that a namespace of NULL indicates to use the default namespace.
Daniel Veillarde95e2392001-06-06 10:46:28 +00006681 *
6682 * Returns the attribute or the attribute declaration or NULL
6683 * if neither was found.
6684 */
6685xmlAttrPtr
Kurt Roeckx95ebe532014-10-13 16:06:21 +08006686xmlHasNsProp(const xmlNode *node, const xmlChar *name, const xmlChar *nameSpace) {
Daniel Veillarde95e2392001-06-06 10:46:28 +00006687
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006688 return(xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD));
Daniel Veillarde95e2392001-06-06 10:46:28 +00006689}
6690
6691/**
Owen Taylor3473f882001-02-23 17:55:21 +00006692 * xmlGetProp:
6693 * @node: the node
6694 * @name: the attribute name
6695 *
6696 * Search and get the value of an attribute associated to a node
6697 * This does the entity substitution.
6698 * This function looks in DTD attribute declaration for #FIXED or
6699 * default declaration values unless DTD use has been turned off.
Daniel Veillard784b9352003-02-16 15:50:27 +00006700 * NOTE: this function acts independently of namespaces associated
Daniel Veillard71531f32003-02-05 13:19:53 +00006701 * to the attribute. Use xmlGetNsProp() or xmlGetNoNsProp()
6702 * for namespace aware processing.
Owen Taylor3473f882001-02-23 17:55:21 +00006703 *
6704 * Returns the attribute value or NULL if not found.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00006705 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00006706 */
6707xmlChar *
Kurt Roeckx95ebe532014-10-13 16:06:21 +08006708xmlGetProp(const xmlNode *node, const xmlChar *name) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00006709 xmlAttrPtr prop;
Owen Taylor3473f882001-02-23 17:55:21 +00006710
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006711 prop = xmlHasProp(node, name);
6712 if (prop == NULL)
6713 return(NULL);
Daniel Veillardaa6de472008-08-25 14:53:31 +00006714 return(xmlGetPropNodeValueInternal(prop));
Owen Taylor3473f882001-02-23 17:55:21 +00006715}
6716
6717/**
Daniel Veillard71531f32003-02-05 13:19:53 +00006718 * xmlGetNoNsProp:
6719 * @node: the node
6720 * @name: the attribute name
6721 *
6722 * Search and get the value of an attribute associated to a node
6723 * This does the entity substitution.
6724 * This function looks in DTD attribute declaration for #FIXED or
6725 * default declaration values unless DTD use has been turned off.
6726 * This function is similar to xmlGetProp except it will accept only
6727 * an attribute in no namespace.
6728 *
6729 * Returns the attribute value or NULL if not found.
6730 * It's up to the caller to free the memory with xmlFree().
6731 */
6732xmlChar *
Kurt Roeckx95ebe532014-10-13 16:06:21 +08006733xmlGetNoNsProp(const xmlNode *node, const xmlChar *name) {
Daniel Veillard71531f32003-02-05 13:19:53 +00006734 xmlAttrPtr prop;
Daniel Veillardaa6de472008-08-25 14:53:31 +00006735
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006736 prop = xmlGetPropNodeInternal(node, name, NULL, xmlCheckDTD);
6737 if (prop == NULL)
Daniel Veillard8874b942005-08-25 13:19:21 +00006738 return(NULL);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006739 return(xmlGetPropNodeValueInternal(prop));
Daniel Veillard71531f32003-02-05 13:19:53 +00006740}
6741
6742/**
Owen Taylor3473f882001-02-23 17:55:21 +00006743 * xmlGetNsProp:
6744 * @node: the node
6745 * @name: the attribute name
Daniel Veillardca2366a2001-06-11 12:09:01 +00006746 * @nameSpace: the URI of the namespace
Owen Taylor3473f882001-02-23 17:55:21 +00006747 *
6748 * Search and get the value of an attribute associated to a node
6749 * This attribute has to be anchored in the namespace specified.
6750 * This does the entity substitution.
6751 * This function looks in DTD attribute declaration for #FIXED or
6752 * default declaration values unless DTD use has been turned off.
6753 *
6754 * Returns the attribute value or NULL if not found.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00006755 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00006756 */
6757xmlChar *
Kurt Roeckx95ebe532014-10-13 16:06:21 +08006758xmlGetNsProp(const xmlNode *node, const xmlChar *name, const xmlChar *nameSpace) {
Owen Taylor3473f882001-02-23 17:55:21 +00006759 xmlAttrPtr prop;
Owen Taylor3473f882001-02-23 17:55:21 +00006760
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006761 prop = xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD);
6762 if (prop == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00006763 return(NULL);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006764 return(xmlGetPropNodeValueInternal(prop));
Owen Taylor3473f882001-02-23 17:55:21 +00006765}
6766
Daniel Veillard2156d432004-03-04 15:59:36 +00006767#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
6768/**
6769 * xmlUnsetProp:
6770 * @node: the node
6771 * @name: the attribute name
6772 *
6773 * Remove an attribute carried by a node.
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006774 * This handles only attributes in no namespace.
Daniel Veillard2156d432004-03-04 15:59:36 +00006775 * Returns 0 if successful, -1 if not found
6776 */
6777int
6778xmlUnsetProp(xmlNodePtr node, const xmlChar *name) {
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +00006779 xmlAttrPtr prop;
Daniel Veillard2156d432004-03-04 15:59:36 +00006780
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006781 prop = xmlGetPropNodeInternal(node, name, NULL, 0);
6782 if (prop == NULL)
Daniel Veillard2156d432004-03-04 15:59:36 +00006783 return(-1);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006784 xmlUnlinkNode((xmlNodePtr) prop);
6785 xmlFreeProp(prop);
6786 return(0);
Daniel Veillard2156d432004-03-04 15:59:36 +00006787}
6788
6789/**
6790 * xmlUnsetNsProp:
6791 * @node: the node
6792 * @ns: the namespace definition
6793 * @name: the attribute name
6794 *
6795 * Remove an attribute carried by a node.
6796 * Returns 0 if successful, -1 if not found
6797 */
6798int
6799xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) {
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +00006800 xmlAttrPtr prop;
Daniel Veillardaa6de472008-08-25 14:53:31 +00006801
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006802 prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0);
6803 if (prop == NULL)
Daniel Veillard2156d432004-03-04 15:59:36 +00006804 return(-1);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006805 xmlUnlinkNode((xmlNodePtr) prop);
6806 xmlFreeProp(prop);
6807 return(0);
Daniel Veillard2156d432004-03-04 15:59:36 +00006808}
6809#endif
6810
6811#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_HTML_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00006812/**
6813 * xmlSetProp:
6814 * @node: the node
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006815 * @name: the attribute name (a QName)
Owen Taylor3473f882001-02-23 17:55:21 +00006816 * @value: the attribute value
6817 *
6818 * Set (or reset) an attribute carried by a node.
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006819 * If @name has a prefix, then the corresponding
6820 * namespace-binding will be used, if in scope; it is an
6821 * error it there's no such ns-binding for the prefix in
6822 * scope.
Owen Taylor3473f882001-02-23 17:55:21 +00006823 * Returns the attribute pointer.
Daniel Veillardaa6de472008-08-25 14:53:31 +00006824 *
Owen Taylor3473f882001-02-23 17:55:21 +00006825 */
6826xmlAttrPtr
6827xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00006828 int len;
6829 const xmlChar *nqname;
Owen Taylor3473f882001-02-23 17:55:21 +00006830
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006831 if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE))
Owen Taylor3473f882001-02-23 17:55:21 +00006832 return(NULL);
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00006833
6834 /*
6835 * handle QNames
6836 */
6837 nqname = xmlSplitQName3(name, &len);
6838 if (nqname != NULL) {
6839 xmlNsPtr ns;
6840 xmlChar *prefix = xmlStrndup(name, len);
6841 ns = xmlSearchNs(node->doc, node, prefix);
6842 if (prefix != NULL)
6843 xmlFree(prefix);
6844 if (ns != NULL)
6845 return(xmlSetNsProp(node, ns, nqname, value));
6846 }
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006847 return(xmlSetNsProp(node, NULL, name, value));
Owen Taylor3473f882001-02-23 17:55:21 +00006848}
6849
6850/**
6851 * xmlSetNsProp:
6852 * @node: the node
6853 * @ns: the namespace definition
6854 * @name: the attribute name
6855 * @value: the attribute value
6856 *
6857 * Set (or reset) an attribute carried by a node.
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006858 * The ns structure must be in scope, this is not checked
Owen Taylor3473f882001-02-23 17:55:21 +00006859 *
6860 * Returns the attribute pointer.
6861 */
6862xmlAttrPtr
6863xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006864 const xmlChar *value)
6865{
Owen Taylor3473f882001-02-23 17:55:21 +00006866 xmlAttrPtr prop;
Daniel Veillardaa6de472008-08-25 14:53:31 +00006867
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006868 if (ns && (ns->href == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00006869 return(NULL);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006870 prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0);
6871 if (prop != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00006872 /*
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006873 * Modify the attribute's value.
6874 */
6875 if (prop->atype == XML_ATTRIBUTE_ID) {
6876 xmlRemoveID(node->doc, prop);
6877 prop->atype = XML_ATTRIBUTE_ID;
6878 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00006879 if (prop->children != NULL)
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006880 xmlFreeNodeList(prop->children);
6881 prop->children = NULL;
6882 prop->last = NULL;
6883 prop->ns = ns;
6884 if (value != NULL) {
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006885 xmlNodePtr tmp;
Daniel Veillardaa6de472008-08-25 14:53:31 +00006886
Daniel Veillard6f8611f2008-02-15 08:33:21 +00006887 if(!xmlCheckUTF8(value)) {
6888 xmlTreeErr(XML_TREE_NOT_UTF8, (xmlNodePtr) node->doc,
6889 NULL);
6890 if (node->doc != NULL)
6891 node->doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
6892 }
6893 prop->children = xmlNewDocText(node->doc, value);
Owen Taylor3473f882001-02-23 17:55:21 +00006894 prop->last = NULL;
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006895 tmp = prop->children;
6896 while (tmp != NULL) {
6897 tmp->parent = (xmlNodePtr) prop;
6898 if (tmp->next == NULL)
6899 prop->last = tmp;
6900 tmp = tmp->next;
6901 }
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006902 }
6903 if (prop->atype == XML_ATTRIBUTE_ID)
6904 xmlAddID(NULL, node->doc, value, prop);
6905 return(prop);
Owen Taylor3473f882001-02-23 17:55:21 +00006906 }
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006907 /*
6908 * No equal attr found; create a new one.
6909 */
6910 return(xmlNewPropInternal(node, ns, name, value, 0));
Owen Taylor3473f882001-02-23 17:55:21 +00006911}
6912
Daniel Veillard652327a2003-09-29 18:02:38 +00006913#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard75bea542001-05-11 17:41:21 +00006914
6915/**
Owen Taylor3473f882001-02-23 17:55:21 +00006916 * xmlNodeIsText:
6917 * @node: the node
Daniel Veillardaa6de472008-08-25 14:53:31 +00006918 *
Owen Taylor3473f882001-02-23 17:55:21 +00006919 * Is this node a Text node ?
6920 * Returns 1 yes, 0 no
6921 */
6922int
Kurt Roeckx95ebe532014-10-13 16:06:21 +08006923xmlNodeIsText(const xmlNode *node) {
Owen Taylor3473f882001-02-23 17:55:21 +00006924 if (node == NULL) return(0);
6925
6926 if (node->type == XML_TEXT_NODE) return(1);
6927 return(0);
6928}
6929
6930/**
6931 * xmlIsBlankNode:
6932 * @node: the node
Daniel Veillardaa6de472008-08-25 14:53:31 +00006933 *
Owen Taylor3473f882001-02-23 17:55:21 +00006934 * Checks whether this node is an empty or whitespace only
6935 * (and possibly ignorable) text-node.
6936 *
6937 * Returns 1 yes, 0 no
6938 */
6939int
Kurt Roeckx95ebe532014-10-13 16:06:21 +08006940xmlIsBlankNode(const xmlNode *node) {
Owen Taylor3473f882001-02-23 17:55:21 +00006941 const xmlChar *cur;
6942 if (node == NULL) return(0);
6943
Daniel Veillard7db37732001-07-12 01:20:08 +00006944 if ((node->type != XML_TEXT_NODE) &&
6945 (node->type != XML_CDATA_SECTION_NODE))
6946 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006947 if (node->content == NULL) return(1);
Owen Taylor3473f882001-02-23 17:55:21 +00006948 cur = node->content;
Owen Taylor3473f882001-02-23 17:55:21 +00006949 while (*cur != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00006950 if (!IS_BLANK_CH(*cur)) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006951 cur++;
6952 }
6953
6954 return(1);
6955}
6956
6957/**
6958 * xmlTextConcat:
6959 * @node: the node
6960 * @content: the content
Daniel Veillard60087f32001-10-10 09:45:09 +00006961 * @len: @content length
Daniel Veillardaa6de472008-08-25 14:53:31 +00006962 *
Owen Taylor3473f882001-02-23 17:55:21 +00006963 * Concat the given string at the end of the existing node content
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006964 *
6965 * Returns -1 in case of error, 0 otherwise
Owen Taylor3473f882001-02-23 17:55:21 +00006966 */
6967
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006968int
Owen Taylor3473f882001-02-23 17:55:21 +00006969xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006970 if (node == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006971
6972 if ((node->type != XML_TEXT_NODE) &&
Rob Richardsa02f1992006-09-16 14:04:26 +00006973 (node->type != XML_CDATA_SECTION_NODE) &&
6974 (node->type != XML_COMMENT_NODE) &&
6975 (node->type != XML_PI_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006976#ifdef DEBUG_TREE
6977 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006978 "xmlTextConcat: node is not text nor CDATA\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006979#endif
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006980 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006981 }
William M. Brack7762bb12004-01-04 14:49:01 +00006982 /* need to check if content is currently in the dictionary */
Daniel Veillard8874b942005-08-25 13:19:21 +00006983 if ((node->content == (xmlChar *) &(node->properties)) ||
6984 ((node->doc != NULL) && (node->doc->dict != NULL) &&
6985 xmlDictOwns(node->doc->dict, node->content))) {
William M. Brack7762bb12004-01-04 14:49:01 +00006986 node->content = xmlStrncatNew(node->content, content, len);
6987 } else {
6988 node->content = xmlStrncat(node->content, content, len);
6989 }
Daniel Veillard8874b942005-08-25 13:19:21 +00006990 node->properties = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006991 if (node->content == NULL)
6992 return(-1);
6993 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006994}
6995
6996/************************************************************************
6997 * *
6998 * Output : to a FILE or in memory *
6999 * *
7000 ************************************************************************/
7001
Owen Taylor3473f882001-02-23 17:55:21 +00007002/**
7003 * xmlBufferCreate:
7004 *
7005 * routine to create an XML buffer.
7006 * returns the new structure.
7007 */
7008xmlBufferPtr
7009xmlBufferCreate(void) {
7010 xmlBufferPtr ret;
7011
7012 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
7013 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007014 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00007015 return(NULL);
7016 }
7017 ret->use = 0;
Daniel Veillarde356c282001-03-10 12:32:04 +00007018 ret->size = xmlDefaultBufferSize;
Owen Taylor3473f882001-02-23 17:55:21 +00007019 ret->alloc = xmlBufferAllocScheme;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00007020 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00007021 if (ret->content == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007022 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00007023 xmlFree(ret);
7024 return(NULL);
7025 }
7026 ret->content[0] = 0;
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007027 ret->contentIO = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00007028 return(ret);
7029}
7030
7031/**
7032 * xmlBufferCreateSize:
7033 * @size: initial size of buffer
7034 *
7035 * routine to create an XML buffer.
7036 * returns the new structure.
7037 */
7038xmlBufferPtr
7039xmlBufferCreateSize(size_t size) {
7040 xmlBufferPtr ret;
7041
7042 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
7043 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007044 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00007045 return(NULL);
7046 }
7047 ret->use = 0;
7048 ret->alloc = xmlBufferAllocScheme;
7049 ret->size = (size ? size+2 : 0); /* +1 for ending null */
7050 if (ret->size){
Daniel Veillard3c908dc2003-04-19 00:07:51 +00007051 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00007052 if (ret->content == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007053 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00007054 xmlFree(ret);
7055 return(NULL);
7056 }
7057 ret->content[0] = 0;
7058 } else
7059 ret->content = NULL;
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007060 ret->contentIO = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00007061 return(ret);
7062}
7063
7064/**
Daniel Veillard79ee2842012-05-15 10:25:31 +08007065 * xmlBufferDetach:
Conrad Irwin7d553f82012-05-10 20:17:25 -07007066 * @buf: the buffer
7067 *
Daniel Veillard79ee2842012-05-15 10:25:31 +08007068 * Remove the string contained in a buffer and gie it back to the
Daniel Veillard94431ec2012-05-15 10:45:05 +08007069 * caller. The buffer is reset to an empty content.
7070 * This doesn't work with immutable buffers as they can't be reset.
Conrad Irwin7d553f82012-05-10 20:17:25 -07007071 *
Daniel Veillard79ee2842012-05-15 10:25:31 +08007072 * Returns the previous string contained by the buffer.
Conrad Irwin7d553f82012-05-10 20:17:25 -07007073 */
7074xmlChar *
7075xmlBufferDetach(xmlBufferPtr buf) {
7076 xmlChar *ret;
7077
Daniel Veillard94431ec2012-05-15 10:45:05 +08007078 if (buf == NULL)
7079 return(NULL);
7080 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE)
7081 return(NULL);
Conrad Irwin7d553f82012-05-10 20:17:25 -07007082
7083 ret = buf->content;
7084 buf->content = NULL;
7085 buf->size = 0;
7086 buf->use = 0;
7087
7088 return ret;
7089}
7090
7091
7092/**
Daniel Veillard53350552003-09-18 13:35:51 +00007093 * xmlBufferCreateStatic:
7094 * @mem: the memory area
7095 * @size: the size in byte
7096 *
MST 2003 John Flecka0e7e932003-12-19 03:13:47 +00007097 * routine to create an XML buffer from an immutable memory area.
7098 * The area won't be modified nor copied, and is expected to be
Daniel Veillard53350552003-09-18 13:35:51 +00007099 * present until the end of the buffer lifetime.
7100 *
7101 * returns the new structure.
7102 */
7103xmlBufferPtr
7104xmlBufferCreateStatic(void *mem, size_t size) {
7105 xmlBufferPtr ret;
7106
7107 if ((mem == NULL) || (size == 0))
7108 return(NULL);
7109
7110 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
7111 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007112 xmlTreeErrMemory("creating buffer");
Daniel Veillard53350552003-09-18 13:35:51 +00007113 return(NULL);
7114 }
7115 ret->use = size;
7116 ret->size = size;
7117 ret->alloc = XML_BUFFER_ALLOC_IMMUTABLE;
7118 ret->content = (xmlChar *) mem;
7119 return(ret);
7120}
7121
7122/**
Owen Taylor3473f882001-02-23 17:55:21 +00007123 * xmlBufferSetAllocationScheme:
Daniel Veillardbd9afb52002-09-25 22:25:35 +00007124 * @buf: the buffer to tune
Owen Taylor3473f882001-02-23 17:55:21 +00007125 * @scheme: allocation scheme to use
7126 *
7127 * Sets the allocation scheme for this buffer
7128 */
7129void
Daniel Veillardaa6de472008-08-25 14:53:31 +00007130xmlBufferSetAllocationScheme(xmlBufferPtr buf,
Owen Taylor3473f882001-02-23 17:55:21 +00007131 xmlBufferAllocationScheme scheme) {
7132 if (buf == NULL) {
7133#ifdef DEBUG_BUFFER
7134 xmlGenericError(xmlGenericErrorContext,
7135 "xmlBufferSetAllocationScheme: buf == NULL\n");
7136#endif
7137 return;
7138 }
Daniel Veillardda3fee42008-09-01 13:08:57 +00007139 if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) ||
7140 (buf->alloc == XML_BUFFER_ALLOC_IO)) return;
7141 if ((scheme == XML_BUFFER_ALLOC_DOUBLEIT) ||
7142 (scheme == XML_BUFFER_ALLOC_EXACT) ||
Conrad Irwin7d0d2a52012-05-14 14:18:58 +08007143 (scheme == XML_BUFFER_ALLOC_HYBRID) ||
Daniel Veillardda3fee42008-09-01 13:08:57 +00007144 (scheme == XML_BUFFER_ALLOC_IMMUTABLE))
7145 buf->alloc = scheme;
Owen Taylor3473f882001-02-23 17:55:21 +00007146}
7147
7148/**
7149 * xmlBufferFree:
7150 * @buf: the buffer to free
7151 *
Daniel Veillard9d06d302002-01-22 18:15:52 +00007152 * Frees an XML buffer. It frees both the content and the structure which
7153 * encapsulate it.
Owen Taylor3473f882001-02-23 17:55:21 +00007154 */
7155void
7156xmlBufferFree(xmlBufferPtr buf) {
7157 if (buf == NULL) {
7158#ifdef DEBUG_BUFFER
7159 xmlGenericError(xmlGenericErrorContext,
7160 "xmlBufferFree: buf == NULL\n");
7161#endif
7162 return;
7163 }
Daniel Veillard53350552003-09-18 13:35:51 +00007164
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007165 if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
7166 (buf->contentIO != NULL)) {
7167 xmlFree(buf->contentIO);
7168 } else if ((buf->content != NULL) &&
Daniel Veillard53350552003-09-18 13:35:51 +00007169 (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00007170 xmlFree(buf->content);
7171 }
Owen Taylor3473f882001-02-23 17:55:21 +00007172 xmlFree(buf);
7173}
7174
7175/**
7176 * xmlBufferEmpty:
7177 * @buf: the buffer
7178 *
7179 * empty a buffer.
7180 */
7181void
7182xmlBufferEmpty(xmlBufferPtr buf) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007183 if (buf == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +00007184 if (buf->content == NULL) return;
7185 buf->use = 0;
Daniel Veillard53350552003-09-18 13:35:51 +00007186 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00007187 buf->content = BAD_CAST "";
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007188 } else if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
7189 (buf->contentIO != NULL)) {
7190 size_t start_buf = buf->content - buf->contentIO;
7191
7192 buf->size += start_buf;
7193 buf->content = buf->contentIO;
7194 buf->content[0] = 0;
Daniel Veillard53350552003-09-18 13:35:51 +00007195 } else {
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007196 buf->content[0] = 0;
7197 }
Owen Taylor3473f882001-02-23 17:55:21 +00007198}
7199
7200/**
7201 * xmlBufferShrink:
7202 * @buf: the buffer to dump
7203 * @len: the number of xmlChar to remove
7204 *
7205 * Remove the beginning of an XML buffer.
7206 *
Daniel Veillardd1640922001-12-17 15:30:10 +00007207 * Returns the number of #xmlChar removed, or -1 in case of failure.
Owen Taylor3473f882001-02-23 17:55:21 +00007208 */
7209int
7210xmlBufferShrink(xmlBufferPtr buf, unsigned int len) {
Daniel Veillard3d97e662004-11-04 10:49:00 +00007211 if (buf == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00007212 if (len == 0) return(0);
7213 if (len > buf->use) return(-1);
7214
7215 buf->use -= len;
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007216 if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) ||
7217 ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL))) {
7218 /*
7219 * we just move the content pointer, but also make sure
7220 * the perceived buffer size has shrinked accordingly
7221 */
Daniel Veillard53350552003-09-18 13:35:51 +00007222 buf->content += len;
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007223 buf->size -= len;
7224
7225 /*
7226 * sometimes though it maybe be better to really shrink
7227 * on IO buffers
7228 */
7229 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7230 size_t start_buf = buf->content - buf->contentIO;
7231 if (start_buf >= buf->size) {
7232 memmove(buf->contentIO, &buf->content[0], buf->use);
7233 buf->content = buf->contentIO;
7234 buf->content[buf->use] = 0;
7235 buf->size += start_buf;
7236 }
7237 }
Daniel Veillard53350552003-09-18 13:35:51 +00007238 } else {
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007239 memmove(buf->content, &buf->content[len], buf->use);
Daniel Veillard53350552003-09-18 13:35:51 +00007240 buf->content[buf->use] = 0;
7241 }
Owen Taylor3473f882001-02-23 17:55:21 +00007242 return(len);
7243}
7244
7245/**
7246 * xmlBufferGrow:
7247 * @buf: the buffer
7248 * @len: the minimum free size to allocate
7249 *
7250 * Grow the available space of an XML buffer.
7251 *
7252 * Returns the new available space or -1 in case of error
7253 */
7254int
7255xmlBufferGrow(xmlBufferPtr buf, unsigned int len) {
7256 int size;
7257 xmlChar *newbuf;
7258
Daniel Veillard3d97e662004-11-04 10:49:00 +00007259 if (buf == NULL) return(-1);
7260
Daniel Veillard53350552003-09-18 13:35:51 +00007261 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00007262 if (len + buf->use < buf->size) return(0);
7263
Daniel Veillardee20cd72009-08-22 15:18:31 +02007264 /*
7265 * Windows has a BIG problem on realloc timing, so we try to double
7266 * the buffer size (if that's enough) (bug 146697)
7267 * Apparently BSD too, and it's probably best for linux too
7268 * On an embedded system this may be something to change
7269 */
7270#if 1
William M. Brack30fe43f2004-07-26 18:00:58 +00007271 if (buf->size > len)
7272 size = buf->size * 2;
7273 else
7274 size = buf->use + len + 100;
7275#else
Owen Taylor3473f882001-02-23 17:55:21 +00007276 size = buf->use + len + 100;
William M. Brack30fe43f2004-07-26 18:00:58 +00007277#endif
Owen Taylor3473f882001-02-23 17:55:21 +00007278
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007279 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7280 size_t start_buf = buf->content - buf->contentIO;
7281
7282 newbuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + size);
7283 if (newbuf == NULL) {
7284 xmlTreeErrMemory("growing buffer");
7285 return(-1);
7286 }
7287 buf->contentIO = newbuf;
7288 buf->content = newbuf + start_buf;
7289 } else {
7290 newbuf = (xmlChar *) xmlRealloc(buf->content, size);
7291 if (newbuf == NULL) {
7292 xmlTreeErrMemory("growing buffer");
7293 return(-1);
7294 }
7295 buf->content = newbuf;
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007296 }
Owen Taylor3473f882001-02-23 17:55:21 +00007297 buf->size = size;
7298 return(buf->size - buf->use);
7299}
7300
7301/**
7302 * xmlBufferDump:
7303 * @file: the file output
7304 * @buf: the buffer to dump
7305 *
7306 * Dumps an XML buffer to a FILE *.
Daniel Veillardd1640922001-12-17 15:30:10 +00007307 * Returns the number of #xmlChar written
Owen Taylor3473f882001-02-23 17:55:21 +00007308 */
7309int
7310xmlBufferDump(FILE *file, xmlBufferPtr buf) {
7311 int ret;
7312
7313 if (buf == NULL) {
7314#ifdef DEBUG_BUFFER
7315 xmlGenericError(xmlGenericErrorContext,
7316 "xmlBufferDump: buf == NULL\n");
7317#endif
7318 return(0);
7319 }
7320 if (buf->content == NULL) {
7321#ifdef DEBUG_BUFFER
7322 xmlGenericError(xmlGenericErrorContext,
7323 "xmlBufferDump: buf->content == NULL\n");
7324#endif
7325 return(0);
7326 }
Daniel Veillardcd337f02001-11-22 18:20:37 +00007327 if (file == NULL)
7328 file = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00007329 ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
7330 return(ret);
7331}
7332
7333/**
7334 * xmlBufferContent:
7335 * @buf: the buffer
7336 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00007337 * Function to extract the content of a buffer
7338 *
Owen Taylor3473f882001-02-23 17:55:21 +00007339 * Returns the internal content
7340 */
7341
Daniel Veillard5e2dace2001-07-18 19:30:27 +00007342const xmlChar *
Kurt Roeckx95ebe532014-10-13 16:06:21 +08007343xmlBufferContent(const xmlBuffer *buf)
Owen Taylor3473f882001-02-23 17:55:21 +00007344{
7345 if(!buf)
7346 return NULL;
7347
7348 return buf->content;
7349}
7350
7351/**
7352 * xmlBufferLength:
Daniel Veillardaa6de472008-08-25 14:53:31 +00007353 * @buf: the buffer
Owen Taylor3473f882001-02-23 17:55:21 +00007354 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00007355 * Function to get the length of a buffer
7356 *
Owen Taylor3473f882001-02-23 17:55:21 +00007357 * Returns the length of data in the internal content
7358 */
7359
7360int
Kurt Roeckx95ebe532014-10-13 16:06:21 +08007361xmlBufferLength(const xmlBuffer *buf)
Owen Taylor3473f882001-02-23 17:55:21 +00007362{
7363 if(!buf)
7364 return 0;
7365
7366 return buf->use;
7367}
7368
7369/**
7370 * xmlBufferResize:
7371 * @buf: the buffer to resize
7372 * @size: the desired size
7373 *
Daniel Veillardd1640922001-12-17 15:30:10 +00007374 * Resize a buffer to accommodate minimum size of @size.
Owen Taylor3473f882001-02-23 17:55:21 +00007375 *
7376 * Returns 0 in case of problems, 1 otherwise
7377 */
7378int
7379xmlBufferResize(xmlBufferPtr buf, unsigned int size)
7380{
7381 unsigned int newSize;
7382 xmlChar* rebuf = NULL;
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007383 size_t start_buf;
Owen Taylor3473f882001-02-23 17:55:21 +00007384
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007385 if (buf == NULL)
7386 return(0);
7387
Daniel Veillard53350552003-09-18 13:35:51 +00007388 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
7389
Owen Taylor3473f882001-02-23 17:55:21 +00007390 /* Don't resize if we don't have to */
7391 if (size < buf->size)
7392 return 1;
7393
7394 /* figure out new size */
7395 switch (buf->alloc){
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007396 case XML_BUFFER_ALLOC_IO:
7397 case XML_BUFFER_ALLOC_DOUBLEIT:
7398 /*take care of empty case*/
7399 newSize = (buf->size ? buf->size*2 : size + 10);
Daniel Veillard1dc9feb2008-11-17 15:59:21 +00007400 while (size > newSize) {
7401 if (newSize > UINT_MAX / 2) {
7402 xmlTreeErrMemory("growing buffer");
7403 return 0;
7404 }
7405 newSize *= 2;
7406 }
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007407 break;
7408 case XML_BUFFER_ALLOC_EXACT:
7409 newSize = size+10;
7410 break;
Conrad Irwin7d0d2a52012-05-14 14:18:58 +08007411 case XML_BUFFER_ALLOC_HYBRID:
7412 if (buf->use < BASE_BUFFER_SIZE)
7413 newSize = size;
7414 else {
7415 newSize = buf->size * 2;
7416 while (size > newSize) {
7417 if (newSize > UINT_MAX / 2) {
7418 xmlTreeErrMemory("growing buffer");
7419 return 0;
7420 }
7421 newSize *= 2;
7422 }
7423 }
7424 break;
7425
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007426 default:
7427 newSize = size+10;
7428 break;
Owen Taylor3473f882001-02-23 17:55:21 +00007429 }
7430
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007431 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7432 start_buf = buf->content - buf->contentIO;
7433
7434 if (start_buf > newSize) {
7435 /* move data back to start */
7436 memmove(buf->contentIO, buf->content, buf->use);
7437 buf->content = buf->contentIO;
7438 buf->content[buf->use] = 0;
7439 buf->size += start_buf;
7440 } else {
7441 rebuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + newSize);
7442 if (rebuf == NULL) {
7443 xmlTreeErrMemory("growing buffer");
7444 return 0;
7445 }
7446 buf->contentIO = rebuf;
7447 buf->content = rebuf + start_buf;
Daniel Veillard6155d8a2003-08-19 15:01:28 +00007448 }
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007449 } else {
7450 if (buf->content == NULL) {
7451 rebuf = (xmlChar *) xmlMallocAtomic(newSize);
7452 } else if (buf->size - buf->use < 100) {
7453 rebuf = (xmlChar *) xmlRealloc(buf->content, newSize);
7454 } else {
7455 /*
7456 * if we are reallocating a buffer far from being full, it's
7457 * better to make a new allocation and copy only the used range
7458 * and free the old one.
7459 */
7460 rebuf = (xmlChar *) xmlMallocAtomic(newSize);
7461 if (rebuf != NULL) {
7462 memcpy(rebuf, buf->content, buf->use);
7463 xmlFree(buf->content);
7464 rebuf[buf->use] = 0;
7465 }
7466 }
7467 if (rebuf == NULL) {
7468 xmlTreeErrMemory("growing buffer");
7469 return 0;
7470 }
7471 buf->content = rebuf;
Daniel Veillard6155d8a2003-08-19 15:01:28 +00007472 }
Owen Taylor3473f882001-02-23 17:55:21 +00007473 buf->size = newSize;
7474
7475 return 1;
7476}
7477
7478/**
7479 * xmlBufferAdd:
7480 * @buf: the buffer to dump
Daniel Veillardd1640922001-12-17 15:30:10 +00007481 * @str: the #xmlChar string
7482 * @len: the number of #xmlChar to add
Owen Taylor3473f882001-02-23 17:55:21 +00007483 *
Daniel Veillard60087f32001-10-10 09:45:09 +00007484 * Add a string range to an XML buffer. if len == -1, the length of
Owen Taylor3473f882001-02-23 17:55:21 +00007485 * str is recomputed.
William M. Bracka3215c72004-07-31 16:24:01 +00007486 *
7487 * Returns 0 successful, a positive error code number otherwise
7488 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00007489 */
William M. Bracka3215c72004-07-31 16:24:01 +00007490int
Owen Taylor3473f882001-02-23 17:55:21 +00007491xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) {
7492 unsigned int needSize;
7493
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007494 if ((str == NULL) || (buf == NULL)) {
William M. Bracka3215c72004-07-31 16:24:01 +00007495 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007496 }
William M. Bracka3215c72004-07-31 16:24:01 +00007497 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007498 if (len < -1) {
7499#ifdef DEBUG_BUFFER
7500 xmlGenericError(xmlGenericErrorContext,
7501 "xmlBufferAdd: len < 0\n");
7502#endif
William M. Bracka3215c72004-07-31 16:24:01 +00007503 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007504 }
William M. Bracka3215c72004-07-31 16:24:01 +00007505 if (len == 0) return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007506
7507 if (len < 0)
7508 len = xmlStrlen(str);
7509
Daniel Veillardc9923322007-04-24 18:12:06 +00007510 if (len < 0) return -1;
7511 if (len == 0) return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007512
7513 needSize = buf->use + len + 2;
7514 if (needSize > buf->size){
7515 if (!xmlBufferResize(buf, needSize)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007516 xmlTreeErrMemory("growing buffer");
William M. Bracka3215c72004-07-31 16:24:01 +00007517 return XML_ERR_NO_MEMORY;
Owen Taylor3473f882001-02-23 17:55:21 +00007518 }
7519 }
7520
7521 memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
7522 buf->use += len;
7523 buf->content[buf->use] = 0;
William M. Bracka3215c72004-07-31 16:24:01 +00007524 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007525}
7526
7527/**
7528 * xmlBufferAddHead:
7529 * @buf: the buffer
Daniel Veillardd1640922001-12-17 15:30:10 +00007530 * @str: the #xmlChar string
7531 * @len: the number of #xmlChar to add
Owen Taylor3473f882001-02-23 17:55:21 +00007532 *
7533 * Add a string range to the beginning of an XML buffer.
Daniel Veillard60087f32001-10-10 09:45:09 +00007534 * if len == -1, the length of @str is recomputed.
William M. Bracka3215c72004-07-31 16:24:01 +00007535 *
7536 * Returns 0 successful, a positive error code number otherwise
7537 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00007538 */
William M. Bracka3215c72004-07-31 16:24:01 +00007539int
Owen Taylor3473f882001-02-23 17:55:21 +00007540xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) {
7541 unsigned int needSize;
7542
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007543 if (buf == NULL)
7544 return(-1);
William M. Bracka3215c72004-07-31 16:24:01 +00007545 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007546 if (str == NULL) {
7547#ifdef DEBUG_BUFFER
7548 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007549 "xmlBufferAddHead: str == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007550#endif
William M. Bracka3215c72004-07-31 16:24:01 +00007551 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007552 }
7553 if (len < -1) {
7554#ifdef DEBUG_BUFFER
7555 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007556 "xmlBufferAddHead: len < 0\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007557#endif
William M. Bracka3215c72004-07-31 16:24:01 +00007558 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007559 }
William M. Bracka3215c72004-07-31 16:24:01 +00007560 if (len == 0) return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007561
7562 if (len < 0)
7563 len = xmlStrlen(str);
7564
William M. Bracka3215c72004-07-31 16:24:01 +00007565 if (len <= 0) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007566
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007567 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7568 size_t start_buf = buf->content - buf->contentIO;
7569
7570 if (start_buf > (unsigned int) len) {
7571 /*
7572 * We can add it in the space previously shrinked
7573 */
7574 buf->content -= len;
7575 memmove(&buf->content[0], str, len);
7576 buf->use += len;
7577 buf->size += len;
7578 return(0);
7579 }
7580 }
Owen Taylor3473f882001-02-23 17:55:21 +00007581 needSize = buf->use + len + 2;
7582 if (needSize > buf->size){
7583 if (!xmlBufferResize(buf, needSize)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007584 xmlTreeErrMemory("growing buffer");
William M. Bracka3215c72004-07-31 16:24:01 +00007585 return XML_ERR_NO_MEMORY;
Owen Taylor3473f882001-02-23 17:55:21 +00007586 }
7587 }
7588
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007589 memmove(&buf->content[len], &buf->content[0], buf->use);
7590 memmove(&buf->content[0], str, len);
Owen Taylor3473f882001-02-23 17:55:21 +00007591 buf->use += len;
7592 buf->content[buf->use] = 0;
William M. Bracka3215c72004-07-31 16:24:01 +00007593 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007594}
7595
7596/**
7597 * xmlBufferCat:
William M. Bracka3215c72004-07-31 16:24:01 +00007598 * @buf: the buffer to add to
Daniel Veillardd1640922001-12-17 15:30:10 +00007599 * @str: the #xmlChar string
Owen Taylor3473f882001-02-23 17:55:21 +00007600 *
7601 * Append a zero terminated string to an XML buffer.
William M. Bracka3215c72004-07-31 16:24:01 +00007602 *
7603 * Returns 0 successful, a positive error code number otherwise
7604 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00007605 */
William M. Bracka3215c72004-07-31 16:24:01 +00007606int
Owen Taylor3473f882001-02-23 17:55:21 +00007607xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007608 if (buf == NULL)
7609 return(-1);
William M. Bracka3215c72004-07-31 16:24:01 +00007610 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7611 if (str == NULL) return -1;
7612 return xmlBufferAdd(buf, str, -1);
Owen Taylor3473f882001-02-23 17:55:21 +00007613}
7614
7615/**
7616 * xmlBufferCCat:
7617 * @buf: the buffer to dump
7618 * @str: the C char string
7619 *
7620 * Append a zero terminated C string to an XML buffer.
William M. Bracka3215c72004-07-31 16:24:01 +00007621 *
7622 * Returns 0 successful, a positive error code number otherwise
7623 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00007624 */
William M. Bracka3215c72004-07-31 16:24:01 +00007625int
Owen Taylor3473f882001-02-23 17:55:21 +00007626xmlBufferCCat(xmlBufferPtr buf, const char *str) {
7627 const char *cur;
7628
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007629 if (buf == NULL)
7630 return(-1);
William M. Bracka3215c72004-07-31 16:24:01 +00007631 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007632 if (str == NULL) {
7633#ifdef DEBUG_BUFFER
7634 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007635 "xmlBufferCCat: str == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007636#endif
William M. Bracka3215c72004-07-31 16:24:01 +00007637 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007638 }
7639 for (cur = str;*cur != 0;cur++) {
7640 if (buf->use + 10 >= buf->size) {
7641 if (!xmlBufferResize(buf, buf->use+10)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007642 xmlTreeErrMemory("growing buffer");
William M. Bracka3215c72004-07-31 16:24:01 +00007643 return XML_ERR_NO_MEMORY;
Owen Taylor3473f882001-02-23 17:55:21 +00007644 }
7645 }
7646 buf->content[buf->use++] = *cur;
7647 }
7648 buf->content[buf->use] = 0;
William M. Bracka3215c72004-07-31 16:24:01 +00007649 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007650}
7651
7652/**
7653 * xmlBufferWriteCHAR:
7654 * @buf: the XML buffer
7655 * @string: the string to add
7656 *
7657 * routine which manages and grows an output buffer. This one adds
7658 * xmlChars at the end of the buffer.
7659 */
7660void
Daniel Veillard53350552003-09-18 13:35:51 +00007661xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007662 if (buf == NULL)
7663 return;
Daniel Veillard53350552003-09-18 13:35:51 +00007664 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00007665 xmlBufferCat(buf, string);
7666}
7667
7668/**
7669 * xmlBufferWriteChar:
7670 * @buf: the XML buffer output
7671 * @string: the string to add
7672 *
7673 * routine which manage and grows an output buffer. This one add
7674 * C chars at the end of the array.
7675 */
7676void
7677xmlBufferWriteChar(xmlBufferPtr buf, const char *string) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007678 if (buf == NULL)
7679 return;
Daniel Veillard53350552003-09-18 13:35:51 +00007680 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00007681 xmlBufferCCat(buf, string);
7682}
7683
7684
7685/**
7686 * xmlBufferWriteQuotedString:
7687 * @buf: the XML buffer output
7688 * @string: the string to add
7689 *
7690 * routine which manage and grows an output buffer. This one writes
Daniel Veillardd1640922001-12-17 15:30:10 +00007691 * a quoted or double quoted #xmlChar string, checking first if it holds
Owen Taylor3473f882001-02-23 17:55:21 +00007692 * quote or double-quotes internally
7693 */
7694void
7695xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) {
Daniel Veillard39057f42003-08-04 01:33:43 +00007696 const xmlChar *cur, *base;
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007697 if (buf == NULL)
7698 return;
Daniel Veillard53350552003-09-18 13:35:51 +00007699 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Daniel Veillard39057f42003-08-04 01:33:43 +00007700 if (xmlStrchr(string, '\"')) {
Daniel Veillard20aa0fb2003-08-04 19:43:15 +00007701 if (xmlStrchr(string, '\'')) {
Owen Taylor3473f882001-02-23 17:55:21 +00007702#ifdef DEBUG_BUFFER
7703 xmlGenericError(xmlGenericErrorContext,
7704 "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n");
7705#endif
Daniel Veillard39057f42003-08-04 01:33:43 +00007706 xmlBufferCCat(buf, "\"");
7707 base = cur = string;
7708 while(*cur != 0){
7709 if(*cur == '"'){
7710 if (base != cur)
7711 xmlBufferAdd(buf, base, cur - base);
7712 xmlBufferAdd(buf, BAD_CAST "&quot;", 6);
7713 cur++;
7714 base = cur;
7715 }
7716 else {
7717 cur++;
7718 }
7719 }
7720 if (base != cur)
7721 xmlBufferAdd(buf, base, cur - base);
7722 xmlBufferCCat(buf, "\"");
Owen Taylor3473f882001-02-23 17:55:21 +00007723 }
Daniel Veillard39057f42003-08-04 01:33:43 +00007724 else{
7725 xmlBufferCCat(buf, "\'");
7726 xmlBufferCat(buf, string);
7727 xmlBufferCCat(buf, "\'");
7728 }
Owen Taylor3473f882001-02-23 17:55:21 +00007729 } else {
7730 xmlBufferCCat(buf, "\"");
7731 xmlBufferCat(buf, string);
7732 xmlBufferCCat(buf, "\"");
7733 }
7734}
7735
7736
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007737/**
7738 * xmlGetDocCompressMode:
7739 * @doc: the document
7740 *
7741 * get the compression ratio for a document, ZLIB based
7742 * Returns 0 (uncompressed) to 9 (max compression)
7743 */
7744int
Kurt Roeckx95ebe532014-10-13 16:06:21 +08007745xmlGetDocCompressMode (const xmlDoc *doc) {
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007746 if (doc == NULL) return(-1);
7747 return(doc->compression);
7748}
7749
7750/**
7751 * xmlSetDocCompressMode:
7752 * @doc: the document
7753 * @mode: the compression ratio
7754 *
7755 * set the compression ratio for a document, ZLIB based
7756 * Correct values: 0 (uncompressed) to 9 (max compression)
7757 */
7758void
7759xmlSetDocCompressMode (xmlDocPtr doc, int mode) {
7760 if (doc == NULL) return;
7761 if (mode < 0) doc->compression = 0;
7762 else if (mode > 9) doc->compression = 9;
7763 else doc->compression = mode;
7764}
7765
7766/**
7767 * xmlGetCompressMode:
7768 *
7769 * get the default compression mode used, ZLIB based.
7770 * Returns 0 (uncompressed) to 9 (max compression)
7771 */
7772int
7773xmlGetCompressMode(void)
7774{
7775 return (xmlCompressMode);
7776}
7777
7778/**
7779 * xmlSetCompressMode:
7780 * @mode: the compression ratio
7781 *
7782 * set the default compression mode used, ZLIB based
7783 * Correct values: 0 (uncompressed) to 9 (max compression)
7784 */
7785void
7786xmlSetCompressMode(int mode) {
7787 if (mode < 0) xmlCompressMode = 0;
7788 else if (mode > 9) xmlCompressMode = 9;
7789 else xmlCompressMode = mode;
7790}
7791
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007792#define XML_TREE_NSMAP_PARENT -1
7793#define XML_TREE_NSMAP_XML -2
7794#define XML_TREE_NSMAP_DOC -3
7795#define XML_TREE_NSMAP_CUSTOM -4
7796
7797typedef struct xmlNsMapItem *xmlNsMapItemPtr;
7798struct xmlNsMapItem {
7799 xmlNsMapItemPtr next;
7800 xmlNsMapItemPtr prev;
7801 xmlNsPtr oldNs; /* old ns decl reference */
7802 xmlNsPtr newNs; /* new ns decl reference */
7803 int shadowDepth; /* Shadowed at this depth */
7804 /*
7805 * depth:
7806 * >= 0 == @node's ns-decls
7807 * -1 == @parent's ns-decls
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00007808 * -2 == the doc->oldNs XML ns-decl
7809 * -3 == the doc->oldNs storage ns-decls
7810 * -4 == ns-decls provided via custom ns-handling
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007811 */
7812 int depth;
7813};
7814
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007815typedef struct xmlNsMap *xmlNsMapPtr;
7816struct xmlNsMap {
7817 xmlNsMapItemPtr first;
7818 xmlNsMapItemPtr last;
7819 xmlNsMapItemPtr pool;
7820};
7821
7822#define XML_NSMAP_NOTEMPTY(m) (((m) != NULL) && ((m)->first != NULL))
7823#define XML_NSMAP_FOREACH(m, i) for (i = (m)->first; i != NULL; i = (i)->next)
7824#define XML_NSMAP_POP(m, i) \
7825 i = (m)->last; \
7826 (m)->last = (i)->prev; \
7827 if ((m)->last == NULL) \
7828 (m)->first = NULL; \
7829 else \
7830 (m)->last->next = NULL; \
7831 (i)->next = (m)->pool; \
7832 (m)->pool = i;
7833
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007834/*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007835* xmlDOMWrapNsMapFree:
7836* @map: the ns-map
Daniel Veillardaa6de472008-08-25 14:53:31 +00007837*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007838* Frees the ns-map
7839*/
7840static void
7841xmlDOMWrapNsMapFree(xmlNsMapPtr nsmap)
7842{
7843 xmlNsMapItemPtr cur, tmp;
7844
7845 if (nsmap == NULL)
7846 return;
7847 cur = nsmap->pool;
7848 while (cur != NULL) {
7849 tmp = cur;
7850 cur = cur->next;
7851 xmlFree(tmp);
7852 }
7853 cur = nsmap->first;
7854 while (cur != NULL) {
7855 tmp = cur;
7856 cur = cur->next;
7857 xmlFree(tmp);
7858 }
7859 xmlFree(nsmap);
7860}
7861
7862/*
7863* xmlDOMWrapNsMapAddItem:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007864* @map: the ns-map
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007865* @oldNs: the old ns-struct
7866* @newNs: the new ns-struct
7867* @depth: depth and ns-kind information
Daniel Veillardaa6de472008-08-25 14:53:31 +00007868*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007869* Adds an ns-mapping item.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007870*/
7871static xmlNsMapItemPtr
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00007872xmlDOMWrapNsMapAddItem(xmlNsMapPtr *nsmap, int position,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007873 xmlNsPtr oldNs, xmlNsPtr newNs, int depth)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007874{
7875 xmlNsMapItemPtr ret;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007876 xmlNsMapPtr map;
7877
7878 if (nsmap == NULL)
7879 return(NULL);
7880 if ((position != -1) && (position != 0))
7881 return(NULL);
7882 map = *nsmap;
7883
7884 if (map == NULL) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007885 /*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007886 * Create the ns-map.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007887 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007888 map = (xmlNsMapPtr) xmlMalloc(sizeof(struct xmlNsMap));
7889 if (map == NULL) {
7890 xmlTreeErrMemory("allocating namespace map");
7891 return (NULL);
7892 }
7893 memset(map, 0, sizeof(struct xmlNsMap));
7894 *nsmap = map;
7895 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00007896
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007897 if (map->pool != NULL) {
7898 /*
7899 * Reuse an item from the pool.
7900 */
7901 ret = map->pool;
7902 map->pool = ret->next;
7903 memset(ret, 0, sizeof(struct xmlNsMapItem));
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007904 } else {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007905 /*
7906 * Create a new item.
7907 */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007908 ret = (xmlNsMapItemPtr) xmlMalloc(sizeof(struct xmlNsMapItem));
7909 if (ret == NULL) {
7910 xmlTreeErrMemory("allocating namespace map item");
7911 return (NULL);
7912 }
7913 memset(ret, 0, sizeof(struct xmlNsMapItem));
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007914 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00007915
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007916 if (map->first == NULL) {
7917 /*
7918 * First ever.
Daniel Veillardaa6de472008-08-25 14:53:31 +00007919 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007920 map->first = ret;
7921 map->last = ret;
7922 } else if (position == -1) {
7923 /*
7924 * Append.
7925 */
7926 ret->prev = map->last;
7927 map->last->next = ret;
Daniel Veillardaa6de472008-08-25 14:53:31 +00007928 map->last = ret;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007929 } else if (position == 0) {
7930 /*
7931 * Set on first position.
7932 */
7933 map->first->prev = ret;
Daniel Veillardaa6de472008-08-25 14:53:31 +00007934 ret->next = map->first;
7935 map->first = ret;
Gaurav Gupta6d93e9e2014-10-06 20:20:00 +08007936 }
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007937
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007938 ret->oldNs = oldNs;
7939 ret->newNs = newNs;
7940 ret->shadowDepth = -1;
7941 ret->depth = depth;
7942 return (ret);
7943}
7944
7945/*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007946* xmlDOMWrapStoreNs:
7947* @doc: the doc
7948* @nsName: the namespace name
7949* @prefix: the prefix
Daniel Veillardaa6de472008-08-25 14:53:31 +00007950*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007951* Creates or reuses an xmlNs struct on doc->oldNs with
7952* the given prefix and namespace name.
Daniel Veillardaa6de472008-08-25 14:53:31 +00007953*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007954* Returns the aquired ns struct or NULL in case of an API
7955* or internal error.
7956*/
7957static xmlNsPtr
7958xmlDOMWrapStoreNs(xmlDocPtr doc,
7959 const xmlChar *nsName,
7960 const xmlChar *prefix)
7961{
7962 xmlNsPtr ns;
7963
7964 if (doc == NULL)
7965 return (NULL);
7966 ns = xmlTreeEnsureXMLDecl(doc);
7967 if (ns == NULL)
7968 return (NULL);
7969 if (ns->next != NULL) {
7970 /* Reuse. */
7971 ns = ns->next;
7972 while (ns != NULL) {
7973 if (((ns->prefix == prefix) ||
7974 xmlStrEqual(ns->prefix, prefix)) &&
7975 xmlStrEqual(ns->href, nsName)) {
7976 return (ns);
7977 }
7978 if (ns->next == NULL)
7979 break;
7980 ns = ns->next;
7981 }
7982 }
7983 /* Create. */
Daniel Veillard76d36452009-09-07 11:19:33 +02007984 if (ns != NULL) {
7985 ns->next = xmlNewNs(NULL, nsName, prefix);
7986 return (ns->next);
7987 }
7988 return(NULL);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007989}
7990
7991/*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00007992* xmlDOMWrapNewCtxt:
7993*
7994* Allocates and initializes a new DOM-wrapper context.
7995*
Jan Pokorný75801652013-12-19 15:09:14 +01007996* Returns the xmlDOMWrapCtxtPtr or NULL in case of an internal error.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00007997*/
7998xmlDOMWrapCtxtPtr
7999xmlDOMWrapNewCtxt(void)
8000{
8001 xmlDOMWrapCtxtPtr ret;
8002
8003 ret = xmlMalloc(sizeof(xmlDOMWrapCtxt));
8004 if (ret == NULL) {
8005 xmlTreeErrMemory("allocating DOM-wrapper context");
8006 return (NULL);
8007 }
8008 memset(ret, 0, sizeof(xmlDOMWrapCtxt));
8009 return (ret);
8010}
8011
8012/*
8013* xmlDOMWrapFreeCtxt:
8014* @ctxt: the DOM-wrapper context
8015*
8016* Frees the DOM-wrapper context.
8017*/
8018void
8019xmlDOMWrapFreeCtxt(xmlDOMWrapCtxtPtr ctxt)
8020{
8021 if (ctxt == NULL)
8022 return;
8023 if (ctxt->namespaceMap != NULL)
8024 xmlDOMWrapNsMapFree((xmlNsMapPtr) ctxt->namespaceMap);
8025 /*
8026 * TODO: Store the namespace map in the context.
8027 */
8028 xmlFree(ctxt);
8029}
8030
8031/*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008032* xmlTreeLookupNsListByPrefix:
8033* @nsList: a list of ns-structs
8034* @prefix: the searched prefix
Daniel Veillardaa6de472008-08-25 14:53:31 +00008035*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008036* Searches for a ns-decl with the given prefix in @nsList.
Daniel Veillardaa6de472008-08-25 14:53:31 +00008037*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008038* Returns the ns-decl if found, NULL if not found and on
8039* API errors.
8040*/
8041static xmlNsPtr
8042xmlTreeNSListLookupByPrefix(xmlNsPtr nsList, const xmlChar *prefix)
8043{
8044 if (nsList == NULL)
8045 return (NULL);
8046 {
8047 xmlNsPtr ns;
8048 ns = nsList;
8049 do {
8050 if ((prefix == ns->prefix) ||
8051 xmlStrEqual(prefix, ns->prefix)) {
8052 return (ns);
8053 }
8054 ns = ns->next;
8055 } while (ns != NULL);
8056 }
8057 return (NULL);
8058}
8059
8060/*
8061*
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008062* xmlDOMWrapNSNormGatherInScopeNs:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008063* @map: the namespace map
8064* @node: the node to start with
Daniel Veillardaa6de472008-08-25 14:53:31 +00008065*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008066* Puts in-scope namespaces into the ns-map.
Daniel Veillardaa6de472008-08-25 14:53:31 +00008067*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008068* Returns 0 on success, -1 on API or internal errors.
8069*/
8070static int
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008071xmlDOMWrapNSNormGatherInScopeNs(xmlNsMapPtr *map,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008072 xmlNodePtr node)
8073{
8074 xmlNodePtr cur;
8075 xmlNsPtr ns;
8076 xmlNsMapItemPtr mi;
8077 int shadowed;
8078
8079 if ((map == NULL) || (*map != NULL))
8080 return (-1);
Daniel Veillard3e62adb2012-08-09 14:24:02 +08008081 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
8082 return (-1);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008083 /*
8084 * Get in-scope ns-decls of @parent.
8085 */
8086 cur = node;
8087 while ((cur != NULL) && (cur != (xmlNodePtr) cur->doc)) {
8088 if (cur->type == XML_ELEMENT_NODE) {
8089 if (cur->nsDef != NULL) {
8090 ns = cur->nsDef;
8091 do {
8092 shadowed = 0;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008093 if (XML_NSMAP_NOTEMPTY(*map)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008094 /*
8095 * Skip shadowed prefixes.
8096 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008097 XML_NSMAP_FOREACH(*map, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008098 if ((ns->prefix == mi->newNs->prefix) ||
8099 xmlStrEqual(ns->prefix, mi->newNs->prefix)) {
8100 shadowed = 1;
8101 break;
8102 }
8103 }
8104 }
8105 /*
8106 * Insert mapping.
8107 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008108 mi = xmlDOMWrapNsMapAddItem(map, 0, NULL,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008109 ns, XML_TREE_NSMAP_PARENT);
8110 if (mi == NULL)
8111 return (-1);
8112 if (shadowed)
8113 mi->shadowDepth = 0;
8114 ns = ns->next;
8115 } while (ns != NULL);
8116 }
8117 }
8118 cur = cur->parent;
8119 }
8120 return (0);
8121}
8122
8123/*
8124* XML_TREE_ADOPT_STR: If we have a dest-dict, put @str in the dict;
8125* otherwise copy it, when it was in the source-dict.
8126*/
8127#define XML_TREE_ADOPT_STR(str) \
8128 if (adoptStr && (str != NULL)) { \
8129 if (destDoc->dict) { \
Daniel Veillard7e21fd12005-07-03 21:44:07 +00008130 const xmlChar *old = str; \
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008131 str = xmlDictLookup(destDoc->dict, str, -1); \
Daniel Veillard7e21fd12005-07-03 21:44:07 +00008132 if ((sourceDoc == NULL) || (sourceDoc->dict == NULL) || \
8133 (!xmlDictOwns(sourceDoc->dict, old))) \
Daniel Veillard39e5c892005-07-03 22:48:50 +00008134 xmlFree((char *)old); \
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008135 } else if ((sourceDoc) && (sourceDoc->dict) && \
8136 xmlDictOwns(sourceDoc->dict, str)) { \
8137 str = BAD_CAST xmlStrdup(str); \
8138 } \
8139 }
8140
8141/*
8142* XML_TREE_ADOPT_STR_2: If @str was in the source-dict, then
8143* put it in dest-dict or copy it.
8144*/
8145#define XML_TREE_ADOPT_STR_2(str) \
8146 if (adoptStr && (str != NULL) && (sourceDoc != NULL) && \
8147 (sourceDoc->dict != NULL) && \
8148 xmlDictOwns(sourceDoc->dict, cur->content)) { \
8149 if (destDoc->dict) \
8150 cur->content = (xmlChar *) \
8151 xmlDictLookup(destDoc->dict, cur->content, -1); \
8152 else \
8153 cur->content = xmlStrdup(BAD_CAST cur->content); \
8154 }
8155
8156/*
8157* xmlDOMWrapNSNormAddNsMapItem2:
8158*
8159* For internal use. Adds a ns-decl mapping.
8160*
Daniel Veillardaa6de472008-08-25 14:53:31 +00008161* Returns 0 on success, -1 on internal errors.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008162*/
8163static int
8164xmlDOMWrapNSNormAddNsMapItem2(xmlNsPtr **list, int *size, int *number,
8165 xmlNsPtr oldNs, xmlNsPtr newNs)
8166{
8167 if (*list == NULL) {
8168 *list = (xmlNsPtr *) xmlMalloc(6 * sizeof(xmlNsPtr));
8169 if (*list == NULL) {
8170 xmlTreeErrMemory("alloc ns map item");
8171 return(-1);
8172 }
8173 *size = 3;
8174 *number = 0;
8175 } else if ((*number) >= (*size)) {
8176 *size *= 2;
8177 *list = (xmlNsPtr *) xmlRealloc(*list,
8178 (*size) * 2 * sizeof(xmlNsPtr));
8179 if (*list == NULL) {
8180 xmlTreeErrMemory("realloc ns map item");
8181 return(-1);
8182 }
8183 }
8184 (*list)[2 * (*number)] = oldNs;
8185 (*list)[2 * (*number) +1] = newNs;
8186 (*number)++;
8187 return (0);
8188}
8189
8190/*
8191* xmlDOMWrapRemoveNode:
Daniel Veillard304e78c2005-07-03 16:19:41 +00008192* @ctxt: a DOM wrapper context
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008193* @doc: the doc
8194* @node: the node to be removed.
Daniel Veillard304e78c2005-07-03 16:19:41 +00008195* @options: set of options, unused at the moment
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008196*
8197* Unlinks the given node from its owner.
8198* This will substitute ns-references to node->nsDef for
8199* ns-references to doc->oldNs, thus ensuring the removed
8200* branch to be autark wrt ns-references.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008201*
8202* NOTE: This function was not intensively tested.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008203*
8204* Returns 0 on success, 1 if the node is not supported,
Daniel Veillardaa6de472008-08-25 14:53:31 +00008205* -1 on API and internal errors.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008206*/
8207int
8208xmlDOMWrapRemoveNode(xmlDOMWrapCtxtPtr ctxt, xmlDocPtr doc,
8209 xmlNodePtr node, int options ATTRIBUTE_UNUSED)
8210{
8211 xmlNsPtr *list = NULL;
8212 int sizeList, nbList, i, j;
8213 xmlNsPtr ns;
8214
8215 if ((node == NULL) || (doc == NULL) || (node->doc != doc))
8216 return (-1);
8217
8218 /* TODO: 0 or -1 ? */
8219 if (node->parent == NULL)
8220 return (0);
8221
Daniel Veillardaa6de472008-08-25 14:53:31 +00008222 switch (node->type) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008223 case XML_TEXT_NODE:
8224 case XML_CDATA_SECTION_NODE:
8225 case XML_ENTITY_REF_NODE:
8226 case XML_PI_NODE:
8227 case XML_COMMENT_NODE:
8228 xmlUnlinkNode(node);
8229 return (0);
Daniel Veillardaa6de472008-08-25 14:53:31 +00008230 case XML_ELEMENT_NODE:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008231 case XML_ATTRIBUTE_NODE:
8232 break;
8233 default:
8234 return (1);
8235 }
8236 xmlUnlinkNode(node);
8237 /*
8238 * Save out-of-scope ns-references in doc->oldNs.
8239 */
8240 do {
8241 switch (node->type) {
8242 case XML_ELEMENT_NODE:
8243 if ((ctxt == NULL) && (node->nsDef != NULL)) {
8244 ns = node->nsDef;
8245 do {
8246 if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
8247 &nbList, ns, ns) == -1)
8248 goto internal_error;
8249 ns = ns->next;
8250 } while (ns != NULL);
8251 }
8252 /* No break on purpose. */
8253 case XML_ATTRIBUTE_NODE:
Daniel Veillardaa6de472008-08-25 14:53:31 +00008254 if (node->ns != NULL) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008255 /*
8256 * Find a mapping.
8257 */
8258 if (list != NULL) {
8259 for (i = 0, j = 0; i < nbList; i++, j += 2) {
8260 if (node->ns == list[j]) {
8261 node->ns = list[++j];
8262 goto next_node;
8263 }
8264 }
8265 }
8266 ns = NULL;
8267 if (ctxt != NULL) {
8268 /*
8269 * User defined.
8270 */
8271 } else {
8272 /*
8273 * Add to doc's oldNs.
8274 */
8275 ns = xmlDOMWrapStoreNs(doc, node->ns->href,
8276 node->ns->prefix);
8277 if (ns == NULL)
8278 goto internal_error;
8279 }
8280 if (ns != NULL) {
8281 /*
8282 * Add mapping.
8283 */
8284 if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
8285 &nbList, node->ns, ns) == -1)
8286 goto internal_error;
8287 }
8288 node->ns = ns;
8289 }
8290 if ((node->type == XML_ELEMENT_NODE) &&
8291 (node->properties != NULL)) {
8292 node = (xmlNodePtr) node->properties;
8293 continue;
8294 }
8295 break;
8296 default:
8297 goto next_sibling;
Daniel Veillardaa6de472008-08-25 14:53:31 +00008298 }
8299next_node:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008300 if ((node->type == XML_ELEMENT_NODE) &&
8301 (node->children != NULL)) {
8302 node = node->children;
8303 continue;
8304 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00008305next_sibling:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008306 if (node == NULL)
8307 break;
8308 if (node->next != NULL)
8309 node = node->next;
8310 else {
8311 node = node->parent;
8312 goto next_sibling;
8313 }
8314 } while (node != NULL);
8315
8316 if (list != NULL)
8317 xmlFree(list);
8318 return (0);
8319
8320internal_error:
8321 if (list != NULL)
8322 xmlFree(list);
8323 return (-1);
8324}
8325
8326/*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008327* xmlSearchNsByNamespaceStrict:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008328* @doc: the document
8329* @node: the start node
8330* @nsName: the searched namespace name
8331* @retNs: the resulting ns-decl
8332* @prefixed: if the found ns-decl must have a prefix (for attributes)
8333*
8334* Dynamically searches for a ns-declaration which matches
8335* the given @nsName in the ancestor-or-self axis of @node.
8336*
8337* Returns 1 if a ns-decl was found, 0 if not and -1 on API
8338* and internal errors.
8339*/
8340static int
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008341xmlSearchNsByNamespaceStrict(xmlDocPtr doc, xmlNodePtr node,
8342 const xmlChar* nsName,
8343 xmlNsPtr *retNs, int prefixed)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008344{
8345 xmlNodePtr cur, prev = NULL, out = NULL;
8346 xmlNsPtr ns, prevns;
8347
8348 if ((doc == NULL) || (nsName == NULL) || (retNs == NULL))
8349 return (-1);
Daniel Veillard3e62adb2012-08-09 14:24:02 +08008350 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
8351 return(-1);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008352
8353 *retNs = NULL;
8354 if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) {
8355 *retNs = xmlTreeEnsureXMLDecl(doc);
8356 if (*retNs == NULL)
8357 return (-1);
8358 return (1);
8359 }
8360 cur = node;
8361 do {
8362 if (cur->type == XML_ELEMENT_NODE) {
8363 if (cur->nsDef != NULL) {
8364 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
8365 if (prefixed && (ns->prefix == NULL))
8366 continue;
8367 if (prev != NULL) {
8368 /*
8369 * Check the last level of ns-decls for a
8370 * shadowing prefix.
8371 */
8372 prevns = prev->nsDef;
8373 do {
8374 if ((prevns->prefix == ns->prefix) ||
8375 ((prevns->prefix != NULL) &&
8376 (ns->prefix != NULL) &&
8377 xmlStrEqual(prevns->prefix, ns->prefix))) {
8378 /*
8379 * Shadowed.
8380 */
8381 break;
8382 }
8383 prevns = prevns->next;
8384 } while (prevns != NULL);
8385 if (prevns != NULL)
8386 continue;
8387 }
8388 /*
8389 * Ns-name comparison.
8390 */
8391 if ((nsName == ns->href) ||
8392 xmlStrEqual(nsName, ns->href)) {
8393 /*
8394 * At this point the prefix can only be shadowed,
8395 * if we are the the (at least) 3rd level of
8396 * ns-decls.
8397 */
8398 if (out) {
8399 int ret;
Daniel Veillardaa6de472008-08-25 14:53:31 +00008400
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008401 ret = xmlNsInScope(doc, node, prev, ns->prefix);
8402 if (ret < 0)
8403 return (-1);
8404 /*
8405 * TODO: Should we try to find a matching ns-name
8406 * only once? This here keeps on searching.
8407 * I think we should try further since, there might
8408 * be an other matching ns-decl with an unshadowed
8409 * prefix.
8410 */
8411 if (! ret)
8412 continue;
8413 }
8414 *retNs = ns;
8415 return (1);
8416 }
8417 }
8418 out = prev;
8419 prev = cur;
8420 }
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008421 } else if ((cur->type == XML_ENTITY_NODE) ||
8422 (cur->type == XML_ENTITY_DECL))
8423 return (0);
8424 cur = cur->parent;
8425 } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
8426 return (0);
8427}
8428
8429/*
8430* xmlSearchNsByPrefixStrict:
8431* @doc: the document
8432* @node: the start node
8433* @prefix: the searched namespace prefix
8434* @retNs: the resulting ns-decl
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008435*
8436* Dynamically searches for a ns-declaration which matches
8437* the given @nsName in the ancestor-or-self axis of @node.
8438*
8439* Returns 1 if a ns-decl was found, 0 if not and -1 on API
8440* and internal errors.
8441*/
8442static int
8443xmlSearchNsByPrefixStrict(xmlDocPtr doc, xmlNodePtr node,
8444 const xmlChar* prefix,
8445 xmlNsPtr *retNs)
8446{
8447 xmlNodePtr cur;
8448 xmlNsPtr ns;
8449
Daniel Veillard3e62adb2012-08-09 14:24:02 +08008450 if ((doc == NULL) || (node == NULL) || (node->type == XML_NAMESPACE_DECL))
8451 return(-1);
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008452
8453 if (retNs)
8454 *retNs = NULL;
8455 if (IS_STR_XML(prefix)) {
8456 if (retNs) {
8457 *retNs = xmlTreeEnsureXMLDecl(doc);
8458 if (*retNs == NULL)
8459 return (-1);
8460 }
8461 return (1);
8462 }
8463 cur = node;
8464 do {
8465 if (cur->type == XML_ELEMENT_NODE) {
8466 if (cur->nsDef != NULL) {
8467 ns = cur->nsDef;
Daniel Veillardaa6de472008-08-25 14:53:31 +00008468 do {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008469 if ((prefix == ns->prefix) ||
8470 xmlStrEqual(prefix, ns->prefix))
8471 {
8472 /*
8473 * Disabled namespaces, e.g. xmlns:abc="".
8474 */
8475 if (ns->href == NULL)
8476 return(0);
8477 if (retNs)
8478 *retNs = ns;
8479 return (1);
8480 }
8481 ns = ns->next;
Daniel Veillardaa6de472008-08-25 14:53:31 +00008482 } while (ns != NULL);
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008483 }
8484 } else if ((cur->type == XML_ENTITY_NODE) ||
8485 (cur->type == XML_ENTITY_DECL))
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008486 return (0);
8487 cur = cur->parent;
8488 } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
8489 return (0);
8490}
8491
8492/*
8493* xmlDOMWrapNSNormDeclareNsForced:
8494* @doc: the doc
8495* @elem: the element-node to declare on
8496* @nsName: the namespace-name of the ns-decl
8497* @prefix: the preferred prefix of the ns-decl
8498* @checkShadow: ensure that the new ns-decl doesn't shadow ancestor ns-decls
8499*
8500* Declares a new namespace on @elem. It tries to use the
8501* given @prefix; if a ns-decl with the given prefix is already existent
8502* on @elem, it will generate an other prefix.
8503*
8504* Returns 1 if a ns-decl was found, 0 if not and -1 on API
8505* and internal errors.
8506*/
8507static xmlNsPtr
8508xmlDOMWrapNSNormDeclareNsForced(xmlDocPtr doc,
8509 xmlNodePtr elem,
8510 const xmlChar *nsName,
8511 const xmlChar *prefix,
8512 int checkShadow)
8513{
8514
8515 xmlNsPtr ret;
8516 char buf[50];
8517 const xmlChar *pref;
8518 int counter = 0;
Daniel Veillard3e62adb2012-08-09 14:24:02 +08008519
8520 if ((doc == NULL) || (elem == NULL) || (elem->type != XML_ELEMENT_NODE))
8521 return(NULL);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008522 /*
8523 * Create a ns-decl on @anchor.
8524 */
8525 pref = prefix;
8526 while (1) {
8527 /*
8528 * Lookup whether the prefix is unused in elem's ns-decls.
8529 */
8530 if ((elem->nsDef != NULL) &&
8531 (xmlTreeNSListLookupByPrefix(elem->nsDef, pref) != NULL))
8532 goto ns_next_prefix;
8533 if (checkShadow && elem->parent &&
8534 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8535 /*
8536 * Does it shadow ancestor ns-decls?
8537 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008538 if (xmlSearchNsByPrefixStrict(doc, elem->parent, pref, NULL) == 1)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008539 goto ns_next_prefix;
8540 }
8541 ret = xmlNewNs(NULL, nsName, pref);
8542 if (ret == NULL)
8543 return (NULL);
8544 if (elem->nsDef == NULL)
8545 elem->nsDef = ret;
8546 else {
8547 xmlNsPtr ns2 = elem->nsDef;
8548 while (ns2->next != NULL)
8549 ns2 = ns2->next;
8550 ns2->next = ret;
8551 }
8552 return (ret);
8553ns_next_prefix:
8554 counter++;
8555 if (counter > 1000)
8556 return (NULL);
8557 if (prefix == NULL) {
8558 snprintf((char *) buf, sizeof(buf),
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008559 "ns_%d", counter);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008560 } else
8561 snprintf((char *) buf, sizeof(buf),
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008562 "%.30s_%d", (char *)prefix, counter);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008563 pref = BAD_CAST buf;
8564 }
8565}
8566
8567/*
8568* xmlDOMWrapNSNormAquireNormalizedNs:
8569* @doc: the doc
8570* @elem: the element-node to declare namespaces on
8571* @ns: the ns-struct to use for the search
8572* @retNs: the found/created ns-struct
8573* @nsMap: the ns-map
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008574* @depth: the current tree depth
8575* @ancestorsOnly: search in ancestor ns-decls only
8576* @prefixed: if the searched ns-decl must have a prefix (for attributes)
8577*
8578* Searches for a matching ns-name in the ns-decls of @nsMap, if not
8579* found it will either declare it on @elem, or store it in doc->oldNs.
8580* If a new ns-decl needs to be declared on @elem, it tries to use the
8581* @ns->prefix for it, if this prefix is already in use on @elem, it will
8582* change the prefix or the new ns-decl.
8583*
8584* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8585*/
8586static int
8587xmlDOMWrapNSNormAquireNormalizedNs(xmlDocPtr doc,
8588 xmlNodePtr elem,
8589 xmlNsPtr ns,
8590 xmlNsPtr *retNs,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008591 xmlNsMapPtr *nsMap,
Daniel Veillardaa6de472008-08-25 14:53:31 +00008592
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008593 int depth,
8594 int ancestorsOnly,
8595 int prefixed)
8596{
Daniel Veillardaa6de472008-08-25 14:53:31 +00008597 xmlNsMapItemPtr mi;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008598
8599 if ((doc == NULL) || (ns == NULL) || (retNs == NULL) ||
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008600 (nsMap == NULL))
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008601 return (-1);
8602
8603 *retNs = NULL;
8604 /*
8605 * Handle XML namespace.
8606 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008607 if (IS_STR_XML(ns->prefix)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008608 /*
8609 * Insert XML namespace mapping.
8610 */
8611 *retNs = xmlTreeEnsureXMLDecl(doc);
8612 if (*retNs == NULL)
8613 return (-1);
8614 return (0);
8615 }
8616 /*
8617 * If the search should be done in ancestors only and no
8618 * @elem (the first ancestor) was specified, then skip the search.
8619 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008620 if ((XML_NSMAP_NOTEMPTY(*nsMap)) &&
8621 (! (ancestorsOnly && (elem == NULL))))
Daniel Veillardaa6de472008-08-25 14:53:31 +00008622 {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008623 /*
8624 * Try to find an equal ns-name in in-scope ns-decls.
8625 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008626 XML_NSMAP_FOREACH(*nsMap, mi) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00008627 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8628 /*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008629 * ancestorsOnly: This should be turned on to gain speed,
8630 * if one knows that the branch itself was already
8631 * ns-wellformed and no stale references existed.
8632 * I.e. it searches in the ancestor axis only.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008633 */
8634 ((! ancestorsOnly) || (mi->depth == XML_TREE_NSMAP_PARENT)) &&
8635 /* Skip shadowed prefixes. */
Daniel Veillardaa6de472008-08-25 14:53:31 +00008636 (mi->shadowDepth == -1) &&
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008637 /* Skip xmlns="" or xmlns:foo="". */
8638 ((mi->newNs->href != NULL) &&
Daniel Veillardaa6de472008-08-25 14:53:31 +00008639 (mi->newNs->href[0] != 0)) &&
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008640 /* Ensure a prefix if wanted. */
8641 ((! prefixed) || (mi->newNs->prefix != NULL)) &&
8642 /* Equal ns name */
8643 ((mi->newNs->href == ns->href) ||
8644 xmlStrEqual(mi->newNs->href, ns->href))) {
8645 /* Set the mapping. */
8646 mi->oldNs = ns;
8647 *retNs = mi->newNs;
8648 return (0);
8649 }
8650 }
8651 }
8652 /*
8653 * No luck, the namespace is out of scope or shadowed.
8654 */
8655 if (elem == NULL) {
8656 xmlNsPtr tmpns;
8657
8658 /*
8659 * Store ns-decls in "oldNs" of the document-node.
8660 */
8661 tmpns = xmlDOMWrapStoreNs(doc, ns->href, ns->prefix);
8662 if (tmpns == NULL)
8663 return (-1);
8664 /*
8665 * Insert mapping.
Daniel Veillardaa6de472008-08-25 14:53:31 +00008666 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008667 if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008668 tmpns, XML_TREE_NSMAP_DOC) == NULL) {
8669 xmlFreeNs(tmpns);
8670 return (-1);
8671 }
8672 *retNs = tmpns;
8673 } else {
8674 xmlNsPtr tmpns;
8675
8676 tmpns = xmlDOMWrapNSNormDeclareNsForced(doc, elem, ns->href,
8677 ns->prefix, 0);
8678 if (tmpns == NULL)
8679 return (-1);
8680
8681 if (*nsMap != NULL) {
8682 /*
8683 * Does it shadow ancestor ns-decls?
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008684 */
8685 XML_NSMAP_FOREACH(*nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008686 if ((mi->depth < depth) &&
8687 (mi->shadowDepth == -1) &&
8688 ((ns->prefix == mi->newNs->prefix) ||
8689 xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
8690 /*
8691 * Shadows.
8692 */
8693 mi->shadowDepth = depth;
8694 break;
8695 }
8696 }
8697 }
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008698 if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns, tmpns, depth) == NULL) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008699 xmlFreeNs(tmpns);
8700 return (-1);
8701 }
8702 *retNs = tmpns;
8703 }
8704 return (0);
8705}
8706
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008707typedef enum {
Daniel Veillardaa6de472008-08-25 14:53:31 +00008708 XML_DOM_RECONNS_REMOVEREDUND = 1<<0
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008709} xmlDOMReconcileNSOptions;
8710
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008711/*
8712* xmlDOMWrapReconcileNamespaces:
Daniel Veillard304e78c2005-07-03 16:19:41 +00008713* @ctxt: DOM wrapper context, unused at the moment
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008714* @elem: the element-node
8715* @options: option flags
8716*
8717* Ensures that ns-references point to ns-decls hold on element-nodes.
8718* Ensures that the tree is namespace wellformed by creating additional
8719* ns-decls where needed. Note that, since prefixes of already existent
8720* ns-decls can be shadowed by this process, it could break QNames in
8721* attribute values or element content.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008722*
8723* NOTE: This function was not intensively tested.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008724*
8725* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
Daniel Veillardaa6de472008-08-25 14:53:31 +00008726*/
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008727
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008728int
8729xmlDOMWrapReconcileNamespaces(xmlDOMWrapCtxtPtr ctxt ATTRIBUTE_UNUSED,
8730 xmlNodePtr elem,
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008731 int options)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008732{
8733 int depth = -1, adoptns = 0, parnsdone = 0;
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008734 xmlNsPtr ns, prevns;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008735 xmlDocPtr doc;
8736 xmlNodePtr cur, curElem = NULL;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008737 xmlNsMapPtr nsMap = NULL;
8738 xmlNsMapItemPtr /* topmi = NULL, */ mi;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008739 /* @ancestorsOnly should be set by an option flag. */
8740 int ancestorsOnly = 0;
Daniel Veillardaa6de472008-08-25 14:53:31 +00008741 int optRemoveRedundantNS =
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008742 ((xmlDOMReconcileNSOptions) options & XML_DOM_RECONNS_REMOVEREDUND) ? 1 : 0;
8743 xmlNsPtr *listRedund = NULL;
8744 int sizeRedund = 0, nbRedund = 0, ret, i, j;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008745
8746 if ((elem == NULL) || (elem->doc == NULL) ||
8747 (elem->type != XML_ELEMENT_NODE))
8748 return (-1);
8749
8750 doc = elem->doc;
8751 cur = elem;
8752 do {
8753 switch (cur->type) {
8754 case XML_ELEMENT_NODE:
8755 adoptns = 1;
8756 curElem = cur;
8757 depth++;
8758 /*
8759 * Namespace declarations.
8760 */
8761 if (cur->nsDef != NULL) {
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008762 prevns = NULL;
Kasimier T. Buchcike8f8d752006-02-02 12:13:07 +00008763 ns = cur->nsDef;
8764 while (ns != NULL) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008765 if (! parnsdone) {
8766 if ((elem->parent) &&
8767 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8768 /*
8769 * Gather ancestor in-scope ns-decls.
8770 */
8771 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8772 elem->parent) == -1)
8773 goto internal_error;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008774 }
8775 parnsdone = 1;
8776 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00008777
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008778 /*
8779 * Lookup the ns ancestor-axis for equal ns-decls in scope.
8780 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008781 if (optRemoveRedundantNS && XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008782 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008783 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8784 (mi->shadowDepth == -1) &&
8785 ((ns->prefix == mi->newNs->prefix) ||
8786 xmlStrEqual(ns->prefix, mi->newNs->prefix)) &&
8787 ((ns->href == mi->newNs->href) ||
8788 xmlStrEqual(ns->href, mi->newNs->href)))
Daniel Veillardaa6de472008-08-25 14:53:31 +00008789 {
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008790 /*
8791 * A redundant ns-decl was found.
8792 * Add it to the list of redundant ns-decls.
8793 */
8794 if (xmlDOMWrapNSNormAddNsMapItem2(&listRedund,
8795 &sizeRedund, &nbRedund, ns, mi->newNs) == -1)
8796 goto internal_error;
8797 /*
8798 * Remove the ns-decl from the element-node.
Daniel Veillardaa6de472008-08-25 14:53:31 +00008799 */
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008800 if (prevns)
8801 prevns->next = ns->next;
8802 else
Daniel Veillardaa6de472008-08-25 14:53:31 +00008803 cur->nsDef = ns->next;
Kasimier T. Buchcike8f8d752006-02-02 12:13:07 +00008804 goto next_ns_decl;
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008805 }
8806 }
8807 }
8808
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008809 /*
8810 * Skip ns-references handling if the referenced
8811 * ns-decl is declared on the same element.
8812 */
8813 if ((cur->ns != NULL) && adoptns && (cur->ns == ns))
Daniel Veillardaa6de472008-08-25 14:53:31 +00008814 adoptns = 0;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008815 /*
8816 * Does it shadow any ns-decl?
8817 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008818 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8819 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008820 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8821 (mi->shadowDepth == -1) &&
8822 ((ns->prefix == mi->newNs->prefix) ||
8823 xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00008824
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008825 mi->shadowDepth = depth;
8826 }
8827 }
8828 }
8829 /*
8830 * Push mapping.
8831 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008832 if (xmlDOMWrapNsMapAddItem(&nsMap, -1, ns, ns,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008833 depth) == NULL)
Daniel Veillardaa6de472008-08-25 14:53:31 +00008834 goto internal_error;
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008835
8836 prevns = ns;
Kasimier T. Buchcike8f8d752006-02-02 12:13:07 +00008837next_ns_decl:
Daniel Veillardaa6de472008-08-25 14:53:31 +00008838 ns = ns->next;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008839 }
8840 }
8841 if (! adoptns)
8842 goto ns_end;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008843 /* No break on purpose. */
8844 case XML_ATTRIBUTE_NODE:
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008845 /* No ns, no fun. */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008846 if (cur->ns == NULL)
8847 goto ns_end;
Daniel Veillardaa6de472008-08-25 14:53:31 +00008848
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008849 if (! parnsdone) {
8850 if ((elem->parent) &&
8851 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8852 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8853 elem->parent) == -1)
8854 goto internal_error;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008855 }
8856 parnsdone = 1;
8857 }
8858 /*
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008859 * Adjust the reference if this was a redundant ns-decl.
8860 */
8861 if (listRedund) {
8862 for (i = 0, j = 0; i < nbRedund; i++, j += 2) {
8863 if (cur->ns == listRedund[j]) {
8864 cur->ns = listRedund[++j];
8865 break;
8866 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00008867 }
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008868 }
8869 /*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008870 * Adopt ns-references.
8871 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008872 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008873 /*
8874 * Search for a mapping.
8875 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008876 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008877 if ((mi->shadowDepth == -1) &&
8878 (cur->ns == mi->oldNs)) {
8879
8880 cur->ns = mi->newNs;
8881 goto ns_end;
8882 }
8883 }
8884 }
8885 /*
8886 * Aquire a normalized ns-decl and add it to the map.
8887 */
8888 if (xmlDOMWrapNSNormAquireNormalizedNs(doc, curElem,
8889 cur->ns, &ns,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008890 &nsMap, depth,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008891 ancestorsOnly,
8892 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
8893 goto internal_error;
8894 cur->ns = ns;
8895
8896ns_end:
8897 if ((cur->type == XML_ELEMENT_NODE) &&
8898 (cur->properties != NULL)) {
8899 /*
8900 * Process attributes.
8901 */
8902 cur = (xmlNodePtr) cur->properties;
8903 continue;
8904 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00008905 break;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008906 default:
8907 goto next_sibling;
8908 }
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008909into_content:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008910 if ((cur->type == XML_ELEMENT_NODE) &&
8911 (cur->children != NULL)) {
8912 /*
8913 * Process content of element-nodes only.
8914 */
8915 cur = cur->children;
8916 continue;
8917 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00008918next_sibling:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008919 if (cur == elem)
8920 break;
8921 if (cur->type == XML_ELEMENT_NODE) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00008922 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008923 /*
8924 * Pop mappings.
8925 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008926 while ((nsMap->last != NULL) &&
8927 (nsMap->last->depth >= depth))
8928 {
Daniel Veillardaa6de472008-08-25 14:53:31 +00008929 XML_NSMAP_POP(nsMap, mi)
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008930 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008931 /*
8932 * Unshadow.
8933 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008934 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008935 if (mi->shadowDepth >= depth)
8936 mi->shadowDepth = -1;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008937 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00008938 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008939 depth--;
8940 }
8941 if (cur->next != NULL)
8942 cur = cur->next;
8943 else {
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008944 if (cur->type == XML_ATTRIBUTE_NODE) {
8945 cur = cur->parent;
8946 goto into_content;
8947 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008948 cur = cur->parent;
8949 goto next_sibling;
8950 }
8951 } while (cur != NULL);
Daniel Veillardaa6de472008-08-25 14:53:31 +00008952
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008953 ret = 0;
8954 goto exit;
8955internal_error:
8956 ret = -1;
8957exit:
Daniel Veillardaa6de472008-08-25 14:53:31 +00008958 if (listRedund) {
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008959 for (i = 0, j = 0; i < nbRedund; i++, j += 2) {
8960 xmlFreeNs(listRedund[j]);
8961 }
8962 xmlFree(listRedund);
8963 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008964 if (nsMap != NULL)
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008965 xmlDOMWrapNsMapFree(nsMap);
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008966 return (ret);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008967}
8968
8969/*
8970* xmlDOMWrapAdoptBranch:
8971* @ctxt: the optional context for custom processing
8972* @sourceDoc: the optional sourceDoc
8973* @node: the element-node to start with
8974* @destDoc: the destination doc for adoption
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008975* @destParent: the optional new parent of @node in @destDoc
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008976* @options: option flags
8977*
8978* Ensures that ns-references point to @destDoc: either to
8979* elements->nsDef entries if @destParent is given, or to
8980* @destDoc->oldNs otherwise.
8981* If @destParent is given, it ensures that the tree is namespace
8982* wellformed by creating additional ns-decls where needed.
8983* Note that, since prefixes of already existent ns-decls can be
8984* shadowed by this process, it could break QNames in attribute
8985* values or element content.
8986*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008987* NOTE: This function was not intensively tested.
8988*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008989* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8990*/
8991static int
8992xmlDOMWrapAdoptBranch(xmlDOMWrapCtxtPtr ctxt,
8993 xmlDocPtr sourceDoc,
8994 xmlNodePtr node,
8995 xmlDocPtr destDoc,
8996 xmlNodePtr destParent,
8997 int options ATTRIBUTE_UNUSED)
8998{
8999 int ret = 0;
9000 xmlNodePtr cur, curElem = NULL;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009001 xmlNsMapPtr nsMap = NULL;
9002 xmlNsMapItemPtr mi;
Daniel Veillard11ce4002006-03-10 00:36:23 +00009003 xmlNsPtr ns = NULL;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009004 int depth = -1, adoptStr = 1;
9005 /* gather @parent's ns-decls. */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009006 int parnsdone;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009007 /* @ancestorsOnly should be set per option. */
9008 int ancestorsOnly = 0;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009009
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009010 /*
9011 * Optimize string adoption for equal or none dicts.
9012 */
9013 if ((sourceDoc != NULL) &&
9014 (sourceDoc->dict == destDoc->dict))
9015 adoptStr = 0;
9016 else
9017 adoptStr = 1;
9018
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009019 /*
9020 * Get the ns-map from the context if available.
9021 */
9022 if (ctxt)
9023 nsMap = (xmlNsMapPtr) ctxt->namespaceMap;
9024 /*
9025 * Disable search for ns-decls in the parent-axis of the
9026 * desination element, if:
9027 * 1) there's no destination parent
9028 * 2) custom ns-reference handling is used
9029 */
9030 if ((destParent == NULL) ||
9031 (ctxt && ctxt->getNsForNodeFunc))
9032 {
9033 parnsdone = 1;
9034 } else
9035 parnsdone = 0;
9036
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009037 cur = node;
Daniel Veillard3e62adb2012-08-09 14:24:02 +08009038 if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
9039 goto internal_error;
9040
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009041 while (cur != NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009042 /*
9043 * Paranoid source-doc sanity check.
9044 */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009045 if (cur->doc != sourceDoc) {
9046 /*
9047 * We'll assume XIncluded nodes if the doc differs.
9048 * TODO: Do we need to reconciliate XIncluded nodes?
9049 * This here skips XIncluded nodes and tries to handle
9050 * broken sequences.
9051 */
9052 if (cur->next == NULL)
9053 goto leave_node;
9054 do {
9055 cur = cur->next;
9056 if ((cur->type == XML_XINCLUDE_END) ||
9057 (cur->doc == node->doc))
9058 break;
9059 } while (cur->next != NULL);
9060
9061 if (cur->doc != node->doc)
9062 goto leave_node;
9063 }
9064 cur->doc = destDoc;
9065 switch (cur->type) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00009066 case XML_XINCLUDE_START:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009067 case XML_XINCLUDE_END:
9068 /*
9069 * TODO
9070 */
9071 return (-1);
Daniel Veillardaa6de472008-08-25 14:53:31 +00009072 case XML_ELEMENT_NODE:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009073 curElem = cur;
9074 depth++;
9075 /*
Daniel Veillardaa6de472008-08-25 14:53:31 +00009076 * Namespace declarations.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009077 * - ns->href and ns->prefix are never in the dict, so
9078 * we need not move the values over to the destination dict.
9079 * - Note that for custom handling of ns-references,
9080 * the ns-decls need not be stored in the ns-map,
9081 * since they won't be referenced by node->ns.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009082 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009083 if ((cur->nsDef) &&
9084 ((ctxt == NULL) || (ctxt->getNsForNodeFunc == NULL)))
9085 {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009086 if (! parnsdone) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009087 /*
9088 * Gather @parent's in-scope ns-decls.
9089 */
9090 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
9091 destParent) == -1)
9092 goto internal_error;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009093 parnsdone = 1;
9094 }
9095 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
9096 /*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009097 * NOTE: ns->prefix and ns->href are never in the dict.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009098 * XML_TREE_ADOPT_STR(ns->prefix)
9099 * XML_TREE_ADOPT_STR(ns->href)
Daniel Veillardaa6de472008-08-25 14:53:31 +00009100 */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009101 /*
9102 * Does it shadow any ns-decl?
Daniel Veillardaa6de472008-08-25 14:53:31 +00009103 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009104 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009105 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009106 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
9107 (mi->shadowDepth == -1) &&
9108 ((ns->prefix == mi->newNs->prefix) ||
9109 xmlStrEqual(ns->prefix,
9110 mi->newNs->prefix))) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00009111
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009112 mi->shadowDepth = depth;
9113 }
9114 }
9115 }
9116 /*
9117 * Push mapping.
9118 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009119 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009120 ns, ns, depth) == NULL)
9121 goto internal_error;
9122 }
9123 }
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009124 /* No break on purpose. */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009125 case XML_ATTRIBUTE_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009126 /* No namespace, no fun. */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009127 if (cur->ns == NULL)
9128 goto ns_end;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009129
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009130 if (! parnsdone) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009131 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
9132 destParent) == -1)
9133 goto internal_error;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009134 parnsdone = 1;
9135 }
9136 /*
9137 * Adopt ns-references.
9138 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009139 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009140 /*
9141 * Search for a mapping.
9142 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009143 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009144 if ((mi->shadowDepth == -1) &&
9145 (cur->ns == mi->oldNs)) {
9146
9147 cur->ns = mi->newNs;
9148 goto ns_end;
9149 }
9150 }
9151 }
9152 /*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009153 * No matching namespace in scope. We need a new one.
9154 */
9155 if ((ctxt) && (ctxt->getNsForNodeFunc)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009156 /*
9157 * User-defined behaviour.
9158 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009159 ns = ctxt->getNsForNodeFunc(ctxt, cur,
9160 cur->ns->href, cur->ns->prefix);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009161 /*
9162 * Insert mapping if ns is available; it's the users fault
9163 * if not.
9164 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009165 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009166 cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009167 goto internal_error;
9168 cur->ns = ns;
9169 } else {
9170 /*
9171 * Aquire a normalized ns-decl and add it to the map.
9172 */
9173 if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc,
Daniel Veillardaa6de472008-08-25 14:53:31 +00009174 /* ns-decls on curElem or on destDoc->oldNs */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009175 destParent ? curElem : NULL,
9176 cur->ns, &ns,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009177 &nsMap, depth,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009178 ancestorsOnly,
9179 /* ns-decls must be prefixed for attributes. */
9180 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
9181 goto internal_error;
9182 cur->ns = ns;
9183 }
9184ns_end:
9185 /*
9186 * Further node properties.
9187 * TODO: Is this all?
9188 */
9189 XML_TREE_ADOPT_STR(cur->name)
9190 if (cur->type == XML_ELEMENT_NODE) {
9191 cur->psvi = NULL;
9192 cur->line = 0;
9193 cur->extra = 0;
9194 /*
9195 * Walk attributes.
9196 */
9197 if (cur->properties != NULL) {
9198 /*
9199 * Process first attribute node.
9200 */
9201 cur = (xmlNodePtr) cur->properties;
9202 continue;
9203 }
9204 } else {
9205 /*
9206 * Attributes.
9207 */
9208 if ((sourceDoc != NULL) &&
9209 (((xmlAttrPtr) cur)->atype == XML_ATTRIBUTE_ID))
Daniel Veillardaa6de472008-08-25 14:53:31 +00009210 {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009211 xmlRemoveID(sourceDoc, (xmlAttrPtr) cur);
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009212 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009213 ((xmlAttrPtr) cur)->atype = 0;
9214 ((xmlAttrPtr) cur)->psvi = NULL;
9215 }
9216 break;
9217 case XML_TEXT_NODE:
Daniel Veillardaa6de472008-08-25 14:53:31 +00009218 case XML_CDATA_SECTION_NODE:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009219 /*
9220 * This puts the content in the dest dict, only if
9221 * it was previously in the source dict.
9222 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009223 XML_TREE_ADOPT_STR_2(cur->content)
9224 goto leave_node;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009225 case XML_ENTITY_REF_NODE:
9226 /*
9227 * Remove reference to the entitity-node.
9228 */
9229 cur->content = NULL;
9230 cur->children = NULL;
9231 cur->last = NULL;
9232 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9233 xmlEntityPtr ent;
9234 /*
9235 * Assign new entity-node if available.
9236 */
9237 ent = xmlGetDocEntity(destDoc, cur->name);
9238 if (ent != NULL) {
9239 cur->content = ent->content;
9240 cur->children = (xmlNodePtr) ent;
9241 cur->last = (xmlNodePtr) ent;
9242 }
9243 }
9244 goto leave_node;
9245 case XML_PI_NODE:
9246 XML_TREE_ADOPT_STR(cur->name)
9247 XML_TREE_ADOPT_STR_2(cur->content)
9248 break;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009249 case XML_COMMENT_NODE:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009250 break;
9251 default:
9252 goto internal_error;
9253 }
9254 /*
9255 * Walk the tree.
9256 */
9257 if (cur->children != NULL) {
9258 cur = cur->children;
9259 continue;
9260 }
9261
9262leave_node:
9263 if (cur == node)
9264 break;
9265 if ((cur->type == XML_ELEMENT_NODE) ||
9266 (cur->type == XML_XINCLUDE_START) ||
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009267 (cur->type == XML_XINCLUDE_END))
9268 {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009269 /*
9270 * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
9271 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009272 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009273 /*
9274 * Pop mappings.
9275 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009276 while ((nsMap->last != NULL) &&
9277 (nsMap->last->depth >= depth))
9278 {
Daniel Veillardaa6de472008-08-25 14:53:31 +00009279 XML_NSMAP_POP(nsMap, mi)
9280 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009281 /*
9282 * Unshadow.
9283 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009284 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009285 if (mi->shadowDepth >= depth)
9286 mi->shadowDepth = -1;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009287 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009288 }
9289 depth--;
9290 }
9291 if (cur->next != NULL)
9292 cur = cur->next;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009293 else if ((cur->type == XML_ATTRIBUTE_NODE) &&
9294 (cur->parent->children != NULL))
9295 {
9296 cur = cur->parent->children;
9297 } else {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009298 cur = cur->parent;
9299 goto leave_node;
9300 }
9301 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009302
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009303 goto exit;
9304
Daniel Veillardaa6de472008-08-25 14:53:31 +00009305internal_error:
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009306 ret = -1;
9307
9308exit:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009309 /*
9310 * Cleanup.
9311 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009312 if (nsMap != NULL) {
9313 if ((ctxt) && (ctxt->namespaceMap == nsMap)) {
9314 /*
9315 * Just cleanup the map but don't free.
9316 */
9317 if (nsMap->first) {
9318 if (nsMap->pool)
9319 nsMap->last->next = nsMap->pool;
9320 nsMap->pool = nsMap->first;
9321 nsMap->first = NULL;
9322 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009323 } else
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009324 xmlDOMWrapNsMapFree(nsMap);
9325 }
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009326 return(ret);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009327}
9328
9329/*
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009330* xmlDOMWrapCloneNode:
9331* @ctxt: the optional context for custom processing
9332* @sourceDoc: the optional sourceDoc
9333* @node: the node to start with
9334* @resNode: the clone of the given @node
9335* @destDoc: the destination doc
9336* @destParent: the optional new parent of @node in @destDoc
Daniel Veillardb2f8f1d2006-04-28 16:30:48 +00009337* @deep: descend into child if set
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009338* @options: option flags
9339*
9340* References of out-of scope ns-decls are remapped to point to @destDoc:
9341* 1) If @destParent is given, then nsDef entries on element-nodes are used
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009342* 2) If *no* @destParent is given, then @destDoc->oldNs entries are used.
9343* This is the case when you don't know already where the cloned branch
9344* will be added to.
Daniel Veillardaa6de472008-08-25 14:53:31 +00009345*
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009346* If @destParent is given, it ensures that the tree is namespace
9347* wellformed by creating additional ns-decls where needed.
9348* Note that, since prefixes of already existent ns-decls can be
9349* shadowed by this process, it could break QNames in attribute
9350* values or element content.
9351* TODO:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009352* 1) What to do with XInclude? Currently this returns an error for XInclude.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009353*
9354* Returns 0 if the operation succeeded,
9355* 1 if a node of unsupported (or not yet supported) type was given,
9356* -1 on API/internal errors.
9357*/
9358
9359int
9360xmlDOMWrapCloneNode(xmlDOMWrapCtxtPtr ctxt,
9361 xmlDocPtr sourceDoc,
9362 xmlNodePtr node,
9363 xmlNodePtr *resNode,
9364 xmlDocPtr destDoc,
9365 xmlNodePtr destParent,
9366 int deep,
9367 int options ATTRIBUTE_UNUSED)
9368{
9369 int ret = 0;
9370 xmlNodePtr cur, curElem = NULL;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009371 xmlNsMapPtr nsMap = NULL;
9372 xmlNsMapItemPtr mi;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009373 xmlNsPtr ns;
9374 int depth = -1;
9375 /* int adoptStr = 1; */
9376 /* gather @parent's ns-decls. */
9377 int parnsdone = 0;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009378 /*
Daniel Veillardaa6de472008-08-25 14:53:31 +00009379 * @ancestorsOnly:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009380 * TODO: @ancestorsOnly should be set per option.
9381 *
9382 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009383 int ancestorsOnly = 0;
9384 xmlNodePtr resultClone = NULL, clone = NULL, parentClone = NULL, prevClone = NULL;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009385 xmlNsPtr cloneNs = NULL, *cloneNsDefSlot = NULL;
9386 xmlDictPtr dict; /* The destination dict */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009387
Daniel Veillard11ce4002006-03-10 00:36:23 +00009388 if ((node == NULL) || (resNode == NULL) || (destDoc == NULL))
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009389 return(-1);
9390 /*
9391 * TODO: Initially we support only element-nodes.
9392 */
9393 if (node->type != XML_ELEMENT_NODE)
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009394 return(1);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009395 /*
9396 * Check node->doc sanity.
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009397 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009398 if ((node->doc != NULL) && (sourceDoc != NULL) &&
9399 (node->doc != sourceDoc)) {
9400 /*
9401 * Might be an XIncluded node.
9402 */
9403 return (-1);
9404 }
9405 if (sourceDoc == NULL)
Daniel Veillardaa6de472008-08-25 14:53:31 +00009406 sourceDoc = node->doc;
Daniel Veillard11ce4002006-03-10 00:36:23 +00009407 if (sourceDoc == NULL)
9408 return (-1);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009409
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009410 dict = destDoc->dict;
9411 /*
9412 * Reuse the namespace map of the context.
9413 */
9414 if (ctxt)
9415 nsMap = (xmlNsMapPtr) ctxt->namespaceMap;
9416
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009417 *resNode = NULL;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009418
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009419 cur = node;
Daniel Veillard3e62adb2012-08-09 14:24:02 +08009420 if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
9421 return(-1);
9422
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009423 while (cur != NULL) {
9424 if (cur->doc != sourceDoc) {
9425 /*
9426 * We'll assume XIncluded nodes if the doc differs.
9427 * TODO: Do we need to reconciliate XIncluded nodes?
9428 * TODO: This here returns -1 in this case.
9429 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009430 goto internal_error;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009431 }
9432 /*
9433 * Create a new node.
9434 */
9435 switch (cur->type) {
9436 case XML_XINCLUDE_START:
9437 case XML_XINCLUDE_END:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009438 /*
9439 * TODO: What to do with XInclude?
9440 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009441 goto internal_error;
9442 break;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009443 case XML_ELEMENT_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009444 case XML_TEXT_NODE:
Daniel Veillardaa6de472008-08-25 14:53:31 +00009445 case XML_CDATA_SECTION_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009446 case XML_COMMENT_NODE:
Daniel Veillardaa6de472008-08-25 14:53:31 +00009447 case XML_PI_NODE:
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009448 case XML_DOCUMENT_FRAG_NODE:
9449 case XML_ENTITY_REF_NODE:
9450 case XML_ENTITY_NODE:
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009451 /*
9452 * Nodes of xmlNode structure.
9453 */
9454 clone = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
9455 if (clone == NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009456 xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating a node");
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009457 goto internal_error;
9458 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009459 memset(clone, 0, sizeof(xmlNode));
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009460 /*
9461 * Set hierachical links.
9462 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009463 if (resultClone != NULL) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009464 clone->parent = parentClone;
9465 if (prevClone) {
9466 prevClone->next = clone;
9467 clone->prev = prevClone;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009468 } else
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009469 parentClone->children = clone;
9470 } else
9471 resultClone = clone;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009472
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009473 break;
9474 case XML_ATTRIBUTE_NODE:
9475 /*
9476 * Attributes (xmlAttr).
9477 */
9478 clone = (xmlNodePtr) xmlMalloc(sizeof(xmlAttr));
9479 if (clone == NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009480 xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating an attr-node");
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009481 goto internal_error;
9482 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009483 memset(clone, 0, sizeof(xmlAttr));
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009484 /*
9485 * Set hierachical links.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009486 * TODO: Change this to add to the end of attributes.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009487 */
9488 if (resultClone != NULL) {
9489 clone->parent = parentClone;
9490 if (prevClone) {
9491 prevClone->next = clone;
9492 clone->prev = prevClone;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009493 } else
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009494 parentClone->properties = (xmlAttrPtr) clone;
9495 } else
9496 resultClone = clone;
9497 break;
9498 default:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009499 /*
9500 * TODO QUESTION: Any other nodes expected?
9501 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009502 goto internal_error;
9503 }
9504
9505 clone->type = cur->type;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009506 clone->doc = destDoc;
9507
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009508 /*
9509 * Clone the name of the node if any.
9510 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009511 if (cur->name == xmlStringText)
9512 clone->name = xmlStringText;
9513 else if (cur->name == xmlStringTextNoenc)
9514 /*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009515 * NOTE: Although xmlStringTextNoenc is never assigned to a node
9516 * in tree.c, it might be set in Libxslt via
Daniel Veillardaa6de472008-08-25 14:53:31 +00009517 * "xsl:disable-output-escaping".
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009518 */
9519 clone->name = xmlStringTextNoenc;
9520 else if (cur->name == xmlStringComment)
9521 clone->name = xmlStringComment;
9522 else if (cur->name != NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009523 DICT_CONST_COPY(cur->name, clone->name);
Daniel Veillardaa6de472008-08-25 14:53:31 +00009524 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009525
9526 switch (cur->type) {
9527 case XML_XINCLUDE_START:
9528 case XML_XINCLUDE_END:
9529 /*
9530 * TODO
9531 */
9532 return (-1);
9533 case XML_ELEMENT_NODE:
9534 curElem = cur;
9535 depth++;
9536 /*
9537 * Namespace declarations.
9538 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009539 if (cur->nsDef != NULL) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009540 if (! parnsdone) {
9541 if (destParent && (ctxt == NULL)) {
9542 /*
9543 * Gather @parent's in-scope ns-decls.
9544 */
9545 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
9546 destParent) == -1)
Daniel Veillardaa6de472008-08-25 14:53:31 +00009547 goto internal_error;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009548 }
9549 parnsdone = 1;
9550 }
9551 /*
9552 * Clone namespace declarations.
9553 */
9554 cloneNsDefSlot = &(clone->nsDef);
9555 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
9556 /*
9557 * Create a new xmlNs.
9558 */
9559 cloneNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
9560 if (cloneNs == NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009561 xmlTreeErrMemory("xmlDOMWrapCloneNode(): "
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009562 "allocating namespace");
9563 return(-1);
9564 }
9565 memset(cloneNs, 0, sizeof(xmlNs));
9566 cloneNs->type = XML_LOCAL_NAMESPACE;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009567
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009568 if (ns->href != NULL)
9569 cloneNs->href = xmlStrdup(ns->href);
9570 if (ns->prefix != NULL)
9571 cloneNs->prefix = xmlStrdup(ns->prefix);
9572
9573 *cloneNsDefSlot = cloneNs;
9574 cloneNsDefSlot = &(cloneNs->next);
9575
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009576 /*
9577 * Note that for custom handling of ns-references,
9578 * the ns-decls need not be stored in the ns-map,
9579 * since they won't be referenced by node->ns.
9580 */
9581 if ((ctxt == NULL) ||
9582 (ctxt->getNsForNodeFunc == NULL))
9583 {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009584 /*
9585 * Does it shadow any ns-decl?
9586 */
9587 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00009588 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009589 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
9590 (mi->shadowDepth == -1) &&
9591 ((ns->prefix == mi->newNs->prefix) ||
9592 xmlStrEqual(ns->prefix,
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009593 mi->newNs->prefix))) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009594 /*
9595 * Mark as shadowed at the current
9596 * depth.
9597 */
9598 mi->shadowDepth = depth;
9599 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009600 }
9601 }
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009602 /*
9603 * Push mapping.
9604 */
9605 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
9606 ns, cloneNs, depth) == NULL)
9607 goto internal_error;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009608 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009609 }
9610 }
9611 /* cur->ns will be processed further down. */
9612 break;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009613 case XML_ATTRIBUTE_NODE:
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009614 /* IDs will be processed further down. */
9615 /* cur->ns will be processed further down. */
9616 break;
9617 case XML_TEXT_NODE:
9618 case XML_CDATA_SECTION_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009619 /*
9620 * Note that this will also cover the values of attributes.
9621 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009622 DICT_COPY(cur->content, clone->content);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009623 goto leave_node;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009624 case XML_ENTITY_NODE:
9625 /* TODO: What to do here? */
9626 goto leave_node;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009627 case XML_ENTITY_REF_NODE:
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009628 if (sourceDoc != destDoc) {
9629 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9630 xmlEntityPtr ent;
9631 /*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009632 * Different doc: Assign new entity-node if available.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009633 */
9634 ent = xmlGetDocEntity(destDoc, cur->name);
9635 if (ent != NULL) {
9636 clone->content = ent->content;
9637 clone->children = (xmlNodePtr) ent;
9638 clone->last = (xmlNodePtr) ent;
9639 }
9640 }
9641 } else {
9642 /*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009643 * Same doc: Use the current node's entity declaration
9644 * and value.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009645 */
9646 clone->content = cur->content;
9647 clone->children = cur->children;
9648 clone->last = cur->last;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009649 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009650 goto leave_node;
9651 case XML_PI_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009652 DICT_COPY(cur->content, clone->content);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009653 goto leave_node;
9654 case XML_COMMENT_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009655 DICT_COPY(cur->content, clone->content);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009656 goto leave_node;
9657 default:
9658 goto internal_error;
9659 }
9660
9661 if (cur->ns == NULL)
9662 goto end_ns_reference;
9663
9664/* handle_ns_reference: */
9665 /*
9666 ** The following will take care of references to ns-decls ********
Daniel Veillardaa6de472008-08-25 14:53:31 +00009667 ** and is intended only for element- and attribute-nodes.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009668 **
9669 */
9670 if (! parnsdone) {
9671 if (destParent && (ctxt == NULL)) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009672 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, destParent) == -1)
Daniel Veillardaa6de472008-08-25 14:53:31 +00009673 goto internal_error;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009674 }
9675 parnsdone = 1;
9676 }
9677 /*
9678 * Adopt ns-references.
9679 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009680 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009681 /*
9682 * Search for a mapping.
9683 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009684 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009685 if ((mi->shadowDepth == -1) &&
9686 (cur->ns == mi->oldNs)) {
9687 /*
9688 * This is the nice case: a mapping was found.
9689 */
9690 clone->ns = mi->newNs;
9691 goto end_ns_reference;
9692 }
9693 }
9694 }
9695 /*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009696 * No matching namespace in scope. We need a new one.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009697 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009698 if ((ctxt != NULL) && (ctxt->getNsForNodeFunc != NULL)) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009699 /*
9700 * User-defined behaviour.
9701 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009702 ns = ctxt->getNsForNodeFunc(ctxt, cur,
9703 cur->ns->href, cur->ns->prefix);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009704 /*
9705 * Add user's mapping.
9706 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009707 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009708 cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
9709 goto internal_error;
9710 clone->ns = ns;
9711 } else {
9712 /*
9713 * Aquire a normalized ns-decl and add it to the map.
9714 */
9715 if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc,
Daniel Veillardaa6de472008-08-25 14:53:31 +00009716 /* ns-decls on curElem or on destDoc->oldNs */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009717 destParent ? curElem : NULL,
9718 cur->ns, &ns,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009719 &nsMap, depth,
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009720 /* if we need to search only in the ancestor-axis */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009721 ancestorsOnly,
9722 /* ns-decls must be prefixed for attributes. */
9723 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
9724 goto internal_error;
9725 clone->ns = ns;
9726 }
9727
9728end_ns_reference:
9729
9730 /*
9731 * Some post-processing.
9732 *
9733 * Handle ID attributes.
9734 */
9735 if ((clone->type == XML_ATTRIBUTE_NODE) &&
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009736 (clone->parent != NULL))
9737 {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009738 if (xmlIsID(destDoc, clone->parent, (xmlAttrPtr) clone)) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00009739
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009740 xmlChar *idVal;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009741
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009742 idVal = xmlNodeListGetString(cur->doc, cur->children, 1);
9743 if (idVal != NULL) {
9744 if (xmlAddID(NULL, destDoc, idVal, (xmlAttrPtr) cur) == NULL) {
9745 /* TODO: error message. */
9746 xmlFree(idVal);
9747 goto internal_error;
9748 }
9749 xmlFree(idVal);
9750 }
9751 }
9752 }
9753 /*
9754 **
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009755 ** The following will traverse the tree **************************
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009756 **
Daniel Veillardaa6de472008-08-25 14:53:31 +00009757 *
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009758 * Walk the element's attributes before descending into child-nodes.
9759 */
9760 if ((cur->type == XML_ELEMENT_NODE) && (cur->properties != NULL)) {
9761 prevClone = NULL;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009762 parentClone = clone;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009763 cur = (xmlNodePtr) cur->properties;
9764 continue;
9765 }
9766into_content:
9767 /*
9768 * Descend into child-nodes.
9769 */
9770 if (cur->children != NULL) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009771 if (deep || (cur->type == XML_ATTRIBUTE_NODE)) {
9772 prevClone = NULL;
9773 parentClone = clone;
9774 cur = cur->children;
9775 continue;
9776 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009777 }
9778
9779leave_node:
9780 /*
9781 * At this point we are done with the node, its content
9782 * and an element-nodes's attribute-nodes.
9783 */
9784 if (cur == node)
9785 break;
9786 if ((cur->type == XML_ELEMENT_NODE) ||
9787 (cur->type == XML_XINCLUDE_START) ||
9788 (cur->type == XML_XINCLUDE_END)) {
9789 /*
9790 * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
9791 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009792 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009793 /*
9794 * Pop mappings.
9795 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009796 while ((nsMap->last != NULL) &&
9797 (nsMap->last->depth >= depth))
9798 {
Daniel Veillardaa6de472008-08-25 14:53:31 +00009799 XML_NSMAP_POP(nsMap, mi)
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009800 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009801 /*
9802 * Unshadow.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009803 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009804 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009805 if (mi->shadowDepth >= depth)
9806 mi->shadowDepth = -1;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009807 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009808 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009809 depth--;
9810 }
9811 if (cur->next != NULL) {
9812 prevClone = clone;
9813 cur = cur->next;
9814 } else if (cur->type != XML_ATTRIBUTE_NODE) {
9815 /*
9816 * Set clone->last.
9817 */
Daniel Veillard11ce4002006-03-10 00:36:23 +00009818 if (clone->parent != NULL)
9819 clone->parent->last = clone;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009820 clone = clone->parent;
Daniel Veillard75d13092013-09-11 15:11:27 +08009821 if (clone != NULL)
9822 parentClone = clone->parent;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009823 /*
9824 * Process parent --> next;
9825 */
9826 cur = cur->parent;
9827 goto leave_node;
9828 } else {
9829 /* This is for attributes only. */
9830 clone = clone->parent;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009831 parentClone = clone->parent;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009832 /*
9833 * Process parent-element --> children.
9834 */
9835 cur = cur->parent;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009836 goto into_content;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009837 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009838 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009839 goto exit;
9840
9841internal_error:
9842 ret = -1;
9843
9844exit:
9845 /*
9846 * Cleanup.
9847 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009848 if (nsMap != NULL) {
9849 if ((ctxt) && (ctxt->namespaceMap == nsMap)) {
9850 /*
9851 * Just cleanup the map but don't free.
9852 */
9853 if (nsMap->first) {
9854 if (nsMap->pool)
9855 nsMap->last->next = nsMap->pool;
9856 nsMap->pool = nsMap->first;
9857 nsMap->first = NULL;
9858 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009859 } else
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009860 xmlDOMWrapNsMapFree(nsMap);
9861 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009862 /*
9863 * TODO: Should we try a cleanup of the cloned node in case of a
9864 * fatal error?
9865 */
9866 *resNode = resultClone;
9867 return (ret);
9868}
9869
9870/*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009871* xmlDOMWrapAdoptAttr:
9872* @ctxt: the optional context for custom processing
9873* @sourceDoc: the optional source document of attr
9874* @attr: the attribute-node to be adopted
9875* @destDoc: the destination doc for adoption
9876* @destParent: the optional new parent of @attr in @destDoc
9877* @options: option flags
9878*
9879* @attr is adopted by @destDoc.
9880* Ensures that ns-references point to @destDoc: either to
9881* elements->nsDef entries if @destParent is given, or to
9882* @destDoc->oldNs otherwise.
9883*
9884* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
9885*/
9886static int
9887xmlDOMWrapAdoptAttr(xmlDOMWrapCtxtPtr ctxt,
9888 xmlDocPtr sourceDoc,
9889 xmlAttrPtr attr,
9890 xmlDocPtr destDoc,
9891 xmlNodePtr destParent,
9892 int options ATTRIBUTE_UNUSED)
9893{
9894 xmlNodePtr cur;
9895 int adoptStr = 1;
9896
9897 if ((attr == NULL) || (destDoc == NULL))
9898 return (-1);
Daniel Veillardaa6de472008-08-25 14:53:31 +00009899
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009900 attr->doc = destDoc;
9901 if (attr->ns != NULL) {
9902 xmlNsPtr ns = NULL;
9903
9904 if (ctxt != NULL) {
9905 /* TODO: User defined. */
9906 }
9907 /* XML Namespace. */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009908 if (IS_STR_XML(attr->ns->prefix)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009909 ns = xmlTreeEnsureXMLDecl(destDoc);
9910 } else if (destParent == NULL) {
9911 /*
9912 * Store in @destDoc->oldNs.
9913 */
9914 ns = xmlDOMWrapStoreNs(destDoc, attr->ns->href, attr->ns->prefix);
9915 } else {
9916 /*
9917 * Declare on @destParent.
9918 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009919 if (xmlSearchNsByNamespaceStrict(destDoc, destParent, attr->ns->href,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009920 &ns, 1) == -1)
9921 goto internal_error;
9922 if (ns == NULL) {
9923 ns = xmlDOMWrapNSNormDeclareNsForced(destDoc, destParent,
9924 attr->ns->href, attr->ns->prefix, 1);
9925 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009926 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009927 if (ns == NULL)
9928 goto internal_error;
9929 attr->ns = ns;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009930 }
9931
9932 XML_TREE_ADOPT_STR(attr->name);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009933 attr->atype = 0;
9934 attr->psvi = NULL;
9935 /*
9936 * Walk content.
9937 */
9938 if (attr->children == NULL)
9939 return (0);
9940 cur = attr->children;
Daniel Veillard3e62adb2012-08-09 14:24:02 +08009941 if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
9942 goto internal_error;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009943 while (cur != NULL) {
9944 cur->doc = destDoc;
9945 switch (cur->type) {
9946 case XML_TEXT_NODE:
9947 case XML_CDATA_SECTION_NODE:
9948 XML_TREE_ADOPT_STR_2(cur->content)
Daniel Veillardaa6de472008-08-25 14:53:31 +00009949 break;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009950 case XML_ENTITY_REF_NODE:
9951 /*
9952 * Remove reference to the entitity-node.
9953 */
9954 cur->content = NULL;
9955 cur->children = NULL;
9956 cur->last = NULL;
9957 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9958 xmlEntityPtr ent;
9959 /*
9960 * Assign new entity-node if available.
9961 */
9962 ent = xmlGetDocEntity(destDoc, cur->name);
9963 if (ent != NULL) {
9964 cur->content = ent->content;
9965 cur->children = (xmlNodePtr) ent;
9966 cur->last = (xmlNodePtr) ent;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009967 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009968 }
9969 break;
9970 default:
9971 break;
9972 }
9973 if (cur->children != NULL) {
9974 cur = cur->children;
9975 continue;
9976 }
9977next_sibling:
9978 if (cur == (xmlNodePtr) attr)
9979 break;
9980 if (cur->next != NULL)
9981 cur = cur->next;
9982 else {
9983 cur = cur->parent;
9984 goto next_sibling;
9985 }
9986 }
9987 return (0);
9988internal_error:
9989 return (-1);
9990}
9991
9992/*
9993* xmlDOMWrapAdoptNode:
9994* @ctxt: the optional context for custom processing
9995* @sourceDoc: the optional sourceDoc
9996* @node: the node to start with
9997* @destDoc: the destination doc
Kasimier T. Buchcik4d9c9482005-06-27 15:04:46 +00009998* @destParent: the optional new parent of @node in @destDoc
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009999* @options: option flags
10000*
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +000010001* References of out-of scope ns-decls are remapped to point to @destDoc:
10002* 1) If @destParent is given, then nsDef entries on element-nodes are used
10003* 2) If *no* @destParent is given, then @destDoc->oldNs entries are used
Daniel Veillard7e35abe2014-03-28 22:55:31 +080010004* This is the case when you have an unlinked node and just want to move it
Daniel Veillardaa6de472008-08-25 14:53:31 +000010005* to the context of
10006*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010007* If @destParent is given, it ensures that the tree is namespace
10008* wellformed by creating additional ns-decls where needed.
10009* Note that, since prefixes of already existent ns-decls can be
10010* shadowed by this process, it could break QNames in attribute
10011* values or element content.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +000010012* NOTE: This function was not intensively tested.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010013*
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +000010014* Returns 0 if the operation succeeded,
10015* 1 if a node of unsupported type was given,
10016* 2 if a node of not yet supported type was given and
10017* -1 on API/internal errors.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010018*/
10019int
10020xmlDOMWrapAdoptNode(xmlDOMWrapCtxtPtr ctxt,
10021 xmlDocPtr sourceDoc,
10022 xmlNodePtr node,
Daniel Veillardaa6de472008-08-25 14:53:31 +000010023 xmlDocPtr destDoc,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010024 xmlNodePtr destParent,
10025 int options)
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +000010026{
Daniel Veillard3e62adb2012-08-09 14:24:02 +080010027 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) ||
10028 (destDoc == NULL) ||
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010029 ((destParent != NULL) && (destParent->doc != destDoc)))
10030 return(-1);
10031 /*
10032 * Check node->doc sanity.
Daniel Veillardaa6de472008-08-25 14:53:31 +000010033 */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010034 if ((node->doc != NULL) && (sourceDoc != NULL) &&
10035 (node->doc != sourceDoc)) {
10036 /*
10037 * Might be an XIncluded node.
10038 */
10039 return (-1);
10040 }
10041 if (sourceDoc == NULL)
10042 sourceDoc = node->doc;
10043 if (sourceDoc == destDoc)
10044 return (-1);
10045 switch (node->type) {
Daniel Veillardaa6de472008-08-25 14:53:31 +000010046 case XML_ELEMENT_NODE:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010047 case XML_ATTRIBUTE_NODE:
10048 case XML_TEXT_NODE:
10049 case XML_CDATA_SECTION_NODE:
10050 case XML_ENTITY_REF_NODE:
10051 case XML_PI_NODE:
10052 case XML_COMMENT_NODE:
10053 break;
10054 case XML_DOCUMENT_FRAG_NODE:
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +000010055 /* TODO: Support document-fragment-nodes. */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010056 return (2);
10057 default:
10058 return (1);
10059 }
10060 /*
10061 * Unlink only if @node was not already added to @destParent.
10062 */
10063 if ((node->parent != NULL) && (destParent != node->parent))
10064 xmlUnlinkNode(node);
10065
10066 if (node->type == XML_ELEMENT_NODE) {
10067 return (xmlDOMWrapAdoptBranch(ctxt, sourceDoc, node,
10068 destDoc, destParent, options));
10069 } else if (node->type == XML_ATTRIBUTE_NODE) {
10070 return (xmlDOMWrapAdoptAttr(ctxt, sourceDoc,
10071 (xmlAttrPtr) node, destDoc, destParent, options));
Daniel Veillardaa6de472008-08-25 14:53:31 +000010072 } else {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010073 xmlNodePtr cur = node;
10074 int adoptStr = 1;
10075
10076 cur->doc = destDoc;
10077 /*
10078 * Optimize string adoption.
10079 */
10080 if ((sourceDoc != NULL) &&
10081 (sourceDoc->dict == destDoc->dict))
10082 adoptStr = 0;
10083 switch (node->type) {
Daniel Veillardaa6de472008-08-25 14:53:31 +000010084 case XML_TEXT_NODE:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010085 case XML_CDATA_SECTION_NODE:
10086 XML_TREE_ADOPT_STR_2(node->content)
10087 break;
10088 case XML_ENTITY_REF_NODE:
10089 /*
10090 * Remove reference to the entitity-node.
10091 */
10092 node->content = NULL;
10093 node->children = NULL;
10094 node->last = NULL;
10095 if ((destDoc->intSubset) || (destDoc->extSubset)) {
10096 xmlEntityPtr ent;
10097 /*
10098 * Assign new entity-node if available.
10099 */
10100 ent = xmlGetDocEntity(destDoc, node->name);
10101 if (ent != NULL) {
10102 node->content = ent->content;
10103 node->children = (xmlNodePtr) ent;
10104 node->last = (xmlNodePtr) ent;
10105 }
10106 }
10107 XML_TREE_ADOPT_STR(node->name)
10108 break;
Daniel Veillard7e21fd12005-07-03 21:44:07 +000010109 case XML_PI_NODE: {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010110 XML_TREE_ADOPT_STR(node->name)
10111 XML_TREE_ADOPT_STR_2(node->content)
10112 break;
Daniel Veillard7e21fd12005-07-03 21:44:07 +000010113 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010114 default:
10115 break;
10116 }
Daniel Veillardaa6de472008-08-25 14:53:31 +000010117 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010118 return (0);
10119}
10120
Daniel Veillard5d4644e2005-04-01 13:11:58 +000010121#define bottom_tree
10122#include "elfgcchack.h"