blob: bea5dfe7658e6f74d362bf2afe1184423049e19a [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
Owen Taylor3473f882001-02-23 17:55:21 +000040
Daniel Veillarda880b122003-04-21 21:36:41 +000041int __xmlRegisterCallbacks = 0;
42
Daniel Veillard56a4cb82001-03-24 17:00:36 +000043xmlNsPtr xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns);
44
45/************************************************************************
46 * *
Daniel Veillard18ec16e2003-10-07 23:16:40 +000047 * Tree memory error handler *
48 * *
49 ************************************************************************/
50/**
51 * xmlTreeErrMemory:
52 * @extra: extra informations
53 *
54 * Handle an out of memory condition
55 */
56static void
57xmlTreeErrMemory(const char *extra)
58{
59 __xmlSimpleError(XML_FROM_TREE, XML_ERR_NO_MEMORY, NULL, NULL, extra);
60}
61
62/**
63 * xmlTreeErr:
64 * @code: the error number
65 * @extra: extra informations
66 *
67 * Handle an out of memory condition
68 */
69static void
70xmlTreeErr(int code, xmlNodePtr node, const char *extra)
71{
72 const char *msg = NULL;
73
74 switch(code) {
75 case XML_TREE_INVALID_HEX:
76 msg = "invalid hexadecimal character value";
77 break;
78 case XML_TREE_INVALID_DEC:
79 msg = "invalid decimal character value";
80 break;
81 case XML_TREE_UNTERMINATED_ENTITY:
82 msg = "unterminated entity reference %15s";
83 break;
84 default:
85 msg = "unexpected error number";
86 }
87 __xmlSimpleError(XML_FROM_TREE, code, node, msg, extra);
88}
89
90/************************************************************************
91 * *
Daniel Veillard56a4cb82001-03-24 17:00:36 +000092 * A few static variables and macros *
93 * *
94 ************************************************************************/
Daniel Veillardd0463562001-10-13 09:15:48 +000095/* #undef xmlStringText */
Daniel Veillard22090732001-07-16 00:06:07 +000096const xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 };
Daniel Veillardd0463562001-10-13 09:15:48 +000097/* #undef xmlStringTextNoenc */
Daniel Veillard22090732001-07-16 00:06:07 +000098const xmlChar xmlStringTextNoenc[] =
Owen Taylor3473f882001-02-23 17:55:21 +000099 { 't', 'e', 'x', 't', 'n', 'o', 'e', 'n', 'c', 0 };
Daniel Veillardd0463562001-10-13 09:15:48 +0000100/* #undef xmlStringComment */
Daniel Veillard22090732001-07-16 00:06:07 +0000101const xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 };
102
Owen Taylor3473f882001-02-23 17:55:21 +0000103static int xmlCompressMode = 0;
104static int xmlCheckDTD = 1;
Owen Taylor3473f882001-02-23 17:55:21 +0000105
Owen Taylor3473f882001-02-23 17:55:21 +0000106#define UPDATE_LAST_CHILD_AND_PARENT(n) if ((n) != NULL) { \
107 xmlNodePtr ulccur = (n)->children; \
108 if (ulccur == NULL) { \
109 (n)->last = NULL; \
110 } else { \
111 while (ulccur->next != NULL) { \
112 ulccur->parent = (n); \
113 ulccur = ulccur->next; \
114 } \
115 ulccur->parent = (n); \
116 (n)->last = ulccur; \
117}}
118
119/* #define DEBUG_BUFFER */
120/* #define DEBUG_TREE */
121
122/************************************************************************
123 * *
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000124 * Functions to move to entities.c once the *
125 * API freeze is smoothen and they can be made public. *
126 * *
127 ************************************************************************/
128#include <libxml/hash.h>
129
Daniel Veillard652327a2003-09-29 18:02:38 +0000130#ifdef LIBXML_TREE_ENABLED
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000131/**
132 * xmlGetEntityFromDtd:
133 * @dtd: A pointer to the DTD to search
134 * @name: The entity name
135 *
136 * Do an entity lookup in the DTD entity hash table and
137 * return the corresponding entity, if found.
138 *
139 * Returns A pointer to the entity structure or NULL if not found.
140 */
141static xmlEntityPtr
142xmlGetEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) {
143 xmlEntitiesTablePtr table;
144
145 if((dtd != NULL) && (dtd->entities != NULL)) {
146 table = (xmlEntitiesTablePtr) dtd->entities;
147 return((xmlEntityPtr) xmlHashLookup(table, name));
148 /* return(xmlGetEntityFromTable(table, name)); */
149 }
150 return(NULL);
151}
152/**
153 * xmlGetParameterEntityFromDtd:
154 * @dtd: A pointer to the DTD to search
155 * @name: The entity name
156 *
157 * Do an entity lookup in the DTD pararmeter entity hash table and
158 * return the corresponding entity, if found.
159 *
160 * Returns A pointer to the entity structure or NULL if not found.
161 */
162static xmlEntityPtr
163xmlGetParameterEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) {
164 xmlEntitiesTablePtr table;
165
166 if ((dtd != NULL) && (dtd->pentities != NULL)) {
167 table = (xmlEntitiesTablePtr) dtd->pentities;
168 return((xmlEntityPtr) xmlHashLookup(table, name));
169 /* return(xmlGetEntityFromTable(table, name)); */
170 }
171 return(NULL);
172}
Daniel Veillard652327a2003-09-29 18:02:38 +0000173#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000174
175/************************************************************************
176 * *
Daniel Veillardc00cda82003-04-07 10:22:39 +0000177 * QName handling helper *
178 * *
179 ************************************************************************/
180
181/**
182 * xmlBuildQName:
183 * @ncname: the Name
184 * @prefix: the prefix
185 * @memory: preallocated memory
186 * @len: preallocated memory length
187 *
188 * Builds the QName @prefix:@ncname in @memory if there is enough space
189 * and prefix is not NULL nor empty, otherwise allocate a new string.
190 * If prefix is NULL or empty it returns ncname.
191 *
192 * Returns the new string which must be freed by the caller if different from
193 * @memory and @ncname or NULL in case of error
194 */
195xmlChar *
196xmlBuildQName(const xmlChar *ncname, const xmlChar *prefix,
197 xmlChar *memory, int len) {
198 int lenn, lenp;
199 xmlChar *ret;
200
Daniel Veillard3b7840c2003-09-11 23:42:01 +0000201 if (ncname == NULL) return(NULL);
202 if (prefix == NULL) return((xmlChar *) ncname);
Daniel Veillardc00cda82003-04-07 10:22:39 +0000203
204 lenn = strlen((char *) ncname);
205 lenp = strlen((char *) prefix);
206
207 if ((memory == NULL) || (len < lenn + lenp + 2)) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000208 ret = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000209 if (ret == NULL) {
210 xmlTreeErrMemory("building QName");
211 return(NULL);
212 }
Daniel Veillardc00cda82003-04-07 10:22:39 +0000213 } else {
214 ret = memory;
215 }
216 memcpy(&ret[0], prefix, lenp);
217 ret[lenp] = ':';
218 memcpy(&ret[lenp + 1], ncname, lenn);
219 ret[lenn + lenp + 1] = 0;
220 return(ret);
221}
222
223/**
224 * xmlSplitQName2:
225 * @name: the full QName
226 * @prefix: a xmlChar **
227 *
228 * parse an XML qualified name string
229 *
230 * [NS 5] QName ::= (Prefix ':')? LocalPart
231 *
232 * [NS 6] Prefix ::= NCName
233 *
234 * [NS 7] LocalPart ::= NCName
235 *
236 * Returns NULL if not a QName, otherwise the local part, and prefix
237 * is updated to get the Prefix if any.
238 */
239
240xmlChar *
241xmlSplitQName2(const xmlChar *name, xmlChar **prefix) {
242 int len = 0;
243 xmlChar *ret = NULL;
244
245 *prefix = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000246 if (name == NULL) return(NULL);
Daniel Veillardc00cda82003-04-07 10:22:39 +0000247
248#ifndef XML_XML_NAMESPACE
249 /* xml: prefix is not really a namespace */
250 if ((name[0] == 'x') && (name[1] == 'm') &&
251 (name[2] == 'l') && (name[3] == ':'))
252 return(NULL);
253#endif
254
255 /* nasty but valid */
256 if (name[0] == ':')
257 return(NULL);
258
259 /*
260 * we are not trying to validate but just to cut, and yes it will
261 * work even if this is as set of UTF-8 encoded chars
262 */
263 while ((name[len] != 0) && (name[len] != ':'))
264 len++;
265
266 if (name[len] == 0)
267 return(NULL);
268
269 *prefix = xmlStrndup(name, len);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000270 if (*prefix == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000271 xmlTreeErrMemory("QName split");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000272 return(NULL);
273 }
Daniel Veillardc00cda82003-04-07 10:22:39 +0000274 ret = xmlStrdup(&name[len + 1]);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000275 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000276 xmlTreeErrMemory("QName split");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000277 if (*prefix != NULL) {
278 xmlFree(*prefix);
279 *prefix = NULL;
280 }
281 return(NULL);
282 }
Daniel Veillardc00cda82003-04-07 10:22:39 +0000283
284 return(ret);
285}
286
Daniel Veillard8d73bcb2003-08-04 01:06:15 +0000287/**
288 * xmlSplitQName3:
289 * @name: the full QName
290 * @len: an int *
291 *
292 * parse an XML qualified name string,i
293 *
294 * returns NULL if it is not a Qualified Name, otherwise, update len
295 * with the lenght in byte of the prefix and return a pointer
296 */
297
298const xmlChar *
299xmlSplitQName3(const xmlChar *name, int *len) {
300 int l = 0;
301
302 if (name == NULL) return(NULL);
303 if (len == NULL) return(NULL);
304
305 /* nasty but valid */
306 if (name[0] == ':')
307 return(NULL);
308
309 /*
310 * we are not trying to validate but just to cut, and yes it will
311 * work even if this is as set of UTF-8 encoded chars
312 */
313 while ((name[l] != 0) && (name[l] != ':'))
314 l++;
315
316 if (name[l] == 0)
317 return(NULL);
318
319 *len = l;
320
321 return(&name[l+1]);
322}
323
Daniel Veillard652327a2003-09-29 18:02:38 +0000324#ifdef LIBXML_TREE_ENABLED
Daniel Veillardc00cda82003-04-07 10:22:39 +0000325/************************************************************************
326 * *
Daniel Veillardd2298792003-02-14 16:54:11 +0000327 * Check Name, NCName and QName strings *
328 * *
329 ************************************************************************/
330
331#define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
332
333/**
334 * xmlValidateNCName:
335 * @value: the value to check
336 * @space: allow spaces in front and end of the string
337 *
338 * Check that a value conforms to the lexical space of NCName
339 *
340 * Returns 0 if this validates, a positive error code number otherwise
341 * and -1 in case of internal or API error.
342 */
343int
344xmlValidateNCName(const xmlChar *value, int space) {
345 const xmlChar *cur = value;
346 int c,l;
347
348 /*
349 * First quick algorithm for ASCII range
350 */
351 if (space)
352 while (IS_BLANK(*cur)) cur++;
353 if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
354 (*cur == '_'))
355 cur++;
356 else
357 goto try_complex;
358 while (((*cur >= 'a') && (*cur <= 'z')) ||
359 ((*cur >= 'A') && (*cur <= 'Z')) ||
360 ((*cur >= '0') && (*cur <= '9')) ||
361 (*cur == '_') || (*cur == '-') || (*cur == '.'))
362 cur++;
363 if (space)
364 while (IS_BLANK(*cur)) cur++;
365 if (*cur == 0)
366 return(0);
367
368try_complex:
369 /*
370 * Second check for chars outside the ASCII range
371 */
372 cur = value;
373 c = CUR_SCHAR(cur, l);
374 if (space) {
375 while (IS_BLANK(c)) {
376 cur += l;
377 c = CUR_SCHAR(cur, l);
378 }
379 }
380 if ((!xmlIsLetter(c)) && (c != '_'))
381 return(1);
382 cur += l;
383 c = CUR_SCHAR(cur, l);
384 while (xmlIsLetter(c) || xmlIsDigit(c) || (c == '.') ||
385 (c == '-') || (c == '_') || xmlIsCombining(c) ||
386 xmlIsExtender(c)) {
387 cur += l;
388 c = CUR_SCHAR(cur, l);
389 }
390 if (space) {
391 while (IS_BLANK(c)) {
392 cur += l;
393 c = CUR_SCHAR(cur, l);
394 }
395 }
396 if (c != 0)
397 return(1);
398
399 return(0);
400}
401
402/**
403 * xmlValidateQName:
404 * @value: the value to check
405 * @space: allow spaces in front and end of the string
406 *
407 * Check that a value conforms to the lexical space of QName
408 *
409 * Returns 0 if this validates, a positive error code number otherwise
410 * and -1 in case of internal or API error.
411 */
412int
413xmlValidateQName(const xmlChar *value, int space) {
414 const xmlChar *cur = value;
415 int c,l;
416
417 /*
418 * First quick algorithm for ASCII range
419 */
420 if (space)
421 while (IS_BLANK(*cur)) cur++;
422 if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
423 (*cur == '_'))
424 cur++;
425 else
426 goto try_complex;
427 while (((*cur >= 'a') && (*cur <= 'z')) ||
428 ((*cur >= 'A') && (*cur <= 'Z')) ||
429 ((*cur >= '0') && (*cur <= '9')) ||
430 (*cur == '_') || (*cur == '-') || (*cur == '.'))
431 cur++;
432 if (*cur == ':') {
433 cur++;
434 if (((*cur >= 'a') && (*cur <= 'z')) ||
435 ((*cur >= 'A') && (*cur <= 'Z')) ||
436 (*cur == '_'))
437 cur++;
438 else
439 goto try_complex;
440 while (((*cur >= 'a') && (*cur <= 'z')) ||
441 ((*cur >= 'A') && (*cur <= 'Z')) ||
442 ((*cur >= '0') && (*cur <= '9')) ||
443 (*cur == '_') || (*cur == '-') || (*cur == '.'))
444 cur++;
445 }
446 if (space)
447 while (IS_BLANK(*cur)) cur++;
448 if (*cur == 0)
449 return(0);
450
451try_complex:
452 /*
453 * Second check for chars outside the ASCII range
454 */
455 cur = value;
456 c = CUR_SCHAR(cur, l);
457 if (space) {
458 while (IS_BLANK(c)) {
459 cur += l;
460 c = CUR_SCHAR(cur, l);
461 }
462 }
463 if ((!xmlIsLetter(c)) && (c != '_'))
464 return(1);
465 cur += l;
466 c = CUR_SCHAR(cur, l);
467 while (xmlIsLetter(c) || xmlIsDigit(c) || (c == '.') ||
468 (c == '-') || (c == '_') || xmlIsCombining(c) ||
469 xmlIsExtender(c)) {
470 cur += l;
471 c = CUR_SCHAR(cur, l);
472 }
473 if (c == ':') {
474 cur += l;
475 c = CUR_SCHAR(cur, l);
476 if ((!xmlIsLetter(c)) && (c != '_'))
477 return(1);
478 cur += l;
479 c = CUR_SCHAR(cur, l);
480 while (xmlIsLetter(c) || xmlIsDigit(c) || (c == '.') ||
481 (c == '-') || (c == '_') || xmlIsCombining(c) ||
482 xmlIsExtender(c)) {
483 cur += l;
484 c = CUR_SCHAR(cur, l);
485 }
486 }
487 if (space) {
488 while (IS_BLANK(c)) {
489 cur += l;
490 c = CUR_SCHAR(cur, l);
491 }
492 }
493 if (c != 0)
494 return(1);
495 return(0);
496}
497
498/**
499 * xmlValidateName:
500 * @value: the value to check
501 * @space: allow spaces in front and end of the string
502 *
503 * Check that a value conforms to the lexical space of Name
504 *
505 * Returns 0 if this validates, a positive error code number otherwise
506 * and -1 in case of internal or API error.
507 */
508int
509xmlValidateName(const xmlChar *value, int space) {
510 const xmlChar *cur = value;
511 int c,l;
512
513 /*
514 * First quick algorithm for ASCII range
515 */
516 if (space)
517 while (IS_BLANK(*cur)) cur++;
518 if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
519 (*cur == '_') || (*cur == ':'))
520 cur++;
521 else
522 goto try_complex;
523 while (((*cur >= 'a') && (*cur <= 'z')) ||
524 ((*cur >= 'A') && (*cur <= 'Z')) ||
525 ((*cur >= '0') && (*cur <= '9')) ||
526 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
527 cur++;
528 if (space)
529 while (IS_BLANK(*cur)) cur++;
530 if (*cur == 0)
531 return(0);
532
533try_complex:
534 /*
535 * Second check for chars outside the ASCII range
536 */
537 cur = value;
538 c = CUR_SCHAR(cur, l);
539 if (space) {
540 while (IS_BLANK(c)) {
541 cur += l;
542 c = CUR_SCHAR(cur, l);
543 }
544 }
545 if ((!xmlIsLetter(c)) && (c != '_') && (c != ':'))
546 return(1);
547 cur += l;
548 c = CUR_SCHAR(cur, l);
549 while (xmlIsLetter(c) || xmlIsDigit(c) || (c == '.') || (c == ':') ||
550 (c == '-') || (c == '_') || xmlIsCombining(c) || xmlIsExtender(c)) {
551 cur += l;
552 c = CUR_SCHAR(cur, l);
553 }
554 if (space) {
555 while (IS_BLANK(c)) {
556 cur += l;
557 c = CUR_SCHAR(cur, l);
558 }
559 }
560 if (c != 0)
561 return(1);
562 return(0);
563}
564
Daniel Veillardd4310742003-02-18 21:12:46 +0000565/**
566 * xmlValidateNMToken:
567 * @value: the value to check
568 * @space: allow spaces in front and end of the string
569 *
570 * Check that a value conforms to the lexical space of NMToken
571 *
572 * Returns 0 if this validates, a positive error code number otherwise
573 * and -1 in case of internal or API error.
574 */
575int
576xmlValidateNMToken(const xmlChar *value, int space) {
577 const xmlChar *cur = value;
578 int c,l;
579
580 /*
581 * First quick algorithm for ASCII range
582 */
583 if (space)
584 while (IS_BLANK(*cur)) cur++;
585 if (((*cur >= 'a') && (*cur <= 'z')) ||
586 ((*cur >= 'A') && (*cur <= 'Z')) ||
587 ((*cur >= '0') && (*cur <= '9')) ||
588 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
589 cur++;
590 else
591 goto try_complex;
592 while (((*cur >= 'a') && (*cur <= 'z')) ||
593 ((*cur >= 'A') && (*cur <= 'Z')) ||
594 ((*cur >= '0') && (*cur <= '9')) ||
595 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
596 cur++;
597 if (space)
598 while (IS_BLANK(*cur)) cur++;
599 if (*cur == 0)
600 return(0);
601
602try_complex:
603 /*
604 * Second check for chars outside the ASCII range
605 */
606 cur = value;
607 c = CUR_SCHAR(cur, l);
608 if (space) {
609 while (IS_BLANK(c)) {
610 cur += l;
611 c = CUR_SCHAR(cur, l);
612 }
613 }
614 if (!(xmlIsLetter(c) || xmlIsDigit(c) || (c == '.') || (c == ':') ||
615 (c == '-') || (c == '_') || xmlIsCombining(c) || xmlIsExtender(c)))
616 return(1);
617 cur += l;
618 c = CUR_SCHAR(cur, l);
619 while (xmlIsLetter(c) || xmlIsDigit(c) || (c == '.') || (c == ':') ||
620 (c == '-') || (c == '_') || xmlIsCombining(c) || xmlIsExtender(c)) {
621 cur += l;
622 c = CUR_SCHAR(cur, l);
623 }
624 if (space) {
625 while (IS_BLANK(c)) {
626 cur += l;
627 c = CUR_SCHAR(cur, l);
628 }
629 }
630 if (c != 0)
631 return(1);
632 return(0);
633}
Daniel Veillard652327a2003-09-29 18:02:38 +0000634#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardd4310742003-02-18 21:12:46 +0000635
Daniel Veillardd2298792003-02-14 16:54:11 +0000636/************************************************************************
637 * *
Owen Taylor3473f882001-02-23 17:55:21 +0000638 * Allocation and deallocation of basic structures *
639 * *
640 ************************************************************************/
641
642/**
643 * xmlSetBufferAllocationScheme:
644 * @scheme: allocation method to use
645 *
646 * Set the buffer allocation method. Types are
647 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
648 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
649 * improves performance
650 */
651void
652xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme) {
653 xmlBufferAllocScheme = scheme;
654}
655
656/**
657 * xmlGetBufferAllocationScheme:
658 *
659 * Types are
660 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
661 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
662 * improves performance
663 *
664 * Returns the current allocation scheme
665 */
666xmlBufferAllocationScheme
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000667xmlGetBufferAllocationScheme(void) {
Daniel Veillarde043ee12001-04-16 14:08:07 +0000668 return(xmlBufferAllocScheme);
Owen Taylor3473f882001-02-23 17:55:21 +0000669}
670
671/**
672 * xmlNewNs:
673 * @node: the element carrying the namespace
674 * @href: the URI associated
675 * @prefix: the prefix for the namespace
676 *
677 * Creation of a new Namespace. This function will refuse to create
678 * a namespace with a similar prefix than an existing one present on this
679 * node.
680 * We use href==NULL in the case of an element creation where the namespace
681 * was not defined.
Daniel Veillardd1640922001-12-17 15:30:10 +0000682 * Returns a new namespace pointer or NULL
Owen Taylor3473f882001-02-23 17:55:21 +0000683 */
684xmlNsPtr
685xmlNewNs(xmlNodePtr node, const xmlChar *href, const xmlChar *prefix) {
686 xmlNsPtr cur;
687
688 if ((node != NULL) && (node->type != XML_ELEMENT_NODE))
689 return(NULL);
690
Daniel Veillard20ee8c02001-10-05 09:18:14 +0000691 if ((prefix != NULL) && (xmlStrEqual(prefix, BAD_CAST "xml")))
692 return(NULL);
693
Owen Taylor3473f882001-02-23 17:55:21 +0000694 /*
695 * Allocate a new Namespace and fill the fields.
696 */
697 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
698 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000699 xmlTreeErrMemory("building namespace");
Owen Taylor3473f882001-02-23 17:55:21 +0000700 return(NULL);
701 }
702 memset(cur, 0, sizeof(xmlNs));
703 cur->type = XML_LOCAL_NAMESPACE;
704
705 if (href != NULL)
706 cur->href = xmlStrdup(href);
707 if (prefix != NULL)
708 cur->prefix = xmlStrdup(prefix);
709
710 /*
711 * Add it at the end to preserve parsing order ...
712 * and checks for existing use of the prefix
713 */
714 if (node != NULL) {
715 if (node->nsDef == NULL) {
716 node->nsDef = cur;
717 } else {
718 xmlNsPtr prev = node->nsDef;
719
720 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
721 (xmlStrEqual(prev->prefix, cur->prefix))) {
722 xmlFreeNs(cur);
723 return(NULL);
724 }
725 while (prev->next != NULL) {
726 prev = prev->next;
727 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
728 (xmlStrEqual(prev->prefix, cur->prefix))) {
729 xmlFreeNs(cur);
730 return(NULL);
731 }
732 }
733 prev->next = cur;
734 }
735 }
736 return(cur);
737}
738
739/**
740 * xmlSetNs:
741 * @node: a node in the document
742 * @ns: a namespace pointer
743 *
744 * Associate a namespace to a node, a posteriori.
745 */
746void
747xmlSetNs(xmlNodePtr node, xmlNsPtr ns) {
748 if (node == NULL) {
749#ifdef DEBUG_TREE
750 xmlGenericError(xmlGenericErrorContext,
751 "xmlSetNs: node == NULL\n");
752#endif
753 return;
754 }
755 node->ns = ns;
756}
757
758/**
759 * xmlFreeNs:
760 * @cur: the namespace pointer
761 *
762 * Free up the structures associated to a namespace
763 */
764void
765xmlFreeNs(xmlNsPtr cur) {
766 if (cur == NULL) {
767#ifdef DEBUG_TREE
768 xmlGenericError(xmlGenericErrorContext,
769 "xmlFreeNs : ns == NULL\n");
770#endif
771 return;
772 }
773 if (cur->href != NULL) xmlFree((char *) cur->href);
774 if (cur->prefix != NULL) xmlFree((char *) cur->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000775 xmlFree(cur);
776}
777
778/**
779 * xmlFreeNsList:
780 * @cur: the first namespace pointer
781 *
782 * Free up all the structures associated to the chained namespaces.
783 */
784void
785xmlFreeNsList(xmlNsPtr cur) {
786 xmlNsPtr next;
787 if (cur == NULL) {
788#ifdef DEBUG_TREE
789 xmlGenericError(xmlGenericErrorContext,
790 "xmlFreeNsList : ns == NULL\n");
791#endif
792 return;
793 }
794 while (cur != NULL) {
795 next = cur->next;
796 xmlFreeNs(cur);
797 cur = next;
798 }
799}
800
801/**
802 * xmlNewDtd:
803 * @doc: the document pointer
804 * @name: the DTD name
805 * @ExternalID: the external ID
806 * @SystemID: the system ID
807 *
808 * Creation of a new DTD for the external subset. To create an
809 * internal subset, use xmlCreateIntSubset().
810 *
811 * Returns a pointer to the new DTD structure
812 */
813xmlDtdPtr
814xmlNewDtd(xmlDocPtr doc, const xmlChar *name,
815 const xmlChar *ExternalID, const xmlChar *SystemID) {
816 xmlDtdPtr cur;
817
818 if ((doc != NULL) && (doc->extSubset != NULL)) {
819#ifdef DEBUG_TREE
820 xmlGenericError(xmlGenericErrorContext,
821 "xmlNewDtd(%s): document %s already have a DTD %s\n",
822 /* !!! */ (char *) name, doc->name,
823 /* !!! */ (char *)doc->extSubset->name);
824#endif
825 return(NULL);
826 }
827
828 /*
829 * Allocate a new DTD and fill the fields.
830 */
831 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
832 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000833 xmlTreeErrMemory("building DTD");
Owen Taylor3473f882001-02-23 17:55:21 +0000834 return(NULL);
835 }
836 memset(cur, 0 , sizeof(xmlDtd));
837 cur->type = XML_DTD_NODE;
838
839 if (name != NULL)
840 cur->name = xmlStrdup(name);
841 if (ExternalID != NULL)
842 cur->ExternalID = xmlStrdup(ExternalID);
843 if (SystemID != NULL)
844 cur->SystemID = xmlStrdup(SystemID);
845 if (doc != NULL)
846 doc->extSubset = cur;
847 cur->doc = doc;
848
Daniel Veillarda880b122003-04-21 21:36:41 +0000849 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +0000850 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000851 return(cur);
852}
853
854/**
855 * xmlGetIntSubset:
856 * @doc: the document pointer
857 *
858 * Get the internal subset of a document
859 * Returns a pointer to the DTD structure or NULL if not found
860 */
861
862xmlDtdPtr
863xmlGetIntSubset(xmlDocPtr doc) {
864 xmlNodePtr cur;
865
866 if (doc == NULL)
867 return(NULL);
868 cur = doc->children;
869 while (cur != NULL) {
870 if (cur->type == XML_DTD_NODE)
871 return((xmlDtdPtr) cur);
872 cur = cur->next;
873 }
874 return((xmlDtdPtr) doc->intSubset);
875}
876
877/**
878 * xmlCreateIntSubset:
879 * @doc: the document pointer
880 * @name: the DTD name
Daniel Veillarde356c282001-03-10 12:32:04 +0000881 * @ExternalID: the external (PUBLIC) ID
Owen Taylor3473f882001-02-23 17:55:21 +0000882 * @SystemID: the system ID
883 *
884 * Create the internal subset of a document
885 * Returns a pointer to the new DTD structure
886 */
887xmlDtdPtr
888xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name,
889 const xmlChar *ExternalID, const xmlChar *SystemID) {
890 xmlDtdPtr cur;
891
892 if ((doc != NULL) && (xmlGetIntSubset(doc) != NULL)) {
893#ifdef DEBUG_TREE
894 xmlGenericError(xmlGenericErrorContext,
895
896 "xmlCreateIntSubset(): document %s already have an internal subset\n",
897 doc->name);
898#endif
899 return(NULL);
900 }
901
902 /*
903 * Allocate a new DTD and fill the fields.
904 */
905 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
906 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000907 xmlTreeErrMemory("building internal subset");
Owen Taylor3473f882001-02-23 17:55:21 +0000908 return(NULL);
909 }
910 memset(cur, 0, sizeof(xmlDtd));
911 cur->type = XML_DTD_NODE;
912
913 if (name != NULL)
914 cur->name = xmlStrdup(name);
915 if (ExternalID != NULL)
916 cur->ExternalID = xmlStrdup(ExternalID);
917 if (SystemID != NULL)
918 cur->SystemID = xmlStrdup(SystemID);
919 if (doc != NULL) {
920 doc->intSubset = cur;
921 cur->parent = doc;
922 cur->doc = doc;
923 if (doc->children == NULL) {
924 doc->children = (xmlNodePtr) cur;
925 doc->last = (xmlNodePtr) cur;
926 } else {
Owen Taylor3473f882001-02-23 17:55:21 +0000927 if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillarde356c282001-03-10 12:32:04 +0000928 xmlNodePtr prev;
929
Owen Taylor3473f882001-02-23 17:55:21 +0000930 prev = doc->children;
931 prev->prev = (xmlNodePtr) cur;
932 cur->next = prev;
933 doc->children = (xmlNodePtr) cur;
934 } else {
Daniel Veillarde356c282001-03-10 12:32:04 +0000935 xmlNodePtr next;
936
937 next = doc->children;
938 while ((next != NULL) && (next->type != XML_ELEMENT_NODE))
939 next = next->next;
940 if (next == NULL) {
941 cur->prev = doc->last;
942 cur->prev->next = (xmlNodePtr) cur;
943 cur->next = NULL;
944 doc->last = (xmlNodePtr) cur;
945 } else {
946 cur->next = next;
947 cur->prev = next->prev;
948 if (cur->prev == NULL)
949 doc->children = (xmlNodePtr) cur;
950 else
951 cur->prev->next = (xmlNodePtr) cur;
952 next->prev = (xmlNodePtr) cur;
953 }
Owen Taylor3473f882001-02-23 17:55:21 +0000954 }
955 }
956 }
Daniel Veillard8a1b1852003-01-05 22:37:17 +0000957
Daniel Veillarda880b122003-04-21 21:36:41 +0000958 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard8a1b1852003-01-05 22:37:17 +0000959 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000960 return(cur);
961}
962
963/**
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000964 * DICT_FREE:
965 * @str: a string
966 *
967 * Free a string if it is not owned by the "dict" dictionnary in the
968 * current scope
969 */
970#define DICT_FREE(str) \
971 if ((str) && ((!dict) || \
972 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
973 xmlFree((char *)(str));
974
975/**
Owen Taylor3473f882001-02-23 17:55:21 +0000976 * xmlFreeDtd:
977 * @cur: the DTD structure to free up
978 *
979 * Free a DTD structure.
980 */
981void
982xmlFreeDtd(xmlDtdPtr cur) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000983 xmlDictPtr dict = NULL;
984
Owen Taylor3473f882001-02-23 17:55:21 +0000985 if (cur == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +0000986 return;
987 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000988 if (cur->doc != NULL) dict = cur->doc->dict;
Daniel Veillard5335dc52003-01-01 20:59:38 +0000989
Daniel Veillarda880b122003-04-21 21:36:41 +0000990 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +0000991 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
992
Owen Taylor3473f882001-02-23 17:55:21 +0000993 if (cur->children != NULL) {
994 xmlNodePtr next, c = cur->children;
995
996 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000997 * Cleanup all the DTD comments they are not in the DTD
Owen Taylor3473f882001-02-23 17:55:21 +0000998 * indexes.
999 */
1000 while (c != NULL) {
1001 next = c->next;
Daniel Veillardd72c7e32003-05-12 21:55:03 +00001002 if ((c->type == XML_COMMENT_NODE) || (c->type == XML_PI_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001003 xmlUnlinkNode(c);
1004 xmlFreeNode(c);
1005 }
1006 c = next;
1007 }
1008 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001009 DICT_FREE(cur->name)
1010 DICT_FREE(cur->SystemID)
1011 DICT_FREE(cur->ExternalID)
Owen Taylor3473f882001-02-23 17:55:21 +00001012 /* TODO !!! */
1013 if (cur->notations != NULL)
1014 xmlFreeNotationTable((xmlNotationTablePtr) cur->notations);
1015
1016 if (cur->elements != NULL)
1017 xmlFreeElementTable((xmlElementTablePtr) cur->elements);
1018 if (cur->attributes != NULL)
1019 xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes);
1020 if (cur->entities != NULL)
1021 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities);
1022 if (cur->pentities != NULL)
1023 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->pentities);
1024
Owen Taylor3473f882001-02-23 17:55:21 +00001025 xmlFree(cur);
1026}
1027
1028/**
1029 * xmlNewDoc:
1030 * @version: xmlChar string giving the version of XML "1.0"
1031 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001032 * Creates a new XML document
1033 *
Owen Taylor3473f882001-02-23 17:55:21 +00001034 * Returns a new document
1035 */
1036xmlDocPtr
1037xmlNewDoc(const xmlChar *version) {
1038 xmlDocPtr cur;
1039
1040 if (version == NULL)
1041 version = (const xmlChar *) "1.0";
1042
1043 /*
1044 * Allocate a new document and fill the fields.
1045 */
1046 cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
1047 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001048 xmlTreeErrMemory("building doc");
Owen Taylor3473f882001-02-23 17:55:21 +00001049 return(NULL);
1050 }
1051 memset(cur, 0, sizeof(xmlDoc));
1052 cur->type = XML_DOCUMENT_NODE;
1053
1054 cur->version = xmlStrdup(version);
1055 cur->standalone = -1;
1056 cur->compression = -1; /* not initialized */
1057 cur->doc = cur;
Daniel Veillarda6874ca2003-07-29 16:47:24 +00001058 /*
1059 * The in memory encoding is always UTF8
1060 * This field will never change and would
1061 * be obsolete if not for binary compatibility.
1062 */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00001063 cur->charset = XML_CHAR_ENCODING_UTF8;
Daniel Veillard5335dc52003-01-01 20:59:38 +00001064
Daniel Veillarda880b122003-04-21 21:36:41 +00001065 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001066 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001067 return(cur);
1068}
1069
1070/**
1071 * xmlFreeDoc:
1072 * @cur: pointer to the document
Owen Taylor3473f882001-02-23 17:55:21 +00001073 *
1074 * Free up all the structures used by a document, tree included.
1075 */
1076void
1077xmlFreeDoc(xmlDocPtr cur) {
Daniel Veillarda9142e72001-06-19 11:07:54 +00001078 xmlDtdPtr extSubset, intSubset;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001079 xmlDictPtr dict = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001080
Owen Taylor3473f882001-02-23 17:55:21 +00001081 if (cur == NULL) {
1082#ifdef DEBUG_TREE
1083 xmlGenericError(xmlGenericErrorContext,
1084 "xmlFreeDoc : document == NULL\n");
1085#endif
1086 return;
1087 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001088 if (cur != NULL) dict = cur->dict;
Daniel Veillard5335dc52003-01-01 20:59:38 +00001089
Daniel Veillarda880b122003-04-21 21:36:41 +00001090 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001091 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
1092
Daniel Veillard76d66f42001-05-16 21:05:17 +00001093 /*
1094 * Do this before freeing the children list to avoid ID lookups
1095 */
1096 if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids);
1097 cur->ids = NULL;
1098 if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
1099 cur->refs = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001100 extSubset = cur->extSubset;
1101 intSubset = cur->intSubset;
Daniel Veillard5997aca2002-03-18 18:36:20 +00001102 if (intSubset == extSubset)
1103 extSubset = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001104 if (extSubset != NULL) {
Daniel Veillard76d66f42001-05-16 21:05:17 +00001105 xmlUnlinkNode((xmlNodePtr) cur->extSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001106 cur->extSubset = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001107 xmlFreeDtd(extSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001108 }
Daniel Veillarda9142e72001-06-19 11:07:54 +00001109 if (intSubset != NULL) {
Daniel Veillard76d66f42001-05-16 21:05:17 +00001110 xmlUnlinkNode((xmlNodePtr) cur->intSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001111 cur->intSubset = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001112 xmlFreeDtd(intSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001113 }
1114
1115 if (cur->children != NULL) xmlFreeNodeList(cur->children);
Owen Taylor3473f882001-02-23 17:55:21 +00001116 if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001117
1118 DICT_FREE(cur->version)
1119 DICT_FREE(cur->name)
1120 DICT_FREE(cur->encoding)
1121 DICT_FREE(cur->URL)
Owen Taylor3473f882001-02-23 17:55:21 +00001122 xmlFree(cur);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001123 if (dict) xmlDictFree(dict);
Owen Taylor3473f882001-02-23 17:55:21 +00001124}
1125
1126/**
1127 * xmlStringLenGetNodeList:
1128 * @doc: the document
1129 * @value: the value of the text
1130 * @len: the length of the string value
1131 *
1132 * Parse the value string and build the node list associated. Should
1133 * produce a flat tree with only TEXTs and ENTITY_REFs.
1134 * Returns a pointer to the first child
1135 */
1136xmlNodePtr
1137xmlStringLenGetNodeList(xmlDocPtr doc, const xmlChar *value, int len) {
1138 xmlNodePtr ret = NULL, last = NULL;
1139 xmlNodePtr node;
1140 xmlChar *val;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001141 const xmlChar *cur = value, *end = cur + len;
Owen Taylor3473f882001-02-23 17:55:21 +00001142 const xmlChar *q;
1143 xmlEntityPtr ent;
1144
1145 if (value == NULL) return(NULL);
1146
1147 q = cur;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001148 while ((cur < end) && (*cur != 0)) {
1149 if (cur[0] == '&') {
1150 int charval = 0;
1151 xmlChar tmp;
1152
Owen Taylor3473f882001-02-23 17:55:21 +00001153 /*
1154 * Save the current text.
1155 */
1156 if (cur != q) {
1157 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
1158 xmlNodeAddContentLen(last, q, cur - q);
1159 } else {
1160 node = xmlNewDocTextLen(doc, q, cur - q);
1161 if (node == NULL) return(ret);
1162 if (last == NULL)
1163 last = ret = node;
1164 else {
1165 last->next = node;
1166 node->prev = last;
1167 last = node;
1168 }
1169 }
1170 }
Owen Taylor3473f882001-02-23 17:55:21 +00001171 q = cur;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001172 if ((cur + 2 < end) && (cur[1] == '#') && (cur[2] == 'x')) {
1173 cur += 3;
1174 if (cur < end)
1175 tmp = *cur;
1176 else
1177 tmp = 0;
1178 while (tmp != ';') { /* Non input consuming loop */
1179 if ((tmp >= '0') && (tmp <= '9'))
1180 charval = charval * 16 + (tmp - '0');
1181 else if ((tmp >= 'a') && (tmp <= 'f'))
1182 charval = charval * 16 + (tmp - 'a') + 10;
1183 else if ((tmp >= 'A') && (tmp <= 'F'))
1184 charval = charval * 16 + (tmp - 'A') + 10;
Owen Taylor3473f882001-02-23 17:55:21 +00001185 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001186 xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
1187 NULL);
Daniel Veillard07cb8222003-09-10 10:51:05 +00001188 charval = 0;
1189 break;
1190 }
1191 cur++;
1192 if (cur < end)
1193 tmp = *cur;
1194 else
1195 tmp = 0;
1196 }
1197 if (tmp == ';')
1198 cur++;
1199 q = cur;
1200 } else if ((cur + 1 < end) && (cur[1] == '#')) {
1201 cur += 2;
1202 if (cur < end)
1203 tmp = *cur;
1204 else
1205 tmp = 0;
1206 while (tmp != ';') { /* Non input consuming loops */
1207 if ((tmp >= '0') && (tmp <= '9'))
1208 charval = charval * 10 + (tmp - '0');
1209 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001210 xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
1211 NULL);
Daniel Veillard07cb8222003-09-10 10:51:05 +00001212 charval = 0;
1213 break;
1214 }
1215 cur++;
1216 if (cur < end)
1217 tmp = *cur;
1218 else
1219 tmp = 0;
1220 }
1221 if (tmp == ';')
1222 cur++;
1223 q = cur;
1224 } else {
1225 /*
1226 * Read the entity string
1227 */
1228 cur++;
1229 q = cur;
1230 while ((cur < end) && (*cur != 0) && (*cur != ';')) cur++;
1231 if ((cur >= end) || (*cur == 0)) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001232 xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY, (xmlNodePtr) doc,
1233 (const char *) q);
Daniel Veillard07cb8222003-09-10 10:51:05 +00001234 return(ret);
1235 }
1236 if (cur != q) {
1237 /*
1238 * Predefined entities don't generate nodes
1239 */
1240 val = xmlStrndup(q, cur - q);
1241 ent = xmlGetDocEntity(doc, val);
1242 if ((ent != NULL) &&
1243 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
1244 if (last == NULL) {
1245 node = xmlNewDocText(doc, ent->content);
1246 last = ret = node;
1247 } else if (last->type != XML_TEXT_NODE) {
1248 node = xmlNewDocText(doc, ent->content);
1249 last = xmlAddNextSibling(last, node);
1250 } else
1251 xmlNodeAddContent(last, ent->content);
1252
1253 } else {
1254 /*
1255 * Create a new REFERENCE_REF node
1256 */
1257 node = xmlNewReference(doc, val);
1258 if (node == NULL) {
1259 if (val != NULL) xmlFree(val);
1260 return(ret);
1261 }
1262 else if ((ent != NULL) && (ent->children == NULL)) {
1263 xmlNodePtr temp;
1264
1265 ent->children = xmlStringGetNodeList(doc,
1266 (const xmlChar*)node->content);
1267 ent->owner = 1;
1268 temp = ent->children;
1269 while (temp) {
1270 temp->parent = (xmlNodePtr)ent;
1271 temp = temp->next;
1272 }
1273 }
1274 if (last == NULL) {
1275 last = ret = node;
1276 } else {
1277 last = xmlAddNextSibling(last, node);
1278 }
1279 }
1280 xmlFree(val);
1281 }
1282 cur++;
1283 q = cur;
1284 }
1285 if (charval != 0) {
1286 xmlChar buf[10];
1287 int l;
1288
1289 l = xmlCopyCharMultiByte(buf, charval);
1290 buf[l] = 0;
1291 node = xmlNewDocText(doc, buf);
1292 if (node != NULL) {
1293 if (last == NULL) {
1294 last = ret = node;
1295 } else {
1296 last = xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001297 }
1298 }
Daniel Veillard07cb8222003-09-10 10:51:05 +00001299 charval = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001300 }
Daniel Veillard07cb8222003-09-10 10:51:05 +00001301 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001302 cur++;
1303 }
Daniel Veillard07cb8222003-09-10 10:51:05 +00001304 if ((cur != q) || (ret == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001305 /*
1306 * Handle the last piece of text.
1307 */
1308 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
1309 xmlNodeAddContentLen(last, q, cur - q);
1310 } else {
1311 node = xmlNewDocTextLen(doc, q, cur - q);
1312 if (node == NULL) return(ret);
Daniel Veillard07cb8222003-09-10 10:51:05 +00001313 if (last == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001314 last = ret = node;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001315 } else {
1316 last = xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001317 }
1318 }
1319 }
1320 return(ret);
1321}
1322
1323/**
1324 * xmlStringGetNodeList:
1325 * @doc: the document
1326 * @value: the value of the attribute
1327 *
1328 * Parse the value string and build the node list associated. Should
1329 * produce a flat tree with only TEXTs and ENTITY_REFs.
1330 * Returns a pointer to the first child
1331 */
1332xmlNodePtr
1333xmlStringGetNodeList(xmlDocPtr doc, const xmlChar *value) {
1334 xmlNodePtr ret = NULL, last = NULL;
1335 xmlNodePtr node;
1336 xmlChar *val;
1337 const xmlChar *cur = value;
1338 const xmlChar *q;
1339 xmlEntityPtr ent;
1340
1341 if (value == NULL) return(NULL);
1342
1343 q = cur;
1344 while (*cur != 0) {
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001345 if (cur[0] == '&') {
1346 int charval = 0;
1347 xmlChar tmp;
1348
Owen Taylor3473f882001-02-23 17:55:21 +00001349 /*
1350 * Save the current text.
1351 */
1352 if (cur != q) {
1353 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
1354 xmlNodeAddContentLen(last, q, cur - q);
1355 } else {
1356 node = xmlNewDocTextLen(doc, q, cur - q);
1357 if (node == NULL) return(ret);
1358 if (last == NULL)
1359 last = ret = node;
1360 else {
1361 last->next = node;
1362 node->prev = last;
1363 last = node;
1364 }
1365 }
1366 }
Owen Taylor3473f882001-02-23 17:55:21 +00001367 q = cur;
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001368 if ((cur[1] == '#') && (cur[2] == 'x')) {
1369 cur += 3;
1370 tmp = *cur;
1371 while (tmp != ';') { /* Non input consuming loop */
1372 if ((tmp >= '0') && (tmp <= '9'))
1373 charval = charval * 16 + (tmp - '0');
1374 else if ((tmp >= 'a') && (tmp <= 'f'))
1375 charval = charval * 16 + (tmp - 'a') + 10;
1376 else if ((tmp >= 'A') && (tmp <= 'F'))
1377 charval = charval * 16 + (tmp - 'A') + 10;
Owen Taylor3473f882001-02-23 17:55:21 +00001378 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001379 xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
1380 NULL);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001381 charval = 0;
1382 break;
1383 }
1384 cur++;
1385 tmp = *cur;
1386 }
1387 if (tmp == ';')
1388 cur++;
1389 q = cur;
1390 } else if (cur[1] == '#') {
1391 cur += 2;
1392 tmp = *cur;
1393 while (tmp != ';') { /* Non input consuming loops */
1394 if ((tmp >= '0') && (tmp <= '9'))
1395 charval = charval * 10 + (tmp - '0');
1396 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001397 xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
1398 NULL);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001399 charval = 0;
1400 break;
1401 }
1402 cur++;
1403 tmp = *cur;
1404 }
1405 if (tmp == ';')
1406 cur++;
1407 q = cur;
1408 } else {
1409 /*
1410 * Read the entity string
1411 */
1412 cur++;
1413 q = cur;
1414 while ((*cur != 0) && (*cur != ';')) cur++;
1415 if (*cur == 0) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001416 xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY,
1417 (xmlNodePtr) doc, (const char *) q);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001418 return(ret);
1419 }
1420 if (cur != q) {
1421 /*
1422 * Predefined entities don't generate nodes
1423 */
1424 val = xmlStrndup(q, cur - q);
1425 ent = xmlGetDocEntity(doc, val);
1426 if ((ent != NULL) &&
1427 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
1428 if (last == NULL) {
1429 node = xmlNewDocText(doc, ent->content);
1430 last = ret = node;
Daniel Veillard6f42c132002-01-06 23:05:13 +00001431 } else if (last->type != XML_TEXT_NODE) {
1432 node = xmlNewDocText(doc, ent->content);
1433 last = xmlAddNextSibling(last, node);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001434 } else
1435 xmlNodeAddContent(last, ent->content);
1436
1437 } else {
1438 /*
1439 * Create a new REFERENCE_REF node
1440 */
1441 node = xmlNewReference(doc, val);
1442 if (node == NULL) {
1443 if (val != NULL) xmlFree(val);
1444 return(ret);
1445 }
Daniel Veillardbf8dae82002-04-18 16:39:10 +00001446 else if ((ent != NULL) && (ent->children == NULL)) {
1447 xmlNodePtr temp;
1448
1449 ent->children = xmlStringGetNodeList(doc,
1450 (const xmlChar*)node->content);
Daniel Veillard2d84a892002-12-30 00:01:08 +00001451 ent->owner = 1;
Daniel Veillardbf8dae82002-04-18 16:39:10 +00001452 temp = ent->children;
1453 while (temp) {
1454 temp->parent = (xmlNodePtr)ent;
1455 temp = temp->next;
1456 }
1457 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001458 if (last == NULL) {
1459 last = ret = node;
1460 } else {
1461 last = xmlAddNextSibling(last, node);
1462 }
1463 }
1464 xmlFree(val);
1465 }
1466 cur++;
1467 q = cur;
1468 }
1469 if (charval != 0) {
1470 xmlChar buf[10];
1471 int len;
1472
1473 len = xmlCopyCharMultiByte(buf, charval);
1474 buf[len] = 0;
1475 node = xmlNewDocText(doc, buf);
1476 if (node != NULL) {
1477 if (last == NULL) {
1478 last = ret = node;
1479 } else {
1480 last = xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001481 }
1482 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001483
1484 charval = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001485 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001486 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001487 cur++;
1488 }
Daniel Veillard75bea542001-05-11 17:41:21 +00001489 if ((cur != q) || (ret == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001490 /*
1491 * Handle the last piece of text.
1492 */
1493 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
1494 xmlNodeAddContentLen(last, q, cur - q);
1495 } else {
1496 node = xmlNewDocTextLen(doc, q, cur - q);
1497 if (node == NULL) return(ret);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001498 if (last == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001499 last = ret = node;
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001500 } else {
1501 last = xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001502 }
1503 }
1504 }
1505 return(ret);
1506}
1507
1508/**
1509 * xmlNodeListGetString:
1510 * @doc: the document
1511 * @list: a Node list
1512 * @inLine: should we replace entity contents or show their external form
1513 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001514 * Build the string equivalent to the text contained in the Node list
Owen Taylor3473f882001-02-23 17:55:21 +00001515 * made of TEXTs and ENTITY_REFs
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001516 *
Daniel Veillardbd9afb52002-09-25 22:25:35 +00001517 * Returns a pointer to the string copy, the caller must free it with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00001518 */
1519xmlChar *
Daniel Veillard7646b182002-04-20 06:41:40 +00001520xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine)
1521{
Owen Taylor3473f882001-02-23 17:55:21 +00001522 xmlNodePtr node = list;
1523 xmlChar *ret = NULL;
1524 xmlEntityPtr ent;
1525
Daniel Veillard7646b182002-04-20 06:41:40 +00001526 if (list == NULL)
1527 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001528
1529 while (node != NULL) {
1530 if ((node->type == XML_TEXT_NODE) ||
Daniel Veillard7646b182002-04-20 06:41:40 +00001531 (node->type == XML_CDATA_SECTION_NODE)) {
1532 if (inLine) {
1533 ret = xmlStrcat(ret, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001534 } else {
Daniel Veillard7646b182002-04-20 06:41:40 +00001535 xmlChar *buffer;
Owen Taylor3473f882001-02-23 17:55:21 +00001536
Daniel Veillard7646b182002-04-20 06:41:40 +00001537 buffer = xmlEncodeEntitiesReentrant(doc, node->content);
1538 if (buffer != NULL) {
1539 ret = xmlStrcat(ret, buffer);
1540 xmlFree(buffer);
1541 }
1542 }
1543 } else if (node->type == XML_ENTITY_REF_NODE) {
1544 if (inLine) {
1545 ent = xmlGetDocEntity(doc, node->name);
1546 if (ent != NULL) {
1547 xmlChar *buffer;
1548
1549 /* an entity content can be any "well balanced chunk",
1550 * i.e. the result of the content [43] production:
1551 * http://www.w3.org/TR/REC-xml#NT-content.
1552 * So it can contain text, CDATA section or nested
1553 * entity reference nodes (among others).
1554 * -> we recursive call xmlNodeListGetString()
1555 * which handles these types */
1556 buffer = xmlNodeListGetString(doc, ent->children, 1);
1557 if (buffer != NULL) {
1558 ret = xmlStrcat(ret, buffer);
1559 xmlFree(buffer);
1560 }
1561 } else {
1562 ret = xmlStrcat(ret, node->content);
1563 }
1564 } else {
1565 xmlChar buf[2];
1566
1567 buf[0] = '&';
1568 buf[1] = 0;
1569 ret = xmlStrncat(ret, buf, 1);
1570 ret = xmlStrcat(ret, node->name);
1571 buf[0] = ';';
1572 buf[1] = 0;
1573 ret = xmlStrncat(ret, buf, 1);
1574 }
1575 }
1576#if 0
1577 else {
1578 xmlGenericError(xmlGenericErrorContext,
1579 "xmlGetNodeListString : invalid node type %d\n",
1580 node->type);
1581 }
1582#endif
1583 node = node->next;
1584 }
1585 return (ret);
1586}
Daniel Veillard652327a2003-09-29 18:02:38 +00001587
1588#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001589/**
1590 * xmlNodeListGetRawString:
1591 * @doc: the document
1592 * @list: a Node list
1593 * @inLine: should we replace entity contents or show their external form
1594 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001595 * Builds the string equivalent to the text contained in the Node list
Owen Taylor3473f882001-02-23 17:55:21 +00001596 * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString()
1597 * this function doesn't do any character encoding handling.
1598 *
Daniel Veillardbd9afb52002-09-25 22:25:35 +00001599 * Returns a pointer to the string copy, the caller must free it with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00001600 */
1601xmlChar *
Daniel Veillard7646b182002-04-20 06:41:40 +00001602xmlNodeListGetRawString(xmlDocPtr doc, xmlNodePtr list, int inLine)
1603{
Owen Taylor3473f882001-02-23 17:55:21 +00001604 xmlNodePtr node = list;
1605 xmlChar *ret = NULL;
1606 xmlEntityPtr ent;
1607
Daniel Veillard7646b182002-04-20 06:41:40 +00001608 if (list == NULL)
1609 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001610
1611 while (node != NULL) {
Daniel Veillard7db37732001-07-12 01:20:08 +00001612 if ((node->type == XML_TEXT_NODE) ||
Daniel Veillard7646b182002-04-20 06:41:40 +00001613 (node->type == XML_CDATA_SECTION_NODE)) {
1614 if (inLine) {
1615 ret = xmlStrcat(ret, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001616 } else {
Daniel Veillard7646b182002-04-20 06:41:40 +00001617 xmlChar *buffer;
1618
1619 buffer = xmlEncodeSpecialChars(doc, node->content);
1620 if (buffer != NULL) {
1621 ret = xmlStrcat(ret, buffer);
1622 xmlFree(buffer);
1623 }
1624 }
1625 } else if (node->type == XML_ENTITY_REF_NODE) {
1626 if (inLine) {
1627 ent = xmlGetDocEntity(doc, node->name);
1628 if (ent != NULL) {
1629 xmlChar *buffer;
1630
1631 /* an entity content can be any "well balanced chunk",
1632 * i.e. the result of the content [43] production:
1633 * http://www.w3.org/TR/REC-xml#NT-content.
1634 * So it can contain text, CDATA section or nested
1635 * entity reference nodes (among others).
1636 * -> we recursive call xmlNodeListGetRawString()
1637 * which handles these types */
1638 buffer =
1639 xmlNodeListGetRawString(doc, ent->children, 1);
1640 if (buffer != NULL) {
1641 ret = xmlStrcat(ret, buffer);
1642 xmlFree(buffer);
1643 }
1644 } else {
1645 ret = xmlStrcat(ret, node->content);
1646 }
1647 } else {
1648 xmlChar buf[2];
1649
1650 buf[0] = '&';
1651 buf[1] = 0;
1652 ret = xmlStrncat(ret, buf, 1);
1653 ret = xmlStrcat(ret, node->name);
1654 buf[0] = ';';
1655 buf[1] = 0;
1656 ret = xmlStrncat(ret, buf, 1);
1657 }
1658 }
Owen Taylor3473f882001-02-23 17:55:21 +00001659#if 0
Daniel Veillard7646b182002-04-20 06:41:40 +00001660 else {
1661 xmlGenericError(xmlGenericErrorContext,
1662 "xmlGetNodeListString : invalid node type %d\n",
1663 node->type);
1664 }
Owen Taylor3473f882001-02-23 17:55:21 +00001665#endif
Daniel Veillard7646b182002-04-20 06:41:40 +00001666 node = node->next;
Owen Taylor3473f882001-02-23 17:55:21 +00001667 }
Daniel Veillard7646b182002-04-20 06:41:40 +00001668 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00001669}
Daniel Veillard652327a2003-09-29 18:02:38 +00001670#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001671
Daniel Veillard652327a2003-09-29 18:02:38 +00001672#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001673/**
1674 * xmlNewProp:
1675 * @node: the holding node
1676 * @name: the name of the attribute
1677 * @value: the value of the attribute
1678 *
1679 * Create a new property carried by a node.
1680 * Returns a pointer to the attribute
1681 */
1682xmlAttrPtr
1683xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
1684 xmlAttrPtr cur;
1685 xmlDocPtr doc = NULL;
1686
1687 if (name == NULL) {
1688#ifdef DEBUG_TREE
1689 xmlGenericError(xmlGenericErrorContext,
1690 "xmlNewProp : name == NULL\n");
1691#endif
1692 return(NULL);
1693 }
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00001694 if ((node != NULL) && (node->type != XML_ELEMENT_NODE))
1695 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001696
1697 /*
1698 * Allocate a new property and fill the fields.
1699 */
1700 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1701 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001702 xmlTreeErrMemory("building attribute");
Owen Taylor3473f882001-02-23 17:55:21 +00001703 return(NULL);
1704 }
1705 memset(cur, 0, sizeof(xmlAttr));
1706 cur->type = XML_ATTRIBUTE_NODE;
1707
1708 cur->parent = node;
1709 if (node != NULL) {
1710 doc = node->doc;
1711 cur->doc = doc;
1712 }
1713 cur->name = xmlStrdup(name);
1714 if (value != NULL) {
1715 xmlChar *buffer;
1716 xmlNodePtr tmp;
1717
1718 buffer = xmlEncodeEntitiesReentrant(doc, value);
1719 cur->children = xmlStringGetNodeList(doc, buffer);
1720 cur->last = NULL;
1721 tmp = cur->children;
1722 while (tmp != NULL) {
1723 tmp->parent = (xmlNodePtr) cur;
1724 tmp->doc = doc;
1725 if (tmp->next == NULL)
1726 cur->last = tmp;
1727 tmp = tmp->next;
1728 }
1729 xmlFree(buffer);
1730 }
1731
1732 /*
1733 * Add it at the end to preserve parsing order ...
1734 */
1735 if (node != NULL) {
1736 if (node->properties == NULL) {
1737 node->properties = cur;
1738 } else {
1739 xmlAttrPtr prev = node->properties;
1740
1741 while (prev->next != NULL) prev = prev->next;
1742 prev->next = cur;
1743 cur->prev = prev;
1744 }
1745 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00001746
Daniel Veillarda880b122003-04-21 21:36:41 +00001747 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001748 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001749 return(cur);
1750}
Daniel Veillard652327a2003-09-29 18:02:38 +00001751#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001752
1753/**
1754 * xmlNewNsProp:
1755 * @node: the holding node
1756 * @ns: the namespace
1757 * @name: the name of the attribute
1758 * @value: the value of the attribute
1759 *
1760 * Create a new property tagged with a namespace and carried by a node.
1761 * Returns a pointer to the attribute
1762 */
1763xmlAttrPtr
1764xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
1765 const xmlChar *value) {
1766 xmlAttrPtr cur;
Daniel Veillarda682b212001-06-07 19:59:42 +00001767 xmlDocPtr doc = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001768
1769 if (name == NULL) {
1770#ifdef DEBUG_TREE
1771 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00001772 "xmlNewNsProp : name == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001773#endif
1774 return(NULL);
1775 }
1776
1777 /*
1778 * Allocate a new property and fill the fields.
1779 */
1780 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1781 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001782 xmlTreeErrMemory("building attribute");
Owen Taylor3473f882001-02-23 17:55:21 +00001783 return(NULL);
1784 }
1785 memset(cur, 0, sizeof(xmlAttr));
1786 cur->type = XML_ATTRIBUTE_NODE;
1787
1788 cur->parent = node;
Daniel Veillarda682b212001-06-07 19:59:42 +00001789 if (node != NULL) {
1790 doc = node->doc;
1791 cur->doc = doc;
1792 }
Owen Taylor3473f882001-02-23 17:55:21 +00001793 cur->ns = ns;
1794 cur->name = xmlStrdup(name);
1795 if (value != NULL) {
1796 xmlChar *buffer;
1797 xmlNodePtr tmp;
1798
Daniel Veillarda682b212001-06-07 19:59:42 +00001799 buffer = xmlEncodeEntitiesReentrant(doc, value);
1800 cur->children = xmlStringGetNodeList(doc, buffer);
Owen Taylor3473f882001-02-23 17:55:21 +00001801 cur->last = NULL;
1802 tmp = cur->children;
1803 while (tmp != NULL) {
1804 tmp->parent = (xmlNodePtr) cur;
1805 if (tmp->next == NULL)
1806 cur->last = tmp;
1807 tmp = tmp->next;
1808 }
1809 xmlFree(buffer);
1810 }
1811
1812 /*
1813 * Add it at the end to preserve parsing order ...
1814 */
1815 if (node != NULL) {
1816 if (node->properties == NULL) {
1817 node->properties = cur;
1818 } else {
1819 xmlAttrPtr prev = node->properties;
1820
1821 while (prev->next != NULL) prev = prev->next;
1822 prev->next = cur;
1823 cur->prev = prev;
1824 }
1825 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00001826
Daniel Veillarda880b122003-04-21 21:36:41 +00001827 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001828 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001829 return(cur);
1830}
1831
1832/**
Daniel Veillard46de64e2002-05-29 08:21:33 +00001833 * xmlNewNsPropEatName:
1834 * @node: the holding node
1835 * @ns: the namespace
1836 * @name: the name of the attribute
1837 * @value: the value of the attribute
1838 *
1839 * Create a new property tagged with a namespace and carried by a node.
1840 * Returns a pointer to the attribute
1841 */
1842xmlAttrPtr
1843xmlNewNsPropEatName(xmlNodePtr node, xmlNsPtr ns, xmlChar *name,
1844 const xmlChar *value) {
1845 xmlAttrPtr cur;
1846 xmlDocPtr doc = NULL;
1847
1848 if (name == NULL) {
1849#ifdef DEBUG_TREE
1850 xmlGenericError(xmlGenericErrorContext,
1851 "xmlNewNsPropEatName : name == NULL\n");
1852#endif
1853 return(NULL);
1854 }
1855
1856 /*
1857 * Allocate a new property and fill the fields.
1858 */
1859 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1860 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001861 xmlTreeErrMemory("building attribute");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001862 xmlFree(name);
Daniel Veillard46de64e2002-05-29 08:21:33 +00001863 return(NULL);
1864 }
1865 memset(cur, 0, sizeof(xmlAttr));
1866 cur->type = XML_ATTRIBUTE_NODE;
1867
1868 cur->parent = node;
1869 if (node != NULL) {
1870 doc = node->doc;
1871 cur->doc = doc;
1872 }
1873 cur->ns = ns;
1874 cur->name = name;
1875 if (value != NULL) {
1876 xmlChar *buffer;
1877 xmlNodePtr tmp;
1878
1879 buffer = xmlEncodeEntitiesReentrant(doc, value);
1880 cur->children = xmlStringGetNodeList(doc, buffer);
1881 cur->last = NULL;
1882 tmp = cur->children;
1883 while (tmp != NULL) {
1884 tmp->parent = (xmlNodePtr) cur;
1885 if (tmp->next == NULL)
1886 cur->last = tmp;
1887 tmp = tmp->next;
1888 }
1889 xmlFree(buffer);
1890 }
1891
1892 /*
1893 * Add it at the end to preserve parsing order ...
1894 */
1895 if (node != NULL) {
1896 if (node->properties == NULL) {
1897 node->properties = cur;
1898 } else {
1899 xmlAttrPtr prev = node->properties;
1900
1901 while (prev->next != NULL) prev = prev->next;
1902 prev->next = cur;
1903 cur->prev = prev;
1904 }
1905 }
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001906
Daniel Veillarda880b122003-04-21 21:36:41 +00001907 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001908 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Daniel Veillard46de64e2002-05-29 08:21:33 +00001909 return(cur);
1910}
1911
1912/**
Owen Taylor3473f882001-02-23 17:55:21 +00001913 * xmlNewDocProp:
1914 * @doc: the document
1915 * @name: the name of the attribute
1916 * @value: the value of the attribute
1917 *
1918 * Create a new property carried by a document.
1919 * Returns a pointer to the attribute
1920 */
1921xmlAttrPtr
1922xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) {
1923 xmlAttrPtr cur;
1924
1925 if (name == NULL) {
1926#ifdef DEBUG_TREE
1927 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00001928 "xmlNewDocProp : name == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001929#endif
1930 return(NULL);
1931 }
1932
1933 /*
1934 * Allocate a new property and fill the fields.
1935 */
1936 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1937 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001938 xmlTreeErrMemory("building attribute");
Owen Taylor3473f882001-02-23 17:55:21 +00001939 return(NULL);
1940 }
1941 memset(cur, 0, sizeof(xmlAttr));
1942 cur->type = XML_ATTRIBUTE_NODE;
1943
1944 cur->name = xmlStrdup(name);
1945 cur->doc = doc;
1946 if (value != NULL) {
1947 xmlNodePtr tmp;
1948
1949 cur->children = xmlStringGetNodeList(doc, value);
1950 cur->last = NULL;
1951
1952 tmp = cur->children;
1953 while (tmp != NULL) {
1954 tmp->parent = (xmlNodePtr) cur;
1955 if (tmp->next == NULL)
1956 cur->last = tmp;
1957 tmp = tmp->next;
1958 }
1959 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00001960
Daniel Veillarda880b122003-04-21 21:36:41 +00001961 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001962 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001963 return(cur);
1964}
1965
1966/**
1967 * xmlFreePropList:
1968 * @cur: the first property in the list
1969 *
1970 * Free a property and all its siblings, all the children are freed too.
1971 */
1972void
1973xmlFreePropList(xmlAttrPtr cur) {
1974 xmlAttrPtr next;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001975 if (cur == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +00001976 while (cur != NULL) {
1977 next = cur->next;
1978 xmlFreeProp(cur);
1979 cur = next;
1980 }
1981}
1982
1983/**
1984 * xmlFreeProp:
1985 * @cur: an attribute
1986 *
1987 * Free one attribute, all the content is freed too
1988 */
1989void
1990xmlFreeProp(xmlAttrPtr cur) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001991 xmlDictPtr dict = NULL;
1992 if (cur == NULL) return;
1993
1994 if (cur->doc != NULL) dict = cur->doc->dict;
Daniel Veillard5335dc52003-01-01 20:59:38 +00001995
Daniel Veillarda880b122003-04-21 21:36:41 +00001996 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001997 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
1998
Owen Taylor3473f882001-02-23 17:55:21 +00001999 /* Check for ID removal -> leading to invalid references ! */
Daniel Veillard76d66f42001-05-16 21:05:17 +00002000 if ((cur->parent != NULL) && (cur->parent->doc != NULL) &&
2001 ((cur->parent->doc->intSubset != NULL) ||
2002 (cur->parent->doc->extSubset != NULL))) {
2003 if (xmlIsID(cur->parent->doc, cur->parent, cur))
2004 xmlRemoveID(cur->parent->doc, cur);
2005 }
Owen Taylor3473f882001-02-23 17:55:21 +00002006 if (cur->children != NULL) xmlFreeNodeList(cur->children);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002007 DICT_FREE(cur->name)
Owen Taylor3473f882001-02-23 17:55:21 +00002008 xmlFree(cur);
2009}
2010
Daniel Veillard652327a2003-09-29 18:02:38 +00002011#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002012/**
2013 * xmlRemoveProp:
2014 * @cur: an attribute
2015 *
2016 * Unlink and free one attribute, all the content is freed too
2017 * Note this doesn't work for namespace definition attributes
2018 *
2019 * Returns 0 if success and -1 in case of error.
2020 */
2021int
2022xmlRemoveProp(xmlAttrPtr cur) {
2023 xmlAttrPtr tmp;
2024 if (cur == NULL) {
2025#ifdef DEBUG_TREE
2026 xmlGenericError(xmlGenericErrorContext,
2027 "xmlRemoveProp : cur == NULL\n");
2028#endif
2029 return(-1);
2030 }
2031 if (cur->parent == NULL) {
2032#ifdef DEBUG_TREE
2033 xmlGenericError(xmlGenericErrorContext,
2034 "xmlRemoveProp : cur->parent == NULL\n");
2035#endif
2036 return(-1);
2037 }
2038 tmp = cur->parent->properties;
2039 if (tmp == cur) {
2040 cur->parent->properties = cur->next;
2041 xmlFreeProp(cur);
2042 return(0);
2043 }
2044 while (tmp != NULL) {
2045 if (tmp->next == cur) {
2046 tmp->next = cur->next;
2047 if (tmp->next != NULL)
2048 tmp->next->prev = tmp;
2049 xmlFreeProp(cur);
2050 return(0);
2051 }
2052 tmp = tmp->next;
2053 }
2054#ifdef DEBUG_TREE
2055 xmlGenericError(xmlGenericErrorContext,
2056 "xmlRemoveProp : attribute not owned by its node\n");
2057#endif
2058 return(-1);
2059}
Daniel Veillard652327a2003-09-29 18:02:38 +00002060#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002061
2062/**
2063 * xmlNewPI:
2064 * @name: the processing instruction name
2065 * @content: the PI content
2066 *
2067 * Creation of a processing instruction element.
2068 * Returns a pointer to the new node object.
2069 */
2070xmlNodePtr
2071xmlNewPI(const xmlChar *name, const xmlChar *content) {
2072 xmlNodePtr cur;
2073
2074 if (name == NULL) {
2075#ifdef DEBUG_TREE
2076 xmlGenericError(xmlGenericErrorContext,
2077 "xmlNewPI : name == NULL\n");
2078#endif
2079 return(NULL);
2080 }
2081
2082 /*
2083 * Allocate a new node and fill the fields.
2084 */
2085 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2086 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002087 xmlTreeErrMemory("building PI");
Owen Taylor3473f882001-02-23 17:55:21 +00002088 return(NULL);
2089 }
2090 memset(cur, 0, sizeof(xmlNode));
2091 cur->type = XML_PI_NODE;
2092
2093 cur->name = xmlStrdup(name);
2094 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002095 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00002096 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002097
Daniel Veillarda880b122003-04-21 21:36:41 +00002098 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002099 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002100 return(cur);
2101}
2102
2103/**
2104 * xmlNewNode:
2105 * @ns: namespace if any
2106 * @name: the node name
2107 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002108 * Creation of a new node element. @ns is optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00002109 *
2110 * Returns a pointer to the new node object.
2111 */
2112xmlNodePtr
2113xmlNewNode(xmlNsPtr ns, const xmlChar *name) {
2114 xmlNodePtr cur;
2115
2116 if (name == NULL) {
2117#ifdef DEBUG_TREE
2118 xmlGenericError(xmlGenericErrorContext,
2119 "xmlNewNode : name == NULL\n");
2120#endif
2121 return(NULL);
2122 }
2123
2124 /*
2125 * Allocate a new node and fill the fields.
2126 */
2127 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2128 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002129 xmlTreeErrMemory("building node");
Owen Taylor3473f882001-02-23 17:55:21 +00002130 return(NULL);
2131 }
2132 memset(cur, 0, sizeof(xmlNode));
2133 cur->type = XML_ELEMENT_NODE;
2134
2135 cur->name = xmlStrdup(name);
2136 cur->ns = ns;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002137
Daniel Veillarda880b122003-04-21 21:36:41 +00002138 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002139 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002140 return(cur);
2141}
2142
2143/**
Daniel Veillard46de64e2002-05-29 08:21:33 +00002144 * xmlNewNodeEatName:
2145 * @ns: namespace if any
2146 * @name: the node name
2147 *
2148 * Creation of a new node element. @ns is optional (NULL).
2149 *
2150 * Returns a pointer to the new node object.
2151 */
2152xmlNodePtr
2153xmlNewNodeEatName(xmlNsPtr ns, xmlChar *name) {
2154 xmlNodePtr cur;
2155
2156 if (name == NULL) {
2157#ifdef DEBUG_TREE
2158 xmlGenericError(xmlGenericErrorContext,
2159 "xmlNewNode : name == NULL\n");
2160#endif
2161 return(NULL);
2162 }
2163
2164 /*
2165 * Allocate a new node and fill the fields.
2166 */
2167 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2168 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002169 xmlTreeErrMemory("building node");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002170 xmlFree(name);
Daniel Veillard46de64e2002-05-29 08:21:33 +00002171 return(NULL);
2172 }
2173 memset(cur, 0, sizeof(xmlNode));
2174 cur->type = XML_ELEMENT_NODE;
2175
2176 cur->name = name;
2177 cur->ns = ns;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002178
Daniel Veillarda880b122003-04-21 21:36:41 +00002179 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002180 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Daniel Veillard46de64e2002-05-29 08:21:33 +00002181 return(cur);
2182}
2183
2184/**
Owen Taylor3473f882001-02-23 17:55:21 +00002185 * xmlNewDocNode:
2186 * @doc: the document
2187 * @ns: namespace if any
2188 * @name: the node name
2189 * @content: the XML text content if any
2190 *
2191 * Creation of a new node element within a document. @ns and @content
Daniel Veillardd1640922001-12-17 15:30:10 +00002192 * are optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00002193 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2194 * references, but XML special chars need to be escaped first by using
2195 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2196 * need entities support.
2197 *
2198 * Returns a pointer to the new node object.
2199 */
2200xmlNodePtr
2201xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns,
2202 const xmlChar *name, const xmlChar *content) {
2203 xmlNodePtr cur;
2204
2205 cur = xmlNewNode(ns, name);
2206 if (cur != NULL) {
2207 cur->doc = doc;
2208 if (content != NULL) {
2209 cur->children = xmlStringGetNodeList(doc, content);
2210 UPDATE_LAST_CHILD_AND_PARENT(cur)
2211 }
2212 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002213
Owen Taylor3473f882001-02-23 17:55:21 +00002214 return(cur);
2215}
2216
Daniel Veillard46de64e2002-05-29 08:21:33 +00002217/**
2218 * xmlNewDocNodeEatName:
2219 * @doc: the document
2220 * @ns: namespace if any
2221 * @name: the node name
2222 * @content: the XML text content if any
2223 *
2224 * Creation of a new node element within a document. @ns and @content
2225 * are optional (NULL).
2226 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2227 * references, but XML special chars need to be escaped first by using
2228 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2229 * need entities support.
2230 *
2231 * Returns a pointer to the new node object.
2232 */
2233xmlNodePtr
2234xmlNewDocNodeEatName(xmlDocPtr doc, xmlNsPtr ns,
2235 xmlChar *name, const xmlChar *content) {
2236 xmlNodePtr cur;
2237
2238 cur = xmlNewNodeEatName(ns, name);
2239 if (cur != NULL) {
2240 cur->doc = doc;
2241 if (content != NULL) {
2242 cur->children = xmlStringGetNodeList(doc, content);
2243 UPDATE_LAST_CHILD_AND_PARENT(cur)
2244 }
2245 }
2246 return(cur);
2247}
2248
Daniel Veillard652327a2003-09-29 18:02:38 +00002249#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002250/**
2251 * xmlNewDocRawNode:
2252 * @doc: the document
2253 * @ns: namespace if any
2254 * @name: the node name
2255 * @content: the text content if any
2256 *
2257 * Creation of a new node element within a document. @ns and @content
Daniel Veillardd1640922001-12-17 15:30:10 +00002258 * are optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00002259 *
2260 * Returns a pointer to the new node object.
2261 */
2262xmlNodePtr
2263xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns,
2264 const xmlChar *name, const xmlChar *content) {
2265 xmlNodePtr cur;
2266
2267 cur = xmlNewNode(ns, name);
2268 if (cur != NULL) {
2269 cur->doc = doc;
2270 if (content != NULL) {
2271 cur->children = xmlNewDocText(doc, content);
2272 UPDATE_LAST_CHILD_AND_PARENT(cur)
2273 }
2274 }
2275 return(cur);
2276}
2277
2278/**
2279 * xmlNewDocFragment:
2280 * @doc: the document owning the fragment
2281 *
2282 * Creation of a new Fragment node.
2283 * Returns a pointer to the new node object.
2284 */
2285xmlNodePtr
2286xmlNewDocFragment(xmlDocPtr doc) {
2287 xmlNodePtr cur;
2288
2289 /*
2290 * Allocate a new DocumentFragment node and fill the fields.
2291 */
2292 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2293 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002294 xmlTreeErrMemory("building fragment");
Owen Taylor3473f882001-02-23 17:55:21 +00002295 return(NULL);
2296 }
2297 memset(cur, 0, sizeof(xmlNode));
2298 cur->type = XML_DOCUMENT_FRAG_NODE;
2299
2300 cur->doc = doc;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002301
Daniel Veillarda880b122003-04-21 21:36:41 +00002302 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002303 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002304 return(cur);
2305}
Daniel Veillard652327a2003-09-29 18:02:38 +00002306#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002307
2308/**
2309 * xmlNewText:
2310 * @content: the text content
2311 *
2312 * Creation of a new text node.
2313 * Returns a pointer to the new node object.
2314 */
2315xmlNodePtr
2316xmlNewText(const xmlChar *content) {
2317 xmlNodePtr cur;
2318
2319 /*
2320 * Allocate a new node and fill the fields.
2321 */
2322 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2323 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002324 xmlTreeErrMemory("building text");
Owen Taylor3473f882001-02-23 17:55:21 +00002325 return(NULL);
2326 }
2327 memset(cur, 0, sizeof(xmlNode));
2328 cur->type = XML_TEXT_NODE;
2329
2330 cur->name = xmlStringText;
2331 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002332 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00002333 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002334
Daniel Veillarda880b122003-04-21 21:36:41 +00002335 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002336 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002337 return(cur);
2338}
2339
Daniel Veillard652327a2003-09-29 18:02:38 +00002340#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002341/**
2342 * xmlNewTextChild:
2343 * @parent: the parent node
2344 * @ns: a namespace if any
2345 * @name: the name of the child
2346 * @content: the text content of the child if any.
2347 *
2348 * Creation of a new child element, added at the end of @parent children list.
Daniel Veillardd1640922001-12-17 15:30:10 +00002349 * @ns and @content parameters are optional (NULL). If content is non NULL,
Owen Taylor3473f882001-02-23 17:55:21 +00002350 * a child TEXT node will be created containing the string content.
2351 *
2352 * Returns a pointer to the new node object.
2353 */
2354xmlNodePtr
2355xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns,
2356 const xmlChar *name, const xmlChar *content) {
2357 xmlNodePtr cur, prev;
2358
2359 if (parent == NULL) {
2360#ifdef DEBUG_TREE
2361 xmlGenericError(xmlGenericErrorContext,
2362 "xmlNewTextChild : parent == NULL\n");
2363#endif
2364 return(NULL);
2365 }
2366
2367 if (name == NULL) {
2368#ifdef DEBUG_TREE
2369 xmlGenericError(xmlGenericErrorContext,
2370 "xmlNewTextChild : name == NULL\n");
2371#endif
2372 return(NULL);
2373 }
2374
2375 /*
2376 * Allocate a new node
2377 */
2378 if (ns == NULL)
2379 cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content);
2380 else
2381 cur = xmlNewDocRawNode(parent->doc, ns, name, content);
2382 if (cur == NULL) return(NULL);
2383
2384 /*
2385 * add the new element at the end of the children list.
2386 */
2387 cur->type = XML_ELEMENT_NODE;
2388 cur->parent = parent;
2389 cur->doc = parent->doc;
2390 if (parent->children == NULL) {
2391 parent->children = cur;
2392 parent->last = cur;
2393 } else {
2394 prev = parent->last;
2395 prev->next = cur;
2396 cur->prev = prev;
2397 parent->last = cur;
2398 }
2399
2400 return(cur);
2401}
Daniel Veillard652327a2003-09-29 18:02:38 +00002402#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002403
2404/**
2405 * xmlNewCharRef:
2406 * @doc: the document
2407 * @name: the char ref string, starting with # or "&# ... ;"
2408 *
2409 * Creation of a new character reference node.
2410 * Returns a pointer to the new node object.
2411 */
2412xmlNodePtr
2413xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) {
2414 xmlNodePtr cur;
2415
2416 /*
2417 * Allocate a new node and fill the fields.
2418 */
2419 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2420 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002421 xmlTreeErrMemory("building character reference");
Owen Taylor3473f882001-02-23 17:55:21 +00002422 return(NULL);
2423 }
2424 memset(cur, 0, sizeof(xmlNode));
2425 cur->type = XML_ENTITY_REF_NODE;
2426
2427 cur->doc = doc;
2428 if (name[0] == '&') {
2429 int len;
2430 name++;
2431 len = xmlStrlen(name);
2432 if (name[len - 1] == ';')
2433 cur->name = xmlStrndup(name, len - 1);
2434 else
2435 cur->name = xmlStrndup(name, len);
2436 } else
2437 cur->name = xmlStrdup(name);
Daniel Veillard5335dc52003-01-01 20:59:38 +00002438
Daniel Veillarda880b122003-04-21 21:36:41 +00002439 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002440 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002441 return(cur);
2442}
2443
2444/**
2445 * xmlNewReference:
2446 * @doc: the document
2447 * @name: the reference name, or the reference string with & and ;
2448 *
2449 * Creation of a new reference node.
2450 * Returns a pointer to the new node object.
2451 */
2452xmlNodePtr
2453xmlNewReference(xmlDocPtr doc, const xmlChar *name) {
2454 xmlNodePtr cur;
2455 xmlEntityPtr ent;
2456
2457 /*
2458 * Allocate a new node and fill the fields.
2459 */
2460 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2461 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002462 xmlTreeErrMemory("building reference");
Owen Taylor3473f882001-02-23 17:55:21 +00002463 return(NULL);
2464 }
2465 memset(cur, 0, sizeof(xmlNode));
2466 cur->type = XML_ENTITY_REF_NODE;
2467
2468 cur->doc = doc;
2469 if (name[0] == '&') {
2470 int len;
2471 name++;
2472 len = xmlStrlen(name);
2473 if (name[len - 1] == ';')
2474 cur->name = xmlStrndup(name, len - 1);
2475 else
2476 cur->name = xmlStrndup(name, len);
2477 } else
2478 cur->name = xmlStrdup(name);
2479
2480 ent = xmlGetDocEntity(doc, cur->name);
2481 if (ent != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002482 cur->content = ent->content;
Owen Taylor3473f882001-02-23 17:55:21 +00002483 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002484 * The parent pointer in entity is a DTD pointer and thus is NOT
Owen Taylor3473f882001-02-23 17:55:21 +00002485 * updated. Not sure if this is 100% correct.
2486 * -George
2487 */
2488 cur->children = (xmlNodePtr) ent;
2489 cur->last = (xmlNodePtr) ent;
2490 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002491
Daniel Veillarda880b122003-04-21 21:36:41 +00002492 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002493 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002494 return(cur);
2495}
2496
2497/**
2498 * xmlNewDocText:
2499 * @doc: the document
2500 * @content: the text content
2501 *
2502 * Creation of a new text node within a document.
2503 * Returns a pointer to the new node object.
2504 */
2505xmlNodePtr
2506xmlNewDocText(xmlDocPtr doc, const xmlChar *content) {
2507 xmlNodePtr cur;
2508
2509 cur = xmlNewText(content);
2510 if (cur != NULL) cur->doc = doc;
2511 return(cur);
2512}
2513
2514/**
2515 * xmlNewTextLen:
2516 * @content: the text content
2517 * @len: the text len.
2518 *
Daniel Veillard60087f32001-10-10 09:45:09 +00002519 * Creation of a new text node with an extra parameter for the content's length
Owen Taylor3473f882001-02-23 17:55:21 +00002520 * Returns a pointer to the new node object.
2521 */
2522xmlNodePtr
2523xmlNewTextLen(const xmlChar *content, int len) {
2524 xmlNodePtr cur;
2525
2526 /*
2527 * Allocate a new node and fill the fields.
2528 */
2529 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2530 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002531 xmlTreeErrMemory("building text");
Owen Taylor3473f882001-02-23 17:55:21 +00002532 return(NULL);
2533 }
2534 memset(cur, 0, sizeof(xmlNode));
2535 cur->type = XML_TEXT_NODE;
2536
2537 cur->name = xmlStringText;
2538 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002539 cur->content = xmlStrndup(content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00002540 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002541
Daniel Veillarda880b122003-04-21 21:36:41 +00002542 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002543 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002544 return(cur);
2545}
2546
2547/**
2548 * xmlNewDocTextLen:
2549 * @doc: the document
2550 * @content: the text content
2551 * @len: the text len.
2552 *
Daniel Veillard60087f32001-10-10 09:45:09 +00002553 * Creation of a new text node with an extra content length parameter. The
Owen Taylor3473f882001-02-23 17:55:21 +00002554 * text node pertain to a given document.
2555 * Returns a pointer to the new node object.
2556 */
2557xmlNodePtr
2558xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) {
2559 xmlNodePtr cur;
2560
2561 cur = xmlNewTextLen(content, len);
2562 if (cur != NULL) cur->doc = doc;
2563 return(cur);
2564}
2565
2566/**
2567 * xmlNewComment:
2568 * @content: the comment content
2569 *
2570 * Creation of a new node containing a comment.
2571 * Returns a pointer to the new node object.
2572 */
2573xmlNodePtr
2574xmlNewComment(const xmlChar *content) {
2575 xmlNodePtr cur;
2576
2577 /*
2578 * Allocate a new node and fill the fields.
2579 */
2580 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2581 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002582 xmlTreeErrMemory("building comment");
Owen Taylor3473f882001-02-23 17:55:21 +00002583 return(NULL);
2584 }
2585 memset(cur, 0, sizeof(xmlNode));
2586 cur->type = XML_COMMENT_NODE;
2587
2588 cur->name = xmlStringComment;
2589 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002590 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00002591 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002592
Daniel Veillarda880b122003-04-21 21:36:41 +00002593 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002594 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002595 return(cur);
2596}
2597
2598/**
2599 * xmlNewCDataBlock:
2600 * @doc: the document
Daniel Veillardd1640922001-12-17 15:30:10 +00002601 * @content: the CDATA block content content
Owen Taylor3473f882001-02-23 17:55:21 +00002602 * @len: the length of the block
2603 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002604 * Creation of a new node containing a CDATA block.
Owen Taylor3473f882001-02-23 17:55:21 +00002605 * Returns a pointer to the new node object.
2606 */
2607xmlNodePtr
2608xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) {
2609 xmlNodePtr cur;
2610
2611 /*
2612 * Allocate a new node and fill the fields.
2613 */
2614 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2615 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002616 xmlTreeErrMemory("building CDATA");
Owen Taylor3473f882001-02-23 17:55:21 +00002617 return(NULL);
2618 }
2619 memset(cur, 0, sizeof(xmlNode));
2620 cur->type = XML_CDATA_SECTION_NODE;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002621 cur->doc = doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002622
2623 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002624 cur->content = xmlStrndup(content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00002625 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002626
Daniel Veillarda880b122003-04-21 21:36:41 +00002627 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002628 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002629 return(cur);
2630}
2631
2632/**
2633 * xmlNewDocComment:
2634 * @doc: the document
2635 * @content: the comment content
2636 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002637 * Creation of a new node containing a comment within a document.
Owen Taylor3473f882001-02-23 17:55:21 +00002638 * Returns a pointer to the new node object.
2639 */
2640xmlNodePtr
2641xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) {
2642 xmlNodePtr cur;
2643
2644 cur = xmlNewComment(content);
2645 if (cur != NULL) cur->doc = doc;
2646 return(cur);
2647}
2648
2649/**
2650 * xmlSetTreeDoc:
2651 * @tree: the top element
2652 * @doc: the document
2653 *
2654 * update all nodes under the tree to point to the right document
2655 */
2656void
2657xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) {
Daniel Veillard19e96c32001-07-09 10:32:59 +00002658 xmlAttrPtr prop;
2659
Owen Taylor3473f882001-02-23 17:55:21 +00002660 if (tree == NULL)
2661 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002662 if (tree->doc != doc) {
Daniel Veillard36065812002-01-24 15:02:46 +00002663 if(tree->type == XML_ELEMENT_NODE) {
2664 prop = tree->properties;
2665 while (prop != NULL) {
2666 prop->doc = doc;
2667 xmlSetListDoc(prop->children, doc);
2668 prop = prop->next;
2669 }
Daniel Veillard19e96c32001-07-09 10:32:59 +00002670 }
Owen Taylor3473f882001-02-23 17:55:21 +00002671 if (tree->children != NULL)
2672 xmlSetListDoc(tree->children, doc);
2673 tree->doc = doc;
2674 }
2675}
2676
2677/**
2678 * xmlSetListDoc:
Daniel Veillardd1640922001-12-17 15:30:10 +00002679 * @list: the first element
Owen Taylor3473f882001-02-23 17:55:21 +00002680 * @doc: the document
2681 *
2682 * update all nodes in the list to point to the right document
2683 */
2684void
2685xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) {
2686 xmlNodePtr cur;
2687
2688 if (list == NULL)
2689 return;
2690 cur = list;
2691 while (cur != NULL) {
2692 if (cur->doc != doc)
2693 xmlSetTreeDoc(cur, doc);
2694 cur = cur->next;
2695 }
2696}
2697
Daniel Veillard652327a2003-09-29 18:02:38 +00002698#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002699/**
2700 * xmlNewChild:
2701 * @parent: the parent node
2702 * @ns: a namespace if any
2703 * @name: the name of the child
2704 * @content: the XML content of the child if any.
2705 *
2706 * Creation of a new child element, added at the end of @parent children list.
Daniel Veillardd1640922001-12-17 15:30:10 +00002707 * @ns and @content parameters are optional (NULL). If content is non NULL,
Owen Taylor3473f882001-02-23 17:55:21 +00002708 * a child list containing the TEXTs and ENTITY_REFs node will be created.
2709 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2710 * references, but XML special chars need to be escaped first by using
2711 * xmlEncodeEntitiesReentrant(). Use xmlNewTextChild() if entities
2712 * support is not needed.
2713 *
2714 * Returns a pointer to the new node object.
2715 */
2716xmlNodePtr
2717xmlNewChild(xmlNodePtr parent, xmlNsPtr ns,
2718 const xmlChar *name, const xmlChar *content) {
2719 xmlNodePtr cur, prev;
2720
2721 if (parent == NULL) {
2722#ifdef DEBUG_TREE
2723 xmlGenericError(xmlGenericErrorContext,
2724 "xmlNewChild : parent == NULL\n");
2725#endif
2726 return(NULL);
2727 }
2728
2729 if (name == NULL) {
2730#ifdef DEBUG_TREE
2731 xmlGenericError(xmlGenericErrorContext,
2732 "xmlNewChild : name == NULL\n");
2733#endif
2734 return(NULL);
2735 }
2736
2737 /*
2738 * Allocate a new node
2739 */
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002740 if (parent->type == XML_ELEMENT_NODE) {
2741 if (ns == NULL)
2742 cur = xmlNewDocNode(parent->doc, parent->ns, name, content);
2743 else
2744 cur = xmlNewDocNode(parent->doc, ns, name, content);
2745 } else if ((parent->type == XML_DOCUMENT_NODE) ||
2746 (parent->type == XML_HTML_DOCUMENT_NODE)) {
2747 if (ns == NULL)
2748 cur = xmlNewDocNode((xmlDocPtr) parent, NULL, name, content);
2749 else
2750 cur = xmlNewDocNode((xmlDocPtr) parent, ns, name, content);
Daniel Veillard7e3f1402002-10-28 18:52:57 +00002751 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2752 cur = xmlNewDocNode( parent->doc, ns, name, content);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002753 } else {
2754 return(NULL);
2755 }
Owen Taylor3473f882001-02-23 17:55:21 +00002756 if (cur == NULL) return(NULL);
2757
2758 /*
2759 * add the new element at the end of the children list.
2760 */
2761 cur->type = XML_ELEMENT_NODE;
2762 cur->parent = parent;
2763 cur->doc = parent->doc;
2764 if (parent->children == NULL) {
2765 parent->children = cur;
2766 parent->last = cur;
2767 } else {
2768 prev = parent->last;
2769 prev->next = cur;
2770 cur->prev = prev;
2771 parent->last = cur;
2772 }
2773
2774 return(cur);
2775}
Daniel Veillard652327a2003-09-29 18:02:38 +00002776#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002777
2778/**
2779 * xmlAddNextSibling:
2780 * @cur: the child node
2781 * @elem: the new node
2782 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002783 * Add a new node @elem as the next sibling of @cur
2784 * If the new node was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00002785 * first unlinked from its existing context.
2786 * As a result of text merging @elem may be freed.
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002787 * If the new node is ATTRIBUTE, it is added into properties instead of children.
2788 * If there is an attribute with equal name, it is first destroyed.
Owen Taylor3473f882001-02-23 17:55:21 +00002789 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002790 * Returns the new node or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002791 */
2792xmlNodePtr
2793xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) {
2794 if (cur == NULL) {
2795#ifdef DEBUG_TREE
2796 xmlGenericError(xmlGenericErrorContext,
2797 "xmlAddNextSibling : cur == NULL\n");
2798#endif
2799 return(NULL);
2800 }
2801 if (elem == NULL) {
2802#ifdef DEBUG_TREE
2803 xmlGenericError(xmlGenericErrorContext,
2804 "xmlAddNextSibling : elem == NULL\n");
2805#endif
2806 return(NULL);
2807 }
2808
2809 xmlUnlinkNode(elem);
2810
2811 if (elem->type == XML_TEXT_NODE) {
2812 if (cur->type == XML_TEXT_NODE) {
Owen Taylor3473f882001-02-23 17:55:21 +00002813 xmlNodeAddContent(cur, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002814 xmlFreeNode(elem);
2815 return(cur);
2816 }
Daniel Veillard9e1c72d2001-08-31 20:03:19 +00002817 if ((cur->next != NULL) && (cur->next->type == XML_TEXT_NODE) &&
2818 (cur->name == cur->next->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002819 xmlChar *tmp;
2820
2821 tmp = xmlStrdup(elem->content);
2822 tmp = xmlStrcat(tmp, cur->next->content);
2823 xmlNodeSetContent(cur->next, tmp);
2824 xmlFree(tmp);
Owen Taylor3473f882001-02-23 17:55:21 +00002825 xmlFreeNode(elem);
2826 return(cur->next);
2827 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002828 } else if (elem->type == XML_ATTRIBUTE_NODE) {
2829 /* check if an attribute with the same name exists */
2830 xmlAttrPtr attr;
2831
2832 if (elem->ns == NULL)
2833 attr = xmlHasProp(cur->parent, elem->name);
2834 else
2835 attr = xmlHasNsProp(cur->parent, elem->name, elem->ns->href);
2836 if ((attr != NULL) && (attr != (xmlAttrPtr) elem)) {
2837 /* different instance, destroy it (attributes must be unique) */
2838 xmlFreeProp(attr);
2839 }
Owen Taylor3473f882001-02-23 17:55:21 +00002840 }
2841
2842 if (elem->doc != cur->doc) {
2843 xmlSetTreeDoc(elem, cur->doc);
2844 }
2845 elem->parent = cur->parent;
2846 elem->prev = cur;
2847 elem->next = cur->next;
2848 cur->next = elem;
2849 if (elem->next != NULL)
2850 elem->next->prev = elem;
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002851 if ((elem->parent != NULL) && (elem->parent->last == cur) && (elem->type != XML_ATTRIBUTE_NODE))
Owen Taylor3473f882001-02-23 17:55:21 +00002852 elem->parent->last = elem;
2853 return(elem);
2854}
2855
Daniel Veillard652327a2003-09-29 18:02:38 +00002856#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002857/**
2858 * xmlAddPrevSibling:
2859 * @cur: the child node
2860 * @elem: the new node
2861 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002862 * Add a new node @elem as the previous sibling of @cur
Owen Taylor3473f882001-02-23 17:55:21 +00002863 * merging adjacent TEXT nodes (@elem may be freed)
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002864 * If the new node was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00002865 * first unlinked from its existing context.
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002866 * If the new node is ATTRIBUTE, it is added into properties instead of children.
2867 * If there is an attribute with equal name, it is first destroyed.
Owen Taylor3473f882001-02-23 17:55:21 +00002868 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002869 * Returns the new node or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002870 */
2871xmlNodePtr
2872xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) {
2873 if (cur == NULL) {
2874#ifdef DEBUG_TREE
2875 xmlGenericError(xmlGenericErrorContext,
2876 "xmlAddPrevSibling : cur == NULL\n");
2877#endif
2878 return(NULL);
2879 }
2880 if (elem == NULL) {
2881#ifdef DEBUG_TREE
2882 xmlGenericError(xmlGenericErrorContext,
2883 "xmlAddPrevSibling : elem == NULL\n");
2884#endif
2885 return(NULL);
2886 }
2887
2888 xmlUnlinkNode(elem);
2889
2890 if (elem->type == XML_TEXT_NODE) {
2891 if (cur->type == XML_TEXT_NODE) {
Owen Taylor3473f882001-02-23 17:55:21 +00002892 xmlChar *tmp;
2893
2894 tmp = xmlStrdup(elem->content);
2895 tmp = xmlStrcat(tmp, cur->content);
2896 xmlNodeSetContent(cur, tmp);
2897 xmlFree(tmp);
Owen Taylor3473f882001-02-23 17:55:21 +00002898 xmlFreeNode(elem);
2899 return(cur);
2900 }
Daniel Veillard9e1c72d2001-08-31 20:03:19 +00002901 if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE) &&
2902 (cur->name == cur->prev->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002903 xmlNodeAddContent(cur->prev, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002904 xmlFreeNode(elem);
2905 return(cur->prev);
2906 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002907 } else if (elem->type == XML_ATTRIBUTE_NODE) {
2908 /* check if an attribute with the same name exists */
2909 xmlAttrPtr attr;
2910
2911 if (elem->ns == NULL)
2912 attr = xmlHasProp(cur->parent, elem->name);
2913 else
2914 attr = xmlHasNsProp(cur->parent, elem->name, elem->ns->href);
2915 if ((attr != NULL) && (attr != (xmlAttrPtr) elem)) {
2916 /* different instance, destroy it (attributes must be unique) */
2917 xmlFreeProp(attr);
2918 }
Owen Taylor3473f882001-02-23 17:55:21 +00002919 }
2920
2921 if (elem->doc != cur->doc) {
2922 xmlSetTreeDoc(elem, cur->doc);
2923 }
2924 elem->parent = cur->parent;
2925 elem->next = cur;
2926 elem->prev = cur->prev;
2927 cur->prev = elem;
2928 if (elem->prev != NULL)
2929 elem->prev->next = elem;
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002930 if (elem->parent != NULL) {
2931 if (elem->type == XML_ATTRIBUTE_NODE) {
2932 if (elem->parent->properties == (xmlAttrPtr) cur) {
2933 elem->parent->properties = (xmlAttrPtr) elem;
2934 }
2935 } else {
2936 if (elem->parent->children == cur) {
2937 elem->parent->children = elem;
2938 }
2939 }
2940 }
Owen Taylor3473f882001-02-23 17:55:21 +00002941 return(elem);
2942}
Daniel Veillard652327a2003-09-29 18:02:38 +00002943#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002944
2945/**
2946 * xmlAddSibling:
2947 * @cur: the child node
2948 * @elem: the new node
2949 *
2950 * Add a new element @elem to the list of siblings of @cur
2951 * merging adjacent TEXT nodes (@elem may be freed)
2952 * If the new element was already inserted in a document it is
2953 * first unlinked from its existing context.
2954 *
2955 * Returns the new element or NULL in case of error.
2956 */
2957xmlNodePtr
2958xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) {
2959 xmlNodePtr parent;
2960
2961 if (cur == NULL) {
2962#ifdef DEBUG_TREE
2963 xmlGenericError(xmlGenericErrorContext,
2964 "xmlAddSibling : cur == NULL\n");
2965#endif
2966 return(NULL);
2967 }
2968
2969 if (elem == NULL) {
2970#ifdef DEBUG_TREE
2971 xmlGenericError(xmlGenericErrorContext,
2972 "xmlAddSibling : elem == NULL\n");
2973#endif
2974 return(NULL);
2975 }
2976
2977 /*
2978 * Constant time is we can rely on the ->parent->last to find
2979 * the last sibling.
2980 */
2981 if ((cur->parent != NULL) &&
2982 (cur->parent->children != NULL) &&
2983 (cur->parent->last != NULL) &&
2984 (cur->parent->last->next == NULL)) {
2985 cur = cur->parent->last;
2986 } else {
2987 while (cur->next != NULL) cur = cur->next;
2988 }
2989
2990 xmlUnlinkNode(elem);
2991
2992 if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002993 xmlNodeAddContent(cur, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002994 xmlFreeNode(elem);
2995 return(cur);
2996 }
2997
2998 if (elem->doc != cur->doc) {
2999 xmlSetTreeDoc(elem, cur->doc);
3000 }
3001 parent = cur->parent;
3002 elem->prev = cur;
3003 elem->next = NULL;
3004 elem->parent = parent;
3005 cur->next = elem;
3006 if (parent != NULL)
3007 parent->last = elem;
3008
3009 return(elem);
3010}
3011
3012/**
3013 * xmlAddChildList:
3014 * @parent: the parent node
3015 * @cur: the first node in the list
3016 *
3017 * Add a list of node at the end of the child list of the parent
3018 * merging adjacent TEXT nodes (@cur may be freed)
3019 *
3020 * Returns the last child or NULL in case of error.
3021 */
3022xmlNodePtr
3023xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) {
3024 xmlNodePtr prev;
3025
3026 if (parent == NULL) {
3027#ifdef DEBUG_TREE
3028 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00003029 "xmlAddChildList : parent == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003030#endif
3031 return(NULL);
3032 }
3033
3034 if (cur == NULL) {
3035#ifdef DEBUG_TREE
3036 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00003037 "xmlAddChildList : child == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003038#endif
3039 return(NULL);
3040 }
3041
3042 if ((cur->doc != NULL) && (parent->doc != NULL) &&
3043 (cur->doc != parent->doc)) {
3044#ifdef DEBUG_TREE
3045 xmlGenericError(xmlGenericErrorContext,
3046 "Elements moved to a different document\n");
3047#endif
3048 }
3049
3050 /*
3051 * add the first element at the end of the children list.
3052 */
Daniel Veillard5335dc52003-01-01 20:59:38 +00003053
Owen Taylor3473f882001-02-23 17:55:21 +00003054 if (parent->children == NULL) {
3055 parent->children = cur;
3056 } else {
3057 /*
3058 * If cur and parent->last both are TEXT nodes, then merge them.
3059 */
3060 if ((cur->type == XML_TEXT_NODE) &&
3061 (parent->last->type == XML_TEXT_NODE) &&
3062 (cur->name == parent->last->name)) {
Daniel Veillard5335dc52003-01-01 20:59:38 +00003063 xmlNodeAddContent(parent->last, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003064 /*
3065 * if it's the only child, nothing more to be done.
3066 */
3067 if (cur->next == NULL) {
3068 xmlFreeNode(cur);
3069 return(parent->last);
3070 }
3071 prev = cur;
3072 cur = cur->next;
3073 xmlFreeNode(prev);
3074 }
3075 prev = parent->last;
3076 prev->next = cur;
3077 cur->prev = prev;
3078 }
3079 while (cur->next != NULL) {
3080 cur->parent = parent;
3081 if (cur->doc != parent->doc) {
3082 xmlSetTreeDoc(cur, parent->doc);
3083 }
3084 cur = cur->next;
3085 }
3086 cur->parent = parent;
3087 cur->doc = parent->doc; /* the parent may not be linked to a doc ! */
3088 parent->last = cur;
3089
3090 return(cur);
3091}
3092
3093/**
3094 * xmlAddChild:
3095 * @parent: the parent node
3096 * @cur: the child node
3097 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003098 * Add a new node to @parent, at the end of the child (or property) list
Owen Taylor3473f882001-02-23 17:55:21 +00003099 * merging adjacent TEXT nodes (in which case @cur is freed)
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003100 * If the new node is ATTRIBUTE, it is added into properties instead of children.
3101 * If there is an attribute with equal name, it is first destroyed.
3102 *
Owen Taylor3473f882001-02-23 17:55:21 +00003103 * Returns the child or NULL in case of error.
3104 */
3105xmlNodePtr
3106xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) {
3107 xmlNodePtr prev;
3108
3109 if (parent == NULL) {
3110#ifdef DEBUG_TREE
3111 xmlGenericError(xmlGenericErrorContext,
3112 "xmlAddChild : parent == NULL\n");
3113#endif
3114 return(NULL);
3115 }
3116
3117 if (cur == NULL) {
3118#ifdef DEBUG_TREE
3119 xmlGenericError(xmlGenericErrorContext,
3120 "xmlAddChild : child == NULL\n");
3121#endif
3122 return(NULL);
3123 }
3124
Owen Taylor3473f882001-02-23 17:55:21 +00003125 /*
3126 * If cur is a TEXT node, merge its content with adjacent TEXT nodes
Owen Taylor3473f882001-02-23 17:55:21 +00003127 * cur is then freed.
3128 */
3129 if (cur->type == XML_TEXT_NODE) {
Daniel Veillard7db37732001-07-12 01:20:08 +00003130 if ((parent->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003131 (parent->content != NULL) &&
3132 (parent != cur)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003133 xmlNodeAddContent(parent, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003134 xmlFreeNode(cur);
3135 return(parent);
3136 }
3137 if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003138 (parent->last->name == cur->name) &&
3139 (parent->last != cur)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003140 xmlNodeAddContent(parent->last, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003141 xmlFreeNode(cur);
3142 return(parent->last);
3143 }
3144 }
3145
3146 /*
3147 * add the new element at the end of the children list.
3148 */
Daniel Veillard5335dc52003-01-01 20:59:38 +00003149 prev = cur->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00003150 cur->parent = parent;
3151 if (cur->doc != parent->doc) {
3152 xmlSetTreeDoc(cur, parent->doc);
3153 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00003154 /* this check prevents a loop on tree-traversions if a developer
3155 * tries to add a node to its parent multiple times
3156 */
3157 if (prev == parent)
3158 return(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00003159
3160 /*
Daniel Veillard7db37732001-07-12 01:20:08 +00003161 * Coalescing
Owen Taylor3473f882001-02-23 17:55:21 +00003162 */
Daniel Veillard7db37732001-07-12 01:20:08 +00003163 if ((parent->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003164 (parent->content != NULL) &&
3165 (parent != cur)) {
Daniel Veillard7db37732001-07-12 01:20:08 +00003166 xmlNodeAddContent(parent, cur->content);
Daniel Veillard7db37732001-07-12 01:20:08 +00003167 xmlFreeNode(cur);
3168 return(parent);
Owen Taylor3473f882001-02-23 17:55:21 +00003169 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003170 if (cur->type == XML_ATTRIBUTE_NODE) {
3171 if (parent->properties == NULL) {
3172 parent->properties = (xmlAttrPtr) cur;
3173 } else {
3174 /* check if an attribute with the same name exists */
3175 xmlAttrPtr lastattr;
Owen Taylor3473f882001-02-23 17:55:21 +00003176
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003177 if (cur->ns == NULL)
3178 lastattr = xmlHasProp(parent, cur->name);
3179 else
3180 lastattr = xmlHasNsProp(parent, cur->name, cur->ns->href);
3181 if ((lastattr != NULL) && (lastattr != (xmlAttrPtr) cur)) {
3182 /* different instance, destroy it (attributes must be unique) */
3183 xmlFreeProp(lastattr);
3184 }
3185 /* find the end */
3186 lastattr = parent->properties;
3187 while (lastattr->next != NULL) {
3188 lastattr = lastattr->next;
3189 }
3190 lastattr->next = (xmlAttrPtr) cur;
3191 ((xmlAttrPtr) cur)->prev = lastattr;
3192 }
3193 } else {
3194 if (parent->children == NULL) {
3195 parent->children = cur;
3196 parent->last = cur;
3197 } else {
3198 prev = parent->last;
3199 prev->next = cur;
3200 cur->prev = prev;
3201 parent->last = cur;
3202 }
3203 }
Owen Taylor3473f882001-02-23 17:55:21 +00003204 return(cur);
3205}
3206
3207/**
3208 * xmlGetLastChild:
3209 * @parent: the parent node
3210 *
3211 * Search the last child of a node.
3212 * Returns the last child or NULL if none.
3213 */
3214xmlNodePtr
3215xmlGetLastChild(xmlNodePtr parent) {
3216 if (parent == NULL) {
3217#ifdef DEBUG_TREE
3218 xmlGenericError(xmlGenericErrorContext,
3219 "xmlGetLastChild : parent == NULL\n");
3220#endif
3221 return(NULL);
3222 }
3223 return(parent->last);
3224}
3225
3226/**
3227 * xmlFreeNodeList:
3228 * @cur: the first node in the list
3229 *
3230 * Free a node and all its siblings, this is a recursive behaviour, all
3231 * the children are freed too.
3232 */
3233void
3234xmlFreeNodeList(xmlNodePtr cur) {
3235 xmlNodePtr next;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003236 xmlDictPtr dict = NULL;
3237
3238 if (cur == NULL) return;
Daniel Veillarde6a55192002-01-14 17:11:53 +00003239 if (cur->type == XML_NAMESPACE_DECL) {
3240 xmlFreeNsList((xmlNsPtr) cur);
3241 return;
3242 }
Daniel Veillard9adc0462003-03-24 18:39:54 +00003243 if ((cur->type == XML_DOCUMENT_NODE) ||
3244#ifdef LIBXML_DOCB_ENABLED
3245 (cur->type == XML_DOCB_DOCUMENT_NODE) ||
Daniel Veillard9adc0462003-03-24 18:39:54 +00003246#endif
Daniel Veillard6560a422003-03-27 21:25:38 +00003247 (cur->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard9adc0462003-03-24 18:39:54 +00003248 xmlFreeDoc((xmlDocPtr) cur);
3249 return;
3250 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003251 if (cur->doc != NULL) dict = cur->doc->dict;
Owen Taylor3473f882001-02-23 17:55:21 +00003252 while (cur != NULL) {
3253 next = cur->next;
Daniel Veillard02141ea2001-04-30 11:46:40 +00003254 if (cur->type != XML_DTD_NODE) {
Daniel Veillard5335dc52003-01-01 20:59:38 +00003255
Daniel Veillarda880b122003-04-21 21:36:41 +00003256 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00003257 xmlDeregisterNodeDefaultValue(cur);
3258
Daniel Veillard02141ea2001-04-30 11:46:40 +00003259 if ((cur->children != NULL) &&
3260 (cur->type != XML_ENTITY_REF_NODE))
3261 xmlFreeNodeList(cur->children);
Daniel Veillard01c13b52002-12-10 15:19:08 +00003262 if (((cur->type == XML_ELEMENT_NODE) ||
3263 (cur->type == XML_XINCLUDE_START) ||
3264 (cur->type == XML_XINCLUDE_END)) &&
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003265 (cur->properties != NULL))
Daniel Veillard02141ea2001-04-30 11:46:40 +00003266 xmlFreePropList(cur->properties);
Daniel Veillard7db37732001-07-12 01:20:08 +00003267 if ((cur->type != XML_ELEMENT_NODE) &&
3268 (cur->type != XML_XINCLUDE_START) &&
3269 (cur->type != XML_XINCLUDE_END) &&
3270 (cur->type != XML_ENTITY_REF_NODE)) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003271 DICT_FREE(cur->content)
Daniel Veillard7db37732001-07-12 01:20:08 +00003272 }
3273 if (((cur->type == XML_ELEMENT_NODE) ||
3274 (cur->type == XML_XINCLUDE_START) ||
3275 (cur->type == XML_XINCLUDE_END)) &&
3276 (cur->nsDef != NULL))
3277 xmlFreeNsList(cur->nsDef);
3278
Daniel Veillard9cc6dc62001-06-11 08:09:20 +00003279 /*
3280 * When a node is a text node or a comment, it uses a global static
3281 * variable for the name of the node.
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003282 * Otherwise the node name might come from the document's
3283 * dictionnary
Daniel Veillard9cc6dc62001-06-11 08:09:20 +00003284 */
Daniel Veillard02141ea2001-04-30 11:46:40 +00003285 if ((cur->name != NULL) &&
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003286 (cur->type != XML_TEXT_NODE) &&
3287 (cur->type != XML_COMMENT_NODE))
3288 DICT_FREE(cur->name)
Daniel Veillard02141ea2001-04-30 11:46:40 +00003289 xmlFree(cur);
3290 }
Owen Taylor3473f882001-02-23 17:55:21 +00003291 cur = next;
3292 }
3293}
3294
3295/**
3296 * xmlFreeNode:
3297 * @cur: the node
3298 *
3299 * Free a node, this is a recursive behaviour, all the children are freed too.
3300 * This doesn't unlink the child from the list, use xmlUnlinkNode() first.
3301 */
3302void
3303xmlFreeNode(xmlNodePtr cur) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003304 xmlDictPtr dict = NULL;
3305
3306 if (cur == NULL) return;
Daniel Veillard5335dc52003-01-01 20:59:38 +00003307
Daniel Veillard02141ea2001-04-30 11:46:40 +00003308 /* use xmlFreeDtd for DTD nodes */
Daniel Veillarde6a55192002-01-14 17:11:53 +00003309 if (cur->type == XML_DTD_NODE) {
3310 xmlFreeDtd((xmlDtdPtr) cur);
Owen Taylor3473f882001-02-23 17:55:21 +00003311 return;
Daniel Veillarde6a55192002-01-14 17:11:53 +00003312 }
3313 if (cur->type == XML_NAMESPACE_DECL) {
3314 xmlFreeNs((xmlNsPtr) cur);
3315 return;
3316 }
Daniel Veillarda70d62f2002-11-07 14:18:03 +00003317 if (cur->type == XML_ATTRIBUTE_NODE) {
3318 xmlFreeProp((xmlAttrPtr) cur);
3319 return;
3320 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00003321
Daniel Veillarda880b122003-04-21 21:36:41 +00003322 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00003323 xmlDeregisterNodeDefaultValue(cur);
3324
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003325 if (cur->doc != NULL) dict = cur->doc->dict;
3326
Owen Taylor3473f882001-02-23 17:55:21 +00003327 if ((cur->children != NULL) &&
3328 (cur->type != XML_ENTITY_REF_NODE))
3329 xmlFreeNodeList(cur->children);
Daniel Veillard01c13b52002-12-10 15:19:08 +00003330 if (((cur->type == XML_ELEMENT_NODE) ||
3331 (cur->type == XML_XINCLUDE_START) ||
3332 (cur->type == XML_XINCLUDE_END)) &&
3333 (cur->properties != NULL))
Daniel Veillard02141ea2001-04-30 11:46:40 +00003334 xmlFreePropList(cur->properties);
Daniel Veillard7db37732001-07-12 01:20:08 +00003335 if ((cur->type != XML_ELEMENT_NODE) &&
3336 (cur->content != NULL) &&
3337 (cur->type != XML_ENTITY_REF_NODE) &&
3338 (cur->type != XML_XINCLUDE_END) &&
3339 (cur->type != XML_XINCLUDE_START)) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003340 DICT_FREE(cur->content)
Daniel Veillard7db37732001-07-12 01:20:08 +00003341 }
3342
Daniel Veillardacd370f2001-06-09 17:17:51 +00003343 /*
3344 * When a node is a text node or a comment, it uses a global static
3345 * variable for the name of the node.
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003346 * Otherwise the node name might come from the document's dictionnary
Daniel Veillardacd370f2001-06-09 17:17:51 +00003347 */
Owen Taylor3473f882001-02-23 17:55:21 +00003348 if ((cur->name != NULL) &&
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003349 (cur->type != XML_TEXT_NODE) &&
3350 (cur->type != XML_COMMENT_NODE))
3351 DICT_FREE(cur->name)
Daniel Veillardacd370f2001-06-09 17:17:51 +00003352
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003353 if (((cur->type == XML_ELEMENT_NODE) ||
3354 (cur->type == XML_XINCLUDE_START) ||
3355 (cur->type == XML_XINCLUDE_END)) &&
3356 (cur->nsDef != NULL))
3357 xmlFreeNsList(cur->nsDef);
Owen Taylor3473f882001-02-23 17:55:21 +00003358 xmlFree(cur);
3359}
3360
3361/**
3362 * xmlUnlinkNode:
3363 * @cur: the node
3364 *
3365 * Unlink a node from it's current context, the node is not freed
3366 */
3367void
3368xmlUnlinkNode(xmlNodePtr cur) {
3369 if (cur == NULL) {
3370#ifdef DEBUG_TREE
3371 xmlGenericError(xmlGenericErrorContext,
3372 "xmlUnlinkNode : node == NULL\n");
3373#endif
3374 return;
3375 }
Daniel Veillard29e43992001-12-13 22:21:58 +00003376 if (cur->type == XML_DTD_NODE) {
3377 xmlDocPtr doc;
3378 doc = cur->doc;
Daniel Veillarda067e652003-05-01 08:03:46 +00003379 if (doc != NULL) {
3380 if (doc->intSubset == (xmlDtdPtr) cur)
3381 doc->intSubset = NULL;
3382 if (doc->extSubset == (xmlDtdPtr) cur)
3383 doc->extSubset = NULL;
3384 }
Daniel Veillard29e43992001-12-13 22:21:58 +00003385 }
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003386 if (cur->parent != NULL) {
3387 xmlNodePtr parent;
3388 parent = cur->parent;
3389 if (cur->type == XML_ATTRIBUTE_NODE) {
3390 if (parent->properties == (xmlAttrPtr) cur)
3391 parent->properties = ((xmlAttrPtr) cur)->next;
3392 } else {
3393 if (parent->children == cur)
3394 parent->children = cur->next;
3395 if (parent->last == cur)
3396 parent->last = cur->prev;
3397 }
3398 cur->parent = NULL;
3399 }
Owen Taylor3473f882001-02-23 17:55:21 +00003400 if (cur->next != NULL)
3401 cur->next->prev = cur->prev;
3402 if (cur->prev != NULL)
3403 cur->prev->next = cur->next;
3404 cur->next = cur->prev = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00003405}
3406
Daniel Veillard652327a2003-09-29 18:02:38 +00003407#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00003408/**
3409 * xmlReplaceNode:
3410 * @old: the old node
3411 * @cur: the node
3412 *
3413 * Unlink the old node from it's current context, prune the new one
Daniel Veillardd1640922001-12-17 15:30:10 +00003414 * at the same place. If @cur was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00003415 * first unlinked from its existing context.
3416 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003417 * Returns the @old node
Owen Taylor3473f882001-02-23 17:55:21 +00003418 */
3419xmlNodePtr
3420xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) {
3421 if (old == NULL) {
3422#ifdef DEBUG_TREE
3423 xmlGenericError(xmlGenericErrorContext,
3424 "xmlReplaceNode : old == NULL\n");
3425#endif
3426 return(NULL);
3427 }
3428 if (cur == NULL) {
3429 xmlUnlinkNode(old);
3430 return(old);
3431 }
3432 if (cur == old) {
3433 return(old);
3434 }
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003435 if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) {
3436#ifdef DEBUG_TREE
3437 xmlGenericError(xmlGenericErrorContext,
3438 "xmlReplaceNode : Trying to replace attribute node with other node type\n");
3439#endif
3440 return(old);
3441 }
3442 if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) {
3443#ifdef DEBUG_TREE
3444 xmlGenericError(xmlGenericErrorContext,
3445 "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n");
3446#endif
3447 return(old);
3448 }
Owen Taylor3473f882001-02-23 17:55:21 +00003449 xmlUnlinkNode(cur);
3450 cur->doc = old->doc;
3451 cur->parent = old->parent;
3452 cur->next = old->next;
3453 if (cur->next != NULL)
3454 cur->next->prev = cur;
3455 cur->prev = old->prev;
3456 if (cur->prev != NULL)
3457 cur->prev->next = cur;
3458 if (cur->parent != NULL) {
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003459 if (cur->type == XML_ATTRIBUTE_NODE) {
3460 if (cur->parent->properties == (xmlAttrPtr)old)
3461 cur->parent->properties = ((xmlAttrPtr) cur);
3462 } else {
3463 if (cur->parent->children == old)
3464 cur->parent->children = cur;
3465 if (cur->parent->last == old)
3466 cur->parent->last = cur;
3467 }
Owen Taylor3473f882001-02-23 17:55:21 +00003468 }
3469 old->next = old->prev = NULL;
3470 old->parent = NULL;
3471 return(old);
3472}
Daniel Veillard652327a2003-09-29 18:02:38 +00003473#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00003474
3475/************************************************************************
3476 * *
3477 * Copy operations *
3478 * *
3479 ************************************************************************/
3480
3481/**
3482 * xmlCopyNamespace:
3483 * @cur: the namespace
3484 *
3485 * Do a copy of the namespace.
3486 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003487 * Returns: a new #xmlNsPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003488 */
3489xmlNsPtr
3490xmlCopyNamespace(xmlNsPtr cur) {
3491 xmlNsPtr ret;
3492
3493 if (cur == NULL) return(NULL);
3494 switch (cur->type) {
3495 case XML_LOCAL_NAMESPACE:
3496 ret = xmlNewNs(NULL, cur->href, cur->prefix);
3497 break;
3498 default:
3499#ifdef DEBUG_TREE
3500 xmlGenericError(xmlGenericErrorContext,
3501 "xmlCopyNamespace: invalid type %d\n", cur->type);
3502#endif
3503 return(NULL);
3504 }
3505 return(ret);
3506}
3507
3508/**
3509 * xmlCopyNamespaceList:
3510 * @cur: the first namespace
3511 *
3512 * Do a copy of an namespace list.
3513 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003514 * Returns: a new #xmlNsPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003515 */
3516xmlNsPtr
3517xmlCopyNamespaceList(xmlNsPtr cur) {
3518 xmlNsPtr ret = NULL;
3519 xmlNsPtr p = NULL,q;
3520
3521 while (cur != NULL) {
3522 q = xmlCopyNamespace(cur);
3523 if (p == NULL) {
3524 ret = p = q;
3525 } else {
3526 p->next = q;
3527 p = q;
3528 }
3529 cur = cur->next;
3530 }
3531 return(ret);
3532}
3533
3534static xmlNodePtr
3535xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent);
3536/**
3537 * xmlCopyProp:
3538 * @target: the element where the attribute will be grafted
3539 * @cur: the attribute
3540 *
3541 * Do a copy of the attribute.
3542 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003543 * Returns: a new #xmlAttrPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003544 */
3545xmlAttrPtr
3546xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) {
3547 xmlAttrPtr ret;
3548
3549 if (cur == NULL) return(NULL);
3550 if (target != NULL)
3551 ret = xmlNewDocProp(target->doc, cur->name, NULL);
3552 else if (cur->parent != NULL)
3553 ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL);
3554 else if (cur->children != NULL)
3555 ret = xmlNewDocProp(cur->children->doc, cur->name, NULL);
3556 else
3557 ret = xmlNewDocProp(NULL, cur->name, NULL);
3558 if (ret == NULL) return(NULL);
3559 ret->parent = target;
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00003560
Owen Taylor3473f882001-02-23 17:55:21 +00003561 if ((cur->ns != NULL) && (target != NULL)) {
Daniel Veillard8107a222002-01-13 14:10:10 +00003562 xmlNsPtr ns;
Daniel Veillard652327a2003-09-29 18:02:38 +00003563
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00003564 ns = xmlSearchNs(target->doc, target, cur->ns->prefix);
3565 if (ns == NULL) {
3566 /*
3567 * Humm, we are copying an element whose namespace is defined
3568 * out of the new tree scope. Search it in the original tree
3569 * and add it at the top of the new tree
3570 */
3571 ns = xmlSearchNs(cur->doc, cur->parent, cur->ns->prefix);
3572 if (ns != NULL) {
3573 xmlNodePtr root = target;
3574 xmlNodePtr pred = NULL;
3575
3576 while (root->parent != NULL) {
3577 pred = root;
3578 root = root->parent;
3579 }
3580 if (root == (xmlNodePtr) target->doc) {
3581 /* correct possibly cycling above the document elt */
3582 root = pred;
3583 }
3584 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
3585 }
3586 } else {
3587 /*
3588 * we have to find something appropriate here since
3589 * we cant be sure, that the namespce we found is identified
3590 * by the prefix
3591 */
Daniel Veillard044fc6b2002-03-04 17:09:44 +00003592 if (xmlStrEqual(ns->href, cur->ns->href)) {
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00003593 /* this is the nice case */
3594 ret->ns = ns;
3595 } else {
3596 /*
3597 * we are in trouble: we need a new reconcilied namespace.
3598 * This is expensive
3599 */
3600 ret->ns = xmlNewReconciliedNs(target->doc, target, cur->ns);
3601 }
3602 }
3603
Owen Taylor3473f882001-02-23 17:55:21 +00003604 } else
3605 ret->ns = NULL;
3606
3607 if (cur->children != NULL) {
3608 xmlNodePtr tmp;
3609
3610 ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret);
3611 ret->last = NULL;
3612 tmp = ret->children;
3613 while (tmp != NULL) {
3614 /* tmp->parent = (xmlNodePtr)ret; */
3615 if (tmp->next == NULL)
3616 ret->last = tmp;
3617 tmp = tmp->next;
3618 }
3619 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00003620 /*
3621 * Try to handle IDs
3622 */
Daniel Veillarda3db2e32002-03-08 15:46:57 +00003623 if ((target!= NULL) && (cur!= NULL) &&
3624 (target->doc != NULL) && (cur->doc != NULL) &&
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00003625 (cur->doc->ids != NULL) && (cur->parent != NULL)) {
3626 if (xmlIsID(cur->doc, cur->parent, cur)) {
3627 xmlChar *id;
3628
3629 id = xmlNodeListGetString(cur->doc, cur->children, 1);
3630 if (id != NULL) {
3631 xmlAddID(NULL, target->doc, id, ret);
3632 xmlFree(id);
3633 }
3634 }
3635 }
Owen Taylor3473f882001-02-23 17:55:21 +00003636 return(ret);
3637}
3638
3639/**
3640 * xmlCopyPropList:
3641 * @target: the element where the attributes will be grafted
3642 * @cur: the first attribute
3643 *
3644 * Do a copy of an attribute list.
3645 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003646 * Returns: a new #xmlAttrPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003647 */
3648xmlAttrPtr
3649xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) {
3650 xmlAttrPtr ret = NULL;
3651 xmlAttrPtr p = NULL,q;
3652
3653 while (cur != NULL) {
3654 q = xmlCopyProp(target, cur);
3655 if (p == NULL) {
3656 ret = p = q;
3657 } else {
3658 p->next = q;
3659 q->prev = p;
3660 p = q;
3661 }
3662 cur = cur->next;
3663 }
3664 return(ret);
3665}
3666
3667/*
Daniel Veillardd1640922001-12-17 15:30:10 +00003668 * NOTE about the CopyNode operations !
Owen Taylor3473f882001-02-23 17:55:21 +00003669 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003670 * They are split into external and internal parts for one
Owen Taylor3473f882001-02-23 17:55:21 +00003671 * tricky reason: namespaces. Doing a direct copy of a node
3672 * say RPM:Copyright without changing the namespace pointer to
3673 * something else can produce stale links. One way to do it is
3674 * to keep a reference counter but this doesn't work as soon
3675 * as one move the element or the subtree out of the scope of
3676 * the existing namespace. The actual solution seems to add
3677 * a copy of the namespace at the top of the copied tree if
3678 * not available in the subtree.
3679 * Hence two functions, the public front-end call the inner ones
3680 */
3681
3682static xmlNodePtr
Daniel Veillard3ec4c612001-08-28 20:39:49 +00003683xmlStaticCopyNode(const xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent,
Owen Taylor3473f882001-02-23 17:55:21 +00003684 int recursive) {
3685 xmlNodePtr ret;
3686
3687 if (node == NULL) return(NULL);
Daniel Veillard39196eb2001-06-19 18:09:42 +00003688 switch (node->type) {
3689 case XML_TEXT_NODE:
3690 case XML_CDATA_SECTION_NODE:
3691 case XML_ELEMENT_NODE:
Daniel Veillardec6725e2002-09-05 11:12:45 +00003692 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00003693 case XML_ENTITY_REF_NODE:
3694 case XML_ENTITY_NODE:
3695 case XML_PI_NODE:
3696 case XML_COMMENT_NODE:
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00003697 case XML_XINCLUDE_START:
3698 case XML_XINCLUDE_END:
3699 break;
3700 case XML_ATTRIBUTE_NODE:
3701 return((xmlNodePtr) xmlCopyProp(parent, (xmlAttrPtr) node));
3702 case XML_NAMESPACE_DECL:
3703 return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node));
3704
Daniel Veillard39196eb2001-06-19 18:09:42 +00003705 case XML_DOCUMENT_NODE:
3706 case XML_HTML_DOCUMENT_NODE:
3707#ifdef LIBXML_DOCB_ENABLED
3708 case XML_DOCB_DOCUMENT_NODE:
3709#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00003710#ifdef LIBXML_TREE_ENABLED
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00003711 return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, recursive));
Daniel Veillard652327a2003-09-29 18:02:38 +00003712#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard39196eb2001-06-19 18:09:42 +00003713 case XML_DOCUMENT_TYPE_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00003714 case XML_NOTATION_NODE:
3715 case XML_DTD_NODE:
3716 case XML_ELEMENT_DECL:
3717 case XML_ATTRIBUTE_DECL:
3718 case XML_ENTITY_DECL:
3719 return(NULL);
3720 }
Daniel Veillardb33c2012001-04-25 12:59:04 +00003721
Owen Taylor3473f882001-02-23 17:55:21 +00003722 /*
3723 * Allocate a new node and fill the fields.
3724 */
3725 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
3726 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00003727 xmlTreeErrMemory("copying node");
Owen Taylor3473f882001-02-23 17:55:21 +00003728 return(NULL);
3729 }
3730 memset(ret, 0, sizeof(xmlNode));
3731 ret->type = node->type;
3732
3733 ret->doc = doc;
3734 ret->parent = parent;
3735 if (node->name == xmlStringText)
3736 ret->name = xmlStringText;
3737 else if (node->name == xmlStringTextNoenc)
3738 ret->name = xmlStringTextNoenc;
3739 else if (node->name == xmlStringComment)
3740 ret->name = xmlStringComment;
3741 else if (node->name != NULL)
3742 ret->name = xmlStrdup(node->name);
Daniel Veillard7db37732001-07-12 01:20:08 +00003743 if ((node->type != XML_ELEMENT_NODE) &&
3744 (node->content != NULL) &&
3745 (node->type != XML_ENTITY_REF_NODE) &&
3746 (node->type != XML_XINCLUDE_END) &&
3747 (node->type != XML_XINCLUDE_START)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003748 ret->content = xmlStrdup(node->content);
Daniel Veillard8107a222002-01-13 14:10:10 +00003749 }else{
3750 if (node->type == XML_ELEMENT_NODE)
3751 ret->content = (void*)(long) node->content;
Owen Taylor3473f882001-02-23 17:55:21 +00003752 }
Daniel Veillardacb2bda2002-01-13 16:15:43 +00003753 if (parent != NULL) {
3754 xmlNodePtr tmp;
3755
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003756 /*
3757 * this is a tricky part for the node register thing:
3758 * in case ret does get coalesced in xmlAddChild
3759 * the deregister-node callback is called; so we register ret now already
3760 */
Daniel Veillarda880b122003-04-21 21:36:41 +00003761 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003762 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
3763
Daniel Veillardacb2bda2002-01-13 16:15:43 +00003764 tmp = xmlAddChild(parent, ret);
3765 /* node could have coalesced */
3766 if (tmp != ret)
3767 return(tmp);
3768 }
Owen Taylor3473f882001-02-23 17:55:21 +00003769
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003770 if (!recursive)
3771 goto out;
Owen Taylor3473f882001-02-23 17:55:21 +00003772 if (node->nsDef != NULL)
3773 ret->nsDef = xmlCopyNamespaceList(node->nsDef);
3774
3775 if (node->ns != NULL) {
3776 xmlNsPtr ns;
3777
3778 ns = xmlSearchNs(doc, ret, node->ns->prefix);
3779 if (ns == NULL) {
3780 /*
3781 * Humm, we are copying an element whose namespace is defined
3782 * out of the new tree scope. Search it in the original tree
3783 * and add it at the top of the new tree
3784 */
3785 ns = xmlSearchNs(node->doc, node, node->ns->prefix);
3786 if (ns != NULL) {
3787 xmlNodePtr root = ret;
3788
3789 while (root->parent != NULL) root = root->parent;
Daniel Veillarde82a9922001-04-22 12:12:58 +00003790 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00003791 }
3792 } else {
3793 /*
3794 * reference the existing namespace definition in our own tree.
3795 */
3796 ret->ns = ns;
3797 }
3798 }
3799 if (node->properties != NULL)
3800 ret->properties = xmlCopyPropList(ret, node->properties);
Daniel Veillardb33c2012001-04-25 12:59:04 +00003801 if (node->type == XML_ENTITY_REF_NODE) {
3802 if ((doc == NULL) || (node->doc != doc)) {
3803 /*
3804 * The copied node will go into a separate document, so
Daniel Veillardd1640922001-12-17 15:30:10 +00003805 * to avoid dangling references to the ENTITY_DECL node
Daniel Veillardb33c2012001-04-25 12:59:04 +00003806 * we cannot keep the reference. Try to find it in the
3807 * target document.
3808 */
3809 ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name);
3810 } else {
3811 ret->children = node->children;
3812 }
Daniel Veillard0ec98632001-11-14 15:04:32 +00003813 ret->last = ret->children;
3814 } else if (node->children != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00003815 ret->children = xmlStaticCopyNodeList(node->children, doc, ret);
Daniel Veillard0ec98632001-11-14 15:04:32 +00003816 UPDATE_LAST_CHILD_AND_PARENT(ret)
3817 }
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003818
3819out:
3820 /* if parent != NULL we already registered the node above */
3821 if (parent == NULL && xmlRegisterNodeDefaultValue)
3822 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
Owen Taylor3473f882001-02-23 17:55:21 +00003823 return(ret);
3824}
3825
3826static xmlNodePtr
3827xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) {
3828 xmlNodePtr ret = NULL;
3829 xmlNodePtr p = NULL,q;
3830
3831 while (node != NULL) {
Daniel Veillard652327a2003-09-29 18:02:38 +00003832#ifdef LIBXML_TREE_ENABLED
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00003833 if (node->type == XML_DTD_NODE ) {
Daniel Veillard4497e692001-06-09 14:19:02 +00003834 if (doc == NULL) {
3835 node = node->next;
3836 continue;
3837 }
Daniel Veillardb33c2012001-04-25 12:59:04 +00003838 if (doc->intSubset == NULL) {
3839 q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node );
3840 q->doc = doc;
3841 q->parent = parent;
3842 doc->intSubset = (xmlDtdPtr) q;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00003843 xmlAddChild(parent, q);
Daniel Veillardb33c2012001-04-25 12:59:04 +00003844 } else {
3845 q = (xmlNodePtr) doc->intSubset;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00003846 xmlAddChild(parent, q);
Daniel Veillardb33c2012001-04-25 12:59:04 +00003847 }
3848 } else
Daniel Veillard652327a2003-09-29 18:02:38 +00003849#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardb33c2012001-04-25 12:59:04 +00003850 q = xmlStaticCopyNode(node, doc, parent, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00003851 if (ret == NULL) {
3852 q->prev = NULL;
3853 ret = p = q;
Daniel Veillardacb2bda2002-01-13 16:15:43 +00003854 } else if (p != q) {
3855 /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */
Owen Taylor3473f882001-02-23 17:55:21 +00003856 p->next = q;
3857 q->prev = p;
3858 p = q;
3859 }
3860 node = node->next;
3861 }
3862 return(ret);
3863}
3864
3865/**
3866 * xmlCopyNode:
3867 * @node: the node
3868 * @recursive: if 1 do a recursive copy.
3869 *
3870 * Do a copy of the node.
3871 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003872 * Returns: a new #xmlNodePtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003873 */
3874xmlNodePtr
Daniel Veillard3ec4c612001-08-28 20:39:49 +00003875xmlCopyNode(const xmlNodePtr node, int recursive) {
Owen Taylor3473f882001-02-23 17:55:21 +00003876 xmlNodePtr ret;
3877
3878 ret = xmlStaticCopyNode(node, NULL, NULL, recursive);
3879 return(ret);
3880}
3881
3882/**
Daniel Veillard82daa812001-04-12 08:55:36 +00003883 * xmlDocCopyNode:
3884 * @node: the node
Daniel Veillardd1640922001-12-17 15:30:10 +00003885 * @doc: the document
Daniel Veillard82daa812001-04-12 08:55:36 +00003886 * @recursive: if 1 do a recursive copy.
3887 *
3888 * Do a copy of the node to a given document.
3889 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003890 * Returns: a new #xmlNodePtr, or NULL in case of error.
Daniel Veillard82daa812001-04-12 08:55:36 +00003891 */
3892xmlNodePtr
Daniel Veillard3ec4c612001-08-28 20:39:49 +00003893xmlDocCopyNode(const xmlNodePtr node, xmlDocPtr doc, int recursive) {
Daniel Veillard82daa812001-04-12 08:55:36 +00003894 xmlNodePtr ret;
3895
3896 ret = xmlStaticCopyNode(node, doc, NULL, recursive);
3897 return(ret);
3898}
3899
3900/**
Owen Taylor3473f882001-02-23 17:55:21 +00003901 * xmlCopyNodeList:
3902 * @node: the first node in the list.
3903 *
3904 * Do a recursive copy of the node list.
3905 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003906 * Returns: a new #xmlNodePtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003907 */
Daniel Veillard3ec4c612001-08-28 20:39:49 +00003908xmlNodePtr xmlCopyNodeList(const xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00003909 xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL);
3910 return(ret);
3911}
3912
Daniel Veillard652327a2003-09-29 18:02:38 +00003913#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00003914/**
Owen Taylor3473f882001-02-23 17:55:21 +00003915 * xmlCopyDtd:
3916 * @dtd: the dtd
3917 *
3918 * Do a copy of the dtd.
3919 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003920 * Returns: a new #xmlDtdPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003921 */
3922xmlDtdPtr
3923xmlCopyDtd(xmlDtdPtr dtd) {
3924 xmlDtdPtr ret;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00003925 xmlNodePtr cur, p = NULL, q;
Owen Taylor3473f882001-02-23 17:55:21 +00003926
3927 if (dtd == NULL) return(NULL);
3928 ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID);
3929 if (ret == NULL) return(NULL);
3930 if (dtd->entities != NULL)
3931 ret->entities = (void *) xmlCopyEntitiesTable(
3932 (xmlEntitiesTablePtr) dtd->entities);
3933 if (dtd->notations != NULL)
3934 ret->notations = (void *) xmlCopyNotationTable(
3935 (xmlNotationTablePtr) dtd->notations);
3936 if (dtd->elements != NULL)
3937 ret->elements = (void *) xmlCopyElementTable(
3938 (xmlElementTablePtr) dtd->elements);
3939 if (dtd->attributes != NULL)
3940 ret->attributes = (void *) xmlCopyAttributeTable(
3941 (xmlAttributeTablePtr) dtd->attributes);
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00003942 if (dtd->pentities != NULL)
3943 ret->pentities = (void *) xmlCopyEntitiesTable(
3944 (xmlEntitiesTablePtr) dtd->pentities);
3945
3946 cur = dtd->children;
3947 while (cur != NULL) {
3948 q = NULL;
3949
3950 if (cur->type == XML_ENTITY_DECL) {
3951 xmlEntityPtr tmp = (xmlEntityPtr) cur;
3952 switch (tmp->etype) {
3953 case XML_INTERNAL_GENERAL_ENTITY:
3954 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
3955 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
3956 q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name);
3957 break;
3958 case XML_INTERNAL_PARAMETER_ENTITY:
3959 case XML_EXTERNAL_PARAMETER_ENTITY:
3960 q = (xmlNodePtr)
3961 xmlGetParameterEntityFromDtd(ret, tmp->name);
3962 break;
3963 case XML_INTERNAL_PREDEFINED_ENTITY:
3964 break;
3965 }
3966 } else if (cur->type == XML_ELEMENT_DECL) {
3967 xmlElementPtr tmp = (xmlElementPtr) cur;
3968 q = (xmlNodePtr)
3969 xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix);
3970 } else if (cur->type == XML_ATTRIBUTE_DECL) {
3971 xmlAttributePtr tmp = (xmlAttributePtr) cur;
3972 q = (xmlNodePtr)
3973 xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix);
3974 } else if (cur->type == XML_COMMENT_NODE) {
3975 q = xmlCopyNode(cur, 0);
3976 }
3977
3978 if (q == NULL) {
3979 cur = cur->next;
3980 continue;
3981 }
3982
3983 if (p == NULL)
3984 ret->children = q;
3985 else
3986 p->next = q;
3987
3988 q->prev = p;
3989 q->parent = (xmlNodePtr) ret;
3990 q->next = NULL;
3991 ret->last = q;
3992 p = q;
3993 cur = cur->next;
3994 }
3995
Owen Taylor3473f882001-02-23 17:55:21 +00003996 return(ret);
3997}
3998
3999/**
4000 * xmlCopyDoc:
4001 * @doc: the document
4002 * @recursive: if 1 do a recursive copy.
4003 *
4004 * Do a copy of the document info. If recursive, the content tree will
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004005 * be copied too as well as DTD, namespaces and entities.
Owen Taylor3473f882001-02-23 17:55:21 +00004006 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004007 * Returns: a new #xmlDocPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004008 */
4009xmlDocPtr
4010xmlCopyDoc(xmlDocPtr doc, int recursive) {
4011 xmlDocPtr ret;
4012
4013 if (doc == NULL) return(NULL);
4014 ret = xmlNewDoc(doc->version);
4015 if (ret == NULL) return(NULL);
4016 if (doc->name != NULL)
4017 ret->name = xmlMemStrdup(doc->name);
4018 if (doc->encoding != NULL)
4019 ret->encoding = xmlStrdup(doc->encoding);
4020 ret->charset = doc->charset;
4021 ret->compression = doc->compression;
4022 ret->standalone = doc->standalone;
4023 if (!recursive) return(ret);
4024
Daniel Veillardb33c2012001-04-25 12:59:04 +00004025 ret->last = NULL;
4026 ret->children = NULL;
4027 if (doc->intSubset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004028 ret->intSubset = xmlCopyDtd(doc->intSubset);
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004029 xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret);
Daniel Veillardb33c2012001-04-25 12:59:04 +00004030 ret->intSubset->parent = ret;
4031 }
Owen Taylor3473f882001-02-23 17:55:21 +00004032 if (doc->oldNs != NULL)
4033 ret->oldNs = xmlCopyNamespaceList(doc->oldNs);
4034 if (doc->children != NULL) {
4035 xmlNodePtr tmp;
Daniel Veillardb33c2012001-04-25 12:59:04 +00004036
4037 ret->children = xmlStaticCopyNodeList(doc->children, ret,
4038 (xmlNodePtr)ret);
Owen Taylor3473f882001-02-23 17:55:21 +00004039 ret->last = NULL;
4040 tmp = ret->children;
4041 while (tmp != NULL) {
4042 if (tmp->next == NULL)
4043 ret->last = tmp;
4044 tmp = tmp->next;
4045 }
4046 }
4047 return(ret);
4048}
Daniel Veillard652327a2003-09-29 18:02:38 +00004049#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004050
4051/************************************************************************
4052 * *
4053 * Content access functions *
4054 * *
4055 ************************************************************************/
4056
4057/**
Daniel Veillard8faa7832001-11-26 15:58:08 +00004058 * xmlGetLineNo:
Daniel Veillard01c13b52002-12-10 15:19:08 +00004059 * @node: valid node
Daniel Veillard8faa7832001-11-26 15:58:08 +00004060 *
4061 * Get line number of node. this requires activation of this option
Daniel Veillardd1640922001-12-17 15:30:10 +00004062 * before invoking the parser by calling xmlLineNumbersDefault(1)
Daniel Veillard8faa7832001-11-26 15:58:08 +00004063 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004064 * Returns the line number if successful, -1 otherwise
Daniel Veillard8faa7832001-11-26 15:58:08 +00004065 */
4066long
4067xmlGetLineNo(xmlNodePtr node)
4068{
4069 long result = -1;
4070
4071 if (!node)
4072 return result;
4073 if (node->type == XML_ELEMENT_NODE)
4074 result = (long) node->content;
4075 else if ((node->prev != NULL) &&
4076 ((node->prev->type == XML_ELEMENT_NODE) ||
4077 (node->prev->type == XML_TEXT_NODE)))
4078 result = xmlGetLineNo(node->prev);
4079 else if ((node->parent != NULL) &&
4080 ((node->parent->type == XML_ELEMENT_NODE) ||
4081 (node->parent->type == XML_TEXT_NODE)))
4082 result = xmlGetLineNo(node->parent);
4083
4084 return result;
4085}
4086
Daniel Veillard652327a2003-09-29 18:02:38 +00004087#ifdef LIBXML_TREE_ENABLED
Daniel Veillard8faa7832001-11-26 15:58:08 +00004088/**
4089 * xmlGetNodePath:
4090 * @node: a node
4091 *
4092 * Build a structure based Path for the given node
4093 *
4094 * Returns the new path or NULL in case of error. The caller must free
4095 * the returned string
4096 */
4097xmlChar *
4098xmlGetNodePath(xmlNodePtr node)
4099{
4100 xmlNodePtr cur, tmp, next;
4101 xmlChar *buffer = NULL, *temp;
4102 size_t buf_len;
4103 xmlChar *buf;
Daniel Veillard96c3a3b2002-10-14 15:39:04 +00004104 const char *sep;
Daniel Veillard8faa7832001-11-26 15:58:08 +00004105 const char *name;
4106 char nametemp[100];
4107 int occur = 0;
4108
4109 if (node == NULL)
4110 return (NULL);
4111
4112 buf_len = 500;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004113 buffer = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004114 if (buffer == NULL) {
4115 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004116 return (NULL);
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004117 }
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004118 buf = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
Daniel Veillard8faa7832001-11-26 15:58:08 +00004119 if (buf == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004120 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004121 xmlFree(buffer);
4122 return (NULL);
4123 }
4124
4125 buffer[0] = 0;
4126 cur = node;
4127 do {
4128 name = "";
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004129 sep = "?";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004130 occur = 0;
4131 if ((cur->type == XML_DOCUMENT_NODE) ||
4132 (cur->type == XML_HTML_DOCUMENT_NODE)) {
4133 if (buffer[0] == '/')
4134 break;
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004135 sep = "/";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004136 next = NULL;
4137 } else if (cur->type == XML_ELEMENT_NODE) {
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004138 sep = "/";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004139 name = (const char *) cur->name;
4140 if (cur->ns) {
4141 snprintf(nametemp, sizeof(nametemp) - 1,
4142 "%s:%s", cur->ns->prefix, cur->name);
4143 nametemp[sizeof(nametemp) - 1] = 0;
4144 name = nametemp;
4145 }
4146 next = cur->parent;
4147
4148 /*
4149 * Thumbler index computation
Daniel Veillardc00cda82003-04-07 10:22:39 +00004150 * TODO: the ocurence test seems bogus for namespaced names
Daniel Veillard8faa7832001-11-26 15:58:08 +00004151 */
4152 tmp = cur->prev;
4153 while (tmp != NULL) {
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00004154 if ((tmp->type == XML_ELEMENT_NODE) &&
4155 (xmlStrEqual(cur->name, tmp->name)))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004156 occur++;
4157 tmp = tmp->prev;
4158 }
4159 if (occur == 0) {
4160 tmp = cur->next;
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004161 while (tmp != NULL && occur == 0) {
4162 if ((tmp->type == XML_ELEMENT_NODE) &&
4163 (xmlStrEqual(cur->name, tmp->name)))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004164 occur++;
4165 tmp = tmp->next;
4166 }
4167 if (occur != 0)
4168 occur = 1;
4169 } else
4170 occur++;
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004171 } else if (cur->type == XML_COMMENT_NODE) {
4172 sep = "/";
4173 name = "comment()";
4174 next = cur->parent;
4175
4176 /*
4177 * Thumbler index computation
4178 */
4179 tmp = cur->prev;
4180 while (tmp != NULL) {
4181 if (tmp->type == XML_COMMENT_NODE)
4182 occur++;
4183 tmp = tmp->prev;
4184 }
4185 if (occur == 0) {
4186 tmp = cur->next;
4187 while (tmp != NULL && occur == 0) {
4188 if (tmp->type == XML_COMMENT_NODE)
4189 occur++;
4190 tmp = tmp->next;
4191 }
4192 if (occur != 0)
4193 occur = 1;
4194 } else
4195 occur++;
4196 } else if ((cur->type == XML_TEXT_NODE) ||
4197 (cur->type == XML_CDATA_SECTION_NODE)) {
4198 sep = "/";
4199 name = "text()";
4200 next = cur->parent;
4201
4202 /*
4203 * Thumbler index computation
4204 */
4205 tmp = cur->prev;
4206 while (tmp != NULL) {
4207 if ((cur->type == XML_TEXT_NODE) ||
4208 (cur->type == XML_CDATA_SECTION_NODE))
4209 occur++;
4210 tmp = tmp->prev;
4211 }
4212 if (occur == 0) {
4213 tmp = cur->next;
4214 while (tmp != NULL && occur == 0) {
4215 if ((cur->type == XML_TEXT_NODE) ||
4216 (cur->type == XML_CDATA_SECTION_NODE))
4217 occur++;
4218 tmp = tmp->next;
4219 }
4220 if (occur != 0)
4221 occur = 1;
4222 } else
4223 occur++;
4224 } else if (cur->type == XML_PI_NODE) {
4225 sep = "/";
4226 snprintf(nametemp, sizeof(nametemp) - 1,
4227 "processing-instruction('%s')", cur->name);
4228 nametemp[sizeof(nametemp) - 1] = 0;
4229 name = nametemp;
4230
4231 next = cur->parent;
4232
4233 /*
4234 * Thumbler index computation
4235 */
4236 tmp = cur->prev;
4237 while (tmp != NULL) {
4238 if ((tmp->type == XML_PI_NODE) &&
4239 (xmlStrEqual(cur->name, tmp->name)))
4240 occur++;
4241 tmp = tmp->prev;
4242 }
4243 if (occur == 0) {
4244 tmp = cur->next;
4245 while (tmp != NULL && occur == 0) {
4246 if ((tmp->type == XML_PI_NODE) &&
4247 (xmlStrEqual(cur->name, tmp->name)))
4248 occur++;
4249 tmp = tmp->next;
4250 }
4251 if (occur != 0)
4252 occur = 1;
4253 } else
4254 occur++;
4255
Daniel Veillard8faa7832001-11-26 15:58:08 +00004256 } else if (cur->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004257 sep = "/@";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004258 name = (const char *) (((xmlAttrPtr) cur)->name);
4259 next = ((xmlAttrPtr) cur)->parent;
4260 } else {
4261 next = cur->parent;
4262 }
4263
4264 /*
4265 * Make sure there is enough room
4266 */
4267 if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) {
4268 buf_len =
4269 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20;
4270 temp = (xmlChar *) xmlRealloc(buffer, buf_len);
4271 if (temp == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004272 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004273 xmlFree(buf);
4274 xmlFree(buffer);
4275 return (NULL);
4276 }
4277 buffer = temp;
4278 temp = (xmlChar *) xmlRealloc(buf, buf_len);
4279 if (temp == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004280 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004281 xmlFree(buf);
4282 xmlFree(buffer);
4283 return (NULL);
4284 }
4285 buf = temp;
4286 }
4287 if (occur == 0)
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004288 snprintf((char *) buf, buf_len, "%s%s%s",
Daniel Veillard8faa7832001-11-26 15:58:08 +00004289 sep, name, (char *) buffer);
4290 else
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004291 snprintf((char *) buf, buf_len, "%s%s[%d]%s",
Daniel Veillard8faa7832001-11-26 15:58:08 +00004292 sep, name, occur, (char *) buffer);
4293 snprintf((char *) buffer, buf_len, "%s", buf);
4294 cur = next;
4295 } while (cur != NULL);
4296 xmlFree(buf);
4297 return (buffer);
4298}
Daniel Veillard652327a2003-09-29 18:02:38 +00004299#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard8faa7832001-11-26 15:58:08 +00004300
4301/**
Owen Taylor3473f882001-02-23 17:55:21 +00004302 * xmlDocGetRootElement:
4303 * @doc: the document
4304 *
4305 * Get the root element of the document (doc->children is a list
4306 * containing possibly comments, PIs, etc ...).
4307 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004308 * Returns the #xmlNodePtr for the root or NULL
Owen Taylor3473f882001-02-23 17:55:21 +00004309 */
4310xmlNodePtr
4311xmlDocGetRootElement(xmlDocPtr doc) {
4312 xmlNodePtr ret;
4313
4314 if (doc == NULL) return(NULL);
4315 ret = doc->children;
4316 while (ret != NULL) {
4317 if (ret->type == XML_ELEMENT_NODE)
4318 return(ret);
4319 ret = ret->next;
4320 }
4321 return(ret);
4322}
4323
Daniel Veillard652327a2003-09-29 18:02:38 +00004324#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00004325/**
4326 * xmlDocSetRootElement:
4327 * @doc: the document
4328 * @root: the new document root element
4329 *
4330 * Set the root element of the document (doc->children is a list
4331 * containing possibly comments, PIs, etc ...).
4332 *
4333 * Returns the old root element if any was found
4334 */
4335xmlNodePtr
4336xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) {
4337 xmlNodePtr old = NULL;
4338
4339 if (doc == NULL) return(NULL);
Daniel Veillardc575b992002-02-08 13:28:40 +00004340 if (root == NULL)
4341 return(NULL);
4342 xmlUnlinkNode(root);
4343 root->doc = doc;
4344 root->parent = (xmlNodePtr) doc;
Owen Taylor3473f882001-02-23 17:55:21 +00004345 old = doc->children;
4346 while (old != NULL) {
4347 if (old->type == XML_ELEMENT_NODE)
4348 break;
4349 old = old->next;
4350 }
4351 if (old == NULL) {
4352 if (doc->children == NULL) {
4353 doc->children = root;
4354 doc->last = root;
4355 } else {
4356 xmlAddSibling(doc->children, root);
4357 }
4358 } else {
4359 xmlReplaceNode(old, root);
4360 }
4361 return(old);
4362}
4363
4364/**
4365 * xmlNodeSetLang:
4366 * @cur: the node being changed
Daniel Veillardd1640922001-12-17 15:30:10 +00004367 * @lang: the language description
Owen Taylor3473f882001-02-23 17:55:21 +00004368 *
4369 * Set the language of a node, i.e. the values of the xml:lang
4370 * attribute.
4371 */
4372void
4373xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004374 xmlNsPtr ns;
4375
Owen Taylor3473f882001-02-23 17:55:21 +00004376 if (cur == NULL) return;
4377 switch(cur->type) {
4378 case XML_TEXT_NODE:
4379 case XML_CDATA_SECTION_NODE:
4380 case XML_COMMENT_NODE:
4381 case XML_DOCUMENT_NODE:
4382 case XML_DOCUMENT_TYPE_NODE:
4383 case XML_DOCUMENT_FRAG_NODE:
4384 case XML_NOTATION_NODE:
4385 case XML_HTML_DOCUMENT_NODE:
4386 case XML_DTD_NODE:
4387 case XML_ELEMENT_DECL:
4388 case XML_ATTRIBUTE_DECL:
4389 case XML_ENTITY_DECL:
4390 case XML_PI_NODE:
4391 case XML_ENTITY_REF_NODE:
4392 case XML_ENTITY_NODE:
4393 case XML_NAMESPACE_DECL:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004394#ifdef LIBXML_DOCB_ENABLED
4395 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004396#endif
4397 case XML_XINCLUDE_START:
4398 case XML_XINCLUDE_END:
4399 return;
4400 case XML_ELEMENT_NODE:
4401 case XML_ATTRIBUTE_NODE:
4402 break;
4403 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004404 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4405 if (ns == NULL)
4406 return;
4407 xmlSetNsProp(cur, ns, BAD_CAST "lang", lang);
Owen Taylor3473f882001-02-23 17:55:21 +00004408}
Daniel Veillard652327a2003-09-29 18:02:38 +00004409#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004410
4411/**
4412 * xmlNodeGetLang:
4413 * @cur: the node being checked
4414 *
4415 * Searches the language of a node, i.e. the values of the xml:lang
4416 * attribute or the one carried by the nearest ancestor.
4417 *
4418 * Returns a pointer to the lang value, or NULL if not found
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004419 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00004420 */
4421xmlChar *
4422xmlNodeGetLang(xmlNodePtr cur) {
4423 xmlChar *lang;
4424
4425 while (cur != NULL) {
Daniel Veillardc17337c2001-05-09 10:51:31 +00004426 lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE);
Owen Taylor3473f882001-02-23 17:55:21 +00004427 if (lang != NULL)
4428 return(lang);
4429 cur = cur->parent;
4430 }
4431 return(NULL);
4432}
4433
4434
Daniel Veillard652327a2003-09-29 18:02:38 +00004435#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00004436/**
4437 * xmlNodeSetSpacePreserve:
4438 * @cur: the node being changed
4439 * @val: the xml:space value ("0": default, 1: "preserve")
4440 *
4441 * Set (or reset) the space preserving behaviour of a node, i.e. the
4442 * value of the xml:space attribute.
4443 */
4444void
4445xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004446 xmlNsPtr ns;
4447
Owen Taylor3473f882001-02-23 17:55:21 +00004448 if (cur == NULL) return;
4449 switch(cur->type) {
4450 case XML_TEXT_NODE:
4451 case XML_CDATA_SECTION_NODE:
4452 case XML_COMMENT_NODE:
4453 case XML_DOCUMENT_NODE:
4454 case XML_DOCUMENT_TYPE_NODE:
4455 case XML_DOCUMENT_FRAG_NODE:
4456 case XML_NOTATION_NODE:
4457 case XML_HTML_DOCUMENT_NODE:
4458 case XML_DTD_NODE:
4459 case XML_ELEMENT_DECL:
4460 case XML_ATTRIBUTE_DECL:
4461 case XML_ENTITY_DECL:
4462 case XML_PI_NODE:
4463 case XML_ENTITY_REF_NODE:
4464 case XML_ENTITY_NODE:
4465 case XML_NAMESPACE_DECL:
4466 case XML_XINCLUDE_START:
4467 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004468#ifdef LIBXML_DOCB_ENABLED
4469 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004470#endif
4471 return;
4472 case XML_ELEMENT_NODE:
4473 case XML_ATTRIBUTE_NODE:
4474 break;
4475 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004476 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4477 if (ns == NULL)
4478 return;
Owen Taylor3473f882001-02-23 17:55:21 +00004479 switch (val) {
4480 case 0:
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004481 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "default");
Owen Taylor3473f882001-02-23 17:55:21 +00004482 break;
4483 case 1:
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004484 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "preserve");
Owen Taylor3473f882001-02-23 17:55:21 +00004485 break;
4486 }
4487}
Daniel Veillard652327a2003-09-29 18:02:38 +00004488#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004489
4490/**
4491 * xmlNodeGetSpacePreserve:
4492 * @cur: the node being checked
4493 *
4494 * Searches the space preserving behaviour of a node, i.e. the values
4495 * of the xml:space attribute or the one carried by the nearest
4496 * ancestor.
4497 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004498 * Returns -1 if xml:space is not inherited, 0 if "default", 1 if "preserve"
Owen Taylor3473f882001-02-23 17:55:21 +00004499 */
4500int
4501xmlNodeGetSpacePreserve(xmlNodePtr cur) {
4502 xmlChar *space;
4503
4504 while (cur != NULL) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004505 space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE);
Owen Taylor3473f882001-02-23 17:55:21 +00004506 if (space != NULL) {
4507 if (xmlStrEqual(space, BAD_CAST "preserve")) {
4508 xmlFree(space);
4509 return(1);
4510 }
4511 if (xmlStrEqual(space, BAD_CAST "default")) {
4512 xmlFree(space);
4513 return(0);
4514 }
4515 xmlFree(space);
4516 }
4517 cur = cur->parent;
4518 }
4519 return(-1);
4520}
4521
Daniel Veillard652327a2003-09-29 18:02:38 +00004522#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00004523/**
4524 * xmlNodeSetName:
4525 * @cur: the node being changed
4526 * @name: the new tag name
4527 *
4528 * Set (or reset) the name of a node.
4529 */
4530void
4531xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) {
4532 if (cur == NULL) return;
4533 if (name == NULL) return;
4534 switch(cur->type) {
4535 case XML_TEXT_NODE:
4536 case XML_CDATA_SECTION_NODE:
4537 case XML_COMMENT_NODE:
4538 case XML_DOCUMENT_TYPE_NODE:
4539 case XML_DOCUMENT_FRAG_NODE:
4540 case XML_NOTATION_NODE:
4541 case XML_HTML_DOCUMENT_NODE:
4542 case XML_NAMESPACE_DECL:
4543 case XML_XINCLUDE_START:
4544 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004545#ifdef LIBXML_DOCB_ENABLED
4546 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004547#endif
4548 return;
4549 case XML_ELEMENT_NODE:
4550 case XML_ATTRIBUTE_NODE:
4551 case XML_PI_NODE:
4552 case XML_ENTITY_REF_NODE:
4553 case XML_ENTITY_NODE:
4554 case XML_DTD_NODE:
4555 case XML_DOCUMENT_NODE:
4556 case XML_ELEMENT_DECL:
4557 case XML_ATTRIBUTE_DECL:
4558 case XML_ENTITY_DECL:
4559 break;
4560 }
4561 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
4562 cur->name = xmlStrdup(name);
4563}
4564
4565/**
4566 * xmlNodeSetBase:
4567 * @cur: the node being changed
4568 * @uri: the new base URI
4569 *
4570 * Set (or reset) the base URI of a node, i.e. the value of the
4571 * xml:base attribute.
4572 */
4573void
Daniel Veillardf85ce8e2003-09-22 10:24:45 +00004574xmlNodeSetBase(xmlNodePtr cur, const xmlChar* uri) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004575 xmlNsPtr ns;
4576
Owen Taylor3473f882001-02-23 17:55:21 +00004577 if (cur == NULL) return;
4578 switch(cur->type) {
4579 case XML_TEXT_NODE:
4580 case XML_CDATA_SECTION_NODE:
4581 case XML_COMMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004582 case XML_DOCUMENT_TYPE_NODE:
4583 case XML_DOCUMENT_FRAG_NODE:
4584 case XML_NOTATION_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004585 case XML_DTD_NODE:
4586 case XML_ELEMENT_DECL:
4587 case XML_ATTRIBUTE_DECL:
4588 case XML_ENTITY_DECL:
4589 case XML_PI_NODE:
4590 case XML_ENTITY_REF_NODE:
4591 case XML_ENTITY_NODE:
4592 case XML_NAMESPACE_DECL:
4593 case XML_XINCLUDE_START:
4594 case XML_XINCLUDE_END:
Owen Taylor3473f882001-02-23 17:55:21 +00004595 return;
4596 case XML_ELEMENT_NODE:
4597 case XML_ATTRIBUTE_NODE:
4598 break;
Daniel Veillard4cbe4702002-05-05 06:57:27 +00004599 case XML_DOCUMENT_NODE:
4600#ifdef LIBXML_DOCB_ENABLED
4601 case XML_DOCB_DOCUMENT_NODE:
4602#endif
4603 case XML_HTML_DOCUMENT_NODE: {
4604 xmlDocPtr doc = (xmlDocPtr) cur;
4605
4606 if (doc->URL != NULL)
4607 xmlFree((xmlChar *) doc->URL);
4608 if (uri == NULL)
4609 doc->URL = NULL;
4610 else
4611 doc->URL = xmlStrdup(uri);
4612 return;
4613 }
Owen Taylor3473f882001-02-23 17:55:21 +00004614 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004615
4616 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4617 if (ns == NULL)
4618 return;
4619 xmlSetNsProp(cur, ns, BAD_CAST "base", uri);
Owen Taylor3473f882001-02-23 17:55:21 +00004620}
Daniel Veillard652327a2003-09-29 18:02:38 +00004621#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004622
4623/**
Owen Taylor3473f882001-02-23 17:55:21 +00004624 * xmlNodeGetBase:
4625 * @doc: the document the node pertains to
4626 * @cur: the node being checked
4627 *
4628 * Searches for the BASE URL. The code should work on both XML
4629 * and HTML document even if base mechanisms are completely different.
4630 * It returns the base as defined in RFC 2396 sections
4631 * 5.1.1. Base URI within Document Content
4632 * and
4633 * 5.1.2. Base URI from the Encapsulating Entity
4634 * However it does not return the document base (5.1.3), use
4635 * xmlDocumentGetBase() for this
4636 *
4637 * Returns a pointer to the base URL, or NULL if not found
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004638 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00004639 */
4640xmlChar *
4641xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00004642 xmlChar *oldbase = NULL;
4643 xmlChar *base, *newbase;
Owen Taylor3473f882001-02-23 17:55:21 +00004644
4645 if ((cur == NULL) && (doc == NULL))
4646 return(NULL);
4647 if (doc == NULL) doc = cur->doc;
4648 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
4649 cur = doc->children;
4650 while ((cur != NULL) && (cur->name != NULL)) {
4651 if (cur->type != XML_ELEMENT_NODE) {
4652 cur = cur->next;
4653 continue;
4654 }
4655 if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) {
4656 cur = cur->children;
4657 continue;
4658 }
4659 if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) {
4660 cur = cur->children;
4661 continue;
4662 }
4663 if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) {
4664 return(xmlGetProp(cur, BAD_CAST "href"));
4665 }
4666 cur = cur->next;
4667 }
4668 return(NULL);
4669 }
4670 while (cur != NULL) {
4671 if (cur->type == XML_ENTITY_DECL) {
4672 xmlEntityPtr ent = (xmlEntityPtr) cur;
4673 return(xmlStrdup(ent->URI));
4674 }
Daniel Veillard42596ad2001-05-22 16:57:14 +00004675 if (cur->type == XML_ELEMENT_NODE) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00004676 base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE);
Daniel Veillard42596ad2001-05-22 16:57:14 +00004677 if (base != NULL) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00004678 if (oldbase != NULL) {
4679 newbase = xmlBuildURI(oldbase, base);
4680 if (newbase != NULL) {
4681 xmlFree(oldbase);
4682 xmlFree(base);
4683 oldbase = newbase;
4684 } else {
4685 xmlFree(oldbase);
4686 xmlFree(base);
4687 return(NULL);
4688 }
4689 } else {
4690 oldbase = base;
4691 }
4692 if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) ||
4693 (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) ||
4694 (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4)))
4695 return(oldbase);
Daniel Veillard42596ad2001-05-22 16:57:14 +00004696 }
4697 }
Owen Taylor3473f882001-02-23 17:55:21 +00004698 cur = cur->parent;
4699 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00004700 if ((doc != NULL) && (doc->URL != NULL)) {
4701 if (oldbase == NULL)
4702 return(xmlStrdup(doc->URL));
4703 newbase = xmlBuildURI(oldbase, doc->URL);
4704 xmlFree(oldbase);
4705 return(newbase);
4706 }
4707 return(oldbase);
Owen Taylor3473f882001-02-23 17:55:21 +00004708}
4709
4710/**
4711 * xmlNodeGetContent:
4712 * @cur: the node being read
4713 *
4714 * Read the value of a node, this can be either the text carried
4715 * directly by this node if it's a TEXT node or the aggregate string
4716 * of the values carried by this node child's (TEXT and ENTITY_REF).
Daniel Veillardd1640922001-12-17 15:30:10 +00004717 * Entity references are substituted.
4718 * Returns a new #xmlChar * or NULL if no content is available.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004719 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00004720 */
4721xmlChar *
Daniel Veillard7646b182002-04-20 06:41:40 +00004722xmlNodeGetContent(xmlNodePtr cur)
4723{
4724 if (cur == NULL)
4725 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004726 switch (cur->type) {
4727 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00004728 case XML_ELEMENT_NODE:{
4729 xmlNodePtr tmp = cur;
4730 xmlBufferPtr buffer;
4731 xmlChar *ret;
Owen Taylor3473f882001-02-23 17:55:21 +00004732
Daniel Veillard814a76d2003-01-23 18:24:20 +00004733 buffer = xmlBufferCreateSize(64);
Daniel Veillard7646b182002-04-20 06:41:40 +00004734 if (buffer == NULL)
4735 return (NULL);
4736 while (tmp != NULL) {
4737 switch (tmp->type) {
4738 case XML_CDATA_SECTION_NODE:
4739 case XML_TEXT_NODE:
4740 if (tmp->content != NULL)
4741 xmlBufferCat(buffer, tmp->content);
4742 break;
4743 case XML_ENTITY_REF_NODE:{
4744 /* recursive substitution of entity references */
4745 xmlChar *cont = xmlNodeGetContent(tmp);
Owen Taylor3473f882001-02-23 17:55:21 +00004746
Daniel Veillard7646b182002-04-20 06:41:40 +00004747 if (cont) {
4748 xmlBufferCat(buffer,
4749 (const xmlChar *) cont);
4750 xmlFree(cont);
4751 }
4752 break;
4753 }
4754 default:
4755 break;
4756 }
4757 /*
4758 * Skip to next node
4759 */
4760 if (tmp->children != NULL) {
4761 if (tmp->children->type != XML_ENTITY_DECL) {
4762 tmp = tmp->children;
4763 continue;
4764 }
4765 }
4766 if (tmp == cur)
4767 break;
Daniel Veillard6c831202001-03-07 15:57:53 +00004768
Daniel Veillard7646b182002-04-20 06:41:40 +00004769 if (tmp->next != NULL) {
4770 tmp = tmp->next;
4771 continue;
4772 }
4773
4774 do {
4775 tmp = tmp->parent;
4776 if (tmp == NULL)
4777 break;
4778 if (tmp == cur) {
4779 tmp = NULL;
4780 break;
4781 }
4782 if (tmp->next != NULL) {
4783 tmp = tmp->next;
4784 break;
4785 }
4786 } while (tmp != NULL);
4787 }
4788 ret = buffer->content;
4789 buffer->content = NULL;
4790 xmlBufferFree(buffer);
4791 return (ret);
4792 }
4793 case XML_ATTRIBUTE_NODE:{
4794 xmlAttrPtr attr = (xmlAttrPtr) cur;
4795
4796 if (attr->parent != NULL)
4797 return (xmlNodeListGetString
4798 (attr->parent->doc, attr->children, 1));
4799 else
4800 return (xmlNodeListGetString(NULL, attr->children, 1));
4801 break;
4802 }
Owen Taylor3473f882001-02-23 17:55:21 +00004803 case XML_COMMENT_NODE:
4804 case XML_PI_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00004805 if (cur->content != NULL)
4806 return (xmlStrdup(cur->content));
4807 return (NULL);
4808 case XML_ENTITY_REF_NODE:{
4809 xmlEntityPtr ent;
4810 xmlNodePtr tmp;
4811 xmlBufferPtr buffer;
4812 xmlChar *ret;
4813
4814 /* lookup entity declaration */
4815 ent = xmlGetDocEntity(cur->doc, cur->name);
4816 if (ent == NULL)
4817 return (NULL);
4818
4819 buffer = xmlBufferCreate();
4820 if (buffer == NULL)
4821 return (NULL);
4822
4823 /* an entity content can be any "well balanced chunk",
4824 * i.e. the result of the content [43] production:
4825 * http://www.w3.org/TR/REC-xml#NT-content
4826 * -> we iterate through child nodes and recursive call
4827 * xmlNodeGetContent() which handles all possible node types */
4828 tmp = ent->children;
4829 while (tmp) {
4830 xmlChar *cont = xmlNodeGetContent(tmp);
4831
4832 if (cont) {
4833 xmlBufferCat(buffer, (const xmlChar *) cont);
4834 xmlFree(cont);
4835 }
4836 tmp = tmp->next;
4837 }
4838
4839 ret = buffer->content;
4840 buffer->content = NULL;
4841 xmlBufferFree(buffer);
4842 return (ret);
4843 }
Owen Taylor3473f882001-02-23 17:55:21 +00004844 case XML_ENTITY_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004845 case XML_DOCUMENT_TYPE_NODE:
4846 case XML_NOTATION_NODE:
4847 case XML_DTD_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00004848 case XML_XINCLUDE_START:
4849 case XML_XINCLUDE_END:
Daniel Veillard9adc0462003-03-24 18:39:54 +00004850 return (NULL);
4851 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004852#ifdef LIBXML_DOCB_ENABLED
Daniel Veillard7646b182002-04-20 06:41:40 +00004853 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004854#endif
Daniel Veillard9adc0462003-03-24 18:39:54 +00004855 case XML_HTML_DOCUMENT_NODE: {
4856 xmlChar *tmp;
4857 xmlChar *res = NULL;
4858
4859 cur = cur->children;
4860 while (cur!= NULL) {
4861 if ((cur->type == XML_ELEMENT_NODE) ||
4862 (cur->type == XML_TEXT_NODE) ||
4863 (cur->type == XML_CDATA_SECTION_NODE)) {
4864 tmp = xmlNodeGetContent(cur);
4865 if (tmp != NULL) {
4866 if (res == NULL)
4867 res = tmp;
4868 else {
4869 res = xmlStrcat(res, tmp);
4870 xmlFree(tmp);
4871 }
4872 }
4873 }
4874 cur = cur->next;
4875 }
4876 return(res);
4877 }
Daniel Veillard96c3a3b2002-10-14 15:39:04 +00004878 case XML_NAMESPACE_DECL: {
4879 xmlChar *tmp;
4880
4881 tmp = xmlStrdup(((xmlNsPtr) cur)->href);
4882 return (tmp);
4883 }
Owen Taylor3473f882001-02-23 17:55:21 +00004884 case XML_ELEMENT_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00004885 /* TODO !!! */
4886 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004887 case XML_ATTRIBUTE_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00004888 /* TODO !!! */
4889 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004890 case XML_ENTITY_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00004891 /* TODO !!! */
4892 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004893 case XML_CDATA_SECTION_NODE:
4894 case XML_TEXT_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00004895 if (cur->content != NULL)
4896 return (xmlStrdup(cur->content));
4897 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004898 }
Daniel Veillard7646b182002-04-20 06:41:40 +00004899 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004900}
Daniel Veillard652327a2003-09-29 18:02:38 +00004901
Owen Taylor3473f882001-02-23 17:55:21 +00004902/**
4903 * xmlNodeSetContent:
4904 * @cur: the node being modified
4905 * @content: the new value of the content
4906 *
4907 * Replace the content of a node.
4908 */
4909void
4910xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) {
4911 if (cur == NULL) {
4912#ifdef DEBUG_TREE
4913 xmlGenericError(xmlGenericErrorContext,
4914 "xmlNodeSetContent : node == NULL\n");
4915#endif
4916 return;
4917 }
4918 switch (cur->type) {
4919 case XML_DOCUMENT_FRAG_NODE:
4920 case XML_ELEMENT_NODE:
Daniel Veillard2c748c62002-01-16 15:37:50 +00004921 case XML_ATTRIBUTE_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004922 if (cur->children != NULL) xmlFreeNodeList(cur->children);
4923 cur->children = xmlStringGetNodeList(cur->doc, content);
4924 UPDATE_LAST_CHILD_AND_PARENT(cur)
4925 break;
Owen Taylor3473f882001-02-23 17:55:21 +00004926 case XML_TEXT_NODE:
4927 case XML_CDATA_SECTION_NODE:
4928 case XML_ENTITY_REF_NODE:
4929 case XML_ENTITY_NODE:
4930 case XML_PI_NODE:
4931 case XML_COMMENT_NODE:
4932 if (cur->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004933 xmlFree(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00004934 }
4935 if (cur->children != NULL) xmlFreeNodeList(cur->children);
4936 cur->last = cur->children = NULL;
4937 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004938 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00004939 } else
4940 cur->content = NULL;
4941 break;
4942 case XML_DOCUMENT_NODE:
4943 case XML_HTML_DOCUMENT_NODE:
4944 case XML_DOCUMENT_TYPE_NODE:
4945 case XML_XINCLUDE_START:
4946 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004947#ifdef LIBXML_DOCB_ENABLED
4948 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004949#endif
4950 break;
4951 case XML_NOTATION_NODE:
4952 break;
4953 case XML_DTD_NODE:
4954 break;
4955 case XML_NAMESPACE_DECL:
4956 break;
4957 case XML_ELEMENT_DECL:
4958 /* TODO !!! */
4959 break;
4960 case XML_ATTRIBUTE_DECL:
4961 /* TODO !!! */
4962 break;
4963 case XML_ENTITY_DECL:
4964 /* TODO !!! */
4965 break;
4966 }
4967}
4968
Daniel Veillard652327a2003-09-29 18:02:38 +00004969#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00004970/**
4971 * xmlNodeSetContentLen:
4972 * @cur: the node being modified
4973 * @content: the new value of the content
4974 * @len: the size of @content
4975 *
4976 * Replace the content of a node.
4977 */
4978void
4979xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
4980 if (cur == NULL) {
4981#ifdef DEBUG_TREE
4982 xmlGenericError(xmlGenericErrorContext,
4983 "xmlNodeSetContentLen : node == NULL\n");
4984#endif
4985 return;
4986 }
4987 switch (cur->type) {
4988 case XML_DOCUMENT_FRAG_NODE:
4989 case XML_ELEMENT_NODE:
Daniel Veillard2c748c62002-01-16 15:37:50 +00004990 case XML_ATTRIBUTE_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004991 if (cur->children != NULL) xmlFreeNodeList(cur->children);
4992 cur->children = xmlStringLenGetNodeList(cur->doc, content, len);
4993 UPDATE_LAST_CHILD_AND_PARENT(cur)
4994 break;
Owen Taylor3473f882001-02-23 17:55:21 +00004995 case XML_TEXT_NODE:
4996 case XML_CDATA_SECTION_NODE:
4997 case XML_ENTITY_REF_NODE:
4998 case XML_ENTITY_NODE:
4999 case XML_PI_NODE:
5000 case XML_COMMENT_NODE:
5001 case XML_NOTATION_NODE:
5002 if (cur->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005003 xmlFree(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00005004 }
5005 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5006 cur->children = cur->last = NULL;
5007 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005008 cur->content = xmlStrndup(content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00005009 } else
5010 cur->content = NULL;
5011 break;
5012 case XML_DOCUMENT_NODE:
5013 case XML_DTD_NODE:
5014 case XML_HTML_DOCUMENT_NODE:
5015 case XML_DOCUMENT_TYPE_NODE:
5016 case XML_NAMESPACE_DECL:
5017 case XML_XINCLUDE_START:
5018 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005019#ifdef LIBXML_DOCB_ENABLED
5020 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005021#endif
5022 break;
5023 case XML_ELEMENT_DECL:
5024 /* TODO !!! */
5025 break;
5026 case XML_ATTRIBUTE_DECL:
5027 /* TODO !!! */
5028 break;
5029 case XML_ENTITY_DECL:
5030 /* TODO !!! */
5031 break;
5032 }
5033}
Daniel Veillard652327a2003-09-29 18:02:38 +00005034#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005035
5036/**
5037 * xmlNodeAddContentLen:
5038 * @cur: the node being modified
5039 * @content: extra content
5040 * @len: the size of @content
5041 *
5042 * Append the extra substring to the node content.
5043 */
5044void
5045xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5046 if (cur == NULL) {
5047#ifdef DEBUG_TREE
5048 xmlGenericError(xmlGenericErrorContext,
5049 "xmlNodeAddContentLen : node == NULL\n");
5050#endif
5051 return;
5052 }
5053 if (len <= 0) return;
5054 switch (cur->type) {
5055 case XML_DOCUMENT_FRAG_NODE:
5056 case XML_ELEMENT_NODE: {
Daniel Veillardacb2bda2002-01-13 16:15:43 +00005057 xmlNodePtr last, newNode, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00005058
Daniel Veillard7db37732001-07-12 01:20:08 +00005059 last = cur->last;
Owen Taylor3473f882001-02-23 17:55:21 +00005060 newNode = xmlNewTextLen(content, len);
5061 if (newNode != NULL) {
Daniel Veillardacb2bda2002-01-13 16:15:43 +00005062 tmp = xmlAddChild(cur, newNode);
5063 if (tmp != newNode)
5064 return;
Owen Taylor3473f882001-02-23 17:55:21 +00005065 if ((last != NULL) && (last->next == newNode)) {
5066 xmlTextMerge(last, newNode);
5067 }
5068 }
5069 break;
5070 }
5071 case XML_ATTRIBUTE_NODE:
5072 break;
5073 case XML_TEXT_NODE:
5074 case XML_CDATA_SECTION_NODE:
5075 case XML_ENTITY_REF_NODE:
5076 case XML_ENTITY_NODE:
5077 case XML_PI_NODE:
5078 case XML_COMMENT_NODE:
5079 case XML_NOTATION_NODE:
5080 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005081 cur->content = xmlStrncat(cur->content, content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00005082 }
5083 case XML_DOCUMENT_NODE:
5084 case XML_DTD_NODE:
5085 case XML_HTML_DOCUMENT_NODE:
5086 case XML_DOCUMENT_TYPE_NODE:
5087 case XML_NAMESPACE_DECL:
5088 case XML_XINCLUDE_START:
5089 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005090#ifdef LIBXML_DOCB_ENABLED
5091 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005092#endif
5093 break;
5094 case XML_ELEMENT_DECL:
5095 case XML_ATTRIBUTE_DECL:
5096 case XML_ENTITY_DECL:
5097 break;
5098 }
5099}
5100
5101/**
5102 * xmlNodeAddContent:
5103 * @cur: the node being modified
5104 * @content: extra content
5105 *
5106 * Append the extra substring to the node content.
5107 */
5108void
5109xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) {
5110 int len;
5111
5112 if (cur == NULL) {
5113#ifdef DEBUG_TREE
5114 xmlGenericError(xmlGenericErrorContext,
5115 "xmlNodeAddContent : node == NULL\n");
5116#endif
5117 return;
5118 }
5119 if (content == NULL) return;
5120 len = xmlStrlen(content);
5121 xmlNodeAddContentLen(cur, content, len);
5122}
5123
5124/**
5125 * xmlTextMerge:
5126 * @first: the first text node
5127 * @second: the second text node being merged
5128 *
5129 * Merge two text nodes into one
5130 * Returns the first text node augmented
5131 */
5132xmlNodePtr
5133xmlTextMerge(xmlNodePtr first, xmlNodePtr second) {
5134 if (first == NULL) return(second);
5135 if (second == NULL) return(first);
5136 if (first->type != XML_TEXT_NODE) return(first);
5137 if (second->type != XML_TEXT_NODE) return(first);
5138 if (second->name != first->name)
5139 return(first);
Owen Taylor3473f882001-02-23 17:55:21 +00005140 xmlNodeAddContent(first, second->content);
Owen Taylor3473f882001-02-23 17:55:21 +00005141 xmlUnlinkNode(second);
5142 xmlFreeNode(second);
5143 return(first);
5144}
5145
Daniel Veillard652327a2003-09-29 18:02:38 +00005146#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00005147/**
5148 * xmlGetNsList:
5149 * @doc: the document
5150 * @node: the current node
5151 *
5152 * Search all the namespace applying to a given element.
Daniel Veillardd1640922001-12-17 15:30:10 +00005153 * Returns an NULL terminated array of all the #xmlNsPtr found
Owen Taylor3473f882001-02-23 17:55:21 +00005154 * that need to be freed by the caller or NULL if no
5155 * namespace if defined
5156 */
5157xmlNsPtr *
Daniel Veillard77044732001-06-29 21:31:07 +00005158xmlGetNsList(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node)
5159{
Owen Taylor3473f882001-02-23 17:55:21 +00005160 xmlNsPtr cur;
5161 xmlNsPtr *ret = NULL;
5162 int nbns = 0;
5163 int maxns = 10;
5164 int i;
5165
5166 while (node != NULL) {
Daniel Veillard77044732001-06-29 21:31:07 +00005167 if (node->type == XML_ELEMENT_NODE) {
5168 cur = node->nsDef;
5169 while (cur != NULL) {
5170 if (ret == NULL) {
5171 ret =
5172 (xmlNsPtr *) xmlMalloc((maxns + 1) *
5173 sizeof(xmlNsPtr));
5174 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005175 xmlTreeErrMemory("getting namespace list");
Daniel Veillard77044732001-06-29 21:31:07 +00005176 return (NULL);
5177 }
5178 ret[nbns] = NULL;
5179 }
5180 for (i = 0; i < nbns; i++) {
5181 if ((cur->prefix == ret[i]->prefix) ||
5182 (xmlStrEqual(cur->prefix, ret[i]->prefix)))
5183 break;
5184 }
5185 if (i >= nbns) {
5186 if (nbns >= maxns) {
5187 maxns *= 2;
5188 ret = (xmlNsPtr *) xmlRealloc(ret,
5189 (maxns +
5190 1) *
5191 sizeof(xmlNsPtr));
5192 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005193 xmlTreeErrMemory("getting namespace list");
Daniel Veillard77044732001-06-29 21:31:07 +00005194 return (NULL);
5195 }
5196 }
5197 ret[nbns++] = cur;
5198 ret[nbns] = NULL;
5199 }
Owen Taylor3473f882001-02-23 17:55:21 +00005200
Daniel Veillard77044732001-06-29 21:31:07 +00005201 cur = cur->next;
5202 }
5203 }
5204 node = node->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00005205 }
Daniel Veillard77044732001-06-29 21:31:07 +00005206 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00005207}
Daniel Veillard652327a2003-09-29 18:02:38 +00005208#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005209
5210/**
5211 * xmlSearchNs:
5212 * @doc: the document
5213 * @node: the current node
Daniel Veillard77851712001-02-27 21:54:07 +00005214 * @nameSpace: the namespace prefix
Owen Taylor3473f882001-02-23 17:55:21 +00005215 *
5216 * Search a Ns registered under a given name space for a document.
5217 * recurse on the parents until it finds the defined namespace
5218 * or return NULL otherwise.
5219 * @nameSpace can be NULL, this is a search for the default namespace.
5220 * We don't allow to cross entities boundaries. If you don't declare
5221 * the namespace within those you will be in troubles !!! A warning
5222 * is generated to cover this case.
5223 *
5224 * Returns the namespace pointer or NULL.
5225 */
5226xmlNsPtr
5227xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) {
5228 xmlNsPtr cur;
5229
5230 if (node == NULL) return(NULL);
5231 if ((nameSpace != NULL) &&
5232 (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) {
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00005233 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
5234 /*
5235 * The XML-1.0 namespace is normally held on the root
5236 * element. In this case exceptionally create it on the
5237 * node element.
5238 */
5239 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5240 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005241 xmlTreeErrMemory("searching namespace");
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00005242 return(NULL);
5243 }
5244 memset(cur, 0, sizeof(xmlNs));
5245 cur->type = XML_LOCAL_NAMESPACE;
5246 cur->href = xmlStrdup(XML_XML_NAMESPACE);
5247 cur->prefix = xmlStrdup((const xmlChar *)"xml");
5248 cur->next = node->nsDef;
5249 node->nsDef = cur;
5250 return(cur);
5251 }
Owen Taylor3473f882001-02-23 17:55:21 +00005252 if (doc->oldNs == NULL) {
5253 /*
5254 * Allocate a new Namespace and fill the fields.
5255 */
5256 doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5257 if (doc->oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005258 xmlTreeErrMemory("searching namespace");
Owen Taylor3473f882001-02-23 17:55:21 +00005259 return(NULL);
5260 }
5261 memset(doc->oldNs, 0, sizeof(xmlNs));
5262 doc->oldNs->type = XML_LOCAL_NAMESPACE;
5263
5264 doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE);
5265 doc->oldNs->prefix = xmlStrdup((const xmlChar *)"xml");
5266 }
5267 return(doc->oldNs);
5268 }
5269 while (node != NULL) {
5270 if ((node->type == XML_ENTITY_REF_NODE) ||
5271 (node->type == XML_ENTITY_NODE) ||
5272 (node->type == XML_ENTITY_DECL))
5273 return(NULL);
5274 if (node->type == XML_ELEMENT_NODE) {
5275 cur = node->nsDef;
5276 while (cur != NULL) {
5277 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
5278 (cur->href != NULL))
5279 return(cur);
5280 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
5281 (cur->href != NULL) &&
5282 (xmlStrEqual(cur->prefix, nameSpace)))
5283 return(cur);
5284 cur = cur->next;
5285 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005286 cur = node->ns;
5287 if (cur != NULL) {
5288 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
5289 (cur->href != NULL))
5290 return(cur);
5291 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
5292 (cur->href != NULL) &&
5293 (xmlStrEqual(cur->prefix, nameSpace)))
5294 return(cur);
5295 }
Owen Taylor3473f882001-02-23 17:55:21 +00005296 }
5297 node = node->parent;
5298 }
5299 return(NULL);
5300}
5301
5302/**
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005303 * xmlNsInScope:
5304 * @doc: the document
5305 * @node: the current node
5306 * @ancestor: the ancestor carrying the namespace
5307 * @prefix: the namespace prefix
5308 *
5309 * Verify that the given namespace held on @ancestor is still in scope
5310 * on node.
5311 *
5312 * Returns 1 if true, 0 if false and -1 in case of error.
5313 */
5314static int
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005315xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node,
5316 xmlNodePtr ancestor, const xmlChar * prefix)
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005317{
5318 xmlNsPtr tst;
5319
5320 while ((node != NULL) && (node != ancestor)) {
5321 if ((node->type == XML_ENTITY_REF_NODE) ||
5322 (node->type == XML_ENTITY_NODE) ||
5323 (node->type == XML_ENTITY_DECL))
5324 return (-1);
5325 if (node->type == XML_ELEMENT_NODE) {
5326 tst = node->nsDef;
5327 while (tst != NULL) {
5328 if ((tst->prefix == NULL)
5329 && (prefix == NULL))
5330 return (0);
5331 if ((tst->prefix != NULL)
5332 && (prefix != NULL)
5333 && (xmlStrEqual(tst->prefix, prefix)))
5334 return (0);
5335 tst = tst->next;
5336 }
5337 }
5338 node = node->parent;
5339 }
5340 if (node != ancestor)
5341 return (-1);
5342 return (1);
5343}
5344
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005345/**
Owen Taylor3473f882001-02-23 17:55:21 +00005346 * xmlSearchNsByHref:
5347 * @doc: the document
5348 * @node: the current node
5349 * @href: the namespace value
5350 *
5351 * Search a Ns aliasing a given URI. Recurse on the parents until it finds
5352 * the defined namespace or return NULL otherwise.
5353 * Returns the namespace pointer or NULL.
5354 */
5355xmlNsPtr
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005356xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href)
5357{
Owen Taylor3473f882001-02-23 17:55:21 +00005358 xmlNsPtr cur;
5359 xmlNodePtr orig = node;
5360
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005361 if ((node == NULL) || (href == NULL))
5362 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005363 if (xmlStrEqual(href, XML_XML_NAMESPACE)) {
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005364 /*
5365 * Only the document can hold the XML spec namespace.
5366 */
5367 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
5368 /*
5369 * The XML-1.0 namespace is normally held on the root
5370 * element. In this case exceptionally create it on the
5371 * node element.
5372 */
5373 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5374 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005375 xmlTreeErrMemory("searching namespace");
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005376 return (NULL);
5377 }
5378 memset(cur, 0, sizeof(xmlNs));
5379 cur->type = XML_LOCAL_NAMESPACE;
5380 cur->href = xmlStrdup(XML_XML_NAMESPACE);
5381 cur->prefix = xmlStrdup((const xmlChar *) "xml");
5382 cur->next = node->nsDef;
5383 node->nsDef = cur;
5384 return (cur);
5385 }
5386 if (doc->oldNs == NULL) {
5387 /*
5388 * Allocate a new Namespace and fill the fields.
5389 */
5390 doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5391 if (doc->oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005392 xmlTreeErrMemory("searching namespace");
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005393 return (NULL);
5394 }
5395 memset(doc->oldNs, 0, sizeof(xmlNs));
5396 doc->oldNs->type = XML_LOCAL_NAMESPACE;
Owen Taylor3473f882001-02-23 17:55:21 +00005397
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005398 doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE);
5399 doc->oldNs->prefix = xmlStrdup((const xmlChar *) "xml");
5400 }
5401 return (doc->oldNs);
Owen Taylor3473f882001-02-23 17:55:21 +00005402 }
5403 while (node != NULL) {
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005404 if ((node->type == XML_ENTITY_REF_NODE) ||
5405 (node->type == XML_ENTITY_NODE) ||
5406 (node->type == XML_ENTITY_DECL))
5407 return (NULL);
5408 if (node->type == XML_ELEMENT_NODE) {
5409 cur = node->nsDef;
5410 while (cur != NULL) {
5411 if ((cur->href != NULL) && (href != NULL) &&
5412 (xmlStrEqual(cur->href, href))) {
5413 if (xmlNsInScope(doc, orig, node, cur->href) == 1)
5414 return (cur);
5415 }
5416 cur = cur->next;
5417 }
5418 cur = node->ns;
5419 if (cur != NULL) {
5420 if ((cur->href != NULL) && (href != NULL) &&
5421 (xmlStrEqual(cur->href, href))) {
5422 if (xmlNsInScope(doc, orig, node, cur->href) == 1)
5423 return (cur);
5424 }
5425 }
5426 }
5427 node = node->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00005428 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005429 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005430}
5431
5432/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005433 * xmlNewReconciliedNs:
Owen Taylor3473f882001-02-23 17:55:21 +00005434 * @doc: the document
5435 * @tree: a node expected to hold the new namespace
5436 * @ns: the original namespace
5437 *
5438 * This function tries to locate a namespace definition in a tree
5439 * ancestors, or create a new namespace definition node similar to
5440 * @ns trying to reuse the same prefix. However if the given prefix is
5441 * null (default namespace) or reused within the subtree defined by
5442 * @tree or on one of its ancestors then a new prefix is generated.
5443 * Returns the (new) namespace definition or NULL in case of error
5444 */
5445xmlNsPtr
5446xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) {
5447 xmlNsPtr def;
5448 xmlChar prefix[50];
5449 int counter = 1;
5450
5451 if (tree == NULL) {
5452#ifdef DEBUG_TREE
5453 xmlGenericError(xmlGenericErrorContext,
5454 "xmlNewReconciliedNs : tree == NULL\n");
5455#endif
5456 return(NULL);
5457 }
5458 if (ns == NULL) {
5459#ifdef DEBUG_TREE
5460 xmlGenericError(xmlGenericErrorContext,
5461 "xmlNewReconciliedNs : ns == NULL\n");
5462#endif
5463 return(NULL);
5464 }
5465 /*
5466 * Search an existing namespace definition inherited.
5467 */
5468 def = xmlSearchNsByHref(doc, tree, ns->href);
5469 if (def != NULL)
5470 return(def);
5471
5472 /*
5473 * Find a close prefix which is not already in use.
5474 * Let's strip namespace prefixes longer than 20 chars !
5475 */
Daniel Veillardf742d342002-03-07 00:05:35 +00005476 if (ns->prefix == NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00005477 snprintf((char *) prefix, sizeof(prefix), "default");
Daniel Veillardf742d342002-03-07 00:05:35 +00005478 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00005479 snprintf((char *) prefix, sizeof(prefix), "%.20s", ns->prefix);
Daniel Veillardf742d342002-03-07 00:05:35 +00005480
Owen Taylor3473f882001-02-23 17:55:21 +00005481 def = xmlSearchNs(doc, tree, prefix);
5482 while (def != NULL) {
5483 if (counter > 1000) return(NULL);
Daniel Veillardf742d342002-03-07 00:05:35 +00005484 if (ns->prefix == NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00005485 snprintf((char *) prefix, sizeof(prefix), "default%d", counter++);
Daniel Veillardf742d342002-03-07 00:05:35 +00005486 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00005487 snprintf((char *) prefix, sizeof(prefix), "%.20s%d", ns->prefix, counter++);
Owen Taylor3473f882001-02-23 17:55:21 +00005488 def = xmlSearchNs(doc, tree, prefix);
5489 }
5490
5491 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00005492 * OK, now we are ready to create a new one.
Owen Taylor3473f882001-02-23 17:55:21 +00005493 */
5494 def = xmlNewNs(tree, ns->href, prefix);
5495 return(def);
5496}
5497
Daniel Veillard652327a2003-09-29 18:02:38 +00005498#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00005499/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005500 * xmlReconciliateNs:
Owen Taylor3473f882001-02-23 17:55:21 +00005501 * @doc: the document
5502 * @tree: a node defining the subtree to reconciliate
5503 *
5504 * This function checks that all the namespaces declared within the given
5505 * tree are properly declared. This is needed for example after Copy or Cut
5506 * and then paste operations. The subtree may still hold pointers to
5507 * namespace declarations outside the subtree or invalid/masked. As much
Daniel Veillardd1640922001-12-17 15:30:10 +00005508 * as possible the function try to reuse the existing namespaces found in
Owen Taylor3473f882001-02-23 17:55:21 +00005509 * the new environment. If not possible the new namespaces are redeclared
5510 * on @tree at the top of the given subtree.
5511 * Returns the number of namespace declarations created or -1 in case of error.
5512 */
5513int
5514xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) {
5515 xmlNsPtr *oldNs = NULL;
5516 xmlNsPtr *newNs = NULL;
5517 int sizeCache = 0;
5518 int nbCache = 0;
5519
5520 xmlNsPtr n;
5521 xmlNodePtr node = tree;
5522 xmlAttrPtr attr;
5523 int ret = 0, i;
5524
5525 while (node != NULL) {
5526 /*
5527 * Reconciliate the node namespace
5528 */
5529 if (node->ns != NULL) {
5530 /*
5531 * initialize the cache if needed
5532 */
5533 if (sizeCache == 0) {
5534 sizeCache = 10;
5535 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
5536 sizeof(xmlNsPtr));
5537 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005538 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005539 return(-1);
5540 }
5541 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
5542 sizeof(xmlNsPtr));
5543 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005544 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005545 xmlFree(oldNs);
5546 return(-1);
5547 }
5548 }
5549 for (i = 0;i < nbCache;i++) {
5550 if (oldNs[i] == node->ns) {
5551 node->ns = newNs[i];
5552 break;
5553 }
5554 }
5555 if (i == nbCache) {
5556 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00005557 * OK we need to recreate a new namespace definition
Owen Taylor3473f882001-02-23 17:55:21 +00005558 */
5559 n = xmlNewReconciliedNs(doc, tree, node->ns);
5560 if (n != NULL) { /* :-( what if else ??? */
5561 /*
5562 * check if we need to grow the cache buffers.
5563 */
5564 if (sizeCache <= nbCache) {
5565 sizeCache *= 2;
5566 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
5567 sizeof(xmlNsPtr));
5568 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005569 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005570 xmlFree(newNs);
5571 return(-1);
5572 }
5573 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
5574 sizeof(xmlNsPtr));
5575 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005576 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005577 xmlFree(oldNs);
5578 return(-1);
5579 }
5580 }
5581 newNs[nbCache] = n;
5582 oldNs[nbCache++] = node->ns;
5583 node->ns = n;
5584 }
5585 }
5586 }
5587 /*
5588 * now check for namespace hold by attributes on the node.
5589 */
5590 attr = node->properties;
5591 while (attr != NULL) {
5592 if (attr->ns != NULL) {
5593 /*
5594 * initialize the cache if needed
5595 */
5596 if (sizeCache == 0) {
5597 sizeCache = 10;
5598 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
5599 sizeof(xmlNsPtr));
5600 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005601 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005602 return(-1);
5603 }
5604 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
5605 sizeof(xmlNsPtr));
5606 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005607 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005608 xmlFree(oldNs);
5609 return(-1);
5610 }
5611 }
5612 for (i = 0;i < nbCache;i++) {
5613 if (oldNs[i] == attr->ns) {
Daniel Veillardce66ce12002-10-28 19:01:59 +00005614 attr->ns = newNs[i];
Owen Taylor3473f882001-02-23 17:55:21 +00005615 break;
5616 }
5617 }
5618 if (i == nbCache) {
5619 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00005620 * OK we need to recreate a new namespace definition
Owen Taylor3473f882001-02-23 17:55:21 +00005621 */
5622 n = xmlNewReconciliedNs(doc, tree, attr->ns);
5623 if (n != NULL) { /* :-( what if else ??? */
5624 /*
5625 * check if we need to grow the cache buffers.
5626 */
5627 if (sizeCache <= nbCache) {
5628 sizeCache *= 2;
5629 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
5630 sizeof(xmlNsPtr));
5631 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005632 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005633 xmlFree(newNs);
5634 return(-1);
5635 }
5636 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
5637 sizeof(xmlNsPtr));
5638 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005639 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005640 xmlFree(oldNs);
5641 return(-1);
5642 }
5643 }
5644 newNs[nbCache] = n;
5645 oldNs[nbCache++] = attr->ns;
5646 attr->ns = n;
5647 }
5648 }
5649 }
5650 attr = attr->next;
5651 }
5652
5653 /*
5654 * Browse the full subtree, deep first
5655 */
5656 if (node->children != NULL) {
5657 /* deep first */
5658 node = node->children;
5659 } else if ((node != tree) && (node->next != NULL)) {
5660 /* then siblings */
5661 node = node->next;
5662 } else if (node != tree) {
5663 /* go up to parents->next if needed */
5664 while (node != tree) {
5665 if (node->parent != NULL)
5666 node = node->parent;
5667 if ((node != tree) && (node->next != NULL)) {
5668 node = node->next;
5669 break;
5670 }
5671 if (node->parent == NULL) {
5672 node = NULL;
5673 break;
5674 }
5675 }
5676 /* exit condition */
5677 if (node == tree)
5678 node = NULL;
Daniel Veillard1e774382002-03-06 17:35:40 +00005679 } else
5680 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005681 }
Daniel Veillardf742d342002-03-07 00:05:35 +00005682 if (oldNs != NULL)
5683 xmlFree(oldNs);
5684 if (newNs != NULL)
5685 xmlFree(newNs);
Owen Taylor3473f882001-02-23 17:55:21 +00005686 return(ret);
5687}
Daniel Veillard652327a2003-09-29 18:02:38 +00005688#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005689
5690/**
5691 * xmlHasProp:
5692 * @node: the node
5693 * @name: the attribute name
5694 *
5695 * Search an attribute associated to a node
5696 * This function also looks in DTD attribute declaration for #FIXED or
5697 * default declaration values unless DTD use has been turned off.
5698 *
5699 * Returns the attribute or the attribute declaration or NULL if
5700 * neither was found.
5701 */
5702xmlAttrPtr
5703xmlHasProp(xmlNodePtr node, const xmlChar *name) {
5704 xmlAttrPtr prop;
5705 xmlDocPtr doc;
5706
5707 if ((node == NULL) || (name == NULL)) return(NULL);
5708 /*
5709 * Check on the properties attached to the node
5710 */
5711 prop = node->properties;
5712 while (prop != NULL) {
5713 if (xmlStrEqual(prop->name, name)) {
5714 return(prop);
5715 }
5716 prop = prop->next;
5717 }
5718 if (!xmlCheckDTD) return(NULL);
5719
5720 /*
5721 * Check if there is a default declaration in the internal
5722 * or external subsets
5723 */
5724 doc = node->doc;
5725 if (doc != NULL) {
5726 xmlAttributePtr attrDecl;
5727 if (doc->intSubset != NULL) {
5728 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
5729 if ((attrDecl == NULL) && (doc->extSubset != NULL))
5730 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
Daniel Veillard75eb1ad2003-07-07 14:42:44 +00005731 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
5732 /* return attribute declaration only if a default value is given
5733 (that includes #FIXED declarations) */
Owen Taylor3473f882001-02-23 17:55:21 +00005734 return((xmlAttrPtr) attrDecl);
5735 }
5736 }
5737 return(NULL);
5738}
5739
5740/**
Daniel Veillarde95e2392001-06-06 10:46:28 +00005741 * xmlHasNsProp:
5742 * @node: the node
5743 * @name: the attribute name
Daniel Veillardca2366a2001-06-11 12:09:01 +00005744 * @nameSpace: the URI of the namespace
Daniel Veillarde95e2392001-06-06 10:46:28 +00005745 *
5746 * Search for an attribute associated to a node
5747 * This attribute has to be anchored in the namespace specified.
5748 * This does the entity substitution.
5749 * This function looks in DTD attribute declaration for #FIXED or
5750 * default declaration values unless DTD use has been turned off.
5751 *
5752 * Returns the attribute or the attribute declaration or NULL
5753 * if neither was found.
5754 */
5755xmlAttrPtr
Daniel Veillardca2366a2001-06-11 12:09:01 +00005756xmlHasNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
Daniel Veillarde95e2392001-06-06 10:46:28 +00005757 xmlAttrPtr prop;
Daniel Veillard652327a2003-09-29 18:02:38 +00005758#ifdef LIBXML_TREE_ENABLED
Daniel Veillarde95e2392001-06-06 10:46:28 +00005759 xmlDocPtr doc;
Daniel Veillard652327a2003-09-29 18:02:38 +00005760#endif /* LIBXML_TREE_ENABLED */
Daniel Veillarde95e2392001-06-06 10:46:28 +00005761
5762 if (node == NULL)
5763 return(NULL);
5764
5765 prop = node->properties;
Daniel Veillardca2366a2001-06-11 12:09:01 +00005766 if (nameSpace == NULL)
Daniel Veillarde95e2392001-06-06 10:46:28 +00005767 return(xmlHasProp(node, name));
5768 while (prop != NULL) {
5769 /*
5770 * One need to have
5771 * - same attribute names
5772 * - and the attribute carrying that namespace
Daniel Veillarde95e2392001-06-06 10:46:28 +00005773 */
5774 if ((xmlStrEqual(prop->name, name)) &&
Daniel Veillarde3c81b52001-06-17 14:50:34 +00005775 ((prop->ns != NULL) && (xmlStrEqual(prop->ns->href, nameSpace)))) {
5776 return(prop);
Daniel Veillarde95e2392001-06-06 10:46:28 +00005777 }
5778 prop = prop->next;
5779 }
5780 if (!xmlCheckDTD) return(NULL);
5781
Daniel Veillard652327a2003-09-29 18:02:38 +00005782#ifdef LIBXML_TREE_ENABLED
Daniel Veillarde95e2392001-06-06 10:46:28 +00005783 /*
5784 * Check if there is a default declaration in the internal
5785 * or external subsets
5786 */
5787 doc = node->doc;
5788 if (doc != NULL) {
5789 if (doc->intSubset != NULL) {
Daniel Veillardef6c46f2002-03-07 22:21:56 +00005790 xmlAttributePtr attrDecl = NULL;
5791 xmlNsPtr *nsList, *cur;
5792 xmlChar *ename;
Daniel Veillarde95e2392001-06-06 10:46:28 +00005793
Daniel Veillardef6c46f2002-03-07 22:21:56 +00005794 nsList = xmlGetNsList(node->doc, node);
5795 if (nsList == NULL)
5796 return(NULL);
5797 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
5798 ename = xmlStrdup(node->ns->prefix);
5799 ename = xmlStrcat(ename, BAD_CAST ":");
5800 ename = xmlStrcat(ename, node->name);
5801 } else {
5802 ename = xmlStrdup(node->name);
Daniel Veillarde95e2392001-06-06 10:46:28 +00005803 }
Daniel Veillardef6c46f2002-03-07 22:21:56 +00005804 if (ename == NULL) {
5805 xmlFree(nsList);
5806 return(NULL);
5807 }
5808
5809 cur = nsList;
5810 while (*cur != NULL) {
5811 if (xmlStrEqual((*cur)->href, nameSpace)) {
5812 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, ename,
5813 name, (*cur)->prefix);
5814 if ((attrDecl == NULL) && (doc->extSubset != NULL))
5815 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, ename,
5816 name, (*cur)->prefix);
5817 }
5818 cur++;
5819 }
5820 xmlFree(nsList);
5821 xmlFree(ename);
5822 return((xmlAttrPtr) attrDecl);
Daniel Veillarde95e2392001-06-06 10:46:28 +00005823 }
5824 }
Daniel Veillard652327a2003-09-29 18:02:38 +00005825#endif /* LIBXML_TREE_ENABLED */
Daniel Veillarde95e2392001-06-06 10:46:28 +00005826 return(NULL);
5827}
5828
5829/**
Owen Taylor3473f882001-02-23 17:55:21 +00005830 * xmlGetProp:
5831 * @node: the node
5832 * @name: the attribute name
5833 *
5834 * Search and get the value of an attribute associated to a node
5835 * This does the entity substitution.
5836 * This function looks in DTD attribute declaration for #FIXED or
5837 * default declaration values unless DTD use has been turned off.
Daniel Veillard784b9352003-02-16 15:50:27 +00005838 * NOTE: this function acts independently of namespaces associated
Daniel Veillard71531f32003-02-05 13:19:53 +00005839 * to the attribute. Use xmlGetNsProp() or xmlGetNoNsProp()
5840 * for namespace aware processing.
Owen Taylor3473f882001-02-23 17:55:21 +00005841 *
5842 * Returns the attribute value or NULL if not found.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00005843 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00005844 */
5845xmlChar *
5846xmlGetProp(xmlNodePtr node, const xmlChar *name) {
5847 xmlAttrPtr prop;
5848 xmlDocPtr doc;
5849
5850 if ((node == NULL) || (name == NULL)) return(NULL);
5851 /*
5852 * Check on the properties attached to the node
5853 */
5854 prop = node->properties;
5855 while (prop != NULL) {
5856 if (xmlStrEqual(prop->name, name)) {
5857 xmlChar *ret;
5858
5859 ret = xmlNodeListGetString(node->doc, prop->children, 1);
5860 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
5861 return(ret);
5862 }
5863 prop = prop->next;
5864 }
5865 if (!xmlCheckDTD) return(NULL);
5866
5867 /*
5868 * Check if there is a default declaration in the internal
5869 * or external subsets
5870 */
5871 doc = node->doc;
5872 if (doc != NULL) {
5873 xmlAttributePtr attrDecl;
5874 if (doc->intSubset != NULL) {
5875 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
5876 if ((attrDecl == NULL) && (doc->extSubset != NULL))
5877 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
Daniel Veillard75eb1ad2003-07-07 14:42:44 +00005878 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
5879 /* return attribute declaration only if a default value is given
5880 (that includes #FIXED declarations) */
Owen Taylor3473f882001-02-23 17:55:21 +00005881 return(xmlStrdup(attrDecl->defaultValue));
5882 }
5883 }
5884 return(NULL);
5885}
5886
5887/**
Daniel Veillard71531f32003-02-05 13:19:53 +00005888 * xmlGetNoNsProp:
5889 * @node: the node
5890 * @name: the attribute name
5891 *
5892 * Search and get the value of an attribute associated to a node
5893 * This does the entity substitution.
5894 * This function looks in DTD attribute declaration for #FIXED or
5895 * default declaration values unless DTD use has been turned off.
5896 * This function is similar to xmlGetProp except it will accept only
5897 * an attribute in no namespace.
5898 *
5899 * Returns the attribute value or NULL if not found.
5900 * It's up to the caller to free the memory with xmlFree().
5901 */
5902xmlChar *
5903xmlGetNoNsProp(xmlNodePtr node, const xmlChar *name) {
5904 xmlAttrPtr prop;
5905 xmlDocPtr doc;
5906
5907 if ((node == NULL) || (name == NULL)) return(NULL);
5908 /*
5909 * Check on the properties attached to the node
5910 */
5911 prop = node->properties;
5912 while (prop != NULL) {
5913 if ((prop->ns == NULL) && (xmlStrEqual(prop->name, name))) {
5914 xmlChar *ret;
5915
5916 ret = xmlNodeListGetString(node->doc, prop->children, 1);
5917 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
5918 return(ret);
5919 }
5920 prop = prop->next;
5921 }
5922 if (!xmlCheckDTD) return(NULL);
5923
5924 /*
5925 * Check if there is a default declaration in the internal
5926 * or external subsets
5927 */
5928 doc = node->doc;
5929 if (doc != NULL) {
5930 xmlAttributePtr attrDecl;
5931 if (doc->intSubset != NULL) {
5932 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
5933 if ((attrDecl == NULL) && (doc->extSubset != NULL))
5934 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
Daniel Veillard75eb1ad2003-07-07 14:42:44 +00005935 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
5936 /* return attribute declaration only if a default value is given
5937 (that includes #FIXED declarations) */
Daniel Veillard71531f32003-02-05 13:19:53 +00005938 return(xmlStrdup(attrDecl->defaultValue));
5939 }
5940 }
5941 return(NULL);
5942}
5943
5944/**
Owen Taylor3473f882001-02-23 17:55:21 +00005945 * xmlGetNsProp:
5946 * @node: the node
5947 * @name: the attribute name
Daniel Veillardca2366a2001-06-11 12:09:01 +00005948 * @nameSpace: the URI of the namespace
Owen Taylor3473f882001-02-23 17:55:21 +00005949 *
5950 * Search and get the value of an attribute associated to a node
5951 * This attribute has to be anchored in the namespace specified.
5952 * This does the entity substitution.
5953 * This function looks in DTD attribute declaration for #FIXED or
5954 * default declaration values unless DTD use has been turned off.
5955 *
5956 * Returns the attribute value or NULL if not found.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00005957 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00005958 */
5959xmlChar *
Daniel Veillardca2366a2001-06-11 12:09:01 +00005960xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
Owen Taylor3473f882001-02-23 17:55:21 +00005961 xmlAttrPtr prop;
5962 xmlDocPtr doc;
5963 xmlNsPtr ns;
5964
5965 if (node == NULL)
5966 return(NULL);
5967
5968 prop = node->properties;
Daniel Veillardca2366a2001-06-11 12:09:01 +00005969 if (nameSpace == NULL)
Daniel Veillard71531f32003-02-05 13:19:53 +00005970 return(xmlGetNoNsProp(node, name));
Owen Taylor3473f882001-02-23 17:55:21 +00005971 while (prop != NULL) {
5972 /*
5973 * One need to have
5974 * - same attribute names
5975 * - and the attribute carrying that namespace
Owen Taylor3473f882001-02-23 17:55:21 +00005976 */
5977 if ((xmlStrEqual(prop->name, name)) &&
Daniel Veillarde8fc08e2001-06-07 19:35:47 +00005978 ((prop->ns != NULL) &&
Daniel Veillardca2366a2001-06-11 12:09:01 +00005979 (xmlStrEqual(prop->ns->href, nameSpace)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00005980 xmlChar *ret;
5981
5982 ret = xmlNodeListGetString(node->doc, prop->children, 1);
5983 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
5984 return(ret);
5985 }
5986 prop = prop->next;
5987 }
5988 if (!xmlCheckDTD) return(NULL);
5989
5990 /*
5991 * Check if there is a default declaration in the internal
5992 * or external subsets
5993 */
5994 doc = node->doc;
5995 if (doc != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005996 if (doc->intSubset != NULL) {
Daniel Veillard5792e162001-04-30 17:44:45 +00005997 xmlAttributePtr attrDecl;
5998
Owen Taylor3473f882001-02-23 17:55:21 +00005999 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
6000 if ((attrDecl == NULL) && (doc->extSubset != NULL))
6001 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
6002
6003 if ((attrDecl != NULL) && (attrDecl->prefix != NULL)) {
6004 /*
6005 * The DTD declaration only allows a prefix search
6006 */
6007 ns = xmlSearchNs(doc, node, attrDecl->prefix);
Daniel Veillardca2366a2001-06-11 12:09:01 +00006008 if ((ns != NULL) && (xmlStrEqual(ns->href, nameSpace)))
Owen Taylor3473f882001-02-23 17:55:21 +00006009 return(xmlStrdup(attrDecl->defaultValue));
6010 }
6011 }
6012 }
6013 return(NULL);
6014}
6015
Daniel Veillard652327a2003-09-29 18:02:38 +00006016#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00006017/**
6018 * xmlSetProp:
6019 * @node: the node
6020 * @name: the attribute name
6021 * @value: the attribute value
6022 *
6023 * Set (or reset) an attribute carried by a node.
6024 * Returns the attribute pointer.
6025 */
6026xmlAttrPtr
6027xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
Daniel Veillard4855c8c2001-11-25 10:35:25 +00006028 xmlAttrPtr prop;
6029 xmlDocPtr doc;
Owen Taylor3473f882001-02-23 17:55:21 +00006030
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006031 if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE))
Owen Taylor3473f882001-02-23 17:55:21 +00006032 return(NULL);
6033 doc = node->doc;
Daniel Veillard4855c8c2001-11-25 10:35:25 +00006034 prop = node->properties;
Owen Taylor3473f882001-02-23 17:55:21 +00006035 while (prop != NULL) {
Daniel Veillard75bea542001-05-11 17:41:21 +00006036 if ((xmlStrEqual(prop->name, name)) &&
6037 (prop->ns == NULL)){
Daniel Veillard4855c8c2001-11-25 10:35:25 +00006038 xmlNodePtr oldprop = prop->children;
6039
Owen Taylor3473f882001-02-23 17:55:21 +00006040 prop->children = NULL;
6041 prop->last = NULL;
6042 if (value != NULL) {
6043 xmlChar *buffer;
6044 xmlNodePtr tmp;
6045
6046 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
6047 prop->children = xmlStringGetNodeList(node->doc, buffer);
6048 prop->last = NULL;
6049 prop->doc = doc;
6050 tmp = prop->children;
6051 while (tmp != NULL) {
6052 tmp->parent = (xmlNodePtr) prop;
6053 tmp->doc = doc;
6054 if (tmp->next == NULL)
6055 prop->last = tmp;
6056 tmp = tmp->next;
6057 }
6058 xmlFree(buffer);
Daniel Veillard75bea542001-05-11 17:41:21 +00006059 }
Daniel Veillard4855c8c2001-11-25 10:35:25 +00006060 if (oldprop != NULL)
6061 xmlFreeNodeList(oldprop);
Owen Taylor3473f882001-02-23 17:55:21 +00006062 return(prop);
6063 }
6064 prop = prop->next;
6065 }
6066 prop = xmlNewProp(node, name, value);
6067 return(prop);
6068}
6069
6070/**
Daniel Veillard75bea542001-05-11 17:41:21 +00006071 * xmlUnsetProp:
6072 * @node: the node
6073 * @name: the attribute name
6074 *
6075 * Remove an attribute carried by a node.
6076 * Returns 0 if successful, -1 if not found
6077 */
6078int
6079xmlUnsetProp(xmlNodePtr node, const xmlChar *name) {
Daniel Veillard814a76d2003-01-23 18:24:20 +00006080 xmlAttrPtr prop, prev = NULL;;
Daniel Veillard75bea542001-05-11 17:41:21 +00006081
6082 if ((node == NULL) || (name == NULL))
6083 return(-1);
Daniel Veillard814a76d2003-01-23 18:24:20 +00006084 prop = node->properties;
Daniel Veillard75bea542001-05-11 17:41:21 +00006085 while (prop != NULL) {
6086 if ((xmlStrEqual(prop->name, name)) &&
6087 (prop->ns == NULL)) {
6088 if (prev == NULL)
6089 node->properties = prop->next;
6090 else
6091 prev->next = prop->next;
6092 xmlFreeProp(prop);
6093 return(0);
6094 }
6095 prev = prop;
6096 prop = prop->next;
6097 }
6098 return(-1);
6099}
6100
6101/**
Owen Taylor3473f882001-02-23 17:55:21 +00006102 * xmlSetNsProp:
6103 * @node: the node
6104 * @ns: the namespace definition
6105 * @name: the attribute name
6106 * @value: the attribute value
6107 *
6108 * Set (or reset) an attribute carried by a node.
6109 * The ns structure must be in scope, this is not checked.
6110 *
6111 * Returns the attribute pointer.
6112 */
6113xmlAttrPtr
6114xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
6115 const xmlChar *value) {
6116 xmlAttrPtr prop;
6117
6118 if ((node == NULL) || (name == NULL))
6119 return(NULL);
6120
6121 if (ns == NULL)
6122 return(xmlSetProp(node, name, value));
6123 if (ns->href == NULL)
6124 return(NULL);
6125 prop = node->properties;
6126
6127 while (prop != NULL) {
6128 /*
6129 * One need to have
6130 * - same attribute names
6131 * - and the attribute carrying that namespace
Owen Taylor3473f882001-02-23 17:55:21 +00006132 */
6133 if ((xmlStrEqual(prop->name, name)) &&
Daniel Veillarda57c26e2002-08-01 12:52:24 +00006134 (prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))) {
Owen Taylor3473f882001-02-23 17:55:21 +00006135 if (prop->children != NULL)
6136 xmlFreeNodeList(prop->children);
6137 prop->children = NULL;
6138 prop->last = NULL;
6139 prop->ns = ns;
6140 if (value != NULL) {
6141 xmlChar *buffer;
6142 xmlNodePtr tmp;
6143
6144 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
6145 prop->children = xmlStringGetNodeList(node->doc, buffer);
6146 prop->last = NULL;
6147 tmp = prop->children;
6148 while (tmp != NULL) {
6149 tmp->parent = (xmlNodePtr) prop;
6150 if (tmp->next == NULL)
6151 prop->last = tmp;
6152 tmp = tmp->next;
6153 }
6154 xmlFree(buffer);
6155 }
6156 return(prop);
6157 }
6158 prop = prop->next;
6159 }
6160 prop = xmlNewNsProp(node, ns, name, value);
6161 return(prop);
6162}
6163
6164/**
Daniel Veillard75bea542001-05-11 17:41:21 +00006165 * xmlUnsetNsProp:
6166 * @node: the node
6167 * @ns: the namespace definition
6168 * @name: the attribute name
6169 *
6170 * Remove an attribute carried by a node.
6171 * Returns 0 if successful, -1 if not found
6172 */
6173int
6174xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) {
6175 xmlAttrPtr prop = node->properties, prev = NULL;;
6176
6177 if ((node == NULL) || (name == NULL))
6178 return(-1);
6179 if (ns == NULL)
6180 return(xmlUnsetProp(node, name));
6181 if (ns->href == NULL)
6182 return(-1);
6183 while (prop != NULL) {
6184 if ((xmlStrEqual(prop->name, name)) &&
Daniel Veillard0bf29002002-08-01 12:54:11 +00006185 (prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))) {
Daniel Veillard75bea542001-05-11 17:41:21 +00006186 if (prev == NULL)
6187 node->properties = prop->next;
6188 else
6189 prev->next = prop->next;
6190 xmlFreeProp(prop);
6191 return(0);
6192 }
6193 prev = prop;
6194 prop = prop->next;
6195 }
6196 return(-1);
6197}
Daniel Veillard652327a2003-09-29 18:02:38 +00006198#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard75bea542001-05-11 17:41:21 +00006199
6200/**
Owen Taylor3473f882001-02-23 17:55:21 +00006201 * xmlNodeIsText:
6202 * @node: the node
6203 *
6204 * Is this node a Text node ?
6205 * Returns 1 yes, 0 no
6206 */
6207int
6208xmlNodeIsText(xmlNodePtr node) {
6209 if (node == NULL) return(0);
6210
6211 if (node->type == XML_TEXT_NODE) return(1);
6212 return(0);
6213}
6214
6215/**
6216 * xmlIsBlankNode:
6217 * @node: the node
6218 *
6219 * Checks whether this node is an empty or whitespace only
6220 * (and possibly ignorable) text-node.
6221 *
6222 * Returns 1 yes, 0 no
6223 */
6224int
6225xmlIsBlankNode(xmlNodePtr node) {
6226 const xmlChar *cur;
6227 if (node == NULL) return(0);
6228
Daniel Veillard7db37732001-07-12 01:20:08 +00006229 if ((node->type != XML_TEXT_NODE) &&
6230 (node->type != XML_CDATA_SECTION_NODE))
6231 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006232 if (node->content == NULL) return(1);
Owen Taylor3473f882001-02-23 17:55:21 +00006233 cur = node->content;
Owen Taylor3473f882001-02-23 17:55:21 +00006234 while (*cur != 0) {
6235 if (!IS_BLANK(*cur)) return(0);
6236 cur++;
6237 }
6238
6239 return(1);
6240}
6241
6242/**
6243 * xmlTextConcat:
6244 * @node: the node
6245 * @content: the content
Daniel Veillard60087f32001-10-10 09:45:09 +00006246 * @len: @content length
Owen Taylor3473f882001-02-23 17:55:21 +00006247 *
6248 * Concat the given string at the end of the existing node content
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006249 *
6250 * Returns -1 in case of error, 0 otherwise
Owen Taylor3473f882001-02-23 17:55:21 +00006251 */
6252
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006253int
Owen Taylor3473f882001-02-23 17:55:21 +00006254xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006255 if (node == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006256
6257 if ((node->type != XML_TEXT_NODE) &&
6258 (node->type != XML_CDATA_SECTION_NODE)) {
6259#ifdef DEBUG_TREE
6260 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006261 "xmlTextConcat: node is not text nor CDATA\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006262#endif
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006263 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006264 }
Owen Taylor3473f882001-02-23 17:55:21 +00006265 node->content = xmlStrncat(node->content, content, len);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006266 if (node->content == NULL)
6267 return(-1);
6268 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006269}
6270
6271/************************************************************************
6272 * *
6273 * Output : to a FILE or in memory *
6274 * *
6275 ************************************************************************/
6276
Owen Taylor3473f882001-02-23 17:55:21 +00006277/**
6278 * xmlBufferCreate:
6279 *
6280 * routine to create an XML buffer.
6281 * returns the new structure.
6282 */
6283xmlBufferPtr
6284xmlBufferCreate(void) {
6285 xmlBufferPtr ret;
6286
6287 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6288 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006289 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006290 return(NULL);
6291 }
6292 ret->use = 0;
Daniel Veillarde356c282001-03-10 12:32:04 +00006293 ret->size = xmlDefaultBufferSize;
Owen Taylor3473f882001-02-23 17:55:21 +00006294 ret->alloc = xmlBufferAllocScheme;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00006295 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00006296 if (ret->content == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006297 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006298 xmlFree(ret);
6299 return(NULL);
6300 }
6301 ret->content[0] = 0;
6302 return(ret);
6303}
6304
6305/**
6306 * xmlBufferCreateSize:
6307 * @size: initial size of buffer
6308 *
6309 * routine to create an XML buffer.
6310 * returns the new structure.
6311 */
6312xmlBufferPtr
6313xmlBufferCreateSize(size_t size) {
6314 xmlBufferPtr ret;
6315
6316 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6317 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006318 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006319 return(NULL);
6320 }
6321 ret->use = 0;
6322 ret->alloc = xmlBufferAllocScheme;
6323 ret->size = (size ? size+2 : 0); /* +1 for ending null */
6324 if (ret->size){
Daniel Veillard3c908dc2003-04-19 00:07:51 +00006325 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00006326 if (ret->content == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006327 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006328 xmlFree(ret);
6329 return(NULL);
6330 }
6331 ret->content[0] = 0;
6332 } else
6333 ret->content = NULL;
6334 return(ret);
6335}
6336
6337/**
Daniel Veillard53350552003-09-18 13:35:51 +00006338 * xmlBufferCreateStatic:
6339 * @mem: the memory area
6340 * @size: the size in byte
6341 *
6342 * routine to create an XML buffer from an immutable memory area,
6343 * The are won't be modified nor copied, and is expected to be
6344 * present until the end of the buffer lifetime.
6345 *
6346 * returns the new structure.
6347 */
6348xmlBufferPtr
6349xmlBufferCreateStatic(void *mem, size_t size) {
6350 xmlBufferPtr ret;
6351
6352 if ((mem == NULL) || (size == 0))
6353 return(NULL);
6354
6355 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6356 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006357 xmlTreeErrMemory("creating buffer");
Daniel Veillard53350552003-09-18 13:35:51 +00006358 return(NULL);
6359 }
6360 ret->use = size;
6361 ret->size = size;
6362 ret->alloc = XML_BUFFER_ALLOC_IMMUTABLE;
6363 ret->content = (xmlChar *) mem;
6364 return(ret);
6365}
6366
6367/**
Owen Taylor3473f882001-02-23 17:55:21 +00006368 * xmlBufferSetAllocationScheme:
Daniel Veillardbd9afb52002-09-25 22:25:35 +00006369 * @buf: the buffer to tune
Owen Taylor3473f882001-02-23 17:55:21 +00006370 * @scheme: allocation scheme to use
6371 *
6372 * Sets the allocation scheme for this buffer
6373 */
6374void
6375xmlBufferSetAllocationScheme(xmlBufferPtr buf,
6376 xmlBufferAllocationScheme scheme) {
6377 if (buf == NULL) {
6378#ifdef DEBUG_BUFFER
6379 xmlGenericError(xmlGenericErrorContext,
6380 "xmlBufferSetAllocationScheme: buf == NULL\n");
6381#endif
6382 return;
6383 }
Daniel Veillard53350552003-09-18 13:35:51 +00006384 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00006385
6386 buf->alloc = scheme;
6387}
6388
6389/**
6390 * xmlBufferFree:
6391 * @buf: the buffer to free
6392 *
Daniel Veillard9d06d302002-01-22 18:15:52 +00006393 * Frees an XML buffer. It frees both the content and the structure which
6394 * encapsulate it.
Owen Taylor3473f882001-02-23 17:55:21 +00006395 */
6396void
6397xmlBufferFree(xmlBufferPtr buf) {
6398 if (buf == NULL) {
6399#ifdef DEBUG_BUFFER
6400 xmlGenericError(xmlGenericErrorContext,
6401 "xmlBufferFree: buf == NULL\n");
6402#endif
6403 return;
6404 }
Daniel Veillard53350552003-09-18 13:35:51 +00006405
6406 if ((buf->content != NULL) &&
6407 (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006408 xmlFree(buf->content);
6409 }
Owen Taylor3473f882001-02-23 17:55:21 +00006410 xmlFree(buf);
6411}
6412
6413/**
6414 * xmlBufferEmpty:
6415 * @buf: the buffer
6416 *
6417 * empty a buffer.
6418 */
6419void
6420xmlBufferEmpty(xmlBufferPtr buf) {
6421 if (buf->content == NULL) return;
6422 buf->use = 0;
Daniel Veillard53350552003-09-18 13:35:51 +00006423 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00006424 buf->content = BAD_CAST "";
Daniel Veillard53350552003-09-18 13:35:51 +00006425 } else {
6426 memset(buf->content, 0, buf->size);
6427 }
Owen Taylor3473f882001-02-23 17:55:21 +00006428}
6429
6430/**
6431 * xmlBufferShrink:
6432 * @buf: the buffer to dump
6433 * @len: the number of xmlChar to remove
6434 *
6435 * Remove the beginning of an XML buffer.
6436 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006437 * Returns the number of #xmlChar removed, or -1 in case of failure.
Owen Taylor3473f882001-02-23 17:55:21 +00006438 */
6439int
6440xmlBufferShrink(xmlBufferPtr buf, unsigned int len) {
6441 if (len == 0) return(0);
6442 if (len > buf->use) return(-1);
6443
6444 buf->use -= len;
Daniel Veillard53350552003-09-18 13:35:51 +00006445 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) {
6446 buf->content += len;
6447 } else {
6448 memmove(buf->content, &buf->content[len], buf->use * sizeof(xmlChar));
6449 buf->content[buf->use] = 0;
6450 }
Owen Taylor3473f882001-02-23 17:55:21 +00006451 return(len);
6452}
6453
6454/**
6455 * xmlBufferGrow:
6456 * @buf: the buffer
6457 * @len: the minimum free size to allocate
6458 *
6459 * Grow the available space of an XML buffer.
6460 *
6461 * Returns the new available space or -1 in case of error
6462 */
6463int
6464xmlBufferGrow(xmlBufferPtr buf, unsigned int len) {
6465 int size;
6466 xmlChar *newbuf;
6467
Daniel Veillard53350552003-09-18 13:35:51 +00006468 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006469 if (len + buf->use < buf->size) return(0);
6470
6471 size = buf->use + len + 100;
6472
6473 newbuf = (xmlChar *) xmlRealloc(buf->content, size);
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006474 if (newbuf == NULL) {
6475 xmlTreeErrMemory("growing buffer");
6476 return(-1);
6477 }
Owen Taylor3473f882001-02-23 17:55:21 +00006478 buf->content = newbuf;
6479 buf->size = size;
6480 return(buf->size - buf->use);
6481}
6482
6483/**
6484 * xmlBufferDump:
6485 * @file: the file output
6486 * @buf: the buffer to dump
6487 *
6488 * Dumps an XML buffer to a FILE *.
Daniel Veillardd1640922001-12-17 15:30:10 +00006489 * Returns the number of #xmlChar written
Owen Taylor3473f882001-02-23 17:55:21 +00006490 */
6491int
6492xmlBufferDump(FILE *file, xmlBufferPtr buf) {
6493 int ret;
6494
6495 if (buf == NULL) {
6496#ifdef DEBUG_BUFFER
6497 xmlGenericError(xmlGenericErrorContext,
6498 "xmlBufferDump: buf == NULL\n");
6499#endif
6500 return(0);
6501 }
6502 if (buf->content == NULL) {
6503#ifdef DEBUG_BUFFER
6504 xmlGenericError(xmlGenericErrorContext,
6505 "xmlBufferDump: buf->content == NULL\n");
6506#endif
6507 return(0);
6508 }
Daniel Veillardcd337f02001-11-22 18:20:37 +00006509 if (file == NULL)
6510 file = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00006511 ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
6512 return(ret);
6513}
6514
6515/**
6516 * xmlBufferContent:
6517 * @buf: the buffer
6518 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00006519 * Function to extract the content of a buffer
6520 *
Owen Taylor3473f882001-02-23 17:55:21 +00006521 * Returns the internal content
6522 */
6523
Daniel Veillard5e2dace2001-07-18 19:30:27 +00006524const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00006525xmlBufferContent(const xmlBufferPtr buf)
6526{
6527 if(!buf)
6528 return NULL;
6529
6530 return buf->content;
6531}
6532
6533/**
6534 * xmlBufferLength:
6535 * @buf: the buffer
6536 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00006537 * Function to get the length of a buffer
6538 *
Owen Taylor3473f882001-02-23 17:55:21 +00006539 * Returns the length of data in the internal content
6540 */
6541
6542int
6543xmlBufferLength(const xmlBufferPtr buf)
6544{
6545 if(!buf)
6546 return 0;
6547
6548 return buf->use;
6549}
6550
6551/**
6552 * xmlBufferResize:
6553 * @buf: the buffer to resize
6554 * @size: the desired size
6555 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006556 * Resize a buffer to accommodate minimum size of @size.
Owen Taylor3473f882001-02-23 17:55:21 +00006557 *
6558 * Returns 0 in case of problems, 1 otherwise
6559 */
6560int
6561xmlBufferResize(xmlBufferPtr buf, unsigned int size)
6562{
6563 unsigned int newSize;
6564 xmlChar* rebuf = NULL;
6565
Daniel Veillard53350552003-09-18 13:35:51 +00006566 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
6567
Owen Taylor3473f882001-02-23 17:55:21 +00006568 /*take care of empty case*/
6569 newSize = (buf->size ? buf->size*2 : size);
6570
6571 /* Don't resize if we don't have to */
6572 if (size < buf->size)
6573 return 1;
6574
6575 /* figure out new size */
6576 switch (buf->alloc){
6577 case XML_BUFFER_ALLOC_DOUBLEIT:
6578 while (size > newSize) newSize *= 2;
6579 break;
6580 case XML_BUFFER_ALLOC_EXACT:
6581 newSize = size+10;
6582 break;
6583 default:
6584 newSize = size+10;
6585 break;
6586 }
6587
6588 if (buf->content == NULL)
Daniel Veillard3c908dc2003-04-19 00:07:51 +00006589 rebuf = (xmlChar *) xmlMallocAtomic(newSize * sizeof(xmlChar));
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006590 else if (buf->size - buf->use < 100) {
Owen Taylor3473f882001-02-23 17:55:21 +00006591 rebuf = (xmlChar *) xmlRealloc(buf->content,
6592 newSize * sizeof(xmlChar));
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006593 } else {
6594 /*
6595 * if we are reallocating a buffer far from being full, it's
6596 * better to make a new allocation and copy only the used range
6597 * and free the old one.
6598 */
6599 rebuf = (xmlChar *) xmlMallocAtomic(newSize * sizeof(xmlChar));
6600 if (rebuf != NULL) {
6601 memcpy(rebuf, buf->content, buf->use);
6602 xmlFree(buf->content);
6603 }
Daniel Veillarde5984082003-08-19 22:21:13 +00006604 rebuf[buf->use] = 0;
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006605 }
Owen Taylor3473f882001-02-23 17:55:21 +00006606 if (rebuf == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006607 xmlTreeErrMemory("growing buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006608 return 0;
6609 }
6610 buf->content = rebuf;
6611 buf->size = newSize;
6612
6613 return 1;
6614}
6615
6616/**
6617 * xmlBufferAdd:
6618 * @buf: the buffer to dump
Daniel Veillardd1640922001-12-17 15:30:10 +00006619 * @str: the #xmlChar string
6620 * @len: the number of #xmlChar to add
Owen Taylor3473f882001-02-23 17:55:21 +00006621 *
Daniel Veillard60087f32001-10-10 09:45:09 +00006622 * Add a string range to an XML buffer. if len == -1, the length of
Owen Taylor3473f882001-02-23 17:55:21 +00006623 * str is recomputed.
6624 */
6625void
6626xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) {
6627 unsigned int needSize;
6628
6629 if (str == NULL) {
6630#ifdef DEBUG_BUFFER
6631 xmlGenericError(xmlGenericErrorContext,
6632 "xmlBufferAdd: str == NULL\n");
6633#endif
6634 return;
6635 }
Daniel Veillard53350552003-09-18 13:35:51 +00006636 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00006637 if (len < -1) {
6638#ifdef DEBUG_BUFFER
6639 xmlGenericError(xmlGenericErrorContext,
6640 "xmlBufferAdd: len < 0\n");
6641#endif
6642 return;
6643 }
6644 if (len == 0) return;
6645
6646 if (len < 0)
6647 len = xmlStrlen(str);
6648
6649 if (len <= 0) return;
6650
6651 needSize = buf->use + len + 2;
6652 if (needSize > buf->size){
6653 if (!xmlBufferResize(buf, needSize)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006654 xmlTreeErrMemory("growing buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006655 return;
6656 }
6657 }
6658
6659 memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
6660 buf->use += len;
6661 buf->content[buf->use] = 0;
6662}
6663
6664/**
6665 * xmlBufferAddHead:
6666 * @buf: the buffer
Daniel Veillardd1640922001-12-17 15:30:10 +00006667 * @str: the #xmlChar string
6668 * @len: the number of #xmlChar to add
Owen Taylor3473f882001-02-23 17:55:21 +00006669 *
6670 * Add a string range to the beginning of an XML buffer.
Daniel Veillard60087f32001-10-10 09:45:09 +00006671 * if len == -1, the length of @str is recomputed.
Owen Taylor3473f882001-02-23 17:55:21 +00006672 */
6673void
6674xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) {
6675 unsigned int needSize;
6676
Daniel Veillard53350552003-09-18 13:35:51 +00006677 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00006678 if (str == NULL) {
6679#ifdef DEBUG_BUFFER
6680 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006681 "xmlBufferAddHead: str == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006682#endif
6683 return;
6684 }
6685 if (len < -1) {
6686#ifdef DEBUG_BUFFER
6687 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006688 "xmlBufferAddHead: len < 0\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006689#endif
6690 return;
6691 }
6692 if (len == 0) return;
6693
6694 if (len < 0)
6695 len = xmlStrlen(str);
6696
6697 if (len <= 0) return;
6698
6699 needSize = buf->use + len + 2;
6700 if (needSize > buf->size){
6701 if (!xmlBufferResize(buf, needSize)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006702 xmlTreeErrMemory("growing buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006703 return;
6704 }
6705 }
6706
6707 memmove(&buf->content[len], &buf->content[0], buf->use * sizeof(xmlChar));
6708 memmove(&buf->content[0], str, len * sizeof(xmlChar));
6709 buf->use += len;
6710 buf->content[buf->use] = 0;
6711}
6712
6713/**
6714 * xmlBufferCat:
6715 * @buf: the buffer to dump
Daniel Veillardd1640922001-12-17 15:30:10 +00006716 * @str: the #xmlChar string
Owen Taylor3473f882001-02-23 17:55:21 +00006717 *
6718 * Append a zero terminated string to an XML buffer.
6719 */
6720void
6721xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) {
Daniel Veillard53350552003-09-18 13:35:51 +00006722 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00006723 if (str != NULL)
6724 xmlBufferAdd(buf, str, -1);
6725}
6726
6727/**
6728 * xmlBufferCCat:
6729 * @buf: the buffer to dump
6730 * @str: the C char string
6731 *
6732 * Append a zero terminated C string to an XML buffer.
6733 */
6734void
6735xmlBufferCCat(xmlBufferPtr buf, const char *str) {
6736 const char *cur;
6737
Daniel Veillard53350552003-09-18 13:35:51 +00006738 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00006739 if (str == NULL) {
6740#ifdef DEBUG_BUFFER
6741 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006742 "xmlBufferCCat: str == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006743#endif
6744 return;
6745 }
6746 for (cur = str;*cur != 0;cur++) {
6747 if (buf->use + 10 >= buf->size) {
6748 if (!xmlBufferResize(buf, buf->use+10)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006749 xmlTreeErrMemory("growing buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006750 return;
6751 }
6752 }
6753 buf->content[buf->use++] = *cur;
6754 }
6755 buf->content[buf->use] = 0;
6756}
6757
6758/**
6759 * xmlBufferWriteCHAR:
6760 * @buf: the XML buffer
6761 * @string: the string to add
6762 *
6763 * routine which manages and grows an output buffer. This one adds
6764 * xmlChars at the end of the buffer.
6765 */
6766void
Daniel Veillard53350552003-09-18 13:35:51 +00006767xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) {
6768 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00006769 xmlBufferCat(buf, string);
6770}
6771
6772/**
6773 * xmlBufferWriteChar:
6774 * @buf: the XML buffer output
6775 * @string: the string to add
6776 *
6777 * routine which manage and grows an output buffer. This one add
6778 * C chars at the end of the array.
6779 */
6780void
6781xmlBufferWriteChar(xmlBufferPtr buf, const char *string) {
Daniel Veillard53350552003-09-18 13:35:51 +00006782 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00006783 xmlBufferCCat(buf, string);
6784}
6785
6786
6787/**
6788 * xmlBufferWriteQuotedString:
6789 * @buf: the XML buffer output
6790 * @string: the string to add
6791 *
6792 * routine which manage and grows an output buffer. This one writes
Daniel Veillardd1640922001-12-17 15:30:10 +00006793 * a quoted or double quoted #xmlChar string, checking first if it holds
Owen Taylor3473f882001-02-23 17:55:21 +00006794 * quote or double-quotes internally
6795 */
6796void
6797xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) {
Daniel Veillard39057f42003-08-04 01:33:43 +00006798 const xmlChar *cur, *base;
Daniel Veillard53350552003-09-18 13:35:51 +00006799 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Daniel Veillard39057f42003-08-04 01:33:43 +00006800 if (xmlStrchr(string, '\"')) {
Daniel Veillard20aa0fb2003-08-04 19:43:15 +00006801 if (xmlStrchr(string, '\'')) {
Owen Taylor3473f882001-02-23 17:55:21 +00006802#ifdef DEBUG_BUFFER
6803 xmlGenericError(xmlGenericErrorContext,
6804 "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n");
6805#endif
Daniel Veillard39057f42003-08-04 01:33:43 +00006806 xmlBufferCCat(buf, "\"");
6807 base = cur = string;
6808 while(*cur != 0){
6809 if(*cur == '"'){
6810 if (base != cur)
6811 xmlBufferAdd(buf, base, cur - base);
6812 xmlBufferAdd(buf, BAD_CAST "&quot;", 6);
6813 cur++;
6814 base = cur;
6815 }
6816 else {
6817 cur++;
6818 }
6819 }
6820 if (base != cur)
6821 xmlBufferAdd(buf, base, cur - base);
6822 xmlBufferCCat(buf, "\"");
Owen Taylor3473f882001-02-23 17:55:21 +00006823 }
Daniel Veillard39057f42003-08-04 01:33:43 +00006824 else{
6825 xmlBufferCCat(buf, "\'");
6826 xmlBufferCat(buf, string);
6827 xmlBufferCCat(buf, "\'");
6828 }
Owen Taylor3473f882001-02-23 17:55:21 +00006829 } else {
6830 xmlBufferCCat(buf, "\"");
6831 xmlBufferCat(buf, string);
6832 xmlBufferCCat(buf, "\"");
6833 }
6834}
6835
6836
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00006837#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00006838/************************************************************************
6839 * *
Daniel Veillarde2238d52003-10-09 13:14:55 +00006840 * Output error handlers *
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006841 * *
6842 ************************************************************************/
6843/**
6844 * xmlSaveErrMemory:
6845 * @extra: extra informations
6846 *
6847 * Handle an out of memory condition
6848 */
6849static void
6850xmlSaveErrMemory(const char *extra)
6851{
6852 __xmlSimpleError(XML_FROM_OUTPUT, XML_ERR_NO_MEMORY, NULL, NULL, extra);
6853}
6854
6855/**
6856 * xmlSaveErr:
6857 * @code: the error number
6858 * @node: the location of the error.
6859 * @extra: extra informations
6860 *
6861 * Handle an out of memory condition
6862 */
6863static void
6864xmlSaveErr(int code, xmlNodePtr node, const char *extra)
6865{
6866 const char *msg = NULL;
6867
6868 switch(code) {
6869 case XML_SAVE_NOT_UTF8:
6870 msg = "string is not in UTF-8";
6871 break;
6872 case XML_SAVE_CHAR_INVALID:
6873 msg = "invalid character value";
6874 break;
6875 case XML_SAVE_UNKNOWN_ENCODING:
6876 msg = "unknown encoding %s";
6877 break;
Daniel Veillarde2238d52003-10-09 13:14:55 +00006878 case XML_SAVE_NO_DOCTYPE:
6879 msg = "document has no DOCTYPE";
6880 break;
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006881 default:
6882 msg = "unexpected error number";
6883 }
Daniel Veillarde2238d52003-10-09 13:14:55 +00006884 __xmlSimpleError(XML_FROM_OUTPUT, code, node, msg, extra);
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006885}
6886/************************************************************************
6887 * *
Owen Taylor3473f882001-02-23 17:55:21 +00006888 * Dumping XML tree content to a simple buffer *
6889 * *
6890 ************************************************************************/
6891
Owen Taylor3473f882001-02-23 17:55:21 +00006892/**
Daniel Veillarda6d05382002-02-13 13:07:41 +00006893 * xmlAttrSerializeContent:
6894 * @buf: the XML buffer output
6895 * @doc: the document
6896 * @attr: the attribute pointer
6897 *
6898 * Serialize the attribute in the buffer
6899 */
6900static void
Daniel Veillardebc4ca92002-11-27 11:43:05 +00006901xmlAttrSerializeContent(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr attr)
6902{
Daniel Veillarda6d05382002-02-13 13:07:41 +00006903 const xmlChar *cur, *base;
6904 xmlNodePtr children;
6905
6906 children = attr->children;
6907 while (children != NULL) {
Daniel Veillardebc4ca92002-11-27 11:43:05 +00006908 switch (children->type) {
6909 case XML_TEXT_NODE:
6910 base = cur = children->content;
6911 while (*cur != 0) {
6912 if (*cur == '\n') {
6913 if (base != cur)
6914 xmlBufferAdd(buf, base, cur - base);
6915 xmlBufferAdd(buf, BAD_CAST "&#10;", 5);
6916 cur++;
6917 base = cur;
Daniel Veillard0046c0f2003-02-23 13:52:30 +00006918 } else if (*cur == '\r') {
6919 if (base != cur)
6920 xmlBufferAdd(buf, base, cur - base);
Daniel Veillardc64b8e92003-02-24 11:47:13 +00006921 xmlBufferAdd(buf, BAD_CAST "&#13;", 5);
Daniel Veillard0046c0f2003-02-23 13:52:30 +00006922 cur++;
6923 base = cur;
6924 } else if (*cur == '\t') {
6925 if (base != cur)
6926 xmlBufferAdd(buf, base, cur - base);
Daniel Veillardc64b8e92003-02-24 11:47:13 +00006927 xmlBufferAdd(buf, BAD_CAST "&#9;", 4);
Daniel Veillard0046c0f2003-02-23 13:52:30 +00006928 cur++;
6929 base = cur;
Daniel Veillarda6d05382002-02-13 13:07:41 +00006930#if 0
Daniel Veillardebc4ca92002-11-27 11:43:05 +00006931 } else if (*cur == '\'') {
6932 if (base != cur)
6933 xmlBufferAdd(buf, base, cur - base);
6934 xmlBufferAdd(buf, BAD_CAST "&apos;", 6);
6935 cur++;
6936 base = cur;
Daniel Veillarda6d05382002-02-13 13:07:41 +00006937#endif
Daniel Veillardebc4ca92002-11-27 11:43:05 +00006938 } else if (*cur == '"') {
6939 if (base != cur)
6940 xmlBufferAdd(buf, base, cur - base);
6941 xmlBufferAdd(buf, BAD_CAST "&quot;", 6);
6942 cur++;
6943 base = cur;
6944 } else if (*cur == '<') {
6945 if (base != cur)
6946 xmlBufferAdd(buf, base, cur - base);
6947 xmlBufferAdd(buf, BAD_CAST "&lt;", 4);
6948 cur++;
6949 base = cur;
6950 } else if (*cur == '>') {
6951 if (base != cur)
6952 xmlBufferAdd(buf, base, cur - base);
6953 xmlBufferAdd(buf, BAD_CAST "&gt;", 4);
6954 cur++;
6955 base = cur;
6956 } else if (*cur == '&') {
6957 if (base != cur)
6958 xmlBufferAdd(buf, base, cur - base);
6959 xmlBufferAdd(buf, BAD_CAST "&amp;", 5);
6960 cur++;
6961 base = cur;
6962 } else if ((*cur >= 0x80) && ((doc == NULL) ||
6963 (doc->encoding ==
6964 NULL))) {
6965 /*
6966 * We assume we have UTF-8 content.
6967 */
6968 char tmp[10];
6969 int val = 0, l = 1;
Daniel Veillarda6d05382002-02-13 13:07:41 +00006970
Daniel Veillardebc4ca92002-11-27 11:43:05 +00006971 if (base != cur)
6972 xmlBufferAdd(buf, base, cur - base);
6973 if (*cur < 0xC0) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006974 xmlSaveErr(XML_SAVE_NOT_UTF8, (xmlNodePtr) attr,
6975 NULL);
Daniel Veillardebc4ca92002-11-27 11:43:05 +00006976 if (doc != NULL)
6977 doc->encoding =
6978 xmlStrdup(BAD_CAST "ISO-8859-1");
6979 snprintf(tmp, sizeof(tmp), "&#%d;", *cur);
6980 tmp[sizeof(tmp) - 1] = 0;
6981 xmlBufferAdd(buf, (xmlChar *) tmp, -1);
6982 cur++;
6983 base = cur;
6984 continue;
6985 } else if (*cur < 0xE0) {
6986 val = (cur[0]) & 0x1F;
6987 val <<= 6;
6988 val |= (cur[1]) & 0x3F;
6989 l = 2;
6990 } else if (*cur < 0xF0) {
6991 val = (cur[0]) & 0x0F;
6992 val <<= 6;
6993 val |= (cur[1]) & 0x3F;
6994 val <<= 6;
6995 val |= (cur[2]) & 0x3F;
6996 l = 3;
6997 } else if (*cur < 0xF8) {
6998 val = (cur[0]) & 0x07;
6999 val <<= 6;
7000 val |= (cur[1]) & 0x3F;
7001 val <<= 6;
7002 val |= (cur[2]) & 0x3F;
7003 val <<= 6;
7004 val |= (cur[3]) & 0x3F;
7005 l = 4;
7006 }
7007 if ((l == 1) || (!IS_CHAR(val))) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007008 xmlSaveErr(XML_SAVE_CHAR_INVALID, (xmlNodePtr) attr,
7009 NULL);
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007010 if (doc != NULL)
7011 doc->encoding =
7012 xmlStrdup(BAD_CAST "ISO-8859-1");
7013 snprintf(tmp, sizeof(tmp), "&#%d;", *cur);
7014 tmp[sizeof(tmp) - 1] = 0;
7015 xmlBufferAdd(buf, (xmlChar *) tmp, -1);
7016 cur++;
7017 base = cur;
7018 continue;
7019 }
7020 /*
7021 * We could do multiple things here. Just save
7022 * as a char ref
7023 */
7024 snprintf(tmp, sizeof(tmp), "&#x%X;", val);
7025 tmp[sizeof(tmp) - 1] = 0;
7026 xmlBufferAdd(buf, (xmlChar *) tmp, -1);
7027 cur += l;
7028 base = cur;
7029 } else {
7030 cur++;
7031 }
7032 }
7033 if (base != cur)
7034 xmlBufferAdd(buf, base, cur - base);
7035 break;
7036 case XML_ENTITY_REF_NODE:
7037 xmlBufferAdd(buf, BAD_CAST "&", 1);
7038 xmlBufferAdd(buf, children->name,
7039 xmlStrlen(children->name));
7040 xmlBufferAdd(buf, BAD_CAST ";", 1);
7041 break;
7042 default:
7043 /* should not happen unless we have a badly built tree */
7044 break;
7045 }
7046 children = children->next;
Owen Taylor3473f882001-02-23 17:55:21 +00007047 }
7048}
7049
7050/**
7051 * xmlNodeDump:
7052 * @buf: the XML buffer output
7053 * @doc: the document
7054 * @cur: the current node
7055 * @level: the imbrication level for indenting
7056 * @format: is formatting allowed
7057 *
7058 * Dump an XML node, recursive behaviour,children are printed too.
Daniel Veillard7424eb62003-01-24 14:14:52 +00007059 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00007060 * or xmlKeepBlanksDefault(0) was called
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007061 *
7062 * Returns the number of bytes written to the buffer or -1 in case of error
Owen Taylor3473f882001-02-23 17:55:21 +00007063 */
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007064int
Owen Taylor3473f882001-02-23 17:55:21 +00007065xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007066 int format)
7067{
7068 unsigned int use;
7069 int ret;
7070 xmlOutputBufferPtr outbuf;
Owen Taylor3473f882001-02-23 17:55:21 +00007071
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00007072 xmlInitParser();
7073
Owen Taylor3473f882001-02-23 17:55:21 +00007074 if (cur == NULL) {
7075#ifdef DEBUG_TREE
7076 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007077 "xmlNodeDump : node == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007078#endif
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007079 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00007080 }
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007081 if (buf == NULL) {
7082#ifdef DEBUG_TREE
7083 xmlGenericError(xmlGenericErrorContext,
7084 "xmlNodeDump : buf == NULL\n");
7085#endif
7086 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00007087 }
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007088 outbuf = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer));
7089 if (outbuf == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007090 xmlSaveErrMemory("creating buffer");
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007091 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00007092 }
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007093 memset(outbuf, 0, (size_t) sizeof(xmlOutputBuffer));
7094 outbuf->buffer = buf;
7095 outbuf->encoder = NULL;
7096 outbuf->writecallback = NULL;
7097 outbuf->closecallback = NULL;
7098 outbuf->context = NULL;
7099 outbuf->written = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007100
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007101 use = buf->use;
7102 xmlNodeDumpOutput(outbuf, doc, cur, level, format, NULL);
7103 xmlFree(outbuf);
7104 ret = buf->use - use;
7105 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00007106}
7107
7108/**
7109 * xmlElemDump:
7110 * @f: the FILE * for the output
7111 * @doc: the document
7112 * @cur: the current node
7113 *
Daniel Veillardd1640922001-12-17 15:30:10 +00007114 * Dump an XML/HTML node, recursive behaviour, children are printed too.
Owen Taylor3473f882001-02-23 17:55:21 +00007115 */
7116void
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007117xmlElemDump(FILE * f, xmlDocPtr doc, xmlNodePtr cur)
7118{
7119 xmlOutputBufferPtr outbuf;
Owen Taylor3473f882001-02-23 17:55:21 +00007120
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00007121 xmlInitParser();
7122
Owen Taylor3473f882001-02-23 17:55:21 +00007123 if (cur == NULL) {
7124#ifdef DEBUG_TREE
7125 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007126 "xmlElemDump : cur == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007127#endif
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007128 return;
Owen Taylor3473f882001-02-23 17:55:21 +00007129 }
Owen Taylor3473f882001-02-23 17:55:21 +00007130#ifdef DEBUG_TREE
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007131 if (doc == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00007132 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007133 "xmlElemDump : doc == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007134 }
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007135#endif
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007136
7137 outbuf = xmlOutputBufferCreateFile(f, NULL);
7138 if (outbuf == NULL)
7139 return;
7140 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00007141#ifdef LIBXML_HTML_ENABLED
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007142 htmlNodeDumpOutput(outbuf, doc, cur, NULL);
7143#else
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007144 xmlSaveErr(XML_ERR_INTERNAL_ERROR, "HTML support not compiled in\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007145#endif /* LIBXML_HTML_ENABLED */
7146 } else
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007147 xmlNodeDumpOutput(outbuf, doc, cur, 0, 1, NULL);
7148 xmlOutputBufferClose(outbuf);
Owen Taylor3473f882001-02-23 17:55:21 +00007149}
7150
7151/************************************************************************
7152 * *
7153 * Dumping XML tree content to an I/O output buffer *
7154 * *
7155 ************************************************************************/
7156
Daniel Veillard4432df22003-09-28 18:58:27 +00007157#ifdef LIBXML_HTML_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00007158static void
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007159xhtmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
7160 int level, int format, const char *encoding);
Daniel Veillard4432df22003-09-28 18:58:27 +00007161#endif
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007162static void
Owen Taylor3473f882001-02-23 17:55:21 +00007163xmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
7164 int level, int format, const char *encoding);
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007165static void
7166xmlNodeDumpOutputInternal(xmlOutputBufferPtr buf, xmlDocPtr doc,
7167 xmlNodePtr cur, int level, int format, const char *encoding);
7168
Daniel Veillard5ecaf7f2003-01-09 13:19:33 +00007169void xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur);
7170
Owen Taylor3473f882001-02-23 17:55:21 +00007171/**
7172 * xmlNsDumpOutput:
7173 * @buf: the XML buffer output
7174 * @cur: a namespace
7175 *
7176 * Dump a local Namespace definition.
7177 * Should be called in the context of attributes dumps.
7178 */
7179static void
7180xmlNsDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) {
7181 if (cur == NULL) {
7182#ifdef DEBUG_TREE
7183 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007184 "xmlNsDumpOutput : Ns == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007185#endif
7186 return;
7187 }
7188 if ((cur->type == XML_LOCAL_NAMESPACE) && (cur->href != NULL)) {
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00007189 if (xmlStrEqual(cur->prefix, BAD_CAST "xml"))
7190 return;
7191
Owen Taylor3473f882001-02-23 17:55:21 +00007192 /* Within the context of an element attributes */
7193 if (cur->prefix != NULL) {
7194 xmlOutputBufferWriteString(buf, " xmlns:");
7195 xmlOutputBufferWriteString(buf, (const char *)cur->prefix);
7196 } else
7197 xmlOutputBufferWriteString(buf, " xmlns");
7198 xmlOutputBufferWriteString(buf, "=");
7199 xmlBufferWriteQuotedString(buf->buffer, cur->href);
7200 }
7201}
7202
7203/**
7204 * xmlNsListDumpOutput:
7205 * @buf: the XML buffer output
7206 * @cur: the first namespace
7207 *
7208 * Dump a list of local Namespace definitions.
7209 * Should be called in the context of attributes dumps.
7210 */
Daniel Veillard5ecaf7f2003-01-09 13:19:33 +00007211void
Owen Taylor3473f882001-02-23 17:55:21 +00007212xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) {
7213 while (cur != NULL) {
7214 xmlNsDumpOutput(buf, cur);
7215 cur = cur->next;
7216 }
7217}
7218
7219/**
7220 * xmlDtdDumpOutput:
7221 * @buf: the XML buffer output
7222 * @doc: the document
7223 * @encoding: an optional encoding string
7224 *
7225 * Dump the XML document DTD, if any.
7226 */
7227static void
7228xmlDtdDumpOutput(xmlOutputBufferPtr buf, xmlDtdPtr dtd, const char *encoding) {
7229 if (dtd == NULL) {
7230#ifdef DEBUG_TREE
7231 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007232 "xmlDtdDumpOutput : no internal subset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007233#endif
7234 return;
7235 }
7236 xmlOutputBufferWriteString(buf, "<!DOCTYPE ");
7237 xmlOutputBufferWriteString(buf, (const char *)dtd->name);
7238 if (dtd->ExternalID != NULL) {
7239 xmlOutputBufferWriteString(buf, " PUBLIC ");
7240 xmlBufferWriteQuotedString(buf->buffer, dtd->ExternalID);
7241 xmlOutputBufferWriteString(buf, " ");
7242 xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID);
7243 } else if (dtd->SystemID != NULL) {
7244 xmlOutputBufferWriteString(buf, " SYSTEM ");
7245 xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID);
7246 }
7247 if ((dtd->entities == NULL) && (dtd->elements == NULL) &&
7248 (dtd->attributes == NULL) && (dtd->notations == NULL)) {
7249 xmlOutputBufferWriteString(buf, ">");
7250 return;
7251 }
7252 xmlOutputBufferWriteString(buf, " [\n");
7253 xmlNodeListDumpOutput(buf, dtd->doc, dtd->children, -1, 0, encoding);
7254 xmlOutputBufferWriteString(buf, "]>");
7255}
7256
7257/**
7258 * xmlAttrDumpOutput:
7259 * @buf: the XML buffer output
7260 * @doc: the document
7261 * @cur: the attribute pointer
7262 * @encoding: an optional encoding string
7263 *
7264 * Dump an XML attribute
7265 */
7266static void
7267xmlAttrDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00007268 const char *encoding ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00007269 if (cur == NULL) {
7270#ifdef DEBUG_TREE
7271 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007272 "xmlAttrDumpOutput : property == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007273#endif
7274 return;
7275 }
7276 xmlOutputBufferWriteString(buf, " ");
7277 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
7278 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
7279 xmlOutputBufferWriteString(buf, ":");
7280 }
7281 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillarda6d05382002-02-13 13:07:41 +00007282 xmlOutputBufferWriteString(buf, "=\"");
7283 xmlAttrSerializeContent(buf->buffer, doc, cur);
7284 xmlOutputBufferWriteString(buf, "\"");
Owen Taylor3473f882001-02-23 17:55:21 +00007285}
7286
7287/**
7288 * xmlAttrListDumpOutput:
7289 * @buf: the XML buffer output
7290 * @doc: the document
7291 * @cur: the first attribute pointer
7292 * @encoding: an optional encoding string
7293 *
7294 * Dump a list of XML attributes
7295 */
7296static void
7297xmlAttrListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
7298 xmlAttrPtr cur, const char *encoding) {
7299 if (cur == NULL) {
7300#ifdef DEBUG_TREE
7301 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007302 "xmlAttrListDumpOutput : property == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007303#endif
7304 return;
7305 }
7306 while (cur != NULL) {
7307 xmlAttrDumpOutput(buf, doc, cur, encoding);
7308 cur = cur->next;
7309 }
7310}
7311
7312
7313
7314/**
7315 * xmlNodeListDumpOutput:
7316 * @buf: the XML buffer output
7317 * @doc: the document
7318 * @cur: the first node
7319 * @level: the imbrication level for indenting
7320 * @format: is formatting allowed
7321 * @encoding: an optional encoding string
7322 *
Daniel Veillardd1640922001-12-17 15:30:10 +00007323 * Dump an XML node list, recursive behaviour, children are printed too.
Daniel Veillard7424eb62003-01-24 14:14:52 +00007324 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00007325 * or xmlKeepBlanksDefault(0) was called
Owen Taylor3473f882001-02-23 17:55:21 +00007326 */
7327static void
7328xmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
7329 xmlNodePtr cur, int level, int format, const char *encoding) {
7330 int i;
7331
7332 if (cur == NULL) {
7333#ifdef DEBUG_TREE
7334 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007335 "xmlNodeListDumpOutput : node == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007336#endif
7337 return;
7338 }
7339 while (cur != NULL) {
7340 if ((format) && (xmlIndentTreeOutput) &&
7341 (cur->type == XML_ELEMENT_NODE))
7342 for (i = 0;i < level;i++)
Aleksey Sanin23002562002-05-24 07:18:40 +00007343 xmlOutputBufferWriteString(buf, xmlTreeIndentString);
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007344 xmlNodeDumpOutputInternal(buf, doc, cur, level, format, encoding);
Owen Taylor3473f882001-02-23 17:55:21 +00007345 if (format) {
7346 xmlOutputBufferWriteString(buf, "\n");
7347 }
7348 cur = cur->next;
7349 }
7350}
7351
7352/**
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007353 * xmlNodeDumpOutputInternal:
Owen Taylor3473f882001-02-23 17:55:21 +00007354 * @buf: the XML buffer output
7355 * @doc: the document
7356 * @cur: the current node
7357 * @level: the imbrication level for indenting
7358 * @format: is formatting allowed
7359 * @encoding: an optional encoding string
7360 *
Daniel Veillardd1640922001-12-17 15:30:10 +00007361 * Dump an XML node, recursive behaviour, children are printed too.
Daniel Veillard7424eb62003-01-24 14:14:52 +00007362 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00007363 * or xmlKeepBlanksDefault(0) was called
Owen Taylor3473f882001-02-23 17:55:21 +00007364 */
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007365static void
7366xmlNodeDumpOutputInternal(xmlOutputBufferPtr buf, xmlDocPtr doc,
7367 xmlNodePtr cur, int level, int format, const char *encoding) {
Owen Taylor3473f882001-02-23 17:55:21 +00007368 int i;
7369 xmlNodePtr tmp;
Daniel Veillard9475a352003-09-26 12:47:50 +00007370 xmlChar *start, *end;
Owen Taylor3473f882001-02-23 17:55:21 +00007371
7372 if (cur == NULL) {
7373#ifdef DEBUG_TREE
7374 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007375 "xmlNodeDumpOutput : node == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007376#endif
7377 return;
7378 }
7379 if (cur->type == XML_XINCLUDE_START)
7380 return;
7381 if (cur->type == XML_XINCLUDE_END)
7382 return;
7383 if (cur->type == XML_DTD_NODE) {
7384 xmlDtdDumpOutput(buf, (xmlDtdPtr) cur, encoding);
7385 return;
7386 }
7387 if (cur->type == XML_ELEMENT_DECL) {
7388 xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur);
7389 return;
7390 }
7391 if (cur->type == XML_ATTRIBUTE_DECL) {
7392 xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur);
7393 return;
7394 }
7395 if (cur->type == XML_ENTITY_DECL) {
7396 xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur);
7397 return;
7398 }
7399 if (cur->type == XML_TEXT_NODE) {
7400 if (cur->content != NULL) {
7401 if ((cur->name == xmlStringText) ||
7402 (cur->name != xmlStringTextNoenc)) {
7403 xmlChar *buffer;
7404
Owen Taylor3473f882001-02-23 17:55:21 +00007405 if (encoding == NULL)
7406 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
7407 else
7408 buffer = xmlEncodeSpecialChars(doc, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00007409 if (buffer != NULL) {
7410 xmlOutputBufferWriteString(buf, (const char *)buffer);
7411 xmlFree(buffer);
7412 }
7413 } else {
7414 /*
7415 * Disable escaping, needed for XSLT
7416 */
Owen Taylor3473f882001-02-23 17:55:21 +00007417 xmlOutputBufferWriteString(buf, (const char *) cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00007418 }
7419 }
7420
7421 return;
7422 }
7423 if (cur->type == XML_PI_NODE) {
7424 if (cur->content != NULL) {
7425 xmlOutputBufferWriteString(buf, "<?");
7426 xmlOutputBufferWriteString(buf, (const char *)cur->name);
7427 if (cur->content != NULL) {
7428 xmlOutputBufferWriteString(buf, " ");
Owen Taylor3473f882001-02-23 17:55:21 +00007429 xmlOutputBufferWriteString(buf, (const char *)cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00007430 }
7431 xmlOutputBufferWriteString(buf, "?>");
7432 } else {
7433 xmlOutputBufferWriteString(buf, "<?");
7434 xmlOutputBufferWriteString(buf, (const char *)cur->name);
7435 xmlOutputBufferWriteString(buf, "?>");
7436 }
7437 return;
7438 }
7439 if (cur->type == XML_COMMENT_NODE) {
7440 if (cur->content != NULL) {
7441 xmlOutputBufferWriteString(buf, "<!--");
Owen Taylor3473f882001-02-23 17:55:21 +00007442 xmlOutputBufferWriteString(buf, (const char *)cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00007443 xmlOutputBufferWriteString(buf, "-->");
7444 }
7445 return;
7446 }
7447 if (cur->type == XML_ENTITY_REF_NODE) {
7448 xmlOutputBufferWriteString(buf, "&");
7449 xmlOutputBufferWriteString(buf, (const char *)cur->name);
7450 xmlOutputBufferWriteString(buf, ";");
7451 return;
7452 }
7453 if (cur->type == XML_CDATA_SECTION_NODE) {
Daniel Veillard9475a352003-09-26 12:47:50 +00007454 start = end = cur->content;
7455 while (*end != '\0') {
7456 if ((*end == ']') && (*(end + 1) == ']') && (*(end + 2) == '>')) {
7457 end = end + 2;
7458 xmlOutputBufferWriteString(buf, "<![CDATA[");
7459 xmlOutputBufferWrite(buf, end - start, (const char *)start);
7460 xmlOutputBufferWriteString(buf, "]]>");
7461 start = end;
7462 }
7463 end++;
7464 }
7465 if (start != end) {
7466 xmlOutputBufferWriteString(buf, "<![CDATA[");
7467 xmlOutputBufferWriteString(buf, (const char *)start);
7468 xmlOutputBufferWriteString(buf, "]]>");
7469 }
Owen Taylor3473f882001-02-23 17:55:21 +00007470 return;
7471 }
Daniel Veillard7b72ee52003-02-27 23:24:53 +00007472 if (cur->type == XML_ATTRIBUTE_NODE) {
Daniel Veillarda1a9d042003-03-18 16:53:17 +00007473 xmlAttrDumpOutput(buf,doc, (xmlAttrPtr) cur,encoding);
Daniel Veillard7b72ee52003-02-27 23:24:53 +00007474 return;
7475 }
Daniel Veillard6aa2f602003-02-10 00:01:56 +00007476 if (cur->type == XML_NAMESPACE_DECL) {
Daniel Veillardfd7ce5f2003-02-10 16:12:39 +00007477 xmlNsDumpOutput(buf, (xmlNsPtr) cur);
Daniel Veillard6aa2f602003-02-10 00:01:56 +00007478 return;
7479 }
Owen Taylor3473f882001-02-23 17:55:21 +00007480
7481 if (format == 1) {
7482 tmp = cur->children;
7483 while (tmp != NULL) {
7484 if ((tmp->type == XML_TEXT_NODE) ||
7485 (tmp->type == XML_ENTITY_REF_NODE)) {
7486 format = 0;
7487 break;
7488 }
7489 tmp = tmp->next;
7490 }
7491 }
7492 xmlOutputBufferWriteString(buf, "<");
7493 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
7494 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
7495 xmlOutputBufferWriteString(buf, ":");
7496 }
7497
7498 xmlOutputBufferWriteString(buf, (const char *)cur->name);
7499 if (cur->nsDef)
7500 xmlNsListDumpOutput(buf, cur->nsDef);
7501 if (cur->properties != NULL)
7502 xmlAttrListDumpOutput(buf, doc, cur->properties, encoding);
7503
Daniel Veillard7db37732001-07-12 01:20:08 +00007504 if (((cur->type == XML_ELEMENT_NODE) || (cur->content == NULL)) &&
7505 (cur->children == NULL) && (!xmlSaveNoEmptyTags)) {
Owen Taylor3473f882001-02-23 17:55:21 +00007506 xmlOutputBufferWriteString(buf, "/>");
7507 return;
7508 }
7509 xmlOutputBufferWriteString(buf, ">");
Daniel Veillard7db37732001-07-12 01:20:08 +00007510 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00007511 xmlChar *buffer;
7512
Owen Taylor3473f882001-02-23 17:55:21 +00007513 if (encoding == NULL)
7514 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
7515 else
7516 buffer = xmlEncodeSpecialChars(doc, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00007517 if (buffer != NULL) {
7518 xmlOutputBufferWriteString(buf, (const char *)buffer);
7519 xmlFree(buffer);
7520 }
7521 }
7522 if (cur->children != NULL) {
7523 if (format) xmlOutputBufferWriteString(buf, "\n");
7524 xmlNodeListDumpOutput(buf, doc, cur->children,
7525 (level >= 0?level+1:-1), format, encoding);
7526 if ((xmlIndentTreeOutput) && (format))
7527 for (i = 0;i < level;i++)
Aleksey Sanin23002562002-05-24 07:18:40 +00007528 xmlOutputBufferWriteString(buf, xmlTreeIndentString);
Owen Taylor3473f882001-02-23 17:55:21 +00007529 }
7530 xmlOutputBufferWriteString(buf, "</");
7531 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
7532 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
7533 xmlOutputBufferWriteString(buf, ":");
7534 }
7535
7536 xmlOutputBufferWriteString(buf, (const char *)cur->name);
7537 xmlOutputBufferWriteString(buf, ">");
7538}
7539
7540/**
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007541 * xmlNodeDumpOutput:
7542 * @buf: the XML buffer output
7543 * @doc: the document
7544 * @cur: the current node
7545 * @level: the imbrication level for indenting
7546 * @format: is formatting allowed
7547 * @encoding: an optional encoding string
7548 *
7549 * Dump an XML node, recursive behaviour, children are printed too.
Daniel Veillard7424eb62003-01-24 14:14:52 +00007550 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007551 * or xmlKeepBlanksDefault(0) was called
7552 */
7553void
7554xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007555 int level, int format, const char *encoding)
7556{
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007557#ifdef LIBXML_HTML_ENABLED
7558 xmlDtdPtr dtd;
7559 int is_xhtml = 0;
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00007560#endif
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007561
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00007562 xmlInitParser();
7563
7564#ifdef LIBXML_HTML_ENABLED
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007565 dtd = xmlGetIntSubset(doc);
7566 if (dtd != NULL) {
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007567 is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
7568 if (is_xhtml < 0)
7569 is_xhtml = 0;
7570 if ((is_xhtml) && (cur->parent == (xmlNodePtr) doc) &&
7571 (cur->type == XML_ELEMENT_NODE) &&
7572 (xmlStrEqual(cur->name, BAD_CAST "html"))) {
7573 if (encoding != NULL)
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00007574 htmlSetMetaEncoding((htmlDocPtr) doc,
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007575 (const xmlChar *) encoding);
7576 else
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00007577 htmlSetMetaEncoding((htmlDocPtr) doc, BAD_CAST "UTF-8");
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007578 }
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007579 }
7580
7581 if (is_xhtml)
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007582 xhtmlNodeDumpOutput(buf, doc, cur, level, format, encoding);
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007583 else
7584#endif
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007585 xmlNodeDumpOutputInternal(buf, doc, cur, level, format, encoding);
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007586}
7587
7588/**
Owen Taylor3473f882001-02-23 17:55:21 +00007589 * xmlDocContentDumpOutput:
7590 * @buf: the XML buffer output
7591 * @cur: the document
7592 * @encoding: an optional encoding string
7593 * @format: should formatting spaces been added
7594 *
7595 * Dump an XML document.
Daniel Veillard7424eb62003-01-24 14:14:52 +00007596 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00007597 * or xmlKeepBlanksDefault(0) was called
Owen Taylor3473f882001-02-23 17:55:21 +00007598 */
7599static void
7600xmlDocContentDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr cur,
7601 const char *encoding, int format) {
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007602#ifdef LIBXML_HTML_ENABLED
7603 xmlDtdPtr dtd;
7604 int is_xhtml = 0;
7605#endif
7606
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00007607 xmlInitParser();
7608
Owen Taylor3473f882001-02-23 17:55:21 +00007609 xmlOutputBufferWriteString(buf, "<?xml version=");
7610 if (cur->version != NULL)
7611 xmlBufferWriteQuotedString(buf->buffer, cur->version);
7612 else
7613 xmlOutputBufferWriteString(buf, "\"1.0\"");
7614 if (encoding == NULL) {
7615 if (cur->encoding != NULL)
7616 encoding = (const char *) cur->encoding;
7617 else if (cur->charset != XML_CHAR_ENCODING_UTF8)
7618 encoding = xmlGetCharEncodingName((xmlCharEncoding) cur->charset);
7619 }
7620 if (encoding != NULL) {
7621 xmlOutputBufferWriteString(buf, " encoding=");
7622 xmlBufferWriteQuotedString(buf->buffer, (xmlChar *) encoding);
7623 }
7624 switch (cur->standalone) {
7625 case 0:
7626 xmlOutputBufferWriteString(buf, " standalone=\"no\"");
7627 break;
7628 case 1:
7629 xmlOutputBufferWriteString(buf, " standalone=\"yes\"");
7630 break;
7631 }
7632 xmlOutputBufferWriteString(buf, "?>\n");
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007633
7634#ifdef LIBXML_HTML_ENABLED
7635 dtd = xmlGetIntSubset(cur);
7636 if (dtd != NULL) {
7637 is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
7638 if (is_xhtml < 0) is_xhtml = 0;
7639 }
7640 if (is_xhtml) {
7641 if (encoding != NULL)
7642 htmlSetMetaEncoding(cur, (const xmlChar *) encoding);
7643 else
7644 htmlSetMetaEncoding(cur, BAD_CAST "UTF-8");
7645 }
7646#endif
Owen Taylor3473f882001-02-23 17:55:21 +00007647 if (cur->children != NULL) {
7648 xmlNodePtr child = cur->children;
7649
7650 while (child != NULL) {
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007651#ifdef LIBXML_HTML_ENABLED
7652 if (is_xhtml)
7653 xhtmlNodeDumpOutput(buf, cur, child, 0, format, encoding);
7654 else
7655#endif
7656 xmlNodeDumpOutputInternal(buf, cur, child, 0, format, encoding);
Owen Taylor3473f882001-02-23 17:55:21 +00007657 xmlOutputBufferWriteString(buf, "\n");
7658 child = child->next;
7659 }
7660 }
7661}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007662#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00007663
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007664#ifdef LIBXML_HTML_ENABLED
7665/************************************************************************
7666 * *
7667 * Functions specific to XHTML serialization *
7668 * *
7669 ************************************************************************/
7670
7671#define XHTML_STRICT_PUBLIC_ID BAD_CAST \
7672 "-//W3C//DTD XHTML 1.0 Strict//EN"
7673#define XHTML_STRICT_SYSTEM_ID BAD_CAST \
7674 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
7675#define XHTML_FRAME_PUBLIC_ID BAD_CAST \
7676 "-//W3C//DTD XHTML 1.0 Frameset//EN"
7677#define XHTML_FRAME_SYSTEM_ID BAD_CAST \
7678 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"
7679#define XHTML_TRANS_PUBLIC_ID BAD_CAST \
7680 "-//W3C//DTD XHTML 1.0 Transitional//EN"
7681#define XHTML_TRANS_SYSTEM_ID BAD_CAST \
7682 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
7683
7684#define XHTML_NS_NAME BAD_CAST "http://www.w3.org/1999/xhtml"
7685/**
7686 * xmlIsXHTML:
7687 * @systemID: the system identifier
7688 * @publicID: the public identifier
7689 *
7690 * Try to find if the document correspond to an XHTML DTD
7691 *
7692 * Returns 1 if true, 0 if not and -1 in case of error
7693 */
7694int
7695xmlIsXHTML(const xmlChar *systemID, const xmlChar *publicID) {
7696 if ((systemID == NULL) && (publicID == NULL))
7697 return(-1);
7698 if (publicID != NULL) {
7699 if (xmlStrEqual(publicID, XHTML_STRICT_PUBLIC_ID)) return(1);
7700 if (xmlStrEqual(publicID, XHTML_FRAME_PUBLIC_ID)) return(1);
7701 if (xmlStrEqual(publicID, XHTML_TRANS_PUBLIC_ID)) return(1);
7702 }
7703 if (systemID != NULL) {
7704 if (xmlStrEqual(systemID, XHTML_STRICT_SYSTEM_ID)) return(1);
7705 if (xmlStrEqual(systemID, XHTML_FRAME_SYSTEM_ID)) return(1);
7706 if (xmlStrEqual(systemID, XHTML_TRANS_SYSTEM_ID)) return(1);
7707 }
7708 return(0);
7709}
7710
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007711#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007712/**
7713 * xhtmlIsEmpty:
7714 * @node: the node
7715 *
7716 * Check if a node is an empty xhtml node
7717 *
7718 * Returns 1 if the node is an empty node, 0 if not and -1 in case of error
7719 */
7720static int
7721xhtmlIsEmpty(xmlNodePtr node) {
7722 if (node == NULL)
7723 return(-1);
7724 if (node->type != XML_ELEMENT_NODE)
7725 return(0);
7726 if ((node->ns != NULL) && (!xmlStrEqual(node->ns->href, XHTML_NS_NAME)))
7727 return(0);
7728 if (node->children != NULL)
7729 return(0);
7730 switch (node->name[0]) {
7731 case 'a':
7732 if (xmlStrEqual(node->name, BAD_CAST "area"))
7733 return(1);
7734 return(0);
7735 case 'b':
7736 if (xmlStrEqual(node->name, BAD_CAST "br"))
7737 return(1);
7738 if (xmlStrEqual(node->name, BAD_CAST "base"))
7739 return(1);
7740 if (xmlStrEqual(node->name, BAD_CAST "basefont"))
7741 return(1);
7742 return(0);
7743 case 'c':
7744 if (xmlStrEqual(node->name, BAD_CAST "col"))
7745 return(1);
7746 return(0);
7747 case 'f':
7748 if (xmlStrEqual(node->name, BAD_CAST "frame"))
7749 return(1);
7750 return(0);
7751 case 'h':
7752 if (xmlStrEqual(node->name, BAD_CAST "hr"))
7753 return(1);
7754 return(0);
7755 case 'i':
7756 if (xmlStrEqual(node->name, BAD_CAST "img"))
7757 return(1);
7758 if (xmlStrEqual(node->name, BAD_CAST "input"))
7759 return(1);
7760 if (xmlStrEqual(node->name, BAD_CAST "isindex"))
7761 return(1);
7762 return(0);
7763 case 'l':
7764 if (xmlStrEqual(node->name, BAD_CAST "link"))
7765 return(1);
7766 return(0);
7767 case 'm':
7768 if (xmlStrEqual(node->name, BAD_CAST "meta"))
7769 return(1);
7770 return(0);
7771 case 'p':
7772 if (xmlStrEqual(node->name, BAD_CAST "param"))
7773 return(1);
7774 return(0);
7775 }
7776 return(0);
7777}
7778
7779/**
7780 * xhtmlAttrListDumpOutput:
7781 * @buf: the XML buffer output
7782 * @doc: the document
7783 * @cur: the first attribute pointer
7784 * @encoding: an optional encoding string
7785 *
7786 * Dump a list of XML attributes
7787 */
7788static void
7789xhtmlAttrListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
7790 xmlAttrPtr cur, const char *encoding) {
7791 xmlAttrPtr xml_lang = NULL;
7792 xmlAttrPtr lang = NULL;
7793 xmlAttrPtr name = NULL;
7794 xmlAttrPtr id = NULL;
Daniel Veillardfd7ce5f2003-02-10 16:12:39 +00007795 xmlNodePtr parent;
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007796
7797 if (cur == NULL) {
7798#ifdef DEBUG_TREE
7799 xmlGenericError(xmlGenericErrorContext,
7800 "xmlAttrListDumpOutput : property == NULL\n");
7801#endif
7802 return;
7803 }
Daniel Veillardfd7ce5f2003-02-10 16:12:39 +00007804 parent = cur->parent;
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007805 while (cur != NULL) {
7806 if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "id")))
7807 id = cur;
7808 else
7809 if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "name")))
7810 name = cur;
7811 else
7812 if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")))
7813 lang = cur;
7814 else
7815 if ((cur->ns != NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")) &&
7816 (xmlStrEqual(cur->ns->prefix, BAD_CAST "xml")))
7817 xml_lang = cur;
7818 else if ((cur->ns == NULL) &&
7819 ((cur->children == NULL) ||
7820 (cur->children->content == NULL) ||
7821 (cur->children->content[0] == 0)) &&
7822 (htmlIsBooleanAttr(cur->name))) {
7823 if (cur->children != NULL)
7824 xmlFreeNode(cur->children);
7825 cur->children = xmlNewText(cur->name);
7826 if (cur->children != NULL)
7827 cur->children->parent = (xmlNodePtr) cur;
7828 }
7829 xmlAttrDumpOutput(buf, doc, cur, encoding);
7830 cur = cur->next;
7831 }
7832 /*
7833 * C.8
7834 */
7835 if ((name != NULL) && (id == NULL)) {
Daniel Veillardfd7ce5f2003-02-10 16:12:39 +00007836 if ((parent != NULL) && (parent->name != NULL) &&
7837 ((xmlStrEqual(parent->name, BAD_CAST "a")) ||
7838 (xmlStrEqual(parent->name, BAD_CAST "p")) ||
7839 (xmlStrEqual(parent->name, BAD_CAST "div")) ||
7840 (xmlStrEqual(parent->name, BAD_CAST "img")) ||
7841 (xmlStrEqual(parent->name, BAD_CAST "map")) ||
7842 (xmlStrEqual(parent->name, BAD_CAST "applet")) ||
7843 (xmlStrEqual(parent->name, BAD_CAST "form")) ||
7844 (xmlStrEqual(parent->name, BAD_CAST "frame")) ||
7845 (xmlStrEqual(parent->name, BAD_CAST "iframe")))) {
7846 xmlOutputBufferWriteString(buf, " id=\"");
7847 xmlAttrSerializeContent(buf->buffer, doc, name);
7848 xmlOutputBufferWriteString(buf, "\"");
7849 }
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007850 }
7851 /*
7852 * C.7.
7853 */
7854 if ((lang != NULL) && (xml_lang == NULL)) {
7855 xmlOutputBufferWriteString(buf, " xml:lang=\"");
7856 xmlAttrSerializeContent(buf->buffer, doc, lang);
7857 xmlOutputBufferWriteString(buf, "\"");
7858 } else
7859 if ((xml_lang != NULL) && (lang == NULL)) {
7860 xmlOutputBufferWriteString(buf, " lang=\"");
7861 xmlAttrSerializeContent(buf->buffer, doc, xml_lang);
7862 xmlOutputBufferWriteString(buf, "\"");
7863 }
7864}
7865
7866/**
7867 * xhtmlNodeListDumpOutput:
7868 * @buf: the XML buffer output
7869 * @doc: the XHTML document
7870 * @cur: the first node
7871 * @level: the imbrication level for indenting
7872 * @format: is formatting allowed
7873 * @encoding: an optional encoding string
7874 *
7875 * Dump an XML node list, recursive behaviour, children are printed too.
Daniel Veillard7424eb62003-01-24 14:14:52 +00007876 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007877 * or xmlKeepBlanksDefault(0) was called
7878 */
7879static void
7880xhtmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
7881 xmlNodePtr cur, int level, int format, const char *encoding) {
7882 int i;
7883
7884 if (cur == NULL) {
7885#ifdef DEBUG_TREE
7886 xmlGenericError(xmlGenericErrorContext,
7887 "xhtmlNodeListDumpOutput : node == NULL\n");
7888#endif
7889 return;
7890 }
7891 while (cur != NULL) {
7892 if ((format) && (xmlIndentTreeOutput) &&
7893 (cur->type == XML_ELEMENT_NODE))
7894 for (i = 0;i < level;i++)
7895 xmlOutputBufferWriteString(buf, xmlTreeIndentString);
7896 xhtmlNodeDumpOutput(buf, doc, cur, level, format, encoding);
7897 if (format) {
7898 xmlOutputBufferWriteString(buf, "\n");
7899 }
7900 cur = cur->next;
7901 }
7902}
7903
7904/**
7905 * xhtmlNodeDumpOutput:
7906 * @buf: the XML buffer output
7907 * @doc: the XHTML document
7908 * @cur: the current node
7909 * @level: the imbrication level for indenting
7910 * @format: is formatting allowed
7911 * @encoding: an optional encoding string
7912 *
7913 * Dump an XHTML node, recursive behaviour, children are printed too.
Daniel Veillard7424eb62003-01-24 14:14:52 +00007914 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007915 * or xmlKeepBlanksDefault(0) was called
7916 */
7917static void
7918xhtmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
7919 int level, int format, const char *encoding) {
7920 int i;
7921 xmlNodePtr tmp;
Daniel Veillard9475a352003-09-26 12:47:50 +00007922 xmlChar *start, *end;
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007923
7924 if (cur == NULL) {
7925#ifdef DEBUG_TREE
7926 xmlGenericError(xmlGenericErrorContext,
7927 "xmlNodeDumpOutput : node == NULL\n");
7928#endif
7929 return;
7930 }
7931 if (cur->type == XML_XINCLUDE_START)
7932 return;
7933 if (cur->type == XML_XINCLUDE_END)
7934 return;
7935 if (cur->type == XML_DTD_NODE) {
7936 xmlDtdDumpOutput(buf, (xmlDtdPtr) cur, encoding);
7937 return;
7938 }
7939 if (cur->type == XML_ELEMENT_DECL) {
7940 xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur);
7941 return;
7942 }
7943 if (cur->type == XML_ATTRIBUTE_DECL) {
7944 xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur);
7945 return;
7946 }
7947 if (cur->type == XML_ENTITY_DECL) {
7948 xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur);
7949 return;
7950 }
7951 if (cur->type == XML_TEXT_NODE) {
7952 if (cur->content != NULL) {
7953 if ((cur->name == xmlStringText) ||
7954 (cur->name != xmlStringTextNoenc)) {
7955 xmlChar *buffer;
7956
7957 if (encoding == NULL)
7958 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
7959 else
7960 buffer = xmlEncodeSpecialChars(doc, cur->content);
7961 if (buffer != NULL) {
7962 xmlOutputBufferWriteString(buf, (const char *)buffer);
7963 xmlFree(buffer);
7964 }
7965 } else {
7966 /*
7967 * Disable escaping, needed for XSLT
7968 */
7969 xmlOutputBufferWriteString(buf, (const char *) cur->content);
7970 }
7971 }
7972
7973 return;
7974 }
7975 if (cur->type == XML_PI_NODE) {
7976 if (cur->content != NULL) {
7977 xmlOutputBufferWriteString(buf, "<?");
7978 xmlOutputBufferWriteString(buf, (const char *)cur->name);
7979 if (cur->content != NULL) {
7980 xmlOutputBufferWriteString(buf, " ");
7981 xmlOutputBufferWriteString(buf, (const char *)cur->content);
7982 }
7983 xmlOutputBufferWriteString(buf, "?>");
7984 } else {
7985 xmlOutputBufferWriteString(buf, "<?");
7986 xmlOutputBufferWriteString(buf, (const char *)cur->name);
7987 xmlOutputBufferWriteString(buf, "?>");
7988 }
7989 return;
7990 }
7991 if (cur->type == XML_COMMENT_NODE) {
7992 if (cur->content != NULL) {
7993 xmlOutputBufferWriteString(buf, "<!--");
7994 xmlOutputBufferWriteString(buf, (const char *)cur->content);
7995 xmlOutputBufferWriteString(buf, "-->");
7996 }
7997 return;
7998 }
7999 if (cur->type == XML_ENTITY_REF_NODE) {
8000 xmlOutputBufferWriteString(buf, "&");
8001 xmlOutputBufferWriteString(buf, (const char *)cur->name);
8002 xmlOutputBufferWriteString(buf, ";");
8003 return;
8004 }
8005 if (cur->type == XML_CDATA_SECTION_NODE) {
Daniel Veillard9475a352003-09-26 12:47:50 +00008006 start = end = cur->content;
8007 while (*end != '\0') {
8008 if (*end == ']' && *(end + 1) == ']' && *(end + 2) == '>') {
8009 end = end + 2;
8010 xmlOutputBufferWriteString(buf, "<![CDATA[");
8011 xmlOutputBufferWrite(buf, end - start, (const char *)start);
8012 xmlOutputBufferWriteString(buf, "]]>");
8013 start = end;
8014 }
8015 end++;
8016 }
8017 if (start != end) {
8018 xmlOutputBufferWriteString(buf, "<![CDATA[");
8019 xmlOutputBufferWriteString(buf, (const char *)start);
8020 xmlOutputBufferWriteString(buf, "]]>");
8021 }
Daniel Veillardd5c2f922002-11-21 14:10:52 +00008022 return;
8023 }
8024
8025 if (format == 1) {
8026 tmp = cur->children;
8027 while (tmp != NULL) {
8028 if ((tmp->type == XML_TEXT_NODE) ||
8029 (tmp->type == XML_ENTITY_REF_NODE)) {
8030 format = 0;
8031 break;
8032 }
8033 tmp = tmp->next;
8034 }
8035 }
8036 xmlOutputBufferWriteString(buf, "<");
8037 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
8038 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
8039 xmlOutputBufferWriteString(buf, ":");
8040 }
8041
8042 xmlOutputBufferWriteString(buf, (const char *)cur->name);
8043 if (cur->nsDef)
8044 xmlNsListDumpOutput(buf, cur->nsDef);
8045 if ((xmlStrEqual(cur->name, BAD_CAST "html") &&
8046 (cur->ns == NULL) && (cur->nsDef == NULL))) {
8047 /*
8048 * 3.1.1. Strictly Conforming Documents A.3.1.1 3/
8049 */
8050 xmlOutputBufferWriteString(buf,
8051 " xmlns=\"http://www.w3.org/1999/xhtml\"");
8052 }
8053 if (cur->properties != NULL)
8054 xhtmlAttrListDumpOutput(buf, doc, cur->properties, encoding);
8055
8056 if ((cur->type == XML_ELEMENT_NODE) && (cur->children == NULL)) {
8057 if (((cur->ns == NULL) || (cur->ns->prefix == NULL)) &&
8058 (xhtmlIsEmpty(cur) == 1)) {
8059 /*
8060 * C.2. Empty Elements
8061 */
8062 xmlOutputBufferWriteString(buf, " />");
8063 } else {
8064 /*
8065 * C.3. Element Minimization and Empty Element Content
8066 */
8067 xmlOutputBufferWriteString(buf, "></");
8068 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
8069 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
8070 xmlOutputBufferWriteString(buf, ":");
8071 }
8072 xmlOutputBufferWriteString(buf, (const char *)cur->name);
8073 xmlOutputBufferWriteString(buf, ">");
8074 }
8075 return;
8076 }
8077 xmlOutputBufferWriteString(buf, ">");
8078 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
8079 xmlChar *buffer;
8080
8081 if (encoding == NULL)
8082 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
8083 else
8084 buffer = xmlEncodeSpecialChars(doc, cur->content);
8085 if (buffer != NULL) {
8086 xmlOutputBufferWriteString(buf, (const char *)buffer);
8087 xmlFree(buffer);
8088 }
8089 }
8090
8091 /*
8092 * 4.8. Script and Style elements
8093 */
8094 if ((cur->type == XML_ELEMENT_NODE) &&
8095 ((xmlStrEqual(cur->name, BAD_CAST "script")) ||
8096 (xmlStrEqual(cur->name, BAD_CAST "style"))) &&
8097 ((cur->ns == NULL) ||
8098 (xmlStrEqual(cur->ns->href, XHTML_NS_NAME)))) {
8099 xmlNodePtr child = cur->children;
8100
8101 while (child != NULL) {
8102 if ((child->type == XML_TEXT_NODE) ||
8103 (child->type == XML_CDATA_SECTION_NODE)) {
Daniel Veillard64b35282002-12-04 15:10:40 +00008104 /*
8105 * Apparently CDATA escaping for style just break on IE,
8106 * mozilla and galeon, so ...
8107 */
8108 if (xmlStrEqual(cur->name, BAD_CAST "style") &&
8109 (xmlStrchr(child->content, '<') == NULL) &&
8110 (xmlStrchr(child->content, '>') == NULL) &&
8111 (xmlStrchr(child->content, '&') == NULL)) {
8112 xhtmlNodeDumpOutput(buf, doc, child, 0, 0, encoding);
8113 } else {
Daniel Veillard9475a352003-09-26 12:47:50 +00008114 start = end = child->content;
8115 while (*end != '\0') {
8116 if (*end == ']' &&
8117 *(end + 1) == ']' &&
8118 *(end + 2) == '>') {
8119 end = end + 2;
8120 xmlOutputBufferWriteString(buf, "<![CDATA[");
8121 xmlOutputBufferWrite(buf, end - start,
8122 (const char *)start);
8123 xmlOutputBufferWriteString(buf, "]]>");
8124 start = end;
8125 }
8126 end++;
8127 }
8128 if (start != end) {
8129 xmlOutputBufferWriteString(buf, "<![CDATA[");
8130 xmlOutputBufferWriteString(buf, (const char *)start);
8131 xmlOutputBufferWriteString(buf, "]]>");
8132 }
Daniel Veillard64b35282002-12-04 15:10:40 +00008133 }
Daniel Veillardd5c2f922002-11-21 14:10:52 +00008134 } else {
8135 xhtmlNodeDumpOutput(buf, doc, child, 0, 0, encoding);
8136 }
8137 child = child->next;
8138 }
8139 } else if (cur->children != NULL) {
8140 if (format) xmlOutputBufferWriteString(buf, "\n");
8141 xhtmlNodeListDumpOutput(buf, doc, cur->children,
8142 (level >= 0?level+1:-1), format, encoding);
8143 if ((xmlIndentTreeOutput) && (format))
8144 for (i = 0;i < level;i++)
8145 xmlOutputBufferWriteString(buf, xmlTreeIndentString);
8146 }
8147 xmlOutputBufferWriteString(buf, "</");
8148 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
8149 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
8150 xmlOutputBufferWriteString(buf, ":");
8151 }
8152
8153 xmlOutputBufferWriteString(buf, (const char *)cur->name);
8154 xmlOutputBufferWriteString(buf, ">");
8155}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00008156#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardd5c2f922002-11-21 14:10:52 +00008157#endif
8158
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00008159#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00008160/************************************************************************
8161 * *
8162 * Saving functions front-ends *
8163 * *
8164 ************************************************************************/
8165
8166/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +00008167 * xmlDocDumpFormatMemoryEnc:
Owen Taylor3473f882001-02-23 17:55:21 +00008168 * @out_doc: Document to generate XML text from
8169 * @doc_txt_ptr: Memory pointer for allocated XML text
8170 * @doc_txt_len: Length of the generated XML text
8171 * @txt_encoding: Character encoding to use when generating XML text
8172 * @format: should formatting spaces been added
8173 *
8174 * Dump the current DOM tree into memory using the character encoding specified
8175 * by the caller. Note it is up to the caller of this function to free the
Daniel Veillardbd9afb52002-09-25 22:25:35 +00008176 * allocated memory with xmlFree().
Daniel Veillard7424eb62003-01-24 14:14:52 +00008177 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00008178 * or xmlKeepBlanksDefault(0) was called
Owen Taylor3473f882001-02-23 17:55:21 +00008179 */
8180
8181void
8182xmlDocDumpFormatMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00008183 int * doc_txt_len, const char * txt_encoding,
Daniel Veillard1731d6a2001-04-10 16:38:06 +00008184 int format) {
Owen Taylor3473f882001-02-23 17:55:21 +00008185 int dummy = 0;
8186
Owen Taylor3473f882001-02-23 17:55:21 +00008187 xmlOutputBufferPtr out_buff = NULL;
8188 xmlCharEncodingHandlerPtr conv_hdlr = NULL;
8189
8190 if (doc_txt_len == NULL) {
8191 doc_txt_len = &dummy; /* Continue, caller just won't get length */
8192 }
8193
8194 if (doc_txt_ptr == NULL) {
8195 *doc_txt_len = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00008196 return;
8197 }
8198
8199 *doc_txt_ptr = NULL;
8200 *doc_txt_len = 0;
8201
8202 if (out_doc == NULL) {
8203 /* No document, no output */
Owen Taylor3473f882001-02-23 17:55:21 +00008204 return;
8205 }
8206
8207 /*
8208 * Validate the encoding value, if provided.
8209 * This logic is copied from xmlSaveFileEnc.
8210 */
8211
8212 if (txt_encoding == NULL)
8213 txt_encoding = (const char *) out_doc->encoding;
8214 if (txt_encoding != NULL) {
Daniel Veillarde1326112003-06-05 09:32:20 +00008215 conv_hdlr = xmlFindCharEncodingHandler(txt_encoding);
Owen Taylor3473f882001-02-23 17:55:21 +00008216 if ( conv_hdlr == NULL ) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00008217 xmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, (xmlNodePtr) out_doc,
8218 txt_encoding);
Owen Taylor3473f882001-02-23 17:55:21 +00008219 return;
8220 }
8221 }
Owen Taylor3473f882001-02-23 17:55:21 +00008222
8223 if ((out_buff = xmlAllocOutputBuffer(conv_hdlr)) == NULL ) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00008224 xmlSaveErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00008225 return;
8226 }
8227
Daniel Veillard1731d6a2001-04-10 16:38:06 +00008228 xmlDocContentDumpOutput(out_buff, out_doc, txt_encoding, format);
Owen Taylor3473f882001-02-23 17:55:21 +00008229 xmlOutputBufferFlush(out_buff);
8230 if (out_buff->conv != NULL) {
8231 *doc_txt_len = out_buff->conv->use;
8232 *doc_txt_ptr = xmlStrndup(out_buff->conv->content, *doc_txt_len);
8233 } else {
8234 *doc_txt_len = out_buff->buffer->use;
8235 *doc_txt_ptr = xmlStrndup(out_buff->buffer->content, *doc_txt_len);
8236 }
8237 (void)xmlOutputBufferClose(out_buff);
8238
8239 if ((*doc_txt_ptr == NULL) && (*doc_txt_len > 0)) {
8240 *doc_txt_len = 0;
Daniel Veillard18ec16e2003-10-07 23:16:40 +00008241 xmlSaveErrMemory("creating output");
Owen Taylor3473f882001-02-23 17:55:21 +00008242 }
8243
8244 return;
8245}
8246
8247/**
8248 * xmlDocDumpMemory:
8249 * @cur: the document
8250 * @mem: OUT: the memory pointer
Daniel Veillard60087f32001-10-10 09:45:09 +00008251 * @size: OUT: the memory length
Owen Taylor3473f882001-02-23 17:55:21 +00008252 *
Daniel Veillardd1640922001-12-17 15:30:10 +00008253 * Dump an XML document in memory and return the #xmlChar * and it's size.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00008254 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00008255 */
8256void
8257xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) {
8258 xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, 0);
8259}
8260
8261/**
8262 * xmlDocDumpFormatMemory:
8263 * @cur: the document
8264 * @mem: OUT: the memory pointer
Daniel Veillard60087f32001-10-10 09:45:09 +00008265 * @size: OUT: the memory length
Owen Taylor3473f882001-02-23 17:55:21 +00008266 * @format: should formatting spaces been added
8267 *
8268 *
Daniel Veillardd1640922001-12-17 15:30:10 +00008269 * Dump an XML document in memory and return the #xmlChar * and it's size.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00008270 * It's up to the caller to free the memory with xmlFree().
Daniel Veillard7424eb62003-01-24 14:14:52 +00008271 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00008272 * or xmlKeepBlanksDefault(0) was called
Owen Taylor3473f882001-02-23 17:55:21 +00008273 */
8274void
8275xmlDocDumpFormatMemory(xmlDocPtr cur, xmlChar**mem, int *size, int format) {
8276 xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, format);
8277}
8278
8279/**
8280 * xmlDocDumpMemoryEnc:
8281 * @out_doc: Document to generate XML text from
8282 * @doc_txt_ptr: Memory pointer for allocated XML text
8283 * @doc_txt_len: Length of the generated XML text
8284 * @txt_encoding: Character encoding to use when generating XML text
8285 *
8286 * Dump the current DOM tree into memory using the character encoding specified
8287 * by the caller. Note it is up to the caller of this function to free the
Daniel Veillardbd9afb52002-09-25 22:25:35 +00008288 * allocated memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00008289 */
8290
8291void
8292xmlDocDumpMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
8293 int * doc_txt_len, const char * txt_encoding) {
8294 xmlDocDumpFormatMemoryEnc(out_doc, doc_txt_ptr, doc_txt_len,
Daniel Veillard1731d6a2001-04-10 16:38:06 +00008295 txt_encoding, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00008296}
8297
8298/**
Daniel Veillard9e412302002-06-10 15:59:44 +00008299 * xmlDocFormatDump:
Owen Taylor3473f882001-02-23 17:55:21 +00008300 * @f: the FILE*
8301 * @cur: the document
Daniel Veillard9e412302002-06-10 15:59:44 +00008302 * @format: should formatting spaces been added
Owen Taylor3473f882001-02-23 17:55:21 +00008303 *
8304 * Dump an XML document to an open FILE.
8305 *
Daniel Veillardd1640922001-12-17 15:30:10 +00008306 * returns: the number of bytes written or -1 in case of failure.
Daniel Veillard7424eb62003-01-24 14:14:52 +00008307 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
8308 * or xmlKeepBlanksDefault(0) was called
Owen Taylor3473f882001-02-23 17:55:21 +00008309 */
8310int
Daniel Veillard9e412302002-06-10 15:59:44 +00008311xmlDocFormatDump(FILE *f, xmlDocPtr cur, int format) {
Owen Taylor3473f882001-02-23 17:55:21 +00008312 xmlOutputBufferPtr buf;
8313 const char * encoding;
8314 xmlCharEncodingHandlerPtr handler = NULL;
8315 int ret;
8316
8317 if (cur == NULL) {
8318#ifdef DEBUG_TREE
8319 xmlGenericError(xmlGenericErrorContext,
8320 "xmlDocDump : document == NULL\n");
8321#endif
8322 return(-1);
8323 }
8324 encoding = (const char *) cur->encoding;
8325
8326 if (encoding != NULL) {
Daniel Veillarde1326112003-06-05 09:32:20 +00008327 handler = xmlFindCharEncodingHandler(encoding);
Owen Taylor3473f882001-02-23 17:55:21 +00008328 if (handler == NULL) {
8329 xmlFree((char *) cur->encoding);
8330 cur->encoding = NULL;
8331 }
8332 }
Owen Taylor3473f882001-02-23 17:55:21 +00008333 buf = xmlOutputBufferCreateFile(f, handler);
8334 if (buf == NULL) return(-1);
Daniel Veillard9e412302002-06-10 15:59:44 +00008335 xmlDocContentDumpOutput(buf, cur, NULL, format);
Owen Taylor3473f882001-02-23 17:55:21 +00008336
8337 ret = xmlOutputBufferClose(buf);
8338 return(ret);
8339}
8340
8341/**
Daniel Veillard9e412302002-06-10 15:59:44 +00008342 * xmlDocDump:
8343 * @f: the FILE*
8344 * @cur: the document
8345 *
8346 * Dump an XML document to an open FILE.
8347 *
8348 * returns: the number of bytes written or -1 in case of failure.
8349 */
8350int
8351xmlDocDump(FILE *f, xmlDocPtr cur) {
Daniel Veillard828ce832003-10-08 19:19:10 +00008352 return(xmlDocFormatDump (f, cur, 0));
Daniel Veillard9e412302002-06-10 15:59:44 +00008353}
8354
8355/**
Owen Taylor3473f882001-02-23 17:55:21 +00008356 * xmlSaveFileTo:
8357 * @buf: an output I/O buffer
8358 * @cur: the document
Daniel Veillardd1640922001-12-17 15:30:10 +00008359 * @encoding: the encoding if any assuming the I/O layer handles the trancoding
Owen Taylor3473f882001-02-23 17:55:21 +00008360 *
8361 * Dump an XML document to an I/O buffer.
8362 *
Daniel Veillardd1640922001-12-17 15:30:10 +00008363 * returns: the number of bytes written or -1 in case of failure.
Owen Taylor3473f882001-02-23 17:55:21 +00008364 */
8365int
CET 2001 Daniel Veillard5a37bde2001-11-01 14:31:22 +00008366xmlSaveFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding) {
Owen Taylor3473f882001-02-23 17:55:21 +00008367 int ret;
8368
8369 if (buf == NULL) return(0);
Daniel Veillard1731d6a2001-04-10 16:38:06 +00008370 xmlDocContentDumpOutput(buf, cur, encoding, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00008371 ret = xmlOutputBufferClose(buf);
8372 return(ret);
8373}
8374
8375/**
Daniel Veillardeefd4492001-04-28 16:55:50 +00008376 * xmlSaveFormatFileTo:
8377 * @buf: an output I/O buffer
8378 * @cur: the document
Daniel Veillardd1640922001-12-17 15:30:10 +00008379 * @encoding: the encoding if any assuming the I/O layer handles the trancoding
Daniel Veillardeefd4492001-04-28 16:55:50 +00008380 * @format: should formatting spaces been added
8381 *
8382 * Dump an XML document to an I/O buffer.
8383 *
Daniel Veillardd1640922001-12-17 15:30:10 +00008384 * returns: the number of bytes written or -1 in case of failure.
Daniel Veillard7424eb62003-01-24 14:14:52 +00008385 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
8386 * or xmlKeepBlanksDefault(0) was called
Daniel Veillardeefd4492001-04-28 16:55:50 +00008387 */
8388int
CET 2001 Daniel Veillard5a37bde2001-11-01 14:31:22 +00008389xmlSaveFormatFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding, int format) {
Daniel Veillardeefd4492001-04-28 16:55:50 +00008390 int ret;
8391
8392 if (buf == NULL) return(0);
8393 xmlDocContentDumpOutput(buf, cur, encoding, format);
8394 ret = xmlOutputBufferClose(buf);
8395 return(ret);
8396}
8397
8398/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00008399 * xmlSaveFormatFileEnc:
Daniel Veillardf012a642001-07-23 19:10:52 +00008400 * @filename: the filename or URL to output
8401 * @cur: the document being saved
8402 * @encoding: the name of the encoding to use or NULL.
8403 * @format: should formatting spaces be added.
Daniel Veillardd1640922001-12-17 15:30:10 +00008404 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00008405 * Dump an XML document to a file or an URL.
8406 *
Daniel Veillardd1640922001-12-17 15:30:10 +00008407 * Returns the number of bytes written or -1 in case of error.
Daniel Veillard7424eb62003-01-24 14:14:52 +00008408 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
8409 * or xmlKeepBlanksDefault(0) was called
Owen Taylor3473f882001-02-23 17:55:21 +00008410 */
8411int
Daniel Veillardf012a642001-07-23 19:10:52 +00008412xmlSaveFormatFileEnc( const char * filename, xmlDocPtr cur,
8413 const char * encoding, int format ) {
Owen Taylor3473f882001-02-23 17:55:21 +00008414 xmlOutputBufferPtr buf;
8415 xmlCharEncodingHandlerPtr handler = NULL;
8416 int ret;
8417
Daniel Veillard4dbe77a2003-01-14 00:17:42 +00008418 if (cur == NULL)
8419 return(-1);
8420
Daniel Veillardfb25a512002-01-13 20:32:08 +00008421 if (encoding == NULL)
8422 encoding = (const char *) cur->encoding;
8423
Owen Taylor3473f882001-02-23 17:55:21 +00008424 if (encoding != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00008425
Owen Taylor3473f882001-02-23 17:55:21 +00008426 handler = xmlFindCharEncodingHandler(encoding);
Daniel Veillard81418e32001-05-22 15:08:55 +00008427 if (handler == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00008428 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00008429 }
8430
Daniel Veillardf012a642001-07-23 19:10:52 +00008431#ifdef HAVE_ZLIB_H
8432 if (cur->compression < 0) cur->compression = xmlCompressMode;
8433#endif
Owen Taylor3473f882001-02-23 17:55:21 +00008434 /*
8435 * save the content to a temp buffer.
8436 */
Daniel Veillardf012a642001-07-23 19:10:52 +00008437 buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression);
Owen Taylor3473f882001-02-23 17:55:21 +00008438 if (buf == NULL) return(-1);
8439
Daniel Veillardf012a642001-07-23 19:10:52 +00008440 xmlDocContentDumpOutput(buf, cur, encoding, format);
Owen Taylor3473f882001-02-23 17:55:21 +00008441
8442 ret = xmlOutputBufferClose(buf);
8443 return(ret);
8444}
8445
Daniel Veillardf012a642001-07-23 19:10:52 +00008446
8447/**
8448 * xmlSaveFileEnc:
8449 * @filename: the filename (or URL)
8450 * @cur: the document
8451 * @encoding: the name of an encoding (or NULL)
8452 *
8453 * Dump an XML document, converting it to the given encoding
8454 *
Daniel Veillardd1640922001-12-17 15:30:10 +00008455 * returns: the number of bytes written or -1 in case of failure.
Daniel Veillardf012a642001-07-23 19:10:52 +00008456 */
8457int
8458xmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) {
8459 return ( xmlSaveFormatFileEnc( filename, cur, encoding, 0 ) );
8460}
8461
Owen Taylor3473f882001-02-23 17:55:21 +00008462/**
Daniel Veillard67fee942001-04-26 18:59:03 +00008463 * xmlSaveFormatFile:
Owen Taylor3473f882001-02-23 17:55:21 +00008464 * @filename: the filename (or URL)
8465 * @cur: the document
Daniel Veillard67fee942001-04-26 18:59:03 +00008466 * @format: should formatting spaces been added
Owen Taylor3473f882001-02-23 17:55:21 +00008467 *
8468 * Dump an XML document to a file. Will use compression if
8469 * compiled in and enabled. If @filename is "-" the stdout file is
Daniel Veillardd1640922001-12-17 15:30:10 +00008470 * used. If @format is set then the document will be indented on output.
Daniel Veillard7424eb62003-01-24 14:14:52 +00008471 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
8472 * or xmlKeepBlanksDefault(0) was called
Daniel Veillard67fee942001-04-26 18:59:03 +00008473 *
Daniel Veillardd1640922001-12-17 15:30:10 +00008474 * returns: the number of bytes written or -1 in case of failure.
Owen Taylor3473f882001-02-23 17:55:21 +00008475 */
8476int
Daniel Veillard67fee942001-04-26 18:59:03 +00008477xmlSaveFormatFile(const char *filename, xmlDocPtr cur, int format) {
Daniel Veillardf012a642001-07-23 19:10:52 +00008478 return ( xmlSaveFormatFileEnc( filename, cur, NULL, format ) );
Owen Taylor3473f882001-02-23 17:55:21 +00008479}
8480
Daniel Veillard67fee942001-04-26 18:59:03 +00008481/**
8482 * xmlSaveFile:
8483 * @filename: the filename (or URL)
8484 * @cur: the document
8485 *
8486 * Dump an XML document to a file. Will use compression if
8487 * compiled in and enabled. If @filename is "-" the stdout file is
8488 * used.
Daniel Veillardd1640922001-12-17 15:30:10 +00008489 * returns: the number of bytes written or -1 in case of failure.
Daniel Veillard67fee942001-04-26 18:59:03 +00008490 */
8491int
8492xmlSaveFile(const char *filename, xmlDocPtr cur) {
Daniel Veillardf012a642001-07-23 19:10:52 +00008493 return(xmlSaveFormatFileEnc(filename, cur, NULL, 0));
Daniel Veillard67fee942001-04-26 18:59:03 +00008494}
8495
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00008496#endif /* LIBXML_OUTPUT_ENABLED */
8497
8498/**
8499 * xmlGetDocCompressMode:
8500 * @doc: the document
8501 *
8502 * get the compression ratio for a document, ZLIB based
8503 * Returns 0 (uncompressed) to 9 (max compression)
8504 */
8505int
8506xmlGetDocCompressMode (xmlDocPtr doc) {
8507 if (doc == NULL) return(-1);
8508 return(doc->compression);
8509}
8510
8511/**
8512 * xmlSetDocCompressMode:
8513 * @doc: the document
8514 * @mode: the compression ratio
8515 *
8516 * set the compression ratio for a document, ZLIB based
8517 * Correct values: 0 (uncompressed) to 9 (max compression)
8518 */
8519void
8520xmlSetDocCompressMode (xmlDocPtr doc, int mode) {
8521 if (doc == NULL) return;
8522 if (mode < 0) doc->compression = 0;
8523 else if (mode > 9) doc->compression = 9;
8524 else doc->compression = mode;
8525}
8526
8527/**
8528 * xmlGetCompressMode:
8529 *
8530 * get the default compression mode used, ZLIB based.
8531 * Returns 0 (uncompressed) to 9 (max compression)
8532 */
8533int
8534xmlGetCompressMode(void)
8535{
8536 return (xmlCompressMode);
8537}
8538
8539/**
8540 * xmlSetCompressMode:
8541 * @mode: the compression ratio
8542 *
8543 * set the default compression mode used, ZLIB based
8544 * Correct values: 0 (uncompressed) to 9 (max compression)
8545 */
8546void
8547xmlSetCompressMode(int mode) {
8548 if (mode < 0) xmlCompressMode = 0;
8549 else if (mode > 9) xmlCompressMode = 9;
8550 else xmlCompressMode = mode;
8551}
8552