blob: 2ea8e7be097f54cd5823d58c9055cfea50b36639 [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 Veillard03a53c32004-10-26 16:06:51 +0000336#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_DEBUG_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
1136 xmlDebugCheckDocument(stderr, cur);
1137#endif
1138
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001139 if (cur != NULL) dict = cur->dict;
Daniel Veillard5335dc52003-01-01 20:59:38 +00001140
Daniel Veillarda880b122003-04-21 21:36:41 +00001141 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001142 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
1143
Daniel Veillard76d66f42001-05-16 21:05:17 +00001144 /*
1145 * Do this before freeing the children list to avoid ID lookups
1146 */
1147 if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids);
1148 cur->ids = NULL;
1149 if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
1150 cur->refs = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001151 extSubset = cur->extSubset;
1152 intSubset = cur->intSubset;
Daniel Veillard5997aca2002-03-18 18:36:20 +00001153 if (intSubset == extSubset)
1154 extSubset = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001155 if (extSubset != NULL) {
Daniel Veillard76d66f42001-05-16 21:05:17 +00001156 xmlUnlinkNode((xmlNodePtr) cur->extSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001157 cur->extSubset = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001158 xmlFreeDtd(extSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001159 }
Daniel Veillarda9142e72001-06-19 11:07:54 +00001160 if (intSubset != NULL) {
Daniel Veillard76d66f42001-05-16 21:05:17 +00001161 xmlUnlinkNode((xmlNodePtr) cur->intSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001162 cur->intSubset = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001163 xmlFreeDtd(intSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001164 }
1165
1166 if (cur->children != NULL) xmlFreeNodeList(cur->children);
Owen Taylor3473f882001-02-23 17:55:21 +00001167 if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001168
1169 DICT_FREE(cur->version)
1170 DICT_FREE(cur->name)
1171 DICT_FREE(cur->encoding)
1172 DICT_FREE(cur->URL)
Owen Taylor3473f882001-02-23 17:55:21 +00001173 xmlFree(cur);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001174 if (dict) xmlDictFree(dict);
Owen Taylor3473f882001-02-23 17:55:21 +00001175}
1176
1177/**
1178 * xmlStringLenGetNodeList:
1179 * @doc: the document
1180 * @value: the value of the text
1181 * @len: the length of the string value
1182 *
1183 * Parse the value string and build the node list associated. Should
1184 * produce a flat tree with only TEXTs and ENTITY_REFs.
1185 * Returns a pointer to the first child
1186 */
1187xmlNodePtr
1188xmlStringLenGetNodeList(xmlDocPtr doc, const xmlChar *value, int len) {
1189 xmlNodePtr ret = NULL, last = NULL;
1190 xmlNodePtr node;
1191 xmlChar *val;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001192 const xmlChar *cur = value, *end = cur + len;
Owen Taylor3473f882001-02-23 17:55:21 +00001193 const xmlChar *q;
1194 xmlEntityPtr ent;
1195
1196 if (value == NULL) return(NULL);
1197
1198 q = cur;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001199 while ((cur < end) && (*cur != 0)) {
1200 if (cur[0] == '&') {
1201 int charval = 0;
1202 xmlChar tmp;
1203
Owen Taylor3473f882001-02-23 17:55:21 +00001204 /*
1205 * Save the current text.
1206 */
1207 if (cur != q) {
1208 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
1209 xmlNodeAddContentLen(last, q, cur - q);
1210 } else {
1211 node = xmlNewDocTextLen(doc, q, cur - q);
1212 if (node == NULL) return(ret);
1213 if (last == NULL)
1214 last = ret = node;
1215 else {
1216 last->next = node;
1217 node->prev = last;
1218 last = node;
1219 }
1220 }
1221 }
Owen Taylor3473f882001-02-23 17:55:21 +00001222 q = cur;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001223 if ((cur + 2 < end) && (cur[1] == '#') && (cur[2] == 'x')) {
1224 cur += 3;
1225 if (cur < end)
1226 tmp = *cur;
1227 else
1228 tmp = 0;
1229 while (tmp != ';') { /* Non input consuming loop */
1230 if ((tmp >= '0') && (tmp <= '9'))
1231 charval = charval * 16 + (tmp - '0');
1232 else if ((tmp >= 'a') && (tmp <= 'f'))
1233 charval = charval * 16 + (tmp - 'a') + 10;
1234 else if ((tmp >= 'A') && (tmp <= 'F'))
1235 charval = charval * 16 + (tmp - 'A') + 10;
Owen Taylor3473f882001-02-23 17:55:21 +00001236 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001237 xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
1238 NULL);
Daniel Veillard07cb8222003-09-10 10:51:05 +00001239 charval = 0;
1240 break;
1241 }
1242 cur++;
1243 if (cur < end)
1244 tmp = *cur;
1245 else
1246 tmp = 0;
1247 }
1248 if (tmp == ';')
1249 cur++;
1250 q = cur;
1251 } else if ((cur + 1 < end) && (cur[1] == '#')) {
1252 cur += 2;
1253 if (cur < end)
1254 tmp = *cur;
1255 else
1256 tmp = 0;
1257 while (tmp != ';') { /* Non input consuming loops */
1258 if ((tmp >= '0') && (tmp <= '9'))
1259 charval = charval * 10 + (tmp - '0');
1260 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001261 xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
1262 NULL);
Daniel Veillard07cb8222003-09-10 10:51:05 +00001263 charval = 0;
1264 break;
1265 }
1266 cur++;
1267 if (cur < end)
1268 tmp = *cur;
1269 else
1270 tmp = 0;
1271 }
1272 if (tmp == ';')
1273 cur++;
1274 q = cur;
1275 } else {
1276 /*
1277 * Read the entity string
1278 */
1279 cur++;
1280 q = cur;
1281 while ((cur < end) && (*cur != 0) && (*cur != ';')) cur++;
1282 if ((cur >= end) || (*cur == 0)) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001283 xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY, (xmlNodePtr) doc,
1284 (const char *) q);
Daniel Veillard07cb8222003-09-10 10:51:05 +00001285 return(ret);
1286 }
1287 if (cur != q) {
1288 /*
1289 * Predefined entities don't generate nodes
1290 */
1291 val = xmlStrndup(q, cur - q);
1292 ent = xmlGetDocEntity(doc, val);
1293 if ((ent != NULL) &&
1294 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
1295 if (last == NULL) {
1296 node = xmlNewDocText(doc, ent->content);
1297 last = ret = node;
1298 } else if (last->type != XML_TEXT_NODE) {
1299 node = xmlNewDocText(doc, ent->content);
1300 last = xmlAddNextSibling(last, node);
1301 } else
1302 xmlNodeAddContent(last, ent->content);
1303
1304 } else {
1305 /*
1306 * Create a new REFERENCE_REF node
1307 */
1308 node = xmlNewReference(doc, val);
1309 if (node == NULL) {
1310 if (val != NULL) xmlFree(val);
1311 return(ret);
1312 }
1313 else if ((ent != NULL) && (ent->children == NULL)) {
1314 xmlNodePtr temp;
1315
1316 ent->children = xmlStringGetNodeList(doc,
1317 (const xmlChar*)node->content);
1318 ent->owner = 1;
1319 temp = ent->children;
1320 while (temp) {
1321 temp->parent = (xmlNodePtr)ent;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001322 ent->last = temp;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001323 temp = temp->next;
1324 }
1325 }
1326 if (last == NULL) {
1327 last = ret = node;
1328 } else {
1329 last = xmlAddNextSibling(last, node);
1330 }
1331 }
1332 xmlFree(val);
1333 }
1334 cur++;
1335 q = cur;
1336 }
1337 if (charval != 0) {
1338 xmlChar buf[10];
1339 int l;
1340
1341 l = xmlCopyCharMultiByte(buf, charval);
1342 buf[l] = 0;
1343 node = xmlNewDocText(doc, buf);
1344 if (node != NULL) {
1345 if (last == NULL) {
1346 last = ret = node;
1347 } else {
1348 last = xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001349 }
1350 }
Daniel Veillard07cb8222003-09-10 10:51:05 +00001351 charval = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001352 }
Daniel Veillard07cb8222003-09-10 10:51:05 +00001353 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001354 cur++;
1355 }
Daniel Veillard07cb8222003-09-10 10:51:05 +00001356 if ((cur != q) || (ret == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001357 /*
1358 * Handle the last piece of text.
1359 */
1360 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
1361 xmlNodeAddContentLen(last, q, cur - q);
1362 } else {
1363 node = xmlNewDocTextLen(doc, q, cur - q);
1364 if (node == NULL) return(ret);
Daniel Veillard07cb8222003-09-10 10:51:05 +00001365 if (last == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001366 last = ret = node;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001367 } else {
1368 last = xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001369 }
1370 }
1371 }
1372 return(ret);
1373}
1374
1375/**
1376 * xmlStringGetNodeList:
1377 * @doc: the document
1378 * @value: the value of the attribute
1379 *
1380 * Parse the value string and build the node list associated. Should
1381 * produce a flat tree with only TEXTs and ENTITY_REFs.
1382 * Returns a pointer to the first child
1383 */
1384xmlNodePtr
1385xmlStringGetNodeList(xmlDocPtr doc, const xmlChar *value) {
1386 xmlNodePtr ret = NULL, last = NULL;
1387 xmlNodePtr node;
1388 xmlChar *val;
1389 const xmlChar *cur = value;
1390 const xmlChar *q;
1391 xmlEntityPtr ent;
1392
1393 if (value == NULL) return(NULL);
1394
1395 q = cur;
1396 while (*cur != 0) {
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001397 if (cur[0] == '&') {
1398 int charval = 0;
1399 xmlChar tmp;
1400
Owen Taylor3473f882001-02-23 17:55:21 +00001401 /*
1402 * Save the current text.
1403 */
1404 if (cur != q) {
1405 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
1406 xmlNodeAddContentLen(last, q, cur - q);
1407 } else {
1408 node = xmlNewDocTextLen(doc, q, cur - q);
1409 if (node == NULL) return(ret);
1410 if (last == NULL)
1411 last = ret = node;
1412 else {
1413 last->next = node;
1414 node->prev = last;
1415 last = node;
1416 }
1417 }
1418 }
Owen Taylor3473f882001-02-23 17:55:21 +00001419 q = cur;
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001420 if ((cur[1] == '#') && (cur[2] == 'x')) {
1421 cur += 3;
1422 tmp = *cur;
1423 while (tmp != ';') { /* Non input consuming loop */
1424 if ((tmp >= '0') && (tmp <= '9'))
1425 charval = charval * 16 + (tmp - '0');
1426 else if ((tmp >= 'a') && (tmp <= 'f'))
1427 charval = charval * 16 + (tmp - 'a') + 10;
1428 else if ((tmp >= 'A') && (tmp <= 'F'))
1429 charval = charval * 16 + (tmp - 'A') + 10;
Owen Taylor3473f882001-02-23 17:55:21 +00001430 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001431 xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
1432 NULL);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001433 charval = 0;
1434 break;
1435 }
1436 cur++;
1437 tmp = *cur;
1438 }
1439 if (tmp == ';')
1440 cur++;
1441 q = cur;
1442 } else if (cur[1] == '#') {
1443 cur += 2;
1444 tmp = *cur;
1445 while (tmp != ';') { /* Non input consuming loops */
1446 if ((tmp >= '0') && (tmp <= '9'))
1447 charval = charval * 10 + (tmp - '0');
1448 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001449 xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
1450 NULL);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001451 charval = 0;
1452 break;
1453 }
1454 cur++;
1455 tmp = *cur;
1456 }
1457 if (tmp == ';')
1458 cur++;
1459 q = cur;
1460 } else {
1461 /*
1462 * Read the entity string
1463 */
1464 cur++;
1465 q = cur;
1466 while ((*cur != 0) && (*cur != ';')) cur++;
1467 if (*cur == 0) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001468 xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY,
1469 (xmlNodePtr) doc, (const char *) q);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001470 return(ret);
1471 }
1472 if (cur != q) {
1473 /*
1474 * Predefined entities don't generate nodes
1475 */
1476 val = xmlStrndup(q, cur - q);
1477 ent = xmlGetDocEntity(doc, val);
1478 if ((ent != NULL) &&
1479 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
1480 if (last == NULL) {
1481 node = xmlNewDocText(doc, ent->content);
1482 last = ret = node;
Daniel Veillard6f42c132002-01-06 23:05:13 +00001483 } else if (last->type != XML_TEXT_NODE) {
1484 node = xmlNewDocText(doc, ent->content);
1485 last = xmlAddNextSibling(last, node);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001486 } else
1487 xmlNodeAddContent(last, ent->content);
1488
1489 } else {
1490 /*
1491 * Create a new REFERENCE_REF node
1492 */
1493 node = xmlNewReference(doc, val);
1494 if (node == NULL) {
1495 if (val != NULL) xmlFree(val);
1496 return(ret);
1497 }
Daniel Veillardbf8dae82002-04-18 16:39:10 +00001498 else if ((ent != NULL) && (ent->children == NULL)) {
1499 xmlNodePtr temp;
1500
1501 ent->children = xmlStringGetNodeList(doc,
1502 (const xmlChar*)node->content);
Daniel Veillard2d84a892002-12-30 00:01:08 +00001503 ent->owner = 1;
Daniel Veillardbf8dae82002-04-18 16:39:10 +00001504 temp = ent->children;
1505 while (temp) {
1506 temp->parent = (xmlNodePtr)ent;
1507 temp = temp->next;
1508 }
1509 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001510 if (last == NULL) {
1511 last = ret = node;
1512 } else {
1513 last = xmlAddNextSibling(last, node);
1514 }
1515 }
1516 xmlFree(val);
1517 }
1518 cur++;
1519 q = cur;
1520 }
1521 if (charval != 0) {
1522 xmlChar buf[10];
1523 int len;
1524
1525 len = xmlCopyCharMultiByte(buf, charval);
1526 buf[len] = 0;
1527 node = xmlNewDocText(doc, buf);
1528 if (node != NULL) {
1529 if (last == NULL) {
1530 last = ret = node;
1531 } else {
1532 last = xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001533 }
1534 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001535
1536 charval = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001537 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001538 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001539 cur++;
1540 }
Daniel Veillard75bea542001-05-11 17:41:21 +00001541 if ((cur != q) || (ret == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001542 /*
1543 * Handle the last piece of text.
1544 */
1545 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
1546 xmlNodeAddContentLen(last, q, cur - q);
1547 } else {
1548 node = xmlNewDocTextLen(doc, q, cur - q);
1549 if (node == NULL) return(ret);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001550 if (last == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001551 last = ret = node;
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001552 } else {
1553 last = xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001554 }
1555 }
1556 }
1557 return(ret);
1558}
1559
1560/**
1561 * xmlNodeListGetString:
1562 * @doc: the document
1563 * @list: a Node list
1564 * @inLine: should we replace entity contents or show their external form
1565 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001566 * Build the string equivalent to the text contained in the Node list
Owen Taylor3473f882001-02-23 17:55:21 +00001567 * made of TEXTs and ENTITY_REFs
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001568 *
Daniel Veillardbd9afb52002-09-25 22:25:35 +00001569 * Returns a pointer to the string copy, the caller must free it with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00001570 */
1571xmlChar *
Daniel Veillard7646b182002-04-20 06:41:40 +00001572xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine)
1573{
Owen Taylor3473f882001-02-23 17:55:21 +00001574 xmlNodePtr node = list;
1575 xmlChar *ret = NULL;
1576 xmlEntityPtr ent;
1577
Daniel Veillard7646b182002-04-20 06:41:40 +00001578 if (list == NULL)
1579 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001580
1581 while (node != NULL) {
1582 if ((node->type == XML_TEXT_NODE) ||
Daniel Veillard7646b182002-04-20 06:41:40 +00001583 (node->type == XML_CDATA_SECTION_NODE)) {
1584 if (inLine) {
1585 ret = xmlStrcat(ret, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001586 } else {
Daniel Veillard7646b182002-04-20 06:41:40 +00001587 xmlChar *buffer;
Owen Taylor3473f882001-02-23 17:55:21 +00001588
Daniel Veillard7646b182002-04-20 06:41:40 +00001589 buffer = xmlEncodeEntitiesReentrant(doc, node->content);
1590 if (buffer != NULL) {
1591 ret = xmlStrcat(ret, buffer);
1592 xmlFree(buffer);
1593 }
1594 }
1595 } else if (node->type == XML_ENTITY_REF_NODE) {
1596 if (inLine) {
1597 ent = xmlGetDocEntity(doc, node->name);
1598 if (ent != NULL) {
1599 xmlChar *buffer;
1600
1601 /* an entity content can be any "well balanced chunk",
1602 * i.e. the result of the content [43] production:
1603 * http://www.w3.org/TR/REC-xml#NT-content.
1604 * So it can contain text, CDATA section or nested
1605 * entity reference nodes (among others).
1606 * -> we recursive call xmlNodeListGetString()
1607 * which handles these types */
1608 buffer = xmlNodeListGetString(doc, ent->children, 1);
1609 if (buffer != NULL) {
1610 ret = xmlStrcat(ret, buffer);
1611 xmlFree(buffer);
1612 }
1613 } else {
1614 ret = xmlStrcat(ret, node->content);
1615 }
1616 } else {
1617 xmlChar buf[2];
1618
1619 buf[0] = '&';
1620 buf[1] = 0;
1621 ret = xmlStrncat(ret, buf, 1);
1622 ret = xmlStrcat(ret, node->name);
1623 buf[0] = ';';
1624 buf[1] = 0;
1625 ret = xmlStrncat(ret, buf, 1);
1626 }
1627 }
1628#if 0
1629 else {
1630 xmlGenericError(xmlGenericErrorContext,
1631 "xmlGetNodeListString : invalid node type %d\n",
1632 node->type);
1633 }
1634#endif
1635 node = node->next;
1636 }
1637 return (ret);
1638}
Daniel Veillard652327a2003-09-29 18:02:38 +00001639
1640#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001641/**
1642 * xmlNodeListGetRawString:
1643 * @doc: the document
1644 * @list: a Node list
1645 * @inLine: should we replace entity contents or show their external form
1646 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001647 * Builds the string equivalent to the text contained in the Node list
Owen Taylor3473f882001-02-23 17:55:21 +00001648 * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString()
1649 * this function doesn't do any character encoding handling.
1650 *
Daniel Veillardbd9afb52002-09-25 22:25:35 +00001651 * Returns a pointer to the string copy, the caller must free it with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00001652 */
1653xmlChar *
Daniel Veillard7646b182002-04-20 06:41:40 +00001654xmlNodeListGetRawString(xmlDocPtr doc, xmlNodePtr list, int inLine)
1655{
Owen Taylor3473f882001-02-23 17:55:21 +00001656 xmlNodePtr node = list;
1657 xmlChar *ret = NULL;
1658 xmlEntityPtr ent;
1659
Daniel Veillard7646b182002-04-20 06:41:40 +00001660 if (list == NULL)
1661 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001662
1663 while (node != NULL) {
Daniel Veillard7db37732001-07-12 01:20:08 +00001664 if ((node->type == XML_TEXT_NODE) ||
Daniel Veillard7646b182002-04-20 06:41:40 +00001665 (node->type == XML_CDATA_SECTION_NODE)) {
1666 if (inLine) {
1667 ret = xmlStrcat(ret, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001668 } else {
Daniel Veillard7646b182002-04-20 06:41:40 +00001669 xmlChar *buffer;
1670
1671 buffer = xmlEncodeSpecialChars(doc, node->content);
1672 if (buffer != NULL) {
1673 ret = xmlStrcat(ret, buffer);
1674 xmlFree(buffer);
1675 }
1676 }
1677 } else if (node->type == XML_ENTITY_REF_NODE) {
1678 if (inLine) {
1679 ent = xmlGetDocEntity(doc, node->name);
1680 if (ent != NULL) {
1681 xmlChar *buffer;
1682
1683 /* an entity content can be any "well balanced chunk",
1684 * i.e. the result of the content [43] production:
1685 * http://www.w3.org/TR/REC-xml#NT-content.
1686 * So it can contain text, CDATA section or nested
1687 * entity reference nodes (among others).
1688 * -> we recursive call xmlNodeListGetRawString()
1689 * which handles these types */
1690 buffer =
1691 xmlNodeListGetRawString(doc, ent->children, 1);
1692 if (buffer != NULL) {
1693 ret = xmlStrcat(ret, buffer);
1694 xmlFree(buffer);
1695 }
1696 } else {
1697 ret = xmlStrcat(ret, node->content);
1698 }
1699 } else {
1700 xmlChar buf[2];
1701
1702 buf[0] = '&';
1703 buf[1] = 0;
1704 ret = xmlStrncat(ret, buf, 1);
1705 ret = xmlStrcat(ret, node->name);
1706 buf[0] = ';';
1707 buf[1] = 0;
1708 ret = xmlStrncat(ret, buf, 1);
1709 }
1710 }
Owen Taylor3473f882001-02-23 17:55:21 +00001711#if 0
Daniel Veillard7646b182002-04-20 06:41:40 +00001712 else {
1713 xmlGenericError(xmlGenericErrorContext,
1714 "xmlGetNodeListString : invalid node type %d\n",
1715 node->type);
1716 }
Owen Taylor3473f882001-02-23 17:55:21 +00001717#endif
Daniel Veillard7646b182002-04-20 06:41:40 +00001718 node = node->next;
Owen Taylor3473f882001-02-23 17:55:21 +00001719 }
Daniel Veillard7646b182002-04-20 06:41:40 +00001720 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00001721}
Daniel Veillard652327a2003-09-29 18:02:38 +00001722#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001723
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001724static xmlAttrPtr xmlNewPropInternal(xmlNodePtr node, xmlNsPtr ns,
1725 const xmlChar *name, const xmlChar *value, int eatname) {
Owen Taylor3473f882001-02-23 17:55:21 +00001726 xmlAttrPtr cur;
1727 xmlDocPtr doc = NULL;
1728
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001729 if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) {
1730 if (eatname == 1)
1731 xmlFree((xmlChar *) name);
1732 return(NULL);
1733 }
Owen Taylor3473f882001-02-23 17:55:21 +00001734
1735 /*
1736 * Allocate a new property and fill the fields.
1737 */
1738 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1739 if (cur == NULL) {
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001740 if (eatname == 1)
1741 xmlFree((xmlChar *) name);
1742 xmlTreeErrMemory("building attribute");
1743 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001744 }
1745 memset(cur, 0, sizeof(xmlAttr));
1746 cur->type = XML_ATTRIBUTE_NODE;
1747
1748 cur->parent = node;
1749 if (node != NULL) {
1750 doc = node->doc;
1751 cur->doc = doc;
1752 }
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001753 cur->ns = ns;
1754
1755 if (eatname == 0) {
1756 if ((doc != NULL) && (doc->dict != NULL))
1757 cur->name = (xmlChar *) xmlDictLookup(doc->dict, name, -1);
1758 else
1759 cur->name = xmlStrdup(name);
1760 } else
1761 cur->name = name;
1762
Owen Taylor3473f882001-02-23 17:55:21 +00001763 if (value != NULL) {
1764 xmlChar *buffer;
1765 xmlNodePtr tmp;
1766
1767 buffer = xmlEncodeEntitiesReentrant(doc, value);
1768 cur->children = xmlStringGetNodeList(doc, buffer);
1769 cur->last = NULL;
1770 tmp = cur->children;
1771 while (tmp != NULL) {
1772 tmp->parent = (xmlNodePtr) cur;
Owen Taylor3473f882001-02-23 17:55:21 +00001773 if (tmp->next == NULL)
1774 cur->last = tmp;
1775 tmp = tmp->next;
1776 }
1777 xmlFree(buffer);
1778 }
1779
1780 /*
1781 * Add it at the end to preserve parsing order ...
1782 */
1783 if (node != NULL) {
1784 if (node->properties == NULL) {
1785 node->properties = cur;
1786 } else {
1787 xmlAttrPtr prev = node->properties;
1788
1789 while (prev->next != NULL) prev = prev->next;
1790 prev->next = cur;
1791 cur->prev = prev;
1792 }
1793 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00001794
Daniel Veillarda880b122003-04-21 21:36:41 +00001795 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001796 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001797 return(cur);
1798}
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001799
1800#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
1801 defined(LIBXML_SCHEMAS_ENABLED)
1802/**
1803 * xmlNewProp:
1804 * @node: the holding node
1805 * @name: the name of the attribute
1806 * @value: the value of the attribute
1807 *
1808 * Create a new property carried by a node.
1809 * Returns a pointer to the attribute
1810 */
1811xmlAttrPtr
1812xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
1813
1814 if (name == NULL) {
1815#ifdef DEBUG_TREE
1816 xmlGenericError(xmlGenericErrorContext,
1817 "xmlNewProp : name == NULL\n");
1818#endif
1819 return(NULL);
1820 }
1821
1822 return xmlNewPropInternal(node, NULL, name, value, 0);
1823}
Daniel Veillard652327a2003-09-29 18:02:38 +00001824#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001825
1826/**
1827 * xmlNewNsProp:
1828 * @node: the holding node
1829 * @ns: the namespace
1830 * @name: the name of the attribute
1831 * @value: the value of the attribute
1832 *
1833 * Create a new property tagged with a namespace and carried by a node.
1834 * Returns a pointer to the attribute
1835 */
1836xmlAttrPtr
1837xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
1838 const xmlChar *value) {
Owen Taylor3473f882001-02-23 17:55:21 +00001839
1840 if (name == NULL) {
1841#ifdef DEBUG_TREE
1842 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00001843 "xmlNewNsProp : name == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001844#endif
1845 return(NULL);
1846 }
1847
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001848 return xmlNewPropInternal(node, ns, name, value, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001849}
1850
1851/**
Daniel Veillard46de64e2002-05-29 08:21:33 +00001852 * xmlNewNsPropEatName:
1853 * @node: the holding node
1854 * @ns: the namespace
1855 * @name: the name of the attribute
1856 * @value: the value of the attribute
1857 *
1858 * Create a new property tagged with a namespace and carried by a node.
1859 * Returns a pointer to the attribute
1860 */
1861xmlAttrPtr
1862xmlNewNsPropEatName(xmlNodePtr node, xmlNsPtr ns, xmlChar *name,
1863 const xmlChar *value) {
Daniel Veillard46de64e2002-05-29 08:21:33 +00001864
1865 if (name == NULL) {
1866#ifdef DEBUG_TREE
1867 xmlGenericError(xmlGenericErrorContext,
1868 "xmlNewNsPropEatName : name == NULL\n");
1869#endif
1870 return(NULL);
1871 }
1872
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001873 return xmlNewPropInternal(node, ns, name, value, 1);
Daniel Veillard46de64e2002-05-29 08:21:33 +00001874}
1875
1876/**
Owen Taylor3473f882001-02-23 17:55:21 +00001877 * xmlNewDocProp:
1878 * @doc: the document
1879 * @name: the name of the attribute
1880 * @value: the value of the attribute
1881 *
1882 * Create a new property carried by a document.
1883 * Returns a pointer to the attribute
1884 */
1885xmlAttrPtr
1886xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) {
1887 xmlAttrPtr cur;
1888
1889 if (name == NULL) {
1890#ifdef DEBUG_TREE
1891 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00001892 "xmlNewDocProp : name == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001893#endif
1894 return(NULL);
1895 }
1896
1897 /*
1898 * Allocate a new property and fill the fields.
1899 */
1900 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1901 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001902 xmlTreeErrMemory("building attribute");
Owen Taylor3473f882001-02-23 17:55:21 +00001903 return(NULL);
1904 }
1905 memset(cur, 0, sizeof(xmlAttr));
1906 cur->type = XML_ATTRIBUTE_NODE;
1907
Daniel Veillard03a53c32004-10-26 16:06:51 +00001908 if ((doc != NULL) && (doc->dict != NULL))
1909 cur->name = xmlDictLookup(doc->dict, name, -1);
1910 else
1911 cur->name = xmlStrdup(name);
Owen Taylor3473f882001-02-23 17:55:21 +00001912 cur->doc = doc;
1913 if (value != NULL) {
1914 xmlNodePtr tmp;
1915
1916 cur->children = xmlStringGetNodeList(doc, value);
1917 cur->last = NULL;
1918
1919 tmp = cur->children;
1920 while (tmp != NULL) {
1921 tmp->parent = (xmlNodePtr) cur;
1922 if (tmp->next == NULL)
1923 cur->last = tmp;
1924 tmp = tmp->next;
1925 }
1926 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00001927
Daniel Veillarda880b122003-04-21 21:36:41 +00001928 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001929 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001930 return(cur);
1931}
1932
1933/**
1934 * xmlFreePropList:
1935 * @cur: the first property in the list
1936 *
1937 * Free a property and all its siblings, all the children are freed too.
1938 */
1939void
1940xmlFreePropList(xmlAttrPtr cur) {
1941 xmlAttrPtr next;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001942 if (cur == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +00001943 while (cur != NULL) {
1944 next = cur->next;
1945 xmlFreeProp(cur);
1946 cur = next;
1947 }
1948}
1949
1950/**
1951 * xmlFreeProp:
1952 * @cur: an attribute
1953 *
1954 * Free one attribute, all the content is freed too
1955 */
1956void
1957xmlFreeProp(xmlAttrPtr cur) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001958 xmlDictPtr dict = NULL;
1959 if (cur == NULL) return;
1960
1961 if (cur->doc != NULL) dict = cur->doc->dict;
Daniel Veillard5335dc52003-01-01 20:59:38 +00001962
Daniel Veillarda880b122003-04-21 21:36:41 +00001963 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001964 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
1965
Owen Taylor3473f882001-02-23 17:55:21 +00001966 /* Check for ID removal -> leading to invalid references ! */
Daniel Veillardda6f4af2005-06-20 17:17:54 +00001967 if ((cur->doc != NULL) && (cur->atype == XML_ATTRIBUTE_ID)) {
1968 xmlRemoveID(cur->doc, cur);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001969 }
Owen Taylor3473f882001-02-23 17:55:21 +00001970 if (cur->children != NULL) xmlFreeNodeList(cur->children);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001971 DICT_FREE(cur->name)
Owen Taylor3473f882001-02-23 17:55:21 +00001972 xmlFree(cur);
1973}
1974
Daniel Veillard652327a2003-09-29 18:02:38 +00001975#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001976/**
1977 * xmlRemoveProp:
1978 * @cur: an attribute
1979 *
1980 * Unlink and free one attribute, all the content is freed too
1981 * Note this doesn't work for namespace definition attributes
1982 *
1983 * Returns 0 if success and -1 in case of error.
1984 */
1985int
1986xmlRemoveProp(xmlAttrPtr cur) {
1987 xmlAttrPtr tmp;
1988 if (cur == NULL) {
1989#ifdef DEBUG_TREE
1990 xmlGenericError(xmlGenericErrorContext,
1991 "xmlRemoveProp : cur == NULL\n");
1992#endif
1993 return(-1);
1994 }
1995 if (cur->parent == NULL) {
1996#ifdef DEBUG_TREE
1997 xmlGenericError(xmlGenericErrorContext,
1998 "xmlRemoveProp : cur->parent == NULL\n");
1999#endif
2000 return(-1);
2001 }
2002 tmp = cur->parent->properties;
2003 if (tmp == cur) {
2004 cur->parent->properties = cur->next;
2005 xmlFreeProp(cur);
2006 return(0);
2007 }
2008 while (tmp != NULL) {
2009 if (tmp->next == cur) {
2010 tmp->next = cur->next;
2011 if (tmp->next != NULL)
2012 tmp->next->prev = tmp;
2013 xmlFreeProp(cur);
2014 return(0);
2015 }
2016 tmp = tmp->next;
2017 }
2018#ifdef DEBUG_TREE
2019 xmlGenericError(xmlGenericErrorContext,
2020 "xmlRemoveProp : attribute not owned by its node\n");
2021#endif
2022 return(-1);
2023}
Daniel Veillard652327a2003-09-29 18:02:38 +00002024#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002025
2026/**
Daniel Veillard03a53c32004-10-26 16:06:51 +00002027 * xmlNewDocPI:
2028 * @doc: the target document
Owen Taylor3473f882001-02-23 17:55:21 +00002029 * @name: the processing instruction name
2030 * @content: the PI content
2031 *
2032 * Creation of a processing instruction element.
2033 * Returns a pointer to the new node object.
2034 */
2035xmlNodePtr
Daniel Veillard03a53c32004-10-26 16:06:51 +00002036xmlNewDocPI(xmlDocPtr doc, const xmlChar *name, const xmlChar *content) {
Owen Taylor3473f882001-02-23 17:55:21 +00002037 xmlNodePtr cur;
2038
2039 if (name == NULL) {
2040#ifdef DEBUG_TREE
2041 xmlGenericError(xmlGenericErrorContext,
2042 "xmlNewPI : name == NULL\n");
2043#endif
2044 return(NULL);
2045 }
2046
2047 /*
2048 * Allocate a new node and fill the fields.
2049 */
2050 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2051 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002052 xmlTreeErrMemory("building PI");
Owen Taylor3473f882001-02-23 17:55:21 +00002053 return(NULL);
2054 }
2055 memset(cur, 0, sizeof(xmlNode));
2056 cur->type = XML_PI_NODE;
2057
Daniel Veillard03a53c32004-10-26 16:06:51 +00002058 if ((doc != NULL) && (doc->dict != NULL))
2059 cur->name = xmlDictLookup(doc->dict, name, -1);
2060 else
2061 cur->name = xmlStrdup(name);
Owen Taylor3473f882001-02-23 17:55:21 +00002062 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002063 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00002064 }
Daniel Veillarda03e3652004-11-02 18:45:30 +00002065 cur->doc = doc;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002066
Daniel Veillarda880b122003-04-21 21:36:41 +00002067 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002068 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002069 return(cur);
2070}
2071
2072/**
Daniel Veillard03a53c32004-10-26 16:06:51 +00002073 * xmlNewPI:
2074 * @name: the processing instruction name
2075 * @content: the PI content
2076 *
2077 * Creation of a processing instruction element.
2078 * Use xmlDocNewPI preferably to get string interning
2079 *
2080 * Returns a pointer to the new node object.
2081 */
2082xmlNodePtr
2083xmlNewPI(const xmlChar *name, const xmlChar *content) {
2084 return(xmlNewDocPI(NULL, name, content));
2085}
2086
2087/**
Owen Taylor3473f882001-02-23 17:55:21 +00002088 * xmlNewNode:
2089 * @ns: namespace if any
2090 * @name: the node name
2091 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002092 * Creation of a new node element. @ns is optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00002093 *
William M. Brackd7cf7f82003-11-14 07:13:16 +00002094 * Returns a pointer to the new node object. Uses xmlStrdup() to make
2095 * copy of @name.
Owen Taylor3473f882001-02-23 17:55:21 +00002096 */
2097xmlNodePtr
2098xmlNewNode(xmlNsPtr ns, const xmlChar *name) {
2099 xmlNodePtr cur;
2100
2101 if (name == NULL) {
2102#ifdef DEBUG_TREE
2103 xmlGenericError(xmlGenericErrorContext,
2104 "xmlNewNode : name == NULL\n");
2105#endif
2106 return(NULL);
2107 }
2108
2109 /*
2110 * Allocate a new node and fill the fields.
2111 */
2112 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2113 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002114 xmlTreeErrMemory("building node");
Owen Taylor3473f882001-02-23 17:55:21 +00002115 return(NULL);
2116 }
2117 memset(cur, 0, sizeof(xmlNode));
2118 cur->type = XML_ELEMENT_NODE;
2119
2120 cur->name = xmlStrdup(name);
2121 cur->ns = ns;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002122
Daniel Veillarda880b122003-04-21 21:36:41 +00002123 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002124 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002125 return(cur);
2126}
2127
2128/**
Daniel Veillard46de64e2002-05-29 08:21:33 +00002129 * xmlNewNodeEatName:
2130 * @ns: namespace if any
2131 * @name: the node name
2132 *
2133 * Creation of a new node element. @ns is optional (NULL).
2134 *
William M. Brackd7cf7f82003-11-14 07:13:16 +00002135 * Returns a pointer to the new node object, with pointer @name as
2136 * new node's name. Use xmlNewNode() if a copy of @name string is
2137 * is needed as new node's name.
Daniel Veillard46de64e2002-05-29 08:21:33 +00002138 */
2139xmlNodePtr
2140xmlNewNodeEatName(xmlNsPtr ns, xmlChar *name) {
2141 xmlNodePtr cur;
2142
2143 if (name == NULL) {
2144#ifdef DEBUG_TREE
2145 xmlGenericError(xmlGenericErrorContext,
2146 "xmlNewNode : name == NULL\n");
2147#endif
2148 return(NULL);
2149 }
2150
2151 /*
2152 * Allocate a new node and fill the fields.
2153 */
2154 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2155 if (cur == NULL) {
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00002156 xmlFree(name);
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002157 xmlTreeErrMemory("building node");
Daniel Veillard46de64e2002-05-29 08:21:33 +00002158 return(NULL);
2159 }
2160 memset(cur, 0, sizeof(xmlNode));
2161 cur->type = XML_ELEMENT_NODE;
2162
2163 cur->name = name;
2164 cur->ns = ns;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002165
Daniel Veillarda880b122003-04-21 21:36:41 +00002166 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002167 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Daniel Veillard46de64e2002-05-29 08:21:33 +00002168 return(cur);
2169}
2170
2171/**
Owen Taylor3473f882001-02-23 17:55:21 +00002172 * xmlNewDocNode:
2173 * @doc: the document
2174 * @ns: namespace if any
2175 * @name: the node name
2176 * @content: the XML text content if any
2177 *
2178 * Creation of a new node element within a document. @ns and @content
Daniel Veillardd1640922001-12-17 15:30:10 +00002179 * are optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00002180 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2181 * references, but XML special chars need to be escaped first by using
2182 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2183 * need entities support.
2184 *
2185 * Returns a pointer to the new node object.
2186 */
2187xmlNodePtr
2188xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns,
2189 const xmlChar *name, const xmlChar *content) {
2190 xmlNodePtr cur;
2191
Daniel Veillard95ddcd32004-10-26 21:53:55 +00002192 if ((doc != NULL) && (doc->dict != NULL))
Daniel Veillard03a53c32004-10-26 16:06:51 +00002193 cur = xmlNewNodeEatName(ns, (xmlChar *)
2194 xmlDictLookup(doc->dict, name, -1));
2195 else
2196 cur = xmlNewNode(ns, name);
Owen Taylor3473f882001-02-23 17:55:21 +00002197 if (cur != NULL) {
2198 cur->doc = doc;
2199 if (content != NULL) {
2200 cur->children = xmlStringGetNodeList(doc, content);
2201 UPDATE_LAST_CHILD_AND_PARENT(cur)
2202 }
2203 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002204
Owen Taylor3473f882001-02-23 17:55:21 +00002205 return(cur);
2206}
2207
Daniel Veillard46de64e2002-05-29 08:21:33 +00002208/**
2209 * xmlNewDocNodeEatName:
2210 * @doc: the document
2211 * @ns: namespace if any
2212 * @name: the node name
2213 * @content: the XML text content if any
2214 *
2215 * Creation of a new node element within a document. @ns and @content
2216 * are optional (NULL).
2217 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2218 * references, but XML special chars need to be escaped first by using
2219 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2220 * need entities support.
2221 *
2222 * Returns a pointer to the new node object.
2223 */
2224xmlNodePtr
2225xmlNewDocNodeEatName(xmlDocPtr doc, xmlNsPtr ns,
2226 xmlChar *name, const xmlChar *content) {
2227 xmlNodePtr cur;
2228
2229 cur = xmlNewNodeEatName(ns, name);
2230 if (cur != NULL) {
2231 cur->doc = doc;
2232 if (content != NULL) {
2233 cur->children = xmlStringGetNodeList(doc, content);
2234 UPDATE_LAST_CHILD_AND_PARENT(cur)
2235 }
2236 }
2237 return(cur);
2238}
2239
Daniel Veillard652327a2003-09-29 18:02:38 +00002240#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002241/**
2242 * xmlNewDocRawNode:
2243 * @doc: the document
2244 * @ns: namespace if any
2245 * @name: the node name
2246 * @content: the text content if any
2247 *
2248 * Creation of a new node element within a document. @ns and @content
Daniel Veillardd1640922001-12-17 15:30:10 +00002249 * are optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00002250 *
2251 * Returns a pointer to the new node object.
2252 */
2253xmlNodePtr
2254xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns,
2255 const xmlChar *name, const xmlChar *content) {
2256 xmlNodePtr cur;
2257
Daniel Veillard95ddcd32004-10-26 21:53:55 +00002258 cur = xmlNewDocNode(doc, ns, name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002259 if (cur != NULL) {
2260 cur->doc = doc;
2261 if (content != NULL) {
2262 cur->children = xmlNewDocText(doc, content);
2263 UPDATE_LAST_CHILD_AND_PARENT(cur)
2264 }
2265 }
2266 return(cur);
2267}
2268
2269/**
2270 * xmlNewDocFragment:
2271 * @doc: the document owning the fragment
2272 *
2273 * Creation of a new Fragment node.
2274 * Returns a pointer to the new node object.
2275 */
2276xmlNodePtr
2277xmlNewDocFragment(xmlDocPtr doc) {
2278 xmlNodePtr cur;
2279
2280 /*
2281 * Allocate a new DocumentFragment node and fill the fields.
2282 */
2283 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2284 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002285 xmlTreeErrMemory("building fragment");
Owen Taylor3473f882001-02-23 17:55:21 +00002286 return(NULL);
2287 }
2288 memset(cur, 0, sizeof(xmlNode));
2289 cur->type = XML_DOCUMENT_FRAG_NODE;
2290
2291 cur->doc = doc;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002292
Daniel Veillarda880b122003-04-21 21:36:41 +00002293 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002294 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002295 return(cur);
2296}
Daniel Veillard652327a2003-09-29 18:02:38 +00002297#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002298
2299/**
2300 * xmlNewText:
2301 * @content: the text content
2302 *
2303 * Creation of a new text node.
2304 * Returns a pointer to the new node object.
2305 */
2306xmlNodePtr
2307xmlNewText(const xmlChar *content) {
2308 xmlNodePtr cur;
2309
2310 /*
2311 * Allocate a new node and fill the fields.
2312 */
2313 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2314 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002315 xmlTreeErrMemory("building text");
Owen Taylor3473f882001-02-23 17:55:21 +00002316 return(NULL);
2317 }
2318 memset(cur, 0, sizeof(xmlNode));
2319 cur->type = XML_TEXT_NODE;
2320
2321 cur->name = xmlStringText;
2322 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002323 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00002324 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002325
Daniel Veillarda880b122003-04-21 21:36:41 +00002326 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002327 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002328 return(cur);
2329}
2330
Daniel Veillard652327a2003-09-29 18:02:38 +00002331#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002332/**
2333 * xmlNewTextChild:
2334 * @parent: the parent node
2335 * @ns: a namespace if any
2336 * @name: the name of the child
2337 * @content: the text content of the child if any.
2338 *
2339 * Creation of a new child element, added at the end of @parent children list.
MST 2003 John Flecke1f70492003-12-20 23:43:28 +00002340 * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
2341 * created element inherits the namespace of @parent. If @content is non NULL,
William M. Brackd7cf7f82003-11-14 07:13:16 +00002342 * a child TEXT node will be created containing the string @content.
MST 2003 John Fleck8b03bc52003-12-17 03:45:01 +00002343 * NOTE: Use xmlNewChild() if @content will contain entities that need to be
2344 * preserved. Use this function, xmlNewTextChild(), if you need to ensure that
2345 * reserved XML chars that might appear in @content, such as the ampersand,
2346 * greater-than or less-than signs, are automatically replaced by their XML
2347 * escaped entity representations.
Owen Taylor3473f882001-02-23 17:55:21 +00002348 *
2349 * Returns a pointer to the new node object.
2350 */
2351xmlNodePtr
2352xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns,
2353 const xmlChar *name, const xmlChar *content) {
2354 xmlNodePtr cur, prev;
2355
2356 if (parent == NULL) {
2357#ifdef DEBUG_TREE
2358 xmlGenericError(xmlGenericErrorContext,
2359 "xmlNewTextChild : parent == NULL\n");
2360#endif
2361 return(NULL);
2362 }
2363
2364 if (name == NULL) {
2365#ifdef DEBUG_TREE
2366 xmlGenericError(xmlGenericErrorContext,
2367 "xmlNewTextChild : name == NULL\n");
2368#endif
2369 return(NULL);
2370 }
2371
2372 /*
2373 * Allocate a new node
2374 */
Daniel Veillard254b1262003-11-01 17:04:58 +00002375 if (parent->type == XML_ELEMENT_NODE) {
2376 if (ns == NULL)
2377 cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content);
2378 else
2379 cur = xmlNewDocRawNode(parent->doc, ns, name, content);
2380 } else if ((parent->type == XML_DOCUMENT_NODE) ||
2381 (parent->type == XML_HTML_DOCUMENT_NODE)) {
2382 if (ns == NULL)
2383 cur = xmlNewDocRawNode((xmlDocPtr) parent, NULL, name, content);
2384 else
2385 cur = xmlNewDocRawNode((xmlDocPtr) parent, ns, name, content);
2386 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2387 cur = xmlNewDocRawNode( parent->doc, ns, name, content);
2388 } else {
2389 return(NULL);
2390 }
Owen Taylor3473f882001-02-23 17:55:21 +00002391 if (cur == NULL) return(NULL);
2392
2393 /*
2394 * add the new element at the end of the children list.
2395 */
2396 cur->type = XML_ELEMENT_NODE;
2397 cur->parent = parent;
2398 cur->doc = parent->doc;
2399 if (parent->children == NULL) {
2400 parent->children = cur;
2401 parent->last = cur;
2402 } else {
2403 prev = parent->last;
2404 prev->next = cur;
2405 cur->prev = prev;
2406 parent->last = cur;
2407 }
2408
2409 return(cur);
2410}
Daniel Veillard652327a2003-09-29 18:02:38 +00002411#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002412
2413/**
2414 * xmlNewCharRef:
2415 * @doc: the document
2416 * @name: the char ref string, starting with # or "&# ... ;"
2417 *
2418 * Creation of a new character reference node.
2419 * Returns a pointer to the new node object.
2420 */
2421xmlNodePtr
2422xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) {
2423 xmlNodePtr cur;
2424
Daniel Veillard36e5cd52004-11-02 14:52:23 +00002425 if (name == NULL)
2426 return(NULL);
2427
Owen Taylor3473f882001-02-23 17:55:21 +00002428 /*
2429 * Allocate a new node and fill the fields.
2430 */
2431 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2432 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002433 xmlTreeErrMemory("building character reference");
Owen Taylor3473f882001-02-23 17:55:21 +00002434 return(NULL);
2435 }
2436 memset(cur, 0, sizeof(xmlNode));
2437 cur->type = XML_ENTITY_REF_NODE;
2438
2439 cur->doc = doc;
2440 if (name[0] == '&') {
2441 int len;
2442 name++;
2443 len = xmlStrlen(name);
2444 if (name[len - 1] == ';')
2445 cur->name = xmlStrndup(name, len - 1);
2446 else
2447 cur->name = xmlStrndup(name, len);
2448 } else
2449 cur->name = xmlStrdup(name);
Daniel Veillard5335dc52003-01-01 20:59:38 +00002450
Daniel Veillarda880b122003-04-21 21:36:41 +00002451 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002452 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002453 return(cur);
2454}
2455
2456/**
2457 * xmlNewReference:
2458 * @doc: the document
2459 * @name: the reference name, or the reference string with & and ;
2460 *
2461 * Creation of a new reference node.
2462 * Returns a pointer to the new node object.
2463 */
2464xmlNodePtr
2465xmlNewReference(xmlDocPtr doc, const xmlChar *name) {
2466 xmlNodePtr cur;
2467 xmlEntityPtr ent;
2468
Daniel Veillard36e5cd52004-11-02 14:52:23 +00002469 if (name == NULL)
2470 return(NULL);
2471
Owen Taylor3473f882001-02-23 17:55:21 +00002472 /*
2473 * Allocate a new node and fill the fields.
2474 */
2475 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2476 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002477 xmlTreeErrMemory("building reference");
Owen Taylor3473f882001-02-23 17:55:21 +00002478 return(NULL);
2479 }
2480 memset(cur, 0, sizeof(xmlNode));
2481 cur->type = XML_ENTITY_REF_NODE;
2482
2483 cur->doc = doc;
2484 if (name[0] == '&') {
2485 int len;
2486 name++;
2487 len = xmlStrlen(name);
2488 if (name[len - 1] == ';')
2489 cur->name = xmlStrndup(name, len - 1);
2490 else
2491 cur->name = xmlStrndup(name, len);
2492 } else
2493 cur->name = xmlStrdup(name);
2494
2495 ent = xmlGetDocEntity(doc, cur->name);
2496 if (ent != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002497 cur->content = ent->content;
Owen Taylor3473f882001-02-23 17:55:21 +00002498 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002499 * The parent pointer in entity is a DTD pointer and thus is NOT
Owen Taylor3473f882001-02-23 17:55:21 +00002500 * updated. Not sure if this is 100% correct.
2501 * -George
2502 */
2503 cur->children = (xmlNodePtr) ent;
2504 cur->last = (xmlNodePtr) ent;
2505 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002506
Daniel Veillarda880b122003-04-21 21:36:41 +00002507 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002508 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002509 return(cur);
2510}
2511
2512/**
2513 * xmlNewDocText:
2514 * @doc: the document
2515 * @content: the text content
2516 *
2517 * Creation of a new text node within a document.
2518 * Returns a pointer to the new node object.
2519 */
2520xmlNodePtr
2521xmlNewDocText(xmlDocPtr doc, const xmlChar *content) {
2522 xmlNodePtr cur;
2523
2524 cur = xmlNewText(content);
2525 if (cur != NULL) cur->doc = doc;
2526 return(cur);
2527}
2528
2529/**
2530 * xmlNewTextLen:
2531 * @content: the text content
2532 * @len: the text len.
2533 *
Daniel Veillard60087f32001-10-10 09:45:09 +00002534 * Creation of a new text node with an extra parameter for the content's length
Owen Taylor3473f882001-02-23 17:55:21 +00002535 * Returns a pointer to the new node object.
2536 */
2537xmlNodePtr
2538xmlNewTextLen(const xmlChar *content, int len) {
2539 xmlNodePtr cur;
2540
2541 /*
2542 * Allocate a new node and fill the fields.
2543 */
2544 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2545 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002546 xmlTreeErrMemory("building text");
Owen Taylor3473f882001-02-23 17:55:21 +00002547 return(NULL);
2548 }
2549 memset(cur, 0, sizeof(xmlNode));
2550 cur->type = XML_TEXT_NODE;
2551
2552 cur->name = xmlStringText;
2553 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002554 cur->content = xmlStrndup(content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00002555 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002556
Daniel Veillarda880b122003-04-21 21:36:41 +00002557 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002558 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002559 return(cur);
2560}
2561
2562/**
2563 * xmlNewDocTextLen:
2564 * @doc: the document
2565 * @content: the text content
2566 * @len: the text len.
2567 *
Daniel Veillard60087f32001-10-10 09:45:09 +00002568 * Creation of a new text node with an extra content length parameter. The
Owen Taylor3473f882001-02-23 17:55:21 +00002569 * text node pertain to a given document.
2570 * Returns a pointer to the new node object.
2571 */
2572xmlNodePtr
2573xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) {
2574 xmlNodePtr cur;
2575
2576 cur = xmlNewTextLen(content, len);
2577 if (cur != NULL) cur->doc = doc;
2578 return(cur);
2579}
2580
2581/**
2582 * xmlNewComment:
2583 * @content: the comment content
2584 *
2585 * Creation of a new node containing a comment.
2586 * Returns a pointer to the new node object.
2587 */
2588xmlNodePtr
2589xmlNewComment(const xmlChar *content) {
2590 xmlNodePtr cur;
2591
2592 /*
2593 * Allocate a new node and fill the fields.
2594 */
2595 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2596 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002597 xmlTreeErrMemory("building comment");
Owen Taylor3473f882001-02-23 17:55:21 +00002598 return(NULL);
2599 }
2600 memset(cur, 0, sizeof(xmlNode));
2601 cur->type = XML_COMMENT_NODE;
2602
2603 cur->name = xmlStringComment;
2604 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002605 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00002606 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002607
Daniel Veillarda880b122003-04-21 21:36:41 +00002608 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002609 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002610 return(cur);
2611}
2612
2613/**
2614 * xmlNewCDataBlock:
2615 * @doc: the document
Daniel Veillardd1640922001-12-17 15:30:10 +00002616 * @content: the CDATA block content content
Owen Taylor3473f882001-02-23 17:55:21 +00002617 * @len: the length of the block
2618 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002619 * Creation of a new node containing a CDATA block.
Owen Taylor3473f882001-02-23 17:55:21 +00002620 * Returns a pointer to the new node object.
2621 */
2622xmlNodePtr
2623xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) {
2624 xmlNodePtr cur;
2625
2626 /*
2627 * Allocate a new node and fill the fields.
2628 */
2629 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2630 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002631 xmlTreeErrMemory("building CDATA");
Owen Taylor3473f882001-02-23 17:55:21 +00002632 return(NULL);
2633 }
2634 memset(cur, 0, sizeof(xmlNode));
2635 cur->type = XML_CDATA_SECTION_NODE;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002636 cur->doc = doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002637
2638 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002639 cur->content = xmlStrndup(content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00002640 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002641
Daniel Veillarda880b122003-04-21 21:36:41 +00002642 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002643 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002644 return(cur);
2645}
2646
2647/**
2648 * xmlNewDocComment:
2649 * @doc: the document
2650 * @content: the comment content
2651 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002652 * Creation of a new node containing a comment within a document.
Owen Taylor3473f882001-02-23 17:55:21 +00002653 * Returns a pointer to the new node object.
2654 */
2655xmlNodePtr
2656xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) {
2657 xmlNodePtr cur;
2658
2659 cur = xmlNewComment(content);
2660 if (cur != NULL) cur->doc = doc;
2661 return(cur);
2662}
2663
2664/**
2665 * xmlSetTreeDoc:
2666 * @tree: the top element
2667 * @doc: the document
2668 *
2669 * update all nodes under the tree to point to the right document
2670 */
2671void
2672xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) {
Daniel Veillard19e96c32001-07-09 10:32:59 +00002673 xmlAttrPtr prop;
2674
Owen Taylor3473f882001-02-23 17:55:21 +00002675 if (tree == NULL)
2676 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002677 if (tree->doc != doc) {
Daniel Veillard36065812002-01-24 15:02:46 +00002678 if(tree->type == XML_ELEMENT_NODE) {
2679 prop = tree->properties;
2680 while (prop != NULL) {
2681 prop->doc = doc;
2682 xmlSetListDoc(prop->children, doc);
2683 prop = prop->next;
2684 }
Daniel Veillard19e96c32001-07-09 10:32:59 +00002685 }
Owen Taylor3473f882001-02-23 17:55:21 +00002686 if (tree->children != NULL)
2687 xmlSetListDoc(tree->children, doc);
2688 tree->doc = doc;
2689 }
2690}
2691
2692/**
2693 * xmlSetListDoc:
Daniel Veillardd1640922001-12-17 15:30:10 +00002694 * @list: the first element
Owen Taylor3473f882001-02-23 17:55:21 +00002695 * @doc: the document
2696 *
2697 * update all nodes in the list to point to the right document
2698 */
2699void
2700xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) {
2701 xmlNodePtr cur;
2702
2703 if (list == NULL)
2704 return;
2705 cur = list;
2706 while (cur != NULL) {
2707 if (cur->doc != doc)
2708 xmlSetTreeDoc(cur, doc);
2709 cur = cur->next;
2710 }
2711}
2712
Daniel Veillard2156d432004-03-04 15:59:36 +00002713#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00002714/**
2715 * xmlNewChild:
2716 * @parent: the parent node
2717 * @ns: a namespace if any
2718 * @name: the name of the child
2719 * @content: the XML content of the child if any.
2720 *
2721 * Creation of a new child element, added at the end of @parent children list.
MST 2003 John Flecke1f70492003-12-20 23:43:28 +00002722 * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
2723 * created element inherits the namespace of @parent. If @content is non NULL,
Owen Taylor3473f882001-02-23 17:55:21 +00002724 * a child list containing the TEXTs and ENTITY_REFs node will be created.
MST 2003 John Fleck8b03bc52003-12-17 03:45:01 +00002725 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
2726 * references. XML special chars must be escaped first by using
2727 * xmlEncodeEntitiesReentrant(), or xmlNewTextChild() should be used.
Owen Taylor3473f882001-02-23 17:55:21 +00002728 *
2729 * Returns a pointer to the new node object.
2730 */
2731xmlNodePtr
2732xmlNewChild(xmlNodePtr parent, xmlNsPtr ns,
2733 const xmlChar *name, const xmlChar *content) {
2734 xmlNodePtr cur, prev;
2735
2736 if (parent == NULL) {
2737#ifdef DEBUG_TREE
2738 xmlGenericError(xmlGenericErrorContext,
2739 "xmlNewChild : parent == NULL\n");
2740#endif
2741 return(NULL);
2742 }
2743
2744 if (name == NULL) {
2745#ifdef DEBUG_TREE
2746 xmlGenericError(xmlGenericErrorContext,
2747 "xmlNewChild : name == NULL\n");
2748#endif
2749 return(NULL);
2750 }
2751
2752 /*
2753 * Allocate a new node
2754 */
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002755 if (parent->type == XML_ELEMENT_NODE) {
2756 if (ns == NULL)
2757 cur = xmlNewDocNode(parent->doc, parent->ns, name, content);
2758 else
2759 cur = xmlNewDocNode(parent->doc, ns, name, content);
2760 } else if ((parent->type == XML_DOCUMENT_NODE) ||
2761 (parent->type == XML_HTML_DOCUMENT_NODE)) {
2762 if (ns == NULL)
2763 cur = xmlNewDocNode((xmlDocPtr) parent, NULL, name, content);
2764 else
2765 cur = xmlNewDocNode((xmlDocPtr) parent, ns, name, content);
Daniel Veillard7e3f1402002-10-28 18:52:57 +00002766 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2767 cur = xmlNewDocNode( parent->doc, ns, name, content);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002768 } else {
2769 return(NULL);
2770 }
Owen Taylor3473f882001-02-23 17:55:21 +00002771 if (cur == NULL) return(NULL);
2772
2773 /*
2774 * add the new element at the end of the children list.
2775 */
2776 cur->type = XML_ELEMENT_NODE;
2777 cur->parent = parent;
2778 cur->doc = parent->doc;
2779 if (parent->children == NULL) {
2780 parent->children = cur;
2781 parent->last = cur;
2782 } else {
2783 prev = parent->last;
2784 prev->next = cur;
2785 cur->prev = prev;
2786 parent->last = cur;
2787 }
2788
2789 return(cur);
2790}
Daniel Veillard652327a2003-09-29 18:02:38 +00002791#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002792
2793/**
2794 * xmlAddNextSibling:
2795 * @cur: the child node
2796 * @elem: the new node
2797 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002798 * Add a new node @elem as the next sibling of @cur
2799 * If the new node was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00002800 * first unlinked from its existing context.
2801 * As a result of text merging @elem may be freed.
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002802 * If the new node is ATTRIBUTE, it is added into properties instead of children.
2803 * If there is an attribute with equal name, it is first destroyed.
Owen Taylor3473f882001-02-23 17:55:21 +00002804 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002805 * Returns the new node or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002806 */
2807xmlNodePtr
2808xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) {
2809 if (cur == NULL) {
2810#ifdef DEBUG_TREE
2811 xmlGenericError(xmlGenericErrorContext,
2812 "xmlAddNextSibling : cur == NULL\n");
2813#endif
2814 return(NULL);
2815 }
2816 if (elem == NULL) {
2817#ifdef DEBUG_TREE
2818 xmlGenericError(xmlGenericErrorContext,
2819 "xmlAddNextSibling : elem == NULL\n");
2820#endif
2821 return(NULL);
2822 }
2823
2824 xmlUnlinkNode(elem);
2825
2826 if (elem->type == XML_TEXT_NODE) {
2827 if (cur->type == XML_TEXT_NODE) {
Owen Taylor3473f882001-02-23 17:55:21 +00002828 xmlNodeAddContent(cur, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002829 xmlFreeNode(elem);
2830 return(cur);
2831 }
Daniel Veillard9e1c72d2001-08-31 20:03:19 +00002832 if ((cur->next != NULL) && (cur->next->type == XML_TEXT_NODE) &&
2833 (cur->name == cur->next->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002834 xmlChar *tmp;
2835
2836 tmp = xmlStrdup(elem->content);
2837 tmp = xmlStrcat(tmp, cur->next->content);
2838 xmlNodeSetContent(cur->next, tmp);
2839 xmlFree(tmp);
Owen Taylor3473f882001-02-23 17:55:21 +00002840 xmlFreeNode(elem);
2841 return(cur->next);
2842 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002843 } else if (elem->type == XML_ATTRIBUTE_NODE) {
2844 /* check if an attribute with the same name exists */
2845 xmlAttrPtr attr;
2846
2847 if (elem->ns == NULL)
2848 attr = xmlHasProp(cur->parent, elem->name);
2849 else
2850 attr = xmlHasNsProp(cur->parent, elem->name, elem->ns->href);
2851 if ((attr != NULL) && (attr != (xmlAttrPtr) elem)) {
2852 /* different instance, destroy it (attributes must be unique) */
2853 xmlFreeProp(attr);
2854 }
Owen Taylor3473f882001-02-23 17:55:21 +00002855 }
2856
2857 if (elem->doc != cur->doc) {
2858 xmlSetTreeDoc(elem, cur->doc);
2859 }
2860 elem->parent = cur->parent;
2861 elem->prev = cur;
2862 elem->next = cur->next;
2863 cur->next = elem;
2864 if (elem->next != NULL)
2865 elem->next->prev = elem;
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002866 if ((elem->parent != NULL) && (elem->parent->last == cur) && (elem->type != XML_ATTRIBUTE_NODE))
Owen Taylor3473f882001-02-23 17:55:21 +00002867 elem->parent->last = elem;
2868 return(elem);
2869}
2870
William M. Brack21e4ef22005-01-02 09:53:13 +00002871#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
2872 defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00002873/**
2874 * xmlAddPrevSibling:
2875 * @cur: the child node
2876 * @elem: the new node
2877 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002878 * Add a new node @elem as the previous sibling of @cur
Owen Taylor3473f882001-02-23 17:55:21 +00002879 * merging adjacent TEXT nodes (@elem may be freed)
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002880 * If the new node was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00002881 * first unlinked from its existing context.
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002882 * If the new node is ATTRIBUTE, it is added into properties instead of children.
2883 * If there is an attribute with equal name, it is first destroyed.
Owen Taylor3473f882001-02-23 17:55:21 +00002884 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002885 * Returns the new node or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002886 */
2887xmlNodePtr
2888xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) {
2889 if (cur == NULL) {
2890#ifdef DEBUG_TREE
2891 xmlGenericError(xmlGenericErrorContext,
2892 "xmlAddPrevSibling : cur == NULL\n");
2893#endif
2894 return(NULL);
2895 }
2896 if (elem == NULL) {
2897#ifdef DEBUG_TREE
2898 xmlGenericError(xmlGenericErrorContext,
2899 "xmlAddPrevSibling : elem == NULL\n");
2900#endif
2901 return(NULL);
2902 }
2903
2904 xmlUnlinkNode(elem);
2905
2906 if (elem->type == XML_TEXT_NODE) {
2907 if (cur->type == XML_TEXT_NODE) {
Owen Taylor3473f882001-02-23 17:55:21 +00002908 xmlChar *tmp;
2909
2910 tmp = xmlStrdup(elem->content);
2911 tmp = xmlStrcat(tmp, cur->content);
2912 xmlNodeSetContent(cur, tmp);
2913 xmlFree(tmp);
Owen Taylor3473f882001-02-23 17:55:21 +00002914 xmlFreeNode(elem);
2915 return(cur);
2916 }
Daniel Veillard9e1c72d2001-08-31 20:03:19 +00002917 if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE) &&
2918 (cur->name == cur->prev->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002919 xmlNodeAddContent(cur->prev, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002920 xmlFreeNode(elem);
2921 return(cur->prev);
2922 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002923 } else if (elem->type == XML_ATTRIBUTE_NODE) {
2924 /* check if an attribute with the same name exists */
2925 xmlAttrPtr attr;
2926
2927 if (elem->ns == NULL)
2928 attr = xmlHasProp(cur->parent, elem->name);
2929 else
2930 attr = xmlHasNsProp(cur->parent, elem->name, elem->ns->href);
2931 if ((attr != NULL) && (attr != (xmlAttrPtr) elem)) {
2932 /* different instance, destroy it (attributes must be unique) */
2933 xmlFreeProp(attr);
2934 }
Owen Taylor3473f882001-02-23 17:55:21 +00002935 }
2936
2937 if (elem->doc != cur->doc) {
2938 xmlSetTreeDoc(elem, cur->doc);
2939 }
2940 elem->parent = cur->parent;
2941 elem->next = cur;
2942 elem->prev = cur->prev;
2943 cur->prev = elem;
2944 if (elem->prev != NULL)
2945 elem->prev->next = elem;
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002946 if (elem->parent != NULL) {
2947 if (elem->type == XML_ATTRIBUTE_NODE) {
2948 if (elem->parent->properties == (xmlAttrPtr) cur) {
2949 elem->parent->properties = (xmlAttrPtr) elem;
2950 }
2951 } else {
2952 if (elem->parent->children == cur) {
2953 elem->parent->children = elem;
2954 }
2955 }
2956 }
Owen Taylor3473f882001-02-23 17:55:21 +00002957 return(elem);
2958}
Daniel Veillard652327a2003-09-29 18:02:38 +00002959#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002960
2961/**
2962 * xmlAddSibling:
2963 * @cur: the child node
2964 * @elem: the new node
2965 *
2966 * Add a new element @elem to the list of siblings of @cur
2967 * merging adjacent TEXT nodes (@elem may be freed)
2968 * If the new element was already inserted in a document it is
2969 * first unlinked from its existing context.
2970 *
2971 * Returns the new element or NULL in case of error.
2972 */
2973xmlNodePtr
2974xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) {
2975 xmlNodePtr parent;
2976
2977 if (cur == NULL) {
2978#ifdef DEBUG_TREE
2979 xmlGenericError(xmlGenericErrorContext,
2980 "xmlAddSibling : cur == NULL\n");
2981#endif
2982 return(NULL);
2983 }
2984
2985 if (elem == NULL) {
2986#ifdef DEBUG_TREE
2987 xmlGenericError(xmlGenericErrorContext,
2988 "xmlAddSibling : elem == NULL\n");
2989#endif
2990 return(NULL);
2991 }
2992
2993 /*
2994 * Constant time is we can rely on the ->parent->last to find
2995 * the last sibling.
2996 */
2997 if ((cur->parent != NULL) &&
2998 (cur->parent->children != NULL) &&
2999 (cur->parent->last != NULL) &&
3000 (cur->parent->last->next == NULL)) {
3001 cur = cur->parent->last;
3002 } else {
3003 while (cur->next != NULL) cur = cur->next;
3004 }
3005
3006 xmlUnlinkNode(elem);
3007
Daniel Veillarde22dd5c2003-10-29 12:53:27 +00003008 if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE) &&
3009 (cur->name == elem->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003010 xmlNodeAddContent(cur, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003011 xmlFreeNode(elem);
3012 return(cur);
3013 }
3014
3015 if (elem->doc != cur->doc) {
3016 xmlSetTreeDoc(elem, cur->doc);
3017 }
3018 parent = cur->parent;
3019 elem->prev = cur;
3020 elem->next = NULL;
3021 elem->parent = parent;
3022 cur->next = elem;
3023 if (parent != NULL)
3024 parent->last = elem;
3025
3026 return(elem);
3027}
3028
3029/**
3030 * xmlAddChildList:
3031 * @parent: the parent node
3032 * @cur: the first node in the list
3033 *
3034 * Add a list of node at the end of the child list of the parent
3035 * merging adjacent TEXT nodes (@cur may be freed)
3036 *
3037 * Returns the last child or NULL in case of error.
3038 */
3039xmlNodePtr
3040xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) {
3041 xmlNodePtr prev;
3042
3043 if (parent == NULL) {
3044#ifdef DEBUG_TREE
3045 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00003046 "xmlAddChildList : parent == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003047#endif
3048 return(NULL);
3049 }
3050
3051 if (cur == NULL) {
3052#ifdef DEBUG_TREE
3053 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00003054 "xmlAddChildList : child == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003055#endif
3056 return(NULL);
3057 }
3058
3059 if ((cur->doc != NULL) && (parent->doc != NULL) &&
3060 (cur->doc != parent->doc)) {
3061#ifdef DEBUG_TREE
3062 xmlGenericError(xmlGenericErrorContext,
3063 "Elements moved to a different document\n");
3064#endif
3065 }
3066
3067 /*
3068 * add the first element at the end of the children list.
3069 */
Daniel Veillard5335dc52003-01-01 20:59:38 +00003070
Owen Taylor3473f882001-02-23 17:55:21 +00003071 if (parent->children == NULL) {
3072 parent->children = cur;
3073 } else {
3074 /*
3075 * If cur and parent->last both are TEXT nodes, then merge them.
3076 */
3077 if ((cur->type == XML_TEXT_NODE) &&
3078 (parent->last->type == XML_TEXT_NODE) &&
3079 (cur->name == parent->last->name)) {
Daniel Veillard5335dc52003-01-01 20:59:38 +00003080 xmlNodeAddContent(parent->last, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003081 /*
3082 * if it's the only child, nothing more to be done.
3083 */
3084 if (cur->next == NULL) {
3085 xmlFreeNode(cur);
3086 return(parent->last);
3087 }
3088 prev = cur;
3089 cur = cur->next;
3090 xmlFreeNode(prev);
3091 }
3092 prev = parent->last;
3093 prev->next = cur;
3094 cur->prev = prev;
3095 }
3096 while (cur->next != NULL) {
3097 cur->parent = parent;
3098 if (cur->doc != parent->doc) {
3099 xmlSetTreeDoc(cur, parent->doc);
3100 }
3101 cur = cur->next;
3102 }
3103 cur->parent = parent;
3104 cur->doc = parent->doc; /* the parent may not be linked to a doc ! */
3105 parent->last = cur;
3106
3107 return(cur);
3108}
3109
3110/**
3111 * xmlAddChild:
3112 * @parent: the parent node
3113 * @cur: the child node
3114 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003115 * Add a new node to @parent, at the end of the child (or property) list
Owen Taylor3473f882001-02-23 17:55:21 +00003116 * merging adjacent TEXT nodes (in which case @cur is freed)
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003117 * If the new node is ATTRIBUTE, it is added into properties instead of children.
3118 * If there is an attribute with equal name, it is first destroyed.
3119 *
Owen Taylor3473f882001-02-23 17:55:21 +00003120 * Returns the child or NULL in case of error.
3121 */
3122xmlNodePtr
3123xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) {
3124 xmlNodePtr prev;
3125
3126 if (parent == NULL) {
3127#ifdef DEBUG_TREE
3128 xmlGenericError(xmlGenericErrorContext,
3129 "xmlAddChild : parent == NULL\n");
3130#endif
3131 return(NULL);
3132 }
3133
3134 if (cur == NULL) {
3135#ifdef DEBUG_TREE
3136 xmlGenericError(xmlGenericErrorContext,
3137 "xmlAddChild : child == NULL\n");
3138#endif
3139 return(NULL);
3140 }
3141
Owen Taylor3473f882001-02-23 17:55:21 +00003142 /*
3143 * If cur is a TEXT node, merge its content with adjacent TEXT nodes
Owen Taylor3473f882001-02-23 17:55:21 +00003144 * cur is then freed.
3145 */
3146 if (cur->type == XML_TEXT_NODE) {
Daniel Veillard7db37732001-07-12 01:20:08 +00003147 if ((parent->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003148 (parent->content != NULL) &&
Daniel Veillarde22dd5c2003-10-29 12:53:27 +00003149 (parent->name == cur->name) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003150 (parent != cur)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003151 xmlNodeAddContent(parent, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003152 xmlFreeNode(cur);
3153 return(parent);
3154 }
3155 if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003156 (parent->last->name == cur->name) &&
3157 (parent->last != cur)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003158 xmlNodeAddContent(parent->last, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003159 xmlFreeNode(cur);
3160 return(parent->last);
3161 }
3162 }
3163
3164 /*
3165 * add the new element at the end of the children list.
3166 */
Daniel Veillard5335dc52003-01-01 20:59:38 +00003167 prev = cur->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00003168 cur->parent = parent;
3169 if (cur->doc != parent->doc) {
3170 xmlSetTreeDoc(cur, parent->doc);
3171 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00003172 /* this check prevents a loop on tree-traversions if a developer
3173 * tries to add a node to its parent multiple times
3174 */
3175 if (prev == parent)
3176 return(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00003177
3178 /*
Daniel Veillard7db37732001-07-12 01:20:08 +00003179 * Coalescing
Owen Taylor3473f882001-02-23 17:55:21 +00003180 */
Daniel Veillard7db37732001-07-12 01:20:08 +00003181 if ((parent->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003182 (parent->content != NULL) &&
3183 (parent != cur)) {
Daniel Veillard7db37732001-07-12 01:20:08 +00003184 xmlNodeAddContent(parent, cur->content);
Daniel Veillard7db37732001-07-12 01:20:08 +00003185 xmlFreeNode(cur);
3186 return(parent);
Owen Taylor3473f882001-02-23 17:55:21 +00003187 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003188 if (cur->type == XML_ATTRIBUTE_NODE) {
3189 if (parent->properties == NULL) {
3190 parent->properties = (xmlAttrPtr) cur;
3191 } else {
3192 /* check if an attribute with the same name exists */
3193 xmlAttrPtr lastattr;
Owen Taylor3473f882001-02-23 17:55:21 +00003194
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003195 if (cur->ns == NULL)
3196 lastattr = xmlHasProp(parent, cur->name);
3197 else
3198 lastattr = xmlHasNsProp(parent, cur->name, cur->ns->href);
3199 if ((lastattr != NULL) && (lastattr != (xmlAttrPtr) cur)) {
3200 /* different instance, destroy it (attributes must be unique) */
3201 xmlFreeProp(lastattr);
3202 }
3203 /* find the end */
3204 lastattr = parent->properties;
3205 while (lastattr->next != NULL) {
3206 lastattr = lastattr->next;
3207 }
3208 lastattr->next = (xmlAttrPtr) cur;
3209 ((xmlAttrPtr) cur)->prev = lastattr;
3210 }
3211 } else {
3212 if (parent->children == NULL) {
3213 parent->children = cur;
3214 parent->last = cur;
3215 } else {
3216 prev = parent->last;
3217 prev->next = cur;
3218 cur->prev = prev;
3219 parent->last = cur;
3220 }
3221 }
Owen Taylor3473f882001-02-23 17:55:21 +00003222 return(cur);
3223}
3224
3225/**
3226 * xmlGetLastChild:
3227 * @parent: the parent node
3228 *
3229 * Search the last child of a node.
3230 * Returns the last child or NULL if none.
3231 */
3232xmlNodePtr
3233xmlGetLastChild(xmlNodePtr parent) {
3234 if (parent == NULL) {
3235#ifdef DEBUG_TREE
3236 xmlGenericError(xmlGenericErrorContext,
3237 "xmlGetLastChild : parent == NULL\n");
3238#endif
3239 return(NULL);
3240 }
3241 return(parent->last);
3242}
3243
3244/**
3245 * xmlFreeNodeList:
3246 * @cur: the first node in the list
3247 *
3248 * Free a node and all its siblings, this is a recursive behaviour, all
3249 * the children are freed too.
3250 */
3251void
3252xmlFreeNodeList(xmlNodePtr cur) {
3253 xmlNodePtr next;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003254 xmlDictPtr dict = NULL;
3255
3256 if (cur == NULL) return;
Daniel Veillarde6a55192002-01-14 17:11:53 +00003257 if (cur->type == XML_NAMESPACE_DECL) {
3258 xmlFreeNsList((xmlNsPtr) cur);
3259 return;
3260 }
Daniel Veillard9adc0462003-03-24 18:39:54 +00003261 if ((cur->type == XML_DOCUMENT_NODE) ||
3262#ifdef LIBXML_DOCB_ENABLED
3263 (cur->type == XML_DOCB_DOCUMENT_NODE) ||
Daniel Veillard9adc0462003-03-24 18:39:54 +00003264#endif
Daniel Veillard6560a422003-03-27 21:25:38 +00003265 (cur->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard9adc0462003-03-24 18:39:54 +00003266 xmlFreeDoc((xmlDocPtr) cur);
3267 return;
3268 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003269 if (cur->doc != NULL) dict = cur->doc->dict;
Owen Taylor3473f882001-02-23 17:55:21 +00003270 while (cur != NULL) {
3271 next = cur->next;
Daniel Veillard02141ea2001-04-30 11:46:40 +00003272 if (cur->type != XML_DTD_NODE) {
Daniel Veillard5335dc52003-01-01 20:59:38 +00003273
Daniel Veillarda880b122003-04-21 21:36:41 +00003274 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00003275 xmlDeregisterNodeDefaultValue(cur);
3276
Daniel Veillard02141ea2001-04-30 11:46:40 +00003277 if ((cur->children != NULL) &&
3278 (cur->type != XML_ENTITY_REF_NODE))
3279 xmlFreeNodeList(cur->children);
Daniel Veillard01c13b52002-12-10 15:19:08 +00003280 if (((cur->type == XML_ELEMENT_NODE) ||
3281 (cur->type == XML_XINCLUDE_START) ||
3282 (cur->type == XML_XINCLUDE_END)) &&
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003283 (cur->properties != NULL))
Daniel Veillard02141ea2001-04-30 11:46:40 +00003284 xmlFreePropList(cur->properties);
Daniel Veillard7db37732001-07-12 01:20:08 +00003285 if ((cur->type != XML_ELEMENT_NODE) &&
3286 (cur->type != XML_XINCLUDE_START) &&
3287 (cur->type != XML_XINCLUDE_END) &&
3288 (cur->type != XML_ENTITY_REF_NODE)) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003289 DICT_FREE(cur->content)
Daniel Veillard7db37732001-07-12 01:20:08 +00003290 }
3291 if (((cur->type == XML_ELEMENT_NODE) ||
3292 (cur->type == XML_XINCLUDE_START) ||
3293 (cur->type == XML_XINCLUDE_END)) &&
3294 (cur->nsDef != NULL))
3295 xmlFreeNsList(cur->nsDef);
3296
Daniel Veillard9cc6dc62001-06-11 08:09:20 +00003297 /*
3298 * When a node is a text node or a comment, it uses a global static
3299 * variable for the name of the node.
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003300 * Otherwise the node name might come from the document's
3301 * dictionnary
Daniel Veillard9cc6dc62001-06-11 08:09:20 +00003302 */
Daniel Veillard02141ea2001-04-30 11:46:40 +00003303 if ((cur->name != NULL) &&
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003304 (cur->type != XML_TEXT_NODE) &&
3305 (cur->type != XML_COMMENT_NODE))
3306 DICT_FREE(cur->name)
Daniel Veillard02141ea2001-04-30 11:46:40 +00003307 xmlFree(cur);
3308 }
Owen Taylor3473f882001-02-23 17:55:21 +00003309 cur = next;
3310 }
3311}
3312
3313/**
3314 * xmlFreeNode:
3315 * @cur: the node
3316 *
3317 * Free a node, this is a recursive behaviour, all the children are freed too.
3318 * This doesn't unlink the child from the list, use xmlUnlinkNode() first.
3319 */
3320void
3321xmlFreeNode(xmlNodePtr cur) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003322 xmlDictPtr dict = NULL;
3323
3324 if (cur == NULL) return;
Daniel Veillard5335dc52003-01-01 20:59:38 +00003325
Daniel Veillard02141ea2001-04-30 11:46:40 +00003326 /* use xmlFreeDtd for DTD nodes */
Daniel Veillarde6a55192002-01-14 17:11:53 +00003327 if (cur->type == XML_DTD_NODE) {
3328 xmlFreeDtd((xmlDtdPtr) cur);
Owen Taylor3473f882001-02-23 17:55:21 +00003329 return;
Daniel Veillarde6a55192002-01-14 17:11:53 +00003330 }
3331 if (cur->type == XML_NAMESPACE_DECL) {
3332 xmlFreeNs((xmlNsPtr) cur);
3333 return;
3334 }
Daniel Veillarda70d62f2002-11-07 14:18:03 +00003335 if (cur->type == XML_ATTRIBUTE_NODE) {
3336 xmlFreeProp((xmlAttrPtr) cur);
3337 return;
3338 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00003339
Daniel Veillarda880b122003-04-21 21:36:41 +00003340 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00003341 xmlDeregisterNodeDefaultValue(cur);
3342
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003343 if (cur->doc != NULL) dict = cur->doc->dict;
3344
Owen Taylor3473f882001-02-23 17:55:21 +00003345 if ((cur->children != NULL) &&
3346 (cur->type != XML_ENTITY_REF_NODE))
3347 xmlFreeNodeList(cur->children);
Daniel Veillard01c13b52002-12-10 15:19:08 +00003348 if (((cur->type == XML_ELEMENT_NODE) ||
3349 (cur->type == XML_XINCLUDE_START) ||
3350 (cur->type == XML_XINCLUDE_END)) &&
3351 (cur->properties != NULL))
Daniel Veillard02141ea2001-04-30 11:46:40 +00003352 xmlFreePropList(cur->properties);
Daniel Veillard7db37732001-07-12 01:20:08 +00003353 if ((cur->type != XML_ELEMENT_NODE) &&
3354 (cur->content != NULL) &&
3355 (cur->type != XML_ENTITY_REF_NODE) &&
3356 (cur->type != XML_XINCLUDE_END) &&
3357 (cur->type != XML_XINCLUDE_START)) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003358 DICT_FREE(cur->content)
Daniel Veillard7db37732001-07-12 01:20:08 +00003359 }
3360
Daniel Veillardacd370f2001-06-09 17:17:51 +00003361 /*
3362 * When a node is a text node or a comment, it uses a global static
3363 * variable for the name of the node.
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003364 * Otherwise the node name might come from the document's dictionnary
Daniel Veillardacd370f2001-06-09 17:17:51 +00003365 */
Owen Taylor3473f882001-02-23 17:55:21 +00003366 if ((cur->name != NULL) &&
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003367 (cur->type != XML_TEXT_NODE) &&
3368 (cur->type != XML_COMMENT_NODE))
3369 DICT_FREE(cur->name)
Daniel Veillardacd370f2001-06-09 17:17:51 +00003370
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003371 if (((cur->type == XML_ELEMENT_NODE) ||
3372 (cur->type == XML_XINCLUDE_START) ||
3373 (cur->type == XML_XINCLUDE_END)) &&
3374 (cur->nsDef != NULL))
3375 xmlFreeNsList(cur->nsDef);
Owen Taylor3473f882001-02-23 17:55:21 +00003376 xmlFree(cur);
3377}
3378
3379/**
3380 * xmlUnlinkNode:
3381 * @cur: the node
3382 *
3383 * Unlink a node from it's current context, the node is not freed
3384 */
3385void
3386xmlUnlinkNode(xmlNodePtr cur) {
3387 if (cur == NULL) {
3388#ifdef DEBUG_TREE
3389 xmlGenericError(xmlGenericErrorContext,
3390 "xmlUnlinkNode : node == NULL\n");
3391#endif
3392 return;
3393 }
Daniel Veillard29e43992001-12-13 22:21:58 +00003394 if (cur->type == XML_DTD_NODE) {
3395 xmlDocPtr doc;
3396 doc = cur->doc;
Daniel Veillarda067e652003-05-01 08:03:46 +00003397 if (doc != NULL) {
3398 if (doc->intSubset == (xmlDtdPtr) cur)
3399 doc->intSubset = NULL;
3400 if (doc->extSubset == (xmlDtdPtr) cur)
3401 doc->extSubset = NULL;
3402 }
Daniel Veillard29e43992001-12-13 22:21:58 +00003403 }
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003404 if (cur->parent != NULL) {
3405 xmlNodePtr parent;
3406 parent = cur->parent;
3407 if (cur->type == XML_ATTRIBUTE_NODE) {
Daniel Veillardda6f4af2005-06-20 17:17:54 +00003408 /* If attribute is an ID from subset then remove it */
3409 if ((((xmlAttrPtr) cur)->atype == XML_ATTRIBUTE_ID) &&
3410 xmlIsID(parent->doc, parent, (xmlAttrPtr) cur)) {
3411 xmlRemoveID(cur->doc, (xmlAttrPtr) cur);
3412 }
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003413 if (parent->properties == (xmlAttrPtr) cur)
3414 parent->properties = ((xmlAttrPtr) cur)->next;
3415 } else {
3416 if (parent->children == cur)
3417 parent->children = cur->next;
3418 if (parent->last == cur)
3419 parent->last = cur->prev;
3420 }
3421 cur->parent = NULL;
3422 }
Owen Taylor3473f882001-02-23 17:55:21 +00003423 if (cur->next != NULL)
3424 cur->next->prev = cur->prev;
3425 if (cur->prev != NULL)
3426 cur->prev->next = cur->next;
3427 cur->next = cur->prev = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00003428}
3429
Daniel Veillard2156d432004-03-04 15:59:36 +00003430#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00003431/**
3432 * xmlReplaceNode:
3433 * @old: the old node
3434 * @cur: the node
3435 *
William M. Brackd7cf7f82003-11-14 07:13:16 +00003436 * Unlink the old node from its current context, prune the new one
Daniel Veillardd1640922001-12-17 15:30:10 +00003437 * at the same place. If @cur was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00003438 * first unlinked from its existing context.
3439 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003440 * Returns the @old node
Owen Taylor3473f882001-02-23 17:55:21 +00003441 */
3442xmlNodePtr
3443xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) {
Daniel Veillardce244ad2004-11-05 10:03:46 +00003444 if (old == cur) return(NULL);
Daniel Veillarda03e3652004-11-02 18:45:30 +00003445 if ((old == NULL) || (old->parent == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003446#ifdef DEBUG_TREE
3447 xmlGenericError(xmlGenericErrorContext,
Daniel Veillarda03e3652004-11-02 18:45:30 +00003448 "xmlReplaceNode : old == NULL or without parent\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003449#endif
3450 return(NULL);
3451 }
3452 if (cur == NULL) {
3453 xmlUnlinkNode(old);
3454 return(old);
3455 }
3456 if (cur == old) {
3457 return(old);
3458 }
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003459 if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) {
3460#ifdef DEBUG_TREE
3461 xmlGenericError(xmlGenericErrorContext,
3462 "xmlReplaceNode : Trying to replace attribute node with other node type\n");
3463#endif
3464 return(old);
3465 }
3466 if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) {
3467#ifdef DEBUG_TREE
3468 xmlGenericError(xmlGenericErrorContext,
3469 "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n");
3470#endif
3471 return(old);
3472 }
Owen Taylor3473f882001-02-23 17:55:21 +00003473 xmlUnlinkNode(cur);
Daniel Veillard64d7d122005-05-11 18:03:42 +00003474 xmlSetTreeDoc(cur, old->doc);
Owen Taylor3473f882001-02-23 17:55:21 +00003475 cur->parent = old->parent;
3476 cur->next = old->next;
3477 if (cur->next != NULL)
3478 cur->next->prev = cur;
3479 cur->prev = old->prev;
3480 if (cur->prev != NULL)
3481 cur->prev->next = cur;
3482 if (cur->parent != NULL) {
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003483 if (cur->type == XML_ATTRIBUTE_NODE) {
3484 if (cur->parent->properties == (xmlAttrPtr)old)
3485 cur->parent->properties = ((xmlAttrPtr) cur);
Daniel Veillardda6f4af2005-06-20 17:17:54 +00003486
3487 /* If old attribute is ID and defined in DTD then remove ID */
3488 if ((((xmlAttrPtr) old)->atype == XML_ATTRIBUTE_ID) &&
3489 xmlIsID(old->doc, old->parent, (xmlAttrPtr) old)) {
3490 xmlRemoveID(old->doc, (xmlAttrPtr) old);
3491 }
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003492 } else {
3493 if (cur->parent->children == old)
3494 cur->parent->children = cur;
3495 if (cur->parent->last == old)
3496 cur->parent->last = cur;
3497 }
Owen Taylor3473f882001-02-23 17:55:21 +00003498 }
3499 old->next = old->prev = NULL;
3500 old->parent = NULL;
3501 return(old);
3502}
Daniel Veillard652327a2003-09-29 18:02:38 +00003503#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00003504
3505/************************************************************************
3506 * *
3507 * Copy operations *
3508 * *
3509 ************************************************************************/
3510
3511/**
3512 * xmlCopyNamespace:
3513 * @cur: the namespace
3514 *
3515 * Do a copy of the namespace.
3516 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003517 * Returns: a new #xmlNsPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003518 */
3519xmlNsPtr
3520xmlCopyNamespace(xmlNsPtr cur) {
3521 xmlNsPtr ret;
3522
3523 if (cur == NULL) return(NULL);
3524 switch (cur->type) {
3525 case XML_LOCAL_NAMESPACE:
3526 ret = xmlNewNs(NULL, cur->href, cur->prefix);
3527 break;
3528 default:
3529#ifdef DEBUG_TREE
3530 xmlGenericError(xmlGenericErrorContext,
3531 "xmlCopyNamespace: invalid type %d\n", cur->type);
3532#endif
3533 return(NULL);
3534 }
3535 return(ret);
3536}
3537
3538/**
3539 * xmlCopyNamespaceList:
3540 * @cur: the first namespace
3541 *
3542 * Do a copy of an namespace list.
3543 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003544 * Returns: a new #xmlNsPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003545 */
3546xmlNsPtr
3547xmlCopyNamespaceList(xmlNsPtr cur) {
3548 xmlNsPtr ret = NULL;
3549 xmlNsPtr p = NULL,q;
3550
3551 while (cur != NULL) {
3552 q = xmlCopyNamespace(cur);
3553 if (p == NULL) {
3554 ret = p = q;
3555 } else {
3556 p->next = q;
3557 p = q;
3558 }
3559 cur = cur->next;
3560 }
3561 return(ret);
3562}
3563
3564static xmlNodePtr
3565xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent);
3566/**
3567 * xmlCopyProp:
3568 * @target: the element where the attribute will be grafted
3569 * @cur: the attribute
3570 *
3571 * Do a copy of the attribute.
3572 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003573 * Returns: a new #xmlAttrPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003574 */
3575xmlAttrPtr
3576xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) {
3577 xmlAttrPtr ret;
3578
3579 if (cur == NULL) return(NULL);
3580 if (target != NULL)
3581 ret = xmlNewDocProp(target->doc, cur->name, NULL);
3582 else if (cur->parent != NULL)
3583 ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL);
3584 else if (cur->children != NULL)
3585 ret = xmlNewDocProp(cur->children->doc, cur->name, NULL);
3586 else
3587 ret = xmlNewDocProp(NULL, cur->name, NULL);
3588 if (ret == NULL) return(NULL);
3589 ret->parent = target;
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00003590
Owen Taylor3473f882001-02-23 17:55:21 +00003591 if ((cur->ns != NULL) && (target != NULL)) {
Daniel Veillard8107a222002-01-13 14:10:10 +00003592 xmlNsPtr ns;
Daniel Veillard652327a2003-09-29 18:02:38 +00003593
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00003594 ns = xmlSearchNs(target->doc, target, cur->ns->prefix);
3595 if (ns == NULL) {
3596 /*
3597 * Humm, we are copying an element whose namespace is defined
3598 * out of the new tree scope. Search it in the original tree
3599 * and add it at the top of the new tree
3600 */
3601 ns = xmlSearchNs(cur->doc, cur->parent, cur->ns->prefix);
3602 if (ns != NULL) {
3603 xmlNodePtr root = target;
3604 xmlNodePtr pred = NULL;
3605
3606 while (root->parent != NULL) {
3607 pred = root;
3608 root = root->parent;
3609 }
3610 if (root == (xmlNodePtr) target->doc) {
3611 /* correct possibly cycling above the document elt */
3612 root = pred;
3613 }
3614 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
3615 }
3616 } else {
3617 /*
3618 * we have to find something appropriate here since
3619 * we cant be sure, that the namespce we found is identified
3620 * by the prefix
3621 */
Daniel Veillard044fc6b2002-03-04 17:09:44 +00003622 if (xmlStrEqual(ns->href, cur->ns->href)) {
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00003623 /* this is the nice case */
3624 ret->ns = ns;
3625 } else {
3626 /*
3627 * we are in trouble: we need a new reconcilied namespace.
3628 * This is expensive
3629 */
3630 ret->ns = xmlNewReconciliedNs(target->doc, target, cur->ns);
3631 }
3632 }
3633
Owen Taylor3473f882001-02-23 17:55:21 +00003634 } else
3635 ret->ns = NULL;
3636
3637 if (cur->children != NULL) {
3638 xmlNodePtr tmp;
3639
3640 ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret);
3641 ret->last = NULL;
3642 tmp = ret->children;
3643 while (tmp != NULL) {
3644 /* tmp->parent = (xmlNodePtr)ret; */
3645 if (tmp->next == NULL)
3646 ret->last = tmp;
3647 tmp = tmp->next;
3648 }
3649 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00003650 /*
3651 * Try to handle IDs
3652 */
Daniel Veillarda3db2e32002-03-08 15:46:57 +00003653 if ((target!= NULL) && (cur!= NULL) &&
3654 (target->doc != NULL) && (cur->doc != NULL) &&
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00003655 (cur->doc->ids != NULL) && (cur->parent != NULL)) {
3656 if (xmlIsID(cur->doc, cur->parent, cur)) {
3657 xmlChar *id;
3658
3659 id = xmlNodeListGetString(cur->doc, cur->children, 1);
3660 if (id != NULL) {
3661 xmlAddID(NULL, target->doc, id, ret);
3662 xmlFree(id);
3663 }
3664 }
3665 }
Owen Taylor3473f882001-02-23 17:55:21 +00003666 return(ret);
3667}
3668
3669/**
3670 * xmlCopyPropList:
3671 * @target: the element where the attributes will be grafted
3672 * @cur: the first attribute
3673 *
3674 * Do a copy of an attribute list.
3675 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003676 * Returns: a new #xmlAttrPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003677 */
3678xmlAttrPtr
3679xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) {
3680 xmlAttrPtr ret = NULL;
3681 xmlAttrPtr p = NULL,q;
3682
3683 while (cur != NULL) {
3684 q = xmlCopyProp(target, cur);
William M. Brack13dfa872004-09-18 04:52:08 +00003685 if (q == NULL)
3686 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003687 if (p == NULL) {
3688 ret = p = q;
3689 } else {
3690 p->next = q;
3691 q->prev = p;
3692 p = q;
3693 }
3694 cur = cur->next;
3695 }
3696 return(ret);
3697}
3698
3699/*
Daniel Veillardd1640922001-12-17 15:30:10 +00003700 * NOTE about the CopyNode operations !
Owen Taylor3473f882001-02-23 17:55:21 +00003701 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003702 * They are split into external and internal parts for one
Owen Taylor3473f882001-02-23 17:55:21 +00003703 * tricky reason: namespaces. Doing a direct copy of a node
3704 * say RPM:Copyright without changing the namespace pointer to
3705 * something else can produce stale links. One way to do it is
3706 * to keep a reference counter but this doesn't work as soon
3707 * as one move the element or the subtree out of the scope of
3708 * the existing namespace. The actual solution seems to add
3709 * a copy of the namespace at the top of the copied tree if
3710 * not available in the subtree.
3711 * Hence two functions, the public front-end call the inner ones
William M. Brack57e9e912004-03-09 16:19:02 +00003712 * The argument "recursive" normally indicates a recursive copy
3713 * of the node with values 0 (no) and 1 (yes). For XInclude,
3714 * however, we allow a value of 2 to indicate copy properties and
3715 * namespace info, but don't recurse on children.
Owen Taylor3473f882001-02-23 17:55:21 +00003716 */
3717
3718static xmlNodePtr
Daniel Veillard3ec4c612001-08-28 20:39:49 +00003719xmlStaticCopyNode(const xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent,
William M. Brack57e9e912004-03-09 16:19:02 +00003720 int extended) {
Owen Taylor3473f882001-02-23 17:55:21 +00003721 xmlNodePtr ret;
3722
3723 if (node == NULL) return(NULL);
Daniel Veillard39196eb2001-06-19 18:09:42 +00003724 switch (node->type) {
3725 case XML_TEXT_NODE:
3726 case XML_CDATA_SECTION_NODE:
3727 case XML_ELEMENT_NODE:
Daniel Veillardec6725e2002-09-05 11:12:45 +00003728 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00003729 case XML_ENTITY_REF_NODE:
3730 case XML_ENTITY_NODE:
3731 case XML_PI_NODE:
3732 case XML_COMMENT_NODE:
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00003733 case XML_XINCLUDE_START:
3734 case XML_XINCLUDE_END:
3735 break;
3736 case XML_ATTRIBUTE_NODE:
3737 return((xmlNodePtr) xmlCopyProp(parent, (xmlAttrPtr) node));
3738 case XML_NAMESPACE_DECL:
3739 return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node));
3740
Daniel Veillard39196eb2001-06-19 18:09:42 +00003741 case XML_DOCUMENT_NODE:
3742 case XML_HTML_DOCUMENT_NODE:
3743#ifdef LIBXML_DOCB_ENABLED
3744 case XML_DOCB_DOCUMENT_NODE:
3745#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00003746#ifdef LIBXML_TREE_ENABLED
William M. Brack57e9e912004-03-09 16:19:02 +00003747 return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, extended));
Daniel Veillard652327a2003-09-29 18:02:38 +00003748#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard39196eb2001-06-19 18:09:42 +00003749 case XML_DOCUMENT_TYPE_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00003750 case XML_NOTATION_NODE:
3751 case XML_DTD_NODE:
3752 case XML_ELEMENT_DECL:
3753 case XML_ATTRIBUTE_DECL:
3754 case XML_ENTITY_DECL:
3755 return(NULL);
3756 }
Daniel Veillardb33c2012001-04-25 12:59:04 +00003757
Owen Taylor3473f882001-02-23 17:55:21 +00003758 /*
3759 * Allocate a new node and fill the fields.
3760 */
3761 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
3762 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00003763 xmlTreeErrMemory("copying node");
Owen Taylor3473f882001-02-23 17:55:21 +00003764 return(NULL);
3765 }
3766 memset(ret, 0, sizeof(xmlNode));
3767 ret->type = node->type;
3768
3769 ret->doc = doc;
3770 ret->parent = parent;
3771 if (node->name == xmlStringText)
3772 ret->name = xmlStringText;
3773 else if (node->name == xmlStringTextNoenc)
3774 ret->name = xmlStringTextNoenc;
3775 else if (node->name == xmlStringComment)
3776 ret->name = xmlStringComment;
Daniel Veillard03a53c32004-10-26 16:06:51 +00003777 else if (node->name != NULL) {
3778 if ((doc != NULL) && (doc->dict != NULL))
3779 ret->name = xmlDictLookup(doc->dict, node->name, -1);
3780 else
3781 ret->name = xmlStrdup(node->name);
3782 }
Daniel Veillard7db37732001-07-12 01:20:08 +00003783 if ((node->type != XML_ELEMENT_NODE) &&
3784 (node->content != NULL) &&
3785 (node->type != XML_ENTITY_REF_NODE) &&
3786 (node->type != XML_XINCLUDE_END) &&
3787 (node->type != XML_XINCLUDE_START)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003788 ret->content = xmlStrdup(node->content);
Daniel Veillard8107a222002-01-13 14:10:10 +00003789 }else{
3790 if (node->type == XML_ELEMENT_NODE)
Daniel Veillard3e35f8e2003-10-21 00:05:38 +00003791 ret->line = node->line;
Owen Taylor3473f882001-02-23 17:55:21 +00003792 }
Daniel Veillardacb2bda2002-01-13 16:15:43 +00003793 if (parent != NULL) {
3794 xmlNodePtr tmp;
3795
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003796 /*
3797 * this is a tricky part for the node register thing:
3798 * in case ret does get coalesced in xmlAddChild
3799 * the deregister-node callback is called; so we register ret now already
3800 */
Daniel Veillarda880b122003-04-21 21:36:41 +00003801 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003802 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
3803
Daniel Veillardacb2bda2002-01-13 16:15:43 +00003804 tmp = xmlAddChild(parent, ret);
3805 /* node could have coalesced */
3806 if (tmp != ret)
3807 return(tmp);
3808 }
Owen Taylor3473f882001-02-23 17:55:21 +00003809
William M. Brack57e9e912004-03-09 16:19:02 +00003810 if (!extended)
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003811 goto out;
Owen Taylor3473f882001-02-23 17:55:21 +00003812 if (node->nsDef != NULL)
3813 ret->nsDef = xmlCopyNamespaceList(node->nsDef);
3814
3815 if (node->ns != NULL) {
3816 xmlNsPtr ns;
3817
3818 ns = xmlSearchNs(doc, ret, node->ns->prefix);
3819 if (ns == NULL) {
3820 /*
3821 * Humm, we are copying an element whose namespace is defined
3822 * out of the new tree scope. Search it in the original tree
3823 * and add it at the top of the new tree
3824 */
3825 ns = xmlSearchNs(node->doc, node, node->ns->prefix);
3826 if (ns != NULL) {
3827 xmlNodePtr root = ret;
3828
3829 while (root->parent != NULL) root = root->parent;
Daniel Veillarde82a9922001-04-22 12:12:58 +00003830 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00003831 }
3832 } else {
3833 /*
3834 * reference the existing namespace definition in our own tree.
3835 */
3836 ret->ns = ns;
3837 }
3838 }
3839 if (node->properties != NULL)
3840 ret->properties = xmlCopyPropList(ret, node->properties);
Daniel Veillardb33c2012001-04-25 12:59:04 +00003841 if (node->type == XML_ENTITY_REF_NODE) {
3842 if ((doc == NULL) || (node->doc != doc)) {
3843 /*
3844 * The copied node will go into a separate document, so
Daniel Veillardd1640922001-12-17 15:30:10 +00003845 * to avoid dangling references to the ENTITY_DECL node
Daniel Veillardb33c2012001-04-25 12:59:04 +00003846 * we cannot keep the reference. Try to find it in the
3847 * target document.
3848 */
3849 ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name);
3850 } else {
3851 ret->children = node->children;
3852 }
Daniel Veillard0ec98632001-11-14 15:04:32 +00003853 ret->last = ret->children;
William M. Brack57e9e912004-03-09 16:19:02 +00003854 } else if ((node->children != NULL) && (extended != 2)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003855 ret->children = xmlStaticCopyNodeList(node->children, doc, ret);
Daniel Veillard0ec98632001-11-14 15:04:32 +00003856 UPDATE_LAST_CHILD_AND_PARENT(ret)
3857 }
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003858
3859out:
3860 /* if parent != NULL we already registered the node above */
Daniel Veillardac996a12004-07-30 12:02:58 +00003861 if ((parent == NULL) &&
3862 ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003863 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
Owen Taylor3473f882001-02-23 17:55:21 +00003864 return(ret);
3865}
3866
3867static xmlNodePtr
3868xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) {
3869 xmlNodePtr ret = NULL;
3870 xmlNodePtr p = NULL,q;
3871
3872 while (node != NULL) {
Daniel Veillard652327a2003-09-29 18:02:38 +00003873#ifdef LIBXML_TREE_ENABLED
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00003874 if (node->type == XML_DTD_NODE ) {
Daniel Veillard4497e692001-06-09 14:19:02 +00003875 if (doc == NULL) {
3876 node = node->next;
3877 continue;
3878 }
Daniel Veillardb33c2012001-04-25 12:59:04 +00003879 if (doc->intSubset == NULL) {
3880 q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node );
3881 q->doc = doc;
3882 q->parent = parent;
3883 doc->intSubset = (xmlDtdPtr) q;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00003884 xmlAddChild(parent, q);
Daniel Veillardb33c2012001-04-25 12:59:04 +00003885 } else {
3886 q = (xmlNodePtr) doc->intSubset;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00003887 xmlAddChild(parent, q);
Daniel Veillardb33c2012001-04-25 12:59:04 +00003888 }
3889 } else
Daniel Veillard652327a2003-09-29 18:02:38 +00003890#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardb33c2012001-04-25 12:59:04 +00003891 q = xmlStaticCopyNode(node, doc, parent, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00003892 if (ret == NULL) {
3893 q->prev = NULL;
3894 ret = p = q;
Daniel Veillardacb2bda2002-01-13 16:15:43 +00003895 } else if (p != q) {
3896 /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */
Owen Taylor3473f882001-02-23 17:55:21 +00003897 p->next = q;
3898 q->prev = p;
3899 p = q;
3900 }
3901 node = node->next;
3902 }
3903 return(ret);
3904}
3905
3906/**
3907 * xmlCopyNode:
3908 * @node: the node
William M. Brack57e9e912004-03-09 16:19:02 +00003909 * @extended: if 1 do a recursive copy (properties, namespaces and children
3910 * when applicable)
3911 * if 2 copy properties and namespaces (when applicable)
Owen Taylor3473f882001-02-23 17:55:21 +00003912 *
3913 * Do a copy of the node.
3914 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003915 * Returns: a new #xmlNodePtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003916 */
3917xmlNodePtr
William M. Brack57e9e912004-03-09 16:19:02 +00003918xmlCopyNode(const xmlNodePtr node, int extended) {
Owen Taylor3473f882001-02-23 17:55:21 +00003919 xmlNodePtr ret;
3920
William M. Brack57e9e912004-03-09 16:19:02 +00003921 ret = xmlStaticCopyNode(node, NULL, NULL, extended);
Owen Taylor3473f882001-02-23 17:55:21 +00003922 return(ret);
3923}
3924
3925/**
Daniel Veillard82daa812001-04-12 08:55:36 +00003926 * xmlDocCopyNode:
3927 * @node: the node
Daniel Veillardd1640922001-12-17 15:30:10 +00003928 * @doc: the document
William M. Brack57e9e912004-03-09 16:19:02 +00003929 * @extended: if 1 do a recursive copy (properties, namespaces and children
3930 * when applicable)
3931 * if 2 copy properties and namespaces (when applicable)
Daniel Veillard82daa812001-04-12 08:55:36 +00003932 *
3933 * Do a copy of the node to a given document.
3934 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003935 * Returns: a new #xmlNodePtr, or NULL in case of error.
Daniel Veillard82daa812001-04-12 08:55:36 +00003936 */
3937xmlNodePtr
William M. Brack57e9e912004-03-09 16:19:02 +00003938xmlDocCopyNode(const xmlNodePtr node, xmlDocPtr doc, int extended) {
Daniel Veillard82daa812001-04-12 08:55:36 +00003939 xmlNodePtr ret;
3940
William M. Brack57e9e912004-03-09 16:19:02 +00003941 ret = xmlStaticCopyNode(node, doc, NULL, extended);
Daniel Veillard82daa812001-04-12 08:55:36 +00003942 return(ret);
3943}
3944
3945/**
Daniel Veillard03a53c32004-10-26 16:06:51 +00003946 * xmlDocCopyNodeList:
3947 * @doc: the target document
3948 * @node: the first node in the list.
3949 *
3950 * Do a recursive copy of the node list.
3951 *
3952 * Returns: a new #xmlNodePtr, or NULL in case of error.
3953 */
3954xmlNodePtr xmlDocCopyNodeList(xmlDocPtr doc, const xmlNodePtr node) {
3955 xmlNodePtr ret = xmlStaticCopyNodeList(node, doc, NULL);
3956 return(ret);
3957}
3958
3959/**
Owen Taylor3473f882001-02-23 17:55:21 +00003960 * xmlCopyNodeList:
3961 * @node: the first node in the list.
3962 *
3963 * Do a recursive copy of the node list.
Daniel Veillard03a53c32004-10-26 16:06:51 +00003964 * Use xmlDocCopyNodeList() if possible to ensure string interning.
Owen Taylor3473f882001-02-23 17:55:21 +00003965 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003966 * Returns: a new #xmlNodePtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003967 */
Daniel Veillard3ec4c612001-08-28 20:39:49 +00003968xmlNodePtr xmlCopyNodeList(const xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00003969 xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL);
3970 return(ret);
3971}
3972
Daniel Veillard2156d432004-03-04 15:59:36 +00003973#if defined(LIBXML_TREE_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00003974/**
Owen Taylor3473f882001-02-23 17:55:21 +00003975 * xmlCopyDtd:
3976 * @dtd: the dtd
3977 *
3978 * Do a copy of the dtd.
3979 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003980 * Returns: a new #xmlDtdPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003981 */
3982xmlDtdPtr
3983xmlCopyDtd(xmlDtdPtr dtd) {
3984 xmlDtdPtr ret;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00003985 xmlNodePtr cur, p = NULL, q;
Owen Taylor3473f882001-02-23 17:55:21 +00003986
3987 if (dtd == NULL) return(NULL);
3988 ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID);
3989 if (ret == NULL) return(NULL);
3990 if (dtd->entities != NULL)
3991 ret->entities = (void *) xmlCopyEntitiesTable(
3992 (xmlEntitiesTablePtr) dtd->entities);
3993 if (dtd->notations != NULL)
3994 ret->notations = (void *) xmlCopyNotationTable(
3995 (xmlNotationTablePtr) dtd->notations);
3996 if (dtd->elements != NULL)
3997 ret->elements = (void *) xmlCopyElementTable(
3998 (xmlElementTablePtr) dtd->elements);
3999 if (dtd->attributes != NULL)
4000 ret->attributes = (void *) xmlCopyAttributeTable(
4001 (xmlAttributeTablePtr) dtd->attributes);
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004002 if (dtd->pentities != NULL)
4003 ret->pentities = (void *) xmlCopyEntitiesTable(
4004 (xmlEntitiesTablePtr) dtd->pentities);
4005
4006 cur = dtd->children;
4007 while (cur != NULL) {
4008 q = NULL;
4009
4010 if (cur->type == XML_ENTITY_DECL) {
4011 xmlEntityPtr tmp = (xmlEntityPtr) cur;
4012 switch (tmp->etype) {
4013 case XML_INTERNAL_GENERAL_ENTITY:
4014 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
4015 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
4016 q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name);
4017 break;
4018 case XML_INTERNAL_PARAMETER_ENTITY:
4019 case XML_EXTERNAL_PARAMETER_ENTITY:
4020 q = (xmlNodePtr)
4021 xmlGetParameterEntityFromDtd(ret, tmp->name);
4022 break;
4023 case XML_INTERNAL_PREDEFINED_ENTITY:
4024 break;
4025 }
4026 } else if (cur->type == XML_ELEMENT_DECL) {
4027 xmlElementPtr tmp = (xmlElementPtr) cur;
4028 q = (xmlNodePtr)
4029 xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix);
4030 } else if (cur->type == XML_ATTRIBUTE_DECL) {
4031 xmlAttributePtr tmp = (xmlAttributePtr) cur;
4032 q = (xmlNodePtr)
4033 xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix);
4034 } else if (cur->type == XML_COMMENT_NODE) {
4035 q = xmlCopyNode(cur, 0);
4036 }
4037
4038 if (q == NULL) {
4039 cur = cur->next;
4040 continue;
4041 }
4042
4043 if (p == NULL)
4044 ret->children = q;
4045 else
4046 p->next = q;
4047
4048 q->prev = p;
4049 q->parent = (xmlNodePtr) ret;
4050 q->next = NULL;
4051 ret->last = q;
4052 p = q;
4053 cur = cur->next;
4054 }
4055
Owen Taylor3473f882001-02-23 17:55:21 +00004056 return(ret);
4057}
Daniel Veillard2156d432004-03-04 15:59:36 +00004058#endif
Owen Taylor3473f882001-02-23 17:55:21 +00004059
Daniel Veillard2156d432004-03-04 15:59:36 +00004060#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004061/**
4062 * xmlCopyDoc:
4063 * @doc: the document
William M. Brack57e9e912004-03-09 16:19:02 +00004064 * @recursive: if not zero do a recursive copy.
Owen Taylor3473f882001-02-23 17:55:21 +00004065 *
4066 * Do a copy of the document info. If recursive, the content tree will
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004067 * be copied too as well as DTD, namespaces and entities.
Owen Taylor3473f882001-02-23 17:55:21 +00004068 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004069 * Returns: a new #xmlDocPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004070 */
4071xmlDocPtr
4072xmlCopyDoc(xmlDocPtr doc, int recursive) {
4073 xmlDocPtr ret;
4074
4075 if (doc == NULL) return(NULL);
4076 ret = xmlNewDoc(doc->version);
4077 if (ret == NULL) return(NULL);
4078 if (doc->name != NULL)
4079 ret->name = xmlMemStrdup(doc->name);
4080 if (doc->encoding != NULL)
4081 ret->encoding = xmlStrdup(doc->encoding);
Daniel Veillardf59507d2005-01-27 17:26:49 +00004082 if (doc->URL != NULL)
4083 ret->URL = xmlStrdup(doc->URL);
Owen Taylor3473f882001-02-23 17:55:21 +00004084 ret->charset = doc->charset;
4085 ret->compression = doc->compression;
4086 ret->standalone = doc->standalone;
4087 if (!recursive) return(ret);
4088
Daniel Veillardb33c2012001-04-25 12:59:04 +00004089 ret->last = NULL;
4090 ret->children = NULL;
Daniel Veillard2156d432004-03-04 15:59:36 +00004091#ifdef LIBXML_TREE_ENABLED
Daniel Veillardb33c2012001-04-25 12:59:04 +00004092 if (doc->intSubset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004093 ret->intSubset = xmlCopyDtd(doc->intSubset);
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004094 xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret);
Daniel Veillardb33c2012001-04-25 12:59:04 +00004095 ret->intSubset->parent = ret;
4096 }
Daniel Veillard2156d432004-03-04 15:59:36 +00004097#endif
Owen Taylor3473f882001-02-23 17:55:21 +00004098 if (doc->oldNs != NULL)
4099 ret->oldNs = xmlCopyNamespaceList(doc->oldNs);
4100 if (doc->children != NULL) {
4101 xmlNodePtr tmp;
Daniel Veillardb33c2012001-04-25 12:59:04 +00004102
4103 ret->children = xmlStaticCopyNodeList(doc->children, ret,
4104 (xmlNodePtr)ret);
Owen Taylor3473f882001-02-23 17:55:21 +00004105 ret->last = NULL;
4106 tmp = ret->children;
4107 while (tmp != NULL) {
4108 if (tmp->next == NULL)
4109 ret->last = tmp;
4110 tmp = tmp->next;
4111 }
4112 }
4113 return(ret);
4114}
Daniel Veillard652327a2003-09-29 18:02:38 +00004115#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004116
4117/************************************************************************
4118 * *
4119 * Content access functions *
4120 * *
4121 ************************************************************************/
4122
4123/**
Daniel Veillard8faa7832001-11-26 15:58:08 +00004124 * xmlGetLineNo:
Daniel Veillard01c13b52002-12-10 15:19:08 +00004125 * @node: valid node
Daniel Veillard8faa7832001-11-26 15:58:08 +00004126 *
William M. Brackd7cf7f82003-11-14 07:13:16 +00004127 * Get line number of @node. This requires activation of this option
Daniel Veillardd1640922001-12-17 15:30:10 +00004128 * before invoking the parser by calling xmlLineNumbersDefault(1)
Daniel Veillard8faa7832001-11-26 15:58:08 +00004129 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004130 * Returns the line number if successful, -1 otherwise
Daniel Veillard8faa7832001-11-26 15:58:08 +00004131 */
4132long
4133xmlGetLineNo(xmlNodePtr node)
4134{
4135 long result = -1;
4136
4137 if (!node)
4138 return result;
4139 if (node->type == XML_ELEMENT_NODE)
Daniel Veillard3e35f8e2003-10-21 00:05:38 +00004140 result = (long) node->line;
Daniel Veillard8faa7832001-11-26 15:58:08 +00004141 else if ((node->prev != NULL) &&
4142 ((node->prev->type == XML_ELEMENT_NODE) ||
4143 (node->prev->type == XML_TEXT_NODE)))
4144 result = xmlGetLineNo(node->prev);
4145 else if ((node->parent != NULL) &&
4146 ((node->parent->type == XML_ELEMENT_NODE) ||
4147 (node->parent->type == XML_TEXT_NODE)))
4148 result = xmlGetLineNo(node->parent);
4149
4150 return result;
4151}
4152
Daniel Veillard2156d432004-03-04 15:59:36 +00004153#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED)
Daniel Veillard8faa7832001-11-26 15:58:08 +00004154/**
4155 * xmlGetNodePath:
4156 * @node: a node
4157 *
4158 * Build a structure based Path for the given node
4159 *
4160 * Returns the new path or NULL in case of error. The caller must free
4161 * the returned string
4162 */
4163xmlChar *
4164xmlGetNodePath(xmlNodePtr node)
4165{
4166 xmlNodePtr cur, tmp, next;
4167 xmlChar *buffer = NULL, *temp;
4168 size_t buf_len;
4169 xmlChar *buf;
Daniel Veillard96c3a3b2002-10-14 15:39:04 +00004170 const char *sep;
Daniel Veillard8faa7832001-11-26 15:58:08 +00004171 const char *name;
4172 char nametemp[100];
4173 int occur = 0;
4174
4175 if (node == NULL)
4176 return (NULL);
4177
4178 buf_len = 500;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004179 buffer = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004180 if (buffer == NULL) {
4181 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004182 return (NULL);
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004183 }
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004184 buf = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
Daniel Veillard8faa7832001-11-26 15:58:08 +00004185 if (buf == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004186 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004187 xmlFree(buffer);
4188 return (NULL);
4189 }
4190
4191 buffer[0] = 0;
4192 cur = node;
4193 do {
4194 name = "";
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004195 sep = "?";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004196 occur = 0;
4197 if ((cur->type == XML_DOCUMENT_NODE) ||
4198 (cur->type == XML_HTML_DOCUMENT_NODE)) {
4199 if (buffer[0] == '/')
4200 break;
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004201 sep = "/";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004202 next = NULL;
4203 } else if (cur->type == XML_ELEMENT_NODE) {
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004204 sep = "/";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004205 name = (const char *) cur->name;
4206 if (cur->ns) {
William M. Brack84d83e32003-12-23 03:45:17 +00004207 if (cur->ns->prefix != NULL)
William M. Brack13dfa872004-09-18 04:52:08 +00004208 snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
4209 (char *)cur->ns->prefix, (char *)cur->name);
William M. Brack84d83e32003-12-23 03:45:17 +00004210 else
William M. Brack13dfa872004-09-18 04:52:08 +00004211 snprintf(nametemp, sizeof(nametemp) - 1, "%s",
4212 (char *)cur->name);
Daniel Veillard8faa7832001-11-26 15:58:08 +00004213 nametemp[sizeof(nametemp) - 1] = 0;
4214 name = nametemp;
4215 }
4216 next = cur->parent;
4217
4218 /*
4219 * Thumbler index computation
Daniel Veillardc00cda82003-04-07 10:22:39 +00004220 * TODO: the ocurence test seems bogus for namespaced names
Daniel Veillard8faa7832001-11-26 15:58:08 +00004221 */
4222 tmp = cur->prev;
4223 while (tmp != NULL) {
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00004224 if ((tmp->type == XML_ELEMENT_NODE) &&
Daniel Veillard0996a162005-02-05 14:00:10 +00004225 (xmlStrEqual(cur->name, tmp->name)) &&
4226 ((tmp->ns == cur->ns) ||
4227 ((tmp->ns != NULL) && (cur->ns != NULL) &&
4228 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004229 occur++;
4230 tmp = tmp->prev;
4231 }
4232 if (occur == 0) {
4233 tmp = cur->next;
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004234 while (tmp != NULL && occur == 0) {
4235 if ((tmp->type == XML_ELEMENT_NODE) &&
Daniel Veillard0996a162005-02-05 14:00:10 +00004236 (xmlStrEqual(cur->name, tmp->name)) &&
4237 ((tmp->ns == cur->ns) ||
4238 ((tmp->ns != NULL) && (cur->ns != NULL) &&
4239 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004240 occur++;
4241 tmp = tmp->next;
4242 }
4243 if (occur != 0)
4244 occur = 1;
4245 } else
4246 occur++;
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004247 } else if (cur->type == XML_COMMENT_NODE) {
4248 sep = "/";
4249 name = "comment()";
4250 next = cur->parent;
4251
4252 /*
4253 * Thumbler index computation
4254 */
4255 tmp = cur->prev;
4256 while (tmp != NULL) {
4257 if (tmp->type == XML_COMMENT_NODE)
4258 occur++;
4259 tmp = tmp->prev;
4260 }
4261 if (occur == 0) {
4262 tmp = cur->next;
4263 while (tmp != NULL && occur == 0) {
4264 if (tmp->type == XML_COMMENT_NODE)
4265 occur++;
4266 tmp = tmp->next;
4267 }
4268 if (occur != 0)
4269 occur = 1;
4270 } else
4271 occur++;
4272 } else if ((cur->type == XML_TEXT_NODE) ||
4273 (cur->type == XML_CDATA_SECTION_NODE)) {
4274 sep = "/";
4275 name = "text()";
4276 next = cur->parent;
4277
4278 /*
4279 * Thumbler index computation
4280 */
4281 tmp = cur->prev;
4282 while (tmp != NULL) {
4283 if ((cur->type == XML_TEXT_NODE) ||
4284 (cur->type == XML_CDATA_SECTION_NODE))
4285 occur++;
4286 tmp = tmp->prev;
4287 }
4288 if (occur == 0) {
4289 tmp = cur->next;
4290 while (tmp != NULL && occur == 0) {
Daniel Veillard1f8658a2004-08-14 21:46:31 +00004291 if ((tmp->type == XML_TEXT_NODE) ||
4292 (tmp->type == XML_CDATA_SECTION_NODE))
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004293 occur++;
4294 tmp = tmp->next;
4295 }
4296 if (occur != 0)
4297 occur = 1;
4298 } else
4299 occur++;
4300 } else if (cur->type == XML_PI_NODE) {
4301 sep = "/";
4302 snprintf(nametemp, sizeof(nametemp) - 1,
William M. Brack13dfa872004-09-18 04:52:08 +00004303 "processing-instruction('%s')", (char *)cur->name);
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004304 nametemp[sizeof(nametemp) - 1] = 0;
4305 name = nametemp;
4306
4307 next = cur->parent;
4308
4309 /*
4310 * Thumbler index computation
4311 */
4312 tmp = cur->prev;
4313 while (tmp != NULL) {
4314 if ((tmp->type == XML_PI_NODE) &&
4315 (xmlStrEqual(cur->name, tmp->name)))
4316 occur++;
4317 tmp = tmp->prev;
4318 }
4319 if (occur == 0) {
4320 tmp = cur->next;
4321 while (tmp != NULL && occur == 0) {
4322 if ((tmp->type == XML_PI_NODE) &&
4323 (xmlStrEqual(cur->name, tmp->name)))
4324 occur++;
4325 tmp = tmp->next;
4326 }
4327 if (occur != 0)
4328 occur = 1;
4329 } else
4330 occur++;
4331
Daniel Veillard8faa7832001-11-26 15:58:08 +00004332 } else if (cur->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004333 sep = "/@";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004334 name = (const char *) (((xmlAttrPtr) cur)->name);
4335 next = ((xmlAttrPtr) cur)->parent;
4336 } else {
4337 next = cur->parent;
4338 }
4339
4340 /*
4341 * Make sure there is enough room
4342 */
4343 if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) {
4344 buf_len =
4345 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20;
4346 temp = (xmlChar *) xmlRealloc(buffer, buf_len);
4347 if (temp == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004348 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004349 xmlFree(buf);
4350 xmlFree(buffer);
4351 return (NULL);
4352 }
4353 buffer = temp;
4354 temp = (xmlChar *) xmlRealloc(buf, buf_len);
4355 if (temp == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004356 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004357 xmlFree(buf);
4358 xmlFree(buffer);
4359 return (NULL);
4360 }
4361 buf = temp;
4362 }
4363 if (occur == 0)
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004364 snprintf((char *) buf, buf_len, "%s%s%s",
Daniel Veillard8faa7832001-11-26 15:58:08 +00004365 sep, name, (char *) buffer);
4366 else
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004367 snprintf((char *) buf, buf_len, "%s%s[%d]%s",
Daniel Veillard8faa7832001-11-26 15:58:08 +00004368 sep, name, occur, (char *) buffer);
William M. Brack13dfa872004-09-18 04:52:08 +00004369 snprintf((char *) buffer, buf_len, "%s", (char *)buf);
Daniel Veillard8faa7832001-11-26 15:58:08 +00004370 cur = next;
4371 } while (cur != NULL);
4372 xmlFree(buf);
4373 return (buffer);
4374}
Daniel Veillard652327a2003-09-29 18:02:38 +00004375#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard8faa7832001-11-26 15:58:08 +00004376
4377/**
Owen Taylor3473f882001-02-23 17:55:21 +00004378 * xmlDocGetRootElement:
4379 * @doc: the document
4380 *
4381 * Get the root element of the document (doc->children is a list
4382 * containing possibly comments, PIs, etc ...).
4383 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004384 * Returns the #xmlNodePtr for the root or NULL
Owen Taylor3473f882001-02-23 17:55:21 +00004385 */
4386xmlNodePtr
4387xmlDocGetRootElement(xmlDocPtr doc) {
4388 xmlNodePtr ret;
4389
4390 if (doc == NULL) return(NULL);
4391 ret = doc->children;
4392 while (ret != NULL) {
4393 if (ret->type == XML_ELEMENT_NODE)
4394 return(ret);
4395 ret = ret->next;
4396 }
4397 return(ret);
4398}
4399
Daniel Veillard2156d432004-03-04 15:59:36 +00004400#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004401/**
4402 * xmlDocSetRootElement:
4403 * @doc: the document
4404 * @root: the new document root element
4405 *
4406 * Set the root element of the document (doc->children is a list
4407 * containing possibly comments, PIs, etc ...).
4408 *
4409 * Returns the old root element if any was found
4410 */
4411xmlNodePtr
4412xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) {
4413 xmlNodePtr old = NULL;
4414
4415 if (doc == NULL) return(NULL);
Daniel Veillardc575b992002-02-08 13:28:40 +00004416 if (root == NULL)
4417 return(NULL);
4418 xmlUnlinkNode(root);
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00004419 xmlSetTreeDoc(root, doc);
Daniel Veillardc575b992002-02-08 13:28:40 +00004420 root->parent = (xmlNodePtr) doc;
Owen Taylor3473f882001-02-23 17:55:21 +00004421 old = doc->children;
4422 while (old != NULL) {
4423 if (old->type == XML_ELEMENT_NODE)
4424 break;
4425 old = old->next;
4426 }
4427 if (old == NULL) {
4428 if (doc->children == NULL) {
4429 doc->children = root;
4430 doc->last = root;
4431 } else {
4432 xmlAddSibling(doc->children, root);
4433 }
4434 } else {
4435 xmlReplaceNode(old, root);
4436 }
4437 return(old);
4438}
Daniel Veillard2156d432004-03-04 15:59:36 +00004439#endif
Owen Taylor3473f882001-02-23 17:55:21 +00004440
Daniel Veillard2156d432004-03-04 15:59:36 +00004441#if defined(LIBXML_TREE_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004442/**
4443 * xmlNodeSetLang:
4444 * @cur: the node being changed
Daniel Veillardd1640922001-12-17 15:30:10 +00004445 * @lang: the language description
Owen Taylor3473f882001-02-23 17:55:21 +00004446 *
4447 * Set the language of a node, i.e. the values of the xml:lang
4448 * attribute.
4449 */
4450void
4451xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004452 xmlNsPtr ns;
4453
Owen Taylor3473f882001-02-23 17:55:21 +00004454 if (cur == NULL) return;
4455 switch(cur->type) {
4456 case XML_TEXT_NODE:
4457 case XML_CDATA_SECTION_NODE:
4458 case XML_COMMENT_NODE:
4459 case XML_DOCUMENT_NODE:
4460 case XML_DOCUMENT_TYPE_NODE:
4461 case XML_DOCUMENT_FRAG_NODE:
4462 case XML_NOTATION_NODE:
4463 case XML_HTML_DOCUMENT_NODE:
4464 case XML_DTD_NODE:
4465 case XML_ELEMENT_DECL:
4466 case XML_ATTRIBUTE_DECL:
4467 case XML_ENTITY_DECL:
4468 case XML_PI_NODE:
4469 case XML_ENTITY_REF_NODE:
4470 case XML_ENTITY_NODE:
4471 case XML_NAMESPACE_DECL:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004472#ifdef LIBXML_DOCB_ENABLED
4473 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004474#endif
4475 case XML_XINCLUDE_START:
4476 case XML_XINCLUDE_END:
4477 return;
4478 case XML_ELEMENT_NODE:
4479 case XML_ATTRIBUTE_NODE:
4480 break;
4481 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004482 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4483 if (ns == NULL)
4484 return;
4485 xmlSetNsProp(cur, ns, BAD_CAST "lang", lang);
Owen Taylor3473f882001-02-23 17:55:21 +00004486}
Daniel Veillard652327a2003-09-29 18:02:38 +00004487#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004488
4489/**
4490 * xmlNodeGetLang:
4491 * @cur: the node being checked
4492 *
4493 * Searches the language of a node, i.e. the values of the xml:lang
4494 * attribute or the one carried by the nearest ancestor.
4495 *
4496 * Returns a pointer to the lang value, or NULL if not found
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004497 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00004498 */
4499xmlChar *
4500xmlNodeGetLang(xmlNodePtr cur) {
4501 xmlChar *lang;
4502
4503 while (cur != NULL) {
Daniel Veillardc17337c2001-05-09 10:51:31 +00004504 lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE);
Owen Taylor3473f882001-02-23 17:55:21 +00004505 if (lang != NULL)
4506 return(lang);
4507 cur = cur->parent;
4508 }
4509 return(NULL);
4510}
4511
4512
Daniel Veillard652327a2003-09-29 18:02:38 +00004513#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00004514/**
4515 * xmlNodeSetSpacePreserve:
4516 * @cur: the node being changed
4517 * @val: the xml:space value ("0": default, 1: "preserve")
4518 *
4519 * Set (or reset) the space preserving behaviour of a node, i.e. the
4520 * value of the xml:space attribute.
4521 */
4522void
4523xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004524 xmlNsPtr ns;
4525
Owen Taylor3473f882001-02-23 17:55:21 +00004526 if (cur == NULL) return;
4527 switch(cur->type) {
4528 case XML_TEXT_NODE:
4529 case XML_CDATA_SECTION_NODE:
4530 case XML_COMMENT_NODE:
4531 case XML_DOCUMENT_NODE:
4532 case XML_DOCUMENT_TYPE_NODE:
4533 case XML_DOCUMENT_FRAG_NODE:
4534 case XML_NOTATION_NODE:
4535 case XML_HTML_DOCUMENT_NODE:
4536 case XML_DTD_NODE:
4537 case XML_ELEMENT_DECL:
4538 case XML_ATTRIBUTE_DECL:
4539 case XML_ENTITY_DECL:
4540 case XML_PI_NODE:
4541 case XML_ENTITY_REF_NODE:
4542 case XML_ENTITY_NODE:
4543 case XML_NAMESPACE_DECL:
4544 case XML_XINCLUDE_START:
4545 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004546#ifdef LIBXML_DOCB_ENABLED
4547 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004548#endif
4549 return;
4550 case XML_ELEMENT_NODE:
4551 case XML_ATTRIBUTE_NODE:
4552 break;
4553 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004554 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4555 if (ns == NULL)
4556 return;
Owen Taylor3473f882001-02-23 17:55:21 +00004557 switch (val) {
4558 case 0:
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004559 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "default");
Owen Taylor3473f882001-02-23 17:55:21 +00004560 break;
4561 case 1:
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004562 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "preserve");
Owen Taylor3473f882001-02-23 17:55:21 +00004563 break;
4564 }
4565}
Daniel Veillard652327a2003-09-29 18:02:38 +00004566#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004567
4568/**
4569 * xmlNodeGetSpacePreserve:
4570 * @cur: the node being checked
4571 *
4572 * Searches the space preserving behaviour of a node, i.e. the values
4573 * of the xml:space attribute or the one carried by the nearest
4574 * ancestor.
4575 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004576 * Returns -1 if xml:space is not inherited, 0 if "default", 1 if "preserve"
Owen Taylor3473f882001-02-23 17:55:21 +00004577 */
4578int
4579xmlNodeGetSpacePreserve(xmlNodePtr cur) {
4580 xmlChar *space;
4581
4582 while (cur != NULL) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004583 space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE);
Owen Taylor3473f882001-02-23 17:55:21 +00004584 if (space != NULL) {
4585 if (xmlStrEqual(space, BAD_CAST "preserve")) {
4586 xmlFree(space);
4587 return(1);
4588 }
4589 if (xmlStrEqual(space, BAD_CAST "default")) {
4590 xmlFree(space);
4591 return(0);
4592 }
4593 xmlFree(space);
4594 }
4595 cur = cur->parent;
4596 }
4597 return(-1);
4598}
4599
Daniel Veillard652327a2003-09-29 18:02:38 +00004600#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00004601/**
4602 * xmlNodeSetName:
4603 * @cur: the node being changed
4604 * @name: the new tag name
4605 *
4606 * Set (or reset) the name of a node.
4607 */
4608void
4609xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) {
Daniel Veillardce244ad2004-11-05 10:03:46 +00004610 xmlDocPtr doc;
4611 xmlDictPtr dict;
4612
Owen Taylor3473f882001-02-23 17:55:21 +00004613 if (cur == NULL) return;
4614 if (name == NULL) return;
4615 switch(cur->type) {
4616 case XML_TEXT_NODE:
4617 case XML_CDATA_SECTION_NODE:
4618 case XML_COMMENT_NODE:
4619 case XML_DOCUMENT_TYPE_NODE:
4620 case XML_DOCUMENT_FRAG_NODE:
4621 case XML_NOTATION_NODE:
4622 case XML_HTML_DOCUMENT_NODE:
4623 case XML_NAMESPACE_DECL:
4624 case XML_XINCLUDE_START:
4625 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004626#ifdef LIBXML_DOCB_ENABLED
4627 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004628#endif
4629 return;
4630 case XML_ELEMENT_NODE:
4631 case XML_ATTRIBUTE_NODE:
4632 case XML_PI_NODE:
4633 case XML_ENTITY_REF_NODE:
4634 case XML_ENTITY_NODE:
4635 case XML_DTD_NODE:
4636 case XML_DOCUMENT_NODE:
4637 case XML_ELEMENT_DECL:
4638 case XML_ATTRIBUTE_DECL:
4639 case XML_ENTITY_DECL:
4640 break;
4641 }
Daniel Veillardce244ad2004-11-05 10:03:46 +00004642 doc = cur->doc;
4643 if (doc != NULL)
4644 dict = doc->dict;
4645 else
4646 dict = NULL;
4647 if (dict != NULL) {
4648 if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name)))
4649 xmlFree((xmlChar *) cur->name);
4650 cur->name = xmlDictLookup(dict, name, -1);
4651 } else {
4652 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
4653 cur->name = xmlStrdup(name);
4654 }
Owen Taylor3473f882001-02-23 17:55:21 +00004655}
Daniel Veillard2156d432004-03-04 15:59:36 +00004656#endif
Owen Taylor3473f882001-02-23 17:55:21 +00004657
Daniel Veillard2156d432004-03-04 15:59:36 +00004658#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004659/**
4660 * xmlNodeSetBase:
4661 * @cur: the node being changed
4662 * @uri: the new base URI
4663 *
4664 * Set (or reset) the base URI of a node, i.e. the value of the
4665 * xml:base attribute.
4666 */
4667void
Daniel Veillardf85ce8e2003-09-22 10:24:45 +00004668xmlNodeSetBase(xmlNodePtr cur, const xmlChar* uri) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004669 xmlNsPtr ns;
4670
Owen Taylor3473f882001-02-23 17:55:21 +00004671 if (cur == NULL) return;
4672 switch(cur->type) {
4673 case XML_TEXT_NODE:
4674 case XML_CDATA_SECTION_NODE:
4675 case XML_COMMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004676 case XML_DOCUMENT_TYPE_NODE:
4677 case XML_DOCUMENT_FRAG_NODE:
4678 case XML_NOTATION_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004679 case XML_DTD_NODE:
4680 case XML_ELEMENT_DECL:
4681 case XML_ATTRIBUTE_DECL:
4682 case XML_ENTITY_DECL:
4683 case XML_PI_NODE:
4684 case XML_ENTITY_REF_NODE:
4685 case XML_ENTITY_NODE:
4686 case XML_NAMESPACE_DECL:
4687 case XML_XINCLUDE_START:
4688 case XML_XINCLUDE_END:
Owen Taylor3473f882001-02-23 17:55:21 +00004689 return;
4690 case XML_ELEMENT_NODE:
4691 case XML_ATTRIBUTE_NODE:
4692 break;
Daniel Veillard4cbe4702002-05-05 06:57:27 +00004693 case XML_DOCUMENT_NODE:
4694#ifdef LIBXML_DOCB_ENABLED
4695 case XML_DOCB_DOCUMENT_NODE:
4696#endif
4697 case XML_HTML_DOCUMENT_NODE: {
4698 xmlDocPtr doc = (xmlDocPtr) cur;
4699
4700 if (doc->URL != NULL)
4701 xmlFree((xmlChar *) doc->URL);
4702 if (uri == NULL)
4703 doc->URL = NULL;
4704 else
4705 doc->URL = xmlStrdup(uri);
4706 return;
4707 }
Owen Taylor3473f882001-02-23 17:55:21 +00004708 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004709
4710 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4711 if (ns == NULL)
4712 return;
4713 xmlSetNsProp(cur, ns, BAD_CAST "base", uri);
Owen Taylor3473f882001-02-23 17:55:21 +00004714}
Daniel Veillard652327a2003-09-29 18:02:38 +00004715#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004716
4717/**
Owen Taylor3473f882001-02-23 17:55:21 +00004718 * xmlNodeGetBase:
4719 * @doc: the document the node pertains to
4720 * @cur: the node being checked
4721 *
4722 * Searches for the BASE URL. The code should work on both XML
4723 * and HTML document even if base mechanisms are completely different.
4724 * It returns the base as defined in RFC 2396 sections
4725 * 5.1.1. Base URI within Document Content
4726 * and
4727 * 5.1.2. Base URI from the Encapsulating Entity
4728 * However it does not return the document base (5.1.3), use
4729 * xmlDocumentGetBase() for this
4730 *
4731 * Returns a pointer to the base URL, or NULL if not found
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004732 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00004733 */
4734xmlChar *
4735xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00004736 xmlChar *oldbase = NULL;
4737 xmlChar *base, *newbase;
Owen Taylor3473f882001-02-23 17:55:21 +00004738
4739 if ((cur == NULL) && (doc == NULL))
4740 return(NULL);
4741 if (doc == NULL) doc = cur->doc;
4742 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
4743 cur = doc->children;
4744 while ((cur != NULL) && (cur->name != NULL)) {
4745 if (cur->type != XML_ELEMENT_NODE) {
4746 cur = cur->next;
4747 continue;
4748 }
4749 if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) {
4750 cur = cur->children;
4751 continue;
4752 }
4753 if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) {
4754 cur = cur->children;
4755 continue;
4756 }
4757 if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) {
4758 return(xmlGetProp(cur, BAD_CAST "href"));
4759 }
4760 cur = cur->next;
4761 }
4762 return(NULL);
4763 }
4764 while (cur != NULL) {
4765 if (cur->type == XML_ENTITY_DECL) {
4766 xmlEntityPtr ent = (xmlEntityPtr) cur;
4767 return(xmlStrdup(ent->URI));
4768 }
Daniel Veillard42596ad2001-05-22 16:57:14 +00004769 if (cur->type == XML_ELEMENT_NODE) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00004770 base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE);
Daniel Veillard42596ad2001-05-22 16:57:14 +00004771 if (base != NULL) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00004772 if (oldbase != NULL) {
4773 newbase = xmlBuildURI(oldbase, base);
4774 if (newbase != NULL) {
4775 xmlFree(oldbase);
4776 xmlFree(base);
4777 oldbase = newbase;
4778 } else {
4779 xmlFree(oldbase);
4780 xmlFree(base);
4781 return(NULL);
4782 }
4783 } else {
4784 oldbase = base;
4785 }
4786 if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) ||
4787 (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) ||
4788 (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4)))
4789 return(oldbase);
Daniel Veillard42596ad2001-05-22 16:57:14 +00004790 }
4791 }
Owen Taylor3473f882001-02-23 17:55:21 +00004792 cur = cur->parent;
4793 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00004794 if ((doc != NULL) && (doc->URL != NULL)) {
4795 if (oldbase == NULL)
4796 return(xmlStrdup(doc->URL));
4797 newbase = xmlBuildURI(oldbase, doc->URL);
4798 xmlFree(oldbase);
4799 return(newbase);
4800 }
4801 return(oldbase);
Owen Taylor3473f882001-02-23 17:55:21 +00004802}
4803
4804/**
Daniel Veillard78697292003-10-19 20:44:43 +00004805 * xmlNodeBufGetContent:
4806 * @buffer: a buffer
4807 * @cur: the node being read
4808 *
4809 * Read the value of a node @cur, this can be either the text carried
4810 * directly by this node if it's a TEXT node or the aggregate string
4811 * of the values carried by this node child's (TEXT and ENTITY_REF).
4812 * Entity references are substituted.
4813 * Fills up the buffer @buffer with this value
4814 *
4815 * Returns 0 in case of success and -1 in case of error.
4816 */
4817int
4818xmlNodeBufGetContent(xmlBufferPtr buffer, xmlNodePtr cur)
4819{
4820 if ((cur == NULL) || (buffer == NULL)) return(-1);
4821 switch (cur->type) {
4822 case XML_CDATA_SECTION_NODE:
4823 case XML_TEXT_NODE:
4824 xmlBufferCat(buffer, cur->content);
4825 break;
4826 case XML_DOCUMENT_FRAG_NODE:
4827 case XML_ELEMENT_NODE:{
4828 xmlNodePtr tmp = cur;
4829
4830 while (tmp != NULL) {
4831 switch (tmp->type) {
4832 case XML_CDATA_SECTION_NODE:
4833 case XML_TEXT_NODE:
4834 if (tmp->content != NULL)
4835 xmlBufferCat(buffer, tmp->content);
4836 break;
4837 case XML_ENTITY_REF_NODE:
4838 xmlNodeBufGetContent(buffer, tmp->children);
4839 break;
4840 default:
4841 break;
4842 }
4843 /*
4844 * Skip to next node
4845 */
4846 if (tmp->children != NULL) {
4847 if (tmp->children->type != XML_ENTITY_DECL) {
4848 tmp = tmp->children;
4849 continue;
4850 }
4851 }
4852 if (tmp == cur)
4853 break;
4854
4855 if (tmp->next != NULL) {
4856 tmp = tmp->next;
4857 continue;
4858 }
4859
4860 do {
4861 tmp = tmp->parent;
4862 if (tmp == NULL)
4863 break;
4864 if (tmp == cur) {
4865 tmp = NULL;
4866 break;
4867 }
4868 if (tmp->next != NULL) {
4869 tmp = tmp->next;
4870 break;
4871 }
4872 } while (tmp != NULL);
4873 }
4874 break;
4875 }
4876 case XML_ATTRIBUTE_NODE:{
4877 xmlAttrPtr attr = (xmlAttrPtr) cur;
4878 xmlNodePtr tmp = attr->children;
4879
4880 while (tmp != NULL) {
4881 if (tmp->type == XML_TEXT_NODE)
4882 xmlBufferCat(buffer, tmp->content);
4883 else
4884 xmlNodeBufGetContent(buffer, tmp);
4885 tmp = tmp->next;
4886 }
4887 break;
4888 }
4889 case XML_COMMENT_NODE:
4890 case XML_PI_NODE:
4891 xmlBufferCat(buffer, cur->content);
4892 break;
4893 case XML_ENTITY_REF_NODE:{
4894 xmlEntityPtr ent;
4895 xmlNodePtr tmp;
4896
4897 /* lookup entity declaration */
4898 ent = xmlGetDocEntity(cur->doc, cur->name);
4899 if (ent == NULL)
4900 return(-1);
4901
4902 /* an entity content can be any "well balanced chunk",
4903 * i.e. the result of the content [43] production:
4904 * http://www.w3.org/TR/REC-xml#NT-content
4905 * -> we iterate through child nodes and recursive call
4906 * xmlNodeGetContent() which handles all possible node types */
4907 tmp = ent->children;
4908 while (tmp) {
4909 xmlNodeBufGetContent(buffer, tmp);
4910 tmp = tmp->next;
4911 }
4912 break;
4913 }
4914 case XML_ENTITY_NODE:
4915 case XML_DOCUMENT_TYPE_NODE:
4916 case XML_NOTATION_NODE:
4917 case XML_DTD_NODE:
4918 case XML_XINCLUDE_START:
4919 case XML_XINCLUDE_END:
4920 break;
4921 case XML_DOCUMENT_NODE:
4922#ifdef LIBXML_DOCB_ENABLED
4923 case XML_DOCB_DOCUMENT_NODE:
4924#endif
4925 case XML_HTML_DOCUMENT_NODE:
4926 cur = cur->children;
4927 while (cur!= NULL) {
4928 if ((cur->type == XML_ELEMENT_NODE) ||
4929 (cur->type == XML_TEXT_NODE) ||
4930 (cur->type == XML_CDATA_SECTION_NODE)) {
4931 xmlNodeBufGetContent(buffer, cur);
4932 }
4933 cur = cur->next;
4934 }
4935 break;
4936 case XML_NAMESPACE_DECL:
4937 xmlBufferCat(buffer, ((xmlNsPtr) cur)->href);
4938 break;
4939 case XML_ELEMENT_DECL:
4940 case XML_ATTRIBUTE_DECL:
4941 case XML_ENTITY_DECL:
4942 break;
4943 }
4944 return(0);
4945}
4946/**
Owen Taylor3473f882001-02-23 17:55:21 +00004947 * xmlNodeGetContent:
4948 * @cur: the node being read
4949 *
4950 * Read the value of a node, this can be either the text carried
4951 * directly by this node if it's a TEXT node or the aggregate string
4952 * of the values carried by this node child's (TEXT and ENTITY_REF).
Daniel Veillardd1640922001-12-17 15:30:10 +00004953 * Entity references are substituted.
4954 * Returns a new #xmlChar * or NULL if no content is available.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004955 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00004956 */
4957xmlChar *
Daniel Veillard7646b182002-04-20 06:41:40 +00004958xmlNodeGetContent(xmlNodePtr cur)
4959{
4960 if (cur == NULL)
4961 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004962 switch (cur->type) {
4963 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00004964 case XML_ELEMENT_NODE:{
Daniel Veillard7646b182002-04-20 06:41:40 +00004965 xmlBufferPtr buffer;
4966 xmlChar *ret;
Owen Taylor3473f882001-02-23 17:55:21 +00004967
Daniel Veillard814a76d2003-01-23 18:24:20 +00004968 buffer = xmlBufferCreateSize(64);
Daniel Veillard7646b182002-04-20 06:41:40 +00004969 if (buffer == NULL)
4970 return (NULL);
Daniel Veillardc4696922003-10-19 21:47:14 +00004971 xmlNodeBufGetContent(buffer, cur);
Daniel Veillard7646b182002-04-20 06:41:40 +00004972 ret = buffer->content;
4973 buffer->content = NULL;
4974 xmlBufferFree(buffer);
4975 return (ret);
4976 }
4977 case XML_ATTRIBUTE_NODE:{
4978 xmlAttrPtr attr = (xmlAttrPtr) cur;
4979
4980 if (attr->parent != NULL)
4981 return (xmlNodeListGetString
4982 (attr->parent->doc, attr->children, 1));
4983 else
4984 return (xmlNodeListGetString(NULL, attr->children, 1));
4985 break;
4986 }
Owen Taylor3473f882001-02-23 17:55:21 +00004987 case XML_COMMENT_NODE:
4988 case XML_PI_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00004989 if (cur->content != NULL)
4990 return (xmlStrdup(cur->content));
4991 return (NULL);
4992 case XML_ENTITY_REF_NODE:{
4993 xmlEntityPtr ent;
Daniel Veillard7646b182002-04-20 06:41:40 +00004994 xmlBufferPtr buffer;
4995 xmlChar *ret;
4996
4997 /* lookup entity declaration */
4998 ent = xmlGetDocEntity(cur->doc, cur->name);
4999 if (ent == NULL)
5000 return (NULL);
5001
5002 buffer = xmlBufferCreate();
5003 if (buffer == NULL)
5004 return (NULL);
5005
Daniel Veillardc4696922003-10-19 21:47:14 +00005006 xmlNodeBufGetContent(buffer, cur);
Daniel Veillard7646b182002-04-20 06:41:40 +00005007
5008 ret = buffer->content;
5009 buffer->content = NULL;
5010 xmlBufferFree(buffer);
5011 return (ret);
5012 }
Owen Taylor3473f882001-02-23 17:55:21 +00005013 case XML_ENTITY_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005014 case XML_DOCUMENT_TYPE_NODE:
5015 case XML_NOTATION_NODE:
5016 case XML_DTD_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005017 case XML_XINCLUDE_START:
5018 case XML_XINCLUDE_END:
Daniel Veillard9adc0462003-03-24 18:39:54 +00005019 return (NULL);
5020 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005021#ifdef LIBXML_DOCB_ENABLED
Daniel Veillard7646b182002-04-20 06:41:40 +00005022 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005023#endif
Daniel Veillard9adc0462003-03-24 18:39:54 +00005024 case XML_HTML_DOCUMENT_NODE: {
Daniel Veillardc4696922003-10-19 21:47:14 +00005025 xmlBufferPtr buffer;
5026 xmlChar *ret;
Daniel Veillard9adc0462003-03-24 18:39:54 +00005027
Daniel Veillardc4696922003-10-19 21:47:14 +00005028 buffer = xmlBufferCreate();
5029 if (buffer == NULL)
5030 return (NULL);
5031
5032 xmlNodeBufGetContent(buffer, (xmlNodePtr) cur);
5033
5034 ret = buffer->content;
5035 buffer->content = NULL;
5036 xmlBufferFree(buffer);
5037 return (ret);
Daniel Veillard9adc0462003-03-24 18:39:54 +00005038 }
Daniel Veillard96c3a3b2002-10-14 15:39:04 +00005039 case XML_NAMESPACE_DECL: {
5040 xmlChar *tmp;
5041
5042 tmp = xmlStrdup(((xmlNsPtr) cur)->href);
5043 return (tmp);
5044 }
Owen Taylor3473f882001-02-23 17:55:21 +00005045 case XML_ELEMENT_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00005046 /* TODO !!! */
5047 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005048 case XML_ATTRIBUTE_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00005049 /* TODO !!! */
5050 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005051 case XML_ENTITY_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00005052 /* TODO !!! */
5053 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005054 case XML_CDATA_SECTION_NODE:
5055 case XML_TEXT_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005056 if (cur->content != NULL)
5057 return (xmlStrdup(cur->content));
5058 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005059 }
Daniel Veillard7646b182002-04-20 06:41:40 +00005060 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005061}
Daniel Veillard652327a2003-09-29 18:02:38 +00005062
Owen Taylor3473f882001-02-23 17:55:21 +00005063/**
5064 * xmlNodeSetContent:
5065 * @cur: the node being modified
5066 * @content: the new value of the content
5067 *
5068 * Replace the content of a node.
5069 */
5070void
5071xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) {
5072 if (cur == NULL) {
5073#ifdef DEBUG_TREE
5074 xmlGenericError(xmlGenericErrorContext,
5075 "xmlNodeSetContent : node == NULL\n");
5076#endif
5077 return;
5078 }
5079 switch (cur->type) {
5080 case XML_DOCUMENT_FRAG_NODE:
5081 case XML_ELEMENT_NODE:
Daniel Veillard2c748c62002-01-16 15:37:50 +00005082 case XML_ATTRIBUTE_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005083 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5084 cur->children = xmlStringGetNodeList(cur->doc, content);
5085 UPDATE_LAST_CHILD_AND_PARENT(cur)
5086 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005087 case XML_TEXT_NODE:
5088 case XML_CDATA_SECTION_NODE:
5089 case XML_ENTITY_REF_NODE:
5090 case XML_ENTITY_NODE:
5091 case XML_PI_NODE:
5092 case XML_COMMENT_NODE:
5093 if (cur->content != NULL) {
William M. Brack7762bb12004-01-04 14:49:01 +00005094 if (!((cur->doc != NULL) && (cur->doc->dict != NULL) &&
Daniel Veillardc587bce2005-05-10 15:28:08 +00005095 (xmlDictOwns(cur->doc->dict, cur->content))))
William M. Brack7762bb12004-01-04 14:49:01 +00005096 xmlFree(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00005097 }
5098 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5099 cur->last = cur->children = NULL;
5100 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005101 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00005102 } else
5103 cur->content = NULL;
5104 break;
5105 case XML_DOCUMENT_NODE:
5106 case XML_HTML_DOCUMENT_NODE:
5107 case XML_DOCUMENT_TYPE_NODE:
5108 case XML_XINCLUDE_START:
5109 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005110#ifdef LIBXML_DOCB_ENABLED
5111 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005112#endif
5113 break;
5114 case XML_NOTATION_NODE:
5115 break;
5116 case XML_DTD_NODE:
5117 break;
5118 case XML_NAMESPACE_DECL:
5119 break;
5120 case XML_ELEMENT_DECL:
5121 /* TODO !!! */
5122 break;
5123 case XML_ATTRIBUTE_DECL:
5124 /* TODO !!! */
5125 break;
5126 case XML_ENTITY_DECL:
5127 /* TODO !!! */
5128 break;
5129 }
5130}
5131
Daniel Veillard652327a2003-09-29 18:02:38 +00005132#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00005133/**
5134 * xmlNodeSetContentLen:
5135 * @cur: the node being modified
5136 * @content: the new value of the content
5137 * @len: the size of @content
5138 *
5139 * Replace the content of a node.
5140 */
5141void
5142xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5143 if (cur == NULL) {
5144#ifdef DEBUG_TREE
5145 xmlGenericError(xmlGenericErrorContext,
5146 "xmlNodeSetContentLen : node == NULL\n");
5147#endif
5148 return;
5149 }
5150 switch (cur->type) {
5151 case XML_DOCUMENT_FRAG_NODE:
5152 case XML_ELEMENT_NODE:
Daniel Veillard2c748c62002-01-16 15:37:50 +00005153 case XML_ATTRIBUTE_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005154 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5155 cur->children = xmlStringLenGetNodeList(cur->doc, content, len);
5156 UPDATE_LAST_CHILD_AND_PARENT(cur)
5157 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005158 case XML_TEXT_NODE:
5159 case XML_CDATA_SECTION_NODE:
5160 case XML_ENTITY_REF_NODE:
5161 case XML_ENTITY_NODE:
5162 case XML_PI_NODE:
5163 case XML_COMMENT_NODE:
5164 case XML_NOTATION_NODE:
5165 if (cur->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005166 xmlFree(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00005167 }
5168 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5169 cur->children = cur->last = NULL;
5170 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005171 cur->content = xmlStrndup(content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00005172 } else
5173 cur->content = NULL;
5174 break;
5175 case XML_DOCUMENT_NODE:
5176 case XML_DTD_NODE:
5177 case XML_HTML_DOCUMENT_NODE:
5178 case XML_DOCUMENT_TYPE_NODE:
5179 case XML_NAMESPACE_DECL:
5180 case XML_XINCLUDE_START:
5181 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005182#ifdef LIBXML_DOCB_ENABLED
5183 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005184#endif
5185 break;
5186 case XML_ELEMENT_DECL:
5187 /* TODO !!! */
5188 break;
5189 case XML_ATTRIBUTE_DECL:
5190 /* TODO !!! */
5191 break;
5192 case XML_ENTITY_DECL:
5193 /* TODO !!! */
5194 break;
5195 }
5196}
Daniel Veillard652327a2003-09-29 18:02:38 +00005197#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005198
5199/**
5200 * xmlNodeAddContentLen:
5201 * @cur: the node being modified
5202 * @content: extra content
5203 * @len: the size of @content
5204 *
5205 * Append the extra substring to the node content.
5206 */
5207void
5208xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5209 if (cur == NULL) {
5210#ifdef DEBUG_TREE
5211 xmlGenericError(xmlGenericErrorContext,
5212 "xmlNodeAddContentLen : node == NULL\n");
5213#endif
5214 return;
5215 }
5216 if (len <= 0) return;
5217 switch (cur->type) {
5218 case XML_DOCUMENT_FRAG_NODE:
5219 case XML_ELEMENT_NODE: {
Daniel Veillardacb2bda2002-01-13 16:15:43 +00005220 xmlNodePtr last, newNode, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00005221
Daniel Veillard7db37732001-07-12 01:20:08 +00005222 last = cur->last;
Owen Taylor3473f882001-02-23 17:55:21 +00005223 newNode = xmlNewTextLen(content, len);
5224 if (newNode != NULL) {
Daniel Veillardacb2bda2002-01-13 16:15:43 +00005225 tmp = xmlAddChild(cur, newNode);
5226 if (tmp != newNode)
5227 return;
Owen Taylor3473f882001-02-23 17:55:21 +00005228 if ((last != NULL) && (last->next == newNode)) {
5229 xmlTextMerge(last, newNode);
5230 }
5231 }
5232 break;
5233 }
5234 case XML_ATTRIBUTE_NODE:
5235 break;
5236 case XML_TEXT_NODE:
5237 case XML_CDATA_SECTION_NODE:
5238 case XML_ENTITY_REF_NODE:
5239 case XML_ENTITY_NODE:
5240 case XML_PI_NODE:
5241 case XML_COMMENT_NODE:
5242 case XML_NOTATION_NODE:
5243 if (content != NULL) {
William M. Brack7762bb12004-01-04 14:49:01 +00005244 if ((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5245 xmlDictOwns(cur->doc->dict, cur->content)) {
5246 cur->content =
5247 xmlStrncatNew(cur->content, content, len);
5248 break;
5249 }
Owen Taylor3473f882001-02-23 17:55:21 +00005250 cur->content = xmlStrncat(cur->content, content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00005251 }
5252 case XML_DOCUMENT_NODE:
5253 case XML_DTD_NODE:
5254 case XML_HTML_DOCUMENT_NODE:
5255 case XML_DOCUMENT_TYPE_NODE:
5256 case XML_NAMESPACE_DECL:
5257 case XML_XINCLUDE_START:
5258 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005259#ifdef LIBXML_DOCB_ENABLED
5260 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005261#endif
5262 break;
5263 case XML_ELEMENT_DECL:
5264 case XML_ATTRIBUTE_DECL:
5265 case XML_ENTITY_DECL:
5266 break;
5267 }
5268}
5269
5270/**
5271 * xmlNodeAddContent:
5272 * @cur: the node being modified
5273 * @content: extra content
5274 *
5275 * Append the extra substring to the node content.
5276 */
5277void
5278xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) {
5279 int len;
5280
5281 if (cur == NULL) {
5282#ifdef DEBUG_TREE
5283 xmlGenericError(xmlGenericErrorContext,
5284 "xmlNodeAddContent : node == NULL\n");
5285#endif
5286 return;
5287 }
5288 if (content == NULL) return;
5289 len = xmlStrlen(content);
5290 xmlNodeAddContentLen(cur, content, len);
5291}
5292
5293/**
5294 * xmlTextMerge:
5295 * @first: the first text node
5296 * @second: the second text node being merged
5297 *
5298 * Merge two text nodes into one
5299 * Returns the first text node augmented
5300 */
5301xmlNodePtr
5302xmlTextMerge(xmlNodePtr first, xmlNodePtr second) {
5303 if (first == NULL) return(second);
5304 if (second == NULL) return(first);
5305 if (first->type != XML_TEXT_NODE) return(first);
5306 if (second->type != XML_TEXT_NODE) return(first);
5307 if (second->name != first->name)
5308 return(first);
Owen Taylor3473f882001-02-23 17:55:21 +00005309 xmlNodeAddContent(first, second->content);
Owen Taylor3473f882001-02-23 17:55:21 +00005310 xmlUnlinkNode(second);
5311 xmlFreeNode(second);
5312 return(first);
5313}
5314
Daniel Veillard2156d432004-03-04 15:59:36 +00005315#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XPATH_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00005316/**
5317 * xmlGetNsList:
5318 * @doc: the document
5319 * @node: the current node
5320 *
5321 * Search all the namespace applying to a given element.
Daniel Veillardd1640922001-12-17 15:30:10 +00005322 * Returns an NULL terminated array of all the #xmlNsPtr found
Owen Taylor3473f882001-02-23 17:55:21 +00005323 * that need to be freed by the caller or NULL if no
5324 * namespace if defined
5325 */
5326xmlNsPtr *
Daniel Veillard77044732001-06-29 21:31:07 +00005327xmlGetNsList(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node)
5328{
Owen Taylor3473f882001-02-23 17:55:21 +00005329 xmlNsPtr cur;
5330 xmlNsPtr *ret = NULL;
5331 int nbns = 0;
5332 int maxns = 10;
5333 int i;
5334
5335 while (node != NULL) {
Daniel Veillard77044732001-06-29 21:31:07 +00005336 if (node->type == XML_ELEMENT_NODE) {
5337 cur = node->nsDef;
5338 while (cur != NULL) {
5339 if (ret == NULL) {
5340 ret =
5341 (xmlNsPtr *) xmlMalloc((maxns + 1) *
5342 sizeof(xmlNsPtr));
5343 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005344 xmlTreeErrMemory("getting namespace list");
Daniel Veillard77044732001-06-29 21:31:07 +00005345 return (NULL);
5346 }
5347 ret[nbns] = NULL;
5348 }
5349 for (i = 0; i < nbns; i++) {
5350 if ((cur->prefix == ret[i]->prefix) ||
5351 (xmlStrEqual(cur->prefix, ret[i]->prefix)))
5352 break;
5353 }
5354 if (i >= nbns) {
5355 if (nbns >= maxns) {
5356 maxns *= 2;
5357 ret = (xmlNsPtr *) xmlRealloc(ret,
5358 (maxns +
5359 1) *
5360 sizeof(xmlNsPtr));
5361 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005362 xmlTreeErrMemory("getting namespace list");
Daniel Veillard77044732001-06-29 21:31:07 +00005363 return (NULL);
5364 }
5365 }
5366 ret[nbns++] = cur;
5367 ret[nbns] = NULL;
5368 }
Owen Taylor3473f882001-02-23 17:55:21 +00005369
Daniel Veillard77044732001-06-29 21:31:07 +00005370 cur = cur->next;
5371 }
5372 }
5373 node = node->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00005374 }
Daniel Veillard77044732001-06-29 21:31:07 +00005375 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00005376}
Daniel Veillard652327a2003-09-29 18:02:38 +00005377#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005378
5379/**
5380 * xmlSearchNs:
5381 * @doc: the document
5382 * @node: the current node
Daniel Veillard77851712001-02-27 21:54:07 +00005383 * @nameSpace: the namespace prefix
Owen Taylor3473f882001-02-23 17:55:21 +00005384 *
5385 * Search a Ns registered under a given name space for a document.
5386 * recurse on the parents until it finds the defined namespace
5387 * or return NULL otherwise.
5388 * @nameSpace can be NULL, this is a search for the default namespace.
5389 * We don't allow to cross entities boundaries. If you don't declare
5390 * the namespace within those you will be in troubles !!! A warning
5391 * is generated to cover this case.
5392 *
5393 * Returns the namespace pointer or NULL.
5394 */
5395xmlNsPtr
5396xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) {
Daniel Veillardf4e56292003-10-28 14:27:41 +00005397
Owen Taylor3473f882001-02-23 17:55:21 +00005398 xmlNsPtr cur;
Daniel Veillardf4e56292003-10-28 14:27:41 +00005399 xmlNodePtr orig = node;
Owen Taylor3473f882001-02-23 17:55:21 +00005400
5401 if (node == NULL) return(NULL);
5402 if ((nameSpace != NULL) &&
5403 (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) {
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00005404 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
5405 /*
5406 * The XML-1.0 namespace is normally held on the root
5407 * element. In this case exceptionally create it on the
5408 * node element.
5409 */
5410 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5411 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005412 xmlTreeErrMemory("searching namespace");
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00005413 return(NULL);
5414 }
5415 memset(cur, 0, sizeof(xmlNs));
5416 cur->type = XML_LOCAL_NAMESPACE;
5417 cur->href = xmlStrdup(XML_XML_NAMESPACE);
5418 cur->prefix = xmlStrdup((const xmlChar *)"xml");
5419 cur->next = node->nsDef;
5420 node->nsDef = cur;
5421 return(cur);
5422 }
Owen Taylor3473f882001-02-23 17:55:21 +00005423 if (doc->oldNs == NULL) {
5424 /*
5425 * Allocate a new Namespace and fill the fields.
5426 */
5427 doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5428 if (doc->oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005429 xmlTreeErrMemory("searching namespace");
Owen Taylor3473f882001-02-23 17:55:21 +00005430 return(NULL);
5431 }
5432 memset(doc->oldNs, 0, sizeof(xmlNs));
5433 doc->oldNs->type = XML_LOCAL_NAMESPACE;
5434
5435 doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE);
5436 doc->oldNs->prefix = xmlStrdup((const xmlChar *)"xml");
5437 }
5438 return(doc->oldNs);
5439 }
5440 while (node != NULL) {
5441 if ((node->type == XML_ENTITY_REF_NODE) ||
5442 (node->type == XML_ENTITY_NODE) ||
5443 (node->type == XML_ENTITY_DECL))
5444 return(NULL);
5445 if (node->type == XML_ELEMENT_NODE) {
5446 cur = node->nsDef;
5447 while (cur != NULL) {
5448 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
5449 (cur->href != NULL))
5450 return(cur);
5451 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
5452 (cur->href != NULL) &&
5453 (xmlStrEqual(cur->prefix, nameSpace)))
5454 return(cur);
5455 cur = cur->next;
5456 }
Daniel Veillardf4e56292003-10-28 14:27:41 +00005457 if (orig != node) {
5458 cur = node->ns;
5459 if (cur != NULL) {
5460 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
5461 (cur->href != NULL))
5462 return(cur);
5463 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
5464 (cur->href != NULL) &&
5465 (xmlStrEqual(cur->prefix, nameSpace)))
5466 return(cur);
5467 }
5468 }
Owen Taylor3473f882001-02-23 17:55:21 +00005469 }
5470 node = node->parent;
5471 }
5472 return(NULL);
5473}
5474
5475/**
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005476 * xmlNsInScope:
5477 * @doc: the document
5478 * @node: the current node
5479 * @ancestor: the ancestor carrying the namespace
5480 * @prefix: the namespace prefix
5481 *
5482 * Verify that the given namespace held on @ancestor is still in scope
5483 * on node.
5484 *
5485 * Returns 1 if true, 0 if false and -1 in case of error.
5486 */
5487static int
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005488xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node,
5489 xmlNodePtr ancestor, const xmlChar * prefix)
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005490{
5491 xmlNsPtr tst;
5492
5493 while ((node != NULL) && (node != ancestor)) {
5494 if ((node->type == XML_ENTITY_REF_NODE) ||
5495 (node->type == XML_ENTITY_NODE) ||
5496 (node->type == XML_ENTITY_DECL))
5497 return (-1);
5498 if (node->type == XML_ELEMENT_NODE) {
5499 tst = node->nsDef;
5500 while (tst != NULL) {
5501 if ((tst->prefix == NULL)
5502 && (prefix == NULL))
5503 return (0);
5504 if ((tst->prefix != NULL)
5505 && (prefix != NULL)
5506 && (xmlStrEqual(tst->prefix, prefix)))
5507 return (0);
5508 tst = tst->next;
5509 }
5510 }
5511 node = node->parent;
5512 }
5513 if (node != ancestor)
5514 return (-1);
5515 return (1);
5516}
5517
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005518/**
Owen Taylor3473f882001-02-23 17:55:21 +00005519 * xmlSearchNsByHref:
5520 * @doc: the document
5521 * @node: the current node
5522 * @href: the namespace value
5523 *
5524 * Search a Ns aliasing a given URI. Recurse on the parents until it finds
5525 * the defined namespace or return NULL otherwise.
5526 * Returns the namespace pointer or NULL.
5527 */
5528xmlNsPtr
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005529xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href)
5530{
Owen Taylor3473f882001-02-23 17:55:21 +00005531 xmlNsPtr cur;
5532 xmlNodePtr orig = node;
Daniel Veillard62040be2004-05-17 03:17:26 +00005533 int is_attr;
Owen Taylor3473f882001-02-23 17:55:21 +00005534
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005535 if ((node == NULL) || (href == NULL))
5536 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005537 if (xmlStrEqual(href, XML_XML_NAMESPACE)) {
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005538 /*
5539 * Only the document can hold the XML spec namespace.
5540 */
5541 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
5542 /*
5543 * The XML-1.0 namespace is normally held on the root
5544 * element. In this case exceptionally create it on the
5545 * node element.
5546 */
5547 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5548 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005549 xmlTreeErrMemory("searching namespace");
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005550 return (NULL);
5551 }
5552 memset(cur, 0, sizeof(xmlNs));
5553 cur->type = XML_LOCAL_NAMESPACE;
5554 cur->href = xmlStrdup(XML_XML_NAMESPACE);
5555 cur->prefix = xmlStrdup((const xmlChar *) "xml");
5556 cur->next = node->nsDef;
5557 node->nsDef = cur;
5558 return (cur);
5559 }
5560 if (doc->oldNs == NULL) {
5561 /*
5562 * Allocate a new Namespace and fill the fields.
5563 */
5564 doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5565 if (doc->oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005566 xmlTreeErrMemory("searching namespace");
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005567 return (NULL);
5568 }
5569 memset(doc->oldNs, 0, sizeof(xmlNs));
5570 doc->oldNs->type = XML_LOCAL_NAMESPACE;
Owen Taylor3473f882001-02-23 17:55:21 +00005571
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005572 doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE);
5573 doc->oldNs->prefix = xmlStrdup((const xmlChar *) "xml");
5574 }
5575 return (doc->oldNs);
Owen Taylor3473f882001-02-23 17:55:21 +00005576 }
Daniel Veillard62040be2004-05-17 03:17:26 +00005577 is_attr = (node->type == XML_ATTRIBUTE_NODE);
Owen Taylor3473f882001-02-23 17:55:21 +00005578 while (node != NULL) {
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005579 if ((node->type == XML_ENTITY_REF_NODE) ||
5580 (node->type == XML_ENTITY_NODE) ||
5581 (node->type == XML_ENTITY_DECL))
5582 return (NULL);
5583 if (node->type == XML_ELEMENT_NODE) {
5584 cur = node->nsDef;
5585 while (cur != NULL) {
5586 if ((cur->href != NULL) && (href != NULL) &&
5587 (xmlStrEqual(cur->href, href))) {
Daniel Veillard62040be2004-05-17 03:17:26 +00005588 if (((!is_attr) || (cur->prefix != NULL)) &&
Kasimier T. Buchcikba70cc02005-02-28 10:28:21 +00005589 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005590 return (cur);
5591 }
5592 cur = cur->next;
5593 }
Daniel Veillardf4e56292003-10-28 14:27:41 +00005594 if (orig != node) {
5595 cur = node->ns;
5596 if (cur != NULL) {
5597 if ((cur->href != NULL) && (href != NULL) &&
5598 (xmlStrEqual(cur->href, href))) {
Daniel Veillard62040be2004-05-17 03:17:26 +00005599 if (((!is_attr) || (cur->prefix != NULL)) &&
Kasimier T. Buchcikba70cc02005-02-28 10:28:21 +00005600 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
Daniel Veillardf4e56292003-10-28 14:27:41 +00005601 return (cur);
5602 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005603 }
Daniel Veillardf4e56292003-10-28 14:27:41 +00005604 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005605 }
5606 node = node->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00005607 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005608 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005609}
5610
5611/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005612 * xmlNewReconciliedNs:
Owen Taylor3473f882001-02-23 17:55:21 +00005613 * @doc: the document
5614 * @tree: a node expected to hold the new namespace
5615 * @ns: the original namespace
5616 *
5617 * This function tries to locate a namespace definition in a tree
5618 * ancestors, or create a new namespace definition node similar to
5619 * @ns trying to reuse the same prefix. However if the given prefix is
5620 * null (default namespace) or reused within the subtree defined by
5621 * @tree or on one of its ancestors then a new prefix is generated.
5622 * Returns the (new) namespace definition or NULL in case of error
5623 */
5624xmlNsPtr
5625xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) {
5626 xmlNsPtr def;
5627 xmlChar prefix[50];
5628 int counter = 1;
5629
5630 if (tree == NULL) {
5631#ifdef DEBUG_TREE
5632 xmlGenericError(xmlGenericErrorContext,
5633 "xmlNewReconciliedNs : tree == NULL\n");
5634#endif
5635 return(NULL);
5636 }
Daniel Veillardce244ad2004-11-05 10:03:46 +00005637 if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005638#ifdef DEBUG_TREE
5639 xmlGenericError(xmlGenericErrorContext,
5640 "xmlNewReconciliedNs : ns == NULL\n");
5641#endif
5642 return(NULL);
5643 }
5644 /*
5645 * Search an existing namespace definition inherited.
5646 */
5647 def = xmlSearchNsByHref(doc, tree, ns->href);
5648 if (def != NULL)
5649 return(def);
5650
5651 /*
5652 * Find a close prefix which is not already in use.
5653 * Let's strip namespace prefixes longer than 20 chars !
5654 */
Daniel Veillardf742d342002-03-07 00:05:35 +00005655 if (ns->prefix == NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00005656 snprintf((char *) prefix, sizeof(prefix), "default");
Daniel Veillardf742d342002-03-07 00:05:35 +00005657 else
William M. Brack13dfa872004-09-18 04:52:08 +00005658 snprintf((char *) prefix, sizeof(prefix), "%.20s", (char *)ns->prefix);
Daniel Veillardf742d342002-03-07 00:05:35 +00005659
Owen Taylor3473f882001-02-23 17:55:21 +00005660 def = xmlSearchNs(doc, tree, prefix);
5661 while (def != NULL) {
5662 if (counter > 1000) return(NULL);
Daniel Veillardf742d342002-03-07 00:05:35 +00005663 if (ns->prefix == NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00005664 snprintf((char *) prefix, sizeof(prefix), "default%d", counter++);
Daniel Veillardf742d342002-03-07 00:05:35 +00005665 else
William M. Brack13dfa872004-09-18 04:52:08 +00005666 snprintf((char *) prefix, sizeof(prefix), "%.20s%d",
5667 (char *)ns->prefix, counter++);
Owen Taylor3473f882001-02-23 17:55:21 +00005668 def = xmlSearchNs(doc, tree, prefix);
5669 }
5670
5671 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00005672 * OK, now we are ready to create a new one.
Owen Taylor3473f882001-02-23 17:55:21 +00005673 */
5674 def = xmlNewNs(tree, ns->href, prefix);
5675 return(def);
5676}
5677
Daniel Veillard652327a2003-09-29 18:02:38 +00005678#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00005679/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005680 * xmlReconciliateNs:
Owen Taylor3473f882001-02-23 17:55:21 +00005681 * @doc: the document
5682 * @tree: a node defining the subtree to reconciliate
5683 *
5684 * This function checks that all the namespaces declared within the given
5685 * tree are properly declared. This is needed for example after Copy or Cut
5686 * and then paste operations. The subtree may still hold pointers to
5687 * namespace declarations outside the subtree or invalid/masked. As much
Daniel Veillardd1640922001-12-17 15:30:10 +00005688 * as possible the function try to reuse the existing namespaces found in
Owen Taylor3473f882001-02-23 17:55:21 +00005689 * the new environment. If not possible the new namespaces are redeclared
5690 * on @tree at the top of the given subtree.
5691 * Returns the number of namespace declarations created or -1 in case of error.
5692 */
5693int
5694xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) {
5695 xmlNsPtr *oldNs = NULL;
5696 xmlNsPtr *newNs = NULL;
5697 int sizeCache = 0;
5698 int nbCache = 0;
5699
5700 xmlNsPtr n;
5701 xmlNodePtr node = tree;
5702 xmlAttrPtr attr;
5703 int ret = 0, i;
5704
Daniel Veillardce244ad2004-11-05 10:03:46 +00005705 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) return(-1);
5706 if ((doc == NULL) || (doc->type != XML_DOCUMENT_NODE)) return(-1);
5707 if (node->doc != doc) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00005708 while (node != NULL) {
5709 /*
5710 * Reconciliate the node namespace
5711 */
5712 if (node->ns != NULL) {
5713 /*
5714 * initialize the cache if needed
5715 */
5716 if (sizeCache == 0) {
5717 sizeCache = 10;
5718 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
5719 sizeof(xmlNsPtr));
5720 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005721 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005722 return(-1);
5723 }
5724 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
5725 sizeof(xmlNsPtr));
5726 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005727 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005728 xmlFree(oldNs);
5729 return(-1);
5730 }
5731 }
5732 for (i = 0;i < nbCache;i++) {
5733 if (oldNs[i] == node->ns) {
5734 node->ns = newNs[i];
5735 break;
5736 }
5737 }
5738 if (i == nbCache) {
5739 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00005740 * OK we need to recreate a new namespace definition
Owen Taylor3473f882001-02-23 17:55:21 +00005741 */
5742 n = xmlNewReconciliedNs(doc, tree, node->ns);
5743 if (n != NULL) { /* :-( what if else ??? */
5744 /*
5745 * check if we need to grow the cache buffers.
5746 */
5747 if (sizeCache <= nbCache) {
5748 sizeCache *= 2;
5749 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
5750 sizeof(xmlNsPtr));
5751 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005752 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005753 xmlFree(newNs);
5754 return(-1);
5755 }
5756 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
5757 sizeof(xmlNsPtr));
5758 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005759 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005760 xmlFree(oldNs);
5761 return(-1);
5762 }
5763 }
5764 newNs[nbCache] = n;
5765 oldNs[nbCache++] = node->ns;
5766 node->ns = n;
5767 }
5768 }
5769 }
5770 /*
5771 * now check for namespace hold by attributes on the node.
5772 */
5773 attr = node->properties;
5774 while (attr != NULL) {
5775 if (attr->ns != NULL) {
5776 /*
5777 * initialize the cache if needed
5778 */
5779 if (sizeCache == 0) {
5780 sizeCache = 10;
5781 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
5782 sizeof(xmlNsPtr));
5783 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005784 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005785 return(-1);
5786 }
5787 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
5788 sizeof(xmlNsPtr));
5789 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005790 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005791 xmlFree(oldNs);
5792 return(-1);
5793 }
5794 }
5795 for (i = 0;i < nbCache;i++) {
5796 if (oldNs[i] == attr->ns) {
Daniel Veillardce66ce12002-10-28 19:01:59 +00005797 attr->ns = newNs[i];
Owen Taylor3473f882001-02-23 17:55:21 +00005798 break;
5799 }
5800 }
5801 if (i == nbCache) {
5802 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00005803 * OK we need to recreate a new namespace definition
Owen Taylor3473f882001-02-23 17:55:21 +00005804 */
5805 n = xmlNewReconciliedNs(doc, tree, attr->ns);
5806 if (n != NULL) { /* :-( what if else ??? */
5807 /*
5808 * check if we need to grow the cache buffers.
5809 */
5810 if (sizeCache <= nbCache) {
5811 sizeCache *= 2;
5812 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
5813 sizeof(xmlNsPtr));
5814 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005815 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005816 xmlFree(newNs);
5817 return(-1);
5818 }
5819 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
5820 sizeof(xmlNsPtr));
5821 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005822 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005823 xmlFree(oldNs);
5824 return(-1);
5825 }
5826 }
5827 newNs[nbCache] = n;
5828 oldNs[nbCache++] = attr->ns;
5829 attr->ns = n;
5830 }
5831 }
5832 }
5833 attr = attr->next;
5834 }
5835
5836 /*
5837 * Browse the full subtree, deep first
5838 */
Daniel Veillardac996a12004-07-30 12:02:58 +00005839 if (node->children != NULL && node->type != XML_ENTITY_REF_NODE) {
Owen Taylor3473f882001-02-23 17:55:21 +00005840 /* deep first */
5841 node = node->children;
5842 } else if ((node != tree) && (node->next != NULL)) {
5843 /* then siblings */
5844 node = node->next;
5845 } else if (node != tree) {
5846 /* go up to parents->next if needed */
5847 while (node != tree) {
5848 if (node->parent != NULL)
5849 node = node->parent;
5850 if ((node != tree) && (node->next != NULL)) {
5851 node = node->next;
5852 break;
5853 }
5854 if (node->parent == NULL) {
5855 node = NULL;
5856 break;
5857 }
5858 }
5859 /* exit condition */
5860 if (node == tree)
5861 node = NULL;
Daniel Veillard1e774382002-03-06 17:35:40 +00005862 } else
5863 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005864 }
Daniel Veillardf742d342002-03-07 00:05:35 +00005865 if (oldNs != NULL)
5866 xmlFree(oldNs);
5867 if (newNs != NULL)
5868 xmlFree(newNs);
Owen Taylor3473f882001-02-23 17:55:21 +00005869 return(ret);
5870}
Daniel Veillard652327a2003-09-29 18:02:38 +00005871#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005872
5873/**
5874 * xmlHasProp:
5875 * @node: the node
5876 * @name: the attribute name
5877 *
5878 * Search an attribute associated to a node
5879 * This function also looks in DTD attribute declaration for #FIXED or
5880 * default declaration values unless DTD use has been turned off.
5881 *
5882 * Returns the attribute or the attribute declaration or NULL if
5883 * neither was found.
5884 */
5885xmlAttrPtr
5886xmlHasProp(xmlNodePtr node, const xmlChar *name) {
5887 xmlAttrPtr prop;
5888 xmlDocPtr doc;
5889
5890 if ((node == NULL) || (name == NULL)) return(NULL);
5891 /*
5892 * Check on the properties attached to the node
5893 */
5894 prop = node->properties;
5895 while (prop != NULL) {
5896 if (xmlStrEqual(prop->name, name)) {
5897 return(prop);
5898 }
5899 prop = prop->next;
5900 }
5901 if (!xmlCheckDTD) return(NULL);
5902
5903 /*
5904 * Check if there is a default declaration in the internal
5905 * or external subsets
5906 */
5907 doc = node->doc;
5908 if (doc != NULL) {
5909 xmlAttributePtr attrDecl;
5910 if (doc->intSubset != NULL) {
5911 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
5912 if ((attrDecl == NULL) && (doc->extSubset != NULL))
5913 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
Daniel Veillard75eb1ad2003-07-07 14:42:44 +00005914 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
5915 /* return attribute declaration only if a default value is given
5916 (that includes #FIXED declarations) */
Owen Taylor3473f882001-02-23 17:55:21 +00005917 return((xmlAttrPtr) attrDecl);
5918 }
5919 }
5920 return(NULL);
5921}
5922
5923/**
Daniel Veillarde95e2392001-06-06 10:46:28 +00005924 * xmlHasNsProp:
5925 * @node: the node
5926 * @name: the attribute name
Daniel Veillardca2366a2001-06-11 12:09:01 +00005927 * @nameSpace: the URI of the namespace
Daniel Veillarde95e2392001-06-06 10:46:28 +00005928 *
5929 * Search for an attribute associated to a node
5930 * This attribute has to be anchored in the namespace specified.
5931 * This does the entity substitution.
5932 * This function looks in DTD attribute declaration for #FIXED or
5933 * default declaration values unless DTD use has been turned off.
William M. Brack2c228442004-10-03 04:10:00 +00005934 * Note that a namespace of NULL indicates to use the default namespace.
Daniel Veillarde95e2392001-06-06 10:46:28 +00005935 *
5936 * Returns the attribute or the attribute declaration or NULL
5937 * if neither was found.
5938 */
5939xmlAttrPtr
Daniel Veillardca2366a2001-06-11 12:09:01 +00005940xmlHasNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
Daniel Veillarde95e2392001-06-06 10:46:28 +00005941 xmlAttrPtr prop;
Daniel Veillard652327a2003-09-29 18:02:38 +00005942#ifdef LIBXML_TREE_ENABLED
Daniel Veillarde95e2392001-06-06 10:46:28 +00005943 xmlDocPtr doc;
Daniel Veillard652327a2003-09-29 18:02:38 +00005944#endif /* LIBXML_TREE_ENABLED */
Daniel Veillarde95e2392001-06-06 10:46:28 +00005945
5946 if (node == NULL)
5947 return(NULL);
5948
5949 prop = node->properties;
Daniel Veillarde95e2392001-06-06 10:46:28 +00005950 while (prop != NULL) {
5951 /*
5952 * One need to have
5953 * - same attribute names
5954 * - and the attribute carrying that namespace
Daniel Veillarde95e2392001-06-06 10:46:28 +00005955 */
William M. Brack2c228442004-10-03 04:10:00 +00005956 if (xmlStrEqual(prop->name, name)) {
5957 if (((prop->ns != NULL) &&
5958 (xmlStrEqual(prop->ns->href, nameSpace))) ||
5959 ((prop->ns == NULL) && (nameSpace == NULL))) {
5960 return(prop);
5961 }
Daniel Veillarde95e2392001-06-06 10:46:28 +00005962 }
5963 prop = prop->next;
5964 }
5965 if (!xmlCheckDTD) return(NULL);
5966
Daniel Veillard652327a2003-09-29 18:02:38 +00005967#ifdef LIBXML_TREE_ENABLED
Daniel Veillarde95e2392001-06-06 10:46:28 +00005968 /*
5969 * Check if there is a default declaration in the internal
5970 * or external subsets
5971 */
5972 doc = node->doc;
5973 if (doc != NULL) {
5974 if (doc->intSubset != NULL) {
Daniel Veillardef6c46f2002-03-07 22:21:56 +00005975 xmlAttributePtr attrDecl = NULL;
5976 xmlNsPtr *nsList, *cur;
5977 xmlChar *ename;
Daniel Veillarde95e2392001-06-06 10:46:28 +00005978
Daniel Veillardef6c46f2002-03-07 22:21:56 +00005979 nsList = xmlGetNsList(node->doc, node);
5980 if (nsList == NULL)
5981 return(NULL);
5982 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
5983 ename = xmlStrdup(node->ns->prefix);
5984 ename = xmlStrcat(ename, BAD_CAST ":");
5985 ename = xmlStrcat(ename, node->name);
5986 } else {
5987 ename = xmlStrdup(node->name);
Daniel Veillarde95e2392001-06-06 10:46:28 +00005988 }
Daniel Veillardef6c46f2002-03-07 22:21:56 +00005989 if (ename == NULL) {
5990 xmlFree(nsList);
5991 return(NULL);
5992 }
5993
William M. Brack2c228442004-10-03 04:10:00 +00005994 if (nameSpace == NULL) {
5995 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, ename,
5996 name, NULL);
5997 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
5998 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, ename,
5999 name, NULL);
Daniel Veillardef6c46f2002-03-07 22:21:56 +00006000 }
William M. Brack2c228442004-10-03 04:10:00 +00006001 } else {
6002 cur = nsList;
6003 while (*cur != NULL) {
6004 if (xmlStrEqual((*cur)->href, nameSpace)) {
6005 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, ename,
6006 name, (*cur)->prefix);
6007 if ((attrDecl == NULL) && (doc->extSubset != NULL))
6008 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, ename,
6009 name, (*cur)->prefix);
6010 }
6011 cur++;
6012 }
Daniel Veillardef6c46f2002-03-07 22:21:56 +00006013 }
6014 xmlFree(nsList);
6015 xmlFree(ename);
6016 return((xmlAttrPtr) attrDecl);
Daniel Veillarde95e2392001-06-06 10:46:28 +00006017 }
6018 }
Daniel Veillard652327a2003-09-29 18:02:38 +00006019#endif /* LIBXML_TREE_ENABLED */
Daniel Veillarde95e2392001-06-06 10:46:28 +00006020 return(NULL);
6021}
6022
6023/**
Owen Taylor3473f882001-02-23 17:55:21 +00006024 * xmlGetProp:
6025 * @node: the node
6026 * @name: the attribute name
6027 *
6028 * Search and get the value of an attribute associated to a node
6029 * This does the entity substitution.
6030 * This function looks in DTD attribute declaration for #FIXED or
6031 * default declaration values unless DTD use has been turned off.
Daniel Veillard784b9352003-02-16 15:50:27 +00006032 * NOTE: this function acts independently of namespaces associated
Daniel Veillard71531f32003-02-05 13:19:53 +00006033 * to the attribute. Use xmlGetNsProp() or xmlGetNoNsProp()
6034 * for namespace aware processing.
Owen Taylor3473f882001-02-23 17:55:21 +00006035 *
6036 * Returns the attribute value or NULL if not found.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00006037 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00006038 */
6039xmlChar *
6040xmlGetProp(xmlNodePtr node, const xmlChar *name) {
6041 xmlAttrPtr prop;
6042 xmlDocPtr doc;
6043
6044 if ((node == NULL) || (name == NULL)) return(NULL);
6045 /*
6046 * Check on the properties attached to the node
6047 */
6048 prop = node->properties;
6049 while (prop != NULL) {
6050 if (xmlStrEqual(prop->name, name)) {
6051 xmlChar *ret;
6052
6053 ret = xmlNodeListGetString(node->doc, prop->children, 1);
6054 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
6055 return(ret);
6056 }
6057 prop = prop->next;
6058 }
6059 if (!xmlCheckDTD) return(NULL);
6060
6061 /*
6062 * Check if there is a default declaration in the internal
6063 * or external subsets
6064 */
6065 doc = node->doc;
6066 if (doc != NULL) {
6067 xmlAttributePtr attrDecl;
6068 if (doc->intSubset != NULL) {
6069 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
6070 if ((attrDecl == NULL) && (doc->extSubset != NULL))
6071 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
Daniel Veillard75eb1ad2003-07-07 14:42:44 +00006072 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6073 /* return attribute declaration only if a default value is given
6074 (that includes #FIXED declarations) */
Owen Taylor3473f882001-02-23 17:55:21 +00006075 return(xmlStrdup(attrDecl->defaultValue));
6076 }
6077 }
6078 return(NULL);
6079}
6080
6081/**
Daniel Veillard71531f32003-02-05 13:19:53 +00006082 * xmlGetNoNsProp:
6083 * @node: the node
6084 * @name: the attribute name
6085 *
6086 * Search and get the value of an attribute associated to a node
6087 * This does the entity substitution.
6088 * This function looks in DTD attribute declaration for #FIXED or
6089 * default declaration values unless DTD use has been turned off.
6090 * This function is similar to xmlGetProp except it will accept only
6091 * an attribute in no namespace.
6092 *
6093 * Returns the attribute value or NULL if not found.
6094 * It's up to the caller to free the memory with xmlFree().
6095 */
6096xmlChar *
6097xmlGetNoNsProp(xmlNodePtr node, const xmlChar *name) {
6098 xmlAttrPtr prop;
6099 xmlDocPtr doc;
6100
6101 if ((node == NULL) || (name == NULL)) return(NULL);
6102 /*
6103 * Check on the properties attached to the node
6104 */
6105 prop = node->properties;
6106 while (prop != NULL) {
6107 if ((prop->ns == NULL) && (xmlStrEqual(prop->name, name))) {
6108 xmlChar *ret;
6109
6110 ret = xmlNodeListGetString(node->doc, prop->children, 1);
6111 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
6112 return(ret);
6113 }
6114 prop = prop->next;
6115 }
6116 if (!xmlCheckDTD) return(NULL);
6117
6118 /*
6119 * Check if there is a default declaration in the internal
6120 * or external subsets
6121 */
6122 doc = node->doc;
6123 if (doc != NULL) {
6124 xmlAttributePtr attrDecl;
6125 if (doc->intSubset != NULL) {
6126 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
6127 if ((attrDecl == NULL) && (doc->extSubset != NULL))
6128 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
Daniel Veillard75eb1ad2003-07-07 14:42:44 +00006129 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6130 /* return attribute declaration only if a default value is given
6131 (that includes #FIXED declarations) */
Daniel Veillard71531f32003-02-05 13:19:53 +00006132 return(xmlStrdup(attrDecl->defaultValue));
6133 }
6134 }
6135 return(NULL);
6136}
6137
6138/**
Owen Taylor3473f882001-02-23 17:55:21 +00006139 * xmlGetNsProp:
6140 * @node: the node
6141 * @name: the attribute name
Daniel Veillardca2366a2001-06-11 12:09:01 +00006142 * @nameSpace: the URI of the namespace
Owen Taylor3473f882001-02-23 17:55:21 +00006143 *
6144 * Search and get the value of an attribute associated to a node
6145 * This attribute has to be anchored in the namespace specified.
6146 * This does the entity substitution.
6147 * This function looks in DTD attribute declaration for #FIXED or
6148 * default declaration values unless DTD use has been turned off.
6149 *
6150 * Returns the attribute value or NULL if not found.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00006151 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00006152 */
6153xmlChar *
Daniel Veillardca2366a2001-06-11 12:09:01 +00006154xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
Owen Taylor3473f882001-02-23 17:55:21 +00006155 xmlAttrPtr prop;
6156 xmlDocPtr doc;
6157 xmlNsPtr ns;
6158
6159 if (node == NULL)
6160 return(NULL);
6161
6162 prop = node->properties;
Daniel Veillardca2366a2001-06-11 12:09:01 +00006163 if (nameSpace == NULL)
Daniel Veillard71531f32003-02-05 13:19:53 +00006164 return(xmlGetNoNsProp(node, name));
Owen Taylor3473f882001-02-23 17:55:21 +00006165 while (prop != NULL) {
6166 /*
6167 * One need to have
6168 * - same attribute names
6169 * - and the attribute carrying that namespace
Owen Taylor3473f882001-02-23 17:55:21 +00006170 */
6171 if ((xmlStrEqual(prop->name, name)) &&
Daniel Veillarde8fc08e2001-06-07 19:35:47 +00006172 ((prop->ns != NULL) &&
Daniel Veillardca2366a2001-06-11 12:09:01 +00006173 (xmlStrEqual(prop->ns->href, nameSpace)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00006174 xmlChar *ret;
6175
6176 ret = xmlNodeListGetString(node->doc, prop->children, 1);
6177 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
6178 return(ret);
6179 }
6180 prop = prop->next;
6181 }
6182 if (!xmlCheckDTD) return(NULL);
6183
6184 /*
6185 * Check if there is a default declaration in the internal
6186 * or external subsets
6187 */
6188 doc = node->doc;
6189 if (doc != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00006190 if (doc->intSubset != NULL) {
Daniel Veillard5792e162001-04-30 17:44:45 +00006191 xmlAttributePtr attrDecl;
6192
Owen Taylor3473f882001-02-23 17:55:21 +00006193 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
6194 if ((attrDecl == NULL) && (doc->extSubset != NULL))
6195 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
6196
6197 if ((attrDecl != NULL) && (attrDecl->prefix != NULL)) {
6198 /*
6199 * The DTD declaration only allows a prefix search
6200 */
6201 ns = xmlSearchNs(doc, node, attrDecl->prefix);
Daniel Veillardca2366a2001-06-11 12:09:01 +00006202 if ((ns != NULL) && (xmlStrEqual(ns->href, nameSpace)))
Owen Taylor3473f882001-02-23 17:55:21 +00006203 return(xmlStrdup(attrDecl->defaultValue));
6204 }
6205 }
6206 }
6207 return(NULL);
6208}
6209
Daniel Veillard2156d432004-03-04 15:59:36 +00006210#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
6211/**
6212 * xmlUnsetProp:
6213 * @node: the node
6214 * @name: the attribute name
6215 *
6216 * Remove an attribute carried by a node.
6217 * Returns 0 if successful, -1 if not found
6218 */
6219int
6220xmlUnsetProp(xmlNodePtr node, const xmlChar *name) {
6221 xmlAttrPtr prop, prev = NULL;;
6222
6223 if ((node == NULL) || (name == NULL))
6224 return(-1);
6225 prop = node->properties;
6226 while (prop != NULL) {
6227 if ((xmlStrEqual(prop->name, name)) &&
6228 (prop->ns == NULL)) {
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00006229 xmlUnlinkNode((xmlNodePtr) prop);
Daniel Veillard2156d432004-03-04 15:59:36 +00006230 xmlFreeProp(prop);
6231 return(0);
6232 }
6233 prev = prop;
6234 prop = prop->next;
6235 }
6236 return(-1);
6237}
6238
6239/**
6240 * xmlUnsetNsProp:
6241 * @node: the node
6242 * @ns: the namespace definition
6243 * @name: the attribute name
6244 *
6245 * Remove an attribute carried by a node.
6246 * Returns 0 if successful, -1 if not found
6247 */
6248int
6249xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) {
Daniel Veillard27f20102004-11-05 11:50:11 +00006250 xmlAttrPtr prop, prev = NULL;;
Daniel Veillard2156d432004-03-04 15:59:36 +00006251
6252 if ((node == NULL) || (name == NULL))
6253 return(-1);
Daniel Veillard27f20102004-11-05 11:50:11 +00006254 prop = node->properties;
Daniel Veillard2156d432004-03-04 15:59:36 +00006255 if (ns == NULL)
6256 return(xmlUnsetProp(node, name));
6257 if (ns->href == NULL)
6258 return(-1);
6259 while (prop != NULL) {
6260 if ((xmlStrEqual(prop->name, name)) &&
6261 (prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))) {
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00006262 xmlUnlinkNode((xmlNodePtr) prop);
Daniel Veillard2156d432004-03-04 15:59:36 +00006263 xmlFreeProp(prop);
6264 return(0);
6265 }
6266 prev = prop;
6267 prop = prop->next;
6268 }
6269 return(-1);
6270}
6271#endif
6272
6273#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_HTML_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00006274/**
6275 * xmlSetProp:
6276 * @node: the node
6277 * @name: the attribute name
6278 * @value: the attribute value
6279 *
6280 * Set (or reset) an attribute carried by a node.
6281 * Returns the attribute pointer.
6282 */
6283xmlAttrPtr
6284xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
Daniel Veillard4855c8c2001-11-25 10:35:25 +00006285 xmlAttrPtr prop;
6286 xmlDocPtr doc;
Owen Taylor3473f882001-02-23 17:55:21 +00006287
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006288 if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE))
Owen Taylor3473f882001-02-23 17:55:21 +00006289 return(NULL);
6290 doc = node->doc;
Daniel Veillard4855c8c2001-11-25 10:35:25 +00006291 prop = node->properties;
Owen Taylor3473f882001-02-23 17:55:21 +00006292 while (prop != NULL) {
Daniel Veillard75bea542001-05-11 17:41:21 +00006293 if ((xmlStrEqual(prop->name, name)) &&
6294 (prop->ns == NULL)){
Daniel Veillard4855c8c2001-11-25 10:35:25 +00006295 xmlNodePtr oldprop = prop->children;
6296
Owen Taylor3473f882001-02-23 17:55:21 +00006297 prop->children = NULL;
6298 prop->last = NULL;
6299 if (value != NULL) {
6300 xmlChar *buffer;
6301 xmlNodePtr tmp;
6302
6303 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
6304 prop->children = xmlStringGetNodeList(node->doc, buffer);
6305 prop->last = NULL;
6306 prop->doc = doc;
6307 tmp = prop->children;
6308 while (tmp != NULL) {
6309 tmp->parent = (xmlNodePtr) prop;
6310 tmp->doc = doc;
6311 if (tmp->next == NULL)
6312 prop->last = tmp;
6313 tmp = tmp->next;
6314 }
6315 xmlFree(buffer);
Daniel Veillard75bea542001-05-11 17:41:21 +00006316 }
Daniel Veillard4855c8c2001-11-25 10:35:25 +00006317 if (oldprop != NULL)
6318 xmlFreeNodeList(oldprop);
Owen Taylor3473f882001-02-23 17:55:21 +00006319 return(prop);
6320 }
6321 prop = prop->next;
6322 }
6323 prop = xmlNewProp(node, name, value);
6324 return(prop);
6325}
6326
6327/**
6328 * xmlSetNsProp:
6329 * @node: the node
6330 * @ns: the namespace definition
6331 * @name: the attribute name
6332 * @value: the attribute value
6333 *
6334 * Set (or reset) an attribute carried by a node.
6335 * The ns structure must be in scope, this is not checked.
6336 *
6337 * Returns the attribute pointer.
6338 */
6339xmlAttrPtr
6340xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
6341 const xmlChar *value) {
6342 xmlAttrPtr prop;
6343
Daniel Veillardb6b36d32005-02-09 16:48:53 +00006344 if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE))
Owen Taylor3473f882001-02-23 17:55:21 +00006345 return(NULL);
6346
6347 if (ns == NULL)
6348 return(xmlSetProp(node, name, value));
6349 if (ns->href == NULL)
6350 return(NULL);
6351 prop = node->properties;
6352
6353 while (prop != NULL) {
6354 /*
6355 * One need to have
6356 * - same attribute names
6357 * - and the attribute carrying that namespace
Owen Taylor3473f882001-02-23 17:55:21 +00006358 */
6359 if ((xmlStrEqual(prop->name, name)) &&
Daniel Veillarda57c26e2002-08-01 12:52:24 +00006360 (prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))) {
Owen Taylor3473f882001-02-23 17:55:21 +00006361 if (prop->children != NULL)
6362 xmlFreeNodeList(prop->children);
6363 prop->children = NULL;
6364 prop->last = NULL;
6365 prop->ns = ns;
6366 if (value != NULL) {
6367 xmlChar *buffer;
6368 xmlNodePtr tmp;
6369
6370 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
6371 prop->children = xmlStringGetNodeList(node->doc, buffer);
6372 prop->last = NULL;
6373 tmp = prop->children;
6374 while (tmp != NULL) {
6375 tmp->parent = (xmlNodePtr) prop;
6376 if (tmp->next == NULL)
6377 prop->last = tmp;
6378 tmp = tmp->next;
6379 }
6380 xmlFree(buffer);
6381 }
6382 return(prop);
6383 }
6384 prop = prop->next;
6385 }
6386 prop = xmlNewNsProp(node, ns, name, value);
6387 return(prop);
6388}
6389
Daniel Veillard652327a2003-09-29 18:02:38 +00006390#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard75bea542001-05-11 17:41:21 +00006391
6392/**
Owen Taylor3473f882001-02-23 17:55:21 +00006393 * xmlNodeIsText:
6394 * @node: the node
6395 *
6396 * Is this node a Text node ?
6397 * Returns 1 yes, 0 no
6398 */
6399int
6400xmlNodeIsText(xmlNodePtr node) {
6401 if (node == NULL) return(0);
6402
6403 if (node->type == XML_TEXT_NODE) return(1);
6404 return(0);
6405}
6406
6407/**
6408 * xmlIsBlankNode:
6409 * @node: the node
6410 *
6411 * Checks whether this node is an empty or whitespace only
6412 * (and possibly ignorable) text-node.
6413 *
6414 * Returns 1 yes, 0 no
6415 */
6416int
6417xmlIsBlankNode(xmlNodePtr node) {
6418 const xmlChar *cur;
6419 if (node == NULL) return(0);
6420
Daniel Veillard7db37732001-07-12 01:20:08 +00006421 if ((node->type != XML_TEXT_NODE) &&
6422 (node->type != XML_CDATA_SECTION_NODE))
6423 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006424 if (node->content == NULL) return(1);
Owen Taylor3473f882001-02-23 17:55:21 +00006425 cur = node->content;
Owen Taylor3473f882001-02-23 17:55:21 +00006426 while (*cur != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00006427 if (!IS_BLANK_CH(*cur)) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006428 cur++;
6429 }
6430
6431 return(1);
6432}
6433
6434/**
6435 * xmlTextConcat:
6436 * @node: the node
6437 * @content: the content
Daniel Veillard60087f32001-10-10 09:45:09 +00006438 * @len: @content length
Owen Taylor3473f882001-02-23 17:55:21 +00006439 *
6440 * Concat the given string at the end of the existing node content
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006441 *
6442 * Returns -1 in case of error, 0 otherwise
Owen Taylor3473f882001-02-23 17:55:21 +00006443 */
6444
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006445int
Owen Taylor3473f882001-02-23 17:55:21 +00006446xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006447 if (node == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006448
6449 if ((node->type != XML_TEXT_NODE) &&
6450 (node->type != XML_CDATA_SECTION_NODE)) {
6451#ifdef DEBUG_TREE
6452 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006453 "xmlTextConcat: node is not text nor CDATA\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006454#endif
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006455 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006456 }
William M. Brack7762bb12004-01-04 14:49:01 +00006457 /* need to check if content is currently in the dictionary */
6458 if ((node->doc != NULL) && (node->doc->dict != NULL) &&
6459 xmlDictOwns(node->doc->dict, node->content)) {
6460 node->content = xmlStrncatNew(node->content, content, len);
6461 } else {
6462 node->content = xmlStrncat(node->content, content, len);
6463 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006464 if (node->content == NULL)
6465 return(-1);
6466 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006467}
6468
6469/************************************************************************
6470 * *
6471 * Output : to a FILE or in memory *
6472 * *
6473 ************************************************************************/
6474
Owen Taylor3473f882001-02-23 17:55:21 +00006475/**
6476 * xmlBufferCreate:
6477 *
6478 * routine to create an XML buffer.
6479 * returns the new structure.
6480 */
6481xmlBufferPtr
6482xmlBufferCreate(void) {
6483 xmlBufferPtr ret;
6484
6485 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6486 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006487 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006488 return(NULL);
6489 }
6490 ret->use = 0;
Daniel Veillarde356c282001-03-10 12:32:04 +00006491 ret->size = xmlDefaultBufferSize;
Owen Taylor3473f882001-02-23 17:55:21 +00006492 ret->alloc = xmlBufferAllocScheme;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00006493 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00006494 if (ret->content == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006495 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006496 xmlFree(ret);
6497 return(NULL);
6498 }
6499 ret->content[0] = 0;
6500 return(ret);
6501}
6502
6503/**
6504 * xmlBufferCreateSize:
6505 * @size: initial size of buffer
6506 *
6507 * routine to create an XML buffer.
6508 * returns the new structure.
6509 */
6510xmlBufferPtr
6511xmlBufferCreateSize(size_t size) {
6512 xmlBufferPtr ret;
6513
6514 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6515 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006516 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006517 return(NULL);
6518 }
6519 ret->use = 0;
6520 ret->alloc = xmlBufferAllocScheme;
6521 ret->size = (size ? size+2 : 0); /* +1 for ending null */
6522 if (ret->size){
Daniel Veillard3c908dc2003-04-19 00:07:51 +00006523 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00006524 if (ret->content == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006525 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006526 xmlFree(ret);
6527 return(NULL);
6528 }
6529 ret->content[0] = 0;
6530 } else
6531 ret->content = NULL;
6532 return(ret);
6533}
6534
6535/**
Daniel Veillard53350552003-09-18 13:35:51 +00006536 * xmlBufferCreateStatic:
6537 * @mem: the memory area
6538 * @size: the size in byte
6539 *
MST 2003 John Flecka0e7e932003-12-19 03:13:47 +00006540 * routine to create an XML buffer from an immutable memory area.
6541 * The area won't be modified nor copied, and is expected to be
Daniel Veillard53350552003-09-18 13:35:51 +00006542 * present until the end of the buffer lifetime.
6543 *
6544 * returns the new structure.
6545 */
6546xmlBufferPtr
6547xmlBufferCreateStatic(void *mem, size_t size) {
6548 xmlBufferPtr ret;
6549
6550 if ((mem == NULL) || (size == 0))
6551 return(NULL);
6552
6553 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6554 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006555 xmlTreeErrMemory("creating buffer");
Daniel Veillard53350552003-09-18 13:35:51 +00006556 return(NULL);
6557 }
6558 ret->use = size;
6559 ret->size = size;
6560 ret->alloc = XML_BUFFER_ALLOC_IMMUTABLE;
6561 ret->content = (xmlChar *) mem;
6562 return(ret);
6563}
6564
6565/**
Owen Taylor3473f882001-02-23 17:55:21 +00006566 * xmlBufferSetAllocationScheme:
Daniel Veillardbd9afb52002-09-25 22:25:35 +00006567 * @buf: the buffer to tune
Owen Taylor3473f882001-02-23 17:55:21 +00006568 * @scheme: allocation scheme to use
6569 *
6570 * Sets the allocation scheme for this buffer
6571 */
6572void
6573xmlBufferSetAllocationScheme(xmlBufferPtr buf,
6574 xmlBufferAllocationScheme scheme) {
6575 if (buf == NULL) {
6576#ifdef DEBUG_BUFFER
6577 xmlGenericError(xmlGenericErrorContext,
6578 "xmlBufferSetAllocationScheme: buf == NULL\n");
6579#endif
6580 return;
6581 }
Daniel Veillard53350552003-09-18 13:35:51 +00006582 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00006583
6584 buf->alloc = scheme;
6585}
6586
6587/**
6588 * xmlBufferFree:
6589 * @buf: the buffer to free
6590 *
Daniel Veillard9d06d302002-01-22 18:15:52 +00006591 * Frees an XML buffer. It frees both the content and the structure which
6592 * encapsulate it.
Owen Taylor3473f882001-02-23 17:55:21 +00006593 */
6594void
6595xmlBufferFree(xmlBufferPtr buf) {
6596 if (buf == NULL) {
6597#ifdef DEBUG_BUFFER
6598 xmlGenericError(xmlGenericErrorContext,
6599 "xmlBufferFree: buf == NULL\n");
6600#endif
6601 return;
6602 }
Daniel Veillard53350552003-09-18 13:35:51 +00006603
6604 if ((buf->content != NULL) &&
6605 (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006606 xmlFree(buf->content);
6607 }
Owen Taylor3473f882001-02-23 17:55:21 +00006608 xmlFree(buf);
6609}
6610
6611/**
6612 * xmlBufferEmpty:
6613 * @buf: the buffer
6614 *
6615 * empty a buffer.
6616 */
6617void
6618xmlBufferEmpty(xmlBufferPtr buf) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00006619 if (buf == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +00006620 if (buf->content == NULL) return;
6621 buf->use = 0;
Daniel Veillard53350552003-09-18 13:35:51 +00006622 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00006623 buf->content = BAD_CAST "";
Daniel Veillard53350552003-09-18 13:35:51 +00006624 } else {
6625 memset(buf->content, 0, buf->size);
6626 }
Owen Taylor3473f882001-02-23 17:55:21 +00006627}
6628
6629/**
6630 * xmlBufferShrink:
6631 * @buf: the buffer to dump
6632 * @len: the number of xmlChar to remove
6633 *
6634 * Remove the beginning of an XML buffer.
6635 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006636 * Returns the number of #xmlChar removed, or -1 in case of failure.
Owen Taylor3473f882001-02-23 17:55:21 +00006637 */
6638int
6639xmlBufferShrink(xmlBufferPtr buf, unsigned int len) {
Daniel Veillard3d97e662004-11-04 10:49:00 +00006640 if (buf == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006641 if (len == 0) return(0);
6642 if (len > buf->use) return(-1);
6643
6644 buf->use -= len;
Daniel Veillard53350552003-09-18 13:35:51 +00006645 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) {
6646 buf->content += len;
6647 } else {
6648 memmove(buf->content, &buf->content[len], buf->use * sizeof(xmlChar));
6649 buf->content[buf->use] = 0;
6650 }
Owen Taylor3473f882001-02-23 17:55:21 +00006651 return(len);
6652}
6653
6654/**
6655 * xmlBufferGrow:
6656 * @buf: the buffer
6657 * @len: the minimum free size to allocate
6658 *
6659 * Grow the available space of an XML buffer.
6660 *
6661 * Returns the new available space or -1 in case of error
6662 */
6663int
6664xmlBufferGrow(xmlBufferPtr buf, unsigned int len) {
6665 int size;
6666 xmlChar *newbuf;
6667
Daniel Veillard3d97e662004-11-04 10:49:00 +00006668 if (buf == NULL) return(-1);
6669
Daniel Veillard53350552003-09-18 13:35:51 +00006670 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006671 if (len + buf->use < buf->size) return(0);
6672
William M. Brack30fe43f2004-07-26 18:00:58 +00006673/*
6674 * Windows has a BIG problem on realloc timing, so we try to double
6675 * the buffer size (if that's enough) (bug 146697)
6676 */
6677#ifdef WIN32
6678 if (buf->size > len)
6679 size = buf->size * 2;
6680 else
6681 size = buf->use + len + 100;
6682#else
Owen Taylor3473f882001-02-23 17:55:21 +00006683 size = buf->use + len + 100;
William M. Brack30fe43f2004-07-26 18:00:58 +00006684#endif
Owen Taylor3473f882001-02-23 17:55:21 +00006685
6686 newbuf = (xmlChar *) xmlRealloc(buf->content, size);
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006687 if (newbuf == NULL) {
6688 xmlTreeErrMemory("growing buffer");
6689 return(-1);
6690 }
Owen Taylor3473f882001-02-23 17:55:21 +00006691 buf->content = newbuf;
6692 buf->size = size;
6693 return(buf->size - buf->use);
6694}
6695
6696/**
6697 * xmlBufferDump:
6698 * @file: the file output
6699 * @buf: the buffer to dump
6700 *
6701 * Dumps an XML buffer to a FILE *.
Daniel Veillardd1640922001-12-17 15:30:10 +00006702 * Returns the number of #xmlChar written
Owen Taylor3473f882001-02-23 17:55:21 +00006703 */
6704int
6705xmlBufferDump(FILE *file, xmlBufferPtr buf) {
6706 int ret;
6707
6708 if (buf == NULL) {
6709#ifdef DEBUG_BUFFER
6710 xmlGenericError(xmlGenericErrorContext,
6711 "xmlBufferDump: buf == NULL\n");
6712#endif
6713 return(0);
6714 }
6715 if (buf->content == NULL) {
6716#ifdef DEBUG_BUFFER
6717 xmlGenericError(xmlGenericErrorContext,
6718 "xmlBufferDump: buf->content == NULL\n");
6719#endif
6720 return(0);
6721 }
Daniel Veillardcd337f02001-11-22 18:20:37 +00006722 if (file == NULL)
6723 file = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00006724 ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
6725 return(ret);
6726}
6727
6728/**
6729 * xmlBufferContent:
6730 * @buf: the buffer
6731 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00006732 * Function to extract the content of a buffer
6733 *
Owen Taylor3473f882001-02-23 17:55:21 +00006734 * Returns the internal content
6735 */
6736
Daniel Veillard5e2dace2001-07-18 19:30:27 +00006737const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00006738xmlBufferContent(const xmlBufferPtr buf)
6739{
6740 if(!buf)
6741 return NULL;
6742
6743 return buf->content;
6744}
6745
6746/**
6747 * xmlBufferLength:
6748 * @buf: the buffer
6749 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00006750 * Function to get the length of a buffer
6751 *
Owen Taylor3473f882001-02-23 17:55:21 +00006752 * Returns the length of data in the internal content
6753 */
6754
6755int
6756xmlBufferLength(const xmlBufferPtr buf)
6757{
6758 if(!buf)
6759 return 0;
6760
6761 return buf->use;
6762}
6763
6764/**
6765 * xmlBufferResize:
6766 * @buf: the buffer to resize
6767 * @size: the desired size
6768 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006769 * Resize a buffer to accommodate minimum size of @size.
Owen Taylor3473f882001-02-23 17:55:21 +00006770 *
6771 * Returns 0 in case of problems, 1 otherwise
6772 */
6773int
6774xmlBufferResize(xmlBufferPtr buf, unsigned int size)
6775{
6776 unsigned int newSize;
6777 xmlChar* rebuf = NULL;
6778
Daniel Veillardd005b9e2004-11-03 17:07:05 +00006779 if (buf == NULL)
6780 return(0);
6781
Daniel Veillard53350552003-09-18 13:35:51 +00006782 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
6783
Owen Taylor3473f882001-02-23 17:55:21 +00006784 /* Don't resize if we don't have to */
6785 if (size < buf->size)
6786 return 1;
6787
6788 /* figure out new size */
6789 switch (buf->alloc){
6790 case XML_BUFFER_ALLOC_DOUBLEIT:
Daniel Veillardbf629492004-04-20 22:20:59 +00006791 /*take care of empty case*/
6792 newSize = (buf->size ? buf->size*2 : size + 10);
Owen Taylor3473f882001-02-23 17:55:21 +00006793 while (size > newSize) newSize *= 2;
6794 break;
6795 case XML_BUFFER_ALLOC_EXACT:
6796 newSize = size+10;
6797 break;
6798 default:
6799 newSize = size+10;
6800 break;
6801 }
6802
6803 if (buf->content == NULL)
Daniel Veillard3c908dc2003-04-19 00:07:51 +00006804 rebuf = (xmlChar *) xmlMallocAtomic(newSize * sizeof(xmlChar));
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006805 else if (buf->size - buf->use < 100) {
Owen Taylor3473f882001-02-23 17:55:21 +00006806 rebuf = (xmlChar *) xmlRealloc(buf->content,
6807 newSize * sizeof(xmlChar));
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006808 } else {
6809 /*
6810 * if we are reallocating a buffer far from being full, it's
6811 * better to make a new allocation and copy only the used range
6812 * and free the old one.
6813 */
6814 rebuf = (xmlChar *) xmlMallocAtomic(newSize * sizeof(xmlChar));
6815 if (rebuf != NULL) {
6816 memcpy(rebuf, buf->content, buf->use);
6817 xmlFree(buf->content);
William M. Brack42331a92004-07-29 07:07:16 +00006818 rebuf[buf->use] = 0;
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006819 }
6820 }
Owen Taylor3473f882001-02-23 17:55:21 +00006821 if (rebuf == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006822 xmlTreeErrMemory("growing buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006823 return 0;
6824 }
6825 buf->content = rebuf;
6826 buf->size = newSize;
6827
6828 return 1;
6829}
6830
6831/**
6832 * xmlBufferAdd:
6833 * @buf: the buffer to dump
Daniel Veillardd1640922001-12-17 15:30:10 +00006834 * @str: the #xmlChar string
6835 * @len: the number of #xmlChar to add
Owen Taylor3473f882001-02-23 17:55:21 +00006836 *
Daniel Veillard60087f32001-10-10 09:45:09 +00006837 * Add a string range to an XML buffer. if len == -1, the length of
Owen Taylor3473f882001-02-23 17:55:21 +00006838 * str is recomputed.
William M. Bracka3215c72004-07-31 16:24:01 +00006839 *
6840 * Returns 0 successful, a positive error code number otherwise
6841 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00006842 */
William M. Bracka3215c72004-07-31 16:24:01 +00006843int
Owen Taylor3473f882001-02-23 17:55:21 +00006844xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) {
6845 unsigned int needSize;
6846
Daniel Veillardd005b9e2004-11-03 17:07:05 +00006847 if ((str == NULL) || (buf == NULL)) {
William M. Bracka3215c72004-07-31 16:24:01 +00006848 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006849 }
William M. Bracka3215c72004-07-31 16:24:01 +00006850 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006851 if (len < -1) {
6852#ifdef DEBUG_BUFFER
6853 xmlGenericError(xmlGenericErrorContext,
6854 "xmlBufferAdd: len < 0\n");
6855#endif
William M. Bracka3215c72004-07-31 16:24:01 +00006856 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006857 }
William M. Bracka3215c72004-07-31 16:24:01 +00006858 if (len == 0) return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006859
6860 if (len < 0)
6861 len = xmlStrlen(str);
6862
William M. Bracka3215c72004-07-31 16:24:01 +00006863 if (len <= 0) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006864
6865 needSize = buf->use + len + 2;
6866 if (needSize > buf->size){
6867 if (!xmlBufferResize(buf, needSize)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006868 xmlTreeErrMemory("growing buffer");
William M. Bracka3215c72004-07-31 16:24:01 +00006869 return XML_ERR_NO_MEMORY;
Owen Taylor3473f882001-02-23 17:55:21 +00006870 }
6871 }
6872
6873 memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
6874 buf->use += len;
6875 buf->content[buf->use] = 0;
William M. Bracka3215c72004-07-31 16:24:01 +00006876 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006877}
6878
6879/**
6880 * xmlBufferAddHead:
6881 * @buf: the buffer
Daniel Veillardd1640922001-12-17 15:30:10 +00006882 * @str: the #xmlChar string
6883 * @len: the number of #xmlChar to add
Owen Taylor3473f882001-02-23 17:55:21 +00006884 *
6885 * Add a string range to the beginning of an XML buffer.
Daniel Veillard60087f32001-10-10 09:45:09 +00006886 * if len == -1, the length of @str is recomputed.
William M. Bracka3215c72004-07-31 16:24:01 +00006887 *
6888 * Returns 0 successful, a positive error code number otherwise
6889 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00006890 */
William M. Bracka3215c72004-07-31 16:24:01 +00006891int
Owen Taylor3473f882001-02-23 17:55:21 +00006892xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) {
6893 unsigned int needSize;
6894
Daniel Veillardd005b9e2004-11-03 17:07:05 +00006895 if (buf == NULL)
6896 return(-1);
William M. Bracka3215c72004-07-31 16:24:01 +00006897 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006898 if (str == NULL) {
6899#ifdef DEBUG_BUFFER
6900 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006901 "xmlBufferAddHead: str == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006902#endif
William M. Bracka3215c72004-07-31 16:24:01 +00006903 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006904 }
6905 if (len < -1) {
6906#ifdef DEBUG_BUFFER
6907 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006908 "xmlBufferAddHead: len < 0\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006909#endif
William M. Bracka3215c72004-07-31 16:24:01 +00006910 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006911 }
William M. Bracka3215c72004-07-31 16:24:01 +00006912 if (len == 0) return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006913
6914 if (len < 0)
6915 len = xmlStrlen(str);
6916
William M. Bracka3215c72004-07-31 16:24:01 +00006917 if (len <= 0) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006918
6919 needSize = buf->use + len + 2;
6920 if (needSize > buf->size){
6921 if (!xmlBufferResize(buf, needSize)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006922 xmlTreeErrMemory("growing buffer");
William M. Bracka3215c72004-07-31 16:24:01 +00006923 return XML_ERR_NO_MEMORY;
Owen Taylor3473f882001-02-23 17:55:21 +00006924 }
6925 }
6926
6927 memmove(&buf->content[len], &buf->content[0], buf->use * sizeof(xmlChar));
6928 memmove(&buf->content[0], str, len * sizeof(xmlChar));
6929 buf->use += len;
6930 buf->content[buf->use] = 0;
William M. Bracka3215c72004-07-31 16:24:01 +00006931 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006932}
6933
6934/**
6935 * xmlBufferCat:
William M. Bracka3215c72004-07-31 16:24:01 +00006936 * @buf: the buffer to add to
Daniel Veillardd1640922001-12-17 15:30:10 +00006937 * @str: the #xmlChar string
Owen Taylor3473f882001-02-23 17:55:21 +00006938 *
6939 * Append a zero terminated string to an XML buffer.
William M. Bracka3215c72004-07-31 16:24:01 +00006940 *
6941 * Returns 0 successful, a positive error code number otherwise
6942 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00006943 */
William M. Bracka3215c72004-07-31 16:24:01 +00006944int
Owen Taylor3473f882001-02-23 17:55:21 +00006945xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00006946 if (buf == NULL)
6947 return(-1);
William M. Bracka3215c72004-07-31 16:24:01 +00006948 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
6949 if (str == NULL) return -1;
6950 return xmlBufferAdd(buf, str, -1);
Owen Taylor3473f882001-02-23 17:55:21 +00006951}
6952
6953/**
6954 * xmlBufferCCat:
6955 * @buf: the buffer to dump
6956 * @str: the C char string
6957 *
6958 * Append a zero terminated C string to an XML buffer.
William M. Bracka3215c72004-07-31 16:24:01 +00006959 *
6960 * Returns 0 successful, a positive error code number otherwise
6961 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00006962 */
William M. Bracka3215c72004-07-31 16:24:01 +00006963int
Owen Taylor3473f882001-02-23 17:55:21 +00006964xmlBufferCCat(xmlBufferPtr buf, const char *str) {
6965 const char *cur;
6966
Daniel Veillardd005b9e2004-11-03 17:07:05 +00006967 if (buf == NULL)
6968 return(-1);
William M. Bracka3215c72004-07-31 16:24:01 +00006969 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006970 if (str == NULL) {
6971#ifdef DEBUG_BUFFER
6972 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006973 "xmlBufferCCat: str == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006974#endif
William M. Bracka3215c72004-07-31 16:24:01 +00006975 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006976 }
6977 for (cur = str;*cur != 0;cur++) {
6978 if (buf->use + 10 >= buf->size) {
6979 if (!xmlBufferResize(buf, buf->use+10)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006980 xmlTreeErrMemory("growing buffer");
William M. Bracka3215c72004-07-31 16:24:01 +00006981 return XML_ERR_NO_MEMORY;
Owen Taylor3473f882001-02-23 17:55:21 +00006982 }
6983 }
6984 buf->content[buf->use++] = *cur;
6985 }
6986 buf->content[buf->use] = 0;
William M. Bracka3215c72004-07-31 16:24:01 +00006987 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006988}
6989
6990/**
6991 * xmlBufferWriteCHAR:
6992 * @buf: the XML buffer
6993 * @string: the string to add
6994 *
6995 * routine which manages and grows an output buffer. This one adds
6996 * xmlChars at the end of the buffer.
6997 */
6998void
Daniel Veillard53350552003-09-18 13:35:51 +00006999xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007000 if (buf == NULL)
7001 return;
Daniel Veillard53350552003-09-18 13:35:51 +00007002 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00007003 xmlBufferCat(buf, string);
7004}
7005
7006/**
7007 * xmlBufferWriteChar:
7008 * @buf: the XML buffer output
7009 * @string: the string to add
7010 *
7011 * routine which manage and grows an output buffer. This one add
7012 * C chars at the end of the array.
7013 */
7014void
7015xmlBufferWriteChar(xmlBufferPtr buf, const char *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 xmlBufferCCat(buf, string);
7020}
7021
7022
7023/**
7024 * xmlBufferWriteQuotedString:
7025 * @buf: the XML buffer output
7026 * @string: the string to add
7027 *
7028 * routine which manage and grows an output buffer. This one writes
Daniel Veillardd1640922001-12-17 15:30:10 +00007029 * a quoted or double quoted #xmlChar string, checking first if it holds
Owen Taylor3473f882001-02-23 17:55:21 +00007030 * quote or double-quotes internally
7031 */
7032void
7033xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) {
Daniel Veillard39057f42003-08-04 01:33:43 +00007034 const xmlChar *cur, *base;
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007035 if (buf == NULL)
7036 return;
Daniel Veillard53350552003-09-18 13:35:51 +00007037 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Daniel Veillard39057f42003-08-04 01:33:43 +00007038 if (xmlStrchr(string, '\"')) {
Daniel Veillard20aa0fb2003-08-04 19:43:15 +00007039 if (xmlStrchr(string, '\'')) {
Owen Taylor3473f882001-02-23 17:55:21 +00007040#ifdef DEBUG_BUFFER
7041 xmlGenericError(xmlGenericErrorContext,
7042 "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n");
7043#endif
Daniel Veillard39057f42003-08-04 01:33:43 +00007044 xmlBufferCCat(buf, "\"");
7045 base = cur = string;
7046 while(*cur != 0){
7047 if(*cur == '"'){
7048 if (base != cur)
7049 xmlBufferAdd(buf, base, cur - base);
7050 xmlBufferAdd(buf, BAD_CAST "&quot;", 6);
7051 cur++;
7052 base = cur;
7053 }
7054 else {
7055 cur++;
7056 }
7057 }
7058 if (base != cur)
7059 xmlBufferAdd(buf, base, cur - base);
7060 xmlBufferCCat(buf, "\"");
Owen Taylor3473f882001-02-23 17:55:21 +00007061 }
Daniel Veillard39057f42003-08-04 01:33:43 +00007062 else{
7063 xmlBufferCCat(buf, "\'");
7064 xmlBufferCat(buf, string);
7065 xmlBufferCCat(buf, "\'");
7066 }
Owen Taylor3473f882001-02-23 17:55:21 +00007067 } else {
7068 xmlBufferCCat(buf, "\"");
7069 xmlBufferCat(buf, string);
7070 xmlBufferCCat(buf, "\"");
7071 }
7072}
7073
7074
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007075/**
7076 * xmlGetDocCompressMode:
7077 * @doc: the document
7078 *
7079 * get the compression ratio for a document, ZLIB based
7080 * Returns 0 (uncompressed) to 9 (max compression)
7081 */
7082int
7083xmlGetDocCompressMode (xmlDocPtr doc) {
7084 if (doc == NULL) return(-1);
7085 return(doc->compression);
7086}
7087
7088/**
7089 * xmlSetDocCompressMode:
7090 * @doc: the document
7091 * @mode: the compression ratio
7092 *
7093 * set the compression ratio for a document, ZLIB based
7094 * Correct values: 0 (uncompressed) to 9 (max compression)
7095 */
7096void
7097xmlSetDocCompressMode (xmlDocPtr doc, int mode) {
7098 if (doc == NULL) return;
7099 if (mode < 0) doc->compression = 0;
7100 else if (mode > 9) doc->compression = 9;
7101 else doc->compression = mode;
7102}
7103
7104/**
7105 * xmlGetCompressMode:
7106 *
7107 * get the default compression mode used, ZLIB based.
7108 * Returns 0 (uncompressed) to 9 (max compression)
7109 */
7110int
7111xmlGetCompressMode(void)
7112{
7113 return (xmlCompressMode);
7114}
7115
7116/**
7117 * xmlSetCompressMode:
7118 * @mode: the compression ratio
7119 *
7120 * set the default compression mode used, ZLIB based
7121 * Correct values: 0 (uncompressed) to 9 (max compression)
7122 */
7123void
7124xmlSetCompressMode(int mode) {
7125 if (mode < 0) xmlCompressMode = 0;
7126 else if (mode > 9) xmlCompressMode = 9;
7127 else xmlCompressMode = mode;
7128}
7129
Kasimier T. Buchcik4d9c9482005-06-27 15:04:46 +00007130/*
7131* xmlDOMWrapNewCtxt:
7132*
7133* Allocates and initializes a new DOM-wrapper context.
7134*
7135* Returns the xmlDOMWrapCtxtPtr or NULL in case of an internal errror.
7136*/
7137xmlDOMWrapCtxtPtr
7138xmlDOMWrapNewCtxt(void)
7139{
7140 xmlDOMWrapCtxtPtr ret;
7141
7142 ret = xmlMalloc(sizeof(xmlDOMWrapCtxt));
7143 if (ret == NULL) {
7144 xmlTreeErrMemory("allocating DOM-wrapper context");
7145 return (NULL);
7146 }
7147 memset(ret, 0, sizeof(xmlDOMWrapCtxt));
7148 return (ret);
7149}
7150
7151/*
7152* xmlDOMWrapFreeCtxt:
7153* @ctxt: the DOM-wrapper context
7154*
7155* Frees the DOM-wrapper context.
7156*/
7157void
7158xmlDOMWrapFreeCtxt(xmlDOMWrapCtxtPtr ctxt)
7159{
7160 if (ctxt == NULL)
7161 return;
7162 xmlFree(ctxt);
7163}
7164
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007165#define XML_TREE_NSMAP_PARENT -1
7166#define XML_TREE_NSMAP_XML -2
7167#define XML_TREE_NSMAP_DOC -3
7168#define XML_TREE_NSMAP_CUSTOM -4
7169
7170typedef struct xmlNsMapItem *xmlNsMapItemPtr;
7171struct xmlNsMapItem {
7172 xmlNsMapItemPtr next;
7173 xmlNsMapItemPtr prev;
7174 xmlNsPtr oldNs; /* old ns decl reference */
7175 xmlNsPtr newNs; /* new ns decl reference */
7176 int shadowDepth; /* Shadowed at this depth */
7177 /*
7178 * depth:
7179 * >= 0 == @node's ns-decls
7180 * -1 == @parent's ns-decls
7181 * -2 == @parent's out-of-scope ns-decls
7182 * -3 == the doc->oldNs XML ns-decl
7183 * -4 == the doc->oldNs storage ns-decls
7184 */
7185 int depth;
7186};
7187
7188/*
7189* xmlTreeAddNsMapItem:
7190* @map: the ns-map
7191* @cur: the current map entry to append a new entry to
7192* @oldNs: the old ns-struct
7193* @newNs: the new ns-struct
7194* @depth: depth and ns-kind information
7195*
7196* Frees the ns-map
7197*/
7198static xmlNsMapItemPtr
7199xmlDOMWrapNSNormAddNsMapItem(xmlNsMapItemPtr *map,
7200 xmlNsMapItemPtr *cur,
7201 xmlNsPtr oldNs,
7202 xmlNsPtr newNs,
7203 int depth)
7204{
7205 xmlNsMapItemPtr ret;
7206
7207 if ((cur != NULL) && (*cur != NULL) && ((*cur)->next != NULL)) {
7208 /*
7209 * Reuse.
7210 */
7211 ret = (*cur)->next;
7212 *cur = ret;
7213 } else {
7214 ret = (xmlNsMapItemPtr) xmlMalloc(sizeof(struct xmlNsMapItem));
7215 if (ret == NULL) {
7216 xmlTreeErrMemory("allocating namespace map item");
7217 return (NULL);
7218 }
7219 memset(ret, 0, sizeof(struct xmlNsMapItem));
7220 if (*map == NULL) {
7221 /*
7222 * First ever.
7223 */
7224 *map = ret;
7225 ret->prev = ret;
7226 if (cur != NULL)
7227 *cur = ret;
7228 } else {
7229 if (cur) {
7230 /*
7231 * Append.
7232 */
7233 (*cur)->next = ret;
7234 ret->prev = *cur;
7235 *cur = ret;
7236 } else {
7237 /*
7238 * Set on first position.
7239 */
7240 ret->next = (*map);
7241 ret->prev = (*map)->prev;
7242 (*map)->prev = ret;
7243 *map = ret;
7244 }
7245 }
7246 }
7247 ret->oldNs = oldNs;
7248 ret->newNs = newNs;
7249 ret->shadowDepth = -1;
7250 ret->depth = depth;
7251 return (ret);
7252}
7253
7254/*
7255* xmlTreeFreeNsMap:
7256* @map: the ns-map
7257*
7258* Frees the ns-map
7259*/
7260static void
7261xmlDOMWrapNSNormFreeNsMap(xmlNsMapItemPtr map)
7262{
7263 xmlNsMapItemPtr mi = map, miprev;
7264
7265 while (mi != NULL) {
7266 miprev = mi;
7267 mi = mi->next;
7268 xmlFree(miprev);
7269 }
7270}
7271
7272/*
7273* xmlTreeEnsureXMLDecl:
7274* @doc: the doc
7275*
7276* Ensures that there is an XML namespace declaration on the doc.
7277*
7278* Returns the XML ns-struct or NULL on API and internal errors.
7279*/
7280static xmlNsPtr
7281xmlTreeEnsureXMLDecl(xmlDocPtr doc)
7282{
7283 if (doc == NULL)
7284 return (NULL);
7285 if (doc->oldNs != NULL)
7286 return (doc->oldNs);
7287 {
7288 xmlNsPtr ns;
7289 ns = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
7290 if (ns == NULL) {
7291 xmlTreeErrMemory(
7292 "allocating the XML namespace");
7293 return (NULL);
7294 }
7295 memset(ns, 0, sizeof(xmlNs));
7296 ns->type = XML_LOCAL_NAMESPACE;
7297 ns->href = xmlStrdup(XML_XML_NAMESPACE);
7298 ns->prefix = xmlStrdup((const xmlChar *)"xml");
7299 doc->oldNs = ns;
7300 return (ns);
7301 }
7302}
7303
7304/*
7305* xmlDOMWrapStoreNs:
7306* @doc: the doc
7307* @nsName: the namespace name
7308* @prefix: the prefix
7309*
7310* Creates or reuses an xmlNs struct on doc->oldNs with
7311* the given prefix and namespace name.
7312*
7313* Returns the aquired ns struct or NULL in case of an API
7314* or internal error.
7315*/
7316static xmlNsPtr
7317xmlDOMWrapStoreNs(xmlDocPtr doc,
7318 const xmlChar *nsName,
7319 const xmlChar *prefix)
7320{
7321 xmlNsPtr ns;
7322
7323 if (doc == NULL)
7324 return (NULL);
7325 ns = xmlTreeEnsureXMLDecl(doc);
7326 if (ns == NULL)
7327 return (NULL);
7328 if (ns->next != NULL) {
7329 /* Reuse. */
7330 ns = ns->next;
7331 while (ns != NULL) {
7332 if (((ns->prefix == prefix) ||
7333 xmlStrEqual(ns->prefix, prefix)) &&
7334 xmlStrEqual(ns->href, nsName)) {
7335 return (ns);
7336 }
7337 if (ns->next == NULL)
7338 break;
7339 ns = ns->next;
7340 }
7341 }
7342 /* Create. */
7343 ns->next = xmlNewNs(NULL, nsName, prefix);
7344 return (ns->next);
7345}
7346
7347/*
7348* xmlTreeLookupNsListByPrefix:
7349* @nsList: a list of ns-structs
7350* @prefix: the searched prefix
7351*
7352* Searches for a ns-decl with the given prefix in @nsList.
7353*
7354* Returns the ns-decl if found, NULL if not found and on
7355* API errors.
7356*/
7357static xmlNsPtr
7358xmlTreeNSListLookupByPrefix(xmlNsPtr nsList, const xmlChar *prefix)
7359{
7360 if (nsList == NULL)
7361 return (NULL);
7362 {
7363 xmlNsPtr ns;
7364 ns = nsList;
7365 do {
7366 if ((prefix == ns->prefix) ||
7367 xmlStrEqual(prefix, ns->prefix)) {
7368 return (ns);
7369 }
7370 ns = ns->next;
7371 } while (ns != NULL);
7372 }
7373 return (NULL);
7374}
7375
7376/*
7377*
7378* xmlTreeGetInScopeNamespaces:
7379* @map: the namespace map
7380* @node: the node to start with
7381*
7382* Puts in-scope namespaces into the ns-map.
7383*
7384* Returns 0 on success, -1 on API or internal errors.
7385*/
7386static int
7387xmlDOMWrapNSNormGatherInScopeNs(xmlNsMapItemPtr *map,
7388 xmlNodePtr node)
7389{
7390 xmlNodePtr cur;
7391 xmlNsPtr ns;
7392 xmlNsMapItemPtr mi;
7393 int shadowed;
7394
7395 if ((map == NULL) || (*map != NULL))
7396 return (-1);
7397 /*
7398 * Get in-scope ns-decls of @parent.
7399 */
7400 cur = node;
7401 while ((cur != NULL) && (cur != (xmlNodePtr) cur->doc)) {
7402 if (cur->type == XML_ELEMENT_NODE) {
7403 if (cur->nsDef != NULL) {
7404 ns = cur->nsDef;
7405 do {
7406 shadowed = 0;
7407 if (*map != NULL) {
7408 /*
7409 * Skip shadowed prefixes.
7410 */
7411 for (mi = *map; mi != NULL; mi = mi->next) {
7412 if ((ns->prefix == mi->newNs->prefix) ||
7413 xmlStrEqual(ns->prefix, mi->newNs->prefix)) {
7414 shadowed = 1;
7415 break;
7416 }
7417 }
7418 }
7419 /*
7420 * Insert mapping.
7421 */
7422 mi = xmlDOMWrapNSNormAddNsMapItem(map, NULL, NULL,
7423 ns, XML_TREE_NSMAP_PARENT);
7424 if (mi == NULL)
7425 return (-1);
7426 if (shadowed)
7427 mi->shadowDepth = 0;
7428 ns = ns->next;
7429 } while (ns != NULL);
7430 }
7431 }
7432 cur = cur->parent;
7433 }
7434 return (0);
7435}
7436
7437/*
7438* XML_TREE_ADOPT_STR: If we have a dest-dict, put @str in the dict;
7439* otherwise copy it, when it was in the source-dict.
7440*/
7441#define XML_TREE_ADOPT_STR(str) \
7442 if (adoptStr && (str != NULL)) { \
7443 if (destDoc->dict) { \
7444 str = xmlDictLookup(destDoc->dict, str, -1); \
7445 } else if ((sourceDoc) && (sourceDoc->dict) && \
7446 xmlDictOwns(sourceDoc->dict, str)) { \
7447 str = BAD_CAST xmlStrdup(str); \
7448 } \
7449 }
7450
7451/*
7452* XML_TREE_ADOPT_STR_2: If @str was in the source-dict, then
7453* put it in dest-dict or copy it.
7454*/
7455#define XML_TREE_ADOPT_STR_2(str) \
7456 if (adoptStr && (str != NULL) && (sourceDoc != NULL) && \
7457 (sourceDoc->dict != NULL) && \
7458 xmlDictOwns(sourceDoc->dict, cur->content)) { \
7459 if (destDoc->dict) \
7460 cur->content = (xmlChar *) \
7461 xmlDictLookup(destDoc->dict, cur->content, -1); \
7462 else \
7463 cur->content = xmlStrdup(BAD_CAST cur->content); \
7464 }
7465
7466/*
7467* xmlDOMWrapNSNormAddNsMapItem2:
7468*
7469* For internal use. Adds a ns-decl mapping.
7470*
7471* Returns 0 on success, -1 on internal errors.
7472*/
7473static int
7474xmlDOMWrapNSNormAddNsMapItem2(xmlNsPtr **list, int *size, int *number,
7475 xmlNsPtr oldNs, xmlNsPtr newNs)
7476{
7477 if (*list == NULL) {
7478 *list = (xmlNsPtr *) xmlMalloc(6 * sizeof(xmlNsPtr));
7479 if (*list == NULL) {
7480 xmlTreeErrMemory("alloc ns map item");
7481 return(-1);
7482 }
7483 *size = 3;
7484 *number = 0;
7485 } else if ((*number) >= (*size)) {
7486 *size *= 2;
7487 *list = (xmlNsPtr *) xmlRealloc(*list,
7488 (*size) * 2 * sizeof(xmlNsPtr));
7489 if (*list == NULL) {
7490 xmlTreeErrMemory("realloc ns map item");
7491 return(-1);
7492 }
7493 }
7494 (*list)[2 * (*number)] = oldNs;
7495 (*list)[2 * (*number) +1] = newNs;
7496 (*number)++;
7497 return (0);
7498}
7499
7500/*
7501* xmlDOMWrapRemoveNode:
7502*
7503* @doc: the doc
7504* @node: the node to be removed.
7505*
7506* Unlinks the given node from its owner.
7507* This will substitute ns-references to node->nsDef for
7508* ns-references to doc->oldNs, thus ensuring the removed
7509* branch to be autark wrt ns-references.
Kasimier T. Buchcik017264f2005-06-27 13:45:24 +00007510* WARNING: This function is in a experimental state.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007511*
7512* Returns 0 on success, 1 if the node is not supported,
7513* -1 on API and internal errors.
7514*/
7515int
7516xmlDOMWrapRemoveNode(xmlDOMWrapCtxtPtr ctxt, xmlDocPtr doc,
7517 xmlNodePtr node, int options ATTRIBUTE_UNUSED)
7518{
7519 xmlNsPtr *list = NULL;
7520 int sizeList, nbList, i, j;
7521 xmlNsPtr ns;
7522
7523 if ((node == NULL) || (doc == NULL) || (node->doc != doc))
7524 return (-1);
7525
7526 /* TODO: 0 or -1 ? */
7527 if (node->parent == NULL)
7528 return (0);
7529
7530 switch (node->type) {
7531 case XML_TEXT_NODE:
7532 case XML_CDATA_SECTION_NODE:
7533 case XML_ENTITY_REF_NODE:
7534 case XML_PI_NODE:
7535 case XML_COMMENT_NODE:
7536 xmlUnlinkNode(node);
7537 return (0);
7538 case XML_ELEMENT_NODE:
7539 case XML_ATTRIBUTE_NODE:
7540 break;
7541 default:
7542 return (1);
7543 }
7544 xmlUnlinkNode(node);
7545 /*
7546 * Save out-of-scope ns-references in doc->oldNs.
7547 */
7548 do {
7549 switch (node->type) {
7550 case XML_ELEMENT_NODE:
7551 if ((ctxt == NULL) && (node->nsDef != NULL)) {
7552 ns = node->nsDef;
7553 do {
7554 if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
7555 &nbList, ns, ns) == -1)
7556 goto internal_error;
7557 ns = ns->next;
7558 } while (ns != NULL);
7559 }
7560 /* No break on purpose. */
7561 case XML_ATTRIBUTE_NODE:
7562 if (node->ns != NULL) {
7563 /*
7564 * Find a mapping.
7565 */
7566 if (list != NULL) {
7567 for (i = 0, j = 0; i < nbList; i++, j += 2) {
7568 if (node->ns == list[j]) {
7569 node->ns = list[++j];
7570 goto next_node;
7571 }
7572 }
7573 }
7574 ns = NULL;
7575 if (ctxt != NULL) {
7576 /*
7577 * User defined.
7578 */
7579 } else {
7580 /*
7581 * Add to doc's oldNs.
7582 */
7583 ns = xmlDOMWrapStoreNs(doc, node->ns->href,
7584 node->ns->prefix);
7585 if (ns == NULL)
7586 goto internal_error;
7587 }
7588 if (ns != NULL) {
7589 /*
7590 * Add mapping.
7591 */
7592 if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
7593 &nbList, node->ns, ns) == -1)
7594 goto internal_error;
7595 }
7596 node->ns = ns;
7597 }
7598 if ((node->type == XML_ELEMENT_NODE) &&
7599 (node->properties != NULL)) {
7600 node = (xmlNodePtr) node->properties;
7601 continue;
7602 }
7603 break;
7604 default:
7605 goto next_sibling;
7606 }
7607next_node:
7608 if ((node->type == XML_ELEMENT_NODE) &&
7609 (node->children != NULL)) {
7610 node = node->children;
7611 continue;
7612 }
7613next_sibling:
7614 if (node == NULL)
7615 break;
7616 if (node->next != NULL)
7617 node = node->next;
7618 else {
7619 node = node->parent;
7620 goto next_sibling;
7621 }
7622 } while (node != NULL);
7623
7624 if (list != NULL)
7625 xmlFree(list);
7626 return (0);
7627
7628internal_error:
7629 if (list != NULL)
7630 xmlFree(list);
7631 return (-1);
7632}
7633
7634/*
7635* xmlSearchNsByHrefStrict:
7636* @doc: the document
7637* @node: the start node
7638* @nsName: the searched namespace name
7639* @retNs: the resulting ns-decl
7640* @prefixed: if the found ns-decl must have a prefix (for attributes)
7641*
7642* Dynamically searches for a ns-declaration which matches
7643* the given @nsName in the ancestor-or-self axis of @node.
7644*
7645* Returns 1 if a ns-decl was found, 0 if not and -1 on API
7646* and internal errors.
7647*/
7648static int
7649xmlSearchNsByHrefStrict(xmlDocPtr doc, xmlNodePtr node, const xmlChar* nsName,
7650 xmlNsPtr *retNs, int prefixed)
7651{
7652 xmlNodePtr cur, prev = NULL, out = NULL;
7653 xmlNsPtr ns, prevns;
7654
7655 if ((doc == NULL) || (nsName == NULL) || (retNs == NULL))
7656 return (-1);
7657
7658 *retNs = NULL;
7659 if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) {
7660 *retNs = xmlTreeEnsureXMLDecl(doc);
7661 if (*retNs == NULL)
7662 return (-1);
7663 return (1);
7664 }
7665 cur = node;
7666 do {
7667 if (cur->type == XML_ELEMENT_NODE) {
7668 if (cur->nsDef != NULL) {
7669 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
7670 if (prefixed && (ns->prefix == NULL))
7671 continue;
7672 if (prev != NULL) {
7673 /*
7674 * Check the last level of ns-decls for a
7675 * shadowing prefix.
7676 */
7677 prevns = prev->nsDef;
7678 do {
7679 if ((prevns->prefix == ns->prefix) ||
7680 ((prevns->prefix != NULL) &&
7681 (ns->prefix != NULL) &&
7682 xmlStrEqual(prevns->prefix, ns->prefix))) {
7683 /*
7684 * Shadowed.
7685 */
7686 break;
7687 }
7688 prevns = prevns->next;
7689 } while (prevns != NULL);
7690 if (prevns != NULL)
7691 continue;
7692 }
7693 /*
7694 * Ns-name comparison.
7695 */
7696 if ((nsName == ns->href) ||
7697 xmlStrEqual(nsName, ns->href)) {
7698 /*
7699 * At this point the prefix can only be shadowed,
7700 * if we are the the (at least) 3rd level of
7701 * ns-decls.
7702 */
7703 if (out) {
7704 int ret;
7705
7706 ret = xmlNsInScope(doc, node, prev, ns->prefix);
7707 if (ret < 0)
7708 return (-1);
7709 /*
7710 * TODO: Should we try to find a matching ns-name
7711 * only once? This here keeps on searching.
7712 * I think we should try further since, there might
7713 * be an other matching ns-decl with an unshadowed
7714 * prefix.
7715 */
7716 if (! ret)
7717 continue;
7718 }
7719 *retNs = ns;
7720 return (1);
7721 }
7722 }
7723 out = prev;
7724 prev = cur;
7725 }
7726 } else if ((node->type == XML_ENTITY_REF_NODE) ||
7727 (node->type == XML_ENTITY_NODE) ||
7728 (node->type == XML_ENTITY_DECL))
7729 return (0);
7730 cur = cur->parent;
7731 } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
7732 return (0);
7733}
7734
7735/*
7736* xmlDOMWrapNSNormDeclareNsForced:
7737* @doc: the doc
7738* @elem: the element-node to declare on
7739* @nsName: the namespace-name of the ns-decl
7740* @prefix: the preferred prefix of the ns-decl
7741* @checkShadow: ensure that the new ns-decl doesn't shadow ancestor ns-decls
7742*
7743* Declares a new namespace on @elem. It tries to use the
7744* given @prefix; if a ns-decl with the given prefix is already existent
7745* on @elem, it will generate an other prefix.
7746*
7747* Returns 1 if a ns-decl was found, 0 if not and -1 on API
7748* and internal errors.
7749*/
7750static xmlNsPtr
7751xmlDOMWrapNSNormDeclareNsForced(xmlDocPtr doc,
7752 xmlNodePtr elem,
7753 const xmlChar *nsName,
7754 const xmlChar *prefix,
7755 int checkShadow)
7756{
7757
7758 xmlNsPtr ret;
7759 char buf[50];
7760 const xmlChar *pref;
7761 int counter = 0;
7762 /*
7763 * Create a ns-decl on @anchor.
7764 */
7765 pref = prefix;
7766 while (1) {
7767 /*
7768 * Lookup whether the prefix is unused in elem's ns-decls.
7769 */
7770 if ((elem->nsDef != NULL) &&
7771 (xmlTreeNSListLookupByPrefix(elem->nsDef, pref) != NULL))
7772 goto ns_next_prefix;
7773 if (checkShadow && elem->parent &&
7774 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
7775 /*
7776 * Does it shadow ancestor ns-decls?
7777 */
7778 if (xmlSearchNs(doc, elem->parent, pref) != NULL)
7779 goto ns_next_prefix;
7780 }
7781 ret = xmlNewNs(NULL, nsName, pref);
7782 if (ret == NULL)
7783 return (NULL);
7784 if (elem->nsDef == NULL)
7785 elem->nsDef = ret;
7786 else {
7787 xmlNsPtr ns2 = elem->nsDef;
7788 while (ns2->next != NULL)
7789 ns2 = ns2->next;
7790 ns2->next = ret;
7791 }
7792 return (ret);
7793ns_next_prefix:
7794 counter++;
7795 if (counter > 1000)
7796 return (NULL);
7797 if (prefix == NULL) {
7798 snprintf((char *) buf, sizeof(buf),
7799 "default%d", counter);
7800 } else
7801 snprintf((char *) buf, sizeof(buf),
7802 "%.30s%d", (char *)prefix, counter);
7803 pref = BAD_CAST buf;
7804 }
7805}
7806
7807/*
7808* xmlDOMWrapNSNormAquireNormalizedNs:
7809* @doc: the doc
7810* @elem: the element-node to declare namespaces on
7811* @ns: the ns-struct to use for the search
7812* @retNs: the found/created ns-struct
7813* @nsMap: the ns-map
7814* @topmi: the last ns-map entry
7815* @depth: the current tree depth
7816* @ancestorsOnly: search in ancestor ns-decls only
7817* @prefixed: if the searched ns-decl must have a prefix (for attributes)
7818*
7819* Searches for a matching ns-name in the ns-decls of @nsMap, if not
7820* found it will either declare it on @elem, or store it in doc->oldNs.
7821* If a new ns-decl needs to be declared on @elem, it tries to use the
7822* @ns->prefix for it, if this prefix is already in use on @elem, it will
7823* change the prefix or the new ns-decl.
7824*
7825* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
7826*/
7827static int
7828xmlDOMWrapNSNormAquireNormalizedNs(xmlDocPtr doc,
7829 xmlNodePtr elem,
7830 xmlNsPtr ns,
7831 xmlNsPtr *retNs,
7832 xmlNsMapItemPtr *nsMap,
7833 xmlNsMapItemPtr *topmi,
7834 int depth,
7835 int ancestorsOnly,
7836 int prefixed)
7837{
7838 xmlNsMapItemPtr mi;
7839
7840 if ((doc == NULL) || (ns == NULL) || (retNs == NULL) ||
7841 (nsMap == NULL) || (topmi == NULL))
7842 return (-1);
7843
7844 *retNs = NULL;
7845 /*
7846 * Handle XML namespace.
7847 */
7848 if ((ns->prefix) &&
7849 (ns->prefix[0] == 'x') &&
7850 (ns->prefix[1] == 'm') &&
7851 (ns->prefix[2] == 'l') &&
7852 (ns->prefix[3] == 0)) {
7853 /*
7854 * Insert XML namespace mapping.
7855 */
7856 *retNs = xmlTreeEnsureXMLDecl(doc);
7857 if (*retNs == NULL)
7858 return (-1);
7859 return (0);
7860 }
7861 /*
7862 * If the search should be done in ancestors only and no
7863 * @elem (the first ancestor) was specified, then skip the search.
7864 */
7865 if ((! (ancestorsOnly && (elem == NULL))) &&
7866 (*nsMap != NULL)) {
7867
7868 /*
7869 * Try to find an equal ns-name in in-scope ns-decls.
7870 */
7871 for (mi = *nsMap; mi != (*topmi)->next; mi = mi->next) {
7872
7873 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
7874 /*
7875 * This should be turned on to gain speed, if one knows
7876 * that the branch itself was already ns-wellformed and no
7877 * stale references existed. I.e. it searches in the ancestor
7878 * axis only.
7879 */
7880 ((! ancestorsOnly) || (mi->depth == XML_TREE_NSMAP_PARENT)) &&
7881 /* Skip shadowed prefixes. */
7882 (mi->shadowDepth == -1) &&
7883 /* Skip xmlns="" or xmlns:foo="". */
7884 ((mi->newNs->href != NULL) &&
7885 (mi->newNs->href[0] != 0)) &&
7886 /* Ensure a prefix if wanted. */
7887 ((! prefixed) || (mi->newNs->prefix != NULL)) &&
7888 /* Equal ns name */
7889 ((mi->newNs->href == ns->href) ||
7890 xmlStrEqual(mi->newNs->href, ns->href))) {
7891 /* Set the mapping. */
7892 mi->oldNs = ns;
7893 *retNs = mi->newNs;
7894 return (0);
7895 }
7896 }
7897 }
7898 /*
7899 * No luck, the namespace is out of scope or shadowed.
7900 */
7901 if (elem == NULL) {
7902 xmlNsPtr tmpns;
7903
7904 /*
7905 * Store ns-decls in "oldNs" of the document-node.
7906 */
7907 tmpns = xmlDOMWrapStoreNs(doc, ns->href, ns->prefix);
7908 if (tmpns == NULL)
7909 return (-1);
7910 /*
7911 * Insert mapping.
7912 */
7913 if (xmlDOMWrapNSNormAddNsMapItem(nsMap, NULL, ns,
7914 tmpns, XML_TREE_NSMAP_DOC) == NULL) {
7915 xmlFreeNs(tmpns);
7916 return (-1);
7917 }
7918 *retNs = tmpns;
7919 } else {
7920 xmlNsPtr tmpns;
7921
7922 tmpns = xmlDOMWrapNSNormDeclareNsForced(doc, elem, ns->href,
7923 ns->prefix, 0);
7924 if (tmpns == NULL)
7925 return (-1);
7926
7927 if (*nsMap != NULL) {
7928 /*
7929 * Does it shadow ancestor ns-decls?
7930 */
7931 for (mi = *nsMap; mi != (*topmi)->next; mi = mi->next) {
7932 if ((mi->depth < depth) &&
7933 (mi->shadowDepth == -1) &&
7934 ((ns->prefix == mi->newNs->prefix) ||
7935 xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
7936 /*
7937 * Shadows.
7938 */
7939 mi->shadowDepth = depth;
7940 break;
7941 }
7942 }
7943 }
7944 if (xmlDOMWrapNSNormAddNsMapItem(nsMap, topmi, ns,
7945 tmpns, depth) == NULL) {
7946 xmlFreeNs(tmpns);
7947 return (-1);
7948 }
7949 *retNs = tmpns;
7950 }
7951 return (0);
7952}
7953
7954/*
7955* xmlDOMWrapReconcileNamespaces:
7956* @elem: the element-node
7957* @options: option flags
7958*
7959* Ensures that ns-references point to ns-decls hold on element-nodes.
7960* Ensures that the tree is namespace wellformed by creating additional
7961* ns-decls where needed. Note that, since prefixes of already existent
7962* ns-decls can be shadowed by this process, it could break QNames in
7963* attribute values or element content.
Kasimier T. Buchcik017264f2005-06-27 13:45:24 +00007964* WARNING: This function is in a experimental state.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007965*
7966* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
7967*/
7968int
7969xmlDOMWrapReconcileNamespaces(xmlDOMWrapCtxtPtr ctxt ATTRIBUTE_UNUSED,
7970 xmlNodePtr elem,
7971 int options ATTRIBUTE_UNUSED)
7972{
7973 int depth = -1, adoptns = 0, parnsdone = 0;
7974 xmlNsPtr ns;
7975 xmlDocPtr doc;
7976 xmlNodePtr cur, curElem = NULL;
7977 xmlNsMapItemPtr nsMap = NULL, topmi = NULL, mi;
7978 /* @ancestorsOnly should be set by an option flag. */
7979 int ancestorsOnly = 0;
7980
7981 if ((elem == NULL) || (elem->doc == NULL) ||
7982 (elem->type != XML_ELEMENT_NODE))
7983 return (-1);
7984
7985 doc = elem->doc;
7986 cur = elem;
7987 do {
7988 switch (cur->type) {
7989 case XML_ELEMENT_NODE:
7990 adoptns = 1;
7991 curElem = cur;
7992 depth++;
7993 /*
7994 * Namespace declarations.
7995 */
7996 if (cur->nsDef != NULL) {
7997 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
7998 if (! parnsdone) {
7999 if ((elem->parent) &&
8000 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8001 /*
8002 * Gather ancestor in-scope ns-decls.
8003 */
8004 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8005 elem->parent) == -1)
8006 goto internal_error;
8007 if (nsMap != NULL)
8008 topmi = nsMap->prev;
8009 }
8010 parnsdone = 1;
8011 }
8012 /*
8013 * Skip ns-references handling if the referenced
8014 * ns-decl is declared on the same element.
8015 */
8016 if ((cur->ns != NULL) && adoptns && (cur->ns == ns))
8017 adoptns = 0;
8018 /*
8019 * Does it shadow any ns-decl?
8020 */
8021 if (nsMap) {
8022 for (mi = nsMap; mi != topmi->next; mi = mi->next) {
8023 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8024 (mi->shadowDepth == -1) &&
8025 ((ns->prefix == mi->newNs->prefix) ||
8026 xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
8027
8028 mi->shadowDepth = depth;
8029 }
8030 }
8031 }
8032 /*
8033 * Push mapping.
8034 */
8035 if (xmlDOMWrapNSNormAddNsMapItem(&nsMap, &topmi, ns, ns,
8036 depth) == NULL)
8037 goto internal_error;
8038 }
8039 }
8040 if (! adoptns)
8041 goto ns_end;
8042
8043 /* No break on purpose. */
8044 case XML_ATTRIBUTE_NODE:
8045 if (cur->ns == NULL)
8046 goto ns_end;
8047 if (! parnsdone) {
8048 if ((elem->parent) &&
8049 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8050 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8051 elem->parent) == -1)
8052 goto internal_error;
8053 if (nsMap != NULL)
8054 topmi = nsMap->prev;
8055 }
8056 parnsdone = 1;
8057 }
8058 /*
8059 * Adopt ns-references.
8060 */
8061 if (nsMap != NULL) {
8062 /*
8063 * Search for a mapping.
8064 */
8065 for (mi = nsMap; mi != topmi->next; mi = mi->next) {
8066 if ((mi->shadowDepth == -1) &&
8067 (cur->ns == mi->oldNs)) {
8068
8069 cur->ns = mi->newNs;
8070 goto ns_end;
8071 }
8072 }
8073 }
8074 /*
8075 * Aquire a normalized ns-decl and add it to the map.
8076 */
8077 if (xmlDOMWrapNSNormAquireNormalizedNs(doc, curElem,
8078 cur->ns, &ns,
8079 &nsMap, &topmi, depth,
8080 ancestorsOnly,
8081 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
8082 goto internal_error;
8083 cur->ns = ns;
8084
8085ns_end:
8086 if ((cur->type == XML_ELEMENT_NODE) &&
8087 (cur->properties != NULL)) {
8088 /*
8089 * Process attributes.
8090 */
8091 cur = (xmlNodePtr) cur->properties;
8092 continue;
8093 }
8094 break;
8095 default:
8096 goto next_sibling;
8097 }
8098 if ((cur->type == XML_ELEMENT_NODE) &&
8099 (cur->children != NULL)) {
8100 /*
8101 * Process content of element-nodes only.
8102 */
8103 cur = cur->children;
8104 continue;
8105 }
8106next_sibling:
8107 if (cur == elem)
8108 break;
8109 if (cur->type == XML_ELEMENT_NODE) {
8110 if (nsMap != NULL) {
8111 /*
8112 * Pop mappings.
8113 */
8114 while ((topmi->depth >= 0) && (topmi->depth >= depth))
8115 topmi = topmi->prev;
8116 /*
8117 * Unshadow.
8118 */
8119 for (mi = nsMap; mi != topmi->next; mi = mi->next)
8120 if (mi->shadowDepth >= depth)
8121 mi->shadowDepth = -1;
8122 }
8123 depth--;
8124 }
8125 if (cur->next != NULL)
8126 cur = cur->next;
8127 else {
8128 cur = cur->parent;
8129 goto next_sibling;
8130 }
8131 } while (cur != NULL);
8132
8133 if (nsMap != NULL)
8134 xmlDOMWrapNSNormFreeNsMap(nsMap);
8135 return (0);
8136internal_error:
8137 if (nsMap != NULL)
8138 xmlDOMWrapNSNormFreeNsMap(nsMap);
8139 return (-1);
8140}
8141
8142/*
8143* xmlDOMWrapAdoptBranch:
8144* @ctxt: the optional context for custom processing
8145* @sourceDoc: the optional sourceDoc
8146* @node: the element-node to start with
8147* @destDoc: the destination doc for adoption
8148* @parent: the optional new parent of @node in @destDoc
8149* @options: option flags
8150*
8151* Ensures that ns-references point to @destDoc: either to
8152* elements->nsDef entries if @destParent is given, or to
8153* @destDoc->oldNs otherwise.
8154* If @destParent is given, it ensures that the tree is namespace
8155* wellformed by creating additional ns-decls where needed.
8156* Note that, since prefixes of already existent ns-decls can be
8157* shadowed by this process, it could break QNames in attribute
8158* values or element content.
8159*
8160* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8161*/
8162static int
8163xmlDOMWrapAdoptBranch(xmlDOMWrapCtxtPtr ctxt,
8164 xmlDocPtr sourceDoc,
8165 xmlNodePtr node,
8166 xmlDocPtr destDoc,
8167 xmlNodePtr destParent,
8168 int options ATTRIBUTE_UNUSED)
8169{
8170 int ret = 0;
8171 xmlNodePtr cur, curElem = NULL;
8172 xmlNsMapItemPtr nsMap = NULL, topmi = NULL, mi;
8173 xmlNsPtr ns;
8174 int depth = -1, adoptStr = 1;
8175 /* gather @parent's ns-decls. */
8176 int parnsdone = 0;
8177 /* @ancestorsOnly should be set per option. */
8178 int ancestorsOnly = 0;
8179
8180 /*
8181 * Optimize string adoption for equal or none dicts.
8182 */
8183 if ((sourceDoc != NULL) &&
8184 (sourceDoc->dict == destDoc->dict))
8185 adoptStr = 0;
8186 else
8187 adoptStr = 1;
8188
8189 cur = node;
8190 while (cur != NULL) {
8191 if (cur->doc != sourceDoc) {
8192 /*
8193 * We'll assume XIncluded nodes if the doc differs.
8194 * TODO: Do we need to reconciliate XIncluded nodes?
8195 * This here skips XIncluded nodes and tries to handle
8196 * broken sequences.
8197 */
8198 if (cur->next == NULL)
8199 goto leave_node;
8200 do {
8201 cur = cur->next;
8202 if ((cur->type == XML_XINCLUDE_END) ||
8203 (cur->doc == node->doc))
8204 break;
8205 } while (cur->next != NULL);
8206
8207 if (cur->doc != node->doc)
8208 goto leave_node;
8209 }
8210 cur->doc = destDoc;
8211 switch (cur->type) {
8212 case XML_XINCLUDE_START:
8213 case XML_XINCLUDE_END:
8214 /*
8215 * TODO
8216 */
8217 return (-1);
8218 case XML_ELEMENT_NODE:
8219 curElem = cur;
8220 depth++;
8221 /*
8222 * Namespace declarations.
8223 */
8224 if ((ctxt == NULL) && (cur->nsDef != NULL)) {
8225 if (! parnsdone) {
8226 if (destParent && (ctxt == NULL)) {
8227 /*
8228 * Gather @parent's in-scope ns-decls.
8229 */
8230 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8231 destParent) == -1)
8232 goto internal_error;
8233 if (nsMap != NULL)
8234 topmi = nsMap->prev;
8235 }
8236 parnsdone = 1;
8237 }
8238 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
8239 /*
8240 * ns->prefix and ns->href seem not to be in the dict.
8241 * XML_TREE_ADOPT_STR(ns->prefix)
8242 * XML_TREE_ADOPT_STR(ns->href)
8243 */
8244 /*
8245 * Does it shadow any ns-decl?
8246 */
8247 if (nsMap) {
8248 for (mi = nsMap; mi != topmi->next;
8249 mi = mi->next) {
8250 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8251 (mi->shadowDepth == -1) &&
8252 ((ns->prefix == mi->newNs->prefix) ||
8253 xmlStrEqual(ns->prefix,
8254 mi->newNs->prefix))) {
8255
8256 mi->shadowDepth = depth;
8257 }
8258 }
8259 }
8260 /*
8261 * Push mapping.
8262 */
8263 if (xmlDOMWrapNSNormAddNsMapItem(&nsMap, &topmi,
8264 ns, ns, depth) == NULL)
8265 goto internal_error;
8266 }
8267 }
8268 /* No break on purpose. */
8269 case XML_ATTRIBUTE_NODE:
8270
8271 if (cur->ns == NULL)
8272 goto ns_end;
8273 if (! parnsdone) {
8274 if (destParent && (ctxt == NULL)) {
8275 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8276 destParent) == -1)
8277 goto internal_error;
8278 if (nsMap != NULL)
8279 topmi = nsMap->prev;
8280 }
8281 parnsdone = 1;
8282 }
8283 /*
8284 * Adopt ns-references.
8285 */
8286 if (nsMap != NULL) {
8287 /*
8288 * Search for a mapping.
8289 */
8290 for (mi = nsMap; mi != topmi->next; mi = mi->next) {
8291 if ((mi->shadowDepth == -1) &&
8292 (cur->ns == mi->oldNs)) {
8293
8294 cur->ns = mi->newNs;
8295 goto ns_end;
8296 }
8297 }
8298 }
8299 /*
8300 * Start searching for an in-scope ns-decl.
8301 */
8302 if (ctxt != NULL) {
8303 /*
8304 * User-defined behaviour.
8305 */
8306#if 0
8307 ctxt->aquireNsDecl(ctxt, cur->ns, &ns);
8308#endif
8309 /*
8310 * Insert mapping if ns is available; it's the users fault
8311 * if not.
8312 */
8313 if (xmlDOMWrapNSNormAddNsMapItem(&nsMap, &topmi,
8314 ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
8315 goto internal_error;
8316 cur->ns = ns;
8317 } else {
8318 /*
8319 * Aquire a normalized ns-decl and add it to the map.
8320 */
8321 if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc,
8322 /* ns-decls on curElem or on destDoc->oldNs */
8323 destParent ? curElem : NULL,
8324 cur->ns, &ns,
8325 &nsMap, &topmi, depth,
8326 ancestorsOnly,
8327 /* ns-decls must be prefixed for attributes. */
8328 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
8329 goto internal_error;
8330 cur->ns = ns;
8331 }
8332ns_end:
8333 /*
8334 * Further node properties.
8335 * TODO: Is this all?
8336 */
8337 XML_TREE_ADOPT_STR(cur->name)
8338 if (cur->type == XML_ELEMENT_NODE) {
8339 cur->psvi = NULL;
8340 cur->line = 0;
8341 cur->extra = 0;
8342 /*
8343 * Walk attributes.
8344 */
8345 if (cur->properties != NULL) {
8346 /*
8347 * Process first attribute node.
8348 */
8349 cur = (xmlNodePtr) cur->properties;
8350 continue;
8351 }
8352 } else {
8353 /*
8354 * Attributes.
8355 */
8356 if ((sourceDoc != NULL) &&
8357 (((xmlAttrPtr) cur)->atype == XML_ATTRIBUTE_ID))
8358 xmlRemoveID(sourceDoc, (xmlAttrPtr) cur);
8359 ((xmlAttrPtr) cur)->atype = 0;
8360 ((xmlAttrPtr) cur)->psvi = NULL;
8361 }
8362 break;
8363 case XML_TEXT_NODE:
8364 case XML_CDATA_SECTION_NODE:
8365 /*
8366 * This puts the content in the dest dict, only if
8367 * it was previously in the source dict.
8368 */
8369 XML_TREE_ADOPT_STR_2(cur->content)
8370 goto leave_node;
8371 case XML_ENTITY_REF_NODE:
8372 /*
8373 * Remove reference to the entitity-node.
8374 */
8375 cur->content = NULL;
8376 cur->children = NULL;
8377 cur->last = NULL;
8378 if ((destDoc->intSubset) || (destDoc->extSubset)) {
8379 xmlEntityPtr ent;
8380 /*
8381 * Assign new entity-node if available.
8382 */
8383 ent = xmlGetDocEntity(destDoc, cur->name);
8384 if (ent != NULL) {
8385 cur->content = ent->content;
8386 cur->children = (xmlNodePtr) ent;
8387 cur->last = (xmlNodePtr) ent;
8388 }
8389 }
8390 goto leave_node;
8391 case XML_PI_NODE:
8392 XML_TREE_ADOPT_STR(cur->name)
8393 XML_TREE_ADOPT_STR_2(cur->content)
8394 break;
8395 case XML_COMMENT_NODE:
8396 break;
8397 default:
8398 goto internal_error;
8399 }
8400 /*
8401 * Walk the tree.
8402 */
8403 if (cur->children != NULL) {
8404 cur = cur->children;
8405 continue;
8406 }
8407
8408leave_node:
8409 if (cur == node)
8410 break;
8411 if ((cur->type == XML_ELEMENT_NODE) ||
8412 (cur->type == XML_XINCLUDE_START) ||
8413 (cur->type == XML_XINCLUDE_END)) {
8414 /*
8415 * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
8416 */
8417 if (nsMap != NULL) {
8418 /*
8419 * Pop mappings.
8420 */
8421 while (topmi->depth >= depth)
8422 topmi = topmi->prev;
8423 /*
8424 * Unshadow.
8425 */
8426 for (mi = nsMap; mi != topmi->next; mi = mi->next)
8427 if (mi->shadowDepth >= depth)
8428 mi->shadowDepth = -1;
8429 }
8430 depth--;
8431 }
8432 if (cur->next != NULL)
8433 cur = cur->next;
8434 else {
8435 cur = cur->parent;
8436 goto leave_node;
8437 }
8438 }
8439 /*
8440 * Cleanup.
8441 */
8442 if (nsMap != NULL)
8443 xmlDOMWrapNSNormFreeNsMap(nsMap);
8444 return (ret);
8445internal_error:
8446 if (nsMap != NULL)
8447 xmlDOMWrapNSNormFreeNsMap(nsMap);
8448 return (-1);
8449}
8450
8451/*
8452* xmlDOMWrapAdoptAttr:
8453* @ctxt: the optional context for custom processing
8454* @sourceDoc: the optional source document of attr
8455* @attr: the attribute-node to be adopted
8456* @destDoc: the destination doc for adoption
8457* @destParent: the optional new parent of @attr in @destDoc
8458* @options: option flags
8459*
8460* @attr is adopted by @destDoc.
8461* Ensures that ns-references point to @destDoc: either to
8462* elements->nsDef entries if @destParent is given, or to
8463* @destDoc->oldNs otherwise.
8464*
8465* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8466*/
8467static int
8468xmlDOMWrapAdoptAttr(xmlDOMWrapCtxtPtr ctxt,
8469 xmlDocPtr sourceDoc,
8470 xmlAttrPtr attr,
8471 xmlDocPtr destDoc,
8472 xmlNodePtr destParent,
8473 int options ATTRIBUTE_UNUSED)
8474{
8475 xmlNodePtr cur;
8476 int adoptStr = 1;
8477
8478 if ((attr == NULL) || (destDoc == NULL))
8479 return (-1);
8480
8481 attr->doc = destDoc;
8482 if (attr->ns != NULL) {
8483 xmlNsPtr ns = NULL;
8484
8485 if (ctxt != NULL) {
8486 /* TODO: User defined. */
8487 }
8488 /* XML Namespace. */
8489 if ((attr->ns->prefix[0] == 'x') && (attr->ns->prefix[1] == 'm') &&
8490 (attr->ns->prefix[2] == 'l') && (attr->ns->prefix[3] == 0)) {
8491 ns = xmlTreeEnsureXMLDecl(destDoc);
8492 } else if (destParent == NULL) {
8493 /*
8494 * Store in @destDoc->oldNs.
8495 */
8496 ns = xmlDOMWrapStoreNs(destDoc, attr->ns->href, attr->ns->prefix);
8497 } else {
8498 /*
8499 * Declare on @destParent.
8500 */
8501 if (xmlSearchNsByHrefStrict(destDoc, destParent, attr->ns->href,
8502 &ns, 1) == -1)
8503 goto internal_error;
8504 if (ns == NULL) {
8505 ns = xmlDOMWrapNSNormDeclareNsForced(destDoc, destParent,
8506 attr->ns->href, attr->ns->prefix, 1);
8507 }
8508 }
8509 if (ns == NULL)
8510 goto internal_error;
8511 attr->ns = ns;
8512 }
8513
8514 XML_TREE_ADOPT_STR(attr->name);
8515 attr->atype = 0;
8516 attr->psvi = NULL;
8517 /*
8518 * Walk content.
8519 */
8520 if (attr->children == NULL)
8521 return (0);
8522 cur = attr->children;
8523 while (cur != NULL) {
8524 cur->doc = destDoc;
8525 switch (cur->type) {
8526 case XML_TEXT_NODE:
8527 case XML_CDATA_SECTION_NODE:
8528 XML_TREE_ADOPT_STR_2(cur->content)
8529 break;
8530 case XML_ENTITY_REF_NODE:
8531 /*
8532 * Remove reference to the entitity-node.
8533 */
8534 cur->content = NULL;
8535 cur->children = NULL;
8536 cur->last = NULL;
8537 if ((destDoc->intSubset) || (destDoc->extSubset)) {
8538 xmlEntityPtr ent;
8539 /*
8540 * Assign new entity-node if available.
8541 */
8542 ent = xmlGetDocEntity(destDoc, cur->name);
8543 if (ent != NULL) {
8544 cur->content = ent->content;
8545 cur->children = (xmlNodePtr) ent;
8546 cur->last = (xmlNodePtr) ent;
8547 }
8548 }
8549 break;
8550 default:
8551 break;
8552 }
8553 if (cur->children != NULL) {
8554 cur = cur->children;
8555 continue;
8556 }
8557next_sibling:
8558 if (cur == (xmlNodePtr) attr)
8559 break;
8560 if (cur->next != NULL)
8561 cur = cur->next;
8562 else {
8563 cur = cur->parent;
8564 goto next_sibling;
8565 }
8566 }
8567 return (0);
8568internal_error:
8569 return (-1);
8570}
8571
8572/*
8573* xmlDOMWrapAdoptNode:
8574* @ctxt: the optional context for custom processing
8575* @sourceDoc: the optional sourceDoc
8576* @node: the node to start with
8577* @destDoc: the destination doc
Kasimier T. Buchcik4d9c9482005-06-27 15:04:46 +00008578* @destParent: the optional new parent of @node in @destDoc
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008579* @options: option flags
8580*
8581* Ensures that ns-references point to @destDoc: either to
8582* elements->nsDef entries if @destParent is given, or to
8583* @destDoc->oldNs otherwise.
8584* If @destParent is given, it ensures that the tree is namespace
8585* wellformed by creating additional ns-decls where needed.
8586* Note that, since prefixes of already existent ns-decls can be
8587* shadowed by this process, it could break QNames in attribute
8588* values or element content.
Kasimier T. Buchcik017264f2005-06-27 13:45:24 +00008589* WARNING: This function is in a experimental state.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008590*
8591* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8592*/
8593int
8594xmlDOMWrapAdoptNode(xmlDOMWrapCtxtPtr ctxt,
8595 xmlDocPtr sourceDoc,
8596 xmlNodePtr node,
8597 xmlDocPtr destDoc,
8598 xmlNodePtr destParent,
8599 int options)
8600{
8601 if ((node == NULL) || (destDoc == NULL) ||
8602 ((destParent != NULL) && (destParent->doc != destDoc)))
8603 return(-1);
8604 /*
8605 * Check node->doc sanity.
8606 */
8607 if ((node->doc != NULL) && (sourceDoc != NULL) &&
8608 (node->doc != sourceDoc)) {
8609 /*
8610 * Might be an XIncluded node.
8611 */
8612 return (-1);
8613 }
8614 if (sourceDoc == NULL)
8615 sourceDoc = node->doc;
8616 if (sourceDoc == destDoc)
8617 return (-1);
8618 switch (node->type) {
8619 case XML_ELEMENT_NODE:
8620 case XML_ATTRIBUTE_NODE:
8621 case XML_TEXT_NODE:
8622 case XML_CDATA_SECTION_NODE:
8623 case XML_ENTITY_REF_NODE:
8624 case XML_PI_NODE:
8625 case XML_COMMENT_NODE:
8626 break;
8627 case XML_DOCUMENT_FRAG_NODE:
8628 return (2);
8629 default:
8630 return (1);
8631 }
8632 /*
8633 * Unlink only if @node was not already added to @destParent.
8634 */
8635 if ((node->parent != NULL) && (destParent != node->parent))
8636 xmlUnlinkNode(node);
8637
8638 if (node->type == XML_ELEMENT_NODE) {
8639 return (xmlDOMWrapAdoptBranch(ctxt, sourceDoc, node,
8640 destDoc, destParent, options));
8641 } else if (node->type == XML_ATTRIBUTE_NODE) {
8642 return (xmlDOMWrapAdoptAttr(ctxt, sourceDoc,
8643 (xmlAttrPtr) node, destDoc, destParent, options));
8644 } else {
8645 xmlNodePtr cur = node;
8646 int adoptStr = 1;
8647
8648 cur->doc = destDoc;
8649 /*
8650 * Optimize string adoption.
8651 */
8652 if ((sourceDoc != NULL) &&
8653 (sourceDoc->dict == destDoc->dict))
8654 adoptStr = 0;
8655 switch (node->type) {
8656 case XML_TEXT_NODE:
8657 case XML_CDATA_SECTION_NODE:
8658 XML_TREE_ADOPT_STR_2(node->content)
8659 break;
8660 case XML_ENTITY_REF_NODE:
8661 /*
8662 * Remove reference to the entitity-node.
8663 */
8664 node->content = NULL;
8665 node->children = NULL;
8666 node->last = NULL;
8667 if ((destDoc->intSubset) || (destDoc->extSubset)) {
8668 xmlEntityPtr ent;
8669 /*
8670 * Assign new entity-node if available.
8671 */
8672 ent = xmlGetDocEntity(destDoc, node->name);
8673 if (ent != NULL) {
8674 node->content = ent->content;
8675 node->children = (xmlNodePtr) ent;
8676 node->last = (xmlNodePtr) ent;
8677 }
8678 }
8679 XML_TREE_ADOPT_STR(node->name)
8680 break;
8681 case XML_PI_NODE:
8682 XML_TREE_ADOPT_STR(node->name)
8683 XML_TREE_ADOPT_STR_2(node->content)
8684 break;
8685 default:
8686 break;
8687 }
8688 }
8689 return (0);
8690}
8691
8692
Daniel Veillard5d4644e2005-04-01 13:11:58 +00008693#define bottom_tree
8694#include "elfgcchack.h"