blob: 4b32f9246b84833a0b64e89b27d64f916cc44840 [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 }
Daniel Veillard42596ad2001-05-22 16:57:14 +00003270 if (cur->type == XML_ELEMENT_NODE) {
3271 base = xmlGetProp(cur, BAD_CAST "xml:base");
3272 if (base != NULL) {
3273 /* TODO : apply cascade in the base resolution ! */
3274 return(base);
3275 }
3276 }
Owen Taylor3473f882001-02-23 17:55:21 +00003277 cur = cur->parent;
3278 }
3279 if ((doc != NULL) && (doc->URL != NULL))
3280 return(xmlStrdup(doc->URL));
3281 return(NULL);
3282}
3283
3284/**
3285 * xmlNodeGetContent:
3286 * @cur: the node being read
3287 *
3288 * Read the value of a node, this can be either the text carried
3289 * directly by this node if it's a TEXT node or the aggregate string
3290 * of the values carried by this node child's (TEXT and ENTITY_REF).
3291 * Entity references are substitued.
3292 * Returns a new xmlChar * or NULL if no content is available.
3293 * It's up to the caller to free the memory.
3294 */
3295xmlChar *
3296xmlNodeGetContent(xmlNodePtr cur) {
3297 if (cur == NULL) return(NULL);
3298 switch (cur->type) {
3299 case XML_DOCUMENT_FRAG_NODE:
3300 case XML_ELEMENT_NODE: {
3301 xmlNodePtr tmp = cur;
3302 xmlBufferPtr buffer;
3303 xmlChar *ret;
3304
3305 buffer = xmlBufferCreate();
3306 if (buffer == NULL)
3307 return(NULL);
3308 while (tmp != NULL) {
3309 switch (tmp->type) {
3310 case XML_ELEMENT_NODE:
3311 case XML_TEXT_NODE:
3312 if (tmp->content != NULL)
3313#ifndef XML_USE_BUFFER_CONTENT
3314 xmlBufferCat(buffer, tmp->content);
3315#else
3316 xmlBufferCat(buffer,
3317 xmlBufferContent(tmp->content));
3318#endif
3319 break;
3320 case XML_ENTITY_REF_NODE: {
3321 xmlEntityPtr ent;
3322
3323 ent = xmlGetDocEntity(cur->doc, tmp->name);
3324 if (ent != NULL)
3325 xmlBufferCat(buffer, ent->content);
3326 }
3327 default:
3328 break;
3329 }
3330 /*
3331 * Skip to next node
3332 */
3333 if (tmp->children != NULL) {
3334 if (tmp->children->type != XML_ENTITY_DECL) {
3335 tmp = tmp->children;
3336 continue;
3337 }
3338 }
Daniel Veillard6c831202001-03-07 15:57:53 +00003339 if (tmp == cur)
3340 break;
3341
Owen Taylor3473f882001-02-23 17:55:21 +00003342 if (tmp->next != NULL) {
3343 tmp = tmp->next;
3344 continue;
3345 }
3346
3347 do {
3348 tmp = tmp->parent;
3349 if (tmp == NULL)
3350 break;
Daniel Veillard6c831202001-03-07 15:57:53 +00003351 if (tmp == cur) {
Owen Taylor3473f882001-02-23 17:55:21 +00003352 tmp = NULL;
3353 break;
3354 }
3355 if (tmp->next != NULL) {
3356 tmp = tmp->next;
3357 break;
3358 }
3359 } while (tmp != NULL);
3360 }
3361 ret = buffer->content;
3362 buffer->content = NULL;
3363 xmlBufferFree(buffer);
3364 return(ret);
3365 }
3366 case XML_ATTRIBUTE_NODE: {
3367 xmlAttrPtr attr = (xmlAttrPtr) cur;
3368 if (attr->parent != NULL)
3369 return(xmlNodeListGetString(attr->parent->doc, attr->children, 1));
3370 else
3371 return(xmlNodeListGetString(NULL, attr->children, 1));
3372 break;
3373 }
3374 case XML_COMMENT_NODE:
3375 case XML_PI_NODE:
3376 if (cur->content != NULL)
3377#ifndef XML_USE_BUFFER_CONTENT
3378 return(xmlStrdup(cur->content));
3379#else
3380 return(xmlStrdup(xmlBufferContent(cur->content)));
3381#endif
3382 return(NULL);
3383 case XML_ENTITY_REF_NODE:
3384 /*
3385 * Locate the entity, and get it's content
3386 * @@@
3387 */
3388 return(NULL);
3389 case XML_ENTITY_NODE:
3390 case XML_DOCUMENT_NODE:
3391 case XML_HTML_DOCUMENT_NODE:
3392 case XML_DOCUMENT_TYPE_NODE:
3393 case XML_NOTATION_NODE:
3394 case XML_DTD_NODE:
3395 case XML_XINCLUDE_START:
3396 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003397#ifdef LIBXML_DOCB_ENABLED
3398 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003399#endif
3400 return(NULL);
3401 case XML_NAMESPACE_DECL:
3402 return(xmlStrdup(((xmlNsPtr)cur)->href));
3403 case XML_ELEMENT_DECL:
3404 /* TODO !!! */
3405 return(NULL);
3406 case XML_ATTRIBUTE_DECL:
3407 /* TODO !!! */
3408 return(NULL);
3409 case XML_ENTITY_DECL:
3410 /* TODO !!! */
3411 return(NULL);
3412 case XML_CDATA_SECTION_NODE:
3413 case XML_TEXT_NODE:
3414 if (cur->content != NULL)
3415#ifndef XML_USE_BUFFER_CONTENT
3416 return(xmlStrdup(cur->content));
3417#else
3418 return(xmlStrdup(xmlBufferContent(cur->content)));
3419#endif
3420 return(NULL);
3421 }
3422 return(NULL);
3423}
3424
3425/**
3426 * xmlNodeSetContent:
3427 * @cur: the node being modified
3428 * @content: the new value of the content
3429 *
3430 * Replace the content of a node.
3431 */
3432void
3433xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) {
3434 if (cur == NULL) {
3435#ifdef DEBUG_TREE
3436 xmlGenericError(xmlGenericErrorContext,
3437 "xmlNodeSetContent : node == NULL\n");
3438#endif
3439 return;
3440 }
3441 switch (cur->type) {
3442 case XML_DOCUMENT_FRAG_NODE:
3443 case XML_ELEMENT_NODE:
3444 if (cur->content != NULL) {
3445#ifndef XML_USE_BUFFER_CONTENT
3446 xmlFree(cur->content);
3447#else
3448 xmlBufferFree(cur->content);
3449#endif
3450 cur->content = NULL;
3451 }
3452 if (cur->children != NULL) xmlFreeNodeList(cur->children);
3453 cur->children = xmlStringGetNodeList(cur->doc, content);
3454 UPDATE_LAST_CHILD_AND_PARENT(cur)
3455 break;
3456 case XML_ATTRIBUTE_NODE:
3457 break;
3458 case XML_TEXT_NODE:
3459 case XML_CDATA_SECTION_NODE:
3460 case XML_ENTITY_REF_NODE:
3461 case XML_ENTITY_NODE:
3462 case XML_PI_NODE:
3463 case XML_COMMENT_NODE:
3464 if (cur->content != NULL) {
3465#ifndef XML_USE_BUFFER_CONTENT
3466 xmlFree(cur->content);
3467#else
3468 xmlBufferFree(cur->content);
3469#endif
3470 }
3471 if (cur->children != NULL) xmlFreeNodeList(cur->children);
3472 cur->last = cur->children = NULL;
3473 if (content != NULL) {
3474#ifndef XML_USE_BUFFER_CONTENT
3475 cur->content = xmlStrdup(content);
3476#else
3477 cur->content = xmlBufferCreateSize(0);
3478 xmlBufferSetAllocationScheme(cur->content,
3479 xmlGetBufferAllocationScheme());
3480 xmlBufferAdd(cur->content, content, -1);
3481#endif
3482 } else
3483 cur->content = NULL;
3484 break;
3485 case XML_DOCUMENT_NODE:
3486 case XML_HTML_DOCUMENT_NODE:
3487 case XML_DOCUMENT_TYPE_NODE:
3488 case XML_XINCLUDE_START:
3489 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003490#ifdef LIBXML_DOCB_ENABLED
3491 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003492#endif
3493 break;
3494 case XML_NOTATION_NODE:
3495 break;
3496 case XML_DTD_NODE:
3497 break;
3498 case XML_NAMESPACE_DECL:
3499 break;
3500 case XML_ELEMENT_DECL:
3501 /* TODO !!! */
3502 break;
3503 case XML_ATTRIBUTE_DECL:
3504 /* TODO !!! */
3505 break;
3506 case XML_ENTITY_DECL:
3507 /* TODO !!! */
3508 break;
3509 }
3510}
3511
3512/**
3513 * xmlNodeSetContentLen:
3514 * @cur: the node being modified
3515 * @content: the new value of the content
3516 * @len: the size of @content
3517 *
3518 * Replace the content of a node.
3519 */
3520void
3521xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
3522 if (cur == NULL) {
3523#ifdef DEBUG_TREE
3524 xmlGenericError(xmlGenericErrorContext,
3525 "xmlNodeSetContentLen : node == NULL\n");
3526#endif
3527 return;
3528 }
3529 switch (cur->type) {
3530 case XML_DOCUMENT_FRAG_NODE:
3531 case XML_ELEMENT_NODE:
3532 if (cur->content != NULL) {
3533#ifndef XML_USE_BUFFER_CONTENT
3534 xmlFree(cur->content);
3535#else
3536 xmlBufferFree(cur->content);
3537#endif
3538 cur->content = NULL;
3539 }
3540 if (cur->children != NULL) xmlFreeNodeList(cur->children);
3541 cur->children = xmlStringLenGetNodeList(cur->doc, content, len);
3542 UPDATE_LAST_CHILD_AND_PARENT(cur)
3543 break;
3544 case XML_ATTRIBUTE_NODE:
3545 break;
3546 case XML_TEXT_NODE:
3547 case XML_CDATA_SECTION_NODE:
3548 case XML_ENTITY_REF_NODE:
3549 case XML_ENTITY_NODE:
3550 case XML_PI_NODE:
3551 case XML_COMMENT_NODE:
3552 case XML_NOTATION_NODE:
3553 if (cur->content != NULL) {
3554#ifndef XML_USE_BUFFER_CONTENT
3555 xmlFree(cur->content);
3556#else
3557 xmlBufferFree(cur->content);
3558#endif
3559 }
3560 if (cur->children != NULL) xmlFreeNodeList(cur->children);
3561 cur->children = cur->last = NULL;
3562 if (content != NULL) {
3563#ifndef XML_USE_BUFFER_CONTENT
3564 cur->content = xmlStrndup(content, len);
3565#else
3566 cur->content = xmlBufferCreateSize(len);
3567 xmlBufferSetAllocationScheme(cur->content,
3568 xmlGetBufferAllocationScheme());
3569 xmlBufferAdd(cur->content, content, len);
3570#endif
3571 } else
3572 cur->content = NULL;
3573 break;
3574 case XML_DOCUMENT_NODE:
3575 case XML_DTD_NODE:
3576 case XML_HTML_DOCUMENT_NODE:
3577 case XML_DOCUMENT_TYPE_NODE:
3578 case XML_NAMESPACE_DECL:
3579 case XML_XINCLUDE_START:
3580 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003581#ifdef LIBXML_DOCB_ENABLED
3582 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003583#endif
3584 break;
3585 case XML_ELEMENT_DECL:
3586 /* TODO !!! */
3587 break;
3588 case XML_ATTRIBUTE_DECL:
3589 /* TODO !!! */
3590 break;
3591 case XML_ENTITY_DECL:
3592 /* TODO !!! */
3593 break;
3594 }
3595}
3596
3597/**
3598 * xmlNodeAddContentLen:
3599 * @cur: the node being modified
3600 * @content: extra content
3601 * @len: the size of @content
3602 *
3603 * Append the extra substring to the node content.
3604 */
3605void
3606xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
3607 if (cur == NULL) {
3608#ifdef DEBUG_TREE
3609 xmlGenericError(xmlGenericErrorContext,
3610 "xmlNodeAddContentLen : node == NULL\n");
3611#endif
3612 return;
3613 }
3614 if (len <= 0) return;
3615 switch (cur->type) {
3616 case XML_DOCUMENT_FRAG_NODE:
3617 case XML_ELEMENT_NODE: {
3618 xmlNodePtr last = NULL, newNode;
3619
3620 if (cur->children != NULL) {
3621 last = cur->last;
3622 } else {
3623 if (cur->content != NULL) {
3624#ifndef XML_USE_BUFFER_CONTENT
3625 cur->children = xmlStringGetNodeList(cur->doc, cur->content);
3626#else
3627 cur->children = xmlStringGetNodeList(cur->doc,
3628 xmlBufferContent(cur->content));
3629#endif
3630 UPDATE_LAST_CHILD_AND_PARENT(cur)
3631#ifndef XML_USE_BUFFER_CONTENT
3632 xmlFree(cur->content);
3633#else
3634 xmlBufferFree(cur->content);
3635#endif
3636 cur->content = NULL;
3637 last = cur->last;
3638 }
3639 }
3640 newNode = xmlNewTextLen(content, len);
3641 if (newNode != NULL) {
3642 xmlAddChild(cur, newNode);
3643 if ((last != NULL) && (last->next == newNode)) {
3644 xmlTextMerge(last, newNode);
3645 }
3646 }
3647 break;
3648 }
3649 case XML_ATTRIBUTE_NODE:
3650 break;
3651 case XML_TEXT_NODE:
3652 case XML_CDATA_SECTION_NODE:
3653 case XML_ENTITY_REF_NODE:
3654 case XML_ENTITY_NODE:
3655 case XML_PI_NODE:
3656 case XML_COMMENT_NODE:
3657 case XML_NOTATION_NODE:
3658 if (content != NULL) {
3659#ifndef XML_USE_BUFFER_CONTENT
3660 cur->content = xmlStrncat(cur->content, content, len);
3661#else
3662 xmlBufferAdd(cur->content, content, len);
3663#endif
3664 }
3665 case XML_DOCUMENT_NODE:
3666 case XML_DTD_NODE:
3667 case XML_HTML_DOCUMENT_NODE:
3668 case XML_DOCUMENT_TYPE_NODE:
3669 case XML_NAMESPACE_DECL:
3670 case XML_XINCLUDE_START:
3671 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003672#ifdef LIBXML_DOCB_ENABLED
3673 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003674#endif
3675 break;
3676 case XML_ELEMENT_DECL:
3677 case XML_ATTRIBUTE_DECL:
3678 case XML_ENTITY_DECL:
3679 break;
3680 }
3681}
3682
3683/**
3684 * xmlNodeAddContent:
3685 * @cur: the node being modified
3686 * @content: extra content
3687 *
3688 * Append the extra substring to the node content.
3689 */
3690void
3691xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) {
3692 int len;
3693
3694 if (cur == NULL) {
3695#ifdef DEBUG_TREE
3696 xmlGenericError(xmlGenericErrorContext,
3697 "xmlNodeAddContent : node == NULL\n");
3698#endif
3699 return;
3700 }
3701 if (content == NULL) return;
3702 len = xmlStrlen(content);
3703 xmlNodeAddContentLen(cur, content, len);
3704}
3705
3706/**
3707 * xmlTextMerge:
3708 * @first: the first text node
3709 * @second: the second text node being merged
3710 *
3711 * Merge two text nodes into one
3712 * Returns the first text node augmented
3713 */
3714xmlNodePtr
3715xmlTextMerge(xmlNodePtr first, xmlNodePtr second) {
3716 if (first == NULL) return(second);
3717 if (second == NULL) return(first);
3718 if (first->type != XML_TEXT_NODE) return(first);
3719 if (second->type != XML_TEXT_NODE) return(first);
3720 if (second->name != first->name)
3721 return(first);
3722#ifndef XML_USE_BUFFER_CONTENT
3723 xmlNodeAddContent(first, second->content);
3724#else
3725 xmlNodeAddContent(first, xmlBufferContent(second->content));
3726#endif
3727 xmlUnlinkNode(second);
3728 xmlFreeNode(second);
3729 return(first);
3730}
3731
3732/**
3733 * xmlGetNsList:
3734 * @doc: the document
3735 * @node: the current node
3736 *
3737 * Search all the namespace applying to a given element.
3738 * Returns an NULL terminated array of all the xmlNsPtr found
3739 * that need to be freed by the caller or NULL if no
3740 * namespace if defined
3741 */
3742xmlNsPtr *
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003743xmlGetNsList(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00003744 xmlNsPtr cur;
3745 xmlNsPtr *ret = NULL;
3746 int nbns = 0;
3747 int maxns = 10;
3748 int i;
3749
3750 while (node != NULL) {
3751 cur = node->nsDef;
3752 while (cur != NULL) {
3753 if (ret == NULL) {
3754 ret = (xmlNsPtr *) xmlMalloc((maxns + 1) * sizeof(xmlNsPtr));
3755 if (ret == NULL) {
3756 xmlGenericError(xmlGenericErrorContext,
3757 "xmlGetNsList : out of memory!\n");
3758 return(NULL);
3759 }
3760 ret[nbns] = NULL;
3761 }
3762 for (i = 0;i < nbns;i++) {
3763 if ((cur->prefix == ret[i]->prefix) ||
3764 (xmlStrEqual(cur->prefix, ret[i]->prefix))) break;
3765 }
3766 if (i >= nbns) {
3767 if (nbns >= maxns) {
3768 maxns *= 2;
3769 ret = (xmlNsPtr *) xmlRealloc(ret,
3770 (maxns + 1) * sizeof(xmlNsPtr));
3771 if (ret == NULL) {
3772 xmlGenericError(xmlGenericErrorContext,
3773 "xmlGetNsList : realloc failed!\n");
3774 return(NULL);
3775 }
3776 }
3777 ret[nbns++] = cur;
3778 ret[nbns] = NULL;
3779 }
3780
3781 cur = cur->next;
3782 }
3783 node = node->parent;
3784 }
3785 return(ret);
3786}
3787
3788/**
3789 * xmlSearchNs:
3790 * @doc: the document
3791 * @node: the current node
Daniel Veillard77851712001-02-27 21:54:07 +00003792 * @nameSpace: the namespace prefix
Owen Taylor3473f882001-02-23 17:55:21 +00003793 *
3794 * Search a Ns registered under a given name space for a document.
3795 * recurse on the parents until it finds the defined namespace
3796 * or return NULL otherwise.
3797 * @nameSpace can be NULL, this is a search for the default namespace.
3798 * We don't allow to cross entities boundaries. If you don't declare
3799 * the namespace within those you will be in troubles !!! A warning
3800 * is generated to cover this case.
3801 *
3802 * Returns the namespace pointer or NULL.
3803 */
3804xmlNsPtr
3805xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) {
3806 xmlNsPtr cur;
3807
3808 if (node == NULL) return(NULL);
3809 if ((nameSpace != NULL) &&
3810 (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) {
3811 if (doc->oldNs == NULL) {
3812 /*
3813 * Allocate a new Namespace and fill the fields.
3814 */
3815 doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
3816 if (doc->oldNs == NULL) {
3817 xmlGenericError(xmlGenericErrorContext,
3818 "xmlSearchNsByHref : malloc failed\n");
3819 return(NULL);
3820 }
3821 memset(doc->oldNs, 0, sizeof(xmlNs));
3822 doc->oldNs->type = XML_LOCAL_NAMESPACE;
3823
3824 doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE);
3825 doc->oldNs->prefix = xmlStrdup((const xmlChar *)"xml");
3826 }
3827 return(doc->oldNs);
3828 }
3829 while (node != NULL) {
3830 if ((node->type == XML_ENTITY_REF_NODE) ||
3831 (node->type == XML_ENTITY_NODE) ||
3832 (node->type == XML_ENTITY_DECL))
3833 return(NULL);
3834 if (node->type == XML_ELEMENT_NODE) {
3835 cur = node->nsDef;
3836 while (cur != NULL) {
3837 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
3838 (cur->href != NULL))
3839 return(cur);
3840 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
3841 (cur->href != NULL) &&
3842 (xmlStrEqual(cur->prefix, nameSpace)))
3843 return(cur);
3844 cur = cur->next;
3845 }
3846 }
3847 node = node->parent;
3848 }
3849 return(NULL);
3850}
3851
3852/**
3853 * xmlSearchNsByHref:
3854 * @doc: the document
3855 * @node: the current node
3856 * @href: the namespace value
3857 *
3858 * Search a Ns aliasing a given URI. Recurse on the parents until it finds
3859 * the defined namespace or return NULL otherwise.
3860 * Returns the namespace pointer or NULL.
3861 */
3862xmlNsPtr
3863xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar *href) {
3864 xmlNsPtr cur;
3865 xmlNodePtr orig = node;
3866
3867 if ((node == NULL) || (href == NULL)) return(NULL);
3868 if (xmlStrEqual(href, XML_XML_NAMESPACE)) {
3869 if (doc->oldNs == NULL) {
3870 /*
3871 * Allocate a new Namespace and fill the fields.
3872 */
3873 doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
3874 if (doc->oldNs == NULL) {
3875 xmlGenericError(xmlGenericErrorContext,
3876 "xmlSearchNsByHref : malloc failed\n");
3877 return(NULL);
3878 }
3879 memset(doc->oldNs, 0, sizeof(xmlNs));
3880 doc->oldNs->type = XML_LOCAL_NAMESPACE;
3881
3882 doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE);
3883 doc->oldNs->prefix = xmlStrdup((const xmlChar *)"xml");
3884 }
3885 return(doc->oldNs);
3886 }
3887 while (node != NULL) {
3888 cur = node->nsDef;
3889 while (cur != NULL) {
3890 if ((cur->href != NULL) && (href != NULL) &&
3891 (xmlStrEqual(cur->href, href))) {
3892 /*
3893 * Check that the prefix is not shadowed between orig and node
3894 */
3895 xmlNodePtr check = orig;
3896 xmlNsPtr tst;
3897
3898 while (check != node) {
3899 tst = check->nsDef;
3900 while (tst != NULL) {
3901 if ((tst->prefix == NULL) && (cur->prefix == NULL))
3902 goto shadowed;
3903 if ((tst->prefix != NULL) && (cur->prefix != NULL) &&
3904 (xmlStrEqual(tst->prefix, cur->prefix)))
3905 goto shadowed;
3906 tst = tst->next;
3907 }
3908 check = check->parent;
3909 }
3910 return(cur);
3911 }
3912shadowed:
3913 cur = cur->next;
3914 }
3915 node = node->parent;
3916 }
3917 return(NULL);
3918}
3919
3920/**
3921 * xmlNewReconciliedNs
3922 * @doc: the document
3923 * @tree: a node expected to hold the new namespace
3924 * @ns: the original namespace
3925 *
3926 * This function tries to locate a namespace definition in a tree
3927 * ancestors, or create a new namespace definition node similar to
3928 * @ns trying to reuse the same prefix. However if the given prefix is
3929 * null (default namespace) or reused within the subtree defined by
3930 * @tree or on one of its ancestors then a new prefix is generated.
3931 * Returns the (new) namespace definition or NULL in case of error
3932 */
3933xmlNsPtr
3934xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) {
3935 xmlNsPtr def;
3936 xmlChar prefix[50];
3937 int counter = 1;
3938
3939 if (tree == NULL) {
3940#ifdef DEBUG_TREE
3941 xmlGenericError(xmlGenericErrorContext,
3942 "xmlNewReconciliedNs : tree == NULL\n");
3943#endif
3944 return(NULL);
3945 }
3946 if (ns == NULL) {
3947#ifdef DEBUG_TREE
3948 xmlGenericError(xmlGenericErrorContext,
3949 "xmlNewReconciliedNs : ns == NULL\n");
3950#endif
3951 return(NULL);
3952 }
3953 /*
3954 * Search an existing namespace definition inherited.
3955 */
3956 def = xmlSearchNsByHref(doc, tree, ns->href);
3957 if (def != NULL)
3958 return(def);
3959
3960 /*
3961 * Find a close prefix which is not already in use.
3962 * Let's strip namespace prefixes longer than 20 chars !
3963 */
3964 sprintf((char *) prefix, "%.20s", ns->prefix);
3965 def = xmlSearchNs(doc, tree, prefix);
3966 while (def != NULL) {
3967 if (counter > 1000) return(NULL);
3968 sprintf((char *) prefix, "%.20s%d", ns->prefix, counter++);
3969 def = xmlSearchNs(doc, tree, prefix);
3970 }
3971
3972 /*
3973 * Ok, now we are ready to create a new one.
3974 */
3975 def = xmlNewNs(tree, ns->href, prefix);
3976 return(def);
3977}
3978
3979/**
3980 * xmlReconciliateNs
3981 * @doc: the document
3982 * @tree: a node defining the subtree to reconciliate
3983 *
3984 * This function checks that all the namespaces declared within the given
3985 * tree are properly declared. This is needed for example after Copy or Cut
3986 * and then paste operations. The subtree may still hold pointers to
3987 * namespace declarations outside the subtree or invalid/masked. As much
3988 * as possible the function try tu reuse the existing namespaces found in
3989 * the new environment. If not possible the new namespaces are redeclared
3990 * on @tree at the top of the given subtree.
3991 * Returns the number of namespace declarations created or -1 in case of error.
3992 */
3993int
3994xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) {
3995 xmlNsPtr *oldNs = NULL;
3996 xmlNsPtr *newNs = NULL;
3997 int sizeCache = 0;
3998 int nbCache = 0;
3999
4000 xmlNsPtr n;
4001 xmlNodePtr node = tree;
4002 xmlAttrPtr attr;
4003 int ret = 0, i;
4004
4005 while (node != NULL) {
4006 /*
4007 * Reconciliate the node namespace
4008 */
4009 if (node->ns != NULL) {
4010 /*
4011 * initialize the cache if needed
4012 */
4013 if (sizeCache == 0) {
4014 sizeCache = 10;
4015 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
4016 sizeof(xmlNsPtr));
4017 if (oldNs == NULL) {
4018 xmlGenericError(xmlGenericErrorContext,
4019 "xmlReconciliateNs : memory pbm\n");
4020 return(-1);
4021 }
4022 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
4023 sizeof(xmlNsPtr));
4024 if (newNs == NULL) {
4025 xmlGenericError(xmlGenericErrorContext,
4026 "xmlReconciliateNs : memory pbm\n");
4027 xmlFree(oldNs);
4028 return(-1);
4029 }
4030 }
4031 for (i = 0;i < nbCache;i++) {
4032 if (oldNs[i] == node->ns) {
4033 node->ns = newNs[i];
4034 break;
4035 }
4036 }
4037 if (i == nbCache) {
4038 /*
4039 * Ok we need to recreate a new namespace definition
4040 */
4041 n = xmlNewReconciliedNs(doc, tree, node->ns);
4042 if (n != NULL) { /* :-( what if else ??? */
4043 /*
4044 * check if we need to grow the cache buffers.
4045 */
4046 if (sizeCache <= nbCache) {
4047 sizeCache *= 2;
4048 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
4049 sizeof(xmlNsPtr));
4050 if (oldNs == NULL) {
4051 xmlGenericError(xmlGenericErrorContext,
4052 "xmlReconciliateNs : memory pbm\n");
4053 xmlFree(newNs);
4054 return(-1);
4055 }
4056 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
4057 sizeof(xmlNsPtr));
4058 if (newNs == NULL) {
4059 xmlGenericError(xmlGenericErrorContext,
4060 "xmlReconciliateNs : memory pbm\n");
4061 xmlFree(oldNs);
4062 return(-1);
4063 }
4064 }
4065 newNs[nbCache] = n;
4066 oldNs[nbCache++] = node->ns;
4067 node->ns = n;
4068 }
4069 }
4070 }
4071 /*
4072 * now check for namespace hold by attributes on the node.
4073 */
4074 attr = node->properties;
4075 while (attr != NULL) {
4076 if (attr->ns != NULL) {
4077 /*
4078 * initialize the cache if needed
4079 */
4080 if (sizeCache == 0) {
4081 sizeCache = 10;
4082 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
4083 sizeof(xmlNsPtr));
4084 if (oldNs == NULL) {
4085 xmlGenericError(xmlGenericErrorContext,
4086 "xmlReconciliateNs : memory pbm\n");
4087 return(-1);
4088 }
4089 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
4090 sizeof(xmlNsPtr));
4091 if (newNs == NULL) {
4092 xmlGenericError(xmlGenericErrorContext,
4093 "xmlReconciliateNs : memory pbm\n");
4094 xmlFree(oldNs);
4095 return(-1);
4096 }
4097 }
4098 for (i = 0;i < nbCache;i++) {
4099 if (oldNs[i] == attr->ns) {
4100 node->ns = newNs[i];
4101 break;
4102 }
4103 }
4104 if (i == nbCache) {
4105 /*
4106 * Ok we need to recreate a new namespace definition
4107 */
4108 n = xmlNewReconciliedNs(doc, tree, attr->ns);
4109 if (n != NULL) { /* :-( what if else ??? */
4110 /*
4111 * check if we need to grow the cache buffers.
4112 */
4113 if (sizeCache <= nbCache) {
4114 sizeCache *= 2;
4115 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
4116 sizeof(xmlNsPtr));
4117 if (oldNs == NULL) {
4118 xmlGenericError(xmlGenericErrorContext,
4119 "xmlReconciliateNs : memory pbm\n");
4120 xmlFree(newNs);
4121 return(-1);
4122 }
4123 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
4124 sizeof(xmlNsPtr));
4125 if (newNs == NULL) {
4126 xmlGenericError(xmlGenericErrorContext,
4127 "xmlReconciliateNs : memory pbm\n");
4128 xmlFree(oldNs);
4129 return(-1);
4130 }
4131 }
4132 newNs[nbCache] = n;
4133 oldNs[nbCache++] = attr->ns;
4134 attr->ns = n;
4135 }
4136 }
4137 }
4138 attr = attr->next;
4139 }
4140
4141 /*
4142 * Browse the full subtree, deep first
4143 */
4144 if (node->children != NULL) {
4145 /* deep first */
4146 node = node->children;
4147 } else if ((node != tree) && (node->next != NULL)) {
4148 /* then siblings */
4149 node = node->next;
4150 } else if (node != tree) {
4151 /* go up to parents->next if needed */
4152 while (node != tree) {
4153 if (node->parent != NULL)
4154 node = node->parent;
4155 if ((node != tree) && (node->next != NULL)) {
4156 node = node->next;
4157 break;
4158 }
4159 if (node->parent == NULL) {
4160 node = NULL;
4161 break;
4162 }
4163 }
4164 /* exit condition */
4165 if (node == tree)
4166 node = NULL;
4167 }
4168 }
4169 return(ret);
4170}
4171
4172/**
4173 * xmlHasProp:
4174 * @node: the node
4175 * @name: the attribute name
4176 *
4177 * Search an attribute associated to a node
4178 * This function also looks in DTD attribute declaration for #FIXED or
4179 * default declaration values unless DTD use has been turned off.
4180 *
4181 * Returns the attribute or the attribute declaration or NULL if
4182 * neither was found.
4183 */
4184xmlAttrPtr
4185xmlHasProp(xmlNodePtr node, const xmlChar *name) {
4186 xmlAttrPtr prop;
4187 xmlDocPtr doc;
4188
4189 if ((node == NULL) || (name == NULL)) return(NULL);
4190 /*
4191 * Check on the properties attached to the node
4192 */
4193 prop = node->properties;
4194 while (prop != NULL) {
4195 if (xmlStrEqual(prop->name, name)) {
4196 return(prop);
4197 }
4198 prop = prop->next;
4199 }
4200 if (!xmlCheckDTD) return(NULL);
4201
4202 /*
4203 * Check if there is a default declaration in the internal
4204 * or external subsets
4205 */
4206 doc = node->doc;
4207 if (doc != NULL) {
4208 xmlAttributePtr attrDecl;
4209 if (doc->intSubset != NULL) {
4210 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
4211 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4212 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
4213 if (attrDecl != NULL)
4214 return((xmlAttrPtr) attrDecl);
4215 }
4216 }
4217 return(NULL);
4218}
4219
4220/**
4221 * xmlGetProp:
4222 * @node: the node
4223 * @name: the attribute name
4224 *
4225 * Search and get the value of an attribute associated to a node
4226 * This does the entity substitution.
4227 * This function looks in DTD attribute declaration for #FIXED or
4228 * default declaration values unless DTD use has been turned off.
4229 *
4230 * Returns the attribute value or NULL if not found.
4231 * It's up to the caller to free the memory.
4232 */
4233xmlChar *
4234xmlGetProp(xmlNodePtr node, const xmlChar *name) {
4235 xmlAttrPtr prop;
4236 xmlDocPtr doc;
4237
4238 if ((node == NULL) || (name == NULL)) return(NULL);
4239 /*
4240 * Check on the properties attached to the node
4241 */
4242 prop = node->properties;
4243 while (prop != NULL) {
4244 if (xmlStrEqual(prop->name, name)) {
4245 xmlChar *ret;
4246
4247 ret = xmlNodeListGetString(node->doc, prop->children, 1);
4248 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
4249 return(ret);
4250 }
4251 prop = prop->next;
4252 }
4253 if (!xmlCheckDTD) return(NULL);
4254
4255 /*
4256 * Check if there is a default declaration in the internal
4257 * or external subsets
4258 */
4259 doc = node->doc;
4260 if (doc != NULL) {
4261 xmlAttributePtr attrDecl;
4262 if (doc->intSubset != NULL) {
4263 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
4264 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4265 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
4266 if (attrDecl != NULL)
4267 return(xmlStrdup(attrDecl->defaultValue));
4268 }
4269 }
4270 return(NULL);
4271}
4272
4273/**
4274 * xmlGetNsProp:
4275 * @node: the node
4276 * @name: the attribute name
4277 * @namespace: the URI of the namespace
4278 *
4279 * Search and get the value of an attribute associated to a node
4280 * This attribute has to be anchored in the namespace specified.
4281 * This does the entity substitution.
4282 * This function looks in DTD attribute declaration for #FIXED or
4283 * default declaration values unless DTD use has been turned off.
4284 *
4285 * Returns the attribute value or NULL if not found.
4286 * It's up to the caller to free the memory.
4287 */
4288xmlChar *
4289xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *namespace) {
4290 xmlAttrPtr prop;
4291 xmlDocPtr doc;
4292 xmlNsPtr ns;
4293
4294 if (node == NULL)
4295 return(NULL);
4296
4297 prop = node->properties;
4298 if (namespace == NULL)
4299 return(xmlGetProp(node, name));
4300 while (prop != NULL) {
4301 /*
4302 * One need to have
4303 * - same attribute names
4304 * - and the attribute carrying that namespace
4305 * or
4306 * no namespace on the attribute and the element carrying it
4307 */
4308 if ((xmlStrEqual(prop->name, name)) &&
4309 (((prop->ns == NULL) && (node->ns != NULL) &&
4310 (xmlStrEqual(node->ns->href, namespace))) ||
Daniel Veillard5792e162001-04-30 17:44:45 +00004311 ((prop->ns != NULL) &&
4312 (xmlStrEqual(prop->ns->href, namespace))))) {
Owen Taylor3473f882001-02-23 17:55:21 +00004313 xmlChar *ret;
4314
4315 ret = xmlNodeListGetString(node->doc, prop->children, 1);
4316 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
4317 return(ret);
4318 }
4319 prop = prop->next;
4320 }
4321 if (!xmlCheckDTD) return(NULL);
4322
4323 /*
4324 * Check if there is a default declaration in the internal
4325 * or external subsets
4326 */
4327 doc = node->doc;
4328 if (doc != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004329 if (doc->intSubset != NULL) {
Daniel Veillard5792e162001-04-30 17:44:45 +00004330 xmlAttributePtr attrDecl;
4331
Owen Taylor3473f882001-02-23 17:55:21 +00004332 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
4333 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4334 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
4335
4336 if ((attrDecl != NULL) && (attrDecl->prefix != NULL)) {
4337 /*
4338 * The DTD declaration only allows a prefix search
4339 */
4340 ns = xmlSearchNs(doc, node, attrDecl->prefix);
4341 if ((ns != NULL) && (xmlStrEqual(ns->href, namespace)))
4342 return(xmlStrdup(attrDecl->defaultValue));
4343 }
4344 }
4345 }
4346 return(NULL);
4347}
4348
4349/**
4350 * xmlSetProp:
4351 * @node: the node
4352 * @name: the attribute name
4353 * @value: the attribute value
4354 *
4355 * Set (or reset) an attribute carried by a node.
4356 * Returns the attribute pointer.
4357 */
4358xmlAttrPtr
4359xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
4360 xmlAttrPtr prop = node->properties;
4361 xmlDocPtr doc = NULL;
4362
4363 if ((node == NULL) || (name == NULL))
4364 return(NULL);
4365 doc = node->doc;
4366 while (prop != NULL) {
Daniel Veillard75bea542001-05-11 17:41:21 +00004367 if ((xmlStrEqual(prop->name, name)) &&
4368 (prop->ns == NULL)){
Owen Taylor3473f882001-02-23 17:55:21 +00004369 if (prop->children != NULL)
4370 xmlFreeNodeList(prop->children);
4371 prop->children = NULL;
4372 prop->last = NULL;
4373 if (value != NULL) {
4374 xmlChar *buffer;
4375 xmlNodePtr tmp;
4376
4377 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
4378 prop->children = xmlStringGetNodeList(node->doc, buffer);
4379 prop->last = NULL;
4380 prop->doc = doc;
4381 tmp = prop->children;
4382 while (tmp != NULL) {
4383 tmp->parent = (xmlNodePtr) prop;
4384 tmp->doc = doc;
4385 if (tmp->next == NULL)
4386 prop->last = tmp;
4387 tmp = tmp->next;
4388 }
4389 xmlFree(buffer);
Daniel Veillard75bea542001-05-11 17:41:21 +00004390 }
Owen Taylor3473f882001-02-23 17:55:21 +00004391 return(prop);
4392 }
4393 prop = prop->next;
4394 }
4395 prop = xmlNewProp(node, name, value);
4396 return(prop);
4397}
4398
4399/**
Daniel Veillard75bea542001-05-11 17:41:21 +00004400 * xmlUnsetProp:
4401 * @node: the node
4402 * @name: the attribute name
4403 *
4404 * Remove an attribute carried by a node.
4405 * Returns 0 if successful, -1 if not found
4406 */
4407int
4408xmlUnsetProp(xmlNodePtr node, const xmlChar *name) {
4409 xmlAttrPtr prop = node->properties, prev = NULL;;
4410
4411 if ((node == NULL) || (name == NULL))
4412 return(-1);
4413 while (prop != NULL) {
4414 if ((xmlStrEqual(prop->name, name)) &&
4415 (prop->ns == NULL)) {
4416 if (prev == NULL)
4417 node->properties = prop->next;
4418 else
4419 prev->next = prop->next;
4420 xmlFreeProp(prop);
4421 return(0);
4422 }
4423 prev = prop;
4424 prop = prop->next;
4425 }
4426 return(-1);
4427}
4428
4429/**
Owen Taylor3473f882001-02-23 17:55:21 +00004430 * xmlSetNsProp:
4431 * @node: the node
4432 * @ns: the namespace definition
4433 * @name: the attribute name
4434 * @value: the attribute value
4435 *
4436 * Set (or reset) an attribute carried by a node.
4437 * The ns structure must be in scope, this is not checked.
4438 *
4439 * Returns the attribute pointer.
4440 */
4441xmlAttrPtr
4442xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
4443 const xmlChar *value) {
4444 xmlAttrPtr prop;
4445
4446 if ((node == NULL) || (name == NULL))
4447 return(NULL);
4448
4449 if (ns == NULL)
4450 return(xmlSetProp(node, name, value));
4451 if (ns->href == NULL)
4452 return(NULL);
4453 prop = node->properties;
4454
4455 while (prop != NULL) {
4456 /*
4457 * One need to have
4458 * - same attribute names
4459 * - and the attribute carrying that namespace
4460 * or
4461 * no namespace on the attribute and the element carrying it
4462 */
4463 if ((xmlStrEqual(prop->name, name)) &&
4464 (((prop->ns == NULL) && (node->ns != NULL) &&
4465 (xmlStrEqual(node->ns->href, ns->href))) ||
4466 ((prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))))) {
4467 if (prop->children != NULL)
4468 xmlFreeNodeList(prop->children);
4469 prop->children = NULL;
4470 prop->last = NULL;
4471 prop->ns = ns;
4472 if (value != NULL) {
4473 xmlChar *buffer;
4474 xmlNodePtr tmp;
4475
4476 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
4477 prop->children = xmlStringGetNodeList(node->doc, buffer);
4478 prop->last = NULL;
4479 tmp = prop->children;
4480 while (tmp != NULL) {
4481 tmp->parent = (xmlNodePtr) prop;
4482 if (tmp->next == NULL)
4483 prop->last = tmp;
4484 tmp = tmp->next;
4485 }
4486 xmlFree(buffer);
4487 }
4488 return(prop);
4489 }
4490 prop = prop->next;
4491 }
4492 prop = xmlNewNsProp(node, ns, name, value);
4493 return(prop);
4494}
4495
4496/**
Daniel Veillard75bea542001-05-11 17:41:21 +00004497 * xmlUnsetNsProp:
4498 * @node: the node
4499 * @ns: the namespace definition
4500 * @name: the attribute name
4501 *
4502 * Remove an attribute carried by a node.
4503 * Returns 0 if successful, -1 if not found
4504 */
4505int
4506xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) {
4507 xmlAttrPtr prop = node->properties, prev = NULL;;
4508
4509 if ((node == NULL) || (name == NULL))
4510 return(-1);
4511 if (ns == NULL)
4512 return(xmlUnsetProp(node, name));
4513 if (ns->href == NULL)
4514 return(-1);
4515 while (prop != NULL) {
4516 if ((xmlStrEqual(prop->name, name)) &&
4517 (((prop->ns == NULL) && (node->ns != NULL) &&
4518 (xmlStrEqual(node->ns->href, ns->href))) ||
4519 ((prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))))) {
4520 if (prev == NULL)
4521 node->properties = prop->next;
4522 else
4523 prev->next = prop->next;
4524 xmlFreeProp(prop);
4525 return(0);
4526 }
4527 prev = prop;
4528 prop = prop->next;
4529 }
4530 return(-1);
4531}
4532
4533/**
Owen Taylor3473f882001-02-23 17:55:21 +00004534 * xmlNodeIsText:
4535 * @node: the node
4536 *
4537 * Is this node a Text node ?
4538 * Returns 1 yes, 0 no
4539 */
4540int
4541xmlNodeIsText(xmlNodePtr node) {
4542 if (node == NULL) return(0);
4543
4544 if (node->type == XML_TEXT_NODE) return(1);
4545 return(0);
4546}
4547
4548/**
4549 * xmlIsBlankNode:
4550 * @node: the node
4551 *
4552 * Checks whether this node is an empty or whitespace only
4553 * (and possibly ignorable) text-node.
4554 *
4555 * Returns 1 yes, 0 no
4556 */
4557int
4558xmlIsBlankNode(xmlNodePtr node) {
4559 const xmlChar *cur;
4560 if (node == NULL) return(0);
4561
4562 if (node->type != XML_TEXT_NODE) return(0);
4563 if (node->content == NULL) return(1);
4564#ifndef XML_USE_BUFFER_CONTENT
4565 cur = node->content;
4566#else
4567 cur = xmlBufferContent(node->content);
4568#endif
4569 while (*cur != 0) {
4570 if (!IS_BLANK(*cur)) return(0);
4571 cur++;
4572 }
4573
4574 return(1);
4575}
4576
4577/**
4578 * xmlTextConcat:
4579 * @node: the node
4580 * @content: the content
4581 * @len: @content lenght
4582 *
4583 * Concat the given string at the end of the existing node content
4584 */
4585
4586void
4587xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) {
4588 if (node == NULL) return;
4589
4590 if ((node->type != XML_TEXT_NODE) &&
4591 (node->type != XML_CDATA_SECTION_NODE)) {
4592#ifdef DEBUG_TREE
4593 xmlGenericError(xmlGenericErrorContext,
4594 "xmlTextConcat: node is not text nor cdata\n");
4595#endif
4596 return;
4597 }
4598#ifndef XML_USE_BUFFER_CONTENT
4599 node->content = xmlStrncat(node->content, content, len);
4600#else
4601 xmlBufferAdd(node->content, content, len);
4602#endif
4603}
4604
4605/************************************************************************
4606 * *
4607 * Output : to a FILE or in memory *
4608 * *
4609 ************************************************************************/
4610
4611#define BASE_BUFFER_SIZE 4000
4612
Daniel Veillarde356c282001-03-10 12:32:04 +00004613int xmlDefaultBufferSize = BASE_BUFFER_SIZE;
4614
Owen Taylor3473f882001-02-23 17:55:21 +00004615/**
4616 * xmlBufferCreate:
4617 *
4618 * routine to create an XML buffer.
4619 * returns the new structure.
4620 */
4621xmlBufferPtr
4622xmlBufferCreate(void) {
4623 xmlBufferPtr ret;
4624
4625 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
4626 if (ret == NULL) {
4627 xmlGenericError(xmlGenericErrorContext,
4628 "xmlBufferCreate : out of memory!\n");
4629 return(NULL);
4630 }
4631 ret->use = 0;
Daniel Veillarde356c282001-03-10 12:32:04 +00004632 ret->size = xmlDefaultBufferSize;
Owen Taylor3473f882001-02-23 17:55:21 +00004633 ret->alloc = xmlBufferAllocScheme;
4634 ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar));
4635 if (ret->content == NULL) {
4636 xmlGenericError(xmlGenericErrorContext,
4637 "xmlBufferCreate : out of memory!\n");
4638 xmlFree(ret);
4639 return(NULL);
4640 }
4641 ret->content[0] = 0;
4642 return(ret);
4643}
4644
4645/**
4646 * xmlBufferCreateSize:
4647 * @size: initial size of buffer
4648 *
4649 * routine to create an XML buffer.
4650 * returns the new structure.
4651 */
4652xmlBufferPtr
4653xmlBufferCreateSize(size_t size) {
4654 xmlBufferPtr ret;
4655
4656 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
4657 if (ret == NULL) {
4658 xmlGenericError(xmlGenericErrorContext,
4659 "xmlBufferCreate : out of memory!\n");
4660 return(NULL);
4661 }
4662 ret->use = 0;
4663 ret->alloc = xmlBufferAllocScheme;
4664 ret->size = (size ? size+2 : 0); /* +1 for ending null */
4665 if (ret->size){
4666 ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar));
4667 if (ret->content == NULL) {
4668 xmlGenericError(xmlGenericErrorContext,
4669 "xmlBufferCreate : out of memory!\n");
4670 xmlFree(ret);
4671 return(NULL);
4672 }
4673 ret->content[0] = 0;
4674 } else
4675 ret->content = NULL;
4676 return(ret);
4677}
4678
4679/**
4680 * xmlBufferSetAllocationScheme:
4681 * @buf: the buffer to free
4682 * @scheme: allocation scheme to use
4683 *
4684 * Sets the allocation scheme for this buffer
4685 */
4686void
4687xmlBufferSetAllocationScheme(xmlBufferPtr buf,
4688 xmlBufferAllocationScheme scheme) {
4689 if (buf == NULL) {
4690#ifdef DEBUG_BUFFER
4691 xmlGenericError(xmlGenericErrorContext,
4692 "xmlBufferSetAllocationScheme: buf == NULL\n");
4693#endif
4694 return;
4695 }
4696
4697 buf->alloc = scheme;
4698}
4699
4700/**
4701 * xmlBufferFree:
4702 * @buf: the buffer to free
4703 *
4704 * Frees an XML buffer.
4705 */
4706void
4707xmlBufferFree(xmlBufferPtr buf) {
4708 if (buf == NULL) {
4709#ifdef DEBUG_BUFFER
4710 xmlGenericError(xmlGenericErrorContext,
4711 "xmlBufferFree: buf == NULL\n");
4712#endif
4713 return;
4714 }
4715 if (buf->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004716 xmlFree(buf->content);
4717 }
Owen Taylor3473f882001-02-23 17:55:21 +00004718 xmlFree(buf);
4719}
4720
4721/**
4722 * xmlBufferEmpty:
4723 * @buf: the buffer
4724 *
4725 * empty a buffer.
4726 */
4727void
4728xmlBufferEmpty(xmlBufferPtr buf) {
4729 if (buf->content == NULL) return;
4730 buf->use = 0;
Daniel Veillard92ad2102001-03-27 12:47:33 +00004731 memset(buf->content, 0, buf->size);
Owen Taylor3473f882001-02-23 17:55:21 +00004732}
4733
4734/**
4735 * xmlBufferShrink:
4736 * @buf: the buffer to dump
4737 * @len: the number of xmlChar to remove
4738 *
4739 * Remove the beginning of an XML buffer.
4740 *
4741 * Returns the number of xmlChar removed, or -1 in case of failure.
4742 */
4743int
4744xmlBufferShrink(xmlBufferPtr buf, unsigned int len) {
4745 if (len == 0) return(0);
4746 if (len > buf->use) return(-1);
4747
4748 buf->use -= len;
4749 memmove(buf->content, &buf->content[len], buf->use * sizeof(xmlChar));
4750
4751 buf->content[buf->use] = 0;
4752 return(len);
4753}
4754
4755/**
4756 * xmlBufferGrow:
4757 * @buf: the buffer
4758 * @len: the minimum free size to allocate
4759 *
4760 * Grow the available space of an XML buffer.
4761 *
4762 * Returns the new available space or -1 in case of error
4763 */
4764int
4765xmlBufferGrow(xmlBufferPtr buf, unsigned int len) {
4766 int size;
4767 xmlChar *newbuf;
4768
4769 if (len + buf->use < buf->size) return(0);
4770
4771 size = buf->use + len + 100;
4772
4773 newbuf = (xmlChar *) xmlRealloc(buf->content, size);
4774 if (newbuf == NULL) return(-1);
4775 buf->content = newbuf;
4776 buf->size = size;
4777 return(buf->size - buf->use);
4778}
4779
4780/**
4781 * xmlBufferDump:
4782 * @file: the file output
4783 * @buf: the buffer to dump
4784 *
4785 * Dumps an XML buffer to a FILE *.
4786 * Returns the number of xmlChar written
4787 */
4788int
4789xmlBufferDump(FILE *file, xmlBufferPtr buf) {
4790 int ret;
4791
4792 if (buf == NULL) {
4793#ifdef DEBUG_BUFFER
4794 xmlGenericError(xmlGenericErrorContext,
4795 "xmlBufferDump: buf == NULL\n");
4796#endif
4797 return(0);
4798 }
4799 if (buf->content == NULL) {
4800#ifdef DEBUG_BUFFER
4801 xmlGenericError(xmlGenericErrorContext,
4802 "xmlBufferDump: buf->content == NULL\n");
4803#endif
4804 return(0);
4805 }
4806 if (file == NULL) file = stdout;
4807 ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
4808 return(ret);
4809}
4810
4811/**
4812 * xmlBufferContent:
4813 * @buf: the buffer
4814 *
4815 * Returns the internal content
4816 */
4817
4818const xmlChar*
4819xmlBufferContent(const xmlBufferPtr buf)
4820{
4821 if(!buf)
4822 return NULL;
4823
4824 return buf->content;
4825}
4826
4827/**
4828 * xmlBufferLength:
4829 * @buf: the buffer
4830 *
4831 * Returns the length of data in the internal content
4832 */
4833
4834int
4835xmlBufferLength(const xmlBufferPtr buf)
4836{
4837 if(!buf)
4838 return 0;
4839
4840 return buf->use;
4841}
4842
4843/**
4844 * xmlBufferResize:
4845 * @buf: the buffer to resize
4846 * @size: the desired size
4847 *
4848 * Resize a buffer to accomodate minimum size of @size.
4849 *
4850 * Returns 0 in case of problems, 1 otherwise
4851 */
4852int
4853xmlBufferResize(xmlBufferPtr buf, unsigned int size)
4854{
4855 unsigned int newSize;
4856 xmlChar* rebuf = NULL;
4857
4858 /*take care of empty case*/
4859 newSize = (buf->size ? buf->size*2 : size);
4860
4861 /* Don't resize if we don't have to */
4862 if (size < buf->size)
4863 return 1;
4864
4865 /* figure out new size */
4866 switch (buf->alloc){
4867 case XML_BUFFER_ALLOC_DOUBLEIT:
4868 while (size > newSize) newSize *= 2;
4869 break;
4870 case XML_BUFFER_ALLOC_EXACT:
4871 newSize = size+10;
4872 break;
4873 default:
4874 newSize = size+10;
4875 break;
4876 }
4877
4878 if (buf->content == NULL)
4879 rebuf = (xmlChar *) xmlMalloc(newSize * sizeof(xmlChar));
4880 else
4881 rebuf = (xmlChar *) xmlRealloc(buf->content,
4882 newSize * sizeof(xmlChar));
4883 if (rebuf == NULL) {
4884 xmlGenericError(xmlGenericErrorContext,
4885 "xmlBufferAdd : out of memory!\n");
4886 return 0;
4887 }
4888 buf->content = rebuf;
4889 buf->size = newSize;
4890
4891 return 1;
4892}
4893
4894/**
4895 * xmlBufferAdd:
4896 * @buf: the buffer to dump
4897 * @str: the xmlChar string
4898 * @len: the number of xmlChar to add
4899 *
4900 * Add a string range to an XML buffer. if len == -1, the lenght of
4901 * str is recomputed.
4902 */
4903void
4904xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) {
4905 unsigned int needSize;
4906
4907 if (str == NULL) {
4908#ifdef DEBUG_BUFFER
4909 xmlGenericError(xmlGenericErrorContext,
4910 "xmlBufferAdd: str == NULL\n");
4911#endif
4912 return;
4913 }
4914 if (len < -1) {
4915#ifdef DEBUG_BUFFER
4916 xmlGenericError(xmlGenericErrorContext,
4917 "xmlBufferAdd: len < 0\n");
4918#endif
4919 return;
4920 }
4921 if (len == 0) return;
4922
4923 if (len < 0)
4924 len = xmlStrlen(str);
4925
4926 if (len <= 0) return;
4927
4928 needSize = buf->use + len + 2;
4929 if (needSize > buf->size){
4930 if (!xmlBufferResize(buf, needSize)){
4931 xmlGenericError(xmlGenericErrorContext,
4932 "xmlBufferAdd : out of memory!\n");
4933 return;
4934 }
4935 }
4936
4937 memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
4938 buf->use += len;
4939 buf->content[buf->use] = 0;
4940}
4941
4942/**
4943 * xmlBufferAddHead:
4944 * @buf: the buffer
4945 * @str: the xmlChar string
4946 * @len: the number of xmlChar to add
4947 *
4948 * Add a string range to the beginning of an XML buffer.
4949 * if len == -1, the lenght of @str is recomputed.
4950 */
4951void
4952xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) {
4953 unsigned int needSize;
4954
4955 if (str == NULL) {
4956#ifdef DEBUG_BUFFER
4957 xmlGenericError(xmlGenericErrorContext,
4958 "xmlBufferAdd: str == NULL\n");
4959#endif
4960 return;
4961 }
4962 if (len < -1) {
4963#ifdef DEBUG_BUFFER
4964 xmlGenericError(xmlGenericErrorContext,
4965 "xmlBufferAdd: len < 0\n");
4966#endif
4967 return;
4968 }
4969 if (len == 0) return;
4970
4971 if (len < 0)
4972 len = xmlStrlen(str);
4973
4974 if (len <= 0) return;
4975
4976 needSize = buf->use + len + 2;
4977 if (needSize > buf->size){
4978 if (!xmlBufferResize(buf, needSize)){
4979 xmlGenericError(xmlGenericErrorContext,
4980 "xmlBufferAddHead : out of memory!\n");
4981 return;
4982 }
4983 }
4984
4985 memmove(&buf->content[len], &buf->content[0], buf->use * sizeof(xmlChar));
4986 memmove(&buf->content[0], str, len * sizeof(xmlChar));
4987 buf->use += len;
4988 buf->content[buf->use] = 0;
4989}
4990
4991/**
4992 * xmlBufferCat:
4993 * @buf: the buffer to dump
4994 * @str: the xmlChar string
4995 *
4996 * Append a zero terminated string to an XML buffer.
4997 */
4998void
4999xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) {
5000 if (str != NULL)
5001 xmlBufferAdd(buf, str, -1);
5002}
5003
5004/**
5005 * xmlBufferCCat:
5006 * @buf: the buffer to dump
5007 * @str: the C char string
5008 *
5009 * Append a zero terminated C string to an XML buffer.
5010 */
5011void
5012xmlBufferCCat(xmlBufferPtr buf, const char *str) {
5013 const char *cur;
5014
5015 if (str == NULL) {
5016#ifdef DEBUG_BUFFER
5017 xmlGenericError(xmlGenericErrorContext,
5018 "xmlBufferAdd: str == NULL\n");
5019#endif
5020 return;
5021 }
5022 for (cur = str;*cur != 0;cur++) {
5023 if (buf->use + 10 >= buf->size) {
5024 if (!xmlBufferResize(buf, buf->use+10)){
5025 xmlGenericError(xmlGenericErrorContext,
5026 "xmlBufferCCat : out of memory!\n");
5027 return;
5028 }
5029 }
5030 buf->content[buf->use++] = *cur;
5031 }
5032 buf->content[buf->use] = 0;
5033}
5034
5035/**
5036 * xmlBufferWriteCHAR:
5037 * @buf: the XML buffer
5038 * @string: the string to add
5039 *
5040 * routine which manages and grows an output buffer. This one adds
5041 * xmlChars at the end of the buffer.
5042 */
5043void
5044#ifdef VMS
5045xmlBufferWriteXmlCHAR
5046#else
5047xmlBufferWriteCHAR
5048#endif
5049(xmlBufferPtr buf, const xmlChar *string) {
5050 xmlBufferCat(buf, string);
5051}
5052
5053/**
5054 * xmlBufferWriteChar:
5055 * @buf: the XML buffer output
5056 * @string: the string to add
5057 *
5058 * routine which manage and grows an output buffer. This one add
5059 * C chars at the end of the array.
5060 */
5061void
5062xmlBufferWriteChar(xmlBufferPtr buf, const char *string) {
5063 xmlBufferCCat(buf, string);
5064}
5065
5066
5067/**
5068 * xmlBufferWriteQuotedString:
5069 * @buf: the XML buffer output
5070 * @string: the string to add
5071 *
5072 * routine which manage and grows an output buffer. This one writes
5073 * a quoted or double quoted xmlChar string, checking first if it holds
5074 * quote or double-quotes internally
5075 */
5076void
5077xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) {
5078 if (xmlStrchr(string, '"')) {
5079 if (xmlStrchr(string, '\'')) {
5080#ifdef DEBUG_BUFFER
5081 xmlGenericError(xmlGenericErrorContext,
5082 "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n");
5083#endif
5084 }
5085 xmlBufferCCat(buf, "'");
5086 xmlBufferCat(buf, string);
5087 xmlBufferCCat(buf, "'");
5088 } else {
5089 xmlBufferCCat(buf, "\"");
5090 xmlBufferCat(buf, string);
5091 xmlBufferCCat(buf, "\"");
5092 }
5093}
5094
5095
5096/************************************************************************
5097 * *
5098 * Dumping XML tree content to a simple buffer *
5099 * *
5100 ************************************************************************/
5101
5102void
5103xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
5104 int format);
5105static void
5106xmlNodeListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
5107 int format);
5108void
5109htmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur);
5110
5111/**
5112 * xmlNsDump:
5113 * @buf: the XML buffer output
5114 * @cur: a namespace
5115 *
5116 * Dump a local Namespace definition.
5117 * Should be called in the context of attributes dumps.
5118 */
5119static void
5120xmlNsDump(xmlBufferPtr buf, xmlNsPtr cur) {
5121 if (cur == NULL) {
5122#ifdef DEBUG_TREE
5123 xmlGenericError(xmlGenericErrorContext,
5124 "xmlNsDump : Ns == NULL\n");
5125#endif
5126 return;
5127 }
5128 if (cur->type == XML_LOCAL_NAMESPACE) {
5129 /* Within the context of an element attributes */
5130 if (cur->prefix != NULL) {
5131 xmlBufferWriteChar(buf, " xmlns:");
5132 xmlBufferWriteCHAR(buf, cur->prefix);
5133 } else
5134 xmlBufferWriteChar(buf, " xmlns");
5135 xmlBufferWriteChar(buf, "=");
5136 xmlBufferWriteQuotedString(buf, cur->href);
5137 }
5138}
5139
5140/**
5141 * xmlNsListDump:
5142 * @buf: the XML buffer output
5143 * @cur: the first namespace
5144 *
5145 * Dump a list of local Namespace definitions.
5146 * Should be called in the context of attributes dumps.
5147 */
5148static void
5149xmlNsListDump(xmlBufferPtr buf, xmlNsPtr cur) {
5150 while (cur != NULL) {
5151 xmlNsDump(buf, cur);
5152 cur = cur->next;
5153 }
5154}
5155
5156/**
5157 * xmlDtdDump:
5158 * @buf: the XML buffer output
5159 * @doc: the document
5160 *
5161 * Dump the XML document DTD, if any.
5162 */
5163static void
5164xmlDtdDump(xmlBufferPtr buf, xmlDtdPtr dtd) {
5165 if (dtd == NULL) {
5166#ifdef DEBUG_TREE
5167 xmlGenericError(xmlGenericErrorContext,
5168 "xmlDtdDump : no internal subset\n");
5169#endif
5170 return;
5171 }
5172 xmlBufferWriteChar(buf, "<!DOCTYPE ");
5173 xmlBufferWriteCHAR(buf, dtd->name);
5174 if (dtd->ExternalID != NULL) {
5175 xmlBufferWriteChar(buf, " PUBLIC ");
5176 xmlBufferWriteQuotedString(buf, dtd->ExternalID);
5177 xmlBufferWriteChar(buf, " ");
5178 xmlBufferWriteQuotedString(buf, dtd->SystemID);
5179 } else if (dtd->SystemID != NULL) {
5180 xmlBufferWriteChar(buf, " SYSTEM ");
5181 xmlBufferWriteQuotedString(buf, dtd->SystemID);
5182 }
5183 if ((dtd->entities == NULL) && (dtd->elements == NULL) &&
5184 (dtd->attributes == NULL) && (dtd->notations == NULL)) {
5185 xmlBufferWriteChar(buf, ">");
5186 return;
5187 }
5188 xmlBufferWriteChar(buf, " [\n");
5189 xmlNodeListDump(buf, dtd->doc, dtd->children, -1, 0);
5190#if 0
5191 if (dtd->entities != NULL)
5192 xmlDumpEntitiesTable(buf, (xmlEntitiesTablePtr) dtd->entities);
5193 if (dtd->notations != NULL)
5194 xmlDumpNotationTable(buf, (xmlNotationTablePtr) dtd->notations);
5195 if (dtd->elements != NULL)
5196 xmlDumpElementTable(buf, (xmlElementTablePtr) dtd->elements);
5197 if (dtd->attributes != NULL)
5198 xmlDumpAttributeTable(buf, (xmlAttributeTablePtr) dtd->attributes);
5199#endif
5200 xmlBufferWriteChar(buf, "]>");
5201}
5202
5203/**
5204 * xmlAttrDump:
5205 * @buf: the XML buffer output
5206 * @doc: the document
5207 * @cur: the attribute pointer
5208 *
5209 * Dump an XML attribute
5210 */
5211static void
5212xmlAttrDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) {
5213 xmlChar *value;
5214
5215 if (cur == NULL) {
5216#ifdef DEBUG_TREE
5217 xmlGenericError(xmlGenericErrorContext,
5218 "xmlAttrDump : property == NULL\n");
5219#endif
5220 return;
5221 }
5222 xmlBufferWriteChar(buf, " ");
5223 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
5224 xmlBufferWriteCHAR(buf, cur->ns->prefix);
5225 xmlBufferWriteChar(buf, ":");
5226 }
5227 xmlBufferWriteCHAR(buf, cur->name);
5228 value = xmlNodeListGetString(doc, cur->children, 0);
5229 if (value != NULL) {
5230 xmlBufferWriteChar(buf, "=");
5231 xmlBufferWriteQuotedString(buf, value);
5232 xmlFree(value);
5233 } else {
5234 xmlBufferWriteChar(buf, "=\"\"");
5235 }
5236}
5237
5238/**
5239 * xmlAttrListDump:
5240 * @buf: the XML buffer output
5241 * @doc: the document
5242 * @cur: the first attribute pointer
5243 *
5244 * Dump a list of XML attributes
5245 */
5246static void
5247xmlAttrListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) {
5248 if (cur == NULL) {
5249#ifdef DEBUG_TREE
5250 xmlGenericError(xmlGenericErrorContext,
5251 "xmlAttrListDump : property == NULL\n");
5252#endif
5253 return;
5254 }
5255 while (cur != NULL) {
5256 xmlAttrDump(buf, doc, cur);
5257 cur = cur->next;
5258 }
5259}
5260
5261
5262
5263/**
5264 * xmlNodeListDump:
5265 * @buf: the XML buffer output
5266 * @doc: the document
5267 * @cur: the first node
5268 * @level: the imbrication level for indenting
5269 * @format: is formatting allowed
5270 *
5271 * Dump an XML node list, recursive behaviour,children are printed too.
5272 */
5273static void
5274xmlNodeListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
5275 int format) {
5276 int i;
5277
5278 if (cur == NULL) {
5279#ifdef DEBUG_TREE
5280 xmlGenericError(xmlGenericErrorContext,
5281 "xmlNodeListDump : node == NULL\n");
5282#endif
5283 return;
5284 }
5285 while (cur != NULL) {
5286 if ((format) && (xmlIndentTreeOutput) &&
5287 (cur->type == XML_ELEMENT_NODE))
5288 for (i = 0;i < level;i++)
5289 xmlBufferWriteChar(buf, " ");
5290 xmlNodeDump(buf, doc, cur, level, format);
5291 if (format) {
5292 xmlBufferWriteChar(buf, "\n");
5293 }
5294 cur = cur->next;
5295 }
5296}
5297
5298/**
5299 * xmlNodeDump:
5300 * @buf: the XML buffer output
5301 * @doc: the document
5302 * @cur: the current node
5303 * @level: the imbrication level for indenting
5304 * @format: is formatting allowed
5305 *
5306 * Dump an XML node, recursive behaviour,children are printed too.
5307 */
5308void
5309xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
5310 int format) {
5311 int i;
5312 xmlNodePtr tmp;
5313
5314 if (cur == NULL) {
5315#ifdef DEBUG_TREE
5316 xmlGenericError(xmlGenericErrorContext,
5317 "xmlNodeDump : node == NULL\n");
5318#endif
5319 return;
5320 }
5321 if (cur->type == XML_XINCLUDE_START)
5322 return;
5323 if (cur->type == XML_XINCLUDE_END)
5324 return;
5325 if (cur->type == XML_DTD_NODE) {
5326 xmlDtdDump(buf, (xmlDtdPtr) cur);
5327 return;
5328 }
5329 if (cur->type == XML_ELEMENT_DECL) {
5330 xmlDumpElementDecl(buf, (xmlElementPtr) cur);
5331 return;
5332 }
5333 if (cur->type == XML_ATTRIBUTE_DECL) {
5334 xmlDumpAttributeDecl(buf, (xmlAttributePtr) cur);
5335 return;
5336 }
5337 if (cur->type == XML_ENTITY_DECL) {
5338 xmlDumpEntityDecl(buf, (xmlEntityPtr) cur);
5339 return;
5340 }
5341 if (cur->type == XML_TEXT_NODE) {
5342 if (cur->content != NULL) {
5343 if ((cur->name == xmlStringText) ||
5344 (cur->name != xmlStringTextNoenc)) {
5345 xmlChar *buffer;
5346
5347#ifndef XML_USE_BUFFER_CONTENT
5348 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
5349#else
5350 buffer = xmlEncodeEntitiesReentrant(doc,
5351 xmlBufferContent(cur->content));
5352#endif
5353 if (buffer != NULL) {
5354 xmlBufferWriteCHAR(buf, buffer);
5355 xmlFree(buffer);
5356 }
5357 } else {
5358 /*
5359 * Disable escaping, needed for XSLT
5360 */
5361#ifndef XML_USE_BUFFER_CONTENT
5362 xmlBufferWriteCHAR(buf, cur->content);
5363#else
5364 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
5365#endif
5366 }
5367 }
5368 return;
5369 }
5370 if (cur->type == XML_PI_NODE) {
5371 if (cur->content != NULL) {
5372 xmlBufferWriteChar(buf, "<?");
5373 xmlBufferWriteCHAR(buf, cur->name);
5374 if (cur->content != NULL) {
5375 xmlBufferWriteChar(buf, " ");
5376#ifndef XML_USE_BUFFER_CONTENT
5377 xmlBufferWriteCHAR(buf, cur->content);
5378#else
5379 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
5380#endif
5381 }
5382 xmlBufferWriteChar(buf, "?>");
5383 } else {
5384 xmlBufferWriteChar(buf, "<?");
5385 xmlBufferWriteCHAR(buf, cur->name);
5386 xmlBufferWriteChar(buf, "?>");
5387 }
5388 return;
5389 }
5390 if (cur->type == XML_COMMENT_NODE) {
5391 if (cur->content != NULL) {
5392 xmlBufferWriteChar(buf, "<!--");
5393#ifndef XML_USE_BUFFER_CONTENT
5394 xmlBufferWriteCHAR(buf, cur->content);
5395#else
5396 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
5397#endif
5398 xmlBufferWriteChar(buf, "-->");
5399 }
5400 return;
5401 }
5402 if (cur->type == XML_ENTITY_REF_NODE) {
5403 xmlBufferWriteChar(buf, "&");
5404 xmlBufferWriteCHAR(buf, cur->name);
5405 xmlBufferWriteChar(buf, ";");
5406 return;
5407 }
5408 if (cur->type == XML_CDATA_SECTION_NODE) {
5409 xmlBufferWriteChar(buf, "<![CDATA[");
5410 if (cur->content != NULL)
5411#ifndef XML_USE_BUFFER_CONTENT
5412 xmlBufferWriteCHAR(buf, cur->content);
5413#else
5414 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
5415#endif
5416 xmlBufferWriteChar(buf, "]]>");
5417 return;
5418 }
5419
5420 if (format == 1) {
5421 tmp = cur->children;
5422 while (tmp != NULL) {
5423 if ((tmp->type == XML_TEXT_NODE) ||
5424 (tmp->type == XML_ENTITY_REF_NODE)) {
5425 format = 0;
5426 break;
5427 }
5428 tmp = tmp->next;
5429 }
5430 }
5431 xmlBufferWriteChar(buf, "<");
5432 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
5433 xmlBufferWriteCHAR(buf, cur->ns->prefix);
5434 xmlBufferWriteChar(buf, ":");
5435 }
5436
5437 xmlBufferWriteCHAR(buf, cur->name);
5438 if (cur->nsDef)
5439 xmlNsListDump(buf, cur->nsDef);
5440 if (cur->properties != NULL)
5441 xmlAttrListDump(buf, doc, cur->properties);
5442
5443 if ((cur->content == NULL) && (cur->children == NULL) &&
5444 (!xmlSaveNoEmptyTags)) {
5445 xmlBufferWriteChar(buf, "/>");
5446 return;
5447 }
5448 xmlBufferWriteChar(buf, ">");
5449 if (cur->content != NULL) {
5450 xmlChar *buffer;
5451
5452#ifndef XML_USE_BUFFER_CONTENT
5453 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
5454#else
5455 buffer = xmlEncodeEntitiesReentrant(doc,
5456 xmlBufferContent(cur->content));
5457#endif
5458 if (buffer != NULL) {
5459 xmlBufferWriteCHAR(buf, buffer);
5460 xmlFree(buffer);
5461 }
5462 }
5463 if (cur->children != NULL) {
5464 if (format) xmlBufferWriteChar(buf, "\n");
5465 xmlNodeListDump(buf, doc, cur->children,
5466 (level >= 0?level+1:-1), format);
5467 if ((xmlIndentTreeOutput) && (format))
5468 for (i = 0;i < level;i++)
5469 xmlBufferWriteChar(buf, " ");
5470 }
5471 xmlBufferWriteChar(buf, "</");
5472 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
5473 xmlBufferWriteCHAR(buf, cur->ns->prefix);
5474 xmlBufferWriteChar(buf, ":");
5475 }
5476
5477 xmlBufferWriteCHAR(buf, cur->name);
5478 xmlBufferWriteChar(buf, ">");
5479}
5480
5481/**
5482 * xmlElemDump:
5483 * @f: the FILE * for the output
5484 * @doc: the document
5485 * @cur: the current node
5486 *
5487 * Dump an XML/HTML node, recursive behaviour,children are printed too.
5488 */
5489void
5490xmlElemDump(FILE *f, xmlDocPtr doc, xmlNodePtr cur) {
5491 xmlBufferPtr buf;
5492
5493 if (cur == NULL) {
5494#ifdef DEBUG_TREE
5495 xmlGenericError(xmlGenericErrorContext,
5496 "xmlElemDump : cur == NULL\n");
5497#endif
5498 return;
5499 }
5500 if (doc == NULL) {
5501#ifdef DEBUG_TREE
5502 xmlGenericError(xmlGenericErrorContext,
5503 "xmlElemDump : doc == NULL\n");
5504#endif
5505 }
5506 buf = xmlBufferCreate();
5507 if (buf == NULL) return;
5508 if ((doc != NULL) &&
5509 (doc->type == XML_HTML_DOCUMENT_NODE)) {
5510#ifdef LIBXML_HTML_ENABLED
5511 htmlNodeDump(buf, doc, cur);
5512#else
5513 xmlGenericError(xmlGenericErrorContext,
5514 "HTML support not compiled in\n");
5515#endif /* LIBXML_HTML_ENABLED */
5516 } else
5517 xmlNodeDump(buf, doc, cur, 0, 1);
5518 xmlBufferDump(f, buf);
5519 xmlBufferFree(buf);
5520}
5521
5522/************************************************************************
5523 * *
5524 * Dumping XML tree content to an I/O output buffer *
5525 * *
5526 ************************************************************************/
5527
5528void
5529xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
5530 int level, int format, const char *encoding);
5531static void
5532xmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
5533 int level, int format, const char *encoding);
5534/**
5535 * xmlNsDumpOutput:
5536 * @buf: the XML buffer output
5537 * @cur: a namespace
5538 *
5539 * Dump a local Namespace definition.
5540 * Should be called in the context of attributes dumps.
5541 */
5542static void
5543xmlNsDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) {
5544 if (cur == NULL) {
5545#ifdef DEBUG_TREE
5546 xmlGenericError(xmlGenericErrorContext,
5547 "xmlNsDump : Ns == NULL\n");
5548#endif
5549 return;
5550 }
5551 if ((cur->type == XML_LOCAL_NAMESPACE) && (cur->href != NULL)) {
5552 /* Within the context of an element attributes */
5553 if (cur->prefix != NULL) {
5554 xmlOutputBufferWriteString(buf, " xmlns:");
5555 xmlOutputBufferWriteString(buf, (const char *)cur->prefix);
5556 } else
5557 xmlOutputBufferWriteString(buf, " xmlns");
5558 xmlOutputBufferWriteString(buf, "=");
5559 xmlBufferWriteQuotedString(buf->buffer, cur->href);
5560 }
5561}
5562
5563/**
5564 * xmlNsListDumpOutput:
5565 * @buf: the XML buffer output
5566 * @cur: the first namespace
5567 *
5568 * Dump a list of local Namespace definitions.
5569 * Should be called in the context of attributes dumps.
5570 */
5571static void
5572xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) {
5573 while (cur != NULL) {
5574 xmlNsDumpOutput(buf, cur);
5575 cur = cur->next;
5576 }
5577}
5578
5579/**
5580 * xmlDtdDumpOutput:
5581 * @buf: the XML buffer output
5582 * @doc: the document
5583 * @encoding: an optional encoding string
5584 *
5585 * Dump the XML document DTD, if any.
5586 */
5587static void
5588xmlDtdDumpOutput(xmlOutputBufferPtr buf, xmlDtdPtr dtd, const char *encoding) {
5589 if (dtd == NULL) {
5590#ifdef DEBUG_TREE
5591 xmlGenericError(xmlGenericErrorContext,
5592 "xmlDtdDump : no internal subset\n");
5593#endif
5594 return;
5595 }
5596 xmlOutputBufferWriteString(buf, "<!DOCTYPE ");
5597 xmlOutputBufferWriteString(buf, (const char *)dtd->name);
5598 if (dtd->ExternalID != NULL) {
5599 xmlOutputBufferWriteString(buf, " PUBLIC ");
5600 xmlBufferWriteQuotedString(buf->buffer, dtd->ExternalID);
5601 xmlOutputBufferWriteString(buf, " ");
5602 xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID);
5603 } else if (dtd->SystemID != NULL) {
5604 xmlOutputBufferWriteString(buf, " SYSTEM ");
5605 xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID);
5606 }
5607 if ((dtd->entities == NULL) && (dtd->elements == NULL) &&
5608 (dtd->attributes == NULL) && (dtd->notations == NULL)) {
5609 xmlOutputBufferWriteString(buf, ">");
5610 return;
5611 }
5612 xmlOutputBufferWriteString(buf, " [\n");
5613 xmlNodeListDumpOutput(buf, dtd->doc, dtd->children, -1, 0, encoding);
5614 xmlOutputBufferWriteString(buf, "]>");
5615}
5616
5617/**
5618 * xmlAttrDumpOutput:
5619 * @buf: the XML buffer output
5620 * @doc: the document
5621 * @cur: the attribute pointer
5622 * @encoding: an optional encoding string
5623 *
5624 * Dump an XML attribute
5625 */
5626static void
5627xmlAttrDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00005628 const char *encoding ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00005629 xmlChar *value;
5630
5631 if (cur == NULL) {
5632#ifdef DEBUG_TREE
5633 xmlGenericError(xmlGenericErrorContext,
5634 "xmlAttrDump : property == NULL\n");
5635#endif
5636 return;
5637 }
5638 xmlOutputBufferWriteString(buf, " ");
5639 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
5640 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
5641 xmlOutputBufferWriteString(buf, ":");
5642 }
5643 xmlOutputBufferWriteString(buf, (const char *)cur->name);
5644 value = xmlNodeListGetString(doc, cur->children, 0);
5645 if (value) {
5646 xmlOutputBufferWriteString(buf, "=");
5647 xmlBufferWriteQuotedString(buf->buffer, value);
5648 xmlFree(value);
5649 } else {
5650 xmlOutputBufferWriteString(buf, "=\"\"");
5651 }
5652}
5653
5654/**
5655 * xmlAttrListDumpOutput:
5656 * @buf: the XML buffer output
5657 * @doc: the document
5658 * @cur: the first attribute pointer
5659 * @encoding: an optional encoding string
5660 *
5661 * Dump a list of XML attributes
5662 */
5663static void
5664xmlAttrListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
5665 xmlAttrPtr cur, const char *encoding) {
5666 if (cur == NULL) {
5667#ifdef DEBUG_TREE
5668 xmlGenericError(xmlGenericErrorContext,
5669 "xmlAttrListDump : property == NULL\n");
5670#endif
5671 return;
5672 }
5673 while (cur != NULL) {
5674 xmlAttrDumpOutput(buf, doc, cur, encoding);
5675 cur = cur->next;
5676 }
5677}
5678
5679
5680
5681/**
5682 * xmlNodeListDumpOutput:
5683 * @buf: the XML buffer output
5684 * @doc: the document
5685 * @cur: the first node
5686 * @level: the imbrication level for indenting
5687 * @format: is formatting allowed
5688 * @encoding: an optional encoding string
5689 *
5690 * Dump an XML node list, recursive behaviour,children are printed too.
5691 */
5692static void
5693xmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
5694 xmlNodePtr cur, int level, int format, const char *encoding) {
5695 int i;
5696
5697 if (cur == NULL) {
5698#ifdef DEBUG_TREE
5699 xmlGenericError(xmlGenericErrorContext,
5700 "xmlNodeListDump : node == NULL\n");
5701#endif
5702 return;
5703 }
5704 while (cur != NULL) {
5705 if ((format) && (xmlIndentTreeOutput) &&
5706 (cur->type == XML_ELEMENT_NODE))
5707 for (i = 0;i < level;i++)
5708 xmlOutputBufferWriteString(buf, " ");
5709 xmlNodeDumpOutput(buf, doc, cur, level, format, encoding);
5710 if (format) {
5711 xmlOutputBufferWriteString(buf, "\n");
5712 }
5713 cur = cur->next;
5714 }
5715}
5716
5717/**
5718 * xmlNodeDumpOutput:
5719 * @buf: the XML buffer output
5720 * @doc: the document
5721 * @cur: the current node
5722 * @level: the imbrication level for indenting
5723 * @format: is formatting allowed
5724 * @encoding: an optional encoding string
5725 *
5726 * Dump an XML node, recursive behaviour,children are printed too.
5727 */
5728void
5729xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
5730 int level, int format, const char *encoding) {
5731 int i;
5732 xmlNodePtr tmp;
5733
5734 if (cur == NULL) {
5735#ifdef DEBUG_TREE
5736 xmlGenericError(xmlGenericErrorContext,
5737 "xmlNodeDump : node == NULL\n");
5738#endif
5739 return;
5740 }
5741 if (cur->type == XML_XINCLUDE_START)
5742 return;
5743 if (cur->type == XML_XINCLUDE_END)
5744 return;
5745 if (cur->type == XML_DTD_NODE) {
5746 xmlDtdDumpOutput(buf, (xmlDtdPtr) cur, encoding);
5747 return;
5748 }
5749 if (cur->type == XML_ELEMENT_DECL) {
5750 xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur);
5751 return;
5752 }
5753 if (cur->type == XML_ATTRIBUTE_DECL) {
5754 xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur);
5755 return;
5756 }
5757 if (cur->type == XML_ENTITY_DECL) {
5758 xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur);
5759 return;
5760 }
5761 if (cur->type == XML_TEXT_NODE) {
5762 if (cur->content != NULL) {
5763 if ((cur->name == xmlStringText) ||
5764 (cur->name != xmlStringTextNoenc)) {
5765 xmlChar *buffer;
5766
5767#ifndef XML_USE_BUFFER_CONTENT
5768 if (encoding == NULL)
5769 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
5770 else
5771 buffer = xmlEncodeSpecialChars(doc, cur->content);
5772#else
5773 if (encoding == NULL)
5774 buffer = xmlEncodeEntitiesReentrant(doc,
5775 xmlBufferContent(cur->content));
5776 else
5777 buffer = xmlEncodeSpecialChars(doc,
5778 xmlBufferContent(cur->content));
5779#endif
5780 if (buffer != NULL) {
5781 xmlOutputBufferWriteString(buf, (const char *)buffer);
5782 xmlFree(buffer);
5783 }
5784 } else {
5785 /*
5786 * Disable escaping, needed for XSLT
5787 */
5788#ifndef XML_USE_BUFFER_CONTENT
5789 xmlOutputBufferWriteString(buf, (const char *) cur->content);
5790#else
5791 xmlOutputBufferWriteString(buf, xmlBufferContent(cur->content));
5792#endif
5793 }
5794 }
5795
5796 return;
5797 }
5798 if (cur->type == XML_PI_NODE) {
5799 if (cur->content != NULL) {
5800 xmlOutputBufferWriteString(buf, "<?");
5801 xmlOutputBufferWriteString(buf, (const char *)cur->name);
5802 if (cur->content != NULL) {
5803 xmlOutputBufferWriteString(buf, " ");
5804#ifndef XML_USE_BUFFER_CONTENT
5805 xmlOutputBufferWriteString(buf, (const char *)cur->content);
5806#else
5807 xmlOutputBufferWriteString(buf, (const char *)xmlBufferContent(cur->content));
5808#endif
5809 }
5810 xmlOutputBufferWriteString(buf, "?>");
5811 } else {
5812 xmlOutputBufferWriteString(buf, "<?");
5813 xmlOutputBufferWriteString(buf, (const char *)cur->name);
5814 xmlOutputBufferWriteString(buf, "?>");
5815 }
5816 return;
5817 }
5818 if (cur->type == XML_COMMENT_NODE) {
5819 if (cur->content != NULL) {
5820 xmlOutputBufferWriteString(buf, "<!--");
5821#ifndef XML_USE_BUFFER_CONTENT
5822 xmlOutputBufferWriteString(buf, (const char *)cur->content);
5823#else
5824 xmlOutputBufferWriteString(buf, (const char *)xmlBufferContent(cur->content));
5825#endif
5826 xmlOutputBufferWriteString(buf, "-->");
5827 }
5828 return;
5829 }
5830 if (cur->type == XML_ENTITY_REF_NODE) {
5831 xmlOutputBufferWriteString(buf, "&");
5832 xmlOutputBufferWriteString(buf, (const char *)cur->name);
5833 xmlOutputBufferWriteString(buf, ";");
5834 return;
5835 }
5836 if (cur->type == XML_CDATA_SECTION_NODE) {
5837 xmlOutputBufferWriteString(buf, "<![CDATA[");
5838 if (cur->content != NULL)
5839#ifndef XML_USE_BUFFER_CONTENT
5840 xmlOutputBufferWriteString(buf, (const char *)cur->content);
5841#else
5842 xmlOutputBufferWriteString(buf, (const char *)xmlBufferContent(cur->content));
5843#endif
5844 xmlOutputBufferWriteString(buf, "]]>");
5845 return;
5846 }
5847
5848 if (format == 1) {
5849 tmp = cur->children;
5850 while (tmp != NULL) {
5851 if ((tmp->type == XML_TEXT_NODE) ||
5852 (tmp->type == XML_ENTITY_REF_NODE)) {
5853 format = 0;
5854 break;
5855 }
5856 tmp = tmp->next;
5857 }
5858 }
5859 xmlOutputBufferWriteString(buf, "<");
5860 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
5861 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
5862 xmlOutputBufferWriteString(buf, ":");
5863 }
5864
5865 xmlOutputBufferWriteString(buf, (const char *)cur->name);
5866 if (cur->nsDef)
5867 xmlNsListDumpOutput(buf, cur->nsDef);
5868 if (cur->properties != NULL)
5869 xmlAttrListDumpOutput(buf, doc, cur->properties, encoding);
5870
5871 if ((cur->content == NULL) && (cur->children == NULL) &&
5872 (!xmlSaveNoEmptyTags)) {
5873 xmlOutputBufferWriteString(buf, "/>");
5874 return;
5875 }
5876 xmlOutputBufferWriteString(buf, ">");
5877 if (cur->content != NULL) {
5878 xmlChar *buffer;
5879
5880#ifndef XML_USE_BUFFER_CONTENT
5881 if (encoding == NULL)
5882 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
5883 else
5884 buffer = xmlEncodeSpecialChars(doc, cur->content);
5885#else
5886 if (encoding == NULL)
5887 buffer = xmlEncodeEntitiesReentrant(doc,
5888 xmlBufferContent(cur->content));
5889 else
5890 buffer = xmlEncodeSpecialChars(doc,
5891 xmlBufferContent(cur->content));
5892#endif
5893 if (buffer != NULL) {
5894 xmlOutputBufferWriteString(buf, (const char *)buffer);
5895 xmlFree(buffer);
5896 }
5897 }
5898 if (cur->children != NULL) {
5899 if (format) xmlOutputBufferWriteString(buf, "\n");
5900 xmlNodeListDumpOutput(buf, doc, cur->children,
5901 (level >= 0?level+1:-1), format, encoding);
5902 if ((xmlIndentTreeOutput) && (format))
5903 for (i = 0;i < level;i++)
5904 xmlOutputBufferWriteString(buf, " ");
5905 }
5906 xmlOutputBufferWriteString(buf, "</");
5907 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
5908 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
5909 xmlOutputBufferWriteString(buf, ":");
5910 }
5911
5912 xmlOutputBufferWriteString(buf, (const char *)cur->name);
5913 xmlOutputBufferWriteString(buf, ">");
5914}
5915
5916/**
5917 * xmlDocContentDumpOutput:
5918 * @buf: the XML buffer output
5919 * @cur: the document
5920 * @encoding: an optional encoding string
5921 * @format: should formatting spaces been added
5922 *
5923 * Dump an XML document.
5924 */
5925static void
5926xmlDocContentDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr cur,
5927 const char *encoding, int format) {
5928 xmlOutputBufferWriteString(buf, "<?xml version=");
5929 if (cur->version != NULL)
5930 xmlBufferWriteQuotedString(buf->buffer, cur->version);
5931 else
5932 xmlOutputBufferWriteString(buf, "\"1.0\"");
5933 if (encoding == NULL) {
5934 if (cur->encoding != NULL)
5935 encoding = (const char *) cur->encoding;
5936 else if (cur->charset != XML_CHAR_ENCODING_UTF8)
5937 encoding = xmlGetCharEncodingName((xmlCharEncoding) cur->charset);
5938 }
5939 if (encoding != NULL) {
5940 xmlOutputBufferWriteString(buf, " encoding=");
5941 xmlBufferWriteQuotedString(buf->buffer, (xmlChar *) encoding);
5942 }
5943 switch (cur->standalone) {
5944 case 0:
5945 xmlOutputBufferWriteString(buf, " standalone=\"no\"");
5946 break;
5947 case 1:
5948 xmlOutputBufferWriteString(buf, " standalone=\"yes\"");
5949 break;
5950 }
5951 xmlOutputBufferWriteString(buf, "?>\n");
5952 if (cur->children != NULL) {
5953 xmlNodePtr child = cur->children;
5954
5955 while (child != NULL) {
5956 xmlNodeDumpOutput(buf, cur, child, 0, format, encoding);
5957 xmlOutputBufferWriteString(buf, "\n");
5958 child = child->next;
5959 }
5960 }
5961}
5962
5963/************************************************************************
5964 * *
5965 * Saving functions front-ends *
5966 * *
5967 ************************************************************************/
5968
5969/**
5970 * xmlDocDumpMemoryEnc:
5971 * @out_doc: Document to generate XML text from
5972 * @doc_txt_ptr: Memory pointer for allocated XML text
5973 * @doc_txt_len: Length of the generated XML text
5974 * @txt_encoding: Character encoding to use when generating XML text
5975 * @format: should formatting spaces been added
5976 *
5977 * Dump the current DOM tree into memory using the character encoding specified
5978 * by the caller. Note it is up to the caller of this function to free the
5979 * allocated memory.
5980 */
5981
5982void
5983xmlDocDumpFormatMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005984 int * doc_txt_len, const char * txt_encoding,
Daniel Veillard1731d6a2001-04-10 16:38:06 +00005985 int format) {
Owen Taylor3473f882001-02-23 17:55:21 +00005986 int dummy = 0;
5987
5988 xmlCharEncoding doc_charset;
5989 xmlOutputBufferPtr out_buff = NULL;
5990 xmlCharEncodingHandlerPtr conv_hdlr = NULL;
5991
5992 if (doc_txt_len == NULL) {
5993 doc_txt_len = &dummy; /* Continue, caller just won't get length */
5994 }
5995
5996 if (doc_txt_ptr == NULL) {
5997 *doc_txt_len = 0;
5998 xmlGenericError(xmlGenericErrorContext,
5999 "xmlDocDumpFormatMemoryEnc: Null return buffer pointer.");
6000 return;
6001 }
6002
6003 *doc_txt_ptr = NULL;
6004 *doc_txt_len = 0;
6005
6006 if (out_doc == NULL) {
6007 /* No document, no output */
6008 xmlGenericError(xmlGenericErrorContext,
6009 "xmlDocDumpFormatMemoryEnc: Null DOM tree document pointer.\n");
6010 return;
6011 }
6012
6013 /*
6014 * Validate the encoding value, if provided.
6015 * This logic is copied from xmlSaveFileEnc.
6016 */
6017
6018 if (txt_encoding == NULL)
6019 txt_encoding = (const char *) out_doc->encoding;
6020 if (txt_encoding != NULL) {
6021 doc_charset = xmlParseCharEncoding(txt_encoding);
6022
6023 if (out_doc->charset != XML_CHAR_ENCODING_UTF8) {
6024 xmlGenericError(xmlGenericErrorContext,
6025 "xmlDocDumpFormatMemoryEnc: Source document not in UTF8\n");
6026 return;
6027
6028 } else if (doc_charset != XML_CHAR_ENCODING_UTF8) {
6029 conv_hdlr = xmlFindCharEncodingHandler(txt_encoding);
6030 if ( conv_hdlr == NULL ) {
6031 xmlGenericError(xmlGenericErrorContext,
6032 "%s: %s %s '%s'\n",
6033 "xmlDocDumpFormatMemoryEnc",
6034 "Failed to identify encoding handler for",
6035 "character set",
6036 txt_encoding);
6037 return;
6038 }
6039 }
6040 }
6041
6042 if ((out_buff = xmlAllocOutputBuffer(conv_hdlr)) == NULL ) {
6043 xmlGenericError(xmlGenericErrorContext,
6044 "xmlDocDumpFormatMemoryEnc: Failed to allocate output buffer.\n");
6045 return;
6046 }
6047
Daniel Veillard1731d6a2001-04-10 16:38:06 +00006048 xmlDocContentDumpOutput(out_buff, out_doc, txt_encoding, format);
Owen Taylor3473f882001-02-23 17:55:21 +00006049 xmlOutputBufferFlush(out_buff);
6050 if (out_buff->conv != NULL) {
6051 *doc_txt_len = out_buff->conv->use;
6052 *doc_txt_ptr = xmlStrndup(out_buff->conv->content, *doc_txt_len);
6053 } else {
6054 *doc_txt_len = out_buff->buffer->use;
6055 *doc_txt_ptr = xmlStrndup(out_buff->buffer->content, *doc_txt_len);
6056 }
6057 (void)xmlOutputBufferClose(out_buff);
6058
6059 if ((*doc_txt_ptr == NULL) && (*doc_txt_len > 0)) {
6060 *doc_txt_len = 0;
6061 xmlGenericError(xmlGenericErrorContext,
6062 "xmlDocDumpFormatMemoryEnc: %s\n",
6063 "Failed to allocate memory for document text representation.");
6064 }
6065
6066 return;
6067}
6068
6069/**
6070 * xmlDocDumpMemory:
6071 * @cur: the document
6072 * @mem: OUT: the memory pointer
6073 * @size: OUT: the memory lenght
6074 *
6075 * Dump an XML document in memory and return the xmlChar * and it's size.
6076 * It's up to the caller to free the memory.
6077 */
6078void
6079xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) {
6080 xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, 0);
6081}
6082
6083/**
6084 * xmlDocDumpFormatMemory:
6085 * @cur: the document
6086 * @mem: OUT: the memory pointer
6087 * @size: OUT: the memory lenght
6088 * @format: should formatting spaces been added
6089 *
6090 *
6091 * Dump an XML document in memory and return the xmlChar * and it's size.
6092 * It's up to the caller to free the memory.
6093 */
6094void
6095xmlDocDumpFormatMemory(xmlDocPtr cur, xmlChar**mem, int *size, int format) {
6096 xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, format);
6097}
6098
6099/**
6100 * xmlDocDumpMemoryEnc:
6101 * @out_doc: Document to generate XML text from
6102 * @doc_txt_ptr: Memory pointer for allocated XML text
6103 * @doc_txt_len: Length of the generated XML text
6104 * @txt_encoding: Character encoding to use when generating XML text
6105 *
6106 * Dump the current DOM tree into memory using the character encoding specified
6107 * by the caller. Note it is up to the caller of this function to free the
6108 * allocated memory.
6109 */
6110
6111void
6112xmlDocDumpMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
6113 int * doc_txt_len, const char * txt_encoding) {
6114 xmlDocDumpFormatMemoryEnc(out_doc, doc_txt_ptr, doc_txt_len,
Daniel Veillard1731d6a2001-04-10 16:38:06 +00006115 txt_encoding, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00006116}
6117
6118/**
6119 * xmlGetDocCompressMode:
6120 * @doc: the document
6121 *
6122 * get the compression ratio for a document, ZLIB based
6123 * Returns 0 (uncompressed) to 9 (max compression)
6124 */
6125int
6126xmlGetDocCompressMode (xmlDocPtr doc) {
6127 if (doc == NULL) return(-1);
6128 return(doc->compression);
6129}
6130
6131/**
6132 * xmlSetDocCompressMode:
6133 * @doc: the document
6134 * @mode: the compression ratio
6135 *
6136 * set the compression ratio for a document, ZLIB based
6137 * Correct values: 0 (uncompressed) to 9 (max compression)
6138 */
6139void
6140xmlSetDocCompressMode (xmlDocPtr doc, int mode) {
6141 if (doc == NULL) return;
6142 if (mode < 0) doc->compression = 0;
6143 else if (mode > 9) doc->compression = 9;
6144 else doc->compression = mode;
6145}
6146
6147/**
6148 * xmlGetCompressMode:
6149 *
6150 * get the default compression mode used, ZLIB based.
6151 * Returns 0 (uncompressed) to 9 (max compression)
6152 */
6153int
6154 xmlGetCompressMode(void) {
6155 return(xmlCompressMode);
6156}
6157
6158/**
6159 * xmlSetCompressMode:
6160 * @mode: the compression ratio
6161 *
6162 * set the default compression mode used, ZLIB based
6163 * Correct values: 0 (uncompressed) to 9 (max compression)
6164 */
6165void
6166xmlSetCompressMode(int mode) {
6167 if (mode < 0) xmlCompressMode = 0;
6168 else if (mode > 9) xmlCompressMode = 9;
6169 else xmlCompressMode = mode;
6170}
6171
6172/**
6173 * xmlDocDump:
6174 * @f: the FILE*
6175 * @cur: the document
6176 *
6177 * Dump an XML document to an open FILE.
6178 *
6179 * returns: the number of byte written or -1 in case of failure.
6180 */
6181int
6182xmlDocDump(FILE *f, xmlDocPtr cur) {
6183 xmlOutputBufferPtr buf;
6184 const char * encoding;
6185 xmlCharEncodingHandlerPtr handler = NULL;
6186 int ret;
6187
6188 if (cur == NULL) {
6189#ifdef DEBUG_TREE
6190 xmlGenericError(xmlGenericErrorContext,
6191 "xmlDocDump : document == NULL\n");
6192#endif
6193 return(-1);
6194 }
6195 encoding = (const char *) cur->encoding;
6196
6197 if (encoding != NULL) {
6198 xmlCharEncoding enc;
6199
6200 enc = xmlParseCharEncoding(encoding);
6201
6202 if (cur->charset != XML_CHAR_ENCODING_UTF8) {
6203 xmlGenericError(xmlGenericErrorContext,
6204 "xmlDocDump: document not in UTF8\n");
6205 return(-1);
6206 }
6207 if (enc != XML_CHAR_ENCODING_UTF8) {
6208 handler = xmlFindCharEncodingHandler(encoding);
6209 if (handler == NULL) {
6210 xmlFree((char *) cur->encoding);
6211 cur->encoding = NULL;
6212 }
6213 }
6214 }
6215 buf = xmlOutputBufferCreateFile(f, handler);
6216 if (buf == NULL) return(-1);
Daniel Veillard1731d6a2001-04-10 16:38:06 +00006217 xmlDocContentDumpOutput(buf, cur, NULL, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00006218
6219 ret = xmlOutputBufferClose(buf);
6220 return(ret);
6221}
6222
6223/**
6224 * xmlSaveFileTo:
6225 * @buf: an output I/O buffer
6226 * @cur: the document
6227 * @encoding: the encoding if any assuming the i/O layer handles the trancoding
6228 *
6229 * Dump an XML document to an I/O buffer.
6230 *
6231 * returns: the number of byte written or -1 in case of failure.
6232 */
6233int
6234xmlSaveFileTo(xmlOutputBuffer *buf, xmlDocPtr cur, const char *encoding) {
6235 int ret;
6236
6237 if (buf == NULL) return(0);
Daniel Veillard1731d6a2001-04-10 16:38:06 +00006238 xmlDocContentDumpOutput(buf, cur, encoding, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00006239 ret = xmlOutputBufferClose(buf);
6240 return(ret);
6241}
6242
6243/**
Daniel Veillardeefd4492001-04-28 16:55:50 +00006244 * xmlSaveFormatFileTo:
6245 * @buf: an output I/O buffer
6246 * @cur: the document
6247 * @encoding: the encoding if any assuming the i/O layer handles the trancoding
6248 * @format: should formatting spaces been added
6249 *
6250 * Dump an XML document to an I/O buffer.
6251 *
6252 * returns: the number of byte written or -1 in case of failure.
6253 */
6254int
6255xmlSaveFormatFileTo(xmlOutputBuffer *buf, xmlDocPtr cur, const char *encoding, int format) {
6256 int ret;
6257
6258 if (buf == NULL) return(0);
6259 xmlDocContentDumpOutput(buf, cur, encoding, format);
6260 ret = xmlOutputBufferClose(buf);
6261 return(ret);
6262}
6263
6264/**
Owen Taylor3473f882001-02-23 17:55:21 +00006265 * xmlSaveFileEnc:
6266 * @filename: the filename (or URL)
6267 * @cur: the document
6268 * @encoding: the name of an encoding (or NULL)
6269 *
6270 * Dump an XML document, converting it to the given encoding
6271 *
6272 * returns: the number of byte written or -1 in case of failure.
6273 */
6274int
6275xmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) {
6276 xmlOutputBufferPtr buf;
6277 xmlCharEncodingHandlerPtr handler = NULL;
Daniel Veillard81418e32001-05-22 15:08:55 +00006278 xmlCharEncoding enc;
Owen Taylor3473f882001-02-23 17:55:21 +00006279 int ret;
6280
6281 if (encoding != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00006282
6283 enc = xmlParseCharEncoding(encoding);
6284 if (cur->charset != XML_CHAR_ENCODING_UTF8) {
6285 xmlGenericError(xmlGenericErrorContext,
6286 "xmlSaveFileEnc: document not in UTF8\n");
6287 return(-1);
6288 }
6289 if (enc != XML_CHAR_ENCODING_UTF8) {
6290 handler = xmlFindCharEncodingHandler(encoding);
Daniel Veillard81418e32001-05-22 15:08:55 +00006291 if (handler == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00006292 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006293 }
6294 }
6295
6296 /*
6297 * save the content to a temp buffer.
6298 */
6299 buf = xmlOutputBufferCreateFilename(filename, handler, 0);
6300 if (buf == NULL) return(-1);
6301
Daniel Veillard1731d6a2001-04-10 16:38:06 +00006302 xmlDocContentDumpOutput(buf, cur, encoding, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00006303
6304 ret = xmlOutputBufferClose(buf);
6305 return(ret);
6306}
6307
6308/**
Daniel Veillard67fee942001-04-26 18:59:03 +00006309 * xmlSaveFormatFile:
Owen Taylor3473f882001-02-23 17:55:21 +00006310 * @filename: the filename (or URL)
6311 * @cur: the document
Daniel Veillard67fee942001-04-26 18:59:03 +00006312 * @format: should formatting spaces been added
Owen Taylor3473f882001-02-23 17:55:21 +00006313 *
6314 * Dump an XML document to a file. Will use compression if
6315 * compiled in and enabled. If @filename is "-" the stdout file is
Daniel Veillard67fee942001-04-26 18:59:03 +00006316 * used. If format is set then the document will be indented on output.
6317 *
Owen Taylor3473f882001-02-23 17:55:21 +00006318 * returns: the number of byte written or -1 in case of failure.
6319 */
6320int
Daniel Veillard67fee942001-04-26 18:59:03 +00006321xmlSaveFormatFile(const char *filename, xmlDocPtr cur, int format) {
Owen Taylor3473f882001-02-23 17:55:21 +00006322 xmlOutputBufferPtr buf;
6323 const char *encoding;
6324 xmlCharEncodingHandlerPtr handler = NULL;
6325 int ret;
6326
6327 if (cur == NULL)
6328 return(-1);
6329 encoding = (const char *) cur->encoding;
6330
6331 /*
6332 * save the content to a temp buffer.
6333 */
6334#ifdef HAVE_ZLIB_H
6335 if (cur->compression < 0) cur->compression = xmlCompressMode;
6336#endif
6337 if (encoding != NULL) {
6338 xmlCharEncoding enc;
6339
6340 enc = xmlParseCharEncoding(encoding);
6341
6342 if (cur->charset != XML_CHAR_ENCODING_UTF8) {
6343 xmlGenericError(xmlGenericErrorContext,
6344 "xmlSaveFile: document not in UTF8\n");
6345 return(-1);
6346 }
6347 if (enc != XML_CHAR_ENCODING_UTF8) {
6348 handler = xmlFindCharEncodingHandler(encoding);
6349 if (handler == NULL) {
6350 xmlFree((char *) cur->encoding);
6351 cur->encoding = NULL;
6352 }
6353 }
6354 }
6355
6356 buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression);
6357 if (buf == NULL) return(-1);
6358
Daniel Veillard67fee942001-04-26 18:59:03 +00006359 xmlDocContentDumpOutput(buf, cur, NULL, format);
Owen Taylor3473f882001-02-23 17:55:21 +00006360
6361 ret = xmlOutputBufferClose(buf);
6362 return(ret);
6363}
6364
Daniel Veillard67fee942001-04-26 18:59:03 +00006365/**
6366 * xmlSaveFile:
6367 * @filename: the filename (or URL)
6368 * @cur: the document
6369 *
6370 * Dump an XML document to a file. Will use compression if
6371 * compiled in and enabled. If @filename is "-" the stdout file is
6372 * used.
6373 * returns: the number of byte written or -1 in case of failure.
6374 */
6375int
6376xmlSaveFile(const char *filename, xmlDocPtr cur) {
6377 return(xmlSaveFormatFile(filename, cur, 0));
6378}
6379