blob: 96a27d831fd316ea4897ff801fbd0831908374f1 [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 Veillard56a4cb82001-03-24 17:00:36 +000052xmlNsPtr xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns);
53
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +000054static xmlChar* xmlGetPropNodeValueInternal(xmlAttrPtr prop);
55
Daniel Veillard56a4cb82001-03-24 17:00:36 +000056/************************************************************************
57 * *
Daniel Veillardaa6de472008-08-25 14:53:31 +000058 * Tree memory error handler *
Daniel Veillard18ec16e2003-10-07 23:16:40 +000059 * *
60 ************************************************************************/
61/**
62 * xmlTreeErrMemory:
63 * @extra: extra informations
64 *
65 * Handle an out of memory condition
66 */
67static void
68xmlTreeErrMemory(const char *extra)
69{
70 __xmlSimpleError(XML_FROM_TREE, XML_ERR_NO_MEMORY, NULL, NULL, extra);
71}
72
73/**
74 * xmlTreeErr:
75 * @code: the error number
76 * @extra: extra informations
77 *
78 * Handle an out of memory condition
79 */
80static void
81xmlTreeErr(int code, xmlNodePtr node, const char *extra)
82{
83 const char *msg = NULL;
84
85 switch(code) {
86 case XML_TREE_INVALID_HEX:
Daniel Veillardac996a12004-07-30 12:02:58 +000087 msg = "invalid hexadecimal character value\n";
Daniel Veillard18ec16e2003-10-07 23:16:40 +000088 break;
89 case XML_TREE_INVALID_DEC:
Daniel Veillardac996a12004-07-30 12:02:58 +000090 msg = "invalid decimal character value\n";
Daniel Veillard18ec16e2003-10-07 23:16:40 +000091 break;
92 case XML_TREE_UNTERMINATED_ENTITY:
Daniel Veillardac996a12004-07-30 12:02:58 +000093 msg = "unterminated entity reference %15s\n";
Daniel Veillard18ec16e2003-10-07 23:16:40 +000094 break;
Daniel Veillard6f8611f2008-02-15 08:33:21 +000095 case XML_TREE_NOT_UTF8:
96 msg = "string is not in UTF-8\n";
97 break;
Daniel Veillard18ec16e2003-10-07 23:16:40 +000098 default:
Daniel Veillardac996a12004-07-30 12:02:58 +000099 msg = "unexpected error number\n";
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000100 }
101 __xmlSimpleError(XML_FROM_TREE, code, node, msg, extra);
102}
103
104/************************************************************************
105 * *
Daniel Veillardaa6de472008-08-25 14:53:31 +0000106 * A few static variables and macros *
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000107 * *
108 ************************************************************************/
Daniel Veillardd0463562001-10-13 09:15:48 +0000109/* #undef xmlStringText */
Daniel Veillard22090732001-07-16 00:06:07 +0000110const xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 };
Daniel Veillardd0463562001-10-13 09:15:48 +0000111/* #undef xmlStringTextNoenc */
Daniel Veillard22090732001-07-16 00:06:07 +0000112const xmlChar xmlStringTextNoenc[] =
Owen Taylor3473f882001-02-23 17:55:21 +0000113 { 't', 'e', 'x', 't', 'n', 'o', 'e', 'n', 'c', 0 };
Daniel Veillardd0463562001-10-13 09:15:48 +0000114/* #undef xmlStringComment */
Daniel Veillard22090732001-07-16 00:06:07 +0000115const xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 };
116
Owen Taylor3473f882001-02-23 17:55:21 +0000117static int xmlCompressMode = 0;
118static int xmlCheckDTD = 1;
Owen Taylor3473f882001-02-23 17:55:21 +0000119
Owen Taylor3473f882001-02-23 17:55:21 +0000120#define UPDATE_LAST_CHILD_AND_PARENT(n) if ((n) != NULL) { \
121 xmlNodePtr ulccur = (n)->children; \
122 if (ulccur == NULL) { \
123 (n)->last = NULL; \
124 } else { \
125 while (ulccur->next != NULL) { \
Daniel Veillardaa6de472008-08-25 14:53:31 +0000126 ulccur->parent = (n); \
Owen Taylor3473f882001-02-23 17:55:21 +0000127 ulccur = ulccur->next; \
128 } \
129 ulccur->parent = (n); \
130 (n)->last = ulccur; \
131}}
132
Kasimier T. Buchcik44353412006-03-06 13:26:16 +0000133#define IS_STR_XML(str) ((str != NULL) && (str[0] == 'x') && \
134 (str[1] == 'm') && (str[2] == 'l') && (str[3] == 0))
135
Owen Taylor3473f882001-02-23 17:55:21 +0000136/* #define DEBUG_BUFFER */
137/* #define DEBUG_TREE */
138
139/************************************************************************
140 * *
Daniel Veillardaa6de472008-08-25 14:53:31 +0000141 * Functions to move to entities.c once the *
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000142 * API freeze is smoothen and they can be made public. *
143 * *
144 ************************************************************************/
145#include <libxml/hash.h>
Daniel Veillardaa6de472008-08-25 14:53:31 +0000146
Daniel Veillard652327a2003-09-29 18:02:38 +0000147#ifdef LIBXML_TREE_ENABLED
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000148/**
149 * xmlGetEntityFromDtd:
150 * @dtd: A pointer to the DTD to search
151 * @name: The entity name
152 *
153 * Do an entity lookup in the DTD entity hash table and
154 * return the corresponding entity, if found.
Daniel Veillardaa6de472008-08-25 14:53:31 +0000155 *
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000156 * Returns A pointer to the entity structure or NULL if not found.
157 */
158static xmlEntityPtr
159xmlGetEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) {
160 xmlEntitiesTablePtr table;
Daniel Veillardaa6de472008-08-25 14:53:31 +0000161
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000162 if((dtd != NULL) && (dtd->entities != NULL)) {
163 table = (xmlEntitiesTablePtr) dtd->entities;
164 return((xmlEntityPtr) xmlHashLookup(table, name));
Daniel Veillardaa6de472008-08-25 14:53:31 +0000165 /* return(xmlGetEntityFromTable(table, name)); */
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000166 }
167 return(NULL);
168}
169/**
170 * xmlGetParameterEntityFromDtd:
171 * @dtd: A pointer to the DTD to search
172 * @name: The entity name
Daniel Veillardaa6de472008-08-25 14:53:31 +0000173 *
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000174 * Do an entity lookup in the DTD pararmeter entity hash table and
175 * return the corresponding entity, if found.
176 *
177 * Returns A pointer to the entity structure or NULL if not found.
178 */
179static xmlEntityPtr
180xmlGetParameterEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) {
181 xmlEntitiesTablePtr table;
Daniel Veillardaa6de472008-08-25 14:53:31 +0000182
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000183 if ((dtd != NULL) && (dtd->pentities != NULL)) {
184 table = (xmlEntitiesTablePtr) dtd->pentities;
185 return((xmlEntityPtr) xmlHashLookup(table, name));
186 /* return(xmlGetEntityFromTable(table, name)); */
187 }
188 return(NULL);
189}
Daniel Veillard652327a2003-09-29 18:02:38 +0000190#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000191
192/************************************************************************
193 * *
Daniel Veillardc00cda82003-04-07 10:22:39 +0000194 * QName handling helper *
195 * *
196 ************************************************************************/
197
198/**
199 * xmlBuildQName:
200 * @ncname: the Name
201 * @prefix: the prefix
202 * @memory: preallocated memory
203 * @len: preallocated memory length
204 *
205 * Builds the QName @prefix:@ncname in @memory if there is enough space
206 * and prefix is not NULL nor empty, otherwise allocate a new string.
207 * If prefix is NULL or empty it returns ncname.
208 *
209 * Returns the new string which must be freed by the caller if different from
210 * @memory and @ncname or NULL in case of error
211 */
212xmlChar *
213xmlBuildQName(const xmlChar *ncname, const xmlChar *prefix,
214 xmlChar *memory, int len) {
215 int lenn, lenp;
216 xmlChar *ret;
217
Daniel Veillard3b7840c2003-09-11 23:42:01 +0000218 if (ncname == NULL) return(NULL);
219 if (prefix == NULL) return((xmlChar *) ncname);
Daniel Veillardc00cda82003-04-07 10:22:39 +0000220
221 lenn = strlen((char *) ncname);
222 lenp = strlen((char *) prefix);
223
224 if ((memory == NULL) || (len < lenn + lenp + 2)) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000225 ret = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000226 if (ret == NULL) {
227 xmlTreeErrMemory("building QName");
228 return(NULL);
229 }
Daniel Veillardc00cda82003-04-07 10:22:39 +0000230 } else {
231 ret = memory;
232 }
233 memcpy(&ret[0], prefix, lenp);
234 ret[lenp] = ':';
235 memcpy(&ret[lenp + 1], ncname, lenn);
236 ret[lenn + lenp + 1] = 0;
237 return(ret);
238}
239
240/**
241 * xmlSplitQName2:
242 * @name: the full QName
Daniel Veillardaa6de472008-08-25 14:53:31 +0000243 * @prefix: a xmlChar **
Daniel Veillardc00cda82003-04-07 10:22:39 +0000244 *
245 * parse an XML qualified name string
246 *
247 * [NS 5] QName ::= (Prefix ':')? LocalPart
248 *
249 * [NS 6] Prefix ::= NCName
250 *
251 * [NS 7] LocalPart ::= NCName
252 *
253 * Returns NULL if not a QName, otherwise the local part, and prefix
254 * is updated to get the Prefix if any.
255 */
256
257xmlChar *
258xmlSplitQName2(const xmlChar *name, xmlChar **prefix) {
259 int len = 0;
260 xmlChar *ret = NULL;
261
Daniel Veillardd5cc0f72004-11-06 19:24:28 +0000262 if (prefix == NULL) return(NULL);
Daniel Veillardc00cda82003-04-07 10:22:39 +0000263 *prefix = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000264 if (name == NULL) return(NULL);
Daniel Veillardc00cda82003-04-07 10:22:39 +0000265
266#ifndef XML_XML_NAMESPACE
267 /* xml: prefix is not really a namespace */
268 if ((name[0] == 'x') && (name[1] == 'm') &&
269 (name[2] == 'l') && (name[3] == ':'))
270 return(NULL);
271#endif
272
273 /* nasty but valid */
274 if (name[0] == ':')
275 return(NULL);
276
277 /*
278 * we are not trying to validate but just to cut, and yes it will
279 * work even if this is as set of UTF-8 encoded chars
280 */
Daniel Veillardaa6de472008-08-25 14:53:31 +0000281 while ((name[len] != 0) && (name[len] != ':'))
Daniel Veillardc00cda82003-04-07 10:22:39 +0000282 len++;
Daniel Veillardaa6de472008-08-25 14:53:31 +0000283
Daniel Veillardc00cda82003-04-07 10:22:39 +0000284 if (name[len] == 0)
285 return(NULL);
286
287 *prefix = xmlStrndup(name, len);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000288 if (*prefix == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000289 xmlTreeErrMemory("QName split");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000290 return(NULL);
291 }
Daniel Veillardc00cda82003-04-07 10:22:39 +0000292 ret = xmlStrdup(&name[len + 1]);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000293 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000294 xmlTreeErrMemory("QName split");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000295 if (*prefix != NULL) {
296 xmlFree(*prefix);
297 *prefix = NULL;
298 }
299 return(NULL);
300 }
Daniel Veillardc00cda82003-04-07 10:22:39 +0000301
302 return(ret);
303}
304
Daniel Veillard8d73bcb2003-08-04 01:06:15 +0000305/**
306 * xmlSplitQName3:
307 * @name: the full QName
308 * @len: an int *
309 *
310 * parse an XML qualified name string,i
311 *
312 * returns NULL if it is not a Qualified Name, otherwise, update len
313 * with the lenght in byte of the prefix and return a pointer
Daniel Veillard54f9a4f2005-09-03 13:28:24 +0000314 * to the start of the name without the prefix
Daniel Veillard8d73bcb2003-08-04 01:06:15 +0000315 */
316
317const xmlChar *
318xmlSplitQName3(const xmlChar *name, int *len) {
319 int l = 0;
320
321 if (name == NULL) return(NULL);
322 if (len == NULL) return(NULL);
323
324 /* nasty but valid */
325 if (name[0] == ':')
326 return(NULL);
327
328 /*
329 * we are not trying to validate but just to cut, and yes it will
330 * work even if this is as set of UTF-8 encoded chars
331 */
Daniel Veillardaa6de472008-08-25 14:53:31 +0000332 while ((name[l] != 0) && (name[l] != ':'))
Daniel Veillard8d73bcb2003-08-04 01:06:15 +0000333 l++;
Daniel Veillardaa6de472008-08-25 14:53:31 +0000334
Daniel Veillard8d73bcb2003-08-04 01:06:15 +0000335 if (name[l] == 0)
336 return(NULL);
337
338 *len = l;
339
340 return(&name[l+1]);
341}
342
Daniel Veillardc00cda82003-04-07 10:22:39 +0000343/************************************************************************
344 * *
Daniel Veillardd2298792003-02-14 16:54:11 +0000345 * Check Name, NCName and QName strings *
346 * *
347 ************************************************************************/
Daniel Veillardaa6de472008-08-25 14:53:31 +0000348
Daniel Veillardd2298792003-02-14 16:54:11 +0000349#define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
350
Daniel Veillardf1a27c62006-10-13 22:33:03 +0000351#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 +0000352/**
353 * xmlValidateNCName:
354 * @value: the value to check
355 * @space: allow spaces in front and end of the string
356 *
357 * Check that a value conforms to the lexical space of NCName
358 *
359 * Returns 0 if this validates, a positive error code number otherwise
360 * and -1 in case of internal or API error.
361 */
362int
363xmlValidateNCName(const xmlChar *value, int space) {
364 const xmlChar *cur = value;
365 int c,l;
366
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000367 if (value == NULL)
368 return(-1);
369
Daniel Veillardd2298792003-02-14 16:54:11 +0000370 /*
371 * First quick algorithm for ASCII range
372 */
373 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000374 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +0000375 if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
376 (*cur == '_'))
377 cur++;
378 else
379 goto try_complex;
380 while (((*cur >= 'a') && (*cur <= 'z')) ||
381 ((*cur >= 'A') && (*cur <= 'Z')) ||
382 ((*cur >= '0') && (*cur <= '9')) ||
383 (*cur == '_') || (*cur == '-') || (*cur == '.'))
384 cur++;
385 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000386 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +0000387 if (*cur == 0)
388 return(0);
389
390try_complex:
391 /*
392 * Second check for chars outside the ASCII range
393 */
394 cur = value;
395 c = CUR_SCHAR(cur, l);
396 if (space) {
397 while (IS_BLANK(c)) {
398 cur += l;
399 c = CUR_SCHAR(cur, l);
400 }
401 }
William M. Brack871611b2003-10-18 04:53:14 +0000402 if ((!IS_LETTER(c)) && (c != '_'))
Daniel Veillardd2298792003-02-14 16:54:11 +0000403 return(1);
404 cur += l;
405 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +0000406 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
407 (c == '-') || (c == '_') || IS_COMBINING(c) ||
408 IS_EXTENDER(c)) {
Daniel Veillardd2298792003-02-14 16:54:11 +0000409 cur += l;
410 c = CUR_SCHAR(cur, l);
411 }
412 if (space) {
413 while (IS_BLANK(c)) {
414 cur += l;
415 c = CUR_SCHAR(cur, l);
416 }
417 }
418 if (c != 0)
419 return(1);
420
421 return(0);
422}
Daniel Veillard2156d432004-03-04 15:59:36 +0000423#endif
Daniel Veillardd2298792003-02-14 16:54:11 +0000424
Daniel Veillard2156d432004-03-04 15:59:36 +0000425#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Daniel Veillardd2298792003-02-14 16:54:11 +0000426/**
427 * xmlValidateQName:
428 * @value: the value to check
429 * @space: allow spaces in front and end of the string
430 *
431 * Check that a value conforms to the lexical space of QName
432 *
433 * Returns 0 if this validates, a positive error code number otherwise
434 * and -1 in case of internal or API error.
435 */
436int
437xmlValidateQName(const xmlChar *value, int space) {
438 const xmlChar *cur = value;
439 int c,l;
440
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000441 if (value == NULL)
442 return(-1);
Daniel Veillardd2298792003-02-14 16:54:11 +0000443 /*
444 * First quick algorithm for ASCII range
445 */
446 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000447 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +0000448 if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
449 (*cur == '_'))
450 cur++;
451 else
452 goto try_complex;
453 while (((*cur >= 'a') && (*cur <= 'z')) ||
454 ((*cur >= 'A') && (*cur <= 'Z')) ||
455 ((*cur >= '0') && (*cur <= '9')) ||
456 (*cur == '_') || (*cur == '-') || (*cur == '.'))
457 cur++;
458 if (*cur == ':') {
459 cur++;
460 if (((*cur >= 'a') && (*cur <= 'z')) ||
461 ((*cur >= 'A') && (*cur <= 'Z')) ||
462 (*cur == '_'))
463 cur++;
464 else
465 goto try_complex;
466 while (((*cur >= 'a') && (*cur <= 'z')) ||
467 ((*cur >= 'A') && (*cur <= 'Z')) ||
468 ((*cur >= '0') && (*cur <= '9')) ||
469 (*cur == '_') || (*cur == '-') || (*cur == '.'))
470 cur++;
471 }
472 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000473 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +0000474 if (*cur == 0)
475 return(0);
476
477try_complex:
478 /*
479 * Second check for chars outside the ASCII range
480 */
481 cur = value;
482 c = CUR_SCHAR(cur, l);
483 if (space) {
484 while (IS_BLANK(c)) {
485 cur += l;
486 c = CUR_SCHAR(cur, l);
487 }
488 }
William M. Brack871611b2003-10-18 04:53:14 +0000489 if ((!IS_LETTER(c)) && (c != '_'))
Daniel Veillardd2298792003-02-14 16:54:11 +0000490 return(1);
491 cur += l;
492 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +0000493 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
494 (c == '-') || (c == '_') || IS_COMBINING(c) ||
495 IS_EXTENDER(c)) {
Daniel Veillardd2298792003-02-14 16:54:11 +0000496 cur += l;
497 c = CUR_SCHAR(cur, l);
498 }
499 if (c == ':') {
500 cur += l;
501 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +0000502 if ((!IS_LETTER(c)) && (c != '_'))
Daniel Veillardd2298792003-02-14 16:54:11 +0000503 return(1);
504 cur += l;
505 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +0000506 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
507 (c == '-') || (c == '_') || IS_COMBINING(c) ||
508 IS_EXTENDER(c)) {
Daniel Veillardd2298792003-02-14 16:54:11 +0000509 cur += l;
510 c = CUR_SCHAR(cur, l);
511 }
512 }
513 if (space) {
514 while (IS_BLANK(c)) {
515 cur += l;
516 c = CUR_SCHAR(cur, l);
517 }
518 }
519 if (c != 0)
520 return(1);
521 return(0);
522}
523
524/**
525 * xmlValidateName:
526 * @value: the value to check
527 * @space: allow spaces in front and end of the string
528 *
529 * Check that a value conforms to the lexical space of Name
530 *
531 * Returns 0 if this validates, a positive error code number otherwise
532 * and -1 in case of internal or API error.
533 */
534int
535xmlValidateName(const xmlChar *value, int space) {
536 const xmlChar *cur = value;
537 int c,l;
538
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000539 if (value == NULL)
540 return(-1);
Daniel Veillardd2298792003-02-14 16:54:11 +0000541 /*
542 * First quick algorithm for ASCII range
543 */
544 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000545 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +0000546 if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
547 (*cur == '_') || (*cur == ':'))
548 cur++;
549 else
550 goto try_complex;
551 while (((*cur >= 'a') && (*cur <= 'z')) ||
552 ((*cur >= 'A') && (*cur <= 'Z')) ||
553 ((*cur >= '0') && (*cur <= '9')) ||
554 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
555 cur++;
556 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000557 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +0000558 if (*cur == 0)
559 return(0);
560
561try_complex:
562 /*
563 * Second check for chars outside the ASCII range
564 */
565 cur = value;
566 c = CUR_SCHAR(cur, l);
567 if (space) {
568 while (IS_BLANK(c)) {
569 cur += l;
570 c = CUR_SCHAR(cur, l);
571 }
572 }
William M. Brack871611b2003-10-18 04:53:14 +0000573 if ((!IS_LETTER(c)) && (c != '_') && (c != ':'))
Daniel Veillardd2298792003-02-14 16:54:11 +0000574 return(1);
575 cur += l;
576 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +0000577 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
578 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) {
Daniel Veillardd2298792003-02-14 16:54:11 +0000579 cur += l;
580 c = CUR_SCHAR(cur, l);
581 }
582 if (space) {
583 while (IS_BLANK(c)) {
584 cur += l;
585 c = CUR_SCHAR(cur, l);
586 }
587 }
588 if (c != 0)
589 return(1);
590 return(0);
591}
592
Daniel Veillardd4310742003-02-18 21:12:46 +0000593/**
594 * xmlValidateNMToken:
595 * @value: the value to check
596 * @space: allow spaces in front and end of the string
597 *
598 * Check that a value conforms to the lexical space of NMToken
599 *
600 * Returns 0 if this validates, a positive error code number otherwise
601 * and -1 in case of internal or API error.
602 */
603int
604xmlValidateNMToken(const xmlChar *value, int space) {
605 const xmlChar *cur = value;
606 int c,l;
607
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000608 if (value == NULL)
609 return(-1);
Daniel Veillardd4310742003-02-18 21:12:46 +0000610 /*
611 * First quick algorithm for ASCII range
612 */
613 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000614 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd4310742003-02-18 21:12:46 +0000615 if (((*cur >= 'a') && (*cur <= 'z')) ||
616 ((*cur >= 'A') && (*cur <= 'Z')) ||
617 ((*cur >= '0') && (*cur <= '9')) ||
618 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
619 cur++;
620 else
621 goto try_complex;
622 while (((*cur >= 'a') && (*cur <= 'z')) ||
623 ((*cur >= 'A') && (*cur <= 'Z')) ||
624 ((*cur >= '0') && (*cur <= '9')) ||
625 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
626 cur++;
627 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000628 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd4310742003-02-18 21:12:46 +0000629 if (*cur == 0)
630 return(0);
631
632try_complex:
633 /*
634 * Second check for chars outside the ASCII range
635 */
636 cur = value;
637 c = CUR_SCHAR(cur, l);
638 if (space) {
639 while (IS_BLANK(c)) {
640 cur += l;
641 c = CUR_SCHAR(cur, l);
642 }
643 }
William M. Brack871611b2003-10-18 04:53:14 +0000644 if (!(IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
645 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)))
Daniel Veillardd4310742003-02-18 21:12:46 +0000646 return(1);
647 cur += l;
648 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +0000649 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
650 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) {
Daniel Veillardd4310742003-02-18 21:12:46 +0000651 cur += l;
652 c = CUR_SCHAR(cur, l);
653 }
654 if (space) {
655 while (IS_BLANK(c)) {
656 cur += l;
657 c = CUR_SCHAR(cur, l);
658 }
659 }
660 if (c != 0)
661 return(1);
662 return(0);
663}
Daniel Veillard652327a2003-09-29 18:02:38 +0000664#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardd4310742003-02-18 21:12:46 +0000665
Daniel Veillardd2298792003-02-14 16:54:11 +0000666/************************************************************************
667 * *
Owen Taylor3473f882001-02-23 17:55:21 +0000668 * Allocation and deallocation of basic structures *
669 * *
670 ************************************************************************/
Daniel Veillardaa6de472008-08-25 14:53:31 +0000671
Owen Taylor3473f882001-02-23 17:55:21 +0000672/**
673 * xmlSetBufferAllocationScheme:
674 * @scheme: allocation method to use
Daniel Veillardaa6de472008-08-25 14:53:31 +0000675 *
Owen Taylor3473f882001-02-23 17:55:21 +0000676 * Set the buffer allocation method. Types are
677 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
Daniel Veillardaa6de472008-08-25 14:53:31 +0000678 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
Owen Taylor3473f882001-02-23 17:55:21 +0000679 * improves performance
680 */
681void
682xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme) {
Daniel Veillardda3fee42008-09-01 13:08:57 +0000683 if ((scheme == XML_BUFFER_ALLOC_EXACT) ||
684 (scheme == XML_BUFFER_ALLOC_DOUBLEIT))
685 xmlBufferAllocScheme = scheme;
Owen Taylor3473f882001-02-23 17:55:21 +0000686}
687
688/**
689 * xmlGetBufferAllocationScheme:
690 *
691 * Types are
692 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
Daniel Veillardaa6de472008-08-25 14:53:31 +0000693 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
Owen Taylor3473f882001-02-23 17:55:21 +0000694 * improves performance
Daniel Veillardaa6de472008-08-25 14:53:31 +0000695 *
Owen Taylor3473f882001-02-23 17:55:21 +0000696 * Returns the current allocation scheme
697 */
698xmlBufferAllocationScheme
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000699xmlGetBufferAllocationScheme(void) {
Daniel Veillarde043ee12001-04-16 14:08:07 +0000700 return(xmlBufferAllocScheme);
Owen Taylor3473f882001-02-23 17:55:21 +0000701}
702
703/**
704 * xmlNewNs:
705 * @node: the element carrying the namespace
706 * @href: the URI associated
707 * @prefix: the prefix for the namespace
708 *
709 * Creation of a new Namespace. This function will refuse to create
710 * a namespace with a similar prefix than an existing one present on this
711 * node.
712 * We use href==NULL in the case of an element creation where the namespace
713 * was not defined.
Daniel Veillardd1640922001-12-17 15:30:10 +0000714 * Returns a new namespace pointer or NULL
Owen Taylor3473f882001-02-23 17:55:21 +0000715 */
716xmlNsPtr
717xmlNewNs(xmlNodePtr node, const xmlChar *href, const xmlChar *prefix) {
718 xmlNsPtr cur;
719
720 if ((node != NULL) && (node->type != XML_ELEMENT_NODE))
721 return(NULL);
722
Daniel Veillard20ee8c02001-10-05 09:18:14 +0000723 if ((prefix != NULL) && (xmlStrEqual(prefix, BAD_CAST "xml")))
724 return(NULL);
725
Owen Taylor3473f882001-02-23 17:55:21 +0000726 /*
727 * Allocate a new Namespace and fill the fields.
728 */
729 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
730 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000731 xmlTreeErrMemory("building namespace");
Owen Taylor3473f882001-02-23 17:55:21 +0000732 return(NULL);
733 }
734 memset(cur, 0, sizeof(xmlNs));
735 cur->type = XML_LOCAL_NAMESPACE;
736
737 if (href != NULL)
Daniel Veillardaa6de472008-08-25 14:53:31 +0000738 cur->href = xmlStrdup(href);
Owen Taylor3473f882001-02-23 17:55:21 +0000739 if (prefix != NULL)
Daniel Veillardaa6de472008-08-25 14:53:31 +0000740 cur->prefix = xmlStrdup(prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000741
742 /*
743 * Add it at the end to preserve parsing order ...
744 * and checks for existing use of the prefix
745 */
746 if (node != NULL) {
747 if (node->nsDef == NULL) {
748 node->nsDef = cur;
749 } else {
750 xmlNsPtr prev = node->nsDef;
751
752 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
753 (xmlStrEqual(prev->prefix, cur->prefix))) {
754 xmlFreeNs(cur);
755 return(NULL);
Daniel Veillardaa6de472008-08-25 14:53:31 +0000756 }
Owen Taylor3473f882001-02-23 17:55:21 +0000757 while (prev->next != NULL) {
758 prev = prev->next;
759 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
760 (xmlStrEqual(prev->prefix, cur->prefix))) {
761 xmlFreeNs(cur);
762 return(NULL);
Daniel Veillardaa6de472008-08-25 14:53:31 +0000763 }
Owen Taylor3473f882001-02-23 17:55:21 +0000764 }
765 prev->next = cur;
766 }
767 }
768 return(cur);
769}
770
771/**
772 * xmlSetNs:
773 * @node: a node in the document
774 * @ns: a namespace pointer
775 *
776 * Associate a namespace to a node, a posteriori.
777 */
778void
779xmlSetNs(xmlNodePtr node, xmlNsPtr ns) {
780 if (node == NULL) {
781#ifdef DEBUG_TREE
782 xmlGenericError(xmlGenericErrorContext,
783 "xmlSetNs: node == NULL\n");
784#endif
785 return;
786 }
787 node->ns = ns;
788}
789
790/**
791 * xmlFreeNs:
792 * @cur: the namespace pointer
793 *
794 * Free up the structures associated to a namespace
795 */
796void
797xmlFreeNs(xmlNsPtr cur) {
798 if (cur == NULL) {
799#ifdef DEBUG_TREE
800 xmlGenericError(xmlGenericErrorContext,
801 "xmlFreeNs : ns == NULL\n");
802#endif
803 return;
804 }
805 if (cur->href != NULL) xmlFree((char *) cur->href);
806 if (cur->prefix != NULL) xmlFree((char *) cur->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000807 xmlFree(cur);
808}
809
810/**
811 * xmlFreeNsList:
812 * @cur: the first namespace pointer
813 *
814 * Free up all the structures associated to the chained namespaces.
815 */
816void
817xmlFreeNsList(xmlNsPtr cur) {
818 xmlNsPtr next;
819 if (cur == NULL) {
820#ifdef DEBUG_TREE
821 xmlGenericError(xmlGenericErrorContext,
822 "xmlFreeNsList : ns == NULL\n");
823#endif
824 return;
825 }
826 while (cur != NULL) {
827 next = cur->next;
828 xmlFreeNs(cur);
829 cur = next;
830 }
831}
832
833/**
834 * xmlNewDtd:
835 * @doc: the document pointer
836 * @name: the DTD name
837 * @ExternalID: the external ID
838 * @SystemID: the system ID
839 *
840 * Creation of a new DTD for the external subset. To create an
841 * internal subset, use xmlCreateIntSubset().
842 *
843 * Returns a pointer to the new DTD structure
844 */
845xmlDtdPtr
846xmlNewDtd(xmlDocPtr doc, const xmlChar *name,
847 const xmlChar *ExternalID, const xmlChar *SystemID) {
848 xmlDtdPtr cur;
849
850 if ((doc != NULL) && (doc->extSubset != NULL)) {
851#ifdef DEBUG_TREE
852 xmlGenericError(xmlGenericErrorContext,
853 "xmlNewDtd(%s): document %s already have a DTD %s\n",
854 /* !!! */ (char *) name, doc->name,
855 /* !!! */ (char *)doc->extSubset->name);
856#endif
857 return(NULL);
858 }
859
860 /*
861 * Allocate a new DTD and fill the fields.
862 */
863 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
864 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000865 xmlTreeErrMemory("building DTD");
Owen Taylor3473f882001-02-23 17:55:21 +0000866 return(NULL);
867 }
868 memset(cur, 0 , sizeof(xmlDtd));
869 cur->type = XML_DTD_NODE;
870
871 if (name != NULL)
Daniel Veillardaa6de472008-08-25 14:53:31 +0000872 cur->name = xmlStrdup(name);
Owen Taylor3473f882001-02-23 17:55:21 +0000873 if (ExternalID != NULL)
Daniel Veillardaa6de472008-08-25 14:53:31 +0000874 cur->ExternalID = xmlStrdup(ExternalID);
Owen Taylor3473f882001-02-23 17:55:21 +0000875 if (SystemID != NULL)
Daniel Veillardaa6de472008-08-25 14:53:31 +0000876 cur->SystemID = xmlStrdup(SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +0000877 if (doc != NULL)
878 doc->extSubset = cur;
879 cur->doc = doc;
880
Daniel Veillarda880b122003-04-21 21:36:41 +0000881 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +0000882 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000883 return(cur);
884}
885
886/**
887 * xmlGetIntSubset:
888 * @doc: the document pointer
889 *
890 * Get the internal subset of a document
891 * Returns a pointer to the DTD structure or NULL if not found
892 */
893
894xmlDtdPtr
895xmlGetIntSubset(xmlDocPtr doc) {
896 xmlNodePtr cur;
897
898 if (doc == NULL)
899 return(NULL);
900 cur = doc->children;
901 while (cur != NULL) {
902 if (cur->type == XML_DTD_NODE)
903 return((xmlDtdPtr) cur);
904 cur = cur->next;
905 }
906 return((xmlDtdPtr) doc->intSubset);
907}
908
909/**
910 * xmlCreateIntSubset:
911 * @doc: the document pointer
912 * @name: the DTD name
Daniel Veillarde356c282001-03-10 12:32:04 +0000913 * @ExternalID: the external (PUBLIC) ID
Owen Taylor3473f882001-02-23 17:55:21 +0000914 * @SystemID: the system ID
915 *
916 * Create the internal subset of a document
917 * Returns a pointer to the new DTD structure
918 */
919xmlDtdPtr
920xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name,
921 const xmlChar *ExternalID, const xmlChar *SystemID) {
922 xmlDtdPtr cur;
923
924 if ((doc != NULL) && (xmlGetIntSubset(doc) != NULL)) {
925#ifdef DEBUG_TREE
926 xmlGenericError(xmlGenericErrorContext,
927
928 "xmlCreateIntSubset(): document %s already have an internal subset\n",
929 doc->name);
930#endif
931 return(NULL);
932 }
933
934 /*
935 * Allocate a new DTD and fill the fields.
936 */
937 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
938 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000939 xmlTreeErrMemory("building internal subset");
Owen Taylor3473f882001-02-23 17:55:21 +0000940 return(NULL);
941 }
942 memset(cur, 0, sizeof(xmlDtd));
943 cur->type = XML_DTD_NODE;
944
William M. Bracka3215c72004-07-31 16:24:01 +0000945 if (name != NULL) {
946 cur->name = xmlStrdup(name);
947 if (cur->name == NULL) {
948 xmlTreeErrMemory("building internal subset");
949 xmlFree(cur);
950 return(NULL);
951 }
952 }
953 if (ExternalID != NULL) {
Daniel Veillardaa6de472008-08-25 14:53:31 +0000954 cur->ExternalID = xmlStrdup(ExternalID);
William M. Bracka3215c72004-07-31 16:24:01 +0000955 if (cur->ExternalID == NULL) {
956 xmlTreeErrMemory("building internal subset");
957 if (cur->name != NULL)
958 xmlFree((char *)cur->name);
959 xmlFree(cur);
960 return(NULL);
961 }
962 }
963 if (SystemID != NULL) {
Daniel Veillardaa6de472008-08-25 14:53:31 +0000964 cur->SystemID = xmlStrdup(SystemID);
William M. Bracka3215c72004-07-31 16:24:01 +0000965 if (cur->SystemID == NULL) {
966 xmlTreeErrMemory("building internal subset");
967 if (cur->name != NULL)
968 xmlFree((char *)cur->name);
969 if (cur->ExternalID != NULL)
970 xmlFree((char *)cur->ExternalID);
971 xmlFree(cur);
972 return(NULL);
973 }
974 }
Owen Taylor3473f882001-02-23 17:55:21 +0000975 if (doc != NULL) {
976 doc->intSubset = cur;
977 cur->parent = doc;
978 cur->doc = doc;
979 if (doc->children == NULL) {
980 doc->children = (xmlNodePtr) cur;
981 doc->last = (xmlNodePtr) cur;
982 } else {
Owen Taylor3473f882001-02-23 17:55:21 +0000983 if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillarde356c282001-03-10 12:32:04 +0000984 xmlNodePtr prev;
985
Owen Taylor3473f882001-02-23 17:55:21 +0000986 prev = doc->children;
987 prev->prev = (xmlNodePtr) cur;
988 cur->next = prev;
989 doc->children = (xmlNodePtr) cur;
990 } else {
Daniel Veillarde356c282001-03-10 12:32:04 +0000991 xmlNodePtr next;
992
993 next = doc->children;
994 while ((next != NULL) && (next->type != XML_ELEMENT_NODE))
995 next = next->next;
996 if (next == NULL) {
997 cur->prev = doc->last;
998 cur->prev->next = (xmlNodePtr) cur;
999 cur->next = NULL;
1000 doc->last = (xmlNodePtr) cur;
1001 } else {
1002 cur->next = next;
1003 cur->prev = next->prev;
1004 if (cur->prev == NULL)
1005 doc->children = (xmlNodePtr) cur;
1006 else
1007 cur->prev->next = (xmlNodePtr) cur;
1008 next->prev = (xmlNodePtr) cur;
1009 }
Owen Taylor3473f882001-02-23 17:55:21 +00001010 }
1011 }
1012 }
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001013
Daniel Veillarda880b122003-04-21 21:36:41 +00001014 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001015 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001016 return(cur);
1017}
1018
1019/**
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001020 * DICT_FREE:
1021 * @str: a string
1022 *
1023 * Free a string if it is not owned by the "dict" dictionnary in the
1024 * current scope
1025 */
1026#define DICT_FREE(str) \
Daniel Veillardaa6de472008-08-25 14:53:31 +00001027 if ((str) && ((!dict) || \
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001028 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
1029 xmlFree((char *)(str));
1030
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00001031
1032/**
1033 * DICT_COPY:
1034 * @str: a string
1035 *
1036 * Copy a string using a "dict" dictionnary in the current scope,
1037 * if availabe.
1038 */
1039#define DICT_COPY(str, cpy) \
1040 if (str) { \
1041 if (dict) { \
1042 if (xmlDictOwns(dict, (const xmlChar *)(str))) \
1043 cpy = (xmlChar *) (str); \
1044 else \
1045 cpy = (xmlChar *) xmlDictLookup((dict), (const xmlChar *)(str), -1); \
1046 } else \
1047 cpy = xmlStrdup((const xmlChar *)(str)); }
1048
1049/**
1050 * DICT_CONST_COPY:
1051 * @str: a string
1052 *
1053 * Copy a string using a "dict" dictionnary in the current scope,
1054 * if availabe.
1055 */
1056#define DICT_CONST_COPY(str, cpy) \
1057 if (str) { \
1058 if (dict) { \
1059 if (xmlDictOwns(dict, (const xmlChar *)(str))) \
1060 cpy = (const xmlChar *) (str); \
1061 else \
1062 cpy = xmlDictLookup((dict), (const xmlChar *)(str), -1); \
1063 } else \
1064 cpy = (const xmlChar *) xmlStrdup((const xmlChar *)(str)); }
1065
1066
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001067/**
Owen Taylor3473f882001-02-23 17:55:21 +00001068 * xmlFreeDtd:
1069 * @cur: the DTD structure to free up
1070 *
1071 * Free a DTD structure.
1072 */
1073void
1074xmlFreeDtd(xmlDtdPtr cur) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001075 xmlDictPtr dict = NULL;
1076
Owen Taylor3473f882001-02-23 17:55:21 +00001077 if (cur == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001078 return;
1079 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001080 if (cur->doc != NULL) dict = cur->doc->dict;
Daniel Veillard5335dc52003-01-01 20:59:38 +00001081
Daniel Veillarda880b122003-04-21 21:36:41 +00001082 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001083 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
1084
Owen Taylor3473f882001-02-23 17:55:21 +00001085 if (cur->children != NULL) {
1086 xmlNodePtr next, c = cur->children;
1087
1088 /*
William M. Brack18a04f22004-08-03 16:42:37 +00001089 * Cleanup all nodes which are not part of the specific lists
1090 * of notations, elements, attributes and entities.
Owen Taylor3473f882001-02-23 17:55:21 +00001091 */
1092 while (c != NULL) {
1093 next = c->next;
William M. Brack18a04f22004-08-03 16:42:37 +00001094 if ((c->type != XML_NOTATION_NODE) &&
1095 (c->type != XML_ELEMENT_DECL) &&
1096 (c->type != XML_ATTRIBUTE_DECL) &&
1097 (c->type != XML_ENTITY_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001098 xmlUnlinkNode(c);
1099 xmlFreeNode(c);
1100 }
1101 c = next;
1102 }
1103 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001104 DICT_FREE(cur->name)
1105 DICT_FREE(cur->SystemID)
1106 DICT_FREE(cur->ExternalID)
Owen Taylor3473f882001-02-23 17:55:21 +00001107 /* TODO !!! */
1108 if (cur->notations != NULL)
1109 xmlFreeNotationTable((xmlNotationTablePtr) cur->notations);
Daniel Veillardaa6de472008-08-25 14:53:31 +00001110
Owen Taylor3473f882001-02-23 17:55:21 +00001111 if (cur->elements != NULL)
1112 xmlFreeElementTable((xmlElementTablePtr) cur->elements);
1113 if (cur->attributes != NULL)
1114 xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes);
1115 if (cur->entities != NULL)
1116 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities);
1117 if (cur->pentities != NULL)
1118 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->pentities);
1119
Owen Taylor3473f882001-02-23 17:55:21 +00001120 xmlFree(cur);
1121}
1122
1123/**
1124 * xmlNewDoc:
1125 * @version: xmlChar string giving the version of XML "1.0"
1126 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001127 * Creates a new XML document
1128 *
Owen Taylor3473f882001-02-23 17:55:21 +00001129 * Returns a new document
1130 */
1131xmlDocPtr
1132xmlNewDoc(const xmlChar *version) {
1133 xmlDocPtr cur;
1134
1135 if (version == NULL)
1136 version = (const xmlChar *) "1.0";
1137
1138 /*
1139 * Allocate a new document and fill the fields.
1140 */
1141 cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
1142 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001143 xmlTreeErrMemory("building doc");
Owen Taylor3473f882001-02-23 17:55:21 +00001144 return(NULL);
1145 }
1146 memset(cur, 0, sizeof(xmlDoc));
1147 cur->type = XML_DOCUMENT_NODE;
1148
Daniel Veillardaa6de472008-08-25 14:53:31 +00001149 cur->version = xmlStrdup(version);
William M. Bracka3215c72004-07-31 16:24:01 +00001150 if (cur->version == NULL) {
1151 xmlTreeErrMemory("building doc");
1152 xmlFree(cur);
Daniel Veillardae0765b2008-07-31 19:54:59 +00001153 return(NULL);
William M. Bracka3215c72004-07-31 16:24:01 +00001154 }
Owen Taylor3473f882001-02-23 17:55:21 +00001155 cur->standalone = -1;
1156 cur->compression = -1; /* not initialized */
1157 cur->doc = cur;
Daniel Veillardae0765b2008-07-31 19:54:59 +00001158 cur->parseFlags = 0;
1159 cur->properties = XML_DOC_USERBUILT;
Daniel Veillarda6874ca2003-07-29 16:47:24 +00001160 /*
1161 * The in memory encoding is always UTF8
1162 * This field will never change and would
1163 * be obsolete if not for binary compatibility.
1164 */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00001165 cur->charset = XML_CHAR_ENCODING_UTF8;
Daniel Veillard5335dc52003-01-01 20:59:38 +00001166
Daniel Veillarda880b122003-04-21 21:36:41 +00001167 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001168 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001169 return(cur);
1170}
1171
1172/**
1173 * xmlFreeDoc:
1174 * @cur: pointer to the document
Owen Taylor3473f882001-02-23 17:55:21 +00001175 *
1176 * Free up all the structures used by a document, tree included.
1177 */
1178void
1179xmlFreeDoc(xmlDocPtr cur) {
Daniel Veillarda9142e72001-06-19 11:07:54 +00001180 xmlDtdPtr extSubset, intSubset;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001181 xmlDictPtr dict = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001182
Owen Taylor3473f882001-02-23 17:55:21 +00001183 if (cur == NULL) {
1184#ifdef DEBUG_TREE
1185 xmlGenericError(xmlGenericErrorContext,
1186 "xmlFreeDoc : document == NULL\n");
1187#endif
1188 return;
1189 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001190#ifdef LIBXML_DEBUG_RUNTIME
Daniel Veillardbca3ad22005-08-23 22:14:02 +00001191#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001192 xmlDebugCheckDocument(stderr, cur);
1193#endif
Daniel Veillardbca3ad22005-08-23 22:14:02 +00001194#endif
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001195
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001196 if (cur != NULL) dict = cur->dict;
Daniel Veillard5335dc52003-01-01 20:59:38 +00001197
Daniel Veillarda880b122003-04-21 21:36:41 +00001198 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001199 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
1200
Daniel Veillard76d66f42001-05-16 21:05:17 +00001201 /*
1202 * Do this before freeing the children list to avoid ID lookups
1203 */
1204 if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids);
1205 cur->ids = NULL;
1206 if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
1207 cur->refs = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001208 extSubset = cur->extSubset;
1209 intSubset = cur->intSubset;
Daniel Veillard5997aca2002-03-18 18:36:20 +00001210 if (intSubset == extSubset)
1211 extSubset = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001212 if (extSubset != NULL) {
Daniel Veillard76d66f42001-05-16 21:05:17 +00001213 xmlUnlinkNode((xmlNodePtr) cur->extSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001214 cur->extSubset = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001215 xmlFreeDtd(extSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001216 }
Daniel Veillarda9142e72001-06-19 11:07:54 +00001217 if (intSubset != NULL) {
Daniel Veillard76d66f42001-05-16 21:05:17 +00001218 xmlUnlinkNode((xmlNodePtr) cur->intSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001219 cur->intSubset = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001220 xmlFreeDtd(intSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001221 }
1222
1223 if (cur->children != NULL) xmlFreeNodeList(cur->children);
Owen Taylor3473f882001-02-23 17:55:21 +00001224 if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001225
1226 DICT_FREE(cur->version)
1227 DICT_FREE(cur->name)
1228 DICT_FREE(cur->encoding)
1229 DICT_FREE(cur->URL)
Owen Taylor3473f882001-02-23 17:55:21 +00001230 xmlFree(cur);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001231 if (dict) xmlDictFree(dict);
Owen Taylor3473f882001-02-23 17:55:21 +00001232}
1233
1234/**
1235 * xmlStringLenGetNodeList:
1236 * @doc: the document
1237 * @value: the value of the text
1238 * @len: the length of the string value
1239 *
1240 * Parse the value string and build the node list associated. Should
1241 * produce a flat tree with only TEXTs and ENTITY_REFs.
1242 * Returns a pointer to the first child
1243 */
1244xmlNodePtr
1245xmlStringLenGetNodeList(xmlDocPtr doc, const xmlChar *value, int len) {
1246 xmlNodePtr ret = NULL, last = NULL;
1247 xmlNodePtr node;
1248 xmlChar *val;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001249 const xmlChar *cur = value, *end = cur + len;
Owen Taylor3473f882001-02-23 17:55:21 +00001250 const xmlChar *q;
1251 xmlEntityPtr ent;
1252
1253 if (value == NULL) return(NULL);
1254
1255 q = cur;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001256 while ((cur < end) && (*cur != 0)) {
1257 if (cur[0] == '&') {
1258 int charval = 0;
1259 xmlChar tmp;
1260
Owen Taylor3473f882001-02-23 17:55:21 +00001261 /*
1262 * Save the current text.
1263 */
1264 if (cur != q) {
1265 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
1266 xmlNodeAddContentLen(last, q, cur - q);
1267 } else {
1268 node = xmlNewDocTextLen(doc, q, cur - q);
1269 if (node == NULL) return(ret);
1270 if (last == NULL)
1271 last = ret = node;
1272 else {
1273 last->next = node;
1274 node->prev = last;
1275 last = node;
1276 }
1277 }
1278 }
Owen Taylor3473f882001-02-23 17:55:21 +00001279 q = cur;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001280 if ((cur + 2 < end) && (cur[1] == '#') && (cur[2] == 'x')) {
1281 cur += 3;
1282 if (cur < end)
1283 tmp = *cur;
1284 else
1285 tmp = 0;
1286 while (tmp != ';') { /* Non input consuming loop */
Daniel Veillardaa6de472008-08-25 14:53:31 +00001287 if ((tmp >= '0') && (tmp <= '9'))
Daniel Veillard07cb8222003-09-10 10:51:05 +00001288 charval = charval * 16 + (tmp - '0');
1289 else if ((tmp >= 'a') && (tmp <= 'f'))
1290 charval = charval * 16 + (tmp - 'a') + 10;
1291 else if ((tmp >= 'A') && (tmp <= 'F'))
1292 charval = charval * 16 + (tmp - 'A') + 10;
Owen Taylor3473f882001-02-23 17:55:21 +00001293 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001294 xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
1295 NULL);
Daniel Veillard07cb8222003-09-10 10:51:05 +00001296 charval = 0;
1297 break;
1298 }
1299 cur++;
1300 if (cur < end)
1301 tmp = *cur;
1302 else
1303 tmp = 0;
1304 }
1305 if (tmp == ';')
1306 cur++;
1307 q = cur;
1308 } else if ((cur + 1 < end) && (cur[1] == '#')) {
1309 cur += 2;
1310 if (cur < end)
1311 tmp = *cur;
1312 else
1313 tmp = 0;
1314 while (tmp != ';') { /* Non input consuming loops */
Daniel Veillardaa6de472008-08-25 14:53:31 +00001315 if ((tmp >= '0') && (tmp <= '9'))
Daniel Veillard07cb8222003-09-10 10:51:05 +00001316 charval = charval * 10 + (tmp - '0');
1317 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001318 xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
1319 NULL);
Daniel Veillard07cb8222003-09-10 10:51:05 +00001320 charval = 0;
1321 break;
1322 }
1323 cur++;
1324 if (cur < end)
1325 tmp = *cur;
1326 else
1327 tmp = 0;
1328 }
1329 if (tmp == ';')
1330 cur++;
1331 q = cur;
1332 } else {
1333 /*
1334 * Read the entity string
1335 */
1336 cur++;
1337 q = cur;
1338 while ((cur < end) && (*cur != 0) && (*cur != ';')) cur++;
1339 if ((cur >= end) || (*cur == 0)) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001340 xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY, (xmlNodePtr) doc,
1341 (const char *) q);
Daniel Veillard07cb8222003-09-10 10:51:05 +00001342 return(ret);
1343 }
1344 if (cur != q) {
1345 /*
1346 * Predefined entities don't generate nodes
1347 */
1348 val = xmlStrndup(q, cur - q);
1349 ent = xmlGetDocEntity(doc, val);
1350 if ((ent != NULL) &&
1351 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
1352 if (last == NULL) {
1353 node = xmlNewDocText(doc, ent->content);
1354 last = ret = node;
1355 } else if (last->type != XML_TEXT_NODE) {
1356 node = xmlNewDocText(doc, ent->content);
1357 last = xmlAddNextSibling(last, node);
1358 } else
1359 xmlNodeAddContent(last, ent->content);
Daniel Veillardaa6de472008-08-25 14:53:31 +00001360
Daniel Veillard07cb8222003-09-10 10:51:05 +00001361 } else {
1362 /*
1363 * Create a new REFERENCE_REF node
1364 */
1365 node = xmlNewReference(doc, val);
1366 if (node == NULL) {
1367 if (val != NULL) xmlFree(val);
1368 return(ret);
1369 }
1370 else if ((ent != NULL) && (ent->children == NULL)) {
1371 xmlNodePtr temp;
1372
1373 ent->children = xmlStringGetNodeList(doc,
1374 (const xmlChar*)node->content);
1375 ent->owner = 1;
1376 temp = ent->children;
1377 while (temp) {
1378 temp->parent = (xmlNodePtr)ent;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001379 ent->last = temp;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001380 temp = temp->next;
1381 }
1382 }
1383 if (last == NULL) {
1384 last = ret = node;
1385 } else {
1386 last = xmlAddNextSibling(last, node);
1387 }
1388 }
1389 xmlFree(val);
1390 }
1391 cur++;
1392 q = cur;
1393 }
1394 if (charval != 0) {
1395 xmlChar buf[10];
1396 int l;
1397
1398 l = xmlCopyCharMultiByte(buf, charval);
1399 buf[l] = 0;
1400 node = xmlNewDocText(doc, buf);
1401 if (node != NULL) {
1402 if (last == NULL) {
1403 last = ret = node;
1404 } else {
1405 last = xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001406 }
1407 }
Daniel Veillard07cb8222003-09-10 10:51:05 +00001408 charval = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001409 }
Daniel Veillard07cb8222003-09-10 10:51:05 +00001410 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001411 cur++;
1412 }
Daniel Veillard07cb8222003-09-10 10:51:05 +00001413 if ((cur != q) || (ret == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001414 /*
1415 * Handle the last piece of text.
1416 */
1417 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
1418 xmlNodeAddContentLen(last, q, cur - q);
1419 } else {
1420 node = xmlNewDocTextLen(doc, q, cur - q);
1421 if (node == NULL) return(ret);
Daniel Veillard07cb8222003-09-10 10:51:05 +00001422 if (last == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001423 last = ret = node;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001424 } else {
1425 last = xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001426 }
1427 }
1428 }
1429 return(ret);
1430}
1431
1432/**
1433 * xmlStringGetNodeList:
1434 * @doc: the document
1435 * @value: the value of the attribute
1436 *
1437 * Parse the value string and build the node list associated. Should
1438 * produce a flat tree with only TEXTs and ENTITY_REFs.
1439 * Returns a pointer to the first child
1440 */
1441xmlNodePtr
1442xmlStringGetNodeList(xmlDocPtr doc, const xmlChar *value) {
1443 xmlNodePtr ret = NULL, last = NULL;
1444 xmlNodePtr node;
1445 xmlChar *val;
1446 const xmlChar *cur = value;
1447 const xmlChar *q;
1448 xmlEntityPtr ent;
1449
1450 if (value == NULL) return(NULL);
1451
1452 q = cur;
1453 while (*cur != 0) {
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001454 if (cur[0] == '&') {
1455 int charval = 0;
1456 xmlChar tmp;
1457
Owen Taylor3473f882001-02-23 17:55:21 +00001458 /*
1459 * Save the current text.
1460 */
1461 if (cur != q) {
1462 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
1463 xmlNodeAddContentLen(last, q, cur - q);
1464 } else {
1465 node = xmlNewDocTextLen(doc, q, cur - q);
1466 if (node == NULL) return(ret);
1467 if (last == NULL)
1468 last = ret = node;
1469 else {
1470 last->next = node;
1471 node->prev = last;
1472 last = node;
1473 }
1474 }
1475 }
Owen Taylor3473f882001-02-23 17:55:21 +00001476 q = cur;
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001477 if ((cur[1] == '#') && (cur[2] == 'x')) {
1478 cur += 3;
1479 tmp = *cur;
1480 while (tmp != ';') { /* Non input consuming loop */
Daniel Veillardaa6de472008-08-25 14:53:31 +00001481 if ((tmp >= '0') && (tmp <= '9'))
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001482 charval = charval * 16 + (tmp - '0');
1483 else if ((tmp >= 'a') && (tmp <= 'f'))
1484 charval = charval * 16 + (tmp - 'a') + 10;
1485 else if ((tmp >= 'A') && (tmp <= 'F'))
1486 charval = charval * 16 + (tmp - 'A') + 10;
Owen Taylor3473f882001-02-23 17:55:21 +00001487 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001488 xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
1489 NULL);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001490 charval = 0;
1491 break;
1492 }
1493 cur++;
1494 tmp = *cur;
1495 }
1496 if (tmp == ';')
1497 cur++;
1498 q = cur;
1499 } else if (cur[1] == '#') {
1500 cur += 2;
1501 tmp = *cur;
1502 while (tmp != ';') { /* Non input consuming loops */
Daniel Veillardaa6de472008-08-25 14:53:31 +00001503 if ((tmp >= '0') && (tmp <= '9'))
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001504 charval = charval * 10 + (tmp - '0');
1505 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001506 xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
1507 NULL);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001508 charval = 0;
1509 break;
1510 }
1511 cur++;
1512 tmp = *cur;
1513 }
1514 if (tmp == ';')
1515 cur++;
1516 q = cur;
1517 } else {
1518 /*
1519 * Read the entity string
1520 */
1521 cur++;
1522 q = cur;
1523 while ((*cur != 0) && (*cur != ';')) cur++;
1524 if (*cur == 0) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001525 xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY,
1526 (xmlNodePtr) doc, (const char *) q);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001527 return(ret);
1528 }
1529 if (cur != q) {
1530 /*
1531 * Predefined entities don't generate nodes
1532 */
1533 val = xmlStrndup(q, cur - q);
1534 ent = xmlGetDocEntity(doc, val);
1535 if ((ent != NULL) &&
1536 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
1537 if (last == NULL) {
1538 node = xmlNewDocText(doc, ent->content);
1539 last = ret = node;
Daniel Veillard6f42c132002-01-06 23:05:13 +00001540 } else if (last->type != XML_TEXT_NODE) {
1541 node = xmlNewDocText(doc, ent->content);
1542 last = xmlAddNextSibling(last, node);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001543 } else
1544 xmlNodeAddContent(last, ent->content);
Daniel Veillardaa6de472008-08-25 14:53:31 +00001545
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001546 } else {
1547 /*
1548 * Create a new REFERENCE_REF node
1549 */
1550 node = xmlNewReference(doc, val);
1551 if (node == NULL) {
1552 if (val != NULL) xmlFree(val);
1553 return(ret);
1554 }
Daniel Veillardbf8dae82002-04-18 16:39:10 +00001555 else if ((ent != NULL) && (ent->children == NULL)) {
1556 xmlNodePtr temp;
1557
1558 ent->children = xmlStringGetNodeList(doc,
1559 (const xmlChar*)node->content);
Daniel Veillard2d84a892002-12-30 00:01:08 +00001560 ent->owner = 1;
Daniel Veillardbf8dae82002-04-18 16:39:10 +00001561 temp = ent->children;
1562 while (temp) {
1563 temp->parent = (xmlNodePtr)ent;
1564 temp = temp->next;
1565 }
1566 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001567 if (last == NULL) {
1568 last = ret = node;
1569 } else {
1570 last = xmlAddNextSibling(last, node);
1571 }
1572 }
1573 xmlFree(val);
1574 }
1575 cur++;
1576 q = cur;
1577 }
1578 if (charval != 0) {
1579 xmlChar buf[10];
1580 int len;
1581
1582 len = xmlCopyCharMultiByte(buf, charval);
1583 buf[len] = 0;
1584 node = xmlNewDocText(doc, buf);
1585 if (node != NULL) {
1586 if (last == NULL) {
1587 last = ret = node;
1588 } else {
1589 last = xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001590 }
1591 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001592
1593 charval = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001594 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001595 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001596 cur++;
1597 }
Daniel Veillard75bea542001-05-11 17:41:21 +00001598 if ((cur != q) || (ret == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001599 /*
1600 * Handle the last piece of text.
1601 */
1602 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
1603 xmlNodeAddContentLen(last, q, cur - q);
1604 } else {
1605 node = xmlNewDocTextLen(doc, q, cur - q);
1606 if (node == NULL) return(ret);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001607 if (last == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001608 last = ret = node;
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001609 } else {
1610 last = xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001611 }
1612 }
1613 }
1614 return(ret);
1615}
1616
1617/**
1618 * xmlNodeListGetString:
1619 * @doc: the document
1620 * @list: a Node list
1621 * @inLine: should we replace entity contents or show their external form
1622 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001623 * Build the string equivalent to the text contained in the Node list
Owen Taylor3473f882001-02-23 17:55:21 +00001624 * made of TEXTs and ENTITY_REFs
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001625 *
Daniel Veillardbd9afb52002-09-25 22:25:35 +00001626 * Returns a pointer to the string copy, the caller must free it with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00001627 */
1628xmlChar *
Daniel Veillard7646b182002-04-20 06:41:40 +00001629xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine)
1630{
Owen Taylor3473f882001-02-23 17:55:21 +00001631 xmlNodePtr node = list;
1632 xmlChar *ret = NULL;
1633 xmlEntityPtr ent;
1634
Daniel Veillard7646b182002-04-20 06:41:40 +00001635 if (list == NULL)
1636 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001637
1638 while (node != NULL) {
1639 if ((node->type == XML_TEXT_NODE) ||
Daniel Veillard7646b182002-04-20 06:41:40 +00001640 (node->type == XML_CDATA_SECTION_NODE)) {
1641 if (inLine) {
1642 ret = xmlStrcat(ret, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001643 } else {
Daniel Veillard7646b182002-04-20 06:41:40 +00001644 xmlChar *buffer;
Owen Taylor3473f882001-02-23 17:55:21 +00001645
Daniel Veillard7646b182002-04-20 06:41:40 +00001646 buffer = xmlEncodeEntitiesReentrant(doc, node->content);
1647 if (buffer != NULL) {
1648 ret = xmlStrcat(ret, buffer);
1649 xmlFree(buffer);
1650 }
1651 }
1652 } else if (node->type == XML_ENTITY_REF_NODE) {
1653 if (inLine) {
1654 ent = xmlGetDocEntity(doc, node->name);
1655 if (ent != NULL) {
1656 xmlChar *buffer;
1657
1658 /* an entity content can be any "well balanced chunk",
1659 * i.e. the result of the content [43] production:
1660 * http://www.w3.org/TR/REC-xml#NT-content.
1661 * So it can contain text, CDATA section or nested
1662 * entity reference nodes (among others).
1663 * -> we recursive call xmlNodeListGetString()
1664 * which handles these types */
1665 buffer = xmlNodeListGetString(doc, ent->children, 1);
1666 if (buffer != NULL) {
1667 ret = xmlStrcat(ret, buffer);
1668 xmlFree(buffer);
1669 }
1670 } else {
1671 ret = xmlStrcat(ret, node->content);
1672 }
1673 } else {
1674 xmlChar buf[2];
1675
1676 buf[0] = '&';
1677 buf[1] = 0;
1678 ret = xmlStrncat(ret, buf, 1);
1679 ret = xmlStrcat(ret, node->name);
1680 buf[0] = ';';
1681 buf[1] = 0;
1682 ret = xmlStrncat(ret, buf, 1);
1683 }
1684 }
1685#if 0
1686 else {
1687 xmlGenericError(xmlGenericErrorContext,
1688 "xmlGetNodeListString : invalid node type %d\n",
1689 node->type);
1690 }
1691#endif
1692 node = node->next;
1693 }
1694 return (ret);
1695}
Daniel Veillard652327a2003-09-29 18:02:38 +00001696
1697#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001698/**
1699 * xmlNodeListGetRawString:
1700 * @doc: the document
1701 * @list: a Node list
1702 * @inLine: should we replace entity contents or show their external form
1703 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001704 * Builds the string equivalent to the text contained in the Node list
Owen Taylor3473f882001-02-23 17:55:21 +00001705 * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString()
1706 * this function doesn't do any character encoding handling.
1707 *
Daniel Veillardbd9afb52002-09-25 22:25:35 +00001708 * Returns a pointer to the string copy, the caller must free it with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00001709 */
1710xmlChar *
Daniel Veillard7646b182002-04-20 06:41:40 +00001711xmlNodeListGetRawString(xmlDocPtr doc, xmlNodePtr list, int inLine)
1712{
Owen Taylor3473f882001-02-23 17:55:21 +00001713 xmlNodePtr node = list;
1714 xmlChar *ret = NULL;
1715 xmlEntityPtr ent;
1716
Daniel Veillard7646b182002-04-20 06:41:40 +00001717 if (list == NULL)
1718 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001719
1720 while (node != NULL) {
Daniel Veillard7db37732001-07-12 01:20:08 +00001721 if ((node->type == XML_TEXT_NODE) ||
Daniel Veillard7646b182002-04-20 06:41:40 +00001722 (node->type == XML_CDATA_SECTION_NODE)) {
1723 if (inLine) {
1724 ret = xmlStrcat(ret, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001725 } else {
Daniel Veillard7646b182002-04-20 06:41:40 +00001726 xmlChar *buffer;
1727
1728 buffer = xmlEncodeSpecialChars(doc, node->content);
1729 if (buffer != NULL) {
1730 ret = xmlStrcat(ret, buffer);
1731 xmlFree(buffer);
1732 }
1733 }
1734 } else if (node->type == XML_ENTITY_REF_NODE) {
1735 if (inLine) {
1736 ent = xmlGetDocEntity(doc, node->name);
1737 if (ent != NULL) {
1738 xmlChar *buffer;
1739
1740 /* an entity content can be any "well balanced chunk",
1741 * i.e. the result of the content [43] production:
1742 * http://www.w3.org/TR/REC-xml#NT-content.
1743 * So it can contain text, CDATA section or nested
1744 * entity reference nodes (among others).
1745 * -> we recursive call xmlNodeListGetRawString()
1746 * which handles these types */
1747 buffer =
1748 xmlNodeListGetRawString(doc, ent->children, 1);
1749 if (buffer != NULL) {
1750 ret = xmlStrcat(ret, buffer);
1751 xmlFree(buffer);
1752 }
1753 } else {
1754 ret = xmlStrcat(ret, node->content);
1755 }
1756 } else {
1757 xmlChar buf[2];
1758
1759 buf[0] = '&';
1760 buf[1] = 0;
1761 ret = xmlStrncat(ret, buf, 1);
1762 ret = xmlStrcat(ret, node->name);
1763 buf[0] = ';';
1764 buf[1] = 0;
1765 ret = xmlStrncat(ret, buf, 1);
1766 }
1767 }
Owen Taylor3473f882001-02-23 17:55:21 +00001768#if 0
Daniel Veillard7646b182002-04-20 06:41:40 +00001769 else {
1770 xmlGenericError(xmlGenericErrorContext,
1771 "xmlGetNodeListString : invalid node type %d\n",
1772 node->type);
1773 }
Owen Taylor3473f882001-02-23 17:55:21 +00001774#endif
Daniel Veillard7646b182002-04-20 06:41:40 +00001775 node = node->next;
Owen Taylor3473f882001-02-23 17:55:21 +00001776 }
Daniel Veillard7646b182002-04-20 06:41:40 +00001777 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00001778}
Daniel Veillard652327a2003-09-29 18:02:38 +00001779#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001780
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001781static xmlAttrPtr
1782xmlNewPropInternal(xmlNodePtr node, xmlNsPtr ns,
1783 const xmlChar * name, const xmlChar * value,
1784 int eatname)
1785{
Owen Taylor3473f882001-02-23 17:55:21 +00001786 xmlAttrPtr cur;
1787 xmlDocPtr doc = NULL;
1788
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001789 if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) {
Daniel Veillard8f6c2b12008-04-03 11:17:21 +00001790 if ((eatname == 1) &&
1791 ((node->doc == NULL) ||
Daniel Veillarded939f82008-04-08 08:20:08 +00001792 (!(xmlDictOwns(node->doc->dict, name)))))
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001793 xmlFree((xmlChar *) name);
1794 return (NULL);
1795 }
Owen Taylor3473f882001-02-23 17:55:21 +00001796
1797 /*
1798 * Allocate a new property and fill the fields.
1799 */
1800 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1801 if (cur == NULL) {
Daniel Veillard8f6c2b12008-04-03 11:17:21 +00001802 if ((eatname == 1) &&
1803 ((node->doc == NULL) ||
Daniel Veillarded939f82008-04-08 08:20:08 +00001804 (!(xmlDictOwns(node->doc->dict, name)))))
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001805 xmlFree((xmlChar *) name);
1806 xmlTreeErrMemory("building attribute");
1807 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001808 }
1809 memset(cur, 0, sizeof(xmlAttr));
1810 cur->type = XML_ATTRIBUTE_NODE;
1811
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001812 cur->parent = node;
Owen Taylor3473f882001-02-23 17:55:21 +00001813 if (node != NULL) {
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001814 doc = node->doc;
1815 cur->doc = doc;
Owen Taylor3473f882001-02-23 17:55:21 +00001816 }
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001817 cur->ns = ns;
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001818
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001819 if (eatname == 0) {
1820 if ((doc != NULL) && (doc->dict != NULL))
1821 cur->name = (xmlChar *) xmlDictLookup(doc->dict, name, -1);
1822 else
1823 cur->name = xmlStrdup(name);
1824 } else
1825 cur->name = name;
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001826
Owen Taylor3473f882001-02-23 17:55:21 +00001827 if (value != NULL) {
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001828 xmlNodePtr tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00001829
Daniel Veillard6f8611f2008-02-15 08:33:21 +00001830 if(!xmlCheckUTF8(value)) {
1831 xmlTreeErr(XML_TREE_NOT_UTF8, (xmlNodePtr) doc,
1832 NULL);
1833 if (doc != NULL)
1834 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
1835 }
1836 cur->children = xmlNewDocText(doc, value);
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001837 cur->last = NULL;
1838 tmp = cur->children;
1839 while (tmp != NULL) {
1840 tmp->parent = (xmlNodePtr) cur;
1841 if (tmp->next == NULL)
1842 cur->last = tmp;
1843 tmp = tmp->next;
1844 }
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001845 }
Owen Taylor3473f882001-02-23 17:55:21 +00001846
1847 /*
1848 * Add it at the end to preserve parsing order ...
1849 */
1850 if (node != NULL) {
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001851 if (node->properties == NULL) {
1852 node->properties = cur;
1853 } else {
1854 xmlAttrPtr prev = node->properties;
Owen Taylor3473f882001-02-23 17:55:21 +00001855
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001856 while (prev->next != NULL)
1857 prev = prev->next;
1858 prev->next = cur;
1859 cur->prev = prev;
1860 }
Owen Taylor3473f882001-02-23 17:55:21 +00001861 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00001862
Daniel Veillardab2a7632009-07-09 08:45:03 +02001863 if ((value != NULL) &&
1864 (xmlIsID((node == NULL) ? NULL : node->doc, node, cur) == 1))
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001865 xmlAddID(NULL, node->doc, value, cur);
1866
Daniel Veillarda880b122003-04-21 21:36:41 +00001867 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001868 xmlRegisterNodeDefaultValue((xmlNodePtr) cur);
1869 return (cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001870}
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001871
1872#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
1873 defined(LIBXML_SCHEMAS_ENABLED)
1874/**
1875 * xmlNewProp:
1876 * @node: the holding node
1877 * @name: the name of the attribute
1878 * @value: the value of the attribute
1879 *
1880 * Create a new property carried by a node.
1881 * Returns a pointer to the attribute
1882 */
1883xmlAttrPtr
1884xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
1885
1886 if (name == NULL) {
1887#ifdef DEBUG_TREE
1888 xmlGenericError(xmlGenericErrorContext,
1889 "xmlNewProp : name == NULL\n");
1890#endif
1891 return(NULL);
1892 }
1893
1894 return xmlNewPropInternal(node, NULL, name, value, 0);
1895}
Daniel Veillard652327a2003-09-29 18:02:38 +00001896#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001897
1898/**
1899 * xmlNewNsProp:
1900 * @node: the holding node
1901 * @ns: the namespace
1902 * @name: the name of the attribute
1903 * @value: the value of the attribute
1904 *
1905 * Create a new property tagged with a namespace and carried by a node.
1906 * Returns a pointer to the attribute
1907 */
1908xmlAttrPtr
1909xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
1910 const xmlChar *value) {
Owen Taylor3473f882001-02-23 17:55:21 +00001911
1912 if (name == NULL) {
1913#ifdef DEBUG_TREE
1914 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00001915 "xmlNewNsProp : name == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001916#endif
1917 return(NULL);
1918 }
1919
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001920 return xmlNewPropInternal(node, ns, name, value, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001921}
1922
1923/**
Daniel Veillard46de64e2002-05-29 08:21:33 +00001924 * xmlNewNsPropEatName:
1925 * @node: the holding node
1926 * @ns: the namespace
1927 * @name: the name of the attribute
1928 * @value: the value of the attribute
1929 *
1930 * Create a new property tagged with a namespace and carried by a node.
1931 * Returns a pointer to the attribute
1932 */
1933xmlAttrPtr
1934xmlNewNsPropEatName(xmlNodePtr node, xmlNsPtr ns, xmlChar *name,
1935 const xmlChar *value) {
Daniel Veillard46de64e2002-05-29 08:21:33 +00001936
1937 if (name == NULL) {
1938#ifdef DEBUG_TREE
1939 xmlGenericError(xmlGenericErrorContext,
1940 "xmlNewNsPropEatName : name == NULL\n");
1941#endif
1942 return(NULL);
1943 }
1944
Daniel Veillard8f6c2b12008-04-03 11:17:21 +00001945 return xmlNewPropInternal(node, ns, name, value, 1);
Daniel Veillard46de64e2002-05-29 08:21:33 +00001946}
1947
1948/**
Owen Taylor3473f882001-02-23 17:55:21 +00001949 * xmlNewDocProp:
1950 * @doc: the document
1951 * @name: the name of the attribute
1952 * @value: the value of the attribute
1953 *
1954 * Create a new property carried by a document.
1955 * Returns a pointer to the attribute
1956 */
1957xmlAttrPtr
1958xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) {
1959 xmlAttrPtr cur;
1960
1961 if (name == NULL) {
1962#ifdef DEBUG_TREE
1963 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00001964 "xmlNewDocProp : name == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001965#endif
1966 return(NULL);
1967 }
1968
1969 /*
1970 * Allocate a new property and fill the fields.
1971 */
1972 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1973 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001974 xmlTreeErrMemory("building attribute");
Owen Taylor3473f882001-02-23 17:55:21 +00001975 return(NULL);
1976 }
1977 memset(cur, 0, sizeof(xmlAttr));
1978 cur->type = XML_ATTRIBUTE_NODE;
1979
Daniel Veillard03a53c32004-10-26 16:06:51 +00001980 if ((doc != NULL) && (doc->dict != NULL))
1981 cur->name = xmlDictLookup(doc->dict, name, -1);
1982 else
1983 cur->name = xmlStrdup(name);
Daniel Veillardaa6de472008-08-25 14:53:31 +00001984 cur->doc = doc;
Owen Taylor3473f882001-02-23 17:55:21 +00001985 if (value != NULL) {
1986 xmlNodePtr tmp;
1987
1988 cur->children = xmlStringGetNodeList(doc, value);
1989 cur->last = NULL;
1990
1991 tmp = cur->children;
1992 while (tmp != NULL) {
1993 tmp->parent = (xmlNodePtr) cur;
1994 if (tmp->next == NULL)
1995 cur->last = tmp;
1996 tmp = tmp->next;
1997 }
1998 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00001999
Daniel Veillarda880b122003-04-21 21:36:41 +00002000 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002001 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002002 return(cur);
2003}
2004
2005/**
2006 * xmlFreePropList:
2007 * @cur: the first property in the list
2008 *
2009 * Free a property and all its siblings, all the children are freed too.
2010 */
2011void
2012xmlFreePropList(xmlAttrPtr cur) {
2013 xmlAttrPtr next;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002014 if (cur == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +00002015 while (cur != NULL) {
2016 next = cur->next;
2017 xmlFreeProp(cur);
2018 cur = next;
2019 }
2020}
2021
2022/**
2023 * xmlFreeProp:
2024 * @cur: an attribute
2025 *
2026 * Free one attribute, all the content is freed too
2027 */
2028void
2029xmlFreeProp(xmlAttrPtr cur) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002030 xmlDictPtr dict = NULL;
2031 if (cur == NULL) return;
2032
2033 if (cur->doc != NULL) dict = cur->doc->dict;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002034
Daniel Veillarda880b122003-04-21 21:36:41 +00002035 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002036 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
2037
Owen Taylor3473f882001-02-23 17:55:21 +00002038 /* Check for ID removal -> leading to invalid references ! */
Daniel Veillardda6f4af2005-06-20 17:17:54 +00002039 if ((cur->doc != NULL) && (cur->atype == XML_ATTRIBUTE_ID)) {
2040 xmlRemoveID(cur->doc, cur);
Daniel Veillard76d66f42001-05-16 21:05:17 +00002041 }
Owen Taylor3473f882001-02-23 17:55:21 +00002042 if (cur->children != NULL) xmlFreeNodeList(cur->children);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002043 DICT_FREE(cur->name)
Owen Taylor3473f882001-02-23 17:55:21 +00002044 xmlFree(cur);
2045}
2046
2047/**
2048 * xmlRemoveProp:
2049 * @cur: an attribute
2050 *
2051 * Unlink and free one attribute, all the content is freed too
2052 * Note this doesn't work for namespace definition attributes
2053 *
2054 * Returns 0 if success and -1 in case of error.
2055 */
2056int
2057xmlRemoveProp(xmlAttrPtr cur) {
2058 xmlAttrPtr tmp;
2059 if (cur == NULL) {
2060#ifdef DEBUG_TREE
2061 xmlGenericError(xmlGenericErrorContext,
2062 "xmlRemoveProp : cur == NULL\n");
2063#endif
2064 return(-1);
2065 }
2066 if (cur->parent == NULL) {
2067#ifdef DEBUG_TREE
2068 xmlGenericError(xmlGenericErrorContext,
2069 "xmlRemoveProp : cur->parent == NULL\n");
2070#endif
2071 return(-1);
2072 }
2073 tmp = cur->parent->properties;
2074 if (tmp == cur) {
2075 cur->parent->properties = cur->next;
Rob Richards19dc9612005-10-28 16:15:16 +00002076 if (cur->next != NULL)
2077 cur->next->prev = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002078 xmlFreeProp(cur);
2079 return(0);
2080 }
2081 while (tmp != NULL) {
2082 if (tmp->next == cur) {
2083 tmp->next = cur->next;
2084 if (tmp->next != NULL)
2085 tmp->next->prev = tmp;
2086 xmlFreeProp(cur);
2087 return(0);
2088 }
2089 tmp = tmp->next;
2090 }
2091#ifdef DEBUG_TREE
2092 xmlGenericError(xmlGenericErrorContext,
2093 "xmlRemoveProp : attribute not owned by its node\n");
2094#endif
2095 return(-1);
2096}
2097
2098/**
Daniel Veillard03a53c32004-10-26 16:06:51 +00002099 * xmlNewDocPI:
2100 * @doc: the target document
Owen Taylor3473f882001-02-23 17:55:21 +00002101 * @name: the processing instruction name
2102 * @content: the PI content
2103 *
2104 * Creation of a processing instruction element.
2105 * Returns a pointer to the new node object.
2106 */
2107xmlNodePtr
Daniel Veillard03a53c32004-10-26 16:06:51 +00002108xmlNewDocPI(xmlDocPtr doc, const xmlChar *name, const xmlChar *content) {
Owen Taylor3473f882001-02-23 17:55:21 +00002109 xmlNodePtr cur;
2110
2111 if (name == NULL) {
2112#ifdef DEBUG_TREE
2113 xmlGenericError(xmlGenericErrorContext,
2114 "xmlNewPI : name == NULL\n");
2115#endif
2116 return(NULL);
2117 }
2118
2119 /*
2120 * Allocate a new node and fill the fields.
2121 */
2122 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2123 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002124 xmlTreeErrMemory("building PI");
Owen Taylor3473f882001-02-23 17:55:21 +00002125 return(NULL);
2126 }
2127 memset(cur, 0, sizeof(xmlNode));
2128 cur->type = XML_PI_NODE;
2129
Daniel Veillard03a53c32004-10-26 16:06:51 +00002130 if ((doc != NULL) && (doc->dict != NULL))
2131 cur->name = xmlDictLookup(doc->dict, name, -1);
2132 else
2133 cur->name = xmlStrdup(name);
Owen Taylor3473f882001-02-23 17:55:21 +00002134 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002135 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00002136 }
Daniel Veillarda03e3652004-11-02 18:45:30 +00002137 cur->doc = doc;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002138
Daniel Veillarda880b122003-04-21 21:36:41 +00002139 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002140 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002141 return(cur);
2142}
2143
2144/**
Daniel Veillard03a53c32004-10-26 16:06:51 +00002145 * xmlNewPI:
2146 * @name: the processing instruction name
2147 * @content: the PI content
2148 *
2149 * Creation of a processing instruction element.
2150 * Use xmlDocNewPI preferably to get string interning
2151 *
2152 * Returns a pointer to the new node object.
2153 */
2154xmlNodePtr
2155xmlNewPI(const xmlChar *name, const xmlChar *content) {
2156 return(xmlNewDocPI(NULL, name, content));
2157}
2158
2159/**
Owen Taylor3473f882001-02-23 17:55:21 +00002160 * xmlNewNode:
2161 * @ns: namespace if any
2162 * @name: the node name
2163 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002164 * Creation of a new node element. @ns is optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00002165 *
William M. Brackd7cf7f82003-11-14 07:13:16 +00002166 * Returns a pointer to the new node object. Uses xmlStrdup() to make
2167 * copy of @name.
Owen Taylor3473f882001-02-23 17:55:21 +00002168 */
2169xmlNodePtr
2170xmlNewNode(xmlNsPtr ns, const xmlChar *name) {
2171 xmlNodePtr cur;
2172
2173 if (name == NULL) {
2174#ifdef DEBUG_TREE
2175 xmlGenericError(xmlGenericErrorContext,
2176 "xmlNewNode : name == NULL\n");
2177#endif
2178 return(NULL);
2179 }
2180
2181 /*
2182 * Allocate a new node and fill the fields.
2183 */
2184 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2185 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002186 xmlTreeErrMemory("building node");
Owen Taylor3473f882001-02-23 17:55:21 +00002187 return(NULL);
2188 }
2189 memset(cur, 0, sizeof(xmlNode));
2190 cur->type = XML_ELEMENT_NODE;
Daniel Veillardaa6de472008-08-25 14:53:31 +00002191
Owen Taylor3473f882001-02-23 17:55:21 +00002192 cur->name = xmlStrdup(name);
2193 cur->ns = ns;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002194
Daniel Veillarda880b122003-04-21 21:36:41 +00002195 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002196 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002197 return(cur);
2198}
2199
2200/**
Daniel Veillard46de64e2002-05-29 08:21:33 +00002201 * xmlNewNodeEatName:
2202 * @ns: namespace if any
2203 * @name: the node name
2204 *
2205 * Creation of a new node element. @ns is optional (NULL).
2206 *
William M. Brackd7cf7f82003-11-14 07:13:16 +00002207 * Returns a pointer to the new node object, with pointer @name as
2208 * new node's name. Use xmlNewNode() if a copy of @name string is
2209 * is needed as new node's name.
Daniel Veillard46de64e2002-05-29 08:21:33 +00002210 */
2211xmlNodePtr
2212xmlNewNodeEatName(xmlNsPtr ns, xmlChar *name) {
2213 xmlNodePtr cur;
2214
2215 if (name == NULL) {
2216#ifdef DEBUG_TREE
2217 xmlGenericError(xmlGenericErrorContext,
2218 "xmlNewNode : name == NULL\n");
2219#endif
2220 return(NULL);
2221 }
2222
2223 /*
2224 * Allocate a new node and fill the fields.
2225 */
2226 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2227 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002228 xmlTreeErrMemory("building node");
Daniel Veillard8f6c2b12008-04-03 11:17:21 +00002229 /* we can't check here that name comes from the doc dictionnary */
Daniel Veillard46de64e2002-05-29 08:21:33 +00002230 return(NULL);
2231 }
2232 memset(cur, 0, sizeof(xmlNode));
2233 cur->type = XML_ELEMENT_NODE;
Daniel Veillardaa6de472008-08-25 14:53:31 +00002234
Daniel Veillard46de64e2002-05-29 08:21:33 +00002235 cur->name = name;
2236 cur->ns = ns;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002237
Daniel Veillarda880b122003-04-21 21:36:41 +00002238 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002239 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Daniel Veillard46de64e2002-05-29 08:21:33 +00002240 return(cur);
2241}
2242
2243/**
Owen Taylor3473f882001-02-23 17:55:21 +00002244 * xmlNewDocNode:
2245 * @doc: the document
2246 * @ns: namespace if any
2247 * @name: the node name
2248 * @content: the XML text content if any
2249 *
2250 * Creation of a new node element within a document. @ns and @content
Daniel Veillardd1640922001-12-17 15:30:10 +00002251 * are optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00002252 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2253 * references, but XML special chars need to be escaped first by using
2254 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2255 * need entities support.
2256 *
2257 * Returns a pointer to the new node object.
2258 */
2259xmlNodePtr
2260xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns,
2261 const xmlChar *name, const xmlChar *content) {
2262 xmlNodePtr cur;
2263
Daniel Veillard95ddcd32004-10-26 21:53:55 +00002264 if ((doc != NULL) && (doc->dict != NULL))
Daniel Veillard03a53c32004-10-26 16:06:51 +00002265 cur = xmlNewNodeEatName(ns, (xmlChar *)
2266 xmlDictLookup(doc->dict, name, -1));
2267 else
2268 cur = xmlNewNode(ns, name);
Owen Taylor3473f882001-02-23 17:55:21 +00002269 if (cur != NULL) {
2270 cur->doc = doc;
2271 if (content != NULL) {
2272 cur->children = xmlStringGetNodeList(doc, content);
2273 UPDATE_LAST_CHILD_AND_PARENT(cur)
2274 }
2275 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002276
Owen Taylor3473f882001-02-23 17:55:21 +00002277 return(cur);
2278}
2279
Daniel Veillard46de64e2002-05-29 08:21:33 +00002280/**
2281 * xmlNewDocNodeEatName:
2282 * @doc: the document
2283 * @ns: namespace if any
2284 * @name: the node name
2285 * @content: the XML text content if any
2286 *
2287 * Creation of a new node element within a document. @ns and @content
2288 * are optional (NULL).
2289 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2290 * references, but XML special chars need to be escaped first by using
2291 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2292 * need entities support.
2293 *
2294 * Returns a pointer to the new node object.
2295 */
2296xmlNodePtr
2297xmlNewDocNodeEatName(xmlDocPtr doc, xmlNsPtr ns,
2298 xmlChar *name, const xmlChar *content) {
2299 xmlNodePtr cur;
2300
2301 cur = xmlNewNodeEatName(ns, name);
2302 if (cur != NULL) {
2303 cur->doc = doc;
2304 if (content != NULL) {
2305 cur->children = xmlStringGetNodeList(doc, content);
2306 UPDATE_LAST_CHILD_AND_PARENT(cur)
2307 }
Daniel Veillard8f6c2b12008-04-03 11:17:21 +00002308 } else {
2309 /* if name don't come from the doc dictionnary free it here */
2310 if ((name != NULL) && (doc != NULL) &&
2311 (!(xmlDictOwns(doc->dict, name))))
2312 xmlFree(name);
Daniel Veillard46de64e2002-05-29 08:21:33 +00002313 }
2314 return(cur);
2315}
2316
Daniel Veillard652327a2003-09-29 18:02:38 +00002317#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002318/**
2319 * xmlNewDocRawNode:
2320 * @doc: the document
2321 * @ns: namespace if any
2322 * @name: the node name
2323 * @content: the text content if any
2324 *
2325 * Creation of a new node element within a document. @ns and @content
Daniel Veillardd1640922001-12-17 15:30:10 +00002326 * are optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00002327 *
2328 * Returns a pointer to the new node object.
2329 */
2330xmlNodePtr
2331xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns,
2332 const xmlChar *name, const xmlChar *content) {
2333 xmlNodePtr cur;
2334
Daniel Veillard95ddcd32004-10-26 21:53:55 +00002335 cur = xmlNewDocNode(doc, ns, name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002336 if (cur != NULL) {
2337 cur->doc = doc;
2338 if (content != NULL) {
2339 cur->children = xmlNewDocText(doc, content);
2340 UPDATE_LAST_CHILD_AND_PARENT(cur)
2341 }
2342 }
2343 return(cur);
2344}
2345
2346/**
2347 * xmlNewDocFragment:
2348 * @doc: the document owning the fragment
2349 *
2350 * Creation of a new Fragment node.
2351 * Returns a pointer to the new node object.
2352 */
2353xmlNodePtr
2354xmlNewDocFragment(xmlDocPtr doc) {
2355 xmlNodePtr cur;
2356
2357 /*
2358 * Allocate a new DocumentFragment node and fill the fields.
2359 */
2360 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2361 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002362 xmlTreeErrMemory("building fragment");
Owen Taylor3473f882001-02-23 17:55:21 +00002363 return(NULL);
2364 }
2365 memset(cur, 0, sizeof(xmlNode));
2366 cur->type = XML_DOCUMENT_FRAG_NODE;
2367
2368 cur->doc = doc;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002369
Daniel Veillarda880b122003-04-21 21:36:41 +00002370 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002371 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002372 return(cur);
2373}
Daniel Veillard652327a2003-09-29 18:02:38 +00002374#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002375
2376/**
2377 * xmlNewText:
2378 * @content: the text content
2379 *
2380 * Creation of a new text node.
2381 * Returns a pointer to the new node object.
2382 */
2383xmlNodePtr
2384xmlNewText(const xmlChar *content) {
2385 xmlNodePtr cur;
2386
2387 /*
2388 * Allocate a new node and fill the fields.
2389 */
2390 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2391 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002392 xmlTreeErrMemory("building text");
Owen Taylor3473f882001-02-23 17:55:21 +00002393 return(NULL);
2394 }
2395 memset(cur, 0, sizeof(xmlNode));
2396 cur->type = XML_TEXT_NODE;
2397
2398 cur->name = xmlStringText;
2399 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002400 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00002401 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002402
Daniel Veillarda880b122003-04-21 21:36:41 +00002403 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002404 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002405 return(cur);
2406}
2407
Daniel Veillard652327a2003-09-29 18:02:38 +00002408#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002409/**
2410 * xmlNewTextChild:
2411 * @parent: the parent node
2412 * @ns: a namespace if any
2413 * @name: the name of the child
2414 * @content: the text content of the child if any.
2415 *
2416 * Creation of a new child element, added at the end of @parent children list.
MST 2003 John Flecke1f70492003-12-20 23:43:28 +00002417 * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
2418 * created element inherits the namespace of @parent. If @content is non NULL,
William M. Brackd7cf7f82003-11-14 07:13:16 +00002419 * a child TEXT node will be created containing the string @content.
MST 2003 John Fleck8b03bc52003-12-17 03:45:01 +00002420 * NOTE: Use xmlNewChild() if @content will contain entities that need to be
2421 * preserved. Use this function, xmlNewTextChild(), if you need to ensure that
Daniel Veillardaa6de472008-08-25 14:53:31 +00002422 * reserved XML chars that might appear in @content, such as the ampersand,
2423 * greater-than or less-than signs, are automatically replaced by their XML
2424 * escaped entity representations.
Owen Taylor3473f882001-02-23 17:55:21 +00002425 *
2426 * Returns a pointer to the new node object.
2427 */
2428xmlNodePtr
2429xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns,
2430 const xmlChar *name, const xmlChar *content) {
2431 xmlNodePtr cur, prev;
2432
2433 if (parent == NULL) {
2434#ifdef DEBUG_TREE
2435 xmlGenericError(xmlGenericErrorContext,
2436 "xmlNewTextChild : parent == NULL\n");
2437#endif
2438 return(NULL);
2439 }
2440
2441 if (name == NULL) {
2442#ifdef DEBUG_TREE
2443 xmlGenericError(xmlGenericErrorContext,
2444 "xmlNewTextChild : name == NULL\n");
2445#endif
2446 return(NULL);
2447 }
2448
2449 /*
2450 * Allocate a new node
2451 */
Daniel Veillard254b1262003-11-01 17:04:58 +00002452 if (parent->type == XML_ELEMENT_NODE) {
2453 if (ns == NULL)
2454 cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content);
2455 else
2456 cur = xmlNewDocRawNode(parent->doc, ns, name, content);
2457 } else if ((parent->type == XML_DOCUMENT_NODE) ||
2458 (parent->type == XML_HTML_DOCUMENT_NODE)) {
2459 if (ns == NULL)
2460 cur = xmlNewDocRawNode((xmlDocPtr) parent, NULL, name, content);
2461 else
2462 cur = xmlNewDocRawNode((xmlDocPtr) parent, ns, name, content);
2463 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2464 cur = xmlNewDocRawNode( parent->doc, ns, name, content);
2465 } else {
2466 return(NULL);
2467 }
Owen Taylor3473f882001-02-23 17:55:21 +00002468 if (cur == NULL) return(NULL);
2469
2470 /*
2471 * add the new element at the end of the children list.
2472 */
2473 cur->type = XML_ELEMENT_NODE;
2474 cur->parent = parent;
2475 cur->doc = parent->doc;
2476 if (parent->children == NULL) {
2477 parent->children = cur;
2478 parent->last = cur;
2479 } else {
2480 prev = parent->last;
2481 prev->next = cur;
2482 cur->prev = prev;
2483 parent->last = cur;
2484 }
2485
2486 return(cur);
2487}
Daniel Veillard652327a2003-09-29 18:02:38 +00002488#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002489
2490/**
2491 * xmlNewCharRef:
2492 * @doc: the document
2493 * @name: the char ref string, starting with # or "&# ... ;"
2494 *
2495 * Creation of a new character reference node.
2496 * Returns a pointer to the new node object.
2497 */
2498xmlNodePtr
2499xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) {
2500 xmlNodePtr cur;
2501
Daniel Veillard36e5cd52004-11-02 14:52:23 +00002502 if (name == NULL)
2503 return(NULL);
2504
Owen Taylor3473f882001-02-23 17:55:21 +00002505 /*
2506 * Allocate a new node and fill the fields.
2507 */
2508 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2509 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002510 xmlTreeErrMemory("building character reference");
Owen Taylor3473f882001-02-23 17:55:21 +00002511 return(NULL);
2512 }
2513 memset(cur, 0, sizeof(xmlNode));
2514 cur->type = XML_ENTITY_REF_NODE;
2515
2516 cur->doc = doc;
2517 if (name[0] == '&') {
2518 int len;
2519 name++;
2520 len = xmlStrlen(name);
2521 if (name[len - 1] == ';')
2522 cur->name = xmlStrndup(name, len - 1);
2523 else
2524 cur->name = xmlStrndup(name, len);
2525 } else
2526 cur->name = xmlStrdup(name);
Daniel Veillard5335dc52003-01-01 20:59:38 +00002527
Daniel Veillarda880b122003-04-21 21:36:41 +00002528 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002529 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002530 return(cur);
2531}
2532
2533/**
2534 * xmlNewReference:
2535 * @doc: the document
2536 * @name: the reference name, or the reference string with & and ;
2537 *
2538 * Creation of a new reference node.
2539 * Returns a pointer to the new node object.
2540 */
2541xmlNodePtr
2542xmlNewReference(xmlDocPtr doc, const xmlChar *name) {
2543 xmlNodePtr cur;
2544 xmlEntityPtr ent;
2545
Daniel Veillard36e5cd52004-11-02 14:52:23 +00002546 if (name == NULL)
2547 return(NULL);
2548
Owen Taylor3473f882001-02-23 17:55:21 +00002549 /*
2550 * Allocate a new node and fill the fields.
2551 */
2552 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2553 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002554 xmlTreeErrMemory("building reference");
Owen Taylor3473f882001-02-23 17:55:21 +00002555 return(NULL);
2556 }
2557 memset(cur, 0, sizeof(xmlNode));
2558 cur->type = XML_ENTITY_REF_NODE;
2559
2560 cur->doc = doc;
2561 if (name[0] == '&') {
2562 int len;
2563 name++;
2564 len = xmlStrlen(name);
2565 if (name[len - 1] == ';')
2566 cur->name = xmlStrndup(name, len - 1);
2567 else
2568 cur->name = xmlStrndup(name, len);
2569 } else
2570 cur->name = xmlStrdup(name);
2571
2572 ent = xmlGetDocEntity(doc, cur->name);
2573 if (ent != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002574 cur->content = ent->content;
Owen Taylor3473f882001-02-23 17:55:21 +00002575 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002576 * The parent pointer in entity is a DTD pointer and thus is NOT
Owen Taylor3473f882001-02-23 17:55:21 +00002577 * updated. Not sure if this is 100% correct.
2578 * -George
2579 */
2580 cur->children = (xmlNodePtr) ent;
2581 cur->last = (xmlNodePtr) ent;
2582 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002583
Daniel Veillarda880b122003-04-21 21:36:41 +00002584 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002585 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002586 return(cur);
2587}
2588
2589/**
2590 * xmlNewDocText:
2591 * @doc: the document
2592 * @content: the text content
2593 *
2594 * Creation of a new text node within a document.
2595 * Returns a pointer to the new node object.
2596 */
2597xmlNodePtr
2598xmlNewDocText(xmlDocPtr doc, const xmlChar *content) {
2599 xmlNodePtr cur;
2600
2601 cur = xmlNewText(content);
2602 if (cur != NULL) cur->doc = doc;
2603 return(cur);
2604}
2605
2606/**
2607 * xmlNewTextLen:
2608 * @content: the text content
2609 * @len: the text len.
2610 *
Daniel Veillard60087f32001-10-10 09:45:09 +00002611 * Creation of a new text node with an extra parameter for the content's length
Owen Taylor3473f882001-02-23 17:55:21 +00002612 * Returns a pointer to the new node object.
2613 */
2614xmlNodePtr
2615xmlNewTextLen(const xmlChar *content, int len) {
2616 xmlNodePtr cur;
2617
2618 /*
2619 * Allocate a new node and fill the fields.
2620 */
2621 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2622 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002623 xmlTreeErrMemory("building text");
Owen Taylor3473f882001-02-23 17:55:21 +00002624 return(NULL);
2625 }
2626 memset(cur, 0, sizeof(xmlNode));
2627 cur->type = XML_TEXT_NODE;
2628
2629 cur->name = xmlStringText;
2630 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002631 cur->content = xmlStrndup(content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00002632 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002633
Daniel Veillarda880b122003-04-21 21:36:41 +00002634 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002635 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002636 return(cur);
2637}
2638
2639/**
2640 * xmlNewDocTextLen:
2641 * @doc: the document
2642 * @content: the text content
2643 * @len: the text len.
2644 *
Daniel Veillard60087f32001-10-10 09:45:09 +00002645 * Creation of a new text node with an extra content length parameter. The
Owen Taylor3473f882001-02-23 17:55:21 +00002646 * text node pertain to a given document.
2647 * Returns a pointer to the new node object.
2648 */
2649xmlNodePtr
2650xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) {
2651 xmlNodePtr cur;
2652
2653 cur = xmlNewTextLen(content, len);
2654 if (cur != NULL) cur->doc = doc;
2655 return(cur);
2656}
2657
2658/**
2659 * xmlNewComment:
2660 * @content: the comment content
2661 *
2662 * Creation of a new node containing a comment.
2663 * Returns a pointer to the new node object.
2664 */
2665xmlNodePtr
2666xmlNewComment(const xmlChar *content) {
2667 xmlNodePtr cur;
2668
2669 /*
2670 * Allocate a new node and fill the fields.
2671 */
2672 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2673 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002674 xmlTreeErrMemory("building comment");
Owen Taylor3473f882001-02-23 17:55:21 +00002675 return(NULL);
2676 }
2677 memset(cur, 0, sizeof(xmlNode));
2678 cur->type = XML_COMMENT_NODE;
2679
2680 cur->name = xmlStringComment;
2681 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002682 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00002683 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002684
Daniel Veillarda880b122003-04-21 21:36:41 +00002685 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002686 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002687 return(cur);
2688}
2689
2690/**
2691 * xmlNewCDataBlock:
2692 * @doc: the document
Daniel Veillardd1640922001-12-17 15:30:10 +00002693 * @content: the CDATA block content content
Owen Taylor3473f882001-02-23 17:55:21 +00002694 * @len: the length of the block
2695 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002696 * Creation of a new node containing a CDATA block.
Owen Taylor3473f882001-02-23 17:55:21 +00002697 * Returns a pointer to the new node object.
2698 */
2699xmlNodePtr
2700xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) {
2701 xmlNodePtr cur;
2702
2703 /*
2704 * Allocate a new node and fill the fields.
2705 */
2706 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2707 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002708 xmlTreeErrMemory("building CDATA");
Owen Taylor3473f882001-02-23 17:55:21 +00002709 return(NULL);
2710 }
2711 memset(cur, 0, sizeof(xmlNode));
2712 cur->type = XML_CDATA_SECTION_NODE;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002713 cur->doc = doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002714
2715 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002716 cur->content = xmlStrndup(content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00002717 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002718
Daniel Veillarda880b122003-04-21 21:36:41 +00002719 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002720 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002721 return(cur);
2722}
2723
2724/**
2725 * xmlNewDocComment:
2726 * @doc: the document
2727 * @content: the comment content
2728 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002729 * Creation of a new node containing a comment within a document.
Owen Taylor3473f882001-02-23 17:55:21 +00002730 * Returns a pointer to the new node object.
2731 */
2732xmlNodePtr
2733xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) {
2734 xmlNodePtr cur;
2735
2736 cur = xmlNewComment(content);
2737 if (cur != NULL) cur->doc = doc;
2738 return(cur);
2739}
2740
2741/**
2742 * xmlSetTreeDoc:
2743 * @tree: the top element
2744 * @doc: the document
2745 *
2746 * update all nodes under the tree to point to the right document
2747 */
2748void
2749xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) {
Daniel Veillard19e96c32001-07-09 10:32:59 +00002750 xmlAttrPtr prop;
2751
Owen Taylor3473f882001-02-23 17:55:21 +00002752 if (tree == NULL)
2753 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002754 if (tree->doc != doc) {
Daniel Veillard36065812002-01-24 15:02:46 +00002755 if(tree->type == XML_ELEMENT_NODE) {
2756 prop = tree->properties;
2757 while (prop != NULL) {
2758 prop->doc = doc;
2759 xmlSetListDoc(prop->children, doc);
2760 prop = prop->next;
2761 }
Daniel Veillard19e96c32001-07-09 10:32:59 +00002762 }
Owen Taylor3473f882001-02-23 17:55:21 +00002763 if (tree->children != NULL)
2764 xmlSetListDoc(tree->children, doc);
2765 tree->doc = doc;
2766 }
2767}
2768
2769/**
2770 * xmlSetListDoc:
Daniel Veillardd1640922001-12-17 15:30:10 +00002771 * @list: the first element
Owen Taylor3473f882001-02-23 17:55:21 +00002772 * @doc: the document
2773 *
2774 * update all nodes in the list to point to the right document
2775 */
2776void
2777xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) {
2778 xmlNodePtr cur;
2779
2780 if (list == NULL)
2781 return;
2782 cur = list;
2783 while (cur != NULL) {
2784 if (cur->doc != doc)
2785 xmlSetTreeDoc(cur, doc);
2786 cur = cur->next;
2787 }
2788}
2789
Daniel Veillard2156d432004-03-04 15:59:36 +00002790#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00002791/**
2792 * xmlNewChild:
2793 * @parent: the parent node
2794 * @ns: a namespace if any
2795 * @name: the name of the child
2796 * @content: the XML content of the child if any.
2797 *
2798 * Creation of a new child element, added at the end of @parent children list.
MST 2003 John Flecke1f70492003-12-20 23:43:28 +00002799 * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
2800 * created element inherits the namespace of @parent. If @content is non NULL,
Owen Taylor3473f882001-02-23 17:55:21 +00002801 * a child list containing the TEXTs and ENTITY_REFs node will be created.
MST 2003 John Fleck8b03bc52003-12-17 03:45:01 +00002802 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
2803 * references. XML special chars must be escaped first by using
2804 * xmlEncodeEntitiesReentrant(), or xmlNewTextChild() should be used.
Owen Taylor3473f882001-02-23 17:55:21 +00002805 *
2806 * Returns a pointer to the new node object.
2807 */
2808xmlNodePtr
2809xmlNewChild(xmlNodePtr parent, xmlNsPtr ns,
2810 const xmlChar *name, const xmlChar *content) {
2811 xmlNodePtr cur, prev;
2812
2813 if (parent == NULL) {
2814#ifdef DEBUG_TREE
2815 xmlGenericError(xmlGenericErrorContext,
2816 "xmlNewChild : parent == NULL\n");
2817#endif
2818 return(NULL);
2819 }
2820
2821 if (name == NULL) {
2822#ifdef DEBUG_TREE
2823 xmlGenericError(xmlGenericErrorContext,
2824 "xmlNewChild : name == NULL\n");
2825#endif
2826 return(NULL);
2827 }
2828
2829 /*
2830 * Allocate a new node
2831 */
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002832 if (parent->type == XML_ELEMENT_NODE) {
2833 if (ns == NULL)
2834 cur = xmlNewDocNode(parent->doc, parent->ns, name, content);
2835 else
2836 cur = xmlNewDocNode(parent->doc, ns, name, content);
2837 } else if ((parent->type == XML_DOCUMENT_NODE) ||
2838 (parent->type == XML_HTML_DOCUMENT_NODE)) {
2839 if (ns == NULL)
2840 cur = xmlNewDocNode((xmlDocPtr) parent, NULL, name, content);
2841 else
2842 cur = xmlNewDocNode((xmlDocPtr) parent, ns, name, content);
Daniel Veillard7e3f1402002-10-28 18:52:57 +00002843 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2844 cur = xmlNewDocNode( parent->doc, ns, name, content);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002845 } else {
2846 return(NULL);
2847 }
Owen Taylor3473f882001-02-23 17:55:21 +00002848 if (cur == NULL) return(NULL);
2849
2850 /*
2851 * add the new element at the end of the children list.
2852 */
2853 cur->type = XML_ELEMENT_NODE;
2854 cur->parent = parent;
2855 cur->doc = parent->doc;
2856 if (parent->children == NULL) {
2857 parent->children = cur;
2858 parent->last = cur;
2859 } else {
2860 prev = parent->last;
2861 prev->next = cur;
2862 cur->prev = prev;
2863 parent->last = cur;
2864 }
2865
2866 return(cur);
2867}
Daniel Veillard652327a2003-09-29 18:02:38 +00002868#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002869
2870/**
Rob Richards65815122006-02-25 17:13:33 +00002871 * xmlAddPropSibling:
Daniel Veillardaa6de472008-08-25 14:53:31 +00002872 * @prev: the attribute to which @prop is added after
Rob Richards65815122006-02-25 17:13:33 +00002873 * @cur: the base attribute passed to calling function
2874 * @prop: the new attribute
2875 *
2876 * Add a new attribute after @prev using @cur as base attribute.
2877 * When inserting before @cur, @prev is passed as @cur->prev.
2878 * When inserting after @cur, @prev is passed as @cur.
Daniel Veillardaa6de472008-08-25 14:53:31 +00002879 * If an existing attribute is found it is detroyed prior to adding @prop.
Rob Richards65815122006-02-25 17:13:33 +00002880 *
2881 * Returns the attribute being inserted or NULL in case of error.
2882 */
2883static xmlNodePtr
2884xmlAddPropSibling(xmlNodePtr prev, xmlNodePtr cur, xmlNodePtr prop) {
2885 xmlAttrPtr attr;
2886
2887 if (cur->type != XML_ATTRIBUTE_NODE)
2888 return(NULL);
2889
2890 /* check if an attribute with the same name exists */
2891 if (prop->ns == NULL)
2892 attr = xmlHasNsProp(cur->parent, prop->name, NULL);
2893 else
2894 attr = xmlHasNsProp(cur->parent, prop->name, prop->ns->href);
2895
2896 if (prop->doc != cur->doc) {
2897 xmlSetTreeDoc(prop, cur->doc);
2898 }
2899 prop->parent = cur->parent;
2900 prop->prev = prev;
2901 if (prev != NULL) {
2902 prop->next = prev->next;
2903 prev->next = prop;
2904 if (prop->next)
2905 prop->next->prev = prop;
2906 } else {
2907 prop->next = cur;
2908 cur->prev = prop;
2909 }
2910 if (prop->prev == NULL && prop->parent != NULL)
2911 prop->parent->properties = (xmlAttrPtr) prop;
2912 if ((attr != NULL) && (attr->type != XML_ATTRIBUTE_DECL)) {
2913 /* different instance, destroy it (attributes must be unique) */
2914 xmlRemoveProp((xmlAttrPtr) attr);
2915 }
2916 return prop;
2917}
2918
2919/**
Owen Taylor3473f882001-02-23 17:55:21 +00002920 * xmlAddNextSibling:
2921 * @cur: the child node
2922 * @elem: the new node
2923 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002924 * Add a new node @elem as the next sibling of @cur
2925 * If the new node was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00002926 * first unlinked from its existing context.
2927 * As a result of text merging @elem may be freed.
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002928 * If the new node is ATTRIBUTE, it is added into properties instead of children.
Daniel Veillardaa6de472008-08-25 14:53:31 +00002929 * If there is an attribute with equal name, it is first destroyed.
Owen Taylor3473f882001-02-23 17:55:21 +00002930 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002931 * Returns the new node or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002932 */
2933xmlNodePtr
2934xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) {
2935 if (cur == NULL) {
2936#ifdef DEBUG_TREE
2937 xmlGenericError(xmlGenericErrorContext,
2938 "xmlAddNextSibling : cur == NULL\n");
2939#endif
2940 return(NULL);
2941 }
2942 if (elem == NULL) {
2943#ifdef DEBUG_TREE
2944 xmlGenericError(xmlGenericErrorContext,
2945 "xmlAddNextSibling : elem == NULL\n");
2946#endif
2947 return(NULL);
2948 }
2949
Rob Richards19dc9612005-10-28 16:15:16 +00002950 if (cur == elem) {
2951#ifdef DEBUG_TREE
2952 xmlGenericError(xmlGenericErrorContext,
2953 "xmlAddNextSibling : cur == elem\n");
2954#endif
2955 return(NULL);
2956 }
2957
Owen Taylor3473f882001-02-23 17:55:21 +00002958 xmlUnlinkNode(elem);
2959
2960 if (elem->type == XML_TEXT_NODE) {
2961 if (cur->type == XML_TEXT_NODE) {
Owen Taylor3473f882001-02-23 17:55:21 +00002962 xmlNodeAddContent(cur, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002963 xmlFreeNode(elem);
2964 return(cur);
2965 }
Daniel Veillard9e1c72d2001-08-31 20:03:19 +00002966 if ((cur->next != NULL) && (cur->next->type == XML_TEXT_NODE) &&
2967 (cur->name == cur->next->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002968 xmlChar *tmp;
2969
2970 tmp = xmlStrdup(elem->content);
2971 tmp = xmlStrcat(tmp, cur->next->content);
2972 xmlNodeSetContent(cur->next, tmp);
2973 xmlFree(tmp);
Owen Taylor3473f882001-02-23 17:55:21 +00002974 xmlFreeNode(elem);
2975 return(cur->next);
2976 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002977 } else if (elem->type == XML_ATTRIBUTE_NODE) {
Rob Richards65815122006-02-25 17:13:33 +00002978 return xmlAddPropSibling(cur, cur, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00002979 }
2980
2981 if (elem->doc != cur->doc) {
2982 xmlSetTreeDoc(elem, cur->doc);
2983 }
2984 elem->parent = cur->parent;
2985 elem->prev = cur;
2986 elem->next = cur->next;
2987 cur->next = elem;
2988 if (elem->next != NULL)
2989 elem->next->prev = elem;
Rob Richards65815122006-02-25 17:13:33 +00002990 if ((elem->parent != NULL) && (elem->parent->last == cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002991 elem->parent->last = elem;
2992 return(elem);
2993}
2994
William M. Brack21e4ef22005-01-02 09:53:13 +00002995#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
2996 defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00002997/**
2998 * xmlAddPrevSibling:
2999 * @cur: the child node
3000 * @elem: the new node
3001 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003002 * Add a new node @elem as the previous sibling of @cur
Owen Taylor3473f882001-02-23 17:55:21 +00003003 * merging adjacent TEXT nodes (@elem may be freed)
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003004 * If the new node was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00003005 * first unlinked from its existing context.
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003006 * If the new node is ATTRIBUTE, it is added into properties instead of children.
Daniel Veillardaa6de472008-08-25 14:53:31 +00003007 * If there is an attribute with equal name, it is first destroyed.
Owen Taylor3473f882001-02-23 17:55:21 +00003008 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003009 * Returns the new node or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003010 */
3011xmlNodePtr
3012xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) {
3013 if (cur == NULL) {
3014#ifdef DEBUG_TREE
3015 xmlGenericError(xmlGenericErrorContext,
3016 "xmlAddPrevSibling : cur == NULL\n");
3017#endif
3018 return(NULL);
3019 }
3020 if (elem == NULL) {
3021#ifdef DEBUG_TREE
3022 xmlGenericError(xmlGenericErrorContext,
3023 "xmlAddPrevSibling : elem == NULL\n");
3024#endif
3025 return(NULL);
3026 }
3027
Rob Richards19dc9612005-10-28 16:15:16 +00003028 if (cur == elem) {
3029#ifdef DEBUG_TREE
3030 xmlGenericError(xmlGenericErrorContext,
3031 "xmlAddPrevSibling : cur == elem\n");
3032#endif
3033 return(NULL);
3034 }
3035
Owen Taylor3473f882001-02-23 17:55:21 +00003036 xmlUnlinkNode(elem);
3037
3038 if (elem->type == XML_TEXT_NODE) {
3039 if (cur->type == XML_TEXT_NODE) {
Owen Taylor3473f882001-02-23 17:55:21 +00003040 xmlChar *tmp;
3041
3042 tmp = xmlStrdup(elem->content);
3043 tmp = xmlStrcat(tmp, cur->content);
3044 xmlNodeSetContent(cur, tmp);
3045 xmlFree(tmp);
Owen Taylor3473f882001-02-23 17:55:21 +00003046 xmlFreeNode(elem);
3047 return(cur);
3048 }
Daniel Veillard9e1c72d2001-08-31 20:03:19 +00003049 if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE) &&
3050 (cur->name == cur->prev->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003051 xmlNodeAddContent(cur->prev, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003052 xmlFreeNode(elem);
3053 return(cur->prev);
3054 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003055 } else if (elem->type == XML_ATTRIBUTE_NODE) {
Rob Richards65815122006-02-25 17:13:33 +00003056 return xmlAddPropSibling(cur->prev, cur, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003057 }
3058
3059 if (elem->doc != cur->doc) {
3060 xmlSetTreeDoc(elem, cur->doc);
3061 }
3062 elem->parent = cur->parent;
3063 elem->next = cur;
3064 elem->prev = cur->prev;
3065 cur->prev = elem;
3066 if (elem->prev != NULL)
3067 elem->prev->next = elem;
Rob Richards65815122006-02-25 17:13:33 +00003068 if ((elem->parent != NULL) && (elem->parent->children == cur)) {
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003069 elem->parent->children = elem;
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003070 }
Owen Taylor3473f882001-02-23 17:55:21 +00003071 return(elem);
3072}
Daniel Veillard652327a2003-09-29 18:02:38 +00003073#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00003074
3075/**
3076 * xmlAddSibling:
3077 * @cur: the child node
3078 * @elem: the new node
3079 *
3080 * Add a new element @elem to the list of siblings of @cur
3081 * merging adjacent TEXT nodes (@elem may be freed)
3082 * If the new element was already inserted in a document it is
3083 * first unlinked from its existing context.
3084 *
3085 * Returns the new element or NULL in case of error.
3086 */
3087xmlNodePtr
3088xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) {
3089 xmlNodePtr parent;
3090
3091 if (cur == NULL) {
3092#ifdef DEBUG_TREE
3093 xmlGenericError(xmlGenericErrorContext,
3094 "xmlAddSibling : cur == NULL\n");
3095#endif
3096 return(NULL);
3097 }
3098
3099 if (elem == NULL) {
3100#ifdef DEBUG_TREE
3101 xmlGenericError(xmlGenericErrorContext,
3102 "xmlAddSibling : elem == NULL\n");
3103#endif
3104 return(NULL);
3105 }
3106
Daniel Veillard43bc89c2009-03-23 19:32:04 +00003107 if (cur == elem) {
3108#ifdef DEBUG_TREE
3109 xmlGenericError(xmlGenericErrorContext,
3110 "xmlAddSibling : cur == elem\n");
3111#endif
3112 return(NULL);
3113 }
3114
Owen Taylor3473f882001-02-23 17:55:21 +00003115 /*
3116 * Constant time is we can rely on the ->parent->last to find
3117 * the last sibling.
3118 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00003119 if ((cur->type != XML_ATTRIBUTE_NODE) && (cur->parent != NULL) &&
Owen Taylor3473f882001-02-23 17:55:21 +00003120 (cur->parent->children != NULL) &&
3121 (cur->parent->last != NULL) &&
3122 (cur->parent->last->next == NULL)) {
3123 cur = cur->parent->last;
3124 } else {
3125 while (cur->next != NULL) cur = cur->next;
3126 }
3127
3128 xmlUnlinkNode(elem);
3129
Daniel Veillarde22dd5c2003-10-29 12:53:27 +00003130 if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE) &&
3131 (cur->name == elem->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003132 xmlNodeAddContent(cur, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003133 xmlFreeNode(elem);
3134 return(cur);
Rob Richards65815122006-02-25 17:13:33 +00003135 } else if (elem->type == XML_ATTRIBUTE_NODE) {
3136 return xmlAddPropSibling(cur, cur, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003137 }
3138
3139 if (elem->doc != cur->doc) {
3140 xmlSetTreeDoc(elem, cur->doc);
3141 }
3142 parent = cur->parent;
3143 elem->prev = cur;
3144 elem->next = NULL;
3145 elem->parent = parent;
3146 cur->next = elem;
3147 if (parent != NULL)
3148 parent->last = elem;
3149
3150 return(elem);
3151}
3152
3153/**
3154 * xmlAddChildList:
3155 * @parent: the parent node
3156 * @cur: the first node in the list
3157 *
3158 * Add a list of node at the end of the child list of the parent
3159 * merging adjacent TEXT nodes (@cur may be freed)
3160 *
3161 * Returns the last child or NULL in case of error.
3162 */
3163xmlNodePtr
3164xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) {
3165 xmlNodePtr prev;
3166
3167 if (parent == NULL) {
3168#ifdef DEBUG_TREE
3169 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00003170 "xmlAddChildList : parent == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003171#endif
3172 return(NULL);
3173 }
3174
3175 if (cur == NULL) {
3176#ifdef DEBUG_TREE
3177 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00003178 "xmlAddChildList : child == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003179#endif
3180 return(NULL);
3181 }
3182
3183 if ((cur->doc != NULL) && (parent->doc != NULL) &&
3184 (cur->doc != parent->doc)) {
3185#ifdef DEBUG_TREE
3186 xmlGenericError(xmlGenericErrorContext,
3187 "Elements moved to a different document\n");
3188#endif
3189 }
3190
3191 /*
3192 * add the first element at the end of the children list.
3193 */
Daniel Veillard5335dc52003-01-01 20:59:38 +00003194
Owen Taylor3473f882001-02-23 17:55:21 +00003195 if (parent->children == NULL) {
3196 parent->children = cur;
3197 } else {
3198 /*
3199 * If cur and parent->last both are TEXT nodes, then merge them.
3200 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00003201 if ((cur->type == XML_TEXT_NODE) &&
Owen Taylor3473f882001-02-23 17:55:21 +00003202 (parent->last->type == XML_TEXT_NODE) &&
3203 (cur->name == parent->last->name)) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00003204 xmlNodeAddContent(parent->last, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003205 /*
3206 * if it's the only child, nothing more to be done.
3207 */
3208 if (cur->next == NULL) {
3209 xmlFreeNode(cur);
3210 return(parent->last);
3211 }
3212 prev = cur;
3213 cur = cur->next;
3214 xmlFreeNode(prev);
3215 }
3216 prev = parent->last;
3217 prev->next = cur;
3218 cur->prev = prev;
3219 }
3220 while (cur->next != NULL) {
3221 cur->parent = parent;
3222 if (cur->doc != parent->doc) {
3223 xmlSetTreeDoc(cur, parent->doc);
3224 }
3225 cur = cur->next;
3226 }
3227 cur->parent = parent;
Rob Richards810a78b2008-12-31 22:13:57 +00003228 /* the parent may not be linked to a doc ! */
3229 if (cur->doc != parent->doc) {
3230 xmlSetTreeDoc(cur, parent->doc);
3231 }
Owen Taylor3473f882001-02-23 17:55:21 +00003232 parent->last = cur;
3233
3234 return(cur);
3235}
3236
3237/**
3238 * xmlAddChild:
3239 * @parent: the parent node
3240 * @cur: the child node
3241 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003242 * Add a new node to @parent, at the end of the child (or property) list
Owen Taylor3473f882001-02-23 17:55:21 +00003243 * merging adjacent TEXT nodes (in which case @cur is freed)
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003244 * If the new node is ATTRIBUTE, it is added into properties instead of children.
Daniel Veillardaa6de472008-08-25 14:53:31 +00003245 * If there is an attribute with equal name, it is first destroyed.
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003246 *
Owen Taylor3473f882001-02-23 17:55:21 +00003247 * Returns the child or NULL in case of error.
3248 */
3249xmlNodePtr
3250xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) {
3251 xmlNodePtr prev;
3252
3253 if (parent == NULL) {
3254#ifdef DEBUG_TREE
3255 xmlGenericError(xmlGenericErrorContext,
3256 "xmlAddChild : parent == NULL\n");
3257#endif
3258 return(NULL);
3259 }
3260
3261 if (cur == NULL) {
3262#ifdef DEBUG_TREE
3263 xmlGenericError(xmlGenericErrorContext,
3264 "xmlAddChild : child == NULL\n");
3265#endif
3266 return(NULL);
3267 }
3268
Rob Richards19dc9612005-10-28 16:15:16 +00003269 if (parent == cur) {
3270#ifdef DEBUG_TREE
3271 xmlGenericError(xmlGenericErrorContext,
3272 "xmlAddChild : parent == cur\n");
3273#endif
3274 return(NULL);
3275 }
Owen Taylor3473f882001-02-23 17:55:21 +00003276 /*
3277 * If cur is a TEXT node, merge its content with adjacent TEXT nodes
Owen Taylor3473f882001-02-23 17:55:21 +00003278 * cur is then freed.
3279 */
3280 if (cur->type == XML_TEXT_NODE) {
Daniel Veillard7db37732001-07-12 01:20:08 +00003281 if ((parent->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003282 (parent->content != NULL) &&
Rob Richards19dc9612005-10-28 16:15:16 +00003283 (parent->name == cur->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003284 xmlNodeAddContent(parent, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003285 xmlFreeNode(cur);
3286 return(parent);
3287 }
3288 if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003289 (parent->last->name == cur->name) &&
3290 (parent->last != cur)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003291 xmlNodeAddContent(parent->last, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003292 xmlFreeNode(cur);
3293 return(parent->last);
3294 }
3295 }
3296
3297 /*
3298 * add the new element at the end of the children list.
3299 */
Daniel Veillard5335dc52003-01-01 20:59:38 +00003300 prev = cur->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00003301 cur->parent = parent;
3302 if (cur->doc != parent->doc) {
3303 xmlSetTreeDoc(cur, parent->doc);
3304 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00003305 /* this check prevents a loop on tree-traversions if a developer
3306 * tries to add a node to its parent multiple times
3307 */
3308 if (prev == parent)
3309 return(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00003310
3311 /*
Daniel Veillard7db37732001-07-12 01:20:08 +00003312 * Coalescing
Owen Taylor3473f882001-02-23 17:55:21 +00003313 */
Daniel Veillard7db37732001-07-12 01:20:08 +00003314 if ((parent->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003315 (parent->content != NULL) &&
3316 (parent != cur)) {
Daniel Veillard7db37732001-07-12 01:20:08 +00003317 xmlNodeAddContent(parent, cur->content);
Daniel Veillard7db37732001-07-12 01:20:08 +00003318 xmlFreeNode(cur);
3319 return(parent);
Owen Taylor3473f882001-02-23 17:55:21 +00003320 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003321 if (cur->type == XML_ATTRIBUTE_NODE) {
Rob Richards19dc9612005-10-28 16:15:16 +00003322 if (parent->type != XML_ELEMENT_NODE)
3323 return(NULL);
Rob Richards810a78b2008-12-31 22:13:57 +00003324 if (parent->properties != NULL) {
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003325 /* check if an attribute with the same name exists */
3326 xmlAttrPtr lastattr;
Owen Taylor3473f882001-02-23 17:55:21 +00003327
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003328 if (cur->ns == NULL)
Rob Richardsc342ec62005-10-25 00:10:12 +00003329 lastattr = xmlHasNsProp(parent, cur->name, NULL);
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003330 else
3331 lastattr = xmlHasNsProp(parent, cur->name, cur->ns->href);
Rob Richardsc342ec62005-10-25 00:10:12 +00003332 if ((lastattr != NULL) && (lastattr != (xmlAttrPtr) cur) && (lastattr->type != XML_ATTRIBUTE_DECL)) {
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003333 /* different instance, destroy it (attributes must be unique) */
Rob Richards19dc9612005-10-28 16:15:16 +00003334 xmlUnlinkNode((xmlNodePtr) lastattr);
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003335 xmlFreeProp(lastattr);
3336 }
Rob Richards19dc9612005-10-28 16:15:16 +00003337 if (lastattr == (xmlAttrPtr) cur)
3338 return(cur);
Rob Richards810a78b2008-12-31 22:13:57 +00003339
3340 }
3341 if (parent->properties == NULL) {
3342 parent->properties = (xmlAttrPtr) cur;
3343 } else {
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003344 /* find the end */
Rob Richards810a78b2008-12-31 22:13:57 +00003345 xmlAttrPtr lastattr = parent->properties;
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003346 while (lastattr->next != NULL) {
3347 lastattr = lastattr->next;
3348 }
3349 lastattr->next = (xmlAttrPtr) cur;
3350 ((xmlAttrPtr) cur)->prev = lastattr;
3351 }
3352 } else {
3353 if (parent->children == NULL) {
3354 parent->children = cur;
3355 parent->last = cur;
3356 } else {
3357 prev = parent->last;
3358 prev->next = cur;
3359 cur->prev = prev;
3360 parent->last = cur;
3361 }
3362 }
Owen Taylor3473f882001-02-23 17:55:21 +00003363 return(cur);
3364}
3365
3366/**
3367 * xmlGetLastChild:
3368 * @parent: the parent node
3369 *
3370 * Search the last child of a node.
3371 * Returns the last child or NULL if none.
3372 */
3373xmlNodePtr
3374xmlGetLastChild(xmlNodePtr parent) {
3375 if (parent == NULL) {
3376#ifdef DEBUG_TREE
3377 xmlGenericError(xmlGenericErrorContext,
3378 "xmlGetLastChild : parent == NULL\n");
3379#endif
3380 return(NULL);
3381 }
3382 return(parent->last);
3383}
3384
Daniel Veillardbe2bd6a2008-11-27 15:26:28 +00003385#ifdef LIBXML_TREE_ENABLED
3386/*
3387 * 5 interfaces from DOM ElementTraversal
3388 */
3389
3390/**
3391 * xmlChildElementCount:
3392 * @parent: the parent node
3393 *
3394 * Finds the current number of child nodes of that element which are
3395 * element nodes.
3396 * Note the handling of entities references is different than in
3397 * the W3C DOM element traversal spec since we don't have back reference
3398 * from entities content to entities references.
3399 *
3400 * Returns the count of element child or 0 if not available
3401 */
3402unsigned long
3403xmlChildElementCount(xmlNodePtr parent) {
3404 unsigned long ret = 0;
3405 xmlNodePtr cur = NULL;
3406
3407 if (parent == NULL)
3408 return(0);
3409 switch (parent->type) {
3410 case XML_ELEMENT_NODE:
3411 case XML_ENTITY_NODE:
3412 case XML_DOCUMENT_NODE:
3413 case XML_HTML_DOCUMENT_NODE:
3414 cur = parent->children;
3415 break;
3416 default:
3417 return(0);
3418 }
3419 while (cur != NULL) {
3420 if (cur->type == XML_ELEMENT_NODE)
3421 ret++;
3422 cur = cur->next;
3423 }
3424 return(ret);
3425}
3426
3427/**
3428 * xmlFirstElementChild:
3429 * @parent: the parent node
3430 *
3431 * Finds the first child node of that element which is a Element node
3432 * Note the handling of entities references is different than in
3433 * the W3C DOM element traversal spec since we don't have back reference
3434 * from entities content to entities references.
3435 *
3436 * Returns the first element child or NULL if not available
3437 */
3438xmlNodePtr
3439xmlFirstElementChild(xmlNodePtr parent) {
3440 xmlNodePtr cur = NULL;
3441
3442 if (parent == NULL)
3443 return(NULL);
3444 switch (parent->type) {
3445 case XML_ELEMENT_NODE:
3446 case XML_ENTITY_NODE:
3447 case XML_DOCUMENT_NODE:
3448 case XML_HTML_DOCUMENT_NODE:
3449 cur = parent->children;
3450 break;
3451 default:
3452 return(NULL);
3453 }
3454 while (cur != NULL) {
3455 if (cur->type == XML_ELEMENT_NODE)
3456 return(cur);
3457 cur = cur->next;
3458 }
3459 return(NULL);
3460}
3461
3462/**
3463 * xmlLastElementChild:
3464 * @parent: the parent node
3465 *
3466 * Finds the last child node of that element which is a Element node
3467 * Note the handling of entities references is different than in
3468 * the W3C DOM element traversal spec since we don't have back reference
3469 * from entities content to entities references.
3470 *
3471 * Returns the last element child or NULL if not available
3472 */
3473xmlNodePtr
3474xmlLastElementChild(xmlNodePtr parent) {
3475 xmlNodePtr cur = NULL;
3476
3477 if (parent == NULL)
3478 return(NULL);
3479 switch (parent->type) {
3480 case XML_ELEMENT_NODE:
3481 case XML_ENTITY_NODE:
3482 case XML_DOCUMENT_NODE:
3483 case XML_HTML_DOCUMENT_NODE:
3484 cur = parent->last;
3485 break;
3486 default:
3487 return(NULL);
3488 }
3489 while (cur != NULL) {
3490 if (cur->type == XML_ELEMENT_NODE)
3491 return(cur);
3492 cur = cur->prev;
3493 }
3494 return(NULL);
3495}
3496
3497/**
3498 * xmlPreviousElementSibling:
3499 * @node: the current node
3500 *
3501 * Finds the first closest previous sibling of the node which is an
3502 * element node.
3503 * Note the handling of entities references is different than in
3504 * the W3C DOM element traversal spec since we don't have back reference
3505 * from entities content to entities references.
3506 *
3507 * Returns the previous element sibling or NULL if not available
3508 */
3509xmlNodePtr
3510xmlPreviousElementSibling(xmlNodePtr node) {
3511 if (node == NULL)
3512 return(NULL);
3513 switch (node->type) {
3514 case XML_ELEMENT_NODE:
3515 case XML_TEXT_NODE:
3516 case XML_CDATA_SECTION_NODE:
3517 case XML_ENTITY_REF_NODE:
3518 case XML_ENTITY_NODE:
3519 case XML_PI_NODE:
3520 case XML_COMMENT_NODE:
3521 case XML_XINCLUDE_START:
3522 case XML_XINCLUDE_END:
3523 node = node->prev;
3524 break;
3525 default:
3526 return(NULL);
3527 }
3528 while (node != NULL) {
3529 if (node->type == XML_ELEMENT_NODE)
3530 return(node);
3531 node = node->next;
3532 }
3533 return(NULL);
3534}
3535
3536/**
3537 * xmlNextElementSibling:
3538 * @node: the current node
3539 *
3540 * Finds the first closest next sibling of the node which is an
3541 * element node.
3542 * Note the handling of entities references is different than in
3543 * the W3C DOM element traversal spec since we don't have back reference
3544 * from entities content to entities references.
3545 *
3546 * Returns the next element sibling or NULL if not available
3547 */
3548xmlNodePtr
3549xmlNextElementSibling(xmlNodePtr node) {
3550 if (node == NULL)
3551 return(NULL);
3552 switch (node->type) {
3553 case XML_ELEMENT_NODE:
3554 case XML_TEXT_NODE:
3555 case XML_CDATA_SECTION_NODE:
3556 case XML_ENTITY_REF_NODE:
3557 case XML_ENTITY_NODE:
3558 case XML_PI_NODE:
3559 case XML_COMMENT_NODE:
3560 case XML_DTD_NODE:
3561 case XML_XINCLUDE_START:
3562 case XML_XINCLUDE_END:
3563 node = node->next;
3564 break;
3565 default:
3566 return(NULL);
3567 }
3568 while (node != NULL) {
3569 if (node->type == XML_ELEMENT_NODE)
3570 return(node);
3571 node = node->next;
3572 }
3573 return(NULL);
3574}
3575
3576#endif /* LIBXML_TREE_ENABLED */
3577
Owen Taylor3473f882001-02-23 17:55:21 +00003578/**
3579 * xmlFreeNodeList:
3580 * @cur: the first node in the list
3581 *
3582 * Free a node and all its siblings, this is a recursive behaviour, all
3583 * the children are freed too.
3584 */
3585void
3586xmlFreeNodeList(xmlNodePtr cur) {
3587 xmlNodePtr next;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003588 xmlDictPtr dict = NULL;
3589
3590 if (cur == NULL) return;
Daniel Veillarde6a55192002-01-14 17:11:53 +00003591 if (cur->type == XML_NAMESPACE_DECL) {
3592 xmlFreeNsList((xmlNsPtr) cur);
3593 return;
3594 }
Daniel Veillard9adc0462003-03-24 18:39:54 +00003595 if ((cur->type == XML_DOCUMENT_NODE) ||
3596#ifdef LIBXML_DOCB_ENABLED
3597 (cur->type == XML_DOCB_DOCUMENT_NODE) ||
Daniel Veillard9adc0462003-03-24 18:39:54 +00003598#endif
Daniel Veillard6560a422003-03-27 21:25:38 +00003599 (cur->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard9adc0462003-03-24 18:39:54 +00003600 xmlFreeDoc((xmlDocPtr) cur);
3601 return;
3602 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003603 if (cur->doc != NULL) dict = cur->doc->dict;
Owen Taylor3473f882001-02-23 17:55:21 +00003604 while (cur != NULL) {
3605 next = cur->next;
Daniel Veillard02141ea2001-04-30 11:46:40 +00003606 if (cur->type != XML_DTD_NODE) {
Daniel Veillard5335dc52003-01-01 20:59:38 +00003607
Daniel Veillarda880b122003-04-21 21:36:41 +00003608 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00003609 xmlDeregisterNodeDefaultValue(cur);
3610
Daniel Veillard02141ea2001-04-30 11:46:40 +00003611 if ((cur->children != NULL) &&
3612 (cur->type != XML_ENTITY_REF_NODE))
3613 xmlFreeNodeList(cur->children);
Daniel Veillard01c13b52002-12-10 15:19:08 +00003614 if (((cur->type == XML_ELEMENT_NODE) ||
3615 (cur->type == XML_XINCLUDE_START) ||
3616 (cur->type == XML_XINCLUDE_END)) &&
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003617 (cur->properties != NULL))
Daniel Veillard02141ea2001-04-30 11:46:40 +00003618 xmlFreePropList(cur->properties);
Daniel Veillard7db37732001-07-12 01:20:08 +00003619 if ((cur->type != XML_ELEMENT_NODE) &&
3620 (cur->type != XML_XINCLUDE_START) &&
3621 (cur->type != XML_XINCLUDE_END) &&
Daniel Veillard8874b942005-08-25 13:19:21 +00003622 (cur->type != XML_ENTITY_REF_NODE) &&
3623 (cur->content != (xmlChar *) &(cur->properties))) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003624 DICT_FREE(cur->content)
Daniel Veillard7db37732001-07-12 01:20:08 +00003625 }
3626 if (((cur->type == XML_ELEMENT_NODE) ||
3627 (cur->type == XML_XINCLUDE_START) ||
3628 (cur->type == XML_XINCLUDE_END)) &&
3629 (cur->nsDef != NULL))
3630 xmlFreeNsList(cur->nsDef);
3631
Daniel Veillard9cc6dc62001-06-11 08:09:20 +00003632 /*
3633 * When a node is a text node or a comment, it uses a global static
3634 * variable for the name of the node.
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003635 * Otherwise the node name might come from the document's
3636 * dictionnary
Daniel Veillard9cc6dc62001-06-11 08:09:20 +00003637 */
Daniel Veillard02141ea2001-04-30 11:46:40 +00003638 if ((cur->name != NULL) &&
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003639 (cur->type != XML_TEXT_NODE) &&
3640 (cur->type != XML_COMMENT_NODE))
3641 DICT_FREE(cur->name)
Daniel Veillard02141ea2001-04-30 11:46:40 +00003642 xmlFree(cur);
3643 }
Owen Taylor3473f882001-02-23 17:55:21 +00003644 cur = next;
3645 }
3646}
3647
3648/**
3649 * xmlFreeNode:
3650 * @cur: the node
3651 *
3652 * Free a node, this is a recursive behaviour, all the children are freed too.
3653 * This doesn't unlink the child from the list, use xmlUnlinkNode() first.
3654 */
3655void
3656xmlFreeNode(xmlNodePtr cur) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003657 xmlDictPtr dict = NULL;
3658
3659 if (cur == NULL) return;
Daniel Veillard5335dc52003-01-01 20:59:38 +00003660
Daniel Veillard02141ea2001-04-30 11:46:40 +00003661 /* use xmlFreeDtd for DTD nodes */
Daniel Veillarde6a55192002-01-14 17:11:53 +00003662 if (cur->type == XML_DTD_NODE) {
3663 xmlFreeDtd((xmlDtdPtr) cur);
Owen Taylor3473f882001-02-23 17:55:21 +00003664 return;
Daniel Veillarde6a55192002-01-14 17:11:53 +00003665 }
3666 if (cur->type == XML_NAMESPACE_DECL) {
3667 xmlFreeNs((xmlNsPtr) cur);
3668 return;
3669 }
Daniel Veillarda70d62f2002-11-07 14:18:03 +00003670 if (cur->type == XML_ATTRIBUTE_NODE) {
3671 xmlFreeProp((xmlAttrPtr) cur);
3672 return;
3673 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00003674
Daniel Veillarda880b122003-04-21 21:36:41 +00003675 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00003676 xmlDeregisterNodeDefaultValue(cur);
3677
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003678 if (cur->doc != NULL) dict = cur->doc->dict;
3679
Daniel Veillard2cba4152008-08-27 11:45:41 +00003680 if (cur->type == XML_ENTITY_DECL) {
3681 xmlEntityPtr ent = (xmlEntityPtr) cur;
3682 DICT_FREE(ent->SystemID);
3683 DICT_FREE(ent->ExternalID);
3684 }
Owen Taylor3473f882001-02-23 17:55:21 +00003685 if ((cur->children != NULL) &&
3686 (cur->type != XML_ENTITY_REF_NODE))
3687 xmlFreeNodeList(cur->children);
Daniel Veillard01c13b52002-12-10 15:19:08 +00003688 if (((cur->type == XML_ELEMENT_NODE) ||
3689 (cur->type == XML_XINCLUDE_START) ||
3690 (cur->type == XML_XINCLUDE_END)) &&
3691 (cur->properties != NULL))
Daniel Veillard02141ea2001-04-30 11:46:40 +00003692 xmlFreePropList(cur->properties);
Daniel Veillard7db37732001-07-12 01:20:08 +00003693 if ((cur->type != XML_ELEMENT_NODE) &&
3694 (cur->content != NULL) &&
3695 (cur->type != XML_ENTITY_REF_NODE) &&
3696 (cur->type != XML_XINCLUDE_END) &&
Daniel Veillard8874b942005-08-25 13:19:21 +00003697 (cur->type != XML_XINCLUDE_START) &&
3698 (cur->content != (xmlChar *) &(cur->properties))) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003699 DICT_FREE(cur->content)
Daniel Veillard7db37732001-07-12 01:20:08 +00003700 }
3701
Daniel Veillardacd370f2001-06-09 17:17:51 +00003702 /*
3703 * When a node is a text node or a comment, it uses a global static
3704 * variable for the name of the node.
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003705 * Otherwise the node name might come from the document's dictionnary
Daniel Veillardacd370f2001-06-09 17:17:51 +00003706 */
Owen Taylor3473f882001-02-23 17:55:21 +00003707 if ((cur->name != NULL) &&
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003708 (cur->type != XML_TEXT_NODE) &&
3709 (cur->type != XML_COMMENT_NODE))
3710 DICT_FREE(cur->name)
Daniel Veillardacd370f2001-06-09 17:17:51 +00003711
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003712 if (((cur->type == XML_ELEMENT_NODE) ||
3713 (cur->type == XML_XINCLUDE_START) ||
3714 (cur->type == XML_XINCLUDE_END)) &&
3715 (cur->nsDef != NULL))
3716 xmlFreeNsList(cur->nsDef);
Owen Taylor3473f882001-02-23 17:55:21 +00003717 xmlFree(cur);
3718}
3719
3720/**
3721 * xmlUnlinkNode:
3722 * @cur: the node
3723 *
3724 * Unlink a node from it's current context, the node is not freed
3725 */
3726void
3727xmlUnlinkNode(xmlNodePtr cur) {
3728 if (cur == NULL) {
3729#ifdef DEBUG_TREE
3730 xmlGenericError(xmlGenericErrorContext,
3731 "xmlUnlinkNode : node == NULL\n");
3732#endif
3733 return;
3734 }
Daniel Veillard29e43992001-12-13 22:21:58 +00003735 if (cur->type == XML_DTD_NODE) {
3736 xmlDocPtr doc;
3737 doc = cur->doc;
Daniel Veillarda067e652003-05-01 08:03:46 +00003738 if (doc != NULL) {
3739 if (doc->intSubset == (xmlDtdPtr) cur)
3740 doc->intSubset = NULL;
3741 if (doc->extSubset == (xmlDtdPtr) cur)
3742 doc->extSubset = NULL;
3743 }
Daniel Veillard29e43992001-12-13 22:21:58 +00003744 }
Daniel Veillard2cba4152008-08-27 11:45:41 +00003745 if (cur->type == XML_ENTITY_DECL) {
3746 xmlDocPtr doc;
3747 doc = cur->doc;
3748 if (doc != NULL) {
3749 if (doc->intSubset != NULL) {
3750 if (xmlHashLookup(doc->intSubset->entities, cur->name) == cur)
3751 xmlHashRemoveEntry(doc->intSubset->entities, cur->name,
3752 NULL);
3753 if (xmlHashLookup(doc->intSubset->pentities, cur->name) == cur)
3754 xmlHashRemoveEntry(doc->intSubset->pentities, cur->name,
3755 NULL);
3756 }
3757 if (doc->extSubset != NULL) {
3758 if (xmlHashLookup(doc->extSubset->entities, cur->name) == cur)
3759 xmlHashRemoveEntry(doc->extSubset->entities, cur->name,
3760 NULL);
3761 if (xmlHashLookup(doc->extSubset->pentities, cur->name) == cur)
3762 xmlHashRemoveEntry(doc->extSubset->pentities, cur->name,
3763 NULL);
3764 }
3765 }
3766 }
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003767 if (cur->parent != NULL) {
3768 xmlNodePtr parent;
3769 parent = cur->parent;
3770 if (cur->type == XML_ATTRIBUTE_NODE) {
3771 if (parent->properties == (xmlAttrPtr) cur)
3772 parent->properties = ((xmlAttrPtr) cur)->next;
3773 } else {
3774 if (parent->children == cur)
3775 parent->children = cur->next;
3776 if (parent->last == cur)
3777 parent->last = cur->prev;
3778 }
3779 cur->parent = NULL;
3780 }
Owen Taylor3473f882001-02-23 17:55:21 +00003781 if (cur->next != NULL)
3782 cur->next->prev = cur->prev;
3783 if (cur->prev != NULL)
3784 cur->prev->next = cur->next;
3785 cur->next = cur->prev = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00003786}
3787
Daniel Veillard2156d432004-03-04 15:59:36 +00003788#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00003789/**
3790 * xmlReplaceNode:
3791 * @old: the old node
3792 * @cur: the node
3793 *
William M. Brackd7cf7f82003-11-14 07:13:16 +00003794 * Unlink the old node from its current context, prune the new one
Daniel Veillardd1640922001-12-17 15:30:10 +00003795 * at the same place. If @cur was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00003796 * first unlinked from its existing context.
3797 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003798 * Returns the @old node
Owen Taylor3473f882001-02-23 17:55:21 +00003799 */
3800xmlNodePtr
3801xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) {
Daniel Veillardce244ad2004-11-05 10:03:46 +00003802 if (old == cur) return(NULL);
Daniel Veillarda03e3652004-11-02 18:45:30 +00003803 if ((old == NULL) || (old->parent == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003804#ifdef DEBUG_TREE
3805 xmlGenericError(xmlGenericErrorContext,
Daniel Veillarda03e3652004-11-02 18:45:30 +00003806 "xmlReplaceNode : old == NULL or without parent\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003807#endif
3808 return(NULL);
3809 }
3810 if (cur == NULL) {
3811 xmlUnlinkNode(old);
3812 return(old);
3813 }
3814 if (cur == old) {
3815 return(old);
3816 }
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003817 if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) {
3818#ifdef DEBUG_TREE
3819 xmlGenericError(xmlGenericErrorContext,
3820 "xmlReplaceNode : Trying to replace attribute node with other node type\n");
3821#endif
3822 return(old);
3823 }
3824 if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) {
3825#ifdef DEBUG_TREE
3826 xmlGenericError(xmlGenericErrorContext,
3827 "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n");
3828#endif
3829 return(old);
3830 }
Owen Taylor3473f882001-02-23 17:55:21 +00003831 xmlUnlinkNode(cur);
Daniel Veillard64d7d122005-05-11 18:03:42 +00003832 xmlSetTreeDoc(cur, old->doc);
Owen Taylor3473f882001-02-23 17:55:21 +00003833 cur->parent = old->parent;
3834 cur->next = old->next;
3835 if (cur->next != NULL)
3836 cur->next->prev = cur;
3837 cur->prev = old->prev;
3838 if (cur->prev != NULL)
3839 cur->prev->next = cur;
3840 if (cur->parent != NULL) {
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003841 if (cur->type == XML_ATTRIBUTE_NODE) {
3842 if (cur->parent->properties == (xmlAttrPtr)old)
3843 cur->parent->properties = ((xmlAttrPtr) cur);
3844 } else {
3845 if (cur->parent->children == old)
3846 cur->parent->children = cur;
3847 if (cur->parent->last == old)
3848 cur->parent->last = cur;
3849 }
Owen Taylor3473f882001-02-23 17:55:21 +00003850 }
3851 old->next = old->prev = NULL;
3852 old->parent = NULL;
3853 return(old);
3854}
Daniel Veillard652327a2003-09-29 18:02:38 +00003855#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00003856
3857/************************************************************************
3858 * *
3859 * Copy operations *
3860 * *
3861 ************************************************************************/
Daniel Veillardaa6de472008-08-25 14:53:31 +00003862
Owen Taylor3473f882001-02-23 17:55:21 +00003863/**
3864 * xmlCopyNamespace:
3865 * @cur: the namespace
3866 *
3867 * Do a copy of the namespace.
3868 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003869 * Returns: a new #xmlNsPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003870 */
3871xmlNsPtr
3872xmlCopyNamespace(xmlNsPtr cur) {
3873 xmlNsPtr ret;
3874
3875 if (cur == NULL) return(NULL);
3876 switch (cur->type) {
3877 case XML_LOCAL_NAMESPACE:
3878 ret = xmlNewNs(NULL, cur->href, cur->prefix);
3879 break;
3880 default:
3881#ifdef DEBUG_TREE
3882 xmlGenericError(xmlGenericErrorContext,
3883 "xmlCopyNamespace: invalid type %d\n", cur->type);
3884#endif
3885 return(NULL);
3886 }
3887 return(ret);
3888}
3889
3890/**
3891 * xmlCopyNamespaceList:
3892 * @cur: the first namespace
3893 *
3894 * Do a copy of an namespace list.
3895 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003896 * Returns: a new #xmlNsPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003897 */
3898xmlNsPtr
3899xmlCopyNamespaceList(xmlNsPtr cur) {
3900 xmlNsPtr ret = NULL;
3901 xmlNsPtr p = NULL,q;
3902
3903 while (cur != NULL) {
3904 q = xmlCopyNamespace(cur);
3905 if (p == NULL) {
3906 ret = p = q;
3907 } else {
3908 p->next = q;
3909 p = q;
3910 }
3911 cur = cur->next;
3912 }
3913 return(ret);
3914}
3915
3916static xmlNodePtr
3917xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent);
Rob Richards19dc9612005-10-28 16:15:16 +00003918
3919static xmlAttrPtr
3920xmlCopyPropInternal(xmlDocPtr doc, xmlNodePtr target, xmlAttrPtr cur) {
Owen Taylor3473f882001-02-23 17:55:21 +00003921 xmlAttrPtr ret;
3922
3923 if (cur == NULL) return(NULL);
3924 if (target != NULL)
3925 ret = xmlNewDocProp(target->doc, cur->name, NULL);
Rob Richards19dc9612005-10-28 16:15:16 +00003926 else if (doc != NULL)
3927 ret = xmlNewDocProp(doc, cur->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003928 else if (cur->parent != NULL)
3929 ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL);
3930 else if (cur->children != NULL)
3931 ret = xmlNewDocProp(cur->children->doc, cur->name, NULL);
3932 else
3933 ret = xmlNewDocProp(NULL, cur->name, NULL);
3934 if (ret == NULL) return(NULL);
3935 ret->parent = target;
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00003936
Owen Taylor3473f882001-02-23 17:55:21 +00003937 if ((cur->ns != NULL) && (target != NULL)) {
Daniel Veillard8107a222002-01-13 14:10:10 +00003938 xmlNsPtr ns;
Daniel Veillard652327a2003-09-29 18:02:38 +00003939
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00003940 ns = xmlSearchNs(target->doc, target, cur->ns->prefix);
3941 if (ns == NULL) {
3942 /*
3943 * Humm, we are copying an element whose namespace is defined
3944 * out of the new tree scope. Search it in the original tree
3945 * and add it at the top of the new tree
3946 */
3947 ns = xmlSearchNs(cur->doc, cur->parent, cur->ns->prefix);
3948 if (ns != NULL) {
3949 xmlNodePtr root = target;
3950 xmlNodePtr pred = NULL;
3951
3952 while (root->parent != NULL) {
3953 pred = root;
3954 root = root->parent;
3955 }
3956 if (root == (xmlNodePtr) target->doc) {
3957 /* correct possibly cycling above the document elt */
3958 root = pred;
3959 }
3960 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
3961 }
3962 } else {
3963 /*
3964 * we have to find something appropriate here since
3965 * we cant be sure, that the namespce we found is identified
3966 * by the prefix
3967 */
Daniel Veillard044fc6b2002-03-04 17:09:44 +00003968 if (xmlStrEqual(ns->href, cur->ns->href)) {
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00003969 /* this is the nice case */
3970 ret->ns = ns;
3971 } else {
3972 /*
3973 * we are in trouble: we need a new reconcilied namespace.
3974 * This is expensive
3975 */
3976 ret->ns = xmlNewReconciliedNs(target->doc, target, cur->ns);
3977 }
3978 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00003979
Owen Taylor3473f882001-02-23 17:55:21 +00003980 } else
3981 ret->ns = NULL;
3982
3983 if (cur->children != NULL) {
3984 xmlNodePtr tmp;
3985
3986 ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret);
3987 ret->last = NULL;
3988 tmp = ret->children;
3989 while (tmp != NULL) {
3990 /* tmp->parent = (xmlNodePtr)ret; */
3991 if (tmp->next == NULL)
3992 ret->last = tmp;
3993 tmp = tmp->next;
3994 }
3995 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00003996 /*
3997 * Try to handle IDs
3998 */
Daniel Veillarda3db2e32002-03-08 15:46:57 +00003999 if ((target!= NULL) && (cur!= NULL) &&
4000 (target->doc != NULL) && (cur->doc != NULL) &&
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00004001 (cur->doc->ids != NULL) && (cur->parent != NULL)) {
4002 if (xmlIsID(cur->doc, cur->parent, cur)) {
4003 xmlChar *id;
4004
4005 id = xmlNodeListGetString(cur->doc, cur->children, 1);
4006 if (id != NULL) {
4007 xmlAddID(NULL, target->doc, id, ret);
4008 xmlFree(id);
4009 }
4010 }
4011 }
Owen Taylor3473f882001-02-23 17:55:21 +00004012 return(ret);
4013}
4014
4015/**
Rob Richards19dc9612005-10-28 16:15:16 +00004016 * xmlCopyProp:
4017 * @target: the element where the attribute will be grafted
4018 * @cur: the attribute
4019 *
4020 * Do a copy of the attribute.
4021 *
4022 * Returns: a new #xmlAttrPtr, or NULL in case of error.
4023 */
4024xmlAttrPtr
4025xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) {
4026 return xmlCopyPropInternal(NULL, target, cur);
4027}
4028
4029/**
Owen Taylor3473f882001-02-23 17:55:21 +00004030 * xmlCopyPropList:
4031 * @target: the element where the attributes will be grafted
4032 * @cur: the first attribute
4033 *
4034 * Do a copy of an attribute list.
4035 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004036 * Returns: a new #xmlAttrPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004037 */
4038xmlAttrPtr
4039xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) {
4040 xmlAttrPtr ret = NULL;
4041 xmlAttrPtr p = NULL,q;
4042
4043 while (cur != NULL) {
4044 q = xmlCopyProp(target, cur);
William M. Brack13dfa872004-09-18 04:52:08 +00004045 if (q == NULL)
4046 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004047 if (p == NULL) {
4048 ret = p = q;
4049 } else {
4050 p->next = q;
4051 q->prev = p;
4052 p = q;
4053 }
4054 cur = cur->next;
4055 }
4056 return(ret);
4057}
4058
4059/*
Daniel Veillardd1640922001-12-17 15:30:10 +00004060 * NOTE about the CopyNode operations !
Owen Taylor3473f882001-02-23 17:55:21 +00004061 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004062 * They are split into external and internal parts for one
Owen Taylor3473f882001-02-23 17:55:21 +00004063 * tricky reason: namespaces. Doing a direct copy of a node
4064 * say RPM:Copyright without changing the namespace pointer to
4065 * something else can produce stale links. One way to do it is
4066 * to keep a reference counter but this doesn't work as soon
4067 * as one move the element or the subtree out of the scope of
4068 * the existing namespace. The actual solution seems to add
4069 * a copy of the namespace at the top of the copied tree if
4070 * not available in the subtree.
4071 * Hence two functions, the public front-end call the inner ones
William M. Brack57e9e912004-03-09 16:19:02 +00004072 * The argument "recursive" normally indicates a recursive copy
4073 * of the node with values 0 (no) and 1 (yes). For XInclude,
4074 * however, we allow a value of 2 to indicate copy properties and
4075 * namespace info, but don't recurse on children.
Owen Taylor3473f882001-02-23 17:55:21 +00004076 */
4077
4078static xmlNodePtr
Daniel Veillard3ec4c612001-08-28 20:39:49 +00004079xmlStaticCopyNode(const xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent,
William M. Brack57e9e912004-03-09 16:19:02 +00004080 int extended) {
Owen Taylor3473f882001-02-23 17:55:21 +00004081 xmlNodePtr ret;
4082
4083 if (node == NULL) return(NULL);
Daniel Veillard39196eb2001-06-19 18:09:42 +00004084 switch (node->type) {
4085 case XML_TEXT_NODE:
4086 case XML_CDATA_SECTION_NODE:
4087 case XML_ELEMENT_NODE:
Daniel Veillardec6725e2002-09-05 11:12:45 +00004088 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00004089 case XML_ENTITY_REF_NODE:
4090 case XML_ENTITY_NODE:
4091 case XML_PI_NODE:
4092 case XML_COMMENT_NODE:
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00004093 case XML_XINCLUDE_START:
4094 case XML_XINCLUDE_END:
4095 break;
4096 case XML_ATTRIBUTE_NODE:
Rob Richards19dc9612005-10-28 16:15:16 +00004097 return((xmlNodePtr) xmlCopyPropInternal(doc, parent, (xmlAttrPtr) node));
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00004098 case XML_NAMESPACE_DECL:
4099 return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node));
Daniel Veillardaa6de472008-08-25 14:53:31 +00004100
Daniel Veillard39196eb2001-06-19 18:09:42 +00004101 case XML_DOCUMENT_NODE:
4102 case XML_HTML_DOCUMENT_NODE:
4103#ifdef LIBXML_DOCB_ENABLED
4104 case XML_DOCB_DOCUMENT_NODE:
4105#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00004106#ifdef LIBXML_TREE_ENABLED
William M. Brack57e9e912004-03-09 16:19:02 +00004107 return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, extended));
Daniel Veillard652327a2003-09-29 18:02:38 +00004108#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard39196eb2001-06-19 18:09:42 +00004109 case XML_DOCUMENT_TYPE_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00004110 case XML_NOTATION_NODE:
4111 case XML_DTD_NODE:
4112 case XML_ELEMENT_DECL:
4113 case XML_ATTRIBUTE_DECL:
4114 case XML_ENTITY_DECL:
4115 return(NULL);
4116 }
Daniel Veillardb33c2012001-04-25 12:59:04 +00004117
Owen Taylor3473f882001-02-23 17:55:21 +00004118 /*
4119 * Allocate a new node and fill the fields.
4120 */
4121 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
4122 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004123 xmlTreeErrMemory("copying node");
Owen Taylor3473f882001-02-23 17:55:21 +00004124 return(NULL);
4125 }
4126 memset(ret, 0, sizeof(xmlNode));
4127 ret->type = node->type;
4128
4129 ret->doc = doc;
Daniel Veillardaa6de472008-08-25 14:53:31 +00004130 ret->parent = parent;
Owen Taylor3473f882001-02-23 17:55:21 +00004131 if (node->name == xmlStringText)
4132 ret->name = xmlStringText;
4133 else if (node->name == xmlStringTextNoenc)
4134 ret->name = xmlStringTextNoenc;
4135 else if (node->name == xmlStringComment)
4136 ret->name = xmlStringComment;
Daniel Veillard03a53c32004-10-26 16:06:51 +00004137 else if (node->name != NULL) {
4138 if ((doc != NULL) && (doc->dict != NULL))
4139 ret->name = xmlDictLookup(doc->dict, node->name, -1);
4140 else
4141 ret->name = xmlStrdup(node->name);
4142 }
Daniel Veillard7db37732001-07-12 01:20:08 +00004143 if ((node->type != XML_ELEMENT_NODE) &&
4144 (node->content != NULL) &&
4145 (node->type != XML_ENTITY_REF_NODE) &&
4146 (node->type != XML_XINCLUDE_END) &&
4147 (node->type != XML_XINCLUDE_START)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004148 ret->content = xmlStrdup(node->content);
Daniel Veillard8107a222002-01-13 14:10:10 +00004149 }else{
4150 if (node->type == XML_ELEMENT_NODE)
Daniel Veillard3e35f8e2003-10-21 00:05:38 +00004151 ret->line = node->line;
Owen Taylor3473f882001-02-23 17:55:21 +00004152 }
Daniel Veillardacb2bda2002-01-13 16:15:43 +00004153 if (parent != NULL) {
4154 xmlNodePtr tmp;
4155
Daniel Veillard8a1b1852003-01-05 22:37:17 +00004156 /*
4157 * this is a tricky part for the node register thing:
4158 * in case ret does get coalesced in xmlAddChild
4159 * the deregister-node callback is called; so we register ret now already
4160 */
Daniel Veillarda880b122003-04-21 21:36:41 +00004161 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00004162 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
4163
Daniel Veillardacb2bda2002-01-13 16:15:43 +00004164 tmp = xmlAddChild(parent, ret);
4165 /* node could have coalesced */
4166 if (tmp != ret)
4167 return(tmp);
4168 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00004169
William M. Brack57e9e912004-03-09 16:19:02 +00004170 if (!extended)
Daniel Veillard8a1b1852003-01-05 22:37:17 +00004171 goto out;
Petr Pajas2afca4a2009-07-30 17:47:32 +02004172 if (((node->type == XML_ELEMENT_NODE) ||
4173 (node->type == XML_XINCLUDE_START)) && (node->nsDef != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00004174 ret->nsDef = xmlCopyNamespaceList(node->nsDef);
4175
4176 if (node->ns != NULL) {
4177 xmlNsPtr ns;
4178
4179 ns = xmlSearchNs(doc, ret, node->ns->prefix);
4180 if (ns == NULL) {
4181 /*
4182 * Humm, we are copying an element whose namespace is defined
4183 * out of the new tree scope. Search it in the original tree
4184 * and add it at the top of the new tree
4185 */
4186 ns = xmlSearchNs(node->doc, node, node->ns->prefix);
4187 if (ns != NULL) {
4188 xmlNodePtr root = ret;
4189
4190 while (root->parent != NULL) root = root->parent;
Daniel Veillarde82a9922001-04-22 12:12:58 +00004191 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00004192 }
4193 } else {
4194 /*
4195 * reference the existing namespace definition in our own tree.
4196 */
4197 ret->ns = ns;
4198 }
4199 }
Petr Pajas2afca4a2009-07-30 17:47:32 +02004200 if (((node->type == XML_ELEMENT_NODE) ||
4201 (node->type == XML_XINCLUDE_START)) && (node->properties != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00004202 ret->properties = xmlCopyPropList(ret, node->properties);
Daniel Veillardb33c2012001-04-25 12:59:04 +00004203 if (node->type == XML_ENTITY_REF_NODE) {
4204 if ((doc == NULL) || (node->doc != doc)) {
4205 /*
4206 * The copied node will go into a separate document, so
Daniel Veillardd1640922001-12-17 15:30:10 +00004207 * to avoid dangling references to the ENTITY_DECL node
Daniel Veillardb33c2012001-04-25 12:59:04 +00004208 * we cannot keep the reference. Try to find it in the
4209 * target document.
4210 */
4211 ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name);
4212 } else {
4213 ret->children = node->children;
4214 }
Daniel Veillard0ec98632001-11-14 15:04:32 +00004215 ret->last = ret->children;
William M. Brack57e9e912004-03-09 16:19:02 +00004216 } else if ((node->children != NULL) && (extended != 2)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004217 ret->children = xmlStaticCopyNodeList(node->children, doc, ret);
Daniel Veillard0ec98632001-11-14 15:04:32 +00004218 UPDATE_LAST_CHILD_AND_PARENT(ret)
4219 }
Daniel Veillard8a1b1852003-01-05 22:37:17 +00004220
4221out:
4222 /* if parent != NULL we already registered the node above */
Daniel Veillardac996a12004-07-30 12:02:58 +00004223 if ((parent == NULL) &&
4224 ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00004225 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
Owen Taylor3473f882001-02-23 17:55:21 +00004226 return(ret);
4227}
4228
4229static xmlNodePtr
4230xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) {
4231 xmlNodePtr ret = NULL;
4232 xmlNodePtr p = NULL,q;
4233
4234 while (node != NULL) {
Daniel Veillard652327a2003-09-29 18:02:38 +00004235#ifdef LIBXML_TREE_ENABLED
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00004236 if (node->type == XML_DTD_NODE ) {
Daniel Veillard4497e692001-06-09 14:19:02 +00004237 if (doc == NULL) {
4238 node = node->next;
4239 continue;
4240 }
Daniel Veillardb33c2012001-04-25 12:59:04 +00004241 if (doc->intSubset == NULL) {
4242 q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node );
4243 q->doc = doc;
4244 q->parent = parent;
4245 doc->intSubset = (xmlDtdPtr) q;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004246 xmlAddChild(parent, q);
Daniel Veillardb33c2012001-04-25 12:59:04 +00004247 } else {
4248 q = (xmlNodePtr) doc->intSubset;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004249 xmlAddChild(parent, q);
Daniel Veillardb33c2012001-04-25 12:59:04 +00004250 }
4251 } else
Daniel Veillard652327a2003-09-29 18:02:38 +00004252#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardb33c2012001-04-25 12:59:04 +00004253 q = xmlStaticCopyNode(node, doc, parent, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00004254 if (ret == NULL) {
4255 q->prev = NULL;
4256 ret = p = q;
Daniel Veillardacb2bda2002-01-13 16:15:43 +00004257 } else if (p != q) {
4258 /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */
Owen Taylor3473f882001-02-23 17:55:21 +00004259 p->next = q;
4260 q->prev = p;
4261 p = q;
4262 }
4263 node = node->next;
4264 }
4265 return(ret);
4266}
4267
4268/**
4269 * xmlCopyNode:
4270 * @node: the node
William M. Brack57e9e912004-03-09 16:19:02 +00004271 * @extended: if 1 do a recursive copy (properties, namespaces and children
4272 * when applicable)
4273 * if 2 copy properties and namespaces (when applicable)
Owen Taylor3473f882001-02-23 17:55:21 +00004274 *
4275 * Do a copy of the node.
4276 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004277 * Returns: a new #xmlNodePtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004278 */
4279xmlNodePtr
William M. Brack57e9e912004-03-09 16:19:02 +00004280xmlCopyNode(const xmlNodePtr node, int extended) {
Owen Taylor3473f882001-02-23 17:55:21 +00004281 xmlNodePtr ret;
4282
William M. Brack57e9e912004-03-09 16:19:02 +00004283 ret = xmlStaticCopyNode(node, NULL, NULL, extended);
Owen Taylor3473f882001-02-23 17:55:21 +00004284 return(ret);
4285}
4286
4287/**
Daniel Veillard82daa812001-04-12 08:55:36 +00004288 * xmlDocCopyNode:
4289 * @node: the node
Daniel Veillardd1640922001-12-17 15:30:10 +00004290 * @doc: the document
William M. Brack57e9e912004-03-09 16:19:02 +00004291 * @extended: if 1 do a recursive copy (properties, namespaces and children
4292 * when applicable)
4293 * if 2 copy properties and namespaces (when applicable)
Daniel Veillard82daa812001-04-12 08:55:36 +00004294 *
4295 * Do a copy of the node to a given document.
4296 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004297 * Returns: a new #xmlNodePtr, or NULL in case of error.
Daniel Veillard82daa812001-04-12 08:55:36 +00004298 */
4299xmlNodePtr
William M. Brack57e9e912004-03-09 16:19:02 +00004300xmlDocCopyNode(const xmlNodePtr node, xmlDocPtr doc, int extended) {
Daniel Veillard82daa812001-04-12 08:55:36 +00004301 xmlNodePtr ret;
4302
William M. Brack57e9e912004-03-09 16:19:02 +00004303 ret = xmlStaticCopyNode(node, doc, NULL, extended);
Daniel Veillard82daa812001-04-12 08:55:36 +00004304 return(ret);
4305}
4306
4307/**
Daniel Veillard03a53c32004-10-26 16:06:51 +00004308 * xmlDocCopyNodeList:
4309 * @doc: the target document
4310 * @node: the first node in the list.
4311 *
4312 * Do a recursive copy of the node list.
4313 *
4314 * Returns: a new #xmlNodePtr, or NULL in case of error.
4315 */
4316xmlNodePtr xmlDocCopyNodeList(xmlDocPtr doc, const xmlNodePtr node) {
4317 xmlNodePtr ret = xmlStaticCopyNodeList(node, doc, NULL);
4318 return(ret);
4319}
4320
4321/**
Owen Taylor3473f882001-02-23 17:55:21 +00004322 * xmlCopyNodeList:
4323 * @node: the first node in the list.
4324 *
4325 * Do a recursive copy of the node list.
Daniel Veillard03a53c32004-10-26 16:06:51 +00004326 * Use xmlDocCopyNodeList() if possible to ensure string interning.
Owen Taylor3473f882001-02-23 17:55:21 +00004327 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004328 * Returns: a new #xmlNodePtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004329 */
Daniel Veillard3ec4c612001-08-28 20:39:49 +00004330xmlNodePtr xmlCopyNodeList(const xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00004331 xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL);
4332 return(ret);
4333}
4334
Daniel Veillard2156d432004-03-04 15:59:36 +00004335#if defined(LIBXML_TREE_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004336/**
Owen Taylor3473f882001-02-23 17:55:21 +00004337 * xmlCopyDtd:
4338 * @dtd: the dtd
4339 *
4340 * Do a copy of the dtd.
4341 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004342 * Returns: a new #xmlDtdPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004343 */
4344xmlDtdPtr
4345xmlCopyDtd(xmlDtdPtr dtd) {
4346 xmlDtdPtr ret;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004347 xmlNodePtr cur, p = NULL, q;
Owen Taylor3473f882001-02-23 17:55:21 +00004348
4349 if (dtd == NULL) return(NULL);
4350 ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID);
4351 if (ret == NULL) return(NULL);
4352 if (dtd->entities != NULL)
4353 ret->entities = (void *) xmlCopyEntitiesTable(
4354 (xmlEntitiesTablePtr) dtd->entities);
4355 if (dtd->notations != NULL)
4356 ret->notations = (void *) xmlCopyNotationTable(
4357 (xmlNotationTablePtr) dtd->notations);
4358 if (dtd->elements != NULL)
4359 ret->elements = (void *) xmlCopyElementTable(
4360 (xmlElementTablePtr) dtd->elements);
4361 if (dtd->attributes != NULL)
4362 ret->attributes = (void *) xmlCopyAttributeTable(
4363 (xmlAttributeTablePtr) dtd->attributes);
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004364 if (dtd->pentities != NULL)
4365 ret->pentities = (void *) xmlCopyEntitiesTable(
4366 (xmlEntitiesTablePtr) dtd->pentities);
Daniel Veillardaa6de472008-08-25 14:53:31 +00004367
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004368 cur = dtd->children;
4369 while (cur != NULL) {
4370 q = NULL;
4371
4372 if (cur->type == XML_ENTITY_DECL) {
4373 xmlEntityPtr tmp = (xmlEntityPtr) cur;
4374 switch (tmp->etype) {
4375 case XML_INTERNAL_GENERAL_ENTITY:
4376 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
4377 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
4378 q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name);
4379 break;
4380 case XML_INTERNAL_PARAMETER_ENTITY:
4381 case XML_EXTERNAL_PARAMETER_ENTITY:
Daniel Veillardaa6de472008-08-25 14:53:31 +00004382 q = (xmlNodePtr)
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004383 xmlGetParameterEntityFromDtd(ret, tmp->name);
4384 break;
4385 case XML_INTERNAL_PREDEFINED_ENTITY:
4386 break;
4387 }
4388 } else if (cur->type == XML_ELEMENT_DECL) {
4389 xmlElementPtr tmp = (xmlElementPtr) cur;
4390 q = (xmlNodePtr)
4391 xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix);
4392 } else if (cur->type == XML_ATTRIBUTE_DECL) {
4393 xmlAttributePtr tmp = (xmlAttributePtr) cur;
Daniel Veillardaa6de472008-08-25 14:53:31 +00004394 q = (xmlNodePtr)
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004395 xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix);
4396 } else if (cur->type == XML_COMMENT_NODE) {
4397 q = xmlCopyNode(cur, 0);
4398 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00004399
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004400 if (q == NULL) {
4401 cur = cur->next;
4402 continue;
4403 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00004404
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004405 if (p == NULL)
4406 ret->children = q;
4407 else
Daniel Veillardaa6de472008-08-25 14:53:31 +00004408 p->next = q;
4409
4410 q->prev = p;
4411 q->parent = (xmlNodePtr) ret;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004412 q->next = NULL;
4413 ret->last = q;
Daniel Veillardaa6de472008-08-25 14:53:31 +00004414 p = q;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004415 cur = cur->next;
4416 }
4417
Owen Taylor3473f882001-02-23 17:55:21 +00004418 return(ret);
4419}
Daniel Veillard2156d432004-03-04 15:59:36 +00004420#endif
Owen Taylor3473f882001-02-23 17:55:21 +00004421
Daniel Veillard2156d432004-03-04 15:59:36 +00004422#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004423/**
4424 * xmlCopyDoc:
4425 * @doc: the document
William M. Brack57e9e912004-03-09 16:19:02 +00004426 * @recursive: if not zero do a recursive copy.
Owen Taylor3473f882001-02-23 17:55:21 +00004427 *
4428 * Do a copy of the document info. If recursive, the content tree will
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004429 * be copied too as well as DTD, namespaces and entities.
Owen Taylor3473f882001-02-23 17:55:21 +00004430 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004431 * Returns: a new #xmlDocPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004432 */
4433xmlDocPtr
4434xmlCopyDoc(xmlDocPtr doc, int recursive) {
4435 xmlDocPtr ret;
4436
4437 if (doc == NULL) return(NULL);
4438 ret = xmlNewDoc(doc->version);
4439 if (ret == NULL) return(NULL);
4440 if (doc->name != NULL)
4441 ret->name = xmlMemStrdup(doc->name);
4442 if (doc->encoding != NULL)
4443 ret->encoding = xmlStrdup(doc->encoding);
Daniel Veillardf59507d2005-01-27 17:26:49 +00004444 if (doc->URL != NULL)
4445 ret->URL = xmlStrdup(doc->URL);
Owen Taylor3473f882001-02-23 17:55:21 +00004446 ret->charset = doc->charset;
4447 ret->compression = doc->compression;
4448 ret->standalone = doc->standalone;
4449 if (!recursive) return(ret);
4450
Daniel Veillardb33c2012001-04-25 12:59:04 +00004451 ret->last = NULL;
4452 ret->children = NULL;
Daniel Veillard2156d432004-03-04 15:59:36 +00004453#ifdef LIBXML_TREE_ENABLED
Daniel Veillardb33c2012001-04-25 12:59:04 +00004454 if (doc->intSubset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004455 ret->intSubset = xmlCopyDtd(doc->intSubset);
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004456 xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret);
Daniel Veillardb33c2012001-04-25 12:59:04 +00004457 ret->intSubset->parent = ret;
4458 }
Daniel Veillard2156d432004-03-04 15:59:36 +00004459#endif
Owen Taylor3473f882001-02-23 17:55:21 +00004460 if (doc->oldNs != NULL)
4461 ret->oldNs = xmlCopyNamespaceList(doc->oldNs);
4462 if (doc->children != NULL) {
4463 xmlNodePtr tmp;
Daniel Veillardaa6de472008-08-25 14:53:31 +00004464
Daniel Veillardb33c2012001-04-25 12:59:04 +00004465 ret->children = xmlStaticCopyNodeList(doc->children, ret,
4466 (xmlNodePtr)ret);
Owen Taylor3473f882001-02-23 17:55:21 +00004467 ret->last = NULL;
4468 tmp = ret->children;
4469 while (tmp != NULL) {
4470 if (tmp->next == NULL)
4471 ret->last = tmp;
4472 tmp = tmp->next;
4473 }
4474 }
4475 return(ret);
4476}
Daniel Veillard652327a2003-09-29 18:02:38 +00004477#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004478
4479/************************************************************************
4480 * *
4481 * Content access functions *
4482 * *
4483 ************************************************************************/
Daniel Veillardaa6de472008-08-25 14:53:31 +00004484
Owen Taylor3473f882001-02-23 17:55:21 +00004485/**
Daniel Veillard8faa7832001-11-26 15:58:08 +00004486 * xmlGetLineNo:
Daniel Veillard01c13b52002-12-10 15:19:08 +00004487 * @node: valid node
Daniel Veillard8faa7832001-11-26 15:58:08 +00004488 *
William M. Brackd7cf7f82003-11-14 07:13:16 +00004489 * Get line number of @node. This requires activation of this option
Daniel Veillardd1640922001-12-17 15:30:10 +00004490 * before invoking the parser by calling xmlLineNumbersDefault(1)
Daniel Veillard8faa7832001-11-26 15:58:08 +00004491 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004492 * Returns the line number if successful, -1 otherwise
Daniel Veillard8faa7832001-11-26 15:58:08 +00004493 */
4494long
4495xmlGetLineNo(xmlNodePtr node)
4496{
4497 long result = -1;
4498
4499 if (!node)
4500 return result;
Daniel Veillard73da77e2005-08-24 14:05:37 +00004501 if ((node->type == XML_ELEMENT_NODE) ||
4502 (node->type == XML_TEXT_NODE) ||
4503 (node->type == XML_COMMENT_NODE) ||
4504 (node->type == XML_PI_NODE))
Daniel Veillard3e35f8e2003-10-21 00:05:38 +00004505 result = (long) node->line;
Daniel Veillard8faa7832001-11-26 15:58:08 +00004506 else if ((node->prev != NULL) &&
4507 ((node->prev->type == XML_ELEMENT_NODE) ||
Daniel Veillard73da77e2005-08-24 14:05:37 +00004508 (node->prev->type == XML_TEXT_NODE) ||
4509 (node->prev->type == XML_COMMENT_NODE) ||
4510 (node->prev->type == XML_PI_NODE)))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004511 result = xmlGetLineNo(node->prev);
4512 else if ((node->parent != NULL) &&
Daniel Veillard73da77e2005-08-24 14:05:37 +00004513 (node->parent->type == XML_ELEMENT_NODE))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004514 result = xmlGetLineNo(node->parent);
4515
4516 return result;
4517}
4518
Daniel Veillard2156d432004-03-04 15:59:36 +00004519#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED)
Daniel Veillard8faa7832001-11-26 15:58:08 +00004520/**
4521 * xmlGetNodePath:
4522 * @node: a node
4523 *
4524 * Build a structure based Path for the given node
4525 *
4526 * Returns the new path or NULL in case of error. The caller must free
4527 * the returned string
4528 */
4529xmlChar *
4530xmlGetNodePath(xmlNodePtr node)
4531{
4532 xmlNodePtr cur, tmp, next;
4533 xmlChar *buffer = NULL, *temp;
4534 size_t buf_len;
4535 xmlChar *buf;
Daniel Veillard96c3a3b2002-10-14 15:39:04 +00004536 const char *sep;
Daniel Veillard8faa7832001-11-26 15:58:08 +00004537 const char *name;
4538 char nametemp[100];
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004539 int occur = 0, generic;
Daniel Veillard8faa7832001-11-26 15:58:08 +00004540
4541 if (node == NULL)
4542 return (NULL);
4543
4544 buf_len = 500;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004545 buffer = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004546 if (buffer == NULL) {
4547 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004548 return (NULL);
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004549 }
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004550 buf = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
Daniel Veillard8faa7832001-11-26 15:58:08 +00004551 if (buf == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004552 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004553 xmlFree(buffer);
4554 return (NULL);
4555 }
4556
4557 buffer[0] = 0;
4558 cur = node;
4559 do {
4560 name = "";
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004561 sep = "?";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004562 occur = 0;
4563 if ((cur->type == XML_DOCUMENT_NODE) ||
4564 (cur->type == XML_HTML_DOCUMENT_NODE)) {
4565 if (buffer[0] == '/')
4566 break;
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004567 sep = "/";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004568 next = NULL;
4569 } else if (cur->type == XML_ELEMENT_NODE) {
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004570 generic = 0;
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004571 sep = "/";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004572 name = (const char *) cur->name;
4573 if (cur->ns) {
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004574 if (cur->ns->prefix != NULL) {
William M. Brack13dfa872004-09-18 04:52:08 +00004575 snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
Daniel Veillardaa6de472008-08-25 14:53:31 +00004576 (char *)cur->ns->prefix, (char *)cur->name);
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004577 nametemp[sizeof(nametemp) - 1] = 0;
4578 name = nametemp;
4579 } else {
4580 /*
4581 * We cannot express named elements in the default
4582 * namespace, so use "*".
4583 */
4584 generic = 1;
4585 name = "*";
Daniel Veillardaa6de472008-08-25 14:53:31 +00004586 }
Daniel Veillard8faa7832001-11-26 15:58:08 +00004587 }
4588 next = cur->parent;
4589
4590 /*
4591 * Thumbler index computation
Daniel Veillardc00cda82003-04-07 10:22:39 +00004592 * TODO: the ocurence test seems bogus for namespaced names
Daniel Veillard8faa7832001-11-26 15:58:08 +00004593 */
4594 tmp = cur->prev;
4595 while (tmp != NULL) {
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00004596 if ((tmp->type == XML_ELEMENT_NODE) &&
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004597 (generic ||
Kasimier T. Buchcik43ceb1e2006-06-12 11:08:18 +00004598 (xmlStrEqual(cur->name, tmp->name) &&
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004599 ((tmp->ns == cur->ns) ||
4600 ((tmp->ns != NULL) && (cur->ns != NULL) &&
Kasimier T. Buchcik43ceb1e2006-06-12 11:08:18 +00004601 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004602 occur++;
4603 tmp = tmp->prev;
4604 }
4605 if (occur == 0) {
4606 tmp = cur->next;
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004607 while (tmp != NULL && occur == 0) {
4608 if ((tmp->type == XML_ELEMENT_NODE) &&
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004609 (generic ||
Kasimier T. Buchcik43ceb1e2006-06-12 11:08:18 +00004610 (xmlStrEqual(cur->name, tmp->name) &&
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004611 ((tmp->ns == cur->ns) ||
4612 ((tmp->ns != NULL) && (cur->ns != NULL) &&
Kasimier T. Buchcik43ceb1e2006-06-12 11:08:18 +00004613 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004614 occur++;
4615 tmp = tmp->next;
4616 }
4617 if (occur != 0)
4618 occur = 1;
4619 } else
4620 occur++;
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004621 } else if (cur->type == XML_COMMENT_NODE) {
4622 sep = "/";
4623 name = "comment()";
4624 next = cur->parent;
4625
4626 /*
4627 * Thumbler index computation
4628 */
4629 tmp = cur->prev;
4630 while (tmp != NULL) {
4631 if (tmp->type == XML_COMMENT_NODE)
4632 occur++;
4633 tmp = tmp->prev;
4634 }
4635 if (occur == 0) {
4636 tmp = cur->next;
4637 while (tmp != NULL && occur == 0) {
4638 if (tmp->type == XML_COMMENT_NODE)
4639 occur++;
4640 tmp = tmp->next;
4641 }
4642 if (occur != 0)
4643 occur = 1;
4644 } else
4645 occur++;
4646 } else if ((cur->type == XML_TEXT_NODE) ||
4647 (cur->type == XML_CDATA_SECTION_NODE)) {
4648 sep = "/";
4649 name = "text()";
4650 next = cur->parent;
4651
4652 /*
4653 * Thumbler index computation
4654 */
4655 tmp = cur->prev;
4656 while (tmp != NULL) {
Kasimier T. Buchcikeb468702006-02-15 10:57:50 +00004657 if ((tmp->type == XML_TEXT_NODE) ||
4658 (tmp->type == XML_CDATA_SECTION_NODE))
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004659 occur++;
4660 tmp = tmp->prev;
4661 }
Kasimier T. Buchcikeb468702006-02-15 10:57:50 +00004662 /*
4663 * Evaluate if this is the only text- or CDATA-section-node;
4664 * if yes, then we'll get "text()", otherwise "text()[1]".
4665 */
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004666 if (occur == 0) {
4667 tmp = cur->next;
Kasimier T. Buchcikeb468702006-02-15 10:57:50 +00004668 while (tmp != NULL) {
4669 if ((tmp->type == XML_TEXT_NODE) ||
4670 (tmp->type == XML_CDATA_SECTION_NODE))
4671 {
4672 occur = 1;
4673 break;
Daniel Veillardaa6de472008-08-25 14:53:31 +00004674 }
Kasimier T. Buchcikeb468702006-02-15 10:57:50 +00004675 tmp = tmp->next;
4676 }
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004677 } else
4678 occur++;
4679 } else if (cur->type == XML_PI_NODE) {
4680 sep = "/";
4681 snprintf(nametemp, sizeof(nametemp) - 1,
William M. Brack13dfa872004-09-18 04:52:08 +00004682 "processing-instruction('%s')", (char *)cur->name);
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004683 nametemp[sizeof(nametemp) - 1] = 0;
4684 name = nametemp;
4685
4686 next = cur->parent;
4687
4688 /*
4689 * Thumbler index computation
4690 */
4691 tmp = cur->prev;
4692 while (tmp != NULL) {
4693 if ((tmp->type == XML_PI_NODE) &&
4694 (xmlStrEqual(cur->name, tmp->name)))
4695 occur++;
4696 tmp = tmp->prev;
4697 }
4698 if (occur == 0) {
4699 tmp = cur->next;
4700 while (tmp != NULL && occur == 0) {
4701 if ((tmp->type == XML_PI_NODE) &&
4702 (xmlStrEqual(cur->name, tmp->name)))
4703 occur++;
4704 tmp = tmp->next;
4705 }
4706 if (occur != 0)
4707 occur = 1;
4708 } else
4709 occur++;
4710
Daniel Veillard8faa7832001-11-26 15:58:08 +00004711 } else if (cur->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004712 sep = "/@";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004713 name = (const char *) (((xmlAttrPtr) cur)->name);
Daniel Veillard365c8062005-07-19 11:31:55 +00004714 if (cur->ns) {
4715 if (cur->ns->prefix != NULL)
4716 snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
Daniel Veillardaa6de472008-08-25 14:53:31 +00004717 (char *)cur->ns->prefix, (char *)cur->name);
Daniel Veillard365c8062005-07-19 11:31:55 +00004718 else
4719 snprintf(nametemp, sizeof(nametemp) - 1, "%s",
Daniel Veillardaa6de472008-08-25 14:53:31 +00004720 (char *)cur->name);
Daniel Veillard365c8062005-07-19 11:31:55 +00004721 nametemp[sizeof(nametemp) - 1] = 0;
4722 name = nametemp;
4723 }
Daniel Veillard8faa7832001-11-26 15:58:08 +00004724 next = ((xmlAttrPtr) cur)->parent;
4725 } else {
4726 next = cur->parent;
4727 }
4728
4729 /*
4730 * Make sure there is enough room
4731 */
4732 if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) {
4733 buf_len =
4734 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20;
4735 temp = (xmlChar *) xmlRealloc(buffer, buf_len);
4736 if (temp == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004737 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004738 xmlFree(buf);
4739 xmlFree(buffer);
4740 return (NULL);
4741 }
4742 buffer = temp;
4743 temp = (xmlChar *) xmlRealloc(buf, buf_len);
4744 if (temp == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004745 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004746 xmlFree(buf);
4747 xmlFree(buffer);
4748 return (NULL);
4749 }
4750 buf = temp;
4751 }
4752 if (occur == 0)
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004753 snprintf((char *) buf, buf_len, "%s%s%s",
Daniel Veillard8faa7832001-11-26 15:58:08 +00004754 sep, name, (char *) buffer);
4755 else
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004756 snprintf((char *) buf, buf_len, "%s%s[%d]%s",
Daniel Veillard8faa7832001-11-26 15:58:08 +00004757 sep, name, occur, (char *) buffer);
William M. Brack13dfa872004-09-18 04:52:08 +00004758 snprintf((char *) buffer, buf_len, "%s", (char *)buf);
Daniel Veillard8faa7832001-11-26 15:58:08 +00004759 cur = next;
4760 } while (cur != NULL);
4761 xmlFree(buf);
4762 return (buffer);
4763}
Daniel Veillard652327a2003-09-29 18:02:38 +00004764#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard8faa7832001-11-26 15:58:08 +00004765
4766/**
Owen Taylor3473f882001-02-23 17:55:21 +00004767 * xmlDocGetRootElement:
4768 * @doc: the document
4769 *
4770 * Get the root element of the document (doc->children is a list
4771 * containing possibly comments, PIs, etc ...).
4772 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004773 * Returns the #xmlNodePtr for the root or NULL
Owen Taylor3473f882001-02-23 17:55:21 +00004774 */
4775xmlNodePtr
4776xmlDocGetRootElement(xmlDocPtr doc) {
4777 xmlNodePtr ret;
4778
4779 if (doc == NULL) return(NULL);
4780 ret = doc->children;
4781 while (ret != NULL) {
4782 if (ret->type == XML_ELEMENT_NODE)
4783 return(ret);
4784 ret = ret->next;
4785 }
4786 return(ret);
4787}
Daniel Veillardaa6de472008-08-25 14:53:31 +00004788
Daniel Veillard2156d432004-03-04 15:59:36 +00004789#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004790/**
4791 * xmlDocSetRootElement:
4792 * @doc: the document
Daniel Veillard26a45c82006-10-20 12:55:34 +00004793 * @root: the new document root element, if root is NULL no action is taken,
4794 * to remove a node from a document use xmlUnlinkNode(root) instead.
Owen Taylor3473f882001-02-23 17:55:21 +00004795 *
4796 * Set the root element of the document (doc->children is a list
4797 * containing possibly comments, PIs, etc ...).
4798 *
Daniel Veillard26a45c82006-10-20 12:55:34 +00004799 * Returns the old root element if any was found, NULL if root was NULL
Owen Taylor3473f882001-02-23 17:55:21 +00004800 */
4801xmlNodePtr
4802xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) {
4803 xmlNodePtr old = NULL;
4804
4805 if (doc == NULL) return(NULL);
Daniel Veillardc575b992002-02-08 13:28:40 +00004806 if (root == NULL)
4807 return(NULL);
4808 xmlUnlinkNode(root);
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00004809 xmlSetTreeDoc(root, doc);
Daniel Veillardc575b992002-02-08 13:28:40 +00004810 root->parent = (xmlNodePtr) doc;
Owen Taylor3473f882001-02-23 17:55:21 +00004811 old = doc->children;
4812 while (old != NULL) {
4813 if (old->type == XML_ELEMENT_NODE)
4814 break;
4815 old = old->next;
4816 }
4817 if (old == NULL) {
4818 if (doc->children == NULL) {
4819 doc->children = root;
4820 doc->last = root;
4821 } else {
4822 xmlAddSibling(doc->children, root);
4823 }
4824 } else {
4825 xmlReplaceNode(old, root);
4826 }
4827 return(old);
4828}
Daniel Veillard2156d432004-03-04 15:59:36 +00004829#endif
Daniel Veillardaa6de472008-08-25 14:53:31 +00004830
Daniel Veillard2156d432004-03-04 15:59:36 +00004831#if defined(LIBXML_TREE_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004832/**
4833 * xmlNodeSetLang:
4834 * @cur: the node being changed
Daniel Veillardd1640922001-12-17 15:30:10 +00004835 * @lang: the language description
Owen Taylor3473f882001-02-23 17:55:21 +00004836 *
4837 * Set the language of a node, i.e. the values of the xml:lang
4838 * attribute.
4839 */
4840void
4841xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004842 xmlNsPtr ns;
4843
Owen Taylor3473f882001-02-23 17:55:21 +00004844 if (cur == NULL) return;
4845 switch(cur->type) {
4846 case XML_TEXT_NODE:
4847 case XML_CDATA_SECTION_NODE:
4848 case XML_COMMENT_NODE:
4849 case XML_DOCUMENT_NODE:
4850 case XML_DOCUMENT_TYPE_NODE:
4851 case XML_DOCUMENT_FRAG_NODE:
4852 case XML_NOTATION_NODE:
4853 case XML_HTML_DOCUMENT_NODE:
4854 case XML_DTD_NODE:
4855 case XML_ELEMENT_DECL:
4856 case XML_ATTRIBUTE_DECL:
4857 case XML_ENTITY_DECL:
4858 case XML_PI_NODE:
4859 case XML_ENTITY_REF_NODE:
4860 case XML_ENTITY_NODE:
4861 case XML_NAMESPACE_DECL:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004862#ifdef LIBXML_DOCB_ENABLED
4863 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004864#endif
4865 case XML_XINCLUDE_START:
4866 case XML_XINCLUDE_END:
4867 return;
4868 case XML_ELEMENT_NODE:
4869 case XML_ATTRIBUTE_NODE:
4870 break;
4871 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004872 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4873 if (ns == NULL)
4874 return;
4875 xmlSetNsProp(cur, ns, BAD_CAST "lang", lang);
Owen Taylor3473f882001-02-23 17:55:21 +00004876}
Daniel Veillard652327a2003-09-29 18:02:38 +00004877#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardaa6de472008-08-25 14:53:31 +00004878
Owen Taylor3473f882001-02-23 17:55:21 +00004879/**
4880 * xmlNodeGetLang:
4881 * @cur: the node being checked
4882 *
4883 * Searches the language of a node, i.e. the values of the xml:lang
4884 * attribute or the one carried by the nearest ancestor.
4885 *
4886 * Returns a pointer to the lang value, or NULL if not found
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004887 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00004888 */
4889xmlChar *
4890xmlNodeGetLang(xmlNodePtr cur) {
4891 xmlChar *lang;
4892
4893 while (cur != NULL) {
Daniel Veillardc17337c2001-05-09 10:51:31 +00004894 lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE);
Owen Taylor3473f882001-02-23 17:55:21 +00004895 if (lang != NULL)
4896 return(lang);
4897 cur = cur->parent;
4898 }
4899 return(NULL);
4900}
Daniel Veillardaa6de472008-08-25 14:53:31 +00004901
Owen Taylor3473f882001-02-23 17:55:21 +00004902
Daniel Veillard652327a2003-09-29 18:02:38 +00004903#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00004904/**
4905 * xmlNodeSetSpacePreserve:
4906 * @cur: the node being changed
4907 * @val: the xml:space value ("0": default, 1: "preserve")
4908 *
4909 * Set (or reset) the space preserving behaviour of a node, i.e. the
4910 * value of the xml:space attribute.
4911 */
4912void
4913xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004914 xmlNsPtr ns;
4915
Owen Taylor3473f882001-02-23 17:55:21 +00004916 if (cur == NULL) return;
4917 switch(cur->type) {
4918 case XML_TEXT_NODE:
4919 case XML_CDATA_SECTION_NODE:
4920 case XML_COMMENT_NODE:
4921 case XML_DOCUMENT_NODE:
4922 case XML_DOCUMENT_TYPE_NODE:
4923 case XML_DOCUMENT_FRAG_NODE:
4924 case XML_NOTATION_NODE:
4925 case XML_HTML_DOCUMENT_NODE:
4926 case XML_DTD_NODE:
4927 case XML_ELEMENT_DECL:
4928 case XML_ATTRIBUTE_DECL:
4929 case XML_ENTITY_DECL:
4930 case XML_PI_NODE:
4931 case XML_ENTITY_REF_NODE:
4932 case XML_ENTITY_NODE:
4933 case XML_NAMESPACE_DECL:
4934 case XML_XINCLUDE_START:
4935 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004936#ifdef LIBXML_DOCB_ENABLED
4937 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004938#endif
4939 return;
4940 case XML_ELEMENT_NODE:
4941 case XML_ATTRIBUTE_NODE:
4942 break;
4943 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004944 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4945 if (ns == NULL)
4946 return;
Owen Taylor3473f882001-02-23 17:55:21 +00004947 switch (val) {
4948 case 0:
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004949 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "default");
Owen Taylor3473f882001-02-23 17:55:21 +00004950 break;
4951 case 1:
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004952 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "preserve");
Owen Taylor3473f882001-02-23 17:55:21 +00004953 break;
4954 }
4955}
Daniel Veillard652327a2003-09-29 18:02:38 +00004956#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004957
4958/**
4959 * xmlNodeGetSpacePreserve:
4960 * @cur: the node being checked
4961 *
4962 * Searches the space preserving behaviour of a node, i.e. the values
4963 * of the xml:space attribute or the one carried by the nearest
4964 * ancestor.
4965 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004966 * Returns -1 if xml:space is not inherited, 0 if "default", 1 if "preserve"
Owen Taylor3473f882001-02-23 17:55:21 +00004967 */
4968int
4969xmlNodeGetSpacePreserve(xmlNodePtr cur) {
4970 xmlChar *space;
4971
4972 while (cur != NULL) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004973 space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE);
Owen Taylor3473f882001-02-23 17:55:21 +00004974 if (space != NULL) {
4975 if (xmlStrEqual(space, BAD_CAST "preserve")) {
4976 xmlFree(space);
4977 return(1);
4978 }
4979 if (xmlStrEqual(space, BAD_CAST "default")) {
4980 xmlFree(space);
4981 return(0);
4982 }
4983 xmlFree(space);
4984 }
4985 cur = cur->parent;
4986 }
4987 return(-1);
4988}
Daniel Veillardaa6de472008-08-25 14:53:31 +00004989
Daniel Veillard652327a2003-09-29 18:02:38 +00004990#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00004991/**
4992 * xmlNodeSetName:
4993 * @cur: the node being changed
4994 * @name: the new tag name
4995 *
4996 * Set (or reset) the name of a node.
4997 */
4998void
4999xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) {
Daniel Veillardce244ad2004-11-05 10:03:46 +00005000 xmlDocPtr doc;
5001 xmlDictPtr dict;
5002
Owen Taylor3473f882001-02-23 17:55:21 +00005003 if (cur == NULL) return;
5004 if (name == NULL) return;
5005 switch(cur->type) {
5006 case XML_TEXT_NODE:
5007 case XML_CDATA_SECTION_NODE:
5008 case XML_COMMENT_NODE:
5009 case XML_DOCUMENT_TYPE_NODE:
5010 case XML_DOCUMENT_FRAG_NODE:
5011 case XML_NOTATION_NODE:
5012 case XML_HTML_DOCUMENT_NODE:
5013 case XML_NAMESPACE_DECL:
5014 case XML_XINCLUDE_START:
5015 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005016#ifdef LIBXML_DOCB_ENABLED
5017 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005018#endif
5019 return;
5020 case XML_ELEMENT_NODE:
5021 case XML_ATTRIBUTE_NODE:
5022 case XML_PI_NODE:
5023 case XML_ENTITY_REF_NODE:
5024 case XML_ENTITY_NODE:
5025 case XML_DTD_NODE:
5026 case XML_DOCUMENT_NODE:
5027 case XML_ELEMENT_DECL:
5028 case XML_ATTRIBUTE_DECL:
5029 case XML_ENTITY_DECL:
5030 break;
5031 }
Daniel Veillardce244ad2004-11-05 10:03:46 +00005032 doc = cur->doc;
5033 if (doc != NULL)
5034 dict = doc->dict;
5035 else
5036 dict = NULL;
5037 if (dict != NULL) {
5038 if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name)))
5039 xmlFree((xmlChar *) cur->name);
5040 cur->name = xmlDictLookup(dict, name, -1);
5041 } else {
5042 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
5043 cur->name = xmlStrdup(name);
5044 }
Owen Taylor3473f882001-02-23 17:55:21 +00005045}
Daniel Veillard2156d432004-03-04 15:59:36 +00005046#endif
Daniel Veillardaa6de472008-08-25 14:53:31 +00005047
Daniel Veillard2156d432004-03-04 15:59:36 +00005048#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00005049/**
5050 * xmlNodeSetBase:
5051 * @cur: the node being changed
5052 * @uri: the new base URI
5053 *
5054 * Set (or reset) the base URI of a node, i.e. the value of the
5055 * xml:base attribute.
5056 */
5057void
Daniel Veillardf85ce8e2003-09-22 10:24:45 +00005058xmlNodeSetBase(xmlNodePtr cur, const xmlChar* uri) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005059 xmlNsPtr ns;
Daniel Veillardb8efdda2006-10-10 12:37:14 +00005060 const xmlChar* fixed;
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005061
Owen Taylor3473f882001-02-23 17:55:21 +00005062 if (cur == NULL) return;
5063 switch(cur->type) {
5064 case XML_TEXT_NODE:
5065 case XML_CDATA_SECTION_NODE:
5066 case XML_COMMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005067 case XML_DOCUMENT_TYPE_NODE:
5068 case XML_DOCUMENT_FRAG_NODE:
5069 case XML_NOTATION_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005070 case XML_DTD_NODE:
5071 case XML_ELEMENT_DECL:
5072 case XML_ATTRIBUTE_DECL:
5073 case XML_ENTITY_DECL:
5074 case XML_PI_NODE:
5075 case XML_ENTITY_REF_NODE:
5076 case XML_ENTITY_NODE:
5077 case XML_NAMESPACE_DECL:
5078 case XML_XINCLUDE_START:
5079 case XML_XINCLUDE_END:
Owen Taylor3473f882001-02-23 17:55:21 +00005080 return;
5081 case XML_ELEMENT_NODE:
5082 case XML_ATTRIBUTE_NODE:
5083 break;
Daniel Veillard4cbe4702002-05-05 06:57:27 +00005084 case XML_DOCUMENT_NODE:
5085#ifdef LIBXML_DOCB_ENABLED
5086 case XML_DOCB_DOCUMENT_NODE:
5087#endif
5088 case XML_HTML_DOCUMENT_NODE: {
5089 xmlDocPtr doc = (xmlDocPtr) cur;
5090
5091 if (doc->URL != NULL)
5092 xmlFree((xmlChar *) doc->URL);
5093 if (uri == NULL)
5094 doc->URL = NULL;
5095 else
Daniel Veillardb8efdda2006-10-10 12:37:14 +00005096 doc->URL = xmlPathToURI(uri);
Daniel Veillard4cbe4702002-05-05 06:57:27 +00005097 return;
5098 }
Owen Taylor3473f882001-02-23 17:55:21 +00005099 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00005100
Daniel Veillardcfa0d812002-01-17 08:46:58 +00005101 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
5102 if (ns == NULL)
5103 return;
Daniel Veillardb8efdda2006-10-10 12:37:14 +00005104 fixed = xmlPathToURI(uri);
5105 if (fixed != NULL) {
5106 xmlSetNsProp(cur, ns, BAD_CAST "base", fixed);
William M. Brack38d452a2007-05-22 16:00:06 +00005107 xmlFree((xmlChar *)fixed);
Daniel Veillardb8efdda2006-10-10 12:37:14 +00005108 } else {
5109 xmlSetNsProp(cur, ns, BAD_CAST "base", uri);
5110 }
Owen Taylor3473f882001-02-23 17:55:21 +00005111}
Daniel Veillard652327a2003-09-29 18:02:38 +00005112#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005113
5114/**
Owen Taylor3473f882001-02-23 17:55:21 +00005115 * xmlNodeGetBase:
5116 * @doc: the document the node pertains to
5117 * @cur: the node being checked
5118 *
5119 * Searches for the BASE URL. The code should work on both XML
5120 * and HTML document even if base mechanisms are completely different.
5121 * It returns the base as defined in RFC 2396 sections
5122 * 5.1.1. Base URI within Document Content
5123 * and
5124 * 5.1.2. Base URI from the Encapsulating Entity
5125 * However it does not return the document base (5.1.3), use
5126 * xmlDocumentGetBase() for this
5127 *
5128 * Returns a pointer to the base URL, or NULL if not found
Daniel Veillardbd9afb52002-09-25 22:25:35 +00005129 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00005130 */
5131xmlChar *
5132xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00005133 xmlChar *oldbase = NULL;
5134 xmlChar *base, *newbase;
Owen Taylor3473f882001-02-23 17:55:21 +00005135
Daniel Veillardaa6de472008-08-25 14:53:31 +00005136 if ((cur == NULL) && (doc == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00005137 return(NULL);
Daniel Veillardaa6de472008-08-25 14:53:31 +00005138 if (doc == NULL) doc = cur->doc;
Owen Taylor3473f882001-02-23 17:55:21 +00005139 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
5140 cur = doc->children;
5141 while ((cur != NULL) && (cur->name != NULL)) {
5142 if (cur->type != XML_ELEMENT_NODE) {
5143 cur = cur->next;
5144 continue;
5145 }
5146 if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) {
5147 cur = cur->children;
5148 continue;
5149 }
5150 if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) {
5151 cur = cur->children;
5152 continue;
5153 }
5154 if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) {
5155 return(xmlGetProp(cur, BAD_CAST "href"));
5156 }
5157 cur = cur->next;
5158 }
5159 return(NULL);
5160 }
5161 while (cur != NULL) {
5162 if (cur->type == XML_ENTITY_DECL) {
5163 xmlEntityPtr ent = (xmlEntityPtr) cur;
5164 return(xmlStrdup(ent->URI));
5165 }
Daniel Veillard42596ad2001-05-22 16:57:14 +00005166 if (cur->type == XML_ELEMENT_NODE) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00005167 base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE);
Daniel Veillard42596ad2001-05-22 16:57:14 +00005168 if (base != NULL) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00005169 if (oldbase != NULL) {
5170 newbase = xmlBuildURI(oldbase, base);
5171 if (newbase != NULL) {
5172 xmlFree(oldbase);
5173 xmlFree(base);
5174 oldbase = newbase;
5175 } else {
5176 xmlFree(oldbase);
5177 xmlFree(base);
5178 return(NULL);
5179 }
5180 } else {
5181 oldbase = base;
5182 }
5183 if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) ||
5184 (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) ||
5185 (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4)))
5186 return(oldbase);
Daniel Veillard42596ad2001-05-22 16:57:14 +00005187 }
5188 }
Owen Taylor3473f882001-02-23 17:55:21 +00005189 cur = cur->parent;
5190 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00005191 if ((doc != NULL) && (doc->URL != NULL)) {
5192 if (oldbase == NULL)
5193 return(xmlStrdup(doc->URL));
5194 newbase = xmlBuildURI(oldbase, doc->URL);
5195 xmlFree(oldbase);
5196 return(newbase);
5197 }
5198 return(oldbase);
Owen Taylor3473f882001-02-23 17:55:21 +00005199}
Daniel Veillardaa6de472008-08-25 14:53:31 +00005200
Owen Taylor3473f882001-02-23 17:55:21 +00005201/**
Daniel Veillard78697292003-10-19 20:44:43 +00005202 * xmlNodeBufGetContent:
5203 * @buffer: a buffer
5204 * @cur: the node being read
5205 *
5206 * Read the value of a node @cur, this can be either the text carried
5207 * directly by this node if it's a TEXT node or the aggregate string
5208 * of the values carried by this node child's (TEXT and ENTITY_REF).
5209 * Entity references are substituted.
5210 * Fills up the buffer @buffer with this value
Daniel Veillardaa6de472008-08-25 14:53:31 +00005211 *
Daniel Veillard78697292003-10-19 20:44:43 +00005212 * Returns 0 in case of success and -1 in case of error.
5213 */
5214int
5215xmlNodeBufGetContent(xmlBufferPtr buffer, xmlNodePtr cur)
5216{
5217 if ((cur == NULL) || (buffer == NULL)) return(-1);
5218 switch (cur->type) {
5219 case XML_CDATA_SECTION_NODE:
5220 case XML_TEXT_NODE:
5221 xmlBufferCat(buffer, cur->content);
5222 break;
5223 case XML_DOCUMENT_FRAG_NODE:
5224 case XML_ELEMENT_NODE:{
5225 xmlNodePtr tmp = cur;
5226
5227 while (tmp != NULL) {
5228 switch (tmp->type) {
5229 case XML_CDATA_SECTION_NODE:
5230 case XML_TEXT_NODE:
5231 if (tmp->content != NULL)
5232 xmlBufferCat(buffer, tmp->content);
5233 break;
5234 case XML_ENTITY_REF_NODE:
Rob Richards77b92ff2005-12-20 15:55:14 +00005235 xmlNodeBufGetContent(buffer, tmp);
5236 break;
Daniel Veillard78697292003-10-19 20:44:43 +00005237 default:
5238 break;
5239 }
5240 /*
5241 * Skip to next node
5242 */
5243 if (tmp->children != NULL) {
5244 if (tmp->children->type != XML_ENTITY_DECL) {
5245 tmp = tmp->children;
5246 continue;
5247 }
5248 }
5249 if (tmp == cur)
5250 break;
5251
5252 if (tmp->next != NULL) {
5253 tmp = tmp->next;
5254 continue;
5255 }
5256
5257 do {
5258 tmp = tmp->parent;
5259 if (tmp == NULL)
5260 break;
5261 if (tmp == cur) {
5262 tmp = NULL;
5263 break;
5264 }
5265 if (tmp->next != NULL) {
5266 tmp = tmp->next;
5267 break;
5268 }
5269 } while (tmp != NULL);
5270 }
5271 break;
5272 }
5273 case XML_ATTRIBUTE_NODE:{
5274 xmlAttrPtr attr = (xmlAttrPtr) cur;
5275 xmlNodePtr tmp = attr->children;
5276
5277 while (tmp != NULL) {
5278 if (tmp->type == XML_TEXT_NODE)
5279 xmlBufferCat(buffer, tmp->content);
5280 else
5281 xmlNodeBufGetContent(buffer, tmp);
5282 tmp = tmp->next;
5283 }
5284 break;
5285 }
5286 case XML_COMMENT_NODE:
5287 case XML_PI_NODE:
5288 xmlBufferCat(buffer, cur->content);
5289 break;
5290 case XML_ENTITY_REF_NODE:{
5291 xmlEntityPtr ent;
5292 xmlNodePtr tmp;
5293
5294 /* lookup entity declaration */
5295 ent = xmlGetDocEntity(cur->doc, cur->name);
5296 if (ent == NULL)
5297 return(-1);
5298
5299 /* an entity content can be any "well balanced chunk",
5300 * i.e. the result of the content [43] production:
5301 * http://www.w3.org/TR/REC-xml#NT-content
5302 * -> we iterate through child nodes and recursive call
5303 * xmlNodeGetContent() which handles all possible node types */
5304 tmp = ent->children;
5305 while (tmp) {
5306 xmlNodeBufGetContent(buffer, tmp);
5307 tmp = tmp->next;
5308 }
5309 break;
5310 }
5311 case XML_ENTITY_NODE:
5312 case XML_DOCUMENT_TYPE_NODE:
5313 case XML_NOTATION_NODE:
5314 case XML_DTD_NODE:
5315 case XML_XINCLUDE_START:
5316 case XML_XINCLUDE_END:
5317 break;
5318 case XML_DOCUMENT_NODE:
5319#ifdef LIBXML_DOCB_ENABLED
5320 case XML_DOCB_DOCUMENT_NODE:
5321#endif
5322 case XML_HTML_DOCUMENT_NODE:
5323 cur = cur->children;
5324 while (cur!= NULL) {
5325 if ((cur->type == XML_ELEMENT_NODE) ||
5326 (cur->type == XML_TEXT_NODE) ||
5327 (cur->type == XML_CDATA_SECTION_NODE)) {
5328 xmlNodeBufGetContent(buffer, cur);
5329 }
5330 cur = cur->next;
5331 }
5332 break;
5333 case XML_NAMESPACE_DECL:
5334 xmlBufferCat(buffer, ((xmlNsPtr) cur)->href);
5335 break;
5336 case XML_ELEMENT_DECL:
5337 case XML_ATTRIBUTE_DECL:
5338 case XML_ENTITY_DECL:
5339 break;
5340 }
5341 return(0);
5342}
5343/**
Owen Taylor3473f882001-02-23 17:55:21 +00005344 * xmlNodeGetContent:
5345 * @cur: the node being read
5346 *
5347 * Read the value of a node, this can be either the text carried
5348 * directly by this node if it's a TEXT node or the aggregate string
5349 * of the values carried by this node child's (TEXT and ENTITY_REF).
Daniel Veillardd1640922001-12-17 15:30:10 +00005350 * Entity references are substituted.
5351 * Returns a new #xmlChar * or NULL if no content is available.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00005352 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00005353 */
5354xmlChar *
Daniel Veillard7646b182002-04-20 06:41:40 +00005355xmlNodeGetContent(xmlNodePtr cur)
5356{
5357 if (cur == NULL)
5358 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005359 switch (cur->type) {
5360 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005361 case XML_ELEMENT_NODE:{
Daniel Veillard7646b182002-04-20 06:41:40 +00005362 xmlBufferPtr buffer;
5363 xmlChar *ret;
Owen Taylor3473f882001-02-23 17:55:21 +00005364
Daniel Veillard814a76d2003-01-23 18:24:20 +00005365 buffer = xmlBufferCreateSize(64);
Daniel Veillard7646b182002-04-20 06:41:40 +00005366 if (buffer == NULL)
5367 return (NULL);
Daniel Veillardc4696922003-10-19 21:47:14 +00005368 xmlNodeBufGetContent(buffer, cur);
Daniel Veillard7646b182002-04-20 06:41:40 +00005369 ret = buffer->content;
5370 buffer->content = NULL;
5371 xmlBufferFree(buffer);
5372 return (ret);
5373 }
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005374 case XML_ATTRIBUTE_NODE:
5375 return(xmlGetPropNodeValueInternal((xmlAttrPtr) cur));
Owen Taylor3473f882001-02-23 17:55:21 +00005376 case XML_COMMENT_NODE:
5377 case XML_PI_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005378 if (cur->content != NULL)
5379 return (xmlStrdup(cur->content));
5380 return (NULL);
5381 case XML_ENTITY_REF_NODE:{
5382 xmlEntityPtr ent;
Daniel Veillard7646b182002-04-20 06:41:40 +00005383 xmlBufferPtr buffer;
5384 xmlChar *ret;
5385
5386 /* lookup entity declaration */
5387 ent = xmlGetDocEntity(cur->doc, cur->name);
5388 if (ent == NULL)
5389 return (NULL);
5390
5391 buffer = xmlBufferCreate();
5392 if (buffer == NULL)
5393 return (NULL);
5394
Daniel Veillardc4696922003-10-19 21:47:14 +00005395 xmlNodeBufGetContent(buffer, cur);
Daniel Veillard7646b182002-04-20 06:41:40 +00005396
5397 ret = buffer->content;
5398 buffer->content = NULL;
5399 xmlBufferFree(buffer);
5400 return (ret);
5401 }
Owen Taylor3473f882001-02-23 17:55:21 +00005402 case XML_ENTITY_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005403 case XML_DOCUMENT_TYPE_NODE:
5404 case XML_NOTATION_NODE:
5405 case XML_DTD_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005406 case XML_XINCLUDE_START:
5407 case XML_XINCLUDE_END:
Daniel Veillard9adc0462003-03-24 18:39:54 +00005408 return (NULL);
5409 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005410#ifdef LIBXML_DOCB_ENABLED
Daniel Veillard7646b182002-04-20 06:41:40 +00005411 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005412#endif
Daniel Veillard9adc0462003-03-24 18:39:54 +00005413 case XML_HTML_DOCUMENT_NODE: {
Daniel Veillardc4696922003-10-19 21:47:14 +00005414 xmlBufferPtr buffer;
5415 xmlChar *ret;
Daniel Veillard9adc0462003-03-24 18:39:54 +00005416
Daniel Veillardc4696922003-10-19 21:47:14 +00005417 buffer = xmlBufferCreate();
5418 if (buffer == NULL)
5419 return (NULL);
5420
5421 xmlNodeBufGetContent(buffer, (xmlNodePtr) cur);
5422
5423 ret = buffer->content;
5424 buffer->content = NULL;
5425 xmlBufferFree(buffer);
5426 return (ret);
Daniel Veillard9adc0462003-03-24 18:39:54 +00005427 }
Daniel Veillard96c3a3b2002-10-14 15:39:04 +00005428 case XML_NAMESPACE_DECL: {
5429 xmlChar *tmp;
5430
5431 tmp = xmlStrdup(((xmlNsPtr) cur)->href);
5432 return (tmp);
5433 }
Owen Taylor3473f882001-02-23 17:55:21 +00005434 case XML_ELEMENT_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00005435 /* TODO !!! */
5436 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005437 case XML_ATTRIBUTE_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00005438 /* TODO !!! */
5439 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005440 case XML_ENTITY_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00005441 /* TODO !!! */
5442 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005443 case XML_CDATA_SECTION_NODE:
5444 case XML_TEXT_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005445 if (cur->content != NULL)
5446 return (xmlStrdup(cur->content));
5447 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005448 }
Daniel Veillard7646b182002-04-20 06:41:40 +00005449 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005450}
Daniel Veillard652327a2003-09-29 18:02:38 +00005451
Owen Taylor3473f882001-02-23 17:55:21 +00005452/**
5453 * xmlNodeSetContent:
5454 * @cur: the node being modified
5455 * @content: the new value of the content
5456 *
5457 * Replace the content of a node.
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00005458 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
5459 * references, but XML special chars need to be escaped first by using
5460 * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars().
Owen Taylor3473f882001-02-23 17:55:21 +00005461 */
5462void
5463xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) {
5464 if (cur == NULL) {
5465#ifdef DEBUG_TREE
5466 xmlGenericError(xmlGenericErrorContext,
5467 "xmlNodeSetContent : node == NULL\n");
5468#endif
5469 return;
5470 }
5471 switch (cur->type) {
5472 case XML_DOCUMENT_FRAG_NODE:
5473 case XML_ELEMENT_NODE:
Daniel Veillard2c748c62002-01-16 15:37:50 +00005474 case XML_ATTRIBUTE_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005475 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5476 cur->children = xmlStringGetNodeList(cur->doc, content);
5477 UPDATE_LAST_CHILD_AND_PARENT(cur)
5478 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005479 case XML_TEXT_NODE:
5480 case XML_CDATA_SECTION_NODE:
5481 case XML_ENTITY_REF_NODE:
5482 case XML_ENTITY_NODE:
5483 case XML_PI_NODE:
5484 case XML_COMMENT_NODE:
Daniel Veillard8874b942005-08-25 13:19:21 +00005485 if ((cur->content != NULL) &&
5486 (cur->content != (xmlChar *) &(cur->properties))) {
William M. Brack7762bb12004-01-04 14:49:01 +00005487 if (!((cur->doc != NULL) && (cur->doc->dict != NULL) &&
Daniel Veillardc587bce2005-05-10 15:28:08 +00005488 (xmlDictOwns(cur->doc->dict, cur->content))))
William M. Brack7762bb12004-01-04 14:49:01 +00005489 xmlFree(cur->content);
Daniel Veillardaa6de472008-08-25 14:53:31 +00005490 }
Owen Taylor3473f882001-02-23 17:55:21 +00005491 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5492 cur->last = cur->children = NULL;
5493 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005494 cur->content = xmlStrdup(content);
Daniel Veillardaa6de472008-08-25 14:53:31 +00005495 } else
Owen Taylor3473f882001-02-23 17:55:21 +00005496 cur->content = NULL;
Daniel Veillard8874b942005-08-25 13:19:21 +00005497 cur->properties = NULL;
5498 cur->nsDef = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00005499 break;
5500 case XML_DOCUMENT_NODE:
5501 case XML_HTML_DOCUMENT_NODE:
5502 case XML_DOCUMENT_TYPE_NODE:
5503 case XML_XINCLUDE_START:
5504 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005505#ifdef LIBXML_DOCB_ENABLED
5506 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005507#endif
5508 break;
5509 case XML_NOTATION_NODE:
5510 break;
5511 case XML_DTD_NODE:
5512 break;
5513 case XML_NAMESPACE_DECL:
5514 break;
5515 case XML_ELEMENT_DECL:
5516 /* TODO !!! */
5517 break;
5518 case XML_ATTRIBUTE_DECL:
5519 /* TODO !!! */
5520 break;
5521 case XML_ENTITY_DECL:
5522 /* TODO !!! */
5523 break;
5524 }
5525}
5526
Daniel Veillard652327a2003-09-29 18:02:38 +00005527#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00005528/**
5529 * xmlNodeSetContentLen:
5530 * @cur: the node being modified
5531 * @content: the new value of the content
5532 * @len: the size of @content
5533 *
5534 * Replace the content of a node.
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00005535 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
5536 * references, but XML special chars need to be escaped first by using
5537 * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars().
Owen Taylor3473f882001-02-23 17:55:21 +00005538 */
5539void
5540xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5541 if (cur == NULL) {
5542#ifdef DEBUG_TREE
5543 xmlGenericError(xmlGenericErrorContext,
5544 "xmlNodeSetContentLen : node == NULL\n");
5545#endif
5546 return;
5547 }
5548 switch (cur->type) {
5549 case XML_DOCUMENT_FRAG_NODE:
5550 case XML_ELEMENT_NODE:
Daniel Veillard2c748c62002-01-16 15:37:50 +00005551 case XML_ATTRIBUTE_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005552 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5553 cur->children = xmlStringLenGetNodeList(cur->doc, content, len);
5554 UPDATE_LAST_CHILD_AND_PARENT(cur)
5555 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005556 case XML_TEXT_NODE:
5557 case XML_CDATA_SECTION_NODE:
5558 case XML_ENTITY_REF_NODE:
5559 case XML_ENTITY_NODE:
5560 case XML_PI_NODE:
5561 case XML_COMMENT_NODE:
5562 case XML_NOTATION_NODE:
Daniel Veillard8874b942005-08-25 13:19:21 +00005563 if ((cur->content != NULL) &&
5564 (cur->content != (xmlChar *) &(cur->properties))) {
5565 if (!((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5566 (xmlDictOwns(cur->doc->dict, cur->content))))
5567 xmlFree(cur->content);
Daniel Veillardaa6de472008-08-25 14:53:31 +00005568 }
Owen Taylor3473f882001-02-23 17:55:21 +00005569 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5570 cur->children = cur->last = NULL;
5571 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005572 cur->content = xmlStrndup(content, len);
Daniel Veillardaa6de472008-08-25 14:53:31 +00005573 } else
Owen Taylor3473f882001-02-23 17:55:21 +00005574 cur->content = NULL;
Daniel Veillard8874b942005-08-25 13:19:21 +00005575 cur->properties = NULL;
5576 cur->nsDef = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00005577 break;
5578 case XML_DOCUMENT_NODE:
5579 case XML_DTD_NODE:
5580 case XML_HTML_DOCUMENT_NODE:
5581 case XML_DOCUMENT_TYPE_NODE:
5582 case XML_NAMESPACE_DECL:
5583 case XML_XINCLUDE_START:
5584 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005585#ifdef LIBXML_DOCB_ENABLED
5586 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005587#endif
5588 break;
5589 case XML_ELEMENT_DECL:
5590 /* TODO !!! */
5591 break;
5592 case XML_ATTRIBUTE_DECL:
5593 /* TODO !!! */
5594 break;
5595 case XML_ENTITY_DECL:
5596 /* TODO !!! */
5597 break;
5598 }
5599}
Daniel Veillard652327a2003-09-29 18:02:38 +00005600#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005601
5602/**
5603 * xmlNodeAddContentLen:
5604 * @cur: the node being modified
5605 * @content: extra content
5606 * @len: the size of @content
Daniel Veillardaa6de472008-08-25 14:53:31 +00005607 *
Owen Taylor3473f882001-02-23 17:55:21 +00005608 * Append the extra substring to the node content.
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00005609 * NOTE: In contrast to xmlNodeSetContentLen(), @content is supposed to be
5610 * raw text, so unescaped XML special chars are allowed, entity
5611 * references are not supported.
Owen Taylor3473f882001-02-23 17:55:21 +00005612 */
5613void
5614xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5615 if (cur == NULL) {
5616#ifdef DEBUG_TREE
5617 xmlGenericError(xmlGenericErrorContext,
5618 "xmlNodeAddContentLen : node == NULL\n");
5619#endif
5620 return;
5621 }
5622 if (len <= 0) return;
5623 switch (cur->type) {
5624 case XML_DOCUMENT_FRAG_NODE:
5625 case XML_ELEMENT_NODE: {
Daniel Veillardacb2bda2002-01-13 16:15:43 +00005626 xmlNodePtr last, newNode, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00005627
Daniel Veillard7db37732001-07-12 01:20:08 +00005628 last = cur->last;
Owen Taylor3473f882001-02-23 17:55:21 +00005629 newNode = xmlNewTextLen(content, len);
5630 if (newNode != NULL) {
Daniel Veillardacb2bda2002-01-13 16:15:43 +00005631 tmp = xmlAddChild(cur, newNode);
5632 if (tmp != newNode)
5633 return;
Owen Taylor3473f882001-02-23 17:55:21 +00005634 if ((last != NULL) && (last->next == newNode)) {
5635 xmlTextMerge(last, newNode);
5636 }
5637 }
5638 break;
5639 }
5640 case XML_ATTRIBUTE_NODE:
5641 break;
5642 case XML_TEXT_NODE:
5643 case XML_CDATA_SECTION_NODE:
5644 case XML_ENTITY_REF_NODE:
5645 case XML_ENTITY_NODE:
5646 case XML_PI_NODE:
5647 case XML_COMMENT_NODE:
5648 case XML_NOTATION_NODE:
5649 if (content != NULL) {
Daniel Veillard8874b942005-08-25 13:19:21 +00005650 if ((cur->content == (xmlChar *) &(cur->properties)) ||
5651 ((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5652 xmlDictOwns(cur->doc->dict, cur->content))) {
5653 cur->content = xmlStrncatNew(cur->content, content, len);
5654 cur->properties = NULL;
5655 cur->nsDef = NULL;
William M. Brack7762bb12004-01-04 14:49:01 +00005656 break;
5657 }
Owen Taylor3473f882001-02-23 17:55:21 +00005658 cur->content = xmlStrncat(cur->content, content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00005659 }
5660 case XML_DOCUMENT_NODE:
5661 case XML_DTD_NODE:
5662 case XML_HTML_DOCUMENT_NODE:
5663 case XML_DOCUMENT_TYPE_NODE:
5664 case XML_NAMESPACE_DECL:
5665 case XML_XINCLUDE_START:
5666 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005667#ifdef LIBXML_DOCB_ENABLED
5668 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005669#endif
5670 break;
5671 case XML_ELEMENT_DECL:
5672 case XML_ATTRIBUTE_DECL:
5673 case XML_ENTITY_DECL:
5674 break;
5675 }
5676}
5677
5678/**
5679 * xmlNodeAddContent:
5680 * @cur: the node being modified
5681 * @content: extra content
Daniel Veillardaa6de472008-08-25 14:53:31 +00005682 *
Owen Taylor3473f882001-02-23 17:55:21 +00005683 * Append the extra substring to the node content.
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00005684 * NOTE: In contrast to xmlNodeSetContent(), @content is supposed to be
5685 * raw text, so unescaped XML special chars are allowed, entity
5686 * references are not supported.
Owen Taylor3473f882001-02-23 17:55:21 +00005687 */
5688void
5689xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) {
5690 int len;
5691
5692 if (cur == NULL) {
5693#ifdef DEBUG_TREE
5694 xmlGenericError(xmlGenericErrorContext,
5695 "xmlNodeAddContent : node == NULL\n");
5696#endif
5697 return;
5698 }
5699 if (content == NULL) return;
5700 len = xmlStrlen(content);
5701 xmlNodeAddContentLen(cur, content, len);
5702}
5703
5704/**
5705 * xmlTextMerge:
5706 * @first: the first text node
5707 * @second: the second text node being merged
Daniel Veillardaa6de472008-08-25 14:53:31 +00005708 *
Owen Taylor3473f882001-02-23 17:55:21 +00005709 * Merge two text nodes into one
5710 * Returns the first text node augmented
5711 */
5712xmlNodePtr
5713xmlTextMerge(xmlNodePtr first, xmlNodePtr second) {
5714 if (first == NULL) return(second);
5715 if (second == NULL) return(first);
5716 if (first->type != XML_TEXT_NODE) return(first);
5717 if (second->type != XML_TEXT_NODE) return(first);
5718 if (second->name != first->name)
5719 return(first);
Owen Taylor3473f882001-02-23 17:55:21 +00005720 xmlNodeAddContent(first, second->content);
Owen Taylor3473f882001-02-23 17:55:21 +00005721 xmlUnlinkNode(second);
5722 xmlFreeNode(second);
5723 return(first);
5724}
5725
Daniel Veillardf1a27c62006-10-13 22:33:03 +00005726#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00005727/**
5728 * xmlGetNsList:
5729 * @doc: the document
5730 * @node: the current node
5731 *
5732 * Search all the namespace applying to a given element.
Daniel Veillardd1640922001-12-17 15:30:10 +00005733 * Returns an NULL terminated array of all the #xmlNsPtr found
Owen Taylor3473f882001-02-23 17:55:21 +00005734 * that need to be freed by the caller or NULL if no
5735 * namespace if defined
5736 */
5737xmlNsPtr *
Daniel Veillard77044732001-06-29 21:31:07 +00005738xmlGetNsList(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node)
5739{
Owen Taylor3473f882001-02-23 17:55:21 +00005740 xmlNsPtr cur;
5741 xmlNsPtr *ret = NULL;
5742 int nbns = 0;
5743 int maxns = 10;
5744 int i;
5745
5746 while (node != NULL) {
Daniel Veillard77044732001-06-29 21:31:07 +00005747 if (node->type == XML_ELEMENT_NODE) {
5748 cur = node->nsDef;
5749 while (cur != NULL) {
5750 if (ret == NULL) {
5751 ret =
5752 (xmlNsPtr *) xmlMalloc((maxns + 1) *
5753 sizeof(xmlNsPtr));
5754 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005755 xmlTreeErrMemory("getting namespace list");
Daniel Veillard77044732001-06-29 21:31:07 +00005756 return (NULL);
5757 }
5758 ret[nbns] = NULL;
5759 }
5760 for (i = 0; i < nbns; i++) {
5761 if ((cur->prefix == ret[i]->prefix) ||
5762 (xmlStrEqual(cur->prefix, ret[i]->prefix)))
5763 break;
5764 }
5765 if (i >= nbns) {
5766 if (nbns >= maxns) {
5767 maxns *= 2;
5768 ret = (xmlNsPtr *) xmlRealloc(ret,
5769 (maxns +
5770 1) *
5771 sizeof(xmlNsPtr));
5772 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005773 xmlTreeErrMemory("getting namespace list");
Daniel Veillard77044732001-06-29 21:31:07 +00005774 return (NULL);
5775 }
5776 }
5777 ret[nbns++] = cur;
5778 ret[nbns] = NULL;
5779 }
Owen Taylor3473f882001-02-23 17:55:21 +00005780
Daniel Veillard77044732001-06-29 21:31:07 +00005781 cur = cur->next;
5782 }
5783 }
5784 node = node->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00005785 }
Daniel Veillard77044732001-06-29 21:31:07 +00005786 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00005787}
Daniel Veillard652327a2003-09-29 18:02:38 +00005788#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005789
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005790/*
5791* xmlTreeEnsureXMLDecl:
5792* @doc: the doc
Daniel Veillardaa6de472008-08-25 14:53:31 +00005793*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005794* Ensures that there is an XML namespace declaration on the doc.
Daniel Veillardaa6de472008-08-25 14:53:31 +00005795*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005796* Returns the XML ns-struct or NULL on API and internal errors.
5797*/
5798static xmlNsPtr
5799xmlTreeEnsureXMLDecl(xmlDocPtr doc)
5800{
5801 if (doc == NULL)
5802 return (NULL);
5803 if (doc->oldNs != NULL)
5804 return (doc->oldNs);
5805 {
5806 xmlNsPtr ns;
5807 ns = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5808 if (ns == NULL) {
5809 xmlTreeErrMemory(
5810 "allocating the XML namespace");
5811 return (NULL);
5812 }
5813 memset(ns, 0, sizeof(xmlNs));
5814 ns->type = XML_LOCAL_NAMESPACE;
Daniel Veillardaa6de472008-08-25 14:53:31 +00005815 ns->href = xmlStrdup(XML_XML_NAMESPACE);
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005816 ns->prefix = xmlStrdup((const xmlChar *)"xml");
5817 doc->oldNs = ns;
5818 return (ns);
5819 }
5820}
5821
Owen Taylor3473f882001-02-23 17:55:21 +00005822/**
5823 * xmlSearchNs:
5824 * @doc: the document
5825 * @node: the current node
Daniel Veillard77851712001-02-27 21:54:07 +00005826 * @nameSpace: the namespace prefix
Owen Taylor3473f882001-02-23 17:55:21 +00005827 *
5828 * Search a Ns registered under a given name space for a document.
5829 * recurse on the parents until it finds the defined namespace
5830 * or return NULL otherwise.
5831 * @nameSpace can be NULL, this is a search for the default namespace.
5832 * We don't allow to cross entities boundaries. If you don't declare
5833 * the namespace within those you will be in troubles !!! A warning
5834 * is generated to cover this case.
5835 *
5836 * Returns the namespace pointer or NULL.
5837 */
5838xmlNsPtr
5839xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00005840
Owen Taylor3473f882001-02-23 17:55:21 +00005841 xmlNsPtr cur;
Daniel Veillardf4e56292003-10-28 14:27:41 +00005842 xmlNodePtr orig = node;
Owen Taylor3473f882001-02-23 17:55:21 +00005843
5844 if (node == NULL) return(NULL);
5845 if ((nameSpace != NULL) &&
5846 (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) {
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00005847 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
5848 /*
5849 * The XML-1.0 namespace is normally held on the root
5850 * element. In this case exceptionally create it on the
5851 * node element.
5852 */
5853 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5854 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005855 xmlTreeErrMemory("searching namespace");
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00005856 return(NULL);
5857 }
5858 memset(cur, 0, sizeof(xmlNs));
5859 cur->type = XML_LOCAL_NAMESPACE;
Daniel Veillardaa6de472008-08-25 14:53:31 +00005860 cur->href = xmlStrdup(XML_XML_NAMESPACE);
5861 cur->prefix = xmlStrdup((const xmlChar *)"xml");
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00005862 cur->next = node->nsDef;
5863 node->nsDef = cur;
5864 return(cur);
5865 }
Daniel Veillard11ce4002006-03-10 00:36:23 +00005866 if (doc == NULL) {
5867 doc = node->doc;
5868 if (doc == NULL)
5869 return(NULL);
5870 }
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005871 /*
5872 * Return the XML namespace declaration held by the doc.
5873 */
5874 if (doc->oldNs == NULL)
5875 return(xmlTreeEnsureXMLDecl(doc));
5876 else
5877 return(doc->oldNs);
Owen Taylor3473f882001-02-23 17:55:21 +00005878 }
5879 while (node != NULL) {
5880 if ((node->type == XML_ENTITY_REF_NODE) ||
5881 (node->type == XML_ENTITY_NODE) ||
5882 (node->type == XML_ENTITY_DECL))
5883 return(NULL);
5884 if (node->type == XML_ELEMENT_NODE) {
5885 cur = node->nsDef;
5886 while (cur != NULL) {
5887 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
5888 (cur->href != NULL))
5889 return(cur);
5890 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
5891 (cur->href != NULL) &&
5892 (xmlStrEqual(cur->prefix, nameSpace)))
5893 return(cur);
5894 cur = cur->next;
5895 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00005896 if (orig != node) {
Daniel Veillardf4e56292003-10-28 14:27:41 +00005897 cur = node->ns;
5898 if (cur != NULL) {
5899 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
5900 (cur->href != NULL))
5901 return(cur);
5902 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
5903 (cur->href != NULL) &&
5904 (xmlStrEqual(cur->prefix, nameSpace)))
5905 return(cur);
5906 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00005907 }
Owen Taylor3473f882001-02-23 17:55:21 +00005908 }
5909 node = node->parent;
5910 }
5911 return(NULL);
5912}
5913
5914/**
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005915 * xmlNsInScope:
5916 * @doc: the document
5917 * @node: the current node
5918 * @ancestor: the ancestor carrying the namespace
5919 * @prefix: the namespace prefix
5920 *
5921 * Verify that the given namespace held on @ancestor is still in scope
5922 * on node.
Daniel Veillardaa6de472008-08-25 14:53:31 +00005923 *
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005924 * Returns 1 if true, 0 if false and -1 in case of error.
5925 */
5926static int
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005927xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node,
5928 xmlNodePtr ancestor, const xmlChar * prefix)
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005929{
5930 xmlNsPtr tst;
5931
5932 while ((node != NULL) && (node != ancestor)) {
5933 if ((node->type == XML_ENTITY_REF_NODE) ||
5934 (node->type == XML_ENTITY_NODE) ||
5935 (node->type == XML_ENTITY_DECL))
5936 return (-1);
5937 if (node->type == XML_ELEMENT_NODE) {
5938 tst = node->nsDef;
5939 while (tst != NULL) {
5940 if ((tst->prefix == NULL)
5941 && (prefix == NULL))
5942 return (0);
5943 if ((tst->prefix != NULL)
5944 && (prefix != NULL)
5945 && (xmlStrEqual(tst->prefix, prefix)))
5946 return (0);
5947 tst = tst->next;
5948 }
5949 }
5950 node = node->parent;
5951 }
5952 if (node != ancestor)
5953 return (-1);
5954 return (1);
5955}
Daniel Veillardaa6de472008-08-25 14:53:31 +00005956
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005957/**
Owen Taylor3473f882001-02-23 17:55:21 +00005958 * xmlSearchNsByHref:
5959 * @doc: the document
5960 * @node: the current node
5961 * @href: the namespace value
5962 *
5963 * Search a Ns aliasing a given URI. Recurse on the parents until it finds
5964 * the defined namespace or return NULL otherwise.
5965 * Returns the namespace pointer or NULL.
5966 */
5967xmlNsPtr
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005968xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href)
5969{
Owen Taylor3473f882001-02-23 17:55:21 +00005970 xmlNsPtr cur;
5971 xmlNodePtr orig = node;
Daniel Veillard62040be2004-05-17 03:17:26 +00005972 int is_attr;
Owen Taylor3473f882001-02-23 17:55:21 +00005973
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005974 if ((node == NULL) || (href == NULL))
5975 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005976 if (xmlStrEqual(href, XML_XML_NAMESPACE)) {
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005977 /*
5978 * Only the document can hold the XML spec namespace.
5979 */
5980 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
5981 /*
5982 * The XML-1.0 namespace is normally held on the root
5983 * element. In this case exceptionally create it on the
5984 * node element.
5985 */
5986 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5987 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005988 xmlTreeErrMemory("searching namespace");
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005989 return (NULL);
5990 }
5991 memset(cur, 0, sizeof(xmlNs));
5992 cur->type = XML_LOCAL_NAMESPACE;
5993 cur->href = xmlStrdup(XML_XML_NAMESPACE);
5994 cur->prefix = xmlStrdup((const xmlChar *) "xml");
5995 cur->next = node->nsDef;
5996 node->nsDef = cur;
5997 return (cur);
5998 }
Daniel Veillard11ce4002006-03-10 00:36:23 +00005999 if (doc == NULL) {
6000 doc = node->doc;
6001 if (doc == NULL)
6002 return(NULL);
6003 }
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00006004 /*
6005 * Return the XML namespace declaration held by the doc.
6006 */
6007 if (doc->oldNs == NULL)
6008 return(xmlTreeEnsureXMLDecl(doc));
6009 else
Daniel Veillardaa6de472008-08-25 14:53:31 +00006010 return(doc->oldNs);
Owen Taylor3473f882001-02-23 17:55:21 +00006011 }
Daniel Veillard62040be2004-05-17 03:17:26 +00006012 is_attr = (node->type == XML_ATTRIBUTE_NODE);
Owen Taylor3473f882001-02-23 17:55:21 +00006013 while (node != NULL) {
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006014 if ((node->type == XML_ENTITY_REF_NODE) ||
6015 (node->type == XML_ENTITY_NODE) ||
6016 (node->type == XML_ENTITY_DECL))
6017 return (NULL);
6018 if (node->type == XML_ELEMENT_NODE) {
6019 cur = node->nsDef;
6020 while (cur != NULL) {
6021 if ((cur->href != NULL) && (href != NULL) &&
6022 (xmlStrEqual(cur->href, href))) {
Daniel Veillard62040be2004-05-17 03:17:26 +00006023 if (((!is_attr) || (cur->prefix != NULL)) &&
Kasimier T. Buchcikba70cc02005-02-28 10:28:21 +00006024 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006025 return (cur);
6026 }
6027 cur = cur->next;
6028 }
Daniel Veillardf4e56292003-10-28 14:27:41 +00006029 if (orig != node) {
6030 cur = node->ns;
6031 if (cur != NULL) {
6032 if ((cur->href != NULL) && (href != NULL) &&
6033 (xmlStrEqual(cur->href, href))) {
Daniel Veillard62040be2004-05-17 03:17:26 +00006034 if (((!is_attr) || (cur->prefix != NULL)) &&
Kasimier T. Buchcikba70cc02005-02-28 10:28:21 +00006035 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
Daniel Veillardf4e56292003-10-28 14:27:41 +00006036 return (cur);
6037 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006038 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00006039 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006040 }
6041 node = node->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00006042 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00006043 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006044}
6045
6046/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00006047 * xmlNewReconciliedNs:
Owen Taylor3473f882001-02-23 17:55:21 +00006048 * @doc: the document
6049 * @tree: a node expected to hold the new namespace
6050 * @ns: the original namespace
6051 *
6052 * This function tries to locate a namespace definition in a tree
6053 * ancestors, or create a new namespace definition node similar to
6054 * @ns trying to reuse the same prefix. However if the given prefix is
6055 * null (default namespace) or reused within the subtree defined by
6056 * @tree or on one of its ancestors then a new prefix is generated.
6057 * Returns the (new) namespace definition or NULL in case of error
6058 */
6059xmlNsPtr
6060xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) {
6061 xmlNsPtr def;
6062 xmlChar prefix[50];
6063 int counter = 1;
6064
6065 if (tree == NULL) {
6066#ifdef DEBUG_TREE
6067 xmlGenericError(xmlGenericErrorContext,
6068 "xmlNewReconciliedNs : tree == NULL\n");
6069#endif
6070 return(NULL);
6071 }
Daniel Veillardce244ad2004-11-05 10:03:46 +00006072 if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006073#ifdef DEBUG_TREE
6074 xmlGenericError(xmlGenericErrorContext,
6075 "xmlNewReconciliedNs : ns == NULL\n");
6076#endif
6077 return(NULL);
6078 }
6079 /*
6080 * Search an existing namespace definition inherited.
6081 */
6082 def = xmlSearchNsByHref(doc, tree, ns->href);
6083 if (def != NULL)
6084 return(def);
6085
6086 /*
6087 * Find a close prefix which is not already in use.
6088 * Let's strip namespace prefixes longer than 20 chars !
6089 */
Daniel Veillardf742d342002-03-07 00:05:35 +00006090 if (ns->prefix == NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00006091 snprintf((char *) prefix, sizeof(prefix), "default");
Daniel Veillardf742d342002-03-07 00:05:35 +00006092 else
William M. Brack13dfa872004-09-18 04:52:08 +00006093 snprintf((char *) prefix, sizeof(prefix), "%.20s", (char *)ns->prefix);
Daniel Veillardf742d342002-03-07 00:05:35 +00006094
Owen Taylor3473f882001-02-23 17:55:21 +00006095 def = xmlSearchNs(doc, tree, prefix);
6096 while (def != NULL) {
6097 if (counter > 1000) return(NULL);
Daniel Veillardf742d342002-03-07 00:05:35 +00006098 if (ns->prefix == NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00006099 snprintf((char *) prefix, sizeof(prefix), "default%d", counter++);
Daniel Veillardf742d342002-03-07 00:05:35 +00006100 else
William M. Brack13dfa872004-09-18 04:52:08 +00006101 snprintf((char *) prefix, sizeof(prefix), "%.20s%d",
Daniel Veillardaa6de472008-08-25 14:53:31 +00006102 (char *)ns->prefix, counter++);
Owen Taylor3473f882001-02-23 17:55:21 +00006103 def = xmlSearchNs(doc, tree, prefix);
6104 }
6105
6106 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00006107 * OK, now we are ready to create a new one.
Owen Taylor3473f882001-02-23 17:55:21 +00006108 */
6109 def = xmlNewNs(tree, ns->href, prefix);
6110 return(def);
6111}
6112
Daniel Veillard652327a2003-09-29 18:02:38 +00006113#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00006114/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00006115 * xmlReconciliateNs:
Owen Taylor3473f882001-02-23 17:55:21 +00006116 * @doc: the document
6117 * @tree: a node defining the subtree to reconciliate
6118 *
6119 * This function checks that all the namespaces declared within the given
6120 * tree are properly declared. This is needed for example after Copy or Cut
6121 * and then paste operations. The subtree may still hold pointers to
6122 * namespace declarations outside the subtree or invalid/masked. As much
Daniel Veillardd1640922001-12-17 15:30:10 +00006123 * as possible the function try to reuse the existing namespaces found in
Owen Taylor3473f882001-02-23 17:55:21 +00006124 * the new environment. If not possible the new namespaces are redeclared
6125 * on @tree at the top of the given subtree.
6126 * Returns the number of namespace declarations created or -1 in case of error.
6127 */
6128int
6129xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) {
6130 xmlNsPtr *oldNs = NULL;
6131 xmlNsPtr *newNs = NULL;
6132 int sizeCache = 0;
6133 int nbCache = 0;
6134
6135 xmlNsPtr n;
6136 xmlNodePtr node = tree;
6137 xmlAttrPtr attr;
6138 int ret = 0, i;
6139
Daniel Veillardce244ad2004-11-05 10:03:46 +00006140 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) return(-1);
6141 if ((doc == NULL) || (doc->type != XML_DOCUMENT_NODE)) return(-1);
6142 if (node->doc != doc) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006143 while (node != NULL) {
6144 /*
6145 * Reconciliate the node namespace
6146 */
6147 if (node->ns != NULL) {
6148 /*
6149 * initialize the cache if needed
6150 */
6151 if (sizeCache == 0) {
6152 sizeCache = 10;
6153 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6154 sizeof(xmlNsPtr));
6155 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006156 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00006157 return(-1);
6158 }
6159 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6160 sizeof(xmlNsPtr));
6161 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006162 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00006163 xmlFree(oldNs);
6164 return(-1);
6165 }
6166 }
6167 for (i = 0;i < nbCache;i++) {
6168 if (oldNs[i] == node->ns) {
6169 node->ns = newNs[i];
6170 break;
6171 }
6172 }
6173 if (i == nbCache) {
6174 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00006175 * OK we need to recreate a new namespace definition
Owen Taylor3473f882001-02-23 17:55:21 +00006176 */
6177 n = xmlNewReconciliedNs(doc, tree, node->ns);
6178 if (n != NULL) { /* :-( what if else ??? */
6179 /*
6180 * check if we need to grow the cache buffers.
6181 */
6182 if (sizeCache <= nbCache) {
6183 sizeCache *= 2;
6184 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
6185 sizeof(xmlNsPtr));
6186 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006187 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00006188 xmlFree(newNs);
6189 return(-1);
6190 }
6191 newNs = (xmlNsPtr *) xmlRealloc(newNs, 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 newNs[nbCache] = n;
6200 oldNs[nbCache++] = node->ns;
6201 node->ns = n;
6202 }
6203 }
6204 }
6205 /*
6206 * now check for namespace hold by attributes on the node.
6207 */
Daniel Veillardb5f11972006-10-14 08:46:40 +00006208 if (node->type == XML_ELEMENT_NODE) {
6209 attr = node->properties;
6210 while (attr != NULL) {
6211 if (attr->ns != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00006212 /*
Daniel Veillardb5f11972006-10-14 08:46:40 +00006213 * initialize the cache if needed
Owen Taylor3473f882001-02-23 17:55:21 +00006214 */
Daniel Veillardb5f11972006-10-14 08:46:40 +00006215 if (sizeCache == 0) {
6216 sizeCache = 10;
6217 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6218 sizeof(xmlNsPtr));
6219 if (oldNs == NULL) {
6220 xmlTreeErrMemory("fixing namespaces");
6221 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006222 }
Daniel Veillardb5f11972006-10-14 08:46:40 +00006223 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
6224 sizeof(xmlNsPtr));
6225 if (newNs == NULL) {
6226 xmlTreeErrMemory("fixing namespaces");
6227 xmlFree(oldNs);
6228 return(-1);
6229 }
6230 }
6231 for (i = 0;i < nbCache;i++) {
6232 if (oldNs[i] == attr->ns) {
6233 attr->ns = newNs[i];
6234 break;
6235 }
6236 }
6237 if (i == nbCache) {
6238 /*
6239 * OK we need to recreate a new namespace definition
6240 */
6241 n = xmlNewReconciliedNs(doc, tree, attr->ns);
6242 if (n != NULL) { /* :-( what if else ??? */
6243 /*
6244 * check if we need to grow the cache buffers.
6245 */
6246 if (sizeCache <= nbCache) {
6247 sizeCache *= 2;
6248 oldNs = (xmlNsPtr *) xmlRealloc(oldNs,
6249 sizeCache * sizeof(xmlNsPtr));
6250 if (oldNs == NULL) {
6251 xmlTreeErrMemory("fixing namespaces");
6252 xmlFree(newNs);
6253 return(-1);
6254 }
6255 newNs = (xmlNsPtr *) xmlRealloc(newNs,
6256 sizeCache * sizeof(xmlNsPtr));
6257 if (newNs == NULL) {
6258 xmlTreeErrMemory("fixing namespaces");
6259 xmlFree(oldNs);
6260 return(-1);
6261 }
6262 }
6263 newNs[nbCache] = n;
6264 oldNs[nbCache++] = attr->ns;
6265 attr->ns = n;
6266 }
Owen Taylor3473f882001-02-23 17:55:21 +00006267 }
6268 }
Daniel Veillardb5f11972006-10-14 08:46:40 +00006269 attr = attr->next;
Owen Taylor3473f882001-02-23 17:55:21 +00006270 }
Owen Taylor3473f882001-02-23 17:55:21 +00006271 }
6272
6273 /*
6274 * Browse the full subtree, deep first
6275 */
Daniel Veillardb5f11972006-10-14 08:46:40 +00006276 if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006277 /* deep first */
6278 node = node->children;
6279 } else if ((node != tree) && (node->next != NULL)) {
6280 /* then siblings */
6281 node = node->next;
6282 } else if (node != tree) {
6283 /* go up to parents->next if needed */
6284 while (node != tree) {
6285 if (node->parent != NULL)
6286 node = node->parent;
6287 if ((node != tree) && (node->next != NULL)) {
6288 node = node->next;
6289 break;
6290 }
6291 if (node->parent == NULL) {
6292 node = NULL;
6293 break;
6294 }
6295 }
6296 /* exit condition */
Daniel Veillardaa6de472008-08-25 14:53:31 +00006297 if (node == tree)
Owen Taylor3473f882001-02-23 17:55:21 +00006298 node = NULL;
Daniel Veillard1e774382002-03-06 17:35:40 +00006299 } else
6300 break;
Owen Taylor3473f882001-02-23 17:55:21 +00006301 }
Daniel Veillardf742d342002-03-07 00:05:35 +00006302 if (oldNs != NULL)
6303 xmlFree(oldNs);
6304 if (newNs != NULL)
6305 xmlFree(newNs);
Owen Taylor3473f882001-02-23 17:55:21 +00006306 return(ret);
6307}
Daniel Veillard652327a2003-09-29 18:02:38 +00006308#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00006309
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006310static xmlAttrPtr
6311xmlGetPropNodeInternal(xmlNodePtr node, const xmlChar *name,
6312 const xmlChar *nsName, int useDTD)
6313{
6314 xmlAttrPtr prop;
6315
6316 if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL))
6317 return(NULL);
6318
6319 if (node->properties != NULL) {
6320 prop = node->properties;
6321 if (nsName == NULL) {
6322 /*
6323 * We want the attr to be in no namespace.
6324 */
6325 do {
6326 if ((prop->ns == NULL) && xmlStrEqual(prop->name, name)) {
6327 return(prop);
6328 }
6329 prop = prop->next;
6330 } while (prop != NULL);
6331 } else {
6332 /*
6333 * We want the attr to be in the specified namespace.
6334 */
6335 do {
6336 if ((prop->ns != NULL) && xmlStrEqual(prop->name, name) &&
6337 ((prop->ns->href == nsName) ||
6338 xmlStrEqual(prop->ns->href, nsName)))
6339 {
6340 return(prop);
6341 }
6342 prop = prop->next;
6343 } while (prop != NULL);
6344 }
6345 }
6346
6347#ifdef LIBXML_TREE_ENABLED
6348 if (! useDTD)
6349 return(NULL);
6350 /*
6351 * Check if there is a default/fixed attribute declaration in
6352 * the internal or external subset.
Daniel Veillardaa6de472008-08-25 14:53:31 +00006353 */
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006354 if ((node->doc != NULL) && (node->doc->intSubset != NULL)) {
6355 xmlDocPtr doc = node->doc;
6356 xmlAttributePtr attrDecl = NULL;
6357 xmlChar *elemQName, *tmpstr = NULL;
6358
6359 /*
Daniel Veillardaa6de472008-08-25 14:53:31 +00006360 * We need the QName of the element for the DTD-lookup.
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006361 */
6362 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
6363 tmpstr = xmlStrdup(node->ns->prefix);
6364 tmpstr = xmlStrcat(tmpstr, BAD_CAST ":");
6365 tmpstr = xmlStrcat(tmpstr, node->name);
6366 if (tmpstr == NULL)
6367 return(NULL);
6368 elemQName = tmpstr;
6369 } else
6370 elemQName = (xmlChar *) node->name;
6371 if (nsName == NULL) {
6372 /*
6373 * The common and nice case: Attr in no namespace.
6374 */
6375 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset,
6376 elemQName, name, NULL);
6377 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
6378 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset,
6379 elemQName, name, NULL);
6380 }
6381 } else {
6382 xmlNsPtr *nsList, *cur;
6383
6384 /*
6385 * The ugly case: Search using the prefixes of in-scope
6386 * ns-decls corresponding to @nsName.
6387 */
6388 nsList = xmlGetNsList(node->doc, node);
6389 if (nsList == NULL) {
6390 if (tmpstr != NULL)
6391 xmlFree(tmpstr);
6392 return(NULL);
6393 }
6394 cur = nsList;
6395 while (*cur != NULL) {
6396 if (xmlStrEqual((*cur)->href, nsName)) {
6397 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elemQName,
6398 name, (*cur)->prefix);
6399 if (attrDecl)
6400 break;
6401 if (doc->extSubset != NULL) {
6402 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elemQName,
6403 name, (*cur)->prefix);
6404 if (attrDecl)
6405 break;
6406 }
6407 }
6408 cur++;
6409 }
6410 xmlFree(nsList);
Daniel Veillardaa6de472008-08-25 14:53:31 +00006411 }
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006412 if (tmpstr != NULL)
6413 xmlFree(tmpstr);
6414 /*
6415 * Only default/fixed attrs are relevant.
6416 */
6417 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6418 return((xmlAttrPtr) attrDecl);
6419 }
6420#endif /* LIBXML_TREE_ENABLED */
6421 return(NULL);
6422}
6423
6424static xmlChar*
6425xmlGetPropNodeValueInternal(xmlAttrPtr prop)
6426{
6427 if (prop == NULL)
6428 return(NULL);
6429 if (prop->type == XML_ATTRIBUTE_NODE) {
6430 /*
6431 * Note that we return at least the empty string.
6432 * TODO: Do we really always want that?
6433 */
6434 if (prop->children != NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00006435 if ((prop->children->next == NULL) &&
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006436 ((prop->children->type == XML_TEXT_NODE) ||
6437 (prop->children->type == XML_CDATA_SECTION_NODE)))
6438 {
6439 /*
6440 * Optimization for the common case: only 1 text node.
6441 */
6442 return(xmlStrdup(prop->children->content));
6443 } else {
6444 xmlChar *ret;
6445
6446 ret = xmlNodeListGetString(prop->doc, prop->children, 1);
6447 if (ret != NULL)
6448 return(ret);
6449 }
6450 }
6451 return(xmlStrdup((xmlChar *)""));
6452 } else if (prop->type == XML_ATTRIBUTE_DECL) {
6453 return(xmlStrdup(((xmlAttributePtr)prop)->defaultValue));
6454 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00006455 return(NULL);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006456}
6457
Owen Taylor3473f882001-02-23 17:55:21 +00006458/**
6459 * xmlHasProp:
6460 * @node: the node
6461 * @name: the attribute name
6462 *
6463 * Search an attribute associated to a node
6464 * This function also looks in DTD attribute declaration for #FIXED or
6465 * default declaration values unless DTD use has been turned off.
6466 *
Daniel Veillardaa6de472008-08-25 14:53:31 +00006467 * Returns the attribute or the attribute declaration or NULL if
Owen Taylor3473f882001-02-23 17:55:21 +00006468 * neither was found.
6469 */
6470xmlAttrPtr
6471xmlHasProp(xmlNodePtr node, const xmlChar *name) {
6472 xmlAttrPtr prop;
6473 xmlDocPtr doc;
6474
Daniel Veillard8874b942005-08-25 13:19:21 +00006475 if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL))
6476 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006477 /*
6478 * Check on the properties attached to the node
6479 */
6480 prop = node->properties;
6481 while (prop != NULL) {
6482 if (xmlStrEqual(prop->name, name)) {
6483 return(prop);
6484 }
6485 prop = prop->next;
6486 }
6487 if (!xmlCheckDTD) return(NULL);
6488
6489 /*
6490 * Check if there is a default declaration in the internal
6491 * or external subsets
6492 */
6493 doc = node->doc;
6494 if (doc != NULL) {
6495 xmlAttributePtr attrDecl;
6496 if (doc->intSubset != NULL) {
6497 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
6498 if ((attrDecl == NULL) && (doc->extSubset != NULL))
6499 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
Daniel Veillard75eb1ad2003-07-07 14:42:44 +00006500 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6501 /* return attribute declaration only if a default value is given
6502 (that includes #FIXED declarations) */
Owen Taylor3473f882001-02-23 17:55:21 +00006503 return((xmlAttrPtr) attrDecl);
6504 }
6505 }
6506 return(NULL);
6507}
6508
6509/**
Daniel Veillarde95e2392001-06-06 10:46:28 +00006510 * xmlHasNsProp:
6511 * @node: the node
6512 * @name: the attribute name
Daniel Veillardca2366a2001-06-11 12:09:01 +00006513 * @nameSpace: the URI of the namespace
Daniel Veillarde95e2392001-06-06 10:46:28 +00006514 *
6515 * Search for an attribute associated to a node
6516 * This attribute has to be anchored in the namespace specified.
6517 * This does the entity substitution.
6518 * This function looks in DTD attribute declaration for #FIXED or
6519 * default declaration values unless DTD use has been turned off.
William M. Brack2c228442004-10-03 04:10:00 +00006520 * Note that a namespace of NULL indicates to use the default namespace.
Daniel Veillarde95e2392001-06-06 10:46:28 +00006521 *
6522 * Returns the attribute or the attribute declaration or NULL
6523 * if neither was found.
6524 */
6525xmlAttrPtr
Daniel Veillardca2366a2001-06-11 12:09:01 +00006526xmlHasNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
Daniel Veillarde95e2392001-06-06 10:46:28 +00006527
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006528 return(xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD));
Daniel Veillarde95e2392001-06-06 10:46:28 +00006529}
6530
6531/**
Owen Taylor3473f882001-02-23 17:55:21 +00006532 * xmlGetProp:
6533 * @node: the node
6534 * @name: the attribute name
6535 *
6536 * Search and get the value of an attribute associated to a node
6537 * This does the entity substitution.
6538 * This function looks in DTD attribute declaration for #FIXED or
6539 * default declaration values unless DTD use has been turned off.
Daniel Veillard784b9352003-02-16 15:50:27 +00006540 * NOTE: this function acts independently of namespaces associated
Daniel Veillard71531f32003-02-05 13:19:53 +00006541 * to the attribute. Use xmlGetNsProp() or xmlGetNoNsProp()
6542 * for namespace aware processing.
Owen Taylor3473f882001-02-23 17:55:21 +00006543 *
6544 * Returns the attribute value or NULL if not found.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00006545 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00006546 */
6547xmlChar *
6548xmlGetProp(xmlNodePtr node, const xmlChar *name) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00006549 xmlAttrPtr prop;
Owen Taylor3473f882001-02-23 17:55:21 +00006550
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006551 prop = xmlHasProp(node, name);
6552 if (prop == NULL)
6553 return(NULL);
Daniel Veillardaa6de472008-08-25 14:53:31 +00006554 return(xmlGetPropNodeValueInternal(prop));
Owen Taylor3473f882001-02-23 17:55:21 +00006555}
6556
6557/**
Daniel Veillard71531f32003-02-05 13:19:53 +00006558 * xmlGetNoNsProp:
6559 * @node: the node
6560 * @name: the attribute name
6561 *
6562 * Search and get the value of an attribute associated to a node
6563 * This does the entity substitution.
6564 * This function looks in DTD attribute declaration for #FIXED or
6565 * default declaration values unless DTD use has been turned off.
6566 * This function is similar to xmlGetProp except it will accept only
6567 * an attribute in no namespace.
6568 *
6569 * Returns the attribute value or NULL if not found.
6570 * It's up to the caller to free the memory with xmlFree().
6571 */
6572xmlChar *
6573xmlGetNoNsProp(xmlNodePtr node, const xmlChar *name) {
6574 xmlAttrPtr prop;
Daniel Veillardaa6de472008-08-25 14:53:31 +00006575
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006576 prop = xmlGetPropNodeInternal(node, name, NULL, xmlCheckDTD);
6577 if (prop == NULL)
Daniel Veillard8874b942005-08-25 13:19:21 +00006578 return(NULL);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006579 return(xmlGetPropNodeValueInternal(prop));
Daniel Veillard71531f32003-02-05 13:19:53 +00006580}
6581
6582/**
Owen Taylor3473f882001-02-23 17:55:21 +00006583 * xmlGetNsProp:
6584 * @node: the node
6585 * @name: the attribute name
Daniel Veillardca2366a2001-06-11 12:09:01 +00006586 * @nameSpace: the URI of the namespace
Owen Taylor3473f882001-02-23 17:55:21 +00006587 *
6588 * Search and get the value of an attribute associated to a node
6589 * This attribute has to be anchored in the namespace specified.
6590 * This does the entity substitution.
6591 * This function looks in DTD attribute declaration for #FIXED or
6592 * default declaration values unless DTD use has been turned off.
6593 *
6594 * Returns the attribute value or NULL if not found.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00006595 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00006596 */
6597xmlChar *
Daniel Veillardca2366a2001-06-11 12:09:01 +00006598xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
Owen Taylor3473f882001-02-23 17:55:21 +00006599 xmlAttrPtr prop;
Owen Taylor3473f882001-02-23 17:55:21 +00006600
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006601 prop = xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD);
6602 if (prop == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00006603 return(NULL);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006604 return(xmlGetPropNodeValueInternal(prop));
Owen Taylor3473f882001-02-23 17:55:21 +00006605}
6606
Daniel Veillard2156d432004-03-04 15:59:36 +00006607#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
6608/**
6609 * xmlUnsetProp:
6610 * @node: the node
6611 * @name: the attribute name
6612 *
6613 * Remove an attribute carried by a node.
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006614 * This handles only attributes in no namespace.
Daniel Veillard2156d432004-03-04 15:59:36 +00006615 * Returns 0 if successful, -1 if not found
6616 */
6617int
6618xmlUnsetProp(xmlNodePtr node, const xmlChar *name) {
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +00006619 xmlAttrPtr prop;
Daniel Veillard2156d432004-03-04 15:59:36 +00006620
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006621 prop = xmlGetPropNodeInternal(node, name, NULL, 0);
6622 if (prop == NULL)
Daniel Veillard2156d432004-03-04 15:59:36 +00006623 return(-1);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006624 xmlUnlinkNode((xmlNodePtr) prop);
6625 xmlFreeProp(prop);
6626 return(0);
Daniel Veillard2156d432004-03-04 15:59:36 +00006627}
6628
6629/**
6630 * xmlUnsetNsProp:
6631 * @node: the node
6632 * @ns: the namespace definition
6633 * @name: the attribute name
6634 *
6635 * Remove an attribute carried by a node.
6636 * Returns 0 if successful, -1 if not found
6637 */
6638int
6639xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) {
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +00006640 xmlAttrPtr prop;
Daniel Veillardaa6de472008-08-25 14:53:31 +00006641
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006642 prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0);
6643 if (prop == NULL)
Daniel Veillard2156d432004-03-04 15:59:36 +00006644 return(-1);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006645 xmlUnlinkNode((xmlNodePtr) prop);
6646 xmlFreeProp(prop);
6647 return(0);
Daniel Veillard2156d432004-03-04 15:59:36 +00006648}
6649#endif
6650
6651#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_HTML_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00006652/**
6653 * xmlSetProp:
6654 * @node: the node
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006655 * @name: the attribute name (a QName)
Owen Taylor3473f882001-02-23 17:55:21 +00006656 * @value: the attribute value
6657 *
6658 * Set (or reset) an attribute carried by a node.
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006659 * If @name has a prefix, then the corresponding
6660 * namespace-binding will be used, if in scope; it is an
6661 * error it there's no such ns-binding for the prefix in
6662 * scope.
Owen Taylor3473f882001-02-23 17:55:21 +00006663 * Returns the attribute pointer.
Daniel Veillardaa6de472008-08-25 14:53:31 +00006664 *
Owen Taylor3473f882001-02-23 17:55:21 +00006665 */
6666xmlAttrPtr
6667xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00006668 int len;
6669 const xmlChar *nqname;
Owen Taylor3473f882001-02-23 17:55:21 +00006670
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006671 if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE))
Owen Taylor3473f882001-02-23 17:55:21 +00006672 return(NULL);
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00006673
6674 /*
6675 * handle QNames
6676 */
6677 nqname = xmlSplitQName3(name, &len);
6678 if (nqname != NULL) {
6679 xmlNsPtr ns;
6680 xmlChar *prefix = xmlStrndup(name, len);
6681 ns = xmlSearchNs(node->doc, node, prefix);
6682 if (prefix != NULL)
6683 xmlFree(prefix);
6684 if (ns != NULL)
6685 return(xmlSetNsProp(node, ns, nqname, value));
6686 }
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006687 return(xmlSetNsProp(node, NULL, name, value));
Owen Taylor3473f882001-02-23 17:55:21 +00006688}
6689
6690/**
6691 * xmlSetNsProp:
6692 * @node: the node
6693 * @ns: the namespace definition
6694 * @name: the attribute name
6695 * @value: the attribute value
6696 *
6697 * Set (or reset) an attribute carried by a node.
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006698 * The ns structure must be in scope, this is not checked
Owen Taylor3473f882001-02-23 17:55:21 +00006699 *
6700 * Returns the attribute pointer.
6701 */
6702xmlAttrPtr
6703xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006704 const xmlChar *value)
6705{
Owen Taylor3473f882001-02-23 17:55:21 +00006706 xmlAttrPtr prop;
Daniel Veillardaa6de472008-08-25 14:53:31 +00006707
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006708 if (ns && (ns->href == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00006709 return(NULL);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006710 prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0);
6711 if (prop != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00006712 /*
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006713 * Modify the attribute's value.
6714 */
6715 if (prop->atype == XML_ATTRIBUTE_ID) {
6716 xmlRemoveID(node->doc, prop);
6717 prop->atype = XML_ATTRIBUTE_ID;
6718 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00006719 if (prop->children != NULL)
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006720 xmlFreeNodeList(prop->children);
6721 prop->children = NULL;
6722 prop->last = NULL;
6723 prop->ns = ns;
6724 if (value != NULL) {
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006725 xmlNodePtr tmp;
Daniel Veillardaa6de472008-08-25 14:53:31 +00006726
Daniel Veillard6f8611f2008-02-15 08:33:21 +00006727 if(!xmlCheckUTF8(value)) {
6728 xmlTreeErr(XML_TREE_NOT_UTF8, (xmlNodePtr) node->doc,
6729 NULL);
6730 if (node->doc != NULL)
6731 node->doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
6732 }
6733 prop->children = xmlNewDocText(node->doc, value);
Owen Taylor3473f882001-02-23 17:55:21 +00006734 prop->last = NULL;
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006735 tmp = prop->children;
6736 while (tmp != NULL) {
6737 tmp->parent = (xmlNodePtr) prop;
6738 if (tmp->next == NULL)
6739 prop->last = tmp;
6740 tmp = tmp->next;
6741 }
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006742 }
6743 if (prop->atype == XML_ATTRIBUTE_ID)
6744 xmlAddID(NULL, node->doc, value, prop);
6745 return(prop);
Owen Taylor3473f882001-02-23 17:55:21 +00006746 }
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006747 /*
6748 * No equal attr found; create a new one.
6749 */
6750 return(xmlNewPropInternal(node, ns, name, value, 0));
Owen Taylor3473f882001-02-23 17:55:21 +00006751}
6752
Daniel Veillard652327a2003-09-29 18:02:38 +00006753#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard75bea542001-05-11 17:41:21 +00006754
6755/**
Owen Taylor3473f882001-02-23 17:55:21 +00006756 * xmlNodeIsText:
6757 * @node: the node
Daniel Veillardaa6de472008-08-25 14:53:31 +00006758 *
Owen Taylor3473f882001-02-23 17:55:21 +00006759 * Is this node a Text node ?
6760 * Returns 1 yes, 0 no
6761 */
6762int
6763xmlNodeIsText(xmlNodePtr node) {
6764 if (node == NULL) return(0);
6765
6766 if (node->type == XML_TEXT_NODE) return(1);
6767 return(0);
6768}
6769
6770/**
6771 * xmlIsBlankNode:
6772 * @node: the node
Daniel Veillardaa6de472008-08-25 14:53:31 +00006773 *
Owen Taylor3473f882001-02-23 17:55:21 +00006774 * Checks whether this node is an empty or whitespace only
6775 * (and possibly ignorable) text-node.
6776 *
6777 * Returns 1 yes, 0 no
6778 */
6779int
6780xmlIsBlankNode(xmlNodePtr node) {
6781 const xmlChar *cur;
6782 if (node == NULL) return(0);
6783
Daniel Veillard7db37732001-07-12 01:20:08 +00006784 if ((node->type != XML_TEXT_NODE) &&
6785 (node->type != XML_CDATA_SECTION_NODE))
6786 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006787 if (node->content == NULL) return(1);
Owen Taylor3473f882001-02-23 17:55:21 +00006788 cur = node->content;
Owen Taylor3473f882001-02-23 17:55:21 +00006789 while (*cur != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00006790 if (!IS_BLANK_CH(*cur)) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006791 cur++;
6792 }
6793
6794 return(1);
6795}
6796
6797/**
6798 * xmlTextConcat:
6799 * @node: the node
6800 * @content: the content
Daniel Veillard60087f32001-10-10 09:45:09 +00006801 * @len: @content length
Daniel Veillardaa6de472008-08-25 14:53:31 +00006802 *
Owen Taylor3473f882001-02-23 17:55:21 +00006803 * Concat the given string at the end of the existing node content
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006804 *
6805 * Returns -1 in case of error, 0 otherwise
Owen Taylor3473f882001-02-23 17:55:21 +00006806 */
6807
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006808int
Owen Taylor3473f882001-02-23 17:55:21 +00006809xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006810 if (node == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006811
6812 if ((node->type != XML_TEXT_NODE) &&
Rob Richardsa02f1992006-09-16 14:04:26 +00006813 (node->type != XML_CDATA_SECTION_NODE) &&
6814 (node->type != XML_COMMENT_NODE) &&
6815 (node->type != XML_PI_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006816#ifdef DEBUG_TREE
6817 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006818 "xmlTextConcat: node is not text nor CDATA\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006819#endif
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006820 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006821 }
William M. Brack7762bb12004-01-04 14:49:01 +00006822 /* need to check if content is currently in the dictionary */
Daniel Veillard8874b942005-08-25 13:19:21 +00006823 if ((node->content == (xmlChar *) &(node->properties)) ||
6824 ((node->doc != NULL) && (node->doc->dict != NULL) &&
6825 xmlDictOwns(node->doc->dict, node->content))) {
William M. Brack7762bb12004-01-04 14:49:01 +00006826 node->content = xmlStrncatNew(node->content, content, len);
6827 } else {
6828 node->content = xmlStrncat(node->content, content, len);
6829 }
Daniel Veillard8874b942005-08-25 13:19:21 +00006830 node->properties = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006831 if (node->content == NULL)
6832 return(-1);
6833 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006834}
6835
6836/************************************************************************
6837 * *
6838 * Output : to a FILE or in memory *
6839 * *
6840 ************************************************************************/
6841
Owen Taylor3473f882001-02-23 17:55:21 +00006842/**
6843 * xmlBufferCreate:
6844 *
6845 * routine to create an XML buffer.
6846 * returns the new structure.
6847 */
6848xmlBufferPtr
6849xmlBufferCreate(void) {
6850 xmlBufferPtr ret;
6851
6852 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6853 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006854 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006855 return(NULL);
6856 }
6857 ret->use = 0;
Daniel Veillarde356c282001-03-10 12:32:04 +00006858 ret->size = xmlDefaultBufferSize;
Owen Taylor3473f882001-02-23 17:55:21 +00006859 ret->alloc = xmlBufferAllocScheme;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00006860 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00006861 if (ret->content == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006862 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006863 xmlFree(ret);
6864 return(NULL);
6865 }
6866 ret->content[0] = 0;
Daniel Veillarde83e93e2008-08-30 12:52:26 +00006867 ret->contentIO = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00006868 return(ret);
6869}
6870
6871/**
6872 * xmlBufferCreateSize:
6873 * @size: initial size of buffer
6874 *
6875 * routine to create an XML buffer.
6876 * returns the new structure.
6877 */
6878xmlBufferPtr
6879xmlBufferCreateSize(size_t size) {
6880 xmlBufferPtr ret;
6881
6882 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6883 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006884 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006885 return(NULL);
6886 }
6887 ret->use = 0;
6888 ret->alloc = xmlBufferAllocScheme;
6889 ret->size = (size ? size+2 : 0); /* +1 for ending null */
6890 if (ret->size){
Daniel Veillard3c908dc2003-04-19 00:07:51 +00006891 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00006892 if (ret->content == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006893 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006894 xmlFree(ret);
6895 return(NULL);
6896 }
6897 ret->content[0] = 0;
6898 } else
6899 ret->content = NULL;
Daniel Veillarde83e93e2008-08-30 12:52:26 +00006900 ret->contentIO = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00006901 return(ret);
6902}
6903
6904/**
Daniel Veillard53350552003-09-18 13:35:51 +00006905 * xmlBufferCreateStatic:
6906 * @mem: the memory area
6907 * @size: the size in byte
6908 *
MST 2003 John Flecka0e7e932003-12-19 03:13:47 +00006909 * routine to create an XML buffer from an immutable memory area.
6910 * The area won't be modified nor copied, and is expected to be
Daniel Veillard53350552003-09-18 13:35:51 +00006911 * present until the end of the buffer lifetime.
6912 *
6913 * returns the new structure.
6914 */
6915xmlBufferPtr
6916xmlBufferCreateStatic(void *mem, size_t size) {
6917 xmlBufferPtr ret;
6918
6919 if ((mem == NULL) || (size == 0))
6920 return(NULL);
6921
6922 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6923 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006924 xmlTreeErrMemory("creating buffer");
Daniel Veillard53350552003-09-18 13:35:51 +00006925 return(NULL);
6926 }
6927 ret->use = size;
6928 ret->size = size;
6929 ret->alloc = XML_BUFFER_ALLOC_IMMUTABLE;
6930 ret->content = (xmlChar *) mem;
6931 return(ret);
6932}
6933
6934/**
Owen Taylor3473f882001-02-23 17:55:21 +00006935 * xmlBufferSetAllocationScheme:
Daniel Veillardbd9afb52002-09-25 22:25:35 +00006936 * @buf: the buffer to tune
Owen Taylor3473f882001-02-23 17:55:21 +00006937 * @scheme: allocation scheme to use
6938 *
6939 * Sets the allocation scheme for this buffer
6940 */
6941void
Daniel Veillardaa6de472008-08-25 14:53:31 +00006942xmlBufferSetAllocationScheme(xmlBufferPtr buf,
Owen Taylor3473f882001-02-23 17:55:21 +00006943 xmlBufferAllocationScheme scheme) {
6944 if (buf == NULL) {
6945#ifdef DEBUG_BUFFER
6946 xmlGenericError(xmlGenericErrorContext,
6947 "xmlBufferSetAllocationScheme: buf == NULL\n");
6948#endif
6949 return;
6950 }
Daniel Veillardda3fee42008-09-01 13:08:57 +00006951 if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) ||
6952 (buf->alloc == XML_BUFFER_ALLOC_IO)) return;
6953 if ((scheme == XML_BUFFER_ALLOC_DOUBLEIT) ||
6954 (scheme == XML_BUFFER_ALLOC_EXACT) ||
6955 (scheme == XML_BUFFER_ALLOC_IMMUTABLE))
6956 buf->alloc = scheme;
Owen Taylor3473f882001-02-23 17:55:21 +00006957}
6958
6959/**
6960 * xmlBufferFree:
6961 * @buf: the buffer to free
6962 *
Daniel Veillard9d06d302002-01-22 18:15:52 +00006963 * Frees an XML buffer. It frees both the content and the structure which
6964 * encapsulate it.
Owen Taylor3473f882001-02-23 17:55:21 +00006965 */
6966void
6967xmlBufferFree(xmlBufferPtr buf) {
6968 if (buf == NULL) {
6969#ifdef DEBUG_BUFFER
6970 xmlGenericError(xmlGenericErrorContext,
6971 "xmlBufferFree: buf == NULL\n");
6972#endif
6973 return;
6974 }
Daniel Veillard53350552003-09-18 13:35:51 +00006975
Daniel Veillarde83e93e2008-08-30 12:52:26 +00006976 if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
6977 (buf->contentIO != NULL)) {
6978 xmlFree(buf->contentIO);
6979 } else if ((buf->content != NULL) &&
Daniel Veillard53350552003-09-18 13:35:51 +00006980 (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006981 xmlFree(buf->content);
6982 }
Owen Taylor3473f882001-02-23 17:55:21 +00006983 xmlFree(buf);
6984}
6985
6986/**
6987 * xmlBufferEmpty:
6988 * @buf: the buffer
6989 *
6990 * empty a buffer.
6991 */
6992void
6993xmlBufferEmpty(xmlBufferPtr buf) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00006994 if (buf == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +00006995 if (buf->content == NULL) return;
6996 buf->use = 0;
Daniel Veillard53350552003-09-18 13:35:51 +00006997 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00006998 buf->content = BAD_CAST "";
Daniel Veillarde83e93e2008-08-30 12:52:26 +00006999 } else if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
7000 (buf->contentIO != NULL)) {
7001 size_t start_buf = buf->content - buf->contentIO;
7002
7003 buf->size += start_buf;
7004 buf->content = buf->contentIO;
7005 buf->content[0] = 0;
Daniel Veillard53350552003-09-18 13:35:51 +00007006 } else {
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007007 buf->content[0] = 0;
7008 }
Owen Taylor3473f882001-02-23 17:55:21 +00007009}
7010
7011/**
7012 * xmlBufferShrink:
7013 * @buf: the buffer to dump
7014 * @len: the number of xmlChar to remove
7015 *
7016 * Remove the beginning of an XML buffer.
7017 *
Daniel Veillardd1640922001-12-17 15:30:10 +00007018 * Returns the number of #xmlChar removed, or -1 in case of failure.
Owen Taylor3473f882001-02-23 17:55:21 +00007019 */
7020int
7021xmlBufferShrink(xmlBufferPtr buf, unsigned int len) {
Daniel Veillard3d97e662004-11-04 10:49:00 +00007022 if (buf == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00007023 if (len == 0) return(0);
7024 if (len > buf->use) return(-1);
7025
7026 buf->use -= len;
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007027 if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) ||
7028 ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL))) {
7029 /*
7030 * we just move the content pointer, but also make sure
7031 * the perceived buffer size has shrinked accordingly
7032 */
Daniel Veillard53350552003-09-18 13:35:51 +00007033 buf->content += len;
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007034 buf->size -= len;
7035
7036 /*
7037 * sometimes though it maybe be better to really shrink
7038 * on IO buffers
7039 */
7040 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7041 size_t start_buf = buf->content - buf->contentIO;
7042 if (start_buf >= buf->size) {
7043 memmove(buf->contentIO, &buf->content[0], buf->use);
7044 buf->content = buf->contentIO;
7045 buf->content[buf->use] = 0;
7046 buf->size += start_buf;
7047 }
7048 }
Daniel Veillard53350552003-09-18 13:35:51 +00007049 } else {
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007050 memmove(buf->content, &buf->content[len], buf->use);
Daniel Veillard53350552003-09-18 13:35:51 +00007051 buf->content[buf->use] = 0;
7052 }
Owen Taylor3473f882001-02-23 17:55:21 +00007053 return(len);
7054}
7055
7056/**
7057 * xmlBufferGrow:
7058 * @buf: the buffer
7059 * @len: the minimum free size to allocate
7060 *
7061 * Grow the available space of an XML buffer.
7062 *
7063 * Returns the new available space or -1 in case of error
7064 */
7065int
7066xmlBufferGrow(xmlBufferPtr buf, unsigned int len) {
7067 int size;
7068 xmlChar *newbuf;
7069
Daniel Veillard3d97e662004-11-04 10:49:00 +00007070 if (buf == NULL) return(-1);
7071
Daniel Veillard53350552003-09-18 13:35:51 +00007072 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00007073 if (len + buf->use < buf->size) return(0);
7074
William M. Brack30fe43f2004-07-26 18:00:58 +00007075/*
7076 * Windows has a BIG problem on realloc timing, so we try to double
7077 * the buffer size (if that's enough) (bug 146697)
7078 */
7079#ifdef WIN32
7080 if (buf->size > len)
7081 size = buf->size * 2;
7082 else
7083 size = buf->use + len + 100;
7084#else
Owen Taylor3473f882001-02-23 17:55:21 +00007085 size = buf->use + len + 100;
William M. Brack30fe43f2004-07-26 18:00:58 +00007086#endif
Owen Taylor3473f882001-02-23 17:55:21 +00007087
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007088 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7089 size_t start_buf = buf->content - buf->contentIO;
7090
7091 newbuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + size);
7092 if (newbuf == NULL) {
7093 xmlTreeErrMemory("growing buffer");
7094 return(-1);
7095 }
7096 buf->contentIO = newbuf;
7097 buf->content = newbuf + start_buf;
7098 } else {
7099 newbuf = (xmlChar *) xmlRealloc(buf->content, size);
7100 if (newbuf == NULL) {
7101 xmlTreeErrMemory("growing buffer");
7102 return(-1);
7103 }
7104 buf->content = newbuf;
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007105 }
Owen Taylor3473f882001-02-23 17:55:21 +00007106 buf->size = size;
7107 return(buf->size - buf->use);
7108}
7109
7110/**
7111 * xmlBufferDump:
7112 * @file: the file output
7113 * @buf: the buffer to dump
7114 *
7115 * Dumps an XML buffer to a FILE *.
Daniel Veillardd1640922001-12-17 15:30:10 +00007116 * Returns the number of #xmlChar written
Owen Taylor3473f882001-02-23 17:55:21 +00007117 */
7118int
7119xmlBufferDump(FILE *file, xmlBufferPtr buf) {
7120 int ret;
7121
7122 if (buf == NULL) {
7123#ifdef DEBUG_BUFFER
7124 xmlGenericError(xmlGenericErrorContext,
7125 "xmlBufferDump: buf == NULL\n");
7126#endif
7127 return(0);
7128 }
7129 if (buf->content == NULL) {
7130#ifdef DEBUG_BUFFER
7131 xmlGenericError(xmlGenericErrorContext,
7132 "xmlBufferDump: buf->content == NULL\n");
7133#endif
7134 return(0);
7135 }
Daniel Veillardcd337f02001-11-22 18:20:37 +00007136 if (file == NULL)
7137 file = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00007138 ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
7139 return(ret);
7140}
7141
7142/**
7143 * xmlBufferContent:
7144 * @buf: the buffer
7145 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00007146 * Function to extract the content of a buffer
7147 *
Owen Taylor3473f882001-02-23 17:55:21 +00007148 * Returns the internal content
7149 */
7150
Daniel Veillard5e2dace2001-07-18 19:30:27 +00007151const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00007152xmlBufferContent(const xmlBufferPtr buf)
7153{
7154 if(!buf)
7155 return NULL;
7156
7157 return buf->content;
7158}
7159
7160/**
7161 * xmlBufferLength:
Daniel Veillardaa6de472008-08-25 14:53:31 +00007162 * @buf: the buffer
Owen Taylor3473f882001-02-23 17:55:21 +00007163 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00007164 * Function to get the length of a buffer
7165 *
Owen Taylor3473f882001-02-23 17:55:21 +00007166 * Returns the length of data in the internal content
7167 */
7168
7169int
7170xmlBufferLength(const xmlBufferPtr buf)
7171{
7172 if(!buf)
7173 return 0;
7174
7175 return buf->use;
7176}
7177
7178/**
7179 * xmlBufferResize:
7180 * @buf: the buffer to resize
7181 * @size: the desired size
7182 *
Daniel Veillardd1640922001-12-17 15:30:10 +00007183 * Resize a buffer to accommodate minimum size of @size.
Owen Taylor3473f882001-02-23 17:55:21 +00007184 *
7185 * Returns 0 in case of problems, 1 otherwise
7186 */
7187int
7188xmlBufferResize(xmlBufferPtr buf, unsigned int size)
7189{
7190 unsigned int newSize;
7191 xmlChar* rebuf = NULL;
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007192 size_t start_buf;
Owen Taylor3473f882001-02-23 17:55:21 +00007193
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007194 if (buf == NULL)
7195 return(0);
7196
Daniel Veillard53350552003-09-18 13:35:51 +00007197 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
7198
Owen Taylor3473f882001-02-23 17:55:21 +00007199 /* Don't resize if we don't have to */
7200 if (size < buf->size)
7201 return 1;
7202
7203 /* figure out new size */
7204 switch (buf->alloc){
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007205 case XML_BUFFER_ALLOC_IO:
7206 case XML_BUFFER_ALLOC_DOUBLEIT:
7207 /*take care of empty case*/
7208 newSize = (buf->size ? buf->size*2 : size + 10);
Daniel Veillard1dc9feb2008-11-17 15:59:21 +00007209 while (size > newSize) {
7210 if (newSize > UINT_MAX / 2) {
7211 xmlTreeErrMemory("growing buffer");
7212 return 0;
7213 }
7214 newSize *= 2;
7215 }
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007216 break;
7217 case XML_BUFFER_ALLOC_EXACT:
7218 newSize = size+10;
7219 break;
7220 default:
7221 newSize = size+10;
7222 break;
Owen Taylor3473f882001-02-23 17:55:21 +00007223 }
7224
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007225 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7226 start_buf = buf->content - buf->contentIO;
7227
7228 if (start_buf > newSize) {
7229 /* move data back to start */
7230 memmove(buf->contentIO, buf->content, buf->use);
7231 buf->content = buf->contentIO;
7232 buf->content[buf->use] = 0;
7233 buf->size += start_buf;
7234 } else {
7235 rebuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + newSize);
7236 if (rebuf == NULL) {
7237 xmlTreeErrMemory("growing buffer");
7238 return 0;
7239 }
7240 buf->contentIO = rebuf;
7241 buf->content = rebuf + start_buf;
Daniel Veillard6155d8a2003-08-19 15:01:28 +00007242 }
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007243 } else {
7244 if (buf->content == NULL) {
7245 rebuf = (xmlChar *) xmlMallocAtomic(newSize);
7246 } else if (buf->size - buf->use < 100) {
7247 rebuf = (xmlChar *) xmlRealloc(buf->content, newSize);
7248 } else {
7249 /*
7250 * if we are reallocating a buffer far from being full, it's
7251 * better to make a new allocation and copy only the used range
7252 * and free the old one.
7253 */
7254 rebuf = (xmlChar *) xmlMallocAtomic(newSize);
7255 if (rebuf != NULL) {
7256 memcpy(rebuf, buf->content, buf->use);
7257 xmlFree(buf->content);
7258 rebuf[buf->use] = 0;
7259 }
7260 }
7261 if (rebuf == NULL) {
7262 xmlTreeErrMemory("growing buffer");
7263 return 0;
7264 }
7265 buf->content = rebuf;
Daniel Veillard6155d8a2003-08-19 15:01:28 +00007266 }
Owen Taylor3473f882001-02-23 17:55:21 +00007267 buf->size = newSize;
7268
7269 return 1;
7270}
7271
7272/**
7273 * xmlBufferAdd:
7274 * @buf: the buffer to dump
Daniel Veillardd1640922001-12-17 15:30:10 +00007275 * @str: the #xmlChar string
7276 * @len: the number of #xmlChar to add
Owen Taylor3473f882001-02-23 17:55:21 +00007277 *
Daniel Veillard60087f32001-10-10 09:45:09 +00007278 * Add a string range to an XML buffer. if len == -1, the length of
Owen Taylor3473f882001-02-23 17:55:21 +00007279 * str is recomputed.
William M. Bracka3215c72004-07-31 16:24:01 +00007280 *
7281 * Returns 0 successful, a positive error code number otherwise
7282 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00007283 */
William M. Bracka3215c72004-07-31 16:24:01 +00007284int
Owen Taylor3473f882001-02-23 17:55:21 +00007285xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) {
7286 unsigned int needSize;
7287
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007288 if ((str == NULL) || (buf == NULL)) {
William M. Bracka3215c72004-07-31 16:24:01 +00007289 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007290 }
William M. Bracka3215c72004-07-31 16:24:01 +00007291 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007292 if (len < -1) {
7293#ifdef DEBUG_BUFFER
7294 xmlGenericError(xmlGenericErrorContext,
7295 "xmlBufferAdd: len < 0\n");
7296#endif
William M. Bracka3215c72004-07-31 16:24:01 +00007297 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007298 }
William M. Bracka3215c72004-07-31 16:24:01 +00007299 if (len == 0) return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007300
7301 if (len < 0)
7302 len = xmlStrlen(str);
7303
Daniel Veillardc9923322007-04-24 18:12:06 +00007304 if (len < 0) return -1;
7305 if (len == 0) return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007306
7307 needSize = buf->use + len + 2;
7308 if (needSize > buf->size){
7309 if (!xmlBufferResize(buf, needSize)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007310 xmlTreeErrMemory("growing buffer");
William M. Bracka3215c72004-07-31 16:24:01 +00007311 return XML_ERR_NO_MEMORY;
Owen Taylor3473f882001-02-23 17:55:21 +00007312 }
7313 }
7314
7315 memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
7316 buf->use += len;
7317 buf->content[buf->use] = 0;
William M. Bracka3215c72004-07-31 16:24:01 +00007318 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007319}
7320
7321/**
7322 * xmlBufferAddHead:
7323 * @buf: the buffer
Daniel Veillardd1640922001-12-17 15:30:10 +00007324 * @str: the #xmlChar string
7325 * @len: the number of #xmlChar to add
Owen Taylor3473f882001-02-23 17:55:21 +00007326 *
7327 * Add a string range to the beginning of an XML buffer.
Daniel Veillard60087f32001-10-10 09:45:09 +00007328 * if len == -1, the length of @str is recomputed.
William M. Bracka3215c72004-07-31 16:24:01 +00007329 *
7330 * Returns 0 successful, a positive error code number otherwise
7331 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00007332 */
William M. Bracka3215c72004-07-31 16:24:01 +00007333int
Owen Taylor3473f882001-02-23 17:55:21 +00007334xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) {
7335 unsigned int needSize;
7336
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007337 if (buf == NULL)
7338 return(-1);
William M. Bracka3215c72004-07-31 16:24:01 +00007339 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007340 if (str == NULL) {
7341#ifdef DEBUG_BUFFER
7342 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007343 "xmlBufferAddHead: str == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007344#endif
William M. Bracka3215c72004-07-31 16:24:01 +00007345 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007346 }
7347 if (len < -1) {
7348#ifdef DEBUG_BUFFER
7349 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007350 "xmlBufferAddHead: len < 0\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007351#endif
William M. Bracka3215c72004-07-31 16:24:01 +00007352 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007353 }
William M. Bracka3215c72004-07-31 16:24:01 +00007354 if (len == 0) return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007355
7356 if (len < 0)
7357 len = xmlStrlen(str);
7358
William M. Bracka3215c72004-07-31 16:24:01 +00007359 if (len <= 0) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007360
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007361 if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
7362 size_t start_buf = buf->content - buf->contentIO;
7363
7364 if (start_buf > (unsigned int) len) {
7365 /*
7366 * We can add it in the space previously shrinked
7367 */
7368 buf->content -= len;
7369 memmove(&buf->content[0], str, len);
7370 buf->use += len;
7371 buf->size += len;
7372 return(0);
7373 }
7374 }
Owen Taylor3473f882001-02-23 17:55:21 +00007375 needSize = buf->use + len + 2;
7376 if (needSize > buf->size){
7377 if (!xmlBufferResize(buf, needSize)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007378 xmlTreeErrMemory("growing buffer");
William M. Bracka3215c72004-07-31 16:24:01 +00007379 return XML_ERR_NO_MEMORY;
Owen Taylor3473f882001-02-23 17:55:21 +00007380 }
7381 }
7382
Daniel Veillarde83e93e2008-08-30 12:52:26 +00007383 memmove(&buf->content[len], &buf->content[0], buf->use);
7384 memmove(&buf->content[0], str, len);
Owen Taylor3473f882001-02-23 17:55:21 +00007385 buf->use += len;
7386 buf->content[buf->use] = 0;
William M. Bracka3215c72004-07-31 16:24:01 +00007387 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007388}
7389
7390/**
7391 * xmlBufferCat:
William M. Bracka3215c72004-07-31 16:24:01 +00007392 * @buf: the buffer to add to
Daniel Veillardd1640922001-12-17 15:30:10 +00007393 * @str: the #xmlChar string
Owen Taylor3473f882001-02-23 17:55:21 +00007394 *
7395 * Append a zero terminated string to an XML buffer.
William M. Bracka3215c72004-07-31 16:24:01 +00007396 *
7397 * Returns 0 successful, a positive error code number otherwise
7398 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00007399 */
William M. Bracka3215c72004-07-31 16:24:01 +00007400int
Owen Taylor3473f882001-02-23 17:55:21 +00007401xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007402 if (buf == NULL)
7403 return(-1);
William M. Bracka3215c72004-07-31 16:24:01 +00007404 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7405 if (str == NULL) return -1;
7406 return xmlBufferAdd(buf, str, -1);
Owen Taylor3473f882001-02-23 17:55:21 +00007407}
7408
7409/**
7410 * xmlBufferCCat:
7411 * @buf: the buffer to dump
7412 * @str: the C char string
7413 *
7414 * Append a zero terminated C string to an XML buffer.
William M. Bracka3215c72004-07-31 16:24:01 +00007415 *
7416 * Returns 0 successful, a positive error code number otherwise
7417 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00007418 */
William M. Bracka3215c72004-07-31 16:24:01 +00007419int
Owen Taylor3473f882001-02-23 17:55:21 +00007420xmlBufferCCat(xmlBufferPtr buf, const char *str) {
7421 const char *cur;
7422
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007423 if (buf == NULL)
7424 return(-1);
William M. Bracka3215c72004-07-31 16:24:01 +00007425 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007426 if (str == NULL) {
7427#ifdef DEBUG_BUFFER
7428 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007429 "xmlBufferCCat: str == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007430#endif
William M. Bracka3215c72004-07-31 16:24:01 +00007431 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007432 }
7433 for (cur = str;*cur != 0;cur++) {
7434 if (buf->use + 10 >= buf->size) {
7435 if (!xmlBufferResize(buf, buf->use+10)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007436 xmlTreeErrMemory("growing buffer");
William M. Bracka3215c72004-07-31 16:24:01 +00007437 return XML_ERR_NO_MEMORY;
Owen Taylor3473f882001-02-23 17:55:21 +00007438 }
7439 }
7440 buf->content[buf->use++] = *cur;
7441 }
7442 buf->content[buf->use] = 0;
William M. Bracka3215c72004-07-31 16:24:01 +00007443 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007444}
7445
7446/**
7447 * xmlBufferWriteCHAR:
7448 * @buf: the XML buffer
7449 * @string: the string to add
7450 *
7451 * routine which manages and grows an output buffer. This one adds
7452 * xmlChars at the end of the buffer.
7453 */
7454void
Daniel Veillard53350552003-09-18 13:35:51 +00007455xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007456 if (buf == NULL)
7457 return;
Daniel Veillard53350552003-09-18 13:35:51 +00007458 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00007459 xmlBufferCat(buf, string);
7460}
7461
7462/**
7463 * xmlBufferWriteChar:
7464 * @buf: the XML buffer output
7465 * @string: the string to add
7466 *
7467 * routine which manage and grows an output buffer. This one add
7468 * C chars at the end of the array.
7469 */
7470void
7471xmlBufferWriteChar(xmlBufferPtr buf, const char *string) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007472 if (buf == NULL)
7473 return;
Daniel Veillard53350552003-09-18 13:35:51 +00007474 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00007475 xmlBufferCCat(buf, string);
7476}
7477
7478
7479/**
7480 * xmlBufferWriteQuotedString:
7481 * @buf: the XML buffer output
7482 * @string: the string to add
7483 *
7484 * routine which manage and grows an output buffer. This one writes
Daniel Veillardd1640922001-12-17 15:30:10 +00007485 * a quoted or double quoted #xmlChar string, checking first if it holds
Owen Taylor3473f882001-02-23 17:55:21 +00007486 * quote or double-quotes internally
7487 */
7488void
7489xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) {
Daniel Veillard39057f42003-08-04 01:33:43 +00007490 const xmlChar *cur, *base;
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007491 if (buf == NULL)
7492 return;
Daniel Veillard53350552003-09-18 13:35:51 +00007493 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Daniel Veillard39057f42003-08-04 01:33:43 +00007494 if (xmlStrchr(string, '\"')) {
Daniel Veillard20aa0fb2003-08-04 19:43:15 +00007495 if (xmlStrchr(string, '\'')) {
Owen Taylor3473f882001-02-23 17:55:21 +00007496#ifdef DEBUG_BUFFER
7497 xmlGenericError(xmlGenericErrorContext,
7498 "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n");
7499#endif
Daniel Veillard39057f42003-08-04 01:33:43 +00007500 xmlBufferCCat(buf, "\"");
7501 base = cur = string;
7502 while(*cur != 0){
7503 if(*cur == '"'){
7504 if (base != cur)
7505 xmlBufferAdd(buf, base, cur - base);
7506 xmlBufferAdd(buf, BAD_CAST "&quot;", 6);
7507 cur++;
7508 base = cur;
7509 }
7510 else {
7511 cur++;
7512 }
7513 }
7514 if (base != cur)
7515 xmlBufferAdd(buf, base, cur - base);
7516 xmlBufferCCat(buf, "\"");
Owen Taylor3473f882001-02-23 17:55:21 +00007517 }
Daniel Veillard39057f42003-08-04 01:33:43 +00007518 else{
7519 xmlBufferCCat(buf, "\'");
7520 xmlBufferCat(buf, string);
7521 xmlBufferCCat(buf, "\'");
7522 }
Owen Taylor3473f882001-02-23 17:55:21 +00007523 } else {
7524 xmlBufferCCat(buf, "\"");
7525 xmlBufferCat(buf, string);
7526 xmlBufferCCat(buf, "\"");
7527 }
7528}
7529
7530
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007531/**
7532 * xmlGetDocCompressMode:
7533 * @doc: the document
7534 *
7535 * get the compression ratio for a document, ZLIB based
7536 * Returns 0 (uncompressed) to 9 (max compression)
7537 */
7538int
7539xmlGetDocCompressMode (xmlDocPtr doc) {
7540 if (doc == NULL) return(-1);
7541 return(doc->compression);
7542}
7543
7544/**
7545 * xmlSetDocCompressMode:
7546 * @doc: the document
7547 * @mode: the compression ratio
7548 *
7549 * set the compression ratio for a document, ZLIB based
7550 * Correct values: 0 (uncompressed) to 9 (max compression)
7551 */
7552void
7553xmlSetDocCompressMode (xmlDocPtr doc, int mode) {
7554 if (doc == NULL) return;
7555 if (mode < 0) doc->compression = 0;
7556 else if (mode > 9) doc->compression = 9;
7557 else doc->compression = mode;
7558}
7559
7560/**
7561 * xmlGetCompressMode:
7562 *
7563 * get the default compression mode used, ZLIB based.
7564 * Returns 0 (uncompressed) to 9 (max compression)
7565 */
7566int
7567xmlGetCompressMode(void)
7568{
7569 return (xmlCompressMode);
7570}
7571
7572/**
7573 * xmlSetCompressMode:
7574 * @mode: the compression ratio
7575 *
7576 * set the default compression mode used, ZLIB based
7577 * Correct values: 0 (uncompressed) to 9 (max compression)
7578 */
7579void
7580xmlSetCompressMode(int mode) {
7581 if (mode < 0) xmlCompressMode = 0;
7582 else if (mode > 9) xmlCompressMode = 9;
7583 else xmlCompressMode = mode;
7584}
7585
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007586#define XML_TREE_NSMAP_PARENT -1
7587#define XML_TREE_NSMAP_XML -2
7588#define XML_TREE_NSMAP_DOC -3
7589#define XML_TREE_NSMAP_CUSTOM -4
7590
7591typedef struct xmlNsMapItem *xmlNsMapItemPtr;
7592struct xmlNsMapItem {
7593 xmlNsMapItemPtr next;
7594 xmlNsMapItemPtr prev;
7595 xmlNsPtr oldNs; /* old ns decl reference */
7596 xmlNsPtr newNs; /* new ns decl reference */
7597 int shadowDepth; /* Shadowed at this depth */
7598 /*
7599 * depth:
7600 * >= 0 == @node's ns-decls
7601 * -1 == @parent's ns-decls
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00007602 * -2 == the doc->oldNs XML ns-decl
7603 * -3 == the doc->oldNs storage ns-decls
7604 * -4 == ns-decls provided via custom ns-handling
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007605 */
7606 int depth;
7607};
7608
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007609typedef struct xmlNsMap *xmlNsMapPtr;
7610struct xmlNsMap {
7611 xmlNsMapItemPtr first;
7612 xmlNsMapItemPtr last;
7613 xmlNsMapItemPtr pool;
7614};
7615
7616#define XML_NSMAP_NOTEMPTY(m) (((m) != NULL) && ((m)->first != NULL))
7617#define XML_NSMAP_FOREACH(m, i) for (i = (m)->first; i != NULL; i = (i)->next)
7618#define XML_NSMAP_POP(m, i) \
7619 i = (m)->last; \
7620 (m)->last = (i)->prev; \
7621 if ((m)->last == NULL) \
7622 (m)->first = NULL; \
7623 else \
7624 (m)->last->next = NULL; \
7625 (i)->next = (m)->pool; \
7626 (m)->pool = i;
7627
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007628/*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007629* xmlDOMWrapNsMapFree:
7630* @map: the ns-map
Daniel Veillardaa6de472008-08-25 14:53:31 +00007631*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007632* Frees the ns-map
7633*/
7634static void
7635xmlDOMWrapNsMapFree(xmlNsMapPtr nsmap)
7636{
7637 xmlNsMapItemPtr cur, tmp;
7638
7639 if (nsmap == NULL)
7640 return;
7641 cur = nsmap->pool;
7642 while (cur != NULL) {
7643 tmp = cur;
7644 cur = cur->next;
7645 xmlFree(tmp);
7646 }
7647 cur = nsmap->first;
7648 while (cur != NULL) {
7649 tmp = cur;
7650 cur = cur->next;
7651 xmlFree(tmp);
7652 }
7653 xmlFree(nsmap);
7654}
7655
7656/*
7657* xmlDOMWrapNsMapAddItem:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007658* @map: the ns-map
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007659* @oldNs: the old ns-struct
7660* @newNs: the new ns-struct
7661* @depth: depth and ns-kind information
Daniel Veillardaa6de472008-08-25 14:53:31 +00007662*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007663* Adds an ns-mapping item.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007664*/
7665static xmlNsMapItemPtr
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00007666xmlDOMWrapNsMapAddItem(xmlNsMapPtr *nsmap, int position,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007667 xmlNsPtr oldNs, xmlNsPtr newNs, int depth)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007668{
7669 xmlNsMapItemPtr ret;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007670 xmlNsMapPtr map;
7671
7672 if (nsmap == NULL)
7673 return(NULL);
7674 if ((position != -1) && (position != 0))
7675 return(NULL);
7676 map = *nsmap;
7677
7678 if (map == NULL) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007679 /*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007680 * Create the ns-map.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007681 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007682 map = (xmlNsMapPtr) xmlMalloc(sizeof(struct xmlNsMap));
7683 if (map == NULL) {
7684 xmlTreeErrMemory("allocating namespace map");
7685 return (NULL);
7686 }
7687 memset(map, 0, sizeof(struct xmlNsMap));
7688 *nsmap = map;
7689 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00007690
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007691 if (map->pool != NULL) {
7692 /*
7693 * Reuse an item from the pool.
7694 */
7695 ret = map->pool;
7696 map->pool = ret->next;
7697 memset(ret, 0, sizeof(struct xmlNsMapItem));
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007698 } else {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007699 /*
7700 * Create a new item.
7701 */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007702 ret = (xmlNsMapItemPtr) xmlMalloc(sizeof(struct xmlNsMapItem));
7703 if (ret == NULL) {
7704 xmlTreeErrMemory("allocating namespace map item");
7705 return (NULL);
7706 }
7707 memset(ret, 0, sizeof(struct xmlNsMapItem));
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007708 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00007709
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007710 if (map->first == NULL) {
7711 /*
7712 * First ever.
Daniel Veillardaa6de472008-08-25 14:53:31 +00007713 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007714 map->first = ret;
7715 map->last = ret;
7716 } else if (position == -1) {
7717 /*
7718 * Append.
7719 */
7720 ret->prev = map->last;
7721 map->last->next = ret;
Daniel Veillardaa6de472008-08-25 14:53:31 +00007722 map->last = ret;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007723 } else if (position == 0) {
7724 /*
7725 * Set on first position.
7726 */
7727 map->first->prev = ret;
Daniel Veillardaa6de472008-08-25 14:53:31 +00007728 ret->next = map->first;
7729 map->first = ret;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007730 } else
7731 return(NULL);
7732
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007733 ret->oldNs = oldNs;
7734 ret->newNs = newNs;
7735 ret->shadowDepth = -1;
7736 ret->depth = depth;
7737 return (ret);
7738}
7739
7740/*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007741* xmlDOMWrapStoreNs:
7742* @doc: the doc
7743* @nsName: the namespace name
7744* @prefix: the prefix
Daniel Veillardaa6de472008-08-25 14:53:31 +00007745*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007746* Creates or reuses an xmlNs struct on doc->oldNs with
7747* the given prefix and namespace name.
Daniel Veillardaa6de472008-08-25 14:53:31 +00007748*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007749* Returns the aquired ns struct or NULL in case of an API
7750* or internal error.
7751*/
7752static xmlNsPtr
7753xmlDOMWrapStoreNs(xmlDocPtr doc,
7754 const xmlChar *nsName,
7755 const xmlChar *prefix)
7756{
7757 xmlNsPtr ns;
7758
7759 if (doc == NULL)
7760 return (NULL);
7761 ns = xmlTreeEnsureXMLDecl(doc);
7762 if (ns == NULL)
7763 return (NULL);
7764 if (ns->next != NULL) {
7765 /* Reuse. */
7766 ns = ns->next;
7767 while (ns != NULL) {
7768 if (((ns->prefix == prefix) ||
7769 xmlStrEqual(ns->prefix, prefix)) &&
7770 xmlStrEqual(ns->href, nsName)) {
7771 return (ns);
7772 }
7773 if (ns->next == NULL)
7774 break;
7775 ns = ns->next;
7776 }
7777 }
7778 /* Create. */
7779 ns->next = xmlNewNs(NULL, nsName, prefix);
7780 return (ns->next);
7781}
7782
7783/*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00007784* xmlDOMWrapNewCtxt:
7785*
7786* Allocates and initializes a new DOM-wrapper context.
7787*
Daniel Veillardaa6de472008-08-25 14:53:31 +00007788* Returns the xmlDOMWrapCtxtPtr or NULL in case of an internal errror.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00007789*/
7790xmlDOMWrapCtxtPtr
7791xmlDOMWrapNewCtxt(void)
7792{
7793 xmlDOMWrapCtxtPtr ret;
7794
7795 ret = xmlMalloc(sizeof(xmlDOMWrapCtxt));
7796 if (ret == NULL) {
7797 xmlTreeErrMemory("allocating DOM-wrapper context");
7798 return (NULL);
7799 }
7800 memset(ret, 0, sizeof(xmlDOMWrapCtxt));
7801 return (ret);
7802}
7803
7804/*
7805* xmlDOMWrapFreeCtxt:
7806* @ctxt: the DOM-wrapper context
7807*
7808* Frees the DOM-wrapper context.
7809*/
7810void
7811xmlDOMWrapFreeCtxt(xmlDOMWrapCtxtPtr ctxt)
7812{
7813 if (ctxt == NULL)
7814 return;
7815 if (ctxt->namespaceMap != NULL)
7816 xmlDOMWrapNsMapFree((xmlNsMapPtr) ctxt->namespaceMap);
7817 /*
7818 * TODO: Store the namespace map in the context.
7819 */
7820 xmlFree(ctxt);
7821}
7822
7823/*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007824* xmlTreeLookupNsListByPrefix:
7825* @nsList: a list of ns-structs
7826* @prefix: the searched prefix
Daniel Veillardaa6de472008-08-25 14:53:31 +00007827*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007828* Searches for a ns-decl with the given prefix in @nsList.
Daniel Veillardaa6de472008-08-25 14:53:31 +00007829*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007830* Returns the ns-decl if found, NULL if not found and on
7831* API errors.
7832*/
7833static xmlNsPtr
7834xmlTreeNSListLookupByPrefix(xmlNsPtr nsList, const xmlChar *prefix)
7835{
7836 if (nsList == NULL)
7837 return (NULL);
7838 {
7839 xmlNsPtr ns;
7840 ns = nsList;
7841 do {
7842 if ((prefix == ns->prefix) ||
7843 xmlStrEqual(prefix, ns->prefix)) {
7844 return (ns);
7845 }
7846 ns = ns->next;
7847 } while (ns != NULL);
7848 }
7849 return (NULL);
7850}
7851
7852/*
7853*
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00007854* xmlDOMWrapNSNormGatherInScopeNs:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007855* @map: the namespace map
7856* @node: the node to start with
Daniel Veillardaa6de472008-08-25 14:53:31 +00007857*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007858* Puts in-scope namespaces into the ns-map.
Daniel Veillardaa6de472008-08-25 14:53:31 +00007859*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007860* Returns 0 on success, -1 on API or internal errors.
7861*/
7862static int
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007863xmlDOMWrapNSNormGatherInScopeNs(xmlNsMapPtr *map,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007864 xmlNodePtr node)
7865{
7866 xmlNodePtr cur;
7867 xmlNsPtr ns;
7868 xmlNsMapItemPtr mi;
7869 int shadowed;
7870
7871 if ((map == NULL) || (*map != NULL))
7872 return (-1);
7873 /*
7874 * Get in-scope ns-decls of @parent.
7875 */
7876 cur = node;
7877 while ((cur != NULL) && (cur != (xmlNodePtr) cur->doc)) {
7878 if (cur->type == XML_ELEMENT_NODE) {
7879 if (cur->nsDef != NULL) {
7880 ns = cur->nsDef;
7881 do {
7882 shadowed = 0;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007883 if (XML_NSMAP_NOTEMPTY(*map)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007884 /*
7885 * Skip shadowed prefixes.
7886 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007887 XML_NSMAP_FOREACH(*map, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007888 if ((ns->prefix == mi->newNs->prefix) ||
7889 xmlStrEqual(ns->prefix, mi->newNs->prefix)) {
7890 shadowed = 1;
7891 break;
7892 }
7893 }
7894 }
7895 /*
7896 * Insert mapping.
7897 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007898 mi = xmlDOMWrapNsMapAddItem(map, 0, NULL,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007899 ns, XML_TREE_NSMAP_PARENT);
7900 if (mi == NULL)
7901 return (-1);
7902 if (shadowed)
7903 mi->shadowDepth = 0;
7904 ns = ns->next;
7905 } while (ns != NULL);
7906 }
7907 }
7908 cur = cur->parent;
7909 }
7910 return (0);
7911}
7912
7913/*
7914* XML_TREE_ADOPT_STR: If we have a dest-dict, put @str in the dict;
7915* otherwise copy it, when it was in the source-dict.
7916*/
7917#define XML_TREE_ADOPT_STR(str) \
7918 if (adoptStr && (str != NULL)) { \
7919 if (destDoc->dict) { \
Daniel Veillard7e21fd12005-07-03 21:44:07 +00007920 const xmlChar *old = str; \
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007921 str = xmlDictLookup(destDoc->dict, str, -1); \
Daniel Veillard7e21fd12005-07-03 21:44:07 +00007922 if ((sourceDoc == NULL) || (sourceDoc->dict == NULL) || \
7923 (!xmlDictOwns(sourceDoc->dict, old))) \
Daniel Veillard39e5c892005-07-03 22:48:50 +00007924 xmlFree((char *)old); \
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007925 } else if ((sourceDoc) && (sourceDoc->dict) && \
7926 xmlDictOwns(sourceDoc->dict, str)) { \
7927 str = BAD_CAST xmlStrdup(str); \
7928 } \
7929 }
7930
7931/*
7932* XML_TREE_ADOPT_STR_2: If @str was in the source-dict, then
7933* put it in dest-dict or copy it.
7934*/
7935#define XML_TREE_ADOPT_STR_2(str) \
7936 if (adoptStr && (str != NULL) && (sourceDoc != NULL) && \
7937 (sourceDoc->dict != NULL) && \
7938 xmlDictOwns(sourceDoc->dict, cur->content)) { \
7939 if (destDoc->dict) \
7940 cur->content = (xmlChar *) \
7941 xmlDictLookup(destDoc->dict, cur->content, -1); \
7942 else \
7943 cur->content = xmlStrdup(BAD_CAST cur->content); \
7944 }
7945
7946/*
7947* xmlDOMWrapNSNormAddNsMapItem2:
7948*
7949* For internal use. Adds a ns-decl mapping.
7950*
Daniel Veillardaa6de472008-08-25 14:53:31 +00007951* Returns 0 on success, -1 on internal errors.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007952*/
7953static int
7954xmlDOMWrapNSNormAddNsMapItem2(xmlNsPtr **list, int *size, int *number,
7955 xmlNsPtr oldNs, xmlNsPtr newNs)
7956{
7957 if (*list == NULL) {
7958 *list = (xmlNsPtr *) xmlMalloc(6 * sizeof(xmlNsPtr));
7959 if (*list == NULL) {
7960 xmlTreeErrMemory("alloc ns map item");
7961 return(-1);
7962 }
7963 *size = 3;
7964 *number = 0;
7965 } else if ((*number) >= (*size)) {
7966 *size *= 2;
7967 *list = (xmlNsPtr *) xmlRealloc(*list,
7968 (*size) * 2 * sizeof(xmlNsPtr));
7969 if (*list == NULL) {
7970 xmlTreeErrMemory("realloc ns map item");
7971 return(-1);
7972 }
7973 }
7974 (*list)[2 * (*number)] = oldNs;
7975 (*list)[2 * (*number) +1] = newNs;
7976 (*number)++;
7977 return (0);
7978}
7979
7980/*
7981* xmlDOMWrapRemoveNode:
Daniel Veillard304e78c2005-07-03 16:19:41 +00007982* @ctxt: a DOM wrapper context
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007983* @doc: the doc
7984* @node: the node to be removed.
Daniel Veillard304e78c2005-07-03 16:19:41 +00007985* @options: set of options, unused at the moment
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007986*
7987* Unlinks the given node from its owner.
7988* This will substitute ns-references to node->nsDef for
7989* ns-references to doc->oldNs, thus ensuring the removed
7990* branch to be autark wrt ns-references.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00007991*
7992* NOTE: This function was not intensively tested.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007993*
7994* Returns 0 on success, 1 if the node is not supported,
Daniel Veillardaa6de472008-08-25 14:53:31 +00007995* -1 on API and internal errors.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007996*/
7997int
7998xmlDOMWrapRemoveNode(xmlDOMWrapCtxtPtr ctxt, xmlDocPtr doc,
7999 xmlNodePtr node, int options ATTRIBUTE_UNUSED)
8000{
8001 xmlNsPtr *list = NULL;
8002 int sizeList, nbList, i, j;
8003 xmlNsPtr ns;
8004
8005 if ((node == NULL) || (doc == NULL) || (node->doc != doc))
8006 return (-1);
8007
8008 /* TODO: 0 or -1 ? */
8009 if (node->parent == NULL)
8010 return (0);
8011
Daniel Veillardaa6de472008-08-25 14:53:31 +00008012 switch (node->type) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008013 case XML_TEXT_NODE:
8014 case XML_CDATA_SECTION_NODE:
8015 case XML_ENTITY_REF_NODE:
8016 case XML_PI_NODE:
8017 case XML_COMMENT_NODE:
8018 xmlUnlinkNode(node);
8019 return (0);
Daniel Veillardaa6de472008-08-25 14:53:31 +00008020 case XML_ELEMENT_NODE:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008021 case XML_ATTRIBUTE_NODE:
8022 break;
8023 default:
8024 return (1);
8025 }
8026 xmlUnlinkNode(node);
8027 /*
8028 * Save out-of-scope ns-references in doc->oldNs.
8029 */
8030 do {
8031 switch (node->type) {
8032 case XML_ELEMENT_NODE:
8033 if ((ctxt == NULL) && (node->nsDef != NULL)) {
8034 ns = node->nsDef;
8035 do {
8036 if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
8037 &nbList, ns, ns) == -1)
8038 goto internal_error;
8039 ns = ns->next;
8040 } while (ns != NULL);
8041 }
8042 /* No break on purpose. */
8043 case XML_ATTRIBUTE_NODE:
Daniel Veillardaa6de472008-08-25 14:53:31 +00008044 if (node->ns != NULL) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008045 /*
8046 * Find a mapping.
8047 */
8048 if (list != NULL) {
8049 for (i = 0, j = 0; i < nbList; i++, j += 2) {
8050 if (node->ns == list[j]) {
8051 node->ns = list[++j];
8052 goto next_node;
8053 }
8054 }
8055 }
8056 ns = NULL;
8057 if (ctxt != NULL) {
8058 /*
8059 * User defined.
8060 */
8061 } else {
8062 /*
8063 * Add to doc's oldNs.
8064 */
8065 ns = xmlDOMWrapStoreNs(doc, node->ns->href,
8066 node->ns->prefix);
8067 if (ns == NULL)
8068 goto internal_error;
8069 }
8070 if (ns != NULL) {
8071 /*
8072 * Add mapping.
8073 */
8074 if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
8075 &nbList, node->ns, ns) == -1)
8076 goto internal_error;
8077 }
8078 node->ns = ns;
8079 }
8080 if ((node->type == XML_ELEMENT_NODE) &&
8081 (node->properties != NULL)) {
8082 node = (xmlNodePtr) node->properties;
8083 continue;
8084 }
8085 break;
8086 default:
8087 goto next_sibling;
Daniel Veillardaa6de472008-08-25 14:53:31 +00008088 }
8089next_node:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008090 if ((node->type == XML_ELEMENT_NODE) &&
8091 (node->children != NULL)) {
8092 node = node->children;
8093 continue;
8094 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00008095next_sibling:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008096 if (node == NULL)
8097 break;
8098 if (node->next != NULL)
8099 node = node->next;
8100 else {
8101 node = node->parent;
8102 goto next_sibling;
8103 }
8104 } while (node != NULL);
8105
8106 if (list != NULL)
8107 xmlFree(list);
8108 return (0);
8109
8110internal_error:
8111 if (list != NULL)
8112 xmlFree(list);
8113 return (-1);
8114}
8115
8116/*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008117* xmlSearchNsByNamespaceStrict:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008118* @doc: the document
8119* @node: the start node
8120* @nsName: the searched namespace name
8121* @retNs: the resulting ns-decl
8122* @prefixed: if the found ns-decl must have a prefix (for attributes)
8123*
8124* Dynamically searches for a ns-declaration which matches
8125* the given @nsName in the ancestor-or-self axis of @node.
8126*
8127* Returns 1 if a ns-decl was found, 0 if not and -1 on API
8128* and internal errors.
8129*/
8130static int
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008131xmlSearchNsByNamespaceStrict(xmlDocPtr doc, xmlNodePtr node,
8132 const xmlChar* nsName,
8133 xmlNsPtr *retNs, int prefixed)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008134{
8135 xmlNodePtr cur, prev = NULL, out = NULL;
8136 xmlNsPtr ns, prevns;
8137
8138 if ((doc == NULL) || (nsName == NULL) || (retNs == NULL))
8139 return (-1);
8140
8141 *retNs = NULL;
8142 if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) {
8143 *retNs = xmlTreeEnsureXMLDecl(doc);
8144 if (*retNs == NULL)
8145 return (-1);
8146 return (1);
8147 }
8148 cur = node;
8149 do {
8150 if (cur->type == XML_ELEMENT_NODE) {
8151 if (cur->nsDef != NULL) {
8152 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
8153 if (prefixed && (ns->prefix == NULL))
8154 continue;
8155 if (prev != NULL) {
8156 /*
8157 * Check the last level of ns-decls for a
8158 * shadowing prefix.
8159 */
8160 prevns = prev->nsDef;
8161 do {
8162 if ((prevns->prefix == ns->prefix) ||
8163 ((prevns->prefix != NULL) &&
8164 (ns->prefix != NULL) &&
8165 xmlStrEqual(prevns->prefix, ns->prefix))) {
8166 /*
8167 * Shadowed.
8168 */
8169 break;
8170 }
8171 prevns = prevns->next;
8172 } while (prevns != NULL);
8173 if (prevns != NULL)
8174 continue;
8175 }
8176 /*
8177 * Ns-name comparison.
8178 */
8179 if ((nsName == ns->href) ||
8180 xmlStrEqual(nsName, ns->href)) {
8181 /*
8182 * At this point the prefix can only be shadowed,
8183 * if we are the the (at least) 3rd level of
8184 * ns-decls.
8185 */
8186 if (out) {
8187 int ret;
Daniel Veillardaa6de472008-08-25 14:53:31 +00008188
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008189 ret = xmlNsInScope(doc, node, prev, ns->prefix);
8190 if (ret < 0)
8191 return (-1);
8192 /*
8193 * TODO: Should we try to find a matching ns-name
8194 * only once? This here keeps on searching.
8195 * I think we should try further since, there might
8196 * be an other matching ns-decl with an unshadowed
8197 * prefix.
8198 */
8199 if (! ret)
8200 continue;
8201 }
8202 *retNs = ns;
8203 return (1);
8204 }
8205 }
8206 out = prev;
8207 prev = cur;
8208 }
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008209 } else if ((cur->type == XML_ENTITY_NODE) ||
8210 (cur->type == XML_ENTITY_DECL))
8211 return (0);
8212 cur = cur->parent;
8213 } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
8214 return (0);
8215}
8216
8217/*
8218* xmlSearchNsByPrefixStrict:
8219* @doc: the document
8220* @node: the start node
8221* @prefix: the searched namespace prefix
8222* @retNs: the resulting ns-decl
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008223*
8224* Dynamically searches for a ns-declaration which matches
8225* the given @nsName in the ancestor-or-self axis of @node.
8226*
8227* Returns 1 if a ns-decl was found, 0 if not and -1 on API
8228* and internal errors.
8229*/
8230static int
8231xmlSearchNsByPrefixStrict(xmlDocPtr doc, xmlNodePtr node,
8232 const xmlChar* prefix,
8233 xmlNsPtr *retNs)
8234{
8235 xmlNodePtr cur;
8236 xmlNsPtr ns;
8237
8238 if ((doc == NULL) || (node == NULL))
8239 return (-1);
8240
8241 if (retNs)
8242 *retNs = NULL;
8243 if (IS_STR_XML(prefix)) {
8244 if (retNs) {
8245 *retNs = xmlTreeEnsureXMLDecl(doc);
8246 if (*retNs == NULL)
8247 return (-1);
8248 }
8249 return (1);
8250 }
8251 cur = node;
8252 do {
8253 if (cur->type == XML_ELEMENT_NODE) {
8254 if (cur->nsDef != NULL) {
8255 ns = cur->nsDef;
Daniel Veillardaa6de472008-08-25 14:53:31 +00008256 do {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008257 if ((prefix == ns->prefix) ||
8258 xmlStrEqual(prefix, ns->prefix))
8259 {
8260 /*
8261 * Disabled namespaces, e.g. xmlns:abc="".
8262 */
8263 if (ns->href == NULL)
8264 return(0);
8265 if (retNs)
8266 *retNs = ns;
8267 return (1);
8268 }
8269 ns = ns->next;
Daniel Veillardaa6de472008-08-25 14:53:31 +00008270 } while (ns != NULL);
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008271 }
8272 } else if ((cur->type == XML_ENTITY_NODE) ||
8273 (cur->type == XML_ENTITY_DECL))
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008274 return (0);
8275 cur = cur->parent;
8276 } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
8277 return (0);
8278}
8279
8280/*
8281* xmlDOMWrapNSNormDeclareNsForced:
8282* @doc: the doc
8283* @elem: the element-node to declare on
8284* @nsName: the namespace-name of the ns-decl
8285* @prefix: the preferred prefix of the ns-decl
8286* @checkShadow: ensure that the new ns-decl doesn't shadow ancestor ns-decls
8287*
8288* Declares a new namespace on @elem. It tries to use the
8289* given @prefix; if a ns-decl with the given prefix is already existent
8290* on @elem, it will generate an other prefix.
8291*
8292* Returns 1 if a ns-decl was found, 0 if not and -1 on API
8293* and internal errors.
8294*/
8295static xmlNsPtr
8296xmlDOMWrapNSNormDeclareNsForced(xmlDocPtr doc,
8297 xmlNodePtr elem,
8298 const xmlChar *nsName,
8299 const xmlChar *prefix,
8300 int checkShadow)
8301{
8302
8303 xmlNsPtr ret;
8304 char buf[50];
8305 const xmlChar *pref;
8306 int counter = 0;
8307 /*
8308 * Create a ns-decl on @anchor.
8309 */
8310 pref = prefix;
8311 while (1) {
8312 /*
8313 * Lookup whether the prefix is unused in elem's ns-decls.
8314 */
8315 if ((elem->nsDef != NULL) &&
8316 (xmlTreeNSListLookupByPrefix(elem->nsDef, pref) != NULL))
8317 goto ns_next_prefix;
8318 if (checkShadow && elem->parent &&
8319 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8320 /*
8321 * Does it shadow ancestor ns-decls?
8322 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008323 if (xmlSearchNsByPrefixStrict(doc, elem->parent, pref, NULL) == 1)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008324 goto ns_next_prefix;
8325 }
8326 ret = xmlNewNs(NULL, nsName, pref);
8327 if (ret == NULL)
8328 return (NULL);
8329 if (elem->nsDef == NULL)
8330 elem->nsDef = ret;
8331 else {
8332 xmlNsPtr ns2 = elem->nsDef;
8333 while (ns2->next != NULL)
8334 ns2 = ns2->next;
8335 ns2->next = ret;
8336 }
8337 return (ret);
8338ns_next_prefix:
8339 counter++;
8340 if (counter > 1000)
8341 return (NULL);
8342 if (prefix == NULL) {
8343 snprintf((char *) buf, sizeof(buf),
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008344 "ns_%d", counter);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008345 } else
8346 snprintf((char *) buf, sizeof(buf),
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008347 "%.30s_%d", (char *)prefix, counter);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008348 pref = BAD_CAST buf;
8349 }
8350}
8351
8352/*
8353* xmlDOMWrapNSNormAquireNormalizedNs:
8354* @doc: the doc
8355* @elem: the element-node to declare namespaces on
8356* @ns: the ns-struct to use for the search
8357* @retNs: the found/created ns-struct
8358* @nsMap: the ns-map
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008359* @depth: the current tree depth
8360* @ancestorsOnly: search in ancestor ns-decls only
8361* @prefixed: if the searched ns-decl must have a prefix (for attributes)
8362*
8363* Searches for a matching ns-name in the ns-decls of @nsMap, if not
8364* found it will either declare it on @elem, or store it in doc->oldNs.
8365* If a new ns-decl needs to be declared on @elem, it tries to use the
8366* @ns->prefix for it, if this prefix is already in use on @elem, it will
8367* change the prefix or the new ns-decl.
8368*
8369* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8370*/
8371static int
8372xmlDOMWrapNSNormAquireNormalizedNs(xmlDocPtr doc,
8373 xmlNodePtr elem,
8374 xmlNsPtr ns,
8375 xmlNsPtr *retNs,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008376 xmlNsMapPtr *nsMap,
Daniel Veillardaa6de472008-08-25 14:53:31 +00008377
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008378 int depth,
8379 int ancestorsOnly,
8380 int prefixed)
8381{
Daniel Veillardaa6de472008-08-25 14:53:31 +00008382 xmlNsMapItemPtr mi;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008383
8384 if ((doc == NULL) || (ns == NULL) || (retNs == NULL) ||
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008385 (nsMap == NULL))
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008386 return (-1);
8387
8388 *retNs = NULL;
8389 /*
8390 * Handle XML namespace.
8391 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008392 if (IS_STR_XML(ns->prefix)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008393 /*
8394 * Insert XML namespace mapping.
8395 */
8396 *retNs = xmlTreeEnsureXMLDecl(doc);
8397 if (*retNs == NULL)
8398 return (-1);
8399 return (0);
8400 }
8401 /*
8402 * If the search should be done in ancestors only and no
8403 * @elem (the first ancestor) was specified, then skip the search.
8404 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008405 if ((XML_NSMAP_NOTEMPTY(*nsMap)) &&
8406 (! (ancestorsOnly && (elem == NULL))))
Daniel Veillardaa6de472008-08-25 14:53:31 +00008407 {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008408 /*
8409 * Try to find an equal ns-name in in-scope ns-decls.
8410 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008411 XML_NSMAP_FOREACH(*nsMap, mi) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00008412 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8413 /*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008414 * ancestorsOnly: This should be turned on to gain speed,
8415 * if one knows that the branch itself was already
8416 * ns-wellformed and no stale references existed.
8417 * I.e. it searches in the ancestor axis only.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008418 */
8419 ((! ancestorsOnly) || (mi->depth == XML_TREE_NSMAP_PARENT)) &&
8420 /* Skip shadowed prefixes. */
Daniel Veillardaa6de472008-08-25 14:53:31 +00008421 (mi->shadowDepth == -1) &&
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008422 /* Skip xmlns="" or xmlns:foo="". */
8423 ((mi->newNs->href != NULL) &&
Daniel Veillardaa6de472008-08-25 14:53:31 +00008424 (mi->newNs->href[0] != 0)) &&
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008425 /* Ensure a prefix if wanted. */
8426 ((! prefixed) || (mi->newNs->prefix != NULL)) &&
8427 /* Equal ns name */
8428 ((mi->newNs->href == ns->href) ||
8429 xmlStrEqual(mi->newNs->href, ns->href))) {
8430 /* Set the mapping. */
8431 mi->oldNs = ns;
8432 *retNs = mi->newNs;
8433 return (0);
8434 }
8435 }
8436 }
8437 /*
8438 * No luck, the namespace is out of scope or shadowed.
8439 */
8440 if (elem == NULL) {
8441 xmlNsPtr tmpns;
8442
8443 /*
8444 * Store ns-decls in "oldNs" of the document-node.
8445 */
8446 tmpns = xmlDOMWrapStoreNs(doc, ns->href, ns->prefix);
8447 if (tmpns == NULL)
8448 return (-1);
8449 /*
8450 * Insert mapping.
Daniel Veillardaa6de472008-08-25 14:53:31 +00008451 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008452 if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008453 tmpns, XML_TREE_NSMAP_DOC) == NULL) {
8454 xmlFreeNs(tmpns);
8455 return (-1);
8456 }
8457 *retNs = tmpns;
8458 } else {
8459 xmlNsPtr tmpns;
8460
8461 tmpns = xmlDOMWrapNSNormDeclareNsForced(doc, elem, ns->href,
8462 ns->prefix, 0);
8463 if (tmpns == NULL)
8464 return (-1);
8465
8466 if (*nsMap != NULL) {
8467 /*
8468 * Does it shadow ancestor ns-decls?
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008469 */
8470 XML_NSMAP_FOREACH(*nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008471 if ((mi->depth < depth) &&
8472 (mi->shadowDepth == -1) &&
8473 ((ns->prefix == mi->newNs->prefix) ||
8474 xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
8475 /*
8476 * Shadows.
8477 */
8478 mi->shadowDepth = depth;
8479 break;
8480 }
8481 }
8482 }
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008483 if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns, tmpns, depth) == NULL) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008484 xmlFreeNs(tmpns);
8485 return (-1);
8486 }
8487 *retNs = tmpns;
8488 }
8489 return (0);
8490}
8491
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008492typedef enum {
Daniel Veillardaa6de472008-08-25 14:53:31 +00008493 XML_DOM_RECONNS_REMOVEREDUND = 1<<0
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008494} xmlDOMReconcileNSOptions;
8495
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008496/*
8497* xmlDOMWrapReconcileNamespaces:
Daniel Veillard304e78c2005-07-03 16:19:41 +00008498* @ctxt: DOM wrapper context, unused at the moment
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008499* @elem: the element-node
8500* @options: option flags
8501*
8502* Ensures that ns-references point to ns-decls hold on element-nodes.
8503* Ensures that the tree is namespace wellformed by creating additional
8504* ns-decls where needed. Note that, since prefixes of already existent
8505* ns-decls can be shadowed by this process, it could break QNames in
8506* attribute values or element content.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008507*
8508* NOTE: This function was not intensively tested.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008509*
8510* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
Daniel Veillardaa6de472008-08-25 14:53:31 +00008511*/
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008512
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008513int
8514xmlDOMWrapReconcileNamespaces(xmlDOMWrapCtxtPtr ctxt ATTRIBUTE_UNUSED,
8515 xmlNodePtr elem,
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008516 int options)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008517{
8518 int depth = -1, adoptns = 0, parnsdone = 0;
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008519 xmlNsPtr ns, prevns;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008520 xmlDocPtr doc;
8521 xmlNodePtr cur, curElem = NULL;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008522 xmlNsMapPtr nsMap = NULL;
8523 xmlNsMapItemPtr /* topmi = NULL, */ mi;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008524 /* @ancestorsOnly should be set by an option flag. */
8525 int ancestorsOnly = 0;
Daniel Veillardaa6de472008-08-25 14:53:31 +00008526 int optRemoveRedundantNS =
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008527 ((xmlDOMReconcileNSOptions) options & XML_DOM_RECONNS_REMOVEREDUND) ? 1 : 0;
8528 xmlNsPtr *listRedund = NULL;
8529 int sizeRedund = 0, nbRedund = 0, ret, i, j;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008530
8531 if ((elem == NULL) || (elem->doc == NULL) ||
8532 (elem->type != XML_ELEMENT_NODE))
8533 return (-1);
8534
8535 doc = elem->doc;
8536 cur = elem;
8537 do {
8538 switch (cur->type) {
8539 case XML_ELEMENT_NODE:
8540 adoptns = 1;
8541 curElem = cur;
8542 depth++;
8543 /*
8544 * Namespace declarations.
8545 */
8546 if (cur->nsDef != NULL) {
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008547 prevns = NULL;
Kasimier T. Buchcike8f8d752006-02-02 12:13:07 +00008548 ns = cur->nsDef;
8549 while (ns != NULL) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008550 if (! parnsdone) {
8551 if ((elem->parent) &&
8552 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8553 /*
8554 * Gather ancestor in-scope ns-decls.
8555 */
8556 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8557 elem->parent) == -1)
8558 goto internal_error;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008559 }
8560 parnsdone = 1;
8561 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00008562
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008563 /*
8564 * Lookup the ns ancestor-axis for equal ns-decls in scope.
8565 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008566 if (optRemoveRedundantNS && XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008567 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008568 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8569 (mi->shadowDepth == -1) &&
8570 ((ns->prefix == mi->newNs->prefix) ||
8571 xmlStrEqual(ns->prefix, mi->newNs->prefix)) &&
8572 ((ns->href == mi->newNs->href) ||
8573 xmlStrEqual(ns->href, mi->newNs->href)))
Daniel Veillardaa6de472008-08-25 14:53:31 +00008574 {
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008575 /*
8576 * A redundant ns-decl was found.
8577 * Add it to the list of redundant ns-decls.
8578 */
8579 if (xmlDOMWrapNSNormAddNsMapItem2(&listRedund,
8580 &sizeRedund, &nbRedund, ns, mi->newNs) == -1)
8581 goto internal_error;
8582 /*
8583 * Remove the ns-decl from the element-node.
Daniel Veillardaa6de472008-08-25 14:53:31 +00008584 */
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008585 if (prevns)
8586 prevns->next = ns->next;
8587 else
Daniel Veillardaa6de472008-08-25 14:53:31 +00008588 cur->nsDef = ns->next;
Kasimier T. Buchcike8f8d752006-02-02 12:13:07 +00008589 goto next_ns_decl;
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008590 }
8591 }
8592 }
8593
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008594 /*
8595 * Skip ns-references handling if the referenced
8596 * ns-decl is declared on the same element.
8597 */
8598 if ((cur->ns != NULL) && adoptns && (cur->ns == ns))
Daniel Veillardaa6de472008-08-25 14:53:31 +00008599 adoptns = 0;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008600 /*
8601 * Does it shadow any ns-decl?
8602 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008603 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8604 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008605 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8606 (mi->shadowDepth == -1) &&
8607 ((ns->prefix == mi->newNs->prefix) ||
8608 xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00008609
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008610 mi->shadowDepth = depth;
8611 }
8612 }
8613 }
8614 /*
8615 * Push mapping.
8616 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008617 if (xmlDOMWrapNsMapAddItem(&nsMap, -1, ns, ns,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008618 depth) == NULL)
Daniel Veillardaa6de472008-08-25 14:53:31 +00008619 goto internal_error;
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008620
8621 prevns = ns;
Kasimier T. Buchcike8f8d752006-02-02 12:13:07 +00008622next_ns_decl:
Daniel Veillardaa6de472008-08-25 14:53:31 +00008623 ns = ns->next;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008624 }
8625 }
8626 if (! adoptns)
8627 goto ns_end;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008628 /* No break on purpose. */
8629 case XML_ATTRIBUTE_NODE:
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008630 /* No ns, no fun. */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008631 if (cur->ns == NULL)
8632 goto ns_end;
Daniel Veillardaa6de472008-08-25 14:53:31 +00008633
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008634 if (! parnsdone) {
8635 if ((elem->parent) &&
8636 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
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 }
8643 /*
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008644 * Adjust the reference if this was a redundant ns-decl.
8645 */
8646 if (listRedund) {
8647 for (i = 0, j = 0; i < nbRedund; i++, j += 2) {
8648 if (cur->ns == listRedund[j]) {
8649 cur->ns = listRedund[++j];
8650 break;
8651 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00008652 }
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008653 }
8654 /*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008655 * Adopt ns-references.
8656 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008657 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008658 /*
8659 * Search for a mapping.
8660 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008661 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008662 if ((mi->shadowDepth == -1) &&
8663 (cur->ns == mi->oldNs)) {
8664
8665 cur->ns = mi->newNs;
8666 goto ns_end;
8667 }
8668 }
8669 }
8670 /*
8671 * Aquire a normalized ns-decl and add it to the map.
8672 */
8673 if (xmlDOMWrapNSNormAquireNormalizedNs(doc, curElem,
8674 cur->ns, &ns,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008675 &nsMap, depth,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008676 ancestorsOnly,
8677 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
8678 goto internal_error;
8679 cur->ns = ns;
8680
8681ns_end:
8682 if ((cur->type == XML_ELEMENT_NODE) &&
8683 (cur->properties != NULL)) {
8684 /*
8685 * Process attributes.
8686 */
8687 cur = (xmlNodePtr) cur->properties;
8688 continue;
8689 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00008690 break;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008691 default:
8692 goto next_sibling;
8693 }
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008694into_content:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008695 if ((cur->type == XML_ELEMENT_NODE) &&
8696 (cur->children != NULL)) {
8697 /*
8698 * Process content of element-nodes only.
8699 */
8700 cur = cur->children;
8701 continue;
8702 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00008703next_sibling:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008704 if (cur == elem)
8705 break;
8706 if (cur->type == XML_ELEMENT_NODE) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00008707 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008708 /*
8709 * Pop mappings.
8710 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008711 while ((nsMap->last != NULL) &&
8712 (nsMap->last->depth >= depth))
8713 {
Daniel Veillardaa6de472008-08-25 14:53:31 +00008714 XML_NSMAP_POP(nsMap, mi)
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008715 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008716 /*
8717 * Unshadow.
8718 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008719 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008720 if (mi->shadowDepth >= depth)
8721 mi->shadowDepth = -1;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008722 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00008723 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008724 depth--;
8725 }
8726 if (cur->next != NULL)
8727 cur = cur->next;
8728 else {
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008729 if (cur->type == XML_ATTRIBUTE_NODE) {
8730 cur = cur->parent;
8731 goto into_content;
8732 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008733 cur = cur->parent;
8734 goto next_sibling;
8735 }
8736 } while (cur != NULL);
Daniel Veillardaa6de472008-08-25 14:53:31 +00008737
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008738 ret = 0;
8739 goto exit;
8740internal_error:
8741 ret = -1;
8742exit:
Daniel Veillardaa6de472008-08-25 14:53:31 +00008743 if (listRedund) {
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008744 for (i = 0, j = 0; i < nbRedund; i++, j += 2) {
8745 xmlFreeNs(listRedund[j]);
8746 }
8747 xmlFree(listRedund);
8748 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008749 if (nsMap != NULL)
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008750 xmlDOMWrapNsMapFree(nsMap);
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008751 return (ret);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008752}
8753
8754/*
8755* xmlDOMWrapAdoptBranch:
8756* @ctxt: the optional context for custom processing
8757* @sourceDoc: the optional sourceDoc
8758* @node: the element-node to start with
8759* @destDoc: the destination doc for adoption
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008760* @destParent: the optional new parent of @node in @destDoc
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008761* @options: option flags
8762*
8763* Ensures that ns-references point to @destDoc: either to
8764* elements->nsDef entries if @destParent is given, or to
8765* @destDoc->oldNs otherwise.
8766* If @destParent is given, it ensures that the tree is namespace
8767* wellformed by creating additional ns-decls where needed.
8768* Note that, since prefixes of already existent ns-decls can be
8769* shadowed by this process, it could break QNames in attribute
8770* values or element content.
8771*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008772* NOTE: This function was not intensively tested.
8773*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008774* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8775*/
8776static int
8777xmlDOMWrapAdoptBranch(xmlDOMWrapCtxtPtr ctxt,
8778 xmlDocPtr sourceDoc,
8779 xmlNodePtr node,
8780 xmlDocPtr destDoc,
8781 xmlNodePtr destParent,
8782 int options ATTRIBUTE_UNUSED)
8783{
8784 int ret = 0;
8785 xmlNodePtr cur, curElem = NULL;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008786 xmlNsMapPtr nsMap = NULL;
8787 xmlNsMapItemPtr mi;
Daniel Veillard11ce4002006-03-10 00:36:23 +00008788 xmlNsPtr ns = NULL;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008789 int depth = -1, adoptStr = 1;
8790 /* gather @parent's ns-decls. */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008791 int parnsdone;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008792 /* @ancestorsOnly should be set per option. */
8793 int ancestorsOnly = 0;
Daniel Veillardaa6de472008-08-25 14:53:31 +00008794
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008795 /*
8796 * Optimize string adoption for equal or none dicts.
8797 */
8798 if ((sourceDoc != NULL) &&
8799 (sourceDoc->dict == destDoc->dict))
8800 adoptStr = 0;
8801 else
8802 adoptStr = 1;
8803
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008804 /*
8805 * Get the ns-map from the context if available.
8806 */
8807 if (ctxt)
8808 nsMap = (xmlNsMapPtr) ctxt->namespaceMap;
8809 /*
8810 * Disable search for ns-decls in the parent-axis of the
8811 * desination element, if:
8812 * 1) there's no destination parent
8813 * 2) custom ns-reference handling is used
8814 */
8815 if ((destParent == NULL) ||
8816 (ctxt && ctxt->getNsForNodeFunc))
8817 {
8818 parnsdone = 1;
8819 } else
8820 parnsdone = 0;
8821
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008822 cur = node;
8823 while (cur != NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008824 /*
8825 * Paranoid source-doc sanity check.
8826 */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008827 if (cur->doc != sourceDoc) {
8828 /*
8829 * We'll assume XIncluded nodes if the doc differs.
8830 * TODO: Do we need to reconciliate XIncluded nodes?
8831 * This here skips XIncluded nodes and tries to handle
8832 * broken sequences.
8833 */
8834 if (cur->next == NULL)
8835 goto leave_node;
8836 do {
8837 cur = cur->next;
8838 if ((cur->type == XML_XINCLUDE_END) ||
8839 (cur->doc == node->doc))
8840 break;
8841 } while (cur->next != NULL);
8842
8843 if (cur->doc != node->doc)
8844 goto leave_node;
8845 }
8846 cur->doc = destDoc;
8847 switch (cur->type) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00008848 case XML_XINCLUDE_START:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008849 case XML_XINCLUDE_END:
8850 /*
8851 * TODO
8852 */
8853 return (-1);
Daniel Veillardaa6de472008-08-25 14:53:31 +00008854 case XML_ELEMENT_NODE:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008855 curElem = cur;
8856 depth++;
8857 /*
Daniel Veillardaa6de472008-08-25 14:53:31 +00008858 * Namespace declarations.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008859 * - ns->href and ns->prefix are never in the dict, so
8860 * we need not move the values over to the destination dict.
8861 * - Note that for custom handling of ns-references,
8862 * the ns-decls need not be stored in the ns-map,
8863 * since they won't be referenced by node->ns.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008864 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008865 if ((cur->nsDef) &&
8866 ((ctxt == NULL) || (ctxt->getNsForNodeFunc == NULL)))
8867 {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008868 if (! parnsdone) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008869 /*
8870 * Gather @parent's in-scope ns-decls.
8871 */
8872 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8873 destParent) == -1)
8874 goto internal_error;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008875 parnsdone = 1;
8876 }
8877 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
8878 /*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008879 * NOTE: ns->prefix and ns->href are never in the dict.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008880 * XML_TREE_ADOPT_STR(ns->prefix)
8881 * XML_TREE_ADOPT_STR(ns->href)
Daniel Veillardaa6de472008-08-25 14:53:31 +00008882 */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008883 /*
8884 * Does it shadow any ns-decl?
Daniel Veillardaa6de472008-08-25 14:53:31 +00008885 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008886 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008887 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008888 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8889 (mi->shadowDepth == -1) &&
8890 ((ns->prefix == mi->newNs->prefix) ||
8891 xmlStrEqual(ns->prefix,
8892 mi->newNs->prefix))) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00008893
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008894 mi->shadowDepth = depth;
8895 }
8896 }
8897 }
8898 /*
8899 * Push mapping.
8900 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008901 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008902 ns, ns, depth) == NULL)
8903 goto internal_error;
8904 }
8905 }
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008906 /* No break on purpose. */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008907 case XML_ATTRIBUTE_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008908 /* No namespace, no fun. */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008909 if (cur->ns == NULL)
8910 goto ns_end;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008911
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008912 if (! parnsdone) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008913 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8914 destParent) == -1)
8915 goto internal_error;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008916 parnsdone = 1;
8917 }
8918 /*
8919 * Adopt ns-references.
8920 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008921 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008922 /*
8923 * Search for a mapping.
8924 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008925 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008926 if ((mi->shadowDepth == -1) &&
8927 (cur->ns == mi->oldNs)) {
8928
8929 cur->ns = mi->newNs;
8930 goto ns_end;
8931 }
8932 }
8933 }
8934 /*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008935 * No matching namespace in scope. We need a new one.
8936 */
8937 if ((ctxt) && (ctxt->getNsForNodeFunc)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008938 /*
8939 * User-defined behaviour.
8940 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008941 ns = ctxt->getNsForNodeFunc(ctxt, cur,
8942 cur->ns->href, cur->ns->prefix);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008943 /*
8944 * Insert mapping if ns is available; it's the users fault
8945 * if not.
8946 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008947 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008948 cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008949 goto internal_error;
8950 cur->ns = ns;
8951 } else {
8952 /*
8953 * Aquire a normalized ns-decl and add it to the map.
8954 */
8955 if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc,
Daniel Veillardaa6de472008-08-25 14:53:31 +00008956 /* ns-decls on curElem or on destDoc->oldNs */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008957 destParent ? curElem : NULL,
8958 cur->ns, &ns,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008959 &nsMap, depth,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008960 ancestorsOnly,
8961 /* ns-decls must be prefixed for attributes. */
8962 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
8963 goto internal_error;
8964 cur->ns = ns;
8965 }
8966ns_end:
8967 /*
8968 * Further node properties.
8969 * TODO: Is this all?
8970 */
8971 XML_TREE_ADOPT_STR(cur->name)
8972 if (cur->type == XML_ELEMENT_NODE) {
8973 cur->psvi = NULL;
8974 cur->line = 0;
8975 cur->extra = 0;
8976 /*
8977 * Walk attributes.
8978 */
8979 if (cur->properties != NULL) {
8980 /*
8981 * Process first attribute node.
8982 */
8983 cur = (xmlNodePtr) cur->properties;
8984 continue;
8985 }
8986 } else {
8987 /*
8988 * Attributes.
8989 */
8990 if ((sourceDoc != NULL) &&
8991 (((xmlAttrPtr) cur)->atype == XML_ATTRIBUTE_ID))
Daniel Veillardaa6de472008-08-25 14:53:31 +00008992 {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008993 xmlRemoveID(sourceDoc, (xmlAttrPtr) cur);
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008994 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008995 ((xmlAttrPtr) cur)->atype = 0;
8996 ((xmlAttrPtr) cur)->psvi = NULL;
8997 }
8998 break;
8999 case XML_TEXT_NODE:
Daniel Veillardaa6de472008-08-25 14:53:31 +00009000 case XML_CDATA_SECTION_NODE:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009001 /*
9002 * This puts the content in the dest dict, only if
9003 * it was previously in the source dict.
9004 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009005 XML_TREE_ADOPT_STR_2(cur->content)
9006 goto leave_node;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009007 case XML_ENTITY_REF_NODE:
9008 /*
9009 * Remove reference to the entitity-node.
9010 */
9011 cur->content = NULL;
9012 cur->children = NULL;
9013 cur->last = NULL;
9014 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9015 xmlEntityPtr ent;
9016 /*
9017 * Assign new entity-node if available.
9018 */
9019 ent = xmlGetDocEntity(destDoc, cur->name);
9020 if (ent != NULL) {
9021 cur->content = ent->content;
9022 cur->children = (xmlNodePtr) ent;
9023 cur->last = (xmlNodePtr) ent;
9024 }
9025 }
9026 goto leave_node;
9027 case XML_PI_NODE:
9028 XML_TREE_ADOPT_STR(cur->name)
9029 XML_TREE_ADOPT_STR_2(cur->content)
9030 break;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009031 case XML_COMMENT_NODE:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009032 break;
9033 default:
9034 goto internal_error;
9035 }
9036 /*
9037 * Walk the tree.
9038 */
9039 if (cur->children != NULL) {
9040 cur = cur->children;
9041 continue;
9042 }
9043
9044leave_node:
9045 if (cur == node)
9046 break;
9047 if ((cur->type == XML_ELEMENT_NODE) ||
9048 (cur->type == XML_XINCLUDE_START) ||
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009049 (cur->type == XML_XINCLUDE_END))
9050 {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009051 /*
9052 * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
9053 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009054 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009055 /*
9056 * Pop mappings.
9057 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009058 while ((nsMap->last != NULL) &&
9059 (nsMap->last->depth >= depth))
9060 {
Daniel Veillardaa6de472008-08-25 14:53:31 +00009061 XML_NSMAP_POP(nsMap, mi)
9062 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009063 /*
9064 * Unshadow.
9065 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009066 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009067 if (mi->shadowDepth >= depth)
9068 mi->shadowDepth = -1;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009069 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009070 }
9071 depth--;
9072 }
9073 if (cur->next != NULL)
9074 cur = cur->next;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009075 else if ((cur->type == XML_ATTRIBUTE_NODE) &&
9076 (cur->parent->children != NULL))
9077 {
9078 cur = cur->parent->children;
9079 } else {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009080 cur = cur->parent;
9081 goto leave_node;
9082 }
9083 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009084
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009085 goto exit;
9086
Daniel Veillardaa6de472008-08-25 14:53:31 +00009087internal_error:
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009088 ret = -1;
9089
9090exit:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009091 /*
9092 * Cleanup.
9093 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009094 if (nsMap != NULL) {
9095 if ((ctxt) && (ctxt->namespaceMap == nsMap)) {
9096 /*
9097 * Just cleanup the map but don't free.
9098 */
9099 if (nsMap->first) {
9100 if (nsMap->pool)
9101 nsMap->last->next = nsMap->pool;
9102 nsMap->pool = nsMap->first;
9103 nsMap->first = NULL;
9104 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009105 } else
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009106 xmlDOMWrapNsMapFree(nsMap);
9107 }
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009108 return(ret);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009109}
9110
9111/*
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009112* xmlDOMWrapCloneNode:
9113* @ctxt: the optional context for custom processing
9114* @sourceDoc: the optional sourceDoc
9115* @node: the node to start with
9116* @resNode: the clone of the given @node
9117* @destDoc: the destination doc
9118* @destParent: the optional new parent of @node in @destDoc
Daniel Veillardb2f8f1d2006-04-28 16:30:48 +00009119* @deep: descend into child if set
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009120* @options: option flags
9121*
9122* References of out-of scope ns-decls are remapped to point to @destDoc:
9123* 1) If @destParent is given, then nsDef entries on element-nodes are used
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009124* 2) If *no* @destParent is given, then @destDoc->oldNs entries are used.
9125* This is the case when you don't know already where the cloned branch
9126* will be added to.
Daniel Veillardaa6de472008-08-25 14:53:31 +00009127*
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009128* If @destParent is given, it ensures that the tree is namespace
9129* wellformed by creating additional ns-decls where needed.
9130* Note that, since prefixes of already existent ns-decls can be
9131* shadowed by this process, it could break QNames in attribute
9132* values or element content.
9133* TODO:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009134* 1) What to do with XInclude? Currently this returns an error for XInclude.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009135*
9136* Returns 0 if the operation succeeded,
9137* 1 if a node of unsupported (or not yet supported) type was given,
9138* -1 on API/internal errors.
9139*/
9140
9141int
9142xmlDOMWrapCloneNode(xmlDOMWrapCtxtPtr ctxt,
9143 xmlDocPtr sourceDoc,
9144 xmlNodePtr node,
9145 xmlNodePtr *resNode,
9146 xmlDocPtr destDoc,
9147 xmlNodePtr destParent,
9148 int deep,
9149 int options ATTRIBUTE_UNUSED)
9150{
9151 int ret = 0;
9152 xmlNodePtr cur, curElem = NULL;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009153 xmlNsMapPtr nsMap = NULL;
9154 xmlNsMapItemPtr mi;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009155 xmlNsPtr ns;
9156 int depth = -1;
9157 /* int adoptStr = 1; */
9158 /* gather @parent's ns-decls. */
9159 int parnsdone = 0;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009160 /*
Daniel Veillardaa6de472008-08-25 14:53:31 +00009161 * @ancestorsOnly:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009162 * TODO: @ancestorsOnly should be set per option.
9163 *
9164 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009165 int ancestorsOnly = 0;
9166 xmlNodePtr resultClone = NULL, clone = NULL, parentClone = NULL, prevClone = NULL;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009167 xmlNsPtr cloneNs = NULL, *cloneNsDefSlot = NULL;
9168 xmlDictPtr dict; /* The destination dict */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009169
Daniel Veillard11ce4002006-03-10 00:36:23 +00009170 if ((node == NULL) || (resNode == NULL) || (destDoc == NULL))
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009171 return(-1);
9172 /*
9173 * TODO: Initially we support only element-nodes.
9174 */
9175 if (node->type != XML_ELEMENT_NODE)
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009176 return(1);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009177 /*
9178 * Check node->doc sanity.
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009179 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009180 if ((node->doc != NULL) && (sourceDoc != NULL) &&
9181 (node->doc != sourceDoc)) {
9182 /*
9183 * Might be an XIncluded node.
9184 */
9185 return (-1);
9186 }
9187 if (sourceDoc == NULL)
Daniel Veillardaa6de472008-08-25 14:53:31 +00009188 sourceDoc = node->doc;
Daniel Veillard11ce4002006-03-10 00:36:23 +00009189 if (sourceDoc == NULL)
9190 return (-1);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009191
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009192 dict = destDoc->dict;
9193 /*
9194 * Reuse the namespace map of the context.
9195 */
9196 if (ctxt)
9197 nsMap = (xmlNsMapPtr) ctxt->namespaceMap;
9198
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009199 *resNode = NULL;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009200
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009201 cur = node;
9202 while (cur != NULL) {
9203 if (cur->doc != sourceDoc) {
9204 /*
9205 * We'll assume XIncluded nodes if the doc differs.
9206 * TODO: Do we need to reconciliate XIncluded nodes?
9207 * TODO: This here returns -1 in this case.
9208 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009209 goto internal_error;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009210 }
9211 /*
9212 * Create a new node.
9213 */
9214 switch (cur->type) {
9215 case XML_XINCLUDE_START:
9216 case XML_XINCLUDE_END:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009217 /*
9218 * TODO: What to do with XInclude?
9219 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009220 goto internal_error;
9221 break;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009222 case XML_ELEMENT_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009223 case XML_TEXT_NODE:
Daniel Veillardaa6de472008-08-25 14:53:31 +00009224 case XML_CDATA_SECTION_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009225 case XML_COMMENT_NODE:
Daniel Veillardaa6de472008-08-25 14:53:31 +00009226 case XML_PI_NODE:
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009227 case XML_DOCUMENT_FRAG_NODE:
9228 case XML_ENTITY_REF_NODE:
9229 case XML_ENTITY_NODE:
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009230 /*
9231 * Nodes of xmlNode structure.
9232 */
9233 clone = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
9234 if (clone == NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009235 xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating a node");
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009236 goto internal_error;
9237 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009238 memset(clone, 0, sizeof(xmlNode));
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009239 /*
9240 * Set hierachical links.
9241 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009242 if (resultClone != NULL) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009243 clone->parent = parentClone;
9244 if (prevClone) {
9245 prevClone->next = clone;
9246 clone->prev = prevClone;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009247 } else
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009248 parentClone->children = clone;
9249 } else
9250 resultClone = clone;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009251
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009252 break;
9253 case XML_ATTRIBUTE_NODE:
9254 /*
9255 * Attributes (xmlAttr).
9256 */
9257 clone = (xmlNodePtr) xmlMalloc(sizeof(xmlAttr));
9258 if (clone == NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009259 xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating an attr-node");
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009260 goto internal_error;
9261 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009262 memset(clone, 0, sizeof(xmlAttr));
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009263 /*
9264 * Set hierachical links.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009265 * TODO: Change this to add to the end of attributes.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009266 */
9267 if (resultClone != NULL) {
9268 clone->parent = parentClone;
9269 if (prevClone) {
9270 prevClone->next = clone;
9271 clone->prev = prevClone;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009272 } else
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009273 parentClone->properties = (xmlAttrPtr) clone;
9274 } else
9275 resultClone = clone;
9276 break;
9277 default:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009278 /*
9279 * TODO QUESTION: Any other nodes expected?
9280 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009281 goto internal_error;
9282 }
9283
9284 clone->type = cur->type;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009285 clone->doc = destDoc;
9286
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009287 /*
9288 * Clone the name of the node if any.
9289 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009290 if (cur->name == xmlStringText)
9291 clone->name = xmlStringText;
9292 else if (cur->name == xmlStringTextNoenc)
9293 /*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009294 * NOTE: Although xmlStringTextNoenc is never assigned to a node
9295 * in tree.c, it might be set in Libxslt via
Daniel Veillardaa6de472008-08-25 14:53:31 +00009296 * "xsl:disable-output-escaping".
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009297 */
9298 clone->name = xmlStringTextNoenc;
9299 else if (cur->name == xmlStringComment)
9300 clone->name = xmlStringComment;
9301 else if (cur->name != NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009302 DICT_CONST_COPY(cur->name, clone->name);
Daniel Veillardaa6de472008-08-25 14:53:31 +00009303 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009304
9305 switch (cur->type) {
9306 case XML_XINCLUDE_START:
9307 case XML_XINCLUDE_END:
9308 /*
9309 * TODO
9310 */
9311 return (-1);
9312 case XML_ELEMENT_NODE:
9313 curElem = cur;
9314 depth++;
9315 /*
9316 * Namespace declarations.
9317 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009318 if (cur->nsDef != NULL) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009319 if (! parnsdone) {
9320 if (destParent && (ctxt == NULL)) {
9321 /*
9322 * Gather @parent's in-scope ns-decls.
9323 */
9324 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
9325 destParent) == -1)
Daniel Veillardaa6de472008-08-25 14:53:31 +00009326 goto internal_error;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009327 }
9328 parnsdone = 1;
9329 }
9330 /*
9331 * Clone namespace declarations.
9332 */
9333 cloneNsDefSlot = &(clone->nsDef);
9334 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
9335 /*
9336 * Create a new xmlNs.
9337 */
9338 cloneNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
9339 if (cloneNs == NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009340 xmlTreeErrMemory("xmlDOMWrapCloneNode(): "
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009341 "allocating namespace");
9342 return(-1);
9343 }
9344 memset(cloneNs, 0, sizeof(xmlNs));
9345 cloneNs->type = XML_LOCAL_NAMESPACE;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009346
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009347 if (ns->href != NULL)
9348 cloneNs->href = xmlStrdup(ns->href);
9349 if (ns->prefix != NULL)
9350 cloneNs->prefix = xmlStrdup(ns->prefix);
9351
9352 *cloneNsDefSlot = cloneNs;
9353 cloneNsDefSlot = &(cloneNs->next);
9354
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009355 /*
9356 * Note that for custom handling of ns-references,
9357 * the ns-decls need not be stored in the ns-map,
9358 * since they won't be referenced by node->ns.
9359 */
9360 if ((ctxt == NULL) ||
9361 (ctxt->getNsForNodeFunc == NULL))
9362 {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009363 /*
9364 * Does it shadow any ns-decl?
9365 */
9366 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00009367 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009368 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
9369 (mi->shadowDepth == -1) &&
9370 ((ns->prefix == mi->newNs->prefix) ||
9371 xmlStrEqual(ns->prefix,
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009372 mi->newNs->prefix))) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009373 /*
9374 * Mark as shadowed at the current
9375 * depth.
9376 */
9377 mi->shadowDepth = depth;
9378 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009379 }
9380 }
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009381 /*
9382 * Push mapping.
9383 */
9384 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
9385 ns, cloneNs, depth) == NULL)
9386 goto internal_error;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009387 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009388 }
9389 }
9390 /* cur->ns will be processed further down. */
9391 break;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009392 case XML_ATTRIBUTE_NODE:
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009393 /* IDs will be processed further down. */
9394 /* cur->ns will be processed further down. */
9395 break;
9396 case XML_TEXT_NODE:
9397 case XML_CDATA_SECTION_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009398 /*
9399 * Note that this will also cover the values of attributes.
9400 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009401 DICT_COPY(cur->content, clone->content);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009402 goto leave_node;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009403 case XML_ENTITY_NODE:
9404 /* TODO: What to do here? */
9405 goto leave_node;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009406 case XML_ENTITY_REF_NODE:
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009407 if (sourceDoc != destDoc) {
9408 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9409 xmlEntityPtr ent;
9410 /*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009411 * Different doc: Assign new entity-node if available.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009412 */
9413 ent = xmlGetDocEntity(destDoc, cur->name);
9414 if (ent != NULL) {
9415 clone->content = ent->content;
9416 clone->children = (xmlNodePtr) ent;
9417 clone->last = (xmlNodePtr) ent;
9418 }
9419 }
9420 } else {
9421 /*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009422 * Same doc: Use the current node's entity declaration
9423 * and value.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009424 */
9425 clone->content = cur->content;
9426 clone->children = cur->children;
9427 clone->last = cur->last;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009428 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009429 goto leave_node;
9430 case XML_PI_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009431 DICT_COPY(cur->content, clone->content);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009432 goto leave_node;
9433 case XML_COMMENT_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009434 DICT_COPY(cur->content, clone->content);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009435 goto leave_node;
9436 default:
9437 goto internal_error;
9438 }
9439
9440 if (cur->ns == NULL)
9441 goto end_ns_reference;
9442
9443/* handle_ns_reference: */
9444 /*
9445 ** The following will take care of references to ns-decls ********
Daniel Veillardaa6de472008-08-25 14:53:31 +00009446 ** and is intended only for element- and attribute-nodes.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009447 **
9448 */
9449 if (! parnsdone) {
9450 if (destParent && (ctxt == NULL)) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009451 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, destParent) == -1)
Daniel Veillardaa6de472008-08-25 14:53:31 +00009452 goto internal_error;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009453 }
9454 parnsdone = 1;
9455 }
9456 /*
9457 * Adopt ns-references.
9458 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009459 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009460 /*
9461 * Search for a mapping.
9462 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009463 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009464 if ((mi->shadowDepth == -1) &&
9465 (cur->ns == mi->oldNs)) {
9466 /*
9467 * This is the nice case: a mapping was found.
9468 */
9469 clone->ns = mi->newNs;
9470 goto end_ns_reference;
9471 }
9472 }
9473 }
9474 /*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009475 * No matching namespace in scope. We need a new one.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009476 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009477 if ((ctxt != NULL) && (ctxt->getNsForNodeFunc != NULL)) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009478 /*
9479 * User-defined behaviour.
9480 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009481 ns = ctxt->getNsForNodeFunc(ctxt, cur,
9482 cur->ns->href, cur->ns->prefix);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009483 /*
9484 * Add user's mapping.
9485 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009486 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009487 cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
9488 goto internal_error;
9489 clone->ns = ns;
9490 } else {
9491 /*
9492 * Aquire a normalized ns-decl and add it to the map.
9493 */
9494 if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc,
Daniel Veillardaa6de472008-08-25 14:53:31 +00009495 /* ns-decls on curElem or on destDoc->oldNs */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009496 destParent ? curElem : NULL,
9497 cur->ns, &ns,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009498 &nsMap, depth,
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009499 /* if we need to search only in the ancestor-axis */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009500 ancestorsOnly,
9501 /* ns-decls must be prefixed for attributes. */
9502 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
9503 goto internal_error;
9504 clone->ns = ns;
9505 }
9506
9507end_ns_reference:
9508
9509 /*
9510 * Some post-processing.
9511 *
9512 * Handle ID attributes.
9513 */
9514 if ((clone->type == XML_ATTRIBUTE_NODE) &&
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009515 (clone->parent != NULL))
9516 {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009517 if (xmlIsID(destDoc, clone->parent, (xmlAttrPtr) clone)) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00009518
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009519 xmlChar *idVal;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009520
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009521 idVal = xmlNodeListGetString(cur->doc, cur->children, 1);
9522 if (idVal != NULL) {
9523 if (xmlAddID(NULL, destDoc, idVal, (xmlAttrPtr) cur) == NULL) {
9524 /* TODO: error message. */
9525 xmlFree(idVal);
9526 goto internal_error;
9527 }
9528 xmlFree(idVal);
9529 }
9530 }
9531 }
9532 /*
9533 **
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009534 ** The following will traverse the tree **************************
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009535 **
Daniel Veillardaa6de472008-08-25 14:53:31 +00009536 *
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009537 * Walk the element's attributes before descending into child-nodes.
9538 */
9539 if ((cur->type == XML_ELEMENT_NODE) && (cur->properties != NULL)) {
9540 prevClone = NULL;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009541 parentClone = clone;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009542 cur = (xmlNodePtr) cur->properties;
9543 continue;
9544 }
9545into_content:
9546 /*
9547 * Descend into child-nodes.
9548 */
9549 if (cur->children != NULL) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009550 if (deep || (cur->type == XML_ATTRIBUTE_NODE)) {
9551 prevClone = NULL;
9552 parentClone = clone;
9553 cur = cur->children;
9554 continue;
9555 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009556 }
9557
9558leave_node:
9559 /*
9560 * At this point we are done with the node, its content
9561 * and an element-nodes's attribute-nodes.
9562 */
9563 if (cur == node)
9564 break;
9565 if ((cur->type == XML_ELEMENT_NODE) ||
9566 (cur->type == XML_XINCLUDE_START) ||
9567 (cur->type == XML_XINCLUDE_END)) {
9568 /*
9569 * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
9570 */
Daniel Veillardaa6de472008-08-25 14:53:31 +00009571 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009572 /*
9573 * Pop mappings.
9574 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009575 while ((nsMap->last != NULL) &&
9576 (nsMap->last->depth >= depth))
9577 {
Daniel Veillardaa6de472008-08-25 14:53:31 +00009578 XML_NSMAP_POP(nsMap, mi)
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009579 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009580 /*
9581 * Unshadow.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009582 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009583 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009584 if (mi->shadowDepth >= depth)
9585 mi->shadowDepth = -1;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009586 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009587 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009588 depth--;
9589 }
9590 if (cur->next != NULL) {
9591 prevClone = clone;
9592 cur = cur->next;
9593 } else if (cur->type != XML_ATTRIBUTE_NODE) {
9594 /*
9595 * Set clone->last.
9596 */
Daniel Veillard11ce4002006-03-10 00:36:23 +00009597 if (clone->parent != NULL)
9598 clone->parent->last = clone;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009599 clone = clone->parent;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009600 parentClone = clone->parent;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009601 /*
9602 * Process parent --> next;
9603 */
9604 cur = cur->parent;
9605 goto leave_node;
9606 } else {
9607 /* This is for attributes only. */
9608 clone = clone->parent;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009609 parentClone = clone->parent;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009610 /*
9611 * Process parent-element --> children.
9612 */
9613 cur = cur->parent;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009614 goto into_content;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009615 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009616 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009617 goto exit;
9618
9619internal_error:
9620 ret = -1;
9621
9622exit:
9623 /*
9624 * Cleanup.
9625 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009626 if (nsMap != NULL) {
9627 if ((ctxt) && (ctxt->namespaceMap == nsMap)) {
9628 /*
9629 * Just cleanup the map but don't free.
9630 */
9631 if (nsMap->first) {
9632 if (nsMap->pool)
9633 nsMap->last->next = nsMap->pool;
9634 nsMap->pool = nsMap->first;
9635 nsMap->first = NULL;
9636 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009637 } else
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009638 xmlDOMWrapNsMapFree(nsMap);
9639 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009640 /*
9641 * TODO: Should we try a cleanup of the cloned node in case of a
9642 * fatal error?
9643 */
9644 *resNode = resultClone;
9645 return (ret);
9646}
9647
9648/*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009649* xmlDOMWrapAdoptAttr:
9650* @ctxt: the optional context for custom processing
9651* @sourceDoc: the optional source document of attr
9652* @attr: the attribute-node to be adopted
9653* @destDoc: the destination doc for adoption
9654* @destParent: the optional new parent of @attr in @destDoc
9655* @options: option flags
9656*
9657* @attr is adopted by @destDoc.
9658* Ensures that ns-references point to @destDoc: either to
9659* elements->nsDef entries if @destParent is given, or to
9660* @destDoc->oldNs otherwise.
9661*
9662* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
9663*/
9664static int
9665xmlDOMWrapAdoptAttr(xmlDOMWrapCtxtPtr ctxt,
9666 xmlDocPtr sourceDoc,
9667 xmlAttrPtr attr,
9668 xmlDocPtr destDoc,
9669 xmlNodePtr destParent,
9670 int options ATTRIBUTE_UNUSED)
9671{
9672 xmlNodePtr cur;
9673 int adoptStr = 1;
9674
9675 if ((attr == NULL) || (destDoc == NULL))
9676 return (-1);
Daniel Veillardaa6de472008-08-25 14:53:31 +00009677
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009678 attr->doc = destDoc;
9679 if (attr->ns != NULL) {
9680 xmlNsPtr ns = NULL;
9681
9682 if (ctxt != NULL) {
9683 /* TODO: User defined. */
9684 }
9685 /* XML Namespace. */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009686 if (IS_STR_XML(attr->ns->prefix)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009687 ns = xmlTreeEnsureXMLDecl(destDoc);
9688 } else if (destParent == NULL) {
9689 /*
9690 * Store in @destDoc->oldNs.
9691 */
9692 ns = xmlDOMWrapStoreNs(destDoc, attr->ns->href, attr->ns->prefix);
9693 } else {
9694 /*
9695 * Declare on @destParent.
9696 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009697 if (xmlSearchNsByNamespaceStrict(destDoc, destParent, attr->ns->href,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009698 &ns, 1) == -1)
9699 goto internal_error;
9700 if (ns == NULL) {
9701 ns = xmlDOMWrapNSNormDeclareNsForced(destDoc, destParent,
9702 attr->ns->href, attr->ns->prefix, 1);
9703 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009704 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009705 if (ns == NULL)
9706 goto internal_error;
9707 attr->ns = ns;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009708 }
9709
9710 XML_TREE_ADOPT_STR(attr->name);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009711 attr->atype = 0;
9712 attr->psvi = NULL;
9713 /*
9714 * Walk content.
9715 */
9716 if (attr->children == NULL)
9717 return (0);
9718 cur = attr->children;
9719 while (cur != NULL) {
9720 cur->doc = destDoc;
9721 switch (cur->type) {
9722 case XML_TEXT_NODE:
9723 case XML_CDATA_SECTION_NODE:
9724 XML_TREE_ADOPT_STR_2(cur->content)
Daniel Veillardaa6de472008-08-25 14:53:31 +00009725 break;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009726 case XML_ENTITY_REF_NODE:
9727 /*
9728 * Remove reference to the entitity-node.
9729 */
9730 cur->content = NULL;
9731 cur->children = NULL;
9732 cur->last = NULL;
9733 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9734 xmlEntityPtr ent;
9735 /*
9736 * Assign new entity-node if available.
9737 */
9738 ent = xmlGetDocEntity(destDoc, cur->name);
9739 if (ent != NULL) {
9740 cur->content = ent->content;
9741 cur->children = (xmlNodePtr) ent;
9742 cur->last = (xmlNodePtr) ent;
Daniel Veillardaa6de472008-08-25 14:53:31 +00009743 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009744 }
9745 break;
9746 default:
9747 break;
9748 }
9749 if (cur->children != NULL) {
9750 cur = cur->children;
9751 continue;
9752 }
9753next_sibling:
9754 if (cur == (xmlNodePtr) attr)
9755 break;
9756 if (cur->next != NULL)
9757 cur = cur->next;
9758 else {
9759 cur = cur->parent;
9760 goto next_sibling;
9761 }
9762 }
9763 return (0);
9764internal_error:
9765 return (-1);
9766}
9767
9768/*
9769* xmlDOMWrapAdoptNode:
9770* @ctxt: the optional context for custom processing
9771* @sourceDoc: the optional sourceDoc
9772* @node: the node to start with
9773* @destDoc: the destination doc
Kasimier T. Buchcik4d9c9482005-06-27 15:04:46 +00009774* @destParent: the optional new parent of @node in @destDoc
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009775* @options: option flags
9776*
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009777* References of out-of scope ns-decls are remapped to point to @destDoc:
9778* 1) If @destParent is given, then nsDef entries on element-nodes are used
9779* 2) If *no* @destParent is given, then @destDoc->oldNs entries are used
9780* This is the case when you have an unliked node and just want to move it
Daniel Veillardaa6de472008-08-25 14:53:31 +00009781* to the context of
9782*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009783* If @destParent is given, it ensures that the tree is namespace
9784* wellformed by creating additional ns-decls where needed.
9785* Note that, since prefixes of already existent ns-decls can be
9786* shadowed by this process, it could break QNames in attribute
9787* values or element content.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009788* NOTE: This function was not intensively tested.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009789*
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009790* Returns 0 if the operation succeeded,
9791* 1 if a node of unsupported type was given,
9792* 2 if a node of not yet supported type was given and
9793* -1 on API/internal errors.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009794*/
9795int
9796xmlDOMWrapAdoptNode(xmlDOMWrapCtxtPtr ctxt,
9797 xmlDocPtr sourceDoc,
9798 xmlNodePtr node,
Daniel Veillardaa6de472008-08-25 14:53:31 +00009799 xmlDocPtr destDoc,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009800 xmlNodePtr destParent,
9801 int options)
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009802{
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009803 if ((node == NULL) || (destDoc == NULL) ||
9804 ((destParent != NULL) && (destParent->doc != destDoc)))
9805 return(-1);
9806 /*
9807 * Check node->doc sanity.
Daniel Veillardaa6de472008-08-25 14:53:31 +00009808 */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009809 if ((node->doc != NULL) && (sourceDoc != NULL) &&
9810 (node->doc != sourceDoc)) {
9811 /*
9812 * Might be an XIncluded node.
9813 */
9814 return (-1);
9815 }
9816 if (sourceDoc == NULL)
9817 sourceDoc = node->doc;
9818 if (sourceDoc == destDoc)
9819 return (-1);
9820 switch (node->type) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00009821 case XML_ELEMENT_NODE:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009822 case XML_ATTRIBUTE_NODE:
9823 case XML_TEXT_NODE:
9824 case XML_CDATA_SECTION_NODE:
9825 case XML_ENTITY_REF_NODE:
9826 case XML_PI_NODE:
9827 case XML_COMMENT_NODE:
9828 break;
9829 case XML_DOCUMENT_FRAG_NODE:
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009830 /* TODO: Support document-fragment-nodes. */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009831 return (2);
9832 default:
9833 return (1);
9834 }
9835 /*
9836 * Unlink only if @node was not already added to @destParent.
9837 */
9838 if ((node->parent != NULL) && (destParent != node->parent))
9839 xmlUnlinkNode(node);
9840
9841 if (node->type == XML_ELEMENT_NODE) {
9842 return (xmlDOMWrapAdoptBranch(ctxt, sourceDoc, node,
9843 destDoc, destParent, options));
9844 } else if (node->type == XML_ATTRIBUTE_NODE) {
9845 return (xmlDOMWrapAdoptAttr(ctxt, sourceDoc,
9846 (xmlAttrPtr) node, destDoc, destParent, options));
Daniel Veillardaa6de472008-08-25 14:53:31 +00009847 } else {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009848 xmlNodePtr cur = node;
9849 int adoptStr = 1;
9850
9851 cur->doc = destDoc;
9852 /*
9853 * Optimize string adoption.
9854 */
9855 if ((sourceDoc != NULL) &&
9856 (sourceDoc->dict == destDoc->dict))
9857 adoptStr = 0;
9858 switch (node->type) {
Daniel Veillardaa6de472008-08-25 14:53:31 +00009859 case XML_TEXT_NODE:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009860 case XML_CDATA_SECTION_NODE:
9861 XML_TREE_ADOPT_STR_2(node->content)
9862 break;
9863 case XML_ENTITY_REF_NODE:
9864 /*
9865 * Remove reference to the entitity-node.
9866 */
9867 node->content = NULL;
9868 node->children = NULL;
9869 node->last = NULL;
9870 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9871 xmlEntityPtr ent;
9872 /*
9873 * Assign new entity-node if available.
9874 */
9875 ent = xmlGetDocEntity(destDoc, node->name);
9876 if (ent != NULL) {
9877 node->content = ent->content;
9878 node->children = (xmlNodePtr) ent;
9879 node->last = (xmlNodePtr) ent;
9880 }
9881 }
9882 XML_TREE_ADOPT_STR(node->name)
9883 break;
Daniel Veillard7e21fd12005-07-03 21:44:07 +00009884 case XML_PI_NODE: {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009885 XML_TREE_ADOPT_STR(node->name)
9886 XML_TREE_ADOPT_STR_2(node->content)
9887 break;
Daniel Veillard7e21fd12005-07-03 21:44:07 +00009888 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009889 default:
9890 break;
9891 }
Daniel Veillardaa6de472008-08-25 14:53:31 +00009892 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009893 return (0);
9894}
9895
Daniel Veillard5d4644e2005-04-01 13:11:58 +00009896#define bottom_tree
9897#include "elfgcchack.h"