blob: 7853e35fd7234e9805459068521ce6df999cbc02 [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
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +000058static xmlChar* xmlGetPropNodeValueInternal(xmlAttrPtr prop);
59
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
163xmlGetEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) {
164 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
184xmlGetParameterEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) {
185 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
919xmlGetIntSubset(xmlDocPtr doc) {
920 xmlNodePtr cur;
921
922 if (doc == NULL)
923 return(NULL);
924 cur = doc->children;
925 while (cur != NULL) {
926 if (cur->type == XML_DTD_NODE)
927 return((xmlDtdPtr) cur);
928 cur = cur->next;
929 }
930 return((xmlDtdPtr) doc->intSubset);
931}
932
933/**
934 * xmlCreateIntSubset:
935 * @doc: the document pointer
936 * @name: the DTD name
Daniel Veillarde356c282001-03-10 12:32:04 +0000937 * @ExternalID: the external (PUBLIC) ID
Owen Taylor3473f882001-02-23 17:55:21 +0000938 * @SystemID: the system ID
939 *
940 * Create the internal subset of a document
941 * Returns a pointer to the new DTD structure
942 */
943xmlDtdPtr
944xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name,
945 const xmlChar *ExternalID, const xmlChar *SystemID) {
946 xmlDtdPtr cur;
947
948 if ((doc != NULL) && (xmlGetIntSubset(doc) != NULL)) {
949#ifdef DEBUG_TREE
950 xmlGenericError(xmlGenericErrorContext,
951
952 "xmlCreateIntSubset(): document %s already have an internal subset\n",
953 doc->name);
954#endif
955 return(NULL);
956 }
957
958 /*
959 * Allocate a new DTD and fill the fields.
960 */
961 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
962 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000963 xmlTreeErrMemory("building internal subset");
Owen Taylor3473f882001-02-23 17:55:21 +0000964 return(NULL);
965 }
966 memset(cur, 0, sizeof(xmlDtd));
967 cur->type = XML_DTD_NODE;
968
William M. Bracka3215c72004-07-31 16:24:01 +0000969 if (name != NULL) {
970 cur->name = xmlStrdup(name);
971 if (cur->name == NULL) {
972 xmlTreeErrMemory("building internal subset");
973 xmlFree(cur);
974 return(NULL);
975 }
976 }
977 if (ExternalID != NULL) {
Daniel Veillardaa6de472008-08-25 14:53:31 +0000978 cur->ExternalID = xmlStrdup(ExternalID);
William M. Bracka3215c72004-07-31 16:24:01 +0000979 if (cur->ExternalID == NULL) {
980 xmlTreeErrMemory("building internal subset");
981 if (cur->name != NULL)
982 xmlFree((char *)cur->name);
983 xmlFree(cur);
984 return(NULL);
985 }
986 }
987 if (SystemID != NULL) {
Daniel Veillardaa6de472008-08-25 14:53:31 +0000988 cur->SystemID = xmlStrdup(SystemID);
William M. Bracka3215c72004-07-31 16:24:01 +0000989 if (cur->SystemID == NULL) {
990 xmlTreeErrMemory("building internal subset");
991 if (cur->name != NULL)
992 xmlFree((char *)cur->name);
993 if (cur->ExternalID != NULL)
994 xmlFree((char *)cur->ExternalID);
995 xmlFree(cur);
996 return(NULL);
997 }
998 }
Owen Taylor3473f882001-02-23 17:55:21 +0000999 if (doc != NULL) {
1000 doc->intSubset = cur;
1001 cur->parent = doc;
1002 cur->doc = doc;
1003 if (doc->children == NULL) {
1004 doc->children = (xmlNodePtr) cur;
1005 doc->last = (xmlNodePtr) cur;
1006 } else {
Owen Taylor3473f882001-02-23 17:55:21 +00001007 if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillarde356c282001-03-10 12:32:04 +00001008 xmlNodePtr prev;
1009
Owen Taylor3473f882001-02-23 17:55:21 +00001010 prev = doc->children;
1011 prev->prev = (xmlNodePtr) cur;
1012 cur->next = prev;
1013 doc->children = (xmlNodePtr) cur;
1014 } else {
Daniel Veillarde356c282001-03-10 12:32:04 +00001015 xmlNodePtr next;
1016
1017 next = doc->children;
1018 while ((next != NULL) && (next->type != XML_ELEMENT_NODE))
1019 next = next->next;
1020 if (next == NULL) {
1021 cur->prev = doc->last;
1022 cur->prev->next = (xmlNodePtr) cur;
1023 cur->next = NULL;
1024 doc->last = (xmlNodePtr) cur;
1025 } else {
1026 cur->next = next;
1027 cur->prev = next->prev;
1028 if (cur->prev == NULL)
1029 doc->children = (xmlNodePtr) cur;
1030 else
1031 cur->prev->next = (xmlNodePtr) cur;
1032 next->prev = (xmlNodePtr) cur;
1033 }
Owen Taylor3473f882001-02-23 17:55:21 +00001034 }
1035 }
1036 }
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001037
Daniel Veillarda880b122003-04-21 21:36:41 +00001038 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001039 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001040 return(cur);
1041}
1042
1043/**
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001044 * DICT_FREE:
1045 * @str: a string
1046 *
1047 * Free a string if it is not owned by the "dict" dictionnary in the
1048 * current scope
1049 */
1050#define DICT_FREE(str) \
Daniel Veillardaa6de472008-08-25 14:53:31 +00001051 if ((str) && ((!dict) || \
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001052 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
1053 xmlFree((char *)(str));
1054
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00001055
1056/**
1057 * DICT_COPY:
1058 * @str: a string
1059 *
1060 * Copy a string using a "dict" dictionnary in the current scope,
1061 * if availabe.
1062 */
1063#define DICT_COPY(str, cpy) \
1064 if (str) { \
1065 if (dict) { \
1066 if (xmlDictOwns(dict, (const xmlChar *)(str))) \
1067 cpy = (xmlChar *) (str); \
1068 else \
1069 cpy = (xmlChar *) xmlDictLookup((dict), (const xmlChar *)(str), -1); \
1070 } else \
1071 cpy = xmlStrdup((const xmlChar *)(str)); }
1072
1073/**
1074 * DICT_CONST_COPY:
1075 * @str: a string
1076 *
1077 * Copy a string using a "dict" dictionnary in the current scope,
1078 * if availabe.
1079 */
1080#define DICT_CONST_COPY(str, cpy) \
1081 if (str) { \
1082 if (dict) { \
1083 if (xmlDictOwns(dict, (const xmlChar *)(str))) \
1084 cpy = (const xmlChar *) (str); \
1085 else \
1086 cpy = xmlDictLookup((dict), (const xmlChar *)(str), -1); \
1087 } else \
1088 cpy = (const xmlChar *) xmlStrdup((const xmlChar *)(str)); }
1089
1090
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001091/**
Owen Taylor3473f882001-02-23 17:55:21 +00001092 * xmlFreeDtd:
1093 * @cur: the DTD structure to free up
1094 *
1095 * Free a DTD structure.
1096 */
1097void
1098xmlFreeDtd(xmlDtdPtr cur) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001099 xmlDictPtr dict = NULL;
1100
Owen Taylor3473f882001-02-23 17:55:21 +00001101 if (cur == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001102 return;
1103 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001104 if (cur->doc != NULL) dict = cur->doc->dict;
Daniel Veillard5335dc52003-01-01 20:59:38 +00001105
Daniel Veillarda880b122003-04-21 21:36:41 +00001106 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001107 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
1108
Owen Taylor3473f882001-02-23 17:55:21 +00001109 if (cur->children != NULL) {
1110 xmlNodePtr next, c = cur->children;
1111
1112 /*
William M. Brack18a04f22004-08-03 16:42:37 +00001113 * Cleanup all nodes which are not part of the specific lists
1114 * of notations, elements, attributes and entities.
Owen Taylor3473f882001-02-23 17:55:21 +00001115 */
1116 while (c != NULL) {
1117 next = c->next;
William M. Brack18a04f22004-08-03 16:42:37 +00001118 if ((c->type != XML_NOTATION_NODE) &&
1119 (c->type != XML_ELEMENT_DECL) &&
1120 (c->type != XML_ATTRIBUTE_DECL) &&
1121 (c->type != XML_ENTITY_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001122 xmlUnlinkNode(c);
1123 xmlFreeNode(c);
1124 }
1125 c = next;
1126 }
1127 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001128 DICT_FREE(cur->name)
1129 DICT_FREE(cur->SystemID)
1130 DICT_FREE(cur->ExternalID)
Owen Taylor3473f882001-02-23 17:55:21 +00001131 /* TODO !!! */
1132 if (cur->notations != NULL)
1133 xmlFreeNotationTable((xmlNotationTablePtr) cur->notations);
Daniel Veillardaa6de472008-08-25 14:53:31 +00001134
Owen Taylor3473f882001-02-23 17:55:21 +00001135 if (cur->elements != NULL)
1136 xmlFreeElementTable((xmlElementTablePtr) cur->elements);
1137 if (cur->attributes != NULL)
1138 xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes);
1139 if (cur->entities != NULL)
1140 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities);
1141 if (cur->pentities != NULL)
1142 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->pentities);
1143
Owen Taylor3473f882001-02-23 17:55:21 +00001144 xmlFree(cur);
1145}
1146
1147/**
1148 * xmlNewDoc:
1149 * @version: xmlChar string giving the version of XML "1.0"
1150 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001151 * Creates a new XML document
1152 *
Owen Taylor3473f882001-02-23 17:55:21 +00001153 * Returns a new document
1154 */
1155xmlDocPtr
1156xmlNewDoc(const xmlChar *version) {
1157 xmlDocPtr cur;
1158
1159 if (version == NULL)
1160 version = (const xmlChar *) "1.0";
1161
1162 /*
1163 * Allocate a new document and fill the fields.
1164 */
1165 cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
1166 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001167 xmlTreeErrMemory("building doc");
Owen Taylor3473f882001-02-23 17:55:21 +00001168 return(NULL);
1169 }
1170 memset(cur, 0, sizeof(xmlDoc));
1171 cur->type = XML_DOCUMENT_NODE;
1172
Daniel Veillardaa6de472008-08-25 14:53:31 +00001173 cur->version = xmlStrdup(version);
William M. Bracka3215c72004-07-31 16:24:01 +00001174 if (cur->version == NULL) {
1175 xmlTreeErrMemory("building doc");
1176 xmlFree(cur);
Daniel Veillardae0765b2008-07-31 19:54:59 +00001177 return(NULL);
William M. Bracka3215c72004-07-31 16:24:01 +00001178 }
Owen Taylor3473f882001-02-23 17:55:21 +00001179 cur->standalone = -1;
1180 cur->compression = -1; /* not initialized */
1181 cur->doc = cur;
Daniel Veillardae0765b2008-07-31 19:54:59 +00001182 cur->parseFlags = 0;
1183 cur->properties = XML_DOC_USERBUILT;
Daniel Veillarda6874ca2003-07-29 16:47:24 +00001184 /*
1185 * The in memory encoding is always UTF8
1186 * This field will never change and would
1187 * be obsolete if not for binary compatibility.
1188 */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00001189 cur->charset = XML_CHAR_ENCODING_UTF8;
Daniel Veillard5335dc52003-01-01 20:59:38 +00001190
Daniel Veillarda880b122003-04-21 21:36:41 +00001191 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001192 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001193 return(cur);
1194}
1195
1196/**
1197 * xmlFreeDoc:
1198 * @cur: pointer to the document
Owen Taylor3473f882001-02-23 17:55:21 +00001199 *
1200 * Free up all the structures used by a document, tree included.
1201 */
1202void
1203xmlFreeDoc(xmlDocPtr cur) {
Daniel Veillarda9142e72001-06-19 11:07:54 +00001204 xmlDtdPtr extSubset, intSubset;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001205 xmlDictPtr dict = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001206
Owen Taylor3473f882001-02-23 17:55:21 +00001207 if (cur == NULL) {
1208#ifdef DEBUG_TREE
1209 xmlGenericError(xmlGenericErrorContext,
1210 "xmlFreeDoc : document == NULL\n");
1211#endif
1212 return;
1213 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001214#ifdef LIBXML_DEBUG_RUNTIME
Daniel Veillardbca3ad22005-08-23 22:14:02 +00001215#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001216 xmlDebugCheckDocument(stderr, cur);
1217#endif
Daniel Veillardbca3ad22005-08-23 22:14:02 +00001218#endif
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001219
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001220 if (cur != NULL) dict = cur->dict;
Daniel Veillard5335dc52003-01-01 20:59:38 +00001221
Daniel Veillarda880b122003-04-21 21:36:41 +00001222 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001223 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
1224
Daniel Veillard76d66f42001-05-16 21:05:17 +00001225 /*
1226 * Do this before freeing the children list to avoid ID lookups
1227 */
1228 if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids);
1229 cur->ids = NULL;
1230 if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
1231 cur->refs = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001232 extSubset = cur->extSubset;
1233 intSubset = cur->intSubset;
Daniel Veillard5997aca2002-03-18 18:36:20 +00001234 if (intSubset == extSubset)
1235 extSubset = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001236 if (extSubset != NULL) {
Daniel Veillard76d66f42001-05-16 21:05:17 +00001237 xmlUnlinkNode((xmlNodePtr) cur->extSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001238 cur->extSubset = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001239 xmlFreeDtd(extSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001240 }
Daniel Veillarda9142e72001-06-19 11:07:54 +00001241 if (intSubset != NULL) {
Daniel Veillard76d66f42001-05-16 21:05:17 +00001242 xmlUnlinkNode((xmlNodePtr) cur->intSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001243 cur->intSubset = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001244 xmlFreeDtd(intSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001245 }
1246
1247 if (cur->children != NULL) xmlFreeNodeList(cur->children);
Owen Taylor3473f882001-02-23 17:55:21 +00001248 if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001249
1250 DICT_FREE(cur->version)
1251 DICT_FREE(cur->name)
1252 DICT_FREE(cur->encoding)
1253 DICT_FREE(cur->URL)
Owen Taylor3473f882001-02-23 17:55:21 +00001254 xmlFree(cur);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001255 if (dict) xmlDictFree(dict);
Owen Taylor3473f882001-02-23 17:55:21 +00001256}
1257
1258/**
1259 * xmlStringLenGetNodeList:
1260 * @doc: the document
1261 * @value: the value of the text
1262 * @len: the length of the string value
1263 *
1264 * Parse the value string and build the node list associated. Should
1265 * produce a flat tree with only TEXTs and ENTITY_REFs.
1266 * Returns a pointer to the first child
1267 */
1268xmlNodePtr
1269xmlStringLenGetNodeList(xmlDocPtr doc, const xmlChar *value, int len) {
1270 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) {
1454 last = ret = node;
1455 } else {
1456 last = xmlAddNextSibling(last, node);
1457 }
1458 } else if (ret == NULL) {
1459 ret = xmlNewDocText(doc, BAD_CAST "");
1460 }
1461
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
1477xmlStringGetNodeList(xmlDocPtr doc, const xmlChar *value) {
1478 xmlNodePtr ret = NULL, last = NULL;
1479 xmlNodePtr node;
1480 xmlChar *val;
1481 const xmlChar *cur = value;
1482 const xmlChar *q;
1483 xmlEntityPtr ent;
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001484 xmlBufPtr buf;
Owen Taylor3473f882001-02-23 17:55:21 +00001485
1486 if (value == NULL) return(NULL);
1487
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001488 buf = xmlBufCreateSize(0);
1489 if (buf == NULL) return(NULL);
1490 xmlBufSetAllocationScheme(buf, XML_BUFFER_ALLOC_HYBRID);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001491
Owen Taylor3473f882001-02-23 17:55:21 +00001492 q = cur;
1493 while (*cur != 0) {
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001494 if (cur[0] == '&') {
1495 int charval = 0;
1496 xmlChar tmp;
1497
Owen Taylor3473f882001-02-23 17:55:21 +00001498 /*
1499 * Save the current text.
1500 */
1501 if (cur != q) {
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001502 if (xmlBufAdd(buf, q, cur - q))
Conrad Irwin7d553f82012-05-10 20:17:25 -07001503 goto out;
Owen Taylor3473f882001-02-23 17:55:21 +00001504 }
Owen Taylor3473f882001-02-23 17:55:21 +00001505 q = cur;
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001506 if ((cur[1] == '#') && (cur[2] == 'x')) {
1507 cur += 3;
1508 tmp = *cur;
1509 while (tmp != ';') { /* Non input consuming loop */
Daniel Veillardaa6de472008-08-25 14:53:31 +00001510 if ((tmp >= '0') && (tmp <= '9'))
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001511 charval = charval * 16 + (tmp - '0');
1512 else if ((tmp >= 'a') && (tmp <= 'f'))
1513 charval = charval * 16 + (tmp - 'a') + 10;
1514 else if ((tmp >= 'A') && (tmp <= 'F'))
1515 charval = charval * 16 + (tmp - 'A') + 10;
Owen Taylor3473f882001-02-23 17:55:21 +00001516 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001517 xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
1518 NULL);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001519 charval = 0;
1520 break;
1521 }
1522 cur++;
1523 tmp = *cur;
1524 }
1525 if (tmp == ';')
1526 cur++;
1527 q = cur;
1528 } else if (cur[1] == '#') {
1529 cur += 2;
1530 tmp = *cur;
1531 while (tmp != ';') { /* Non input consuming loops */
Daniel Veillardaa6de472008-08-25 14:53:31 +00001532 if ((tmp >= '0') && (tmp <= '9'))
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001533 charval = charval * 10 + (tmp - '0');
1534 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001535 xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
1536 NULL);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001537 charval = 0;
1538 break;
1539 }
1540 cur++;
1541 tmp = *cur;
1542 }
1543 if (tmp == ';')
1544 cur++;
1545 q = cur;
1546 } else {
1547 /*
1548 * Read the entity string
1549 */
1550 cur++;
1551 q = cur;
1552 while ((*cur != 0) && (*cur != ';')) cur++;
1553 if (*cur == 0) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001554 xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY,
1555 (xmlNodePtr) doc, (const char *) q);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001556 goto out;
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001557 }
1558 if (cur != q) {
1559 /*
1560 * Predefined entities don't generate nodes
1561 */
1562 val = xmlStrndup(q, cur - q);
1563 ent = xmlGetDocEntity(doc, val);
1564 if ((ent != NULL) &&
1565 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
Conrad Irwin7d553f82012-05-10 20:17:25 -07001566
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001567 if (xmlBufCat(buf, ent->content))
Conrad Irwin7d553f82012-05-10 20:17:25 -07001568 goto out;
Daniel Veillardaa6de472008-08-25 14:53:31 +00001569
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001570 } else {
1571 /*
Conrad Irwin7d553f82012-05-10 20:17:25 -07001572 * Flush buffer so far
1573 */
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001574 if (!xmlBufIsEmpty(buf)) {
Conrad Irwin7d553f82012-05-10 20:17:25 -07001575 node = xmlNewDocText(doc, NULL);
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001576 node->content = xmlBufDetach(buf);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001577
1578 if (last == NULL) {
1579 last = ret = node;
1580 } else {
1581 last = xmlAddNextSibling(last, node);
1582 }
1583 }
1584
1585 /*
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001586 * Create a new REFERENCE_REF node
1587 */
1588 node = xmlNewReference(doc, val);
1589 if (node == NULL) {
1590 if (val != NULL) xmlFree(val);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001591 goto out;
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001592 }
Daniel Veillardbf8dae82002-04-18 16:39:10 +00001593 else if ((ent != NULL) && (ent->children == NULL)) {
1594 xmlNodePtr temp;
1595
1596 ent->children = xmlStringGetNodeList(doc,
1597 (const xmlChar*)node->content);
Daniel Veillard2d84a892002-12-30 00:01:08 +00001598 ent->owner = 1;
Daniel Veillardbf8dae82002-04-18 16:39:10 +00001599 temp = ent->children;
1600 while (temp) {
1601 temp->parent = (xmlNodePtr)ent;
1602 temp = temp->next;
1603 }
1604 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001605 if (last == NULL) {
1606 last = ret = node;
1607 } else {
1608 last = xmlAddNextSibling(last, node);
1609 }
1610 }
1611 xmlFree(val);
1612 }
1613 cur++;
1614 q = cur;
1615 }
1616 if (charval != 0) {
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001617 xmlChar buffer[10];
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001618 int len;
1619
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001620 len = xmlCopyCharMultiByte(buffer, charval);
1621 buffer[len] = 0;
Conrad Irwin7d553f82012-05-10 20:17:25 -07001622
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001623 if (xmlBufCat(buf, buffer))
Conrad Irwin7d553f82012-05-10 20:17:25 -07001624 goto out;
1625 charval = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001626 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001627 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001628 cur++;
1629 }
Daniel Veillard75bea542001-05-11 17:41:21 +00001630 if ((cur != q) || (ret == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001631 /*
1632 * Handle the last piece of text.
1633 */
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001634 xmlBufAdd(buf, q, cur - q);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001635 }
1636
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001637 if (!xmlBufIsEmpty(buf)) {
Conrad Irwin7d553f82012-05-10 20:17:25 -07001638 node = xmlNewDocText(doc, NULL);
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001639 node->content = xmlBufDetach(buf);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001640
1641 if (last == NULL) {
1642 last = ret = node;
Owen Taylor3473f882001-02-23 17:55:21 +00001643 } else {
Conrad Irwin7d553f82012-05-10 20:17:25 -07001644 last = xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001645 }
1646 }
Conrad Irwin7d553f82012-05-10 20:17:25 -07001647
1648out:
Daniel Veillardc15df7d2012-08-07 15:15:04 +08001649 xmlBufFree(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001650 return(ret);
1651}
1652
1653/**
1654 * xmlNodeListGetString:
1655 * @doc: the document
1656 * @list: a Node list
1657 * @inLine: should we replace entity contents or show their external form
1658 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001659 * Build the string equivalent to the text contained in the Node list
Owen Taylor3473f882001-02-23 17:55:21 +00001660 * made of TEXTs and ENTITY_REFs
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001661 *
Daniel Veillardbd9afb52002-09-25 22:25:35 +00001662 * Returns a pointer to the string copy, the caller must free it with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00001663 */
1664xmlChar *
Daniel Veillard7646b182002-04-20 06:41:40 +00001665xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine)
1666{
Owen Taylor3473f882001-02-23 17:55:21 +00001667 xmlNodePtr node = list;
1668 xmlChar *ret = NULL;
1669 xmlEntityPtr ent;
Daniel Veillard7d4c5292012-09-05 11:45:32 +08001670 int attr;
Owen Taylor3473f882001-02-23 17:55:21 +00001671
Daniel Veillard7646b182002-04-20 06:41:40 +00001672 if (list == NULL)
1673 return (NULL);
Daniel Veillard7d4c5292012-09-05 11:45:32 +08001674 if ((list->parent != NULL) && (list->parent->type == XML_ATTRIBUTE_NODE))
1675 attr = 1;
1676 else
1677 attr = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001678
1679 while (node != NULL) {
1680 if ((node->type == XML_TEXT_NODE) ||
Daniel Veillard7646b182002-04-20 06:41:40 +00001681 (node->type == XML_CDATA_SECTION_NODE)) {
1682 if (inLine) {
1683 ret = xmlStrcat(ret, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001684 } else {
Daniel Veillard7646b182002-04-20 06:41:40 +00001685 xmlChar *buffer;
Owen Taylor3473f882001-02-23 17:55:21 +00001686
Daniel Veillard7d4c5292012-09-05 11:45:32 +08001687 if (attr)
1688 buffer = xmlEncodeAttributeEntities(doc, node->content);
1689 else
1690 buffer = xmlEncodeEntitiesReentrant(doc, node->content);
Daniel Veillard7646b182002-04-20 06:41:40 +00001691 if (buffer != NULL) {
1692 ret = xmlStrcat(ret, buffer);
1693 xmlFree(buffer);
1694 }
1695 }
1696 } else if (node->type == XML_ENTITY_REF_NODE) {
1697 if (inLine) {
1698 ent = xmlGetDocEntity(doc, node->name);
1699 if (ent != NULL) {
1700 xmlChar *buffer;
1701
1702 /* an entity content can be any "well balanced chunk",
1703 * i.e. the result of the content [43] production:
1704 * http://www.w3.org/TR/REC-xml#NT-content.
1705 * So it can contain text, CDATA section or nested
1706 * entity reference nodes (among others).
1707 * -> we recursive call xmlNodeListGetString()
1708 * which handles these types */
1709 buffer = xmlNodeListGetString(doc, ent->children, 1);
1710 if (buffer != NULL) {
1711 ret = xmlStrcat(ret, buffer);
1712 xmlFree(buffer);
1713 }
1714 } else {
1715 ret = xmlStrcat(ret, node->content);
1716 }
1717 } else {
1718 xmlChar buf[2];
1719
1720 buf[0] = '&';
1721 buf[1] = 0;
1722 ret = xmlStrncat(ret, buf, 1);
1723 ret = xmlStrcat(ret, node->name);
1724 buf[0] = ';';
1725 buf[1] = 0;
1726 ret = xmlStrncat(ret, buf, 1);
1727 }
1728 }
1729#if 0
1730 else {
1731 xmlGenericError(xmlGenericErrorContext,
1732 "xmlGetNodeListString : invalid node type %d\n",
1733 node->type);
1734 }
1735#endif
1736 node = node->next;
1737 }
1738 return (ret);
1739}
Daniel Veillard652327a2003-09-29 18:02:38 +00001740
1741#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001742/**
1743 * xmlNodeListGetRawString:
1744 * @doc: the document
1745 * @list: a Node list
1746 * @inLine: should we replace entity contents or show their external form
1747 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001748 * Builds the string equivalent to the text contained in the Node list
Owen Taylor3473f882001-02-23 17:55:21 +00001749 * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString()
1750 * this function doesn't do any character encoding handling.
1751 *
Daniel Veillardbd9afb52002-09-25 22:25:35 +00001752 * Returns a pointer to the string copy, the caller must free it with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00001753 */
1754xmlChar *
Daniel Veillard7646b182002-04-20 06:41:40 +00001755xmlNodeListGetRawString(xmlDocPtr doc, xmlNodePtr list, int inLine)
1756{
Owen Taylor3473f882001-02-23 17:55:21 +00001757 xmlNodePtr node = list;
1758 xmlChar *ret = NULL;
1759 xmlEntityPtr ent;
1760
Daniel Veillard7646b182002-04-20 06:41:40 +00001761 if (list == NULL)
1762 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001763
1764 while (node != NULL) {
Daniel Veillard7db37732001-07-12 01:20:08 +00001765 if ((node->type == XML_TEXT_NODE) ||
Daniel Veillard7646b182002-04-20 06:41:40 +00001766 (node->type == XML_CDATA_SECTION_NODE)) {
1767 if (inLine) {
1768 ret = xmlStrcat(ret, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001769 } else {
Daniel Veillard7646b182002-04-20 06:41:40 +00001770 xmlChar *buffer;
1771
1772 buffer = xmlEncodeSpecialChars(doc, node->content);
1773 if (buffer != NULL) {
1774 ret = xmlStrcat(ret, buffer);
1775 xmlFree(buffer);
1776 }
1777 }
1778 } else if (node->type == XML_ENTITY_REF_NODE) {
1779 if (inLine) {
1780 ent = xmlGetDocEntity(doc, node->name);
1781 if (ent != NULL) {
1782 xmlChar *buffer;
1783
1784 /* an entity content can be any "well balanced chunk",
1785 * i.e. the result of the content [43] production:
1786 * http://www.w3.org/TR/REC-xml#NT-content.
1787 * So it can contain text, CDATA section or nested
1788 * entity reference nodes (among others).
1789 * -> we recursive call xmlNodeListGetRawString()
1790 * which handles these types */
1791 buffer =
1792 xmlNodeListGetRawString(doc, ent->children, 1);
1793 if (buffer != NULL) {
1794 ret = xmlStrcat(ret, buffer);
1795 xmlFree(buffer);
1796 }
1797 } else {
1798 ret = xmlStrcat(ret, node->content);
1799 }
1800 } else {
1801 xmlChar buf[2];
1802
1803 buf[0] = '&';
1804 buf[1] = 0;
1805 ret = xmlStrncat(ret, buf, 1);
1806 ret = xmlStrcat(ret, node->name);
1807 buf[0] = ';';
1808 buf[1] = 0;
1809 ret = xmlStrncat(ret, buf, 1);
1810 }
1811 }
Owen Taylor3473f882001-02-23 17:55:21 +00001812#if 0
Daniel Veillard7646b182002-04-20 06:41:40 +00001813 else {
1814 xmlGenericError(xmlGenericErrorContext,
1815 "xmlGetNodeListString : invalid node type %d\n",
1816 node->type);
1817 }
Owen Taylor3473f882001-02-23 17:55:21 +00001818#endif
Daniel Veillard7646b182002-04-20 06:41:40 +00001819 node = node->next;
Owen Taylor3473f882001-02-23 17:55:21 +00001820 }
Daniel Veillard7646b182002-04-20 06:41:40 +00001821 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00001822}
Daniel Veillard652327a2003-09-29 18:02:38 +00001823#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001824
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001825static xmlAttrPtr
1826xmlNewPropInternal(xmlNodePtr node, xmlNsPtr ns,
1827 const xmlChar * name, const xmlChar * value,
1828 int eatname)
1829{
Owen Taylor3473f882001-02-23 17:55:21 +00001830 xmlAttrPtr cur;
1831 xmlDocPtr doc = NULL;
1832
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001833 if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) {
Daniel Veillard8f6c2b12008-04-03 11:17:21 +00001834 if ((eatname == 1) &&
1835 ((node->doc == NULL) ||
Daniel Veillarded939f82008-04-08 08:20:08 +00001836 (!(xmlDictOwns(node->doc->dict, name)))))
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001837 xmlFree((xmlChar *) name);
1838 return (NULL);
1839 }
Owen Taylor3473f882001-02-23 17:55:21 +00001840
1841 /*
1842 * Allocate a new property and fill the fields.
1843 */
1844 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1845 if (cur == NULL) {
Daniel Veillard8f6c2b12008-04-03 11:17:21 +00001846 if ((eatname == 1) &&
Daniel Veillard76d36452009-09-07 11:19:33 +02001847 ((node == NULL) || (node->doc == NULL) ||
Daniel Veillarded939f82008-04-08 08:20:08 +00001848 (!(xmlDictOwns(node->doc->dict, name)))))
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001849 xmlFree((xmlChar *) name);
1850 xmlTreeErrMemory("building attribute");
1851 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001852 }
1853 memset(cur, 0, sizeof(xmlAttr));
1854 cur->type = XML_ATTRIBUTE_NODE;
1855
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001856 cur->parent = node;
Owen Taylor3473f882001-02-23 17:55:21 +00001857 if (node != NULL) {
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001858 doc = node->doc;
1859 cur->doc = doc;
Owen Taylor3473f882001-02-23 17:55:21 +00001860 }
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001861 cur->ns = ns;
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001862
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001863 if (eatname == 0) {
1864 if ((doc != NULL) && (doc->dict != NULL))
1865 cur->name = (xmlChar *) xmlDictLookup(doc->dict, name, -1);
1866 else
1867 cur->name = xmlStrdup(name);
1868 } else
1869 cur->name = name;
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001870
Owen Taylor3473f882001-02-23 17:55:21 +00001871 if (value != NULL) {
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001872 xmlNodePtr tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00001873
Daniel Veillard6f8611f2008-02-15 08:33:21 +00001874 if(!xmlCheckUTF8(value)) {
1875 xmlTreeErr(XML_TREE_NOT_UTF8, (xmlNodePtr) doc,
1876 NULL);
1877 if (doc != NULL)
1878 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
1879 }
1880 cur->children = xmlNewDocText(doc, value);
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001881 cur->last = NULL;
1882 tmp = cur->children;
1883 while (tmp != NULL) {
1884 tmp->parent = (xmlNodePtr) cur;
1885 if (tmp->next == NULL)
1886 cur->last = tmp;
1887 tmp = tmp->next;
1888 }
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001889 }
Owen Taylor3473f882001-02-23 17:55:21 +00001890
1891 /*
1892 * Add it at the end to preserve parsing order ...
1893 */
1894 if (node != NULL) {
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001895 if (node->properties == NULL) {
1896 node->properties = cur;
1897 } else {
1898 xmlAttrPtr prev = node->properties;
Owen Taylor3473f882001-02-23 17:55:21 +00001899
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001900 while (prev->next != NULL)
1901 prev = prev->next;
1902 prev->next = cur;
1903 cur->prev = prev;
1904 }
Owen Taylor3473f882001-02-23 17:55:21 +00001905 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00001906
Daniel Veillard76d36452009-09-07 11:19:33 +02001907 if ((value != NULL) && (node != NULL) &&
1908 (xmlIsID(node->doc, node, cur) == 1))
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001909 xmlAddID(NULL, node->doc, value, cur);
1910
Daniel Veillarda880b122003-04-21 21:36:41 +00001911 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001912 xmlRegisterNodeDefaultValue((xmlNodePtr) cur);
1913 return (cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001914}
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001915
1916#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
1917 defined(LIBXML_SCHEMAS_ENABLED)
1918/**
1919 * xmlNewProp:
1920 * @node: the holding node
1921 * @name: the name of the attribute
1922 * @value: the value of the attribute
1923 *
1924 * Create a new property carried by a node.
1925 * Returns a pointer to the attribute
1926 */
1927xmlAttrPtr
1928xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
1929
1930 if (name == NULL) {
1931#ifdef DEBUG_TREE
1932 xmlGenericError(xmlGenericErrorContext,
1933 "xmlNewProp : name == NULL\n");
1934#endif
1935 return(NULL);
1936 }
1937
1938 return xmlNewPropInternal(node, NULL, name, value, 0);
1939}
Daniel Veillard652327a2003-09-29 18:02:38 +00001940#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001941
1942/**
1943 * xmlNewNsProp:
1944 * @node: the holding node
1945 * @ns: the namespace
1946 * @name: the name of the attribute
1947 * @value: the value of the attribute
1948 *
1949 * Create a new property tagged with a namespace and carried by a node.
1950 * Returns a pointer to the attribute
1951 */
1952xmlAttrPtr
1953xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
1954 const xmlChar *value) {
Owen Taylor3473f882001-02-23 17:55:21 +00001955
1956 if (name == NULL) {
1957#ifdef DEBUG_TREE
1958 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00001959 "xmlNewNsProp : name == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001960#endif
1961 return(NULL);
1962 }
1963
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001964 return xmlNewPropInternal(node, ns, name, value, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001965}
1966
1967/**
Daniel Veillard46de64e2002-05-29 08:21:33 +00001968 * xmlNewNsPropEatName:
1969 * @node: the holding node
1970 * @ns: the namespace
1971 * @name: the name of the attribute
1972 * @value: the value of the attribute
1973 *
1974 * Create a new property tagged with a namespace and carried by a node.
1975 * Returns a pointer to the attribute
1976 */
1977xmlAttrPtr
1978xmlNewNsPropEatName(xmlNodePtr node, xmlNsPtr ns, xmlChar *name,
1979 const xmlChar *value) {
Daniel Veillard46de64e2002-05-29 08:21:33 +00001980
1981 if (name == NULL) {
1982#ifdef DEBUG_TREE
1983 xmlGenericError(xmlGenericErrorContext,
1984 "xmlNewNsPropEatName : name == NULL\n");
1985#endif
1986 return(NULL);
1987 }
1988
Daniel Veillard8f6c2b12008-04-03 11:17:21 +00001989 return xmlNewPropInternal(node, ns, name, value, 1);
Daniel Veillard46de64e2002-05-29 08:21:33 +00001990}
1991
1992/**
Owen Taylor3473f882001-02-23 17:55:21 +00001993 * xmlNewDocProp:
1994 * @doc: the document
1995 * @name: the name of the attribute
1996 * @value: the value of the attribute
1997 *
1998 * Create a new property carried by a document.
1999 * Returns a pointer to the attribute
2000 */
2001xmlAttrPtr
2002xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) {
2003 xmlAttrPtr cur;
2004
2005 if (name == NULL) {
2006#ifdef DEBUG_TREE
2007 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00002008 "xmlNewDocProp : name == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002009#endif
2010 return(NULL);
2011 }
2012
2013 /*
2014 * Allocate a new property and fill the fields.
2015 */
2016 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
2017 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002018 xmlTreeErrMemory("building attribute");
Owen Taylor3473f882001-02-23 17:55:21 +00002019 return(NULL);
2020 }
2021 memset(cur, 0, sizeof(xmlAttr));
2022 cur->type = XML_ATTRIBUTE_NODE;
2023
Daniel Veillard03a53c32004-10-26 16:06:51 +00002024 if ((doc != NULL) && (doc->dict != NULL))
2025 cur->name = xmlDictLookup(doc->dict, name, -1);
2026 else
2027 cur->name = xmlStrdup(name);
Daniel Veillardaa6de472008-08-25 14:53:31 +00002028 cur->doc = doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002029 if (value != NULL) {
2030 xmlNodePtr tmp;
2031
2032 cur->children = xmlStringGetNodeList(doc, value);
2033 cur->last = NULL;
2034
2035 tmp = cur->children;
2036 while (tmp != NULL) {
2037 tmp->parent = (xmlNodePtr) cur;
2038 if (tmp->next == NULL)
2039 cur->last = tmp;
2040 tmp = tmp->next;
2041 }
2042 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002043
Daniel Veillarda880b122003-04-21 21:36:41 +00002044 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002045 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002046 return(cur);
2047}
2048
2049/**
2050 * xmlFreePropList:
2051 * @cur: the first property in the list
2052 *
2053 * Free a property and all its siblings, all the children are freed too.
2054 */
2055void
2056xmlFreePropList(xmlAttrPtr cur) {
2057 xmlAttrPtr next;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002058 if (cur == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +00002059 while (cur != NULL) {
2060 next = cur->next;
2061 xmlFreeProp(cur);
2062 cur = next;
2063 }
2064}
2065
2066/**
2067 * xmlFreeProp:
2068 * @cur: an attribute
2069 *
2070 * Free one attribute, all the content is freed too
2071 */
2072void
2073xmlFreeProp(xmlAttrPtr cur) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002074 xmlDictPtr dict = NULL;
2075 if (cur == NULL) return;
2076
2077 if (cur->doc != NULL) dict = cur->doc->dict;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002078
Daniel Veillarda880b122003-04-21 21:36:41 +00002079 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002080 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
2081
Owen Taylor3473f882001-02-23 17:55:21 +00002082 /* Check for ID removal -> leading to invalid references ! */
Daniel Veillardda6f4af2005-06-20 17:17:54 +00002083 if ((cur->doc != NULL) && (cur->atype == XML_ATTRIBUTE_ID)) {
2084 xmlRemoveID(cur->doc, cur);
Daniel Veillard76d66f42001-05-16 21:05:17 +00002085 }
Owen Taylor3473f882001-02-23 17:55:21 +00002086 if (cur->children != NULL) xmlFreeNodeList(cur->children);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002087 DICT_FREE(cur->name)
Owen Taylor3473f882001-02-23 17:55:21 +00002088 xmlFree(cur);
2089}
2090
2091/**
2092 * xmlRemoveProp:
2093 * @cur: an attribute
2094 *
2095 * Unlink and free one attribute, all the content is freed too
2096 * Note this doesn't work for namespace definition attributes
2097 *
2098 * Returns 0 if success and -1 in case of error.
2099 */
2100int
2101xmlRemoveProp(xmlAttrPtr cur) {
2102 xmlAttrPtr tmp;
2103 if (cur == NULL) {
2104#ifdef DEBUG_TREE
2105 xmlGenericError(xmlGenericErrorContext,
2106 "xmlRemoveProp : cur == NULL\n");
2107#endif
2108 return(-1);
2109 }
2110 if (cur->parent == NULL) {
2111#ifdef DEBUG_TREE
2112 xmlGenericError(xmlGenericErrorContext,
2113 "xmlRemoveProp : cur->parent == NULL\n");
2114#endif
2115 return(-1);
2116 }
2117 tmp = cur->parent->properties;
2118 if (tmp == cur) {
2119 cur->parent->properties = cur->next;
Rob Richards19dc9612005-10-28 16:15:16 +00002120 if (cur->next != NULL)
2121 cur->next->prev = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002122 xmlFreeProp(cur);
2123 return(0);
2124 }
2125 while (tmp != NULL) {
2126 if (tmp->next == cur) {
2127 tmp->next = cur->next;
2128 if (tmp->next != NULL)
2129 tmp->next->prev = tmp;
2130 xmlFreeProp(cur);
2131 return(0);
2132 }
2133 tmp = tmp->next;
2134 }
2135#ifdef DEBUG_TREE
2136 xmlGenericError(xmlGenericErrorContext,
2137 "xmlRemoveProp : attribute not owned by its node\n");
2138#endif
2139 return(-1);
2140}
2141
2142/**
Daniel Veillard03a53c32004-10-26 16:06:51 +00002143 * xmlNewDocPI:
2144 * @doc: the target document
Owen Taylor3473f882001-02-23 17:55:21 +00002145 * @name: the processing instruction name
2146 * @content: the PI content
2147 *
2148 * Creation of a processing instruction element.
2149 * Returns a pointer to the new node object.
2150 */
2151xmlNodePtr
Daniel Veillard03a53c32004-10-26 16:06:51 +00002152xmlNewDocPI(xmlDocPtr doc, const xmlChar *name, const xmlChar *content) {
Owen Taylor3473f882001-02-23 17:55:21 +00002153 xmlNodePtr cur;
2154
2155 if (name == NULL) {
2156#ifdef DEBUG_TREE
2157 xmlGenericError(xmlGenericErrorContext,
2158 "xmlNewPI : name == NULL\n");
2159#endif
2160 return(NULL);
2161 }
2162
2163 /*
2164 * Allocate a new node and fill the fields.
2165 */
2166 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2167 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002168 xmlTreeErrMemory("building PI");
Owen Taylor3473f882001-02-23 17:55:21 +00002169 return(NULL);
2170 }
2171 memset(cur, 0, sizeof(xmlNode));
2172 cur->type = XML_PI_NODE;
2173
Daniel Veillard03a53c32004-10-26 16:06:51 +00002174 if ((doc != NULL) && (doc->dict != NULL))
2175 cur->name = xmlDictLookup(doc->dict, name, -1);
2176 else
2177 cur->name = xmlStrdup(name);
Owen Taylor3473f882001-02-23 17:55:21 +00002178 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002179 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00002180 }
Daniel Veillarda03e3652004-11-02 18:45:30 +00002181 cur->doc = doc;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002182
Daniel Veillarda880b122003-04-21 21:36:41 +00002183 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002184 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002185 return(cur);
2186}
2187
2188/**
Daniel Veillard03a53c32004-10-26 16:06:51 +00002189 * xmlNewPI:
2190 * @name: the processing instruction name
2191 * @content: the PI content
2192 *
2193 * Creation of a processing instruction element.
2194 * Use xmlDocNewPI preferably to get string interning
2195 *
2196 * Returns a pointer to the new node object.
2197 */
2198xmlNodePtr
2199xmlNewPI(const xmlChar *name, const xmlChar *content) {
2200 return(xmlNewDocPI(NULL, name, content));
2201}
2202
2203/**
Owen Taylor3473f882001-02-23 17:55:21 +00002204 * xmlNewNode:
2205 * @ns: namespace if any
2206 * @name: the node name
2207 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002208 * Creation of a new node element. @ns is optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00002209 *
William M. Brackd7cf7f82003-11-14 07:13:16 +00002210 * Returns a pointer to the new node object. Uses xmlStrdup() to make
2211 * copy of @name.
Owen Taylor3473f882001-02-23 17:55:21 +00002212 */
2213xmlNodePtr
2214xmlNewNode(xmlNsPtr ns, const xmlChar *name) {
2215 xmlNodePtr cur;
2216
2217 if (name == NULL) {
2218#ifdef DEBUG_TREE
2219 xmlGenericError(xmlGenericErrorContext,
2220 "xmlNewNode : name == NULL\n");
2221#endif
2222 return(NULL);
2223 }
2224
2225 /*
2226 * Allocate a new node and fill the fields.
2227 */
2228 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2229 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002230 xmlTreeErrMemory("building node");
Owen Taylor3473f882001-02-23 17:55:21 +00002231 return(NULL);
2232 }
2233 memset(cur, 0, sizeof(xmlNode));
2234 cur->type = XML_ELEMENT_NODE;
Daniel Veillardaa6de472008-08-25 14:53:31 +00002235
Owen Taylor3473f882001-02-23 17:55:21 +00002236 cur->name = xmlStrdup(name);
2237 cur->ns = ns;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002238
Daniel Veillarda880b122003-04-21 21:36:41 +00002239 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002240 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002241 return(cur);
2242}
2243
2244/**
Daniel Veillard46de64e2002-05-29 08:21:33 +00002245 * xmlNewNodeEatName:
2246 * @ns: namespace if any
2247 * @name: the node name
2248 *
2249 * Creation of a new node element. @ns is optional (NULL).
2250 *
William M. Brackd7cf7f82003-11-14 07:13:16 +00002251 * Returns a pointer to the new node object, with pointer @name as
2252 * new node's name. Use xmlNewNode() if a copy of @name string is
2253 * is needed as new node's name.
Daniel Veillard46de64e2002-05-29 08:21:33 +00002254 */
2255xmlNodePtr
2256xmlNewNodeEatName(xmlNsPtr ns, xmlChar *name) {
2257 xmlNodePtr cur;
2258
2259 if (name == NULL) {
2260#ifdef DEBUG_TREE
2261 xmlGenericError(xmlGenericErrorContext,
2262 "xmlNewNode : name == NULL\n");
2263#endif
2264 return(NULL);
2265 }
2266
2267 /*
2268 * Allocate a new node and fill the fields.
2269 */
2270 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2271 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002272 xmlTreeErrMemory("building node");
Daniel Veillard8f6c2b12008-04-03 11:17:21 +00002273 /* we can't check here that name comes from the doc dictionnary */
Daniel Veillard46de64e2002-05-29 08:21:33 +00002274 return(NULL);
2275 }
2276 memset(cur, 0, sizeof(xmlNode));
2277 cur->type = XML_ELEMENT_NODE;
Daniel Veillardaa6de472008-08-25 14:53:31 +00002278
Daniel Veillard46de64e2002-05-29 08:21:33 +00002279 cur->name = name;
2280 cur->ns = ns;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002281
Daniel Veillarda880b122003-04-21 21:36:41 +00002282 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002283 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Daniel Veillard46de64e2002-05-29 08:21:33 +00002284 return(cur);
2285}
2286
2287/**
Owen Taylor3473f882001-02-23 17:55:21 +00002288 * xmlNewDocNode:
2289 * @doc: the document
2290 * @ns: namespace if any
2291 * @name: the node name
2292 * @content: the XML text content if any
2293 *
2294 * Creation of a new node element within a document. @ns and @content
Daniel Veillardd1640922001-12-17 15:30:10 +00002295 * are optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00002296 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2297 * references, but XML special chars need to be escaped first by using
2298 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2299 * need entities support.
2300 *
2301 * Returns a pointer to the new node object.
2302 */
2303xmlNodePtr
2304xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns,
2305 const xmlChar *name, const xmlChar *content) {
2306 xmlNodePtr cur;
2307
Daniel Veillard95ddcd32004-10-26 21:53:55 +00002308 if ((doc != NULL) && (doc->dict != NULL))
Daniel Veillard03a53c32004-10-26 16:06:51 +00002309 cur = xmlNewNodeEatName(ns, (xmlChar *)
2310 xmlDictLookup(doc->dict, name, -1));
2311 else
2312 cur = xmlNewNode(ns, name);
Owen Taylor3473f882001-02-23 17:55:21 +00002313 if (cur != NULL) {
2314 cur->doc = doc;
2315 if (content != NULL) {
2316 cur->children = xmlStringGetNodeList(doc, content);
2317 UPDATE_LAST_CHILD_AND_PARENT(cur)
2318 }
2319 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002320
Owen Taylor3473f882001-02-23 17:55:21 +00002321 return(cur);
2322}
2323
Daniel Veillard46de64e2002-05-29 08:21:33 +00002324/**
2325 * xmlNewDocNodeEatName:
2326 * @doc: the document
2327 * @ns: namespace if any
2328 * @name: the node name
2329 * @content: the XML text content if any
2330 *
2331 * Creation of a new node element within a document. @ns and @content
2332 * are optional (NULL).
2333 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2334 * references, but XML special chars need to be escaped first by using
2335 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2336 * need entities support.
2337 *
2338 * Returns a pointer to the new node object.
2339 */
2340xmlNodePtr
2341xmlNewDocNodeEatName(xmlDocPtr doc, xmlNsPtr ns,
2342 xmlChar *name, const xmlChar *content) {
2343 xmlNodePtr cur;
2344
2345 cur = xmlNewNodeEatName(ns, name);
2346 if (cur != NULL) {
2347 cur->doc = doc;
2348 if (content != NULL) {
2349 cur->children = xmlStringGetNodeList(doc, content);
2350 UPDATE_LAST_CHILD_AND_PARENT(cur)
2351 }
Daniel Veillard8f6c2b12008-04-03 11:17:21 +00002352 } else {
2353 /* if name don't come from the doc dictionnary free it here */
2354 if ((name != NULL) && (doc != NULL) &&
2355 (!(xmlDictOwns(doc->dict, name))))
2356 xmlFree(name);
Daniel Veillard46de64e2002-05-29 08:21:33 +00002357 }
2358 return(cur);
2359}
2360
Daniel Veillard652327a2003-09-29 18:02:38 +00002361#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002362/**
2363 * xmlNewDocRawNode:
2364 * @doc: the document
2365 * @ns: namespace if any
2366 * @name: the node name
2367 * @content: the text content if any
2368 *
2369 * Creation of a new node element within a document. @ns and @content
Daniel Veillardd1640922001-12-17 15:30:10 +00002370 * are optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00002371 *
2372 * Returns a pointer to the new node object.
2373 */
2374xmlNodePtr
2375xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns,
2376 const xmlChar *name, const xmlChar *content) {
2377 xmlNodePtr cur;
2378
Daniel Veillard95ddcd32004-10-26 21:53:55 +00002379 cur = xmlNewDocNode(doc, ns, name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002380 if (cur != NULL) {
2381 cur->doc = doc;
2382 if (content != NULL) {
2383 cur->children = xmlNewDocText(doc, content);
2384 UPDATE_LAST_CHILD_AND_PARENT(cur)
2385 }
2386 }
2387 return(cur);
2388}
2389
2390/**
2391 * xmlNewDocFragment:
2392 * @doc: the document owning the fragment
2393 *
2394 * Creation of a new Fragment node.
2395 * Returns a pointer to the new node object.
2396 */
2397xmlNodePtr
2398xmlNewDocFragment(xmlDocPtr doc) {
2399 xmlNodePtr cur;
2400
2401 /*
2402 * Allocate a new DocumentFragment node and fill the fields.
2403 */
2404 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2405 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002406 xmlTreeErrMemory("building fragment");
Owen Taylor3473f882001-02-23 17:55:21 +00002407 return(NULL);
2408 }
2409 memset(cur, 0, sizeof(xmlNode));
2410 cur->type = XML_DOCUMENT_FRAG_NODE;
2411
2412 cur->doc = doc;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002413
Daniel Veillarda880b122003-04-21 21:36:41 +00002414 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002415 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002416 return(cur);
2417}
Daniel Veillard652327a2003-09-29 18:02:38 +00002418#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002419
2420/**
2421 * xmlNewText:
2422 * @content: the text content
2423 *
2424 * Creation of a new text node.
2425 * Returns a pointer to the new node object.
2426 */
2427xmlNodePtr
2428xmlNewText(const xmlChar *content) {
2429 xmlNodePtr cur;
2430
2431 /*
2432 * Allocate a new node and fill the fields.
2433 */
2434 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2435 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002436 xmlTreeErrMemory("building text");
Owen Taylor3473f882001-02-23 17:55:21 +00002437 return(NULL);
2438 }
2439 memset(cur, 0, sizeof(xmlNode));
2440 cur->type = XML_TEXT_NODE;
2441
2442 cur->name = xmlStringText;
2443 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002444 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00002445 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002446
Daniel Veillarda880b122003-04-21 21:36:41 +00002447 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002448 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002449 return(cur);
2450}
2451
Daniel Veillard652327a2003-09-29 18:02:38 +00002452#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002453/**
2454 * xmlNewTextChild:
2455 * @parent: the parent node
2456 * @ns: a namespace if any
2457 * @name: the name of the child
2458 * @content: the text content of the child if any.
2459 *
2460 * Creation of a new child element, added at the end of @parent children list.
MST 2003 John Flecke1f70492003-12-20 23:43:28 +00002461 * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
2462 * created element inherits the namespace of @parent. If @content is non NULL,
William M. Brackd7cf7f82003-11-14 07:13:16 +00002463 * a child TEXT node will be created containing the string @content.
MST 2003 John Fleck8b03bc52003-12-17 03:45:01 +00002464 * NOTE: Use xmlNewChild() if @content will contain entities that need to be
2465 * preserved. Use this function, xmlNewTextChild(), if you need to ensure that
Daniel Veillardaa6de472008-08-25 14:53:31 +00002466 * reserved XML chars that might appear in @content, such as the ampersand,
2467 * greater-than or less-than signs, are automatically replaced by their XML
2468 * escaped entity representations.
Owen Taylor3473f882001-02-23 17:55:21 +00002469 *
2470 * Returns a pointer to the new node object.
2471 */
2472xmlNodePtr
2473xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns,
2474 const xmlChar *name, const xmlChar *content) {
2475 xmlNodePtr cur, prev;
2476
2477 if (parent == NULL) {
2478#ifdef DEBUG_TREE
2479 xmlGenericError(xmlGenericErrorContext,
2480 "xmlNewTextChild : parent == NULL\n");
2481#endif
2482 return(NULL);
2483 }
2484
2485 if (name == NULL) {
2486#ifdef DEBUG_TREE
2487 xmlGenericError(xmlGenericErrorContext,
2488 "xmlNewTextChild : name == NULL\n");
2489#endif
2490 return(NULL);
2491 }
2492
2493 /*
2494 * Allocate a new node
2495 */
Daniel Veillard254b1262003-11-01 17:04:58 +00002496 if (parent->type == XML_ELEMENT_NODE) {
2497 if (ns == NULL)
2498 cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content);
2499 else
2500 cur = xmlNewDocRawNode(parent->doc, ns, name, content);
2501 } else if ((parent->type == XML_DOCUMENT_NODE) ||
2502 (parent->type == XML_HTML_DOCUMENT_NODE)) {
2503 if (ns == NULL)
2504 cur = xmlNewDocRawNode((xmlDocPtr) parent, NULL, name, content);
2505 else
2506 cur = xmlNewDocRawNode((xmlDocPtr) parent, ns, name, content);
2507 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2508 cur = xmlNewDocRawNode( parent->doc, ns, name, content);
2509 } else {
2510 return(NULL);
2511 }
Owen Taylor3473f882001-02-23 17:55:21 +00002512 if (cur == NULL) return(NULL);
2513
2514 /*
2515 * add the new element at the end of the children list.
2516 */
2517 cur->type = XML_ELEMENT_NODE;
2518 cur->parent = parent;
2519 cur->doc = parent->doc;
2520 if (parent->children == NULL) {
2521 parent->children = cur;
2522 parent->last = cur;
2523 } else {
2524 prev = parent->last;
2525 prev->next = cur;
2526 cur->prev = prev;
2527 parent->last = cur;
2528 }
2529
2530 return(cur);
2531}
Daniel Veillard652327a2003-09-29 18:02:38 +00002532#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002533
2534/**
2535 * xmlNewCharRef:
2536 * @doc: the document
2537 * @name: the char ref string, starting with # or "&# ... ;"
2538 *
2539 * Creation of a new character reference node.
2540 * Returns a pointer to the new node object.
2541 */
2542xmlNodePtr
2543xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) {
2544 xmlNodePtr cur;
2545
Daniel Veillard36e5cd52004-11-02 14:52:23 +00002546 if (name == NULL)
2547 return(NULL);
2548
Owen Taylor3473f882001-02-23 17:55:21 +00002549 /*
2550 * Allocate a new node and fill the fields.
2551 */
2552 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2553 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002554 xmlTreeErrMemory("building character reference");
Owen Taylor3473f882001-02-23 17:55:21 +00002555 return(NULL);
2556 }
2557 memset(cur, 0, sizeof(xmlNode));
2558 cur->type = XML_ENTITY_REF_NODE;
2559
2560 cur->doc = doc;
2561 if (name[0] == '&') {
2562 int len;
2563 name++;
2564 len = xmlStrlen(name);
2565 if (name[len - 1] == ';')
2566 cur->name = xmlStrndup(name, len - 1);
2567 else
2568 cur->name = xmlStrndup(name, len);
2569 } else
2570 cur->name = xmlStrdup(name);
Daniel Veillard5335dc52003-01-01 20:59:38 +00002571
Daniel Veillarda880b122003-04-21 21:36:41 +00002572 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002573 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002574 return(cur);
2575}
2576
2577/**
2578 * xmlNewReference:
2579 * @doc: the document
2580 * @name: the reference name, or the reference string with & and ;
2581 *
2582 * Creation of a new reference node.
2583 * Returns a pointer to the new node object.
2584 */
2585xmlNodePtr
2586xmlNewReference(xmlDocPtr doc, const xmlChar *name) {
2587 xmlNodePtr cur;
2588 xmlEntityPtr ent;
2589
Daniel Veillard36e5cd52004-11-02 14:52:23 +00002590 if (name == NULL)
2591 return(NULL);
2592
Owen Taylor3473f882001-02-23 17:55:21 +00002593 /*
2594 * Allocate a new node and fill the fields.
2595 */
2596 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2597 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002598 xmlTreeErrMemory("building reference");
Owen Taylor3473f882001-02-23 17:55:21 +00002599 return(NULL);
2600 }
2601 memset(cur, 0, sizeof(xmlNode));
2602 cur->type = XML_ENTITY_REF_NODE;
2603
2604 cur->doc = doc;
2605 if (name[0] == '&') {
2606 int len;
2607 name++;
2608 len = xmlStrlen(name);
2609 if (name[len - 1] == ';')
2610 cur->name = xmlStrndup(name, len - 1);
2611 else
2612 cur->name = xmlStrndup(name, len);
2613 } else
2614 cur->name = xmlStrdup(name);
2615
2616 ent = xmlGetDocEntity(doc, cur->name);
2617 if (ent != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002618 cur->content = ent->content;
Owen Taylor3473f882001-02-23 17:55:21 +00002619 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002620 * The parent pointer in entity is a DTD pointer and thus is NOT
Owen Taylor3473f882001-02-23 17:55:21 +00002621 * updated. Not sure if this is 100% correct.
2622 * -George
2623 */
2624 cur->children = (xmlNodePtr) ent;
2625 cur->last = (xmlNodePtr) ent;
2626 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002627
Daniel Veillarda880b122003-04-21 21:36:41 +00002628 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002629 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002630 return(cur);
2631}
2632
2633/**
2634 * xmlNewDocText:
2635 * @doc: the document
2636 * @content: the text content
2637 *
2638 * Creation of a new text node within a document.
2639 * Returns a pointer to the new node object.
2640 */
2641xmlNodePtr
2642xmlNewDocText(xmlDocPtr doc, const xmlChar *content) {
2643 xmlNodePtr cur;
2644
2645 cur = xmlNewText(content);
2646 if (cur != NULL) cur->doc = doc;
2647 return(cur);
2648}
2649
2650/**
2651 * xmlNewTextLen:
2652 * @content: the text content
2653 * @len: the text len.
2654 *
Daniel Veillard60087f32001-10-10 09:45:09 +00002655 * Creation of a new text node with an extra parameter for the content's length
Owen Taylor3473f882001-02-23 17:55:21 +00002656 * Returns a pointer to the new node object.
2657 */
2658xmlNodePtr
2659xmlNewTextLen(const xmlChar *content, int len) {
2660 xmlNodePtr cur;
2661
2662 /*
2663 * Allocate a new node and fill the fields.
2664 */
2665 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2666 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002667 xmlTreeErrMemory("building text");
Owen Taylor3473f882001-02-23 17:55:21 +00002668 return(NULL);
2669 }
2670 memset(cur, 0, sizeof(xmlNode));
2671 cur->type = XML_TEXT_NODE;
2672
2673 cur->name = xmlStringText;
2674 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002675 cur->content = xmlStrndup(content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00002676 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002677
Daniel Veillarda880b122003-04-21 21:36:41 +00002678 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002679 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002680 return(cur);
2681}
2682
2683/**
2684 * xmlNewDocTextLen:
2685 * @doc: the document
2686 * @content: the text content
2687 * @len: the text len.
2688 *
Daniel Veillard60087f32001-10-10 09:45:09 +00002689 * Creation of a new text node with an extra content length parameter. The
Owen Taylor3473f882001-02-23 17:55:21 +00002690 * text node pertain to a given document.
2691 * Returns a pointer to the new node object.
2692 */
2693xmlNodePtr
2694xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) {
2695 xmlNodePtr cur;
2696
2697 cur = xmlNewTextLen(content, len);
2698 if (cur != NULL) cur->doc = doc;
2699 return(cur);
2700}
2701
2702/**
2703 * xmlNewComment:
2704 * @content: the comment content
2705 *
2706 * Creation of a new node containing a comment.
2707 * Returns a pointer to the new node object.
2708 */
2709xmlNodePtr
2710xmlNewComment(const xmlChar *content) {
2711 xmlNodePtr cur;
2712
2713 /*
2714 * Allocate a new node and fill the fields.
2715 */
2716 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2717 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002718 xmlTreeErrMemory("building comment");
Owen Taylor3473f882001-02-23 17:55:21 +00002719 return(NULL);
2720 }
2721 memset(cur, 0, sizeof(xmlNode));
2722 cur->type = XML_COMMENT_NODE;
2723
2724 cur->name = xmlStringComment;
2725 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002726 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00002727 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002728
Daniel Veillarda880b122003-04-21 21:36:41 +00002729 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002730 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002731 return(cur);
2732}
2733
2734/**
2735 * xmlNewCDataBlock:
2736 * @doc: the document
Daniel Veillardd1640922001-12-17 15:30:10 +00002737 * @content: the CDATA block content content
Owen Taylor3473f882001-02-23 17:55:21 +00002738 * @len: the length of the block
2739 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002740 * Creation of a new node containing a CDATA block.
Owen Taylor3473f882001-02-23 17:55:21 +00002741 * Returns a pointer to the new node object.
2742 */
2743xmlNodePtr
2744xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) {
2745 xmlNodePtr cur;
2746
2747 /*
2748 * Allocate a new node and fill the fields.
2749 */
2750 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2751 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002752 xmlTreeErrMemory("building CDATA");
Owen Taylor3473f882001-02-23 17:55:21 +00002753 return(NULL);
2754 }
2755 memset(cur, 0, sizeof(xmlNode));
2756 cur->type = XML_CDATA_SECTION_NODE;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002757 cur->doc = doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002758
2759 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002760 cur->content = xmlStrndup(content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00002761 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002762
Daniel Veillarda880b122003-04-21 21:36:41 +00002763 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002764 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002765 return(cur);
2766}
2767
2768/**
2769 * xmlNewDocComment:
2770 * @doc: the document
2771 * @content: the comment content
2772 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002773 * Creation of a new node containing a comment within a document.
Owen Taylor3473f882001-02-23 17:55:21 +00002774 * Returns a pointer to the new node object.
2775 */
2776xmlNodePtr
2777xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) {
2778 xmlNodePtr cur;
2779
2780 cur = xmlNewComment(content);
2781 if (cur != NULL) cur->doc = doc;
2782 return(cur);
2783}
2784
2785/**
2786 * xmlSetTreeDoc:
2787 * @tree: the top element
2788 * @doc: the document
2789 *
2790 * update all nodes under the tree to point to the right document
2791 */
2792void
2793xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) {
Daniel Veillard19e96c32001-07-09 10:32:59 +00002794 xmlAttrPtr prop;
2795
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002796 if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +00002797 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002798 if (tree->doc != doc) {
Daniel Veillard36065812002-01-24 15:02:46 +00002799 if(tree->type == XML_ELEMENT_NODE) {
2800 prop = tree->properties;
2801 while (prop != NULL) {
2802 prop->doc = doc;
2803 xmlSetListDoc(prop->children, doc);
2804 prop = prop->next;
2805 }
Daniel Veillard19e96c32001-07-09 10:32:59 +00002806 }
Owen Taylor3473f882001-02-23 17:55:21 +00002807 if (tree->children != NULL)
2808 xmlSetListDoc(tree->children, doc);
2809 tree->doc = doc;
2810 }
2811}
2812
2813/**
2814 * xmlSetListDoc:
Daniel Veillardd1640922001-12-17 15:30:10 +00002815 * @list: the first element
Owen Taylor3473f882001-02-23 17:55:21 +00002816 * @doc: the document
2817 *
2818 * update all nodes in the list to point to the right document
2819 */
2820void
2821xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) {
2822 xmlNodePtr cur;
2823
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002824 if ((list == NULL) || (list->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +00002825 return;
2826 cur = list;
2827 while (cur != NULL) {
2828 if (cur->doc != doc)
2829 xmlSetTreeDoc(cur, doc);
2830 cur = cur->next;
2831 }
2832}
2833
Daniel Veillard2156d432004-03-04 15:59:36 +00002834#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00002835/**
2836 * xmlNewChild:
2837 * @parent: the parent node
2838 * @ns: a namespace if any
2839 * @name: the name of the child
2840 * @content: the XML content of the child if any.
2841 *
2842 * Creation of a new child element, added at the end of @parent children list.
MST 2003 John Flecke1f70492003-12-20 23:43:28 +00002843 * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
2844 * created element inherits the namespace of @parent. If @content is non NULL,
Owen Taylor3473f882001-02-23 17:55:21 +00002845 * a child list containing the TEXTs and ENTITY_REFs node will be created.
MST 2003 John Fleck8b03bc52003-12-17 03:45:01 +00002846 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
2847 * references. XML special chars must be escaped first by using
2848 * xmlEncodeEntitiesReentrant(), or xmlNewTextChild() should be used.
Owen Taylor3473f882001-02-23 17:55:21 +00002849 *
2850 * Returns a pointer to the new node object.
2851 */
2852xmlNodePtr
2853xmlNewChild(xmlNodePtr parent, xmlNsPtr ns,
2854 const xmlChar *name, const xmlChar *content) {
2855 xmlNodePtr cur, prev;
2856
2857 if (parent == NULL) {
2858#ifdef DEBUG_TREE
2859 xmlGenericError(xmlGenericErrorContext,
2860 "xmlNewChild : parent == NULL\n");
2861#endif
2862 return(NULL);
2863 }
2864
2865 if (name == NULL) {
2866#ifdef DEBUG_TREE
2867 xmlGenericError(xmlGenericErrorContext,
2868 "xmlNewChild : name == NULL\n");
2869#endif
2870 return(NULL);
2871 }
2872
2873 /*
2874 * Allocate a new node
2875 */
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002876 if (parent->type == XML_ELEMENT_NODE) {
2877 if (ns == NULL)
2878 cur = xmlNewDocNode(parent->doc, parent->ns, name, content);
2879 else
2880 cur = xmlNewDocNode(parent->doc, ns, name, content);
2881 } else if ((parent->type == XML_DOCUMENT_NODE) ||
2882 (parent->type == XML_HTML_DOCUMENT_NODE)) {
2883 if (ns == NULL)
2884 cur = xmlNewDocNode((xmlDocPtr) parent, NULL, name, content);
2885 else
2886 cur = xmlNewDocNode((xmlDocPtr) parent, ns, name, content);
Daniel Veillard7e3f1402002-10-28 18:52:57 +00002887 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2888 cur = xmlNewDocNode( parent->doc, ns, name, content);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002889 } else {
2890 return(NULL);
2891 }
Owen Taylor3473f882001-02-23 17:55:21 +00002892 if (cur == NULL) return(NULL);
2893
2894 /*
2895 * add the new element at the end of the children list.
2896 */
2897 cur->type = XML_ELEMENT_NODE;
2898 cur->parent = parent;
2899 cur->doc = parent->doc;
2900 if (parent->children == NULL) {
2901 parent->children = cur;
2902 parent->last = cur;
2903 } else {
2904 prev = parent->last;
2905 prev->next = cur;
2906 cur->prev = prev;
2907 parent->last = cur;
2908 }
2909
2910 return(cur);
2911}
Daniel Veillard652327a2003-09-29 18:02:38 +00002912#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002913
2914/**
Rob Richards65815122006-02-25 17:13:33 +00002915 * xmlAddPropSibling:
Daniel Veillardaa6de472008-08-25 14:53:31 +00002916 * @prev: the attribute to which @prop is added after
Rob Richards65815122006-02-25 17:13:33 +00002917 * @cur: the base attribute passed to calling function
2918 * @prop: the new attribute
2919 *
2920 * Add a new attribute after @prev using @cur as base attribute.
2921 * When inserting before @cur, @prev is passed as @cur->prev.
2922 * When inserting after @cur, @prev is passed as @cur.
Daniel Veillardaa6de472008-08-25 14:53:31 +00002923 * If an existing attribute is found it is detroyed prior to adding @prop.
Rob Richards65815122006-02-25 17:13:33 +00002924 *
2925 * Returns the attribute being inserted or NULL in case of error.
2926 */
2927static xmlNodePtr
2928xmlAddPropSibling(xmlNodePtr prev, xmlNodePtr cur, xmlNodePtr prop) {
2929 xmlAttrPtr attr;
2930
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002931 if ((cur == NULL) || (cur->type != XML_ATTRIBUTE_NODE) ||
2932 (prop == NULL) || (prop->type != XML_ATTRIBUTE_NODE) ||
2933 ((prev != NULL) && (prev->type != XML_ATTRIBUTE_NODE)))
Rob Richards65815122006-02-25 17:13:33 +00002934 return(NULL);
2935
2936 /* check if an attribute with the same name exists */
2937 if (prop->ns == NULL)
2938 attr = xmlHasNsProp(cur->parent, prop->name, NULL);
2939 else
2940 attr = xmlHasNsProp(cur->parent, prop->name, prop->ns->href);
2941
2942 if (prop->doc != cur->doc) {
2943 xmlSetTreeDoc(prop, cur->doc);
2944 }
2945 prop->parent = cur->parent;
2946 prop->prev = prev;
2947 if (prev != NULL) {
2948 prop->next = prev->next;
2949 prev->next = prop;
2950 if (prop->next)
2951 prop->next->prev = prop;
2952 } else {
2953 prop->next = cur;
2954 cur->prev = prop;
2955 }
2956 if (prop->prev == NULL && prop->parent != NULL)
2957 prop->parent->properties = (xmlAttrPtr) prop;
2958 if ((attr != NULL) && (attr->type != XML_ATTRIBUTE_DECL)) {
2959 /* different instance, destroy it (attributes must be unique) */
2960 xmlRemoveProp((xmlAttrPtr) attr);
2961 }
2962 return prop;
2963}
2964
2965/**
Owen Taylor3473f882001-02-23 17:55:21 +00002966 * xmlAddNextSibling:
2967 * @cur: the child node
2968 * @elem: the new node
2969 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002970 * Add a new node @elem as the next sibling of @cur
2971 * If the new node was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00002972 * first unlinked from its existing context.
2973 * As a result of text merging @elem may be freed.
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002974 * If the new node is ATTRIBUTE, it is added into properties instead of children.
Daniel Veillardaa6de472008-08-25 14:53:31 +00002975 * If there is an attribute with equal name, it is first destroyed.
Owen Taylor3473f882001-02-23 17:55:21 +00002976 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002977 * Returns the new node or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002978 */
2979xmlNodePtr
2980xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) {
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002981 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002982#ifdef DEBUG_TREE
2983 xmlGenericError(xmlGenericErrorContext,
2984 "xmlAddNextSibling : cur == NULL\n");
2985#endif
2986 return(NULL);
2987 }
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002988 if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002989#ifdef DEBUG_TREE
2990 xmlGenericError(xmlGenericErrorContext,
2991 "xmlAddNextSibling : elem == NULL\n");
2992#endif
2993 return(NULL);
2994 }
2995
Rob Richards19dc9612005-10-28 16:15:16 +00002996 if (cur == elem) {
2997#ifdef DEBUG_TREE
2998 xmlGenericError(xmlGenericErrorContext,
2999 "xmlAddNextSibling : cur == elem\n");
3000#endif
3001 return(NULL);
3002 }
3003
Owen Taylor3473f882001-02-23 17:55:21 +00003004 xmlUnlinkNode(elem);
3005
3006 if (elem->type == XML_TEXT_NODE) {
3007 if (cur->type == XML_TEXT_NODE) {
Owen Taylor3473f882001-02-23 17:55:21 +00003008 xmlNodeAddContent(cur, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003009 xmlFreeNode(elem);
3010 return(cur);
3011 }
Daniel Veillard9e1c72d2001-08-31 20:03:19 +00003012 if ((cur->next != NULL) && (cur->next->type == XML_TEXT_NODE) &&
3013 (cur->name == cur->next->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003014 xmlChar *tmp;
3015
3016 tmp = xmlStrdup(elem->content);
3017 tmp = xmlStrcat(tmp, cur->next->content);
3018 xmlNodeSetContent(cur->next, tmp);
3019 xmlFree(tmp);
Owen Taylor3473f882001-02-23 17:55:21 +00003020 xmlFreeNode(elem);
3021 return(cur->next);
3022 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003023 } else if (elem->type == XML_ATTRIBUTE_NODE) {
Rob Richards65815122006-02-25 17:13:33 +00003024 return xmlAddPropSibling(cur, cur, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003025 }
3026
3027 if (elem->doc != cur->doc) {
3028 xmlSetTreeDoc(elem, cur->doc);
3029 }
3030 elem->parent = cur->parent;
3031 elem->prev = cur;
3032 elem->next = cur->next;
3033 cur->next = elem;
3034 if (elem->next != NULL)
3035 elem->next->prev = elem;
Rob Richards65815122006-02-25 17:13:33 +00003036 if ((elem->parent != NULL) && (elem->parent->last == cur))
Owen Taylor3473f882001-02-23 17:55:21 +00003037 elem->parent->last = elem;
3038 return(elem);
3039}
3040
William M. Brack21e4ef22005-01-02 09:53:13 +00003041#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
Nicolas Le Cam41586ca2013-06-17 13:01:33 +02003042 defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00003043/**
3044 * xmlAddPrevSibling:
3045 * @cur: the child node
3046 * @elem: the new node
3047 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003048 * Add a new node @elem as the previous sibling of @cur
Owen Taylor3473f882001-02-23 17:55:21 +00003049 * merging adjacent TEXT nodes (@elem may be freed)
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003050 * If the new node was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00003051 * first unlinked from its existing context.
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003052 * If the new node is ATTRIBUTE, it is added into properties instead of children.
Daniel Veillardaa6de472008-08-25 14:53:31 +00003053 * If there is an attribute with equal name, it is first destroyed.
Owen Taylor3473f882001-02-23 17:55:21 +00003054 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003055 * Returns the new node or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003056 */
3057xmlNodePtr
3058xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) {
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003059 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003060#ifdef DEBUG_TREE
3061 xmlGenericError(xmlGenericErrorContext,
3062 "xmlAddPrevSibling : cur == NULL\n");
3063#endif
3064 return(NULL);
3065 }
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003066 if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003067#ifdef DEBUG_TREE
3068 xmlGenericError(xmlGenericErrorContext,
3069 "xmlAddPrevSibling : elem == NULL\n");
3070#endif
3071 return(NULL);
3072 }
3073
Rob Richards19dc9612005-10-28 16:15:16 +00003074 if (cur == elem) {
3075#ifdef DEBUG_TREE
3076 xmlGenericError(xmlGenericErrorContext,
3077 "xmlAddPrevSibling : cur == elem\n");
3078#endif
3079 return(NULL);
3080 }
3081
Owen Taylor3473f882001-02-23 17:55:21 +00003082 xmlUnlinkNode(elem);
3083
3084 if (elem->type == XML_TEXT_NODE) {
3085 if (cur->type == XML_TEXT_NODE) {
Owen Taylor3473f882001-02-23 17:55:21 +00003086 xmlChar *tmp;
3087
3088 tmp = xmlStrdup(elem->content);
3089 tmp = xmlStrcat(tmp, cur->content);
3090 xmlNodeSetContent(cur, tmp);
3091 xmlFree(tmp);
Owen Taylor3473f882001-02-23 17:55:21 +00003092 xmlFreeNode(elem);
3093 return(cur);
3094 }
Daniel Veillard9e1c72d2001-08-31 20:03:19 +00003095 if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE) &&
3096 (cur->name == cur->prev->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003097 xmlNodeAddContent(cur->prev, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003098 xmlFreeNode(elem);
3099 return(cur->prev);
3100 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003101 } else if (elem->type == XML_ATTRIBUTE_NODE) {
Rob Richards65815122006-02-25 17:13:33 +00003102 return xmlAddPropSibling(cur->prev, cur, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003103 }
3104
3105 if (elem->doc != cur->doc) {
3106 xmlSetTreeDoc(elem, cur->doc);
3107 }
3108 elem->parent = cur->parent;
3109 elem->next = cur;
3110 elem->prev = cur->prev;
3111 cur->prev = elem;
3112 if (elem->prev != NULL)
3113 elem->prev->next = elem;
Rob Richards65815122006-02-25 17:13:33 +00003114 if ((elem->parent != NULL) && (elem->parent->children == cur)) {
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003115 elem->parent->children = elem;
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003116 }
Owen Taylor3473f882001-02-23 17:55:21 +00003117 return(elem);
3118}
Daniel Veillard652327a2003-09-29 18:02:38 +00003119#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00003120
3121/**
3122 * xmlAddSibling:
3123 * @cur: the child node
3124 * @elem: the new node
3125 *
3126 * Add a new element @elem to the list of siblings of @cur
3127 * merging adjacent TEXT nodes (@elem may be freed)
3128 * If the new element was already inserted in a document it is
3129 * first unlinked from its existing context.
3130 *
3131 * Returns the new element or NULL in case of error.
3132 */
3133xmlNodePtr
3134xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) {
3135 xmlNodePtr parent;
3136
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003137 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003138#ifdef DEBUG_TREE
3139 xmlGenericError(xmlGenericErrorContext,
3140 "xmlAddSibling : cur == NULL\n");
3141#endif
3142 return(NULL);
3143 }
3144
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003145 if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003146#ifdef DEBUG_TREE
3147 xmlGenericError(xmlGenericErrorContext,
3148 "xmlAddSibling : elem == NULL\n");
3149#endif
3150 return(NULL);
3151 }
3152
Daniel Veillard43bc89c2009-03-23 19:32:04 +00003153 if (cur == elem) {
3154#ifdef DEBUG_TREE
3155 xmlGenericError(xmlGenericErrorContext,
3156 "xmlAddSibling : cur == elem\n");
3157#endif
3158 return(NULL);
3159 }
3160
Owen Taylor3473f882001-02-23 17:55:21 +00003161 /*
3162 * Constant time is we can rely on the ->parent->last to find
3163 * the last sibling.
3164 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00003165 if ((cur->type != XML_ATTRIBUTE_NODE) && (cur->parent != NULL) &&
Owen Taylor3473f882001-02-23 17:55:21 +00003166 (cur->parent->children != NULL) &&
3167 (cur->parent->last != NULL) &&
3168 (cur->parent->last->next == NULL)) {
3169 cur = cur->parent->last;
3170 } else {
3171 while (cur->next != NULL) cur = cur->next;
3172 }
3173
3174 xmlUnlinkNode(elem);
3175
Daniel Veillarde22dd5c2003-10-29 12:53:27 +00003176 if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE) &&
3177 (cur->name == elem->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003178 xmlNodeAddContent(cur, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003179 xmlFreeNode(elem);
3180 return(cur);
Rob Richards65815122006-02-25 17:13:33 +00003181 } else if (elem->type == XML_ATTRIBUTE_NODE) {
3182 return xmlAddPropSibling(cur, cur, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003183 }
3184
3185 if (elem->doc != cur->doc) {
3186 xmlSetTreeDoc(elem, cur->doc);
3187 }
3188 parent = cur->parent;
3189 elem->prev = cur;
3190 elem->next = NULL;
3191 elem->parent = parent;
3192 cur->next = elem;
3193 if (parent != NULL)
3194 parent->last = elem;
3195
3196 return(elem);
3197}
3198
3199/**
3200 * xmlAddChildList:
3201 * @parent: the parent node
3202 * @cur: the first node in the list
3203 *
3204 * Add a list of node at the end of the child list of the parent
3205 * merging adjacent TEXT nodes (@cur may be freed)
3206 *
3207 * Returns the last child or NULL in case of error.
3208 */
3209xmlNodePtr
3210xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) {
3211 xmlNodePtr prev;
3212
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003213 if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003214#ifdef DEBUG_TREE
3215 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00003216 "xmlAddChildList : parent == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003217#endif
3218 return(NULL);
3219 }
3220
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003221 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003222#ifdef DEBUG_TREE
3223 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00003224 "xmlAddChildList : child == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003225#endif
3226 return(NULL);
3227 }
3228
3229 if ((cur->doc != NULL) && (parent->doc != NULL) &&
3230 (cur->doc != parent->doc)) {
3231#ifdef DEBUG_TREE
3232 xmlGenericError(xmlGenericErrorContext,
3233 "Elements moved to a different document\n");
3234#endif
3235 }
3236
3237 /*
3238 * add the first element at the end of the children list.
3239 */
Daniel Veillard5335dc52003-01-01 20:59:38 +00003240
Owen Taylor3473f882001-02-23 17:55:21 +00003241 if (parent->children == NULL) {
3242 parent->children = cur;
3243 } else {
3244 /*
3245 * If cur and parent->last both are TEXT nodes, then merge them.
3246 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00003247 if ((cur->type == XML_TEXT_NODE) &&
Owen Taylor3473f882001-02-23 17:55:21 +00003248 (parent->last->type == XML_TEXT_NODE) &&
3249 (cur->name == parent->last->name)) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00003250 xmlNodeAddContent(parent->last, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003251 /*
3252 * if it's the only child, nothing more to be done.
3253 */
3254 if (cur->next == NULL) {
3255 xmlFreeNode(cur);
3256 return(parent->last);
3257 }
3258 prev = cur;
3259 cur = cur->next;
3260 xmlFreeNode(prev);
3261 }
3262 prev = parent->last;
3263 prev->next = cur;
3264 cur->prev = prev;
3265 }
3266 while (cur->next != NULL) {
3267 cur->parent = parent;
3268 if (cur->doc != parent->doc) {
3269 xmlSetTreeDoc(cur, parent->doc);
3270 }
3271 cur = cur->next;
3272 }
3273 cur->parent = parent;
Rob Richards810a78b2008-12-31 22:13:57 +00003274 /* the parent may not be linked to a doc ! */
3275 if (cur->doc != parent->doc) {
3276 xmlSetTreeDoc(cur, parent->doc);
3277 }
Owen Taylor3473f882001-02-23 17:55:21 +00003278 parent->last = cur;
3279
3280 return(cur);
3281}
3282
3283/**
3284 * xmlAddChild:
3285 * @parent: the parent node
3286 * @cur: the child node
3287 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003288 * Add a new node to @parent, at the end of the child (or property) list
Owen Taylor3473f882001-02-23 17:55:21 +00003289 * merging adjacent TEXT nodes (in which case @cur is freed)
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003290 * If the new node is ATTRIBUTE, it is added into properties instead of children.
Daniel Veillardaa6de472008-08-25 14:53:31 +00003291 * If there is an attribute with equal name, it is first destroyed.
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003292 *
Owen Taylor3473f882001-02-23 17:55:21 +00003293 * Returns the child or NULL in case of error.
3294 */
3295xmlNodePtr
3296xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) {
3297 xmlNodePtr prev;
3298
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003299 if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003300#ifdef DEBUG_TREE
3301 xmlGenericError(xmlGenericErrorContext,
3302 "xmlAddChild : parent == NULL\n");
3303#endif
3304 return(NULL);
3305 }
3306
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003307 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003308#ifdef DEBUG_TREE
3309 xmlGenericError(xmlGenericErrorContext,
3310 "xmlAddChild : child == NULL\n");
3311#endif
3312 return(NULL);
3313 }
3314
Rob Richards19dc9612005-10-28 16:15:16 +00003315 if (parent == cur) {
3316#ifdef DEBUG_TREE
3317 xmlGenericError(xmlGenericErrorContext,
3318 "xmlAddChild : parent == cur\n");
3319#endif
3320 return(NULL);
3321 }
Owen Taylor3473f882001-02-23 17:55:21 +00003322 /*
3323 * If cur is a TEXT node, merge its content with adjacent TEXT nodes
Owen Taylor3473f882001-02-23 17:55:21 +00003324 * cur is then freed.
3325 */
3326 if (cur->type == XML_TEXT_NODE) {
Daniel Veillard7db37732001-07-12 01:20:08 +00003327 if ((parent->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003328 (parent->content != NULL) &&
Rob Richards19dc9612005-10-28 16:15:16 +00003329 (parent->name == cur->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003330 xmlNodeAddContent(parent, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003331 xmlFreeNode(cur);
3332 return(parent);
3333 }
3334 if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003335 (parent->last->name == cur->name) &&
3336 (parent->last != cur)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003337 xmlNodeAddContent(parent->last, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003338 xmlFreeNode(cur);
3339 return(parent->last);
3340 }
3341 }
3342
3343 /*
3344 * add the new element at the end of the children list.
3345 */
Daniel Veillard5335dc52003-01-01 20:59:38 +00003346 prev = cur->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00003347 cur->parent = parent;
3348 if (cur->doc != parent->doc) {
3349 xmlSetTreeDoc(cur, parent->doc);
3350 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00003351 /* this check prevents a loop on tree-traversions if a developer
3352 * tries to add a node to its parent multiple times
3353 */
3354 if (prev == parent)
3355 return(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00003356
3357 /*
Daniel Veillard7db37732001-07-12 01:20:08 +00003358 * Coalescing
Owen Taylor3473f882001-02-23 17:55:21 +00003359 */
Daniel Veillard7db37732001-07-12 01:20:08 +00003360 if ((parent->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003361 (parent->content != NULL) &&
3362 (parent != cur)) {
Daniel Veillard7db37732001-07-12 01:20:08 +00003363 xmlNodeAddContent(parent, cur->content);
Daniel Veillard7db37732001-07-12 01:20:08 +00003364 xmlFreeNode(cur);
3365 return(parent);
Owen Taylor3473f882001-02-23 17:55:21 +00003366 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003367 if (cur->type == XML_ATTRIBUTE_NODE) {
Rob Richards19dc9612005-10-28 16:15:16 +00003368 if (parent->type != XML_ELEMENT_NODE)
3369 return(NULL);
Rob Richards810a78b2008-12-31 22:13:57 +00003370 if (parent->properties != NULL) {
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003371 /* check if an attribute with the same name exists */
3372 xmlAttrPtr lastattr;
Owen Taylor3473f882001-02-23 17:55:21 +00003373
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003374 if (cur->ns == NULL)
Rob Richardsc342ec62005-10-25 00:10:12 +00003375 lastattr = xmlHasNsProp(parent, cur->name, NULL);
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003376 else
3377 lastattr = xmlHasNsProp(parent, cur->name, cur->ns->href);
Rob Richardsc342ec62005-10-25 00:10:12 +00003378 if ((lastattr != NULL) && (lastattr != (xmlAttrPtr) cur) && (lastattr->type != XML_ATTRIBUTE_DECL)) {
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003379 /* different instance, destroy it (attributes must be unique) */
Rob Richards19dc9612005-10-28 16:15:16 +00003380 xmlUnlinkNode((xmlNodePtr) lastattr);
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003381 xmlFreeProp(lastattr);
3382 }
Rob Richards19dc9612005-10-28 16:15:16 +00003383 if (lastattr == (xmlAttrPtr) cur)
3384 return(cur);
Rob Richards810a78b2008-12-31 22:13:57 +00003385
3386 }
3387 if (parent->properties == NULL) {
3388 parent->properties = (xmlAttrPtr) cur;
3389 } else {
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003390 /* find the end */
Rob Richards810a78b2008-12-31 22:13:57 +00003391 xmlAttrPtr lastattr = parent->properties;
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003392 while (lastattr->next != NULL) {
3393 lastattr = lastattr->next;
3394 }
3395 lastattr->next = (xmlAttrPtr) cur;
3396 ((xmlAttrPtr) cur)->prev = lastattr;
3397 }
3398 } else {
3399 if (parent->children == NULL) {
3400 parent->children = cur;
3401 parent->last = cur;
3402 } else {
3403 prev = parent->last;
3404 prev->next = cur;
3405 cur->prev = prev;
3406 parent->last = cur;
3407 }
3408 }
Owen Taylor3473f882001-02-23 17:55:21 +00003409 return(cur);
3410}
3411
3412/**
3413 * xmlGetLastChild:
3414 * @parent: the parent node
3415 *
3416 * Search the last child of a node.
3417 * Returns the last child or NULL if none.
3418 */
3419xmlNodePtr
3420xmlGetLastChild(xmlNodePtr parent) {
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003421 if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003422#ifdef DEBUG_TREE
3423 xmlGenericError(xmlGenericErrorContext,
3424 "xmlGetLastChild : parent == NULL\n");
3425#endif
3426 return(NULL);
3427 }
3428 return(parent->last);
3429}
3430
Daniel Veillardbe2bd6a2008-11-27 15:26:28 +00003431#ifdef LIBXML_TREE_ENABLED
3432/*
3433 * 5 interfaces from DOM ElementTraversal
3434 */
3435
3436/**
3437 * xmlChildElementCount:
3438 * @parent: the parent node
3439 *
3440 * Finds the current number of child nodes of that element which are
3441 * element nodes.
3442 * Note the handling of entities references is different than in
3443 * the W3C DOM element traversal spec since we don't have back reference
3444 * from entities content to entities references.
3445 *
3446 * Returns the count of element child or 0 if not available
3447 */
3448unsigned long
3449xmlChildElementCount(xmlNodePtr parent) {
3450 unsigned long ret = 0;
3451 xmlNodePtr cur = NULL;
3452
3453 if (parent == NULL)
3454 return(0);
3455 switch (parent->type) {
3456 case XML_ELEMENT_NODE:
3457 case XML_ENTITY_NODE:
3458 case XML_DOCUMENT_NODE:
3459 case XML_HTML_DOCUMENT_NODE:
3460 cur = parent->children;
3461 break;
3462 default:
3463 return(0);
3464 }
3465 while (cur != NULL) {
3466 if (cur->type == XML_ELEMENT_NODE)
3467 ret++;
3468 cur = cur->next;
3469 }
3470 return(ret);
3471}
3472
3473/**
3474 * xmlFirstElementChild:
3475 * @parent: the parent node
3476 *
3477 * Finds the first child node of that element which is a Element node
3478 * Note the handling of entities references is different than in
3479 * the W3C DOM element traversal spec since we don't have back reference
3480 * from entities content to entities references.
3481 *
3482 * Returns the first element child or NULL if not available
3483 */
3484xmlNodePtr
3485xmlFirstElementChild(xmlNodePtr parent) {
3486 xmlNodePtr cur = NULL;
3487
3488 if (parent == NULL)
3489 return(NULL);
3490 switch (parent->type) {
3491 case XML_ELEMENT_NODE:
3492 case XML_ENTITY_NODE:
3493 case XML_DOCUMENT_NODE:
3494 case XML_HTML_DOCUMENT_NODE:
3495 cur = parent->children;
3496 break;
3497 default:
3498 return(NULL);
3499 }
3500 while (cur != NULL) {
3501 if (cur->type == XML_ELEMENT_NODE)
3502 return(cur);
3503 cur = cur->next;
3504 }
3505 return(NULL);
3506}
3507
3508/**
3509 * xmlLastElementChild:
3510 * @parent: the parent node
3511 *
3512 * Finds the last child node of that element which is a Element node
3513 * Note the handling of entities references is different than in
3514 * the W3C DOM element traversal spec since we don't have back reference
3515 * from entities content to entities references.
3516 *
3517 * Returns the last element child or NULL if not available
3518 */
3519xmlNodePtr
3520xmlLastElementChild(xmlNodePtr parent) {
3521 xmlNodePtr cur = NULL;
3522
3523 if (parent == NULL)
3524 return(NULL);
3525 switch (parent->type) {
3526 case XML_ELEMENT_NODE:
3527 case XML_ENTITY_NODE:
3528 case XML_DOCUMENT_NODE:
3529 case XML_HTML_DOCUMENT_NODE:
3530 cur = parent->last;
3531 break;
3532 default:
3533 return(NULL);
3534 }
3535 while (cur != NULL) {
3536 if (cur->type == XML_ELEMENT_NODE)
3537 return(cur);
3538 cur = cur->prev;
3539 }
3540 return(NULL);
3541}
3542
3543/**
3544 * xmlPreviousElementSibling:
3545 * @node: the current node
3546 *
3547 * Finds the first closest previous sibling of the node which is an
3548 * element node.
3549 * Note the handling of entities references is different than in
3550 * the W3C DOM element traversal spec since we don't have back reference
3551 * from entities content to entities references.
3552 *
3553 * Returns the previous element sibling or NULL if not available
3554 */
3555xmlNodePtr
3556xmlPreviousElementSibling(xmlNodePtr node) {
3557 if (node == NULL)
3558 return(NULL);
3559 switch (node->type) {
3560 case XML_ELEMENT_NODE:
3561 case XML_TEXT_NODE:
3562 case XML_CDATA_SECTION_NODE:
3563 case XML_ENTITY_REF_NODE:
3564 case XML_ENTITY_NODE:
3565 case XML_PI_NODE:
3566 case XML_COMMENT_NODE:
3567 case XML_XINCLUDE_START:
3568 case XML_XINCLUDE_END:
3569 node = node->prev;
3570 break;
3571 default:
3572 return(NULL);
3573 }
3574 while (node != NULL) {
3575 if (node->type == XML_ELEMENT_NODE)
3576 return(node);
François Delyon2f700902010-02-03 17:32:37 +01003577 node = node->prev;
Daniel Veillardbe2bd6a2008-11-27 15:26:28 +00003578 }
3579 return(NULL);
3580}
3581
3582/**
3583 * xmlNextElementSibling:
3584 * @node: the current node
3585 *
3586 * Finds the first closest next sibling of the node which is an
3587 * element node.
3588 * Note the handling of entities references is different than in
3589 * the W3C DOM element traversal spec since we don't have back reference
3590 * from entities content to entities references.
3591 *
3592 * Returns the next element sibling or NULL if not available
3593 */
3594xmlNodePtr
3595xmlNextElementSibling(xmlNodePtr node) {
3596 if (node == NULL)
3597 return(NULL);
3598 switch (node->type) {
3599 case XML_ELEMENT_NODE:
3600 case XML_TEXT_NODE:
3601 case XML_CDATA_SECTION_NODE:
3602 case XML_ENTITY_REF_NODE:
3603 case XML_ENTITY_NODE:
3604 case XML_PI_NODE:
3605 case XML_COMMENT_NODE:
3606 case XML_DTD_NODE:
3607 case XML_XINCLUDE_START:
3608 case XML_XINCLUDE_END:
3609 node = node->next;
3610 break;
3611 default:
3612 return(NULL);
3613 }
3614 while (node != NULL) {
3615 if (node->type == XML_ELEMENT_NODE)
3616 return(node);
3617 node = node->next;
3618 }
3619 return(NULL);
3620}
3621
3622#endif /* LIBXML_TREE_ENABLED */
3623
Owen Taylor3473f882001-02-23 17:55:21 +00003624/**
3625 * xmlFreeNodeList:
3626 * @cur: the first node in the list
3627 *
3628 * Free a node and all its siblings, this is a recursive behaviour, all
3629 * the children are freed too.
3630 */
3631void
3632xmlFreeNodeList(xmlNodePtr cur) {
3633 xmlNodePtr next;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003634 xmlDictPtr dict = NULL;
3635
3636 if (cur == NULL) return;
Daniel Veillarde6a55192002-01-14 17:11:53 +00003637 if (cur->type == XML_NAMESPACE_DECL) {
3638 xmlFreeNsList((xmlNsPtr) cur);
3639 return;
3640 }
Daniel Veillard9adc0462003-03-24 18:39:54 +00003641 if ((cur->type == XML_DOCUMENT_NODE) ||
3642#ifdef LIBXML_DOCB_ENABLED
3643 (cur->type == XML_DOCB_DOCUMENT_NODE) ||
Daniel Veillard9adc0462003-03-24 18:39:54 +00003644#endif
Daniel Veillard6560a422003-03-27 21:25:38 +00003645 (cur->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard9adc0462003-03-24 18:39:54 +00003646 xmlFreeDoc((xmlDocPtr) cur);
3647 return;
3648 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003649 if (cur->doc != NULL) dict = cur->doc->dict;
Owen Taylor3473f882001-02-23 17:55:21 +00003650 while (cur != NULL) {
3651 next = cur->next;
Daniel Veillard02141ea2001-04-30 11:46:40 +00003652 if (cur->type != XML_DTD_NODE) {
Daniel Veillard5335dc52003-01-01 20:59:38 +00003653
Daniel Veillarda880b122003-04-21 21:36:41 +00003654 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00003655 xmlDeregisterNodeDefaultValue(cur);
3656
Daniel Veillard02141ea2001-04-30 11:46:40 +00003657 if ((cur->children != NULL) &&
3658 (cur->type != XML_ENTITY_REF_NODE))
3659 xmlFreeNodeList(cur->children);
Daniel Veillard01c13b52002-12-10 15:19:08 +00003660 if (((cur->type == XML_ELEMENT_NODE) ||
3661 (cur->type == XML_XINCLUDE_START) ||
3662 (cur->type == XML_XINCLUDE_END)) &&
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003663 (cur->properties != NULL))
Daniel Veillard02141ea2001-04-30 11:46:40 +00003664 xmlFreePropList(cur->properties);
Daniel Veillard7db37732001-07-12 01:20:08 +00003665 if ((cur->type != XML_ELEMENT_NODE) &&
3666 (cur->type != XML_XINCLUDE_START) &&
3667 (cur->type != XML_XINCLUDE_END) &&
Daniel Veillard8874b942005-08-25 13:19:21 +00003668 (cur->type != XML_ENTITY_REF_NODE) &&
3669 (cur->content != (xmlChar *) &(cur->properties))) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003670 DICT_FREE(cur->content)
Daniel Veillard7db37732001-07-12 01:20:08 +00003671 }
3672 if (((cur->type == XML_ELEMENT_NODE) ||
3673 (cur->type == XML_XINCLUDE_START) ||
3674 (cur->type == XML_XINCLUDE_END)) &&
3675 (cur->nsDef != NULL))
3676 xmlFreeNsList(cur->nsDef);
3677
Daniel Veillard9cc6dc62001-06-11 08:09:20 +00003678 /*
3679 * When a node is a text node or a comment, it uses a global static
3680 * variable for the name of the node.
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003681 * Otherwise the node name might come from the document's
3682 * dictionnary
Daniel Veillard9cc6dc62001-06-11 08:09:20 +00003683 */
Daniel Veillard02141ea2001-04-30 11:46:40 +00003684 if ((cur->name != NULL) &&
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003685 (cur->type != XML_TEXT_NODE) &&
3686 (cur->type != XML_COMMENT_NODE))
3687 DICT_FREE(cur->name)
Daniel Veillard02141ea2001-04-30 11:46:40 +00003688 xmlFree(cur);
3689 }
Owen Taylor3473f882001-02-23 17:55:21 +00003690 cur = next;
3691 }
3692}
3693
3694/**
3695 * xmlFreeNode:
3696 * @cur: the node
3697 *
3698 * Free a node, this is a recursive behaviour, all the children are freed too.
3699 * This doesn't unlink the child from the list, use xmlUnlinkNode() first.
3700 */
3701void
3702xmlFreeNode(xmlNodePtr cur) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003703 xmlDictPtr dict = NULL;
3704
3705 if (cur == NULL) return;
Daniel Veillard5335dc52003-01-01 20:59:38 +00003706
Daniel Veillard02141ea2001-04-30 11:46:40 +00003707 /* use xmlFreeDtd for DTD nodes */
Daniel Veillarde6a55192002-01-14 17:11:53 +00003708 if (cur->type == XML_DTD_NODE) {
3709 xmlFreeDtd((xmlDtdPtr) cur);
Owen Taylor3473f882001-02-23 17:55:21 +00003710 return;
Daniel Veillarde6a55192002-01-14 17:11:53 +00003711 }
3712 if (cur->type == XML_NAMESPACE_DECL) {
3713 xmlFreeNs((xmlNsPtr) cur);
3714 return;
3715 }
Daniel Veillarda70d62f2002-11-07 14:18:03 +00003716 if (cur->type == XML_ATTRIBUTE_NODE) {
3717 xmlFreeProp((xmlAttrPtr) cur);
3718 return;
3719 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00003720
Daniel Veillarda880b122003-04-21 21:36:41 +00003721 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00003722 xmlDeregisterNodeDefaultValue(cur);
3723
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003724 if (cur->doc != NULL) dict = cur->doc->dict;
3725
Daniel Veillard2cba4152008-08-27 11:45:41 +00003726 if (cur->type == XML_ENTITY_DECL) {
3727 xmlEntityPtr ent = (xmlEntityPtr) cur;
3728 DICT_FREE(ent->SystemID);
3729 DICT_FREE(ent->ExternalID);
3730 }
Owen Taylor3473f882001-02-23 17:55:21 +00003731 if ((cur->children != NULL) &&
3732 (cur->type != XML_ENTITY_REF_NODE))
3733 xmlFreeNodeList(cur->children);
Daniel Veillard01c13b52002-12-10 15:19:08 +00003734 if (((cur->type == XML_ELEMENT_NODE) ||
3735 (cur->type == XML_XINCLUDE_START) ||
3736 (cur->type == XML_XINCLUDE_END)) &&
3737 (cur->properties != NULL))
Daniel Veillard02141ea2001-04-30 11:46:40 +00003738 xmlFreePropList(cur->properties);
Daniel Veillard7db37732001-07-12 01:20:08 +00003739 if ((cur->type != XML_ELEMENT_NODE) &&
3740 (cur->content != NULL) &&
3741 (cur->type != XML_ENTITY_REF_NODE) &&
3742 (cur->type != XML_XINCLUDE_END) &&
Daniel Veillard8874b942005-08-25 13:19:21 +00003743 (cur->type != XML_XINCLUDE_START) &&
3744 (cur->content != (xmlChar *) &(cur->properties))) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003745 DICT_FREE(cur->content)
Daniel Veillard7db37732001-07-12 01:20:08 +00003746 }
3747
Daniel Veillardacd370f2001-06-09 17:17:51 +00003748 /*
3749 * When a node is a text node or a comment, it uses a global static
3750 * variable for the name of the node.
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003751 * Otherwise the node name might come from the document's dictionnary
Daniel Veillardacd370f2001-06-09 17:17:51 +00003752 */
Owen Taylor3473f882001-02-23 17:55:21 +00003753 if ((cur->name != NULL) &&
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003754 (cur->type != XML_TEXT_NODE) &&
3755 (cur->type != XML_COMMENT_NODE))
3756 DICT_FREE(cur->name)
Daniel Veillardacd370f2001-06-09 17:17:51 +00003757
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003758 if (((cur->type == XML_ELEMENT_NODE) ||
3759 (cur->type == XML_XINCLUDE_START) ||
3760 (cur->type == XML_XINCLUDE_END)) &&
3761 (cur->nsDef != NULL))
3762 xmlFreeNsList(cur->nsDef);
Owen Taylor3473f882001-02-23 17:55:21 +00003763 xmlFree(cur);
3764}
3765
3766/**
3767 * xmlUnlinkNode:
3768 * @cur: the node
3769 *
3770 * Unlink a node from it's current context, the node is not freed
Daniel Veillard39d027c2012-05-11 12:38:23 +08003771 * If one need to free the node, use xmlFreeNode() routine after the
3772 * unlink to discard it.
Daniel Veillard6ca24a32012-08-08 15:31:55 +08003773 * Note that namespace nodes can't be unlinked as they do not have
3774 * pointer to their parent.
Owen Taylor3473f882001-02-23 17:55:21 +00003775 */
3776void
3777xmlUnlinkNode(xmlNodePtr cur) {
3778 if (cur == NULL) {
3779#ifdef DEBUG_TREE
3780 xmlGenericError(xmlGenericErrorContext,
3781 "xmlUnlinkNode : node == NULL\n");
3782#endif
3783 return;
3784 }
Daniel Veillard6ca24a32012-08-08 15:31:55 +08003785 if (cur->type == XML_NAMESPACE_DECL)
3786 return;
Daniel Veillard29e43992001-12-13 22:21:58 +00003787 if (cur->type == XML_DTD_NODE) {
3788 xmlDocPtr doc;
3789 doc = cur->doc;
Daniel Veillarda067e652003-05-01 08:03:46 +00003790 if (doc != NULL) {
3791 if (doc->intSubset == (xmlDtdPtr) cur)
3792 doc->intSubset = NULL;
3793 if (doc->extSubset == (xmlDtdPtr) cur)
3794 doc->extSubset = NULL;
3795 }
Daniel Veillard29e43992001-12-13 22:21:58 +00003796 }
Daniel Veillard2cba4152008-08-27 11:45:41 +00003797 if (cur->type == XML_ENTITY_DECL) {
3798 xmlDocPtr doc;
3799 doc = cur->doc;
3800 if (doc != NULL) {
3801 if (doc->intSubset != NULL) {
3802 if (xmlHashLookup(doc->intSubset->entities, cur->name) == cur)
3803 xmlHashRemoveEntry(doc->intSubset->entities, cur->name,
3804 NULL);
3805 if (xmlHashLookup(doc->intSubset->pentities, cur->name) == cur)
3806 xmlHashRemoveEntry(doc->intSubset->pentities, cur->name,
3807 NULL);
3808 }
3809 if (doc->extSubset != NULL) {
3810 if (xmlHashLookup(doc->extSubset->entities, cur->name) == cur)
3811 xmlHashRemoveEntry(doc->extSubset->entities, cur->name,
3812 NULL);
3813 if (xmlHashLookup(doc->extSubset->pentities, cur->name) == cur)
3814 xmlHashRemoveEntry(doc->extSubset->pentities, cur->name,
3815 NULL);
3816 }
3817 }
3818 }
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003819 if (cur->parent != NULL) {
3820 xmlNodePtr parent;
3821 parent = cur->parent;
3822 if (cur->type == XML_ATTRIBUTE_NODE) {
3823 if (parent->properties == (xmlAttrPtr) cur)
3824 parent->properties = ((xmlAttrPtr) cur)->next;
3825 } else {
3826 if (parent->children == cur)
3827 parent->children = cur->next;
3828 if (parent->last == cur)
3829 parent->last = cur->prev;
3830 }
3831 cur->parent = NULL;
3832 }
Owen Taylor3473f882001-02-23 17:55:21 +00003833 if (cur->next != NULL)
3834 cur->next->prev = cur->prev;
3835 if (cur->prev != NULL)
3836 cur->prev->next = cur->next;
3837 cur->next = cur->prev = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00003838}
3839
Daniel Veillard2156d432004-03-04 15:59:36 +00003840#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00003841/**
3842 * xmlReplaceNode:
3843 * @old: the old node
3844 * @cur: the node
3845 *
William M. Brackd7cf7f82003-11-14 07:13:16 +00003846 * Unlink the old node from its current context, prune the new one
Daniel Veillardd1640922001-12-17 15:30:10 +00003847 * at the same place. If @cur was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00003848 * first unlinked from its existing context.
3849 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003850 * Returns the @old node
Owen Taylor3473f882001-02-23 17:55:21 +00003851 */
3852xmlNodePtr
3853xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) {
Daniel Veillardce244ad2004-11-05 10:03:46 +00003854 if (old == cur) return(NULL);
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003855 if ((old == NULL) || (old->type == XML_NAMESPACE_DECL) ||
3856 (old->parent == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003857#ifdef DEBUG_TREE
3858 xmlGenericError(xmlGenericErrorContext,
Daniel Veillarda03e3652004-11-02 18:45:30 +00003859 "xmlReplaceNode : old == NULL or without parent\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003860#endif
3861 return(NULL);
3862 }
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003863 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003864 xmlUnlinkNode(old);
3865 return(old);
3866 }
3867 if (cur == old) {
3868 return(old);
3869 }
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003870 if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) {
3871#ifdef DEBUG_TREE
3872 xmlGenericError(xmlGenericErrorContext,
3873 "xmlReplaceNode : Trying to replace attribute node with other node type\n");
3874#endif
3875 return(old);
3876 }
3877 if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) {
3878#ifdef DEBUG_TREE
3879 xmlGenericError(xmlGenericErrorContext,
3880 "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n");
3881#endif
3882 return(old);
3883 }
Owen Taylor3473f882001-02-23 17:55:21 +00003884 xmlUnlinkNode(cur);
Daniel Veillard64d7d122005-05-11 18:03:42 +00003885 xmlSetTreeDoc(cur, old->doc);
Owen Taylor3473f882001-02-23 17:55:21 +00003886 cur->parent = old->parent;
3887 cur->next = old->next;
3888 if (cur->next != NULL)
3889 cur->next->prev = cur;
3890 cur->prev = old->prev;
3891 if (cur->prev != NULL)
3892 cur->prev->next = cur;
3893 if (cur->parent != NULL) {
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003894 if (cur->type == XML_ATTRIBUTE_NODE) {
3895 if (cur->parent->properties == (xmlAttrPtr)old)
3896 cur->parent->properties = ((xmlAttrPtr) cur);
3897 } else {
3898 if (cur->parent->children == old)
3899 cur->parent->children = cur;
3900 if (cur->parent->last == old)
3901 cur->parent->last = cur;
3902 }
Owen Taylor3473f882001-02-23 17:55:21 +00003903 }
3904 old->next = old->prev = NULL;
3905 old->parent = NULL;
3906 return(old);
3907}
Daniel Veillard652327a2003-09-29 18:02:38 +00003908#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00003909
3910/************************************************************************
3911 * *
3912 * Copy operations *
3913 * *
3914 ************************************************************************/
Daniel Veillardaa6de472008-08-25 14:53:31 +00003915
Owen Taylor3473f882001-02-23 17:55:21 +00003916/**
3917 * xmlCopyNamespace:
3918 * @cur: the namespace
3919 *
3920 * Do a copy of the namespace.
3921 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003922 * Returns: a new #xmlNsPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003923 */
3924xmlNsPtr
3925xmlCopyNamespace(xmlNsPtr cur) {
3926 xmlNsPtr ret;
3927
3928 if (cur == NULL) return(NULL);
3929 switch (cur->type) {
3930 case XML_LOCAL_NAMESPACE:
3931 ret = xmlNewNs(NULL, cur->href, cur->prefix);
3932 break;
3933 default:
3934#ifdef DEBUG_TREE
3935 xmlGenericError(xmlGenericErrorContext,
3936 "xmlCopyNamespace: invalid type %d\n", cur->type);
3937#endif
3938 return(NULL);
3939 }
3940 return(ret);
3941}
3942
3943/**
3944 * xmlCopyNamespaceList:
3945 * @cur: the first namespace
3946 *
3947 * Do a copy of an namespace list.
3948 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003949 * Returns: a new #xmlNsPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003950 */
3951xmlNsPtr
3952xmlCopyNamespaceList(xmlNsPtr cur) {
3953 xmlNsPtr ret = NULL;
3954 xmlNsPtr p = NULL,q;
3955
3956 while (cur != NULL) {
3957 q = xmlCopyNamespace(cur);
3958 if (p == NULL) {
3959 ret = p = q;
3960 } else {
3961 p->next = q;
3962 p = q;
3963 }
3964 cur = cur->next;
3965 }
3966 return(ret);
3967}
3968
3969static xmlNodePtr
3970xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent);
Rob Richards19dc9612005-10-28 16:15:16 +00003971
3972static xmlAttrPtr
3973xmlCopyPropInternal(xmlDocPtr doc, xmlNodePtr target, xmlAttrPtr cur) {
Owen Taylor3473f882001-02-23 17:55:21 +00003974 xmlAttrPtr ret;
3975
3976 if (cur == NULL) return(NULL);
Daniel Veillard3e62adb2012-08-09 14:24:02 +08003977 if ((target != NULL) && (target->type != XML_ELEMENT_NODE))
3978 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003979 if (target != NULL)
3980 ret = xmlNewDocProp(target->doc, cur->name, NULL);
Rob Richards19dc9612005-10-28 16:15:16 +00003981 else if (doc != NULL)
3982 ret = xmlNewDocProp(doc, cur->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003983 else if (cur->parent != NULL)
3984 ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL);
3985 else if (cur->children != NULL)
3986 ret = xmlNewDocProp(cur->children->doc, cur->name, NULL);
3987 else
3988 ret = xmlNewDocProp(NULL, cur->name, NULL);
3989 if (ret == NULL) return(NULL);
3990 ret->parent = target;
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00003991
Owen Taylor3473f882001-02-23 17:55:21 +00003992 if ((cur->ns != NULL) && (target != NULL)) {
Daniel Veillard8107a222002-01-13 14:10:10 +00003993 xmlNsPtr ns;
Daniel Veillard652327a2003-09-29 18:02:38 +00003994
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00003995 ns = xmlSearchNs(target->doc, target, cur->ns->prefix);
3996 if (ns == NULL) {
3997 /*
3998 * Humm, we are copying an element whose namespace is defined
3999 * out of the new tree scope. Search it in the original tree
4000 * and add it at the top of the new tree
4001 */
4002 ns = xmlSearchNs(cur->doc, cur->parent, cur->ns->prefix);
4003 if (ns != NULL) {
4004 xmlNodePtr root = target;
4005 xmlNodePtr pred = NULL;
4006
4007 while (root->parent != NULL) {
4008 pred = root;
4009 root = root->parent;
4010 }
4011 if (root == (xmlNodePtr) target->doc) {
4012 /* correct possibly cycling above the document elt */
4013 root = pred;
4014 }
4015 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
4016 }
4017 } else {
4018 /*
4019 * we have to find something appropriate here since
4020 * we cant be sure, that the namespce we found is identified
4021 * by the prefix
4022 */
Daniel Veillard044fc6b2002-03-04 17:09:44 +00004023 if (xmlStrEqual(ns->href, cur->ns->href)) {
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00004024 /* this is the nice case */
4025 ret->ns = ns;
4026 } else {
4027 /*
4028 * we are in trouble: we need a new reconcilied namespace.
4029 * This is expensive
4030 */
4031 ret->ns = xmlNewReconciliedNs(target->doc, target, cur->ns);
4032 }
4033 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00004034
Owen Taylor3473f882001-02-23 17:55:21 +00004035 } else
4036 ret->ns = NULL;
4037
4038 if (cur->children != NULL) {
4039 xmlNodePtr tmp;
4040
4041 ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret);
4042 ret->last = NULL;
4043 tmp = ret->children;
4044 while (tmp != NULL) {
4045 /* tmp->parent = (xmlNodePtr)ret; */
4046 if (tmp->next == NULL)
4047 ret->last = tmp;
4048 tmp = tmp->next;
4049 }
4050 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00004051 /*
4052 * Try to handle IDs
4053 */
Daniel Veillarda3db2e32002-03-08 15:46:57 +00004054 if ((target!= NULL) && (cur!= NULL) &&
4055 (target->doc != NULL) && (cur->doc != NULL) &&
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00004056 (cur->doc->ids != NULL) && (cur->parent != NULL)) {
4057 if (xmlIsID(cur->doc, cur->parent, cur)) {
4058 xmlChar *id;
4059
4060 id = xmlNodeListGetString(cur->doc, cur->children, 1);
4061 if (id != NULL) {
4062 xmlAddID(NULL, target->doc, id, ret);
4063 xmlFree(id);
4064 }
4065 }
4066 }
Owen Taylor3473f882001-02-23 17:55:21 +00004067 return(ret);
4068}
4069
4070/**
Rob Richards19dc9612005-10-28 16:15:16 +00004071 * xmlCopyProp:
4072 * @target: the element where the attribute will be grafted
4073 * @cur: the attribute
4074 *
4075 * Do a copy of the attribute.
4076 *
4077 * Returns: a new #xmlAttrPtr, or NULL in case of error.
4078 */
4079xmlAttrPtr
4080xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) {
4081 return xmlCopyPropInternal(NULL, target, cur);
4082}
4083
4084/**
Owen Taylor3473f882001-02-23 17:55:21 +00004085 * xmlCopyPropList:
4086 * @target: the element where the attributes will be grafted
4087 * @cur: the first attribute
4088 *
4089 * Do a copy of an attribute list.
4090 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004091 * Returns: a new #xmlAttrPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004092 */
4093xmlAttrPtr
4094xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) {
4095 xmlAttrPtr ret = NULL;
4096 xmlAttrPtr p = NULL,q;
4097
Daniel Veillard3e62adb2012-08-09 14:24:02 +08004098 if ((target != NULL) && (target->type != XML_ELEMENT_NODE))
4099 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004100 while (cur != NULL) {
4101 q = xmlCopyProp(target, cur);
William M. Brack13dfa872004-09-18 04:52:08 +00004102 if (q == NULL)
4103 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004104 if (p == NULL) {
4105 ret = p = q;
4106 } else {
4107 p->next = q;
4108 q->prev = p;
4109 p = q;
4110 }
4111 cur = cur->next;
4112 }
4113 return(ret);
4114}
4115
4116/*
Daniel Veillardd1640922001-12-17 15:30:10 +00004117 * NOTE about the CopyNode operations !
Owen Taylor3473f882001-02-23 17:55:21 +00004118 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004119 * They are split into external and internal parts for one
Owen Taylor3473f882001-02-23 17:55:21 +00004120 * tricky reason: namespaces. Doing a direct copy of a node
4121 * say RPM:Copyright without changing the namespace pointer to
4122 * something else can produce stale links. One way to do it is
4123 * to keep a reference counter but this doesn't work as soon
4124 * as one move the element or the subtree out of the scope of
4125 * the existing namespace. The actual solution seems to add
4126 * a copy of the namespace at the top of the copied tree if
4127 * not available in the subtree.
4128 * Hence two functions, the public front-end call the inner ones
William M. Brack57e9e912004-03-09 16:19:02 +00004129 * The argument "recursive" normally indicates a recursive copy
4130 * of the node with values 0 (no) and 1 (yes). For XInclude,
4131 * however, we allow a value of 2 to indicate copy properties and
4132 * namespace info, but don't recurse on children.
Owen Taylor3473f882001-02-23 17:55:21 +00004133 */
4134
4135static xmlNodePtr
Daniel Veillard3ec4c612001-08-28 20:39:49 +00004136xmlStaticCopyNode(const xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent,
William M. Brack57e9e912004-03-09 16:19:02 +00004137 int extended) {
Owen Taylor3473f882001-02-23 17:55:21 +00004138 xmlNodePtr ret;
4139
4140 if (node == NULL) return(NULL);
Daniel Veillard39196eb2001-06-19 18:09:42 +00004141 switch (node->type) {
4142 case XML_TEXT_NODE:
4143 case XML_CDATA_SECTION_NODE:
4144 case XML_ELEMENT_NODE:
Daniel Veillardec6725e2002-09-05 11:12:45 +00004145 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00004146 case XML_ENTITY_REF_NODE:
4147 case XML_ENTITY_NODE:
4148 case XML_PI_NODE:
4149 case XML_COMMENT_NODE:
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00004150 case XML_XINCLUDE_START:
4151 case XML_XINCLUDE_END:
4152 break;
4153 case XML_ATTRIBUTE_NODE:
Rob Richards19dc9612005-10-28 16:15:16 +00004154 return((xmlNodePtr) xmlCopyPropInternal(doc, parent, (xmlAttrPtr) node));
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00004155 case XML_NAMESPACE_DECL:
4156 return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node));
Daniel Veillardaa6de472008-08-25 14:53:31 +00004157
Daniel Veillard39196eb2001-06-19 18:09:42 +00004158 case XML_DOCUMENT_NODE:
4159 case XML_HTML_DOCUMENT_NODE:
4160#ifdef LIBXML_DOCB_ENABLED
4161 case XML_DOCB_DOCUMENT_NODE:
4162#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00004163#ifdef LIBXML_TREE_ENABLED
William M. Brack57e9e912004-03-09 16:19:02 +00004164 return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, extended));
Daniel Veillard652327a2003-09-29 18:02:38 +00004165#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard39196eb2001-06-19 18:09:42 +00004166 case XML_DOCUMENT_TYPE_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00004167 case XML_NOTATION_NODE:
4168 case XML_DTD_NODE:
4169 case XML_ELEMENT_DECL:
4170 case XML_ATTRIBUTE_DECL:
4171 case XML_ENTITY_DECL:
4172 return(NULL);
4173 }
Daniel Veillardb33c2012001-04-25 12:59:04 +00004174
Owen Taylor3473f882001-02-23 17:55:21 +00004175 /*
4176 * Allocate a new node and fill the fields.
4177 */
4178 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
4179 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004180 xmlTreeErrMemory("copying node");
Owen Taylor3473f882001-02-23 17:55:21 +00004181 return(NULL);
4182 }
4183 memset(ret, 0, sizeof(xmlNode));
4184 ret->type = node->type;
4185
4186 ret->doc = doc;
Daniel Veillardaa6de472008-08-25 14:53:31 +00004187 ret->parent = parent;
Owen Taylor3473f882001-02-23 17:55:21 +00004188 if (node->name == xmlStringText)
4189 ret->name = xmlStringText;
4190 else if (node->name == xmlStringTextNoenc)
4191 ret->name = xmlStringTextNoenc;
4192 else if (node->name == xmlStringComment)
4193 ret->name = xmlStringComment;
Daniel Veillard03a53c32004-10-26 16:06:51 +00004194 else if (node->name != NULL) {
4195 if ((doc != NULL) && (doc->dict != NULL))
4196 ret->name = xmlDictLookup(doc->dict, node->name, -1);
4197 else
4198 ret->name = xmlStrdup(node->name);
4199 }
Daniel Veillard7db37732001-07-12 01:20:08 +00004200 if ((node->type != XML_ELEMENT_NODE) &&
4201 (node->content != NULL) &&
4202 (node->type != XML_ENTITY_REF_NODE) &&
4203 (node->type != XML_XINCLUDE_END) &&
4204 (node->type != XML_XINCLUDE_START)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004205 ret->content = xmlStrdup(node->content);
Daniel Veillard8107a222002-01-13 14:10:10 +00004206 }else{
4207 if (node->type == XML_ELEMENT_NODE)
Daniel Veillard3e35f8e2003-10-21 00:05:38 +00004208 ret->line = node->line;
Owen Taylor3473f882001-02-23 17:55:21 +00004209 }
Daniel Veillardacb2bda2002-01-13 16:15:43 +00004210 if (parent != NULL) {
4211 xmlNodePtr tmp;
4212
Daniel Veillard8a1b1852003-01-05 22:37:17 +00004213 /*
4214 * this is a tricky part for the node register thing:
4215 * in case ret does get coalesced in xmlAddChild
4216 * the deregister-node callback is called; so we register ret now already
4217 */
Daniel Veillarda880b122003-04-21 21:36:41 +00004218 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00004219 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
4220
Daniel Veillardacb2bda2002-01-13 16:15:43 +00004221 tmp = xmlAddChild(parent, ret);
4222 /* node could have coalesced */
4223 if (tmp != ret)
4224 return(tmp);
4225 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00004226
William M. Brack57e9e912004-03-09 16:19:02 +00004227 if (!extended)
Daniel Veillard8a1b1852003-01-05 22:37:17 +00004228 goto out;
Petr Pajas2afca4a2009-07-30 17:47:32 +02004229 if (((node->type == XML_ELEMENT_NODE) ||
4230 (node->type == XML_XINCLUDE_START)) && (node->nsDef != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00004231 ret->nsDef = xmlCopyNamespaceList(node->nsDef);
4232
4233 if (node->ns != NULL) {
4234 xmlNsPtr ns;
4235
4236 ns = xmlSearchNs(doc, ret, node->ns->prefix);
4237 if (ns == NULL) {
4238 /*
4239 * Humm, we are copying an element whose namespace is defined
4240 * out of the new tree scope. Search it in the original tree
4241 * and add it at the top of the new tree
4242 */
4243 ns = xmlSearchNs(node->doc, node, node->ns->prefix);
4244 if (ns != NULL) {
4245 xmlNodePtr root = ret;
4246
4247 while (root->parent != NULL) root = root->parent;
Daniel Veillarde82a9922001-04-22 12:12:58 +00004248 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
Rob Richardsddb01cb2010-01-29 13:32:12 -05004249 } else {
4250 ret->ns = xmlNewReconciliedNs(doc, ret, node->ns);
Owen Taylor3473f882001-02-23 17:55:21 +00004251 }
4252 } else {
4253 /*
4254 * reference the existing namespace definition in our own tree.
4255 */
4256 ret->ns = ns;
4257 }
4258 }
Petr Pajas2afca4a2009-07-30 17:47:32 +02004259 if (((node->type == XML_ELEMENT_NODE) ||
4260 (node->type == XML_XINCLUDE_START)) && (node->properties != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00004261 ret->properties = xmlCopyPropList(ret, node->properties);
Daniel Veillardb33c2012001-04-25 12:59:04 +00004262 if (node->type == XML_ENTITY_REF_NODE) {
4263 if ((doc == NULL) || (node->doc != doc)) {
4264 /*
4265 * The copied node will go into a separate document, so
Daniel Veillardd1640922001-12-17 15:30:10 +00004266 * to avoid dangling references to the ENTITY_DECL node
Daniel Veillardb33c2012001-04-25 12:59:04 +00004267 * we cannot keep the reference. Try to find it in the
4268 * target document.
4269 */
4270 ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name);
4271 } else {
4272 ret->children = node->children;
4273 }
Daniel Veillard0ec98632001-11-14 15:04:32 +00004274 ret->last = ret->children;
William M. Brack57e9e912004-03-09 16:19:02 +00004275 } else if ((node->children != NULL) && (extended != 2)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004276 ret->children = xmlStaticCopyNodeList(node->children, doc, ret);
Daniel Veillard0ec98632001-11-14 15:04:32 +00004277 UPDATE_LAST_CHILD_AND_PARENT(ret)
4278 }
Daniel Veillard8a1b1852003-01-05 22:37:17 +00004279
4280out:
4281 /* if parent != NULL we already registered the node above */
Daniel Veillardac996a12004-07-30 12:02:58 +00004282 if ((parent == NULL) &&
4283 ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00004284 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
Owen Taylor3473f882001-02-23 17:55:21 +00004285 return(ret);
4286}
4287
4288static xmlNodePtr
4289xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) {
4290 xmlNodePtr ret = NULL;
4291 xmlNodePtr p = NULL,q;
4292
4293 while (node != NULL) {
Daniel Veillard652327a2003-09-29 18:02:38 +00004294#ifdef LIBXML_TREE_ENABLED
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00004295 if (node->type == XML_DTD_NODE ) {
Daniel Veillard4497e692001-06-09 14:19:02 +00004296 if (doc == NULL) {
4297 node = node->next;
4298 continue;
4299 }
Daniel Veillardb33c2012001-04-25 12:59:04 +00004300 if (doc->intSubset == NULL) {
4301 q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node );
Gaurav98a4e712013-11-29 23:28:21 +08004302 if (q == NULL) return(NULL);
Daniel Veillardb33c2012001-04-25 12:59:04 +00004303 q->doc = doc;
4304 q->parent = parent;
4305 doc->intSubset = (xmlDtdPtr) q;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004306 xmlAddChild(parent, q);
Daniel Veillardb33c2012001-04-25 12:59:04 +00004307 } else {
4308 q = (xmlNodePtr) doc->intSubset;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004309 xmlAddChild(parent, q);
Daniel Veillardb33c2012001-04-25 12:59:04 +00004310 }
4311 } else
Daniel Veillard652327a2003-09-29 18:02:38 +00004312#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardb33c2012001-04-25 12:59:04 +00004313 q = xmlStaticCopyNode(node, doc, parent, 1);
Gaurav98a4e712013-11-29 23:28:21 +08004314 if (q == NULL) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004315 if (ret == NULL) {
4316 q->prev = NULL;
4317 ret = p = q;
Daniel Veillardacb2bda2002-01-13 16:15:43 +00004318 } else if (p != q) {
4319 /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */
Owen Taylor3473f882001-02-23 17:55:21 +00004320 p->next = q;
4321 q->prev = p;
4322 p = q;
4323 }
4324 node = node->next;
4325 }
4326 return(ret);
4327}
4328
4329/**
4330 * xmlCopyNode:
4331 * @node: the node
William M. Brack57e9e912004-03-09 16:19:02 +00004332 * @extended: if 1 do a recursive copy (properties, namespaces and children
4333 * when applicable)
4334 * if 2 copy properties and namespaces (when applicable)
Owen Taylor3473f882001-02-23 17:55:21 +00004335 *
4336 * Do a copy of the node.
4337 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004338 * Returns: a new #xmlNodePtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004339 */
4340xmlNodePtr
William M. Brack57e9e912004-03-09 16:19:02 +00004341xmlCopyNode(const xmlNodePtr node, int extended) {
Owen Taylor3473f882001-02-23 17:55:21 +00004342 xmlNodePtr ret;
4343
William M. Brack57e9e912004-03-09 16:19:02 +00004344 ret = xmlStaticCopyNode(node, NULL, NULL, extended);
Owen Taylor3473f882001-02-23 17:55:21 +00004345 return(ret);
4346}
4347
4348/**
Daniel Veillard82daa812001-04-12 08:55:36 +00004349 * xmlDocCopyNode:
4350 * @node: the node
Daniel Veillardd1640922001-12-17 15:30:10 +00004351 * @doc: the document
William M. Brack57e9e912004-03-09 16:19:02 +00004352 * @extended: if 1 do a recursive copy (properties, namespaces and children
4353 * when applicable)
4354 * if 2 copy properties and namespaces (when applicable)
Daniel Veillard82daa812001-04-12 08:55:36 +00004355 *
4356 * Do a copy of the node to a given document.
4357 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004358 * Returns: a new #xmlNodePtr, or NULL in case of error.
Daniel Veillard82daa812001-04-12 08:55:36 +00004359 */
4360xmlNodePtr
William M. Brack57e9e912004-03-09 16:19:02 +00004361xmlDocCopyNode(const xmlNodePtr node, xmlDocPtr doc, int extended) {
Daniel Veillard82daa812001-04-12 08:55:36 +00004362 xmlNodePtr ret;
4363
William M. Brack57e9e912004-03-09 16:19:02 +00004364 ret = xmlStaticCopyNode(node, doc, NULL, extended);
Daniel Veillard82daa812001-04-12 08:55:36 +00004365 return(ret);
4366}
4367
4368/**
Daniel Veillard03a53c32004-10-26 16:06:51 +00004369 * xmlDocCopyNodeList:
4370 * @doc: the target document
4371 * @node: the first node in the list.
4372 *
4373 * Do a recursive copy of the node list.
4374 *
4375 * Returns: a new #xmlNodePtr, or NULL in case of error.
4376 */
4377xmlNodePtr xmlDocCopyNodeList(xmlDocPtr doc, const xmlNodePtr node) {
4378 xmlNodePtr ret = xmlStaticCopyNodeList(node, doc, NULL);
4379 return(ret);
4380}
4381
4382/**
Owen Taylor3473f882001-02-23 17:55:21 +00004383 * xmlCopyNodeList:
4384 * @node: the first node in the list.
4385 *
4386 * Do a recursive copy of the node list.
Daniel Veillard03a53c32004-10-26 16:06:51 +00004387 * Use xmlDocCopyNodeList() if possible to ensure string interning.
Owen Taylor3473f882001-02-23 17:55:21 +00004388 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004389 * Returns: a new #xmlNodePtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004390 */
Daniel Veillard3ec4c612001-08-28 20:39:49 +00004391xmlNodePtr xmlCopyNodeList(const xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00004392 xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL);
4393 return(ret);
4394}
4395
Daniel Veillard2156d432004-03-04 15:59:36 +00004396#if defined(LIBXML_TREE_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004397/**
Owen Taylor3473f882001-02-23 17:55:21 +00004398 * xmlCopyDtd:
4399 * @dtd: the dtd
4400 *
4401 * Do a copy of the dtd.
4402 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004403 * Returns: a new #xmlDtdPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004404 */
4405xmlDtdPtr
4406xmlCopyDtd(xmlDtdPtr dtd) {
4407 xmlDtdPtr ret;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004408 xmlNodePtr cur, p = NULL, q;
Owen Taylor3473f882001-02-23 17:55:21 +00004409
4410 if (dtd == NULL) return(NULL);
4411 ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID);
4412 if (ret == NULL) return(NULL);
4413 if (dtd->entities != NULL)
4414 ret->entities = (void *) xmlCopyEntitiesTable(
4415 (xmlEntitiesTablePtr) dtd->entities);
4416 if (dtd->notations != NULL)
4417 ret->notations = (void *) xmlCopyNotationTable(
4418 (xmlNotationTablePtr) dtd->notations);
4419 if (dtd->elements != NULL)
4420 ret->elements = (void *) xmlCopyElementTable(
4421 (xmlElementTablePtr) dtd->elements);
4422 if (dtd->attributes != NULL)
4423 ret->attributes = (void *) xmlCopyAttributeTable(
4424 (xmlAttributeTablePtr) dtd->attributes);
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004425 if (dtd->pentities != NULL)
4426 ret->pentities = (void *) xmlCopyEntitiesTable(
4427 (xmlEntitiesTablePtr) dtd->pentities);
Daniel Veillardaa6de472008-08-25 14:53:31 +00004428
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004429 cur = dtd->children;
4430 while (cur != NULL) {
4431 q = NULL;
4432
4433 if (cur->type == XML_ENTITY_DECL) {
4434 xmlEntityPtr tmp = (xmlEntityPtr) cur;
4435 switch (tmp->etype) {
4436 case XML_INTERNAL_GENERAL_ENTITY:
4437 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
4438 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
4439 q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name);
4440 break;
4441 case XML_INTERNAL_PARAMETER_ENTITY:
4442 case XML_EXTERNAL_PARAMETER_ENTITY:
Daniel Veillardaa6de472008-08-25 14:53:31 +00004443 q = (xmlNodePtr)
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004444 xmlGetParameterEntityFromDtd(ret, tmp->name);
4445 break;
4446 case XML_INTERNAL_PREDEFINED_ENTITY:
4447 break;
4448 }
4449 } else if (cur->type == XML_ELEMENT_DECL) {
4450 xmlElementPtr tmp = (xmlElementPtr) cur;
4451 q = (xmlNodePtr)
4452 xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix);
4453 } else if (cur->type == XML_ATTRIBUTE_DECL) {
4454 xmlAttributePtr tmp = (xmlAttributePtr) cur;
Daniel Veillardaa6de472008-08-25 14:53:31 +00004455 q = (xmlNodePtr)
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004456 xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix);
4457 } else if (cur->type == XML_COMMENT_NODE) {
4458 q = xmlCopyNode(cur, 0);
4459 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00004460
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004461 if (q == NULL) {
4462 cur = cur->next;
4463 continue;
4464 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00004465
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004466 if (p == NULL)
4467 ret->children = q;
4468 else
Daniel Veillardaa6de472008-08-25 14:53:31 +00004469 p->next = q;
4470
4471 q->prev = p;
4472 q->parent = (xmlNodePtr) ret;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004473 q->next = NULL;
4474 ret->last = q;
Daniel Veillardaa6de472008-08-25 14:53:31 +00004475 p = q;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004476 cur = cur->next;
4477 }
4478
Owen Taylor3473f882001-02-23 17:55:21 +00004479 return(ret);
4480}
Daniel Veillard2156d432004-03-04 15:59:36 +00004481#endif
Owen Taylor3473f882001-02-23 17:55:21 +00004482
Daniel Veillard2156d432004-03-04 15:59:36 +00004483#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004484/**
4485 * xmlCopyDoc:
4486 * @doc: the document
William M. Brack57e9e912004-03-09 16:19:02 +00004487 * @recursive: if not zero do a recursive copy.
Owen Taylor3473f882001-02-23 17:55:21 +00004488 *
4489 * Do a copy of the document info. If recursive, the content tree will
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004490 * be copied too as well as DTD, namespaces and entities.
Owen Taylor3473f882001-02-23 17:55:21 +00004491 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004492 * Returns: a new #xmlDocPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004493 */
4494xmlDocPtr
4495xmlCopyDoc(xmlDocPtr doc, int recursive) {
4496 xmlDocPtr ret;
4497
4498 if (doc == NULL) return(NULL);
4499 ret = xmlNewDoc(doc->version);
4500 if (ret == NULL) return(NULL);
4501 if (doc->name != NULL)
4502 ret->name = xmlMemStrdup(doc->name);
4503 if (doc->encoding != NULL)
4504 ret->encoding = xmlStrdup(doc->encoding);
Daniel Veillardf59507d2005-01-27 17:26:49 +00004505 if (doc->URL != NULL)
4506 ret->URL = xmlStrdup(doc->URL);
Owen Taylor3473f882001-02-23 17:55:21 +00004507 ret->charset = doc->charset;
4508 ret->compression = doc->compression;
4509 ret->standalone = doc->standalone;
4510 if (!recursive) return(ret);
4511
Daniel Veillardb33c2012001-04-25 12:59:04 +00004512 ret->last = NULL;
4513 ret->children = NULL;
Daniel Veillard2156d432004-03-04 15:59:36 +00004514#ifdef LIBXML_TREE_ENABLED
Daniel Veillardb33c2012001-04-25 12:59:04 +00004515 if (doc->intSubset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004516 ret->intSubset = xmlCopyDtd(doc->intSubset);
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004517 xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret);
Daniel Veillardb33c2012001-04-25 12:59:04 +00004518 ret->intSubset->parent = ret;
4519 }
Daniel Veillard2156d432004-03-04 15:59:36 +00004520#endif
Owen Taylor3473f882001-02-23 17:55:21 +00004521 if (doc->oldNs != NULL)
4522 ret->oldNs = xmlCopyNamespaceList(doc->oldNs);
4523 if (doc->children != NULL) {
4524 xmlNodePtr tmp;
Daniel Veillardaa6de472008-08-25 14:53:31 +00004525
Daniel Veillardb33c2012001-04-25 12:59:04 +00004526 ret->children = xmlStaticCopyNodeList(doc->children, ret,
4527 (xmlNodePtr)ret);
Owen Taylor3473f882001-02-23 17:55:21 +00004528 ret->last = NULL;
4529 tmp = ret->children;
4530 while (tmp != NULL) {
4531 if (tmp->next == NULL)
4532 ret->last = tmp;
4533 tmp = tmp->next;
4534 }
4535 }
4536 return(ret);
4537}
Daniel Veillard652327a2003-09-29 18:02:38 +00004538#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004539
4540/************************************************************************
4541 * *
4542 * Content access functions *
4543 * *
4544 ************************************************************************/
Daniel Veillardaa6de472008-08-25 14:53:31 +00004545
Owen Taylor3473f882001-02-23 17:55:21 +00004546/**
Daniel Veillard968a03a2012-08-13 12:41:33 +08004547 * xmlGetLineNoInternal:
Daniel Veillard01c13b52002-12-10 15:19:08 +00004548 * @node: valid node
Daniel Veillard968a03a2012-08-13 12:41:33 +08004549 * @depth: used to limit any risk of recursion
Daniel Veillard8faa7832001-11-26 15:58:08 +00004550 *
Daniel Veillard968a03a2012-08-13 12:41:33 +08004551 * Get line number of @node.
4552 * Try to override the limitation of lines being store in 16 bits ints
Daniel Veillard8faa7832001-11-26 15:58:08 +00004553 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004554 * Returns the line number if successful, -1 otherwise
Daniel Veillard8faa7832001-11-26 15:58:08 +00004555 */
Daniel Veillard968a03a2012-08-13 12:41:33 +08004556static long
4557xmlGetLineNoInternal(xmlNodePtr node, int depth)
Daniel Veillard8faa7832001-11-26 15:58:08 +00004558{
4559 long result = -1;
4560
Daniel Veillard968a03a2012-08-13 12:41:33 +08004561 if (depth >= 5)
4562 return(-1);
4563
Daniel Veillard8faa7832001-11-26 15:58:08 +00004564 if (!node)
4565 return result;
Daniel Veillard73da77e2005-08-24 14:05:37 +00004566 if ((node->type == XML_ELEMENT_NODE) ||
4567 (node->type == XML_TEXT_NODE) ||
4568 (node->type == XML_COMMENT_NODE) ||
Daniel Veillard968a03a2012-08-13 12:41:33 +08004569 (node->type == XML_PI_NODE)) {
4570 if (node->line == 65535) {
4571 if ((node->type == XML_TEXT_NODE) && (node->psvi != NULL))
4572 result = (long) node->psvi;
4573 else if ((node->type == XML_ELEMENT_NODE) &&
4574 (node->children != NULL))
4575 result = xmlGetLineNoInternal(node->children, depth + 1);
4576 else if (node->next != NULL)
4577 result = xmlGetLineNoInternal(node->next, depth + 1);
4578 else if (node->prev != NULL)
4579 result = xmlGetLineNoInternal(node->prev, depth + 1);
4580 }
4581 if ((result == -1) || (result == 65535))
4582 result = (long) node->line;
4583 } else if ((node->prev != NULL) &&
Daniel Veillard8faa7832001-11-26 15:58:08 +00004584 ((node->prev->type == XML_ELEMENT_NODE) ||
Daniel Veillard73da77e2005-08-24 14:05:37 +00004585 (node->prev->type == XML_TEXT_NODE) ||
4586 (node->prev->type == XML_COMMENT_NODE) ||
4587 (node->prev->type == XML_PI_NODE)))
Daniel Veillard968a03a2012-08-13 12:41:33 +08004588 result = xmlGetLineNoInternal(node->prev, depth + 1);
Daniel Veillard8faa7832001-11-26 15:58:08 +00004589 else if ((node->parent != NULL) &&
Daniel Veillard73da77e2005-08-24 14:05:37 +00004590 (node->parent->type == XML_ELEMENT_NODE))
Daniel Veillard968a03a2012-08-13 12:41:33 +08004591 result = xmlGetLineNoInternal(node->parent, depth + 1);
Daniel Veillard8faa7832001-11-26 15:58:08 +00004592
4593 return result;
4594}
4595
Daniel Veillard968a03a2012-08-13 12:41:33 +08004596/**
4597 * xmlGetLineNo:
4598 * @node: valid node
4599 *
4600 * Get line number of @node.
4601 * Try to override the limitation of lines being store in 16 bits ints
4602 * if XML_PARSE_BIG_LINES parser option was used
4603 *
4604 * Returns the line number if successful, -1 otherwise
4605 */
4606long
4607xmlGetLineNo(xmlNodePtr node)
4608{
4609 return(xmlGetLineNoInternal(node, 0));
4610}
4611
Daniel Veillard2156d432004-03-04 15:59:36 +00004612#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED)
Daniel Veillard8faa7832001-11-26 15:58:08 +00004613/**
4614 * xmlGetNodePath:
4615 * @node: a node
4616 *
4617 * Build a structure based Path for the given node
4618 *
4619 * Returns the new path or NULL in case of error. The caller must free
4620 * the returned string
4621 */
4622xmlChar *
4623xmlGetNodePath(xmlNodePtr node)
4624{
4625 xmlNodePtr cur, tmp, next;
4626 xmlChar *buffer = NULL, *temp;
4627 size_t buf_len;
4628 xmlChar *buf;
Daniel Veillard96c3a3b2002-10-14 15:39:04 +00004629 const char *sep;
Daniel Veillard8faa7832001-11-26 15:58:08 +00004630 const char *name;
4631 char nametemp[100];
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004632 int occur = 0, generic;
Daniel Veillard8faa7832001-11-26 15:58:08 +00004633
Daniel Veillard3e62adb2012-08-09 14:24:02 +08004634 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004635 return (NULL);
4636
4637 buf_len = 500;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004638 buffer = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004639 if (buffer == NULL) {
4640 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004641 return (NULL);
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004642 }
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004643 buf = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
Daniel Veillard8faa7832001-11-26 15:58:08 +00004644 if (buf == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004645 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004646 xmlFree(buffer);
4647 return (NULL);
4648 }
4649
4650 buffer[0] = 0;
4651 cur = node;
4652 do {
4653 name = "";
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004654 sep = "?";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004655 occur = 0;
4656 if ((cur->type == XML_DOCUMENT_NODE) ||
4657 (cur->type == XML_HTML_DOCUMENT_NODE)) {
4658 if (buffer[0] == '/')
4659 break;
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004660 sep = "/";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004661 next = NULL;
4662 } else if (cur->type == XML_ELEMENT_NODE) {
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004663 generic = 0;
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004664 sep = "/";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004665 name = (const char *) cur->name;
4666 if (cur->ns) {
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004667 if (cur->ns->prefix != NULL) {
William M. Brack13dfa872004-09-18 04:52:08 +00004668 snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
Daniel Veillardaa6de472008-08-25 14:53:31 +00004669 (char *)cur->ns->prefix, (char *)cur->name);
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004670 nametemp[sizeof(nametemp) - 1] = 0;
4671 name = nametemp;
4672 } else {
4673 /*
4674 * We cannot express named elements in the default
4675 * namespace, so use "*".
4676 */
4677 generic = 1;
4678 name = "*";
Daniel Veillardaa6de472008-08-25 14:53:31 +00004679 }
Daniel Veillard8faa7832001-11-26 15:58:08 +00004680 }
4681 next = cur->parent;
4682
4683 /*
4684 * Thumbler index computation
Daniel Veillardc00cda82003-04-07 10:22:39 +00004685 * TODO: the ocurence test seems bogus for namespaced names
Daniel Veillard8faa7832001-11-26 15:58:08 +00004686 */
4687 tmp = cur->prev;
4688 while (tmp != NULL) {
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00004689 if ((tmp->type == XML_ELEMENT_NODE) &&
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004690 (generic ||
Kasimier T. Buchcik43ceb1e2006-06-12 11:08:18 +00004691 (xmlStrEqual(cur->name, tmp->name) &&
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004692 ((tmp->ns == cur->ns) ||
4693 ((tmp->ns != NULL) && (cur->ns != NULL) &&
Kasimier T. Buchcik43ceb1e2006-06-12 11:08:18 +00004694 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004695 occur++;
4696 tmp = tmp->prev;
4697 }
4698 if (occur == 0) {
4699 tmp = cur->next;
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004700 while (tmp != NULL && occur == 0) {
4701 if ((tmp->type == XML_ELEMENT_NODE) &&
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004702 (generic ||
Kasimier T. Buchcik43ceb1e2006-06-12 11:08:18 +00004703 (xmlStrEqual(cur->name, tmp->name) &&
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004704 ((tmp->ns == cur->ns) ||
4705 ((tmp->ns != NULL) && (cur->ns != NULL) &&
Kasimier T. Buchcik43ceb1e2006-06-12 11:08:18 +00004706 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004707 occur++;
4708 tmp = tmp->next;
4709 }
4710 if (occur != 0)
4711 occur = 1;
4712 } else
4713 occur++;
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004714 } else if (cur->type == XML_COMMENT_NODE) {
4715 sep = "/";
4716 name = "comment()";
4717 next = cur->parent;
4718
4719 /*
4720 * Thumbler index computation
4721 */
4722 tmp = cur->prev;
4723 while (tmp != NULL) {
4724 if (tmp->type == XML_COMMENT_NODE)
4725 occur++;
4726 tmp = tmp->prev;
4727 }
4728 if (occur == 0) {
4729 tmp = cur->next;
4730 while (tmp != NULL && occur == 0) {
4731 if (tmp->type == XML_COMMENT_NODE)
4732 occur++;
4733 tmp = tmp->next;
4734 }
4735 if (occur != 0)
4736 occur = 1;
4737 } else
4738 occur++;
4739 } else if ((cur->type == XML_TEXT_NODE) ||
4740 (cur->type == XML_CDATA_SECTION_NODE)) {
4741 sep = "/";
4742 name = "text()";
4743 next = cur->parent;
4744
4745 /*
4746 * Thumbler index computation
4747 */
4748 tmp = cur->prev;
4749 while (tmp != NULL) {
Kasimier T. Buchcikeb468702006-02-15 10:57:50 +00004750 if ((tmp->type == XML_TEXT_NODE) ||
4751 (tmp->type == XML_CDATA_SECTION_NODE))
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004752 occur++;
4753 tmp = tmp->prev;
4754 }
Kasimier T. Buchcikeb468702006-02-15 10:57:50 +00004755 /*
4756 * Evaluate if this is the only text- or CDATA-section-node;
4757 * if yes, then we'll get "text()", otherwise "text()[1]".
4758 */
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004759 if (occur == 0) {
4760 tmp = cur->next;
Kasimier T. Buchcikeb468702006-02-15 10:57:50 +00004761 while (tmp != NULL) {
4762 if ((tmp->type == XML_TEXT_NODE) ||
4763 (tmp->type == XML_CDATA_SECTION_NODE))
4764 {
4765 occur = 1;
4766 break;
Daniel Veillardaa6de472008-08-25 14:53:31 +00004767 }
Kasimier T. Buchcikeb468702006-02-15 10:57:50 +00004768 tmp = tmp->next;
4769 }
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004770 } else
4771 occur++;
4772 } else if (cur->type == XML_PI_NODE) {
4773 sep = "/";
4774 snprintf(nametemp, sizeof(nametemp) - 1,
William M. Brack13dfa872004-09-18 04:52:08 +00004775 "processing-instruction('%s')", (char *)cur->name);
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004776 nametemp[sizeof(nametemp) - 1] = 0;
4777 name = nametemp;
4778
4779 next = cur->parent;
4780
4781 /*
4782 * Thumbler index computation
4783 */
4784 tmp = cur->prev;
4785 while (tmp != NULL) {
4786 if ((tmp->type == XML_PI_NODE) &&
4787 (xmlStrEqual(cur->name, tmp->name)))
4788 occur++;
4789 tmp = tmp->prev;
4790 }
4791 if (occur == 0) {
4792 tmp = cur->next;
4793 while (tmp != NULL && occur == 0) {
4794 if ((tmp->type == XML_PI_NODE) &&
4795 (xmlStrEqual(cur->name, tmp->name)))
4796 occur++;
4797 tmp = tmp->next;
4798 }
4799 if (occur != 0)
4800 occur = 1;
4801 } else
4802 occur++;
4803
Daniel Veillard8faa7832001-11-26 15:58:08 +00004804 } else if (cur->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004805 sep = "/@";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004806 name = (const char *) (((xmlAttrPtr) cur)->name);
Daniel Veillard365c8062005-07-19 11:31:55 +00004807 if (cur->ns) {
4808 if (cur->ns->prefix != NULL)
4809 snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
Daniel Veillardaa6de472008-08-25 14:53:31 +00004810 (char *)cur->ns->prefix, (char *)cur->name);
Daniel Veillard365c8062005-07-19 11:31:55 +00004811 else
4812 snprintf(nametemp, sizeof(nametemp) - 1, "%s",
Daniel Veillardaa6de472008-08-25 14:53:31 +00004813 (char *)cur->name);
Daniel Veillard365c8062005-07-19 11:31:55 +00004814 nametemp[sizeof(nametemp) - 1] = 0;
4815 name = nametemp;
4816 }
Daniel Veillard8faa7832001-11-26 15:58:08 +00004817 next = ((xmlAttrPtr) cur)->parent;
4818 } else {
4819 next = cur->parent;
4820 }
4821
4822 /*
4823 * Make sure there is enough room
4824 */
4825 if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) {
4826 buf_len =
4827 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20;
4828 temp = (xmlChar *) xmlRealloc(buffer, buf_len);
4829 if (temp == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004830 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004831 xmlFree(buf);
4832 xmlFree(buffer);
4833 return (NULL);
4834 }
4835 buffer = temp;
4836 temp = (xmlChar *) xmlRealloc(buf, buf_len);
4837 if (temp == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004838 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004839 xmlFree(buf);
4840 xmlFree(buffer);
4841 return (NULL);
4842 }
4843 buf = temp;
4844 }
4845 if (occur == 0)
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004846 snprintf((char *) buf, buf_len, "%s%s%s",
Daniel Veillard8faa7832001-11-26 15:58:08 +00004847 sep, name, (char *) buffer);
4848 else
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004849 snprintf((char *) buf, buf_len, "%s%s[%d]%s",
Daniel Veillard8faa7832001-11-26 15:58:08 +00004850 sep, name, occur, (char *) buffer);
William M. Brack13dfa872004-09-18 04:52:08 +00004851 snprintf((char *) buffer, buf_len, "%s", (char *)buf);
Daniel Veillard8faa7832001-11-26 15:58:08 +00004852 cur = next;
4853 } while (cur != NULL);
4854 xmlFree(buf);
4855 return (buffer);
4856}
Daniel Veillard652327a2003-09-29 18:02:38 +00004857#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard8faa7832001-11-26 15:58:08 +00004858
4859/**
Owen Taylor3473f882001-02-23 17:55:21 +00004860 * xmlDocGetRootElement:
4861 * @doc: the document
4862 *
4863 * Get the root element of the document (doc->children is a list
4864 * containing possibly comments, PIs, etc ...).
4865 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004866 * Returns the #xmlNodePtr for the root or NULL
Owen Taylor3473f882001-02-23 17:55:21 +00004867 */
4868xmlNodePtr
4869xmlDocGetRootElement(xmlDocPtr doc) {
4870 xmlNodePtr ret;
4871
4872 if (doc == NULL) return(NULL);
4873 ret = doc->children;
4874 while (ret != NULL) {
4875 if (ret->type == XML_ELEMENT_NODE)
4876 return(ret);
4877 ret = ret->next;
4878 }
4879 return(ret);
4880}
Daniel Veillardaa6de472008-08-25 14:53:31 +00004881
Daniel Veillard2156d432004-03-04 15:59:36 +00004882#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004883/**
4884 * xmlDocSetRootElement:
4885 * @doc: the document
Daniel Veillard26a45c82006-10-20 12:55:34 +00004886 * @root: the new document root element, if root is NULL no action is taken,
4887 * to remove a node from a document use xmlUnlinkNode(root) instead.
Owen Taylor3473f882001-02-23 17:55:21 +00004888 *
4889 * Set the root element of the document (doc->children is a list
4890 * containing possibly comments, PIs, etc ...).
4891 *
Daniel Veillard26a45c82006-10-20 12:55:34 +00004892 * Returns the old root element if any was found, NULL if root was NULL
Owen Taylor3473f882001-02-23 17:55:21 +00004893 */
4894xmlNodePtr
4895xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) {
4896 xmlNodePtr old = NULL;
4897
4898 if (doc == NULL) return(NULL);
Daniel Veillard3e62adb2012-08-09 14:24:02 +08004899 if ((root == NULL) || (root->type == XML_NAMESPACE_DECL))
Daniel Veillardc575b992002-02-08 13:28:40 +00004900 return(NULL);
4901 xmlUnlinkNode(root);
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00004902 xmlSetTreeDoc(root, doc);
Daniel Veillardc575b992002-02-08 13:28:40 +00004903 root->parent = (xmlNodePtr) doc;
Owen Taylor3473f882001-02-23 17:55:21 +00004904 old = doc->children;
4905 while (old != NULL) {
4906 if (old->type == XML_ELEMENT_NODE)
4907 break;
4908 old = old->next;
4909 }
4910 if (old == NULL) {
4911 if (doc->children == NULL) {
4912 doc->children = root;
4913 doc->last = root;
4914 } else {
4915 xmlAddSibling(doc->children, root);
4916 }
4917 } else {
4918 xmlReplaceNode(old, root);
4919 }
4920 return(old);
4921}
Daniel Veillard2156d432004-03-04 15:59:36 +00004922#endif
Daniel Veillardaa6de472008-08-25 14:53:31 +00004923
Daniel Veillard2156d432004-03-04 15:59:36 +00004924#if defined(LIBXML_TREE_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004925/**
4926 * xmlNodeSetLang:
4927 * @cur: the node being changed
Daniel Veillardd1640922001-12-17 15:30:10 +00004928 * @lang: the language description
Owen Taylor3473f882001-02-23 17:55:21 +00004929 *
4930 * Set the language of a node, i.e. the values of the xml:lang
4931 * attribute.
4932 */
4933void
4934xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004935 xmlNsPtr ns;
4936
Owen Taylor3473f882001-02-23 17:55:21 +00004937 if (cur == NULL) return;
4938 switch(cur->type) {
4939 case XML_TEXT_NODE:
4940 case XML_CDATA_SECTION_NODE:
4941 case XML_COMMENT_NODE:
4942 case XML_DOCUMENT_NODE:
4943 case XML_DOCUMENT_TYPE_NODE:
4944 case XML_DOCUMENT_FRAG_NODE:
4945 case XML_NOTATION_NODE:
4946 case XML_HTML_DOCUMENT_NODE:
4947 case XML_DTD_NODE:
4948 case XML_ELEMENT_DECL:
4949 case XML_ATTRIBUTE_DECL:
4950 case XML_ENTITY_DECL:
4951 case XML_PI_NODE:
4952 case XML_ENTITY_REF_NODE:
4953 case XML_ENTITY_NODE:
4954 case XML_NAMESPACE_DECL:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004955#ifdef LIBXML_DOCB_ENABLED
4956 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004957#endif
4958 case XML_XINCLUDE_START:
4959 case XML_XINCLUDE_END:
4960 return;
4961 case XML_ELEMENT_NODE:
4962 case XML_ATTRIBUTE_NODE:
4963 break;
4964 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004965 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4966 if (ns == NULL)
4967 return;
4968 xmlSetNsProp(cur, ns, BAD_CAST "lang", lang);
Owen Taylor3473f882001-02-23 17:55:21 +00004969}
Daniel Veillard652327a2003-09-29 18:02:38 +00004970#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardaa6de472008-08-25 14:53:31 +00004971
Owen Taylor3473f882001-02-23 17:55:21 +00004972/**
4973 * xmlNodeGetLang:
4974 * @cur: the node being checked
4975 *
4976 * Searches the language of a node, i.e. the values of the xml:lang
4977 * attribute or the one carried by the nearest ancestor.
4978 *
4979 * Returns a pointer to the lang value, or NULL if not found
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004980 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00004981 */
4982xmlChar *
4983xmlNodeGetLang(xmlNodePtr cur) {
4984 xmlChar *lang;
4985
Daniel Veillard3e62adb2012-08-09 14:24:02 +08004986 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
4987 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004988 while (cur != NULL) {
Daniel Veillardc17337c2001-05-09 10:51:31 +00004989 lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE);
Owen Taylor3473f882001-02-23 17:55:21 +00004990 if (lang != NULL)
4991 return(lang);
4992 cur = cur->parent;
4993 }
4994 return(NULL);
4995}
Daniel Veillardaa6de472008-08-25 14:53:31 +00004996
Owen Taylor3473f882001-02-23 17:55:21 +00004997
Daniel Veillard652327a2003-09-29 18:02:38 +00004998#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00004999/**
5000 * xmlNodeSetSpacePreserve:
5001 * @cur: the node being changed
5002 * @val: the xml:space value ("0": default, 1: "preserve")
5003 *
5004 * Set (or reset) the space preserving behaviour of a node, i.e. the
5005 * value of the xml:space attribute.
5006 */
5007void
5008xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005009 xmlNsPtr ns;
5010
Owen Taylor3473f882001-02-23 17:55:21 +00005011 if (cur == NULL) return;
5012 switch(cur->type) {
5013 case XML_TEXT_NODE:
5014 case XML_CDATA_SECTION_NODE:
5015 case XML_COMMENT_NODE:
5016 case XML_DOCUMENT_NODE:
5017 case XML_DOCUMENT_TYPE_NODE:
5018 case XML_DOCUMENT_FRAG_NODE:
5019 case XML_NOTATION_NODE:
5020 case XML_HTML_DOCUMENT_NODE:
5021 case XML_DTD_NODE:
5022 case XML_ELEMENT_DECL:
5023 case XML_ATTRIBUTE_DECL:
5024 case XML_ENTITY_DECL:
5025 case XML_PI_NODE:
5026 case XML_ENTITY_REF_NODE:
5027 case XML_ENTITY_NODE:
5028 case XML_NAMESPACE_DECL:
5029 case XML_XINCLUDE_START:
5030 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005031#ifdef LIBXML_DOCB_ENABLED
5032 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005033#endif
5034 return;
5035 case XML_ELEMENT_NODE:
5036 case XML_ATTRIBUTE_NODE:
5037 break;
5038 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005039 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
5040 if (ns == NULL)
5041 return;
Owen Taylor3473f882001-02-23 17:55:21 +00005042 switch (val) {
5043 case 0:
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005044 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "default");
Owen Taylor3473f882001-02-23 17:55:21 +00005045 break;
5046 case 1:
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005047 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "preserve");
Owen Taylor3473f882001-02-23 17:55:21 +00005048 break;
5049 }
5050}
Daniel Veillard652327a2003-09-29 18:02:38 +00005051#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005052
5053/**
5054 * xmlNodeGetSpacePreserve:
5055 * @cur: the node being checked
5056 *
5057 * Searches the space preserving behaviour of a node, i.e. the values
5058 * of the xml:space attribute or the one carried by the nearest
5059 * ancestor.
5060 *
Daniel Veillardd1640922001-12-17 15:30:10 +00005061 * Returns -1 if xml:space is not inherited, 0 if "default", 1 if "preserve"
Owen Taylor3473f882001-02-23 17:55:21 +00005062 */
5063int
5064xmlNodeGetSpacePreserve(xmlNodePtr cur) {
5065 xmlChar *space;
5066
Daniel Veillard3e62adb2012-08-09 14:24:02 +08005067 if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE))
5068 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00005069 while (cur != NULL) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005070 space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE);
Owen Taylor3473f882001-02-23 17:55:21 +00005071 if (space != NULL) {
5072 if (xmlStrEqual(space, BAD_CAST "preserve")) {
5073 xmlFree(space);
5074 return(1);
5075 }
5076 if (xmlStrEqual(space, BAD_CAST "default")) {
5077 xmlFree(space);
5078 return(0);
5079 }
5080 xmlFree(space);
5081 }
5082 cur = cur->parent;
5083 }
5084 return(-1);
5085}
Daniel Veillardaa6de472008-08-25 14:53:31 +00005086
Daniel Veillard652327a2003-09-29 18:02:38 +00005087#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00005088/**
5089 * xmlNodeSetName:
5090 * @cur: the node being changed
5091 * @name: the new tag name
5092 *
5093 * Set (or reset) the name of a node.
5094 */
5095void
5096xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) {
Daniel Veillardce244ad2004-11-05 10:03:46 +00005097 xmlDocPtr doc;
5098 xmlDictPtr dict;
Tristan Van Berkomf0dd6e12014-04-22 21:15:05 -04005099 const xmlChar *freeme = NULL;
Daniel Veillardce244ad2004-11-05 10:03:46 +00005100
Owen Taylor3473f882001-02-23 17:55:21 +00005101 if (cur == NULL) return;
5102 if (name == NULL) return;
5103 switch(cur->type) {
5104 case XML_TEXT_NODE:
5105 case XML_CDATA_SECTION_NODE:
5106 case XML_COMMENT_NODE:
5107 case XML_DOCUMENT_TYPE_NODE:
5108 case XML_DOCUMENT_FRAG_NODE:
5109 case XML_NOTATION_NODE:
5110 case XML_HTML_DOCUMENT_NODE:
5111 case XML_NAMESPACE_DECL:
5112 case XML_XINCLUDE_START:
5113 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005114#ifdef LIBXML_DOCB_ENABLED
5115 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005116#endif
5117 return;
5118 case XML_ELEMENT_NODE:
5119 case XML_ATTRIBUTE_NODE:
5120 case XML_PI_NODE:
5121 case XML_ENTITY_REF_NODE:
5122 case XML_ENTITY_NODE:
5123 case XML_DTD_NODE:
5124 case XML_DOCUMENT_NODE:
5125 case XML_ELEMENT_DECL:
5126 case XML_ATTRIBUTE_DECL:
5127 case XML_ENTITY_DECL:
5128 break;
5129 }
Daniel Veillardce244ad2004-11-05 10:03:46 +00005130 doc = cur->doc;
5131 if (doc != NULL)
5132 dict = doc->dict;
5133 else
5134 dict = NULL;
5135 if (dict != NULL) {
5136 if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name)))
Tristan Van Berkomf0dd6e12014-04-22 21:15:05 -04005137 freeme = cur->name;
Daniel Veillardce244ad2004-11-05 10:03:46 +00005138 cur->name = xmlDictLookup(dict, name, -1);
5139 } else {
Tristan Van Berkomf0dd6e12014-04-22 21:15:05 -04005140 if (cur->name != NULL)
5141 freeme = cur->name;
Daniel Veillardce244ad2004-11-05 10:03:46 +00005142 cur->name = xmlStrdup(name);
5143 }
Tristan Van Berkomf0dd6e12014-04-22 21:15:05 -04005144
5145 if (freeme)
5146 xmlFree((xmlChar *) freeme);
Owen Taylor3473f882001-02-23 17:55:21 +00005147}
Daniel Veillard2156d432004-03-04 15:59:36 +00005148#endif
Daniel Veillardaa6de472008-08-25 14:53:31 +00005149
Daniel Veillard2156d432004-03-04 15:59:36 +00005150#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00005151/**
5152 * xmlNodeSetBase:
5153 * @cur: the node being changed
5154 * @uri: the new base URI
5155 *
5156 * Set (or reset) the base URI of a node, i.e. the value of the
5157 * xml:base attribute.
5158 */
5159void
Daniel Veillardf85ce8e2003-09-22 10:24:45 +00005160xmlNodeSetBase(xmlNodePtr cur, const xmlChar* uri) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005161 xmlNsPtr ns;
Martin Trappelf3703102010-01-22 12:08:00 +01005162 xmlChar* fixed;
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005163
Owen Taylor3473f882001-02-23 17:55:21 +00005164 if (cur == NULL) return;
5165 switch(cur->type) {
5166 case XML_TEXT_NODE:
5167 case XML_CDATA_SECTION_NODE:
5168 case XML_COMMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005169 case XML_DOCUMENT_TYPE_NODE:
5170 case XML_DOCUMENT_FRAG_NODE:
5171 case XML_NOTATION_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005172 case XML_DTD_NODE:
5173 case XML_ELEMENT_DECL:
5174 case XML_ATTRIBUTE_DECL:
5175 case XML_ENTITY_DECL:
5176 case XML_PI_NODE:
5177 case XML_ENTITY_REF_NODE:
5178 case XML_ENTITY_NODE:
5179 case XML_NAMESPACE_DECL:
5180 case XML_XINCLUDE_START:
5181 case XML_XINCLUDE_END:
Owen Taylor3473f882001-02-23 17:55:21 +00005182 return;
5183 case XML_ELEMENT_NODE:
5184 case XML_ATTRIBUTE_NODE:
5185 break;
Daniel Veillard4cbe4702002-05-05 06:57:27 +00005186 case XML_DOCUMENT_NODE:
5187#ifdef LIBXML_DOCB_ENABLED
5188 case XML_DOCB_DOCUMENT_NODE:
5189#endif
5190 case XML_HTML_DOCUMENT_NODE: {
5191 xmlDocPtr doc = (xmlDocPtr) cur;
5192
5193 if (doc->URL != NULL)
5194 xmlFree((xmlChar *) doc->URL);
5195 if (uri == NULL)
5196 doc->URL = NULL;
5197 else
Daniel Veillardb8efdda2006-10-10 12:37:14 +00005198 doc->URL = xmlPathToURI(uri);
Daniel Veillard4cbe4702002-05-05 06:57:27 +00005199 return;
5200 }
Owen Taylor3473f882001-02-23 17:55:21 +00005201 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00005202
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005203 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
5204 if (ns == NULL)
5205 return;
Daniel Veillardb8efdda2006-10-10 12:37:14 +00005206 fixed = xmlPathToURI(uri);
5207 if (fixed != NULL) {
5208 xmlSetNsProp(cur, ns, BAD_CAST "base", fixed);
Martin Trappelf3703102010-01-22 12:08:00 +01005209 xmlFree(fixed);
Daniel Veillardb8efdda2006-10-10 12:37:14 +00005210 } else {
5211 xmlSetNsProp(cur, ns, BAD_CAST "base", uri);
5212 }
Owen Taylor3473f882001-02-23 17:55:21 +00005213}
Daniel Veillard652327a2003-09-29 18:02:38 +00005214#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005215
5216/**
Owen Taylor3473f882001-02-23 17:55:21 +00005217 * xmlNodeGetBase:
5218 * @doc: the document the node pertains to
5219 * @cur: the node being checked
5220 *
5221 * Searches for the BASE URL. The code should work on both XML
5222 * and HTML document even if base mechanisms are completely different.
5223 * It returns the base as defined in RFC 2396 sections
5224 * 5.1.1. Base URI within Document Content
5225 * and
5226 * 5.1.2. Base URI from the Encapsulating Entity
5227 * However it does not return the document base (5.1.3), use
Daniel Veillarde4d18492010-03-09 11:12:30 +01005228 * doc->URL in this case
Owen Taylor3473f882001-02-23 17:55:21 +00005229 *
5230 * Returns a pointer to the base URL, or NULL if not found
Daniel Veillardbd9afb52002-09-25 22:25:35 +00005231 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00005232 */
5233xmlChar *
5234xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00005235 xmlChar *oldbase = NULL;
5236 xmlChar *base, *newbase;
Owen Taylor3473f882001-02-23 17:55:21 +00005237
Daniel Veillardaa6de472008-08-25 14:53:31 +00005238 if ((cur == NULL) && (doc == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00005239 return(NULL);
Daniel Veillard3e62adb2012-08-09 14:24:02 +08005240 if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
5241 return(NULL);
Daniel Veillardaa6de472008-08-25 14:53:31 +00005242 if (doc == NULL) doc = cur->doc;
Owen Taylor3473f882001-02-23 17:55:21 +00005243 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
5244 cur = doc->children;
5245 while ((cur != NULL) && (cur->name != NULL)) {
5246 if (cur->type != XML_ELEMENT_NODE) {
5247 cur = cur->next;
5248 continue;
5249 }
5250 if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) {
5251 cur = cur->children;
5252 continue;
5253 }
5254 if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) {
5255 cur = cur->children;
5256 continue;
5257 }
5258 if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) {
5259 return(xmlGetProp(cur, BAD_CAST "href"));
5260 }
5261 cur = cur->next;
5262 }
5263 return(NULL);
5264 }
5265 while (cur != NULL) {
5266 if (cur->type == XML_ENTITY_DECL) {
5267 xmlEntityPtr ent = (xmlEntityPtr) cur;
5268 return(xmlStrdup(ent->URI));
5269 }
Daniel Veillard42596ad2001-05-22 16:57:14 +00005270 if (cur->type == XML_ELEMENT_NODE) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00005271 base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE);
Daniel Veillard42596ad2001-05-22 16:57:14 +00005272 if (base != NULL) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00005273 if (oldbase != NULL) {
5274 newbase = xmlBuildURI(oldbase, base);
5275 if (newbase != NULL) {
5276 xmlFree(oldbase);
5277 xmlFree(base);
5278 oldbase = newbase;
5279 } else {
5280 xmlFree(oldbase);
5281 xmlFree(base);
5282 return(NULL);
5283 }
5284 } else {
5285 oldbase = base;
5286 }
5287 if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) ||
5288 (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) ||
5289 (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4)))
5290 return(oldbase);
Daniel Veillard42596ad2001-05-22 16:57:14 +00005291 }
5292 }
Owen Taylor3473f882001-02-23 17:55:21 +00005293 cur = cur->parent;
5294 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00005295 if ((doc != NULL) && (doc->URL != NULL)) {
5296 if (oldbase == NULL)
5297 return(xmlStrdup(doc->URL));
5298 newbase = xmlBuildURI(oldbase, doc->URL);
5299 xmlFree(oldbase);
5300 return(newbase);
5301 }
5302 return(oldbase);
Owen Taylor3473f882001-02-23 17:55:21 +00005303}
Daniel Veillardaa6de472008-08-25 14:53:31 +00005304
Owen Taylor3473f882001-02-23 17:55:21 +00005305/**
Daniel Veillard78697292003-10-19 20:44:43 +00005306 * xmlNodeBufGetContent:
5307 * @buffer: a buffer
5308 * @cur: the node being read
5309 *
5310 * Read the value of a node @cur, this can be either the text carried
5311 * directly by this node if it's a TEXT node or the aggregate string
5312 * of the values carried by this node child's (TEXT and ENTITY_REF).
5313 * Entity references are substituted.
5314 * Fills up the buffer @buffer with this value
Daniel Veillardaa6de472008-08-25 14:53:31 +00005315 *
Daniel Veillard78697292003-10-19 20:44:43 +00005316 * Returns 0 in case of success and -1 in case of error.
5317 */
5318int
5319xmlNodeBufGetContent(xmlBufferPtr buffer, xmlNodePtr cur)
5320{
Daniel Veillarddddeede2012-07-16 14:44:26 +08005321 xmlBufPtr buf;
5322 int ret;
5323
Daniel Veillard78697292003-10-19 20:44:43 +00005324 if ((cur == NULL) || (buffer == NULL)) return(-1);
Daniel Veillarddddeede2012-07-16 14:44:26 +08005325 buf = xmlBufFromBuffer(buffer);
5326 ret = xmlBufGetNodeContent(buf, cur);
5327 buffer = xmlBufBackToBuffer(buf);
5328 if ((ret < 0) || (buffer == NULL))
5329 return(-1);
5330 return(0);
5331}
5332
5333/**
5334 * xmlBufGetNodeContent:
Daniel Veillard28cc42d2012-08-10 10:00:18 +08005335 * @buf: a buffer xmlBufPtr
Daniel Veillarddddeede2012-07-16 14:44:26 +08005336 * @cur: the node being read
5337 *
5338 * Read the value of a node @cur, this can be either the text carried
5339 * directly by this node if it's a TEXT node or the aggregate string
5340 * of the values carried by this node child's (TEXT and ENTITY_REF).
5341 * Entity references are substituted.
5342 * Fills up the buffer @buffer with this value
5343 *
5344 * Returns 0 in case of success and -1 in case of error.
5345 */
5346int
5347xmlBufGetNodeContent(xmlBufPtr buf, xmlNodePtr cur)
5348{
5349 if ((cur == NULL) || (buf == NULL)) return(-1);
Daniel Veillard78697292003-10-19 20:44:43 +00005350 switch (cur->type) {
5351 case XML_CDATA_SECTION_NODE:
5352 case XML_TEXT_NODE:
Daniel Veillarddddeede2012-07-16 14:44:26 +08005353 xmlBufCat(buf, cur->content);
Daniel Veillard78697292003-10-19 20:44:43 +00005354 break;
5355 case XML_DOCUMENT_FRAG_NODE:
5356 case XML_ELEMENT_NODE:{
5357 xmlNodePtr tmp = cur;
5358
5359 while (tmp != NULL) {
5360 switch (tmp->type) {
5361 case XML_CDATA_SECTION_NODE:
5362 case XML_TEXT_NODE:
5363 if (tmp->content != NULL)
Daniel Veillarddddeede2012-07-16 14:44:26 +08005364 xmlBufCat(buf, tmp->content);
Daniel Veillard78697292003-10-19 20:44:43 +00005365 break;
5366 case XML_ENTITY_REF_NODE:
Daniel Veillarddddeede2012-07-16 14:44:26 +08005367 xmlBufGetNodeContent(buf, tmp);
Rob Richards77b92ff2005-12-20 15:55:14 +00005368 break;
Daniel Veillard78697292003-10-19 20:44:43 +00005369 default:
5370 break;
5371 }
5372 /*
5373 * Skip to next node
5374 */
5375 if (tmp->children != NULL) {
5376 if (tmp->children->type != XML_ENTITY_DECL) {
5377 tmp = tmp->children;
5378 continue;
5379 }
5380 }
5381 if (tmp == cur)
5382 break;
5383
5384 if (tmp->next != NULL) {
5385 tmp = tmp->next;
5386 continue;
5387 }
5388
5389 do {
5390 tmp = tmp->parent;
5391 if (tmp == NULL)
5392 break;
5393 if (tmp == cur) {
5394 tmp = NULL;
5395 break;
5396 }
5397 if (tmp->next != NULL) {
5398 tmp = tmp->next;
5399 break;
5400 }
5401 } while (tmp != NULL);
5402 }
5403 break;
5404 }
5405 case XML_ATTRIBUTE_NODE:{
5406 xmlAttrPtr attr = (xmlAttrPtr) cur;
5407 xmlNodePtr tmp = attr->children;
5408
5409 while (tmp != NULL) {
5410 if (tmp->type == XML_TEXT_NODE)
Daniel Veillarddddeede2012-07-16 14:44:26 +08005411 xmlBufCat(buf, tmp->content);
Daniel Veillard78697292003-10-19 20:44:43 +00005412 else
Daniel Veillarddddeede2012-07-16 14:44:26 +08005413 xmlBufGetNodeContent(buf, tmp);
Daniel Veillard78697292003-10-19 20:44:43 +00005414 tmp = tmp->next;
5415 }
5416 break;
5417 }
5418 case XML_COMMENT_NODE:
5419 case XML_PI_NODE:
Daniel Veillarddddeede2012-07-16 14:44:26 +08005420 xmlBufCat(buf, cur->content);
Daniel Veillard78697292003-10-19 20:44:43 +00005421 break;
5422 case XML_ENTITY_REF_NODE:{
5423 xmlEntityPtr ent;
5424 xmlNodePtr tmp;
5425
5426 /* lookup entity declaration */
5427 ent = xmlGetDocEntity(cur->doc, cur->name);
5428 if (ent == NULL)
5429 return(-1);
5430
5431 /* an entity content can be any "well balanced chunk",
5432 * i.e. the result of the content [43] production:
5433 * http://www.w3.org/TR/REC-xml#NT-content
5434 * -> we iterate through child nodes and recursive call
5435 * xmlNodeGetContent() which handles all possible node types */
5436 tmp = ent->children;
5437 while (tmp) {
Daniel Veillarddddeede2012-07-16 14:44:26 +08005438 xmlBufGetNodeContent(buf, tmp);
Daniel Veillard78697292003-10-19 20:44:43 +00005439 tmp = tmp->next;
5440 }
5441 break;
5442 }
5443 case XML_ENTITY_NODE:
5444 case XML_DOCUMENT_TYPE_NODE:
5445 case XML_NOTATION_NODE:
5446 case XML_DTD_NODE:
5447 case XML_XINCLUDE_START:
5448 case XML_XINCLUDE_END:
5449 break;
5450 case XML_DOCUMENT_NODE:
5451#ifdef LIBXML_DOCB_ENABLED
5452 case XML_DOCB_DOCUMENT_NODE:
5453#endif
5454 case XML_HTML_DOCUMENT_NODE:
5455 cur = cur->children;
5456 while (cur!= NULL) {
5457 if ((cur->type == XML_ELEMENT_NODE) ||
5458 (cur->type == XML_TEXT_NODE) ||
5459 (cur->type == XML_CDATA_SECTION_NODE)) {
Daniel Veillarddddeede2012-07-16 14:44:26 +08005460 xmlBufGetNodeContent(buf, cur);
Daniel Veillard78697292003-10-19 20:44:43 +00005461 }
5462 cur = cur->next;
5463 }
5464 break;
5465 case XML_NAMESPACE_DECL:
Daniel Veillarddddeede2012-07-16 14:44:26 +08005466 xmlBufCat(buf, ((xmlNsPtr) cur)->href);
Daniel Veillard78697292003-10-19 20:44:43 +00005467 break;
5468 case XML_ELEMENT_DECL:
5469 case XML_ATTRIBUTE_DECL:
5470 case XML_ENTITY_DECL:
5471 break;
5472 }
5473 return(0);
5474}
Daniel Veillarddddeede2012-07-16 14:44:26 +08005475
Daniel Veillard78697292003-10-19 20:44:43 +00005476/**
Owen Taylor3473f882001-02-23 17:55:21 +00005477 * xmlNodeGetContent:
5478 * @cur: the node being read
5479 *
5480 * Read the value of a node, this can be either the text carried
5481 * directly by this node if it's a TEXT node or the aggregate string
5482 * of the values carried by this node child's (TEXT and ENTITY_REF).
Daniel Veillardd1640922001-12-17 15:30:10 +00005483 * Entity references are substituted.
5484 * Returns a new #xmlChar * or NULL if no content is available.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00005485 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00005486 */
5487xmlChar *
Daniel Veillard7646b182002-04-20 06:41:40 +00005488xmlNodeGetContent(xmlNodePtr cur)
5489{
5490 if (cur == NULL)
5491 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005492 switch (cur->type) {
5493 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005494 case XML_ELEMENT_NODE:{
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005495 xmlBufPtr buf;
Daniel Veillard7646b182002-04-20 06:41:40 +00005496 xmlChar *ret;
Owen Taylor3473f882001-02-23 17:55:21 +00005497
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005498 buf = xmlBufCreateSize(64);
5499 if (buf == NULL)
Daniel Veillard7646b182002-04-20 06:41:40 +00005500 return (NULL);
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005501 xmlBufGetNodeContent(buf, cur);
5502 ret = xmlBufDetach(buf);
5503 xmlBufFree(buf);
Daniel Veillard7646b182002-04-20 06:41:40 +00005504 return (ret);
5505 }
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005506 case XML_ATTRIBUTE_NODE:
5507 return(xmlGetPropNodeValueInternal((xmlAttrPtr) cur));
Owen Taylor3473f882001-02-23 17:55:21 +00005508 case XML_COMMENT_NODE:
5509 case XML_PI_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005510 if (cur->content != NULL)
5511 return (xmlStrdup(cur->content));
5512 return (NULL);
5513 case XML_ENTITY_REF_NODE:{
5514 xmlEntityPtr ent;
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005515 xmlBufPtr buf;
Daniel Veillard7646b182002-04-20 06:41:40 +00005516 xmlChar *ret;
5517
5518 /* lookup entity declaration */
5519 ent = xmlGetDocEntity(cur->doc, cur->name);
5520 if (ent == NULL)
5521 return (NULL);
5522
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005523 buf = xmlBufCreate();
5524 if (buf == NULL)
Daniel Veillard7646b182002-04-20 06:41:40 +00005525 return (NULL);
5526
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005527 xmlBufGetNodeContent(buf, cur);
Daniel Veillard7646b182002-04-20 06:41:40 +00005528
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005529 ret = xmlBufDetach(buf);
5530 xmlBufFree(buf);
Daniel Veillard7646b182002-04-20 06:41:40 +00005531 return (ret);
5532 }
Owen Taylor3473f882001-02-23 17:55:21 +00005533 case XML_ENTITY_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005534 case XML_DOCUMENT_TYPE_NODE:
5535 case XML_NOTATION_NODE:
5536 case XML_DTD_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005537 case XML_XINCLUDE_START:
5538 case XML_XINCLUDE_END:
Daniel Veillard9adc0462003-03-24 18:39:54 +00005539 return (NULL);
5540 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005541#ifdef LIBXML_DOCB_ENABLED
Daniel Veillard7646b182002-04-20 06:41:40 +00005542 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005543#endif
Daniel Veillard9adc0462003-03-24 18:39:54 +00005544 case XML_HTML_DOCUMENT_NODE: {
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005545 xmlBufPtr buf;
Daniel Veillardc4696922003-10-19 21:47:14 +00005546 xmlChar *ret;
Daniel Veillard9adc0462003-03-24 18:39:54 +00005547
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005548 buf = xmlBufCreate();
5549 if (buf == NULL)
Daniel Veillardc4696922003-10-19 21:47:14 +00005550 return (NULL);
5551
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005552 xmlBufGetNodeContent(buf, (xmlNodePtr) cur);
Daniel Veillardc4696922003-10-19 21:47:14 +00005553
Daniel Veillardc15df7d2012-08-07 15:15:04 +08005554 ret = xmlBufDetach(buf);
5555 xmlBufFree(buf);
Daniel Veillardc4696922003-10-19 21:47:14 +00005556 return (ret);
Daniel Veillard9adc0462003-03-24 18:39:54 +00005557 }
Daniel Veillard96c3a3b2002-10-14 15:39:04 +00005558 case XML_NAMESPACE_DECL: {
5559 xmlChar *tmp;
5560
5561 tmp = xmlStrdup(((xmlNsPtr) cur)->href);
5562 return (tmp);
5563 }
Owen Taylor3473f882001-02-23 17:55:21 +00005564 case XML_ELEMENT_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00005565 /* TODO !!! */
5566 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005567 case XML_ATTRIBUTE_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00005568 /* TODO !!! */
5569 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005570 case XML_ENTITY_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00005571 /* TODO !!! */
5572 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005573 case XML_CDATA_SECTION_NODE:
5574 case XML_TEXT_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005575 if (cur->content != NULL)
5576 return (xmlStrdup(cur->content));
5577 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005578 }
Daniel Veillard7646b182002-04-20 06:41:40 +00005579 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005580}
Daniel Veillard652327a2003-09-29 18:02:38 +00005581
Owen Taylor3473f882001-02-23 17:55:21 +00005582/**
5583 * xmlNodeSetContent:
5584 * @cur: the node being modified
5585 * @content: the new value of the content
5586 *
5587 * Replace the content of a node.
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00005588 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
5589 * references, but XML special chars need to be escaped first by using
5590 * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars().
Owen Taylor3473f882001-02-23 17:55:21 +00005591 */
5592void
5593xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) {
5594 if (cur == NULL) {
5595#ifdef DEBUG_TREE
5596 xmlGenericError(xmlGenericErrorContext,
5597 "xmlNodeSetContent : node == NULL\n");
5598#endif
5599 return;
5600 }
5601 switch (cur->type) {
5602 case XML_DOCUMENT_FRAG_NODE:
5603 case XML_ELEMENT_NODE:
Daniel Veillard2c748c62002-01-16 15:37:50 +00005604 case XML_ATTRIBUTE_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005605 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5606 cur->children = xmlStringGetNodeList(cur->doc, content);
5607 UPDATE_LAST_CHILD_AND_PARENT(cur)
5608 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005609 case XML_TEXT_NODE:
5610 case XML_CDATA_SECTION_NODE:
5611 case XML_ENTITY_REF_NODE:
5612 case XML_ENTITY_NODE:
5613 case XML_PI_NODE:
5614 case XML_COMMENT_NODE:
Daniel Veillard8874b942005-08-25 13:19:21 +00005615 if ((cur->content != NULL) &&
5616 (cur->content != (xmlChar *) &(cur->properties))) {
William M. Brack7762bb12004-01-04 14:49:01 +00005617 if (!((cur->doc != NULL) && (cur->doc->dict != NULL) &&
Daniel Veillardc587bce2005-05-10 15:28:08 +00005618 (xmlDictOwns(cur->doc->dict, cur->content))))
William M. Brack7762bb12004-01-04 14:49:01 +00005619 xmlFree(cur->content);
Daniel Veillardaa6de472008-08-25 14:53:31 +00005620 }
Owen Taylor3473f882001-02-23 17:55:21 +00005621 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5622 cur->last = cur->children = NULL;
5623 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005624 cur->content = xmlStrdup(content);
Daniel Veillardaa6de472008-08-25 14:53:31 +00005625 } else
Owen Taylor3473f882001-02-23 17:55:21 +00005626 cur->content = NULL;
Daniel Veillard8874b942005-08-25 13:19:21 +00005627 cur->properties = NULL;
5628 cur->nsDef = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00005629 break;
5630 case XML_DOCUMENT_NODE:
5631 case XML_HTML_DOCUMENT_NODE:
5632 case XML_DOCUMENT_TYPE_NODE:
5633 case XML_XINCLUDE_START:
5634 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005635#ifdef LIBXML_DOCB_ENABLED
5636 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005637#endif
5638 break;
5639 case XML_NOTATION_NODE:
5640 break;
5641 case XML_DTD_NODE:
5642 break;
5643 case XML_NAMESPACE_DECL:
5644 break;
5645 case XML_ELEMENT_DECL:
5646 /* TODO !!! */
5647 break;
5648 case XML_ATTRIBUTE_DECL:
5649 /* TODO !!! */
5650 break;
5651 case XML_ENTITY_DECL:
5652 /* TODO !!! */
5653 break;
5654 }
5655}
5656
Daniel Veillard652327a2003-09-29 18:02:38 +00005657#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00005658/**
5659 * xmlNodeSetContentLen:
5660 * @cur: the node being modified
5661 * @content: the new value of the content
5662 * @len: the size of @content
5663 *
5664 * Replace the content of a node.
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00005665 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
5666 * references, but XML special chars need to be escaped first by using
5667 * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars().
Owen Taylor3473f882001-02-23 17:55:21 +00005668 */
5669void
5670xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5671 if (cur == NULL) {
5672#ifdef DEBUG_TREE
5673 xmlGenericError(xmlGenericErrorContext,
5674 "xmlNodeSetContentLen : node == NULL\n");
5675#endif
5676 return;
5677 }
5678 switch (cur->type) {
5679 case XML_DOCUMENT_FRAG_NODE:
5680 case XML_ELEMENT_NODE:
Daniel Veillard2c748c62002-01-16 15:37:50 +00005681 case XML_ATTRIBUTE_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005682 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5683 cur->children = xmlStringLenGetNodeList(cur->doc, content, len);
5684 UPDATE_LAST_CHILD_AND_PARENT(cur)
5685 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005686 case XML_TEXT_NODE:
5687 case XML_CDATA_SECTION_NODE:
5688 case XML_ENTITY_REF_NODE:
5689 case XML_ENTITY_NODE:
5690 case XML_PI_NODE:
5691 case XML_COMMENT_NODE:
5692 case XML_NOTATION_NODE:
Daniel Veillard8874b942005-08-25 13:19:21 +00005693 if ((cur->content != NULL) &&
5694 (cur->content != (xmlChar *) &(cur->properties))) {
5695 if (!((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5696 (xmlDictOwns(cur->doc->dict, cur->content))))
5697 xmlFree(cur->content);
Daniel Veillardaa6de472008-08-25 14:53:31 +00005698 }
Owen Taylor3473f882001-02-23 17:55:21 +00005699 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5700 cur->children = cur->last = NULL;
5701 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005702 cur->content = xmlStrndup(content, len);
Daniel Veillardaa6de472008-08-25 14:53:31 +00005703 } else
Owen Taylor3473f882001-02-23 17:55:21 +00005704 cur->content = NULL;
Daniel Veillard8874b942005-08-25 13:19:21 +00005705 cur->properties = NULL;
5706 cur->nsDef = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00005707 break;
5708 case XML_DOCUMENT_NODE:
5709 case XML_DTD_NODE:
5710 case XML_HTML_DOCUMENT_NODE:
5711 case XML_DOCUMENT_TYPE_NODE:
5712 case XML_NAMESPACE_DECL:
5713 case XML_XINCLUDE_START:
5714 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005715#ifdef LIBXML_DOCB_ENABLED
5716 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005717#endif
5718 break;
5719 case XML_ELEMENT_DECL:
5720 /* TODO !!! */
5721 break;
5722 case XML_ATTRIBUTE_DECL:
5723 /* TODO !!! */
5724 break;
5725 case XML_ENTITY_DECL:
5726 /* TODO !!! */
5727 break;
5728 }
5729}
Daniel Veillard652327a2003-09-29 18:02:38 +00005730#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005731
5732/**
5733 * xmlNodeAddContentLen:
5734 * @cur: the node being modified
5735 * @content: extra content
5736 * @len: the size of @content
Daniel Veillardaa6de472008-08-25 14:53:31 +00005737 *
Owen Taylor3473f882001-02-23 17:55:21 +00005738 * Append the extra substring to the node content.
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00005739 * NOTE: In contrast to xmlNodeSetContentLen(), @content is supposed to be
5740 * raw text, so unescaped XML special chars are allowed, entity
5741 * references are not supported.
Owen Taylor3473f882001-02-23 17:55:21 +00005742 */
5743void
5744xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5745 if (cur == NULL) {
5746#ifdef DEBUG_TREE
5747 xmlGenericError(xmlGenericErrorContext,
5748 "xmlNodeAddContentLen : node == NULL\n");
5749#endif
5750 return;
5751 }
5752 if (len <= 0) return;
5753 switch (cur->type) {
5754 case XML_DOCUMENT_FRAG_NODE:
5755 case XML_ELEMENT_NODE: {
Daniel Veillardacb2bda2002-01-13 16:15:43 +00005756 xmlNodePtr last, newNode, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00005757
Daniel Veillard7db37732001-07-12 01:20:08 +00005758 last = cur->last;
Owen Taylor3473f882001-02-23 17:55:21 +00005759 newNode = xmlNewTextLen(content, len);
5760 if (newNode != NULL) {
Daniel Veillardacb2bda2002-01-13 16:15:43 +00005761 tmp = xmlAddChild(cur, newNode);
5762 if (tmp != newNode)
5763 return;
Owen Taylor3473f882001-02-23 17:55:21 +00005764 if ((last != NULL) && (last->next == newNode)) {
5765 xmlTextMerge(last, newNode);
5766 }
5767 }
5768 break;
5769 }
5770 case XML_ATTRIBUTE_NODE:
5771 break;
5772 case XML_TEXT_NODE:
5773 case XML_CDATA_SECTION_NODE:
5774 case XML_ENTITY_REF_NODE:
5775 case XML_ENTITY_NODE:
5776 case XML_PI_NODE:
5777 case XML_COMMENT_NODE:
5778 case XML_NOTATION_NODE:
5779 if (content != NULL) {
Daniel Veillard8874b942005-08-25 13:19:21 +00005780 if ((cur->content == (xmlChar *) &(cur->properties)) ||
5781 ((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5782 xmlDictOwns(cur->doc->dict, cur->content))) {
5783 cur->content = xmlStrncatNew(cur->content, content, len);
5784 cur->properties = NULL;
5785 cur->nsDef = NULL;
William M. Brack7762bb12004-01-04 14:49:01 +00005786 break;
5787 }
Owen Taylor3473f882001-02-23 17:55:21 +00005788 cur->content = xmlStrncat(cur->content, content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00005789 }
5790 case XML_DOCUMENT_NODE:
5791 case XML_DTD_NODE:
5792 case XML_HTML_DOCUMENT_NODE:
5793 case XML_DOCUMENT_TYPE_NODE:
5794 case XML_NAMESPACE_DECL:
5795 case XML_XINCLUDE_START:
5796 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005797#ifdef LIBXML_DOCB_ENABLED
5798 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005799#endif
5800 break;
5801 case XML_ELEMENT_DECL:
5802 case XML_ATTRIBUTE_DECL:
5803 case XML_ENTITY_DECL:
5804 break;
5805 }
5806}
5807
5808/**
5809 * xmlNodeAddContent:
5810 * @cur: the node being modified
5811 * @content: extra content
Daniel Veillardaa6de472008-08-25 14:53:31 +00005812 *
Owen Taylor3473f882001-02-23 17:55:21 +00005813 * Append the extra substring to the node content.
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00005814 * NOTE: In contrast to xmlNodeSetContent(), @content is supposed to be
5815 * raw text, so unescaped XML special chars are allowed, entity
5816 * references are not supported.
Owen Taylor3473f882001-02-23 17:55:21 +00005817 */
5818void
5819xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) {
5820 int len;
5821
5822 if (cur == NULL) {
5823#ifdef DEBUG_TREE
5824 xmlGenericError(xmlGenericErrorContext,
5825 "xmlNodeAddContent : node == NULL\n");
5826#endif
5827 return;
5828 }
5829 if (content == NULL) return;
5830 len = xmlStrlen(content);
5831 xmlNodeAddContentLen(cur, content, len);
5832}
5833
5834/**
5835 * xmlTextMerge:
5836 * @first: the first text node
5837 * @second: the second text node being merged
Daniel Veillardaa6de472008-08-25 14:53:31 +00005838 *
Owen Taylor3473f882001-02-23 17:55:21 +00005839 * Merge two text nodes into one
5840 * Returns the first text node augmented
5841 */
5842xmlNodePtr
5843xmlTextMerge(xmlNodePtr first, xmlNodePtr second) {
5844 if (first == NULL) return(second);
5845 if (second == NULL) return(first);
5846 if (first->type != XML_TEXT_NODE) return(first);
5847 if (second->type != XML_TEXT_NODE) return(first);
5848 if (second->name != first->name)
5849 return(first);
Owen Taylor3473f882001-02-23 17:55:21 +00005850 xmlNodeAddContent(first, second->content);
Owen Taylor3473f882001-02-23 17:55:21 +00005851 xmlUnlinkNode(second);
5852 xmlFreeNode(second);
5853 return(first);
5854}
5855
Daniel Veillardf1a27c62006-10-13 22:33:03 +00005856#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00005857/**
5858 * xmlGetNsList:
5859 * @doc: the document
5860 * @node: the current node
5861 *
5862 * Search all the namespace applying to a given element.
Daniel Veillardd1640922001-12-17 15:30:10 +00005863 * Returns an NULL terminated array of all the #xmlNsPtr found
Owen Taylor3473f882001-02-23 17:55:21 +00005864 * that need to be freed by the caller or NULL if no
5865 * namespace if defined
5866 */
5867xmlNsPtr *
Daniel Veillard77044732001-06-29 21:31:07 +00005868xmlGetNsList(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node)
5869{
Owen Taylor3473f882001-02-23 17:55:21 +00005870 xmlNsPtr cur;
5871 xmlNsPtr *ret = NULL;
5872 int nbns = 0;
5873 int maxns = 10;
5874 int i;
5875
Daniel Veillard3e62adb2012-08-09 14:24:02 +08005876 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
5877 return(NULL);
5878
Owen Taylor3473f882001-02-23 17:55:21 +00005879 while (node != NULL) {
Daniel Veillard77044732001-06-29 21:31:07 +00005880 if (node->type == XML_ELEMENT_NODE) {
5881 cur = node->nsDef;
5882 while (cur != NULL) {
5883 if (ret == NULL) {
5884 ret =
5885 (xmlNsPtr *) xmlMalloc((maxns + 1) *
5886 sizeof(xmlNsPtr));
5887 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005888 xmlTreeErrMemory("getting namespace list");
Daniel Veillard77044732001-06-29 21:31:07 +00005889 return (NULL);
5890 }
5891 ret[nbns] = NULL;
5892 }
5893 for (i = 0; i < nbns; i++) {
5894 if ((cur->prefix == ret[i]->prefix) ||
5895 (xmlStrEqual(cur->prefix, ret[i]->prefix)))
5896 break;
5897 }
5898 if (i >= nbns) {
5899 if (nbns >= maxns) {
5900 maxns *= 2;
5901 ret = (xmlNsPtr *) xmlRealloc(ret,
5902 (maxns +
5903 1) *
5904 sizeof(xmlNsPtr));
5905 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005906 xmlTreeErrMemory("getting namespace list");
Daniel Veillard77044732001-06-29 21:31:07 +00005907 return (NULL);
5908 }
5909 }
5910 ret[nbns++] = cur;
5911 ret[nbns] = NULL;
5912 }
Owen Taylor3473f882001-02-23 17:55:21 +00005913
Daniel Veillard77044732001-06-29 21:31:07 +00005914 cur = cur->next;
5915 }
5916 }
5917 node = node->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00005918 }
Daniel Veillard77044732001-06-29 21:31:07 +00005919 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00005920}
Daniel Veillard652327a2003-09-29 18:02:38 +00005921#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005922
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005923/*
5924* xmlTreeEnsureXMLDecl:
5925* @doc: the doc
Daniel Veillardaa6de472008-08-25 14:53:31 +00005926*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005927* Ensures that there is an XML namespace declaration on the doc.
Daniel Veillardaa6de472008-08-25 14:53:31 +00005928*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005929* Returns the XML ns-struct or NULL on API and internal errors.
5930*/
5931static xmlNsPtr
5932xmlTreeEnsureXMLDecl(xmlDocPtr doc)
5933{
5934 if (doc == NULL)
5935 return (NULL);
5936 if (doc->oldNs != NULL)
5937 return (doc->oldNs);
5938 {
5939 xmlNsPtr ns;
5940 ns = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5941 if (ns == NULL) {
5942 xmlTreeErrMemory(
5943 "allocating the XML namespace");
5944 return (NULL);
5945 }
5946 memset(ns, 0, sizeof(xmlNs));
5947 ns->type = XML_LOCAL_NAMESPACE;
Daniel Veillardaa6de472008-08-25 14:53:31 +00005948 ns->href = xmlStrdup(XML_XML_NAMESPACE);
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005949 ns->prefix = xmlStrdup((const xmlChar *)"xml");
5950 doc->oldNs = ns;
5951 return (ns);
5952 }
5953}
5954
Owen Taylor3473f882001-02-23 17:55:21 +00005955/**
5956 * xmlSearchNs:
5957 * @doc: the document
5958 * @node: the current node
Daniel Veillard77851712001-02-27 21:54:07 +00005959 * @nameSpace: the namespace prefix
Owen Taylor3473f882001-02-23 17:55:21 +00005960 *
5961 * Search a Ns registered under a given name space for a document.
5962 * recurse on the parents until it finds the defined namespace
5963 * or return NULL otherwise.
5964 * @nameSpace can be NULL, this is a search for the default namespace.
5965 * We don't allow to cross entities boundaries. If you don't declare
5966 * the namespace within those you will be in troubles !!! A warning
5967 * is generated to cover this case.
5968 *
5969 * Returns the namespace pointer or NULL.
5970 */
5971xmlNsPtr
5972xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00005973
Owen Taylor3473f882001-02-23 17:55:21 +00005974 xmlNsPtr cur;
Daniel Veillardf4e56292003-10-28 14:27:41 +00005975 xmlNodePtr orig = node;
Owen Taylor3473f882001-02-23 17:55:21 +00005976
Daniel Veillard3e62adb2012-08-09 14:24:02 +08005977 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005978 if ((nameSpace != NULL) &&
5979 (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) {
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00005980 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
5981 /*
5982 * The XML-1.0 namespace is normally held on the root
5983 * element. In this case exceptionally create it on the
5984 * node element.
5985 */
5986 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5987 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005988 xmlTreeErrMemory("searching namespace");
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00005989 return(NULL);
5990 }
5991 memset(cur, 0, sizeof(xmlNs));
5992 cur->type = XML_LOCAL_NAMESPACE;
Daniel Veillardaa6de472008-08-25 14:53:31 +00005993 cur->href = xmlStrdup(XML_XML_NAMESPACE);
5994 cur->prefix = xmlStrdup((const xmlChar *)"xml");
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00005995 cur->next = node->nsDef;
5996 node->nsDef = cur;
5997 return(cur);
5998 }
Daniel Veillard11ce4002006-03-10 00:36:23 +00005999 if (doc == NULL) {
6000 doc = node->doc;
6001 if (doc == NULL)
6002 return(NULL);
6003 }
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00006004 /*
6005 * Return the XML namespace declaration held by the doc.
6006 */
6007 if (doc->oldNs == NULL)
6008 return(xmlTreeEnsureXMLDecl(doc));
6009 else
6010 return(doc->oldNs);
Owen Taylor3473f882001-02-23 17:55:21 +00006011 }
6012 while (node != NULL) {
6013 if ((node->type == XML_ENTITY_REF_NODE) ||
6014 (node->type == XML_ENTITY_NODE) ||
6015 (node->type == XML_ENTITY_DECL))
6016 return(NULL);
6017 if (node->type == XML_ELEMENT_NODE) {
6018 cur = node->nsDef;
6019 while (cur != NULL) {
6020 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
6021 (cur->href != NULL))
6022 return(cur);
6023 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
6024 (cur->href != NULL) &&
6025 (xmlStrEqual(cur->prefix, nameSpace)))
6026 return(cur);
6027 cur = cur->next;
6028 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00006029 if (orig != node) {
Daniel Veillardf4e56292003-10-28 14:27:41 +00006030 cur = node->ns;
6031 if (cur != NULL) {
6032 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
6033 (cur->href != NULL))
6034 return(cur);
6035 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
6036 (cur->href != NULL) &&
6037 (xmlStrEqual(cur->prefix, nameSpace)))
6038 return(cur);
6039 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00006040 }
Owen Taylor3473f882001-02-23 17:55:21 +00006041 }
6042 node = node->parent;
6043 }
6044 return(NULL);
6045}
6046
6047/**
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006048 * xmlNsInScope:
6049 * @doc: the document
6050 * @node: the current node
6051 * @ancestor: the ancestor carrying the namespace
6052 * @prefix: the namespace prefix
6053 *
6054 * Verify that the given namespace held on @ancestor is still in scope
6055 * on node.
Daniel Veillardaa6de472008-08-25 14:53:31 +00006056 *
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006057 * Returns 1 if true, 0 if false and -1 in case of error.
6058 */
6059static int
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006060xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node,
6061 xmlNodePtr ancestor, const xmlChar * prefix)
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006062{
6063 xmlNsPtr tst;
6064
6065 while ((node != NULL) && (node != ancestor)) {
6066 if ((node->type == XML_ENTITY_REF_NODE) ||
6067 (node->type == XML_ENTITY_NODE) ||
6068 (node->type == XML_ENTITY_DECL))
6069 return (-1);
6070 if (node->type == XML_ELEMENT_NODE) {
6071 tst = node->nsDef;
6072 while (tst != NULL) {
6073 if ((tst->prefix == NULL)
6074 && (prefix == NULL))
6075 return (0);
6076 if ((tst->prefix != NULL)
6077 && (prefix != NULL)
6078 && (xmlStrEqual(tst->prefix, prefix)))
6079 return (0);
6080 tst = tst->next;
6081 }
6082 }
6083 node = node->parent;
6084 }
6085 if (node != ancestor)
6086 return (-1);
6087 return (1);
6088}
Daniel Veillardaa6de472008-08-25 14:53:31 +00006089
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006090/**
Owen Taylor3473f882001-02-23 17:55:21 +00006091 * xmlSearchNsByHref:
6092 * @doc: the document
6093 * @node: the current node
6094 * @href: the namespace value
6095 *
6096 * Search a Ns aliasing a given URI. Recurse on the parents until it finds
6097 * the defined namespace or return NULL otherwise.
6098 * Returns the namespace pointer or NULL.
6099 */
6100xmlNsPtr
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006101xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href)
6102{
Owen Taylor3473f882001-02-23 17:55:21 +00006103 xmlNsPtr cur;
6104 xmlNodePtr orig = node;
Daniel Veillard62040be2004-05-17 03:17:26 +00006105 int is_attr;
Owen Taylor3473f882001-02-23 17:55:21 +00006106
Daniel Veillard3e62adb2012-08-09 14:24:02 +08006107 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) || (href == NULL))
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006108 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006109 if (xmlStrEqual(href, XML_XML_NAMESPACE)) {
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006110 /*
6111 * Only the document can hold the XML spec namespace.
6112 */
6113 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
6114 /*
6115 * The XML-1.0 namespace is normally held on the root
6116 * element. In this case exceptionally create it on the
6117 * node element.
6118 */
6119 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
6120 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006121 xmlTreeErrMemory("searching namespace");
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006122 return (NULL);
6123 }
6124 memset(cur, 0, sizeof(xmlNs));
6125 cur->type = XML_LOCAL_NAMESPACE;
6126 cur->href = xmlStrdup(XML_XML_NAMESPACE);
6127 cur->prefix = xmlStrdup((const xmlChar *) "xml");
6128 cur->next = node->nsDef;
6129 node->nsDef = cur;
6130 return (cur);
6131 }
Daniel Veillard11ce4002006-03-10 00:36:23 +00006132 if (doc == NULL) {
6133 doc = node->doc;
6134 if (doc == NULL)
6135 return(NULL);
6136 }
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00006137 /*
6138 * Return the XML namespace declaration held by the doc.
6139 */
6140 if (doc->oldNs == NULL)
6141 return(xmlTreeEnsureXMLDecl(doc));
6142 else
Daniel Veillardaa6de472008-08-25 14:53:31 +00006143 return(doc->oldNs);
Owen Taylor3473f882001-02-23 17:55:21 +00006144 }
Daniel Veillard62040be2004-05-17 03:17:26 +00006145 is_attr = (node->type == XML_ATTRIBUTE_NODE);
Owen Taylor3473f882001-02-23 17:55:21 +00006146 while (node != NULL) {
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006147 if ((node->type == XML_ENTITY_REF_NODE) ||
6148 (node->type == XML_ENTITY_NODE) ||
6149 (node->type == XML_ENTITY_DECL))
6150 return (NULL);
6151 if (node->type == XML_ELEMENT_NODE) {
6152 cur = node->nsDef;
6153 while (cur != NULL) {
6154 if ((cur->href != NULL) && (href != NULL) &&
6155 (xmlStrEqual(cur->href, href))) {
Daniel Veillard62040be2004-05-17 03:17:26 +00006156 if (((!is_attr) || (cur->prefix != NULL)) &&
Kasimier T. Buchcikba70cc02005-02-28 10:28:21 +00006157 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006158 return (cur);
6159 }
6160 cur = cur->next;
6161 }
Daniel Veillardf4e56292003-10-28 14:27:41 +00006162 if (orig != node) {
6163 cur = node->ns;
6164 if (cur != NULL) {
6165 if ((cur->href != NULL) && (href != NULL) &&
6166 (xmlStrEqual(cur->href, href))) {
Daniel Veillard62040be2004-05-17 03:17:26 +00006167 if (((!is_attr) || (cur->prefix != NULL)) &&
Kasimier T. Buchcikba70cc02005-02-28 10:28:21 +00006168 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
Daniel Veillardf4e56292003-10-28 14:27:41 +00006169 return (cur);
6170 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006171 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00006172 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006173 }
6174 node = node->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00006175 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006176 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006177}
6178
6179/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00006180 * xmlNewReconciliedNs:
Owen Taylor3473f882001-02-23 17:55:21 +00006181 * @doc: the document
6182 * @tree: a node expected to hold the new namespace
6183 * @ns: the original namespace
6184 *
6185 * This function tries to locate a namespace definition in a tree
6186 * ancestors, or create a new namespace definition node similar to
6187 * @ns trying to reuse the same prefix. However if the given prefix is
6188 * null (default namespace) or reused within the subtree defined by
6189 * @tree or on one of its ancestors then a new prefix is generated.
6190 * Returns the (new) namespace definition or NULL in case of error
6191 */
Daniel Veillard8ed10722009-08-20 19:17:36 +02006192static xmlNsPtr
Owen Taylor3473f882001-02-23 17:55:21 +00006193xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) {
6194 xmlNsPtr def;
6195 xmlChar prefix[50];
6196 int counter = 1;
6197
Daniel Veillard3e62adb2012-08-09 14:24:02 +08006198 if ((tree == NULL) || (tree->type != XML_ELEMENT_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006199#ifdef DEBUG_TREE
6200 xmlGenericError(xmlGenericErrorContext,
6201 "xmlNewReconciliedNs : tree == NULL\n");
6202#endif
6203 return(NULL);
6204 }
Daniel Veillardce244ad2004-11-05 10:03:46 +00006205 if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006206#ifdef DEBUG_TREE
6207 xmlGenericError(xmlGenericErrorContext,
6208 "xmlNewReconciliedNs : ns == NULL\n");
6209#endif
6210 return(NULL);
6211 }
6212 /*
6213 * Search an existing namespace definition inherited.
6214 */
6215 def = xmlSearchNsByHref(doc, tree, ns->href);
6216 if (def != NULL)
6217 return(def);
6218
6219 /*
6220 * Find a close prefix which is not already in use.
6221 * Let's strip namespace prefixes longer than 20 chars !
6222 */
Daniel Veillardf742d342002-03-07 00:05:35 +00006223 if (ns->prefix == NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00006224 snprintf((char *) prefix, sizeof(prefix), "default");
Daniel Veillardf742d342002-03-07 00:05:35 +00006225 else
William M. Brack13dfa872004-09-18 04:52:08 +00006226 snprintf((char *) prefix, sizeof(prefix), "%.20s", (char *)ns->prefix);
Daniel Veillardf742d342002-03-07 00:05:35 +00006227
Owen Taylor3473f882001-02-23 17:55:21 +00006228 def = xmlSearchNs(doc, tree, prefix);
6229 while (def != NULL) {
6230 if (counter > 1000) return(NULL);
Daniel Veillardf742d342002-03-07 00:05:35 +00006231 if (ns->prefix == NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00006232 snprintf((char *) prefix, sizeof(prefix), "default%d", counter++);
Daniel Veillardf742d342002-03-07 00:05:35 +00006233 else
William M. Brack13dfa872004-09-18 04:52:08 +00006234 snprintf((char *) prefix, sizeof(prefix), "%.20s%d",
Daniel Veillardaa6de472008-08-25 14:53:31 +00006235 (char *)ns->prefix, counter++);
Owen Taylor3473f882001-02-23 17:55:21 +00006236 def = xmlSearchNs(doc, tree, prefix);
6237 }
6238
6239 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00006240 * OK, now we are ready to create a new one.
Owen Taylor3473f882001-02-23 17:55:21 +00006241 */
6242 def = xmlNewNs(tree, ns->href, prefix);
6243 return(def);
6244}
6245
Daniel Veillard652327a2003-09-29 18:02:38 +00006246#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00006247/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00006248 * xmlReconciliateNs:
Owen Taylor3473f882001-02-23 17:55:21 +00006249 * @doc: the document
6250 * @tree: a node defining the subtree to reconciliate
6251 *
6252 * This function checks that all the namespaces declared within the given
6253 * tree are properly declared. This is needed for example after Copy or Cut
6254 * and then paste operations. The subtree may still hold pointers to
6255 * namespace declarations outside the subtree or invalid/masked. As much
Daniel Veillardd1640922001-12-17 15:30:10 +00006256 * as possible the function try to reuse the existing namespaces found in
Owen Taylor3473f882001-02-23 17:55:21 +00006257 * the new environment. If not possible the new namespaces are redeclared
6258 * on @tree at the top of the given subtree.
6259 * Returns the number of namespace declarations created or -1 in case of error.
6260 */
6261int
6262xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) {
6263 xmlNsPtr *oldNs = NULL;
6264 xmlNsPtr *newNs = NULL;
6265 int sizeCache = 0;
6266 int nbCache = 0;
6267
6268 xmlNsPtr n;
6269 xmlNodePtr node = tree;
6270 xmlAttrPtr attr;
6271 int ret = 0, i;
6272
Daniel Veillardce244ad2004-11-05 10:03:46 +00006273 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) return(-1);
6274 if ((doc == NULL) || (doc->type != XML_DOCUMENT_NODE)) return(-1);
6275 if (node->doc != doc) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006276 while (node != NULL) {
6277 /*
6278 * Reconciliate the node namespace
6279 */
6280 if (node->ns != NULL) {
6281 /*
6282 * initialize the cache if needed
6283 */
6284 if (sizeCache == 0) {
6285 sizeCache = 10;
6286 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6287 sizeof(xmlNsPtr));
6288 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006289 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00006290 return(-1);
6291 }
6292 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6293 sizeof(xmlNsPtr));
6294 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006295 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00006296 xmlFree(oldNs);
6297 return(-1);
6298 }
6299 }
6300 for (i = 0;i < nbCache;i++) {
6301 if (oldNs[i] == node->ns) {
6302 node->ns = newNs[i];
6303 break;
6304 }
6305 }
6306 if (i == nbCache) {
6307 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00006308 * OK we need to recreate a new namespace definition
Owen Taylor3473f882001-02-23 17:55:21 +00006309 */
6310 n = xmlNewReconciliedNs(doc, tree, node->ns);
6311 if (n != NULL) { /* :-( what if else ??? */
6312 /*
6313 * check if we need to grow the cache buffers.
6314 */
6315 if (sizeCache <= nbCache) {
6316 sizeCache *= 2;
6317 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
6318 sizeof(xmlNsPtr));
6319 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006320 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00006321 xmlFree(newNs);
6322 return(-1);
6323 }
6324 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
6325 sizeof(xmlNsPtr));
6326 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006327 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00006328 xmlFree(oldNs);
6329 return(-1);
6330 }
6331 }
6332 newNs[nbCache] = n;
6333 oldNs[nbCache++] = node->ns;
6334 node->ns = n;
6335 }
6336 }
6337 }
6338 /*
6339 * now check for namespace hold by attributes on the node.
6340 */
Daniel Veillardb5f11972006-10-14 08:46:40 +00006341 if (node->type == XML_ELEMENT_NODE) {
6342 attr = node->properties;
6343 while (attr != NULL) {
6344 if (attr->ns != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00006345 /*
Daniel Veillardb5f11972006-10-14 08:46:40 +00006346 * initialize the cache if needed
Owen Taylor3473f882001-02-23 17:55:21 +00006347 */
Daniel Veillardb5f11972006-10-14 08:46:40 +00006348 if (sizeCache == 0) {
6349 sizeCache = 10;
6350 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6351 sizeof(xmlNsPtr));
6352 if (oldNs == NULL) {
6353 xmlTreeErrMemory("fixing namespaces");
6354 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006355 }
Daniel Veillardb5f11972006-10-14 08:46:40 +00006356 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6357 sizeof(xmlNsPtr));
6358 if (newNs == NULL) {
6359 xmlTreeErrMemory("fixing namespaces");
6360 xmlFree(oldNs);
6361 return(-1);
6362 }
6363 }
6364 for (i = 0;i < nbCache;i++) {
6365 if (oldNs[i] == attr->ns) {
6366 attr->ns = newNs[i];
6367 break;
6368 }
6369 }
6370 if (i == nbCache) {
6371 /*
6372 * OK we need to recreate a new namespace definition
6373 */
6374 n = xmlNewReconciliedNs(doc, tree, attr->ns);
6375 if (n != NULL) { /* :-( what if else ??? */
6376 /*
6377 * check if we need to grow the cache buffers.
6378 */
6379 if (sizeCache <= nbCache) {
6380 sizeCache *= 2;
6381 oldNs = (xmlNsPtr *) xmlRealloc(oldNs,
6382 sizeCache * sizeof(xmlNsPtr));
6383 if (oldNs == NULL) {
6384 xmlTreeErrMemory("fixing namespaces");
6385 xmlFree(newNs);
6386 return(-1);
6387 }
6388 newNs = (xmlNsPtr *) xmlRealloc(newNs,
6389 sizeCache * sizeof(xmlNsPtr));
6390 if (newNs == NULL) {
6391 xmlTreeErrMemory("fixing namespaces");
6392 xmlFree(oldNs);
6393 return(-1);
6394 }
6395 }
6396 newNs[nbCache] = n;
6397 oldNs[nbCache++] = attr->ns;
6398 attr->ns = n;
6399 }
Owen Taylor3473f882001-02-23 17:55:21 +00006400 }
6401 }
Daniel Veillardb5f11972006-10-14 08:46:40 +00006402 attr = attr->next;
Owen Taylor3473f882001-02-23 17:55:21 +00006403 }
Owen Taylor3473f882001-02-23 17:55:21 +00006404 }
6405
6406 /*
6407 * Browse the full subtree, deep first
6408 */
Daniel Veillardb5f11972006-10-14 08:46:40 +00006409 if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006410 /* deep first */
6411 node = node->children;
6412 } else if ((node != tree) && (node->next != NULL)) {
6413 /* then siblings */
6414 node = node->next;
6415 } else if (node != tree) {
6416 /* go up to parents->next if needed */
6417 while (node != tree) {
6418 if (node->parent != NULL)
6419 node = node->parent;
6420 if ((node != tree) && (node->next != NULL)) {
6421 node = node->next;
6422 break;
6423 }
6424 if (node->parent == NULL) {
6425 node = NULL;
6426 break;
6427 }
6428 }
6429 /* exit condition */
Daniel Veillardaa6de472008-08-25 14:53:31 +00006430 if (node == tree)
Owen Taylor3473f882001-02-23 17:55:21 +00006431 node = NULL;
Daniel Veillard1e774382002-03-06 17:35:40 +00006432 } else
6433 break;
Owen Taylor3473f882001-02-23 17:55:21 +00006434 }
Daniel Veillardf742d342002-03-07 00:05:35 +00006435 if (oldNs != NULL)
6436 xmlFree(oldNs);
6437 if (newNs != NULL)
6438 xmlFree(newNs);
Owen Taylor3473f882001-02-23 17:55:21 +00006439 return(ret);
6440}
Daniel Veillard652327a2003-09-29 18:02:38 +00006441#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00006442
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006443static xmlAttrPtr
6444xmlGetPropNodeInternal(xmlNodePtr node, const xmlChar *name,
6445 const xmlChar *nsName, int useDTD)
6446{
6447 xmlAttrPtr prop;
6448
6449 if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL))
6450 return(NULL);
6451
6452 if (node->properties != NULL) {
6453 prop = node->properties;
6454 if (nsName == NULL) {
6455 /*
6456 * We want the attr to be in no namespace.
6457 */
6458 do {
6459 if ((prop->ns == NULL) && xmlStrEqual(prop->name, name)) {
6460 return(prop);
6461 }
6462 prop = prop->next;
6463 } while (prop != NULL);
6464 } else {
6465 /*
6466 * We want the attr to be in the specified namespace.
6467 */
6468 do {
6469 if ((prop->ns != NULL) && xmlStrEqual(prop->name, name) &&
6470 ((prop->ns->href == nsName) ||
6471 xmlStrEqual(prop->ns->href, nsName)))
6472 {
6473 return(prop);
6474 }
6475 prop = prop->next;
6476 } while (prop != NULL);
6477 }
6478 }
6479
6480#ifdef LIBXML_TREE_ENABLED
6481 if (! useDTD)
6482 return(NULL);
6483 /*
6484 * Check if there is a default/fixed attribute declaration in
6485 * the internal or external subset.
Daniel Veillardaa6de472008-08-25 14:53:31 +00006486 */
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006487 if ((node->doc != NULL) && (node->doc->intSubset != NULL)) {
6488 xmlDocPtr doc = node->doc;
6489 xmlAttributePtr attrDecl = NULL;
6490 xmlChar *elemQName, *tmpstr = NULL;
6491
6492 /*
Daniel Veillardaa6de472008-08-25 14:53:31 +00006493 * We need the QName of the element for the DTD-lookup.
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006494 */
6495 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
6496 tmpstr = xmlStrdup(node->ns->prefix);
6497 tmpstr = xmlStrcat(tmpstr, BAD_CAST ":");
6498 tmpstr = xmlStrcat(tmpstr, node->name);
6499 if (tmpstr == NULL)
6500 return(NULL);
6501 elemQName = tmpstr;
6502 } else
6503 elemQName = (xmlChar *) node->name;
6504 if (nsName == NULL) {
6505 /*
6506 * The common and nice case: Attr in no namespace.
6507 */
6508 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset,
6509 elemQName, name, NULL);
6510 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
6511 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset,
6512 elemQName, name, NULL);
6513 }
6514 } else {
6515 xmlNsPtr *nsList, *cur;
6516
6517 /*
6518 * The ugly case: Search using the prefixes of in-scope
6519 * ns-decls corresponding to @nsName.
6520 */
6521 nsList = xmlGetNsList(node->doc, node);
6522 if (nsList == NULL) {
6523 if (tmpstr != NULL)
6524 xmlFree(tmpstr);
6525 return(NULL);
6526 }
6527 cur = nsList;
6528 while (*cur != NULL) {
6529 if (xmlStrEqual((*cur)->href, nsName)) {
6530 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elemQName,
6531 name, (*cur)->prefix);
6532 if (attrDecl)
6533 break;
6534 if (doc->extSubset != NULL) {
6535 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elemQName,
6536 name, (*cur)->prefix);
6537 if (attrDecl)
6538 break;
6539 }
6540 }
6541 cur++;
6542 }
6543 xmlFree(nsList);
Daniel Veillardaa6de472008-08-25 14:53:31 +00006544 }
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006545 if (tmpstr != NULL)
6546 xmlFree(tmpstr);
6547 /*
6548 * Only default/fixed attrs are relevant.
6549 */
6550 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6551 return((xmlAttrPtr) attrDecl);
6552 }
6553#endif /* LIBXML_TREE_ENABLED */
6554 return(NULL);
6555}
6556
6557static xmlChar*
6558xmlGetPropNodeValueInternal(xmlAttrPtr prop)
6559{
6560 if (prop == NULL)
6561 return(NULL);
6562 if (prop->type == XML_ATTRIBUTE_NODE) {
6563 /*
6564 * Note that we return at least the empty string.
6565 * TODO: Do we really always want that?
6566 */
6567 if (prop->children != NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00006568 if ((prop->children->next == NULL) &&
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006569 ((prop->children->type == XML_TEXT_NODE) ||
6570 (prop->children->type == XML_CDATA_SECTION_NODE)))
6571 {
6572 /*
6573 * Optimization for the common case: only 1 text node.
6574 */
6575 return(xmlStrdup(prop->children->content));
6576 } else {
6577 xmlChar *ret;
6578
6579 ret = xmlNodeListGetString(prop->doc, prop->children, 1);
6580 if (ret != NULL)
6581 return(ret);
6582 }
6583 }
6584 return(xmlStrdup((xmlChar *)""));
6585 } else if (prop->type == XML_ATTRIBUTE_DECL) {
6586 return(xmlStrdup(((xmlAttributePtr)prop)->defaultValue));
6587 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00006588 return(NULL);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006589}
6590
Owen Taylor3473f882001-02-23 17:55:21 +00006591/**
6592 * xmlHasProp:
6593 * @node: the node
6594 * @name: the attribute name
6595 *
6596 * Search an attribute associated to a node
6597 * This function also looks in DTD attribute declaration for #FIXED or
6598 * default declaration values unless DTD use has been turned off.
6599 *
Daniel Veillardaa6de472008-08-25 14:53:31 +00006600 * Returns the attribute or the attribute declaration or NULL if
Owen Taylor3473f882001-02-23 17:55:21 +00006601 * neither was found.
6602 */
6603xmlAttrPtr
6604xmlHasProp(xmlNodePtr node, const xmlChar *name) {
6605 xmlAttrPtr prop;
6606 xmlDocPtr doc;
6607
Daniel Veillard8874b942005-08-25 13:19:21 +00006608 if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL))
6609 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006610 /*
6611 * Check on the properties attached to the node
6612 */
6613 prop = node->properties;
6614 while (prop != NULL) {
6615 if (xmlStrEqual(prop->name, name)) {
6616 return(prop);
6617 }
6618 prop = prop->next;
6619 }
6620 if (!xmlCheckDTD) return(NULL);
6621
6622 /*
6623 * Check if there is a default declaration in the internal
6624 * or external subsets
6625 */
6626 doc = node->doc;
6627 if (doc != NULL) {
6628 xmlAttributePtr attrDecl;
6629 if (doc->intSubset != NULL) {
6630 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
6631 if ((attrDecl == NULL) && (doc->extSubset != NULL))
6632 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
Daniel Veillard75eb1ad2003-07-07 14:42:44 +00006633 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6634 /* return attribute declaration only if a default value is given
6635 (that includes #FIXED declarations) */
Owen Taylor3473f882001-02-23 17:55:21 +00006636 return((xmlAttrPtr) attrDecl);
6637 }
6638 }
6639 return(NULL);
6640}
6641
6642/**
Daniel Veillarde95e2392001-06-06 10:46:28 +00006643 * xmlHasNsProp:
6644 * @node: the node
6645 * @name: the attribute name
Daniel Veillardca2366a2001-06-11 12:09:01 +00006646 * @nameSpace: the URI of the namespace
Daniel Veillarde95e2392001-06-06 10:46:28 +00006647 *
6648 * Search for an attribute associated to a node
6649 * This attribute has to be anchored in the namespace specified.
6650 * This does the entity substitution.
6651 * This function looks in DTD attribute declaration for #FIXED or
6652 * default declaration values unless DTD use has been turned off.
William M. Brack2c228442004-10-03 04:10:00 +00006653 * Note that a namespace of NULL indicates to use the default namespace.
Daniel Veillarde95e2392001-06-06 10:46:28 +00006654 *
6655 * Returns the attribute or the attribute declaration or NULL
6656 * if neither was found.
6657 */
6658xmlAttrPtr
Daniel Veillardca2366a2001-06-11 12:09:01 +00006659xmlHasNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
Daniel Veillarde95e2392001-06-06 10:46:28 +00006660
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006661 return(xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD));
Daniel Veillarde95e2392001-06-06 10:46:28 +00006662}
6663
6664/**
Owen Taylor3473f882001-02-23 17:55:21 +00006665 * xmlGetProp:
6666 * @node: the node
6667 * @name: the attribute name
6668 *
6669 * Search and get the value of an attribute associated to a node
6670 * This does the entity substitution.
6671 * This function looks in DTD attribute declaration for #FIXED or
6672 * default declaration values unless DTD use has been turned off.
Daniel Veillard784b9352003-02-16 15:50:27 +00006673 * NOTE: this function acts independently of namespaces associated
Daniel Veillard71531f32003-02-05 13:19:53 +00006674 * to the attribute. Use xmlGetNsProp() or xmlGetNoNsProp()
6675 * for namespace aware processing.
Owen Taylor3473f882001-02-23 17:55:21 +00006676 *
6677 * Returns the attribute value or NULL if not found.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00006678 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00006679 */
6680xmlChar *
6681xmlGetProp(xmlNodePtr node, const xmlChar *name) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00006682 xmlAttrPtr prop;
Owen Taylor3473f882001-02-23 17:55:21 +00006683
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006684 prop = xmlHasProp(node, name);
6685 if (prop == NULL)
6686 return(NULL);
Daniel Veillardaa6de472008-08-25 14:53:31 +00006687 return(xmlGetPropNodeValueInternal(prop));
Owen Taylor3473f882001-02-23 17:55:21 +00006688}
6689
6690/**
Daniel Veillard71531f32003-02-05 13:19:53 +00006691 * xmlGetNoNsProp:
6692 * @node: the node
6693 * @name: the attribute name
6694 *
6695 * Search and get the value of an attribute associated to a node
6696 * This does the entity substitution.
6697 * This function looks in DTD attribute declaration for #FIXED or
6698 * default declaration values unless DTD use has been turned off.
6699 * This function is similar to xmlGetProp except it will accept only
6700 * an attribute in no namespace.
6701 *
6702 * Returns the attribute value or NULL if not found.
6703 * It's up to the caller to free the memory with xmlFree().
6704 */
6705xmlChar *
6706xmlGetNoNsProp(xmlNodePtr node, const xmlChar *name) {
6707 xmlAttrPtr prop;
Daniel Veillardaa6de472008-08-25 14:53:31 +00006708
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006709 prop = xmlGetPropNodeInternal(node, name, NULL, xmlCheckDTD);
6710 if (prop == NULL)
Daniel Veillard8874b942005-08-25 13:19:21 +00006711 return(NULL);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006712 return(xmlGetPropNodeValueInternal(prop));
Daniel Veillard71531f32003-02-05 13:19:53 +00006713}
6714
6715/**
Owen Taylor3473f882001-02-23 17:55:21 +00006716 * xmlGetNsProp:
6717 * @node: the node
6718 * @name: the attribute name
Daniel Veillardca2366a2001-06-11 12:09:01 +00006719 * @nameSpace: the URI of the namespace
Owen Taylor3473f882001-02-23 17:55:21 +00006720 *
6721 * Search and get the value of an attribute associated to a node
6722 * This attribute has to be anchored in the namespace specified.
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 *
6727 * Returns the attribute value or NULL if not found.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00006728 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00006729 */
6730xmlChar *
Daniel Veillardca2366a2001-06-11 12:09:01 +00006731xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
Owen Taylor3473f882001-02-23 17:55:21 +00006732 xmlAttrPtr prop;
Owen Taylor3473f882001-02-23 17:55:21 +00006733
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006734 prop = xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD);
6735 if (prop == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00006736 return(NULL);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006737 return(xmlGetPropNodeValueInternal(prop));
Owen Taylor3473f882001-02-23 17:55:21 +00006738}
6739
Daniel Veillard2156d432004-03-04 15:59:36 +00006740#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
6741/**
6742 * xmlUnsetProp:
6743 * @node: the node
6744 * @name: the attribute name
6745 *
6746 * Remove an attribute carried by a node.
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006747 * This handles only attributes in no namespace.
Daniel Veillard2156d432004-03-04 15:59:36 +00006748 * Returns 0 if successful, -1 if not found
6749 */
6750int
6751xmlUnsetProp(xmlNodePtr node, const xmlChar *name) {
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +00006752 xmlAttrPtr prop;
Daniel Veillard2156d432004-03-04 15:59:36 +00006753
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006754 prop = xmlGetPropNodeInternal(node, name, NULL, 0);
6755 if (prop == NULL)
Daniel Veillard2156d432004-03-04 15:59:36 +00006756 return(-1);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006757 xmlUnlinkNode((xmlNodePtr) prop);
6758 xmlFreeProp(prop);
6759 return(0);
Daniel Veillard2156d432004-03-04 15:59:36 +00006760}
6761
6762/**
6763 * xmlUnsetNsProp:
6764 * @node: the node
6765 * @ns: the namespace definition
6766 * @name: the attribute name
6767 *
6768 * Remove an attribute carried by a node.
6769 * Returns 0 if successful, -1 if not found
6770 */
6771int
6772xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) {
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +00006773 xmlAttrPtr prop;
Daniel Veillardaa6de472008-08-25 14:53:31 +00006774
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006775 prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0);
6776 if (prop == NULL)
Daniel Veillard2156d432004-03-04 15:59:36 +00006777 return(-1);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006778 xmlUnlinkNode((xmlNodePtr) prop);
6779 xmlFreeProp(prop);
6780 return(0);
Daniel Veillard2156d432004-03-04 15:59:36 +00006781}
6782#endif
6783
6784#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_HTML_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00006785/**
6786 * xmlSetProp:
6787 * @node: the node
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006788 * @name: the attribute name (a QName)
Owen Taylor3473f882001-02-23 17:55:21 +00006789 * @value: the attribute value
6790 *
6791 * Set (or reset) an attribute carried by a node.
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006792 * If @name has a prefix, then the corresponding
6793 * namespace-binding will be used, if in scope; it is an
6794 * error it there's no such ns-binding for the prefix in
6795 * scope.
Owen Taylor3473f882001-02-23 17:55:21 +00006796 * Returns the attribute pointer.
Daniel Veillardaa6de472008-08-25 14:53:31 +00006797 *
Owen Taylor3473f882001-02-23 17:55:21 +00006798 */
6799xmlAttrPtr
6800xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00006801 int len;
6802 const xmlChar *nqname;
Owen Taylor3473f882001-02-23 17:55:21 +00006803
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006804 if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE))
Owen Taylor3473f882001-02-23 17:55:21 +00006805 return(NULL);
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00006806
6807 /*
6808 * handle QNames
6809 */
6810 nqname = xmlSplitQName3(name, &len);
6811 if (nqname != NULL) {
6812 xmlNsPtr ns;
6813 xmlChar *prefix = xmlStrndup(name, len);
6814 ns = xmlSearchNs(node->doc, node, prefix);
6815 if (prefix != NULL)
6816 xmlFree(prefix);
6817 if (ns != NULL)
6818 return(xmlSetNsProp(node, ns, nqname, value));
6819 }
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006820 return(xmlSetNsProp(node, NULL, name, value));
Owen Taylor3473f882001-02-23 17:55:21 +00006821}
6822
6823/**
6824 * xmlSetNsProp:
6825 * @node: the node
6826 * @ns: the namespace definition
6827 * @name: the attribute name
6828 * @value: the attribute value
6829 *
6830 * Set (or reset) an attribute carried by a node.
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006831 * The ns structure must be in scope, this is not checked
Owen Taylor3473f882001-02-23 17:55:21 +00006832 *
6833 * Returns the attribute pointer.
6834 */
6835xmlAttrPtr
6836xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006837 const xmlChar *value)
6838{
Owen Taylor3473f882001-02-23 17:55:21 +00006839 xmlAttrPtr prop;
Daniel Veillardaa6de472008-08-25 14:53:31 +00006840
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006841 if (ns && (ns->href == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00006842 return(NULL);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006843 prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0);
6844 if (prop != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00006845 /*
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006846 * Modify the attribute's value.
6847 */
6848 if (prop->atype == XML_ATTRIBUTE_ID) {
6849 xmlRemoveID(node->doc, prop);
6850 prop->atype = XML_ATTRIBUTE_ID;
6851 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00006852 if (prop->children != NULL)
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006853 xmlFreeNodeList(prop->children);
6854 prop->children = NULL;
6855 prop->last = NULL;
6856 prop->ns = ns;
6857 if (value != NULL) {
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006858 xmlNodePtr tmp;
Daniel Veillardaa6de472008-08-25 14:53:31 +00006859
Daniel Veillard6f8611f2008-02-15 08:33:21 +00006860 if(!xmlCheckUTF8(value)) {
6861 xmlTreeErr(XML_TREE_NOT_UTF8, (xmlNodePtr) node->doc,
6862 NULL);
6863 if (node->doc != NULL)
6864 node->doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
6865 }
6866 prop->children = xmlNewDocText(node->doc, value);
Owen Taylor3473f882001-02-23 17:55:21 +00006867 prop->last = NULL;
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006868 tmp = prop->children;
6869 while (tmp != NULL) {
6870 tmp->parent = (xmlNodePtr) prop;
6871 if (tmp->next == NULL)
6872 prop->last = tmp;
6873 tmp = tmp->next;
6874 }
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006875 }
6876 if (prop->atype == XML_ATTRIBUTE_ID)
6877 xmlAddID(NULL, node->doc, value, prop);
6878 return(prop);
Owen Taylor3473f882001-02-23 17:55:21 +00006879 }
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006880 /*
6881 * No equal attr found; create a new one.
6882 */
6883 return(xmlNewPropInternal(node, ns, name, value, 0));
Owen Taylor3473f882001-02-23 17:55:21 +00006884}
6885
Daniel Veillard652327a2003-09-29 18:02:38 +00006886#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard75bea542001-05-11 17:41:21 +00006887
6888/**
Owen Taylor3473f882001-02-23 17:55:21 +00006889 * xmlNodeIsText:
6890 * @node: the node
Daniel Veillardaa6de472008-08-25 14:53:31 +00006891 *
Owen Taylor3473f882001-02-23 17:55:21 +00006892 * Is this node a Text node ?
6893 * Returns 1 yes, 0 no
6894 */
6895int
6896xmlNodeIsText(xmlNodePtr node) {
6897 if (node == NULL) return(0);
6898
6899 if (node->type == XML_TEXT_NODE) return(1);
6900 return(0);
6901}
6902
6903/**
6904 * xmlIsBlankNode:
6905 * @node: the node
Daniel Veillardaa6de472008-08-25 14:53:31 +00006906 *
Owen Taylor3473f882001-02-23 17:55:21 +00006907 * Checks whether this node is an empty or whitespace only
6908 * (and possibly ignorable) text-node.
6909 *
6910 * Returns 1 yes, 0 no
6911 */
6912int
6913xmlIsBlankNode(xmlNodePtr node) {
6914 const xmlChar *cur;
6915 if (node == NULL) return(0);
6916
Daniel Veillard7db37732001-07-12 01:20:08 +00006917 if ((node->type != XML_TEXT_NODE) &&
6918 (node->type != XML_CDATA_SECTION_NODE))
6919 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006920 if (node->content == NULL) return(1);
Owen Taylor3473f882001-02-23 17:55:21 +00006921 cur = node->content;
Owen Taylor3473f882001-02-23 17:55:21 +00006922 while (*cur != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00006923 if (!IS_BLANK_CH(*cur)) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006924 cur++;
6925 }
6926
6927 return(1);
6928}
6929
6930/**
6931 * xmlTextConcat:
6932 * @node: the node
6933 * @content: the content
Daniel Veillard60087f32001-10-10 09:45:09 +00006934 * @len: @content length
Daniel Veillardaa6de472008-08-25 14:53:31 +00006935 *
Owen Taylor3473f882001-02-23 17:55:21 +00006936 * Concat the given string at the end of the existing node content
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006937 *
6938 * Returns -1 in case of error, 0 otherwise
Owen Taylor3473f882001-02-23 17:55:21 +00006939 */
6940
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006941int
Owen Taylor3473f882001-02-23 17:55:21 +00006942xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006943 if (node == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006944
6945 if ((node->type != XML_TEXT_NODE) &&
Rob Richardsa02f1992006-09-16 14:04:26 +00006946 (node->type != XML_CDATA_SECTION_NODE) &&
6947 (node->type != XML_COMMENT_NODE) &&
6948 (node->type != XML_PI_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006949#ifdef DEBUG_TREE
6950 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006951 "xmlTextConcat: node is not text nor CDATA\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006952#endif
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006953 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006954 }
William M. Brack7762bb12004-01-04 14:49:01 +00006955 /* need to check if content is currently in the dictionary */
Daniel Veillard8874b942005-08-25 13:19:21 +00006956 if ((node->content == (xmlChar *) &(node->properties)) ||
6957 ((node->doc != NULL) && (node->doc->dict != NULL) &&
6958 xmlDictOwns(node->doc->dict, node->content))) {
William M. Brack7762bb12004-01-04 14:49:01 +00006959 node->content = xmlStrncatNew(node->content, content, len);
6960 } else {
6961 node->content = xmlStrncat(node->content, content, len);
6962 }
Daniel Veillard8874b942005-08-25 13:19:21 +00006963 node->properties = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006964 if (node->content == NULL)
6965 return(-1);
6966 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006967}
6968
6969/************************************************************************
6970 * *
6971 * Output : to a FILE or in memory *
6972 * *
6973 ************************************************************************/
6974
Owen Taylor3473f882001-02-23 17:55:21 +00006975/**
6976 * xmlBufferCreate:
6977 *
6978 * routine to create an XML buffer.
6979 * returns the new structure.
6980 */
6981xmlBufferPtr
6982xmlBufferCreate(void) {
6983 xmlBufferPtr ret;
6984
6985 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6986 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006987 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006988 return(NULL);
6989 }
6990 ret->use = 0;
Daniel Veillarde356c282001-03-10 12:32:04 +00006991 ret->size = xmlDefaultBufferSize;
Owen Taylor3473f882001-02-23 17:55:21 +00006992 ret->alloc = xmlBufferAllocScheme;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00006993 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00006994 if (ret->content == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006995 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006996 xmlFree(ret);
6997 return(NULL);
6998 }
6999 ret->content[0] = 0;
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007000 ret->contentIO = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00007001 return(ret);
7002}
7003
7004/**
7005 * xmlBufferCreateSize:
7006 * @size: initial size of buffer
7007 *
7008 * routine to create an XML buffer.
7009 * returns the new structure.
7010 */
7011xmlBufferPtr
7012xmlBufferCreateSize(size_t size) {
7013 xmlBufferPtr ret;
7014
7015 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
7016 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007017 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00007018 return(NULL);
7019 }
7020 ret->use = 0;
7021 ret->alloc = xmlBufferAllocScheme;
7022 ret->size = (size ? size+2 : 0); /* +1 for ending null */
7023 if (ret->size){
Daniel Veillard3c908dc2003-04-19 00:07:51 +00007024 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00007025 if (ret->content == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007026 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00007027 xmlFree(ret);
7028 return(NULL);
7029 }
7030 ret->content[0] = 0;
7031 } else
7032 ret->content = NULL;
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007033 ret->contentIO = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00007034 return(ret);
7035}
7036
7037/**
Daniel Veillard79ee2842012-05-15 10:25:31 +08007038 * xmlBufferDetach:
Conrad Irwin7d553f82012-05-10 20:17:25 -07007039 * @buf: the buffer
7040 *
Daniel Veillard79ee2842012-05-15 10:25:31 +08007041 * Remove the string contained in a buffer and gie it back to the
Daniel Veillard94431ec2012-05-15 10:45:05 +08007042 * caller. The buffer is reset to an empty content.
7043 * This doesn't work with immutable buffers as they can't be reset.
Conrad Irwin7d553f82012-05-10 20:17:25 -07007044 *
Daniel Veillard79ee2842012-05-15 10:25:31 +08007045 * Returns the previous string contained by the buffer.
Conrad Irwin7d553f82012-05-10 20:17:25 -07007046 */
7047xmlChar *
7048xmlBufferDetach(xmlBufferPtr buf) {
7049 xmlChar *ret;
7050
Daniel Veillard94431ec2012-05-15 10:45:05 +08007051 if (buf == NULL)
7052 return(NULL);
7053 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE)
7054 return(NULL);
Conrad Irwin7d553f82012-05-10 20:17:25 -07007055
7056 ret = buf->content;
7057 buf->content = NULL;
7058 buf->size = 0;
7059 buf->use = 0;
7060
7061 return ret;
7062}
7063
7064
7065/**
Daniel Veillard53350552003-09-18 13:35:51 +00007066 * xmlBufferCreateStatic:
7067 * @mem: the memory area
7068 * @size: the size in byte
7069 *
MST 2003 John Flecka0e7e932003-12-19 03:13:47 +00007070 * routine to create an XML buffer from an immutable memory area.
7071 * The area won't be modified nor copied, and is expected to be
Daniel Veillard53350552003-09-18 13:35:51 +00007072 * present until the end of the buffer lifetime.
7073 *
7074 * returns the new structure.
7075 */
7076xmlBufferPtr
7077xmlBufferCreateStatic(void *mem, size_t size) {
7078 xmlBufferPtr ret;
7079
7080 if ((mem == NULL) || (size == 0))
7081 return(NULL);
7082
7083 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
7084 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007085 xmlTreeErrMemory("creating buffer");
Daniel Veillard53350552003-09-18 13:35:51 +00007086 return(NULL);
7087 }
7088 ret->use = size;
7089 ret->size = size;
7090 ret->alloc = XML_BUFFER_ALLOC_IMMUTABLE;
7091 ret->content = (xmlChar *) mem;
7092 return(ret);
7093}
7094
7095/**
Owen Taylor3473f882001-02-23 17:55:21 +00007096 * xmlBufferSetAllocationScheme:
Daniel Veillardbd9afb52002-09-25 22:25:35 +00007097 * @buf: the buffer to tune
Owen Taylor3473f882001-02-23 17:55:21 +00007098 * @scheme: allocation scheme to use
7099 *
7100 * Sets the allocation scheme for this buffer
7101 */
7102void
Daniel Veillardaa6de472008-08-25 14:53:31 +00007103xmlBufferSetAllocationScheme(xmlBufferPtr buf,
Owen Taylor3473f882001-02-23 17:55:21 +00007104 xmlBufferAllocationScheme scheme) {
7105 if (buf == NULL) {
7106#ifdef DEBUG_BUFFER
7107 xmlGenericError(xmlGenericErrorContext,
7108 "xmlBufferSetAllocationScheme: buf == NULL\n");
7109#endif
7110 return;
7111 }
Daniel Veillardda3fee42008-09-01 13:08:57 +00007112 if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) ||
7113 (buf->alloc == XML_BUFFER_ALLOC_IO)) return;
7114 if ((scheme == XML_BUFFER_ALLOC_DOUBLEIT) ||
7115 (scheme == XML_BUFFER_ALLOC_EXACT) ||
Conrad Irwin7d0d2a52012-05-14 14:18:58 +08007116 (scheme == XML_BUFFER_ALLOC_HYBRID) ||
Daniel Veillardda3fee42008-09-01 13:08:57 +00007117 (scheme == XML_BUFFER_ALLOC_IMMUTABLE))
7118 buf->alloc = scheme;
Owen Taylor3473f882001-02-23 17:55:21 +00007119}
7120
7121/**
7122 * xmlBufferFree:
7123 * @buf: the buffer to free
7124 *
Daniel Veillard9d06d302002-01-22 18:15:52 +00007125 * Frees an XML buffer. It frees both the content and the structure which
7126 * encapsulate it.
Owen Taylor3473f882001-02-23 17:55:21 +00007127 */
7128void
7129xmlBufferFree(xmlBufferPtr buf) {
7130 if (buf == NULL) {
7131#ifdef DEBUG_BUFFER
7132 xmlGenericError(xmlGenericErrorContext,
7133 "xmlBufferFree: buf == NULL\n");
7134#endif
7135 return;
7136 }
Daniel Veillard53350552003-09-18 13:35:51 +00007137
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007138 if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
7139 (buf->contentIO != NULL)) {
7140 xmlFree(buf->contentIO);
7141 } else if ((buf->content != NULL) &&
Daniel Veillard53350552003-09-18 13:35:51 +00007142 (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00007143 xmlFree(buf->content);
7144 }
Owen Taylor3473f882001-02-23 17:55:21 +00007145 xmlFree(buf);
7146}
7147
7148/**
7149 * xmlBufferEmpty:
7150 * @buf: the buffer
7151 *
7152 * empty a buffer.
7153 */
7154void
7155xmlBufferEmpty(xmlBufferPtr buf) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007156 if (buf == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +00007157 if (buf->content == NULL) return;
7158 buf->use = 0;
Daniel Veillard53350552003-09-18 13:35:51 +00007159 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00007160 buf->content = BAD_CAST "";
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007161 } else if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
7162 (buf->contentIO != NULL)) {
7163 size_t start_buf = buf->content - buf->contentIO;
7164
7165 buf->size += start_buf;
7166 buf->content = buf->contentIO;
7167 buf->content[0] = 0;
Daniel Veillard53350552003-09-18 13:35:51 +00007168 } else {
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007169 buf->content[0] = 0;
7170 }
Owen Taylor3473f882001-02-23 17:55:21 +00007171}
7172
7173/**
7174 * xmlBufferShrink:
7175 * @buf: the buffer to dump
7176 * @len: the number of xmlChar to remove
7177 *
7178 * Remove the beginning of an XML buffer.
7179 *
Daniel Veillardd1640922001-12-17 15:30:10 +00007180 * Returns the number of #xmlChar removed, or -1 in case of failure.
Owen Taylor3473f882001-02-23 17:55:21 +00007181 */
7182int
7183xmlBufferShrink(xmlBufferPtr buf, unsigned int len) {
Daniel Veillard3d97e662004-11-04 10:49:00 +00007184 if (buf == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00007185 if (len == 0) return(0);
7186 if (len > buf->use) return(-1);
7187
7188 buf->use -= len;
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007189 if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) ||
7190 ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL))) {
7191 /*
7192 * we just move the content pointer, but also make sure
7193 * the perceived buffer size has shrinked accordingly
7194 */
Daniel Veillard53350552003-09-18 13:35:51 +00007195 buf->content += len;
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007196 buf->size -= len;
7197
7198 /*
7199 * sometimes though it maybe be better to really shrink
7200 * on IO buffers
7201 */
7202 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7203 size_t start_buf = buf->content - buf->contentIO;
7204 if (start_buf >= buf->size) {
7205 memmove(buf->contentIO, &buf->content[0], buf->use);
7206 buf->content = buf->contentIO;
7207 buf->content[buf->use] = 0;
7208 buf->size += start_buf;
7209 }
7210 }
Daniel Veillard53350552003-09-18 13:35:51 +00007211 } else {
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007212 memmove(buf->content, &buf->content[len], buf->use);
Daniel Veillard53350552003-09-18 13:35:51 +00007213 buf->content[buf->use] = 0;
7214 }
Owen Taylor3473f882001-02-23 17:55:21 +00007215 return(len);
7216}
7217
7218/**
7219 * xmlBufferGrow:
7220 * @buf: the buffer
7221 * @len: the minimum free size to allocate
7222 *
7223 * Grow the available space of an XML buffer.
7224 *
7225 * Returns the new available space or -1 in case of error
7226 */
7227int
7228xmlBufferGrow(xmlBufferPtr buf, unsigned int len) {
7229 int size;
7230 xmlChar *newbuf;
7231
Daniel Veillard3d97e662004-11-04 10:49:00 +00007232 if (buf == NULL) return(-1);
7233
Daniel Veillard53350552003-09-18 13:35:51 +00007234 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00007235 if (len + buf->use < buf->size) return(0);
7236
Daniel Veillardee20cd72009-08-22 15:18:31 +02007237 /*
7238 * Windows has a BIG problem on realloc timing, so we try to double
7239 * the buffer size (if that's enough) (bug 146697)
7240 * Apparently BSD too, and it's probably best for linux too
7241 * On an embedded system this may be something to change
7242 */
7243#if 1
William M. Brack30fe43f2004-07-26 18:00:58 +00007244 if (buf->size > len)
7245 size = buf->size * 2;
7246 else
7247 size = buf->use + len + 100;
7248#else
Owen Taylor3473f882001-02-23 17:55:21 +00007249 size = buf->use + len + 100;
William M. Brack30fe43f2004-07-26 18:00:58 +00007250#endif
Owen Taylor3473f882001-02-23 17:55:21 +00007251
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007252 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7253 size_t start_buf = buf->content - buf->contentIO;
7254
7255 newbuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + size);
7256 if (newbuf == NULL) {
7257 xmlTreeErrMemory("growing buffer");
7258 return(-1);
7259 }
7260 buf->contentIO = newbuf;
7261 buf->content = newbuf + start_buf;
7262 } else {
7263 newbuf = (xmlChar *) xmlRealloc(buf->content, size);
7264 if (newbuf == NULL) {
7265 xmlTreeErrMemory("growing buffer");
7266 return(-1);
7267 }
7268 buf->content = newbuf;
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007269 }
Owen Taylor3473f882001-02-23 17:55:21 +00007270 buf->size = size;
7271 return(buf->size - buf->use);
7272}
7273
7274/**
7275 * xmlBufferDump:
7276 * @file: the file output
7277 * @buf: the buffer to dump
7278 *
7279 * Dumps an XML buffer to a FILE *.
Daniel Veillardd1640922001-12-17 15:30:10 +00007280 * Returns the number of #xmlChar written
Owen Taylor3473f882001-02-23 17:55:21 +00007281 */
7282int
7283xmlBufferDump(FILE *file, xmlBufferPtr buf) {
7284 int ret;
7285
7286 if (buf == NULL) {
7287#ifdef DEBUG_BUFFER
7288 xmlGenericError(xmlGenericErrorContext,
7289 "xmlBufferDump: buf == NULL\n");
7290#endif
7291 return(0);
7292 }
7293 if (buf->content == NULL) {
7294#ifdef DEBUG_BUFFER
7295 xmlGenericError(xmlGenericErrorContext,
7296 "xmlBufferDump: buf->content == NULL\n");
7297#endif
7298 return(0);
7299 }
Daniel Veillardcd337f02001-11-22 18:20:37 +00007300 if (file == NULL)
7301 file = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00007302 ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
7303 return(ret);
7304}
7305
7306/**
7307 * xmlBufferContent:
7308 * @buf: the buffer
7309 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00007310 * Function to extract the content of a buffer
7311 *
Owen Taylor3473f882001-02-23 17:55:21 +00007312 * Returns the internal content
7313 */
7314
Daniel Veillard5e2dace2001-07-18 19:30:27 +00007315const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00007316xmlBufferContent(const xmlBufferPtr buf)
7317{
7318 if(!buf)
7319 return NULL;
7320
7321 return buf->content;
7322}
7323
7324/**
7325 * xmlBufferLength:
Daniel Veillardaa6de472008-08-25 14:53:31 +00007326 * @buf: the buffer
Owen Taylor3473f882001-02-23 17:55:21 +00007327 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00007328 * Function to get the length of a buffer
7329 *
Owen Taylor3473f882001-02-23 17:55:21 +00007330 * Returns the length of data in the internal content
7331 */
7332
7333int
7334xmlBufferLength(const xmlBufferPtr buf)
7335{
7336 if(!buf)
7337 return 0;
7338
7339 return buf->use;
7340}
7341
7342/**
7343 * xmlBufferResize:
7344 * @buf: the buffer to resize
7345 * @size: the desired size
7346 *
Daniel Veillardd1640922001-12-17 15:30:10 +00007347 * Resize a buffer to accommodate minimum size of @size.
Owen Taylor3473f882001-02-23 17:55:21 +00007348 *
7349 * Returns 0 in case of problems, 1 otherwise
7350 */
7351int
7352xmlBufferResize(xmlBufferPtr buf, unsigned int size)
7353{
7354 unsigned int newSize;
7355 xmlChar* rebuf = NULL;
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007356 size_t start_buf;
Owen Taylor3473f882001-02-23 17:55:21 +00007357
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007358 if (buf == NULL)
7359 return(0);
7360
Daniel Veillard53350552003-09-18 13:35:51 +00007361 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
7362
Owen Taylor3473f882001-02-23 17:55:21 +00007363 /* Don't resize if we don't have to */
7364 if (size < buf->size)
7365 return 1;
7366
7367 /* figure out new size */
7368 switch (buf->alloc){
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007369 case XML_BUFFER_ALLOC_IO:
7370 case XML_BUFFER_ALLOC_DOUBLEIT:
7371 /*take care of empty case*/
7372 newSize = (buf->size ? buf->size*2 : size + 10);
Daniel Veillard1dc9feb2008-11-17 15:59:21 +00007373 while (size > newSize) {
7374 if (newSize > UINT_MAX / 2) {
7375 xmlTreeErrMemory("growing buffer");
7376 return 0;
7377 }
7378 newSize *= 2;
7379 }
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007380 break;
7381 case XML_BUFFER_ALLOC_EXACT:
7382 newSize = size+10;
7383 break;
Conrad Irwin7d0d2a52012-05-14 14:18:58 +08007384 case XML_BUFFER_ALLOC_HYBRID:
7385 if (buf->use < BASE_BUFFER_SIZE)
7386 newSize = size;
7387 else {
7388 newSize = buf->size * 2;
7389 while (size > newSize) {
7390 if (newSize > UINT_MAX / 2) {
7391 xmlTreeErrMemory("growing buffer");
7392 return 0;
7393 }
7394 newSize *= 2;
7395 }
7396 }
7397 break;
7398
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007399 default:
7400 newSize = size+10;
7401 break;
Owen Taylor3473f882001-02-23 17:55:21 +00007402 }
7403
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007404 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7405 start_buf = buf->content - buf->contentIO;
7406
7407 if (start_buf > newSize) {
7408 /* move data back to start */
7409 memmove(buf->contentIO, buf->content, buf->use);
7410 buf->content = buf->contentIO;
7411 buf->content[buf->use] = 0;
7412 buf->size += start_buf;
7413 } else {
7414 rebuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + newSize);
7415 if (rebuf == NULL) {
7416 xmlTreeErrMemory("growing buffer");
7417 return 0;
7418 }
7419 buf->contentIO = rebuf;
7420 buf->content = rebuf + start_buf;
Daniel Veillard6155d8a2003-08-19 15:01:28 +00007421 }
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007422 } else {
7423 if (buf->content == NULL) {
7424 rebuf = (xmlChar *) xmlMallocAtomic(newSize);
7425 } else if (buf->size - buf->use < 100) {
7426 rebuf = (xmlChar *) xmlRealloc(buf->content, newSize);
7427 } else {
7428 /*
7429 * if we are reallocating a buffer far from being full, it's
7430 * better to make a new allocation and copy only the used range
7431 * and free the old one.
7432 */
7433 rebuf = (xmlChar *) xmlMallocAtomic(newSize);
7434 if (rebuf != NULL) {
7435 memcpy(rebuf, buf->content, buf->use);
7436 xmlFree(buf->content);
7437 rebuf[buf->use] = 0;
7438 }
7439 }
7440 if (rebuf == NULL) {
7441 xmlTreeErrMemory("growing buffer");
7442 return 0;
7443 }
7444 buf->content = rebuf;
Daniel Veillard6155d8a2003-08-19 15:01:28 +00007445 }
Owen Taylor3473f882001-02-23 17:55:21 +00007446 buf->size = newSize;
7447
7448 return 1;
7449}
7450
7451/**
7452 * xmlBufferAdd:
7453 * @buf: the buffer to dump
Daniel Veillardd1640922001-12-17 15:30:10 +00007454 * @str: the #xmlChar string
7455 * @len: the number of #xmlChar to add
Owen Taylor3473f882001-02-23 17:55:21 +00007456 *
Daniel Veillard60087f32001-10-10 09:45:09 +00007457 * Add a string range to an XML buffer. if len == -1, the length of
Owen Taylor3473f882001-02-23 17:55:21 +00007458 * str is recomputed.
William M. Bracka3215c72004-07-31 16:24:01 +00007459 *
7460 * Returns 0 successful, a positive error code number otherwise
7461 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00007462 */
William M. Bracka3215c72004-07-31 16:24:01 +00007463int
Owen Taylor3473f882001-02-23 17:55:21 +00007464xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) {
7465 unsigned int needSize;
7466
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007467 if ((str == NULL) || (buf == NULL)) {
William M. Bracka3215c72004-07-31 16:24:01 +00007468 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007469 }
William M. Bracka3215c72004-07-31 16:24:01 +00007470 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007471 if (len < -1) {
7472#ifdef DEBUG_BUFFER
7473 xmlGenericError(xmlGenericErrorContext,
7474 "xmlBufferAdd: len < 0\n");
7475#endif
William M. Bracka3215c72004-07-31 16:24:01 +00007476 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007477 }
William M. Bracka3215c72004-07-31 16:24:01 +00007478 if (len == 0) return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007479
7480 if (len < 0)
7481 len = xmlStrlen(str);
7482
Daniel Veillardc9923322007-04-24 18:12:06 +00007483 if (len < 0) return -1;
7484 if (len == 0) return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007485
7486 needSize = buf->use + len + 2;
7487 if (needSize > buf->size){
7488 if (!xmlBufferResize(buf, needSize)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007489 xmlTreeErrMemory("growing buffer");
William M. Bracka3215c72004-07-31 16:24:01 +00007490 return XML_ERR_NO_MEMORY;
Owen Taylor3473f882001-02-23 17:55:21 +00007491 }
7492 }
7493
7494 memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
7495 buf->use += len;
7496 buf->content[buf->use] = 0;
William M. Bracka3215c72004-07-31 16:24:01 +00007497 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007498}
7499
7500/**
7501 * xmlBufferAddHead:
7502 * @buf: the buffer
Daniel Veillardd1640922001-12-17 15:30:10 +00007503 * @str: the #xmlChar string
7504 * @len: the number of #xmlChar to add
Owen Taylor3473f882001-02-23 17:55:21 +00007505 *
7506 * Add a string range to the beginning of an XML buffer.
Daniel Veillard60087f32001-10-10 09:45:09 +00007507 * if len == -1, the length of @str is recomputed.
William M. Bracka3215c72004-07-31 16:24:01 +00007508 *
7509 * Returns 0 successful, a positive error code number otherwise
7510 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00007511 */
William M. Bracka3215c72004-07-31 16:24:01 +00007512int
Owen Taylor3473f882001-02-23 17:55:21 +00007513xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) {
7514 unsigned int needSize;
7515
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007516 if (buf == NULL)
7517 return(-1);
William M. Bracka3215c72004-07-31 16:24:01 +00007518 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007519 if (str == NULL) {
7520#ifdef DEBUG_BUFFER
7521 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007522 "xmlBufferAddHead: str == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007523#endif
William M. Bracka3215c72004-07-31 16:24:01 +00007524 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007525 }
7526 if (len < -1) {
7527#ifdef DEBUG_BUFFER
7528 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007529 "xmlBufferAddHead: len < 0\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007530#endif
William M. Bracka3215c72004-07-31 16:24:01 +00007531 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007532 }
William M. Bracka3215c72004-07-31 16:24:01 +00007533 if (len == 0) return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007534
7535 if (len < 0)
7536 len = xmlStrlen(str);
7537
William M. Bracka3215c72004-07-31 16:24:01 +00007538 if (len <= 0) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007539
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007540 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7541 size_t start_buf = buf->content - buf->contentIO;
7542
7543 if (start_buf > (unsigned int) len) {
7544 /*
7545 * We can add it in the space previously shrinked
7546 */
7547 buf->content -= len;
7548 memmove(&buf->content[0], str, len);
7549 buf->use += len;
7550 buf->size += len;
7551 return(0);
7552 }
7553 }
Owen Taylor3473f882001-02-23 17:55:21 +00007554 needSize = buf->use + len + 2;
7555 if (needSize > buf->size){
7556 if (!xmlBufferResize(buf, needSize)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007557 xmlTreeErrMemory("growing buffer");
William M. Bracka3215c72004-07-31 16:24:01 +00007558 return XML_ERR_NO_MEMORY;
Owen Taylor3473f882001-02-23 17:55:21 +00007559 }
7560 }
7561
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007562 memmove(&buf->content[len], &buf->content[0], buf->use);
7563 memmove(&buf->content[0], str, len);
Owen Taylor3473f882001-02-23 17:55:21 +00007564 buf->use += len;
7565 buf->content[buf->use] = 0;
William M. Bracka3215c72004-07-31 16:24:01 +00007566 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007567}
7568
7569/**
7570 * xmlBufferCat:
William M. Bracka3215c72004-07-31 16:24:01 +00007571 * @buf: the buffer to add to
Daniel Veillardd1640922001-12-17 15:30:10 +00007572 * @str: the #xmlChar string
Owen Taylor3473f882001-02-23 17:55:21 +00007573 *
7574 * Append a zero terminated string to an XML buffer.
William M. Bracka3215c72004-07-31 16:24:01 +00007575 *
7576 * Returns 0 successful, a positive error code number otherwise
7577 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00007578 */
William M. Bracka3215c72004-07-31 16:24:01 +00007579int
Owen Taylor3473f882001-02-23 17:55:21 +00007580xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007581 if (buf == NULL)
7582 return(-1);
William M. Bracka3215c72004-07-31 16:24:01 +00007583 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7584 if (str == NULL) return -1;
7585 return xmlBufferAdd(buf, str, -1);
Owen Taylor3473f882001-02-23 17:55:21 +00007586}
7587
7588/**
7589 * xmlBufferCCat:
7590 * @buf: the buffer to dump
7591 * @str: the C char string
7592 *
7593 * Append a zero terminated C string to an XML buffer.
William M. Bracka3215c72004-07-31 16:24:01 +00007594 *
7595 * Returns 0 successful, a positive error code number otherwise
7596 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00007597 */
William M. Bracka3215c72004-07-31 16:24:01 +00007598int
Owen Taylor3473f882001-02-23 17:55:21 +00007599xmlBufferCCat(xmlBufferPtr buf, const char *str) {
7600 const char *cur;
7601
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007602 if (buf == NULL)
7603 return(-1);
William M. Bracka3215c72004-07-31 16:24:01 +00007604 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007605 if (str == NULL) {
7606#ifdef DEBUG_BUFFER
7607 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007608 "xmlBufferCCat: str == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007609#endif
William M. Bracka3215c72004-07-31 16:24:01 +00007610 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007611 }
7612 for (cur = str;*cur != 0;cur++) {
7613 if (buf->use + 10 >= buf->size) {
7614 if (!xmlBufferResize(buf, buf->use+10)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007615 xmlTreeErrMemory("growing buffer");
William M. Bracka3215c72004-07-31 16:24:01 +00007616 return XML_ERR_NO_MEMORY;
Owen Taylor3473f882001-02-23 17:55:21 +00007617 }
7618 }
7619 buf->content[buf->use++] = *cur;
7620 }
7621 buf->content[buf->use] = 0;
William M. Bracka3215c72004-07-31 16:24:01 +00007622 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007623}
7624
7625/**
7626 * xmlBufferWriteCHAR:
7627 * @buf: the XML buffer
7628 * @string: the string to add
7629 *
7630 * routine which manages and grows an output buffer. This one adds
7631 * xmlChars at the end of the buffer.
7632 */
7633void
Daniel Veillard53350552003-09-18 13:35:51 +00007634xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007635 if (buf == NULL)
7636 return;
Daniel Veillard53350552003-09-18 13:35:51 +00007637 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00007638 xmlBufferCat(buf, string);
7639}
7640
7641/**
7642 * xmlBufferWriteChar:
7643 * @buf: the XML buffer output
7644 * @string: the string to add
7645 *
7646 * routine which manage and grows an output buffer. This one add
7647 * C chars at the end of the array.
7648 */
7649void
7650xmlBufferWriteChar(xmlBufferPtr buf, const char *string) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007651 if (buf == NULL)
7652 return;
Daniel Veillard53350552003-09-18 13:35:51 +00007653 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00007654 xmlBufferCCat(buf, string);
7655}
7656
7657
7658/**
7659 * xmlBufferWriteQuotedString:
7660 * @buf: the XML buffer output
7661 * @string: the string to add
7662 *
7663 * routine which manage and grows an output buffer. This one writes
Daniel Veillardd1640922001-12-17 15:30:10 +00007664 * a quoted or double quoted #xmlChar string, checking first if it holds
Owen Taylor3473f882001-02-23 17:55:21 +00007665 * quote or double-quotes internally
7666 */
7667void
7668xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) {
Daniel Veillard39057f42003-08-04 01:33:43 +00007669 const xmlChar *cur, *base;
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007670 if (buf == NULL)
7671 return;
Daniel Veillard53350552003-09-18 13:35:51 +00007672 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Daniel Veillard39057f42003-08-04 01:33:43 +00007673 if (xmlStrchr(string, '\"')) {
Daniel Veillard20aa0fb2003-08-04 19:43:15 +00007674 if (xmlStrchr(string, '\'')) {
Owen Taylor3473f882001-02-23 17:55:21 +00007675#ifdef DEBUG_BUFFER
7676 xmlGenericError(xmlGenericErrorContext,
7677 "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n");
7678#endif
Daniel Veillard39057f42003-08-04 01:33:43 +00007679 xmlBufferCCat(buf, "\"");
7680 base = cur = string;
7681 while(*cur != 0){
7682 if(*cur == '"'){
7683 if (base != cur)
7684 xmlBufferAdd(buf, base, cur - base);
7685 xmlBufferAdd(buf, BAD_CAST "&quot;", 6);
7686 cur++;
7687 base = cur;
7688 }
7689 else {
7690 cur++;
7691 }
7692 }
7693 if (base != cur)
7694 xmlBufferAdd(buf, base, cur - base);
7695 xmlBufferCCat(buf, "\"");
Owen Taylor3473f882001-02-23 17:55:21 +00007696 }
Daniel Veillard39057f42003-08-04 01:33:43 +00007697 else{
7698 xmlBufferCCat(buf, "\'");
7699 xmlBufferCat(buf, string);
7700 xmlBufferCCat(buf, "\'");
7701 }
Owen Taylor3473f882001-02-23 17:55:21 +00007702 } else {
7703 xmlBufferCCat(buf, "\"");
7704 xmlBufferCat(buf, string);
7705 xmlBufferCCat(buf, "\"");
7706 }
7707}
7708
7709
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007710/**
7711 * xmlGetDocCompressMode:
7712 * @doc: the document
7713 *
7714 * get the compression ratio for a document, ZLIB based
7715 * Returns 0 (uncompressed) to 9 (max compression)
7716 */
7717int
7718xmlGetDocCompressMode (xmlDocPtr doc) {
7719 if (doc == NULL) return(-1);
7720 return(doc->compression);
7721}
7722
7723/**
7724 * xmlSetDocCompressMode:
7725 * @doc: the document
7726 * @mode: the compression ratio
7727 *
7728 * set the compression ratio for a document, ZLIB based
7729 * Correct values: 0 (uncompressed) to 9 (max compression)
7730 */
7731void
7732xmlSetDocCompressMode (xmlDocPtr doc, int mode) {
7733 if (doc == NULL) return;
7734 if (mode < 0) doc->compression = 0;
7735 else if (mode > 9) doc->compression = 9;
7736 else doc->compression = mode;
7737}
7738
7739/**
7740 * xmlGetCompressMode:
7741 *
7742 * get the default compression mode used, ZLIB based.
7743 * Returns 0 (uncompressed) to 9 (max compression)
7744 */
7745int
7746xmlGetCompressMode(void)
7747{
7748 return (xmlCompressMode);
7749}
7750
7751/**
7752 * xmlSetCompressMode:
7753 * @mode: the compression ratio
7754 *
7755 * set the default compression mode used, ZLIB based
7756 * Correct values: 0 (uncompressed) to 9 (max compression)
7757 */
7758void
7759xmlSetCompressMode(int mode) {
7760 if (mode < 0) xmlCompressMode = 0;
7761 else if (mode > 9) xmlCompressMode = 9;
7762 else xmlCompressMode = mode;
7763}
7764
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007765#define XML_TREE_NSMAP_PARENT -1
7766#define XML_TREE_NSMAP_XML -2
7767#define XML_TREE_NSMAP_DOC -3
7768#define XML_TREE_NSMAP_CUSTOM -4
7769
7770typedef struct xmlNsMapItem *xmlNsMapItemPtr;
7771struct xmlNsMapItem {
7772 xmlNsMapItemPtr next;
7773 xmlNsMapItemPtr prev;
7774 xmlNsPtr oldNs; /* old ns decl reference */
7775 xmlNsPtr newNs; /* new ns decl reference */
7776 int shadowDepth; /* Shadowed at this depth */
7777 /*
7778 * depth:
7779 * >= 0 == @node's ns-decls
7780 * -1 == @parent's ns-decls
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00007781 * -2 == the doc->oldNs XML ns-decl
7782 * -3 == the doc->oldNs storage ns-decls
7783 * -4 == ns-decls provided via custom ns-handling
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007784 */
7785 int depth;
7786};
7787
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007788typedef struct xmlNsMap *xmlNsMapPtr;
7789struct xmlNsMap {
7790 xmlNsMapItemPtr first;
7791 xmlNsMapItemPtr last;
7792 xmlNsMapItemPtr pool;
7793};
7794
7795#define XML_NSMAP_NOTEMPTY(m) (((m) != NULL) && ((m)->first != NULL))
7796#define XML_NSMAP_FOREACH(m, i) for (i = (m)->first; i != NULL; i = (i)->next)
7797#define XML_NSMAP_POP(m, i) \
7798 i = (m)->last; \
7799 (m)->last = (i)->prev; \
7800 if ((m)->last == NULL) \
7801 (m)->first = NULL; \
7802 else \
7803 (m)->last->next = NULL; \
7804 (i)->next = (m)->pool; \
7805 (m)->pool = i;
7806
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007807/*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007808* xmlDOMWrapNsMapFree:
7809* @map: the ns-map
Daniel Veillardaa6de472008-08-25 14:53:31 +00007810*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007811* Frees the ns-map
7812*/
7813static void
7814xmlDOMWrapNsMapFree(xmlNsMapPtr nsmap)
7815{
7816 xmlNsMapItemPtr cur, tmp;
7817
7818 if (nsmap == NULL)
7819 return;
7820 cur = nsmap->pool;
7821 while (cur != NULL) {
7822 tmp = cur;
7823 cur = cur->next;
7824 xmlFree(tmp);
7825 }
7826 cur = nsmap->first;
7827 while (cur != NULL) {
7828 tmp = cur;
7829 cur = cur->next;
7830 xmlFree(tmp);
7831 }
7832 xmlFree(nsmap);
7833}
7834
7835/*
7836* xmlDOMWrapNsMapAddItem:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007837* @map: the ns-map
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007838* @oldNs: the old ns-struct
7839* @newNs: the new ns-struct
7840* @depth: depth and ns-kind information
Daniel Veillardaa6de472008-08-25 14:53:31 +00007841*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007842* Adds an ns-mapping item.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007843*/
7844static xmlNsMapItemPtr
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00007845xmlDOMWrapNsMapAddItem(xmlNsMapPtr *nsmap, int position,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007846 xmlNsPtr oldNs, xmlNsPtr newNs, int depth)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007847{
7848 xmlNsMapItemPtr ret;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007849 xmlNsMapPtr map;
7850
7851 if (nsmap == NULL)
7852 return(NULL);
7853 if ((position != -1) && (position != 0))
7854 return(NULL);
7855 map = *nsmap;
7856
7857 if (map == NULL) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007858 /*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007859 * Create the ns-map.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007860 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007861 map = (xmlNsMapPtr) xmlMalloc(sizeof(struct xmlNsMap));
7862 if (map == NULL) {
7863 xmlTreeErrMemory("allocating namespace map");
7864 return (NULL);
7865 }
7866 memset(map, 0, sizeof(struct xmlNsMap));
7867 *nsmap = map;
7868 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00007869
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007870 if (map->pool != NULL) {
7871 /*
7872 * Reuse an item from the pool.
7873 */
7874 ret = map->pool;
7875 map->pool = ret->next;
7876 memset(ret, 0, sizeof(struct xmlNsMapItem));
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007877 } else {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007878 /*
7879 * Create a new item.
7880 */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007881 ret = (xmlNsMapItemPtr) xmlMalloc(sizeof(struct xmlNsMapItem));
7882 if (ret == NULL) {
7883 xmlTreeErrMemory("allocating namespace map item");
7884 return (NULL);
7885 }
7886 memset(ret, 0, sizeof(struct xmlNsMapItem));
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007887 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00007888
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007889 if (map->first == NULL) {
7890 /*
7891 * First ever.
Daniel Veillardaa6de472008-08-25 14:53:31 +00007892 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007893 map->first = ret;
7894 map->last = ret;
7895 } else if (position == -1) {
7896 /*
7897 * Append.
7898 */
7899 ret->prev = map->last;
7900 map->last->next = ret;
Daniel Veillardaa6de472008-08-25 14:53:31 +00007901 map->last = ret;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007902 } else if (position == 0) {
7903 /*
7904 * Set on first position.
7905 */
7906 map->first->prev = ret;
Daniel Veillardaa6de472008-08-25 14:53:31 +00007907 ret->next = map->first;
7908 map->first = ret;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007909 } else
7910 return(NULL);
7911
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007912 ret->oldNs = oldNs;
7913 ret->newNs = newNs;
7914 ret->shadowDepth = -1;
7915 ret->depth = depth;
7916 return (ret);
7917}
7918
7919/*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007920* xmlDOMWrapStoreNs:
7921* @doc: the doc
7922* @nsName: the namespace name
7923* @prefix: the prefix
Daniel Veillardaa6de472008-08-25 14:53:31 +00007924*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007925* Creates or reuses an xmlNs struct on doc->oldNs with
7926* the given prefix and namespace name.
Daniel Veillardaa6de472008-08-25 14:53:31 +00007927*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007928* Returns the aquired ns struct or NULL in case of an API
7929* or internal error.
7930*/
7931static xmlNsPtr
7932xmlDOMWrapStoreNs(xmlDocPtr doc,
7933 const xmlChar *nsName,
7934 const xmlChar *prefix)
7935{
7936 xmlNsPtr ns;
7937
7938 if (doc == NULL)
7939 return (NULL);
7940 ns = xmlTreeEnsureXMLDecl(doc);
7941 if (ns == NULL)
7942 return (NULL);
7943 if (ns->next != NULL) {
7944 /* Reuse. */
7945 ns = ns->next;
7946 while (ns != NULL) {
7947 if (((ns->prefix == prefix) ||
7948 xmlStrEqual(ns->prefix, prefix)) &&
7949 xmlStrEqual(ns->href, nsName)) {
7950 return (ns);
7951 }
7952 if (ns->next == NULL)
7953 break;
7954 ns = ns->next;
7955 }
7956 }
7957 /* Create. */
Daniel Veillard76d36452009-09-07 11:19:33 +02007958 if (ns != NULL) {
7959 ns->next = xmlNewNs(NULL, nsName, prefix);
7960 return (ns->next);
7961 }
7962 return(NULL);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007963}
7964
7965/*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00007966* xmlDOMWrapNewCtxt:
7967*
7968* Allocates and initializes a new DOM-wrapper context.
7969*
Jan Pokorný75801652013-12-19 15:09:14 +01007970* Returns the xmlDOMWrapCtxtPtr or NULL in case of an internal error.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00007971*/
7972xmlDOMWrapCtxtPtr
7973xmlDOMWrapNewCtxt(void)
7974{
7975 xmlDOMWrapCtxtPtr ret;
7976
7977 ret = xmlMalloc(sizeof(xmlDOMWrapCtxt));
7978 if (ret == NULL) {
7979 xmlTreeErrMemory("allocating DOM-wrapper context");
7980 return (NULL);
7981 }
7982 memset(ret, 0, sizeof(xmlDOMWrapCtxt));
7983 return (ret);
7984}
7985
7986/*
7987* xmlDOMWrapFreeCtxt:
7988* @ctxt: the DOM-wrapper context
7989*
7990* Frees the DOM-wrapper context.
7991*/
7992void
7993xmlDOMWrapFreeCtxt(xmlDOMWrapCtxtPtr ctxt)
7994{
7995 if (ctxt == NULL)
7996 return;
7997 if (ctxt->namespaceMap != NULL)
7998 xmlDOMWrapNsMapFree((xmlNsMapPtr) ctxt->namespaceMap);
7999 /*
8000 * TODO: Store the namespace map in the context.
8001 */
8002 xmlFree(ctxt);
8003}
8004
8005/*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008006* xmlTreeLookupNsListByPrefix:
8007* @nsList: a list of ns-structs
8008* @prefix: the searched prefix
Daniel Veillardaa6de472008-08-25 14:53:31 +00008009*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008010* Searches for a ns-decl with the given prefix in @nsList.
Daniel Veillardaa6de472008-08-25 14:53:31 +00008011*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008012* Returns the ns-decl if found, NULL if not found and on
8013* API errors.
8014*/
8015static xmlNsPtr
8016xmlTreeNSListLookupByPrefix(xmlNsPtr nsList, const xmlChar *prefix)
8017{
8018 if (nsList == NULL)
8019 return (NULL);
8020 {
8021 xmlNsPtr ns;
8022 ns = nsList;
8023 do {
8024 if ((prefix == ns->prefix) ||
8025 xmlStrEqual(prefix, ns->prefix)) {
8026 return (ns);
8027 }
8028 ns = ns->next;
8029 } while (ns != NULL);
8030 }
8031 return (NULL);
8032}
8033
8034/*
8035*
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008036* xmlDOMWrapNSNormGatherInScopeNs:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008037* @map: the namespace map
8038* @node: the node to start with
Daniel Veillardaa6de472008-08-25 14:53:31 +00008039*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008040* Puts in-scope namespaces into the ns-map.
Daniel Veillardaa6de472008-08-25 14:53:31 +00008041*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008042* Returns 0 on success, -1 on API or internal errors.
8043*/
8044static int
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008045xmlDOMWrapNSNormGatherInScopeNs(xmlNsMapPtr *map,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008046 xmlNodePtr node)
8047{
8048 xmlNodePtr cur;
8049 xmlNsPtr ns;
8050 xmlNsMapItemPtr mi;
8051 int shadowed;
8052
8053 if ((map == NULL) || (*map != NULL))
8054 return (-1);
Daniel Veillard3e62adb2012-08-09 14:24:02 +08008055 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
8056 return (-1);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008057 /*
8058 * Get in-scope ns-decls of @parent.
8059 */
8060 cur = node;
8061 while ((cur != NULL) && (cur != (xmlNodePtr) cur->doc)) {
8062 if (cur->type == XML_ELEMENT_NODE) {
8063 if (cur->nsDef != NULL) {
8064 ns = cur->nsDef;
8065 do {
8066 shadowed = 0;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008067 if (XML_NSMAP_NOTEMPTY(*map)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008068 /*
8069 * Skip shadowed prefixes.
8070 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008071 XML_NSMAP_FOREACH(*map, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008072 if ((ns->prefix == mi->newNs->prefix) ||
8073 xmlStrEqual(ns->prefix, mi->newNs->prefix)) {
8074 shadowed = 1;
8075 break;
8076 }
8077 }
8078 }
8079 /*
8080 * Insert mapping.
8081 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008082 mi = xmlDOMWrapNsMapAddItem(map, 0, NULL,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008083 ns, XML_TREE_NSMAP_PARENT);
8084 if (mi == NULL)
8085 return (-1);
8086 if (shadowed)
8087 mi->shadowDepth = 0;
8088 ns = ns->next;
8089 } while (ns != NULL);
8090 }
8091 }
8092 cur = cur->parent;
8093 }
8094 return (0);
8095}
8096
8097/*
8098* XML_TREE_ADOPT_STR: If we have a dest-dict, put @str in the dict;
8099* otherwise copy it, when it was in the source-dict.
8100*/
8101#define XML_TREE_ADOPT_STR(str) \
8102 if (adoptStr && (str != NULL)) { \
8103 if (destDoc->dict) { \
Daniel Veillard7e21fd12005-07-03 21:44:07 +00008104 const xmlChar *old = str; \
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008105 str = xmlDictLookup(destDoc->dict, str, -1); \
Daniel Veillard7e21fd12005-07-03 21:44:07 +00008106 if ((sourceDoc == NULL) || (sourceDoc->dict == NULL) || \
8107 (!xmlDictOwns(sourceDoc->dict, old))) \
Daniel Veillard39e5c892005-07-03 22:48:50 +00008108 xmlFree((char *)old); \
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008109 } else if ((sourceDoc) && (sourceDoc->dict) && \
8110 xmlDictOwns(sourceDoc->dict, str)) { \
8111 str = BAD_CAST xmlStrdup(str); \
8112 } \
8113 }
8114
8115/*
8116* XML_TREE_ADOPT_STR_2: If @str was in the source-dict, then
8117* put it in dest-dict or copy it.
8118*/
8119#define XML_TREE_ADOPT_STR_2(str) \
8120 if (adoptStr && (str != NULL) && (sourceDoc != NULL) && \
8121 (sourceDoc->dict != NULL) && \
8122 xmlDictOwns(sourceDoc->dict, cur->content)) { \
8123 if (destDoc->dict) \
8124 cur->content = (xmlChar *) \
8125 xmlDictLookup(destDoc->dict, cur->content, -1); \
8126 else \
8127 cur->content = xmlStrdup(BAD_CAST cur->content); \
8128 }
8129
8130/*
8131* xmlDOMWrapNSNormAddNsMapItem2:
8132*
8133* For internal use. Adds a ns-decl mapping.
8134*
Daniel Veillardaa6de472008-08-25 14:53:31 +00008135* Returns 0 on success, -1 on internal errors.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008136*/
8137static int
8138xmlDOMWrapNSNormAddNsMapItem2(xmlNsPtr **list, int *size, int *number,
8139 xmlNsPtr oldNs, xmlNsPtr newNs)
8140{
8141 if (*list == NULL) {
8142 *list = (xmlNsPtr *) xmlMalloc(6 * sizeof(xmlNsPtr));
8143 if (*list == NULL) {
8144 xmlTreeErrMemory("alloc ns map item");
8145 return(-1);
8146 }
8147 *size = 3;
8148 *number = 0;
8149 } else if ((*number) >= (*size)) {
8150 *size *= 2;
8151 *list = (xmlNsPtr *) xmlRealloc(*list,
8152 (*size) * 2 * sizeof(xmlNsPtr));
8153 if (*list == NULL) {
8154 xmlTreeErrMemory("realloc ns map item");
8155 return(-1);
8156 }
8157 }
8158 (*list)[2 * (*number)] = oldNs;
8159 (*list)[2 * (*number) +1] = newNs;
8160 (*number)++;
8161 return (0);
8162}
8163
8164/*
8165* xmlDOMWrapRemoveNode:
Daniel Veillard304e78c2005-07-03 16:19:41 +00008166* @ctxt: a DOM wrapper context
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008167* @doc: the doc
8168* @node: the node to be removed.
Daniel Veillard304e78c2005-07-03 16:19:41 +00008169* @options: set of options, unused at the moment
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008170*
8171* Unlinks the given node from its owner.
8172* This will substitute ns-references to node->nsDef for
8173* ns-references to doc->oldNs, thus ensuring the removed
8174* branch to be autark wrt ns-references.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008175*
8176* NOTE: This function was not intensively tested.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008177*
8178* Returns 0 on success, 1 if the node is not supported,
Daniel Veillardaa6de472008-08-25 14:53:31 +00008179* -1 on API and internal errors.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008180*/
8181int
8182xmlDOMWrapRemoveNode(xmlDOMWrapCtxtPtr ctxt, xmlDocPtr doc,
8183 xmlNodePtr node, int options ATTRIBUTE_UNUSED)
8184{
8185 xmlNsPtr *list = NULL;
8186 int sizeList, nbList, i, j;
8187 xmlNsPtr ns;
8188
8189 if ((node == NULL) || (doc == NULL) || (node->doc != doc))
8190 return (-1);
8191
8192 /* TODO: 0 or -1 ? */
8193 if (node->parent == NULL)
8194 return (0);
8195
Daniel Veillardaa6de472008-08-25 14:53:31 +00008196 switch (node->type) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008197 case XML_TEXT_NODE:
8198 case XML_CDATA_SECTION_NODE:
8199 case XML_ENTITY_REF_NODE:
8200 case XML_PI_NODE:
8201 case XML_COMMENT_NODE:
8202 xmlUnlinkNode(node);
8203 return (0);
Daniel Veillardaa6de472008-08-25 14:53:31 +00008204 case XML_ELEMENT_NODE:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008205 case XML_ATTRIBUTE_NODE:
8206 break;
8207 default:
8208 return (1);
8209 }
8210 xmlUnlinkNode(node);
8211 /*
8212 * Save out-of-scope ns-references in doc->oldNs.
8213 */
8214 do {
8215 switch (node->type) {
8216 case XML_ELEMENT_NODE:
8217 if ((ctxt == NULL) && (node->nsDef != NULL)) {
8218 ns = node->nsDef;
8219 do {
8220 if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
8221 &nbList, ns, ns) == -1)
8222 goto internal_error;
8223 ns = ns->next;
8224 } while (ns != NULL);
8225 }
8226 /* No break on purpose. */
8227 case XML_ATTRIBUTE_NODE:
Daniel Veillardaa6de472008-08-25 14:53:31 +00008228 if (node->ns != NULL) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008229 /*
8230 * Find a mapping.
8231 */
8232 if (list != NULL) {
8233 for (i = 0, j = 0; i < nbList; i++, j += 2) {
8234 if (node->ns == list[j]) {
8235 node->ns = list[++j];
8236 goto next_node;
8237 }
8238 }
8239 }
8240 ns = NULL;
8241 if (ctxt != NULL) {
8242 /*
8243 * User defined.
8244 */
8245 } else {
8246 /*
8247 * Add to doc's oldNs.
8248 */
8249 ns = xmlDOMWrapStoreNs(doc, node->ns->href,
8250 node->ns->prefix);
8251 if (ns == NULL)
8252 goto internal_error;
8253 }
8254 if (ns != NULL) {
8255 /*
8256 * Add mapping.
8257 */
8258 if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
8259 &nbList, node->ns, ns) == -1)
8260 goto internal_error;
8261 }
8262 node->ns = ns;
8263 }
8264 if ((node->type == XML_ELEMENT_NODE) &&
8265 (node->properties != NULL)) {
8266 node = (xmlNodePtr) node->properties;
8267 continue;
8268 }
8269 break;
8270 default:
8271 goto next_sibling;
Daniel Veillardaa6de472008-08-25 14:53:31 +00008272 }
8273next_node:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008274 if ((node->type == XML_ELEMENT_NODE) &&
8275 (node->children != NULL)) {
8276 node = node->children;
8277 continue;
8278 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00008279next_sibling:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008280 if (node == NULL)
8281 break;
8282 if (node->next != NULL)
8283 node = node->next;
8284 else {
8285 node = node->parent;
8286 goto next_sibling;
8287 }
8288 } while (node != NULL);
8289
8290 if (list != NULL)
8291 xmlFree(list);
8292 return (0);
8293
8294internal_error:
8295 if (list != NULL)
8296 xmlFree(list);
8297 return (-1);
8298}
8299
8300/*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008301* xmlSearchNsByNamespaceStrict:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008302* @doc: the document
8303* @node: the start node
8304* @nsName: the searched namespace name
8305* @retNs: the resulting ns-decl
8306* @prefixed: if the found ns-decl must have a prefix (for attributes)
8307*
8308* Dynamically searches for a ns-declaration which matches
8309* the given @nsName in the ancestor-or-self axis of @node.
8310*
8311* Returns 1 if a ns-decl was found, 0 if not and -1 on API
8312* and internal errors.
8313*/
8314static int
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008315xmlSearchNsByNamespaceStrict(xmlDocPtr doc, xmlNodePtr node,
8316 const xmlChar* nsName,
8317 xmlNsPtr *retNs, int prefixed)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008318{
8319 xmlNodePtr cur, prev = NULL, out = NULL;
8320 xmlNsPtr ns, prevns;
8321
8322 if ((doc == NULL) || (nsName == NULL) || (retNs == NULL))
8323 return (-1);
Daniel Veillard3e62adb2012-08-09 14:24:02 +08008324 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
8325 return(-1);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008326
8327 *retNs = NULL;
8328 if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) {
8329 *retNs = xmlTreeEnsureXMLDecl(doc);
8330 if (*retNs == NULL)
8331 return (-1);
8332 return (1);
8333 }
8334 cur = node;
8335 do {
8336 if (cur->type == XML_ELEMENT_NODE) {
8337 if (cur->nsDef != NULL) {
8338 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
8339 if (prefixed && (ns->prefix == NULL))
8340 continue;
8341 if (prev != NULL) {
8342 /*
8343 * Check the last level of ns-decls for a
8344 * shadowing prefix.
8345 */
8346 prevns = prev->nsDef;
8347 do {
8348 if ((prevns->prefix == ns->prefix) ||
8349 ((prevns->prefix != NULL) &&
8350 (ns->prefix != NULL) &&
8351 xmlStrEqual(prevns->prefix, ns->prefix))) {
8352 /*
8353 * Shadowed.
8354 */
8355 break;
8356 }
8357 prevns = prevns->next;
8358 } while (prevns != NULL);
8359 if (prevns != NULL)
8360 continue;
8361 }
8362 /*
8363 * Ns-name comparison.
8364 */
8365 if ((nsName == ns->href) ||
8366 xmlStrEqual(nsName, ns->href)) {
8367 /*
8368 * At this point the prefix can only be shadowed,
8369 * if we are the the (at least) 3rd level of
8370 * ns-decls.
8371 */
8372 if (out) {
8373 int ret;
Daniel Veillardaa6de472008-08-25 14:53:31 +00008374
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008375 ret = xmlNsInScope(doc, node, prev, ns->prefix);
8376 if (ret < 0)
8377 return (-1);
8378 /*
8379 * TODO: Should we try to find a matching ns-name
8380 * only once? This here keeps on searching.
8381 * I think we should try further since, there might
8382 * be an other matching ns-decl with an unshadowed
8383 * prefix.
8384 */
8385 if (! ret)
8386 continue;
8387 }
8388 *retNs = ns;
8389 return (1);
8390 }
8391 }
8392 out = prev;
8393 prev = cur;
8394 }
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008395 } else if ((cur->type == XML_ENTITY_NODE) ||
8396 (cur->type == XML_ENTITY_DECL))
8397 return (0);
8398 cur = cur->parent;
8399 } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
8400 return (0);
8401}
8402
8403/*
8404* xmlSearchNsByPrefixStrict:
8405* @doc: the document
8406* @node: the start node
8407* @prefix: the searched namespace prefix
8408* @retNs: the resulting ns-decl
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008409*
8410* Dynamically searches for a ns-declaration which matches
8411* the given @nsName in the ancestor-or-self axis of @node.
8412*
8413* Returns 1 if a ns-decl was found, 0 if not and -1 on API
8414* and internal errors.
8415*/
8416static int
8417xmlSearchNsByPrefixStrict(xmlDocPtr doc, xmlNodePtr node,
8418 const xmlChar* prefix,
8419 xmlNsPtr *retNs)
8420{
8421 xmlNodePtr cur;
8422 xmlNsPtr ns;
8423
Daniel Veillard3e62adb2012-08-09 14:24:02 +08008424 if ((doc == NULL) || (node == NULL) || (node->type == XML_NAMESPACE_DECL))
8425 return(-1);
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008426
8427 if (retNs)
8428 *retNs = NULL;
8429 if (IS_STR_XML(prefix)) {
8430 if (retNs) {
8431 *retNs = xmlTreeEnsureXMLDecl(doc);
8432 if (*retNs == NULL)
8433 return (-1);
8434 }
8435 return (1);
8436 }
8437 cur = node;
8438 do {
8439 if (cur->type == XML_ELEMENT_NODE) {
8440 if (cur->nsDef != NULL) {
8441 ns = cur->nsDef;
Daniel Veillardaa6de472008-08-25 14:53:31 +00008442 do {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008443 if ((prefix == ns->prefix) ||
8444 xmlStrEqual(prefix, ns->prefix))
8445 {
8446 /*
8447 * Disabled namespaces, e.g. xmlns:abc="".
8448 */
8449 if (ns->href == NULL)
8450 return(0);
8451 if (retNs)
8452 *retNs = ns;
8453 return (1);
8454 }
8455 ns = ns->next;
Daniel Veillardaa6de472008-08-25 14:53:31 +00008456 } while (ns != NULL);
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008457 }
8458 } else if ((cur->type == XML_ENTITY_NODE) ||
8459 (cur->type == XML_ENTITY_DECL))
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008460 return (0);
8461 cur = cur->parent;
8462 } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
8463 return (0);
8464}
8465
8466/*
8467* xmlDOMWrapNSNormDeclareNsForced:
8468* @doc: the doc
8469* @elem: the element-node to declare on
8470* @nsName: the namespace-name of the ns-decl
8471* @prefix: the preferred prefix of the ns-decl
8472* @checkShadow: ensure that the new ns-decl doesn't shadow ancestor ns-decls
8473*
8474* Declares a new namespace on @elem. It tries to use the
8475* given @prefix; if a ns-decl with the given prefix is already existent
8476* on @elem, it will generate an other prefix.
8477*
8478* Returns 1 if a ns-decl was found, 0 if not and -1 on API
8479* and internal errors.
8480*/
8481static xmlNsPtr
8482xmlDOMWrapNSNormDeclareNsForced(xmlDocPtr doc,
8483 xmlNodePtr elem,
8484 const xmlChar *nsName,
8485 const xmlChar *prefix,
8486 int checkShadow)
8487{
8488
8489 xmlNsPtr ret;
8490 char buf[50];
8491 const xmlChar *pref;
8492 int counter = 0;
Daniel Veillard3e62adb2012-08-09 14:24:02 +08008493
8494 if ((doc == NULL) || (elem == NULL) || (elem->type != XML_ELEMENT_NODE))
8495 return(NULL);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008496 /*
8497 * Create a ns-decl on @anchor.
8498 */
8499 pref = prefix;
8500 while (1) {
8501 /*
8502 * Lookup whether the prefix is unused in elem's ns-decls.
8503 */
8504 if ((elem->nsDef != NULL) &&
8505 (xmlTreeNSListLookupByPrefix(elem->nsDef, pref) != NULL))
8506 goto ns_next_prefix;
8507 if (checkShadow && elem->parent &&
8508 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8509 /*
8510 * Does it shadow ancestor ns-decls?
8511 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008512 if (xmlSearchNsByPrefixStrict(doc, elem->parent, pref, NULL) == 1)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008513 goto ns_next_prefix;
8514 }
8515 ret = xmlNewNs(NULL, nsName, pref);
8516 if (ret == NULL)
8517 return (NULL);
8518 if (elem->nsDef == NULL)
8519 elem->nsDef = ret;
8520 else {
8521 xmlNsPtr ns2 = elem->nsDef;
8522 while (ns2->next != NULL)
8523 ns2 = ns2->next;
8524 ns2->next = ret;
8525 }
8526 return (ret);
8527ns_next_prefix:
8528 counter++;
8529 if (counter > 1000)
8530 return (NULL);
8531 if (prefix == NULL) {
8532 snprintf((char *) buf, sizeof(buf),
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008533 "ns_%d", counter);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008534 } else
8535 snprintf((char *) buf, sizeof(buf),
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008536 "%.30s_%d", (char *)prefix, counter);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008537 pref = BAD_CAST buf;
8538 }
8539}
8540
8541/*
8542* xmlDOMWrapNSNormAquireNormalizedNs:
8543* @doc: the doc
8544* @elem: the element-node to declare namespaces on
8545* @ns: the ns-struct to use for the search
8546* @retNs: the found/created ns-struct
8547* @nsMap: the ns-map
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008548* @depth: the current tree depth
8549* @ancestorsOnly: search in ancestor ns-decls only
8550* @prefixed: if the searched ns-decl must have a prefix (for attributes)
8551*
8552* Searches for a matching ns-name in the ns-decls of @nsMap, if not
8553* found it will either declare it on @elem, or store it in doc->oldNs.
8554* If a new ns-decl needs to be declared on @elem, it tries to use the
8555* @ns->prefix for it, if this prefix is already in use on @elem, it will
8556* change the prefix or the new ns-decl.
8557*
8558* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8559*/
8560static int
8561xmlDOMWrapNSNormAquireNormalizedNs(xmlDocPtr doc,
8562 xmlNodePtr elem,
8563 xmlNsPtr ns,
8564 xmlNsPtr *retNs,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008565 xmlNsMapPtr *nsMap,
Daniel Veillardaa6de472008-08-25 14:53:31 +00008566
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008567 int depth,
8568 int ancestorsOnly,
8569 int prefixed)
8570{
Daniel Veillardaa6de472008-08-25 14:53:31 +00008571 xmlNsMapItemPtr mi;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008572
8573 if ((doc == NULL) || (ns == NULL) || (retNs == NULL) ||
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008574 (nsMap == NULL))
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008575 return (-1);
8576
8577 *retNs = NULL;
8578 /*
8579 * Handle XML namespace.
8580 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008581 if (IS_STR_XML(ns->prefix)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008582 /*
8583 * Insert XML namespace mapping.
8584 */
8585 *retNs = xmlTreeEnsureXMLDecl(doc);
8586 if (*retNs == NULL)
8587 return (-1);
8588 return (0);
8589 }
8590 /*
8591 * If the search should be done in ancestors only and no
8592 * @elem (the first ancestor) was specified, then skip the search.
8593 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008594 if ((XML_NSMAP_NOTEMPTY(*nsMap)) &&
8595 (! (ancestorsOnly && (elem == NULL))))
Daniel Veillardaa6de472008-08-25 14:53:31 +00008596 {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008597 /*
8598 * Try to find an equal ns-name in in-scope ns-decls.
8599 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008600 XML_NSMAP_FOREACH(*nsMap, mi) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00008601 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8602 /*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008603 * ancestorsOnly: This should be turned on to gain speed,
8604 * if one knows that the branch itself was already
8605 * ns-wellformed and no stale references existed.
8606 * I.e. it searches in the ancestor axis only.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008607 */
8608 ((! ancestorsOnly) || (mi->depth == XML_TREE_NSMAP_PARENT)) &&
8609 /* Skip shadowed prefixes. */
Daniel Veillardaa6de472008-08-25 14:53:31 +00008610 (mi->shadowDepth == -1) &&
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008611 /* Skip xmlns="" or xmlns:foo="". */
8612 ((mi->newNs->href != NULL) &&
Daniel Veillardaa6de472008-08-25 14:53:31 +00008613 (mi->newNs->href[0] != 0)) &&
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008614 /* Ensure a prefix if wanted. */
8615 ((! prefixed) || (mi->newNs->prefix != NULL)) &&
8616 /* Equal ns name */
8617 ((mi->newNs->href == ns->href) ||
8618 xmlStrEqual(mi->newNs->href, ns->href))) {
8619 /* Set the mapping. */
8620 mi->oldNs = ns;
8621 *retNs = mi->newNs;
8622 return (0);
8623 }
8624 }
8625 }
8626 /*
8627 * No luck, the namespace is out of scope or shadowed.
8628 */
8629 if (elem == NULL) {
8630 xmlNsPtr tmpns;
8631
8632 /*
8633 * Store ns-decls in "oldNs" of the document-node.
8634 */
8635 tmpns = xmlDOMWrapStoreNs(doc, ns->href, ns->prefix);
8636 if (tmpns == NULL)
8637 return (-1);
8638 /*
8639 * Insert mapping.
Daniel Veillardaa6de472008-08-25 14:53:31 +00008640 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008641 if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008642 tmpns, XML_TREE_NSMAP_DOC) == NULL) {
8643 xmlFreeNs(tmpns);
8644 return (-1);
8645 }
8646 *retNs = tmpns;
8647 } else {
8648 xmlNsPtr tmpns;
8649
8650 tmpns = xmlDOMWrapNSNormDeclareNsForced(doc, elem, ns->href,
8651 ns->prefix, 0);
8652 if (tmpns == NULL)
8653 return (-1);
8654
8655 if (*nsMap != NULL) {
8656 /*
8657 * Does it shadow ancestor ns-decls?
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008658 */
8659 XML_NSMAP_FOREACH(*nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008660 if ((mi->depth < depth) &&
8661 (mi->shadowDepth == -1) &&
8662 ((ns->prefix == mi->newNs->prefix) ||
8663 xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
8664 /*
8665 * Shadows.
8666 */
8667 mi->shadowDepth = depth;
8668 break;
8669 }
8670 }
8671 }
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008672 if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns, tmpns, depth) == NULL) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008673 xmlFreeNs(tmpns);
8674 return (-1);
8675 }
8676 *retNs = tmpns;
8677 }
8678 return (0);
8679}
8680
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008681typedef enum {
Daniel Veillardaa6de472008-08-25 14:53:31 +00008682 XML_DOM_RECONNS_REMOVEREDUND = 1<<0
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008683} xmlDOMReconcileNSOptions;
8684
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008685/*
8686* xmlDOMWrapReconcileNamespaces:
Daniel Veillard304e78c2005-07-03 16:19:41 +00008687* @ctxt: DOM wrapper context, unused at the moment
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008688* @elem: the element-node
8689* @options: option flags
8690*
8691* Ensures that ns-references point to ns-decls hold on element-nodes.
8692* Ensures that the tree is namespace wellformed by creating additional
8693* ns-decls where needed. Note that, since prefixes of already existent
8694* ns-decls can be shadowed by this process, it could break QNames in
8695* attribute values or element content.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008696*
8697* NOTE: This function was not intensively tested.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008698*
8699* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
Daniel Veillardaa6de472008-08-25 14:53:31 +00008700*/
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008701
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008702int
8703xmlDOMWrapReconcileNamespaces(xmlDOMWrapCtxtPtr ctxt ATTRIBUTE_UNUSED,
8704 xmlNodePtr elem,
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008705 int options)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008706{
8707 int depth = -1, adoptns = 0, parnsdone = 0;
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008708 xmlNsPtr ns, prevns;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008709 xmlDocPtr doc;
8710 xmlNodePtr cur, curElem = NULL;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008711 xmlNsMapPtr nsMap = NULL;
8712 xmlNsMapItemPtr /* topmi = NULL, */ mi;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008713 /* @ancestorsOnly should be set by an option flag. */
8714 int ancestorsOnly = 0;
Daniel Veillardaa6de472008-08-25 14:53:31 +00008715 int optRemoveRedundantNS =
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008716 ((xmlDOMReconcileNSOptions) options & XML_DOM_RECONNS_REMOVEREDUND) ? 1 : 0;
8717 xmlNsPtr *listRedund = NULL;
8718 int sizeRedund = 0, nbRedund = 0, ret, i, j;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008719
8720 if ((elem == NULL) || (elem->doc == NULL) ||
8721 (elem->type != XML_ELEMENT_NODE))
8722 return (-1);
8723
8724 doc = elem->doc;
8725 cur = elem;
8726 do {
8727 switch (cur->type) {
8728 case XML_ELEMENT_NODE:
8729 adoptns = 1;
8730 curElem = cur;
8731 depth++;
8732 /*
8733 * Namespace declarations.
8734 */
8735 if (cur->nsDef != NULL) {
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008736 prevns = NULL;
Kasimier T. Buchcike8f8d752006-02-02 12:13:07 +00008737 ns = cur->nsDef;
8738 while (ns != NULL) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008739 if (! parnsdone) {
8740 if ((elem->parent) &&
8741 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8742 /*
8743 * Gather ancestor in-scope ns-decls.
8744 */
8745 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8746 elem->parent) == -1)
8747 goto internal_error;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008748 }
8749 parnsdone = 1;
8750 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00008751
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008752 /*
8753 * Lookup the ns ancestor-axis for equal ns-decls in scope.
8754 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008755 if (optRemoveRedundantNS && XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008756 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008757 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8758 (mi->shadowDepth == -1) &&
8759 ((ns->prefix == mi->newNs->prefix) ||
8760 xmlStrEqual(ns->prefix, mi->newNs->prefix)) &&
8761 ((ns->href == mi->newNs->href) ||
8762 xmlStrEqual(ns->href, mi->newNs->href)))
Daniel Veillardaa6de472008-08-25 14:53:31 +00008763 {
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008764 /*
8765 * A redundant ns-decl was found.
8766 * Add it to the list of redundant ns-decls.
8767 */
8768 if (xmlDOMWrapNSNormAddNsMapItem2(&listRedund,
8769 &sizeRedund, &nbRedund, ns, mi->newNs) == -1)
8770 goto internal_error;
8771 /*
8772 * Remove the ns-decl from the element-node.
Daniel Veillardaa6de472008-08-25 14:53:31 +00008773 */
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008774 if (prevns)
8775 prevns->next = ns->next;
8776 else
Daniel Veillardaa6de472008-08-25 14:53:31 +00008777 cur->nsDef = ns->next;
Kasimier T. Buchcike8f8d752006-02-02 12:13:07 +00008778 goto next_ns_decl;
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008779 }
8780 }
8781 }
8782
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008783 /*
8784 * Skip ns-references handling if the referenced
8785 * ns-decl is declared on the same element.
8786 */
8787 if ((cur->ns != NULL) && adoptns && (cur->ns == ns))
Daniel Veillardaa6de472008-08-25 14:53:31 +00008788 adoptns = 0;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008789 /*
8790 * Does it shadow any ns-decl?
8791 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008792 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8793 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008794 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8795 (mi->shadowDepth == -1) &&
8796 ((ns->prefix == mi->newNs->prefix) ||
8797 xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00008798
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008799 mi->shadowDepth = depth;
8800 }
8801 }
8802 }
8803 /*
8804 * Push mapping.
8805 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008806 if (xmlDOMWrapNsMapAddItem(&nsMap, -1, ns, ns,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008807 depth) == NULL)
Daniel Veillardaa6de472008-08-25 14:53:31 +00008808 goto internal_error;
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008809
8810 prevns = ns;
Kasimier T. Buchcike8f8d752006-02-02 12:13:07 +00008811next_ns_decl:
Daniel Veillardaa6de472008-08-25 14:53:31 +00008812 ns = ns->next;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008813 }
8814 }
8815 if (! adoptns)
8816 goto ns_end;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008817 /* No break on purpose. */
8818 case XML_ATTRIBUTE_NODE:
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008819 /* No ns, no fun. */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008820 if (cur->ns == NULL)
8821 goto ns_end;
Daniel Veillardaa6de472008-08-25 14:53:31 +00008822
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008823 if (! parnsdone) {
8824 if ((elem->parent) &&
8825 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8826 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8827 elem->parent) == -1)
8828 goto internal_error;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008829 }
8830 parnsdone = 1;
8831 }
8832 /*
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008833 * Adjust the reference if this was a redundant ns-decl.
8834 */
8835 if (listRedund) {
8836 for (i = 0, j = 0; i < nbRedund; i++, j += 2) {
8837 if (cur->ns == listRedund[j]) {
8838 cur->ns = listRedund[++j];
8839 break;
8840 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00008841 }
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008842 }
8843 /*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008844 * Adopt ns-references.
8845 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008846 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008847 /*
8848 * Search for a mapping.
8849 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008850 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008851 if ((mi->shadowDepth == -1) &&
8852 (cur->ns == mi->oldNs)) {
8853
8854 cur->ns = mi->newNs;
8855 goto ns_end;
8856 }
8857 }
8858 }
8859 /*
8860 * Aquire a normalized ns-decl and add it to the map.
8861 */
8862 if (xmlDOMWrapNSNormAquireNormalizedNs(doc, curElem,
8863 cur->ns, &ns,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008864 &nsMap, depth,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008865 ancestorsOnly,
8866 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
8867 goto internal_error;
8868 cur->ns = ns;
8869
8870ns_end:
8871 if ((cur->type == XML_ELEMENT_NODE) &&
8872 (cur->properties != NULL)) {
8873 /*
8874 * Process attributes.
8875 */
8876 cur = (xmlNodePtr) cur->properties;
8877 continue;
8878 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00008879 break;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008880 default:
8881 goto next_sibling;
8882 }
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008883into_content:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008884 if ((cur->type == XML_ELEMENT_NODE) &&
8885 (cur->children != NULL)) {
8886 /*
8887 * Process content of element-nodes only.
8888 */
8889 cur = cur->children;
8890 continue;
8891 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00008892next_sibling:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008893 if (cur == elem)
8894 break;
8895 if (cur->type == XML_ELEMENT_NODE) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00008896 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008897 /*
8898 * Pop mappings.
8899 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008900 while ((nsMap->last != NULL) &&
8901 (nsMap->last->depth >= depth))
8902 {
Daniel Veillardaa6de472008-08-25 14:53:31 +00008903 XML_NSMAP_POP(nsMap, mi)
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008904 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008905 /*
8906 * Unshadow.
8907 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008908 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008909 if (mi->shadowDepth >= depth)
8910 mi->shadowDepth = -1;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008911 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00008912 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008913 depth--;
8914 }
8915 if (cur->next != NULL)
8916 cur = cur->next;
8917 else {
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008918 if (cur->type == XML_ATTRIBUTE_NODE) {
8919 cur = cur->parent;
8920 goto into_content;
8921 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008922 cur = cur->parent;
8923 goto next_sibling;
8924 }
8925 } while (cur != NULL);
Daniel Veillardaa6de472008-08-25 14:53:31 +00008926
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008927 ret = 0;
8928 goto exit;
8929internal_error:
8930 ret = -1;
8931exit:
Daniel Veillardaa6de472008-08-25 14:53:31 +00008932 if (listRedund) {
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008933 for (i = 0, j = 0; i < nbRedund; i++, j += 2) {
8934 xmlFreeNs(listRedund[j]);
8935 }
8936 xmlFree(listRedund);
8937 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008938 if (nsMap != NULL)
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008939 xmlDOMWrapNsMapFree(nsMap);
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008940 return (ret);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008941}
8942
8943/*
8944* xmlDOMWrapAdoptBranch:
8945* @ctxt: the optional context for custom processing
8946* @sourceDoc: the optional sourceDoc
8947* @node: the element-node to start with
8948* @destDoc: the destination doc for adoption
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008949* @destParent: the optional new parent of @node in @destDoc
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008950* @options: option flags
8951*
8952* Ensures that ns-references point to @destDoc: either to
8953* elements->nsDef entries if @destParent is given, or to
8954* @destDoc->oldNs otherwise.
8955* If @destParent is given, it ensures that the tree is namespace
8956* wellformed by creating additional ns-decls where needed.
8957* Note that, since prefixes of already existent ns-decls can be
8958* shadowed by this process, it could break QNames in attribute
8959* values or element content.
8960*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008961* NOTE: This function was not intensively tested.
8962*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008963* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8964*/
8965static int
8966xmlDOMWrapAdoptBranch(xmlDOMWrapCtxtPtr ctxt,
8967 xmlDocPtr sourceDoc,
8968 xmlNodePtr node,
8969 xmlDocPtr destDoc,
8970 xmlNodePtr destParent,
8971 int options ATTRIBUTE_UNUSED)
8972{
8973 int ret = 0;
8974 xmlNodePtr cur, curElem = NULL;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008975 xmlNsMapPtr nsMap = NULL;
8976 xmlNsMapItemPtr mi;
Daniel Veillard11ce4002006-03-10 00:36:23 +00008977 xmlNsPtr ns = NULL;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008978 int depth = -1, adoptStr = 1;
8979 /* gather @parent's ns-decls. */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008980 int parnsdone;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008981 /* @ancestorsOnly should be set per option. */
8982 int ancestorsOnly = 0;
Daniel Veillardaa6de472008-08-25 14:53:31 +00008983
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008984 /*
8985 * Optimize string adoption for equal or none dicts.
8986 */
8987 if ((sourceDoc != NULL) &&
8988 (sourceDoc->dict == destDoc->dict))
8989 adoptStr = 0;
8990 else
8991 adoptStr = 1;
8992
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008993 /*
8994 * Get the ns-map from the context if available.
8995 */
8996 if (ctxt)
8997 nsMap = (xmlNsMapPtr) ctxt->namespaceMap;
8998 /*
8999 * Disable search for ns-decls in the parent-axis of the
9000 * desination element, if:
9001 * 1) there's no destination parent
9002 * 2) custom ns-reference handling is used
9003 */
9004 if ((destParent == NULL) ||
9005 (ctxt && ctxt->getNsForNodeFunc))
9006 {
9007 parnsdone = 1;
9008 } else
9009 parnsdone = 0;
9010
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009011 cur = node;
Daniel Veillard3e62adb2012-08-09 14:24:02 +08009012 if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
9013 goto internal_error;
9014
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009015 while (cur != NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009016 /*
9017 * Paranoid source-doc sanity check.
9018 */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009019 if (cur->doc != sourceDoc) {
9020 /*
9021 * We'll assume XIncluded nodes if the doc differs.
9022 * TODO: Do we need to reconciliate XIncluded nodes?
9023 * This here skips XIncluded nodes and tries to handle
9024 * broken sequences.
9025 */
9026 if (cur->next == NULL)
9027 goto leave_node;
9028 do {
9029 cur = cur->next;
9030 if ((cur->type == XML_XINCLUDE_END) ||
9031 (cur->doc == node->doc))
9032 break;
9033 } while (cur->next != NULL);
9034
9035 if (cur->doc != node->doc)
9036 goto leave_node;
9037 }
9038 cur->doc = destDoc;
9039 switch (cur->type) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00009040 case XML_XINCLUDE_START:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009041 case XML_XINCLUDE_END:
9042 /*
9043 * TODO
9044 */
9045 return (-1);
Daniel Veillardaa6de472008-08-25 14:53:31 +00009046 case XML_ELEMENT_NODE:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009047 curElem = cur;
9048 depth++;
9049 /*
Daniel Veillardaa6de472008-08-25 14:53:31 +00009050 * Namespace declarations.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009051 * - ns->href and ns->prefix are never in the dict, so
9052 * we need not move the values over to the destination dict.
9053 * - Note that for custom handling of ns-references,
9054 * the ns-decls need not be stored in the ns-map,
9055 * since they won't be referenced by node->ns.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009056 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009057 if ((cur->nsDef) &&
9058 ((ctxt == NULL) || (ctxt->getNsForNodeFunc == NULL)))
9059 {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009060 if (! parnsdone) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009061 /*
9062 * Gather @parent's in-scope ns-decls.
9063 */
9064 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
9065 destParent) == -1)
9066 goto internal_error;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009067 parnsdone = 1;
9068 }
9069 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
9070 /*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009071 * NOTE: ns->prefix and ns->href are never in the dict.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009072 * XML_TREE_ADOPT_STR(ns->prefix)
9073 * XML_TREE_ADOPT_STR(ns->href)
Daniel Veillardaa6de472008-08-25 14:53:31 +00009074 */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009075 /*
9076 * Does it shadow any ns-decl?
Daniel Veillardaa6de472008-08-25 14:53:31 +00009077 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009078 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009079 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009080 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
9081 (mi->shadowDepth == -1) &&
9082 ((ns->prefix == mi->newNs->prefix) ||
9083 xmlStrEqual(ns->prefix,
9084 mi->newNs->prefix))) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00009085
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009086 mi->shadowDepth = depth;
9087 }
9088 }
9089 }
9090 /*
9091 * Push mapping.
9092 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009093 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009094 ns, ns, depth) == NULL)
9095 goto internal_error;
9096 }
9097 }
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009098 /* No break on purpose. */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009099 case XML_ATTRIBUTE_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009100 /* No namespace, no fun. */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009101 if (cur->ns == NULL)
9102 goto ns_end;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009103
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009104 if (! parnsdone) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009105 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
9106 destParent) == -1)
9107 goto internal_error;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009108 parnsdone = 1;
9109 }
9110 /*
9111 * Adopt ns-references.
9112 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009113 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009114 /*
9115 * Search for a mapping.
9116 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009117 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009118 if ((mi->shadowDepth == -1) &&
9119 (cur->ns == mi->oldNs)) {
9120
9121 cur->ns = mi->newNs;
9122 goto ns_end;
9123 }
9124 }
9125 }
9126 /*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009127 * No matching namespace in scope. We need a new one.
9128 */
9129 if ((ctxt) && (ctxt->getNsForNodeFunc)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009130 /*
9131 * User-defined behaviour.
9132 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009133 ns = ctxt->getNsForNodeFunc(ctxt, cur,
9134 cur->ns->href, cur->ns->prefix);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009135 /*
9136 * Insert mapping if ns is available; it's the users fault
9137 * if not.
9138 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009139 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009140 cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009141 goto internal_error;
9142 cur->ns = ns;
9143 } else {
9144 /*
9145 * Aquire a normalized ns-decl and add it to the map.
9146 */
9147 if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc,
Daniel Veillardaa6de472008-08-25 14:53:31 +00009148 /* ns-decls on curElem or on destDoc->oldNs */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009149 destParent ? curElem : NULL,
9150 cur->ns, &ns,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009151 &nsMap, depth,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009152 ancestorsOnly,
9153 /* ns-decls must be prefixed for attributes. */
9154 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
9155 goto internal_error;
9156 cur->ns = ns;
9157 }
9158ns_end:
9159 /*
9160 * Further node properties.
9161 * TODO: Is this all?
9162 */
9163 XML_TREE_ADOPT_STR(cur->name)
9164 if (cur->type == XML_ELEMENT_NODE) {
9165 cur->psvi = NULL;
9166 cur->line = 0;
9167 cur->extra = 0;
9168 /*
9169 * Walk attributes.
9170 */
9171 if (cur->properties != NULL) {
9172 /*
9173 * Process first attribute node.
9174 */
9175 cur = (xmlNodePtr) cur->properties;
9176 continue;
9177 }
9178 } else {
9179 /*
9180 * Attributes.
9181 */
9182 if ((sourceDoc != NULL) &&
9183 (((xmlAttrPtr) cur)->atype == XML_ATTRIBUTE_ID))
Daniel Veillardaa6de472008-08-25 14:53:31 +00009184 {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009185 xmlRemoveID(sourceDoc, (xmlAttrPtr) cur);
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009186 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009187 ((xmlAttrPtr) cur)->atype = 0;
9188 ((xmlAttrPtr) cur)->psvi = NULL;
9189 }
9190 break;
9191 case XML_TEXT_NODE:
Daniel Veillardaa6de472008-08-25 14:53:31 +00009192 case XML_CDATA_SECTION_NODE:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009193 /*
9194 * This puts the content in the dest dict, only if
9195 * it was previously in the source dict.
9196 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009197 XML_TREE_ADOPT_STR_2(cur->content)
9198 goto leave_node;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009199 case XML_ENTITY_REF_NODE:
9200 /*
9201 * Remove reference to the entitity-node.
9202 */
9203 cur->content = NULL;
9204 cur->children = NULL;
9205 cur->last = NULL;
9206 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9207 xmlEntityPtr ent;
9208 /*
9209 * Assign new entity-node if available.
9210 */
9211 ent = xmlGetDocEntity(destDoc, cur->name);
9212 if (ent != NULL) {
9213 cur->content = ent->content;
9214 cur->children = (xmlNodePtr) ent;
9215 cur->last = (xmlNodePtr) ent;
9216 }
9217 }
9218 goto leave_node;
9219 case XML_PI_NODE:
9220 XML_TREE_ADOPT_STR(cur->name)
9221 XML_TREE_ADOPT_STR_2(cur->content)
9222 break;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009223 case XML_COMMENT_NODE:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009224 break;
9225 default:
9226 goto internal_error;
9227 }
9228 /*
9229 * Walk the tree.
9230 */
9231 if (cur->children != NULL) {
9232 cur = cur->children;
9233 continue;
9234 }
9235
9236leave_node:
9237 if (cur == node)
9238 break;
9239 if ((cur->type == XML_ELEMENT_NODE) ||
9240 (cur->type == XML_XINCLUDE_START) ||
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009241 (cur->type == XML_XINCLUDE_END))
9242 {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009243 /*
9244 * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
9245 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009246 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009247 /*
9248 * Pop mappings.
9249 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009250 while ((nsMap->last != NULL) &&
9251 (nsMap->last->depth >= depth))
9252 {
Daniel Veillardaa6de472008-08-25 14:53:31 +00009253 XML_NSMAP_POP(nsMap, mi)
9254 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009255 /*
9256 * Unshadow.
9257 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009258 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009259 if (mi->shadowDepth >= depth)
9260 mi->shadowDepth = -1;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009261 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009262 }
9263 depth--;
9264 }
9265 if (cur->next != NULL)
9266 cur = cur->next;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009267 else if ((cur->type == XML_ATTRIBUTE_NODE) &&
9268 (cur->parent->children != NULL))
9269 {
9270 cur = cur->parent->children;
9271 } else {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009272 cur = cur->parent;
9273 goto leave_node;
9274 }
9275 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009276
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009277 goto exit;
9278
Daniel Veillardaa6de472008-08-25 14:53:31 +00009279internal_error:
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009280 ret = -1;
9281
9282exit:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009283 /*
9284 * Cleanup.
9285 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009286 if (nsMap != NULL) {
9287 if ((ctxt) && (ctxt->namespaceMap == nsMap)) {
9288 /*
9289 * Just cleanup the map but don't free.
9290 */
9291 if (nsMap->first) {
9292 if (nsMap->pool)
9293 nsMap->last->next = nsMap->pool;
9294 nsMap->pool = nsMap->first;
9295 nsMap->first = NULL;
9296 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009297 } else
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009298 xmlDOMWrapNsMapFree(nsMap);
9299 }
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009300 return(ret);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009301}
9302
9303/*
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009304* xmlDOMWrapCloneNode:
9305* @ctxt: the optional context for custom processing
9306* @sourceDoc: the optional sourceDoc
9307* @node: the node to start with
9308* @resNode: the clone of the given @node
9309* @destDoc: the destination doc
9310* @destParent: the optional new parent of @node in @destDoc
Daniel Veillardb2f8f1d2006-04-28 16:30:48 +00009311* @deep: descend into child if set
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009312* @options: option flags
9313*
9314* References of out-of scope ns-decls are remapped to point to @destDoc:
9315* 1) If @destParent is given, then nsDef entries on element-nodes are used
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009316* 2) If *no* @destParent is given, then @destDoc->oldNs entries are used.
9317* This is the case when you don't know already where the cloned branch
9318* will be added to.
Daniel Veillardaa6de472008-08-25 14:53:31 +00009319*
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009320* If @destParent is given, it ensures that the tree is namespace
9321* wellformed by creating additional ns-decls where needed.
9322* Note that, since prefixes of already existent ns-decls can be
9323* shadowed by this process, it could break QNames in attribute
9324* values or element content.
9325* TODO:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009326* 1) What to do with XInclude? Currently this returns an error for XInclude.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009327*
9328* Returns 0 if the operation succeeded,
9329* 1 if a node of unsupported (or not yet supported) type was given,
9330* -1 on API/internal errors.
9331*/
9332
9333int
9334xmlDOMWrapCloneNode(xmlDOMWrapCtxtPtr ctxt,
9335 xmlDocPtr sourceDoc,
9336 xmlNodePtr node,
9337 xmlNodePtr *resNode,
9338 xmlDocPtr destDoc,
9339 xmlNodePtr destParent,
9340 int deep,
9341 int options ATTRIBUTE_UNUSED)
9342{
9343 int ret = 0;
9344 xmlNodePtr cur, curElem = NULL;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009345 xmlNsMapPtr nsMap = NULL;
9346 xmlNsMapItemPtr mi;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009347 xmlNsPtr ns;
9348 int depth = -1;
9349 /* int adoptStr = 1; */
9350 /* gather @parent's ns-decls. */
9351 int parnsdone = 0;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009352 /*
Daniel Veillardaa6de472008-08-25 14:53:31 +00009353 * @ancestorsOnly:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009354 * TODO: @ancestorsOnly should be set per option.
9355 *
9356 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009357 int ancestorsOnly = 0;
9358 xmlNodePtr resultClone = NULL, clone = NULL, parentClone = NULL, prevClone = NULL;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009359 xmlNsPtr cloneNs = NULL, *cloneNsDefSlot = NULL;
9360 xmlDictPtr dict; /* The destination dict */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009361
Daniel Veillard11ce4002006-03-10 00:36:23 +00009362 if ((node == NULL) || (resNode == NULL) || (destDoc == NULL))
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009363 return(-1);
9364 /*
9365 * TODO: Initially we support only element-nodes.
9366 */
9367 if (node->type != XML_ELEMENT_NODE)
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009368 return(1);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009369 /*
9370 * Check node->doc sanity.
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009371 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009372 if ((node->doc != NULL) && (sourceDoc != NULL) &&
9373 (node->doc != sourceDoc)) {
9374 /*
9375 * Might be an XIncluded node.
9376 */
9377 return (-1);
9378 }
9379 if (sourceDoc == NULL)
Daniel Veillardaa6de472008-08-25 14:53:31 +00009380 sourceDoc = node->doc;
Daniel Veillard11ce4002006-03-10 00:36:23 +00009381 if (sourceDoc == NULL)
9382 return (-1);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009383
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009384 dict = destDoc->dict;
9385 /*
9386 * Reuse the namespace map of the context.
9387 */
9388 if (ctxt)
9389 nsMap = (xmlNsMapPtr) ctxt->namespaceMap;
9390
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009391 *resNode = NULL;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009392
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009393 cur = node;
Daniel Veillard3e62adb2012-08-09 14:24:02 +08009394 if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
9395 return(-1);
9396
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009397 while (cur != NULL) {
9398 if (cur->doc != sourceDoc) {
9399 /*
9400 * We'll assume XIncluded nodes if the doc differs.
9401 * TODO: Do we need to reconciliate XIncluded nodes?
9402 * TODO: This here returns -1 in this case.
9403 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009404 goto internal_error;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009405 }
9406 /*
9407 * Create a new node.
9408 */
9409 switch (cur->type) {
9410 case XML_XINCLUDE_START:
9411 case XML_XINCLUDE_END:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009412 /*
9413 * TODO: What to do with XInclude?
9414 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009415 goto internal_error;
9416 break;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009417 case XML_ELEMENT_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009418 case XML_TEXT_NODE:
Daniel Veillardaa6de472008-08-25 14:53:31 +00009419 case XML_CDATA_SECTION_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009420 case XML_COMMENT_NODE:
Daniel Veillardaa6de472008-08-25 14:53:31 +00009421 case XML_PI_NODE:
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009422 case XML_DOCUMENT_FRAG_NODE:
9423 case XML_ENTITY_REF_NODE:
9424 case XML_ENTITY_NODE:
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009425 /*
9426 * Nodes of xmlNode structure.
9427 */
9428 clone = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
9429 if (clone == NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009430 xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating a node");
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009431 goto internal_error;
9432 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009433 memset(clone, 0, sizeof(xmlNode));
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009434 /*
9435 * Set hierachical links.
9436 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009437 if (resultClone != NULL) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009438 clone->parent = parentClone;
9439 if (prevClone) {
9440 prevClone->next = clone;
9441 clone->prev = prevClone;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009442 } else
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009443 parentClone->children = clone;
9444 } else
9445 resultClone = clone;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009446
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009447 break;
9448 case XML_ATTRIBUTE_NODE:
9449 /*
9450 * Attributes (xmlAttr).
9451 */
9452 clone = (xmlNodePtr) xmlMalloc(sizeof(xmlAttr));
9453 if (clone == NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009454 xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating an attr-node");
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009455 goto internal_error;
9456 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009457 memset(clone, 0, sizeof(xmlAttr));
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009458 /*
9459 * Set hierachical links.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009460 * TODO: Change this to add to the end of attributes.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009461 */
9462 if (resultClone != NULL) {
9463 clone->parent = parentClone;
9464 if (prevClone) {
9465 prevClone->next = clone;
9466 clone->prev = prevClone;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009467 } else
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009468 parentClone->properties = (xmlAttrPtr) clone;
9469 } else
9470 resultClone = clone;
9471 break;
9472 default:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009473 /*
9474 * TODO QUESTION: Any other nodes expected?
9475 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009476 goto internal_error;
9477 }
9478
9479 clone->type = cur->type;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009480 clone->doc = destDoc;
9481
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009482 /*
9483 * Clone the name of the node if any.
9484 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009485 if (cur->name == xmlStringText)
9486 clone->name = xmlStringText;
9487 else if (cur->name == xmlStringTextNoenc)
9488 /*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009489 * NOTE: Although xmlStringTextNoenc is never assigned to a node
9490 * in tree.c, it might be set in Libxslt via
Daniel Veillardaa6de472008-08-25 14:53:31 +00009491 * "xsl:disable-output-escaping".
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009492 */
9493 clone->name = xmlStringTextNoenc;
9494 else if (cur->name == xmlStringComment)
9495 clone->name = xmlStringComment;
9496 else if (cur->name != NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009497 DICT_CONST_COPY(cur->name, clone->name);
Daniel Veillardaa6de472008-08-25 14:53:31 +00009498 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009499
9500 switch (cur->type) {
9501 case XML_XINCLUDE_START:
9502 case XML_XINCLUDE_END:
9503 /*
9504 * TODO
9505 */
9506 return (-1);
9507 case XML_ELEMENT_NODE:
9508 curElem = cur;
9509 depth++;
9510 /*
9511 * Namespace declarations.
9512 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009513 if (cur->nsDef != NULL) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009514 if (! parnsdone) {
9515 if (destParent && (ctxt == NULL)) {
9516 /*
9517 * Gather @parent's in-scope ns-decls.
9518 */
9519 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
9520 destParent) == -1)
Daniel Veillardaa6de472008-08-25 14:53:31 +00009521 goto internal_error;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009522 }
9523 parnsdone = 1;
9524 }
9525 /*
9526 * Clone namespace declarations.
9527 */
9528 cloneNsDefSlot = &(clone->nsDef);
9529 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
9530 /*
9531 * Create a new xmlNs.
9532 */
9533 cloneNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
9534 if (cloneNs == NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009535 xmlTreeErrMemory("xmlDOMWrapCloneNode(): "
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009536 "allocating namespace");
9537 return(-1);
9538 }
9539 memset(cloneNs, 0, sizeof(xmlNs));
9540 cloneNs->type = XML_LOCAL_NAMESPACE;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009541
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009542 if (ns->href != NULL)
9543 cloneNs->href = xmlStrdup(ns->href);
9544 if (ns->prefix != NULL)
9545 cloneNs->prefix = xmlStrdup(ns->prefix);
9546
9547 *cloneNsDefSlot = cloneNs;
9548 cloneNsDefSlot = &(cloneNs->next);
9549
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009550 /*
9551 * Note that for custom handling of ns-references,
9552 * the ns-decls need not be stored in the ns-map,
9553 * since they won't be referenced by node->ns.
9554 */
9555 if ((ctxt == NULL) ||
9556 (ctxt->getNsForNodeFunc == NULL))
9557 {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009558 /*
9559 * Does it shadow any ns-decl?
9560 */
9561 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00009562 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009563 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
9564 (mi->shadowDepth == -1) &&
9565 ((ns->prefix == mi->newNs->prefix) ||
9566 xmlStrEqual(ns->prefix,
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009567 mi->newNs->prefix))) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009568 /*
9569 * Mark as shadowed at the current
9570 * depth.
9571 */
9572 mi->shadowDepth = depth;
9573 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009574 }
9575 }
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009576 /*
9577 * Push mapping.
9578 */
9579 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
9580 ns, cloneNs, depth) == NULL)
9581 goto internal_error;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009582 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009583 }
9584 }
9585 /* cur->ns will be processed further down. */
9586 break;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009587 case XML_ATTRIBUTE_NODE:
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009588 /* IDs will be processed further down. */
9589 /* cur->ns will be processed further down. */
9590 break;
9591 case XML_TEXT_NODE:
9592 case XML_CDATA_SECTION_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009593 /*
9594 * Note that this will also cover the values of attributes.
9595 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009596 DICT_COPY(cur->content, clone->content);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009597 goto leave_node;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009598 case XML_ENTITY_NODE:
9599 /* TODO: What to do here? */
9600 goto leave_node;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009601 case XML_ENTITY_REF_NODE:
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009602 if (sourceDoc != destDoc) {
9603 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9604 xmlEntityPtr ent;
9605 /*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009606 * Different doc: Assign new entity-node if available.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009607 */
9608 ent = xmlGetDocEntity(destDoc, cur->name);
9609 if (ent != NULL) {
9610 clone->content = ent->content;
9611 clone->children = (xmlNodePtr) ent;
9612 clone->last = (xmlNodePtr) ent;
9613 }
9614 }
9615 } else {
9616 /*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009617 * Same doc: Use the current node's entity declaration
9618 * and value.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009619 */
9620 clone->content = cur->content;
9621 clone->children = cur->children;
9622 clone->last = cur->last;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009623 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009624 goto leave_node;
9625 case XML_PI_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009626 DICT_COPY(cur->content, clone->content);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009627 goto leave_node;
9628 case XML_COMMENT_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009629 DICT_COPY(cur->content, clone->content);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009630 goto leave_node;
9631 default:
9632 goto internal_error;
9633 }
9634
9635 if (cur->ns == NULL)
9636 goto end_ns_reference;
9637
9638/* handle_ns_reference: */
9639 /*
9640 ** The following will take care of references to ns-decls ********
Daniel Veillardaa6de472008-08-25 14:53:31 +00009641 ** and is intended only for element- and attribute-nodes.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009642 **
9643 */
9644 if (! parnsdone) {
9645 if (destParent && (ctxt == NULL)) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009646 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, destParent) == -1)
Daniel Veillardaa6de472008-08-25 14:53:31 +00009647 goto internal_error;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009648 }
9649 parnsdone = 1;
9650 }
9651 /*
9652 * Adopt ns-references.
9653 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009654 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009655 /*
9656 * Search for a mapping.
9657 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009658 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009659 if ((mi->shadowDepth == -1) &&
9660 (cur->ns == mi->oldNs)) {
9661 /*
9662 * This is the nice case: a mapping was found.
9663 */
9664 clone->ns = mi->newNs;
9665 goto end_ns_reference;
9666 }
9667 }
9668 }
9669 /*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009670 * No matching namespace in scope. We need a new one.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009671 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009672 if ((ctxt != NULL) && (ctxt->getNsForNodeFunc != NULL)) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009673 /*
9674 * User-defined behaviour.
9675 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009676 ns = ctxt->getNsForNodeFunc(ctxt, cur,
9677 cur->ns->href, cur->ns->prefix);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009678 /*
9679 * Add user's mapping.
9680 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009681 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009682 cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
9683 goto internal_error;
9684 clone->ns = ns;
9685 } else {
9686 /*
9687 * Aquire a normalized ns-decl and add it to the map.
9688 */
9689 if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc,
Daniel Veillardaa6de472008-08-25 14:53:31 +00009690 /* ns-decls on curElem or on destDoc->oldNs */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009691 destParent ? curElem : NULL,
9692 cur->ns, &ns,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009693 &nsMap, depth,
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009694 /* if we need to search only in the ancestor-axis */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009695 ancestorsOnly,
9696 /* ns-decls must be prefixed for attributes. */
9697 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
9698 goto internal_error;
9699 clone->ns = ns;
9700 }
9701
9702end_ns_reference:
9703
9704 /*
9705 * Some post-processing.
9706 *
9707 * Handle ID attributes.
9708 */
9709 if ((clone->type == XML_ATTRIBUTE_NODE) &&
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009710 (clone->parent != NULL))
9711 {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009712 if (xmlIsID(destDoc, clone->parent, (xmlAttrPtr) clone)) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00009713
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009714 xmlChar *idVal;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009715
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009716 idVal = xmlNodeListGetString(cur->doc, cur->children, 1);
9717 if (idVal != NULL) {
9718 if (xmlAddID(NULL, destDoc, idVal, (xmlAttrPtr) cur) == NULL) {
9719 /* TODO: error message. */
9720 xmlFree(idVal);
9721 goto internal_error;
9722 }
9723 xmlFree(idVal);
9724 }
9725 }
9726 }
9727 /*
9728 **
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009729 ** The following will traverse the tree **************************
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009730 **
Daniel Veillardaa6de472008-08-25 14:53:31 +00009731 *
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009732 * Walk the element's attributes before descending into child-nodes.
9733 */
9734 if ((cur->type == XML_ELEMENT_NODE) && (cur->properties != NULL)) {
9735 prevClone = NULL;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009736 parentClone = clone;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009737 cur = (xmlNodePtr) cur->properties;
9738 continue;
9739 }
9740into_content:
9741 /*
9742 * Descend into child-nodes.
9743 */
9744 if (cur->children != NULL) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009745 if (deep || (cur->type == XML_ATTRIBUTE_NODE)) {
9746 prevClone = NULL;
9747 parentClone = clone;
9748 cur = cur->children;
9749 continue;
9750 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009751 }
9752
9753leave_node:
9754 /*
9755 * At this point we are done with the node, its content
9756 * and an element-nodes's attribute-nodes.
9757 */
9758 if (cur == node)
9759 break;
9760 if ((cur->type == XML_ELEMENT_NODE) ||
9761 (cur->type == XML_XINCLUDE_START) ||
9762 (cur->type == XML_XINCLUDE_END)) {
9763 /*
9764 * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
9765 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009766 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009767 /*
9768 * Pop mappings.
9769 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009770 while ((nsMap->last != NULL) &&
9771 (nsMap->last->depth >= depth))
9772 {
Daniel Veillardaa6de472008-08-25 14:53:31 +00009773 XML_NSMAP_POP(nsMap, mi)
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009774 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009775 /*
9776 * Unshadow.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009777 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009778 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009779 if (mi->shadowDepth >= depth)
9780 mi->shadowDepth = -1;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009781 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009782 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009783 depth--;
9784 }
9785 if (cur->next != NULL) {
9786 prevClone = clone;
9787 cur = cur->next;
9788 } else if (cur->type != XML_ATTRIBUTE_NODE) {
9789 /*
9790 * Set clone->last.
9791 */
Daniel Veillard11ce4002006-03-10 00:36:23 +00009792 if (clone->parent != NULL)
9793 clone->parent->last = clone;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009794 clone = clone->parent;
Daniel Veillard75d13092013-09-11 15:11:27 +08009795 if (clone != NULL)
9796 parentClone = clone->parent;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009797 /*
9798 * Process parent --> next;
9799 */
9800 cur = cur->parent;
9801 goto leave_node;
9802 } else {
9803 /* This is for attributes only. */
9804 clone = clone->parent;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009805 parentClone = clone->parent;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009806 /*
9807 * Process parent-element --> children.
9808 */
9809 cur = cur->parent;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009810 goto into_content;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009811 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009812 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009813 goto exit;
9814
9815internal_error:
9816 ret = -1;
9817
9818exit:
9819 /*
9820 * Cleanup.
9821 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009822 if (nsMap != NULL) {
9823 if ((ctxt) && (ctxt->namespaceMap == nsMap)) {
9824 /*
9825 * Just cleanup the map but don't free.
9826 */
9827 if (nsMap->first) {
9828 if (nsMap->pool)
9829 nsMap->last->next = nsMap->pool;
9830 nsMap->pool = nsMap->first;
9831 nsMap->first = NULL;
9832 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009833 } else
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009834 xmlDOMWrapNsMapFree(nsMap);
9835 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009836 /*
9837 * TODO: Should we try a cleanup of the cloned node in case of a
9838 * fatal error?
9839 */
9840 *resNode = resultClone;
9841 return (ret);
9842}
9843
9844/*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009845* xmlDOMWrapAdoptAttr:
9846* @ctxt: the optional context for custom processing
9847* @sourceDoc: the optional source document of attr
9848* @attr: the attribute-node to be adopted
9849* @destDoc: the destination doc for adoption
9850* @destParent: the optional new parent of @attr in @destDoc
9851* @options: option flags
9852*
9853* @attr is adopted by @destDoc.
9854* Ensures that ns-references point to @destDoc: either to
9855* elements->nsDef entries if @destParent is given, or to
9856* @destDoc->oldNs otherwise.
9857*
9858* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
9859*/
9860static int
9861xmlDOMWrapAdoptAttr(xmlDOMWrapCtxtPtr ctxt,
9862 xmlDocPtr sourceDoc,
9863 xmlAttrPtr attr,
9864 xmlDocPtr destDoc,
9865 xmlNodePtr destParent,
9866 int options ATTRIBUTE_UNUSED)
9867{
9868 xmlNodePtr cur;
9869 int adoptStr = 1;
9870
9871 if ((attr == NULL) || (destDoc == NULL))
9872 return (-1);
Daniel Veillardaa6de472008-08-25 14:53:31 +00009873
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009874 attr->doc = destDoc;
9875 if (attr->ns != NULL) {
9876 xmlNsPtr ns = NULL;
9877
9878 if (ctxt != NULL) {
9879 /* TODO: User defined. */
9880 }
9881 /* XML Namespace. */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009882 if (IS_STR_XML(attr->ns->prefix)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009883 ns = xmlTreeEnsureXMLDecl(destDoc);
9884 } else if (destParent == NULL) {
9885 /*
9886 * Store in @destDoc->oldNs.
9887 */
9888 ns = xmlDOMWrapStoreNs(destDoc, attr->ns->href, attr->ns->prefix);
9889 } else {
9890 /*
9891 * Declare on @destParent.
9892 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009893 if (xmlSearchNsByNamespaceStrict(destDoc, destParent, attr->ns->href,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009894 &ns, 1) == -1)
9895 goto internal_error;
9896 if (ns == NULL) {
9897 ns = xmlDOMWrapNSNormDeclareNsForced(destDoc, destParent,
9898 attr->ns->href, attr->ns->prefix, 1);
9899 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009900 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009901 if (ns == NULL)
9902 goto internal_error;
9903 attr->ns = ns;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009904 }
9905
9906 XML_TREE_ADOPT_STR(attr->name);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009907 attr->atype = 0;
9908 attr->psvi = NULL;
9909 /*
9910 * Walk content.
9911 */
9912 if (attr->children == NULL)
9913 return (0);
9914 cur = attr->children;
Daniel Veillard3e62adb2012-08-09 14:24:02 +08009915 if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL))
9916 goto internal_error;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009917 while (cur != NULL) {
9918 cur->doc = destDoc;
9919 switch (cur->type) {
9920 case XML_TEXT_NODE:
9921 case XML_CDATA_SECTION_NODE:
9922 XML_TREE_ADOPT_STR_2(cur->content)
Daniel Veillardaa6de472008-08-25 14:53:31 +00009923 break;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009924 case XML_ENTITY_REF_NODE:
9925 /*
9926 * Remove reference to the entitity-node.
9927 */
9928 cur->content = NULL;
9929 cur->children = NULL;
9930 cur->last = NULL;
9931 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9932 xmlEntityPtr ent;
9933 /*
9934 * Assign new entity-node if available.
9935 */
9936 ent = xmlGetDocEntity(destDoc, cur->name);
9937 if (ent != NULL) {
9938 cur->content = ent->content;
9939 cur->children = (xmlNodePtr) ent;
9940 cur->last = (xmlNodePtr) ent;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009941 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009942 }
9943 break;
9944 default:
9945 break;
9946 }
9947 if (cur->children != NULL) {
9948 cur = cur->children;
9949 continue;
9950 }
9951next_sibling:
9952 if (cur == (xmlNodePtr) attr)
9953 break;
9954 if (cur->next != NULL)
9955 cur = cur->next;
9956 else {
9957 cur = cur->parent;
9958 goto next_sibling;
9959 }
9960 }
9961 return (0);
9962internal_error:
9963 return (-1);
9964}
9965
9966/*
9967* xmlDOMWrapAdoptNode:
9968* @ctxt: the optional context for custom processing
9969* @sourceDoc: the optional sourceDoc
9970* @node: the node to start with
9971* @destDoc: the destination doc
Kasimier T. Buchcik4d9c9482005-06-27 15:04:46 +00009972* @destParent: the optional new parent of @node in @destDoc
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009973* @options: option flags
9974*
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009975* References of out-of scope ns-decls are remapped to point to @destDoc:
9976* 1) If @destParent is given, then nsDef entries on element-nodes are used
9977* 2) If *no* @destParent is given, then @destDoc->oldNs entries are used
Daniel Veillard7e35abe2014-03-28 22:55:31 +08009978* This is the case when you have an unlinked node and just want to move it
Daniel Veillardaa6de472008-08-25 14:53:31 +00009979* to the context of
9980*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009981* If @destParent is given, it ensures that the tree is namespace
9982* wellformed by creating additional ns-decls where needed.
9983* Note that, since prefixes of already existent ns-decls can be
9984* shadowed by this process, it could break QNames in attribute
9985* values or element content.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009986* NOTE: This function was not intensively tested.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009987*
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009988* Returns 0 if the operation succeeded,
9989* 1 if a node of unsupported type was given,
9990* 2 if a node of not yet supported type was given and
9991* -1 on API/internal errors.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009992*/
9993int
9994xmlDOMWrapAdoptNode(xmlDOMWrapCtxtPtr ctxt,
9995 xmlDocPtr sourceDoc,
9996 xmlNodePtr node,
Daniel Veillardaa6de472008-08-25 14:53:31 +00009997 xmlDocPtr destDoc,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009998 xmlNodePtr destParent,
9999 int options)
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +000010000{
Daniel Veillard3e62adb2012-08-09 14:24:02 +080010001 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) ||
10002 (destDoc == NULL) ||
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010003 ((destParent != NULL) && (destParent->doc != destDoc)))
10004 return(-1);
10005 /*
10006 * Check node->doc sanity.
Daniel Veillardaa6de472008-08-25 14:53:31 +000010007 */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010008 if ((node->doc != NULL) && (sourceDoc != NULL) &&
10009 (node->doc != sourceDoc)) {
10010 /*
10011 * Might be an XIncluded node.
10012 */
10013 return (-1);
10014 }
10015 if (sourceDoc == NULL)
10016 sourceDoc = node->doc;
10017 if (sourceDoc == destDoc)
10018 return (-1);
10019 switch (node->type) {
Daniel Veillardaa6de472008-08-25 14:53:31 +000010020 case XML_ELEMENT_NODE:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010021 case XML_ATTRIBUTE_NODE:
10022 case XML_TEXT_NODE:
10023 case XML_CDATA_SECTION_NODE:
10024 case XML_ENTITY_REF_NODE:
10025 case XML_PI_NODE:
10026 case XML_COMMENT_NODE:
10027 break;
10028 case XML_DOCUMENT_FRAG_NODE:
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +000010029 /* TODO: Support document-fragment-nodes. */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010030 return (2);
10031 default:
10032 return (1);
10033 }
10034 /*
10035 * Unlink only if @node was not already added to @destParent.
10036 */
10037 if ((node->parent != NULL) && (destParent != node->parent))
10038 xmlUnlinkNode(node);
10039
10040 if (node->type == XML_ELEMENT_NODE) {
10041 return (xmlDOMWrapAdoptBranch(ctxt, sourceDoc, node,
10042 destDoc, destParent, options));
10043 } else if (node->type == XML_ATTRIBUTE_NODE) {
10044 return (xmlDOMWrapAdoptAttr(ctxt, sourceDoc,
10045 (xmlAttrPtr) node, destDoc, destParent, options));
Daniel Veillardaa6de472008-08-25 14:53:31 +000010046 } else {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010047 xmlNodePtr cur = node;
10048 int adoptStr = 1;
10049
10050 cur->doc = destDoc;
10051 /*
10052 * Optimize string adoption.
10053 */
10054 if ((sourceDoc != NULL) &&
10055 (sourceDoc->dict == destDoc->dict))
10056 adoptStr = 0;
10057 switch (node->type) {
Daniel Veillardaa6de472008-08-25 14:53:31 +000010058 case XML_TEXT_NODE:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010059 case XML_CDATA_SECTION_NODE:
10060 XML_TREE_ADOPT_STR_2(node->content)
10061 break;
10062 case XML_ENTITY_REF_NODE:
10063 /*
10064 * Remove reference to the entitity-node.
10065 */
10066 node->content = NULL;
10067 node->children = NULL;
10068 node->last = NULL;
10069 if ((destDoc->intSubset) || (destDoc->extSubset)) {
10070 xmlEntityPtr ent;
10071 /*
10072 * Assign new entity-node if available.
10073 */
10074 ent = xmlGetDocEntity(destDoc, node->name);
10075 if (ent != NULL) {
10076 node->content = ent->content;
10077 node->children = (xmlNodePtr) ent;
10078 node->last = (xmlNodePtr) ent;
10079 }
10080 }
10081 XML_TREE_ADOPT_STR(node->name)
10082 break;
Daniel Veillard7e21fd12005-07-03 21:44:07 +000010083 case XML_PI_NODE: {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010084 XML_TREE_ADOPT_STR(node->name)
10085 XML_TREE_ADOPT_STR_2(node->content)
10086 break;
Daniel Veillard7e21fd12005-07-03 21:44:07 +000010087 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010088 default:
10089 break;
10090 }
Daniel Veillardaa6de472008-08-25 14:53:31 +000010091 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +000010092 return (0);
10093}
10094
Daniel Veillard5d4644e2005-04-01 13:11:58 +000010095#define bottom_tree
10096#include "elfgcchack.h"