blob: 3e5378fccaea2f347ce1331def0b6e7ae6bf0e45 [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;
Daniel Veillard73da77e2005-08-24 14:05:37 +00004141 if ((node->type == XML_ELEMENT_NODE) ||
4142 (node->type == XML_TEXT_NODE) ||
4143 (node->type == XML_COMMENT_NODE) ||
4144 (node->type == XML_PI_NODE))
Daniel Veillard3e35f8e2003-10-21 00:05:38 +00004145 result = (long) node->line;
Daniel Veillard8faa7832001-11-26 15:58:08 +00004146 else if ((node->prev != NULL) &&
4147 ((node->prev->type == XML_ELEMENT_NODE) ||
Daniel Veillard73da77e2005-08-24 14:05:37 +00004148 (node->prev->type == XML_TEXT_NODE) ||
4149 (node->prev->type == XML_COMMENT_NODE) ||
4150 (node->prev->type == XML_PI_NODE)))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004151 result = xmlGetLineNo(node->prev);
4152 else if ((node->parent != NULL) &&
Daniel Veillard73da77e2005-08-24 14:05:37 +00004153 (node->parent->type == XML_ELEMENT_NODE))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004154 result = xmlGetLineNo(node->parent);
4155
4156 return result;
4157}
4158
Daniel Veillard2156d432004-03-04 15:59:36 +00004159#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED)
Daniel Veillard8faa7832001-11-26 15:58:08 +00004160/**
4161 * xmlGetNodePath:
4162 * @node: a node
4163 *
4164 * Build a structure based Path for the given node
4165 *
4166 * Returns the new path or NULL in case of error. The caller must free
4167 * the returned string
4168 */
4169xmlChar *
4170xmlGetNodePath(xmlNodePtr node)
4171{
4172 xmlNodePtr cur, tmp, next;
4173 xmlChar *buffer = NULL, *temp;
4174 size_t buf_len;
4175 xmlChar *buf;
Daniel Veillard96c3a3b2002-10-14 15:39:04 +00004176 const char *sep;
Daniel Veillard8faa7832001-11-26 15:58:08 +00004177 const char *name;
4178 char nametemp[100];
4179 int occur = 0;
4180
4181 if (node == NULL)
4182 return (NULL);
4183
4184 buf_len = 500;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004185 buffer = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004186 if (buffer == NULL) {
4187 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004188 return (NULL);
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004189 }
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004190 buf = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
Daniel Veillard8faa7832001-11-26 15:58:08 +00004191 if (buf == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004192 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004193 xmlFree(buffer);
4194 return (NULL);
4195 }
4196
4197 buffer[0] = 0;
4198 cur = node;
4199 do {
4200 name = "";
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004201 sep = "?";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004202 occur = 0;
4203 if ((cur->type == XML_DOCUMENT_NODE) ||
4204 (cur->type == XML_HTML_DOCUMENT_NODE)) {
4205 if (buffer[0] == '/')
4206 break;
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004207 sep = "/";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004208 next = NULL;
4209 } else if (cur->type == XML_ELEMENT_NODE) {
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004210 sep = "/";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004211 name = (const char *) cur->name;
4212 if (cur->ns) {
William M. Brack84d83e32003-12-23 03:45:17 +00004213 if (cur->ns->prefix != NULL)
William M. Brack13dfa872004-09-18 04:52:08 +00004214 snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
4215 (char *)cur->ns->prefix, (char *)cur->name);
William M. Brack84d83e32003-12-23 03:45:17 +00004216 else
William M. Brack13dfa872004-09-18 04:52:08 +00004217 snprintf(nametemp, sizeof(nametemp) - 1, "%s",
4218 (char *)cur->name);
Daniel Veillard8faa7832001-11-26 15:58:08 +00004219 nametemp[sizeof(nametemp) - 1] = 0;
4220 name = nametemp;
4221 }
4222 next = cur->parent;
4223
4224 /*
4225 * Thumbler index computation
Daniel Veillardc00cda82003-04-07 10:22:39 +00004226 * TODO: the ocurence test seems bogus for namespaced names
Daniel Veillard8faa7832001-11-26 15:58:08 +00004227 */
4228 tmp = cur->prev;
4229 while (tmp != NULL) {
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00004230 if ((tmp->type == XML_ELEMENT_NODE) &&
Daniel Veillard0996a162005-02-05 14:00:10 +00004231 (xmlStrEqual(cur->name, tmp->name)) &&
4232 ((tmp->ns == cur->ns) ||
4233 ((tmp->ns != NULL) && (cur->ns != NULL) &&
4234 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004235 occur++;
4236 tmp = tmp->prev;
4237 }
4238 if (occur == 0) {
4239 tmp = cur->next;
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004240 while (tmp != NULL && occur == 0) {
4241 if ((tmp->type == XML_ELEMENT_NODE) &&
Daniel Veillard0996a162005-02-05 14:00:10 +00004242 (xmlStrEqual(cur->name, tmp->name)) &&
4243 ((tmp->ns == cur->ns) ||
4244 ((tmp->ns != NULL) && (cur->ns != NULL) &&
4245 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004246 occur++;
4247 tmp = tmp->next;
4248 }
4249 if (occur != 0)
4250 occur = 1;
4251 } else
4252 occur++;
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004253 } else if (cur->type == XML_COMMENT_NODE) {
4254 sep = "/";
4255 name = "comment()";
4256 next = cur->parent;
4257
4258 /*
4259 * Thumbler index computation
4260 */
4261 tmp = cur->prev;
4262 while (tmp != NULL) {
4263 if (tmp->type == XML_COMMENT_NODE)
4264 occur++;
4265 tmp = tmp->prev;
4266 }
4267 if (occur == 0) {
4268 tmp = cur->next;
4269 while (tmp != NULL && occur == 0) {
4270 if (tmp->type == XML_COMMENT_NODE)
4271 occur++;
4272 tmp = tmp->next;
4273 }
4274 if (occur != 0)
4275 occur = 1;
4276 } else
4277 occur++;
4278 } else if ((cur->type == XML_TEXT_NODE) ||
4279 (cur->type == XML_CDATA_SECTION_NODE)) {
4280 sep = "/";
4281 name = "text()";
4282 next = cur->parent;
4283
4284 /*
4285 * Thumbler index computation
4286 */
4287 tmp = cur->prev;
4288 while (tmp != NULL) {
4289 if ((cur->type == XML_TEXT_NODE) ||
4290 (cur->type == XML_CDATA_SECTION_NODE))
4291 occur++;
4292 tmp = tmp->prev;
4293 }
4294 if (occur == 0) {
4295 tmp = cur->next;
4296 while (tmp != NULL && occur == 0) {
Daniel Veillard1f8658a2004-08-14 21:46:31 +00004297 if ((tmp->type == XML_TEXT_NODE) ||
4298 (tmp->type == XML_CDATA_SECTION_NODE))
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004299 occur++;
4300 tmp = tmp->next;
4301 }
4302 if (occur != 0)
4303 occur = 1;
4304 } else
4305 occur++;
4306 } else if (cur->type == XML_PI_NODE) {
4307 sep = "/";
4308 snprintf(nametemp, sizeof(nametemp) - 1,
William M. Brack13dfa872004-09-18 04:52:08 +00004309 "processing-instruction('%s')", (char *)cur->name);
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004310 nametemp[sizeof(nametemp) - 1] = 0;
4311 name = nametemp;
4312
4313 next = cur->parent;
4314
4315 /*
4316 * Thumbler index computation
4317 */
4318 tmp = cur->prev;
4319 while (tmp != NULL) {
4320 if ((tmp->type == XML_PI_NODE) &&
4321 (xmlStrEqual(cur->name, tmp->name)))
4322 occur++;
4323 tmp = tmp->prev;
4324 }
4325 if (occur == 0) {
4326 tmp = cur->next;
4327 while (tmp != NULL && occur == 0) {
4328 if ((tmp->type == XML_PI_NODE) &&
4329 (xmlStrEqual(cur->name, tmp->name)))
4330 occur++;
4331 tmp = tmp->next;
4332 }
4333 if (occur != 0)
4334 occur = 1;
4335 } else
4336 occur++;
4337
Daniel Veillard8faa7832001-11-26 15:58:08 +00004338 } else if (cur->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004339 sep = "/@";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004340 name = (const char *) (((xmlAttrPtr) cur)->name);
Daniel Veillard365c8062005-07-19 11:31:55 +00004341 if (cur->ns) {
4342 if (cur->ns->prefix != NULL)
4343 snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
4344 (char *)cur->ns->prefix, (char *)cur->name);
4345 else
4346 snprintf(nametemp, sizeof(nametemp) - 1, "%s",
4347 (char *)cur->name);
4348 nametemp[sizeof(nametemp) - 1] = 0;
4349 name = nametemp;
4350 }
Daniel Veillard8faa7832001-11-26 15:58:08 +00004351 next = ((xmlAttrPtr) cur)->parent;
4352 } else {
4353 next = cur->parent;
4354 }
4355
4356 /*
4357 * Make sure there is enough room
4358 */
4359 if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) {
4360 buf_len =
4361 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20;
4362 temp = (xmlChar *) xmlRealloc(buffer, buf_len);
4363 if (temp == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004364 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004365 xmlFree(buf);
4366 xmlFree(buffer);
4367 return (NULL);
4368 }
4369 buffer = temp;
4370 temp = (xmlChar *) xmlRealloc(buf, buf_len);
4371 if (temp == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004372 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004373 xmlFree(buf);
4374 xmlFree(buffer);
4375 return (NULL);
4376 }
4377 buf = temp;
4378 }
4379 if (occur == 0)
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004380 snprintf((char *) buf, buf_len, "%s%s%s",
Daniel Veillard8faa7832001-11-26 15:58:08 +00004381 sep, name, (char *) buffer);
4382 else
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004383 snprintf((char *) buf, buf_len, "%s%s[%d]%s",
Daniel Veillard8faa7832001-11-26 15:58:08 +00004384 sep, name, occur, (char *) buffer);
William M. Brack13dfa872004-09-18 04:52:08 +00004385 snprintf((char *) buffer, buf_len, "%s", (char *)buf);
Daniel Veillard8faa7832001-11-26 15:58:08 +00004386 cur = next;
4387 } while (cur != NULL);
4388 xmlFree(buf);
4389 return (buffer);
4390}
Daniel Veillard652327a2003-09-29 18:02:38 +00004391#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard8faa7832001-11-26 15:58:08 +00004392
4393/**
Owen Taylor3473f882001-02-23 17:55:21 +00004394 * xmlDocGetRootElement:
4395 * @doc: the document
4396 *
4397 * Get the root element of the document (doc->children is a list
4398 * containing possibly comments, PIs, etc ...).
4399 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004400 * Returns the #xmlNodePtr for the root or NULL
Owen Taylor3473f882001-02-23 17:55:21 +00004401 */
4402xmlNodePtr
4403xmlDocGetRootElement(xmlDocPtr doc) {
4404 xmlNodePtr ret;
4405
4406 if (doc == NULL) return(NULL);
4407 ret = doc->children;
4408 while (ret != NULL) {
4409 if (ret->type == XML_ELEMENT_NODE)
4410 return(ret);
4411 ret = ret->next;
4412 }
4413 return(ret);
4414}
4415
Daniel Veillard2156d432004-03-04 15:59:36 +00004416#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004417/**
4418 * xmlDocSetRootElement:
4419 * @doc: the document
4420 * @root: the new document root element
4421 *
4422 * Set the root element of the document (doc->children is a list
4423 * containing possibly comments, PIs, etc ...).
4424 *
4425 * Returns the old root element if any was found
4426 */
4427xmlNodePtr
4428xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) {
4429 xmlNodePtr old = NULL;
4430
4431 if (doc == NULL) return(NULL);
Daniel Veillardc575b992002-02-08 13:28:40 +00004432 if (root == NULL)
4433 return(NULL);
4434 xmlUnlinkNode(root);
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00004435 xmlSetTreeDoc(root, doc);
Daniel Veillardc575b992002-02-08 13:28:40 +00004436 root->parent = (xmlNodePtr) doc;
Owen Taylor3473f882001-02-23 17:55:21 +00004437 old = doc->children;
4438 while (old != NULL) {
4439 if (old->type == XML_ELEMENT_NODE)
4440 break;
4441 old = old->next;
4442 }
4443 if (old == NULL) {
4444 if (doc->children == NULL) {
4445 doc->children = root;
4446 doc->last = root;
4447 } else {
4448 xmlAddSibling(doc->children, root);
4449 }
4450 } else {
4451 xmlReplaceNode(old, root);
4452 }
4453 return(old);
4454}
Daniel Veillard2156d432004-03-04 15:59:36 +00004455#endif
Owen Taylor3473f882001-02-23 17:55:21 +00004456
Daniel Veillard2156d432004-03-04 15:59:36 +00004457#if defined(LIBXML_TREE_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004458/**
4459 * xmlNodeSetLang:
4460 * @cur: the node being changed
Daniel Veillardd1640922001-12-17 15:30:10 +00004461 * @lang: the language description
Owen Taylor3473f882001-02-23 17:55:21 +00004462 *
4463 * Set the language of a node, i.e. the values of the xml:lang
4464 * attribute.
4465 */
4466void
4467xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004468 xmlNsPtr ns;
4469
Owen Taylor3473f882001-02-23 17:55:21 +00004470 if (cur == NULL) return;
4471 switch(cur->type) {
4472 case XML_TEXT_NODE:
4473 case XML_CDATA_SECTION_NODE:
4474 case XML_COMMENT_NODE:
4475 case XML_DOCUMENT_NODE:
4476 case XML_DOCUMENT_TYPE_NODE:
4477 case XML_DOCUMENT_FRAG_NODE:
4478 case XML_NOTATION_NODE:
4479 case XML_HTML_DOCUMENT_NODE:
4480 case XML_DTD_NODE:
4481 case XML_ELEMENT_DECL:
4482 case XML_ATTRIBUTE_DECL:
4483 case XML_ENTITY_DECL:
4484 case XML_PI_NODE:
4485 case XML_ENTITY_REF_NODE:
4486 case XML_ENTITY_NODE:
4487 case XML_NAMESPACE_DECL:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004488#ifdef LIBXML_DOCB_ENABLED
4489 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004490#endif
4491 case XML_XINCLUDE_START:
4492 case XML_XINCLUDE_END:
4493 return;
4494 case XML_ELEMENT_NODE:
4495 case XML_ATTRIBUTE_NODE:
4496 break;
4497 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004498 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4499 if (ns == NULL)
4500 return;
4501 xmlSetNsProp(cur, ns, BAD_CAST "lang", lang);
Owen Taylor3473f882001-02-23 17:55:21 +00004502}
Daniel Veillard652327a2003-09-29 18:02:38 +00004503#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004504
4505/**
4506 * xmlNodeGetLang:
4507 * @cur: the node being checked
4508 *
4509 * Searches the language of a node, i.e. the values of the xml:lang
4510 * attribute or the one carried by the nearest ancestor.
4511 *
4512 * Returns a pointer to the lang value, or NULL if not found
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004513 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00004514 */
4515xmlChar *
4516xmlNodeGetLang(xmlNodePtr cur) {
4517 xmlChar *lang;
4518
4519 while (cur != NULL) {
Daniel Veillardc17337c2001-05-09 10:51:31 +00004520 lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE);
Owen Taylor3473f882001-02-23 17:55:21 +00004521 if (lang != NULL)
4522 return(lang);
4523 cur = cur->parent;
4524 }
4525 return(NULL);
4526}
4527
4528
Daniel Veillard652327a2003-09-29 18:02:38 +00004529#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00004530/**
4531 * xmlNodeSetSpacePreserve:
4532 * @cur: the node being changed
4533 * @val: the xml:space value ("0": default, 1: "preserve")
4534 *
4535 * Set (or reset) the space preserving behaviour of a node, i.e. the
4536 * value of the xml:space attribute.
4537 */
4538void
4539xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004540 xmlNsPtr ns;
4541
Owen Taylor3473f882001-02-23 17:55:21 +00004542 if (cur == NULL) return;
4543 switch(cur->type) {
4544 case XML_TEXT_NODE:
4545 case XML_CDATA_SECTION_NODE:
4546 case XML_COMMENT_NODE:
4547 case XML_DOCUMENT_NODE:
4548 case XML_DOCUMENT_TYPE_NODE:
4549 case XML_DOCUMENT_FRAG_NODE:
4550 case XML_NOTATION_NODE:
4551 case XML_HTML_DOCUMENT_NODE:
4552 case XML_DTD_NODE:
4553 case XML_ELEMENT_DECL:
4554 case XML_ATTRIBUTE_DECL:
4555 case XML_ENTITY_DECL:
4556 case XML_PI_NODE:
4557 case XML_ENTITY_REF_NODE:
4558 case XML_ENTITY_NODE:
4559 case XML_NAMESPACE_DECL:
4560 case XML_XINCLUDE_START:
4561 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004562#ifdef LIBXML_DOCB_ENABLED
4563 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004564#endif
4565 return;
4566 case XML_ELEMENT_NODE:
4567 case XML_ATTRIBUTE_NODE:
4568 break;
4569 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004570 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4571 if (ns == NULL)
4572 return;
Owen Taylor3473f882001-02-23 17:55:21 +00004573 switch (val) {
4574 case 0:
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004575 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "default");
Owen Taylor3473f882001-02-23 17:55:21 +00004576 break;
4577 case 1:
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004578 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "preserve");
Owen Taylor3473f882001-02-23 17:55:21 +00004579 break;
4580 }
4581}
Daniel Veillard652327a2003-09-29 18:02:38 +00004582#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004583
4584/**
4585 * xmlNodeGetSpacePreserve:
4586 * @cur: the node being checked
4587 *
4588 * Searches the space preserving behaviour of a node, i.e. the values
4589 * of the xml:space attribute or the one carried by the nearest
4590 * ancestor.
4591 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004592 * Returns -1 if xml:space is not inherited, 0 if "default", 1 if "preserve"
Owen Taylor3473f882001-02-23 17:55:21 +00004593 */
4594int
4595xmlNodeGetSpacePreserve(xmlNodePtr cur) {
4596 xmlChar *space;
4597
4598 while (cur != NULL) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004599 space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE);
Owen Taylor3473f882001-02-23 17:55:21 +00004600 if (space != NULL) {
4601 if (xmlStrEqual(space, BAD_CAST "preserve")) {
4602 xmlFree(space);
4603 return(1);
4604 }
4605 if (xmlStrEqual(space, BAD_CAST "default")) {
4606 xmlFree(space);
4607 return(0);
4608 }
4609 xmlFree(space);
4610 }
4611 cur = cur->parent;
4612 }
4613 return(-1);
4614}
4615
Daniel Veillard652327a2003-09-29 18:02:38 +00004616#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00004617/**
4618 * xmlNodeSetName:
4619 * @cur: the node being changed
4620 * @name: the new tag name
4621 *
4622 * Set (or reset) the name of a node.
4623 */
4624void
4625xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) {
Daniel Veillardce244ad2004-11-05 10:03:46 +00004626 xmlDocPtr doc;
4627 xmlDictPtr dict;
4628
Owen Taylor3473f882001-02-23 17:55:21 +00004629 if (cur == NULL) return;
4630 if (name == NULL) return;
4631 switch(cur->type) {
4632 case XML_TEXT_NODE:
4633 case XML_CDATA_SECTION_NODE:
4634 case XML_COMMENT_NODE:
4635 case XML_DOCUMENT_TYPE_NODE:
4636 case XML_DOCUMENT_FRAG_NODE:
4637 case XML_NOTATION_NODE:
4638 case XML_HTML_DOCUMENT_NODE:
4639 case XML_NAMESPACE_DECL:
4640 case XML_XINCLUDE_START:
4641 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004642#ifdef LIBXML_DOCB_ENABLED
4643 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004644#endif
4645 return;
4646 case XML_ELEMENT_NODE:
4647 case XML_ATTRIBUTE_NODE:
4648 case XML_PI_NODE:
4649 case XML_ENTITY_REF_NODE:
4650 case XML_ENTITY_NODE:
4651 case XML_DTD_NODE:
4652 case XML_DOCUMENT_NODE:
4653 case XML_ELEMENT_DECL:
4654 case XML_ATTRIBUTE_DECL:
4655 case XML_ENTITY_DECL:
4656 break;
4657 }
Daniel Veillardce244ad2004-11-05 10:03:46 +00004658 doc = cur->doc;
4659 if (doc != NULL)
4660 dict = doc->dict;
4661 else
4662 dict = NULL;
4663 if (dict != NULL) {
4664 if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name)))
4665 xmlFree((xmlChar *) cur->name);
4666 cur->name = xmlDictLookup(dict, name, -1);
4667 } else {
4668 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
4669 cur->name = xmlStrdup(name);
4670 }
Owen Taylor3473f882001-02-23 17:55:21 +00004671}
Daniel Veillard2156d432004-03-04 15:59:36 +00004672#endif
Owen Taylor3473f882001-02-23 17:55:21 +00004673
Daniel Veillard2156d432004-03-04 15:59:36 +00004674#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004675/**
4676 * xmlNodeSetBase:
4677 * @cur: the node being changed
4678 * @uri: the new base URI
4679 *
4680 * Set (or reset) the base URI of a node, i.e. the value of the
4681 * xml:base attribute.
4682 */
4683void
Daniel Veillardf85ce8e2003-09-22 10:24:45 +00004684xmlNodeSetBase(xmlNodePtr cur, const xmlChar* uri) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004685 xmlNsPtr ns;
4686
Owen Taylor3473f882001-02-23 17:55:21 +00004687 if (cur == NULL) return;
4688 switch(cur->type) {
4689 case XML_TEXT_NODE:
4690 case XML_CDATA_SECTION_NODE:
4691 case XML_COMMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004692 case XML_DOCUMENT_TYPE_NODE:
4693 case XML_DOCUMENT_FRAG_NODE:
4694 case XML_NOTATION_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004695 case XML_DTD_NODE:
4696 case XML_ELEMENT_DECL:
4697 case XML_ATTRIBUTE_DECL:
4698 case XML_ENTITY_DECL:
4699 case XML_PI_NODE:
4700 case XML_ENTITY_REF_NODE:
4701 case XML_ENTITY_NODE:
4702 case XML_NAMESPACE_DECL:
4703 case XML_XINCLUDE_START:
4704 case XML_XINCLUDE_END:
Owen Taylor3473f882001-02-23 17:55:21 +00004705 return;
4706 case XML_ELEMENT_NODE:
4707 case XML_ATTRIBUTE_NODE:
4708 break;
Daniel Veillard4cbe4702002-05-05 06:57:27 +00004709 case XML_DOCUMENT_NODE:
4710#ifdef LIBXML_DOCB_ENABLED
4711 case XML_DOCB_DOCUMENT_NODE:
4712#endif
4713 case XML_HTML_DOCUMENT_NODE: {
4714 xmlDocPtr doc = (xmlDocPtr) cur;
4715
4716 if (doc->URL != NULL)
4717 xmlFree((xmlChar *) doc->URL);
4718 if (uri == NULL)
4719 doc->URL = NULL;
4720 else
4721 doc->URL = xmlStrdup(uri);
4722 return;
4723 }
Owen Taylor3473f882001-02-23 17:55:21 +00004724 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004725
4726 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4727 if (ns == NULL)
4728 return;
4729 xmlSetNsProp(cur, ns, BAD_CAST "base", uri);
Owen Taylor3473f882001-02-23 17:55:21 +00004730}
Daniel Veillard652327a2003-09-29 18:02:38 +00004731#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004732
4733/**
Owen Taylor3473f882001-02-23 17:55:21 +00004734 * xmlNodeGetBase:
4735 * @doc: the document the node pertains to
4736 * @cur: the node being checked
4737 *
4738 * Searches for the BASE URL. The code should work on both XML
4739 * and HTML document even if base mechanisms are completely different.
4740 * It returns the base as defined in RFC 2396 sections
4741 * 5.1.1. Base URI within Document Content
4742 * and
4743 * 5.1.2. Base URI from the Encapsulating Entity
4744 * However it does not return the document base (5.1.3), use
4745 * xmlDocumentGetBase() for this
4746 *
4747 * Returns a pointer to the base URL, or NULL if not found
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004748 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00004749 */
4750xmlChar *
4751xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00004752 xmlChar *oldbase = NULL;
4753 xmlChar *base, *newbase;
Owen Taylor3473f882001-02-23 17:55:21 +00004754
4755 if ((cur == NULL) && (doc == NULL))
4756 return(NULL);
4757 if (doc == NULL) doc = cur->doc;
4758 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
4759 cur = doc->children;
4760 while ((cur != NULL) && (cur->name != NULL)) {
4761 if (cur->type != XML_ELEMENT_NODE) {
4762 cur = cur->next;
4763 continue;
4764 }
4765 if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) {
4766 cur = cur->children;
4767 continue;
4768 }
4769 if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) {
4770 cur = cur->children;
4771 continue;
4772 }
4773 if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) {
4774 return(xmlGetProp(cur, BAD_CAST "href"));
4775 }
4776 cur = cur->next;
4777 }
4778 return(NULL);
4779 }
4780 while (cur != NULL) {
4781 if (cur->type == XML_ENTITY_DECL) {
4782 xmlEntityPtr ent = (xmlEntityPtr) cur;
4783 return(xmlStrdup(ent->URI));
4784 }
Daniel Veillard42596ad2001-05-22 16:57:14 +00004785 if (cur->type == XML_ELEMENT_NODE) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00004786 base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE);
Daniel Veillard42596ad2001-05-22 16:57:14 +00004787 if (base != NULL) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00004788 if (oldbase != NULL) {
4789 newbase = xmlBuildURI(oldbase, base);
4790 if (newbase != NULL) {
4791 xmlFree(oldbase);
4792 xmlFree(base);
4793 oldbase = newbase;
4794 } else {
4795 xmlFree(oldbase);
4796 xmlFree(base);
4797 return(NULL);
4798 }
4799 } else {
4800 oldbase = base;
4801 }
4802 if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) ||
4803 (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) ||
4804 (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4)))
4805 return(oldbase);
Daniel Veillard42596ad2001-05-22 16:57:14 +00004806 }
4807 }
Owen Taylor3473f882001-02-23 17:55:21 +00004808 cur = cur->parent;
4809 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00004810 if ((doc != NULL) && (doc->URL != NULL)) {
4811 if (oldbase == NULL)
4812 return(xmlStrdup(doc->URL));
4813 newbase = xmlBuildURI(oldbase, doc->URL);
4814 xmlFree(oldbase);
4815 return(newbase);
4816 }
4817 return(oldbase);
Owen Taylor3473f882001-02-23 17:55:21 +00004818}
4819
4820/**
Daniel Veillard78697292003-10-19 20:44:43 +00004821 * xmlNodeBufGetContent:
4822 * @buffer: a buffer
4823 * @cur: the node being read
4824 *
4825 * Read the value of a node @cur, this can be either the text carried
4826 * directly by this node if it's a TEXT node or the aggregate string
4827 * of the values carried by this node child's (TEXT and ENTITY_REF).
4828 * Entity references are substituted.
4829 * Fills up the buffer @buffer with this value
4830 *
4831 * Returns 0 in case of success and -1 in case of error.
4832 */
4833int
4834xmlNodeBufGetContent(xmlBufferPtr buffer, xmlNodePtr cur)
4835{
4836 if ((cur == NULL) || (buffer == NULL)) return(-1);
4837 switch (cur->type) {
4838 case XML_CDATA_SECTION_NODE:
4839 case XML_TEXT_NODE:
4840 xmlBufferCat(buffer, cur->content);
4841 break;
4842 case XML_DOCUMENT_FRAG_NODE:
4843 case XML_ELEMENT_NODE:{
4844 xmlNodePtr tmp = cur;
4845
4846 while (tmp != NULL) {
4847 switch (tmp->type) {
4848 case XML_CDATA_SECTION_NODE:
4849 case XML_TEXT_NODE:
4850 if (tmp->content != NULL)
4851 xmlBufferCat(buffer, tmp->content);
4852 break;
4853 case XML_ENTITY_REF_NODE:
4854 xmlNodeBufGetContent(buffer, tmp->children);
4855 break;
4856 default:
4857 break;
4858 }
4859 /*
4860 * Skip to next node
4861 */
4862 if (tmp->children != NULL) {
4863 if (tmp->children->type != XML_ENTITY_DECL) {
4864 tmp = tmp->children;
4865 continue;
4866 }
4867 }
4868 if (tmp == cur)
4869 break;
4870
4871 if (tmp->next != NULL) {
4872 tmp = tmp->next;
4873 continue;
4874 }
4875
4876 do {
4877 tmp = tmp->parent;
4878 if (tmp == NULL)
4879 break;
4880 if (tmp == cur) {
4881 tmp = NULL;
4882 break;
4883 }
4884 if (tmp->next != NULL) {
4885 tmp = tmp->next;
4886 break;
4887 }
4888 } while (tmp != NULL);
4889 }
4890 break;
4891 }
4892 case XML_ATTRIBUTE_NODE:{
4893 xmlAttrPtr attr = (xmlAttrPtr) cur;
4894 xmlNodePtr tmp = attr->children;
4895
4896 while (tmp != NULL) {
4897 if (tmp->type == XML_TEXT_NODE)
4898 xmlBufferCat(buffer, tmp->content);
4899 else
4900 xmlNodeBufGetContent(buffer, tmp);
4901 tmp = tmp->next;
4902 }
4903 break;
4904 }
4905 case XML_COMMENT_NODE:
4906 case XML_PI_NODE:
4907 xmlBufferCat(buffer, cur->content);
4908 break;
4909 case XML_ENTITY_REF_NODE:{
4910 xmlEntityPtr ent;
4911 xmlNodePtr tmp;
4912
4913 /* lookup entity declaration */
4914 ent = xmlGetDocEntity(cur->doc, cur->name);
4915 if (ent == NULL)
4916 return(-1);
4917
4918 /* an entity content can be any "well balanced chunk",
4919 * i.e. the result of the content [43] production:
4920 * http://www.w3.org/TR/REC-xml#NT-content
4921 * -> we iterate through child nodes and recursive call
4922 * xmlNodeGetContent() which handles all possible node types */
4923 tmp = ent->children;
4924 while (tmp) {
4925 xmlNodeBufGetContent(buffer, tmp);
4926 tmp = tmp->next;
4927 }
4928 break;
4929 }
4930 case XML_ENTITY_NODE:
4931 case XML_DOCUMENT_TYPE_NODE:
4932 case XML_NOTATION_NODE:
4933 case XML_DTD_NODE:
4934 case XML_XINCLUDE_START:
4935 case XML_XINCLUDE_END:
4936 break;
4937 case XML_DOCUMENT_NODE:
4938#ifdef LIBXML_DOCB_ENABLED
4939 case XML_DOCB_DOCUMENT_NODE:
4940#endif
4941 case XML_HTML_DOCUMENT_NODE:
4942 cur = cur->children;
4943 while (cur!= NULL) {
4944 if ((cur->type == XML_ELEMENT_NODE) ||
4945 (cur->type == XML_TEXT_NODE) ||
4946 (cur->type == XML_CDATA_SECTION_NODE)) {
4947 xmlNodeBufGetContent(buffer, cur);
4948 }
4949 cur = cur->next;
4950 }
4951 break;
4952 case XML_NAMESPACE_DECL:
4953 xmlBufferCat(buffer, ((xmlNsPtr) cur)->href);
4954 break;
4955 case XML_ELEMENT_DECL:
4956 case XML_ATTRIBUTE_DECL:
4957 case XML_ENTITY_DECL:
4958 break;
4959 }
4960 return(0);
4961}
4962/**
Owen Taylor3473f882001-02-23 17:55:21 +00004963 * xmlNodeGetContent:
4964 * @cur: the node being read
4965 *
4966 * Read the value of a node, this can be either the text carried
4967 * directly by this node if it's a TEXT node or the aggregate string
4968 * of the values carried by this node child's (TEXT and ENTITY_REF).
Daniel Veillardd1640922001-12-17 15:30:10 +00004969 * Entity references are substituted.
4970 * Returns a new #xmlChar * or NULL if no content is available.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004971 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00004972 */
4973xmlChar *
Daniel Veillard7646b182002-04-20 06:41:40 +00004974xmlNodeGetContent(xmlNodePtr cur)
4975{
4976 if (cur == NULL)
4977 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004978 switch (cur->type) {
4979 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00004980 case XML_ELEMENT_NODE:{
Daniel Veillard7646b182002-04-20 06:41:40 +00004981 xmlBufferPtr buffer;
4982 xmlChar *ret;
Owen Taylor3473f882001-02-23 17:55:21 +00004983
Daniel Veillard814a76d2003-01-23 18:24:20 +00004984 buffer = xmlBufferCreateSize(64);
Daniel Veillard7646b182002-04-20 06:41:40 +00004985 if (buffer == NULL)
4986 return (NULL);
Daniel Veillardc4696922003-10-19 21:47:14 +00004987 xmlNodeBufGetContent(buffer, cur);
Daniel Veillard7646b182002-04-20 06:41:40 +00004988 ret = buffer->content;
4989 buffer->content = NULL;
4990 xmlBufferFree(buffer);
4991 return (ret);
4992 }
4993 case XML_ATTRIBUTE_NODE:{
4994 xmlAttrPtr attr = (xmlAttrPtr) cur;
4995
4996 if (attr->parent != NULL)
4997 return (xmlNodeListGetString
4998 (attr->parent->doc, attr->children, 1));
4999 else
5000 return (xmlNodeListGetString(NULL, attr->children, 1));
5001 break;
5002 }
Owen Taylor3473f882001-02-23 17:55:21 +00005003 case XML_COMMENT_NODE:
5004 case XML_PI_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005005 if (cur->content != NULL)
5006 return (xmlStrdup(cur->content));
5007 return (NULL);
5008 case XML_ENTITY_REF_NODE:{
5009 xmlEntityPtr ent;
Daniel Veillard7646b182002-04-20 06:41:40 +00005010 xmlBufferPtr buffer;
5011 xmlChar *ret;
5012
5013 /* lookup entity declaration */
5014 ent = xmlGetDocEntity(cur->doc, cur->name);
5015 if (ent == NULL)
5016 return (NULL);
5017
5018 buffer = xmlBufferCreate();
5019 if (buffer == NULL)
5020 return (NULL);
5021
Daniel Veillardc4696922003-10-19 21:47:14 +00005022 xmlNodeBufGetContent(buffer, cur);
Daniel Veillard7646b182002-04-20 06:41:40 +00005023
5024 ret = buffer->content;
5025 buffer->content = NULL;
5026 xmlBufferFree(buffer);
5027 return (ret);
5028 }
Owen Taylor3473f882001-02-23 17:55:21 +00005029 case XML_ENTITY_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005030 case XML_DOCUMENT_TYPE_NODE:
5031 case XML_NOTATION_NODE:
5032 case XML_DTD_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005033 case XML_XINCLUDE_START:
5034 case XML_XINCLUDE_END:
Daniel Veillard9adc0462003-03-24 18:39:54 +00005035 return (NULL);
5036 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005037#ifdef LIBXML_DOCB_ENABLED
Daniel Veillard7646b182002-04-20 06:41:40 +00005038 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005039#endif
Daniel Veillard9adc0462003-03-24 18:39:54 +00005040 case XML_HTML_DOCUMENT_NODE: {
Daniel Veillardc4696922003-10-19 21:47:14 +00005041 xmlBufferPtr buffer;
5042 xmlChar *ret;
Daniel Veillard9adc0462003-03-24 18:39:54 +00005043
Daniel Veillardc4696922003-10-19 21:47:14 +00005044 buffer = xmlBufferCreate();
5045 if (buffer == NULL)
5046 return (NULL);
5047
5048 xmlNodeBufGetContent(buffer, (xmlNodePtr) cur);
5049
5050 ret = buffer->content;
5051 buffer->content = NULL;
5052 xmlBufferFree(buffer);
5053 return (ret);
Daniel Veillard9adc0462003-03-24 18:39:54 +00005054 }
Daniel Veillard96c3a3b2002-10-14 15:39:04 +00005055 case XML_NAMESPACE_DECL: {
5056 xmlChar *tmp;
5057
5058 tmp = xmlStrdup(((xmlNsPtr) cur)->href);
5059 return (tmp);
5060 }
Owen Taylor3473f882001-02-23 17:55:21 +00005061 case XML_ELEMENT_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00005062 /* TODO !!! */
5063 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005064 case XML_ATTRIBUTE_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00005065 /* TODO !!! */
5066 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005067 case XML_ENTITY_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00005068 /* TODO !!! */
5069 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005070 case XML_CDATA_SECTION_NODE:
5071 case XML_TEXT_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005072 if (cur->content != NULL)
5073 return (xmlStrdup(cur->content));
5074 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005075 }
Daniel Veillard7646b182002-04-20 06:41:40 +00005076 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005077}
Daniel Veillard652327a2003-09-29 18:02:38 +00005078
Owen Taylor3473f882001-02-23 17:55:21 +00005079/**
5080 * xmlNodeSetContent:
5081 * @cur: the node being modified
5082 * @content: the new value of the content
5083 *
5084 * Replace the content of a node.
5085 */
5086void
5087xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) {
5088 if (cur == NULL) {
5089#ifdef DEBUG_TREE
5090 xmlGenericError(xmlGenericErrorContext,
5091 "xmlNodeSetContent : node == NULL\n");
5092#endif
5093 return;
5094 }
5095 switch (cur->type) {
5096 case XML_DOCUMENT_FRAG_NODE:
5097 case XML_ELEMENT_NODE:
Daniel Veillard2c748c62002-01-16 15:37:50 +00005098 case XML_ATTRIBUTE_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005099 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5100 cur->children = xmlStringGetNodeList(cur->doc, content);
5101 UPDATE_LAST_CHILD_AND_PARENT(cur)
5102 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005103 case XML_TEXT_NODE:
5104 case XML_CDATA_SECTION_NODE:
5105 case XML_ENTITY_REF_NODE:
5106 case XML_ENTITY_NODE:
5107 case XML_PI_NODE:
5108 case XML_COMMENT_NODE:
5109 if (cur->content != NULL) {
William M. Brack7762bb12004-01-04 14:49:01 +00005110 if (!((cur->doc != NULL) && (cur->doc->dict != NULL) &&
Daniel Veillardc587bce2005-05-10 15:28:08 +00005111 (xmlDictOwns(cur->doc->dict, cur->content))))
William M. Brack7762bb12004-01-04 14:49:01 +00005112 xmlFree(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00005113 }
5114 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5115 cur->last = cur->children = NULL;
5116 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005117 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00005118 } else
5119 cur->content = NULL;
5120 break;
5121 case XML_DOCUMENT_NODE:
5122 case XML_HTML_DOCUMENT_NODE:
5123 case XML_DOCUMENT_TYPE_NODE:
5124 case XML_XINCLUDE_START:
5125 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005126#ifdef LIBXML_DOCB_ENABLED
5127 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005128#endif
5129 break;
5130 case XML_NOTATION_NODE:
5131 break;
5132 case XML_DTD_NODE:
5133 break;
5134 case XML_NAMESPACE_DECL:
5135 break;
5136 case XML_ELEMENT_DECL:
5137 /* TODO !!! */
5138 break;
5139 case XML_ATTRIBUTE_DECL:
5140 /* TODO !!! */
5141 break;
5142 case XML_ENTITY_DECL:
5143 /* TODO !!! */
5144 break;
5145 }
5146}
5147
Daniel Veillard652327a2003-09-29 18:02:38 +00005148#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00005149/**
5150 * xmlNodeSetContentLen:
5151 * @cur: the node being modified
5152 * @content: the new value of the content
5153 * @len: the size of @content
5154 *
5155 * Replace the content of a node.
5156 */
5157void
5158xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5159 if (cur == NULL) {
5160#ifdef DEBUG_TREE
5161 xmlGenericError(xmlGenericErrorContext,
5162 "xmlNodeSetContentLen : node == NULL\n");
5163#endif
5164 return;
5165 }
5166 switch (cur->type) {
5167 case XML_DOCUMENT_FRAG_NODE:
5168 case XML_ELEMENT_NODE:
Daniel Veillard2c748c62002-01-16 15:37:50 +00005169 case XML_ATTRIBUTE_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005170 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5171 cur->children = xmlStringLenGetNodeList(cur->doc, content, len);
5172 UPDATE_LAST_CHILD_AND_PARENT(cur)
5173 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005174 case XML_TEXT_NODE:
5175 case XML_CDATA_SECTION_NODE:
5176 case XML_ENTITY_REF_NODE:
5177 case XML_ENTITY_NODE:
5178 case XML_PI_NODE:
5179 case XML_COMMENT_NODE:
5180 case XML_NOTATION_NODE:
5181 if (cur->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005182 xmlFree(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00005183 }
5184 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5185 cur->children = cur->last = NULL;
5186 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005187 cur->content = xmlStrndup(content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00005188 } else
5189 cur->content = NULL;
5190 break;
5191 case XML_DOCUMENT_NODE:
5192 case XML_DTD_NODE:
5193 case XML_HTML_DOCUMENT_NODE:
5194 case XML_DOCUMENT_TYPE_NODE:
5195 case XML_NAMESPACE_DECL:
5196 case XML_XINCLUDE_START:
5197 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005198#ifdef LIBXML_DOCB_ENABLED
5199 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005200#endif
5201 break;
5202 case XML_ELEMENT_DECL:
5203 /* TODO !!! */
5204 break;
5205 case XML_ATTRIBUTE_DECL:
5206 /* TODO !!! */
5207 break;
5208 case XML_ENTITY_DECL:
5209 /* TODO !!! */
5210 break;
5211 }
5212}
Daniel Veillard652327a2003-09-29 18:02:38 +00005213#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005214
5215/**
5216 * xmlNodeAddContentLen:
5217 * @cur: the node being modified
5218 * @content: extra content
5219 * @len: the size of @content
5220 *
5221 * Append the extra substring to the node content.
5222 */
5223void
5224xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5225 if (cur == NULL) {
5226#ifdef DEBUG_TREE
5227 xmlGenericError(xmlGenericErrorContext,
5228 "xmlNodeAddContentLen : node == NULL\n");
5229#endif
5230 return;
5231 }
5232 if (len <= 0) return;
5233 switch (cur->type) {
5234 case XML_DOCUMENT_FRAG_NODE:
5235 case XML_ELEMENT_NODE: {
Daniel Veillardacb2bda2002-01-13 16:15:43 +00005236 xmlNodePtr last, newNode, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00005237
Daniel Veillard7db37732001-07-12 01:20:08 +00005238 last = cur->last;
Owen Taylor3473f882001-02-23 17:55:21 +00005239 newNode = xmlNewTextLen(content, len);
5240 if (newNode != NULL) {
Daniel Veillardacb2bda2002-01-13 16:15:43 +00005241 tmp = xmlAddChild(cur, newNode);
5242 if (tmp != newNode)
5243 return;
Owen Taylor3473f882001-02-23 17:55:21 +00005244 if ((last != NULL) && (last->next == newNode)) {
5245 xmlTextMerge(last, newNode);
5246 }
5247 }
5248 break;
5249 }
5250 case XML_ATTRIBUTE_NODE:
5251 break;
5252 case XML_TEXT_NODE:
5253 case XML_CDATA_SECTION_NODE:
5254 case XML_ENTITY_REF_NODE:
5255 case XML_ENTITY_NODE:
5256 case XML_PI_NODE:
5257 case XML_COMMENT_NODE:
5258 case XML_NOTATION_NODE:
5259 if (content != NULL) {
William M. Brack7762bb12004-01-04 14:49:01 +00005260 if ((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5261 xmlDictOwns(cur->doc->dict, cur->content)) {
5262 cur->content =
5263 xmlStrncatNew(cur->content, content, len);
5264 break;
5265 }
Owen Taylor3473f882001-02-23 17:55:21 +00005266 cur->content = xmlStrncat(cur->content, content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00005267 }
5268 case XML_DOCUMENT_NODE:
5269 case XML_DTD_NODE:
5270 case XML_HTML_DOCUMENT_NODE:
5271 case XML_DOCUMENT_TYPE_NODE:
5272 case XML_NAMESPACE_DECL:
5273 case XML_XINCLUDE_START:
5274 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005275#ifdef LIBXML_DOCB_ENABLED
5276 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005277#endif
5278 break;
5279 case XML_ELEMENT_DECL:
5280 case XML_ATTRIBUTE_DECL:
5281 case XML_ENTITY_DECL:
5282 break;
5283 }
5284}
5285
5286/**
5287 * xmlNodeAddContent:
5288 * @cur: the node being modified
5289 * @content: extra content
5290 *
5291 * Append the extra substring to the node content.
5292 */
5293void
5294xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) {
5295 int len;
5296
5297 if (cur == NULL) {
5298#ifdef DEBUG_TREE
5299 xmlGenericError(xmlGenericErrorContext,
5300 "xmlNodeAddContent : node == NULL\n");
5301#endif
5302 return;
5303 }
5304 if (content == NULL) return;
5305 len = xmlStrlen(content);
5306 xmlNodeAddContentLen(cur, content, len);
5307}
5308
5309/**
5310 * xmlTextMerge:
5311 * @first: the first text node
5312 * @second: the second text node being merged
5313 *
5314 * Merge two text nodes into one
5315 * Returns the first text node augmented
5316 */
5317xmlNodePtr
5318xmlTextMerge(xmlNodePtr first, xmlNodePtr second) {
5319 if (first == NULL) return(second);
5320 if (second == NULL) return(first);
5321 if (first->type != XML_TEXT_NODE) return(first);
5322 if (second->type != XML_TEXT_NODE) return(first);
5323 if (second->name != first->name)
5324 return(first);
Owen Taylor3473f882001-02-23 17:55:21 +00005325 xmlNodeAddContent(first, second->content);
Owen Taylor3473f882001-02-23 17:55:21 +00005326 xmlUnlinkNode(second);
5327 xmlFreeNode(second);
5328 return(first);
5329}
5330
Daniel Veillard2156d432004-03-04 15:59:36 +00005331#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XPATH_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00005332/**
5333 * xmlGetNsList:
5334 * @doc: the document
5335 * @node: the current node
5336 *
5337 * Search all the namespace applying to a given element.
Daniel Veillardd1640922001-12-17 15:30:10 +00005338 * Returns an NULL terminated array of all the #xmlNsPtr found
Owen Taylor3473f882001-02-23 17:55:21 +00005339 * that need to be freed by the caller or NULL if no
5340 * namespace if defined
5341 */
5342xmlNsPtr *
Daniel Veillard77044732001-06-29 21:31:07 +00005343xmlGetNsList(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node)
5344{
Owen Taylor3473f882001-02-23 17:55:21 +00005345 xmlNsPtr cur;
5346 xmlNsPtr *ret = NULL;
5347 int nbns = 0;
5348 int maxns = 10;
5349 int i;
5350
5351 while (node != NULL) {
Daniel Veillard77044732001-06-29 21:31:07 +00005352 if (node->type == XML_ELEMENT_NODE) {
5353 cur = node->nsDef;
5354 while (cur != NULL) {
5355 if (ret == NULL) {
5356 ret =
5357 (xmlNsPtr *) xmlMalloc((maxns + 1) *
5358 sizeof(xmlNsPtr));
5359 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005360 xmlTreeErrMemory("getting namespace list");
Daniel Veillard77044732001-06-29 21:31:07 +00005361 return (NULL);
5362 }
5363 ret[nbns] = NULL;
5364 }
5365 for (i = 0; i < nbns; i++) {
5366 if ((cur->prefix == ret[i]->prefix) ||
5367 (xmlStrEqual(cur->prefix, ret[i]->prefix)))
5368 break;
5369 }
5370 if (i >= nbns) {
5371 if (nbns >= maxns) {
5372 maxns *= 2;
5373 ret = (xmlNsPtr *) xmlRealloc(ret,
5374 (maxns +
5375 1) *
5376 sizeof(xmlNsPtr));
5377 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005378 xmlTreeErrMemory("getting namespace list");
Daniel Veillard77044732001-06-29 21:31:07 +00005379 return (NULL);
5380 }
5381 }
5382 ret[nbns++] = cur;
5383 ret[nbns] = NULL;
5384 }
Owen Taylor3473f882001-02-23 17:55:21 +00005385
Daniel Veillard77044732001-06-29 21:31:07 +00005386 cur = cur->next;
5387 }
5388 }
5389 node = node->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00005390 }
Daniel Veillard77044732001-06-29 21:31:07 +00005391 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00005392}
Daniel Veillard652327a2003-09-29 18:02:38 +00005393#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005394
5395/**
5396 * xmlSearchNs:
5397 * @doc: the document
5398 * @node: the current node
Daniel Veillard77851712001-02-27 21:54:07 +00005399 * @nameSpace: the namespace prefix
Owen Taylor3473f882001-02-23 17:55:21 +00005400 *
5401 * Search a Ns registered under a given name space for a document.
5402 * recurse on the parents until it finds the defined namespace
5403 * or return NULL otherwise.
5404 * @nameSpace can be NULL, this is a search for the default namespace.
5405 * We don't allow to cross entities boundaries. If you don't declare
5406 * the namespace within those you will be in troubles !!! A warning
5407 * is generated to cover this case.
5408 *
5409 * Returns the namespace pointer or NULL.
5410 */
5411xmlNsPtr
5412xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) {
Daniel Veillardf4e56292003-10-28 14:27:41 +00005413
Owen Taylor3473f882001-02-23 17:55:21 +00005414 xmlNsPtr cur;
Daniel Veillardf4e56292003-10-28 14:27:41 +00005415 xmlNodePtr orig = node;
Owen Taylor3473f882001-02-23 17:55:21 +00005416
5417 if (node == NULL) return(NULL);
5418 if ((nameSpace != NULL) &&
5419 (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) {
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00005420 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
5421 /*
5422 * The XML-1.0 namespace is normally held on the root
5423 * element. In this case exceptionally create it on the
5424 * node element.
5425 */
5426 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5427 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005428 xmlTreeErrMemory("searching namespace");
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00005429 return(NULL);
5430 }
5431 memset(cur, 0, sizeof(xmlNs));
5432 cur->type = XML_LOCAL_NAMESPACE;
5433 cur->href = xmlStrdup(XML_XML_NAMESPACE);
5434 cur->prefix = xmlStrdup((const xmlChar *)"xml");
5435 cur->next = node->nsDef;
5436 node->nsDef = cur;
5437 return(cur);
5438 }
Owen Taylor3473f882001-02-23 17:55:21 +00005439 if (doc->oldNs == NULL) {
5440 /*
5441 * Allocate a new Namespace and fill the fields.
5442 */
5443 doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5444 if (doc->oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005445 xmlTreeErrMemory("searching namespace");
Owen Taylor3473f882001-02-23 17:55:21 +00005446 return(NULL);
5447 }
5448 memset(doc->oldNs, 0, sizeof(xmlNs));
5449 doc->oldNs->type = XML_LOCAL_NAMESPACE;
5450
5451 doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE);
5452 doc->oldNs->prefix = xmlStrdup((const xmlChar *)"xml");
5453 }
5454 return(doc->oldNs);
5455 }
5456 while (node != NULL) {
5457 if ((node->type == XML_ENTITY_REF_NODE) ||
5458 (node->type == XML_ENTITY_NODE) ||
5459 (node->type == XML_ENTITY_DECL))
5460 return(NULL);
5461 if (node->type == XML_ELEMENT_NODE) {
5462 cur = node->nsDef;
5463 while (cur != NULL) {
5464 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
5465 (cur->href != NULL))
5466 return(cur);
5467 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
5468 (cur->href != NULL) &&
5469 (xmlStrEqual(cur->prefix, nameSpace)))
5470 return(cur);
5471 cur = cur->next;
5472 }
Daniel Veillardf4e56292003-10-28 14:27:41 +00005473 if (orig != node) {
5474 cur = node->ns;
5475 if (cur != NULL) {
5476 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
5477 (cur->href != NULL))
5478 return(cur);
5479 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
5480 (cur->href != NULL) &&
5481 (xmlStrEqual(cur->prefix, nameSpace)))
5482 return(cur);
5483 }
5484 }
Owen Taylor3473f882001-02-23 17:55:21 +00005485 }
5486 node = node->parent;
5487 }
5488 return(NULL);
5489}
5490
5491/**
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005492 * xmlNsInScope:
5493 * @doc: the document
5494 * @node: the current node
5495 * @ancestor: the ancestor carrying the namespace
5496 * @prefix: the namespace prefix
5497 *
5498 * Verify that the given namespace held on @ancestor is still in scope
5499 * on node.
5500 *
5501 * Returns 1 if true, 0 if false and -1 in case of error.
5502 */
5503static int
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005504xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node,
5505 xmlNodePtr ancestor, const xmlChar * prefix)
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005506{
5507 xmlNsPtr tst;
5508
5509 while ((node != NULL) && (node != ancestor)) {
5510 if ((node->type == XML_ENTITY_REF_NODE) ||
5511 (node->type == XML_ENTITY_NODE) ||
5512 (node->type == XML_ENTITY_DECL))
5513 return (-1);
5514 if (node->type == XML_ELEMENT_NODE) {
5515 tst = node->nsDef;
5516 while (tst != NULL) {
5517 if ((tst->prefix == NULL)
5518 && (prefix == NULL))
5519 return (0);
5520 if ((tst->prefix != NULL)
5521 && (prefix != NULL)
5522 && (xmlStrEqual(tst->prefix, prefix)))
5523 return (0);
5524 tst = tst->next;
5525 }
5526 }
5527 node = node->parent;
5528 }
5529 if (node != ancestor)
5530 return (-1);
5531 return (1);
5532}
5533
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005534/**
Owen Taylor3473f882001-02-23 17:55:21 +00005535 * xmlSearchNsByHref:
5536 * @doc: the document
5537 * @node: the current node
5538 * @href: the namespace value
5539 *
5540 * Search a Ns aliasing a given URI. Recurse on the parents until it finds
5541 * the defined namespace or return NULL otherwise.
5542 * Returns the namespace pointer or NULL.
5543 */
5544xmlNsPtr
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005545xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href)
5546{
Owen Taylor3473f882001-02-23 17:55:21 +00005547 xmlNsPtr cur;
5548 xmlNodePtr orig = node;
Daniel Veillard62040be2004-05-17 03:17:26 +00005549 int is_attr;
Owen Taylor3473f882001-02-23 17:55:21 +00005550
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005551 if ((node == NULL) || (href == NULL))
5552 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005553 if (xmlStrEqual(href, XML_XML_NAMESPACE)) {
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005554 /*
5555 * Only the document can hold the XML spec namespace.
5556 */
5557 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
5558 /*
5559 * The XML-1.0 namespace is normally held on the root
5560 * element. In this case exceptionally create it on the
5561 * node element.
5562 */
5563 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5564 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005565 xmlTreeErrMemory("searching namespace");
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005566 return (NULL);
5567 }
5568 memset(cur, 0, sizeof(xmlNs));
5569 cur->type = XML_LOCAL_NAMESPACE;
5570 cur->href = xmlStrdup(XML_XML_NAMESPACE);
5571 cur->prefix = xmlStrdup((const xmlChar *) "xml");
5572 cur->next = node->nsDef;
5573 node->nsDef = cur;
5574 return (cur);
5575 }
5576 if (doc->oldNs == NULL) {
5577 /*
5578 * Allocate a new Namespace and fill the fields.
5579 */
5580 doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5581 if (doc->oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005582 xmlTreeErrMemory("searching namespace");
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005583 return (NULL);
5584 }
5585 memset(doc->oldNs, 0, sizeof(xmlNs));
5586 doc->oldNs->type = XML_LOCAL_NAMESPACE;
Owen Taylor3473f882001-02-23 17:55:21 +00005587
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005588 doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE);
5589 doc->oldNs->prefix = xmlStrdup((const xmlChar *) "xml");
5590 }
5591 return (doc->oldNs);
Owen Taylor3473f882001-02-23 17:55:21 +00005592 }
Daniel Veillard62040be2004-05-17 03:17:26 +00005593 is_attr = (node->type == XML_ATTRIBUTE_NODE);
Owen Taylor3473f882001-02-23 17:55:21 +00005594 while (node != NULL) {
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005595 if ((node->type == XML_ENTITY_REF_NODE) ||
5596 (node->type == XML_ENTITY_NODE) ||
5597 (node->type == XML_ENTITY_DECL))
5598 return (NULL);
5599 if (node->type == XML_ELEMENT_NODE) {
5600 cur = node->nsDef;
5601 while (cur != NULL) {
5602 if ((cur->href != NULL) && (href != NULL) &&
5603 (xmlStrEqual(cur->href, href))) {
Daniel Veillard62040be2004-05-17 03:17:26 +00005604 if (((!is_attr) || (cur->prefix != NULL)) &&
Kasimier T. Buchcikba70cc02005-02-28 10:28:21 +00005605 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005606 return (cur);
5607 }
5608 cur = cur->next;
5609 }
Daniel Veillardf4e56292003-10-28 14:27:41 +00005610 if (orig != node) {
5611 cur = node->ns;
5612 if (cur != NULL) {
5613 if ((cur->href != NULL) && (href != NULL) &&
5614 (xmlStrEqual(cur->href, href))) {
Daniel Veillard62040be2004-05-17 03:17:26 +00005615 if (((!is_attr) || (cur->prefix != NULL)) &&
Kasimier T. Buchcikba70cc02005-02-28 10:28:21 +00005616 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
Daniel Veillardf4e56292003-10-28 14:27:41 +00005617 return (cur);
5618 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005619 }
Daniel Veillardf4e56292003-10-28 14:27:41 +00005620 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005621 }
5622 node = node->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00005623 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005624 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005625}
5626
5627/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005628 * xmlNewReconciliedNs:
Owen Taylor3473f882001-02-23 17:55:21 +00005629 * @doc: the document
5630 * @tree: a node expected to hold the new namespace
5631 * @ns: the original namespace
5632 *
5633 * This function tries to locate a namespace definition in a tree
5634 * ancestors, or create a new namespace definition node similar to
5635 * @ns trying to reuse the same prefix. However if the given prefix is
5636 * null (default namespace) or reused within the subtree defined by
5637 * @tree or on one of its ancestors then a new prefix is generated.
5638 * Returns the (new) namespace definition or NULL in case of error
5639 */
5640xmlNsPtr
5641xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) {
5642 xmlNsPtr def;
5643 xmlChar prefix[50];
5644 int counter = 1;
5645
5646 if (tree == NULL) {
5647#ifdef DEBUG_TREE
5648 xmlGenericError(xmlGenericErrorContext,
5649 "xmlNewReconciliedNs : tree == NULL\n");
5650#endif
5651 return(NULL);
5652 }
Daniel Veillardce244ad2004-11-05 10:03:46 +00005653 if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005654#ifdef DEBUG_TREE
5655 xmlGenericError(xmlGenericErrorContext,
5656 "xmlNewReconciliedNs : ns == NULL\n");
5657#endif
5658 return(NULL);
5659 }
5660 /*
5661 * Search an existing namespace definition inherited.
5662 */
5663 def = xmlSearchNsByHref(doc, tree, ns->href);
5664 if (def != NULL)
5665 return(def);
5666
5667 /*
5668 * Find a close prefix which is not already in use.
5669 * Let's strip namespace prefixes longer than 20 chars !
5670 */
Daniel Veillardf742d342002-03-07 00:05:35 +00005671 if (ns->prefix == NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00005672 snprintf((char *) prefix, sizeof(prefix), "default");
Daniel Veillardf742d342002-03-07 00:05:35 +00005673 else
William M. Brack13dfa872004-09-18 04:52:08 +00005674 snprintf((char *) prefix, sizeof(prefix), "%.20s", (char *)ns->prefix);
Daniel Veillardf742d342002-03-07 00:05:35 +00005675
Owen Taylor3473f882001-02-23 17:55:21 +00005676 def = xmlSearchNs(doc, tree, prefix);
5677 while (def != NULL) {
5678 if (counter > 1000) return(NULL);
Daniel Veillardf742d342002-03-07 00:05:35 +00005679 if (ns->prefix == NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00005680 snprintf((char *) prefix, sizeof(prefix), "default%d", counter++);
Daniel Veillardf742d342002-03-07 00:05:35 +00005681 else
William M. Brack13dfa872004-09-18 04:52:08 +00005682 snprintf((char *) prefix, sizeof(prefix), "%.20s%d",
5683 (char *)ns->prefix, counter++);
Owen Taylor3473f882001-02-23 17:55:21 +00005684 def = xmlSearchNs(doc, tree, prefix);
5685 }
5686
5687 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00005688 * OK, now we are ready to create a new one.
Owen Taylor3473f882001-02-23 17:55:21 +00005689 */
5690 def = xmlNewNs(tree, ns->href, prefix);
5691 return(def);
5692}
5693
Daniel Veillard652327a2003-09-29 18:02:38 +00005694#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00005695/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005696 * xmlReconciliateNs:
Owen Taylor3473f882001-02-23 17:55:21 +00005697 * @doc: the document
5698 * @tree: a node defining the subtree to reconciliate
5699 *
5700 * This function checks that all the namespaces declared within the given
5701 * tree are properly declared. This is needed for example after Copy or Cut
5702 * and then paste operations. The subtree may still hold pointers to
5703 * namespace declarations outside the subtree or invalid/masked. As much
Daniel Veillardd1640922001-12-17 15:30:10 +00005704 * as possible the function try to reuse the existing namespaces found in
Owen Taylor3473f882001-02-23 17:55:21 +00005705 * the new environment. If not possible the new namespaces are redeclared
5706 * on @tree at the top of the given subtree.
5707 * Returns the number of namespace declarations created or -1 in case of error.
5708 */
5709int
5710xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) {
5711 xmlNsPtr *oldNs = NULL;
5712 xmlNsPtr *newNs = NULL;
5713 int sizeCache = 0;
5714 int nbCache = 0;
5715
5716 xmlNsPtr n;
5717 xmlNodePtr node = tree;
5718 xmlAttrPtr attr;
5719 int ret = 0, i;
5720
Daniel Veillardce244ad2004-11-05 10:03:46 +00005721 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) return(-1);
5722 if ((doc == NULL) || (doc->type != XML_DOCUMENT_NODE)) return(-1);
5723 if (node->doc != doc) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00005724 while (node != NULL) {
5725 /*
5726 * Reconciliate the node namespace
5727 */
5728 if (node->ns != NULL) {
5729 /*
5730 * initialize the cache if needed
5731 */
5732 if (sizeCache == 0) {
5733 sizeCache = 10;
5734 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
5735 sizeof(xmlNsPtr));
5736 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005737 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005738 return(-1);
5739 }
5740 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
5741 sizeof(xmlNsPtr));
5742 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005743 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005744 xmlFree(oldNs);
5745 return(-1);
5746 }
5747 }
5748 for (i = 0;i < nbCache;i++) {
5749 if (oldNs[i] == node->ns) {
5750 node->ns = newNs[i];
5751 break;
5752 }
5753 }
5754 if (i == nbCache) {
5755 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00005756 * OK we need to recreate a new namespace definition
Owen Taylor3473f882001-02-23 17:55:21 +00005757 */
5758 n = xmlNewReconciliedNs(doc, tree, node->ns);
5759 if (n != NULL) { /* :-( what if else ??? */
5760 /*
5761 * check if we need to grow the cache buffers.
5762 */
5763 if (sizeCache <= nbCache) {
5764 sizeCache *= 2;
5765 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
5766 sizeof(xmlNsPtr));
5767 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005768 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005769 xmlFree(newNs);
5770 return(-1);
5771 }
5772 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
5773 sizeof(xmlNsPtr));
5774 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005775 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005776 xmlFree(oldNs);
5777 return(-1);
5778 }
5779 }
5780 newNs[nbCache] = n;
5781 oldNs[nbCache++] = node->ns;
5782 node->ns = n;
5783 }
5784 }
5785 }
5786 /*
5787 * now check for namespace hold by attributes on the node.
5788 */
5789 attr = node->properties;
5790 while (attr != NULL) {
5791 if (attr->ns != NULL) {
5792 /*
5793 * initialize the cache if needed
5794 */
5795 if (sizeCache == 0) {
5796 sizeCache = 10;
5797 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
5798 sizeof(xmlNsPtr));
5799 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005800 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005801 return(-1);
5802 }
5803 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
5804 sizeof(xmlNsPtr));
5805 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005806 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005807 xmlFree(oldNs);
5808 return(-1);
5809 }
5810 }
5811 for (i = 0;i < nbCache;i++) {
5812 if (oldNs[i] == attr->ns) {
Daniel Veillardce66ce12002-10-28 19:01:59 +00005813 attr->ns = newNs[i];
Owen Taylor3473f882001-02-23 17:55:21 +00005814 break;
5815 }
5816 }
5817 if (i == nbCache) {
5818 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00005819 * OK we need to recreate a new namespace definition
Owen Taylor3473f882001-02-23 17:55:21 +00005820 */
5821 n = xmlNewReconciliedNs(doc, tree, attr->ns);
5822 if (n != NULL) { /* :-( what if else ??? */
5823 /*
5824 * check if we need to grow the cache buffers.
5825 */
5826 if (sizeCache <= nbCache) {
5827 sizeCache *= 2;
5828 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
5829 sizeof(xmlNsPtr));
5830 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005831 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005832 xmlFree(newNs);
5833 return(-1);
5834 }
5835 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
5836 sizeof(xmlNsPtr));
5837 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005838 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005839 xmlFree(oldNs);
5840 return(-1);
5841 }
5842 }
5843 newNs[nbCache] = n;
5844 oldNs[nbCache++] = attr->ns;
5845 attr->ns = n;
5846 }
5847 }
5848 }
5849 attr = attr->next;
5850 }
5851
5852 /*
5853 * Browse the full subtree, deep first
5854 */
Daniel Veillardac996a12004-07-30 12:02:58 +00005855 if (node->children != NULL && node->type != XML_ENTITY_REF_NODE) {
Owen Taylor3473f882001-02-23 17:55:21 +00005856 /* deep first */
5857 node = node->children;
5858 } else if ((node != tree) && (node->next != NULL)) {
5859 /* then siblings */
5860 node = node->next;
5861 } else if (node != tree) {
5862 /* go up to parents->next if needed */
5863 while (node != tree) {
5864 if (node->parent != NULL)
5865 node = node->parent;
5866 if ((node != tree) && (node->next != NULL)) {
5867 node = node->next;
5868 break;
5869 }
5870 if (node->parent == NULL) {
5871 node = NULL;
5872 break;
5873 }
5874 }
5875 /* exit condition */
5876 if (node == tree)
5877 node = NULL;
Daniel Veillard1e774382002-03-06 17:35:40 +00005878 } else
5879 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005880 }
Daniel Veillardf742d342002-03-07 00:05:35 +00005881 if (oldNs != NULL)
5882 xmlFree(oldNs);
5883 if (newNs != NULL)
5884 xmlFree(newNs);
Owen Taylor3473f882001-02-23 17:55:21 +00005885 return(ret);
5886}
Daniel Veillard652327a2003-09-29 18:02:38 +00005887#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005888
5889/**
5890 * xmlHasProp:
5891 * @node: the node
5892 * @name: the attribute name
5893 *
5894 * Search an attribute associated to a node
5895 * This function also looks in DTD attribute declaration for #FIXED or
5896 * default declaration values unless DTD use has been turned off.
5897 *
5898 * Returns the attribute or the attribute declaration or NULL if
5899 * neither was found.
5900 */
5901xmlAttrPtr
5902xmlHasProp(xmlNodePtr node, const xmlChar *name) {
5903 xmlAttrPtr prop;
5904 xmlDocPtr doc;
5905
5906 if ((node == NULL) || (name == NULL)) return(NULL);
5907 /*
5908 * Check on the properties attached to the node
5909 */
5910 prop = node->properties;
5911 while (prop != NULL) {
5912 if (xmlStrEqual(prop->name, name)) {
5913 return(prop);
5914 }
5915 prop = prop->next;
5916 }
5917 if (!xmlCheckDTD) return(NULL);
5918
5919 /*
5920 * Check if there is a default declaration in the internal
5921 * or external subsets
5922 */
5923 doc = node->doc;
5924 if (doc != NULL) {
5925 xmlAttributePtr attrDecl;
5926 if (doc->intSubset != NULL) {
5927 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
5928 if ((attrDecl == NULL) && (doc->extSubset != NULL))
5929 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
Daniel Veillard75eb1ad2003-07-07 14:42:44 +00005930 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
5931 /* return attribute declaration only if a default value is given
5932 (that includes #FIXED declarations) */
Owen Taylor3473f882001-02-23 17:55:21 +00005933 return((xmlAttrPtr) attrDecl);
5934 }
5935 }
5936 return(NULL);
5937}
5938
5939/**
Daniel Veillarde95e2392001-06-06 10:46:28 +00005940 * xmlHasNsProp:
5941 * @node: the node
5942 * @name: the attribute name
Daniel Veillardca2366a2001-06-11 12:09:01 +00005943 * @nameSpace: the URI of the namespace
Daniel Veillarde95e2392001-06-06 10:46:28 +00005944 *
5945 * Search for an attribute associated to a node
5946 * This attribute has to be anchored in the namespace specified.
5947 * This does the entity substitution.
5948 * This function looks in DTD attribute declaration for #FIXED or
5949 * default declaration values unless DTD use has been turned off.
William M. Brack2c228442004-10-03 04:10:00 +00005950 * Note that a namespace of NULL indicates to use the default namespace.
Daniel Veillarde95e2392001-06-06 10:46:28 +00005951 *
5952 * Returns the attribute or the attribute declaration or NULL
5953 * if neither was found.
5954 */
5955xmlAttrPtr
Daniel Veillardca2366a2001-06-11 12:09:01 +00005956xmlHasNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
Daniel Veillarde95e2392001-06-06 10:46:28 +00005957 xmlAttrPtr prop;
Daniel Veillard652327a2003-09-29 18:02:38 +00005958#ifdef LIBXML_TREE_ENABLED
Daniel Veillarde95e2392001-06-06 10:46:28 +00005959 xmlDocPtr doc;
Daniel Veillard652327a2003-09-29 18:02:38 +00005960#endif /* LIBXML_TREE_ENABLED */
Daniel Veillarde95e2392001-06-06 10:46:28 +00005961
5962 if (node == NULL)
5963 return(NULL);
5964
5965 prop = node->properties;
Daniel Veillarde95e2392001-06-06 10:46:28 +00005966 while (prop != NULL) {
5967 /*
5968 * One need to have
5969 * - same attribute names
5970 * - and the attribute carrying that namespace
Daniel Veillarde95e2392001-06-06 10:46:28 +00005971 */
William M. Brack2c228442004-10-03 04:10:00 +00005972 if (xmlStrEqual(prop->name, name)) {
5973 if (((prop->ns != NULL) &&
5974 (xmlStrEqual(prop->ns->href, nameSpace))) ||
5975 ((prop->ns == NULL) && (nameSpace == NULL))) {
5976 return(prop);
5977 }
Daniel Veillarde95e2392001-06-06 10:46:28 +00005978 }
5979 prop = prop->next;
5980 }
5981 if (!xmlCheckDTD) return(NULL);
5982
Daniel Veillard652327a2003-09-29 18:02:38 +00005983#ifdef LIBXML_TREE_ENABLED
Daniel Veillarde95e2392001-06-06 10:46:28 +00005984 /*
5985 * Check if there is a default declaration in the internal
5986 * or external subsets
5987 */
5988 doc = node->doc;
5989 if (doc != NULL) {
5990 if (doc->intSubset != NULL) {
Daniel Veillardef6c46f2002-03-07 22:21:56 +00005991 xmlAttributePtr attrDecl = NULL;
5992 xmlNsPtr *nsList, *cur;
5993 xmlChar *ename;
Daniel Veillarde95e2392001-06-06 10:46:28 +00005994
Daniel Veillardef6c46f2002-03-07 22:21:56 +00005995 nsList = xmlGetNsList(node->doc, node);
5996 if (nsList == NULL)
5997 return(NULL);
5998 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
5999 ename = xmlStrdup(node->ns->prefix);
6000 ename = xmlStrcat(ename, BAD_CAST ":");
6001 ename = xmlStrcat(ename, node->name);
6002 } else {
6003 ename = xmlStrdup(node->name);
Daniel Veillarde95e2392001-06-06 10:46:28 +00006004 }
Daniel Veillardef6c46f2002-03-07 22:21:56 +00006005 if (ename == NULL) {
6006 xmlFree(nsList);
6007 return(NULL);
6008 }
6009
William M. Brack2c228442004-10-03 04:10:00 +00006010 if (nameSpace == NULL) {
6011 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, ename,
6012 name, NULL);
6013 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
6014 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, ename,
6015 name, NULL);
Daniel Veillardef6c46f2002-03-07 22:21:56 +00006016 }
William M. Brack2c228442004-10-03 04:10:00 +00006017 } else {
6018 cur = nsList;
6019 while (*cur != NULL) {
6020 if (xmlStrEqual((*cur)->href, nameSpace)) {
6021 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, ename,
6022 name, (*cur)->prefix);
6023 if ((attrDecl == NULL) && (doc->extSubset != NULL))
6024 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, ename,
6025 name, (*cur)->prefix);
6026 }
6027 cur++;
6028 }
Daniel Veillardef6c46f2002-03-07 22:21:56 +00006029 }
6030 xmlFree(nsList);
6031 xmlFree(ename);
6032 return((xmlAttrPtr) attrDecl);
Daniel Veillarde95e2392001-06-06 10:46:28 +00006033 }
6034 }
Daniel Veillard652327a2003-09-29 18:02:38 +00006035#endif /* LIBXML_TREE_ENABLED */
Daniel Veillarde95e2392001-06-06 10:46:28 +00006036 return(NULL);
6037}
6038
6039/**
Owen Taylor3473f882001-02-23 17:55:21 +00006040 * xmlGetProp:
6041 * @node: the node
6042 * @name: the attribute name
6043 *
6044 * Search and get the value of an attribute associated to a node
6045 * This does the entity substitution.
6046 * This function looks in DTD attribute declaration for #FIXED or
6047 * default declaration values unless DTD use has been turned off.
Daniel Veillard784b9352003-02-16 15:50:27 +00006048 * NOTE: this function acts independently of namespaces associated
Daniel Veillard71531f32003-02-05 13:19:53 +00006049 * to the attribute. Use xmlGetNsProp() or xmlGetNoNsProp()
6050 * for namespace aware processing.
Owen Taylor3473f882001-02-23 17:55:21 +00006051 *
6052 * Returns the attribute value or NULL if not found.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00006053 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00006054 */
6055xmlChar *
6056xmlGetProp(xmlNodePtr node, const xmlChar *name) {
6057 xmlAttrPtr prop;
6058 xmlDocPtr doc;
6059
6060 if ((node == NULL) || (name == NULL)) return(NULL);
6061 /*
6062 * Check on the properties attached to the node
6063 */
6064 prop = node->properties;
6065 while (prop != NULL) {
6066 if (xmlStrEqual(prop->name, name)) {
6067 xmlChar *ret;
6068
6069 ret = xmlNodeListGetString(node->doc, prop->children, 1);
6070 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
6071 return(ret);
6072 }
6073 prop = prop->next;
6074 }
6075 if (!xmlCheckDTD) return(NULL);
6076
6077 /*
6078 * Check if there is a default declaration in the internal
6079 * or external subsets
6080 */
6081 doc = node->doc;
6082 if (doc != NULL) {
6083 xmlAttributePtr attrDecl;
6084 if (doc->intSubset != NULL) {
6085 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
6086 if ((attrDecl == NULL) && (doc->extSubset != NULL))
6087 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
Daniel Veillard75eb1ad2003-07-07 14:42:44 +00006088 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6089 /* return attribute declaration only if a default value is given
6090 (that includes #FIXED declarations) */
Owen Taylor3473f882001-02-23 17:55:21 +00006091 return(xmlStrdup(attrDecl->defaultValue));
6092 }
6093 }
6094 return(NULL);
6095}
6096
6097/**
Daniel Veillard71531f32003-02-05 13:19:53 +00006098 * xmlGetNoNsProp:
6099 * @node: the node
6100 * @name: the attribute name
6101 *
6102 * Search and get the value of an attribute associated to a node
6103 * This does the entity substitution.
6104 * This function looks in DTD attribute declaration for #FIXED or
6105 * default declaration values unless DTD use has been turned off.
6106 * This function is similar to xmlGetProp except it will accept only
6107 * an attribute in no namespace.
6108 *
6109 * Returns the attribute value or NULL if not found.
6110 * It's up to the caller to free the memory with xmlFree().
6111 */
6112xmlChar *
6113xmlGetNoNsProp(xmlNodePtr node, const xmlChar *name) {
6114 xmlAttrPtr prop;
6115 xmlDocPtr doc;
6116
6117 if ((node == NULL) || (name == NULL)) return(NULL);
6118 /*
6119 * Check on the properties attached to the node
6120 */
6121 prop = node->properties;
6122 while (prop != NULL) {
6123 if ((prop->ns == NULL) && (xmlStrEqual(prop->name, name))) {
6124 xmlChar *ret;
6125
6126 ret = xmlNodeListGetString(node->doc, prop->children, 1);
6127 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
6128 return(ret);
6129 }
6130 prop = prop->next;
6131 }
6132 if (!xmlCheckDTD) return(NULL);
6133
6134 /*
6135 * Check if there is a default declaration in the internal
6136 * or external subsets
6137 */
6138 doc = node->doc;
6139 if (doc != NULL) {
6140 xmlAttributePtr attrDecl;
6141 if (doc->intSubset != NULL) {
6142 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
6143 if ((attrDecl == NULL) && (doc->extSubset != NULL))
6144 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
Daniel Veillard75eb1ad2003-07-07 14:42:44 +00006145 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6146 /* return attribute declaration only if a default value is given
6147 (that includes #FIXED declarations) */
Daniel Veillard71531f32003-02-05 13:19:53 +00006148 return(xmlStrdup(attrDecl->defaultValue));
6149 }
6150 }
6151 return(NULL);
6152}
6153
6154/**
Owen Taylor3473f882001-02-23 17:55:21 +00006155 * xmlGetNsProp:
6156 * @node: the node
6157 * @name: the attribute name
Daniel Veillardca2366a2001-06-11 12:09:01 +00006158 * @nameSpace: the URI of the namespace
Owen Taylor3473f882001-02-23 17:55:21 +00006159 *
6160 * Search and get the value of an attribute associated to a node
6161 * This attribute has to be anchored in the namespace specified.
6162 * This does the entity substitution.
6163 * This function looks in DTD attribute declaration for #FIXED or
6164 * default declaration values unless DTD use has been turned off.
6165 *
6166 * Returns the attribute value or NULL if not found.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00006167 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00006168 */
6169xmlChar *
Daniel Veillardca2366a2001-06-11 12:09:01 +00006170xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
Owen Taylor3473f882001-02-23 17:55:21 +00006171 xmlAttrPtr prop;
6172 xmlDocPtr doc;
6173 xmlNsPtr ns;
6174
6175 if (node == NULL)
6176 return(NULL);
6177
6178 prop = node->properties;
Daniel Veillardca2366a2001-06-11 12:09:01 +00006179 if (nameSpace == NULL)
Daniel Veillard71531f32003-02-05 13:19:53 +00006180 return(xmlGetNoNsProp(node, name));
Owen Taylor3473f882001-02-23 17:55:21 +00006181 while (prop != NULL) {
6182 /*
6183 * One need to have
6184 * - same attribute names
6185 * - and the attribute carrying that namespace
Owen Taylor3473f882001-02-23 17:55:21 +00006186 */
6187 if ((xmlStrEqual(prop->name, name)) &&
Daniel Veillarde8fc08e2001-06-07 19:35:47 +00006188 ((prop->ns != NULL) &&
Daniel Veillardca2366a2001-06-11 12:09:01 +00006189 (xmlStrEqual(prop->ns->href, nameSpace)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00006190 xmlChar *ret;
6191
6192 ret = xmlNodeListGetString(node->doc, prop->children, 1);
6193 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
6194 return(ret);
6195 }
6196 prop = prop->next;
6197 }
6198 if (!xmlCheckDTD) return(NULL);
6199
6200 /*
6201 * Check if there is a default declaration in the internal
6202 * or external subsets
6203 */
6204 doc = node->doc;
6205 if (doc != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00006206 if (doc->intSubset != NULL) {
Daniel Veillard5792e162001-04-30 17:44:45 +00006207 xmlAttributePtr attrDecl;
6208
Owen Taylor3473f882001-02-23 17:55:21 +00006209 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
6210 if ((attrDecl == NULL) && (doc->extSubset != NULL))
6211 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
6212
6213 if ((attrDecl != NULL) && (attrDecl->prefix != NULL)) {
6214 /*
6215 * The DTD declaration only allows a prefix search
6216 */
6217 ns = xmlSearchNs(doc, node, attrDecl->prefix);
Daniel Veillardca2366a2001-06-11 12:09:01 +00006218 if ((ns != NULL) && (xmlStrEqual(ns->href, nameSpace)))
Owen Taylor3473f882001-02-23 17:55:21 +00006219 return(xmlStrdup(attrDecl->defaultValue));
6220 }
6221 }
6222 }
6223 return(NULL);
6224}
6225
Daniel Veillard2156d432004-03-04 15:59:36 +00006226#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
6227/**
6228 * xmlUnsetProp:
6229 * @node: the node
6230 * @name: the attribute name
6231 *
6232 * Remove an attribute carried by a node.
6233 * Returns 0 if successful, -1 if not found
6234 */
6235int
6236xmlUnsetProp(xmlNodePtr node, const xmlChar *name) {
6237 xmlAttrPtr prop, prev = NULL;;
6238
6239 if ((node == NULL) || (name == NULL))
6240 return(-1);
6241 prop = node->properties;
6242 while (prop != NULL) {
6243 if ((xmlStrEqual(prop->name, name)) &&
6244 (prop->ns == NULL)) {
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00006245 xmlUnlinkNode((xmlNodePtr) prop);
Daniel Veillard2156d432004-03-04 15:59:36 +00006246 xmlFreeProp(prop);
6247 return(0);
6248 }
6249 prev = prop;
6250 prop = prop->next;
6251 }
6252 return(-1);
6253}
6254
6255/**
6256 * xmlUnsetNsProp:
6257 * @node: the node
6258 * @ns: the namespace definition
6259 * @name: the attribute name
6260 *
6261 * Remove an attribute carried by a node.
6262 * Returns 0 if successful, -1 if not found
6263 */
6264int
6265xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) {
Daniel Veillard27f20102004-11-05 11:50:11 +00006266 xmlAttrPtr prop, prev = NULL;;
Daniel Veillard2156d432004-03-04 15:59:36 +00006267
6268 if ((node == NULL) || (name == NULL))
6269 return(-1);
Daniel Veillard27f20102004-11-05 11:50:11 +00006270 prop = node->properties;
Daniel Veillard2156d432004-03-04 15:59:36 +00006271 if (ns == NULL)
6272 return(xmlUnsetProp(node, name));
6273 if (ns->href == NULL)
6274 return(-1);
6275 while (prop != NULL) {
6276 if ((xmlStrEqual(prop->name, name)) &&
6277 (prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))) {
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00006278 xmlUnlinkNode((xmlNodePtr) prop);
Daniel Veillard2156d432004-03-04 15:59:36 +00006279 xmlFreeProp(prop);
6280 return(0);
6281 }
6282 prev = prop;
6283 prop = prop->next;
6284 }
6285 return(-1);
6286}
6287#endif
6288
6289#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_HTML_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00006290/**
6291 * xmlSetProp:
6292 * @node: the node
6293 * @name: the attribute name
6294 * @value: the attribute value
6295 *
6296 * Set (or reset) an attribute carried by a node.
6297 * Returns the attribute pointer.
6298 */
6299xmlAttrPtr
6300xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
Daniel Veillard4855c8c2001-11-25 10:35:25 +00006301 xmlAttrPtr prop;
6302 xmlDocPtr doc;
Owen Taylor3473f882001-02-23 17:55:21 +00006303
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006304 if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE))
Owen Taylor3473f882001-02-23 17:55:21 +00006305 return(NULL);
6306 doc = node->doc;
Daniel Veillard4855c8c2001-11-25 10:35:25 +00006307 prop = node->properties;
Owen Taylor3473f882001-02-23 17:55:21 +00006308 while (prop != NULL) {
Daniel Veillard75bea542001-05-11 17:41:21 +00006309 if ((xmlStrEqual(prop->name, name)) &&
6310 (prop->ns == NULL)){
Daniel Veillard4855c8c2001-11-25 10:35:25 +00006311 xmlNodePtr oldprop = prop->children;
6312
Owen Taylor3473f882001-02-23 17:55:21 +00006313 prop->children = NULL;
6314 prop->last = NULL;
6315 if (value != NULL) {
6316 xmlChar *buffer;
6317 xmlNodePtr tmp;
6318
6319 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
6320 prop->children = xmlStringGetNodeList(node->doc, buffer);
6321 prop->last = NULL;
6322 prop->doc = doc;
6323 tmp = prop->children;
6324 while (tmp != NULL) {
6325 tmp->parent = (xmlNodePtr) prop;
6326 tmp->doc = doc;
6327 if (tmp->next == NULL)
6328 prop->last = tmp;
6329 tmp = tmp->next;
6330 }
6331 xmlFree(buffer);
Daniel Veillard75bea542001-05-11 17:41:21 +00006332 }
Daniel Veillard4855c8c2001-11-25 10:35:25 +00006333 if (oldprop != NULL)
6334 xmlFreeNodeList(oldprop);
Owen Taylor3473f882001-02-23 17:55:21 +00006335 return(prop);
6336 }
6337 prop = prop->next;
6338 }
6339 prop = xmlNewProp(node, name, value);
6340 return(prop);
6341}
6342
6343/**
6344 * xmlSetNsProp:
6345 * @node: the node
6346 * @ns: the namespace definition
6347 * @name: the attribute name
6348 * @value: the attribute value
6349 *
6350 * Set (or reset) an attribute carried by a node.
6351 * The ns structure must be in scope, this is not checked.
6352 *
6353 * Returns the attribute pointer.
6354 */
6355xmlAttrPtr
6356xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
6357 const xmlChar *value) {
6358 xmlAttrPtr prop;
6359
Daniel Veillardb6b36d32005-02-09 16:48:53 +00006360 if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE))
Owen Taylor3473f882001-02-23 17:55:21 +00006361 return(NULL);
6362
6363 if (ns == NULL)
6364 return(xmlSetProp(node, name, value));
6365 if (ns->href == NULL)
6366 return(NULL);
6367 prop = node->properties;
6368
6369 while (prop != NULL) {
6370 /*
6371 * One need to have
6372 * - same attribute names
6373 * - and the attribute carrying that namespace
Owen Taylor3473f882001-02-23 17:55:21 +00006374 */
6375 if ((xmlStrEqual(prop->name, name)) &&
Daniel Veillarda57c26e2002-08-01 12:52:24 +00006376 (prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))) {
Owen Taylor3473f882001-02-23 17:55:21 +00006377 if (prop->children != NULL)
6378 xmlFreeNodeList(prop->children);
6379 prop->children = NULL;
6380 prop->last = NULL;
6381 prop->ns = ns;
6382 if (value != NULL) {
6383 xmlChar *buffer;
6384 xmlNodePtr tmp;
6385
6386 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
6387 prop->children = xmlStringGetNodeList(node->doc, buffer);
6388 prop->last = NULL;
6389 tmp = prop->children;
6390 while (tmp != NULL) {
6391 tmp->parent = (xmlNodePtr) prop;
6392 if (tmp->next == NULL)
6393 prop->last = tmp;
6394 tmp = tmp->next;
6395 }
6396 xmlFree(buffer);
6397 }
6398 return(prop);
6399 }
6400 prop = prop->next;
6401 }
6402 prop = xmlNewNsProp(node, ns, name, value);
6403 return(prop);
6404}
6405
Daniel Veillard652327a2003-09-29 18:02:38 +00006406#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard75bea542001-05-11 17:41:21 +00006407
6408/**
Owen Taylor3473f882001-02-23 17:55:21 +00006409 * xmlNodeIsText:
6410 * @node: the node
6411 *
6412 * Is this node a Text node ?
6413 * Returns 1 yes, 0 no
6414 */
6415int
6416xmlNodeIsText(xmlNodePtr node) {
6417 if (node == NULL) return(0);
6418
6419 if (node->type == XML_TEXT_NODE) return(1);
6420 return(0);
6421}
6422
6423/**
6424 * xmlIsBlankNode:
6425 * @node: the node
6426 *
6427 * Checks whether this node is an empty or whitespace only
6428 * (and possibly ignorable) text-node.
6429 *
6430 * Returns 1 yes, 0 no
6431 */
6432int
6433xmlIsBlankNode(xmlNodePtr node) {
6434 const xmlChar *cur;
6435 if (node == NULL) return(0);
6436
Daniel Veillard7db37732001-07-12 01:20:08 +00006437 if ((node->type != XML_TEXT_NODE) &&
6438 (node->type != XML_CDATA_SECTION_NODE))
6439 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006440 if (node->content == NULL) return(1);
Owen Taylor3473f882001-02-23 17:55:21 +00006441 cur = node->content;
Owen Taylor3473f882001-02-23 17:55:21 +00006442 while (*cur != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00006443 if (!IS_BLANK_CH(*cur)) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006444 cur++;
6445 }
6446
6447 return(1);
6448}
6449
6450/**
6451 * xmlTextConcat:
6452 * @node: the node
6453 * @content: the content
Daniel Veillard60087f32001-10-10 09:45:09 +00006454 * @len: @content length
Owen Taylor3473f882001-02-23 17:55:21 +00006455 *
6456 * Concat the given string at the end of the existing node content
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006457 *
6458 * Returns -1 in case of error, 0 otherwise
Owen Taylor3473f882001-02-23 17:55:21 +00006459 */
6460
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006461int
Owen Taylor3473f882001-02-23 17:55:21 +00006462xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006463 if (node == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006464
6465 if ((node->type != XML_TEXT_NODE) &&
6466 (node->type != XML_CDATA_SECTION_NODE)) {
6467#ifdef DEBUG_TREE
6468 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006469 "xmlTextConcat: node is not text nor CDATA\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006470#endif
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006471 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006472 }
William M. Brack7762bb12004-01-04 14:49:01 +00006473 /* need to check if content is currently in the dictionary */
6474 if ((node->doc != NULL) && (node->doc->dict != NULL) &&
6475 xmlDictOwns(node->doc->dict, node->content)) {
6476 node->content = xmlStrncatNew(node->content, content, len);
6477 } else {
6478 node->content = xmlStrncat(node->content, content, len);
6479 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006480 if (node->content == NULL)
6481 return(-1);
6482 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006483}
6484
6485/************************************************************************
6486 * *
6487 * Output : to a FILE or in memory *
6488 * *
6489 ************************************************************************/
6490
Owen Taylor3473f882001-02-23 17:55:21 +00006491/**
6492 * xmlBufferCreate:
6493 *
6494 * routine to create an XML buffer.
6495 * returns the new structure.
6496 */
6497xmlBufferPtr
6498xmlBufferCreate(void) {
6499 xmlBufferPtr ret;
6500
6501 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6502 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006503 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006504 return(NULL);
6505 }
6506 ret->use = 0;
Daniel Veillarde356c282001-03-10 12:32:04 +00006507 ret->size = xmlDefaultBufferSize;
Owen Taylor3473f882001-02-23 17:55:21 +00006508 ret->alloc = xmlBufferAllocScheme;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00006509 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00006510 if (ret->content == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006511 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006512 xmlFree(ret);
6513 return(NULL);
6514 }
6515 ret->content[0] = 0;
6516 return(ret);
6517}
6518
6519/**
6520 * xmlBufferCreateSize:
6521 * @size: initial size of buffer
6522 *
6523 * routine to create an XML buffer.
6524 * returns the new structure.
6525 */
6526xmlBufferPtr
6527xmlBufferCreateSize(size_t size) {
6528 xmlBufferPtr ret;
6529
6530 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6531 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006532 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006533 return(NULL);
6534 }
6535 ret->use = 0;
6536 ret->alloc = xmlBufferAllocScheme;
6537 ret->size = (size ? size+2 : 0); /* +1 for ending null */
6538 if (ret->size){
Daniel Veillard3c908dc2003-04-19 00:07:51 +00006539 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00006540 if (ret->content == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006541 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006542 xmlFree(ret);
6543 return(NULL);
6544 }
6545 ret->content[0] = 0;
6546 } else
6547 ret->content = NULL;
6548 return(ret);
6549}
6550
6551/**
Daniel Veillard53350552003-09-18 13:35:51 +00006552 * xmlBufferCreateStatic:
6553 * @mem: the memory area
6554 * @size: the size in byte
6555 *
MST 2003 John Flecka0e7e932003-12-19 03:13:47 +00006556 * routine to create an XML buffer from an immutable memory area.
6557 * The area won't be modified nor copied, and is expected to be
Daniel Veillard53350552003-09-18 13:35:51 +00006558 * present until the end of the buffer lifetime.
6559 *
6560 * returns the new structure.
6561 */
6562xmlBufferPtr
6563xmlBufferCreateStatic(void *mem, size_t size) {
6564 xmlBufferPtr ret;
6565
6566 if ((mem == NULL) || (size == 0))
6567 return(NULL);
6568
6569 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6570 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006571 xmlTreeErrMemory("creating buffer");
Daniel Veillard53350552003-09-18 13:35:51 +00006572 return(NULL);
6573 }
6574 ret->use = size;
6575 ret->size = size;
6576 ret->alloc = XML_BUFFER_ALLOC_IMMUTABLE;
6577 ret->content = (xmlChar *) mem;
6578 return(ret);
6579}
6580
6581/**
Owen Taylor3473f882001-02-23 17:55:21 +00006582 * xmlBufferSetAllocationScheme:
Daniel Veillardbd9afb52002-09-25 22:25:35 +00006583 * @buf: the buffer to tune
Owen Taylor3473f882001-02-23 17:55:21 +00006584 * @scheme: allocation scheme to use
6585 *
6586 * Sets the allocation scheme for this buffer
6587 */
6588void
6589xmlBufferSetAllocationScheme(xmlBufferPtr buf,
6590 xmlBufferAllocationScheme scheme) {
6591 if (buf == NULL) {
6592#ifdef DEBUG_BUFFER
6593 xmlGenericError(xmlGenericErrorContext,
6594 "xmlBufferSetAllocationScheme: buf == NULL\n");
6595#endif
6596 return;
6597 }
Daniel Veillard53350552003-09-18 13:35:51 +00006598 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00006599
6600 buf->alloc = scheme;
6601}
6602
6603/**
6604 * xmlBufferFree:
6605 * @buf: the buffer to free
6606 *
Daniel Veillard9d06d302002-01-22 18:15:52 +00006607 * Frees an XML buffer. It frees both the content and the structure which
6608 * encapsulate it.
Owen Taylor3473f882001-02-23 17:55:21 +00006609 */
6610void
6611xmlBufferFree(xmlBufferPtr buf) {
6612 if (buf == NULL) {
6613#ifdef DEBUG_BUFFER
6614 xmlGenericError(xmlGenericErrorContext,
6615 "xmlBufferFree: buf == NULL\n");
6616#endif
6617 return;
6618 }
Daniel Veillard53350552003-09-18 13:35:51 +00006619
6620 if ((buf->content != NULL) &&
6621 (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006622 xmlFree(buf->content);
6623 }
Owen Taylor3473f882001-02-23 17:55:21 +00006624 xmlFree(buf);
6625}
6626
6627/**
6628 * xmlBufferEmpty:
6629 * @buf: the buffer
6630 *
6631 * empty a buffer.
6632 */
6633void
6634xmlBufferEmpty(xmlBufferPtr buf) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00006635 if (buf == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +00006636 if (buf->content == NULL) return;
6637 buf->use = 0;
Daniel Veillard53350552003-09-18 13:35:51 +00006638 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00006639 buf->content = BAD_CAST "";
Daniel Veillard53350552003-09-18 13:35:51 +00006640 } else {
6641 memset(buf->content, 0, buf->size);
6642 }
Owen Taylor3473f882001-02-23 17:55:21 +00006643}
6644
6645/**
6646 * xmlBufferShrink:
6647 * @buf: the buffer to dump
6648 * @len: the number of xmlChar to remove
6649 *
6650 * Remove the beginning of an XML buffer.
6651 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006652 * Returns the number of #xmlChar removed, or -1 in case of failure.
Owen Taylor3473f882001-02-23 17:55:21 +00006653 */
6654int
6655xmlBufferShrink(xmlBufferPtr buf, unsigned int len) {
Daniel Veillard3d97e662004-11-04 10:49:00 +00006656 if (buf == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006657 if (len == 0) return(0);
6658 if (len > buf->use) return(-1);
6659
6660 buf->use -= len;
Daniel Veillard53350552003-09-18 13:35:51 +00006661 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) {
6662 buf->content += len;
6663 } else {
6664 memmove(buf->content, &buf->content[len], buf->use * sizeof(xmlChar));
6665 buf->content[buf->use] = 0;
6666 }
Owen Taylor3473f882001-02-23 17:55:21 +00006667 return(len);
6668}
6669
6670/**
6671 * xmlBufferGrow:
6672 * @buf: the buffer
6673 * @len: the minimum free size to allocate
6674 *
6675 * Grow the available space of an XML buffer.
6676 *
6677 * Returns the new available space or -1 in case of error
6678 */
6679int
6680xmlBufferGrow(xmlBufferPtr buf, unsigned int len) {
6681 int size;
6682 xmlChar *newbuf;
6683
Daniel Veillard3d97e662004-11-04 10:49:00 +00006684 if (buf == NULL) return(-1);
6685
Daniel Veillard53350552003-09-18 13:35:51 +00006686 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006687 if (len + buf->use < buf->size) return(0);
6688
William M. Brack30fe43f2004-07-26 18:00:58 +00006689/*
6690 * Windows has a BIG problem on realloc timing, so we try to double
6691 * the buffer size (if that's enough) (bug 146697)
6692 */
6693#ifdef WIN32
6694 if (buf->size > len)
6695 size = buf->size * 2;
6696 else
6697 size = buf->use + len + 100;
6698#else
Owen Taylor3473f882001-02-23 17:55:21 +00006699 size = buf->use + len + 100;
William M. Brack30fe43f2004-07-26 18:00:58 +00006700#endif
Owen Taylor3473f882001-02-23 17:55:21 +00006701
6702 newbuf = (xmlChar *) xmlRealloc(buf->content, size);
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006703 if (newbuf == NULL) {
6704 xmlTreeErrMemory("growing buffer");
6705 return(-1);
6706 }
Owen Taylor3473f882001-02-23 17:55:21 +00006707 buf->content = newbuf;
6708 buf->size = size;
6709 return(buf->size - buf->use);
6710}
6711
6712/**
6713 * xmlBufferDump:
6714 * @file: the file output
6715 * @buf: the buffer to dump
6716 *
6717 * Dumps an XML buffer to a FILE *.
Daniel Veillardd1640922001-12-17 15:30:10 +00006718 * Returns the number of #xmlChar written
Owen Taylor3473f882001-02-23 17:55:21 +00006719 */
6720int
6721xmlBufferDump(FILE *file, xmlBufferPtr buf) {
6722 int ret;
6723
6724 if (buf == NULL) {
6725#ifdef DEBUG_BUFFER
6726 xmlGenericError(xmlGenericErrorContext,
6727 "xmlBufferDump: buf == NULL\n");
6728#endif
6729 return(0);
6730 }
6731 if (buf->content == NULL) {
6732#ifdef DEBUG_BUFFER
6733 xmlGenericError(xmlGenericErrorContext,
6734 "xmlBufferDump: buf->content == NULL\n");
6735#endif
6736 return(0);
6737 }
Daniel Veillardcd337f02001-11-22 18:20:37 +00006738 if (file == NULL)
6739 file = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00006740 ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
6741 return(ret);
6742}
6743
6744/**
6745 * xmlBufferContent:
6746 * @buf: the buffer
6747 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00006748 * Function to extract the content of a buffer
6749 *
Owen Taylor3473f882001-02-23 17:55:21 +00006750 * Returns the internal content
6751 */
6752
Daniel Veillard5e2dace2001-07-18 19:30:27 +00006753const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00006754xmlBufferContent(const xmlBufferPtr buf)
6755{
6756 if(!buf)
6757 return NULL;
6758
6759 return buf->content;
6760}
6761
6762/**
6763 * xmlBufferLength:
6764 * @buf: the buffer
6765 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00006766 * Function to get the length of a buffer
6767 *
Owen Taylor3473f882001-02-23 17:55:21 +00006768 * Returns the length of data in the internal content
6769 */
6770
6771int
6772xmlBufferLength(const xmlBufferPtr buf)
6773{
6774 if(!buf)
6775 return 0;
6776
6777 return buf->use;
6778}
6779
6780/**
6781 * xmlBufferResize:
6782 * @buf: the buffer to resize
6783 * @size: the desired size
6784 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006785 * Resize a buffer to accommodate minimum size of @size.
Owen Taylor3473f882001-02-23 17:55:21 +00006786 *
6787 * Returns 0 in case of problems, 1 otherwise
6788 */
6789int
6790xmlBufferResize(xmlBufferPtr buf, unsigned int size)
6791{
6792 unsigned int newSize;
6793 xmlChar* rebuf = NULL;
6794
Daniel Veillardd005b9e2004-11-03 17:07:05 +00006795 if (buf == NULL)
6796 return(0);
6797
Daniel Veillard53350552003-09-18 13:35:51 +00006798 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
6799
Owen Taylor3473f882001-02-23 17:55:21 +00006800 /* Don't resize if we don't have to */
6801 if (size < buf->size)
6802 return 1;
6803
6804 /* figure out new size */
6805 switch (buf->alloc){
6806 case XML_BUFFER_ALLOC_DOUBLEIT:
Daniel Veillardbf629492004-04-20 22:20:59 +00006807 /*take care of empty case*/
6808 newSize = (buf->size ? buf->size*2 : size + 10);
Owen Taylor3473f882001-02-23 17:55:21 +00006809 while (size > newSize) newSize *= 2;
6810 break;
6811 case XML_BUFFER_ALLOC_EXACT:
6812 newSize = size+10;
6813 break;
6814 default:
6815 newSize = size+10;
6816 break;
6817 }
6818
6819 if (buf->content == NULL)
Daniel Veillard3c908dc2003-04-19 00:07:51 +00006820 rebuf = (xmlChar *) xmlMallocAtomic(newSize * sizeof(xmlChar));
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006821 else if (buf->size - buf->use < 100) {
Owen Taylor3473f882001-02-23 17:55:21 +00006822 rebuf = (xmlChar *) xmlRealloc(buf->content,
6823 newSize * sizeof(xmlChar));
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006824 } else {
6825 /*
6826 * if we are reallocating a buffer far from being full, it's
6827 * better to make a new allocation and copy only the used range
6828 * and free the old one.
6829 */
6830 rebuf = (xmlChar *) xmlMallocAtomic(newSize * sizeof(xmlChar));
6831 if (rebuf != NULL) {
6832 memcpy(rebuf, buf->content, buf->use);
6833 xmlFree(buf->content);
William M. Brack42331a92004-07-29 07:07:16 +00006834 rebuf[buf->use] = 0;
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006835 }
6836 }
Owen Taylor3473f882001-02-23 17:55:21 +00006837 if (rebuf == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006838 xmlTreeErrMemory("growing buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006839 return 0;
6840 }
6841 buf->content = rebuf;
6842 buf->size = newSize;
6843
6844 return 1;
6845}
6846
6847/**
6848 * xmlBufferAdd:
6849 * @buf: the buffer to dump
Daniel Veillardd1640922001-12-17 15:30:10 +00006850 * @str: the #xmlChar string
6851 * @len: the number of #xmlChar to add
Owen Taylor3473f882001-02-23 17:55:21 +00006852 *
Daniel Veillard60087f32001-10-10 09:45:09 +00006853 * Add a string range to an XML buffer. if len == -1, the length of
Owen Taylor3473f882001-02-23 17:55:21 +00006854 * str is recomputed.
William M. Bracka3215c72004-07-31 16:24:01 +00006855 *
6856 * Returns 0 successful, a positive error code number otherwise
6857 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00006858 */
William M. Bracka3215c72004-07-31 16:24:01 +00006859int
Owen Taylor3473f882001-02-23 17:55:21 +00006860xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) {
6861 unsigned int needSize;
6862
Daniel Veillardd005b9e2004-11-03 17:07:05 +00006863 if ((str == NULL) || (buf == NULL)) {
William M. Bracka3215c72004-07-31 16:24:01 +00006864 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006865 }
William M. Bracka3215c72004-07-31 16:24:01 +00006866 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006867 if (len < -1) {
6868#ifdef DEBUG_BUFFER
6869 xmlGenericError(xmlGenericErrorContext,
6870 "xmlBufferAdd: len < 0\n");
6871#endif
William M. Bracka3215c72004-07-31 16:24:01 +00006872 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006873 }
William M. Bracka3215c72004-07-31 16:24:01 +00006874 if (len == 0) return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006875
6876 if (len < 0)
6877 len = xmlStrlen(str);
6878
William M. Bracka3215c72004-07-31 16:24:01 +00006879 if (len <= 0) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006880
6881 needSize = buf->use + len + 2;
6882 if (needSize > buf->size){
6883 if (!xmlBufferResize(buf, needSize)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006884 xmlTreeErrMemory("growing buffer");
William M. Bracka3215c72004-07-31 16:24:01 +00006885 return XML_ERR_NO_MEMORY;
Owen Taylor3473f882001-02-23 17:55:21 +00006886 }
6887 }
6888
6889 memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
6890 buf->use += len;
6891 buf->content[buf->use] = 0;
William M. Bracka3215c72004-07-31 16:24:01 +00006892 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006893}
6894
6895/**
6896 * xmlBufferAddHead:
6897 * @buf: the buffer
Daniel Veillardd1640922001-12-17 15:30:10 +00006898 * @str: the #xmlChar string
6899 * @len: the number of #xmlChar to add
Owen Taylor3473f882001-02-23 17:55:21 +00006900 *
6901 * Add a string range to the beginning of an XML buffer.
Daniel Veillard60087f32001-10-10 09:45:09 +00006902 * if len == -1, the length of @str is recomputed.
William M. Bracka3215c72004-07-31 16:24:01 +00006903 *
6904 * Returns 0 successful, a positive error code number otherwise
6905 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00006906 */
William M. Bracka3215c72004-07-31 16:24:01 +00006907int
Owen Taylor3473f882001-02-23 17:55:21 +00006908xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) {
6909 unsigned int needSize;
6910
Daniel Veillardd005b9e2004-11-03 17:07:05 +00006911 if (buf == NULL)
6912 return(-1);
William M. Bracka3215c72004-07-31 16:24:01 +00006913 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006914 if (str == NULL) {
6915#ifdef DEBUG_BUFFER
6916 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006917 "xmlBufferAddHead: str == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006918#endif
William M. Bracka3215c72004-07-31 16:24:01 +00006919 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006920 }
6921 if (len < -1) {
6922#ifdef DEBUG_BUFFER
6923 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006924 "xmlBufferAddHead: len < 0\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006925#endif
William M. Bracka3215c72004-07-31 16:24:01 +00006926 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006927 }
William M. Bracka3215c72004-07-31 16:24:01 +00006928 if (len == 0) return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006929
6930 if (len < 0)
6931 len = xmlStrlen(str);
6932
William M. Bracka3215c72004-07-31 16:24:01 +00006933 if (len <= 0) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006934
6935 needSize = buf->use + len + 2;
6936 if (needSize > buf->size){
6937 if (!xmlBufferResize(buf, needSize)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006938 xmlTreeErrMemory("growing buffer");
William M. Bracka3215c72004-07-31 16:24:01 +00006939 return XML_ERR_NO_MEMORY;
Owen Taylor3473f882001-02-23 17:55:21 +00006940 }
6941 }
6942
6943 memmove(&buf->content[len], &buf->content[0], buf->use * sizeof(xmlChar));
6944 memmove(&buf->content[0], str, len * sizeof(xmlChar));
6945 buf->use += len;
6946 buf->content[buf->use] = 0;
William M. Bracka3215c72004-07-31 16:24:01 +00006947 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006948}
6949
6950/**
6951 * xmlBufferCat:
William M. Bracka3215c72004-07-31 16:24:01 +00006952 * @buf: the buffer to add to
Daniel Veillardd1640922001-12-17 15:30:10 +00006953 * @str: the #xmlChar string
Owen Taylor3473f882001-02-23 17:55:21 +00006954 *
6955 * Append a zero terminated string to an XML buffer.
William M. Bracka3215c72004-07-31 16:24:01 +00006956 *
6957 * Returns 0 successful, a positive error code number otherwise
6958 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00006959 */
William M. Bracka3215c72004-07-31 16:24:01 +00006960int
Owen Taylor3473f882001-02-23 17:55:21 +00006961xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00006962 if (buf == NULL)
6963 return(-1);
William M. Bracka3215c72004-07-31 16:24:01 +00006964 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
6965 if (str == NULL) return -1;
6966 return xmlBufferAdd(buf, str, -1);
Owen Taylor3473f882001-02-23 17:55:21 +00006967}
6968
6969/**
6970 * xmlBufferCCat:
6971 * @buf: the buffer to dump
6972 * @str: the C char string
6973 *
6974 * Append a zero terminated C string to an XML buffer.
William M. Bracka3215c72004-07-31 16:24:01 +00006975 *
6976 * Returns 0 successful, a positive error code number otherwise
6977 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00006978 */
William M. Bracka3215c72004-07-31 16:24:01 +00006979int
Owen Taylor3473f882001-02-23 17:55:21 +00006980xmlBufferCCat(xmlBufferPtr buf, const char *str) {
6981 const char *cur;
6982
Daniel Veillardd005b9e2004-11-03 17:07:05 +00006983 if (buf == NULL)
6984 return(-1);
William M. Bracka3215c72004-07-31 16:24:01 +00006985 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006986 if (str == NULL) {
6987#ifdef DEBUG_BUFFER
6988 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006989 "xmlBufferCCat: str == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006990#endif
William M. Bracka3215c72004-07-31 16:24:01 +00006991 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006992 }
6993 for (cur = str;*cur != 0;cur++) {
6994 if (buf->use + 10 >= buf->size) {
6995 if (!xmlBufferResize(buf, buf->use+10)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006996 xmlTreeErrMemory("growing buffer");
William M. Bracka3215c72004-07-31 16:24:01 +00006997 return XML_ERR_NO_MEMORY;
Owen Taylor3473f882001-02-23 17:55:21 +00006998 }
6999 }
7000 buf->content[buf->use++] = *cur;
7001 }
7002 buf->content[buf->use] = 0;
William M. Bracka3215c72004-07-31 16:24:01 +00007003 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007004}
7005
7006/**
7007 * xmlBufferWriteCHAR:
7008 * @buf: the XML buffer
7009 * @string: the string to add
7010 *
7011 * routine which manages and grows an output buffer. This one adds
7012 * xmlChars at the end of the buffer.
7013 */
7014void
Daniel Veillard53350552003-09-18 13:35:51 +00007015xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007016 if (buf == NULL)
7017 return;
Daniel Veillard53350552003-09-18 13:35:51 +00007018 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00007019 xmlBufferCat(buf, string);
7020}
7021
7022/**
7023 * xmlBufferWriteChar:
7024 * @buf: the XML buffer output
7025 * @string: the string to add
7026 *
7027 * routine which manage and grows an output buffer. This one add
7028 * C chars at the end of the array.
7029 */
7030void
7031xmlBufferWriteChar(xmlBufferPtr buf, const char *string) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007032 if (buf == NULL)
7033 return;
Daniel Veillard53350552003-09-18 13:35:51 +00007034 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00007035 xmlBufferCCat(buf, string);
7036}
7037
7038
7039/**
7040 * xmlBufferWriteQuotedString:
7041 * @buf: the XML buffer output
7042 * @string: the string to add
7043 *
7044 * routine which manage and grows an output buffer. This one writes
Daniel Veillardd1640922001-12-17 15:30:10 +00007045 * a quoted or double quoted #xmlChar string, checking first if it holds
Owen Taylor3473f882001-02-23 17:55:21 +00007046 * quote or double-quotes internally
7047 */
7048void
7049xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) {
Daniel Veillard39057f42003-08-04 01:33:43 +00007050 const xmlChar *cur, *base;
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007051 if (buf == NULL)
7052 return;
Daniel Veillard53350552003-09-18 13:35:51 +00007053 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Daniel Veillard39057f42003-08-04 01:33:43 +00007054 if (xmlStrchr(string, '\"')) {
Daniel Veillard20aa0fb2003-08-04 19:43:15 +00007055 if (xmlStrchr(string, '\'')) {
Owen Taylor3473f882001-02-23 17:55:21 +00007056#ifdef DEBUG_BUFFER
7057 xmlGenericError(xmlGenericErrorContext,
7058 "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n");
7059#endif
Daniel Veillard39057f42003-08-04 01:33:43 +00007060 xmlBufferCCat(buf, "\"");
7061 base = cur = string;
7062 while(*cur != 0){
7063 if(*cur == '"'){
7064 if (base != cur)
7065 xmlBufferAdd(buf, base, cur - base);
7066 xmlBufferAdd(buf, BAD_CAST "&quot;", 6);
7067 cur++;
7068 base = cur;
7069 }
7070 else {
7071 cur++;
7072 }
7073 }
7074 if (base != cur)
7075 xmlBufferAdd(buf, base, cur - base);
7076 xmlBufferCCat(buf, "\"");
Owen Taylor3473f882001-02-23 17:55:21 +00007077 }
Daniel Veillard39057f42003-08-04 01:33:43 +00007078 else{
7079 xmlBufferCCat(buf, "\'");
7080 xmlBufferCat(buf, string);
7081 xmlBufferCCat(buf, "\'");
7082 }
Owen Taylor3473f882001-02-23 17:55:21 +00007083 } else {
7084 xmlBufferCCat(buf, "\"");
7085 xmlBufferCat(buf, string);
7086 xmlBufferCCat(buf, "\"");
7087 }
7088}
7089
7090
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007091/**
7092 * xmlGetDocCompressMode:
7093 * @doc: the document
7094 *
7095 * get the compression ratio for a document, ZLIB based
7096 * Returns 0 (uncompressed) to 9 (max compression)
7097 */
7098int
7099xmlGetDocCompressMode (xmlDocPtr doc) {
7100 if (doc == NULL) return(-1);
7101 return(doc->compression);
7102}
7103
7104/**
7105 * xmlSetDocCompressMode:
7106 * @doc: the document
7107 * @mode: the compression ratio
7108 *
7109 * set the compression ratio for a document, ZLIB based
7110 * Correct values: 0 (uncompressed) to 9 (max compression)
7111 */
7112void
7113xmlSetDocCompressMode (xmlDocPtr doc, int mode) {
7114 if (doc == NULL) return;
7115 if (mode < 0) doc->compression = 0;
7116 else if (mode > 9) doc->compression = 9;
7117 else doc->compression = mode;
7118}
7119
7120/**
7121 * xmlGetCompressMode:
7122 *
7123 * get the default compression mode used, ZLIB based.
7124 * Returns 0 (uncompressed) to 9 (max compression)
7125 */
7126int
7127xmlGetCompressMode(void)
7128{
7129 return (xmlCompressMode);
7130}
7131
7132/**
7133 * xmlSetCompressMode:
7134 * @mode: the compression ratio
7135 *
7136 * set the default compression mode used, ZLIB based
7137 * Correct values: 0 (uncompressed) to 9 (max compression)
7138 */
7139void
7140xmlSetCompressMode(int mode) {
7141 if (mode < 0) xmlCompressMode = 0;
7142 else if (mode > 9) xmlCompressMode = 9;
7143 else xmlCompressMode = mode;
7144}
7145
Kasimier T. Buchcik4d9c9482005-06-27 15:04:46 +00007146/*
7147* xmlDOMWrapNewCtxt:
7148*
7149* Allocates and initializes a new DOM-wrapper context.
7150*
7151* Returns the xmlDOMWrapCtxtPtr or NULL in case of an internal errror.
7152*/
7153xmlDOMWrapCtxtPtr
7154xmlDOMWrapNewCtxt(void)
7155{
7156 xmlDOMWrapCtxtPtr ret;
7157
7158 ret = xmlMalloc(sizeof(xmlDOMWrapCtxt));
7159 if (ret == NULL) {
7160 xmlTreeErrMemory("allocating DOM-wrapper context");
7161 return (NULL);
7162 }
7163 memset(ret, 0, sizeof(xmlDOMWrapCtxt));
7164 return (ret);
7165}
7166
7167/*
7168* xmlDOMWrapFreeCtxt:
7169* @ctxt: the DOM-wrapper context
7170*
7171* Frees the DOM-wrapper context.
7172*/
7173void
7174xmlDOMWrapFreeCtxt(xmlDOMWrapCtxtPtr ctxt)
7175{
7176 if (ctxt == NULL)
7177 return;
7178 xmlFree(ctxt);
7179}
7180
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007181#define XML_TREE_NSMAP_PARENT -1
7182#define XML_TREE_NSMAP_XML -2
7183#define XML_TREE_NSMAP_DOC -3
7184#define XML_TREE_NSMAP_CUSTOM -4
7185
7186typedef struct xmlNsMapItem *xmlNsMapItemPtr;
7187struct xmlNsMapItem {
7188 xmlNsMapItemPtr next;
7189 xmlNsMapItemPtr prev;
7190 xmlNsPtr oldNs; /* old ns decl reference */
7191 xmlNsPtr newNs; /* new ns decl reference */
7192 int shadowDepth; /* Shadowed at this depth */
7193 /*
7194 * depth:
7195 * >= 0 == @node's ns-decls
7196 * -1 == @parent's ns-decls
7197 * -2 == @parent's out-of-scope ns-decls
7198 * -3 == the doc->oldNs XML ns-decl
7199 * -4 == the doc->oldNs storage ns-decls
7200 */
7201 int depth;
7202};
7203
7204/*
7205* xmlTreeAddNsMapItem:
7206* @map: the ns-map
7207* @cur: the current map entry to append a new entry to
7208* @oldNs: the old ns-struct
7209* @newNs: the new ns-struct
7210* @depth: depth and ns-kind information
7211*
7212* Frees the ns-map
7213*/
7214static xmlNsMapItemPtr
7215xmlDOMWrapNSNormAddNsMapItem(xmlNsMapItemPtr *map,
7216 xmlNsMapItemPtr *cur,
7217 xmlNsPtr oldNs,
7218 xmlNsPtr newNs,
7219 int depth)
7220{
7221 xmlNsMapItemPtr ret;
7222
7223 if ((cur != NULL) && (*cur != NULL) && ((*cur)->next != NULL)) {
7224 /*
7225 * Reuse.
7226 */
7227 ret = (*cur)->next;
7228 *cur = ret;
7229 } else {
7230 ret = (xmlNsMapItemPtr) xmlMalloc(sizeof(struct xmlNsMapItem));
7231 if (ret == NULL) {
7232 xmlTreeErrMemory("allocating namespace map item");
7233 return (NULL);
7234 }
7235 memset(ret, 0, sizeof(struct xmlNsMapItem));
7236 if (*map == NULL) {
7237 /*
7238 * First ever.
7239 */
7240 *map = ret;
7241 ret->prev = ret;
7242 if (cur != NULL)
7243 *cur = ret;
7244 } else {
7245 if (cur) {
7246 /*
7247 * Append.
7248 */
7249 (*cur)->next = ret;
7250 ret->prev = *cur;
7251 *cur = ret;
7252 } else {
7253 /*
7254 * Set on first position.
7255 */
7256 ret->next = (*map);
7257 ret->prev = (*map)->prev;
7258 (*map)->prev = ret;
7259 *map = ret;
7260 }
7261 }
7262 }
7263 ret->oldNs = oldNs;
7264 ret->newNs = newNs;
7265 ret->shadowDepth = -1;
7266 ret->depth = depth;
7267 return (ret);
7268}
7269
7270/*
7271* xmlTreeFreeNsMap:
7272* @map: the ns-map
7273*
7274* Frees the ns-map
7275*/
7276static void
7277xmlDOMWrapNSNormFreeNsMap(xmlNsMapItemPtr map)
7278{
7279 xmlNsMapItemPtr mi = map, miprev;
7280
7281 while (mi != NULL) {
7282 miprev = mi;
7283 mi = mi->next;
7284 xmlFree(miprev);
7285 }
7286}
7287
7288/*
7289* xmlTreeEnsureXMLDecl:
7290* @doc: the doc
7291*
7292* Ensures that there is an XML namespace declaration on the doc.
7293*
7294* Returns the XML ns-struct or NULL on API and internal errors.
7295*/
7296static xmlNsPtr
7297xmlTreeEnsureXMLDecl(xmlDocPtr doc)
7298{
7299 if (doc == NULL)
7300 return (NULL);
7301 if (doc->oldNs != NULL)
7302 return (doc->oldNs);
7303 {
7304 xmlNsPtr ns;
7305 ns = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
7306 if (ns == NULL) {
7307 xmlTreeErrMemory(
7308 "allocating the XML namespace");
7309 return (NULL);
7310 }
7311 memset(ns, 0, sizeof(xmlNs));
7312 ns->type = XML_LOCAL_NAMESPACE;
7313 ns->href = xmlStrdup(XML_XML_NAMESPACE);
7314 ns->prefix = xmlStrdup((const xmlChar *)"xml");
7315 doc->oldNs = ns;
7316 return (ns);
7317 }
7318}
7319
7320/*
7321* xmlDOMWrapStoreNs:
7322* @doc: the doc
7323* @nsName: the namespace name
7324* @prefix: the prefix
7325*
7326* Creates or reuses an xmlNs struct on doc->oldNs with
7327* the given prefix and namespace name.
7328*
7329* Returns the aquired ns struct or NULL in case of an API
7330* or internal error.
7331*/
7332static xmlNsPtr
7333xmlDOMWrapStoreNs(xmlDocPtr doc,
7334 const xmlChar *nsName,
7335 const xmlChar *prefix)
7336{
7337 xmlNsPtr ns;
7338
7339 if (doc == NULL)
7340 return (NULL);
7341 ns = xmlTreeEnsureXMLDecl(doc);
7342 if (ns == NULL)
7343 return (NULL);
7344 if (ns->next != NULL) {
7345 /* Reuse. */
7346 ns = ns->next;
7347 while (ns != NULL) {
7348 if (((ns->prefix == prefix) ||
7349 xmlStrEqual(ns->prefix, prefix)) &&
7350 xmlStrEqual(ns->href, nsName)) {
7351 return (ns);
7352 }
7353 if (ns->next == NULL)
7354 break;
7355 ns = ns->next;
7356 }
7357 }
7358 /* Create. */
7359 ns->next = xmlNewNs(NULL, nsName, prefix);
7360 return (ns->next);
7361}
7362
7363/*
7364* xmlTreeLookupNsListByPrefix:
7365* @nsList: a list of ns-structs
7366* @prefix: the searched prefix
7367*
7368* Searches for a ns-decl with the given prefix in @nsList.
7369*
7370* Returns the ns-decl if found, NULL if not found and on
7371* API errors.
7372*/
7373static xmlNsPtr
7374xmlTreeNSListLookupByPrefix(xmlNsPtr nsList, const xmlChar *prefix)
7375{
7376 if (nsList == NULL)
7377 return (NULL);
7378 {
7379 xmlNsPtr ns;
7380 ns = nsList;
7381 do {
7382 if ((prefix == ns->prefix) ||
7383 xmlStrEqual(prefix, ns->prefix)) {
7384 return (ns);
7385 }
7386 ns = ns->next;
7387 } while (ns != NULL);
7388 }
7389 return (NULL);
7390}
7391
7392/*
7393*
7394* xmlTreeGetInScopeNamespaces:
7395* @map: the namespace map
7396* @node: the node to start with
7397*
7398* Puts in-scope namespaces into the ns-map.
7399*
7400* Returns 0 on success, -1 on API or internal errors.
7401*/
7402static int
7403xmlDOMWrapNSNormGatherInScopeNs(xmlNsMapItemPtr *map,
7404 xmlNodePtr node)
7405{
7406 xmlNodePtr cur;
7407 xmlNsPtr ns;
7408 xmlNsMapItemPtr mi;
7409 int shadowed;
7410
7411 if ((map == NULL) || (*map != NULL))
7412 return (-1);
7413 /*
7414 * Get in-scope ns-decls of @parent.
7415 */
7416 cur = node;
7417 while ((cur != NULL) && (cur != (xmlNodePtr) cur->doc)) {
7418 if (cur->type == XML_ELEMENT_NODE) {
7419 if (cur->nsDef != NULL) {
7420 ns = cur->nsDef;
7421 do {
7422 shadowed = 0;
7423 if (*map != NULL) {
7424 /*
7425 * Skip shadowed prefixes.
7426 */
7427 for (mi = *map; mi != NULL; mi = mi->next) {
7428 if ((ns->prefix == mi->newNs->prefix) ||
7429 xmlStrEqual(ns->prefix, mi->newNs->prefix)) {
7430 shadowed = 1;
7431 break;
7432 }
7433 }
7434 }
7435 /*
7436 * Insert mapping.
7437 */
7438 mi = xmlDOMWrapNSNormAddNsMapItem(map, NULL, NULL,
7439 ns, XML_TREE_NSMAP_PARENT);
7440 if (mi == NULL)
7441 return (-1);
7442 if (shadowed)
7443 mi->shadowDepth = 0;
7444 ns = ns->next;
7445 } while (ns != NULL);
7446 }
7447 }
7448 cur = cur->parent;
7449 }
7450 return (0);
7451}
7452
7453/*
7454* XML_TREE_ADOPT_STR: If we have a dest-dict, put @str in the dict;
7455* otherwise copy it, when it was in the source-dict.
7456*/
7457#define XML_TREE_ADOPT_STR(str) \
7458 if (adoptStr && (str != NULL)) { \
7459 if (destDoc->dict) { \
Daniel Veillard7e21fd12005-07-03 21:44:07 +00007460 const xmlChar *old = str; \
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007461 str = xmlDictLookup(destDoc->dict, str, -1); \
Daniel Veillard7e21fd12005-07-03 21:44:07 +00007462 if ((sourceDoc == NULL) || (sourceDoc->dict == NULL) || \
7463 (!xmlDictOwns(sourceDoc->dict, old))) \
Daniel Veillard39e5c892005-07-03 22:48:50 +00007464 xmlFree((char *)old); \
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007465 } else if ((sourceDoc) && (sourceDoc->dict) && \
7466 xmlDictOwns(sourceDoc->dict, str)) { \
7467 str = BAD_CAST xmlStrdup(str); \
7468 } \
7469 }
7470
7471/*
7472* XML_TREE_ADOPT_STR_2: If @str was in the source-dict, then
7473* put it in dest-dict or copy it.
7474*/
7475#define XML_TREE_ADOPT_STR_2(str) \
7476 if (adoptStr && (str != NULL) && (sourceDoc != NULL) && \
7477 (sourceDoc->dict != NULL) && \
7478 xmlDictOwns(sourceDoc->dict, cur->content)) { \
7479 if (destDoc->dict) \
7480 cur->content = (xmlChar *) \
7481 xmlDictLookup(destDoc->dict, cur->content, -1); \
7482 else \
7483 cur->content = xmlStrdup(BAD_CAST cur->content); \
7484 }
7485
7486/*
7487* xmlDOMWrapNSNormAddNsMapItem2:
7488*
7489* For internal use. Adds a ns-decl mapping.
7490*
7491* Returns 0 on success, -1 on internal errors.
7492*/
7493static int
7494xmlDOMWrapNSNormAddNsMapItem2(xmlNsPtr **list, int *size, int *number,
7495 xmlNsPtr oldNs, xmlNsPtr newNs)
7496{
7497 if (*list == NULL) {
7498 *list = (xmlNsPtr *) xmlMalloc(6 * sizeof(xmlNsPtr));
7499 if (*list == NULL) {
7500 xmlTreeErrMemory("alloc ns map item");
7501 return(-1);
7502 }
7503 *size = 3;
7504 *number = 0;
7505 } else if ((*number) >= (*size)) {
7506 *size *= 2;
7507 *list = (xmlNsPtr *) xmlRealloc(*list,
7508 (*size) * 2 * sizeof(xmlNsPtr));
7509 if (*list == NULL) {
7510 xmlTreeErrMemory("realloc ns map item");
7511 return(-1);
7512 }
7513 }
7514 (*list)[2 * (*number)] = oldNs;
7515 (*list)[2 * (*number) +1] = newNs;
7516 (*number)++;
7517 return (0);
7518}
7519
7520/*
7521* xmlDOMWrapRemoveNode:
Daniel Veillard304e78c2005-07-03 16:19:41 +00007522* @ctxt: a DOM wrapper context
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007523* @doc: the doc
7524* @node: the node to be removed.
Daniel Veillard304e78c2005-07-03 16:19:41 +00007525* @options: set of options, unused at the moment
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007526*
7527* Unlinks the given node from its owner.
7528* This will substitute ns-references to node->nsDef for
7529* ns-references to doc->oldNs, thus ensuring the removed
7530* branch to be autark wrt ns-references.
Kasimier T. Buchcik017264f2005-06-27 13:45:24 +00007531* WARNING: This function is in a experimental state.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007532*
7533* Returns 0 on success, 1 if the node is not supported,
7534* -1 on API and internal errors.
7535*/
7536int
7537xmlDOMWrapRemoveNode(xmlDOMWrapCtxtPtr ctxt, xmlDocPtr doc,
7538 xmlNodePtr node, int options ATTRIBUTE_UNUSED)
7539{
7540 xmlNsPtr *list = NULL;
7541 int sizeList, nbList, i, j;
7542 xmlNsPtr ns;
7543
7544 if ((node == NULL) || (doc == NULL) || (node->doc != doc))
7545 return (-1);
7546
7547 /* TODO: 0 or -1 ? */
7548 if (node->parent == NULL)
7549 return (0);
7550
7551 switch (node->type) {
7552 case XML_TEXT_NODE:
7553 case XML_CDATA_SECTION_NODE:
7554 case XML_ENTITY_REF_NODE:
7555 case XML_PI_NODE:
7556 case XML_COMMENT_NODE:
7557 xmlUnlinkNode(node);
7558 return (0);
7559 case XML_ELEMENT_NODE:
7560 case XML_ATTRIBUTE_NODE:
7561 break;
7562 default:
7563 return (1);
7564 }
7565 xmlUnlinkNode(node);
7566 /*
7567 * Save out-of-scope ns-references in doc->oldNs.
7568 */
7569 do {
7570 switch (node->type) {
7571 case XML_ELEMENT_NODE:
7572 if ((ctxt == NULL) && (node->nsDef != NULL)) {
7573 ns = node->nsDef;
7574 do {
7575 if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
7576 &nbList, ns, ns) == -1)
7577 goto internal_error;
7578 ns = ns->next;
7579 } while (ns != NULL);
7580 }
7581 /* No break on purpose. */
7582 case XML_ATTRIBUTE_NODE:
7583 if (node->ns != NULL) {
7584 /*
7585 * Find a mapping.
7586 */
7587 if (list != NULL) {
7588 for (i = 0, j = 0; i < nbList; i++, j += 2) {
7589 if (node->ns == list[j]) {
7590 node->ns = list[++j];
7591 goto next_node;
7592 }
7593 }
7594 }
7595 ns = NULL;
7596 if (ctxt != NULL) {
7597 /*
7598 * User defined.
7599 */
7600 } else {
7601 /*
7602 * Add to doc's oldNs.
7603 */
7604 ns = xmlDOMWrapStoreNs(doc, node->ns->href,
7605 node->ns->prefix);
7606 if (ns == NULL)
7607 goto internal_error;
7608 }
7609 if (ns != NULL) {
7610 /*
7611 * Add mapping.
7612 */
7613 if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
7614 &nbList, node->ns, ns) == -1)
7615 goto internal_error;
7616 }
7617 node->ns = ns;
7618 }
7619 if ((node->type == XML_ELEMENT_NODE) &&
7620 (node->properties != NULL)) {
7621 node = (xmlNodePtr) node->properties;
7622 continue;
7623 }
7624 break;
7625 default:
7626 goto next_sibling;
7627 }
7628next_node:
7629 if ((node->type == XML_ELEMENT_NODE) &&
7630 (node->children != NULL)) {
7631 node = node->children;
7632 continue;
7633 }
7634next_sibling:
7635 if (node == NULL)
7636 break;
7637 if (node->next != NULL)
7638 node = node->next;
7639 else {
7640 node = node->parent;
7641 goto next_sibling;
7642 }
7643 } while (node != NULL);
7644
7645 if (list != NULL)
7646 xmlFree(list);
7647 return (0);
7648
7649internal_error:
7650 if (list != NULL)
7651 xmlFree(list);
7652 return (-1);
7653}
7654
7655/*
7656* xmlSearchNsByHrefStrict:
7657* @doc: the document
7658* @node: the start node
7659* @nsName: the searched namespace name
7660* @retNs: the resulting ns-decl
7661* @prefixed: if the found ns-decl must have a prefix (for attributes)
7662*
7663* Dynamically searches for a ns-declaration which matches
7664* the given @nsName in the ancestor-or-self axis of @node.
7665*
7666* Returns 1 if a ns-decl was found, 0 if not and -1 on API
7667* and internal errors.
7668*/
7669static int
7670xmlSearchNsByHrefStrict(xmlDocPtr doc, xmlNodePtr node, const xmlChar* nsName,
7671 xmlNsPtr *retNs, int prefixed)
7672{
7673 xmlNodePtr cur, prev = NULL, out = NULL;
7674 xmlNsPtr ns, prevns;
7675
7676 if ((doc == NULL) || (nsName == NULL) || (retNs == NULL))
7677 return (-1);
7678
7679 *retNs = NULL;
7680 if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) {
7681 *retNs = xmlTreeEnsureXMLDecl(doc);
7682 if (*retNs == NULL)
7683 return (-1);
7684 return (1);
7685 }
7686 cur = node;
7687 do {
7688 if (cur->type == XML_ELEMENT_NODE) {
7689 if (cur->nsDef != NULL) {
7690 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
7691 if (prefixed && (ns->prefix == NULL))
7692 continue;
7693 if (prev != NULL) {
7694 /*
7695 * Check the last level of ns-decls for a
7696 * shadowing prefix.
7697 */
7698 prevns = prev->nsDef;
7699 do {
7700 if ((prevns->prefix == ns->prefix) ||
7701 ((prevns->prefix != NULL) &&
7702 (ns->prefix != NULL) &&
7703 xmlStrEqual(prevns->prefix, ns->prefix))) {
7704 /*
7705 * Shadowed.
7706 */
7707 break;
7708 }
7709 prevns = prevns->next;
7710 } while (prevns != NULL);
7711 if (prevns != NULL)
7712 continue;
7713 }
7714 /*
7715 * Ns-name comparison.
7716 */
7717 if ((nsName == ns->href) ||
7718 xmlStrEqual(nsName, ns->href)) {
7719 /*
7720 * At this point the prefix can only be shadowed,
7721 * if we are the the (at least) 3rd level of
7722 * ns-decls.
7723 */
7724 if (out) {
7725 int ret;
7726
7727 ret = xmlNsInScope(doc, node, prev, ns->prefix);
7728 if (ret < 0)
7729 return (-1);
7730 /*
7731 * TODO: Should we try to find a matching ns-name
7732 * only once? This here keeps on searching.
7733 * I think we should try further since, there might
7734 * be an other matching ns-decl with an unshadowed
7735 * prefix.
7736 */
7737 if (! ret)
7738 continue;
7739 }
7740 *retNs = ns;
7741 return (1);
7742 }
7743 }
7744 out = prev;
7745 prev = cur;
7746 }
7747 } else if ((node->type == XML_ENTITY_REF_NODE) ||
7748 (node->type == XML_ENTITY_NODE) ||
7749 (node->type == XML_ENTITY_DECL))
7750 return (0);
7751 cur = cur->parent;
7752 } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
7753 return (0);
7754}
7755
7756/*
7757* xmlDOMWrapNSNormDeclareNsForced:
7758* @doc: the doc
7759* @elem: the element-node to declare on
7760* @nsName: the namespace-name of the ns-decl
7761* @prefix: the preferred prefix of the ns-decl
7762* @checkShadow: ensure that the new ns-decl doesn't shadow ancestor ns-decls
7763*
7764* Declares a new namespace on @elem. It tries to use the
7765* given @prefix; if a ns-decl with the given prefix is already existent
7766* on @elem, it will generate an other prefix.
7767*
7768* Returns 1 if a ns-decl was found, 0 if not and -1 on API
7769* and internal errors.
7770*/
7771static xmlNsPtr
7772xmlDOMWrapNSNormDeclareNsForced(xmlDocPtr doc,
7773 xmlNodePtr elem,
7774 const xmlChar *nsName,
7775 const xmlChar *prefix,
7776 int checkShadow)
7777{
7778
7779 xmlNsPtr ret;
7780 char buf[50];
7781 const xmlChar *pref;
7782 int counter = 0;
7783 /*
7784 * Create a ns-decl on @anchor.
7785 */
7786 pref = prefix;
7787 while (1) {
7788 /*
7789 * Lookup whether the prefix is unused in elem's ns-decls.
7790 */
7791 if ((elem->nsDef != NULL) &&
7792 (xmlTreeNSListLookupByPrefix(elem->nsDef, pref) != NULL))
7793 goto ns_next_prefix;
7794 if (checkShadow && elem->parent &&
7795 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
7796 /*
7797 * Does it shadow ancestor ns-decls?
7798 */
7799 if (xmlSearchNs(doc, elem->parent, pref) != NULL)
7800 goto ns_next_prefix;
7801 }
7802 ret = xmlNewNs(NULL, nsName, pref);
7803 if (ret == NULL)
7804 return (NULL);
7805 if (elem->nsDef == NULL)
7806 elem->nsDef = ret;
7807 else {
7808 xmlNsPtr ns2 = elem->nsDef;
7809 while (ns2->next != NULL)
7810 ns2 = ns2->next;
7811 ns2->next = ret;
7812 }
7813 return (ret);
7814ns_next_prefix:
7815 counter++;
7816 if (counter > 1000)
7817 return (NULL);
7818 if (prefix == NULL) {
7819 snprintf((char *) buf, sizeof(buf),
7820 "default%d", counter);
7821 } else
7822 snprintf((char *) buf, sizeof(buf),
7823 "%.30s%d", (char *)prefix, counter);
7824 pref = BAD_CAST buf;
7825 }
7826}
7827
7828/*
7829* xmlDOMWrapNSNormAquireNormalizedNs:
7830* @doc: the doc
7831* @elem: the element-node to declare namespaces on
7832* @ns: the ns-struct to use for the search
7833* @retNs: the found/created ns-struct
7834* @nsMap: the ns-map
7835* @topmi: the last ns-map entry
7836* @depth: the current tree depth
7837* @ancestorsOnly: search in ancestor ns-decls only
7838* @prefixed: if the searched ns-decl must have a prefix (for attributes)
7839*
7840* Searches for a matching ns-name in the ns-decls of @nsMap, if not
7841* found it will either declare it on @elem, or store it in doc->oldNs.
7842* If a new ns-decl needs to be declared on @elem, it tries to use the
7843* @ns->prefix for it, if this prefix is already in use on @elem, it will
7844* change the prefix or the new ns-decl.
7845*
7846* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
7847*/
7848static int
7849xmlDOMWrapNSNormAquireNormalizedNs(xmlDocPtr doc,
7850 xmlNodePtr elem,
7851 xmlNsPtr ns,
7852 xmlNsPtr *retNs,
7853 xmlNsMapItemPtr *nsMap,
7854 xmlNsMapItemPtr *topmi,
7855 int depth,
7856 int ancestorsOnly,
7857 int prefixed)
7858{
7859 xmlNsMapItemPtr mi;
7860
7861 if ((doc == NULL) || (ns == NULL) || (retNs == NULL) ||
7862 (nsMap == NULL) || (topmi == NULL))
7863 return (-1);
7864
7865 *retNs = NULL;
7866 /*
7867 * Handle XML namespace.
7868 */
7869 if ((ns->prefix) &&
7870 (ns->prefix[0] == 'x') &&
7871 (ns->prefix[1] == 'm') &&
7872 (ns->prefix[2] == 'l') &&
7873 (ns->prefix[3] == 0)) {
7874 /*
7875 * Insert XML namespace mapping.
7876 */
7877 *retNs = xmlTreeEnsureXMLDecl(doc);
7878 if (*retNs == NULL)
7879 return (-1);
7880 return (0);
7881 }
7882 /*
7883 * If the search should be done in ancestors only and no
7884 * @elem (the first ancestor) was specified, then skip the search.
7885 */
7886 if ((! (ancestorsOnly && (elem == NULL))) &&
7887 (*nsMap != NULL)) {
7888
7889 /*
7890 * Try to find an equal ns-name in in-scope ns-decls.
7891 */
7892 for (mi = *nsMap; mi != (*topmi)->next; mi = mi->next) {
7893
7894 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
7895 /*
7896 * This should be turned on to gain speed, if one knows
7897 * that the branch itself was already ns-wellformed and no
7898 * stale references existed. I.e. it searches in the ancestor
7899 * axis only.
7900 */
7901 ((! ancestorsOnly) || (mi->depth == XML_TREE_NSMAP_PARENT)) &&
7902 /* Skip shadowed prefixes. */
7903 (mi->shadowDepth == -1) &&
7904 /* Skip xmlns="" or xmlns:foo="". */
7905 ((mi->newNs->href != NULL) &&
7906 (mi->newNs->href[0] != 0)) &&
7907 /* Ensure a prefix if wanted. */
7908 ((! prefixed) || (mi->newNs->prefix != NULL)) &&
7909 /* Equal ns name */
7910 ((mi->newNs->href == ns->href) ||
7911 xmlStrEqual(mi->newNs->href, ns->href))) {
7912 /* Set the mapping. */
7913 mi->oldNs = ns;
7914 *retNs = mi->newNs;
7915 return (0);
7916 }
7917 }
7918 }
7919 /*
7920 * No luck, the namespace is out of scope or shadowed.
7921 */
7922 if (elem == NULL) {
7923 xmlNsPtr tmpns;
7924
7925 /*
7926 * Store ns-decls in "oldNs" of the document-node.
7927 */
7928 tmpns = xmlDOMWrapStoreNs(doc, ns->href, ns->prefix);
7929 if (tmpns == NULL)
7930 return (-1);
7931 /*
7932 * Insert mapping.
7933 */
7934 if (xmlDOMWrapNSNormAddNsMapItem(nsMap, NULL, ns,
7935 tmpns, XML_TREE_NSMAP_DOC) == NULL) {
7936 xmlFreeNs(tmpns);
7937 return (-1);
7938 }
7939 *retNs = tmpns;
7940 } else {
7941 xmlNsPtr tmpns;
7942
7943 tmpns = xmlDOMWrapNSNormDeclareNsForced(doc, elem, ns->href,
7944 ns->prefix, 0);
7945 if (tmpns == NULL)
7946 return (-1);
7947
7948 if (*nsMap != NULL) {
7949 /*
7950 * Does it shadow ancestor ns-decls?
7951 */
7952 for (mi = *nsMap; mi != (*topmi)->next; mi = mi->next) {
7953 if ((mi->depth < depth) &&
7954 (mi->shadowDepth == -1) &&
7955 ((ns->prefix == mi->newNs->prefix) ||
7956 xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
7957 /*
7958 * Shadows.
7959 */
7960 mi->shadowDepth = depth;
7961 break;
7962 }
7963 }
7964 }
7965 if (xmlDOMWrapNSNormAddNsMapItem(nsMap, topmi, ns,
7966 tmpns, depth) == NULL) {
7967 xmlFreeNs(tmpns);
7968 return (-1);
7969 }
7970 *retNs = tmpns;
7971 }
7972 return (0);
7973}
7974
7975/*
7976* xmlDOMWrapReconcileNamespaces:
Daniel Veillard304e78c2005-07-03 16:19:41 +00007977* @ctxt: DOM wrapper context, unused at the moment
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007978* @elem: the element-node
7979* @options: option flags
7980*
7981* Ensures that ns-references point to ns-decls hold on element-nodes.
7982* Ensures that the tree is namespace wellformed by creating additional
7983* ns-decls where needed. Note that, since prefixes of already existent
7984* ns-decls can be shadowed by this process, it could break QNames in
7985* attribute values or element content.
Kasimier T. Buchcik017264f2005-06-27 13:45:24 +00007986* WARNING: This function is in a experimental state.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007987*
7988* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
7989*/
7990int
7991xmlDOMWrapReconcileNamespaces(xmlDOMWrapCtxtPtr ctxt ATTRIBUTE_UNUSED,
7992 xmlNodePtr elem,
7993 int options ATTRIBUTE_UNUSED)
7994{
7995 int depth = -1, adoptns = 0, parnsdone = 0;
7996 xmlNsPtr ns;
7997 xmlDocPtr doc;
7998 xmlNodePtr cur, curElem = NULL;
7999 xmlNsMapItemPtr nsMap = NULL, topmi = NULL, mi;
8000 /* @ancestorsOnly should be set by an option flag. */
8001 int ancestorsOnly = 0;
8002
8003 if ((elem == NULL) || (elem->doc == NULL) ||
8004 (elem->type != XML_ELEMENT_NODE))
8005 return (-1);
8006
8007 doc = elem->doc;
8008 cur = elem;
8009 do {
8010 switch (cur->type) {
8011 case XML_ELEMENT_NODE:
8012 adoptns = 1;
8013 curElem = cur;
8014 depth++;
8015 /*
8016 * Namespace declarations.
8017 */
8018 if (cur->nsDef != NULL) {
8019 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
8020 if (! parnsdone) {
8021 if ((elem->parent) &&
8022 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8023 /*
8024 * Gather ancestor in-scope ns-decls.
8025 */
8026 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8027 elem->parent) == -1)
8028 goto internal_error;
8029 if (nsMap != NULL)
8030 topmi = nsMap->prev;
8031 }
8032 parnsdone = 1;
8033 }
8034 /*
8035 * Skip ns-references handling if the referenced
8036 * ns-decl is declared on the same element.
8037 */
8038 if ((cur->ns != NULL) && adoptns && (cur->ns == ns))
8039 adoptns = 0;
8040 /*
8041 * Does it shadow any ns-decl?
8042 */
8043 if (nsMap) {
8044 for (mi = nsMap; mi != topmi->next; mi = mi->next) {
8045 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8046 (mi->shadowDepth == -1) &&
8047 ((ns->prefix == mi->newNs->prefix) ||
8048 xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
8049
8050 mi->shadowDepth = depth;
8051 }
8052 }
8053 }
8054 /*
8055 * Push mapping.
8056 */
8057 if (xmlDOMWrapNSNormAddNsMapItem(&nsMap, &topmi, ns, ns,
8058 depth) == NULL)
8059 goto internal_error;
8060 }
8061 }
8062 if (! adoptns)
8063 goto ns_end;
8064
8065 /* No break on purpose. */
8066 case XML_ATTRIBUTE_NODE:
8067 if (cur->ns == NULL)
8068 goto ns_end;
8069 if (! parnsdone) {
8070 if ((elem->parent) &&
8071 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8072 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8073 elem->parent) == -1)
8074 goto internal_error;
8075 if (nsMap != NULL)
8076 topmi = nsMap->prev;
8077 }
8078 parnsdone = 1;
8079 }
8080 /*
8081 * Adopt ns-references.
8082 */
8083 if (nsMap != NULL) {
8084 /*
8085 * Search for a mapping.
8086 */
8087 for (mi = nsMap; mi != topmi->next; mi = mi->next) {
8088 if ((mi->shadowDepth == -1) &&
8089 (cur->ns == mi->oldNs)) {
8090
8091 cur->ns = mi->newNs;
8092 goto ns_end;
8093 }
8094 }
8095 }
8096 /*
8097 * Aquire a normalized ns-decl and add it to the map.
8098 */
8099 if (xmlDOMWrapNSNormAquireNormalizedNs(doc, curElem,
8100 cur->ns, &ns,
8101 &nsMap, &topmi, depth,
8102 ancestorsOnly,
8103 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
8104 goto internal_error;
8105 cur->ns = ns;
8106
8107ns_end:
8108 if ((cur->type == XML_ELEMENT_NODE) &&
8109 (cur->properties != NULL)) {
8110 /*
8111 * Process attributes.
8112 */
8113 cur = (xmlNodePtr) cur->properties;
8114 continue;
8115 }
8116 break;
8117 default:
8118 goto next_sibling;
8119 }
8120 if ((cur->type == XML_ELEMENT_NODE) &&
8121 (cur->children != NULL)) {
8122 /*
8123 * Process content of element-nodes only.
8124 */
8125 cur = cur->children;
8126 continue;
8127 }
8128next_sibling:
8129 if (cur == elem)
8130 break;
8131 if (cur->type == XML_ELEMENT_NODE) {
8132 if (nsMap != NULL) {
8133 /*
8134 * Pop mappings.
8135 */
8136 while ((topmi->depth >= 0) && (topmi->depth >= depth))
8137 topmi = topmi->prev;
8138 /*
8139 * Unshadow.
8140 */
8141 for (mi = nsMap; mi != topmi->next; mi = mi->next)
8142 if (mi->shadowDepth >= depth)
8143 mi->shadowDepth = -1;
8144 }
8145 depth--;
8146 }
8147 if (cur->next != NULL)
8148 cur = cur->next;
8149 else {
8150 cur = cur->parent;
8151 goto next_sibling;
8152 }
8153 } while (cur != NULL);
8154
8155 if (nsMap != NULL)
8156 xmlDOMWrapNSNormFreeNsMap(nsMap);
8157 return (0);
8158internal_error:
8159 if (nsMap != NULL)
8160 xmlDOMWrapNSNormFreeNsMap(nsMap);
8161 return (-1);
8162}
8163
8164/*
8165* xmlDOMWrapAdoptBranch:
8166* @ctxt: the optional context for custom processing
8167* @sourceDoc: the optional sourceDoc
8168* @node: the element-node to start with
8169* @destDoc: the destination doc for adoption
8170* @parent: the optional new parent of @node in @destDoc
8171* @options: option flags
8172*
8173* Ensures that ns-references point to @destDoc: either to
8174* elements->nsDef entries if @destParent is given, or to
8175* @destDoc->oldNs otherwise.
8176* If @destParent is given, it ensures that the tree is namespace
8177* wellformed by creating additional ns-decls where needed.
8178* Note that, since prefixes of already existent ns-decls can be
8179* shadowed by this process, it could break QNames in attribute
8180* values or element content.
8181*
8182* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8183*/
8184static int
8185xmlDOMWrapAdoptBranch(xmlDOMWrapCtxtPtr ctxt,
8186 xmlDocPtr sourceDoc,
8187 xmlNodePtr node,
8188 xmlDocPtr destDoc,
8189 xmlNodePtr destParent,
8190 int options ATTRIBUTE_UNUSED)
8191{
8192 int ret = 0;
8193 xmlNodePtr cur, curElem = NULL;
8194 xmlNsMapItemPtr nsMap = NULL, topmi = NULL, mi;
8195 xmlNsPtr ns;
8196 int depth = -1, adoptStr = 1;
8197 /* gather @parent's ns-decls. */
8198 int parnsdone = 0;
8199 /* @ancestorsOnly should be set per option. */
8200 int ancestorsOnly = 0;
8201
8202 /*
8203 * Optimize string adoption for equal or none dicts.
8204 */
8205 if ((sourceDoc != NULL) &&
8206 (sourceDoc->dict == destDoc->dict))
8207 adoptStr = 0;
8208 else
8209 adoptStr = 1;
8210
8211 cur = node;
8212 while (cur != NULL) {
8213 if (cur->doc != sourceDoc) {
8214 /*
8215 * We'll assume XIncluded nodes if the doc differs.
8216 * TODO: Do we need to reconciliate XIncluded nodes?
8217 * This here skips XIncluded nodes and tries to handle
8218 * broken sequences.
8219 */
8220 if (cur->next == NULL)
8221 goto leave_node;
8222 do {
8223 cur = cur->next;
8224 if ((cur->type == XML_XINCLUDE_END) ||
8225 (cur->doc == node->doc))
8226 break;
8227 } while (cur->next != NULL);
8228
8229 if (cur->doc != node->doc)
8230 goto leave_node;
8231 }
8232 cur->doc = destDoc;
8233 switch (cur->type) {
8234 case XML_XINCLUDE_START:
8235 case XML_XINCLUDE_END:
8236 /*
8237 * TODO
8238 */
8239 return (-1);
8240 case XML_ELEMENT_NODE:
8241 curElem = cur;
8242 depth++;
8243 /*
8244 * Namespace declarations.
8245 */
8246 if ((ctxt == NULL) && (cur->nsDef != NULL)) {
8247 if (! parnsdone) {
8248 if (destParent && (ctxt == NULL)) {
8249 /*
8250 * Gather @parent's in-scope ns-decls.
8251 */
8252 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8253 destParent) == -1)
8254 goto internal_error;
8255 if (nsMap != NULL)
8256 topmi = nsMap->prev;
8257 }
8258 parnsdone = 1;
8259 }
8260 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
8261 /*
8262 * ns->prefix and ns->href seem not to be in the dict.
8263 * XML_TREE_ADOPT_STR(ns->prefix)
8264 * XML_TREE_ADOPT_STR(ns->href)
8265 */
8266 /*
8267 * Does it shadow any ns-decl?
8268 */
8269 if (nsMap) {
8270 for (mi = nsMap; mi != topmi->next;
8271 mi = mi->next) {
8272 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8273 (mi->shadowDepth == -1) &&
8274 ((ns->prefix == mi->newNs->prefix) ||
8275 xmlStrEqual(ns->prefix,
8276 mi->newNs->prefix))) {
8277
8278 mi->shadowDepth = depth;
8279 }
8280 }
8281 }
8282 /*
8283 * Push mapping.
8284 */
8285 if (xmlDOMWrapNSNormAddNsMapItem(&nsMap, &topmi,
8286 ns, ns, depth) == NULL)
8287 goto internal_error;
8288 }
8289 }
8290 /* No break on purpose. */
8291 case XML_ATTRIBUTE_NODE:
8292
8293 if (cur->ns == NULL)
8294 goto ns_end;
8295 if (! parnsdone) {
8296 if (destParent && (ctxt == NULL)) {
8297 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8298 destParent) == -1)
8299 goto internal_error;
8300 if (nsMap != NULL)
8301 topmi = nsMap->prev;
8302 }
8303 parnsdone = 1;
8304 }
8305 /*
8306 * Adopt ns-references.
8307 */
8308 if (nsMap != NULL) {
8309 /*
8310 * Search for a mapping.
8311 */
8312 for (mi = nsMap; mi != topmi->next; mi = mi->next) {
8313 if ((mi->shadowDepth == -1) &&
8314 (cur->ns == mi->oldNs)) {
8315
8316 cur->ns = mi->newNs;
8317 goto ns_end;
8318 }
8319 }
8320 }
8321 /*
8322 * Start searching for an in-scope ns-decl.
8323 */
8324 if (ctxt != NULL) {
8325 /*
8326 * User-defined behaviour.
8327 */
8328#if 0
8329 ctxt->aquireNsDecl(ctxt, cur->ns, &ns);
8330#endif
8331 /*
8332 * Insert mapping if ns is available; it's the users fault
8333 * if not.
8334 */
8335 if (xmlDOMWrapNSNormAddNsMapItem(&nsMap, &topmi,
8336 ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
8337 goto internal_error;
8338 cur->ns = ns;
8339 } else {
8340 /*
8341 * Aquire a normalized ns-decl and add it to the map.
8342 */
8343 if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc,
8344 /* ns-decls on curElem or on destDoc->oldNs */
8345 destParent ? curElem : NULL,
8346 cur->ns, &ns,
8347 &nsMap, &topmi, depth,
8348 ancestorsOnly,
8349 /* ns-decls must be prefixed for attributes. */
8350 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
8351 goto internal_error;
8352 cur->ns = ns;
8353 }
8354ns_end:
8355 /*
8356 * Further node properties.
8357 * TODO: Is this all?
8358 */
8359 XML_TREE_ADOPT_STR(cur->name)
8360 if (cur->type == XML_ELEMENT_NODE) {
8361 cur->psvi = NULL;
8362 cur->line = 0;
8363 cur->extra = 0;
8364 /*
8365 * Walk attributes.
8366 */
8367 if (cur->properties != NULL) {
8368 /*
8369 * Process first attribute node.
8370 */
8371 cur = (xmlNodePtr) cur->properties;
8372 continue;
8373 }
8374 } else {
8375 /*
8376 * Attributes.
8377 */
8378 if ((sourceDoc != NULL) &&
8379 (((xmlAttrPtr) cur)->atype == XML_ATTRIBUTE_ID))
8380 xmlRemoveID(sourceDoc, (xmlAttrPtr) cur);
8381 ((xmlAttrPtr) cur)->atype = 0;
8382 ((xmlAttrPtr) cur)->psvi = NULL;
8383 }
8384 break;
8385 case XML_TEXT_NODE:
8386 case XML_CDATA_SECTION_NODE:
8387 /*
8388 * This puts the content in the dest dict, only if
8389 * it was previously in the source dict.
8390 */
8391 XML_TREE_ADOPT_STR_2(cur->content)
8392 goto leave_node;
8393 case XML_ENTITY_REF_NODE:
8394 /*
8395 * Remove reference to the entitity-node.
8396 */
8397 cur->content = NULL;
8398 cur->children = NULL;
8399 cur->last = NULL;
8400 if ((destDoc->intSubset) || (destDoc->extSubset)) {
8401 xmlEntityPtr ent;
8402 /*
8403 * Assign new entity-node if available.
8404 */
8405 ent = xmlGetDocEntity(destDoc, cur->name);
8406 if (ent != NULL) {
8407 cur->content = ent->content;
8408 cur->children = (xmlNodePtr) ent;
8409 cur->last = (xmlNodePtr) ent;
8410 }
8411 }
8412 goto leave_node;
8413 case XML_PI_NODE:
8414 XML_TREE_ADOPT_STR(cur->name)
8415 XML_TREE_ADOPT_STR_2(cur->content)
8416 break;
8417 case XML_COMMENT_NODE:
8418 break;
8419 default:
8420 goto internal_error;
8421 }
8422 /*
8423 * Walk the tree.
8424 */
8425 if (cur->children != NULL) {
8426 cur = cur->children;
8427 continue;
8428 }
8429
8430leave_node:
8431 if (cur == node)
8432 break;
8433 if ((cur->type == XML_ELEMENT_NODE) ||
8434 (cur->type == XML_XINCLUDE_START) ||
8435 (cur->type == XML_XINCLUDE_END)) {
8436 /*
8437 * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
8438 */
8439 if (nsMap != NULL) {
8440 /*
8441 * Pop mappings.
8442 */
8443 while (topmi->depth >= depth)
8444 topmi = topmi->prev;
8445 /*
8446 * Unshadow.
8447 */
8448 for (mi = nsMap; mi != topmi->next; mi = mi->next)
8449 if (mi->shadowDepth >= depth)
8450 mi->shadowDepth = -1;
8451 }
8452 depth--;
8453 }
8454 if (cur->next != NULL)
8455 cur = cur->next;
8456 else {
8457 cur = cur->parent;
8458 goto leave_node;
8459 }
8460 }
8461 /*
8462 * Cleanup.
8463 */
8464 if (nsMap != NULL)
8465 xmlDOMWrapNSNormFreeNsMap(nsMap);
8466 return (ret);
8467internal_error:
8468 if (nsMap != NULL)
8469 xmlDOMWrapNSNormFreeNsMap(nsMap);
8470 return (-1);
8471}
8472
8473/*
8474* xmlDOMWrapAdoptAttr:
8475* @ctxt: the optional context for custom processing
8476* @sourceDoc: the optional source document of attr
8477* @attr: the attribute-node to be adopted
8478* @destDoc: the destination doc for adoption
8479* @destParent: the optional new parent of @attr in @destDoc
8480* @options: option flags
8481*
8482* @attr is adopted by @destDoc.
8483* Ensures that ns-references point to @destDoc: either to
8484* elements->nsDef entries if @destParent is given, or to
8485* @destDoc->oldNs otherwise.
8486*
8487* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8488*/
8489static int
8490xmlDOMWrapAdoptAttr(xmlDOMWrapCtxtPtr ctxt,
8491 xmlDocPtr sourceDoc,
8492 xmlAttrPtr attr,
8493 xmlDocPtr destDoc,
8494 xmlNodePtr destParent,
8495 int options ATTRIBUTE_UNUSED)
8496{
8497 xmlNodePtr cur;
8498 int adoptStr = 1;
8499
8500 if ((attr == NULL) || (destDoc == NULL))
8501 return (-1);
8502
8503 attr->doc = destDoc;
8504 if (attr->ns != NULL) {
8505 xmlNsPtr ns = NULL;
8506
8507 if (ctxt != NULL) {
8508 /* TODO: User defined. */
8509 }
8510 /* XML Namespace. */
8511 if ((attr->ns->prefix[0] == 'x') && (attr->ns->prefix[1] == 'm') &&
8512 (attr->ns->prefix[2] == 'l') && (attr->ns->prefix[3] == 0)) {
8513 ns = xmlTreeEnsureXMLDecl(destDoc);
8514 } else if (destParent == NULL) {
8515 /*
8516 * Store in @destDoc->oldNs.
8517 */
8518 ns = xmlDOMWrapStoreNs(destDoc, attr->ns->href, attr->ns->prefix);
8519 } else {
8520 /*
8521 * Declare on @destParent.
8522 */
8523 if (xmlSearchNsByHrefStrict(destDoc, destParent, attr->ns->href,
8524 &ns, 1) == -1)
8525 goto internal_error;
8526 if (ns == NULL) {
8527 ns = xmlDOMWrapNSNormDeclareNsForced(destDoc, destParent,
8528 attr->ns->href, attr->ns->prefix, 1);
8529 }
8530 }
8531 if (ns == NULL)
8532 goto internal_error;
8533 attr->ns = ns;
8534 }
8535
8536 XML_TREE_ADOPT_STR(attr->name);
8537 attr->atype = 0;
8538 attr->psvi = NULL;
8539 /*
8540 * Walk content.
8541 */
8542 if (attr->children == NULL)
8543 return (0);
8544 cur = attr->children;
8545 while (cur != NULL) {
8546 cur->doc = destDoc;
8547 switch (cur->type) {
8548 case XML_TEXT_NODE:
8549 case XML_CDATA_SECTION_NODE:
8550 XML_TREE_ADOPT_STR_2(cur->content)
8551 break;
8552 case XML_ENTITY_REF_NODE:
8553 /*
8554 * Remove reference to the entitity-node.
8555 */
8556 cur->content = NULL;
8557 cur->children = NULL;
8558 cur->last = NULL;
8559 if ((destDoc->intSubset) || (destDoc->extSubset)) {
8560 xmlEntityPtr ent;
8561 /*
8562 * Assign new entity-node if available.
8563 */
8564 ent = xmlGetDocEntity(destDoc, cur->name);
8565 if (ent != NULL) {
8566 cur->content = ent->content;
8567 cur->children = (xmlNodePtr) ent;
8568 cur->last = (xmlNodePtr) ent;
8569 }
8570 }
8571 break;
8572 default:
8573 break;
8574 }
8575 if (cur->children != NULL) {
8576 cur = cur->children;
8577 continue;
8578 }
8579next_sibling:
8580 if (cur == (xmlNodePtr) attr)
8581 break;
8582 if (cur->next != NULL)
8583 cur = cur->next;
8584 else {
8585 cur = cur->parent;
8586 goto next_sibling;
8587 }
8588 }
8589 return (0);
8590internal_error:
8591 return (-1);
8592}
8593
8594/*
8595* xmlDOMWrapAdoptNode:
8596* @ctxt: the optional context for custom processing
8597* @sourceDoc: the optional sourceDoc
8598* @node: the node to start with
8599* @destDoc: the destination doc
Kasimier T. Buchcik4d9c9482005-06-27 15:04:46 +00008600* @destParent: the optional new parent of @node in @destDoc
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008601* @options: option flags
8602*
8603* Ensures that ns-references point to @destDoc: either to
8604* elements->nsDef entries if @destParent is given, or to
8605* @destDoc->oldNs otherwise.
8606* If @destParent is given, it ensures that the tree is namespace
8607* wellformed by creating additional ns-decls where needed.
8608* Note that, since prefixes of already existent ns-decls can be
8609* shadowed by this process, it could break QNames in attribute
8610* values or element content.
Kasimier T. Buchcik017264f2005-06-27 13:45:24 +00008611* WARNING: This function is in a experimental state.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008612*
8613* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8614*/
8615int
8616xmlDOMWrapAdoptNode(xmlDOMWrapCtxtPtr ctxt,
8617 xmlDocPtr sourceDoc,
8618 xmlNodePtr node,
8619 xmlDocPtr destDoc,
8620 xmlNodePtr destParent,
8621 int options)
8622{
8623 if ((node == NULL) || (destDoc == NULL) ||
8624 ((destParent != NULL) && (destParent->doc != destDoc)))
8625 return(-1);
8626 /*
8627 * Check node->doc sanity.
8628 */
8629 if ((node->doc != NULL) && (sourceDoc != NULL) &&
8630 (node->doc != sourceDoc)) {
8631 /*
8632 * Might be an XIncluded node.
8633 */
8634 return (-1);
8635 }
8636 if (sourceDoc == NULL)
8637 sourceDoc = node->doc;
8638 if (sourceDoc == destDoc)
8639 return (-1);
8640 switch (node->type) {
8641 case XML_ELEMENT_NODE:
8642 case XML_ATTRIBUTE_NODE:
8643 case XML_TEXT_NODE:
8644 case XML_CDATA_SECTION_NODE:
8645 case XML_ENTITY_REF_NODE:
8646 case XML_PI_NODE:
8647 case XML_COMMENT_NODE:
8648 break;
8649 case XML_DOCUMENT_FRAG_NODE:
8650 return (2);
8651 default:
8652 return (1);
8653 }
8654 /*
8655 * Unlink only if @node was not already added to @destParent.
8656 */
8657 if ((node->parent != NULL) && (destParent != node->parent))
8658 xmlUnlinkNode(node);
8659
8660 if (node->type == XML_ELEMENT_NODE) {
8661 return (xmlDOMWrapAdoptBranch(ctxt, sourceDoc, node,
8662 destDoc, destParent, options));
8663 } else if (node->type == XML_ATTRIBUTE_NODE) {
8664 return (xmlDOMWrapAdoptAttr(ctxt, sourceDoc,
8665 (xmlAttrPtr) node, destDoc, destParent, options));
8666 } else {
8667 xmlNodePtr cur = node;
8668 int adoptStr = 1;
8669
8670 cur->doc = destDoc;
8671 /*
8672 * Optimize string adoption.
8673 */
8674 if ((sourceDoc != NULL) &&
8675 (sourceDoc->dict == destDoc->dict))
8676 adoptStr = 0;
8677 switch (node->type) {
8678 case XML_TEXT_NODE:
8679 case XML_CDATA_SECTION_NODE:
8680 XML_TREE_ADOPT_STR_2(node->content)
8681 break;
8682 case XML_ENTITY_REF_NODE:
8683 /*
8684 * Remove reference to the entitity-node.
8685 */
8686 node->content = NULL;
8687 node->children = NULL;
8688 node->last = NULL;
8689 if ((destDoc->intSubset) || (destDoc->extSubset)) {
8690 xmlEntityPtr ent;
8691 /*
8692 * Assign new entity-node if available.
8693 */
8694 ent = xmlGetDocEntity(destDoc, node->name);
8695 if (ent != NULL) {
8696 node->content = ent->content;
8697 node->children = (xmlNodePtr) ent;
8698 node->last = (xmlNodePtr) ent;
8699 }
8700 }
8701 XML_TREE_ADOPT_STR(node->name)
8702 break;
Daniel Veillard7e21fd12005-07-03 21:44:07 +00008703 case XML_PI_NODE: {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008704 XML_TREE_ADOPT_STR(node->name)
8705 XML_TREE_ADOPT_STR_2(node->content)
8706 break;
Daniel Veillard7e21fd12005-07-03 21:44:07 +00008707 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008708 default:
8709 break;
8710 }
8711 }
8712 return (0);
8713}
8714
8715
Daniel Veillard5d4644e2005-04-01 13:11:58 +00008716#define bottom_tree
8717#include "elfgcchack.h"