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