blob: 64d5b4fdb06f805f71890b1f58c42968d86c959d [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 ! */
17
18#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 * *
48 * Forward declarations *
49 * *
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 Veillard18ec16e2003-10-07 23:16:40 +000058 * Tree memory error handler *
59 * *
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 Veillard56a4cb82001-03-24 17:00:36 +0000106 * A few static variables and macros *
107 * *
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) { \
126 ulccur->parent = (n); \
127 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 Veillard8ee9c8f2002-01-26 21:42:58 +0000141 * Functions to move to entities.c once the *
142 * API freeze is smoothen and they can be made public. *
143 * *
144 ************************************************************************/
145#include <libxml/hash.h>
146
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.
155 *
156 * Returns A pointer to the entity structure or NULL if not found.
157 */
158static xmlEntityPtr
159xmlGetEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) {
160 xmlEntitiesTablePtr table;
161
162 if((dtd != NULL) && (dtd->entities != NULL)) {
163 table = (xmlEntitiesTablePtr) dtd->entities;
164 return((xmlEntityPtr) xmlHashLookup(table, name));
165 /* return(xmlGetEntityFromTable(table, name)); */
166 }
167 return(NULL);
168}
169/**
170 * xmlGetParameterEntityFromDtd:
171 * @dtd: A pointer to the DTD to search
172 * @name: The entity name
173 *
174 * 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;
182
183 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
243 * @prefix: a xmlChar **
244 *
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 */
281 while ((name[len] != 0) && (name[len] != ':'))
282 len++;
283
284 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 */
332 while ((name[l] != 0) && (name[l] != ':'))
333 l++;
334
335 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 ************************************************************************/
348
349#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 ************************************************************************/
671
672/**
673 * xmlSetBufferAllocationScheme:
674 * @scheme: allocation method to use
675 *
676 * Set the buffer allocation method. Types are
677 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
678 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
679 * improves performance
680 */
681void
682xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme) {
683 xmlBufferAllocScheme = scheme;
684}
685
686/**
687 * xmlGetBufferAllocationScheme:
688 *
689 * Types are
690 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
691 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
692 * improves performance
693 *
694 * Returns the current allocation scheme
695 */
696xmlBufferAllocationScheme
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000697xmlGetBufferAllocationScheme(void) {
Daniel Veillarde043ee12001-04-16 14:08:07 +0000698 return(xmlBufferAllocScheme);
Owen Taylor3473f882001-02-23 17:55:21 +0000699}
700
701/**
702 * xmlNewNs:
703 * @node: the element carrying the namespace
704 * @href: the URI associated
705 * @prefix: the prefix for the namespace
706 *
707 * Creation of a new Namespace. This function will refuse to create
708 * a namespace with a similar prefix than an existing one present on this
709 * node.
710 * We use href==NULL in the case of an element creation where the namespace
711 * was not defined.
Daniel Veillardd1640922001-12-17 15:30:10 +0000712 * Returns a new namespace pointer or NULL
Owen Taylor3473f882001-02-23 17:55:21 +0000713 */
714xmlNsPtr
715xmlNewNs(xmlNodePtr node, const xmlChar *href, const xmlChar *prefix) {
716 xmlNsPtr cur;
717
718 if ((node != NULL) && (node->type != XML_ELEMENT_NODE))
719 return(NULL);
720
Daniel Veillard20ee8c02001-10-05 09:18:14 +0000721 if ((prefix != NULL) && (xmlStrEqual(prefix, BAD_CAST "xml")))
722 return(NULL);
723
Owen Taylor3473f882001-02-23 17:55:21 +0000724 /*
725 * Allocate a new Namespace and fill the fields.
726 */
727 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
728 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000729 xmlTreeErrMemory("building namespace");
Owen Taylor3473f882001-02-23 17:55:21 +0000730 return(NULL);
731 }
732 memset(cur, 0, sizeof(xmlNs));
733 cur->type = XML_LOCAL_NAMESPACE;
734
735 if (href != NULL)
736 cur->href = xmlStrdup(href);
737 if (prefix != NULL)
738 cur->prefix = xmlStrdup(prefix);
739
740 /*
741 * Add it at the end to preserve parsing order ...
742 * and checks for existing use of the prefix
743 */
744 if (node != NULL) {
745 if (node->nsDef == NULL) {
746 node->nsDef = cur;
747 } else {
748 xmlNsPtr prev = node->nsDef;
749
750 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
751 (xmlStrEqual(prev->prefix, cur->prefix))) {
752 xmlFreeNs(cur);
753 return(NULL);
754 }
755 while (prev->next != NULL) {
756 prev = prev->next;
757 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
758 (xmlStrEqual(prev->prefix, cur->prefix))) {
759 xmlFreeNs(cur);
760 return(NULL);
761 }
762 }
763 prev->next = cur;
764 }
765 }
766 return(cur);
767}
768
769/**
770 * xmlSetNs:
771 * @node: a node in the document
772 * @ns: a namespace pointer
773 *
774 * Associate a namespace to a node, a posteriori.
775 */
776void
777xmlSetNs(xmlNodePtr node, xmlNsPtr ns) {
778 if (node == NULL) {
779#ifdef DEBUG_TREE
780 xmlGenericError(xmlGenericErrorContext,
781 "xmlSetNs: node == NULL\n");
782#endif
783 return;
784 }
785 node->ns = ns;
786}
787
788/**
789 * xmlFreeNs:
790 * @cur: the namespace pointer
791 *
792 * Free up the structures associated to a namespace
793 */
794void
795xmlFreeNs(xmlNsPtr cur) {
796 if (cur == NULL) {
797#ifdef DEBUG_TREE
798 xmlGenericError(xmlGenericErrorContext,
799 "xmlFreeNs : ns == NULL\n");
800#endif
801 return;
802 }
803 if (cur->href != NULL) xmlFree((char *) cur->href);
804 if (cur->prefix != NULL) xmlFree((char *) cur->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000805 xmlFree(cur);
806}
807
808/**
809 * xmlFreeNsList:
810 * @cur: the first namespace pointer
811 *
812 * Free up all the structures associated to the chained namespaces.
813 */
814void
815xmlFreeNsList(xmlNsPtr cur) {
816 xmlNsPtr next;
817 if (cur == NULL) {
818#ifdef DEBUG_TREE
819 xmlGenericError(xmlGenericErrorContext,
820 "xmlFreeNsList : ns == NULL\n");
821#endif
822 return;
823 }
824 while (cur != NULL) {
825 next = cur->next;
826 xmlFreeNs(cur);
827 cur = next;
828 }
829}
830
831/**
832 * xmlNewDtd:
833 * @doc: the document pointer
834 * @name: the DTD name
835 * @ExternalID: the external ID
836 * @SystemID: the system ID
837 *
838 * Creation of a new DTD for the external subset. To create an
839 * internal subset, use xmlCreateIntSubset().
840 *
841 * Returns a pointer to the new DTD structure
842 */
843xmlDtdPtr
844xmlNewDtd(xmlDocPtr doc, const xmlChar *name,
845 const xmlChar *ExternalID, const xmlChar *SystemID) {
846 xmlDtdPtr cur;
847
848 if ((doc != NULL) && (doc->extSubset != NULL)) {
849#ifdef DEBUG_TREE
850 xmlGenericError(xmlGenericErrorContext,
851 "xmlNewDtd(%s): document %s already have a DTD %s\n",
852 /* !!! */ (char *) name, doc->name,
853 /* !!! */ (char *)doc->extSubset->name);
854#endif
855 return(NULL);
856 }
857
858 /*
859 * Allocate a new DTD and fill the fields.
860 */
861 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
862 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000863 xmlTreeErrMemory("building DTD");
Owen Taylor3473f882001-02-23 17:55:21 +0000864 return(NULL);
865 }
866 memset(cur, 0 , sizeof(xmlDtd));
867 cur->type = XML_DTD_NODE;
868
869 if (name != NULL)
870 cur->name = xmlStrdup(name);
871 if (ExternalID != NULL)
872 cur->ExternalID = xmlStrdup(ExternalID);
873 if (SystemID != NULL)
874 cur->SystemID = xmlStrdup(SystemID);
875 if (doc != NULL)
876 doc->extSubset = cur;
877 cur->doc = doc;
878
Daniel Veillarda880b122003-04-21 21:36:41 +0000879 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +0000880 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000881 return(cur);
882}
883
884/**
885 * xmlGetIntSubset:
886 * @doc: the document pointer
887 *
888 * Get the internal subset of a document
889 * Returns a pointer to the DTD structure or NULL if not found
890 */
891
892xmlDtdPtr
893xmlGetIntSubset(xmlDocPtr doc) {
894 xmlNodePtr cur;
895
896 if (doc == NULL)
897 return(NULL);
898 cur = doc->children;
899 while (cur != NULL) {
900 if (cur->type == XML_DTD_NODE)
901 return((xmlDtdPtr) cur);
902 cur = cur->next;
903 }
904 return((xmlDtdPtr) doc->intSubset);
905}
906
907/**
908 * xmlCreateIntSubset:
909 * @doc: the document pointer
910 * @name: the DTD name
Daniel Veillarde356c282001-03-10 12:32:04 +0000911 * @ExternalID: the external (PUBLIC) ID
Owen Taylor3473f882001-02-23 17:55:21 +0000912 * @SystemID: the system ID
913 *
914 * Create the internal subset of a document
915 * Returns a pointer to the new DTD structure
916 */
917xmlDtdPtr
918xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name,
919 const xmlChar *ExternalID, const xmlChar *SystemID) {
920 xmlDtdPtr cur;
921
922 if ((doc != NULL) && (xmlGetIntSubset(doc) != NULL)) {
923#ifdef DEBUG_TREE
924 xmlGenericError(xmlGenericErrorContext,
925
926 "xmlCreateIntSubset(): document %s already have an internal subset\n",
927 doc->name);
928#endif
929 return(NULL);
930 }
931
932 /*
933 * Allocate a new DTD and fill the fields.
934 */
935 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
936 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000937 xmlTreeErrMemory("building internal subset");
Owen Taylor3473f882001-02-23 17:55:21 +0000938 return(NULL);
939 }
940 memset(cur, 0, sizeof(xmlDtd));
941 cur->type = XML_DTD_NODE;
942
William M. Bracka3215c72004-07-31 16:24:01 +0000943 if (name != NULL) {
944 cur->name = xmlStrdup(name);
945 if (cur->name == NULL) {
946 xmlTreeErrMemory("building internal subset");
947 xmlFree(cur);
948 return(NULL);
949 }
950 }
951 if (ExternalID != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +0000952 cur->ExternalID = xmlStrdup(ExternalID);
William M. Bracka3215c72004-07-31 16:24:01 +0000953 if (cur->ExternalID == NULL) {
954 xmlTreeErrMemory("building internal subset");
955 if (cur->name != NULL)
956 xmlFree((char *)cur->name);
957 xmlFree(cur);
958 return(NULL);
959 }
960 }
961 if (SystemID != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +0000962 cur->SystemID = xmlStrdup(SystemID);
William M. Bracka3215c72004-07-31 16:24:01 +0000963 if (cur->SystemID == NULL) {
964 xmlTreeErrMemory("building internal subset");
965 if (cur->name != NULL)
966 xmlFree((char *)cur->name);
967 if (cur->ExternalID != NULL)
968 xmlFree((char *)cur->ExternalID);
969 xmlFree(cur);
970 return(NULL);
971 }
972 }
Owen Taylor3473f882001-02-23 17:55:21 +0000973 if (doc != NULL) {
974 doc->intSubset = cur;
975 cur->parent = doc;
976 cur->doc = doc;
977 if (doc->children == NULL) {
978 doc->children = (xmlNodePtr) cur;
979 doc->last = (xmlNodePtr) cur;
980 } else {
Owen Taylor3473f882001-02-23 17:55:21 +0000981 if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillarde356c282001-03-10 12:32:04 +0000982 xmlNodePtr prev;
983
Owen Taylor3473f882001-02-23 17:55:21 +0000984 prev = doc->children;
985 prev->prev = (xmlNodePtr) cur;
986 cur->next = prev;
987 doc->children = (xmlNodePtr) cur;
988 } else {
Daniel Veillarde356c282001-03-10 12:32:04 +0000989 xmlNodePtr next;
990
991 next = doc->children;
992 while ((next != NULL) && (next->type != XML_ELEMENT_NODE))
993 next = next->next;
994 if (next == NULL) {
995 cur->prev = doc->last;
996 cur->prev->next = (xmlNodePtr) cur;
997 cur->next = NULL;
998 doc->last = (xmlNodePtr) cur;
999 } else {
1000 cur->next = next;
1001 cur->prev = next->prev;
1002 if (cur->prev == NULL)
1003 doc->children = (xmlNodePtr) cur;
1004 else
1005 cur->prev->next = (xmlNodePtr) cur;
1006 next->prev = (xmlNodePtr) cur;
1007 }
Owen Taylor3473f882001-02-23 17:55:21 +00001008 }
1009 }
1010 }
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001011
Daniel Veillarda880b122003-04-21 21:36:41 +00001012 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001013 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001014 return(cur);
1015}
1016
1017/**
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001018 * DICT_FREE:
1019 * @str: a string
1020 *
1021 * Free a string if it is not owned by the "dict" dictionnary in the
1022 * current scope
1023 */
1024#define DICT_FREE(str) \
1025 if ((str) && ((!dict) || \
1026 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
1027 xmlFree((char *)(str));
1028
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00001029
1030/**
1031 * DICT_COPY:
1032 * @str: a string
1033 *
1034 * Copy a string using a "dict" dictionnary in the current scope,
1035 * if availabe.
1036 */
1037#define DICT_COPY(str, cpy) \
1038 if (str) { \
1039 if (dict) { \
1040 if (xmlDictOwns(dict, (const xmlChar *)(str))) \
1041 cpy = (xmlChar *) (str); \
1042 else \
1043 cpy = (xmlChar *) xmlDictLookup((dict), (const xmlChar *)(str), -1); \
1044 } else \
1045 cpy = xmlStrdup((const xmlChar *)(str)); }
1046
1047/**
1048 * DICT_CONST_COPY:
1049 * @str: a string
1050 *
1051 * Copy a string using a "dict" dictionnary in the current scope,
1052 * if availabe.
1053 */
1054#define DICT_CONST_COPY(str, cpy) \
1055 if (str) { \
1056 if (dict) { \
1057 if (xmlDictOwns(dict, (const xmlChar *)(str))) \
1058 cpy = (const xmlChar *) (str); \
1059 else \
1060 cpy = xmlDictLookup((dict), (const xmlChar *)(str), -1); \
1061 } else \
1062 cpy = (const xmlChar *) xmlStrdup((const xmlChar *)(str)); }
1063
1064
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001065/**
Owen Taylor3473f882001-02-23 17:55:21 +00001066 * xmlFreeDtd:
1067 * @cur: the DTD structure to free up
1068 *
1069 * Free a DTD structure.
1070 */
1071void
1072xmlFreeDtd(xmlDtdPtr cur) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001073 xmlDictPtr dict = NULL;
1074
Owen Taylor3473f882001-02-23 17:55:21 +00001075 if (cur == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001076 return;
1077 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001078 if (cur->doc != NULL) dict = cur->doc->dict;
Daniel Veillard5335dc52003-01-01 20:59:38 +00001079
Daniel Veillarda880b122003-04-21 21:36:41 +00001080 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001081 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
1082
Owen Taylor3473f882001-02-23 17:55:21 +00001083 if (cur->children != NULL) {
1084 xmlNodePtr next, c = cur->children;
1085
1086 /*
William M. Brack18a04f22004-08-03 16:42:37 +00001087 * Cleanup all nodes which are not part of the specific lists
1088 * of notations, elements, attributes and entities.
Owen Taylor3473f882001-02-23 17:55:21 +00001089 */
1090 while (c != NULL) {
1091 next = c->next;
William M. Brack18a04f22004-08-03 16:42:37 +00001092 if ((c->type != XML_NOTATION_NODE) &&
1093 (c->type != XML_ELEMENT_DECL) &&
1094 (c->type != XML_ATTRIBUTE_DECL) &&
1095 (c->type != XML_ENTITY_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001096 xmlUnlinkNode(c);
1097 xmlFreeNode(c);
1098 }
1099 c = next;
1100 }
1101 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001102 DICT_FREE(cur->name)
1103 DICT_FREE(cur->SystemID)
1104 DICT_FREE(cur->ExternalID)
Owen Taylor3473f882001-02-23 17:55:21 +00001105 /* TODO !!! */
1106 if (cur->notations != NULL)
1107 xmlFreeNotationTable((xmlNotationTablePtr) cur->notations);
1108
1109 if (cur->elements != NULL)
1110 xmlFreeElementTable((xmlElementTablePtr) cur->elements);
1111 if (cur->attributes != NULL)
1112 xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes);
1113 if (cur->entities != NULL)
1114 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities);
1115 if (cur->pentities != NULL)
1116 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->pentities);
1117
Owen Taylor3473f882001-02-23 17:55:21 +00001118 xmlFree(cur);
1119}
1120
1121/**
1122 * xmlNewDoc:
1123 * @version: xmlChar string giving the version of XML "1.0"
1124 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001125 * Creates a new XML document
1126 *
Owen Taylor3473f882001-02-23 17:55:21 +00001127 * Returns a new document
1128 */
1129xmlDocPtr
1130xmlNewDoc(const xmlChar *version) {
1131 xmlDocPtr cur;
1132
1133 if (version == NULL)
1134 version = (const xmlChar *) "1.0";
1135
1136 /*
1137 * Allocate a new document and fill the fields.
1138 */
1139 cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
1140 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001141 xmlTreeErrMemory("building doc");
Owen Taylor3473f882001-02-23 17:55:21 +00001142 return(NULL);
1143 }
1144 memset(cur, 0, sizeof(xmlDoc));
1145 cur->type = XML_DOCUMENT_NODE;
1146
1147 cur->version = xmlStrdup(version);
William M. Bracka3215c72004-07-31 16:24:01 +00001148 if (cur->version == NULL) {
1149 xmlTreeErrMemory("building doc");
1150 xmlFree(cur);
1151 return(NULL);
1152 }
Owen Taylor3473f882001-02-23 17:55:21 +00001153 cur->standalone = -1;
1154 cur->compression = -1; /* not initialized */
1155 cur->doc = cur;
Daniel Veillarda6874ca2003-07-29 16:47:24 +00001156 /*
1157 * The in memory encoding is always UTF8
1158 * This field will never change and would
1159 * be obsolete if not for binary compatibility.
1160 */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00001161 cur->charset = XML_CHAR_ENCODING_UTF8;
Daniel Veillard5335dc52003-01-01 20:59:38 +00001162
Daniel Veillarda880b122003-04-21 21:36:41 +00001163 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001164 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001165 return(cur);
1166}
1167
1168/**
1169 * xmlFreeDoc:
1170 * @cur: pointer to the document
Owen Taylor3473f882001-02-23 17:55:21 +00001171 *
1172 * Free up all the structures used by a document, tree included.
1173 */
1174void
1175xmlFreeDoc(xmlDocPtr cur) {
Daniel Veillarda9142e72001-06-19 11:07:54 +00001176 xmlDtdPtr extSubset, intSubset;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001177 xmlDictPtr dict = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001178
Owen Taylor3473f882001-02-23 17:55:21 +00001179 if (cur == NULL) {
1180#ifdef DEBUG_TREE
1181 xmlGenericError(xmlGenericErrorContext,
1182 "xmlFreeDoc : document == NULL\n");
1183#endif
1184 return;
1185 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001186#ifdef LIBXML_DEBUG_RUNTIME
Daniel Veillardbca3ad22005-08-23 22:14:02 +00001187#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001188 xmlDebugCheckDocument(stderr, cur);
1189#endif
Daniel Veillardbca3ad22005-08-23 22:14:02 +00001190#endif
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001191
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001192 if (cur != NULL) dict = cur->dict;
Daniel Veillard5335dc52003-01-01 20:59:38 +00001193
Daniel Veillarda880b122003-04-21 21:36:41 +00001194 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001195 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
1196
Daniel Veillard76d66f42001-05-16 21:05:17 +00001197 /*
1198 * Do this before freeing the children list to avoid ID lookups
1199 */
1200 if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids);
1201 cur->ids = NULL;
1202 if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
1203 cur->refs = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001204 extSubset = cur->extSubset;
1205 intSubset = cur->intSubset;
Daniel Veillard5997aca2002-03-18 18:36:20 +00001206 if (intSubset == extSubset)
1207 extSubset = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001208 if (extSubset != NULL) {
Daniel Veillard76d66f42001-05-16 21:05:17 +00001209 xmlUnlinkNode((xmlNodePtr) cur->extSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001210 cur->extSubset = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001211 xmlFreeDtd(extSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001212 }
Daniel Veillarda9142e72001-06-19 11:07:54 +00001213 if (intSubset != NULL) {
Daniel Veillard76d66f42001-05-16 21:05:17 +00001214 xmlUnlinkNode((xmlNodePtr) cur->intSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001215 cur->intSubset = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001216 xmlFreeDtd(intSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001217 }
1218
1219 if (cur->children != NULL) xmlFreeNodeList(cur->children);
Owen Taylor3473f882001-02-23 17:55:21 +00001220 if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001221
1222 DICT_FREE(cur->version)
1223 DICT_FREE(cur->name)
1224 DICT_FREE(cur->encoding)
1225 DICT_FREE(cur->URL)
Owen Taylor3473f882001-02-23 17:55:21 +00001226 xmlFree(cur);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001227 if (dict) xmlDictFree(dict);
Owen Taylor3473f882001-02-23 17:55:21 +00001228}
1229
1230/**
1231 * xmlStringLenGetNodeList:
1232 * @doc: the document
1233 * @value: the value of the text
1234 * @len: the length of the string value
1235 *
1236 * Parse the value string and build the node list associated. Should
1237 * produce a flat tree with only TEXTs and ENTITY_REFs.
1238 * Returns a pointer to the first child
1239 */
1240xmlNodePtr
1241xmlStringLenGetNodeList(xmlDocPtr doc, const xmlChar *value, int len) {
1242 xmlNodePtr ret = NULL, last = NULL;
1243 xmlNodePtr node;
1244 xmlChar *val;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001245 const xmlChar *cur = value, *end = cur + len;
Owen Taylor3473f882001-02-23 17:55:21 +00001246 const xmlChar *q;
1247 xmlEntityPtr ent;
1248
1249 if (value == NULL) return(NULL);
1250
1251 q = cur;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001252 while ((cur < end) && (*cur != 0)) {
1253 if (cur[0] == '&') {
1254 int charval = 0;
1255 xmlChar tmp;
1256
Owen Taylor3473f882001-02-23 17:55:21 +00001257 /*
1258 * Save the current text.
1259 */
1260 if (cur != q) {
1261 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
1262 xmlNodeAddContentLen(last, q, cur - q);
1263 } else {
1264 node = xmlNewDocTextLen(doc, q, cur - q);
1265 if (node == NULL) return(ret);
1266 if (last == NULL)
1267 last = ret = node;
1268 else {
1269 last->next = node;
1270 node->prev = last;
1271 last = node;
1272 }
1273 }
1274 }
Owen Taylor3473f882001-02-23 17:55:21 +00001275 q = cur;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001276 if ((cur + 2 < end) && (cur[1] == '#') && (cur[2] == 'x')) {
1277 cur += 3;
1278 if (cur < end)
1279 tmp = *cur;
1280 else
1281 tmp = 0;
1282 while (tmp != ';') { /* Non input consuming loop */
1283 if ((tmp >= '0') && (tmp <= '9'))
1284 charval = charval * 16 + (tmp - '0');
1285 else if ((tmp >= 'a') && (tmp <= 'f'))
1286 charval = charval * 16 + (tmp - 'a') + 10;
1287 else if ((tmp >= 'A') && (tmp <= 'F'))
1288 charval = charval * 16 + (tmp - 'A') + 10;
Owen Taylor3473f882001-02-23 17:55:21 +00001289 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001290 xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
1291 NULL);
Daniel Veillard07cb8222003-09-10 10:51:05 +00001292 charval = 0;
1293 break;
1294 }
1295 cur++;
1296 if (cur < end)
1297 tmp = *cur;
1298 else
1299 tmp = 0;
1300 }
1301 if (tmp == ';')
1302 cur++;
1303 q = cur;
1304 } else if ((cur + 1 < end) && (cur[1] == '#')) {
1305 cur += 2;
1306 if (cur < end)
1307 tmp = *cur;
1308 else
1309 tmp = 0;
1310 while (tmp != ';') { /* Non input consuming loops */
1311 if ((tmp >= '0') && (tmp <= '9'))
1312 charval = charval * 10 + (tmp - '0');
1313 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001314 xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
1315 NULL);
Daniel Veillard07cb8222003-09-10 10:51:05 +00001316 charval = 0;
1317 break;
1318 }
1319 cur++;
1320 if (cur < end)
1321 tmp = *cur;
1322 else
1323 tmp = 0;
1324 }
1325 if (tmp == ';')
1326 cur++;
1327 q = cur;
1328 } else {
1329 /*
1330 * Read the entity string
1331 */
1332 cur++;
1333 q = cur;
1334 while ((cur < end) && (*cur != 0) && (*cur != ';')) cur++;
1335 if ((cur >= end) || (*cur == 0)) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001336 xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY, (xmlNodePtr) doc,
1337 (const char *) q);
Daniel Veillard07cb8222003-09-10 10:51:05 +00001338 return(ret);
1339 }
1340 if (cur != q) {
1341 /*
1342 * Predefined entities don't generate nodes
1343 */
1344 val = xmlStrndup(q, cur - q);
1345 ent = xmlGetDocEntity(doc, val);
1346 if ((ent != NULL) &&
1347 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
1348 if (last == NULL) {
1349 node = xmlNewDocText(doc, ent->content);
1350 last = ret = node;
1351 } else if (last->type != XML_TEXT_NODE) {
1352 node = xmlNewDocText(doc, ent->content);
1353 last = xmlAddNextSibling(last, node);
1354 } else
1355 xmlNodeAddContent(last, ent->content);
1356
1357 } else {
1358 /*
1359 * Create a new REFERENCE_REF node
1360 */
1361 node = xmlNewReference(doc, val);
1362 if (node == NULL) {
1363 if (val != NULL) xmlFree(val);
1364 return(ret);
1365 }
1366 else if ((ent != NULL) && (ent->children == NULL)) {
1367 xmlNodePtr temp;
1368
1369 ent->children = xmlStringGetNodeList(doc,
1370 (const xmlChar*)node->content);
1371 ent->owner = 1;
1372 temp = ent->children;
1373 while (temp) {
1374 temp->parent = (xmlNodePtr)ent;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001375 ent->last = temp;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001376 temp = temp->next;
1377 }
1378 }
1379 if (last == NULL) {
1380 last = ret = node;
1381 } else {
1382 last = xmlAddNextSibling(last, node);
1383 }
1384 }
1385 xmlFree(val);
1386 }
1387 cur++;
1388 q = cur;
1389 }
1390 if (charval != 0) {
1391 xmlChar buf[10];
1392 int l;
1393
1394 l = xmlCopyCharMultiByte(buf, charval);
1395 buf[l] = 0;
1396 node = xmlNewDocText(doc, buf);
1397 if (node != NULL) {
1398 if (last == NULL) {
1399 last = ret = node;
1400 } else {
1401 last = xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001402 }
1403 }
Daniel Veillard07cb8222003-09-10 10:51:05 +00001404 charval = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001405 }
Daniel Veillard07cb8222003-09-10 10:51:05 +00001406 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001407 cur++;
1408 }
Daniel Veillard07cb8222003-09-10 10:51:05 +00001409 if ((cur != q) || (ret == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001410 /*
1411 * Handle the last piece of text.
1412 */
1413 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
1414 xmlNodeAddContentLen(last, q, cur - q);
1415 } else {
1416 node = xmlNewDocTextLen(doc, q, cur - q);
1417 if (node == NULL) return(ret);
Daniel Veillard07cb8222003-09-10 10:51:05 +00001418 if (last == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001419 last = ret = node;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001420 } else {
1421 last = xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001422 }
1423 }
1424 }
1425 return(ret);
1426}
1427
1428/**
1429 * xmlStringGetNodeList:
1430 * @doc: the document
1431 * @value: the value of the attribute
1432 *
1433 * Parse the value string and build the node list associated. Should
1434 * produce a flat tree with only TEXTs and ENTITY_REFs.
1435 * Returns a pointer to the first child
1436 */
1437xmlNodePtr
1438xmlStringGetNodeList(xmlDocPtr doc, const xmlChar *value) {
1439 xmlNodePtr ret = NULL, last = NULL;
1440 xmlNodePtr node;
1441 xmlChar *val;
1442 const xmlChar *cur = value;
1443 const xmlChar *q;
1444 xmlEntityPtr ent;
1445
1446 if (value == NULL) return(NULL);
1447
1448 q = cur;
1449 while (*cur != 0) {
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001450 if (cur[0] == '&') {
1451 int charval = 0;
1452 xmlChar tmp;
1453
Owen Taylor3473f882001-02-23 17:55:21 +00001454 /*
1455 * Save the current text.
1456 */
1457 if (cur != q) {
1458 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
1459 xmlNodeAddContentLen(last, q, cur - q);
1460 } else {
1461 node = xmlNewDocTextLen(doc, q, cur - q);
1462 if (node == NULL) return(ret);
1463 if (last == NULL)
1464 last = ret = node;
1465 else {
1466 last->next = node;
1467 node->prev = last;
1468 last = node;
1469 }
1470 }
1471 }
Owen Taylor3473f882001-02-23 17:55:21 +00001472 q = cur;
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001473 if ((cur[1] == '#') && (cur[2] == 'x')) {
1474 cur += 3;
1475 tmp = *cur;
1476 while (tmp != ';') { /* Non input consuming loop */
1477 if ((tmp >= '0') && (tmp <= '9'))
1478 charval = charval * 16 + (tmp - '0');
1479 else if ((tmp >= 'a') && (tmp <= 'f'))
1480 charval = charval * 16 + (tmp - 'a') + 10;
1481 else if ((tmp >= 'A') && (tmp <= 'F'))
1482 charval = charval * 16 + (tmp - 'A') + 10;
Owen Taylor3473f882001-02-23 17:55:21 +00001483 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001484 xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
1485 NULL);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001486 charval = 0;
1487 break;
1488 }
1489 cur++;
1490 tmp = *cur;
1491 }
1492 if (tmp == ';')
1493 cur++;
1494 q = cur;
1495 } else if (cur[1] == '#') {
1496 cur += 2;
1497 tmp = *cur;
1498 while (tmp != ';') { /* Non input consuming loops */
1499 if ((tmp >= '0') && (tmp <= '9'))
1500 charval = charval * 10 + (tmp - '0');
1501 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001502 xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
1503 NULL);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001504 charval = 0;
1505 break;
1506 }
1507 cur++;
1508 tmp = *cur;
1509 }
1510 if (tmp == ';')
1511 cur++;
1512 q = cur;
1513 } else {
1514 /*
1515 * Read the entity string
1516 */
1517 cur++;
1518 q = cur;
1519 while ((*cur != 0) && (*cur != ';')) cur++;
1520 if (*cur == 0) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001521 xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY,
1522 (xmlNodePtr) doc, (const char *) q);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001523 return(ret);
1524 }
1525 if (cur != q) {
1526 /*
1527 * Predefined entities don't generate nodes
1528 */
1529 val = xmlStrndup(q, cur - q);
1530 ent = xmlGetDocEntity(doc, val);
1531 if ((ent != NULL) &&
1532 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
1533 if (last == NULL) {
1534 node = xmlNewDocText(doc, ent->content);
1535 last = ret = node;
Daniel Veillard6f42c132002-01-06 23:05:13 +00001536 } else if (last->type != XML_TEXT_NODE) {
1537 node = xmlNewDocText(doc, ent->content);
1538 last = xmlAddNextSibling(last, node);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001539 } else
1540 xmlNodeAddContent(last, ent->content);
1541
1542 } else {
1543 /*
1544 * Create a new REFERENCE_REF node
1545 */
1546 node = xmlNewReference(doc, val);
1547 if (node == NULL) {
1548 if (val != NULL) xmlFree(val);
1549 return(ret);
1550 }
Daniel Veillardbf8dae82002-04-18 16:39:10 +00001551 else if ((ent != NULL) && (ent->children == NULL)) {
1552 xmlNodePtr temp;
1553
1554 ent->children = xmlStringGetNodeList(doc,
1555 (const xmlChar*)node->content);
Daniel Veillard2d84a892002-12-30 00:01:08 +00001556 ent->owner = 1;
Daniel Veillardbf8dae82002-04-18 16:39:10 +00001557 temp = ent->children;
1558 while (temp) {
1559 temp->parent = (xmlNodePtr)ent;
1560 temp = temp->next;
1561 }
1562 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001563 if (last == NULL) {
1564 last = ret = node;
1565 } else {
1566 last = xmlAddNextSibling(last, node);
1567 }
1568 }
1569 xmlFree(val);
1570 }
1571 cur++;
1572 q = cur;
1573 }
1574 if (charval != 0) {
1575 xmlChar buf[10];
1576 int len;
1577
1578 len = xmlCopyCharMultiByte(buf, charval);
1579 buf[len] = 0;
1580 node = xmlNewDocText(doc, buf);
1581 if (node != NULL) {
1582 if (last == NULL) {
1583 last = ret = node;
1584 } else {
1585 last = xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001586 }
1587 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001588
1589 charval = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001590 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001591 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001592 cur++;
1593 }
Daniel Veillard75bea542001-05-11 17:41:21 +00001594 if ((cur != q) || (ret == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001595 /*
1596 * Handle the last piece of text.
1597 */
1598 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
1599 xmlNodeAddContentLen(last, q, cur - q);
1600 } else {
1601 node = xmlNewDocTextLen(doc, q, cur - q);
1602 if (node == NULL) return(ret);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001603 if (last == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001604 last = ret = node;
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001605 } else {
1606 last = xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001607 }
1608 }
1609 }
1610 return(ret);
1611}
1612
1613/**
1614 * xmlNodeListGetString:
1615 * @doc: the document
1616 * @list: a Node list
1617 * @inLine: should we replace entity contents or show their external form
1618 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001619 * Build the string equivalent to the text contained in the Node list
Owen Taylor3473f882001-02-23 17:55:21 +00001620 * made of TEXTs and ENTITY_REFs
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001621 *
Daniel Veillardbd9afb52002-09-25 22:25:35 +00001622 * Returns a pointer to the string copy, the caller must free it with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00001623 */
1624xmlChar *
Daniel Veillard7646b182002-04-20 06:41:40 +00001625xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine)
1626{
Owen Taylor3473f882001-02-23 17:55:21 +00001627 xmlNodePtr node = list;
1628 xmlChar *ret = NULL;
1629 xmlEntityPtr ent;
1630
Daniel Veillard7646b182002-04-20 06:41:40 +00001631 if (list == NULL)
1632 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001633
1634 while (node != NULL) {
1635 if ((node->type == XML_TEXT_NODE) ||
Daniel Veillard7646b182002-04-20 06:41:40 +00001636 (node->type == XML_CDATA_SECTION_NODE)) {
1637 if (inLine) {
1638 ret = xmlStrcat(ret, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001639 } else {
Daniel Veillard7646b182002-04-20 06:41:40 +00001640 xmlChar *buffer;
Owen Taylor3473f882001-02-23 17:55:21 +00001641
Daniel Veillard7646b182002-04-20 06:41:40 +00001642 buffer = xmlEncodeEntitiesReentrant(doc, node->content);
1643 if (buffer != NULL) {
1644 ret = xmlStrcat(ret, buffer);
1645 xmlFree(buffer);
1646 }
1647 }
1648 } else if (node->type == XML_ENTITY_REF_NODE) {
1649 if (inLine) {
1650 ent = xmlGetDocEntity(doc, node->name);
1651 if (ent != NULL) {
1652 xmlChar *buffer;
1653
1654 /* an entity content can be any "well balanced chunk",
1655 * i.e. the result of the content [43] production:
1656 * http://www.w3.org/TR/REC-xml#NT-content.
1657 * So it can contain text, CDATA section or nested
1658 * entity reference nodes (among others).
1659 * -> we recursive call xmlNodeListGetString()
1660 * which handles these types */
1661 buffer = xmlNodeListGetString(doc, ent->children, 1);
1662 if (buffer != NULL) {
1663 ret = xmlStrcat(ret, buffer);
1664 xmlFree(buffer);
1665 }
1666 } else {
1667 ret = xmlStrcat(ret, node->content);
1668 }
1669 } else {
1670 xmlChar buf[2];
1671
1672 buf[0] = '&';
1673 buf[1] = 0;
1674 ret = xmlStrncat(ret, buf, 1);
1675 ret = xmlStrcat(ret, node->name);
1676 buf[0] = ';';
1677 buf[1] = 0;
1678 ret = xmlStrncat(ret, buf, 1);
1679 }
1680 }
1681#if 0
1682 else {
1683 xmlGenericError(xmlGenericErrorContext,
1684 "xmlGetNodeListString : invalid node type %d\n",
1685 node->type);
1686 }
1687#endif
1688 node = node->next;
1689 }
1690 return (ret);
1691}
Daniel Veillard652327a2003-09-29 18:02:38 +00001692
1693#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001694/**
1695 * xmlNodeListGetRawString:
1696 * @doc: the document
1697 * @list: a Node list
1698 * @inLine: should we replace entity contents or show their external form
1699 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001700 * Builds the string equivalent to the text contained in the Node list
Owen Taylor3473f882001-02-23 17:55:21 +00001701 * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString()
1702 * this function doesn't do any character encoding handling.
1703 *
Daniel Veillardbd9afb52002-09-25 22:25:35 +00001704 * Returns a pointer to the string copy, the caller must free it with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00001705 */
1706xmlChar *
Daniel Veillard7646b182002-04-20 06:41:40 +00001707xmlNodeListGetRawString(xmlDocPtr doc, xmlNodePtr list, int inLine)
1708{
Owen Taylor3473f882001-02-23 17:55:21 +00001709 xmlNodePtr node = list;
1710 xmlChar *ret = NULL;
1711 xmlEntityPtr ent;
1712
Daniel Veillard7646b182002-04-20 06:41:40 +00001713 if (list == NULL)
1714 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001715
1716 while (node != NULL) {
Daniel Veillard7db37732001-07-12 01:20:08 +00001717 if ((node->type == XML_TEXT_NODE) ||
Daniel Veillard7646b182002-04-20 06:41:40 +00001718 (node->type == XML_CDATA_SECTION_NODE)) {
1719 if (inLine) {
1720 ret = xmlStrcat(ret, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001721 } else {
Daniel Veillard7646b182002-04-20 06:41:40 +00001722 xmlChar *buffer;
1723
1724 buffer = xmlEncodeSpecialChars(doc, node->content);
1725 if (buffer != NULL) {
1726 ret = xmlStrcat(ret, buffer);
1727 xmlFree(buffer);
1728 }
1729 }
1730 } else if (node->type == XML_ENTITY_REF_NODE) {
1731 if (inLine) {
1732 ent = xmlGetDocEntity(doc, node->name);
1733 if (ent != NULL) {
1734 xmlChar *buffer;
1735
1736 /* an entity content can be any "well balanced chunk",
1737 * i.e. the result of the content [43] production:
1738 * http://www.w3.org/TR/REC-xml#NT-content.
1739 * So it can contain text, CDATA section or nested
1740 * entity reference nodes (among others).
1741 * -> we recursive call xmlNodeListGetRawString()
1742 * which handles these types */
1743 buffer =
1744 xmlNodeListGetRawString(doc, ent->children, 1);
1745 if (buffer != NULL) {
1746 ret = xmlStrcat(ret, buffer);
1747 xmlFree(buffer);
1748 }
1749 } else {
1750 ret = xmlStrcat(ret, node->content);
1751 }
1752 } else {
1753 xmlChar buf[2];
1754
1755 buf[0] = '&';
1756 buf[1] = 0;
1757 ret = xmlStrncat(ret, buf, 1);
1758 ret = xmlStrcat(ret, node->name);
1759 buf[0] = ';';
1760 buf[1] = 0;
1761 ret = xmlStrncat(ret, buf, 1);
1762 }
1763 }
Owen Taylor3473f882001-02-23 17:55:21 +00001764#if 0
Daniel Veillard7646b182002-04-20 06:41:40 +00001765 else {
1766 xmlGenericError(xmlGenericErrorContext,
1767 "xmlGetNodeListString : invalid node type %d\n",
1768 node->type);
1769 }
Owen Taylor3473f882001-02-23 17:55:21 +00001770#endif
Daniel Veillard7646b182002-04-20 06:41:40 +00001771 node = node->next;
Owen Taylor3473f882001-02-23 17:55:21 +00001772 }
Daniel Veillard7646b182002-04-20 06:41:40 +00001773 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00001774}
Daniel Veillard652327a2003-09-29 18:02:38 +00001775#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001776
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001777static xmlAttrPtr
1778xmlNewPropInternal(xmlNodePtr node, xmlNsPtr ns,
1779 const xmlChar * name, const xmlChar * value,
1780 int eatname)
1781{
Owen Taylor3473f882001-02-23 17:55:21 +00001782 xmlAttrPtr cur;
1783 xmlDocPtr doc = NULL;
1784
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001785 if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) {
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001786 if (eatname == 1)
1787 xmlFree((xmlChar *) name);
1788 return (NULL);
1789 }
Owen Taylor3473f882001-02-23 17:55:21 +00001790
1791 /*
1792 * Allocate a new property and fill the fields.
1793 */
1794 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1795 if (cur == NULL) {
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001796 if (eatname == 1)
1797 xmlFree((xmlChar *) name);
1798 xmlTreeErrMemory("building attribute");
1799 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001800 }
1801 memset(cur, 0, sizeof(xmlAttr));
1802 cur->type = XML_ATTRIBUTE_NODE;
1803
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001804 cur->parent = node;
Owen Taylor3473f882001-02-23 17:55:21 +00001805 if (node != NULL) {
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001806 doc = node->doc;
1807 cur->doc = doc;
Owen Taylor3473f882001-02-23 17:55:21 +00001808 }
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001809 cur->ns = ns;
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001810
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001811 if (eatname == 0) {
1812 if ((doc != NULL) && (doc->dict != NULL))
1813 cur->name = (xmlChar *) xmlDictLookup(doc->dict, name, -1);
1814 else
1815 cur->name = xmlStrdup(name);
1816 } else
1817 cur->name = name;
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001818
Owen Taylor3473f882001-02-23 17:55:21 +00001819 if (value != NULL) {
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001820 xmlNodePtr tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00001821
Daniel Veillard6f8611f2008-02-15 08:33:21 +00001822 if(!xmlCheckUTF8(value)) {
1823 xmlTreeErr(XML_TREE_NOT_UTF8, (xmlNodePtr) doc,
1824 NULL);
1825 if (doc != NULL)
1826 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
1827 }
1828 cur->children = xmlNewDocText(doc, value);
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001829 cur->last = NULL;
1830 tmp = cur->children;
1831 while (tmp != NULL) {
1832 tmp->parent = (xmlNodePtr) cur;
1833 if (tmp->next == NULL)
1834 cur->last = tmp;
1835 tmp = tmp->next;
1836 }
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001837 }
Owen Taylor3473f882001-02-23 17:55:21 +00001838
1839 /*
1840 * Add it at the end to preserve parsing order ...
1841 */
1842 if (node != NULL) {
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001843 if (node->properties == NULL) {
1844 node->properties = cur;
1845 } else {
1846 xmlAttrPtr prev = node->properties;
Owen Taylor3473f882001-02-23 17:55:21 +00001847
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001848 while (prev->next != NULL)
1849 prev = prev->next;
1850 prev->next = cur;
1851 cur->prev = prev;
1852 }
Owen Taylor3473f882001-02-23 17:55:21 +00001853 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00001854
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001855 if (xmlIsID((node == NULL) ? NULL : node->doc, node, cur) == 1)
1856 xmlAddID(NULL, node->doc, value, cur);
1857
Daniel Veillarda880b122003-04-21 21:36:41 +00001858 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001859 xmlRegisterNodeDefaultValue((xmlNodePtr) cur);
1860 return (cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001861}
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001862
1863#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
1864 defined(LIBXML_SCHEMAS_ENABLED)
1865/**
1866 * xmlNewProp:
1867 * @node: the holding node
1868 * @name: the name of the attribute
1869 * @value: the value of the attribute
1870 *
1871 * Create a new property carried by a node.
1872 * Returns a pointer to the attribute
1873 */
1874xmlAttrPtr
1875xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
1876
1877 if (name == NULL) {
1878#ifdef DEBUG_TREE
1879 xmlGenericError(xmlGenericErrorContext,
1880 "xmlNewProp : name == NULL\n");
1881#endif
1882 return(NULL);
1883 }
1884
1885 return xmlNewPropInternal(node, NULL, name, value, 0);
1886}
Daniel Veillard652327a2003-09-29 18:02:38 +00001887#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001888
1889/**
1890 * xmlNewNsProp:
1891 * @node: the holding node
1892 * @ns: the namespace
1893 * @name: the name of the attribute
1894 * @value: the value of the attribute
1895 *
1896 * Create a new property tagged with a namespace and carried by a node.
1897 * Returns a pointer to the attribute
1898 */
1899xmlAttrPtr
1900xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
1901 const xmlChar *value) {
Owen Taylor3473f882001-02-23 17:55:21 +00001902
1903 if (name == NULL) {
1904#ifdef DEBUG_TREE
1905 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00001906 "xmlNewNsProp : name == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001907#endif
1908 return(NULL);
1909 }
1910
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001911 return xmlNewPropInternal(node, ns, name, value, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001912}
1913
1914/**
Daniel Veillard46de64e2002-05-29 08:21:33 +00001915 * xmlNewNsPropEatName:
1916 * @node: the holding node
1917 * @ns: the namespace
1918 * @name: the name of the attribute
1919 * @value: the value of the attribute
1920 *
1921 * Create a new property tagged with a namespace and carried by a node.
1922 * Returns a pointer to the attribute
1923 */
1924xmlAttrPtr
1925xmlNewNsPropEatName(xmlNodePtr node, xmlNsPtr ns, xmlChar *name,
1926 const xmlChar *value) {
Daniel Veillard46de64e2002-05-29 08:21:33 +00001927
1928 if (name == NULL) {
1929#ifdef DEBUG_TREE
1930 xmlGenericError(xmlGenericErrorContext,
1931 "xmlNewNsPropEatName : name == NULL\n");
1932#endif
1933 return(NULL);
1934 }
1935
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001936 return xmlNewPropInternal(node, ns, name, value, 1);
Daniel Veillard46de64e2002-05-29 08:21:33 +00001937}
1938
1939/**
Owen Taylor3473f882001-02-23 17:55:21 +00001940 * xmlNewDocProp:
1941 * @doc: the document
1942 * @name: the name of the attribute
1943 * @value: the value of the attribute
1944 *
1945 * Create a new property carried by a document.
1946 * Returns a pointer to the attribute
1947 */
1948xmlAttrPtr
1949xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) {
1950 xmlAttrPtr cur;
1951
1952 if (name == NULL) {
1953#ifdef DEBUG_TREE
1954 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00001955 "xmlNewDocProp : name == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001956#endif
1957 return(NULL);
1958 }
1959
1960 /*
1961 * Allocate a new property and fill the fields.
1962 */
1963 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1964 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001965 xmlTreeErrMemory("building attribute");
Owen Taylor3473f882001-02-23 17:55:21 +00001966 return(NULL);
1967 }
1968 memset(cur, 0, sizeof(xmlAttr));
1969 cur->type = XML_ATTRIBUTE_NODE;
1970
Daniel Veillard03a53c32004-10-26 16:06:51 +00001971 if ((doc != NULL) && (doc->dict != NULL))
1972 cur->name = xmlDictLookup(doc->dict, name, -1);
1973 else
1974 cur->name = xmlStrdup(name);
Owen Taylor3473f882001-02-23 17:55:21 +00001975 cur->doc = doc;
1976 if (value != NULL) {
1977 xmlNodePtr tmp;
1978
1979 cur->children = xmlStringGetNodeList(doc, value);
1980 cur->last = NULL;
1981
1982 tmp = cur->children;
1983 while (tmp != NULL) {
1984 tmp->parent = (xmlNodePtr) cur;
1985 if (tmp->next == NULL)
1986 cur->last = tmp;
1987 tmp = tmp->next;
1988 }
1989 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00001990
Daniel Veillarda880b122003-04-21 21:36:41 +00001991 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001992 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001993 return(cur);
1994}
1995
1996/**
1997 * xmlFreePropList:
1998 * @cur: the first property in the list
1999 *
2000 * Free a property and all its siblings, all the children are freed too.
2001 */
2002void
2003xmlFreePropList(xmlAttrPtr cur) {
2004 xmlAttrPtr next;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002005 if (cur == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +00002006 while (cur != NULL) {
2007 next = cur->next;
2008 xmlFreeProp(cur);
2009 cur = next;
2010 }
2011}
2012
2013/**
2014 * xmlFreeProp:
2015 * @cur: an attribute
2016 *
2017 * Free one attribute, all the content is freed too
2018 */
2019void
2020xmlFreeProp(xmlAttrPtr cur) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002021 xmlDictPtr dict = NULL;
2022 if (cur == NULL) return;
2023
2024 if (cur->doc != NULL) dict = cur->doc->dict;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002025
Daniel Veillarda880b122003-04-21 21:36:41 +00002026 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002027 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
2028
Owen Taylor3473f882001-02-23 17:55:21 +00002029 /* Check for ID removal -> leading to invalid references ! */
Daniel Veillardda6f4af2005-06-20 17:17:54 +00002030 if ((cur->doc != NULL) && (cur->atype == XML_ATTRIBUTE_ID)) {
2031 xmlRemoveID(cur->doc, cur);
Daniel Veillard76d66f42001-05-16 21:05:17 +00002032 }
Owen Taylor3473f882001-02-23 17:55:21 +00002033 if (cur->children != NULL) xmlFreeNodeList(cur->children);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002034 DICT_FREE(cur->name)
Owen Taylor3473f882001-02-23 17:55:21 +00002035 xmlFree(cur);
2036}
2037
2038/**
2039 * xmlRemoveProp:
2040 * @cur: an attribute
2041 *
2042 * Unlink and free one attribute, all the content is freed too
2043 * Note this doesn't work for namespace definition attributes
2044 *
2045 * Returns 0 if success and -1 in case of error.
2046 */
2047int
2048xmlRemoveProp(xmlAttrPtr cur) {
2049 xmlAttrPtr tmp;
2050 if (cur == NULL) {
2051#ifdef DEBUG_TREE
2052 xmlGenericError(xmlGenericErrorContext,
2053 "xmlRemoveProp : cur == NULL\n");
2054#endif
2055 return(-1);
2056 }
2057 if (cur->parent == NULL) {
2058#ifdef DEBUG_TREE
2059 xmlGenericError(xmlGenericErrorContext,
2060 "xmlRemoveProp : cur->parent == NULL\n");
2061#endif
2062 return(-1);
2063 }
2064 tmp = cur->parent->properties;
2065 if (tmp == cur) {
2066 cur->parent->properties = cur->next;
Rob Richards19dc9612005-10-28 16:15:16 +00002067 if (cur->next != NULL)
2068 cur->next->prev = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002069 xmlFreeProp(cur);
2070 return(0);
2071 }
2072 while (tmp != NULL) {
2073 if (tmp->next == cur) {
2074 tmp->next = cur->next;
2075 if (tmp->next != NULL)
2076 tmp->next->prev = tmp;
2077 xmlFreeProp(cur);
2078 return(0);
2079 }
2080 tmp = tmp->next;
2081 }
2082#ifdef DEBUG_TREE
2083 xmlGenericError(xmlGenericErrorContext,
2084 "xmlRemoveProp : attribute not owned by its node\n");
2085#endif
2086 return(-1);
2087}
2088
2089/**
Daniel Veillard03a53c32004-10-26 16:06:51 +00002090 * xmlNewDocPI:
2091 * @doc: the target document
Owen Taylor3473f882001-02-23 17:55:21 +00002092 * @name: the processing instruction name
2093 * @content: the PI content
2094 *
2095 * Creation of a processing instruction element.
2096 * Returns a pointer to the new node object.
2097 */
2098xmlNodePtr
Daniel Veillard03a53c32004-10-26 16:06:51 +00002099xmlNewDocPI(xmlDocPtr doc, const xmlChar *name, const xmlChar *content) {
Owen Taylor3473f882001-02-23 17:55:21 +00002100 xmlNodePtr cur;
2101
2102 if (name == NULL) {
2103#ifdef DEBUG_TREE
2104 xmlGenericError(xmlGenericErrorContext,
2105 "xmlNewPI : name == NULL\n");
2106#endif
2107 return(NULL);
2108 }
2109
2110 /*
2111 * Allocate a new node and fill the fields.
2112 */
2113 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2114 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002115 xmlTreeErrMemory("building PI");
Owen Taylor3473f882001-02-23 17:55:21 +00002116 return(NULL);
2117 }
2118 memset(cur, 0, sizeof(xmlNode));
2119 cur->type = XML_PI_NODE;
2120
Daniel Veillard03a53c32004-10-26 16:06:51 +00002121 if ((doc != NULL) && (doc->dict != NULL))
2122 cur->name = xmlDictLookup(doc->dict, name, -1);
2123 else
2124 cur->name = xmlStrdup(name);
Owen Taylor3473f882001-02-23 17:55:21 +00002125 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002126 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00002127 }
Daniel Veillarda03e3652004-11-02 18:45:30 +00002128 cur->doc = doc;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002129
Daniel Veillarda880b122003-04-21 21:36:41 +00002130 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002131 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002132 return(cur);
2133}
2134
2135/**
Daniel Veillard03a53c32004-10-26 16:06:51 +00002136 * xmlNewPI:
2137 * @name: the processing instruction name
2138 * @content: the PI content
2139 *
2140 * Creation of a processing instruction element.
2141 * Use xmlDocNewPI preferably to get string interning
2142 *
2143 * Returns a pointer to the new node object.
2144 */
2145xmlNodePtr
2146xmlNewPI(const xmlChar *name, const xmlChar *content) {
2147 return(xmlNewDocPI(NULL, name, content));
2148}
2149
2150/**
Owen Taylor3473f882001-02-23 17:55:21 +00002151 * xmlNewNode:
2152 * @ns: namespace if any
2153 * @name: the node name
2154 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002155 * Creation of a new node element. @ns is optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00002156 *
William M. Brackd7cf7f82003-11-14 07:13:16 +00002157 * Returns a pointer to the new node object. Uses xmlStrdup() to make
2158 * copy of @name.
Owen Taylor3473f882001-02-23 17:55:21 +00002159 */
2160xmlNodePtr
2161xmlNewNode(xmlNsPtr ns, const xmlChar *name) {
2162 xmlNodePtr cur;
2163
2164 if (name == NULL) {
2165#ifdef DEBUG_TREE
2166 xmlGenericError(xmlGenericErrorContext,
2167 "xmlNewNode : name == NULL\n");
2168#endif
2169 return(NULL);
2170 }
2171
2172 /*
2173 * Allocate a new node and fill the fields.
2174 */
2175 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2176 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002177 xmlTreeErrMemory("building node");
Owen Taylor3473f882001-02-23 17:55:21 +00002178 return(NULL);
2179 }
2180 memset(cur, 0, sizeof(xmlNode));
2181 cur->type = XML_ELEMENT_NODE;
2182
2183 cur->name = xmlStrdup(name);
2184 cur->ns = ns;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002185
Daniel Veillarda880b122003-04-21 21:36:41 +00002186 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002187 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002188 return(cur);
2189}
2190
2191/**
Daniel Veillard46de64e2002-05-29 08:21:33 +00002192 * xmlNewNodeEatName:
2193 * @ns: namespace if any
2194 * @name: the node name
2195 *
2196 * Creation of a new node element. @ns is optional (NULL).
2197 *
William M. Brackd7cf7f82003-11-14 07:13:16 +00002198 * Returns a pointer to the new node object, with pointer @name as
2199 * new node's name. Use xmlNewNode() if a copy of @name string is
2200 * is needed as new node's name.
Daniel Veillard46de64e2002-05-29 08:21:33 +00002201 */
2202xmlNodePtr
2203xmlNewNodeEatName(xmlNsPtr ns, xmlChar *name) {
2204 xmlNodePtr cur;
2205
2206 if (name == NULL) {
2207#ifdef DEBUG_TREE
2208 xmlGenericError(xmlGenericErrorContext,
2209 "xmlNewNode : name == NULL\n");
2210#endif
2211 return(NULL);
2212 }
2213
2214 /*
2215 * Allocate a new node and fill the fields.
2216 */
2217 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2218 if (cur == NULL) {
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00002219 xmlFree(name);
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002220 xmlTreeErrMemory("building node");
Daniel Veillard46de64e2002-05-29 08:21:33 +00002221 return(NULL);
2222 }
2223 memset(cur, 0, sizeof(xmlNode));
2224 cur->type = XML_ELEMENT_NODE;
2225
2226 cur->name = name;
2227 cur->ns = ns;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002228
Daniel Veillarda880b122003-04-21 21:36:41 +00002229 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002230 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Daniel Veillard46de64e2002-05-29 08:21:33 +00002231 return(cur);
2232}
2233
2234/**
Owen Taylor3473f882001-02-23 17:55:21 +00002235 * xmlNewDocNode:
2236 * @doc: the document
2237 * @ns: namespace if any
2238 * @name: the node name
2239 * @content: the XML text content if any
2240 *
2241 * Creation of a new node element within a document. @ns and @content
Daniel Veillardd1640922001-12-17 15:30:10 +00002242 * are optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00002243 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2244 * references, but XML special chars need to be escaped first by using
2245 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2246 * need entities support.
2247 *
2248 * Returns a pointer to the new node object.
2249 */
2250xmlNodePtr
2251xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns,
2252 const xmlChar *name, const xmlChar *content) {
2253 xmlNodePtr cur;
2254
Daniel Veillard95ddcd32004-10-26 21:53:55 +00002255 if ((doc != NULL) && (doc->dict != NULL))
Daniel Veillard03a53c32004-10-26 16:06:51 +00002256 cur = xmlNewNodeEatName(ns, (xmlChar *)
2257 xmlDictLookup(doc->dict, name, -1));
2258 else
2259 cur = xmlNewNode(ns, name);
Owen Taylor3473f882001-02-23 17:55:21 +00002260 if (cur != NULL) {
2261 cur->doc = doc;
2262 if (content != NULL) {
2263 cur->children = xmlStringGetNodeList(doc, content);
2264 UPDATE_LAST_CHILD_AND_PARENT(cur)
2265 }
2266 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002267
Owen Taylor3473f882001-02-23 17:55:21 +00002268 return(cur);
2269}
2270
Daniel Veillard46de64e2002-05-29 08:21:33 +00002271/**
2272 * xmlNewDocNodeEatName:
2273 * @doc: the document
2274 * @ns: namespace if any
2275 * @name: the node name
2276 * @content: the XML text content if any
2277 *
2278 * Creation of a new node element within a document. @ns and @content
2279 * are optional (NULL).
2280 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2281 * references, but XML special chars need to be escaped first by using
2282 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2283 * need entities support.
2284 *
2285 * Returns a pointer to the new node object.
2286 */
2287xmlNodePtr
2288xmlNewDocNodeEatName(xmlDocPtr doc, xmlNsPtr ns,
2289 xmlChar *name, const xmlChar *content) {
2290 xmlNodePtr cur;
2291
2292 cur = xmlNewNodeEatName(ns, name);
2293 if (cur != NULL) {
2294 cur->doc = doc;
2295 if (content != NULL) {
2296 cur->children = xmlStringGetNodeList(doc, content);
2297 UPDATE_LAST_CHILD_AND_PARENT(cur)
2298 }
2299 }
2300 return(cur);
2301}
2302
Daniel Veillard652327a2003-09-29 18:02:38 +00002303#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002304/**
2305 * xmlNewDocRawNode:
2306 * @doc: the document
2307 * @ns: namespace if any
2308 * @name: the node name
2309 * @content: the text content if any
2310 *
2311 * Creation of a new node element within a document. @ns and @content
Daniel Veillardd1640922001-12-17 15:30:10 +00002312 * are optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00002313 *
2314 * Returns a pointer to the new node object.
2315 */
2316xmlNodePtr
2317xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns,
2318 const xmlChar *name, const xmlChar *content) {
2319 xmlNodePtr cur;
2320
Daniel Veillard95ddcd32004-10-26 21:53:55 +00002321 cur = xmlNewDocNode(doc, ns, name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002322 if (cur != NULL) {
2323 cur->doc = doc;
2324 if (content != NULL) {
2325 cur->children = xmlNewDocText(doc, content);
2326 UPDATE_LAST_CHILD_AND_PARENT(cur)
2327 }
2328 }
2329 return(cur);
2330}
2331
2332/**
2333 * xmlNewDocFragment:
2334 * @doc: the document owning the fragment
2335 *
2336 * Creation of a new Fragment node.
2337 * Returns a pointer to the new node object.
2338 */
2339xmlNodePtr
2340xmlNewDocFragment(xmlDocPtr doc) {
2341 xmlNodePtr cur;
2342
2343 /*
2344 * Allocate a new DocumentFragment node and fill the fields.
2345 */
2346 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2347 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002348 xmlTreeErrMemory("building fragment");
Owen Taylor3473f882001-02-23 17:55:21 +00002349 return(NULL);
2350 }
2351 memset(cur, 0, sizeof(xmlNode));
2352 cur->type = XML_DOCUMENT_FRAG_NODE;
2353
2354 cur->doc = doc;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002355
Daniel Veillarda880b122003-04-21 21:36:41 +00002356 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002357 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002358 return(cur);
2359}
Daniel Veillard652327a2003-09-29 18:02:38 +00002360#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002361
2362/**
2363 * xmlNewText:
2364 * @content: the text content
2365 *
2366 * Creation of a new text node.
2367 * Returns a pointer to the new node object.
2368 */
2369xmlNodePtr
2370xmlNewText(const xmlChar *content) {
2371 xmlNodePtr cur;
2372
2373 /*
2374 * Allocate a new node and fill the fields.
2375 */
2376 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2377 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002378 xmlTreeErrMemory("building text");
Owen Taylor3473f882001-02-23 17:55:21 +00002379 return(NULL);
2380 }
2381 memset(cur, 0, sizeof(xmlNode));
2382 cur->type = XML_TEXT_NODE;
2383
2384 cur->name = xmlStringText;
2385 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002386 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00002387 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002388
Daniel Veillarda880b122003-04-21 21:36:41 +00002389 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002390 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002391 return(cur);
2392}
2393
Daniel Veillard652327a2003-09-29 18:02:38 +00002394#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002395/**
2396 * xmlNewTextChild:
2397 * @parent: the parent node
2398 * @ns: a namespace if any
2399 * @name: the name of the child
2400 * @content: the text content of the child if any.
2401 *
2402 * Creation of a new child element, added at the end of @parent children list.
MST 2003 John Flecke1f70492003-12-20 23:43:28 +00002403 * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
2404 * created element inherits the namespace of @parent. If @content is non NULL,
William M. Brackd7cf7f82003-11-14 07:13:16 +00002405 * a child TEXT node will be created containing the string @content.
MST 2003 John Fleck8b03bc52003-12-17 03:45:01 +00002406 * NOTE: Use xmlNewChild() if @content will contain entities that need to be
2407 * preserved. Use this function, xmlNewTextChild(), if you need to ensure that
2408 * reserved XML chars that might appear in @content, such as the ampersand,
2409 * greater-than or less-than signs, are automatically replaced by their XML
2410 * escaped entity representations.
Owen Taylor3473f882001-02-23 17:55:21 +00002411 *
2412 * Returns a pointer to the new node object.
2413 */
2414xmlNodePtr
2415xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns,
2416 const xmlChar *name, const xmlChar *content) {
2417 xmlNodePtr cur, prev;
2418
2419 if (parent == NULL) {
2420#ifdef DEBUG_TREE
2421 xmlGenericError(xmlGenericErrorContext,
2422 "xmlNewTextChild : parent == NULL\n");
2423#endif
2424 return(NULL);
2425 }
2426
2427 if (name == NULL) {
2428#ifdef DEBUG_TREE
2429 xmlGenericError(xmlGenericErrorContext,
2430 "xmlNewTextChild : name == NULL\n");
2431#endif
2432 return(NULL);
2433 }
2434
2435 /*
2436 * Allocate a new node
2437 */
Daniel Veillard254b1262003-11-01 17:04:58 +00002438 if (parent->type == XML_ELEMENT_NODE) {
2439 if (ns == NULL)
2440 cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content);
2441 else
2442 cur = xmlNewDocRawNode(parent->doc, ns, name, content);
2443 } else if ((parent->type == XML_DOCUMENT_NODE) ||
2444 (parent->type == XML_HTML_DOCUMENT_NODE)) {
2445 if (ns == NULL)
2446 cur = xmlNewDocRawNode((xmlDocPtr) parent, NULL, name, content);
2447 else
2448 cur = xmlNewDocRawNode((xmlDocPtr) parent, ns, name, content);
2449 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2450 cur = xmlNewDocRawNode( parent->doc, ns, name, content);
2451 } else {
2452 return(NULL);
2453 }
Owen Taylor3473f882001-02-23 17:55:21 +00002454 if (cur == NULL) return(NULL);
2455
2456 /*
2457 * add the new element at the end of the children list.
2458 */
2459 cur->type = XML_ELEMENT_NODE;
2460 cur->parent = parent;
2461 cur->doc = parent->doc;
2462 if (parent->children == NULL) {
2463 parent->children = cur;
2464 parent->last = cur;
2465 } else {
2466 prev = parent->last;
2467 prev->next = cur;
2468 cur->prev = prev;
2469 parent->last = cur;
2470 }
2471
2472 return(cur);
2473}
Daniel Veillard652327a2003-09-29 18:02:38 +00002474#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002475
2476/**
2477 * xmlNewCharRef:
2478 * @doc: the document
2479 * @name: the char ref string, starting with # or "&# ... ;"
2480 *
2481 * Creation of a new character reference node.
2482 * Returns a pointer to the new node object.
2483 */
2484xmlNodePtr
2485xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) {
2486 xmlNodePtr cur;
2487
Daniel Veillard36e5cd52004-11-02 14:52:23 +00002488 if (name == NULL)
2489 return(NULL);
2490
Owen Taylor3473f882001-02-23 17:55:21 +00002491 /*
2492 * Allocate a new node and fill the fields.
2493 */
2494 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2495 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002496 xmlTreeErrMemory("building character reference");
Owen Taylor3473f882001-02-23 17:55:21 +00002497 return(NULL);
2498 }
2499 memset(cur, 0, sizeof(xmlNode));
2500 cur->type = XML_ENTITY_REF_NODE;
2501
2502 cur->doc = doc;
2503 if (name[0] == '&') {
2504 int len;
2505 name++;
2506 len = xmlStrlen(name);
2507 if (name[len - 1] == ';')
2508 cur->name = xmlStrndup(name, len - 1);
2509 else
2510 cur->name = xmlStrndup(name, len);
2511 } else
2512 cur->name = xmlStrdup(name);
Daniel Veillard5335dc52003-01-01 20:59:38 +00002513
Daniel Veillarda880b122003-04-21 21:36:41 +00002514 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002515 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002516 return(cur);
2517}
2518
2519/**
2520 * xmlNewReference:
2521 * @doc: the document
2522 * @name: the reference name, or the reference string with & and ;
2523 *
2524 * Creation of a new reference node.
2525 * Returns a pointer to the new node object.
2526 */
2527xmlNodePtr
2528xmlNewReference(xmlDocPtr doc, const xmlChar *name) {
2529 xmlNodePtr cur;
2530 xmlEntityPtr ent;
2531
Daniel Veillard36e5cd52004-11-02 14:52:23 +00002532 if (name == NULL)
2533 return(NULL);
2534
Owen Taylor3473f882001-02-23 17:55:21 +00002535 /*
2536 * Allocate a new node and fill the fields.
2537 */
2538 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2539 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002540 xmlTreeErrMemory("building reference");
Owen Taylor3473f882001-02-23 17:55:21 +00002541 return(NULL);
2542 }
2543 memset(cur, 0, sizeof(xmlNode));
2544 cur->type = XML_ENTITY_REF_NODE;
2545
2546 cur->doc = doc;
2547 if (name[0] == '&') {
2548 int len;
2549 name++;
2550 len = xmlStrlen(name);
2551 if (name[len - 1] == ';')
2552 cur->name = xmlStrndup(name, len - 1);
2553 else
2554 cur->name = xmlStrndup(name, len);
2555 } else
2556 cur->name = xmlStrdup(name);
2557
2558 ent = xmlGetDocEntity(doc, cur->name);
2559 if (ent != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002560 cur->content = ent->content;
Owen Taylor3473f882001-02-23 17:55:21 +00002561 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002562 * The parent pointer in entity is a DTD pointer and thus is NOT
Owen Taylor3473f882001-02-23 17:55:21 +00002563 * updated. Not sure if this is 100% correct.
2564 * -George
2565 */
2566 cur->children = (xmlNodePtr) ent;
2567 cur->last = (xmlNodePtr) ent;
2568 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002569
Daniel Veillarda880b122003-04-21 21:36:41 +00002570 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002571 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002572 return(cur);
2573}
2574
2575/**
2576 * xmlNewDocText:
2577 * @doc: the document
2578 * @content: the text content
2579 *
2580 * Creation of a new text node within a document.
2581 * Returns a pointer to the new node object.
2582 */
2583xmlNodePtr
2584xmlNewDocText(xmlDocPtr doc, const xmlChar *content) {
2585 xmlNodePtr cur;
2586
2587 cur = xmlNewText(content);
2588 if (cur != NULL) cur->doc = doc;
2589 return(cur);
2590}
2591
2592/**
2593 * xmlNewTextLen:
2594 * @content: the text content
2595 * @len: the text len.
2596 *
Daniel Veillard60087f32001-10-10 09:45:09 +00002597 * Creation of a new text node with an extra parameter for the content's length
Owen Taylor3473f882001-02-23 17:55:21 +00002598 * Returns a pointer to the new node object.
2599 */
2600xmlNodePtr
2601xmlNewTextLen(const xmlChar *content, int len) {
2602 xmlNodePtr cur;
2603
2604 /*
2605 * Allocate a new node and fill the fields.
2606 */
2607 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2608 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002609 xmlTreeErrMemory("building text");
Owen Taylor3473f882001-02-23 17:55:21 +00002610 return(NULL);
2611 }
2612 memset(cur, 0, sizeof(xmlNode));
2613 cur->type = XML_TEXT_NODE;
2614
2615 cur->name = xmlStringText;
2616 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002617 cur->content = xmlStrndup(content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00002618 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002619
Daniel Veillarda880b122003-04-21 21:36:41 +00002620 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002621 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002622 return(cur);
2623}
2624
2625/**
2626 * xmlNewDocTextLen:
2627 * @doc: the document
2628 * @content: the text content
2629 * @len: the text len.
2630 *
Daniel Veillard60087f32001-10-10 09:45:09 +00002631 * Creation of a new text node with an extra content length parameter. The
Owen Taylor3473f882001-02-23 17:55:21 +00002632 * text node pertain to a given document.
2633 * Returns a pointer to the new node object.
2634 */
2635xmlNodePtr
2636xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) {
2637 xmlNodePtr cur;
2638
2639 cur = xmlNewTextLen(content, len);
2640 if (cur != NULL) cur->doc = doc;
2641 return(cur);
2642}
2643
2644/**
2645 * xmlNewComment:
2646 * @content: the comment content
2647 *
2648 * Creation of a new node containing a comment.
2649 * Returns a pointer to the new node object.
2650 */
2651xmlNodePtr
2652xmlNewComment(const xmlChar *content) {
2653 xmlNodePtr cur;
2654
2655 /*
2656 * Allocate a new node and fill the fields.
2657 */
2658 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2659 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002660 xmlTreeErrMemory("building comment");
Owen Taylor3473f882001-02-23 17:55:21 +00002661 return(NULL);
2662 }
2663 memset(cur, 0, sizeof(xmlNode));
2664 cur->type = XML_COMMENT_NODE;
2665
2666 cur->name = xmlStringComment;
2667 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002668 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00002669 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002670
Daniel Veillarda880b122003-04-21 21:36:41 +00002671 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002672 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002673 return(cur);
2674}
2675
2676/**
2677 * xmlNewCDataBlock:
2678 * @doc: the document
Daniel Veillardd1640922001-12-17 15:30:10 +00002679 * @content: the CDATA block content content
Owen Taylor3473f882001-02-23 17:55:21 +00002680 * @len: the length of the block
2681 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002682 * Creation of a new node containing a CDATA block.
Owen Taylor3473f882001-02-23 17:55:21 +00002683 * Returns a pointer to the new node object.
2684 */
2685xmlNodePtr
2686xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) {
2687 xmlNodePtr cur;
2688
2689 /*
2690 * Allocate a new node and fill the fields.
2691 */
2692 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2693 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002694 xmlTreeErrMemory("building CDATA");
Owen Taylor3473f882001-02-23 17:55:21 +00002695 return(NULL);
2696 }
2697 memset(cur, 0, sizeof(xmlNode));
2698 cur->type = XML_CDATA_SECTION_NODE;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002699 cur->doc = doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002700
2701 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002702 cur->content = xmlStrndup(content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00002703 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002704
Daniel Veillarda880b122003-04-21 21:36:41 +00002705 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002706 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002707 return(cur);
2708}
2709
2710/**
2711 * xmlNewDocComment:
2712 * @doc: the document
2713 * @content: the comment content
2714 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002715 * Creation of a new node containing a comment within a document.
Owen Taylor3473f882001-02-23 17:55:21 +00002716 * Returns a pointer to the new node object.
2717 */
2718xmlNodePtr
2719xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) {
2720 xmlNodePtr cur;
2721
2722 cur = xmlNewComment(content);
2723 if (cur != NULL) cur->doc = doc;
2724 return(cur);
2725}
2726
2727/**
2728 * xmlSetTreeDoc:
2729 * @tree: the top element
2730 * @doc: the document
2731 *
2732 * update all nodes under the tree to point to the right document
2733 */
2734void
2735xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) {
Daniel Veillard19e96c32001-07-09 10:32:59 +00002736 xmlAttrPtr prop;
2737
Owen Taylor3473f882001-02-23 17:55:21 +00002738 if (tree == NULL)
2739 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002740 if (tree->doc != doc) {
Daniel Veillard36065812002-01-24 15:02:46 +00002741 if(tree->type == XML_ELEMENT_NODE) {
2742 prop = tree->properties;
2743 while (prop != NULL) {
2744 prop->doc = doc;
2745 xmlSetListDoc(prop->children, doc);
2746 prop = prop->next;
2747 }
Daniel Veillard19e96c32001-07-09 10:32:59 +00002748 }
Owen Taylor3473f882001-02-23 17:55:21 +00002749 if (tree->children != NULL)
2750 xmlSetListDoc(tree->children, doc);
2751 tree->doc = doc;
2752 }
2753}
2754
2755/**
2756 * xmlSetListDoc:
Daniel Veillardd1640922001-12-17 15:30:10 +00002757 * @list: the first element
Owen Taylor3473f882001-02-23 17:55:21 +00002758 * @doc: the document
2759 *
2760 * update all nodes in the list to point to the right document
2761 */
2762void
2763xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) {
2764 xmlNodePtr cur;
2765
2766 if (list == NULL)
2767 return;
2768 cur = list;
2769 while (cur != NULL) {
2770 if (cur->doc != doc)
2771 xmlSetTreeDoc(cur, doc);
2772 cur = cur->next;
2773 }
2774}
2775
Daniel Veillard2156d432004-03-04 15:59:36 +00002776#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00002777/**
2778 * xmlNewChild:
2779 * @parent: the parent node
2780 * @ns: a namespace if any
2781 * @name: the name of the child
2782 * @content: the XML content of the child if any.
2783 *
2784 * Creation of a new child element, added at the end of @parent children list.
MST 2003 John Flecke1f70492003-12-20 23:43:28 +00002785 * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
2786 * created element inherits the namespace of @parent. If @content is non NULL,
Owen Taylor3473f882001-02-23 17:55:21 +00002787 * a child list containing the TEXTs and ENTITY_REFs node will be created.
MST 2003 John Fleck8b03bc52003-12-17 03:45:01 +00002788 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
2789 * references. XML special chars must be escaped first by using
2790 * xmlEncodeEntitiesReentrant(), or xmlNewTextChild() should be used.
Owen Taylor3473f882001-02-23 17:55:21 +00002791 *
2792 * Returns a pointer to the new node object.
2793 */
2794xmlNodePtr
2795xmlNewChild(xmlNodePtr parent, xmlNsPtr ns,
2796 const xmlChar *name, const xmlChar *content) {
2797 xmlNodePtr cur, prev;
2798
2799 if (parent == NULL) {
2800#ifdef DEBUG_TREE
2801 xmlGenericError(xmlGenericErrorContext,
2802 "xmlNewChild : parent == NULL\n");
2803#endif
2804 return(NULL);
2805 }
2806
2807 if (name == NULL) {
2808#ifdef DEBUG_TREE
2809 xmlGenericError(xmlGenericErrorContext,
2810 "xmlNewChild : name == NULL\n");
2811#endif
2812 return(NULL);
2813 }
2814
2815 /*
2816 * Allocate a new node
2817 */
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002818 if (parent->type == XML_ELEMENT_NODE) {
2819 if (ns == NULL)
2820 cur = xmlNewDocNode(parent->doc, parent->ns, name, content);
2821 else
2822 cur = xmlNewDocNode(parent->doc, ns, name, content);
2823 } else if ((parent->type == XML_DOCUMENT_NODE) ||
2824 (parent->type == XML_HTML_DOCUMENT_NODE)) {
2825 if (ns == NULL)
2826 cur = xmlNewDocNode((xmlDocPtr) parent, NULL, name, content);
2827 else
2828 cur = xmlNewDocNode((xmlDocPtr) parent, ns, name, content);
Daniel Veillard7e3f1402002-10-28 18:52:57 +00002829 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2830 cur = xmlNewDocNode( parent->doc, ns, name, content);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002831 } else {
2832 return(NULL);
2833 }
Owen Taylor3473f882001-02-23 17:55:21 +00002834 if (cur == NULL) return(NULL);
2835
2836 /*
2837 * add the new element at the end of the children list.
2838 */
2839 cur->type = XML_ELEMENT_NODE;
2840 cur->parent = parent;
2841 cur->doc = parent->doc;
2842 if (parent->children == NULL) {
2843 parent->children = cur;
2844 parent->last = cur;
2845 } else {
2846 prev = parent->last;
2847 prev->next = cur;
2848 cur->prev = prev;
2849 parent->last = cur;
2850 }
2851
2852 return(cur);
2853}
Daniel Veillard652327a2003-09-29 18:02:38 +00002854#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002855
2856/**
Rob Richards65815122006-02-25 17:13:33 +00002857 * xmlAddPropSibling:
2858 * @prev: the attribute to which @prop is added after
2859 * @cur: the base attribute passed to calling function
2860 * @prop: the new attribute
2861 *
2862 * Add a new attribute after @prev using @cur as base attribute.
2863 * When inserting before @cur, @prev is passed as @cur->prev.
2864 * When inserting after @cur, @prev is passed as @cur.
2865 * If an existing attribute is found it is detroyed prior to adding @prop.
2866 *
2867 * Returns the attribute being inserted or NULL in case of error.
2868 */
2869static xmlNodePtr
2870xmlAddPropSibling(xmlNodePtr prev, xmlNodePtr cur, xmlNodePtr prop) {
2871 xmlAttrPtr attr;
2872
2873 if (cur->type != XML_ATTRIBUTE_NODE)
2874 return(NULL);
2875
2876 /* check if an attribute with the same name exists */
2877 if (prop->ns == NULL)
2878 attr = xmlHasNsProp(cur->parent, prop->name, NULL);
2879 else
2880 attr = xmlHasNsProp(cur->parent, prop->name, prop->ns->href);
2881
2882 if (prop->doc != cur->doc) {
2883 xmlSetTreeDoc(prop, cur->doc);
2884 }
2885 prop->parent = cur->parent;
2886 prop->prev = prev;
2887 if (prev != NULL) {
2888 prop->next = prev->next;
2889 prev->next = prop;
2890 if (prop->next)
2891 prop->next->prev = prop;
2892 } else {
2893 prop->next = cur;
2894 cur->prev = prop;
2895 }
2896 if (prop->prev == NULL && prop->parent != NULL)
2897 prop->parent->properties = (xmlAttrPtr) prop;
2898 if ((attr != NULL) && (attr->type != XML_ATTRIBUTE_DECL)) {
2899 /* different instance, destroy it (attributes must be unique) */
2900 xmlRemoveProp((xmlAttrPtr) attr);
2901 }
2902 return prop;
2903}
2904
2905/**
Owen Taylor3473f882001-02-23 17:55:21 +00002906 * xmlAddNextSibling:
2907 * @cur: the child node
2908 * @elem: the new node
2909 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002910 * Add a new node @elem as the next sibling of @cur
2911 * If the new node was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00002912 * first unlinked from its existing context.
2913 * As a result of text merging @elem may be freed.
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002914 * If the new node is ATTRIBUTE, it is added into properties instead of children.
2915 * If there is an attribute with equal name, it is first destroyed.
Owen Taylor3473f882001-02-23 17:55:21 +00002916 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002917 * Returns the new node or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002918 */
2919xmlNodePtr
2920xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) {
2921 if (cur == NULL) {
2922#ifdef DEBUG_TREE
2923 xmlGenericError(xmlGenericErrorContext,
2924 "xmlAddNextSibling : cur == NULL\n");
2925#endif
2926 return(NULL);
2927 }
2928 if (elem == NULL) {
2929#ifdef DEBUG_TREE
2930 xmlGenericError(xmlGenericErrorContext,
2931 "xmlAddNextSibling : elem == NULL\n");
2932#endif
2933 return(NULL);
2934 }
2935
Rob Richards19dc9612005-10-28 16:15:16 +00002936 if (cur == elem) {
2937#ifdef DEBUG_TREE
2938 xmlGenericError(xmlGenericErrorContext,
2939 "xmlAddNextSibling : cur == elem\n");
2940#endif
2941 return(NULL);
2942 }
2943
Owen Taylor3473f882001-02-23 17:55:21 +00002944 xmlUnlinkNode(elem);
2945
2946 if (elem->type == XML_TEXT_NODE) {
2947 if (cur->type == XML_TEXT_NODE) {
Owen Taylor3473f882001-02-23 17:55:21 +00002948 xmlNodeAddContent(cur, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002949 xmlFreeNode(elem);
2950 return(cur);
2951 }
Daniel Veillard9e1c72d2001-08-31 20:03:19 +00002952 if ((cur->next != NULL) && (cur->next->type == XML_TEXT_NODE) &&
2953 (cur->name == cur->next->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002954 xmlChar *tmp;
2955
2956 tmp = xmlStrdup(elem->content);
2957 tmp = xmlStrcat(tmp, cur->next->content);
2958 xmlNodeSetContent(cur->next, tmp);
2959 xmlFree(tmp);
Owen Taylor3473f882001-02-23 17:55:21 +00002960 xmlFreeNode(elem);
2961 return(cur->next);
2962 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002963 } else if (elem->type == XML_ATTRIBUTE_NODE) {
Rob Richards65815122006-02-25 17:13:33 +00002964 return xmlAddPropSibling(cur, cur, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00002965 }
2966
2967 if (elem->doc != cur->doc) {
2968 xmlSetTreeDoc(elem, cur->doc);
2969 }
2970 elem->parent = cur->parent;
2971 elem->prev = cur;
2972 elem->next = cur->next;
2973 cur->next = elem;
2974 if (elem->next != NULL)
2975 elem->next->prev = elem;
Rob Richards65815122006-02-25 17:13:33 +00002976 if ((elem->parent != NULL) && (elem->parent->last == cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002977 elem->parent->last = elem;
2978 return(elem);
2979}
2980
William M. Brack21e4ef22005-01-02 09:53:13 +00002981#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
2982 defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00002983/**
2984 * xmlAddPrevSibling:
2985 * @cur: the child node
2986 * @elem: the new node
2987 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002988 * Add a new node @elem as the previous sibling of @cur
Owen Taylor3473f882001-02-23 17:55:21 +00002989 * merging adjacent TEXT nodes (@elem may be freed)
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002990 * If the new node was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00002991 * first unlinked from its existing context.
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002992 * If the new node is ATTRIBUTE, it is added into properties instead of children.
2993 * If there is an attribute with equal name, it is first destroyed.
Owen Taylor3473f882001-02-23 17:55:21 +00002994 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002995 * Returns the new node or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002996 */
2997xmlNodePtr
2998xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) {
2999 if (cur == NULL) {
3000#ifdef DEBUG_TREE
3001 xmlGenericError(xmlGenericErrorContext,
3002 "xmlAddPrevSibling : cur == NULL\n");
3003#endif
3004 return(NULL);
3005 }
3006 if (elem == NULL) {
3007#ifdef DEBUG_TREE
3008 xmlGenericError(xmlGenericErrorContext,
3009 "xmlAddPrevSibling : elem == NULL\n");
3010#endif
3011 return(NULL);
3012 }
3013
Rob Richards19dc9612005-10-28 16:15:16 +00003014 if (cur == elem) {
3015#ifdef DEBUG_TREE
3016 xmlGenericError(xmlGenericErrorContext,
3017 "xmlAddPrevSibling : cur == elem\n");
3018#endif
3019 return(NULL);
3020 }
3021
Owen Taylor3473f882001-02-23 17:55:21 +00003022 xmlUnlinkNode(elem);
3023
3024 if (elem->type == XML_TEXT_NODE) {
3025 if (cur->type == XML_TEXT_NODE) {
Owen Taylor3473f882001-02-23 17:55:21 +00003026 xmlChar *tmp;
3027
3028 tmp = xmlStrdup(elem->content);
3029 tmp = xmlStrcat(tmp, cur->content);
3030 xmlNodeSetContent(cur, tmp);
3031 xmlFree(tmp);
Owen Taylor3473f882001-02-23 17:55:21 +00003032 xmlFreeNode(elem);
3033 return(cur);
3034 }
Daniel Veillard9e1c72d2001-08-31 20:03:19 +00003035 if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE) &&
3036 (cur->name == cur->prev->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003037 xmlNodeAddContent(cur->prev, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003038 xmlFreeNode(elem);
3039 return(cur->prev);
3040 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003041 } else if (elem->type == XML_ATTRIBUTE_NODE) {
Rob Richards65815122006-02-25 17:13:33 +00003042 return xmlAddPropSibling(cur->prev, cur, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003043 }
3044
3045 if (elem->doc != cur->doc) {
3046 xmlSetTreeDoc(elem, cur->doc);
3047 }
3048 elem->parent = cur->parent;
3049 elem->next = cur;
3050 elem->prev = cur->prev;
3051 cur->prev = elem;
3052 if (elem->prev != NULL)
3053 elem->prev->next = elem;
Rob Richards65815122006-02-25 17:13:33 +00003054 if ((elem->parent != NULL) && (elem->parent->children == cur)) {
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003055 elem->parent->children = elem;
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003056 }
Owen Taylor3473f882001-02-23 17:55:21 +00003057 return(elem);
3058}
Daniel Veillard652327a2003-09-29 18:02:38 +00003059#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00003060
3061/**
3062 * xmlAddSibling:
3063 * @cur: the child node
3064 * @elem: the new node
3065 *
3066 * Add a new element @elem to the list of siblings of @cur
3067 * merging adjacent TEXT nodes (@elem may be freed)
3068 * If the new element was already inserted in a document it is
3069 * first unlinked from its existing context.
3070 *
3071 * Returns the new element or NULL in case of error.
3072 */
3073xmlNodePtr
3074xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) {
3075 xmlNodePtr parent;
3076
3077 if (cur == NULL) {
3078#ifdef DEBUG_TREE
3079 xmlGenericError(xmlGenericErrorContext,
3080 "xmlAddSibling : cur == NULL\n");
3081#endif
3082 return(NULL);
3083 }
3084
3085 if (elem == NULL) {
3086#ifdef DEBUG_TREE
3087 xmlGenericError(xmlGenericErrorContext,
3088 "xmlAddSibling : elem == NULL\n");
3089#endif
3090 return(NULL);
3091 }
3092
3093 /*
3094 * Constant time is we can rely on the ->parent->last to find
3095 * the last sibling.
3096 */
Rob Richards65815122006-02-25 17:13:33 +00003097 if ((cur->type != XML_ATTRIBUTE_NODE) && (cur->parent != NULL) &&
Owen Taylor3473f882001-02-23 17:55:21 +00003098 (cur->parent->children != NULL) &&
3099 (cur->parent->last != NULL) &&
3100 (cur->parent->last->next == NULL)) {
3101 cur = cur->parent->last;
3102 } else {
3103 while (cur->next != NULL) cur = cur->next;
3104 }
3105
3106 xmlUnlinkNode(elem);
3107
Daniel Veillarde22dd5c2003-10-29 12:53:27 +00003108 if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE) &&
3109 (cur->name == elem->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003110 xmlNodeAddContent(cur, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003111 xmlFreeNode(elem);
3112 return(cur);
Rob Richards65815122006-02-25 17:13:33 +00003113 } else if (elem->type == XML_ATTRIBUTE_NODE) {
3114 return xmlAddPropSibling(cur, cur, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003115 }
3116
3117 if (elem->doc != cur->doc) {
3118 xmlSetTreeDoc(elem, cur->doc);
3119 }
3120 parent = cur->parent;
3121 elem->prev = cur;
3122 elem->next = NULL;
3123 elem->parent = parent;
3124 cur->next = elem;
3125 if (parent != NULL)
3126 parent->last = elem;
3127
3128 return(elem);
3129}
3130
3131/**
3132 * xmlAddChildList:
3133 * @parent: the parent node
3134 * @cur: the first node in the list
3135 *
3136 * Add a list of node at the end of the child list of the parent
3137 * merging adjacent TEXT nodes (@cur may be freed)
3138 *
3139 * Returns the last child or NULL in case of error.
3140 */
3141xmlNodePtr
3142xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) {
3143 xmlNodePtr prev;
3144
3145 if (parent == NULL) {
3146#ifdef DEBUG_TREE
3147 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00003148 "xmlAddChildList : parent == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003149#endif
3150 return(NULL);
3151 }
3152
3153 if (cur == NULL) {
3154#ifdef DEBUG_TREE
3155 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00003156 "xmlAddChildList : child == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003157#endif
3158 return(NULL);
3159 }
3160
3161 if ((cur->doc != NULL) && (parent->doc != NULL) &&
3162 (cur->doc != parent->doc)) {
3163#ifdef DEBUG_TREE
3164 xmlGenericError(xmlGenericErrorContext,
3165 "Elements moved to a different document\n");
3166#endif
3167 }
3168
3169 /*
3170 * add the first element at the end of the children list.
3171 */
Daniel Veillard5335dc52003-01-01 20:59:38 +00003172
Owen Taylor3473f882001-02-23 17:55:21 +00003173 if (parent->children == NULL) {
3174 parent->children = cur;
3175 } else {
3176 /*
3177 * If cur and parent->last both are TEXT nodes, then merge them.
3178 */
3179 if ((cur->type == XML_TEXT_NODE) &&
3180 (parent->last->type == XML_TEXT_NODE) &&
3181 (cur->name == parent->last->name)) {
Daniel Veillard5335dc52003-01-01 20:59:38 +00003182 xmlNodeAddContent(parent->last, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003183 /*
3184 * if it's the only child, nothing more to be done.
3185 */
3186 if (cur->next == NULL) {
3187 xmlFreeNode(cur);
3188 return(parent->last);
3189 }
3190 prev = cur;
3191 cur = cur->next;
3192 xmlFreeNode(prev);
3193 }
3194 prev = parent->last;
3195 prev->next = cur;
3196 cur->prev = prev;
3197 }
3198 while (cur->next != NULL) {
3199 cur->parent = parent;
3200 if (cur->doc != parent->doc) {
3201 xmlSetTreeDoc(cur, parent->doc);
3202 }
3203 cur = cur->next;
3204 }
3205 cur->parent = parent;
3206 cur->doc = parent->doc; /* the parent may not be linked to a doc ! */
3207 parent->last = cur;
3208
3209 return(cur);
3210}
3211
3212/**
3213 * xmlAddChild:
3214 * @parent: the parent node
3215 * @cur: the child node
3216 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003217 * Add a new node to @parent, at the end of the child (or property) list
Owen Taylor3473f882001-02-23 17:55:21 +00003218 * merging adjacent TEXT nodes (in which case @cur is freed)
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003219 * If the new node is ATTRIBUTE, it is added into properties instead of children.
3220 * If there is an attribute with equal name, it is first destroyed.
3221 *
Owen Taylor3473f882001-02-23 17:55:21 +00003222 * Returns the child or NULL in case of error.
3223 */
3224xmlNodePtr
3225xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) {
3226 xmlNodePtr prev;
3227
3228 if (parent == NULL) {
3229#ifdef DEBUG_TREE
3230 xmlGenericError(xmlGenericErrorContext,
3231 "xmlAddChild : parent == NULL\n");
3232#endif
3233 return(NULL);
3234 }
3235
3236 if (cur == NULL) {
3237#ifdef DEBUG_TREE
3238 xmlGenericError(xmlGenericErrorContext,
3239 "xmlAddChild : child == NULL\n");
3240#endif
3241 return(NULL);
3242 }
3243
Rob Richards19dc9612005-10-28 16:15:16 +00003244 if (parent == cur) {
3245#ifdef DEBUG_TREE
3246 xmlGenericError(xmlGenericErrorContext,
3247 "xmlAddChild : parent == cur\n");
3248#endif
3249 return(NULL);
3250 }
Owen Taylor3473f882001-02-23 17:55:21 +00003251 /*
3252 * If cur is a TEXT node, merge its content with adjacent TEXT nodes
Owen Taylor3473f882001-02-23 17:55:21 +00003253 * cur is then freed.
3254 */
3255 if (cur->type == XML_TEXT_NODE) {
Daniel Veillard7db37732001-07-12 01:20:08 +00003256 if ((parent->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003257 (parent->content != NULL) &&
Rob Richards19dc9612005-10-28 16:15:16 +00003258 (parent->name == cur->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003259 xmlNodeAddContent(parent, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003260 xmlFreeNode(cur);
3261 return(parent);
3262 }
3263 if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003264 (parent->last->name == cur->name) &&
3265 (parent->last != cur)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003266 xmlNodeAddContent(parent->last, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003267 xmlFreeNode(cur);
3268 return(parent->last);
3269 }
3270 }
3271
3272 /*
3273 * add the new element at the end of the children list.
3274 */
Daniel Veillard5335dc52003-01-01 20:59:38 +00003275 prev = cur->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00003276 cur->parent = parent;
3277 if (cur->doc != parent->doc) {
3278 xmlSetTreeDoc(cur, parent->doc);
3279 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00003280 /* this check prevents a loop on tree-traversions if a developer
3281 * tries to add a node to its parent multiple times
3282 */
3283 if (prev == parent)
3284 return(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00003285
3286 /*
Daniel Veillard7db37732001-07-12 01:20:08 +00003287 * Coalescing
Owen Taylor3473f882001-02-23 17:55:21 +00003288 */
Daniel Veillard7db37732001-07-12 01:20:08 +00003289 if ((parent->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003290 (parent->content != NULL) &&
3291 (parent != cur)) {
Daniel Veillard7db37732001-07-12 01:20:08 +00003292 xmlNodeAddContent(parent, cur->content);
Daniel Veillard7db37732001-07-12 01:20:08 +00003293 xmlFreeNode(cur);
3294 return(parent);
Owen Taylor3473f882001-02-23 17:55:21 +00003295 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003296 if (cur->type == XML_ATTRIBUTE_NODE) {
Rob Richards19dc9612005-10-28 16:15:16 +00003297 if (parent->type != XML_ELEMENT_NODE)
3298 return(NULL);
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003299 if (parent->properties == NULL) {
3300 parent->properties = (xmlAttrPtr) cur;
3301 } else {
3302 /* check if an attribute with the same name exists */
3303 xmlAttrPtr lastattr;
Owen Taylor3473f882001-02-23 17:55:21 +00003304
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003305 if (cur->ns == NULL)
Rob Richardsc342ec62005-10-25 00:10:12 +00003306 lastattr = xmlHasNsProp(parent, cur->name, NULL);
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003307 else
3308 lastattr = xmlHasNsProp(parent, cur->name, cur->ns->href);
Rob Richardsc342ec62005-10-25 00:10:12 +00003309 if ((lastattr != NULL) && (lastattr != (xmlAttrPtr) cur) && (lastattr->type != XML_ATTRIBUTE_DECL)) {
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003310 /* different instance, destroy it (attributes must be unique) */
Rob Richards19dc9612005-10-28 16:15:16 +00003311 xmlUnlinkNode((xmlNodePtr) lastattr);
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003312 xmlFreeProp(lastattr);
3313 }
Rob Richards19dc9612005-10-28 16:15:16 +00003314 if (lastattr == (xmlAttrPtr) cur)
3315 return(cur);
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003316 /* find the end */
3317 lastattr = parent->properties;
3318 while (lastattr->next != NULL) {
3319 lastattr = lastattr->next;
3320 }
3321 lastattr->next = (xmlAttrPtr) cur;
3322 ((xmlAttrPtr) cur)->prev = lastattr;
3323 }
3324 } else {
3325 if (parent->children == NULL) {
3326 parent->children = cur;
3327 parent->last = cur;
3328 } else {
3329 prev = parent->last;
3330 prev->next = cur;
3331 cur->prev = prev;
3332 parent->last = cur;
3333 }
3334 }
Owen Taylor3473f882001-02-23 17:55:21 +00003335 return(cur);
3336}
3337
3338/**
3339 * xmlGetLastChild:
3340 * @parent: the parent node
3341 *
3342 * Search the last child of a node.
3343 * Returns the last child or NULL if none.
3344 */
3345xmlNodePtr
3346xmlGetLastChild(xmlNodePtr parent) {
3347 if (parent == NULL) {
3348#ifdef DEBUG_TREE
3349 xmlGenericError(xmlGenericErrorContext,
3350 "xmlGetLastChild : parent == NULL\n");
3351#endif
3352 return(NULL);
3353 }
3354 return(parent->last);
3355}
3356
3357/**
3358 * xmlFreeNodeList:
3359 * @cur: the first node in the list
3360 *
3361 * Free a node and all its siblings, this is a recursive behaviour, all
3362 * the children are freed too.
3363 */
3364void
3365xmlFreeNodeList(xmlNodePtr cur) {
3366 xmlNodePtr next;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003367 xmlDictPtr dict = NULL;
3368
3369 if (cur == NULL) return;
Daniel Veillarde6a55192002-01-14 17:11:53 +00003370 if (cur->type == XML_NAMESPACE_DECL) {
3371 xmlFreeNsList((xmlNsPtr) cur);
3372 return;
3373 }
Daniel Veillard9adc0462003-03-24 18:39:54 +00003374 if ((cur->type == XML_DOCUMENT_NODE) ||
3375#ifdef LIBXML_DOCB_ENABLED
3376 (cur->type == XML_DOCB_DOCUMENT_NODE) ||
Daniel Veillard9adc0462003-03-24 18:39:54 +00003377#endif
Daniel Veillard6560a422003-03-27 21:25:38 +00003378 (cur->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard9adc0462003-03-24 18:39:54 +00003379 xmlFreeDoc((xmlDocPtr) cur);
3380 return;
3381 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003382 if (cur->doc != NULL) dict = cur->doc->dict;
Owen Taylor3473f882001-02-23 17:55:21 +00003383 while (cur != NULL) {
3384 next = cur->next;
Daniel Veillard02141ea2001-04-30 11:46:40 +00003385 if (cur->type != XML_DTD_NODE) {
Daniel Veillard5335dc52003-01-01 20:59:38 +00003386
Daniel Veillarda880b122003-04-21 21:36:41 +00003387 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00003388 xmlDeregisterNodeDefaultValue(cur);
3389
Daniel Veillard02141ea2001-04-30 11:46:40 +00003390 if ((cur->children != NULL) &&
3391 (cur->type != XML_ENTITY_REF_NODE))
3392 xmlFreeNodeList(cur->children);
Daniel Veillard01c13b52002-12-10 15:19:08 +00003393 if (((cur->type == XML_ELEMENT_NODE) ||
3394 (cur->type == XML_XINCLUDE_START) ||
3395 (cur->type == XML_XINCLUDE_END)) &&
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003396 (cur->properties != NULL))
Daniel Veillard02141ea2001-04-30 11:46:40 +00003397 xmlFreePropList(cur->properties);
Daniel Veillard7db37732001-07-12 01:20:08 +00003398 if ((cur->type != XML_ELEMENT_NODE) &&
3399 (cur->type != XML_XINCLUDE_START) &&
3400 (cur->type != XML_XINCLUDE_END) &&
Daniel Veillard8874b942005-08-25 13:19:21 +00003401 (cur->type != XML_ENTITY_REF_NODE) &&
3402 (cur->content != (xmlChar *) &(cur->properties))) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003403 DICT_FREE(cur->content)
Daniel Veillard7db37732001-07-12 01:20:08 +00003404 }
3405 if (((cur->type == XML_ELEMENT_NODE) ||
3406 (cur->type == XML_XINCLUDE_START) ||
3407 (cur->type == XML_XINCLUDE_END)) &&
3408 (cur->nsDef != NULL))
3409 xmlFreeNsList(cur->nsDef);
3410
Daniel Veillard9cc6dc62001-06-11 08:09:20 +00003411 /*
3412 * When a node is a text node or a comment, it uses a global static
3413 * variable for the name of the node.
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003414 * Otherwise the node name might come from the document's
3415 * dictionnary
Daniel Veillard9cc6dc62001-06-11 08:09:20 +00003416 */
Daniel Veillard02141ea2001-04-30 11:46:40 +00003417 if ((cur->name != NULL) &&
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003418 (cur->type != XML_TEXT_NODE) &&
3419 (cur->type != XML_COMMENT_NODE))
3420 DICT_FREE(cur->name)
Daniel Veillard02141ea2001-04-30 11:46:40 +00003421 xmlFree(cur);
3422 }
Owen Taylor3473f882001-02-23 17:55:21 +00003423 cur = next;
3424 }
3425}
3426
3427/**
3428 * xmlFreeNode:
3429 * @cur: the node
3430 *
3431 * Free a node, this is a recursive behaviour, all the children are freed too.
3432 * This doesn't unlink the child from the list, use xmlUnlinkNode() first.
3433 */
3434void
3435xmlFreeNode(xmlNodePtr cur) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003436 xmlDictPtr dict = NULL;
3437
3438 if (cur == NULL) return;
Daniel Veillard5335dc52003-01-01 20:59:38 +00003439
Daniel Veillard02141ea2001-04-30 11:46:40 +00003440 /* use xmlFreeDtd for DTD nodes */
Daniel Veillarde6a55192002-01-14 17:11:53 +00003441 if (cur->type == XML_DTD_NODE) {
3442 xmlFreeDtd((xmlDtdPtr) cur);
Owen Taylor3473f882001-02-23 17:55:21 +00003443 return;
Daniel Veillarde6a55192002-01-14 17:11:53 +00003444 }
3445 if (cur->type == XML_NAMESPACE_DECL) {
3446 xmlFreeNs((xmlNsPtr) cur);
3447 return;
3448 }
Daniel Veillarda70d62f2002-11-07 14:18:03 +00003449 if (cur->type == XML_ATTRIBUTE_NODE) {
3450 xmlFreeProp((xmlAttrPtr) cur);
3451 return;
3452 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00003453
Daniel Veillarda880b122003-04-21 21:36:41 +00003454 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00003455 xmlDeregisterNodeDefaultValue(cur);
3456
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003457 if (cur->doc != NULL) dict = cur->doc->dict;
3458
Owen Taylor3473f882001-02-23 17:55:21 +00003459 if ((cur->children != NULL) &&
3460 (cur->type != XML_ENTITY_REF_NODE))
3461 xmlFreeNodeList(cur->children);
Daniel Veillard01c13b52002-12-10 15:19:08 +00003462 if (((cur->type == XML_ELEMENT_NODE) ||
3463 (cur->type == XML_XINCLUDE_START) ||
3464 (cur->type == XML_XINCLUDE_END)) &&
3465 (cur->properties != NULL))
Daniel Veillard02141ea2001-04-30 11:46:40 +00003466 xmlFreePropList(cur->properties);
Daniel Veillard7db37732001-07-12 01:20:08 +00003467 if ((cur->type != XML_ELEMENT_NODE) &&
3468 (cur->content != NULL) &&
3469 (cur->type != XML_ENTITY_REF_NODE) &&
3470 (cur->type != XML_XINCLUDE_END) &&
Daniel Veillard8874b942005-08-25 13:19:21 +00003471 (cur->type != XML_XINCLUDE_START) &&
3472 (cur->content != (xmlChar *) &(cur->properties))) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003473 DICT_FREE(cur->content)
Daniel Veillard7db37732001-07-12 01:20:08 +00003474 }
3475
Daniel Veillardacd370f2001-06-09 17:17:51 +00003476 /*
3477 * When a node is a text node or a comment, it uses a global static
3478 * variable for the name of the node.
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003479 * Otherwise the node name might come from the document's dictionnary
Daniel Veillardacd370f2001-06-09 17:17:51 +00003480 */
Owen Taylor3473f882001-02-23 17:55:21 +00003481 if ((cur->name != NULL) &&
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003482 (cur->type != XML_TEXT_NODE) &&
3483 (cur->type != XML_COMMENT_NODE))
3484 DICT_FREE(cur->name)
Daniel Veillardacd370f2001-06-09 17:17:51 +00003485
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003486 if (((cur->type == XML_ELEMENT_NODE) ||
3487 (cur->type == XML_XINCLUDE_START) ||
3488 (cur->type == XML_XINCLUDE_END)) &&
3489 (cur->nsDef != NULL))
3490 xmlFreeNsList(cur->nsDef);
Owen Taylor3473f882001-02-23 17:55:21 +00003491 xmlFree(cur);
3492}
3493
3494/**
3495 * xmlUnlinkNode:
3496 * @cur: the node
3497 *
3498 * Unlink a node from it's current context, the node is not freed
3499 */
3500void
3501xmlUnlinkNode(xmlNodePtr cur) {
3502 if (cur == NULL) {
3503#ifdef DEBUG_TREE
3504 xmlGenericError(xmlGenericErrorContext,
3505 "xmlUnlinkNode : node == NULL\n");
3506#endif
3507 return;
3508 }
Daniel Veillard29e43992001-12-13 22:21:58 +00003509 if (cur->type == XML_DTD_NODE) {
3510 xmlDocPtr doc;
3511 doc = cur->doc;
Daniel Veillarda067e652003-05-01 08:03:46 +00003512 if (doc != NULL) {
3513 if (doc->intSubset == (xmlDtdPtr) cur)
3514 doc->intSubset = NULL;
3515 if (doc->extSubset == (xmlDtdPtr) cur)
3516 doc->extSubset = NULL;
3517 }
Daniel Veillard29e43992001-12-13 22:21:58 +00003518 }
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003519 if (cur->parent != NULL) {
3520 xmlNodePtr parent;
3521 parent = cur->parent;
3522 if (cur->type == XML_ATTRIBUTE_NODE) {
3523 if (parent->properties == (xmlAttrPtr) cur)
3524 parent->properties = ((xmlAttrPtr) cur)->next;
3525 } else {
3526 if (parent->children == cur)
3527 parent->children = cur->next;
3528 if (parent->last == cur)
3529 parent->last = cur->prev;
3530 }
3531 cur->parent = NULL;
3532 }
Owen Taylor3473f882001-02-23 17:55:21 +00003533 if (cur->next != NULL)
3534 cur->next->prev = cur->prev;
3535 if (cur->prev != NULL)
3536 cur->prev->next = cur->next;
3537 cur->next = cur->prev = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00003538}
3539
Daniel Veillard2156d432004-03-04 15:59:36 +00003540#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00003541/**
3542 * xmlReplaceNode:
3543 * @old: the old node
3544 * @cur: the node
3545 *
William M. Brackd7cf7f82003-11-14 07:13:16 +00003546 * Unlink the old node from its current context, prune the new one
Daniel Veillardd1640922001-12-17 15:30:10 +00003547 * at the same place. If @cur was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00003548 * first unlinked from its existing context.
3549 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003550 * Returns the @old node
Owen Taylor3473f882001-02-23 17:55:21 +00003551 */
3552xmlNodePtr
3553xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) {
Daniel Veillardce244ad2004-11-05 10:03:46 +00003554 if (old == cur) return(NULL);
Daniel Veillarda03e3652004-11-02 18:45:30 +00003555 if ((old == NULL) || (old->parent == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003556#ifdef DEBUG_TREE
3557 xmlGenericError(xmlGenericErrorContext,
Daniel Veillarda03e3652004-11-02 18:45:30 +00003558 "xmlReplaceNode : old == NULL or without parent\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003559#endif
3560 return(NULL);
3561 }
3562 if (cur == NULL) {
3563 xmlUnlinkNode(old);
3564 return(old);
3565 }
3566 if (cur == old) {
3567 return(old);
3568 }
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003569 if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) {
3570#ifdef DEBUG_TREE
3571 xmlGenericError(xmlGenericErrorContext,
3572 "xmlReplaceNode : Trying to replace attribute node with other node type\n");
3573#endif
3574 return(old);
3575 }
3576 if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) {
3577#ifdef DEBUG_TREE
3578 xmlGenericError(xmlGenericErrorContext,
3579 "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n");
3580#endif
3581 return(old);
3582 }
Owen Taylor3473f882001-02-23 17:55:21 +00003583 xmlUnlinkNode(cur);
Daniel Veillard64d7d122005-05-11 18:03:42 +00003584 xmlSetTreeDoc(cur, old->doc);
Owen Taylor3473f882001-02-23 17:55:21 +00003585 cur->parent = old->parent;
3586 cur->next = old->next;
3587 if (cur->next != NULL)
3588 cur->next->prev = cur;
3589 cur->prev = old->prev;
3590 if (cur->prev != NULL)
3591 cur->prev->next = cur;
3592 if (cur->parent != NULL) {
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003593 if (cur->type == XML_ATTRIBUTE_NODE) {
3594 if (cur->parent->properties == (xmlAttrPtr)old)
3595 cur->parent->properties = ((xmlAttrPtr) cur);
3596 } else {
3597 if (cur->parent->children == old)
3598 cur->parent->children = cur;
3599 if (cur->parent->last == old)
3600 cur->parent->last = cur;
3601 }
Owen Taylor3473f882001-02-23 17:55:21 +00003602 }
3603 old->next = old->prev = NULL;
3604 old->parent = NULL;
3605 return(old);
3606}
Daniel Veillard652327a2003-09-29 18:02:38 +00003607#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00003608
3609/************************************************************************
3610 * *
3611 * Copy operations *
3612 * *
3613 ************************************************************************/
3614
3615/**
3616 * xmlCopyNamespace:
3617 * @cur: the namespace
3618 *
3619 * Do a copy of the namespace.
3620 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003621 * Returns: a new #xmlNsPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003622 */
3623xmlNsPtr
3624xmlCopyNamespace(xmlNsPtr cur) {
3625 xmlNsPtr ret;
3626
3627 if (cur == NULL) return(NULL);
3628 switch (cur->type) {
3629 case XML_LOCAL_NAMESPACE:
3630 ret = xmlNewNs(NULL, cur->href, cur->prefix);
3631 break;
3632 default:
3633#ifdef DEBUG_TREE
3634 xmlGenericError(xmlGenericErrorContext,
3635 "xmlCopyNamespace: invalid type %d\n", cur->type);
3636#endif
3637 return(NULL);
3638 }
3639 return(ret);
3640}
3641
3642/**
3643 * xmlCopyNamespaceList:
3644 * @cur: the first namespace
3645 *
3646 * Do a copy of an namespace list.
3647 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003648 * Returns: a new #xmlNsPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003649 */
3650xmlNsPtr
3651xmlCopyNamespaceList(xmlNsPtr cur) {
3652 xmlNsPtr ret = NULL;
3653 xmlNsPtr p = NULL,q;
3654
3655 while (cur != NULL) {
3656 q = xmlCopyNamespace(cur);
3657 if (p == NULL) {
3658 ret = p = q;
3659 } else {
3660 p->next = q;
3661 p = q;
3662 }
3663 cur = cur->next;
3664 }
3665 return(ret);
3666}
3667
3668static xmlNodePtr
3669xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent);
Rob Richards19dc9612005-10-28 16:15:16 +00003670
3671static xmlAttrPtr
3672xmlCopyPropInternal(xmlDocPtr doc, xmlNodePtr target, xmlAttrPtr cur) {
Owen Taylor3473f882001-02-23 17:55:21 +00003673 xmlAttrPtr ret;
3674
3675 if (cur == NULL) return(NULL);
3676 if (target != NULL)
3677 ret = xmlNewDocProp(target->doc, cur->name, NULL);
Rob Richards19dc9612005-10-28 16:15:16 +00003678 else if (doc != NULL)
3679 ret = xmlNewDocProp(doc, cur->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003680 else if (cur->parent != NULL)
3681 ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL);
3682 else if (cur->children != NULL)
3683 ret = xmlNewDocProp(cur->children->doc, cur->name, NULL);
3684 else
3685 ret = xmlNewDocProp(NULL, cur->name, NULL);
3686 if (ret == NULL) return(NULL);
3687 ret->parent = target;
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00003688
Owen Taylor3473f882001-02-23 17:55:21 +00003689 if ((cur->ns != NULL) && (target != NULL)) {
Daniel Veillard8107a222002-01-13 14:10:10 +00003690 xmlNsPtr ns;
Daniel Veillard652327a2003-09-29 18:02:38 +00003691
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00003692 ns = xmlSearchNs(target->doc, target, cur->ns->prefix);
3693 if (ns == NULL) {
3694 /*
3695 * Humm, we are copying an element whose namespace is defined
3696 * out of the new tree scope. Search it in the original tree
3697 * and add it at the top of the new tree
3698 */
3699 ns = xmlSearchNs(cur->doc, cur->parent, cur->ns->prefix);
3700 if (ns != NULL) {
3701 xmlNodePtr root = target;
3702 xmlNodePtr pred = NULL;
3703
3704 while (root->parent != NULL) {
3705 pred = root;
3706 root = root->parent;
3707 }
3708 if (root == (xmlNodePtr) target->doc) {
3709 /* correct possibly cycling above the document elt */
3710 root = pred;
3711 }
3712 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
3713 }
3714 } else {
3715 /*
3716 * we have to find something appropriate here since
3717 * we cant be sure, that the namespce we found is identified
3718 * by the prefix
3719 */
Daniel Veillard044fc6b2002-03-04 17:09:44 +00003720 if (xmlStrEqual(ns->href, cur->ns->href)) {
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00003721 /* this is the nice case */
3722 ret->ns = ns;
3723 } else {
3724 /*
3725 * we are in trouble: we need a new reconcilied namespace.
3726 * This is expensive
3727 */
3728 ret->ns = xmlNewReconciliedNs(target->doc, target, cur->ns);
3729 }
3730 }
3731
Owen Taylor3473f882001-02-23 17:55:21 +00003732 } else
3733 ret->ns = NULL;
3734
3735 if (cur->children != NULL) {
3736 xmlNodePtr tmp;
3737
3738 ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret);
3739 ret->last = NULL;
3740 tmp = ret->children;
3741 while (tmp != NULL) {
3742 /* tmp->parent = (xmlNodePtr)ret; */
3743 if (tmp->next == NULL)
3744 ret->last = tmp;
3745 tmp = tmp->next;
3746 }
3747 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00003748 /*
3749 * Try to handle IDs
3750 */
Daniel Veillarda3db2e32002-03-08 15:46:57 +00003751 if ((target!= NULL) && (cur!= NULL) &&
3752 (target->doc != NULL) && (cur->doc != NULL) &&
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00003753 (cur->doc->ids != NULL) && (cur->parent != NULL)) {
3754 if (xmlIsID(cur->doc, cur->parent, cur)) {
3755 xmlChar *id;
3756
3757 id = xmlNodeListGetString(cur->doc, cur->children, 1);
3758 if (id != NULL) {
3759 xmlAddID(NULL, target->doc, id, ret);
3760 xmlFree(id);
3761 }
3762 }
3763 }
Owen Taylor3473f882001-02-23 17:55:21 +00003764 return(ret);
3765}
3766
3767/**
Rob Richards19dc9612005-10-28 16:15:16 +00003768 * xmlCopyProp:
3769 * @target: the element where the attribute will be grafted
3770 * @cur: the attribute
3771 *
3772 * Do a copy of the attribute.
3773 *
3774 * Returns: a new #xmlAttrPtr, or NULL in case of error.
3775 */
3776xmlAttrPtr
3777xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) {
3778 return xmlCopyPropInternal(NULL, target, cur);
3779}
3780
3781/**
Owen Taylor3473f882001-02-23 17:55:21 +00003782 * xmlCopyPropList:
3783 * @target: the element where the attributes will be grafted
3784 * @cur: the first attribute
3785 *
3786 * Do a copy of an attribute list.
3787 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003788 * Returns: a new #xmlAttrPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003789 */
3790xmlAttrPtr
3791xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) {
3792 xmlAttrPtr ret = NULL;
3793 xmlAttrPtr p = NULL,q;
3794
3795 while (cur != NULL) {
3796 q = xmlCopyProp(target, cur);
William M. Brack13dfa872004-09-18 04:52:08 +00003797 if (q == NULL)
3798 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003799 if (p == NULL) {
3800 ret = p = q;
3801 } else {
3802 p->next = q;
3803 q->prev = p;
3804 p = q;
3805 }
3806 cur = cur->next;
3807 }
3808 return(ret);
3809}
3810
3811/*
Daniel Veillardd1640922001-12-17 15:30:10 +00003812 * NOTE about the CopyNode operations !
Owen Taylor3473f882001-02-23 17:55:21 +00003813 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003814 * They are split into external and internal parts for one
Owen Taylor3473f882001-02-23 17:55:21 +00003815 * tricky reason: namespaces. Doing a direct copy of a node
3816 * say RPM:Copyright without changing the namespace pointer to
3817 * something else can produce stale links. One way to do it is
3818 * to keep a reference counter but this doesn't work as soon
3819 * as one move the element or the subtree out of the scope of
3820 * the existing namespace. The actual solution seems to add
3821 * a copy of the namespace at the top of the copied tree if
3822 * not available in the subtree.
3823 * Hence two functions, the public front-end call the inner ones
William M. Brack57e9e912004-03-09 16:19:02 +00003824 * The argument "recursive" normally indicates a recursive copy
3825 * of the node with values 0 (no) and 1 (yes). For XInclude,
3826 * however, we allow a value of 2 to indicate copy properties and
3827 * namespace info, but don't recurse on children.
Owen Taylor3473f882001-02-23 17:55:21 +00003828 */
3829
3830static xmlNodePtr
Daniel Veillard3ec4c612001-08-28 20:39:49 +00003831xmlStaticCopyNode(const xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent,
William M. Brack57e9e912004-03-09 16:19:02 +00003832 int extended) {
Owen Taylor3473f882001-02-23 17:55:21 +00003833 xmlNodePtr ret;
3834
3835 if (node == NULL) return(NULL);
Daniel Veillard39196eb2001-06-19 18:09:42 +00003836 switch (node->type) {
3837 case XML_TEXT_NODE:
3838 case XML_CDATA_SECTION_NODE:
3839 case XML_ELEMENT_NODE:
Daniel Veillardec6725e2002-09-05 11:12:45 +00003840 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00003841 case XML_ENTITY_REF_NODE:
3842 case XML_ENTITY_NODE:
3843 case XML_PI_NODE:
3844 case XML_COMMENT_NODE:
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00003845 case XML_XINCLUDE_START:
3846 case XML_XINCLUDE_END:
3847 break;
3848 case XML_ATTRIBUTE_NODE:
Rob Richards19dc9612005-10-28 16:15:16 +00003849 return((xmlNodePtr) xmlCopyPropInternal(doc, parent, (xmlAttrPtr) node));
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00003850 case XML_NAMESPACE_DECL:
3851 return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node));
3852
Daniel Veillard39196eb2001-06-19 18:09:42 +00003853 case XML_DOCUMENT_NODE:
3854 case XML_HTML_DOCUMENT_NODE:
3855#ifdef LIBXML_DOCB_ENABLED
3856 case XML_DOCB_DOCUMENT_NODE:
3857#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00003858#ifdef LIBXML_TREE_ENABLED
William M. Brack57e9e912004-03-09 16:19:02 +00003859 return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, extended));
Daniel Veillard652327a2003-09-29 18:02:38 +00003860#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard39196eb2001-06-19 18:09:42 +00003861 case XML_DOCUMENT_TYPE_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00003862 case XML_NOTATION_NODE:
3863 case XML_DTD_NODE:
3864 case XML_ELEMENT_DECL:
3865 case XML_ATTRIBUTE_DECL:
3866 case XML_ENTITY_DECL:
3867 return(NULL);
3868 }
Daniel Veillardb33c2012001-04-25 12:59:04 +00003869
Owen Taylor3473f882001-02-23 17:55:21 +00003870 /*
3871 * Allocate a new node and fill the fields.
3872 */
3873 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
3874 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00003875 xmlTreeErrMemory("copying node");
Owen Taylor3473f882001-02-23 17:55:21 +00003876 return(NULL);
3877 }
3878 memset(ret, 0, sizeof(xmlNode));
3879 ret->type = node->type;
3880
3881 ret->doc = doc;
3882 ret->parent = parent;
3883 if (node->name == xmlStringText)
3884 ret->name = xmlStringText;
3885 else if (node->name == xmlStringTextNoenc)
3886 ret->name = xmlStringTextNoenc;
3887 else if (node->name == xmlStringComment)
3888 ret->name = xmlStringComment;
Daniel Veillard03a53c32004-10-26 16:06:51 +00003889 else if (node->name != NULL) {
3890 if ((doc != NULL) && (doc->dict != NULL))
3891 ret->name = xmlDictLookup(doc->dict, node->name, -1);
3892 else
3893 ret->name = xmlStrdup(node->name);
3894 }
Daniel Veillard7db37732001-07-12 01:20:08 +00003895 if ((node->type != XML_ELEMENT_NODE) &&
3896 (node->content != NULL) &&
3897 (node->type != XML_ENTITY_REF_NODE) &&
3898 (node->type != XML_XINCLUDE_END) &&
3899 (node->type != XML_XINCLUDE_START)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003900 ret->content = xmlStrdup(node->content);
Daniel Veillard8107a222002-01-13 14:10:10 +00003901 }else{
3902 if (node->type == XML_ELEMENT_NODE)
Daniel Veillard3e35f8e2003-10-21 00:05:38 +00003903 ret->line = node->line;
Owen Taylor3473f882001-02-23 17:55:21 +00003904 }
Daniel Veillardacb2bda2002-01-13 16:15:43 +00003905 if (parent != NULL) {
3906 xmlNodePtr tmp;
3907
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003908 /*
3909 * this is a tricky part for the node register thing:
3910 * in case ret does get coalesced in xmlAddChild
3911 * the deregister-node callback is called; so we register ret now already
3912 */
Daniel Veillarda880b122003-04-21 21:36:41 +00003913 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003914 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
3915
Daniel Veillardacb2bda2002-01-13 16:15:43 +00003916 tmp = xmlAddChild(parent, ret);
3917 /* node could have coalesced */
3918 if (tmp != ret)
3919 return(tmp);
3920 }
Owen Taylor3473f882001-02-23 17:55:21 +00003921
William M. Brack57e9e912004-03-09 16:19:02 +00003922 if (!extended)
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003923 goto out;
Daniel Veillard8874b942005-08-25 13:19:21 +00003924 if ((node->type == XML_ELEMENT_NODE) && (node->nsDef != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00003925 ret->nsDef = xmlCopyNamespaceList(node->nsDef);
3926
3927 if (node->ns != NULL) {
3928 xmlNsPtr ns;
3929
3930 ns = xmlSearchNs(doc, ret, node->ns->prefix);
3931 if (ns == NULL) {
3932 /*
3933 * Humm, we are copying an element whose namespace is defined
3934 * out of the new tree scope. Search it in the original tree
3935 * and add it at the top of the new tree
3936 */
3937 ns = xmlSearchNs(node->doc, node, node->ns->prefix);
3938 if (ns != NULL) {
3939 xmlNodePtr root = ret;
3940
3941 while (root->parent != NULL) root = root->parent;
Daniel Veillarde82a9922001-04-22 12:12:58 +00003942 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00003943 }
3944 } else {
3945 /*
3946 * reference the existing namespace definition in our own tree.
3947 */
3948 ret->ns = ns;
3949 }
3950 }
Daniel Veillard8874b942005-08-25 13:19:21 +00003951 if ((node->type == XML_ELEMENT_NODE) && (node->properties != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00003952 ret->properties = xmlCopyPropList(ret, node->properties);
Daniel Veillardb33c2012001-04-25 12:59:04 +00003953 if (node->type == XML_ENTITY_REF_NODE) {
3954 if ((doc == NULL) || (node->doc != doc)) {
3955 /*
3956 * The copied node will go into a separate document, so
Daniel Veillardd1640922001-12-17 15:30:10 +00003957 * to avoid dangling references to the ENTITY_DECL node
Daniel Veillardb33c2012001-04-25 12:59:04 +00003958 * we cannot keep the reference. Try to find it in the
3959 * target document.
3960 */
3961 ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name);
3962 } else {
3963 ret->children = node->children;
3964 }
Daniel Veillard0ec98632001-11-14 15:04:32 +00003965 ret->last = ret->children;
William M. Brack57e9e912004-03-09 16:19:02 +00003966 } else if ((node->children != NULL) && (extended != 2)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003967 ret->children = xmlStaticCopyNodeList(node->children, doc, ret);
Daniel Veillard0ec98632001-11-14 15:04:32 +00003968 UPDATE_LAST_CHILD_AND_PARENT(ret)
3969 }
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003970
3971out:
3972 /* if parent != NULL we already registered the node above */
Daniel Veillardac996a12004-07-30 12:02:58 +00003973 if ((parent == NULL) &&
3974 ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003975 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
Owen Taylor3473f882001-02-23 17:55:21 +00003976 return(ret);
3977}
3978
3979static xmlNodePtr
3980xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) {
3981 xmlNodePtr ret = NULL;
3982 xmlNodePtr p = NULL,q;
3983
3984 while (node != NULL) {
Daniel Veillard652327a2003-09-29 18:02:38 +00003985#ifdef LIBXML_TREE_ENABLED
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00003986 if (node->type == XML_DTD_NODE ) {
Daniel Veillard4497e692001-06-09 14:19:02 +00003987 if (doc == NULL) {
3988 node = node->next;
3989 continue;
3990 }
Daniel Veillardb33c2012001-04-25 12:59:04 +00003991 if (doc->intSubset == NULL) {
3992 q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node );
3993 q->doc = doc;
3994 q->parent = parent;
3995 doc->intSubset = (xmlDtdPtr) q;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00003996 xmlAddChild(parent, q);
Daniel Veillardb33c2012001-04-25 12:59:04 +00003997 } else {
3998 q = (xmlNodePtr) doc->intSubset;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00003999 xmlAddChild(parent, q);
Daniel Veillardb33c2012001-04-25 12:59:04 +00004000 }
4001 } else
Daniel Veillard652327a2003-09-29 18:02:38 +00004002#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardb33c2012001-04-25 12:59:04 +00004003 q = xmlStaticCopyNode(node, doc, parent, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00004004 if (ret == NULL) {
4005 q->prev = NULL;
4006 ret = p = q;
Daniel Veillardacb2bda2002-01-13 16:15:43 +00004007 } else if (p != q) {
4008 /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */
Owen Taylor3473f882001-02-23 17:55:21 +00004009 p->next = q;
4010 q->prev = p;
4011 p = q;
4012 }
4013 node = node->next;
4014 }
4015 return(ret);
4016}
4017
4018/**
4019 * xmlCopyNode:
4020 * @node: the node
William M. Brack57e9e912004-03-09 16:19:02 +00004021 * @extended: if 1 do a recursive copy (properties, namespaces and children
4022 * when applicable)
4023 * if 2 copy properties and namespaces (when applicable)
Owen Taylor3473f882001-02-23 17:55:21 +00004024 *
4025 * Do a copy of the node.
4026 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004027 * Returns: a new #xmlNodePtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004028 */
4029xmlNodePtr
William M. Brack57e9e912004-03-09 16:19:02 +00004030xmlCopyNode(const xmlNodePtr node, int extended) {
Owen Taylor3473f882001-02-23 17:55:21 +00004031 xmlNodePtr ret;
4032
William M. Brack57e9e912004-03-09 16:19:02 +00004033 ret = xmlStaticCopyNode(node, NULL, NULL, extended);
Owen Taylor3473f882001-02-23 17:55:21 +00004034 return(ret);
4035}
4036
4037/**
Daniel Veillard82daa812001-04-12 08:55:36 +00004038 * xmlDocCopyNode:
4039 * @node: the node
Daniel Veillardd1640922001-12-17 15:30:10 +00004040 * @doc: the document
William M. Brack57e9e912004-03-09 16:19:02 +00004041 * @extended: if 1 do a recursive copy (properties, namespaces and children
4042 * when applicable)
4043 * if 2 copy properties and namespaces (when applicable)
Daniel Veillard82daa812001-04-12 08:55:36 +00004044 *
4045 * Do a copy of the node to a given document.
4046 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004047 * Returns: a new #xmlNodePtr, or NULL in case of error.
Daniel Veillard82daa812001-04-12 08:55:36 +00004048 */
4049xmlNodePtr
William M. Brack57e9e912004-03-09 16:19:02 +00004050xmlDocCopyNode(const xmlNodePtr node, xmlDocPtr doc, int extended) {
Daniel Veillard82daa812001-04-12 08:55:36 +00004051 xmlNodePtr ret;
4052
William M. Brack57e9e912004-03-09 16:19:02 +00004053 ret = xmlStaticCopyNode(node, doc, NULL, extended);
Daniel Veillard82daa812001-04-12 08:55:36 +00004054 return(ret);
4055}
4056
4057/**
Daniel Veillard03a53c32004-10-26 16:06:51 +00004058 * xmlDocCopyNodeList:
4059 * @doc: the target document
4060 * @node: the first node in the list.
4061 *
4062 * Do a recursive copy of the node list.
4063 *
4064 * Returns: a new #xmlNodePtr, or NULL in case of error.
4065 */
4066xmlNodePtr xmlDocCopyNodeList(xmlDocPtr doc, const xmlNodePtr node) {
4067 xmlNodePtr ret = xmlStaticCopyNodeList(node, doc, NULL);
4068 return(ret);
4069}
4070
4071/**
Owen Taylor3473f882001-02-23 17:55:21 +00004072 * xmlCopyNodeList:
4073 * @node: the first node in the list.
4074 *
4075 * Do a recursive copy of the node list.
Daniel Veillard03a53c32004-10-26 16:06:51 +00004076 * Use xmlDocCopyNodeList() if possible to ensure string interning.
Owen Taylor3473f882001-02-23 17:55:21 +00004077 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004078 * Returns: a new #xmlNodePtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004079 */
Daniel Veillard3ec4c612001-08-28 20:39:49 +00004080xmlNodePtr xmlCopyNodeList(const xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00004081 xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL);
4082 return(ret);
4083}
4084
Daniel Veillard2156d432004-03-04 15:59:36 +00004085#if defined(LIBXML_TREE_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004086/**
Owen Taylor3473f882001-02-23 17:55:21 +00004087 * xmlCopyDtd:
4088 * @dtd: the dtd
4089 *
4090 * Do a copy of the dtd.
4091 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004092 * Returns: a new #xmlDtdPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004093 */
4094xmlDtdPtr
4095xmlCopyDtd(xmlDtdPtr dtd) {
4096 xmlDtdPtr ret;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004097 xmlNodePtr cur, p = NULL, q;
Owen Taylor3473f882001-02-23 17:55:21 +00004098
4099 if (dtd == NULL) return(NULL);
4100 ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID);
4101 if (ret == NULL) return(NULL);
4102 if (dtd->entities != NULL)
4103 ret->entities = (void *) xmlCopyEntitiesTable(
4104 (xmlEntitiesTablePtr) dtd->entities);
4105 if (dtd->notations != NULL)
4106 ret->notations = (void *) xmlCopyNotationTable(
4107 (xmlNotationTablePtr) dtd->notations);
4108 if (dtd->elements != NULL)
4109 ret->elements = (void *) xmlCopyElementTable(
4110 (xmlElementTablePtr) dtd->elements);
4111 if (dtd->attributes != NULL)
4112 ret->attributes = (void *) xmlCopyAttributeTable(
4113 (xmlAttributeTablePtr) dtd->attributes);
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004114 if (dtd->pentities != NULL)
4115 ret->pentities = (void *) xmlCopyEntitiesTable(
4116 (xmlEntitiesTablePtr) dtd->pentities);
4117
4118 cur = dtd->children;
4119 while (cur != NULL) {
4120 q = NULL;
4121
4122 if (cur->type == XML_ENTITY_DECL) {
4123 xmlEntityPtr tmp = (xmlEntityPtr) cur;
4124 switch (tmp->etype) {
4125 case XML_INTERNAL_GENERAL_ENTITY:
4126 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
4127 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
4128 q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name);
4129 break;
4130 case XML_INTERNAL_PARAMETER_ENTITY:
4131 case XML_EXTERNAL_PARAMETER_ENTITY:
4132 q = (xmlNodePtr)
4133 xmlGetParameterEntityFromDtd(ret, tmp->name);
4134 break;
4135 case XML_INTERNAL_PREDEFINED_ENTITY:
4136 break;
4137 }
4138 } else if (cur->type == XML_ELEMENT_DECL) {
4139 xmlElementPtr tmp = (xmlElementPtr) cur;
4140 q = (xmlNodePtr)
4141 xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix);
4142 } else if (cur->type == XML_ATTRIBUTE_DECL) {
4143 xmlAttributePtr tmp = (xmlAttributePtr) cur;
4144 q = (xmlNodePtr)
4145 xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix);
4146 } else if (cur->type == XML_COMMENT_NODE) {
4147 q = xmlCopyNode(cur, 0);
4148 }
4149
4150 if (q == NULL) {
4151 cur = cur->next;
4152 continue;
4153 }
4154
4155 if (p == NULL)
4156 ret->children = q;
4157 else
4158 p->next = q;
4159
4160 q->prev = p;
4161 q->parent = (xmlNodePtr) ret;
4162 q->next = NULL;
4163 ret->last = q;
4164 p = q;
4165 cur = cur->next;
4166 }
4167
Owen Taylor3473f882001-02-23 17:55:21 +00004168 return(ret);
4169}
Daniel Veillard2156d432004-03-04 15:59:36 +00004170#endif
Owen Taylor3473f882001-02-23 17:55:21 +00004171
Daniel Veillard2156d432004-03-04 15:59:36 +00004172#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004173/**
4174 * xmlCopyDoc:
4175 * @doc: the document
William M. Brack57e9e912004-03-09 16:19:02 +00004176 * @recursive: if not zero do a recursive copy.
Owen Taylor3473f882001-02-23 17:55:21 +00004177 *
4178 * Do a copy of the document info. If recursive, the content tree will
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004179 * be copied too as well as DTD, namespaces and entities.
Owen Taylor3473f882001-02-23 17:55:21 +00004180 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004181 * Returns: a new #xmlDocPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004182 */
4183xmlDocPtr
4184xmlCopyDoc(xmlDocPtr doc, int recursive) {
4185 xmlDocPtr ret;
4186
4187 if (doc == NULL) return(NULL);
4188 ret = xmlNewDoc(doc->version);
4189 if (ret == NULL) return(NULL);
4190 if (doc->name != NULL)
4191 ret->name = xmlMemStrdup(doc->name);
4192 if (doc->encoding != NULL)
4193 ret->encoding = xmlStrdup(doc->encoding);
Daniel Veillardf59507d2005-01-27 17:26:49 +00004194 if (doc->URL != NULL)
4195 ret->URL = xmlStrdup(doc->URL);
Owen Taylor3473f882001-02-23 17:55:21 +00004196 ret->charset = doc->charset;
4197 ret->compression = doc->compression;
4198 ret->standalone = doc->standalone;
4199 if (!recursive) return(ret);
4200
Daniel Veillardb33c2012001-04-25 12:59:04 +00004201 ret->last = NULL;
4202 ret->children = NULL;
Daniel Veillard2156d432004-03-04 15:59:36 +00004203#ifdef LIBXML_TREE_ENABLED
Daniel Veillardb33c2012001-04-25 12:59:04 +00004204 if (doc->intSubset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004205 ret->intSubset = xmlCopyDtd(doc->intSubset);
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004206 xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret);
Daniel Veillardb33c2012001-04-25 12:59:04 +00004207 ret->intSubset->parent = ret;
4208 }
Daniel Veillard2156d432004-03-04 15:59:36 +00004209#endif
Owen Taylor3473f882001-02-23 17:55:21 +00004210 if (doc->oldNs != NULL)
4211 ret->oldNs = xmlCopyNamespaceList(doc->oldNs);
4212 if (doc->children != NULL) {
4213 xmlNodePtr tmp;
Daniel Veillardb33c2012001-04-25 12:59:04 +00004214
4215 ret->children = xmlStaticCopyNodeList(doc->children, ret,
4216 (xmlNodePtr)ret);
Owen Taylor3473f882001-02-23 17:55:21 +00004217 ret->last = NULL;
4218 tmp = ret->children;
4219 while (tmp != NULL) {
4220 if (tmp->next == NULL)
4221 ret->last = tmp;
4222 tmp = tmp->next;
4223 }
4224 }
4225 return(ret);
4226}
Daniel Veillard652327a2003-09-29 18:02:38 +00004227#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004228
4229/************************************************************************
4230 * *
4231 * Content access functions *
4232 * *
4233 ************************************************************************/
4234
4235/**
Daniel Veillard8faa7832001-11-26 15:58:08 +00004236 * xmlGetLineNo:
Daniel Veillard01c13b52002-12-10 15:19:08 +00004237 * @node: valid node
Daniel Veillard8faa7832001-11-26 15:58:08 +00004238 *
William M. Brackd7cf7f82003-11-14 07:13:16 +00004239 * Get line number of @node. This requires activation of this option
Daniel Veillardd1640922001-12-17 15:30:10 +00004240 * before invoking the parser by calling xmlLineNumbersDefault(1)
Daniel Veillard8faa7832001-11-26 15:58:08 +00004241 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004242 * Returns the line number if successful, -1 otherwise
Daniel Veillard8faa7832001-11-26 15:58:08 +00004243 */
4244long
4245xmlGetLineNo(xmlNodePtr node)
4246{
4247 long result = -1;
4248
4249 if (!node)
4250 return result;
Daniel Veillard73da77e2005-08-24 14:05:37 +00004251 if ((node->type == XML_ELEMENT_NODE) ||
4252 (node->type == XML_TEXT_NODE) ||
4253 (node->type == XML_COMMENT_NODE) ||
4254 (node->type == XML_PI_NODE))
Daniel Veillard3e35f8e2003-10-21 00:05:38 +00004255 result = (long) node->line;
Daniel Veillard8faa7832001-11-26 15:58:08 +00004256 else if ((node->prev != NULL) &&
4257 ((node->prev->type == XML_ELEMENT_NODE) ||
Daniel Veillard73da77e2005-08-24 14:05:37 +00004258 (node->prev->type == XML_TEXT_NODE) ||
4259 (node->prev->type == XML_COMMENT_NODE) ||
4260 (node->prev->type == XML_PI_NODE)))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004261 result = xmlGetLineNo(node->prev);
4262 else if ((node->parent != NULL) &&
Daniel Veillard73da77e2005-08-24 14:05:37 +00004263 (node->parent->type == XML_ELEMENT_NODE))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004264 result = xmlGetLineNo(node->parent);
4265
4266 return result;
4267}
4268
Daniel Veillard2156d432004-03-04 15:59:36 +00004269#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED)
Daniel Veillard8faa7832001-11-26 15:58:08 +00004270/**
4271 * xmlGetNodePath:
4272 * @node: a node
4273 *
4274 * Build a structure based Path for the given node
4275 *
4276 * Returns the new path or NULL in case of error. The caller must free
4277 * the returned string
4278 */
4279xmlChar *
4280xmlGetNodePath(xmlNodePtr node)
4281{
4282 xmlNodePtr cur, tmp, next;
4283 xmlChar *buffer = NULL, *temp;
4284 size_t buf_len;
4285 xmlChar *buf;
Daniel Veillard96c3a3b2002-10-14 15:39:04 +00004286 const char *sep;
Daniel Veillard8faa7832001-11-26 15:58:08 +00004287 const char *name;
4288 char nametemp[100];
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004289 int occur = 0, generic;
Daniel Veillard8faa7832001-11-26 15:58:08 +00004290
4291 if (node == NULL)
4292 return (NULL);
4293
4294 buf_len = 500;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004295 buffer = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004296 if (buffer == NULL) {
4297 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004298 return (NULL);
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004299 }
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004300 buf = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
Daniel Veillard8faa7832001-11-26 15:58:08 +00004301 if (buf == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004302 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004303 xmlFree(buffer);
4304 return (NULL);
4305 }
4306
4307 buffer[0] = 0;
4308 cur = node;
4309 do {
4310 name = "";
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004311 sep = "?";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004312 occur = 0;
4313 if ((cur->type == XML_DOCUMENT_NODE) ||
4314 (cur->type == XML_HTML_DOCUMENT_NODE)) {
4315 if (buffer[0] == '/')
4316 break;
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004317 sep = "/";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004318 next = NULL;
4319 } else if (cur->type == XML_ELEMENT_NODE) {
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004320 generic = 0;
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004321 sep = "/";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004322 name = (const char *) cur->name;
4323 if (cur->ns) {
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004324 if (cur->ns->prefix != NULL) {
William M. Brack13dfa872004-09-18 04:52:08 +00004325 snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
4326 (char *)cur->ns->prefix, (char *)cur->name);
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004327 nametemp[sizeof(nametemp) - 1] = 0;
4328 name = nametemp;
4329 } else {
4330 /*
4331 * We cannot express named elements in the default
4332 * namespace, so use "*".
4333 */
4334 generic = 1;
4335 name = "*";
4336 }
Daniel Veillard8faa7832001-11-26 15:58:08 +00004337 }
4338 next = cur->parent;
4339
4340 /*
4341 * Thumbler index computation
Daniel Veillardc00cda82003-04-07 10:22:39 +00004342 * TODO: the ocurence test seems bogus for namespaced names
Daniel Veillard8faa7832001-11-26 15:58:08 +00004343 */
4344 tmp = cur->prev;
4345 while (tmp != NULL) {
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00004346 if ((tmp->type == XML_ELEMENT_NODE) &&
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004347 (generic ||
Kasimier T. Buchcik43ceb1e2006-06-12 11:08:18 +00004348 (xmlStrEqual(cur->name, tmp->name) &&
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004349 ((tmp->ns == cur->ns) ||
4350 ((tmp->ns != NULL) && (cur->ns != NULL) &&
Kasimier T. Buchcik43ceb1e2006-06-12 11:08:18 +00004351 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004352 occur++;
4353 tmp = tmp->prev;
4354 }
4355 if (occur == 0) {
4356 tmp = cur->next;
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004357 while (tmp != NULL && occur == 0) {
4358 if ((tmp->type == XML_ELEMENT_NODE) &&
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004359 (generic ||
Kasimier T. Buchcik43ceb1e2006-06-12 11:08:18 +00004360 (xmlStrEqual(cur->name, tmp->name) &&
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004361 ((tmp->ns == cur->ns) ||
4362 ((tmp->ns != NULL) && (cur->ns != NULL) &&
Kasimier T. Buchcik43ceb1e2006-06-12 11:08:18 +00004363 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004364 occur++;
4365 tmp = tmp->next;
4366 }
4367 if (occur != 0)
4368 occur = 1;
4369 } else
4370 occur++;
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004371 } else if (cur->type == XML_COMMENT_NODE) {
4372 sep = "/";
4373 name = "comment()";
4374 next = cur->parent;
4375
4376 /*
4377 * Thumbler index computation
4378 */
4379 tmp = cur->prev;
4380 while (tmp != NULL) {
4381 if (tmp->type == XML_COMMENT_NODE)
4382 occur++;
4383 tmp = tmp->prev;
4384 }
4385 if (occur == 0) {
4386 tmp = cur->next;
4387 while (tmp != NULL && occur == 0) {
4388 if (tmp->type == XML_COMMENT_NODE)
4389 occur++;
4390 tmp = tmp->next;
4391 }
4392 if (occur != 0)
4393 occur = 1;
4394 } else
4395 occur++;
4396 } else if ((cur->type == XML_TEXT_NODE) ||
4397 (cur->type == XML_CDATA_SECTION_NODE)) {
4398 sep = "/";
4399 name = "text()";
4400 next = cur->parent;
4401
4402 /*
4403 * Thumbler index computation
4404 */
4405 tmp = cur->prev;
4406 while (tmp != NULL) {
Kasimier T. Buchcikeb468702006-02-15 10:57:50 +00004407 if ((tmp->type == XML_TEXT_NODE) ||
4408 (tmp->type == XML_CDATA_SECTION_NODE))
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004409 occur++;
4410 tmp = tmp->prev;
4411 }
Kasimier T. Buchcikeb468702006-02-15 10:57:50 +00004412 /*
4413 * Evaluate if this is the only text- or CDATA-section-node;
4414 * if yes, then we'll get "text()", otherwise "text()[1]".
4415 */
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004416 if (occur == 0) {
4417 tmp = cur->next;
Kasimier T. Buchcikeb468702006-02-15 10:57:50 +00004418 while (tmp != NULL) {
4419 if ((tmp->type == XML_TEXT_NODE) ||
4420 (tmp->type == XML_CDATA_SECTION_NODE))
4421 {
4422 occur = 1;
4423 break;
4424 }
4425 tmp = tmp->next;
4426 }
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004427 } else
4428 occur++;
4429 } else if (cur->type == XML_PI_NODE) {
4430 sep = "/";
4431 snprintf(nametemp, sizeof(nametemp) - 1,
William M. Brack13dfa872004-09-18 04:52:08 +00004432 "processing-instruction('%s')", (char *)cur->name);
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004433 nametemp[sizeof(nametemp) - 1] = 0;
4434 name = nametemp;
4435
4436 next = cur->parent;
4437
4438 /*
4439 * Thumbler index computation
4440 */
4441 tmp = cur->prev;
4442 while (tmp != NULL) {
4443 if ((tmp->type == XML_PI_NODE) &&
4444 (xmlStrEqual(cur->name, tmp->name)))
4445 occur++;
4446 tmp = tmp->prev;
4447 }
4448 if (occur == 0) {
4449 tmp = cur->next;
4450 while (tmp != NULL && occur == 0) {
4451 if ((tmp->type == XML_PI_NODE) &&
4452 (xmlStrEqual(cur->name, tmp->name)))
4453 occur++;
4454 tmp = tmp->next;
4455 }
4456 if (occur != 0)
4457 occur = 1;
4458 } else
4459 occur++;
4460
Daniel Veillard8faa7832001-11-26 15:58:08 +00004461 } else if (cur->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004462 sep = "/@";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004463 name = (const char *) (((xmlAttrPtr) cur)->name);
Daniel Veillard365c8062005-07-19 11:31:55 +00004464 if (cur->ns) {
4465 if (cur->ns->prefix != NULL)
4466 snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
4467 (char *)cur->ns->prefix, (char *)cur->name);
4468 else
4469 snprintf(nametemp, sizeof(nametemp) - 1, "%s",
4470 (char *)cur->name);
4471 nametemp[sizeof(nametemp) - 1] = 0;
4472 name = nametemp;
4473 }
Daniel Veillard8faa7832001-11-26 15:58:08 +00004474 next = ((xmlAttrPtr) cur)->parent;
4475 } else {
4476 next = cur->parent;
4477 }
4478
4479 /*
4480 * Make sure there is enough room
4481 */
4482 if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) {
4483 buf_len =
4484 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20;
4485 temp = (xmlChar *) xmlRealloc(buffer, buf_len);
4486 if (temp == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004487 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004488 xmlFree(buf);
4489 xmlFree(buffer);
4490 return (NULL);
4491 }
4492 buffer = temp;
4493 temp = (xmlChar *) xmlRealloc(buf, buf_len);
4494 if (temp == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004495 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004496 xmlFree(buf);
4497 xmlFree(buffer);
4498 return (NULL);
4499 }
4500 buf = temp;
4501 }
4502 if (occur == 0)
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004503 snprintf((char *) buf, buf_len, "%s%s%s",
Daniel Veillard8faa7832001-11-26 15:58:08 +00004504 sep, name, (char *) buffer);
4505 else
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004506 snprintf((char *) buf, buf_len, "%s%s[%d]%s",
Daniel Veillard8faa7832001-11-26 15:58:08 +00004507 sep, name, occur, (char *) buffer);
William M. Brack13dfa872004-09-18 04:52:08 +00004508 snprintf((char *) buffer, buf_len, "%s", (char *)buf);
Daniel Veillard8faa7832001-11-26 15:58:08 +00004509 cur = next;
4510 } while (cur != NULL);
4511 xmlFree(buf);
4512 return (buffer);
4513}
Daniel Veillard652327a2003-09-29 18:02:38 +00004514#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard8faa7832001-11-26 15:58:08 +00004515
4516/**
Owen Taylor3473f882001-02-23 17:55:21 +00004517 * xmlDocGetRootElement:
4518 * @doc: the document
4519 *
4520 * Get the root element of the document (doc->children is a list
4521 * containing possibly comments, PIs, etc ...).
4522 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004523 * Returns the #xmlNodePtr for the root or NULL
Owen Taylor3473f882001-02-23 17:55:21 +00004524 */
4525xmlNodePtr
4526xmlDocGetRootElement(xmlDocPtr doc) {
4527 xmlNodePtr ret;
4528
4529 if (doc == NULL) return(NULL);
4530 ret = doc->children;
4531 while (ret != NULL) {
4532 if (ret->type == XML_ELEMENT_NODE)
4533 return(ret);
4534 ret = ret->next;
4535 }
4536 return(ret);
4537}
4538
Daniel Veillard2156d432004-03-04 15:59:36 +00004539#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004540/**
4541 * xmlDocSetRootElement:
4542 * @doc: the document
Daniel Veillard26a45c82006-10-20 12:55:34 +00004543 * @root: the new document root element, if root is NULL no action is taken,
4544 * to remove a node from a document use xmlUnlinkNode(root) instead.
Owen Taylor3473f882001-02-23 17:55:21 +00004545 *
4546 * Set the root element of the document (doc->children is a list
4547 * containing possibly comments, PIs, etc ...).
4548 *
Daniel Veillard26a45c82006-10-20 12:55:34 +00004549 * Returns the old root element if any was found, NULL if root was NULL
Owen Taylor3473f882001-02-23 17:55:21 +00004550 */
4551xmlNodePtr
4552xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) {
4553 xmlNodePtr old = NULL;
4554
4555 if (doc == NULL) return(NULL);
Daniel Veillardc575b992002-02-08 13:28:40 +00004556 if (root == NULL)
4557 return(NULL);
4558 xmlUnlinkNode(root);
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00004559 xmlSetTreeDoc(root, doc);
Daniel Veillardc575b992002-02-08 13:28:40 +00004560 root->parent = (xmlNodePtr) doc;
Owen Taylor3473f882001-02-23 17:55:21 +00004561 old = doc->children;
4562 while (old != NULL) {
4563 if (old->type == XML_ELEMENT_NODE)
4564 break;
4565 old = old->next;
4566 }
4567 if (old == NULL) {
4568 if (doc->children == NULL) {
4569 doc->children = root;
4570 doc->last = root;
4571 } else {
4572 xmlAddSibling(doc->children, root);
4573 }
4574 } else {
4575 xmlReplaceNode(old, root);
4576 }
4577 return(old);
4578}
Daniel Veillard2156d432004-03-04 15:59:36 +00004579#endif
Owen Taylor3473f882001-02-23 17:55:21 +00004580
Daniel Veillard2156d432004-03-04 15:59:36 +00004581#if defined(LIBXML_TREE_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004582/**
4583 * xmlNodeSetLang:
4584 * @cur: the node being changed
Daniel Veillardd1640922001-12-17 15:30:10 +00004585 * @lang: the language description
Owen Taylor3473f882001-02-23 17:55:21 +00004586 *
4587 * Set the language of a node, i.e. the values of the xml:lang
4588 * attribute.
4589 */
4590void
4591xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004592 xmlNsPtr ns;
4593
Owen Taylor3473f882001-02-23 17:55:21 +00004594 if (cur == NULL) return;
4595 switch(cur->type) {
4596 case XML_TEXT_NODE:
4597 case XML_CDATA_SECTION_NODE:
4598 case XML_COMMENT_NODE:
4599 case XML_DOCUMENT_NODE:
4600 case XML_DOCUMENT_TYPE_NODE:
4601 case XML_DOCUMENT_FRAG_NODE:
4602 case XML_NOTATION_NODE:
4603 case XML_HTML_DOCUMENT_NODE:
4604 case XML_DTD_NODE:
4605 case XML_ELEMENT_DECL:
4606 case XML_ATTRIBUTE_DECL:
4607 case XML_ENTITY_DECL:
4608 case XML_PI_NODE:
4609 case XML_ENTITY_REF_NODE:
4610 case XML_ENTITY_NODE:
4611 case XML_NAMESPACE_DECL:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004612#ifdef LIBXML_DOCB_ENABLED
4613 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004614#endif
4615 case XML_XINCLUDE_START:
4616 case XML_XINCLUDE_END:
4617 return;
4618 case XML_ELEMENT_NODE:
4619 case XML_ATTRIBUTE_NODE:
4620 break;
4621 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004622 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4623 if (ns == NULL)
4624 return;
4625 xmlSetNsProp(cur, ns, BAD_CAST "lang", lang);
Owen Taylor3473f882001-02-23 17:55:21 +00004626}
Daniel Veillard652327a2003-09-29 18:02:38 +00004627#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004628
4629/**
4630 * xmlNodeGetLang:
4631 * @cur: the node being checked
4632 *
4633 * Searches the language of a node, i.e. the values of the xml:lang
4634 * attribute or the one carried by the nearest ancestor.
4635 *
4636 * Returns a pointer to the lang value, or NULL if not found
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004637 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00004638 */
4639xmlChar *
4640xmlNodeGetLang(xmlNodePtr cur) {
4641 xmlChar *lang;
4642
4643 while (cur != NULL) {
Daniel Veillardc17337c2001-05-09 10:51:31 +00004644 lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE);
Owen Taylor3473f882001-02-23 17:55:21 +00004645 if (lang != NULL)
4646 return(lang);
4647 cur = cur->parent;
4648 }
4649 return(NULL);
4650}
4651
4652
Daniel Veillard652327a2003-09-29 18:02:38 +00004653#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00004654/**
4655 * xmlNodeSetSpacePreserve:
4656 * @cur: the node being changed
4657 * @val: the xml:space value ("0": default, 1: "preserve")
4658 *
4659 * Set (or reset) the space preserving behaviour of a node, i.e. the
4660 * value of the xml:space attribute.
4661 */
4662void
4663xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004664 xmlNsPtr ns;
4665
Owen Taylor3473f882001-02-23 17:55:21 +00004666 if (cur == NULL) return;
4667 switch(cur->type) {
4668 case XML_TEXT_NODE:
4669 case XML_CDATA_SECTION_NODE:
4670 case XML_COMMENT_NODE:
4671 case XML_DOCUMENT_NODE:
4672 case XML_DOCUMENT_TYPE_NODE:
4673 case XML_DOCUMENT_FRAG_NODE:
4674 case XML_NOTATION_NODE:
4675 case XML_HTML_DOCUMENT_NODE:
4676 case XML_DTD_NODE:
4677 case XML_ELEMENT_DECL:
4678 case XML_ATTRIBUTE_DECL:
4679 case XML_ENTITY_DECL:
4680 case XML_PI_NODE:
4681 case XML_ENTITY_REF_NODE:
4682 case XML_ENTITY_NODE:
4683 case XML_NAMESPACE_DECL:
4684 case XML_XINCLUDE_START:
4685 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004686#ifdef LIBXML_DOCB_ENABLED
4687 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004688#endif
4689 return;
4690 case XML_ELEMENT_NODE:
4691 case XML_ATTRIBUTE_NODE:
4692 break;
4693 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004694 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4695 if (ns == NULL)
4696 return;
Owen Taylor3473f882001-02-23 17:55:21 +00004697 switch (val) {
4698 case 0:
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004699 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "default");
Owen Taylor3473f882001-02-23 17:55:21 +00004700 break;
4701 case 1:
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004702 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "preserve");
Owen Taylor3473f882001-02-23 17:55:21 +00004703 break;
4704 }
4705}
Daniel Veillard652327a2003-09-29 18:02:38 +00004706#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004707
4708/**
4709 * xmlNodeGetSpacePreserve:
4710 * @cur: the node being checked
4711 *
4712 * Searches the space preserving behaviour of a node, i.e. the values
4713 * of the xml:space attribute or the one carried by the nearest
4714 * ancestor.
4715 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004716 * Returns -1 if xml:space is not inherited, 0 if "default", 1 if "preserve"
Owen Taylor3473f882001-02-23 17:55:21 +00004717 */
4718int
4719xmlNodeGetSpacePreserve(xmlNodePtr cur) {
4720 xmlChar *space;
4721
4722 while (cur != NULL) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004723 space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE);
Owen Taylor3473f882001-02-23 17:55:21 +00004724 if (space != NULL) {
4725 if (xmlStrEqual(space, BAD_CAST "preserve")) {
4726 xmlFree(space);
4727 return(1);
4728 }
4729 if (xmlStrEqual(space, BAD_CAST "default")) {
4730 xmlFree(space);
4731 return(0);
4732 }
4733 xmlFree(space);
4734 }
4735 cur = cur->parent;
4736 }
4737 return(-1);
4738}
4739
Daniel Veillard652327a2003-09-29 18:02:38 +00004740#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00004741/**
4742 * xmlNodeSetName:
4743 * @cur: the node being changed
4744 * @name: the new tag name
4745 *
4746 * Set (or reset) the name of a node.
4747 */
4748void
4749xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) {
Daniel Veillardce244ad2004-11-05 10:03:46 +00004750 xmlDocPtr doc;
4751 xmlDictPtr dict;
4752
Owen Taylor3473f882001-02-23 17:55:21 +00004753 if (cur == NULL) return;
4754 if (name == NULL) return;
4755 switch(cur->type) {
4756 case XML_TEXT_NODE:
4757 case XML_CDATA_SECTION_NODE:
4758 case XML_COMMENT_NODE:
4759 case XML_DOCUMENT_TYPE_NODE:
4760 case XML_DOCUMENT_FRAG_NODE:
4761 case XML_NOTATION_NODE:
4762 case XML_HTML_DOCUMENT_NODE:
4763 case XML_NAMESPACE_DECL:
4764 case XML_XINCLUDE_START:
4765 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004766#ifdef LIBXML_DOCB_ENABLED
4767 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004768#endif
4769 return;
4770 case XML_ELEMENT_NODE:
4771 case XML_ATTRIBUTE_NODE:
4772 case XML_PI_NODE:
4773 case XML_ENTITY_REF_NODE:
4774 case XML_ENTITY_NODE:
4775 case XML_DTD_NODE:
4776 case XML_DOCUMENT_NODE:
4777 case XML_ELEMENT_DECL:
4778 case XML_ATTRIBUTE_DECL:
4779 case XML_ENTITY_DECL:
4780 break;
4781 }
Daniel Veillardce244ad2004-11-05 10:03:46 +00004782 doc = cur->doc;
4783 if (doc != NULL)
4784 dict = doc->dict;
4785 else
4786 dict = NULL;
4787 if (dict != NULL) {
4788 if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name)))
4789 xmlFree((xmlChar *) cur->name);
4790 cur->name = xmlDictLookup(dict, name, -1);
4791 } else {
4792 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
4793 cur->name = xmlStrdup(name);
4794 }
Owen Taylor3473f882001-02-23 17:55:21 +00004795}
Daniel Veillard2156d432004-03-04 15:59:36 +00004796#endif
Owen Taylor3473f882001-02-23 17:55:21 +00004797
Daniel Veillard2156d432004-03-04 15:59:36 +00004798#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004799/**
4800 * xmlNodeSetBase:
4801 * @cur: the node being changed
4802 * @uri: the new base URI
4803 *
4804 * Set (or reset) the base URI of a node, i.e. the value of the
4805 * xml:base attribute.
4806 */
4807void
Daniel Veillardf85ce8e2003-09-22 10:24:45 +00004808xmlNodeSetBase(xmlNodePtr cur, const xmlChar* uri) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004809 xmlNsPtr ns;
Daniel Veillardb8efdda2006-10-10 12:37:14 +00004810 const xmlChar* fixed;
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004811
Owen Taylor3473f882001-02-23 17:55:21 +00004812 if (cur == NULL) return;
4813 switch(cur->type) {
4814 case XML_TEXT_NODE:
4815 case XML_CDATA_SECTION_NODE:
4816 case XML_COMMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004817 case XML_DOCUMENT_TYPE_NODE:
4818 case XML_DOCUMENT_FRAG_NODE:
4819 case XML_NOTATION_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004820 case XML_DTD_NODE:
4821 case XML_ELEMENT_DECL:
4822 case XML_ATTRIBUTE_DECL:
4823 case XML_ENTITY_DECL:
4824 case XML_PI_NODE:
4825 case XML_ENTITY_REF_NODE:
4826 case XML_ENTITY_NODE:
4827 case XML_NAMESPACE_DECL:
4828 case XML_XINCLUDE_START:
4829 case XML_XINCLUDE_END:
Owen Taylor3473f882001-02-23 17:55:21 +00004830 return;
4831 case XML_ELEMENT_NODE:
4832 case XML_ATTRIBUTE_NODE:
4833 break;
Daniel Veillard4cbe4702002-05-05 06:57:27 +00004834 case XML_DOCUMENT_NODE:
4835#ifdef LIBXML_DOCB_ENABLED
4836 case XML_DOCB_DOCUMENT_NODE:
4837#endif
4838 case XML_HTML_DOCUMENT_NODE: {
4839 xmlDocPtr doc = (xmlDocPtr) cur;
4840
4841 if (doc->URL != NULL)
4842 xmlFree((xmlChar *) doc->URL);
4843 if (uri == NULL)
4844 doc->URL = NULL;
4845 else
Daniel Veillardb8efdda2006-10-10 12:37:14 +00004846 doc->URL = xmlPathToURI(uri);
Daniel Veillard4cbe4702002-05-05 06:57:27 +00004847 return;
4848 }
Owen Taylor3473f882001-02-23 17:55:21 +00004849 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004850
4851 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4852 if (ns == NULL)
4853 return;
Daniel Veillardb8efdda2006-10-10 12:37:14 +00004854 fixed = xmlPathToURI(uri);
4855 if (fixed != NULL) {
4856 xmlSetNsProp(cur, ns, BAD_CAST "base", fixed);
William M. Brack38d452a2007-05-22 16:00:06 +00004857 xmlFree((xmlChar *)fixed);
Daniel Veillardb8efdda2006-10-10 12:37:14 +00004858 } else {
4859 xmlSetNsProp(cur, ns, BAD_CAST "base", uri);
4860 }
Owen Taylor3473f882001-02-23 17:55:21 +00004861}
Daniel Veillard652327a2003-09-29 18:02:38 +00004862#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004863
4864/**
Owen Taylor3473f882001-02-23 17:55:21 +00004865 * xmlNodeGetBase:
4866 * @doc: the document the node pertains to
4867 * @cur: the node being checked
4868 *
4869 * Searches for the BASE URL. The code should work on both XML
4870 * and HTML document even if base mechanisms are completely different.
4871 * It returns the base as defined in RFC 2396 sections
4872 * 5.1.1. Base URI within Document Content
4873 * and
4874 * 5.1.2. Base URI from the Encapsulating Entity
4875 * However it does not return the document base (5.1.3), use
4876 * xmlDocumentGetBase() for this
4877 *
4878 * Returns a pointer to the base URL, or NULL if not found
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004879 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00004880 */
4881xmlChar *
4882xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00004883 xmlChar *oldbase = NULL;
4884 xmlChar *base, *newbase;
Owen Taylor3473f882001-02-23 17:55:21 +00004885
4886 if ((cur == NULL) && (doc == NULL))
4887 return(NULL);
4888 if (doc == NULL) doc = cur->doc;
4889 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
4890 cur = doc->children;
4891 while ((cur != NULL) && (cur->name != NULL)) {
4892 if (cur->type != XML_ELEMENT_NODE) {
4893 cur = cur->next;
4894 continue;
4895 }
4896 if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) {
4897 cur = cur->children;
4898 continue;
4899 }
4900 if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) {
4901 cur = cur->children;
4902 continue;
4903 }
4904 if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) {
4905 return(xmlGetProp(cur, BAD_CAST "href"));
4906 }
4907 cur = cur->next;
4908 }
4909 return(NULL);
4910 }
4911 while (cur != NULL) {
4912 if (cur->type == XML_ENTITY_DECL) {
4913 xmlEntityPtr ent = (xmlEntityPtr) cur;
4914 return(xmlStrdup(ent->URI));
4915 }
Daniel Veillard42596ad2001-05-22 16:57:14 +00004916 if (cur->type == XML_ELEMENT_NODE) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00004917 base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE);
Daniel Veillard42596ad2001-05-22 16:57:14 +00004918 if (base != NULL) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00004919 if (oldbase != NULL) {
4920 newbase = xmlBuildURI(oldbase, base);
4921 if (newbase != NULL) {
4922 xmlFree(oldbase);
4923 xmlFree(base);
4924 oldbase = newbase;
4925 } else {
4926 xmlFree(oldbase);
4927 xmlFree(base);
4928 return(NULL);
4929 }
4930 } else {
4931 oldbase = base;
4932 }
4933 if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) ||
4934 (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) ||
4935 (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4)))
4936 return(oldbase);
Daniel Veillard42596ad2001-05-22 16:57:14 +00004937 }
4938 }
Owen Taylor3473f882001-02-23 17:55:21 +00004939 cur = cur->parent;
4940 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00004941 if ((doc != NULL) && (doc->URL != NULL)) {
4942 if (oldbase == NULL)
4943 return(xmlStrdup(doc->URL));
4944 newbase = xmlBuildURI(oldbase, doc->URL);
4945 xmlFree(oldbase);
4946 return(newbase);
4947 }
4948 return(oldbase);
Owen Taylor3473f882001-02-23 17:55:21 +00004949}
4950
4951/**
Daniel Veillard78697292003-10-19 20:44:43 +00004952 * xmlNodeBufGetContent:
4953 * @buffer: a buffer
4954 * @cur: the node being read
4955 *
4956 * Read the value of a node @cur, this can be either the text carried
4957 * directly by this node if it's a TEXT node or the aggregate string
4958 * of the values carried by this node child's (TEXT and ENTITY_REF).
4959 * Entity references are substituted.
4960 * Fills up the buffer @buffer with this value
4961 *
4962 * Returns 0 in case of success and -1 in case of error.
4963 */
4964int
4965xmlNodeBufGetContent(xmlBufferPtr buffer, xmlNodePtr cur)
4966{
4967 if ((cur == NULL) || (buffer == NULL)) return(-1);
4968 switch (cur->type) {
4969 case XML_CDATA_SECTION_NODE:
4970 case XML_TEXT_NODE:
4971 xmlBufferCat(buffer, cur->content);
4972 break;
4973 case XML_DOCUMENT_FRAG_NODE:
4974 case XML_ELEMENT_NODE:{
4975 xmlNodePtr tmp = cur;
4976
4977 while (tmp != NULL) {
4978 switch (tmp->type) {
4979 case XML_CDATA_SECTION_NODE:
4980 case XML_TEXT_NODE:
4981 if (tmp->content != NULL)
4982 xmlBufferCat(buffer, tmp->content);
4983 break;
4984 case XML_ENTITY_REF_NODE:
Rob Richards77b92ff2005-12-20 15:55:14 +00004985 xmlNodeBufGetContent(buffer, tmp);
4986 break;
Daniel Veillard78697292003-10-19 20:44:43 +00004987 default:
4988 break;
4989 }
4990 /*
4991 * Skip to next node
4992 */
4993 if (tmp->children != NULL) {
4994 if (tmp->children->type != XML_ENTITY_DECL) {
4995 tmp = tmp->children;
4996 continue;
4997 }
4998 }
4999 if (tmp == cur)
5000 break;
5001
5002 if (tmp->next != NULL) {
5003 tmp = tmp->next;
5004 continue;
5005 }
5006
5007 do {
5008 tmp = tmp->parent;
5009 if (tmp == NULL)
5010 break;
5011 if (tmp == cur) {
5012 tmp = NULL;
5013 break;
5014 }
5015 if (tmp->next != NULL) {
5016 tmp = tmp->next;
5017 break;
5018 }
5019 } while (tmp != NULL);
5020 }
5021 break;
5022 }
5023 case XML_ATTRIBUTE_NODE:{
5024 xmlAttrPtr attr = (xmlAttrPtr) cur;
5025 xmlNodePtr tmp = attr->children;
5026
5027 while (tmp != NULL) {
5028 if (tmp->type == XML_TEXT_NODE)
5029 xmlBufferCat(buffer, tmp->content);
5030 else
5031 xmlNodeBufGetContent(buffer, tmp);
5032 tmp = tmp->next;
5033 }
5034 break;
5035 }
5036 case XML_COMMENT_NODE:
5037 case XML_PI_NODE:
5038 xmlBufferCat(buffer, cur->content);
5039 break;
5040 case XML_ENTITY_REF_NODE:{
5041 xmlEntityPtr ent;
5042 xmlNodePtr tmp;
5043
5044 /* lookup entity declaration */
5045 ent = xmlGetDocEntity(cur->doc, cur->name);
5046 if (ent == NULL)
5047 return(-1);
5048
5049 /* an entity content can be any "well balanced chunk",
5050 * i.e. the result of the content [43] production:
5051 * http://www.w3.org/TR/REC-xml#NT-content
5052 * -> we iterate through child nodes and recursive call
5053 * xmlNodeGetContent() which handles all possible node types */
5054 tmp = ent->children;
5055 while (tmp) {
5056 xmlNodeBufGetContent(buffer, tmp);
5057 tmp = tmp->next;
5058 }
5059 break;
5060 }
5061 case XML_ENTITY_NODE:
5062 case XML_DOCUMENT_TYPE_NODE:
5063 case XML_NOTATION_NODE:
5064 case XML_DTD_NODE:
5065 case XML_XINCLUDE_START:
5066 case XML_XINCLUDE_END:
5067 break;
5068 case XML_DOCUMENT_NODE:
5069#ifdef LIBXML_DOCB_ENABLED
5070 case XML_DOCB_DOCUMENT_NODE:
5071#endif
5072 case XML_HTML_DOCUMENT_NODE:
5073 cur = cur->children;
5074 while (cur!= NULL) {
5075 if ((cur->type == XML_ELEMENT_NODE) ||
5076 (cur->type == XML_TEXT_NODE) ||
5077 (cur->type == XML_CDATA_SECTION_NODE)) {
5078 xmlNodeBufGetContent(buffer, cur);
5079 }
5080 cur = cur->next;
5081 }
5082 break;
5083 case XML_NAMESPACE_DECL:
5084 xmlBufferCat(buffer, ((xmlNsPtr) cur)->href);
5085 break;
5086 case XML_ELEMENT_DECL:
5087 case XML_ATTRIBUTE_DECL:
5088 case XML_ENTITY_DECL:
5089 break;
5090 }
5091 return(0);
5092}
5093/**
Owen Taylor3473f882001-02-23 17:55:21 +00005094 * xmlNodeGetContent:
5095 * @cur: the node being read
5096 *
5097 * Read the value of a node, this can be either the text carried
5098 * directly by this node if it's a TEXT node or the aggregate string
5099 * of the values carried by this node child's (TEXT and ENTITY_REF).
Daniel Veillardd1640922001-12-17 15:30:10 +00005100 * Entity references are substituted.
5101 * Returns a new #xmlChar * or NULL if no content is available.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00005102 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00005103 */
5104xmlChar *
Daniel Veillard7646b182002-04-20 06:41:40 +00005105xmlNodeGetContent(xmlNodePtr cur)
5106{
5107 if (cur == NULL)
5108 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005109 switch (cur->type) {
5110 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005111 case XML_ELEMENT_NODE:{
Daniel Veillard7646b182002-04-20 06:41:40 +00005112 xmlBufferPtr buffer;
5113 xmlChar *ret;
Owen Taylor3473f882001-02-23 17:55:21 +00005114
Daniel Veillard814a76d2003-01-23 18:24:20 +00005115 buffer = xmlBufferCreateSize(64);
Daniel Veillard7646b182002-04-20 06:41:40 +00005116 if (buffer == NULL)
5117 return (NULL);
Daniel Veillardc4696922003-10-19 21:47:14 +00005118 xmlNodeBufGetContent(buffer, cur);
Daniel Veillard7646b182002-04-20 06:41:40 +00005119 ret = buffer->content;
5120 buffer->content = NULL;
5121 xmlBufferFree(buffer);
5122 return (ret);
5123 }
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005124 case XML_ATTRIBUTE_NODE:
5125 return(xmlGetPropNodeValueInternal((xmlAttrPtr) cur));
Owen Taylor3473f882001-02-23 17:55:21 +00005126 case XML_COMMENT_NODE:
5127 case XML_PI_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005128 if (cur->content != NULL)
5129 return (xmlStrdup(cur->content));
5130 return (NULL);
5131 case XML_ENTITY_REF_NODE:{
5132 xmlEntityPtr ent;
Daniel Veillard7646b182002-04-20 06:41:40 +00005133 xmlBufferPtr buffer;
5134 xmlChar *ret;
5135
5136 /* lookup entity declaration */
5137 ent = xmlGetDocEntity(cur->doc, cur->name);
5138 if (ent == NULL)
5139 return (NULL);
5140
5141 buffer = xmlBufferCreate();
5142 if (buffer == NULL)
5143 return (NULL);
5144
Daniel Veillardc4696922003-10-19 21:47:14 +00005145 xmlNodeBufGetContent(buffer, cur);
Daniel Veillard7646b182002-04-20 06:41:40 +00005146
5147 ret = buffer->content;
5148 buffer->content = NULL;
5149 xmlBufferFree(buffer);
5150 return (ret);
5151 }
Owen Taylor3473f882001-02-23 17:55:21 +00005152 case XML_ENTITY_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005153 case XML_DOCUMENT_TYPE_NODE:
5154 case XML_NOTATION_NODE:
5155 case XML_DTD_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005156 case XML_XINCLUDE_START:
5157 case XML_XINCLUDE_END:
Daniel Veillard9adc0462003-03-24 18:39:54 +00005158 return (NULL);
5159 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005160#ifdef LIBXML_DOCB_ENABLED
Daniel Veillard7646b182002-04-20 06:41:40 +00005161 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005162#endif
Daniel Veillard9adc0462003-03-24 18:39:54 +00005163 case XML_HTML_DOCUMENT_NODE: {
Daniel Veillardc4696922003-10-19 21:47:14 +00005164 xmlBufferPtr buffer;
5165 xmlChar *ret;
Daniel Veillard9adc0462003-03-24 18:39:54 +00005166
Daniel Veillardc4696922003-10-19 21:47:14 +00005167 buffer = xmlBufferCreate();
5168 if (buffer == NULL)
5169 return (NULL);
5170
5171 xmlNodeBufGetContent(buffer, (xmlNodePtr) cur);
5172
5173 ret = buffer->content;
5174 buffer->content = NULL;
5175 xmlBufferFree(buffer);
5176 return (ret);
Daniel Veillard9adc0462003-03-24 18:39:54 +00005177 }
Daniel Veillard96c3a3b2002-10-14 15:39:04 +00005178 case XML_NAMESPACE_DECL: {
5179 xmlChar *tmp;
5180
5181 tmp = xmlStrdup(((xmlNsPtr) cur)->href);
5182 return (tmp);
5183 }
Owen Taylor3473f882001-02-23 17:55:21 +00005184 case XML_ELEMENT_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00005185 /* TODO !!! */
5186 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005187 case XML_ATTRIBUTE_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00005188 /* TODO !!! */
5189 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005190 case XML_ENTITY_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00005191 /* TODO !!! */
5192 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005193 case XML_CDATA_SECTION_NODE:
5194 case XML_TEXT_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005195 if (cur->content != NULL)
5196 return (xmlStrdup(cur->content));
5197 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005198 }
Daniel Veillard7646b182002-04-20 06:41:40 +00005199 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005200}
Daniel Veillard652327a2003-09-29 18:02:38 +00005201
Owen Taylor3473f882001-02-23 17:55:21 +00005202/**
5203 * xmlNodeSetContent:
5204 * @cur: the node being modified
5205 * @content: the new value of the content
5206 *
5207 * Replace the content of a node.
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00005208 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
5209 * references, but XML special chars need to be escaped first by using
5210 * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars().
Owen Taylor3473f882001-02-23 17:55:21 +00005211 */
5212void
5213xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) {
5214 if (cur == NULL) {
5215#ifdef DEBUG_TREE
5216 xmlGenericError(xmlGenericErrorContext,
5217 "xmlNodeSetContent : node == NULL\n");
5218#endif
5219 return;
5220 }
5221 switch (cur->type) {
5222 case XML_DOCUMENT_FRAG_NODE:
5223 case XML_ELEMENT_NODE:
Daniel Veillard2c748c62002-01-16 15:37:50 +00005224 case XML_ATTRIBUTE_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005225 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5226 cur->children = xmlStringGetNodeList(cur->doc, content);
5227 UPDATE_LAST_CHILD_AND_PARENT(cur)
5228 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005229 case XML_TEXT_NODE:
5230 case XML_CDATA_SECTION_NODE:
5231 case XML_ENTITY_REF_NODE:
5232 case XML_ENTITY_NODE:
5233 case XML_PI_NODE:
5234 case XML_COMMENT_NODE:
Daniel Veillard8874b942005-08-25 13:19:21 +00005235 if ((cur->content != NULL) &&
5236 (cur->content != (xmlChar *) &(cur->properties))) {
William M. Brack7762bb12004-01-04 14:49:01 +00005237 if (!((cur->doc != NULL) && (cur->doc->dict != NULL) &&
Daniel Veillardc587bce2005-05-10 15:28:08 +00005238 (xmlDictOwns(cur->doc->dict, cur->content))))
William M. Brack7762bb12004-01-04 14:49:01 +00005239 xmlFree(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00005240 }
5241 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5242 cur->last = cur->children = NULL;
5243 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005244 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00005245 } else
5246 cur->content = NULL;
Daniel Veillard8874b942005-08-25 13:19:21 +00005247 cur->properties = NULL;
5248 cur->nsDef = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00005249 break;
5250 case XML_DOCUMENT_NODE:
5251 case XML_HTML_DOCUMENT_NODE:
5252 case XML_DOCUMENT_TYPE_NODE:
5253 case XML_XINCLUDE_START:
5254 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005255#ifdef LIBXML_DOCB_ENABLED
5256 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005257#endif
5258 break;
5259 case XML_NOTATION_NODE:
5260 break;
5261 case XML_DTD_NODE:
5262 break;
5263 case XML_NAMESPACE_DECL:
5264 break;
5265 case XML_ELEMENT_DECL:
5266 /* TODO !!! */
5267 break;
5268 case XML_ATTRIBUTE_DECL:
5269 /* TODO !!! */
5270 break;
5271 case XML_ENTITY_DECL:
5272 /* TODO !!! */
5273 break;
5274 }
5275}
5276
Daniel Veillard652327a2003-09-29 18:02:38 +00005277#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00005278/**
5279 * xmlNodeSetContentLen:
5280 * @cur: the node being modified
5281 * @content: the new value of the content
5282 * @len: the size of @content
5283 *
5284 * Replace the content of a node.
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00005285 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
5286 * references, but XML special chars need to be escaped first by using
5287 * xmlEncodeEntitiesReentrant() resp. xmlEncodeSpecialChars().
Owen Taylor3473f882001-02-23 17:55:21 +00005288 */
5289void
5290xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5291 if (cur == NULL) {
5292#ifdef DEBUG_TREE
5293 xmlGenericError(xmlGenericErrorContext,
5294 "xmlNodeSetContentLen : node == NULL\n");
5295#endif
5296 return;
5297 }
5298 switch (cur->type) {
5299 case XML_DOCUMENT_FRAG_NODE:
5300 case XML_ELEMENT_NODE:
Daniel Veillard2c748c62002-01-16 15:37:50 +00005301 case XML_ATTRIBUTE_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005302 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5303 cur->children = xmlStringLenGetNodeList(cur->doc, content, len);
5304 UPDATE_LAST_CHILD_AND_PARENT(cur)
5305 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005306 case XML_TEXT_NODE:
5307 case XML_CDATA_SECTION_NODE:
5308 case XML_ENTITY_REF_NODE:
5309 case XML_ENTITY_NODE:
5310 case XML_PI_NODE:
5311 case XML_COMMENT_NODE:
5312 case XML_NOTATION_NODE:
Daniel Veillard8874b942005-08-25 13:19:21 +00005313 if ((cur->content != NULL) &&
5314 (cur->content != (xmlChar *) &(cur->properties))) {
5315 if (!((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5316 (xmlDictOwns(cur->doc->dict, cur->content))))
5317 xmlFree(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00005318 }
5319 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5320 cur->children = cur->last = NULL;
5321 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005322 cur->content = xmlStrndup(content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00005323 } else
5324 cur->content = NULL;
Daniel Veillard8874b942005-08-25 13:19:21 +00005325 cur->properties = NULL;
5326 cur->nsDef = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00005327 break;
5328 case XML_DOCUMENT_NODE:
5329 case XML_DTD_NODE:
5330 case XML_HTML_DOCUMENT_NODE:
5331 case XML_DOCUMENT_TYPE_NODE:
5332 case XML_NAMESPACE_DECL:
5333 case XML_XINCLUDE_START:
5334 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005335#ifdef LIBXML_DOCB_ENABLED
5336 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005337#endif
5338 break;
5339 case XML_ELEMENT_DECL:
5340 /* TODO !!! */
5341 break;
5342 case XML_ATTRIBUTE_DECL:
5343 /* TODO !!! */
5344 break;
5345 case XML_ENTITY_DECL:
5346 /* TODO !!! */
5347 break;
5348 }
5349}
Daniel Veillard652327a2003-09-29 18:02:38 +00005350#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005351
5352/**
5353 * xmlNodeAddContentLen:
5354 * @cur: the node being modified
5355 * @content: extra content
5356 * @len: the size of @content
5357 *
5358 * Append the extra substring to the node content.
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00005359 * NOTE: In contrast to xmlNodeSetContentLen(), @content is supposed to be
5360 * raw text, so unescaped XML special chars are allowed, entity
5361 * references are not supported.
Owen Taylor3473f882001-02-23 17:55:21 +00005362 */
5363void
5364xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5365 if (cur == NULL) {
5366#ifdef DEBUG_TREE
5367 xmlGenericError(xmlGenericErrorContext,
5368 "xmlNodeAddContentLen : node == NULL\n");
5369#endif
5370 return;
5371 }
5372 if (len <= 0) return;
5373 switch (cur->type) {
5374 case XML_DOCUMENT_FRAG_NODE:
5375 case XML_ELEMENT_NODE: {
Daniel Veillardacb2bda2002-01-13 16:15:43 +00005376 xmlNodePtr last, newNode, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00005377
Daniel Veillard7db37732001-07-12 01:20:08 +00005378 last = cur->last;
Owen Taylor3473f882001-02-23 17:55:21 +00005379 newNode = xmlNewTextLen(content, len);
5380 if (newNode != NULL) {
Daniel Veillardacb2bda2002-01-13 16:15:43 +00005381 tmp = xmlAddChild(cur, newNode);
5382 if (tmp != newNode)
5383 return;
Owen Taylor3473f882001-02-23 17:55:21 +00005384 if ((last != NULL) && (last->next == newNode)) {
5385 xmlTextMerge(last, newNode);
5386 }
5387 }
5388 break;
5389 }
5390 case XML_ATTRIBUTE_NODE:
5391 break;
5392 case XML_TEXT_NODE:
5393 case XML_CDATA_SECTION_NODE:
5394 case XML_ENTITY_REF_NODE:
5395 case XML_ENTITY_NODE:
5396 case XML_PI_NODE:
5397 case XML_COMMENT_NODE:
5398 case XML_NOTATION_NODE:
5399 if (content != NULL) {
Daniel Veillard8874b942005-08-25 13:19:21 +00005400 if ((cur->content == (xmlChar *) &(cur->properties)) ||
5401 ((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5402 xmlDictOwns(cur->doc->dict, cur->content))) {
5403 cur->content = xmlStrncatNew(cur->content, content, len);
5404 cur->properties = NULL;
5405 cur->nsDef = NULL;
William M. Brack7762bb12004-01-04 14:49:01 +00005406 break;
5407 }
Owen Taylor3473f882001-02-23 17:55:21 +00005408 cur->content = xmlStrncat(cur->content, content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00005409 }
5410 case XML_DOCUMENT_NODE:
5411 case XML_DTD_NODE:
5412 case XML_HTML_DOCUMENT_NODE:
5413 case XML_DOCUMENT_TYPE_NODE:
5414 case XML_NAMESPACE_DECL:
5415 case XML_XINCLUDE_START:
5416 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005417#ifdef LIBXML_DOCB_ENABLED
5418 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005419#endif
5420 break;
5421 case XML_ELEMENT_DECL:
5422 case XML_ATTRIBUTE_DECL:
5423 case XML_ENTITY_DECL:
5424 break;
5425 }
5426}
5427
5428/**
5429 * xmlNodeAddContent:
5430 * @cur: the node being modified
5431 * @content: extra content
5432 *
5433 * Append the extra substring to the node content.
Daniel Veillard0e05f4c2006-11-01 15:33:04 +00005434 * NOTE: In contrast to xmlNodeSetContent(), @content is supposed to be
5435 * raw text, so unescaped XML special chars are allowed, entity
5436 * references are not supported.
Owen Taylor3473f882001-02-23 17:55:21 +00005437 */
5438void
5439xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) {
5440 int len;
5441
5442 if (cur == NULL) {
5443#ifdef DEBUG_TREE
5444 xmlGenericError(xmlGenericErrorContext,
5445 "xmlNodeAddContent : node == NULL\n");
5446#endif
5447 return;
5448 }
5449 if (content == NULL) return;
5450 len = xmlStrlen(content);
5451 xmlNodeAddContentLen(cur, content, len);
5452}
5453
5454/**
5455 * xmlTextMerge:
5456 * @first: the first text node
5457 * @second: the second text node being merged
5458 *
5459 * Merge two text nodes into one
5460 * Returns the first text node augmented
5461 */
5462xmlNodePtr
5463xmlTextMerge(xmlNodePtr first, xmlNodePtr second) {
5464 if (first == NULL) return(second);
5465 if (second == NULL) return(first);
5466 if (first->type != XML_TEXT_NODE) return(first);
5467 if (second->type != XML_TEXT_NODE) return(first);
5468 if (second->name != first->name)
5469 return(first);
Owen Taylor3473f882001-02-23 17:55:21 +00005470 xmlNodeAddContent(first, second->content);
Owen Taylor3473f882001-02-23 17:55:21 +00005471 xmlUnlinkNode(second);
5472 xmlFreeNode(second);
5473 return(first);
5474}
5475
Daniel Veillardf1a27c62006-10-13 22:33:03 +00005476#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00005477/**
5478 * xmlGetNsList:
5479 * @doc: the document
5480 * @node: the current node
5481 *
5482 * Search all the namespace applying to a given element.
Daniel Veillardd1640922001-12-17 15:30:10 +00005483 * Returns an NULL terminated array of all the #xmlNsPtr found
Owen Taylor3473f882001-02-23 17:55:21 +00005484 * that need to be freed by the caller or NULL if no
5485 * namespace if defined
5486 */
5487xmlNsPtr *
Daniel Veillard77044732001-06-29 21:31:07 +00005488xmlGetNsList(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node)
5489{
Owen Taylor3473f882001-02-23 17:55:21 +00005490 xmlNsPtr cur;
5491 xmlNsPtr *ret = NULL;
5492 int nbns = 0;
5493 int maxns = 10;
5494 int i;
5495
5496 while (node != NULL) {
Daniel Veillard77044732001-06-29 21:31:07 +00005497 if (node->type == XML_ELEMENT_NODE) {
5498 cur = node->nsDef;
5499 while (cur != NULL) {
5500 if (ret == NULL) {
5501 ret =
5502 (xmlNsPtr *) xmlMalloc((maxns + 1) *
5503 sizeof(xmlNsPtr));
5504 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005505 xmlTreeErrMemory("getting namespace list");
Daniel Veillard77044732001-06-29 21:31:07 +00005506 return (NULL);
5507 }
5508 ret[nbns] = NULL;
5509 }
5510 for (i = 0; i < nbns; i++) {
5511 if ((cur->prefix == ret[i]->prefix) ||
5512 (xmlStrEqual(cur->prefix, ret[i]->prefix)))
5513 break;
5514 }
5515 if (i >= nbns) {
5516 if (nbns >= maxns) {
5517 maxns *= 2;
5518 ret = (xmlNsPtr *) xmlRealloc(ret,
5519 (maxns +
5520 1) *
5521 sizeof(xmlNsPtr));
5522 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005523 xmlTreeErrMemory("getting namespace list");
Daniel Veillard77044732001-06-29 21:31:07 +00005524 return (NULL);
5525 }
5526 }
5527 ret[nbns++] = cur;
5528 ret[nbns] = NULL;
5529 }
Owen Taylor3473f882001-02-23 17:55:21 +00005530
Daniel Veillard77044732001-06-29 21:31:07 +00005531 cur = cur->next;
5532 }
5533 }
5534 node = node->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00005535 }
Daniel Veillard77044732001-06-29 21:31:07 +00005536 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00005537}
Daniel Veillard652327a2003-09-29 18:02:38 +00005538#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005539
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005540/*
5541* xmlTreeEnsureXMLDecl:
5542* @doc: the doc
5543*
5544* Ensures that there is an XML namespace declaration on the doc.
5545*
5546* Returns the XML ns-struct or NULL on API and internal errors.
5547*/
5548static xmlNsPtr
5549xmlTreeEnsureXMLDecl(xmlDocPtr doc)
5550{
5551 if (doc == NULL)
5552 return (NULL);
5553 if (doc->oldNs != NULL)
5554 return (doc->oldNs);
5555 {
5556 xmlNsPtr ns;
5557 ns = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5558 if (ns == NULL) {
5559 xmlTreeErrMemory(
5560 "allocating the XML namespace");
5561 return (NULL);
5562 }
5563 memset(ns, 0, sizeof(xmlNs));
5564 ns->type = XML_LOCAL_NAMESPACE;
5565 ns->href = xmlStrdup(XML_XML_NAMESPACE);
5566 ns->prefix = xmlStrdup((const xmlChar *)"xml");
5567 doc->oldNs = ns;
5568 return (ns);
5569 }
5570}
5571
Owen Taylor3473f882001-02-23 17:55:21 +00005572/**
5573 * xmlSearchNs:
5574 * @doc: the document
5575 * @node: the current node
Daniel Veillard77851712001-02-27 21:54:07 +00005576 * @nameSpace: the namespace prefix
Owen Taylor3473f882001-02-23 17:55:21 +00005577 *
5578 * Search a Ns registered under a given name space for a document.
5579 * recurse on the parents until it finds the defined namespace
5580 * or return NULL otherwise.
5581 * @nameSpace can be NULL, this is a search for the default namespace.
5582 * We don't allow to cross entities boundaries. If you don't declare
5583 * the namespace within those you will be in troubles !!! A warning
5584 * is generated to cover this case.
5585 *
5586 * Returns the namespace pointer or NULL.
5587 */
5588xmlNsPtr
5589xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) {
Daniel Veillardf4e56292003-10-28 14:27:41 +00005590
Owen Taylor3473f882001-02-23 17:55:21 +00005591 xmlNsPtr cur;
Daniel Veillardf4e56292003-10-28 14:27:41 +00005592 xmlNodePtr orig = node;
Owen Taylor3473f882001-02-23 17:55:21 +00005593
5594 if (node == NULL) return(NULL);
5595 if ((nameSpace != NULL) &&
5596 (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) {
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00005597 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
5598 /*
5599 * The XML-1.0 namespace is normally held on the root
5600 * element. In this case exceptionally create it on the
5601 * node element.
5602 */
5603 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5604 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005605 xmlTreeErrMemory("searching namespace");
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00005606 return(NULL);
5607 }
5608 memset(cur, 0, sizeof(xmlNs));
5609 cur->type = XML_LOCAL_NAMESPACE;
5610 cur->href = xmlStrdup(XML_XML_NAMESPACE);
5611 cur->prefix = xmlStrdup((const xmlChar *)"xml");
5612 cur->next = node->nsDef;
5613 node->nsDef = cur;
5614 return(cur);
5615 }
Daniel Veillard11ce4002006-03-10 00:36:23 +00005616 if (doc == NULL) {
5617 doc = node->doc;
5618 if (doc == NULL)
5619 return(NULL);
5620 }
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005621 /*
5622 * Return the XML namespace declaration held by the doc.
5623 */
5624 if (doc->oldNs == NULL)
5625 return(xmlTreeEnsureXMLDecl(doc));
5626 else
5627 return(doc->oldNs);
Owen Taylor3473f882001-02-23 17:55:21 +00005628 }
5629 while (node != NULL) {
5630 if ((node->type == XML_ENTITY_REF_NODE) ||
5631 (node->type == XML_ENTITY_NODE) ||
5632 (node->type == XML_ENTITY_DECL))
5633 return(NULL);
5634 if (node->type == XML_ELEMENT_NODE) {
5635 cur = node->nsDef;
5636 while (cur != NULL) {
5637 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
5638 (cur->href != NULL))
5639 return(cur);
5640 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
5641 (cur->href != NULL) &&
5642 (xmlStrEqual(cur->prefix, nameSpace)))
5643 return(cur);
5644 cur = cur->next;
5645 }
Daniel Veillardf4e56292003-10-28 14:27:41 +00005646 if (orig != node) {
5647 cur = node->ns;
5648 if (cur != NULL) {
5649 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
5650 (cur->href != NULL))
5651 return(cur);
5652 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
5653 (cur->href != NULL) &&
5654 (xmlStrEqual(cur->prefix, nameSpace)))
5655 return(cur);
5656 }
5657 }
Owen Taylor3473f882001-02-23 17:55:21 +00005658 }
5659 node = node->parent;
5660 }
5661 return(NULL);
5662}
5663
5664/**
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005665 * xmlNsInScope:
5666 * @doc: the document
5667 * @node: the current node
5668 * @ancestor: the ancestor carrying the namespace
5669 * @prefix: the namespace prefix
5670 *
5671 * Verify that the given namespace held on @ancestor is still in scope
5672 * on node.
5673 *
5674 * Returns 1 if true, 0 if false and -1 in case of error.
5675 */
5676static int
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005677xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node,
5678 xmlNodePtr ancestor, const xmlChar * prefix)
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005679{
5680 xmlNsPtr tst;
5681
5682 while ((node != NULL) && (node != ancestor)) {
5683 if ((node->type == XML_ENTITY_REF_NODE) ||
5684 (node->type == XML_ENTITY_NODE) ||
5685 (node->type == XML_ENTITY_DECL))
5686 return (-1);
5687 if (node->type == XML_ELEMENT_NODE) {
5688 tst = node->nsDef;
5689 while (tst != NULL) {
5690 if ((tst->prefix == NULL)
5691 && (prefix == NULL))
5692 return (0);
5693 if ((tst->prefix != NULL)
5694 && (prefix != NULL)
5695 && (xmlStrEqual(tst->prefix, prefix)))
5696 return (0);
5697 tst = tst->next;
5698 }
5699 }
5700 node = node->parent;
5701 }
5702 if (node != ancestor)
5703 return (-1);
5704 return (1);
5705}
5706
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005707/**
Owen Taylor3473f882001-02-23 17:55:21 +00005708 * xmlSearchNsByHref:
5709 * @doc: the document
5710 * @node: the current node
5711 * @href: the namespace value
5712 *
5713 * Search a Ns aliasing a given URI. Recurse on the parents until it finds
5714 * the defined namespace or return NULL otherwise.
5715 * Returns the namespace pointer or NULL.
5716 */
5717xmlNsPtr
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005718xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href)
5719{
Owen Taylor3473f882001-02-23 17:55:21 +00005720 xmlNsPtr cur;
5721 xmlNodePtr orig = node;
Daniel Veillard62040be2004-05-17 03:17:26 +00005722 int is_attr;
Owen Taylor3473f882001-02-23 17:55:21 +00005723
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005724 if ((node == NULL) || (href == NULL))
5725 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005726 if (xmlStrEqual(href, XML_XML_NAMESPACE)) {
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005727 /*
5728 * Only the document can hold the XML spec namespace.
5729 */
5730 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
5731 /*
5732 * The XML-1.0 namespace is normally held on the root
5733 * element. In this case exceptionally create it on the
5734 * node element.
5735 */
5736 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5737 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005738 xmlTreeErrMemory("searching namespace");
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005739 return (NULL);
5740 }
5741 memset(cur, 0, sizeof(xmlNs));
5742 cur->type = XML_LOCAL_NAMESPACE;
5743 cur->href = xmlStrdup(XML_XML_NAMESPACE);
5744 cur->prefix = xmlStrdup((const xmlChar *) "xml");
5745 cur->next = node->nsDef;
5746 node->nsDef = cur;
5747 return (cur);
5748 }
Daniel Veillard11ce4002006-03-10 00:36:23 +00005749 if (doc == NULL) {
5750 doc = node->doc;
5751 if (doc == NULL)
5752 return(NULL);
5753 }
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005754 /*
5755 * Return the XML namespace declaration held by the doc.
5756 */
5757 if (doc->oldNs == NULL)
5758 return(xmlTreeEnsureXMLDecl(doc));
5759 else
5760 return(doc->oldNs);
Owen Taylor3473f882001-02-23 17:55:21 +00005761 }
Daniel Veillard62040be2004-05-17 03:17:26 +00005762 is_attr = (node->type == XML_ATTRIBUTE_NODE);
Owen Taylor3473f882001-02-23 17:55:21 +00005763 while (node != NULL) {
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005764 if ((node->type == XML_ENTITY_REF_NODE) ||
5765 (node->type == XML_ENTITY_NODE) ||
5766 (node->type == XML_ENTITY_DECL))
5767 return (NULL);
5768 if (node->type == XML_ELEMENT_NODE) {
5769 cur = node->nsDef;
5770 while (cur != NULL) {
5771 if ((cur->href != NULL) && (href != NULL) &&
5772 (xmlStrEqual(cur->href, href))) {
Daniel Veillard62040be2004-05-17 03:17:26 +00005773 if (((!is_attr) || (cur->prefix != NULL)) &&
Kasimier T. Buchcikba70cc02005-02-28 10:28:21 +00005774 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005775 return (cur);
5776 }
5777 cur = cur->next;
5778 }
Daniel Veillardf4e56292003-10-28 14:27:41 +00005779 if (orig != node) {
5780 cur = node->ns;
5781 if (cur != NULL) {
5782 if ((cur->href != NULL) && (href != NULL) &&
5783 (xmlStrEqual(cur->href, href))) {
Daniel Veillard62040be2004-05-17 03:17:26 +00005784 if (((!is_attr) || (cur->prefix != NULL)) &&
Kasimier T. Buchcikba70cc02005-02-28 10:28:21 +00005785 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
Daniel Veillardf4e56292003-10-28 14:27:41 +00005786 return (cur);
5787 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005788 }
Daniel Veillardf4e56292003-10-28 14:27:41 +00005789 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005790 }
5791 node = node->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00005792 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005793 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005794}
5795
5796/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005797 * xmlNewReconciliedNs:
Owen Taylor3473f882001-02-23 17:55:21 +00005798 * @doc: the document
5799 * @tree: a node expected to hold the new namespace
5800 * @ns: the original namespace
5801 *
5802 * This function tries to locate a namespace definition in a tree
5803 * ancestors, or create a new namespace definition node similar to
5804 * @ns trying to reuse the same prefix. However if the given prefix is
5805 * null (default namespace) or reused within the subtree defined by
5806 * @tree or on one of its ancestors then a new prefix is generated.
5807 * Returns the (new) namespace definition or NULL in case of error
5808 */
5809xmlNsPtr
5810xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) {
5811 xmlNsPtr def;
5812 xmlChar prefix[50];
5813 int counter = 1;
5814
5815 if (tree == NULL) {
5816#ifdef DEBUG_TREE
5817 xmlGenericError(xmlGenericErrorContext,
5818 "xmlNewReconciliedNs : tree == NULL\n");
5819#endif
5820 return(NULL);
5821 }
Daniel Veillardce244ad2004-11-05 10:03:46 +00005822 if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005823#ifdef DEBUG_TREE
5824 xmlGenericError(xmlGenericErrorContext,
5825 "xmlNewReconciliedNs : ns == NULL\n");
5826#endif
5827 return(NULL);
5828 }
5829 /*
5830 * Search an existing namespace definition inherited.
5831 */
5832 def = xmlSearchNsByHref(doc, tree, ns->href);
5833 if (def != NULL)
5834 return(def);
5835
5836 /*
5837 * Find a close prefix which is not already in use.
5838 * Let's strip namespace prefixes longer than 20 chars !
5839 */
Daniel Veillardf742d342002-03-07 00:05:35 +00005840 if (ns->prefix == NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00005841 snprintf((char *) prefix, sizeof(prefix), "default");
Daniel Veillardf742d342002-03-07 00:05:35 +00005842 else
William M. Brack13dfa872004-09-18 04:52:08 +00005843 snprintf((char *) prefix, sizeof(prefix), "%.20s", (char *)ns->prefix);
Daniel Veillardf742d342002-03-07 00:05:35 +00005844
Owen Taylor3473f882001-02-23 17:55:21 +00005845 def = xmlSearchNs(doc, tree, prefix);
5846 while (def != NULL) {
5847 if (counter > 1000) return(NULL);
Daniel Veillardf742d342002-03-07 00:05:35 +00005848 if (ns->prefix == NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00005849 snprintf((char *) prefix, sizeof(prefix), "default%d", counter++);
Daniel Veillardf742d342002-03-07 00:05:35 +00005850 else
William M. Brack13dfa872004-09-18 04:52:08 +00005851 snprintf((char *) prefix, sizeof(prefix), "%.20s%d",
5852 (char *)ns->prefix, counter++);
Owen Taylor3473f882001-02-23 17:55:21 +00005853 def = xmlSearchNs(doc, tree, prefix);
5854 }
5855
5856 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00005857 * OK, now we are ready to create a new one.
Owen Taylor3473f882001-02-23 17:55:21 +00005858 */
5859 def = xmlNewNs(tree, ns->href, prefix);
5860 return(def);
5861}
5862
Daniel Veillard652327a2003-09-29 18:02:38 +00005863#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00005864/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005865 * xmlReconciliateNs:
Owen Taylor3473f882001-02-23 17:55:21 +00005866 * @doc: the document
5867 * @tree: a node defining the subtree to reconciliate
5868 *
5869 * This function checks that all the namespaces declared within the given
5870 * tree are properly declared. This is needed for example after Copy or Cut
5871 * and then paste operations. The subtree may still hold pointers to
5872 * namespace declarations outside the subtree or invalid/masked. As much
Daniel Veillardd1640922001-12-17 15:30:10 +00005873 * as possible the function try to reuse the existing namespaces found in
Owen Taylor3473f882001-02-23 17:55:21 +00005874 * the new environment. If not possible the new namespaces are redeclared
5875 * on @tree at the top of the given subtree.
5876 * Returns the number of namespace declarations created or -1 in case of error.
5877 */
5878int
5879xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) {
5880 xmlNsPtr *oldNs = NULL;
5881 xmlNsPtr *newNs = NULL;
5882 int sizeCache = 0;
5883 int nbCache = 0;
5884
5885 xmlNsPtr n;
5886 xmlNodePtr node = tree;
5887 xmlAttrPtr attr;
5888 int ret = 0, i;
5889
Daniel Veillardce244ad2004-11-05 10:03:46 +00005890 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) return(-1);
5891 if ((doc == NULL) || (doc->type != XML_DOCUMENT_NODE)) return(-1);
5892 if (node->doc != doc) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00005893 while (node != NULL) {
5894 /*
5895 * Reconciliate the node namespace
5896 */
5897 if (node->ns != NULL) {
5898 /*
5899 * initialize the cache if needed
5900 */
5901 if (sizeCache == 0) {
5902 sizeCache = 10;
5903 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
5904 sizeof(xmlNsPtr));
5905 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005906 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005907 return(-1);
5908 }
5909 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
5910 sizeof(xmlNsPtr));
5911 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005912 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005913 xmlFree(oldNs);
5914 return(-1);
5915 }
5916 }
5917 for (i = 0;i < nbCache;i++) {
5918 if (oldNs[i] == node->ns) {
5919 node->ns = newNs[i];
5920 break;
5921 }
5922 }
5923 if (i == nbCache) {
5924 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00005925 * OK we need to recreate a new namespace definition
Owen Taylor3473f882001-02-23 17:55:21 +00005926 */
5927 n = xmlNewReconciliedNs(doc, tree, node->ns);
5928 if (n != NULL) { /* :-( what if else ??? */
5929 /*
5930 * check if we need to grow the cache buffers.
5931 */
5932 if (sizeCache <= nbCache) {
5933 sizeCache *= 2;
5934 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
5935 sizeof(xmlNsPtr));
5936 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005937 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005938 xmlFree(newNs);
5939 return(-1);
5940 }
5941 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
5942 sizeof(xmlNsPtr));
5943 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005944 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005945 xmlFree(oldNs);
5946 return(-1);
5947 }
5948 }
5949 newNs[nbCache] = n;
5950 oldNs[nbCache++] = node->ns;
5951 node->ns = n;
5952 }
5953 }
5954 }
5955 /*
5956 * now check for namespace hold by attributes on the node.
5957 */
Daniel Veillardb5f11972006-10-14 08:46:40 +00005958 if (node->type == XML_ELEMENT_NODE) {
5959 attr = node->properties;
5960 while (attr != NULL) {
5961 if (attr->ns != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005962 /*
Daniel Veillardb5f11972006-10-14 08:46:40 +00005963 * initialize the cache if needed
Owen Taylor3473f882001-02-23 17:55:21 +00005964 */
Daniel Veillardb5f11972006-10-14 08:46:40 +00005965 if (sizeCache == 0) {
5966 sizeCache = 10;
5967 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
5968 sizeof(xmlNsPtr));
5969 if (oldNs == NULL) {
5970 xmlTreeErrMemory("fixing namespaces");
5971 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00005972 }
Daniel Veillardb5f11972006-10-14 08:46:40 +00005973 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
5974 sizeof(xmlNsPtr));
5975 if (newNs == NULL) {
5976 xmlTreeErrMemory("fixing namespaces");
5977 xmlFree(oldNs);
5978 return(-1);
5979 }
5980 }
5981 for (i = 0;i < nbCache;i++) {
5982 if (oldNs[i] == attr->ns) {
5983 attr->ns = newNs[i];
5984 break;
5985 }
5986 }
5987 if (i == nbCache) {
5988 /*
5989 * OK we need to recreate a new namespace definition
5990 */
5991 n = xmlNewReconciliedNs(doc, tree, attr->ns);
5992 if (n != NULL) { /* :-( what if else ??? */
5993 /*
5994 * check if we need to grow the cache buffers.
5995 */
5996 if (sizeCache <= nbCache) {
5997 sizeCache *= 2;
5998 oldNs = (xmlNsPtr *) xmlRealloc(oldNs,
5999 sizeCache * sizeof(xmlNsPtr));
6000 if (oldNs == NULL) {
6001 xmlTreeErrMemory("fixing namespaces");
6002 xmlFree(newNs);
6003 return(-1);
6004 }
6005 newNs = (xmlNsPtr *) xmlRealloc(newNs,
6006 sizeCache * sizeof(xmlNsPtr));
6007 if (newNs == NULL) {
6008 xmlTreeErrMemory("fixing namespaces");
6009 xmlFree(oldNs);
6010 return(-1);
6011 }
6012 }
6013 newNs[nbCache] = n;
6014 oldNs[nbCache++] = attr->ns;
6015 attr->ns = n;
6016 }
Owen Taylor3473f882001-02-23 17:55:21 +00006017 }
6018 }
Daniel Veillardb5f11972006-10-14 08:46:40 +00006019 attr = attr->next;
Owen Taylor3473f882001-02-23 17:55:21 +00006020 }
Owen Taylor3473f882001-02-23 17:55:21 +00006021 }
6022
6023 /*
6024 * Browse the full subtree, deep first
6025 */
Daniel Veillardb5f11972006-10-14 08:46:40 +00006026 if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006027 /* deep first */
6028 node = node->children;
6029 } else if ((node != tree) && (node->next != NULL)) {
6030 /* then siblings */
6031 node = node->next;
6032 } else if (node != tree) {
6033 /* go up to parents->next if needed */
6034 while (node != tree) {
6035 if (node->parent != NULL)
6036 node = node->parent;
6037 if ((node != tree) && (node->next != NULL)) {
6038 node = node->next;
6039 break;
6040 }
6041 if (node->parent == NULL) {
6042 node = NULL;
6043 break;
6044 }
6045 }
6046 /* exit condition */
6047 if (node == tree)
6048 node = NULL;
Daniel Veillard1e774382002-03-06 17:35:40 +00006049 } else
6050 break;
Owen Taylor3473f882001-02-23 17:55:21 +00006051 }
Daniel Veillardf742d342002-03-07 00:05:35 +00006052 if (oldNs != NULL)
6053 xmlFree(oldNs);
6054 if (newNs != NULL)
6055 xmlFree(newNs);
Owen Taylor3473f882001-02-23 17:55:21 +00006056 return(ret);
6057}
Daniel Veillard652327a2003-09-29 18:02:38 +00006058#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00006059
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006060static xmlAttrPtr
6061xmlGetPropNodeInternal(xmlNodePtr node, const xmlChar *name,
6062 const xmlChar *nsName, int useDTD)
6063{
6064 xmlAttrPtr prop;
6065
6066 if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL))
6067 return(NULL);
6068
6069 if (node->properties != NULL) {
6070 prop = node->properties;
6071 if (nsName == NULL) {
6072 /*
6073 * We want the attr to be in no namespace.
6074 */
6075 do {
6076 if ((prop->ns == NULL) && xmlStrEqual(prop->name, name)) {
6077 return(prop);
6078 }
6079 prop = prop->next;
6080 } while (prop != NULL);
6081 } else {
6082 /*
6083 * We want the attr to be in the specified namespace.
6084 */
6085 do {
6086 if ((prop->ns != NULL) && xmlStrEqual(prop->name, name) &&
6087 ((prop->ns->href == nsName) ||
6088 xmlStrEqual(prop->ns->href, nsName)))
6089 {
6090 return(prop);
6091 }
6092 prop = prop->next;
6093 } while (prop != NULL);
6094 }
6095 }
6096
6097#ifdef LIBXML_TREE_ENABLED
6098 if (! useDTD)
6099 return(NULL);
6100 /*
6101 * Check if there is a default/fixed attribute declaration in
6102 * the internal or external subset.
6103 */
6104 if ((node->doc != NULL) && (node->doc->intSubset != NULL)) {
6105 xmlDocPtr doc = node->doc;
6106 xmlAttributePtr attrDecl = NULL;
6107 xmlChar *elemQName, *tmpstr = NULL;
6108
6109 /*
6110 * We need the QName of the element for the DTD-lookup.
6111 */
6112 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
6113 tmpstr = xmlStrdup(node->ns->prefix);
6114 tmpstr = xmlStrcat(tmpstr, BAD_CAST ":");
6115 tmpstr = xmlStrcat(tmpstr, node->name);
6116 if (tmpstr == NULL)
6117 return(NULL);
6118 elemQName = tmpstr;
6119 } else
6120 elemQName = (xmlChar *) node->name;
6121 if (nsName == NULL) {
6122 /*
6123 * The common and nice case: Attr in no namespace.
6124 */
6125 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset,
6126 elemQName, name, NULL);
6127 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
6128 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset,
6129 elemQName, name, NULL);
6130 }
6131 } else {
6132 xmlNsPtr *nsList, *cur;
6133
6134 /*
6135 * The ugly case: Search using the prefixes of in-scope
6136 * ns-decls corresponding to @nsName.
6137 */
6138 nsList = xmlGetNsList(node->doc, node);
6139 if (nsList == NULL) {
6140 if (tmpstr != NULL)
6141 xmlFree(tmpstr);
6142 return(NULL);
6143 }
6144 cur = nsList;
6145 while (*cur != NULL) {
6146 if (xmlStrEqual((*cur)->href, nsName)) {
6147 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elemQName,
6148 name, (*cur)->prefix);
6149 if (attrDecl)
6150 break;
6151 if (doc->extSubset != NULL) {
6152 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elemQName,
6153 name, (*cur)->prefix);
6154 if (attrDecl)
6155 break;
6156 }
6157 }
6158 cur++;
6159 }
6160 xmlFree(nsList);
6161 }
6162 if (tmpstr != NULL)
6163 xmlFree(tmpstr);
6164 /*
6165 * Only default/fixed attrs are relevant.
6166 */
6167 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6168 return((xmlAttrPtr) attrDecl);
6169 }
6170#endif /* LIBXML_TREE_ENABLED */
6171 return(NULL);
6172}
6173
6174static xmlChar*
6175xmlGetPropNodeValueInternal(xmlAttrPtr prop)
6176{
6177 if (prop == NULL)
6178 return(NULL);
6179 if (prop->type == XML_ATTRIBUTE_NODE) {
6180 /*
6181 * Note that we return at least the empty string.
6182 * TODO: Do we really always want that?
6183 */
6184 if (prop->children != NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00006185 if ((prop->children->next == NULL) &&
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006186 ((prop->children->type == XML_TEXT_NODE) ||
6187 (prop->children->type == XML_CDATA_SECTION_NODE)))
6188 {
6189 /*
6190 * Optimization for the common case: only 1 text node.
6191 */
6192 return(xmlStrdup(prop->children->content));
6193 } else {
6194 xmlChar *ret;
6195
6196 ret = xmlNodeListGetString(prop->doc, prop->children, 1);
6197 if (ret != NULL)
6198 return(ret);
6199 }
6200 }
6201 return(xmlStrdup((xmlChar *)""));
6202 } else if (prop->type == XML_ATTRIBUTE_DECL) {
6203 return(xmlStrdup(((xmlAttributePtr)prop)->defaultValue));
6204 }
6205 return(NULL);
6206}
6207
Owen Taylor3473f882001-02-23 17:55:21 +00006208/**
6209 * xmlHasProp:
6210 * @node: the node
6211 * @name: the attribute name
6212 *
6213 * Search an attribute associated to a node
6214 * This function also looks in DTD attribute declaration for #FIXED or
6215 * default declaration values unless DTD use has been turned off.
6216 *
6217 * Returns the attribute or the attribute declaration or NULL if
6218 * neither was found.
6219 */
6220xmlAttrPtr
6221xmlHasProp(xmlNodePtr node, const xmlChar *name) {
6222 xmlAttrPtr prop;
6223 xmlDocPtr doc;
6224
Daniel Veillard8874b942005-08-25 13:19:21 +00006225 if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL))
6226 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006227 /*
6228 * Check on the properties attached to the node
6229 */
6230 prop = node->properties;
6231 while (prop != NULL) {
6232 if (xmlStrEqual(prop->name, name)) {
6233 return(prop);
6234 }
6235 prop = prop->next;
6236 }
6237 if (!xmlCheckDTD) return(NULL);
6238
6239 /*
6240 * Check if there is a default declaration in the internal
6241 * or external subsets
6242 */
6243 doc = node->doc;
6244 if (doc != NULL) {
6245 xmlAttributePtr attrDecl;
6246 if (doc->intSubset != NULL) {
6247 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
6248 if ((attrDecl == NULL) && (doc->extSubset != NULL))
6249 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
Daniel Veillard75eb1ad2003-07-07 14:42:44 +00006250 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6251 /* return attribute declaration only if a default value is given
6252 (that includes #FIXED declarations) */
Owen Taylor3473f882001-02-23 17:55:21 +00006253 return((xmlAttrPtr) attrDecl);
6254 }
6255 }
6256 return(NULL);
6257}
6258
6259/**
Daniel Veillarde95e2392001-06-06 10:46:28 +00006260 * xmlHasNsProp:
6261 * @node: the node
6262 * @name: the attribute name
Daniel Veillardca2366a2001-06-11 12:09:01 +00006263 * @nameSpace: the URI of the namespace
Daniel Veillarde95e2392001-06-06 10:46:28 +00006264 *
6265 * Search for an attribute associated to a node
6266 * This attribute has to be anchored in the namespace specified.
6267 * This does the entity substitution.
6268 * This function looks in DTD attribute declaration for #FIXED or
6269 * default declaration values unless DTD use has been turned off.
William M. Brack2c228442004-10-03 04:10:00 +00006270 * Note that a namespace of NULL indicates to use the default namespace.
Daniel Veillarde95e2392001-06-06 10:46:28 +00006271 *
6272 * Returns the attribute or the attribute declaration or NULL
6273 * if neither was found.
6274 */
6275xmlAttrPtr
Daniel Veillardca2366a2001-06-11 12:09:01 +00006276xmlHasNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
Daniel Veillarde95e2392001-06-06 10:46:28 +00006277
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006278 return(xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD));
Daniel Veillarde95e2392001-06-06 10:46:28 +00006279}
6280
6281/**
Owen Taylor3473f882001-02-23 17:55:21 +00006282 * xmlGetProp:
6283 * @node: the node
6284 * @name: the attribute name
6285 *
6286 * Search and get the value of an attribute associated to a node
6287 * This does the entity substitution.
6288 * This function looks in DTD attribute declaration for #FIXED or
6289 * default declaration values unless DTD use has been turned off.
Daniel Veillard784b9352003-02-16 15:50:27 +00006290 * NOTE: this function acts independently of namespaces associated
Daniel Veillard71531f32003-02-05 13:19:53 +00006291 * to the attribute. Use xmlGetNsProp() or xmlGetNoNsProp()
6292 * for namespace aware processing.
Owen Taylor3473f882001-02-23 17:55:21 +00006293 *
6294 * Returns the attribute value or NULL if not found.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00006295 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00006296 */
6297xmlChar *
6298xmlGetProp(xmlNodePtr node, const xmlChar *name) {
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006299 xmlAttrPtr prop;
Owen Taylor3473f882001-02-23 17:55:21 +00006300
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006301 prop = xmlHasProp(node, name);
6302 if (prop == NULL)
6303 return(NULL);
6304 return(xmlGetPropNodeValueInternal(prop));
Owen Taylor3473f882001-02-23 17:55:21 +00006305}
6306
6307/**
Daniel Veillard71531f32003-02-05 13:19:53 +00006308 * xmlGetNoNsProp:
6309 * @node: the node
6310 * @name: the attribute name
6311 *
6312 * Search and get the value of an attribute associated to a node
6313 * This does the entity substitution.
6314 * This function looks in DTD attribute declaration for #FIXED or
6315 * default declaration values unless DTD use has been turned off.
6316 * This function is similar to xmlGetProp except it will accept only
6317 * an attribute in no namespace.
6318 *
6319 * Returns the attribute value or NULL if not found.
6320 * It's up to the caller to free the memory with xmlFree().
6321 */
6322xmlChar *
6323xmlGetNoNsProp(xmlNodePtr node, const xmlChar *name) {
6324 xmlAttrPtr prop;
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006325
6326 prop = xmlGetPropNodeInternal(node, name, NULL, xmlCheckDTD);
6327 if (prop == NULL)
Daniel Veillard8874b942005-08-25 13:19:21 +00006328 return(NULL);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006329 return(xmlGetPropNodeValueInternal(prop));
Daniel Veillard71531f32003-02-05 13:19:53 +00006330}
6331
6332/**
Owen Taylor3473f882001-02-23 17:55:21 +00006333 * xmlGetNsProp:
6334 * @node: the node
6335 * @name: the attribute name
Daniel Veillardca2366a2001-06-11 12:09:01 +00006336 * @nameSpace: the URI of the namespace
Owen Taylor3473f882001-02-23 17:55:21 +00006337 *
6338 * Search and get the value of an attribute associated to a node
6339 * This attribute has to be anchored in the namespace specified.
6340 * This does the entity substitution.
6341 * This function looks in DTD attribute declaration for #FIXED or
6342 * default declaration values unless DTD use has been turned off.
6343 *
6344 * Returns the attribute value or NULL if not found.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00006345 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00006346 */
6347xmlChar *
Daniel Veillardca2366a2001-06-11 12:09:01 +00006348xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
Owen Taylor3473f882001-02-23 17:55:21 +00006349 xmlAttrPtr prop;
Owen Taylor3473f882001-02-23 17:55:21 +00006350
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006351 prop = xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD);
6352 if (prop == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00006353 return(NULL);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006354 return(xmlGetPropNodeValueInternal(prop));
Owen Taylor3473f882001-02-23 17:55:21 +00006355}
6356
Daniel Veillard2156d432004-03-04 15:59:36 +00006357#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
6358/**
6359 * xmlUnsetProp:
6360 * @node: the node
6361 * @name: the attribute name
6362 *
6363 * Remove an attribute carried by a node.
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006364 * This handles only attributes in no namespace.
Daniel Veillard2156d432004-03-04 15:59:36 +00006365 * Returns 0 if successful, -1 if not found
6366 */
6367int
6368xmlUnsetProp(xmlNodePtr node, const xmlChar *name) {
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +00006369 xmlAttrPtr prop;
Daniel Veillard2156d432004-03-04 15:59:36 +00006370
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006371 prop = xmlGetPropNodeInternal(node, name, NULL, 0);
6372 if (prop == NULL)
Daniel Veillard2156d432004-03-04 15:59:36 +00006373 return(-1);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006374 xmlUnlinkNode((xmlNodePtr) prop);
6375 xmlFreeProp(prop);
6376 return(0);
Daniel Veillard2156d432004-03-04 15:59:36 +00006377}
6378
6379/**
6380 * xmlUnsetNsProp:
6381 * @node: the node
6382 * @ns: the namespace definition
6383 * @name: the attribute name
6384 *
6385 * Remove an attribute carried by a node.
6386 * Returns 0 if successful, -1 if not found
6387 */
6388int
6389xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) {
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +00006390 xmlAttrPtr prop;
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006391
6392 prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0);
6393 if (prop == NULL)
Daniel Veillard2156d432004-03-04 15:59:36 +00006394 return(-1);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006395 xmlUnlinkNode((xmlNodePtr) prop);
6396 xmlFreeProp(prop);
6397 return(0);
Daniel Veillard2156d432004-03-04 15:59:36 +00006398}
6399#endif
6400
6401#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_HTML_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00006402/**
6403 * xmlSetProp:
6404 * @node: the node
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006405 * @name: the attribute name (a QName)
Owen Taylor3473f882001-02-23 17:55:21 +00006406 * @value: the attribute value
6407 *
6408 * Set (or reset) an attribute carried by a node.
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006409 * If @name has a prefix, then the corresponding
6410 * namespace-binding will be used, if in scope; it is an
6411 * error it there's no such ns-binding for the prefix in
6412 * scope.
Owen Taylor3473f882001-02-23 17:55:21 +00006413 * Returns the attribute pointer.
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006414 *
Owen Taylor3473f882001-02-23 17:55:21 +00006415 */
6416xmlAttrPtr
6417xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00006418 int len;
6419 const xmlChar *nqname;
Owen Taylor3473f882001-02-23 17:55:21 +00006420
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006421 if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE))
Owen Taylor3473f882001-02-23 17:55:21 +00006422 return(NULL);
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00006423
6424 /*
6425 * handle QNames
6426 */
6427 nqname = xmlSplitQName3(name, &len);
6428 if (nqname != NULL) {
6429 xmlNsPtr ns;
6430 xmlChar *prefix = xmlStrndup(name, len);
6431 ns = xmlSearchNs(node->doc, node, prefix);
6432 if (prefix != NULL)
6433 xmlFree(prefix);
6434 if (ns != NULL)
6435 return(xmlSetNsProp(node, ns, nqname, value));
6436 }
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006437 return(xmlSetNsProp(node, NULL, name, value));
Owen Taylor3473f882001-02-23 17:55:21 +00006438}
6439
6440/**
6441 * xmlSetNsProp:
6442 * @node: the node
6443 * @ns: the namespace definition
6444 * @name: the attribute name
6445 * @value: the attribute value
6446 *
6447 * Set (or reset) an attribute carried by a node.
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006448 * The ns structure must be in scope, this is not checked
Owen Taylor3473f882001-02-23 17:55:21 +00006449 *
6450 * Returns the attribute pointer.
6451 */
6452xmlAttrPtr
6453xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006454 const xmlChar *value)
6455{
Owen Taylor3473f882001-02-23 17:55:21 +00006456 xmlAttrPtr prop;
6457
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006458 if (ns && (ns->href == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00006459 return(NULL);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006460 prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0);
6461 if (prop != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00006462 /*
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006463 * Modify the attribute's value.
6464 */
6465 if (prop->atype == XML_ATTRIBUTE_ID) {
6466 xmlRemoveID(node->doc, prop);
6467 prop->atype = XML_ATTRIBUTE_ID;
6468 }
6469 if (prop->children != NULL)
6470 xmlFreeNodeList(prop->children);
6471 prop->children = NULL;
6472 prop->last = NULL;
6473 prop->ns = ns;
6474 if (value != NULL) {
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006475 xmlNodePtr tmp;
6476
Daniel Veillard6f8611f2008-02-15 08:33:21 +00006477 if(!xmlCheckUTF8(value)) {
6478 xmlTreeErr(XML_TREE_NOT_UTF8, (xmlNodePtr) node->doc,
6479 NULL);
6480 if (node->doc != NULL)
6481 node->doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
6482 }
6483 prop->children = xmlNewDocText(node->doc, value);
Owen Taylor3473f882001-02-23 17:55:21 +00006484 prop->last = NULL;
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006485 tmp = prop->children;
6486 while (tmp != NULL) {
6487 tmp->parent = (xmlNodePtr) prop;
6488 if (tmp->next == NULL)
6489 prop->last = tmp;
6490 tmp = tmp->next;
6491 }
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006492 }
6493 if (prop->atype == XML_ATTRIBUTE_ID)
6494 xmlAddID(NULL, node->doc, value, prop);
6495 return(prop);
Owen Taylor3473f882001-02-23 17:55:21 +00006496 }
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006497 /*
6498 * No equal attr found; create a new one.
6499 */
6500 return(xmlNewPropInternal(node, ns, name, value, 0));
Owen Taylor3473f882001-02-23 17:55:21 +00006501}
6502
Daniel Veillard652327a2003-09-29 18:02:38 +00006503#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard75bea542001-05-11 17:41:21 +00006504
6505/**
Owen Taylor3473f882001-02-23 17:55:21 +00006506 * xmlNodeIsText:
6507 * @node: the node
6508 *
6509 * Is this node a Text node ?
6510 * Returns 1 yes, 0 no
6511 */
6512int
6513xmlNodeIsText(xmlNodePtr node) {
6514 if (node == NULL) return(0);
6515
6516 if (node->type == XML_TEXT_NODE) return(1);
6517 return(0);
6518}
6519
6520/**
6521 * xmlIsBlankNode:
6522 * @node: the node
6523 *
6524 * Checks whether this node is an empty or whitespace only
6525 * (and possibly ignorable) text-node.
6526 *
6527 * Returns 1 yes, 0 no
6528 */
6529int
6530xmlIsBlankNode(xmlNodePtr node) {
6531 const xmlChar *cur;
6532 if (node == NULL) return(0);
6533
Daniel Veillard7db37732001-07-12 01:20:08 +00006534 if ((node->type != XML_TEXT_NODE) &&
6535 (node->type != XML_CDATA_SECTION_NODE))
6536 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006537 if (node->content == NULL) return(1);
Owen Taylor3473f882001-02-23 17:55:21 +00006538 cur = node->content;
Owen Taylor3473f882001-02-23 17:55:21 +00006539 while (*cur != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00006540 if (!IS_BLANK_CH(*cur)) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006541 cur++;
6542 }
6543
6544 return(1);
6545}
6546
6547/**
6548 * xmlTextConcat:
6549 * @node: the node
6550 * @content: the content
Daniel Veillard60087f32001-10-10 09:45:09 +00006551 * @len: @content length
Owen Taylor3473f882001-02-23 17:55:21 +00006552 *
6553 * Concat the given string at the end of the existing node content
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006554 *
6555 * Returns -1 in case of error, 0 otherwise
Owen Taylor3473f882001-02-23 17:55:21 +00006556 */
6557
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006558int
Owen Taylor3473f882001-02-23 17:55:21 +00006559xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006560 if (node == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006561
6562 if ((node->type != XML_TEXT_NODE) &&
Rob Richardsa02f1992006-09-16 14:04:26 +00006563 (node->type != XML_CDATA_SECTION_NODE) &&
6564 (node->type != XML_COMMENT_NODE) &&
6565 (node->type != XML_PI_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006566#ifdef DEBUG_TREE
6567 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006568 "xmlTextConcat: node is not text nor CDATA\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006569#endif
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006570 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006571 }
William M. Brack7762bb12004-01-04 14:49:01 +00006572 /* need to check if content is currently in the dictionary */
Daniel Veillard8874b942005-08-25 13:19:21 +00006573 if ((node->content == (xmlChar *) &(node->properties)) ||
6574 ((node->doc != NULL) && (node->doc->dict != NULL) &&
6575 xmlDictOwns(node->doc->dict, node->content))) {
William M. Brack7762bb12004-01-04 14:49:01 +00006576 node->content = xmlStrncatNew(node->content, content, len);
6577 } else {
6578 node->content = xmlStrncat(node->content, content, len);
6579 }
Daniel Veillard8874b942005-08-25 13:19:21 +00006580 node->properties = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006581 if (node->content == NULL)
6582 return(-1);
6583 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006584}
6585
6586/************************************************************************
6587 * *
6588 * Output : to a FILE or in memory *
6589 * *
6590 ************************************************************************/
6591
Owen Taylor3473f882001-02-23 17:55:21 +00006592/**
6593 * xmlBufferCreate:
6594 *
6595 * routine to create an XML buffer.
6596 * returns the new structure.
6597 */
6598xmlBufferPtr
6599xmlBufferCreate(void) {
6600 xmlBufferPtr ret;
6601
6602 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6603 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006604 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006605 return(NULL);
6606 }
6607 ret->use = 0;
Daniel Veillarde356c282001-03-10 12:32:04 +00006608 ret->size = xmlDefaultBufferSize;
Owen Taylor3473f882001-02-23 17:55:21 +00006609 ret->alloc = xmlBufferAllocScheme;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00006610 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00006611 if (ret->content == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006612 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006613 xmlFree(ret);
6614 return(NULL);
6615 }
6616 ret->content[0] = 0;
6617 return(ret);
6618}
6619
6620/**
6621 * xmlBufferCreateSize:
6622 * @size: initial size of buffer
6623 *
6624 * routine to create an XML buffer.
6625 * returns the new structure.
6626 */
6627xmlBufferPtr
6628xmlBufferCreateSize(size_t size) {
6629 xmlBufferPtr ret;
6630
6631 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6632 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006633 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006634 return(NULL);
6635 }
6636 ret->use = 0;
6637 ret->alloc = xmlBufferAllocScheme;
6638 ret->size = (size ? size+2 : 0); /* +1 for ending null */
6639 if (ret->size){
Daniel Veillard3c908dc2003-04-19 00:07:51 +00006640 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00006641 if (ret->content == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006642 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006643 xmlFree(ret);
6644 return(NULL);
6645 }
6646 ret->content[0] = 0;
6647 } else
6648 ret->content = NULL;
6649 return(ret);
6650}
6651
6652/**
Daniel Veillard53350552003-09-18 13:35:51 +00006653 * xmlBufferCreateStatic:
6654 * @mem: the memory area
6655 * @size: the size in byte
6656 *
MST 2003 John Flecka0e7e932003-12-19 03:13:47 +00006657 * routine to create an XML buffer from an immutable memory area.
6658 * The area won't be modified nor copied, and is expected to be
Daniel Veillard53350552003-09-18 13:35:51 +00006659 * present until the end of the buffer lifetime.
6660 *
6661 * returns the new structure.
6662 */
6663xmlBufferPtr
6664xmlBufferCreateStatic(void *mem, size_t size) {
6665 xmlBufferPtr ret;
6666
6667 if ((mem == NULL) || (size == 0))
6668 return(NULL);
6669
6670 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6671 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006672 xmlTreeErrMemory("creating buffer");
Daniel Veillard53350552003-09-18 13:35:51 +00006673 return(NULL);
6674 }
6675 ret->use = size;
6676 ret->size = size;
6677 ret->alloc = XML_BUFFER_ALLOC_IMMUTABLE;
6678 ret->content = (xmlChar *) mem;
6679 return(ret);
6680}
6681
6682/**
Owen Taylor3473f882001-02-23 17:55:21 +00006683 * xmlBufferSetAllocationScheme:
Daniel Veillardbd9afb52002-09-25 22:25:35 +00006684 * @buf: the buffer to tune
Owen Taylor3473f882001-02-23 17:55:21 +00006685 * @scheme: allocation scheme to use
6686 *
6687 * Sets the allocation scheme for this buffer
6688 */
6689void
6690xmlBufferSetAllocationScheme(xmlBufferPtr buf,
6691 xmlBufferAllocationScheme scheme) {
6692 if (buf == NULL) {
6693#ifdef DEBUG_BUFFER
6694 xmlGenericError(xmlGenericErrorContext,
6695 "xmlBufferSetAllocationScheme: buf == NULL\n");
6696#endif
6697 return;
6698 }
Daniel Veillard53350552003-09-18 13:35:51 +00006699 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00006700
6701 buf->alloc = scheme;
6702}
6703
6704/**
6705 * xmlBufferFree:
6706 * @buf: the buffer to free
6707 *
Daniel Veillard9d06d302002-01-22 18:15:52 +00006708 * Frees an XML buffer. It frees both the content and the structure which
6709 * encapsulate it.
Owen Taylor3473f882001-02-23 17:55:21 +00006710 */
6711void
6712xmlBufferFree(xmlBufferPtr buf) {
6713 if (buf == NULL) {
6714#ifdef DEBUG_BUFFER
6715 xmlGenericError(xmlGenericErrorContext,
6716 "xmlBufferFree: buf == NULL\n");
6717#endif
6718 return;
6719 }
Daniel Veillard53350552003-09-18 13:35:51 +00006720
6721 if ((buf->content != NULL) &&
6722 (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006723 xmlFree(buf->content);
6724 }
Owen Taylor3473f882001-02-23 17:55:21 +00006725 xmlFree(buf);
6726}
6727
6728/**
6729 * xmlBufferEmpty:
6730 * @buf: the buffer
6731 *
6732 * empty a buffer.
6733 */
6734void
6735xmlBufferEmpty(xmlBufferPtr buf) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00006736 if (buf == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +00006737 if (buf->content == NULL) return;
6738 buf->use = 0;
Daniel Veillard53350552003-09-18 13:35:51 +00006739 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00006740 buf->content = BAD_CAST "";
Daniel Veillard53350552003-09-18 13:35:51 +00006741 } else {
6742 memset(buf->content, 0, buf->size);
6743 }
Owen Taylor3473f882001-02-23 17:55:21 +00006744}
6745
6746/**
6747 * xmlBufferShrink:
6748 * @buf: the buffer to dump
6749 * @len: the number of xmlChar to remove
6750 *
6751 * Remove the beginning of an XML buffer.
6752 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006753 * Returns the number of #xmlChar removed, or -1 in case of failure.
Owen Taylor3473f882001-02-23 17:55:21 +00006754 */
6755int
6756xmlBufferShrink(xmlBufferPtr buf, unsigned int len) {
Daniel Veillard3d97e662004-11-04 10:49:00 +00006757 if (buf == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006758 if (len == 0) return(0);
6759 if (len > buf->use) return(-1);
6760
6761 buf->use -= len;
Daniel Veillard53350552003-09-18 13:35:51 +00006762 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) {
6763 buf->content += len;
6764 } else {
6765 memmove(buf->content, &buf->content[len], buf->use * sizeof(xmlChar));
6766 buf->content[buf->use] = 0;
6767 }
Owen Taylor3473f882001-02-23 17:55:21 +00006768 return(len);
6769}
6770
6771/**
6772 * xmlBufferGrow:
6773 * @buf: the buffer
6774 * @len: the minimum free size to allocate
6775 *
6776 * Grow the available space of an XML buffer.
6777 *
6778 * Returns the new available space or -1 in case of error
6779 */
6780int
6781xmlBufferGrow(xmlBufferPtr buf, unsigned int len) {
6782 int size;
6783 xmlChar *newbuf;
6784
Daniel Veillard3d97e662004-11-04 10:49:00 +00006785 if (buf == NULL) return(-1);
6786
Daniel Veillard53350552003-09-18 13:35:51 +00006787 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006788 if (len + buf->use < buf->size) return(0);
6789
William M. Brack30fe43f2004-07-26 18:00:58 +00006790/*
6791 * Windows has a BIG problem on realloc timing, so we try to double
6792 * the buffer size (if that's enough) (bug 146697)
6793 */
6794#ifdef WIN32
6795 if (buf->size > len)
6796 size = buf->size * 2;
6797 else
6798 size = buf->use + len + 100;
6799#else
Owen Taylor3473f882001-02-23 17:55:21 +00006800 size = buf->use + len + 100;
William M. Brack30fe43f2004-07-26 18:00:58 +00006801#endif
Owen Taylor3473f882001-02-23 17:55:21 +00006802
6803 newbuf = (xmlChar *) xmlRealloc(buf->content, size);
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006804 if (newbuf == NULL) {
6805 xmlTreeErrMemory("growing buffer");
6806 return(-1);
6807 }
Owen Taylor3473f882001-02-23 17:55:21 +00006808 buf->content = newbuf;
6809 buf->size = size;
6810 return(buf->size - buf->use);
6811}
6812
6813/**
6814 * xmlBufferDump:
6815 * @file: the file output
6816 * @buf: the buffer to dump
6817 *
6818 * Dumps an XML buffer to a FILE *.
Daniel Veillardd1640922001-12-17 15:30:10 +00006819 * Returns the number of #xmlChar written
Owen Taylor3473f882001-02-23 17:55:21 +00006820 */
6821int
6822xmlBufferDump(FILE *file, xmlBufferPtr buf) {
6823 int ret;
6824
6825 if (buf == NULL) {
6826#ifdef DEBUG_BUFFER
6827 xmlGenericError(xmlGenericErrorContext,
6828 "xmlBufferDump: buf == NULL\n");
6829#endif
6830 return(0);
6831 }
6832 if (buf->content == NULL) {
6833#ifdef DEBUG_BUFFER
6834 xmlGenericError(xmlGenericErrorContext,
6835 "xmlBufferDump: buf->content == NULL\n");
6836#endif
6837 return(0);
6838 }
Daniel Veillardcd337f02001-11-22 18:20:37 +00006839 if (file == NULL)
6840 file = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00006841 ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
6842 return(ret);
6843}
6844
6845/**
6846 * xmlBufferContent:
6847 * @buf: the buffer
6848 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00006849 * Function to extract the content of a buffer
6850 *
Owen Taylor3473f882001-02-23 17:55:21 +00006851 * Returns the internal content
6852 */
6853
Daniel Veillard5e2dace2001-07-18 19:30:27 +00006854const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00006855xmlBufferContent(const xmlBufferPtr buf)
6856{
6857 if(!buf)
6858 return NULL;
6859
6860 return buf->content;
6861}
6862
6863/**
6864 * xmlBufferLength:
6865 * @buf: the buffer
6866 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00006867 * Function to get the length of a buffer
6868 *
Owen Taylor3473f882001-02-23 17:55:21 +00006869 * Returns the length of data in the internal content
6870 */
6871
6872int
6873xmlBufferLength(const xmlBufferPtr buf)
6874{
6875 if(!buf)
6876 return 0;
6877
6878 return buf->use;
6879}
6880
6881/**
6882 * xmlBufferResize:
6883 * @buf: the buffer to resize
6884 * @size: the desired size
6885 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006886 * Resize a buffer to accommodate minimum size of @size.
Owen Taylor3473f882001-02-23 17:55:21 +00006887 *
6888 * Returns 0 in case of problems, 1 otherwise
6889 */
6890int
6891xmlBufferResize(xmlBufferPtr buf, unsigned int size)
6892{
6893 unsigned int newSize;
6894 xmlChar* rebuf = NULL;
6895
Daniel Veillardd005b9e2004-11-03 17:07:05 +00006896 if (buf == NULL)
6897 return(0);
6898
Daniel Veillard53350552003-09-18 13:35:51 +00006899 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
6900
Owen Taylor3473f882001-02-23 17:55:21 +00006901 /* Don't resize if we don't have to */
6902 if (size < buf->size)
6903 return 1;
6904
6905 /* figure out new size */
6906 switch (buf->alloc){
6907 case XML_BUFFER_ALLOC_DOUBLEIT:
Daniel Veillardbf629492004-04-20 22:20:59 +00006908 /*take care of empty case*/
6909 newSize = (buf->size ? buf->size*2 : size + 10);
Owen Taylor3473f882001-02-23 17:55:21 +00006910 while (size > newSize) newSize *= 2;
6911 break;
6912 case XML_BUFFER_ALLOC_EXACT:
6913 newSize = size+10;
6914 break;
6915 default:
6916 newSize = size+10;
6917 break;
6918 }
6919
6920 if (buf->content == NULL)
Daniel Veillard3c908dc2003-04-19 00:07:51 +00006921 rebuf = (xmlChar *) xmlMallocAtomic(newSize * sizeof(xmlChar));
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006922 else if (buf->size - buf->use < 100) {
Owen Taylor3473f882001-02-23 17:55:21 +00006923 rebuf = (xmlChar *) xmlRealloc(buf->content,
6924 newSize * sizeof(xmlChar));
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006925 } else {
6926 /*
6927 * if we are reallocating a buffer far from being full, it's
6928 * better to make a new allocation and copy only the used range
6929 * and free the old one.
6930 */
6931 rebuf = (xmlChar *) xmlMallocAtomic(newSize * sizeof(xmlChar));
6932 if (rebuf != NULL) {
6933 memcpy(rebuf, buf->content, buf->use);
6934 xmlFree(buf->content);
William M. Brack42331a92004-07-29 07:07:16 +00006935 rebuf[buf->use] = 0;
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006936 }
6937 }
Owen Taylor3473f882001-02-23 17:55:21 +00006938 if (rebuf == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006939 xmlTreeErrMemory("growing buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006940 return 0;
6941 }
6942 buf->content = rebuf;
6943 buf->size = newSize;
6944
6945 return 1;
6946}
6947
6948/**
6949 * xmlBufferAdd:
6950 * @buf: the buffer to dump
Daniel Veillardd1640922001-12-17 15:30:10 +00006951 * @str: the #xmlChar string
6952 * @len: the number of #xmlChar to add
Owen Taylor3473f882001-02-23 17:55:21 +00006953 *
Daniel Veillard60087f32001-10-10 09:45:09 +00006954 * Add a string range to an XML buffer. if len == -1, the length of
Owen Taylor3473f882001-02-23 17:55:21 +00006955 * str is recomputed.
William M. Bracka3215c72004-07-31 16:24:01 +00006956 *
6957 * Returns 0 successful, a positive error code number otherwise
6958 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00006959 */
William M. Bracka3215c72004-07-31 16:24:01 +00006960int
Owen Taylor3473f882001-02-23 17:55:21 +00006961xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) {
6962 unsigned int needSize;
6963
Daniel Veillardd005b9e2004-11-03 17:07:05 +00006964 if ((str == NULL) || (buf == NULL)) {
William M. Bracka3215c72004-07-31 16:24:01 +00006965 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006966 }
William M. Bracka3215c72004-07-31 16:24:01 +00006967 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006968 if (len < -1) {
6969#ifdef DEBUG_BUFFER
6970 xmlGenericError(xmlGenericErrorContext,
6971 "xmlBufferAdd: len < 0\n");
6972#endif
William M. Bracka3215c72004-07-31 16:24:01 +00006973 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006974 }
William M. Bracka3215c72004-07-31 16:24:01 +00006975 if (len == 0) return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006976
6977 if (len < 0)
6978 len = xmlStrlen(str);
6979
Daniel Veillardc9923322007-04-24 18:12:06 +00006980 if (len < 0) return -1;
6981 if (len == 0) return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006982
6983 needSize = buf->use + len + 2;
6984 if (needSize > buf->size){
6985 if (!xmlBufferResize(buf, needSize)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006986 xmlTreeErrMemory("growing buffer");
William M. Bracka3215c72004-07-31 16:24:01 +00006987 return XML_ERR_NO_MEMORY;
Owen Taylor3473f882001-02-23 17:55:21 +00006988 }
6989 }
6990
6991 memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
6992 buf->use += len;
6993 buf->content[buf->use] = 0;
William M. Bracka3215c72004-07-31 16:24:01 +00006994 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006995}
6996
6997/**
6998 * xmlBufferAddHead:
6999 * @buf: the buffer
Daniel Veillardd1640922001-12-17 15:30:10 +00007000 * @str: the #xmlChar string
7001 * @len: the number of #xmlChar to add
Owen Taylor3473f882001-02-23 17:55:21 +00007002 *
7003 * Add a string range to the beginning of an XML buffer.
Daniel Veillard60087f32001-10-10 09:45:09 +00007004 * if len == -1, the length of @str is recomputed.
William M. Bracka3215c72004-07-31 16:24:01 +00007005 *
7006 * Returns 0 successful, a positive error code number otherwise
7007 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00007008 */
William M. Bracka3215c72004-07-31 16:24:01 +00007009int
Owen Taylor3473f882001-02-23 17:55:21 +00007010xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) {
7011 unsigned int needSize;
7012
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007013 if (buf == NULL)
7014 return(-1);
William M. Bracka3215c72004-07-31 16:24:01 +00007015 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007016 if (str == NULL) {
7017#ifdef DEBUG_BUFFER
7018 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007019 "xmlBufferAddHead: str == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007020#endif
William M. Bracka3215c72004-07-31 16:24:01 +00007021 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007022 }
7023 if (len < -1) {
7024#ifdef DEBUG_BUFFER
7025 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007026 "xmlBufferAddHead: len < 0\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007027#endif
William M. Bracka3215c72004-07-31 16:24:01 +00007028 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007029 }
William M. Bracka3215c72004-07-31 16:24:01 +00007030 if (len == 0) return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007031
7032 if (len < 0)
7033 len = xmlStrlen(str);
7034
William M. Bracka3215c72004-07-31 16:24:01 +00007035 if (len <= 0) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007036
7037 needSize = buf->use + len + 2;
7038 if (needSize > buf->size){
7039 if (!xmlBufferResize(buf, needSize)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007040 xmlTreeErrMemory("growing buffer");
William M. Bracka3215c72004-07-31 16:24:01 +00007041 return XML_ERR_NO_MEMORY;
Owen Taylor3473f882001-02-23 17:55:21 +00007042 }
7043 }
7044
7045 memmove(&buf->content[len], &buf->content[0], buf->use * sizeof(xmlChar));
7046 memmove(&buf->content[0], str, len * sizeof(xmlChar));
7047 buf->use += len;
7048 buf->content[buf->use] = 0;
William M. Bracka3215c72004-07-31 16:24:01 +00007049 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007050}
7051
7052/**
7053 * xmlBufferCat:
William M. Bracka3215c72004-07-31 16:24:01 +00007054 * @buf: the buffer to add to
Daniel Veillardd1640922001-12-17 15:30:10 +00007055 * @str: the #xmlChar string
Owen Taylor3473f882001-02-23 17:55:21 +00007056 *
7057 * Append a zero terminated string to an XML buffer.
William M. Bracka3215c72004-07-31 16:24:01 +00007058 *
7059 * Returns 0 successful, a positive error code number otherwise
7060 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00007061 */
William M. Bracka3215c72004-07-31 16:24:01 +00007062int
Owen Taylor3473f882001-02-23 17:55:21 +00007063xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007064 if (buf == NULL)
7065 return(-1);
William M. Bracka3215c72004-07-31 16:24:01 +00007066 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7067 if (str == NULL) return -1;
7068 return xmlBufferAdd(buf, str, -1);
Owen Taylor3473f882001-02-23 17:55:21 +00007069}
7070
7071/**
7072 * xmlBufferCCat:
7073 * @buf: the buffer to dump
7074 * @str: the C char string
7075 *
7076 * Append a zero terminated C string to an XML buffer.
William M. Bracka3215c72004-07-31 16:24:01 +00007077 *
7078 * Returns 0 successful, a positive error code number otherwise
7079 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00007080 */
William M. Bracka3215c72004-07-31 16:24:01 +00007081int
Owen Taylor3473f882001-02-23 17:55:21 +00007082xmlBufferCCat(xmlBufferPtr buf, const char *str) {
7083 const char *cur;
7084
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007085 if (buf == NULL)
7086 return(-1);
William M. Bracka3215c72004-07-31 16:24:01 +00007087 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007088 if (str == NULL) {
7089#ifdef DEBUG_BUFFER
7090 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007091 "xmlBufferCCat: str == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007092#endif
William M. Bracka3215c72004-07-31 16:24:01 +00007093 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007094 }
7095 for (cur = str;*cur != 0;cur++) {
7096 if (buf->use + 10 >= buf->size) {
7097 if (!xmlBufferResize(buf, buf->use+10)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007098 xmlTreeErrMemory("growing buffer");
William M. Bracka3215c72004-07-31 16:24:01 +00007099 return XML_ERR_NO_MEMORY;
Owen Taylor3473f882001-02-23 17:55:21 +00007100 }
7101 }
7102 buf->content[buf->use++] = *cur;
7103 }
7104 buf->content[buf->use] = 0;
William M. Bracka3215c72004-07-31 16:24:01 +00007105 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007106}
7107
7108/**
7109 * xmlBufferWriteCHAR:
7110 * @buf: the XML buffer
7111 * @string: the string to add
7112 *
7113 * routine which manages and grows an output buffer. This one adds
7114 * xmlChars at the end of the buffer.
7115 */
7116void
Daniel Veillard53350552003-09-18 13:35:51 +00007117xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007118 if (buf == NULL)
7119 return;
Daniel Veillard53350552003-09-18 13:35:51 +00007120 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00007121 xmlBufferCat(buf, string);
7122}
7123
7124/**
7125 * xmlBufferWriteChar:
7126 * @buf: the XML buffer output
7127 * @string: the string to add
7128 *
7129 * routine which manage and grows an output buffer. This one add
7130 * C chars at the end of the array.
7131 */
7132void
7133xmlBufferWriteChar(xmlBufferPtr buf, const char *string) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007134 if (buf == NULL)
7135 return;
Daniel Veillard53350552003-09-18 13:35:51 +00007136 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00007137 xmlBufferCCat(buf, string);
7138}
7139
7140
7141/**
7142 * xmlBufferWriteQuotedString:
7143 * @buf: the XML buffer output
7144 * @string: the string to add
7145 *
7146 * routine which manage and grows an output buffer. This one writes
Daniel Veillardd1640922001-12-17 15:30:10 +00007147 * a quoted or double quoted #xmlChar string, checking first if it holds
Owen Taylor3473f882001-02-23 17:55:21 +00007148 * quote or double-quotes internally
7149 */
7150void
7151xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) {
Daniel Veillard39057f42003-08-04 01:33:43 +00007152 const xmlChar *cur, *base;
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007153 if (buf == NULL)
7154 return;
Daniel Veillard53350552003-09-18 13:35:51 +00007155 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Daniel Veillard39057f42003-08-04 01:33:43 +00007156 if (xmlStrchr(string, '\"')) {
Daniel Veillard20aa0fb2003-08-04 19:43:15 +00007157 if (xmlStrchr(string, '\'')) {
Owen Taylor3473f882001-02-23 17:55:21 +00007158#ifdef DEBUG_BUFFER
7159 xmlGenericError(xmlGenericErrorContext,
7160 "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n");
7161#endif
Daniel Veillard39057f42003-08-04 01:33:43 +00007162 xmlBufferCCat(buf, "\"");
7163 base = cur = string;
7164 while(*cur != 0){
7165 if(*cur == '"'){
7166 if (base != cur)
7167 xmlBufferAdd(buf, base, cur - base);
7168 xmlBufferAdd(buf, BAD_CAST "&quot;", 6);
7169 cur++;
7170 base = cur;
7171 }
7172 else {
7173 cur++;
7174 }
7175 }
7176 if (base != cur)
7177 xmlBufferAdd(buf, base, cur - base);
7178 xmlBufferCCat(buf, "\"");
Owen Taylor3473f882001-02-23 17:55:21 +00007179 }
Daniel Veillard39057f42003-08-04 01:33:43 +00007180 else{
7181 xmlBufferCCat(buf, "\'");
7182 xmlBufferCat(buf, string);
7183 xmlBufferCCat(buf, "\'");
7184 }
Owen Taylor3473f882001-02-23 17:55:21 +00007185 } else {
7186 xmlBufferCCat(buf, "\"");
7187 xmlBufferCat(buf, string);
7188 xmlBufferCCat(buf, "\"");
7189 }
7190}
7191
7192
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007193/**
7194 * xmlGetDocCompressMode:
7195 * @doc: the document
7196 *
7197 * get the compression ratio for a document, ZLIB based
7198 * Returns 0 (uncompressed) to 9 (max compression)
7199 */
7200int
7201xmlGetDocCompressMode (xmlDocPtr doc) {
7202 if (doc == NULL) return(-1);
7203 return(doc->compression);
7204}
7205
7206/**
7207 * xmlSetDocCompressMode:
7208 * @doc: the document
7209 * @mode: the compression ratio
7210 *
7211 * set the compression ratio for a document, ZLIB based
7212 * Correct values: 0 (uncompressed) to 9 (max compression)
7213 */
7214void
7215xmlSetDocCompressMode (xmlDocPtr doc, int mode) {
7216 if (doc == NULL) return;
7217 if (mode < 0) doc->compression = 0;
7218 else if (mode > 9) doc->compression = 9;
7219 else doc->compression = mode;
7220}
7221
7222/**
7223 * xmlGetCompressMode:
7224 *
7225 * get the default compression mode used, ZLIB based.
7226 * Returns 0 (uncompressed) to 9 (max compression)
7227 */
7228int
7229xmlGetCompressMode(void)
7230{
7231 return (xmlCompressMode);
7232}
7233
7234/**
7235 * xmlSetCompressMode:
7236 * @mode: the compression ratio
7237 *
7238 * set the default compression mode used, ZLIB based
7239 * Correct values: 0 (uncompressed) to 9 (max compression)
7240 */
7241void
7242xmlSetCompressMode(int mode) {
7243 if (mode < 0) xmlCompressMode = 0;
7244 else if (mode > 9) xmlCompressMode = 9;
7245 else xmlCompressMode = mode;
7246}
7247
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007248#define XML_TREE_NSMAP_PARENT -1
7249#define XML_TREE_NSMAP_XML -2
7250#define XML_TREE_NSMAP_DOC -3
7251#define XML_TREE_NSMAP_CUSTOM -4
7252
7253typedef struct xmlNsMapItem *xmlNsMapItemPtr;
7254struct xmlNsMapItem {
7255 xmlNsMapItemPtr next;
7256 xmlNsMapItemPtr prev;
7257 xmlNsPtr oldNs; /* old ns decl reference */
7258 xmlNsPtr newNs; /* new ns decl reference */
7259 int shadowDepth; /* Shadowed at this depth */
7260 /*
7261 * depth:
7262 * >= 0 == @node's ns-decls
7263 * -1 == @parent's ns-decls
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00007264 * -2 == the doc->oldNs XML ns-decl
7265 * -3 == the doc->oldNs storage ns-decls
7266 * -4 == ns-decls provided via custom ns-handling
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007267 */
7268 int depth;
7269};
7270
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007271typedef struct xmlNsMap *xmlNsMapPtr;
7272struct xmlNsMap {
7273 xmlNsMapItemPtr first;
7274 xmlNsMapItemPtr last;
7275 xmlNsMapItemPtr pool;
7276};
7277
7278#define XML_NSMAP_NOTEMPTY(m) (((m) != NULL) && ((m)->first != NULL))
7279#define XML_NSMAP_FOREACH(m, i) for (i = (m)->first; i != NULL; i = (i)->next)
7280#define XML_NSMAP_POP(m, i) \
7281 i = (m)->last; \
7282 (m)->last = (i)->prev; \
7283 if ((m)->last == NULL) \
7284 (m)->first = NULL; \
7285 else \
7286 (m)->last->next = NULL; \
7287 (i)->next = (m)->pool; \
7288 (m)->pool = i;
7289
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007290/*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007291* xmlDOMWrapNsMapFree:
7292* @map: the ns-map
7293*
7294* Frees the ns-map
7295*/
7296static void
7297xmlDOMWrapNsMapFree(xmlNsMapPtr nsmap)
7298{
7299 xmlNsMapItemPtr cur, tmp;
7300
7301 if (nsmap == NULL)
7302 return;
7303 cur = nsmap->pool;
7304 while (cur != NULL) {
7305 tmp = cur;
7306 cur = cur->next;
7307 xmlFree(tmp);
7308 }
7309 cur = nsmap->first;
7310 while (cur != NULL) {
7311 tmp = cur;
7312 cur = cur->next;
7313 xmlFree(tmp);
7314 }
7315 xmlFree(nsmap);
7316}
7317
7318/*
7319* xmlDOMWrapNsMapAddItem:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007320* @map: the ns-map
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007321* @oldNs: the old ns-struct
7322* @newNs: the new ns-struct
7323* @depth: depth and ns-kind information
7324*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007325* Adds an ns-mapping item.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007326*/
7327static xmlNsMapItemPtr
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00007328xmlDOMWrapNsMapAddItem(xmlNsMapPtr *nsmap, int position,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007329 xmlNsPtr oldNs, xmlNsPtr newNs, int depth)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007330{
7331 xmlNsMapItemPtr ret;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007332 xmlNsMapPtr map;
7333
7334 if (nsmap == NULL)
7335 return(NULL);
7336 if ((position != -1) && (position != 0))
7337 return(NULL);
7338 map = *nsmap;
7339
7340 if (map == NULL) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007341 /*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007342 * Create the ns-map.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007343 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007344 map = (xmlNsMapPtr) xmlMalloc(sizeof(struct xmlNsMap));
7345 if (map == NULL) {
7346 xmlTreeErrMemory("allocating namespace map");
7347 return (NULL);
7348 }
7349 memset(map, 0, sizeof(struct xmlNsMap));
7350 *nsmap = map;
7351 }
7352
7353 if (map->pool != NULL) {
7354 /*
7355 * Reuse an item from the pool.
7356 */
7357 ret = map->pool;
7358 map->pool = ret->next;
7359 memset(ret, 0, sizeof(struct xmlNsMapItem));
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007360 } else {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007361 /*
7362 * Create a new item.
7363 */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007364 ret = (xmlNsMapItemPtr) xmlMalloc(sizeof(struct xmlNsMapItem));
7365 if (ret == NULL) {
7366 xmlTreeErrMemory("allocating namespace map item");
7367 return (NULL);
7368 }
7369 memset(ret, 0, sizeof(struct xmlNsMapItem));
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007370 }
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007371
7372 if (map->first == NULL) {
7373 /*
7374 * First ever.
7375 */
7376 map->first = ret;
7377 map->last = ret;
7378 } else if (position == -1) {
7379 /*
7380 * Append.
7381 */
7382 ret->prev = map->last;
7383 map->last->next = ret;
7384 map->last = ret;
7385 } else if (position == 0) {
7386 /*
7387 * Set on first position.
7388 */
7389 map->first->prev = ret;
7390 ret->next = map->first;
7391 map->first = ret;
7392 } else
7393 return(NULL);
7394
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007395 ret->oldNs = oldNs;
7396 ret->newNs = newNs;
7397 ret->shadowDepth = -1;
7398 ret->depth = depth;
7399 return (ret);
7400}
7401
7402/*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007403* xmlDOMWrapStoreNs:
7404* @doc: the doc
7405* @nsName: the namespace name
7406* @prefix: the prefix
7407*
7408* Creates or reuses an xmlNs struct on doc->oldNs with
7409* the given prefix and namespace name.
7410*
7411* Returns the aquired ns struct or NULL in case of an API
7412* or internal error.
7413*/
7414static xmlNsPtr
7415xmlDOMWrapStoreNs(xmlDocPtr doc,
7416 const xmlChar *nsName,
7417 const xmlChar *prefix)
7418{
7419 xmlNsPtr ns;
7420
7421 if (doc == NULL)
7422 return (NULL);
7423 ns = xmlTreeEnsureXMLDecl(doc);
7424 if (ns == NULL)
7425 return (NULL);
7426 if (ns->next != NULL) {
7427 /* Reuse. */
7428 ns = ns->next;
7429 while (ns != NULL) {
7430 if (((ns->prefix == prefix) ||
7431 xmlStrEqual(ns->prefix, prefix)) &&
7432 xmlStrEqual(ns->href, nsName)) {
7433 return (ns);
7434 }
7435 if (ns->next == NULL)
7436 break;
7437 ns = ns->next;
7438 }
7439 }
7440 /* Create. */
7441 ns->next = xmlNewNs(NULL, nsName, prefix);
7442 return (ns->next);
7443}
7444
7445/*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00007446* xmlDOMWrapNewCtxt:
7447*
7448* Allocates and initializes a new DOM-wrapper context.
7449*
7450* Returns the xmlDOMWrapCtxtPtr or NULL in case of an internal errror.
7451*/
7452xmlDOMWrapCtxtPtr
7453xmlDOMWrapNewCtxt(void)
7454{
7455 xmlDOMWrapCtxtPtr ret;
7456
7457 ret = xmlMalloc(sizeof(xmlDOMWrapCtxt));
7458 if (ret == NULL) {
7459 xmlTreeErrMemory("allocating DOM-wrapper context");
7460 return (NULL);
7461 }
7462 memset(ret, 0, sizeof(xmlDOMWrapCtxt));
7463 return (ret);
7464}
7465
7466/*
7467* xmlDOMWrapFreeCtxt:
7468* @ctxt: the DOM-wrapper context
7469*
7470* Frees the DOM-wrapper context.
7471*/
7472void
7473xmlDOMWrapFreeCtxt(xmlDOMWrapCtxtPtr ctxt)
7474{
7475 if (ctxt == NULL)
7476 return;
7477 if (ctxt->namespaceMap != NULL)
7478 xmlDOMWrapNsMapFree((xmlNsMapPtr) ctxt->namespaceMap);
7479 /*
7480 * TODO: Store the namespace map in the context.
7481 */
7482 xmlFree(ctxt);
7483}
7484
7485/*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007486* xmlTreeLookupNsListByPrefix:
7487* @nsList: a list of ns-structs
7488* @prefix: the searched prefix
7489*
7490* Searches for a ns-decl with the given prefix in @nsList.
7491*
7492* Returns the ns-decl if found, NULL if not found and on
7493* API errors.
7494*/
7495static xmlNsPtr
7496xmlTreeNSListLookupByPrefix(xmlNsPtr nsList, const xmlChar *prefix)
7497{
7498 if (nsList == NULL)
7499 return (NULL);
7500 {
7501 xmlNsPtr ns;
7502 ns = nsList;
7503 do {
7504 if ((prefix == ns->prefix) ||
7505 xmlStrEqual(prefix, ns->prefix)) {
7506 return (ns);
7507 }
7508 ns = ns->next;
7509 } while (ns != NULL);
7510 }
7511 return (NULL);
7512}
7513
7514/*
7515*
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00007516* xmlDOMWrapNSNormGatherInScopeNs:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007517* @map: the namespace map
7518* @node: the node to start with
7519*
7520* Puts in-scope namespaces into the ns-map.
7521*
7522* Returns 0 on success, -1 on API or internal errors.
7523*/
7524static int
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007525xmlDOMWrapNSNormGatherInScopeNs(xmlNsMapPtr *map,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007526 xmlNodePtr node)
7527{
7528 xmlNodePtr cur;
7529 xmlNsPtr ns;
7530 xmlNsMapItemPtr mi;
7531 int shadowed;
7532
7533 if ((map == NULL) || (*map != NULL))
7534 return (-1);
7535 /*
7536 * Get in-scope ns-decls of @parent.
7537 */
7538 cur = node;
7539 while ((cur != NULL) && (cur != (xmlNodePtr) cur->doc)) {
7540 if (cur->type == XML_ELEMENT_NODE) {
7541 if (cur->nsDef != NULL) {
7542 ns = cur->nsDef;
7543 do {
7544 shadowed = 0;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007545 if (XML_NSMAP_NOTEMPTY(*map)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007546 /*
7547 * Skip shadowed prefixes.
7548 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007549 XML_NSMAP_FOREACH(*map, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007550 if ((ns->prefix == mi->newNs->prefix) ||
7551 xmlStrEqual(ns->prefix, mi->newNs->prefix)) {
7552 shadowed = 1;
7553 break;
7554 }
7555 }
7556 }
7557 /*
7558 * Insert mapping.
7559 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007560 mi = xmlDOMWrapNsMapAddItem(map, 0, NULL,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007561 ns, XML_TREE_NSMAP_PARENT);
7562 if (mi == NULL)
7563 return (-1);
7564 if (shadowed)
7565 mi->shadowDepth = 0;
7566 ns = ns->next;
7567 } while (ns != NULL);
7568 }
7569 }
7570 cur = cur->parent;
7571 }
7572 return (0);
7573}
7574
7575/*
7576* XML_TREE_ADOPT_STR: If we have a dest-dict, put @str in the dict;
7577* otherwise copy it, when it was in the source-dict.
7578*/
7579#define XML_TREE_ADOPT_STR(str) \
7580 if (adoptStr && (str != NULL)) { \
7581 if (destDoc->dict) { \
Daniel Veillard7e21fd12005-07-03 21:44:07 +00007582 const xmlChar *old = str; \
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007583 str = xmlDictLookup(destDoc->dict, str, -1); \
Daniel Veillard7e21fd12005-07-03 21:44:07 +00007584 if ((sourceDoc == NULL) || (sourceDoc->dict == NULL) || \
7585 (!xmlDictOwns(sourceDoc->dict, old))) \
Daniel Veillard39e5c892005-07-03 22:48:50 +00007586 xmlFree((char *)old); \
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007587 } else if ((sourceDoc) && (sourceDoc->dict) && \
7588 xmlDictOwns(sourceDoc->dict, str)) { \
7589 str = BAD_CAST xmlStrdup(str); \
7590 } \
7591 }
7592
7593/*
7594* XML_TREE_ADOPT_STR_2: If @str was in the source-dict, then
7595* put it in dest-dict or copy it.
7596*/
7597#define XML_TREE_ADOPT_STR_2(str) \
7598 if (adoptStr && (str != NULL) && (sourceDoc != NULL) && \
7599 (sourceDoc->dict != NULL) && \
7600 xmlDictOwns(sourceDoc->dict, cur->content)) { \
7601 if (destDoc->dict) \
7602 cur->content = (xmlChar *) \
7603 xmlDictLookup(destDoc->dict, cur->content, -1); \
7604 else \
7605 cur->content = xmlStrdup(BAD_CAST cur->content); \
7606 }
7607
7608/*
7609* xmlDOMWrapNSNormAddNsMapItem2:
7610*
7611* For internal use. Adds a ns-decl mapping.
7612*
7613* Returns 0 on success, -1 on internal errors.
7614*/
7615static int
7616xmlDOMWrapNSNormAddNsMapItem2(xmlNsPtr **list, int *size, int *number,
7617 xmlNsPtr oldNs, xmlNsPtr newNs)
7618{
7619 if (*list == NULL) {
7620 *list = (xmlNsPtr *) xmlMalloc(6 * sizeof(xmlNsPtr));
7621 if (*list == NULL) {
7622 xmlTreeErrMemory("alloc ns map item");
7623 return(-1);
7624 }
7625 *size = 3;
7626 *number = 0;
7627 } else if ((*number) >= (*size)) {
7628 *size *= 2;
7629 *list = (xmlNsPtr *) xmlRealloc(*list,
7630 (*size) * 2 * sizeof(xmlNsPtr));
7631 if (*list == NULL) {
7632 xmlTreeErrMemory("realloc ns map item");
7633 return(-1);
7634 }
7635 }
7636 (*list)[2 * (*number)] = oldNs;
7637 (*list)[2 * (*number) +1] = newNs;
7638 (*number)++;
7639 return (0);
7640}
7641
7642/*
7643* xmlDOMWrapRemoveNode:
Daniel Veillard304e78c2005-07-03 16:19:41 +00007644* @ctxt: a DOM wrapper context
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007645* @doc: the doc
7646* @node: the node to be removed.
Daniel Veillard304e78c2005-07-03 16:19:41 +00007647* @options: set of options, unused at the moment
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007648*
7649* Unlinks the given node from its owner.
7650* This will substitute ns-references to node->nsDef for
7651* ns-references to doc->oldNs, thus ensuring the removed
7652* branch to be autark wrt ns-references.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00007653*
7654* NOTE: This function was not intensively tested.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007655*
7656* Returns 0 on success, 1 if the node is not supported,
7657* -1 on API and internal errors.
7658*/
7659int
7660xmlDOMWrapRemoveNode(xmlDOMWrapCtxtPtr ctxt, xmlDocPtr doc,
7661 xmlNodePtr node, int options ATTRIBUTE_UNUSED)
7662{
7663 xmlNsPtr *list = NULL;
7664 int sizeList, nbList, i, j;
7665 xmlNsPtr ns;
7666
7667 if ((node == NULL) || (doc == NULL) || (node->doc != doc))
7668 return (-1);
7669
7670 /* TODO: 0 or -1 ? */
7671 if (node->parent == NULL)
7672 return (0);
7673
7674 switch (node->type) {
7675 case XML_TEXT_NODE:
7676 case XML_CDATA_SECTION_NODE:
7677 case XML_ENTITY_REF_NODE:
7678 case XML_PI_NODE:
7679 case XML_COMMENT_NODE:
7680 xmlUnlinkNode(node);
7681 return (0);
7682 case XML_ELEMENT_NODE:
7683 case XML_ATTRIBUTE_NODE:
7684 break;
7685 default:
7686 return (1);
7687 }
7688 xmlUnlinkNode(node);
7689 /*
7690 * Save out-of-scope ns-references in doc->oldNs.
7691 */
7692 do {
7693 switch (node->type) {
7694 case XML_ELEMENT_NODE:
7695 if ((ctxt == NULL) && (node->nsDef != NULL)) {
7696 ns = node->nsDef;
7697 do {
7698 if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
7699 &nbList, ns, ns) == -1)
7700 goto internal_error;
7701 ns = ns->next;
7702 } while (ns != NULL);
7703 }
7704 /* No break on purpose. */
7705 case XML_ATTRIBUTE_NODE:
7706 if (node->ns != NULL) {
7707 /*
7708 * Find a mapping.
7709 */
7710 if (list != NULL) {
7711 for (i = 0, j = 0; i < nbList; i++, j += 2) {
7712 if (node->ns == list[j]) {
7713 node->ns = list[++j];
7714 goto next_node;
7715 }
7716 }
7717 }
7718 ns = NULL;
7719 if (ctxt != NULL) {
7720 /*
7721 * User defined.
7722 */
7723 } else {
7724 /*
7725 * Add to doc's oldNs.
7726 */
7727 ns = xmlDOMWrapStoreNs(doc, node->ns->href,
7728 node->ns->prefix);
7729 if (ns == NULL)
7730 goto internal_error;
7731 }
7732 if (ns != NULL) {
7733 /*
7734 * Add mapping.
7735 */
7736 if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
7737 &nbList, node->ns, ns) == -1)
7738 goto internal_error;
7739 }
7740 node->ns = ns;
7741 }
7742 if ((node->type == XML_ELEMENT_NODE) &&
7743 (node->properties != NULL)) {
7744 node = (xmlNodePtr) node->properties;
7745 continue;
7746 }
7747 break;
7748 default:
7749 goto next_sibling;
7750 }
7751next_node:
7752 if ((node->type == XML_ELEMENT_NODE) &&
7753 (node->children != NULL)) {
7754 node = node->children;
7755 continue;
7756 }
7757next_sibling:
7758 if (node == NULL)
7759 break;
7760 if (node->next != NULL)
7761 node = node->next;
7762 else {
7763 node = node->parent;
7764 goto next_sibling;
7765 }
7766 } while (node != NULL);
7767
7768 if (list != NULL)
7769 xmlFree(list);
7770 return (0);
7771
7772internal_error:
7773 if (list != NULL)
7774 xmlFree(list);
7775 return (-1);
7776}
7777
7778/*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007779* xmlSearchNsByNamespaceStrict:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007780* @doc: the document
7781* @node: the start node
7782* @nsName: the searched namespace name
7783* @retNs: the resulting ns-decl
7784* @prefixed: if the found ns-decl must have a prefix (for attributes)
7785*
7786* Dynamically searches for a ns-declaration which matches
7787* the given @nsName in the ancestor-or-self axis of @node.
7788*
7789* Returns 1 if a ns-decl was found, 0 if not and -1 on API
7790* and internal errors.
7791*/
7792static int
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007793xmlSearchNsByNamespaceStrict(xmlDocPtr doc, xmlNodePtr node,
7794 const xmlChar* nsName,
7795 xmlNsPtr *retNs, int prefixed)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007796{
7797 xmlNodePtr cur, prev = NULL, out = NULL;
7798 xmlNsPtr ns, prevns;
7799
7800 if ((doc == NULL) || (nsName == NULL) || (retNs == NULL))
7801 return (-1);
7802
7803 *retNs = NULL;
7804 if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) {
7805 *retNs = xmlTreeEnsureXMLDecl(doc);
7806 if (*retNs == NULL)
7807 return (-1);
7808 return (1);
7809 }
7810 cur = node;
7811 do {
7812 if (cur->type == XML_ELEMENT_NODE) {
7813 if (cur->nsDef != NULL) {
7814 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
7815 if (prefixed && (ns->prefix == NULL))
7816 continue;
7817 if (prev != NULL) {
7818 /*
7819 * Check the last level of ns-decls for a
7820 * shadowing prefix.
7821 */
7822 prevns = prev->nsDef;
7823 do {
7824 if ((prevns->prefix == ns->prefix) ||
7825 ((prevns->prefix != NULL) &&
7826 (ns->prefix != NULL) &&
7827 xmlStrEqual(prevns->prefix, ns->prefix))) {
7828 /*
7829 * Shadowed.
7830 */
7831 break;
7832 }
7833 prevns = prevns->next;
7834 } while (prevns != NULL);
7835 if (prevns != NULL)
7836 continue;
7837 }
7838 /*
7839 * Ns-name comparison.
7840 */
7841 if ((nsName == ns->href) ||
7842 xmlStrEqual(nsName, ns->href)) {
7843 /*
7844 * At this point the prefix can only be shadowed,
7845 * if we are the the (at least) 3rd level of
7846 * ns-decls.
7847 */
7848 if (out) {
7849 int ret;
7850
7851 ret = xmlNsInScope(doc, node, prev, ns->prefix);
7852 if (ret < 0)
7853 return (-1);
7854 /*
7855 * TODO: Should we try to find a matching ns-name
7856 * only once? This here keeps on searching.
7857 * I think we should try further since, there might
7858 * be an other matching ns-decl with an unshadowed
7859 * prefix.
7860 */
7861 if (! ret)
7862 continue;
7863 }
7864 *retNs = ns;
7865 return (1);
7866 }
7867 }
7868 out = prev;
7869 prev = cur;
7870 }
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007871 } else if ((cur->type == XML_ENTITY_NODE) ||
7872 (cur->type == XML_ENTITY_DECL))
7873 return (0);
7874 cur = cur->parent;
7875 } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
7876 return (0);
7877}
7878
7879/*
7880* xmlSearchNsByPrefixStrict:
7881* @doc: the document
7882* @node: the start node
7883* @prefix: the searched namespace prefix
7884* @retNs: the resulting ns-decl
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007885*
7886* Dynamically searches for a ns-declaration which matches
7887* the given @nsName in the ancestor-or-self axis of @node.
7888*
7889* Returns 1 if a ns-decl was found, 0 if not and -1 on API
7890* and internal errors.
7891*/
7892static int
7893xmlSearchNsByPrefixStrict(xmlDocPtr doc, xmlNodePtr node,
7894 const xmlChar* prefix,
7895 xmlNsPtr *retNs)
7896{
7897 xmlNodePtr cur;
7898 xmlNsPtr ns;
7899
7900 if ((doc == NULL) || (node == NULL))
7901 return (-1);
7902
7903 if (retNs)
7904 *retNs = NULL;
7905 if (IS_STR_XML(prefix)) {
7906 if (retNs) {
7907 *retNs = xmlTreeEnsureXMLDecl(doc);
7908 if (*retNs == NULL)
7909 return (-1);
7910 }
7911 return (1);
7912 }
7913 cur = node;
7914 do {
7915 if (cur->type == XML_ELEMENT_NODE) {
7916 if (cur->nsDef != NULL) {
7917 ns = cur->nsDef;
7918 do {
7919 if ((prefix == ns->prefix) ||
7920 xmlStrEqual(prefix, ns->prefix))
7921 {
7922 /*
7923 * Disabled namespaces, e.g. xmlns:abc="".
7924 */
7925 if (ns->href == NULL)
7926 return(0);
7927 if (retNs)
7928 *retNs = ns;
7929 return (1);
7930 }
7931 ns = ns->next;
7932 } while (ns != NULL);
7933 }
7934 } else if ((cur->type == XML_ENTITY_NODE) ||
7935 (cur->type == XML_ENTITY_DECL))
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007936 return (0);
7937 cur = cur->parent;
7938 } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
7939 return (0);
7940}
7941
7942/*
7943* xmlDOMWrapNSNormDeclareNsForced:
7944* @doc: the doc
7945* @elem: the element-node to declare on
7946* @nsName: the namespace-name of the ns-decl
7947* @prefix: the preferred prefix of the ns-decl
7948* @checkShadow: ensure that the new ns-decl doesn't shadow ancestor ns-decls
7949*
7950* Declares a new namespace on @elem. It tries to use the
7951* given @prefix; if a ns-decl with the given prefix is already existent
7952* on @elem, it will generate an other prefix.
7953*
7954* Returns 1 if a ns-decl was found, 0 if not and -1 on API
7955* and internal errors.
7956*/
7957static xmlNsPtr
7958xmlDOMWrapNSNormDeclareNsForced(xmlDocPtr doc,
7959 xmlNodePtr elem,
7960 const xmlChar *nsName,
7961 const xmlChar *prefix,
7962 int checkShadow)
7963{
7964
7965 xmlNsPtr ret;
7966 char buf[50];
7967 const xmlChar *pref;
7968 int counter = 0;
7969 /*
7970 * Create a ns-decl on @anchor.
7971 */
7972 pref = prefix;
7973 while (1) {
7974 /*
7975 * Lookup whether the prefix is unused in elem's ns-decls.
7976 */
7977 if ((elem->nsDef != NULL) &&
7978 (xmlTreeNSListLookupByPrefix(elem->nsDef, pref) != NULL))
7979 goto ns_next_prefix;
7980 if (checkShadow && elem->parent &&
7981 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
7982 /*
7983 * Does it shadow ancestor ns-decls?
7984 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007985 if (xmlSearchNsByPrefixStrict(doc, elem->parent, pref, NULL) == 1)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007986 goto ns_next_prefix;
7987 }
7988 ret = xmlNewNs(NULL, nsName, pref);
7989 if (ret == NULL)
7990 return (NULL);
7991 if (elem->nsDef == NULL)
7992 elem->nsDef = ret;
7993 else {
7994 xmlNsPtr ns2 = elem->nsDef;
7995 while (ns2->next != NULL)
7996 ns2 = ns2->next;
7997 ns2->next = ret;
7998 }
7999 return (ret);
8000ns_next_prefix:
8001 counter++;
8002 if (counter > 1000)
8003 return (NULL);
8004 if (prefix == NULL) {
8005 snprintf((char *) buf, sizeof(buf),
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008006 "ns_%d", counter);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008007 } else
8008 snprintf((char *) buf, sizeof(buf),
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008009 "%.30s_%d", (char *)prefix, counter);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008010 pref = BAD_CAST buf;
8011 }
8012}
8013
8014/*
8015* xmlDOMWrapNSNormAquireNormalizedNs:
8016* @doc: the doc
8017* @elem: the element-node to declare namespaces on
8018* @ns: the ns-struct to use for the search
8019* @retNs: the found/created ns-struct
8020* @nsMap: the ns-map
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008021* @depth: the current tree depth
8022* @ancestorsOnly: search in ancestor ns-decls only
8023* @prefixed: if the searched ns-decl must have a prefix (for attributes)
8024*
8025* Searches for a matching ns-name in the ns-decls of @nsMap, if not
8026* found it will either declare it on @elem, or store it in doc->oldNs.
8027* If a new ns-decl needs to be declared on @elem, it tries to use the
8028* @ns->prefix for it, if this prefix is already in use on @elem, it will
8029* change the prefix or the new ns-decl.
8030*
8031* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8032*/
8033static int
8034xmlDOMWrapNSNormAquireNormalizedNs(xmlDocPtr doc,
8035 xmlNodePtr elem,
8036 xmlNsPtr ns,
8037 xmlNsPtr *retNs,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008038 xmlNsMapPtr *nsMap,
8039
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008040 int depth,
8041 int ancestorsOnly,
8042 int prefixed)
8043{
8044 xmlNsMapItemPtr mi;
8045
8046 if ((doc == NULL) || (ns == NULL) || (retNs == NULL) ||
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008047 (nsMap == NULL))
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008048 return (-1);
8049
8050 *retNs = NULL;
8051 /*
8052 * Handle XML namespace.
8053 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008054 if (IS_STR_XML(ns->prefix)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008055 /*
8056 * Insert XML namespace mapping.
8057 */
8058 *retNs = xmlTreeEnsureXMLDecl(doc);
8059 if (*retNs == NULL)
8060 return (-1);
8061 return (0);
8062 }
8063 /*
8064 * If the search should be done in ancestors only and no
8065 * @elem (the first ancestor) was specified, then skip the search.
8066 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008067 if ((XML_NSMAP_NOTEMPTY(*nsMap)) &&
8068 (! (ancestorsOnly && (elem == NULL))))
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008069 {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008070 /*
8071 * Try to find an equal ns-name in in-scope ns-decls.
8072 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008073 XML_NSMAP_FOREACH(*nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008074 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8075 /*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008076 * ancestorsOnly: This should be turned on to gain speed,
8077 * if one knows that the branch itself was already
8078 * ns-wellformed and no stale references existed.
8079 * I.e. it searches in the ancestor axis only.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008080 */
8081 ((! ancestorsOnly) || (mi->depth == XML_TREE_NSMAP_PARENT)) &&
8082 /* Skip shadowed prefixes. */
8083 (mi->shadowDepth == -1) &&
8084 /* Skip xmlns="" or xmlns:foo="". */
8085 ((mi->newNs->href != NULL) &&
8086 (mi->newNs->href[0] != 0)) &&
8087 /* Ensure a prefix if wanted. */
8088 ((! prefixed) || (mi->newNs->prefix != NULL)) &&
8089 /* Equal ns name */
8090 ((mi->newNs->href == ns->href) ||
8091 xmlStrEqual(mi->newNs->href, ns->href))) {
8092 /* Set the mapping. */
8093 mi->oldNs = ns;
8094 *retNs = mi->newNs;
8095 return (0);
8096 }
8097 }
8098 }
8099 /*
8100 * No luck, the namespace is out of scope or shadowed.
8101 */
8102 if (elem == NULL) {
8103 xmlNsPtr tmpns;
8104
8105 /*
8106 * Store ns-decls in "oldNs" of the document-node.
8107 */
8108 tmpns = xmlDOMWrapStoreNs(doc, ns->href, ns->prefix);
8109 if (tmpns == NULL)
8110 return (-1);
8111 /*
8112 * Insert mapping.
8113 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008114 if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008115 tmpns, XML_TREE_NSMAP_DOC) == NULL) {
8116 xmlFreeNs(tmpns);
8117 return (-1);
8118 }
8119 *retNs = tmpns;
8120 } else {
8121 xmlNsPtr tmpns;
8122
8123 tmpns = xmlDOMWrapNSNormDeclareNsForced(doc, elem, ns->href,
8124 ns->prefix, 0);
8125 if (tmpns == NULL)
8126 return (-1);
8127
8128 if (*nsMap != NULL) {
8129 /*
8130 * Does it shadow ancestor ns-decls?
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008131 */
8132 XML_NSMAP_FOREACH(*nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008133 if ((mi->depth < depth) &&
8134 (mi->shadowDepth == -1) &&
8135 ((ns->prefix == mi->newNs->prefix) ||
8136 xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
8137 /*
8138 * Shadows.
8139 */
8140 mi->shadowDepth = depth;
8141 break;
8142 }
8143 }
8144 }
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008145 if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns, tmpns, depth) == NULL) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008146 xmlFreeNs(tmpns);
8147 return (-1);
8148 }
8149 *retNs = tmpns;
8150 }
8151 return (0);
8152}
8153
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008154typedef enum {
8155 XML_DOM_RECONNS_REMOVEREDUND = 1<<0
8156} xmlDOMReconcileNSOptions;
8157
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008158/*
8159* xmlDOMWrapReconcileNamespaces:
Daniel Veillard304e78c2005-07-03 16:19:41 +00008160* @ctxt: DOM wrapper context, unused at the moment
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008161* @elem: the element-node
8162* @options: option flags
8163*
8164* Ensures that ns-references point to ns-decls hold on element-nodes.
8165* Ensures that the tree is namespace wellformed by creating additional
8166* ns-decls where needed. Note that, since prefixes of already existent
8167* ns-decls can be shadowed by this process, it could break QNames in
8168* attribute values or element content.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008169*
8170* NOTE: This function was not intensively tested.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008171*
8172* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008173*/
8174
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008175int
8176xmlDOMWrapReconcileNamespaces(xmlDOMWrapCtxtPtr ctxt ATTRIBUTE_UNUSED,
8177 xmlNodePtr elem,
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008178 int options)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008179{
8180 int depth = -1, adoptns = 0, parnsdone = 0;
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008181 xmlNsPtr ns, prevns;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008182 xmlDocPtr doc;
8183 xmlNodePtr cur, curElem = NULL;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008184 xmlNsMapPtr nsMap = NULL;
8185 xmlNsMapItemPtr /* topmi = NULL, */ mi;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008186 /* @ancestorsOnly should be set by an option flag. */
8187 int ancestorsOnly = 0;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008188 int optRemoveRedundantNS =
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008189 ((xmlDOMReconcileNSOptions) options & XML_DOM_RECONNS_REMOVEREDUND) ? 1 : 0;
8190 xmlNsPtr *listRedund = NULL;
8191 int sizeRedund = 0, nbRedund = 0, ret, i, j;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008192
8193 if ((elem == NULL) || (elem->doc == NULL) ||
8194 (elem->type != XML_ELEMENT_NODE))
8195 return (-1);
8196
8197 doc = elem->doc;
8198 cur = elem;
8199 do {
8200 switch (cur->type) {
8201 case XML_ELEMENT_NODE:
8202 adoptns = 1;
8203 curElem = cur;
8204 depth++;
8205 /*
8206 * Namespace declarations.
8207 */
8208 if (cur->nsDef != NULL) {
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008209 prevns = NULL;
Kasimier T. Buchcike8f8d752006-02-02 12:13:07 +00008210 ns = cur->nsDef;
8211 while (ns != NULL) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008212 if (! parnsdone) {
8213 if ((elem->parent) &&
8214 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8215 /*
8216 * Gather ancestor in-scope ns-decls.
8217 */
8218 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8219 elem->parent) == -1)
8220 goto internal_error;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008221 }
8222 parnsdone = 1;
8223 }
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008224
8225 /*
8226 * Lookup the ns ancestor-axis for equal ns-decls in scope.
8227 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008228 if (optRemoveRedundantNS && XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008229 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008230 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8231 (mi->shadowDepth == -1) &&
8232 ((ns->prefix == mi->newNs->prefix) ||
8233 xmlStrEqual(ns->prefix, mi->newNs->prefix)) &&
8234 ((ns->href == mi->newNs->href) ||
8235 xmlStrEqual(ns->href, mi->newNs->href)))
8236 {
8237 /*
8238 * A redundant ns-decl was found.
8239 * Add it to the list of redundant ns-decls.
8240 */
8241 if (xmlDOMWrapNSNormAddNsMapItem2(&listRedund,
8242 &sizeRedund, &nbRedund, ns, mi->newNs) == -1)
8243 goto internal_error;
8244 /*
8245 * Remove the ns-decl from the element-node.
Kasimier T. Buchcike8f8d752006-02-02 12:13:07 +00008246 */
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008247 if (prevns)
8248 prevns->next = ns->next;
8249 else
Kasimier T. Buchcike8f8d752006-02-02 12:13:07 +00008250 cur->nsDef = ns->next;
8251 goto next_ns_decl;
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008252 }
8253 }
8254 }
8255
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008256 /*
8257 * Skip ns-references handling if the referenced
8258 * ns-decl is declared on the same element.
8259 */
8260 if ((cur->ns != NULL) && adoptns && (cur->ns == ns))
8261 adoptns = 0;
8262 /*
8263 * Does it shadow any ns-decl?
8264 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008265 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8266 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008267 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8268 (mi->shadowDepth == -1) &&
8269 ((ns->prefix == mi->newNs->prefix) ||
8270 xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
8271
8272 mi->shadowDepth = depth;
8273 }
8274 }
8275 }
8276 /*
8277 * Push mapping.
8278 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008279 if (xmlDOMWrapNsMapAddItem(&nsMap, -1, ns, ns,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008280 depth) == NULL)
Kasimier T. Buchcike8f8d752006-02-02 12:13:07 +00008281 goto internal_error;
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008282
8283 prevns = ns;
Kasimier T. Buchcike8f8d752006-02-02 12:13:07 +00008284next_ns_decl:
8285 ns = ns->next;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008286 }
8287 }
8288 if (! adoptns)
8289 goto ns_end;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008290 /* No break on purpose. */
8291 case XML_ATTRIBUTE_NODE:
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008292 /* No ns, no fun. */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008293 if (cur->ns == NULL)
8294 goto ns_end;
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008295
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008296 if (! parnsdone) {
8297 if ((elem->parent) &&
8298 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8299 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8300 elem->parent) == -1)
8301 goto internal_error;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008302 }
8303 parnsdone = 1;
8304 }
8305 /*
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008306 * Adjust the reference if this was a redundant ns-decl.
8307 */
8308 if (listRedund) {
8309 for (i = 0, j = 0; i < nbRedund; i++, j += 2) {
8310 if (cur->ns == listRedund[j]) {
8311 cur->ns = listRedund[++j];
8312 break;
8313 }
8314 }
8315 }
8316 /*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008317 * Adopt ns-references.
8318 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008319 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008320 /*
8321 * Search for a mapping.
8322 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008323 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008324 if ((mi->shadowDepth == -1) &&
8325 (cur->ns == mi->oldNs)) {
8326
8327 cur->ns = mi->newNs;
8328 goto ns_end;
8329 }
8330 }
8331 }
8332 /*
8333 * Aquire a normalized ns-decl and add it to the map.
8334 */
8335 if (xmlDOMWrapNSNormAquireNormalizedNs(doc, curElem,
8336 cur->ns, &ns,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008337 &nsMap, depth,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008338 ancestorsOnly,
8339 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
8340 goto internal_error;
8341 cur->ns = ns;
8342
8343ns_end:
8344 if ((cur->type == XML_ELEMENT_NODE) &&
8345 (cur->properties != NULL)) {
8346 /*
8347 * Process attributes.
8348 */
8349 cur = (xmlNodePtr) cur->properties;
8350 continue;
8351 }
8352 break;
8353 default:
8354 goto next_sibling;
8355 }
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008356into_content:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008357 if ((cur->type == XML_ELEMENT_NODE) &&
8358 (cur->children != NULL)) {
8359 /*
8360 * Process content of element-nodes only.
8361 */
8362 cur = cur->children;
8363 continue;
8364 }
8365next_sibling:
8366 if (cur == elem)
8367 break;
8368 if (cur->type == XML_ELEMENT_NODE) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008369 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008370 /*
8371 * Pop mappings.
8372 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008373 while ((nsMap->last != NULL) &&
8374 (nsMap->last->depth >= depth))
8375 {
8376 XML_NSMAP_POP(nsMap, mi)
8377 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008378 /*
8379 * Unshadow.
8380 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008381 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008382 if (mi->shadowDepth >= depth)
8383 mi->shadowDepth = -1;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008384 }
8385 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008386 depth--;
8387 }
8388 if (cur->next != NULL)
8389 cur = cur->next;
8390 else {
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008391 if (cur->type == XML_ATTRIBUTE_NODE) {
8392 cur = cur->parent;
8393 goto into_content;
8394 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008395 cur = cur->parent;
8396 goto next_sibling;
8397 }
8398 } while (cur != NULL);
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008399
8400 ret = 0;
8401 goto exit;
8402internal_error:
8403 ret = -1;
8404exit:
Kasimier T. Buchcike8f8d752006-02-02 12:13:07 +00008405 if (listRedund) {
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008406 for (i = 0, j = 0; i < nbRedund; i++, j += 2) {
8407 xmlFreeNs(listRedund[j]);
8408 }
8409 xmlFree(listRedund);
8410 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008411 if (nsMap != NULL)
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008412 xmlDOMWrapNsMapFree(nsMap);
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008413 return (ret);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008414}
8415
8416/*
8417* xmlDOMWrapAdoptBranch:
8418* @ctxt: the optional context for custom processing
8419* @sourceDoc: the optional sourceDoc
8420* @node: the element-node to start with
8421* @destDoc: the destination doc for adoption
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008422* @destParent: the optional new parent of @node in @destDoc
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008423* @options: option flags
8424*
8425* Ensures that ns-references point to @destDoc: either to
8426* elements->nsDef entries if @destParent is given, or to
8427* @destDoc->oldNs otherwise.
8428* If @destParent is given, it ensures that the tree is namespace
8429* wellformed by creating additional ns-decls where needed.
8430* Note that, since prefixes of already existent ns-decls can be
8431* shadowed by this process, it could break QNames in attribute
8432* values or element content.
8433*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008434* NOTE: This function was not intensively tested.
8435*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008436* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8437*/
8438static int
8439xmlDOMWrapAdoptBranch(xmlDOMWrapCtxtPtr ctxt,
8440 xmlDocPtr sourceDoc,
8441 xmlNodePtr node,
8442 xmlDocPtr destDoc,
8443 xmlNodePtr destParent,
8444 int options ATTRIBUTE_UNUSED)
8445{
8446 int ret = 0;
8447 xmlNodePtr cur, curElem = NULL;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008448 xmlNsMapPtr nsMap = NULL;
8449 xmlNsMapItemPtr mi;
Daniel Veillard11ce4002006-03-10 00:36:23 +00008450 xmlNsPtr ns = NULL;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008451 int depth = -1, adoptStr = 1;
8452 /* gather @parent's ns-decls. */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008453 int parnsdone;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008454 /* @ancestorsOnly should be set per option. */
8455 int ancestorsOnly = 0;
8456
8457 /*
8458 * Optimize string adoption for equal or none dicts.
8459 */
8460 if ((sourceDoc != NULL) &&
8461 (sourceDoc->dict == destDoc->dict))
8462 adoptStr = 0;
8463 else
8464 adoptStr = 1;
8465
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008466 /*
8467 * Get the ns-map from the context if available.
8468 */
8469 if (ctxt)
8470 nsMap = (xmlNsMapPtr) ctxt->namespaceMap;
8471 /*
8472 * Disable search for ns-decls in the parent-axis of the
8473 * desination element, if:
8474 * 1) there's no destination parent
8475 * 2) custom ns-reference handling is used
8476 */
8477 if ((destParent == NULL) ||
8478 (ctxt && ctxt->getNsForNodeFunc))
8479 {
8480 parnsdone = 1;
8481 } else
8482 parnsdone = 0;
8483
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008484 cur = node;
8485 while (cur != NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008486 /*
8487 * Paranoid source-doc sanity check.
8488 */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008489 if (cur->doc != sourceDoc) {
8490 /*
8491 * We'll assume XIncluded nodes if the doc differs.
8492 * TODO: Do we need to reconciliate XIncluded nodes?
8493 * This here skips XIncluded nodes and tries to handle
8494 * broken sequences.
8495 */
8496 if (cur->next == NULL)
8497 goto leave_node;
8498 do {
8499 cur = cur->next;
8500 if ((cur->type == XML_XINCLUDE_END) ||
8501 (cur->doc == node->doc))
8502 break;
8503 } while (cur->next != NULL);
8504
8505 if (cur->doc != node->doc)
8506 goto leave_node;
8507 }
8508 cur->doc = destDoc;
8509 switch (cur->type) {
8510 case XML_XINCLUDE_START:
8511 case XML_XINCLUDE_END:
8512 /*
8513 * TODO
8514 */
8515 return (-1);
8516 case XML_ELEMENT_NODE:
8517 curElem = cur;
8518 depth++;
8519 /*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008520 * Namespace declarations.
8521 * - ns->href and ns->prefix are never in the dict, so
8522 * we need not move the values over to the destination dict.
8523 * - Note that for custom handling of ns-references,
8524 * the ns-decls need not be stored in the ns-map,
8525 * since they won't be referenced by node->ns.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008526 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008527 if ((cur->nsDef) &&
8528 ((ctxt == NULL) || (ctxt->getNsForNodeFunc == NULL)))
8529 {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008530 if (! parnsdone) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008531 /*
8532 * Gather @parent's in-scope ns-decls.
8533 */
8534 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8535 destParent) == -1)
8536 goto internal_error;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008537 parnsdone = 1;
8538 }
8539 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
8540 /*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008541 * NOTE: ns->prefix and ns->href are never in the dict.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008542 * XML_TREE_ADOPT_STR(ns->prefix)
8543 * XML_TREE_ADOPT_STR(ns->href)
8544 */
8545 /*
8546 * Does it shadow any ns-decl?
8547 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008548 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008549 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008550 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8551 (mi->shadowDepth == -1) &&
8552 ((ns->prefix == mi->newNs->prefix) ||
8553 xmlStrEqual(ns->prefix,
8554 mi->newNs->prefix))) {
8555
8556 mi->shadowDepth = depth;
8557 }
8558 }
8559 }
8560 /*
8561 * Push mapping.
8562 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008563 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008564 ns, ns, depth) == NULL)
8565 goto internal_error;
8566 }
8567 }
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008568 /* No break on purpose. */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008569 case XML_ATTRIBUTE_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008570 /* No namespace, no fun. */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008571 if (cur->ns == NULL)
8572 goto ns_end;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008573
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008574 if (! parnsdone) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008575 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8576 destParent) == -1)
8577 goto internal_error;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008578 parnsdone = 1;
8579 }
8580 /*
8581 * Adopt ns-references.
8582 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008583 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008584 /*
8585 * Search for a mapping.
8586 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008587 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008588 if ((mi->shadowDepth == -1) &&
8589 (cur->ns == mi->oldNs)) {
8590
8591 cur->ns = mi->newNs;
8592 goto ns_end;
8593 }
8594 }
8595 }
8596 /*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008597 * No matching namespace in scope. We need a new one.
8598 */
8599 if ((ctxt) && (ctxt->getNsForNodeFunc)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008600 /*
8601 * User-defined behaviour.
8602 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008603 ns = ctxt->getNsForNodeFunc(ctxt, cur,
8604 cur->ns->href, cur->ns->prefix);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008605 /*
8606 * Insert mapping if ns is available; it's the users fault
8607 * if not.
8608 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008609 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008610 cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008611 goto internal_error;
8612 cur->ns = ns;
8613 } else {
8614 /*
8615 * Aquire a normalized ns-decl and add it to the map.
8616 */
8617 if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc,
8618 /* ns-decls on curElem or on destDoc->oldNs */
8619 destParent ? curElem : NULL,
8620 cur->ns, &ns,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008621 &nsMap, depth,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008622 ancestorsOnly,
8623 /* ns-decls must be prefixed for attributes. */
8624 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
8625 goto internal_error;
8626 cur->ns = ns;
8627 }
8628ns_end:
8629 /*
8630 * Further node properties.
8631 * TODO: Is this all?
8632 */
8633 XML_TREE_ADOPT_STR(cur->name)
8634 if (cur->type == XML_ELEMENT_NODE) {
8635 cur->psvi = NULL;
8636 cur->line = 0;
8637 cur->extra = 0;
8638 /*
8639 * Walk attributes.
8640 */
8641 if (cur->properties != NULL) {
8642 /*
8643 * Process first attribute node.
8644 */
8645 cur = (xmlNodePtr) cur->properties;
8646 continue;
8647 }
8648 } else {
8649 /*
8650 * Attributes.
8651 */
8652 if ((sourceDoc != NULL) &&
8653 (((xmlAttrPtr) cur)->atype == XML_ATTRIBUTE_ID))
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008654 {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008655 xmlRemoveID(sourceDoc, (xmlAttrPtr) cur);
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008656 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008657 ((xmlAttrPtr) cur)->atype = 0;
8658 ((xmlAttrPtr) cur)->psvi = NULL;
8659 }
8660 break;
8661 case XML_TEXT_NODE:
8662 case XML_CDATA_SECTION_NODE:
8663 /*
8664 * This puts the content in the dest dict, only if
8665 * it was previously in the source dict.
8666 */
8667 XML_TREE_ADOPT_STR_2(cur->content)
8668 goto leave_node;
8669 case XML_ENTITY_REF_NODE:
8670 /*
8671 * Remove reference to the entitity-node.
8672 */
8673 cur->content = NULL;
8674 cur->children = NULL;
8675 cur->last = NULL;
8676 if ((destDoc->intSubset) || (destDoc->extSubset)) {
8677 xmlEntityPtr ent;
8678 /*
8679 * Assign new entity-node if available.
8680 */
8681 ent = xmlGetDocEntity(destDoc, cur->name);
8682 if (ent != NULL) {
8683 cur->content = ent->content;
8684 cur->children = (xmlNodePtr) ent;
8685 cur->last = (xmlNodePtr) ent;
8686 }
8687 }
8688 goto leave_node;
8689 case XML_PI_NODE:
8690 XML_TREE_ADOPT_STR(cur->name)
8691 XML_TREE_ADOPT_STR_2(cur->content)
8692 break;
8693 case XML_COMMENT_NODE:
8694 break;
8695 default:
8696 goto internal_error;
8697 }
8698 /*
8699 * Walk the tree.
8700 */
8701 if (cur->children != NULL) {
8702 cur = cur->children;
8703 continue;
8704 }
8705
8706leave_node:
8707 if (cur == node)
8708 break;
8709 if ((cur->type == XML_ELEMENT_NODE) ||
8710 (cur->type == XML_XINCLUDE_START) ||
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008711 (cur->type == XML_XINCLUDE_END))
8712 {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008713 /*
8714 * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
8715 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008716 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008717 /*
8718 * Pop mappings.
8719 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008720 while ((nsMap->last != NULL) &&
8721 (nsMap->last->depth >= depth))
8722 {
8723 XML_NSMAP_POP(nsMap, mi)
8724 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008725 /*
8726 * Unshadow.
8727 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008728 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008729 if (mi->shadowDepth >= depth)
8730 mi->shadowDepth = -1;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008731 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008732 }
8733 depth--;
8734 }
8735 if (cur->next != NULL)
8736 cur = cur->next;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008737 else if ((cur->type == XML_ATTRIBUTE_NODE) &&
8738 (cur->parent->children != NULL))
8739 {
8740 cur = cur->parent->children;
8741 } else {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008742 cur = cur->parent;
8743 goto leave_node;
8744 }
8745 }
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008746
8747 goto exit;
8748
8749internal_error:
8750 ret = -1;
8751
8752exit:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008753 /*
8754 * Cleanup.
8755 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008756 if (nsMap != NULL) {
8757 if ((ctxt) && (ctxt->namespaceMap == nsMap)) {
8758 /*
8759 * Just cleanup the map but don't free.
8760 */
8761 if (nsMap->first) {
8762 if (nsMap->pool)
8763 nsMap->last->next = nsMap->pool;
8764 nsMap->pool = nsMap->first;
8765 nsMap->first = NULL;
8766 }
8767 } else
8768 xmlDOMWrapNsMapFree(nsMap);
8769 }
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008770 return(ret);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008771}
8772
8773/*
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008774* xmlDOMWrapCloneNode:
8775* @ctxt: the optional context for custom processing
8776* @sourceDoc: the optional sourceDoc
8777* @node: the node to start with
8778* @resNode: the clone of the given @node
8779* @destDoc: the destination doc
8780* @destParent: the optional new parent of @node in @destDoc
Daniel Veillardb2f8f1d2006-04-28 16:30:48 +00008781* @deep: descend into child if set
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008782* @options: option flags
8783*
8784* References of out-of scope ns-decls are remapped to point to @destDoc:
8785* 1) If @destParent is given, then nsDef entries on element-nodes are used
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008786* 2) If *no* @destParent is given, then @destDoc->oldNs entries are used.
8787* This is the case when you don't know already where the cloned branch
8788* will be added to.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008789*
8790* If @destParent is given, it ensures that the tree is namespace
8791* wellformed by creating additional ns-decls where needed.
8792* Note that, since prefixes of already existent ns-decls can be
8793* shadowed by this process, it could break QNames in attribute
8794* values or element content.
8795* TODO:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008796* 1) What to do with XInclude? Currently this returns an error for XInclude.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008797*
8798* Returns 0 if the operation succeeded,
8799* 1 if a node of unsupported (or not yet supported) type was given,
8800* -1 on API/internal errors.
8801*/
8802
8803int
8804xmlDOMWrapCloneNode(xmlDOMWrapCtxtPtr ctxt,
8805 xmlDocPtr sourceDoc,
8806 xmlNodePtr node,
8807 xmlNodePtr *resNode,
8808 xmlDocPtr destDoc,
8809 xmlNodePtr destParent,
8810 int deep,
8811 int options ATTRIBUTE_UNUSED)
8812{
8813 int ret = 0;
8814 xmlNodePtr cur, curElem = NULL;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008815 xmlNsMapPtr nsMap = NULL;
8816 xmlNsMapItemPtr mi;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008817 xmlNsPtr ns;
8818 int depth = -1;
8819 /* int adoptStr = 1; */
8820 /* gather @parent's ns-decls. */
8821 int parnsdone = 0;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008822 /*
8823 * @ancestorsOnly:
8824 * TODO: @ancestorsOnly should be set per option.
8825 *
8826 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008827 int ancestorsOnly = 0;
8828 xmlNodePtr resultClone = NULL, clone = NULL, parentClone = NULL, prevClone = NULL;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008829 xmlNsPtr cloneNs = NULL, *cloneNsDefSlot = NULL;
8830 xmlDictPtr dict; /* The destination dict */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008831
Daniel Veillard11ce4002006-03-10 00:36:23 +00008832 if ((node == NULL) || (resNode == NULL) || (destDoc == NULL))
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008833 return(-1);
8834 /*
8835 * TODO: Initially we support only element-nodes.
8836 */
8837 if (node->type != XML_ELEMENT_NODE)
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008838 return(1);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008839 /*
8840 * Check node->doc sanity.
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008841 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008842 if ((node->doc != NULL) && (sourceDoc != NULL) &&
8843 (node->doc != sourceDoc)) {
8844 /*
8845 * Might be an XIncluded node.
8846 */
8847 return (-1);
8848 }
8849 if (sourceDoc == NULL)
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008850 sourceDoc = node->doc;
Daniel Veillard11ce4002006-03-10 00:36:23 +00008851 if (sourceDoc == NULL)
8852 return (-1);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008853
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008854 dict = destDoc->dict;
8855 /*
8856 * Reuse the namespace map of the context.
8857 */
8858 if (ctxt)
8859 nsMap = (xmlNsMapPtr) ctxt->namespaceMap;
8860
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008861 *resNode = NULL;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008862
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008863 cur = node;
8864 while (cur != NULL) {
8865 if (cur->doc != sourceDoc) {
8866 /*
8867 * We'll assume XIncluded nodes if the doc differs.
8868 * TODO: Do we need to reconciliate XIncluded nodes?
8869 * TODO: This here returns -1 in this case.
8870 */
8871 goto internal_error;
8872 }
8873 /*
8874 * Create a new node.
8875 */
8876 switch (cur->type) {
8877 case XML_XINCLUDE_START:
8878 case XML_XINCLUDE_END:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008879 /*
8880 * TODO: What to do with XInclude?
8881 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008882 goto internal_error;
8883 break;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008884 case XML_ELEMENT_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008885 case XML_TEXT_NODE:
8886 case XML_CDATA_SECTION_NODE:
8887 case XML_COMMENT_NODE:
8888 case XML_PI_NODE:
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008889 case XML_DOCUMENT_FRAG_NODE:
8890 case XML_ENTITY_REF_NODE:
8891 case XML_ENTITY_NODE:
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008892 /*
8893 * Nodes of xmlNode structure.
8894 */
8895 clone = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
8896 if (clone == NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008897 xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating a node");
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008898 goto internal_error;
8899 }
8900 memset(clone, 0, sizeof(xmlNode));
8901 /*
8902 * Set hierachical links.
8903 */
8904 if (resultClone != NULL) {
8905 clone->parent = parentClone;
8906 if (prevClone) {
8907 prevClone->next = clone;
8908 clone->prev = prevClone;
8909 } else
8910 parentClone->children = clone;
8911 } else
8912 resultClone = clone;
8913
8914 break;
8915 case XML_ATTRIBUTE_NODE:
8916 /*
8917 * Attributes (xmlAttr).
8918 */
8919 clone = (xmlNodePtr) xmlMalloc(sizeof(xmlAttr));
8920 if (clone == NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008921 xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating an attr-node");
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008922 goto internal_error;
8923 }
8924 memset(clone, 0, sizeof(xmlAttr));
8925 /*
8926 * Set hierachical links.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008927 * TODO: Change this to add to the end of attributes.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008928 */
8929 if (resultClone != NULL) {
8930 clone->parent = parentClone;
8931 if (prevClone) {
8932 prevClone->next = clone;
8933 clone->prev = prevClone;
8934 } else
8935 parentClone->properties = (xmlAttrPtr) clone;
8936 } else
8937 resultClone = clone;
8938 break;
8939 default:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008940 /*
8941 * TODO QUESTION: Any other nodes expected?
8942 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008943 goto internal_error;
8944 }
8945
8946 clone->type = cur->type;
8947 clone->doc = destDoc;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008948
8949 /*
8950 * Clone the name of the node if any.
8951 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008952 if (cur->name == xmlStringText)
8953 clone->name = xmlStringText;
8954 else if (cur->name == xmlStringTextNoenc)
8955 /*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008956 * NOTE: Although xmlStringTextNoenc is never assigned to a node
8957 * in tree.c, it might be set in Libxslt via
8958 * "xsl:disable-output-escaping".
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008959 */
8960 clone->name = xmlStringTextNoenc;
8961 else if (cur->name == xmlStringComment)
8962 clone->name = xmlStringComment;
8963 else if (cur->name != NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008964 DICT_CONST_COPY(cur->name, clone->name);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008965 }
8966
8967 switch (cur->type) {
8968 case XML_XINCLUDE_START:
8969 case XML_XINCLUDE_END:
8970 /*
8971 * TODO
8972 */
8973 return (-1);
8974 case XML_ELEMENT_NODE:
8975 curElem = cur;
8976 depth++;
8977 /*
8978 * Namespace declarations.
8979 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008980 if (cur->nsDef != NULL) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008981 if (! parnsdone) {
8982 if (destParent && (ctxt == NULL)) {
8983 /*
8984 * Gather @parent's in-scope ns-decls.
8985 */
8986 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8987 destParent) == -1)
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008988 goto internal_error;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008989 }
8990 parnsdone = 1;
8991 }
8992 /*
8993 * Clone namespace declarations.
8994 */
8995 cloneNsDefSlot = &(clone->nsDef);
8996 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
8997 /*
8998 * Create a new xmlNs.
8999 */
9000 cloneNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
9001 if (cloneNs == NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009002 xmlTreeErrMemory("xmlDOMWrapCloneNode(): "
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009003 "allocating namespace");
9004 return(-1);
9005 }
9006 memset(cloneNs, 0, sizeof(xmlNs));
9007 cloneNs->type = XML_LOCAL_NAMESPACE;
9008
9009 if (ns->href != NULL)
9010 cloneNs->href = xmlStrdup(ns->href);
9011 if (ns->prefix != NULL)
9012 cloneNs->prefix = xmlStrdup(ns->prefix);
9013
9014 *cloneNsDefSlot = cloneNs;
9015 cloneNsDefSlot = &(cloneNs->next);
9016
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009017 /*
9018 * Note that for custom handling of ns-references,
9019 * the ns-decls need not be stored in the ns-map,
9020 * since they won't be referenced by node->ns.
9021 */
9022 if ((ctxt == NULL) ||
9023 (ctxt->getNsForNodeFunc == NULL))
9024 {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009025 /*
9026 * Does it shadow any ns-decl?
9027 */
9028 if (XML_NSMAP_NOTEMPTY(nsMap)) {
9029 XML_NSMAP_FOREACH(nsMap, mi) {
9030 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
9031 (mi->shadowDepth == -1) &&
9032 ((ns->prefix == mi->newNs->prefix) ||
9033 xmlStrEqual(ns->prefix,
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009034 mi->newNs->prefix))) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009035 /*
9036 * Mark as shadowed at the current
9037 * depth.
9038 */
9039 mi->shadowDepth = depth;
9040 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009041 }
9042 }
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009043 /*
9044 * Push mapping.
9045 */
9046 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
9047 ns, cloneNs, depth) == NULL)
9048 goto internal_error;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009049 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009050 }
9051 }
9052 /* cur->ns will be processed further down. */
9053 break;
9054 case XML_ATTRIBUTE_NODE:
9055 /* IDs will be processed further down. */
9056 /* cur->ns will be processed further down. */
9057 break;
9058 case XML_TEXT_NODE:
9059 case XML_CDATA_SECTION_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009060 /*
9061 * Note that this will also cover the values of attributes.
9062 */
9063 DICT_COPY(cur->content, clone->content);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009064 goto leave_node;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009065 case XML_ENTITY_NODE:
9066 /* TODO: What to do here? */
9067 goto leave_node;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009068 case XML_ENTITY_REF_NODE:
9069 if (sourceDoc != destDoc) {
9070 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9071 xmlEntityPtr ent;
9072 /*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009073 * Different doc: Assign new entity-node if available.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009074 */
9075 ent = xmlGetDocEntity(destDoc, cur->name);
9076 if (ent != NULL) {
9077 clone->content = ent->content;
9078 clone->children = (xmlNodePtr) ent;
9079 clone->last = (xmlNodePtr) ent;
9080 }
9081 }
9082 } else {
9083 /*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009084 * Same doc: Use the current node's entity declaration
9085 * and value.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009086 */
9087 clone->content = cur->content;
9088 clone->children = cur->children;
9089 clone->last = cur->last;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009090 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009091 goto leave_node;
9092 case XML_PI_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009093 DICT_COPY(cur->content, clone->content);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009094 goto leave_node;
9095 case XML_COMMENT_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009096 DICT_COPY(cur->content, clone->content);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009097 goto leave_node;
9098 default:
9099 goto internal_error;
9100 }
9101
9102 if (cur->ns == NULL)
9103 goto end_ns_reference;
9104
9105/* handle_ns_reference: */
9106 /*
9107 ** The following will take care of references to ns-decls ********
9108 ** and is intended only for element- and attribute-nodes.
9109 **
9110 */
9111 if (! parnsdone) {
9112 if (destParent && (ctxt == NULL)) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009113 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, destParent) == -1)
9114 goto internal_error;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009115 }
9116 parnsdone = 1;
9117 }
9118 /*
9119 * Adopt ns-references.
9120 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009121 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009122 /*
9123 * Search for a mapping.
9124 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009125 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009126 if ((mi->shadowDepth == -1) &&
9127 (cur->ns == mi->oldNs)) {
9128 /*
9129 * This is the nice case: a mapping was found.
9130 */
9131 clone->ns = mi->newNs;
9132 goto end_ns_reference;
9133 }
9134 }
9135 }
9136 /*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009137 * No matching namespace in scope. We need a new one.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009138 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009139 if ((ctxt != NULL) && (ctxt->getNsForNodeFunc != NULL)) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009140 /*
9141 * User-defined behaviour.
9142 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009143 ns = ctxt->getNsForNodeFunc(ctxt, cur,
9144 cur->ns->href, cur->ns->prefix);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009145 /*
9146 * Add user's mapping.
9147 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009148 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009149 cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
9150 goto internal_error;
9151 clone->ns = ns;
9152 } else {
9153 /*
9154 * Aquire a normalized ns-decl and add it to the map.
9155 */
9156 if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc,
9157 /* ns-decls on curElem or on destDoc->oldNs */
9158 destParent ? curElem : NULL,
9159 cur->ns, &ns,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009160 &nsMap, depth,
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009161 /* if we need to search only in the ancestor-axis */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009162 ancestorsOnly,
9163 /* ns-decls must be prefixed for attributes. */
9164 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
9165 goto internal_error;
9166 clone->ns = ns;
9167 }
9168
9169end_ns_reference:
9170
9171 /*
9172 * Some post-processing.
9173 *
9174 * Handle ID attributes.
9175 */
9176 if ((clone->type == XML_ATTRIBUTE_NODE) &&
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009177 (clone->parent != NULL))
9178 {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009179 if (xmlIsID(destDoc, clone->parent, (xmlAttrPtr) clone)) {
9180
9181 xmlChar *idVal;
9182
9183 idVal = xmlNodeListGetString(cur->doc, cur->children, 1);
9184 if (idVal != NULL) {
9185 if (xmlAddID(NULL, destDoc, idVal, (xmlAttrPtr) cur) == NULL) {
9186 /* TODO: error message. */
9187 xmlFree(idVal);
9188 goto internal_error;
9189 }
9190 xmlFree(idVal);
9191 }
9192 }
9193 }
9194 /*
9195 **
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009196 ** The following will traverse the tree **************************
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009197 **
9198 *
9199 * Walk the element's attributes before descending into child-nodes.
9200 */
9201 if ((cur->type == XML_ELEMENT_NODE) && (cur->properties != NULL)) {
9202 prevClone = NULL;
9203 parentClone = clone;
9204 cur = (xmlNodePtr) cur->properties;
9205 continue;
9206 }
9207into_content:
9208 /*
9209 * Descend into child-nodes.
9210 */
9211 if (cur->children != NULL) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009212 if (deep || (cur->type == XML_ATTRIBUTE_NODE)) {
9213 prevClone = NULL;
9214 parentClone = clone;
9215 cur = cur->children;
9216 continue;
9217 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009218 }
9219
9220leave_node:
9221 /*
9222 * At this point we are done with the node, its content
9223 * and an element-nodes's attribute-nodes.
9224 */
9225 if (cur == node)
9226 break;
9227 if ((cur->type == XML_ELEMENT_NODE) ||
9228 (cur->type == XML_XINCLUDE_START) ||
9229 (cur->type == XML_XINCLUDE_END)) {
9230 /*
9231 * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
9232 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009233 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009234 /*
9235 * Pop mappings.
9236 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009237 while ((nsMap->last != NULL) &&
9238 (nsMap->last->depth >= depth))
9239 {
9240 XML_NSMAP_POP(nsMap, mi)
9241 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009242 /*
9243 * Unshadow.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009244 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009245 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009246 if (mi->shadowDepth >= depth)
9247 mi->shadowDepth = -1;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009248 }
9249 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009250 depth--;
9251 }
9252 if (cur->next != NULL) {
9253 prevClone = clone;
9254 cur = cur->next;
9255 } else if (cur->type != XML_ATTRIBUTE_NODE) {
9256 /*
9257 * Set clone->last.
9258 */
Daniel Veillard11ce4002006-03-10 00:36:23 +00009259 if (clone->parent != NULL)
9260 clone->parent->last = clone;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009261 clone = clone->parent;
9262 parentClone = clone->parent;
9263 /*
9264 * Process parent --> next;
9265 */
9266 cur = cur->parent;
9267 goto leave_node;
9268 } else {
9269 /* This is for attributes only. */
9270 clone = clone->parent;
9271 parentClone = clone->parent;
9272 /*
9273 * Process parent-element --> children.
9274 */
9275 cur = cur->parent;
9276 goto into_content;
9277 }
9278 }
9279 goto exit;
9280
9281internal_error:
9282 ret = -1;
9283
9284exit:
9285 /*
9286 * Cleanup.
9287 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009288 if (nsMap != NULL) {
9289 if ((ctxt) && (ctxt->namespaceMap == nsMap)) {
9290 /*
9291 * Just cleanup the map but don't free.
9292 */
9293 if (nsMap->first) {
9294 if (nsMap->pool)
9295 nsMap->last->next = nsMap->pool;
9296 nsMap->pool = nsMap->first;
9297 nsMap->first = NULL;
9298 }
9299 } else
9300 xmlDOMWrapNsMapFree(nsMap);
9301 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009302 /*
9303 * TODO: Should we try a cleanup of the cloned node in case of a
9304 * fatal error?
9305 */
9306 *resNode = resultClone;
9307 return (ret);
9308}
9309
9310/*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009311* xmlDOMWrapAdoptAttr:
9312* @ctxt: the optional context for custom processing
9313* @sourceDoc: the optional source document of attr
9314* @attr: the attribute-node to be adopted
9315* @destDoc: the destination doc for adoption
9316* @destParent: the optional new parent of @attr in @destDoc
9317* @options: option flags
9318*
9319* @attr is adopted by @destDoc.
9320* Ensures that ns-references point to @destDoc: either to
9321* elements->nsDef entries if @destParent is given, or to
9322* @destDoc->oldNs otherwise.
9323*
9324* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
9325*/
9326static int
9327xmlDOMWrapAdoptAttr(xmlDOMWrapCtxtPtr ctxt,
9328 xmlDocPtr sourceDoc,
9329 xmlAttrPtr attr,
9330 xmlDocPtr destDoc,
9331 xmlNodePtr destParent,
9332 int options ATTRIBUTE_UNUSED)
9333{
9334 xmlNodePtr cur;
9335 int adoptStr = 1;
9336
9337 if ((attr == NULL) || (destDoc == NULL))
9338 return (-1);
9339
9340 attr->doc = destDoc;
9341 if (attr->ns != NULL) {
9342 xmlNsPtr ns = NULL;
9343
9344 if (ctxt != NULL) {
9345 /* TODO: User defined. */
9346 }
9347 /* XML Namespace. */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009348 if (IS_STR_XML(attr->ns->prefix)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009349 ns = xmlTreeEnsureXMLDecl(destDoc);
9350 } else if (destParent == NULL) {
9351 /*
9352 * Store in @destDoc->oldNs.
9353 */
9354 ns = xmlDOMWrapStoreNs(destDoc, attr->ns->href, attr->ns->prefix);
9355 } else {
9356 /*
9357 * Declare on @destParent.
9358 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009359 if (xmlSearchNsByNamespaceStrict(destDoc, destParent, attr->ns->href,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009360 &ns, 1) == -1)
9361 goto internal_error;
9362 if (ns == NULL) {
9363 ns = xmlDOMWrapNSNormDeclareNsForced(destDoc, destParent,
9364 attr->ns->href, attr->ns->prefix, 1);
9365 }
9366 }
9367 if (ns == NULL)
9368 goto internal_error;
9369 attr->ns = ns;
9370 }
9371
9372 XML_TREE_ADOPT_STR(attr->name);
9373 attr->atype = 0;
9374 attr->psvi = NULL;
9375 /*
9376 * Walk content.
9377 */
9378 if (attr->children == NULL)
9379 return (0);
9380 cur = attr->children;
9381 while (cur != NULL) {
9382 cur->doc = destDoc;
9383 switch (cur->type) {
9384 case XML_TEXT_NODE:
9385 case XML_CDATA_SECTION_NODE:
9386 XML_TREE_ADOPT_STR_2(cur->content)
9387 break;
9388 case XML_ENTITY_REF_NODE:
9389 /*
9390 * Remove reference to the entitity-node.
9391 */
9392 cur->content = NULL;
9393 cur->children = NULL;
9394 cur->last = NULL;
9395 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9396 xmlEntityPtr ent;
9397 /*
9398 * Assign new entity-node if available.
9399 */
9400 ent = xmlGetDocEntity(destDoc, cur->name);
9401 if (ent != NULL) {
9402 cur->content = ent->content;
9403 cur->children = (xmlNodePtr) ent;
9404 cur->last = (xmlNodePtr) ent;
9405 }
9406 }
9407 break;
9408 default:
9409 break;
9410 }
9411 if (cur->children != NULL) {
9412 cur = cur->children;
9413 continue;
9414 }
9415next_sibling:
9416 if (cur == (xmlNodePtr) attr)
9417 break;
9418 if (cur->next != NULL)
9419 cur = cur->next;
9420 else {
9421 cur = cur->parent;
9422 goto next_sibling;
9423 }
9424 }
9425 return (0);
9426internal_error:
9427 return (-1);
9428}
9429
9430/*
9431* xmlDOMWrapAdoptNode:
9432* @ctxt: the optional context for custom processing
9433* @sourceDoc: the optional sourceDoc
9434* @node: the node to start with
9435* @destDoc: the destination doc
Kasimier T. Buchcik4d9c9482005-06-27 15:04:46 +00009436* @destParent: the optional new parent of @node in @destDoc
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009437* @options: option flags
9438*
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009439* References of out-of scope ns-decls are remapped to point to @destDoc:
9440* 1) If @destParent is given, then nsDef entries on element-nodes are used
9441* 2) If *no* @destParent is given, then @destDoc->oldNs entries are used
9442* This is the case when you have an unliked node and just want to move it
9443* to the context of
9444*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009445* If @destParent is given, it ensures that the tree is namespace
9446* wellformed by creating additional ns-decls where needed.
9447* Note that, since prefixes of already existent ns-decls can be
9448* shadowed by this process, it could break QNames in attribute
9449* values or element content.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009450* NOTE: This function was not intensively tested.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009451*
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009452* Returns 0 if the operation succeeded,
9453* 1 if a node of unsupported type was given,
9454* 2 if a node of not yet supported type was given and
9455* -1 on API/internal errors.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009456*/
9457int
9458xmlDOMWrapAdoptNode(xmlDOMWrapCtxtPtr ctxt,
9459 xmlDocPtr sourceDoc,
9460 xmlNodePtr node,
9461 xmlDocPtr destDoc,
9462 xmlNodePtr destParent,
9463 int options)
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009464{
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009465 if ((node == NULL) || (destDoc == NULL) ||
9466 ((destParent != NULL) && (destParent->doc != destDoc)))
9467 return(-1);
9468 /*
9469 * Check node->doc sanity.
9470 */
9471 if ((node->doc != NULL) && (sourceDoc != NULL) &&
9472 (node->doc != sourceDoc)) {
9473 /*
9474 * Might be an XIncluded node.
9475 */
9476 return (-1);
9477 }
9478 if (sourceDoc == NULL)
9479 sourceDoc = node->doc;
9480 if (sourceDoc == destDoc)
9481 return (-1);
9482 switch (node->type) {
9483 case XML_ELEMENT_NODE:
9484 case XML_ATTRIBUTE_NODE:
9485 case XML_TEXT_NODE:
9486 case XML_CDATA_SECTION_NODE:
9487 case XML_ENTITY_REF_NODE:
9488 case XML_PI_NODE:
9489 case XML_COMMENT_NODE:
9490 break;
9491 case XML_DOCUMENT_FRAG_NODE:
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009492 /* TODO: Support document-fragment-nodes. */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009493 return (2);
9494 default:
9495 return (1);
9496 }
9497 /*
9498 * Unlink only if @node was not already added to @destParent.
9499 */
9500 if ((node->parent != NULL) && (destParent != node->parent))
9501 xmlUnlinkNode(node);
9502
9503 if (node->type == XML_ELEMENT_NODE) {
9504 return (xmlDOMWrapAdoptBranch(ctxt, sourceDoc, node,
9505 destDoc, destParent, options));
9506 } else if (node->type == XML_ATTRIBUTE_NODE) {
9507 return (xmlDOMWrapAdoptAttr(ctxt, sourceDoc,
9508 (xmlAttrPtr) node, destDoc, destParent, options));
9509 } else {
9510 xmlNodePtr cur = node;
9511 int adoptStr = 1;
9512
9513 cur->doc = destDoc;
9514 /*
9515 * Optimize string adoption.
9516 */
9517 if ((sourceDoc != NULL) &&
9518 (sourceDoc->dict == destDoc->dict))
9519 adoptStr = 0;
9520 switch (node->type) {
9521 case XML_TEXT_NODE:
9522 case XML_CDATA_SECTION_NODE:
9523 XML_TREE_ADOPT_STR_2(node->content)
9524 break;
9525 case XML_ENTITY_REF_NODE:
9526 /*
9527 * Remove reference to the entitity-node.
9528 */
9529 node->content = NULL;
9530 node->children = NULL;
9531 node->last = NULL;
9532 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9533 xmlEntityPtr ent;
9534 /*
9535 * Assign new entity-node if available.
9536 */
9537 ent = xmlGetDocEntity(destDoc, node->name);
9538 if (ent != NULL) {
9539 node->content = ent->content;
9540 node->children = (xmlNodePtr) ent;
9541 node->last = (xmlNodePtr) ent;
9542 }
9543 }
9544 XML_TREE_ADOPT_STR(node->name)
9545 break;
Daniel Veillard7e21fd12005-07-03 21:44:07 +00009546 case XML_PI_NODE: {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009547 XML_TREE_ADOPT_STR(node->name)
9548 XML_TREE_ADOPT_STR_2(node->content)
9549 break;
Daniel Veillard7e21fd12005-07-03 21:44:07 +00009550 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009551 default:
9552 break;
9553 }
9554 }
9555 return (0);
9556}
9557
Daniel Veillard5d4644e2005-04-01 13:11:58 +00009558#define bottom_tree
9559#include "elfgcchack.h"