blob: 07a555aa1393be4e1d6ad49e1b1a669d2b9dde32 [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
Daniel Veillardd1640922001-12-17 15:30:10 +00002 * tree.c : implementation of access function for an XML tree.
Owen Taylor3473f882001-02-23 17:55:21 +00003 *
Daniel Veillardd5c2f922002-11-21 14:10:52 +00004 * References:
5 * XHTML 1.0 W3C REC: http://www.w3.org/TR/2002/REC-xhtml1-20020801/
6 *
Owen Taylor3473f882001-02-23 17:55:21 +00007 * See Copyright for the status of this software.
8 *
Daniel Veillardc5d64342001-06-24 12:13:24 +00009 * daniel@veillard.com
Owen Taylor3473f882001-02-23 17:55:21 +000010 *
Owen Taylor3473f882001-02-23 17:55:21 +000011 */
12
Daniel Veillard34ce8be2002-03-18 19:37:11 +000013#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000014#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000015
Owen Taylor3473f882001-02-23 17:55:21 +000016#include <string.h> /* for memset() only ! */
17
18#ifdef HAVE_CTYPE_H
19#include <ctype.h>
20#endif
21#ifdef HAVE_STDLIB_H
22#include <stdlib.h>
23#endif
24#ifdef HAVE_ZLIB_H
25#include <zlib.h>
26#endif
27
28#include <libxml/xmlmemory.h>
29#include <libxml/tree.h>
30#include <libxml/parser.h>
Daniel Veillardb8c9be92001-07-09 16:01:19 +000031#include <libxml/uri.h>
Owen Taylor3473f882001-02-23 17:55:21 +000032#include <libxml/entities.h>
33#include <libxml/valid.h>
34#include <libxml/xmlerror.h>
Daniel Veillardbdb9ba72001-04-11 11:28:06 +000035#include <libxml/parserInternals.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000036#include <libxml/globals.h>
Daniel Veillardd5c2f922002-11-21 14:10:52 +000037#ifdef LIBXML_HTML_ENABLED
38#include <libxml/HTMLtree.h>
39#endif
William M. Brack1d8c9b22004-12-25 10:14:57 +000040#ifdef LIBXML_DEBUG_ENABLED
41#include <libxml/debugXML.h>
42#endif
Owen Taylor3473f882001-02-23 17:55:21 +000043
Daniel Veillarda880b122003-04-21 21:36:41 +000044int __xmlRegisterCallbacks = 0;
45
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +000046/************************************************************************
47 * *
48 * Forward declarations *
49 * *
50 ************************************************************************/
51
Daniel Veillard56a4cb82001-03-24 17:00:36 +000052xmlNsPtr xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns);
53
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +000054static xmlChar* xmlGetPropNodeValueInternal(xmlAttrPtr prop);
55
Daniel Veillard56a4cb82001-03-24 17:00:36 +000056/************************************************************************
57 * *
Daniel Veillard18ec16e2003-10-07 23:16:40 +000058 * Tree memory error handler *
59 * *
60 ************************************************************************/
61/**
62 * xmlTreeErrMemory:
63 * @extra: extra informations
64 *
65 * Handle an out of memory condition
66 */
67static void
68xmlTreeErrMemory(const char *extra)
69{
70 __xmlSimpleError(XML_FROM_TREE, XML_ERR_NO_MEMORY, NULL, NULL, extra);
71}
72
73/**
74 * xmlTreeErr:
75 * @code: the error number
76 * @extra: extra informations
77 *
78 * Handle an out of memory condition
79 */
80static void
81xmlTreeErr(int code, xmlNodePtr node, const char *extra)
82{
83 const char *msg = NULL;
84
85 switch(code) {
86 case XML_TREE_INVALID_HEX:
Daniel Veillardac996a12004-07-30 12:02:58 +000087 msg = "invalid hexadecimal character value\n";
Daniel Veillard18ec16e2003-10-07 23:16:40 +000088 break;
89 case XML_TREE_INVALID_DEC:
Daniel Veillardac996a12004-07-30 12:02:58 +000090 msg = "invalid decimal character value\n";
Daniel Veillard18ec16e2003-10-07 23:16:40 +000091 break;
92 case XML_TREE_UNTERMINATED_ENTITY:
Daniel Veillardac996a12004-07-30 12:02:58 +000093 msg = "unterminated entity reference %15s\n";
Daniel Veillard18ec16e2003-10-07 23:16:40 +000094 break;
95 default:
Daniel Veillardac996a12004-07-30 12:02:58 +000096 msg = "unexpected error number\n";
Daniel Veillard18ec16e2003-10-07 23:16:40 +000097 }
98 __xmlSimpleError(XML_FROM_TREE, code, node, msg, extra);
99}
100
101/************************************************************************
102 * *
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000103 * A few static variables and macros *
104 * *
105 ************************************************************************/
Daniel Veillardd0463562001-10-13 09:15:48 +0000106/* #undef xmlStringText */
Daniel Veillard22090732001-07-16 00:06:07 +0000107const xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 };
Daniel Veillardd0463562001-10-13 09:15:48 +0000108/* #undef xmlStringTextNoenc */
Daniel Veillard22090732001-07-16 00:06:07 +0000109const xmlChar xmlStringTextNoenc[] =
Owen Taylor3473f882001-02-23 17:55:21 +0000110 { 't', 'e', 'x', 't', 'n', 'o', 'e', 'n', 'c', 0 };
Daniel Veillardd0463562001-10-13 09:15:48 +0000111/* #undef xmlStringComment */
Daniel Veillard22090732001-07-16 00:06:07 +0000112const xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 };
113
Owen Taylor3473f882001-02-23 17:55:21 +0000114static int xmlCompressMode = 0;
115static int xmlCheckDTD = 1;
Owen Taylor3473f882001-02-23 17:55:21 +0000116
Owen Taylor3473f882001-02-23 17:55:21 +0000117#define UPDATE_LAST_CHILD_AND_PARENT(n) if ((n) != NULL) { \
118 xmlNodePtr ulccur = (n)->children; \
119 if (ulccur == NULL) { \
120 (n)->last = NULL; \
121 } else { \
122 while (ulccur->next != NULL) { \
123 ulccur->parent = (n); \
124 ulccur = ulccur->next; \
125 } \
126 ulccur->parent = (n); \
127 (n)->last = ulccur; \
128}}
129
Kasimier T. Buchcik44353412006-03-06 13:26:16 +0000130#define IS_STR_XML(str) ((str != NULL) && (str[0] == 'x') && \
131 (str[1] == 'm') && (str[2] == 'l') && (str[3] == 0))
132
Owen Taylor3473f882001-02-23 17:55:21 +0000133/* #define DEBUG_BUFFER */
134/* #define DEBUG_TREE */
135
136/************************************************************************
137 * *
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000138 * Functions to move to entities.c once the *
139 * API freeze is smoothen and they can be made public. *
140 * *
141 ************************************************************************/
142#include <libxml/hash.h>
143
Daniel Veillard652327a2003-09-29 18:02:38 +0000144#ifdef LIBXML_TREE_ENABLED
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000145/**
146 * xmlGetEntityFromDtd:
147 * @dtd: A pointer to the DTD to search
148 * @name: The entity name
149 *
150 * Do an entity lookup in the DTD entity hash table and
151 * return the corresponding entity, if found.
152 *
153 * Returns A pointer to the entity structure or NULL if not found.
154 */
155static xmlEntityPtr
156xmlGetEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) {
157 xmlEntitiesTablePtr table;
158
159 if((dtd != NULL) && (dtd->entities != NULL)) {
160 table = (xmlEntitiesTablePtr) dtd->entities;
161 return((xmlEntityPtr) xmlHashLookup(table, name));
162 /* return(xmlGetEntityFromTable(table, name)); */
163 }
164 return(NULL);
165}
166/**
167 * xmlGetParameterEntityFromDtd:
168 * @dtd: A pointer to the DTD to search
169 * @name: The entity name
170 *
171 * Do an entity lookup in the DTD pararmeter entity hash table and
172 * return the corresponding entity, if found.
173 *
174 * Returns A pointer to the entity structure or NULL if not found.
175 */
176static xmlEntityPtr
177xmlGetParameterEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) {
178 xmlEntitiesTablePtr table;
179
180 if ((dtd != NULL) && (dtd->pentities != NULL)) {
181 table = (xmlEntitiesTablePtr) dtd->pentities;
182 return((xmlEntityPtr) xmlHashLookup(table, name));
183 /* return(xmlGetEntityFromTable(table, name)); */
184 }
185 return(NULL);
186}
Daniel Veillard652327a2003-09-29 18:02:38 +0000187#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000188
189/************************************************************************
190 * *
Daniel Veillardc00cda82003-04-07 10:22:39 +0000191 * QName handling helper *
192 * *
193 ************************************************************************/
194
195/**
196 * xmlBuildQName:
197 * @ncname: the Name
198 * @prefix: the prefix
199 * @memory: preallocated memory
200 * @len: preallocated memory length
201 *
202 * Builds the QName @prefix:@ncname in @memory if there is enough space
203 * and prefix is not NULL nor empty, otherwise allocate a new string.
204 * If prefix is NULL or empty it returns ncname.
205 *
206 * Returns the new string which must be freed by the caller if different from
207 * @memory and @ncname or NULL in case of error
208 */
209xmlChar *
210xmlBuildQName(const xmlChar *ncname, const xmlChar *prefix,
211 xmlChar *memory, int len) {
212 int lenn, lenp;
213 xmlChar *ret;
214
Daniel Veillard3b7840c2003-09-11 23:42:01 +0000215 if (ncname == NULL) return(NULL);
216 if (prefix == NULL) return((xmlChar *) ncname);
Daniel Veillardc00cda82003-04-07 10:22:39 +0000217
218 lenn = strlen((char *) ncname);
219 lenp = strlen((char *) prefix);
220
221 if ((memory == NULL) || (len < lenn + lenp + 2)) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000222 ret = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000223 if (ret == NULL) {
224 xmlTreeErrMemory("building QName");
225 return(NULL);
226 }
Daniel Veillardc00cda82003-04-07 10:22:39 +0000227 } else {
228 ret = memory;
229 }
230 memcpy(&ret[0], prefix, lenp);
231 ret[lenp] = ':';
232 memcpy(&ret[lenp + 1], ncname, lenn);
233 ret[lenn + lenp + 1] = 0;
234 return(ret);
235}
236
237/**
238 * xmlSplitQName2:
239 * @name: the full QName
240 * @prefix: a xmlChar **
241 *
242 * parse an XML qualified name string
243 *
244 * [NS 5] QName ::= (Prefix ':')? LocalPart
245 *
246 * [NS 6] Prefix ::= NCName
247 *
248 * [NS 7] LocalPart ::= NCName
249 *
250 * Returns NULL if not a QName, otherwise the local part, and prefix
251 * is updated to get the Prefix if any.
252 */
253
254xmlChar *
255xmlSplitQName2(const xmlChar *name, xmlChar **prefix) {
256 int len = 0;
257 xmlChar *ret = NULL;
258
Daniel Veillardd5cc0f72004-11-06 19:24:28 +0000259 if (prefix == NULL) return(NULL);
Daniel Veillardc00cda82003-04-07 10:22:39 +0000260 *prefix = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000261 if (name == NULL) return(NULL);
Daniel Veillardc00cda82003-04-07 10:22:39 +0000262
263#ifndef XML_XML_NAMESPACE
264 /* xml: prefix is not really a namespace */
265 if ((name[0] == 'x') && (name[1] == 'm') &&
266 (name[2] == 'l') && (name[3] == ':'))
267 return(NULL);
268#endif
269
270 /* nasty but valid */
271 if (name[0] == ':')
272 return(NULL);
273
274 /*
275 * we are not trying to validate but just to cut, and yes it will
276 * work even if this is as set of UTF-8 encoded chars
277 */
278 while ((name[len] != 0) && (name[len] != ':'))
279 len++;
280
281 if (name[len] == 0)
282 return(NULL);
283
284 *prefix = xmlStrndup(name, len);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000285 if (*prefix == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000286 xmlTreeErrMemory("QName split");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000287 return(NULL);
288 }
Daniel Veillardc00cda82003-04-07 10:22:39 +0000289 ret = xmlStrdup(&name[len + 1]);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000290 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000291 xmlTreeErrMemory("QName split");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000292 if (*prefix != NULL) {
293 xmlFree(*prefix);
294 *prefix = NULL;
295 }
296 return(NULL);
297 }
Daniel Veillardc00cda82003-04-07 10:22:39 +0000298
299 return(ret);
300}
301
Daniel Veillard8d73bcb2003-08-04 01:06:15 +0000302/**
303 * xmlSplitQName3:
304 * @name: the full QName
305 * @len: an int *
306 *
307 * parse an XML qualified name string,i
308 *
309 * returns NULL if it is not a Qualified Name, otherwise, update len
310 * with the lenght in byte of the prefix and return a pointer
Daniel Veillard54f9a4f2005-09-03 13:28:24 +0000311 * to the start of the name without the prefix
Daniel Veillard8d73bcb2003-08-04 01:06:15 +0000312 */
313
314const xmlChar *
315xmlSplitQName3(const xmlChar *name, int *len) {
316 int l = 0;
317
318 if (name == NULL) return(NULL);
319 if (len == NULL) return(NULL);
320
321 /* nasty but valid */
322 if (name[0] == ':')
323 return(NULL);
324
325 /*
326 * we are not trying to validate but just to cut, and yes it will
327 * work even if this is as set of UTF-8 encoded chars
328 */
329 while ((name[l] != 0) && (name[l] != ':'))
330 l++;
331
332 if (name[l] == 0)
333 return(NULL);
334
335 *len = l;
336
337 return(&name[l+1]);
338}
339
Daniel Veillardc00cda82003-04-07 10:22:39 +0000340/************************************************************************
341 * *
Daniel Veillardd2298792003-02-14 16:54:11 +0000342 * Check Name, NCName and QName strings *
343 * *
344 ************************************************************************/
345
346#define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
347
Daniel Veillard6b6d6802005-07-03 21:00:34 +0000348#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_DEBUG_ENABLED) || defined (LIBXML_HTML_ENABLED)
Daniel Veillardd2298792003-02-14 16:54:11 +0000349/**
350 * xmlValidateNCName:
351 * @value: the value to check
352 * @space: allow spaces in front and end of the string
353 *
354 * Check that a value conforms to the lexical space of NCName
355 *
356 * Returns 0 if this validates, a positive error code number otherwise
357 * and -1 in case of internal or API error.
358 */
359int
360xmlValidateNCName(const xmlChar *value, int space) {
361 const xmlChar *cur = value;
362 int c,l;
363
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000364 if (value == NULL)
365 return(-1);
366
Daniel Veillardd2298792003-02-14 16:54:11 +0000367 /*
368 * First quick algorithm for ASCII range
369 */
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 >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
373 (*cur == '_'))
374 cur++;
375 else
376 goto try_complex;
377 while (((*cur >= 'a') && (*cur <= 'z')) ||
378 ((*cur >= 'A') && (*cur <= 'Z')) ||
379 ((*cur >= '0') && (*cur <= '9')) ||
380 (*cur == '_') || (*cur == '-') || (*cur == '.'))
381 cur++;
382 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000383 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +0000384 if (*cur == 0)
385 return(0);
386
387try_complex:
388 /*
389 * Second check for chars outside the ASCII range
390 */
391 cur = value;
392 c = CUR_SCHAR(cur, l);
393 if (space) {
394 while (IS_BLANK(c)) {
395 cur += l;
396 c = CUR_SCHAR(cur, l);
397 }
398 }
William M. Brack871611b2003-10-18 04:53:14 +0000399 if ((!IS_LETTER(c)) && (c != '_'))
Daniel Veillardd2298792003-02-14 16:54:11 +0000400 return(1);
401 cur += l;
402 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +0000403 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
404 (c == '-') || (c == '_') || IS_COMBINING(c) ||
405 IS_EXTENDER(c)) {
Daniel Veillardd2298792003-02-14 16:54:11 +0000406 cur += l;
407 c = CUR_SCHAR(cur, l);
408 }
409 if (space) {
410 while (IS_BLANK(c)) {
411 cur += l;
412 c = CUR_SCHAR(cur, l);
413 }
414 }
415 if (c != 0)
416 return(1);
417
418 return(0);
419}
Daniel Veillard2156d432004-03-04 15:59:36 +0000420#endif
Daniel Veillardd2298792003-02-14 16:54:11 +0000421
Daniel Veillard2156d432004-03-04 15:59:36 +0000422#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Daniel Veillardd2298792003-02-14 16:54:11 +0000423/**
424 * xmlValidateQName:
425 * @value: the value to check
426 * @space: allow spaces in front and end of the string
427 *
428 * Check that a value conforms to the lexical space of QName
429 *
430 * Returns 0 if this validates, a positive error code number otherwise
431 * and -1 in case of internal or API error.
432 */
433int
434xmlValidateQName(const xmlChar *value, int space) {
435 const xmlChar *cur = value;
436 int c,l;
437
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000438 if (value == NULL)
439 return(-1);
Daniel Veillardd2298792003-02-14 16:54:11 +0000440 /*
441 * First quick algorithm for ASCII range
442 */
443 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000444 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +0000445 if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
446 (*cur == '_'))
447 cur++;
448 else
449 goto try_complex;
450 while (((*cur >= 'a') && (*cur <= 'z')) ||
451 ((*cur >= 'A') && (*cur <= 'Z')) ||
452 ((*cur >= '0') && (*cur <= '9')) ||
453 (*cur == '_') || (*cur == '-') || (*cur == '.'))
454 cur++;
455 if (*cur == ':') {
456 cur++;
457 if (((*cur >= 'a') && (*cur <= 'z')) ||
458 ((*cur >= 'A') && (*cur <= 'Z')) ||
459 (*cur == '_'))
460 cur++;
461 else
462 goto try_complex;
463 while (((*cur >= 'a') && (*cur <= 'z')) ||
464 ((*cur >= 'A') && (*cur <= 'Z')) ||
465 ((*cur >= '0') && (*cur <= '9')) ||
466 (*cur == '_') || (*cur == '-') || (*cur == '.'))
467 cur++;
468 }
469 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000470 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +0000471 if (*cur == 0)
472 return(0);
473
474try_complex:
475 /*
476 * Second check for chars outside the ASCII range
477 */
478 cur = value;
479 c = CUR_SCHAR(cur, l);
480 if (space) {
481 while (IS_BLANK(c)) {
482 cur += l;
483 c = CUR_SCHAR(cur, l);
484 }
485 }
William M. Brack871611b2003-10-18 04:53:14 +0000486 if ((!IS_LETTER(c)) && (c != '_'))
Daniel Veillardd2298792003-02-14 16:54:11 +0000487 return(1);
488 cur += l;
489 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +0000490 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
491 (c == '-') || (c == '_') || IS_COMBINING(c) ||
492 IS_EXTENDER(c)) {
Daniel Veillardd2298792003-02-14 16:54:11 +0000493 cur += l;
494 c = CUR_SCHAR(cur, l);
495 }
496 if (c == ':') {
497 cur += l;
498 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +0000499 if ((!IS_LETTER(c)) && (c != '_'))
Daniel Veillardd2298792003-02-14 16:54:11 +0000500 return(1);
501 cur += l;
502 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +0000503 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
504 (c == '-') || (c == '_') || IS_COMBINING(c) ||
505 IS_EXTENDER(c)) {
Daniel Veillardd2298792003-02-14 16:54:11 +0000506 cur += l;
507 c = CUR_SCHAR(cur, l);
508 }
509 }
510 if (space) {
511 while (IS_BLANK(c)) {
512 cur += l;
513 c = CUR_SCHAR(cur, l);
514 }
515 }
516 if (c != 0)
517 return(1);
518 return(0);
519}
520
521/**
522 * xmlValidateName:
523 * @value: the value to check
524 * @space: allow spaces in front and end of the string
525 *
526 * Check that a value conforms to the lexical space of Name
527 *
528 * Returns 0 if this validates, a positive error code number otherwise
529 * and -1 in case of internal or API error.
530 */
531int
532xmlValidateName(const xmlChar *value, int space) {
533 const xmlChar *cur = value;
534 int c,l;
535
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000536 if (value == NULL)
537 return(-1);
Daniel Veillardd2298792003-02-14 16:54:11 +0000538 /*
539 * First quick algorithm for ASCII range
540 */
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 >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
544 (*cur == '_') || (*cur == ':'))
545 cur++;
546 else
547 goto try_complex;
548 while (((*cur >= 'a') && (*cur <= 'z')) ||
549 ((*cur >= 'A') && (*cur <= 'Z')) ||
550 ((*cur >= '0') && (*cur <= '9')) ||
551 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
552 cur++;
553 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000554 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +0000555 if (*cur == 0)
556 return(0);
557
558try_complex:
559 /*
560 * Second check for chars outside the ASCII range
561 */
562 cur = value;
563 c = CUR_SCHAR(cur, l);
564 if (space) {
565 while (IS_BLANK(c)) {
566 cur += l;
567 c = CUR_SCHAR(cur, l);
568 }
569 }
William M. Brack871611b2003-10-18 04:53:14 +0000570 if ((!IS_LETTER(c)) && (c != '_') && (c != ':'))
Daniel Veillardd2298792003-02-14 16:54:11 +0000571 return(1);
572 cur += l;
573 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +0000574 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
575 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) {
Daniel Veillardd2298792003-02-14 16:54:11 +0000576 cur += l;
577 c = CUR_SCHAR(cur, l);
578 }
579 if (space) {
580 while (IS_BLANK(c)) {
581 cur += l;
582 c = CUR_SCHAR(cur, l);
583 }
584 }
585 if (c != 0)
586 return(1);
587 return(0);
588}
589
Daniel Veillardd4310742003-02-18 21:12:46 +0000590/**
591 * xmlValidateNMToken:
592 * @value: the value to check
593 * @space: allow spaces in front and end of the string
594 *
595 * Check that a value conforms to the lexical space of NMToken
596 *
597 * Returns 0 if this validates, a positive error code number otherwise
598 * and -1 in case of internal or API error.
599 */
600int
601xmlValidateNMToken(const xmlChar *value, int space) {
602 const xmlChar *cur = value;
603 int c,l;
604
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000605 if (value == NULL)
606 return(-1);
Daniel Veillardd4310742003-02-18 21:12:46 +0000607 /*
608 * First quick algorithm for ASCII range
609 */
610 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000611 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd4310742003-02-18 21:12:46 +0000612 if (((*cur >= 'a') && (*cur <= 'z')) ||
613 ((*cur >= 'A') && (*cur <= 'Z')) ||
614 ((*cur >= '0') && (*cur <= '9')) ||
615 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
616 cur++;
617 else
618 goto try_complex;
619 while (((*cur >= 'a') && (*cur <= 'z')) ||
620 ((*cur >= 'A') && (*cur <= 'Z')) ||
621 ((*cur >= '0') && (*cur <= '9')) ||
622 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
623 cur++;
624 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000625 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd4310742003-02-18 21:12:46 +0000626 if (*cur == 0)
627 return(0);
628
629try_complex:
630 /*
631 * Second check for chars outside the ASCII range
632 */
633 cur = value;
634 c = CUR_SCHAR(cur, l);
635 if (space) {
636 while (IS_BLANK(c)) {
637 cur += l;
638 c = CUR_SCHAR(cur, l);
639 }
640 }
William M. Brack871611b2003-10-18 04:53:14 +0000641 if (!(IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
642 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)))
Daniel Veillardd4310742003-02-18 21:12:46 +0000643 return(1);
644 cur += l;
645 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +0000646 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
647 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) {
Daniel Veillardd4310742003-02-18 21:12:46 +0000648 cur += l;
649 c = CUR_SCHAR(cur, l);
650 }
651 if (space) {
652 while (IS_BLANK(c)) {
653 cur += l;
654 c = CUR_SCHAR(cur, l);
655 }
656 }
657 if (c != 0)
658 return(1);
659 return(0);
660}
Daniel Veillard652327a2003-09-29 18:02:38 +0000661#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardd4310742003-02-18 21:12:46 +0000662
Daniel Veillardd2298792003-02-14 16:54:11 +0000663/************************************************************************
664 * *
Owen Taylor3473f882001-02-23 17:55:21 +0000665 * Allocation and deallocation of basic structures *
666 * *
667 ************************************************************************/
668
669/**
670 * xmlSetBufferAllocationScheme:
671 * @scheme: allocation method to use
672 *
673 * Set the buffer allocation method. Types are
674 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
675 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
676 * improves performance
677 */
678void
679xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme) {
680 xmlBufferAllocScheme = scheme;
681}
682
683/**
684 * xmlGetBufferAllocationScheme:
685 *
686 * Types are
687 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
688 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
689 * improves performance
690 *
691 * Returns the current allocation scheme
692 */
693xmlBufferAllocationScheme
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000694xmlGetBufferAllocationScheme(void) {
Daniel Veillarde043ee12001-04-16 14:08:07 +0000695 return(xmlBufferAllocScheme);
Owen Taylor3473f882001-02-23 17:55:21 +0000696}
697
698/**
699 * xmlNewNs:
700 * @node: the element carrying the namespace
701 * @href: the URI associated
702 * @prefix: the prefix for the namespace
703 *
704 * Creation of a new Namespace. This function will refuse to create
705 * a namespace with a similar prefix than an existing one present on this
706 * node.
707 * We use href==NULL in the case of an element creation where the namespace
708 * was not defined.
Daniel Veillardd1640922001-12-17 15:30:10 +0000709 * Returns a new namespace pointer or NULL
Owen Taylor3473f882001-02-23 17:55:21 +0000710 */
711xmlNsPtr
712xmlNewNs(xmlNodePtr node, const xmlChar *href, const xmlChar *prefix) {
713 xmlNsPtr cur;
714
715 if ((node != NULL) && (node->type != XML_ELEMENT_NODE))
716 return(NULL);
717
Daniel Veillard20ee8c02001-10-05 09:18:14 +0000718 if ((prefix != NULL) && (xmlStrEqual(prefix, BAD_CAST "xml")))
719 return(NULL);
720
Owen Taylor3473f882001-02-23 17:55:21 +0000721 /*
722 * Allocate a new Namespace and fill the fields.
723 */
724 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
725 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000726 xmlTreeErrMemory("building namespace");
Owen Taylor3473f882001-02-23 17:55:21 +0000727 return(NULL);
728 }
729 memset(cur, 0, sizeof(xmlNs));
730 cur->type = XML_LOCAL_NAMESPACE;
731
732 if (href != NULL)
733 cur->href = xmlStrdup(href);
734 if (prefix != NULL)
735 cur->prefix = xmlStrdup(prefix);
736
737 /*
738 * Add it at the end to preserve parsing order ...
739 * and checks for existing use of the prefix
740 */
741 if (node != NULL) {
742 if (node->nsDef == NULL) {
743 node->nsDef = cur;
744 } else {
745 xmlNsPtr prev = node->nsDef;
746
747 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
748 (xmlStrEqual(prev->prefix, cur->prefix))) {
749 xmlFreeNs(cur);
750 return(NULL);
751 }
752 while (prev->next != NULL) {
753 prev = prev->next;
754 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
755 (xmlStrEqual(prev->prefix, cur->prefix))) {
756 xmlFreeNs(cur);
757 return(NULL);
758 }
759 }
760 prev->next = cur;
761 }
762 }
763 return(cur);
764}
765
766/**
767 * xmlSetNs:
768 * @node: a node in the document
769 * @ns: a namespace pointer
770 *
771 * Associate a namespace to a node, a posteriori.
772 */
773void
774xmlSetNs(xmlNodePtr node, xmlNsPtr ns) {
775 if (node == NULL) {
776#ifdef DEBUG_TREE
777 xmlGenericError(xmlGenericErrorContext,
778 "xmlSetNs: node == NULL\n");
779#endif
780 return;
781 }
782 node->ns = ns;
783}
784
785/**
786 * xmlFreeNs:
787 * @cur: the namespace pointer
788 *
789 * Free up the structures associated to a namespace
790 */
791void
792xmlFreeNs(xmlNsPtr cur) {
793 if (cur == NULL) {
794#ifdef DEBUG_TREE
795 xmlGenericError(xmlGenericErrorContext,
796 "xmlFreeNs : ns == NULL\n");
797#endif
798 return;
799 }
800 if (cur->href != NULL) xmlFree((char *) cur->href);
801 if (cur->prefix != NULL) xmlFree((char *) cur->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000802 xmlFree(cur);
803}
804
805/**
806 * xmlFreeNsList:
807 * @cur: the first namespace pointer
808 *
809 * Free up all the structures associated to the chained namespaces.
810 */
811void
812xmlFreeNsList(xmlNsPtr cur) {
813 xmlNsPtr next;
814 if (cur == NULL) {
815#ifdef DEBUG_TREE
816 xmlGenericError(xmlGenericErrorContext,
817 "xmlFreeNsList : ns == NULL\n");
818#endif
819 return;
820 }
821 while (cur != NULL) {
822 next = cur->next;
823 xmlFreeNs(cur);
824 cur = next;
825 }
826}
827
828/**
829 * xmlNewDtd:
830 * @doc: the document pointer
831 * @name: the DTD name
832 * @ExternalID: the external ID
833 * @SystemID: the system ID
834 *
835 * Creation of a new DTD for the external subset. To create an
836 * internal subset, use xmlCreateIntSubset().
837 *
838 * Returns a pointer to the new DTD structure
839 */
840xmlDtdPtr
841xmlNewDtd(xmlDocPtr doc, const xmlChar *name,
842 const xmlChar *ExternalID, const xmlChar *SystemID) {
843 xmlDtdPtr cur;
844
845 if ((doc != NULL) && (doc->extSubset != NULL)) {
846#ifdef DEBUG_TREE
847 xmlGenericError(xmlGenericErrorContext,
848 "xmlNewDtd(%s): document %s already have a DTD %s\n",
849 /* !!! */ (char *) name, doc->name,
850 /* !!! */ (char *)doc->extSubset->name);
851#endif
852 return(NULL);
853 }
854
855 /*
856 * Allocate a new DTD and fill the fields.
857 */
858 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
859 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000860 xmlTreeErrMemory("building DTD");
Owen Taylor3473f882001-02-23 17:55:21 +0000861 return(NULL);
862 }
863 memset(cur, 0 , sizeof(xmlDtd));
864 cur->type = XML_DTD_NODE;
865
866 if (name != NULL)
867 cur->name = xmlStrdup(name);
868 if (ExternalID != NULL)
869 cur->ExternalID = xmlStrdup(ExternalID);
870 if (SystemID != NULL)
871 cur->SystemID = xmlStrdup(SystemID);
872 if (doc != NULL)
873 doc->extSubset = cur;
874 cur->doc = doc;
875
Daniel Veillarda880b122003-04-21 21:36:41 +0000876 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +0000877 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000878 return(cur);
879}
880
881/**
882 * xmlGetIntSubset:
883 * @doc: the document pointer
884 *
885 * Get the internal subset of a document
886 * Returns a pointer to the DTD structure or NULL if not found
887 */
888
889xmlDtdPtr
890xmlGetIntSubset(xmlDocPtr doc) {
891 xmlNodePtr cur;
892
893 if (doc == NULL)
894 return(NULL);
895 cur = doc->children;
896 while (cur != NULL) {
897 if (cur->type == XML_DTD_NODE)
898 return((xmlDtdPtr) cur);
899 cur = cur->next;
900 }
901 return((xmlDtdPtr) doc->intSubset);
902}
903
904/**
905 * xmlCreateIntSubset:
906 * @doc: the document pointer
907 * @name: the DTD name
Daniel Veillarde356c282001-03-10 12:32:04 +0000908 * @ExternalID: the external (PUBLIC) ID
Owen Taylor3473f882001-02-23 17:55:21 +0000909 * @SystemID: the system ID
910 *
911 * Create the internal subset of a document
912 * Returns a pointer to the new DTD structure
913 */
914xmlDtdPtr
915xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name,
916 const xmlChar *ExternalID, const xmlChar *SystemID) {
917 xmlDtdPtr cur;
918
919 if ((doc != NULL) && (xmlGetIntSubset(doc) != NULL)) {
920#ifdef DEBUG_TREE
921 xmlGenericError(xmlGenericErrorContext,
922
923 "xmlCreateIntSubset(): document %s already have an internal subset\n",
924 doc->name);
925#endif
926 return(NULL);
927 }
928
929 /*
930 * Allocate a new DTD and fill the fields.
931 */
932 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
933 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000934 xmlTreeErrMemory("building internal subset");
Owen Taylor3473f882001-02-23 17:55:21 +0000935 return(NULL);
936 }
937 memset(cur, 0, sizeof(xmlDtd));
938 cur->type = XML_DTD_NODE;
939
William M. Bracka3215c72004-07-31 16:24:01 +0000940 if (name != NULL) {
941 cur->name = xmlStrdup(name);
942 if (cur->name == NULL) {
943 xmlTreeErrMemory("building internal subset");
944 xmlFree(cur);
945 return(NULL);
946 }
947 }
948 if (ExternalID != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +0000949 cur->ExternalID = xmlStrdup(ExternalID);
William M. Bracka3215c72004-07-31 16:24:01 +0000950 if (cur->ExternalID == NULL) {
951 xmlTreeErrMemory("building internal subset");
952 if (cur->name != NULL)
953 xmlFree((char *)cur->name);
954 xmlFree(cur);
955 return(NULL);
956 }
957 }
958 if (SystemID != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +0000959 cur->SystemID = xmlStrdup(SystemID);
William M. Bracka3215c72004-07-31 16:24:01 +0000960 if (cur->SystemID == NULL) {
961 xmlTreeErrMemory("building internal subset");
962 if (cur->name != NULL)
963 xmlFree((char *)cur->name);
964 if (cur->ExternalID != NULL)
965 xmlFree((char *)cur->ExternalID);
966 xmlFree(cur);
967 return(NULL);
968 }
969 }
Owen Taylor3473f882001-02-23 17:55:21 +0000970 if (doc != NULL) {
971 doc->intSubset = cur;
972 cur->parent = doc;
973 cur->doc = doc;
974 if (doc->children == NULL) {
975 doc->children = (xmlNodePtr) cur;
976 doc->last = (xmlNodePtr) cur;
977 } else {
Owen Taylor3473f882001-02-23 17:55:21 +0000978 if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillarde356c282001-03-10 12:32:04 +0000979 xmlNodePtr prev;
980
Owen Taylor3473f882001-02-23 17:55:21 +0000981 prev = doc->children;
982 prev->prev = (xmlNodePtr) cur;
983 cur->next = prev;
984 doc->children = (xmlNodePtr) cur;
985 } else {
Daniel Veillarde356c282001-03-10 12:32:04 +0000986 xmlNodePtr next;
987
988 next = doc->children;
989 while ((next != NULL) && (next->type != XML_ELEMENT_NODE))
990 next = next->next;
991 if (next == NULL) {
992 cur->prev = doc->last;
993 cur->prev->next = (xmlNodePtr) cur;
994 cur->next = NULL;
995 doc->last = (xmlNodePtr) cur;
996 } else {
997 cur->next = next;
998 cur->prev = next->prev;
999 if (cur->prev == NULL)
1000 doc->children = (xmlNodePtr) cur;
1001 else
1002 cur->prev->next = (xmlNodePtr) cur;
1003 next->prev = (xmlNodePtr) cur;
1004 }
Owen Taylor3473f882001-02-23 17:55:21 +00001005 }
1006 }
1007 }
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001008
Daniel Veillarda880b122003-04-21 21:36:41 +00001009 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001010 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001011 return(cur);
1012}
1013
1014/**
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001015 * DICT_FREE:
1016 * @str: a string
1017 *
1018 * Free a string if it is not owned by the "dict" dictionnary in the
1019 * current scope
1020 */
1021#define DICT_FREE(str) \
1022 if ((str) && ((!dict) || \
1023 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
1024 xmlFree((char *)(str));
1025
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00001026
1027/**
1028 * DICT_COPY:
1029 * @str: a string
1030 *
1031 * Copy a string using a "dict" dictionnary in the current scope,
1032 * if availabe.
1033 */
1034#define DICT_COPY(str, cpy) \
1035 if (str) { \
1036 if (dict) { \
1037 if (xmlDictOwns(dict, (const xmlChar *)(str))) \
1038 cpy = (xmlChar *) (str); \
1039 else \
1040 cpy = (xmlChar *) xmlDictLookup((dict), (const xmlChar *)(str), -1); \
1041 } else \
1042 cpy = xmlStrdup((const xmlChar *)(str)); }
1043
1044/**
1045 * DICT_CONST_COPY:
1046 * @str: a string
1047 *
1048 * Copy a string using a "dict" dictionnary in the current scope,
1049 * if availabe.
1050 */
1051#define DICT_CONST_COPY(str, cpy) \
1052 if (str) { \
1053 if (dict) { \
1054 if (xmlDictOwns(dict, (const xmlChar *)(str))) \
1055 cpy = (const xmlChar *) (str); \
1056 else \
1057 cpy = xmlDictLookup((dict), (const xmlChar *)(str), -1); \
1058 } else \
1059 cpy = (const xmlChar *) xmlStrdup((const xmlChar *)(str)); }
1060
1061
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001062/**
Owen Taylor3473f882001-02-23 17:55:21 +00001063 * xmlFreeDtd:
1064 * @cur: the DTD structure to free up
1065 *
1066 * Free a DTD structure.
1067 */
1068void
1069xmlFreeDtd(xmlDtdPtr cur) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001070 xmlDictPtr dict = NULL;
1071
Owen Taylor3473f882001-02-23 17:55:21 +00001072 if (cur == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001073 return;
1074 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001075 if (cur->doc != NULL) dict = cur->doc->dict;
Daniel Veillard5335dc52003-01-01 20:59:38 +00001076
Daniel Veillarda880b122003-04-21 21:36:41 +00001077 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001078 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
1079
Owen Taylor3473f882001-02-23 17:55:21 +00001080 if (cur->children != NULL) {
1081 xmlNodePtr next, c = cur->children;
1082
1083 /*
William M. Brack18a04f22004-08-03 16:42:37 +00001084 * Cleanup all nodes which are not part of the specific lists
1085 * of notations, elements, attributes and entities.
Owen Taylor3473f882001-02-23 17:55:21 +00001086 */
1087 while (c != NULL) {
1088 next = c->next;
William M. Brack18a04f22004-08-03 16:42:37 +00001089 if ((c->type != XML_NOTATION_NODE) &&
1090 (c->type != XML_ELEMENT_DECL) &&
1091 (c->type != XML_ATTRIBUTE_DECL) &&
1092 (c->type != XML_ENTITY_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001093 xmlUnlinkNode(c);
1094 xmlFreeNode(c);
1095 }
1096 c = next;
1097 }
1098 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001099 DICT_FREE(cur->name)
1100 DICT_FREE(cur->SystemID)
1101 DICT_FREE(cur->ExternalID)
Owen Taylor3473f882001-02-23 17:55:21 +00001102 /* TODO !!! */
1103 if (cur->notations != NULL)
1104 xmlFreeNotationTable((xmlNotationTablePtr) cur->notations);
1105
1106 if (cur->elements != NULL)
1107 xmlFreeElementTable((xmlElementTablePtr) cur->elements);
1108 if (cur->attributes != NULL)
1109 xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes);
1110 if (cur->entities != NULL)
1111 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities);
1112 if (cur->pentities != NULL)
1113 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->pentities);
1114
Owen Taylor3473f882001-02-23 17:55:21 +00001115 xmlFree(cur);
1116}
1117
1118/**
1119 * xmlNewDoc:
1120 * @version: xmlChar string giving the version of XML "1.0"
1121 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001122 * Creates a new XML document
1123 *
Owen Taylor3473f882001-02-23 17:55:21 +00001124 * Returns a new document
1125 */
1126xmlDocPtr
1127xmlNewDoc(const xmlChar *version) {
1128 xmlDocPtr cur;
1129
1130 if (version == NULL)
1131 version = (const xmlChar *) "1.0";
1132
1133 /*
1134 * Allocate a new document and fill the fields.
1135 */
1136 cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
1137 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001138 xmlTreeErrMemory("building doc");
Owen Taylor3473f882001-02-23 17:55:21 +00001139 return(NULL);
1140 }
1141 memset(cur, 0, sizeof(xmlDoc));
1142 cur->type = XML_DOCUMENT_NODE;
1143
1144 cur->version = xmlStrdup(version);
William M. Bracka3215c72004-07-31 16:24:01 +00001145 if (cur->version == NULL) {
1146 xmlTreeErrMemory("building doc");
1147 xmlFree(cur);
1148 return(NULL);
1149 }
Owen Taylor3473f882001-02-23 17:55:21 +00001150 cur->standalone = -1;
1151 cur->compression = -1; /* not initialized */
1152 cur->doc = cur;
Daniel Veillarda6874ca2003-07-29 16:47:24 +00001153 /*
1154 * The in memory encoding is always UTF8
1155 * This field will never change and would
1156 * be obsolete if not for binary compatibility.
1157 */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00001158 cur->charset = XML_CHAR_ENCODING_UTF8;
Daniel Veillard5335dc52003-01-01 20:59:38 +00001159
Daniel Veillarda880b122003-04-21 21:36:41 +00001160 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001161 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001162 return(cur);
1163}
1164
1165/**
1166 * xmlFreeDoc:
1167 * @cur: pointer to the document
Owen Taylor3473f882001-02-23 17:55:21 +00001168 *
1169 * Free up all the structures used by a document, tree included.
1170 */
1171void
1172xmlFreeDoc(xmlDocPtr cur) {
Daniel Veillarda9142e72001-06-19 11:07:54 +00001173 xmlDtdPtr extSubset, intSubset;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001174 xmlDictPtr dict = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001175
Owen Taylor3473f882001-02-23 17:55:21 +00001176 if (cur == NULL) {
1177#ifdef DEBUG_TREE
1178 xmlGenericError(xmlGenericErrorContext,
1179 "xmlFreeDoc : document == NULL\n");
1180#endif
1181 return;
1182 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001183#ifdef LIBXML_DEBUG_RUNTIME
Daniel Veillardbca3ad22005-08-23 22:14:02 +00001184#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001185 xmlDebugCheckDocument(stderr, cur);
1186#endif
Daniel Veillardbca3ad22005-08-23 22:14:02 +00001187#endif
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001188
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001189 if (cur != NULL) dict = cur->dict;
Daniel Veillard5335dc52003-01-01 20:59:38 +00001190
Daniel Veillarda880b122003-04-21 21:36:41 +00001191 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001192 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
1193
Daniel Veillard76d66f42001-05-16 21:05:17 +00001194 /*
1195 * Do this before freeing the children list to avoid ID lookups
1196 */
1197 if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids);
1198 cur->ids = NULL;
1199 if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
1200 cur->refs = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001201 extSubset = cur->extSubset;
1202 intSubset = cur->intSubset;
Daniel Veillard5997aca2002-03-18 18:36:20 +00001203 if (intSubset == extSubset)
1204 extSubset = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001205 if (extSubset != NULL) {
Daniel Veillard76d66f42001-05-16 21:05:17 +00001206 xmlUnlinkNode((xmlNodePtr) cur->extSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001207 cur->extSubset = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001208 xmlFreeDtd(extSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001209 }
Daniel Veillarda9142e72001-06-19 11:07:54 +00001210 if (intSubset != NULL) {
Daniel Veillard76d66f42001-05-16 21:05:17 +00001211 xmlUnlinkNode((xmlNodePtr) cur->intSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001212 cur->intSubset = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001213 xmlFreeDtd(intSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001214 }
1215
1216 if (cur->children != NULL) xmlFreeNodeList(cur->children);
Owen Taylor3473f882001-02-23 17:55:21 +00001217 if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001218
1219 DICT_FREE(cur->version)
1220 DICT_FREE(cur->name)
1221 DICT_FREE(cur->encoding)
1222 DICT_FREE(cur->URL)
Owen Taylor3473f882001-02-23 17:55:21 +00001223 xmlFree(cur);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001224 if (dict) xmlDictFree(dict);
Owen Taylor3473f882001-02-23 17:55:21 +00001225}
1226
1227/**
1228 * xmlStringLenGetNodeList:
1229 * @doc: the document
1230 * @value: the value of the text
1231 * @len: the length of the string value
1232 *
1233 * Parse the value string and build the node list associated. Should
1234 * produce a flat tree with only TEXTs and ENTITY_REFs.
1235 * Returns a pointer to the first child
1236 */
1237xmlNodePtr
1238xmlStringLenGetNodeList(xmlDocPtr doc, const xmlChar *value, int len) {
1239 xmlNodePtr ret = NULL, last = NULL;
1240 xmlNodePtr node;
1241 xmlChar *val;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001242 const xmlChar *cur = value, *end = cur + len;
Owen Taylor3473f882001-02-23 17:55:21 +00001243 const xmlChar *q;
1244 xmlEntityPtr ent;
1245
1246 if (value == NULL) return(NULL);
1247
1248 q = cur;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001249 while ((cur < end) && (*cur != 0)) {
1250 if (cur[0] == '&') {
1251 int charval = 0;
1252 xmlChar tmp;
1253
Owen Taylor3473f882001-02-23 17:55:21 +00001254 /*
1255 * Save the current text.
1256 */
1257 if (cur != q) {
1258 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
1259 xmlNodeAddContentLen(last, q, cur - q);
1260 } else {
1261 node = xmlNewDocTextLen(doc, q, cur - q);
1262 if (node == NULL) return(ret);
1263 if (last == NULL)
1264 last = ret = node;
1265 else {
1266 last->next = node;
1267 node->prev = last;
1268 last = node;
1269 }
1270 }
1271 }
Owen Taylor3473f882001-02-23 17:55:21 +00001272 q = cur;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001273 if ((cur + 2 < end) && (cur[1] == '#') && (cur[2] == 'x')) {
1274 cur += 3;
1275 if (cur < end)
1276 tmp = *cur;
1277 else
1278 tmp = 0;
1279 while (tmp != ';') { /* Non input consuming loop */
1280 if ((tmp >= '0') && (tmp <= '9'))
1281 charval = charval * 16 + (tmp - '0');
1282 else if ((tmp >= 'a') && (tmp <= 'f'))
1283 charval = charval * 16 + (tmp - 'a') + 10;
1284 else if ((tmp >= 'A') && (tmp <= 'F'))
1285 charval = charval * 16 + (tmp - 'A') + 10;
Owen Taylor3473f882001-02-23 17:55:21 +00001286 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001287 xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
1288 NULL);
Daniel Veillard07cb8222003-09-10 10:51:05 +00001289 charval = 0;
1290 break;
1291 }
1292 cur++;
1293 if (cur < end)
1294 tmp = *cur;
1295 else
1296 tmp = 0;
1297 }
1298 if (tmp == ';')
1299 cur++;
1300 q = cur;
1301 } else if ((cur + 1 < end) && (cur[1] == '#')) {
1302 cur += 2;
1303 if (cur < end)
1304 tmp = *cur;
1305 else
1306 tmp = 0;
1307 while (tmp != ';') { /* Non input consuming loops */
1308 if ((tmp >= '0') && (tmp <= '9'))
1309 charval = charval * 10 + (tmp - '0');
1310 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001311 xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
1312 NULL);
Daniel Veillard07cb8222003-09-10 10:51:05 +00001313 charval = 0;
1314 break;
1315 }
1316 cur++;
1317 if (cur < end)
1318 tmp = *cur;
1319 else
1320 tmp = 0;
1321 }
1322 if (tmp == ';')
1323 cur++;
1324 q = cur;
1325 } else {
1326 /*
1327 * Read the entity string
1328 */
1329 cur++;
1330 q = cur;
1331 while ((cur < end) && (*cur != 0) && (*cur != ';')) cur++;
1332 if ((cur >= end) || (*cur == 0)) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001333 xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY, (xmlNodePtr) doc,
1334 (const char *) q);
Daniel Veillard07cb8222003-09-10 10:51:05 +00001335 return(ret);
1336 }
1337 if (cur != q) {
1338 /*
1339 * Predefined entities don't generate nodes
1340 */
1341 val = xmlStrndup(q, cur - q);
1342 ent = xmlGetDocEntity(doc, val);
1343 if ((ent != NULL) &&
1344 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
1345 if (last == NULL) {
1346 node = xmlNewDocText(doc, ent->content);
1347 last = ret = node;
1348 } else if (last->type != XML_TEXT_NODE) {
1349 node = xmlNewDocText(doc, ent->content);
1350 last = xmlAddNextSibling(last, node);
1351 } else
1352 xmlNodeAddContent(last, ent->content);
1353
1354 } else {
1355 /*
1356 * Create a new REFERENCE_REF node
1357 */
1358 node = xmlNewReference(doc, val);
1359 if (node == NULL) {
1360 if (val != NULL) xmlFree(val);
1361 return(ret);
1362 }
1363 else if ((ent != NULL) && (ent->children == NULL)) {
1364 xmlNodePtr temp;
1365
1366 ent->children = xmlStringGetNodeList(doc,
1367 (const xmlChar*)node->content);
1368 ent->owner = 1;
1369 temp = ent->children;
1370 while (temp) {
1371 temp->parent = (xmlNodePtr)ent;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001372 ent->last = temp;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001373 temp = temp->next;
1374 }
1375 }
1376 if (last == NULL) {
1377 last = ret = node;
1378 } else {
1379 last = xmlAddNextSibling(last, node);
1380 }
1381 }
1382 xmlFree(val);
1383 }
1384 cur++;
1385 q = cur;
1386 }
1387 if (charval != 0) {
1388 xmlChar buf[10];
1389 int l;
1390
1391 l = xmlCopyCharMultiByte(buf, charval);
1392 buf[l] = 0;
1393 node = xmlNewDocText(doc, buf);
1394 if (node != NULL) {
1395 if (last == NULL) {
1396 last = ret = node;
1397 } else {
1398 last = xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001399 }
1400 }
Daniel Veillard07cb8222003-09-10 10:51:05 +00001401 charval = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001402 }
Daniel Veillard07cb8222003-09-10 10:51:05 +00001403 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001404 cur++;
1405 }
Daniel Veillard07cb8222003-09-10 10:51:05 +00001406 if ((cur != q) || (ret == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001407 /*
1408 * Handle the last piece of text.
1409 */
1410 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
1411 xmlNodeAddContentLen(last, q, cur - q);
1412 } else {
1413 node = xmlNewDocTextLen(doc, q, cur - q);
1414 if (node == NULL) return(ret);
Daniel Veillard07cb8222003-09-10 10:51:05 +00001415 if (last == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001416 last = ret = node;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001417 } else {
1418 last = xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001419 }
1420 }
1421 }
1422 return(ret);
1423}
1424
1425/**
1426 * xmlStringGetNodeList:
1427 * @doc: the document
1428 * @value: the value of the attribute
1429 *
1430 * Parse the value string and build the node list associated. Should
1431 * produce a flat tree with only TEXTs and ENTITY_REFs.
1432 * Returns a pointer to the first child
1433 */
1434xmlNodePtr
1435xmlStringGetNodeList(xmlDocPtr doc, const xmlChar *value) {
1436 xmlNodePtr ret = NULL, last = NULL;
1437 xmlNodePtr node;
1438 xmlChar *val;
1439 const xmlChar *cur = value;
1440 const xmlChar *q;
1441 xmlEntityPtr ent;
1442
1443 if (value == NULL) return(NULL);
1444
1445 q = cur;
1446 while (*cur != 0) {
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001447 if (cur[0] == '&') {
1448 int charval = 0;
1449 xmlChar tmp;
1450
Owen Taylor3473f882001-02-23 17:55:21 +00001451 /*
1452 * Save the current text.
1453 */
1454 if (cur != q) {
1455 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
1456 xmlNodeAddContentLen(last, q, cur - q);
1457 } else {
1458 node = xmlNewDocTextLen(doc, q, cur - q);
1459 if (node == NULL) return(ret);
1460 if (last == NULL)
1461 last = ret = node;
1462 else {
1463 last->next = node;
1464 node->prev = last;
1465 last = node;
1466 }
1467 }
1468 }
Owen Taylor3473f882001-02-23 17:55:21 +00001469 q = cur;
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001470 if ((cur[1] == '#') && (cur[2] == 'x')) {
1471 cur += 3;
1472 tmp = *cur;
1473 while (tmp != ';') { /* Non input consuming loop */
1474 if ((tmp >= '0') && (tmp <= '9'))
1475 charval = charval * 16 + (tmp - '0');
1476 else if ((tmp >= 'a') && (tmp <= 'f'))
1477 charval = charval * 16 + (tmp - 'a') + 10;
1478 else if ((tmp >= 'A') && (tmp <= 'F'))
1479 charval = charval * 16 + (tmp - 'A') + 10;
Owen Taylor3473f882001-02-23 17:55:21 +00001480 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001481 xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
1482 NULL);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001483 charval = 0;
1484 break;
1485 }
1486 cur++;
1487 tmp = *cur;
1488 }
1489 if (tmp == ';')
1490 cur++;
1491 q = cur;
1492 } else if (cur[1] == '#') {
1493 cur += 2;
1494 tmp = *cur;
1495 while (tmp != ';') { /* Non input consuming loops */
1496 if ((tmp >= '0') && (tmp <= '9'))
1497 charval = charval * 10 + (tmp - '0');
1498 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001499 xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
1500 NULL);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001501 charval = 0;
1502 break;
1503 }
1504 cur++;
1505 tmp = *cur;
1506 }
1507 if (tmp == ';')
1508 cur++;
1509 q = cur;
1510 } else {
1511 /*
1512 * Read the entity string
1513 */
1514 cur++;
1515 q = cur;
1516 while ((*cur != 0) && (*cur != ';')) cur++;
1517 if (*cur == 0) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001518 xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY,
1519 (xmlNodePtr) doc, (const char *) q);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001520 return(ret);
1521 }
1522 if (cur != q) {
1523 /*
1524 * Predefined entities don't generate nodes
1525 */
1526 val = xmlStrndup(q, cur - q);
1527 ent = xmlGetDocEntity(doc, val);
1528 if ((ent != NULL) &&
1529 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
1530 if (last == NULL) {
1531 node = xmlNewDocText(doc, ent->content);
1532 last = ret = node;
Daniel Veillard6f42c132002-01-06 23:05:13 +00001533 } else if (last->type != XML_TEXT_NODE) {
1534 node = xmlNewDocText(doc, ent->content);
1535 last = xmlAddNextSibling(last, node);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001536 } else
1537 xmlNodeAddContent(last, ent->content);
1538
1539 } else {
1540 /*
1541 * Create a new REFERENCE_REF node
1542 */
1543 node = xmlNewReference(doc, val);
1544 if (node == NULL) {
1545 if (val != NULL) xmlFree(val);
1546 return(ret);
1547 }
Daniel Veillardbf8dae82002-04-18 16:39:10 +00001548 else if ((ent != NULL) && (ent->children == NULL)) {
1549 xmlNodePtr temp;
1550
1551 ent->children = xmlStringGetNodeList(doc,
1552 (const xmlChar*)node->content);
Daniel Veillard2d84a892002-12-30 00:01:08 +00001553 ent->owner = 1;
Daniel Veillardbf8dae82002-04-18 16:39:10 +00001554 temp = ent->children;
1555 while (temp) {
1556 temp->parent = (xmlNodePtr)ent;
1557 temp = temp->next;
1558 }
1559 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001560 if (last == NULL) {
1561 last = ret = node;
1562 } else {
1563 last = xmlAddNextSibling(last, node);
1564 }
1565 }
1566 xmlFree(val);
1567 }
1568 cur++;
1569 q = cur;
1570 }
1571 if (charval != 0) {
1572 xmlChar buf[10];
1573 int len;
1574
1575 len = xmlCopyCharMultiByte(buf, charval);
1576 buf[len] = 0;
1577 node = xmlNewDocText(doc, buf);
1578 if (node != NULL) {
1579 if (last == NULL) {
1580 last = ret = node;
1581 } else {
1582 last = xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001583 }
1584 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001585
1586 charval = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001587 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001588 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001589 cur++;
1590 }
Daniel Veillard75bea542001-05-11 17:41:21 +00001591 if ((cur != q) || (ret == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001592 /*
1593 * Handle the last piece of text.
1594 */
1595 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
1596 xmlNodeAddContentLen(last, q, cur - q);
1597 } else {
1598 node = xmlNewDocTextLen(doc, q, cur - q);
1599 if (node == NULL) return(ret);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001600 if (last == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001601 last = ret = node;
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001602 } else {
1603 last = xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001604 }
1605 }
1606 }
1607 return(ret);
1608}
1609
1610/**
1611 * xmlNodeListGetString:
1612 * @doc: the document
1613 * @list: a Node list
1614 * @inLine: should we replace entity contents or show their external form
1615 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001616 * Build the string equivalent to the text contained in the Node list
Owen Taylor3473f882001-02-23 17:55:21 +00001617 * made of TEXTs and ENTITY_REFs
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001618 *
Daniel Veillardbd9afb52002-09-25 22:25:35 +00001619 * Returns a pointer to the string copy, the caller must free it with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00001620 */
1621xmlChar *
Daniel Veillard7646b182002-04-20 06:41:40 +00001622xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine)
1623{
Owen Taylor3473f882001-02-23 17:55:21 +00001624 xmlNodePtr node = list;
1625 xmlChar *ret = NULL;
1626 xmlEntityPtr ent;
1627
Daniel Veillard7646b182002-04-20 06:41:40 +00001628 if (list == NULL)
1629 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001630
1631 while (node != NULL) {
1632 if ((node->type == XML_TEXT_NODE) ||
Daniel Veillard7646b182002-04-20 06:41:40 +00001633 (node->type == XML_CDATA_SECTION_NODE)) {
1634 if (inLine) {
1635 ret = xmlStrcat(ret, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001636 } else {
Daniel Veillard7646b182002-04-20 06:41:40 +00001637 xmlChar *buffer;
Owen Taylor3473f882001-02-23 17:55:21 +00001638
Daniel Veillard7646b182002-04-20 06:41:40 +00001639 buffer = xmlEncodeEntitiesReentrant(doc, node->content);
1640 if (buffer != NULL) {
1641 ret = xmlStrcat(ret, buffer);
1642 xmlFree(buffer);
1643 }
1644 }
1645 } else if (node->type == XML_ENTITY_REF_NODE) {
1646 if (inLine) {
1647 ent = xmlGetDocEntity(doc, node->name);
1648 if (ent != NULL) {
1649 xmlChar *buffer;
1650
1651 /* an entity content can be any "well balanced chunk",
1652 * i.e. the result of the content [43] production:
1653 * http://www.w3.org/TR/REC-xml#NT-content.
1654 * So it can contain text, CDATA section or nested
1655 * entity reference nodes (among others).
1656 * -> we recursive call xmlNodeListGetString()
1657 * which handles these types */
1658 buffer = xmlNodeListGetString(doc, ent->children, 1);
1659 if (buffer != NULL) {
1660 ret = xmlStrcat(ret, buffer);
1661 xmlFree(buffer);
1662 }
1663 } else {
1664 ret = xmlStrcat(ret, node->content);
1665 }
1666 } else {
1667 xmlChar buf[2];
1668
1669 buf[0] = '&';
1670 buf[1] = 0;
1671 ret = xmlStrncat(ret, buf, 1);
1672 ret = xmlStrcat(ret, node->name);
1673 buf[0] = ';';
1674 buf[1] = 0;
1675 ret = xmlStrncat(ret, buf, 1);
1676 }
1677 }
1678#if 0
1679 else {
1680 xmlGenericError(xmlGenericErrorContext,
1681 "xmlGetNodeListString : invalid node type %d\n",
1682 node->type);
1683 }
1684#endif
1685 node = node->next;
1686 }
1687 return (ret);
1688}
Daniel Veillard652327a2003-09-29 18:02:38 +00001689
1690#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001691/**
1692 * xmlNodeListGetRawString:
1693 * @doc: the document
1694 * @list: a Node list
1695 * @inLine: should we replace entity contents or show their external form
1696 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001697 * Builds the string equivalent to the text contained in the Node list
Owen Taylor3473f882001-02-23 17:55:21 +00001698 * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString()
1699 * this function doesn't do any character encoding handling.
1700 *
Daniel Veillardbd9afb52002-09-25 22:25:35 +00001701 * Returns a pointer to the string copy, the caller must free it with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00001702 */
1703xmlChar *
Daniel Veillard7646b182002-04-20 06:41:40 +00001704xmlNodeListGetRawString(xmlDocPtr doc, xmlNodePtr list, int inLine)
1705{
Owen Taylor3473f882001-02-23 17:55:21 +00001706 xmlNodePtr node = list;
1707 xmlChar *ret = NULL;
1708 xmlEntityPtr ent;
1709
Daniel Veillard7646b182002-04-20 06:41:40 +00001710 if (list == NULL)
1711 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001712
1713 while (node != NULL) {
Daniel Veillard7db37732001-07-12 01:20:08 +00001714 if ((node->type == XML_TEXT_NODE) ||
Daniel Veillard7646b182002-04-20 06:41:40 +00001715 (node->type == XML_CDATA_SECTION_NODE)) {
1716 if (inLine) {
1717 ret = xmlStrcat(ret, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001718 } else {
Daniel Veillard7646b182002-04-20 06:41:40 +00001719 xmlChar *buffer;
1720
1721 buffer = xmlEncodeSpecialChars(doc, node->content);
1722 if (buffer != NULL) {
1723 ret = xmlStrcat(ret, buffer);
1724 xmlFree(buffer);
1725 }
1726 }
1727 } else if (node->type == XML_ENTITY_REF_NODE) {
1728 if (inLine) {
1729 ent = xmlGetDocEntity(doc, node->name);
1730 if (ent != NULL) {
1731 xmlChar *buffer;
1732
1733 /* an entity content can be any "well balanced chunk",
1734 * i.e. the result of the content [43] production:
1735 * http://www.w3.org/TR/REC-xml#NT-content.
1736 * So it can contain text, CDATA section or nested
1737 * entity reference nodes (among others).
1738 * -> we recursive call xmlNodeListGetRawString()
1739 * which handles these types */
1740 buffer =
1741 xmlNodeListGetRawString(doc, ent->children, 1);
1742 if (buffer != NULL) {
1743 ret = xmlStrcat(ret, buffer);
1744 xmlFree(buffer);
1745 }
1746 } else {
1747 ret = xmlStrcat(ret, node->content);
1748 }
1749 } else {
1750 xmlChar buf[2];
1751
1752 buf[0] = '&';
1753 buf[1] = 0;
1754 ret = xmlStrncat(ret, buf, 1);
1755 ret = xmlStrcat(ret, node->name);
1756 buf[0] = ';';
1757 buf[1] = 0;
1758 ret = xmlStrncat(ret, buf, 1);
1759 }
1760 }
Owen Taylor3473f882001-02-23 17:55:21 +00001761#if 0
Daniel Veillard7646b182002-04-20 06:41:40 +00001762 else {
1763 xmlGenericError(xmlGenericErrorContext,
1764 "xmlGetNodeListString : invalid node type %d\n",
1765 node->type);
1766 }
Owen Taylor3473f882001-02-23 17:55:21 +00001767#endif
Daniel Veillard7646b182002-04-20 06:41:40 +00001768 node = node->next;
Owen Taylor3473f882001-02-23 17:55:21 +00001769 }
Daniel Veillard7646b182002-04-20 06:41:40 +00001770 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00001771}
Daniel Veillard652327a2003-09-29 18:02:38 +00001772#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001773
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001774static xmlAttrPtr
1775xmlNewPropInternal(xmlNodePtr node, xmlNsPtr ns,
1776 const xmlChar * name, const xmlChar * value,
1777 int eatname)
1778{
Owen Taylor3473f882001-02-23 17:55:21 +00001779 xmlAttrPtr cur;
1780 xmlDocPtr doc = NULL;
1781
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001782 if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) {
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001783 if (eatname == 1)
1784 xmlFree((xmlChar *) name);
1785 return (NULL);
1786 }
Owen Taylor3473f882001-02-23 17:55:21 +00001787
1788 /*
1789 * Allocate a new property and fill the fields.
1790 */
1791 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1792 if (cur == NULL) {
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001793 if (eatname == 1)
1794 xmlFree((xmlChar *) name);
1795 xmlTreeErrMemory("building attribute");
1796 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001797 }
1798 memset(cur, 0, sizeof(xmlAttr));
1799 cur->type = XML_ATTRIBUTE_NODE;
1800
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001801 cur->parent = node;
Owen Taylor3473f882001-02-23 17:55:21 +00001802 if (node != NULL) {
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001803 doc = node->doc;
1804 cur->doc = doc;
Owen Taylor3473f882001-02-23 17:55:21 +00001805 }
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001806 cur->ns = ns;
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001807
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001808 if (eatname == 0) {
1809 if ((doc != NULL) && (doc->dict != NULL))
1810 cur->name = (xmlChar *) xmlDictLookup(doc->dict, name, -1);
1811 else
1812 cur->name = xmlStrdup(name);
1813 } else
1814 cur->name = name;
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001815
Owen Taylor3473f882001-02-23 17:55:21 +00001816 if (value != NULL) {
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001817 xmlChar *buffer;
1818 xmlNodePtr tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00001819
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001820 buffer = xmlEncodeEntitiesReentrant(doc, value);
1821 cur->children = xmlStringGetNodeList(doc, buffer);
1822 cur->last = NULL;
1823 tmp = cur->children;
1824 while (tmp != NULL) {
1825 tmp->parent = (xmlNodePtr) cur;
1826 if (tmp->next == NULL)
1827 cur->last = tmp;
1828 tmp = tmp->next;
1829 }
1830 xmlFree(buffer);
1831 }
Owen Taylor3473f882001-02-23 17:55:21 +00001832
1833 /*
1834 * Add it at the end to preserve parsing order ...
1835 */
1836 if (node != NULL) {
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001837 if (node->properties == NULL) {
1838 node->properties = cur;
1839 } else {
1840 xmlAttrPtr prev = node->properties;
Owen Taylor3473f882001-02-23 17:55:21 +00001841
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001842 while (prev->next != NULL)
1843 prev = prev->next;
1844 prev->next = cur;
1845 cur->prev = prev;
1846 }
Owen Taylor3473f882001-02-23 17:55:21 +00001847 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00001848
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001849 if (xmlIsID((node == NULL) ? NULL : node->doc, node, cur) == 1)
1850 xmlAddID(NULL, node->doc, value, cur);
1851
Daniel Veillarda880b122003-04-21 21:36:41 +00001852 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001853 xmlRegisterNodeDefaultValue((xmlNodePtr) cur);
1854 return (cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001855}
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001856
1857#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
1858 defined(LIBXML_SCHEMAS_ENABLED)
1859/**
1860 * xmlNewProp:
1861 * @node: the holding node
1862 * @name: the name of the attribute
1863 * @value: the value of the attribute
1864 *
1865 * Create a new property carried by a node.
1866 * Returns a pointer to the attribute
1867 */
1868xmlAttrPtr
1869xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
1870
1871 if (name == NULL) {
1872#ifdef DEBUG_TREE
1873 xmlGenericError(xmlGenericErrorContext,
1874 "xmlNewProp : name == NULL\n");
1875#endif
1876 return(NULL);
1877 }
1878
1879 return xmlNewPropInternal(node, NULL, name, value, 0);
1880}
Daniel Veillard652327a2003-09-29 18:02:38 +00001881#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001882
1883/**
1884 * xmlNewNsProp:
1885 * @node: the holding node
1886 * @ns: the namespace
1887 * @name: the name of the attribute
1888 * @value: the value of the attribute
1889 *
1890 * Create a new property tagged with a namespace and carried by a node.
1891 * Returns a pointer to the attribute
1892 */
1893xmlAttrPtr
1894xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
1895 const xmlChar *value) {
Owen Taylor3473f882001-02-23 17:55:21 +00001896
1897 if (name == NULL) {
1898#ifdef DEBUG_TREE
1899 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00001900 "xmlNewNsProp : name == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001901#endif
1902 return(NULL);
1903 }
1904
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001905 return xmlNewPropInternal(node, ns, name, value, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001906}
1907
1908/**
Daniel Veillard46de64e2002-05-29 08:21:33 +00001909 * xmlNewNsPropEatName:
1910 * @node: the holding node
1911 * @ns: the namespace
1912 * @name: the name of the attribute
1913 * @value: the value of the attribute
1914 *
1915 * Create a new property tagged with a namespace and carried by a node.
1916 * Returns a pointer to the attribute
1917 */
1918xmlAttrPtr
1919xmlNewNsPropEatName(xmlNodePtr node, xmlNsPtr ns, xmlChar *name,
1920 const xmlChar *value) {
Daniel Veillard46de64e2002-05-29 08:21:33 +00001921
1922 if (name == NULL) {
1923#ifdef DEBUG_TREE
1924 xmlGenericError(xmlGenericErrorContext,
1925 "xmlNewNsPropEatName : name == NULL\n");
1926#endif
1927 return(NULL);
1928 }
1929
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00001930 return xmlNewPropInternal(node, ns, name, value, 1);
Daniel Veillard46de64e2002-05-29 08:21:33 +00001931}
1932
1933/**
Owen Taylor3473f882001-02-23 17:55:21 +00001934 * xmlNewDocProp:
1935 * @doc: the document
1936 * @name: the name of the attribute
1937 * @value: the value of the attribute
1938 *
1939 * Create a new property carried by a document.
1940 * Returns a pointer to the attribute
1941 */
1942xmlAttrPtr
1943xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) {
1944 xmlAttrPtr cur;
1945
1946 if (name == NULL) {
1947#ifdef DEBUG_TREE
1948 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00001949 "xmlNewDocProp : name == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001950#endif
1951 return(NULL);
1952 }
1953
1954 /*
1955 * Allocate a new property and fill the fields.
1956 */
1957 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1958 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001959 xmlTreeErrMemory("building attribute");
Owen Taylor3473f882001-02-23 17:55:21 +00001960 return(NULL);
1961 }
1962 memset(cur, 0, sizeof(xmlAttr));
1963 cur->type = XML_ATTRIBUTE_NODE;
1964
Daniel Veillard03a53c32004-10-26 16:06:51 +00001965 if ((doc != NULL) && (doc->dict != NULL))
1966 cur->name = xmlDictLookup(doc->dict, name, -1);
1967 else
1968 cur->name = xmlStrdup(name);
Owen Taylor3473f882001-02-23 17:55:21 +00001969 cur->doc = doc;
1970 if (value != NULL) {
1971 xmlNodePtr tmp;
1972
1973 cur->children = xmlStringGetNodeList(doc, value);
1974 cur->last = NULL;
1975
1976 tmp = cur->children;
1977 while (tmp != NULL) {
1978 tmp->parent = (xmlNodePtr) cur;
1979 if (tmp->next == NULL)
1980 cur->last = tmp;
1981 tmp = tmp->next;
1982 }
1983 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00001984
Daniel Veillarda880b122003-04-21 21:36:41 +00001985 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001986 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001987 return(cur);
1988}
1989
1990/**
1991 * xmlFreePropList:
1992 * @cur: the first property in the list
1993 *
1994 * Free a property and all its siblings, all the children are freed too.
1995 */
1996void
1997xmlFreePropList(xmlAttrPtr cur) {
1998 xmlAttrPtr next;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001999 if (cur == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +00002000 while (cur != NULL) {
2001 next = cur->next;
2002 xmlFreeProp(cur);
2003 cur = next;
2004 }
2005}
2006
2007/**
2008 * xmlFreeProp:
2009 * @cur: an attribute
2010 *
2011 * Free one attribute, all the content is freed too
2012 */
2013void
2014xmlFreeProp(xmlAttrPtr cur) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002015 xmlDictPtr dict = NULL;
2016 if (cur == NULL) return;
2017
2018 if (cur->doc != NULL) dict = cur->doc->dict;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002019
Daniel Veillarda880b122003-04-21 21:36:41 +00002020 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002021 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
2022
Owen Taylor3473f882001-02-23 17:55:21 +00002023 /* Check for ID removal -> leading to invalid references ! */
Daniel Veillardda6f4af2005-06-20 17:17:54 +00002024 if ((cur->doc != NULL) && (cur->atype == XML_ATTRIBUTE_ID)) {
2025 xmlRemoveID(cur->doc, cur);
Daniel Veillard76d66f42001-05-16 21:05:17 +00002026 }
Owen Taylor3473f882001-02-23 17:55:21 +00002027 if (cur->children != NULL) xmlFreeNodeList(cur->children);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002028 DICT_FREE(cur->name)
Owen Taylor3473f882001-02-23 17:55:21 +00002029 xmlFree(cur);
2030}
2031
2032/**
2033 * xmlRemoveProp:
2034 * @cur: an attribute
2035 *
2036 * Unlink and free one attribute, all the content is freed too
2037 * Note this doesn't work for namespace definition attributes
2038 *
2039 * Returns 0 if success and -1 in case of error.
2040 */
2041int
2042xmlRemoveProp(xmlAttrPtr cur) {
2043 xmlAttrPtr tmp;
2044 if (cur == NULL) {
2045#ifdef DEBUG_TREE
2046 xmlGenericError(xmlGenericErrorContext,
2047 "xmlRemoveProp : cur == NULL\n");
2048#endif
2049 return(-1);
2050 }
2051 if (cur->parent == NULL) {
2052#ifdef DEBUG_TREE
2053 xmlGenericError(xmlGenericErrorContext,
2054 "xmlRemoveProp : cur->parent == NULL\n");
2055#endif
2056 return(-1);
2057 }
2058 tmp = cur->parent->properties;
2059 if (tmp == cur) {
2060 cur->parent->properties = cur->next;
Rob Richards19dc9612005-10-28 16:15:16 +00002061 if (cur->next != NULL)
2062 cur->next->prev = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002063 xmlFreeProp(cur);
2064 return(0);
2065 }
2066 while (tmp != NULL) {
2067 if (tmp->next == cur) {
2068 tmp->next = cur->next;
2069 if (tmp->next != NULL)
2070 tmp->next->prev = tmp;
2071 xmlFreeProp(cur);
2072 return(0);
2073 }
2074 tmp = tmp->next;
2075 }
2076#ifdef DEBUG_TREE
2077 xmlGenericError(xmlGenericErrorContext,
2078 "xmlRemoveProp : attribute not owned by its node\n");
2079#endif
2080 return(-1);
2081}
2082
2083/**
Daniel Veillard03a53c32004-10-26 16:06:51 +00002084 * xmlNewDocPI:
2085 * @doc: the target document
Owen Taylor3473f882001-02-23 17:55:21 +00002086 * @name: the processing instruction name
2087 * @content: the PI content
2088 *
2089 * Creation of a processing instruction element.
2090 * Returns a pointer to the new node object.
2091 */
2092xmlNodePtr
Daniel Veillard03a53c32004-10-26 16:06:51 +00002093xmlNewDocPI(xmlDocPtr doc, const xmlChar *name, const xmlChar *content) {
Owen Taylor3473f882001-02-23 17:55:21 +00002094 xmlNodePtr cur;
2095
2096 if (name == NULL) {
2097#ifdef DEBUG_TREE
2098 xmlGenericError(xmlGenericErrorContext,
2099 "xmlNewPI : name == NULL\n");
2100#endif
2101 return(NULL);
2102 }
2103
2104 /*
2105 * Allocate a new node and fill the fields.
2106 */
2107 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2108 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002109 xmlTreeErrMemory("building PI");
Owen Taylor3473f882001-02-23 17:55:21 +00002110 return(NULL);
2111 }
2112 memset(cur, 0, sizeof(xmlNode));
2113 cur->type = XML_PI_NODE;
2114
Daniel Veillard03a53c32004-10-26 16:06:51 +00002115 if ((doc != NULL) && (doc->dict != NULL))
2116 cur->name = xmlDictLookup(doc->dict, name, -1);
2117 else
2118 cur->name = xmlStrdup(name);
Owen Taylor3473f882001-02-23 17:55:21 +00002119 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002120 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00002121 }
Daniel Veillarda03e3652004-11-02 18:45:30 +00002122 cur->doc = doc;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002123
Daniel Veillarda880b122003-04-21 21:36:41 +00002124 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002125 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002126 return(cur);
2127}
2128
2129/**
Daniel Veillard03a53c32004-10-26 16:06:51 +00002130 * xmlNewPI:
2131 * @name: the processing instruction name
2132 * @content: the PI content
2133 *
2134 * Creation of a processing instruction element.
2135 * Use xmlDocNewPI preferably to get string interning
2136 *
2137 * Returns a pointer to the new node object.
2138 */
2139xmlNodePtr
2140xmlNewPI(const xmlChar *name, const xmlChar *content) {
2141 return(xmlNewDocPI(NULL, name, content));
2142}
2143
2144/**
Owen Taylor3473f882001-02-23 17:55:21 +00002145 * xmlNewNode:
2146 * @ns: namespace if any
2147 * @name: the node name
2148 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002149 * Creation of a new node element. @ns is optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00002150 *
William M. Brackd7cf7f82003-11-14 07:13:16 +00002151 * Returns a pointer to the new node object. Uses xmlStrdup() to make
2152 * copy of @name.
Owen Taylor3473f882001-02-23 17:55:21 +00002153 */
2154xmlNodePtr
2155xmlNewNode(xmlNsPtr ns, const xmlChar *name) {
2156 xmlNodePtr cur;
2157
2158 if (name == NULL) {
2159#ifdef DEBUG_TREE
2160 xmlGenericError(xmlGenericErrorContext,
2161 "xmlNewNode : name == NULL\n");
2162#endif
2163 return(NULL);
2164 }
2165
2166 /*
2167 * Allocate a new node and fill the fields.
2168 */
2169 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2170 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002171 xmlTreeErrMemory("building node");
Owen Taylor3473f882001-02-23 17:55:21 +00002172 return(NULL);
2173 }
2174 memset(cur, 0, sizeof(xmlNode));
2175 cur->type = XML_ELEMENT_NODE;
2176
2177 cur->name = xmlStrdup(name);
2178 cur->ns = ns;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002179
Daniel Veillarda880b122003-04-21 21:36:41 +00002180 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002181 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002182 return(cur);
2183}
2184
2185/**
Daniel Veillard46de64e2002-05-29 08:21:33 +00002186 * xmlNewNodeEatName:
2187 * @ns: namespace if any
2188 * @name: the node name
2189 *
2190 * Creation of a new node element. @ns is optional (NULL).
2191 *
William M. Brackd7cf7f82003-11-14 07:13:16 +00002192 * Returns a pointer to the new node object, with pointer @name as
2193 * new node's name. Use xmlNewNode() if a copy of @name string is
2194 * is needed as new node's name.
Daniel Veillard46de64e2002-05-29 08:21:33 +00002195 */
2196xmlNodePtr
2197xmlNewNodeEatName(xmlNsPtr ns, xmlChar *name) {
2198 xmlNodePtr cur;
2199
2200 if (name == NULL) {
2201#ifdef DEBUG_TREE
2202 xmlGenericError(xmlGenericErrorContext,
2203 "xmlNewNode : name == NULL\n");
2204#endif
2205 return(NULL);
2206 }
2207
2208 /*
2209 * Allocate a new node and fill the fields.
2210 */
2211 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2212 if (cur == NULL) {
Daniel Veillard5cd3e8c2005-03-27 11:25:28 +00002213 xmlFree(name);
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002214 xmlTreeErrMemory("building node");
Daniel Veillard46de64e2002-05-29 08:21:33 +00002215 return(NULL);
2216 }
2217 memset(cur, 0, sizeof(xmlNode));
2218 cur->type = XML_ELEMENT_NODE;
2219
2220 cur->name = name;
2221 cur->ns = ns;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002222
Daniel Veillarda880b122003-04-21 21:36:41 +00002223 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002224 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Daniel Veillard46de64e2002-05-29 08:21:33 +00002225 return(cur);
2226}
2227
2228/**
Owen Taylor3473f882001-02-23 17:55:21 +00002229 * xmlNewDocNode:
2230 * @doc: the document
2231 * @ns: namespace if any
2232 * @name: the node name
2233 * @content: the XML text content if any
2234 *
2235 * Creation of a new node element within a document. @ns and @content
Daniel Veillardd1640922001-12-17 15:30:10 +00002236 * are optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00002237 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2238 * references, but XML special chars need to be escaped first by using
2239 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2240 * need entities support.
2241 *
2242 * Returns a pointer to the new node object.
2243 */
2244xmlNodePtr
2245xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns,
2246 const xmlChar *name, const xmlChar *content) {
2247 xmlNodePtr cur;
2248
Daniel Veillard95ddcd32004-10-26 21:53:55 +00002249 if ((doc != NULL) && (doc->dict != NULL))
Daniel Veillard03a53c32004-10-26 16:06:51 +00002250 cur = xmlNewNodeEatName(ns, (xmlChar *)
2251 xmlDictLookup(doc->dict, name, -1));
2252 else
2253 cur = xmlNewNode(ns, name);
Owen Taylor3473f882001-02-23 17:55:21 +00002254 if (cur != NULL) {
2255 cur->doc = doc;
2256 if (content != NULL) {
2257 cur->children = xmlStringGetNodeList(doc, content);
2258 UPDATE_LAST_CHILD_AND_PARENT(cur)
2259 }
2260 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002261
Owen Taylor3473f882001-02-23 17:55:21 +00002262 return(cur);
2263}
2264
Daniel Veillard46de64e2002-05-29 08:21:33 +00002265/**
2266 * xmlNewDocNodeEatName:
2267 * @doc: the document
2268 * @ns: namespace if any
2269 * @name: the node name
2270 * @content: the XML text content if any
2271 *
2272 * Creation of a new node element within a document. @ns and @content
2273 * are optional (NULL).
2274 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2275 * references, but XML special chars need to be escaped first by using
2276 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2277 * need entities support.
2278 *
2279 * Returns a pointer to the new node object.
2280 */
2281xmlNodePtr
2282xmlNewDocNodeEatName(xmlDocPtr doc, xmlNsPtr ns,
2283 xmlChar *name, const xmlChar *content) {
2284 xmlNodePtr cur;
2285
2286 cur = xmlNewNodeEatName(ns, name);
2287 if (cur != NULL) {
2288 cur->doc = doc;
2289 if (content != NULL) {
2290 cur->children = xmlStringGetNodeList(doc, content);
2291 UPDATE_LAST_CHILD_AND_PARENT(cur)
2292 }
2293 }
2294 return(cur);
2295}
2296
Daniel Veillard652327a2003-09-29 18:02:38 +00002297#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002298/**
2299 * xmlNewDocRawNode:
2300 * @doc: the document
2301 * @ns: namespace if any
2302 * @name: the node name
2303 * @content: the text content if any
2304 *
2305 * Creation of a new node element within a document. @ns and @content
Daniel Veillardd1640922001-12-17 15:30:10 +00002306 * are optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00002307 *
2308 * Returns a pointer to the new node object.
2309 */
2310xmlNodePtr
2311xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns,
2312 const xmlChar *name, const xmlChar *content) {
2313 xmlNodePtr cur;
2314
Daniel Veillard95ddcd32004-10-26 21:53:55 +00002315 cur = xmlNewDocNode(doc, ns, name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002316 if (cur != NULL) {
2317 cur->doc = doc;
2318 if (content != NULL) {
2319 cur->children = xmlNewDocText(doc, content);
2320 UPDATE_LAST_CHILD_AND_PARENT(cur)
2321 }
2322 }
2323 return(cur);
2324}
2325
2326/**
2327 * xmlNewDocFragment:
2328 * @doc: the document owning the fragment
2329 *
2330 * Creation of a new Fragment node.
2331 * Returns a pointer to the new node object.
2332 */
2333xmlNodePtr
2334xmlNewDocFragment(xmlDocPtr doc) {
2335 xmlNodePtr cur;
2336
2337 /*
2338 * Allocate a new DocumentFragment node and fill the fields.
2339 */
2340 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2341 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002342 xmlTreeErrMemory("building fragment");
Owen Taylor3473f882001-02-23 17:55:21 +00002343 return(NULL);
2344 }
2345 memset(cur, 0, sizeof(xmlNode));
2346 cur->type = XML_DOCUMENT_FRAG_NODE;
2347
2348 cur->doc = doc;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002349
Daniel Veillarda880b122003-04-21 21:36:41 +00002350 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002351 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002352 return(cur);
2353}
Daniel Veillard652327a2003-09-29 18:02:38 +00002354#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002355
2356/**
2357 * xmlNewText:
2358 * @content: the text content
2359 *
2360 * Creation of a new text node.
2361 * Returns a pointer to the new node object.
2362 */
2363xmlNodePtr
2364xmlNewText(const xmlChar *content) {
2365 xmlNodePtr cur;
2366
2367 /*
2368 * Allocate a new node and fill the fields.
2369 */
2370 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2371 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002372 xmlTreeErrMemory("building text");
Owen Taylor3473f882001-02-23 17:55:21 +00002373 return(NULL);
2374 }
2375 memset(cur, 0, sizeof(xmlNode));
2376 cur->type = XML_TEXT_NODE;
2377
2378 cur->name = xmlStringText;
2379 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002380 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00002381 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002382
Daniel Veillarda880b122003-04-21 21:36:41 +00002383 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002384 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002385 return(cur);
2386}
2387
Daniel Veillard652327a2003-09-29 18:02:38 +00002388#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002389/**
2390 * xmlNewTextChild:
2391 * @parent: the parent node
2392 * @ns: a namespace if any
2393 * @name: the name of the child
2394 * @content: the text content of the child if any.
2395 *
2396 * Creation of a new child element, added at the end of @parent children list.
MST 2003 John Flecke1f70492003-12-20 23:43:28 +00002397 * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
2398 * created element inherits the namespace of @parent. If @content is non NULL,
William M. Brackd7cf7f82003-11-14 07:13:16 +00002399 * a child TEXT node will be created containing the string @content.
MST 2003 John Fleck8b03bc52003-12-17 03:45:01 +00002400 * NOTE: Use xmlNewChild() if @content will contain entities that need to be
2401 * preserved. Use this function, xmlNewTextChild(), if you need to ensure that
2402 * reserved XML chars that might appear in @content, such as the ampersand,
2403 * greater-than or less-than signs, are automatically replaced by their XML
2404 * escaped entity representations.
Owen Taylor3473f882001-02-23 17:55:21 +00002405 *
2406 * Returns a pointer to the new node object.
2407 */
2408xmlNodePtr
2409xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns,
2410 const xmlChar *name, const xmlChar *content) {
2411 xmlNodePtr cur, prev;
2412
2413 if (parent == NULL) {
2414#ifdef DEBUG_TREE
2415 xmlGenericError(xmlGenericErrorContext,
2416 "xmlNewTextChild : parent == NULL\n");
2417#endif
2418 return(NULL);
2419 }
2420
2421 if (name == NULL) {
2422#ifdef DEBUG_TREE
2423 xmlGenericError(xmlGenericErrorContext,
2424 "xmlNewTextChild : name == NULL\n");
2425#endif
2426 return(NULL);
2427 }
2428
2429 /*
2430 * Allocate a new node
2431 */
Daniel Veillard254b1262003-11-01 17:04:58 +00002432 if (parent->type == XML_ELEMENT_NODE) {
2433 if (ns == NULL)
2434 cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content);
2435 else
2436 cur = xmlNewDocRawNode(parent->doc, ns, name, content);
2437 } else if ((parent->type == XML_DOCUMENT_NODE) ||
2438 (parent->type == XML_HTML_DOCUMENT_NODE)) {
2439 if (ns == NULL)
2440 cur = xmlNewDocRawNode((xmlDocPtr) parent, NULL, name, content);
2441 else
2442 cur = xmlNewDocRawNode((xmlDocPtr) parent, ns, name, content);
2443 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2444 cur = xmlNewDocRawNode( parent->doc, ns, name, content);
2445 } else {
2446 return(NULL);
2447 }
Owen Taylor3473f882001-02-23 17:55:21 +00002448 if (cur == NULL) return(NULL);
2449
2450 /*
2451 * add the new element at the end of the children list.
2452 */
2453 cur->type = XML_ELEMENT_NODE;
2454 cur->parent = parent;
2455 cur->doc = parent->doc;
2456 if (parent->children == NULL) {
2457 parent->children = cur;
2458 parent->last = cur;
2459 } else {
2460 prev = parent->last;
2461 prev->next = cur;
2462 cur->prev = prev;
2463 parent->last = cur;
2464 }
2465
2466 return(cur);
2467}
Daniel Veillard652327a2003-09-29 18:02:38 +00002468#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002469
2470/**
2471 * xmlNewCharRef:
2472 * @doc: the document
2473 * @name: the char ref string, starting with # or "&# ... ;"
2474 *
2475 * Creation of a new character reference node.
2476 * Returns a pointer to the new node object.
2477 */
2478xmlNodePtr
2479xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) {
2480 xmlNodePtr cur;
2481
Daniel Veillard36e5cd52004-11-02 14:52:23 +00002482 if (name == NULL)
2483 return(NULL);
2484
Owen Taylor3473f882001-02-23 17:55:21 +00002485 /*
2486 * Allocate a new node and fill the fields.
2487 */
2488 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2489 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002490 xmlTreeErrMemory("building character reference");
Owen Taylor3473f882001-02-23 17:55:21 +00002491 return(NULL);
2492 }
2493 memset(cur, 0, sizeof(xmlNode));
2494 cur->type = XML_ENTITY_REF_NODE;
2495
2496 cur->doc = doc;
2497 if (name[0] == '&') {
2498 int len;
2499 name++;
2500 len = xmlStrlen(name);
2501 if (name[len - 1] == ';')
2502 cur->name = xmlStrndup(name, len - 1);
2503 else
2504 cur->name = xmlStrndup(name, len);
2505 } else
2506 cur->name = xmlStrdup(name);
Daniel Veillard5335dc52003-01-01 20:59:38 +00002507
Daniel Veillarda880b122003-04-21 21:36:41 +00002508 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002509 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002510 return(cur);
2511}
2512
2513/**
2514 * xmlNewReference:
2515 * @doc: the document
2516 * @name: the reference name, or the reference string with & and ;
2517 *
2518 * Creation of a new reference node.
2519 * Returns a pointer to the new node object.
2520 */
2521xmlNodePtr
2522xmlNewReference(xmlDocPtr doc, const xmlChar *name) {
2523 xmlNodePtr cur;
2524 xmlEntityPtr ent;
2525
Daniel Veillard36e5cd52004-11-02 14:52:23 +00002526 if (name == NULL)
2527 return(NULL);
2528
Owen Taylor3473f882001-02-23 17:55:21 +00002529 /*
2530 * Allocate a new node and fill the fields.
2531 */
2532 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2533 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002534 xmlTreeErrMemory("building reference");
Owen Taylor3473f882001-02-23 17:55:21 +00002535 return(NULL);
2536 }
2537 memset(cur, 0, sizeof(xmlNode));
2538 cur->type = XML_ENTITY_REF_NODE;
2539
2540 cur->doc = doc;
2541 if (name[0] == '&') {
2542 int len;
2543 name++;
2544 len = xmlStrlen(name);
2545 if (name[len - 1] == ';')
2546 cur->name = xmlStrndup(name, len - 1);
2547 else
2548 cur->name = xmlStrndup(name, len);
2549 } else
2550 cur->name = xmlStrdup(name);
2551
2552 ent = xmlGetDocEntity(doc, cur->name);
2553 if (ent != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002554 cur->content = ent->content;
Owen Taylor3473f882001-02-23 17:55:21 +00002555 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002556 * The parent pointer in entity is a DTD pointer and thus is NOT
Owen Taylor3473f882001-02-23 17:55:21 +00002557 * updated. Not sure if this is 100% correct.
2558 * -George
2559 */
2560 cur->children = (xmlNodePtr) ent;
2561 cur->last = (xmlNodePtr) ent;
2562 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002563
Daniel Veillarda880b122003-04-21 21:36:41 +00002564 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002565 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002566 return(cur);
2567}
2568
2569/**
2570 * xmlNewDocText:
2571 * @doc: the document
2572 * @content: the text content
2573 *
2574 * Creation of a new text node within a document.
2575 * Returns a pointer to the new node object.
2576 */
2577xmlNodePtr
2578xmlNewDocText(xmlDocPtr doc, const xmlChar *content) {
2579 xmlNodePtr cur;
2580
2581 cur = xmlNewText(content);
2582 if (cur != NULL) cur->doc = doc;
2583 return(cur);
2584}
2585
2586/**
2587 * xmlNewTextLen:
2588 * @content: the text content
2589 * @len: the text len.
2590 *
Daniel Veillard60087f32001-10-10 09:45:09 +00002591 * Creation of a new text node with an extra parameter for the content's length
Owen Taylor3473f882001-02-23 17:55:21 +00002592 * Returns a pointer to the new node object.
2593 */
2594xmlNodePtr
2595xmlNewTextLen(const xmlChar *content, int len) {
2596 xmlNodePtr cur;
2597
2598 /*
2599 * Allocate a new node and fill the fields.
2600 */
2601 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2602 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002603 xmlTreeErrMemory("building text");
Owen Taylor3473f882001-02-23 17:55:21 +00002604 return(NULL);
2605 }
2606 memset(cur, 0, sizeof(xmlNode));
2607 cur->type = XML_TEXT_NODE;
2608
2609 cur->name = xmlStringText;
2610 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002611 cur->content = xmlStrndup(content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00002612 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002613
Daniel Veillarda880b122003-04-21 21:36:41 +00002614 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002615 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002616 return(cur);
2617}
2618
2619/**
2620 * xmlNewDocTextLen:
2621 * @doc: the document
2622 * @content: the text content
2623 * @len: the text len.
2624 *
Daniel Veillard60087f32001-10-10 09:45:09 +00002625 * Creation of a new text node with an extra content length parameter. The
Owen Taylor3473f882001-02-23 17:55:21 +00002626 * text node pertain to a given document.
2627 * Returns a pointer to the new node object.
2628 */
2629xmlNodePtr
2630xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) {
2631 xmlNodePtr cur;
2632
2633 cur = xmlNewTextLen(content, len);
2634 if (cur != NULL) cur->doc = doc;
2635 return(cur);
2636}
2637
2638/**
2639 * xmlNewComment:
2640 * @content: the comment content
2641 *
2642 * Creation of a new node containing a comment.
2643 * Returns a pointer to the new node object.
2644 */
2645xmlNodePtr
2646xmlNewComment(const xmlChar *content) {
2647 xmlNodePtr cur;
2648
2649 /*
2650 * Allocate a new node and fill the fields.
2651 */
2652 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2653 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002654 xmlTreeErrMemory("building comment");
Owen Taylor3473f882001-02-23 17:55:21 +00002655 return(NULL);
2656 }
2657 memset(cur, 0, sizeof(xmlNode));
2658 cur->type = XML_COMMENT_NODE;
2659
2660 cur->name = xmlStringComment;
2661 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002662 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00002663 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002664
Daniel Veillarda880b122003-04-21 21:36:41 +00002665 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002666 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002667 return(cur);
2668}
2669
2670/**
2671 * xmlNewCDataBlock:
2672 * @doc: the document
Daniel Veillardd1640922001-12-17 15:30:10 +00002673 * @content: the CDATA block content content
Owen Taylor3473f882001-02-23 17:55:21 +00002674 * @len: the length of the block
2675 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002676 * Creation of a new node containing a CDATA block.
Owen Taylor3473f882001-02-23 17:55:21 +00002677 * Returns a pointer to the new node object.
2678 */
2679xmlNodePtr
2680xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) {
2681 xmlNodePtr cur;
2682
2683 /*
2684 * Allocate a new node and fill the fields.
2685 */
2686 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2687 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002688 xmlTreeErrMemory("building CDATA");
Owen Taylor3473f882001-02-23 17:55:21 +00002689 return(NULL);
2690 }
2691 memset(cur, 0, sizeof(xmlNode));
2692 cur->type = XML_CDATA_SECTION_NODE;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002693 cur->doc = doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002694
2695 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002696 cur->content = xmlStrndup(content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00002697 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002698
Daniel Veillarda880b122003-04-21 21:36:41 +00002699 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002700 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002701 return(cur);
2702}
2703
2704/**
2705 * xmlNewDocComment:
2706 * @doc: the document
2707 * @content: the comment content
2708 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002709 * Creation of a new node containing a comment within a document.
Owen Taylor3473f882001-02-23 17:55:21 +00002710 * Returns a pointer to the new node object.
2711 */
2712xmlNodePtr
2713xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) {
2714 xmlNodePtr cur;
2715
2716 cur = xmlNewComment(content);
2717 if (cur != NULL) cur->doc = doc;
2718 return(cur);
2719}
2720
2721/**
2722 * xmlSetTreeDoc:
2723 * @tree: the top element
2724 * @doc: the document
2725 *
2726 * update all nodes under the tree to point to the right document
2727 */
2728void
2729xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) {
Daniel Veillard19e96c32001-07-09 10:32:59 +00002730 xmlAttrPtr prop;
2731
Owen Taylor3473f882001-02-23 17:55:21 +00002732 if (tree == NULL)
2733 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002734 if (tree->doc != doc) {
Daniel Veillard36065812002-01-24 15:02:46 +00002735 if(tree->type == XML_ELEMENT_NODE) {
2736 prop = tree->properties;
2737 while (prop != NULL) {
2738 prop->doc = doc;
2739 xmlSetListDoc(prop->children, doc);
2740 prop = prop->next;
2741 }
Daniel Veillard19e96c32001-07-09 10:32:59 +00002742 }
Owen Taylor3473f882001-02-23 17:55:21 +00002743 if (tree->children != NULL)
2744 xmlSetListDoc(tree->children, doc);
2745 tree->doc = doc;
2746 }
2747}
2748
2749/**
2750 * xmlSetListDoc:
Daniel Veillardd1640922001-12-17 15:30:10 +00002751 * @list: the first element
Owen Taylor3473f882001-02-23 17:55:21 +00002752 * @doc: the document
2753 *
2754 * update all nodes in the list to point to the right document
2755 */
2756void
2757xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) {
2758 xmlNodePtr cur;
2759
2760 if (list == NULL)
2761 return;
2762 cur = list;
2763 while (cur != NULL) {
2764 if (cur->doc != doc)
2765 xmlSetTreeDoc(cur, doc);
2766 cur = cur->next;
2767 }
2768}
2769
Daniel Veillard2156d432004-03-04 15:59:36 +00002770#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00002771/**
2772 * xmlNewChild:
2773 * @parent: the parent node
2774 * @ns: a namespace if any
2775 * @name: the name of the child
2776 * @content: the XML content of the child if any.
2777 *
2778 * Creation of a new child element, added at the end of @parent children list.
MST 2003 John Flecke1f70492003-12-20 23:43:28 +00002779 * @ns and @content parameters are optional (NULL). If @ns is NULL, the newly
2780 * created element inherits the namespace of @parent. If @content is non NULL,
Owen Taylor3473f882001-02-23 17:55:21 +00002781 * a child list containing the TEXTs and ENTITY_REFs node will be created.
MST 2003 John Fleck8b03bc52003-12-17 03:45:01 +00002782 * NOTE: @content is supposed to be a piece of XML CDATA, so it allows entity
2783 * references. XML special chars must be escaped first by using
2784 * xmlEncodeEntitiesReentrant(), or xmlNewTextChild() should be used.
Owen Taylor3473f882001-02-23 17:55:21 +00002785 *
2786 * Returns a pointer to the new node object.
2787 */
2788xmlNodePtr
2789xmlNewChild(xmlNodePtr parent, xmlNsPtr ns,
2790 const xmlChar *name, const xmlChar *content) {
2791 xmlNodePtr cur, prev;
2792
2793 if (parent == NULL) {
2794#ifdef DEBUG_TREE
2795 xmlGenericError(xmlGenericErrorContext,
2796 "xmlNewChild : parent == NULL\n");
2797#endif
2798 return(NULL);
2799 }
2800
2801 if (name == NULL) {
2802#ifdef DEBUG_TREE
2803 xmlGenericError(xmlGenericErrorContext,
2804 "xmlNewChild : name == NULL\n");
2805#endif
2806 return(NULL);
2807 }
2808
2809 /*
2810 * Allocate a new node
2811 */
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002812 if (parent->type == XML_ELEMENT_NODE) {
2813 if (ns == NULL)
2814 cur = xmlNewDocNode(parent->doc, parent->ns, name, content);
2815 else
2816 cur = xmlNewDocNode(parent->doc, ns, name, content);
2817 } else if ((parent->type == XML_DOCUMENT_NODE) ||
2818 (parent->type == XML_HTML_DOCUMENT_NODE)) {
2819 if (ns == NULL)
2820 cur = xmlNewDocNode((xmlDocPtr) parent, NULL, name, content);
2821 else
2822 cur = xmlNewDocNode((xmlDocPtr) parent, ns, name, content);
Daniel Veillard7e3f1402002-10-28 18:52:57 +00002823 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2824 cur = xmlNewDocNode( parent->doc, ns, name, content);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002825 } else {
2826 return(NULL);
2827 }
Owen Taylor3473f882001-02-23 17:55:21 +00002828 if (cur == NULL) return(NULL);
2829
2830 /*
2831 * add the new element at the end of the children list.
2832 */
2833 cur->type = XML_ELEMENT_NODE;
2834 cur->parent = parent;
2835 cur->doc = parent->doc;
2836 if (parent->children == NULL) {
2837 parent->children = cur;
2838 parent->last = cur;
2839 } else {
2840 prev = parent->last;
2841 prev->next = cur;
2842 cur->prev = prev;
2843 parent->last = cur;
2844 }
2845
2846 return(cur);
2847}
Daniel Veillard652327a2003-09-29 18:02:38 +00002848#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002849
2850/**
Rob Richards65815122006-02-25 17:13:33 +00002851 * xmlAddPropSibling:
2852 * @prev: the attribute to which @prop is added after
2853 * @cur: the base attribute passed to calling function
2854 * @prop: the new attribute
2855 *
2856 * Add a new attribute after @prev using @cur as base attribute.
2857 * When inserting before @cur, @prev is passed as @cur->prev.
2858 * When inserting after @cur, @prev is passed as @cur.
2859 * If an existing attribute is found it is detroyed prior to adding @prop.
2860 *
2861 * Returns the attribute being inserted or NULL in case of error.
2862 */
2863static xmlNodePtr
2864xmlAddPropSibling(xmlNodePtr prev, xmlNodePtr cur, xmlNodePtr prop) {
2865 xmlAttrPtr attr;
2866
2867 if (cur->type != XML_ATTRIBUTE_NODE)
2868 return(NULL);
2869
2870 /* check if an attribute with the same name exists */
2871 if (prop->ns == NULL)
2872 attr = xmlHasNsProp(cur->parent, prop->name, NULL);
2873 else
2874 attr = xmlHasNsProp(cur->parent, prop->name, prop->ns->href);
2875
2876 if (prop->doc != cur->doc) {
2877 xmlSetTreeDoc(prop, cur->doc);
2878 }
2879 prop->parent = cur->parent;
2880 prop->prev = prev;
2881 if (prev != NULL) {
2882 prop->next = prev->next;
2883 prev->next = prop;
2884 if (prop->next)
2885 prop->next->prev = prop;
2886 } else {
2887 prop->next = cur;
2888 cur->prev = prop;
2889 }
2890 if (prop->prev == NULL && prop->parent != NULL)
2891 prop->parent->properties = (xmlAttrPtr) prop;
2892 if ((attr != NULL) && (attr->type != XML_ATTRIBUTE_DECL)) {
2893 /* different instance, destroy it (attributes must be unique) */
2894 xmlRemoveProp((xmlAttrPtr) attr);
2895 }
2896 return prop;
2897}
2898
2899/**
Owen Taylor3473f882001-02-23 17:55:21 +00002900 * xmlAddNextSibling:
2901 * @cur: the child node
2902 * @elem: the new node
2903 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002904 * Add a new node @elem as the next sibling of @cur
2905 * If the new node was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00002906 * first unlinked from its existing context.
2907 * As a result of text merging @elem may be freed.
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002908 * If the new node is ATTRIBUTE, it is added into properties instead of children.
2909 * If there is an attribute with equal name, it is first destroyed.
Owen Taylor3473f882001-02-23 17:55:21 +00002910 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002911 * Returns the new node or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002912 */
2913xmlNodePtr
2914xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) {
2915 if (cur == NULL) {
2916#ifdef DEBUG_TREE
2917 xmlGenericError(xmlGenericErrorContext,
2918 "xmlAddNextSibling : cur == NULL\n");
2919#endif
2920 return(NULL);
2921 }
2922 if (elem == NULL) {
2923#ifdef DEBUG_TREE
2924 xmlGenericError(xmlGenericErrorContext,
2925 "xmlAddNextSibling : elem == NULL\n");
2926#endif
2927 return(NULL);
2928 }
2929
Rob Richards19dc9612005-10-28 16:15:16 +00002930 if (cur == elem) {
2931#ifdef DEBUG_TREE
2932 xmlGenericError(xmlGenericErrorContext,
2933 "xmlAddNextSibling : cur == elem\n");
2934#endif
2935 return(NULL);
2936 }
2937
Owen Taylor3473f882001-02-23 17:55:21 +00002938 xmlUnlinkNode(elem);
2939
2940 if (elem->type == XML_TEXT_NODE) {
2941 if (cur->type == XML_TEXT_NODE) {
Owen Taylor3473f882001-02-23 17:55:21 +00002942 xmlNodeAddContent(cur, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002943 xmlFreeNode(elem);
2944 return(cur);
2945 }
Daniel Veillard9e1c72d2001-08-31 20:03:19 +00002946 if ((cur->next != NULL) && (cur->next->type == XML_TEXT_NODE) &&
2947 (cur->name == cur->next->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002948 xmlChar *tmp;
2949
2950 tmp = xmlStrdup(elem->content);
2951 tmp = xmlStrcat(tmp, cur->next->content);
2952 xmlNodeSetContent(cur->next, tmp);
2953 xmlFree(tmp);
Owen Taylor3473f882001-02-23 17:55:21 +00002954 xmlFreeNode(elem);
2955 return(cur->next);
2956 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002957 } else if (elem->type == XML_ATTRIBUTE_NODE) {
Rob Richards65815122006-02-25 17:13:33 +00002958 return xmlAddPropSibling(cur, cur, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00002959 }
2960
2961 if (elem->doc != cur->doc) {
2962 xmlSetTreeDoc(elem, cur->doc);
2963 }
2964 elem->parent = cur->parent;
2965 elem->prev = cur;
2966 elem->next = cur->next;
2967 cur->next = elem;
2968 if (elem->next != NULL)
2969 elem->next->prev = elem;
Rob Richards65815122006-02-25 17:13:33 +00002970 if ((elem->parent != NULL) && (elem->parent->last == cur))
Owen Taylor3473f882001-02-23 17:55:21 +00002971 elem->parent->last = elem;
2972 return(elem);
2973}
2974
William M. Brack21e4ef22005-01-02 09:53:13 +00002975#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
2976 defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00002977/**
2978 * xmlAddPrevSibling:
2979 * @cur: the child node
2980 * @elem: the new node
2981 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002982 * Add a new node @elem as the previous sibling of @cur
Owen Taylor3473f882001-02-23 17:55:21 +00002983 * merging adjacent TEXT nodes (@elem may be freed)
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002984 * If the new node was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00002985 * first unlinked from its existing context.
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002986 * If the new node is ATTRIBUTE, it is added into properties instead of children.
2987 * If there is an attribute with equal name, it is first destroyed.
Owen Taylor3473f882001-02-23 17:55:21 +00002988 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002989 * Returns the new node or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002990 */
2991xmlNodePtr
2992xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) {
2993 if (cur == NULL) {
2994#ifdef DEBUG_TREE
2995 xmlGenericError(xmlGenericErrorContext,
2996 "xmlAddPrevSibling : cur == NULL\n");
2997#endif
2998 return(NULL);
2999 }
3000 if (elem == NULL) {
3001#ifdef DEBUG_TREE
3002 xmlGenericError(xmlGenericErrorContext,
3003 "xmlAddPrevSibling : elem == NULL\n");
3004#endif
3005 return(NULL);
3006 }
3007
Rob Richards19dc9612005-10-28 16:15:16 +00003008 if (cur == elem) {
3009#ifdef DEBUG_TREE
3010 xmlGenericError(xmlGenericErrorContext,
3011 "xmlAddPrevSibling : cur == elem\n");
3012#endif
3013 return(NULL);
3014 }
3015
Owen Taylor3473f882001-02-23 17:55:21 +00003016 xmlUnlinkNode(elem);
3017
3018 if (elem->type == XML_TEXT_NODE) {
3019 if (cur->type == XML_TEXT_NODE) {
Owen Taylor3473f882001-02-23 17:55:21 +00003020 xmlChar *tmp;
3021
3022 tmp = xmlStrdup(elem->content);
3023 tmp = xmlStrcat(tmp, cur->content);
3024 xmlNodeSetContent(cur, tmp);
3025 xmlFree(tmp);
Owen Taylor3473f882001-02-23 17:55:21 +00003026 xmlFreeNode(elem);
3027 return(cur);
3028 }
Daniel Veillard9e1c72d2001-08-31 20:03:19 +00003029 if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE) &&
3030 (cur->name == cur->prev->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003031 xmlNodeAddContent(cur->prev, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003032 xmlFreeNode(elem);
3033 return(cur->prev);
3034 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003035 } else if (elem->type == XML_ATTRIBUTE_NODE) {
Rob Richards65815122006-02-25 17:13:33 +00003036 return xmlAddPropSibling(cur->prev, cur, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003037 }
3038
3039 if (elem->doc != cur->doc) {
3040 xmlSetTreeDoc(elem, cur->doc);
3041 }
3042 elem->parent = cur->parent;
3043 elem->next = cur;
3044 elem->prev = cur->prev;
3045 cur->prev = elem;
3046 if (elem->prev != NULL)
3047 elem->prev->next = elem;
Rob Richards65815122006-02-25 17:13:33 +00003048 if ((elem->parent != NULL) && (elem->parent->children == cur)) {
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003049 elem->parent->children = elem;
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003050 }
Owen Taylor3473f882001-02-23 17:55:21 +00003051 return(elem);
3052}
Daniel Veillard652327a2003-09-29 18:02:38 +00003053#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00003054
3055/**
3056 * xmlAddSibling:
3057 * @cur: the child node
3058 * @elem: the new node
3059 *
3060 * Add a new element @elem to the list of siblings of @cur
3061 * merging adjacent TEXT nodes (@elem may be freed)
3062 * If the new element was already inserted in a document it is
3063 * first unlinked from its existing context.
3064 *
3065 * Returns the new element or NULL in case of error.
3066 */
3067xmlNodePtr
3068xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) {
3069 xmlNodePtr parent;
3070
3071 if (cur == NULL) {
3072#ifdef DEBUG_TREE
3073 xmlGenericError(xmlGenericErrorContext,
3074 "xmlAddSibling : cur == NULL\n");
3075#endif
3076 return(NULL);
3077 }
3078
3079 if (elem == NULL) {
3080#ifdef DEBUG_TREE
3081 xmlGenericError(xmlGenericErrorContext,
3082 "xmlAddSibling : elem == NULL\n");
3083#endif
3084 return(NULL);
3085 }
3086
3087 /*
3088 * Constant time is we can rely on the ->parent->last to find
3089 * the last sibling.
3090 */
Rob Richards65815122006-02-25 17:13:33 +00003091 if ((cur->type != XML_ATTRIBUTE_NODE) && (cur->parent != NULL) &&
Owen Taylor3473f882001-02-23 17:55:21 +00003092 (cur->parent->children != NULL) &&
3093 (cur->parent->last != NULL) &&
3094 (cur->parent->last->next == NULL)) {
3095 cur = cur->parent->last;
3096 } else {
3097 while (cur->next != NULL) cur = cur->next;
3098 }
3099
3100 xmlUnlinkNode(elem);
3101
Daniel Veillarde22dd5c2003-10-29 12:53:27 +00003102 if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE) &&
3103 (cur->name == elem->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003104 xmlNodeAddContent(cur, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003105 xmlFreeNode(elem);
3106 return(cur);
Rob Richards65815122006-02-25 17:13:33 +00003107 } else if (elem->type == XML_ATTRIBUTE_NODE) {
3108 return xmlAddPropSibling(cur, cur, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003109 }
3110
3111 if (elem->doc != cur->doc) {
3112 xmlSetTreeDoc(elem, cur->doc);
3113 }
3114 parent = cur->parent;
3115 elem->prev = cur;
3116 elem->next = NULL;
3117 elem->parent = parent;
3118 cur->next = elem;
3119 if (parent != NULL)
3120 parent->last = elem;
3121
3122 return(elem);
3123}
3124
3125/**
3126 * xmlAddChildList:
3127 * @parent: the parent node
3128 * @cur: the first node in the list
3129 *
3130 * Add a list of node at the end of the child list of the parent
3131 * merging adjacent TEXT nodes (@cur may be freed)
3132 *
3133 * Returns the last child or NULL in case of error.
3134 */
3135xmlNodePtr
3136xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) {
3137 xmlNodePtr prev;
3138
3139 if (parent == NULL) {
3140#ifdef DEBUG_TREE
3141 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00003142 "xmlAddChildList : parent == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003143#endif
3144 return(NULL);
3145 }
3146
3147 if (cur == NULL) {
3148#ifdef DEBUG_TREE
3149 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00003150 "xmlAddChildList : child == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003151#endif
3152 return(NULL);
3153 }
3154
3155 if ((cur->doc != NULL) && (parent->doc != NULL) &&
3156 (cur->doc != parent->doc)) {
3157#ifdef DEBUG_TREE
3158 xmlGenericError(xmlGenericErrorContext,
3159 "Elements moved to a different document\n");
3160#endif
3161 }
3162
3163 /*
3164 * add the first element at the end of the children list.
3165 */
Daniel Veillard5335dc52003-01-01 20:59:38 +00003166
Owen Taylor3473f882001-02-23 17:55:21 +00003167 if (parent->children == NULL) {
3168 parent->children = cur;
3169 } else {
3170 /*
3171 * If cur and parent->last both are TEXT nodes, then merge them.
3172 */
3173 if ((cur->type == XML_TEXT_NODE) &&
3174 (parent->last->type == XML_TEXT_NODE) &&
3175 (cur->name == parent->last->name)) {
Daniel Veillard5335dc52003-01-01 20:59:38 +00003176 xmlNodeAddContent(parent->last, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003177 /*
3178 * if it's the only child, nothing more to be done.
3179 */
3180 if (cur->next == NULL) {
3181 xmlFreeNode(cur);
3182 return(parent->last);
3183 }
3184 prev = cur;
3185 cur = cur->next;
3186 xmlFreeNode(prev);
3187 }
3188 prev = parent->last;
3189 prev->next = cur;
3190 cur->prev = prev;
3191 }
3192 while (cur->next != NULL) {
3193 cur->parent = parent;
3194 if (cur->doc != parent->doc) {
3195 xmlSetTreeDoc(cur, parent->doc);
3196 }
3197 cur = cur->next;
3198 }
3199 cur->parent = parent;
3200 cur->doc = parent->doc; /* the parent may not be linked to a doc ! */
3201 parent->last = cur;
3202
3203 return(cur);
3204}
3205
3206/**
3207 * xmlAddChild:
3208 * @parent: the parent node
3209 * @cur: the child node
3210 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003211 * Add a new node to @parent, at the end of the child (or property) list
Owen Taylor3473f882001-02-23 17:55:21 +00003212 * merging adjacent TEXT nodes (in which case @cur is freed)
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003213 * If the new node is ATTRIBUTE, it is added into properties instead of children.
3214 * If there is an attribute with equal name, it is first destroyed.
3215 *
Owen Taylor3473f882001-02-23 17:55:21 +00003216 * Returns the child or NULL in case of error.
3217 */
3218xmlNodePtr
3219xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) {
3220 xmlNodePtr prev;
3221
3222 if (parent == NULL) {
3223#ifdef DEBUG_TREE
3224 xmlGenericError(xmlGenericErrorContext,
3225 "xmlAddChild : parent == NULL\n");
3226#endif
3227 return(NULL);
3228 }
3229
3230 if (cur == NULL) {
3231#ifdef DEBUG_TREE
3232 xmlGenericError(xmlGenericErrorContext,
3233 "xmlAddChild : child == NULL\n");
3234#endif
3235 return(NULL);
3236 }
3237
Rob Richards19dc9612005-10-28 16:15:16 +00003238 if (parent == cur) {
3239#ifdef DEBUG_TREE
3240 xmlGenericError(xmlGenericErrorContext,
3241 "xmlAddChild : parent == cur\n");
3242#endif
3243 return(NULL);
3244 }
Owen Taylor3473f882001-02-23 17:55:21 +00003245 /*
3246 * If cur is a TEXT node, merge its content with adjacent TEXT nodes
Owen Taylor3473f882001-02-23 17:55:21 +00003247 * cur is then freed.
3248 */
3249 if (cur->type == XML_TEXT_NODE) {
Daniel Veillard7db37732001-07-12 01:20:08 +00003250 if ((parent->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003251 (parent->content != NULL) &&
Rob Richards19dc9612005-10-28 16:15:16 +00003252 (parent->name == cur->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003253 xmlNodeAddContent(parent, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003254 xmlFreeNode(cur);
3255 return(parent);
3256 }
3257 if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003258 (parent->last->name == cur->name) &&
3259 (parent->last != cur)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003260 xmlNodeAddContent(parent->last, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003261 xmlFreeNode(cur);
3262 return(parent->last);
3263 }
3264 }
3265
3266 /*
3267 * add the new element at the end of the children list.
3268 */
Daniel Veillard5335dc52003-01-01 20:59:38 +00003269 prev = cur->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00003270 cur->parent = parent;
3271 if (cur->doc != parent->doc) {
3272 xmlSetTreeDoc(cur, parent->doc);
3273 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00003274 /* this check prevents a loop on tree-traversions if a developer
3275 * tries to add a node to its parent multiple times
3276 */
3277 if (prev == parent)
3278 return(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00003279
3280 /*
Daniel Veillard7db37732001-07-12 01:20:08 +00003281 * Coalescing
Owen Taylor3473f882001-02-23 17:55:21 +00003282 */
Daniel Veillard7db37732001-07-12 01:20:08 +00003283 if ((parent->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003284 (parent->content != NULL) &&
3285 (parent != cur)) {
Daniel Veillard7db37732001-07-12 01:20:08 +00003286 xmlNodeAddContent(parent, cur->content);
Daniel Veillard7db37732001-07-12 01:20:08 +00003287 xmlFreeNode(cur);
3288 return(parent);
Owen Taylor3473f882001-02-23 17:55:21 +00003289 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003290 if (cur->type == XML_ATTRIBUTE_NODE) {
Rob Richards19dc9612005-10-28 16:15:16 +00003291 if (parent->type != XML_ELEMENT_NODE)
3292 return(NULL);
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003293 if (parent->properties == NULL) {
3294 parent->properties = (xmlAttrPtr) cur;
3295 } else {
3296 /* check if an attribute with the same name exists */
3297 xmlAttrPtr lastattr;
Owen Taylor3473f882001-02-23 17:55:21 +00003298
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003299 if (cur->ns == NULL)
Rob Richardsc342ec62005-10-25 00:10:12 +00003300 lastattr = xmlHasNsProp(parent, cur->name, NULL);
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003301 else
3302 lastattr = xmlHasNsProp(parent, cur->name, cur->ns->href);
Rob Richardsc342ec62005-10-25 00:10:12 +00003303 if ((lastattr != NULL) && (lastattr != (xmlAttrPtr) cur) && (lastattr->type != XML_ATTRIBUTE_DECL)) {
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003304 /* different instance, destroy it (attributes must be unique) */
Rob Richards19dc9612005-10-28 16:15:16 +00003305 xmlUnlinkNode((xmlNodePtr) lastattr);
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003306 xmlFreeProp(lastattr);
3307 }
Rob Richards19dc9612005-10-28 16:15:16 +00003308 if (lastattr == (xmlAttrPtr) cur)
3309 return(cur);
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003310 /* find the end */
3311 lastattr = parent->properties;
3312 while (lastattr->next != NULL) {
3313 lastattr = lastattr->next;
3314 }
3315 lastattr->next = (xmlAttrPtr) cur;
3316 ((xmlAttrPtr) cur)->prev = lastattr;
3317 }
3318 } else {
3319 if (parent->children == NULL) {
3320 parent->children = cur;
3321 parent->last = cur;
3322 } else {
3323 prev = parent->last;
3324 prev->next = cur;
3325 cur->prev = prev;
3326 parent->last = cur;
3327 }
3328 }
Owen Taylor3473f882001-02-23 17:55:21 +00003329 return(cur);
3330}
3331
3332/**
3333 * xmlGetLastChild:
3334 * @parent: the parent node
3335 *
3336 * Search the last child of a node.
3337 * Returns the last child or NULL if none.
3338 */
3339xmlNodePtr
3340xmlGetLastChild(xmlNodePtr parent) {
3341 if (parent == NULL) {
3342#ifdef DEBUG_TREE
3343 xmlGenericError(xmlGenericErrorContext,
3344 "xmlGetLastChild : parent == NULL\n");
3345#endif
3346 return(NULL);
3347 }
3348 return(parent->last);
3349}
3350
3351/**
3352 * xmlFreeNodeList:
3353 * @cur: the first node in the list
3354 *
3355 * Free a node and all its siblings, this is a recursive behaviour, all
3356 * the children are freed too.
3357 */
3358void
3359xmlFreeNodeList(xmlNodePtr cur) {
3360 xmlNodePtr next;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003361 xmlDictPtr dict = NULL;
3362
3363 if (cur == NULL) return;
Daniel Veillarde6a55192002-01-14 17:11:53 +00003364 if (cur->type == XML_NAMESPACE_DECL) {
3365 xmlFreeNsList((xmlNsPtr) cur);
3366 return;
3367 }
Daniel Veillard9adc0462003-03-24 18:39:54 +00003368 if ((cur->type == XML_DOCUMENT_NODE) ||
3369#ifdef LIBXML_DOCB_ENABLED
3370 (cur->type == XML_DOCB_DOCUMENT_NODE) ||
Daniel Veillard9adc0462003-03-24 18:39:54 +00003371#endif
Daniel Veillard6560a422003-03-27 21:25:38 +00003372 (cur->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard9adc0462003-03-24 18:39:54 +00003373 xmlFreeDoc((xmlDocPtr) cur);
3374 return;
3375 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003376 if (cur->doc != NULL) dict = cur->doc->dict;
Owen Taylor3473f882001-02-23 17:55:21 +00003377 while (cur != NULL) {
3378 next = cur->next;
Daniel Veillard02141ea2001-04-30 11:46:40 +00003379 if (cur->type != XML_DTD_NODE) {
Daniel Veillard5335dc52003-01-01 20:59:38 +00003380
Daniel Veillarda880b122003-04-21 21:36:41 +00003381 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00003382 xmlDeregisterNodeDefaultValue(cur);
3383
Daniel Veillard02141ea2001-04-30 11:46:40 +00003384 if ((cur->children != NULL) &&
3385 (cur->type != XML_ENTITY_REF_NODE))
3386 xmlFreeNodeList(cur->children);
Daniel Veillard01c13b52002-12-10 15:19:08 +00003387 if (((cur->type == XML_ELEMENT_NODE) ||
3388 (cur->type == XML_XINCLUDE_START) ||
3389 (cur->type == XML_XINCLUDE_END)) &&
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003390 (cur->properties != NULL))
Daniel Veillard02141ea2001-04-30 11:46:40 +00003391 xmlFreePropList(cur->properties);
Daniel Veillard7db37732001-07-12 01:20:08 +00003392 if ((cur->type != XML_ELEMENT_NODE) &&
3393 (cur->type != XML_XINCLUDE_START) &&
3394 (cur->type != XML_XINCLUDE_END) &&
Daniel Veillard8874b942005-08-25 13:19:21 +00003395 (cur->type != XML_ENTITY_REF_NODE) &&
3396 (cur->content != (xmlChar *) &(cur->properties))) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003397 DICT_FREE(cur->content)
Daniel Veillard7db37732001-07-12 01:20:08 +00003398 }
3399 if (((cur->type == XML_ELEMENT_NODE) ||
3400 (cur->type == XML_XINCLUDE_START) ||
3401 (cur->type == XML_XINCLUDE_END)) &&
3402 (cur->nsDef != NULL))
3403 xmlFreeNsList(cur->nsDef);
3404
Daniel Veillard9cc6dc62001-06-11 08:09:20 +00003405 /*
3406 * When a node is a text node or a comment, it uses a global static
3407 * variable for the name of the node.
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003408 * Otherwise the node name might come from the document's
3409 * dictionnary
Daniel Veillard9cc6dc62001-06-11 08:09:20 +00003410 */
Daniel Veillard02141ea2001-04-30 11:46:40 +00003411 if ((cur->name != NULL) &&
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003412 (cur->type != XML_TEXT_NODE) &&
3413 (cur->type != XML_COMMENT_NODE))
3414 DICT_FREE(cur->name)
Daniel Veillard02141ea2001-04-30 11:46:40 +00003415 xmlFree(cur);
3416 }
Owen Taylor3473f882001-02-23 17:55:21 +00003417 cur = next;
3418 }
3419}
3420
3421/**
3422 * xmlFreeNode:
3423 * @cur: the node
3424 *
3425 * Free a node, this is a recursive behaviour, all the children are freed too.
3426 * This doesn't unlink the child from the list, use xmlUnlinkNode() first.
3427 */
3428void
3429xmlFreeNode(xmlNodePtr cur) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003430 xmlDictPtr dict = NULL;
3431
3432 if (cur == NULL) return;
Daniel Veillard5335dc52003-01-01 20:59:38 +00003433
Daniel Veillard02141ea2001-04-30 11:46:40 +00003434 /* use xmlFreeDtd for DTD nodes */
Daniel Veillarde6a55192002-01-14 17:11:53 +00003435 if (cur->type == XML_DTD_NODE) {
3436 xmlFreeDtd((xmlDtdPtr) cur);
Owen Taylor3473f882001-02-23 17:55:21 +00003437 return;
Daniel Veillarde6a55192002-01-14 17:11:53 +00003438 }
3439 if (cur->type == XML_NAMESPACE_DECL) {
3440 xmlFreeNs((xmlNsPtr) cur);
3441 return;
3442 }
Daniel Veillarda70d62f2002-11-07 14:18:03 +00003443 if (cur->type == XML_ATTRIBUTE_NODE) {
3444 xmlFreeProp((xmlAttrPtr) cur);
3445 return;
3446 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00003447
Daniel Veillarda880b122003-04-21 21:36:41 +00003448 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00003449 xmlDeregisterNodeDefaultValue(cur);
3450
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003451 if (cur->doc != NULL) dict = cur->doc->dict;
3452
Owen Taylor3473f882001-02-23 17:55:21 +00003453 if ((cur->children != NULL) &&
3454 (cur->type != XML_ENTITY_REF_NODE))
3455 xmlFreeNodeList(cur->children);
Daniel Veillard01c13b52002-12-10 15:19:08 +00003456 if (((cur->type == XML_ELEMENT_NODE) ||
3457 (cur->type == XML_XINCLUDE_START) ||
3458 (cur->type == XML_XINCLUDE_END)) &&
3459 (cur->properties != NULL))
Daniel Veillard02141ea2001-04-30 11:46:40 +00003460 xmlFreePropList(cur->properties);
Daniel Veillard7db37732001-07-12 01:20:08 +00003461 if ((cur->type != XML_ELEMENT_NODE) &&
3462 (cur->content != NULL) &&
3463 (cur->type != XML_ENTITY_REF_NODE) &&
3464 (cur->type != XML_XINCLUDE_END) &&
Daniel Veillard8874b942005-08-25 13:19:21 +00003465 (cur->type != XML_XINCLUDE_START) &&
3466 (cur->content != (xmlChar *) &(cur->properties))) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003467 DICT_FREE(cur->content)
Daniel Veillard7db37732001-07-12 01:20:08 +00003468 }
3469
Daniel Veillardacd370f2001-06-09 17:17:51 +00003470 /*
3471 * When a node is a text node or a comment, it uses a global static
3472 * variable for the name of the node.
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003473 * Otherwise the node name might come from the document's dictionnary
Daniel Veillardacd370f2001-06-09 17:17:51 +00003474 */
Owen Taylor3473f882001-02-23 17:55:21 +00003475 if ((cur->name != NULL) &&
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003476 (cur->type != XML_TEXT_NODE) &&
3477 (cur->type != XML_COMMENT_NODE))
3478 DICT_FREE(cur->name)
Daniel Veillardacd370f2001-06-09 17:17:51 +00003479
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003480 if (((cur->type == XML_ELEMENT_NODE) ||
3481 (cur->type == XML_XINCLUDE_START) ||
3482 (cur->type == XML_XINCLUDE_END)) &&
3483 (cur->nsDef != NULL))
3484 xmlFreeNsList(cur->nsDef);
Owen Taylor3473f882001-02-23 17:55:21 +00003485 xmlFree(cur);
3486}
3487
3488/**
3489 * xmlUnlinkNode:
3490 * @cur: the node
3491 *
3492 * Unlink a node from it's current context, the node is not freed
3493 */
3494void
3495xmlUnlinkNode(xmlNodePtr cur) {
3496 if (cur == NULL) {
3497#ifdef DEBUG_TREE
3498 xmlGenericError(xmlGenericErrorContext,
3499 "xmlUnlinkNode : node == NULL\n");
3500#endif
3501 return;
3502 }
Daniel Veillard29e43992001-12-13 22:21:58 +00003503 if (cur->type == XML_DTD_NODE) {
3504 xmlDocPtr doc;
3505 doc = cur->doc;
Daniel Veillarda067e652003-05-01 08:03:46 +00003506 if (doc != NULL) {
3507 if (doc->intSubset == (xmlDtdPtr) cur)
3508 doc->intSubset = NULL;
3509 if (doc->extSubset == (xmlDtdPtr) cur)
3510 doc->extSubset = NULL;
3511 }
Daniel Veillard29e43992001-12-13 22:21:58 +00003512 }
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003513 if (cur->parent != NULL) {
3514 xmlNodePtr parent;
3515 parent = cur->parent;
3516 if (cur->type == XML_ATTRIBUTE_NODE) {
3517 if (parent->properties == (xmlAttrPtr) cur)
3518 parent->properties = ((xmlAttrPtr) cur)->next;
3519 } else {
3520 if (parent->children == cur)
3521 parent->children = cur->next;
3522 if (parent->last == cur)
3523 parent->last = cur->prev;
3524 }
3525 cur->parent = NULL;
3526 }
Owen Taylor3473f882001-02-23 17:55:21 +00003527 if (cur->next != NULL)
3528 cur->next->prev = cur->prev;
3529 if (cur->prev != NULL)
3530 cur->prev->next = cur->next;
3531 cur->next = cur->prev = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00003532}
3533
Daniel Veillard2156d432004-03-04 15:59:36 +00003534#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00003535/**
3536 * xmlReplaceNode:
3537 * @old: the old node
3538 * @cur: the node
3539 *
William M. Brackd7cf7f82003-11-14 07:13:16 +00003540 * Unlink the old node from its current context, prune the new one
Daniel Veillardd1640922001-12-17 15:30:10 +00003541 * at the same place. If @cur was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00003542 * first unlinked from its existing context.
3543 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003544 * Returns the @old node
Owen Taylor3473f882001-02-23 17:55:21 +00003545 */
3546xmlNodePtr
3547xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) {
Daniel Veillardce244ad2004-11-05 10:03:46 +00003548 if (old == cur) return(NULL);
Daniel Veillarda03e3652004-11-02 18:45:30 +00003549 if ((old == NULL) || (old->parent == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003550#ifdef DEBUG_TREE
3551 xmlGenericError(xmlGenericErrorContext,
Daniel Veillarda03e3652004-11-02 18:45:30 +00003552 "xmlReplaceNode : old == NULL or without parent\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003553#endif
3554 return(NULL);
3555 }
3556 if (cur == NULL) {
3557 xmlUnlinkNode(old);
3558 return(old);
3559 }
3560 if (cur == old) {
3561 return(old);
3562 }
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003563 if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) {
3564#ifdef DEBUG_TREE
3565 xmlGenericError(xmlGenericErrorContext,
3566 "xmlReplaceNode : Trying to replace attribute node with other node type\n");
3567#endif
3568 return(old);
3569 }
3570 if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) {
3571#ifdef DEBUG_TREE
3572 xmlGenericError(xmlGenericErrorContext,
3573 "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n");
3574#endif
3575 return(old);
3576 }
Owen Taylor3473f882001-02-23 17:55:21 +00003577 xmlUnlinkNode(cur);
Daniel Veillard64d7d122005-05-11 18:03:42 +00003578 xmlSetTreeDoc(cur, old->doc);
Owen Taylor3473f882001-02-23 17:55:21 +00003579 cur->parent = old->parent;
3580 cur->next = old->next;
3581 if (cur->next != NULL)
3582 cur->next->prev = cur;
3583 cur->prev = old->prev;
3584 if (cur->prev != NULL)
3585 cur->prev->next = cur;
3586 if (cur->parent != NULL) {
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003587 if (cur->type == XML_ATTRIBUTE_NODE) {
3588 if (cur->parent->properties == (xmlAttrPtr)old)
3589 cur->parent->properties = ((xmlAttrPtr) cur);
3590 } else {
3591 if (cur->parent->children == old)
3592 cur->parent->children = cur;
3593 if (cur->parent->last == old)
3594 cur->parent->last = cur;
3595 }
Owen Taylor3473f882001-02-23 17:55:21 +00003596 }
3597 old->next = old->prev = NULL;
3598 old->parent = NULL;
3599 return(old);
3600}
Daniel Veillard652327a2003-09-29 18:02:38 +00003601#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00003602
3603/************************************************************************
3604 * *
3605 * Copy operations *
3606 * *
3607 ************************************************************************/
3608
3609/**
3610 * xmlCopyNamespace:
3611 * @cur: the namespace
3612 *
3613 * Do a copy of the namespace.
3614 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003615 * Returns: a new #xmlNsPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003616 */
3617xmlNsPtr
3618xmlCopyNamespace(xmlNsPtr cur) {
3619 xmlNsPtr ret;
3620
3621 if (cur == NULL) return(NULL);
3622 switch (cur->type) {
3623 case XML_LOCAL_NAMESPACE:
3624 ret = xmlNewNs(NULL, cur->href, cur->prefix);
3625 break;
3626 default:
3627#ifdef DEBUG_TREE
3628 xmlGenericError(xmlGenericErrorContext,
3629 "xmlCopyNamespace: invalid type %d\n", cur->type);
3630#endif
3631 return(NULL);
3632 }
3633 return(ret);
3634}
3635
3636/**
3637 * xmlCopyNamespaceList:
3638 * @cur: the first namespace
3639 *
3640 * Do a copy of an namespace list.
3641 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003642 * Returns: a new #xmlNsPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003643 */
3644xmlNsPtr
3645xmlCopyNamespaceList(xmlNsPtr cur) {
3646 xmlNsPtr ret = NULL;
3647 xmlNsPtr p = NULL,q;
3648
3649 while (cur != NULL) {
3650 q = xmlCopyNamespace(cur);
3651 if (p == NULL) {
3652 ret = p = q;
3653 } else {
3654 p->next = q;
3655 p = q;
3656 }
3657 cur = cur->next;
3658 }
3659 return(ret);
3660}
3661
3662static xmlNodePtr
3663xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent);
Rob Richards19dc9612005-10-28 16:15:16 +00003664
3665static xmlAttrPtr
3666xmlCopyPropInternal(xmlDocPtr doc, xmlNodePtr target, xmlAttrPtr cur) {
Owen Taylor3473f882001-02-23 17:55:21 +00003667 xmlAttrPtr ret;
3668
3669 if (cur == NULL) return(NULL);
3670 if (target != NULL)
3671 ret = xmlNewDocProp(target->doc, cur->name, NULL);
Rob Richards19dc9612005-10-28 16:15:16 +00003672 else if (doc != NULL)
3673 ret = xmlNewDocProp(doc, cur->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003674 else if (cur->parent != NULL)
3675 ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL);
3676 else if (cur->children != NULL)
3677 ret = xmlNewDocProp(cur->children->doc, cur->name, NULL);
3678 else
3679 ret = xmlNewDocProp(NULL, cur->name, NULL);
3680 if (ret == NULL) return(NULL);
3681 ret->parent = target;
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00003682
Owen Taylor3473f882001-02-23 17:55:21 +00003683 if ((cur->ns != NULL) && (target != NULL)) {
Daniel Veillard8107a222002-01-13 14:10:10 +00003684 xmlNsPtr ns;
Daniel Veillard652327a2003-09-29 18:02:38 +00003685
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00003686 ns = xmlSearchNs(target->doc, target, cur->ns->prefix);
3687 if (ns == NULL) {
3688 /*
3689 * Humm, we are copying an element whose namespace is defined
3690 * out of the new tree scope. Search it in the original tree
3691 * and add it at the top of the new tree
3692 */
3693 ns = xmlSearchNs(cur->doc, cur->parent, cur->ns->prefix);
3694 if (ns != NULL) {
3695 xmlNodePtr root = target;
3696 xmlNodePtr pred = NULL;
3697
3698 while (root->parent != NULL) {
3699 pred = root;
3700 root = root->parent;
3701 }
3702 if (root == (xmlNodePtr) target->doc) {
3703 /* correct possibly cycling above the document elt */
3704 root = pred;
3705 }
3706 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
3707 }
3708 } else {
3709 /*
3710 * we have to find something appropriate here since
3711 * we cant be sure, that the namespce we found is identified
3712 * by the prefix
3713 */
Daniel Veillard044fc6b2002-03-04 17:09:44 +00003714 if (xmlStrEqual(ns->href, cur->ns->href)) {
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00003715 /* this is the nice case */
3716 ret->ns = ns;
3717 } else {
3718 /*
3719 * we are in trouble: we need a new reconcilied namespace.
3720 * This is expensive
3721 */
3722 ret->ns = xmlNewReconciliedNs(target->doc, target, cur->ns);
3723 }
3724 }
3725
Owen Taylor3473f882001-02-23 17:55:21 +00003726 } else
3727 ret->ns = NULL;
3728
3729 if (cur->children != NULL) {
3730 xmlNodePtr tmp;
3731
3732 ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret);
3733 ret->last = NULL;
3734 tmp = ret->children;
3735 while (tmp != NULL) {
3736 /* tmp->parent = (xmlNodePtr)ret; */
3737 if (tmp->next == NULL)
3738 ret->last = tmp;
3739 tmp = tmp->next;
3740 }
3741 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00003742 /*
3743 * Try to handle IDs
3744 */
Daniel Veillarda3db2e32002-03-08 15:46:57 +00003745 if ((target!= NULL) && (cur!= NULL) &&
3746 (target->doc != NULL) && (cur->doc != NULL) &&
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00003747 (cur->doc->ids != NULL) && (cur->parent != NULL)) {
3748 if (xmlIsID(cur->doc, cur->parent, cur)) {
3749 xmlChar *id;
3750
3751 id = xmlNodeListGetString(cur->doc, cur->children, 1);
3752 if (id != NULL) {
3753 xmlAddID(NULL, target->doc, id, ret);
3754 xmlFree(id);
3755 }
3756 }
3757 }
Owen Taylor3473f882001-02-23 17:55:21 +00003758 return(ret);
3759}
3760
3761/**
Rob Richards19dc9612005-10-28 16:15:16 +00003762 * xmlCopyProp:
3763 * @target: the element where the attribute will be grafted
3764 * @cur: the attribute
3765 *
3766 * Do a copy of the attribute.
3767 *
3768 * Returns: a new #xmlAttrPtr, or NULL in case of error.
3769 */
3770xmlAttrPtr
3771xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) {
3772 return xmlCopyPropInternal(NULL, target, cur);
3773}
3774
3775/**
Owen Taylor3473f882001-02-23 17:55:21 +00003776 * xmlCopyPropList:
3777 * @target: the element where the attributes will be grafted
3778 * @cur: the first attribute
3779 *
3780 * Do a copy of an attribute list.
3781 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003782 * Returns: a new #xmlAttrPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003783 */
3784xmlAttrPtr
3785xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) {
3786 xmlAttrPtr ret = NULL;
3787 xmlAttrPtr p = NULL,q;
3788
3789 while (cur != NULL) {
3790 q = xmlCopyProp(target, cur);
William M. Brack13dfa872004-09-18 04:52:08 +00003791 if (q == NULL)
3792 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003793 if (p == NULL) {
3794 ret = p = q;
3795 } else {
3796 p->next = q;
3797 q->prev = p;
3798 p = q;
3799 }
3800 cur = cur->next;
3801 }
3802 return(ret);
3803}
3804
3805/*
Daniel Veillardd1640922001-12-17 15:30:10 +00003806 * NOTE about the CopyNode operations !
Owen Taylor3473f882001-02-23 17:55:21 +00003807 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003808 * They are split into external and internal parts for one
Owen Taylor3473f882001-02-23 17:55:21 +00003809 * tricky reason: namespaces. Doing a direct copy of a node
3810 * say RPM:Copyright without changing the namespace pointer to
3811 * something else can produce stale links. One way to do it is
3812 * to keep a reference counter but this doesn't work as soon
3813 * as one move the element or the subtree out of the scope of
3814 * the existing namespace. The actual solution seems to add
3815 * a copy of the namespace at the top of the copied tree if
3816 * not available in the subtree.
3817 * Hence two functions, the public front-end call the inner ones
William M. Brack57e9e912004-03-09 16:19:02 +00003818 * The argument "recursive" normally indicates a recursive copy
3819 * of the node with values 0 (no) and 1 (yes). For XInclude,
3820 * however, we allow a value of 2 to indicate copy properties and
3821 * namespace info, but don't recurse on children.
Owen Taylor3473f882001-02-23 17:55:21 +00003822 */
3823
3824static xmlNodePtr
Daniel Veillard3ec4c612001-08-28 20:39:49 +00003825xmlStaticCopyNode(const xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent,
William M. Brack57e9e912004-03-09 16:19:02 +00003826 int extended) {
Owen Taylor3473f882001-02-23 17:55:21 +00003827 xmlNodePtr ret;
3828
3829 if (node == NULL) return(NULL);
Daniel Veillard39196eb2001-06-19 18:09:42 +00003830 switch (node->type) {
3831 case XML_TEXT_NODE:
3832 case XML_CDATA_SECTION_NODE:
3833 case XML_ELEMENT_NODE:
Daniel Veillardec6725e2002-09-05 11:12:45 +00003834 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00003835 case XML_ENTITY_REF_NODE:
3836 case XML_ENTITY_NODE:
3837 case XML_PI_NODE:
3838 case XML_COMMENT_NODE:
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00003839 case XML_XINCLUDE_START:
3840 case XML_XINCLUDE_END:
3841 break;
3842 case XML_ATTRIBUTE_NODE:
Rob Richards19dc9612005-10-28 16:15:16 +00003843 return((xmlNodePtr) xmlCopyPropInternal(doc, parent, (xmlAttrPtr) node));
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00003844 case XML_NAMESPACE_DECL:
3845 return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node));
3846
Daniel Veillard39196eb2001-06-19 18:09:42 +00003847 case XML_DOCUMENT_NODE:
3848 case XML_HTML_DOCUMENT_NODE:
3849#ifdef LIBXML_DOCB_ENABLED
3850 case XML_DOCB_DOCUMENT_NODE:
3851#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00003852#ifdef LIBXML_TREE_ENABLED
William M. Brack57e9e912004-03-09 16:19:02 +00003853 return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, extended));
Daniel Veillard652327a2003-09-29 18:02:38 +00003854#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard39196eb2001-06-19 18:09:42 +00003855 case XML_DOCUMENT_TYPE_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00003856 case XML_NOTATION_NODE:
3857 case XML_DTD_NODE:
3858 case XML_ELEMENT_DECL:
3859 case XML_ATTRIBUTE_DECL:
3860 case XML_ENTITY_DECL:
3861 return(NULL);
3862 }
Daniel Veillardb33c2012001-04-25 12:59:04 +00003863
Owen Taylor3473f882001-02-23 17:55:21 +00003864 /*
3865 * Allocate a new node and fill the fields.
3866 */
3867 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
3868 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00003869 xmlTreeErrMemory("copying node");
Owen Taylor3473f882001-02-23 17:55:21 +00003870 return(NULL);
3871 }
3872 memset(ret, 0, sizeof(xmlNode));
3873 ret->type = node->type;
3874
3875 ret->doc = doc;
3876 ret->parent = parent;
3877 if (node->name == xmlStringText)
3878 ret->name = xmlStringText;
3879 else if (node->name == xmlStringTextNoenc)
3880 ret->name = xmlStringTextNoenc;
3881 else if (node->name == xmlStringComment)
3882 ret->name = xmlStringComment;
Daniel Veillard03a53c32004-10-26 16:06:51 +00003883 else if (node->name != NULL) {
3884 if ((doc != NULL) && (doc->dict != NULL))
3885 ret->name = xmlDictLookup(doc->dict, node->name, -1);
3886 else
3887 ret->name = xmlStrdup(node->name);
3888 }
Daniel Veillard7db37732001-07-12 01:20:08 +00003889 if ((node->type != XML_ELEMENT_NODE) &&
3890 (node->content != NULL) &&
3891 (node->type != XML_ENTITY_REF_NODE) &&
3892 (node->type != XML_XINCLUDE_END) &&
3893 (node->type != XML_XINCLUDE_START)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003894 ret->content = xmlStrdup(node->content);
Daniel Veillard8107a222002-01-13 14:10:10 +00003895 }else{
3896 if (node->type == XML_ELEMENT_NODE)
Daniel Veillard3e35f8e2003-10-21 00:05:38 +00003897 ret->line = node->line;
Owen Taylor3473f882001-02-23 17:55:21 +00003898 }
Daniel Veillardacb2bda2002-01-13 16:15:43 +00003899 if (parent != NULL) {
3900 xmlNodePtr tmp;
3901
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003902 /*
3903 * this is a tricky part for the node register thing:
3904 * in case ret does get coalesced in xmlAddChild
3905 * the deregister-node callback is called; so we register ret now already
3906 */
Daniel Veillarda880b122003-04-21 21:36:41 +00003907 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003908 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
3909
Daniel Veillardacb2bda2002-01-13 16:15:43 +00003910 tmp = xmlAddChild(parent, ret);
3911 /* node could have coalesced */
3912 if (tmp != ret)
3913 return(tmp);
3914 }
Owen Taylor3473f882001-02-23 17:55:21 +00003915
William M. Brack57e9e912004-03-09 16:19:02 +00003916 if (!extended)
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003917 goto out;
Daniel Veillard8874b942005-08-25 13:19:21 +00003918 if ((node->type == XML_ELEMENT_NODE) && (node->nsDef != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00003919 ret->nsDef = xmlCopyNamespaceList(node->nsDef);
3920
3921 if (node->ns != NULL) {
3922 xmlNsPtr ns;
3923
3924 ns = xmlSearchNs(doc, ret, node->ns->prefix);
3925 if (ns == NULL) {
3926 /*
3927 * Humm, we are copying an element whose namespace is defined
3928 * out of the new tree scope. Search it in the original tree
3929 * and add it at the top of the new tree
3930 */
3931 ns = xmlSearchNs(node->doc, node, node->ns->prefix);
3932 if (ns != NULL) {
3933 xmlNodePtr root = ret;
3934
3935 while (root->parent != NULL) root = root->parent;
Daniel Veillarde82a9922001-04-22 12:12:58 +00003936 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00003937 }
3938 } else {
3939 /*
3940 * reference the existing namespace definition in our own tree.
3941 */
3942 ret->ns = ns;
3943 }
3944 }
Daniel Veillard8874b942005-08-25 13:19:21 +00003945 if ((node->type == XML_ELEMENT_NODE) && (node->properties != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00003946 ret->properties = xmlCopyPropList(ret, node->properties);
Daniel Veillardb33c2012001-04-25 12:59:04 +00003947 if (node->type == XML_ENTITY_REF_NODE) {
3948 if ((doc == NULL) || (node->doc != doc)) {
3949 /*
3950 * The copied node will go into a separate document, so
Daniel Veillardd1640922001-12-17 15:30:10 +00003951 * to avoid dangling references to the ENTITY_DECL node
Daniel Veillardb33c2012001-04-25 12:59:04 +00003952 * we cannot keep the reference. Try to find it in the
3953 * target document.
3954 */
3955 ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name);
3956 } else {
3957 ret->children = node->children;
3958 }
Daniel Veillard0ec98632001-11-14 15:04:32 +00003959 ret->last = ret->children;
William M. Brack57e9e912004-03-09 16:19:02 +00003960 } else if ((node->children != NULL) && (extended != 2)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003961 ret->children = xmlStaticCopyNodeList(node->children, doc, ret);
Daniel Veillard0ec98632001-11-14 15:04:32 +00003962 UPDATE_LAST_CHILD_AND_PARENT(ret)
3963 }
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003964
3965out:
3966 /* if parent != NULL we already registered the node above */
Daniel Veillardac996a12004-07-30 12:02:58 +00003967 if ((parent == NULL) &&
3968 ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003969 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
Owen Taylor3473f882001-02-23 17:55:21 +00003970 return(ret);
3971}
3972
3973static xmlNodePtr
3974xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) {
3975 xmlNodePtr ret = NULL;
3976 xmlNodePtr p = NULL,q;
3977
3978 while (node != NULL) {
Daniel Veillard652327a2003-09-29 18:02:38 +00003979#ifdef LIBXML_TREE_ENABLED
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00003980 if (node->type == XML_DTD_NODE ) {
Daniel Veillard4497e692001-06-09 14:19:02 +00003981 if (doc == NULL) {
3982 node = node->next;
3983 continue;
3984 }
Daniel Veillardb33c2012001-04-25 12:59:04 +00003985 if (doc->intSubset == NULL) {
3986 q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node );
3987 q->doc = doc;
3988 q->parent = parent;
3989 doc->intSubset = (xmlDtdPtr) q;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00003990 xmlAddChild(parent, q);
Daniel Veillardb33c2012001-04-25 12:59:04 +00003991 } else {
3992 q = (xmlNodePtr) doc->intSubset;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00003993 xmlAddChild(parent, q);
Daniel Veillardb33c2012001-04-25 12:59:04 +00003994 }
3995 } else
Daniel Veillard652327a2003-09-29 18:02:38 +00003996#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardb33c2012001-04-25 12:59:04 +00003997 q = xmlStaticCopyNode(node, doc, parent, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00003998 if (ret == NULL) {
3999 q->prev = NULL;
4000 ret = p = q;
Daniel Veillardacb2bda2002-01-13 16:15:43 +00004001 } else if (p != q) {
4002 /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */
Owen Taylor3473f882001-02-23 17:55:21 +00004003 p->next = q;
4004 q->prev = p;
4005 p = q;
4006 }
4007 node = node->next;
4008 }
4009 return(ret);
4010}
4011
4012/**
4013 * xmlCopyNode:
4014 * @node: the node
William M. Brack57e9e912004-03-09 16:19:02 +00004015 * @extended: if 1 do a recursive copy (properties, namespaces and children
4016 * when applicable)
4017 * if 2 copy properties and namespaces (when applicable)
Owen Taylor3473f882001-02-23 17:55:21 +00004018 *
4019 * Do a copy of the node.
4020 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004021 * Returns: a new #xmlNodePtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004022 */
4023xmlNodePtr
William M. Brack57e9e912004-03-09 16:19:02 +00004024xmlCopyNode(const xmlNodePtr node, int extended) {
Owen Taylor3473f882001-02-23 17:55:21 +00004025 xmlNodePtr ret;
4026
William M. Brack57e9e912004-03-09 16:19:02 +00004027 ret = xmlStaticCopyNode(node, NULL, NULL, extended);
Owen Taylor3473f882001-02-23 17:55:21 +00004028 return(ret);
4029}
4030
4031/**
Daniel Veillard82daa812001-04-12 08:55:36 +00004032 * xmlDocCopyNode:
4033 * @node: the node
Daniel Veillardd1640922001-12-17 15:30:10 +00004034 * @doc: the document
William M. Brack57e9e912004-03-09 16:19:02 +00004035 * @extended: if 1 do a recursive copy (properties, namespaces and children
4036 * when applicable)
4037 * if 2 copy properties and namespaces (when applicable)
Daniel Veillard82daa812001-04-12 08:55:36 +00004038 *
4039 * Do a copy of the node to a given document.
4040 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004041 * Returns: a new #xmlNodePtr, or NULL in case of error.
Daniel Veillard82daa812001-04-12 08:55:36 +00004042 */
4043xmlNodePtr
William M. Brack57e9e912004-03-09 16:19:02 +00004044xmlDocCopyNode(const xmlNodePtr node, xmlDocPtr doc, int extended) {
Daniel Veillard82daa812001-04-12 08:55:36 +00004045 xmlNodePtr ret;
4046
William M. Brack57e9e912004-03-09 16:19:02 +00004047 ret = xmlStaticCopyNode(node, doc, NULL, extended);
Daniel Veillard82daa812001-04-12 08:55:36 +00004048 return(ret);
4049}
4050
4051/**
Daniel Veillard03a53c32004-10-26 16:06:51 +00004052 * xmlDocCopyNodeList:
4053 * @doc: the target document
4054 * @node: the first node in the list.
4055 *
4056 * Do a recursive copy of the node list.
4057 *
4058 * Returns: a new #xmlNodePtr, or NULL in case of error.
4059 */
4060xmlNodePtr xmlDocCopyNodeList(xmlDocPtr doc, const xmlNodePtr node) {
4061 xmlNodePtr ret = xmlStaticCopyNodeList(node, doc, NULL);
4062 return(ret);
4063}
4064
4065/**
Owen Taylor3473f882001-02-23 17:55:21 +00004066 * xmlCopyNodeList:
4067 * @node: the first node in the list.
4068 *
4069 * Do a recursive copy of the node list.
Daniel Veillard03a53c32004-10-26 16:06:51 +00004070 * Use xmlDocCopyNodeList() if possible to ensure string interning.
Owen Taylor3473f882001-02-23 17:55:21 +00004071 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004072 * Returns: a new #xmlNodePtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004073 */
Daniel Veillard3ec4c612001-08-28 20:39:49 +00004074xmlNodePtr xmlCopyNodeList(const xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00004075 xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL);
4076 return(ret);
4077}
4078
Daniel Veillard2156d432004-03-04 15:59:36 +00004079#if defined(LIBXML_TREE_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004080/**
Owen Taylor3473f882001-02-23 17:55:21 +00004081 * xmlCopyDtd:
4082 * @dtd: the dtd
4083 *
4084 * Do a copy of the dtd.
4085 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004086 * Returns: a new #xmlDtdPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004087 */
4088xmlDtdPtr
4089xmlCopyDtd(xmlDtdPtr dtd) {
4090 xmlDtdPtr ret;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004091 xmlNodePtr cur, p = NULL, q;
Owen Taylor3473f882001-02-23 17:55:21 +00004092
4093 if (dtd == NULL) return(NULL);
4094 ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID);
4095 if (ret == NULL) return(NULL);
4096 if (dtd->entities != NULL)
4097 ret->entities = (void *) xmlCopyEntitiesTable(
4098 (xmlEntitiesTablePtr) dtd->entities);
4099 if (dtd->notations != NULL)
4100 ret->notations = (void *) xmlCopyNotationTable(
4101 (xmlNotationTablePtr) dtd->notations);
4102 if (dtd->elements != NULL)
4103 ret->elements = (void *) xmlCopyElementTable(
4104 (xmlElementTablePtr) dtd->elements);
4105 if (dtd->attributes != NULL)
4106 ret->attributes = (void *) xmlCopyAttributeTable(
4107 (xmlAttributeTablePtr) dtd->attributes);
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004108 if (dtd->pentities != NULL)
4109 ret->pentities = (void *) xmlCopyEntitiesTable(
4110 (xmlEntitiesTablePtr) dtd->pentities);
4111
4112 cur = dtd->children;
4113 while (cur != NULL) {
4114 q = NULL;
4115
4116 if (cur->type == XML_ENTITY_DECL) {
4117 xmlEntityPtr tmp = (xmlEntityPtr) cur;
4118 switch (tmp->etype) {
4119 case XML_INTERNAL_GENERAL_ENTITY:
4120 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
4121 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
4122 q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name);
4123 break;
4124 case XML_INTERNAL_PARAMETER_ENTITY:
4125 case XML_EXTERNAL_PARAMETER_ENTITY:
4126 q = (xmlNodePtr)
4127 xmlGetParameterEntityFromDtd(ret, tmp->name);
4128 break;
4129 case XML_INTERNAL_PREDEFINED_ENTITY:
4130 break;
4131 }
4132 } else if (cur->type == XML_ELEMENT_DECL) {
4133 xmlElementPtr tmp = (xmlElementPtr) cur;
4134 q = (xmlNodePtr)
4135 xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix);
4136 } else if (cur->type == XML_ATTRIBUTE_DECL) {
4137 xmlAttributePtr tmp = (xmlAttributePtr) cur;
4138 q = (xmlNodePtr)
4139 xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix);
4140 } else if (cur->type == XML_COMMENT_NODE) {
4141 q = xmlCopyNode(cur, 0);
4142 }
4143
4144 if (q == NULL) {
4145 cur = cur->next;
4146 continue;
4147 }
4148
4149 if (p == NULL)
4150 ret->children = q;
4151 else
4152 p->next = q;
4153
4154 q->prev = p;
4155 q->parent = (xmlNodePtr) ret;
4156 q->next = NULL;
4157 ret->last = q;
4158 p = q;
4159 cur = cur->next;
4160 }
4161
Owen Taylor3473f882001-02-23 17:55:21 +00004162 return(ret);
4163}
Daniel Veillard2156d432004-03-04 15:59:36 +00004164#endif
Owen Taylor3473f882001-02-23 17:55:21 +00004165
Daniel Veillard2156d432004-03-04 15:59:36 +00004166#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004167/**
4168 * xmlCopyDoc:
4169 * @doc: the document
William M. Brack57e9e912004-03-09 16:19:02 +00004170 * @recursive: if not zero do a recursive copy.
Owen Taylor3473f882001-02-23 17:55:21 +00004171 *
4172 * Do a copy of the document info. If recursive, the content tree will
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004173 * be copied too as well as DTD, namespaces and entities.
Owen Taylor3473f882001-02-23 17:55:21 +00004174 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004175 * Returns: a new #xmlDocPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004176 */
4177xmlDocPtr
4178xmlCopyDoc(xmlDocPtr doc, int recursive) {
4179 xmlDocPtr ret;
4180
4181 if (doc == NULL) return(NULL);
4182 ret = xmlNewDoc(doc->version);
4183 if (ret == NULL) return(NULL);
4184 if (doc->name != NULL)
4185 ret->name = xmlMemStrdup(doc->name);
4186 if (doc->encoding != NULL)
4187 ret->encoding = xmlStrdup(doc->encoding);
Daniel Veillardf59507d2005-01-27 17:26:49 +00004188 if (doc->URL != NULL)
4189 ret->URL = xmlStrdup(doc->URL);
Owen Taylor3473f882001-02-23 17:55:21 +00004190 ret->charset = doc->charset;
4191 ret->compression = doc->compression;
4192 ret->standalone = doc->standalone;
4193 if (!recursive) return(ret);
4194
Daniel Veillardb33c2012001-04-25 12:59:04 +00004195 ret->last = NULL;
4196 ret->children = NULL;
Daniel Veillard2156d432004-03-04 15:59:36 +00004197#ifdef LIBXML_TREE_ENABLED
Daniel Veillardb33c2012001-04-25 12:59:04 +00004198 if (doc->intSubset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004199 ret->intSubset = xmlCopyDtd(doc->intSubset);
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004200 xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret);
Daniel Veillardb33c2012001-04-25 12:59:04 +00004201 ret->intSubset->parent = ret;
4202 }
Daniel Veillard2156d432004-03-04 15:59:36 +00004203#endif
Owen Taylor3473f882001-02-23 17:55:21 +00004204 if (doc->oldNs != NULL)
4205 ret->oldNs = xmlCopyNamespaceList(doc->oldNs);
4206 if (doc->children != NULL) {
4207 xmlNodePtr tmp;
Daniel Veillardb33c2012001-04-25 12:59:04 +00004208
4209 ret->children = xmlStaticCopyNodeList(doc->children, ret,
4210 (xmlNodePtr)ret);
Owen Taylor3473f882001-02-23 17:55:21 +00004211 ret->last = NULL;
4212 tmp = ret->children;
4213 while (tmp != NULL) {
4214 if (tmp->next == NULL)
4215 ret->last = tmp;
4216 tmp = tmp->next;
4217 }
4218 }
4219 return(ret);
4220}
Daniel Veillard652327a2003-09-29 18:02:38 +00004221#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004222
4223/************************************************************************
4224 * *
4225 * Content access functions *
4226 * *
4227 ************************************************************************/
4228
4229/**
Daniel Veillard8faa7832001-11-26 15:58:08 +00004230 * xmlGetLineNo:
Daniel Veillard01c13b52002-12-10 15:19:08 +00004231 * @node: valid node
Daniel Veillard8faa7832001-11-26 15:58:08 +00004232 *
William M. Brackd7cf7f82003-11-14 07:13:16 +00004233 * Get line number of @node. This requires activation of this option
Daniel Veillardd1640922001-12-17 15:30:10 +00004234 * before invoking the parser by calling xmlLineNumbersDefault(1)
Daniel Veillard8faa7832001-11-26 15:58:08 +00004235 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004236 * Returns the line number if successful, -1 otherwise
Daniel Veillard8faa7832001-11-26 15:58:08 +00004237 */
4238long
4239xmlGetLineNo(xmlNodePtr node)
4240{
4241 long result = -1;
4242
4243 if (!node)
4244 return result;
Daniel Veillard73da77e2005-08-24 14:05:37 +00004245 if ((node->type == XML_ELEMENT_NODE) ||
4246 (node->type == XML_TEXT_NODE) ||
4247 (node->type == XML_COMMENT_NODE) ||
4248 (node->type == XML_PI_NODE))
Daniel Veillard3e35f8e2003-10-21 00:05:38 +00004249 result = (long) node->line;
Daniel Veillard8faa7832001-11-26 15:58:08 +00004250 else if ((node->prev != NULL) &&
4251 ((node->prev->type == XML_ELEMENT_NODE) ||
Daniel Veillard73da77e2005-08-24 14:05:37 +00004252 (node->prev->type == XML_TEXT_NODE) ||
4253 (node->prev->type == XML_COMMENT_NODE) ||
4254 (node->prev->type == XML_PI_NODE)))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004255 result = xmlGetLineNo(node->prev);
4256 else if ((node->parent != NULL) &&
Daniel Veillard73da77e2005-08-24 14:05:37 +00004257 (node->parent->type == XML_ELEMENT_NODE))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004258 result = xmlGetLineNo(node->parent);
4259
4260 return result;
4261}
4262
Daniel Veillard2156d432004-03-04 15:59:36 +00004263#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED)
Daniel Veillard8faa7832001-11-26 15:58:08 +00004264/**
4265 * xmlGetNodePath:
4266 * @node: a node
4267 *
4268 * Build a structure based Path for the given node
4269 *
4270 * Returns the new path or NULL in case of error. The caller must free
4271 * the returned string
4272 */
4273xmlChar *
4274xmlGetNodePath(xmlNodePtr node)
4275{
4276 xmlNodePtr cur, tmp, next;
4277 xmlChar *buffer = NULL, *temp;
4278 size_t buf_len;
4279 xmlChar *buf;
Daniel Veillard96c3a3b2002-10-14 15:39:04 +00004280 const char *sep;
Daniel Veillard8faa7832001-11-26 15:58:08 +00004281 const char *name;
4282 char nametemp[100];
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004283 int occur = 0, generic;
Daniel Veillard8faa7832001-11-26 15:58:08 +00004284
4285 if (node == NULL)
4286 return (NULL);
4287
4288 buf_len = 500;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004289 buffer = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004290 if (buffer == NULL) {
4291 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004292 return (NULL);
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004293 }
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004294 buf = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
Daniel Veillard8faa7832001-11-26 15:58:08 +00004295 if (buf == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004296 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004297 xmlFree(buffer);
4298 return (NULL);
4299 }
4300
4301 buffer[0] = 0;
4302 cur = node;
4303 do {
4304 name = "";
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004305 sep = "?";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004306 occur = 0;
4307 if ((cur->type == XML_DOCUMENT_NODE) ||
4308 (cur->type == XML_HTML_DOCUMENT_NODE)) {
4309 if (buffer[0] == '/')
4310 break;
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004311 sep = "/";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004312 next = NULL;
4313 } else if (cur->type == XML_ELEMENT_NODE) {
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004314 generic = 0;
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004315 sep = "/";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004316 name = (const char *) cur->name;
4317 if (cur->ns) {
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004318 if (cur->ns->prefix != NULL) {
William M. Brack13dfa872004-09-18 04:52:08 +00004319 snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
4320 (char *)cur->ns->prefix, (char *)cur->name);
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004321 nametemp[sizeof(nametemp) - 1] = 0;
4322 name = nametemp;
4323 } else {
4324 /*
4325 * We cannot express named elements in the default
4326 * namespace, so use "*".
4327 */
4328 generic = 1;
4329 name = "*";
4330 }
Daniel Veillard8faa7832001-11-26 15:58:08 +00004331 }
4332 next = cur->parent;
4333
4334 /*
4335 * Thumbler index computation
Daniel Veillardc00cda82003-04-07 10:22:39 +00004336 * TODO: the ocurence test seems bogus for namespaced names
Daniel Veillard8faa7832001-11-26 15:58:08 +00004337 */
4338 tmp = cur->prev;
4339 while (tmp != NULL) {
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00004340 if ((tmp->type == XML_ELEMENT_NODE) &&
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004341 (generic ||
Kasimier T. Buchcik43ceb1e2006-06-12 11:08:18 +00004342 (xmlStrEqual(cur->name, tmp->name) &&
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004343 ((tmp->ns == cur->ns) ||
4344 ((tmp->ns != NULL) && (cur->ns != NULL) &&
Kasimier T. Buchcik43ceb1e2006-06-12 11:08:18 +00004345 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004346 occur++;
4347 tmp = tmp->prev;
4348 }
4349 if (occur == 0) {
4350 tmp = cur->next;
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004351 while (tmp != NULL && occur == 0) {
4352 if ((tmp->type == XML_ELEMENT_NODE) &&
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004353 (generic ||
Kasimier T. Buchcik43ceb1e2006-06-12 11:08:18 +00004354 (xmlStrEqual(cur->name, tmp->name) &&
Kasimier T. Buchcikd38c63f2006-06-12 10:58:24 +00004355 ((tmp->ns == cur->ns) ||
4356 ((tmp->ns != NULL) && (cur->ns != NULL) &&
Kasimier T. Buchcik43ceb1e2006-06-12 11:08:18 +00004357 (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix)))))))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004358 occur++;
4359 tmp = tmp->next;
4360 }
4361 if (occur != 0)
4362 occur = 1;
4363 } else
4364 occur++;
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004365 } else if (cur->type == XML_COMMENT_NODE) {
4366 sep = "/";
4367 name = "comment()";
4368 next = cur->parent;
4369
4370 /*
4371 * Thumbler index computation
4372 */
4373 tmp = cur->prev;
4374 while (tmp != NULL) {
4375 if (tmp->type == XML_COMMENT_NODE)
4376 occur++;
4377 tmp = tmp->prev;
4378 }
4379 if (occur == 0) {
4380 tmp = cur->next;
4381 while (tmp != NULL && occur == 0) {
4382 if (tmp->type == XML_COMMENT_NODE)
4383 occur++;
4384 tmp = tmp->next;
4385 }
4386 if (occur != 0)
4387 occur = 1;
4388 } else
4389 occur++;
4390 } else if ((cur->type == XML_TEXT_NODE) ||
4391 (cur->type == XML_CDATA_SECTION_NODE)) {
4392 sep = "/";
4393 name = "text()";
4394 next = cur->parent;
4395
4396 /*
4397 * Thumbler index computation
4398 */
4399 tmp = cur->prev;
4400 while (tmp != NULL) {
Kasimier T. Buchcikeb468702006-02-15 10:57:50 +00004401 if ((tmp->type == XML_TEXT_NODE) ||
4402 (tmp->type == XML_CDATA_SECTION_NODE))
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004403 occur++;
4404 tmp = tmp->prev;
4405 }
Kasimier T. Buchcikeb468702006-02-15 10:57:50 +00004406 /*
4407 * Evaluate if this is the only text- or CDATA-section-node;
4408 * if yes, then we'll get "text()", otherwise "text()[1]".
4409 */
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004410 if (occur == 0) {
4411 tmp = cur->next;
Kasimier T. Buchcikeb468702006-02-15 10:57:50 +00004412 while (tmp != NULL) {
4413 if ((tmp->type == XML_TEXT_NODE) ||
4414 (tmp->type == XML_CDATA_SECTION_NODE))
4415 {
4416 occur = 1;
4417 break;
4418 }
4419 tmp = tmp->next;
4420 }
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004421 } else
4422 occur++;
4423 } else if (cur->type == XML_PI_NODE) {
4424 sep = "/";
4425 snprintf(nametemp, sizeof(nametemp) - 1,
William M. Brack13dfa872004-09-18 04:52:08 +00004426 "processing-instruction('%s')", (char *)cur->name);
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004427 nametemp[sizeof(nametemp) - 1] = 0;
4428 name = nametemp;
4429
4430 next = cur->parent;
4431
4432 /*
4433 * Thumbler index computation
4434 */
4435 tmp = cur->prev;
4436 while (tmp != NULL) {
4437 if ((tmp->type == XML_PI_NODE) &&
4438 (xmlStrEqual(cur->name, tmp->name)))
4439 occur++;
4440 tmp = tmp->prev;
4441 }
4442 if (occur == 0) {
4443 tmp = cur->next;
4444 while (tmp != NULL && occur == 0) {
4445 if ((tmp->type == XML_PI_NODE) &&
4446 (xmlStrEqual(cur->name, tmp->name)))
4447 occur++;
4448 tmp = tmp->next;
4449 }
4450 if (occur != 0)
4451 occur = 1;
4452 } else
4453 occur++;
4454
Daniel Veillard8faa7832001-11-26 15:58:08 +00004455 } else if (cur->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004456 sep = "/@";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004457 name = (const char *) (((xmlAttrPtr) cur)->name);
Daniel Veillard365c8062005-07-19 11:31:55 +00004458 if (cur->ns) {
4459 if (cur->ns->prefix != NULL)
4460 snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s",
4461 (char *)cur->ns->prefix, (char *)cur->name);
4462 else
4463 snprintf(nametemp, sizeof(nametemp) - 1, "%s",
4464 (char *)cur->name);
4465 nametemp[sizeof(nametemp) - 1] = 0;
4466 name = nametemp;
4467 }
Daniel Veillard8faa7832001-11-26 15:58:08 +00004468 next = ((xmlAttrPtr) cur)->parent;
4469 } else {
4470 next = cur->parent;
4471 }
4472
4473 /*
4474 * Make sure there is enough room
4475 */
4476 if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) {
4477 buf_len =
4478 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20;
4479 temp = (xmlChar *) xmlRealloc(buffer, buf_len);
4480 if (temp == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004481 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004482 xmlFree(buf);
4483 xmlFree(buffer);
4484 return (NULL);
4485 }
4486 buffer = temp;
4487 temp = (xmlChar *) xmlRealloc(buf, buf_len);
4488 if (temp == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004489 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004490 xmlFree(buf);
4491 xmlFree(buffer);
4492 return (NULL);
4493 }
4494 buf = temp;
4495 }
4496 if (occur == 0)
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004497 snprintf((char *) buf, buf_len, "%s%s%s",
Daniel Veillard8faa7832001-11-26 15:58:08 +00004498 sep, name, (char *) buffer);
4499 else
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004500 snprintf((char *) buf, buf_len, "%s%s[%d]%s",
Daniel Veillard8faa7832001-11-26 15:58:08 +00004501 sep, name, occur, (char *) buffer);
William M. Brack13dfa872004-09-18 04:52:08 +00004502 snprintf((char *) buffer, buf_len, "%s", (char *)buf);
Daniel Veillard8faa7832001-11-26 15:58:08 +00004503 cur = next;
4504 } while (cur != NULL);
4505 xmlFree(buf);
4506 return (buffer);
4507}
Daniel Veillard652327a2003-09-29 18:02:38 +00004508#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard8faa7832001-11-26 15:58:08 +00004509
4510/**
Owen Taylor3473f882001-02-23 17:55:21 +00004511 * xmlDocGetRootElement:
4512 * @doc: the document
4513 *
4514 * Get the root element of the document (doc->children is a list
4515 * containing possibly comments, PIs, etc ...).
4516 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004517 * Returns the #xmlNodePtr for the root or NULL
Owen Taylor3473f882001-02-23 17:55:21 +00004518 */
4519xmlNodePtr
4520xmlDocGetRootElement(xmlDocPtr doc) {
4521 xmlNodePtr ret;
4522
4523 if (doc == NULL) return(NULL);
4524 ret = doc->children;
4525 while (ret != NULL) {
4526 if (ret->type == XML_ELEMENT_NODE)
4527 return(ret);
4528 ret = ret->next;
4529 }
4530 return(ret);
4531}
4532
Daniel Veillard2156d432004-03-04 15:59:36 +00004533#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_WRITER_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004534/**
4535 * xmlDocSetRootElement:
4536 * @doc: the document
4537 * @root: the new document root element
4538 *
4539 * Set the root element of the document (doc->children is a list
4540 * containing possibly comments, PIs, etc ...).
4541 *
4542 * Returns the old root element if any was found
4543 */
4544xmlNodePtr
4545xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) {
4546 xmlNodePtr old = NULL;
4547
4548 if (doc == NULL) return(NULL);
Daniel Veillardc575b992002-02-08 13:28:40 +00004549 if (root == NULL)
4550 return(NULL);
4551 xmlUnlinkNode(root);
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00004552 xmlSetTreeDoc(root, doc);
Daniel Veillardc575b992002-02-08 13:28:40 +00004553 root->parent = (xmlNodePtr) doc;
Owen Taylor3473f882001-02-23 17:55:21 +00004554 old = doc->children;
4555 while (old != NULL) {
4556 if (old->type == XML_ELEMENT_NODE)
4557 break;
4558 old = old->next;
4559 }
4560 if (old == NULL) {
4561 if (doc->children == NULL) {
4562 doc->children = root;
4563 doc->last = root;
4564 } else {
4565 xmlAddSibling(doc->children, root);
4566 }
4567 } else {
4568 xmlReplaceNode(old, root);
4569 }
4570 return(old);
4571}
Daniel Veillard2156d432004-03-04 15:59:36 +00004572#endif
Owen Taylor3473f882001-02-23 17:55:21 +00004573
Daniel Veillard2156d432004-03-04 15:59:36 +00004574#if defined(LIBXML_TREE_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004575/**
4576 * xmlNodeSetLang:
4577 * @cur: the node being changed
Daniel Veillardd1640922001-12-17 15:30:10 +00004578 * @lang: the language description
Owen Taylor3473f882001-02-23 17:55:21 +00004579 *
4580 * Set the language of a node, i.e. the values of the xml:lang
4581 * attribute.
4582 */
4583void
4584xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004585 xmlNsPtr ns;
4586
Owen Taylor3473f882001-02-23 17:55:21 +00004587 if (cur == NULL) return;
4588 switch(cur->type) {
4589 case XML_TEXT_NODE:
4590 case XML_CDATA_SECTION_NODE:
4591 case XML_COMMENT_NODE:
4592 case XML_DOCUMENT_NODE:
4593 case XML_DOCUMENT_TYPE_NODE:
4594 case XML_DOCUMENT_FRAG_NODE:
4595 case XML_NOTATION_NODE:
4596 case XML_HTML_DOCUMENT_NODE:
4597 case XML_DTD_NODE:
4598 case XML_ELEMENT_DECL:
4599 case XML_ATTRIBUTE_DECL:
4600 case XML_ENTITY_DECL:
4601 case XML_PI_NODE:
4602 case XML_ENTITY_REF_NODE:
4603 case XML_ENTITY_NODE:
4604 case XML_NAMESPACE_DECL:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004605#ifdef LIBXML_DOCB_ENABLED
4606 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004607#endif
4608 case XML_XINCLUDE_START:
4609 case XML_XINCLUDE_END:
4610 return;
4611 case XML_ELEMENT_NODE:
4612 case XML_ATTRIBUTE_NODE:
4613 break;
4614 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004615 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4616 if (ns == NULL)
4617 return;
4618 xmlSetNsProp(cur, ns, BAD_CAST "lang", lang);
Owen Taylor3473f882001-02-23 17:55:21 +00004619}
Daniel Veillard652327a2003-09-29 18:02:38 +00004620#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004621
4622/**
4623 * xmlNodeGetLang:
4624 * @cur: the node being checked
4625 *
4626 * Searches the language of a node, i.e. the values of the xml:lang
4627 * attribute or the one carried by the nearest ancestor.
4628 *
4629 * Returns a pointer to the lang value, or NULL if not found
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004630 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00004631 */
4632xmlChar *
4633xmlNodeGetLang(xmlNodePtr cur) {
4634 xmlChar *lang;
4635
4636 while (cur != NULL) {
Daniel Veillardc17337c2001-05-09 10:51:31 +00004637 lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE);
Owen Taylor3473f882001-02-23 17:55:21 +00004638 if (lang != NULL)
4639 return(lang);
4640 cur = cur->parent;
4641 }
4642 return(NULL);
4643}
4644
4645
Daniel Veillard652327a2003-09-29 18:02:38 +00004646#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00004647/**
4648 * xmlNodeSetSpacePreserve:
4649 * @cur: the node being changed
4650 * @val: the xml:space value ("0": default, 1: "preserve")
4651 *
4652 * Set (or reset) the space preserving behaviour of a node, i.e. the
4653 * value of the xml:space attribute.
4654 */
4655void
4656xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004657 xmlNsPtr ns;
4658
Owen Taylor3473f882001-02-23 17:55:21 +00004659 if (cur == NULL) return;
4660 switch(cur->type) {
4661 case XML_TEXT_NODE:
4662 case XML_CDATA_SECTION_NODE:
4663 case XML_COMMENT_NODE:
4664 case XML_DOCUMENT_NODE:
4665 case XML_DOCUMENT_TYPE_NODE:
4666 case XML_DOCUMENT_FRAG_NODE:
4667 case XML_NOTATION_NODE:
4668 case XML_HTML_DOCUMENT_NODE:
4669 case XML_DTD_NODE:
4670 case XML_ELEMENT_DECL:
4671 case XML_ATTRIBUTE_DECL:
4672 case XML_ENTITY_DECL:
4673 case XML_PI_NODE:
4674 case XML_ENTITY_REF_NODE:
4675 case XML_ENTITY_NODE:
4676 case XML_NAMESPACE_DECL:
4677 case XML_XINCLUDE_START:
4678 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004679#ifdef LIBXML_DOCB_ENABLED
4680 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004681#endif
4682 return;
4683 case XML_ELEMENT_NODE:
4684 case XML_ATTRIBUTE_NODE:
4685 break;
4686 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004687 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4688 if (ns == NULL)
4689 return;
Owen Taylor3473f882001-02-23 17:55:21 +00004690 switch (val) {
4691 case 0:
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004692 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "default");
Owen Taylor3473f882001-02-23 17:55:21 +00004693 break;
4694 case 1:
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004695 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "preserve");
Owen Taylor3473f882001-02-23 17:55:21 +00004696 break;
4697 }
4698}
Daniel Veillard652327a2003-09-29 18:02:38 +00004699#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004700
4701/**
4702 * xmlNodeGetSpacePreserve:
4703 * @cur: the node being checked
4704 *
4705 * Searches the space preserving behaviour of a node, i.e. the values
4706 * of the xml:space attribute or the one carried by the nearest
4707 * ancestor.
4708 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004709 * Returns -1 if xml:space is not inherited, 0 if "default", 1 if "preserve"
Owen Taylor3473f882001-02-23 17:55:21 +00004710 */
4711int
4712xmlNodeGetSpacePreserve(xmlNodePtr cur) {
4713 xmlChar *space;
4714
4715 while (cur != NULL) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004716 space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE);
Owen Taylor3473f882001-02-23 17:55:21 +00004717 if (space != NULL) {
4718 if (xmlStrEqual(space, BAD_CAST "preserve")) {
4719 xmlFree(space);
4720 return(1);
4721 }
4722 if (xmlStrEqual(space, BAD_CAST "default")) {
4723 xmlFree(space);
4724 return(0);
4725 }
4726 xmlFree(space);
4727 }
4728 cur = cur->parent;
4729 }
4730 return(-1);
4731}
4732
Daniel Veillard652327a2003-09-29 18:02:38 +00004733#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00004734/**
4735 * xmlNodeSetName:
4736 * @cur: the node being changed
4737 * @name: the new tag name
4738 *
4739 * Set (or reset) the name of a node.
4740 */
4741void
4742xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) {
Daniel Veillardce244ad2004-11-05 10:03:46 +00004743 xmlDocPtr doc;
4744 xmlDictPtr dict;
4745
Owen Taylor3473f882001-02-23 17:55:21 +00004746 if (cur == NULL) return;
4747 if (name == NULL) return;
4748 switch(cur->type) {
4749 case XML_TEXT_NODE:
4750 case XML_CDATA_SECTION_NODE:
4751 case XML_COMMENT_NODE:
4752 case XML_DOCUMENT_TYPE_NODE:
4753 case XML_DOCUMENT_FRAG_NODE:
4754 case XML_NOTATION_NODE:
4755 case XML_HTML_DOCUMENT_NODE:
4756 case XML_NAMESPACE_DECL:
4757 case XML_XINCLUDE_START:
4758 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004759#ifdef LIBXML_DOCB_ENABLED
4760 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004761#endif
4762 return;
4763 case XML_ELEMENT_NODE:
4764 case XML_ATTRIBUTE_NODE:
4765 case XML_PI_NODE:
4766 case XML_ENTITY_REF_NODE:
4767 case XML_ENTITY_NODE:
4768 case XML_DTD_NODE:
4769 case XML_DOCUMENT_NODE:
4770 case XML_ELEMENT_DECL:
4771 case XML_ATTRIBUTE_DECL:
4772 case XML_ENTITY_DECL:
4773 break;
4774 }
Daniel Veillardce244ad2004-11-05 10:03:46 +00004775 doc = cur->doc;
4776 if (doc != NULL)
4777 dict = doc->dict;
4778 else
4779 dict = NULL;
4780 if (dict != NULL) {
4781 if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name)))
4782 xmlFree((xmlChar *) cur->name);
4783 cur->name = xmlDictLookup(dict, name, -1);
4784 } else {
4785 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
4786 cur->name = xmlStrdup(name);
4787 }
Owen Taylor3473f882001-02-23 17:55:21 +00004788}
Daniel Veillard2156d432004-03-04 15:59:36 +00004789#endif
Owen Taylor3473f882001-02-23 17:55:21 +00004790
Daniel Veillard2156d432004-03-04 15:59:36 +00004791#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00004792/**
4793 * xmlNodeSetBase:
4794 * @cur: the node being changed
4795 * @uri: the new base URI
4796 *
4797 * Set (or reset) the base URI of a node, i.e. the value of the
4798 * xml:base attribute.
4799 */
4800void
Daniel Veillardf85ce8e2003-09-22 10:24:45 +00004801xmlNodeSetBase(xmlNodePtr cur, const xmlChar* uri) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004802 xmlNsPtr ns;
4803
Owen Taylor3473f882001-02-23 17:55:21 +00004804 if (cur == NULL) return;
4805 switch(cur->type) {
4806 case XML_TEXT_NODE:
4807 case XML_CDATA_SECTION_NODE:
4808 case XML_COMMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004809 case XML_DOCUMENT_TYPE_NODE:
4810 case XML_DOCUMENT_FRAG_NODE:
4811 case XML_NOTATION_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004812 case XML_DTD_NODE:
4813 case XML_ELEMENT_DECL:
4814 case XML_ATTRIBUTE_DECL:
4815 case XML_ENTITY_DECL:
4816 case XML_PI_NODE:
4817 case XML_ENTITY_REF_NODE:
4818 case XML_ENTITY_NODE:
4819 case XML_NAMESPACE_DECL:
4820 case XML_XINCLUDE_START:
4821 case XML_XINCLUDE_END:
Owen Taylor3473f882001-02-23 17:55:21 +00004822 return;
4823 case XML_ELEMENT_NODE:
4824 case XML_ATTRIBUTE_NODE:
4825 break;
Daniel Veillard4cbe4702002-05-05 06:57:27 +00004826 case XML_DOCUMENT_NODE:
4827#ifdef LIBXML_DOCB_ENABLED
4828 case XML_DOCB_DOCUMENT_NODE:
4829#endif
4830 case XML_HTML_DOCUMENT_NODE: {
4831 xmlDocPtr doc = (xmlDocPtr) cur;
4832
4833 if (doc->URL != NULL)
4834 xmlFree((xmlChar *) doc->URL);
4835 if (uri == NULL)
4836 doc->URL = NULL;
4837 else
4838 doc->URL = xmlStrdup(uri);
4839 return;
4840 }
Owen Taylor3473f882001-02-23 17:55:21 +00004841 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004842
4843 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4844 if (ns == NULL)
4845 return;
4846 xmlSetNsProp(cur, ns, BAD_CAST "base", uri);
Owen Taylor3473f882001-02-23 17:55:21 +00004847}
Daniel Veillard652327a2003-09-29 18:02:38 +00004848#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004849
4850/**
Owen Taylor3473f882001-02-23 17:55:21 +00004851 * xmlNodeGetBase:
4852 * @doc: the document the node pertains to
4853 * @cur: the node being checked
4854 *
4855 * Searches for the BASE URL. The code should work on both XML
4856 * and HTML document even if base mechanisms are completely different.
4857 * It returns the base as defined in RFC 2396 sections
4858 * 5.1.1. Base URI within Document Content
4859 * and
4860 * 5.1.2. Base URI from the Encapsulating Entity
4861 * However it does not return the document base (5.1.3), use
4862 * xmlDocumentGetBase() for this
4863 *
4864 * Returns a pointer to the base URL, or NULL if not found
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004865 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00004866 */
4867xmlChar *
4868xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00004869 xmlChar *oldbase = NULL;
4870 xmlChar *base, *newbase;
Owen Taylor3473f882001-02-23 17:55:21 +00004871
4872 if ((cur == NULL) && (doc == NULL))
4873 return(NULL);
4874 if (doc == NULL) doc = cur->doc;
4875 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
4876 cur = doc->children;
4877 while ((cur != NULL) && (cur->name != NULL)) {
4878 if (cur->type != XML_ELEMENT_NODE) {
4879 cur = cur->next;
4880 continue;
4881 }
4882 if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) {
4883 cur = cur->children;
4884 continue;
4885 }
4886 if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) {
4887 cur = cur->children;
4888 continue;
4889 }
4890 if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) {
4891 return(xmlGetProp(cur, BAD_CAST "href"));
4892 }
4893 cur = cur->next;
4894 }
4895 return(NULL);
4896 }
4897 while (cur != NULL) {
4898 if (cur->type == XML_ENTITY_DECL) {
4899 xmlEntityPtr ent = (xmlEntityPtr) cur;
4900 return(xmlStrdup(ent->URI));
4901 }
Daniel Veillard42596ad2001-05-22 16:57:14 +00004902 if (cur->type == XML_ELEMENT_NODE) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00004903 base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE);
Daniel Veillard42596ad2001-05-22 16:57:14 +00004904 if (base != NULL) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00004905 if (oldbase != NULL) {
4906 newbase = xmlBuildURI(oldbase, base);
4907 if (newbase != NULL) {
4908 xmlFree(oldbase);
4909 xmlFree(base);
4910 oldbase = newbase;
4911 } else {
4912 xmlFree(oldbase);
4913 xmlFree(base);
4914 return(NULL);
4915 }
4916 } else {
4917 oldbase = base;
4918 }
4919 if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) ||
4920 (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) ||
4921 (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4)))
4922 return(oldbase);
Daniel Veillard42596ad2001-05-22 16:57:14 +00004923 }
4924 }
Owen Taylor3473f882001-02-23 17:55:21 +00004925 cur = cur->parent;
4926 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00004927 if ((doc != NULL) && (doc->URL != NULL)) {
4928 if (oldbase == NULL)
4929 return(xmlStrdup(doc->URL));
4930 newbase = xmlBuildURI(oldbase, doc->URL);
4931 xmlFree(oldbase);
4932 return(newbase);
4933 }
4934 return(oldbase);
Owen Taylor3473f882001-02-23 17:55:21 +00004935}
4936
4937/**
Daniel Veillard78697292003-10-19 20:44:43 +00004938 * xmlNodeBufGetContent:
4939 * @buffer: a buffer
4940 * @cur: the node being read
4941 *
4942 * Read the value of a node @cur, this can be either the text carried
4943 * directly by this node if it's a TEXT node or the aggregate string
4944 * of the values carried by this node child's (TEXT and ENTITY_REF).
4945 * Entity references are substituted.
4946 * Fills up the buffer @buffer with this value
4947 *
4948 * Returns 0 in case of success and -1 in case of error.
4949 */
4950int
4951xmlNodeBufGetContent(xmlBufferPtr buffer, xmlNodePtr cur)
4952{
4953 if ((cur == NULL) || (buffer == NULL)) return(-1);
4954 switch (cur->type) {
4955 case XML_CDATA_SECTION_NODE:
4956 case XML_TEXT_NODE:
4957 xmlBufferCat(buffer, cur->content);
4958 break;
4959 case XML_DOCUMENT_FRAG_NODE:
4960 case XML_ELEMENT_NODE:{
4961 xmlNodePtr tmp = cur;
4962
4963 while (tmp != NULL) {
4964 switch (tmp->type) {
4965 case XML_CDATA_SECTION_NODE:
4966 case XML_TEXT_NODE:
4967 if (tmp->content != NULL)
4968 xmlBufferCat(buffer, tmp->content);
4969 break;
4970 case XML_ENTITY_REF_NODE:
Rob Richards77b92ff2005-12-20 15:55:14 +00004971 xmlNodeBufGetContent(buffer, tmp);
4972 break;
Daniel Veillard78697292003-10-19 20:44:43 +00004973 default:
4974 break;
4975 }
4976 /*
4977 * Skip to next node
4978 */
4979 if (tmp->children != NULL) {
4980 if (tmp->children->type != XML_ENTITY_DECL) {
4981 tmp = tmp->children;
4982 continue;
4983 }
4984 }
4985 if (tmp == cur)
4986 break;
4987
4988 if (tmp->next != NULL) {
4989 tmp = tmp->next;
4990 continue;
4991 }
4992
4993 do {
4994 tmp = tmp->parent;
4995 if (tmp == NULL)
4996 break;
4997 if (tmp == cur) {
4998 tmp = NULL;
4999 break;
5000 }
5001 if (tmp->next != NULL) {
5002 tmp = tmp->next;
5003 break;
5004 }
5005 } while (tmp != NULL);
5006 }
5007 break;
5008 }
5009 case XML_ATTRIBUTE_NODE:{
5010 xmlAttrPtr attr = (xmlAttrPtr) cur;
5011 xmlNodePtr tmp = attr->children;
5012
5013 while (tmp != NULL) {
5014 if (tmp->type == XML_TEXT_NODE)
5015 xmlBufferCat(buffer, tmp->content);
5016 else
5017 xmlNodeBufGetContent(buffer, tmp);
5018 tmp = tmp->next;
5019 }
5020 break;
5021 }
5022 case XML_COMMENT_NODE:
5023 case XML_PI_NODE:
5024 xmlBufferCat(buffer, cur->content);
5025 break;
5026 case XML_ENTITY_REF_NODE:{
5027 xmlEntityPtr ent;
5028 xmlNodePtr tmp;
5029
5030 /* lookup entity declaration */
5031 ent = xmlGetDocEntity(cur->doc, cur->name);
5032 if (ent == NULL)
5033 return(-1);
5034
5035 /* an entity content can be any "well balanced chunk",
5036 * i.e. the result of the content [43] production:
5037 * http://www.w3.org/TR/REC-xml#NT-content
5038 * -> we iterate through child nodes and recursive call
5039 * xmlNodeGetContent() which handles all possible node types */
5040 tmp = ent->children;
5041 while (tmp) {
5042 xmlNodeBufGetContent(buffer, tmp);
5043 tmp = tmp->next;
5044 }
5045 break;
5046 }
5047 case XML_ENTITY_NODE:
5048 case XML_DOCUMENT_TYPE_NODE:
5049 case XML_NOTATION_NODE:
5050 case XML_DTD_NODE:
5051 case XML_XINCLUDE_START:
5052 case XML_XINCLUDE_END:
5053 break;
5054 case XML_DOCUMENT_NODE:
5055#ifdef LIBXML_DOCB_ENABLED
5056 case XML_DOCB_DOCUMENT_NODE:
5057#endif
5058 case XML_HTML_DOCUMENT_NODE:
5059 cur = cur->children;
5060 while (cur!= NULL) {
5061 if ((cur->type == XML_ELEMENT_NODE) ||
5062 (cur->type == XML_TEXT_NODE) ||
5063 (cur->type == XML_CDATA_SECTION_NODE)) {
5064 xmlNodeBufGetContent(buffer, cur);
5065 }
5066 cur = cur->next;
5067 }
5068 break;
5069 case XML_NAMESPACE_DECL:
5070 xmlBufferCat(buffer, ((xmlNsPtr) cur)->href);
5071 break;
5072 case XML_ELEMENT_DECL:
5073 case XML_ATTRIBUTE_DECL:
5074 case XML_ENTITY_DECL:
5075 break;
5076 }
5077 return(0);
5078}
5079/**
Owen Taylor3473f882001-02-23 17:55:21 +00005080 * xmlNodeGetContent:
5081 * @cur: the node being read
5082 *
5083 * Read the value of a node, this can be either the text carried
5084 * directly by this node if it's a TEXT node or the aggregate string
5085 * of the values carried by this node child's (TEXT and ENTITY_REF).
Daniel Veillardd1640922001-12-17 15:30:10 +00005086 * Entity references are substituted.
5087 * Returns a new #xmlChar * or NULL if no content is available.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00005088 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00005089 */
5090xmlChar *
Daniel Veillard7646b182002-04-20 06:41:40 +00005091xmlNodeGetContent(xmlNodePtr cur)
5092{
5093 if (cur == NULL)
5094 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005095 switch (cur->type) {
5096 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005097 case XML_ELEMENT_NODE:{
Daniel Veillard7646b182002-04-20 06:41:40 +00005098 xmlBufferPtr buffer;
5099 xmlChar *ret;
Owen Taylor3473f882001-02-23 17:55:21 +00005100
Daniel Veillard814a76d2003-01-23 18:24:20 +00005101 buffer = xmlBufferCreateSize(64);
Daniel Veillard7646b182002-04-20 06:41:40 +00005102 if (buffer == NULL)
5103 return (NULL);
Daniel Veillardc4696922003-10-19 21:47:14 +00005104 xmlNodeBufGetContent(buffer, cur);
Daniel Veillard7646b182002-04-20 06:41:40 +00005105 ret = buffer->content;
5106 buffer->content = NULL;
5107 xmlBufferFree(buffer);
5108 return (ret);
5109 }
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005110 case XML_ATTRIBUTE_NODE:
5111 return(xmlGetPropNodeValueInternal((xmlAttrPtr) cur));
Owen Taylor3473f882001-02-23 17:55:21 +00005112 case XML_COMMENT_NODE:
5113 case XML_PI_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005114 if (cur->content != NULL)
5115 return (xmlStrdup(cur->content));
5116 return (NULL);
5117 case XML_ENTITY_REF_NODE:{
5118 xmlEntityPtr ent;
Daniel Veillard7646b182002-04-20 06:41:40 +00005119 xmlBufferPtr buffer;
5120 xmlChar *ret;
5121
5122 /* lookup entity declaration */
5123 ent = xmlGetDocEntity(cur->doc, cur->name);
5124 if (ent == NULL)
5125 return (NULL);
5126
5127 buffer = xmlBufferCreate();
5128 if (buffer == NULL)
5129 return (NULL);
5130
Daniel Veillardc4696922003-10-19 21:47:14 +00005131 xmlNodeBufGetContent(buffer, cur);
Daniel Veillard7646b182002-04-20 06:41:40 +00005132
5133 ret = buffer->content;
5134 buffer->content = NULL;
5135 xmlBufferFree(buffer);
5136 return (ret);
5137 }
Owen Taylor3473f882001-02-23 17:55:21 +00005138 case XML_ENTITY_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005139 case XML_DOCUMENT_TYPE_NODE:
5140 case XML_NOTATION_NODE:
5141 case XML_DTD_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005142 case XML_XINCLUDE_START:
5143 case XML_XINCLUDE_END:
Daniel Veillard9adc0462003-03-24 18:39:54 +00005144 return (NULL);
5145 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005146#ifdef LIBXML_DOCB_ENABLED
Daniel Veillard7646b182002-04-20 06:41:40 +00005147 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005148#endif
Daniel Veillard9adc0462003-03-24 18:39:54 +00005149 case XML_HTML_DOCUMENT_NODE: {
Daniel Veillardc4696922003-10-19 21:47:14 +00005150 xmlBufferPtr buffer;
5151 xmlChar *ret;
Daniel Veillard9adc0462003-03-24 18:39:54 +00005152
Daniel Veillardc4696922003-10-19 21:47:14 +00005153 buffer = xmlBufferCreate();
5154 if (buffer == NULL)
5155 return (NULL);
5156
5157 xmlNodeBufGetContent(buffer, (xmlNodePtr) cur);
5158
5159 ret = buffer->content;
5160 buffer->content = NULL;
5161 xmlBufferFree(buffer);
5162 return (ret);
Daniel Veillard9adc0462003-03-24 18:39:54 +00005163 }
Daniel Veillard96c3a3b2002-10-14 15:39:04 +00005164 case XML_NAMESPACE_DECL: {
5165 xmlChar *tmp;
5166
5167 tmp = xmlStrdup(((xmlNsPtr) cur)->href);
5168 return (tmp);
5169 }
Owen Taylor3473f882001-02-23 17:55:21 +00005170 case XML_ELEMENT_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00005171 /* TODO !!! */
5172 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005173 case XML_ATTRIBUTE_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00005174 /* TODO !!! */
5175 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005176 case XML_ENTITY_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00005177 /* TODO !!! */
5178 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005179 case XML_CDATA_SECTION_NODE:
5180 case XML_TEXT_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00005181 if (cur->content != NULL)
5182 return (xmlStrdup(cur->content));
5183 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005184 }
Daniel Veillard7646b182002-04-20 06:41:40 +00005185 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005186}
Daniel Veillard652327a2003-09-29 18:02:38 +00005187
Owen Taylor3473f882001-02-23 17:55:21 +00005188/**
5189 * xmlNodeSetContent:
5190 * @cur: the node being modified
5191 * @content: the new value of the content
5192 *
5193 * Replace the content of a node.
5194 */
5195void
5196xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) {
5197 if (cur == NULL) {
5198#ifdef DEBUG_TREE
5199 xmlGenericError(xmlGenericErrorContext,
5200 "xmlNodeSetContent : node == NULL\n");
5201#endif
5202 return;
5203 }
5204 switch (cur->type) {
5205 case XML_DOCUMENT_FRAG_NODE:
5206 case XML_ELEMENT_NODE:
Daniel Veillard2c748c62002-01-16 15:37:50 +00005207 case XML_ATTRIBUTE_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005208 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5209 cur->children = xmlStringGetNodeList(cur->doc, content);
5210 UPDATE_LAST_CHILD_AND_PARENT(cur)
5211 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005212 case XML_TEXT_NODE:
5213 case XML_CDATA_SECTION_NODE:
5214 case XML_ENTITY_REF_NODE:
5215 case XML_ENTITY_NODE:
5216 case XML_PI_NODE:
5217 case XML_COMMENT_NODE:
Daniel Veillard8874b942005-08-25 13:19:21 +00005218 if ((cur->content != NULL) &&
5219 (cur->content != (xmlChar *) &(cur->properties))) {
William M. Brack7762bb12004-01-04 14:49:01 +00005220 if (!((cur->doc != NULL) && (cur->doc->dict != NULL) &&
Daniel Veillardc587bce2005-05-10 15:28:08 +00005221 (xmlDictOwns(cur->doc->dict, cur->content))))
William M. Brack7762bb12004-01-04 14:49:01 +00005222 xmlFree(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00005223 }
5224 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5225 cur->last = cur->children = NULL;
5226 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005227 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00005228 } else
5229 cur->content = NULL;
Daniel Veillard8874b942005-08-25 13:19:21 +00005230 cur->properties = NULL;
5231 cur->nsDef = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00005232 break;
5233 case XML_DOCUMENT_NODE:
5234 case XML_HTML_DOCUMENT_NODE:
5235 case XML_DOCUMENT_TYPE_NODE:
5236 case XML_XINCLUDE_START:
5237 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005238#ifdef LIBXML_DOCB_ENABLED
5239 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005240#endif
5241 break;
5242 case XML_NOTATION_NODE:
5243 break;
5244 case XML_DTD_NODE:
5245 break;
5246 case XML_NAMESPACE_DECL:
5247 break;
5248 case XML_ELEMENT_DECL:
5249 /* TODO !!! */
5250 break;
5251 case XML_ATTRIBUTE_DECL:
5252 /* TODO !!! */
5253 break;
5254 case XML_ENTITY_DECL:
5255 /* TODO !!! */
5256 break;
5257 }
5258}
5259
Daniel Veillard652327a2003-09-29 18:02:38 +00005260#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00005261/**
5262 * xmlNodeSetContentLen:
5263 * @cur: the node being modified
5264 * @content: the new value of the content
5265 * @len: the size of @content
5266 *
5267 * Replace the content of a node.
5268 */
5269void
5270xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5271 if (cur == NULL) {
5272#ifdef DEBUG_TREE
5273 xmlGenericError(xmlGenericErrorContext,
5274 "xmlNodeSetContentLen : node == NULL\n");
5275#endif
5276 return;
5277 }
5278 switch (cur->type) {
5279 case XML_DOCUMENT_FRAG_NODE:
5280 case XML_ELEMENT_NODE:
Daniel Veillard2c748c62002-01-16 15:37:50 +00005281 case XML_ATTRIBUTE_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005282 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5283 cur->children = xmlStringLenGetNodeList(cur->doc, content, len);
5284 UPDATE_LAST_CHILD_AND_PARENT(cur)
5285 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005286 case XML_TEXT_NODE:
5287 case XML_CDATA_SECTION_NODE:
5288 case XML_ENTITY_REF_NODE:
5289 case XML_ENTITY_NODE:
5290 case XML_PI_NODE:
5291 case XML_COMMENT_NODE:
5292 case XML_NOTATION_NODE:
Daniel Veillard8874b942005-08-25 13:19:21 +00005293 if ((cur->content != NULL) &&
5294 (cur->content != (xmlChar *) &(cur->properties))) {
5295 if (!((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5296 (xmlDictOwns(cur->doc->dict, cur->content))))
5297 xmlFree(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00005298 }
5299 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5300 cur->children = cur->last = NULL;
5301 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005302 cur->content = xmlStrndup(content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00005303 } else
5304 cur->content = NULL;
Daniel Veillard8874b942005-08-25 13:19:21 +00005305 cur->properties = NULL;
5306 cur->nsDef = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00005307 break;
5308 case XML_DOCUMENT_NODE:
5309 case XML_DTD_NODE:
5310 case XML_HTML_DOCUMENT_NODE:
5311 case XML_DOCUMENT_TYPE_NODE:
5312 case XML_NAMESPACE_DECL:
5313 case XML_XINCLUDE_START:
5314 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005315#ifdef LIBXML_DOCB_ENABLED
5316 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005317#endif
5318 break;
5319 case XML_ELEMENT_DECL:
5320 /* TODO !!! */
5321 break;
5322 case XML_ATTRIBUTE_DECL:
5323 /* TODO !!! */
5324 break;
5325 case XML_ENTITY_DECL:
5326 /* TODO !!! */
5327 break;
5328 }
5329}
Daniel Veillard652327a2003-09-29 18:02:38 +00005330#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005331
5332/**
5333 * xmlNodeAddContentLen:
5334 * @cur: the node being modified
5335 * @content: extra content
5336 * @len: the size of @content
5337 *
5338 * Append the extra substring to the node content.
5339 */
5340void
5341xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5342 if (cur == NULL) {
5343#ifdef DEBUG_TREE
5344 xmlGenericError(xmlGenericErrorContext,
5345 "xmlNodeAddContentLen : node == NULL\n");
5346#endif
5347 return;
5348 }
5349 if (len <= 0) return;
5350 switch (cur->type) {
5351 case XML_DOCUMENT_FRAG_NODE:
5352 case XML_ELEMENT_NODE: {
Daniel Veillardacb2bda2002-01-13 16:15:43 +00005353 xmlNodePtr last, newNode, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00005354
Daniel Veillard7db37732001-07-12 01:20:08 +00005355 last = cur->last;
Owen Taylor3473f882001-02-23 17:55:21 +00005356 newNode = xmlNewTextLen(content, len);
5357 if (newNode != NULL) {
Daniel Veillardacb2bda2002-01-13 16:15:43 +00005358 tmp = xmlAddChild(cur, newNode);
5359 if (tmp != newNode)
5360 return;
Owen Taylor3473f882001-02-23 17:55:21 +00005361 if ((last != NULL) && (last->next == newNode)) {
5362 xmlTextMerge(last, newNode);
5363 }
5364 }
5365 break;
5366 }
5367 case XML_ATTRIBUTE_NODE:
5368 break;
5369 case XML_TEXT_NODE:
5370 case XML_CDATA_SECTION_NODE:
5371 case XML_ENTITY_REF_NODE:
5372 case XML_ENTITY_NODE:
5373 case XML_PI_NODE:
5374 case XML_COMMENT_NODE:
5375 case XML_NOTATION_NODE:
5376 if (content != NULL) {
Daniel Veillard8874b942005-08-25 13:19:21 +00005377 if ((cur->content == (xmlChar *) &(cur->properties)) ||
5378 ((cur->doc != NULL) && (cur->doc->dict != NULL) &&
5379 xmlDictOwns(cur->doc->dict, cur->content))) {
5380 cur->content = xmlStrncatNew(cur->content, content, len);
5381 cur->properties = NULL;
5382 cur->nsDef = NULL;
William M. Brack7762bb12004-01-04 14:49:01 +00005383 break;
5384 }
Owen Taylor3473f882001-02-23 17:55:21 +00005385 cur->content = xmlStrncat(cur->content, content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00005386 }
5387 case XML_DOCUMENT_NODE:
5388 case XML_DTD_NODE:
5389 case XML_HTML_DOCUMENT_NODE:
5390 case XML_DOCUMENT_TYPE_NODE:
5391 case XML_NAMESPACE_DECL:
5392 case XML_XINCLUDE_START:
5393 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005394#ifdef LIBXML_DOCB_ENABLED
5395 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005396#endif
5397 break;
5398 case XML_ELEMENT_DECL:
5399 case XML_ATTRIBUTE_DECL:
5400 case XML_ENTITY_DECL:
5401 break;
5402 }
5403}
5404
5405/**
5406 * xmlNodeAddContent:
5407 * @cur: the node being modified
5408 * @content: extra content
5409 *
5410 * Append the extra substring to the node content.
5411 */
5412void
5413xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) {
5414 int len;
5415
5416 if (cur == NULL) {
5417#ifdef DEBUG_TREE
5418 xmlGenericError(xmlGenericErrorContext,
5419 "xmlNodeAddContent : node == NULL\n");
5420#endif
5421 return;
5422 }
5423 if (content == NULL) return;
5424 len = xmlStrlen(content);
5425 xmlNodeAddContentLen(cur, content, len);
5426}
5427
5428/**
5429 * xmlTextMerge:
5430 * @first: the first text node
5431 * @second: the second text node being merged
5432 *
5433 * Merge two text nodes into one
5434 * Returns the first text node augmented
5435 */
5436xmlNodePtr
5437xmlTextMerge(xmlNodePtr first, xmlNodePtr second) {
5438 if (first == NULL) return(second);
5439 if (second == NULL) return(first);
5440 if (first->type != XML_TEXT_NODE) return(first);
5441 if (second->type != XML_TEXT_NODE) return(first);
5442 if (second->name != first->name)
5443 return(first);
Owen Taylor3473f882001-02-23 17:55:21 +00005444 xmlNodeAddContent(first, second->content);
Owen Taylor3473f882001-02-23 17:55:21 +00005445 xmlUnlinkNode(second);
5446 xmlFreeNode(second);
5447 return(first);
5448}
5449
Daniel Veillard2156d432004-03-04 15:59:36 +00005450#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XPATH_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00005451/**
5452 * xmlGetNsList:
5453 * @doc: the document
5454 * @node: the current node
5455 *
5456 * Search all the namespace applying to a given element.
Daniel Veillardd1640922001-12-17 15:30:10 +00005457 * Returns an NULL terminated array of all the #xmlNsPtr found
Owen Taylor3473f882001-02-23 17:55:21 +00005458 * that need to be freed by the caller or NULL if no
5459 * namespace if defined
5460 */
5461xmlNsPtr *
Daniel Veillard77044732001-06-29 21:31:07 +00005462xmlGetNsList(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node)
5463{
Owen Taylor3473f882001-02-23 17:55:21 +00005464 xmlNsPtr cur;
5465 xmlNsPtr *ret = NULL;
5466 int nbns = 0;
5467 int maxns = 10;
5468 int i;
5469
5470 while (node != NULL) {
Daniel Veillard77044732001-06-29 21:31:07 +00005471 if (node->type == XML_ELEMENT_NODE) {
5472 cur = node->nsDef;
5473 while (cur != NULL) {
5474 if (ret == NULL) {
5475 ret =
5476 (xmlNsPtr *) xmlMalloc((maxns + 1) *
5477 sizeof(xmlNsPtr));
5478 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005479 xmlTreeErrMemory("getting namespace list");
Daniel Veillard77044732001-06-29 21:31:07 +00005480 return (NULL);
5481 }
5482 ret[nbns] = NULL;
5483 }
5484 for (i = 0; i < nbns; i++) {
5485 if ((cur->prefix == ret[i]->prefix) ||
5486 (xmlStrEqual(cur->prefix, ret[i]->prefix)))
5487 break;
5488 }
5489 if (i >= nbns) {
5490 if (nbns >= maxns) {
5491 maxns *= 2;
5492 ret = (xmlNsPtr *) xmlRealloc(ret,
5493 (maxns +
5494 1) *
5495 sizeof(xmlNsPtr));
5496 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005497 xmlTreeErrMemory("getting namespace list");
Daniel Veillard77044732001-06-29 21:31:07 +00005498 return (NULL);
5499 }
5500 }
5501 ret[nbns++] = cur;
5502 ret[nbns] = NULL;
5503 }
Owen Taylor3473f882001-02-23 17:55:21 +00005504
Daniel Veillard77044732001-06-29 21:31:07 +00005505 cur = cur->next;
5506 }
5507 }
5508 node = node->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00005509 }
Daniel Veillard77044732001-06-29 21:31:07 +00005510 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00005511}
Daniel Veillard652327a2003-09-29 18:02:38 +00005512#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005513
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005514/*
5515* xmlTreeEnsureXMLDecl:
5516* @doc: the doc
5517*
5518* Ensures that there is an XML namespace declaration on the doc.
5519*
5520* Returns the XML ns-struct or NULL on API and internal errors.
5521*/
5522static xmlNsPtr
5523xmlTreeEnsureXMLDecl(xmlDocPtr doc)
5524{
5525 if (doc == NULL)
5526 return (NULL);
5527 if (doc->oldNs != NULL)
5528 return (doc->oldNs);
5529 {
5530 xmlNsPtr ns;
5531 ns = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5532 if (ns == NULL) {
5533 xmlTreeErrMemory(
5534 "allocating the XML namespace");
5535 return (NULL);
5536 }
5537 memset(ns, 0, sizeof(xmlNs));
5538 ns->type = XML_LOCAL_NAMESPACE;
5539 ns->href = xmlStrdup(XML_XML_NAMESPACE);
5540 ns->prefix = xmlStrdup((const xmlChar *)"xml");
5541 doc->oldNs = ns;
5542 return (ns);
5543 }
5544}
5545
Owen Taylor3473f882001-02-23 17:55:21 +00005546/**
5547 * xmlSearchNs:
5548 * @doc: the document
5549 * @node: the current node
Daniel Veillard77851712001-02-27 21:54:07 +00005550 * @nameSpace: the namespace prefix
Owen Taylor3473f882001-02-23 17:55:21 +00005551 *
5552 * Search a Ns registered under a given name space for a document.
5553 * recurse on the parents until it finds the defined namespace
5554 * or return NULL otherwise.
5555 * @nameSpace can be NULL, this is a search for the default namespace.
5556 * We don't allow to cross entities boundaries. If you don't declare
5557 * the namespace within those you will be in troubles !!! A warning
5558 * is generated to cover this case.
5559 *
5560 * Returns the namespace pointer or NULL.
5561 */
5562xmlNsPtr
5563xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) {
Daniel Veillardf4e56292003-10-28 14:27:41 +00005564
Owen Taylor3473f882001-02-23 17:55:21 +00005565 xmlNsPtr cur;
Daniel Veillardf4e56292003-10-28 14:27:41 +00005566 xmlNodePtr orig = node;
Owen Taylor3473f882001-02-23 17:55:21 +00005567
5568 if (node == NULL) return(NULL);
5569 if ((nameSpace != NULL) &&
5570 (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) {
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00005571 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
5572 /*
5573 * The XML-1.0 namespace is normally held on the root
5574 * element. In this case exceptionally create it on the
5575 * node element.
5576 */
5577 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5578 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005579 xmlTreeErrMemory("searching namespace");
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00005580 return(NULL);
5581 }
5582 memset(cur, 0, sizeof(xmlNs));
5583 cur->type = XML_LOCAL_NAMESPACE;
5584 cur->href = xmlStrdup(XML_XML_NAMESPACE);
5585 cur->prefix = xmlStrdup((const xmlChar *)"xml");
5586 cur->next = node->nsDef;
5587 node->nsDef = cur;
5588 return(cur);
5589 }
Daniel Veillard11ce4002006-03-10 00:36:23 +00005590 if (doc == NULL) {
5591 doc = node->doc;
5592 if (doc == NULL)
5593 return(NULL);
5594 }
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005595 /*
5596 * Return the XML namespace declaration held by the doc.
5597 */
5598 if (doc->oldNs == NULL)
5599 return(xmlTreeEnsureXMLDecl(doc));
5600 else
5601 return(doc->oldNs);
Owen Taylor3473f882001-02-23 17:55:21 +00005602 }
5603 while (node != NULL) {
5604 if ((node->type == XML_ENTITY_REF_NODE) ||
5605 (node->type == XML_ENTITY_NODE) ||
5606 (node->type == XML_ENTITY_DECL))
5607 return(NULL);
5608 if (node->type == XML_ELEMENT_NODE) {
5609 cur = node->nsDef;
5610 while (cur != NULL) {
5611 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
5612 (cur->href != NULL))
5613 return(cur);
5614 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
5615 (cur->href != NULL) &&
5616 (xmlStrEqual(cur->prefix, nameSpace)))
5617 return(cur);
5618 cur = cur->next;
5619 }
Daniel Veillardf4e56292003-10-28 14:27:41 +00005620 if (orig != node) {
5621 cur = node->ns;
5622 if (cur != NULL) {
5623 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
5624 (cur->href != NULL))
5625 return(cur);
5626 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
5627 (cur->href != NULL) &&
5628 (xmlStrEqual(cur->prefix, nameSpace)))
5629 return(cur);
5630 }
5631 }
Owen Taylor3473f882001-02-23 17:55:21 +00005632 }
5633 node = node->parent;
5634 }
5635 return(NULL);
5636}
5637
5638/**
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005639 * xmlNsInScope:
5640 * @doc: the document
5641 * @node: the current node
5642 * @ancestor: the ancestor carrying the namespace
5643 * @prefix: the namespace prefix
5644 *
5645 * Verify that the given namespace held on @ancestor is still in scope
5646 * on node.
5647 *
5648 * Returns 1 if true, 0 if false and -1 in case of error.
5649 */
5650static int
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005651xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node,
5652 xmlNodePtr ancestor, const xmlChar * prefix)
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005653{
5654 xmlNsPtr tst;
5655
5656 while ((node != NULL) && (node != ancestor)) {
5657 if ((node->type == XML_ENTITY_REF_NODE) ||
5658 (node->type == XML_ENTITY_NODE) ||
5659 (node->type == XML_ENTITY_DECL))
5660 return (-1);
5661 if (node->type == XML_ELEMENT_NODE) {
5662 tst = node->nsDef;
5663 while (tst != NULL) {
5664 if ((tst->prefix == NULL)
5665 && (prefix == NULL))
5666 return (0);
5667 if ((tst->prefix != NULL)
5668 && (prefix != NULL)
5669 && (xmlStrEqual(tst->prefix, prefix)))
5670 return (0);
5671 tst = tst->next;
5672 }
5673 }
5674 node = node->parent;
5675 }
5676 if (node != ancestor)
5677 return (-1);
5678 return (1);
5679}
5680
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005681/**
Owen Taylor3473f882001-02-23 17:55:21 +00005682 * xmlSearchNsByHref:
5683 * @doc: the document
5684 * @node: the current node
5685 * @href: the namespace value
5686 *
5687 * Search a Ns aliasing a given URI. Recurse on the parents until it finds
5688 * the defined namespace or return NULL otherwise.
5689 * Returns the namespace pointer or NULL.
5690 */
5691xmlNsPtr
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005692xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href)
5693{
Owen Taylor3473f882001-02-23 17:55:21 +00005694 xmlNsPtr cur;
5695 xmlNodePtr orig = node;
Daniel Veillard62040be2004-05-17 03:17:26 +00005696 int is_attr;
Owen Taylor3473f882001-02-23 17:55:21 +00005697
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005698 if ((node == NULL) || (href == NULL))
5699 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005700 if (xmlStrEqual(href, XML_XML_NAMESPACE)) {
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005701 /*
5702 * Only the document can hold the XML spec namespace.
5703 */
5704 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
5705 /*
5706 * The XML-1.0 namespace is normally held on the root
5707 * element. In this case exceptionally create it on the
5708 * node element.
5709 */
5710 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5711 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005712 xmlTreeErrMemory("searching namespace");
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005713 return (NULL);
5714 }
5715 memset(cur, 0, sizeof(xmlNs));
5716 cur->type = XML_LOCAL_NAMESPACE;
5717 cur->href = xmlStrdup(XML_XML_NAMESPACE);
5718 cur->prefix = xmlStrdup((const xmlChar *) "xml");
5719 cur->next = node->nsDef;
5720 node->nsDef = cur;
5721 return (cur);
5722 }
Daniel Veillard11ce4002006-03-10 00:36:23 +00005723 if (doc == NULL) {
5724 doc = node->doc;
5725 if (doc == NULL)
5726 return(NULL);
5727 }
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00005728 /*
5729 * Return the XML namespace declaration held by the doc.
5730 */
5731 if (doc->oldNs == NULL)
5732 return(xmlTreeEnsureXMLDecl(doc));
5733 else
5734 return(doc->oldNs);
Owen Taylor3473f882001-02-23 17:55:21 +00005735 }
Daniel Veillard62040be2004-05-17 03:17:26 +00005736 is_attr = (node->type == XML_ATTRIBUTE_NODE);
Owen Taylor3473f882001-02-23 17:55:21 +00005737 while (node != NULL) {
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005738 if ((node->type == XML_ENTITY_REF_NODE) ||
5739 (node->type == XML_ENTITY_NODE) ||
5740 (node->type == XML_ENTITY_DECL))
5741 return (NULL);
5742 if (node->type == XML_ELEMENT_NODE) {
5743 cur = node->nsDef;
5744 while (cur != NULL) {
5745 if ((cur->href != NULL) && (href != NULL) &&
5746 (xmlStrEqual(cur->href, href))) {
Daniel Veillard62040be2004-05-17 03:17:26 +00005747 if (((!is_attr) || (cur->prefix != NULL)) &&
Kasimier T. Buchcikba70cc02005-02-28 10:28:21 +00005748 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005749 return (cur);
5750 }
5751 cur = cur->next;
5752 }
Daniel Veillardf4e56292003-10-28 14:27:41 +00005753 if (orig != node) {
5754 cur = node->ns;
5755 if (cur != NULL) {
5756 if ((cur->href != NULL) && (href != NULL) &&
5757 (xmlStrEqual(cur->href, href))) {
Daniel Veillard62040be2004-05-17 03:17:26 +00005758 if (((!is_attr) || (cur->prefix != NULL)) &&
Kasimier T. Buchcikba70cc02005-02-28 10:28:21 +00005759 (xmlNsInScope(doc, orig, node, cur->prefix) == 1))
Daniel Veillardf4e56292003-10-28 14:27:41 +00005760 return (cur);
5761 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005762 }
Daniel Veillardf4e56292003-10-28 14:27:41 +00005763 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005764 }
5765 node = node->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00005766 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005767 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005768}
5769
5770/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005771 * xmlNewReconciliedNs:
Owen Taylor3473f882001-02-23 17:55:21 +00005772 * @doc: the document
5773 * @tree: a node expected to hold the new namespace
5774 * @ns: the original namespace
5775 *
5776 * This function tries to locate a namespace definition in a tree
5777 * ancestors, or create a new namespace definition node similar to
5778 * @ns trying to reuse the same prefix. However if the given prefix is
5779 * null (default namespace) or reused within the subtree defined by
5780 * @tree or on one of its ancestors then a new prefix is generated.
5781 * Returns the (new) namespace definition or NULL in case of error
5782 */
5783xmlNsPtr
5784xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) {
5785 xmlNsPtr def;
5786 xmlChar prefix[50];
5787 int counter = 1;
5788
5789 if (tree == NULL) {
5790#ifdef DEBUG_TREE
5791 xmlGenericError(xmlGenericErrorContext,
5792 "xmlNewReconciliedNs : tree == NULL\n");
5793#endif
5794 return(NULL);
5795 }
Daniel Veillardce244ad2004-11-05 10:03:46 +00005796 if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005797#ifdef DEBUG_TREE
5798 xmlGenericError(xmlGenericErrorContext,
5799 "xmlNewReconciliedNs : ns == NULL\n");
5800#endif
5801 return(NULL);
5802 }
5803 /*
5804 * Search an existing namespace definition inherited.
5805 */
5806 def = xmlSearchNsByHref(doc, tree, ns->href);
5807 if (def != NULL)
5808 return(def);
5809
5810 /*
5811 * Find a close prefix which is not already in use.
5812 * Let's strip namespace prefixes longer than 20 chars !
5813 */
Daniel Veillardf742d342002-03-07 00:05:35 +00005814 if (ns->prefix == NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00005815 snprintf((char *) prefix, sizeof(prefix), "default");
Daniel Veillardf742d342002-03-07 00:05:35 +00005816 else
William M. Brack13dfa872004-09-18 04:52:08 +00005817 snprintf((char *) prefix, sizeof(prefix), "%.20s", (char *)ns->prefix);
Daniel Veillardf742d342002-03-07 00:05:35 +00005818
Owen Taylor3473f882001-02-23 17:55:21 +00005819 def = xmlSearchNs(doc, tree, prefix);
5820 while (def != NULL) {
5821 if (counter > 1000) return(NULL);
Daniel Veillardf742d342002-03-07 00:05:35 +00005822 if (ns->prefix == NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00005823 snprintf((char *) prefix, sizeof(prefix), "default%d", counter++);
Daniel Veillardf742d342002-03-07 00:05:35 +00005824 else
William M. Brack13dfa872004-09-18 04:52:08 +00005825 snprintf((char *) prefix, sizeof(prefix), "%.20s%d",
5826 (char *)ns->prefix, counter++);
Owen Taylor3473f882001-02-23 17:55:21 +00005827 def = xmlSearchNs(doc, tree, prefix);
5828 }
5829
5830 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00005831 * OK, now we are ready to create a new one.
Owen Taylor3473f882001-02-23 17:55:21 +00005832 */
5833 def = xmlNewNs(tree, ns->href, prefix);
5834 return(def);
5835}
5836
Daniel Veillard652327a2003-09-29 18:02:38 +00005837#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00005838/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005839 * xmlReconciliateNs:
Owen Taylor3473f882001-02-23 17:55:21 +00005840 * @doc: the document
5841 * @tree: a node defining the subtree to reconciliate
5842 *
5843 * This function checks that all the namespaces declared within the given
5844 * tree are properly declared. This is needed for example after Copy or Cut
5845 * and then paste operations. The subtree may still hold pointers to
5846 * namespace declarations outside the subtree or invalid/masked. As much
Daniel Veillardd1640922001-12-17 15:30:10 +00005847 * as possible the function try to reuse the existing namespaces found in
Owen Taylor3473f882001-02-23 17:55:21 +00005848 * the new environment. If not possible the new namespaces are redeclared
5849 * on @tree at the top of the given subtree.
5850 * Returns the number of namespace declarations created or -1 in case of error.
5851 */
5852int
5853xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) {
5854 xmlNsPtr *oldNs = NULL;
5855 xmlNsPtr *newNs = NULL;
5856 int sizeCache = 0;
5857 int nbCache = 0;
5858
5859 xmlNsPtr n;
5860 xmlNodePtr node = tree;
5861 xmlAttrPtr attr;
5862 int ret = 0, i;
5863
Daniel Veillardce244ad2004-11-05 10:03:46 +00005864 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) return(-1);
5865 if ((doc == NULL) || (doc->type != XML_DOCUMENT_NODE)) return(-1);
5866 if (node->doc != doc) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00005867 while (node != NULL) {
5868 /*
5869 * Reconciliate the node namespace
5870 */
5871 if (node->ns != NULL) {
5872 /*
5873 * initialize the cache if needed
5874 */
5875 if (sizeCache == 0) {
5876 sizeCache = 10;
5877 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
5878 sizeof(xmlNsPtr));
5879 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005880 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005881 return(-1);
5882 }
5883 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
5884 sizeof(xmlNsPtr));
5885 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005886 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005887 xmlFree(oldNs);
5888 return(-1);
5889 }
5890 }
5891 for (i = 0;i < nbCache;i++) {
5892 if (oldNs[i] == node->ns) {
5893 node->ns = newNs[i];
5894 break;
5895 }
5896 }
5897 if (i == nbCache) {
5898 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00005899 * OK we need to recreate a new namespace definition
Owen Taylor3473f882001-02-23 17:55:21 +00005900 */
5901 n = xmlNewReconciliedNs(doc, tree, node->ns);
5902 if (n != NULL) { /* :-( what if else ??? */
5903 /*
5904 * check if we need to grow the cache buffers.
5905 */
5906 if (sizeCache <= nbCache) {
5907 sizeCache *= 2;
5908 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
5909 sizeof(xmlNsPtr));
5910 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005911 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005912 xmlFree(newNs);
5913 return(-1);
5914 }
5915 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
5916 sizeof(xmlNsPtr));
5917 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005918 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005919 xmlFree(oldNs);
5920 return(-1);
5921 }
5922 }
5923 newNs[nbCache] = n;
5924 oldNs[nbCache++] = node->ns;
5925 node->ns = n;
5926 }
5927 }
5928 }
5929 /*
5930 * now check for namespace hold by attributes on the node.
5931 */
5932 attr = node->properties;
5933 while (attr != NULL) {
5934 if (attr->ns != NULL) {
5935 /*
5936 * initialize the cache if needed
5937 */
5938 if (sizeCache == 0) {
5939 sizeCache = 10;
5940 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
5941 sizeof(xmlNsPtr));
5942 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005943 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005944 return(-1);
5945 }
5946 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
5947 sizeof(xmlNsPtr));
5948 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005949 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005950 xmlFree(oldNs);
5951 return(-1);
5952 }
5953 }
5954 for (i = 0;i < nbCache;i++) {
5955 if (oldNs[i] == attr->ns) {
Daniel Veillardce66ce12002-10-28 19:01:59 +00005956 attr->ns = newNs[i];
Owen Taylor3473f882001-02-23 17:55:21 +00005957 break;
5958 }
5959 }
5960 if (i == nbCache) {
5961 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00005962 * OK we need to recreate a new namespace definition
Owen Taylor3473f882001-02-23 17:55:21 +00005963 */
5964 n = xmlNewReconciliedNs(doc, tree, attr->ns);
5965 if (n != NULL) { /* :-( what if else ??? */
5966 /*
5967 * check if we need to grow the cache buffers.
5968 */
5969 if (sizeCache <= nbCache) {
5970 sizeCache *= 2;
5971 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
5972 sizeof(xmlNsPtr));
5973 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005974 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005975 xmlFree(newNs);
5976 return(-1);
5977 }
5978 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
5979 sizeof(xmlNsPtr));
5980 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005981 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005982 xmlFree(oldNs);
5983 return(-1);
5984 }
5985 }
5986 newNs[nbCache] = n;
5987 oldNs[nbCache++] = attr->ns;
5988 attr->ns = n;
5989 }
5990 }
5991 }
5992 attr = attr->next;
5993 }
5994
5995 /*
5996 * Browse the full subtree, deep first
5997 */
Daniel Veillardac996a12004-07-30 12:02:58 +00005998 if (node->children != NULL && node->type != XML_ENTITY_REF_NODE) {
Owen Taylor3473f882001-02-23 17:55:21 +00005999 /* deep first */
6000 node = node->children;
6001 } else if ((node != tree) && (node->next != NULL)) {
6002 /* then siblings */
6003 node = node->next;
6004 } else if (node != tree) {
6005 /* go up to parents->next if needed */
6006 while (node != tree) {
6007 if (node->parent != NULL)
6008 node = node->parent;
6009 if ((node != tree) && (node->next != NULL)) {
6010 node = node->next;
6011 break;
6012 }
6013 if (node->parent == NULL) {
6014 node = NULL;
6015 break;
6016 }
6017 }
6018 /* exit condition */
6019 if (node == tree)
6020 node = NULL;
Daniel Veillard1e774382002-03-06 17:35:40 +00006021 } else
6022 break;
Owen Taylor3473f882001-02-23 17:55:21 +00006023 }
Daniel Veillardf742d342002-03-07 00:05:35 +00006024 if (oldNs != NULL)
6025 xmlFree(oldNs);
6026 if (newNs != NULL)
6027 xmlFree(newNs);
Owen Taylor3473f882001-02-23 17:55:21 +00006028 return(ret);
6029}
Daniel Veillard652327a2003-09-29 18:02:38 +00006030#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00006031
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006032static xmlAttrPtr
6033xmlGetPropNodeInternal(xmlNodePtr node, const xmlChar *name,
6034 const xmlChar *nsName, int useDTD)
6035{
6036 xmlAttrPtr prop;
6037
6038 if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL))
6039 return(NULL);
6040
6041 if (node->properties != NULL) {
6042 prop = node->properties;
6043 if (nsName == NULL) {
6044 /*
6045 * We want the attr to be in no namespace.
6046 */
6047 do {
6048 if ((prop->ns == NULL) && xmlStrEqual(prop->name, name)) {
6049 return(prop);
6050 }
6051 prop = prop->next;
6052 } while (prop != NULL);
6053 } else {
6054 /*
6055 * We want the attr to be in the specified namespace.
6056 */
6057 do {
6058 if ((prop->ns != NULL) && xmlStrEqual(prop->name, name) &&
6059 ((prop->ns->href == nsName) ||
6060 xmlStrEqual(prop->ns->href, nsName)))
6061 {
6062 return(prop);
6063 }
6064 prop = prop->next;
6065 } while (prop != NULL);
6066 }
6067 }
6068
6069#ifdef LIBXML_TREE_ENABLED
6070 if (! useDTD)
6071 return(NULL);
6072 /*
6073 * Check if there is a default/fixed attribute declaration in
6074 * the internal or external subset.
6075 */
6076 if ((node->doc != NULL) && (node->doc->intSubset != NULL)) {
6077 xmlDocPtr doc = node->doc;
6078 xmlAttributePtr attrDecl = NULL;
6079 xmlChar *elemQName, *tmpstr = NULL;
6080
6081 /*
6082 * We need the QName of the element for the DTD-lookup.
6083 */
6084 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
6085 tmpstr = xmlStrdup(node->ns->prefix);
6086 tmpstr = xmlStrcat(tmpstr, BAD_CAST ":");
6087 tmpstr = xmlStrcat(tmpstr, node->name);
6088 if (tmpstr == NULL)
6089 return(NULL);
6090 elemQName = tmpstr;
6091 } else
6092 elemQName = (xmlChar *) node->name;
6093 if (nsName == NULL) {
6094 /*
6095 * The common and nice case: Attr in no namespace.
6096 */
6097 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset,
6098 elemQName, name, NULL);
6099 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
6100 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset,
6101 elemQName, name, NULL);
6102 }
6103 } else {
6104 xmlNsPtr *nsList, *cur;
6105
6106 /*
6107 * The ugly case: Search using the prefixes of in-scope
6108 * ns-decls corresponding to @nsName.
6109 */
6110 nsList = xmlGetNsList(node->doc, node);
6111 if (nsList == NULL) {
6112 if (tmpstr != NULL)
6113 xmlFree(tmpstr);
6114 return(NULL);
6115 }
6116 cur = nsList;
6117 while (*cur != NULL) {
6118 if (xmlStrEqual((*cur)->href, nsName)) {
6119 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elemQName,
6120 name, (*cur)->prefix);
6121 if (attrDecl)
6122 break;
6123 if (doc->extSubset != NULL) {
6124 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elemQName,
6125 name, (*cur)->prefix);
6126 if (attrDecl)
6127 break;
6128 }
6129 }
6130 cur++;
6131 }
6132 xmlFree(nsList);
6133 }
6134 if (tmpstr != NULL)
6135 xmlFree(tmpstr);
6136 /*
6137 * Only default/fixed attrs are relevant.
6138 */
6139 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6140 return((xmlAttrPtr) attrDecl);
6141 }
6142#endif /* LIBXML_TREE_ENABLED */
6143 return(NULL);
6144}
6145
6146static xmlChar*
6147xmlGetPropNodeValueInternal(xmlAttrPtr prop)
6148{
6149 if (prop == NULL)
6150 return(NULL);
6151 if (prop->type == XML_ATTRIBUTE_NODE) {
6152 /*
6153 * Note that we return at least the empty string.
6154 * TODO: Do we really always want that?
6155 */
6156 if (prop->children != NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00006157 if ((prop->children->next == NULL) &&
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006158 ((prop->children->type == XML_TEXT_NODE) ||
6159 (prop->children->type == XML_CDATA_SECTION_NODE)))
6160 {
6161 /*
6162 * Optimization for the common case: only 1 text node.
6163 */
6164 return(xmlStrdup(prop->children->content));
6165 } else {
6166 xmlChar *ret;
6167
6168 ret = xmlNodeListGetString(prop->doc, prop->children, 1);
6169 if (ret != NULL)
6170 return(ret);
6171 }
6172 }
6173 return(xmlStrdup((xmlChar *)""));
6174 } else if (prop->type == XML_ATTRIBUTE_DECL) {
6175 return(xmlStrdup(((xmlAttributePtr)prop)->defaultValue));
6176 }
6177 return(NULL);
6178}
6179
Owen Taylor3473f882001-02-23 17:55:21 +00006180/**
6181 * xmlHasProp:
6182 * @node: the node
6183 * @name: the attribute name
6184 *
6185 * Search an attribute associated to a node
6186 * This function also looks in DTD attribute declaration for #FIXED or
6187 * default declaration values unless DTD use has been turned off.
6188 *
6189 * Returns the attribute or the attribute declaration or NULL if
6190 * neither was found.
6191 */
6192xmlAttrPtr
6193xmlHasProp(xmlNodePtr node, const xmlChar *name) {
6194 xmlAttrPtr prop;
6195 xmlDocPtr doc;
6196
Daniel Veillard8874b942005-08-25 13:19:21 +00006197 if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL))
6198 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006199 /*
6200 * Check on the properties attached to the node
6201 */
6202 prop = node->properties;
6203 while (prop != NULL) {
6204 if (xmlStrEqual(prop->name, name)) {
6205 return(prop);
6206 }
6207 prop = prop->next;
6208 }
6209 if (!xmlCheckDTD) return(NULL);
6210
6211 /*
6212 * Check if there is a default declaration in the internal
6213 * or external subsets
6214 */
6215 doc = node->doc;
6216 if (doc != NULL) {
6217 xmlAttributePtr attrDecl;
6218 if (doc->intSubset != NULL) {
6219 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
6220 if ((attrDecl == NULL) && (doc->extSubset != NULL))
6221 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
Daniel Veillard75eb1ad2003-07-07 14:42:44 +00006222 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6223 /* return attribute declaration only if a default value is given
6224 (that includes #FIXED declarations) */
Owen Taylor3473f882001-02-23 17:55:21 +00006225 return((xmlAttrPtr) attrDecl);
6226 }
6227 }
6228 return(NULL);
6229}
6230
6231/**
Daniel Veillarde95e2392001-06-06 10:46:28 +00006232 * xmlHasNsProp:
6233 * @node: the node
6234 * @name: the attribute name
Daniel Veillardca2366a2001-06-11 12:09:01 +00006235 * @nameSpace: the URI of the namespace
Daniel Veillarde95e2392001-06-06 10:46:28 +00006236 *
6237 * Search for an attribute associated to a node
6238 * This attribute has to be anchored in the namespace specified.
6239 * This does the entity substitution.
6240 * This function looks in DTD attribute declaration for #FIXED or
6241 * default declaration values unless DTD use has been turned off.
William M. Brack2c228442004-10-03 04:10:00 +00006242 * Note that a namespace of NULL indicates to use the default namespace.
Daniel Veillarde95e2392001-06-06 10:46:28 +00006243 *
6244 * Returns the attribute or the attribute declaration or NULL
6245 * if neither was found.
6246 */
6247xmlAttrPtr
Daniel Veillardca2366a2001-06-11 12:09:01 +00006248xmlHasNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
Daniel Veillarde95e2392001-06-06 10:46:28 +00006249
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006250 return(xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD));
Daniel Veillarde95e2392001-06-06 10:46:28 +00006251}
6252
6253/**
Owen Taylor3473f882001-02-23 17:55:21 +00006254 * xmlGetProp:
6255 * @node: the node
6256 * @name: the attribute name
6257 *
6258 * Search and get the value of an attribute associated to a node
6259 * This does the entity substitution.
6260 * This function looks in DTD attribute declaration for #FIXED or
6261 * default declaration values unless DTD use has been turned off.
Daniel Veillard784b9352003-02-16 15:50:27 +00006262 * NOTE: this function acts independently of namespaces associated
Daniel Veillard71531f32003-02-05 13:19:53 +00006263 * to the attribute. Use xmlGetNsProp() or xmlGetNoNsProp()
6264 * for namespace aware processing.
Owen Taylor3473f882001-02-23 17:55:21 +00006265 *
6266 * Returns the attribute value or NULL if not found.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00006267 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00006268 */
6269xmlChar *
6270xmlGetProp(xmlNodePtr node, const xmlChar *name) {
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006271 xmlAttrPtr prop;
Owen Taylor3473f882001-02-23 17:55:21 +00006272
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006273 prop = xmlHasProp(node, name);
6274 if (prop == NULL)
6275 return(NULL);
6276 return(xmlGetPropNodeValueInternal(prop));
Owen Taylor3473f882001-02-23 17:55:21 +00006277}
6278
6279/**
Daniel Veillard71531f32003-02-05 13:19:53 +00006280 * xmlGetNoNsProp:
6281 * @node: the node
6282 * @name: the attribute name
6283 *
6284 * Search and get the value of an attribute associated to a node
6285 * This does the entity substitution.
6286 * This function looks in DTD attribute declaration for #FIXED or
6287 * default declaration values unless DTD use has been turned off.
6288 * This function is similar to xmlGetProp except it will accept only
6289 * an attribute in no namespace.
6290 *
6291 * Returns the attribute value or NULL if not found.
6292 * It's up to the caller to free the memory with xmlFree().
6293 */
6294xmlChar *
6295xmlGetNoNsProp(xmlNodePtr node, const xmlChar *name) {
6296 xmlAttrPtr prop;
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006297
6298 prop = xmlGetPropNodeInternal(node, name, NULL, xmlCheckDTD);
6299 if (prop == NULL)
Daniel Veillard8874b942005-08-25 13:19:21 +00006300 return(NULL);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006301 return(xmlGetPropNodeValueInternal(prop));
Daniel Veillard71531f32003-02-05 13:19:53 +00006302}
6303
6304/**
Owen Taylor3473f882001-02-23 17:55:21 +00006305 * xmlGetNsProp:
6306 * @node: the node
6307 * @name: the attribute name
Daniel Veillardca2366a2001-06-11 12:09:01 +00006308 * @nameSpace: the URI of the namespace
Owen Taylor3473f882001-02-23 17:55:21 +00006309 *
6310 * Search and get the value of an attribute associated to a node
6311 * This attribute has to be anchored in the namespace specified.
6312 * This does the entity substitution.
6313 * This function looks in DTD attribute declaration for #FIXED or
6314 * default declaration values unless DTD use has been turned off.
6315 *
6316 * Returns the attribute value or NULL if not found.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00006317 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00006318 */
6319xmlChar *
Daniel Veillardca2366a2001-06-11 12:09:01 +00006320xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
Owen Taylor3473f882001-02-23 17:55:21 +00006321 xmlAttrPtr prop;
Owen Taylor3473f882001-02-23 17:55:21 +00006322
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006323 prop = xmlGetPropNodeInternal(node, name, nameSpace, xmlCheckDTD);
6324 if (prop == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00006325 return(NULL);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006326 return(xmlGetPropNodeValueInternal(prop));
Owen Taylor3473f882001-02-23 17:55:21 +00006327}
6328
Daniel Veillard2156d432004-03-04 15:59:36 +00006329#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
6330/**
6331 * xmlUnsetProp:
6332 * @node: the node
6333 * @name: the attribute name
6334 *
6335 * Remove an attribute carried by a node.
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006336 * This handles only attributes in no namespace.
Daniel Veillard2156d432004-03-04 15:59:36 +00006337 * Returns 0 if successful, -1 if not found
6338 */
6339int
6340xmlUnsetProp(xmlNodePtr node, const xmlChar *name) {
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +00006341 xmlAttrPtr prop;
Daniel Veillard2156d432004-03-04 15:59:36 +00006342
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006343 prop = xmlGetPropNodeInternal(node, name, NULL, 0);
6344 if (prop == NULL)
Daniel Veillard2156d432004-03-04 15:59:36 +00006345 return(-1);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006346 xmlUnlinkNode((xmlNodePtr) prop);
6347 xmlFreeProp(prop);
6348 return(0);
Daniel Veillard2156d432004-03-04 15:59:36 +00006349}
6350
6351/**
6352 * xmlUnsetNsProp:
6353 * @node: the node
6354 * @ns: the namespace definition
6355 * @name: the attribute name
6356 *
6357 * Remove an attribute carried by a node.
6358 * Returns 0 if successful, -1 if not found
6359 */
6360int
6361xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) {
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +00006362 xmlAttrPtr prop;
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006363
6364 prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0);
6365 if (prop == NULL)
Daniel Veillard2156d432004-03-04 15:59:36 +00006366 return(-1);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006367 xmlUnlinkNode((xmlNodePtr) prop);
6368 xmlFreeProp(prop);
6369 return(0);
Daniel Veillard2156d432004-03-04 15:59:36 +00006370}
6371#endif
6372
6373#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_XINCLUDE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_HTML_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00006374/**
6375 * xmlSetProp:
6376 * @node: the node
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006377 * @name: the attribute name (a QName)
Owen Taylor3473f882001-02-23 17:55:21 +00006378 * @value: the attribute value
6379 *
6380 * Set (or reset) an attribute carried by a node.
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006381 * If @name has a prefix, then the corresponding
6382 * namespace-binding will be used, if in scope; it is an
6383 * error it there's no such ns-binding for the prefix in
6384 * scope.
Owen Taylor3473f882001-02-23 17:55:21 +00006385 * Returns the attribute pointer.
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006386 *
Owen Taylor3473f882001-02-23 17:55:21 +00006387 */
6388xmlAttrPtr
6389xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00006390 int len;
6391 const xmlChar *nqname;
Owen Taylor3473f882001-02-23 17:55:21 +00006392
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006393 if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE))
Owen Taylor3473f882001-02-23 17:55:21 +00006394 return(NULL);
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00006395
6396 /*
6397 * handle QNames
6398 */
6399 nqname = xmlSplitQName3(name, &len);
6400 if (nqname != NULL) {
6401 xmlNsPtr ns;
6402 xmlChar *prefix = xmlStrndup(name, len);
6403 ns = xmlSearchNs(node->doc, node, prefix);
6404 if (prefix != NULL)
6405 xmlFree(prefix);
6406 if (ns != NULL)
6407 return(xmlSetNsProp(node, ns, nqname, value));
6408 }
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006409 return(xmlSetNsProp(node, NULL, name, value));
Owen Taylor3473f882001-02-23 17:55:21 +00006410}
6411
6412/**
6413 * xmlSetNsProp:
6414 * @node: the node
6415 * @ns: the namespace definition
6416 * @name: the attribute name
6417 * @value: the attribute value
6418 *
6419 * Set (or reset) an attribute carried by a node.
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006420 * The ns structure must be in scope, this is not checked
Owen Taylor3473f882001-02-23 17:55:21 +00006421 *
6422 * Returns the attribute pointer.
6423 */
6424xmlAttrPtr
6425xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006426 const xmlChar *value)
6427{
Owen Taylor3473f882001-02-23 17:55:21 +00006428 xmlAttrPtr prop;
6429
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006430 if (ns && (ns->href == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00006431 return(NULL);
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006432 prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0);
6433 if (prop != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00006434 /*
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006435 * Modify the attribute's value.
6436 */
6437 if (prop->atype == XML_ATTRIBUTE_ID) {
6438 xmlRemoveID(node->doc, prop);
6439 prop->atype = XML_ATTRIBUTE_ID;
6440 }
6441 if (prop->children != NULL)
6442 xmlFreeNodeList(prop->children);
6443 prop->children = NULL;
6444 prop->last = NULL;
6445 prop->ns = ns;
6446 if (value != NULL) {
6447 xmlChar *buffer;
6448 xmlNodePtr tmp;
6449
6450 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
6451 prop->children = xmlStringGetNodeList(node->doc, buffer);
Owen Taylor3473f882001-02-23 17:55:21 +00006452 prop->last = NULL;
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006453 tmp = prop->children;
6454 while (tmp != NULL) {
6455 tmp->parent = (xmlNodePtr) prop;
6456 if (tmp->next == NULL)
6457 prop->last = tmp;
6458 tmp = tmp->next;
6459 }
6460 xmlFree(buffer);
6461 }
6462 if (prop->atype == XML_ATTRIBUTE_ID)
6463 xmlAddID(NULL, node->doc, value, prop);
6464 return(prop);
Owen Taylor3473f882001-02-23 17:55:21 +00006465 }
Kasimier T. Buchcik30f874d2006-03-02 18:04:29 +00006466 /*
6467 * No equal attr found; create a new one.
6468 */
6469 return(xmlNewPropInternal(node, ns, name, value, 0));
Owen Taylor3473f882001-02-23 17:55:21 +00006470}
6471
Daniel Veillard652327a2003-09-29 18:02:38 +00006472#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard75bea542001-05-11 17:41:21 +00006473
6474/**
Owen Taylor3473f882001-02-23 17:55:21 +00006475 * xmlNodeIsText:
6476 * @node: the node
6477 *
6478 * Is this node a Text node ?
6479 * Returns 1 yes, 0 no
6480 */
6481int
6482xmlNodeIsText(xmlNodePtr node) {
6483 if (node == NULL) return(0);
6484
6485 if (node->type == XML_TEXT_NODE) return(1);
6486 return(0);
6487}
6488
6489/**
6490 * xmlIsBlankNode:
6491 * @node: the node
6492 *
6493 * Checks whether this node is an empty or whitespace only
6494 * (and possibly ignorable) text-node.
6495 *
6496 * Returns 1 yes, 0 no
6497 */
6498int
6499xmlIsBlankNode(xmlNodePtr node) {
6500 const xmlChar *cur;
6501 if (node == NULL) return(0);
6502
Daniel Veillard7db37732001-07-12 01:20:08 +00006503 if ((node->type != XML_TEXT_NODE) &&
6504 (node->type != XML_CDATA_SECTION_NODE))
6505 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006506 if (node->content == NULL) return(1);
Owen Taylor3473f882001-02-23 17:55:21 +00006507 cur = node->content;
Owen Taylor3473f882001-02-23 17:55:21 +00006508 while (*cur != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00006509 if (!IS_BLANK_CH(*cur)) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006510 cur++;
6511 }
6512
6513 return(1);
6514}
6515
6516/**
6517 * xmlTextConcat:
6518 * @node: the node
6519 * @content: the content
Daniel Veillard60087f32001-10-10 09:45:09 +00006520 * @len: @content length
Owen Taylor3473f882001-02-23 17:55:21 +00006521 *
6522 * Concat the given string at the end of the existing node content
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006523 *
6524 * Returns -1 in case of error, 0 otherwise
Owen Taylor3473f882001-02-23 17:55:21 +00006525 */
6526
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006527int
Owen Taylor3473f882001-02-23 17:55:21 +00006528xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006529 if (node == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006530
6531 if ((node->type != XML_TEXT_NODE) &&
Rob Richardsa02f1992006-09-16 14:04:26 +00006532 (node->type != XML_CDATA_SECTION_NODE) &&
6533 (node->type != XML_COMMENT_NODE) &&
6534 (node->type != XML_PI_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006535#ifdef DEBUG_TREE
6536 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006537 "xmlTextConcat: node is not text nor CDATA\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006538#endif
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006539 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006540 }
William M. Brack7762bb12004-01-04 14:49:01 +00006541 /* need to check if content is currently in the dictionary */
Daniel Veillard8874b942005-08-25 13:19:21 +00006542 if ((node->content == (xmlChar *) &(node->properties)) ||
6543 ((node->doc != NULL) && (node->doc->dict != NULL) &&
6544 xmlDictOwns(node->doc->dict, node->content))) {
William M. Brack7762bb12004-01-04 14:49:01 +00006545 node->content = xmlStrncatNew(node->content, content, len);
6546 } else {
6547 node->content = xmlStrncat(node->content, content, len);
6548 }
Daniel Veillard8874b942005-08-25 13:19:21 +00006549 node->properties = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006550 if (node->content == NULL)
6551 return(-1);
6552 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006553}
6554
6555/************************************************************************
6556 * *
6557 * Output : to a FILE or in memory *
6558 * *
6559 ************************************************************************/
6560
Owen Taylor3473f882001-02-23 17:55:21 +00006561/**
6562 * xmlBufferCreate:
6563 *
6564 * routine to create an XML buffer.
6565 * returns the new structure.
6566 */
6567xmlBufferPtr
6568xmlBufferCreate(void) {
6569 xmlBufferPtr ret;
6570
6571 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6572 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006573 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006574 return(NULL);
6575 }
6576 ret->use = 0;
Daniel Veillarde356c282001-03-10 12:32:04 +00006577 ret->size = xmlDefaultBufferSize;
Owen Taylor3473f882001-02-23 17:55:21 +00006578 ret->alloc = xmlBufferAllocScheme;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00006579 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00006580 if (ret->content == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006581 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006582 xmlFree(ret);
6583 return(NULL);
6584 }
6585 ret->content[0] = 0;
6586 return(ret);
6587}
6588
6589/**
6590 * xmlBufferCreateSize:
6591 * @size: initial size of buffer
6592 *
6593 * routine to create an XML buffer.
6594 * returns the new structure.
6595 */
6596xmlBufferPtr
6597xmlBufferCreateSize(size_t size) {
6598 xmlBufferPtr ret;
6599
6600 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6601 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006602 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006603 return(NULL);
6604 }
6605 ret->use = 0;
6606 ret->alloc = xmlBufferAllocScheme;
6607 ret->size = (size ? size+2 : 0); /* +1 for ending null */
6608 if (ret->size){
Daniel Veillard3c908dc2003-04-19 00:07:51 +00006609 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00006610 if (ret->content == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006611 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006612 xmlFree(ret);
6613 return(NULL);
6614 }
6615 ret->content[0] = 0;
6616 } else
6617 ret->content = NULL;
6618 return(ret);
6619}
6620
6621/**
Daniel Veillard53350552003-09-18 13:35:51 +00006622 * xmlBufferCreateStatic:
6623 * @mem: the memory area
6624 * @size: the size in byte
6625 *
MST 2003 John Flecka0e7e932003-12-19 03:13:47 +00006626 * routine to create an XML buffer from an immutable memory area.
6627 * The area won't be modified nor copied, and is expected to be
Daniel Veillard53350552003-09-18 13:35:51 +00006628 * present until the end of the buffer lifetime.
6629 *
6630 * returns the new structure.
6631 */
6632xmlBufferPtr
6633xmlBufferCreateStatic(void *mem, size_t size) {
6634 xmlBufferPtr ret;
6635
6636 if ((mem == NULL) || (size == 0))
6637 return(NULL);
6638
6639 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6640 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006641 xmlTreeErrMemory("creating buffer");
Daniel Veillard53350552003-09-18 13:35:51 +00006642 return(NULL);
6643 }
6644 ret->use = size;
6645 ret->size = size;
6646 ret->alloc = XML_BUFFER_ALLOC_IMMUTABLE;
6647 ret->content = (xmlChar *) mem;
6648 return(ret);
6649}
6650
6651/**
Owen Taylor3473f882001-02-23 17:55:21 +00006652 * xmlBufferSetAllocationScheme:
Daniel Veillardbd9afb52002-09-25 22:25:35 +00006653 * @buf: the buffer to tune
Owen Taylor3473f882001-02-23 17:55:21 +00006654 * @scheme: allocation scheme to use
6655 *
6656 * Sets the allocation scheme for this buffer
6657 */
6658void
6659xmlBufferSetAllocationScheme(xmlBufferPtr buf,
6660 xmlBufferAllocationScheme scheme) {
6661 if (buf == NULL) {
6662#ifdef DEBUG_BUFFER
6663 xmlGenericError(xmlGenericErrorContext,
6664 "xmlBufferSetAllocationScheme: buf == NULL\n");
6665#endif
6666 return;
6667 }
Daniel Veillard53350552003-09-18 13:35:51 +00006668 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00006669
6670 buf->alloc = scheme;
6671}
6672
6673/**
6674 * xmlBufferFree:
6675 * @buf: the buffer to free
6676 *
Daniel Veillard9d06d302002-01-22 18:15:52 +00006677 * Frees an XML buffer. It frees both the content and the structure which
6678 * encapsulate it.
Owen Taylor3473f882001-02-23 17:55:21 +00006679 */
6680void
6681xmlBufferFree(xmlBufferPtr buf) {
6682 if (buf == NULL) {
6683#ifdef DEBUG_BUFFER
6684 xmlGenericError(xmlGenericErrorContext,
6685 "xmlBufferFree: buf == NULL\n");
6686#endif
6687 return;
6688 }
Daniel Veillard53350552003-09-18 13:35:51 +00006689
6690 if ((buf->content != NULL) &&
6691 (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006692 xmlFree(buf->content);
6693 }
Owen Taylor3473f882001-02-23 17:55:21 +00006694 xmlFree(buf);
6695}
6696
6697/**
6698 * xmlBufferEmpty:
6699 * @buf: the buffer
6700 *
6701 * empty a buffer.
6702 */
6703void
6704xmlBufferEmpty(xmlBufferPtr buf) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00006705 if (buf == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +00006706 if (buf->content == NULL) return;
6707 buf->use = 0;
Daniel Veillard53350552003-09-18 13:35:51 +00006708 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00006709 buf->content = BAD_CAST "";
Daniel Veillard53350552003-09-18 13:35:51 +00006710 } else {
6711 memset(buf->content, 0, buf->size);
6712 }
Owen Taylor3473f882001-02-23 17:55:21 +00006713}
6714
6715/**
6716 * xmlBufferShrink:
6717 * @buf: the buffer to dump
6718 * @len: the number of xmlChar to remove
6719 *
6720 * Remove the beginning of an XML buffer.
6721 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006722 * Returns the number of #xmlChar removed, or -1 in case of failure.
Owen Taylor3473f882001-02-23 17:55:21 +00006723 */
6724int
6725xmlBufferShrink(xmlBufferPtr buf, unsigned int len) {
Daniel Veillard3d97e662004-11-04 10:49:00 +00006726 if (buf == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006727 if (len == 0) return(0);
6728 if (len > buf->use) return(-1);
6729
6730 buf->use -= len;
Daniel Veillard53350552003-09-18 13:35:51 +00006731 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) {
6732 buf->content += len;
6733 } else {
6734 memmove(buf->content, &buf->content[len], buf->use * sizeof(xmlChar));
6735 buf->content[buf->use] = 0;
6736 }
Owen Taylor3473f882001-02-23 17:55:21 +00006737 return(len);
6738}
6739
6740/**
6741 * xmlBufferGrow:
6742 * @buf: the buffer
6743 * @len: the minimum free size to allocate
6744 *
6745 * Grow the available space of an XML buffer.
6746 *
6747 * Returns the new available space or -1 in case of error
6748 */
6749int
6750xmlBufferGrow(xmlBufferPtr buf, unsigned int len) {
6751 int size;
6752 xmlChar *newbuf;
6753
Daniel Veillard3d97e662004-11-04 10:49:00 +00006754 if (buf == NULL) return(-1);
6755
Daniel Veillard53350552003-09-18 13:35:51 +00006756 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006757 if (len + buf->use < buf->size) return(0);
6758
William M. Brack30fe43f2004-07-26 18:00:58 +00006759/*
6760 * Windows has a BIG problem on realloc timing, so we try to double
6761 * the buffer size (if that's enough) (bug 146697)
6762 */
6763#ifdef WIN32
6764 if (buf->size > len)
6765 size = buf->size * 2;
6766 else
6767 size = buf->use + len + 100;
6768#else
Owen Taylor3473f882001-02-23 17:55:21 +00006769 size = buf->use + len + 100;
William M. Brack30fe43f2004-07-26 18:00:58 +00006770#endif
Owen Taylor3473f882001-02-23 17:55:21 +00006771
6772 newbuf = (xmlChar *) xmlRealloc(buf->content, size);
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006773 if (newbuf == NULL) {
6774 xmlTreeErrMemory("growing buffer");
6775 return(-1);
6776 }
Owen Taylor3473f882001-02-23 17:55:21 +00006777 buf->content = newbuf;
6778 buf->size = size;
6779 return(buf->size - buf->use);
6780}
6781
6782/**
6783 * xmlBufferDump:
6784 * @file: the file output
6785 * @buf: the buffer to dump
6786 *
6787 * Dumps an XML buffer to a FILE *.
Daniel Veillardd1640922001-12-17 15:30:10 +00006788 * Returns the number of #xmlChar written
Owen Taylor3473f882001-02-23 17:55:21 +00006789 */
6790int
6791xmlBufferDump(FILE *file, xmlBufferPtr buf) {
6792 int ret;
6793
6794 if (buf == NULL) {
6795#ifdef DEBUG_BUFFER
6796 xmlGenericError(xmlGenericErrorContext,
6797 "xmlBufferDump: buf == NULL\n");
6798#endif
6799 return(0);
6800 }
6801 if (buf->content == NULL) {
6802#ifdef DEBUG_BUFFER
6803 xmlGenericError(xmlGenericErrorContext,
6804 "xmlBufferDump: buf->content == NULL\n");
6805#endif
6806 return(0);
6807 }
Daniel Veillardcd337f02001-11-22 18:20:37 +00006808 if (file == NULL)
6809 file = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00006810 ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
6811 return(ret);
6812}
6813
6814/**
6815 * xmlBufferContent:
6816 * @buf: the buffer
6817 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00006818 * Function to extract the content of a buffer
6819 *
Owen Taylor3473f882001-02-23 17:55:21 +00006820 * Returns the internal content
6821 */
6822
Daniel Veillard5e2dace2001-07-18 19:30:27 +00006823const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00006824xmlBufferContent(const xmlBufferPtr buf)
6825{
6826 if(!buf)
6827 return NULL;
6828
6829 return buf->content;
6830}
6831
6832/**
6833 * xmlBufferLength:
6834 * @buf: the buffer
6835 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00006836 * Function to get the length of a buffer
6837 *
Owen Taylor3473f882001-02-23 17:55:21 +00006838 * Returns the length of data in the internal content
6839 */
6840
6841int
6842xmlBufferLength(const xmlBufferPtr buf)
6843{
6844 if(!buf)
6845 return 0;
6846
6847 return buf->use;
6848}
6849
6850/**
6851 * xmlBufferResize:
6852 * @buf: the buffer to resize
6853 * @size: the desired size
6854 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006855 * Resize a buffer to accommodate minimum size of @size.
Owen Taylor3473f882001-02-23 17:55:21 +00006856 *
6857 * Returns 0 in case of problems, 1 otherwise
6858 */
6859int
6860xmlBufferResize(xmlBufferPtr buf, unsigned int size)
6861{
6862 unsigned int newSize;
6863 xmlChar* rebuf = NULL;
6864
Daniel Veillardd005b9e2004-11-03 17:07:05 +00006865 if (buf == NULL)
6866 return(0);
6867
Daniel Veillard53350552003-09-18 13:35:51 +00006868 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
6869
Owen Taylor3473f882001-02-23 17:55:21 +00006870 /* Don't resize if we don't have to */
6871 if (size < buf->size)
6872 return 1;
6873
6874 /* figure out new size */
6875 switch (buf->alloc){
6876 case XML_BUFFER_ALLOC_DOUBLEIT:
Daniel Veillardbf629492004-04-20 22:20:59 +00006877 /*take care of empty case*/
6878 newSize = (buf->size ? buf->size*2 : size + 10);
Owen Taylor3473f882001-02-23 17:55:21 +00006879 while (size > newSize) newSize *= 2;
6880 break;
6881 case XML_BUFFER_ALLOC_EXACT:
6882 newSize = size+10;
6883 break;
6884 default:
6885 newSize = size+10;
6886 break;
6887 }
6888
6889 if (buf->content == NULL)
Daniel Veillard3c908dc2003-04-19 00:07:51 +00006890 rebuf = (xmlChar *) xmlMallocAtomic(newSize * sizeof(xmlChar));
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006891 else if (buf->size - buf->use < 100) {
Owen Taylor3473f882001-02-23 17:55:21 +00006892 rebuf = (xmlChar *) xmlRealloc(buf->content,
6893 newSize * sizeof(xmlChar));
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006894 } else {
6895 /*
6896 * if we are reallocating a buffer far from being full, it's
6897 * better to make a new allocation and copy only the used range
6898 * and free the old one.
6899 */
6900 rebuf = (xmlChar *) xmlMallocAtomic(newSize * sizeof(xmlChar));
6901 if (rebuf != NULL) {
6902 memcpy(rebuf, buf->content, buf->use);
6903 xmlFree(buf->content);
William M. Brack42331a92004-07-29 07:07:16 +00006904 rebuf[buf->use] = 0;
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006905 }
6906 }
Owen Taylor3473f882001-02-23 17:55:21 +00006907 if (rebuf == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006908 xmlTreeErrMemory("growing buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006909 return 0;
6910 }
6911 buf->content = rebuf;
6912 buf->size = newSize;
6913
6914 return 1;
6915}
6916
6917/**
6918 * xmlBufferAdd:
6919 * @buf: the buffer to dump
Daniel Veillardd1640922001-12-17 15:30:10 +00006920 * @str: the #xmlChar string
6921 * @len: the number of #xmlChar to add
Owen Taylor3473f882001-02-23 17:55:21 +00006922 *
Daniel Veillard60087f32001-10-10 09:45:09 +00006923 * Add a string range to an XML buffer. if len == -1, the length of
Owen Taylor3473f882001-02-23 17:55:21 +00006924 * str is recomputed.
William M. Bracka3215c72004-07-31 16:24:01 +00006925 *
6926 * Returns 0 successful, a positive error code number otherwise
6927 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00006928 */
William M. Bracka3215c72004-07-31 16:24:01 +00006929int
Owen Taylor3473f882001-02-23 17:55:21 +00006930xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) {
6931 unsigned int needSize;
6932
Daniel Veillardd005b9e2004-11-03 17:07:05 +00006933 if ((str == NULL) || (buf == NULL)) {
William M. Bracka3215c72004-07-31 16:24:01 +00006934 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006935 }
William M. Bracka3215c72004-07-31 16:24:01 +00006936 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006937 if (len < -1) {
6938#ifdef DEBUG_BUFFER
6939 xmlGenericError(xmlGenericErrorContext,
6940 "xmlBufferAdd: len < 0\n");
6941#endif
William M. Bracka3215c72004-07-31 16:24:01 +00006942 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006943 }
William M. Bracka3215c72004-07-31 16:24:01 +00006944 if (len == 0) return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006945
6946 if (len < 0)
6947 len = xmlStrlen(str);
6948
William M. Bracka3215c72004-07-31 16:24:01 +00006949 if (len <= 0) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006950
6951 needSize = buf->use + len + 2;
6952 if (needSize > buf->size){
6953 if (!xmlBufferResize(buf, needSize)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006954 xmlTreeErrMemory("growing buffer");
William M. Bracka3215c72004-07-31 16:24:01 +00006955 return XML_ERR_NO_MEMORY;
Owen Taylor3473f882001-02-23 17:55:21 +00006956 }
6957 }
6958
6959 memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
6960 buf->use += len;
6961 buf->content[buf->use] = 0;
William M. Bracka3215c72004-07-31 16:24:01 +00006962 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006963}
6964
6965/**
6966 * xmlBufferAddHead:
6967 * @buf: the buffer
Daniel Veillardd1640922001-12-17 15:30:10 +00006968 * @str: the #xmlChar string
6969 * @len: the number of #xmlChar to add
Owen Taylor3473f882001-02-23 17:55:21 +00006970 *
6971 * Add a string range to the beginning of an XML buffer.
Daniel Veillard60087f32001-10-10 09:45:09 +00006972 * if len == -1, the length of @str is recomputed.
William M. Bracka3215c72004-07-31 16:24:01 +00006973 *
6974 * Returns 0 successful, a positive error code number otherwise
6975 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00006976 */
William M. Bracka3215c72004-07-31 16:24:01 +00006977int
Owen Taylor3473f882001-02-23 17:55:21 +00006978xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) {
6979 unsigned int needSize;
6980
Daniel Veillardd005b9e2004-11-03 17:07:05 +00006981 if (buf == NULL)
6982 return(-1);
William M. Bracka3215c72004-07-31 16:24:01 +00006983 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006984 if (str == NULL) {
6985#ifdef DEBUG_BUFFER
6986 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006987 "xmlBufferAddHead: str == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006988#endif
William M. Bracka3215c72004-07-31 16:24:01 +00006989 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006990 }
6991 if (len < -1) {
6992#ifdef DEBUG_BUFFER
6993 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006994 "xmlBufferAddHead: len < 0\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006995#endif
William M. Bracka3215c72004-07-31 16:24:01 +00006996 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006997 }
William M. Bracka3215c72004-07-31 16:24:01 +00006998 if (len == 0) return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006999
7000 if (len < 0)
7001 len = xmlStrlen(str);
7002
William M. Bracka3215c72004-07-31 16:24:01 +00007003 if (len <= 0) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007004
7005 needSize = buf->use + len + 2;
7006 if (needSize > buf->size){
7007 if (!xmlBufferResize(buf, needSize)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007008 xmlTreeErrMemory("growing buffer");
William M. Bracka3215c72004-07-31 16:24:01 +00007009 return XML_ERR_NO_MEMORY;
Owen Taylor3473f882001-02-23 17:55:21 +00007010 }
7011 }
7012
7013 memmove(&buf->content[len], &buf->content[0], buf->use * sizeof(xmlChar));
7014 memmove(&buf->content[0], str, len * sizeof(xmlChar));
7015 buf->use += len;
7016 buf->content[buf->use] = 0;
William M. Bracka3215c72004-07-31 16:24:01 +00007017 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007018}
7019
7020/**
7021 * xmlBufferCat:
William M. Bracka3215c72004-07-31 16:24:01 +00007022 * @buf: the buffer to add to
Daniel Veillardd1640922001-12-17 15:30:10 +00007023 * @str: the #xmlChar string
Owen Taylor3473f882001-02-23 17:55:21 +00007024 *
7025 * Append a zero terminated string to an XML buffer.
William M. Bracka3215c72004-07-31 16:24:01 +00007026 *
7027 * Returns 0 successful, a positive error code number otherwise
7028 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00007029 */
William M. Bracka3215c72004-07-31 16:24:01 +00007030int
Owen Taylor3473f882001-02-23 17:55:21 +00007031xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007032 if (buf == NULL)
7033 return(-1);
William M. Bracka3215c72004-07-31 16:24:01 +00007034 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
7035 if (str == NULL) return -1;
7036 return xmlBufferAdd(buf, str, -1);
Owen Taylor3473f882001-02-23 17:55:21 +00007037}
7038
7039/**
7040 * xmlBufferCCat:
7041 * @buf: the buffer to dump
7042 * @str: the C char string
7043 *
7044 * Append a zero terminated C string to an XML buffer.
William M. Bracka3215c72004-07-31 16:24:01 +00007045 *
7046 * Returns 0 successful, a positive error code number otherwise
7047 * and -1 in case of internal or API error.
Owen Taylor3473f882001-02-23 17:55:21 +00007048 */
William M. Bracka3215c72004-07-31 16:24:01 +00007049int
Owen Taylor3473f882001-02-23 17:55:21 +00007050xmlBufferCCat(xmlBufferPtr buf, const char *str) {
7051 const char *cur;
7052
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007053 if (buf == NULL)
7054 return(-1);
William M. Bracka3215c72004-07-31 16:24:01 +00007055 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007056 if (str == NULL) {
7057#ifdef DEBUG_BUFFER
7058 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007059 "xmlBufferCCat: str == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007060#endif
William M. Bracka3215c72004-07-31 16:24:01 +00007061 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007062 }
7063 for (cur = str;*cur != 0;cur++) {
7064 if (buf->use + 10 >= buf->size) {
7065 if (!xmlBufferResize(buf, buf->use+10)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007066 xmlTreeErrMemory("growing buffer");
William M. Bracka3215c72004-07-31 16:24:01 +00007067 return XML_ERR_NO_MEMORY;
Owen Taylor3473f882001-02-23 17:55:21 +00007068 }
7069 }
7070 buf->content[buf->use++] = *cur;
7071 }
7072 buf->content[buf->use] = 0;
William M. Bracka3215c72004-07-31 16:24:01 +00007073 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007074}
7075
7076/**
7077 * xmlBufferWriteCHAR:
7078 * @buf: the XML buffer
7079 * @string: the string to add
7080 *
7081 * routine which manages and grows an output buffer. This one adds
7082 * xmlChars at the end of the buffer.
7083 */
7084void
Daniel Veillard53350552003-09-18 13:35:51 +00007085xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007086 if (buf == NULL)
7087 return;
Daniel Veillard53350552003-09-18 13:35:51 +00007088 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00007089 xmlBufferCat(buf, string);
7090}
7091
7092/**
7093 * xmlBufferWriteChar:
7094 * @buf: the XML buffer output
7095 * @string: the string to add
7096 *
7097 * routine which manage and grows an output buffer. This one add
7098 * C chars at the end of the array.
7099 */
7100void
7101xmlBufferWriteChar(xmlBufferPtr buf, const char *string) {
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007102 if (buf == NULL)
7103 return;
Daniel Veillard53350552003-09-18 13:35:51 +00007104 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00007105 xmlBufferCCat(buf, string);
7106}
7107
7108
7109/**
7110 * xmlBufferWriteQuotedString:
7111 * @buf: the XML buffer output
7112 * @string: the string to add
7113 *
7114 * routine which manage and grows an output buffer. This one writes
Daniel Veillardd1640922001-12-17 15:30:10 +00007115 * a quoted or double quoted #xmlChar string, checking first if it holds
Owen Taylor3473f882001-02-23 17:55:21 +00007116 * quote or double-quotes internally
7117 */
7118void
7119xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) {
Daniel Veillard39057f42003-08-04 01:33:43 +00007120 const xmlChar *cur, *base;
Daniel Veillardd005b9e2004-11-03 17:07:05 +00007121 if (buf == NULL)
7122 return;
Daniel Veillard53350552003-09-18 13:35:51 +00007123 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Daniel Veillard39057f42003-08-04 01:33:43 +00007124 if (xmlStrchr(string, '\"')) {
Daniel Veillard20aa0fb2003-08-04 19:43:15 +00007125 if (xmlStrchr(string, '\'')) {
Owen Taylor3473f882001-02-23 17:55:21 +00007126#ifdef DEBUG_BUFFER
7127 xmlGenericError(xmlGenericErrorContext,
7128 "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n");
7129#endif
Daniel Veillard39057f42003-08-04 01:33:43 +00007130 xmlBufferCCat(buf, "\"");
7131 base = cur = string;
7132 while(*cur != 0){
7133 if(*cur == '"'){
7134 if (base != cur)
7135 xmlBufferAdd(buf, base, cur - base);
7136 xmlBufferAdd(buf, BAD_CAST "&quot;", 6);
7137 cur++;
7138 base = cur;
7139 }
7140 else {
7141 cur++;
7142 }
7143 }
7144 if (base != cur)
7145 xmlBufferAdd(buf, base, cur - base);
7146 xmlBufferCCat(buf, "\"");
Owen Taylor3473f882001-02-23 17:55:21 +00007147 }
Daniel Veillard39057f42003-08-04 01:33:43 +00007148 else{
7149 xmlBufferCCat(buf, "\'");
7150 xmlBufferCat(buf, string);
7151 xmlBufferCCat(buf, "\'");
7152 }
Owen Taylor3473f882001-02-23 17:55:21 +00007153 } else {
7154 xmlBufferCCat(buf, "\"");
7155 xmlBufferCat(buf, string);
7156 xmlBufferCCat(buf, "\"");
7157 }
7158}
7159
7160
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007161/**
7162 * xmlGetDocCompressMode:
7163 * @doc: the document
7164 *
7165 * get the compression ratio for a document, ZLIB based
7166 * Returns 0 (uncompressed) to 9 (max compression)
7167 */
7168int
7169xmlGetDocCompressMode (xmlDocPtr doc) {
7170 if (doc == NULL) return(-1);
7171 return(doc->compression);
7172}
7173
7174/**
7175 * xmlSetDocCompressMode:
7176 * @doc: the document
7177 * @mode: the compression ratio
7178 *
7179 * set the compression ratio for a document, ZLIB based
7180 * Correct values: 0 (uncompressed) to 9 (max compression)
7181 */
7182void
7183xmlSetDocCompressMode (xmlDocPtr doc, int mode) {
7184 if (doc == NULL) return;
7185 if (mode < 0) doc->compression = 0;
7186 else if (mode > 9) doc->compression = 9;
7187 else doc->compression = mode;
7188}
7189
7190/**
7191 * xmlGetCompressMode:
7192 *
7193 * get the default compression mode used, ZLIB based.
7194 * Returns 0 (uncompressed) to 9 (max compression)
7195 */
7196int
7197xmlGetCompressMode(void)
7198{
7199 return (xmlCompressMode);
7200}
7201
7202/**
7203 * xmlSetCompressMode:
7204 * @mode: the compression ratio
7205 *
7206 * set the default compression mode used, ZLIB based
7207 * Correct values: 0 (uncompressed) to 9 (max compression)
7208 */
7209void
7210xmlSetCompressMode(int mode) {
7211 if (mode < 0) xmlCompressMode = 0;
7212 else if (mode > 9) xmlCompressMode = 9;
7213 else xmlCompressMode = mode;
7214}
7215
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007216#define XML_TREE_NSMAP_PARENT -1
7217#define XML_TREE_NSMAP_XML -2
7218#define XML_TREE_NSMAP_DOC -3
7219#define XML_TREE_NSMAP_CUSTOM -4
7220
7221typedef struct xmlNsMapItem *xmlNsMapItemPtr;
7222struct xmlNsMapItem {
7223 xmlNsMapItemPtr next;
7224 xmlNsMapItemPtr prev;
7225 xmlNsPtr oldNs; /* old ns decl reference */
7226 xmlNsPtr newNs; /* new ns decl reference */
7227 int shadowDepth; /* Shadowed at this depth */
7228 /*
7229 * depth:
7230 * >= 0 == @node's ns-decls
7231 * -1 == @parent's ns-decls
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00007232 * -2 == the doc->oldNs XML ns-decl
7233 * -3 == the doc->oldNs storage ns-decls
7234 * -4 == ns-decls provided via custom ns-handling
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007235 */
7236 int depth;
7237};
7238
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007239typedef struct xmlNsMap *xmlNsMapPtr;
7240struct xmlNsMap {
7241 xmlNsMapItemPtr first;
7242 xmlNsMapItemPtr last;
7243 xmlNsMapItemPtr pool;
7244};
7245
7246#define XML_NSMAP_NOTEMPTY(m) (((m) != NULL) && ((m)->first != NULL))
7247#define XML_NSMAP_FOREACH(m, i) for (i = (m)->first; i != NULL; i = (i)->next)
7248#define XML_NSMAP_POP(m, i) \
7249 i = (m)->last; \
7250 (m)->last = (i)->prev; \
7251 if ((m)->last == NULL) \
7252 (m)->first = NULL; \
7253 else \
7254 (m)->last->next = NULL; \
7255 (i)->next = (m)->pool; \
7256 (m)->pool = i;
7257
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007258/*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007259* xmlDOMWrapNsMapFree:
7260* @map: the ns-map
7261*
7262* Frees the ns-map
7263*/
7264static void
7265xmlDOMWrapNsMapFree(xmlNsMapPtr nsmap)
7266{
7267 xmlNsMapItemPtr cur, tmp;
7268
7269 if (nsmap == NULL)
7270 return;
7271 cur = nsmap->pool;
7272 while (cur != NULL) {
7273 tmp = cur;
7274 cur = cur->next;
7275 xmlFree(tmp);
7276 }
7277 cur = nsmap->first;
7278 while (cur != NULL) {
7279 tmp = cur;
7280 cur = cur->next;
7281 xmlFree(tmp);
7282 }
7283 xmlFree(nsmap);
7284}
7285
7286/*
7287* xmlDOMWrapNsMapAddItem:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007288* @map: the ns-map
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007289* @oldNs: the old ns-struct
7290* @newNs: the new ns-struct
7291* @depth: depth and ns-kind information
7292*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007293* Adds an ns-mapping item.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007294*/
7295static xmlNsMapItemPtr
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00007296xmlDOMWrapNsMapAddItem(xmlNsMapPtr *nsmap, int position,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007297 xmlNsPtr oldNs, xmlNsPtr newNs, int depth)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007298{
7299 xmlNsMapItemPtr ret;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007300 xmlNsMapPtr map;
7301
7302 if (nsmap == NULL)
7303 return(NULL);
7304 if ((position != -1) && (position != 0))
7305 return(NULL);
7306 map = *nsmap;
7307
7308 if (map == NULL) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007309 /*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007310 * Create the ns-map.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007311 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007312 map = (xmlNsMapPtr) xmlMalloc(sizeof(struct xmlNsMap));
7313 if (map == NULL) {
7314 xmlTreeErrMemory("allocating namespace map");
7315 return (NULL);
7316 }
7317 memset(map, 0, sizeof(struct xmlNsMap));
7318 *nsmap = map;
7319 }
7320
7321 if (map->pool != NULL) {
7322 /*
7323 * Reuse an item from the pool.
7324 */
7325 ret = map->pool;
7326 map->pool = ret->next;
7327 memset(ret, 0, sizeof(struct xmlNsMapItem));
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007328 } else {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007329 /*
7330 * Create a new item.
7331 */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007332 ret = (xmlNsMapItemPtr) xmlMalloc(sizeof(struct xmlNsMapItem));
7333 if (ret == NULL) {
7334 xmlTreeErrMemory("allocating namespace map item");
7335 return (NULL);
7336 }
7337 memset(ret, 0, sizeof(struct xmlNsMapItem));
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007338 }
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007339
7340 if (map->first == NULL) {
7341 /*
7342 * First ever.
7343 */
7344 map->first = ret;
7345 map->last = ret;
7346 } else if (position == -1) {
7347 /*
7348 * Append.
7349 */
7350 ret->prev = map->last;
7351 map->last->next = ret;
7352 map->last = ret;
7353 } else if (position == 0) {
7354 /*
7355 * Set on first position.
7356 */
7357 map->first->prev = ret;
7358 ret->next = map->first;
7359 map->first = ret;
7360 } else
7361 return(NULL);
7362
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007363 ret->oldNs = oldNs;
7364 ret->newNs = newNs;
7365 ret->shadowDepth = -1;
7366 ret->depth = depth;
7367 return (ret);
7368}
7369
7370/*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007371* xmlDOMWrapStoreNs:
7372* @doc: the doc
7373* @nsName: the namespace name
7374* @prefix: the prefix
7375*
7376* Creates or reuses an xmlNs struct on doc->oldNs with
7377* the given prefix and namespace name.
7378*
7379* Returns the aquired ns struct or NULL in case of an API
7380* or internal error.
7381*/
7382static xmlNsPtr
7383xmlDOMWrapStoreNs(xmlDocPtr doc,
7384 const xmlChar *nsName,
7385 const xmlChar *prefix)
7386{
7387 xmlNsPtr ns;
7388
7389 if (doc == NULL)
7390 return (NULL);
7391 ns = xmlTreeEnsureXMLDecl(doc);
7392 if (ns == NULL)
7393 return (NULL);
7394 if (ns->next != NULL) {
7395 /* Reuse. */
7396 ns = ns->next;
7397 while (ns != NULL) {
7398 if (((ns->prefix == prefix) ||
7399 xmlStrEqual(ns->prefix, prefix)) &&
7400 xmlStrEqual(ns->href, nsName)) {
7401 return (ns);
7402 }
7403 if (ns->next == NULL)
7404 break;
7405 ns = ns->next;
7406 }
7407 }
7408 /* Create. */
7409 ns->next = xmlNewNs(NULL, nsName, prefix);
7410 return (ns->next);
7411}
7412
7413/*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00007414* xmlDOMWrapNewCtxt:
7415*
7416* Allocates and initializes a new DOM-wrapper context.
7417*
7418* Returns the xmlDOMWrapCtxtPtr or NULL in case of an internal errror.
7419*/
7420xmlDOMWrapCtxtPtr
7421xmlDOMWrapNewCtxt(void)
7422{
7423 xmlDOMWrapCtxtPtr ret;
7424
7425 ret = xmlMalloc(sizeof(xmlDOMWrapCtxt));
7426 if (ret == NULL) {
7427 xmlTreeErrMemory("allocating DOM-wrapper context");
7428 return (NULL);
7429 }
7430 memset(ret, 0, sizeof(xmlDOMWrapCtxt));
7431 return (ret);
7432}
7433
7434/*
7435* xmlDOMWrapFreeCtxt:
7436* @ctxt: the DOM-wrapper context
7437*
7438* Frees the DOM-wrapper context.
7439*/
7440void
7441xmlDOMWrapFreeCtxt(xmlDOMWrapCtxtPtr ctxt)
7442{
7443 if (ctxt == NULL)
7444 return;
7445 if (ctxt->namespaceMap != NULL)
7446 xmlDOMWrapNsMapFree((xmlNsMapPtr) ctxt->namespaceMap);
7447 /*
7448 * TODO: Store the namespace map in the context.
7449 */
7450 xmlFree(ctxt);
7451}
7452
7453/*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007454* xmlTreeLookupNsListByPrefix:
7455* @nsList: a list of ns-structs
7456* @prefix: the searched prefix
7457*
7458* Searches for a ns-decl with the given prefix in @nsList.
7459*
7460* Returns the ns-decl if found, NULL if not found and on
7461* API errors.
7462*/
7463static xmlNsPtr
7464xmlTreeNSListLookupByPrefix(xmlNsPtr nsList, const xmlChar *prefix)
7465{
7466 if (nsList == NULL)
7467 return (NULL);
7468 {
7469 xmlNsPtr ns;
7470 ns = nsList;
7471 do {
7472 if ((prefix == ns->prefix) ||
7473 xmlStrEqual(prefix, ns->prefix)) {
7474 return (ns);
7475 }
7476 ns = ns->next;
7477 } while (ns != NULL);
7478 }
7479 return (NULL);
7480}
7481
7482/*
7483*
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00007484* xmlDOMWrapNSNormGatherInScopeNs:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007485* @map: the namespace map
7486* @node: the node to start with
7487*
7488* Puts in-scope namespaces into the ns-map.
7489*
7490* Returns 0 on success, -1 on API or internal errors.
7491*/
7492static int
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007493xmlDOMWrapNSNormGatherInScopeNs(xmlNsMapPtr *map,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007494 xmlNodePtr node)
7495{
7496 xmlNodePtr cur;
7497 xmlNsPtr ns;
7498 xmlNsMapItemPtr mi;
7499 int shadowed;
7500
7501 if ((map == NULL) || (*map != NULL))
7502 return (-1);
7503 /*
7504 * Get in-scope ns-decls of @parent.
7505 */
7506 cur = node;
7507 while ((cur != NULL) && (cur != (xmlNodePtr) cur->doc)) {
7508 if (cur->type == XML_ELEMENT_NODE) {
7509 if (cur->nsDef != NULL) {
7510 ns = cur->nsDef;
7511 do {
7512 shadowed = 0;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007513 if (XML_NSMAP_NOTEMPTY(*map)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007514 /*
7515 * Skip shadowed prefixes.
7516 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007517 XML_NSMAP_FOREACH(*map, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007518 if ((ns->prefix == mi->newNs->prefix) ||
7519 xmlStrEqual(ns->prefix, mi->newNs->prefix)) {
7520 shadowed = 1;
7521 break;
7522 }
7523 }
7524 }
7525 /*
7526 * Insert mapping.
7527 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007528 mi = xmlDOMWrapNsMapAddItem(map, 0, NULL,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007529 ns, XML_TREE_NSMAP_PARENT);
7530 if (mi == NULL)
7531 return (-1);
7532 if (shadowed)
7533 mi->shadowDepth = 0;
7534 ns = ns->next;
7535 } while (ns != NULL);
7536 }
7537 }
7538 cur = cur->parent;
7539 }
7540 return (0);
7541}
7542
7543/*
7544* XML_TREE_ADOPT_STR: If we have a dest-dict, put @str in the dict;
7545* otherwise copy it, when it was in the source-dict.
7546*/
7547#define XML_TREE_ADOPT_STR(str) \
7548 if (adoptStr && (str != NULL)) { \
7549 if (destDoc->dict) { \
Daniel Veillard7e21fd12005-07-03 21:44:07 +00007550 const xmlChar *old = str; \
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007551 str = xmlDictLookup(destDoc->dict, str, -1); \
Daniel Veillard7e21fd12005-07-03 21:44:07 +00007552 if ((sourceDoc == NULL) || (sourceDoc->dict == NULL) || \
7553 (!xmlDictOwns(sourceDoc->dict, old))) \
Daniel Veillard39e5c892005-07-03 22:48:50 +00007554 xmlFree((char *)old); \
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007555 } else if ((sourceDoc) && (sourceDoc->dict) && \
7556 xmlDictOwns(sourceDoc->dict, str)) { \
7557 str = BAD_CAST xmlStrdup(str); \
7558 } \
7559 }
7560
7561/*
7562* XML_TREE_ADOPT_STR_2: If @str was in the source-dict, then
7563* put it in dest-dict or copy it.
7564*/
7565#define XML_TREE_ADOPT_STR_2(str) \
7566 if (adoptStr && (str != NULL) && (sourceDoc != NULL) && \
7567 (sourceDoc->dict != NULL) && \
7568 xmlDictOwns(sourceDoc->dict, cur->content)) { \
7569 if (destDoc->dict) \
7570 cur->content = (xmlChar *) \
7571 xmlDictLookup(destDoc->dict, cur->content, -1); \
7572 else \
7573 cur->content = xmlStrdup(BAD_CAST cur->content); \
7574 }
7575
7576/*
7577* xmlDOMWrapNSNormAddNsMapItem2:
7578*
7579* For internal use. Adds a ns-decl mapping.
7580*
7581* Returns 0 on success, -1 on internal errors.
7582*/
7583static int
7584xmlDOMWrapNSNormAddNsMapItem2(xmlNsPtr **list, int *size, int *number,
7585 xmlNsPtr oldNs, xmlNsPtr newNs)
7586{
7587 if (*list == NULL) {
7588 *list = (xmlNsPtr *) xmlMalloc(6 * sizeof(xmlNsPtr));
7589 if (*list == NULL) {
7590 xmlTreeErrMemory("alloc ns map item");
7591 return(-1);
7592 }
7593 *size = 3;
7594 *number = 0;
7595 } else if ((*number) >= (*size)) {
7596 *size *= 2;
7597 *list = (xmlNsPtr *) xmlRealloc(*list,
7598 (*size) * 2 * sizeof(xmlNsPtr));
7599 if (*list == NULL) {
7600 xmlTreeErrMemory("realloc ns map item");
7601 return(-1);
7602 }
7603 }
7604 (*list)[2 * (*number)] = oldNs;
7605 (*list)[2 * (*number) +1] = newNs;
7606 (*number)++;
7607 return (0);
7608}
7609
7610/*
7611* xmlDOMWrapRemoveNode:
Daniel Veillard304e78c2005-07-03 16:19:41 +00007612* @ctxt: a DOM wrapper context
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007613* @doc: the doc
7614* @node: the node to be removed.
Daniel Veillard304e78c2005-07-03 16:19:41 +00007615* @options: set of options, unused at the moment
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007616*
7617* Unlinks the given node from its owner.
7618* This will substitute ns-references to node->nsDef for
7619* ns-references to doc->oldNs, thus ensuring the removed
7620* branch to be autark wrt ns-references.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00007621*
7622* NOTE: This function was not intensively tested.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007623*
7624* Returns 0 on success, 1 if the node is not supported,
7625* -1 on API and internal errors.
7626*/
7627int
7628xmlDOMWrapRemoveNode(xmlDOMWrapCtxtPtr ctxt, xmlDocPtr doc,
7629 xmlNodePtr node, int options ATTRIBUTE_UNUSED)
7630{
7631 xmlNsPtr *list = NULL;
7632 int sizeList, nbList, i, j;
7633 xmlNsPtr ns;
7634
7635 if ((node == NULL) || (doc == NULL) || (node->doc != doc))
7636 return (-1);
7637
7638 /* TODO: 0 or -1 ? */
7639 if (node->parent == NULL)
7640 return (0);
7641
7642 switch (node->type) {
7643 case XML_TEXT_NODE:
7644 case XML_CDATA_SECTION_NODE:
7645 case XML_ENTITY_REF_NODE:
7646 case XML_PI_NODE:
7647 case XML_COMMENT_NODE:
7648 xmlUnlinkNode(node);
7649 return (0);
7650 case XML_ELEMENT_NODE:
7651 case XML_ATTRIBUTE_NODE:
7652 break;
7653 default:
7654 return (1);
7655 }
7656 xmlUnlinkNode(node);
7657 /*
7658 * Save out-of-scope ns-references in doc->oldNs.
7659 */
7660 do {
7661 switch (node->type) {
7662 case XML_ELEMENT_NODE:
7663 if ((ctxt == NULL) && (node->nsDef != NULL)) {
7664 ns = node->nsDef;
7665 do {
7666 if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
7667 &nbList, ns, ns) == -1)
7668 goto internal_error;
7669 ns = ns->next;
7670 } while (ns != NULL);
7671 }
7672 /* No break on purpose. */
7673 case XML_ATTRIBUTE_NODE:
7674 if (node->ns != NULL) {
7675 /*
7676 * Find a mapping.
7677 */
7678 if (list != NULL) {
7679 for (i = 0, j = 0; i < nbList; i++, j += 2) {
7680 if (node->ns == list[j]) {
7681 node->ns = list[++j];
7682 goto next_node;
7683 }
7684 }
7685 }
7686 ns = NULL;
7687 if (ctxt != NULL) {
7688 /*
7689 * User defined.
7690 */
7691 } else {
7692 /*
7693 * Add to doc's oldNs.
7694 */
7695 ns = xmlDOMWrapStoreNs(doc, node->ns->href,
7696 node->ns->prefix);
7697 if (ns == NULL)
7698 goto internal_error;
7699 }
7700 if (ns != NULL) {
7701 /*
7702 * Add mapping.
7703 */
7704 if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList,
7705 &nbList, node->ns, ns) == -1)
7706 goto internal_error;
7707 }
7708 node->ns = ns;
7709 }
7710 if ((node->type == XML_ELEMENT_NODE) &&
7711 (node->properties != NULL)) {
7712 node = (xmlNodePtr) node->properties;
7713 continue;
7714 }
7715 break;
7716 default:
7717 goto next_sibling;
7718 }
7719next_node:
7720 if ((node->type == XML_ELEMENT_NODE) &&
7721 (node->children != NULL)) {
7722 node = node->children;
7723 continue;
7724 }
7725next_sibling:
7726 if (node == NULL)
7727 break;
7728 if (node->next != NULL)
7729 node = node->next;
7730 else {
7731 node = node->parent;
7732 goto next_sibling;
7733 }
7734 } while (node != NULL);
7735
7736 if (list != NULL)
7737 xmlFree(list);
7738 return (0);
7739
7740internal_error:
7741 if (list != NULL)
7742 xmlFree(list);
7743 return (-1);
7744}
7745
7746/*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007747* xmlSearchNsByNamespaceStrict:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007748* @doc: the document
7749* @node: the start node
7750* @nsName: the searched namespace name
7751* @retNs: the resulting ns-decl
7752* @prefixed: if the found ns-decl must have a prefix (for attributes)
7753*
7754* Dynamically searches for a ns-declaration which matches
7755* the given @nsName in the ancestor-or-self axis of @node.
7756*
7757* Returns 1 if a ns-decl was found, 0 if not and -1 on API
7758* and internal errors.
7759*/
7760static int
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007761xmlSearchNsByNamespaceStrict(xmlDocPtr doc, xmlNodePtr node,
7762 const xmlChar* nsName,
7763 xmlNsPtr *retNs, int prefixed)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007764{
7765 xmlNodePtr cur, prev = NULL, out = NULL;
7766 xmlNsPtr ns, prevns;
7767
7768 if ((doc == NULL) || (nsName == NULL) || (retNs == NULL))
7769 return (-1);
7770
7771 *retNs = NULL;
7772 if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) {
7773 *retNs = xmlTreeEnsureXMLDecl(doc);
7774 if (*retNs == NULL)
7775 return (-1);
7776 return (1);
7777 }
7778 cur = node;
7779 do {
7780 if (cur->type == XML_ELEMENT_NODE) {
7781 if (cur->nsDef != NULL) {
7782 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
7783 if (prefixed && (ns->prefix == NULL))
7784 continue;
7785 if (prev != NULL) {
7786 /*
7787 * Check the last level of ns-decls for a
7788 * shadowing prefix.
7789 */
7790 prevns = prev->nsDef;
7791 do {
7792 if ((prevns->prefix == ns->prefix) ||
7793 ((prevns->prefix != NULL) &&
7794 (ns->prefix != NULL) &&
7795 xmlStrEqual(prevns->prefix, ns->prefix))) {
7796 /*
7797 * Shadowed.
7798 */
7799 break;
7800 }
7801 prevns = prevns->next;
7802 } while (prevns != NULL);
7803 if (prevns != NULL)
7804 continue;
7805 }
7806 /*
7807 * Ns-name comparison.
7808 */
7809 if ((nsName == ns->href) ||
7810 xmlStrEqual(nsName, ns->href)) {
7811 /*
7812 * At this point the prefix can only be shadowed,
7813 * if we are the the (at least) 3rd level of
7814 * ns-decls.
7815 */
7816 if (out) {
7817 int ret;
7818
7819 ret = xmlNsInScope(doc, node, prev, ns->prefix);
7820 if (ret < 0)
7821 return (-1);
7822 /*
7823 * TODO: Should we try to find a matching ns-name
7824 * only once? This here keeps on searching.
7825 * I think we should try further since, there might
7826 * be an other matching ns-decl with an unshadowed
7827 * prefix.
7828 */
7829 if (! ret)
7830 continue;
7831 }
7832 *retNs = ns;
7833 return (1);
7834 }
7835 }
7836 out = prev;
7837 prev = cur;
7838 }
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007839 } else if ((cur->type == XML_ENTITY_NODE) ||
7840 (cur->type == XML_ENTITY_DECL))
7841 return (0);
7842 cur = cur->parent;
7843 } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
7844 return (0);
7845}
7846
7847/*
7848* xmlSearchNsByPrefixStrict:
7849* @doc: the document
7850* @node: the start node
7851* @prefix: the searched namespace prefix
7852* @retNs: the resulting ns-decl
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007853*
7854* Dynamically searches for a ns-declaration which matches
7855* the given @nsName in the ancestor-or-self axis of @node.
7856*
7857* Returns 1 if a ns-decl was found, 0 if not and -1 on API
7858* and internal errors.
7859*/
7860static int
7861xmlSearchNsByPrefixStrict(xmlDocPtr doc, xmlNodePtr node,
7862 const xmlChar* prefix,
7863 xmlNsPtr *retNs)
7864{
7865 xmlNodePtr cur;
7866 xmlNsPtr ns;
7867
7868 if ((doc == NULL) || (node == NULL))
7869 return (-1);
7870
7871 if (retNs)
7872 *retNs = NULL;
7873 if (IS_STR_XML(prefix)) {
7874 if (retNs) {
7875 *retNs = xmlTreeEnsureXMLDecl(doc);
7876 if (*retNs == NULL)
7877 return (-1);
7878 }
7879 return (1);
7880 }
7881 cur = node;
7882 do {
7883 if (cur->type == XML_ELEMENT_NODE) {
7884 if (cur->nsDef != NULL) {
7885 ns = cur->nsDef;
7886 do {
7887 if ((prefix == ns->prefix) ||
7888 xmlStrEqual(prefix, ns->prefix))
7889 {
7890 /*
7891 * Disabled namespaces, e.g. xmlns:abc="".
7892 */
7893 if (ns->href == NULL)
7894 return(0);
7895 if (retNs)
7896 *retNs = ns;
7897 return (1);
7898 }
7899 ns = ns->next;
7900 } while (ns != NULL);
7901 }
7902 } else if ((cur->type == XML_ENTITY_NODE) ||
7903 (cur->type == XML_ENTITY_DECL))
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007904 return (0);
7905 cur = cur->parent;
7906 } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur));
7907 return (0);
7908}
7909
7910/*
7911* xmlDOMWrapNSNormDeclareNsForced:
7912* @doc: the doc
7913* @elem: the element-node to declare on
7914* @nsName: the namespace-name of the ns-decl
7915* @prefix: the preferred prefix of the ns-decl
7916* @checkShadow: ensure that the new ns-decl doesn't shadow ancestor ns-decls
7917*
7918* Declares a new namespace on @elem. It tries to use the
7919* given @prefix; if a ns-decl with the given prefix is already existent
7920* on @elem, it will generate an other prefix.
7921*
7922* Returns 1 if a ns-decl was found, 0 if not and -1 on API
7923* and internal errors.
7924*/
7925static xmlNsPtr
7926xmlDOMWrapNSNormDeclareNsForced(xmlDocPtr doc,
7927 xmlNodePtr elem,
7928 const xmlChar *nsName,
7929 const xmlChar *prefix,
7930 int checkShadow)
7931{
7932
7933 xmlNsPtr ret;
7934 char buf[50];
7935 const xmlChar *pref;
7936 int counter = 0;
7937 /*
7938 * Create a ns-decl on @anchor.
7939 */
7940 pref = prefix;
7941 while (1) {
7942 /*
7943 * Lookup whether the prefix is unused in elem's ns-decls.
7944 */
7945 if ((elem->nsDef != NULL) &&
7946 (xmlTreeNSListLookupByPrefix(elem->nsDef, pref) != NULL))
7947 goto ns_next_prefix;
7948 if (checkShadow && elem->parent &&
7949 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
7950 /*
7951 * Does it shadow ancestor ns-decls?
7952 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00007953 if (xmlSearchNsByPrefixStrict(doc, elem->parent, pref, NULL) == 1)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007954 goto ns_next_prefix;
7955 }
7956 ret = xmlNewNs(NULL, nsName, pref);
7957 if (ret == NULL)
7958 return (NULL);
7959 if (elem->nsDef == NULL)
7960 elem->nsDef = ret;
7961 else {
7962 xmlNsPtr ns2 = elem->nsDef;
7963 while (ns2->next != NULL)
7964 ns2 = ns2->next;
7965 ns2->next = ret;
7966 }
7967 return (ret);
7968ns_next_prefix:
7969 counter++;
7970 if (counter > 1000)
7971 return (NULL);
7972 if (prefix == NULL) {
7973 snprintf((char *) buf, sizeof(buf),
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00007974 "ns_%d", counter);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007975 } else
7976 snprintf((char *) buf, sizeof(buf),
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00007977 "%.30s_%d", (char *)prefix, counter);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007978 pref = BAD_CAST buf;
7979 }
7980}
7981
7982/*
7983* xmlDOMWrapNSNormAquireNormalizedNs:
7984* @doc: the doc
7985* @elem: the element-node to declare namespaces on
7986* @ns: the ns-struct to use for the search
7987* @retNs: the found/created ns-struct
7988* @nsMap: the ns-map
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00007989* @depth: the current tree depth
7990* @ancestorsOnly: search in ancestor ns-decls only
7991* @prefixed: if the searched ns-decl must have a prefix (for attributes)
7992*
7993* Searches for a matching ns-name in the ns-decls of @nsMap, if not
7994* found it will either declare it on @elem, or store it in doc->oldNs.
7995* If a new ns-decl needs to be declared on @elem, it tries to use the
7996* @ns->prefix for it, if this prefix is already in use on @elem, it will
7997* change the prefix or the new ns-decl.
7998*
7999* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8000*/
8001static int
8002xmlDOMWrapNSNormAquireNormalizedNs(xmlDocPtr doc,
8003 xmlNodePtr elem,
8004 xmlNsPtr ns,
8005 xmlNsPtr *retNs,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008006 xmlNsMapPtr *nsMap,
8007
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008008 int depth,
8009 int ancestorsOnly,
8010 int prefixed)
8011{
8012 xmlNsMapItemPtr mi;
8013
8014 if ((doc == NULL) || (ns == NULL) || (retNs == NULL) ||
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008015 (nsMap == NULL))
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008016 return (-1);
8017
8018 *retNs = NULL;
8019 /*
8020 * Handle XML namespace.
8021 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008022 if (IS_STR_XML(ns->prefix)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008023 /*
8024 * Insert XML namespace mapping.
8025 */
8026 *retNs = xmlTreeEnsureXMLDecl(doc);
8027 if (*retNs == NULL)
8028 return (-1);
8029 return (0);
8030 }
8031 /*
8032 * If the search should be done in ancestors only and no
8033 * @elem (the first ancestor) was specified, then skip the search.
8034 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008035 if ((XML_NSMAP_NOTEMPTY(*nsMap)) &&
8036 (! (ancestorsOnly && (elem == NULL))))
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008037 {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008038 /*
8039 * Try to find an equal ns-name in in-scope ns-decls.
8040 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008041 XML_NSMAP_FOREACH(*nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008042 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8043 /*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008044 * ancestorsOnly: This should be turned on to gain speed,
8045 * if one knows that the branch itself was already
8046 * ns-wellformed and no stale references existed.
8047 * I.e. it searches in the ancestor axis only.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008048 */
8049 ((! ancestorsOnly) || (mi->depth == XML_TREE_NSMAP_PARENT)) &&
8050 /* Skip shadowed prefixes. */
8051 (mi->shadowDepth == -1) &&
8052 /* Skip xmlns="" or xmlns:foo="". */
8053 ((mi->newNs->href != NULL) &&
8054 (mi->newNs->href[0] != 0)) &&
8055 /* Ensure a prefix if wanted. */
8056 ((! prefixed) || (mi->newNs->prefix != NULL)) &&
8057 /* Equal ns name */
8058 ((mi->newNs->href == ns->href) ||
8059 xmlStrEqual(mi->newNs->href, ns->href))) {
8060 /* Set the mapping. */
8061 mi->oldNs = ns;
8062 *retNs = mi->newNs;
8063 return (0);
8064 }
8065 }
8066 }
8067 /*
8068 * No luck, the namespace is out of scope or shadowed.
8069 */
8070 if (elem == NULL) {
8071 xmlNsPtr tmpns;
8072
8073 /*
8074 * Store ns-decls in "oldNs" of the document-node.
8075 */
8076 tmpns = xmlDOMWrapStoreNs(doc, ns->href, ns->prefix);
8077 if (tmpns == NULL)
8078 return (-1);
8079 /*
8080 * Insert mapping.
8081 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008082 if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008083 tmpns, XML_TREE_NSMAP_DOC) == NULL) {
8084 xmlFreeNs(tmpns);
8085 return (-1);
8086 }
8087 *retNs = tmpns;
8088 } else {
8089 xmlNsPtr tmpns;
8090
8091 tmpns = xmlDOMWrapNSNormDeclareNsForced(doc, elem, ns->href,
8092 ns->prefix, 0);
8093 if (tmpns == NULL)
8094 return (-1);
8095
8096 if (*nsMap != NULL) {
8097 /*
8098 * Does it shadow ancestor ns-decls?
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008099 */
8100 XML_NSMAP_FOREACH(*nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008101 if ((mi->depth < depth) &&
8102 (mi->shadowDepth == -1) &&
8103 ((ns->prefix == mi->newNs->prefix) ||
8104 xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
8105 /*
8106 * Shadows.
8107 */
8108 mi->shadowDepth = depth;
8109 break;
8110 }
8111 }
8112 }
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008113 if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns, tmpns, depth) == NULL) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008114 xmlFreeNs(tmpns);
8115 return (-1);
8116 }
8117 *retNs = tmpns;
8118 }
8119 return (0);
8120}
8121
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008122typedef enum {
8123 XML_DOM_RECONNS_REMOVEREDUND = 1<<0
8124} xmlDOMReconcileNSOptions;
8125
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008126/*
8127* xmlDOMWrapReconcileNamespaces:
Daniel Veillard304e78c2005-07-03 16:19:41 +00008128* @ctxt: DOM wrapper context, unused at the moment
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008129* @elem: the element-node
8130* @options: option flags
8131*
8132* Ensures that ns-references point to ns-decls hold on element-nodes.
8133* Ensures that the tree is namespace wellformed by creating additional
8134* ns-decls where needed. Note that, since prefixes of already existent
8135* ns-decls can be shadowed by this process, it could break QNames in
8136* attribute values or element content.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008137*
8138* NOTE: This function was not intensively tested.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008139*
8140* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008141*/
8142
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008143int
8144xmlDOMWrapReconcileNamespaces(xmlDOMWrapCtxtPtr ctxt ATTRIBUTE_UNUSED,
8145 xmlNodePtr elem,
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008146 int options)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008147{
8148 int depth = -1, adoptns = 0, parnsdone = 0;
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008149 xmlNsPtr ns, prevns;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008150 xmlDocPtr doc;
8151 xmlNodePtr cur, curElem = NULL;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008152 xmlNsMapPtr nsMap = NULL;
8153 xmlNsMapItemPtr /* topmi = NULL, */ mi;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008154 /* @ancestorsOnly should be set by an option flag. */
8155 int ancestorsOnly = 0;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008156 int optRemoveRedundantNS =
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008157 ((xmlDOMReconcileNSOptions) options & XML_DOM_RECONNS_REMOVEREDUND) ? 1 : 0;
8158 xmlNsPtr *listRedund = NULL;
8159 int sizeRedund = 0, nbRedund = 0, ret, i, j;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008160
8161 if ((elem == NULL) || (elem->doc == NULL) ||
8162 (elem->type != XML_ELEMENT_NODE))
8163 return (-1);
8164
8165 doc = elem->doc;
8166 cur = elem;
8167 do {
8168 switch (cur->type) {
8169 case XML_ELEMENT_NODE:
8170 adoptns = 1;
8171 curElem = cur;
8172 depth++;
8173 /*
8174 * Namespace declarations.
8175 */
8176 if (cur->nsDef != NULL) {
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008177 prevns = NULL;
Kasimier T. Buchcike8f8d752006-02-02 12:13:07 +00008178 ns = cur->nsDef;
8179 while (ns != NULL) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008180 if (! parnsdone) {
8181 if ((elem->parent) &&
8182 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8183 /*
8184 * Gather ancestor in-scope ns-decls.
8185 */
8186 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8187 elem->parent) == -1)
8188 goto internal_error;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008189 }
8190 parnsdone = 1;
8191 }
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008192
8193 /*
8194 * Lookup the ns ancestor-axis for equal ns-decls in scope.
8195 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008196 if (optRemoveRedundantNS && XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008197 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008198 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8199 (mi->shadowDepth == -1) &&
8200 ((ns->prefix == mi->newNs->prefix) ||
8201 xmlStrEqual(ns->prefix, mi->newNs->prefix)) &&
8202 ((ns->href == mi->newNs->href) ||
8203 xmlStrEqual(ns->href, mi->newNs->href)))
8204 {
8205 /*
8206 * A redundant ns-decl was found.
8207 * Add it to the list of redundant ns-decls.
8208 */
8209 if (xmlDOMWrapNSNormAddNsMapItem2(&listRedund,
8210 &sizeRedund, &nbRedund, ns, mi->newNs) == -1)
8211 goto internal_error;
8212 /*
8213 * Remove the ns-decl from the element-node.
Kasimier T. Buchcike8f8d752006-02-02 12:13:07 +00008214 */
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008215 if (prevns)
8216 prevns->next = ns->next;
8217 else
Kasimier T. Buchcike8f8d752006-02-02 12:13:07 +00008218 cur->nsDef = ns->next;
8219 goto next_ns_decl;
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008220 }
8221 }
8222 }
8223
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008224 /*
8225 * Skip ns-references handling if the referenced
8226 * ns-decl is declared on the same element.
8227 */
8228 if ((cur->ns != NULL) && adoptns && (cur->ns == ns))
8229 adoptns = 0;
8230 /*
8231 * Does it shadow any ns-decl?
8232 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008233 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8234 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008235 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8236 (mi->shadowDepth == -1) &&
8237 ((ns->prefix == mi->newNs->prefix) ||
8238 xmlStrEqual(ns->prefix, mi->newNs->prefix))) {
8239
8240 mi->shadowDepth = depth;
8241 }
8242 }
8243 }
8244 /*
8245 * Push mapping.
8246 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008247 if (xmlDOMWrapNsMapAddItem(&nsMap, -1, ns, ns,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008248 depth) == NULL)
Kasimier T. Buchcike8f8d752006-02-02 12:13:07 +00008249 goto internal_error;
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008250
8251 prevns = ns;
Kasimier T. Buchcike8f8d752006-02-02 12:13:07 +00008252next_ns_decl:
8253 ns = ns->next;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008254 }
8255 }
8256 if (! adoptns)
8257 goto ns_end;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008258 /* No break on purpose. */
8259 case XML_ATTRIBUTE_NODE:
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008260 /* No ns, no fun. */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008261 if (cur->ns == NULL)
8262 goto ns_end;
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008263
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008264 if (! parnsdone) {
8265 if ((elem->parent) &&
8266 ((xmlNodePtr) elem->parent->doc != elem->parent)) {
8267 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8268 elem->parent) == -1)
8269 goto internal_error;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008270 }
8271 parnsdone = 1;
8272 }
8273 /*
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008274 * Adjust the reference if this was a redundant ns-decl.
8275 */
8276 if (listRedund) {
8277 for (i = 0, j = 0; i < nbRedund; i++, j += 2) {
8278 if (cur->ns == listRedund[j]) {
8279 cur->ns = listRedund[++j];
8280 break;
8281 }
8282 }
8283 }
8284 /*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008285 * Adopt ns-references.
8286 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008287 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008288 /*
8289 * Search for a mapping.
8290 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008291 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008292 if ((mi->shadowDepth == -1) &&
8293 (cur->ns == mi->oldNs)) {
8294
8295 cur->ns = mi->newNs;
8296 goto ns_end;
8297 }
8298 }
8299 }
8300 /*
8301 * Aquire a normalized ns-decl and add it to the map.
8302 */
8303 if (xmlDOMWrapNSNormAquireNormalizedNs(doc, curElem,
8304 cur->ns, &ns,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008305 &nsMap, depth,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008306 ancestorsOnly,
8307 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
8308 goto internal_error;
8309 cur->ns = ns;
8310
8311ns_end:
8312 if ((cur->type == XML_ELEMENT_NODE) &&
8313 (cur->properties != NULL)) {
8314 /*
8315 * Process attributes.
8316 */
8317 cur = (xmlNodePtr) cur->properties;
8318 continue;
8319 }
8320 break;
8321 default:
8322 goto next_sibling;
8323 }
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008324into_content:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008325 if ((cur->type == XML_ELEMENT_NODE) &&
8326 (cur->children != NULL)) {
8327 /*
8328 * Process content of element-nodes only.
8329 */
8330 cur = cur->children;
8331 continue;
8332 }
8333next_sibling:
8334 if (cur == elem)
8335 break;
8336 if (cur->type == XML_ELEMENT_NODE) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008337 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008338 /*
8339 * Pop mappings.
8340 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008341 while ((nsMap->last != NULL) &&
8342 (nsMap->last->depth >= depth))
8343 {
8344 XML_NSMAP_POP(nsMap, mi)
8345 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008346 /*
8347 * Unshadow.
8348 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008349 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008350 if (mi->shadowDepth >= depth)
8351 mi->shadowDepth = -1;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008352 }
8353 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008354 depth--;
8355 }
8356 if (cur->next != NULL)
8357 cur = cur->next;
8358 else {
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008359 if (cur->type == XML_ATTRIBUTE_NODE) {
8360 cur = cur->parent;
8361 goto into_content;
8362 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008363 cur = cur->parent;
8364 goto next_sibling;
8365 }
8366 } while (cur != NULL);
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008367
8368 ret = 0;
8369 goto exit;
8370internal_error:
8371 ret = -1;
8372exit:
Kasimier T. Buchcike8f8d752006-02-02 12:13:07 +00008373 if (listRedund) {
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008374 for (i = 0, j = 0; i < nbRedund; i++, j += 2) {
8375 xmlFreeNs(listRedund[j]);
8376 }
8377 xmlFree(listRedund);
8378 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008379 if (nsMap != NULL)
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008380 xmlDOMWrapNsMapFree(nsMap);
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008381 return (ret);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008382}
8383
8384/*
8385* xmlDOMWrapAdoptBranch:
8386* @ctxt: the optional context for custom processing
8387* @sourceDoc: the optional sourceDoc
8388* @node: the element-node to start with
8389* @destDoc: the destination doc for adoption
Kasimier T. Buchcike01b2fd2006-02-01 16:36:13 +00008390* @destParent: the optional new parent of @node in @destDoc
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008391* @options: option flags
8392*
8393* Ensures that ns-references point to @destDoc: either to
8394* elements->nsDef entries if @destParent is given, or to
8395* @destDoc->oldNs otherwise.
8396* If @destParent is given, it ensures that the tree is namespace
8397* wellformed by creating additional ns-decls where needed.
8398* Note that, since prefixes of already existent ns-decls can be
8399* shadowed by this process, it could break QNames in attribute
8400* values or element content.
8401*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008402* NOTE: This function was not intensively tested.
8403*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008404* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
8405*/
8406static int
8407xmlDOMWrapAdoptBranch(xmlDOMWrapCtxtPtr ctxt,
8408 xmlDocPtr sourceDoc,
8409 xmlNodePtr node,
8410 xmlDocPtr destDoc,
8411 xmlNodePtr destParent,
8412 int options ATTRIBUTE_UNUSED)
8413{
8414 int ret = 0;
8415 xmlNodePtr cur, curElem = NULL;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008416 xmlNsMapPtr nsMap = NULL;
8417 xmlNsMapItemPtr mi;
Daniel Veillard11ce4002006-03-10 00:36:23 +00008418 xmlNsPtr ns = NULL;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008419 int depth = -1, adoptStr = 1;
8420 /* gather @parent's ns-decls. */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008421 int parnsdone;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008422 /* @ancestorsOnly should be set per option. */
8423 int ancestorsOnly = 0;
8424
8425 /*
8426 * Optimize string adoption for equal or none dicts.
8427 */
8428 if ((sourceDoc != NULL) &&
8429 (sourceDoc->dict == destDoc->dict))
8430 adoptStr = 0;
8431 else
8432 adoptStr = 1;
8433
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008434 /*
8435 * Get the ns-map from the context if available.
8436 */
8437 if (ctxt)
8438 nsMap = (xmlNsMapPtr) ctxt->namespaceMap;
8439 /*
8440 * Disable search for ns-decls in the parent-axis of the
8441 * desination element, if:
8442 * 1) there's no destination parent
8443 * 2) custom ns-reference handling is used
8444 */
8445 if ((destParent == NULL) ||
8446 (ctxt && ctxt->getNsForNodeFunc))
8447 {
8448 parnsdone = 1;
8449 } else
8450 parnsdone = 0;
8451
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008452 cur = node;
8453 while (cur != NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008454 /*
8455 * Paranoid source-doc sanity check.
8456 */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008457 if (cur->doc != sourceDoc) {
8458 /*
8459 * We'll assume XIncluded nodes if the doc differs.
8460 * TODO: Do we need to reconciliate XIncluded nodes?
8461 * This here skips XIncluded nodes and tries to handle
8462 * broken sequences.
8463 */
8464 if (cur->next == NULL)
8465 goto leave_node;
8466 do {
8467 cur = cur->next;
8468 if ((cur->type == XML_XINCLUDE_END) ||
8469 (cur->doc == node->doc))
8470 break;
8471 } while (cur->next != NULL);
8472
8473 if (cur->doc != node->doc)
8474 goto leave_node;
8475 }
8476 cur->doc = destDoc;
8477 switch (cur->type) {
8478 case XML_XINCLUDE_START:
8479 case XML_XINCLUDE_END:
8480 /*
8481 * TODO
8482 */
8483 return (-1);
8484 case XML_ELEMENT_NODE:
8485 curElem = cur;
8486 depth++;
8487 /*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008488 * Namespace declarations.
8489 * - ns->href and ns->prefix are never in the dict, so
8490 * we need not move the values over to the destination dict.
8491 * - Note that for custom handling of ns-references,
8492 * the ns-decls need not be stored in the ns-map,
8493 * since they won't be referenced by node->ns.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008494 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008495 if ((cur->nsDef) &&
8496 ((ctxt == NULL) || (ctxt->getNsForNodeFunc == NULL)))
8497 {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008498 if (! parnsdone) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008499 /*
8500 * Gather @parent's in-scope ns-decls.
8501 */
8502 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8503 destParent) == -1)
8504 goto internal_error;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008505 parnsdone = 1;
8506 }
8507 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
8508 /*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008509 * NOTE: ns->prefix and ns->href are never in the dict.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008510 * XML_TREE_ADOPT_STR(ns->prefix)
8511 * XML_TREE_ADOPT_STR(ns->href)
8512 */
8513 /*
8514 * Does it shadow any ns-decl?
8515 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008516 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008517 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008518 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8519 (mi->shadowDepth == -1) &&
8520 ((ns->prefix == mi->newNs->prefix) ||
8521 xmlStrEqual(ns->prefix,
8522 mi->newNs->prefix))) {
8523
8524 mi->shadowDepth = depth;
8525 }
8526 }
8527 }
8528 /*
8529 * Push mapping.
8530 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008531 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008532 ns, ns, depth) == NULL)
8533 goto internal_error;
8534 }
8535 }
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008536 /* No break on purpose. */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008537 case XML_ATTRIBUTE_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008538 /* No namespace, no fun. */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008539 if (cur->ns == NULL)
8540 goto ns_end;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008541
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008542 if (! parnsdone) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008543 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8544 destParent) == -1)
8545 goto internal_error;
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008546 parnsdone = 1;
8547 }
8548 /*
8549 * Adopt ns-references.
8550 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008551 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008552 /*
8553 * Search for a mapping.
8554 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008555 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008556 if ((mi->shadowDepth == -1) &&
8557 (cur->ns == mi->oldNs)) {
8558
8559 cur->ns = mi->newNs;
8560 goto ns_end;
8561 }
8562 }
8563 }
8564 /*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008565 * No matching namespace in scope. We need a new one.
8566 */
8567 if ((ctxt) && (ctxt->getNsForNodeFunc)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008568 /*
8569 * User-defined behaviour.
8570 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008571 ns = ctxt->getNsForNodeFunc(ctxt, cur,
8572 cur->ns->href, cur->ns->prefix);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008573 /*
8574 * Insert mapping if ns is available; it's the users fault
8575 * if not.
8576 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008577 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008578 cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008579 goto internal_error;
8580 cur->ns = ns;
8581 } else {
8582 /*
8583 * Aquire a normalized ns-decl and add it to the map.
8584 */
8585 if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc,
8586 /* ns-decls on curElem or on destDoc->oldNs */
8587 destParent ? curElem : NULL,
8588 cur->ns, &ns,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008589 &nsMap, depth,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008590 ancestorsOnly,
8591 /* ns-decls must be prefixed for attributes. */
8592 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
8593 goto internal_error;
8594 cur->ns = ns;
8595 }
8596ns_end:
8597 /*
8598 * Further node properties.
8599 * TODO: Is this all?
8600 */
8601 XML_TREE_ADOPT_STR(cur->name)
8602 if (cur->type == XML_ELEMENT_NODE) {
8603 cur->psvi = NULL;
8604 cur->line = 0;
8605 cur->extra = 0;
8606 /*
8607 * Walk attributes.
8608 */
8609 if (cur->properties != NULL) {
8610 /*
8611 * Process first attribute node.
8612 */
8613 cur = (xmlNodePtr) cur->properties;
8614 continue;
8615 }
8616 } else {
8617 /*
8618 * Attributes.
8619 */
8620 if ((sourceDoc != NULL) &&
8621 (((xmlAttrPtr) cur)->atype == XML_ATTRIBUTE_ID))
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008622 {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008623 xmlRemoveID(sourceDoc, (xmlAttrPtr) cur);
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008624 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008625 ((xmlAttrPtr) cur)->atype = 0;
8626 ((xmlAttrPtr) cur)->psvi = NULL;
8627 }
8628 break;
8629 case XML_TEXT_NODE:
8630 case XML_CDATA_SECTION_NODE:
8631 /*
8632 * This puts the content in the dest dict, only if
8633 * it was previously in the source dict.
8634 */
8635 XML_TREE_ADOPT_STR_2(cur->content)
8636 goto leave_node;
8637 case XML_ENTITY_REF_NODE:
8638 /*
8639 * Remove reference to the entitity-node.
8640 */
8641 cur->content = NULL;
8642 cur->children = NULL;
8643 cur->last = NULL;
8644 if ((destDoc->intSubset) || (destDoc->extSubset)) {
8645 xmlEntityPtr ent;
8646 /*
8647 * Assign new entity-node if available.
8648 */
8649 ent = xmlGetDocEntity(destDoc, cur->name);
8650 if (ent != NULL) {
8651 cur->content = ent->content;
8652 cur->children = (xmlNodePtr) ent;
8653 cur->last = (xmlNodePtr) ent;
8654 }
8655 }
8656 goto leave_node;
8657 case XML_PI_NODE:
8658 XML_TREE_ADOPT_STR(cur->name)
8659 XML_TREE_ADOPT_STR_2(cur->content)
8660 break;
8661 case XML_COMMENT_NODE:
8662 break;
8663 default:
8664 goto internal_error;
8665 }
8666 /*
8667 * Walk the tree.
8668 */
8669 if (cur->children != NULL) {
8670 cur = cur->children;
8671 continue;
8672 }
8673
8674leave_node:
8675 if (cur == node)
8676 break;
8677 if ((cur->type == XML_ELEMENT_NODE) ||
8678 (cur->type == XML_XINCLUDE_START) ||
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008679 (cur->type == XML_XINCLUDE_END))
8680 {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008681 /*
8682 * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
8683 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008684 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008685 /*
8686 * Pop mappings.
8687 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008688 while ((nsMap->last != NULL) &&
8689 (nsMap->last->depth >= depth))
8690 {
8691 XML_NSMAP_POP(nsMap, mi)
8692 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008693 /*
8694 * Unshadow.
8695 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008696 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008697 if (mi->shadowDepth >= depth)
8698 mi->shadowDepth = -1;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008699 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008700 }
8701 depth--;
8702 }
8703 if (cur->next != NULL)
8704 cur = cur->next;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008705 else if ((cur->type == XML_ATTRIBUTE_NODE) &&
8706 (cur->parent->children != NULL))
8707 {
8708 cur = cur->parent->children;
8709 } else {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008710 cur = cur->parent;
8711 goto leave_node;
8712 }
8713 }
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008714
8715 goto exit;
8716
8717internal_error:
8718 ret = -1;
8719
8720exit:
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008721 /*
8722 * Cleanup.
8723 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008724 if (nsMap != NULL) {
8725 if ((ctxt) && (ctxt->namespaceMap == nsMap)) {
8726 /*
8727 * Just cleanup the map but don't free.
8728 */
8729 if (nsMap->first) {
8730 if (nsMap->pool)
8731 nsMap->last->next = nsMap->pool;
8732 nsMap->pool = nsMap->first;
8733 nsMap->first = NULL;
8734 }
8735 } else
8736 xmlDOMWrapNsMapFree(nsMap);
8737 }
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008738 return(ret);
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00008739}
8740
8741/*
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008742* xmlDOMWrapCloneNode:
8743* @ctxt: the optional context for custom processing
8744* @sourceDoc: the optional sourceDoc
8745* @node: the node to start with
8746* @resNode: the clone of the given @node
8747* @destDoc: the destination doc
8748* @destParent: the optional new parent of @node in @destDoc
Daniel Veillardb2f8f1d2006-04-28 16:30:48 +00008749* @deep: descend into child if set
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008750* @options: option flags
8751*
8752* References of out-of scope ns-decls are remapped to point to @destDoc:
8753* 1) If @destParent is given, then nsDef entries on element-nodes are used
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008754* 2) If *no* @destParent is given, then @destDoc->oldNs entries are used.
8755* This is the case when you don't know already where the cloned branch
8756* will be added to.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008757*
8758* If @destParent is given, it ensures that the tree is namespace
8759* wellformed by creating additional ns-decls where needed.
8760* Note that, since prefixes of already existent ns-decls can be
8761* shadowed by this process, it could break QNames in attribute
8762* values or element content.
8763* TODO:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008764* 1) What to do with XInclude? Currently this returns an error for XInclude.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008765*
8766* Returns 0 if the operation succeeded,
8767* 1 if a node of unsupported (or not yet supported) type was given,
8768* -1 on API/internal errors.
8769*/
8770
8771int
8772xmlDOMWrapCloneNode(xmlDOMWrapCtxtPtr ctxt,
8773 xmlDocPtr sourceDoc,
8774 xmlNodePtr node,
8775 xmlNodePtr *resNode,
8776 xmlDocPtr destDoc,
8777 xmlNodePtr destParent,
8778 int deep,
8779 int options ATTRIBUTE_UNUSED)
8780{
8781 int ret = 0;
8782 xmlNodePtr cur, curElem = NULL;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008783 xmlNsMapPtr nsMap = NULL;
8784 xmlNsMapItemPtr mi;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008785 xmlNsPtr ns;
8786 int depth = -1;
8787 /* int adoptStr = 1; */
8788 /* gather @parent's ns-decls. */
8789 int parnsdone = 0;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008790 /*
8791 * @ancestorsOnly:
8792 * TODO: @ancestorsOnly should be set per option.
8793 *
8794 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008795 int ancestorsOnly = 0;
8796 xmlNodePtr resultClone = NULL, clone = NULL, parentClone = NULL, prevClone = NULL;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008797 xmlNsPtr cloneNs = NULL, *cloneNsDefSlot = NULL;
8798 xmlDictPtr dict; /* The destination dict */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008799
Daniel Veillard11ce4002006-03-10 00:36:23 +00008800 if ((node == NULL) || (resNode == NULL) || (destDoc == NULL))
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008801 return(-1);
8802 /*
8803 * TODO: Initially we support only element-nodes.
8804 */
8805 if (node->type != XML_ELEMENT_NODE)
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008806 return(1);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008807 /*
8808 * Check node->doc sanity.
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008809 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008810 if ((node->doc != NULL) && (sourceDoc != NULL) &&
8811 (node->doc != sourceDoc)) {
8812 /*
8813 * Might be an XIncluded node.
8814 */
8815 return (-1);
8816 }
8817 if (sourceDoc == NULL)
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008818 sourceDoc = node->doc;
Daniel Veillard11ce4002006-03-10 00:36:23 +00008819 if (sourceDoc == NULL)
8820 return (-1);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008821
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008822 dict = destDoc->dict;
8823 /*
8824 * Reuse the namespace map of the context.
8825 */
8826 if (ctxt)
8827 nsMap = (xmlNsMapPtr) ctxt->namespaceMap;
8828
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008829 *resNode = NULL;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008830
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008831 cur = node;
8832 while (cur != NULL) {
8833 if (cur->doc != sourceDoc) {
8834 /*
8835 * We'll assume XIncluded nodes if the doc differs.
8836 * TODO: Do we need to reconciliate XIncluded nodes?
8837 * TODO: This here returns -1 in this case.
8838 */
8839 goto internal_error;
8840 }
8841 /*
8842 * Create a new node.
8843 */
8844 switch (cur->type) {
8845 case XML_XINCLUDE_START:
8846 case XML_XINCLUDE_END:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008847 /*
8848 * TODO: What to do with XInclude?
8849 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008850 goto internal_error;
8851 break;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008852 case XML_ELEMENT_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008853 case XML_TEXT_NODE:
8854 case XML_CDATA_SECTION_NODE:
8855 case XML_COMMENT_NODE:
8856 case XML_PI_NODE:
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008857 case XML_DOCUMENT_FRAG_NODE:
8858 case XML_ENTITY_REF_NODE:
8859 case XML_ENTITY_NODE:
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008860 /*
8861 * Nodes of xmlNode structure.
8862 */
8863 clone = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
8864 if (clone == NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008865 xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating a node");
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008866 goto internal_error;
8867 }
8868 memset(clone, 0, sizeof(xmlNode));
8869 /*
8870 * Set hierachical links.
8871 */
8872 if (resultClone != NULL) {
8873 clone->parent = parentClone;
8874 if (prevClone) {
8875 prevClone->next = clone;
8876 clone->prev = prevClone;
8877 } else
8878 parentClone->children = clone;
8879 } else
8880 resultClone = clone;
8881
8882 break;
8883 case XML_ATTRIBUTE_NODE:
8884 /*
8885 * Attributes (xmlAttr).
8886 */
8887 clone = (xmlNodePtr) xmlMalloc(sizeof(xmlAttr));
8888 if (clone == NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008889 xmlTreeErrMemory("xmlDOMWrapCloneNode(): allocating an attr-node");
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008890 goto internal_error;
8891 }
8892 memset(clone, 0, sizeof(xmlAttr));
8893 /*
8894 * Set hierachical links.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008895 * TODO: Change this to add to the end of attributes.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008896 */
8897 if (resultClone != NULL) {
8898 clone->parent = parentClone;
8899 if (prevClone) {
8900 prevClone->next = clone;
8901 clone->prev = prevClone;
8902 } else
8903 parentClone->properties = (xmlAttrPtr) clone;
8904 } else
8905 resultClone = clone;
8906 break;
8907 default:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008908 /*
8909 * TODO QUESTION: Any other nodes expected?
8910 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008911 goto internal_error;
8912 }
8913
8914 clone->type = cur->type;
8915 clone->doc = destDoc;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008916
8917 /*
8918 * Clone the name of the node if any.
8919 */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008920 if (cur->name == xmlStringText)
8921 clone->name = xmlStringText;
8922 else if (cur->name == xmlStringTextNoenc)
8923 /*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008924 * NOTE: Although xmlStringTextNoenc is never assigned to a node
8925 * in tree.c, it might be set in Libxslt via
8926 * "xsl:disable-output-escaping".
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008927 */
8928 clone->name = xmlStringTextNoenc;
8929 else if (cur->name == xmlStringComment)
8930 clone->name = xmlStringComment;
8931 else if (cur->name != NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008932 DICT_CONST_COPY(cur->name, clone->name);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008933 }
8934
8935 switch (cur->type) {
8936 case XML_XINCLUDE_START:
8937 case XML_XINCLUDE_END:
8938 /*
8939 * TODO
8940 */
8941 return (-1);
8942 case XML_ELEMENT_NODE:
8943 curElem = cur;
8944 depth++;
8945 /*
8946 * Namespace declarations.
8947 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008948 if (cur->nsDef != NULL) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008949 if (! parnsdone) {
8950 if (destParent && (ctxt == NULL)) {
8951 /*
8952 * Gather @parent's in-scope ns-decls.
8953 */
8954 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap,
8955 destParent) == -1)
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008956 goto internal_error;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008957 }
8958 parnsdone = 1;
8959 }
8960 /*
8961 * Clone namespace declarations.
8962 */
8963 cloneNsDefSlot = &(clone->nsDef);
8964 for (ns = cur->nsDef; ns != NULL; ns = ns->next) {
8965 /*
8966 * Create a new xmlNs.
8967 */
8968 cloneNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
8969 if (cloneNs == NULL) {
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008970 xmlTreeErrMemory("xmlDOMWrapCloneNode(): "
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00008971 "allocating namespace");
8972 return(-1);
8973 }
8974 memset(cloneNs, 0, sizeof(xmlNs));
8975 cloneNs->type = XML_LOCAL_NAMESPACE;
8976
8977 if (ns->href != NULL)
8978 cloneNs->href = xmlStrdup(ns->href);
8979 if (ns->prefix != NULL)
8980 cloneNs->prefix = xmlStrdup(ns->prefix);
8981
8982 *cloneNsDefSlot = cloneNs;
8983 cloneNsDefSlot = &(cloneNs->next);
8984
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00008985 /*
8986 * Note that for custom handling of ns-references,
8987 * the ns-decls need not be stored in the ns-map,
8988 * since they won't be referenced by node->ns.
8989 */
8990 if ((ctxt == NULL) ||
8991 (ctxt->getNsForNodeFunc == NULL))
8992 {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00008993 /*
8994 * Does it shadow any ns-decl?
8995 */
8996 if (XML_NSMAP_NOTEMPTY(nsMap)) {
8997 XML_NSMAP_FOREACH(nsMap, mi) {
8998 if ((mi->depth >= XML_TREE_NSMAP_PARENT) &&
8999 (mi->shadowDepth == -1) &&
9000 ((ns->prefix == mi->newNs->prefix) ||
9001 xmlStrEqual(ns->prefix,
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009002 mi->newNs->prefix))) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009003 /*
9004 * Mark as shadowed at the current
9005 * depth.
9006 */
9007 mi->shadowDepth = depth;
9008 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009009 }
9010 }
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009011 /*
9012 * Push mapping.
9013 */
9014 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
9015 ns, cloneNs, depth) == NULL)
9016 goto internal_error;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009017 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009018 }
9019 }
9020 /* cur->ns will be processed further down. */
9021 break;
9022 case XML_ATTRIBUTE_NODE:
9023 /* IDs will be processed further down. */
9024 /* cur->ns will be processed further down. */
9025 break;
9026 case XML_TEXT_NODE:
9027 case XML_CDATA_SECTION_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009028 /*
9029 * Note that this will also cover the values of attributes.
9030 */
9031 DICT_COPY(cur->content, clone->content);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009032 goto leave_node;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009033 case XML_ENTITY_NODE:
9034 /* TODO: What to do here? */
9035 goto leave_node;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009036 case XML_ENTITY_REF_NODE:
9037 if (sourceDoc != destDoc) {
9038 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9039 xmlEntityPtr ent;
9040 /*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009041 * Different doc: Assign new entity-node if available.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009042 */
9043 ent = xmlGetDocEntity(destDoc, cur->name);
9044 if (ent != NULL) {
9045 clone->content = ent->content;
9046 clone->children = (xmlNodePtr) ent;
9047 clone->last = (xmlNodePtr) ent;
9048 }
9049 }
9050 } else {
9051 /*
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009052 * Same doc: Use the current node's entity declaration
9053 * and value.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009054 */
9055 clone->content = cur->content;
9056 clone->children = cur->children;
9057 clone->last = cur->last;
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009058 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009059 goto leave_node;
9060 case XML_PI_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009061 DICT_COPY(cur->content, clone->content);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009062 goto leave_node;
9063 case XML_COMMENT_NODE:
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009064 DICT_COPY(cur->content, clone->content);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009065 goto leave_node;
9066 default:
9067 goto internal_error;
9068 }
9069
9070 if (cur->ns == NULL)
9071 goto end_ns_reference;
9072
9073/* handle_ns_reference: */
9074 /*
9075 ** The following will take care of references to ns-decls ********
9076 ** and is intended only for element- and attribute-nodes.
9077 **
9078 */
9079 if (! parnsdone) {
9080 if (destParent && (ctxt == NULL)) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009081 if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, destParent) == -1)
9082 goto internal_error;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009083 }
9084 parnsdone = 1;
9085 }
9086 /*
9087 * Adopt ns-references.
9088 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009089 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009090 /*
9091 * Search for a mapping.
9092 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009093 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009094 if ((mi->shadowDepth == -1) &&
9095 (cur->ns == mi->oldNs)) {
9096 /*
9097 * This is the nice case: a mapping was found.
9098 */
9099 clone->ns = mi->newNs;
9100 goto end_ns_reference;
9101 }
9102 }
9103 }
9104 /*
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009105 * No matching namespace in scope. We need a new one.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009106 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009107 if ((ctxt != NULL) && (ctxt->getNsForNodeFunc != NULL)) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009108 /*
9109 * User-defined behaviour.
9110 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009111 ns = ctxt->getNsForNodeFunc(ctxt, cur,
9112 cur->ns->href, cur->ns->prefix);
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009113 /*
9114 * Add user's mapping.
9115 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009116 if (xmlDOMWrapNsMapAddItem(&nsMap, -1,
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009117 cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL)
9118 goto internal_error;
9119 clone->ns = ns;
9120 } else {
9121 /*
9122 * Aquire a normalized ns-decl and add it to the map.
9123 */
9124 if (xmlDOMWrapNSNormAquireNormalizedNs(destDoc,
9125 /* ns-decls on curElem or on destDoc->oldNs */
9126 destParent ? curElem : NULL,
9127 cur->ns, &ns,
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009128 &nsMap, depth,
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009129 /* if we need to search only in the ancestor-axis */
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009130 ancestorsOnly,
9131 /* ns-decls must be prefixed for attributes. */
9132 (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1)
9133 goto internal_error;
9134 clone->ns = ns;
9135 }
9136
9137end_ns_reference:
9138
9139 /*
9140 * Some post-processing.
9141 *
9142 * Handle ID attributes.
9143 */
9144 if ((clone->type == XML_ATTRIBUTE_NODE) &&
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009145 (clone->parent != NULL))
9146 {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009147 if (xmlIsID(destDoc, clone->parent, (xmlAttrPtr) clone)) {
9148
9149 xmlChar *idVal;
9150
9151 idVal = xmlNodeListGetString(cur->doc, cur->children, 1);
9152 if (idVal != NULL) {
9153 if (xmlAddID(NULL, destDoc, idVal, (xmlAttrPtr) cur) == NULL) {
9154 /* TODO: error message. */
9155 xmlFree(idVal);
9156 goto internal_error;
9157 }
9158 xmlFree(idVal);
9159 }
9160 }
9161 }
9162 /*
9163 **
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009164 ** The following will traverse the tree **************************
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009165 **
9166 *
9167 * Walk the element's attributes before descending into child-nodes.
9168 */
9169 if ((cur->type == XML_ELEMENT_NODE) && (cur->properties != NULL)) {
9170 prevClone = NULL;
9171 parentClone = clone;
9172 cur = (xmlNodePtr) cur->properties;
9173 continue;
9174 }
9175into_content:
9176 /*
9177 * Descend into child-nodes.
9178 */
9179 if (cur->children != NULL) {
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009180 if (deep || (cur->type == XML_ATTRIBUTE_NODE)) {
9181 prevClone = NULL;
9182 parentClone = clone;
9183 cur = cur->children;
9184 continue;
9185 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009186 }
9187
9188leave_node:
9189 /*
9190 * At this point we are done with the node, its content
9191 * and an element-nodes's attribute-nodes.
9192 */
9193 if (cur == node)
9194 break;
9195 if ((cur->type == XML_ELEMENT_NODE) ||
9196 (cur->type == XML_XINCLUDE_START) ||
9197 (cur->type == XML_XINCLUDE_END)) {
9198 /*
9199 * TODO: Do we expect nsDefs on XML_XINCLUDE_START?
9200 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009201 if (XML_NSMAP_NOTEMPTY(nsMap)) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009202 /*
9203 * Pop mappings.
9204 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009205 while ((nsMap->last != NULL) &&
9206 (nsMap->last->depth >= depth))
9207 {
9208 XML_NSMAP_POP(nsMap, mi)
9209 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009210 /*
9211 * Unshadow.
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009212 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009213 XML_NSMAP_FOREACH(nsMap, mi) {
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009214 if (mi->shadowDepth >= depth)
9215 mi->shadowDepth = -1;
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009216 }
9217 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009218 depth--;
9219 }
9220 if (cur->next != NULL) {
9221 prevClone = clone;
9222 cur = cur->next;
9223 } else if (cur->type != XML_ATTRIBUTE_NODE) {
9224 /*
9225 * Set clone->last.
9226 */
Daniel Veillard11ce4002006-03-10 00:36:23 +00009227 if (clone->parent != NULL)
9228 clone->parent->last = clone;
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009229 clone = clone->parent;
9230 parentClone = clone->parent;
9231 /*
9232 * Process parent --> next;
9233 */
9234 cur = cur->parent;
9235 goto leave_node;
9236 } else {
9237 /* This is for attributes only. */
9238 clone = clone->parent;
9239 parentClone = clone->parent;
9240 /*
9241 * Process parent-element --> children.
9242 */
9243 cur = cur->parent;
9244 goto into_content;
9245 }
9246 }
9247 goto exit;
9248
9249internal_error:
9250 ret = -1;
9251
9252exit:
9253 /*
9254 * Cleanup.
9255 */
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009256 if (nsMap != NULL) {
9257 if ((ctxt) && (ctxt->namespaceMap == nsMap)) {
9258 /*
9259 * Just cleanup the map but don't free.
9260 */
9261 if (nsMap->first) {
9262 if (nsMap->pool)
9263 nsMap->last->next = nsMap->pool;
9264 nsMap->pool = nsMap->first;
9265 nsMap->first = NULL;
9266 }
9267 } else
9268 xmlDOMWrapNsMapFree(nsMap);
9269 }
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009270 /*
9271 * TODO: Should we try a cleanup of the cloned node in case of a
9272 * fatal error?
9273 */
9274 *resNode = resultClone;
9275 return (ret);
9276}
9277
9278/*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009279* xmlDOMWrapAdoptAttr:
9280* @ctxt: the optional context for custom processing
9281* @sourceDoc: the optional source document of attr
9282* @attr: the attribute-node to be adopted
9283* @destDoc: the destination doc for adoption
9284* @destParent: the optional new parent of @attr in @destDoc
9285* @options: option flags
9286*
9287* @attr is adopted by @destDoc.
9288* Ensures that ns-references point to @destDoc: either to
9289* elements->nsDef entries if @destParent is given, or to
9290* @destDoc->oldNs otherwise.
9291*
9292* Returns 0 if succeeded, -1 otherwise and on API/internal errors.
9293*/
9294static int
9295xmlDOMWrapAdoptAttr(xmlDOMWrapCtxtPtr ctxt,
9296 xmlDocPtr sourceDoc,
9297 xmlAttrPtr attr,
9298 xmlDocPtr destDoc,
9299 xmlNodePtr destParent,
9300 int options ATTRIBUTE_UNUSED)
9301{
9302 xmlNodePtr cur;
9303 int adoptStr = 1;
9304
9305 if ((attr == NULL) || (destDoc == NULL))
9306 return (-1);
9307
9308 attr->doc = destDoc;
9309 if (attr->ns != NULL) {
9310 xmlNsPtr ns = NULL;
9311
9312 if (ctxt != NULL) {
9313 /* TODO: User defined. */
9314 }
9315 /* XML Namespace. */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009316 if (IS_STR_XML(attr->ns->prefix)) {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009317 ns = xmlTreeEnsureXMLDecl(destDoc);
9318 } else if (destParent == NULL) {
9319 /*
9320 * Store in @destDoc->oldNs.
9321 */
9322 ns = xmlDOMWrapStoreNs(destDoc, attr->ns->href, attr->ns->prefix);
9323 } else {
9324 /*
9325 * Declare on @destParent.
9326 */
Kasimier T. Buchcik44353412006-03-06 13:26:16 +00009327 if (xmlSearchNsByNamespaceStrict(destDoc, destParent, attr->ns->href,
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009328 &ns, 1) == -1)
9329 goto internal_error;
9330 if (ns == NULL) {
9331 ns = xmlDOMWrapNSNormDeclareNsForced(destDoc, destParent,
9332 attr->ns->href, attr->ns->prefix, 1);
9333 }
9334 }
9335 if (ns == NULL)
9336 goto internal_error;
9337 attr->ns = ns;
9338 }
9339
9340 XML_TREE_ADOPT_STR(attr->name);
9341 attr->atype = 0;
9342 attr->psvi = NULL;
9343 /*
9344 * Walk content.
9345 */
9346 if (attr->children == NULL)
9347 return (0);
9348 cur = attr->children;
9349 while (cur != NULL) {
9350 cur->doc = destDoc;
9351 switch (cur->type) {
9352 case XML_TEXT_NODE:
9353 case XML_CDATA_SECTION_NODE:
9354 XML_TREE_ADOPT_STR_2(cur->content)
9355 break;
9356 case XML_ENTITY_REF_NODE:
9357 /*
9358 * Remove reference to the entitity-node.
9359 */
9360 cur->content = NULL;
9361 cur->children = NULL;
9362 cur->last = NULL;
9363 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9364 xmlEntityPtr ent;
9365 /*
9366 * Assign new entity-node if available.
9367 */
9368 ent = xmlGetDocEntity(destDoc, cur->name);
9369 if (ent != NULL) {
9370 cur->content = ent->content;
9371 cur->children = (xmlNodePtr) ent;
9372 cur->last = (xmlNodePtr) ent;
9373 }
9374 }
9375 break;
9376 default:
9377 break;
9378 }
9379 if (cur->children != NULL) {
9380 cur = cur->children;
9381 continue;
9382 }
9383next_sibling:
9384 if (cur == (xmlNodePtr) attr)
9385 break;
9386 if (cur->next != NULL)
9387 cur = cur->next;
9388 else {
9389 cur = cur->parent;
9390 goto next_sibling;
9391 }
9392 }
9393 return (0);
9394internal_error:
9395 return (-1);
9396}
9397
9398/*
9399* xmlDOMWrapAdoptNode:
9400* @ctxt: the optional context for custom processing
9401* @sourceDoc: the optional sourceDoc
9402* @node: the node to start with
9403* @destDoc: the destination doc
Kasimier T. Buchcik4d9c9482005-06-27 15:04:46 +00009404* @destParent: the optional new parent of @node in @destDoc
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009405* @options: option flags
9406*
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009407* References of out-of scope ns-decls are remapped to point to @destDoc:
9408* 1) If @destParent is given, then nsDef entries on element-nodes are used
9409* 2) If *no* @destParent is given, then @destDoc->oldNs entries are used
9410* This is the case when you have an unliked node and just want to move it
9411* to the context of
9412*
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009413* If @destParent is given, it ensures that the tree is namespace
9414* wellformed by creating additional ns-decls where needed.
9415* Note that, since prefixes of already existent ns-decls can be
9416* shadowed by this process, it could break QNames in attribute
9417* values or element content.
Kasimier T. Buchcik978039b2006-06-16 19:46:26 +00009418* NOTE: This function was not intensively tested.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009419*
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009420* Returns 0 if the operation succeeded,
9421* 1 if a node of unsupported type was given,
9422* 2 if a node of not yet supported type was given and
9423* -1 on API/internal errors.
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009424*/
9425int
9426xmlDOMWrapAdoptNode(xmlDOMWrapCtxtPtr ctxt,
9427 xmlDocPtr sourceDoc,
9428 xmlNodePtr node,
9429 xmlDocPtr destDoc,
9430 xmlNodePtr destParent,
9431 int options)
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009432{
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009433 if ((node == NULL) || (destDoc == NULL) ||
9434 ((destParent != NULL) && (destParent->doc != destDoc)))
9435 return(-1);
9436 /*
9437 * Check node->doc sanity.
9438 */
9439 if ((node->doc != NULL) && (sourceDoc != NULL) &&
9440 (node->doc != sourceDoc)) {
9441 /*
9442 * Might be an XIncluded node.
9443 */
9444 return (-1);
9445 }
9446 if (sourceDoc == NULL)
9447 sourceDoc = node->doc;
9448 if (sourceDoc == destDoc)
9449 return (-1);
9450 switch (node->type) {
9451 case XML_ELEMENT_NODE:
9452 case XML_ATTRIBUTE_NODE:
9453 case XML_TEXT_NODE:
9454 case XML_CDATA_SECTION_NODE:
9455 case XML_ENTITY_REF_NODE:
9456 case XML_PI_NODE:
9457 case XML_COMMENT_NODE:
9458 break;
9459 case XML_DOCUMENT_FRAG_NODE:
Kasimier T. Buchcikcab801b2006-02-03 16:35:27 +00009460 /* TODO: Support document-fragment-nodes. */
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009461 return (2);
9462 default:
9463 return (1);
9464 }
9465 /*
9466 * Unlink only if @node was not already added to @destParent.
9467 */
9468 if ((node->parent != NULL) && (destParent != node->parent))
9469 xmlUnlinkNode(node);
9470
9471 if (node->type == XML_ELEMENT_NODE) {
9472 return (xmlDOMWrapAdoptBranch(ctxt, sourceDoc, node,
9473 destDoc, destParent, options));
9474 } else if (node->type == XML_ATTRIBUTE_NODE) {
9475 return (xmlDOMWrapAdoptAttr(ctxt, sourceDoc,
9476 (xmlAttrPtr) node, destDoc, destParent, options));
9477 } else {
9478 xmlNodePtr cur = node;
9479 int adoptStr = 1;
9480
9481 cur->doc = destDoc;
9482 /*
9483 * Optimize string adoption.
9484 */
9485 if ((sourceDoc != NULL) &&
9486 (sourceDoc->dict == destDoc->dict))
9487 adoptStr = 0;
9488 switch (node->type) {
9489 case XML_TEXT_NODE:
9490 case XML_CDATA_SECTION_NODE:
9491 XML_TREE_ADOPT_STR_2(node->content)
9492 break;
9493 case XML_ENTITY_REF_NODE:
9494 /*
9495 * Remove reference to the entitity-node.
9496 */
9497 node->content = NULL;
9498 node->children = NULL;
9499 node->last = NULL;
9500 if ((destDoc->intSubset) || (destDoc->extSubset)) {
9501 xmlEntityPtr ent;
9502 /*
9503 * Assign new entity-node if available.
9504 */
9505 ent = xmlGetDocEntity(destDoc, node->name);
9506 if (ent != NULL) {
9507 node->content = ent->content;
9508 node->children = (xmlNodePtr) ent;
9509 node->last = (xmlNodePtr) ent;
9510 }
9511 }
9512 XML_TREE_ADOPT_STR(node->name)
9513 break;
Daniel Veillard7e21fd12005-07-03 21:44:07 +00009514 case XML_PI_NODE: {
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009515 XML_TREE_ADOPT_STR(node->name)
9516 XML_TREE_ADOPT_STR_2(node->content)
9517 break;
Daniel Veillard7e21fd12005-07-03 21:44:07 +00009518 }
Kasimier T. Buchcikbc0e3c62005-06-27 10:28:23 +00009519 default:
9520 break;
9521 }
9522 }
9523 return (0);
9524}
9525
Daniel Veillard5d4644e2005-04-01 13:11:58 +00009526#define bottom_tree
9527#include "elfgcchack.h"