blob: b3aeaf2c30fa6131e20965677ef9183ad1cbcc3d [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
Daniel Veillard56a4cb82001-03-24 17:00:36 +000046xmlNsPtr xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns);
47
48/************************************************************************
49 * *
Daniel Veillard18ec16e2003-10-07 23:16:40 +000050 * Tree memory error handler *
51 * *
52 ************************************************************************/
53/**
54 * xmlTreeErrMemory:
55 * @extra: extra informations
56 *
57 * Handle an out of memory condition
58 */
59static void
60xmlTreeErrMemory(const char *extra)
61{
62 __xmlSimpleError(XML_FROM_TREE, XML_ERR_NO_MEMORY, NULL, NULL, extra);
63}
64
65/**
66 * xmlTreeErr:
67 * @code: the error number
68 * @extra: extra informations
69 *
70 * Handle an out of memory condition
71 */
72static void
73xmlTreeErr(int code, xmlNodePtr node, const char *extra)
74{
75 const char *msg = NULL;
76
77 switch(code) {
78 case XML_TREE_INVALID_HEX:
Daniel Veillardac996a12004-07-30 12:02:58 +000079 msg = "invalid hexadecimal character value\n";
Daniel Veillard18ec16e2003-10-07 23:16:40 +000080 break;
81 case XML_TREE_INVALID_DEC:
Daniel Veillardac996a12004-07-30 12:02:58 +000082 msg = "invalid decimal character value\n";
Daniel Veillard18ec16e2003-10-07 23:16:40 +000083 break;
84 case XML_TREE_UNTERMINATED_ENTITY:
Daniel Veillardac996a12004-07-30 12:02:58 +000085 msg = "unterminated entity reference %15s\n";
Daniel Veillard18ec16e2003-10-07 23:16:40 +000086 break;
87 default:
Daniel Veillardac996a12004-07-30 12:02:58 +000088 msg = "unexpected error number\n";
Daniel Veillard18ec16e2003-10-07 23:16:40 +000089 }
90 __xmlSimpleError(XML_FROM_TREE, code, node, msg, extra);
91}
92
93/************************************************************************
94 * *
Daniel Veillard56a4cb82001-03-24 17:00:36 +000095 * A few static variables and macros *
96 * *
97 ************************************************************************/
Daniel Veillardd0463562001-10-13 09:15:48 +000098/* #undef xmlStringText */
Daniel Veillard22090732001-07-16 00:06:07 +000099const xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 };
Daniel Veillardd0463562001-10-13 09:15:48 +0000100/* #undef xmlStringTextNoenc */
Daniel Veillard22090732001-07-16 00:06:07 +0000101const xmlChar xmlStringTextNoenc[] =
Owen Taylor3473f882001-02-23 17:55:21 +0000102 { 't', 'e', 'x', 't', 'n', 'o', 'e', 'n', 'c', 0 };
Daniel Veillardd0463562001-10-13 09:15:48 +0000103/* #undef xmlStringComment */
Daniel Veillard22090732001-07-16 00:06:07 +0000104const xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 };
105
Owen Taylor3473f882001-02-23 17:55:21 +0000106static int xmlCompressMode = 0;
107static int xmlCheckDTD = 1;
Owen Taylor3473f882001-02-23 17:55:21 +0000108
Owen Taylor3473f882001-02-23 17:55:21 +0000109#define UPDATE_LAST_CHILD_AND_PARENT(n) if ((n) != NULL) { \
110 xmlNodePtr ulccur = (n)->children; \
111 if (ulccur == NULL) { \
112 (n)->last = NULL; \
113 } else { \
114 while (ulccur->next != NULL) { \
115 ulccur->parent = (n); \
116 ulccur = ulccur->next; \
117 } \
118 ulccur->parent = (n); \
119 (n)->last = ulccur; \
120}}
121
122/* #define DEBUG_BUFFER */
123/* #define DEBUG_TREE */
124
125/************************************************************************
126 * *
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000127 * Functions to move to entities.c once the *
128 * API freeze is smoothen and they can be made public. *
129 * *
130 ************************************************************************/
131#include <libxml/hash.h>
132
Daniel Veillard652327a2003-09-29 18:02:38 +0000133#ifdef LIBXML_TREE_ENABLED
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000134/**
135 * xmlGetEntityFromDtd:
136 * @dtd: A pointer to the DTD to search
137 * @name: The entity name
138 *
139 * Do an entity lookup in the DTD entity hash table and
140 * return the corresponding entity, if found.
141 *
142 * Returns A pointer to the entity structure or NULL if not found.
143 */
144static xmlEntityPtr
145xmlGetEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) {
146 xmlEntitiesTablePtr table;
147
148 if((dtd != NULL) && (dtd->entities != NULL)) {
149 table = (xmlEntitiesTablePtr) dtd->entities;
150 return((xmlEntityPtr) xmlHashLookup(table, name));
151 /* return(xmlGetEntityFromTable(table, name)); */
152 }
153 return(NULL);
154}
155/**
156 * xmlGetParameterEntityFromDtd:
157 * @dtd: A pointer to the DTD to search
158 * @name: The entity name
159 *
160 * Do an entity lookup in the DTD pararmeter entity hash table and
161 * return the corresponding entity, if found.
162 *
163 * Returns A pointer to the entity structure or NULL if not found.
164 */
165static xmlEntityPtr
166xmlGetParameterEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) {
167 xmlEntitiesTablePtr table;
168
169 if ((dtd != NULL) && (dtd->pentities != NULL)) {
170 table = (xmlEntitiesTablePtr) dtd->pentities;
171 return((xmlEntityPtr) xmlHashLookup(table, name));
172 /* return(xmlGetEntityFromTable(table, name)); */
173 }
174 return(NULL);
175}
Daniel Veillard652327a2003-09-29 18:02:38 +0000176#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000177
178/************************************************************************
179 * *
Daniel Veillardc00cda82003-04-07 10:22:39 +0000180 * QName handling helper *
181 * *
182 ************************************************************************/
183
184/**
185 * xmlBuildQName:
186 * @ncname: the Name
187 * @prefix: the prefix
188 * @memory: preallocated memory
189 * @len: preallocated memory length
190 *
191 * Builds the QName @prefix:@ncname in @memory if there is enough space
192 * and prefix is not NULL nor empty, otherwise allocate a new string.
193 * If prefix is NULL or empty it returns ncname.
194 *
195 * Returns the new string which must be freed by the caller if different from
196 * @memory and @ncname or NULL in case of error
197 */
198xmlChar *
199xmlBuildQName(const xmlChar *ncname, const xmlChar *prefix,
200 xmlChar *memory, int len) {
201 int lenn, lenp;
202 xmlChar *ret;
203
Daniel Veillard3b7840c2003-09-11 23:42:01 +0000204 if (ncname == NULL) return(NULL);
205 if (prefix == NULL) return((xmlChar *) ncname);
Daniel Veillardc00cda82003-04-07 10:22:39 +0000206
207 lenn = strlen((char *) ncname);
208 lenp = strlen((char *) prefix);
209
210 if ((memory == NULL) || (len < lenn + lenp + 2)) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000211 ret = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000212 if (ret == NULL) {
213 xmlTreeErrMemory("building QName");
214 return(NULL);
215 }
Daniel Veillardc00cda82003-04-07 10:22:39 +0000216 } else {
217 ret = memory;
218 }
219 memcpy(&ret[0], prefix, lenp);
220 ret[lenp] = ':';
221 memcpy(&ret[lenp + 1], ncname, lenn);
222 ret[lenn + lenp + 1] = 0;
223 return(ret);
224}
225
226/**
227 * xmlSplitQName2:
228 * @name: the full QName
229 * @prefix: a xmlChar **
230 *
231 * parse an XML qualified name string
232 *
233 * [NS 5] QName ::= (Prefix ':')? LocalPart
234 *
235 * [NS 6] Prefix ::= NCName
236 *
237 * [NS 7] LocalPart ::= NCName
238 *
239 * Returns NULL if not a QName, otherwise the local part, and prefix
240 * is updated to get the Prefix if any.
241 */
242
243xmlChar *
244xmlSplitQName2(const xmlChar *name, xmlChar **prefix) {
245 int len = 0;
246 xmlChar *ret = NULL;
247
Daniel Veillardd5cc0f72004-11-06 19:24:28 +0000248 if (prefix == NULL) return(NULL);
Daniel Veillardc00cda82003-04-07 10:22:39 +0000249 *prefix = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000250 if (name == NULL) return(NULL);
Daniel Veillardc00cda82003-04-07 10:22:39 +0000251
252#ifndef XML_XML_NAMESPACE
253 /* xml: prefix is not really a namespace */
254 if ((name[0] == 'x') && (name[1] == 'm') &&
255 (name[2] == 'l') && (name[3] == ':'))
256 return(NULL);
257#endif
258
259 /* nasty but valid */
260 if (name[0] == ':')
261 return(NULL);
262
263 /*
264 * we are not trying to validate but just to cut, and yes it will
265 * work even if this is as set of UTF-8 encoded chars
266 */
267 while ((name[len] != 0) && (name[len] != ':'))
268 len++;
269
270 if (name[len] == 0)
271 return(NULL);
272
273 *prefix = xmlStrndup(name, len);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000274 if (*prefix == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000275 xmlTreeErrMemory("QName split");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000276 return(NULL);
277 }
Daniel Veillardc00cda82003-04-07 10:22:39 +0000278 ret = xmlStrdup(&name[len + 1]);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000279 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000280 xmlTreeErrMemory("QName split");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000281 if (*prefix != NULL) {
282 xmlFree(*prefix);
283 *prefix = NULL;
284 }
285 return(NULL);
286 }
Daniel Veillardc00cda82003-04-07 10:22:39 +0000287
288 return(ret);
289}
290
Daniel Veillard8d73bcb2003-08-04 01:06:15 +0000291/**
292 * xmlSplitQName3:
293 * @name: the full QName
294 * @len: an int *
295 *
296 * parse an XML qualified name string,i
297 *
298 * returns NULL if it is not a Qualified Name, otherwise, update len
299 * with the lenght in byte of the prefix and return a pointer
300 */
301
302const xmlChar *
303xmlSplitQName3(const xmlChar *name, int *len) {
304 int l = 0;
305
306 if (name == NULL) return(NULL);
307 if (len == NULL) return(NULL);
308
309 /* nasty but valid */
310 if (name[0] == ':')
311 return(NULL);
312
313 /*
314 * we are not trying to validate but just to cut, and yes it will
315 * work even if this is as set of UTF-8 encoded chars
316 */
317 while ((name[l] != 0) && (name[l] != ':'))
318 l++;
319
320 if (name[l] == 0)
321 return(NULL);
322
323 *len = l;
324
325 return(&name[l+1]);
326}
327
Daniel Veillardc00cda82003-04-07 10:22:39 +0000328/************************************************************************
329 * *
Daniel Veillardd2298792003-02-14 16:54:11 +0000330 * Check Name, NCName and QName strings *
331 * *
332 ************************************************************************/
333
334#define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
335
Daniel Veillard6b6d6802005-07-03 21:00:34 +0000336#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_DEBUG_ENABLED) || defined (LIBXML_HTML_ENABLED)
Daniel Veillardd2298792003-02-14 16:54:11 +0000337/**
338 * xmlValidateNCName:
339 * @value: the value to check
340 * @space: allow spaces in front and end of the string
341 *
342 * Check that a value conforms to the lexical space of NCName
343 *
344 * Returns 0 if this validates, a positive error code number otherwise
345 * and -1 in case of internal or API error.
346 */
347int
348xmlValidateNCName(const xmlChar *value, int space) {
349 const xmlChar *cur = value;
350 int c,l;
351
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000352 if (value == NULL)
353 return(-1);
354
Daniel Veillardd2298792003-02-14 16:54:11 +0000355 /*
356 * First quick algorithm for ASCII range
357 */
358 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000359 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +0000360 if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
361 (*cur == '_'))
362 cur++;
363 else
364 goto try_complex;
365 while (((*cur >= 'a') && (*cur <= 'z')) ||
366 ((*cur >= 'A') && (*cur <= 'Z')) ||
367 ((*cur >= '0') && (*cur <= '9')) ||
368 (*cur == '_') || (*cur == '-') || (*cur == '.'))
369 cur++;
370 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000371 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +0000372 if (*cur == 0)
373 return(0);
374
375try_complex:
376 /*
377 * Second check for chars outside the ASCII range
378 */
379 cur = value;
380 c = CUR_SCHAR(cur, l);
381 if (space) {
382 while (IS_BLANK(c)) {
383 cur += l;
384 c = CUR_SCHAR(cur, l);
385 }
386 }
William M. Brack871611b2003-10-18 04:53:14 +0000387 if ((!IS_LETTER(c)) && (c != '_'))
Daniel Veillardd2298792003-02-14 16:54:11 +0000388 return(1);
389 cur += l;
390 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +0000391 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
392 (c == '-') || (c == '_') || IS_COMBINING(c) ||
393 IS_EXTENDER(c)) {
Daniel Veillardd2298792003-02-14 16:54:11 +0000394 cur += l;
395 c = CUR_SCHAR(cur, l);
396 }
397 if (space) {
398 while (IS_BLANK(c)) {
399 cur += l;
400 c = CUR_SCHAR(cur, l);
401 }
402 }
403 if (c != 0)
404 return(1);
405
406 return(0);
407}
Daniel Veillard2156d432004-03-04 15:59:36 +0000408#endif
Daniel Veillardd2298792003-02-14 16:54:11 +0000409
Daniel Veillard2156d432004-03-04 15:59:36 +0000410#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Daniel Veillardd2298792003-02-14 16:54:11 +0000411/**
412 * xmlValidateQName:
413 * @value: the value to check
414 * @space: allow spaces in front and end of the string
415 *
416 * Check that a value conforms to the lexical space of QName
417 *
418 * Returns 0 if this validates, a positive error code number otherwise
419 * and -1 in case of internal or API error.
420 */
421int
422xmlValidateQName(const xmlChar *value, int space) {
423 const xmlChar *cur = value;
424 int c,l;
425
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000426 if (value == NULL)
427 return(-1);
Daniel Veillardd2298792003-02-14 16:54:11 +0000428 /*
429 * First quick algorithm for ASCII range
430 */
431 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000432 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +0000433 if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
434 (*cur == '_'))
435 cur++;
436 else
437 goto try_complex;
438 while (((*cur >= 'a') && (*cur <= 'z')) ||
439 ((*cur >= 'A') && (*cur <= 'Z')) ||
440 ((*cur >= '0') && (*cur <= '9')) ||
441 (*cur == '_') || (*cur == '-') || (*cur == '.'))
442 cur++;
443 if (*cur == ':') {
444 cur++;
445 if (((*cur >= 'a') && (*cur <= 'z')) ||
446 ((*cur >= 'A') && (*cur <= 'Z')) ||
447 (*cur == '_'))
448 cur++;
449 else
450 goto try_complex;
451 while (((*cur >= 'a') && (*cur <= 'z')) ||
452 ((*cur >= 'A') && (*cur <= 'Z')) ||
453 ((*cur >= '0') && (*cur <= '9')) ||
454 (*cur == '_') || (*cur == '-') || (*cur == '.'))
455 cur++;
456 }
457 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000458 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +0000459 if (*cur == 0)
460 return(0);
461
462try_complex:
463 /*
464 * Second check for chars outside the ASCII range
465 */
466 cur = value;
467 c = CUR_SCHAR(cur, l);
468 if (space) {
469 while (IS_BLANK(c)) {
470 cur += l;
471 c = CUR_SCHAR(cur, l);
472 }
473 }
William M. Brack871611b2003-10-18 04:53:14 +0000474 if ((!IS_LETTER(c)) && (c != '_'))
Daniel Veillardd2298792003-02-14 16:54:11 +0000475 return(1);
476 cur += l;
477 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +0000478 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
479 (c == '-') || (c == '_') || IS_COMBINING(c) ||
480 IS_EXTENDER(c)) {
Daniel Veillardd2298792003-02-14 16:54:11 +0000481 cur += l;
482 c = CUR_SCHAR(cur, l);
483 }
484 if (c == ':') {
485 cur += l;
486 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +0000487 if ((!IS_LETTER(c)) && (c != '_'))
Daniel Veillardd2298792003-02-14 16:54:11 +0000488 return(1);
489 cur += l;
490 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +0000491 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
492 (c == '-') || (c == '_') || IS_COMBINING(c) ||
493 IS_EXTENDER(c)) {
Daniel Veillardd2298792003-02-14 16:54:11 +0000494 cur += l;
495 c = CUR_SCHAR(cur, l);
496 }
497 }
498 if (space) {
499 while (IS_BLANK(c)) {
500 cur += l;
501 c = CUR_SCHAR(cur, l);
502 }
503 }
504 if (c != 0)
505 return(1);
506 return(0);
507}
508
509/**
510 * xmlValidateName:
511 * @value: the value to check
512 * @space: allow spaces in front and end of the string
513 *
514 * Check that a value conforms to the lexical space of Name
515 *
516 * Returns 0 if this validates, a positive error code number otherwise
517 * and -1 in case of internal or API error.
518 */
519int
520xmlValidateName(const xmlChar *value, int space) {
521 const xmlChar *cur = value;
522 int c,l;
523
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000524 if (value == NULL)
525 return(-1);
Daniel Veillardd2298792003-02-14 16:54:11 +0000526 /*
527 * First quick algorithm for ASCII range
528 */
529 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000530 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +0000531 if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
532 (*cur == '_') || (*cur == ':'))
533 cur++;
534 else
535 goto try_complex;
536 while (((*cur >= 'a') && (*cur <= 'z')) ||
537 ((*cur >= 'A') && (*cur <= 'Z')) ||
538 ((*cur >= '0') && (*cur <= '9')) ||
539 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
540 cur++;
541 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000542 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +0000543 if (*cur == 0)
544 return(0);
545
546try_complex:
547 /*
548 * Second check for chars outside the ASCII range
549 */
550 cur = value;
551 c = CUR_SCHAR(cur, l);
552 if (space) {
553 while (IS_BLANK(c)) {
554 cur += l;
555 c = CUR_SCHAR(cur, l);
556 }
557 }
William M. Brack871611b2003-10-18 04:53:14 +0000558 if ((!IS_LETTER(c)) && (c != '_') && (c != ':'))
Daniel Veillardd2298792003-02-14 16:54:11 +0000559 return(1);
560 cur += l;
561 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +0000562 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
563 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) {
Daniel Veillardd2298792003-02-14 16:54:11 +0000564 cur += l;
565 c = CUR_SCHAR(cur, l);
566 }
567 if (space) {
568 while (IS_BLANK(c)) {
569 cur += l;
570 c = CUR_SCHAR(cur, l);
571 }
572 }
573 if (c != 0)
574 return(1);
575 return(0);
576}
577
Daniel Veillardd4310742003-02-18 21:12:46 +0000578/**
579 * xmlValidateNMToken:
580 * @value: the value to check
581 * @space: allow spaces in front and end of the string
582 *
583 * Check that a value conforms to the lexical space of NMToken
584 *
585 * Returns 0 if this validates, a positive error code number otherwise
586 * and -1 in case of internal or API error.
587 */
588int
589xmlValidateNMToken(const xmlChar *value, int space) {
590 const xmlChar *cur = value;
591 int c,l;
592
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000593 if (value == NULL)
594 return(-1);
Daniel Veillardd4310742003-02-18 21:12:46 +0000595 /*
596 * First quick algorithm for ASCII range
597 */
598 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000599 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd4310742003-02-18 21:12:46 +0000600 if (((*cur >= 'a') && (*cur <= 'z')) ||
601 ((*cur >= 'A') && (*cur <= 'Z')) ||
602 ((*cur >= '0') && (*cur <= '9')) ||
603 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
604 cur++;
605 else
606 goto try_complex;
607 while (((*cur >= 'a') && (*cur <= 'z')) ||
608 ((*cur >= 'A') && (*cur <= 'Z')) ||
609 ((*cur >= '0') && (*cur <= '9')) ||
610 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
611 cur++;
612 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000613 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd4310742003-02-18 21:12:46 +0000614 if (*cur == 0)
615 return(0);
616
617try_complex:
618 /*
619 * Second check for chars outside the ASCII range
620 */
621 cur = value;
622 c = CUR_SCHAR(cur, l);
623 if (space) {
624 while (IS_BLANK(c)) {
625 cur += l;
626 c = CUR_SCHAR(cur, l);
627 }
628 }
William M. Brack871611b2003-10-18 04:53:14 +0000629 if (!(IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
630 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)))
Daniel Veillardd4310742003-02-18 21:12:46 +0000631 return(1);
632 cur += l;
633 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +0000634 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
635 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) {
Daniel Veillardd4310742003-02-18 21:12:46 +0000636 cur += l;
637 c = CUR_SCHAR(cur, l);
638 }
639 if (space) {
640 while (IS_BLANK(c)) {
641 cur += l;
642 c = CUR_SCHAR(cur, l);
643 }
644 }
645 if (c != 0)
646 return(1);
647 return(0);
648}
Daniel Veillard652327a2003-09-29 18:02:38 +0000649#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardd4310742003-02-18 21:12:46 +0000650
Daniel Veillardd2298792003-02-14 16:54:11 +0000651/************************************************************************
652 * *
Owen Taylor3473f882001-02-23 17:55:21 +0000653 * Allocation and deallocation of basic structures *
654 * *
655 ************************************************************************/
656
657/**
658 * xmlSetBufferAllocationScheme:
659 * @scheme: allocation method to use
660 *
661 * Set the buffer allocation method. Types are
662 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
663 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
664 * improves performance
665 */
666void
667xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme) {
668 xmlBufferAllocScheme = scheme;
669}
670
671/**
672 * xmlGetBufferAllocationScheme:
673 *
674 * Types are
675 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
676 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
677 * improves performance
678 *
679 * Returns the current allocation scheme
680 */
681xmlBufferAllocationScheme
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000682xmlGetBufferAllocationScheme(void) {
Daniel Veillarde043ee12001-04-16 14:08:07 +0000683 return(xmlBufferAllocScheme);
Owen Taylor3473f882001-02-23 17:55:21 +0000684}
685
686/**
687 * xmlNewNs:
688 * @node: the element carrying the namespace
689 * @href: the URI associated
690 * @prefix: the prefix for the namespace
691 *
692 * Creation of a new Namespace. This function will refuse to create
693 * a namespace with a similar prefix than an existing one present on this
694 * node.
695 * We use href==NULL in the case of an element creation where the namespace
696 * was not defined.
Daniel Veillardd1640922001-12-17 15:30:10 +0000697 * Returns a new namespace pointer or NULL
Owen Taylor3473f882001-02-23 17:55:21 +0000698 */
699xmlNsPtr
700xmlNewNs(xmlNodePtr node, const xmlChar *href, const xmlChar *prefix) {
701 xmlNsPtr cur;
702
703 if ((node != NULL) && (node->type != XML_ELEMENT_NODE))
704 return(NULL);
705
Daniel Veillard20ee8c02001-10-05 09:18:14 +0000706 if ((prefix != NULL) && (xmlStrEqual(prefix, BAD_CAST "xml")))
707 return(NULL);
708
Owen Taylor3473f882001-02-23 17:55:21 +0000709 /*
710 * Allocate a new Namespace and fill the fields.
711 */
712 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
713 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000714 xmlTreeErrMemory("building namespace");
Owen Taylor3473f882001-02-23 17:55:21 +0000715 return(NULL);
716 }
717 memset(cur, 0, sizeof(xmlNs));
718 cur->type = XML_LOCAL_NAMESPACE;
719
720 if (href != NULL)
721 cur->href = xmlStrdup(href);
722 if (prefix != NULL)
723 cur->prefix = xmlStrdup(prefix);
724
725 /*
726 * Add it at the end to preserve parsing order ...
727 * and checks for existing use of the prefix
728 */
729 if (node != NULL) {
730 if (node->nsDef == NULL) {
731 node->nsDef = cur;
732 } else {
733 xmlNsPtr prev = node->nsDef;
734
735 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
736 (xmlStrEqual(prev->prefix, cur->prefix))) {
737 xmlFreeNs(cur);
738 return(NULL);
739 }
740 while (prev->next != NULL) {
741 prev = prev->next;
742 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
743 (xmlStrEqual(prev->prefix, cur->prefix))) {
744 xmlFreeNs(cur);
745 return(NULL);
746 }
747 }
748 prev->next = cur;
749 }
750 }
751 return(cur);
752}
753
754/**
755 * xmlSetNs:
756 * @node: a node in the document
757 * @ns: a namespace pointer
758 *
759 * Associate a namespace to a node, a posteriori.
760 */
761void
762xmlSetNs(xmlNodePtr node, xmlNsPtr ns) {
763 if (node == NULL) {
764#ifdef DEBUG_TREE
765 xmlGenericError(xmlGenericErrorContext,
766 "xmlSetNs: node == NULL\n");
767#endif
768 return;
769 }
770 node->ns = ns;
771}
772
773/**
774 * xmlFreeNs:
775 * @cur: the namespace pointer
776 *
777 * Free up the structures associated to a namespace
778 */
779void
780xmlFreeNs(xmlNsPtr cur) {
781 if (cur == NULL) {
782#ifdef DEBUG_TREE
783 xmlGenericError(xmlGenericErrorContext,
784 "xmlFreeNs : ns == NULL\n");
785#endif
786 return;
787 }
788 if (cur->href != NULL) xmlFree((char *) cur->href);
789 if (cur->prefix != NULL) xmlFree((char *) cur->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000790 xmlFree(cur);
791}
792
793/**
794 * xmlFreeNsList:
795 * @cur: the first namespace pointer
796 *
797 * Free up all the structures associated to the chained namespaces.
798 */
799void
800xmlFreeNsList(xmlNsPtr cur) {
801 xmlNsPtr next;
802 if (cur == NULL) {
803#ifdef DEBUG_TREE
804 xmlGenericError(xmlGenericErrorContext,
805 "xmlFreeNsList : ns == NULL\n");
806#endif
807 return;
808 }
809 while (cur != NULL) {
810 next = cur->next;
811 xmlFreeNs(cur);
812 cur = next;
813 }
814}
815
816/**
817 * xmlNewDtd:
818 * @doc: the document pointer
819 * @name: the DTD name
820 * @ExternalID: the external ID
821 * @SystemID: the system ID
822 *
823 * Creation of a new DTD for the external subset. To create an
824 * internal subset, use xmlCreateIntSubset().
825 *
826 * Returns a pointer to the new DTD structure
827 */
828xmlDtdPtr
829xmlNewDtd(xmlDocPtr doc, const xmlChar *name,
830 const xmlChar *ExternalID, const xmlChar *SystemID) {
831 xmlDtdPtr cur;
832
833 if ((doc != NULL) && (doc->extSubset != NULL)) {
834#ifdef DEBUG_TREE
835 xmlGenericError(xmlGenericErrorContext,
836 "xmlNewDtd(%s): document %s already have a DTD %s\n",
837 /* !!! */ (char *) name, doc->name,
838 /* !!! */ (char *)doc->extSubset->name);
839#endif
840 return(NULL);
841 }
842
843 /*
844 * Allocate a new DTD and fill the fields.
845 */
846 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
847 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000848 xmlTreeErrMemory("building DTD");
Owen Taylor3473f882001-02-23 17:55:21 +0000849 return(NULL);
850 }
851 memset(cur, 0 , sizeof(xmlDtd));
852 cur->type = XML_DTD_NODE;
853
854 if (name != NULL)
855 cur->name = xmlStrdup(name);
856 if (ExternalID != NULL)
857 cur->ExternalID = xmlStrdup(ExternalID);
858 if (SystemID != NULL)
859 cur->SystemID = xmlStrdup(SystemID);
860 if (doc != NULL)
861 doc->extSubset = cur;
862 cur->doc = doc;
863
Daniel Veillarda880b122003-04-21 21:36:41 +0000864 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +0000865 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000866 return(cur);
867}
868
869/**
870 * xmlGetIntSubset:
871 * @doc: the document pointer
872 *
873 * Get the internal subset of a document
874 * Returns a pointer to the DTD structure or NULL if not found
875 */
876
877xmlDtdPtr
878xmlGetIntSubset(xmlDocPtr doc) {
879 xmlNodePtr cur;
880
881 if (doc == NULL)
882 return(NULL);
883 cur = doc->children;
884 while (cur != NULL) {
885 if (cur->type == XML_DTD_NODE)
886 return((xmlDtdPtr) cur);
887 cur = cur->next;
888 }
889 return((xmlDtdPtr) doc->intSubset);
890}
891
892/**
893 * xmlCreateIntSubset:
894 * @doc: the document pointer
895 * @name: the DTD name
Daniel Veillarde356c282001-03-10 12:32:04 +0000896 * @ExternalID: the external (PUBLIC) ID
Owen Taylor3473f882001-02-23 17:55:21 +0000897 * @SystemID: the system ID
898 *
899 * Create the internal subset of a document
900 * Returns a pointer to the new DTD structure
901 */
902xmlDtdPtr
903xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name,
904 const xmlChar *ExternalID, const xmlChar *SystemID) {
905 xmlDtdPtr cur;
906
907 if ((doc != NULL) && (xmlGetIntSubset(doc) != NULL)) {
908#ifdef DEBUG_TREE
909 xmlGenericError(xmlGenericErrorContext,
910
911 "xmlCreateIntSubset(): document %s already have an internal subset\n",
912 doc->name);
913#endif
914 return(NULL);
915 }
916
917 /*
918 * Allocate a new DTD and fill the fields.
919 */
920 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
921 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000922 xmlTreeErrMemory("building internal subset");
Owen Taylor3473f882001-02-23 17:55:21 +0000923 return(NULL);
924 }
925 memset(cur, 0, sizeof(xmlDtd));
926 cur->type = XML_DTD_NODE;
927
William M. Bracka3215c72004-07-31 16:24:01 +0000928 if (name != NULL) {
929 cur->name = xmlStrdup(name);
930 if (cur->name == NULL) {
931 xmlTreeErrMemory("building internal subset");
932 xmlFree(cur);
933 return(NULL);
934 }
935 }
936 if (ExternalID != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +0000937 cur->ExternalID = xmlStrdup(ExternalID);
William M. Bracka3215c72004-07-31 16:24:01 +0000938 if (cur->ExternalID == NULL) {
939 xmlTreeErrMemory("building internal subset");
940 if (cur->name != NULL)
941 xmlFree((char *)cur->name);
942 xmlFree(cur);
943 return(NULL);
944 }
945 }
946 if (SystemID != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +0000947 cur->SystemID = xmlStrdup(SystemID);
William M. Bracka3215c72004-07-31 16:24:01 +0000948 if (cur->SystemID == NULL) {
949 xmlTreeErrMemory("building internal subset");
950 if (cur->name != NULL)
951 xmlFree((char *)cur->name);
952 if (cur->ExternalID != NULL)
953 xmlFree((char *)cur->ExternalID);
954 xmlFree(cur);
955 return(NULL);
956 }
957 }
Owen Taylor3473f882001-02-23 17:55:21 +0000958 if (doc != NULL) {
959 doc->intSubset = cur;
960 cur->parent = doc;
961 cur->doc = doc;
962 if (doc->children == NULL) {
963 doc->children = (xmlNodePtr) cur;
964 doc->last = (xmlNodePtr) cur;
965 } else {
Owen Taylor3473f882001-02-23 17:55:21 +0000966 if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillarde356c282001-03-10 12:32:04 +0000967 xmlNodePtr prev;
968
Owen Taylor3473f882001-02-23 17:55:21 +0000969 prev = doc->children;
970 prev->prev = (xmlNodePtr) cur;
971 cur->next = prev;
972 doc->children = (xmlNodePtr) cur;
973 } else {
Daniel Veillarde356c282001-03-10 12:32:04 +0000974 xmlNodePtr next;
975
976 next = doc->children;
977 while ((next != NULL) && (next->type != XML_ELEMENT_NODE))
978 next = next->next;
979 if (next == NULL) {
980 cur->prev = doc->last;
981 cur->prev->next = (xmlNodePtr) cur;
982 cur->next = NULL;
983 doc->last = (xmlNodePtr) cur;
984 } else {
985 cur->next = next;
986 cur->prev = next->prev;
987 if (cur->prev == NULL)
988 doc->children = (xmlNodePtr) cur;
989 else
990 cur->prev->next = (xmlNodePtr) cur;
991 next->prev = (xmlNodePtr) cur;
992 }
Owen Taylor3473f882001-02-23 17:55:21 +0000993 }
994 }
995 }
Daniel Veillard8a1b1852003-01-05 22:37:17 +0000996
Daniel Veillarda880b122003-04-21 21:36:41 +0000997 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard8a1b1852003-01-05 22:37:17 +0000998 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000999 return(cur);
1000}
1001
1002/**
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001003 * DICT_FREE:
1004 * @str: a string
1005 *
1006 * Free a string if it is not owned by the "dict" dictionnary in the
1007 * current scope
1008 */
1009#define DICT_FREE(str) \
1010 if ((str) && ((!dict) || \
1011 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
1012 xmlFree((char *)(str));
1013
1014/**
Owen Taylor3473f882001-02-23 17:55:21 +00001015 * xmlFreeDtd:
1016 * @cur: the DTD structure to free up
1017 *
1018 * Free a DTD structure.
1019 */
1020void
1021xmlFreeDtd(xmlDtdPtr cur) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001022 xmlDictPtr dict = NULL;
1023
Owen Taylor3473f882001-02-23 17:55:21 +00001024 if (cur == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001025 return;
1026 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001027 if (cur->doc != NULL) dict = cur->doc->dict;
Daniel Veillard5335dc52003-01-01 20:59:38 +00001028
Daniel Veillarda880b122003-04-21 21:36:41 +00001029 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001030 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
1031
Owen Taylor3473f882001-02-23 17:55:21 +00001032 if (cur->children != NULL) {
1033 xmlNodePtr next, c = cur->children;
1034
1035 /*
William M. Brack18a04f22004-08-03 16:42:37 +00001036 * Cleanup all nodes which are not part of the specific lists
1037 * of notations, elements, attributes and entities.
Owen Taylor3473f882001-02-23 17:55:21 +00001038 */
1039 while (c != NULL) {
1040 next = c->next;
William M. Brack18a04f22004-08-03 16:42:37 +00001041 if ((c->type != XML_NOTATION_NODE) &&
1042 (c->type != XML_ELEMENT_DECL) &&
1043 (c->type != XML_ATTRIBUTE_DECL) &&
1044 (c->type != XML_ENTITY_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001045 xmlUnlinkNode(c);
1046 xmlFreeNode(c);
1047 }
1048 c = next;
1049 }
1050 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001051 DICT_FREE(cur->name)
1052 DICT_FREE(cur->SystemID)
1053 DICT_FREE(cur->ExternalID)
Owen Taylor3473f882001-02-23 17:55:21 +00001054 /* TODO !!! */
1055 if (cur->notations != NULL)
1056 xmlFreeNotationTable((xmlNotationTablePtr) cur->notations);
1057
1058 if (cur->elements != NULL)
1059 xmlFreeElementTable((xmlElementTablePtr) cur->elements);
1060 if (cur->attributes != NULL)
1061 xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes);
1062 if (cur->entities != NULL)
1063 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities);
1064 if (cur->pentities != NULL)
1065 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->pentities);
1066
Owen Taylor3473f882001-02-23 17:55:21 +00001067 xmlFree(cur);
1068}
1069
1070/**
1071 * xmlNewDoc:
1072 * @version: xmlChar string giving the version of XML "1.0"
1073 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001074 * Creates a new XML document
1075 *
Owen Taylor3473f882001-02-23 17:55:21 +00001076 * Returns a new document
1077 */
1078xmlDocPtr
1079xmlNewDoc(const xmlChar *version) {
1080 xmlDocPtr cur;
1081
1082 if (version == NULL)
1083 version = (const xmlChar *) "1.0";
1084
1085 /*
1086 * Allocate a new document and fill the fields.
1087 */
1088 cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
1089 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001090 xmlTreeErrMemory("building doc");
Owen Taylor3473f882001-02-23 17:55:21 +00001091 return(NULL);
1092 }
1093 memset(cur, 0, sizeof(xmlDoc));
1094 cur->type = XML_DOCUMENT_NODE;
1095
1096 cur->version = xmlStrdup(version);
William M. Bracka3215c72004-07-31 16:24:01 +00001097 if (cur->version == NULL) {
1098 xmlTreeErrMemory("building doc");
1099 xmlFree(cur);
1100 return(NULL);
1101 }
Owen Taylor3473f882001-02-23 17:55:21 +00001102 cur->standalone = -1;
1103 cur->compression = -1; /* not initialized */
1104 cur->doc = cur;
Daniel Veillarda6874ca2003-07-29 16:47:24 +00001105 /*
1106 * The in memory encoding is always UTF8
1107 * This field will never change and would
1108 * be obsolete if not for binary compatibility.
1109 */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00001110 cur->charset = XML_CHAR_ENCODING_UTF8;
Daniel Veillard5335dc52003-01-01 20:59:38 +00001111
Daniel Veillarda880b122003-04-21 21:36:41 +00001112 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001113 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001114 return(cur);
1115}
1116
1117/**
1118 * xmlFreeDoc:
1119 * @cur: pointer to the document
Owen Taylor3473f882001-02-23 17:55:21 +00001120 *
1121 * Free up all the structures used by a document, tree included.
1122 */
1123void
1124xmlFreeDoc(xmlDocPtr cur) {
Daniel Veillarda9142e72001-06-19 11:07:54 +00001125 xmlDtdPtr extSubset, intSubset;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001126 xmlDictPtr dict = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001127
Owen Taylor3473f882001-02-23 17:55:21 +00001128 if (cur == NULL) {
1129#ifdef DEBUG_TREE
1130 xmlGenericError(xmlGenericErrorContext,
1131 "xmlFreeDoc : document == NULL\n");
1132#endif
1133 return;
1134 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001135#ifdef LIBXML_DEBUG_RUNTIME
Daniel Veillardbca3ad22005-08-23 22:14:02 +00001136#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001137 xmlDebugCheckDocument(stderr, cur);
1138#endif
Daniel Veillardbca3ad22005-08-23 22:14:02 +00001139#endif
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001140
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001141 if (cur != NULL) dict = cur->dict;
Daniel Veillard5335dc52003-01-01 20:59:38 +00001142
Daniel Veillarda880b122003-04-21 21:36:41 +00001143 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001144 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
1145
Daniel Veillard76d66f42001-05-16 21:05:17 +00001146 /*
1147 * Do this before freeing the children list to avoid ID lookups
1148 */
1149 if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids);
1150 cur->ids = NULL;
1151 if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
1152 cur->refs = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001153 extSubset = cur->extSubset;
1154 intSubset = cur->intSubset;
Daniel Veillard5997aca2002-03-18 18:36:20 +00001155 if (intSubset == extSubset)
1156 extSubset = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001157 if (extSubset != NULL) {
Daniel Veillard76d66f42001-05-16 21:05:17 +00001158 xmlUnlinkNode((xmlNodePtr) cur->extSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001159 cur->extSubset = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001160 xmlFreeDtd(extSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001161 }
Daniel Veillarda9142e72001-06-19 11:07:54 +00001162 if (intSubset != NULL) {
Daniel Veillard76d66f42001-05-16 21:05:17 +00001163 xmlUnlinkNode((xmlNodePtr) cur->intSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001164 cur->intSubset = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001165 xmlFreeDtd(intSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001166 }
1167
1168 if (cur->children != NULL) xmlFreeNodeList(cur->children);
Owen Taylor3473f882001-02-23 17:55:21 +00001169 if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001170
1171 DICT_FREE(cur->version)
1172 DICT_FREE(cur->name)
1173 DICT_FREE(cur->encoding)
1174 DICT_FREE(cur->URL)
Owen Taylor3473f882001-02-23 17:55:21 +00001175 xmlFree(cur);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001176 if (dict) xmlDictFree(dict);
Owen Taylor3473f882001-02-23 17:55:21 +00001177}
1178
1179/**
1180 * xmlStringLenGetNodeList:
1181 * @doc: the document
1182 * @value: the value of the text
1183 * @len: the length of the string value
1184 *
1185 * Parse the value string and build the node list associated. Should
1186 * produce a flat tree with only TEXTs and ENTITY_REFs.
1187 * Returns a pointer to the first child
1188 */
1189xmlNodePtr
1190xmlStringLenGetNodeList(xmlDocPtr doc, const xmlChar *value, int len) {
1191 xmlNodePtr ret = NULL, last = NULL;
1192 xmlNodePtr node;
1193 xmlChar *val;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001194 const xmlChar *cur = value, *end = cur + len;
Owen Taylor3473f882001-02-23 17:55:21 +00001195 const xmlChar *q;
1196 xmlEntityPtr ent;
1197
1198 if (value == NULL) return(NULL);
1199
1200 q = cur;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001201 while ((cur < end) && (*cur != 0)) {
1202 if (cur[0] == '&') {
1203 int charval = 0;
1204 xmlChar tmp;
1205
Owen Taylor3473f882001-02-23 17:55:21 +00001206 /*
1207 * Save the current text.
1208 */
1209 if (cur != q) {
1210 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
1211 xmlNodeAddContentLen(last, q, cur - q);
1212 } else {
1213 node = xmlNewDocTextLen(doc, q, cur - q);
1214 if (node == NULL) return(ret);
1215 if (last == NULL)
1216 last = ret = node;
1217 else {
1218 last->next = node;
1219 node->prev = last;
1220 last = node;
1221 }
1222 }
1223 }
Owen Taylor3473f882001-02-23 17:55:21 +00001224 q = cur;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001225 if ((cur + 2 < end) && (cur[1] == '#') && (cur[2] == 'x')) {
1226 cur += 3;
1227 if (cur < end)
1228 tmp = *cur;
1229 else
1230 tmp = 0;
1231 while (tmp != ';') { /* Non input consuming loop */
1232 if ((tmp >= '0') && (tmp <= '9'))
1233 charval = charval * 16 + (tmp - '0');
1234 else if ((tmp >= 'a') && (tmp <= 'f'))
1235 charval = charval * 16 + (tmp - 'a') + 10;
1236 else if ((tmp >= 'A') && (tmp <= 'F'))
1237 charval = charval * 16 + (tmp - 'A') + 10;
Owen Taylor3473f882001-02-23 17:55:21 +00001238 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001239 xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
1240 NULL);
Daniel Veillard07cb8222003-09-10 10:51:05 +00001241 charval = 0;
1242 break;
1243 }
1244 cur++;
1245 if (cur < end)
1246 tmp = *cur;
1247 else
1248 tmp = 0;
1249 }
1250 if (tmp == ';')
1251 cur++;
1252 q = cur;
1253 } else if ((cur + 1 < end) && (cur[1] == '#')) {
1254 cur += 2;
1255 if (cur < end)
1256 tmp = *cur;
1257 else
1258 tmp = 0;
1259 while (tmp != ';') { /* Non input consuming loops */
1260 if ((tmp >= '0') && (tmp <= '9'))
1261 charval = charval * 10 + (tmp - '0');
1262 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001263 xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
1264 NULL);
Daniel Veillard07cb8222003-09-10 10:51:05 +00001265 charval = 0;
1266 break;
1267 }
1268 cur++;
1269 if (cur < end)
1270 tmp = *cur;
1271 else
1272 tmp = 0;
1273 }
1274 if (tmp == ';')
1275 cur++;
1276 q = cur;
1277 } else {
1278 /*
1279 * Read the entity string
1280 */
1281 cur++;
1282 q = cur;
1283 while ((cur < end) && (*cur != 0) && (*cur != ';')) cur++;
1284 if ((cur >= end) || (*cur == 0)) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001285 xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY, (xmlNodePtr) doc,
1286 (const char *) q);
Daniel Veillard07cb8222003-09-10 10:51:05 +00001287 return(ret);
1288 }
1289 if (cur != q) {
1290 /*
1291 * Predefined entities don't generate nodes
1292 */
1293 val = xmlStrndup(q, cur - q);
1294 ent = xmlGetDocEntity(doc, val);
1295 if ((ent != NULL) &&
1296 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
1297 if (last == NULL) {
1298 node = xmlNewDocText(doc, ent->content);
1299 last = ret = node;
1300 } else if (last->type != XML_TEXT_NODE) {
1301 node = xmlNewDocText(doc, ent->content);
1302 last = xmlAddNextSibling(last, node);
1303 } else
1304 xmlNodeAddContent(last, ent->content);
1305
1306 } else {
1307 /*
1308 * Create a new REFERENCE_REF node
1309 */
1310 node = xmlNewReference(doc, val);
1311 if (node == NULL) {
1312 if (val != NULL) xmlFree(val);
1313 return(ret);
1314 }
1315 else if ((ent != NULL) && (ent->children == NULL)) {
1316 xmlNodePtr temp;
1317
1318 ent->children = xmlStringGetNodeList(doc,
1319 (const xmlChar*)node->content);
1320 ent->owner = 1;
1321 temp = ent->children;
1322 while (temp) {
1323 temp->parent = (xmlNodePtr)ent;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001324 ent->last = temp;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001325 temp = temp->next;
1326 }
1327 }
1328 if (last == NULL) {
1329 last = ret = node;
1330 } else {
1331 last = xmlAddNextSibling(last, node);
1332 }
1333 }
1334 xmlFree(val);
1335 }
1336 cur++;
1337 q = cur;
1338 }
1339 if (charval != 0) {
1340 xmlChar buf[10];
1341 int l;
1342
1343 l = xmlCopyCharMultiByte(buf, charval);
1344 buf[l] = 0;
1345 node = xmlNewDocText(doc, buf);
1346 if (node != NULL) {
1347 if (last == NULL) {
1348 last = ret = node;
1349 } else {
1350 last = xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001351 }
1352 }
Daniel Veillard07cb8222003-09-10 10:51:05 +00001353 charval = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001354 }
Daniel Veillard07cb8222003-09-10 10:51:05 +00001355 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001356 cur++;
1357 }
Daniel Veillard07cb8222003-09-10 10:51:05 +00001358 if ((cur != q) || (ret == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001359 /*
1360 * Handle the last piece of text.
1361 */
1362 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
1363 xmlNodeAddContentLen(last, q, cur - q);
1364 } else {
1365 node = xmlNewDocTextLen(doc, q, cur - q);
1366 if (node == NULL) return(ret);
Daniel Veillard07cb8222003-09-10 10:51:05 +00001367 if (last == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001368 last = ret = node;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001369 } else {
1370 last = xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001371 }
1372 }
1373 }
1374 return(ret);
1375}
1376
1377/**
1378 * xmlStringGetNodeList:
1379 * @doc: the document
1380 * @value: the value of the attribute
1381 *
1382 * Parse the value string and build the node list associated. Should
1383 * produce a flat tree with only TEXTs and ENTITY_REFs.
1384 * Returns a pointer to the first child
1385 */
1386xmlNodePtr
1387xmlStringGetNodeList(xmlDocPtr doc, const xmlChar *value) {
1388 xmlNodePtr ret = NULL, last = NULL;
1389 xmlNodePtr node;
1390 xmlChar *val;
1391 const xmlChar *cur = value;
1392 const xmlChar *q;
1393 xmlEntityPtr ent;
1394
1395 if (value == NULL) return(NULL);
1396
1397 q = cur;
1398 while (*cur != 0) {
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001399 if (cur[0] == '&') {
1400 int charval = 0;
1401 xmlChar tmp;
1402
Owen Taylor3473f882001-02-23 17:55:21 +00001403 /*
1404 * Save the current text.
1405 */
1406 if (cur != q) {
1407 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
1408 xmlNodeAddContentLen(last, q, cur - q);
1409 } else {
1410 node = xmlNewDocTextLen(doc, q, cur - q);
1411 if (node == NULL) return(ret);
1412 if (last == NULL)
1413 last = ret = node;
1414 else {
1415 last->next = node;
1416 node->prev = last;
1417 last = node;
1418 }
1419 }
1420 }
Owen Taylor3473f882001-02-23 17:55:21 +00001421 q = cur;
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001422 if ((cur[1] == '#') && (cur[2] == 'x')) {
1423 cur += 3;
1424 tmp = *cur;
1425 while (tmp != ';') { /* Non input consuming loop */
1426 if ((tmp >= '0') && (tmp <= '9'))
1427 charval = charval * 16 + (tmp - '0');
1428 else if ((tmp >= 'a') && (tmp <= 'f'))
1429 charval = charval * 16 + (tmp - 'a') + 10;
1430 else if ((tmp >= 'A') && (tmp <= 'F'))
1431 charval = charval * 16 + (tmp - 'A') + 10;
Owen Taylor3473f882001-02-23 17:55:21 +00001432 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001433 xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
1434 NULL);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001435 charval = 0;
1436 break;
1437 }
1438 cur++;
1439 tmp = *cur;
1440 }
1441 if (tmp == ';')
1442 cur++;
1443 q = cur;
1444 } else if (cur[1] == '#') {
1445 cur += 2;
1446 tmp = *cur;
1447 while (tmp != ';') { /* Non input consuming loops */
1448 if ((tmp >= '0') && (tmp <= '9'))
1449 charval = charval * 10 + (tmp - '0');
1450 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001451 xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
1452 NULL);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001453 charval = 0;
1454 break;
1455 }
1456 cur++;
1457 tmp = *cur;
1458 }
1459 if (tmp == ';')
1460 cur++;
1461 q = cur;
1462 } else {
1463 /*
1464 * Read the entity string
1465 */
1466 cur++;
1467 q = cur;
1468 while ((*cur != 0) && (*cur != ';')) cur++;
1469 if (*cur == 0) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001470 xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY,
1471 (xmlNodePtr) doc, (const char *) q);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001472 return(ret);
1473 }
1474 if (cur != q) {
1475 /*
1476 * Predefined entities don't generate nodes
1477 */
1478 val = xmlStrndup(q, cur - q);
1479 ent = xmlGetDocEntity(doc, val);
1480 if ((ent != NULL) &&
1481 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
1482 if (last == NULL) {
1483 node = xmlNewDocText(doc, ent->content);
1484 last = ret = node;
Daniel Veillard6f42c132002-01-06 23:05:13 +00001485 } else if (last->type != XML_TEXT_NODE) {
1486 node = xmlNewDocText(doc, ent->content);
1487 last = xmlAddNextSibling(last, node);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001488 } else
1489 xmlNodeAddContent(last, ent->content);
1490
1491 } else {
1492 /*
1493 * Create a new REFERENCE_REF node
1494 */
1495 node = xmlNewReference(doc, val);
1496 if (node == NULL) {
1497 if (val != NULL) xmlFree(val);
1498 return(ret);
1499 }
Daniel Veillardbf8dae82002-04-18 16:39:10 +00001500 else if ((ent != NULL) && (ent->children == NULL)) {
1501 xmlNodePtr temp;
1502
1503 ent->children = xmlStringGetNodeList(doc,
1504 (const xmlChar*)node->content);
Daniel Veillard2d84a892002-12-30 00:01:08 +00001505 ent->owner = 1;
Daniel Veillardbf8dae82002-04-18 16:39:10 +00001506 temp = ent->children;
1507 while (temp) {
1508 temp->parent = (xmlNodePtr)ent;
1509 temp = temp->next;
1510 }
1511 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001512 if (last == NULL) {
1513 last = ret = node;
1514 } else {
1515 last = xmlAddNextSibling(last, node);
1516 }
1517 }
1518 xmlFree(val);
1519 }
1520 cur++;
1521 q = cur;
1522 }
1523 if (charval != 0) {
1524 xmlChar buf[10];
1525 int len;
1526
1527 len = xmlCopyCharMultiByte(buf, charval);
1528 buf[len] = 0;
1529 node = xmlNewDocText(doc, buf);
1530 if (node != NULL) {
1531 if (last == NULL) {
1532 last = ret = node;
1533 } else {
1534 last = xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001535 }
1536 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001537
1538 charval = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001539 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001540 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001541 cur++;
1542 }
Daniel Veillard75bea542001-05-11 17:41:21 +00001543 if ((cur != q) || (ret == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001544 /*
1545 * Handle the last piece of text.
1546 */
1547 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
1548 xmlNodeAddContentLen(last, q, cur - q);
1549 } else {
1550 node = xmlNewDocTextLen(doc, q, cur - q);
1551 if (node == NULL) return(ret);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001552 if (last == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001553 last = ret = node;
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001554 } else {
1555 last = xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001556 }
1557 }
1558 }
1559 return(ret);
1560}
1561
1562/**
1563 * xmlNodeListGetString:
1564 * @doc: the document
1565 * @list: a Node list
1566 * @inLine: should we replace entity contents or show their external form
1567 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001568 * Build the string equivalent to the text contained in the Node list
Owen Taylor3473f882001-02-23 17:55:21 +00001569 * made of TEXTs and ENTITY_REFs
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001570 *
Daniel Veillardbd9afb52002-09-25 22:25:35 +00001571 * Returns a pointer to the string copy, the caller must free it with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00001572 */
1573xmlChar *
Daniel Veillard7646b182002-04-20 06:41:40 +00001574xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine)
1575{
Owen Taylor3473f882001-02-23 17:55:21 +00001576 xmlNodePtr node = list;
1577 xmlChar *ret = NULL;
1578 xmlEntityPtr ent;
1579
Daniel Veillard7646b182002-04-20 06:41:40 +00001580 if (list == NULL)
1581 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001582
1583 while (node != NULL) {
1584 if ((node->type == XML_TEXT_NODE) ||
Daniel Veillard7646b182002-04-20 06:41:40 +00001585 (node->type == XML_CDATA_SECTION_NODE)) {
1586 if (inLine) {
1587 ret = xmlStrcat(ret, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001588 } else {
Daniel Veillard7646b182002-04-20 06:41:40 +00001589 xmlChar *buffer;
Owen Taylor3473f882001-02-23 17:55:21 +00001590
Daniel Veillard7646b182002-04-20 06:41:40 +00001591 buffer = xmlEncodeEntitiesReentrant(doc, node->content);
1592 if (buffer != NULL) {
1593 ret = xmlStrcat(ret, buffer);
1594 xmlFree(buffer);
1595 }
1596 }
1597 } else if (node->type == XML_ENTITY_REF_NODE) {
1598 if (inLine) {
1599 ent = xmlGetDocEntity(doc, node->name);
1600 if (ent != NULL) {
1601 xmlChar *buffer;
1602
1603 /* an entity content can be any "well balanced chunk",
1604 * i.e. the result of the content [43] production:
1605 * http://www.w3.org/TR/REC-xml#NT-content.
1606 * So it can contain text, CDATA section or nested
1607 * entity reference nodes (among others).
1608 * -> we recursive call xmlNodeListGetString()
1609 * which handles these types */
1610 buffer = xmlNodeListGetString(doc, ent->children, 1);
1611 if (buffer != NULL) {
1612 ret = xmlStrcat(ret, buffer);
1613 xmlFree(buffer);
1614 }
1615 } else {
1616 ret = xmlStrcat(ret, node->content);
1617 }
1618 } else {
1619 xmlChar buf[2];
1620
1621 buf[0] = '&';
1622 buf[1] = 0;
1623 ret = xmlStrncat(ret, buf, 1);
1624 ret = xmlStrcat(ret, node->name);
1625 buf[0] = ';';
1626 buf[1] = 0;
1627 ret = xmlStrncat(ret, buf, 1);
1628 }
1629 }
1630#if 0
1631 else {
1632 xmlGenericError(xmlGenericErrorContext,
1633 "xmlGetNodeListString : invalid node type %d\n",
1634 node->type);
1635 }
1636#endif
1637 node = node->next;
1638 }
1639 return (ret);
1640}
Daniel Veillard652327a2003-09-29 18:02:38 +00001641
1642#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001643/**
1644 * xmlNodeListGetRawString:
1645 * @doc: the document
1646 * @list: a Node list
1647 * @inLine: should we replace entity contents or show their external form
1648 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001649 * Builds the string equivalent to the text contained in the Node list
Owen Taylor3473f882001-02-23 17:55:21 +00001650 * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString()
1651 * this function doesn't do any character encoding handling.
1652 *
Daniel Veillardbd9afb52002-09-25 22:25:35 +00001653 * Returns a pointer to the string copy, the caller must free it with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00001654 */
1655xmlChar *
Daniel Veillard7646b182002-04-20 06:41:40 +00001656xmlNodeListGetRawString(xmlDocPtr doc, xmlNodePtr list, int inLine)
1657{
Owen Taylor3473f882001-02-23 17:55:21 +00001658 xmlNodePtr node = list;
1659 xmlChar *ret = NULL;
1660 xmlEntityPtr ent;
1661
Daniel Veillard7646b182002-04-20 06:41:40 +00001662 if (list == NULL)
1663 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001664
1665 while (node != NULL) {
Daniel Veillard7db37732001-07-12 01:20:08 +00001666 if ((node->type == XML_TEXT_NODE) ||
Daniel Veillard7646b182002-04-20 06:41:40 +00001667 (node->type == XML_CDATA_SECTION_NODE)) {
1668 if (inLine) {
1669 ret = xmlStrcat(ret, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001670 } else {
Daniel Veillard7646b182002-04-20 06:41:40 +00001671 xmlChar *buffer;
1672
1673 buffer = xmlEncodeSpecialChars(doc, node->content);
1674 if (buffer != NULL) {
1675 ret = xmlStrcat(ret, buffer);
1676 xmlFree(buffer);
1677 }
1678 }
1679 } else if (node->type == XML_ENTITY_REF_NODE) {
1680 if (inLine) {
1681 ent = xmlGetDocEntity(doc, node->name);
1682 if (ent != NULL) {
1683 xmlChar *buffer;
1684
1685 /* an entity content can be any "well balanced chunk",
1686 * i.e. the result of the content [43] production:
1687 * http://www.w3.org/TR/REC-xml#NT-content.
1688 * So it can contain text, CDATA section or nested
1689 * entity reference nodes (among others).
1690 * -> we recursive call xmlNodeListGetRawString()
1691 * which handles these types */
1692 buffer =
1693 xmlNodeListGetRawString(doc, ent->children, 1);
1694 if (buffer != NULL) {
1695 ret = xmlStrcat(ret, buffer);
1696 xmlFree(buffer);
1697 }
1698 } else {
1699 ret = xmlStrcat(ret, node->content);
1700 }
1701 } else {
1702 xmlChar buf[2];
1703
1704 buf[0] = '&';
1705 buf[1] = 0;
1706 ret = xmlStrncat(ret, buf, 1);
1707 ret = xmlStrcat(ret, node->name);
1708 buf[0] = ';';
1709 buf[1] = 0;
1710 ret = xmlStrncat(ret, buf, 1);
1711 }
1712 }
Owen Taylor3473f882001-02-23 17:55:21 +00001713#if 0
Daniel Veillard7646b182002-04-20 06:41:40 +00001714 else {
1715 xmlGenericError(xmlGenericErrorContext,
1716 "xmlGetNodeListString : invalid node type %d\n",
1717 node->type);
1718 }
Owen Taylor3473f882001-02-23 17:55:21 +00001719#endif
Daniel Veillard7646b182002-04-20 06:41:40 +00001720 node = node->next;
Owen Taylor3473f882001-02-23 17:55:21 +00001721 }
Daniel Veillard7646b182002-04-20 06:41:40 +00001722 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00001723}
Daniel Veillard652327a2003-09-29 18:02:38 +00001724#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001725
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001726static xmlAttrPtr xmlNewPropInternal(xmlNodePtr node, xmlNsPtr ns,
1727 const xmlChar *name, const xmlChar *value, int eatname) {
Owen Taylor3473f882001-02-23 17:55:21 +00001728 xmlAttrPtr cur;
1729 xmlDocPtr doc = NULL;
1730
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001731 if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) {
1732 if (eatname == 1)
1733 xmlFree((xmlChar *) name);
1734 return(NULL);
1735 }
Owen Taylor3473f882001-02-23 17:55:21 +00001736
1737 /*
1738 * Allocate a new property and fill the fields.
1739 */
1740 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1741 if (cur == NULL) {
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001742 if (eatname == 1)
1743 xmlFree((xmlChar *) name);
1744 xmlTreeErrMemory("building attribute");
1745 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001746 }
1747 memset(cur, 0, sizeof(xmlAttr));
1748 cur->type = XML_ATTRIBUTE_NODE;
1749
1750 cur->parent = node;
1751 if (node != NULL) {
1752 doc = node->doc;
1753 cur->doc = doc;
1754 }
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001755 cur->ns = ns;
1756
1757 if (eatname == 0) {
1758 if ((doc != NULL) && (doc->dict != NULL))
1759 cur->name = (xmlChar *) xmlDictLookup(doc->dict, name, -1);
1760 else
1761 cur->name = xmlStrdup(name);
1762 } else
1763 cur->name = name;
1764
Owen Taylor3473f882001-02-23 17:55:21 +00001765 if (value != NULL) {
1766 xmlChar *buffer;
1767 xmlNodePtr tmp;
1768
1769 buffer = xmlEncodeEntitiesReentrant(doc, value);
1770 cur->children = xmlStringGetNodeList(doc, buffer);
1771 cur->last = NULL;
1772 tmp = cur->children;
1773 while (tmp != NULL) {
1774 tmp->parent = (xmlNodePtr) cur;
Owen Taylor3473f882001-02-23 17:55:21 +00001775 if (tmp->next == NULL)
1776 cur->last = tmp;
1777 tmp = tmp->next;
1778 }
1779 xmlFree(buffer);
1780 }
1781
1782 /*
1783 * Add it at the end to preserve parsing order ...
1784 */
1785 if (node != NULL) {
1786 if (node->properties == NULL) {
1787 node->properties = cur;
1788 } else {
1789 xmlAttrPtr prev = node->properties;
1790
1791 while (prev->next != NULL) prev = prev->next;
1792 prev->next = cur;
1793 cur->prev = prev;
1794 }
1795 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00001796
Daniel Veillarda880b122003-04-21 21:36:41 +00001797 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001798 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001799 return(cur);
1800}
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001801
1802#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
1803 defined(LIBXML_SCHEMAS_ENABLED)
1804/**
1805 * xmlNewProp:
1806 * @node: the holding node
1807 * @name: the name of the attribute
1808 * @value: the value of the attribute
1809 *
1810 * Create a new property carried by a node.
1811 * Returns a pointer to the attribute
1812 */
1813xmlAttrPtr
1814xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
1815
1816 if (name == NULL) {
1817#ifdef DEBUG_TREE
1818 xmlGenericError(xmlGenericErrorContext,
1819 "xmlNewProp : name == NULL\n");
1820#endif
1821 return(NULL);
1822 }
1823
1824 return xmlNewPropInternal(node, NULL, name, value, 0);
1825}
Daniel Veillard652327a2003-09-29 18:02:38 +00001826#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001827
1828/**
1829 * xmlNewNsProp:
1830 * @node: the holding node
1831 * @ns: the namespace
1832 * @name: the name of the attribute
1833 * @value: the value of the attribute
1834 *
1835 * Create a new property tagged with a namespace and carried by a node.
1836 * Returns a pointer to the attribute
1837 */
1838xmlAttrPtr
1839xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
1840 const xmlChar *value) {
Owen Taylor3473f882001-02-23 17:55:21 +00001841
1842 if (name == NULL) {
1843#ifdef DEBUG_TREE
1844 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00001845 "xmlNewNsProp : name == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001846#endif
1847 return(NULL);
1848 }
1849
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001850 return xmlNewPropInternal(node, ns, name, value, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001851}
1852
1853/**
Daniel Veillard46de64e2002-05-29 08:21:33 +00001854 * xmlNewNsPropEatName:
1855 * @node: the holding node
1856 * @ns: the namespace
1857 * @name: the name of the attribute
1858 * @value: the value of the attribute
1859 *
1860 * Create a new property tagged with a namespace and carried by a node.
1861 * Returns a pointer to the attribute
1862 */
1863xmlAttrPtr
1864xmlNewNsPropEatName(xmlNodePtr node, xmlNsPtr ns, xmlChar *name,
1865 const xmlChar *value) {
Daniel Veillard46de64e2002-05-29 08:21:33 +00001866
1867 if (name == NULL) {
1868#ifdef DEBUG_TREE
1869 xmlGenericError(xmlGenericErrorContext,
1870 "xmlNewNsPropEatName : name == NULL\n");
1871#endif
1872 return(NULL);
1873 }
1874
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001875 return xmlNewPropInternal(node, ns, name, value, 1);
Daniel Veillard46de64e2002-05-29 08:21:33 +00001876}
1877
1878/**
Owen Taylor3473f882001-02-23 17:55:21 +00001879 * xmlNewDocProp:
1880 * @doc: the document
1881 * @name: the name of the attribute
1882 * @value: the value of the attribute
1883 *
1884 * Create a new property carried by a document.
1885 * Returns a pointer to the attribute
1886 */
1887xmlAttrPtr
1888xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) {
1889 xmlAttrPtr cur;
1890
1891 if (name == NULL) {
1892#ifdef DEBUG_TREE
1893 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00001894 "xmlNewDocProp : name == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001895#endif
1896 return(NULL);
1897 }
1898
1899 /*
1900 * Allocate a new property and fill the fields.
1901 */
1902 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1903 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001904 xmlTreeErrMemory("building attribute");
Owen Taylor3473f882001-02-23 17:55:21 +00001905 return(NULL);
1906 }
1907 memset(cur, 0, sizeof(xmlAttr));
1908 cur->type = XML_ATTRIBUTE_NODE;
1909
Daniel Veillard03a53c32004-10-26 16:06:51 +00001910 if ((doc != NULL) && (doc->dict != NULL))
1911 cur->name = xmlDictLookup(doc->dict, name, -1);
1912 else
1913 cur->name = xmlStrdup(name);
Owen Taylor3473f882001-02-23 17:55:21 +00001914 cur->doc = doc;
1915 if (value != NULL) {
1916 xmlNodePtr tmp;
1917
1918 cur->children = xmlStringGetNodeList(doc, value);
1919 cur->last = NULL;
1920
1921 tmp = cur->children;
1922 while (tmp != NULL) {
1923 tmp->parent = (xmlNodePtr) cur;
1924 if (tmp->next == NULL)
1925 cur->last = tmp;
1926 tmp = tmp->next;
1927 }
1928 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00001929
Daniel Veillarda880b122003-04-21 21:36:41 +00001930 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001931 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001932 return(cur);
1933}
1934
1935/**
1936 * xmlFreePropList:
1937 * @cur: the first property in the list
1938 *
1939 * Free a property and all its siblings, all the children are freed too.
1940 */
1941void
1942xmlFreePropList(xmlAttrPtr cur) {
1943 xmlAttrPtr next;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001944 if (cur == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +00001945 while (cur != NULL) {
1946 next = cur->next;
1947 xmlFreeProp(cur);
1948 cur = next;
1949 }
1950}
1951
1952/**
1953 * xmlFreeProp:
1954 * @cur: an attribute
1955 *
1956 * Free one attribute, all the content is freed too
1957 */
1958void
1959xmlFreeProp(xmlAttrPtr cur) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001960 xmlDictPtr dict = NULL;
1961 if (cur == NULL) return;
1962
1963 if (cur->doc != NULL) dict = cur->doc->dict;
Daniel Veillard5335dc52003-01-01 20:59:38 +00001964
Daniel Veillarda880b122003-04-21 21:36:41 +00001965 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001966 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
1967
Owen Taylor3473f882001-02-23 17:55:21 +00001968 /* Check for ID removal -> leading to invalid references ! */
Daniel Veillardda6f4af2005-06-20 17:17:54 +00001969 if ((cur->doc != NULL) && (cur->atype == XML_ATTRIBUTE_ID)) {
1970 xmlRemoveID(cur->doc, cur);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001971 }
Owen Taylor3473f882001-02-23 17:55:21 +00001972 if (cur->children != NULL) xmlFreeNodeList(cur->children);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001973 DICT_FREE(cur->name)
Owen Taylor3473f882001-02-23 17:55:21 +00001974 xmlFree(cur);
1975}
1976
Daniel Veillard652327a2003-09-29 18:02:38 +00001977#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001978/**
1979 * xmlRemoveProp:
1980 * @cur: an attribute
1981 *
1982 * Unlink and free one attribute, all the content is freed too
1983 * Note this doesn't work for namespace definition attributes
1984 *
1985 * Returns 0 if success and -1 in case of error.
1986 */
1987int
1988xmlRemoveProp(xmlAttrPtr cur) {
1989 xmlAttrPtr tmp;
1990 if (cur == NULL) {
1991#ifdef DEBUG_TREE
1992 xmlGenericError(xmlGenericErrorContext,
1993 "xmlRemoveProp : cur == NULL\n");
1994#endif
1995 return(-1);
1996 }
1997 if (cur->parent == NULL) {
1998#ifdef DEBUG_TREE
1999 xmlGenericError(xmlGenericErrorContext,
2000 "xmlRemoveProp : cur->parent == NULL\n");
2001#endif
2002 return(-1);
2003 }
2004 tmp = cur->parent->properties;
2005 if (tmp == cur) {
2006 cur->parent->properties = cur->next;
2007 xmlFreeProp(cur);
2008 return(0);
2009 }
2010 while (tmp != NULL) {
2011 if (tmp->next == cur) {
2012 tmp->next = cur->next;
2013 if (tmp->next != NULL)
2014 tmp->next->prev = tmp;
2015 xmlFreeProp(cur);
2016 return(0);
2017 }
2018 tmp = tmp->next;
2019 }
2020#ifdef DEBUG_TREE
2021 xmlGenericError(xmlGenericErrorContext,
2022 "xmlRemoveProp : attribute not owned by its node\n");
2023#endif
2024 return(-1);
2025}
Daniel Veillard652327a2003-09-29 18:02:38 +00002026#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002027
2028/**
Daniel Veillard03a53c32004-10-26 16:06:51 +00002029 * xmlNewDocPI:
2030 * @doc: the target document
Owen Taylor3473f882001-02-23 17:55:21 +00002031 * @name: the processing instruction name
2032 * @content: the PI content
2033 *
2034 * Creation of a processing instruction element.
2035 * Returns a pointer to the new node object.
2036 */
2037xmlNodePtr
Daniel Veillard03a53c32004-10-26 16:06:51 +00002038xmlNewDocPI(xmlDocPtr doc, const xmlChar *name, const xmlChar *content) {
Owen Taylor3473f882001-02-23 17:55:21 +00002039 xmlNodePtr cur;
2040
2041 if (name == NULL) {
2042#ifdef DEBUG_TREE
2043 xmlGenericError(xmlGenericErrorContext,
2044 "xmlNewPI : name == NULL\n");
2045#endif
2046 return(NULL);
2047 }
2048
2049 /*
2050 * Allocate a new node and fill the fields.
2051 */
2052 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2053 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002054 xmlTreeErrMemory("building PI");
Owen Taylor3473f882001-02-23 17:55:21 +00002055 return(NULL);
2056 }
2057 memset(cur, 0, sizeof(xmlNode));
2058 cur->type = XML_PI_NODE;
2059
Daniel Veillard03a53c32004-10-26 16:06:51 +00002060 if ((doc != NULL) && (doc->dict != NULL))
2061 cur->name = xmlDictLookup(doc->dict, name, -1);
2062 else
2063 cur->name = xmlStrdup(name);
Owen Taylor3473f882001-02-23 17:55:21 +00002064 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002065 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00002066 }
Daniel Veillarda03e3652004-11-02 18:45:30 +00002067 cur->doc = doc;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002068
Daniel Veillarda880b122003-04-21 21:36:41 +00002069 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002070 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002071 return(cur);
2072}
2073
2074/**
Daniel Veillard03a53c32004-10-26 16:06:51 +00002075 * xmlNewPI:
2076 * @name: the processing instruction name
2077 * @content: the PI content
2078 *
2079 * Creation of a processing instruction element.
2080 * Use xmlDocNewPI preferably to get string interning
2081 *
2082 * Returns a pointer to the new node object.
2083 */
2084xmlNodePtr
2085xmlNewPI(const xmlChar *name, const xmlChar *content) {
2086 return(xmlNewDocPI(NULL, name, content));
2087}
2088
2089/**
Owen Taylor3473f882001-02-23 17:55:21 +00002090 * xmlNewNode:
2091 * @ns: namespace if any
2092 * @name: the node name
2093 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002094 * Creation of a new node element. @ns is optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00002095 *
William M. Brackd7cf7f82003-11-14 07:13:16 +00002096 * Returns a pointer to the new node object. Uses xmlStrdup() to make
2097 * copy of @name.
Owen Taylor3473f882001-02-23 17:55:21 +00002098 */
2099xmlNodePtr
2100xmlNewNode(xmlNsPtr ns, const xmlChar *name) {
2101 xmlNodePtr cur;
2102
2103 if (name == NULL) {
2104#ifdef DEBUG_TREE
2105 xmlGenericError(xmlGenericErrorContext,
2106 "xmlNewNode : name == NULL\n");
2107#endif
2108 return(NULL);
2109 }
2110
2111 /*
2112 * Allocate a new node and fill the fields.
2113 */
2114 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2115 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002116 xmlTreeErrMemory("building node");
Owen Taylor3473f882001-02-23 17:55:21 +00002117 return(NULL);
2118 }
2119 memset(cur, 0, sizeof(xmlNode));
2120 cur->type = XML_ELEMENT_NODE;
2121
2122 cur->name = xmlStrdup(name);
2123 cur->ns = ns;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002124
Daniel Veillarda880b122003-04-21 21:36:41 +00002125 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002126 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002127 return(cur);
2128}
2129
2130/**
Daniel Veillard46de64e2002-05-29 08:21:33 +00002131 * xmlNewNodeEatName:
2132 * @ns: namespace if any
2133 * @name: the node name
2134 *
2135 * Creation of a new node element. @ns is optional (NULL).
2136 *
William M. Brackd7cf7f82003-11-14 07:13:16 +00002137 * Returns a pointer to the new node object, with pointer @name as
2138 * new node's name. Use xmlNewNode() if a copy of @name string is
2139 * is needed as new node's name.
Daniel Veillard46de64e2002-05-29 08:21:33 +00002140 */
2141xmlNodePtr
2142xmlNewNodeEatName(xmlNsPtr ns, xmlChar *name) {
2143 xmlNodePtr cur;
2144
2145 if (name == NULL) {
2146#ifdef DEBUG_TREE
2147 xmlGenericError(xmlGenericErrorContext,
2148 "xmlNewNode : name == NULL\n");
2149#endif
2150 return(NULL);
2151 }
2152
2153 /*
2154 * Allocate a new node and fill the fields.
2155 */
2156 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2157 if (cur == NULL) {
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00002158 xmlFree(name);
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002159 xmlTreeErrMemory("building node");
Daniel Veillard46de64e2002-05-29 08:21:33 +00002160 return(NULL);
2161 }
2162 memset(cur, 0, sizeof(xmlNode));
2163 cur->type = XML_ELEMENT_NODE;
2164
2165 cur->name = name;
2166 cur->ns = ns;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002167
Daniel Veillarda880b122003-04-21 21:36:41 +00002168 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002169 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Daniel Veillard46de64e2002-05-29 08:21:33 +00002170 return(cur);
2171}
2172
2173/**
Owen Taylor3473f882001-02-23 17:55:21 +00002174 * xmlNewDocNode:
2175 * @doc: the document
2176 * @ns: namespace if any
2177 * @name: the node name
2178 * @content: the XML text content if any
2179 *
2180 * Creation of a new node element within a document. @ns and @content
Daniel Veillardd1640922001-12-17 15:30:10 +00002181 * are optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00002182 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2183 * references, but XML special chars need to be escaped first by using
2184 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2185 * need entities support.
2186 *
2187 * Returns a pointer to the new node object.
2188 */
2189xmlNodePtr
2190xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns,
2191 const xmlChar *name, const xmlChar *content) {
2192 xmlNodePtr cur;
2193
Daniel Veillard95ddcd32004-10-26 21:53:55 +00002194 if ((doc != NULL) && (doc->dict != NULL))
Daniel Veillard03a53c32004-10-26 16:06:51 +00002195 cur = xmlNewNodeEatName(ns, (xmlChar *)
2196 xmlDictLookup(doc->dict, name, -1));
2197 else
2198 cur = xmlNewNode(ns, name);
Owen Taylor3473f882001-02-23 17:55:21 +00002199 if (cur != NULL) {
2200 cur->doc = doc;
2201 if (content != NULL) {
2202 cur->children = xmlStringGetNodeList(doc, content);
2203 UPDATE_LAST_CHILD_AND_PARENT(cur)
2204 }
2205 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002206
Owen Taylor3473f882001-02-23 17:55:21 +00002207 return(cur);
2208}
2209
Daniel Veillard46de64e2002-05-29 08:21:33 +00002210/**
2211 * xmlNewDocNodeEatName:
2212 * @doc: the document
2213 * @ns: namespace if any
2214 * @name: the node name
2215 * @content: the XML text content if any
2216 *
2217 * Creation of a new node element within a document. @ns and @content
2218 * are optional (NULL).
2219 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2220 * references, but XML special chars need to be escaped first by using
2221 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2222 * need entities support.
2223 *
2224 * Returns a pointer to the new node object.
2225 */
2226xmlNodePtr
2227xmlNewDocNodeEatName(xmlDocPtr doc, xmlNsPtr ns,
2228 xmlChar *name, const xmlChar *content) {
2229 xmlNodePtr cur;
2230
2231 cur = xmlNewNodeEatName(ns, name);
2232 if (cur != NULL) {
2233 cur->doc = doc;
2234 if (content != NULL) {
2235 cur->children = xmlStringGetNodeList(doc, content);
2236 UPDATE_LAST_CHILD_AND_PARENT(cur)
2237 }
2238 }
2239 return(cur);
2240}
2241
Daniel Veillard652327a2003-09-29 18:02:38 +00002242#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002243/**
2244 * xmlNewDocRawNode:
2245 * @doc: the document
2246 * @ns: namespace if any
2247 * @name: the node name
2248 * @content: the text content if any
2249 *
2250 * Creation of a new node element within a document. @ns and @content
Daniel Veillardd1640922001-12-17 15:30:10 +00002251 * are optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00002252 *
2253 * Returns a pointer to the new node object.
2254 */
2255xmlNodePtr
2256xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns,
2257 const xmlChar *name, const xmlChar *content) {
2258 xmlNodePtr cur;
2259
Daniel Veillard95ddcd32004-10-26 21:53:55 +00002260 cur = xmlNewDocNode(doc, ns, name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002261 if (cur != NULL) {
2262 cur->doc = doc;
2263 if (content != NULL) {
2264 cur->children = xmlNewDocText(doc, content);
2265 UPDATE_LAST_CHILD_AND_PARENT(cur)
2266 }
2267 }
2268 return(cur);
2269}
2270
2271/**
2272 * xmlNewDocFragment:
2273 * @doc: the document owning the fragment
2274 *
2275 * Creation of a new Fragment node.
2276 * Returns a pointer to the new node object.
2277 */
2278xmlNodePtr
2279xmlNewDocFragment(xmlDocPtr doc) {
2280 xmlNodePtr cur;
2281
2282 /*
2283 * Allocate a new DocumentFragment node and fill the fields.
2284 */
2285 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2286 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002287 xmlTreeErrMemory("building fragment");
Owen Taylor3473f882001-02-23 17:55:21 +00002288 return(NULL);
2289 }
2290 memset(cur, 0, sizeof(xmlNode));
2291 cur->type = XML_DOCUMENT_FRAG_NODE;
2292
2293 cur->doc = doc;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002294
Daniel Veillarda880b122003-04-21 21:36:41 +00002295 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002296 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002297 return(cur);
2298}
Daniel Veillard652327a2003-09-29 18:02:38 +00002299#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002300
2301/**
2302 * xmlNewText:
2303 * @content: the text content
2304 *
2305 * Creation of a new text node.
2306 * Returns a pointer to the new node object.
2307 */
2308xmlNodePtr
2309xmlNewText(const xmlChar *content) {
2310 xmlNodePtr cur;
2311
2312 /*
2313 * Allocate a new node and fill the fields.
2314 */
2315 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2316 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002317 xmlTreeErrMemory("building text");
Owen Taylor3473f882001-02-23 17:55:21 +00002318 return(NULL);
2319 }
2320 memset(cur, 0, sizeof(xmlNode));
2321 cur->type = XML_TEXT_NODE;
2322
2323 cur->name = xmlStringText;
2324 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002325 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00002326 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002327
Daniel Veillarda880b122003-04-21 21:36:41 +00002328 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002329 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002330 return(cur);
2331}
2332
Daniel Veillard652327a2003-09-29 18:02:38 +00002333#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002334/**
2335 * xmlNewTextChild:
2336 * @parent: the parent node
2337 * @ns: a namespace if any
2338 * @name: the name of the child
2339 * @content: the text content of the child if any.
2340 *
2341 * Creation of a new child element, added at the end of @parent children list.
MST 2003 John Flecke1f70492003-12-20 23:43:28 +00002342 * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
2343 * created element inherits the namespace of @parent. If @content is non NULL,
William M. Brackd7cf7f82003-11-14 07:13:16 +00002344 * a child TEXT node will be created containing the string @content.
MST 2003 John Fleck8b03bc52003-12-17 03:45:01 +00002345 * NOTE: Use xmlNewChild() if @content will contain entities that need to be
2346 * preserved. Use this function, xmlNewTextChild(), if you need to ensure that
2347 * reserved XML chars that might appear in @content, such as the ampersand,
2348 * greater-than or less-than signs, are automatically replaced by their XML
2349 * escaped entity representations.
Owen Taylor3473f882001-02-23 17:55:21 +00002350 *
2351 * Returns a pointer to the new node object.
2352 */
2353xmlNodePtr
2354xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns,
2355 const xmlChar *name, const xmlChar *content) {
2356 xmlNodePtr cur, prev;
2357
2358 if (parent == NULL) {
2359#ifdef DEBUG_TREE
2360 xmlGenericError(xmlGenericErrorContext,
2361 "xmlNewTextChild : parent == NULL\n");
2362#endif
2363 return(NULL);
2364 }
2365
2366 if (name == NULL) {
2367#ifdef DEBUG_TREE
2368 xmlGenericError(xmlGenericErrorContext,
2369 "xmlNewTextChild : name == NULL\n");
2370#endif
2371 return(NULL);
2372 }
2373
2374 /*
2375 * Allocate a new node
2376 */
Daniel Veillard254b1262003-11-01 17:04:58 +00002377 if (parent->type == XML_ELEMENT_NODE) {
2378 if (ns == NULL)
2379 cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content);
2380 else
2381 cur = xmlNewDocRawNode(parent->doc, ns, name, content);
2382 } else if ((parent->type == XML_DOCUMENT_NODE) ||
2383 (parent->type == XML_HTML_DOCUMENT_NODE)) {
2384 if (ns == NULL)
2385 cur = xmlNewDocRawNode((xmlDocPtr) parent, NULL, name, content);
2386 else
2387 cur = xmlNewDocRawNode((xmlDocPtr) parent, ns, name, content);
2388 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2389 cur = xmlNewDocRawNode( parent->doc, ns, name, content);
2390 } else {
2391 return(NULL);
2392 }
Owen Taylor3473f882001-02-23 17:55:21 +00002393 if (cur == NULL) return(NULL);
2394
2395 /*
2396 * add the new element at the end of the children list.
2397 */
2398 cur->type = XML_ELEMENT_NODE;
2399 cur->parent = parent;
2400 cur->doc = parent->doc;
2401 if (parent->children == NULL) {
2402 parent->children = cur;
2403 parent->last = cur;
2404 } else {
2405 prev = parent->last;
2406 prev->next = cur;
2407 cur->prev = prev;
2408 parent->last = cur;
2409 }
2410
2411 return(cur);
2412}
Daniel Veillard652327a2003-09-29 18:02:38 +00002413#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002414
2415/**
2416 * xmlNewCharRef:
2417 * @doc: the document
2418 * @name: the char ref string, starting with # or "&# ... ;"
2419 *
2420 * Creation of a new character reference node.
2421 * Returns a pointer to the new node object.
2422 */
2423xmlNodePtr
2424xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) {
2425 xmlNodePtr cur;
2426
Daniel Veillard36e5cd52004-11-02 14:52:23 +00002427 if (name == NULL)
2428 return(NULL);
2429
Owen Taylor3473f882001-02-23 17:55:21 +00002430 /*
2431 * Allocate a new node and fill the fields.
2432 */
2433 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2434 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002435 xmlTreeErrMemory("building character reference");
Owen Taylor3473f882001-02-23 17:55:21 +00002436 return(NULL);
2437 }
2438 memset(cur, 0, sizeof(xmlNode));
2439 cur->type = XML_ENTITY_REF_NODE;
2440
2441 cur->doc = doc;
2442 if (name[0] == '&') {
2443 int len;
2444 name++;
2445 len = xmlStrlen(name);
2446 if (name[len - 1] == ';')
2447 cur->name = xmlStrndup(name, len - 1);
2448 else
2449 cur->name = xmlStrndup(name, len);
2450 } else
2451 cur->name = xmlStrdup(name);
Daniel Veillard5335dc52003-01-01 20:59:38 +00002452
Daniel Veillarda880b122003-04-21 21:36:41 +00002453 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002454 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002455 return(cur);
2456}
2457
2458/**
2459 * xmlNewReference:
2460 * @doc: the document
2461 * @name: the reference name, or the reference string with & and ;
2462 *
2463 * Creation of a new reference node.
2464 * Returns a pointer to the new node object.
2465 */
2466xmlNodePtr
2467xmlNewReference(xmlDocPtr doc, const xmlChar *name) {
2468 xmlNodePtr cur;
2469 xmlEntityPtr ent;
2470
Daniel Veillard36e5cd52004-11-02 14:52:23 +00002471 if (name == NULL)
2472 return(NULL);
2473
Owen Taylor3473f882001-02-23 17:55:21 +00002474 /*
2475 * Allocate a new node and fill the fields.
2476 */
2477 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2478 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002479 xmlTreeErrMemory("building reference");
Owen Taylor3473f882001-02-23 17:55:21 +00002480 return(NULL);
2481 }
2482 memset(cur, 0, sizeof(xmlNode));
2483 cur->type = XML_ENTITY_REF_NODE;
2484
2485 cur->doc = doc;
2486 if (name[0] == '&') {
2487 int len;
2488 name++;
2489 len = xmlStrlen(name);
2490 if (name[len - 1] == ';')
2491 cur->name = xmlStrndup(name, len - 1);
2492 else
2493 cur->name = xmlStrndup(name, len);
2494 } else
2495 cur->name = xmlStrdup(name);
2496
2497 ent = xmlGetDocEntity(doc, cur->name);
2498 if (ent != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002499 cur->content = ent->content;
Owen Taylor3473f882001-02-23 17:55:21 +00002500 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002501 * The parent pointer in entity is a DTD pointer and thus is NOT
Owen Taylor3473f882001-02-23 17:55:21 +00002502 * updated. Not sure if this is 100% correct.
2503 * -George
2504 */
2505 cur->children = (xmlNodePtr) ent;
2506 cur->last = (xmlNodePtr) ent;
2507 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002508
Daniel Veillarda880b122003-04-21 21:36:41 +00002509 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002510 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002511 return(cur);
2512}
2513
2514/**
2515 * xmlNewDocText:
2516 * @doc: the document
2517 * @content: the text content
2518 *
2519 * Creation of a new text node within a document.
2520 * Returns a pointer to the new node object.
2521 */
2522xmlNodePtr
2523xmlNewDocText(xmlDocPtr doc, const xmlChar *content) {
2524 xmlNodePtr cur;
2525
2526 cur = xmlNewText(content);
2527 if (cur != NULL) cur->doc = doc;
2528 return(cur);
2529}
2530
2531/**
2532 * xmlNewTextLen:
2533 * @content: the text content
2534 * @len: the text len.
2535 *
Daniel Veillard60087f32001-10-10 09:45:09 +00002536 * Creation of a new text node with an extra parameter for the content's length
Owen Taylor3473f882001-02-23 17:55:21 +00002537 * Returns a pointer to the new node object.
2538 */
2539xmlNodePtr
2540xmlNewTextLen(const xmlChar *content, int len) {
2541 xmlNodePtr cur;
2542
2543 /*
2544 * Allocate a new node and fill the fields.
2545 */
2546 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2547 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002548 xmlTreeErrMemory("building text");
Owen Taylor3473f882001-02-23 17:55:21 +00002549 return(NULL);
2550 }
2551 memset(cur, 0, sizeof(xmlNode));
2552 cur->type = XML_TEXT_NODE;
2553
2554 cur->name = xmlStringText;
2555 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002556 cur->content = xmlStrndup(content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00002557 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002558
Daniel Veillarda880b122003-04-21 21:36:41 +00002559 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002560 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002561 return(cur);
2562}
2563
2564/**
2565 * xmlNewDocTextLen:
2566 * @doc: the document
2567 * @content: the text content
2568 * @len: the text len.
2569 *
Daniel Veillard60087f32001-10-10 09:45:09 +00002570 * Creation of a new text node with an extra content length parameter. The
Owen Taylor3473f882001-02-23 17:55:21 +00002571 * text node pertain to a given document.
2572 * Returns a pointer to the new node object.
2573 */
2574xmlNodePtr
2575xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) {
2576 xmlNodePtr cur;
2577
2578 cur = xmlNewTextLen(content, len);
2579 if (cur != NULL) cur->doc = doc;
2580 return(cur);
2581}
2582
2583/**
2584 * xmlNewComment:
2585 * @content: the comment content
2586 *
2587 * Creation of a new node containing a comment.
2588 * Returns a pointer to the new node object.
2589 */
2590xmlNodePtr
2591xmlNewComment(const xmlChar *content) {
2592 xmlNodePtr cur;
2593
2594 /*
2595 * Allocate a new node and fill the fields.
2596 */
2597 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2598 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002599 xmlTreeErrMemory("building comment");
Owen Taylor3473f882001-02-23 17:55:21 +00002600 return(NULL);
2601 }
2602 memset(cur, 0, sizeof(xmlNode));
2603 cur->type = XML_COMMENT_NODE;
2604
2605 cur->name = xmlStringComment;
2606 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002607 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00002608 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002609
Daniel Veillarda880b122003-04-21 21:36:41 +00002610 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002611 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002612 return(cur);
2613}
2614
2615/**
2616 * xmlNewCDataBlock:
2617 * @doc: the document
Daniel Veillardd1640922001-12-17 15:30:10 +00002618 * @content: the CDATA block content content
Owen Taylor3473f882001-02-23 17:55:21 +00002619 * @len: the length of the block
2620 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002621 * Creation of a new node containing a CDATA block.
Owen Taylor3473f882001-02-23 17:55:21 +00002622 * Returns a pointer to the new node object.
2623 */
2624xmlNodePtr
2625xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) {
2626 xmlNodePtr cur;
2627
2628 /*
2629 * Allocate a new node and fill the fields.
2630 */
2631 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2632 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002633 xmlTreeErrMemory("building CDATA");
Owen Taylor3473f882001-02-23 17:55:21 +00002634 return(NULL);
2635 }
2636 memset(cur, 0, sizeof(xmlNode));
2637 cur->type = XML_CDATA_SECTION_NODE;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002638 cur->doc = doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002639
2640 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002641 cur->content = xmlStrndup(content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00002642 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002643
Daniel Veillarda880b122003-04-21 21:36:41 +00002644 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002645 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002646 return(cur);
2647}
2648
2649/**
2650 * xmlNewDocComment:
2651 * @doc: the document
2652 * @content: the comment content
2653 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002654 * Creation of a new node containing a comment within a document.
Owen Taylor3473f882001-02-23 17:55:21 +00002655 * Returns a pointer to the new node object.
2656 */
2657xmlNodePtr
2658xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) {
2659 xmlNodePtr cur;
2660
2661 cur = xmlNewComment(content);
2662 if (cur != NULL) cur->doc = doc;
2663 return(cur);
2664}
2665
2666/**
2667 * xmlSetTreeDoc:
2668 * @tree: the top element
2669 * @doc: the document
2670 *
2671 * update all nodes under the tree to point to the right document
2672 */
2673void
2674xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) {
Daniel Veillard19e96c32001-07-09 10:32:59 +00002675 xmlAttrPtr prop;
2676
Owen Taylor3473f882001-02-23 17:55:21 +00002677 if (tree == NULL)
2678 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002679 if (tree->doc != doc) {
Daniel Veillard36065812002-01-24 15:02:46 +00002680 if(tree->type == XML_ELEMENT_NODE) {
2681 prop = tree->properties;
2682 while (prop != NULL) {
2683 prop->doc = doc;
2684 xmlSetListDoc(prop->children, doc);
2685 prop = prop->next;
2686 }
Daniel Veillard19e96c32001-07-09 10:32:59 +00002687 }
Owen Taylor3473f882001-02-23 17:55:21 +00002688 if (tree->children != NULL)
2689 xmlSetListDoc(tree->children, doc);
2690 tree->doc = doc;
2691 }
2692}
2693
2694/**
2695 * xmlSetListDoc:
Daniel Veillardd1640922001-12-17 15:30:10 +00002696 * @list: the first element
Owen Taylor3473f882001-02-23 17:55:21 +00002697 * @doc: the document
2698 *
2699 * update all nodes in the list to point to the right document
2700 */
2701void
2702xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) {
2703 xmlNodePtr cur;
2704
2705 if (list == NULL)
2706 return;
2707 cur = list;
2708 while (cur != NULL) {
2709 if (cur->doc != doc)
2710 xmlSetTreeDoc(cur, doc);
2711 cur = cur->next;
2712 }
2713}
2714
Daniel Veillard2156d432004-03-04 15:59:36 +00002715#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00002716/**
2717 * xmlNewChild:
2718 * @parent: the parent node
2719 * @ns: a namespace if any
2720 * @name: the name of the child
2721 * @content: the XML content of the child if any.
2722 *
2723 * Creation of a new child element, added at the end of @parent children list.
MST 2003 John Flecke1f70492003-12-20 23:43:28 +00002724 * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
2725 * created element inherits the namespace of @parent. If @content is non NULL,
Owen Taylor3473f882001-02-23 17:55:21 +00002726 * a child list containing the TEXTs and ENTITY_REFs node will be created.
MST 2003 John Fleck8b03bc52003-12-17 03:45:01 +00002727 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
2728 * references. XML special chars must be escaped first by using
2729 * xmlEncodeEntitiesReentrant(), or xmlNewTextChild() should be used.
Owen Taylor3473f882001-02-23 17:55:21 +00002730 *
2731 * Returns a pointer to the new node object.
2732 */
2733xmlNodePtr
2734xmlNewChild(xmlNodePtr parent, xmlNsPtr ns,
2735 const xmlChar *name, const xmlChar *content) {
2736 xmlNodePtr cur, prev;
2737
2738 if (parent == NULL) {
2739#ifdef DEBUG_TREE
2740 xmlGenericError(xmlGenericErrorContext,
2741 "xmlNewChild : parent == NULL\n");
2742#endif
2743 return(NULL);
2744 }
2745
2746 if (name == NULL) {
2747#ifdef DEBUG_TREE
2748 xmlGenericError(xmlGenericErrorContext,
2749 "xmlNewChild : name == NULL\n");
2750#endif
2751 return(NULL);
2752 }
2753
2754 /*
2755 * Allocate a new node
2756 */
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002757 if (parent->type == XML_ELEMENT_NODE) {
2758 if (ns == NULL)
2759 cur = xmlNewDocNode(parent->doc, parent->ns, name, content);
2760 else
2761 cur = xmlNewDocNode(parent->doc, ns, name, content);
2762 } else if ((parent->type == XML_DOCUMENT_NODE) ||
2763 (parent->type == XML_HTML_DOCUMENT_NODE)) {
2764 if (ns == NULL)
2765 cur = xmlNewDocNode((xmlDocPtr) parent, NULL, name, content);
2766 else
2767 cur = xmlNewDocNode((xmlDocPtr) parent, ns, name, content);
Daniel Veillard7e3f1402002-10-28 18:52:57 +00002768 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2769 cur = xmlNewDocNode( parent->doc, ns, name, content);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002770 } else {
2771 return(NULL);
2772 }
Owen Taylor3473f882001-02-23 17:55:21 +00002773 if (cur == NULL) return(NULL);
2774
2775 /*
2776 * add the new element at the end of the children list.
2777 */
2778 cur->type = XML_ELEMENT_NODE;
2779 cur->parent = parent;
2780 cur->doc = parent->doc;
2781 if (parent->children == NULL) {
2782 parent->children = cur;
2783 parent->last = cur;
2784 } else {
2785 prev = parent->last;
2786 prev->next = cur;
2787 cur->prev = prev;
2788 parent->last = cur;
2789 }
2790
2791 return(cur);
2792}
Daniel Veillard652327a2003-09-29 18:02:38 +00002793#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002794
2795/**
2796 * xmlAddNextSibling:
2797 * @cur: the child node
2798 * @elem: the new node
2799 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002800 * Add a new node @elem as the next sibling of @cur
2801 * If the new node was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00002802 * first unlinked from its existing context.
2803 * As a result of text merging @elem may be freed.
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002804 * If the new node is ATTRIBUTE, it is added into properties instead of children.
2805 * If there is an attribute with equal name, it is first destroyed.
Owen Taylor3473f882001-02-23 17:55:21 +00002806 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002807 * Returns the new node or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002808 */
2809xmlNodePtr
2810xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) {
2811 if (cur == NULL) {
2812#ifdef DEBUG_TREE
2813 xmlGenericError(xmlGenericErrorContext,
2814 "xmlAddNextSibling : cur == NULL\n");
2815#endif
2816 return(NULL);
2817 }
2818 if (elem == NULL) {
2819#ifdef DEBUG_TREE
2820 xmlGenericError(xmlGenericErrorContext,
2821 "xmlAddNextSibling : elem == NULL\n");
2822#endif
2823 return(NULL);
2824 }
2825
2826 xmlUnlinkNode(elem);
2827
2828 if (elem->type == XML_TEXT_NODE) {
2829 if (cur->type == XML_TEXT_NODE) {
Owen Taylor3473f882001-02-23 17:55:21 +00002830 xmlNodeAddContent(cur, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002831 xmlFreeNode(elem);
2832 return(cur);
2833 }
Daniel Veillard9e1c72d2001-08-31 20:03:19 +00002834 if ((cur->next != NULL) && (cur->next->type == XML_TEXT_NODE) &&
2835 (cur->name == cur->next->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002836 xmlChar *tmp;
2837
2838 tmp = xmlStrdup(elem->content);
2839 tmp = xmlStrcat(tmp, cur->next->content);
2840 xmlNodeSetContent(cur->next, tmp);
2841 xmlFree(tmp);
Owen Taylor3473f882001-02-23 17:55:21 +00002842 xmlFreeNode(elem);
2843 return(cur->next);
2844 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002845 } else if (elem->type == XML_ATTRIBUTE_NODE) {
2846 /* check if an attribute with the same name exists */
2847 xmlAttrPtr attr;
2848
2849 if (elem->ns == NULL)
2850 attr = xmlHasProp(cur->parent, elem->name);
2851 else
2852 attr = xmlHasNsProp(cur->parent, elem->name, elem->ns->href);
2853 if ((attr != NULL) && (attr != (xmlAttrPtr) elem)) {
2854 /* different instance, destroy it (attributes must be unique) */
2855 xmlFreeProp(attr);
2856 }
Owen Taylor3473f882001-02-23 17:55:21 +00002857 }
2858
2859 if (elem->doc != cur->doc) {
2860 xmlSetTreeDoc(elem, cur->doc);
2861 }
2862 elem->parent = cur->parent;
2863 elem->prev = cur;
2864 elem->next = cur->next;
2865 cur->next = elem;
2866 if (elem->next != NULL)
2867 elem->next->prev = elem;
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002868 if ((elem->parent != NULL) && (elem->parent->last == cur) && (elem->type != XML_ATTRIBUTE_NODE))
Owen Taylor3473f882001-02-23 17:55:21 +00002869 elem->parent->last = elem;
2870 return(elem);
2871}
2872
William M. Brack21e4ef22005-01-02 09:53:13 +00002873#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
2874 defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00002875/**
2876 * xmlAddPrevSibling:
2877 * @cur: the child node
2878 * @elem: the new node
2879 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002880 * Add a new node @elem as the previous sibling of @cur
Owen Taylor3473f882001-02-23 17:55:21 +00002881 * merging adjacent TEXT nodes (@elem may be freed)
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002882 * If the new node was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00002883 * first unlinked from its existing context.
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002884 * If the new node is ATTRIBUTE, it is added into properties instead of children.
2885 * If there is an attribute with equal name, it is first destroyed.
Owen Taylor3473f882001-02-23 17:55:21 +00002886 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002887 * Returns the new node or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002888 */
2889xmlNodePtr
2890xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) {
2891 if (cur == NULL) {
2892#ifdef DEBUG_TREE
2893 xmlGenericError(xmlGenericErrorContext,
2894 "xmlAddPrevSibling : cur == NULL\n");
2895#endif
2896 return(NULL);
2897 }
2898 if (elem == NULL) {
2899#ifdef DEBUG_TREE
2900 xmlGenericError(xmlGenericErrorContext,
2901 "xmlAddPrevSibling : elem == NULL\n");
2902#endif
2903 return(NULL);
2904 }
2905
2906 xmlUnlinkNode(elem);
2907
2908 if (elem->type == XML_TEXT_NODE) {
2909 if (cur->type == XML_TEXT_NODE) {
Owen Taylor3473f882001-02-23 17:55:21 +00002910 xmlChar *tmp;
2911
2912 tmp = xmlStrdup(elem->content);
2913 tmp = xmlStrcat(tmp, cur->content);
2914 xmlNodeSetContent(cur, tmp);
2915 xmlFree(tmp);
Owen Taylor3473f882001-02-23 17:55:21 +00002916 xmlFreeNode(elem);
2917 return(cur);
2918 }
Daniel Veillard9e1c72d2001-08-31 20:03:19 +00002919 if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE) &&
2920 (cur->name == cur->prev->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002921 xmlNodeAddContent(cur->prev, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002922 xmlFreeNode(elem);
2923 return(cur->prev);
2924 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002925 } else if (elem->type == XML_ATTRIBUTE_NODE) {
2926 /* check if an attribute with the same name exists */
2927 xmlAttrPtr attr;
2928
2929 if (elem->ns == NULL)
2930 attr = xmlHasProp(cur->parent, elem->name);
2931 else
2932 attr = xmlHasNsProp(cur->parent, elem->name, elem->ns->href);
2933 if ((attr != NULL) && (attr != (xmlAttrPtr) elem)) {
2934 /* different instance, destroy it (attributes must be unique) */
2935 xmlFreeProp(attr);
2936 }
Owen Taylor3473f882001-02-23 17:55:21 +00002937 }
2938
2939 if (elem->doc != cur->doc) {
2940 xmlSetTreeDoc(elem, cur->doc);
2941 }
2942 elem->parent = cur->parent;
2943 elem->next = cur;
2944 elem->prev = cur->prev;
2945 cur->prev = elem;
2946 if (elem->prev != NULL)
2947 elem->prev->next = elem;
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002948 if (elem->parent != NULL) {
2949 if (elem->type == XML_ATTRIBUTE_NODE) {
2950 if (elem->parent->properties == (xmlAttrPtr) cur) {
2951 elem->parent->properties = (xmlAttrPtr) elem;
2952 }
2953 } else {
2954 if (elem->parent->children == cur) {
2955 elem->parent->children = elem;
2956 }
2957 }
2958 }
Owen Taylor3473f882001-02-23 17:55:21 +00002959 return(elem);
2960}
Daniel Veillard652327a2003-09-29 18:02:38 +00002961#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002962
2963/**
2964 * xmlAddSibling:
2965 * @cur: the child node
2966 * @elem: the new node
2967 *
2968 * Add a new element @elem to the list of siblings of @cur
2969 * merging adjacent TEXT nodes (@elem may be freed)
2970 * If the new element was already inserted in a document it is
2971 * first unlinked from its existing context.
2972 *
2973 * Returns the new element or NULL in case of error.
2974 */
2975xmlNodePtr
2976xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) {
2977 xmlNodePtr parent;
2978
2979 if (cur == NULL) {
2980#ifdef DEBUG_TREE
2981 xmlGenericError(xmlGenericErrorContext,
2982 "xmlAddSibling : cur == NULL\n");
2983#endif
2984 return(NULL);
2985 }
2986
2987 if (elem == NULL) {
2988#ifdef DEBUG_TREE
2989 xmlGenericError(xmlGenericErrorContext,
2990 "xmlAddSibling : elem == NULL\n");
2991#endif
2992 return(NULL);
2993 }
2994
2995 /*
2996 * Constant time is we can rely on the ->parent->last to find
2997 * the last sibling.
2998 */
2999 if ((cur->parent != NULL) &&
3000 (cur->parent->children != NULL) &&
3001 (cur->parent->last != NULL) &&
3002 (cur->parent->last->next == NULL)) {
3003 cur = cur->parent->last;
3004 } else {
3005 while (cur->next != NULL) cur = cur->next;
3006 }
3007
3008 xmlUnlinkNode(elem);
3009
Daniel Veillarde22dd5c2003-10-29 12:53:27 +00003010 if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE) &&
3011 (cur->name == elem->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003012 xmlNodeAddContent(cur, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003013 xmlFreeNode(elem);
3014 return(cur);
3015 }
3016
3017 if (elem->doc != cur->doc) {
3018 xmlSetTreeDoc(elem, cur->doc);
3019 }
3020 parent = cur->parent;
3021 elem->prev = cur;
3022 elem->next = NULL;
3023 elem->parent = parent;
3024 cur->next = elem;
3025 if (parent != NULL)
3026 parent->last = elem;
3027
3028 return(elem);
3029}
3030
3031/**
3032 * xmlAddChildList:
3033 * @parent: the parent node
3034 * @cur: the first node in the list
3035 *
3036 * Add a list of node at the end of the child list of the parent
3037 * merging adjacent TEXT nodes (@cur may be freed)
3038 *
3039 * Returns the last child or NULL in case of error.
3040 */
3041xmlNodePtr
3042xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) {
3043 xmlNodePtr prev;
3044
3045 if (parent == NULL) {
3046#ifdef DEBUG_TREE
3047 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00003048 "xmlAddChildList : parent == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003049#endif
3050 return(NULL);
3051 }
3052
3053 if (cur == NULL) {
3054#ifdef DEBUG_TREE
3055 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00003056 "xmlAddChildList : child == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003057#endif
3058 return(NULL);
3059 }
3060
3061 if ((cur->doc != NULL) && (parent->doc != NULL) &&
3062 (cur->doc != parent->doc)) {
3063#ifdef DEBUG_TREE
3064 xmlGenericError(xmlGenericErrorContext,
3065 "Elements moved to a different document\n");
3066#endif
3067 }
3068
3069 /*
3070 * add the first element at the end of the children list.
3071 */
Daniel Veillard5335dc52003-01-01 20:59:38 +00003072
Owen Taylor3473f882001-02-23 17:55:21 +00003073 if (parent->children == NULL) {
3074 parent->children = cur;
3075 } else {
3076 /*
3077 * If cur and parent->last both are TEXT nodes, then merge them.
3078 */
3079 if ((cur->type == XML_TEXT_NODE) &&
3080 (parent->last->type == XML_TEXT_NODE) &&
3081 (cur->name == parent->last->name)) {
Daniel Veillard5335dc52003-01-01 20:59:38 +00003082 xmlNodeAddContent(parent->last, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003083 /*
3084 * if it's the only child, nothing more to be done.
3085 */
3086 if (cur->next == NULL) {
3087 xmlFreeNode(cur);
3088 return(parent->last);
3089 }
3090 prev = cur;
3091 cur = cur->next;
3092 xmlFreeNode(prev);
3093 }
3094 prev = parent->last;
3095 prev->next = cur;
3096 cur->prev = prev;
3097 }
3098 while (cur->next != NULL) {
3099 cur->parent = parent;
3100 if (cur->doc != parent->doc) {
3101 xmlSetTreeDoc(cur, parent->doc);
3102 }
3103 cur = cur->next;
3104 }
3105 cur->parent = parent;
3106 cur->doc = parent->doc; /* the parent may not be linked to a doc ! */
3107 parent->last = cur;
3108
3109 return(cur);
3110}
3111
3112/**
3113 * xmlAddChild:
3114 * @parent: the parent node
3115 * @cur: the child node
3116 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003117 * Add a new node to @parent, at the end of the child (or property) list
Owen Taylor3473f882001-02-23 17:55:21 +00003118 * merging adjacent TEXT nodes (in which case @cur is freed)
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003119 * If the new node is ATTRIBUTE, it is added into properties instead of children.
3120 * If there is an attribute with equal name, it is first destroyed.
3121 *
Owen Taylor3473f882001-02-23 17:55:21 +00003122 * Returns the child or NULL in case of error.
3123 */
3124xmlNodePtr
3125xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) {
3126 xmlNodePtr prev;
3127
3128 if (parent == NULL) {
3129#ifdef DEBUG_TREE
3130 xmlGenericError(xmlGenericErrorContext,
3131 "xmlAddChild : parent == NULL\n");
3132#endif
3133 return(NULL);
3134 }
3135
3136 if (cur == NULL) {
3137#ifdef DEBUG_TREE
3138 xmlGenericError(xmlGenericErrorContext,
3139 "xmlAddChild : child == NULL\n");
3140#endif
3141 return(NULL);
3142 }
3143
Owen Taylor3473f882001-02-23 17:55:21 +00003144 /*
3145 * If cur is a TEXT node, merge its content with adjacent TEXT nodes
Owen Taylor3473f882001-02-23 17:55:21 +00003146 * cur is then freed.
3147 */
3148 if (cur->type == XML_TEXT_NODE) {
Daniel Veillard7db37732001-07-12 01:20:08 +00003149 if ((parent->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003150 (parent->content != NULL) &&
Daniel Veillarde22dd5c2003-10-29 12:53:27 +00003151 (parent->name == cur->name) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003152 (parent != cur)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003153 xmlNodeAddContent(parent, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003154 xmlFreeNode(cur);
3155 return(parent);
3156 }
3157 if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003158 (parent->last->name == cur->name) &&
3159 (parent->last != cur)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003160 xmlNodeAddContent(parent->last, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003161 xmlFreeNode(cur);
3162 return(parent->last);
3163 }
3164 }
3165
3166 /*
3167 * add the new element at the end of the children list.
3168 */
Daniel Veillard5335dc52003-01-01 20:59:38 +00003169 prev = cur->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00003170 cur->parent = parent;
3171 if (cur->doc != parent->doc) {
3172 xmlSetTreeDoc(cur, parent->doc);
3173 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00003174 /* this check prevents a loop on tree-traversions if a developer
3175 * tries to add a node to its parent multiple times
3176 */
3177 if (prev == parent)
3178 return(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00003179
3180 /*
Daniel Veillard7db37732001-07-12 01:20:08 +00003181 * Coalescing
Owen Taylor3473f882001-02-23 17:55:21 +00003182 */
Daniel Veillard7db37732001-07-12 01:20:08 +00003183 if ((parent->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003184 (parent->content != NULL) &&
3185 (parent != cur)) {
Daniel Veillard7db37732001-07-12 01:20:08 +00003186 xmlNodeAddContent(parent, cur->content);
Daniel Veillard7db37732001-07-12 01:20:08 +00003187 xmlFreeNode(cur);
3188 return(parent);
Owen Taylor3473f882001-02-23 17:55:21 +00003189 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003190 if (cur->type == XML_ATTRIBUTE_NODE) {
3191 if (parent->properties == NULL) {
3192 parent->properties = (xmlAttrPtr) cur;
3193 } else {
3194 /* check if an attribute with the same name exists */
3195 xmlAttrPtr lastattr;
Owen Taylor3473f882001-02-23 17:55:21 +00003196
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003197 if (cur->ns == NULL)
3198 lastattr = xmlHasProp(parent, cur->name);
3199 else
3200 lastattr = xmlHasNsProp(parent, cur->name, cur->ns->href);
3201 if ((lastattr != NULL) && (lastattr != (xmlAttrPtr) cur)) {
3202 /* different instance, destroy it (attributes must be unique) */
3203 xmlFreeProp(lastattr);
3204 }
3205 /* find the end */
3206 lastattr = parent->properties;
3207 while (lastattr->next != NULL) {
3208 lastattr = lastattr->next;
3209 }
3210 lastattr->next = (xmlAttrPtr) cur;
3211 ((xmlAttrPtr) cur)->prev = lastattr;
3212 }
3213 } else {
3214 if (parent->children == NULL) {
3215 parent->children = cur;
3216 parent->last = cur;
3217 } else {
3218 prev = parent->last;
3219 prev->next = cur;
3220 cur->prev = prev;
3221 parent->last = cur;
3222 }
3223 }
Owen Taylor3473f882001-02-23 17:55:21 +00003224 return(cur);
3225}
3226
3227/**
3228 * xmlGetLastChild:
3229 * @parent: the parent node
3230 *
3231 * Search the last child of a node.
3232 * Returns the last child or NULL if none.
3233 */
3234xmlNodePtr
3235xmlGetLastChild(xmlNodePtr parent) {
3236 if (parent == NULL) {
3237#ifdef DEBUG_TREE
3238 xmlGenericError(xmlGenericErrorContext,
3239 "xmlGetLastChild : parent == NULL\n");
3240#endif
3241 return(NULL);
3242 }
3243 return(parent->last);
3244}
3245
3246/**
3247 * xmlFreeNodeList:
3248 * @cur: the first node in the list
3249 *
3250 * Free a node and all its siblings, this is a recursive behaviour, all
3251 * the children are freed too.
3252 */
3253void
3254xmlFreeNodeList(xmlNodePtr cur) {
3255 xmlNodePtr next;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003256 xmlDictPtr dict = NULL;
3257
3258 if (cur == NULL) return;
Daniel Veillarde6a55192002-01-14 17:11:53 +00003259 if (cur->type == XML_NAMESPACE_DECL) {
3260 xmlFreeNsList((xmlNsPtr) cur);
3261 return;
3262 }
Daniel Veillard9adc0462003-03-24 18:39:54 +00003263 if ((cur->type == XML_DOCUMENT_NODE) ||
3264#ifdef LIBXML_DOCB_ENABLED
3265 (cur->type == XML_DOCB_DOCUMENT_NODE) ||
Daniel Veillard9adc0462003-03-24 18:39:54 +00003266#endif
Daniel Veillard6560a422003-03-27 21:25:38 +00003267 (cur->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard9adc0462003-03-24 18:39:54 +00003268 xmlFreeDoc((xmlDocPtr) cur);
3269 return;
3270 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003271 if (cur->doc != NULL) dict = cur->doc->dict;
Owen Taylor3473f882001-02-23 17:55:21 +00003272 while (cur != NULL) {
3273 next = cur->next;
Daniel Veillard02141ea2001-04-30 11:46:40 +00003274 if (cur->type != XML_DTD_NODE) {
Daniel Veillard5335dc52003-01-01 20:59:38 +00003275
Daniel Veillarda880b122003-04-21 21:36:41 +00003276 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00003277 xmlDeregisterNodeDefaultValue(cur);
3278
Daniel Veillard02141ea2001-04-30 11:46:40 +00003279 if ((cur->children != NULL) &&
3280 (cur->type != XML_ENTITY_REF_NODE))
3281 xmlFreeNodeList(cur->children);
Daniel Veillard01c13b52002-12-10 15:19:08 +00003282 if (((cur->type == XML_ELEMENT_NODE) ||
3283 (cur->type == XML_XINCLUDE_START) ||
3284 (cur->type == XML_XINCLUDE_END)) &&
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003285 (cur->properties != NULL))
Daniel Veillard02141ea2001-04-30 11:46:40 +00003286 xmlFreePropList(cur->properties);
Daniel Veillard7db37732001-07-12 01:20:08 +00003287 if ((cur->type != XML_ELEMENT_NODE) &&
3288 (cur->type != XML_XINCLUDE_START) &&
3289 (cur->type != XML_XINCLUDE_END) &&
3290 (cur->type != XML_ENTITY_REF_NODE)) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003291 DICT_FREE(cur->content)
Daniel Veillard7db37732001-07-12 01:20:08 +00003292 }
3293 if (((cur->type == XML_ELEMENT_NODE) ||
3294 (cur->type == XML_XINCLUDE_START) ||
3295 (cur->type == XML_XINCLUDE_END)) &&
3296 (cur->nsDef != NULL))
3297 xmlFreeNsList(cur->nsDef);
3298
Daniel Veillard9cc6dc62001-06-11 08:09:20 +00003299 /*
3300 * When a node is a text node or a comment, it uses a global static
3301 * variable for the name of the node.
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003302 * Otherwise the node name might come from the document's
3303 * dictionnary
Daniel Veillard9cc6dc62001-06-11 08:09:20 +00003304 */
Daniel Veillard02141ea2001-04-30 11:46:40 +00003305 if ((cur->name != NULL) &&
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003306 (cur->type != XML_TEXT_NODE) &&
3307 (cur->type != XML_COMMENT_NODE))
3308 DICT_FREE(cur->name)
Daniel Veillard02141ea2001-04-30 11:46:40 +00003309 xmlFree(cur);
3310 }
Owen Taylor3473f882001-02-23 17:55:21 +00003311 cur = next;
3312 }
3313}
3314
3315/**
3316 * xmlFreeNode:
3317 * @cur: the node
3318 *
3319 * Free a node, this is a recursive behaviour, all the children are freed too.
3320 * This doesn't unlink the child from the list, use xmlUnlinkNode() first.
3321 */
3322void
3323xmlFreeNode(xmlNodePtr cur) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003324 xmlDictPtr dict = NULL;
3325
3326 if (cur == NULL) return;
Daniel Veillard5335dc52003-01-01 20:59:38 +00003327
Daniel Veillard02141ea2001-04-30 11:46:40 +00003328 /* use xmlFreeDtd for DTD nodes */
Daniel Veillarde6a55192002-01-14 17:11:53 +00003329 if (cur->type == XML_DTD_NODE) {
3330 xmlFreeDtd((xmlDtdPtr) cur);
Owen Taylor3473f882001-02-23 17:55:21 +00003331 return;
Daniel Veillarde6a55192002-01-14 17:11:53 +00003332 }
3333 if (cur->type == XML_NAMESPACE_DECL) {
3334 xmlFreeNs((xmlNsPtr) cur);
3335 return;
3336 }
Daniel Veillarda70d62f2002-11-07 14:18:03 +00003337 if (cur->type == XML_ATTRIBUTE_NODE) {
3338 xmlFreeProp((xmlAttrPtr) cur);
3339 return;
3340 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00003341
Daniel Veillarda880b122003-04-21 21:36:41 +00003342 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00003343 xmlDeregisterNodeDefaultValue(cur);
3344
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003345 if (cur->doc != NULL) dict = cur->doc->dict;
3346
Owen Taylor3473f882001-02-23 17:55:21 +00003347 if ((cur->children != NULL) &&
3348 (cur->type != XML_ENTITY_REF_NODE))
3349 xmlFreeNodeList(cur->children);
Daniel Veillard01c13b52002-12-10 15:19:08 +00003350 if (((cur->type == XML_ELEMENT_NODE) ||
3351 (cur->type == XML_XINCLUDE_START) ||
3352 (cur->type == XML_XINCLUDE_END)) &&
3353 (cur->properties != NULL))
Daniel Veillard02141ea2001-04-30 11:46:40 +00003354 xmlFreePropList(cur->properties);
Daniel Veillard7db37732001-07-12 01:20:08 +00003355 if ((cur->type != XML_ELEMENT_NODE) &&
3356 (cur->content != NULL) &&
3357 (cur->type != XML_ENTITY_REF_NODE) &&
3358 (cur->type != XML_XINCLUDE_END) &&
3359 (cur->type != XML_XINCLUDE_START)) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003360 DICT_FREE(cur->content)
Daniel Veillard7db37732001-07-12 01:20:08 +00003361 }
3362
Daniel Veillardacd370f2001-06-09 17:17:51 +00003363 /*
3364 * When a node is a text node or a comment, it uses a global static
3365 * variable for the name of the node.
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003366 * Otherwise the node name might come from the document's dictionnary
Daniel Veillardacd370f2001-06-09 17:17:51 +00003367 */
Owen Taylor3473f882001-02-23 17:55:21 +00003368 if ((cur->name != NULL) &&
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003369 (cur->type != XML_TEXT_NODE) &&
3370 (cur->type != XML_COMMENT_NODE))
3371 DICT_FREE(cur->name)
Daniel Veillardacd370f2001-06-09 17:17:51 +00003372
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003373 if (((cur->type == XML_ELEMENT_NODE) ||
3374 (cur->type == XML_XINCLUDE_START) ||
3375 (cur->type == XML_XINCLUDE_END)) &&
3376 (cur->nsDef != NULL))
3377 xmlFreeNsList(cur->nsDef);
Owen Taylor3473f882001-02-23 17:55:21 +00003378 xmlFree(cur);
3379}
3380
3381/**
3382 * xmlUnlinkNode:
3383 * @cur: the node
3384 *
3385 * Unlink a node from it's current context, the node is not freed
3386 */
3387void
3388xmlUnlinkNode(xmlNodePtr cur) {
3389 if (cur == NULL) {
3390#ifdef DEBUG_TREE
3391 xmlGenericError(xmlGenericErrorContext,
3392 "xmlUnlinkNode : node == NULL\n");
3393#endif
3394 return;
3395 }
Daniel Veillard29e43992001-12-13 22:21:58 +00003396 if (cur->type == XML_DTD_NODE) {
3397 xmlDocPtr doc;
3398 doc = cur->doc;
Daniel Veillarda067e652003-05-01 08:03:46 +00003399 if (doc != NULL) {
3400 if (doc->intSubset == (xmlDtdPtr) cur)
3401 doc->intSubset = NULL;
3402 if (doc->extSubset == (xmlDtdPtr) cur)
3403 doc->extSubset = NULL;
3404 }
Daniel Veillard29e43992001-12-13 22:21:58 +00003405 }
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003406 if (cur->parent != NULL) {
3407 xmlNodePtr parent;
3408 parent = cur->parent;
3409 if (cur->type == XML_ATTRIBUTE_NODE) {
Daniel Veillardda6f4af2005-06-20 17:17:54 +00003410 /* If attribute is an ID from subset then remove it */
3411 if ((((xmlAttrPtr) cur)->atype == XML_ATTRIBUTE_ID) &&
3412 xmlIsID(parent->doc, parent, (xmlAttrPtr) cur)) {
3413 xmlRemoveID(cur->doc, (xmlAttrPtr) cur);
3414 }
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003415 if (parent->properties == (xmlAttrPtr) cur)
3416 parent->properties = ((xmlAttrPtr) cur)->next;
3417 } else {
3418 if (parent->children == cur)
3419 parent->children = cur->next;
3420 if (parent->last == cur)
3421 parent->last = cur->prev;
3422 }
3423 cur->parent = NULL;
3424 }
Owen Taylor3473f882001-02-23 17:55:21 +00003425 if (cur->next != NULL)
3426 cur->next->prev = cur->prev;
3427 if (cur->prev != NULL)
3428 cur->prev->next = cur->next;
3429 cur->next = cur->prev = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00003430}
3431
Daniel Veillard2156d432004-03-04 15:59:36 +00003432#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00003433/**
3434 * xmlReplaceNode:
3435 * @old: the old node
3436 * @cur: the node
3437 *
William M. Brackd7cf7f82003-11-14 07:13:16 +00003438 * Unlink the old node from its current context, prune the new one
Daniel Veillardd1640922001-12-17 15:30:10 +00003439 * at the same place. If @cur was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00003440 * first unlinked from its existing context.
3441 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003442 * Returns the @old node
Owen Taylor3473f882001-02-23 17:55:21 +00003443 */
3444xmlNodePtr
3445xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) {
Daniel Veillardce244ad2004-11-05 10:03:46 +00003446 if (old == cur) return(NULL);
Daniel Veillarda03e3652004-11-02 18:45:30 +00003447 if ((old == NULL) || (old->parent == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003448#ifdef DEBUG_TREE
3449 xmlGenericError(xmlGenericErrorContext,
Daniel Veillarda03e3652004-11-02 18:45:30 +00003450 "xmlReplaceNode : old == NULL or without parent\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003451#endif
3452 return(NULL);
3453 }
3454 if (cur == NULL) {
3455 xmlUnlinkNode(old);
3456 return(old);
3457 }
3458 if (cur == old) {
3459 return(old);
3460 }
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003461 if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) {
3462#ifdef DEBUG_TREE
3463 xmlGenericError(xmlGenericErrorContext,
3464 "xmlReplaceNode : Trying to replace attribute node with other node type\n");
3465#endif
3466 return(old);
3467 }
3468 if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) {
3469#ifdef DEBUG_TREE
3470 xmlGenericError(xmlGenericErrorContext,
3471 "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n");
3472#endif
3473 return(old);
3474 }
Owen Taylor3473f882001-02-23 17:55:21 +00003475 xmlUnlinkNode(cur);
Daniel Veillard64d7d122005-05-11 18:03:42 +00003476 xmlSetTreeDoc(cur, old->doc);
Owen Taylor3473f882001-02-23 17:55:21 +00003477 cur->parent = old->parent;
3478 cur->next = old->next;
3479 if (cur->next != NULL)
3480 cur->next->prev = cur;
3481 cur->prev = old->prev;
3482 if (cur->prev != NULL)
3483 cur->prev->next = cur;
3484 if (cur->parent != NULL) {
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003485 if (cur->type == XML_ATTRIBUTE_NODE) {
3486 if (cur->parent->properties == (xmlAttrPtr)old)
3487 cur->parent->properties = ((xmlAttrPtr) cur);
Daniel Veillardda6f4af2005-06-20 17:17:54 +00003488
3489 /* If old attribute is ID and defined in DTD then remove ID */
3490 if ((((xmlAttrPtr) old)->atype == XML_ATTRIBUTE_ID) &&
3491 xmlIsID(old->doc, old->parent, (xmlAttrPtr) old)) {
3492 xmlRemoveID(old->doc, (xmlAttrPtr) old);
3493 }
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003494 } else {
3495 if (cur->parent->children == old)
3496 cur->parent->children = cur;
3497 if (cur->parent->last == old)
3498 cur->parent->last = cur;
3499 }
Owen Taylor3473f882001-02-23 17:55:21 +00003500 }
3501 old->next = old->prev = NULL;
3502 old->parent = NULL;
3503 return(old);
3504}
Daniel Veillard652327a2003-09-29 18:02:38 +00003505#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00003506
3507/************************************************************************
3508 * *
3509 * Copy operations *
3510 * *
3511 ************************************************************************/
3512
3513/**
3514 * xmlCopyNamespace:
3515 * @cur: the namespace
3516 *
3517 * Do a copy of the namespace.
3518 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003519 * Returns: a new #xmlNsPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003520 */
3521xmlNsPtr
3522xmlCopyNamespace(xmlNsPtr cur) {
3523 xmlNsPtr ret;
3524
3525 if (cur == NULL) return(NULL);
3526 switch (cur->type) {
3527 case XML_LOCAL_NAMESPACE:
3528 ret = xmlNewNs(NULL, cur->href, cur->prefix);
3529 break;
3530 default:
3531#ifdef DEBUG_TREE
3532 xmlGenericError(xmlGenericErrorContext,
3533 "xmlCopyNamespace: invalid type %d\n", cur->type);
3534#endif
3535 return(NULL);
3536 }
3537 return(ret);
3538}
3539
3540/**
3541 * xmlCopyNamespaceList:
3542 * @cur: the first namespace
3543 *
3544 * Do a copy of an namespace list.
3545 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003546 * Returns: a new #xmlNsPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003547 */
3548xmlNsPtr
3549xmlCopyNamespaceList(xmlNsPtr cur) {
3550 xmlNsPtr ret = NULL;
3551 xmlNsPtr p = NULL,q;
3552
3553 while (cur != NULL) {
3554 q = xmlCopyNamespace(cur);
3555 if (p == NULL) {
3556 ret = p = q;
3557 } else {
3558 p->next = q;
3559 p = q;
3560 }
3561 cur = cur->next;
3562 }
3563 return(ret);
3564}
3565
3566static xmlNodePtr
3567xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent);
3568/**
3569 * xmlCopyProp:
3570 * @target: the element where the attribute will be grafted
3571 * @cur: the attribute
3572 *
3573 * Do a copy of the attribute.
3574 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003575 * Returns: a new #xmlAttrPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003576 */
3577xmlAttrPtr
3578xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) {
3579 xmlAttrPtr ret;
3580
3581 if (cur == NULL) return(NULL);
3582 if (target != NULL)
3583 ret = xmlNewDocProp(target->doc, cur->name, NULL);
3584 else if (cur->parent != NULL)
3585 ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL);
3586 else if (cur->children != NULL)
3587 ret = xmlNewDocProp(cur->children->doc, cur->name, NULL);
3588 else
3589 ret = xmlNewDocProp(NULL, cur->name, NULL);
3590 if (ret == NULL) return(NULL);
3591 ret->parent = target;
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00003592
Owen Taylor3473f882001-02-23 17:55:21 +00003593 if ((cur->ns != NULL) && (target != NULL)) {
Daniel Veillard8107a222002-01-13 14:10:10 +00003594 xmlNsPtr ns;
Daniel Veillard652327a2003-09-29 18:02:38 +00003595
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00003596 ns = xmlSearchNs(target->doc, target, cur->ns->prefix);
3597 if (ns == NULL) {
3598 /*
3599 * Humm, we are copying an element whose namespace is defined
3600 * out of the new tree scope. Search it in the original tree
3601 * and add it at the top of the new tree
3602 */
3603 ns = xmlSearchNs(cur->doc, cur->parent, cur->ns->prefix);
3604 if (ns != NULL) {
3605 xmlNodePtr root = target;
3606 xmlNodePtr pred = NULL;
3607
3608 while (root->parent != NULL) {
3609 pred = root;
3610 root = root->parent;
3611 }
3612 if (root == (xmlNodePtr) target->doc) {
3613 /* correct possibly cycling above the document elt */
3614 root = pred;
3615 }
3616 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
3617 }
3618 } else {
3619 /*
3620 * we have to find something appropriate here since
3621 * we cant be sure, that the namespce we found is identified
3622 * by the prefix
3623 */
Daniel Veillard044fc6b2002-03-04 17:09:44 +00003624 if (xmlStrEqual(ns->href, cur->ns->href)) {
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00003625 /* this is the nice case */
3626 ret->ns = ns;
3627 } else {
3628 /*
3629 * we are in trouble: we need a new reconcilied namespace.
3630 * This is expensive
3631 */
3632 ret->ns = xmlNewReconciliedNs(target->doc, target, cur->ns);
3633 }
3634 }
3635
Owen Taylor3473f882001-02-23 17:55:21 +00003636 } else
3637 ret->ns = NULL;
3638
3639 if (cur->children != NULL) {
3640 xmlNodePtr tmp;
3641
3642 ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret);
3643 ret->last = NULL;
3644 tmp = ret->children;
3645 while (tmp != NULL) {
3646 /* tmp->parent = (xmlNodePtr)ret; */
3647 if (tmp->next == NULL)
3648 ret->last = tmp;
3649 tmp = tmp->next;
3650 }
3651 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00003652 /*
3653 * Try to handle IDs
3654 */
Daniel Veillarda3db2e32002-03-08 15:46:57 +00003655 if ((target!= NULL) && (cur!= NULL) &&
3656 (target->doc != NULL) && (cur->doc != NULL) &&
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00003657 (cur->doc->ids != NULL) && (cur->parent != NULL)) {
3658 if (xmlIsID(cur->doc, cur->parent, cur)) {
3659 xmlChar *id;
3660
3661 id = xmlNodeListGetString(cur->doc, cur->children, 1);
3662 if (id != NULL) {
3663 xmlAddID(NULL, target->doc, id, ret);
3664 xmlFree(id);
3665 }
3666 }
3667 }
Owen Taylor3473f882001-02-23 17:55:21 +00003668 return(ret);
3669}
3670
3671/**
3672 * xmlCopyPropList:
3673 * @target: the element where the attributes will be grafted
3674 * @cur: the first attribute
3675 *
3676 * Do a copy of an attribute list.
3677 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003678 * Returns: a new #xmlAttrPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003679 */
3680xmlAttrPtr
3681xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) {
3682 xmlAttrPtr ret = NULL;
3683 xmlAttrPtr p = NULL,q;
3684
3685 while (cur != NULL) {
3686 q = xmlCopyProp(target, cur);
William M. Brack13dfa872004-09-18 04:52:08 +00003687 if (q == NULL)
3688 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003689 if (p == NULL) {
3690 ret = p = q;
3691 } else {
3692 p->next = q;
3693 q->prev = p;
3694 p = q;
3695 }
3696 cur = cur->next;
3697 }
3698 return(ret);
3699}
3700
3701/*
Daniel Veillardd1640922001-12-17 15:30:10 +00003702 * NOTE about the CopyNode operations !
Owen Taylor3473f882001-02-23 17:55:21 +00003703 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003704 * They are split into external and internal parts for one
Owen Taylor3473f882001-02-23 17:55:21 +00003705 * tricky reason: namespaces. Doing a direct copy of a node
3706 * say RPM:Copyright without changing the namespace pointer to
3707 * something else can produce stale links. One way to do it is
3708 * to keep a reference counter but this doesn't work as soon
3709 * as one move the element or the subtree out of the scope of
3710 * the existing namespace. The actual solution seems to add
3711 * a copy of the namespace at the top of the copied tree if
3712 * not available in the subtree.
3713 * Hence two functions, the public front-end call the inner ones
William M. Brack57e9e912004-03-09 16:19:02 +00003714 * The argument "recursive" normally indicates a recursive copy
3715 * of the node with values 0 (no) and 1 (yes). For XInclude,
3716 * however, we allow a value of 2 to indicate copy properties and
3717 * namespace info, but don't recurse on children.
Owen Taylor3473f882001-02-23 17:55:21 +00003718 */
3719
3720static xmlNodePtr
Daniel Veillard3ec4c612001-08-28 20:39:49 +00003721xmlStaticCopyNode(const xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent,
William M. Brack57e9e912004-03-09 16:19:02 +00003722 int extended) {
Owen Taylor3473f882001-02-23 17:55:21 +00003723 xmlNodePtr ret;
3724
3725 if (node == NULL) return(NULL);
Daniel Veillard39196eb2001-06-19 18:09:42 +00003726 switch (node->type) {
3727 case XML_TEXT_NODE:
3728 case XML_CDATA_SECTION_NODE:
3729 case XML_ELEMENT_NODE:
Daniel Veillardec6725e2002-09-05 11:12:45 +00003730 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00003731 case XML_ENTITY_REF_NODE:
3732 case XML_ENTITY_NODE:
3733 case XML_PI_NODE:
3734 case XML_COMMENT_NODE:
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00003735 case XML_XINCLUDE_START:
3736 case XML_XINCLUDE_END:
3737 break;
3738 case XML_ATTRIBUTE_NODE:
3739 return((xmlNodePtr) xmlCopyProp(parent, (xmlAttrPtr) node));
3740 case XML_NAMESPACE_DECL:
3741 return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node));
3742
Daniel Veillard39196eb2001-06-19 18:09:42 +00003743 case XML_DOCUMENT_NODE:
3744 case XML_HTML_DOCUMENT_NODE:
3745#ifdef LIBXML_DOCB_ENABLED
3746 case XML_DOCB_DOCUMENT_NODE:
3747#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00003748#ifdef LIBXML_TREE_ENABLED
William M. Brack57e9e912004-03-09 16:19:02 +00003749 return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, extended));
Daniel Veillard652327a2003-09-29 18:02:38 +00003750#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard39196eb2001-06-19 18:09:42 +00003751 case XML_DOCUMENT_TYPE_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00003752 case XML_NOTATION_NODE:
3753 case XML_DTD_NODE:
3754 case XML_ELEMENT_DECL:
3755 case XML_ATTRIBUTE_DECL:
3756 case XML_ENTITY_DECL:
3757 return(NULL);
3758 }
Daniel Veillardb33c2012001-04-25 12:59:04 +00003759
Owen Taylor3473f882001-02-23 17:55:21 +00003760 /*
3761 * Allocate a new node and fill the fields.
3762 */
3763 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
3764 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00003765 xmlTreeErrMemory("copying node");
Owen Taylor3473f882001-02-23 17:55:21 +00003766 return(NULL);
3767 }
3768 memset(ret, 0, sizeof(xmlNode));
3769 ret->type = node->type;
3770
3771 ret->doc = doc;
3772 ret->parent = parent;
3773 if (node->name == xmlStringText)
3774 ret->name = xmlStringText;
3775 else if (node->name == xmlStringTextNoenc)
3776 ret->name = xmlStringTextNoenc;
3777 else if (node->name == xmlStringComment)
3778 ret->name = xmlStringComment;
Daniel Veillard03a53c32004-10-26 16:06:51 +00003779 else if (node->name != NULL) {
3780 if ((doc != NULL) && (doc->dict != NULL))
3781 ret->name = xmlDictLookup(doc->dict, node->name, -1);
3782 else
3783 ret->name = xmlStrdup(node->name);
3784 }
Daniel Veillard7db37732001-07-12 01:20:08 +00003785 if ((node->type != XML_ELEMENT_NODE) &&
3786 (node->content != NULL) &&
3787 (node->type != XML_ENTITY_REF_NODE) &&
3788 (node->type != XML_XINCLUDE_END) &&
3789 (node->type != XML_XINCLUDE_START)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003790 ret->content = xmlStrdup(node->content);
Daniel Veillard8107a222002-01-13 14:10:10 +00003791 }else{
3792 if (node->type == XML_ELEMENT_NODE)
Daniel Veillard3e35f8e2003-10-21 00:05:38 +00003793 ret->line = node->line;
Owen Taylor3473f882001-02-23 17:55:21 +00003794 }
Daniel Veillardacb2bda2002-01-13 16:15:43 +00003795 if (parent != NULL) {
3796 xmlNodePtr tmp;
3797
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003798 /*
3799 * this is a tricky part for the node register thing:
3800 * in case ret does get coalesced in xmlAddChild
3801 * the deregister-node callback is called; so we register ret now already
3802 */
Daniel Veillarda880b122003-04-21 21:36:41 +00003803 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003804 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
3805
Daniel Veillardacb2bda2002-01-13 16:15:43 +00003806 tmp = xmlAddChild(parent, ret);
3807 /* node could have coalesced */
3808 if (tmp != ret)
3809 return(tmp);
3810 }
Owen Taylor3473f882001-02-23 17:55:21 +00003811
William M. Brack57e9e912004-03-09 16:19:02 +00003812 if (!extended)
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003813 goto out;
Owen Taylor3473f882001-02-23 17:55:21 +00003814 if (node->nsDef != NULL)
3815 ret->nsDef = xmlCopyNamespaceList(node->nsDef);
3816
3817 if (node->ns != NULL) {
3818 xmlNsPtr ns;
3819
3820 ns = xmlSearchNs(doc, ret, node->ns->prefix);
3821 if (ns == NULL) {
3822 /*
3823 * Humm, we are copying an element whose namespace is defined
3824 * out of the new tree scope. Search it in the original tree
3825 * and add it at the top of the new tree
3826 */
3827 ns = xmlSearchNs(node->doc, node, node->ns->prefix);
3828 if (ns != NULL) {
3829 xmlNodePtr root = ret;
3830
3831 while (root->parent != NULL) root = root->parent;
Daniel Veillarde82a9922001-04-22 12:12:58 +00003832 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00003833 }
3834 } else {
3835 /*
3836 * reference the existing namespace definition in our own tree.
3837 */
3838 ret->ns = ns;
3839 }
3840 }
3841 if (node->properties != NULL)
3842 ret->properties = xmlCopyPropList(ret, node->properties);
Daniel Veillardb33c2012001-04-25 12:59:04 +00003843 if (node->type == XML_ENTITY_REF_NODE) {
3844 if ((doc == NULL) || (node->doc != doc)) {
3845 /*
3846 * The copied node will go into a separate document, so
Daniel Veillardd1640922001-12-17 15:30:10 +00003847 * to avoid dangling references to the ENTITY_DECL node
Daniel Veillardb33c2012001-04-25 12:59:04 +00003848 * we cannot keep the reference. Try to find it in the
3849 * target document.
3850 */
3851 ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name);
3852 } else {
3853 ret->children = node->children;
3854 }
Daniel Veillard0ec98632001-11-14 15:04:32 +00003855 ret->last = ret->children;
William M. Brack57e9e912004-03-09 16:19:02 +00003856 } else if ((node->children != NULL) && (extended != 2)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003857 ret->children = xmlStaticCopyNodeList(node->children, doc, ret);
Daniel Veillard0ec98632001-11-14 15:04:32 +00003858 UPDATE_LAST_CHILD_AND_PARENT(ret)
3859 }
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003860
3861out:
3862 /* if parent != NULL we already registered the node above */
Daniel Veillardac996a12004-07-30 12:02:58 +00003863 if ((parent == NULL) &&
3864 ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003865 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
Owen Taylor3473f882001-02-23 17:55:21 +00003866 return(ret);
3867}
3868
3869static xmlNodePtr
3870xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) {
3871 xmlNodePtr ret = NULL;
3872 xmlNodePtr p = NULL,q;
3873
3874 while (node != NULL) {
Daniel Veillard652327a2003-09-29 18:02:38 +00003875#ifdef LIBXML_TREE_ENABLED
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00003876 if (node->type == XML_DTD_NODE ) {
Daniel Veillard4497e692001-06-09 14:19:02 +00003877 if (doc == NULL) {
3878 node = node->next;
3879 continue;
3880 }
Daniel Veillardb33c2012001-04-25 12:59:04 +00003881 if (doc->intSubset == NULL) {
3882 q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node );
3883 q->doc = doc;
3884 q->parent = parent;
3885 doc->intSubset = (xmlDtdPtr) q;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00003886 xmlAddChild(parent, q);
Daniel Veillardb33c2012001-04-25 12:59:04 +00003887 } else {
3888 q = (xmlNodePtr) doc->intSubset;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00003889 xmlAddChild(parent, q);
Daniel Veillardb33c2012001-04-25 12:59:04 +00003890 }
3891 } else
Daniel Veillard652327a2003-09-29 18:02:38 +00003892#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardb33c2012001-04-25 12:59:04 +00003893 q = xmlStaticCopyNode(node, doc, parent, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00003894 if (ret == NULL) {
3895 q->prev = NULL;
3896 ret = p = q;
Daniel Veillardacb2bda2002-01-13 16:15:43 +00003897 } else if (p != q) {
3898 /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */
Owen Taylor3473f882001-02-23 17:55:21 +00003899 p->next = q;
3900 q->prev = p;
3901 p = q;
3902 }
3903 node = node->next;
3904 }
3905 return(ret);
3906}
3907
3908/**
3909 * xmlCopyNode:
3910 * @node: the node
William M. Brack57e9e912004-03-09 16:19:02 +00003911 * @extended: if 1 do a recursive copy (properties, namespaces and children
3912 * when applicable)
3913 * if 2 copy properties and namespaces (when applicable)
Owen Taylor3473f882001-02-23 17:55:21 +00003914 *
3915 * Do a copy of the node.
3916 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003917 * Returns: a new #xmlNodePtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003918 */
3919xmlNodePtr
William M. Brack57e9e912004-03-09 16:19:02 +00003920xmlCopyNode(const xmlNodePtr node, int extended) {
Owen Taylor3473f882001-02-23 17:55:21 +00003921 xmlNodePtr ret;
3922
William M. Brack57e9e912004-03-09 16:19:02 +00003923 ret = xmlStaticCopyNode(node, NULL, NULL, extended);
Owen Taylor3473f882001-02-23 17:55:21 +00003924 return(ret);
3925}
3926
3927/**
Daniel Veillard82daa812001-04-12 08:55:36 +00003928 * xmlDocCopyNode:
3929 * @node: the node
Daniel Veillardd1640922001-12-17 15:30:10 +00003930 * @doc: the document
William M. Brack57e9e912004-03-09 16:19:02 +00003931 * @extended: if 1 do a recursive copy (properties, namespaces and children
3932 * when applicable)
3933 * if 2 copy properties and namespaces (when applicable)
Daniel Veillard82daa812001-04-12 08:55:36 +00003934 *
3935 * Do a copy of the node to a given document.
3936 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003937 * Returns: a new #xmlNodePtr, or NULL in case of error.
Daniel Veillard82daa812001-04-12 08:55:36 +00003938 */
3939xmlNodePtr
William M. Brack57e9e912004-03-09 16:19:02 +00003940xmlDocCopyNode(const xmlNodePtr node, xmlDocPtr doc, int extended) {
Daniel Veillard82daa812001-04-12 08:55:36 +00003941 xmlNodePtr ret;
3942
William M. Brack57e9e912004-03-09 16:19:02 +00003943 ret = xmlStaticCopyNode(node, doc, NULL, extended);
Daniel Veillard82daa812001-04-12 08:55:36 +00003944 return(ret);
3945}
3946
3947/**
Daniel Veillard03a53c32004-10-26 16:06:51 +00003948 * xmlDocCopyNodeList:
3949 * @doc: the target document
3950 * @node: the first node in the list.
3951 *
3952 * Do a recursive copy of the node list.
3953 *
3954 * Returns: a new #xmlNodePtr, or NULL in case of error.
3955 */
3956xmlNodePtr xmlDocCopyNodeList(xmlDocPtr doc, const xmlNodePtr node) {
3957 xmlNodePtr ret = xmlStaticCopyNodeList(node, doc, NULL);
3958 return(ret);
3959}
3960
3961/**
Owen Taylor3473f882001-02-23 17:55:21 +00003962 * xmlCopyNodeList:
3963 * @node: the first node in the list.
3964 *
3965 * Do a recursive copy of the node list.
Daniel Veillard03a53c32004-10-26 16:06:51 +00003966 * Use xmlDocCopyNodeList() if possible to ensure string interning.
Owen Taylor3473f882001-02-23 17:55:21 +00003967 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003968 * Returns: a new #xmlNodePtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003969 */
Daniel Veillard3ec4c612001-08-28 20:39:49 +00003970xmlNodePtr xmlCopyNodeList(const xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00003971 xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL);
3972 return(ret);
3973}
3974
Daniel Veillard2156d432004-03-04 15:59:36 +00003975#if defined(LIBXML_TREE_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00003976/**
Owen Taylor3473f882001-02-23 17:55:21 +00003977 * xmlCopyDtd:
3978 * @dtd: the dtd
3979 *
3980 * Do a copy of the dtd.
3981 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003982 * Returns: a new #xmlDtdPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003983 */
3984xmlDtdPtr
3985xmlCopyDtd(xmlDtdPtr dtd) {
3986 xmlDtdPtr ret;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00003987 xmlNodePtr cur, p = NULL, q;
Owen Taylor3473f882001-02-23 17:55:21 +00003988
3989 if (dtd == NULL) return(NULL);
3990 ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID);
3991 if (ret == NULL) return(NULL);
3992 if (dtd->entities != NULL)
3993 ret->entities = (void *) xmlCopyEntitiesTable(
3994 (xmlEntitiesTablePtr) dtd->entities);
3995 if (dtd->notations != NULL)
3996 ret->notations = (void *) xmlCopyNotationTable(
3997 (xmlNotationTablePtr) dtd->notations);
3998 if (dtd->elements != NULL)
3999 ret->elements = (void *) xmlCopyElementTable(
4000 (xmlElementTablePtr) dtd->elements);
4001 if (dtd->attributes != NULL)
4002 ret->attributes = (void *) xmlCopyAttributeTable(
4003 (xmlAttributeTablePtr) dtd->attributes);
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004004 if (dtd->pentities != NULL)
4005 ret->pentities = (void *) xmlCopyEntitiesTable(
4006 (xmlEntitiesTablePtr) dtd->pentities);
4007
4008 cur = dtd->children;
4009 while (cur != NULL) {
4010 q = NULL;
4011
4012 if (cur->type == XML_ENTITY_DECL) {
4013 xmlEntityPtr tmp = (xmlEntityPtr) cur;
4014 switch (tmp->etype) {
4015 case XML_INTERNAL_GENERAL_ENTITY:
4016 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
4017 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
4018 q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name);
4019 break;
4020 case XML_INTERNAL_PARAMETER_ENTITY:
4021 case XML_EXTERNAL_PARAMETER_ENTITY:
4022 q = (xmlNodePtr)
4023 xmlGetParameterEntityFromDtd(ret, tmp->name);
4024 break;
4025 case XML_INTERNAL_PREDEFINED_ENTITY:
4026 break;
4027 }
4028 } else if (cur->type == XML_ELEMENT_DECL) {
4029 xmlElementPtr tmp = (xmlElementPtr) cur;
4030 q = (xmlNodePtr)
4031 xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix);
4032 } else if (cur->type == XML_ATTRIBUTE_DECL) {
4033 xmlAttributePtr tmp = (xmlAttributePtr) cur;
4034 q = (xmlNodePtr)
4035 xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix);
4036 } else if (cur->type == XML_COMMENT_NODE) {
4037 q = xmlCopyNode(cur, 0);
4038 }
4039
4040 if (q == NULL) {
4041 cur = cur->next;
4042 continue;
4043 }
4044
4045 if (p == NULL)
4046 ret->children = q;
4047 else
4048 p->next = q;
4049
4050 q->prev = p;
4051 q->parent = (xmlNodePtr) ret;
4052 q->next = NULL;
4053 ret->last = q;
4054 p = q;
4055 cur = cur->next;
4056 }
4057
Owen Taylor3473f882001-02-23 17:55:21 +00004058 return(ret);
4059}
Daniel Veillard2156d432004-03-04 15:59:36 +00004060#endif
Owen Taylor3473f882001-02-23 17:55:21 +00004061
Daniel Veillard2156d432004-03-04 15:59:36 +00004062#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004063/**
4064 * xmlCopyDoc:
4065 * @doc: the document
William M. Brack57e9e912004-03-09 16:19:02 +00004066 * @recursive: if not zero do a recursive copy.
Owen Taylor3473f882001-02-23 17:55:21 +00004067 *
4068 * Do a copy of the document info. If recursive, the content tree will
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004069 * be copied too as well as DTD, namespaces and entities.
Owen Taylor3473f882001-02-23 17:55:21 +00004070 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004071 * Returns: a new #xmlDocPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004072 */
4073xmlDocPtr
4074xmlCopyDoc(xmlDocPtr doc, int recursive) {
4075 xmlDocPtr ret;
4076
4077 if (doc == NULL) return(NULL);
4078 ret = xmlNewDoc(doc->version);
4079 if (ret == NULL) return(NULL);
4080 if (doc->name != NULL)
4081 ret->name = xmlMemStrdup(doc->name);
4082 if (doc->encoding != NULL)
4083 ret->encoding = xmlStrdup(doc->encoding);
Daniel Veillardf59507d2005-01-27 17:26:49 +00004084 if (doc->URL != NULL)
4085 ret->URL = xmlStrdup(doc->URL);
Owen Taylor3473f882001-02-23 17:55:21 +00004086 ret->charset = doc->charset;
4087 ret->compression = doc->compression;
4088 ret->standalone = doc->standalone;
4089 if (!recursive) return(ret);
4090
Daniel Veillardb33c2012001-04-25 12:59:04 +00004091 ret->last = NULL;
4092 ret->children = NULL;
Daniel Veillard2156d432004-03-04 15:59:36 +00004093#ifdef LIBXML_TREE_ENABLED
Daniel Veillardb33c2012001-04-25 12:59:04 +00004094 if (doc->intSubset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004095 ret->intSubset = xmlCopyDtd(doc->intSubset);
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004096 xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret);
Daniel Veillardb33c2012001-04-25 12:59:04 +00004097 ret->intSubset->parent = ret;
4098 }
Daniel Veillard2156d432004-03-04 15:59:36 +00004099#endif
Owen Taylor3473f882001-02-23 17:55:21 +00004100 if (doc->oldNs != NULL)
4101 ret->oldNs = xmlCopyNamespaceList(doc->oldNs);
4102 if (doc->children != NULL) {
4103 xmlNodePtr tmp;
Daniel Veillardb33c2012001-04-25 12:59:04 +00004104
4105 ret->children = xmlStaticCopyNodeList(doc->children, ret,
4106 (xmlNodePtr)ret);
Owen Taylor3473f882001-02-23 17:55:21 +00004107 ret->last = NULL;
4108 tmp = ret->children;
4109 while (tmp != NULL) {
4110 if (tmp->next == NULL)
4111 ret->last = tmp;
4112 tmp = tmp->next;
4113 }
4114 }
4115 return(ret);
4116}
Daniel Veillard652327a2003-09-29 18:02:38 +00004117#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004118
4119/************************************************************************
4120 * *
4121 * Content access functions *
4122 * *
4123 ************************************************************************/
4124
4125/**
Daniel Veillard8faa7832001-11-26 15:58:08 +00004126 * xmlGetLineNo:
Daniel Veillard01c13b52002-12-10 15:19:08 +00004127 * @node: valid node
Daniel Veillard8faa7832001-11-26 15:58:08 +00004128 *
William M. Brackd7cf7f82003-11-14 07:13:16 +00004129 * Get line number of @node. This requires activation of this option
Daniel Veillardd1640922001-12-17 15:30:10 +00004130 * before invoking the parser by calling xmlLineNumbersDefault(1)
Daniel Veillard8faa7832001-11-26 15:58:08 +00004131 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004132 * Returns the line number if successful, -1 otherwise
Daniel Veillard8faa7832001-11-26 15:58:08 +00004133 */
4134long
4135xmlGetLineNo(xmlNodePtr node)
4136{
4137 long result = -1;
4138
4139 if (!node)
4140 return result;
4141 if (node->type == XML_ELEMENT_NODE)
Daniel Veillard3e35f8e2003-10-21 00:05:38 +00004142 result = (long) node->line;
Daniel Veillard8faa7832001-11-26 15:58:08 +00004143 else if ((node->prev != NULL) &&
4144 ((node->prev->type == XML_ELEMENT_NODE) ||
4145 (node->prev->type == XML_TEXT_NODE)))
4146 result = xmlGetLineNo(node->prev);
4147 else if ((node->parent != NULL) &&
4148 ((node->parent->type == XML_ELEMENT_NODE) ||
4149 (node->parent->type == XML_TEXT_NODE)))
4150 result = xmlGetLineNo(node->parent);
4151
4152 return result;
4153}
4154
Daniel Veillard2156d432004-03-04 15:59:36 +00004155#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED)
Daniel Veillard8faa7832001-11-26 15:58:08 +00004156/**
4157 * xmlGetNodePath:
4158 * @node: a node
4159 *
4160 * Build a structure based Path for the given node
4161 *
4162 * Returns the new path or NULL in case of error. The caller must free
4163 * the returned string
4164 */
4165xmlChar *
4166xmlGetNodePath(xmlNodePtr node)
4167{
4168 xmlNodePtr cur, tmp, next;
4169 xmlChar *buffer = NULL, *temp;
4170 size_t buf_len;
4171 xmlChar *buf;
Daniel Veillard96c3a3b2002-10-14 15:39:04 +00004172 const char *sep;
Daniel Veillard8faa7832001-11-26 15:58:08 +00004173 const char *name;
4174 char nametemp[100];
4175 int occur = 0;
4176
4177 if (node == NULL)
4178 return (NULL);
4179
4180 buf_len = 500;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004181 buffer = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004182 if (buffer == NULL) {
4183 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004184 return (NULL);
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004185 }
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004186 buf = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
Daniel Veillard8faa7832001-11-26 15:58:08 +00004187 if (buf == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004188 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004189 xmlFree(buffer);
4190 return (NULL);
4191 }
4192
4193 buffer[0] = 0;
4194 cur = node;
4195 do {
4196 name = "";
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004197 sep = "?";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004198 occur = 0;
4199 if ((cur->type == XML_DOCUMENT_NODE) ||
4200 (cur->type == XML_HTML_DOCUMENT_NODE)) {
4201 if (buffer[0] == '/')
4202 break;
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004203 sep = "/";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004204 next = NULL;
4205 } else if (cur->type == XML_ELEMENT_NODE) {
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004206 sep = "/";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004207 name = (const char *) cur->name;
4208 if (cur->ns) {
William M. Brack84d83e32003-12-23 03:45:17 +00004209 if (cur->ns->prefix != NULL)
William M. Brack13dfa872004-09-18 04:52:08 +00004210 snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
4211 (char *)cur->ns->prefix, (char *)cur->name);
William M. Brack84d83e32003-12-23 03:45:17 +00004212 else
William M. Brack13dfa872004-09-18 04:52:08 +00004213 snprintf(nametemp, sizeof(nametemp) - 1, "%s",
4214 (char *)cur->name);
Daniel Veillard8faa7832001-11-26 15:58:08 +00004215 nametemp[sizeof(nametemp) - 1] = 0;
4216 name = nametemp;
4217 }
4218 next = cur->parent;
4219
4220 /*
4221 * Thumbler index computation
Daniel Veillardc00cda82003-04-07 10:22:39 +00004222 * TODO: the ocurence test seems bogus for namespaced names
Daniel Veillard8faa7832001-11-26 15:58:08 +00004223 */
4224 tmp = cur->prev;
4225 while (tmp != NULL) {
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00004226 if ((tmp->type == XML_ELEMENT_NODE) &&
Daniel Veillard0996a162005-02-05 14:00:10 +00004227 (xmlStrEqual(cur->name, tmp->name)) &&
4228 ((tmp->ns == cur->ns) ||
4229 ((tmp->ns != NULL) && (cur->ns != NULL) &&
4230 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004231 occur++;
4232 tmp = tmp->prev;
4233 }
4234 if (occur == 0) {
4235 tmp = cur->next;
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004236 while (tmp != NULL && occur == 0) {
4237 if ((tmp->type == XML_ELEMENT_NODE) &&
Daniel Veillard0996a162005-02-05 14:00:10 +00004238 (xmlStrEqual(cur->name, tmp->name)) &&
4239 ((tmp->ns == cur->ns) ||
4240 ((tmp->ns != NULL) && (cur->ns != NULL) &&
4241 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004242 occur++;
4243 tmp = tmp->next;
4244 }
4245 if (occur != 0)
4246 occur = 1;
4247 } else
4248 occur++;
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004249 } else if (cur->type == XML_COMMENT_NODE) {
4250 sep = "/";
4251 name = "comment()";
4252 next = cur->parent;
4253
4254 /*
4255 * Thumbler index computation
4256 */
4257 tmp = cur->prev;
4258 while (tmp != NULL) {
4259 if (tmp->type == XML_COMMENT_NODE)
4260 occur++;
4261 tmp = tmp->prev;
4262 }
4263 if (occur == 0) {
4264 tmp = cur->next;
4265 while (tmp != NULL && occur == 0) {
4266 if (tmp->type == XML_COMMENT_NODE)
4267 occur++;
4268 tmp = tmp->next;
4269 }
4270 if (occur != 0)
4271 occur = 1;
4272 } else
4273 occur++;
4274 } else if ((cur->type == XML_TEXT_NODE) ||
4275 (cur->type == XML_CDATA_SECTION_NODE)) {
4276 sep = "/";
4277 name = "text()";
4278 next = cur->parent;
4279
4280 /*
4281 * Thumbler index computation
4282 */
4283 tmp = cur->prev;
4284 while (tmp != NULL) {
4285 if ((cur->type == XML_TEXT_NODE) ||
4286 (cur->type == XML_CDATA_SECTION_NODE))
4287 occur++;
4288 tmp = tmp->prev;
4289 }
4290 if (occur == 0) {
4291 tmp = cur->next;
4292 while (tmp != NULL && occur == 0) {
Daniel Veillard1f8658a2004-08-14 21:46:31 +00004293 if ((tmp->type == XML_TEXT_NODE) ||
4294 (tmp->type == XML_CDATA_SECTION_NODE))
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004295 occur++;
4296 tmp = tmp->next;
4297 }
4298 if (occur != 0)
4299 occur = 1;
4300 } else
4301 occur++;
4302 } else if (cur->type == XML_PI_NODE) {
4303 sep = "/";
4304 snprintf(nametemp, sizeof(nametemp) - 1,
William M. Brack13dfa872004-09-18 04:52:08 +00004305 "processing-instruction('%s')", (char *)cur->name);
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004306 nametemp[sizeof(nametemp) - 1] = 0;
4307 name = nametemp;
4308
4309 next = cur->parent;
4310
4311 /*
4312 * Thumbler index computation
4313 */
4314 tmp = cur->prev;
4315 while (tmp != NULL) {
4316 if ((tmp->type == XML_PI_NODE) &&
4317 (xmlStrEqual(cur->name, tmp->name)))
4318 occur++;
4319 tmp = tmp->prev;
4320 }
4321 if (occur == 0) {
4322 tmp = cur->next;
4323 while (tmp != NULL && occur == 0) {
4324 if ((tmp->type == XML_PI_NODE) &&
4325 (xmlStrEqual(cur->name, tmp->name)))
4326 occur++;
4327 tmp = tmp->next;
4328 }
4329 if (occur != 0)
4330 occur = 1;
4331 } else
4332 occur++;
4333
Daniel Veillard8faa7832001-11-26 15:58:08 +00004334 } else if (cur->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004335 sep = "/@";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004336 name = (const char *) (((xmlAttrPtr) cur)->name);
Daniel Veillard365c8062005-07-19 11:31:55 +00004337 if (cur->ns) {
4338 if (cur->ns->prefix != NULL)
4339 snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
4340 (char *)cur->ns->prefix, (char *)cur->name);
4341 else
4342 snprintf(nametemp, sizeof(nametemp) - 1, "%s",
4343 (char *)cur->name);
4344 nametemp[sizeof(nametemp) - 1] = 0;
4345 name = nametemp;
4346 }
Daniel Veillard8faa7832001-11-26 15:58:08 +00004347 next = ((xmlAttrPtr) cur)->parent;
4348 } else {
4349 next = cur->parent;
4350 }
4351
4352 /*
4353 * Make sure there is enough room
4354 */
4355 if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) {
4356 buf_len =
4357 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20;
4358 temp = (xmlChar *) xmlRealloc(buffer, buf_len);
4359 if (temp == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004360 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004361 xmlFree(buf);
4362 xmlFree(buffer);
4363 return (NULL);
4364 }
4365 buffer = temp;
4366 temp = (xmlChar *) xmlRealloc(buf, buf_len);
4367 if (temp == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004368 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004369 xmlFree(buf);
4370 xmlFree(buffer);
4371 return (NULL);
4372 }
4373 buf = temp;
4374 }
4375 if (occur == 0)
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004376 snprintf((char *) buf, buf_len, "%s%s%s",
Daniel Veillard8faa7832001-11-26 15:58:08 +00004377 sep, name, (char *) buffer);
4378 else
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004379 snprintf((char *) buf, buf_len, "%s%s[%d]%s",
Daniel Veillard8faa7832001-11-26 15:58:08 +00004380 sep, name, occur, (char *) buffer);
William M. Brack13dfa872004-09-18 04:52:08 +00004381 snprintf((char *) buffer, buf_len, "%s", (char *)buf);
Daniel Veillard8faa7832001-11-26 15:58:08 +00004382 cur = next;
4383 } while (cur != NULL);
4384 xmlFree(buf);
4385 return (buffer);
4386}
Daniel Veillard652327a2003-09-29 18:02:38 +00004387#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard8faa7832001-11-26 15:58:08 +00004388
4389/**
Owen Taylor3473f882001-02-23 17:55:21 +00004390 * xmlDocGetRootElement:
4391 * @doc: the document
4392 *
4393 * Get the root element of the document (doc->children is a list
4394 * containing possibly comments, PIs, etc ...).
4395 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004396 * Returns the #xmlNodePtr for the root or NULL
Owen Taylor3473f882001-02-23 17:55:21 +00004397 */
4398xmlNodePtr
4399xmlDocGetRootElement(xmlDocPtr doc) {
4400 xmlNodePtr ret;
4401
4402 if (doc == NULL) return(NULL);
4403 ret = doc->children;
4404 while (ret != NULL) {
4405 if (ret->type == XML_ELEMENT_NODE)
4406 return(ret);
4407 ret = ret->next;
4408 }
4409 return(ret);
4410}
4411
Daniel Veillard2156d432004-03-04 15:59:36 +00004412#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004413/**
4414 * xmlDocSetRootElement:
4415 * @doc: the document
4416 * @root: the new document root element
4417 *
4418 * Set the root element of the document (doc->children is a list
4419 * containing possibly comments, PIs, etc ...).
4420 *
4421 * Returns the old root element if any was found
4422 */
4423xmlNodePtr
4424xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) {
4425 xmlNodePtr old = NULL;
4426
4427 if (doc == NULL) return(NULL);
Daniel Veillardc575b992002-02-08 13:28:40 +00004428 if (root == NULL)
4429 return(NULL);
4430 xmlUnlinkNode(root);
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00004431 xmlSetTreeDoc(root, doc);
Daniel Veillardc575b992002-02-08 13:28:40 +00004432 root->parent = (xmlNodePtr) doc;
Owen Taylor3473f882001-02-23 17:55:21 +00004433 old = doc->children;
4434 while (old != NULL) {
4435 if (old->type == XML_ELEMENT_NODE)
4436 break;
4437 old = old->next;
4438 }
4439 if (old == NULL) {
4440 if (doc->children == NULL) {
4441 doc->children = root;
4442 doc->last = root;
4443 } else {
4444 xmlAddSibling(doc->children, root);
4445 }
4446 } else {
4447 xmlReplaceNode(old, root);
4448 }
4449 return(old);
4450}
Daniel Veillard2156d432004-03-04 15:59:36 +00004451#endif
Owen Taylor3473f882001-02-23 17:55:21 +00004452
Daniel Veillard2156d432004-03-04 15:59:36 +00004453#if defined(LIBXML_TREE_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004454/**
4455 * xmlNodeSetLang:
4456 * @cur: the node being changed
Daniel Veillardd1640922001-12-17 15:30:10 +00004457 * @lang: the language description
Owen Taylor3473f882001-02-23 17:55:21 +00004458 *
4459 * Set the language of a node, i.e. the values of the xml:lang
4460 * attribute.
4461 */
4462void
4463xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004464 xmlNsPtr ns;
4465
Owen Taylor3473f882001-02-23 17:55:21 +00004466 if (cur == NULL) return;
4467 switch(cur->type) {
4468 case XML_TEXT_NODE:
4469 case XML_CDATA_SECTION_NODE:
4470 case XML_COMMENT_NODE:
4471 case XML_DOCUMENT_NODE:
4472 case XML_DOCUMENT_TYPE_NODE:
4473 case XML_DOCUMENT_FRAG_NODE:
4474 case XML_NOTATION_NODE:
4475 case XML_HTML_DOCUMENT_NODE:
4476 case XML_DTD_NODE:
4477 case XML_ELEMENT_DECL:
4478 case XML_ATTRIBUTE_DECL:
4479 case XML_ENTITY_DECL:
4480 case XML_PI_NODE:
4481 case XML_ENTITY_REF_NODE:
4482 case XML_ENTITY_NODE:
4483 case XML_NAMESPACE_DECL:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004484#ifdef LIBXML_DOCB_ENABLED
4485 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004486#endif
4487 case XML_XINCLUDE_START:
4488 case XML_XINCLUDE_END:
4489 return;
4490 case XML_ELEMENT_NODE:
4491 case XML_ATTRIBUTE_NODE:
4492 break;
4493 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004494 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4495 if (ns == NULL)
4496 return;
4497 xmlSetNsProp(cur, ns, BAD_CAST "lang", lang);
Owen Taylor3473f882001-02-23 17:55:21 +00004498}
Daniel Veillard652327a2003-09-29 18:02:38 +00004499#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004500
4501/**
4502 * xmlNodeGetLang:
4503 * @cur: the node being checked
4504 *
4505 * Searches the language of a node, i.e. the values of the xml:lang
4506 * attribute or the one carried by the nearest ancestor.
4507 *
4508 * Returns a pointer to the lang value, or NULL if not found
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004509 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00004510 */
4511xmlChar *
4512xmlNodeGetLang(xmlNodePtr cur) {
4513 xmlChar *lang;
4514
4515 while (cur != NULL) {
Daniel Veillardc17337c2001-05-09 10:51:31 +00004516 lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE);
Owen Taylor3473f882001-02-23 17:55:21 +00004517 if (lang != NULL)
4518 return(lang);
4519 cur = cur->parent;
4520 }
4521 return(NULL);
4522}
4523
4524
Daniel Veillard652327a2003-09-29 18:02:38 +00004525#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00004526/**
4527 * xmlNodeSetSpacePreserve:
4528 * @cur: the node being changed
4529 * @val: the xml:space value ("0": default, 1: "preserve")
4530 *
4531 * Set (or reset) the space preserving behaviour of a node, i.e. the
4532 * value of the xml:space attribute.
4533 */
4534void
4535xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004536 xmlNsPtr ns;
4537
Owen Taylor3473f882001-02-23 17:55:21 +00004538 if (cur == NULL) return;
4539 switch(cur->type) {
4540 case XML_TEXT_NODE:
4541 case XML_CDATA_SECTION_NODE:
4542 case XML_COMMENT_NODE:
4543 case XML_DOCUMENT_NODE:
4544 case XML_DOCUMENT_TYPE_NODE:
4545 case XML_DOCUMENT_FRAG_NODE:
4546 case XML_NOTATION_NODE:
4547 case XML_HTML_DOCUMENT_NODE:
4548 case XML_DTD_NODE:
4549 case XML_ELEMENT_DECL:
4550 case XML_ATTRIBUTE_DECL:
4551 case XML_ENTITY_DECL:
4552 case XML_PI_NODE:
4553 case XML_ENTITY_REF_NODE:
4554 case XML_ENTITY_NODE:
4555 case XML_NAMESPACE_DECL:
4556 case XML_XINCLUDE_START:
4557 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004558#ifdef LIBXML_DOCB_ENABLED
4559 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004560#endif
4561 return;
4562 case XML_ELEMENT_NODE:
4563 case XML_ATTRIBUTE_NODE:
4564 break;
4565 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004566 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4567 if (ns == NULL)
4568 return;
Owen Taylor3473f882001-02-23 17:55:21 +00004569 switch (val) {
4570 case 0:
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004571 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "default");
Owen Taylor3473f882001-02-23 17:55:21 +00004572 break;
4573 case 1:
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004574 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "preserve");
Owen Taylor3473f882001-02-23 17:55:21 +00004575 break;
4576 }
4577}
Daniel Veillard652327a2003-09-29 18:02:38 +00004578#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004579
4580/**
4581 * xmlNodeGetSpacePreserve:
4582 * @cur: the node being checked
4583 *
4584 * Searches the space preserving behaviour of a node, i.e. the values
4585 * of the xml:space attribute or the one carried by the nearest
4586 * ancestor.
4587 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004588 * Returns -1 if xml:space is not inherited, 0 if "default", 1 if "preserve"
Owen Taylor3473f882001-02-23 17:55:21 +00004589 */
4590int
4591xmlNodeGetSpacePreserve(xmlNodePtr cur) {
4592 xmlChar *space;
4593
4594 while (cur != NULL) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004595 space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE);
Owen Taylor3473f882001-02-23 17:55:21 +00004596 if (space != NULL) {
4597 if (xmlStrEqual(space, BAD_CAST "preserve")) {
4598 xmlFree(space);
4599 return(1);
4600 }
4601 if (xmlStrEqual(space, BAD_CAST "default")) {
4602 xmlFree(space);
4603 return(0);
4604 }
4605 xmlFree(space);
4606 }
4607 cur = cur->parent;
4608 }
4609 return(-1);
4610}
4611
Daniel Veillard652327a2003-09-29 18:02:38 +00004612#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00004613/**
4614 * xmlNodeSetName:
4615 * @cur: the node being changed
4616 * @name: the new tag name
4617 *
4618 * Set (or reset) the name of a node.
4619 */
4620void
4621xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) {
Daniel Veillardce244ad2004-11-05 10:03:46 +00004622 xmlDocPtr doc;
4623 xmlDictPtr dict;
4624
Owen Taylor3473f882001-02-23 17:55:21 +00004625 if (cur == NULL) return;
4626 if (name == NULL) return;
4627 switch(cur->type) {
4628 case XML_TEXT_NODE:
4629 case XML_CDATA_SECTION_NODE:
4630 case XML_COMMENT_NODE:
4631 case XML_DOCUMENT_TYPE_NODE:
4632 case XML_DOCUMENT_FRAG_NODE:
4633 case XML_NOTATION_NODE:
4634 case XML_HTML_DOCUMENT_NODE:
4635 case XML_NAMESPACE_DECL:
4636 case XML_XINCLUDE_START:
4637 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004638#ifdef LIBXML_DOCB_ENABLED
4639 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004640#endif
4641 return;
4642 case XML_ELEMENT_NODE:
4643 case XML_ATTRIBUTE_NODE:
4644 case XML_PI_NODE:
4645 case XML_ENTITY_REF_NODE:
4646 case XML_ENTITY_NODE:
4647 case XML_DTD_NODE:
4648 case XML_DOCUMENT_NODE:
4649 case XML_ELEMENT_DECL:
4650 case XML_ATTRIBUTE_DECL:
4651 case XML_ENTITY_DECL:
4652 break;
4653 }
Daniel Veillardce244ad2004-11-05 10:03:46 +00004654 doc = cur->doc;
4655 if (doc != NULL)
4656 dict = doc->dict;
4657 else
4658 dict = NULL;
4659 if (dict != NULL) {
4660 if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name)))
4661 xmlFree((xmlChar *) cur->name);
4662 cur->name = xmlDictLookup(dict, name, -1);
4663 } else {
4664 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
4665 cur->name = xmlStrdup(name);
4666 }
Owen Taylor3473f882001-02-23 17:55:21 +00004667}
Daniel Veillard2156d432004-03-04 15:59:36 +00004668#endif
Owen Taylor3473f882001-02-23 17:55:21 +00004669
Daniel Veillard2156d432004-03-04 15:59:36 +00004670#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004671/**
4672 * xmlNodeSetBase:
4673 * @cur: the node being changed
4674 * @uri: the new base URI
4675 *
4676 * Set (or reset) the base URI of a node, i.e. the value of the
4677 * xml:base attribute.
4678 */
4679void
Daniel Veillardf85ce8e2003-09-22 10:24:45 +00004680xmlNodeSetBase(xmlNodePtr cur, const xmlChar* uri) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004681 xmlNsPtr ns;
4682
Owen Taylor3473f882001-02-23 17:55:21 +00004683 if (cur == NULL) return;
4684 switch(cur->type) {
4685 case XML_TEXT_NODE:
4686 case XML_CDATA_SECTION_NODE:
4687 case XML_COMMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004688 case XML_DOCUMENT_TYPE_NODE:
4689 case XML_DOCUMENT_FRAG_NODE:
4690 case XML_NOTATION_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004691 case XML_DTD_NODE:
4692 case XML_ELEMENT_DECL:
4693 case XML_ATTRIBUTE_DECL:
4694 case XML_ENTITY_DECL:
4695 case XML_PI_NODE:
4696 case XML_ENTITY_REF_NODE:
4697 case XML_ENTITY_NODE:
4698 case XML_NAMESPACE_DECL:
4699 case XML_XINCLUDE_START:
4700 case XML_XINCLUDE_END:
Owen Taylor3473f882001-02-23 17:55:21 +00004701 return;
4702 case XML_ELEMENT_NODE:
4703 case XML_ATTRIBUTE_NODE:
4704 break;
Daniel Veillard4cbe4702002-05-05 06:57:27 +00004705 case XML_DOCUMENT_NODE:
4706#ifdef LIBXML_DOCB_ENABLED
4707 case XML_DOCB_DOCUMENT_NODE:
4708#endif
4709 case XML_HTML_DOCUMENT_NODE: {
4710 xmlDocPtr doc = (xmlDocPtr) cur;
4711
4712 if (doc->URL != NULL)
4713 xmlFree((xmlChar *) doc->URL);
4714 if (uri == NULL)
4715 doc->URL = NULL;
4716 else
4717 doc->URL = xmlStrdup(uri);
4718 return;
4719 }
Owen Taylor3473f882001-02-23 17:55:21 +00004720 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004721
4722 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4723 if (ns == NULL)
4724 return;
4725 xmlSetNsProp(cur, ns, BAD_CAST "base", uri);
Owen Taylor3473f882001-02-23 17:55:21 +00004726}
Daniel Veillard652327a2003-09-29 18:02:38 +00004727#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004728
4729/**
Owen Taylor3473f882001-02-23 17:55:21 +00004730 * xmlNodeGetBase:
4731 * @doc: the document the node pertains to
4732 * @cur: the node being checked
4733 *
4734 * Searches for the BASE URL. The code should work on both XML
4735 * and HTML document even if base mechanisms are completely different.
4736 * It returns the base as defined in RFC 2396 sections
4737 * 5.1.1. Base URI within Document Content
4738 * and
4739 * 5.1.2. Base URI from the Encapsulating Entity
4740 * However it does not return the document base (5.1.3), use
4741 * xmlDocumentGetBase() for this
4742 *
4743 * Returns a pointer to the base URL, or NULL if not found
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004744 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00004745 */
4746xmlChar *
4747xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00004748 xmlChar *oldbase = NULL;
4749 xmlChar *base, *newbase;
Owen Taylor3473f882001-02-23 17:55:21 +00004750
4751 if ((cur == NULL) && (doc == NULL))
4752 return(NULL);
4753 if (doc == NULL) doc = cur->doc;
4754 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
4755 cur = doc->children;
4756 while ((cur != NULL) && (cur->name != NULL)) {
4757 if (cur->type != XML_ELEMENT_NODE) {
4758 cur = cur->next;
4759 continue;
4760 }
4761 if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) {
4762 cur = cur->children;
4763 continue;
4764 }
4765 if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) {
4766 cur = cur->children;
4767 continue;
4768 }
4769 if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) {
4770 return(xmlGetProp(cur, BAD_CAST "href"));
4771 }
4772 cur = cur->next;
4773 }
4774 return(NULL);
4775 }
4776 while (cur != NULL) {
4777 if (cur->type == XML_ENTITY_DECL) {
4778 xmlEntityPtr ent = (xmlEntityPtr) cur;
4779 return(xmlStrdup(ent->URI));
4780 }
Daniel Veillard42596ad2001-05-22 16:57:14 +00004781 if (cur->type == XML_ELEMENT_NODE) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00004782 base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE);
Daniel Veillard42596ad2001-05-22 16:57:14 +00004783 if (base != NULL) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00004784 if (oldbase != NULL) {
4785 newbase = xmlBuildURI(oldbase, base);
4786 if (newbase != NULL) {
4787 xmlFree(oldbase);
4788 xmlFree(base);
4789 oldbase = newbase;
4790 } else {
4791 xmlFree(oldbase);
4792 xmlFree(base);
4793 return(NULL);
4794 }
4795 } else {
4796 oldbase = base;
4797 }
4798 if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) ||
4799 (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) ||
4800 (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4)))
4801 return(oldbase);
Daniel Veillard42596ad2001-05-22 16:57:14 +00004802 }
4803 }
Owen Taylor3473f882001-02-23 17:55:21 +00004804 cur = cur->parent;
4805 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00004806 if ((doc != NULL) && (doc->URL != NULL)) {
4807 if (oldbase == NULL)
4808 return(xmlStrdup(doc->URL));
4809 newbase = xmlBuildURI(oldbase, doc->URL);
4810 xmlFree(oldbase);
4811 return(newbase);
4812 }
4813 return(oldbase);
Owen Taylor3473f882001-02-23 17:55:21 +00004814}
4815
4816/**
Daniel Veillard78697292003-10-19 20:44:43 +00004817 * xmlNodeBufGetContent:
4818 * @buffer: a buffer
4819 * @cur: the node being read
4820 *
4821 * Read the value of a node @cur, this can be either the text carried
4822 * directly by this node if it's a TEXT node or the aggregate string
4823 * of the values carried by this node child's (TEXT and ENTITY_REF).
4824 * Entity references are substituted.
4825 * Fills up the buffer @buffer with this value
4826 *
4827 * Returns 0 in case of success and -1 in case of error.
4828 */
4829int
4830xmlNodeBufGetContent(xmlBufferPtr buffer, xmlNodePtr cur)
4831{
4832 if ((cur == NULL) || (buffer == NULL)) return(-1);
4833 switch (cur->type) {
4834 case XML_CDATA_SECTION_NODE:
4835 case XML_TEXT_NODE:
4836 xmlBufferCat(buffer, cur->content);
4837 break;
4838 case XML_DOCUMENT_FRAG_NODE:
4839 case XML_ELEMENT_NODE:{
4840 xmlNodePtr tmp = cur;
4841
4842 while (tmp != NULL) {
4843 switch (tmp->type) {
4844 case XML_CDATA_SECTION_NODE:
4845 case XML_TEXT_NODE:
4846 if (tmp->content != NULL)
4847 xmlBufferCat(buffer, tmp->content);
4848 break;
4849 case XML_ENTITY_REF_NODE:
4850 xmlNodeBufGetContent(buffer, tmp->children);
4851 break;
4852 default:
4853 break;
4854 }
4855 /*
4856 * Skip to next node
4857 */
4858 if (tmp->children != NULL) {
4859 if (tmp->children->type != XML_ENTITY_DECL) {
4860 tmp = tmp->children;
4861 continue;
4862 }
4863 }
4864 if (tmp == cur)
4865 break;
4866
4867 if (tmp->next != NULL) {
4868 tmp = tmp->next;
4869 continue;
4870 }
4871
4872 do {
4873 tmp = tmp->parent;
4874 if (tmp == NULL)
4875 break;
4876 if (tmp == cur) {
4877 tmp = NULL;
4878 break;
4879 }
4880 if (tmp->next != NULL) {
4881 tmp = tmp->next;
4882 break;
4883 }
4884 } while (tmp != NULL);
4885 }
4886 break;
4887 }
4888 case XML_ATTRIBUTE_NODE:{
4889 xmlAttrPtr attr = (xmlAttrPtr) cur;
4890 xmlNodePtr tmp = attr->children;
4891
4892 while (tmp != NULL) {
4893 if (tmp->type == XML_TEXT_NODE)
4894 xmlBufferCat(buffer, tmp->content);
4895 else
4896 xmlNodeBufGetContent(buffer, tmp);
4897 tmp = tmp->next;
4898 }
4899 break;
4900 }
4901 case XML_COMMENT_NODE:
4902 case XML_PI_NODE:
4903 xmlBufferCat(buffer, cur->content);
4904 break;
4905 case XML_ENTITY_REF_NODE:{
4906 xmlEntityPtr ent;
4907 xmlNodePtr tmp;
4908
4909 /* lookup entity declaration */
4910 ent = xmlGetDocEntity(cur->doc, cur->name);
4911 if (ent == NULL)
4912 return(-1);
4913
4914 /* an entity content can be any "well balanced chunk",
4915 * i.e. the result of the content [43] production:
4916 * http://www.w3.org/TR/REC-xml#NT-content
4917 * -> we iterate through child nodes and recursive call
4918 * xmlNodeGetContent() which handles all possible node types */
4919 tmp = ent->children;
4920 while (tmp) {
4921 xmlNodeBufGetContent(buffer, tmp);
4922 tmp = tmp->next;
4923 }
4924 break;
4925 }
4926 case XML_ENTITY_NODE:
4927 case XML_DOCUMENT_TYPE_NODE:
4928 case XML_NOTATION_NODE:
4929 case XML_DTD_NODE:
4930 case XML_XINCLUDE_START:
4931 case XML_XINCLUDE_END:
4932 break;
4933 case XML_DOCUMENT_NODE:
4934#ifdef LIBXML_DOCB_ENABLED
4935 case XML_DOCB_DOCUMENT_NODE:
4936#endif
4937 case XML_HTML_DOCUMENT_NODE:
4938 cur = cur->children;
4939 while (cur!= NULL) {
4940 if ((cur->type == XML_ELEMENT_NODE) ||
4941 (cur->type == XML_TEXT_NODE) ||
4942 (cur->type == XML_CDATA_SECTION_NODE)) {
4943 xmlNodeBufGetContent(buffer, cur);
4944 }
4945 cur = cur->next;
4946 }
4947 break;
4948 case XML_NAMESPACE_DECL:
4949 xmlBufferCat(buffer, ((xmlNsPtr) cur)->href);
4950 break;
4951 case XML_ELEMENT_DECL:
4952 case XML_ATTRIBUTE_DECL:
4953 case XML_ENTITY_DECL:
4954 break;
4955 }
4956 return(0);
4957}
4958/**
Owen Taylor3473f882001-02-23 17:55:21 +00004959 * xmlNodeGetContent:
4960 * @cur: the node being read
4961 *
4962 * Read the value of a node, this can be either the text carried
4963 * directly by this node if it's a TEXT node or the aggregate string
4964 * of the values carried by this node child's (TEXT and ENTITY_REF).
Daniel Veillardd1640922001-12-17 15:30:10 +00004965 * Entity references are substituted.
4966 * Returns a new #xmlChar * or NULL if no content is available.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004967 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00004968 */
4969xmlChar *
Daniel Veillard7646b182002-04-20 06:41:40 +00004970xmlNodeGetContent(xmlNodePtr cur)
4971{
4972 if (cur == NULL)
4973 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004974 switch (cur->type) {
4975 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00004976 case XML_ELEMENT_NODE:{
Daniel Veillard7646b182002-04-20 06:41:40 +00004977 xmlBufferPtr buffer;
4978 xmlChar *ret;
Owen Taylor3473f882001-02-23 17:55:21 +00004979
Daniel Veillard814a76d2003-01-23 18:24:20 +00004980 buffer = xmlBufferCreateSize(64);
Daniel Veillard7646b182002-04-20 06:41:40 +00004981 if (buffer == NULL)
4982 return (NULL);
Daniel Veillardc4696922003-10-19 21:47:14 +00004983 xmlNodeBufGetContent(buffer, cur);
Daniel Veillard7646b182002-04-20 06:41:40 +00004984 ret = buffer->content;
4985 buffer->content = NULL;
4986 xmlBufferFree(buffer);
4987 return (ret);
4988 }
4989 case XML_ATTRIBUTE_NODE:{
4990 xmlAttrPtr attr = (xmlAttrPtr) cur;
4991
4992 if (attr->parent != NULL)
4993 return (xmlNodeListGetString
4994 (attr->parent->doc, attr->children, 1));
4995 else
4996 return (xmlNodeListGetString(NULL, attr->children, 1));
4997 break;
4998 }
Owen Taylor3473f882001-02-23 17:55:21 +00004999 case XML_COMMENT_NODE:
5000 case XML_PI_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005001 if (cur->content != NULL)
5002 return (xmlStrdup(cur->content));
5003 return (NULL);
5004 case XML_ENTITY_REF_NODE:{
5005 xmlEntityPtr ent;
Daniel Veillard7646b182002-04-20 06:41:40 +00005006 xmlBufferPtr buffer;
5007 xmlChar *ret;
5008
5009 /* lookup entity declaration */
5010 ent = xmlGetDocEntity(cur->doc, cur->name);
5011 if (ent == NULL)
5012 return (NULL);
5013
5014 buffer = xmlBufferCreate();
5015 if (buffer == NULL)
5016 return (NULL);
5017
Daniel Veillardc4696922003-10-19 21:47:14 +00005018 xmlNodeBufGetContent(buffer, cur);
Daniel Veillard7646b182002-04-20 06:41:40 +00005019
5020 ret = buffer->content;
5021 buffer->content = NULL;
5022 xmlBufferFree(buffer);
5023 return (ret);
5024 }
Owen Taylor3473f882001-02-23 17:55:21 +00005025 case XML_ENTITY_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005026 case XML_DOCUMENT_TYPE_NODE:
5027 case XML_NOTATION_NODE:
5028 case XML_DTD_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005029 case XML_XINCLUDE_START:
5030 case XML_XINCLUDE_END:
Daniel Veillard9adc0462003-03-24 18:39:54 +00005031 return (NULL);
5032 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005033#ifdef LIBXML_DOCB_ENABLED
Daniel Veillard7646b182002-04-20 06:41:40 +00005034 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005035#endif
Daniel Veillard9adc0462003-03-24 18:39:54 +00005036 case XML_HTML_DOCUMENT_NODE: {
Daniel Veillardc4696922003-10-19 21:47:14 +00005037 xmlBufferPtr buffer;
5038 xmlChar *ret;
Daniel Veillard9adc0462003-03-24 18:39:54 +00005039
Daniel Veillardc4696922003-10-19 21:47:14 +00005040 buffer = xmlBufferCreate();
5041 if (buffer == NULL)
5042 return (NULL);
5043
5044 xmlNodeBufGetContent(buffer, (xmlNodePtr) cur);
5045
5046 ret = buffer->content;
5047 buffer->content = NULL;
5048 xmlBufferFree(buffer);
5049 return (ret);
Daniel Veillard9adc0462003-03-24 18:39:54 +00005050 }
Daniel Veillard96c3a3b2002-10-14 15:39:04 +00005051 case XML_NAMESPACE_DECL: {
5052 xmlChar *tmp;
5053
5054 tmp = xmlStrdup(((xmlNsPtr) cur)->href);
5055 return (tmp);
5056 }
Owen Taylor3473f882001-02-23 17:55:21 +00005057 case XML_ELEMENT_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00005058 /* TODO !!! */
5059 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005060 case XML_ATTRIBUTE_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00005061 /* TODO !!! */
5062 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005063 case XML_ENTITY_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00005064 /* TODO !!! */
5065 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005066 case XML_CDATA_SECTION_NODE:
5067 case XML_TEXT_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005068 if (cur->content != NULL)
5069 return (xmlStrdup(cur->content));
5070 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005071 }
Daniel Veillard7646b182002-04-20 06:41:40 +00005072 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005073}
Daniel Veillard652327a2003-09-29 18:02:38 +00005074
Owen Taylor3473f882001-02-23 17:55:21 +00005075/**
5076 * xmlNodeSetContent:
5077 * @cur: the node being modified
5078 * @content: the new value of the content
5079 *
5080 * Replace the content of a node.
5081 */
5082void
5083xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) {
5084 if (cur == NULL) {
5085#ifdef DEBUG_TREE
5086 xmlGenericError(xmlGenericErrorContext,
5087 "xmlNodeSetContent : node == NULL\n");
5088#endif
5089 return;
5090 }
5091 switch (cur->type) {
5092 case XML_DOCUMENT_FRAG_NODE:
5093 case XML_ELEMENT_NODE:
Daniel Veillard2c748c62002-01-16 15:37:50 +00005094 case XML_ATTRIBUTE_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005095 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5096 cur->children = xmlStringGetNodeList(cur->doc, content);
5097 UPDATE_LAST_CHILD_AND_PARENT(cur)
5098 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005099 case XML_TEXT_NODE:
5100 case XML_CDATA_SECTION_NODE:
5101 case XML_ENTITY_REF_NODE:
5102 case XML_ENTITY_NODE:
5103 case XML_PI_NODE:
5104 case XML_COMMENT_NODE:
5105 if (cur->content != NULL) {
William M. Brack7762bb12004-01-04 14:49:01 +00005106 if (!((cur->doc != NULL) && (cur->doc->dict != NULL) &&
Daniel Veillardc587bce2005-05-10 15:28:08 +00005107 (xmlDictOwns(cur->doc->dict, cur->content))))
William M. Brack7762bb12004-01-04 14:49:01 +00005108 xmlFree(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00005109 }
5110 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5111 cur->last = cur->children = NULL;
5112 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005113 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00005114 } else
5115 cur->content = NULL;
5116 break;
5117 case XML_DOCUMENT_NODE:
5118 case XML_HTML_DOCUMENT_NODE:
5119 case XML_DOCUMENT_TYPE_NODE:
5120 case XML_XINCLUDE_START:
5121 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005122#ifdef LIBXML_DOCB_ENABLED
5123 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005124#endif
5125 break;
5126 case XML_NOTATION_NODE:
5127 break;
5128 case XML_DTD_NODE:
5129 break;
5130 case XML_NAMESPACE_DECL:
5131 break;
5132 case XML_ELEMENT_DECL:
5133 /* TODO !!! */
5134 break;
5135 case XML_ATTRIBUTE_DECL:
5136 /* TODO !!! */
5137 break;
5138 case XML_ENTITY_DECL:
5139 /* TODO !!! */
5140 break;
5141 }
5142}
5143
Daniel Veillard652327a2003-09-29 18:02:38 +00005144#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00005145/**
5146 * xmlNodeSetContentLen:
5147 * @cur: the node being modified
5148 * @content: the new value of the content
5149 * @len: the size of @content
5150 *
5151 * Replace the content of a node.
5152 */
5153void
5154xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5155 if (cur == NULL) {
5156#ifdef DEBUG_TREE
5157 xmlGenericError(xmlGenericErrorContext,
5158 "xmlNodeSetContentLen : node == NULL\n");
5159#endif
5160 return;
5161 }
5162 switch (cur->type) {
5163 case XML_DOCUMENT_FRAG_NODE:
5164 case XML_ELEMENT_NODE:
Daniel Veillard2c748c62002-01-16 15:37:50 +00005165 case XML_ATTRIBUTE_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005166 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5167 cur->children = xmlStringLenGetNodeList(cur->doc, content, len);
5168 UPDATE_LAST_CHILD_AND_PARENT(cur)
5169 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005170 case XML_TEXT_NODE:
5171 case XML_CDATA_SECTION_NODE:
5172 case XML_ENTITY_REF_NODE:
5173 case XML_ENTITY_NODE:
5174 case XML_PI_NODE:
5175 case XML_COMMENT_NODE:
5176 case XML_NOTATION_NODE:
5177 if (cur->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005178 xmlFree(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00005179 }
5180 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5181 cur->children = cur->last = NULL;
5182 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005183 cur->content = xmlStrndup(content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00005184 } else
5185 cur->content = NULL;
5186 break;
5187 case XML_DOCUMENT_NODE:
5188 case XML_DTD_NODE:
5189 case XML_HTML_DOCUMENT_NODE:
5190 case XML_DOCUMENT_TYPE_NODE:
5191 case XML_NAMESPACE_DECL:
5192 case XML_XINCLUDE_START:
5193 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005194#ifdef LIBXML_DOCB_ENABLED
5195 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005196#endif
5197 break;
5198 case XML_ELEMENT_DECL:
5199 /* TODO !!! */
5200 break;
5201 case XML_ATTRIBUTE_DECL:
5202 /* TODO !!! */
5203 break;
5204 case XML_ENTITY_DECL:
5205 /* TODO !!! */
5206 break;
5207 }
5208}
Daniel Veillard652327a2003-09-29 18:02:38 +00005209#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005210
5211/**
5212 * xmlNodeAddContentLen:
5213 * @cur: the node being modified
5214 * @content: extra content
5215 * @len: the size of @content
5216 *
5217 * Append the extra substring to the node content.
5218 */
5219void
5220xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5221 if (cur == NULL) {
5222#ifdef DEBUG_TREE
5223 xmlGenericError(xmlGenericErrorContext,
5224 "xmlNodeAddContentLen : node == NULL\n");
5225#endif
5226 return;
5227 }
5228 if (len <= 0) return;
5229 switch (cur->type) {
5230 case XML_DOCUMENT_FRAG_NODE:
5231 case XML_ELEMENT_NODE: {
Daniel Veillardacb2bda2002-01-13 16:15:43 +00005232 xmlNodePtr last, newNode, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00005233
Daniel Veillard7db37732001-07-12 01:20:08 +00005234 last = cur->last;
Owen Taylor3473f882001-02-23 17:55:21 +00005235 newNode = xmlNewTextLen(content, len);
5236 if (newNode != NULL) {
Daniel Veillardacb2bda2002-01-13 16:15:43 +00005237 tmp = xmlAddChild(cur, newNode);
5238 if (tmp != newNode)
5239 return;
Owen Taylor3473f882001-02-23 17:55:21 +00005240 if ((last != NULL) && (last->next == newNode)) {
5241 xmlTextMerge(last, newNode);
5242 }
5243 }
5244 break;
5245 }
5246 case XML_ATTRIBUTE_NODE:
5247 break;
5248 case XML_TEXT_NODE:
5249 case XML_CDATA_SECTION_NODE:
5250 case XML_ENTITY_REF_NODE:
5251 case XML_ENTITY_NODE:
5252 case XML_PI_NODE:
5253 case XML_COMMENT_NODE:
5254 case XML_NOTATION_NODE:
5255 if (content != NULL) {
William M. Brack7762bb12004-01-04 14:49:01 +00005256 if ((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5257 xmlDictOwns(cur->doc->dict, cur->content)) {
5258 cur->content =
5259 xmlStrncatNew(cur->content, content, len);
5260 break;
5261 }
Owen Taylor3473f882001-02-23 17:55:21 +00005262 cur->content = xmlStrncat(cur->content, content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00005263 }
5264 case XML_DOCUMENT_NODE:
5265 case XML_DTD_NODE:
5266 case XML_HTML_DOCUMENT_NODE:
5267 case XML_DOCUMENT_TYPE_NODE:
5268 case XML_NAMESPACE_DECL:
5269 case XML_XINCLUDE_START:
5270 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005271#ifdef LIBXML_DOCB_ENABLED
5272 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005273#endif
5274 break;
5275 case XML_ELEMENT_DECL:
5276 case XML_ATTRIBUTE_DECL:
5277 case XML_ENTITY_DECL:
5278 break;
5279 }
5280}
5281
5282/**
5283 * xmlNodeAddContent:
5284 * @cur: the node being modified
5285 * @content: extra content
5286 *
5287 * Append the extra substring to the node content.
5288 */
5289void
5290xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) {
5291 int len;
5292
5293 if (cur == NULL) {
5294#ifdef DEBUG_TREE
5295 xmlGenericError(xmlGenericErrorContext,
5296 "xmlNodeAddContent : node == NULL\n");
5297#endif
5298 return;
5299 }
5300 if (content == NULL) return;
5301 len = xmlStrlen(content);
5302 xmlNodeAddContentLen(cur, content, len);
5303}
5304
5305/**
5306 * xmlTextMerge:
5307 * @first: the first text node
5308 * @second: the second text node being merged
5309 *
5310 * Merge two text nodes into one
5311 * Returns the first text node augmented
5312 */
5313xmlNodePtr
5314xmlTextMerge(xmlNodePtr first, xmlNodePtr second) {
5315 if (first == NULL) return(second);
5316 if (second == NULL) return(first);
5317 if (first->type != XML_TEXT_NODE) return(first);
5318 if (second->type != XML_TEXT_NODE) return(first);
5319 if (second->name != first->name)
5320 return(first);
Owen Taylor3473f882001-02-23 17:55:21 +00005321 xmlNodeAddContent(first, second->content);
Owen Taylor3473f882001-02-23 17:55:21 +00005322 xmlUnlinkNode(second);
5323 xmlFreeNode(second);
5324 return(first);
5325}
5326
Daniel Veillard2156d432004-03-04 15:59:36 +00005327#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XPATH_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00005328/**
5329 * xmlGetNsList:
5330 * @doc: the document
5331 * @node: the current node
5332 *
5333 * Search all the namespace applying to a given element.
Daniel Veillardd1640922001-12-17 15:30:10 +00005334 * Returns an NULL terminated array of all the #xmlNsPtr found
Owen Taylor3473f882001-02-23 17:55:21 +00005335 * that need to be freed by the caller or NULL if no
5336 * namespace if defined
5337 */
5338xmlNsPtr *
Daniel Veillard77044732001-06-29 21:31:07 +00005339xmlGetNsList(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node)
5340{
Owen Taylor3473f882001-02-23 17:55:21 +00005341 xmlNsPtr cur;
5342 xmlNsPtr *ret = NULL;
5343 int nbns = 0;
5344 int maxns = 10;
5345 int i;
5346
5347 while (node != NULL) {
Daniel Veillard77044732001-06-29 21:31:07 +00005348 if (node->type == XML_ELEMENT_NODE) {
5349 cur = node->nsDef;
5350 while (cur != NULL) {
5351 if (ret == NULL) {
5352 ret =
5353 (xmlNsPtr *) xmlMalloc((maxns + 1) *
5354 sizeof(xmlNsPtr));
5355 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005356 xmlTreeErrMemory("getting namespace list");
Daniel Veillard77044732001-06-29 21:31:07 +00005357 return (NULL);
5358 }
5359 ret[nbns] = NULL;
5360 }
5361 for (i = 0; i < nbns; i++) {
5362 if ((cur->prefix == ret[i]->prefix) ||
5363 (xmlStrEqual(cur->prefix, ret[i]->prefix)))
5364 break;
5365 }
5366 if (i >= nbns) {
5367 if (nbns >= maxns) {
5368 maxns *= 2;
5369 ret = (xmlNsPtr *) xmlRealloc(ret,
5370 (maxns +
5371 1) *
5372 sizeof(xmlNsPtr));
5373 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005374 xmlTreeErrMemory("getting namespace list");
Daniel Veillard77044732001-06-29 21:31:07 +00005375 return (NULL);
5376 }
5377 }
5378 ret[nbns++] = cur;
5379 ret[nbns] = NULL;
5380 }
Owen Taylor3473f882001-02-23 17:55:21 +00005381
Daniel Veillard77044732001-06-29 21:31:07 +00005382 cur = cur->next;
5383 }
5384 }
5385 node = node->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00005386 }
Daniel Veillard77044732001-06-29 21:31:07 +00005387 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00005388}
Daniel Veillard652327a2003-09-29 18:02:38 +00005389#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005390
5391/**
5392 * xmlSearchNs:
5393 * @doc: the document
5394 * @node: the current node
Daniel Veillard77851712001-02-27 21:54:07 +00005395 * @nameSpace: the namespace prefix
Owen Taylor3473f882001-02-23 17:55:21 +00005396 *
5397 * Search a Ns registered under a given name space for a document.
5398 * recurse on the parents until it finds the defined namespace
5399 * or return NULL otherwise.
5400 * @nameSpace can be NULL, this is a search for the default namespace.
5401 * We don't allow to cross entities boundaries. If you don't declare
5402 * the namespace within those you will be in troubles !!! A warning
5403 * is generated to cover this case.
5404 *
5405 * Returns the namespace pointer or NULL.
5406 */
5407xmlNsPtr
5408xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) {
Daniel Veillardf4e56292003-10-28 14:27:41 +00005409
Owen Taylor3473f882001-02-23 17:55:21 +00005410 xmlNsPtr cur;
Daniel Veillardf4e56292003-10-28 14:27:41 +00005411 xmlNodePtr orig = node;
Owen Taylor3473f882001-02-23 17:55:21 +00005412
5413 if (node == NULL) return(NULL);
5414 if ((nameSpace != NULL) &&
5415 (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) {
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00005416 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
5417 /*
5418 * The XML-1.0 namespace is normally held on the root
5419 * element. In this case exceptionally create it on the
5420 * node element.
5421 */
5422 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5423 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005424 xmlTreeErrMemory("searching namespace");
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00005425 return(NULL);
5426 }
5427 memset(cur, 0, sizeof(xmlNs));
5428 cur->type = XML_LOCAL_NAMESPACE;
5429 cur->href = xmlStrdup(XML_XML_NAMESPACE);
5430 cur->prefix = xmlStrdup((const xmlChar *)"xml");
5431 cur->next = node->nsDef;
5432 node->nsDef = cur;
5433 return(cur);
5434 }
Owen Taylor3473f882001-02-23 17:55:21 +00005435 if (doc->oldNs == NULL) {
5436 /*
5437 * Allocate a new Namespace and fill the fields.
5438 */
5439 doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5440 if (doc->oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005441 xmlTreeErrMemory("searching namespace");
Owen Taylor3473f882001-02-23 17:55:21 +00005442 return(NULL);
5443 }
5444 memset(doc->oldNs, 0, sizeof(xmlNs));
5445 doc->oldNs->type = XML_LOCAL_NAMESPACE;
5446
5447 doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE);
5448 doc->oldNs->prefix = xmlStrdup((const xmlChar *)"xml");
5449 }
5450 return(doc->oldNs);
5451 }
5452 while (node != NULL) {
5453 if ((node->type == XML_ENTITY_REF_NODE) ||
5454 (node->type == XML_ENTITY_NODE) ||
5455 (node->type == XML_ENTITY_DECL))
5456 return(NULL);
5457 if (node->type == XML_ELEMENT_NODE) {
5458 cur = node->nsDef;
5459 while (cur != NULL) {
5460 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
5461 (cur->href != NULL))
5462 return(cur);
5463 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
5464 (cur->href != NULL) &&
5465 (xmlStrEqual(cur->prefix, nameSpace)))
5466 return(cur);
5467 cur = cur->next;
5468 }
Daniel Veillardf4e56292003-10-28 14:27:41 +00005469 if (orig != node) {
5470 cur = node->ns;
5471 if (cur != NULL) {
5472 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
5473 (cur->href != NULL))
5474 return(cur);
5475 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
5476 (cur->href != NULL) &&
5477 (xmlStrEqual(cur->prefix, nameSpace)))
5478 return(cur);
5479 }
5480 }
Owen Taylor3473f882001-02-23 17:55:21 +00005481 }
5482 node = node->parent;
5483 }
5484 return(NULL);
5485}
5486
5487/**
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005488 * xmlNsInScope:
5489 * @doc: the document
5490 * @node: the current node
5491 * @ancestor: the ancestor carrying the namespace
5492 * @prefix: the namespace prefix
5493 *
5494 * Verify that the given namespace held on @ancestor is still in scope
5495 * on node.
5496 *
5497 * Returns 1 if true, 0 if false and -1 in case of error.
5498 */
5499static int
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005500xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node,
5501 xmlNodePtr ancestor, const xmlChar * prefix)
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005502{
5503 xmlNsPtr tst;
5504
5505 while ((node != NULL) && (node != ancestor)) {
5506 if ((node->type == XML_ENTITY_REF_NODE) ||
5507 (node->type == XML_ENTITY_NODE) ||
5508 (node->type == XML_ENTITY_DECL))
5509 return (-1);
5510 if (node->type == XML_ELEMENT_NODE) {
5511 tst = node->nsDef;
5512 while (tst != NULL) {
5513 if ((tst->prefix == NULL)
5514 && (prefix == NULL))
5515 return (0);
5516 if ((tst->prefix != NULL)
5517 && (prefix != NULL)
5518 && (xmlStrEqual(tst->prefix, prefix)))
5519 return (0);
5520 tst = tst->next;
5521 }
5522 }
5523 node = node->parent;
5524 }
5525 if (node != ancestor)
5526 return (-1);
5527 return (1);
5528}
5529
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005530/**
Owen Taylor3473f882001-02-23 17:55:21 +00005531 * xmlSearchNsByHref:
5532 * @doc: the document
5533 * @node: the current node
5534 * @href: the namespace value
5535 *
5536 * Search a Ns aliasing a given URI. Recurse on the parents until it finds
5537 * the defined namespace or return NULL otherwise.
5538 * Returns the namespace pointer or NULL.
5539 */
5540xmlNsPtr
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005541xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href)
5542{
Owen Taylor3473f882001-02-23 17:55:21 +00005543 xmlNsPtr cur;
5544 xmlNodePtr orig = node;
Daniel Veillard62040be2004-05-17 03:17:26 +00005545 int is_attr;
Owen Taylor3473f882001-02-23 17:55:21 +00005546
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005547 if ((node == NULL) || (href == NULL))
5548 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005549 if (xmlStrEqual(href, XML_XML_NAMESPACE)) {
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005550 /*
5551 * Only the document can hold the XML spec namespace.
5552 */
5553 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
5554 /*
5555 * The XML-1.0 namespace is normally held on the root
5556 * element. In this case exceptionally create it on the
5557 * node element.
5558 */
5559 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5560 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005561 xmlTreeErrMemory("searching namespace");
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005562 return (NULL);
5563 }
5564 memset(cur, 0, sizeof(xmlNs));
5565 cur->type = XML_LOCAL_NAMESPACE;
5566 cur->href = xmlStrdup(XML_XML_NAMESPACE);
5567 cur->prefix = xmlStrdup((const xmlChar *) "xml");
5568 cur->next = node->nsDef;
5569 node->nsDef = cur;
5570 return (cur);
5571 }
5572 if (doc->oldNs == NULL) {
5573 /*
5574 * Allocate a new Namespace and fill the fields.
5575 */
5576 doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5577 if (doc->oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005578 xmlTreeErrMemory("searching namespace");
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005579 return (NULL);
5580 }
5581 memset(doc->oldNs, 0, sizeof(xmlNs));
5582 doc->oldNs->type = XML_LOCAL_NAMESPACE;
Owen Taylor3473f882001-02-23 17:55:21 +00005583
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005584 doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE);
5585 doc->oldNs->prefix = xmlStrdup((const xmlChar *) "xml");
5586 }
5587 return (doc->oldNs);
Owen Taylor3473f882001-02-23 17:55:21 +00005588 }
Daniel Veillard62040be2004-05-17 03:17:26 +00005589 is_attr = (node->type == XML_ATTRIBUTE_NODE);
Owen Taylor3473f882001-02-23 17:55:21 +00005590 while (node != NULL) {
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005591 if ((node->type == XML_ENTITY_REF_NODE) ||
5592 (node->type == XML_ENTITY_NODE) ||
5593 (node->type == XML_ENTITY_DECL))
5594 return (NULL);
5595 if (node->type == XML_ELEMENT_NODE) {
5596 cur = node->nsDef;
5597 while (cur != NULL) {
5598 if ((cur->href != NULL) && (href != NULL) &&
5599 (xmlStrEqual(cur->href, href))) {
Daniel Veillard62040be2004-05-17 03:17:26 +00005600 if (((!is_attr) || (cur->prefix != NULL)) &&
Kasimier T. Buchcikba70cc02005-02-28 10:28:21 +00005601 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005602 return (cur);
5603 }
5604 cur = cur->next;
5605 }
Daniel Veillardf4e56292003-10-28 14:27:41 +00005606 if (orig != node) {
5607 cur = node->ns;
5608 if (cur != NULL) {
5609 if ((cur->href != NULL) && (href != NULL) &&
5610 (xmlStrEqual(cur->href, href))) {
Daniel Veillard62040be2004-05-17 03:17:26 +00005611 if (((!is_attr) || (cur->prefix != NULL)) &&
Kasimier T. Buchcikba70cc02005-02-28 10:28:21 +00005612 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
Daniel Veillardf4e56292003-10-28 14:27:41 +00005613 return (cur);
5614 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005615 }
Daniel Veillardf4e56292003-10-28 14:27:41 +00005616 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005617 }
5618 node = node->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00005619 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005620 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005621}
5622
5623/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005624 * xmlNewReconciliedNs:
Owen Taylor3473f882001-02-23 17:55:21 +00005625 * @doc: the document
5626 * @tree: a node expected to hold the new namespace
5627 * @ns: the original namespace
5628 *
5629 * This function tries to locate a namespace definition in a tree
5630 * ancestors, or create a new namespace definition node similar to
5631 * @ns trying to reuse the same prefix. However if the given prefix is
5632 * null (default namespace) or reused within the subtree defined by
5633 * @tree or on one of its ancestors then a new prefix is generated.
5634 * Returns the (new) namespace definition or NULL in case of error
5635 */
5636xmlNsPtr
5637xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) {
5638 xmlNsPtr def;
5639 xmlChar prefix[50];
5640 int counter = 1;
5641
5642 if (tree == NULL) {
5643#ifdef DEBUG_TREE
5644 xmlGenericError(xmlGenericErrorContext,
5645 "xmlNewReconciliedNs : tree == NULL\n");
5646#endif
5647 return(NULL);
5648 }
Daniel Veillardce244ad2004-11-05 10:03:46 +00005649 if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005650#ifdef DEBUG_TREE
5651 xmlGenericError(xmlGenericErrorContext,
5652 "xmlNewReconciliedNs : ns == NULL\n");
5653#endif
5654 return(NULL);
5655 }
5656 /*
5657 * Search an existing namespace definition inherited.
5658 */
5659 def = xmlSearchNsByHref(doc, tree, ns->href);
5660 if (def != NULL)
5661 return(def);
5662
5663 /*
5664 * Find a close prefix which is not already in use.
5665 * Let's strip namespace prefixes longer than 20 chars !
5666 */
Daniel Veillardf742d342002-03-07 00:05:35 +00005667 if (ns->prefix == NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00005668 snprintf((char *) prefix, sizeof(prefix), "default");
Daniel Veillardf742d342002-03-07 00:05:35 +00005669 else
William M. Brack13dfa872004-09-18 04:52:08 +00005670 snprintf((char *) prefix, sizeof(prefix), "%.20s", (char *)ns->prefix);
Daniel Veillardf742d342002-03-07 00:05:35 +00005671
Owen Taylor3473f882001-02-23 17:55:21 +00005672 def = xmlSearchNs(doc, tree, prefix);
5673 while (def != NULL) {
5674 if (counter > 1000) return(NULL);
Daniel Veillardf742d342002-03-07 00:05:35 +00005675 if (ns->prefix == NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00005676 snprintf((char *) prefix, sizeof(prefix), "default%d", counter++);
Daniel Veillardf742d342002-03-07 00:05:35 +00005677 else
William M. Brack13dfa872004-09-18 04:52:08 +00005678 snprintf((char *) prefix, sizeof(prefix), "%.20s%d",
5679 (char *)ns->prefix, counter++);
Owen Taylor3473f882001-02-23 17:55:21 +00005680 def = xmlSearchNs(doc, tree, prefix);
5681 }
5682
5683 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00005684 * OK, now we are ready to create a new one.
Owen Taylor3473f882001-02-23 17:55:21 +00005685 */
5686 def = xmlNewNs(tree, ns->href, prefix);
5687 return(def);
5688}
5689
Daniel Veillard652327a2003-09-29 18:02:38 +00005690#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00005691/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005692 * xmlReconciliateNs:
Owen Taylor3473f882001-02-23 17:55:21 +00005693 * @doc: the document
5694 * @tree: a node defining the subtree to reconciliate
5695 *
5696 * This function checks that all the namespaces declared within the given
5697 * tree are properly declared. This is needed for example after Copy or Cut
5698 * and then paste operations. The subtree may still hold pointers to
5699 * namespace declarations outside the subtree or invalid/masked. As much
Daniel Veillardd1640922001-12-17 15:30:10 +00005700 * as possible the function try to reuse the existing namespaces found in
Owen Taylor3473f882001-02-23 17:55:21 +00005701 * the new environment. If not possible the new namespaces are redeclared
5702 * on @tree at the top of the given subtree.
5703 * Returns the number of namespace declarations created or -1 in case of error.
5704 */
5705int
5706xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) {
5707 xmlNsPtr *oldNs = NULL;
5708 xmlNsPtr *newNs = NULL;
5709 int sizeCache = 0;
5710 int nbCache = 0;
5711
5712 xmlNsPtr n;
5713 xmlNodePtr node = tree;
5714 xmlAttrPtr attr;
5715 int ret = 0, i;
5716
Daniel Veillardce244ad2004-11-05 10:03:46 +00005717 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) return(-1);
5718 if ((doc == NULL) || (doc->type != XML_DOCUMENT_NODE)) return(-1);
5719 if (node->doc != doc) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00005720 while (node != NULL) {
5721 /*
5722 * Reconciliate the node namespace
5723 */
5724 if (node->ns != NULL) {
5725 /*
5726 * initialize the cache if needed
5727 */
5728 if (sizeCache == 0) {
5729 sizeCache = 10;
5730 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
5731 sizeof(xmlNsPtr));
5732 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005733 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005734 return(-1);
5735 }
5736 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
5737 sizeof(xmlNsPtr));
5738 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005739 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005740 xmlFree(oldNs);
5741 return(-1);
5742 }
5743 }
5744 for (i = 0;i < nbCache;i++) {
5745 if (oldNs[i] == node->ns) {
5746 node->ns = newNs[i];
5747 break;
5748 }
5749 }
5750 if (i == nbCache) {
5751 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00005752 * OK we need to recreate a new namespace definition
Owen Taylor3473f882001-02-23 17:55:21 +00005753 */
5754 n = xmlNewReconciliedNs(doc, tree, node->ns);
5755 if (n != NULL) { /* :-( what if else ??? */
5756 /*
5757 * check if we need to grow the cache buffers.
5758 */
5759 if (sizeCache <= nbCache) {
5760 sizeCache *= 2;
5761 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
5762 sizeof(xmlNsPtr));
5763 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005764 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005765 xmlFree(newNs);
5766 return(-1);
5767 }
5768 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
5769 sizeof(xmlNsPtr));
5770 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005771 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005772 xmlFree(oldNs);
5773 return(-1);
5774 }
5775 }
5776 newNs[nbCache] = n;
5777 oldNs[nbCache++] = node->ns;
5778 node->ns = n;
5779 }
5780 }
5781 }
5782 /*
5783 * now check for namespace hold by attributes on the node.
5784 */
5785 attr = node->properties;
5786 while (attr != NULL) {
5787 if (attr->ns != NULL) {
5788 /*
5789 * initialize the cache if needed
5790 */
5791 if (sizeCache == 0) {
5792 sizeCache = 10;
5793 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
5794 sizeof(xmlNsPtr));
5795 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005796 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005797 return(-1);
5798 }
5799 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
5800 sizeof(xmlNsPtr));
5801 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005802 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005803 xmlFree(oldNs);
5804 return(-1);
5805 }
5806 }
5807 for (i = 0;i < nbCache;i++) {
5808 if (oldNs[i] == attr->ns) {
Daniel Veillardce66ce12002-10-28 19:01:59 +00005809 attr->ns = newNs[i];
Owen Taylor3473f882001-02-23 17:55:21 +00005810 break;
5811 }
5812 }
5813 if (i == nbCache) {
5814 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00005815 * OK we need to recreate a new namespace definition
Owen Taylor3473f882001-02-23 17:55:21 +00005816 */
5817 n = xmlNewReconciliedNs(doc, tree, attr->ns);
5818 if (n != NULL) { /* :-( what if else ??? */
5819 /*
5820 * check if we need to grow the cache buffers.
5821 */
5822 if (sizeCache <= nbCache) {
5823 sizeCache *= 2;
5824 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
5825 sizeof(xmlNsPtr));
5826 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005827 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005828 xmlFree(newNs);
5829 return(-1);
5830 }
5831 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
5832 sizeof(xmlNsPtr));
5833 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005834 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005835 xmlFree(oldNs);
5836 return(-1);
5837 }
5838 }
5839 newNs[nbCache] = n;
5840 oldNs[nbCache++] = attr->ns;
5841 attr->ns = n;
5842 }
5843 }
5844 }
5845 attr = attr->next;
5846 }
5847
5848 /*
5849 * Browse the full subtree, deep first
5850 */
Daniel Veillardac996a12004-07-30 12:02:58 +00005851 if (node->children != NULL && node->type != XML_ENTITY_REF_NODE) {
Owen Taylor3473f882001-02-23 17:55:21 +00005852 /* deep first */
5853 node = node->children;
5854 } else if ((node != tree) && (node->next != NULL)) {
5855 /* then siblings */
5856 node = node->next;
5857 } else if (node != tree) {
5858 /* go up to parents->next if needed */
5859 while (node != tree) {
5860 if (node->parent != NULL)
5861 node = node->parent;
5862 if ((node != tree) && (node->next != NULL)) {
5863 node = node->next;
5864 break;
5865 }
5866 if (node->parent == NULL) {
5867 node = NULL;
5868 break;
5869 }
5870 }
5871 /* exit condition */
5872 if (node == tree)
5873 node = NULL;
Daniel Veillard1e774382002-03-06 17:35:40 +00005874 } else
5875 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005876 }
Daniel Veillardf742d342002-03-07 00:05:35 +00005877 if (oldNs != NULL)
5878 xmlFree(oldNs);
5879 if (newNs != NULL)
5880 xmlFree(newNs);
Owen Taylor3473f882001-02-23 17:55:21 +00005881 return(ret);
5882}
Daniel Veillard652327a2003-09-29 18:02:38 +00005883#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005884
5885/**
5886 * xmlHasProp:
5887 * @node: the node
5888 * @name: the attribute name
5889 *
5890 * Search an attribute associated to a node
5891 * This function also looks in DTD attribute declaration for #FIXED or
5892 * default declaration values unless DTD use has been turned off.
5893 *
5894 * Returns the attribute or the attribute declaration or NULL if
5895 * neither was found.
5896 */
5897xmlAttrPtr
5898xmlHasProp(xmlNodePtr node, const xmlChar *name) {
5899 xmlAttrPtr prop;
5900 xmlDocPtr doc;
5901
5902 if ((node == NULL) || (name == NULL)) return(NULL);
5903 /*
5904 * Check on the properties attached to the node
5905 */
5906 prop = node->properties;
5907 while (prop != NULL) {
5908 if (xmlStrEqual(prop->name, name)) {
5909 return(prop);
5910 }
5911 prop = prop->next;
5912 }
5913 if (!xmlCheckDTD) return(NULL);
5914
5915 /*
5916 * Check if there is a default declaration in the internal
5917 * or external subsets
5918 */
5919 doc = node->doc;
5920 if (doc != NULL) {
5921 xmlAttributePtr attrDecl;
5922 if (doc->intSubset != NULL) {
5923 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
5924 if ((attrDecl == NULL) && (doc->extSubset != NULL))
5925 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
Daniel Veillard75eb1ad2003-07-07 14:42:44 +00005926 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
5927 /* return attribute declaration only if a default value is given
5928 (that includes #FIXED declarations) */
Owen Taylor3473f882001-02-23 17:55:21 +00005929 return((xmlAttrPtr) attrDecl);
5930 }
5931 }
5932 return(NULL);
5933}
5934
5935/**
Daniel Veillarde95e2392001-06-06 10:46:28 +00005936 * xmlHasNsProp:
5937 * @node: the node
5938 * @name: the attribute name
Daniel Veillardca2366a2001-06-11 12:09:01 +00005939 * @nameSpace: the URI of the namespace
Daniel Veillarde95e2392001-06-06 10:46:28 +00005940 *
5941 * Search for an attribute associated to a node
5942 * This attribute has to be anchored in the namespace specified.
5943 * This does the entity substitution.
5944 * This function looks in DTD attribute declaration for #FIXED or
5945 * default declaration values unless DTD use has been turned off.
William M. Brack2c228442004-10-03 04:10:00 +00005946 * Note that a namespace of NULL indicates to use the default namespace.
Daniel Veillarde95e2392001-06-06 10:46:28 +00005947 *
5948 * Returns the attribute or the attribute declaration or NULL
5949 * if neither was found.
5950 */
5951xmlAttrPtr
Daniel Veillardca2366a2001-06-11 12:09:01 +00005952xmlHasNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
Daniel Veillarde95e2392001-06-06 10:46:28 +00005953 xmlAttrPtr prop;
Daniel Veillard652327a2003-09-29 18:02:38 +00005954#ifdef LIBXML_TREE_ENABLED
Daniel Veillarde95e2392001-06-06 10:46:28 +00005955 xmlDocPtr doc;
Daniel Veillard652327a2003-09-29 18:02:38 +00005956#endif /* LIBXML_TREE_ENABLED */
Daniel Veillarde95e2392001-06-06 10:46:28 +00005957
5958 if (node == NULL)
5959 return(NULL);
5960
5961 prop = node->properties;
Daniel Veillarde95e2392001-06-06 10:46:28 +00005962 while (prop != NULL) {
5963 /*
5964 * One need to have
5965 * - same attribute names
5966 * - and the attribute carrying that namespace
Daniel Veillarde95e2392001-06-06 10:46:28 +00005967 */
William M. Brack2c228442004-10-03 04:10:00 +00005968 if (xmlStrEqual(prop->name, name)) {
5969 if (((prop->ns != NULL) &&
5970 (xmlStrEqual(prop->ns->href, nameSpace))) ||
5971 ((prop->ns == NULL) && (nameSpace == NULL))) {
5972 return(prop);
5973 }
Daniel Veillarde95e2392001-06-06 10:46:28 +00005974 }
5975 prop = prop->next;
5976 }
5977 if (!xmlCheckDTD) return(NULL);
5978
Daniel Veillard652327a2003-09-29 18:02:38 +00005979#ifdef LIBXML_TREE_ENABLED
Daniel Veillarde95e2392001-06-06 10:46:28 +00005980 /*
5981 * Check if there is a default declaration in the internal
5982 * or external subsets
5983 */
5984 doc = node->doc;
5985 if (doc != NULL) {
5986 if (doc->intSubset != NULL) {
Daniel Veillardef6c46f2002-03-07 22:21:56 +00005987 xmlAttributePtr attrDecl = NULL;
5988 xmlNsPtr *nsList, *cur;
5989 xmlChar *ename;
Daniel Veillarde95e2392001-06-06 10:46:28 +00005990
Daniel Veillardef6c46f2002-03-07 22:21:56 +00005991 nsList = xmlGetNsList(node->doc, node);
5992 if (nsList == NULL)
5993 return(NULL);
5994 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
5995 ename = xmlStrdup(node->ns->prefix);
5996 ename = xmlStrcat(ename, BAD_CAST ":");
5997 ename = xmlStrcat(ename, node->name);
5998 } else {
5999 ename = xmlStrdup(node->name);
Daniel Veillarde95e2392001-06-06 10:46:28 +00006000 }
Daniel Veillardef6c46f2002-03-07 22:21:56 +00006001 if (ename == NULL) {
6002 xmlFree(nsList);
6003 return(NULL);
6004 }
6005
William M. Brack2c228442004-10-03 04:10:00 +00006006 if (nameSpace == NULL) {
6007 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, ename,
6008 name, NULL);
6009 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
6010 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, ename,
6011 name, NULL);
Daniel Veillardef6c46f2002-03-07 22:21:56 +00006012 }
William M. Brack2c228442004-10-03 04:10:00 +00006013 } else {
6014 cur = nsList;
6015 while (*cur != NULL) {
6016 if (xmlStrEqual((*cur)->href, nameSpace)) {
6017 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, ename,
6018 name, (*cur)->prefix);
6019 if ((attrDecl == NULL) && (doc->extSubset != NULL))
6020 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, ename,
6021 name, (*cur)->prefix);
6022 }
6023 cur++;
6024 }
Daniel Veillardef6c46f2002-03-07 22:21:56 +00006025 }
6026 xmlFree(nsList);
6027 xmlFree(ename);
6028 return((xmlAttrPtr) attrDecl);
Daniel Veillarde95e2392001-06-06 10:46:28 +00006029 }
6030 }
Daniel Veillard652327a2003-09-29 18:02:38 +00006031#endif /* LIBXML_TREE_ENABLED */
Daniel Veillarde95e2392001-06-06 10:46:28 +00006032 return(NULL);
6033}
6034
6035/**
Owen Taylor3473f882001-02-23 17:55:21 +00006036 * xmlGetProp:
6037 * @node: the node
6038 * @name: the attribute name
6039 *
6040 * Search and get the value of an attribute associated to a node
6041 * This does the entity substitution.
6042 * This function looks in DTD attribute declaration for #FIXED or
6043 * default declaration values unless DTD use has been turned off.
Daniel Veillard784b9352003-02-16 15:50:27 +00006044 * NOTE: this function acts independently of namespaces associated
Daniel Veillard71531f32003-02-05 13:19:53 +00006045 * to the attribute. Use xmlGetNsProp() or xmlGetNoNsProp()
6046 * for namespace aware processing.
Owen Taylor3473f882001-02-23 17:55:21 +00006047 *
6048 * Returns the attribute value or NULL if not found.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00006049 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00006050 */
6051xmlChar *
6052xmlGetProp(xmlNodePtr node, const xmlChar *name) {
6053 xmlAttrPtr prop;
6054 xmlDocPtr doc;
6055
6056 if ((node == NULL) || (name == NULL)) return(NULL);
6057 /*
6058 * Check on the properties attached to the node
6059 */
6060 prop = node->properties;
6061 while (prop != NULL) {
6062 if (xmlStrEqual(prop->name, name)) {
6063 xmlChar *ret;
6064
6065 ret = xmlNodeListGetString(node->doc, prop->children, 1);
6066 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
6067 return(ret);
6068 }
6069 prop = prop->next;
6070 }
6071 if (!xmlCheckDTD) return(NULL);
6072
6073 /*
6074 * Check if there is a default declaration in the internal
6075 * or external subsets
6076 */
6077 doc = node->doc;
6078 if (doc != NULL) {
6079 xmlAttributePtr attrDecl;
6080 if (doc->intSubset != NULL) {
6081 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
6082 if ((attrDecl == NULL) && (doc->extSubset != NULL))
6083 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
Daniel Veillard75eb1ad2003-07-07 14:42:44 +00006084 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6085 /* return attribute declaration only if a default value is given
6086 (that includes #FIXED declarations) */
Owen Taylor3473f882001-02-23 17:55:21 +00006087 return(xmlStrdup(attrDecl->defaultValue));
6088 }
6089 }
6090 return(NULL);
6091}
6092
6093/**
Daniel Veillard71531f32003-02-05 13:19:53 +00006094 * xmlGetNoNsProp:
6095 * @node: the node
6096 * @name: the attribute name
6097 *
6098 * Search and get the value of an attribute associated to a node
6099 * This does the entity substitution.
6100 * This function looks in DTD attribute declaration for #FIXED or
6101 * default declaration values unless DTD use has been turned off.
6102 * This function is similar to xmlGetProp except it will accept only
6103 * an attribute in no namespace.
6104 *
6105 * Returns the attribute value or NULL if not found.
6106 * It's up to the caller to free the memory with xmlFree().
6107 */
6108xmlChar *
6109xmlGetNoNsProp(xmlNodePtr node, const xmlChar *name) {
6110 xmlAttrPtr prop;
6111 xmlDocPtr doc;
6112
6113 if ((node == NULL) || (name == NULL)) return(NULL);
6114 /*
6115 * Check on the properties attached to the node
6116 */
6117 prop = node->properties;
6118 while (prop != NULL) {
6119 if ((prop->ns == NULL) && (xmlStrEqual(prop->name, name))) {
6120 xmlChar *ret;
6121
6122 ret = xmlNodeListGetString(node->doc, prop->children, 1);
6123 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
6124 return(ret);
6125 }
6126 prop = prop->next;
6127 }
6128 if (!xmlCheckDTD) return(NULL);
6129
6130 /*
6131 * Check if there is a default declaration in the internal
6132 * or external subsets
6133 */
6134 doc = node->doc;
6135 if (doc != NULL) {
6136 xmlAttributePtr attrDecl;
6137 if (doc->intSubset != NULL) {
6138 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
6139 if ((attrDecl == NULL) && (doc->extSubset != NULL))
6140 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
Daniel Veillard75eb1ad2003-07-07 14:42:44 +00006141 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6142 /* return attribute declaration only if a default value is given
6143 (that includes #FIXED declarations) */
Daniel Veillard71531f32003-02-05 13:19:53 +00006144 return(xmlStrdup(attrDecl->defaultValue));
6145 }
6146 }
6147 return(NULL);
6148}
6149
6150/**
Owen Taylor3473f882001-02-23 17:55:21 +00006151 * xmlGetNsProp:
6152 * @node: the node
6153 * @name: the attribute name
Daniel Veillardca2366a2001-06-11 12:09:01 +00006154 * @nameSpace: the URI of the namespace
Owen Taylor3473f882001-02-23 17:55:21 +00006155 *
6156 * Search and get the value of an attribute associated to a node
6157 * This attribute has to be anchored in the namespace specified.
6158 * This does the entity substitution.
6159 * This function looks in DTD attribute declaration for #FIXED or
6160 * default declaration values unless DTD use has been turned off.
6161 *
6162 * Returns the attribute value or NULL if not found.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00006163 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00006164 */
6165xmlChar *
Daniel Veillardca2366a2001-06-11 12:09:01 +00006166xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
Owen Taylor3473f882001-02-23 17:55:21 +00006167 xmlAttrPtr prop;
6168 xmlDocPtr doc;
6169 xmlNsPtr ns;
6170
6171 if (node == NULL)
6172 return(NULL);
6173
6174 prop = node->properties;
Daniel Veillardca2366a2001-06-11 12:09:01 +00006175 if (nameSpace == NULL)
Daniel Veillard71531f32003-02-05 13:19:53 +00006176 return(xmlGetNoNsProp(node, name));
Owen Taylor3473f882001-02-23 17:55:21 +00006177 while (prop != NULL) {
6178 /*
6179 * One need to have
6180 * - same attribute names
6181 * - and the attribute carrying that namespace
Owen Taylor3473f882001-02-23 17:55:21 +00006182 */
6183 if ((xmlStrEqual(prop->name, name)) &&
Daniel Veillarde8fc08e2001-06-07 19:35:47 +00006184 ((prop->ns != NULL) &&
Daniel Veillardca2366a2001-06-11 12:09:01 +00006185 (xmlStrEqual(prop->ns->href, nameSpace)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00006186 xmlChar *ret;
6187
6188 ret = xmlNodeListGetString(node->doc, prop->children, 1);
6189 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
6190 return(ret);
6191 }
6192 prop = prop->next;
6193 }
6194 if (!xmlCheckDTD) return(NULL);
6195
6196 /*
6197 * Check if there is a default declaration in the internal
6198 * or external subsets
6199 */
6200 doc = node->doc;
6201 if (doc != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00006202 if (doc->intSubset != NULL) {
Daniel Veillard5792e162001-04-30 17:44:45 +00006203 xmlAttributePtr attrDecl;
6204
Owen Taylor3473f882001-02-23 17:55:21 +00006205 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
6206 if ((attrDecl == NULL) && (doc->extSubset != NULL))
6207 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
6208
6209 if ((attrDecl != NULL) && (attrDecl->prefix != NULL)) {
6210 /*
6211 * The DTD declaration only allows a prefix search
6212 */
6213 ns = xmlSearchNs(doc, node, attrDecl->prefix);
Daniel Veillardca2366a2001-06-11 12:09:01 +00006214 if ((ns != NULL) && (xmlStrEqual(ns->href, nameSpace)))
Owen Taylor3473f882001-02-23 17:55:21 +00006215 return(xmlStrdup(attrDecl->defaultValue));
6216 }
6217 }
6218 }
6219 return(NULL);
6220}
6221
Daniel Veillard2156d432004-03-04 15:59:36 +00006222#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
6223/**
6224 * xmlUnsetProp:
6225 * @node: the node
6226 * @name: the attribute name
6227 *
6228 * Remove an attribute carried by a node.
6229 * Returns 0 if successful, -1 if not found
6230 */
6231int
6232xmlUnsetProp(xmlNodePtr node, const xmlChar *name) {
6233 xmlAttrPtr prop, prev = NULL;;
6234
6235 if ((node == NULL) || (name == NULL))
6236 return(-1);
6237 prop = node->properties;
6238 while (prop != NULL) {
6239 if ((xmlStrEqual(prop->name, name)) &&
6240 (prop->ns == NULL)) {
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00006241 xmlUnlinkNode((xmlNodePtr) prop);
Daniel Veillard2156d432004-03-04 15:59:36 +00006242 xmlFreeProp(prop);
6243 return(0);
6244 }
6245 prev = prop;
6246 prop = prop->next;
6247 }
6248 return(-1);
6249}
6250
6251/**
6252 * xmlUnsetNsProp:
6253 * @node: the node
6254 * @ns: the namespace definition
6255 * @name: the attribute name
6256 *
6257 * Remove an attribute carried by a node.
6258 * Returns 0 if successful, -1 if not found
6259 */
6260int
6261xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) {
Daniel Veillard27f20102004-11-05 11:50:11 +00006262 xmlAttrPtr prop, prev = NULL;;
Daniel Veillard2156d432004-03-04 15:59:36 +00006263
6264 if ((node == NULL) || (name == NULL))
6265 return(-1);
Daniel Veillard27f20102004-11-05 11:50:11 +00006266 prop = node->properties;
Daniel Veillard2156d432004-03-04 15:59:36 +00006267 if (ns == NULL)
6268 return(xmlUnsetProp(node, name));
6269 if (ns->href == NULL)
6270 return(-1);
6271 while (prop != NULL) {
6272 if ((xmlStrEqual(prop->name, name)) &&
6273 (prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))) {
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00006274 xmlUnlinkNode((xmlNodePtr) prop);
Daniel Veillard2156d432004-03-04 15:59:36 +00006275 xmlFreeProp(prop);
6276 return(0);
6277 }
6278 prev = prop;
6279 prop = prop->next;
6280 }
6281 return(-1);
6282}
6283#endif
6284
6285#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_HTML_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00006286/**
6287 * xmlSetProp:
6288 * @node: the node
6289 * @name: the attribute name
6290 * @value: the attribute value
6291 *
6292 * Set (or reset) an attribute carried by a node.
6293 * Returns the attribute pointer.
6294 */
6295xmlAttrPtr
6296xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
Daniel Veillard4855c8c2001-11-25 10:35:25 +00006297 xmlAttrPtr prop;
6298 xmlDocPtr doc;
Owen Taylor3473f882001-02-23 17:55:21 +00006299
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006300 if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE))
Owen Taylor3473f882001-02-23 17:55:21 +00006301 return(NULL);
6302 doc = node->doc;
Daniel Veillard4855c8c2001-11-25 10:35:25 +00006303 prop = node->properties;
Owen Taylor3473f882001-02-23 17:55:21 +00006304 while (prop != NULL) {
Daniel Veillard75bea542001-05-11 17:41:21 +00006305 if ((xmlStrEqual(prop->name, name)) &&
6306 (prop->ns == NULL)){
Daniel Veillard4855c8c2001-11-25 10:35:25 +00006307 xmlNodePtr oldprop = prop->children;
6308
Owen Taylor3473f882001-02-23 17:55:21 +00006309 prop->children = NULL;
6310 prop->last = NULL;
6311 if (value != NULL) {
6312 xmlChar *buffer;
6313 xmlNodePtr tmp;
6314
6315 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
6316 prop->children = xmlStringGetNodeList(node->doc, buffer);
6317 prop->last = NULL;
6318 prop->doc = doc;
6319 tmp = prop->children;
6320 while (tmp != NULL) {
6321 tmp->parent = (xmlNodePtr) prop;
6322 tmp->doc = doc;
6323 if (tmp->next == NULL)
6324 prop->last = tmp;
6325 tmp = tmp->next;
6326 }
6327 xmlFree(buffer);
Daniel Veillard75bea542001-05-11 17:41:21 +00006328 }
Daniel Veillard4855c8c2001-11-25 10:35:25 +00006329 if (oldprop != NULL)
6330 xmlFreeNodeList(oldprop);
Owen Taylor3473f882001-02-23 17:55:21 +00006331 return(prop);
6332 }
6333 prop = prop->next;
6334 }
6335 prop = xmlNewProp(node, name, value);
6336 return(prop);
6337}
6338
6339/**
6340 * xmlSetNsProp:
6341 * @node: the node
6342 * @ns: the namespace definition
6343 * @name: the attribute name
6344 * @value: the attribute value
6345 *
6346 * Set (or reset) an attribute carried by a node.
6347 * The ns structure must be in scope, this is not checked.
6348 *
6349 * Returns the attribute pointer.
6350 */
6351xmlAttrPtr
6352xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
6353 const xmlChar *value) {
6354 xmlAttrPtr prop;
6355
Daniel Veillardb6b36d32005-02-09 16:48:53 +00006356 if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE))
Owen Taylor3473f882001-02-23 17:55:21 +00006357 return(NULL);
6358
6359 if (ns == NULL)
6360 return(xmlSetProp(node, name, value));
6361 if (ns->href == NULL)
6362 return(NULL);
6363 prop = node->properties;
6364
6365 while (prop != NULL) {
6366 /*
6367 * One need to have
6368 * - same attribute names
6369 * - and the attribute carrying that namespace
Owen Taylor3473f882001-02-23 17:55:21 +00006370 */
6371 if ((xmlStrEqual(prop->name, name)) &&
Daniel Veillarda57c26e2002-08-01 12:52:24 +00006372 (prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))) {
Owen Taylor3473f882001-02-23 17:55:21 +00006373 if (prop->children != NULL)
6374 xmlFreeNodeList(prop->children);
6375 prop->children = NULL;
6376 prop->last = NULL;
6377 prop->ns = ns;
6378 if (value != NULL) {
6379 xmlChar *buffer;
6380 xmlNodePtr tmp;
6381
6382 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
6383 prop->children = xmlStringGetNodeList(node->doc, buffer);
6384 prop->last = NULL;
6385 tmp = prop->children;
6386 while (tmp != NULL) {
6387 tmp->parent = (xmlNodePtr) prop;
6388 if (tmp->next == NULL)
6389 prop->last = tmp;
6390 tmp = tmp->next;
6391 }
6392 xmlFree(buffer);
6393 }
6394 return(prop);
6395 }
6396 prop = prop->next;
6397 }
6398 prop = xmlNewNsProp(node, ns, name, value);
6399 return(prop);
6400}
6401
Daniel Veillard652327a2003-09-29 18:02:38 +00006402#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard75bea542001-05-11 17:41:21 +00006403
6404/**
Owen Taylor3473f882001-02-23 17:55:21 +00006405 * xmlNodeIsText:
6406 * @node: the node
6407 *
6408 * Is this node a Text node ?
6409 * Returns 1 yes, 0 no
6410 */
6411int
6412xmlNodeIsText(xmlNodePtr node) {
6413 if (node == NULL) return(0);
6414
6415 if (node->type == XML_TEXT_NODE) return(1);
6416 return(0);
6417}
6418
6419/**
6420 * xmlIsBlankNode:
6421 * @node: the node
6422 *
6423 * Checks whether this node is an empty or whitespace only
6424 * (and possibly ignorable) text-node.
6425 *
6426 * Returns 1 yes, 0 no
6427 */
6428int
6429xmlIsBlankNode(xmlNodePtr node) {
6430 const xmlChar *cur;
6431 if (node == NULL) return(0);
6432
Daniel Veillard7db37732001-07-12 01:20:08 +00006433 if ((node->type != XML_TEXT_NODE) &&
6434 (node->type != XML_CDATA_SECTION_NODE))
6435 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006436 if (node->content == NULL) return(1);
Owen Taylor3473f882001-02-23 17:55:21 +00006437 cur = node->content;
Owen Taylor3473f882001-02-23 17:55:21 +00006438 while (*cur != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00006439 if (!IS_BLANK_CH(*cur)) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006440 cur++;
6441 }
6442
6443 return(1);
6444}
6445
6446/**
6447 * xmlTextConcat:
6448 * @node: the node
6449 * @content: the content
Daniel Veillard60087f32001-10-10 09:45:09 +00006450 * @len: @content length
Owen Taylor3473f882001-02-23 17:55:21 +00006451 *
6452 * Concat the given string at the end of the existing node content
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006453 *
6454 * Returns -1 in case of error, 0 otherwise
Owen Taylor3473f882001-02-23 17:55:21 +00006455 */
6456
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006457int
Owen Taylor3473f882001-02-23 17:55:21 +00006458xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006459 if (node == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006460
6461 if ((node->type != XML_TEXT_NODE) &&
6462 (node->type != XML_CDATA_SECTION_NODE)) {
6463#ifdef DEBUG_TREE
6464 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006465 "xmlTextConcat: node is not text nor CDATA\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006466#endif
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006467 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006468 }
William M. Brack7762bb12004-01-04 14:49:01 +00006469 /* need to check if content is currently in the dictionary */
6470 if ((node->doc != NULL) && (node->doc->dict != NULL) &&
6471 xmlDictOwns(node->doc->dict, node->content)) {
6472 node->content = xmlStrncatNew(node->content, content, len);
6473 } else {
6474 node->content = xmlStrncat(node->content, content, len);
6475 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006476 if (node->content == NULL)
6477 return(-1);
6478 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006479}
6480
6481/************************************************************************
6482 * *
6483 * Output : to a FILE or in memory *
6484 * *
6485 ************************************************************************/
6486
Owen Taylor3473f882001-02-23 17:55:21 +00006487/**
6488 * xmlBufferCreate:
6489 *
6490 * routine to create an XML buffer.
6491 * returns the new structure.
6492 */
6493xmlBufferPtr
6494xmlBufferCreate(void) {
6495 xmlBufferPtr ret;
6496
6497 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6498 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006499 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006500 return(NULL);
6501 }
6502 ret->use = 0;
Daniel Veillarde356c282001-03-10 12:32:04 +00006503 ret->size = xmlDefaultBufferSize;
Owen Taylor3473f882001-02-23 17:55:21 +00006504 ret->alloc = xmlBufferAllocScheme;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00006505 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00006506 if (ret->content == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006507 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006508 xmlFree(ret);
6509 return(NULL);
6510 }
6511 ret->content[0] = 0;
6512 return(ret);
6513}
6514
6515/**
6516 * xmlBufferCreateSize:
6517 * @size: initial size of buffer
6518 *
6519 * routine to create an XML buffer.
6520 * returns the new structure.
6521 */
6522xmlBufferPtr
6523xmlBufferCreateSize(size_t size) {
6524 xmlBufferPtr ret;
6525
6526 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6527 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006528 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006529 return(NULL);
6530 }
6531 ret->use = 0;
6532 ret->alloc = xmlBufferAllocScheme;
6533 ret->size = (size ? size+2 : 0); /* +1 for ending null */
6534 if (ret->size){
Daniel Veillard3c908dc2003-04-19 00:07:51 +00006535 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00006536 if (ret->content == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006537 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006538 xmlFree(ret);
6539 return(NULL);
6540 }
6541 ret->content[0] = 0;
6542 } else
6543 ret->content = NULL;
6544 return(ret);
6545}
6546
6547/**
Daniel Veillard53350552003-09-18 13:35:51 +00006548 * xmlBufferCreateStatic:
6549 * @mem: the memory area
6550 * @size: the size in byte
6551 *
MST 2003 John Flecka0e7e932003-12-19 03:13:47 +00006552 * routine to create an XML buffer from an immutable memory area.
6553 * The area won't be modified nor copied, and is expected to be
Daniel Veillard53350552003-09-18 13:35:51 +00006554 * present until the end of the buffer lifetime.
6555 *
6556 * returns the new structure.
6557 */
6558xmlBufferPtr
6559xmlBufferCreateStatic(void *mem, size_t size) {
6560 xmlBufferPtr ret;
6561
6562 if ((mem == NULL) || (size == 0))
6563 return(NULL);
6564
6565 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6566 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006567 xmlTreeErrMemory("creating buffer");
Daniel Veillard53350552003-09-18 13:35:51 +00006568 return(NULL);
6569 }
6570 ret->use = size;
6571 ret->size = size;
6572 ret->alloc = XML_BUFFER_ALLOC_IMMUTABLE;
6573 ret->content = (xmlChar *) mem;
6574 return(ret);
6575}
6576
6577/**
Owen Taylor3473f882001-02-23 17:55:21 +00006578 * xmlBufferSetAllocationScheme:
Daniel Veillardbd9afb52002-09-25 22:25:35 +00006579 * @buf: the buffer to tune
Owen Taylor3473f882001-02-23 17:55:21 +00006580 * @scheme: allocation scheme to use
6581 *
6582 * Sets the allocation scheme for this buffer
6583 */
6584void
6585xmlBufferSetAllocationScheme(xmlBufferPtr buf,
6586 xmlBufferAllocationScheme scheme) {
6587 if (buf == NULL) {
6588#ifdef DEBUG_BUFFER
6589 xmlGenericError(xmlGenericErrorContext,
6590 "xmlBufferSetAllocationScheme: buf == NULL\n");
6591#endif
6592 return;
6593 }
Daniel Veillard53350552003-09-18 13:35:51 +00006594 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00006595
6596 buf->alloc = scheme;
6597}
6598
6599/**
6600 * xmlBufferFree:
6601 * @buf: the buffer to free
6602 *
Daniel Veillard9d06d302002-01-22 18:15:52 +00006603 * Frees an XML buffer. It frees both the content and the structure which
6604 * encapsulate it.
Owen Taylor3473f882001-02-23 17:55:21 +00006605 */
6606void
6607xmlBufferFree(xmlBufferPtr buf) {
6608 if (buf == NULL) {
6609#ifdef DEBUG_BUFFER
6610 xmlGenericError(xmlGenericErrorContext,
6611 "xmlBufferFree: buf == NULL\n");
6612#endif
6613 return;
6614 }
Daniel Veillard53350552003-09-18 13:35:51 +00006615
6616 if ((buf->content != NULL) &&
6617 (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006618 xmlFree(buf->content);
6619 }
Owen Taylor3473f882001-02-23 17:55:21 +00006620 xmlFree(buf);
6621}
6622
6623/**
6624 * xmlBufferEmpty:
6625 * @buf: the buffer
6626 *
6627 * empty a buffer.
6628 */
6629void
6630xmlBufferEmpty(xmlBufferPtr buf) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00006631 if (buf == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +00006632 if (buf->content == NULL) return;
6633 buf->use = 0;
Daniel Veillard53350552003-09-18 13:35:51 +00006634 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00006635 buf->content = BAD_CAST "";
Daniel Veillard53350552003-09-18 13:35:51 +00006636 } else {
6637 memset(buf->content, 0, buf->size);
6638 }
Owen Taylor3473f882001-02-23 17:55:21 +00006639}
6640
6641/**
6642 * xmlBufferShrink:
6643 * @buf: the buffer to dump
6644 * @len: the number of xmlChar to remove
6645 *
6646 * Remove the beginning of an XML buffer.
6647 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006648 * Returns the number of #xmlChar removed, or -1 in case of failure.
Owen Taylor3473f882001-02-23 17:55:21 +00006649 */
6650int
6651xmlBufferShrink(xmlBufferPtr buf, unsigned int len) {
Daniel Veillard3d97e662004-11-04 10:49:00 +00006652 if (buf == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006653 if (len == 0) return(0);
6654 if (len > buf->use) return(-1);
6655
6656 buf->use -= len;
Daniel Veillard53350552003-09-18 13:35:51 +00006657 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) {
6658 buf->content += len;
6659 } else {
6660 memmove(buf->content, &buf->content[len], buf->use * sizeof(xmlChar));
6661 buf->content[buf->use] = 0;
6662 }
Owen Taylor3473f882001-02-23 17:55:21 +00006663 return(len);
6664}
6665
6666/**
6667 * xmlBufferGrow:
6668 * @buf: the buffer
6669 * @len: the minimum free size to allocate
6670 *
6671 * Grow the available space of an XML buffer.
6672 *
6673 * Returns the new available space or -1 in case of error
6674 */
6675int
6676xmlBufferGrow(xmlBufferPtr buf, unsigned int len) {
6677 int size;
6678 xmlChar *newbuf;
6679
Daniel Veillard3d97e662004-11-04 10:49:00 +00006680 if (buf == NULL) return(-1);
6681
Daniel Veillard53350552003-09-18 13:35:51 +00006682 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006683 if (len + buf->use < buf->size) return(0);
6684
William M. Brack30fe43f2004-07-26 18:00:58 +00006685/*
6686 * Windows has a BIG problem on realloc timing, so we try to double
6687 * the buffer size (if that's enough) (bug 146697)
6688 */
6689#ifdef WIN32
6690 if (buf->size > len)
6691 size = buf->size * 2;
6692 else
6693 size = buf->use + len + 100;
6694#else
Owen Taylor3473f882001-02-23 17:55:21 +00006695 size = buf->use + len + 100;
William M. Brack30fe43f2004-07-26 18:00:58 +00006696#endif
Owen Taylor3473f882001-02-23 17:55:21 +00006697
6698 newbuf = (xmlChar *) xmlRealloc(buf->content, size);
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006699 if (newbuf == NULL) {
6700 xmlTreeErrMemory("growing buffer");
6701 return(-1);
6702 }
Owen Taylor3473f882001-02-23 17:55:21 +00006703 buf->content = newbuf;
6704 buf->size = size;
6705 return(buf->size - buf->use);
6706}
6707
6708/**
6709 * xmlBufferDump:
6710 * @file: the file output
6711 * @buf: the buffer to dump
6712 *
6713 * Dumps an XML buffer to a FILE *.
Daniel Veillardd1640922001-12-17 15:30:10 +00006714 * Returns the number of #xmlChar written
Owen Taylor3473f882001-02-23 17:55:21 +00006715 */
6716int
6717xmlBufferDump(FILE *file, xmlBufferPtr buf) {
6718 int ret;
6719
6720 if (buf == NULL) {
6721#ifdef DEBUG_BUFFER
6722 xmlGenericError(xmlGenericErrorContext,
6723 "xmlBufferDump: buf == NULL\n");
6724#endif
6725 return(0);
6726 }
6727 if (buf->content == NULL) {
6728#ifdef DEBUG_BUFFER
6729 xmlGenericError(xmlGenericErrorContext,
6730 "xmlBufferDump: buf->content == NULL\n");
6731#endif
6732 return(0);
6733 }
Daniel Veillardcd337f02001-11-22 18:20:37 +00006734 if (file == NULL)
6735 file = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00006736 ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
6737 return(ret);
6738}
6739
6740/**
6741 * xmlBufferContent:
6742 * @buf: the buffer
6743 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00006744 * Function to extract the content of a buffer
6745 *
Owen Taylor3473f882001-02-23 17:55:21 +00006746 * Returns the internal content
6747 */
6748
Daniel Veillard5e2dace2001-07-18 19:30:27 +00006749const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00006750xmlBufferContent(const xmlBufferPtr buf)
6751{
6752 if(!buf)
6753 return NULL;
6754
6755 return buf->content;
6756}
6757
6758/**
6759 * xmlBufferLength:
6760 * @buf: the buffer
6761 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00006762 * Function to get the length of a buffer
6763 *
Owen Taylor3473f882001-02-23 17:55:21 +00006764 * Returns the length of data in the internal content
6765 */
6766
6767int
6768xmlBufferLength(const xmlBufferPtr buf)
6769{
6770 if(!buf)
6771 return 0;
6772
6773 return buf->use;
6774}
6775
6776/**
6777 * xmlBufferResize:
6778 * @buf: the buffer to resize
6779 * @size: the desired size
6780 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006781 * Resize a buffer to accommodate minimum size of @size.
Owen Taylor3473f882001-02-23 17:55:21 +00006782 *
6783 * Returns 0 in case of problems, 1 otherwise
6784 */
6785int
6786xmlBufferResize(xmlBufferPtr buf, unsigned int size)
6787{
6788 unsigned int newSize;
6789 xmlChar* rebuf = NULL;
6790
Daniel Veillardd005b9e2004-11-03 17:07:05 +00006791 if (buf == NULL)
6792 return(0);
6793
Daniel Veillard53350552003-09-18 13:35:51 +00006794 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
6795
Owen Taylor3473f882001-02-23 17:55:21 +00006796 /* Don't resize if we don't have to */
6797 if (size < buf->size)
6798 return 1;
6799
6800 /* figure out new size */
6801 switch (buf->alloc){
6802 case XML_BUFFER_ALLOC_DOUBLEIT:
Daniel Veillardbf629492004-04-20 22:20:59 +00006803 /*take care of empty case*/
6804 newSize = (buf->size ? buf->size*2 : size + 10);
Owen Taylor3473f882001-02-23 17:55:21 +00006805 while (size > newSize) newSize *= 2;
6806 break;
6807 case XML_BUFFER_ALLOC_EXACT:
6808 newSize = size+10;
6809 break;
6810 default:
6811 newSize = size+10;
6812 break;
6813 }
6814
6815 if (buf->content == NULL)
Daniel Veillard3c908dc2003-04-19 00:07:51 +00006816 rebuf = (xmlChar *) xmlMallocAtomic(newSize * sizeof(xmlChar));
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006817 else if (buf->size - buf->use < 100) {
Owen Taylor3473f882001-02-23 17:55:21 +00006818 rebuf = (xmlChar *) xmlRealloc(buf->content,
6819 newSize * sizeof(xmlChar));
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006820 } else {
6821 /*
6822 * if we are reallocating a buffer far from being full, it's
6823 * better to make a new allocation and copy only the used range
6824 * and free the old one.
6825 */
6826 rebuf = (xmlChar *) xmlMallocAtomic(newSize * sizeof(xmlChar));
6827 if (rebuf != NULL) {
6828 memcpy(rebuf, buf->content, buf->use);
6829 xmlFree(buf->content);
William M. Brack42331a92004-07-29 07:07:16 +00006830 rebuf[buf->use] = 0;
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006831 }
6832 }
Owen Taylor3473f882001-02-23 17:55:21 +00006833 if (rebuf == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006834 xmlTreeErrMemory("growing buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006835 return 0;
6836 }
6837 buf->content = rebuf;
6838 buf->size = newSize;
6839
6840 return 1;
6841}
6842
6843/**
6844 * xmlBufferAdd:
6845 * @buf: the buffer to dump
Daniel Veillardd1640922001-12-17 15:30:10 +00006846 * @str: the #xmlChar string
6847 * @len: the number of #xmlChar to add
Owen Taylor3473f882001-02-23 17:55:21 +00006848 *
Daniel Veillard60087f32001-10-10 09:45:09 +00006849 * Add a string range to an XML buffer. if len == -1, the length of
Owen Taylor3473f882001-02-23 17:55:21 +00006850 * str is recomputed.
William M. Bracka3215c72004-07-31 16:24:01 +00006851 *
6852 * Returns 0 successful, a positive error code number otherwise
6853 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00006854 */
William M. Bracka3215c72004-07-31 16:24:01 +00006855int
Owen Taylor3473f882001-02-23 17:55:21 +00006856xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) {
6857 unsigned int needSize;
6858
Daniel Veillardd005b9e2004-11-03 17:07:05 +00006859 if ((str == NULL) || (buf == NULL)) {
William M. Bracka3215c72004-07-31 16:24:01 +00006860 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006861 }
William M. Bracka3215c72004-07-31 16:24:01 +00006862 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006863 if (len < -1) {
6864#ifdef DEBUG_BUFFER
6865 xmlGenericError(xmlGenericErrorContext,
6866 "xmlBufferAdd: len < 0\n");
6867#endif
William M. Bracka3215c72004-07-31 16:24:01 +00006868 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006869 }
William M. Bracka3215c72004-07-31 16:24:01 +00006870 if (len == 0) return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006871
6872 if (len < 0)
6873 len = xmlStrlen(str);
6874
William M. Bracka3215c72004-07-31 16:24:01 +00006875 if (len <= 0) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006876
6877 needSize = buf->use + len + 2;
6878 if (needSize > buf->size){
6879 if (!xmlBufferResize(buf, needSize)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006880 xmlTreeErrMemory("growing buffer");
William M. Bracka3215c72004-07-31 16:24:01 +00006881 return XML_ERR_NO_MEMORY;
Owen Taylor3473f882001-02-23 17:55:21 +00006882 }
6883 }
6884
6885 memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
6886 buf->use += len;
6887 buf->content[buf->use] = 0;
William M. Bracka3215c72004-07-31 16:24:01 +00006888 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006889}
6890
6891/**
6892 * xmlBufferAddHead:
6893 * @buf: the buffer
Daniel Veillardd1640922001-12-17 15:30:10 +00006894 * @str: the #xmlChar string
6895 * @len: the number of #xmlChar to add
Owen Taylor3473f882001-02-23 17:55:21 +00006896 *
6897 * Add a string range to the beginning of an XML buffer.
Daniel Veillard60087f32001-10-10 09:45:09 +00006898 * if len == -1, the length of @str is recomputed.
William M. Bracka3215c72004-07-31 16:24:01 +00006899 *
6900 * Returns 0 successful, a positive error code number otherwise
6901 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00006902 */
William M. Bracka3215c72004-07-31 16:24:01 +00006903int
Owen Taylor3473f882001-02-23 17:55:21 +00006904xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) {
6905 unsigned int needSize;
6906
Daniel Veillardd005b9e2004-11-03 17:07:05 +00006907 if (buf == NULL)
6908 return(-1);
William M. Bracka3215c72004-07-31 16:24:01 +00006909 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006910 if (str == NULL) {
6911#ifdef DEBUG_BUFFER
6912 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006913 "xmlBufferAddHead: str == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006914#endif
William M. Bracka3215c72004-07-31 16:24:01 +00006915 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006916 }
6917 if (len < -1) {
6918#ifdef DEBUG_BUFFER
6919 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006920 "xmlBufferAddHead: len < 0\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006921#endif
William M. Bracka3215c72004-07-31 16:24:01 +00006922 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006923 }
William M. Bracka3215c72004-07-31 16:24:01 +00006924 if (len == 0) return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006925
6926 if (len < 0)
6927 len = xmlStrlen(str);
6928
William M. Bracka3215c72004-07-31 16:24:01 +00006929 if (len <= 0) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006930
6931 needSize = buf->use + len + 2;
6932 if (needSize > buf->size){
6933 if (!xmlBufferResize(buf, needSize)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006934 xmlTreeErrMemory("growing buffer");
William M. Bracka3215c72004-07-31 16:24:01 +00006935 return XML_ERR_NO_MEMORY;
Owen Taylor3473f882001-02-23 17:55:21 +00006936 }
6937 }
6938
6939 memmove(&buf->content[len], &buf->content[0], buf->use * sizeof(xmlChar));
6940 memmove(&buf->content[0], str, len * sizeof(xmlChar));
6941 buf->use += len;
6942 buf->content[buf->use] = 0;
William M. Bracka3215c72004-07-31 16:24:01 +00006943 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006944}
6945
6946/**
6947 * xmlBufferCat:
William M. Bracka3215c72004-07-31 16:24:01 +00006948 * @buf: the buffer to add to
Daniel Veillardd1640922001-12-17 15:30:10 +00006949 * @str: the #xmlChar string
Owen Taylor3473f882001-02-23 17:55:21 +00006950 *
6951 * Append a zero terminated string to an XML buffer.
William M. Bracka3215c72004-07-31 16:24:01 +00006952 *
6953 * Returns 0 successful, a positive error code number otherwise
6954 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00006955 */
William M. Bracka3215c72004-07-31 16:24:01 +00006956int
Owen Taylor3473f882001-02-23 17:55:21 +00006957xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00006958 if (buf == NULL)
6959 return(-1);
William M. Bracka3215c72004-07-31 16:24:01 +00006960 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
6961 if (str == NULL) return -1;
6962 return xmlBufferAdd(buf, str, -1);
Owen Taylor3473f882001-02-23 17:55:21 +00006963}
6964
6965/**
6966 * xmlBufferCCat:
6967 * @buf: the buffer to dump
6968 * @str: the C char string
6969 *
6970 * Append a zero terminated C string to an XML buffer.
William M. Bracka3215c72004-07-31 16:24:01 +00006971 *
6972 * Returns 0 successful, a positive error code number otherwise
6973 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00006974 */
William M. Bracka3215c72004-07-31 16:24:01 +00006975int
Owen Taylor3473f882001-02-23 17:55:21 +00006976xmlBufferCCat(xmlBufferPtr buf, const char *str) {
6977 const char *cur;
6978
Daniel Veillardd005b9e2004-11-03 17:07:05 +00006979 if (buf == NULL)
6980 return(-1);
William M. Bracka3215c72004-07-31 16:24:01 +00006981 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006982 if (str == NULL) {
6983#ifdef DEBUG_BUFFER
6984 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006985 "xmlBufferCCat: str == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006986#endif
William M. Bracka3215c72004-07-31 16:24:01 +00006987 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006988 }
6989 for (cur = str;*cur != 0;cur++) {
6990 if (buf->use + 10 >= buf->size) {
6991 if (!xmlBufferResize(buf, buf->use+10)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006992 xmlTreeErrMemory("growing buffer");
William M. Bracka3215c72004-07-31 16:24:01 +00006993 return XML_ERR_NO_MEMORY;
Owen Taylor3473f882001-02-23 17:55:21 +00006994 }
6995 }
6996 buf->content[buf->use++] = *cur;
6997 }
6998 buf->content[buf->use] = 0;
William M. Bracka3215c72004-07-31 16:24:01 +00006999 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007000}
7001
7002/**
7003 * xmlBufferWriteCHAR:
7004 * @buf: the XML buffer
7005 * @string: the string to add
7006 *
7007 * routine which manages and grows an output buffer. This one adds
7008 * xmlChars at the end of the buffer.
7009 */
7010void
Daniel Veillard53350552003-09-18 13:35:51 +00007011xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007012 if (buf == NULL)
7013 return;
Daniel Veillard53350552003-09-18 13:35:51 +00007014 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00007015 xmlBufferCat(buf, string);
7016}
7017
7018/**
7019 * xmlBufferWriteChar:
7020 * @buf: the XML buffer output
7021 * @string: the string to add
7022 *
7023 * routine which manage and grows an output buffer. This one add
7024 * C chars at the end of the array.
7025 */
7026void
7027xmlBufferWriteChar(xmlBufferPtr buf, const char *string) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007028 if (buf == NULL)
7029 return;
Daniel Veillard53350552003-09-18 13:35:51 +00007030 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00007031 xmlBufferCCat(buf, string);
7032}
7033
7034
7035/**
7036 * xmlBufferWriteQuotedString:
7037 * @buf: the XML buffer output
7038 * @string: the string to add
7039 *
7040 * routine which manage and grows an output buffer. This one writes
Daniel Veillardd1640922001-12-17 15:30:10 +00007041 * a quoted or double quoted #xmlChar string, checking first if it holds
Owen Taylor3473f882001-02-23 17:55:21 +00007042 * quote or double-quotes internally
7043 */
7044void
7045xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) {
Daniel Veillard39057f42003-08-04 01:33:43 +00007046 const xmlChar *cur, *base;
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007047 if (buf == NULL)
7048 return;
Daniel Veillard53350552003-09-18 13:35:51 +00007049 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Daniel Veillard39057f42003-08-04 01:33:43 +00007050 if (xmlStrchr(string, '\"')) {
Daniel Veillard20aa0fb2003-08-04 19:43:15 +00007051 if (xmlStrchr(string, '\'')) {
Owen Taylor3473f882001-02-23 17:55:21 +00007052#ifdef DEBUG_BUFFER
7053 xmlGenericError(xmlGenericErrorContext,
7054 "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n");
7055#endif
Daniel Veillard39057f42003-08-04 01:33:43 +00007056 xmlBufferCCat(buf, "\"");
7057 base = cur = string;
7058 while(*cur != 0){
7059 if(*cur == '"'){
7060 if (base != cur)
7061 xmlBufferAdd(buf, base, cur - base);
7062 xmlBufferAdd(buf, BAD_CAST "&quot;", 6);
7063 cur++;
7064 base = cur;
7065 }
7066 else {
7067 cur++;
7068 }
7069 }
7070 if (base != cur)
7071 xmlBufferAdd(buf, base, cur - base);
7072 xmlBufferCCat(buf, "\"");
Owen Taylor3473f882001-02-23 17:55:21 +00007073 }
Daniel Veillard39057f42003-08-04 01:33:43 +00007074 else{
7075 xmlBufferCCat(buf, "\'");
7076 xmlBufferCat(buf, string);
7077 xmlBufferCCat(buf, "\'");
7078 }
Owen Taylor3473f882001-02-23 17:55:21 +00007079 } else {
7080 xmlBufferCCat(buf, "\"");
7081 xmlBufferCat(buf, string);
7082 xmlBufferCCat(buf, "\"");
7083 }
7084}
7085
7086
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007087/**
7088 * xmlGetDocCompressMode:
7089 * @doc: the document
7090 *
7091 * get the compression ratio for a document, ZLIB based
7092 * Returns 0 (uncompressed) to 9 (max compression)
7093 */
7094int
7095xmlGetDocCompressMode (xmlDocPtr doc) {
7096 if (doc == NULL) return(-1);
7097 return(doc->compression);
7098}
7099
7100/**
7101 * xmlSetDocCompressMode:
7102 * @doc: the document
7103 * @mode: the compression ratio
7104 *
7105 * set the compression ratio for a document, ZLIB based
7106 * Correct values: 0 (uncompressed) to 9 (max compression)
7107 */
7108void
7109xmlSetDocCompressMode (xmlDocPtr doc, int mode) {
7110 if (doc == NULL) return;
7111 if (mode < 0) doc->compression = 0;
7112 else if (mode > 9) doc->compression = 9;
7113 else doc->compression = mode;
7114}
7115
7116/**
7117 * xmlGetCompressMode:
7118 *
7119 * get the default compression mode used, ZLIB based.
7120 * Returns 0 (uncompressed) to 9 (max compression)
7121 */
7122int
7123xmlGetCompressMode(void)
7124{
7125 return (xmlCompressMode);
7126}
7127
7128/**
7129 * xmlSetCompressMode:
7130 * @mode: the compression ratio
7131 *
7132 * set the default compression mode used, ZLIB based
7133 * Correct values: 0 (uncompressed) to 9 (max compression)
7134 */
7135void
7136xmlSetCompressMode(int mode) {
7137 if (mode < 0) xmlCompressMode = 0;
7138 else if (mode > 9) xmlCompressMode = 9;
7139 else xmlCompressMode = mode;
7140}
7141
Kasimier T. Buchcik4d9c9482005-06-27 15:04:46 +00007142/*
7143* xmlDOMWrapNewCtxt:
7144*
7145* Allocates and initializes a new DOM-wrapper context.
7146*
7147* Returns the xmlDOMWrapCtxtPtr or NULL in case of an internal errror.
7148*/
7149xmlDOMWrapCtxtPtr
7150xmlDOMWrapNewCtxt(void)
7151{
7152 xmlDOMWrapCtxtPtr ret;
7153
7154 ret = xmlMalloc(sizeof(xmlDOMWrapCtxt));
7155 if (ret == NULL) {
7156 xmlTreeErrMemory("allocating DOM-wrapper context");
7157 return (NULL);
7158 }
7159 memset(ret, 0, sizeof(xmlDOMWrapCtxt));
7160 return (ret);
7161}
7162
7163/*
7164* xmlDOMWrapFreeCtxt:
7165* @ctxt: the DOM-wrapper context
7166*
7167* Frees the DOM-wrapper context.
7168*/
7169void
7170xmlDOMWrapFreeCtxt(xmlDOMWrapCtxtPtr ctxt)
7171{
7172 if (ctxt == NULL)
7173 return;
7174 xmlFree(ctxt);
7175}
7176
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007177#define XML_TREE_NSMAP_PARENT -1
7178#define XML_TREE_NSMAP_XML -2
7179#define XML_TREE_NSMAP_DOC -3
7180#define XML_TREE_NSMAP_CUSTOM -4
7181
7182typedef struct xmlNsMapItem *xmlNsMapItemPtr;
7183struct xmlNsMapItem {
7184 xmlNsMapItemPtr next;
7185 xmlNsMapItemPtr prev;
7186 xmlNsPtr oldNs; /* old ns decl reference */
7187 xmlNsPtr newNs; /* new ns decl reference */
7188 int shadowDepth; /* Shadowed at this depth */
7189 /*
7190 * depth:
7191 * >= 0 == @node's ns-decls
7192 * -1 == @parent's ns-decls
7193 * -2 == @parent's out-of-scope ns-decls
7194 * -3 == the doc->oldNs XML ns-decl
7195 * -4 == the doc->oldNs storage ns-decls
7196 */
7197 int depth;
7198};
7199
7200/*
7201* xmlTreeAddNsMapItem:
7202* @map: the ns-map
7203* @cur: the current map entry to append a new entry to
7204* @oldNs: the old ns-struct
7205* @newNs: the new ns-struct
7206* @depth: depth and ns-kind information
7207*
7208* Frees the ns-map
7209*/
7210static xmlNsMapItemPtr
7211xmlDOMWrapNSNormAddNsMapItem(xmlNsMapItemPtr *map,
7212 xmlNsMapItemPtr *cur,
7213 xmlNsPtr oldNs,
7214 xmlNsPtr newNs,
7215 int depth)
7216{
7217 xmlNsMapItemPtr ret;
7218
7219 if ((cur != NULL) && (*cur != NULL) && ((*cur)->next != NULL)) {
7220 /*
7221 * Reuse.
7222 */
7223 ret = (*cur)->next;
7224 *cur = ret;
7225 } else {
7226 ret = (xmlNsMapItemPtr) xmlMalloc(sizeof(struct xmlNsMapItem));
7227 if (ret == NULL) {
7228 xmlTreeErrMemory("allocating namespace map item");
7229 return (NULL);
7230 }
7231 memset(ret, 0, sizeof(struct xmlNsMapItem));
7232 if (*map == NULL) {
7233 /*
7234 * First ever.
7235 */
7236 *map = ret;
7237 ret->prev = ret;
7238 if (cur != NULL)
7239 *cur = ret;
7240 } else {
7241 if (cur) {
7242 /*
7243 * Append.
7244 */
7245 (*cur)->next = ret;
7246 ret->prev = *cur;
7247 *cur = ret;
7248 } else {
7249 /*
7250 * Set on first position.
7251 */
7252 ret->next = (*map);
7253 ret->prev = (*map)->prev;
7254 (*map)->prev = ret;
7255 *map = ret;
7256 }
7257 }
7258 }
7259 ret->oldNs = oldNs;
7260 ret->newNs = newNs;
7261 ret->shadowDepth = -1;
7262 ret->depth = depth;
7263 return (ret);
7264}
7265
7266/*
7267* xmlTreeFreeNsMap:
7268* @map: the ns-map
7269*
7270* Frees the ns-map
7271*/
7272static void
7273xmlDOMWrapNSNormFreeNsMap(xmlNsMapItemPtr map)
7274{
7275 xmlNsMapItemPtr mi = map, miprev;
7276
7277 while (mi != NULL) {
7278 miprev = mi;
7279 mi = mi->next;
7280 xmlFree(miprev);
7281 }
7282}
7283
7284/*
7285* xmlTreeEnsureXMLDecl:
7286* @doc: the doc
7287*
7288* Ensures that there is an XML namespace declaration on the doc.
7289*
7290* Returns the XML ns-struct or NULL on API and internal errors.
7291*/
7292static xmlNsPtr
7293xmlTreeEnsureXMLDecl(xmlDocPtr doc)
7294{
7295 if (doc == NULL)
7296 return (NULL);
7297 if (doc->oldNs != NULL)
7298 return (doc->oldNs);
7299 {
7300 xmlNsPtr ns;
7301 ns = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
7302 if (ns == NULL) {
7303 xmlTreeErrMemory(
7304 "allocating the XML namespace");
7305 return (NULL);
7306 }
7307 memset(ns, 0, sizeof(xmlNs));
7308 ns->type = XML_LOCAL_NAMESPACE;
7309 ns->href = xmlStrdup(XML_XML_NAMESPACE);
7310 ns->prefix = xmlStrdup((const xmlChar *)"xml");
7311 doc->oldNs = ns;
7312 return (ns);
7313 }
7314}
7315
7316/*
7317* xmlDOMWrapStoreNs:
7318* @doc: the doc
7319* @nsName: the namespace name
7320* @prefix: the prefix
7321*
7322* Creates or reuses an xmlNs struct on doc->oldNs with
7323* the given prefix and namespace name.
7324*
7325* Returns the aquired ns struct or NULL in case of an API
7326* or internal error.
7327*/
7328static xmlNsPtr
7329xmlDOMWrapStoreNs(xmlDocPtr doc,
7330 const xmlChar *nsName,
7331 const xmlChar *prefix)
7332{
7333 xmlNsPtr ns;
7334
7335 if (doc == NULL)
7336 return (NULL);
7337 ns = xmlTreeEnsureXMLDecl(doc);
7338 if (ns == NULL)
7339 return (NULL);
7340 if (ns->next != NULL) {
7341 /* Reuse. */
7342 ns = ns->next;
7343 while (ns != NULL) {
7344 if (((ns->prefix == prefix) ||
7345 xmlStrEqual(ns->prefix, prefix)) &&
7346 xmlStrEqual(ns->href, nsName)) {
7347 return (ns);
7348 }
7349 if (ns->next == NULL)
7350 break;
7351 ns = ns->next;
7352 }
7353 }
7354 /* Create. */
7355 ns->next = xmlNewNs(NULL, nsName, prefix);
7356 return (ns->next);
7357}
7358
7359/*
7360* xmlTreeLookupNsListByPrefix:
7361* @nsList: a list of ns-structs
7362* @prefix: the searched prefix
7363*
7364* Searches for a ns-decl with the given prefix in @nsList.
7365*
7366* Returns the ns-decl if found, NULL if not found and on
7367* API errors.
7368*/
7369static xmlNsPtr
7370xmlTreeNSListLookupByPrefix(xmlNsPtr nsList, const xmlChar *prefix)
7371{
7372 if (nsList == NULL)
7373 return (NULL);
7374 {
7375 xmlNsPtr ns;
7376 ns = nsList;
7377 do {
7378 if ((prefix == ns->prefix) ||
7379 xmlStrEqual(prefix, ns->prefix)) {
7380 return (ns);
7381 }
7382 ns = ns->next;
7383 } while (ns != NULL);
7384 }
7385 return (NULL);
7386}
7387
7388/*
7389*
7390* xmlTreeGetInScopeNamespaces:
7391* @map: the namespace map
7392* @node: the node to start with
7393*
7394* Puts in-scope namespaces into the ns-map.
7395*
7396* Returns 0 on success, -1 on API or internal errors.
7397*/
7398static int
7399xmlDOMWrapNSNormGatherInScopeNs(xmlNsMapItemPtr *map,
7400 xmlNodePtr node)
7401{
7402 xmlNodePtr cur;
7403 xmlNsPtr ns;
7404 xmlNsMapItemPtr mi;
7405 int shadowed;
7406
7407 if ((map == NULL) || (*map != NULL))
7408 return (-1);
7409 /*
7410 * Get in-scope ns-decls of @parent.
7411 */
7412 cur = node;
7413 while ((cur != NULL) && (cur != (xmlNodePtr) cur->doc)) {
7414 if (cur->type == XML_ELEMENT_NODE) {
7415 if (cur->nsDef != NULL) {
7416 ns = cur->nsDef;
7417 do {
7418 shadowed = 0;
7419 if (*map != NULL) {
7420 /*
7421 * Skip shadowed prefixes.
7422 */
7423 for (mi = *map; mi != NULL; mi = mi->next) {
7424 if ((ns->prefix == mi->newNs->prefix) ||
7425 xmlStrEqual(ns->prefix, mi->newNs->prefix)) {
7426 shadowed = 1;
7427 break;
7428 }
7429 }
7430 }
7431 /*
7432 * Insert mapping.
7433 */
7434 mi = xmlDOMWrapNSNormAddNsMapItem(map, NULL, NULL,
7435 ns, XML_TREE_NSMAP_PARENT);
7436 if (mi == NULL)
7437 return (-1);
7438 if (shadowed)
7439 mi->shadowDepth = 0;
7440 ns = ns->next;
7441 } while (ns != NULL);
7442 }
7443 }
7444 cur = cur->parent;
7445 }
7446 return (0);
7447}
7448
7449/*
7450* XML_TREE_ADOPT_STR: If we have a dest-dict, put @str in the dict;
7451* otherwise copy it, when it was in the source-dict.
7452*/
7453#define XML_TREE_ADOPT_STR(str) \
7454 if (adoptStr && (str != NULL)) { \
7455 if (destDoc->dict) { \
Daniel Veillard7e21fd12005-07-03 21:44:07 +00007456 const xmlChar *old = str; \
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007457 str = xmlDictLookup(destDoc->dict, str, -1); \
Daniel Veillard7e21fd12005-07-03 21:44:07 +00007458 if ((sourceDoc == NULL) || (sourceDoc->dict == NULL) || \
7459 (!xmlDictOwns(sourceDoc->dict, old))) \
Daniel Veillard39e5c892005-07-03 22:48:50 +00007460 xmlFree((char *)old); \
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007461 } else if ((sourceDoc) && (sourceDoc->dict) && \
7462 xmlDictOwns(sourceDoc->dict, str)) { \
7463 str = BAD_CAST xmlStrdup(str); \
7464 } \
7465 }
7466
7467/*
7468* XML_TREE_ADOPT_STR_2: If @str was in the source-dict, then
7469* put it in dest-dict or copy it.
7470*/
7471#define XML_TREE_ADOPT_STR_2(str) \
7472 if (adoptStr && (str != NULL) && (sourceDoc != NULL) && \
7473 (sourceDoc->dict != NULL) && \
7474 xmlDictOwns(sourceDoc->dict, cur->content)) { \
7475 if (destDoc->dict) \
7476 cur->content = (xmlChar *) \
7477 xmlDictLookup(destDoc->dict, cur->content, -1); \
7478 else \
7479 cur->content = xmlStrdup(BAD_CAST cur->content); \
7480 }
7481
7482/*
7483* xmlDOMWrapNSNormAddNsMapItem2:
7484*
7485* For internal use. Adds a ns-decl mapping.
7486*
7487* Returns 0 on success, -1 on internal errors.
7488*/
7489static int
7490xmlDOMWrapNSNormAddNsMapItem2(xmlNsPtr **list, int *size, int *number,
7491 xmlNsPtr oldNs, xmlNsPtr newNs)
7492{
7493 if (*list == NULL) {
7494 *list = (xmlNsPtr *) xmlMalloc(6 * sizeof(xmlNsPtr));
7495 if (*list == NULL) {
7496 xmlTreeErrMemory("alloc ns map item");
7497 return(-1);
7498 }
7499 *size = 3;
7500 *number = 0;
7501 } else if ((*number) >= (*size)) {
7502 *size *= 2;
7503 *list = (xmlNsPtr *) xmlRealloc(*list,
7504 (*size) * 2 * sizeof(xmlNsPtr));
7505 if (*list == NULL) {
7506 xmlTreeErrMemory("realloc ns map item");
7507 return(-1);
7508 }
7509 }
7510 (*list)[2 * (*number)] = oldNs;
7511 (*list)[2 * (*number) +1] = newNs;
7512 (*number)++;
7513 return (0);
7514}
7515
7516/*
7517* xmlDOMWrapRemoveNode:
Daniel Veillard304e78c2005-07-03 16:19:41 +00007518* @ctxt: a DOM wrapper context
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007519* @doc: the doc
7520* @node: the node to be removed.
Daniel Veillard304e78c2005-07-03 16:19:41 +00007521* @options: set of options, unused at the moment
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007522*
7523* Unlinks the given node from its owner.
7524* This will substitute ns-references to node->nsDef for
7525* ns-references to doc->oldNs, thus ensuring the removed
7526* branch to be autark wrt ns-references.
Kasimier T. Buchcik017264f2005-06-27 13:45:24 +00007527* WARNING: This function is in a experimental state.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007528*
7529* Returns 0 on success, 1 if the node is not supported,
7530* -1 on API and internal errors.
7531*/
7532int
7533xmlDOMWrapRemoveNode(xmlDOMWrapCtxtPtr ctxt, xmlDocPtr doc,
7534 xmlNodePtr node, int options ATTRIBUTE_UNUSED)
7535{
7536 xmlNsPtr *list = NULL;
7537 int sizeList, nbList, i, j;
7538 xmlNsPtr ns;
7539
7540 if ((node == NULL) || (doc == NULL) || (node->doc != doc))
7541 return (-1);
7542
7543 /* TODO: 0 or -1 ? */
7544 if (node->parent == NULL)
7545 return (0);
7546
7547 switch (node->type) {
7548 case XML_TEXT_NODE:
7549 case XML_CDATA_SECTION_NODE:
7550 case XML_ENTITY_REF_NODE:
7551 case XML_PI_NODE:
7552 case XML_COMMENT_NODE:
7553 xmlUnlinkNode(node);
7554 return (0);
7555 case XML_ELEMENT_NODE:
7556 case XML_ATTRIBUTE_NODE:
7557 break;
7558 default:
7559 return (1);
7560 }
7561 xmlUnlinkNode(node);
7562 /*
7563 * Save out-of-scope ns-references in doc->oldNs.
7564 */
7565 do {
7566 switch (node->type) {
7567 case XML_ELEMENT_NODE:
7568 if ((ctxt == NULL) && (node->nsDef != NULL)) {
7569 ns = node->nsDef;
7570 do {
7571 if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
7572 &nbList, ns, ns) == -1)
7573 goto internal_error;
7574 ns = ns->next;
7575 } while (ns != NULL);
7576 }
7577 /* No break on purpose. */
7578 case XML_ATTRIBUTE_NODE:
7579 if (node->ns != NULL) {
7580 /*
7581 * Find a mapping.
7582 */
7583 if (list != NULL) {
7584 for (i = 0, j = 0; i < nbList; i++, j += 2) {
7585 if (node->ns == list[j]) {
7586 node->ns = list[++j];
7587 goto next_node;
7588 }
7589 }
7590 }
7591 ns = NULL;
7592 if (ctxt != NULL) {
7593 /*
7594 * User defined.
7595 */
7596 } else {
7597 /*
7598 * Add to doc's oldNs.
7599 */
7600 ns = xmlDOMWrapStoreNs(doc, node->ns->href,
7601 node->ns->prefix);
7602 if (ns == NULL)
7603 goto internal_error;
7604 }
7605 if (ns != NULL) {
7606 /*
7607 * Add mapping.
7608 */
7609 if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
7610 &nbList, node->ns, ns) == -1)
7611 goto internal_error;
7612 }
7613 node->ns = ns;
7614 }
7615 if ((node->type == XML_ELEMENT_NODE) &&
7616 (node->properties != NULL)) {
7617 node = (xmlNodePtr) node->properties;
7618 continue;
7619 }
7620 break;
7621 default:
7622 goto next_sibling;
7623 }
7624next_node:
7625 if ((node->type == XML_ELEMENT_NODE) &&
7626 (node->children != NULL)) {
7627 node = node->children;
7628 continue;
7629 }
7630next_sibling:
7631 if (node == NULL)
7632 break;
7633 if (node->next != NULL)
7634 node = node->next;
7635 else {
7636 node = node->parent;
7637 goto next_sibling;
7638 }
7639 } while (node != NULL);
7640
7641 if (list != NULL)
7642 xmlFree(list);
7643 return (0);
7644
7645internal_error:
7646 if (list != NULL)
7647 xmlFree(list);
7648 return (-1);
7649}
7650
7651/*
7652* xmlSearchNsByHrefStrict:
7653* @doc: the document
7654* @node: the start node
7655* @nsName: the searched namespace name
7656* @retNs: the resulting ns-decl
7657* @prefixed: if the found ns-decl must have a prefix (for attributes)
7658*
7659* Dynamically searches for a ns-declaration which matches
7660* the given @nsName in the ancestor-or-self axis of @node.
7661*
7662* Returns 1 if a ns-decl was found, 0 if not and -1 on API
7663* and internal errors.
7664*/
7665static int
7666xmlSearchNsByHrefStrict(xmlDocPtr doc, xmlNodePtr node, const xmlChar* nsName,
7667 xmlNsPtr *retNs, int prefixed)
7668{
7669 xmlNodePtr cur, prev = NULL, out = NULL;
7670 xmlNsPtr ns, prevns;
7671
7672 if ((doc == NULL) || (nsName == NULL) || (retNs == NULL))
7673 return (-1);
7674
7675 *retNs = NULL;
7676 if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) {
7677 *retNs = xmlTreeEnsureXMLDecl(doc);
7678 if (*retNs == NULL)
7679 return (-1);
7680 return (1);
7681 }
7682 cur = node;
7683 do {
7684 if (cur->type == XML_ELEMENT_NODE) {
7685 if (cur->nsDef != NULL) {
7686 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
7687 if (prefixed && (ns->prefix == NULL))
7688 continue;
7689 if (prev != NULL) {
7690 /*
7691 * Check the last level of ns-decls for a
7692 * shadowing prefix.
7693 */
7694 prevns = prev->nsDef;
7695 do {
7696 if ((prevns->prefix == ns->prefix) ||
7697 ((prevns->prefix != NULL) &&
7698 (ns->prefix != NULL) &&
7699 xmlStrEqual(prevns->prefix, ns->prefix))) {
7700 /*
7701 * Shadowed.
7702 */
7703 break;
7704 }
7705 prevns = prevns->next;
7706 } while (prevns != NULL);
7707 if (prevns != NULL)
7708 continue;
7709 }
7710 /*
7711 * Ns-name comparison.
7712 */
7713 if ((nsName == ns->href) ||
7714 xmlStrEqual(nsName, ns->href)) {
7715 /*
7716 * At this point the prefix can only be shadowed,
7717 * if we are the the (at least) 3rd level of
7718 * ns-decls.
7719 */
7720 if (out) {
7721 int ret;
7722
7723 ret = xmlNsInScope(doc, node, prev, ns->prefix);
7724 if (ret < 0)
7725 return (-1);
7726 /*
7727 * TODO: Should we try to find a matching ns-name
7728 * only once? This here keeps on searching.
7729 * I think we should try further since, there might
7730 * be an other matching ns-decl with an unshadowed
7731 * prefix.
7732 */
7733 if (! ret)
7734 continue;
7735 }
7736 *retNs = ns;
7737 return (1);
7738 }
7739 }
7740 out = prev;
7741 prev = cur;
7742 }
7743 } else if ((node->type == XML_ENTITY_REF_NODE) ||
7744 (node->type == XML_ENTITY_NODE) ||
7745 (node->type == XML_ENTITY_DECL))
7746 return (0);
7747 cur = cur->parent;
7748 } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
7749 return (0);
7750}
7751
7752/*
7753* xmlDOMWrapNSNormDeclareNsForced:
7754* @doc: the doc
7755* @elem: the element-node to declare on
7756* @nsName: the namespace-name of the ns-decl
7757* @prefix: the preferred prefix of the ns-decl
7758* @checkShadow: ensure that the new ns-decl doesn't shadow ancestor ns-decls
7759*
7760* Declares a new namespace on @elem. It tries to use the
7761* given @prefix; if a ns-decl with the given prefix is already existent
7762* on @elem, it will generate an other prefix.
7763*
7764* Returns 1 if a ns-decl was found, 0 if not and -1 on API
7765* and internal errors.
7766*/
7767static xmlNsPtr
7768xmlDOMWrapNSNormDeclareNsForced(xmlDocPtr doc,
7769 xmlNodePtr elem,
7770 const xmlChar *nsName,
7771 const xmlChar *prefix,
7772 int checkShadow)
7773{
7774
7775 xmlNsPtr ret;
7776 char buf[50];
7777 const xmlChar *pref;
7778 int counter = 0;
7779 /*
7780 * Create a ns-decl on @anchor.
7781 */
7782 pref = prefix;
7783 while (1) {
7784 /*
7785 * Lookup whether the prefix is unused in elem's ns-decls.
7786 */
7787 if ((elem->nsDef != NULL) &&
7788 (xmlTreeNSListLookupByPrefix(elem->nsDef, pref) != NULL))
7789 goto ns_next_prefix;
7790 if (checkShadow && elem->parent &&
7791 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
7792 /*
7793 * Does it shadow ancestor ns-decls?
7794 */
7795 if (xmlSearchNs(doc, elem->parent, pref) != NULL)
7796 goto ns_next_prefix;
7797 }
7798 ret = xmlNewNs(NULL, nsName, pref);
7799 if (ret == NULL)
7800 return (NULL);
7801 if (elem->nsDef == NULL)
7802 elem->nsDef = ret;
7803 else {
7804 xmlNsPtr ns2 = elem->nsDef;
7805 while (ns2->next != NULL)
7806 ns2 = ns2->next;
7807 ns2->next = ret;
7808 }
7809 return (ret);
7810ns_next_prefix:
7811 counter++;
7812 if (counter > 1000)
7813 return (NULL);
7814 if (prefix == NULL) {
7815 snprintf((char *) buf, sizeof(buf),
7816 "default%d", counter);
7817 } else
7818 snprintf((char *) buf, sizeof(buf),
7819 "%.30s%d", (char *)prefix, counter);
7820 pref = BAD_CAST buf;
7821 }
7822}
7823
7824/*
7825* xmlDOMWrapNSNormAquireNormalizedNs:
7826* @doc: the doc
7827* @elem: the element-node to declare namespaces on
7828* @ns: the ns-struct to use for the search
7829* @retNs: the found/created ns-struct
7830* @nsMap: the ns-map
7831* @topmi: the last ns-map entry
7832* @depth: the current tree depth
7833* @ancestorsOnly: search in ancestor ns-decls only
7834* @prefixed: if the searched ns-decl must have a prefix (for attributes)
7835*
7836* Searches for a matching ns-name in the ns-decls of @nsMap, if not
7837* found it will either declare it on @elem, or store it in doc->oldNs.
7838* If a new ns-decl needs to be declared on @elem, it tries to use the
7839* @ns->prefix for it, if this prefix is already in use on @elem, it will
7840* change the prefix or the new ns-decl.
7841*
7842* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
7843*/
7844static int
7845xmlDOMWrapNSNormAquireNormalizedNs(xmlDocPtr doc,
7846 xmlNodePtr elem,
7847 xmlNsPtr ns,
7848 xmlNsPtr *retNs,
7849 xmlNsMapItemPtr *nsMap,
7850 xmlNsMapItemPtr *topmi,
7851 int depth,
7852 int ancestorsOnly,
7853 int prefixed)
7854{
7855 xmlNsMapItemPtr mi;
7856
7857 if ((doc == NULL) || (ns == NULL) || (retNs == NULL) ||
7858 (nsMap == NULL) || (topmi == NULL))
7859 return (-1);
7860
7861 *retNs = NULL;
7862 /*
7863 * Handle XML namespace.
7864 */
7865 if ((ns->prefix) &&
7866 (ns->prefix[0] == 'x') &&
7867 (ns->prefix[1] == 'm') &&
7868 (ns->prefix[2] == 'l') &&
7869 (ns->prefix[3] == 0)) {
7870 /*
7871 * Insert XML namespace mapping.
7872 */
7873 *retNs = xmlTreeEnsureXMLDecl(doc);
7874 if (*retNs == NULL)
7875 return (-1);
7876 return (0);
7877 }
7878 /*
7879 * If the search should be done in ancestors only and no
7880 * @elem (the first ancestor) was specified, then skip the search.
7881 */
7882 if ((! (ancestorsOnly && (elem == NULL))) &&
7883 (*nsMap != NULL)) {
7884
7885 /*
7886 * Try to find an equal ns-name in in-scope ns-decls.
7887 */
7888 for (mi = *nsMap; mi != (*topmi)->next; mi = mi->next) {
7889
7890 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
7891 /*
7892 * This should be turned on to gain speed, if one knows
7893 * that the branch itself was already ns-wellformed and no
7894 * stale references existed. I.e. it searches in the ancestor
7895 * axis only.
7896 */
7897 ((! ancestorsOnly) || (mi->depth == XML_TREE_NSMAP_PARENT)) &&
7898 /* Skip shadowed prefixes. */
7899 (mi->shadowDepth == -1) &&
7900 /* Skip xmlns="" or xmlns:foo="". */
7901 ((mi->newNs->href != NULL) &&
7902 (mi->newNs->href[0] != 0)) &&
7903 /* Ensure a prefix if wanted. */
7904 ((! prefixed) || (mi->newNs->prefix != NULL)) &&
7905 /* Equal ns name */
7906 ((mi->newNs->href == ns->href) ||
7907 xmlStrEqual(mi->newNs->href, ns->href))) {
7908 /* Set the mapping. */
7909 mi->oldNs = ns;
7910 *retNs = mi->newNs;
7911 return (0);
7912 }
7913 }
7914 }
7915 /*
7916 * No luck, the namespace is out of scope or shadowed.
7917 */
7918 if (elem == NULL) {
7919 xmlNsPtr tmpns;
7920
7921 /*
7922 * Store ns-decls in "oldNs" of the document-node.
7923 */
7924 tmpns = xmlDOMWrapStoreNs(doc, ns->href, ns->prefix);
7925 if (tmpns == NULL)
7926 return (-1);
7927 /*
7928 * Insert mapping.
7929 */
7930 if (xmlDOMWrapNSNormAddNsMapItem(nsMap, NULL, ns,
7931 tmpns, XML_TREE_NSMAP_DOC) == NULL) {
7932 xmlFreeNs(tmpns);
7933 return (-1);
7934 }
7935 *retNs = tmpns;
7936 } else {
7937 xmlNsPtr tmpns;
7938
7939 tmpns = xmlDOMWrapNSNormDeclareNsForced(doc, elem, ns->href,
7940 ns->prefix, 0);
7941 if (tmpns == NULL)
7942 return (-1);
7943
7944 if (*nsMap != NULL) {
7945 /*
7946 * Does it shadow ancestor ns-decls?
7947 */
7948 for (mi = *nsMap; mi != (*topmi)->next; mi = mi->next) {
7949 if ((mi->depth < depth) &&
7950 (mi->shadowDepth == -1) &&
7951 ((ns->prefix == mi->newNs->prefix) ||
7952 xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
7953 /*
7954 * Shadows.
7955 */
7956 mi->shadowDepth = depth;
7957 break;
7958 }
7959 }
7960 }
7961 if (xmlDOMWrapNSNormAddNsMapItem(nsMap, topmi, ns,
7962 tmpns, depth) == NULL) {
7963 xmlFreeNs(tmpns);
7964 return (-1);
7965 }
7966 *retNs = tmpns;
7967 }
7968 return (0);
7969}
7970
7971/*
7972* xmlDOMWrapReconcileNamespaces:
Daniel Veillard304e78c2005-07-03 16:19:41 +00007973* @ctxt: DOM wrapper context, unused at the moment
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007974* @elem: the element-node
7975* @options: option flags
7976*
7977* Ensures that ns-references point to ns-decls hold on element-nodes.
7978* Ensures that the tree is namespace wellformed by creating additional
7979* ns-decls where needed. Note that, since prefixes of already existent
7980* ns-decls can be shadowed by this process, it could break QNames in
7981* attribute values or element content.
Kasimier T. Buchcik017264f2005-06-27 13:45:24 +00007982* WARNING: This function is in a experimental state.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007983*
7984* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
7985*/
7986int
7987xmlDOMWrapReconcileNamespaces(xmlDOMWrapCtxtPtr ctxt ATTRIBUTE_UNUSED,
7988 xmlNodePtr elem,
7989 int options ATTRIBUTE_UNUSED)
7990{
7991 int depth = -1, adoptns = 0, parnsdone = 0;
7992 xmlNsPtr ns;
7993 xmlDocPtr doc;
7994 xmlNodePtr cur, curElem = NULL;
7995 xmlNsMapItemPtr nsMap = NULL, topmi = NULL, mi;
7996 /* @ancestorsOnly should be set by an option flag. */
7997 int ancestorsOnly = 0;
7998
7999 if ((elem == NULL) || (elem->doc == NULL) ||
8000 (elem->type != XML_ELEMENT_NODE))
8001 return (-1);
8002
8003 doc = elem->doc;
8004 cur = elem;
8005 do {
8006 switch (cur->type) {
8007 case XML_ELEMENT_NODE:
8008 adoptns = 1;
8009 curElem = cur;
8010 depth++;
8011 /*
8012 * Namespace declarations.
8013 */
8014 if (cur->nsDef != NULL) {
8015 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
8016 if (! parnsdone) {
8017 if ((elem->parent) &&
8018 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8019 /*
8020 * Gather ancestor in-scope ns-decls.
8021 */
8022 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8023 elem->parent) == -1)
8024 goto internal_error;
8025 if (nsMap != NULL)
8026 topmi = nsMap->prev;
8027 }
8028 parnsdone = 1;
8029 }
8030 /*
8031 * Skip ns-references handling if the referenced
8032 * ns-decl is declared on the same element.
8033 */
8034 if ((cur->ns != NULL) && adoptns && (cur->ns == ns))
8035 adoptns = 0;
8036 /*
8037 * Does it shadow any ns-decl?
8038 */
8039 if (nsMap) {
8040 for (mi = nsMap; mi != topmi->next; mi = mi->next) {
8041 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8042 (mi->shadowDepth == -1) &&
8043 ((ns->prefix == mi->newNs->prefix) ||
8044 xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
8045
8046 mi->shadowDepth = depth;
8047 }
8048 }
8049 }
8050 /*
8051 * Push mapping.
8052 */
8053 if (xmlDOMWrapNSNormAddNsMapItem(&nsMap, &topmi, ns, ns,
8054 depth) == NULL)
8055 goto internal_error;
8056 }
8057 }
8058 if (! adoptns)
8059 goto ns_end;
8060
8061 /* No break on purpose. */
8062 case XML_ATTRIBUTE_NODE:
8063 if (cur->ns == NULL)
8064 goto ns_end;
8065 if (! parnsdone) {
8066 if ((elem->parent) &&
8067 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8068 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8069 elem->parent) == -1)
8070 goto internal_error;
8071 if (nsMap != NULL)
8072 topmi = nsMap->prev;
8073 }
8074 parnsdone = 1;
8075 }
8076 /*
8077 * Adopt ns-references.
8078 */
8079 if (nsMap != NULL) {
8080 /*
8081 * Search for a mapping.
8082 */
8083 for (mi = nsMap; mi != topmi->next; mi = mi->next) {
8084 if ((mi->shadowDepth == -1) &&
8085 (cur->ns == mi->oldNs)) {
8086
8087 cur->ns = mi->newNs;
8088 goto ns_end;
8089 }
8090 }
8091 }
8092 /*
8093 * Aquire a normalized ns-decl and add it to the map.
8094 */
8095 if (xmlDOMWrapNSNormAquireNormalizedNs(doc, curElem,
8096 cur->ns, &ns,
8097 &nsMap, &topmi, depth,
8098 ancestorsOnly,
8099 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
8100 goto internal_error;
8101 cur->ns = ns;
8102
8103ns_end:
8104 if ((cur->type == XML_ELEMENT_NODE) &&
8105 (cur->properties != NULL)) {
8106 /*
8107 * Process attributes.
8108 */
8109 cur = (xmlNodePtr) cur->properties;
8110 continue;
8111 }
8112 break;
8113 default:
8114 goto next_sibling;
8115 }
8116 if ((cur->type == XML_ELEMENT_NODE) &&
8117 (cur->children != NULL)) {
8118 /*
8119 * Process content of element-nodes only.
8120 */
8121 cur = cur->children;
8122 continue;
8123 }
8124next_sibling:
8125 if (cur == elem)
8126 break;
8127 if (cur->type == XML_ELEMENT_NODE) {
8128 if (nsMap != NULL) {
8129 /*
8130 * Pop mappings.
8131 */
8132 while ((topmi->depth >= 0) && (topmi->depth >= depth))
8133 topmi = topmi->prev;
8134 /*
8135 * Unshadow.
8136 */
8137 for (mi = nsMap; mi != topmi->next; mi = mi->next)
8138 if (mi->shadowDepth >= depth)
8139 mi->shadowDepth = -1;
8140 }
8141 depth--;
8142 }
8143 if (cur->next != NULL)
8144 cur = cur->next;
8145 else {
8146 cur = cur->parent;
8147 goto next_sibling;
8148 }
8149 } while (cur != NULL);
8150
8151 if (nsMap != NULL)
8152 xmlDOMWrapNSNormFreeNsMap(nsMap);
8153 return (0);
8154internal_error:
8155 if (nsMap != NULL)
8156 xmlDOMWrapNSNormFreeNsMap(nsMap);
8157 return (-1);
8158}
8159
8160/*
8161* xmlDOMWrapAdoptBranch:
8162* @ctxt: the optional context for custom processing
8163* @sourceDoc: the optional sourceDoc
8164* @node: the element-node to start with
8165* @destDoc: the destination doc for adoption
8166* @parent: the optional new parent of @node in @destDoc
8167* @options: option flags
8168*
8169* Ensures that ns-references point to @destDoc: either to
8170* elements->nsDef entries if @destParent is given, or to
8171* @destDoc->oldNs otherwise.
8172* If @destParent is given, it ensures that the tree is namespace
8173* wellformed by creating additional ns-decls where needed.
8174* Note that, since prefixes of already existent ns-decls can be
8175* shadowed by this process, it could break QNames in attribute
8176* values or element content.
8177*
8178* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8179*/
8180static int
8181xmlDOMWrapAdoptBranch(xmlDOMWrapCtxtPtr ctxt,
8182 xmlDocPtr sourceDoc,
8183 xmlNodePtr node,
8184 xmlDocPtr destDoc,
8185 xmlNodePtr destParent,
8186 int options ATTRIBUTE_UNUSED)
8187{
8188 int ret = 0;
8189 xmlNodePtr cur, curElem = NULL;
8190 xmlNsMapItemPtr nsMap = NULL, topmi = NULL, mi;
8191 xmlNsPtr ns;
8192 int depth = -1, adoptStr = 1;
8193 /* gather @parent's ns-decls. */
8194 int parnsdone = 0;
8195 /* @ancestorsOnly should be set per option. */
8196 int ancestorsOnly = 0;
8197
8198 /*
8199 * Optimize string adoption for equal or none dicts.
8200 */
8201 if ((sourceDoc != NULL) &&
8202 (sourceDoc->dict == destDoc->dict))
8203 adoptStr = 0;
8204 else
8205 adoptStr = 1;
8206
8207 cur = node;
8208 while (cur != NULL) {
8209 if (cur->doc != sourceDoc) {
8210 /*
8211 * We'll assume XIncluded nodes if the doc differs.
8212 * TODO: Do we need to reconciliate XIncluded nodes?
8213 * This here skips XIncluded nodes and tries to handle
8214 * broken sequences.
8215 */
8216 if (cur->next == NULL)
8217 goto leave_node;
8218 do {
8219 cur = cur->next;
8220 if ((cur->type == XML_XINCLUDE_END) ||
8221 (cur->doc == node->doc))
8222 break;
8223 } while (cur->next != NULL);
8224
8225 if (cur->doc != node->doc)
8226 goto leave_node;
8227 }
8228 cur->doc = destDoc;
8229 switch (cur->type) {
8230 case XML_XINCLUDE_START:
8231 case XML_XINCLUDE_END:
8232 /*
8233 * TODO
8234 */
8235 return (-1);
8236 case XML_ELEMENT_NODE:
8237 curElem = cur;
8238 depth++;
8239 /*
8240 * Namespace declarations.
8241 */
8242 if ((ctxt == NULL) && (cur->nsDef != NULL)) {
8243 if (! parnsdone) {
8244 if (destParent && (ctxt == NULL)) {
8245 /*
8246 * Gather @parent's in-scope ns-decls.
8247 */
8248 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8249 destParent) == -1)
8250 goto internal_error;
8251 if (nsMap != NULL)
8252 topmi = nsMap->prev;
8253 }
8254 parnsdone = 1;
8255 }
8256 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
8257 /*
8258 * ns->prefix and ns->href seem not to be in the dict.
8259 * XML_TREE_ADOPT_STR(ns->prefix)
8260 * XML_TREE_ADOPT_STR(ns->href)
8261 */
8262 /*
8263 * Does it shadow any ns-decl?
8264 */
8265 if (nsMap) {
8266 for (mi = nsMap; mi != topmi->next;
8267 mi = mi->next) {
8268 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8269 (mi->shadowDepth == -1) &&
8270 ((ns->prefix == mi->newNs->prefix) ||
8271 xmlStrEqual(ns->prefix,
8272 mi->newNs->prefix))) {
8273
8274 mi->shadowDepth = depth;
8275 }
8276 }
8277 }
8278 /*
8279 * Push mapping.
8280 */
8281 if (xmlDOMWrapNSNormAddNsMapItem(&nsMap, &topmi,
8282 ns, ns, depth) == NULL)
8283 goto internal_error;
8284 }
8285 }
8286 /* No break on purpose. */
8287 case XML_ATTRIBUTE_NODE:
8288
8289 if (cur->ns == NULL)
8290 goto ns_end;
8291 if (! parnsdone) {
8292 if (destParent && (ctxt == NULL)) {
8293 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8294 destParent) == -1)
8295 goto internal_error;
8296 if (nsMap != NULL)
8297 topmi = nsMap->prev;
8298 }
8299 parnsdone = 1;
8300 }
8301 /*
8302 * Adopt ns-references.
8303 */
8304 if (nsMap != NULL) {
8305 /*
8306 * Search for a mapping.
8307 */
8308 for (mi = nsMap; mi != topmi->next; mi = mi->next) {
8309 if ((mi->shadowDepth == -1) &&
8310 (cur->ns == mi->oldNs)) {
8311
8312 cur->ns = mi->newNs;
8313 goto ns_end;
8314 }
8315 }
8316 }
8317 /*
8318 * Start searching for an in-scope ns-decl.
8319 */
8320 if (ctxt != NULL) {
8321 /*
8322 * User-defined behaviour.
8323 */
8324#if 0
8325 ctxt->aquireNsDecl(ctxt, cur->ns, &ns);
8326#endif
8327 /*
8328 * Insert mapping if ns is available; it's the users fault
8329 * if not.
8330 */
8331 if (xmlDOMWrapNSNormAddNsMapItem(&nsMap, &topmi,
8332 ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
8333 goto internal_error;
8334 cur->ns = ns;
8335 } else {
8336 /*
8337 * Aquire a normalized ns-decl and add it to the map.
8338 */
8339 if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc,
8340 /* ns-decls on curElem or on destDoc->oldNs */
8341 destParent ? curElem : NULL,
8342 cur->ns, &ns,
8343 &nsMap, &topmi, depth,
8344 ancestorsOnly,
8345 /* ns-decls must be prefixed for attributes. */
8346 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
8347 goto internal_error;
8348 cur->ns = ns;
8349 }
8350ns_end:
8351 /*
8352 * Further node properties.
8353 * TODO: Is this all?
8354 */
8355 XML_TREE_ADOPT_STR(cur->name)
8356 if (cur->type == XML_ELEMENT_NODE) {
8357 cur->psvi = NULL;
8358 cur->line = 0;
8359 cur->extra = 0;
8360 /*
8361 * Walk attributes.
8362 */
8363 if (cur->properties != NULL) {
8364 /*
8365 * Process first attribute node.
8366 */
8367 cur = (xmlNodePtr) cur->properties;
8368 continue;
8369 }
8370 } else {
8371 /*
8372 * Attributes.
8373 */
8374 if ((sourceDoc != NULL) &&
8375 (((xmlAttrPtr) cur)->atype == XML_ATTRIBUTE_ID))
8376 xmlRemoveID(sourceDoc, (xmlAttrPtr) cur);
8377 ((xmlAttrPtr) cur)->atype = 0;
8378 ((xmlAttrPtr) cur)->psvi = NULL;
8379 }
8380 break;
8381 case XML_TEXT_NODE:
8382 case XML_CDATA_SECTION_NODE:
8383 /*
8384 * This puts the content in the dest dict, only if
8385 * it was previously in the source dict.
8386 */
8387 XML_TREE_ADOPT_STR_2(cur->content)
8388 goto leave_node;
8389 case XML_ENTITY_REF_NODE:
8390 /*
8391 * Remove reference to the entitity-node.
8392 */
8393 cur->content = NULL;
8394 cur->children = NULL;
8395 cur->last = NULL;
8396 if ((destDoc->intSubset) || (destDoc->extSubset)) {
8397 xmlEntityPtr ent;
8398 /*
8399 * Assign new entity-node if available.
8400 */
8401 ent = xmlGetDocEntity(destDoc, cur->name);
8402 if (ent != NULL) {
8403 cur->content = ent->content;
8404 cur->children = (xmlNodePtr) ent;
8405 cur->last = (xmlNodePtr) ent;
8406 }
8407 }
8408 goto leave_node;
8409 case XML_PI_NODE:
8410 XML_TREE_ADOPT_STR(cur->name)
8411 XML_TREE_ADOPT_STR_2(cur->content)
8412 break;
8413 case XML_COMMENT_NODE:
8414 break;
8415 default:
8416 goto internal_error;
8417 }
8418 /*
8419 * Walk the tree.
8420 */
8421 if (cur->children != NULL) {
8422 cur = cur->children;
8423 continue;
8424 }
8425
8426leave_node:
8427 if (cur == node)
8428 break;
8429 if ((cur->type == XML_ELEMENT_NODE) ||
8430 (cur->type == XML_XINCLUDE_START) ||
8431 (cur->type == XML_XINCLUDE_END)) {
8432 /*
8433 * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
8434 */
8435 if (nsMap != NULL) {
8436 /*
8437 * Pop mappings.
8438 */
8439 while (topmi->depth >= depth)
8440 topmi = topmi->prev;
8441 /*
8442 * Unshadow.
8443 */
8444 for (mi = nsMap; mi != topmi->next; mi = mi->next)
8445 if (mi->shadowDepth >= depth)
8446 mi->shadowDepth = -1;
8447 }
8448 depth--;
8449 }
8450 if (cur->next != NULL)
8451 cur = cur->next;
8452 else {
8453 cur = cur->parent;
8454 goto leave_node;
8455 }
8456 }
8457 /*
8458 * Cleanup.
8459 */
8460 if (nsMap != NULL)
8461 xmlDOMWrapNSNormFreeNsMap(nsMap);
8462 return (ret);
8463internal_error:
8464 if (nsMap != NULL)
8465 xmlDOMWrapNSNormFreeNsMap(nsMap);
8466 return (-1);
8467}
8468
8469/*
8470* xmlDOMWrapAdoptAttr:
8471* @ctxt: the optional context for custom processing
8472* @sourceDoc: the optional source document of attr
8473* @attr: the attribute-node to be adopted
8474* @destDoc: the destination doc for adoption
8475* @destParent: the optional new parent of @attr in @destDoc
8476* @options: option flags
8477*
8478* @attr is adopted by @destDoc.
8479* Ensures that ns-references point to @destDoc: either to
8480* elements->nsDef entries if @destParent is given, or to
8481* @destDoc->oldNs otherwise.
8482*
8483* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8484*/
8485static int
8486xmlDOMWrapAdoptAttr(xmlDOMWrapCtxtPtr ctxt,
8487 xmlDocPtr sourceDoc,
8488 xmlAttrPtr attr,
8489 xmlDocPtr destDoc,
8490 xmlNodePtr destParent,
8491 int options ATTRIBUTE_UNUSED)
8492{
8493 xmlNodePtr cur;
8494 int adoptStr = 1;
8495
8496 if ((attr == NULL) || (destDoc == NULL))
8497 return (-1);
8498
8499 attr->doc = destDoc;
8500 if (attr->ns != NULL) {
8501 xmlNsPtr ns = NULL;
8502
8503 if (ctxt != NULL) {
8504 /* TODO: User defined. */
8505 }
8506 /* XML Namespace. */
8507 if ((attr->ns->prefix[0] == 'x') && (attr->ns->prefix[1] == 'm') &&
8508 (attr->ns->prefix[2] == 'l') && (attr->ns->prefix[3] == 0)) {
8509 ns = xmlTreeEnsureXMLDecl(destDoc);
8510 } else if (destParent == NULL) {
8511 /*
8512 * Store in @destDoc->oldNs.
8513 */
8514 ns = xmlDOMWrapStoreNs(destDoc, attr->ns->href, attr->ns->prefix);
8515 } else {
8516 /*
8517 * Declare on @destParent.
8518 */
8519 if (xmlSearchNsByHrefStrict(destDoc, destParent, attr->ns->href,
8520 &ns, 1) == -1)
8521 goto internal_error;
8522 if (ns == NULL) {
8523 ns = xmlDOMWrapNSNormDeclareNsForced(destDoc, destParent,
8524 attr->ns->href, attr->ns->prefix, 1);
8525 }
8526 }
8527 if (ns == NULL)
8528 goto internal_error;
8529 attr->ns = ns;
8530 }
8531
8532 XML_TREE_ADOPT_STR(attr->name);
8533 attr->atype = 0;
8534 attr->psvi = NULL;
8535 /*
8536 * Walk content.
8537 */
8538 if (attr->children == NULL)
8539 return (0);
8540 cur = attr->children;
8541 while (cur != NULL) {
8542 cur->doc = destDoc;
8543 switch (cur->type) {
8544 case XML_TEXT_NODE:
8545 case XML_CDATA_SECTION_NODE:
8546 XML_TREE_ADOPT_STR_2(cur->content)
8547 break;
8548 case XML_ENTITY_REF_NODE:
8549 /*
8550 * Remove reference to the entitity-node.
8551 */
8552 cur->content = NULL;
8553 cur->children = NULL;
8554 cur->last = NULL;
8555 if ((destDoc->intSubset) || (destDoc->extSubset)) {
8556 xmlEntityPtr ent;
8557 /*
8558 * Assign new entity-node if available.
8559 */
8560 ent = xmlGetDocEntity(destDoc, cur->name);
8561 if (ent != NULL) {
8562 cur->content = ent->content;
8563 cur->children = (xmlNodePtr) ent;
8564 cur->last = (xmlNodePtr) ent;
8565 }
8566 }
8567 break;
8568 default:
8569 break;
8570 }
8571 if (cur->children != NULL) {
8572 cur = cur->children;
8573 continue;
8574 }
8575next_sibling:
8576 if (cur == (xmlNodePtr) attr)
8577 break;
8578 if (cur->next != NULL)
8579 cur = cur->next;
8580 else {
8581 cur = cur->parent;
8582 goto next_sibling;
8583 }
8584 }
8585 return (0);
8586internal_error:
8587 return (-1);
8588}
8589
8590/*
8591* xmlDOMWrapAdoptNode:
8592* @ctxt: the optional context for custom processing
8593* @sourceDoc: the optional sourceDoc
8594* @node: the node to start with
8595* @destDoc: the destination doc
Kasimier T. Buchcik4d9c9482005-06-27 15:04:46 +00008596* @destParent: the optional new parent of @node in @destDoc
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008597* @options: option flags
8598*
8599* Ensures that ns-references point to @destDoc: either to
8600* elements->nsDef entries if @destParent is given, or to
8601* @destDoc->oldNs otherwise.
8602* If @destParent is given, it ensures that the tree is namespace
8603* wellformed by creating additional ns-decls where needed.
8604* Note that, since prefixes of already existent ns-decls can be
8605* shadowed by this process, it could break QNames in attribute
8606* values or element content.
Kasimier T. Buchcik017264f2005-06-27 13:45:24 +00008607* WARNING: This function is in a experimental state.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008608*
8609* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8610*/
8611int
8612xmlDOMWrapAdoptNode(xmlDOMWrapCtxtPtr ctxt,
8613 xmlDocPtr sourceDoc,
8614 xmlNodePtr node,
8615 xmlDocPtr destDoc,
8616 xmlNodePtr destParent,
8617 int options)
8618{
8619 if ((node == NULL) || (destDoc == NULL) ||
8620 ((destParent != NULL) && (destParent->doc != destDoc)))
8621 return(-1);
8622 /*
8623 * Check node->doc sanity.
8624 */
8625 if ((node->doc != NULL) && (sourceDoc != NULL) &&
8626 (node->doc != sourceDoc)) {
8627 /*
8628 * Might be an XIncluded node.
8629 */
8630 return (-1);
8631 }
8632 if (sourceDoc == NULL)
8633 sourceDoc = node->doc;
8634 if (sourceDoc == destDoc)
8635 return (-1);
8636 switch (node->type) {
8637 case XML_ELEMENT_NODE:
8638 case XML_ATTRIBUTE_NODE:
8639 case XML_TEXT_NODE:
8640 case XML_CDATA_SECTION_NODE:
8641 case XML_ENTITY_REF_NODE:
8642 case XML_PI_NODE:
8643 case XML_COMMENT_NODE:
8644 break;
8645 case XML_DOCUMENT_FRAG_NODE:
8646 return (2);
8647 default:
8648 return (1);
8649 }
8650 /*
8651 * Unlink only if @node was not already added to @destParent.
8652 */
8653 if ((node->parent != NULL) && (destParent != node->parent))
8654 xmlUnlinkNode(node);
8655
8656 if (node->type == XML_ELEMENT_NODE) {
8657 return (xmlDOMWrapAdoptBranch(ctxt, sourceDoc, node,
8658 destDoc, destParent, options));
8659 } else if (node->type == XML_ATTRIBUTE_NODE) {
8660 return (xmlDOMWrapAdoptAttr(ctxt, sourceDoc,
8661 (xmlAttrPtr) node, destDoc, destParent, options));
8662 } else {
8663 xmlNodePtr cur = node;
8664 int adoptStr = 1;
8665
8666 cur->doc = destDoc;
8667 /*
8668 * Optimize string adoption.
8669 */
8670 if ((sourceDoc != NULL) &&
8671 (sourceDoc->dict == destDoc->dict))
8672 adoptStr = 0;
8673 switch (node->type) {
8674 case XML_TEXT_NODE:
8675 case XML_CDATA_SECTION_NODE:
8676 XML_TREE_ADOPT_STR_2(node->content)
8677 break;
8678 case XML_ENTITY_REF_NODE:
8679 /*
8680 * Remove reference to the entitity-node.
8681 */
8682 node->content = NULL;
8683 node->children = NULL;
8684 node->last = NULL;
8685 if ((destDoc->intSubset) || (destDoc->extSubset)) {
8686 xmlEntityPtr ent;
8687 /*
8688 * Assign new entity-node if available.
8689 */
8690 ent = xmlGetDocEntity(destDoc, node->name);
8691 if (ent != NULL) {
8692 node->content = ent->content;
8693 node->children = (xmlNodePtr) ent;
8694 node->last = (xmlNodePtr) ent;
8695 }
8696 }
8697 XML_TREE_ADOPT_STR(node->name)
8698 break;
Daniel Veillard7e21fd12005-07-03 21:44:07 +00008699 case XML_PI_NODE: {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008700 XML_TREE_ADOPT_STR(node->name)
8701 XML_TREE_ADOPT_STR_2(node->content)
8702 break;
Daniel Veillard7e21fd12005-07-03 21:44:07 +00008703 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008704 default:
8705 break;
8706 }
8707 }
8708 return (0);
8709}
8710
8711
Daniel Veillard5d4644e2005-04-01 13:11:58 +00008712#define bottom_tree
8713#include "elfgcchack.h"