blob: f90c3d48697b2225a53ec1f46257092ef1f9e225 [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
2 * tree.c : implemetation of access function for an XML tree.
3 *
4 * See Copyright for the status of this software.
5 *
6 * Daniel.Veillard@w3.org
7 *
8 * 14 Nov 2000 ht - Changed the name of function xmlBufferWriteChar under VMS
9 * as it was similar to xmlBufferWriteCHAR when compiling without case
10 * sensitivity.
11 *
12 */
13
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>
31#include <libxml/entities.h>
32#include <libxml/valid.h>
33#include <libxml/xmlerror.h>
Daniel Veillardbdb9ba72001-04-11 11:28:06 +000034#include <libxml/parserInternals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000035
Daniel Veillard56a4cb82001-03-24 17:00:36 +000036xmlNsPtr xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns);
37
38/************************************************************************
39 * *
40 * A few static variables and macros *
41 * *
42 ************************************************************************/
43
Owen Taylor3473f882001-02-23 17:55:21 +000044xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 };
45xmlChar xmlStringTextNoenc[] =
46 { 't', 'e', 'x', 't', 'n', 'o', 'e', 'n', 'c', 0 };
47xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 };
48int oldXMLWDcompatibility = 0;
49int xmlIndentTreeOutput = 0;
50xmlBufferAllocationScheme xmlBufferAllocScheme = XML_BUFFER_ALLOC_EXACT;
51
52static int xmlCompressMode = 0;
53static int xmlCheckDTD = 1;
54int xmlSaveNoEmptyTags = 0;
55
Owen Taylor3473f882001-02-23 17:55:21 +000056#define UPDATE_LAST_CHILD_AND_PARENT(n) if ((n) != NULL) { \
57 xmlNodePtr ulccur = (n)->children; \
58 if (ulccur == NULL) { \
59 (n)->last = NULL; \
60 } else { \
61 while (ulccur->next != NULL) { \
62 ulccur->parent = (n); \
63 ulccur = ulccur->next; \
64 } \
65 ulccur->parent = (n); \
66 (n)->last = ulccur; \
67}}
68
69/* #define DEBUG_BUFFER */
70/* #define DEBUG_TREE */
71
72/************************************************************************
73 * *
74 * Allocation and deallocation of basic structures *
75 * *
76 ************************************************************************/
77
78/**
79 * xmlSetBufferAllocationScheme:
80 * @scheme: allocation method to use
81 *
82 * Set the buffer allocation method. Types are
83 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
84 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
85 * improves performance
86 */
87void
88xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme) {
89 xmlBufferAllocScheme = scheme;
90}
91
92/**
93 * xmlGetBufferAllocationScheme:
94 *
95 * Types are
96 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
97 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
98 * improves performance
99 *
100 * Returns the current allocation scheme
101 */
102xmlBufferAllocationScheme
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000103xmlGetBufferAllocationScheme(void) {
Daniel Veillarde043ee12001-04-16 14:08:07 +0000104 return(xmlBufferAllocScheme);
Owen Taylor3473f882001-02-23 17:55:21 +0000105}
106
107/**
108 * xmlNewNs:
109 * @node: the element carrying the namespace
110 * @href: the URI associated
111 * @prefix: the prefix for the namespace
112 *
113 * Creation of a new Namespace. This function will refuse to create
114 * a namespace with a similar prefix than an existing one present on this
115 * node.
116 * We use href==NULL in the case of an element creation where the namespace
117 * was not defined.
118 * Returns returns a new namespace pointer or NULL
119 */
120xmlNsPtr
121xmlNewNs(xmlNodePtr node, const xmlChar *href, const xmlChar *prefix) {
122 xmlNsPtr cur;
123
124 if ((node != NULL) && (node->type != XML_ELEMENT_NODE))
125 return(NULL);
126
127 /*
128 * Allocate a new Namespace and fill the fields.
129 */
130 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
131 if (cur == NULL) {
132 xmlGenericError(xmlGenericErrorContext,
133 "xmlNewNs : malloc failed\n");
134 return(NULL);
135 }
136 memset(cur, 0, sizeof(xmlNs));
137 cur->type = XML_LOCAL_NAMESPACE;
138
139 if (href != NULL)
140 cur->href = xmlStrdup(href);
141 if (prefix != NULL)
142 cur->prefix = xmlStrdup(prefix);
143
144 /*
145 * Add it at the end to preserve parsing order ...
146 * and checks for existing use of the prefix
147 */
148 if (node != NULL) {
149 if (node->nsDef == NULL) {
150 node->nsDef = cur;
151 } else {
152 xmlNsPtr prev = node->nsDef;
153
154 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
155 (xmlStrEqual(prev->prefix, cur->prefix))) {
156 xmlFreeNs(cur);
157 return(NULL);
158 }
159 while (prev->next != NULL) {
160 prev = prev->next;
161 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
162 (xmlStrEqual(prev->prefix, cur->prefix))) {
163 xmlFreeNs(cur);
164 return(NULL);
165 }
166 }
167 prev->next = cur;
168 }
169 }
170 return(cur);
171}
172
173/**
174 * xmlSetNs:
175 * @node: a node in the document
176 * @ns: a namespace pointer
177 *
178 * Associate a namespace to a node, a posteriori.
179 */
180void
181xmlSetNs(xmlNodePtr node, xmlNsPtr ns) {
182 if (node == NULL) {
183#ifdef DEBUG_TREE
184 xmlGenericError(xmlGenericErrorContext,
185 "xmlSetNs: node == NULL\n");
186#endif
187 return;
188 }
189 node->ns = ns;
190}
191
192/**
193 * xmlFreeNs:
194 * @cur: the namespace pointer
195 *
196 * Free up the structures associated to a namespace
197 */
198void
199xmlFreeNs(xmlNsPtr cur) {
200 if (cur == NULL) {
201#ifdef DEBUG_TREE
202 xmlGenericError(xmlGenericErrorContext,
203 "xmlFreeNs : ns == NULL\n");
204#endif
205 return;
206 }
207 if (cur->href != NULL) xmlFree((char *) cur->href);
208 if (cur->prefix != NULL) xmlFree((char *) cur->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000209 xmlFree(cur);
210}
211
212/**
213 * xmlFreeNsList:
214 * @cur: the first namespace pointer
215 *
216 * Free up all the structures associated to the chained namespaces.
217 */
218void
219xmlFreeNsList(xmlNsPtr cur) {
220 xmlNsPtr next;
221 if (cur == NULL) {
222#ifdef DEBUG_TREE
223 xmlGenericError(xmlGenericErrorContext,
224 "xmlFreeNsList : ns == NULL\n");
225#endif
226 return;
227 }
228 while (cur != NULL) {
229 next = cur->next;
230 xmlFreeNs(cur);
231 cur = next;
232 }
233}
234
235/**
236 * xmlNewDtd:
237 * @doc: the document pointer
238 * @name: the DTD name
239 * @ExternalID: the external ID
240 * @SystemID: the system ID
241 *
242 * Creation of a new DTD for the external subset. To create an
243 * internal subset, use xmlCreateIntSubset().
244 *
245 * Returns a pointer to the new DTD structure
246 */
247xmlDtdPtr
248xmlNewDtd(xmlDocPtr doc, const xmlChar *name,
249 const xmlChar *ExternalID, const xmlChar *SystemID) {
250 xmlDtdPtr cur;
251
252 if ((doc != NULL) && (doc->extSubset != NULL)) {
253#ifdef DEBUG_TREE
254 xmlGenericError(xmlGenericErrorContext,
255 "xmlNewDtd(%s): document %s already have a DTD %s\n",
256 /* !!! */ (char *) name, doc->name,
257 /* !!! */ (char *)doc->extSubset->name);
258#endif
259 return(NULL);
260 }
261
262 /*
263 * Allocate a new DTD and fill the fields.
264 */
265 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
266 if (cur == NULL) {
267 xmlGenericError(xmlGenericErrorContext,
268 "xmlNewDtd : malloc failed\n");
269 return(NULL);
270 }
271 memset(cur, 0 , sizeof(xmlDtd));
272 cur->type = XML_DTD_NODE;
273
274 if (name != NULL)
275 cur->name = xmlStrdup(name);
276 if (ExternalID != NULL)
277 cur->ExternalID = xmlStrdup(ExternalID);
278 if (SystemID != NULL)
279 cur->SystemID = xmlStrdup(SystemID);
280 if (doc != NULL)
281 doc->extSubset = cur;
282 cur->doc = doc;
283
284 return(cur);
285}
286
287/**
288 * xmlGetIntSubset:
289 * @doc: the document pointer
290 *
291 * Get the internal subset of a document
292 * Returns a pointer to the DTD structure or NULL if not found
293 */
294
295xmlDtdPtr
296xmlGetIntSubset(xmlDocPtr doc) {
297 xmlNodePtr cur;
298
299 if (doc == NULL)
300 return(NULL);
301 cur = doc->children;
302 while (cur != NULL) {
303 if (cur->type == XML_DTD_NODE)
304 return((xmlDtdPtr) cur);
305 cur = cur->next;
306 }
307 return((xmlDtdPtr) doc->intSubset);
308}
309
310/**
311 * xmlCreateIntSubset:
312 * @doc: the document pointer
313 * @name: the DTD name
Daniel Veillarde356c282001-03-10 12:32:04 +0000314 * @ExternalID: the external (PUBLIC) ID
Owen Taylor3473f882001-02-23 17:55:21 +0000315 * @SystemID: the system ID
316 *
317 * Create the internal subset of a document
318 * Returns a pointer to the new DTD structure
319 */
320xmlDtdPtr
321xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name,
322 const xmlChar *ExternalID, const xmlChar *SystemID) {
323 xmlDtdPtr cur;
324
325 if ((doc != NULL) && (xmlGetIntSubset(doc) != NULL)) {
326#ifdef DEBUG_TREE
327 xmlGenericError(xmlGenericErrorContext,
328
329 "xmlCreateIntSubset(): document %s already have an internal subset\n",
330 doc->name);
331#endif
332 return(NULL);
333 }
334
335 /*
336 * Allocate a new DTD and fill the fields.
337 */
338 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
339 if (cur == NULL) {
340 xmlGenericError(xmlGenericErrorContext,
341 "xmlNewDtd : malloc failed\n");
342 return(NULL);
343 }
344 memset(cur, 0, sizeof(xmlDtd));
345 cur->type = XML_DTD_NODE;
346
347 if (name != NULL)
348 cur->name = xmlStrdup(name);
349 if (ExternalID != NULL)
350 cur->ExternalID = xmlStrdup(ExternalID);
351 if (SystemID != NULL)
352 cur->SystemID = xmlStrdup(SystemID);
353 if (doc != NULL) {
354 doc->intSubset = cur;
355 cur->parent = doc;
356 cur->doc = doc;
357 if (doc->children == NULL) {
358 doc->children = (xmlNodePtr) cur;
359 doc->last = (xmlNodePtr) cur;
360 } else {
Owen Taylor3473f882001-02-23 17:55:21 +0000361 if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillarde356c282001-03-10 12:32:04 +0000362 xmlNodePtr prev;
363
Owen Taylor3473f882001-02-23 17:55:21 +0000364 prev = doc->children;
365 prev->prev = (xmlNodePtr) cur;
366 cur->next = prev;
367 doc->children = (xmlNodePtr) cur;
368 } else {
Daniel Veillarde356c282001-03-10 12:32:04 +0000369 xmlNodePtr next;
370
371 next = doc->children;
372 while ((next != NULL) && (next->type != XML_ELEMENT_NODE))
373 next = next->next;
374 if (next == NULL) {
375 cur->prev = doc->last;
376 cur->prev->next = (xmlNodePtr) cur;
377 cur->next = NULL;
378 doc->last = (xmlNodePtr) cur;
379 } else {
380 cur->next = next;
381 cur->prev = next->prev;
382 if (cur->prev == NULL)
383 doc->children = (xmlNodePtr) cur;
384 else
385 cur->prev->next = (xmlNodePtr) cur;
386 next->prev = (xmlNodePtr) cur;
387 }
Owen Taylor3473f882001-02-23 17:55:21 +0000388 }
389 }
390 }
391 return(cur);
392}
393
394/**
395 * xmlFreeDtd:
396 * @cur: the DTD structure to free up
397 *
398 * Free a DTD structure.
399 */
400void
401xmlFreeDtd(xmlDtdPtr cur) {
402 if (cur == NULL) {
403#ifdef DEBUG_TREE
404 xmlGenericError(xmlGenericErrorContext,
405 "xmlFreeDtd : DTD == NULL\n");
406#endif
407 return;
408 }
409 if (cur->children != NULL) {
410 xmlNodePtr next, c = cur->children;
411
412 /*
413 * Cleanup all the DTD comments they are not in the Dtd
414 * indexes.
415 */
416 while (c != NULL) {
417 next = c->next;
418 if (c->type == XML_COMMENT_NODE) {
419 xmlUnlinkNode(c);
420 xmlFreeNode(c);
421 }
422 c = next;
423 }
424 }
425 if (cur->name != NULL) xmlFree((char *) cur->name);
426 if (cur->SystemID != NULL) xmlFree((char *) cur->SystemID);
427 if (cur->ExternalID != NULL) xmlFree((char *) cur->ExternalID);
428 /* TODO !!! */
429 if (cur->notations != NULL)
430 xmlFreeNotationTable((xmlNotationTablePtr) cur->notations);
431
432 if (cur->elements != NULL)
433 xmlFreeElementTable((xmlElementTablePtr) cur->elements);
434 if (cur->attributes != NULL)
435 xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes);
436 if (cur->entities != NULL)
437 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities);
438 if (cur->pentities != NULL)
439 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->pentities);
440
Owen Taylor3473f882001-02-23 17:55:21 +0000441 xmlFree(cur);
442}
443
444/**
445 * xmlNewDoc:
446 * @version: xmlChar string giving the version of XML "1.0"
447 *
448 * Returns a new document
449 */
450xmlDocPtr
451xmlNewDoc(const xmlChar *version) {
452 xmlDocPtr cur;
453
454 if (version == NULL)
455 version = (const xmlChar *) "1.0";
456
457 /*
458 * Allocate a new document and fill the fields.
459 */
460 cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
461 if (cur == NULL) {
462 xmlGenericError(xmlGenericErrorContext,
463 "xmlNewDoc : malloc failed\n");
464 return(NULL);
465 }
466 memset(cur, 0, sizeof(xmlDoc));
467 cur->type = XML_DOCUMENT_NODE;
468
469 cur->version = xmlStrdup(version);
470 cur->standalone = -1;
471 cur->compression = -1; /* not initialized */
472 cur->doc = cur;
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000473 cur->charset = XML_CHAR_ENCODING_UTF8;
Owen Taylor3473f882001-02-23 17:55:21 +0000474 return(cur);
475}
476
477/**
478 * xmlFreeDoc:
479 * @cur: pointer to the document
480 * @:
481 *
482 * Free up all the structures used by a document, tree included.
483 */
484void
485xmlFreeDoc(xmlDocPtr cur) {
486 if (cur == NULL) {
487#ifdef DEBUG_TREE
488 xmlGenericError(xmlGenericErrorContext,
489 "xmlFreeDoc : document == NULL\n");
490#endif
491 return;
492 }
Daniel Veillard76d66f42001-05-16 21:05:17 +0000493 /*
494 * Do this before freeing the children list to avoid ID lookups
495 */
496 if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids);
497 cur->ids = NULL;
498 if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
499 cur->refs = NULL;
500 if (cur->extSubset != NULL) {
501 xmlUnlinkNode((xmlNodePtr) cur->extSubset);
502 xmlFreeDtd(cur->extSubset);
503 cur->extSubset = NULL;
504 }
505 if (cur->intSubset != NULL) {
506 xmlUnlinkNode((xmlNodePtr) cur->intSubset);
507 xmlFreeDtd(cur->intSubset);
508 cur->intSubset = NULL;
509 }
510
511 if (cur->children != NULL) xmlFreeNodeList(cur->children);
512
Owen Taylor3473f882001-02-23 17:55:21 +0000513 if (cur->version != NULL) xmlFree((char *) cur->version);
514 if (cur->name != NULL) xmlFree((char *) cur->name);
515 if (cur->encoding != NULL) xmlFree((char *) cur->encoding);
Owen Taylor3473f882001-02-23 17:55:21 +0000516 if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
Owen Taylor3473f882001-02-23 17:55:21 +0000517 if (cur->URL != NULL) xmlFree((char *) cur->URL);
Owen Taylor3473f882001-02-23 17:55:21 +0000518 xmlFree(cur);
519}
520
521/**
522 * xmlStringLenGetNodeList:
523 * @doc: the document
524 * @value: the value of the text
525 * @len: the length of the string value
526 *
527 * Parse the value string and build the node list associated. Should
528 * produce a flat tree with only TEXTs and ENTITY_REFs.
529 * Returns a pointer to the first child
530 */
531xmlNodePtr
532xmlStringLenGetNodeList(xmlDocPtr doc, const xmlChar *value, int len) {
533 xmlNodePtr ret = NULL, last = NULL;
534 xmlNodePtr node;
535 xmlChar *val;
536 const xmlChar *cur = value;
537 const xmlChar *q;
538 xmlEntityPtr ent;
539
540 if (value == NULL) return(NULL);
541
542 q = cur;
543 while ((*cur != 0) && (cur - value < len)) {
544 if (*cur == '&') {
545 /*
546 * Save the current text.
547 */
548 if (cur != q) {
549 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
550 xmlNodeAddContentLen(last, q, cur - q);
551 } else {
552 node = xmlNewDocTextLen(doc, q, cur - q);
553 if (node == NULL) return(ret);
554 if (last == NULL)
555 last = ret = node;
556 else {
557 last->next = node;
558 node->prev = last;
559 last = node;
560 }
561 }
562 }
563 /*
564 * Read the entity string
565 */
566 cur++;
567 q = cur;
568 while ((*cur != 0) && (cur - value < len) && (*cur != ';')) cur++;
569 if ((*cur == 0) || (cur - value >= len)) {
570#ifdef DEBUG_TREE
571 xmlGenericError(xmlGenericErrorContext,
572 "xmlStringLenGetNodeList: unterminated entity %30s\n", q);
573#endif
574 return(ret);
575 }
576 if (cur != q) {
577 /*
578 * Predefined entities don't generate nodes
579 */
580 val = xmlStrndup(q, cur - q);
581 ent = xmlGetDocEntity(doc, val);
582 if ((ent != NULL) &&
583 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
584 if (last == NULL) {
585 node = xmlNewDocText(doc, ent->content);
586 last = ret = node;
587 } else
588 xmlNodeAddContent(last, ent->content);
589
590 } else {
591 /*
592 * Create a new REFERENCE_REF node
593 */
594 node = xmlNewReference(doc, val);
595 if (node == NULL) {
596 if (val != NULL) xmlFree(val);
597 return(ret);
598 }
599 if (last == NULL)
600 last = ret = node;
601 else {
602 last->next = node;
603 node->prev = last;
604 last = node;
605 }
606 }
607 xmlFree(val);
608 }
609 cur++;
610 q = cur;
611 } else
612 cur++;
613 }
614 if (cur != q) {
615 /*
616 * Handle the last piece of text.
617 */
618 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
619 xmlNodeAddContentLen(last, q, cur - q);
620 } else {
621 node = xmlNewDocTextLen(doc, q, cur - q);
622 if (node == NULL) return(ret);
623 if (last == NULL)
624 last = ret = node;
625 else {
626 last->next = node;
627 node->prev = last;
628 last = node;
629 }
630 }
631 }
632 return(ret);
633}
634
635/**
636 * xmlStringGetNodeList:
637 * @doc: the document
638 * @value: the value of the attribute
639 *
640 * Parse the value string and build the node list associated. Should
641 * produce a flat tree with only TEXTs and ENTITY_REFs.
642 * Returns a pointer to the first child
643 */
644xmlNodePtr
645xmlStringGetNodeList(xmlDocPtr doc, const xmlChar *value) {
646 xmlNodePtr ret = NULL, last = NULL;
647 xmlNodePtr node;
648 xmlChar *val;
649 const xmlChar *cur = value;
650 const xmlChar *q;
651 xmlEntityPtr ent;
652
653 if (value == NULL) return(NULL);
654
655 q = cur;
656 while (*cur != 0) {
Daniel Veillardbdb9ba72001-04-11 11:28:06 +0000657 if (cur[0] == '&') {
658 int charval = 0;
659 xmlChar tmp;
660
Owen Taylor3473f882001-02-23 17:55:21 +0000661 /*
662 * Save the current text.
663 */
664 if (cur != q) {
665 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
666 xmlNodeAddContentLen(last, q, cur - q);
667 } else {
668 node = xmlNewDocTextLen(doc, q, cur - q);
669 if (node == NULL) return(ret);
670 if (last == NULL)
671 last = ret = node;
672 else {
673 last->next = node;
674 node->prev = last;
675 last = node;
676 }
677 }
678 }
Owen Taylor3473f882001-02-23 17:55:21 +0000679 q = cur;
Daniel Veillardbdb9ba72001-04-11 11:28:06 +0000680 if ((cur[1] == '#') && (cur[2] == 'x')) {
681 cur += 3;
682 tmp = *cur;
683 while (tmp != ';') { /* Non input consuming loop */
684 if ((tmp >= '0') && (tmp <= '9'))
685 charval = charval * 16 + (tmp - '0');
686 else if ((tmp >= 'a') && (tmp <= 'f'))
687 charval = charval * 16 + (tmp - 'a') + 10;
688 else if ((tmp >= 'A') && (tmp <= 'F'))
689 charval = charval * 16 + (tmp - 'A') + 10;
Owen Taylor3473f882001-02-23 17:55:21 +0000690 else {
Daniel Veillardbdb9ba72001-04-11 11:28:06 +0000691 xmlGenericError(xmlGenericErrorContext,
692 "xmlStringGetNodeList: incharvalid hexadecimal charvalue\n");
693 charval = 0;
694 break;
695 }
696 cur++;
697 tmp = *cur;
698 }
699 if (tmp == ';')
700 cur++;
701 q = cur;
702 } else if (cur[1] == '#') {
703 cur += 2;
704 tmp = *cur;
705 while (tmp != ';') { /* Non input consuming loops */
706 if ((tmp >= '0') && (tmp <= '9'))
707 charval = charval * 10 + (tmp - '0');
708 else {
709 xmlGenericError(xmlGenericErrorContext,
710 "xmlStringGetNodeList: incharvalid decimal charvalue\n");
711 charval = 0;
712 break;
713 }
714 cur++;
715 tmp = *cur;
716 }
717 if (tmp == ';')
718 cur++;
719 q = cur;
720 } else {
721 /*
722 * Read the entity string
723 */
724 cur++;
725 q = cur;
726 while ((*cur != 0) && (*cur != ';')) cur++;
727 if (*cur == 0) {
728#ifdef DEBUG_TREE
729 xmlGenericError(xmlGenericErrorContext,
730 "xmlStringGetNodeList: unterminated entity %30s\n", q);
731#endif
732 return(ret);
733 }
734 if (cur != q) {
735 /*
736 * Predefined entities don't generate nodes
737 */
738 val = xmlStrndup(q, cur - q);
739 ent = xmlGetDocEntity(doc, val);
740 if ((ent != NULL) &&
741 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
742 if (last == NULL) {
743 node = xmlNewDocText(doc, ent->content);
744 last = ret = node;
745 } else
746 xmlNodeAddContent(last, ent->content);
747
748 } else {
749 /*
750 * Create a new REFERENCE_REF node
751 */
752 node = xmlNewReference(doc, val);
753 if (node == NULL) {
754 if (val != NULL) xmlFree(val);
755 return(ret);
756 }
757 if (last == NULL) {
758 last = ret = node;
759 } else {
760 last = xmlAddNextSibling(last, node);
761 }
762 }
763 xmlFree(val);
764 }
765 cur++;
766 q = cur;
767 }
768 if (charval != 0) {
769 xmlChar buf[10];
770 int len;
771
772 len = xmlCopyCharMultiByte(buf, charval);
773 buf[len] = 0;
774 node = xmlNewDocText(doc, buf);
775 if (node != NULL) {
776 if (last == NULL) {
777 last = ret = node;
778 } else {
779 last = xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +0000780 }
781 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +0000782
783 charval = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000784 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +0000785 } else
Owen Taylor3473f882001-02-23 17:55:21 +0000786 cur++;
787 }
Daniel Veillard75bea542001-05-11 17:41:21 +0000788 if ((cur != q) || (ret == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000789 /*
790 * Handle the last piece of text.
791 */
792 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
793 xmlNodeAddContentLen(last, q, cur - q);
794 } else {
795 node = xmlNewDocTextLen(doc, q, cur - q);
796 if (node == NULL) return(ret);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +0000797 if (last == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +0000798 last = ret = node;
Daniel Veillardbdb9ba72001-04-11 11:28:06 +0000799 } else {
800 last = xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +0000801 }
802 }
803 }
804 return(ret);
805}
806
807/**
808 * xmlNodeListGetString:
809 * @doc: the document
810 * @list: a Node list
811 * @inLine: should we replace entity contents or show their external form
812 *
813 * Returns the string equivalent to the text contained in the Node list
814 * made of TEXTs and ENTITY_REFs
815 * Returns a pointer to the string copy, the calller must free it.
816 */
817xmlChar *
818xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine) {
819 xmlNodePtr node = list;
820 xmlChar *ret = NULL;
821 xmlEntityPtr ent;
822
823 if (list == NULL) return(NULL);
824
825 while (node != NULL) {
826 if ((node->type == XML_TEXT_NODE) ||
827 (node->type == XML_CDATA_SECTION_NODE)) {
828 if (inLine) {
829#ifndef XML_USE_BUFFER_CONTENT
830 ret = xmlStrcat(ret, node->content);
831#else
832 ret = xmlStrcat(ret, xmlBufferContent(node->content));
833#endif
834 } else {
835 xmlChar *buffer;
836
837#ifndef XML_USE_BUFFER_CONTENT
838 buffer = xmlEncodeEntitiesReentrant(doc, node->content);
839#else
840 buffer = xmlEncodeEntitiesReentrant(doc,
841 xmlBufferContent(node->content));
842#endif
843 if (buffer != NULL) {
844 ret = xmlStrcat(ret, buffer);
845 xmlFree(buffer);
846 }
847 }
848 } else if (node->type == XML_ENTITY_REF_NODE) {
849 if (inLine) {
850 ent = xmlGetDocEntity(doc, node->name);
851 if (ent != NULL)
852 ret = xmlStrcat(ret, ent->content);
853 else {
854#ifndef XML_USE_BUFFER_CONTENT
855 ret = xmlStrcat(ret, node->content);
856#else
857 ret = xmlStrcat(ret, xmlBufferContent(node->content));
858#endif
859 }
860 } else {
861 xmlChar buf[2];
862 buf[0] = '&'; buf[1] = 0;
863 ret = xmlStrncat(ret, buf, 1);
864 ret = xmlStrcat(ret, node->name);
865 buf[0] = ';'; buf[1] = 0;
866 ret = xmlStrncat(ret, buf, 1);
867 }
868 }
869#if 0
870 else {
871 xmlGenericError(xmlGenericErrorContext,
872 "xmlGetNodeListString : invalide node type %d\n",
873 node->type);
874 }
875#endif
876 node = node->next;
877 }
878 return(ret);
879}
880
881/**
882 * xmlNodeListGetRawString:
883 * @doc: the document
884 * @list: a Node list
885 * @inLine: should we replace entity contents or show their external form
886 *
887 * Returns the string equivalent to the text contained in the Node list
888 * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString()
889 * this function doesn't do any character encoding handling.
890 *
891 * Returns a pointer to the string copy, the calller must free it.
892 */
893xmlChar *
894xmlNodeListGetRawString(xmlDocPtr doc, xmlNodePtr list, int inLine) {
895 xmlNodePtr node = list;
896 xmlChar *ret = NULL;
897 xmlEntityPtr ent;
898
899 if (list == NULL) return(NULL);
900
901 while (node != NULL) {
902 if (node->type == XML_TEXT_NODE) {
903 if (inLine) {
904#ifndef XML_USE_BUFFER_CONTENT
905 ret = xmlStrcat(ret, node->content);
906#else
907 ret = xmlStrcat(ret, xmlBufferContent(node->content));
908#endif
909 } else {
910 xmlChar *buffer;
911
912#ifndef XML_USE_BUFFER_CONTENT
913 buffer = xmlEncodeSpecialChars(doc, node->content);
914#else
915 buffer = xmlEncodeSpecialChars(doc,
916 xmlBufferContent(node->content));
917#endif
918 if (buffer != NULL) {
919 ret = xmlStrcat(ret, buffer);
920 xmlFree(buffer);
921 }
922 }
923 } else if (node->type == XML_ENTITY_REF_NODE) {
924 if (inLine) {
925 ent = xmlGetDocEntity(doc, node->name);
926 if (ent != NULL)
927 ret = xmlStrcat(ret, ent->content);
928 else {
929#ifndef XML_USE_BUFFER_CONTENT
930 ret = xmlStrcat(ret, node->content);
931#else
932 ret = xmlStrcat(ret, xmlBufferContent(node->content));
933#endif
934 }
935 } else {
936 xmlChar buf[2];
937 buf[0] = '&'; buf[1] = 0;
938 ret = xmlStrncat(ret, buf, 1);
939 ret = xmlStrcat(ret, node->name);
940 buf[0] = ';'; buf[1] = 0;
941 ret = xmlStrncat(ret, buf, 1);
942 }
943 }
944#if 0
945 else {
946 xmlGenericError(xmlGenericErrorContext,
947 "xmlGetNodeListString : invalide node type %d\n",
948 node->type);
949 }
950#endif
951 node = node->next;
952 }
953 return(ret);
954}
955
956/**
957 * xmlNewProp:
958 * @node: the holding node
959 * @name: the name of the attribute
960 * @value: the value of the attribute
961 *
962 * Create a new property carried by a node.
963 * Returns a pointer to the attribute
964 */
965xmlAttrPtr
966xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
967 xmlAttrPtr cur;
968 xmlDocPtr doc = NULL;
969
970 if (name == NULL) {
971#ifdef DEBUG_TREE
972 xmlGenericError(xmlGenericErrorContext,
973 "xmlNewProp : name == NULL\n");
974#endif
975 return(NULL);
976 }
977
978 /*
979 * Allocate a new property and fill the fields.
980 */
981 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
982 if (cur == NULL) {
983 xmlGenericError(xmlGenericErrorContext,
984 "xmlNewProp : malloc failed\n");
985 return(NULL);
986 }
987 memset(cur, 0, sizeof(xmlAttr));
988 cur->type = XML_ATTRIBUTE_NODE;
989
990 cur->parent = node;
991 if (node != NULL) {
992 doc = node->doc;
993 cur->doc = doc;
994 }
995 cur->name = xmlStrdup(name);
996 if (value != NULL) {
997 xmlChar *buffer;
998 xmlNodePtr tmp;
999
1000 buffer = xmlEncodeEntitiesReentrant(doc, value);
1001 cur->children = xmlStringGetNodeList(doc, buffer);
1002 cur->last = NULL;
1003 tmp = cur->children;
1004 while (tmp != NULL) {
1005 tmp->parent = (xmlNodePtr) cur;
1006 tmp->doc = doc;
1007 if (tmp->next == NULL)
1008 cur->last = tmp;
1009 tmp = tmp->next;
1010 }
1011 xmlFree(buffer);
1012 }
1013
1014 /*
1015 * Add it at the end to preserve parsing order ...
1016 */
1017 if (node != NULL) {
1018 if (node->properties == NULL) {
1019 node->properties = cur;
1020 } else {
1021 xmlAttrPtr prev = node->properties;
1022
1023 while (prev->next != NULL) prev = prev->next;
1024 prev->next = cur;
1025 cur->prev = prev;
1026 }
1027 }
1028 return(cur);
1029}
1030
1031/**
1032 * xmlNewNsProp:
1033 * @node: the holding node
1034 * @ns: the namespace
1035 * @name: the name of the attribute
1036 * @value: the value of the attribute
1037 *
1038 * Create a new property tagged with a namespace and carried by a node.
1039 * Returns a pointer to the attribute
1040 */
1041xmlAttrPtr
1042xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
1043 const xmlChar *value) {
1044 xmlAttrPtr cur;
1045
1046 if (name == NULL) {
1047#ifdef DEBUG_TREE
1048 xmlGenericError(xmlGenericErrorContext,
1049 "xmlNewProp : name == NULL\n");
1050#endif
1051 return(NULL);
1052 }
1053
1054 /*
1055 * Allocate a new property and fill the fields.
1056 */
1057 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1058 if (cur == NULL) {
1059 xmlGenericError(xmlGenericErrorContext,
1060 "xmlNewProp : malloc failed\n");
1061 return(NULL);
1062 }
1063 memset(cur, 0, sizeof(xmlAttr));
1064 cur->type = XML_ATTRIBUTE_NODE;
1065
1066 cur->parent = node;
1067 if (node != NULL)
1068 cur->doc = node->doc;
1069 cur->ns = ns;
1070 cur->name = xmlStrdup(name);
1071 if (value != NULL) {
1072 xmlChar *buffer;
1073 xmlNodePtr tmp;
1074
1075 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
1076 cur->children = xmlStringGetNodeList(node->doc, buffer);
1077 cur->last = NULL;
1078 tmp = cur->children;
1079 while (tmp != NULL) {
1080 tmp->parent = (xmlNodePtr) cur;
1081 if (tmp->next == NULL)
1082 cur->last = tmp;
1083 tmp = tmp->next;
1084 }
1085 xmlFree(buffer);
1086 }
1087
1088 /*
1089 * Add it at the end to preserve parsing order ...
1090 */
1091 if (node != NULL) {
1092 if (node->properties == NULL) {
1093 node->properties = cur;
1094 } else {
1095 xmlAttrPtr prev = node->properties;
1096
1097 while (prev->next != NULL) prev = prev->next;
1098 prev->next = cur;
1099 cur->prev = prev;
1100 }
1101 }
1102 return(cur);
1103}
1104
1105/**
1106 * xmlNewDocProp:
1107 * @doc: the document
1108 * @name: the name of the attribute
1109 * @value: the value of the attribute
1110 *
1111 * Create a new property carried by a document.
1112 * Returns a pointer to the attribute
1113 */
1114xmlAttrPtr
1115xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) {
1116 xmlAttrPtr cur;
1117
1118 if (name == NULL) {
1119#ifdef DEBUG_TREE
1120 xmlGenericError(xmlGenericErrorContext,
1121 "xmlNewProp : name == NULL\n");
1122#endif
1123 return(NULL);
1124 }
1125
1126 /*
1127 * Allocate a new property and fill the fields.
1128 */
1129 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1130 if (cur == NULL) {
1131 xmlGenericError(xmlGenericErrorContext,
1132 "xmlNewProp : malloc failed\n");
1133 return(NULL);
1134 }
1135 memset(cur, 0, sizeof(xmlAttr));
1136 cur->type = XML_ATTRIBUTE_NODE;
1137
1138 cur->name = xmlStrdup(name);
1139 cur->doc = doc;
1140 if (value != NULL) {
1141 xmlNodePtr tmp;
1142
1143 cur->children = xmlStringGetNodeList(doc, value);
1144 cur->last = NULL;
1145
1146 tmp = cur->children;
1147 while (tmp != NULL) {
1148 tmp->parent = (xmlNodePtr) cur;
1149 if (tmp->next == NULL)
1150 cur->last = tmp;
1151 tmp = tmp->next;
1152 }
1153 }
1154 return(cur);
1155}
1156
1157/**
1158 * xmlFreePropList:
1159 * @cur: the first property in the list
1160 *
1161 * Free a property and all its siblings, all the children are freed too.
1162 */
1163void
1164xmlFreePropList(xmlAttrPtr cur) {
1165 xmlAttrPtr next;
1166 if (cur == NULL) {
1167#ifdef DEBUG_TREE
1168 xmlGenericError(xmlGenericErrorContext,
1169 "xmlFreePropList : property == NULL\n");
1170#endif
1171 return;
1172 }
1173 while (cur != NULL) {
1174 next = cur->next;
1175 xmlFreeProp(cur);
1176 cur = next;
1177 }
1178}
1179
1180/**
1181 * xmlFreeProp:
1182 * @cur: an attribute
1183 *
1184 * Free one attribute, all the content is freed too
1185 */
1186void
1187xmlFreeProp(xmlAttrPtr cur) {
1188 if (cur == NULL) {
1189#ifdef DEBUG_TREE
1190 xmlGenericError(xmlGenericErrorContext,
1191 "xmlFreeProp : property == NULL\n");
1192#endif
1193 return;
1194 }
1195 /* Check for ID removal -> leading to invalid references ! */
Daniel Veillard76d66f42001-05-16 21:05:17 +00001196 if ((cur->parent != NULL) && (cur->parent->doc != NULL) &&
1197 ((cur->parent->doc->intSubset != NULL) ||
1198 (cur->parent->doc->extSubset != NULL))) {
1199 if (xmlIsID(cur->parent->doc, cur->parent, cur))
1200 xmlRemoveID(cur->parent->doc, cur);
1201 }
Owen Taylor3473f882001-02-23 17:55:21 +00001202 if (cur->name != NULL) xmlFree((char *) cur->name);
1203 if (cur->children != NULL) xmlFreeNodeList(cur->children);
Owen Taylor3473f882001-02-23 17:55:21 +00001204 xmlFree(cur);
1205}
1206
1207/**
1208 * xmlRemoveProp:
1209 * @cur: an attribute
1210 *
1211 * Unlink and free one attribute, all the content is freed too
1212 * Note this doesn't work for namespace definition attributes
1213 *
1214 * Returns 0 if success and -1 in case of error.
1215 */
1216int
1217xmlRemoveProp(xmlAttrPtr cur) {
1218 xmlAttrPtr tmp;
1219 if (cur == NULL) {
1220#ifdef DEBUG_TREE
1221 xmlGenericError(xmlGenericErrorContext,
1222 "xmlRemoveProp : cur == NULL\n");
1223#endif
1224 return(-1);
1225 }
1226 if (cur->parent == NULL) {
1227#ifdef DEBUG_TREE
1228 xmlGenericError(xmlGenericErrorContext,
1229 "xmlRemoveProp : cur->parent == NULL\n");
1230#endif
1231 return(-1);
1232 }
1233 tmp = cur->parent->properties;
1234 if (tmp == cur) {
1235 cur->parent->properties = cur->next;
1236 xmlFreeProp(cur);
1237 return(0);
1238 }
1239 while (tmp != NULL) {
1240 if (tmp->next == cur) {
1241 tmp->next = cur->next;
1242 if (tmp->next != NULL)
1243 tmp->next->prev = tmp;
1244 xmlFreeProp(cur);
1245 return(0);
1246 }
1247 tmp = tmp->next;
1248 }
1249#ifdef DEBUG_TREE
1250 xmlGenericError(xmlGenericErrorContext,
1251 "xmlRemoveProp : attribute not owned by its node\n");
1252#endif
1253 return(-1);
1254}
1255
1256/**
1257 * xmlNewPI:
1258 * @name: the processing instruction name
1259 * @content: the PI content
1260 *
1261 * Creation of a processing instruction element.
1262 * Returns a pointer to the new node object.
1263 */
1264xmlNodePtr
1265xmlNewPI(const xmlChar *name, const xmlChar *content) {
1266 xmlNodePtr cur;
1267
1268 if (name == NULL) {
1269#ifdef DEBUG_TREE
1270 xmlGenericError(xmlGenericErrorContext,
1271 "xmlNewPI : name == NULL\n");
1272#endif
1273 return(NULL);
1274 }
1275
1276 /*
1277 * Allocate a new node and fill the fields.
1278 */
1279 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1280 if (cur == NULL) {
1281 xmlGenericError(xmlGenericErrorContext,
1282 "xmlNewPI : malloc failed\n");
1283 return(NULL);
1284 }
1285 memset(cur, 0, sizeof(xmlNode));
1286 cur->type = XML_PI_NODE;
1287
1288 cur->name = xmlStrdup(name);
1289 if (content != NULL) {
1290#ifndef XML_USE_BUFFER_CONTENT
1291 cur->content = xmlStrdup(content);
1292#else
1293 cur->content = xmlBufferCreateSize(0);
1294 xmlBufferSetAllocationScheme(cur->content,
1295 xmlGetBufferAllocationScheme());
1296 xmlBufferAdd(cur->content, content, -1);
1297#endif
1298 }
1299 return(cur);
1300}
1301
1302/**
1303 * xmlNewNode:
1304 * @ns: namespace if any
1305 * @name: the node name
1306 *
1307 * Creation of a new node element. @ns is optionnal (NULL).
1308 *
1309 * Returns a pointer to the new node object.
1310 */
1311xmlNodePtr
1312xmlNewNode(xmlNsPtr ns, const xmlChar *name) {
1313 xmlNodePtr cur;
1314
1315 if (name == NULL) {
1316#ifdef DEBUG_TREE
1317 xmlGenericError(xmlGenericErrorContext,
1318 "xmlNewNode : name == NULL\n");
1319#endif
1320 return(NULL);
1321 }
1322
1323 /*
1324 * Allocate a new node and fill the fields.
1325 */
1326 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1327 if (cur == NULL) {
1328 xmlGenericError(xmlGenericErrorContext,
1329 "xmlNewNode : malloc failed\n");
1330 return(NULL);
1331 }
1332 memset(cur, 0, sizeof(xmlNode));
1333 cur->type = XML_ELEMENT_NODE;
1334
1335 cur->name = xmlStrdup(name);
1336 cur->ns = ns;
1337 return(cur);
1338}
1339
1340/**
1341 * xmlNewDocNode:
1342 * @doc: the document
1343 * @ns: namespace if any
1344 * @name: the node name
1345 * @content: the XML text content if any
1346 *
1347 * Creation of a new node element within a document. @ns and @content
1348 * are optionnal (NULL).
1349 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
1350 * references, but XML special chars need to be escaped first by using
1351 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
1352 * need entities support.
1353 *
1354 * Returns a pointer to the new node object.
1355 */
1356xmlNodePtr
1357xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns,
1358 const xmlChar *name, const xmlChar *content) {
1359 xmlNodePtr cur;
1360
1361 cur = xmlNewNode(ns, name);
1362 if (cur != NULL) {
1363 cur->doc = doc;
1364 if (content != NULL) {
1365 cur->children = xmlStringGetNodeList(doc, content);
1366 UPDATE_LAST_CHILD_AND_PARENT(cur)
1367 }
1368 }
1369 return(cur);
1370}
1371
1372
1373/**
1374 * xmlNewDocRawNode:
1375 * @doc: the document
1376 * @ns: namespace if any
1377 * @name: the node name
1378 * @content: the text content if any
1379 *
1380 * Creation of a new node element within a document. @ns and @content
1381 * are optionnal (NULL).
1382 *
1383 * Returns a pointer to the new node object.
1384 */
1385xmlNodePtr
1386xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns,
1387 const xmlChar *name, const xmlChar *content) {
1388 xmlNodePtr cur;
1389
1390 cur = xmlNewNode(ns, name);
1391 if (cur != NULL) {
1392 cur->doc = doc;
1393 if (content != NULL) {
1394 cur->children = xmlNewDocText(doc, content);
1395 UPDATE_LAST_CHILD_AND_PARENT(cur)
1396 }
1397 }
1398 return(cur);
1399}
1400
1401/**
1402 * xmlNewDocFragment:
1403 * @doc: the document owning the fragment
1404 *
1405 * Creation of a new Fragment node.
1406 * Returns a pointer to the new node object.
1407 */
1408xmlNodePtr
1409xmlNewDocFragment(xmlDocPtr doc) {
1410 xmlNodePtr cur;
1411
1412 /*
1413 * Allocate a new DocumentFragment node and fill the fields.
1414 */
1415 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1416 if (cur == NULL) {
1417 xmlGenericError(xmlGenericErrorContext,
1418 "xmlNewDocFragment : malloc failed\n");
1419 return(NULL);
1420 }
1421 memset(cur, 0, sizeof(xmlNode));
1422 cur->type = XML_DOCUMENT_FRAG_NODE;
1423
1424 cur->doc = doc;
1425 return(cur);
1426}
1427
1428/**
1429 * xmlNewText:
1430 * @content: the text content
1431 *
1432 * Creation of a new text node.
1433 * Returns a pointer to the new node object.
1434 */
1435xmlNodePtr
1436xmlNewText(const xmlChar *content) {
1437 xmlNodePtr cur;
1438
1439 /*
1440 * Allocate a new node and fill the fields.
1441 */
1442 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1443 if (cur == NULL) {
1444 xmlGenericError(xmlGenericErrorContext,
1445 "xmlNewText : malloc failed\n");
1446 return(NULL);
1447 }
1448 memset(cur, 0, sizeof(xmlNode));
1449 cur->type = XML_TEXT_NODE;
1450
1451 cur->name = xmlStringText;
1452 if (content != NULL) {
1453#ifndef XML_USE_BUFFER_CONTENT
1454 cur->content = xmlStrdup(content);
1455#else
1456 cur->content = xmlBufferCreateSize(0);
1457 xmlBufferSetAllocationScheme(cur->content,
1458 xmlGetBufferAllocationScheme());
1459 xmlBufferAdd(cur->content, content, -1);
1460#endif
1461 }
1462 return(cur);
1463}
1464
1465/**
1466 * xmlNewTextChild:
1467 * @parent: the parent node
1468 * @ns: a namespace if any
1469 * @name: the name of the child
1470 * @content: the text content of the child if any.
1471 *
1472 * Creation of a new child element, added at the end of @parent children list.
1473 * @ns and @content parameters are optionnal (NULL). If content is non NULL,
1474 * a child TEXT node will be created containing the string content.
1475 *
1476 * Returns a pointer to the new node object.
1477 */
1478xmlNodePtr
1479xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns,
1480 const xmlChar *name, const xmlChar *content) {
1481 xmlNodePtr cur, prev;
1482
1483 if (parent == NULL) {
1484#ifdef DEBUG_TREE
1485 xmlGenericError(xmlGenericErrorContext,
1486 "xmlNewTextChild : parent == NULL\n");
1487#endif
1488 return(NULL);
1489 }
1490
1491 if (name == NULL) {
1492#ifdef DEBUG_TREE
1493 xmlGenericError(xmlGenericErrorContext,
1494 "xmlNewTextChild : name == NULL\n");
1495#endif
1496 return(NULL);
1497 }
1498
1499 /*
1500 * Allocate a new node
1501 */
1502 if (ns == NULL)
1503 cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content);
1504 else
1505 cur = xmlNewDocRawNode(parent->doc, ns, name, content);
1506 if (cur == NULL) return(NULL);
1507
1508 /*
1509 * add the new element at the end of the children list.
1510 */
1511 cur->type = XML_ELEMENT_NODE;
1512 cur->parent = parent;
1513 cur->doc = parent->doc;
1514 if (parent->children == NULL) {
1515 parent->children = cur;
1516 parent->last = cur;
1517 } else {
1518 prev = parent->last;
1519 prev->next = cur;
1520 cur->prev = prev;
1521 parent->last = cur;
1522 }
1523
1524 return(cur);
1525}
1526
1527/**
1528 * xmlNewCharRef:
1529 * @doc: the document
1530 * @name: the char ref string, starting with # or "&# ... ;"
1531 *
1532 * Creation of a new character reference node.
1533 * Returns a pointer to the new node object.
1534 */
1535xmlNodePtr
1536xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) {
1537 xmlNodePtr cur;
1538
1539 /*
1540 * Allocate a new node and fill the fields.
1541 */
1542 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1543 if (cur == NULL) {
1544 xmlGenericError(xmlGenericErrorContext,
1545 "xmlNewText : malloc failed\n");
1546 return(NULL);
1547 }
1548 memset(cur, 0, sizeof(xmlNode));
1549 cur->type = XML_ENTITY_REF_NODE;
1550
1551 cur->doc = doc;
1552 if (name[0] == '&') {
1553 int len;
1554 name++;
1555 len = xmlStrlen(name);
1556 if (name[len - 1] == ';')
1557 cur->name = xmlStrndup(name, len - 1);
1558 else
1559 cur->name = xmlStrndup(name, len);
1560 } else
1561 cur->name = xmlStrdup(name);
1562 return(cur);
1563}
1564
1565/**
1566 * xmlNewReference:
1567 * @doc: the document
1568 * @name: the reference name, or the reference string with & and ;
1569 *
1570 * Creation of a new reference node.
1571 * Returns a pointer to the new node object.
1572 */
1573xmlNodePtr
1574xmlNewReference(xmlDocPtr doc, const xmlChar *name) {
1575 xmlNodePtr cur;
1576 xmlEntityPtr ent;
1577
1578 /*
1579 * Allocate a new node and fill the fields.
1580 */
1581 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1582 if (cur == NULL) {
1583 xmlGenericError(xmlGenericErrorContext,
1584 "xmlNewText : malloc failed\n");
1585 return(NULL);
1586 }
1587 memset(cur, 0, sizeof(xmlNode));
1588 cur->type = XML_ENTITY_REF_NODE;
1589
1590 cur->doc = doc;
1591 if (name[0] == '&') {
1592 int len;
1593 name++;
1594 len = xmlStrlen(name);
1595 if (name[len - 1] == ';')
1596 cur->name = xmlStrndup(name, len - 1);
1597 else
1598 cur->name = xmlStrndup(name, len);
1599 } else
1600 cur->name = xmlStrdup(name);
1601
1602 ent = xmlGetDocEntity(doc, cur->name);
1603 if (ent != NULL) {
1604#ifndef XML_USE_BUFFER_CONTENT
1605 cur->content = ent->content;
1606#else
1607 /*
1608 * CJN 11.18.99 this might be a problem, since the xmlBuffer gets
1609 * a copy of this pointer. Let's hope we don't manipulate it
1610 * later
1611 */
1612 cur->content = xmlBufferCreateSize(0);
1613 xmlBufferSetAllocationScheme(cur->content,
1614 xmlGetBufferAllocationScheme());
1615 if (ent->content != NULL)
1616 xmlBufferAdd(cur->content, ent->content, -1);
1617#endif
1618 /*
1619 * The parent pointer in entity is a Dtd pointer and thus is NOT
1620 * updated. Not sure if this is 100% correct.
1621 * -George
1622 */
1623 cur->children = (xmlNodePtr) ent;
1624 cur->last = (xmlNodePtr) ent;
1625 }
1626 return(cur);
1627}
1628
1629/**
1630 * xmlNewDocText:
1631 * @doc: the document
1632 * @content: the text content
1633 *
1634 * Creation of a new text node within a document.
1635 * Returns a pointer to the new node object.
1636 */
1637xmlNodePtr
1638xmlNewDocText(xmlDocPtr doc, const xmlChar *content) {
1639 xmlNodePtr cur;
1640
1641 cur = xmlNewText(content);
1642 if (cur != NULL) cur->doc = doc;
1643 return(cur);
1644}
1645
1646/**
1647 * xmlNewTextLen:
1648 * @content: the text content
1649 * @len: the text len.
1650 *
1651 * Creation of a new text node with an extra parameter for the content's lenght
1652 * Returns a pointer to the new node object.
1653 */
1654xmlNodePtr
1655xmlNewTextLen(const xmlChar *content, int len) {
1656 xmlNodePtr cur;
1657
1658 /*
1659 * Allocate a new node and fill the fields.
1660 */
1661 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1662 if (cur == NULL) {
1663 xmlGenericError(xmlGenericErrorContext,
1664 "xmlNewText : malloc failed\n");
1665 return(NULL);
1666 }
1667 memset(cur, 0, sizeof(xmlNode));
1668 cur->type = XML_TEXT_NODE;
1669
1670 cur->name = xmlStringText;
1671 if (content != NULL) {
1672#ifndef XML_USE_BUFFER_CONTENT
1673 cur->content = xmlStrndup(content, len);
1674#else
1675 cur->content = xmlBufferCreateSize(len);
1676 xmlBufferSetAllocationScheme(cur->content,
1677 xmlGetBufferAllocationScheme());
1678 xmlBufferAdd(cur->content, content, len);
1679#endif
1680 }
1681 return(cur);
1682}
1683
1684/**
1685 * xmlNewDocTextLen:
1686 * @doc: the document
1687 * @content: the text content
1688 * @len: the text len.
1689 *
1690 * Creation of a new text node with an extra content lenght parameter. The
1691 * text node pertain to a given document.
1692 * Returns a pointer to the new node object.
1693 */
1694xmlNodePtr
1695xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) {
1696 xmlNodePtr cur;
1697
1698 cur = xmlNewTextLen(content, len);
1699 if (cur != NULL) cur->doc = doc;
1700 return(cur);
1701}
1702
1703/**
1704 * xmlNewComment:
1705 * @content: the comment content
1706 *
1707 * Creation of a new node containing a comment.
1708 * Returns a pointer to the new node object.
1709 */
1710xmlNodePtr
1711xmlNewComment(const xmlChar *content) {
1712 xmlNodePtr cur;
1713
1714 /*
1715 * Allocate a new node and fill the fields.
1716 */
1717 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1718 if (cur == NULL) {
1719 xmlGenericError(xmlGenericErrorContext,
1720 "xmlNewComment : malloc failed\n");
1721 return(NULL);
1722 }
1723 memset(cur, 0, sizeof(xmlNode));
1724 cur->type = XML_COMMENT_NODE;
1725
1726 cur->name = xmlStringComment;
1727 if (content != NULL) {
1728#ifndef XML_USE_BUFFER_CONTENT
1729 cur->content = xmlStrdup(content);
1730#else
1731 cur->content = xmlBufferCreateSize(0);
1732 xmlBufferSetAllocationScheme(cur->content,
1733 xmlGetBufferAllocationScheme());
1734 xmlBufferAdd(cur->content, content, -1);
1735#endif
1736 }
1737 return(cur);
1738}
1739
1740/**
1741 * xmlNewCDataBlock:
1742 * @doc: the document
1743 * @content: the CData block content content
1744 * @len: the length of the block
1745 *
1746 * Creation of a new node containing a CData block.
1747 * Returns a pointer to the new node object.
1748 */
1749xmlNodePtr
1750xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) {
1751 xmlNodePtr cur;
1752
1753 /*
1754 * Allocate a new node and fill the fields.
1755 */
1756 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1757 if (cur == NULL) {
1758 xmlGenericError(xmlGenericErrorContext,
1759 "xmlNewCDataBlock : malloc failed\n");
1760 return(NULL);
1761 }
1762 memset(cur, 0, sizeof(xmlNode));
1763 cur->type = XML_CDATA_SECTION_NODE;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001764 cur->doc = doc;
Owen Taylor3473f882001-02-23 17:55:21 +00001765
1766 if (content != NULL) {
1767#ifndef XML_USE_BUFFER_CONTENT
1768 cur->content = xmlStrndup(content, len);
1769#else
1770 cur->content = xmlBufferCreateSize(len);
1771 xmlBufferSetAllocationScheme(cur->content,
1772 xmlGetBufferAllocationScheme());
1773 xmlBufferAdd(cur->content, content, len);
1774#endif
1775 }
1776 return(cur);
1777}
1778
1779/**
1780 * xmlNewDocComment:
1781 * @doc: the document
1782 * @content: the comment content
1783 *
1784 * Creation of a new node containing a commentwithin a document.
1785 * Returns a pointer to the new node object.
1786 */
1787xmlNodePtr
1788xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) {
1789 xmlNodePtr cur;
1790
1791 cur = xmlNewComment(content);
1792 if (cur != NULL) cur->doc = doc;
1793 return(cur);
1794}
1795
1796/**
1797 * xmlSetTreeDoc:
1798 * @tree: the top element
1799 * @doc: the document
1800 *
1801 * update all nodes under the tree to point to the right document
1802 */
1803void
1804xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) {
1805 if (tree == NULL)
1806 return;
1807 if (tree->type == XML_ENTITY_DECL)
1808 return;
1809 if (tree->doc != doc) {
1810 if (tree->children != NULL)
1811 xmlSetListDoc(tree->children, doc);
1812 tree->doc = doc;
1813 }
1814}
1815
1816/**
1817 * xmlSetListDoc:
1818 * @tree: the first element
1819 * @doc: the document
1820 *
1821 * update all nodes in the list to point to the right document
1822 */
1823void
1824xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) {
1825 xmlNodePtr cur;
1826
1827 if (list == NULL)
1828 return;
1829 cur = list;
1830 while (cur != NULL) {
1831 if (cur->doc != doc)
1832 xmlSetTreeDoc(cur, doc);
1833 cur = cur->next;
1834 }
1835}
1836
1837
1838/**
1839 * xmlNewChild:
1840 * @parent: the parent node
1841 * @ns: a namespace if any
1842 * @name: the name of the child
1843 * @content: the XML content of the child if any.
1844 *
1845 * Creation of a new child element, added at the end of @parent children list.
1846 * @ns and @content parameters are optionnal (NULL). If content is non NULL,
1847 * a child list containing the TEXTs and ENTITY_REFs node will be created.
1848 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
1849 * references, but XML special chars need to be escaped first by using
1850 * xmlEncodeEntitiesReentrant(). Use xmlNewTextChild() if entities
1851 * support is not needed.
1852 *
1853 * Returns a pointer to the new node object.
1854 */
1855xmlNodePtr
1856xmlNewChild(xmlNodePtr parent, xmlNsPtr ns,
1857 const xmlChar *name, const xmlChar *content) {
1858 xmlNodePtr cur, prev;
1859
1860 if (parent == NULL) {
1861#ifdef DEBUG_TREE
1862 xmlGenericError(xmlGenericErrorContext,
1863 "xmlNewChild : parent == NULL\n");
1864#endif
1865 return(NULL);
1866 }
1867
1868 if (name == NULL) {
1869#ifdef DEBUG_TREE
1870 xmlGenericError(xmlGenericErrorContext,
1871 "xmlNewChild : name == NULL\n");
1872#endif
1873 return(NULL);
1874 }
1875
1876 /*
1877 * Allocate a new node
1878 */
1879 if (ns == NULL)
1880 cur = xmlNewDocNode(parent->doc, parent->ns, name, content);
1881 else
1882 cur = xmlNewDocNode(parent->doc, ns, name, content);
1883 if (cur == NULL) return(NULL);
1884
1885 /*
1886 * add the new element at the end of the children list.
1887 */
1888 cur->type = XML_ELEMENT_NODE;
1889 cur->parent = parent;
1890 cur->doc = parent->doc;
1891 if (parent->children == NULL) {
1892 parent->children = cur;
1893 parent->last = cur;
1894 } else {
1895 prev = parent->last;
1896 prev->next = cur;
1897 cur->prev = prev;
1898 parent->last = cur;
1899 }
1900
1901 return(cur);
1902}
1903
1904/**
1905 * xmlAddNextSibling:
1906 * @cur: the child node
1907 * @elem: the new node
1908 *
1909 * Add a new element @elem as the next siblings of @cur
1910 * If the new element was already inserted in a document it is
1911 * first unlinked from its existing context.
1912 * As a result of text merging @elem may be freed.
1913 *
1914 * Returns the new element or NULL in case of error.
1915 */
1916xmlNodePtr
1917xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) {
1918 if (cur == NULL) {
1919#ifdef DEBUG_TREE
1920 xmlGenericError(xmlGenericErrorContext,
1921 "xmlAddNextSibling : cur == NULL\n");
1922#endif
1923 return(NULL);
1924 }
1925 if (elem == NULL) {
1926#ifdef DEBUG_TREE
1927 xmlGenericError(xmlGenericErrorContext,
1928 "xmlAddNextSibling : elem == NULL\n");
1929#endif
1930 return(NULL);
1931 }
1932
1933 xmlUnlinkNode(elem);
1934
1935 if (elem->type == XML_TEXT_NODE) {
1936 if (cur->type == XML_TEXT_NODE) {
1937#ifndef XML_USE_BUFFER_CONTENT
1938 xmlNodeAddContent(cur, elem->content);
1939#else
1940 xmlNodeAddContent(cur, xmlBufferContent(elem->content));
1941#endif
1942 xmlFreeNode(elem);
1943 return(cur);
1944 }
1945 if ((cur->next != NULL) && (cur->type == XML_TEXT_NODE)) {
1946#ifndef XML_USE_BUFFER_CONTENT
1947 xmlChar *tmp;
1948
1949 tmp = xmlStrdup(elem->content);
1950 tmp = xmlStrcat(tmp, cur->next->content);
1951 xmlNodeSetContent(cur->next, tmp);
1952 xmlFree(tmp);
1953#else
1954 xmlBufferAddHead(cur->next->content,
1955 xmlBufferContent(elem->content),
1956 xmlBufferLength(elem->content));
1957#endif
1958 xmlFreeNode(elem);
1959 return(cur->next);
1960 }
1961 }
1962
1963 if (elem->doc != cur->doc) {
1964 xmlSetTreeDoc(elem, cur->doc);
1965 }
1966 elem->parent = cur->parent;
1967 elem->prev = cur;
1968 elem->next = cur->next;
1969 cur->next = elem;
1970 if (elem->next != NULL)
1971 elem->next->prev = elem;
1972 if ((elem->parent != NULL) && (elem->parent->last == cur))
1973 elem->parent->last = elem;
1974 return(elem);
1975}
1976
1977/**
1978 * xmlAddPrevSibling:
1979 * @cur: the child node
1980 * @elem: the new node
1981 *
1982 * Add a new element @elem as the previous siblings of @cur
1983 * merging adjacent TEXT nodes (@elem may be freed)
1984 * If the new element was already inserted in a document it is
1985 * first unlinked from its existing context.
1986 *
1987 * Returns the new element or NULL in case of error.
1988 */
1989xmlNodePtr
1990xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) {
1991 if (cur == NULL) {
1992#ifdef DEBUG_TREE
1993 xmlGenericError(xmlGenericErrorContext,
1994 "xmlAddPrevSibling : cur == NULL\n");
1995#endif
1996 return(NULL);
1997 }
1998 if (elem == NULL) {
1999#ifdef DEBUG_TREE
2000 xmlGenericError(xmlGenericErrorContext,
2001 "xmlAddPrevSibling : elem == NULL\n");
2002#endif
2003 return(NULL);
2004 }
2005
2006 xmlUnlinkNode(elem);
2007
2008 if (elem->type == XML_TEXT_NODE) {
2009 if (cur->type == XML_TEXT_NODE) {
2010#ifndef XML_USE_BUFFER_CONTENT
2011 xmlChar *tmp;
2012
2013 tmp = xmlStrdup(elem->content);
2014 tmp = xmlStrcat(tmp, cur->content);
2015 xmlNodeSetContent(cur, tmp);
2016 xmlFree(tmp);
2017#else
2018 xmlBufferAddHead(cur->content, xmlBufferContent(elem->content),
2019 xmlBufferLength(elem->content));
2020#endif
2021 xmlFreeNode(elem);
2022 return(cur);
2023 }
2024 if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE)) {
2025#ifndef XML_USE_BUFFER_CONTENT
2026 xmlNodeAddContent(cur->prev, elem->content);
2027#else
2028 xmlNodeAddContent(cur->prev, xmlBufferContent(elem->content));
2029#endif
2030 xmlFreeNode(elem);
2031 return(cur->prev);
2032 }
2033 }
2034
2035 if (elem->doc != cur->doc) {
2036 xmlSetTreeDoc(elem, cur->doc);
2037 }
2038 elem->parent = cur->parent;
2039 elem->next = cur;
2040 elem->prev = cur->prev;
2041 cur->prev = elem;
2042 if (elem->prev != NULL)
2043 elem->prev->next = elem;
2044 if ((elem->parent != NULL) && (elem->parent->children == cur))
2045 elem->parent->children = elem;
2046 return(elem);
2047}
2048
2049/**
2050 * xmlAddSibling:
2051 * @cur: the child node
2052 * @elem: the new node
2053 *
2054 * Add a new element @elem to the list of siblings of @cur
2055 * merging adjacent TEXT nodes (@elem may be freed)
2056 * If the new element was already inserted in a document it is
2057 * first unlinked from its existing context.
2058 *
2059 * Returns the new element or NULL in case of error.
2060 */
2061xmlNodePtr
2062xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) {
2063 xmlNodePtr parent;
2064
2065 if (cur == NULL) {
2066#ifdef DEBUG_TREE
2067 xmlGenericError(xmlGenericErrorContext,
2068 "xmlAddSibling : cur == NULL\n");
2069#endif
2070 return(NULL);
2071 }
2072
2073 if (elem == NULL) {
2074#ifdef DEBUG_TREE
2075 xmlGenericError(xmlGenericErrorContext,
2076 "xmlAddSibling : elem == NULL\n");
2077#endif
2078 return(NULL);
2079 }
2080
2081 /*
2082 * Constant time is we can rely on the ->parent->last to find
2083 * the last sibling.
2084 */
2085 if ((cur->parent != NULL) &&
2086 (cur->parent->children != NULL) &&
2087 (cur->parent->last != NULL) &&
2088 (cur->parent->last->next == NULL)) {
2089 cur = cur->parent->last;
2090 } else {
2091 while (cur->next != NULL) cur = cur->next;
2092 }
2093
2094 xmlUnlinkNode(elem);
2095
2096 if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE)) {
2097#ifndef XML_USE_BUFFER_CONTENT
2098 xmlNodeAddContent(cur, elem->content);
2099#else
2100 xmlNodeAddContent(cur, xmlBufferContent(elem->content));
2101#endif
2102 xmlFreeNode(elem);
2103 return(cur);
2104 }
2105
2106 if (elem->doc != cur->doc) {
2107 xmlSetTreeDoc(elem, cur->doc);
2108 }
2109 parent = cur->parent;
2110 elem->prev = cur;
2111 elem->next = NULL;
2112 elem->parent = parent;
2113 cur->next = elem;
2114 if (parent != NULL)
2115 parent->last = elem;
2116
2117 return(elem);
2118}
2119
2120/**
2121 * xmlAddChildList:
2122 * @parent: the parent node
2123 * @cur: the first node in the list
2124 *
2125 * Add a list of node at the end of the child list of the parent
2126 * merging adjacent TEXT nodes (@cur may be freed)
2127 *
2128 * Returns the last child or NULL in case of error.
2129 */
2130xmlNodePtr
2131xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) {
2132 xmlNodePtr prev;
2133
2134 if (parent == NULL) {
2135#ifdef DEBUG_TREE
2136 xmlGenericError(xmlGenericErrorContext,
2137 "xmlAddChild : parent == NULL\n");
2138#endif
2139 return(NULL);
2140 }
2141
2142 if (cur == NULL) {
2143#ifdef DEBUG_TREE
2144 xmlGenericError(xmlGenericErrorContext,
2145 "xmlAddChild : child == NULL\n");
2146#endif
2147 return(NULL);
2148 }
2149
2150 if ((cur->doc != NULL) && (parent->doc != NULL) &&
2151 (cur->doc != parent->doc)) {
2152#ifdef DEBUG_TREE
2153 xmlGenericError(xmlGenericErrorContext,
2154 "Elements moved to a different document\n");
2155#endif
2156 }
2157
2158 /*
2159 * add the first element at the end of the children list.
2160 */
2161 if (parent->children == NULL) {
2162 parent->children = cur;
2163 } else {
2164 /*
2165 * If cur and parent->last both are TEXT nodes, then merge them.
2166 */
2167 if ((cur->type == XML_TEXT_NODE) &&
2168 (parent->last->type == XML_TEXT_NODE) &&
2169 (cur->name == parent->last->name)) {
2170#ifndef XML_USE_BUFFER_CONTENT
2171 xmlNodeAddContent(parent->last, cur->content);
2172#else
2173 xmlNodeAddContent(parent->last, xmlBufferContent(cur->content));
2174#endif
2175 /*
2176 * if it's the only child, nothing more to be done.
2177 */
2178 if (cur->next == NULL) {
2179 xmlFreeNode(cur);
2180 return(parent->last);
2181 }
2182 prev = cur;
2183 cur = cur->next;
2184 xmlFreeNode(prev);
2185 }
2186 prev = parent->last;
2187 prev->next = cur;
2188 cur->prev = prev;
2189 }
2190 while (cur->next != NULL) {
2191 cur->parent = parent;
2192 if (cur->doc != parent->doc) {
2193 xmlSetTreeDoc(cur, parent->doc);
2194 }
2195 cur = cur->next;
2196 }
2197 cur->parent = parent;
2198 cur->doc = parent->doc; /* the parent may not be linked to a doc ! */
2199 parent->last = cur;
2200
2201 return(cur);
2202}
2203
2204/**
2205 * xmlAddChild:
2206 * @parent: the parent node
2207 * @cur: the child node
2208 *
2209 * Add a new child element, to @parent, at the end of the child list
2210 * merging adjacent TEXT nodes (in which case @cur is freed)
2211 * Returns the child or NULL in case of error.
2212 */
2213xmlNodePtr
2214xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) {
2215 xmlNodePtr prev;
2216
2217 if (parent == NULL) {
2218#ifdef DEBUG_TREE
2219 xmlGenericError(xmlGenericErrorContext,
2220 "xmlAddChild : parent == NULL\n");
2221#endif
2222 return(NULL);
2223 }
2224
2225 if (cur == NULL) {
2226#ifdef DEBUG_TREE
2227 xmlGenericError(xmlGenericErrorContext,
2228 "xmlAddChild : child == NULL\n");
2229#endif
2230 return(NULL);
2231 }
2232
2233 if ((cur->doc != NULL) && (parent->doc != NULL) &&
2234 (cur->doc != parent->doc)) {
2235#ifdef DEBUG_TREE
2236 xmlGenericError(xmlGenericErrorContext,
2237 "Elements moved to a different document\n");
2238#endif
2239 }
2240
2241 /*
2242 * If cur is a TEXT node, merge its content with adjacent TEXT nodes
2243 * or with parent->content if parent->content != NULL.
2244 * cur is then freed.
2245 */
2246 if (cur->type == XML_TEXT_NODE) {
2247 if (((parent->type == XML_ELEMENT_NODE) ||
2248 (parent->type == XML_TEXT_NODE)) &&
2249 (parent->content != NULL)) {
2250#ifndef XML_USE_BUFFER_CONTENT
2251 xmlNodeAddContent(parent, cur->content);
2252#else
2253 xmlNodeAddContent(parent, xmlBufferContent(cur->content));
2254#endif
2255 xmlFreeNode(cur);
2256 return(parent);
2257 }
2258 if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) &&
2259 (parent->last->name == cur->name)) {
2260#ifndef XML_USE_BUFFER_CONTENT
2261 xmlNodeAddContent(parent->last, cur->content);
2262#else
2263 xmlNodeAddContent(parent->last, xmlBufferContent(cur->content));
2264#endif
2265 xmlFreeNode(cur);
2266 return(parent->last);
2267 }
2268 }
2269
2270 /*
2271 * add the new element at the end of the children list.
2272 */
2273 cur->parent = parent;
2274 if (cur->doc != parent->doc) {
2275 xmlSetTreeDoc(cur, parent->doc);
2276 }
2277
2278 /*
2279 * Handle the case where parent->content != NULL, in that case it will
2280 * create a intermediate TEXT node.
2281 */
2282 if (((parent->type == XML_ELEMENT_NODE) || (parent->type == XML_TEXT_NODE)) &&
2283 (parent->content != NULL)) {
2284 xmlNodePtr text;
2285
2286#ifndef XML_USE_BUFFER_CONTENT
2287 text = xmlNewDocText(parent->doc, parent->content);
2288#else
2289 text = xmlNewDocText(parent->doc, xmlBufferContent(parent->content));
2290#endif
2291 if (text != NULL) {
2292 text->next = parent->children;
2293 if (text->next != NULL)
2294 text->next->prev = text;
2295 parent->children = text;
2296 UPDATE_LAST_CHILD_AND_PARENT(parent)
2297#ifndef XML_USE_BUFFER_CONTENT
2298 xmlFree(parent->content);
2299#else
2300 xmlBufferFree(parent->content);
2301#endif
2302 parent->content = NULL;
2303 }
2304 }
2305 if (parent->children == NULL) {
2306 parent->children = cur;
2307 parent->last = cur;
2308 } else {
2309 prev = parent->last;
2310 prev->next = cur;
2311 cur->prev = prev;
2312 parent->last = cur;
2313 }
2314
2315 return(cur);
2316}
2317
2318/**
2319 * xmlGetLastChild:
2320 * @parent: the parent node
2321 *
2322 * Search the last child of a node.
2323 * Returns the last child or NULL if none.
2324 */
2325xmlNodePtr
2326xmlGetLastChild(xmlNodePtr parent) {
2327 if (parent == NULL) {
2328#ifdef DEBUG_TREE
2329 xmlGenericError(xmlGenericErrorContext,
2330 "xmlGetLastChild : parent == NULL\n");
2331#endif
2332 return(NULL);
2333 }
2334 return(parent->last);
2335}
2336
2337/**
2338 * xmlFreeNodeList:
2339 * @cur: the first node in the list
2340 *
2341 * Free a node and all its siblings, this is a recursive behaviour, all
2342 * the children are freed too.
2343 */
2344void
2345xmlFreeNodeList(xmlNodePtr cur) {
2346 xmlNodePtr next;
2347 if (cur == NULL) {
2348#ifdef DEBUG_TREE
2349 xmlGenericError(xmlGenericErrorContext,
2350 "xmlFreeNodeList : node == NULL\n");
2351#endif
2352 return;
2353 }
2354 while (cur != NULL) {
2355 next = cur->next;
Daniel Veillard02141ea2001-04-30 11:46:40 +00002356 /* unroll to speed up freeing the document */
2357 if (cur->type != XML_DTD_NODE) {
2358 if ((cur->children != NULL) &&
2359 (cur->type != XML_ENTITY_REF_NODE))
2360 xmlFreeNodeList(cur->children);
2361 if (cur->properties != NULL)
2362 xmlFreePropList(cur->properties);
2363 if (cur->type != XML_ENTITY_REF_NODE)
2364#ifndef XML_USE_BUFFER_CONTENT
2365 if (cur->content != NULL) xmlFree(cur->content);
2366#else
2367 if (cur->content != NULL) xmlBufferFree(cur->content);
2368#endif
2369 if ((cur->name != NULL) &&
2370 (cur->name != xmlStringText) &&
2371 (cur->name != xmlStringTextNoenc) &&
2372 (cur->name != xmlStringComment))
2373 xmlFree((char *) cur->name);
2374 /* TODO : derecursivate this function */
2375 if (cur->nsDef != NULL) xmlFreeNsList(cur->nsDef);
2376 xmlFree(cur);
2377 }
Owen Taylor3473f882001-02-23 17:55:21 +00002378 cur = next;
2379 }
2380}
2381
2382/**
2383 * xmlFreeNode:
2384 * @cur: the node
2385 *
2386 * Free a node, this is a recursive behaviour, all the children are freed too.
2387 * This doesn't unlink the child from the list, use xmlUnlinkNode() first.
2388 */
2389void
2390xmlFreeNode(xmlNodePtr cur) {
2391 if (cur == NULL) {
2392#ifdef DEBUG_TREE
2393 xmlGenericError(xmlGenericErrorContext,
2394 "xmlFreeNode : node == NULL\n");
2395#endif
2396 return;
2397 }
Daniel Veillard02141ea2001-04-30 11:46:40 +00002398 /* use xmlFreeDtd for DTD nodes */
Owen Taylor3473f882001-02-23 17:55:21 +00002399 if (cur->type == XML_DTD_NODE)
2400 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002401 if ((cur->children != NULL) &&
2402 (cur->type != XML_ENTITY_REF_NODE))
2403 xmlFreeNodeList(cur->children);
Daniel Veillard02141ea2001-04-30 11:46:40 +00002404 if (cur->properties != NULL)
2405 xmlFreePropList(cur->properties);
Owen Taylor3473f882001-02-23 17:55:21 +00002406 if (cur->type != XML_ENTITY_REF_NODE)
2407#ifndef XML_USE_BUFFER_CONTENT
2408 if (cur->content != NULL) xmlFree(cur->content);
2409#else
2410 if (cur->content != NULL) xmlBufferFree(cur->content);
2411#endif
2412 if ((cur->name != NULL) &&
2413 (cur->name != xmlStringText) &&
2414 (cur->name != xmlStringTextNoenc) &&
2415 (cur->name != xmlStringComment))
2416 xmlFree((char *) cur->name);
2417 if (cur->nsDef != NULL) xmlFreeNsList(cur->nsDef);
Owen Taylor3473f882001-02-23 17:55:21 +00002418 xmlFree(cur);
2419}
2420
2421/**
2422 * xmlUnlinkNode:
2423 * @cur: the node
2424 *
2425 * Unlink a node from it's current context, the node is not freed
2426 */
2427void
2428xmlUnlinkNode(xmlNodePtr cur) {
2429 if (cur == NULL) {
2430#ifdef DEBUG_TREE
2431 xmlGenericError(xmlGenericErrorContext,
2432 "xmlUnlinkNode : node == NULL\n");
2433#endif
2434 return;
2435 }
2436 if ((cur->parent != NULL) && (cur->parent->children == cur))
2437 cur->parent->children = cur->next;
2438 if ((cur->parent != NULL) && (cur->parent->last == cur))
2439 cur->parent->last = cur->prev;
2440 if (cur->next != NULL)
2441 cur->next->prev = cur->prev;
2442 if (cur->prev != NULL)
2443 cur->prev->next = cur->next;
2444 cur->next = cur->prev = NULL;
2445 cur->parent = NULL;
2446}
2447
2448/**
2449 * xmlReplaceNode:
2450 * @old: the old node
2451 * @cur: the node
2452 *
2453 * Unlink the old node from it's current context, prune the new one
2454 * at the same place. If cur was already inserted in a document it is
2455 * first unlinked from its existing context.
2456 *
2457 * Returns the old node
2458 */
2459xmlNodePtr
2460xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) {
2461 if (old == NULL) {
2462#ifdef DEBUG_TREE
2463 xmlGenericError(xmlGenericErrorContext,
2464 "xmlReplaceNode : old == NULL\n");
2465#endif
2466 return(NULL);
2467 }
2468 if (cur == NULL) {
2469 xmlUnlinkNode(old);
2470 return(old);
2471 }
2472 if (cur == old) {
2473 return(old);
2474 }
2475 xmlUnlinkNode(cur);
2476 cur->doc = old->doc;
2477 cur->parent = old->parent;
2478 cur->next = old->next;
2479 if (cur->next != NULL)
2480 cur->next->prev = cur;
2481 cur->prev = old->prev;
2482 if (cur->prev != NULL)
2483 cur->prev->next = cur;
2484 if (cur->parent != NULL) {
2485 if (cur->parent->children == old)
2486 cur->parent->children = cur;
2487 if (cur->parent->last == old)
2488 cur->parent->last = cur;
2489 }
2490 old->next = old->prev = NULL;
2491 old->parent = NULL;
2492 return(old);
2493}
2494
2495/************************************************************************
2496 * *
2497 * Copy operations *
2498 * *
2499 ************************************************************************/
2500
2501/**
2502 * xmlCopyNamespace:
2503 * @cur: the namespace
2504 *
2505 * Do a copy of the namespace.
2506 *
2507 * Returns: a new xmlNsPtr, or NULL in case of error.
2508 */
2509xmlNsPtr
2510xmlCopyNamespace(xmlNsPtr cur) {
2511 xmlNsPtr ret;
2512
2513 if (cur == NULL) return(NULL);
2514 switch (cur->type) {
2515 case XML_LOCAL_NAMESPACE:
2516 ret = xmlNewNs(NULL, cur->href, cur->prefix);
2517 break;
2518 default:
2519#ifdef DEBUG_TREE
2520 xmlGenericError(xmlGenericErrorContext,
2521 "xmlCopyNamespace: invalid type %d\n", cur->type);
2522#endif
2523 return(NULL);
2524 }
2525 return(ret);
2526}
2527
2528/**
2529 * xmlCopyNamespaceList:
2530 * @cur: the first namespace
2531 *
2532 * Do a copy of an namespace list.
2533 *
2534 * Returns: a new xmlNsPtr, or NULL in case of error.
2535 */
2536xmlNsPtr
2537xmlCopyNamespaceList(xmlNsPtr cur) {
2538 xmlNsPtr ret = NULL;
2539 xmlNsPtr p = NULL,q;
2540
2541 while (cur != NULL) {
2542 q = xmlCopyNamespace(cur);
2543 if (p == NULL) {
2544 ret = p = q;
2545 } else {
2546 p->next = q;
2547 p = q;
2548 }
2549 cur = cur->next;
2550 }
2551 return(ret);
2552}
2553
2554static xmlNodePtr
2555xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent);
2556/**
2557 * xmlCopyProp:
2558 * @target: the element where the attribute will be grafted
2559 * @cur: the attribute
2560 *
2561 * Do a copy of the attribute.
2562 *
2563 * Returns: a new xmlAttrPtr, or NULL in case of error.
2564 */
2565xmlAttrPtr
2566xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) {
2567 xmlAttrPtr ret;
2568
2569 if (cur == NULL) return(NULL);
2570 if (target != NULL)
2571 ret = xmlNewDocProp(target->doc, cur->name, NULL);
2572 else if (cur->parent != NULL)
2573 ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL);
2574 else if (cur->children != NULL)
2575 ret = xmlNewDocProp(cur->children->doc, cur->name, NULL);
2576 else
2577 ret = xmlNewDocProp(NULL, cur->name, NULL);
2578 if (ret == NULL) return(NULL);
2579 ret->parent = target;
2580
2581 if ((cur->ns != NULL) && (target != NULL)) {
2582 xmlNsPtr ns;
2583
2584 ns = xmlSearchNs(target->doc, target, cur->ns->prefix);
2585 ret->ns = ns;
2586 } else
2587 ret->ns = NULL;
2588
2589 if (cur->children != NULL) {
2590 xmlNodePtr tmp;
2591
2592 ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret);
2593 ret->last = NULL;
2594 tmp = ret->children;
2595 while (tmp != NULL) {
2596 /* tmp->parent = (xmlNodePtr)ret; */
2597 if (tmp->next == NULL)
2598 ret->last = tmp;
2599 tmp = tmp->next;
2600 }
2601 }
2602 return(ret);
2603}
2604
2605/**
2606 * xmlCopyPropList:
2607 * @target: the element where the attributes will be grafted
2608 * @cur: the first attribute
2609 *
2610 * Do a copy of an attribute list.
2611 *
2612 * Returns: a new xmlAttrPtr, or NULL in case of error.
2613 */
2614xmlAttrPtr
2615xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) {
2616 xmlAttrPtr ret = NULL;
2617 xmlAttrPtr p = NULL,q;
2618
2619 while (cur != NULL) {
2620 q = xmlCopyProp(target, cur);
2621 if (p == NULL) {
2622 ret = p = q;
2623 } else {
2624 p->next = q;
2625 q->prev = p;
2626 p = q;
2627 }
2628 cur = cur->next;
2629 }
2630 return(ret);
2631}
2632
2633/*
2634 * NOTE abeut the CopyNode operations !
2635 *
2636 * They are splitted into external and internal parts for one
2637 * tricky reason: namespaces. Doing a direct copy of a node
2638 * say RPM:Copyright without changing the namespace pointer to
2639 * something else can produce stale links. One way to do it is
2640 * to keep a reference counter but this doesn't work as soon
2641 * as one move the element or the subtree out of the scope of
2642 * the existing namespace. The actual solution seems to add
2643 * a copy of the namespace at the top of the copied tree if
2644 * not available in the subtree.
2645 * Hence two functions, the public front-end call the inner ones
2646 */
2647
2648static xmlNodePtr
2649xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent);
2650
2651static xmlNodePtr
2652xmlStaticCopyNode(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent,
2653 int recursive) {
2654 xmlNodePtr ret;
2655
2656 if (node == NULL) return(NULL);
Daniel Veillardb33c2012001-04-25 12:59:04 +00002657
Owen Taylor3473f882001-02-23 17:55:21 +00002658 /*
2659 * Allocate a new node and fill the fields.
2660 */
2661 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2662 if (ret == NULL) {
2663 xmlGenericError(xmlGenericErrorContext,
2664 "xmlStaticCopyNode : malloc failed\n");
2665 return(NULL);
2666 }
2667 memset(ret, 0, sizeof(xmlNode));
2668 ret->type = node->type;
2669
2670 ret->doc = doc;
2671 ret->parent = parent;
2672 if (node->name == xmlStringText)
2673 ret->name = xmlStringText;
2674 else if (node->name == xmlStringTextNoenc)
2675 ret->name = xmlStringTextNoenc;
2676 else if (node->name == xmlStringComment)
2677 ret->name = xmlStringComment;
2678 else if (node->name != NULL)
2679 ret->name = xmlStrdup(node->name);
2680 if ((node->content != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
2681#ifndef XML_USE_BUFFER_CONTENT
2682 ret->content = xmlStrdup(node->content);
2683#else
2684 ret->content = xmlBufferCreateSize(xmlBufferLength(node->content));
2685 xmlBufferSetAllocationScheme(ret->content,
2686 xmlGetBufferAllocationScheme());
2687 xmlBufferAdd(ret->content,
2688 xmlBufferContent(node->content),
2689 xmlBufferLength(node->content));
2690#endif
2691 }
2692 if (parent != NULL)
2693 xmlAddChild(parent, ret);
2694
2695 if (!recursive) return(ret);
2696 if (node->nsDef != NULL)
2697 ret->nsDef = xmlCopyNamespaceList(node->nsDef);
2698
2699 if (node->ns != NULL) {
2700 xmlNsPtr ns;
2701
2702 ns = xmlSearchNs(doc, ret, node->ns->prefix);
2703 if (ns == NULL) {
2704 /*
2705 * Humm, we are copying an element whose namespace is defined
2706 * out of the new tree scope. Search it in the original tree
2707 * and add it at the top of the new tree
2708 */
2709 ns = xmlSearchNs(node->doc, node, node->ns->prefix);
2710 if (ns != NULL) {
2711 xmlNodePtr root = ret;
2712
2713 while (root->parent != NULL) root = root->parent;
Daniel Veillarde82a9922001-04-22 12:12:58 +00002714 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00002715 }
2716 } else {
2717 /*
2718 * reference the existing namespace definition in our own tree.
2719 */
2720 ret->ns = ns;
2721 }
2722 }
2723 if (node->properties != NULL)
2724 ret->properties = xmlCopyPropList(ret, node->properties);
Daniel Veillardb33c2012001-04-25 12:59:04 +00002725 if (node->type == XML_ENTITY_REF_NODE) {
2726 if ((doc == NULL) || (node->doc != doc)) {
2727 /*
2728 * The copied node will go into a separate document, so
2729 * to havoid dandling references to the ENTITY_DECL node
2730 * we cannot keep the reference. Try to find it in the
2731 * target document.
2732 */
2733 ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name);
2734 } else {
2735 ret->children = node->children;
2736 }
2737 } else if (node->children != NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00002738 ret->children = xmlStaticCopyNodeList(node->children, doc, ret);
2739 UPDATE_LAST_CHILD_AND_PARENT(ret)
2740 return(ret);
2741}
2742
2743static xmlNodePtr
2744xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) {
2745 xmlNodePtr ret = NULL;
2746 xmlNodePtr p = NULL,q;
2747
2748 while (node != NULL) {
Daniel Veillardb33c2012001-04-25 12:59:04 +00002749 if( node->type == XML_DTD_NODE ) {
2750 if (doc->intSubset == NULL) {
2751 q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node );
2752 q->doc = doc;
2753 q->parent = parent;
2754 doc->intSubset = (xmlDtdPtr) q;
2755 } else {
2756 q = (xmlNodePtr) doc->intSubset;
2757 }
2758 } else
2759 q = xmlStaticCopyNode(node, doc, parent, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00002760 if (ret == NULL) {
2761 q->prev = NULL;
2762 ret = p = q;
2763 } else {
2764 p->next = q;
2765 q->prev = p;
2766 p = q;
2767 }
2768 node = node->next;
2769 }
2770 return(ret);
2771}
2772
2773/**
2774 * xmlCopyNode:
2775 * @node: the node
2776 * @recursive: if 1 do a recursive copy.
2777 *
2778 * Do a copy of the node.
2779 *
2780 * Returns: a new xmlNodePtr, or NULL in case of error.
2781 */
2782xmlNodePtr
2783xmlCopyNode(xmlNodePtr node, int recursive) {
2784 xmlNodePtr ret;
2785
2786 ret = xmlStaticCopyNode(node, NULL, NULL, recursive);
2787 return(ret);
2788}
2789
2790/**
Daniel Veillard82daa812001-04-12 08:55:36 +00002791 * xmlDocCopyNode:
2792 * @node: the node
2793 * @recursive: if 1 do a recursive copy.
2794 *
2795 * Do a copy of the node to a given document.
2796 *
2797 * Returns: a new xmlNodePtr, or NULL in case of error.
2798 */
2799xmlNodePtr
2800xmlDocCopyNode(xmlNodePtr node, xmlDocPtr doc, int recursive) {
2801 xmlNodePtr ret;
2802
2803 ret = xmlStaticCopyNode(node, doc, NULL, recursive);
2804 return(ret);
2805}
2806
2807/**
Owen Taylor3473f882001-02-23 17:55:21 +00002808 * xmlCopyNodeList:
2809 * @node: the first node in the list.
2810 *
2811 * Do a recursive copy of the node list.
2812 *
2813 * Returns: a new xmlNodePtr, or NULL in case of error.
2814 */
2815xmlNodePtr xmlCopyNodeList(xmlNodePtr node) {
2816 xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL);
2817 return(ret);
2818}
2819
2820/**
2821 * xmlCopyElement:
2822 * @elem: the element
2823 *
2824 * Do a copy of the element definition.
2825 *
2826 * Returns: a new xmlElementPtr, or NULL in case of error.
2827xmlElementPtr
2828xmlCopyElement(xmlElementPtr elem) {
2829 xmlElementPtr ret;
2830
2831 if (elem == NULL) return(NULL);
2832 ret = xmlNewDocElement(elem->doc, elem->ns, elem->name, elem->content);
2833 if (ret == NULL) return(NULL);
2834 if (!recursive) return(ret);
2835 if (elem->properties != NULL)
2836 ret->properties = xmlCopyPropList(elem->properties);
2837
2838 if (elem->nsDef != NULL)
2839 ret->nsDef = xmlCopyNamespaceList(elem->nsDef);
2840 if (elem->children != NULL)
2841 ret->children = xmlCopyElementList(elem->children);
2842 return(ret);
2843}
2844 */
2845
2846/**
2847 * xmlCopyDtd:
2848 * @dtd: the dtd
2849 *
2850 * Do a copy of the dtd.
2851 *
2852 * Returns: a new xmlDtdPtr, or NULL in case of error.
2853 */
2854xmlDtdPtr
2855xmlCopyDtd(xmlDtdPtr dtd) {
2856 xmlDtdPtr ret;
2857
2858 if (dtd == NULL) return(NULL);
2859 ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID);
2860 if (ret == NULL) return(NULL);
2861 if (dtd->entities != NULL)
2862 ret->entities = (void *) xmlCopyEntitiesTable(
2863 (xmlEntitiesTablePtr) dtd->entities);
2864 if (dtd->notations != NULL)
2865 ret->notations = (void *) xmlCopyNotationTable(
2866 (xmlNotationTablePtr) dtd->notations);
2867 if (dtd->elements != NULL)
2868 ret->elements = (void *) xmlCopyElementTable(
2869 (xmlElementTablePtr) dtd->elements);
2870 if (dtd->attributes != NULL)
2871 ret->attributes = (void *) xmlCopyAttributeTable(
2872 (xmlAttributeTablePtr) dtd->attributes);
2873 return(ret);
2874}
2875
2876/**
2877 * xmlCopyDoc:
2878 * @doc: the document
2879 * @recursive: if 1 do a recursive copy.
2880 *
2881 * Do a copy of the document info. If recursive, the content tree will
2882 * be copied too as well as Dtd, namespaces and entities.
2883 *
2884 * Returns: a new xmlDocPtr, or NULL in case of error.
2885 */
2886xmlDocPtr
2887xmlCopyDoc(xmlDocPtr doc, int recursive) {
2888 xmlDocPtr ret;
2889
2890 if (doc == NULL) return(NULL);
2891 ret = xmlNewDoc(doc->version);
2892 if (ret == NULL) return(NULL);
2893 if (doc->name != NULL)
2894 ret->name = xmlMemStrdup(doc->name);
2895 if (doc->encoding != NULL)
2896 ret->encoding = xmlStrdup(doc->encoding);
2897 ret->charset = doc->charset;
2898 ret->compression = doc->compression;
2899 ret->standalone = doc->standalone;
2900 if (!recursive) return(ret);
2901
Daniel Veillardb33c2012001-04-25 12:59:04 +00002902 ret->last = NULL;
2903 ret->children = NULL;
2904 if (doc->intSubset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002905 ret->intSubset = xmlCopyDtd(doc->intSubset);
Daniel Veillardb33c2012001-04-25 12:59:04 +00002906 ret->intSubset->doc = ret;
2907 ret->intSubset->parent = ret;
2908 }
Owen Taylor3473f882001-02-23 17:55:21 +00002909 if (doc->oldNs != NULL)
2910 ret->oldNs = xmlCopyNamespaceList(doc->oldNs);
2911 if (doc->children != NULL) {
2912 xmlNodePtr tmp;
Daniel Veillardb33c2012001-04-25 12:59:04 +00002913
2914 ret->children = xmlStaticCopyNodeList(doc->children, ret,
2915 (xmlNodePtr)ret);
Owen Taylor3473f882001-02-23 17:55:21 +00002916 ret->last = NULL;
2917 tmp = ret->children;
2918 while (tmp != NULL) {
2919 if (tmp->next == NULL)
2920 ret->last = tmp;
2921 tmp = tmp->next;
2922 }
2923 }
2924 return(ret);
2925}
2926
2927/************************************************************************
2928 * *
2929 * Content access functions *
2930 * *
2931 ************************************************************************/
2932
2933/**
2934 * xmlDocGetRootElement:
2935 * @doc: the document
2936 *
2937 * Get the root element of the document (doc->children is a list
2938 * containing possibly comments, PIs, etc ...).
2939 *
2940 * Returns the xmlNodePtr for the root or NULL
2941 */
2942xmlNodePtr
2943xmlDocGetRootElement(xmlDocPtr doc) {
2944 xmlNodePtr ret;
2945
2946 if (doc == NULL) return(NULL);
2947 ret = doc->children;
2948 while (ret != NULL) {
2949 if (ret->type == XML_ELEMENT_NODE)
2950 return(ret);
2951 ret = ret->next;
2952 }
2953 return(ret);
2954}
2955
2956/**
2957 * xmlDocSetRootElement:
2958 * @doc: the document
2959 * @root: the new document root element
2960 *
2961 * Set the root element of the document (doc->children is a list
2962 * containing possibly comments, PIs, etc ...).
2963 *
2964 * Returns the old root element if any was found
2965 */
2966xmlNodePtr
2967xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) {
2968 xmlNodePtr old = NULL;
2969
2970 if (doc == NULL) return(NULL);
2971 old = doc->children;
2972 while (old != NULL) {
2973 if (old->type == XML_ELEMENT_NODE)
2974 break;
2975 old = old->next;
2976 }
2977 if (old == NULL) {
2978 if (doc->children == NULL) {
2979 doc->children = root;
2980 doc->last = root;
2981 } else {
2982 xmlAddSibling(doc->children, root);
2983 }
2984 } else {
2985 xmlReplaceNode(old, root);
2986 }
2987 return(old);
2988}
2989
2990/**
2991 * xmlNodeSetLang:
2992 * @cur: the node being changed
2993 * @lang: the langage description
2994 *
2995 * Set the language of a node, i.e. the values of the xml:lang
2996 * attribute.
2997 */
2998void
2999xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) {
3000 if (cur == NULL) return;
3001 switch(cur->type) {
3002 case XML_TEXT_NODE:
3003 case XML_CDATA_SECTION_NODE:
3004 case XML_COMMENT_NODE:
3005 case XML_DOCUMENT_NODE:
3006 case XML_DOCUMENT_TYPE_NODE:
3007 case XML_DOCUMENT_FRAG_NODE:
3008 case XML_NOTATION_NODE:
3009 case XML_HTML_DOCUMENT_NODE:
3010 case XML_DTD_NODE:
3011 case XML_ELEMENT_DECL:
3012 case XML_ATTRIBUTE_DECL:
3013 case XML_ENTITY_DECL:
3014 case XML_PI_NODE:
3015 case XML_ENTITY_REF_NODE:
3016 case XML_ENTITY_NODE:
3017 case XML_NAMESPACE_DECL:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003018#ifdef LIBXML_DOCB_ENABLED
3019 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003020#endif
3021 case XML_XINCLUDE_START:
3022 case XML_XINCLUDE_END:
3023 return;
3024 case XML_ELEMENT_NODE:
3025 case XML_ATTRIBUTE_NODE:
3026 break;
3027 }
3028 xmlSetProp(cur, BAD_CAST "xml:lang", lang);
3029}
3030
3031/**
3032 * xmlNodeGetLang:
3033 * @cur: the node being checked
3034 *
3035 * Searches the language of a node, i.e. the values of the xml:lang
3036 * attribute or the one carried by the nearest ancestor.
3037 *
3038 * Returns a pointer to the lang value, or NULL if not found
3039 * It's up to the caller to free the memory.
3040 */
3041xmlChar *
3042xmlNodeGetLang(xmlNodePtr cur) {
3043 xmlChar *lang;
3044
3045 while (cur != NULL) {
Daniel Veillardc17337c2001-05-09 10:51:31 +00003046 lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE);
Owen Taylor3473f882001-02-23 17:55:21 +00003047 if (lang != NULL)
3048 return(lang);
3049 cur = cur->parent;
3050 }
3051 return(NULL);
3052}
3053
3054
3055/**
3056 * xmlNodeSetSpacePreserve:
3057 * @cur: the node being changed
3058 * @val: the xml:space value ("0": default, 1: "preserve")
3059 *
3060 * Set (or reset) the space preserving behaviour of a node, i.e. the
3061 * value of the xml:space attribute.
3062 */
3063void
3064xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) {
3065 if (cur == NULL) return;
3066 switch(cur->type) {
3067 case XML_TEXT_NODE:
3068 case XML_CDATA_SECTION_NODE:
3069 case XML_COMMENT_NODE:
3070 case XML_DOCUMENT_NODE:
3071 case XML_DOCUMENT_TYPE_NODE:
3072 case XML_DOCUMENT_FRAG_NODE:
3073 case XML_NOTATION_NODE:
3074 case XML_HTML_DOCUMENT_NODE:
3075 case XML_DTD_NODE:
3076 case XML_ELEMENT_DECL:
3077 case XML_ATTRIBUTE_DECL:
3078 case XML_ENTITY_DECL:
3079 case XML_PI_NODE:
3080 case XML_ENTITY_REF_NODE:
3081 case XML_ENTITY_NODE:
3082 case XML_NAMESPACE_DECL:
3083 case XML_XINCLUDE_START:
3084 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003085#ifdef LIBXML_DOCB_ENABLED
3086 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003087#endif
3088 return;
3089 case XML_ELEMENT_NODE:
3090 case XML_ATTRIBUTE_NODE:
3091 break;
3092 }
3093 switch (val) {
3094 case 0:
3095 xmlSetProp(cur, BAD_CAST "xml:space", BAD_CAST "default");
3096 break;
3097 case 1:
3098 xmlSetProp(cur, BAD_CAST "xml:space",
3099 BAD_CAST "preserve");
3100 break;
3101 }
3102}
3103
3104/**
3105 * xmlNodeGetSpacePreserve:
3106 * @cur: the node being checked
3107 *
3108 * Searches the space preserving behaviour of a node, i.e. the values
3109 * of the xml:space attribute or the one carried by the nearest
3110 * ancestor.
3111 *
3112 * Returns -1 if xml:space is not inheried, 0 if "default", 1 if "preserve"
3113 */
3114int
3115xmlNodeGetSpacePreserve(xmlNodePtr cur) {
3116 xmlChar *space;
3117
3118 while (cur != NULL) {
3119 space = xmlGetProp(cur, BAD_CAST "xml:space");
3120 if (space != NULL) {
3121 if (xmlStrEqual(space, BAD_CAST "preserve")) {
3122 xmlFree(space);
3123 return(1);
3124 }
3125 if (xmlStrEqual(space, BAD_CAST "default")) {
3126 xmlFree(space);
3127 return(0);
3128 }
3129 xmlFree(space);
3130 }
3131 cur = cur->parent;
3132 }
3133 return(-1);
3134}
3135
3136/**
3137 * xmlNodeSetName:
3138 * @cur: the node being changed
3139 * @name: the new tag name
3140 *
3141 * Set (or reset) the name of a node.
3142 */
3143void
3144xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) {
3145 if (cur == NULL) return;
3146 if (name == NULL) return;
3147 switch(cur->type) {
3148 case XML_TEXT_NODE:
3149 case XML_CDATA_SECTION_NODE:
3150 case XML_COMMENT_NODE:
3151 case XML_DOCUMENT_TYPE_NODE:
3152 case XML_DOCUMENT_FRAG_NODE:
3153 case XML_NOTATION_NODE:
3154 case XML_HTML_DOCUMENT_NODE:
3155 case XML_NAMESPACE_DECL:
3156 case XML_XINCLUDE_START:
3157 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003158#ifdef LIBXML_DOCB_ENABLED
3159 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003160#endif
3161 return;
3162 case XML_ELEMENT_NODE:
3163 case XML_ATTRIBUTE_NODE:
3164 case XML_PI_NODE:
3165 case XML_ENTITY_REF_NODE:
3166 case XML_ENTITY_NODE:
3167 case XML_DTD_NODE:
3168 case XML_DOCUMENT_NODE:
3169 case XML_ELEMENT_DECL:
3170 case XML_ATTRIBUTE_DECL:
3171 case XML_ENTITY_DECL:
3172 break;
3173 }
3174 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
3175 cur->name = xmlStrdup(name);
3176}
3177
3178/**
3179 * xmlNodeSetBase:
3180 * @cur: the node being changed
3181 * @uri: the new base URI
3182 *
3183 * Set (or reset) the base URI of a node, i.e. the value of the
3184 * xml:base attribute.
3185 */
3186void
3187xmlNodeSetBase(xmlNodePtr cur, xmlChar* uri) {
3188 if (cur == NULL) return;
3189 switch(cur->type) {
3190 case XML_TEXT_NODE:
3191 case XML_CDATA_SECTION_NODE:
3192 case XML_COMMENT_NODE:
3193 case XML_DOCUMENT_NODE:
3194 case XML_DOCUMENT_TYPE_NODE:
3195 case XML_DOCUMENT_FRAG_NODE:
3196 case XML_NOTATION_NODE:
3197 case XML_HTML_DOCUMENT_NODE:
3198 case XML_DTD_NODE:
3199 case XML_ELEMENT_DECL:
3200 case XML_ATTRIBUTE_DECL:
3201 case XML_ENTITY_DECL:
3202 case XML_PI_NODE:
3203 case XML_ENTITY_REF_NODE:
3204 case XML_ENTITY_NODE:
3205 case XML_NAMESPACE_DECL:
3206 case XML_XINCLUDE_START:
3207 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003208#ifdef LIBXML_DOCB_ENABLED
3209 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003210#endif
3211 return;
3212 case XML_ELEMENT_NODE:
3213 case XML_ATTRIBUTE_NODE:
3214 break;
3215 }
3216 xmlSetProp(cur, BAD_CAST "xml:base", uri);
3217}
3218
3219/**
Owen Taylor3473f882001-02-23 17:55:21 +00003220 * xmlNodeGetBase:
3221 * @doc: the document the node pertains to
3222 * @cur: the node being checked
3223 *
3224 * Searches for the BASE URL. The code should work on both XML
3225 * and HTML document even if base mechanisms are completely different.
3226 * It returns the base as defined in RFC 2396 sections
3227 * 5.1.1. Base URI within Document Content
3228 * and
3229 * 5.1.2. Base URI from the Encapsulating Entity
3230 * However it does not return the document base (5.1.3), use
3231 * xmlDocumentGetBase() for this
3232 *
3233 * Returns a pointer to the base URL, or NULL if not found
3234 * It's up to the caller to free the memory.
3235 */
3236xmlChar *
3237xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) {
3238 xmlChar *base;
3239
3240 if ((cur == NULL) && (doc == NULL))
3241 return(NULL);
3242 if (doc == NULL) doc = cur->doc;
3243 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
3244 cur = doc->children;
3245 while ((cur != NULL) && (cur->name != NULL)) {
3246 if (cur->type != XML_ELEMENT_NODE) {
3247 cur = cur->next;
3248 continue;
3249 }
3250 if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) {
3251 cur = cur->children;
3252 continue;
3253 }
3254 if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) {
3255 cur = cur->children;
3256 continue;
3257 }
3258 if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) {
3259 return(xmlGetProp(cur, BAD_CAST "href"));
3260 }
3261 cur = cur->next;
3262 }
3263 return(NULL);
3264 }
3265 while (cur != NULL) {
3266 if (cur->type == XML_ENTITY_DECL) {
3267 xmlEntityPtr ent = (xmlEntityPtr) cur;
3268 return(xmlStrdup(ent->URI));
3269 }
3270 base = xmlGetProp(cur, BAD_CAST "xml:base");
3271 if (base != NULL)
3272 return(base);
3273 cur = cur->parent;
3274 }
3275 if ((doc != NULL) && (doc->URL != NULL))
3276 return(xmlStrdup(doc->URL));
3277 return(NULL);
3278}
3279
3280/**
3281 * xmlNodeGetContent:
3282 * @cur: the node being read
3283 *
3284 * Read the value of a node, this can be either the text carried
3285 * directly by this node if it's a TEXT node or the aggregate string
3286 * of the values carried by this node child's (TEXT and ENTITY_REF).
3287 * Entity references are substitued.
3288 * Returns a new xmlChar * or NULL if no content is available.
3289 * It's up to the caller to free the memory.
3290 */
3291xmlChar *
3292xmlNodeGetContent(xmlNodePtr cur) {
3293 if (cur == NULL) return(NULL);
3294 switch (cur->type) {
3295 case XML_DOCUMENT_FRAG_NODE:
3296 case XML_ELEMENT_NODE: {
3297 xmlNodePtr tmp = cur;
3298 xmlBufferPtr buffer;
3299 xmlChar *ret;
3300
3301 buffer = xmlBufferCreate();
3302 if (buffer == NULL)
3303 return(NULL);
3304 while (tmp != NULL) {
3305 switch (tmp->type) {
3306 case XML_ELEMENT_NODE:
3307 case XML_TEXT_NODE:
3308 if (tmp->content != NULL)
3309#ifndef XML_USE_BUFFER_CONTENT
3310 xmlBufferCat(buffer, tmp->content);
3311#else
3312 xmlBufferCat(buffer,
3313 xmlBufferContent(tmp->content));
3314#endif
3315 break;
3316 case XML_ENTITY_REF_NODE: {
3317 xmlEntityPtr ent;
3318
3319 ent = xmlGetDocEntity(cur->doc, tmp->name);
3320 if (ent != NULL)
3321 xmlBufferCat(buffer, ent->content);
3322 }
3323 default:
3324 break;
3325 }
3326 /*
3327 * Skip to next node
3328 */
3329 if (tmp->children != NULL) {
3330 if (tmp->children->type != XML_ENTITY_DECL) {
3331 tmp = tmp->children;
3332 continue;
3333 }
3334 }
Daniel Veillard6c831202001-03-07 15:57:53 +00003335 if (tmp == cur)
3336 break;
3337
Owen Taylor3473f882001-02-23 17:55:21 +00003338 if (tmp->next != NULL) {
3339 tmp = tmp->next;
3340 continue;
3341 }
3342
3343 do {
3344 tmp = tmp->parent;
3345 if (tmp == NULL)
3346 break;
Daniel Veillard6c831202001-03-07 15:57:53 +00003347 if (tmp == cur) {
Owen Taylor3473f882001-02-23 17:55:21 +00003348 tmp = NULL;
3349 break;
3350 }
3351 if (tmp->next != NULL) {
3352 tmp = tmp->next;
3353 break;
3354 }
3355 } while (tmp != NULL);
3356 }
3357 ret = buffer->content;
3358 buffer->content = NULL;
3359 xmlBufferFree(buffer);
3360 return(ret);
3361 }
3362 case XML_ATTRIBUTE_NODE: {
3363 xmlAttrPtr attr = (xmlAttrPtr) cur;
3364 if (attr->parent != NULL)
3365 return(xmlNodeListGetString(attr->parent->doc, attr->children, 1));
3366 else
3367 return(xmlNodeListGetString(NULL, attr->children, 1));
3368 break;
3369 }
3370 case XML_COMMENT_NODE:
3371 case XML_PI_NODE:
3372 if (cur->content != NULL)
3373#ifndef XML_USE_BUFFER_CONTENT
3374 return(xmlStrdup(cur->content));
3375#else
3376 return(xmlStrdup(xmlBufferContent(cur->content)));
3377#endif
3378 return(NULL);
3379 case XML_ENTITY_REF_NODE:
3380 /*
3381 * Locate the entity, and get it's content
3382 * @@@
3383 */
3384 return(NULL);
3385 case XML_ENTITY_NODE:
3386 case XML_DOCUMENT_NODE:
3387 case XML_HTML_DOCUMENT_NODE:
3388 case XML_DOCUMENT_TYPE_NODE:
3389 case XML_NOTATION_NODE:
3390 case XML_DTD_NODE:
3391 case XML_XINCLUDE_START:
3392 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003393#ifdef LIBXML_DOCB_ENABLED
3394 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003395#endif
3396 return(NULL);
3397 case XML_NAMESPACE_DECL:
3398 return(xmlStrdup(((xmlNsPtr)cur)->href));
3399 case XML_ELEMENT_DECL:
3400 /* TODO !!! */
3401 return(NULL);
3402 case XML_ATTRIBUTE_DECL:
3403 /* TODO !!! */
3404 return(NULL);
3405 case XML_ENTITY_DECL:
3406 /* TODO !!! */
3407 return(NULL);
3408 case XML_CDATA_SECTION_NODE:
3409 case XML_TEXT_NODE:
3410 if (cur->content != NULL)
3411#ifndef XML_USE_BUFFER_CONTENT
3412 return(xmlStrdup(cur->content));
3413#else
3414 return(xmlStrdup(xmlBufferContent(cur->content)));
3415#endif
3416 return(NULL);
3417 }
3418 return(NULL);
3419}
3420
3421/**
3422 * xmlNodeSetContent:
3423 * @cur: the node being modified
3424 * @content: the new value of the content
3425 *
3426 * Replace the content of a node.
3427 */
3428void
3429xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) {
3430 if (cur == NULL) {
3431#ifdef DEBUG_TREE
3432 xmlGenericError(xmlGenericErrorContext,
3433 "xmlNodeSetContent : node == NULL\n");
3434#endif
3435 return;
3436 }
3437 switch (cur->type) {
3438 case XML_DOCUMENT_FRAG_NODE:
3439 case XML_ELEMENT_NODE:
3440 if (cur->content != NULL) {
3441#ifndef XML_USE_BUFFER_CONTENT
3442 xmlFree(cur->content);
3443#else
3444 xmlBufferFree(cur->content);
3445#endif
3446 cur->content = NULL;
3447 }
3448 if (cur->children != NULL) xmlFreeNodeList(cur->children);
3449 cur->children = xmlStringGetNodeList(cur->doc, content);
3450 UPDATE_LAST_CHILD_AND_PARENT(cur)
3451 break;
3452 case XML_ATTRIBUTE_NODE:
3453 break;
3454 case XML_TEXT_NODE:
3455 case XML_CDATA_SECTION_NODE:
3456 case XML_ENTITY_REF_NODE:
3457 case XML_ENTITY_NODE:
3458 case XML_PI_NODE:
3459 case XML_COMMENT_NODE:
3460 if (cur->content != NULL) {
3461#ifndef XML_USE_BUFFER_CONTENT
3462 xmlFree(cur->content);
3463#else
3464 xmlBufferFree(cur->content);
3465#endif
3466 }
3467 if (cur->children != NULL) xmlFreeNodeList(cur->children);
3468 cur->last = cur->children = NULL;
3469 if (content != NULL) {
3470#ifndef XML_USE_BUFFER_CONTENT
3471 cur->content = xmlStrdup(content);
3472#else
3473 cur->content = xmlBufferCreateSize(0);
3474 xmlBufferSetAllocationScheme(cur->content,
3475 xmlGetBufferAllocationScheme());
3476 xmlBufferAdd(cur->content, content, -1);
3477#endif
3478 } else
3479 cur->content = NULL;
3480 break;
3481 case XML_DOCUMENT_NODE:
3482 case XML_HTML_DOCUMENT_NODE:
3483 case XML_DOCUMENT_TYPE_NODE:
3484 case XML_XINCLUDE_START:
3485 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003486#ifdef LIBXML_DOCB_ENABLED
3487 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003488#endif
3489 break;
3490 case XML_NOTATION_NODE:
3491 break;
3492 case XML_DTD_NODE:
3493 break;
3494 case XML_NAMESPACE_DECL:
3495 break;
3496 case XML_ELEMENT_DECL:
3497 /* TODO !!! */
3498 break;
3499 case XML_ATTRIBUTE_DECL:
3500 /* TODO !!! */
3501 break;
3502 case XML_ENTITY_DECL:
3503 /* TODO !!! */
3504 break;
3505 }
3506}
3507
3508/**
3509 * xmlNodeSetContentLen:
3510 * @cur: the node being modified
3511 * @content: the new value of the content
3512 * @len: the size of @content
3513 *
3514 * Replace the content of a node.
3515 */
3516void
3517xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
3518 if (cur == NULL) {
3519#ifdef DEBUG_TREE
3520 xmlGenericError(xmlGenericErrorContext,
3521 "xmlNodeSetContentLen : node == NULL\n");
3522#endif
3523 return;
3524 }
3525 switch (cur->type) {
3526 case XML_DOCUMENT_FRAG_NODE:
3527 case XML_ELEMENT_NODE:
3528 if (cur->content != NULL) {
3529#ifndef XML_USE_BUFFER_CONTENT
3530 xmlFree(cur->content);
3531#else
3532 xmlBufferFree(cur->content);
3533#endif
3534 cur->content = NULL;
3535 }
3536 if (cur->children != NULL) xmlFreeNodeList(cur->children);
3537 cur->children = xmlStringLenGetNodeList(cur->doc, content, len);
3538 UPDATE_LAST_CHILD_AND_PARENT(cur)
3539 break;
3540 case XML_ATTRIBUTE_NODE:
3541 break;
3542 case XML_TEXT_NODE:
3543 case XML_CDATA_SECTION_NODE:
3544 case XML_ENTITY_REF_NODE:
3545 case XML_ENTITY_NODE:
3546 case XML_PI_NODE:
3547 case XML_COMMENT_NODE:
3548 case XML_NOTATION_NODE:
3549 if (cur->content != NULL) {
3550#ifndef XML_USE_BUFFER_CONTENT
3551 xmlFree(cur->content);
3552#else
3553 xmlBufferFree(cur->content);
3554#endif
3555 }
3556 if (cur->children != NULL) xmlFreeNodeList(cur->children);
3557 cur->children = cur->last = NULL;
3558 if (content != NULL) {
3559#ifndef XML_USE_BUFFER_CONTENT
3560 cur->content = xmlStrndup(content, len);
3561#else
3562 cur->content = xmlBufferCreateSize(len);
3563 xmlBufferSetAllocationScheme(cur->content,
3564 xmlGetBufferAllocationScheme());
3565 xmlBufferAdd(cur->content, content, len);
3566#endif
3567 } else
3568 cur->content = NULL;
3569 break;
3570 case XML_DOCUMENT_NODE:
3571 case XML_DTD_NODE:
3572 case XML_HTML_DOCUMENT_NODE:
3573 case XML_DOCUMENT_TYPE_NODE:
3574 case XML_NAMESPACE_DECL:
3575 case XML_XINCLUDE_START:
3576 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003577#ifdef LIBXML_DOCB_ENABLED
3578 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003579#endif
3580 break;
3581 case XML_ELEMENT_DECL:
3582 /* TODO !!! */
3583 break;
3584 case XML_ATTRIBUTE_DECL:
3585 /* TODO !!! */
3586 break;
3587 case XML_ENTITY_DECL:
3588 /* TODO !!! */
3589 break;
3590 }
3591}
3592
3593/**
3594 * xmlNodeAddContentLen:
3595 * @cur: the node being modified
3596 * @content: extra content
3597 * @len: the size of @content
3598 *
3599 * Append the extra substring to the node content.
3600 */
3601void
3602xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
3603 if (cur == NULL) {
3604#ifdef DEBUG_TREE
3605 xmlGenericError(xmlGenericErrorContext,
3606 "xmlNodeAddContentLen : node == NULL\n");
3607#endif
3608 return;
3609 }
3610 if (len <= 0) return;
3611 switch (cur->type) {
3612 case XML_DOCUMENT_FRAG_NODE:
3613 case XML_ELEMENT_NODE: {
3614 xmlNodePtr last = NULL, newNode;
3615
3616 if (cur->children != NULL) {
3617 last = cur->last;
3618 } else {
3619 if (cur->content != NULL) {
3620#ifndef XML_USE_BUFFER_CONTENT
3621 cur->children = xmlStringGetNodeList(cur->doc, cur->content);
3622#else
3623 cur->children = xmlStringGetNodeList(cur->doc,
3624 xmlBufferContent(cur->content));
3625#endif
3626 UPDATE_LAST_CHILD_AND_PARENT(cur)
3627#ifndef XML_USE_BUFFER_CONTENT
3628 xmlFree(cur->content);
3629#else
3630 xmlBufferFree(cur->content);
3631#endif
3632 cur->content = NULL;
3633 last = cur->last;
3634 }
3635 }
3636 newNode = xmlNewTextLen(content, len);
3637 if (newNode != NULL) {
3638 xmlAddChild(cur, newNode);
3639 if ((last != NULL) && (last->next == newNode)) {
3640 xmlTextMerge(last, newNode);
3641 }
3642 }
3643 break;
3644 }
3645 case XML_ATTRIBUTE_NODE:
3646 break;
3647 case XML_TEXT_NODE:
3648 case XML_CDATA_SECTION_NODE:
3649 case XML_ENTITY_REF_NODE:
3650 case XML_ENTITY_NODE:
3651 case XML_PI_NODE:
3652 case XML_COMMENT_NODE:
3653 case XML_NOTATION_NODE:
3654 if (content != NULL) {
3655#ifndef XML_USE_BUFFER_CONTENT
3656 cur->content = xmlStrncat(cur->content, content, len);
3657#else
3658 xmlBufferAdd(cur->content, content, len);
3659#endif
3660 }
3661 case XML_DOCUMENT_NODE:
3662 case XML_DTD_NODE:
3663 case XML_HTML_DOCUMENT_NODE:
3664 case XML_DOCUMENT_TYPE_NODE:
3665 case XML_NAMESPACE_DECL:
3666 case XML_XINCLUDE_START:
3667 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003668#ifdef LIBXML_DOCB_ENABLED
3669 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003670#endif
3671 break;
3672 case XML_ELEMENT_DECL:
3673 case XML_ATTRIBUTE_DECL:
3674 case XML_ENTITY_DECL:
3675 break;
3676 }
3677}
3678
3679/**
3680 * xmlNodeAddContent:
3681 * @cur: the node being modified
3682 * @content: extra content
3683 *
3684 * Append the extra substring to the node content.
3685 */
3686void
3687xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) {
3688 int len;
3689
3690 if (cur == NULL) {
3691#ifdef DEBUG_TREE
3692 xmlGenericError(xmlGenericErrorContext,
3693 "xmlNodeAddContent : node == NULL\n");
3694#endif
3695 return;
3696 }
3697 if (content == NULL) return;
3698 len = xmlStrlen(content);
3699 xmlNodeAddContentLen(cur, content, len);
3700}
3701
3702/**
3703 * xmlTextMerge:
3704 * @first: the first text node
3705 * @second: the second text node being merged
3706 *
3707 * Merge two text nodes into one
3708 * Returns the first text node augmented
3709 */
3710xmlNodePtr
3711xmlTextMerge(xmlNodePtr first, xmlNodePtr second) {
3712 if (first == NULL) return(second);
3713 if (second == NULL) return(first);
3714 if (first->type != XML_TEXT_NODE) return(first);
3715 if (second->type != XML_TEXT_NODE) return(first);
3716 if (second->name != first->name)
3717 return(first);
3718#ifndef XML_USE_BUFFER_CONTENT
3719 xmlNodeAddContent(first, second->content);
3720#else
3721 xmlNodeAddContent(first, xmlBufferContent(second->content));
3722#endif
3723 xmlUnlinkNode(second);
3724 xmlFreeNode(second);
3725 return(first);
3726}
3727
3728/**
3729 * xmlGetNsList:
3730 * @doc: the document
3731 * @node: the current node
3732 *
3733 * Search all the namespace applying to a given element.
3734 * Returns an NULL terminated array of all the xmlNsPtr found
3735 * that need to be freed by the caller or NULL if no
3736 * namespace if defined
3737 */
3738xmlNsPtr *
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003739xmlGetNsList(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00003740 xmlNsPtr cur;
3741 xmlNsPtr *ret = NULL;
3742 int nbns = 0;
3743 int maxns = 10;
3744 int i;
3745
3746 while (node != NULL) {
3747 cur = node->nsDef;
3748 while (cur != NULL) {
3749 if (ret == NULL) {
3750 ret = (xmlNsPtr *) xmlMalloc((maxns + 1) * sizeof(xmlNsPtr));
3751 if (ret == NULL) {
3752 xmlGenericError(xmlGenericErrorContext,
3753 "xmlGetNsList : out of memory!\n");
3754 return(NULL);
3755 }
3756 ret[nbns] = NULL;
3757 }
3758 for (i = 0;i < nbns;i++) {
3759 if ((cur->prefix == ret[i]->prefix) ||
3760 (xmlStrEqual(cur->prefix, ret[i]->prefix))) break;
3761 }
3762 if (i >= nbns) {
3763 if (nbns >= maxns) {
3764 maxns *= 2;
3765 ret = (xmlNsPtr *) xmlRealloc(ret,
3766 (maxns + 1) * sizeof(xmlNsPtr));
3767 if (ret == NULL) {
3768 xmlGenericError(xmlGenericErrorContext,
3769 "xmlGetNsList : realloc failed!\n");
3770 return(NULL);
3771 }
3772 }
3773 ret[nbns++] = cur;
3774 ret[nbns] = NULL;
3775 }
3776
3777 cur = cur->next;
3778 }
3779 node = node->parent;
3780 }
3781 return(ret);
3782}
3783
3784/**
3785 * xmlSearchNs:
3786 * @doc: the document
3787 * @node: the current node
Daniel Veillard77851712001-02-27 21:54:07 +00003788 * @nameSpace: the namespace prefix
Owen Taylor3473f882001-02-23 17:55:21 +00003789 *
3790 * Search a Ns registered under a given name space for a document.
3791 * recurse on the parents until it finds the defined namespace
3792 * or return NULL otherwise.
3793 * @nameSpace can be NULL, this is a search for the default namespace.
3794 * We don't allow to cross entities boundaries. If you don't declare
3795 * the namespace within those you will be in troubles !!! A warning
3796 * is generated to cover this case.
3797 *
3798 * Returns the namespace pointer or NULL.
3799 */
3800xmlNsPtr
3801xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) {
3802 xmlNsPtr cur;
3803
3804 if (node == NULL) return(NULL);
3805 if ((nameSpace != NULL) &&
3806 (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) {
3807 if (doc->oldNs == NULL) {
3808 /*
3809 * Allocate a new Namespace and fill the fields.
3810 */
3811 doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
3812 if (doc->oldNs == NULL) {
3813 xmlGenericError(xmlGenericErrorContext,
3814 "xmlSearchNsByHref : malloc failed\n");
3815 return(NULL);
3816 }
3817 memset(doc->oldNs, 0, sizeof(xmlNs));
3818 doc->oldNs->type = XML_LOCAL_NAMESPACE;
3819
3820 doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE);
3821 doc->oldNs->prefix = xmlStrdup((const xmlChar *)"xml");
3822 }
3823 return(doc->oldNs);
3824 }
3825 while (node != NULL) {
3826 if ((node->type == XML_ENTITY_REF_NODE) ||
3827 (node->type == XML_ENTITY_NODE) ||
3828 (node->type == XML_ENTITY_DECL))
3829 return(NULL);
3830 if (node->type == XML_ELEMENT_NODE) {
3831 cur = node->nsDef;
3832 while (cur != NULL) {
3833 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
3834 (cur->href != NULL))
3835 return(cur);
3836 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
3837 (cur->href != NULL) &&
3838 (xmlStrEqual(cur->prefix, nameSpace)))
3839 return(cur);
3840 cur = cur->next;
3841 }
3842 }
3843 node = node->parent;
3844 }
3845 return(NULL);
3846}
3847
3848/**
3849 * xmlSearchNsByHref:
3850 * @doc: the document
3851 * @node: the current node
3852 * @href: the namespace value
3853 *
3854 * Search a Ns aliasing a given URI. Recurse on the parents until it finds
3855 * the defined namespace or return NULL otherwise.
3856 * Returns the namespace pointer or NULL.
3857 */
3858xmlNsPtr
3859xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar *href) {
3860 xmlNsPtr cur;
3861 xmlNodePtr orig = node;
3862
3863 if ((node == NULL) || (href == NULL)) return(NULL);
3864 if (xmlStrEqual(href, XML_XML_NAMESPACE)) {
3865 if (doc->oldNs == NULL) {
3866 /*
3867 * Allocate a new Namespace and fill the fields.
3868 */
3869 doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
3870 if (doc->oldNs == NULL) {
3871 xmlGenericError(xmlGenericErrorContext,
3872 "xmlSearchNsByHref : malloc failed\n");
3873 return(NULL);
3874 }
3875 memset(doc->oldNs, 0, sizeof(xmlNs));
3876 doc->oldNs->type = XML_LOCAL_NAMESPACE;
3877
3878 doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE);
3879 doc->oldNs->prefix = xmlStrdup((const xmlChar *)"xml");
3880 }
3881 return(doc->oldNs);
3882 }
3883 while (node != NULL) {
3884 cur = node->nsDef;
3885 while (cur != NULL) {
3886 if ((cur->href != NULL) && (href != NULL) &&
3887 (xmlStrEqual(cur->href, href))) {
3888 /*
3889 * Check that the prefix is not shadowed between orig and node
3890 */
3891 xmlNodePtr check = orig;
3892 xmlNsPtr tst;
3893
3894 while (check != node) {
3895 tst = check->nsDef;
3896 while (tst != NULL) {
3897 if ((tst->prefix == NULL) && (cur->prefix == NULL))
3898 goto shadowed;
3899 if ((tst->prefix != NULL) && (cur->prefix != NULL) &&
3900 (xmlStrEqual(tst->prefix, cur->prefix)))
3901 goto shadowed;
3902 tst = tst->next;
3903 }
3904 check = check->parent;
3905 }
3906 return(cur);
3907 }
3908shadowed:
3909 cur = cur->next;
3910 }
3911 node = node->parent;
3912 }
3913 return(NULL);
3914}
3915
3916/**
3917 * xmlNewReconciliedNs
3918 * @doc: the document
3919 * @tree: a node expected to hold the new namespace
3920 * @ns: the original namespace
3921 *
3922 * This function tries to locate a namespace definition in a tree
3923 * ancestors, or create a new namespace definition node similar to
3924 * @ns trying to reuse the same prefix. However if the given prefix is
3925 * null (default namespace) or reused within the subtree defined by
3926 * @tree or on one of its ancestors then a new prefix is generated.
3927 * Returns the (new) namespace definition or NULL in case of error
3928 */
3929xmlNsPtr
3930xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) {
3931 xmlNsPtr def;
3932 xmlChar prefix[50];
3933 int counter = 1;
3934
3935 if (tree == NULL) {
3936#ifdef DEBUG_TREE
3937 xmlGenericError(xmlGenericErrorContext,
3938 "xmlNewReconciliedNs : tree == NULL\n");
3939#endif
3940 return(NULL);
3941 }
3942 if (ns == NULL) {
3943#ifdef DEBUG_TREE
3944 xmlGenericError(xmlGenericErrorContext,
3945 "xmlNewReconciliedNs : ns == NULL\n");
3946#endif
3947 return(NULL);
3948 }
3949 /*
3950 * Search an existing namespace definition inherited.
3951 */
3952 def = xmlSearchNsByHref(doc, tree, ns->href);
3953 if (def != NULL)
3954 return(def);
3955
3956 /*
3957 * Find a close prefix which is not already in use.
3958 * Let's strip namespace prefixes longer than 20 chars !
3959 */
3960 sprintf((char *) prefix, "%.20s", ns->prefix);
3961 def = xmlSearchNs(doc, tree, prefix);
3962 while (def != NULL) {
3963 if (counter > 1000) return(NULL);
3964 sprintf((char *) prefix, "%.20s%d", ns->prefix, counter++);
3965 def = xmlSearchNs(doc, tree, prefix);
3966 }
3967
3968 /*
3969 * Ok, now we are ready to create a new one.
3970 */
3971 def = xmlNewNs(tree, ns->href, prefix);
3972 return(def);
3973}
3974
3975/**
3976 * xmlReconciliateNs
3977 * @doc: the document
3978 * @tree: a node defining the subtree to reconciliate
3979 *
3980 * This function checks that all the namespaces declared within the given
3981 * tree are properly declared. This is needed for example after Copy or Cut
3982 * and then paste operations. The subtree may still hold pointers to
3983 * namespace declarations outside the subtree or invalid/masked. As much
3984 * as possible the function try tu reuse the existing namespaces found in
3985 * the new environment. If not possible the new namespaces are redeclared
3986 * on @tree at the top of the given subtree.
3987 * Returns the number of namespace declarations created or -1 in case of error.
3988 */
3989int
3990xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) {
3991 xmlNsPtr *oldNs = NULL;
3992 xmlNsPtr *newNs = NULL;
3993 int sizeCache = 0;
3994 int nbCache = 0;
3995
3996 xmlNsPtr n;
3997 xmlNodePtr node = tree;
3998 xmlAttrPtr attr;
3999 int ret = 0, i;
4000
4001 while (node != NULL) {
4002 /*
4003 * Reconciliate the node namespace
4004 */
4005 if (node->ns != NULL) {
4006 /*
4007 * initialize the cache if needed
4008 */
4009 if (sizeCache == 0) {
4010 sizeCache = 10;
4011 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
4012 sizeof(xmlNsPtr));
4013 if (oldNs == NULL) {
4014 xmlGenericError(xmlGenericErrorContext,
4015 "xmlReconciliateNs : memory pbm\n");
4016 return(-1);
4017 }
4018 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
4019 sizeof(xmlNsPtr));
4020 if (newNs == NULL) {
4021 xmlGenericError(xmlGenericErrorContext,
4022 "xmlReconciliateNs : memory pbm\n");
4023 xmlFree(oldNs);
4024 return(-1);
4025 }
4026 }
4027 for (i = 0;i < nbCache;i++) {
4028 if (oldNs[i] == node->ns) {
4029 node->ns = newNs[i];
4030 break;
4031 }
4032 }
4033 if (i == nbCache) {
4034 /*
4035 * Ok we need to recreate a new namespace definition
4036 */
4037 n = xmlNewReconciliedNs(doc, tree, node->ns);
4038 if (n != NULL) { /* :-( what if else ??? */
4039 /*
4040 * check if we need to grow the cache buffers.
4041 */
4042 if (sizeCache <= nbCache) {
4043 sizeCache *= 2;
4044 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
4045 sizeof(xmlNsPtr));
4046 if (oldNs == NULL) {
4047 xmlGenericError(xmlGenericErrorContext,
4048 "xmlReconciliateNs : memory pbm\n");
4049 xmlFree(newNs);
4050 return(-1);
4051 }
4052 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
4053 sizeof(xmlNsPtr));
4054 if (newNs == NULL) {
4055 xmlGenericError(xmlGenericErrorContext,
4056 "xmlReconciliateNs : memory pbm\n");
4057 xmlFree(oldNs);
4058 return(-1);
4059 }
4060 }
4061 newNs[nbCache] = n;
4062 oldNs[nbCache++] = node->ns;
4063 node->ns = n;
4064 }
4065 }
4066 }
4067 /*
4068 * now check for namespace hold by attributes on the node.
4069 */
4070 attr = node->properties;
4071 while (attr != NULL) {
4072 if (attr->ns != NULL) {
4073 /*
4074 * initialize the cache if needed
4075 */
4076 if (sizeCache == 0) {
4077 sizeCache = 10;
4078 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
4079 sizeof(xmlNsPtr));
4080 if (oldNs == NULL) {
4081 xmlGenericError(xmlGenericErrorContext,
4082 "xmlReconciliateNs : memory pbm\n");
4083 return(-1);
4084 }
4085 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
4086 sizeof(xmlNsPtr));
4087 if (newNs == NULL) {
4088 xmlGenericError(xmlGenericErrorContext,
4089 "xmlReconciliateNs : memory pbm\n");
4090 xmlFree(oldNs);
4091 return(-1);
4092 }
4093 }
4094 for (i = 0;i < nbCache;i++) {
4095 if (oldNs[i] == attr->ns) {
4096 node->ns = newNs[i];
4097 break;
4098 }
4099 }
4100 if (i == nbCache) {
4101 /*
4102 * Ok we need to recreate a new namespace definition
4103 */
4104 n = xmlNewReconciliedNs(doc, tree, attr->ns);
4105 if (n != NULL) { /* :-( what if else ??? */
4106 /*
4107 * check if we need to grow the cache buffers.
4108 */
4109 if (sizeCache <= nbCache) {
4110 sizeCache *= 2;
4111 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
4112 sizeof(xmlNsPtr));
4113 if (oldNs == NULL) {
4114 xmlGenericError(xmlGenericErrorContext,
4115 "xmlReconciliateNs : memory pbm\n");
4116 xmlFree(newNs);
4117 return(-1);
4118 }
4119 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
4120 sizeof(xmlNsPtr));
4121 if (newNs == NULL) {
4122 xmlGenericError(xmlGenericErrorContext,
4123 "xmlReconciliateNs : memory pbm\n");
4124 xmlFree(oldNs);
4125 return(-1);
4126 }
4127 }
4128 newNs[nbCache] = n;
4129 oldNs[nbCache++] = attr->ns;
4130 attr->ns = n;
4131 }
4132 }
4133 }
4134 attr = attr->next;
4135 }
4136
4137 /*
4138 * Browse the full subtree, deep first
4139 */
4140 if (node->children != NULL) {
4141 /* deep first */
4142 node = node->children;
4143 } else if ((node != tree) && (node->next != NULL)) {
4144 /* then siblings */
4145 node = node->next;
4146 } else if (node != tree) {
4147 /* go up to parents->next if needed */
4148 while (node != tree) {
4149 if (node->parent != NULL)
4150 node = node->parent;
4151 if ((node != tree) && (node->next != NULL)) {
4152 node = node->next;
4153 break;
4154 }
4155 if (node->parent == NULL) {
4156 node = NULL;
4157 break;
4158 }
4159 }
4160 /* exit condition */
4161 if (node == tree)
4162 node = NULL;
4163 }
4164 }
4165 return(ret);
4166}
4167
4168/**
4169 * xmlHasProp:
4170 * @node: the node
4171 * @name: the attribute name
4172 *
4173 * Search an attribute associated to a node
4174 * This function also looks in DTD attribute declaration for #FIXED or
4175 * default declaration values unless DTD use has been turned off.
4176 *
4177 * Returns the attribute or the attribute declaration or NULL if
4178 * neither was found.
4179 */
4180xmlAttrPtr
4181xmlHasProp(xmlNodePtr node, const xmlChar *name) {
4182 xmlAttrPtr prop;
4183 xmlDocPtr doc;
4184
4185 if ((node == NULL) || (name == NULL)) return(NULL);
4186 /*
4187 * Check on the properties attached to the node
4188 */
4189 prop = node->properties;
4190 while (prop != NULL) {
4191 if (xmlStrEqual(prop->name, name)) {
4192 return(prop);
4193 }
4194 prop = prop->next;
4195 }
4196 if (!xmlCheckDTD) return(NULL);
4197
4198 /*
4199 * Check if there is a default declaration in the internal
4200 * or external subsets
4201 */
4202 doc = node->doc;
4203 if (doc != NULL) {
4204 xmlAttributePtr attrDecl;
4205 if (doc->intSubset != NULL) {
4206 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
4207 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4208 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
4209 if (attrDecl != NULL)
4210 return((xmlAttrPtr) attrDecl);
4211 }
4212 }
4213 return(NULL);
4214}
4215
4216/**
4217 * xmlGetProp:
4218 * @node: the node
4219 * @name: the attribute name
4220 *
4221 * Search and get the value of an attribute associated to a node
4222 * This does the entity substitution.
4223 * This function looks in DTD attribute declaration for #FIXED or
4224 * default declaration values unless DTD use has been turned off.
4225 *
4226 * Returns the attribute value or NULL if not found.
4227 * It's up to the caller to free the memory.
4228 */
4229xmlChar *
4230xmlGetProp(xmlNodePtr node, const xmlChar *name) {
4231 xmlAttrPtr prop;
4232 xmlDocPtr doc;
4233
4234 if ((node == NULL) || (name == NULL)) return(NULL);
4235 /*
4236 * Check on the properties attached to the node
4237 */
4238 prop = node->properties;
4239 while (prop != NULL) {
4240 if (xmlStrEqual(prop->name, name)) {
4241 xmlChar *ret;
4242
4243 ret = xmlNodeListGetString(node->doc, prop->children, 1);
4244 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
4245 return(ret);
4246 }
4247 prop = prop->next;
4248 }
4249 if (!xmlCheckDTD) return(NULL);
4250
4251 /*
4252 * Check if there is a default declaration in the internal
4253 * or external subsets
4254 */
4255 doc = node->doc;
4256 if (doc != NULL) {
4257 xmlAttributePtr attrDecl;
4258 if (doc->intSubset != NULL) {
4259 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
4260 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4261 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
4262 if (attrDecl != NULL)
4263 return(xmlStrdup(attrDecl->defaultValue));
4264 }
4265 }
4266 return(NULL);
4267}
4268
4269/**
4270 * xmlGetNsProp:
4271 * @node: the node
4272 * @name: the attribute name
4273 * @namespace: the URI of the namespace
4274 *
4275 * Search and get the value of an attribute associated to a node
4276 * This attribute has to be anchored in the namespace specified.
4277 * This does the entity substitution.
4278 * This function looks in DTD attribute declaration for #FIXED or
4279 * default declaration values unless DTD use has been turned off.
4280 *
4281 * Returns the attribute value or NULL if not found.
4282 * It's up to the caller to free the memory.
4283 */
4284xmlChar *
4285xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *namespace) {
4286 xmlAttrPtr prop;
4287 xmlDocPtr doc;
4288 xmlNsPtr ns;
4289
4290 if (node == NULL)
4291 return(NULL);
4292
4293 prop = node->properties;
4294 if (namespace == NULL)
4295 return(xmlGetProp(node, name));
4296 while (prop != NULL) {
4297 /*
4298 * One need to have
4299 * - same attribute names
4300 * - and the attribute carrying that namespace
4301 * or
4302 * no namespace on the attribute and the element carrying it
4303 */
4304 if ((xmlStrEqual(prop->name, name)) &&
4305 (((prop->ns == NULL) && (node->ns != NULL) &&
4306 (xmlStrEqual(node->ns->href, namespace))) ||
Daniel Veillard5792e162001-04-30 17:44:45 +00004307 ((prop->ns != NULL) &&
4308 (xmlStrEqual(prop->ns->href, namespace))))) {
Owen Taylor3473f882001-02-23 17:55:21 +00004309 xmlChar *ret;
4310
4311 ret = xmlNodeListGetString(node->doc, prop->children, 1);
4312 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
4313 return(ret);
4314 }
4315 prop = prop->next;
4316 }
4317 if (!xmlCheckDTD) return(NULL);
4318
4319 /*
4320 * Check if there is a default declaration in the internal
4321 * or external subsets
4322 */
4323 doc = node->doc;
4324 if (doc != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004325 if (doc->intSubset != NULL) {
Daniel Veillard5792e162001-04-30 17:44:45 +00004326 xmlAttributePtr attrDecl;
4327
Owen Taylor3473f882001-02-23 17:55:21 +00004328 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
4329 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4330 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
4331
4332 if ((attrDecl != NULL) && (attrDecl->prefix != NULL)) {
4333 /*
4334 * The DTD declaration only allows a prefix search
4335 */
4336 ns = xmlSearchNs(doc, node, attrDecl->prefix);
4337 if ((ns != NULL) && (xmlStrEqual(ns->href, namespace)))
4338 return(xmlStrdup(attrDecl->defaultValue));
4339 }
4340 }
4341 }
4342 return(NULL);
4343}
4344
4345/**
4346 * xmlSetProp:
4347 * @node: the node
4348 * @name: the attribute name
4349 * @value: the attribute value
4350 *
4351 * Set (or reset) an attribute carried by a node.
4352 * Returns the attribute pointer.
4353 */
4354xmlAttrPtr
4355xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
4356 xmlAttrPtr prop = node->properties;
4357 xmlDocPtr doc = NULL;
4358
4359 if ((node == NULL) || (name == NULL))
4360 return(NULL);
4361 doc = node->doc;
4362 while (prop != NULL) {
Daniel Veillard75bea542001-05-11 17:41:21 +00004363 if ((xmlStrEqual(prop->name, name)) &&
4364 (prop->ns == NULL)){
Owen Taylor3473f882001-02-23 17:55:21 +00004365 if (prop->children != NULL)
4366 xmlFreeNodeList(prop->children);
4367 prop->children = NULL;
4368 prop->last = NULL;
4369 if (value != NULL) {
4370 xmlChar *buffer;
4371 xmlNodePtr tmp;
4372
4373 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
4374 prop->children = xmlStringGetNodeList(node->doc, buffer);
4375 prop->last = NULL;
4376 prop->doc = doc;
4377 tmp = prop->children;
4378 while (tmp != NULL) {
4379 tmp->parent = (xmlNodePtr) prop;
4380 tmp->doc = doc;
4381 if (tmp->next == NULL)
4382 prop->last = tmp;
4383 tmp = tmp->next;
4384 }
4385 xmlFree(buffer);
Daniel Veillard75bea542001-05-11 17:41:21 +00004386 }
Owen Taylor3473f882001-02-23 17:55:21 +00004387 return(prop);
4388 }
4389 prop = prop->next;
4390 }
4391 prop = xmlNewProp(node, name, value);
4392 return(prop);
4393}
4394
4395/**
Daniel Veillard75bea542001-05-11 17:41:21 +00004396 * xmlUnsetProp:
4397 * @node: the node
4398 * @name: the attribute name
4399 *
4400 * Remove an attribute carried by a node.
4401 * Returns 0 if successful, -1 if not found
4402 */
4403int
4404xmlUnsetProp(xmlNodePtr node, const xmlChar *name) {
4405 xmlAttrPtr prop = node->properties, prev = NULL;;
4406
4407 if ((node == NULL) || (name == NULL))
4408 return(-1);
4409 while (prop != NULL) {
4410 if ((xmlStrEqual(prop->name, name)) &&
4411 (prop->ns == NULL)) {
4412 if (prev == NULL)
4413 node->properties = prop->next;
4414 else
4415 prev->next = prop->next;
4416 xmlFreeProp(prop);
4417 return(0);
4418 }
4419 prev = prop;
4420 prop = prop->next;
4421 }
4422 return(-1);
4423}
4424
4425/**
Owen Taylor3473f882001-02-23 17:55:21 +00004426 * xmlSetNsProp:
4427 * @node: the node
4428 * @ns: the namespace definition
4429 * @name: the attribute name
4430 * @value: the attribute value
4431 *
4432 * Set (or reset) an attribute carried by a node.
4433 * The ns structure must be in scope, this is not checked.
4434 *
4435 * Returns the attribute pointer.
4436 */
4437xmlAttrPtr
4438xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
4439 const xmlChar *value) {
4440 xmlAttrPtr prop;
4441
4442 if ((node == NULL) || (name == NULL))
4443 return(NULL);
4444
4445 if (ns == NULL)
4446 return(xmlSetProp(node, name, value));
4447 if (ns->href == NULL)
4448 return(NULL);
4449 prop = node->properties;
4450
4451 while (prop != NULL) {
4452 /*
4453 * One need to have
4454 * - same attribute names
4455 * - and the attribute carrying that namespace
4456 * or
4457 * no namespace on the attribute and the element carrying it
4458 */
4459 if ((xmlStrEqual(prop->name, name)) &&
4460 (((prop->ns == NULL) && (node->ns != NULL) &&
4461 (xmlStrEqual(node->ns->href, ns->href))) ||
4462 ((prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))))) {
4463 if (prop->children != NULL)
4464 xmlFreeNodeList(prop->children);
4465 prop->children = NULL;
4466 prop->last = NULL;
4467 prop->ns = ns;
4468 if (value != NULL) {
4469 xmlChar *buffer;
4470 xmlNodePtr tmp;
4471
4472 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
4473 prop->children = xmlStringGetNodeList(node->doc, buffer);
4474 prop->last = NULL;
4475 tmp = prop->children;
4476 while (tmp != NULL) {
4477 tmp->parent = (xmlNodePtr) prop;
4478 if (tmp->next == NULL)
4479 prop->last = tmp;
4480 tmp = tmp->next;
4481 }
4482 xmlFree(buffer);
4483 }
4484 return(prop);
4485 }
4486 prop = prop->next;
4487 }
4488 prop = xmlNewNsProp(node, ns, name, value);
4489 return(prop);
4490}
4491
4492/**
Daniel Veillard75bea542001-05-11 17:41:21 +00004493 * xmlUnsetNsProp:
4494 * @node: the node
4495 * @ns: the namespace definition
4496 * @name: the attribute name
4497 *
4498 * Remove an attribute carried by a node.
4499 * Returns 0 if successful, -1 if not found
4500 */
4501int
4502xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) {
4503 xmlAttrPtr prop = node->properties, prev = NULL;;
4504
4505 if ((node == NULL) || (name == NULL))
4506 return(-1);
4507 if (ns == NULL)
4508 return(xmlUnsetProp(node, name));
4509 if (ns->href == NULL)
4510 return(-1);
4511 while (prop != NULL) {
4512 if ((xmlStrEqual(prop->name, name)) &&
4513 (((prop->ns == NULL) && (node->ns != NULL) &&
4514 (xmlStrEqual(node->ns->href, ns->href))) ||
4515 ((prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))))) {
4516 if (prev == NULL)
4517 node->properties = prop->next;
4518 else
4519 prev->next = prop->next;
4520 xmlFreeProp(prop);
4521 return(0);
4522 }
4523 prev = prop;
4524 prop = prop->next;
4525 }
4526 return(-1);
4527}
4528
4529/**
Owen Taylor3473f882001-02-23 17:55:21 +00004530 * xmlNodeIsText:
4531 * @node: the node
4532 *
4533 * Is this node a Text node ?
4534 * Returns 1 yes, 0 no
4535 */
4536int
4537xmlNodeIsText(xmlNodePtr node) {
4538 if (node == NULL) return(0);
4539
4540 if (node->type == XML_TEXT_NODE) return(1);
4541 return(0);
4542}
4543
4544/**
4545 * xmlIsBlankNode:
4546 * @node: the node
4547 *
4548 * Checks whether this node is an empty or whitespace only
4549 * (and possibly ignorable) text-node.
4550 *
4551 * Returns 1 yes, 0 no
4552 */
4553int
4554xmlIsBlankNode(xmlNodePtr node) {
4555 const xmlChar *cur;
4556 if (node == NULL) return(0);
4557
4558 if (node->type != XML_TEXT_NODE) return(0);
4559 if (node->content == NULL) return(1);
4560#ifndef XML_USE_BUFFER_CONTENT
4561 cur = node->content;
4562#else
4563 cur = xmlBufferContent(node->content);
4564#endif
4565 while (*cur != 0) {
4566 if (!IS_BLANK(*cur)) return(0);
4567 cur++;
4568 }
4569
4570 return(1);
4571}
4572
4573/**
4574 * xmlTextConcat:
4575 * @node: the node
4576 * @content: the content
4577 * @len: @content lenght
4578 *
4579 * Concat the given string at the end of the existing node content
4580 */
4581
4582void
4583xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) {
4584 if (node == NULL) return;
4585
4586 if ((node->type != XML_TEXT_NODE) &&
4587 (node->type != XML_CDATA_SECTION_NODE)) {
4588#ifdef DEBUG_TREE
4589 xmlGenericError(xmlGenericErrorContext,
4590 "xmlTextConcat: node is not text nor cdata\n");
4591#endif
4592 return;
4593 }
4594#ifndef XML_USE_BUFFER_CONTENT
4595 node->content = xmlStrncat(node->content, content, len);
4596#else
4597 xmlBufferAdd(node->content, content, len);
4598#endif
4599}
4600
4601/************************************************************************
4602 * *
4603 * Output : to a FILE or in memory *
4604 * *
4605 ************************************************************************/
4606
4607#define BASE_BUFFER_SIZE 4000
4608
Daniel Veillarde356c282001-03-10 12:32:04 +00004609int xmlDefaultBufferSize = BASE_BUFFER_SIZE;
4610
Owen Taylor3473f882001-02-23 17:55:21 +00004611/**
4612 * xmlBufferCreate:
4613 *
4614 * routine to create an XML buffer.
4615 * returns the new structure.
4616 */
4617xmlBufferPtr
4618xmlBufferCreate(void) {
4619 xmlBufferPtr ret;
4620
4621 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
4622 if (ret == NULL) {
4623 xmlGenericError(xmlGenericErrorContext,
4624 "xmlBufferCreate : out of memory!\n");
4625 return(NULL);
4626 }
4627 ret->use = 0;
Daniel Veillarde356c282001-03-10 12:32:04 +00004628 ret->size = xmlDefaultBufferSize;
Owen Taylor3473f882001-02-23 17:55:21 +00004629 ret->alloc = xmlBufferAllocScheme;
4630 ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar));
4631 if (ret->content == NULL) {
4632 xmlGenericError(xmlGenericErrorContext,
4633 "xmlBufferCreate : out of memory!\n");
4634 xmlFree(ret);
4635 return(NULL);
4636 }
4637 ret->content[0] = 0;
4638 return(ret);
4639}
4640
4641/**
4642 * xmlBufferCreateSize:
4643 * @size: initial size of buffer
4644 *
4645 * routine to create an XML buffer.
4646 * returns the new structure.
4647 */
4648xmlBufferPtr
4649xmlBufferCreateSize(size_t size) {
4650 xmlBufferPtr ret;
4651
4652 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
4653 if (ret == NULL) {
4654 xmlGenericError(xmlGenericErrorContext,
4655 "xmlBufferCreate : out of memory!\n");
4656 return(NULL);
4657 }
4658 ret->use = 0;
4659 ret->alloc = xmlBufferAllocScheme;
4660 ret->size = (size ? size+2 : 0); /* +1 for ending null */
4661 if (ret->size){
4662 ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar));
4663 if (ret->content == NULL) {
4664 xmlGenericError(xmlGenericErrorContext,
4665 "xmlBufferCreate : out of memory!\n");
4666 xmlFree(ret);
4667 return(NULL);
4668 }
4669 ret->content[0] = 0;
4670 } else
4671 ret->content = NULL;
4672 return(ret);
4673}
4674
4675/**
4676 * xmlBufferSetAllocationScheme:
4677 * @buf: the buffer to free
4678 * @scheme: allocation scheme to use
4679 *
4680 * Sets the allocation scheme for this buffer
4681 */
4682void
4683xmlBufferSetAllocationScheme(xmlBufferPtr buf,
4684 xmlBufferAllocationScheme scheme) {
4685 if (buf == NULL) {
4686#ifdef DEBUG_BUFFER
4687 xmlGenericError(xmlGenericErrorContext,
4688 "xmlBufferSetAllocationScheme: buf == NULL\n");
4689#endif
4690 return;
4691 }
4692
4693 buf->alloc = scheme;
4694}
4695
4696/**
4697 * xmlBufferFree:
4698 * @buf: the buffer to free
4699 *
4700 * Frees an XML buffer.
4701 */
4702void
4703xmlBufferFree(xmlBufferPtr buf) {
4704 if (buf == NULL) {
4705#ifdef DEBUG_BUFFER
4706 xmlGenericError(xmlGenericErrorContext,
4707 "xmlBufferFree: buf == NULL\n");
4708#endif
4709 return;
4710 }
4711 if (buf->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004712 xmlFree(buf->content);
4713 }
Owen Taylor3473f882001-02-23 17:55:21 +00004714 xmlFree(buf);
4715}
4716
4717/**
4718 * xmlBufferEmpty:
4719 * @buf: the buffer
4720 *
4721 * empty a buffer.
4722 */
4723void
4724xmlBufferEmpty(xmlBufferPtr buf) {
4725 if (buf->content == NULL) return;
4726 buf->use = 0;
Daniel Veillard92ad2102001-03-27 12:47:33 +00004727 memset(buf->content, 0, buf->size);
Owen Taylor3473f882001-02-23 17:55:21 +00004728}
4729
4730/**
4731 * xmlBufferShrink:
4732 * @buf: the buffer to dump
4733 * @len: the number of xmlChar to remove
4734 *
4735 * Remove the beginning of an XML buffer.
4736 *
4737 * Returns the number of xmlChar removed, or -1 in case of failure.
4738 */
4739int
4740xmlBufferShrink(xmlBufferPtr buf, unsigned int len) {
4741 if (len == 0) return(0);
4742 if (len > buf->use) return(-1);
4743
4744 buf->use -= len;
4745 memmove(buf->content, &buf->content[len], buf->use * sizeof(xmlChar));
4746
4747 buf->content[buf->use] = 0;
4748 return(len);
4749}
4750
4751/**
4752 * xmlBufferGrow:
4753 * @buf: the buffer
4754 * @len: the minimum free size to allocate
4755 *
4756 * Grow the available space of an XML buffer.
4757 *
4758 * Returns the new available space or -1 in case of error
4759 */
4760int
4761xmlBufferGrow(xmlBufferPtr buf, unsigned int len) {
4762 int size;
4763 xmlChar *newbuf;
4764
4765 if (len + buf->use < buf->size) return(0);
4766
4767 size = buf->use + len + 100;
4768
4769 newbuf = (xmlChar *) xmlRealloc(buf->content, size);
4770 if (newbuf == NULL) return(-1);
4771 buf->content = newbuf;
4772 buf->size = size;
4773 return(buf->size - buf->use);
4774}
4775
4776/**
4777 * xmlBufferDump:
4778 * @file: the file output
4779 * @buf: the buffer to dump
4780 *
4781 * Dumps an XML buffer to a FILE *.
4782 * Returns the number of xmlChar written
4783 */
4784int
4785xmlBufferDump(FILE *file, xmlBufferPtr buf) {
4786 int ret;
4787
4788 if (buf == NULL) {
4789#ifdef DEBUG_BUFFER
4790 xmlGenericError(xmlGenericErrorContext,
4791 "xmlBufferDump: buf == NULL\n");
4792#endif
4793 return(0);
4794 }
4795 if (buf->content == NULL) {
4796#ifdef DEBUG_BUFFER
4797 xmlGenericError(xmlGenericErrorContext,
4798 "xmlBufferDump: buf->content == NULL\n");
4799#endif
4800 return(0);
4801 }
4802 if (file == NULL) file = stdout;
4803 ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
4804 return(ret);
4805}
4806
4807/**
4808 * xmlBufferContent:
4809 * @buf: the buffer
4810 *
4811 * Returns the internal content
4812 */
4813
4814const xmlChar*
4815xmlBufferContent(const xmlBufferPtr buf)
4816{
4817 if(!buf)
4818 return NULL;
4819
4820 return buf->content;
4821}
4822
4823/**
4824 * xmlBufferLength:
4825 * @buf: the buffer
4826 *
4827 * Returns the length of data in the internal content
4828 */
4829
4830int
4831xmlBufferLength(const xmlBufferPtr buf)
4832{
4833 if(!buf)
4834 return 0;
4835
4836 return buf->use;
4837}
4838
4839/**
4840 * xmlBufferResize:
4841 * @buf: the buffer to resize
4842 * @size: the desired size
4843 *
4844 * Resize a buffer to accomodate minimum size of @size.
4845 *
4846 * Returns 0 in case of problems, 1 otherwise
4847 */
4848int
4849xmlBufferResize(xmlBufferPtr buf, unsigned int size)
4850{
4851 unsigned int newSize;
4852 xmlChar* rebuf = NULL;
4853
4854 /*take care of empty case*/
4855 newSize = (buf->size ? buf->size*2 : size);
4856
4857 /* Don't resize if we don't have to */
4858 if (size < buf->size)
4859 return 1;
4860
4861 /* figure out new size */
4862 switch (buf->alloc){
4863 case XML_BUFFER_ALLOC_DOUBLEIT:
4864 while (size > newSize) newSize *= 2;
4865 break;
4866 case XML_BUFFER_ALLOC_EXACT:
4867 newSize = size+10;
4868 break;
4869 default:
4870 newSize = size+10;
4871 break;
4872 }
4873
4874 if (buf->content == NULL)
4875 rebuf = (xmlChar *) xmlMalloc(newSize * sizeof(xmlChar));
4876 else
4877 rebuf = (xmlChar *) xmlRealloc(buf->content,
4878 newSize * sizeof(xmlChar));
4879 if (rebuf == NULL) {
4880 xmlGenericError(xmlGenericErrorContext,
4881 "xmlBufferAdd : out of memory!\n");
4882 return 0;
4883 }
4884 buf->content = rebuf;
4885 buf->size = newSize;
4886
4887 return 1;
4888}
4889
4890/**
4891 * xmlBufferAdd:
4892 * @buf: the buffer to dump
4893 * @str: the xmlChar string
4894 * @len: the number of xmlChar to add
4895 *
4896 * Add a string range to an XML buffer. if len == -1, the lenght of
4897 * str is recomputed.
4898 */
4899void
4900xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) {
4901 unsigned int needSize;
4902
4903 if (str == NULL) {
4904#ifdef DEBUG_BUFFER
4905 xmlGenericError(xmlGenericErrorContext,
4906 "xmlBufferAdd: str == NULL\n");
4907#endif
4908 return;
4909 }
4910 if (len < -1) {
4911#ifdef DEBUG_BUFFER
4912 xmlGenericError(xmlGenericErrorContext,
4913 "xmlBufferAdd: len < 0\n");
4914#endif
4915 return;
4916 }
4917 if (len == 0) return;
4918
4919 if (len < 0)
4920 len = xmlStrlen(str);
4921
4922 if (len <= 0) return;
4923
4924 needSize = buf->use + len + 2;
4925 if (needSize > buf->size){
4926 if (!xmlBufferResize(buf, needSize)){
4927 xmlGenericError(xmlGenericErrorContext,
4928 "xmlBufferAdd : out of memory!\n");
4929 return;
4930 }
4931 }
4932
4933 memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
4934 buf->use += len;
4935 buf->content[buf->use] = 0;
4936}
4937
4938/**
4939 * xmlBufferAddHead:
4940 * @buf: the buffer
4941 * @str: the xmlChar string
4942 * @len: the number of xmlChar to add
4943 *
4944 * Add a string range to the beginning of an XML buffer.
4945 * if len == -1, the lenght of @str is recomputed.
4946 */
4947void
4948xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) {
4949 unsigned int needSize;
4950
4951 if (str == NULL) {
4952#ifdef DEBUG_BUFFER
4953 xmlGenericError(xmlGenericErrorContext,
4954 "xmlBufferAdd: str == NULL\n");
4955#endif
4956 return;
4957 }
4958 if (len < -1) {
4959#ifdef DEBUG_BUFFER
4960 xmlGenericError(xmlGenericErrorContext,
4961 "xmlBufferAdd: len < 0\n");
4962#endif
4963 return;
4964 }
4965 if (len == 0) return;
4966
4967 if (len < 0)
4968 len = xmlStrlen(str);
4969
4970 if (len <= 0) return;
4971
4972 needSize = buf->use + len + 2;
4973 if (needSize > buf->size){
4974 if (!xmlBufferResize(buf, needSize)){
4975 xmlGenericError(xmlGenericErrorContext,
4976 "xmlBufferAddHead : out of memory!\n");
4977 return;
4978 }
4979 }
4980
4981 memmove(&buf->content[len], &buf->content[0], buf->use * sizeof(xmlChar));
4982 memmove(&buf->content[0], str, len * sizeof(xmlChar));
4983 buf->use += len;
4984 buf->content[buf->use] = 0;
4985}
4986
4987/**
4988 * xmlBufferCat:
4989 * @buf: the buffer to dump
4990 * @str: the xmlChar string
4991 *
4992 * Append a zero terminated string to an XML buffer.
4993 */
4994void
4995xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) {
4996 if (str != NULL)
4997 xmlBufferAdd(buf, str, -1);
4998}
4999
5000/**
5001 * xmlBufferCCat:
5002 * @buf: the buffer to dump
5003 * @str: the C char string
5004 *
5005 * Append a zero terminated C string to an XML buffer.
5006 */
5007void
5008xmlBufferCCat(xmlBufferPtr buf, const char *str) {
5009 const char *cur;
5010
5011 if (str == NULL) {
5012#ifdef DEBUG_BUFFER
5013 xmlGenericError(xmlGenericErrorContext,
5014 "xmlBufferAdd: str == NULL\n");
5015#endif
5016 return;
5017 }
5018 for (cur = str;*cur != 0;cur++) {
5019 if (buf->use + 10 >= buf->size) {
5020 if (!xmlBufferResize(buf, buf->use+10)){
5021 xmlGenericError(xmlGenericErrorContext,
5022 "xmlBufferCCat : out of memory!\n");
5023 return;
5024 }
5025 }
5026 buf->content[buf->use++] = *cur;
5027 }
5028 buf->content[buf->use] = 0;
5029}
5030
5031/**
5032 * xmlBufferWriteCHAR:
5033 * @buf: the XML buffer
5034 * @string: the string to add
5035 *
5036 * routine which manages and grows an output buffer. This one adds
5037 * xmlChars at the end of the buffer.
5038 */
5039void
5040#ifdef VMS
5041xmlBufferWriteXmlCHAR
5042#else
5043xmlBufferWriteCHAR
5044#endif
5045(xmlBufferPtr buf, const xmlChar *string) {
5046 xmlBufferCat(buf, string);
5047}
5048
5049/**
5050 * xmlBufferWriteChar:
5051 * @buf: the XML buffer output
5052 * @string: the string to add
5053 *
5054 * routine which manage and grows an output buffer. This one add
5055 * C chars at the end of the array.
5056 */
5057void
5058xmlBufferWriteChar(xmlBufferPtr buf, const char *string) {
5059 xmlBufferCCat(buf, string);
5060}
5061
5062
5063/**
5064 * xmlBufferWriteQuotedString:
5065 * @buf: the XML buffer output
5066 * @string: the string to add
5067 *
5068 * routine which manage and grows an output buffer. This one writes
5069 * a quoted or double quoted xmlChar string, checking first if it holds
5070 * quote or double-quotes internally
5071 */
5072void
5073xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) {
5074 if (xmlStrchr(string, '"')) {
5075 if (xmlStrchr(string, '\'')) {
5076#ifdef DEBUG_BUFFER
5077 xmlGenericError(xmlGenericErrorContext,
5078 "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n");
5079#endif
5080 }
5081 xmlBufferCCat(buf, "'");
5082 xmlBufferCat(buf, string);
5083 xmlBufferCCat(buf, "'");
5084 } else {
5085 xmlBufferCCat(buf, "\"");
5086 xmlBufferCat(buf, string);
5087 xmlBufferCCat(buf, "\"");
5088 }
5089}
5090
5091
5092/************************************************************************
5093 * *
5094 * Dumping XML tree content to a simple buffer *
5095 * *
5096 ************************************************************************/
5097
5098void
5099xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
5100 int format);
5101static void
5102xmlNodeListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
5103 int format);
5104void
5105htmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur);
5106
5107/**
5108 * xmlNsDump:
5109 * @buf: the XML buffer output
5110 * @cur: a namespace
5111 *
5112 * Dump a local Namespace definition.
5113 * Should be called in the context of attributes dumps.
5114 */
5115static void
5116xmlNsDump(xmlBufferPtr buf, xmlNsPtr cur) {
5117 if (cur == NULL) {
5118#ifdef DEBUG_TREE
5119 xmlGenericError(xmlGenericErrorContext,
5120 "xmlNsDump : Ns == NULL\n");
5121#endif
5122 return;
5123 }
5124 if (cur->type == XML_LOCAL_NAMESPACE) {
5125 /* Within the context of an element attributes */
5126 if (cur->prefix != NULL) {
5127 xmlBufferWriteChar(buf, " xmlns:");
5128 xmlBufferWriteCHAR(buf, cur->prefix);
5129 } else
5130 xmlBufferWriteChar(buf, " xmlns");
5131 xmlBufferWriteChar(buf, "=");
5132 xmlBufferWriteQuotedString(buf, cur->href);
5133 }
5134}
5135
5136/**
5137 * xmlNsListDump:
5138 * @buf: the XML buffer output
5139 * @cur: the first namespace
5140 *
5141 * Dump a list of local Namespace definitions.
5142 * Should be called in the context of attributes dumps.
5143 */
5144static void
5145xmlNsListDump(xmlBufferPtr buf, xmlNsPtr cur) {
5146 while (cur != NULL) {
5147 xmlNsDump(buf, cur);
5148 cur = cur->next;
5149 }
5150}
5151
5152/**
5153 * xmlDtdDump:
5154 * @buf: the XML buffer output
5155 * @doc: the document
5156 *
5157 * Dump the XML document DTD, if any.
5158 */
5159static void
5160xmlDtdDump(xmlBufferPtr buf, xmlDtdPtr dtd) {
5161 if (dtd == NULL) {
5162#ifdef DEBUG_TREE
5163 xmlGenericError(xmlGenericErrorContext,
5164 "xmlDtdDump : no internal subset\n");
5165#endif
5166 return;
5167 }
5168 xmlBufferWriteChar(buf, "<!DOCTYPE ");
5169 xmlBufferWriteCHAR(buf, dtd->name);
5170 if (dtd->ExternalID != NULL) {
5171 xmlBufferWriteChar(buf, " PUBLIC ");
5172 xmlBufferWriteQuotedString(buf, dtd->ExternalID);
5173 xmlBufferWriteChar(buf, " ");
5174 xmlBufferWriteQuotedString(buf, dtd->SystemID);
5175 } else if (dtd->SystemID != NULL) {
5176 xmlBufferWriteChar(buf, " SYSTEM ");
5177 xmlBufferWriteQuotedString(buf, dtd->SystemID);
5178 }
5179 if ((dtd->entities == NULL) && (dtd->elements == NULL) &&
5180 (dtd->attributes == NULL) && (dtd->notations == NULL)) {
5181 xmlBufferWriteChar(buf, ">");
5182 return;
5183 }
5184 xmlBufferWriteChar(buf, " [\n");
5185 xmlNodeListDump(buf, dtd->doc, dtd->children, -1, 0);
5186#if 0
5187 if (dtd->entities != NULL)
5188 xmlDumpEntitiesTable(buf, (xmlEntitiesTablePtr) dtd->entities);
5189 if (dtd->notations != NULL)
5190 xmlDumpNotationTable(buf, (xmlNotationTablePtr) dtd->notations);
5191 if (dtd->elements != NULL)
5192 xmlDumpElementTable(buf, (xmlElementTablePtr) dtd->elements);
5193 if (dtd->attributes != NULL)
5194 xmlDumpAttributeTable(buf, (xmlAttributeTablePtr) dtd->attributes);
5195#endif
5196 xmlBufferWriteChar(buf, "]>");
5197}
5198
5199/**
5200 * xmlAttrDump:
5201 * @buf: the XML buffer output
5202 * @doc: the document
5203 * @cur: the attribute pointer
5204 *
5205 * Dump an XML attribute
5206 */
5207static void
5208xmlAttrDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) {
5209 xmlChar *value;
5210
5211 if (cur == NULL) {
5212#ifdef DEBUG_TREE
5213 xmlGenericError(xmlGenericErrorContext,
5214 "xmlAttrDump : property == NULL\n");
5215#endif
5216 return;
5217 }
5218 xmlBufferWriteChar(buf, " ");
5219 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
5220 xmlBufferWriteCHAR(buf, cur->ns->prefix);
5221 xmlBufferWriteChar(buf, ":");
5222 }
5223 xmlBufferWriteCHAR(buf, cur->name);
5224 value = xmlNodeListGetString(doc, cur->children, 0);
5225 if (value != NULL) {
5226 xmlBufferWriteChar(buf, "=");
5227 xmlBufferWriteQuotedString(buf, value);
5228 xmlFree(value);
5229 } else {
5230 xmlBufferWriteChar(buf, "=\"\"");
5231 }
5232}
5233
5234/**
5235 * xmlAttrListDump:
5236 * @buf: the XML buffer output
5237 * @doc: the document
5238 * @cur: the first attribute pointer
5239 *
5240 * Dump a list of XML attributes
5241 */
5242static void
5243xmlAttrListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) {
5244 if (cur == NULL) {
5245#ifdef DEBUG_TREE
5246 xmlGenericError(xmlGenericErrorContext,
5247 "xmlAttrListDump : property == NULL\n");
5248#endif
5249 return;
5250 }
5251 while (cur != NULL) {
5252 xmlAttrDump(buf, doc, cur);
5253 cur = cur->next;
5254 }
5255}
5256
5257
5258
5259/**
5260 * xmlNodeListDump:
5261 * @buf: the XML buffer output
5262 * @doc: the document
5263 * @cur: the first node
5264 * @level: the imbrication level for indenting
5265 * @format: is formatting allowed
5266 *
5267 * Dump an XML node list, recursive behaviour,children are printed too.
5268 */
5269static void
5270xmlNodeListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
5271 int format) {
5272 int i;
5273
5274 if (cur == NULL) {
5275#ifdef DEBUG_TREE
5276 xmlGenericError(xmlGenericErrorContext,
5277 "xmlNodeListDump : node == NULL\n");
5278#endif
5279 return;
5280 }
5281 while (cur != NULL) {
5282 if ((format) && (xmlIndentTreeOutput) &&
5283 (cur->type == XML_ELEMENT_NODE))
5284 for (i = 0;i < level;i++)
5285 xmlBufferWriteChar(buf, " ");
5286 xmlNodeDump(buf, doc, cur, level, format);
5287 if (format) {
5288 xmlBufferWriteChar(buf, "\n");
5289 }
5290 cur = cur->next;
5291 }
5292}
5293
5294/**
5295 * xmlNodeDump:
5296 * @buf: the XML buffer output
5297 * @doc: the document
5298 * @cur: the current node
5299 * @level: the imbrication level for indenting
5300 * @format: is formatting allowed
5301 *
5302 * Dump an XML node, recursive behaviour,children are printed too.
5303 */
5304void
5305xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
5306 int format) {
5307 int i;
5308 xmlNodePtr tmp;
5309
5310 if (cur == NULL) {
5311#ifdef DEBUG_TREE
5312 xmlGenericError(xmlGenericErrorContext,
5313 "xmlNodeDump : node == NULL\n");
5314#endif
5315 return;
5316 }
5317 if (cur->type == XML_XINCLUDE_START)
5318 return;
5319 if (cur->type == XML_XINCLUDE_END)
5320 return;
5321 if (cur->type == XML_DTD_NODE) {
5322 xmlDtdDump(buf, (xmlDtdPtr) cur);
5323 return;
5324 }
5325 if (cur->type == XML_ELEMENT_DECL) {
5326 xmlDumpElementDecl(buf, (xmlElementPtr) cur);
5327 return;
5328 }
5329 if (cur->type == XML_ATTRIBUTE_DECL) {
5330 xmlDumpAttributeDecl(buf, (xmlAttributePtr) cur);
5331 return;
5332 }
5333 if (cur->type == XML_ENTITY_DECL) {
5334 xmlDumpEntityDecl(buf, (xmlEntityPtr) cur);
5335 return;
5336 }
5337 if (cur->type == XML_TEXT_NODE) {
5338 if (cur->content != NULL) {
5339 if ((cur->name == xmlStringText) ||
5340 (cur->name != xmlStringTextNoenc)) {
5341 xmlChar *buffer;
5342
5343#ifndef XML_USE_BUFFER_CONTENT
5344 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
5345#else
5346 buffer = xmlEncodeEntitiesReentrant(doc,
5347 xmlBufferContent(cur->content));
5348#endif
5349 if (buffer != NULL) {
5350 xmlBufferWriteCHAR(buf, buffer);
5351 xmlFree(buffer);
5352 }
5353 } else {
5354 /*
5355 * Disable escaping, needed for XSLT
5356 */
5357#ifndef XML_USE_BUFFER_CONTENT
5358 xmlBufferWriteCHAR(buf, cur->content);
5359#else
5360 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
5361#endif
5362 }
5363 }
5364 return;
5365 }
5366 if (cur->type == XML_PI_NODE) {
5367 if (cur->content != NULL) {
5368 xmlBufferWriteChar(buf, "<?");
5369 xmlBufferWriteCHAR(buf, cur->name);
5370 if (cur->content != NULL) {
5371 xmlBufferWriteChar(buf, " ");
5372#ifndef XML_USE_BUFFER_CONTENT
5373 xmlBufferWriteCHAR(buf, cur->content);
5374#else
5375 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
5376#endif
5377 }
5378 xmlBufferWriteChar(buf, "?>");
5379 } else {
5380 xmlBufferWriteChar(buf, "<?");
5381 xmlBufferWriteCHAR(buf, cur->name);
5382 xmlBufferWriteChar(buf, "?>");
5383 }
5384 return;
5385 }
5386 if (cur->type == XML_COMMENT_NODE) {
5387 if (cur->content != NULL) {
5388 xmlBufferWriteChar(buf, "<!--");
5389#ifndef XML_USE_BUFFER_CONTENT
5390 xmlBufferWriteCHAR(buf, cur->content);
5391#else
5392 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
5393#endif
5394 xmlBufferWriteChar(buf, "-->");
5395 }
5396 return;
5397 }
5398 if (cur->type == XML_ENTITY_REF_NODE) {
5399 xmlBufferWriteChar(buf, "&");
5400 xmlBufferWriteCHAR(buf, cur->name);
5401 xmlBufferWriteChar(buf, ";");
5402 return;
5403 }
5404 if (cur->type == XML_CDATA_SECTION_NODE) {
5405 xmlBufferWriteChar(buf, "<![CDATA[");
5406 if (cur->content != NULL)
5407#ifndef XML_USE_BUFFER_CONTENT
5408 xmlBufferWriteCHAR(buf, cur->content);
5409#else
5410 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
5411#endif
5412 xmlBufferWriteChar(buf, "]]>");
5413 return;
5414 }
5415
5416 if (format == 1) {
5417 tmp = cur->children;
5418 while (tmp != NULL) {
5419 if ((tmp->type == XML_TEXT_NODE) ||
5420 (tmp->type == XML_ENTITY_REF_NODE)) {
5421 format = 0;
5422 break;
5423 }
5424 tmp = tmp->next;
5425 }
5426 }
5427 xmlBufferWriteChar(buf, "<");
5428 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
5429 xmlBufferWriteCHAR(buf, cur->ns->prefix);
5430 xmlBufferWriteChar(buf, ":");
5431 }
5432
5433 xmlBufferWriteCHAR(buf, cur->name);
5434 if (cur->nsDef)
5435 xmlNsListDump(buf, cur->nsDef);
5436 if (cur->properties != NULL)
5437 xmlAttrListDump(buf, doc, cur->properties);
5438
5439 if ((cur->content == NULL) && (cur->children == NULL) &&
5440 (!xmlSaveNoEmptyTags)) {
5441 xmlBufferWriteChar(buf, "/>");
5442 return;
5443 }
5444 xmlBufferWriteChar(buf, ">");
5445 if (cur->content != NULL) {
5446 xmlChar *buffer;
5447
5448#ifndef XML_USE_BUFFER_CONTENT
5449 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
5450#else
5451 buffer = xmlEncodeEntitiesReentrant(doc,
5452 xmlBufferContent(cur->content));
5453#endif
5454 if (buffer != NULL) {
5455 xmlBufferWriteCHAR(buf, buffer);
5456 xmlFree(buffer);
5457 }
5458 }
5459 if (cur->children != NULL) {
5460 if (format) xmlBufferWriteChar(buf, "\n");
5461 xmlNodeListDump(buf, doc, cur->children,
5462 (level >= 0?level+1:-1), format);
5463 if ((xmlIndentTreeOutput) && (format))
5464 for (i = 0;i < level;i++)
5465 xmlBufferWriteChar(buf, " ");
5466 }
5467 xmlBufferWriteChar(buf, "</");
5468 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
5469 xmlBufferWriteCHAR(buf, cur->ns->prefix);
5470 xmlBufferWriteChar(buf, ":");
5471 }
5472
5473 xmlBufferWriteCHAR(buf, cur->name);
5474 xmlBufferWriteChar(buf, ">");
5475}
5476
5477/**
5478 * xmlElemDump:
5479 * @f: the FILE * for the output
5480 * @doc: the document
5481 * @cur: the current node
5482 *
5483 * Dump an XML/HTML node, recursive behaviour,children are printed too.
5484 */
5485void
5486xmlElemDump(FILE *f, xmlDocPtr doc, xmlNodePtr cur) {
5487 xmlBufferPtr buf;
5488
5489 if (cur == NULL) {
5490#ifdef DEBUG_TREE
5491 xmlGenericError(xmlGenericErrorContext,
5492 "xmlElemDump : cur == NULL\n");
5493#endif
5494 return;
5495 }
5496 if (doc == NULL) {
5497#ifdef DEBUG_TREE
5498 xmlGenericError(xmlGenericErrorContext,
5499 "xmlElemDump : doc == NULL\n");
5500#endif
5501 }
5502 buf = xmlBufferCreate();
5503 if (buf == NULL) return;
5504 if ((doc != NULL) &&
5505 (doc->type == XML_HTML_DOCUMENT_NODE)) {
5506#ifdef LIBXML_HTML_ENABLED
5507 htmlNodeDump(buf, doc, cur);
5508#else
5509 xmlGenericError(xmlGenericErrorContext,
5510 "HTML support not compiled in\n");
5511#endif /* LIBXML_HTML_ENABLED */
5512 } else
5513 xmlNodeDump(buf, doc, cur, 0, 1);
5514 xmlBufferDump(f, buf);
5515 xmlBufferFree(buf);
5516}
5517
5518/************************************************************************
5519 * *
5520 * Dumping XML tree content to an I/O output buffer *
5521 * *
5522 ************************************************************************/
5523
5524void
5525xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
5526 int level, int format, const char *encoding);
5527static void
5528xmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
5529 int level, int format, const char *encoding);
5530/**
5531 * xmlNsDumpOutput:
5532 * @buf: the XML buffer output
5533 * @cur: a namespace
5534 *
5535 * Dump a local Namespace definition.
5536 * Should be called in the context of attributes dumps.
5537 */
5538static void
5539xmlNsDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) {
5540 if (cur == NULL) {
5541#ifdef DEBUG_TREE
5542 xmlGenericError(xmlGenericErrorContext,
5543 "xmlNsDump : Ns == NULL\n");
5544#endif
5545 return;
5546 }
5547 if ((cur->type == XML_LOCAL_NAMESPACE) && (cur->href != NULL)) {
5548 /* Within the context of an element attributes */
5549 if (cur->prefix != NULL) {
5550 xmlOutputBufferWriteString(buf, " xmlns:");
5551 xmlOutputBufferWriteString(buf, (const char *)cur->prefix);
5552 } else
5553 xmlOutputBufferWriteString(buf, " xmlns");
5554 xmlOutputBufferWriteString(buf, "=");
5555 xmlBufferWriteQuotedString(buf->buffer, cur->href);
5556 }
5557}
5558
5559/**
5560 * xmlNsListDumpOutput:
5561 * @buf: the XML buffer output
5562 * @cur: the first namespace
5563 *
5564 * Dump a list of local Namespace definitions.
5565 * Should be called in the context of attributes dumps.
5566 */
5567static void
5568xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) {
5569 while (cur != NULL) {
5570 xmlNsDumpOutput(buf, cur);
5571 cur = cur->next;
5572 }
5573}
5574
5575/**
5576 * xmlDtdDumpOutput:
5577 * @buf: the XML buffer output
5578 * @doc: the document
5579 * @encoding: an optional encoding string
5580 *
5581 * Dump the XML document DTD, if any.
5582 */
5583static void
5584xmlDtdDumpOutput(xmlOutputBufferPtr buf, xmlDtdPtr dtd, const char *encoding) {
5585 if (dtd == NULL) {
5586#ifdef DEBUG_TREE
5587 xmlGenericError(xmlGenericErrorContext,
5588 "xmlDtdDump : no internal subset\n");
5589#endif
5590 return;
5591 }
5592 xmlOutputBufferWriteString(buf, "<!DOCTYPE ");
5593 xmlOutputBufferWriteString(buf, (const char *)dtd->name);
5594 if (dtd->ExternalID != NULL) {
5595 xmlOutputBufferWriteString(buf, " PUBLIC ");
5596 xmlBufferWriteQuotedString(buf->buffer, dtd->ExternalID);
5597 xmlOutputBufferWriteString(buf, " ");
5598 xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID);
5599 } else if (dtd->SystemID != NULL) {
5600 xmlOutputBufferWriteString(buf, " SYSTEM ");
5601 xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID);
5602 }
5603 if ((dtd->entities == NULL) && (dtd->elements == NULL) &&
5604 (dtd->attributes == NULL) && (dtd->notations == NULL)) {
5605 xmlOutputBufferWriteString(buf, ">");
5606 return;
5607 }
5608 xmlOutputBufferWriteString(buf, " [\n");
5609 xmlNodeListDumpOutput(buf, dtd->doc, dtd->children, -1, 0, encoding);
5610 xmlOutputBufferWriteString(buf, "]>");
5611}
5612
5613/**
5614 * xmlAttrDumpOutput:
5615 * @buf: the XML buffer output
5616 * @doc: the document
5617 * @cur: the attribute pointer
5618 * @encoding: an optional encoding string
5619 *
5620 * Dump an XML attribute
5621 */
5622static void
5623xmlAttrDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00005624 const char *encoding ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00005625 xmlChar *value;
5626
5627 if (cur == NULL) {
5628#ifdef DEBUG_TREE
5629 xmlGenericError(xmlGenericErrorContext,
5630 "xmlAttrDump : property == NULL\n");
5631#endif
5632 return;
5633 }
5634 xmlOutputBufferWriteString(buf, " ");
5635 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
5636 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
5637 xmlOutputBufferWriteString(buf, ":");
5638 }
5639 xmlOutputBufferWriteString(buf, (const char *)cur->name);
5640 value = xmlNodeListGetString(doc, cur->children, 0);
5641 if (value) {
5642 xmlOutputBufferWriteString(buf, "=");
5643 xmlBufferWriteQuotedString(buf->buffer, value);
5644 xmlFree(value);
5645 } else {
5646 xmlOutputBufferWriteString(buf, "=\"\"");
5647 }
5648}
5649
5650/**
5651 * xmlAttrListDumpOutput:
5652 * @buf: the XML buffer output
5653 * @doc: the document
5654 * @cur: the first attribute pointer
5655 * @encoding: an optional encoding string
5656 *
5657 * Dump a list of XML attributes
5658 */
5659static void
5660xmlAttrListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
5661 xmlAttrPtr cur, const char *encoding) {
5662 if (cur == NULL) {
5663#ifdef DEBUG_TREE
5664 xmlGenericError(xmlGenericErrorContext,
5665 "xmlAttrListDump : property == NULL\n");
5666#endif
5667 return;
5668 }
5669 while (cur != NULL) {
5670 xmlAttrDumpOutput(buf, doc, cur, encoding);
5671 cur = cur->next;
5672 }
5673}
5674
5675
5676
5677/**
5678 * xmlNodeListDumpOutput:
5679 * @buf: the XML buffer output
5680 * @doc: the document
5681 * @cur: the first node
5682 * @level: the imbrication level for indenting
5683 * @format: is formatting allowed
5684 * @encoding: an optional encoding string
5685 *
5686 * Dump an XML node list, recursive behaviour,children are printed too.
5687 */
5688static void
5689xmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
5690 xmlNodePtr cur, int level, int format, const char *encoding) {
5691 int i;
5692
5693 if (cur == NULL) {
5694#ifdef DEBUG_TREE
5695 xmlGenericError(xmlGenericErrorContext,
5696 "xmlNodeListDump : node == NULL\n");
5697#endif
5698 return;
5699 }
5700 while (cur != NULL) {
5701 if ((format) && (xmlIndentTreeOutput) &&
5702 (cur->type == XML_ELEMENT_NODE))
5703 for (i = 0;i < level;i++)
5704 xmlOutputBufferWriteString(buf, " ");
5705 xmlNodeDumpOutput(buf, doc, cur, level, format, encoding);
5706 if (format) {
5707 xmlOutputBufferWriteString(buf, "\n");
5708 }
5709 cur = cur->next;
5710 }
5711}
5712
5713/**
5714 * xmlNodeDumpOutput:
5715 * @buf: the XML buffer output
5716 * @doc: the document
5717 * @cur: the current node
5718 * @level: the imbrication level for indenting
5719 * @format: is formatting allowed
5720 * @encoding: an optional encoding string
5721 *
5722 * Dump an XML node, recursive behaviour,children are printed too.
5723 */
5724void
5725xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
5726 int level, int format, const char *encoding) {
5727 int i;
5728 xmlNodePtr tmp;
5729
5730 if (cur == NULL) {
5731#ifdef DEBUG_TREE
5732 xmlGenericError(xmlGenericErrorContext,
5733 "xmlNodeDump : node == NULL\n");
5734#endif
5735 return;
5736 }
5737 if (cur->type == XML_XINCLUDE_START)
5738 return;
5739 if (cur->type == XML_XINCLUDE_END)
5740 return;
5741 if (cur->type == XML_DTD_NODE) {
5742 xmlDtdDumpOutput(buf, (xmlDtdPtr) cur, encoding);
5743 return;
5744 }
5745 if (cur->type == XML_ELEMENT_DECL) {
5746 xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur);
5747 return;
5748 }
5749 if (cur->type == XML_ATTRIBUTE_DECL) {
5750 xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur);
5751 return;
5752 }
5753 if (cur->type == XML_ENTITY_DECL) {
5754 xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur);
5755 return;
5756 }
5757 if (cur->type == XML_TEXT_NODE) {
5758 if (cur->content != NULL) {
5759 if ((cur->name == xmlStringText) ||
5760 (cur->name != xmlStringTextNoenc)) {
5761 xmlChar *buffer;
5762
5763#ifndef XML_USE_BUFFER_CONTENT
5764 if (encoding == NULL)
5765 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
5766 else
5767 buffer = xmlEncodeSpecialChars(doc, cur->content);
5768#else
5769 if (encoding == NULL)
5770 buffer = xmlEncodeEntitiesReentrant(doc,
5771 xmlBufferContent(cur->content));
5772 else
5773 buffer = xmlEncodeSpecialChars(doc,
5774 xmlBufferContent(cur->content));
5775#endif
5776 if (buffer != NULL) {
5777 xmlOutputBufferWriteString(buf, (const char *)buffer);
5778 xmlFree(buffer);
5779 }
5780 } else {
5781 /*
5782 * Disable escaping, needed for XSLT
5783 */
5784#ifndef XML_USE_BUFFER_CONTENT
5785 xmlOutputBufferWriteString(buf, (const char *) cur->content);
5786#else
5787 xmlOutputBufferWriteString(buf, xmlBufferContent(cur->content));
5788#endif
5789 }
5790 }
5791
5792 return;
5793 }
5794 if (cur->type == XML_PI_NODE) {
5795 if (cur->content != NULL) {
5796 xmlOutputBufferWriteString(buf, "<?");
5797 xmlOutputBufferWriteString(buf, (const char *)cur->name);
5798 if (cur->content != NULL) {
5799 xmlOutputBufferWriteString(buf, " ");
5800#ifndef XML_USE_BUFFER_CONTENT
5801 xmlOutputBufferWriteString(buf, (const char *)cur->content);
5802#else
5803 xmlOutputBufferWriteString(buf, (const char *)xmlBufferContent(cur->content));
5804#endif
5805 }
5806 xmlOutputBufferWriteString(buf, "?>");
5807 } else {
5808 xmlOutputBufferWriteString(buf, "<?");
5809 xmlOutputBufferWriteString(buf, (const char *)cur->name);
5810 xmlOutputBufferWriteString(buf, "?>");
5811 }
5812 return;
5813 }
5814 if (cur->type == XML_COMMENT_NODE) {
5815 if (cur->content != NULL) {
5816 xmlOutputBufferWriteString(buf, "<!--");
5817#ifndef XML_USE_BUFFER_CONTENT
5818 xmlOutputBufferWriteString(buf, (const char *)cur->content);
5819#else
5820 xmlOutputBufferWriteString(buf, (const char *)xmlBufferContent(cur->content));
5821#endif
5822 xmlOutputBufferWriteString(buf, "-->");
5823 }
5824 return;
5825 }
5826 if (cur->type == XML_ENTITY_REF_NODE) {
5827 xmlOutputBufferWriteString(buf, "&");
5828 xmlOutputBufferWriteString(buf, (const char *)cur->name);
5829 xmlOutputBufferWriteString(buf, ";");
5830 return;
5831 }
5832 if (cur->type == XML_CDATA_SECTION_NODE) {
5833 xmlOutputBufferWriteString(buf, "<![CDATA[");
5834 if (cur->content != NULL)
5835#ifndef XML_USE_BUFFER_CONTENT
5836 xmlOutputBufferWriteString(buf, (const char *)cur->content);
5837#else
5838 xmlOutputBufferWriteString(buf, (const char *)xmlBufferContent(cur->content));
5839#endif
5840 xmlOutputBufferWriteString(buf, "]]>");
5841 return;
5842 }
5843
5844 if (format == 1) {
5845 tmp = cur->children;
5846 while (tmp != NULL) {
5847 if ((tmp->type == XML_TEXT_NODE) ||
5848 (tmp->type == XML_ENTITY_REF_NODE)) {
5849 format = 0;
5850 break;
5851 }
5852 tmp = tmp->next;
5853 }
5854 }
5855 xmlOutputBufferWriteString(buf, "<");
5856 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
5857 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
5858 xmlOutputBufferWriteString(buf, ":");
5859 }
5860
5861 xmlOutputBufferWriteString(buf, (const char *)cur->name);
5862 if (cur->nsDef)
5863 xmlNsListDumpOutput(buf, cur->nsDef);
5864 if (cur->properties != NULL)
5865 xmlAttrListDumpOutput(buf, doc, cur->properties, encoding);
5866
5867 if ((cur->content == NULL) && (cur->children == NULL) &&
5868 (!xmlSaveNoEmptyTags)) {
5869 xmlOutputBufferWriteString(buf, "/>");
5870 return;
5871 }
5872 xmlOutputBufferWriteString(buf, ">");
5873 if (cur->content != NULL) {
5874 xmlChar *buffer;
5875
5876#ifndef XML_USE_BUFFER_CONTENT
5877 if (encoding == NULL)
5878 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
5879 else
5880 buffer = xmlEncodeSpecialChars(doc, cur->content);
5881#else
5882 if (encoding == NULL)
5883 buffer = xmlEncodeEntitiesReentrant(doc,
5884 xmlBufferContent(cur->content));
5885 else
5886 buffer = xmlEncodeSpecialChars(doc,
5887 xmlBufferContent(cur->content));
5888#endif
5889 if (buffer != NULL) {
5890 xmlOutputBufferWriteString(buf, (const char *)buffer);
5891 xmlFree(buffer);
5892 }
5893 }
5894 if (cur->children != NULL) {
5895 if (format) xmlOutputBufferWriteString(buf, "\n");
5896 xmlNodeListDumpOutput(buf, doc, cur->children,
5897 (level >= 0?level+1:-1), format, encoding);
5898 if ((xmlIndentTreeOutput) && (format))
5899 for (i = 0;i < level;i++)
5900 xmlOutputBufferWriteString(buf, " ");
5901 }
5902 xmlOutputBufferWriteString(buf, "</");
5903 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
5904 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
5905 xmlOutputBufferWriteString(buf, ":");
5906 }
5907
5908 xmlOutputBufferWriteString(buf, (const char *)cur->name);
5909 xmlOutputBufferWriteString(buf, ">");
5910}
5911
5912/**
5913 * xmlDocContentDumpOutput:
5914 * @buf: the XML buffer output
5915 * @cur: the document
5916 * @encoding: an optional encoding string
5917 * @format: should formatting spaces been added
5918 *
5919 * Dump an XML document.
5920 */
5921static void
5922xmlDocContentDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr cur,
5923 const char *encoding, int format) {
5924 xmlOutputBufferWriteString(buf, "<?xml version=");
5925 if (cur->version != NULL)
5926 xmlBufferWriteQuotedString(buf->buffer, cur->version);
5927 else
5928 xmlOutputBufferWriteString(buf, "\"1.0\"");
5929 if (encoding == NULL) {
5930 if (cur->encoding != NULL)
5931 encoding = (const char *) cur->encoding;
5932 else if (cur->charset != XML_CHAR_ENCODING_UTF8)
5933 encoding = xmlGetCharEncodingName((xmlCharEncoding) cur->charset);
5934 }
5935 if (encoding != NULL) {
5936 xmlOutputBufferWriteString(buf, " encoding=");
5937 xmlBufferWriteQuotedString(buf->buffer, (xmlChar *) encoding);
5938 }
5939 switch (cur->standalone) {
5940 case 0:
5941 xmlOutputBufferWriteString(buf, " standalone=\"no\"");
5942 break;
5943 case 1:
5944 xmlOutputBufferWriteString(buf, " standalone=\"yes\"");
5945 break;
5946 }
5947 xmlOutputBufferWriteString(buf, "?>\n");
5948 if (cur->children != NULL) {
5949 xmlNodePtr child = cur->children;
5950
5951 while (child != NULL) {
5952 xmlNodeDumpOutput(buf, cur, child, 0, format, encoding);
5953 xmlOutputBufferWriteString(buf, "\n");
5954 child = child->next;
5955 }
5956 }
5957}
5958
5959/************************************************************************
5960 * *
5961 * Saving functions front-ends *
5962 * *
5963 ************************************************************************/
5964
5965/**
5966 * xmlDocDumpMemoryEnc:
5967 * @out_doc: Document to generate XML text from
5968 * @doc_txt_ptr: Memory pointer for allocated XML text
5969 * @doc_txt_len: Length of the generated XML text
5970 * @txt_encoding: Character encoding to use when generating XML text
5971 * @format: should formatting spaces been added
5972 *
5973 * Dump the current DOM tree into memory using the character encoding specified
5974 * by the caller. Note it is up to the caller of this function to free the
5975 * allocated memory.
5976 */
5977
5978void
5979xmlDocDumpFormatMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005980 int * doc_txt_len, const char * txt_encoding,
Daniel Veillard1731d6a2001-04-10 16:38:06 +00005981 int format) {
Owen Taylor3473f882001-02-23 17:55:21 +00005982 int dummy = 0;
5983
5984 xmlCharEncoding doc_charset;
5985 xmlOutputBufferPtr out_buff = NULL;
5986 xmlCharEncodingHandlerPtr conv_hdlr = NULL;
5987
5988 if (doc_txt_len == NULL) {
5989 doc_txt_len = &dummy; /* Continue, caller just won't get length */
5990 }
5991
5992 if (doc_txt_ptr == NULL) {
5993 *doc_txt_len = 0;
5994 xmlGenericError(xmlGenericErrorContext,
5995 "xmlDocDumpFormatMemoryEnc: Null return buffer pointer.");
5996 return;
5997 }
5998
5999 *doc_txt_ptr = NULL;
6000 *doc_txt_len = 0;
6001
6002 if (out_doc == NULL) {
6003 /* No document, no output */
6004 xmlGenericError(xmlGenericErrorContext,
6005 "xmlDocDumpFormatMemoryEnc: Null DOM tree document pointer.\n");
6006 return;
6007 }
6008
6009 /*
6010 * Validate the encoding value, if provided.
6011 * This logic is copied from xmlSaveFileEnc.
6012 */
6013
6014 if (txt_encoding == NULL)
6015 txt_encoding = (const char *) out_doc->encoding;
6016 if (txt_encoding != NULL) {
6017 doc_charset = xmlParseCharEncoding(txt_encoding);
6018
6019 if (out_doc->charset != XML_CHAR_ENCODING_UTF8) {
6020 xmlGenericError(xmlGenericErrorContext,
6021 "xmlDocDumpFormatMemoryEnc: Source document not in UTF8\n");
6022 return;
6023
6024 } else if (doc_charset != XML_CHAR_ENCODING_UTF8) {
6025 conv_hdlr = xmlFindCharEncodingHandler(txt_encoding);
6026 if ( conv_hdlr == NULL ) {
6027 xmlGenericError(xmlGenericErrorContext,
6028 "%s: %s %s '%s'\n",
6029 "xmlDocDumpFormatMemoryEnc",
6030 "Failed to identify encoding handler for",
6031 "character set",
6032 txt_encoding);
6033 return;
6034 }
6035 }
6036 }
6037
6038 if ((out_buff = xmlAllocOutputBuffer(conv_hdlr)) == NULL ) {
6039 xmlGenericError(xmlGenericErrorContext,
6040 "xmlDocDumpFormatMemoryEnc: Failed to allocate output buffer.\n");
6041 return;
6042 }
6043
Daniel Veillard1731d6a2001-04-10 16:38:06 +00006044 xmlDocContentDumpOutput(out_buff, out_doc, txt_encoding, format);
Owen Taylor3473f882001-02-23 17:55:21 +00006045 xmlOutputBufferFlush(out_buff);
6046 if (out_buff->conv != NULL) {
6047 *doc_txt_len = out_buff->conv->use;
6048 *doc_txt_ptr = xmlStrndup(out_buff->conv->content, *doc_txt_len);
6049 } else {
6050 *doc_txt_len = out_buff->buffer->use;
6051 *doc_txt_ptr = xmlStrndup(out_buff->buffer->content, *doc_txt_len);
6052 }
6053 (void)xmlOutputBufferClose(out_buff);
6054
6055 if ((*doc_txt_ptr == NULL) && (*doc_txt_len > 0)) {
6056 *doc_txt_len = 0;
6057 xmlGenericError(xmlGenericErrorContext,
6058 "xmlDocDumpFormatMemoryEnc: %s\n",
6059 "Failed to allocate memory for document text representation.");
6060 }
6061
6062 return;
6063}
6064
6065/**
6066 * xmlDocDumpMemory:
6067 * @cur: the document
6068 * @mem: OUT: the memory pointer
6069 * @size: OUT: the memory lenght
6070 *
6071 * Dump an XML document in memory and return the xmlChar * and it's size.
6072 * It's up to the caller to free the memory.
6073 */
6074void
6075xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) {
6076 xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, 0);
6077}
6078
6079/**
6080 * xmlDocDumpFormatMemory:
6081 * @cur: the document
6082 * @mem: OUT: the memory pointer
6083 * @size: OUT: the memory lenght
6084 * @format: should formatting spaces been added
6085 *
6086 *
6087 * Dump an XML document in memory and return the xmlChar * and it's size.
6088 * It's up to the caller to free the memory.
6089 */
6090void
6091xmlDocDumpFormatMemory(xmlDocPtr cur, xmlChar**mem, int *size, int format) {
6092 xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, format);
6093}
6094
6095/**
6096 * xmlDocDumpMemoryEnc:
6097 * @out_doc: Document to generate XML text from
6098 * @doc_txt_ptr: Memory pointer for allocated XML text
6099 * @doc_txt_len: Length of the generated XML text
6100 * @txt_encoding: Character encoding to use when generating XML text
6101 *
6102 * Dump the current DOM tree into memory using the character encoding specified
6103 * by the caller. Note it is up to the caller of this function to free the
6104 * allocated memory.
6105 */
6106
6107void
6108xmlDocDumpMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
6109 int * doc_txt_len, const char * txt_encoding) {
6110 xmlDocDumpFormatMemoryEnc(out_doc, doc_txt_ptr, doc_txt_len,
Daniel Veillard1731d6a2001-04-10 16:38:06 +00006111 txt_encoding, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00006112}
6113
6114/**
6115 * xmlGetDocCompressMode:
6116 * @doc: the document
6117 *
6118 * get the compression ratio for a document, ZLIB based
6119 * Returns 0 (uncompressed) to 9 (max compression)
6120 */
6121int
6122xmlGetDocCompressMode (xmlDocPtr doc) {
6123 if (doc == NULL) return(-1);
6124 return(doc->compression);
6125}
6126
6127/**
6128 * xmlSetDocCompressMode:
6129 * @doc: the document
6130 * @mode: the compression ratio
6131 *
6132 * set the compression ratio for a document, ZLIB based
6133 * Correct values: 0 (uncompressed) to 9 (max compression)
6134 */
6135void
6136xmlSetDocCompressMode (xmlDocPtr doc, int mode) {
6137 if (doc == NULL) return;
6138 if (mode < 0) doc->compression = 0;
6139 else if (mode > 9) doc->compression = 9;
6140 else doc->compression = mode;
6141}
6142
6143/**
6144 * xmlGetCompressMode:
6145 *
6146 * get the default compression mode used, ZLIB based.
6147 * Returns 0 (uncompressed) to 9 (max compression)
6148 */
6149int
6150 xmlGetCompressMode(void) {
6151 return(xmlCompressMode);
6152}
6153
6154/**
6155 * xmlSetCompressMode:
6156 * @mode: the compression ratio
6157 *
6158 * set the default compression mode used, ZLIB based
6159 * Correct values: 0 (uncompressed) to 9 (max compression)
6160 */
6161void
6162xmlSetCompressMode(int mode) {
6163 if (mode < 0) xmlCompressMode = 0;
6164 else if (mode > 9) xmlCompressMode = 9;
6165 else xmlCompressMode = mode;
6166}
6167
6168/**
6169 * xmlDocDump:
6170 * @f: the FILE*
6171 * @cur: the document
6172 *
6173 * Dump an XML document to an open FILE.
6174 *
6175 * returns: the number of byte written or -1 in case of failure.
6176 */
6177int
6178xmlDocDump(FILE *f, xmlDocPtr cur) {
6179 xmlOutputBufferPtr buf;
6180 const char * encoding;
6181 xmlCharEncodingHandlerPtr handler = NULL;
6182 int ret;
6183
6184 if (cur == NULL) {
6185#ifdef DEBUG_TREE
6186 xmlGenericError(xmlGenericErrorContext,
6187 "xmlDocDump : document == NULL\n");
6188#endif
6189 return(-1);
6190 }
6191 encoding = (const char *) cur->encoding;
6192
6193 if (encoding != NULL) {
6194 xmlCharEncoding enc;
6195
6196 enc = xmlParseCharEncoding(encoding);
6197
6198 if (cur->charset != XML_CHAR_ENCODING_UTF8) {
6199 xmlGenericError(xmlGenericErrorContext,
6200 "xmlDocDump: document not in UTF8\n");
6201 return(-1);
6202 }
6203 if (enc != XML_CHAR_ENCODING_UTF8) {
6204 handler = xmlFindCharEncodingHandler(encoding);
6205 if (handler == NULL) {
6206 xmlFree((char *) cur->encoding);
6207 cur->encoding = NULL;
6208 }
6209 }
6210 }
6211 buf = xmlOutputBufferCreateFile(f, handler);
6212 if (buf == NULL) return(-1);
Daniel Veillard1731d6a2001-04-10 16:38:06 +00006213 xmlDocContentDumpOutput(buf, cur, NULL, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00006214
6215 ret = xmlOutputBufferClose(buf);
6216 return(ret);
6217}
6218
6219/**
6220 * xmlSaveFileTo:
6221 * @buf: an output I/O buffer
6222 * @cur: the document
6223 * @encoding: the encoding if any assuming the i/O layer handles the trancoding
6224 *
6225 * Dump an XML document to an I/O buffer.
6226 *
6227 * returns: the number of byte written or -1 in case of failure.
6228 */
6229int
6230xmlSaveFileTo(xmlOutputBuffer *buf, xmlDocPtr cur, const char *encoding) {
6231 int ret;
6232
6233 if (buf == NULL) return(0);
Daniel Veillard1731d6a2001-04-10 16:38:06 +00006234 xmlDocContentDumpOutput(buf, cur, encoding, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00006235 ret = xmlOutputBufferClose(buf);
6236 return(ret);
6237}
6238
6239/**
Daniel Veillardeefd4492001-04-28 16:55:50 +00006240 * xmlSaveFormatFileTo:
6241 * @buf: an output I/O buffer
6242 * @cur: the document
6243 * @encoding: the encoding if any assuming the i/O layer handles the trancoding
6244 * @format: should formatting spaces been added
6245 *
6246 * Dump an XML document to an I/O buffer.
6247 *
6248 * returns: the number of byte written or -1 in case of failure.
6249 */
6250int
6251xmlSaveFormatFileTo(xmlOutputBuffer *buf, xmlDocPtr cur, const char *encoding, int format) {
6252 int ret;
6253
6254 if (buf == NULL) return(0);
6255 xmlDocContentDumpOutput(buf, cur, encoding, format);
6256 ret = xmlOutputBufferClose(buf);
6257 return(ret);
6258}
6259
6260/**
Owen Taylor3473f882001-02-23 17:55:21 +00006261 * xmlSaveFileEnc:
6262 * @filename: the filename (or URL)
6263 * @cur: the document
6264 * @encoding: the name of an encoding (or NULL)
6265 *
6266 * Dump an XML document, converting it to the given encoding
6267 *
6268 * returns: the number of byte written or -1 in case of failure.
6269 */
6270int
6271xmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) {
6272 xmlOutputBufferPtr buf;
6273 xmlCharEncodingHandlerPtr handler = NULL;
Daniel Veillard81418e32001-05-22 15:08:55 +00006274 xmlCharEncoding enc;
Owen Taylor3473f882001-02-23 17:55:21 +00006275 int ret;
6276
6277 if (encoding != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00006278
6279 enc = xmlParseCharEncoding(encoding);
6280 if (cur->charset != XML_CHAR_ENCODING_UTF8) {
6281 xmlGenericError(xmlGenericErrorContext,
6282 "xmlSaveFileEnc: document not in UTF8\n");
6283 return(-1);
6284 }
6285 if (enc != XML_CHAR_ENCODING_UTF8) {
6286 handler = xmlFindCharEncodingHandler(encoding);
Daniel Veillard81418e32001-05-22 15:08:55 +00006287 if (handler == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00006288 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006289 }
6290 }
6291
6292 /*
6293 * save the content to a temp buffer.
6294 */
6295 buf = xmlOutputBufferCreateFilename(filename, handler, 0);
6296 if (buf == NULL) return(-1);
6297
Daniel Veillard1731d6a2001-04-10 16:38:06 +00006298 xmlDocContentDumpOutput(buf, cur, encoding, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00006299
6300 ret = xmlOutputBufferClose(buf);
6301 return(ret);
6302}
6303
6304/**
Daniel Veillard67fee942001-04-26 18:59:03 +00006305 * xmlSaveFormatFile:
Owen Taylor3473f882001-02-23 17:55:21 +00006306 * @filename: the filename (or URL)
6307 * @cur: the document
Daniel Veillard67fee942001-04-26 18:59:03 +00006308 * @format: should formatting spaces been added
Owen Taylor3473f882001-02-23 17:55:21 +00006309 *
6310 * Dump an XML document to a file. Will use compression if
6311 * compiled in and enabled. If @filename is "-" the stdout file is
Daniel Veillard67fee942001-04-26 18:59:03 +00006312 * used. If format is set then the document will be indented on output.
6313 *
Owen Taylor3473f882001-02-23 17:55:21 +00006314 * returns: the number of byte written or -1 in case of failure.
6315 */
6316int
Daniel Veillard67fee942001-04-26 18:59:03 +00006317xmlSaveFormatFile(const char *filename, xmlDocPtr cur, int format) {
Owen Taylor3473f882001-02-23 17:55:21 +00006318 xmlOutputBufferPtr buf;
6319 const char *encoding;
6320 xmlCharEncodingHandlerPtr handler = NULL;
6321 int ret;
6322
6323 if (cur == NULL)
6324 return(-1);
6325 encoding = (const char *) cur->encoding;
6326
6327 /*
6328 * save the content to a temp buffer.
6329 */
6330#ifdef HAVE_ZLIB_H
6331 if (cur->compression < 0) cur->compression = xmlCompressMode;
6332#endif
6333 if (encoding != NULL) {
6334 xmlCharEncoding enc;
6335
6336 enc = xmlParseCharEncoding(encoding);
6337
6338 if (cur->charset != XML_CHAR_ENCODING_UTF8) {
6339 xmlGenericError(xmlGenericErrorContext,
6340 "xmlSaveFile: document not in UTF8\n");
6341 return(-1);
6342 }
6343 if (enc != XML_CHAR_ENCODING_UTF8) {
6344 handler = xmlFindCharEncodingHandler(encoding);
6345 if (handler == NULL) {
6346 xmlFree((char *) cur->encoding);
6347 cur->encoding = NULL;
6348 }
6349 }
6350 }
6351
6352 buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression);
6353 if (buf == NULL) return(-1);
6354
Daniel Veillard67fee942001-04-26 18:59:03 +00006355 xmlDocContentDumpOutput(buf, cur, NULL, format);
Owen Taylor3473f882001-02-23 17:55:21 +00006356
6357 ret = xmlOutputBufferClose(buf);
6358 return(ret);
6359}
6360
Daniel Veillard67fee942001-04-26 18:59:03 +00006361/**
6362 * xmlSaveFile:
6363 * @filename: the filename (or URL)
6364 * @cur: the document
6365 *
6366 * Dump an XML document to a file. Will use compression if
6367 * compiled in and enabled. If @filename is "-" the stdout file is
6368 * used.
6369 * returns: the number of byte written or -1 in case of failure.
6370 */
6371int
6372xmlSaveFile(const char *filename, xmlDocPtr cur) {
6373 return(xmlSaveFormatFile(filename, cur, 0));
6374}
6375