blob: 8baae3d19424da9a5e0d87cb67e0ee509cc8b255 [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 Veillarda880b122003-04-21 21:36:41 +000044int __xmlRegisterCallbacks = 0;
45
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +000046/************************************************************************
47 * *
Daniel Veillardaa6de472008-08-25 14:53:31 +000048 * Forward declarations *
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +000049 * *
50 ************************************************************************/
51
Daniel Veillard8ed10722009-08-20 19:17:36 +020052static xmlNsPtr
53xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns);
Daniel Veillard56a4cb82001-03-24 17:00:36 +000054
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +000055static xmlChar* xmlGetPropNodeValueInternal(xmlAttrPtr prop);
56
Daniel Veillard56a4cb82001-03-24 17:00:36 +000057/************************************************************************
58 * *
Daniel Veillardaa6de472008-08-25 14:53:31 +000059 * Tree memory error handler *
Daniel Veillard18ec16e2003-10-07 23:16:40 +000060 * *
61 ************************************************************************/
62/**
63 * xmlTreeErrMemory:
64 * @extra: extra informations
65 *
66 * Handle an out of memory condition
67 */
68static void
69xmlTreeErrMemory(const char *extra)
70{
71 __xmlSimpleError(XML_FROM_TREE, XML_ERR_NO_MEMORY, NULL, NULL, extra);
72}
73
74/**
75 * xmlTreeErr:
76 * @code: the error number
77 * @extra: extra informations
78 *
79 * Handle an out of memory condition
80 */
81static void
82xmlTreeErr(int code, xmlNodePtr node, const char *extra)
83{
84 const char *msg = NULL;
85
86 switch(code) {
87 case XML_TREE_INVALID_HEX:
Daniel Veillardac996a12004-07-30 12:02:58 +000088 msg = "invalid hexadecimal character value\n";
Daniel Veillard18ec16e2003-10-07 23:16:40 +000089 break;
90 case XML_TREE_INVALID_DEC:
Daniel Veillardac996a12004-07-30 12:02:58 +000091 msg = "invalid decimal character value\n";
Daniel Veillard18ec16e2003-10-07 23:16:40 +000092 break;
93 case XML_TREE_UNTERMINATED_ENTITY:
Daniel Veillardac996a12004-07-30 12:02:58 +000094 msg = "unterminated entity reference %15s\n";
Daniel Veillard18ec16e2003-10-07 23:16:40 +000095 break;
Daniel Veillard6f8611f2008-02-15 08:33:21 +000096 case XML_TREE_NOT_UTF8:
97 msg = "string is not in UTF-8\n";
98 break;
Daniel Veillard18ec16e2003-10-07 23:16:40 +000099 default:
Daniel Veillardac996a12004-07-30 12:02:58 +0000100 msg = "unexpected error number\n";
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000101 }
102 __xmlSimpleError(XML_FROM_TREE, code, node, msg, extra);
103}
104
105/************************************************************************
106 * *
Daniel Veillardaa6de472008-08-25 14:53:31 +0000107 * A few static variables and macros *
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000108 * *
109 ************************************************************************/
Daniel Veillardd0463562001-10-13 09:15:48 +0000110/* #undef xmlStringText */
Daniel Veillard22090732001-07-16 00:06:07 +0000111const xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 };
Daniel Veillardd0463562001-10-13 09:15:48 +0000112/* #undef xmlStringTextNoenc */
Daniel Veillard22090732001-07-16 00:06:07 +0000113const xmlChar xmlStringTextNoenc[] =
Owen Taylor3473f882001-02-23 17:55:21 +0000114 { 't', 'e', 'x', 't', 'n', 'o', 'e', 'n', 'c', 0 };
Daniel Veillardd0463562001-10-13 09:15:48 +0000115/* #undef xmlStringComment */
Daniel Veillard22090732001-07-16 00:06:07 +0000116const xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 };
117
Owen Taylor3473f882001-02-23 17:55:21 +0000118static int xmlCompressMode = 0;
119static int xmlCheckDTD = 1;
Owen Taylor3473f882001-02-23 17:55:21 +0000120
Owen Taylor3473f882001-02-23 17:55:21 +0000121#define UPDATE_LAST_CHILD_AND_PARENT(n) if ((n) != NULL) { \
122 xmlNodePtr ulccur = (n)->children; \
123 if (ulccur == NULL) { \
124 (n)->last = NULL; \
125 } else { \
126 while (ulccur->next != NULL) { \
Daniel Veillardaa6de472008-08-25 14:53:31 +0000127 ulccur->parent = (n); \
Owen Taylor3473f882001-02-23 17:55:21 +0000128 ulccur = ulccur->next; \
129 } \
130 ulccur->parent = (n); \
131 (n)->last = ulccur; \
132}}
133
Kasimier T. Buchcik44353412006-03-06 13:26:16 +0000134#define IS_STR_XML(str) ((str != NULL) && (str[0] == 'x') && \
135 (str[1] == 'm') && (str[2] == 'l') && (str[3] == 0))
136
Owen Taylor3473f882001-02-23 17:55:21 +0000137/* #define DEBUG_BUFFER */
138/* #define DEBUG_TREE */
139
140/************************************************************************
141 * *
Daniel Veillardaa6de472008-08-25 14:53:31 +0000142 * Functions to move to entities.c once the *
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000143 * API freeze is smoothen and they can be made public. *
144 * *
145 ************************************************************************/
146#include <libxml/hash.h>
Daniel Veillardaa6de472008-08-25 14:53:31 +0000147
Daniel Veillard652327a2003-09-29 18:02:38 +0000148#ifdef LIBXML_TREE_ENABLED
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000149/**
150 * xmlGetEntityFromDtd:
151 * @dtd: A pointer to the DTD to search
152 * @name: The entity name
153 *
154 * Do an entity lookup in the DTD entity hash table and
155 * return the corresponding entity, if found.
Daniel Veillardaa6de472008-08-25 14:53:31 +0000156 *
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000157 * Returns A pointer to the entity structure or NULL if not found.
158 */
159static xmlEntityPtr
160xmlGetEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) {
161 xmlEntitiesTablePtr table;
Daniel Veillardaa6de472008-08-25 14:53:31 +0000162
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000163 if((dtd != NULL) && (dtd->entities != NULL)) {
164 table = (xmlEntitiesTablePtr) dtd->entities;
165 return((xmlEntityPtr) xmlHashLookup(table, name));
Daniel Veillardaa6de472008-08-25 14:53:31 +0000166 /* return(xmlGetEntityFromTable(table, name)); */
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000167 }
168 return(NULL);
169}
170/**
171 * xmlGetParameterEntityFromDtd:
172 * @dtd: A pointer to the DTD to search
173 * @name: The entity name
Daniel Veillardaa6de472008-08-25 14:53:31 +0000174 *
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000175 * Do an entity lookup in the DTD pararmeter entity hash table and
176 * return the corresponding entity, if found.
177 *
178 * Returns A pointer to the entity structure or NULL if not found.
179 */
180static xmlEntityPtr
181xmlGetParameterEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) {
182 xmlEntitiesTablePtr table;
Daniel Veillardaa6de472008-08-25 14:53:31 +0000183
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000184 if ((dtd != NULL) && (dtd->pentities != NULL)) {
185 table = (xmlEntitiesTablePtr) dtd->pentities;
186 return((xmlEntityPtr) xmlHashLookup(table, name));
187 /* return(xmlGetEntityFromTable(table, name)); */
188 }
189 return(NULL);
190}
Daniel Veillard652327a2003-09-29 18:02:38 +0000191#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000192
193/************************************************************************
194 * *
Daniel Veillardc00cda82003-04-07 10:22:39 +0000195 * QName handling helper *
196 * *
197 ************************************************************************/
198
199/**
200 * xmlBuildQName:
201 * @ncname: the Name
202 * @prefix: the prefix
203 * @memory: preallocated memory
204 * @len: preallocated memory length
205 *
206 * Builds the QName @prefix:@ncname in @memory if there is enough space
207 * and prefix is not NULL nor empty, otherwise allocate a new string.
208 * If prefix is NULL or empty it returns ncname.
209 *
210 * Returns the new string which must be freed by the caller if different from
211 * @memory and @ncname or NULL in case of error
212 */
213xmlChar *
214xmlBuildQName(const xmlChar *ncname, const xmlChar *prefix,
215 xmlChar *memory, int len) {
216 int lenn, lenp;
217 xmlChar *ret;
218
Daniel Veillard3b7840c2003-09-11 23:42:01 +0000219 if (ncname == NULL) return(NULL);
220 if (prefix == NULL) return((xmlChar *) ncname);
Daniel Veillardc00cda82003-04-07 10:22:39 +0000221
222 lenn = strlen((char *) ncname);
223 lenp = strlen((char *) prefix);
224
225 if ((memory == NULL) || (len < lenn + lenp + 2)) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000226 ret = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000227 if (ret == NULL) {
228 xmlTreeErrMemory("building QName");
229 return(NULL);
230 }
Daniel Veillardc00cda82003-04-07 10:22:39 +0000231 } else {
232 ret = memory;
233 }
234 memcpy(&ret[0], prefix, lenp);
235 ret[lenp] = ':';
236 memcpy(&ret[lenp + 1], ncname, lenn);
237 ret[lenn + lenp + 1] = 0;
238 return(ret);
239}
240
241/**
242 * xmlSplitQName2:
243 * @name: the full QName
Daniel Veillardaa6de472008-08-25 14:53:31 +0000244 * @prefix: a xmlChar **
Daniel Veillardc00cda82003-04-07 10:22:39 +0000245 *
246 * parse an XML qualified name string
247 *
248 * [NS 5] QName ::= (Prefix ':')? LocalPart
249 *
250 * [NS 6] Prefix ::= NCName
251 *
252 * [NS 7] LocalPart ::= NCName
253 *
254 * Returns NULL if not a QName, otherwise the local part, and prefix
255 * is updated to get the Prefix if any.
256 */
257
258xmlChar *
259xmlSplitQName2(const xmlChar *name, xmlChar **prefix) {
260 int len = 0;
261 xmlChar *ret = NULL;
262
Daniel Veillardd5cc0f72004-11-06 19:24:28 +0000263 if (prefix == NULL) return(NULL);
Daniel Veillardc00cda82003-04-07 10:22:39 +0000264 *prefix = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000265 if (name == NULL) return(NULL);
Daniel Veillardc00cda82003-04-07 10:22:39 +0000266
267#ifndef XML_XML_NAMESPACE
268 /* xml: prefix is not really a namespace */
269 if ((name[0] == 'x') && (name[1] == 'm') &&
270 (name[2] == 'l') && (name[3] == ':'))
271 return(NULL);
272#endif
273
274 /* nasty but valid */
275 if (name[0] == ':')
276 return(NULL);
277
278 /*
279 * we are not trying to validate but just to cut, and yes it will
280 * work even if this is as set of UTF-8 encoded chars
281 */
Daniel Veillardaa6de472008-08-25 14:53:31 +0000282 while ((name[len] != 0) && (name[len] != ':'))
Daniel Veillardc00cda82003-04-07 10:22:39 +0000283 len++;
Daniel Veillardaa6de472008-08-25 14:53:31 +0000284
Daniel Veillardc00cda82003-04-07 10:22:39 +0000285 if (name[len] == 0)
286 return(NULL);
287
288 *prefix = xmlStrndup(name, len);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000289 if (*prefix == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000290 xmlTreeErrMemory("QName split");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000291 return(NULL);
292 }
Daniel Veillardc00cda82003-04-07 10:22:39 +0000293 ret = xmlStrdup(&name[len + 1]);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000294 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000295 xmlTreeErrMemory("QName split");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000296 if (*prefix != NULL) {
297 xmlFree(*prefix);
298 *prefix = NULL;
299 }
300 return(NULL);
301 }
Daniel Veillardc00cda82003-04-07 10:22:39 +0000302
303 return(ret);
304}
305
Daniel Veillard8d73bcb2003-08-04 01:06:15 +0000306/**
307 * xmlSplitQName3:
308 * @name: the full QName
309 * @len: an int *
310 *
311 * parse an XML qualified name string,i
312 *
313 * returns NULL if it is not a Qualified Name, otherwise, update len
314 * with the lenght in byte of the prefix and return a pointer
Daniel Veillard54f9a4f2005-09-03 13:28:24 +0000315 * to the start of the name without the prefix
Daniel Veillard8d73bcb2003-08-04 01:06:15 +0000316 */
317
318const xmlChar *
319xmlSplitQName3(const xmlChar *name, int *len) {
320 int l = 0;
321
322 if (name == NULL) return(NULL);
323 if (len == NULL) return(NULL);
324
325 /* nasty but valid */
326 if (name[0] == ':')
327 return(NULL);
328
329 /*
330 * we are not trying to validate but just to cut, and yes it will
331 * work even if this is as set of UTF-8 encoded chars
332 */
Daniel Veillardaa6de472008-08-25 14:53:31 +0000333 while ((name[l] != 0) && (name[l] != ':'))
Daniel Veillard8d73bcb2003-08-04 01:06:15 +0000334 l++;
Daniel Veillardaa6de472008-08-25 14:53:31 +0000335
Daniel Veillard8d73bcb2003-08-04 01:06:15 +0000336 if (name[l] == 0)
337 return(NULL);
338
339 *len = l;
340
341 return(&name[l+1]);
342}
343
Daniel Veillardc00cda82003-04-07 10:22:39 +0000344/************************************************************************
345 * *
Daniel Veillardd2298792003-02-14 16:54:11 +0000346 * Check Name, NCName and QName strings *
347 * *
348 ************************************************************************/
Daniel Veillardaa6de472008-08-25 14:53:31 +0000349
Daniel Veillardd2298792003-02-14 16:54:11 +0000350#define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
351
Daniel Veillardf1a27c62006-10-13 22:33:03 +0000352#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)
Daniel Veillardd2298792003-02-14 16:54:11 +0000353/**
354 * xmlValidateNCName:
355 * @value: the value to check
356 * @space: allow spaces in front and end of the string
357 *
358 * Check that a value conforms to the lexical space of NCName
359 *
360 * Returns 0 if this validates, a positive error code number otherwise
361 * and -1 in case of internal or API error.
362 */
363int
364xmlValidateNCName(const xmlChar *value, int space) {
365 const xmlChar *cur = value;
366 int c,l;
367
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000368 if (value == NULL)
369 return(-1);
370
Daniel Veillardd2298792003-02-14 16:54:11 +0000371 /*
372 * First quick algorithm for ASCII range
373 */
374 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000375 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +0000376 if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
377 (*cur == '_'))
378 cur++;
379 else
380 goto try_complex;
381 while (((*cur >= 'a') && (*cur <= 'z')) ||
382 ((*cur >= 'A') && (*cur <= 'Z')) ||
383 ((*cur >= '0') && (*cur <= '9')) ||
384 (*cur == '_') || (*cur == '-') || (*cur == '.'))
385 cur++;
386 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000387 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +0000388 if (*cur == 0)
389 return(0);
390
391try_complex:
392 /*
393 * Second check for chars outside the ASCII range
394 */
395 cur = value;
396 c = CUR_SCHAR(cur, l);
397 if (space) {
398 while (IS_BLANK(c)) {
399 cur += l;
400 c = CUR_SCHAR(cur, l);
401 }
402 }
William M. Brack871611b2003-10-18 04:53:14 +0000403 if ((!IS_LETTER(c)) && (c != '_'))
Daniel Veillardd2298792003-02-14 16:54:11 +0000404 return(1);
405 cur += l;
406 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +0000407 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
408 (c == '-') || (c == '_') || IS_COMBINING(c) ||
409 IS_EXTENDER(c)) {
Daniel Veillardd2298792003-02-14 16:54:11 +0000410 cur += l;
411 c = CUR_SCHAR(cur, l);
412 }
413 if (space) {
414 while (IS_BLANK(c)) {
415 cur += l;
416 c = CUR_SCHAR(cur, l);
417 }
418 }
419 if (c != 0)
420 return(1);
421
422 return(0);
423}
Daniel Veillard2156d432004-03-04 15:59:36 +0000424#endif
Daniel Veillardd2298792003-02-14 16:54:11 +0000425
Daniel Veillard2156d432004-03-04 15:59:36 +0000426#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Daniel Veillardd2298792003-02-14 16:54:11 +0000427/**
428 * xmlValidateQName:
429 * @value: the value to check
430 * @space: allow spaces in front and end of the string
431 *
432 * Check that a value conforms to the lexical space of QName
433 *
434 * Returns 0 if this validates, a positive error code number otherwise
435 * and -1 in case of internal or API error.
436 */
437int
438xmlValidateQName(const xmlChar *value, int space) {
439 const xmlChar *cur = value;
440 int c,l;
441
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000442 if (value == NULL)
443 return(-1);
Daniel Veillardd2298792003-02-14 16:54:11 +0000444 /*
445 * First quick algorithm for ASCII range
446 */
447 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000448 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +0000449 if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
450 (*cur == '_'))
451 cur++;
452 else
453 goto try_complex;
454 while (((*cur >= 'a') && (*cur <= 'z')) ||
455 ((*cur >= 'A') && (*cur <= 'Z')) ||
456 ((*cur >= '0') && (*cur <= '9')) ||
457 (*cur == '_') || (*cur == '-') || (*cur == '.'))
458 cur++;
459 if (*cur == ':') {
460 cur++;
461 if (((*cur >= 'a') && (*cur <= 'z')) ||
462 ((*cur >= 'A') && (*cur <= 'Z')) ||
463 (*cur == '_'))
464 cur++;
465 else
466 goto try_complex;
467 while (((*cur >= 'a') && (*cur <= 'z')) ||
468 ((*cur >= 'A') && (*cur <= 'Z')) ||
469 ((*cur >= '0') && (*cur <= '9')) ||
470 (*cur == '_') || (*cur == '-') || (*cur == '.'))
471 cur++;
472 }
473 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000474 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +0000475 if (*cur == 0)
476 return(0);
477
478try_complex:
479 /*
480 * Second check for chars outside the ASCII range
481 */
482 cur = value;
483 c = CUR_SCHAR(cur, l);
484 if (space) {
485 while (IS_BLANK(c)) {
486 cur += l;
487 c = CUR_SCHAR(cur, l);
488 }
489 }
William M. Brack871611b2003-10-18 04:53:14 +0000490 if ((!IS_LETTER(c)) && (c != '_'))
Daniel Veillardd2298792003-02-14 16:54:11 +0000491 return(1);
492 cur += l;
493 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +0000494 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
495 (c == '-') || (c == '_') || IS_COMBINING(c) ||
496 IS_EXTENDER(c)) {
Daniel Veillardd2298792003-02-14 16:54:11 +0000497 cur += l;
498 c = CUR_SCHAR(cur, l);
499 }
500 if (c == ':') {
501 cur += l;
502 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +0000503 if ((!IS_LETTER(c)) && (c != '_'))
Daniel Veillardd2298792003-02-14 16:54:11 +0000504 return(1);
505 cur += l;
506 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +0000507 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
508 (c == '-') || (c == '_') || IS_COMBINING(c) ||
509 IS_EXTENDER(c)) {
Daniel Veillardd2298792003-02-14 16:54:11 +0000510 cur += l;
511 c = CUR_SCHAR(cur, l);
512 }
513 }
514 if (space) {
515 while (IS_BLANK(c)) {
516 cur += l;
517 c = CUR_SCHAR(cur, l);
518 }
519 }
520 if (c != 0)
521 return(1);
522 return(0);
523}
524
525/**
526 * xmlValidateName:
527 * @value: the value to check
528 * @space: allow spaces in front and end of the string
529 *
530 * Check that a value conforms to the lexical space of Name
531 *
532 * Returns 0 if this validates, a positive error code number otherwise
533 * and -1 in case of internal or API error.
534 */
535int
536xmlValidateName(const xmlChar *value, int space) {
537 const xmlChar *cur = value;
538 int c,l;
539
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000540 if (value == NULL)
541 return(-1);
Daniel Veillardd2298792003-02-14 16:54:11 +0000542 /*
543 * First quick algorithm for ASCII range
544 */
545 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000546 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +0000547 if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
548 (*cur == '_') || (*cur == ':'))
549 cur++;
550 else
551 goto try_complex;
552 while (((*cur >= 'a') && (*cur <= 'z')) ||
553 ((*cur >= 'A') && (*cur <= 'Z')) ||
554 ((*cur >= '0') && (*cur <= '9')) ||
555 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
556 cur++;
557 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000558 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +0000559 if (*cur == 0)
560 return(0);
561
562try_complex:
563 /*
564 * Second check for chars outside the ASCII range
565 */
566 cur = value;
567 c = CUR_SCHAR(cur, l);
568 if (space) {
569 while (IS_BLANK(c)) {
570 cur += l;
571 c = CUR_SCHAR(cur, l);
572 }
573 }
William M. Brack871611b2003-10-18 04:53:14 +0000574 if ((!IS_LETTER(c)) && (c != '_') && (c != ':'))
Daniel Veillardd2298792003-02-14 16:54:11 +0000575 return(1);
576 cur += l;
577 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +0000578 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
579 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) {
Daniel Veillardd2298792003-02-14 16:54:11 +0000580 cur += l;
581 c = CUR_SCHAR(cur, l);
582 }
583 if (space) {
584 while (IS_BLANK(c)) {
585 cur += l;
586 c = CUR_SCHAR(cur, l);
587 }
588 }
589 if (c != 0)
590 return(1);
591 return(0);
592}
593
Daniel Veillardd4310742003-02-18 21:12:46 +0000594/**
595 * xmlValidateNMToken:
596 * @value: the value to check
597 * @space: allow spaces in front and end of the string
598 *
599 * Check that a value conforms to the lexical space of NMToken
600 *
601 * Returns 0 if this validates, a positive error code number otherwise
602 * and -1 in case of internal or API error.
603 */
604int
605xmlValidateNMToken(const xmlChar *value, int space) {
606 const xmlChar *cur = value;
607 int c,l;
608
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000609 if (value == NULL)
610 return(-1);
Daniel Veillardd4310742003-02-18 21:12:46 +0000611 /*
612 * First quick algorithm for ASCII range
613 */
614 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000615 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd4310742003-02-18 21:12:46 +0000616 if (((*cur >= 'a') && (*cur <= 'z')) ||
617 ((*cur >= 'A') && (*cur <= 'Z')) ||
618 ((*cur >= '0') && (*cur <= '9')) ||
619 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
620 cur++;
621 else
622 goto try_complex;
623 while (((*cur >= 'a') && (*cur <= 'z')) ||
624 ((*cur >= 'A') && (*cur <= 'Z')) ||
625 ((*cur >= '0') && (*cur <= '9')) ||
626 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
627 cur++;
628 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000629 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd4310742003-02-18 21:12:46 +0000630 if (*cur == 0)
631 return(0);
632
633try_complex:
634 /*
635 * Second check for chars outside the ASCII range
636 */
637 cur = value;
638 c = CUR_SCHAR(cur, l);
639 if (space) {
640 while (IS_BLANK(c)) {
641 cur += l;
642 c = CUR_SCHAR(cur, l);
643 }
644 }
William M. Brack871611b2003-10-18 04:53:14 +0000645 if (!(IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
646 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)))
Daniel Veillardd4310742003-02-18 21:12:46 +0000647 return(1);
648 cur += l;
649 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +0000650 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
651 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) {
Daniel Veillardd4310742003-02-18 21:12:46 +0000652 cur += l;
653 c = CUR_SCHAR(cur, l);
654 }
655 if (space) {
656 while (IS_BLANK(c)) {
657 cur += l;
658 c = CUR_SCHAR(cur, l);
659 }
660 }
661 if (c != 0)
662 return(1);
663 return(0);
664}
Daniel Veillard652327a2003-09-29 18:02:38 +0000665#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardd4310742003-02-18 21:12:46 +0000666
Daniel Veillardd2298792003-02-14 16:54:11 +0000667/************************************************************************
668 * *
Owen Taylor3473f882001-02-23 17:55:21 +0000669 * Allocation and deallocation of basic structures *
670 * *
671 ************************************************************************/
Daniel Veillardaa6de472008-08-25 14:53:31 +0000672
Owen Taylor3473f882001-02-23 17:55:21 +0000673/**
674 * xmlSetBufferAllocationScheme:
675 * @scheme: allocation method to use
Daniel Veillardaa6de472008-08-25 14:53:31 +0000676 *
Owen Taylor3473f882001-02-23 17:55:21 +0000677 * Set the buffer allocation method. Types are
678 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
Daniel Veillardaa6de472008-08-25 14:53:31 +0000679 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
Owen Taylor3473f882001-02-23 17:55:21 +0000680 * improves performance
681 */
682void
683xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme) {
Daniel Veillardda3fee42008-09-01 13:08:57 +0000684 if ((scheme == XML_BUFFER_ALLOC_EXACT) ||
Conrad Irwin7d0d2a52012-05-14 14:18:58 +0800685 (scheme == XML_BUFFER_ALLOC_DOUBLEIT) ||
686 (scheme == XML_BUFFER_ALLOC_HYBRID))
Daniel Veillardda3fee42008-09-01 13:08:57 +0000687 xmlBufferAllocScheme = scheme;
Owen Taylor3473f882001-02-23 17:55:21 +0000688}
689
690/**
691 * xmlGetBufferAllocationScheme:
692 *
693 * Types are
694 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
Daniel Veillardaa6de472008-08-25 14:53:31 +0000695 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
Owen Taylor3473f882001-02-23 17:55:21 +0000696 * improves performance
Conrad Irwin7d0d2a52012-05-14 14:18:58 +0800697 * XML_BUFFER_ALLOC_HYBRID - use exact sizes on small strings to keep memory usage tight
698 * in normal usage, and doubleit on large strings to avoid
699 * pathological performance.
Daniel Veillardaa6de472008-08-25 14:53:31 +0000700 *
Owen Taylor3473f882001-02-23 17:55:21 +0000701 * Returns the current allocation scheme
702 */
703xmlBufferAllocationScheme
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000704xmlGetBufferAllocationScheme(void) {
Daniel Veillarde043ee12001-04-16 14:08:07 +0000705 return(xmlBufferAllocScheme);
Owen Taylor3473f882001-02-23 17:55:21 +0000706}
707
708/**
709 * xmlNewNs:
710 * @node: the element carrying the namespace
711 * @href: the URI associated
712 * @prefix: the prefix for the namespace
713 *
714 * Creation of a new Namespace. This function will refuse to create
715 * a namespace with a similar prefix than an existing one present on this
716 * node.
717 * We use href==NULL in the case of an element creation where the namespace
718 * was not defined.
Daniel Veillardd1640922001-12-17 15:30:10 +0000719 * Returns a new namespace pointer or NULL
Owen Taylor3473f882001-02-23 17:55:21 +0000720 */
721xmlNsPtr
722xmlNewNs(xmlNodePtr node, const xmlChar *href, const xmlChar *prefix) {
723 xmlNsPtr cur;
724
725 if ((node != NULL) && (node->type != XML_ELEMENT_NODE))
726 return(NULL);
727
Daniel Veillardaa54d372010-09-09 18:17:47 +0200728 if ((prefix != NULL) && (xmlStrEqual(prefix, BAD_CAST "xml"))) {
729 /* xml namespace is predefined, no need to add it */
730 if (xmlStrEqual(href, XML_XML_NAMESPACE))
731 return(NULL);
732
733 /*
734 * Problem, this is an attempt to bind xml prefix to a wrong
735 * namespace, which breaks
736 * Namespace constraint: Reserved Prefixes and Namespace Names
737 * from XML namespace. But documents authors may not care in
738 * their context so let's proceed.
739 */
740 }
Daniel Veillard20ee8c02001-10-05 09:18:14 +0000741
Owen Taylor3473f882001-02-23 17:55:21 +0000742 /*
743 * Allocate a new Namespace and fill the fields.
744 */
745 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
746 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000747 xmlTreeErrMemory("building namespace");
Owen Taylor3473f882001-02-23 17:55:21 +0000748 return(NULL);
749 }
750 memset(cur, 0, sizeof(xmlNs));
751 cur->type = XML_LOCAL_NAMESPACE;
752
753 if (href != NULL)
Daniel Veillardaa6de472008-08-25 14:53:31 +0000754 cur->href = xmlStrdup(href);
Owen Taylor3473f882001-02-23 17:55:21 +0000755 if (prefix != NULL)
Daniel Veillardaa6de472008-08-25 14:53:31 +0000756 cur->prefix = xmlStrdup(prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000757
758 /*
759 * Add it at the end to preserve parsing order ...
760 * and checks for existing use of the prefix
761 */
762 if (node != NULL) {
763 if (node->nsDef == NULL) {
764 node->nsDef = cur;
765 } else {
766 xmlNsPtr prev = node->nsDef;
767
768 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
769 (xmlStrEqual(prev->prefix, cur->prefix))) {
770 xmlFreeNs(cur);
771 return(NULL);
Daniel Veillardaa6de472008-08-25 14:53:31 +0000772 }
Owen Taylor3473f882001-02-23 17:55:21 +0000773 while (prev->next != NULL) {
774 prev = prev->next;
775 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
776 (xmlStrEqual(prev->prefix, cur->prefix))) {
777 xmlFreeNs(cur);
778 return(NULL);
Daniel Veillardaa6de472008-08-25 14:53:31 +0000779 }
Owen Taylor3473f882001-02-23 17:55:21 +0000780 }
781 prev->next = cur;
782 }
783 }
784 return(cur);
785}
786
787/**
788 * xmlSetNs:
789 * @node: a node in the document
790 * @ns: a namespace pointer
791 *
792 * Associate a namespace to a node, a posteriori.
793 */
794void
795xmlSetNs(xmlNodePtr node, xmlNsPtr ns) {
796 if (node == NULL) {
797#ifdef DEBUG_TREE
798 xmlGenericError(xmlGenericErrorContext,
799 "xmlSetNs: node == NULL\n");
800#endif
801 return;
802 }
803 node->ns = ns;
804}
805
806/**
807 * xmlFreeNs:
808 * @cur: the namespace pointer
809 *
810 * Free up the structures associated to a namespace
811 */
812void
813xmlFreeNs(xmlNsPtr cur) {
814 if (cur == NULL) {
815#ifdef DEBUG_TREE
816 xmlGenericError(xmlGenericErrorContext,
817 "xmlFreeNs : ns == NULL\n");
818#endif
819 return;
820 }
821 if (cur->href != NULL) xmlFree((char *) cur->href);
822 if (cur->prefix != NULL) xmlFree((char *) cur->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000823 xmlFree(cur);
824}
825
826/**
827 * xmlFreeNsList:
828 * @cur: the first namespace pointer
829 *
830 * Free up all the structures associated to the chained namespaces.
831 */
832void
833xmlFreeNsList(xmlNsPtr cur) {
834 xmlNsPtr next;
835 if (cur == NULL) {
836#ifdef DEBUG_TREE
837 xmlGenericError(xmlGenericErrorContext,
838 "xmlFreeNsList : ns == NULL\n");
839#endif
840 return;
841 }
842 while (cur != NULL) {
843 next = cur->next;
844 xmlFreeNs(cur);
845 cur = next;
846 }
847}
848
849/**
850 * xmlNewDtd:
851 * @doc: the document pointer
852 * @name: the DTD name
853 * @ExternalID: the external ID
854 * @SystemID: the system ID
855 *
856 * Creation of a new DTD for the external subset. To create an
857 * internal subset, use xmlCreateIntSubset().
858 *
859 * Returns a pointer to the new DTD structure
860 */
861xmlDtdPtr
862xmlNewDtd(xmlDocPtr doc, const xmlChar *name,
863 const xmlChar *ExternalID, const xmlChar *SystemID) {
864 xmlDtdPtr cur;
865
866 if ((doc != NULL) && (doc->extSubset != NULL)) {
867#ifdef DEBUG_TREE
868 xmlGenericError(xmlGenericErrorContext,
869 "xmlNewDtd(%s): document %s already have a DTD %s\n",
870 /* !!! */ (char *) name, doc->name,
871 /* !!! */ (char *)doc->extSubset->name);
872#endif
873 return(NULL);
874 }
875
876 /*
877 * Allocate a new DTD and fill the fields.
878 */
879 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
880 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000881 xmlTreeErrMemory("building DTD");
Owen Taylor3473f882001-02-23 17:55:21 +0000882 return(NULL);
883 }
884 memset(cur, 0 , sizeof(xmlDtd));
885 cur->type = XML_DTD_NODE;
886
887 if (name != NULL)
Daniel Veillardaa6de472008-08-25 14:53:31 +0000888 cur->name = xmlStrdup(name);
Owen Taylor3473f882001-02-23 17:55:21 +0000889 if (ExternalID != NULL)
Daniel Veillardaa6de472008-08-25 14:53:31 +0000890 cur->ExternalID = xmlStrdup(ExternalID);
Owen Taylor3473f882001-02-23 17:55:21 +0000891 if (SystemID != NULL)
Daniel Veillardaa6de472008-08-25 14:53:31 +0000892 cur->SystemID = xmlStrdup(SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +0000893 if (doc != NULL)
894 doc->extSubset = cur;
895 cur->doc = doc;
896
Daniel Veillarda880b122003-04-21 21:36:41 +0000897 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +0000898 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000899 return(cur);
900}
901
902/**
903 * xmlGetIntSubset:
904 * @doc: the document pointer
905 *
906 * Get the internal subset of a document
907 * Returns a pointer to the DTD structure or NULL if not found
908 */
909
910xmlDtdPtr
911xmlGetIntSubset(xmlDocPtr doc) {
912 xmlNodePtr cur;
913
914 if (doc == NULL)
915 return(NULL);
916 cur = doc->children;
917 while (cur != NULL) {
918 if (cur->type == XML_DTD_NODE)
919 return((xmlDtdPtr) cur);
920 cur = cur->next;
921 }
922 return((xmlDtdPtr) doc->intSubset);
923}
924
925/**
926 * xmlCreateIntSubset:
927 * @doc: the document pointer
928 * @name: the DTD name
Daniel Veillarde356c282001-03-10 12:32:04 +0000929 * @ExternalID: the external (PUBLIC) ID
Owen Taylor3473f882001-02-23 17:55:21 +0000930 * @SystemID: the system ID
931 *
932 * Create the internal subset of a document
933 * Returns a pointer to the new DTD structure
934 */
935xmlDtdPtr
936xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name,
937 const xmlChar *ExternalID, const xmlChar *SystemID) {
938 xmlDtdPtr cur;
939
940 if ((doc != NULL) && (xmlGetIntSubset(doc) != NULL)) {
941#ifdef DEBUG_TREE
942 xmlGenericError(xmlGenericErrorContext,
943
944 "xmlCreateIntSubset(): document %s already have an internal subset\n",
945 doc->name);
946#endif
947 return(NULL);
948 }
949
950 /*
951 * Allocate a new DTD and fill the fields.
952 */
953 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
954 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000955 xmlTreeErrMemory("building internal subset");
Owen Taylor3473f882001-02-23 17:55:21 +0000956 return(NULL);
957 }
958 memset(cur, 0, sizeof(xmlDtd));
959 cur->type = XML_DTD_NODE;
960
William M. Bracka3215c72004-07-31 16:24:01 +0000961 if (name != NULL) {
962 cur->name = xmlStrdup(name);
963 if (cur->name == NULL) {
964 xmlTreeErrMemory("building internal subset");
965 xmlFree(cur);
966 return(NULL);
967 }
968 }
969 if (ExternalID != NULL) {
Daniel Veillardaa6de472008-08-25 14:53:31 +0000970 cur->ExternalID = xmlStrdup(ExternalID);
William M. Bracka3215c72004-07-31 16:24:01 +0000971 if (cur->ExternalID == NULL) {
972 xmlTreeErrMemory("building internal subset");
973 if (cur->name != NULL)
974 xmlFree((char *)cur->name);
975 xmlFree(cur);
976 return(NULL);
977 }
978 }
979 if (SystemID != NULL) {
Daniel Veillardaa6de472008-08-25 14:53:31 +0000980 cur->SystemID = xmlStrdup(SystemID);
William M. Bracka3215c72004-07-31 16:24:01 +0000981 if (cur->SystemID == NULL) {
982 xmlTreeErrMemory("building internal subset");
983 if (cur->name != NULL)
984 xmlFree((char *)cur->name);
985 if (cur->ExternalID != NULL)
986 xmlFree((char *)cur->ExternalID);
987 xmlFree(cur);
988 return(NULL);
989 }
990 }
Owen Taylor3473f882001-02-23 17:55:21 +0000991 if (doc != NULL) {
992 doc->intSubset = cur;
993 cur->parent = doc;
994 cur->doc = doc;
995 if (doc->children == NULL) {
996 doc->children = (xmlNodePtr) cur;
997 doc->last = (xmlNodePtr) cur;
998 } else {
Owen Taylor3473f882001-02-23 17:55:21 +0000999 if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillarde356c282001-03-10 12:32:04 +00001000 xmlNodePtr prev;
1001
Owen Taylor3473f882001-02-23 17:55:21 +00001002 prev = doc->children;
1003 prev->prev = (xmlNodePtr) cur;
1004 cur->next = prev;
1005 doc->children = (xmlNodePtr) cur;
1006 } else {
Daniel Veillarde356c282001-03-10 12:32:04 +00001007 xmlNodePtr next;
1008
1009 next = doc->children;
1010 while ((next != NULL) && (next->type != XML_ELEMENT_NODE))
1011 next = next->next;
1012 if (next == NULL) {
1013 cur->prev = doc->last;
1014 cur->prev->next = (xmlNodePtr) cur;
1015 cur->next = NULL;
1016 doc->last = (xmlNodePtr) cur;
1017 } else {
1018 cur->next = next;
1019 cur->prev = next->prev;
1020 if (cur->prev == NULL)
1021 doc->children = (xmlNodePtr) cur;
1022 else
1023 cur->prev->next = (xmlNodePtr) cur;
1024 next->prev = (xmlNodePtr) cur;
1025 }
Owen Taylor3473f882001-02-23 17:55:21 +00001026 }
1027 }
1028 }
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001029
Daniel Veillarda880b122003-04-21 21:36:41 +00001030 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001031 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001032 return(cur);
1033}
1034
1035/**
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001036 * DICT_FREE:
1037 * @str: a string
1038 *
1039 * Free a string if it is not owned by the "dict" dictionnary in the
1040 * current scope
1041 */
1042#define DICT_FREE(str) \
Daniel Veillardaa6de472008-08-25 14:53:31 +00001043 if ((str) && ((!dict) || \
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001044 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
1045 xmlFree((char *)(str));
1046
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00001047
1048/**
1049 * DICT_COPY:
1050 * @str: a string
1051 *
1052 * Copy a string using a "dict" dictionnary in the current scope,
1053 * if availabe.
1054 */
1055#define DICT_COPY(str, cpy) \
1056 if (str) { \
1057 if (dict) { \
1058 if (xmlDictOwns(dict, (const xmlChar *)(str))) \
1059 cpy = (xmlChar *) (str); \
1060 else \
1061 cpy = (xmlChar *) xmlDictLookup((dict), (const xmlChar *)(str), -1); \
1062 } else \
1063 cpy = xmlStrdup((const xmlChar *)(str)); }
1064
1065/**
1066 * DICT_CONST_COPY:
1067 * @str: a string
1068 *
1069 * Copy a string using a "dict" dictionnary in the current scope,
1070 * if availabe.
1071 */
1072#define DICT_CONST_COPY(str, cpy) \
1073 if (str) { \
1074 if (dict) { \
1075 if (xmlDictOwns(dict, (const xmlChar *)(str))) \
1076 cpy = (const xmlChar *) (str); \
1077 else \
1078 cpy = xmlDictLookup((dict), (const xmlChar *)(str), -1); \
1079 } else \
1080 cpy = (const xmlChar *) xmlStrdup((const xmlChar *)(str)); }
1081
1082
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001083/**
Owen Taylor3473f882001-02-23 17:55:21 +00001084 * xmlFreeDtd:
1085 * @cur: the DTD structure to free up
1086 *
1087 * Free a DTD structure.
1088 */
1089void
1090xmlFreeDtd(xmlDtdPtr cur) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001091 xmlDictPtr dict = NULL;
1092
Owen Taylor3473f882001-02-23 17:55:21 +00001093 if (cur == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001094 return;
1095 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001096 if (cur->doc != NULL) dict = cur->doc->dict;
Daniel Veillard5335dc52003-01-01 20:59:38 +00001097
Daniel Veillarda880b122003-04-21 21:36:41 +00001098 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001099 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
1100
Owen Taylor3473f882001-02-23 17:55:21 +00001101 if (cur->children != NULL) {
1102 xmlNodePtr next, c = cur->children;
1103
1104 /*
William M. Brack18a04f22004-08-03 16:42:37 +00001105 * Cleanup all nodes which are not part of the specific lists
1106 * of notations, elements, attributes and entities.
Owen Taylor3473f882001-02-23 17:55:21 +00001107 */
1108 while (c != NULL) {
1109 next = c->next;
William M. Brack18a04f22004-08-03 16:42:37 +00001110 if ((c->type != XML_NOTATION_NODE) &&
1111 (c->type != XML_ELEMENT_DECL) &&
1112 (c->type != XML_ATTRIBUTE_DECL) &&
1113 (c->type != XML_ENTITY_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001114 xmlUnlinkNode(c);
1115 xmlFreeNode(c);
1116 }
1117 c = next;
1118 }
1119 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001120 DICT_FREE(cur->name)
1121 DICT_FREE(cur->SystemID)
1122 DICT_FREE(cur->ExternalID)
Owen Taylor3473f882001-02-23 17:55:21 +00001123 /* TODO !!! */
1124 if (cur->notations != NULL)
1125 xmlFreeNotationTable((xmlNotationTablePtr) cur->notations);
Daniel Veillardaa6de472008-08-25 14:53:31 +00001126
Owen Taylor3473f882001-02-23 17:55:21 +00001127 if (cur->elements != NULL)
1128 xmlFreeElementTable((xmlElementTablePtr) cur->elements);
1129 if (cur->attributes != NULL)
1130 xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes);
1131 if (cur->entities != NULL)
1132 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities);
1133 if (cur->pentities != NULL)
1134 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->pentities);
1135
Owen Taylor3473f882001-02-23 17:55:21 +00001136 xmlFree(cur);
1137}
1138
1139/**
1140 * xmlNewDoc:
1141 * @version: xmlChar string giving the version of XML "1.0"
1142 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001143 * Creates a new XML document
1144 *
Owen Taylor3473f882001-02-23 17:55:21 +00001145 * Returns a new document
1146 */
1147xmlDocPtr
1148xmlNewDoc(const xmlChar *version) {
1149 xmlDocPtr cur;
1150
1151 if (version == NULL)
1152 version = (const xmlChar *) "1.0";
1153
1154 /*
1155 * Allocate a new document and fill the fields.
1156 */
1157 cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
1158 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001159 xmlTreeErrMemory("building doc");
Owen Taylor3473f882001-02-23 17:55:21 +00001160 return(NULL);
1161 }
1162 memset(cur, 0, sizeof(xmlDoc));
1163 cur->type = XML_DOCUMENT_NODE;
1164
Daniel Veillardaa6de472008-08-25 14:53:31 +00001165 cur->version = xmlStrdup(version);
William M. Bracka3215c72004-07-31 16:24:01 +00001166 if (cur->version == NULL) {
1167 xmlTreeErrMemory("building doc");
1168 xmlFree(cur);
Daniel Veillardae0765b2008-07-31 19:54:59 +00001169 return(NULL);
William M. Bracka3215c72004-07-31 16:24:01 +00001170 }
Owen Taylor3473f882001-02-23 17:55:21 +00001171 cur->standalone = -1;
1172 cur->compression = -1; /* not initialized */
1173 cur->doc = cur;
Daniel Veillardae0765b2008-07-31 19:54:59 +00001174 cur->parseFlags = 0;
1175 cur->properties = XML_DOC_USERBUILT;
Daniel Veillarda6874ca2003-07-29 16:47:24 +00001176 /*
1177 * The in memory encoding is always UTF8
1178 * This field will never change and would
1179 * be obsolete if not for binary compatibility.
1180 */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00001181 cur->charset = XML_CHAR_ENCODING_UTF8;
Daniel Veillard5335dc52003-01-01 20:59:38 +00001182
Daniel Veillarda880b122003-04-21 21:36:41 +00001183 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001184 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001185 return(cur);
1186}
1187
1188/**
1189 * xmlFreeDoc:
1190 * @cur: pointer to the document
Owen Taylor3473f882001-02-23 17:55:21 +00001191 *
1192 * Free up all the structures used by a document, tree included.
1193 */
1194void
1195xmlFreeDoc(xmlDocPtr cur) {
Daniel Veillarda9142e72001-06-19 11:07:54 +00001196 xmlDtdPtr extSubset, intSubset;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001197 xmlDictPtr dict = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001198
Owen Taylor3473f882001-02-23 17:55:21 +00001199 if (cur == NULL) {
1200#ifdef DEBUG_TREE
1201 xmlGenericError(xmlGenericErrorContext,
1202 "xmlFreeDoc : document == NULL\n");
1203#endif
1204 return;
1205 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001206#ifdef LIBXML_DEBUG_RUNTIME
Daniel Veillardbca3ad22005-08-23 22:14:02 +00001207#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001208 xmlDebugCheckDocument(stderr, cur);
1209#endif
Daniel Veillardbca3ad22005-08-23 22:14:02 +00001210#endif
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001211
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001212 if (cur != NULL) dict = cur->dict;
Daniel Veillard5335dc52003-01-01 20:59:38 +00001213
Daniel Veillarda880b122003-04-21 21:36:41 +00001214 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001215 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
1216
Daniel Veillard76d66f42001-05-16 21:05:17 +00001217 /*
1218 * Do this before freeing the children list to avoid ID lookups
1219 */
1220 if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids);
1221 cur->ids = NULL;
1222 if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
1223 cur->refs = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001224 extSubset = cur->extSubset;
1225 intSubset = cur->intSubset;
Daniel Veillard5997aca2002-03-18 18:36:20 +00001226 if (intSubset == extSubset)
1227 extSubset = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001228 if (extSubset != NULL) {
Daniel Veillard76d66f42001-05-16 21:05:17 +00001229 xmlUnlinkNode((xmlNodePtr) cur->extSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001230 cur->extSubset = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001231 xmlFreeDtd(extSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001232 }
Daniel Veillarda9142e72001-06-19 11:07:54 +00001233 if (intSubset != NULL) {
Daniel Veillard76d66f42001-05-16 21:05:17 +00001234 xmlUnlinkNode((xmlNodePtr) cur->intSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001235 cur->intSubset = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001236 xmlFreeDtd(intSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001237 }
1238
1239 if (cur->children != NULL) xmlFreeNodeList(cur->children);
Owen Taylor3473f882001-02-23 17:55:21 +00001240 if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001241
1242 DICT_FREE(cur->version)
1243 DICT_FREE(cur->name)
1244 DICT_FREE(cur->encoding)
1245 DICT_FREE(cur->URL)
Owen Taylor3473f882001-02-23 17:55:21 +00001246 xmlFree(cur);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001247 if (dict) xmlDictFree(dict);
Owen Taylor3473f882001-02-23 17:55:21 +00001248}
1249
1250/**
1251 * xmlStringLenGetNodeList:
1252 * @doc: the document
1253 * @value: the value of the text
1254 * @len: the length of the string value
1255 *
1256 * Parse the value string and build the node list associated. Should
1257 * produce a flat tree with only TEXTs and ENTITY_REFs.
1258 * Returns a pointer to the first child
1259 */
1260xmlNodePtr
1261xmlStringLenGetNodeList(xmlDocPtr doc, const xmlChar *value, int len) {
1262 xmlNodePtr ret = NULL, last = NULL;
1263 xmlNodePtr node;
1264 xmlChar *val;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001265 const xmlChar *cur = value, *end = cur + len;
Owen Taylor3473f882001-02-23 17:55:21 +00001266 const xmlChar *q;
1267 xmlEntityPtr ent;
Conrad Irwin7d553f82012-05-10 20:17:25 -07001268 xmlBufferPtr buffer;
Owen Taylor3473f882001-02-23 17:55:21 +00001269
1270 if (value == NULL) return(NULL);
1271
Conrad Irwin7d553f82012-05-10 20:17:25 -07001272 buffer = xmlBufferCreateSize(0);
1273 if (buffer == NULL) return(NULL);
Conrad Irwin7d0d2a52012-05-14 14:18:58 +08001274 xmlBufferSetAllocationScheme(buffer, XML_BUFFER_ALLOC_HYBRID);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001275
Owen Taylor3473f882001-02-23 17:55:21 +00001276 q = cur;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001277 while ((cur < end) && (*cur != 0)) {
1278 if (cur[0] == '&') {
1279 int charval = 0;
1280 xmlChar tmp;
1281
Owen Taylor3473f882001-02-23 17:55:21 +00001282 /*
1283 * Save the current text.
1284 */
1285 if (cur != q) {
Conrad Irwin7d553f82012-05-10 20:17:25 -07001286 if (xmlBufferAdd(buffer, q, cur - q))
1287 goto out;
Owen Taylor3473f882001-02-23 17:55:21 +00001288 }
Owen Taylor3473f882001-02-23 17:55:21 +00001289 q = cur;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001290 if ((cur + 2 < end) && (cur[1] == '#') && (cur[2] == 'x')) {
1291 cur += 3;
1292 if (cur < end)
1293 tmp = *cur;
1294 else
1295 tmp = 0;
1296 while (tmp != ';') { /* Non input consuming loop */
Daniel Veillardaa6de472008-08-25 14:53:31 +00001297 if ((tmp >= '0') && (tmp <= '9'))
Daniel Veillard07cb8222003-09-10 10:51:05 +00001298 charval = charval * 16 + (tmp - '0');
1299 else if ((tmp >= 'a') && (tmp <= 'f'))
1300 charval = charval * 16 + (tmp - 'a') + 10;
1301 else if ((tmp >= 'A') && (tmp <= 'F'))
1302 charval = charval * 16 + (tmp - 'A') + 10;
Owen Taylor3473f882001-02-23 17:55:21 +00001303 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001304 xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
1305 NULL);
Daniel Veillard07cb8222003-09-10 10:51:05 +00001306 charval = 0;
1307 break;
1308 }
1309 cur++;
1310 if (cur < end)
1311 tmp = *cur;
1312 else
1313 tmp = 0;
1314 }
1315 if (tmp == ';')
1316 cur++;
1317 q = cur;
1318 } else if ((cur + 1 < end) && (cur[1] == '#')) {
1319 cur += 2;
1320 if (cur < end)
1321 tmp = *cur;
1322 else
1323 tmp = 0;
1324 while (tmp != ';') { /* Non input consuming loops */
Daniel Veillardaa6de472008-08-25 14:53:31 +00001325 if ((tmp >= '0') && (tmp <= '9'))
Daniel Veillard07cb8222003-09-10 10:51:05 +00001326 charval = charval * 10 + (tmp - '0');
1327 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001328 xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
1329 NULL);
Daniel Veillard07cb8222003-09-10 10:51:05 +00001330 charval = 0;
1331 break;
1332 }
1333 cur++;
1334 if (cur < end)
1335 tmp = *cur;
1336 else
1337 tmp = 0;
1338 }
1339 if (tmp == ';')
1340 cur++;
1341 q = cur;
1342 } else {
1343 /*
1344 * Read the entity string
1345 */
1346 cur++;
1347 q = cur;
1348 while ((cur < end) && (*cur != 0) && (*cur != ';')) cur++;
1349 if ((cur >= end) || (*cur == 0)) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001350 xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY, (xmlNodePtr) doc,
1351 (const char *) q);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001352 goto out;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001353 }
1354 if (cur != q) {
1355 /*
1356 * Predefined entities don't generate nodes
1357 */
1358 val = xmlStrndup(q, cur - q);
1359 ent = xmlGetDocEntity(doc, val);
1360 if ((ent != NULL) &&
1361 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
Conrad Irwin7d553f82012-05-10 20:17:25 -07001362
1363 if (xmlBufferCat(buffer, ent->content))
1364 goto out;
Daniel Veillardaa6de472008-08-25 14:53:31 +00001365
Daniel Veillard07cb8222003-09-10 10:51:05 +00001366 } else {
1367 /*
Conrad Irwin7d553f82012-05-10 20:17:25 -07001368 * Flush buffer so far
1369 */
1370 if (buffer->use) {
1371 node = xmlNewDocText(doc, NULL);
1372 if (node == NULL) {
1373 if (val != NULL) xmlFree(val);
1374 goto out;
1375 }
1376 node->content = xmlBufferDetach(buffer);
1377
1378 if (last == NULL) {
1379 last = ret = node;
1380 } else {
1381 last = xmlAddNextSibling(last, node);
1382 }
1383 }
1384
1385 /*
Daniel Veillard07cb8222003-09-10 10:51:05 +00001386 * Create a new REFERENCE_REF node
1387 */
1388 node = xmlNewReference(doc, val);
1389 if (node == NULL) {
1390 if (val != NULL) xmlFree(val);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001391 goto out;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001392 }
1393 else if ((ent != NULL) && (ent->children == NULL)) {
1394 xmlNodePtr temp;
1395
1396 ent->children = xmlStringGetNodeList(doc,
1397 (const xmlChar*)node->content);
1398 ent->owner = 1;
1399 temp = ent->children;
1400 while (temp) {
1401 temp->parent = (xmlNodePtr)ent;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001402 ent->last = temp;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001403 temp = temp->next;
1404 }
1405 }
1406 if (last == NULL) {
1407 last = ret = node;
1408 } else {
1409 last = xmlAddNextSibling(last, node);
1410 }
1411 }
1412 xmlFree(val);
1413 }
1414 cur++;
1415 q = cur;
1416 }
1417 if (charval != 0) {
1418 xmlChar buf[10];
1419 int l;
1420
1421 l = xmlCopyCharMultiByte(buf, charval);
1422 buf[l] = 0;
Conrad Irwin7d553f82012-05-10 20:17:25 -07001423
1424 if (xmlBufferCat(buffer, buf))
1425 goto out;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001426 charval = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001427 }
Daniel Veillard07cb8222003-09-10 10:51:05 +00001428 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001429 cur++;
1430 }
Conrad Irwin7d553f82012-05-10 20:17:25 -07001431
1432 if (cur != q) {
Owen Taylor3473f882001-02-23 17:55:21 +00001433 /*
1434 * Handle the last piece of text.
1435 */
Conrad Irwin7d553f82012-05-10 20:17:25 -07001436 if (xmlBufferAdd(buffer, q, cur - q))
1437 goto out;
Owen Taylor3473f882001-02-23 17:55:21 +00001438 }
Conrad Irwin7d553f82012-05-10 20:17:25 -07001439
1440 if (buffer->use) {
1441 node = xmlNewDocText(doc, NULL);
1442 if (node == NULL) goto out;
1443 node->content = xmlBufferDetach(buffer);
1444
1445 if (last == NULL) {
1446 last = ret = node;
1447 } else {
1448 last = xmlAddNextSibling(last, node);
1449 }
1450 } else if (ret == NULL) {
1451 ret = xmlNewDocText(doc, BAD_CAST "");
1452 }
1453
1454out:
1455 xmlBufferFree(buffer);
Owen Taylor3473f882001-02-23 17:55:21 +00001456 return(ret);
1457}
1458
1459/**
1460 * xmlStringGetNodeList:
1461 * @doc: the document
1462 * @value: the value of the attribute
1463 *
1464 * Parse the value string and build the node list associated. Should
1465 * produce a flat tree with only TEXTs and ENTITY_REFs.
1466 * Returns a pointer to the first child
1467 */
1468xmlNodePtr
1469xmlStringGetNodeList(xmlDocPtr doc, const xmlChar *value) {
1470 xmlNodePtr ret = NULL, last = NULL;
1471 xmlNodePtr node;
1472 xmlChar *val;
1473 const xmlChar *cur = value;
1474 const xmlChar *q;
1475 xmlEntityPtr ent;
Conrad Irwin7d553f82012-05-10 20:17:25 -07001476 xmlBufferPtr buffer;
Owen Taylor3473f882001-02-23 17:55:21 +00001477
1478 if (value == NULL) return(NULL);
1479
Conrad Irwin7d553f82012-05-10 20:17:25 -07001480 buffer = xmlBufferCreateSize(0);
1481 if (buffer == NULL) return(NULL);
Conrad Irwin7d0d2a52012-05-14 14:18:58 +08001482 xmlBufferSetAllocationScheme(buffer, XML_BUFFER_ALLOC_HYBRID);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001483
Owen Taylor3473f882001-02-23 17:55:21 +00001484 q = cur;
1485 while (*cur != 0) {
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001486 if (cur[0] == '&') {
1487 int charval = 0;
1488 xmlChar tmp;
1489
Owen Taylor3473f882001-02-23 17:55:21 +00001490 /*
1491 * Save the current text.
1492 */
1493 if (cur != q) {
Conrad Irwin7d553f82012-05-10 20:17:25 -07001494 if (xmlBufferAdd(buffer, q, cur - q))
1495 goto out;
Owen Taylor3473f882001-02-23 17:55:21 +00001496 }
Owen Taylor3473f882001-02-23 17:55:21 +00001497 q = cur;
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001498 if ((cur[1] == '#') && (cur[2] == 'x')) {
1499 cur += 3;
1500 tmp = *cur;
1501 while (tmp != ';') { /* Non input consuming loop */
Daniel Veillardaa6de472008-08-25 14:53:31 +00001502 if ((tmp >= '0') && (tmp <= '9'))
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001503 charval = charval * 16 + (tmp - '0');
1504 else if ((tmp >= 'a') && (tmp <= 'f'))
1505 charval = charval * 16 + (tmp - 'a') + 10;
1506 else if ((tmp >= 'A') && (tmp <= 'F'))
1507 charval = charval * 16 + (tmp - 'A') + 10;
Owen Taylor3473f882001-02-23 17:55:21 +00001508 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001509 xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
1510 NULL);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001511 charval = 0;
1512 break;
1513 }
1514 cur++;
1515 tmp = *cur;
1516 }
1517 if (tmp == ';')
1518 cur++;
1519 q = cur;
1520 } else if (cur[1] == '#') {
1521 cur += 2;
1522 tmp = *cur;
1523 while (tmp != ';') { /* Non input consuming loops */
Daniel Veillardaa6de472008-08-25 14:53:31 +00001524 if ((tmp >= '0') && (tmp <= '9'))
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001525 charval = charval * 10 + (tmp - '0');
1526 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001527 xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
1528 NULL);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001529 charval = 0;
1530 break;
1531 }
1532 cur++;
1533 tmp = *cur;
1534 }
1535 if (tmp == ';')
1536 cur++;
1537 q = cur;
1538 } else {
1539 /*
1540 * Read the entity string
1541 */
1542 cur++;
1543 q = cur;
1544 while ((*cur != 0) && (*cur != ';')) cur++;
1545 if (*cur == 0) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001546 xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY,
1547 (xmlNodePtr) doc, (const char *) q);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001548 goto out;
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001549 }
1550 if (cur != q) {
1551 /*
1552 * Predefined entities don't generate nodes
1553 */
1554 val = xmlStrndup(q, cur - q);
1555 ent = xmlGetDocEntity(doc, val);
1556 if ((ent != NULL) &&
1557 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
Conrad Irwin7d553f82012-05-10 20:17:25 -07001558
1559 if (xmlBufferCat(buffer, ent->content))
1560 goto out;
Daniel Veillardaa6de472008-08-25 14:53:31 +00001561
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001562 } else {
1563 /*
Conrad Irwin7d553f82012-05-10 20:17:25 -07001564 * Flush buffer so far
1565 */
1566 if (buffer->use) {
1567 node = xmlNewDocText(doc, NULL);
1568 node->content = xmlBufferDetach(buffer);
1569
1570 if (last == NULL) {
1571 last = ret = node;
1572 } else {
1573 last = xmlAddNextSibling(last, node);
1574 }
1575 }
1576
1577 /*
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001578 * Create a new REFERENCE_REF node
1579 */
1580 node = xmlNewReference(doc, val);
1581 if (node == NULL) {
1582 if (val != NULL) xmlFree(val);
Conrad Irwin7d553f82012-05-10 20:17:25 -07001583 goto out;
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001584 }
Daniel Veillardbf8dae82002-04-18 16:39:10 +00001585 else if ((ent != NULL) && (ent->children == NULL)) {
1586 xmlNodePtr temp;
1587
1588 ent->children = xmlStringGetNodeList(doc,
1589 (const xmlChar*)node->content);
Daniel Veillard2d84a892002-12-30 00:01:08 +00001590 ent->owner = 1;
Daniel Veillardbf8dae82002-04-18 16:39:10 +00001591 temp = ent->children;
1592 while (temp) {
1593 temp->parent = (xmlNodePtr)ent;
1594 temp = temp->next;
1595 }
1596 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001597 if (last == NULL) {
1598 last = ret = node;
1599 } else {
1600 last = xmlAddNextSibling(last, node);
1601 }
1602 }
1603 xmlFree(val);
1604 }
1605 cur++;
1606 q = cur;
1607 }
1608 if (charval != 0) {
1609 xmlChar buf[10];
1610 int len;
1611
1612 len = xmlCopyCharMultiByte(buf, charval);
1613 buf[len] = 0;
Conrad Irwin7d553f82012-05-10 20:17:25 -07001614
1615 if (xmlBufferCat(buffer, buf))
1616 goto out;
1617 charval = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001618 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001619 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001620 cur++;
1621 }
Daniel Veillard75bea542001-05-11 17:41:21 +00001622 if ((cur != q) || (ret == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001623 /*
1624 * Handle the last piece of text.
1625 */
Conrad Irwin7d553f82012-05-10 20:17:25 -07001626 xmlBufferAdd(buffer, q, cur - q);
1627 }
1628
1629 if (buffer->use) {
1630 node = xmlNewDocText(doc, NULL);
1631 node->content = xmlBufferDetach(buffer);
1632
1633 if (last == NULL) {
1634 last = ret = node;
Owen Taylor3473f882001-02-23 17:55:21 +00001635 } else {
Conrad Irwin7d553f82012-05-10 20:17:25 -07001636 last = xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001637 }
1638 }
Conrad Irwin7d553f82012-05-10 20:17:25 -07001639
1640out:
1641 xmlBufferFree(buffer);
Owen Taylor3473f882001-02-23 17:55:21 +00001642 return(ret);
1643}
1644
1645/**
1646 * xmlNodeListGetString:
1647 * @doc: the document
1648 * @list: a Node list
1649 * @inLine: should we replace entity contents or show their external form
1650 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001651 * Build the string equivalent to the text contained in the Node list
Owen Taylor3473f882001-02-23 17:55:21 +00001652 * made of TEXTs and ENTITY_REFs
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001653 *
Daniel Veillardbd9afb52002-09-25 22:25:35 +00001654 * Returns a pointer to the string copy, the caller must free it with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00001655 */
1656xmlChar *
Daniel Veillard7646b182002-04-20 06:41:40 +00001657xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine)
1658{
Owen Taylor3473f882001-02-23 17:55:21 +00001659 xmlNodePtr node = list;
1660 xmlChar *ret = NULL;
1661 xmlEntityPtr ent;
1662
Daniel Veillard7646b182002-04-20 06:41:40 +00001663 if (list == NULL)
1664 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001665
1666 while (node != NULL) {
1667 if ((node->type == XML_TEXT_NODE) ||
Daniel Veillard7646b182002-04-20 06:41:40 +00001668 (node->type == XML_CDATA_SECTION_NODE)) {
1669 if (inLine) {
1670 ret = xmlStrcat(ret, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001671 } else {
Daniel Veillard7646b182002-04-20 06:41:40 +00001672 xmlChar *buffer;
Owen Taylor3473f882001-02-23 17:55:21 +00001673
Daniel Veillard7646b182002-04-20 06:41:40 +00001674 buffer = xmlEncodeEntitiesReentrant(doc, node->content);
1675 if (buffer != NULL) {
1676 ret = xmlStrcat(ret, buffer);
1677 xmlFree(buffer);
1678 }
1679 }
1680 } else if (node->type == XML_ENTITY_REF_NODE) {
1681 if (inLine) {
1682 ent = xmlGetDocEntity(doc, node->name);
1683 if (ent != NULL) {
1684 xmlChar *buffer;
1685
1686 /* an entity content can be any "well balanced chunk",
1687 * i.e. the result of the content [43] production:
1688 * http://www.w3.org/TR/REC-xml#NT-content.
1689 * So it can contain text, CDATA section or nested
1690 * entity reference nodes (among others).
1691 * -> we recursive call xmlNodeListGetString()
1692 * which handles these types */
1693 buffer = xmlNodeListGetString(doc, ent->children, 1);
1694 if (buffer != NULL) {
1695 ret = xmlStrcat(ret, buffer);
1696 xmlFree(buffer);
1697 }
1698 } else {
1699 ret = xmlStrcat(ret, node->content);
1700 }
1701 } else {
1702 xmlChar buf[2];
1703
1704 buf[0] = '&';
1705 buf[1] = 0;
1706 ret = xmlStrncat(ret, buf, 1);
1707 ret = xmlStrcat(ret, node->name);
1708 buf[0] = ';';
1709 buf[1] = 0;
1710 ret = xmlStrncat(ret, buf, 1);
1711 }
1712 }
1713#if 0
1714 else {
1715 xmlGenericError(xmlGenericErrorContext,
1716 "xmlGetNodeListString : invalid node type %d\n",
1717 node->type);
1718 }
1719#endif
1720 node = node->next;
1721 }
1722 return (ret);
1723}
Daniel Veillard652327a2003-09-29 18:02:38 +00001724
1725#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001726/**
1727 * xmlNodeListGetRawString:
1728 * @doc: the document
1729 * @list: a Node list
1730 * @inLine: should we replace entity contents or show their external form
1731 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001732 * Builds the string equivalent to the text contained in the Node list
Owen Taylor3473f882001-02-23 17:55:21 +00001733 * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString()
1734 * this function doesn't do any character encoding handling.
1735 *
Daniel Veillardbd9afb52002-09-25 22:25:35 +00001736 * Returns a pointer to the string copy, the caller must free it with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00001737 */
1738xmlChar *
Daniel Veillard7646b182002-04-20 06:41:40 +00001739xmlNodeListGetRawString(xmlDocPtr doc, xmlNodePtr list, int inLine)
1740{
Owen Taylor3473f882001-02-23 17:55:21 +00001741 xmlNodePtr node = list;
1742 xmlChar *ret = NULL;
1743 xmlEntityPtr ent;
1744
Daniel Veillard7646b182002-04-20 06:41:40 +00001745 if (list == NULL)
1746 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001747
1748 while (node != NULL) {
Daniel Veillard7db37732001-07-12 01:20:08 +00001749 if ((node->type == XML_TEXT_NODE) ||
Daniel Veillard7646b182002-04-20 06:41:40 +00001750 (node->type == XML_CDATA_SECTION_NODE)) {
1751 if (inLine) {
1752 ret = xmlStrcat(ret, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001753 } else {
Daniel Veillard7646b182002-04-20 06:41:40 +00001754 xmlChar *buffer;
1755
1756 buffer = xmlEncodeSpecialChars(doc, node->content);
1757 if (buffer != NULL) {
1758 ret = xmlStrcat(ret, buffer);
1759 xmlFree(buffer);
1760 }
1761 }
1762 } else if (node->type == XML_ENTITY_REF_NODE) {
1763 if (inLine) {
1764 ent = xmlGetDocEntity(doc, node->name);
1765 if (ent != NULL) {
1766 xmlChar *buffer;
1767
1768 /* an entity content can be any "well balanced chunk",
1769 * i.e. the result of the content [43] production:
1770 * http://www.w3.org/TR/REC-xml#NT-content.
1771 * So it can contain text, CDATA section or nested
1772 * entity reference nodes (among others).
1773 * -> we recursive call xmlNodeListGetRawString()
1774 * which handles these types */
1775 buffer =
1776 xmlNodeListGetRawString(doc, ent->children, 1);
1777 if (buffer != NULL) {
1778 ret = xmlStrcat(ret, buffer);
1779 xmlFree(buffer);
1780 }
1781 } else {
1782 ret = xmlStrcat(ret, node->content);
1783 }
1784 } else {
1785 xmlChar buf[2];
1786
1787 buf[0] = '&';
1788 buf[1] = 0;
1789 ret = xmlStrncat(ret, buf, 1);
1790 ret = xmlStrcat(ret, node->name);
1791 buf[0] = ';';
1792 buf[1] = 0;
1793 ret = xmlStrncat(ret, buf, 1);
1794 }
1795 }
Owen Taylor3473f882001-02-23 17:55:21 +00001796#if 0
Daniel Veillard7646b182002-04-20 06:41:40 +00001797 else {
1798 xmlGenericError(xmlGenericErrorContext,
1799 "xmlGetNodeListString : invalid node type %d\n",
1800 node->type);
1801 }
Owen Taylor3473f882001-02-23 17:55:21 +00001802#endif
Daniel Veillard7646b182002-04-20 06:41:40 +00001803 node = node->next;
Owen Taylor3473f882001-02-23 17:55:21 +00001804 }
Daniel Veillard7646b182002-04-20 06:41:40 +00001805 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00001806}
Daniel Veillard652327a2003-09-29 18:02:38 +00001807#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001808
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001809static xmlAttrPtr
1810xmlNewPropInternal(xmlNodePtr node, xmlNsPtr ns,
1811 const xmlChar * name, const xmlChar * value,
1812 int eatname)
1813{
Owen Taylor3473f882001-02-23 17:55:21 +00001814 xmlAttrPtr cur;
1815 xmlDocPtr doc = NULL;
1816
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001817 if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) {
Daniel Veillard8f6c2b12008-04-03 11:17:21 +00001818 if ((eatname == 1) &&
1819 ((node->doc == NULL) ||
Daniel Veillarded939f82008-04-08 08:20:08 +00001820 (!(xmlDictOwns(node->doc->dict, name)))))
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001821 xmlFree((xmlChar *) name);
1822 return (NULL);
1823 }
Owen Taylor3473f882001-02-23 17:55:21 +00001824
1825 /*
1826 * Allocate a new property and fill the fields.
1827 */
1828 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1829 if (cur == NULL) {
Daniel Veillard8f6c2b12008-04-03 11:17:21 +00001830 if ((eatname == 1) &&
Daniel Veillard76d36452009-09-07 11:19:33 +02001831 ((node == NULL) || (node->doc == NULL) ||
Daniel Veillarded939f82008-04-08 08:20:08 +00001832 (!(xmlDictOwns(node->doc->dict, name)))))
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001833 xmlFree((xmlChar *) name);
1834 xmlTreeErrMemory("building attribute");
1835 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001836 }
1837 memset(cur, 0, sizeof(xmlAttr));
1838 cur->type = XML_ATTRIBUTE_NODE;
1839
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001840 cur->parent = node;
Owen Taylor3473f882001-02-23 17:55:21 +00001841 if (node != NULL) {
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001842 doc = node->doc;
1843 cur->doc = doc;
Owen Taylor3473f882001-02-23 17:55:21 +00001844 }
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001845 cur->ns = ns;
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001846
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001847 if (eatname == 0) {
1848 if ((doc != NULL) && (doc->dict != NULL))
1849 cur->name = (xmlChar *) xmlDictLookup(doc->dict, name, -1);
1850 else
1851 cur->name = xmlStrdup(name);
1852 } else
1853 cur->name = name;
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001854
Owen Taylor3473f882001-02-23 17:55:21 +00001855 if (value != NULL) {
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001856 xmlNodePtr tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00001857
Daniel Veillard6f8611f2008-02-15 08:33:21 +00001858 if(!xmlCheckUTF8(value)) {
1859 xmlTreeErr(XML_TREE_NOT_UTF8, (xmlNodePtr) doc,
1860 NULL);
1861 if (doc != NULL)
1862 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
1863 }
1864 cur->children = xmlNewDocText(doc, value);
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001865 cur->last = NULL;
1866 tmp = cur->children;
1867 while (tmp != NULL) {
1868 tmp->parent = (xmlNodePtr) cur;
1869 if (tmp->next == NULL)
1870 cur->last = tmp;
1871 tmp = tmp->next;
1872 }
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001873 }
Owen Taylor3473f882001-02-23 17:55:21 +00001874
1875 /*
1876 * Add it at the end to preserve parsing order ...
1877 */
1878 if (node != NULL) {
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001879 if (node->properties == NULL) {
1880 node->properties = cur;
1881 } else {
1882 xmlAttrPtr prev = node->properties;
Owen Taylor3473f882001-02-23 17:55:21 +00001883
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001884 while (prev->next != NULL)
1885 prev = prev->next;
1886 prev->next = cur;
1887 cur->prev = prev;
1888 }
Owen Taylor3473f882001-02-23 17:55:21 +00001889 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00001890
Daniel Veillard76d36452009-09-07 11:19:33 +02001891 if ((value != NULL) && (node != NULL) &&
1892 (xmlIsID(node->doc, node, cur) == 1))
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001893 xmlAddID(NULL, node->doc, value, cur);
1894
Daniel Veillarda880b122003-04-21 21:36:41 +00001895 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001896 xmlRegisterNodeDefaultValue((xmlNodePtr) cur);
1897 return (cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001898}
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001899
1900#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
1901 defined(LIBXML_SCHEMAS_ENABLED)
1902/**
1903 * xmlNewProp:
1904 * @node: the holding node
1905 * @name: the name of the attribute
1906 * @value: the value of the attribute
1907 *
1908 * Create a new property carried by a node.
1909 * Returns a pointer to the attribute
1910 */
1911xmlAttrPtr
1912xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
1913
1914 if (name == NULL) {
1915#ifdef DEBUG_TREE
1916 xmlGenericError(xmlGenericErrorContext,
1917 "xmlNewProp : name == NULL\n");
1918#endif
1919 return(NULL);
1920 }
1921
1922 return xmlNewPropInternal(node, NULL, name, value, 0);
1923}
Daniel Veillard652327a2003-09-29 18:02:38 +00001924#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001925
1926/**
1927 * xmlNewNsProp:
1928 * @node: the holding node
1929 * @ns: the namespace
1930 * @name: the name of the attribute
1931 * @value: the value of the attribute
1932 *
1933 * Create a new property tagged with a namespace and carried by a node.
1934 * Returns a pointer to the attribute
1935 */
1936xmlAttrPtr
1937xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
1938 const xmlChar *value) {
Owen Taylor3473f882001-02-23 17:55:21 +00001939
1940 if (name == NULL) {
1941#ifdef DEBUG_TREE
1942 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00001943 "xmlNewNsProp : name == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001944#endif
1945 return(NULL);
1946 }
1947
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001948 return xmlNewPropInternal(node, ns, name, value, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001949}
1950
1951/**
Daniel Veillard46de64e2002-05-29 08:21:33 +00001952 * xmlNewNsPropEatName:
1953 * @node: the holding node
1954 * @ns: the namespace
1955 * @name: the name of the attribute
1956 * @value: the value of the attribute
1957 *
1958 * Create a new property tagged with a namespace and carried by a node.
1959 * Returns a pointer to the attribute
1960 */
1961xmlAttrPtr
1962xmlNewNsPropEatName(xmlNodePtr node, xmlNsPtr ns, xmlChar *name,
1963 const xmlChar *value) {
Daniel Veillard46de64e2002-05-29 08:21:33 +00001964
1965 if (name == NULL) {
1966#ifdef DEBUG_TREE
1967 xmlGenericError(xmlGenericErrorContext,
1968 "xmlNewNsPropEatName : name == NULL\n");
1969#endif
1970 return(NULL);
1971 }
1972
Daniel Veillard8f6c2b12008-04-03 11:17:21 +00001973 return xmlNewPropInternal(node, ns, name, value, 1);
Daniel Veillard46de64e2002-05-29 08:21:33 +00001974}
1975
1976/**
Owen Taylor3473f882001-02-23 17:55:21 +00001977 * xmlNewDocProp:
1978 * @doc: the document
1979 * @name: the name of the attribute
1980 * @value: the value of the attribute
1981 *
1982 * Create a new property carried by a document.
1983 * Returns a pointer to the attribute
1984 */
1985xmlAttrPtr
1986xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) {
1987 xmlAttrPtr cur;
1988
1989 if (name == NULL) {
1990#ifdef DEBUG_TREE
1991 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00001992 "xmlNewDocProp : name == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001993#endif
1994 return(NULL);
1995 }
1996
1997 /*
1998 * Allocate a new property and fill the fields.
1999 */
2000 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
2001 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002002 xmlTreeErrMemory("building attribute");
Owen Taylor3473f882001-02-23 17:55:21 +00002003 return(NULL);
2004 }
2005 memset(cur, 0, sizeof(xmlAttr));
2006 cur->type = XML_ATTRIBUTE_NODE;
2007
Daniel Veillard03a53c32004-10-26 16:06:51 +00002008 if ((doc != NULL) && (doc->dict != NULL))
2009 cur->name = xmlDictLookup(doc->dict, name, -1);
2010 else
2011 cur->name = xmlStrdup(name);
Daniel Veillardaa6de472008-08-25 14:53:31 +00002012 cur->doc = doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002013 if (value != NULL) {
2014 xmlNodePtr tmp;
2015
2016 cur->children = xmlStringGetNodeList(doc, value);
2017 cur->last = NULL;
2018
2019 tmp = cur->children;
2020 while (tmp != NULL) {
2021 tmp->parent = (xmlNodePtr) cur;
2022 if (tmp->next == NULL)
2023 cur->last = tmp;
2024 tmp = tmp->next;
2025 }
2026 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002027
Daniel Veillarda880b122003-04-21 21:36:41 +00002028 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002029 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002030 return(cur);
2031}
2032
2033/**
2034 * xmlFreePropList:
2035 * @cur: the first property in the list
2036 *
2037 * Free a property and all its siblings, all the children are freed too.
2038 */
2039void
2040xmlFreePropList(xmlAttrPtr cur) {
2041 xmlAttrPtr next;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002042 if (cur == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +00002043 while (cur != NULL) {
2044 next = cur->next;
2045 xmlFreeProp(cur);
2046 cur = next;
2047 }
2048}
2049
2050/**
2051 * xmlFreeProp:
2052 * @cur: an attribute
2053 *
2054 * Free one attribute, all the content is freed too
2055 */
2056void
2057xmlFreeProp(xmlAttrPtr cur) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002058 xmlDictPtr dict = NULL;
2059 if (cur == NULL) return;
2060
2061 if (cur->doc != NULL) dict = cur->doc->dict;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002062
Daniel Veillarda880b122003-04-21 21:36:41 +00002063 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002064 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
2065
Owen Taylor3473f882001-02-23 17:55:21 +00002066 /* Check for ID removal -> leading to invalid references ! */
Daniel Veillardda6f4af2005-06-20 17:17:54 +00002067 if ((cur->doc != NULL) && (cur->atype == XML_ATTRIBUTE_ID)) {
2068 xmlRemoveID(cur->doc, cur);
Daniel Veillard76d66f42001-05-16 21:05:17 +00002069 }
Owen Taylor3473f882001-02-23 17:55:21 +00002070 if (cur->children != NULL) xmlFreeNodeList(cur->children);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002071 DICT_FREE(cur->name)
Owen Taylor3473f882001-02-23 17:55:21 +00002072 xmlFree(cur);
2073}
2074
2075/**
2076 * xmlRemoveProp:
2077 * @cur: an attribute
2078 *
2079 * Unlink and free one attribute, all the content is freed too
2080 * Note this doesn't work for namespace definition attributes
2081 *
2082 * Returns 0 if success and -1 in case of error.
2083 */
2084int
2085xmlRemoveProp(xmlAttrPtr cur) {
2086 xmlAttrPtr tmp;
2087 if (cur == NULL) {
2088#ifdef DEBUG_TREE
2089 xmlGenericError(xmlGenericErrorContext,
2090 "xmlRemoveProp : cur == NULL\n");
2091#endif
2092 return(-1);
2093 }
2094 if (cur->parent == NULL) {
2095#ifdef DEBUG_TREE
2096 xmlGenericError(xmlGenericErrorContext,
2097 "xmlRemoveProp : cur->parent == NULL\n");
2098#endif
2099 return(-1);
2100 }
2101 tmp = cur->parent->properties;
2102 if (tmp == cur) {
2103 cur->parent->properties = cur->next;
Rob Richards19dc9612005-10-28 16:15:16 +00002104 if (cur->next != NULL)
2105 cur->next->prev = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002106 xmlFreeProp(cur);
2107 return(0);
2108 }
2109 while (tmp != NULL) {
2110 if (tmp->next == cur) {
2111 tmp->next = cur->next;
2112 if (tmp->next != NULL)
2113 tmp->next->prev = tmp;
2114 xmlFreeProp(cur);
2115 return(0);
2116 }
2117 tmp = tmp->next;
2118 }
2119#ifdef DEBUG_TREE
2120 xmlGenericError(xmlGenericErrorContext,
2121 "xmlRemoveProp : attribute not owned by its node\n");
2122#endif
2123 return(-1);
2124}
2125
2126/**
Daniel Veillard03a53c32004-10-26 16:06:51 +00002127 * xmlNewDocPI:
2128 * @doc: the target document
Owen Taylor3473f882001-02-23 17:55:21 +00002129 * @name: the processing instruction name
2130 * @content: the PI content
2131 *
2132 * Creation of a processing instruction element.
2133 * Returns a pointer to the new node object.
2134 */
2135xmlNodePtr
Daniel Veillard03a53c32004-10-26 16:06:51 +00002136xmlNewDocPI(xmlDocPtr doc, const xmlChar *name, const xmlChar *content) {
Owen Taylor3473f882001-02-23 17:55:21 +00002137 xmlNodePtr cur;
2138
2139 if (name == NULL) {
2140#ifdef DEBUG_TREE
2141 xmlGenericError(xmlGenericErrorContext,
2142 "xmlNewPI : name == NULL\n");
2143#endif
2144 return(NULL);
2145 }
2146
2147 /*
2148 * Allocate a new node and fill the fields.
2149 */
2150 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2151 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002152 xmlTreeErrMemory("building PI");
Owen Taylor3473f882001-02-23 17:55:21 +00002153 return(NULL);
2154 }
2155 memset(cur, 0, sizeof(xmlNode));
2156 cur->type = XML_PI_NODE;
2157
Daniel Veillard03a53c32004-10-26 16:06:51 +00002158 if ((doc != NULL) && (doc->dict != NULL))
2159 cur->name = xmlDictLookup(doc->dict, name, -1);
2160 else
2161 cur->name = xmlStrdup(name);
Owen Taylor3473f882001-02-23 17:55:21 +00002162 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002163 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00002164 }
Daniel Veillarda03e3652004-11-02 18:45:30 +00002165 cur->doc = doc;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002166
Daniel Veillarda880b122003-04-21 21:36:41 +00002167 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002168 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002169 return(cur);
2170}
2171
2172/**
Daniel Veillard03a53c32004-10-26 16:06:51 +00002173 * xmlNewPI:
2174 * @name: the processing instruction name
2175 * @content: the PI content
2176 *
2177 * Creation of a processing instruction element.
2178 * Use xmlDocNewPI preferably to get string interning
2179 *
2180 * Returns a pointer to the new node object.
2181 */
2182xmlNodePtr
2183xmlNewPI(const xmlChar *name, const xmlChar *content) {
2184 return(xmlNewDocPI(NULL, name, content));
2185}
2186
2187/**
Owen Taylor3473f882001-02-23 17:55:21 +00002188 * xmlNewNode:
2189 * @ns: namespace if any
2190 * @name: the node name
2191 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002192 * Creation of a new node element. @ns is optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00002193 *
William M. Brackd7cf7f82003-11-14 07:13:16 +00002194 * Returns a pointer to the new node object. Uses xmlStrdup() to make
2195 * copy of @name.
Owen Taylor3473f882001-02-23 17:55:21 +00002196 */
2197xmlNodePtr
2198xmlNewNode(xmlNsPtr ns, const xmlChar *name) {
2199 xmlNodePtr cur;
2200
2201 if (name == NULL) {
2202#ifdef DEBUG_TREE
2203 xmlGenericError(xmlGenericErrorContext,
2204 "xmlNewNode : name == NULL\n");
2205#endif
2206 return(NULL);
2207 }
2208
2209 /*
2210 * Allocate a new node and fill the fields.
2211 */
2212 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2213 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002214 xmlTreeErrMemory("building node");
Owen Taylor3473f882001-02-23 17:55:21 +00002215 return(NULL);
2216 }
2217 memset(cur, 0, sizeof(xmlNode));
2218 cur->type = XML_ELEMENT_NODE;
Daniel Veillardaa6de472008-08-25 14:53:31 +00002219
Owen Taylor3473f882001-02-23 17:55:21 +00002220 cur->name = xmlStrdup(name);
2221 cur->ns = ns;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002222
Daniel Veillarda880b122003-04-21 21:36:41 +00002223 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002224 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002225 return(cur);
2226}
2227
2228/**
Daniel Veillard46de64e2002-05-29 08:21:33 +00002229 * xmlNewNodeEatName:
2230 * @ns: namespace if any
2231 * @name: the node name
2232 *
2233 * Creation of a new node element. @ns is optional (NULL).
2234 *
William M. Brackd7cf7f82003-11-14 07:13:16 +00002235 * Returns a pointer to the new node object, with pointer @name as
2236 * new node's name. Use xmlNewNode() if a copy of @name string is
2237 * is needed as new node's name.
Daniel Veillard46de64e2002-05-29 08:21:33 +00002238 */
2239xmlNodePtr
2240xmlNewNodeEatName(xmlNsPtr ns, xmlChar *name) {
2241 xmlNodePtr cur;
2242
2243 if (name == NULL) {
2244#ifdef DEBUG_TREE
2245 xmlGenericError(xmlGenericErrorContext,
2246 "xmlNewNode : name == NULL\n");
2247#endif
2248 return(NULL);
2249 }
2250
2251 /*
2252 * Allocate a new node and fill the fields.
2253 */
2254 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2255 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002256 xmlTreeErrMemory("building node");
Daniel Veillard8f6c2b12008-04-03 11:17:21 +00002257 /* we can't check here that name comes from the doc dictionnary */
Daniel Veillard46de64e2002-05-29 08:21:33 +00002258 return(NULL);
2259 }
2260 memset(cur, 0, sizeof(xmlNode));
2261 cur->type = XML_ELEMENT_NODE;
Daniel Veillardaa6de472008-08-25 14:53:31 +00002262
Daniel Veillard46de64e2002-05-29 08:21:33 +00002263 cur->name = name;
2264 cur->ns = ns;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002265
Daniel Veillarda880b122003-04-21 21:36:41 +00002266 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002267 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Daniel Veillard46de64e2002-05-29 08:21:33 +00002268 return(cur);
2269}
2270
2271/**
Owen Taylor3473f882001-02-23 17:55:21 +00002272 * xmlNewDocNode:
2273 * @doc: the document
2274 * @ns: namespace if any
2275 * @name: the node name
2276 * @content: the XML text content if any
2277 *
2278 * Creation of a new node element within a document. @ns and @content
Daniel Veillardd1640922001-12-17 15:30:10 +00002279 * are optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00002280 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2281 * references, but XML special chars need to be escaped first by using
2282 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2283 * need entities support.
2284 *
2285 * Returns a pointer to the new node object.
2286 */
2287xmlNodePtr
2288xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns,
2289 const xmlChar *name, const xmlChar *content) {
2290 xmlNodePtr cur;
2291
Daniel Veillard95ddcd32004-10-26 21:53:55 +00002292 if ((doc != NULL) && (doc->dict != NULL))
Daniel Veillard03a53c32004-10-26 16:06:51 +00002293 cur = xmlNewNodeEatName(ns, (xmlChar *)
2294 xmlDictLookup(doc->dict, name, -1));
2295 else
2296 cur = xmlNewNode(ns, name);
Owen Taylor3473f882001-02-23 17:55:21 +00002297 if (cur != NULL) {
2298 cur->doc = doc;
2299 if (content != NULL) {
2300 cur->children = xmlStringGetNodeList(doc, content);
2301 UPDATE_LAST_CHILD_AND_PARENT(cur)
2302 }
2303 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002304
Owen Taylor3473f882001-02-23 17:55:21 +00002305 return(cur);
2306}
2307
Daniel Veillard46de64e2002-05-29 08:21:33 +00002308/**
2309 * xmlNewDocNodeEatName:
2310 * @doc: the document
2311 * @ns: namespace if any
2312 * @name: the node name
2313 * @content: the XML text content if any
2314 *
2315 * Creation of a new node element within a document. @ns and @content
2316 * are optional (NULL).
2317 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2318 * references, but XML special chars need to be escaped first by using
2319 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2320 * need entities support.
2321 *
2322 * Returns a pointer to the new node object.
2323 */
2324xmlNodePtr
2325xmlNewDocNodeEatName(xmlDocPtr doc, xmlNsPtr ns,
2326 xmlChar *name, const xmlChar *content) {
2327 xmlNodePtr cur;
2328
2329 cur = xmlNewNodeEatName(ns, name);
2330 if (cur != NULL) {
2331 cur->doc = doc;
2332 if (content != NULL) {
2333 cur->children = xmlStringGetNodeList(doc, content);
2334 UPDATE_LAST_CHILD_AND_PARENT(cur)
2335 }
Daniel Veillard8f6c2b12008-04-03 11:17:21 +00002336 } else {
2337 /* if name don't come from the doc dictionnary free it here */
2338 if ((name != NULL) && (doc != NULL) &&
2339 (!(xmlDictOwns(doc->dict, name))))
2340 xmlFree(name);
Daniel Veillard46de64e2002-05-29 08:21:33 +00002341 }
2342 return(cur);
2343}
2344
Daniel Veillard652327a2003-09-29 18:02:38 +00002345#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002346/**
2347 * xmlNewDocRawNode:
2348 * @doc: the document
2349 * @ns: namespace if any
2350 * @name: the node name
2351 * @content: the text content if any
2352 *
2353 * Creation of a new node element within a document. @ns and @content
Daniel Veillardd1640922001-12-17 15:30:10 +00002354 * are optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00002355 *
2356 * Returns a pointer to the new node object.
2357 */
2358xmlNodePtr
2359xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns,
2360 const xmlChar *name, const xmlChar *content) {
2361 xmlNodePtr cur;
2362
Daniel Veillard95ddcd32004-10-26 21:53:55 +00002363 cur = xmlNewDocNode(doc, ns, name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002364 if (cur != NULL) {
2365 cur->doc = doc;
2366 if (content != NULL) {
2367 cur->children = xmlNewDocText(doc, content);
2368 UPDATE_LAST_CHILD_AND_PARENT(cur)
2369 }
2370 }
2371 return(cur);
2372}
2373
2374/**
2375 * xmlNewDocFragment:
2376 * @doc: the document owning the fragment
2377 *
2378 * Creation of a new Fragment node.
2379 * Returns a pointer to the new node object.
2380 */
2381xmlNodePtr
2382xmlNewDocFragment(xmlDocPtr doc) {
2383 xmlNodePtr cur;
2384
2385 /*
2386 * Allocate a new DocumentFragment node and fill the fields.
2387 */
2388 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2389 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002390 xmlTreeErrMemory("building fragment");
Owen Taylor3473f882001-02-23 17:55:21 +00002391 return(NULL);
2392 }
2393 memset(cur, 0, sizeof(xmlNode));
2394 cur->type = XML_DOCUMENT_FRAG_NODE;
2395
2396 cur->doc = doc;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002397
Daniel Veillarda880b122003-04-21 21:36:41 +00002398 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002399 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002400 return(cur);
2401}
Daniel Veillard652327a2003-09-29 18:02:38 +00002402#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002403
2404/**
2405 * xmlNewText:
2406 * @content: the text content
2407 *
2408 * Creation of a new text node.
2409 * Returns a pointer to the new node object.
2410 */
2411xmlNodePtr
2412xmlNewText(const xmlChar *content) {
2413 xmlNodePtr cur;
2414
2415 /*
2416 * Allocate a new node and fill the fields.
2417 */
2418 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2419 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002420 xmlTreeErrMemory("building text");
Owen Taylor3473f882001-02-23 17:55:21 +00002421 return(NULL);
2422 }
2423 memset(cur, 0, sizeof(xmlNode));
2424 cur->type = XML_TEXT_NODE;
2425
2426 cur->name = xmlStringText;
2427 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002428 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00002429 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002430
Daniel Veillarda880b122003-04-21 21:36:41 +00002431 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002432 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002433 return(cur);
2434}
2435
Daniel Veillard652327a2003-09-29 18:02:38 +00002436#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002437/**
2438 * xmlNewTextChild:
2439 * @parent: the parent node
2440 * @ns: a namespace if any
2441 * @name: the name of the child
2442 * @content: the text content of the child if any.
2443 *
2444 * Creation of a new child element, added at the end of @parent children list.
MST 2003 John Flecke1f70492003-12-20 23:43:28 +00002445 * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
2446 * created element inherits the namespace of @parent. If @content is non NULL,
William M. Brackd7cf7f82003-11-14 07:13:16 +00002447 * a child TEXT node will be created containing the string @content.
MST 2003 John Fleck8b03bc52003-12-17 03:45:01 +00002448 * NOTE: Use xmlNewChild() if @content will contain entities that need to be
2449 * preserved. Use this function, xmlNewTextChild(), if you need to ensure that
Daniel Veillardaa6de472008-08-25 14:53:31 +00002450 * reserved XML chars that might appear in @content, such as the ampersand,
2451 * greater-than or less-than signs, are automatically replaced by their XML
2452 * escaped entity representations.
Owen Taylor3473f882001-02-23 17:55:21 +00002453 *
2454 * Returns a pointer to the new node object.
2455 */
2456xmlNodePtr
2457xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns,
2458 const xmlChar *name, const xmlChar *content) {
2459 xmlNodePtr cur, prev;
2460
2461 if (parent == NULL) {
2462#ifdef DEBUG_TREE
2463 xmlGenericError(xmlGenericErrorContext,
2464 "xmlNewTextChild : parent == NULL\n");
2465#endif
2466 return(NULL);
2467 }
2468
2469 if (name == NULL) {
2470#ifdef DEBUG_TREE
2471 xmlGenericError(xmlGenericErrorContext,
2472 "xmlNewTextChild : name == NULL\n");
2473#endif
2474 return(NULL);
2475 }
2476
2477 /*
2478 * Allocate a new node
2479 */
Daniel Veillard254b1262003-11-01 17:04:58 +00002480 if (parent->type == XML_ELEMENT_NODE) {
2481 if (ns == NULL)
2482 cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content);
2483 else
2484 cur = xmlNewDocRawNode(parent->doc, ns, name, content);
2485 } else if ((parent->type == XML_DOCUMENT_NODE) ||
2486 (parent->type == XML_HTML_DOCUMENT_NODE)) {
2487 if (ns == NULL)
2488 cur = xmlNewDocRawNode((xmlDocPtr) parent, NULL, name, content);
2489 else
2490 cur = xmlNewDocRawNode((xmlDocPtr) parent, ns, name, content);
2491 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2492 cur = xmlNewDocRawNode( parent->doc, ns, name, content);
2493 } else {
2494 return(NULL);
2495 }
Owen Taylor3473f882001-02-23 17:55:21 +00002496 if (cur == NULL) return(NULL);
2497
2498 /*
2499 * add the new element at the end of the children list.
2500 */
2501 cur->type = XML_ELEMENT_NODE;
2502 cur->parent = parent;
2503 cur->doc = parent->doc;
2504 if (parent->children == NULL) {
2505 parent->children = cur;
2506 parent->last = cur;
2507 } else {
2508 prev = parent->last;
2509 prev->next = cur;
2510 cur->prev = prev;
2511 parent->last = cur;
2512 }
2513
2514 return(cur);
2515}
Daniel Veillard652327a2003-09-29 18:02:38 +00002516#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002517
2518/**
2519 * xmlNewCharRef:
2520 * @doc: the document
2521 * @name: the char ref string, starting with # or "&# ... ;"
2522 *
2523 * Creation of a new character reference node.
2524 * Returns a pointer to the new node object.
2525 */
2526xmlNodePtr
2527xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) {
2528 xmlNodePtr cur;
2529
Daniel Veillard36e5cd52004-11-02 14:52:23 +00002530 if (name == NULL)
2531 return(NULL);
2532
Owen Taylor3473f882001-02-23 17:55:21 +00002533 /*
2534 * Allocate a new node and fill the fields.
2535 */
2536 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2537 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002538 xmlTreeErrMemory("building character reference");
Owen Taylor3473f882001-02-23 17:55:21 +00002539 return(NULL);
2540 }
2541 memset(cur, 0, sizeof(xmlNode));
2542 cur->type = XML_ENTITY_REF_NODE;
2543
2544 cur->doc = doc;
2545 if (name[0] == '&') {
2546 int len;
2547 name++;
2548 len = xmlStrlen(name);
2549 if (name[len - 1] == ';')
2550 cur->name = xmlStrndup(name, len - 1);
2551 else
2552 cur->name = xmlStrndup(name, len);
2553 } else
2554 cur->name = xmlStrdup(name);
Daniel Veillard5335dc52003-01-01 20:59:38 +00002555
Daniel Veillarda880b122003-04-21 21:36:41 +00002556 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002557 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002558 return(cur);
2559}
2560
2561/**
2562 * xmlNewReference:
2563 * @doc: the document
2564 * @name: the reference name, or the reference string with & and ;
2565 *
2566 * Creation of a new reference node.
2567 * Returns a pointer to the new node object.
2568 */
2569xmlNodePtr
2570xmlNewReference(xmlDocPtr doc, const xmlChar *name) {
2571 xmlNodePtr cur;
2572 xmlEntityPtr ent;
2573
Daniel Veillard36e5cd52004-11-02 14:52:23 +00002574 if (name == NULL)
2575 return(NULL);
2576
Owen Taylor3473f882001-02-23 17:55:21 +00002577 /*
2578 * Allocate a new node and fill the fields.
2579 */
2580 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2581 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002582 xmlTreeErrMemory("building reference");
Owen Taylor3473f882001-02-23 17:55:21 +00002583 return(NULL);
2584 }
2585 memset(cur, 0, sizeof(xmlNode));
2586 cur->type = XML_ENTITY_REF_NODE;
2587
2588 cur->doc = doc;
2589 if (name[0] == '&') {
2590 int len;
2591 name++;
2592 len = xmlStrlen(name);
2593 if (name[len - 1] == ';')
2594 cur->name = xmlStrndup(name, len - 1);
2595 else
2596 cur->name = xmlStrndup(name, len);
2597 } else
2598 cur->name = xmlStrdup(name);
2599
2600 ent = xmlGetDocEntity(doc, cur->name);
2601 if (ent != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002602 cur->content = ent->content;
Owen Taylor3473f882001-02-23 17:55:21 +00002603 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002604 * The parent pointer in entity is a DTD pointer and thus is NOT
Owen Taylor3473f882001-02-23 17:55:21 +00002605 * updated. Not sure if this is 100% correct.
2606 * -George
2607 */
2608 cur->children = (xmlNodePtr) ent;
2609 cur->last = (xmlNodePtr) ent;
2610 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002611
Daniel Veillarda880b122003-04-21 21:36:41 +00002612 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002613 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002614 return(cur);
2615}
2616
2617/**
2618 * xmlNewDocText:
2619 * @doc: the document
2620 * @content: the text content
2621 *
2622 * Creation of a new text node within a document.
2623 * Returns a pointer to the new node object.
2624 */
2625xmlNodePtr
2626xmlNewDocText(xmlDocPtr doc, const xmlChar *content) {
2627 xmlNodePtr cur;
2628
2629 cur = xmlNewText(content);
2630 if (cur != NULL) cur->doc = doc;
2631 return(cur);
2632}
2633
2634/**
2635 * xmlNewTextLen:
2636 * @content: the text content
2637 * @len: the text len.
2638 *
Daniel Veillard60087f32001-10-10 09:45:09 +00002639 * Creation of a new text node with an extra parameter for the content's length
Owen Taylor3473f882001-02-23 17:55:21 +00002640 * Returns a pointer to the new node object.
2641 */
2642xmlNodePtr
2643xmlNewTextLen(const xmlChar *content, int len) {
2644 xmlNodePtr cur;
2645
2646 /*
2647 * Allocate a new node and fill the fields.
2648 */
2649 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2650 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002651 xmlTreeErrMemory("building text");
Owen Taylor3473f882001-02-23 17:55:21 +00002652 return(NULL);
2653 }
2654 memset(cur, 0, sizeof(xmlNode));
2655 cur->type = XML_TEXT_NODE;
2656
2657 cur->name = xmlStringText;
2658 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002659 cur->content = xmlStrndup(content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00002660 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002661
Daniel Veillarda880b122003-04-21 21:36:41 +00002662 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002663 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002664 return(cur);
2665}
2666
2667/**
2668 * xmlNewDocTextLen:
2669 * @doc: the document
2670 * @content: the text content
2671 * @len: the text len.
2672 *
Daniel Veillard60087f32001-10-10 09:45:09 +00002673 * Creation of a new text node with an extra content length parameter. The
Owen Taylor3473f882001-02-23 17:55:21 +00002674 * text node pertain to a given document.
2675 * Returns a pointer to the new node object.
2676 */
2677xmlNodePtr
2678xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) {
2679 xmlNodePtr cur;
2680
2681 cur = xmlNewTextLen(content, len);
2682 if (cur != NULL) cur->doc = doc;
2683 return(cur);
2684}
2685
2686/**
2687 * xmlNewComment:
2688 * @content: the comment content
2689 *
2690 * Creation of a new node containing a comment.
2691 * Returns a pointer to the new node object.
2692 */
2693xmlNodePtr
2694xmlNewComment(const xmlChar *content) {
2695 xmlNodePtr cur;
2696
2697 /*
2698 * Allocate a new node and fill the fields.
2699 */
2700 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2701 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002702 xmlTreeErrMemory("building comment");
Owen Taylor3473f882001-02-23 17:55:21 +00002703 return(NULL);
2704 }
2705 memset(cur, 0, sizeof(xmlNode));
2706 cur->type = XML_COMMENT_NODE;
2707
2708 cur->name = xmlStringComment;
2709 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002710 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00002711 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002712
Daniel Veillarda880b122003-04-21 21:36:41 +00002713 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002714 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002715 return(cur);
2716}
2717
2718/**
2719 * xmlNewCDataBlock:
2720 * @doc: the document
Daniel Veillardd1640922001-12-17 15:30:10 +00002721 * @content: the CDATA block content content
Owen Taylor3473f882001-02-23 17:55:21 +00002722 * @len: the length of the block
2723 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002724 * Creation of a new node containing a CDATA block.
Owen Taylor3473f882001-02-23 17:55:21 +00002725 * Returns a pointer to the new node object.
2726 */
2727xmlNodePtr
2728xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) {
2729 xmlNodePtr cur;
2730
2731 /*
2732 * Allocate a new node and fill the fields.
2733 */
2734 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2735 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002736 xmlTreeErrMemory("building CDATA");
Owen Taylor3473f882001-02-23 17:55:21 +00002737 return(NULL);
2738 }
2739 memset(cur, 0, sizeof(xmlNode));
2740 cur->type = XML_CDATA_SECTION_NODE;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002741 cur->doc = doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002742
2743 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002744 cur->content = xmlStrndup(content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00002745 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002746
Daniel Veillarda880b122003-04-21 21:36:41 +00002747 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002748 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002749 return(cur);
2750}
2751
2752/**
2753 * xmlNewDocComment:
2754 * @doc: the document
2755 * @content: the comment content
2756 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002757 * Creation of a new node containing a comment within a document.
Owen Taylor3473f882001-02-23 17:55:21 +00002758 * Returns a pointer to the new node object.
2759 */
2760xmlNodePtr
2761xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) {
2762 xmlNodePtr cur;
2763
2764 cur = xmlNewComment(content);
2765 if (cur != NULL) cur->doc = doc;
2766 return(cur);
2767}
2768
2769/**
2770 * xmlSetTreeDoc:
2771 * @tree: the top element
2772 * @doc: the document
2773 *
2774 * update all nodes under the tree to point to the right document
2775 */
2776void
2777xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) {
Daniel Veillard19e96c32001-07-09 10:32:59 +00002778 xmlAttrPtr prop;
2779
Owen Taylor3473f882001-02-23 17:55:21 +00002780 if (tree == NULL)
2781 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002782 if (tree->doc != doc) {
Daniel Veillard36065812002-01-24 15:02:46 +00002783 if(tree->type == XML_ELEMENT_NODE) {
2784 prop = tree->properties;
2785 while (prop != NULL) {
2786 prop->doc = doc;
2787 xmlSetListDoc(prop->children, doc);
2788 prop = prop->next;
2789 }
Daniel Veillard19e96c32001-07-09 10:32:59 +00002790 }
Owen Taylor3473f882001-02-23 17:55:21 +00002791 if (tree->children != NULL)
2792 xmlSetListDoc(tree->children, doc);
2793 tree->doc = doc;
2794 }
2795}
2796
2797/**
2798 * xmlSetListDoc:
Daniel Veillardd1640922001-12-17 15:30:10 +00002799 * @list: the first element
Owen Taylor3473f882001-02-23 17:55:21 +00002800 * @doc: the document
2801 *
2802 * update all nodes in the list to point to the right document
2803 */
2804void
2805xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) {
2806 xmlNodePtr cur;
2807
2808 if (list == NULL)
2809 return;
2810 cur = list;
2811 while (cur != NULL) {
2812 if (cur->doc != doc)
2813 xmlSetTreeDoc(cur, doc);
2814 cur = cur->next;
2815 }
2816}
2817
Daniel Veillard2156d432004-03-04 15:59:36 +00002818#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00002819/**
2820 * xmlNewChild:
2821 * @parent: the parent node
2822 * @ns: a namespace if any
2823 * @name: the name of the child
2824 * @content: the XML content of the child if any.
2825 *
2826 * Creation of a new child element, added at the end of @parent children list.
MST 2003 John Flecke1f70492003-12-20 23:43:28 +00002827 * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
2828 * created element inherits the namespace of @parent. If @content is non NULL,
Owen Taylor3473f882001-02-23 17:55:21 +00002829 * a child list containing the TEXTs and ENTITY_REFs node will be created.
MST 2003 John Fleck8b03bc52003-12-17 03:45:01 +00002830 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
2831 * references. XML special chars must be escaped first by using
2832 * xmlEncodeEntitiesReentrant(), or xmlNewTextChild() should be used.
Owen Taylor3473f882001-02-23 17:55:21 +00002833 *
2834 * Returns a pointer to the new node object.
2835 */
2836xmlNodePtr
2837xmlNewChild(xmlNodePtr parent, xmlNsPtr ns,
2838 const xmlChar *name, const xmlChar *content) {
2839 xmlNodePtr cur, prev;
2840
2841 if (parent == NULL) {
2842#ifdef DEBUG_TREE
2843 xmlGenericError(xmlGenericErrorContext,
2844 "xmlNewChild : parent == NULL\n");
2845#endif
2846 return(NULL);
2847 }
2848
2849 if (name == NULL) {
2850#ifdef DEBUG_TREE
2851 xmlGenericError(xmlGenericErrorContext,
2852 "xmlNewChild : name == NULL\n");
2853#endif
2854 return(NULL);
2855 }
2856
2857 /*
2858 * Allocate a new node
2859 */
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002860 if (parent->type == XML_ELEMENT_NODE) {
2861 if (ns == NULL)
2862 cur = xmlNewDocNode(parent->doc, parent->ns, name, content);
2863 else
2864 cur = xmlNewDocNode(parent->doc, ns, name, content);
2865 } else if ((parent->type == XML_DOCUMENT_NODE) ||
2866 (parent->type == XML_HTML_DOCUMENT_NODE)) {
2867 if (ns == NULL)
2868 cur = xmlNewDocNode((xmlDocPtr) parent, NULL, name, content);
2869 else
2870 cur = xmlNewDocNode((xmlDocPtr) parent, ns, name, content);
Daniel Veillard7e3f1402002-10-28 18:52:57 +00002871 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2872 cur = xmlNewDocNode( parent->doc, ns, name, content);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002873 } else {
2874 return(NULL);
2875 }
Owen Taylor3473f882001-02-23 17:55:21 +00002876 if (cur == NULL) return(NULL);
2877
2878 /*
2879 * add the new element at the end of the children list.
2880 */
2881 cur->type = XML_ELEMENT_NODE;
2882 cur->parent = parent;
2883 cur->doc = parent->doc;
2884 if (parent->children == NULL) {
2885 parent->children = cur;
2886 parent->last = cur;
2887 } else {
2888 prev = parent->last;
2889 prev->next = cur;
2890 cur->prev = prev;
2891 parent->last = cur;
2892 }
2893
2894 return(cur);
2895}
Daniel Veillard652327a2003-09-29 18:02:38 +00002896#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002897
2898/**
Rob Richards65815122006-02-25 17:13:33 +00002899 * xmlAddPropSibling:
Daniel Veillardaa6de472008-08-25 14:53:31 +00002900 * @prev: the attribute to which @prop is added after
Rob Richards65815122006-02-25 17:13:33 +00002901 * @cur: the base attribute passed to calling function
2902 * @prop: the new attribute
2903 *
2904 * Add a new attribute after @prev using @cur as base attribute.
2905 * When inserting before @cur, @prev is passed as @cur->prev.
2906 * When inserting after @cur, @prev is passed as @cur.
Daniel Veillardaa6de472008-08-25 14:53:31 +00002907 * If an existing attribute is found it is detroyed prior to adding @prop.
Rob Richards65815122006-02-25 17:13:33 +00002908 *
2909 * Returns the attribute being inserted or NULL in case of error.
2910 */
2911static xmlNodePtr
2912xmlAddPropSibling(xmlNodePtr prev, xmlNodePtr cur, xmlNodePtr prop) {
2913 xmlAttrPtr attr;
2914
2915 if (cur->type != XML_ATTRIBUTE_NODE)
2916 return(NULL);
2917
2918 /* check if an attribute with the same name exists */
2919 if (prop->ns == NULL)
2920 attr = xmlHasNsProp(cur->parent, prop->name, NULL);
2921 else
2922 attr = xmlHasNsProp(cur->parent, prop->name, prop->ns->href);
2923
2924 if (prop->doc != cur->doc) {
2925 xmlSetTreeDoc(prop, cur->doc);
2926 }
2927 prop->parent = cur->parent;
2928 prop->prev = prev;
2929 if (prev != NULL) {
2930 prop->next = prev->next;
2931 prev->next = prop;
2932 if (prop->next)
2933 prop->next->prev = prop;
2934 } else {
2935 prop->next = cur;
2936 cur->prev = prop;
2937 }
2938 if (prop->prev == NULL && prop->parent != NULL)
2939 prop->parent->properties = (xmlAttrPtr) prop;
2940 if ((attr != NULL) && (attr->type != XML_ATTRIBUTE_DECL)) {
2941 /* different instance, destroy it (attributes must be unique) */
2942 xmlRemoveProp((xmlAttrPtr) attr);
2943 }
2944 return prop;
2945}
2946
2947/**
Owen Taylor3473f882001-02-23 17:55:21 +00002948 * xmlAddNextSibling:
2949 * @cur: the child node
2950 * @elem: the new node
2951 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002952 * Add a new node @elem as the next sibling of @cur
2953 * If the new node was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00002954 * first unlinked from its existing context.
2955 * As a result of text merging @elem may be freed.
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002956 * If the new node is ATTRIBUTE, it is added into properties instead of children.
Daniel Veillardaa6de472008-08-25 14:53:31 +00002957 * If there is an attribute with equal name, it is first destroyed.
Owen Taylor3473f882001-02-23 17:55:21 +00002958 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002959 * Returns the new node or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002960 */
2961xmlNodePtr
2962xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) {
2963 if (cur == NULL) {
2964#ifdef DEBUG_TREE
2965 xmlGenericError(xmlGenericErrorContext,
2966 "xmlAddNextSibling : cur == NULL\n");
2967#endif
2968 return(NULL);
2969 }
2970 if (elem == NULL) {
2971#ifdef DEBUG_TREE
2972 xmlGenericError(xmlGenericErrorContext,
2973 "xmlAddNextSibling : elem == NULL\n");
2974#endif
2975 return(NULL);
2976 }
2977
Rob Richards19dc9612005-10-28 16:15:16 +00002978 if (cur == elem) {
2979#ifdef DEBUG_TREE
2980 xmlGenericError(xmlGenericErrorContext,
2981 "xmlAddNextSibling : cur == elem\n");
2982#endif
2983 return(NULL);
2984 }
2985
Owen Taylor3473f882001-02-23 17:55:21 +00002986 xmlUnlinkNode(elem);
2987
2988 if (elem->type == XML_TEXT_NODE) {
2989 if (cur->type == XML_TEXT_NODE) {
Owen Taylor3473f882001-02-23 17:55:21 +00002990 xmlNodeAddContent(cur, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002991 xmlFreeNode(elem);
2992 return(cur);
2993 }
Daniel Veillard9e1c72d2001-08-31 20:03:19 +00002994 if ((cur->next != NULL) && (cur->next->type == XML_TEXT_NODE) &&
2995 (cur->name == cur->next->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002996 xmlChar *tmp;
2997
2998 tmp = xmlStrdup(elem->content);
2999 tmp = xmlStrcat(tmp, cur->next->content);
3000 xmlNodeSetContent(cur->next, tmp);
3001 xmlFree(tmp);
Owen Taylor3473f882001-02-23 17:55:21 +00003002 xmlFreeNode(elem);
3003 return(cur->next);
3004 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003005 } else if (elem->type == XML_ATTRIBUTE_NODE) {
Rob Richards65815122006-02-25 17:13:33 +00003006 return xmlAddPropSibling(cur, cur, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003007 }
3008
3009 if (elem->doc != cur->doc) {
3010 xmlSetTreeDoc(elem, cur->doc);
3011 }
3012 elem->parent = cur->parent;
3013 elem->prev = cur;
3014 elem->next = cur->next;
3015 cur->next = elem;
3016 if (elem->next != NULL)
3017 elem->next->prev = elem;
Rob Richards65815122006-02-25 17:13:33 +00003018 if ((elem->parent != NULL) && (elem->parent->last == cur))
Owen Taylor3473f882001-02-23 17:55:21 +00003019 elem->parent->last = elem;
3020 return(elem);
3021}
3022
William M. Brack21e4ef22005-01-02 09:53:13 +00003023#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
3024 defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00003025/**
3026 * xmlAddPrevSibling:
3027 * @cur: the child node
3028 * @elem: the new node
3029 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003030 * Add a new node @elem as the previous sibling of @cur
Owen Taylor3473f882001-02-23 17:55:21 +00003031 * merging adjacent TEXT nodes (@elem may be freed)
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003032 * If the new node was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00003033 * first unlinked from its existing context.
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003034 * If the new node is ATTRIBUTE, it is added into properties instead of children.
Daniel Veillardaa6de472008-08-25 14:53:31 +00003035 * If there is an attribute with equal name, it is first destroyed.
Owen Taylor3473f882001-02-23 17:55:21 +00003036 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003037 * Returns the new node or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003038 */
3039xmlNodePtr
3040xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) {
3041 if (cur == NULL) {
3042#ifdef DEBUG_TREE
3043 xmlGenericError(xmlGenericErrorContext,
3044 "xmlAddPrevSibling : cur == NULL\n");
3045#endif
3046 return(NULL);
3047 }
3048 if (elem == NULL) {
3049#ifdef DEBUG_TREE
3050 xmlGenericError(xmlGenericErrorContext,
3051 "xmlAddPrevSibling : elem == NULL\n");
3052#endif
3053 return(NULL);
3054 }
3055
Rob Richards19dc9612005-10-28 16:15:16 +00003056 if (cur == elem) {
3057#ifdef DEBUG_TREE
3058 xmlGenericError(xmlGenericErrorContext,
3059 "xmlAddPrevSibling : cur == elem\n");
3060#endif
3061 return(NULL);
3062 }
3063
Owen Taylor3473f882001-02-23 17:55:21 +00003064 xmlUnlinkNode(elem);
3065
3066 if (elem->type == XML_TEXT_NODE) {
3067 if (cur->type == XML_TEXT_NODE) {
Owen Taylor3473f882001-02-23 17:55:21 +00003068 xmlChar *tmp;
3069
3070 tmp = xmlStrdup(elem->content);
3071 tmp = xmlStrcat(tmp, cur->content);
3072 xmlNodeSetContent(cur, tmp);
3073 xmlFree(tmp);
Owen Taylor3473f882001-02-23 17:55:21 +00003074 xmlFreeNode(elem);
3075 return(cur);
3076 }
Daniel Veillard9e1c72d2001-08-31 20:03:19 +00003077 if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE) &&
3078 (cur->name == cur->prev->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003079 xmlNodeAddContent(cur->prev, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003080 xmlFreeNode(elem);
3081 return(cur->prev);
3082 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003083 } else if (elem->type == XML_ATTRIBUTE_NODE) {
Rob Richards65815122006-02-25 17:13:33 +00003084 return xmlAddPropSibling(cur->prev, cur, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003085 }
3086
3087 if (elem->doc != cur->doc) {
3088 xmlSetTreeDoc(elem, cur->doc);
3089 }
3090 elem->parent = cur->parent;
3091 elem->next = cur;
3092 elem->prev = cur->prev;
3093 cur->prev = elem;
3094 if (elem->prev != NULL)
3095 elem->prev->next = elem;
Rob Richards65815122006-02-25 17:13:33 +00003096 if ((elem->parent != NULL) && (elem->parent->children == cur)) {
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003097 elem->parent->children = elem;
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003098 }
Owen Taylor3473f882001-02-23 17:55:21 +00003099 return(elem);
3100}
Daniel Veillard652327a2003-09-29 18:02:38 +00003101#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00003102
3103/**
3104 * xmlAddSibling:
3105 * @cur: the child node
3106 * @elem: the new node
3107 *
3108 * Add a new element @elem to the list of siblings of @cur
3109 * merging adjacent TEXT nodes (@elem may be freed)
3110 * If the new element was already inserted in a document it is
3111 * first unlinked from its existing context.
3112 *
3113 * Returns the new element or NULL in case of error.
3114 */
3115xmlNodePtr
3116xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) {
3117 xmlNodePtr parent;
3118
3119 if (cur == NULL) {
3120#ifdef DEBUG_TREE
3121 xmlGenericError(xmlGenericErrorContext,
3122 "xmlAddSibling : cur == NULL\n");
3123#endif
3124 return(NULL);
3125 }
3126
3127 if (elem == NULL) {
3128#ifdef DEBUG_TREE
3129 xmlGenericError(xmlGenericErrorContext,
3130 "xmlAddSibling : elem == NULL\n");
3131#endif
3132 return(NULL);
3133 }
3134
Daniel Veillard43bc89c2009-03-23 19:32:04 +00003135 if (cur == elem) {
3136#ifdef DEBUG_TREE
3137 xmlGenericError(xmlGenericErrorContext,
3138 "xmlAddSibling : cur == elem\n");
3139#endif
3140 return(NULL);
3141 }
3142
Owen Taylor3473f882001-02-23 17:55:21 +00003143 /*
3144 * Constant time is we can rely on the ->parent->last to find
3145 * the last sibling.
3146 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00003147 if ((cur->type != XML_ATTRIBUTE_NODE) && (cur->parent != NULL) &&
Owen Taylor3473f882001-02-23 17:55:21 +00003148 (cur->parent->children != NULL) &&
3149 (cur->parent->last != NULL) &&
3150 (cur->parent->last->next == NULL)) {
3151 cur = cur->parent->last;
3152 } else {
3153 while (cur->next != NULL) cur = cur->next;
3154 }
3155
3156 xmlUnlinkNode(elem);
3157
Daniel Veillarde22dd5c2003-10-29 12:53:27 +00003158 if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE) &&
3159 (cur->name == elem->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003160 xmlNodeAddContent(cur, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003161 xmlFreeNode(elem);
3162 return(cur);
Rob Richards65815122006-02-25 17:13:33 +00003163 } else if (elem->type == XML_ATTRIBUTE_NODE) {
3164 return xmlAddPropSibling(cur, cur, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003165 }
3166
3167 if (elem->doc != cur->doc) {
3168 xmlSetTreeDoc(elem, cur->doc);
3169 }
3170 parent = cur->parent;
3171 elem->prev = cur;
3172 elem->next = NULL;
3173 elem->parent = parent;
3174 cur->next = elem;
3175 if (parent != NULL)
3176 parent->last = elem;
3177
3178 return(elem);
3179}
3180
3181/**
3182 * xmlAddChildList:
3183 * @parent: the parent node
3184 * @cur: the first node in the list
3185 *
3186 * Add a list of node at the end of the child list of the parent
3187 * merging adjacent TEXT nodes (@cur may be freed)
3188 *
3189 * Returns the last child or NULL in case of error.
3190 */
3191xmlNodePtr
3192xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) {
3193 xmlNodePtr prev;
3194
3195 if (parent == NULL) {
3196#ifdef DEBUG_TREE
3197 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00003198 "xmlAddChildList : parent == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003199#endif
3200 return(NULL);
3201 }
3202
3203 if (cur == NULL) {
3204#ifdef DEBUG_TREE
3205 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00003206 "xmlAddChildList : child == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003207#endif
3208 return(NULL);
3209 }
3210
3211 if ((cur->doc != NULL) && (parent->doc != NULL) &&
3212 (cur->doc != parent->doc)) {
3213#ifdef DEBUG_TREE
3214 xmlGenericError(xmlGenericErrorContext,
3215 "Elements moved to a different document\n");
3216#endif
3217 }
3218
3219 /*
3220 * add the first element at the end of the children list.
3221 */
Daniel Veillard5335dc52003-01-01 20:59:38 +00003222
Owen Taylor3473f882001-02-23 17:55:21 +00003223 if (parent->children == NULL) {
3224 parent->children = cur;
3225 } else {
3226 /*
3227 * If cur and parent->last both are TEXT nodes, then merge them.
3228 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00003229 if ((cur->type == XML_TEXT_NODE) &&
Owen Taylor3473f882001-02-23 17:55:21 +00003230 (parent->last->type == XML_TEXT_NODE) &&
3231 (cur->name == parent->last->name)) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00003232 xmlNodeAddContent(parent->last, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003233 /*
3234 * if it's the only child, nothing more to be done.
3235 */
3236 if (cur->next == NULL) {
3237 xmlFreeNode(cur);
3238 return(parent->last);
3239 }
3240 prev = cur;
3241 cur = cur->next;
3242 xmlFreeNode(prev);
3243 }
3244 prev = parent->last;
3245 prev->next = cur;
3246 cur->prev = prev;
3247 }
3248 while (cur->next != NULL) {
3249 cur->parent = parent;
3250 if (cur->doc != parent->doc) {
3251 xmlSetTreeDoc(cur, parent->doc);
3252 }
3253 cur = cur->next;
3254 }
3255 cur->parent = parent;
Rob Richards810a78b2008-12-31 22:13:57 +00003256 /* the parent may not be linked to a doc ! */
3257 if (cur->doc != parent->doc) {
3258 xmlSetTreeDoc(cur, parent->doc);
3259 }
Owen Taylor3473f882001-02-23 17:55:21 +00003260 parent->last = cur;
3261
3262 return(cur);
3263}
3264
3265/**
3266 * xmlAddChild:
3267 * @parent: the parent node
3268 * @cur: the child node
3269 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003270 * Add a new node to @parent, at the end of the child (or property) list
Owen Taylor3473f882001-02-23 17:55:21 +00003271 * merging adjacent TEXT nodes (in which case @cur is freed)
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003272 * If the new node is ATTRIBUTE, it is added into properties instead of children.
Daniel Veillardaa6de472008-08-25 14:53:31 +00003273 * If there is an attribute with equal name, it is first destroyed.
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003274 *
Owen Taylor3473f882001-02-23 17:55:21 +00003275 * Returns the child or NULL in case of error.
3276 */
3277xmlNodePtr
3278xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) {
3279 xmlNodePtr prev;
3280
3281 if (parent == NULL) {
3282#ifdef DEBUG_TREE
3283 xmlGenericError(xmlGenericErrorContext,
3284 "xmlAddChild : parent == NULL\n");
3285#endif
3286 return(NULL);
3287 }
3288
3289 if (cur == NULL) {
3290#ifdef DEBUG_TREE
3291 xmlGenericError(xmlGenericErrorContext,
3292 "xmlAddChild : child == NULL\n");
3293#endif
3294 return(NULL);
3295 }
3296
Rob Richards19dc9612005-10-28 16:15:16 +00003297 if (parent == cur) {
3298#ifdef DEBUG_TREE
3299 xmlGenericError(xmlGenericErrorContext,
3300 "xmlAddChild : parent == cur\n");
3301#endif
3302 return(NULL);
3303 }
Owen Taylor3473f882001-02-23 17:55:21 +00003304 /*
3305 * If cur is a TEXT node, merge its content with adjacent TEXT nodes
Owen Taylor3473f882001-02-23 17:55:21 +00003306 * cur is then freed.
3307 */
3308 if (cur->type == XML_TEXT_NODE) {
Daniel Veillard7db37732001-07-12 01:20:08 +00003309 if ((parent->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003310 (parent->content != NULL) &&
Rob Richards19dc9612005-10-28 16:15:16 +00003311 (parent->name == cur->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003312 xmlNodeAddContent(parent, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003313 xmlFreeNode(cur);
3314 return(parent);
3315 }
3316 if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003317 (parent->last->name == cur->name) &&
3318 (parent->last != cur)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003319 xmlNodeAddContent(parent->last, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003320 xmlFreeNode(cur);
3321 return(parent->last);
3322 }
3323 }
3324
3325 /*
3326 * add the new element at the end of the children list.
3327 */
Daniel Veillard5335dc52003-01-01 20:59:38 +00003328 prev = cur->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00003329 cur->parent = parent;
3330 if (cur->doc != parent->doc) {
3331 xmlSetTreeDoc(cur, parent->doc);
3332 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00003333 /* this check prevents a loop on tree-traversions if a developer
3334 * tries to add a node to its parent multiple times
3335 */
3336 if (prev == parent)
3337 return(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00003338
3339 /*
Daniel Veillard7db37732001-07-12 01:20:08 +00003340 * Coalescing
Owen Taylor3473f882001-02-23 17:55:21 +00003341 */
Daniel Veillard7db37732001-07-12 01:20:08 +00003342 if ((parent->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003343 (parent->content != NULL) &&
3344 (parent != cur)) {
Daniel Veillard7db37732001-07-12 01:20:08 +00003345 xmlNodeAddContent(parent, cur->content);
Daniel Veillard7db37732001-07-12 01:20:08 +00003346 xmlFreeNode(cur);
3347 return(parent);
Owen Taylor3473f882001-02-23 17:55:21 +00003348 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003349 if (cur->type == XML_ATTRIBUTE_NODE) {
Rob Richards19dc9612005-10-28 16:15:16 +00003350 if (parent->type != XML_ELEMENT_NODE)
3351 return(NULL);
Rob Richards810a78b2008-12-31 22:13:57 +00003352 if (parent->properties != NULL) {
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003353 /* check if an attribute with the same name exists */
3354 xmlAttrPtr lastattr;
Owen Taylor3473f882001-02-23 17:55:21 +00003355
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003356 if (cur->ns == NULL)
Rob Richardsc342ec62005-10-25 00:10:12 +00003357 lastattr = xmlHasNsProp(parent, cur->name, NULL);
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003358 else
3359 lastattr = xmlHasNsProp(parent, cur->name, cur->ns->href);
Rob Richardsc342ec62005-10-25 00:10:12 +00003360 if ((lastattr != NULL) && (lastattr != (xmlAttrPtr) cur) && (lastattr->type != XML_ATTRIBUTE_DECL)) {
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003361 /* different instance, destroy it (attributes must be unique) */
Rob Richards19dc9612005-10-28 16:15:16 +00003362 xmlUnlinkNode((xmlNodePtr) lastattr);
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003363 xmlFreeProp(lastattr);
3364 }
Rob Richards19dc9612005-10-28 16:15:16 +00003365 if (lastattr == (xmlAttrPtr) cur)
3366 return(cur);
Rob Richards810a78b2008-12-31 22:13:57 +00003367
3368 }
3369 if (parent->properties == NULL) {
3370 parent->properties = (xmlAttrPtr) cur;
3371 } else {
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003372 /* find the end */
Rob Richards810a78b2008-12-31 22:13:57 +00003373 xmlAttrPtr lastattr = parent->properties;
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003374 while (lastattr->next != NULL) {
3375 lastattr = lastattr->next;
3376 }
3377 lastattr->next = (xmlAttrPtr) cur;
3378 ((xmlAttrPtr) cur)->prev = lastattr;
3379 }
3380 } else {
3381 if (parent->children == NULL) {
3382 parent->children = cur;
3383 parent->last = cur;
3384 } else {
3385 prev = parent->last;
3386 prev->next = cur;
3387 cur->prev = prev;
3388 parent->last = cur;
3389 }
3390 }
Owen Taylor3473f882001-02-23 17:55:21 +00003391 return(cur);
3392}
3393
3394/**
3395 * xmlGetLastChild:
3396 * @parent: the parent node
3397 *
3398 * Search the last child of a node.
3399 * Returns the last child or NULL if none.
3400 */
3401xmlNodePtr
3402xmlGetLastChild(xmlNodePtr parent) {
3403 if (parent == NULL) {
3404#ifdef DEBUG_TREE
3405 xmlGenericError(xmlGenericErrorContext,
3406 "xmlGetLastChild : parent == NULL\n");
3407#endif
3408 return(NULL);
3409 }
3410 return(parent->last);
3411}
3412
Daniel Veillardbe2bd6a2008-11-27 15:26:28 +00003413#ifdef LIBXML_TREE_ENABLED
3414/*
3415 * 5 interfaces from DOM ElementTraversal
3416 */
3417
3418/**
3419 * xmlChildElementCount:
3420 * @parent: the parent node
3421 *
3422 * Finds the current number of child nodes of that element which are
3423 * element nodes.
3424 * Note the handling of entities references is different than in
3425 * the W3C DOM element traversal spec since we don't have back reference
3426 * from entities content to entities references.
3427 *
3428 * Returns the count of element child or 0 if not available
3429 */
3430unsigned long
3431xmlChildElementCount(xmlNodePtr parent) {
3432 unsigned long ret = 0;
3433 xmlNodePtr cur = NULL;
3434
3435 if (parent == NULL)
3436 return(0);
3437 switch (parent->type) {
3438 case XML_ELEMENT_NODE:
3439 case XML_ENTITY_NODE:
3440 case XML_DOCUMENT_NODE:
3441 case XML_HTML_DOCUMENT_NODE:
3442 cur = parent->children;
3443 break;
3444 default:
3445 return(0);
3446 }
3447 while (cur != NULL) {
3448 if (cur->type == XML_ELEMENT_NODE)
3449 ret++;
3450 cur = cur->next;
3451 }
3452 return(ret);
3453}
3454
3455/**
3456 * xmlFirstElementChild:
3457 * @parent: the parent node
3458 *
3459 * Finds the first child node of that element which is a Element node
3460 * Note the handling of entities references is different than in
3461 * the W3C DOM element traversal spec since we don't have back reference
3462 * from entities content to entities references.
3463 *
3464 * Returns the first element child or NULL if not available
3465 */
3466xmlNodePtr
3467xmlFirstElementChild(xmlNodePtr parent) {
3468 xmlNodePtr cur = NULL;
3469
3470 if (parent == NULL)
3471 return(NULL);
3472 switch (parent->type) {
3473 case XML_ELEMENT_NODE:
3474 case XML_ENTITY_NODE:
3475 case XML_DOCUMENT_NODE:
3476 case XML_HTML_DOCUMENT_NODE:
3477 cur = parent->children;
3478 break;
3479 default:
3480 return(NULL);
3481 }
3482 while (cur != NULL) {
3483 if (cur->type == XML_ELEMENT_NODE)
3484 return(cur);
3485 cur = cur->next;
3486 }
3487 return(NULL);
3488}
3489
3490/**
3491 * xmlLastElementChild:
3492 * @parent: the parent node
3493 *
3494 * Finds the last child node of that element which is a Element node
3495 * Note the handling of entities references is different than in
3496 * the W3C DOM element traversal spec since we don't have back reference
3497 * from entities content to entities references.
3498 *
3499 * Returns the last element child or NULL if not available
3500 */
3501xmlNodePtr
3502xmlLastElementChild(xmlNodePtr parent) {
3503 xmlNodePtr cur = NULL;
3504
3505 if (parent == NULL)
3506 return(NULL);
3507 switch (parent->type) {
3508 case XML_ELEMENT_NODE:
3509 case XML_ENTITY_NODE:
3510 case XML_DOCUMENT_NODE:
3511 case XML_HTML_DOCUMENT_NODE:
3512 cur = parent->last;
3513 break;
3514 default:
3515 return(NULL);
3516 }
3517 while (cur != NULL) {
3518 if (cur->type == XML_ELEMENT_NODE)
3519 return(cur);
3520 cur = cur->prev;
3521 }
3522 return(NULL);
3523}
3524
3525/**
3526 * xmlPreviousElementSibling:
3527 * @node: the current node
3528 *
3529 * Finds the first closest previous sibling of the node which is an
3530 * element node.
3531 * Note the handling of entities references is different than in
3532 * the W3C DOM element traversal spec since we don't have back reference
3533 * from entities content to entities references.
3534 *
3535 * Returns the previous element sibling or NULL if not available
3536 */
3537xmlNodePtr
3538xmlPreviousElementSibling(xmlNodePtr node) {
3539 if (node == NULL)
3540 return(NULL);
3541 switch (node->type) {
3542 case XML_ELEMENT_NODE:
3543 case XML_TEXT_NODE:
3544 case XML_CDATA_SECTION_NODE:
3545 case XML_ENTITY_REF_NODE:
3546 case XML_ENTITY_NODE:
3547 case XML_PI_NODE:
3548 case XML_COMMENT_NODE:
3549 case XML_XINCLUDE_START:
3550 case XML_XINCLUDE_END:
3551 node = node->prev;
3552 break;
3553 default:
3554 return(NULL);
3555 }
3556 while (node != NULL) {
3557 if (node->type == XML_ELEMENT_NODE)
3558 return(node);
François Delyon2f700902010-02-03 17:32:37 +01003559 node = node->prev;
Daniel Veillardbe2bd6a2008-11-27 15:26:28 +00003560 }
3561 return(NULL);
3562}
3563
3564/**
3565 * xmlNextElementSibling:
3566 * @node: the current node
3567 *
3568 * Finds the first closest next sibling of the node which is an
3569 * element node.
3570 * Note the handling of entities references is different than in
3571 * the W3C DOM element traversal spec since we don't have back reference
3572 * from entities content to entities references.
3573 *
3574 * Returns the next element sibling or NULL if not available
3575 */
3576xmlNodePtr
3577xmlNextElementSibling(xmlNodePtr node) {
3578 if (node == NULL)
3579 return(NULL);
3580 switch (node->type) {
3581 case XML_ELEMENT_NODE:
3582 case XML_TEXT_NODE:
3583 case XML_CDATA_SECTION_NODE:
3584 case XML_ENTITY_REF_NODE:
3585 case XML_ENTITY_NODE:
3586 case XML_PI_NODE:
3587 case XML_COMMENT_NODE:
3588 case XML_DTD_NODE:
3589 case XML_XINCLUDE_START:
3590 case XML_XINCLUDE_END:
3591 node = node->next;
3592 break;
3593 default:
3594 return(NULL);
3595 }
3596 while (node != NULL) {
3597 if (node->type == XML_ELEMENT_NODE)
3598 return(node);
3599 node = node->next;
3600 }
3601 return(NULL);
3602}
3603
3604#endif /* LIBXML_TREE_ENABLED */
3605
Owen Taylor3473f882001-02-23 17:55:21 +00003606/**
3607 * xmlFreeNodeList:
3608 * @cur: the first node in the list
3609 *
3610 * Free a node and all its siblings, this is a recursive behaviour, all
3611 * the children are freed too.
3612 */
3613void
3614xmlFreeNodeList(xmlNodePtr cur) {
3615 xmlNodePtr next;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003616 xmlDictPtr dict = NULL;
3617
3618 if (cur == NULL) return;
Daniel Veillarde6a55192002-01-14 17:11:53 +00003619 if (cur->type == XML_NAMESPACE_DECL) {
3620 xmlFreeNsList((xmlNsPtr) cur);
3621 return;
3622 }
Daniel Veillard9adc0462003-03-24 18:39:54 +00003623 if ((cur->type == XML_DOCUMENT_NODE) ||
3624#ifdef LIBXML_DOCB_ENABLED
3625 (cur->type == XML_DOCB_DOCUMENT_NODE) ||
Daniel Veillard9adc0462003-03-24 18:39:54 +00003626#endif
Daniel Veillard6560a422003-03-27 21:25:38 +00003627 (cur->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard9adc0462003-03-24 18:39:54 +00003628 xmlFreeDoc((xmlDocPtr) cur);
3629 return;
3630 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003631 if (cur->doc != NULL) dict = cur->doc->dict;
Owen Taylor3473f882001-02-23 17:55:21 +00003632 while (cur != NULL) {
3633 next = cur->next;
Daniel Veillard02141ea2001-04-30 11:46:40 +00003634 if (cur->type != XML_DTD_NODE) {
Daniel Veillard5335dc52003-01-01 20:59:38 +00003635
Daniel Veillarda880b122003-04-21 21:36:41 +00003636 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00003637 xmlDeregisterNodeDefaultValue(cur);
3638
Daniel Veillard02141ea2001-04-30 11:46:40 +00003639 if ((cur->children != NULL) &&
3640 (cur->type != XML_ENTITY_REF_NODE))
3641 xmlFreeNodeList(cur->children);
Daniel Veillard01c13b52002-12-10 15:19:08 +00003642 if (((cur->type == XML_ELEMENT_NODE) ||
3643 (cur->type == XML_XINCLUDE_START) ||
3644 (cur->type == XML_XINCLUDE_END)) &&
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003645 (cur->properties != NULL))
Daniel Veillard02141ea2001-04-30 11:46:40 +00003646 xmlFreePropList(cur->properties);
Daniel Veillard7db37732001-07-12 01:20:08 +00003647 if ((cur->type != XML_ELEMENT_NODE) &&
3648 (cur->type != XML_XINCLUDE_START) &&
3649 (cur->type != XML_XINCLUDE_END) &&
Daniel Veillard8874b942005-08-25 13:19:21 +00003650 (cur->type != XML_ENTITY_REF_NODE) &&
3651 (cur->content != (xmlChar *) &(cur->properties))) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003652 DICT_FREE(cur->content)
Daniel Veillard7db37732001-07-12 01:20:08 +00003653 }
3654 if (((cur->type == XML_ELEMENT_NODE) ||
3655 (cur->type == XML_XINCLUDE_START) ||
3656 (cur->type == XML_XINCLUDE_END)) &&
3657 (cur->nsDef != NULL))
3658 xmlFreeNsList(cur->nsDef);
3659
Daniel Veillard9cc6dc62001-06-11 08:09:20 +00003660 /*
3661 * When a node is a text node or a comment, it uses a global static
3662 * variable for the name of the node.
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003663 * Otherwise the node name might come from the document's
3664 * dictionnary
Daniel Veillard9cc6dc62001-06-11 08:09:20 +00003665 */
Daniel Veillard02141ea2001-04-30 11:46:40 +00003666 if ((cur->name != NULL) &&
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003667 (cur->type != XML_TEXT_NODE) &&
3668 (cur->type != XML_COMMENT_NODE))
3669 DICT_FREE(cur->name)
Daniel Veillard02141ea2001-04-30 11:46:40 +00003670 xmlFree(cur);
3671 }
Owen Taylor3473f882001-02-23 17:55:21 +00003672 cur = next;
3673 }
3674}
3675
3676/**
3677 * xmlFreeNode:
3678 * @cur: the node
3679 *
3680 * Free a node, this is a recursive behaviour, all the children are freed too.
3681 * This doesn't unlink the child from the list, use xmlUnlinkNode() first.
3682 */
3683void
3684xmlFreeNode(xmlNodePtr cur) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003685 xmlDictPtr dict = NULL;
3686
3687 if (cur == NULL) return;
Daniel Veillard5335dc52003-01-01 20:59:38 +00003688
Daniel Veillard02141ea2001-04-30 11:46:40 +00003689 /* use xmlFreeDtd for DTD nodes */
Daniel Veillarde6a55192002-01-14 17:11:53 +00003690 if (cur->type == XML_DTD_NODE) {
3691 xmlFreeDtd((xmlDtdPtr) cur);
Owen Taylor3473f882001-02-23 17:55:21 +00003692 return;
Daniel Veillarde6a55192002-01-14 17:11:53 +00003693 }
3694 if (cur->type == XML_NAMESPACE_DECL) {
3695 xmlFreeNs((xmlNsPtr) cur);
3696 return;
3697 }
Daniel Veillarda70d62f2002-11-07 14:18:03 +00003698 if (cur->type == XML_ATTRIBUTE_NODE) {
3699 xmlFreeProp((xmlAttrPtr) cur);
3700 return;
3701 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00003702
Daniel Veillarda880b122003-04-21 21:36:41 +00003703 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00003704 xmlDeregisterNodeDefaultValue(cur);
3705
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003706 if (cur->doc != NULL) dict = cur->doc->dict;
3707
Daniel Veillard2cba4152008-08-27 11:45:41 +00003708 if (cur->type == XML_ENTITY_DECL) {
3709 xmlEntityPtr ent = (xmlEntityPtr) cur;
3710 DICT_FREE(ent->SystemID);
3711 DICT_FREE(ent->ExternalID);
3712 }
Owen Taylor3473f882001-02-23 17:55:21 +00003713 if ((cur->children != NULL) &&
3714 (cur->type != XML_ENTITY_REF_NODE))
3715 xmlFreeNodeList(cur->children);
Daniel Veillard01c13b52002-12-10 15:19:08 +00003716 if (((cur->type == XML_ELEMENT_NODE) ||
3717 (cur->type == XML_XINCLUDE_START) ||
3718 (cur->type == XML_XINCLUDE_END)) &&
3719 (cur->properties != NULL))
Daniel Veillard02141ea2001-04-30 11:46:40 +00003720 xmlFreePropList(cur->properties);
Daniel Veillard7db37732001-07-12 01:20:08 +00003721 if ((cur->type != XML_ELEMENT_NODE) &&
3722 (cur->content != NULL) &&
3723 (cur->type != XML_ENTITY_REF_NODE) &&
3724 (cur->type != XML_XINCLUDE_END) &&
Daniel Veillard8874b942005-08-25 13:19:21 +00003725 (cur->type != XML_XINCLUDE_START) &&
3726 (cur->content != (xmlChar *) &(cur->properties))) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003727 DICT_FREE(cur->content)
Daniel Veillard7db37732001-07-12 01:20:08 +00003728 }
3729
Daniel Veillardacd370f2001-06-09 17:17:51 +00003730 /*
3731 * When a node is a text node or a comment, it uses a global static
3732 * variable for the name of the node.
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003733 * Otherwise the node name might come from the document's dictionnary
Daniel Veillardacd370f2001-06-09 17:17:51 +00003734 */
Owen Taylor3473f882001-02-23 17:55:21 +00003735 if ((cur->name != NULL) &&
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003736 (cur->type != XML_TEXT_NODE) &&
3737 (cur->type != XML_COMMENT_NODE))
3738 DICT_FREE(cur->name)
Daniel Veillardacd370f2001-06-09 17:17:51 +00003739
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003740 if (((cur->type == XML_ELEMENT_NODE) ||
3741 (cur->type == XML_XINCLUDE_START) ||
3742 (cur->type == XML_XINCLUDE_END)) &&
3743 (cur->nsDef != NULL))
3744 xmlFreeNsList(cur->nsDef);
Owen Taylor3473f882001-02-23 17:55:21 +00003745 xmlFree(cur);
3746}
3747
3748/**
3749 * xmlUnlinkNode:
3750 * @cur: the node
3751 *
3752 * Unlink a node from it's current context, the node is not freed
Daniel Veillard39d027c2012-05-11 12:38:23 +08003753 * If one need to free the node, use xmlFreeNode() routine after the
3754 * unlink to discard it.
Owen Taylor3473f882001-02-23 17:55:21 +00003755 */
3756void
3757xmlUnlinkNode(xmlNodePtr cur) {
3758 if (cur == NULL) {
3759#ifdef DEBUG_TREE
3760 xmlGenericError(xmlGenericErrorContext,
3761 "xmlUnlinkNode : node == NULL\n");
3762#endif
3763 return;
3764 }
Daniel Veillard29e43992001-12-13 22:21:58 +00003765 if (cur->type == XML_DTD_NODE) {
3766 xmlDocPtr doc;
3767 doc = cur->doc;
Daniel Veillarda067e652003-05-01 08:03:46 +00003768 if (doc != NULL) {
3769 if (doc->intSubset == (xmlDtdPtr) cur)
3770 doc->intSubset = NULL;
3771 if (doc->extSubset == (xmlDtdPtr) cur)
3772 doc->extSubset = NULL;
3773 }
Daniel Veillard29e43992001-12-13 22:21:58 +00003774 }
Daniel Veillard2cba4152008-08-27 11:45:41 +00003775 if (cur->type == XML_ENTITY_DECL) {
3776 xmlDocPtr doc;
3777 doc = cur->doc;
3778 if (doc != NULL) {
3779 if (doc->intSubset != NULL) {
3780 if (xmlHashLookup(doc->intSubset->entities, cur->name) == cur)
3781 xmlHashRemoveEntry(doc->intSubset->entities, cur->name,
3782 NULL);
3783 if (xmlHashLookup(doc->intSubset->pentities, cur->name) == cur)
3784 xmlHashRemoveEntry(doc->intSubset->pentities, cur->name,
3785 NULL);
3786 }
3787 if (doc->extSubset != NULL) {
3788 if (xmlHashLookup(doc->extSubset->entities, cur->name) == cur)
3789 xmlHashRemoveEntry(doc->extSubset->entities, cur->name,
3790 NULL);
3791 if (xmlHashLookup(doc->extSubset->pentities, cur->name) == cur)
3792 xmlHashRemoveEntry(doc->extSubset->pentities, cur->name,
3793 NULL);
3794 }
3795 }
3796 }
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003797 if (cur->parent != NULL) {
3798 xmlNodePtr parent;
3799 parent = cur->parent;
3800 if (cur->type == XML_ATTRIBUTE_NODE) {
3801 if (parent->properties == (xmlAttrPtr) cur)
3802 parent->properties = ((xmlAttrPtr) cur)->next;
3803 } else {
3804 if (parent->children == cur)
3805 parent->children = cur->next;
3806 if (parent->last == cur)
3807 parent->last = cur->prev;
3808 }
3809 cur->parent = NULL;
3810 }
Owen Taylor3473f882001-02-23 17:55:21 +00003811 if (cur->next != NULL)
3812 cur->next->prev = cur->prev;
3813 if (cur->prev != NULL)
3814 cur->prev->next = cur->next;
3815 cur->next = cur->prev = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00003816}
3817
Daniel Veillard2156d432004-03-04 15:59:36 +00003818#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00003819/**
3820 * xmlReplaceNode:
3821 * @old: the old node
3822 * @cur: the node
3823 *
William M. Brackd7cf7f82003-11-14 07:13:16 +00003824 * Unlink the old node from its current context, prune the new one
Daniel Veillardd1640922001-12-17 15:30:10 +00003825 * at the same place. If @cur was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00003826 * first unlinked from its existing context.
3827 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003828 * Returns the @old node
Owen Taylor3473f882001-02-23 17:55:21 +00003829 */
3830xmlNodePtr
3831xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) {
Daniel Veillardce244ad2004-11-05 10:03:46 +00003832 if (old == cur) return(NULL);
Daniel Veillarda03e3652004-11-02 18:45:30 +00003833 if ((old == NULL) || (old->parent == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003834#ifdef DEBUG_TREE
3835 xmlGenericError(xmlGenericErrorContext,
Daniel Veillarda03e3652004-11-02 18:45:30 +00003836 "xmlReplaceNode : old == NULL or without parent\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003837#endif
3838 return(NULL);
3839 }
3840 if (cur == NULL) {
3841 xmlUnlinkNode(old);
3842 return(old);
3843 }
3844 if (cur == old) {
3845 return(old);
3846 }
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003847 if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) {
3848#ifdef DEBUG_TREE
3849 xmlGenericError(xmlGenericErrorContext,
3850 "xmlReplaceNode : Trying to replace attribute node with other node type\n");
3851#endif
3852 return(old);
3853 }
3854 if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) {
3855#ifdef DEBUG_TREE
3856 xmlGenericError(xmlGenericErrorContext,
3857 "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n");
3858#endif
3859 return(old);
3860 }
Owen Taylor3473f882001-02-23 17:55:21 +00003861 xmlUnlinkNode(cur);
Daniel Veillard64d7d122005-05-11 18:03:42 +00003862 xmlSetTreeDoc(cur, old->doc);
Owen Taylor3473f882001-02-23 17:55:21 +00003863 cur->parent = old->parent;
3864 cur->next = old->next;
3865 if (cur->next != NULL)
3866 cur->next->prev = cur;
3867 cur->prev = old->prev;
3868 if (cur->prev != NULL)
3869 cur->prev->next = cur;
3870 if (cur->parent != NULL) {
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003871 if (cur->type == XML_ATTRIBUTE_NODE) {
3872 if (cur->parent->properties == (xmlAttrPtr)old)
3873 cur->parent->properties = ((xmlAttrPtr) cur);
3874 } else {
3875 if (cur->parent->children == old)
3876 cur->parent->children = cur;
3877 if (cur->parent->last == old)
3878 cur->parent->last = cur;
3879 }
Owen Taylor3473f882001-02-23 17:55:21 +00003880 }
3881 old->next = old->prev = NULL;
3882 old->parent = NULL;
3883 return(old);
3884}
Daniel Veillard652327a2003-09-29 18:02:38 +00003885#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00003886
3887/************************************************************************
3888 * *
3889 * Copy operations *
3890 * *
3891 ************************************************************************/
Daniel Veillardaa6de472008-08-25 14:53:31 +00003892
Owen Taylor3473f882001-02-23 17:55:21 +00003893/**
3894 * xmlCopyNamespace:
3895 * @cur: the namespace
3896 *
3897 * Do a copy of the namespace.
3898 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003899 * Returns: a new #xmlNsPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003900 */
3901xmlNsPtr
3902xmlCopyNamespace(xmlNsPtr cur) {
3903 xmlNsPtr ret;
3904
3905 if (cur == NULL) return(NULL);
3906 switch (cur->type) {
3907 case XML_LOCAL_NAMESPACE:
3908 ret = xmlNewNs(NULL, cur->href, cur->prefix);
3909 break;
3910 default:
3911#ifdef DEBUG_TREE
3912 xmlGenericError(xmlGenericErrorContext,
3913 "xmlCopyNamespace: invalid type %d\n", cur->type);
3914#endif
3915 return(NULL);
3916 }
3917 return(ret);
3918}
3919
3920/**
3921 * xmlCopyNamespaceList:
3922 * @cur: the first namespace
3923 *
3924 * Do a copy of an namespace list.
3925 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003926 * Returns: a new #xmlNsPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003927 */
3928xmlNsPtr
3929xmlCopyNamespaceList(xmlNsPtr cur) {
3930 xmlNsPtr ret = NULL;
3931 xmlNsPtr p = NULL,q;
3932
3933 while (cur != NULL) {
3934 q = xmlCopyNamespace(cur);
3935 if (p == NULL) {
3936 ret = p = q;
3937 } else {
3938 p->next = q;
3939 p = q;
3940 }
3941 cur = cur->next;
3942 }
3943 return(ret);
3944}
3945
3946static xmlNodePtr
3947xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent);
Rob Richards19dc9612005-10-28 16:15:16 +00003948
3949static xmlAttrPtr
3950xmlCopyPropInternal(xmlDocPtr doc, xmlNodePtr target, xmlAttrPtr cur) {
Owen Taylor3473f882001-02-23 17:55:21 +00003951 xmlAttrPtr ret;
3952
3953 if (cur == NULL) return(NULL);
3954 if (target != NULL)
3955 ret = xmlNewDocProp(target->doc, cur->name, NULL);
Rob Richards19dc9612005-10-28 16:15:16 +00003956 else if (doc != NULL)
3957 ret = xmlNewDocProp(doc, cur->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003958 else if (cur->parent != NULL)
3959 ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL);
3960 else if (cur->children != NULL)
3961 ret = xmlNewDocProp(cur->children->doc, cur->name, NULL);
3962 else
3963 ret = xmlNewDocProp(NULL, cur->name, NULL);
3964 if (ret == NULL) return(NULL);
3965 ret->parent = target;
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00003966
Owen Taylor3473f882001-02-23 17:55:21 +00003967 if ((cur->ns != NULL) && (target != NULL)) {
Daniel Veillard8107a222002-01-13 14:10:10 +00003968 xmlNsPtr ns;
Daniel Veillard652327a2003-09-29 18:02:38 +00003969
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00003970 ns = xmlSearchNs(target->doc, target, cur->ns->prefix);
3971 if (ns == NULL) {
3972 /*
3973 * Humm, we are copying an element whose namespace is defined
3974 * out of the new tree scope. Search it in the original tree
3975 * and add it at the top of the new tree
3976 */
3977 ns = xmlSearchNs(cur->doc, cur->parent, cur->ns->prefix);
3978 if (ns != NULL) {
3979 xmlNodePtr root = target;
3980 xmlNodePtr pred = NULL;
3981
3982 while (root->parent != NULL) {
3983 pred = root;
3984 root = root->parent;
3985 }
3986 if (root == (xmlNodePtr) target->doc) {
3987 /* correct possibly cycling above the document elt */
3988 root = pred;
3989 }
3990 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
3991 }
3992 } else {
3993 /*
3994 * we have to find something appropriate here since
3995 * we cant be sure, that the namespce we found is identified
3996 * by the prefix
3997 */
Daniel Veillard044fc6b2002-03-04 17:09:44 +00003998 if (xmlStrEqual(ns->href, cur->ns->href)) {
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00003999 /* this is the nice case */
4000 ret->ns = ns;
4001 } else {
4002 /*
4003 * we are in trouble: we need a new reconcilied namespace.
4004 * This is expensive
4005 */
4006 ret->ns = xmlNewReconciliedNs(target->doc, target, cur->ns);
4007 }
4008 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00004009
Owen Taylor3473f882001-02-23 17:55:21 +00004010 } else
4011 ret->ns = NULL;
4012
4013 if (cur->children != NULL) {
4014 xmlNodePtr tmp;
4015
4016 ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret);
4017 ret->last = NULL;
4018 tmp = ret->children;
4019 while (tmp != NULL) {
4020 /* tmp->parent = (xmlNodePtr)ret; */
4021 if (tmp->next == NULL)
4022 ret->last = tmp;
4023 tmp = tmp->next;
4024 }
4025 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00004026 /*
4027 * Try to handle IDs
4028 */
Daniel Veillarda3db2e32002-03-08 15:46:57 +00004029 if ((target!= NULL) && (cur!= NULL) &&
4030 (target->doc != NULL) && (cur->doc != NULL) &&
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00004031 (cur->doc->ids != NULL) && (cur->parent != NULL)) {
4032 if (xmlIsID(cur->doc, cur->parent, cur)) {
4033 xmlChar *id;
4034
4035 id = xmlNodeListGetString(cur->doc, cur->children, 1);
4036 if (id != NULL) {
4037 xmlAddID(NULL, target->doc, id, ret);
4038 xmlFree(id);
4039 }
4040 }
4041 }
Owen Taylor3473f882001-02-23 17:55:21 +00004042 return(ret);
4043}
4044
4045/**
Rob Richards19dc9612005-10-28 16:15:16 +00004046 * xmlCopyProp:
4047 * @target: the element where the attribute will be grafted
4048 * @cur: the attribute
4049 *
4050 * Do a copy of the attribute.
4051 *
4052 * Returns: a new #xmlAttrPtr, or NULL in case of error.
4053 */
4054xmlAttrPtr
4055xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) {
4056 return xmlCopyPropInternal(NULL, target, cur);
4057}
4058
4059/**
Owen Taylor3473f882001-02-23 17:55:21 +00004060 * xmlCopyPropList:
4061 * @target: the element where the attributes will be grafted
4062 * @cur: the first attribute
4063 *
4064 * Do a copy of an attribute list.
4065 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004066 * Returns: a new #xmlAttrPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004067 */
4068xmlAttrPtr
4069xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) {
4070 xmlAttrPtr ret = NULL;
4071 xmlAttrPtr p = NULL,q;
4072
4073 while (cur != NULL) {
4074 q = xmlCopyProp(target, cur);
William M. Brack13dfa872004-09-18 04:52:08 +00004075 if (q == NULL)
4076 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004077 if (p == NULL) {
4078 ret = p = q;
4079 } else {
4080 p->next = q;
4081 q->prev = p;
4082 p = q;
4083 }
4084 cur = cur->next;
4085 }
4086 return(ret);
4087}
4088
4089/*
Daniel Veillardd1640922001-12-17 15:30:10 +00004090 * NOTE about the CopyNode operations !
Owen Taylor3473f882001-02-23 17:55:21 +00004091 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004092 * They are split into external and internal parts for one
Owen Taylor3473f882001-02-23 17:55:21 +00004093 * tricky reason: namespaces. Doing a direct copy of a node
4094 * say RPM:Copyright without changing the namespace pointer to
4095 * something else can produce stale links. One way to do it is
4096 * to keep a reference counter but this doesn't work as soon
4097 * as one move the element or the subtree out of the scope of
4098 * the existing namespace. The actual solution seems to add
4099 * a copy of the namespace at the top of the copied tree if
4100 * not available in the subtree.
4101 * Hence two functions, the public front-end call the inner ones
William M. Brack57e9e912004-03-09 16:19:02 +00004102 * The argument "recursive" normally indicates a recursive copy
4103 * of the node with values 0 (no) and 1 (yes). For XInclude,
4104 * however, we allow a value of 2 to indicate copy properties and
4105 * namespace info, but don't recurse on children.
Owen Taylor3473f882001-02-23 17:55:21 +00004106 */
4107
4108static xmlNodePtr
Daniel Veillard3ec4c612001-08-28 20:39:49 +00004109xmlStaticCopyNode(const xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent,
William M. Brack57e9e912004-03-09 16:19:02 +00004110 int extended) {
Owen Taylor3473f882001-02-23 17:55:21 +00004111 xmlNodePtr ret;
4112
4113 if (node == NULL) return(NULL);
Daniel Veillard39196eb2001-06-19 18:09:42 +00004114 switch (node->type) {
4115 case XML_TEXT_NODE:
4116 case XML_CDATA_SECTION_NODE:
4117 case XML_ELEMENT_NODE:
Daniel Veillardec6725e2002-09-05 11:12:45 +00004118 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00004119 case XML_ENTITY_REF_NODE:
4120 case XML_ENTITY_NODE:
4121 case XML_PI_NODE:
4122 case XML_COMMENT_NODE:
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00004123 case XML_XINCLUDE_START:
4124 case XML_XINCLUDE_END:
4125 break;
4126 case XML_ATTRIBUTE_NODE:
Rob Richards19dc9612005-10-28 16:15:16 +00004127 return((xmlNodePtr) xmlCopyPropInternal(doc, parent, (xmlAttrPtr) node));
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00004128 case XML_NAMESPACE_DECL:
4129 return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node));
Daniel Veillardaa6de472008-08-25 14:53:31 +00004130
Daniel Veillard39196eb2001-06-19 18:09:42 +00004131 case XML_DOCUMENT_NODE:
4132 case XML_HTML_DOCUMENT_NODE:
4133#ifdef LIBXML_DOCB_ENABLED
4134 case XML_DOCB_DOCUMENT_NODE:
4135#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00004136#ifdef LIBXML_TREE_ENABLED
William M. Brack57e9e912004-03-09 16:19:02 +00004137 return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, extended));
Daniel Veillard652327a2003-09-29 18:02:38 +00004138#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard39196eb2001-06-19 18:09:42 +00004139 case XML_DOCUMENT_TYPE_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00004140 case XML_NOTATION_NODE:
4141 case XML_DTD_NODE:
4142 case XML_ELEMENT_DECL:
4143 case XML_ATTRIBUTE_DECL:
4144 case XML_ENTITY_DECL:
4145 return(NULL);
4146 }
Daniel Veillardb33c2012001-04-25 12:59:04 +00004147
Owen Taylor3473f882001-02-23 17:55:21 +00004148 /*
4149 * Allocate a new node and fill the fields.
4150 */
4151 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
4152 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004153 xmlTreeErrMemory("copying node");
Owen Taylor3473f882001-02-23 17:55:21 +00004154 return(NULL);
4155 }
4156 memset(ret, 0, sizeof(xmlNode));
4157 ret->type = node->type;
4158
4159 ret->doc = doc;
Daniel Veillardaa6de472008-08-25 14:53:31 +00004160 ret->parent = parent;
Owen Taylor3473f882001-02-23 17:55:21 +00004161 if (node->name == xmlStringText)
4162 ret->name = xmlStringText;
4163 else if (node->name == xmlStringTextNoenc)
4164 ret->name = xmlStringTextNoenc;
4165 else if (node->name == xmlStringComment)
4166 ret->name = xmlStringComment;
Daniel Veillard03a53c32004-10-26 16:06:51 +00004167 else if (node->name != NULL) {
4168 if ((doc != NULL) && (doc->dict != NULL))
4169 ret->name = xmlDictLookup(doc->dict, node->name, -1);
4170 else
4171 ret->name = xmlStrdup(node->name);
4172 }
Daniel Veillard7db37732001-07-12 01:20:08 +00004173 if ((node->type != XML_ELEMENT_NODE) &&
4174 (node->content != NULL) &&
4175 (node->type != XML_ENTITY_REF_NODE) &&
4176 (node->type != XML_XINCLUDE_END) &&
4177 (node->type != XML_XINCLUDE_START)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004178 ret->content = xmlStrdup(node->content);
Daniel Veillard8107a222002-01-13 14:10:10 +00004179 }else{
4180 if (node->type == XML_ELEMENT_NODE)
Daniel Veillard3e35f8e2003-10-21 00:05:38 +00004181 ret->line = node->line;
Owen Taylor3473f882001-02-23 17:55:21 +00004182 }
Daniel Veillardacb2bda2002-01-13 16:15:43 +00004183 if (parent != NULL) {
4184 xmlNodePtr tmp;
4185
Daniel Veillard8a1b1852003-01-05 22:37:17 +00004186 /*
4187 * this is a tricky part for the node register thing:
4188 * in case ret does get coalesced in xmlAddChild
4189 * the deregister-node callback is called; so we register ret now already
4190 */
Daniel Veillarda880b122003-04-21 21:36:41 +00004191 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00004192 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
4193
Daniel Veillardacb2bda2002-01-13 16:15:43 +00004194 tmp = xmlAddChild(parent, ret);
4195 /* node could have coalesced */
4196 if (tmp != ret)
4197 return(tmp);
4198 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00004199
William M. Brack57e9e912004-03-09 16:19:02 +00004200 if (!extended)
Daniel Veillard8a1b1852003-01-05 22:37:17 +00004201 goto out;
Petr Pajas2afca4a2009-07-30 17:47:32 +02004202 if (((node->type == XML_ELEMENT_NODE) ||
4203 (node->type == XML_XINCLUDE_START)) && (node->nsDef != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00004204 ret->nsDef = xmlCopyNamespaceList(node->nsDef);
4205
4206 if (node->ns != NULL) {
4207 xmlNsPtr ns;
4208
4209 ns = xmlSearchNs(doc, ret, node->ns->prefix);
4210 if (ns == NULL) {
4211 /*
4212 * Humm, we are copying an element whose namespace is defined
4213 * out of the new tree scope. Search it in the original tree
4214 * and add it at the top of the new tree
4215 */
4216 ns = xmlSearchNs(node->doc, node, node->ns->prefix);
4217 if (ns != NULL) {
4218 xmlNodePtr root = ret;
4219
4220 while (root->parent != NULL) root = root->parent;
Daniel Veillarde82a9922001-04-22 12:12:58 +00004221 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
Rob Richardsddb01cb2010-01-29 13:32:12 -05004222 } else {
4223 ret->ns = xmlNewReconciliedNs(doc, ret, node->ns);
Owen Taylor3473f882001-02-23 17:55:21 +00004224 }
4225 } else {
4226 /*
4227 * reference the existing namespace definition in our own tree.
4228 */
4229 ret->ns = ns;
4230 }
4231 }
Petr Pajas2afca4a2009-07-30 17:47:32 +02004232 if (((node->type == XML_ELEMENT_NODE) ||
4233 (node->type == XML_XINCLUDE_START)) && (node->properties != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00004234 ret->properties = xmlCopyPropList(ret, node->properties);
Daniel Veillardb33c2012001-04-25 12:59:04 +00004235 if (node->type == XML_ENTITY_REF_NODE) {
4236 if ((doc == NULL) || (node->doc != doc)) {
4237 /*
4238 * The copied node will go into a separate document, so
Daniel Veillardd1640922001-12-17 15:30:10 +00004239 * to avoid dangling references to the ENTITY_DECL node
Daniel Veillardb33c2012001-04-25 12:59:04 +00004240 * we cannot keep the reference. Try to find it in the
4241 * target document.
4242 */
4243 ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name);
4244 } else {
4245 ret->children = node->children;
4246 }
Daniel Veillard0ec98632001-11-14 15:04:32 +00004247 ret->last = ret->children;
William M. Brack57e9e912004-03-09 16:19:02 +00004248 } else if ((node->children != NULL) && (extended != 2)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004249 ret->children = xmlStaticCopyNodeList(node->children, doc, ret);
Daniel Veillard0ec98632001-11-14 15:04:32 +00004250 UPDATE_LAST_CHILD_AND_PARENT(ret)
4251 }
Daniel Veillard8a1b1852003-01-05 22:37:17 +00004252
4253out:
4254 /* if parent != NULL we already registered the node above */
Daniel Veillardac996a12004-07-30 12:02:58 +00004255 if ((parent == NULL) &&
4256 ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00004257 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
Owen Taylor3473f882001-02-23 17:55:21 +00004258 return(ret);
4259}
4260
4261static xmlNodePtr
4262xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) {
4263 xmlNodePtr ret = NULL;
4264 xmlNodePtr p = NULL,q;
4265
4266 while (node != NULL) {
Daniel Veillard652327a2003-09-29 18:02:38 +00004267#ifdef LIBXML_TREE_ENABLED
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00004268 if (node->type == XML_DTD_NODE ) {
Daniel Veillard4497e692001-06-09 14:19:02 +00004269 if (doc == NULL) {
4270 node = node->next;
4271 continue;
4272 }
Daniel Veillardb33c2012001-04-25 12:59:04 +00004273 if (doc->intSubset == NULL) {
4274 q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node );
4275 q->doc = doc;
4276 q->parent = parent;
4277 doc->intSubset = (xmlDtdPtr) q;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004278 xmlAddChild(parent, q);
Daniel Veillardb33c2012001-04-25 12:59:04 +00004279 } else {
4280 q = (xmlNodePtr) doc->intSubset;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004281 xmlAddChild(parent, q);
Daniel Veillardb33c2012001-04-25 12:59:04 +00004282 }
4283 } else
Daniel Veillard652327a2003-09-29 18:02:38 +00004284#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardb33c2012001-04-25 12:59:04 +00004285 q = xmlStaticCopyNode(node, doc, parent, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00004286 if (ret == NULL) {
4287 q->prev = NULL;
4288 ret = p = q;
Daniel Veillardacb2bda2002-01-13 16:15:43 +00004289 } else if (p != q) {
4290 /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */
Owen Taylor3473f882001-02-23 17:55:21 +00004291 p->next = q;
4292 q->prev = p;
4293 p = q;
4294 }
4295 node = node->next;
4296 }
4297 return(ret);
4298}
4299
4300/**
4301 * xmlCopyNode:
4302 * @node: the node
William M. Brack57e9e912004-03-09 16:19:02 +00004303 * @extended: if 1 do a recursive copy (properties, namespaces and children
4304 * when applicable)
4305 * if 2 copy properties and namespaces (when applicable)
Owen Taylor3473f882001-02-23 17:55:21 +00004306 *
4307 * Do a copy of the node.
4308 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004309 * Returns: a new #xmlNodePtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004310 */
4311xmlNodePtr
William M. Brack57e9e912004-03-09 16:19:02 +00004312xmlCopyNode(const xmlNodePtr node, int extended) {
Owen Taylor3473f882001-02-23 17:55:21 +00004313 xmlNodePtr ret;
4314
William M. Brack57e9e912004-03-09 16:19:02 +00004315 ret = xmlStaticCopyNode(node, NULL, NULL, extended);
Owen Taylor3473f882001-02-23 17:55:21 +00004316 return(ret);
4317}
4318
4319/**
Daniel Veillard82daa812001-04-12 08:55:36 +00004320 * xmlDocCopyNode:
4321 * @node: the node
Daniel Veillardd1640922001-12-17 15:30:10 +00004322 * @doc: the document
William M. Brack57e9e912004-03-09 16:19:02 +00004323 * @extended: if 1 do a recursive copy (properties, namespaces and children
4324 * when applicable)
4325 * if 2 copy properties and namespaces (when applicable)
Daniel Veillard82daa812001-04-12 08:55:36 +00004326 *
4327 * Do a copy of the node to a given document.
4328 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004329 * Returns: a new #xmlNodePtr, or NULL in case of error.
Daniel Veillard82daa812001-04-12 08:55:36 +00004330 */
4331xmlNodePtr
William M. Brack57e9e912004-03-09 16:19:02 +00004332xmlDocCopyNode(const xmlNodePtr node, xmlDocPtr doc, int extended) {
Daniel Veillard82daa812001-04-12 08:55:36 +00004333 xmlNodePtr ret;
4334
William M. Brack57e9e912004-03-09 16:19:02 +00004335 ret = xmlStaticCopyNode(node, doc, NULL, extended);
Daniel Veillard82daa812001-04-12 08:55:36 +00004336 return(ret);
4337}
4338
4339/**
Daniel Veillard03a53c32004-10-26 16:06:51 +00004340 * xmlDocCopyNodeList:
4341 * @doc: the target document
4342 * @node: the first node in the list.
4343 *
4344 * Do a recursive copy of the node list.
4345 *
4346 * Returns: a new #xmlNodePtr, or NULL in case of error.
4347 */
4348xmlNodePtr xmlDocCopyNodeList(xmlDocPtr doc, const xmlNodePtr node) {
4349 xmlNodePtr ret = xmlStaticCopyNodeList(node, doc, NULL);
4350 return(ret);
4351}
4352
4353/**
Owen Taylor3473f882001-02-23 17:55:21 +00004354 * xmlCopyNodeList:
4355 * @node: the first node in the list.
4356 *
4357 * Do a recursive copy of the node list.
Daniel Veillard03a53c32004-10-26 16:06:51 +00004358 * Use xmlDocCopyNodeList() if possible to ensure string interning.
Owen Taylor3473f882001-02-23 17:55:21 +00004359 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004360 * Returns: a new #xmlNodePtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004361 */
Daniel Veillard3ec4c612001-08-28 20:39:49 +00004362xmlNodePtr xmlCopyNodeList(const xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00004363 xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL);
4364 return(ret);
4365}
4366
Daniel Veillard2156d432004-03-04 15:59:36 +00004367#if defined(LIBXML_TREE_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004368/**
Owen Taylor3473f882001-02-23 17:55:21 +00004369 * xmlCopyDtd:
4370 * @dtd: the dtd
4371 *
4372 * Do a copy of the dtd.
4373 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004374 * Returns: a new #xmlDtdPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004375 */
4376xmlDtdPtr
4377xmlCopyDtd(xmlDtdPtr dtd) {
4378 xmlDtdPtr ret;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004379 xmlNodePtr cur, p = NULL, q;
Owen Taylor3473f882001-02-23 17:55:21 +00004380
4381 if (dtd == NULL) return(NULL);
4382 ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID);
4383 if (ret == NULL) return(NULL);
4384 if (dtd->entities != NULL)
4385 ret->entities = (void *) xmlCopyEntitiesTable(
4386 (xmlEntitiesTablePtr) dtd->entities);
4387 if (dtd->notations != NULL)
4388 ret->notations = (void *) xmlCopyNotationTable(
4389 (xmlNotationTablePtr) dtd->notations);
4390 if (dtd->elements != NULL)
4391 ret->elements = (void *) xmlCopyElementTable(
4392 (xmlElementTablePtr) dtd->elements);
4393 if (dtd->attributes != NULL)
4394 ret->attributes = (void *) xmlCopyAttributeTable(
4395 (xmlAttributeTablePtr) dtd->attributes);
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004396 if (dtd->pentities != NULL)
4397 ret->pentities = (void *) xmlCopyEntitiesTable(
4398 (xmlEntitiesTablePtr) dtd->pentities);
Daniel Veillardaa6de472008-08-25 14:53:31 +00004399
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004400 cur = dtd->children;
4401 while (cur != NULL) {
4402 q = NULL;
4403
4404 if (cur->type == XML_ENTITY_DECL) {
4405 xmlEntityPtr tmp = (xmlEntityPtr) cur;
4406 switch (tmp->etype) {
4407 case XML_INTERNAL_GENERAL_ENTITY:
4408 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
4409 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
4410 q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name);
4411 break;
4412 case XML_INTERNAL_PARAMETER_ENTITY:
4413 case XML_EXTERNAL_PARAMETER_ENTITY:
Daniel Veillardaa6de472008-08-25 14:53:31 +00004414 q = (xmlNodePtr)
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004415 xmlGetParameterEntityFromDtd(ret, tmp->name);
4416 break;
4417 case XML_INTERNAL_PREDEFINED_ENTITY:
4418 break;
4419 }
4420 } else if (cur->type == XML_ELEMENT_DECL) {
4421 xmlElementPtr tmp = (xmlElementPtr) cur;
4422 q = (xmlNodePtr)
4423 xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix);
4424 } else if (cur->type == XML_ATTRIBUTE_DECL) {
4425 xmlAttributePtr tmp = (xmlAttributePtr) cur;
Daniel Veillardaa6de472008-08-25 14:53:31 +00004426 q = (xmlNodePtr)
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004427 xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix);
4428 } else if (cur->type == XML_COMMENT_NODE) {
4429 q = xmlCopyNode(cur, 0);
4430 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00004431
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004432 if (q == NULL) {
4433 cur = cur->next;
4434 continue;
4435 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00004436
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004437 if (p == NULL)
4438 ret->children = q;
4439 else
Daniel Veillardaa6de472008-08-25 14:53:31 +00004440 p->next = q;
4441
4442 q->prev = p;
4443 q->parent = (xmlNodePtr) ret;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004444 q->next = NULL;
4445 ret->last = q;
Daniel Veillardaa6de472008-08-25 14:53:31 +00004446 p = q;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004447 cur = cur->next;
4448 }
4449
Owen Taylor3473f882001-02-23 17:55:21 +00004450 return(ret);
4451}
Daniel Veillard2156d432004-03-04 15:59:36 +00004452#endif
Owen Taylor3473f882001-02-23 17:55:21 +00004453
Daniel Veillard2156d432004-03-04 15:59:36 +00004454#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004455/**
4456 * xmlCopyDoc:
4457 * @doc: the document
William M. Brack57e9e912004-03-09 16:19:02 +00004458 * @recursive: if not zero do a recursive copy.
Owen Taylor3473f882001-02-23 17:55:21 +00004459 *
4460 * Do a copy of the document info. If recursive, the content tree will
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004461 * be copied too as well as DTD, namespaces and entities.
Owen Taylor3473f882001-02-23 17:55:21 +00004462 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004463 * Returns: a new #xmlDocPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004464 */
4465xmlDocPtr
4466xmlCopyDoc(xmlDocPtr doc, int recursive) {
4467 xmlDocPtr ret;
4468
4469 if (doc == NULL) return(NULL);
4470 ret = xmlNewDoc(doc->version);
4471 if (ret == NULL) return(NULL);
4472 if (doc->name != NULL)
4473 ret->name = xmlMemStrdup(doc->name);
4474 if (doc->encoding != NULL)
4475 ret->encoding = xmlStrdup(doc->encoding);
Daniel Veillardf59507d2005-01-27 17:26:49 +00004476 if (doc->URL != NULL)
4477 ret->URL = xmlStrdup(doc->URL);
Owen Taylor3473f882001-02-23 17:55:21 +00004478 ret->charset = doc->charset;
4479 ret->compression = doc->compression;
4480 ret->standalone = doc->standalone;
4481 if (!recursive) return(ret);
4482
Daniel Veillardb33c2012001-04-25 12:59:04 +00004483 ret->last = NULL;
4484 ret->children = NULL;
Daniel Veillard2156d432004-03-04 15:59:36 +00004485#ifdef LIBXML_TREE_ENABLED
Daniel Veillardb33c2012001-04-25 12:59:04 +00004486 if (doc->intSubset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004487 ret->intSubset = xmlCopyDtd(doc->intSubset);
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004488 xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret);
Daniel Veillardb33c2012001-04-25 12:59:04 +00004489 ret->intSubset->parent = ret;
4490 }
Daniel Veillard2156d432004-03-04 15:59:36 +00004491#endif
Owen Taylor3473f882001-02-23 17:55:21 +00004492 if (doc->oldNs != NULL)
4493 ret->oldNs = xmlCopyNamespaceList(doc->oldNs);
4494 if (doc->children != NULL) {
4495 xmlNodePtr tmp;
Daniel Veillardaa6de472008-08-25 14:53:31 +00004496
Daniel Veillardb33c2012001-04-25 12:59:04 +00004497 ret->children = xmlStaticCopyNodeList(doc->children, ret,
4498 (xmlNodePtr)ret);
Owen Taylor3473f882001-02-23 17:55:21 +00004499 ret->last = NULL;
4500 tmp = ret->children;
4501 while (tmp != NULL) {
4502 if (tmp->next == NULL)
4503 ret->last = tmp;
4504 tmp = tmp->next;
4505 }
4506 }
4507 return(ret);
4508}
Daniel Veillard652327a2003-09-29 18:02:38 +00004509#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004510
4511/************************************************************************
4512 * *
4513 * Content access functions *
4514 * *
4515 ************************************************************************/
Daniel Veillardaa6de472008-08-25 14:53:31 +00004516
Owen Taylor3473f882001-02-23 17:55:21 +00004517/**
Daniel Veillard8faa7832001-11-26 15:58:08 +00004518 * xmlGetLineNo:
Daniel Veillard01c13b52002-12-10 15:19:08 +00004519 * @node: valid node
Daniel Veillard8faa7832001-11-26 15:58:08 +00004520 *
William M. Brackd7cf7f82003-11-14 07:13:16 +00004521 * Get line number of @node. This requires activation of this option
Daniel Veillardd1640922001-12-17 15:30:10 +00004522 * before invoking the parser by calling xmlLineNumbersDefault(1)
Daniel Veillard8faa7832001-11-26 15:58:08 +00004523 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004524 * Returns the line number if successful, -1 otherwise
Daniel Veillard8faa7832001-11-26 15:58:08 +00004525 */
4526long
4527xmlGetLineNo(xmlNodePtr node)
4528{
4529 long result = -1;
4530
4531 if (!node)
4532 return result;
Daniel Veillard73da77e2005-08-24 14:05:37 +00004533 if ((node->type == XML_ELEMENT_NODE) ||
4534 (node->type == XML_TEXT_NODE) ||
4535 (node->type == XML_COMMENT_NODE) ||
4536 (node->type == XML_PI_NODE))
Daniel Veillard3e35f8e2003-10-21 00:05:38 +00004537 result = (long) node->line;
Daniel Veillard8faa7832001-11-26 15:58:08 +00004538 else if ((node->prev != NULL) &&
4539 ((node->prev->type == XML_ELEMENT_NODE) ||
Daniel Veillard73da77e2005-08-24 14:05:37 +00004540 (node->prev->type == XML_TEXT_NODE) ||
4541 (node->prev->type == XML_COMMENT_NODE) ||
4542 (node->prev->type == XML_PI_NODE)))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004543 result = xmlGetLineNo(node->prev);
4544 else if ((node->parent != NULL) &&
Daniel Veillard73da77e2005-08-24 14:05:37 +00004545 (node->parent->type == XML_ELEMENT_NODE))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004546 result = xmlGetLineNo(node->parent);
4547
4548 return result;
4549}
4550
Daniel Veillard2156d432004-03-04 15:59:36 +00004551#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED)
Daniel Veillard8faa7832001-11-26 15:58:08 +00004552/**
4553 * xmlGetNodePath:
4554 * @node: a node
4555 *
4556 * Build a structure based Path for the given node
4557 *
4558 * Returns the new path or NULL in case of error. The caller must free
4559 * the returned string
4560 */
4561xmlChar *
4562xmlGetNodePath(xmlNodePtr node)
4563{
4564 xmlNodePtr cur, tmp, next;
4565 xmlChar *buffer = NULL, *temp;
4566 size_t buf_len;
4567 xmlChar *buf;
Daniel Veillard96c3a3b2002-10-14 15:39:04 +00004568 const char *sep;
Daniel Veillard8faa7832001-11-26 15:58:08 +00004569 const char *name;
4570 char nametemp[100];
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004571 int occur = 0, generic;
Daniel Veillard8faa7832001-11-26 15:58:08 +00004572
4573 if (node == NULL)
4574 return (NULL);
4575
4576 buf_len = 500;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004577 buffer = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004578 if (buffer == NULL) {
4579 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004580 return (NULL);
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004581 }
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004582 buf = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
Daniel Veillard8faa7832001-11-26 15:58:08 +00004583 if (buf == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004584 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004585 xmlFree(buffer);
4586 return (NULL);
4587 }
4588
4589 buffer[0] = 0;
4590 cur = node;
4591 do {
4592 name = "";
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004593 sep = "?";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004594 occur = 0;
4595 if ((cur->type == XML_DOCUMENT_NODE) ||
4596 (cur->type == XML_HTML_DOCUMENT_NODE)) {
4597 if (buffer[0] == '/')
4598 break;
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004599 sep = "/";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004600 next = NULL;
4601 } else if (cur->type == XML_ELEMENT_NODE) {
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004602 generic = 0;
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004603 sep = "/";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004604 name = (const char *) cur->name;
4605 if (cur->ns) {
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004606 if (cur->ns->prefix != NULL) {
William M. Brack13dfa872004-09-18 04:52:08 +00004607 snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
Daniel Veillardaa6de472008-08-25 14:53:31 +00004608 (char *)cur->ns->prefix, (char *)cur->name);
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004609 nametemp[sizeof(nametemp) - 1] = 0;
4610 name = nametemp;
4611 } else {
4612 /*
4613 * We cannot express named elements in the default
4614 * namespace, so use "*".
4615 */
4616 generic = 1;
4617 name = "*";
Daniel Veillardaa6de472008-08-25 14:53:31 +00004618 }
Daniel Veillard8faa7832001-11-26 15:58:08 +00004619 }
4620 next = cur->parent;
4621
4622 /*
4623 * Thumbler index computation
Daniel Veillardc00cda82003-04-07 10:22:39 +00004624 * TODO: the ocurence test seems bogus for namespaced names
Daniel Veillard8faa7832001-11-26 15:58:08 +00004625 */
4626 tmp = cur->prev;
4627 while (tmp != NULL) {
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00004628 if ((tmp->type == XML_ELEMENT_NODE) &&
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004629 (generic ||
Kasimier T. Buchcik43ceb1e2006-06-12 11:08:18 +00004630 (xmlStrEqual(cur->name, tmp->name) &&
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004631 ((tmp->ns == cur->ns) ||
4632 ((tmp->ns != NULL) && (cur->ns != NULL) &&
Kasimier T. Buchcik43ceb1e2006-06-12 11:08:18 +00004633 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004634 occur++;
4635 tmp = tmp->prev;
4636 }
4637 if (occur == 0) {
4638 tmp = cur->next;
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004639 while (tmp != NULL && occur == 0) {
4640 if ((tmp->type == XML_ELEMENT_NODE) &&
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004641 (generic ||
Kasimier T. Buchcik43ceb1e2006-06-12 11:08:18 +00004642 (xmlStrEqual(cur->name, tmp->name) &&
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004643 ((tmp->ns == cur->ns) ||
4644 ((tmp->ns != NULL) && (cur->ns != NULL) &&
Kasimier T. Buchcik43ceb1e2006-06-12 11:08:18 +00004645 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004646 occur++;
4647 tmp = tmp->next;
4648 }
4649 if (occur != 0)
4650 occur = 1;
4651 } else
4652 occur++;
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004653 } else if (cur->type == XML_COMMENT_NODE) {
4654 sep = "/";
4655 name = "comment()";
4656 next = cur->parent;
4657
4658 /*
4659 * Thumbler index computation
4660 */
4661 tmp = cur->prev;
4662 while (tmp != NULL) {
4663 if (tmp->type == XML_COMMENT_NODE)
4664 occur++;
4665 tmp = tmp->prev;
4666 }
4667 if (occur == 0) {
4668 tmp = cur->next;
4669 while (tmp != NULL && occur == 0) {
4670 if (tmp->type == XML_COMMENT_NODE)
4671 occur++;
4672 tmp = tmp->next;
4673 }
4674 if (occur != 0)
4675 occur = 1;
4676 } else
4677 occur++;
4678 } else if ((cur->type == XML_TEXT_NODE) ||
4679 (cur->type == XML_CDATA_SECTION_NODE)) {
4680 sep = "/";
4681 name = "text()";
4682 next = cur->parent;
4683
4684 /*
4685 * Thumbler index computation
4686 */
4687 tmp = cur->prev;
4688 while (tmp != NULL) {
Kasimier T. Buchcikeb468702006-02-15 10:57:50 +00004689 if ((tmp->type == XML_TEXT_NODE) ||
4690 (tmp->type == XML_CDATA_SECTION_NODE))
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004691 occur++;
4692 tmp = tmp->prev;
4693 }
Kasimier T. Buchcikeb468702006-02-15 10:57:50 +00004694 /*
4695 * Evaluate if this is the only text- or CDATA-section-node;
4696 * if yes, then we'll get "text()", otherwise "text()[1]".
4697 */
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004698 if (occur == 0) {
4699 tmp = cur->next;
Kasimier T. Buchcikeb468702006-02-15 10:57:50 +00004700 while (tmp != NULL) {
4701 if ((tmp->type == XML_TEXT_NODE) ||
4702 (tmp->type == XML_CDATA_SECTION_NODE))
4703 {
4704 occur = 1;
4705 break;
Daniel Veillardaa6de472008-08-25 14:53:31 +00004706 }
Kasimier T. Buchcikeb468702006-02-15 10:57:50 +00004707 tmp = tmp->next;
4708 }
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004709 } else
4710 occur++;
4711 } else if (cur->type == XML_PI_NODE) {
4712 sep = "/";
4713 snprintf(nametemp, sizeof(nametemp) - 1,
William M. Brack13dfa872004-09-18 04:52:08 +00004714 "processing-instruction('%s')", (char *)cur->name);
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004715 nametemp[sizeof(nametemp) - 1] = 0;
4716 name = nametemp;
4717
4718 next = cur->parent;
4719
4720 /*
4721 * Thumbler index computation
4722 */
4723 tmp = cur->prev;
4724 while (tmp != NULL) {
4725 if ((tmp->type == XML_PI_NODE) &&
4726 (xmlStrEqual(cur->name, tmp->name)))
4727 occur++;
4728 tmp = tmp->prev;
4729 }
4730 if (occur == 0) {
4731 tmp = cur->next;
4732 while (tmp != NULL && occur == 0) {
4733 if ((tmp->type == XML_PI_NODE) &&
4734 (xmlStrEqual(cur->name, tmp->name)))
4735 occur++;
4736 tmp = tmp->next;
4737 }
4738 if (occur != 0)
4739 occur = 1;
4740 } else
4741 occur++;
4742
Daniel Veillard8faa7832001-11-26 15:58:08 +00004743 } else if (cur->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004744 sep = "/@";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004745 name = (const char *) (((xmlAttrPtr) cur)->name);
Daniel Veillard365c8062005-07-19 11:31:55 +00004746 if (cur->ns) {
4747 if (cur->ns->prefix != NULL)
4748 snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
Daniel Veillardaa6de472008-08-25 14:53:31 +00004749 (char *)cur->ns->prefix, (char *)cur->name);
Daniel Veillard365c8062005-07-19 11:31:55 +00004750 else
4751 snprintf(nametemp, sizeof(nametemp) - 1, "%s",
Daniel Veillardaa6de472008-08-25 14:53:31 +00004752 (char *)cur->name);
Daniel Veillard365c8062005-07-19 11:31:55 +00004753 nametemp[sizeof(nametemp) - 1] = 0;
4754 name = nametemp;
4755 }
Daniel Veillard8faa7832001-11-26 15:58:08 +00004756 next = ((xmlAttrPtr) cur)->parent;
4757 } else {
4758 next = cur->parent;
4759 }
4760
4761 /*
4762 * Make sure there is enough room
4763 */
4764 if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) {
4765 buf_len =
4766 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20;
4767 temp = (xmlChar *) xmlRealloc(buffer, buf_len);
4768 if (temp == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004769 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004770 xmlFree(buf);
4771 xmlFree(buffer);
4772 return (NULL);
4773 }
4774 buffer = temp;
4775 temp = (xmlChar *) xmlRealloc(buf, buf_len);
4776 if (temp == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004777 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004778 xmlFree(buf);
4779 xmlFree(buffer);
4780 return (NULL);
4781 }
4782 buf = temp;
4783 }
4784 if (occur == 0)
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004785 snprintf((char *) buf, buf_len, "%s%s%s",
Daniel Veillard8faa7832001-11-26 15:58:08 +00004786 sep, name, (char *) buffer);
4787 else
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004788 snprintf((char *) buf, buf_len, "%s%s[%d]%s",
Daniel Veillard8faa7832001-11-26 15:58:08 +00004789 sep, name, occur, (char *) buffer);
William M. Brack13dfa872004-09-18 04:52:08 +00004790 snprintf((char *) buffer, buf_len, "%s", (char *)buf);
Daniel Veillard8faa7832001-11-26 15:58:08 +00004791 cur = next;
4792 } while (cur != NULL);
4793 xmlFree(buf);
4794 return (buffer);
4795}
Daniel Veillard652327a2003-09-29 18:02:38 +00004796#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard8faa7832001-11-26 15:58:08 +00004797
4798/**
Owen Taylor3473f882001-02-23 17:55:21 +00004799 * xmlDocGetRootElement:
4800 * @doc: the document
4801 *
4802 * Get the root element of the document (doc->children is a list
4803 * containing possibly comments, PIs, etc ...).
4804 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004805 * Returns the #xmlNodePtr for the root or NULL
Owen Taylor3473f882001-02-23 17:55:21 +00004806 */
4807xmlNodePtr
4808xmlDocGetRootElement(xmlDocPtr doc) {
4809 xmlNodePtr ret;
4810
4811 if (doc == NULL) return(NULL);
4812 ret = doc->children;
4813 while (ret != NULL) {
4814 if (ret->type == XML_ELEMENT_NODE)
4815 return(ret);
4816 ret = ret->next;
4817 }
4818 return(ret);
4819}
Daniel Veillardaa6de472008-08-25 14:53:31 +00004820
Daniel Veillard2156d432004-03-04 15:59:36 +00004821#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004822/**
4823 * xmlDocSetRootElement:
4824 * @doc: the document
Daniel Veillard26a45c82006-10-20 12:55:34 +00004825 * @root: the new document root element, if root is NULL no action is taken,
4826 * to remove a node from a document use xmlUnlinkNode(root) instead.
Owen Taylor3473f882001-02-23 17:55:21 +00004827 *
4828 * Set the root element of the document (doc->children is a list
4829 * containing possibly comments, PIs, etc ...).
4830 *
Daniel Veillard26a45c82006-10-20 12:55:34 +00004831 * Returns the old root element if any was found, NULL if root was NULL
Owen Taylor3473f882001-02-23 17:55:21 +00004832 */
4833xmlNodePtr
4834xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) {
4835 xmlNodePtr old = NULL;
4836
4837 if (doc == NULL) return(NULL);
Daniel Veillardc575b992002-02-08 13:28:40 +00004838 if (root == NULL)
4839 return(NULL);
4840 xmlUnlinkNode(root);
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00004841 xmlSetTreeDoc(root, doc);
Daniel Veillardc575b992002-02-08 13:28:40 +00004842 root->parent = (xmlNodePtr) doc;
Owen Taylor3473f882001-02-23 17:55:21 +00004843 old = doc->children;
4844 while (old != NULL) {
4845 if (old->type == XML_ELEMENT_NODE)
4846 break;
4847 old = old->next;
4848 }
4849 if (old == NULL) {
4850 if (doc->children == NULL) {
4851 doc->children = root;
4852 doc->last = root;
4853 } else {
4854 xmlAddSibling(doc->children, root);
4855 }
4856 } else {
4857 xmlReplaceNode(old, root);
4858 }
4859 return(old);
4860}
Daniel Veillard2156d432004-03-04 15:59:36 +00004861#endif
Daniel Veillardaa6de472008-08-25 14:53:31 +00004862
Daniel Veillard2156d432004-03-04 15:59:36 +00004863#if defined(LIBXML_TREE_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004864/**
4865 * xmlNodeSetLang:
4866 * @cur: the node being changed
Daniel Veillardd1640922001-12-17 15:30:10 +00004867 * @lang: the language description
Owen Taylor3473f882001-02-23 17:55:21 +00004868 *
4869 * Set the language of a node, i.e. the values of the xml:lang
4870 * attribute.
4871 */
4872void
4873xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004874 xmlNsPtr ns;
4875
Owen Taylor3473f882001-02-23 17:55:21 +00004876 if (cur == NULL) return;
4877 switch(cur->type) {
4878 case XML_TEXT_NODE:
4879 case XML_CDATA_SECTION_NODE:
4880 case XML_COMMENT_NODE:
4881 case XML_DOCUMENT_NODE:
4882 case XML_DOCUMENT_TYPE_NODE:
4883 case XML_DOCUMENT_FRAG_NODE:
4884 case XML_NOTATION_NODE:
4885 case XML_HTML_DOCUMENT_NODE:
4886 case XML_DTD_NODE:
4887 case XML_ELEMENT_DECL:
4888 case XML_ATTRIBUTE_DECL:
4889 case XML_ENTITY_DECL:
4890 case XML_PI_NODE:
4891 case XML_ENTITY_REF_NODE:
4892 case XML_ENTITY_NODE:
4893 case XML_NAMESPACE_DECL:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004894#ifdef LIBXML_DOCB_ENABLED
4895 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004896#endif
4897 case XML_XINCLUDE_START:
4898 case XML_XINCLUDE_END:
4899 return;
4900 case XML_ELEMENT_NODE:
4901 case XML_ATTRIBUTE_NODE:
4902 break;
4903 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004904 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4905 if (ns == NULL)
4906 return;
4907 xmlSetNsProp(cur, ns, BAD_CAST "lang", lang);
Owen Taylor3473f882001-02-23 17:55:21 +00004908}
Daniel Veillard652327a2003-09-29 18:02:38 +00004909#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardaa6de472008-08-25 14:53:31 +00004910
Owen Taylor3473f882001-02-23 17:55:21 +00004911/**
4912 * xmlNodeGetLang:
4913 * @cur: the node being checked
4914 *
4915 * Searches the language of a node, i.e. the values of the xml:lang
4916 * attribute or the one carried by the nearest ancestor.
4917 *
4918 * Returns a pointer to the lang value, or NULL if not found
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004919 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00004920 */
4921xmlChar *
4922xmlNodeGetLang(xmlNodePtr cur) {
4923 xmlChar *lang;
4924
4925 while (cur != NULL) {
Daniel Veillardc17337c2001-05-09 10:51:31 +00004926 lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE);
Owen Taylor3473f882001-02-23 17:55:21 +00004927 if (lang != NULL)
4928 return(lang);
4929 cur = cur->parent;
4930 }
4931 return(NULL);
4932}
Daniel Veillardaa6de472008-08-25 14:53:31 +00004933
Owen Taylor3473f882001-02-23 17:55:21 +00004934
Daniel Veillard652327a2003-09-29 18:02:38 +00004935#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00004936/**
4937 * xmlNodeSetSpacePreserve:
4938 * @cur: the node being changed
4939 * @val: the xml:space value ("0": default, 1: "preserve")
4940 *
4941 * Set (or reset) the space preserving behaviour of a node, i.e. the
4942 * value of the xml:space attribute.
4943 */
4944void
4945xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004946 xmlNsPtr ns;
4947
Owen Taylor3473f882001-02-23 17:55:21 +00004948 if (cur == NULL) return;
4949 switch(cur->type) {
4950 case XML_TEXT_NODE:
4951 case XML_CDATA_SECTION_NODE:
4952 case XML_COMMENT_NODE:
4953 case XML_DOCUMENT_NODE:
4954 case XML_DOCUMENT_TYPE_NODE:
4955 case XML_DOCUMENT_FRAG_NODE:
4956 case XML_NOTATION_NODE:
4957 case XML_HTML_DOCUMENT_NODE:
4958 case XML_DTD_NODE:
4959 case XML_ELEMENT_DECL:
4960 case XML_ATTRIBUTE_DECL:
4961 case XML_ENTITY_DECL:
4962 case XML_PI_NODE:
4963 case XML_ENTITY_REF_NODE:
4964 case XML_ENTITY_NODE:
4965 case XML_NAMESPACE_DECL:
4966 case XML_XINCLUDE_START:
4967 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004968#ifdef LIBXML_DOCB_ENABLED
4969 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004970#endif
4971 return;
4972 case XML_ELEMENT_NODE:
4973 case XML_ATTRIBUTE_NODE:
4974 break;
4975 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004976 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4977 if (ns == NULL)
4978 return;
Owen Taylor3473f882001-02-23 17:55:21 +00004979 switch (val) {
4980 case 0:
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004981 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "default");
Owen Taylor3473f882001-02-23 17:55:21 +00004982 break;
4983 case 1:
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004984 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "preserve");
Owen Taylor3473f882001-02-23 17:55:21 +00004985 break;
4986 }
4987}
Daniel Veillard652327a2003-09-29 18:02:38 +00004988#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004989
4990/**
4991 * xmlNodeGetSpacePreserve:
4992 * @cur: the node being checked
4993 *
4994 * Searches the space preserving behaviour of a node, i.e. the values
4995 * of the xml:space attribute or the one carried by the nearest
4996 * ancestor.
4997 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004998 * Returns -1 if xml:space is not inherited, 0 if "default", 1 if "preserve"
Owen Taylor3473f882001-02-23 17:55:21 +00004999 */
5000int
5001xmlNodeGetSpacePreserve(xmlNodePtr cur) {
5002 xmlChar *space;
5003
5004 while (cur != NULL) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005005 space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE);
Owen Taylor3473f882001-02-23 17:55:21 +00005006 if (space != NULL) {
5007 if (xmlStrEqual(space, BAD_CAST "preserve")) {
5008 xmlFree(space);
5009 return(1);
5010 }
5011 if (xmlStrEqual(space, BAD_CAST "default")) {
5012 xmlFree(space);
5013 return(0);
5014 }
5015 xmlFree(space);
5016 }
5017 cur = cur->parent;
5018 }
5019 return(-1);
5020}
Daniel Veillardaa6de472008-08-25 14:53:31 +00005021
Daniel Veillard652327a2003-09-29 18:02:38 +00005022#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00005023/**
5024 * xmlNodeSetName:
5025 * @cur: the node being changed
5026 * @name: the new tag name
5027 *
5028 * Set (or reset) the name of a node.
5029 */
5030void
5031xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) {
Daniel Veillardce244ad2004-11-05 10:03:46 +00005032 xmlDocPtr doc;
5033 xmlDictPtr dict;
5034
Owen Taylor3473f882001-02-23 17:55:21 +00005035 if (cur == NULL) return;
5036 if (name == NULL) return;
5037 switch(cur->type) {
5038 case XML_TEXT_NODE:
5039 case XML_CDATA_SECTION_NODE:
5040 case XML_COMMENT_NODE:
5041 case XML_DOCUMENT_TYPE_NODE:
5042 case XML_DOCUMENT_FRAG_NODE:
5043 case XML_NOTATION_NODE:
5044 case XML_HTML_DOCUMENT_NODE:
5045 case XML_NAMESPACE_DECL:
5046 case XML_XINCLUDE_START:
5047 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005048#ifdef LIBXML_DOCB_ENABLED
5049 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005050#endif
5051 return;
5052 case XML_ELEMENT_NODE:
5053 case XML_ATTRIBUTE_NODE:
5054 case XML_PI_NODE:
5055 case XML_ENTITY_REF_NODE:
5056 case XML_ENTITY_NODE:
5057 case XML_DTD_NODE:
5058 case XML_DOCUMENT_NODE:
5059 case XML_ELEMENT_DECL:
5060 case XML_ATTRIBUTE_DECL:
5061 case XML_ENTITY_DECL:
5062 break;
5063 }
Daniel Veillardce244ad2004-11-05 10:03:46 +00005064 doc = cur->doc;
5065 if (doc != NULL)
5066 dict = doc->dict;
5067 else
5068 dict = NULL;
5069 if (dict != NULL) {
5070 if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name)))
5071 xmlFree((xmlChar *) cur->name);
5072 cur->name = xmlDictLookup(dict, name, -1);
5073 } else {
5074 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
5075 cur->name = xmlStrdup(name);
5076 }
Owen Taylor3473f882001-02-23 17:55:21 +00005077}
Daniel Veillard2156d432004-03-04 15:59:36 +00005078#endif
Daniel Veillardaa6de472008-08-25 14:53:31 +00005079
Daniel Veillard2156d432004-03-04 15:59:36 +00005080#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00005081/**
5082 * xmlNodeSetBase:
5083 * @cur: the node being changed
5084 * @uri: the new base URI
5085 *
5086 * Set (or reset) the base URI of a node, i.e. the value of the
5087 * xml:base attribute.
5088 */
5089void
Daniel Veillardf85ce8e2003-09-22 10:24:45 +00005090xmlNodeSetBase(xmlNodePtr cur, const xmlChar* uri) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005091 xmlNsPtr ns;
Martin Trappelf3703102010-01-22 12:08:00 +01005092 xmlChar* fixed;
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005093
Owen Taylor3473f882001-02-23 17:55:21 +00005094 if (cur == NULL) return;
5095 switch(cur->type) {
5096 case XML_TEXT_NODE:
5097 case XML_CDATA_SECTION_NODE:
5098 case XML_COMMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005099 case XML_DOCUMENT_TYPE_NODE:
5100 case XML_DOCUMENT_FRAG_NODE:
5101 case XML_NOTATION_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005102 case XML_DTD_NODE:
5103 case XML_ELEMENT_DECL:
5104 case XML_ATTRIBUTE_DECL:
5105 case XML_ENTITY_DECL:
5106 case XML_PI_NODE:
5107 case XML_ENTITY_REF_NODE:
5108 case XML_ENTITY_NODE:
5109 case XML_NAMESPACE_DECL:
5110 case XML_XINCLUDE_START:
5111 case XML_XINCLUDE_END:
Owen Taylor3473f882001-02-23 17:55:21 +00005112 return;
5113 case XML_ELEMENT_NODE:
5114 case XML_ATTRIBUTE_NODE:
5115 break;
Daniel Veillard4cbe4702002-05-05 06:57:27 +00005116 case XML_DOCUMENT_NODE:
5117#ifdef LIBXML_DOCB_ENABLED
5118 case XML_DOCB_DOCUMENT_NODE:
5119#endif
5120 case XML_HTML_DOCUMENT_NODE: {
5121 xmlDocPtr doc = (xmlDocPtr) cur;
5122
5123 if (doc->URL != NULL)
5124 xmlFree((xmlChar *) doc->URL);
5125 if (uri == NULL)
5126 doc->URL = NULL;
5127 else
Daniel Veillardb8efdda2006-10-10 12:37:14 +00005128 doc->URL = xmlPathToURI(uri);
Daniel Veillard4cbe4702002-05-05 06:57:27 +00005129 return;
5130 }
Owen Taylor3473f882001-02-23 17:55:21 +00005131 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00005132
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005133 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
5134 if (ns == NULL)
5135 return;
Daniel Veillardb8efdda2006-10-10 12:37:14 +00005136 fixed = xmlPathToURI(uri);
5137 if (fixed != NULL) {
5138 xmlSetNsProp(cur, ns, BAD_CAST "base", fixed);
Martin Trappelf3703102010-01-22 12:08:00 +01005139 xmlFree(fixed);
Daniel Veillardb8efdda2006-10-10 12:37:14 +00005140 } else {
5141 xmlSetNsProp(cur, ns, BAD_CAST "base", uri);
5142 }
Owen Taylor3473f882001-02-23 17:55:21 +00005143}
Daniel Veillard652327a2003-09-29 18:02:38 +00005144#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005145
5146/**
Owen Taylor3473f882001-02-23 17:55:21 +00005147 * xmlNodeGetBase:
5148 * @doc: the document the node pertains to
5149 * @cur: the node being checked
5150 *
5151 * Searches for the BASE URL. The code should work on both XML
5152 * and HTML document even if base mechanisms are completely different.
5153 * It returns the base as defined in RFC 2396 sections
5154 * 5.1.1. Base URI within Document Content
5155 * and
5156 * 5.1.2. Base URI from the Encapsulating Entity
5157 * However it does not return the document base (5.1.3), use
Daniel Veillarde4d18492010-03-09 11:12:30 +01005158 * doc->URL in this case
Owen Taylor3473f882001-02-23 17:55:21 +00005159 *
5160 * Returns a pointer to the base URL, or NULL if not found
Daniel Veillardbd9afb52002-09-25 22:25:35 +00005161 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00005162 */
5163xmlChar *
5164xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00005165 xmlChar *oldbase = NULL;
5166 xmlChar *base, *newbase;
Owen Taylor3473f882001-02-23 17:55:21 +00005167
Daniel Veillardaa6de472008-08-25 14:53:31 +00005168 if ((cur == NULL) && (doc == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00005169 return(NULL);
Daniel Veillardaa6de472008-08-25 14:53:31 +00005170 if (doc == NULL) doc = cur->doc;
Owen Taylor3473f882001-02-23 17:55:21 +00005171 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
5172 cur = doc->children;
5173 while ((cur != NULL) && (cur->name != NULL)) {
5174 if (cur->type != XML_ELEMENT_NODE) {
5175 cur = cur->next;
5176 continue;
5177 }
5178 if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) {
5179 cur = cur->children;
5180 continue;
5181 }
5182 if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) {
5183 cur = cur->children;
5184 continue;
5185 }
5186 if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) {
5187 return(xmlGetProp(cur, BAD_CAST "href"));
5188 }
5189 cur = cur->next;
5190 }
5191 return(NULL);
5192 }
5193 while (cur != NULL) {
5194 if (cur->type == XML_ENTITY_DECL) {
5195 xmlEntityPtr ent = (xmlEntityPtr) cur;
5196 return(xmlStrdup(ent->URI));
5197 }
Daniel Veillard42596ad2001-05-22 16:57:14 +00005198 if (cur->type == XML_ELEMENT_NODE) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00005199 base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE);
Daniel Veillard42596ad2001-05-22 16:57:14 +00005200 if (base != NULL) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00005201 if (oldbase != NULL) {
5202 newbase = xmlBuildURI(oldbase, base);
5203 if (newbase != NULL) {
5204 xmlFree(oldbase);
5205 xmlFree(base);
5206 oldbase = newbase;
5207 } else {
5208 xmlFree(oldbase);
5209 xmlFree(base);
5210 return(NULL);
5211 }
5212 } else {
5213 oldbase = base;
5214 }
5215 if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) ||
5216 (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) ||
5217 (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4)))
5218 return(oldbase);
Daniel Veillard42596ad2001-05-22 16:57:14 +00005219 }
5220 }
Owen Taylor3473f882001-02-23 17:55:21 +00005221 cur = cur->parent;
5222 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00005223 if ((doc != NULL) && (doc->URL != NULL)) {
5224 if (oldbase == NULL)
5225 return(xmlStrdup(doc->URL));
5226 newbase = xmlBuildURI(oldbase, doc->URL);
5227 xmlFree(oldbase);
5228 return(newbase);
5229 }
5230 return(oldbase);
Owen Taylor3473f882001-02-23 17:55:21 +00005231}
Daniel Veillardaa6de472008-08-25 14:53:31 +00005232
Owen Taylor3473f882001-02-23 17:55:21 +00005233/**
Daniel Veillard78697292003-10-19 20:44:43 +00005234 * xmlNodeBufGetContent:
5235 * @buffer: a buffer
5236 * @cur: the node being read
5237 *
5238 * Read the value of a node @cur, this can be either the text carried
5239 * directly by this node if it's a TEXT node or the aggregate string
5240 * of the values carried by this node child's (TEXT and ENTITY_REF).
5241 * Entity references are substituted.
5242 * Fills up the buffer @buffer with this value
Daniel Veillardaa6de472008-08-25 14:53:31 +00005243 *
Daniel Veillard78697292003-10-19 20:44:43 +00005244 * Returns 0 in case of success and -1 in case of error.
5245 */
5246int
5247xmlNodeBufGetContent(xmlBufferPtr buffer, xmlNodePtr cur)
5248{
5249 if ((cur == NULL) || (buffer == NULL)) return(-1);
5250 switch (cur->type) {
5251 case XML_CDATA_SECTION_NODE:
5252 case XML_TEXT_NODE:
5253 xmlBufferCat(buffer, cur->content);
5254 break;
5255 case XML_DOCUMENT_FRAG_NODE:
5256 case XML_ELEMENT_NODE:{
5257 xmlNodePtr tmp = cur;
5258
5259 while (tmp != NULL) {
5260 switch (tmp->type) {
5261 case XML_CDATA_SECTION_NODE:
5262 case XML_TEXT_NODE:
5263 if (tmp->content != NULL)
5264 xmlBufferCat(buffer, tmp->content);
5265 break;
5266 case XML_ENTITY_REF_NODE:
Rob Richards77b92ff2005-12-20 15:55:14 +00005267 xmlNodeBufGetContent(buffer, tmp);
5268 break;
Daniel Veillard78697292003-10-19 20:44:43 +00005269 default:
5270 break;
5271 }
5272 /*
5273 * Skip to next node
5274 */
5275 if (tmp->children != NULL) {
5276 if (tmp->children->type != XML_ENTITY_DECL) {
5277 tmp = tmp->children;
5278 continue;
5279 }
5280 }
5281 if (tmp == cur)
5282 break;
5283
5284 if (tmp->next != NULL) {
5285 tmp = tmp->next;
5286 continue;
5287 }
5288
5289 do {
5290 tmp = tmp->parent;
5291 if (tmp == NULL)
5292 break;
5293 if (tmp == cur) {
5294 tmp = NULL;
5295 break;
5296 }
5297 if (tmp->next != NULL) {
5298 tmp = tmp->next;
5299 break;
5300 }
5301 } while (tmp != NULL);
5302 }
5303 break;
5304 }
5305 case XML_ATTRIBUTE_NODE:{
5306 xmlAttrPtr attr = (xmlAttrPtr) cur;
5307 xmlNodePtr tmp = attr->children;
5308
5309 while (tmp != NULL) {
5310 if (tmp->type == XML_TEXT_NODE)
5311 xmlBufferCat(buffer, tmp->content);
5312 else
5313 xmlNodeBufGetContent(buffer, tmp);
5314 tmp = tmp->next;
5315 }
5316 break;
5317 }
5318 case XML_COMMENT_NODE:
5319 case XML_PI_NODE:
5320 xmlBufferCat(buffer, cur->content);
5321 break;
5322 case XML_ENTITY_REF_NODE:{
5323 xmlEntityPtr ent;
5324 xmlNodePtr tmp;
5325
5326 /* lookup entity declaration */
5327 ent = xmlGetDocEntity(cur->doc, cur->name);
5328 if (ent == NULL)
5329 return(-1);
5330
5331 /* an entity content can be any "well balanced chunk",
5332 * i.e. the result of the content [43] production:
5333 * http://www.w3.org/TR/REC-xml#NT-content
5334 * -> we iterate through child nodes and recursive call
5335 * xmlNodeGetContent() which handles all possible node types */
5336 tmp = ent->children;
5337 while (tmp) {
5338 xmlNodeBufGetContent(buffer, tmp);
5339 tmp = tmp->next;
5340 }
5341 break;
5342 }
5343 case XML_ENTITY_NODE:
5344 case XML_DOCUMENT_TYPE_NODE:
5345 case XML_NOTATION_NODE:
5346 case XML_DTD_NODE:
5347 case XML_XINCLUDE_START:
5348 case XML_XINCLUDE_END:
5349 break;
5350 case XML_DOCUMENT_NODE:
5351#ifdef LIBXML_DOCB_ENABLED
5352 case XML_DOCB_DOCUMENT_NODE:
5353#endif
5354 case XML_HTML_DOCUMENT_NODE:
5355 cur = cur->children;
5356 while (cur!= NULL) {
5357 if ((cur->type == XML_ELEMENT_NODE) ||
5358 (cur->type == XML_TEXT_NODE) ||
5359 (cur->type == XML_CDATA_SECTION_NODE)) {
5360 xmlNodeBufGetContent(buffer, cur);
5361 }
5362 cur = cur->next;
5363 }
5364 break;
5365 case XML_NAMESPACE_DECL:
5366 xmlBufferCat(buffer, ((xmlNsPtr) cur)->href);
5367 break;
5368 case XML_ELEMENT_DECL:
5369 case XML_ATTRIBUTE_DECL:
5370 case XML_ENTITY_DECL:
5371 break;
5372 }
5373 return(0);
5374}
5375/**
Owen Taylor3473f882001-02-23 17:55:21 +00005376 * xmlNodeGetContent:
5377 * @cur: the node being read
5378 *
5379 * Read the value of a node, this can be either the text carried
5380 * directly by this node if it's a TEXT node or the aggregate string
5381 * of the values carried by this node child's (TEXT and ENTITY_REF).
Daniel Veillardd1640922001-12-17 15:30:10 +00005382 * Entity references are substituted.
5383 * Returns a new #xmlChar * or NULL if no content is available.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00005384 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00005385 */
5386xmlChar *
Daniel Veillard7646b182002-04-20 06:41:40 +00005387xmlNodeGetContent(xmlNodePtr cur)
5388{
5389 if (cur == NULL)
5390 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005391 switch (cur->type) {
5392 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005393 case XML_ELEMENT_NODE:{
Daniel Veillard7646b182002-04-20 06:41:40 +00005394 xmlBufferPtr buffer;
5395 xmlChar *ret;
Owen Taylor3473f882001-02-23 17:55:21 +00005396
Daniel Veillard814a76d2003-01-23 18:24:20 +00005397 buffer = xmlBufferCreateSize(64);
Daniel Veillard7646b182002-04-20 06:41:40 +00005398 if (buffer == NULL)
5399 return (NULL);
Daniel Veillardc4696922003-10-19 21:47:14 +00005400 xmlNodeBufGetContent(buffer, cur);
Daniel Veillard7646b182002-04-20 06:41:40 +00005401 ret = buffer->content;
5402 buffer->content = NULL;
5403 xmlBufferFree(buffer);
5404 return (ret);
5405 }
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005406 case XML_ATTRIBUTE_NODE:
5407 return(xmlGetPropNodeValueInternal((xmlAttrPtr) cur));
Owen Taylor3473f882001-02-23 17:55:21 +00005408 case XML_COMMENT_NODE:
5409 case XML_PI_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005410 if (cur->content != NULL)
5411 return (xmlStrdup(cur->content));
5412 return (NULL);
5413 case XML_ENTITY_REF_NODE:{
5414 xmlEntityPtr ent;
Daniel Veillard7646b182002-04-20 06:41:40 +00005415 xmlBufferPtr buffer;
5416 xmlChar *ret;
5417
5418 /* lookup entity declaration */
5419 ent = xmlGetDocEntity(cur->doc, cur->name);
5420 if (ent == NULL)
5421 return (NULL);
5422
5423 buffer = xmlBufferCreate();
5424 if (buffer == NULL)
5425 return (NULL);
5426
Daniel Veillardc4696922003-10-19 21:47:14 +00005427 xmlNodeBufGetContent(buffer, cur);
Daniel Veillard7646b182002-04-20 06:41:40 +00005428
5429 ret = buffer->content;
5430 buffer->content = NULL;
5431 xmlBufferFree(buffer);
5432 return (ret);
5433 }
Owen Taylor3473f882001-02-23 17:55:21 +00005434 case XML_ENTITY_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005435 case XML_DOCUMENT_TYPE_NODE:
5436 case XML_NOTATION_NODE:
5437 case XML_DTD_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005438 case XML_XINCLUDE_START:
5439 case XML_XINCLUDE_END:
Daniel Veillard9adc0462003-03-24 18:39:54 +00005440 return (NULL);
5441 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005442#ifdef LIBXML_DOCB_ENABLED
Daniel Veillard7646b182002-04-20 06:41:40 +00005443 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005444#endif
Daniel Veillard9adc0462003-03-24 18:39:54 +00005445 case XML_HTML_DOCUMENT_NODE: {
Daniel Veillardc4696922003-10-19 21:47:14 +00005446 xmlBufferPtr buffer;
5447 xmlChar *ret;
Daniel Veillard9adc0462003-03-24 18:39:54 +00005448
Daniel Veillardc4696922003-10-19 21:47:14 +00005449 buffer = xmlBufferCreate();
5450 if (buffer == NULL)
5451 return (NULL);
5452
5453 xmlNodeBufGetContent(buffer, (xmlNodePtr) cur);
5454
5455 ret = buffer->content;
5456 buffer->content = NULL;
5457 xmlBufferFree(buffer);
5458 return (ret);
Daniel Veillard9adc0462003-03-24 18:39:54 +00005459 }
Daniel Veillard96c3a3b2002-10-14 15:39:04 +00005460 case XML_NAMESPACE_DECL: {
5461 xmlChar *tmp;
5462
5463 tmp = xmlStrdup(((xmlNsPtr) cur)->href);
5464 return (tmp);
5465 }
Owen Taylor3473f882001-02-23 17:55:21 +00005466 case XML_ELEMENT_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00005467 /* TODO !!! */
5468 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005469 case XML_ATTRIBUTE_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00005470 /* TODO !!! */
5471 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005472 case XML_ENTITY_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00005473 /* TODO !!! */
5474 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005475 case XML_CDATA_SECTION_NODE:
5476 case XML_TEXT_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005477 if (cur->content != NULL)
5478 return (xmlStrdup(cur->content));
5479 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005480 }
Daniel Veillard7646b182002-04-20 06:41:40 +00005481 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005482}
Daniel Veillard652327a2003-09-29 18:02:38 +00005483
Owen Taylor3473f882001-02-23 17:55:21 +00005484/**
5485 * xmlNodeSetContent:
5486 * @cur: the node being modified
5487 * @content: the new value of the content
5488 *
5489 * Replace the content of a node.
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00005490 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
5491 * references, but XML special chars need to be escaped first by using
5492 * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars().
Owen Taylor3473f882001-02-23 17:55:21 +00005493 */
5494void
5495xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) {
5496 if (cur == NULL) {
5497#ifdef DEBUG_TREE
5498 xmlGenericError(xmlGenericErrorContext,
5499 "xmlNodeSetContent : node == NULL\n");
5500#endif
5501 return;
5502 }
5503 switch (cur->type) {
5504 case XML_DOCUMENT_FRAG_NODE:
5505 case XML_ELEMENT_NODE:
Daniel Veillard2c748c62002-01-16 15:37:50 +00005506 case XML_ATTRIBUTE_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005507 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5508 cur->children = xmlStringGetNodeList(cur->doc, content);
5509 UPDATE_LAST_CHILD_AND_PARENT(cur)
5510 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005511 case XML_TEXT_NODE:
5512 case XML_CDATA_SECTION_NODE:
5513 case XML_ENTITY_REF_NODE:
5514 case XML_ENTITY_NODE:
5515 case XML_PI_NODE:
5516 case XML_COMMENT_NODE:
Daniel Veillard8874b942005-08-25 13:19:21 +00005517 if ((cur->content != NULL) &&
5518 (cur->content != (xmlChar *) &(cur->properties))) {
William M. Brack7762bb12004-01-04 14:49:01 +00005519 if (!((cur->doc != NULL) && (cur->doc->dict != NULL) &&
Daniel Veillardc587bce2005-05-10 15:28:08 +00005520 (xmlDictOwns(cur->doc->dict, cur->content))))
William M. Brack7762bb12004-01-04 14:49:01 +00005521 xmlFree(cur->content);
Daniel Veillardaa6de472008-08-25 14:53:31 +00005522 }
Owen Taylor3473f882001-02-23 17:55:21 +00005523 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5524 cur->last = cur->children = NULL;
5525 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005526 cur->content = xmlStrdup(content);
Daniel Veillardaa6de472008-08-25 14:53:31 +00005527 } else
Owen Taylor3473f882001-02-23 17:55:21 +00005528 cur->content = NULL;
Daniel Veillard8874b942005-08-25 13:19:21 +00005529 cur->properties = NULL;
5530 cur->nsDef = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00005531 break;
5532 case XML_DOCUMENT_NODE:
5533 case XML_HTML_DOCUMENT_NODE:
5534 case XML_DOCUMENT_TYPE_NODE:
5535 case XML_XINCLUDE_START:
5536 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005537#ifdef LIBXML_DOCB_ENABLED
5538 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005539#endif
5540 break;
5541 case XML_NOTATION_NODE:
5542 break;
5543 case XML_DTD_NODE:
5544 break;
5545 case XML_NAMESPACE_DECL:
5546 break;
5547 case XML_ELEMENT_DECL:
5548 /* TODO !!! */
5549 break;
5550 case XML_ATTRIBUTE_DECL:
5551 /* TODO !!! */
5552 break;
5553 case XML_ENTITY_DECL:
5554 /* TODO !!! */
5555 break;
5556 }
5557}
5558
Daniel Veillard652327a2003-09-29 18:02:38 +00005559#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00005560/**
5561 * xmlNodeSetContentLen:
5562 * @cur: the node being modified
5563 * @content: the new value of the content
5564 * @len: the size of @content
5565 *
5566 * Replace the content of a node.
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00005567 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
5568 * references, but XML special chars need to be escaped first by using
5569 * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars().
Owen Taylor3473f882001-02-23 17:55:21 +00005570 */
5571void
5572xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5573 if (cur == NULL) {
5574#ifdef DEBUG_TREE
5575 xmlGenericError(xmlGenericErrorContext,
5576 "xmlNodeSetContentLen : node == NULL\n");
5577#endif
5578 return;
5579 }
5580 switch (cur->type) {
5581 case XML_DOCUMENT_FRAG_NODE:
5582 case XML_ELEMENT_NODE:
Daniel Veillard2c748c62002-01-16 15:37:50 +00005583 case XML_ATTRIBUTE_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005584 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5585 cur->children = xmlStringLenGetNodeList(cur->doc, content, len);
5586 UPDATE_LAST_CHILD_AND_PARENT(cur)
5587 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005588 case XML_TEXT_NODE:
5589 case XML_CDATA_SECTION_NODE:
5590 case XML_ENTITY_REF_NODE:
5591 case XML_ENTITY_NODE:
5592 case XML_PI_NODE:
5593 case XML_COMMENT_NODE:
5594 case XML_NOTATION_NODE:
Daniel Veillard8874b942005-08-25 13:19:21 +00005595 if ((cur->content != NULL) &&
5596 (cur->content != (xmlChar *) &(cur->properties))) {
5597 if (!((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5598 (xmlDictOwns(cur->doc->dict, cur->content))))
5599 xmlFree(cur->content);
Daniel Veillardaa6de472008-08-25 14:53:31 +00005600 }
Owen Taylor3473f882001-02-23 17:55:21 +00005601 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5602 cur->children = cur->last = NULL;
5603 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005604 cur->content = xmlStrndup(content, len);
Daniel Veillardaa6de472008-08-25 14:53:31 +00005605 } else
Owen Taylor3473f882001-02-23 17:55:21 +00005606 cur->content = NULL;
Daniel Veillard8874b942005-08-25 13:19:21 +00005607 cur->properties = NULL;
5608 cur->nsDef = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00005609 break;
5610 case XML_DOCUMENT_NODE:
5611 case XML_DTD_NODE:
5612 case XML_HTML_DOCUMENT_NODE:
5613 case XML_DOCUMENT_TYPE_NODE:
5614 case XML_NAMESPACE_DECL:
5615 case XML_XINCLUDE_START:
5616 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005617#ifdef LIBXML_DOCB_ENABLED
5618 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005619#endif
5620 break;
5621 case XML_ELEMENT_DECL:
5622 /* TODO !!! */
5623 break;
5624 case XML_ATTRIBUTE_DECL:
5625 /* TODO !!! */
5626 break;
5627 case XML_ENTITY_DECL:
5628 /* TODO !!! */
5629 break;
5630 }
5631}
Daniel Veillard652327a2003-09-29 18:02:38 +00005632#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005633
5634/**
5635 * xmlNodeAddContentLen:
5636 * @cur: the node being modified
5637 * @content: extra content
5638 * @len: the size of @content
Daniel Veillardaa6de472008-08-25 14:53:31 +00005639 *
Owen Taylor3473f882001-02-23 17:55:21 +00005640 * Append the extra substring to the node content.
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00005641 * NOTE: In contrast to xmlNodeSetContentLen(), @content is supposed to be
5642 * raw text, so unescaped XML special chars are allowed, entity
5643 * references are not supported.
Owen Taylor3473f882001-02-23 17:55:21 +00005644 */
5645void
5646xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5647 if (cur == NULL) {
5648#ifdef DEBUG_TREE
5649 xmlGenericError(xmlGenericErrorContext,
5650 "xmlNodeAddContentLen : node == NULL\n");
5651#endif
5652 return;
5653 }
5654 if (len <= 0) return;
5655 switch (cur->type) {
5656 case XML_DOCUMENT_FRAG_NODE:
5657 case XML_ELEMENT_NODE: {
Daniel Veillardacb2bda2002-01-13 16:15:43 +00005658 xmlNodePtr last, newNode, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00005659
Daniel Veillard7db37732001-07-12 01:20:08 +00005660 last = cur->last;
Owen Taylor3473f882001-02-23 17:55:21 +00005661 newNode = xmlNewTextLen(content, len);
5662 if (newNode != NULL) {
Daniel Veillardacb2bda2002-01-13 16:15:43 +00005663 tmp = xmlAddChild(cur, newNode);
5664 if (tmp != newNode)
5665 return;
Owen Taylor3473f882001-02-23 17:55:21 +00005666 if ((last != NULL) && (last->next == newNode)) {
5667 xmlTextMerge(last, newNode);
5668 }
5669 }
5670 break;
5671 }
5672 case XML_ATTRIBUTE_NODE:
5673 break;
5674 case XML_TEXT_NODE:
5675 case XML_CDATA_SECTION_NODE:
5676 case XML_ENTITY_REF_NODE:
5677 case XML_ENTITY_NODE:
5678 case XML_PI_NODE:
5679 case XML_COMMENT_NODE:
5680 case XML_NOTATION_NODE:
5681 if (content != NULL) {
Daniel Veillard8874b942005-08-25 13:19:21 +00005682 if ((cur->content == (xmlChar *) &(cur->properties)) ||
5683 ((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5684 xmlDictOwns(cur->doc->dict, cur->content))) {
5685 cur->content = xmlStrncatNew(cur->content, content, len);
5686 cur->properties = NULL;
5687 cur->nsDef = NULL;
William M. Brack7762bb12004-01-04 14:49:01 +00005688 break;
5689 }
Owen Taylor3473f882001-02-23 17:55:21 +00005690 cur->content = xmlStrncat(cur->content, content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00005691 }
5692 case XML_DOCUMENT_NODE:
5693 case XML_DTD_NODE:
5694 case XML_HTML_DOCUMENT_NODE:
5695 case XML_DOCUMENT_TYPE_NODE:
5696 case XML_NAMESPACE_DECL:
5697 case XML_XINCLUDE_START:
5698 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005699#ifdef LIBXML_DOCB_ENABLED
5700 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005701#endif
5702 break;
5703 case XML_ELEMENT_DECL:
5704 case XML_ATTRIBUTE_DECL:
5705 case XML_ENTITY_DECL:
5706 break;
5707 }
5708}
5709
5710/**
5711 * xmlNodeAddContent:
5712 * @cur: the node being modified
5713 * @content: extra content
Daniel Veillardaa6de472008-08-25 14:53:31 +00005714 *
Owen Taylor3473f882001-02-23 17:55:21 +00005715 * Append the extra substring to the node content.
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00005716 * NOTE: In contrast to xmlNodeSetContent(), @content is supposed to be
5717 * raw text, so unescaped XML special chars are allowed, entity
5718 * references are not supported.
Owen Taylor3473f882001-02-23 17:55:21 +00005719 */
5720void
5721xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) {
5722 int len;
5723
5724 if (cur == NULL) {
5725#ifdef DEBUG_TREE
5726 xmlGenericError(xmlGenericErrorContext,
5727 "xmlNodeAddContent : node == NULL\n");
5728#endif
5729 return;
5730 }
5731 if (content == NULL) return;
5732 len = xmlStrlen(content);
5733 xmlNodeAddContentLen(cur, content, len);
5734}
5735
5736/**
5737 * xmlTextMerge:
5738 * @first: the first text node
5739 * @second: the second text node being merged
Daniel Veillardaa6de472008-08-25 14:53:31 +00005740 *
Owen Taylor3473f882001-02-23 17:55:21 +00005741 * Merge two text nodes into one
5742 * Returns the first text node augmented
5743 */
5744xmlNodePtr
5745xmlTextMerge(xmlNodePtr first, xmlNodePtr second) {
5746 if (first == NULL) return(second);
5747 if (second == NULL) return(first);
5748 if (first->type != XML_TEXT_NODE) return(first);
5749 if (second->type != XML_TEXT_NODE) return(first);
5750 if (second->name != first->name)
5751 return(first);
Owen Taylor3473f882001-02-23 17:55:21 +00005752 xmlNodeAddContent(first, second->content);
Owen Taylor3473f882001-02-23 17:55:21 +00005753 xmlUnlinkNode(second);
5754 xmlFreeNode(second);
5755 return(first);
5756}
5757
Daniel Veillardf1a27c62006-10-13 22:33:03 +00005758#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00005759/**
5760 * xmlGetNsList:
5761 * @doc: the document
5762 * @node: the current node
5763 *
5764 * Search all the namespace applying to a given element.
Daniel Veillardd1640922001-12-17 15:30:10 +00005765 * Returns an NULL terminated array of all the #xmlNsPtr found
Owen Taylor3473f882001-02-23 17:55:21 +00005766 * that need to be freed by the caller or NULL if no
5767 * namespace if defined
5768 */
5769xmlNsPtr *
Daniel Veillard77044732001-06-29 21:31:07 +00005770xmlGetNsList(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node)
5771{
Owen Taylor3473f882001-02-23 17:55:21 +00005772 xmlNsPtr cur;
5773 xmlNsPtr *ret = NULL;
5774 int nbns = 0;
5775 int maxns = 10;
5776 int i;
5777
5778 while (node != NULL) {
Daniel Veillard77044732001-06-29 21:31:07 +00005779 if (node->type == XML_ELEMENT_NODE) {
5780 cur = node->nsDef;
5781 while (cur != NULL) {
5782 if (ret == NULL) {
5783 ret =
5784 (xmlNsPtr *) xmlMalloc((maxns + 1) *
5785 sizeof(xmlNsPtr));
5786 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005787 xmlTreeErrMemory("getting namespace list");
Daniel Veillard77044732001-06-29 21:31:07 +00005788 return (NULL);
5789 }
5790 ret[nbns] = NULL;
5791 }
5792 for (i = 0; i < nbns; i++) {
5793 if ((cur->prefix == ret[i]->prefix) ||
5794 (xmlStrEqual(cur->prefix, ret[i]->prefix)))
5795 break;
5796 }
5797 if (i >= nbns) {
5798 if (nbns >= maxns) {
5799 maxns *= 2;
5800 ret = (xmlNsPtr *) xmlRealloc(ret,
5801 (maxns +
5802 1) *
5803 sizeof(xmlNsPtr));
5804 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005805 xmlTreeErrMemory("getting namespace list");
Daniel Veillard77044732001-06-29 21:31:07 +00005806 return (NULL);
5807 }
5808 }
5809 ret[nbns++] = cur;
5810 ret[nbns] = NULL;
5811 }
Owen Taylor3473f882001-02-23 17:55:21 +00005812
Daniel Veillard77044732001-06-29 21:31:07 +00005813 cur = cur->next;
5814 }
5815 }
5816 node = node->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00005817 }
Daniel Veillard77044732001-06-29 21:31:07 +00005818 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00005819}
Daniel Veillard652327a2003-09-29 18:02:38 +00005820#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005821
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005822/*
5823* xmlTreeEnsureXMLDecl:
5824* @doc: the doc
Daniel Veillardaa6de472008-08-25 14:53:31 +00005825*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005826* Ensures that there is an XML namespace declaration on the doc.
Daniel Veillardaa6de472008-08-25 14:53:31 +00005827*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005828* Returns the XML ns-struct or NULL on API and internal errors.
5829*/
5830static xmlNsPtr
5831xmlTreeEnsureXMLDecl(xmlDocPtr doc)
5832{
5833 if (doc == NULL)
5834 return (NULL);
5835 if (doc->oldNs != NULL)
5836 return (doc->oldNs);
5837 {
5838 xmlNsPtr ns;
5839 ns = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5840 if (ns == NULL) {
5841 xmlTreeErrMemory(
5842 "allocating the XML namespace");
5843 return (NULL);
5844 }
5845 memset(ns, 0, sizeof(xmlNs));
5846 ns->type = XML_LOCAL_NAMESPACE;
Daniel Veillardaa6de472008-08-25 14:53:31 +00005847 ns->href = xmlStrdup(XML_XML_NAMESPACE);
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005848 ns->prefix = xmlStrdup((const xmlChar *)"xml");
5849 doc->oldNs = ns;
5850 return (ns);
5851 }
5852}
5853
Owen Taylor3473f882001-02-23 17:55:21 +00005854/**
5855 * xmlSearchNs:
5856 * @doc: the document
5857 * @node: the current node
Daniel Veillard77851712001-02-27 21:54:07 +00005858 * @nameSpace: the namespace prefix
Owen Taylor3473f882001-02-23 17:55:21 +00005859 *
5860 * Search a Ns registered under a given name space for a document.
5861 * recurse on the parents until it finds the defined namespace
5862 * or return NULL otherwise.
5863 * @nameSpace can be NULL, this is a search for the default namespace.
5864 * We don't allow to cross entities boundaries. If you don't declare
5865 * the namespace within those you will be in troubles !!! A warning
5866 * is generated to cover this case.
5867 *
5868 * Returns the namespace pointer or NULL.
5869 */
5870xmlNsPtr
5871xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00005872
Owen Taylor3473f882001-02-23 17:55:21 +00005873 xmlNsPtr cur;
Daniel Veillardf4e56292003-10-28 14:27:41 +00005874 xmlNodePtr orig = node;
Owen Taylor3473f882001-02-23 17:55:21 +00005875
5876 if (node == NULL) return(NULL);
5877 if ((nameSpace != NULL) &&
5878 (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) {
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00005879 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
5880 /*
5881 * The XML-1.0 namespace is normally held on the root
5882 * element. In this case exceptionally create it on the
5883 * node element.
5884 */
5885 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5886 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005887 xmlTreeErrMemory("searching namespace");
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00005888 return(NULL);
5889 }
5890 memset(cur, 0, sizeof(xmlNs));
5891 cur->type = XML_LOCAL_NAMESPACE;
Daniel Veillardaa6de472008-08-25 14:53:31 +00005892 cur->href = xmlStrdup(XML_XML_NAMESPACE);
5893 cur->prefix = xmlStrdup((const xmlChar *)"xml");
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00005894 cur->next = node->nsDef;
5895 node->nsDef = cur;
5896 return(cur);
5897 }
Daniel Veillard11ce4002006-03-10 00:36:23 +00005898 if (doc == NULL) {
5899 doc = node->doc;
5900 if (doc == NULL)
5901 return(NULL);
5902 }
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005903 /*
5904 * Return the XML namespace declaration held by the doc.
5905 */
5906 if (doc->oldNs == NULL)
5907 return(xmlTreeEnsureXMLDecl(doc));
5908 else
5909 return(doc->oldNs);
Owen Taylor3473f882001-02-23 17:55:21 +00005910 }
5911 while (node != NULL) {
5912 if ((node->type == XML_ENTITY_REF_NODE) ||
5913 (node->type == XML_ENTITY_NODE) ||
5914 (node->type == XML_ENTITY_DECL))
5915 return(NULL);
5916 if (node->type == XML_ELEMENT_NODE) {
5917 cur = node->nsDef;
5918 while (cur != NULL) {
5919 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
5920 (cur->href != NULL))
5921 return(cur);
5922 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
5923 (cur->href != NULL) &&
5924 (xmlStrEqual(cur->prefix, nameSpace)))
5925 return(cur);
5926 cur = cur->next;
5927 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00005928 if (orig != node) {
Daniel Veillardf4e56292003-10-28 14:27:41 +00005929 cur = node->ns;
5930 if (cur != NULL) {
5931 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
5932 (cur->href != NULL))
5933 return(cur);
5934 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
5935 (cur->href != NULL) &&
5936 (xmlStrEqual(cur->prefix, nameSpace)))
5937 return(cur);
5938 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00005939 }
Owen Taylor3473f882001-02-23 17:55:21 +00005940 }
5941 node = node->parent;
5942 }
5943 return(NULL);
5944}
5945
5946/**
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005947 * xmlNsInScope:
5948 * @doc: the document
5949 * @node: the current node
5950 * @ancestor: the ancestor carrying the namespace
5951 * @prefix: the namespace prefix
5952 *
5953 * Verify that the given namespace held on @ancestor is still in scope
5954 * on node.
Daniel Veillardaa6de472008-08-25 14:53:31 +00005955 *
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005956 * Returns 1 if true, 0 if false and -1 in case of error.
5957 */
5958static int
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005959xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node,
5960 xmlNodePtr ancestor, const xmlChar * prefix)
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005961{
5962 xmlNsPtr tst;
5963
5964 while ((node != NULL) && (node != ancestor)) {
5965 if ((node->type == XML_ENTITY_REF_NODE) ||
5966 (node->type == XML_ENTITY_NODE) ||
5967 (node->type == XML_ENTITY_DECL))
5968 return (-1);
5969 if (node->type == XML_ELEMENT_NODE) {
5970 tst = node->nsDef;
5971 while (tst != NULL) {
5972 if ((tst->prefix == NULL)
5973 && (prefix == NULL))
5974 return (0);
5975 if ((tst->prefix != NULL)
5976 && (prefix != NULL)
5977 && (xmlStrEqual(tst->prefix, prefix)))
5978 return (0);
5979 tst = tst->next;
5980 }
5981 }
5982 node = node->parent;
5983 }
5984 if (node != ancestor)
5985 return (-1);
5986 return (1);
5987}
Daniel Veillardaa6de472008-08-25 14:53:31 +00005988
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005989/**
Owen Taylor3473f882001-02-23 17:55:21 +00005990 * xmlSearchNsByHref:
5991 * @doc: the document
5992 * @node: the current node
5993 * @href: the namespace value
5994 *
5995 * Search a Ns aliasing a given URI. Recurse on the parents until it finds
5996 * the defined namespace or return NULL otherwise.
5997 * Returns the namespace pointer or NULL.
5998 */
5999xmlNsPtr
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006000xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href)
6001{
Owen Taylor3473f882001-02-23 17:55:21 +00006002 xmlNsPtr cur;
6003 xmlNodePtr orig = node;
Daniel Veillard62040be2004-05-17 03:17:26 +00006004 int is_attr;
Owen Taylor3473f882001-02-23 17:55:21 +00006005
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006006 if ((node == NULL) || (href == NULL))
6007 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006008 if (xmlStrEqual(href, XML_XML_NAMESPACE)) {
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006009 /*
6010 * Only the document can hold the XML spec namespace.
6011 */
6012 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
6013 /*
6014 * The XML-1.0 namespace is normally held on the root
6015 * element. In this case exceptionally create it on the
6016 * node element.
6017 */
6018 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
6019 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006020 xmlTreeErrMemory("searching namespace");
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006021 return (NULL);
6022 }
6023 memset(cur, 0, sizeof(xmlNs));
6024 cur->type = XML_LOCAL_NAMESPACE;
6025 cur->href = xmlStrdup(XML_XML_NAMESPACE);
6026 cur->prefix = xmlStrdup((const xmlChar *) "xml");
6027 cur->next = node->nsDef;
6028 node->nsDef = cur;
6029 return (cur);
6030 }
Daniel Veillard11ce4002006-03-10 00:36:23 +00006031 if (doc == NULL) {
6032 doc = node->doc;
6033 if (doc == NULL)
6034 return(NULL);
6035 }
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00006036 /*
6037 * Return the XML namespace declaration held by the doc.
6038 */
6039 if (doc->oldNs == NULL)
6040 return(xmlTreeEnsureXMLDecl(doc));
6041 else
Daniel Veillardaa6de472008-08-25 14:53:31 +00006042 return(doc->oldNs);
Owen Taylor3473f882001-02-23 17:55:21 +00006043 }
Daniel Veillard62040be2004-05-17 03:17:26 +00006044 is_attr = (node->type == XML_ATTRIBUTE_NODE);
Owen Taylor3473f882001-02-23 17:55:21 +00006045 while (node != NULL) {
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006046 if ((node->type == XML_ENTITY_REF_NODE) ||
6047 (node->type == XML_ENTITY_NODE) ||
6048 (node->type == XML_ENTITY_DECL))
6049 return (NULL);
6050 if (node->type == XML_ELEMENT_NODE) {
6051 cur = node->nsDef;
6052 while (cur != NULL) {
6053 if ((cur->href != NULL) && (href != NULL) &&
6054 (xmlStrEqual(cur->href, href))) {
Daniel Veillard62040be2004-05-17 03:17:26 +00006055 if (((!is_attr) || (cur->prefix != NULL)) &&
Kasimier T. Buchcikba70cc02005-02-28 10:28:21 +00006056 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006057 return (cur);
6058 }
6059 cur = cur->next;
6060 }
Daniel Veillardf4e56292003-10-28 14:27:41 +00006061 if (orig != node) {
6062 cur = node->ns;
6063 if (cur != NULL) {
6064 if ((cur->href != NULL) && (href != NULL) &&
6065 (xmlStrEqual(cur->href, href))) {
Daniel Veillard62040be2004-05-17 03:17:26 +00006066 if (((!is_attr) || (cur->prefix != NULL)) &&
Kasimier T. Buchcikba70cc02005-02-28 10:28:21 +00006067 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
Daniel Veillardf4e56292003-10-28 14:27:41 +00006068 return (cur);
6069 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006070 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00006071 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006072 }
6073 node = node->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00006074 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006075 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006076}
6077
6078/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00006079 * xmlNewReconciliedNs:
Owen Taylor3473f882001-02-23 17:55:21 +00006080 * @doc: the document
6081 * @tree: a node expected to hold the new namespace
6082 * @ns: the original namespace
6083 *
6084 * This function tries to locate a namespace definition in a tree
6085 * ancestors, or create a new namespace definition node similar to
6086 * @ns trying to reuse the same prefix. However if the given prefix is
6087 * null (default namespace) or reused within the subtree defined by
6088 * @tree or on one of its ancestors then a new prefix is generated.
6089 * Returns the (new) namespace definition or NULL in case of error
6090 */
Daniel Veillard8ed10722009-08-20 19:17:36 +02006091static xmlNsPtr
Owen Taylor3473f882001-02-23 17:55:21 +00006092xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) {
6093 xmlNsPtr def;
6094 xmlChar prefix[50];
6095 int counter = 1;
6096
6097 if (tree == NULL) {
6098#ifdef DEBUG_TREE
6099 xmlGenericError(xmlGenericErrorContext,
6100 "xmlNewReconciliedNs : tree == NULL\n");
6101#endif
6102 return(NULL);
6103 }
Daniel Veillardce244ad2004-11-05 10:03:46 +00006104 if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006105#ifdef DEBUG_TREE
6106 xmlGenericError(xmlGenericErrorContext,
6107 "xmlNewReconciliedNs : ns == NULL\n");
6108#endif
6109 return(NULL);
6110 }
6111 /*
6112 * Search an existing namespace definition inherited.
6113 */
6114 def = xmlSearchNsByHref(doc, tree, ns->href);
6115 if (def != NULL)
6116 return(def);
6117
6118 /*
6119 * Find a close prefix which is not already in use.
6120 * Let's strip namespace prefixes longer than 20 chars !
6121 */
Daniel Veillardf742d342002-03-07 00:05:35 +00006122 if (ns->prefix == NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00006123 snprintf((char *) prefix, sizeof(prefix), "default");
Daniel Veillardf742d342002-03-07 00:05:35 +00006124 else
William M. Brack13dfa872004-09-18 04:52:08 +00006125 snprintf((char *) prefix, sizeof(prefix), "%.20s", (char *)ns->prefix);
Daniel Veillardf742d342002-03-07 00:05:35 +00006126
Owen Taylor3473f882001-02-23 17:55:21 +00006127 def = xmlSearchNs(doc, tree, prefix);
6128 while (def != NULL) {
6129 if (counter > 1000) return(NULL);
Daniel Veillardf742d342002-03-07 00:05:35 +00006130 if (ns->prefix == NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00006131 snprintf((char *) prefix, sizeof(prefix), "default%d", counter++);
Daniel Veillardf742d342002-03-07 00:05:35 +00006132 else
William M. Brack13dfa872004-09-18 04:52:08 +00006133 snprintf((char *) prefix, sizeof(prefix), "%.20s%d",
Daniel Veillardaa6de472008-08-25 14:53:31 +00006134 (char *)ns->prefix, counter++);
Owen Taylor3473f882001-02-23 17:55:21 +00006135 def = xmlSearchNs(doc, tree, prefix);
6136 }
6137
6138 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00006139 * OK, now we are ready to create a new one.
Owen Taylor3473f882001-02-23 17:55:21 +00006140 */
6141 def = xmlNewNs(tree, ns->href, prefix);
6142 return(def);
6143}
6144
Daniel Veillard652327a2003-09-29 18:02:38 +00006145#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00006146/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00006147 * xmlReconciliateNs:
Owen Taylor3473f882001-02-23 17:55:21 +00006148 * @doc: the document
6149 * @tree: a node defining the subtree to reconciliate
6150 *
6151 * This function checks that all the namespaces declared within the given
6152 * tree are properly declared. This is needed for example after Copy or Cut
6153 * and then paste operations. The subtree may still hold pointers to
6154 * namespace declarations outside the subtree or invalid/masked. As much
Daniel Veillardd1640922001-12-17 15:30:10 +00006155 * as possible the function try to reuse the existing namespaces found in
Owen Taylor3473f882001-02-23 17:55:21 +00006156 * the new environment. If not possible the new namespaces are redeclared
6157 * on @tree at the top of the given subtree.
6158 * Returns the number of namespace declarations created or -1 in case of error.
6159 */
6160int
6161xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) {
6162 xmlNsPtr *oldNs = NULL;
6163 xmlNsPtr *newNs = NULL;
6164 int sizeCache = 0;
6165 int nbCache = 0;
6166
6167 xmlNsPtr n;
6168 xmlNodePtr node = tree;
6169 xmlAttrPtr attr;
6170 int ret = 0, i;
6171
Daniel Veillardce244ad2004-11-05 10:03:46 +00006172 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) return(-1);
6173 if ((doc == NULL) || (doc->type != XML_DOCUMENT_NODE)) return(-1);
6174 if (node->doc != doc) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006175 while (node != NULL) {
6176 /*
6177 * Reconciliate the node namespace
6178 */
6179 if (node->ns != NULL) {
6180 /*
6181 * initialize the cache if needed
6182 */
6183 if (sizeCache == 0) {
6184 sizeCache = 10;
6185 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6186 sizeof(xmlNsPtr));
6187 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006188 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00006189 return(-1);
6190 }
6191 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6192 sizeof(xmlNsPtr));
6193 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006194 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00006195 xmlFree(oldNs);
6196 return(-1);
6197 }
6198 }
6199 for (i = 0;i < nbCache;i++) {
6200 if (oldNs[i] == node->ns) {
6201 node->ns = newNs[i];
6202 break;
6203 }
6204 }
6205 if (i == nbCache) {
6206 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00006207 * OK we need to recreate a new namespace definition
Owen Taylor3473f882001-02-23 17:55:21 +00006208 */
6209 n = xmlNewReconciliedNs(doc, tree, node->ns);
6210 if (n != NULL) { /* :-( what if else ??? */
6211 /*
6212 * check if we need to grow the cache buffers.
6213 */
6214 if (sizeCache <= nbCache) {
6215 sizeCache *= 2;
6216 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
6217 sizeof(xmlNsPtr));
6218 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006219 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00006220 xmlFree(newNs);
6221 return(-1);
6222 }
6223 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
6224 sizeof(xmlNsPtr));
6225 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006226 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00006227 xmlFree(oldNs);
6228 return(-1);
6229 }
6230 }
6231 newNs[nbCache] = n;
6232 oldNs[nbCache++] = node->ns;
6233 node->ns = n;
6234 }
6235 }
6236 }
6237 /*
6238 * now check for namespace hold by attributes on the node.
6239 */
Daniel Veillardb5f11972006-10-14 08:46:40 +00006240 if (node->type == XML_ELEMENT_NODE) {
6241 attr = node->properties;
6242 while (attr != NULL) {
6243 if (attr->ns != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00006244 /*
Daniel Veillardb5f11972006-10-14 08:46:40 +00006245 * initialize the cache if needed
Owen Taylor3473f882001-02-23 17:55:21 +00006246 */
Daniel Veillardb5f11972006-10-14 08:46:40 +00006247 if (sizeCache == 0) {
6248 sizeCache = 10;
6249 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6250 sizeof(xmlNsPtr));
6251 if (oldNs == NULL) {
6252 xmlTreeErrMemory("fixing namespaces");
6253 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006254 }
Daniel Veillardb5f11972006-10-14 08:46:40 +00006255 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6256 sizeof(xmlNsPtr));
6257 if (newNs == NULL) {
6258 xmlTreeErrMemory("fixing namespaces");
6259 xmlFree(oldNs);
6260 return(-1);
6261 }
6262 }
6263 for (i = 0;i < nbCache;i++) {
6264 if (oldNs[i] == attr->ns) {
6265 attr->ns = newNs[i];
6266 break;
6267 }
6268 }
6269 if (i == nbCache) {
6270 /*
6271 * OK we need to recreate a new namespace definition
6272 */
6273 n = xmlNewReconciliedNs(doc, tree, attr->ns);
6274 if (n != NULL) { /* :-( what if else ??? */
6275 /*
6276 * check if we need to grow the cache buffers.
6277 */
6278 if (sizeCache <= nbCache) {
6279 sizeCache *= 2;
6280 oldNs = (xmlNsPtr *) xmlRealloc(oldNs,
6281 sizeCache * sizeof(xmlNsPtr));
6282 if (oldNs == NULL) {
6283 xmlTreeErrMemory("fixing namespaces");
6284 xmlFree(newNs);
6285 return(-1);
6286 }
6287 newNs = (xmlNsPtr *) xmlRealloc(newNs,
6288 sizeCache * sizeof(xmlNsPtr));
6289 if (newNs == NULL) {
6290 xmlTreeErrMemory("fixing namespaces");
6291 xmlFree(oldNs);
6292 return(-1);
6293 }
6294 }
6295 newNs[nbCache] = n;
6296 oldNs[nbCache++] = attr->ns;
6297 attr->ns = n;
6298 }
Owen Taylor3473f882001-02-23 17:55:21 +00006299 }
6300 }
Daniel Veillardb5f11972006-10-14 08:46:40 +00006301 attr = attr->next;
Owen Taylor3473f882001-02-23 17:55:21 +00006302 }
Owen Taylor3473f882001-02-23 17:55:21 +00006303 }
6304
6305 /*
6306 * Browse the full subtree, deep first
6307 */
Daniel Veillardb5f11972006-10-14 08:46:40 +00006308 if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006309 /* deep first */
6310 node = node->children;
6311 } else if ((node != tree) && (node->next != NULL)) {
6312 /* then siblings */
6313 node = node->next;
6314 } else if (node != tree) {
6315 /* go up to parents->next if needed */
6316 while (node != tree) {
6317 if (node->parent != NULL)
6318 node = node->parent;
6319 if ((node != tree) && (node->next != NULL)) {
6320 node = node->next;
6321 break;
6322 }
6323 if (node->parent == NULL) {
6324 node = NULL;
6325 break;
6326 }
6327 }
6328 /* exit condition */
Daniel Veillardaa6de472008-08-25 14:53:31 +00006329 if (node == tree)
Owen Taylor3473f882001-02-23 17:55:21 +00006330 node = NULL;
Daniel Veillard1e774382002-03-06 17:35:40 +00006331 } else
6332 break;
Owen Taylor3473f882001-02-23 17:55:21 +00006333 }
Daniel Veillardf742d342002-03-07 00:05:35 +00006334 if (oldNs != NULL)
6335 xmlFree(oldNs);
6336 if (newNs != NULL)
6337 xmlFree(newNs);
Owen Taylor3473f882001-02-23 17:55:21 +00006338 return(ret);
6339}
Daniel Veillard652327a2003-09-29 18:02:38 +00006340#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00006341
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006342static xmlAttrPtr
6343xmlGetPropNodeInternal(xmlNodePtr node, const xmlChar *name,
6344 const xmlChar *nsName, int useDTD)
6345{
6346 xmlAttrPtr prop;
6347
6348 if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL))
6349 return(NULL);
6350
6351 if (node->properties != NULL) {
6352 prop = node->properties;
6353 if (nsName == NULL) {
6354 /*
6355 * We want the attr to be in no namespace.
6356 */
6357 do {
6358 if ((prop->ns == NULL) && xmlStrEqual(prop->name, name)) {
6359 return(prop);
6360 }
6361 prop = prop->next;
6362 } while (prop != NULL);
6363 } else {
6364 /*
6365 * We want the attr to be in the specified namespace.
6366 */
6367 do {
6368 if ((prop->ns != NULL) && xmlStrEqual(prop->name, name) &&
6369 ((prop->ns->href == nsName) ||
6370 xmlStrEqual(prop->ns->href, nsName)))
6371 {
6372 return(prop);
6373 }
6374 prop = prop->next;
6375 } while (prop != NULL);
6376 }
6377 }
6378
6379#ifdef LIBXML_TREE_ENABLED
6380 if (! useDTD)
6381 return(NULL);
6382 /*
6383 * Check if there is a default/fixed attribute declaration in
6384 * the internal or external subset.
Daniel Veillardaa6de472008-08-25 14:53:31 +00006385 */
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006386 if ((node->doc != NULL) && (node->doc->intSubset != NULL)) {
6387 xmlDocPtr doc = node->doc;
6388 xmlAttributePtr attrDecl = NULL;
6389 xmlChar *elemQName, *tmpstr = NULL;
6390
6391 /*
Daniel Veillardaa6de472008-08-25 14:53:31 +00006392 * We need the QName of the element for the DTD-lookup.
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006393 */
6394 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
6395 tmpstr = xmlStrdup(node->ns->prefix);
6396 tmpstr = xmlStrcat(tmpstr, BAD_CAST ":");
6397 tmpstr = xmlStrcat(tmpstr, node->name);
6398 if (tmpstr == NULL)
6399 return(NULL);
6400 elemQName = tmpstr;
6401 } else
6402 elemQName = (xmlChar *) node->name;
6403 if (nsName == NULL) {
6404 /*
6405 * The common and nice case: Attr in no namespace.
6406 */
6407 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset,
6408 elemQName, name, NULL);
6409 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
6410 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset,
6411 elemQName, name, NULL);
6412 }
6413 } else {
6414 xmlNsPtr *nsList, *cur;
6415
6416 /*
6417 * The ugly case: Search using the prefixes of in-scope
6418 * ns-decls corresponding to @nsName.
6419 */
6420 nsList = xmlGetNsList(node->doc, node);
6421 if (nsList == NULL) {
6422 if (tmpstr != NULL)
6423 xmlFree(tmpstr);
6424 return(NULL);
6425 }
6426 cur = nsList;
6427 while (*cur != NULL) {
6428 if (xmlStrEqual((*cur)->href, nsName)) {
6429 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elemQName,
6430 name, (*cur)->prefix);
6431 if (attrDecl)
6432 break;
6433 if (doc->extSubset != NULL) {
6434 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elemQName,
6435 name, (*cur)->prefix);
6436 if (attrDecl)
6437 break;
6438 }
6439 }
6440 cur++;
6441 }
6442 xmlFree(nsList);
Daniel Veillardaa6de472008-08-25 14:53:31 +00006443 }
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006444 if (tmpstr != NULL)
6445 xmlFree(tmpstr);
6446 /*
6447 * Only default/fixed attrs are relevant.
6448 */
6449 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6450 return((xmlAttrPtr) attrDecl);
6451 }
6452#endif /* LIBXML_TREE_ENABLED */
6453 return(NULL);
6454}
6455
6456static xmlChar*
6457xmlGetPropNodeValueInternal(xmlAttrPtr prop)
6458{
6459 if (prop == NULL)
6460 return(NULL);
6461 if (prop->type == XML_ATTRIBUTE_NODE) {
6462 /*
6463 * Note that we return at least the empty string.
6464 * TODO: Do we really always want that?
6465 */
6466 if (prop->children != NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00006467 if ((prop->children->next == NULL) &&
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006468 ((prop->children->type == XML_TEXT_NODE) ||
6469 (prop->children->type == XML_CDATA_SECTION_NODE)))
6470 {
6471 /*
6472 * Optimization for the common case: only 1 text node.
6473 */
6474 return(xmlStrdup(prop->children->content));
6475 } else {
6476 xmlChar *ret;
6477
6478 ret = xmlNodeListGetString(prop->doc, prop->children, 1);
6479 if (ret != NULL)
6480 return(ret);
6481 }
6482 }
6483 return(xmlStrdup((xmlChar *)""));
6484 } else if (prop->type == XML_ATTRIBUTE_DECL) {
6485 return(xmlStrdup(((xmlAttributePtr)prop)->defaultValue));
6486 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00006487 return(NULL);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006488}
6489
Owen Taylor3473f882001-02-23 17:55:21 +00006490/**
6491 * xmlHasProp:
6492 * @node: the node
6493 * @name: the attribute name
6494 *
6495 * Search an attribute associated to a node
6496 * This function also looks in DTD attribute declaration for #FIXED or
6497 * default declaration values unless DTD use has been turned off.
6498 *
Daniel Veillardaa6de472008-08-25 14:53:31 +00006499 * Returns the attribute or the attribute declaration or NULL if
Owen Taylor3473f882001-02-23 17:55:21 +00006500 * neither was found.
6501 */
6502xmlAttrPtr
6503xmlHasProp(xmlNodePtr node, const xmlChar *name) {
6504 xmlAttrPtr prop;
6505 xmlDocPtr doc;
6506
Daniel Veillard8874b942005-08-25 13:19:21 +00006507 if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL))
6508 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006509 /*
6510 * Check on the properties attached to the node
6511 */
6512 prop = node->properties;
6513 while (prop != NULL) {
6514 if (xmlStrEqual(prop->name, name)) {
6515 return(prop);
6516 }
6517 prop = prop->next;
6518 }
6519 if (!xmlCheckDTD) return(NULL);
6520
6521 /*
6522 * Check if there is a default declaration in the internal
6523 * or external subsets
6524 */
6525 doc = node->doc;
6526 if (doc != NULL) {
6527 xmlAttributePtr attrDecl;
6528 if (doc->intSubset != NULL) {
6529 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
6530 if ((attrDecl == NULL) && (doc->extSubset != NULL))
6531 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
Daniel Veillard75eb1ad2003-07-07 14:42:44 +00006532 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6533 /* return attribute declaration only if a default value is given
6534 (that includes #FIXED declarations) */
Owen Taylor3473f882001-02-23 17:55:21 +00006535 return((xmlAttrPtr) attrDecl);
6536 }
6537 }
6538 return(NULL);
6539}
6540
6541/**
Daniel Veillarde95e2392001-06-06 10:46:28 +00006542 * xmlHasNsProp:
6543 * @node: the node
6544 * @name: the attribute name
Daniel Veillardca2366a2001-06-11 12:09:01 +00006545 * @nameSpace: the URI of the namespace
Daniel Veillarde95e2392001-06-06 10:46:28 +00006546 *
6547 * Search for an attribute associated to a node
6548 * This attribute has to be anchored in the namespace specified.
6549 * This does the entity substitution.
6550 * This function looks in DTD attribute declaration for #FIXED or
6551 * default declaration values unless DTD use has been turned off.
William M. Brack2c228442004-10-03 04:10:00 +00006552 * Note that a namespace of NULL indicates to use the default namespace.
Daniel Veillarde95e2392001-06-06 10:46:28 +00006553 *
6554 * Returns the attribute or the attribute declaration or NULL
6555 * if neither was found.
6556 */
6557xmlAttrPtr
Daniel Veillardca2366a2001-06-11 12:09:01 +00006558xmlHasNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
Daniel Veillarde95e2392001-06-06 10:46:28 +00006559
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006560 return(xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD));
Daniel Veillarde95e2392001-06-06 10:46:28 +00006561}
6562
6563/**
Owen Taylor3473f882001-02-23 17:55:21 +00006564 * xmlGetProp:
6565 * @node: the node
6566 * @name: the attribute name
6567 *
6568 * Search and get the value of an attribute associated to a node
6569 * This does the entity substitution.
6570 * This function looks in DTD attribute declaration for #FIXED or
6571 * default declaration values unless DTD use has been turned off.
Daniel Veillard784b9352003-02-16 15:50:27 +00006572 * NOTE: this function acts independently of namespaces associated
Daniel Veillard71531f32003-02-05 13:19:53 +00006573 * to the attribute. Use xmlGetNsProp() or xmlGetNoNsProp()
6574 * for namespace aware processing.
Owen Taylor3473f882001-02-23 17:55:21 +00006575 *
6576 * Returns the attribute value or NULL if not found.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00006577 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00006578 */
6579xmlChar *
6580xmlGetProp(xmlNodePtr node, const xmlChar *name) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00006581 xmlAttrPtr prop;
Owen Taylor3473f882001-02-23 17:55:21 +00006582
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006583 prop = xmlHasProp(node, name);
6584 if (prop == NULL)
6585 return(NULL);
Daniel Veillardaa6de472008-08-25 14:53:31 +00006586 return(xmlGetPropNodeValueInternal(prop));
Owen Taylor3473f882001-02-23 17:55:21 +00006587}
6588
6589/**
Daniel Veillard71531f32003-02-05 13:19:53 +00006590 * xmlGetNoNsProp:
6591 * @node: the node
6592 * @name: the attribute name
6593 *
6594 * Search and get the value of an attribute associated to a node
6595 * This does the entity substitution.
6596 * This function looks in DTD attribute declaration for #FIXED or
6597 * default declaration values unless DTD use has been turned off.
6598 * This function is similar to xmlGetProp except it will accept only
6599 * an attribute in no namespace.
6600 *
6601 * Returns the attribute value or NULL if not found.
6602 * It's up to the caller to free the memory with xmlFree().
6603 */
6604xmlChar *
6605xmlGetNoNsProp(xmlNodePtr node, const xmlChar *name) {
6606 xmlAttrPtr prop;
Daniel Veillardaa6de472008-08-25 14:53:31 +00006607
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006608 prop = xmlGetPropNodeInternal(node, name, NULL, xmlCheckDTD);
6609 if (prop == NULL)
Daniel Veillard8874b942005-08-25 13:19:21 +00006610 return(NULL);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006611 return(xmlGetPropNodeValueInternal(prop));
Daniel Veillard71531f32003-02-05 13:19:53 +00006612}
6613
6614/**
Owen Taylor3473f882001-02-23 17:55:21 +00006615 * xmlGetNsProp:
6616 * @node: the node
6617 * @name: the attribute name
Daniel Veillardca2366a2001-06-11 12:09:01 +00006618 * @nameSpace: the URI of the namespace
Owen Taylor3473f882001-02-23 17:55:21 +00006619 *
6620 * Search and get the value of an attribute associated to a node
6621 * This attribute has to be anchored in the namespace specified.
6622 * This does the entity substitution.
6623 * This function looks in DTD attribute declaration for #FIXED or
6624 * default declaration values unless DTD use has been turned off.
6625 *
6626 * Returns the attribute value or NULL if not found.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00006627 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00006628 */
6629xmlChar *
Daniel Veillardca2366a2001-06-11 12:09:01 +00006630xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
Owen Taylor3473f882001-02-23 17:55:21 +00006631 xmlAttrPtr prop;
Owen Taylor3473f882001-02-23 17:55:21 +00006632
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006633 prop = xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD);
6634 if (prop == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00006635 return(NULL);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006636 return(xmlGetPropNodeValueInternal(prop));
Owen Taylor3473f882001-02-23 17:55:21 +00006637}
6638
Daniel Veillard2156d432004-03-04 15:59:36 +00006639#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
6640/**
6641 * xmlUnsetProp:
6642 * @node: the node
6643 * @name: the attribute name
6644 *
6645 * Remove an attribute carried by a node.
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006646 * This handles only attributes in no namespace.
Daniel Veillard2156d432004-03-04 15:59:36 +00006647 * Returns 0 if successful, -1 if not found
6648 */
6649int
6650xmlUnsetProp(xmlNodePtr node, const xmlChar *name) {
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +00006651 xmlAttrPtr prop;
Daniel Veillard2156d432004-03-04 15:59:36 +00006652
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006653 prop = xmlGetPropNodeInternal(node, name, NULL, 0);
6654 if (prop == NULL)
Daniel Veillard2156d432004-03-04 15:59:36 +00006655 return(-1);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006656 xmlUnlinkNode((xmlNodePtr) prop);
6657 xmlFreeProp(prop);
6658 return(0);
Daniel Veillard2156d432004-03-04 15:59:36 +00006659}
6660
6661/**
6662 * xmlUnsetNsProp:
6663 * @node: the node
6664 * @ns: the namespace definition
6665 * @name: the attribute name
6666 *
6667 * Remove an attribute carried by a node.
6668 * Returns 0 if successful, -1 if not found
6669 */
6670int
6671xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) {
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +00006672 xmlAttrPtr prop;
Daniel Veillardaa6de472008-08-25 14:53:31 +00006673
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006674 prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0);
6675 if (prop == NULL)
Daniel Veillard2156d432004-03-04 15:59:36 +00006676 return(-1);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006677 xmlUnlinkNode((xmlNodePtr) prop);
6678 xmlFreeProp(prop);
6679 return(0);
Daniel Veillard2156d432004-03-04 15:59:36 +00006680}
6681#endif
6682
6683#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_HTML_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00006684/**
6685 * xmlSetProp:
6686 * @node: the node
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006687 * @name: the attribute name (a QName)
Owen Taylor3473f882001-02-23 17:55:21 +00006688 * @value: the attribute value
6689 *
6690 * Set (or reset) an attribute carried by a node.
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006691 * If @name has a prefix, then the corresponding
6692 * namespace-binding will be used, if in scope; it is an
6693 * error it there's no such ns-binding for the prefix in
6694 * scope.
Owen Taylor3473f882001-02-23 17:55:21 +00006695 * Returns the attribute pointer.
Daniel Veillardaa6de472008-08-25 14:53:31 +00006696 *
Owen Taylor3473f882001-02-23 17:55:21 +00006697 */
6698xmlAttrPtr
6699xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00006700 int len;
6701 const xmlChar *nqname;
Owen Taylor3473f882001-02-23 17:55:21 +00006702
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006703 if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE))
Owen Taylor3473f882001-02-23 17:55:21 +00006704 return(NULL);
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00006705
6706 /*
6707 * handle QNames
6708 */
6709 nqname = xmlSplitQName3(name, &len);
6710 if (nqname != NULL) {
6711 xmlNsPtr ns;
6712 xmlChar *prefix = xmlStrndup(name, len);
6713 ns = xmlSearchNs(node->doc, node, prefix);
6714 if (prefix != NULL)
6715 xmlFree(prefix);
6716 if (ns != NULL)
6717 return(xmlSetNsProp(node, ns, nqname, value));
6718 }
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006719 return(xmlSetNsProp(node, NULL, name, value));
Owen Taylor3473f882001-02-23 17:55:21 +00006720}
6721
6722/**
6723 * xmlSetNsProp:
6724 * @node: the node
6725 * @ns: the namespace definition
6726 * @name: the attribute name
6727 * @value: the attribute value
6728 *
6729 * Set (or reset) an attribute carried by a node.
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006730 * The ns structure must be in scope, this is not checked
Owen Taylor3473f882001-02-23 17:55:21 +00006731 *
6732 * Returns the attribute pointer.
6733 */
6734xmlAttrPtr
6735xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006736 const xmlChar *value)
6737{
Owen Taylor3473f882001-02-23 17:55:21 +00006738 xmlAttrPtr prop;
Daniel Veillardaa6de472008-08-25 14:53:31 +00006739
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006740 if (ns && (ns->href == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00006741 return(NULL);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006742 prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0);
6743 if (prop != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00006744 /*
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006745 * Modify the attribute's value.
6746 */
6747 if (prop->atype == XML_ATTRIBUTE_ID) {
6748 xmlRemoveID(node->doc, prop);
6749 prop->atype = XML_ATTRIBUTE_ID;
6750 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00006751 if (prop->children != NULL)
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006752 xmlFreeNodeList(prop->children);
6753 prop->children = NULL;
6754 prop->last = NULL;
6755 prop->ns = ns;
6756 if (value != NULL) {
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006757 xmlNodePtr tmp;
Daniel Veillardaa6de472008-08-25 14:53:31 +00006758
Daniel Veillard6f8611f2008-02-15 08:33:21 +00006759 if(!xmlCheckUTF8(value)) {
6760 xmlTreeErr(XML_TREE_NOT_UTF8, (xmlNodePtr) node->doc,
6761 NULL);
6762 if (node->doc != NULL)
6763 node->doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
6764 }
6765 prop->children = xmlNewDocText(node->doc, value);
Owen Taylor3473f882001-02-23 17:55:21 +00006766 prop->last = NULL;
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006767 tmp = prop->children;
6768 while (tmp != NULL) {
6769 tmp->parent = (xmlNodePtr) prop;
6770 if (tmp->next == NULL)
6771 prop->last = tmp;
6772 tmp = tmp->next;
6773 }
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006774 }
6775 if (prop->atype == XML_ATTRIBUTE_ID)
6776 xmlAddID(NULL, node->doc, value, prop);
6777 return(prop);
Owen Taylor3473f882001-02-23 17:55:21 +00006778 }
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006779 /*
6780 * No equal attr found; create a new one.
6781 */
6782 return(xmlNewPropInternal(node, ns, name, value, 0));
Owen Taylor3473f882001-02-23 17:55:21 +00006783}
6784
Daniel Veillard652327a2003-09-29 18:02:38 +00006785#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard75bea542001-05-11 17:41:21 +00006786
6787/**
Owen Taylor3473f882001-02-23 17:55:21 +00006788 * xmlNodeIsText:
6789 * @node: the node
Daniel Veillardaa6de472008-08-25 14:53:31 +00006790 *
Owen Taylor3473f882001-02-23 17:55:21 +00006791 * Is this node a Text node ?
6792 * Returns 1 yes, 0 no
6793 */
6794int
6795xmlNodeIsText(xmlNodePtr node) {
6796 if (node == NULL) return(0);
6797
6798 if (node->type == XML_TEXT_NODE) return(1);
6799 return(0);
6800}
6801
6802/**
6803 * xmlIsBlankNode:
6804 * @node: the node
Daniel Veillardaa6de472008-08-25 14:53:31 +00006805 *
Owen Taylor3473f882001-02-23 17:55:21 +00006806 * Checks whether this node is an empty or whitespace only
6807 * (and possibly ignorable) text-node.
6808 *
6809 * Returns 1 yes, 0 no
6810 */
6811int
6812xmlIsBlankNode(xmlNodePtr node) {
6813 const xmlChar *cur;
6814 if (node == NULL) return(0);
6815
Daniel Veillard7db37732001-07-12 01:20:08 +00006816 if ((node->type != XML_TEXT_NODE) &&
6817 (node->type != XML_CDATA_SECTION_NODE))
6818 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006819 if (node->content == NULL) return(1);
Owen Taylor3473f882001-02-23 17:55:21 +00006820 cur = node->content;
Owen Taylor3473f882001-02-23 17:55:21 +00006821 while (*cur != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00006822 if (!IS_BLANK_CH(*cur)) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006823 cur++;
6824 }
6825
6826 return(1);
6827}
6828
6829/**
6830 * xmlTextConcat:
6831 * @node: the node
6832 * @content: the content
Daniel Veillard60087f32001-10-10 09:45:09 +00006833 * @len: @content length
Daniel Veillardaa6de472008-08-25 14:53:31 +00006834 *
Owen Taylor3473f882001-02-23 17:55:21 +00006835 * Concat the given string at the end of the existing node content
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006836 *
6837 * Returns -1 in case of error, 0 otherwise
Owen Taylor3473f882001-02-23 17:55:21 +00006838 */
6839
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006840int
Owen Taylor3473f882001-02-23 17:55:21 +00006841xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006842 if (node == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006843
6844 if ((node->type != XML_TEXT_NODE) &&
Rob Richardsa02f1992006-09-16 14:04:26 +00006845 (node->type != XML_CDATA_SECTION_NODE) &&
6846 (node->type != XML_COMMENT_NODE) &&
6847 (node->type != XML_PI_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006848#ifdef DEBUG_TREE
6849 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006850 "xmlTextConcat: node is not text nor CDATA\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006851#endif
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006852 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006853 }
William M. Brack7762bb12004-01-04 14:49:01 +00006854 /* need to check if content is currently in the dictionary */
Daniel Veillard8874b942005-08-25 13:19:21 +00006855 if ((node->content == (xmlChar *) &(node->properties)) ||
6856 ((node->doc != NULL) && (node->doc->dict != NULL) &&
6857 xmlDictOwns(node->doc->dict, node->content))) {
William M. Brack7762bb12004-01-04 14:49:01 +00006858 node->content = xmlStrncatNew(node->content, content, len);
6859 } else {
6860 node->content = xmlStrncat(node->content, content, len);
6861 }
Daniel Veillard8874b942005-08-25 13:19:21 +00006862 node->properties = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006863 if (node->content == NULL)
6864 return(-1);
6865 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006866}
6867
6868/************************************************************************
6869 * *
6870 * Output : to a FILE or in memory *
6871 * *
6872 ************************************************************************/
6873
Owen Taylor3473f882001-02-23 17:55:21 +00006874/**
6875 * xmlBufferCreate:
6876 *
6877 * routine to create an XML buffer.
6878 * returns the new structure.
6879 */
6880xmlBufferPtr
6881xmlBufferCreate(void) {
6882 xmlBufferPtr ret;
6883
6884 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6885 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006886 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006887 return(NULL);
6888 }
6889 ret->use = 0;
Daniel Veillarde356c282001-03-10 12:32:04 +00006890 ret->size = xmlDefaultBufferSize;
Owen Taylor3473f882001-02-23 17:55:21 +00006891 ret->alloc = xmlBufferAllocScheme;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00006892 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00006893 if (ret->content == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006894 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006895 xmlFree(ret);
6896 return(NULL);
6897 }
6898 ret->content[0] = 0;
Daniel Veillarde83e93e2008-08-30 12:52:26 +00006899 ret->contentIO = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00006900 return(ret);
6901}
6902
6903/**
6904 * xmlBufferCreateSize:
6905 * @size: initial size of buffer
6906 *
6907 * routine to create an XML buffer.
6908 * returns the new structure.
6909 */
6910xmlBufferPtr
6911xmlBufferCreateSize(size_t size) {
6912 xmlBufferPtr ret;
6913
6914 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6915 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006916 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006917 return(NULL);
6918 }
6919 ret->use = 0;
6920 ret->alloc = xmlBufferAllocScheme;
6921 ret->size = (size ? size+2 : 0); /* +1 for ending null */
6922 if (ret->size){
Daniel Veillard3c908dc2003-04-19 00:07:51 +00006923 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00006924 if (ret->content == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006925 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006926 xmlFree(ret);
6927 return(NULL);
6928 }
6929 ret->content[0] = 0;
6930 } else
6931 ret->content = NULL;
Daniel Veillarde83e93e2008-08-30 12:52:26 +00006932 ret->contentIO = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00006933 return(ret);
6934}
6935
6936/**
Daniel Veillard79ee2842012-05-15 10:25:31 +08006937 * xmlBufferDetach:
Conrad Irwin7d553f82012-05-10 20:17:25 -07006938 * @buf: the buffer
6939 *
Daniel Veillard79ee2842012-05-15 10:25:31 +08006940 * Remove the string contained in a buffer and gie it back to the
Daniel Veillard94431ec2012-05-15 10:45:05 +08006941 * caller. The buffer is reset to an empty content.
6942 * This doesn't work with immutable buffers as they can't be reset.
Conrad Irwin7d553f82012-05-10 20:17:25 -07006943 *
Daniel Veillard79ee2842012-05-15 10:25:31 +08006944 * Returns the previous string contained by the buffer.
Conrad Irwin7d553f82012-05-10 20:17:25 -07006945 */
6946xmlChar *
6947xmlBufferDetach(xmlBufferPtr buf) {
6948 xmlChar *ret;
6949
Daniel Veillard94431ec2012-05-15 10:45:05 +08006950 if (buf == NULL)
6951 return(NULL);
6952 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE)
6953 return(NULL);
Conrad Irwin7d553f82012-05-10 20:17:25 -07006954
6955 ret = buf->content;
6956 buf->content = NULL;
6957 buf->size = 0;
6958 buf->use = 0;
6959
6960 return ret;
6961}
6962
6963
6964/**
Daniel Veillard53350552003-09-18 13:35:51 +00006965 * xmlBufferCreateStatic:
6966 * @mem: the memory area
6967 * @size: the size in byte
6968 *
MST 2003 John Flecka0e7e932003-12-19 03:13:47 +00006969 * routine to create an XML buffer from an immutable memory area.
6970 * The area won't be modified nor copied, and is expected to be
Daniel Veillard53350552003-09-18 13:35:51 +00006971 * present until the end of the buffer lifetime.
6972 *
6973 * returns the new structure.
6974 */
6975xmlBufferPtr
6976xmlBufferCreateStatic(void *mem, size_t size) {
6977 xmlBufferPtr ret;
6978
6979 if ((mem == NULL) || (size == 0))
6980 return(NULL);
6981
6982 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6983 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006984 xmlTreeErrMemory("creating buffer");
Daniel Veillard53350552003-09-18 13:35:51 +00006985 return(NULL);
6986 }
6987 ret->use = size;
6988 ret->size = size;
6989 ret->alloc = XML_BUFFER_ALLOC_IMMUTABLE;
6990 ret->content = (xmlChar *) mem;
6991 return(ret);
6992}
6993
6994/**
Owen Taylor3473f882001-02-23 17:55:21 +00006995 * xmlBufferSetAllocationScheme:
Daniel Veillardbd9afb52002-09-25 22:25:35 +00006996 * @buf: the buffer to tune
Owen Taylor3473f882001-02-23 17:55:21 +00006997 * @scheme: allocation scheme to use
6998 *
6999 * Sets the allocation scheme for this buffer
7000 */
7001void
Daniel Veillardaa6de472008-08-25 14:53:31 +00007002xmlBufferSetAllocationScheme(xmlBufferPtr buf,
Owen Taylor3473f882001-02-23 17:55:21 +00007003 xmlBufferAllocationScheme scheme) {
7004 if (buf == NULL) {
7005#ifdef DEBUG_BUFFER
7006 xmlGenericError(xmlGenericErrorContext,
7007 "xmlBufferSetAllocationScheme: buf == NULL\n");
7008#endif
7009 return;
7010 }
Daniel Veillardda3fee42008-09-01 13:08:57 +00007011 if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) ||
7012 (buf->alloc == XML_BUFFER_ALLOC_IO)) return;
7013 if ((scheme == XML_BUFFER_ALLOC_DOUBLEIT) ||
7014 (scheme == XML_BUFFER_ALLOC_EXACT) ||
Conrad Irwin7d0d2a52012-05-14 14:18:58 +08007015 (scheme == XML_BUFFER_ALLOC_HYBRID) ||
Daniel Veillardda3fee42008-09-01 13:08:57 +00007016 (scheme == XML_BUFFER_ALLOC_IMMUTABLE))
7017 buf->alloc = scheme;
Owen Taylor3473f882001-02-23 17:55:21 +00007018}
7019
7020/**
7021 * xmlBufferFree:
7022 * @buf: the buffer to free
7023 *
Daniel Veillard9d06d302002-01-22 18:15:52 +00007024 * Frees an XML buffer. It frees both the content and the structure which
7025 * encapsulate it.
Owen Taylor3473f882001-02-23 17:55:21 +00007026 */
7027void
7028xmlBufferFree(xmlBufferPtr buf) {
7029 if (buf == NULL) {
7030#ifdef DEBUG_BUFFER
7031 xmlGenericError(xmlGenericErrorContext,
7032 "xmlBufferFree: buf == NULL\n");
7033#endif
7034 return;
7035 }
Daniel Veillard53350552003-09-18 13:35:51 +00007036
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007037 if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
7038 (buf->contentIO != NULL)) {
7039 xmlFree(buf->contentIO);
7040 } else if ((buf->content != NULL) &&
Daniel Veillard53350552003-09-18 13:35:51 +00007041 (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00007042 xmlFree(buf->content);
7043 }
Owen Taylor3473f882001-02-23 17:55:21 +00007044 xmlFree(buf);
7045}
7046
7047/**
7048 * xmlBufferEmpty:
7049 * @buf: the buffer
7050 *
7051 * empty a buffer.
7052 */
7053void
7054xmlBufferEmpty(xmlBufferPtr buf) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007055 if (buf == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +00007056 if (buf->content == NULL) return;
7057 buf->use = 0;
Daniel Veillard53350552003-09-18 13:35:51 +00007058 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00007059 buf->content = BAD_CAST "";
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007060 } else if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
7061 (buf->contentIO != NULL)) {
7062 size_t start_buf = buf->content - buf->contentIO;
7063
7064 buf->size += start_buf;
7065 buf->content = buf->contentIO;
7066 buf->content[0] = 0;
Daniel Veillard53350552003-09-18 13:35:51 +00007067 } else {
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007068 buf->content[0] = 0;
7069 }
Owen Taylor3473f882001-02-23 17:55:21 +00007070}
7071
7072/**
7073 * xmlBufferShrink:
7074 * @buf: the buffer to dump
7075 * @len: the number of xmlChar to remove
7076 *
7077 * Remove the beginning of an XML buffer.
7078 *
Daniel Veillardd1640922001-12-17 15:30:10 +00007079 * Returns the number of #xmlChar removed, or -1 in case of failure.
Owen Taylor3473f882001-02-23 17:55:21 +00007080 */
7081int
7082xmlBufferShrink(xmlBufferPtr buf, unsigned int len) {
Daniel Veillard3d97e662004-11-04 10:49:00 +00007083 if (buf == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00007084 if (len == 0) return(0);
7085 if (len > buf->use) return(-1);
7086
7087 buf->use -= len;
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007088 if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) ||
7089 ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL))) {
7090 /*
7091 * we just move the content pointer, but also make sure
7092 * the perceived buffer size has shrinked accordingly
7093 */
Daniel Veillard53350552003-09-18 13:35:51 +00007094 buf->content += len;
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007095 buf->size -= len;
7096
7097 /*
7098 * sometimes though it maybe be better to really shrink
7099 * on IO buffers
7100 */
7101 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7102 size_t start_buf = buf->content - buf->contentIO;
7103 if (start_buf >= buf->size) {
7104 memmove(buf->contentIO, &buf->content[0], buf->use);
7105 buf->content = buf->contentIO;
7106 buf->content[buf->use] = 0;
7107 buf->size += start_buf;
7108 }
7109 }
Daniel Veillard53350552003-09-18 13:35:51 +00007110 } else {
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007111 memmove(buf->content, &buf->content[len], buf->use);
Daniel Veillard53350552003-09-18 13:35:51 +00007112 buf->content[buf->use] = 0;
7113 }
Owen Taylor3473f882001-02-23 17:55:21 +00007114 return(len);
7115}
7116
7117/**
7118 * xmlBufferGrow:
7119 * @buf: the buffer
7120 * @len: the minimum free size to allocate
7121 *
7122 * Grow the available space of an XML buffer.
7123 *
7124 * Returns the new available space or -1 in case of error
7125 */
7126int
7127xmlBufferGrow(xmlBufferPtr buf, unsigned int len) {
7128 int size;
7129 xmlChar *newbuf;
7130
Daniel Veillard3d97e662004-11-04 10:49:00 +00007131 if (buf == NULL) return(-1);
7132
Daniel Veillard53350552003-09-18 13:35:51 +00007133 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00007134 if (len + buf->use < buf->size) return(0);
7135
Daniel Veillardee20cd72009-08-22 15:18:31 +02007136 /*
7137 * Windows has a BIG problem on realloc timing, so we try to double
7138 * the buffer size (if that's enough) (bug 146697)
7139 * Apparently BSD too, and it's probably best for linux too
7140 * On an embedded system this may be something to change
7141 */
7142#if 1
William M. Brack30fe43f2004-07-26 18:00:58 +00007143 if (buf->size > len)
7144 size = buf->size * 2;
7145 else
7146 size = buf->use + len + 100;
7147#else
Owen Taylor3473f882001-02-23 17:55:21 +00007148 size = buf->use + len + 100;
William M. Brack30fe43f2004-07-26 18:00:58 +00007149#endif
Owen Taylor3473f882001-02-23 17:55:21 +00007150
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007151 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7152 size_t start_buf = buf->content - buf->contentIO;
7153
7154 newbuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + size);
7155 if (newbuf == NULL) {
7156 xmlTreeErrMemory("growing buffer");
7157 return(-1);
7158 }
7159 buf->contentIO = newbuf;
7160 buf->content = newbuf + start_buf;
7161 } else {
7162 newbuf = (xmlChar *) xmlRealloc(buf->content, size);
7163 if (newbuf == NULL) {
7164 xmlTreeErrMemory("growing buffer");
7165 return(-1);
7166 }
7167 buf->content = newbuf;
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007168 }
Owen Taylor3473f882001-02-23 17:55:21 +00007169 buf->size = size;
7170 return(buf->size - buf->use);
7171}
7172
7173/**
7174 * xmlBufferDump:
7175 * @file: the file output
7176 * @buf: the buffer to dump
7177 *
7178 * Dumps an XML buffer to a FILE *.
Daniel Veillardd1640922001-12-17 15:30:10 +00007179 * Returns the number of #xmlChar written
Owen Taylor3473f882001-02-23 17:55:21 +00007180 */
7181int
7182xmlBufferDump(FILE *file, xmlBufferPtr buf) {
7183 int ret;
7184
7185 if (buf == NULL) {
7186#ifdef DEBUG_BUFFER
7187 xmlGenericError(xmlGenericErrorContext,
7188 "xmlBufferDump: buf == NULL\n");
7189#endif
7190 return(0);
7191 }
7192 if (buf->content == NULL) {
7193#ifdef DEBUG_BUFFER
7194 xmlGenericError(xmlGenericErrorContext,
7195 "xmlBufferDump: buf->content == NULL\n");
7196#endif
7197 return(0);
7198 }
Daniel Veillardcd337f02001-11-22 18:20:37 +00007199 if (file == NULL)
7200 file = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00007201 ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
7202 return(ret);
7203}
7204
7205/**
7206 * xmlBufferContent:
7207 * @buf: the buffer
7208 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00007209 * Function to extract the content of a buffer
7210 *
Owen Taylor3473f882001-02-23 17:55:21 +00007211 * Returns the internal content
7212 */
7213
Daniel Veillard5e2dace2001-07-18 19:30:27 +00007214const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00007215xmlBufferContent(const xmlBufferPtr buf)
7216{
7217 if(!buf)
7218 return NULL;
7219
7220 return buf->content;
7221}
7222
7223/**
7224 * xmlBufferLength:
Daniel Veillardaa6de472008-08-25 14:53:31 +00007225 * @buf: the buffer
Owen Taylor3473f882001-02-23 17:55:21 +00007226 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00007227 * Function to get the length of a buffer
7228 *
Owen Taylor3473f882001-02-23 17:55:21 +00007229 * Returns the length of data in the internal content
7230 */
7231
7232int
7233xmlBufferLength(const xmlBufferPtr buf)
7234{
7235 if(!buf)
7236 return 0;
7237
7238 return buf->use;
7239}
7240
7241/**
7242 * xmlBufferResize:
7243 * @buf: the buffer to resize
7244 * @size: the desired size
7245 *
Daniel Veillardd1640922001-12-17 15:30:10 +00007246 * Resize a buffer to accommodate minimum size of @size.
Owen Taylor3473f882001-02-23 17:55:21 +00007247 *
7248 * Returns 0 in case of problems, 1 otherwise
7249 */
7250int
7251xmlBufferResize(xmlBufferPtr buf, unsigned int size)
7252{
7253 unsigned int newSize;
7254 xmlChar* rebuf = NULL;
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007255 size_t start_buf;
Owen Taylor3473f882001-02-23 17:55:21 +00007256
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007257 if (buf == NULL)
7258 return(0);
7259
Daniel Veillard53350552003-09-18 13:35:51 +00007260 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
7261
Owen Taylor3473f882001-02-23 17:55:21 +00007262 /* Don't resize if we don't have to */
7263 if (size < buf->size)
7264 return 1;
7265
7266 /* figure out new size */
7267 switch (buf->alloc){
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007268 case XML_BUFFER_ALLOC_IO:
7269 case XML_BUFFER_ALLOC_DOUBLEIT:
7270 /*take care of empty case*/
7271 newSize = (buf->size ? buf->size*2 : size + 10);
Daniel Veillard1dc9feb2008-11-17 15:59:21 +00007272 while (size > newSize) {
7273 if (newSize > UINT_MAX / 2) {
7274 xmlTreeErrMemory("growing buffer");
7275 return 0;
7276 }
7277 newSize *= 2;
7278 }
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007279 break;
7280 case XML_BUFFER_ALLOC_EXACT:
7281 newSize = size+10;
7282 break;
Conrad Irwin7d0d2a52012-05-14 14:18:58 +08007283 case XML_BUFFER_ALLOC_HYBRID:
7284 if (buf->use < BASE_BUFFER_SIZE)
7285 newSize = size;
7286 else {
7287 newSize = buf->size * 2;
7288 while (size > newSize) {
7289 if (newSize > UINT_MAX / 2) {
7290 xmlTreeErrMemory("growing buffer");
7291 return 0;
7292 }
7293 newSize *= 2;
7294 }
7295 }
7296 break;
7297
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007298 default:
7299 newSize = size+10;
7300 break;
Owen Taylor3473f882001-02-23 17:55:21 +00007301 }
7302
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007303 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7304 start_buf = buf->content - buf->contentIO;
7305
7306 if (start_buf > newSize) {
7307 /* move data back to start */
7308 memmove(buf->contentIO, buf->content, buf->use);
7309 buf->content = buf->contentIO;
7310 buf->content[buf->use] = 0;
7311 buf->size += start_buf;
7312 } else {
7313 rebuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + newSize);
7314 if (rebuf == NULL) {
7315 xmlTreeErrMemory("growing buffer");
7316 return 0;
7317 }
7318 buf->contentIO = rebuf;
7319 buf->content = rebuf + start_buf;
Daniel Veillard6155d8a2003-08-19 15:01:28 +00007320 }
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007321 } else {
7322 if (buf->content == NULL) {
7323 rebuf = (xmlChar *) xmlMallocAtomic(newSize);
7324 } else if (buf->size - buf->use < 100) {
7325 rebuf = (xmlChar *) xmlRealloc(buf->content, newSize);
7326 } else {
7327 /*
7328 * if we are reallocating a buffer far from being full, it's
7329 * better to make a new allocation and copy only the used range
7330 * and free the old one.
7331 */
7332 rebuf = (xmlChar *) xmlMallocAtomic(newSize);
7333 if (rebuf != NULL) {
7334 memcpy(rebuf, buf->content, buf->use);
7335 xmlFree(buf->content);
7336 rebuf[buf->use] = 0;
7337 }
7338 }
7339 if (rebuf == NULL) {
7340 xmlTreeErrMemory("growing buffer");
7341 return 0;
7342 }
7343 buf->content = rebuf;
Daniel Veillard6155d8a2003-08-19 15:01:28 +00007344 }
Owen Taylor3473f882001-02-23 17:55:21 +00007345 buf->size = newSize;
7346
7347 return 1;
7348}
7349
7350/**
7351 * xmlBufferAdd:
7352 * @buf: the buffer to dump
Daniel Veillardd1640922001-12-17 15:30:10 +00007353 * @str: the #xmlChar string
7354 * @len: the number of #xmlChar to add
Owen Taylor3473f882001-02-23 17:55:21 +00007355 *
Daniel Veillard60087f32001-10-10 09:45:09 +00007356 * Add a string range to an XML buffer. if len == -1, the length of
Owen Taylor3473f882001-02-23 17:55:21 +00007357 * str is recomputed.
William M. Bracka3215c72004-07-31 16:24:01 +00007358 *
7359 * Returns 0 successful, a positive error code number otherwise
7360 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00007361 */
William M. Bracka3215c72004-07-31 16:24:01 +00007362int
Owen Taylor3473f882001-02-23 17:55:21 +00007363xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) {
7364 unsigned int needSize;
7365
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007366 if ((str == NULL) || (buf == NULL)) {
William M. Bracka3215c72004-07-31 16:24:01 +00007367 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007368 }
William M. Bracka3215c72004-07-31 16:24:01 +00007369 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007370 if (len < -1) {
7371#ifdef DEBUG_BUFFER
7372 xmlGenericError(xmlGenericErrorContext,
7373 "xmlBufferAdd: len < 0\n");
7374#endif
William M. Bracka3215c72004-07-31 16:24:01 +00007375 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007376 }
William M. Bracka3215c72004-07-31 16:24:01 +00007377 if (len == 0) return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007378
7379 if (len < 0)
7380 len = xmlStrlen(str);
7381
Daniel Veillardc9923322007-04-24 18:12:06 +00007382 if (len < 0) return -1;
7383 if (len == 0) return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007384
7385 needSize = buf->use + len + 2;
7386 if (needSize > buf->size){
7387 if (!xmlBufferResize(buf, needSize)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007388 xmlTreeErrMemory("growing buffer");
William M. Bracka3215c72004-07-31 16:24:01 +00007389 return XML_ERR_NO_MEMORY;
Owen Taylor3473f882001-02-23 17:55:21 +00007390 }
7391 }
7392
7393 memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
7394 buf->use += len;
7395 buf->content[buf->use] = 0;
William M. Bracka3215c72004-07-31 16:24:01 +00007396 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007397}
7398
7399/**
7400 * xmlBufferAddHead:
7401 * @buf: the buffer
Daniel Veillardd1640922001-12-17 15:30:10 +00007402 * @str: the #xmlChar string
7403 * @len: the number of #xmlChar to add
Owen Taylor3473f882001-02-23 17:55:21 +00007404 *
7405 * Add a string range to the beginning of an XML buffer.
Daniel Veillard60087f32001-10-10 09:45:09 +00007406 * if len == -1, the length of @str is recomputed.
William M. Bracka3215c72004-07-31 16:24:01 +00007407 *
7408 * Returns 0 successful, a positive error code number otherwise
7409 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00007410 */
William M. Bracka3215c72004-07-31 16:24:01 +00007411int
Owen Taylor3473f882001-02-23 17:55:21 +00007412xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) {
7413 unsigned int needSize;
7414
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007415 if (buf == NULL)
7416 return(-1);
William M. Bracka3215c72004-07-31 16:24:01 +00007417 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007418 if (str == NULL) {
7419#ifdef DEBUG_BUFFER
7420 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007421 "xmlBufferAddHead: str == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007422#endif
William M. Bracka3215c72004-07-31 16:24:01 +00007423 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007424 }
7425 if (len < -1) {
7426#ifdef DEBUG_BUFFER
7427 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007428 "xmlBufferAddHead: len < 0\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007429#endif
William M. Bracka3215c72004-07-31 16:24:01 +00007430 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007431 }
William M. Bracka3215c72004-07-31 16:24:01 +00007432 if (len == 0) return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007433
7434 if (len < 0)
7435 len = xmlStrlen(str);
7436
William M. Bracka3215c72004-07-31 16:24:01 +00007437 if (len <= 0) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007438
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007439 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7440 size_t start_buf = buf->content - buf->contentIO;
7441
7442 if (start_buf > (unsigned int) len) {
7443 /*
7444 * We can add it in the space previously shrinked
7445 */
7446 buf->content -= len;
7447 memmove(&buf->content[0], str, len);
7448 buf->use += len;
7449 buf->size += len;
7450 return(0);
7451 }
7452 }
Owen Taylor3473f882001-02-23 17:55:21 +00007453 needSize = buf->use + len + 2;
7454 if (needSize > buf->size){
7455 if (!xmlBufferResize(buf, needSize)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007456 xmlTreeErrMemory("growing buffer");
William M. Bracka3215c72004-07-31 16:24:01 +00007457 return XML_ERR_NO_MEMORY;
Owen Taylor3473f882001-02-23 17:55:21 +00007458 }
7459 }
7460
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007461 memmove(&buf->content[len], &buf->content[0], buf->use);
7462 memmove(&buf->content[0], str, len);
Owen Taylor3473f882001-02-23 17:55:21 +00007463 buf->use += len;
7464 buf->content[buf->use] = 0;
William M. Bracka3215c72004-07-31 16:24:01 +00007465 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007466}
7467
7468/**
7469 * xmlBufferCat:
William M. Bracka3215c72004-07-31 16:24:01 +00007470 * @buf: the buffer to add to
Daniel Veillardd1640922001-12-17 15:30:10 +00007471 * @str: the #xmlChar string
Owen Taylor3473f882001-02-23 17:55:21 +00007472 *
7473 * Append a zero terminated string to an XML buffer.
William M. Bracka3215c72004-07-31 16:24:01 +00007474 *
7475 * Returns 0 successful, a positive error code number otherwise
7476 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00007477 */
William M. Bracka3215c72004-07-31 16:24:01 +00007478int
Owen Taylor3473f882001-02-23 17:55:21 +00007479xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007480 if (buf == NULL)
7481 return(-1);
William M. Bracka3215c72004-07-31 16:24:01 +00007482 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7483 if (str == NULL) return -1;
7484 return xmlBufferAdd(buf, str, -1);
Owen Taylor3473f882001-02-23 17:55:21 +00007485}
7486
7487/**
7488 * xmlBufferCCat:
7489 * @buf: the buffer to dump
7490 * @str: the C char string
7491 *
7492 * Append a zero terminated C string to an XML buffer.
William M. Bracka3215c72004-07-31 16:24:01 +00007493 *
7494 * Returns 0 successful, a positive error code number otherwise
7495 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00007496 */
William M. Bracka3215c72004-07-31 16:24:01 +00007497int
Owen Taylor3473f882001-02-23 17:55:21 +00007498xmlBufferCCat(xmlBufferPtr buf, const char *str) {
7499 const char *cur;
7500
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007501 if (buf == NULL)
7502 return(-1);
William M. Bracka3215c72004-07-31 16:24:01 +00007503 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007504 if (str == NULL) {
7505#ifdef DEBUG_BUFFER
7506 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007507 "xmlBufferCCat: str == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007508#endif
William M. Bracka3215c72004-07-31 16:24:01 +00007509 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007510 }
7511 for (cur = str;*cur != 0;cur++) {
7512 if (buf->use + 10 >= buf->size) {
7513 if (!xmlBufferResize(buf, buf->use+10)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007514 xmlTreeErrMemory("growing buffer");
William M. Bracka3215c72004-07-31 16:24:01 +00007515 return XML_ERR_NO_MEMORY;
Owen Taylor3473f882001-02-23 17:55:21 +00007516 }
7517 }
7518 buf->content[buf->use++] = *cur;
7519 }
7520 buf->content[buf->use] = 0;
William M. Bracka3215c72004-07-31 16:24:01 +00007521 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007522}
7523
7524/**
7525 * xmlBufferWriteCHAR:
7526 * @buf: the XML buffer
7527 * @string: the string to add
7528 *
7529 * routine which manages and grows an output buffer. This one adds
7530 * xmlChars at the end of the buffer.
7531 */
7532void
Daniel Veillard53350552003-09-18 13:35:51 +00007533xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007534 if (buf == NULL)
7535 return;
Daniel Veillard53350552003-09-18 13:35:51 +00007536 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00007537 xmlBufferCat(buf, string);
7538}
7539
7540/**
7541 * xmlBufferWriteChar:
7542 * @buf: the XML buffer output
7543 * @string: the string to add
7544 *
7545 * routine which manage and grows an output buffer. This one add
7546 * C chars at the end of the array.
7547 */
7548void
7549xmlBufferWriteChar(xmlBufferPtr buf, const char *string) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007550 if (buf == NULL)
7551 return;
Daniel Veillard53350552003-09-18 13:35:51 +00007552 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00007553 xmlBufferCCat(buf, string);
7554}
7555
7556
7557/**
7558 * xmlBufferWriteQuotedString:
7559 * @buf: the XML buffer output
7560 * @string: the string to add
7561 *
7562 * routine which manage and grows an output buffer. This one writes
Daniel Veillardd1640922001-12-17 15:30:10 +00007563 * a quoted or double quoted #xmlChar string, checking first if it holds
Owen Taylor3473f882001-02-23 17:55:21 +00007564 * quote or double-quotes internally
7565 */
7566void
7567xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) {
Daniel Veillard39057f42003-08-04 01:33:43 +00007568 const xmlChar *cur, *base;
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007569 if (buf == NULL)
7570 return;
Daniel Veillard53350552003-09-18 13:35:51 +00007571 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Daniel Veillard39057f42003-08-04 01:33:43 +00007572 if (xmlStrchr(string, '\"')) {
Daniel Veillard20aa0fb2003-08-04 19:43:15 +00007573 if (xmlStrchr(string, '\'')) {
Owen Taylor3473f882001-02-23 17:55:21 +00007574#ifdef DEBUG_BUFFER
7575 xmlGenericError(xmlGenericErrorContext,
7576 "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n");
7577#endif
Daniel Veillard39057f42003-08-04 01:33:43 +00007578 xmlBufferCCat(buf, "\"");
7579 base = cur = string;
7580 while(*cur != 0){
7581 if(*cur == '"'){
7582 if (base != cur)
7583 xmlBufferAdd(buf, base, cur - base);
7584 xmlBufferAdd(buf, BAD_CAST "&quot;", 6);
7585 cur++;
7586 base = cur;
7587 }
7588 else {
7589 cur++;
7590 }
7591 }
7592 if (base != cur)
7593 xmlBufferAdd(buf, base, cur - base);
7594 xmlBufferCCat(buf, "\"");
Owen Taylor3473f882001-02-23 17:55:21 +00007595 }
Daniel Veillard39057f42003-08-04 01:33:43 +00007596 else{
7597 xmlBufferCCat(buf, "\'");
7598 xmlBufferCat(buf, string);
7599 xmlBufferCCat(buf, "\'");
7600 }
Owen Taylor3473f882001-02-23 17:55:21 +00007601 } else {
7602 xmlBufferCCat(buf, "\"");
7603 xmlBufferCat(buf, string);
7604 xmlBufferCCat(buf, "\"");
7605 }
7606}
7607
7608
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007609/**
7610 * xmlGetDocCompressMode:
7611 * @doc: the document
7612 *
7613 * get the compression ratio for a document, ZLIB based
7614 * Returns 0 (uncompressed) to 9 (max compression)
7615 */
7616int
7617xmlGetDocCompressMode (xmlDocPtr doc) {
7618 if (doc == NULL) return(-1);
7619 return(doc->compression);
7620}
7621
7622/**
7623 * xmlSetDocCompressMode:
7624 * @doc: the document
7625 * @mode: the compression ratio
7626 *
7627 * set the compression ratio for a document, ZLIB based
7628 * Correct values: 0 (uncompressed) to 9 (max compression)
7629 */
7630void
7631xmlSetDocCompressMode (xmlDocPtr doc, int mode) {
7632 if (doc == NULL) return;
7633 if (mode < 0) doc->compression = 0;
7634 else if (mode > 9) doc->compression = 9;
7635 else doc->compression = mode;
7636}
7637
7638/**
7639 * xmlGetCompressMode:
7640 *
7641 * get the default compression mode used, ZLIB based.
7642 * Returns 0 (uncompressed) to 9 (max compression)
7643 */
7644int
7645xmlGetCompressMode(void)
7646{
7647 return (xmlCompressMode);
7648}
7649
7650/**
7651 * xmlSetCompressMode:
7652 * @mode: the compression ratio
7653 *
7654 * set the default compression mode used, ZLIB based
7655 * Correct values: 0 (uncompressed) to 9 (max compression)
7656 */
7657void
7658xmlSetCompressMode(int mode) {
7659 if (mode < 0) xmlCompressMode = 0;
7660 else if (mode > 9) xmlCompressMode = 9;
7661 else xmlCompressMode = mode;
7662}
7663
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007664#define XML_TREE_NSMAP_PARENT -1
7665#define XML_TREE_NSMAP_XML -2
7666#define XML_TREE_NSMAP_DOC -3
7667#define XML_TREE_NSMAP_CUSTOM -4
7668
7669typedef struct xmlNsMapItem *xmlNsMapItemPtr;
7670struct xmlNsMapItem {
7671 xmlNsMapItemPtr next;
7672 xmlNsMapItemPtr prev;
7673 xmlNsPtr oldNs; /* old ns decl reference */
7674 xmlNsPtr newNs; /* new ns decl reference */
7675 int shadowDepth; /* Shadowed at this depth */
7676 /*
7677 * depth:
7678 * >= 0 == @node's ns-decls
7679 * -1 == @parent's ns-decls
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00007680 * -2 == the doc->oldNs XML ns-decl
7681 * -3 == the doc->oldNs storage ns-decls
7682 * -4 == ns-decls provided via custom ns-handling
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007683 */
7684 int depth;
7685};
7686
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007687typedef struct xmlNsMap *xmlNsMapPtr;
7688struct xmlNsMap {
7689 xmlNsMapItemPtr first;
7690 xmlNsMapItemPtr last;
7691 xmlNsMapItemPtr pool;
7692};
7693
7694#define XML_NSMAP_NOTEMPTY(m) (((m) != NULL) && ((m)->first != NULL))
7695#define XML_NSMAP_FOREACH(m, i) for (i = (m)->first; i != NULL; i = (i)->next)
7696#define XML_NSMAP_POP(m, i) \
7697 i = (m)->last; \
7698 (m)->last = (i)->prev; \
7699 if ((m)->last == NULL) \
7700 (m)->first = NULL; \
7701 else \
7702 (m)->last->next = NULL; \
7703 (i)->next = (m)->pool; \
7704 (m)->pool = i;
7705
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007706/*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007707* xmlDOMWrapNsMapFree:
7708* @map: the ns-map
Daniel Veillardaa6de472008-08-25 14:53:31 +00007709*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007710* Frees the ns-map
7711*/
7712static void
7713xmlDOMWrapNsMapFree(xmlNsMapPtr nsmap)
7714{
7715 xmlNsMapItemPtr cur, tmp;
7716
7717 if (nsmap == NULL)
7718 return;
7719 cur = nsmap->pool;
7720 while (cur != NULL) {
7721 tmp = cur;
7722 cur = cur->next;
7723 xmlFree(tmp);
7724 }
7725 cur = nsmap->first;
7726 while (cur != NULL) {
7727 tmp = cur;
7728 cur = cur->next;
7729 xmlFree(tmp);
7730 }
7731 xmlFree(nsmap);
7732}
7733
7734/*
7735* xmlDOMWrapNsMapAddItem:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007736* @map: the ns-map
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007737* @oldNs: the old ns-struct
7738* @newNs: the new ns-struct
7739* @depth: depth and ns-kind information
Daniel Veillardaa6de472008-08-25 14:53:31 +00007740*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007741* Adds an ns-mapping item.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007742*/
7743static xmlNsMapItemPtr
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00007744xmlDOMWrapNsMapAddItem(xmlNsMapPtr *nsmap, int position,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007745 xmlNsPtr oldNs, xmlNsPtr newNs, int depth)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007746{
7747 xmlNsMapItemPtr ret;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007748 xmlNsMapPtr map;
7749
7750 if (nsmap == NULL)
7751 return(NULL);
7752 if ((position != -1) && (position != 0))
7753 return(NULL);
7754 map = *nsmap;
7755
7756 if (map == NULL) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007757 /*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007758 * Create the ns-map.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007759 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007760 map = (xmlNsMapPtr) xmlMalloc(sizeof(struct xmlNsMap));
7761 if (map == NULL) {
7762 xmlTreeErrMemory("allocating namespace map");
7763 return (NULL);
7764 }
7765 memset(map, 0, sizeof(struct xmlNsMap));
7766 *nsmap = map;
7767 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00007768
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007769 if (map->pool != NULL) {
7770 /*
7771 * Reuse an item from the pool.
7772 */
7773 ret = map->pool;
7774 map->pool = ret->next;
7775 memset(ret, 0, sizeof(struct xmlNsMapItem));
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007776 } else {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007777 /*
7778 * Create a new item.
7779 */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007780 ret = (xmlNsMapItemPtr) xmlMalloc(sizeof(struct xmlNsMapItem));
7781 if (ret == NULL) {
7782 xmlTreeErrMemory("allocating namespace map item");
7783 return (NULL);
7784 }
7785 memset(ret, 0, sizeof(struct xmlNsMapItem));
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007786 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00007787
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007788 if (map->first == NULL) {
7789 /*
7790 * First ever.
Daniel Veillardaa6de472008-08-25 14:53:31 +00007791 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007792 map->first = ret;
7793 map->last = ret;
7794 } else if (position == -1) {
7795 /*
7796 * Append.
7797 */
7798 ret->prev = map->last;
7799 map->last->next = ret;
Daniel Veillardaa6de472008-08-25 14:53:31 +00007800 map->last = ret;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007801 } else if (position == 0) {
7802 /*
7803 * Set on first position.
7804 */
7805 map->first->prev = ret;
Daniel Veillardaa6de472008-08-25 14:53:31 +00007806 ret->next = map->first;
7807 map->first = ret;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007808 } else
7809 return(NULL);
7810
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007811 ret->oldNs = oldNs;
7812 ret->newNs = newNs;
7813 ret->shadowDepth = -1;
7814 ret->depth = depth;
7815 return (ret);
7816}
7817
7818/*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007819* xmlDOMWrapStoreNs:
7820* @doc: the doc
7821* @nsName: the namespace name
7822* @prefix: the prefix
Daniel Veillardaa6de472008-08-25 14:53:31 +00007823*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007824* Creates or reuses an xmlNs struct on doc->oldNs with
7825* the given prefix and namespace name.
Daniel Veillardaa6de472008-08-25 14:53:31 +00007826*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007827* Returns the aquired ns struct or NULL in case of an API
7828* or internal error.
7829*/
7830static xmlNsPtr
7831xmlDOMWrapStoreNs(xmlDocPtr doc,
7832 const xmlChar *nsName,
7833 const xmlChar *prefix)
7834{
7835 xmlNsPtr ns;
7836
7837 if (doc == NULL)
7838 return (NULL);
7839 ns = xmlTreeEnsureXMLDecl(doc);
7840 if (ns == NULL)
7841 return (NULL);
7842 if (ns->next != NULL) {
7843 /* Reuse. */
7844 ns = ns->next;
7845 while (ns != NULL) {
7846 if (((ns->prefix == prefix) ||
7847 xmlStrEqual(ns->prefix, prefix)) &&
7848 xmlStrEqual(ns->href, nsName)) {
7849 return (ns);
7850 }
7851 if (ns->next == NULL)
7852 break;
7853 ns = ns->next;
7854 }
7855 }
7856 /* Create. */
Daniel Veillard76d36452009-09-07 11:19:33 +02007857 if (ns != NULL) {
7858 ns->next = xmlNewNs(NULL, nsName, prefix);
7859 return (ns->next);
7860 }
7861 return(NULL);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007862}
7863
7864/*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00007865* xmlDOMWrapNewCtxt:
7866*
7867* Allocates and initializes a new DOM-wrapper context.
7868*
Daniel Veillardaa6de472008-08-25 14:53:31 +00007869* Returns the xmlDOMWrapCtxtPtr or NULL in case of an internal errror.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00007870*/
7871xmlDOMWrapCtxtPtr
7872xmlDOMWrapNewCtxt(void)
7873{
7874 xmlDOMWrapCtxtPtr ret;
7875
7876 ret = xmlMalloc(sizeof(xmlDOMWrapCtxt));
7877 if (ret == NULL) {
7878 xmlTreeErrMemory("allocating DOM-wrapper context");
7879 return (NULL);
7880 }
7881 memset(ret, 0, sizeof(xmlDOMWrapCtxt));
7882 return (ret);
7883}
7884
7885/*
7886* xmlDOMWrapFreeCtxt:
7887* @ctxt: the DOM-wrapper context
7888*
7889* Frees the DOM-wrapper context.
7890*/
7891void
7892xmlDOMWrapFreeCtxt(xmlDOMWrapCtxtPtr ctxt)
7893{
7894 if (ctxt == NULL)
7895 return;
7896 if (ctxt->namespaceMap != NULL)
7897 xmlDOMWrapNsMapFree((xmlNsMapPtr) ctxt->namespaceMap);
7898 /*
7899 * TODO: Store the namespace map in the context.
7900 */
7901 xmlFree(ctxt);
7902}
7903
7904/*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007905* xmlTreeLookupNsListByPrefix:
7906* @nsList: a list of ns-structs
7907* @prefix: the searched prefix
Daniel Veillardaa6de472008-08-25 14:53:31 +00007908*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007909* Searches for a ns-decl with the given prefix in @nsList.
Daniel Veillardaa6de472008-08-25 14:53:31 +00007910*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007911* Returns the ns-decl if found, NULL if not found and on
7912* API errors.
7913*/
7914static xmlNsPtr
7915xmlTreeNSListLookupByPrefix(xmlNsPtr nsList, const xmlChar *prefix)
7916{
7917 if (nsList == NULL)
7918 return (NULL);
7919 {
7920 xmlNsPtr ns;
7921 ns = nsList;
7922 do {
7923 if ((prefix == ns->prefix) ||
7924 xmlStrEqual(prefix, ns->prefix)) {
7925 return (ns);
7926 }
7927 ns = ns->next;
7928 } while (ns != NULL);
7929 }
7930 return (NULL);
7931}
7932
7933/*
7934*
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00007935* xmlDOMWrapNSNormGatherInScopeNs:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007936* @map: the namespace map
7937* @node: the node to start with
Daniel Veillardaa6de472008-08-25 14:53:31 +00007938*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007939* Puts in-scope namespaces into the ns-map.
Daniel Veillardaa6de472008-08-25 14:53:31 +00007940*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007941* Returns 0 on success, -1 on API or internal errors.
7942*/
7943static int
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007944xmlDOMWrapNSNormGatherInScopeNs(xmlNsMapPtr *map,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007945 xmlNodePtr node)
7946{
7947 xmlNodePtr cur;
7948 xmlNsPtr ns;
7949 xmlNsMapItemPtr mi;
7950 int shadowed;
7951
7952 if ((map == NULL) || (*map != NULL))
7953 return (-1);
7954 /*
7955 * Get in-scope ns-decls of @parent.
7956 */
7957 cur = node;
7958 while ((cur != NULL) && (cur != (xmlNodePtr) cur->doc)) {
7959 if (cur->type == XML_ELEMENT_NODE) {
7960 if (cur->nsDef != NULL) {
7961 ns = cur->nsDef;
7962 do {
7963 shadowed = 0;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007964 if (XML_NSMAP_NOTEMPTY(*map)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007965 /*
7966 * Skip shadowed prefixes.
7967 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007968 XML_NSMAP_FOREACH(*map, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007969 if ((ns->prefix == mi->newNs->prefix) ||
7970 xmlStrEqual(ns->prefix, mi->newNs->prefix)) {
7971 shadowed = 1;
7972 break;
7973 }
7974 }
7975 }
7976 /*
7977 * Insert mapping.
7978 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007979 mi = xmlDOMWrapNsMapAddItem(map, 0, NULL,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007980 ns, XML_TREE_NSMAP_PARENT);
7981 if (mi == NULL)
7982 return (-1);
7983 if (shadowed)
7984 mi->shadowDepth = 0;
7985 ns = ns->next;
7986 } while (ns != NULL);
7987 }
7988 }
7989 cur = cur->parent;
7990 }
7991 return (0);
7992}
7993
7994/*
7995* XML_TREE_ADOPT_STR: If we have a dest-dict, put @str in the dict;
7996* otherwise copy it, when it was in the source-dict.
7997*/
7998#define XML_TREE_ADOPT_STR(str) \
7999 if (adoptStr && (str != NULL)) { \
8000 if (destDoc->dict) { \
Daniel Veillard7e21fd12005-07-03 21:44:07 +00008001 const xmlChar *old = str; \
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008002 str = xmlDictLookup(destDoc->dict, str, -1); \
Daniel Veillard7e21fd12005-07-03 21:44:07 +00008003 if ((sourceDoc == NULL) || (sourceDoc->dict == NULL) || \
8004 (!xmlDictOwns(sourceDoc->dict, old))) \
Daniel Veillard39e5c892005-07-03 22:48:50 +00008005 xmlFree((char *)old); \
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008006 } else if ((sourceDoc) && (sourceDoc->dict) && \
8007 xmlDictOwns(sourceDoc->dict, str)) { \
8008 str = BAD_CAST xmlStrdup(str); \
8009 } \
8010 }
8011
8012/*
8013* XML_TREE_ADOPT_STR_2: If @str was in the source-dict, then
8014* put it in dest-dict or copy it.
8015*/
8016#define XML_TREE_ADOPT_STR_2(str) \
8017 if (adoptStr && (str != NULL) && (sourceDoc != NULL) && \
8018 (sourceDoc->dict != NULL) && \
8019 xmlDictOwns(sourceDoc->dict, cur->content)) { \
8020 if (destDoc->dict) \
8021 cur->content = (xmlChar *) \
8022 xmlDictLookup(destDoc->dict, cur->content, -1); \
8023 else \
8024 cur->content = xmlStrdup(BAD_CAST cur->content); \
8025 }
8026
8027/*
8028* xmlDOMWrapNSNormAddNsMapItem2:
8029*
8030* For internal use. Adds a ns-decl mapping.
8031*
Daniel Veillardaa6de472008-08-25 14:53:31 +00008032* Returns 0 on success, -1 on internal errors.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008033*/
8034static int
8035xmlDOMWrapNSNormAddNsMapItem2(xmlNsPtr **list, int *size, int *number,
8036 xmlNsPtr oldNs, xmlNsPtr newNs)
8037{
8038 if (*list == NULL) {
8039 *list = (xmlNsPtr *) xmlMalloc(6 * sizeof(xmlNsPtr));
8040 if (*list == NULL) {
8041 xmlTreeErrMemory("alloc ns map item");
8042 return(-1);
8043 }
8044 *size = 3;
8045 *number = 0;
8046 } else if ((*number) >= (*size)) {
8047 *size *= 2;
8048 *list = (xmlNsPtr *) xmlRealloc(*list,
8049 (*size) * 2 * sizeof(xmlNsPtr));
8050 if (*list == NULL) {
8051 xmlTreeErrMemory("realloc ns map item");
8052 return(-1);
8053 }
8054 }
8055 (*list)[2 * (*number)] = oldNs;
8056 (*list)[2 * (*number) +1] = newNs;
8057 (*number)++;
8058 return (0);
8059}
8060
8061/*
8062* xmlDOMWrapRemoveNode:
Daniel Veillard304e78c2005-07-03 16:19:41 +00008063* @ctxt: a DOM wrapper context
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008064* @doc: the doc
8065* @node: the node to be removed.
Daniel Veillard304e78c2005-07-03 16:19:41 +00008066* @options: set of options, unused at the moment
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008067*
8068* Unlinks the given node from its owner.
8069* This will substitute ns-references to node->nsDef for
8070* ns-references to doc->oldNs, thus ensuring the removed
8071* branch to be autark wrt ns-references.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008072*
8073* NOTE: This function was not intensively tested.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008074*
8075* Returns 0 on success, 1 if the node is not supported,
Daniel Veillardaa6de472008-08-25 14:53:31 +00008076* -1 on API and internal errors.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008077*/
8078int
8079xmlDOMWrapRemoveNode(xmlDOMWrapCtxtPtr ctxt, xmlDocPtr doc,
8080 xmlNodePtr node, int options ATTRIBUTE_UNUSED)
8081{
8082 xmlNsPtr *list = NULL;
8083 int sizeList, nbList, i, j;
8084 xmlNsPtr ns;
8085
8086 if ((node == NULL) || (doc == NULL) || (node->doc != doc))
8087 return (-1);
8088
8089 /* TODO: 0 or -1 ? */
8090 if (node->parent == NULL)
8091 return (0);
8092
Daniel Veillardaa6de472008-08-25 14:53:31 +00008093 switch (node->type) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008094 case XML_TEXT_NODE:
8095 case XML_CDATA_SECTION_NODE:
8096 case XML_ENTITY_REF_NODE:
8097 case XML_PI_NODE:
8098 case XML_COMMENT_NODE:
8099 xmlUnlinkNode(node);
8100 return (0);
Daniel Veillardaa6de472008-08-25 14:53:31 +00008101 case XML_ELEMENT_NODE:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008102 case XML_ATTRIBUTE_NODE:
8103 break;
8104 default:
8105 return (1);
8106 }
8107 xmlUnlinkNode(node);
8108 /*
8109 * Save out-of-scope ns-references in doc->oldNs.
8110 */
8111 do {
8112 switch (node->type) {
8113 case XML_ELEMENT_NODE:
8114 if ((ctxt == NULL) && (node->nsDef != NULL)) {
8115 ns = node->nsDef;
8116 do {
8117 if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
8118 &nbList, ns, ns) == -1)
8119 goto internal_error;
8120 ns = ns->next;
8121 } while (ns != NULL);
8122 }
8123 /* No break on purpose. */
8124 case XML_ATTRIBUTE_NODE:
Daniel Veillardaa6de472008-08-25 14:53:31 +00008125 if (node->ns != NULL) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008126 /*
8127 * Find a mapping.
8128 */
8129 if (list != NULL) {
8130 for (i = 0, j = 0; i < nbList; i++, j += 2) {
8131 if (node->ns == list[j]) {
8132 node->ns = list[++j];
8133 goto next_node;
8134 }
8135 }
8136 }
8137 ns = NULL;
8138 if (ctxt != NULL) {
8139 /*
8140 * User defined.
8141 */
8142 } else {
8143 /*
8144 * Add to doc's oldNs.
8145 */
8146 ns = xmlDOMWrapStoreNs(doc, node->ns->href,
8147 node->ns->prefix);
8148 if (ns == NULL)
8149 goto internal_error;
8150 }
8151 if (ns != NULL) {
8152 /*
8153 * Add mapping.
8154 */
8155 if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
8156 &nbList, node->ns, ns) == -1)
8157 goto internal_error;
8158 }
8159 node->ns = ns;
8160 }
8161 if ((node->type == XML_ELEMENT_NODE) &&
8162 (node->properties != NULL)) {
8163 node = (xmlNodePtr) node->properties;
8164 continue;
8165 }
8166 break;
8167 default:
8168 goto next_sibling;
Daniel Veillardaa6de472008-08-25 14:53:31 +00008169 }
8170next_node:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008171 if ((node->type == XML_ELEMENT_NODE) &&
8172 (node->children != NULL)) {
8173 node = node->children;
8174 continue;
8175 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00008176next_sibling:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008177 if (node == NULL)
8178 break;
8179 if (node->next != NULL)
8180 node = node->next;
8181 else {
8182 node = node->parent;
8183 goto next_sibling;
8184 }
8185 } while (node != NULL);
8186
8187 if (list != NULL)
8188 xmlFree(list);
8189 return (0);
8190
8191internal_error:
8192 if (list != NULL)
8193 xmlFree(list);
8194 return (-1);
8195}
8196
8197/*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008198* xmlSearchNsByNamespaceStrict:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008199* @doc: the document
8200* @node: the start node
8201* @nsName: the searched namespace name
8202* @retNs: the resulting ns-decl
8203* @prefixed: if the found ns-decl must have a prefix (for attributes)
8204*
8205* Dynamically searches for a ns-declaration which matches
8206* the given @nsName in the ancestor-or-self axis of @node.
8207*
8208* Returns 1 if a ns-decl was found, 0 if not and -1 on API
8209* and internal errors.
8210*/
8211static int
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008212xmlSearchNsByNamespaceStrict(xmlDocPtr doc, xmlNodePtr node,
8213 const xmlChar* nsName,
8214 xmlNsPtr *retNs, int prefixed)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008215{
8216 xmlNodePtr cur, prev = NULL, out = NULL;
8217 xmlNsPtr ns, prevns;
8218
8219 if ((doc == NULL) || (nsName == NULL) || (retNs == NULL))
8220 return (-1);
8221
8222 *retNs = NULL;
8223 if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) {
8224 *retNs = xmlTreeEnsureXMLDecl(doc);
8225 if (*retNs == NULL)
8226 return (-1);
8227 return (1);
8228 }
8229 cur = node;
8230 do {
8231 if (cur->type == XML_ELEMENT_NODE) {
8232 if (cur->nsDef != NULL) {
8233 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
8234 if (prefixed && (ns->prefix == NULL))
8235 continue;
8236 if (prev != NULL) {
8237 /*
8238 * Check the last level of ns-decls for a
8239 * shadowing prefix.
8240 */
8241 prevns = prev->nsDef;
8242 do {
8243 if ((prevns->prefix == ns->prefix) ||
8244 ((prevns->prefix != NULL) &&
8245 (ns->prefix != NULL) &&
8246 xmlStrEqual(prevns->prefix, ns->prefix))) {
8247 /*
8248 * Shadowed.
8249 */
8250 break;
8251 }
8252 prevns = prevns->next;
8253 } while (prevns != NULL);
8254 if (prevns != NULL)
8255 continue;
8256 }
8257 /*
8258 * Ns-name comparison.
8259 */
8260 if ((nsName == ns->href) ||
8261 xmlStrEqual(nsName, ns->href)) {
8262 /*
8263 * At this point the prefix can only be shadowed,
8264 * if we are the the (at least) 3rd level of
8265 * ns-decls.
8266 */
8267 if (out) {
8268 int ret;
Daniel Veillardaa6de472008-08-25 14:53:31 +00008269
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008270 ret = xmlNsInScope(doc, node, prev, ns->prefix);
8271 if (ret < 0)
8272 return (-1);
8273 /*
8274 * TODO: Should we try to find a matching ns-name
8275 * only once? This here keeps on searching.
8276 * I think we should try further since, there might
8277 * be an other matching ns-decl with an unshadowed
8278 * prefix.
8279 */
8280 if (! ret)
8281 continue;
8282 }
8283 *retNs = ns;
8284 return (1);
8285 }
8286 }
8287 out = prev;
8288 prev = cur;
8289 }
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008290 } else if ((cur->type == XML_ENTITY_NODE) ||
8291 (cur->type == XML_ENTITY_DECL))
8292 return (0);
8293 cur = cur->parent;
8294 } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
8295 return (0);
8296}
8297
8298/*
8299* xmlSearchNsByPrefixStrict:
8300* @doc: the document
8301* @node: the start node
8302* @prefix: the searched namespace prefix
8303* @retNs: the resulting ns-decl
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008304*
8305* Dynamically searches for a ns-declaration which matches
8306* the given @nsName in the ancestor-or-self axis of @node.
8307*
8308* Returns 1 if a ns-decl was found, 0 if not and -1 on API
8309* and internal errors.
8310*/
8311static int
8312xmlSearchNsByPrefixStrict(xmlDocPtr doc, xmlNodePtr node,
8313 const xmlChar* prefix,
8314 xmlNsPtr *retNs)
8315{
8316 xmlNodePtr cur;
8317 xmlNsPtr ns;
8318
8319 if ((doc == NULL) || (node == NULL))
8320 return (-1);
8321
8322 if (retNs)
8323 *retNs = NULL;
8324 if (IS_STR_XML(prefix)) {
8325 if (retNs) {
8326 *retNs = xmlTreeEnsureXMLDecl(doc);
8327 if (*retNs == NULL)
8328 return (-1);
8329 }
8330 return (1);
8331 }
8332 cur = node;
8333 do {
8334 if (cur->type == XML_ELEMENT_NODE) {
8335 if (cur->nsDef != NULL) {
8336 ns = cur->nsDef;
Daniel Veillardaa6de472008-08-25 14:53:31 +00008337 do {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008338 if ((prefix == ns->prefix) ||
8339 xmlStrEqual(prefix, ns->prefix))
8340 {
8341 /*
8342 * Disabled namespaces, e.g. xmlns:abc="".
8343 */
8344 if (ns->href == NULL)
8345 return(0);
8346 if (retNs)
8347 *retNs = ns;
8348 return (1);
8349 }
8350 ns = ns->next;
Daniel Veillardaa6de472008-08-25 14:53:31 +00008351 } while (ns != NULL);
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008352 }
8353 } else if ((cur->type == XML_ENTITY_NODE) ||
8354 (cur->type == XML_ENTITY_DECL))
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008355 return (0);
8356 cur = cur->parent;
8357 } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
8358 return (0);
8359}
8360
8361/*
8362* xmlDOMWrapNSNormDeclareNsForced:
8363* @doc: the doc
8364* @elem: the element-node to declare on
8365* @nsName: the namespace-name of the ns-decl
8366* @prefix: the preferred prefix of the ns-decl
8367* @checkShadow: ensure that the new ns-decl doesn't shadow ancestor ns-decls
8368*
8369* Declares a new namespace on @elem. It tries to use the
8370* given @prefix; if a ns-decl with the given prefix is already existent
8371* on @elem, it will generate an other prefix.
8372*
8373* Returns 1 if a ns-decl was found, 0 if not and -1 on API
8374* and internal errors.
8375*/
8376static xmlNsPtr
8377xmlDOMWrapNSNormDeclareNsForced(xmlDocPtr doc,
8378 xmlNodePtr elem,
8379 const xmlChar *nsName,
8380 const xmlChar *prefix,
8381 int checkShadow)
8382{
8383
8384 xmlNsPtr ret;
8385 char buf[50];
8386 const xmlChar *pref;
8387 int counter = 0;
8388 /*
8389 * Create a ns-decl on @anchor.
8390 */
8391 pref = prefix;
8392 while (1) {
8393 /*
8394 * Lookup whether the prefix is unused in elem's ns-decls.
8395 */
8396 if ((elem->nsDef != NULL) &&
8397 (xmlTreeNSListLookupByPrefix(elem->nsDef, pref) != NULL))
8398 goto ns_next_prefix;
8399 if (checkShadow && elem->parent &&
8400 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8401 /*
8402 * Does it shadow ancestor ns-decls?
8403 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008404 if (xmlSearchNsByPrefixStrict(doc, elem->parent, pref, NULL) == 1)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008405 goto ns_next_prefix;
8406 }
8407 ret = xmlNewNs(NULL, nsName, pref);
8408 if (ret == NULL)
8409 return (NULL);
8410 if (elem->nsDef == NULL)
8411 elem->nsDef = ret;
8412 else {
8413 xmlNsPtr ns2 = elem->nsDef;
8414 while (ns2->next != NULL)
8415 ns2 = ns2->next;
8416 ns2->next = ret;
8417 }
8418 return (ret);
8419ns_next_prefix:
8420 counter++;
8421 if (counter > 1000)
8422 return (NULL);
8423 if (prefix == NULL) {
8424 snprintf((char *) buf, sizeof(buf),
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008425 "ns_%d", counter);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008426 } else
8427 snprintf((char *) buf, sizeof(buf),
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008428 "%.30s_%d", (char *)prefix, counter);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008429 pref = BAD_CAST buf;
8430 }
8431}
8432
8433/*
8434* xmlDOMWrapNSNormAquireNormalizedNs:
8435* @doc: the doc
8436* @elem: the element-node to declare namespaces on
8437* @ns: the ns-struct to use for the search
8438* @retNs: the found/created ns-struct
8439* @nsMap: the ns-map
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008440* @depth: the current tree depth
8441* @ancestorsOnly: search in ancestor ns-decls only
8442* @prefixed: if the searched ns-decl must have a prefix (for attributes)
8443*
8444* Searches for a matching ns-name in the ns-decls of @nsMap, if not
8445* found it will either declare it on @elem, or store it in doc->oldNs.
8446* If a new ns-decl needs to be declared on @elem, it tries to use the
8447* @ns->prefix for it, if this prefix is already in use on @elem, it will
8448* change the prefix or the new ns-decl.
8449*
8450* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8451*/
8452static int
8453xmlDOMWrapNSNormAquireNormalizedNs(xmlDocPtr doc,
8454 xmlNodePtr elem,
8455 xmlNsPtr ns,
8456 xmlNsPtr *retNs,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008457 xmlNsMapPtr *nsMap,
Daniel Veillardaa6de472008-08-25 14:53:31 +00008458
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008459 int depth,
8460 int ancestorsOnly,
8461 int prefixed)
8462{
Daniel Veillardaa6de472008-08-25 14:53:31 +00008463 xmlNsMapItemPtr mi;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008464
8465 if ((doc == NULL) || (ns == NULL) || (retNs == NULL) ||
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008466 (nsMap == NULL))
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008467 return (-1);
8468
8469 *retNs = NULL;
8470 /*
8471 * Handle XML namespace.
8472 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008473 if (IS_STR_XML(ns->prefix)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008474 /*
8475 * Insert XML namespace mapping.
8476 */
8477 *retNs = xmlTreeEnsureXMLDecl(doc);
8478 if (*retNs == NULL)
8479 return (-1);
8480 return (0);
8481 }
8482 /*
8483 * If the search should be done in ancestors only and no
8484 * @elem (the first ancestor) was specified, then skip the search.
8485 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008486 if ((XML_NSMAP_NOTEMPTY(*nsMap)) &&
8487 (! (ancestorsOnly && (elem == NULL))))
Daniel Veillardaa6de472008-08-25 14:53:31 +00008488 {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008489 /*
8490 * Try to find an equal ns-name in in-scope ns-decls.
8491 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008492 XML_NSMAP_FOREACH(*nsMap, mi) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00008493 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8494 /*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008495 * ancestorsOnly: This should be turned on to gain speed,
8496 * if one knows that the branch itself was already
8497 * ns-wellformed and no stale references existed.
8498 * I.e. it searches in the ancestor axis only.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008499 */
8500 ((! ancestorsOnly) || (mi->depth == XML_TREE_NSMAP_PARENT)) &&
8501 /* Skip shadowed prefixes. */
Daniel Veillardaa6de472008-08-25 14:53:31 +00008502 (mi->shadowDepth == -1) &&
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008503 /* Skip xmlns="" or xmlns:foo="". */
8504 ((mi->newNs->href != NULL) &&
Daniel Veillardaa6de472008-08-25 14:53:31 +00008505 (mi->newNs->href[0] != 0)) &&
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008506 /* Ensure a prefix if wanted. */
8507 ((! prefixed) || (mi->newNs->prefix != NULL)) &&
8508 /* Equal ns name */
8509 ((mi->newNs->href == ns->href) ||
8510 xmlStrEqual(mi->newNs->href, ns->href))) {
8511 /* Set the mapping. */
8512 mi->oldNs = ns;
8513 *retNs = mi->newNs;
8514 return (0);
8515 }
8516 }
8517 }
8518 /*
8519 * No luck, the namespace is out of scope or shadowed.
8520 */
8521 if (elem == NULL) {
8522 xmlNsPtr tmpns;
8523
8524 /*
8525 * Store ns-decls in "oldNs" of the document-node.
8526 */
8527 tmpns = xmlDOMWrapStoreNs(doc, ns->href, ns->prefix);
8528 if (tmpns == NULL)
8529 return (-1);
8530 /*
8531 * Insert mapping.
Daniel Veillardaa6de472008-08-25 14:53:31 +00008532 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008533 if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008534 tmpns, XML_TREE_NSMAP_DOC) == NULL) {
8535 xmlFreeNs(tmpns);
8536 return (-1);
8537 }
8538 *retNs = tmpns;
8539 } else {
8540 xmlNsPtr tmpns;
8541
8542 tmpns = xmlDOMWrapNSNormDeclareNsForced(doc, elem, ns->href,
8543 ns->prefix, 0);
8544 if (tmpns == NULL)
8545 return (-1);
8546
8547 if (*nsMap != NULL) {
8548 /*
8549 * Does it shadow ancestor ns-decls?
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008550 */
8551 XML_NSMAP_FOREACH(*nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008552 if ((mi->depth < depth) &&
8553 (mi->shadowDepth == -1) &&
8554 ((ns->prefix == mi->newNs->prefix) ||
8555 xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
8556 /*
8557 * Shadows.
8558 */
8559 mi->shadowDepth = depth;
8560 break;
8561 }
8562 }
8563 }
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008564 if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns, tmpns, depth) == NULL) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008565 xmlFreeNs(tmpns);
8566 return (-1);
8567 }
8568 *retNs = tmpns;
8569 }
8570 return (0);
8571}
8572
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008573typedef enum {
Daniel Veillardaa6de472008-08-25 14:53:31 +00008574 XML_DOM_RECONNS_REMOVEREDUND = 1<<0
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008575} xmlDOMReconcileNSOptions;
8576
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008577/*
8578* xmlDOMWrapReconcileNamespaces:
Daniel Veillard304e78c2005-07-03 16:19:41 +00008579* @ctxt: DOM wrapper context, unused at the moment
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008580* @elem: the element-node
8581* @options: option flags
8582*
8583* Ensures that ns-references point to ns-decls hold on element-nodes.
8584* Ensures that the tree is namespace wellformed by creating additional
8585* ns-decls where needed. Note that, since prefixes of already existent
8586* ns-decls can be shadowed by this process, it could break QNames in
8587* attribute values or element content.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008588*
8589* NOTE: This function was not intensively tested.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008590*
8591* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
Daniel Veillardaa6de472008-08-25 14:53:31 +00008592*/
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008593
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008594int
8595xmlDOMWrapReconcileNamespaces(xmlDOMWrapCtxtPtr ctxt ATTRIBUTE_UNUSED,
8596 xmlNodePtr elem,
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008597 int options)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008598{
8599 int depth = -1, adoptns = 0, parnsdone = 0;
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008600 xmlNsPtr ns, prevns;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008601 xmlDocPtr doc;
8602 xmlNodePtr cur, curElem = NULL;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008603 xmlNsMapPtr nsMap = NULL;
8604 xmlNsMapItemPtr /* topmi = NULL, */ mi;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008605 /* @ancestorsOnly should be set by an option flag. */
8606 int ancestorsOnly = 0;
Daniel Veillardaa6de472008-08-25 14:53:31 +00008607 int optRemoveRedundantNS =
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008608 ((xmlDOMReconcileNSOptions) options & XML_DOM_RECONNS_REMOVEREDUND) ? 1 : 0;
8609 xmlNsPtr *listRedund = NULL;
8610 int sizeRedund = 0, nbRedund = 0, ret, i, j;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008611
8612 if ((elem == NULL) || (elem->doc == NULL) ||
8613 (elem->type != XML_ELEMENT_NODE))
8614 return (-1);
8615
8616 doc = elem->doc;
8617 cur = elem;
8618 do {
8619 switch (cur->type) {
8620 case XML_ELEMENT_NODE:
8621 adoptns = 1;
8622 curElem = cur;
8623 depth++;
8624 /*
8625 * Namespace declarations.
8626 */
8627 if (cur->nsDef != NULL) {
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008628 prevns = NULL;
Kasimier T. Buchcike8f8d752006-02-02 12:13:07 +00008629 ns = cur->nsDef;
8630 while (ns != NULL) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008631 if (! parnsdone) {
8632 if ((elem->parent) &&
8633 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8634 /*
8635 * Gather ancestor in-scope ns-decls.
8636 */
8637 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8638 elem->parent) == -1)
8639 goto internal_error;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008640 }
8641 parnsdone = 1;
8642 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00008643
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008644 /*
8645 * Lookup the ns ancestor-axis for equal ns-decls in scope.
8646 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008647 if (optRemoveRedundantNS && XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008648 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008649 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8650 (mi->shadowDepth == -1) &&
8651 ((ns->prefix == mi->newNs->prefix) ||
8652 xmlStrEqual(ns->prefix, mi->newNs->prefix)) &&
8653 ((ns->href == mi->newNs->href) ||
8654 xmlStrEqual(ns->href, mi->newNs->href)))
Daniel Veillardaa6de472008-08-25 14:53:31 +00008655 {
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008656 /*
8657 * A redundant ns-decl was found.
8658 * Add it to the list of redundant ns-decls.
8659 */
8660 if (xmlDOMWrapNSNormAddNsMapItem2(&listRedund,
8661 &sizeRedund, &nbRedund, ns, mi->newNs) == -1)
8662 goto internal_error;
8663 /*
8664 * Remove the ns-decl from the element-node.
Daniel Veillardaa6de472008-08-25 14:53:31 +00008665 */
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008666 if (prevns)
8667 prevns->next = ns->next;
8668 else
Daniel Veillardaa6de472008-08-25 14:53:31 +00008669 cur->nsDef = ns->next;
Kasimier T. Buchcike8f8d752006-02-02 12:13:07 +00008670 goto next_ns_decl;
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008671 }
8672 }
8673 }
8674
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008675 /*
8676 * Skip ns-references handling if the referenced
8677 * ns-decl is declared on the same element.
8678 */
8679 if ((cur->ns != NULL) && adoptns && (cur->ns == ns))
Daniel Veillardaa6de472008-08-25 14:53:31 +00008680 adoptns = 0;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008681 /*
8682 * Does it shadow any ns-decl?
8683 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008684 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8685 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008686 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8687 (mi->shadowDepth == -1) &&
8688 ((ns->prefix == mi->newNs->prefix) ||
8689 xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00008690
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008691 mi->shadowDepth = depth;
8692 }
8693 }
8694 }
8695 /*
8696 * Push mapping.
8697 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008698 if (xmlDOMWrapNsMapAddItem(&nsMap, -1, ns, ns,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008699 depth) == NULL)
Daniel Veillardaa6de472008-08-25 14:53:31 +00008700 goto internal_error;
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008701
8702 prevns = ns;
Kasimier T. Buchcike8f8d752006-02-02 12:13:07 +00008703next_ns_decl:
Daniel Veillardaa6de472008-08-25 14:53:31 +00008704 ns = ns->next;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008705 }
8706 }
8707 if (! adoptns)
8708 goto ns_end;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008709 /* No break on purpose. */
8710 case XML_ATTRIBUTE_NODE:
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008711 /* No ns, no fun. */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008712 if (cur->ns == NULL)
8713 goto ns_end;
Daniel Veillardaa6de472008-08-25 14:53:31 +00008714
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008715 if (! parnsdone) {
8716 if ((elem->parent) &&
8717 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8718 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8719 elem->parent) == -1)
8720 goto internal_error;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008721 }
8722 parnsdone = 1;
8723 }
8724 /*
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008725 * Adjust the reference if this was a redundant ns-decl.
8726 */
8727 if (listRedund) {
8728 for (i = 0, j = 0; i < nbRedund; i++, j += 2) {
8729 if (cur->ns == listRedund[j]) {
8730 cur->ns = listRedund[++j];
8731 break;
8732 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00008733 }
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008734 }
8735 /*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008736 * Adopt ns-references.
8737 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008738 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008739 /*
8740 * Search for a mapping.
8741 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008742 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008743 if ((mi->shadowDepth == -1) &&
8744 (cur->ns == mi->oldNs)) {
8745
8746 cur->ns = mi->newNs;
8747 goto ns_end;
8748 }
8749 }
8750 }
8751 /*
8752 * Aquire a normalized ns-decl and add it to the map.
8753 */
8754 if (xmlDOMWrapNSNormAquireNormalizedNs(doc, curElem,
8755 cur->ns, &ns,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008756 &nsMap, depth,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008757 ancestorsOnly,
8758 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
8759 goto internal_error;
8760 cur->ns = ns;
8761
8762ns_end:
8763 if ((cur->type == XML_ELEMENT_NODE) &&
8764 (cur->properties != NULL)) {
8765 /*
8766 * Process attributes.
8767 */
8768 cur = (xmlNodePtr) cur->properties;
8769 continue;
8770 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00008771 break;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008772 default:
8773 goto next_sibling;
8774 }
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008775into_content:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008776 if ((cur->type == XML_ELEMENT_NODE) &&
8777 (cur->children != NULL)) {
8778 /*
8779 * Process content of element-nodes only.
8780 */
8781 cur = cur->children;
8782 continue;
8783 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00008784next_sibling:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008785 if (cur == elem)
8786 break;
8787 if (cur->type == XML_ELEMENT_NODE) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00008788 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008789 /*
8790 * Pop mappings.
8791 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008792 while ((nsMap->last != NULL) &&
8793 (nsMap->last->depth >= depth))
8794 {
Daniel Veillardaa6de472008-08-25 14:53:31 +00008795 XML_NSMAP_POP(nsMap, mi)
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008796 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008797 /*
8798 * Unshadow.
8799 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008800 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008801 if (mi->shadowDepth >= depth)
8802 mi->shadowDepth = -1;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008803 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00008804 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008805 depth--;
8806 }
8807 if (cur->next != NULL)
8808 cur = cur->next;
8809 else {
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008810 if (cur->type == XML_ATTRIBUTE_NODE) {
8811 cur = cur->parent;
8812 goto into_content;
8813 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008814 cur = cur->parent;
8815 goto next_sibling;
8816 }
8817 } while (cur != NULL);
Daniel Veillardaa6de472008-08-25 14:53:31 +00008818
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008819 ret = 0;
8820 goto exit;
8821internal_error:
8822 ret = -1;
8823exit:
Daniel Veillardaa6de472008-08-25 14:53:31 +00008824 if (listRedund) {
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008825 for (i = 0, j = 0; i < nbRedund; i++, j += 2) {
8826 xmlFreeNs(listRedund[j]);
8827 }
8828 xmlFree(listRedund);
8829 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008830 if (nsMap != NULL)
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008831 xmlDOMWrapNsMapFree(nsMap);
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008832 return (ret);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008833}
8834
8835/*
8836* xmlDOMWrapAdoptBranch:
8837* @ctxt: the optional context for custom processing
8838* @sourceDoc: the optional sourceDoc
8839* @node: the element-node to start with
8840* @destDoc: the destination doc for adoption
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008841* @destParent: the optional new parent of @node in @destDoc
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008842* @options: option flags
8843*
8844* Ensures that ns-references point to @destDoc: either to
8845* elements->nsDef entries if @destParent is given, or to
8846* @destDoc->oldNs otherwise.
8847* If @destParent is given, it ensures that the tree is namespace
8848* wellformed by creating additional ns-decls where needed.
8849* Note that, since prefixes of already existent ns-decls can be
8850* shadowed by this process, it could break QNames in attribute
8851* values or element content.
8852*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008853* NOTE: This function was not intensively tested.
8854*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008855* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8856*/
8857static int
8858xmlDOMWrapAdoptBranch(xmlDOMWrapCtxtPtr ctxt,
8859 xmlDocPtr sourceDoc,
8860 xmlNodePtr node,
8861 xmlDocPtr destDoc,
8862 xmlNodePtr destParent,
8863 int options ATTRIBUTE_UNUSED)
8864{
8865 int ret = 0;
8866 xmlNodePtr cur, curElem = NULL;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008867 xmlNsMapPtr nsMap = NULL;
8868 xmlNsMapItemPtr mi;
Daniel Veillard11ce4002006-03-10 00:36:23 +00008869 xmlNsPtr ns = NULL;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008870 int depth = -1, adoptStr = 1;
8871 /* gather @parent's ns-decls. */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008872 int parnsdone;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008873 /* @ancestorsOnly should be set per option. */
8874 int ancestorsOnly = 0;
Daniel Veillardaa6de472008-08-25 14:53:31 +00008875
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008876 /*
8877 * Optimize string adoption for equal or none dicts.
8878 */
8879 if ((sourceDoc != NULL) &&
8880 (sourceDoc->dict == destDoc->dict))
8881 adoptStr = 0;
8882 else
8883 adoptStr = 1;
8884
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008885 /*
8886 * Get the ns-map from the context if available.
8887 */
8888 if (ctxt)
8889 nsMap = (xmlNsMapPtr) ctxt->namespaceMap;
8890 /*
8891 * Disable search for ns-decls in the parent-axis of the
8892 * desination element, if:
8893 * 1) there's no destination parent
8894 * 2) custom ns-reference handling is used
8895 */
8896 if ((destParent == NULL) ||
8897 (ctxt && ctxt->getNsForNodeFunc))
8898 {
8899 parnsdone = 1;
8900 } else
8901 parnsdone = 0;
8902
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008903 cur = node;
8904 while (cur != NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008905 /*
8906 * Paranoid source-doc sanity check.
8907 */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008908 if (cur->doc != sourceDoc) {
8909 /*
8910 * We'll assume XIncluded nodes if the doc differs.
8911 * TODO: Do we need to reconciliate XIncluded nodes?
8912 * This here skips XIncluded nodes and tries to handle
8913 * broken sequences.
8914 */
8915 if (cur->next == NULL)
8916 goto leave_node;
8917 do {
8918 cur = cur->next;
8919 if ((cur->type == XML_XINCLUDE_END) ||
8920 (cur->doc == node->doc))
8921 break;
8922 } while (cur->next != NULL);
8923
8924 if (cur->doc != node->doc)
8925 goto leave_node;
8926 }
8927 cur->doc = destDoc;
8928 switch (cur->type) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00008929 case XML_XINCLUDE_START:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008930 case XML_XINCLUDE_END:
8931 /*
8932 * TODO
8933 */
8934 return (-1);
Daniel Veillardaa6de472008-08-25 14:53:31 +00008935 case XML_ELEMENT_NODE:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008936 curElem = cur;
8937 depth++;
8938 /*
Daniel Veillardaa6de472008-08-25 14:53:31 +00008939 * Namespace declarations.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008940 * - ns->href and ns->prefix are never in the dict, so
8941 * we need not move the values over to the destination dict.
8942 * - Note that for custom handling of ns-references,
8943 * the ns-decls need not be stored in the ns-map,
8944 * since they won't be referenced by node->ns.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008945 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008946 if ((cur->nsDef) &&
8947 ((ctxt == NULL) || (ctxt->getNsForNodeFunc == NULL)))
8948 {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008949 if (! parnsdone) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008950 /*
8951 * Gather @parent's in-scope ns-decls.
8952 */
8953 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8954 destParent) == -1)
8955 goto internal_error;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008956 parnsdone = 1;
8957 }
8958 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
8959 /*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008960 * NOTE: ns->prefix and ns->href are never in the dict.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008961 * XML_TREE_ADOPT_STR(ns->prefix)
8962 * XML_TREE_ADOPT_STR(ns->href)
Daniel Veillardaa6de472008-08-25 14:53:31 +00008963 */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008964 /*
8965 * Does it shadow any ns-decl?
Daniel Veillardaa6de472008-08-25 14:53:31 +00008966 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008967 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008968 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008969 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8970 (mi->shadowDepth == -1) &&
8971 ((ns->prefix == mi->newNs->prefix) ||
8972 xmlStrEqual(ns->prefix,
8973 mi->newNs->prefix))) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00008974
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008975 mi->shadowDepth = depth;
8976 }
8977 }
8978 }
8979 /*
8980 * Push mapping.
8981 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008982 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008983 ns, ns, depth) == NULL)
8984 goto internal_error;
8985 }
8986 }
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008987 /* No break on purpose. */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008988 case XML_ATTRIBUTE_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008989 /* No namespace, no fun. */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008990 if (cur->ns == NULL)
8991 goto ns_end;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008992
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008993 if (! parnsdone) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008994 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8995 destParent) == -1)
8996 goto internal_error;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008997 parnsdone = 1;
8998 }
8999 /*
9000 * Adopt ns-references.
9001 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009002 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009003 /*
9004 * Search for a mapping.
9005 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009006 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009007 if ((mi->shadowDepth == -1) &&
9008 (cur->ns == mi->oldNs)) {
9009
9010 cur->ns = mi->newNs;
9011 goto ns_end;
9012 }
9013 }
9014 }
9015 /*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009016 * No matching namespace in scope. We need a new one.
9017 */
9018 if ((ctxt) && (ctxt->getNsForNodeFunc)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009019 /*
9020 * User-defined behaviour.
9021 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009022 ns = ctxt->getNsForNodeFunc(ctxt, cur,
9023 cur->ns->href, cur->ns->prefix);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009024 /*
9025 * Insert mapping if ns is available; it's the users fault
9026 * if not.
9027 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009028 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009029 cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009030 goto internal_error;
9031 cur->ns = ns;
9032 } else {
9033 /*
9034 * Aquire a normalized ns-decl and add it to the map.
9035 */
9036 if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc,
Daniel Veillardaa6de472008-08-25 14:53:31 +00009037 /* ns-decls on curElem or on destDoc->oldNs */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009038 destParent ? curElem : NULL,
9039 cur->ns, &ns,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009040 &nsMap, depth,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009041 ancestorsOnly,
9042 /* ns-decls must be prefixed for attributes. */
9043 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
9044 goto internal_error;
9045 cur->ns = ns;
9046 }
9047ns_end:
9048 /*
9049 * Further node properties.
9050 * TODO: Is this all?
9051 */
9052 XML_TREE_ADOPT_STR(cur->name)
9053 if (cur->type == XML_ELEMENT_NODE) {
9054 cur->psvi = NULL;
9055 cur->line = 0;
9056 cur->extra = 0;
9057 /*
9058 * Walk attributes.
9059 */
9060 if (cur->properties != NULL) {
9061 /*
9062 * Process first attribute node.
9063 */
9064 cur = (xmlNodePtr) cur->properties;
9065 continue;
9066 }
9067 } else {
9068 /*
9069 * Attributes.
9070 */
9071 if ((sourceDoc != NULL) &&
9072 (((xmlAttrPtr) cur)->atype == XML_ATTRIBUTE_ID))
Daniel Veillardaa6de472008-08-25 14:53:31 +00009073 {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009074 xmlRemoveID(sourceDoc, (xmlAttrPtr) cur);
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009075 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009076 ((xmlAttrPtr) cur)->atype = 0;
9077 ((xmlAttrPtr) cur)->psvi = NULL;
9078 }
9079 break;
9080 case XML_TEXT_NODE:
Daniel Veillardaa6de472008-08-25 14:53:31 +00009081 case XML_CDATA_SECTION_NODE:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009082 /*
9083 * This puts the content in the dest dict, only if
9084 * it was previously in the source dict.
9085 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009086 XML_TREE_ADOPT_STR_2(cur->content)
9087 goto leave_node;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009088 case XML_ENTITY_REF_NODE:
9089 /*
9090 * Remove reference to the entitity-node.
9091 */
9092 cur->content = NULL;
9093 cur->children = NULL;
9094 cur->last = NULL;
9095 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9096 xmlEntityPtr ent;
9097 /*
9098 * Assign new entity-node if available.
9099 */
9100 ent = xmlGetDocEntity(destDoc, cur->name);
9101 if (ent != NULL) {
9102 cur->content = ent->content;
9103 cur->children = (xmlNodePtr) ent;
9104 cur->last = (xmlNodePtr) ent;
9105 }
9106 }
9107 goto leave_node;
9108 case XML_PI_NODE:
9109 XML_TREE_ADOPT_STR(cur->name)
9110 XML_TREE_ADOPT_STR_2(cur->content)
9111 break;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009112 case XML_COMMENT_NODE:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009113 break;
9114 default:
9115 goto internal_error;
9116 }
9117 /*
9118 * Walk the tree.
9119 */
9120 if (cur->children != NULL) {
9121 cur = cur->children;
9122 continue;
9123 }
9124
9125leave_node:
9126 if (cur == node)
9127 break;
9128 if ((cur->type == XML_ELEMENT_NODE) ||
9129 (cur->type == XML_XINCLUDE_START) ||
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009130 (cur->type == XML_XINCLUDE_END))
9131 {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009132 /*
9133 * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
9134 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009135 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009136 /*
9137 * Pop mappings.
9138 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009139 while ((nsMap->last != NULL) &&
9140 (nsMap->last->depth >= depth))
9141 {
Daniel Veillardaa6de472008-08-25 14:53:31 +00009142 XML_NSMAP_POP(nsMap, mi)
9143 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009144 /*
9145 * Unshadow.
9146 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009147 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009148 if (mi->shadowDepth >= depth)
9149 mi->shadowDepth = -1;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009150 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009151 }
9152 depth--;
9153 }
9154 if (cur->next != NULL)
9155 cur = cur->next;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009156 else if ((cur->type == XML_ATTRIBUTE_NODE) &&
9157 (cur->parent->children != NULL))
9158 {
9159 cur = cur->parent->children;
9160 } else {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009161 cur = cur->parent;
9162 goto leave_node;
9163 }
9164 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009165
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009166 goto exit;
9167
Daniel Veillardaa6de472008-08-25 14:53:31 +00009168internal_error:
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009169 ret = -1;
9170
9171exit:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009172 /*
9173 * Cleanup.
9174 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009175 if (nsMap != NULL) {
9176 if ((ctxt) && (ctxt->namespaceMap == nsMap)) {
9177 /*
9178 * Just cleanup the map but don't free.
9179 */
9180 if (nsMap->first) {
9181 if (nsMap->pool)
9182 nsMap->last->next = nsMap->pool;
9183 nsMap->pool = nsMap->first;
9184 nsMap->first = NULL;
9185 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009186 } else
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009187 xmlDOMWrapNsMapFree(nsMap);
9188 }
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009189 return(ret);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009190}
9191
9192/*
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009193* xmlDOMWrapCloneNode:
9194* @ctxt: the optional context for custom processing
9195* @sourceDoc: the optional sourceDoc
9196* @node: the node to start with
9197* @resNode: the clone of the given @node
9198* @destDoc: the destination doc
9199* @destParent: the optional new parent of @node in @destDoc
Daniel Veillardb2f8f1d2006-04-28 16:30:48 +00009200* @deep: descend into child if set
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009201* @options: option flags
9202*
9203* References of out-of scope ns-decls are remapped to point to @destDoc:
9204* 1) If @destParent is given, then nsDef entries on element-nodes are used
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009205* 2) If *no* @destParent is given, then @destDoc->oldNs entries are used.
9206* This is the case when you don't know already where the cloned branch
9207* will be added to.
Daniel Veillardaa6de472008-08-25 14:53:31 +00009208*
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009209* If @destParent is given, it ensures that the tree is namespace
9210* wellformed by creating additional ns-decls where needed.
9211* Note that, since prefixes of already existent ns-decls can be
9212* shadowed by this process, it could break QNames in attribute
9213* values or element content.
9214* TODO:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009215* 1) What to do with XInclude? Currently this returns an error for XInclude.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009216*
9217* Returns 0 if the operation succeeded,
9218* 1 if a node of unsupported (or not yet supported) type was given,
9219* -1 on API/internal errors.
9220*/
9221
9222int
9223xmlDOMWrapCloneNode(xmlDOMWrapCtxtPtr ctxt,
9224 xmlDocPtr sourceDoc,
9225 xmlNodePtr node,
9226 xmlNodePtr *resNode,
9227 xmlDocPtr destDoc,
9228 xmlNodePtr destParent,
9229 int deep,
9230 int options ATTRIBUTE_UNUSED)
9231{
9232 int ret = 0;
9233 xmlNodePtr cur, curElem = NULL;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009234 xmlNsMapPtr nsMap = NULL;
9235 xmlNsMapItemPtr mi;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009236 xmlNsPtr ns;
9237 int depth = -1;
9238 /* int adoptStr = 1; */
9239 /* gather @parent's ns-decls. */
9240 int parnsdone = 0;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009241 /*
Daniel Veillardaa6de472008-08-25 14:53:31 +00009242 * @ancestorsOnly:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009243 * TODO: @ancestorsOnly should be set per option.
9244 *
9245 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009246 int ancestorsOnly = 0;
9247 xmlNodePtr resultClone = NULL, clone = NULL, parentClone = NULL, prevClone = NULL;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009248 xmlNsPtr cloneNs = NULL, *cloneNsDefSlot = NULL;
9249 xmlDictPtr dict; /* The destination dict */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009250
Daniel Veillard11ce4002006-03-10 00:36:23 +00009251 if ((node == NULL) || (resNode == NULL) || (destDoc == NULL))
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009252 return(-1);
9253 /*
9254 * TODO: Initially we support only element-nodes.
9255 */
9256 if (node->type != XML_ELEMENT_NODE)
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009257 return(1);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009258 /*
9259 * Check node->doc sanity.
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009260 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009261 if ((node->doc != NULL) && (sourceDoc != NULL) &&
9262 (node->doc != sourceDoc)) {
9263 /*
9264 * Might be an XIncluded node.
9265 */
9266 return (-1);
9267 }
9268 if (sourceDoc == NULL)
Daniel Veillardaa6de472008-08-25 14:53:31 +00009269 sourceDoc = node->doc;
Daniel Veillard11ce4002006-03-10 00:36:23 +00009270 if (sourceDoc == NULL)
9271 return (-1);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009272
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009273 dict = destDoc->dict;
9274 /*
9275 * Reuse the namespace map of the context.
9276 */
9277 if (ctxt)
9278 nsMap = (xmlNsMapPtr) ctxt->namespaceMap;
9279
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009280 *resNode = NULL;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009281
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009282 cur = node;
9283 while (cur != NULL) {
9284 if (cur->doc != sourceDoc) {
9285 /*
9286 * We'll assume XIncluded nodes if the doc differs.
9287 * TODO: Do we need to reconciliate XIncluded nodes?
9288 * TODO: This here returns -1 in this case.
9289 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009290 goto internal_error;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009291 }
9292 /*
9293 * Create a new node.
9294 */
9295 switch (cur->type) {
9296 case XML_XINCLUDE_START:
9297 case XML_XINCLUDE_END:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009298 /*
9299 * TODO: What to do with XInclude?
9300 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009301 goto internal_error;
9302 break;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009303 case XML_ELEMENT_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009304 case XML_TEXT_NODE:
Daniel Veillardaa6de472008-08-25 14:53:31 +00009305 case XML_CDATA_SECTION_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009306 case XML_COMMENT_NODE:
Daniel Veillardaa6de472008-08-25 14:53:31 +00009307 case XML_PI_NODE:
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009308 case XML_DOCUMENT_FRAG_NODE:
9309 case XML_ENTITY_REF_NODE:
9310 case XML_ENTITY_NODE:
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009311 /*
9312 * Nodes of xmlNode structure.
9313 */
9314 clone = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
9315 if (clone == NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009316 xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating a node");
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009317 goto internal_error;
9318 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009319 memset(clone, 0, sizeof(xmlNode));
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009320 /*
9321 * Set hierachical links.
9322 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009323 if (resultClone != NULL) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009324 clone->parent = parentClone;
9325 if (prevClone) {
9326 prevClone->next = clone;
9327 clone->prev = prevClone;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009328 } else
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009329 parentClone->children = clone;
9330 } else
9331 resultClone = clone;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009332
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009333 break;
9334 case XML_ATTRIBUTE_NODE:
9335 /*
9336 * Attributes (xmlAttr).
9337 */
9338 clone = (xmlNodePtr) xmlMalloc(sizeof(xmlAttr));
9339 if (clone == NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009340 xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating an attr-node");
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009341 goto internal_error;
9342 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009343 memset(clone, 0, sizeof(xmlAttr));
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009344 /*
9345 * Set hierachical links.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009346 * TODO: Change this to add to the end of attributes.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009347 */
9348 if (resultClone != NULL) {
9349 clone->parent = parentClone;
9350 if (prevClone) {
9351 prevClone->next = clone;
9352 clone->prev = prevClone;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009353 } else
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009354 parentClone->properties = (xmlAttrPtr) clone;
9355 } else
9356 resultClone = clone;
9357 break;
9358 default:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009359 /*
9360 * TODO QUESTION: Any other nodes expected?
9361 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009362 goto internal_error;
9363 }
9364
9365 clone->type = cur->type;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009366 clone->doc = destDoc;
9367
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009368 /*
9369 * Clone the name of the node if any.
9370 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009371 if (cur->name == xmlStringText)
9372 clone->name = xmlStringText;
9373 else if (cur->name == xmlStringTextNoenc)
9374 /*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009375 * NOTE: Although xmlStringTextNoenc is never assigned to a node
9376 * in tree.c, it might be set in Libxslt via
Daniel Veillardaa6de472008-08-25 14:53:31 +00009377 * "xsl:disable-output-escaping".
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009378 */
9379 clone->name = xmlStringTextNoenc;
9380 else if (cur->name == xmlStringComment)
9381 clone->name = xmlStringComment;
9382 else if (cur->name != NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009383 DICT_CONST_COPY(cur->name, clone->name);
Daniel Veillardaa6de472008-08-25 14:53:31 +00009384 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009385
9386 switch (cur->type) {
9387 case XML_XINCLUDE_START:
9388 case XML_XINCLUDE_END:
9389 /*
9390 * TODO
9391 */
9392 return (-1);
9393 case XML_ELEMENT_NODE:
9394 curElem = cur;
9395 depth++;
9396 /*
9397 * Namespace declarations.
9398 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009399 if (cur->nsDef != NULL) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009400 if (! parnsdone) {
9401 if (destParent && (ctxt == NULL)) {
9402 /*
9403 * Gather @parent's in-scope ns-decls.
9404 */
9405 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
9406 destParent) == -1)
Daniel Veillardaa6de472008-08-25 14:53:31 +00009407 goto internal_error;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009408 }
9409 parnsdone = 1;
9410 }
9411 /*
9412 * Clone namespace declarations.
9413 */
9414 cloneNsDefSlot = &(clone->nsDef);
9415 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
9416 /*
9417 * Create a new xmlNs.
9418 */
9419 cloneNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
9420 if (cloneNs == NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009421 xmlTreeErrMemory("xmlDOMWrapCloneNode(): "
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009422 "allocating namespace");
9423 return(-1);
9424 }
9425 memset(cloneNs, 0, sizeof(xmlNs));
9426 cloneNs->type = XML_LOCAL_NAMESPACE;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009427
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009428 if (ns->href != NULL)
9429 cloneNs->href = xmlStrdup(ns->href);
9430 if (ns->prefix != NULL)
9431 cloneNs->prefix = xmlStrdup(ns->prefix);
9432
9433 *cloneNsDefSlot = cloneNs;
9434 cloneNsDefSlot = &(cloneNs->next);
9435
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009436 /*
9437 * Note that for custom handling of ns-references,
9438 * the ns-decls need not be stored in the ns-map,
9439 * since they won't be referenced by node->ns.
9440 */
9441 if ((ctxt == NULL) ||
9442 (ctxt->getNsForNodeFunc == NULL))
9443 {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009444 /*
9445 * Does it shadow any ns-decl?
9446 */
9447 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00009448 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009449 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
9450 (mi->shadowDepth == -1) &&
9451 ((ns->prefix == mi->newNs->prefix) ||
9452 xmlStrEqual(ns->prefix,
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009453 mi->newNs->prefix))) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009454 /*
9455 * Mark as shadowed at the current
9456 * depth.
9457 */
9458 mi->shadowDepth = depth;
9459 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009460 }
9461 }
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009462 /*
9463 * Push mapping.
9464 */
9465 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
9466 ns, cloneNs, depth) == NULL)
9467 goto internal_error;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009468 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009469 }
9470 }
9471 /* cur->ns will be processed further down. */
9472 break;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009473 case XML_ATTRIBUTE_NODE:
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009474 /* IDs will be processed further down. */
9475 /* cur->ns will be processed further down. */
9476 break;
9477 case XML_TEXT_NODE:
9478 case XML_CDATA_SECTION_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009479 /*
9480 * Note that this will also cover the values of attributes.
9481 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009482 DICT_COPY(cur->content, clone->content);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009483 goto leave_node;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009484 case XML_ENTITY_NODE:
9485 /* TODO: What to do here? */
9486 goto leave_node;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009487 case XML_ENTITY_REF_NODE:
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009488 if (sourceDoc != destDoc) {
9489 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9490 xmlEntityPtr ent;
9491 /*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009492 * Different doc: Assign new entity-node if available.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009493 */
9494 ent = xmlGetDocEntity(destDoc, cur->name);
9495 if (ent != NULL) {
9496 clone->content = ent->content;
9497 clone->children = (xmlNodePtr) ent;
9498 clone->last = (xmlNodePtr) ent;
9499 }
9500 }
9501 } else {
9502 /*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009503 * Same doc: Use the current node's entity declaration
9504 * and value.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009505 */
9506 clone->content = cur->content;
9507 clone->children = cur->children;
9508 clone->last = cur->last;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009509 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009510 goto leave_node;
9511 case XML_PI_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009512 DICT_COPY(cur->content, clone->content);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009513 goto leave_node;
9514 case XML_COMMENT_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009515 DICT_COPY(cur->content, clone->content);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009516 goto leave_node;
9517 default:
9518 goto internal_error;
9519 }
9520
9521 if (cur->ns == NULL)
9522 goto end_ns_reference;
9523
9524/* handle_ns_reference: */
9525 /*
9526 ** The following will take care of references to ns-decls ********
Daniel Veillardaa6de472008-08-25 14:53:31 +00009527 ** and is intended only for element- and attribute-nodes.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009528 **
9529 */
9530 if (! parnsdone) {
9531 if (destParent && (ctxt == NULL)) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009532 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, destParent) == -1)
Daniel Veillardaa6de472008-08-25 14:53:31 +00009533 goto internal_error;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009534 }
9535 parnsdone = 1;
9536 }
9537 /*
9538 * Adopt ns-references.
9539 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009540 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009541 /*
9542 * Search for a mapping.
9543 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009544 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009545 if ((mi->shadowDepth == -1) &&
9546 (cur->ns == mi->oldNs)) {
9547 /*
9548 * This is the nice case: a mapping was found.
9549 */
9550 clone->ns = mi->newNs;
9551 goto end_ns_reference;
9552 }
9553 }
9554 }
9555 /*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009556 * No matching namespace in scope. We need a new one.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009557 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009558 if ((ctxt != NULL) && (ctxt->getNsForNodeFunc != NULL)) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009559 /*
9560 * User-defined behaviour.
9561 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009562 ns = ctxt->getNsForNodeFunc(ctxt, cur,
9563 cur->ns->href, cur->ns->prefix);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009564 /*
9565 * Add user's mapping.
9566 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009567 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009568 cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
9569 goto internal_error;
9570 clone->ns = ns;
9571 } else {
9572 /*
9573 * Aquire a normalized ns-decl and add it to the map.
9574 */
9575 if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc,
Daniel Veillardaa6de472008-08-25 14:53:31 +00009576 /* ns-decls on curElem or on destDoc->oldNs */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009577 destParent ? curElem : NULL,
9578 cur->ns, &ns,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009579 &nsMap, depth,
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009580 /* if we need to search only in the ancestor-axis */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009581 ancestorsOnly,
9582 /* ns-decls must be prefixed for attributes. */
9583 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
9584 goto internal_error;
9585 clone->ns = ns;
9586 }
9587
9588end_ns_reference:
9589
9590 /*
9591 * Some post-processing.
9592 *
9593 * Handle ID attributes.
9594 */
9595 if ((clone->type == XML_ATTRIBUTE_NODE) &&
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009596 (clone->parent != NULL))
9597 {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009598 if (xmlIsID(destDoc, clone->parent, (xmlAttrPtr) clone)) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00009599
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009600 xmlChar *idVal;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009601
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009602 idVal = xmlNodeListGetString(cur->doc, cur->children, 1);
9603 if (idVal != NULL) {
9604 if (xmlAddID(NULL, destDoc, idVal, (xmlAttrPtr) cur) == NULL) {
9605 /* TODO: error message. */
9606 xmlFree(idVal);
9607 goto internal_error;
9608 }
9609 xmlFree(idVal);
9610 }
9611 }
9612 }
9613 /*
9614 **
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009615 ** The following will traverse the tree **************************
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009616 **
Daniel Veillardaa6de472008-08-25 14:53:31 +00009617 *
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009618 * Walk the element's attributes before descending into child-nodes.
9619 */
9620 if ((cur->type == XML_ELEMENT_NODE) && (cur->properties != NULL)) {
9621 prevClone = NULL;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009622 parentClone = clone;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009623 cur = (xmlNodePtr) cur->properties;
9624 continue;
9625 }
9626into_content:
9627 /*
9628 * Descend into child-nodes.
9629 */
9630 if (cur->children != NULL) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009631 if (deep || (cur->type == XML_ATTRIBUTE_NODE)) {
9632 prevClone = NULL;
9633 parentClone = clone;
9634 cur = cur->children;
9635 continue;
9636 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009637 }
9638
9639leave_node:
9640 /*
9641 * At this point we are done with the node, its content
9642 * and an element-nodes's attribute-nodes.
9643 */
9644 if (cur == node)
9645 break;
9646 if ((cur->type == XML_ELEMENT_NODE) ||
9647 (cur->type == XML_XINCLUDE_START) ||
9648 (cur->type == XML_XINCLUDE_END)) {
9649 /*
9650 * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
9651 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009652 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009653 /*
9654 * Pop mappings.
9655 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009656 while ((nsMap->last != NULL) &&
9657 (nsMap->last->depth >= depth))
9658 {
Daniel Veillardaa6de472008-08-25 14:53:31 +00009659 XML_NSMAP_POP(nsMap, mi)
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009660 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009661 /*
9662 * Unshadow.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009663 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009664 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009665 if (mi->shadowDepth >= depth)
9666 mi->shadowDepth = -1;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009667 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009668 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009669 depth--;
9670 }
9671 if (cur->next != NULL) {
9672 prevClone = clone;
9673 cur = cur->next;
9674 } else if (cur->type != XML_ATTRIBUTE_NODE) {
9675 /*
9676 * Set clone->last.
9677 */
Daniel Veillard11ce4002006-03-10 00:36:23 +00009678 if (clone->parent != NULL)
9679 clone->parent->last = clone;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009680 clone = clone->parent;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009681 parentClone = clone->parent;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009682 /*
9683 * Process parent --> next;
9684 */
9685 cur = cur->parent;
9686 goto leave_node;
9687 } else {
9688 /* This is for attributes only. */
9689 clone = clone->parent;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009690 parentClone = clone->parent;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009691 /*
9692 * Process parent-element --> children.
9693 */
9694 cur = cur->parent;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009695 goto into_content;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009696 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009697 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009698 goto exit;
9699
9700internal_error:
9701 ret = -1;
9702
9703exit:
9704 /*
9705 * Cleanup.
9706 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009707 if (nsMap != NULL) {
9708 if ((ctxt) && (ctxt->namespaceMap == nsMap)) {
9709 /*
9710 * Just cleanup the map but don't free.
9711 */
9712 if (nsMap->first) {
9713 if (nsMap->pool)
9714 nsMap->last->next = nsMap->pool;
9715 nsMap->pool = nsMap->first;
9716 nsMap->first = NULL;
9717 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009718 } else
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009719 xmlDOMWrapNsMapFree(nsMap);
9720 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009721 /*
9722 * TODO: Should we try a cleanup of the cloned node in case of a
9723 * fatal error?
9724 */
9725 *resNode = resultClone;
9726 return (ret);
9727}
9728
9729/*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009730* xmlDOMWrapAdoptAttr:
9731* @ctxt: the optional context for custom processing
9732* @sourceDoc: the optional source document of attr
9733* @attr: the attribute-node to be adopted
9734* @destDoc: the destination doc for adoption
9735* @destParent: the optional new parent of @attr in @destDoc
9736* @options: option flags
9737*
9738* @attr is adopted by @destDoc.
9739* Ensures that ns-references point to @destDoc: either to
9740* elements->nsDef entries if @destParent is given, or to
9741* @destDoc->oldNs otherwise.
9742*
9743* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
9744*/
9745static int
9746xmlDOMWrapAdoptAttr(xmlDOMWrapCtxtPtr ctxt,
9747 xmlDocPtr sourceDoc,
9748 xmlAttrPtr attr,
9749 xmlDocPtr destDoc,
9750 xmlNodePtr destParent,
9751 int options ATTRIBUTE_UNUSED)
9752{
9753 xmlNodePtr cur;
9754 int adoptStr = 1;
9755
9756 if ((attr == NULL) || (destDoc == NULL))
9757 return (-1);
Daniel Veillardaa6de472008-08-25 14:53:31 +00009758
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009759 attr->doc = destDoc;
9760 if (attr->ns != NULL) {
9761 xmlNsPtr ns = NULL;
9762
9763 if (ctxt != NULL) {
9764 /* TODO: User defined. */
9765 }
9766 /* XML Namespace. */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009767 if (IS_STR_XML(attr->ns->prefix)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009768 ns = xmlTreeEnsureXMLDecl(destDoc);
9769 } else if (destParent == NULL) {
9770 /*
9771 * Store in @destDoc->oldNs.
9772 */
9773 ns = xmlDOMWrapStoreNs(destDoc, attr->ns->href, attr->ns->prefix);
9774 } else {
9775 /*
9776 * Declare on @destParent.
9777 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009778 if (xmlSearchNsByNamespaceStrict(destDoc, destParent, attr->ns->href,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009779 &ns, 1) == -1)
9780 goto internal_error;
9781 if (ns == NULL) {
9782 ns = xmlDOMWrapNSNormDeclareNsForced(destDoc, destParent,
9783 attr->ns->href, attr->ns->prefix, 1);
9784 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009785 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009786 if (ns == NULL)
9787 goto internal_error;
9788 attr->ns = ns;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009789 }
9790
9791 XML_TREE_ADOPT_STR(attr->name);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009792 attr->atype = 0;
9793 attr->psvi = NULL;
9794 /*
9795 * Walk content.
9796 */
9797 if (attr->children == NULL)
9798 return (0);
9799 cur = attr->children;
9800 while (cur != NULL) {
9801 cur->doc = destDoc;
9802 switch (cur->type) {
9803 case XML_TEXT_NODE:
9804 case XML_CDATA_SECTION_NODE:
9805 XML_TREE_ADOPT_STR_2(cur->content)
Daniel Veillardaa6de472008-08-25 14:53:31 +00009806 break;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009807 case XML_ENTITY_REF_NODE:
9808 /*
9809 * Remove reference to the entitity-node.
9810 */
9811 cur->content = NULL;
9812 cur->children = NULL;
9813 cur->last = NULL;
9814 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9815 xmlEntityPtr ent;
9816 /*
9817 * Assign new entity-node if available.
9818 */
9819 ent = xmlGetDocEntity(destDoc, cur->name);
9820 if (ent != NULL) {
9821 cur->content = ent->content;
9822 cur->children = (xmlNodePtr) ent;
9823 cur->last = (xmlNodePtr) ent;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009824 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009825 }
9826 break;
9827 default:
9828 break;
9829 }
9830 if (cur->children != NULL) {
9831 cur = cur->children;
9832 continue;
9833 }
9834next_sibling:
9835 if (cur == (xmlNodePtr) attr)
9836 break;
9837 if (cur->next != NULL)
9838 cur = cur->next;
9839 else {
9840 cur = cur->parent;
9841 goto next_sibling;
9842 }
9843 }
9844 return (0);
9845internal_error:
9846 return (-1);
9847}
9848
9849/*
9850* xmlDOMWrapAdoptNode:
9851* @ctxt: the optional context for custom processing
9852* @sourceDoc: the optional sourceDoc
9853* @node: the node to start with
9854* @destDoc: the destination doc
Kasimier T. Buchcik4d9c9482005-06-27 15:04:46 +00009855* @destParent: the optional new parent of @node in @destDoc
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009856* @options: option flags
9857*
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009858* References of out-of scope ns-decls are remapped to point to @destDoc:
9859* 1) If @destParent is given, then nsDef entries on element-nodes are used
9860* 2) If *no* @destParent is given, then @destDoc->oldNs entries are used
9861* This is the case when you have an unliked node and just want to move it
Daniel Veillardaa6de472008-08-25 14:53:31 +00009862* to the context of
9863*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009864* If @destParent is given, it ensures that the tree is namespace
9865* wellformed by creating additional ns-decls where needed.
9866* Note that, since prefixes of already existent ns-decls can be
9867* shadowed by this process, it could break QNames in attribute
9868* values or element content.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009869* NOTE: This function was not intensively tested.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009870*
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009871* Returns 0 if the operation succeeded,
9872* 1 if a node of unsupported type was given,
9873* 2 if a node of not yet supported type was given and
9874* -1 on API/internal errors.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009875*/
9876int
9877xmlDOMWrapAdoptNode(xmlDOMWrapCtxtPtr ctxt,
9878 xmlDocPtr sourceDoc,
9879 xmlNodePtr node,
Daniel Veillardaa6de472008-08-25 14:53:31 +00009880 xmlDocPtr destDoc,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009881 xmlNodePtr destParent,
9882 int options)
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009883{
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009884 if ((node == NULL) || (destDoc == NULL) ||
9885 ((destParent != NULL) && (destParent->doc != destDoc)))
9886 return(-1);
9887 /*
9888 * Check node->doc sanity.
Daniel Veillardaa6de472008-08-25 14:53:31 +00009889 */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009890 if ((node->doc != NULL) && (sourceDoc != NULL) &&
9891 (node->doc != sourceDoc)) {
9892 /*
9893 * Might be an XIncluded node.
9894 */
9895 return (-1);
9896 }
9897 if (sourceDoc == NULL)
9898 sourceDoc = node->doc;
9899 if (sourceDoc == destDoc)
9900 return (-1);
9901 switch (node->type) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00009902 case XML_ELEMENT_NODE:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009903 case XML_ATTRIBUTE_NODE:
9904 case XML_TEXT_NODE:
9905 case XML_CDATA_SECTION_NODE:
9906 case XML_ENTITY_REF_NODE:
9907 case XML_PI_NODE:
9908 case XML_COMMENT_NODE:
9909 break;
9910 case XML_DOCUMENT_FRAG_NODE:
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009911 /* TODO: Support document-fragment-nodes. */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009912 return (2);
9913 default:
9914 return (1);
9915 }
9916 /*
9917 * Unlink only if @node was not already added to @destParent.
9918 */
9919 if ((node->parent != NULL) && (destParent != node->parent))
9920 xmlUnlinkNode(node);
9921
9922 if (node->type == XML_ELEMENT_NODE) {
9923 return (xmlDOMWrapAdoptBranch(ctxt, sourceDoc, node,
9924 destDoc, destParent, options));
9925 } else if (node->type == XML_ATTRIBUTE_NODE) {
9926 return (xmlDOMWrapAdoptAttr(ctxt, sourceDoc,
9927 (xmlAttrPtr) node, destDoc, destParent, options));
Daniel Veillardaa6de472008-08-25 14:53:31 +00009928 } else {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009929 xmlNodePtr cur = node;
9930 int adoptStr = 1;
9931
9932 cur->doc = destDoc;
9933 /*
9934 * Optimize string adoption.
9935 */
9936 if ((sourceDoc != NULL) &&
9937 (sourceDoc->dict == destDoc->dict))
9938 adoptStr = 0;
9939 switch (node->type) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00009940 case XML_TEXT_NODE:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009941 case XML_CDATA_SECTION_NODE:
9942 XML_TREE_ADOPT_STR_2(node->content)
9943 break;
9944 case XML_ENTITY_REF_NODE:
9945 /*
9946 * Remove reference to the entitity-node.
9947 */
9948 node->content = NULL;
9949 node->children = NULL;
9950 node->last = NULL;
9951 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9952 xmlEntityPtr ent;
9953 /*
9954 * Assign new entity-node if available.
9955 */
9956 ent = xmlGetDocEntity(destDoc, node->name);
9957 if (ent != NULL) {
9958 node->content = ent->content;
9959 node->children = (xmlNodePtr) ent;
9960 node->last = (xmlNodePtr) ent;
9961 }
9962 }
9963 XML_TREE_ADOPT_STR(node->name)
9964 break;
Daniel Veillard7e21fd12005-07-03 21:44:07 +00009965 case XML_PI_NODE: {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009966 XML_TREE_ADOPT_STR(node->name)
9967 XML_TREE_ADOPT_STR_2(node->content)
9968 break;
Daniel Veillard7e21fd12005-07-03 21:44:07 +00009969 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009970 default:
9971 break;
9972 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009973 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009974 return (0);
9975}
9976
Daniel Veillard5d4644e2005-04-01 13:11:58 +00009977#define bottom_tree
9978#include "elfgcchack.h"