blob: feabe8bf07ef44fa28d3c4184c00deb2a2ba615d [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
Daniel Veillardd1640922001-12-17 15:30:10 +00002 * tree.c : implementation of access function for an XML tree.
Owen Taylor3473f882001-02-23 17:55:21 +00003 *
4 * See Copyright for the status of this software.
5 *
Daniel Veillardc5d64342001-06-24 12:13:24 +00006 * daniel@veillard.com
Owen Taylor3473f882001-02-23 17:55:21 +00007 *
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>
Daniel Veillardb8c9be92001-07-09 16:01:19 +000031#include <libxml/uri.h>
Owen Taylor3473f882001-02-23 17:55:21 +000032#include <libxml/entities.h>
33#include <libxml/valid.h>
34#include <libxml/xmlerror.h>
Daniel Veillardbdb9ba72001-04-11 11:28:06 +000035#include <libxml/parserInternals.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000036#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000037
Daniel Veillard56a4cb82001-03-24 17:00:36 +000038xmlNsPtr xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns);
39
40/************************************************************************
41 * *
42 * A few static variables and macros *
43 * *
44 ************************************************************************/
Daniel Veillardd0463562001-10-13 09:15:48 +000045/* #undef xmlStringText */
Daniel Veillard22090732001-07-16 00:06:07 +000046const xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 };
Daniel Veillardd0463562001-10-13 09:15:48 +000047/* #undef xmlStringTextNoenc */
Daniel Veillard22090732001-07-16 00:06:07 +000048const xmlChar xmlStringTextNoenc[] =
Owen Taylor3473f882001-02-23 17:55:21 +000049 { 't', 'e', 'x', 't', 'n', 'o', 'e', 'n', 'c', 0 };
Daniel Veillardd0463562001-10-13 09:15:48 +000050/* #undef xmlStringComment */
Daniel Veillard22090732001-07-16 00:06:07 +000051const xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 };
52
Owen Taylor3473f882001-02-23 17:55:21 +000053static int xmlCompressMode = 0;
54static int xmlCheckDTD = 1;
Owen Taylor3473f882001-02-23 17:55:21 +000055
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.
Daniel Veillardd1640922001-12-17 15:30:10 +0000118 * Returns a new namespace pointer or NULL
Owen Taylor3473f882001-02-23 17:55:21 +0000119 */
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
Daniel Veillard20ee8c02001-10-05 09:18:14 +0000127 if ((prefix != NULL) && (xmlStrEqual(prefix, BAD_CAST "xml")))
128 return(NULL);
129
Owen Taylor3473f882001-02-23 17:55:21 +0000130 /*
131 * Allocate a new Namespace and fill the fields.
132 */
133 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
134 if (cur == NULL) {
135 xmlGenericError(xmlGenericErrorContext,
136 "xmlNewNs : malloc failed\n");
137 return(NULL);
138 }
139 memset(cur, 0, sizeof(xmlNs));
140 cur->type = XML_LOCAL_NAMESPACE;
141
142 if (href != NULL)
143 cur->href = xmlStrdup(href);
144 if (prefix != NULL)
145 cur->prefix = xmlStrdup(prefix);
146
147 /*
148 * Add it at the end to preserve parsing order ...
149 * and checks for existing use of the prefix
150 */
151 if (node != NULL) {
152 if (node->nsDef == NULL) {
153 node->nsDef = cur;
154 } else {
155 xmlNsPtr prev = node->nsDef;
156
157 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
158 (xmlStrEqual(prev->prefix, cur->prefix))) {
159 xmlFreeNs(cur);
160 return(NULL);
161 }
162 while (prev->next != NULL) {
163 prev = prev->next;
164 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
165 (xmlStrEqual(prev->prefix, cur->prefix))) {
166 xmlFreeNs(cur);
167 return(NULL);
168 }
169 }
170 prev->next = cur;
171 }
172 }
173 return(cur);
174}
175
176/**
177 * xmlSetNs:
178 * @node: a node in the document
179 * @ns: a namespace pointer
180 *
181 * Associate a namespace to a node, a posteriori.
182 */
183void
184xmlSetNs(xmlNodePtr node, xmlNsPtr ns) {
185 if (node == NULL) {
186#ifdef DEBUG_TREE
187 xmlGenericError(xmlGenericErrorContext,
188 "xmlSetNs: node == NULL\n");
189#endif
190 return;
191 }
192 node->ns = ns;
193}
194
195/**
196 * xmlFreeNs:
197 * @cur: the namespace pointer
198 *
199 * Free up the structures associated to a namespace
200 */
201void
202xmlFreeNs(xmlNsPtr cur) {
203 if (cur == NULL) {
204#ifdef DEBUG_TREE
205 xmlGenericError(xmlGenericErrorContext,
206 "xmlFreeNs : ns == NULL\n");
207#endif
208 return;
209 }
210 if (cur->href != NULL) xmlFree((char *) cur->href);
211 if (cur->prefix != NULL) xmlFree((char *) cur->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000212 xmlFree(cur);
213}
214
215/**
216 * xmlFreeNsList:
217 * @cur: the first namespace pointer
218 *
219 * Free up all the structures associated to the chained namespaces.
220 */
221void
222xmlFreeNsList(xmlNsPtr cur) {
223 xmlNsPtr next;
224 if (cur == NULL) {
225#ifdef DEBUG_TREE
226 xmlGenericError(xmlGenericErrorContext,
227 "xmlFreeNsList : ns == NULL\n");
228#endif
229 return;
230 }
231 while (cur != NULL) {
232 next = cur->next;
233 xmlFreeNs(cur);
234 cur = next;
235 }
236}
237
238/**
239 * xmlNewDtd:
240 * @doc: the document pointer
241 * @name: the DTD name
242 * @ExternalID: the external ID
243 * @SystemID: the system ID
244 *
245 * Creation of a new DTD for the external subset. To create an
246 * internal subset, use xmlCreateIntSubset().
247 *
248 * Returns a pointer to the new DTD structure
249 */
250xmlDtdPtr
251xmlNewDtd(xmlDocPtr doc, const xmlChar *name,
252 const xmlChar *ExternalID, const xmlChar *SystemID) {
253 xmlDtdPtr cur;
254
255 if ((doc != NULL) && (doc->extSubset != NULL)) {
256#ifdef DEBUG_TREE
257 xmlGenericError(xmlGenericErrorContext,
258 "xmlNewDtd(%s): document %s already have a DTD %s\n",
259 /* !!! */ (char *) name, doc->name,
260 /* !!! */ (char *)doc->extSubset->name);
261#endif
262 return(NULL);
263 }
264
265 /*
266 * Allocate a new DTD and fill the fields.
267 */
268 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
269 if (cur == NULL) {
270 xmlGenericError(xmlGenericErrorContext,
271 "xmlNewDtd : malloc failed\n");
272 return(NULL);
273 }
274 memset(cur, 0 , sizeof(xmlDtd));
275 cur->type = XML_DTD_NODE;
276
277 if (name != NULL)
278 cur->name = xmlStrdup(name);
279 if (ExternalID != NULL)
280 cur->ExternalID = xmlStrdup(ExternalID);
281 if (SystemID != NULL)
282 cur->SystemID = xmlStrdup(SystemID);
283 if (doc != NULL)
284 doc->extSubset = cur;
285 cur->doc = doc;
286
287 return(cur);
288}
289
290/**
291 * xmlGetIntSubset:
292 * @doc: the document pointer
293 *
294 * Get the internal subset of a document
295 * Returns a pointer to the DTD structure or NULL if not found
296 */
297
298xmlDtdPtr
299xmlGetIntSubset(xmlDocPtr doc) {
300 xmlNodePtr cur;
301
302 if (doc == NULL)
303 return(NULL);
304 cur = doc->children;
305 while (cur != NULL) {
306 if (cur->type == XML_DTD_NODE)
307 return((xmlDtdPtr) cur);
308 cur = cur->next;
309 }
310 return((xmlDtdPtr) doc->intSubset);
311}
312
313/**
314 * xmlCreateIntSubset:
315 * @doc: the document pointer
316 * @name: the DTD name
Daniel Veillarde356c282001-03-10 12:32:04 +0000317 * @ExternalID: the external (PUBLIC) ID
Owen Taylor3473f882001-02-23 17:55:21 +0000318 * @SystemID: the system ID
319 *
320 * Create the internal subset of a document
321 * Returns a pointer to the new DTD structure
322 */
323xmlDtdPtr
324xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name,
325 const xmlChar *ExternalID, const xmlChar *SystemID) {
326 xmlDtdPtr cur;
327
328 if ((doc != NULL) && (xmlGetIntSubset(doc) != NULL)) {
329#ifdef DEBUG_TREE
330 xmlGenericError(xmlGenericErrorContext,
331
332 "xmlCreateIntSubset(): document %s already have an internal subset\n",
333 doc->name);
334#endif
335 return(NULL);
336 }
337
338 /*
339 * Allocate a new DTD and fill the fields.
340 */
341 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
342 if (cur == NULL) {
343 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +0000344 "xmlCreateIntSubset : malloc failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000345 return(NULL);
346 }
347 memset(cur, 0, sizeof(xmlDtd));
348 cur->type = XML_DTD_NODE;
349
350 if (name != NULL)
351 cur->name = xmlStrdup(name);
352 if (ExternalID != NULL)
353 cur->ExternalID = xmlStrdup(ExternalID);
354 if (SystemID != NULL)
355 cur->SystemID = xmlStrdup(SystemID);
356 if (doc != NULL) {
357 doc->intSubset = cur;
358 cur->parent = doc;
359 cur->doc = doc;
360 if (doc->children == NULL) {
361 doc->children = (xmlNodePtr) cur;
362 doc->last = (xmlNodePtr) cur;
363 } else {
Owen Taylor3473f882001-02-23 17:55:21 +0000364 if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillarde356c282001-03-10 12:32:04 +0000365 xmlNodePtr prev;
366
Owen Taylor3473f882001-02-23 17:55:21 +0000367 prev = doc->children;
368 prev->prev = (xmlNodePtr) cur;
369 cur->next = prev;
370 doc->children = (xmlNodePtr) cur;
371 } else {
Daniel Veillarde356c282001-03-10 12:32:04 +0000372 xmlNodePtr next;
373
374 next = doc->children;
375 while ((next != NULL) && (next->type != XML_ELEMENT_NODE))
376 next = next->next;
377 if (next == NULL) {
378 cur->prev = doc->last;
379 cur->prev->next = (xmlNodePtr) cur;
380 cur->next = NULL;
381 doc->last = (xmlNodePtr) cur;
382 } else {
383 cur->next = next;
384 cur->prev = next->prev;
385 if (cur->prev == NULL)
386 doc->children = (xmlNodePtr) cur;
387 else
388 cur->prev->next = (xmlNodePtr) cur;
389 next->prev = (xmlNodePtr) cur;
390 }
Owen Taylor3473f882001-02-23 17:55:21 +0000391 }
392 }
393 }
394 return(cur);
395}
396
397/**
398 * xmlFreeDtd:
399 * @cur: the DTD structure to free up
400 *
401 * Free a DTD structure.
402 */
403void
404xmlFreeDtd(xmlDtdPtr cur) {
405 if (cur == NULL) {
406#ifdef DEBUG_TREE
407 xmlGenericError(xmlGenericErrorContext,
408 "xmlFreeDtd : DTD == NULL\n");
409#endif
410 return;
411 }
412 if (cur->children != NULL) {
413 xmlNodePtr next, c = cur->children;
414
415 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000416 * Cleanup all the DTD comments they are not in the DTD
Owen Taylor3473f882001-02-23 17:55:21 +0000417 * indexes.
418 */
419 while (c != NULL) {
420 next = c->next;
421 if (c->type == XML_COMMENT_NODE) {
422 xmlUnlinkNode(c);
423 xmlFreeNode(c);
424 }
425 c = next;
426 }
427 }
428 if (cur->name != NULL) xmlFree((char *) cur->name);
429 if (cur->SystemID != NULL) xmlFree((char *) cur->SystemID);
430 if (cur->ExternalID != NULL) xmlFree((char *) cur->ExternalID);
431 /* TODO !!! */
432 if (cur->notations != NULL)
433 xmlFreeNotationTable((xmlNotationTablePtr) cur->notations);
434
435 if (cur->elements != NULL)
436 xmlFreeElementTable((xmlElementTablePtr) cur->elements);
437 if (cur->attributes != NULL)
438 xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes);
439 if (cur->entities != NULL)
440 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities);
441 if (cur->pentities != NULL)
442 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->pentities);
443
Owen Taylor3473f882001-02-23 17:55:21 +0000444 xmlFree(cur);
445}
446
447/**
448 * xmlNewDoc:
449 * @version: xmlChar string giving the version of XML "1.0"
450 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000451 * Creates a new XML document
452 *
Owen Taylor3473f882001-02-23 17:55:21 +0000453 * Returns a new document
454 */
455xmlDocPtr
456xmlNewDoc(const xmlChar *version) {
457 xmlDocPtr cur;
458
459 if (version == NULL)
460 version = (const xmlChar *) "1.0";
461
462 /*
463 * Allocate a new document and fill the fields.
464 */
465 cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
466 if (cur == NULL) {
467 xmlGenericError(xmlGenericErrorContext,
468 "xmlNewDoc : malloc failed\n");
469 return(NULL);
470 }
471 memset(cur, 0, sizeof(xmlDoc));
472 cur->type = XML_DOCUMENT_NODE;
473
474 cur->version = xmlStrdup(version);
475 cur->standalone = -1;
476 cur->compression = -1; /* not initialized */
477 cur->doc = cur;
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000478 cur->charset = XML_CHAR_ENCODING_UTF8;
Owen Taylor3473f882001-02-23 17:55:21 +0000479 return(cur);
480}
481
482/**
483 * xmlFreeDoc:
484 * @cur: pointer to the document
Owen Taylor3473f882001-02-23 17:55:21 +0000485 *
486 * Free up all the structures used by a document, tree included.
487 */
488void
489xmlFreeDoc(xmlDocPtr cur) {
Daniel Veillarda9142e72001-06-19 11:07:54 +0000490 xmlDtdPtr extSubset, intSubset;
491
Owen Taylor3473f882001-02-23 17:55:21 +0000492 if (cur == NULL) {
493#ifdef DEBUG_TREE
494 xmlGenericError(xmlGenericErrorContext,
495 "xmlFreeDoc : document == NULL\n");
496#endif
497 return;
498 }
Daniel Veillard76d66f42001-05-16 21:05:17 +0000499 /*
500 * Do this before freeing the children list to avoid ID lookups
501 */
502 if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids);
503 cur->ids = NULL;
504 if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
505 cur->refs = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +0000506 extSubset = cur->extSubset;
507 intSubset = cur->intSubset;
508 if (extSubset != NULL) {
Daniel Veillard76d66f42001-05-16 21:05:17 +0000509 xmlUnlinkNode((xmlNodePtr) cur->extSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +0000510 cur->extSubset = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +0000511 xmlFreeDtd(extSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +0000512 }
Daniel Veillarda9142e72001-06-19 11:07:54 +0000513 if (intSubset != NULL) {
Daniel Veillard76d66f42001-05-16 21:05:17 +0000514 xmlUnlinkNode((xmlNodePtr) cur->intSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +0000515 cur->intSubset = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +0000516 xmlFreeDtd(intSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +0000517 }
518
519 if (cur->children != NULL) xmlFreeNodeList(cur->children);
520
Owen Taylor3473f882001-02-23 17:55:21 +0000521 if (cur->version != NULL) xmlFree((char *) cur->version);
522 if (cur->name != NULL) xmlFree((char *) cur->name);
523 if (cur->encoding != NULL) xmlFree((char *) cur->encoding);
Owen Taylor3473f882001-02-23 17:55:21 +0000524 if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
Owen Taylor3473f882001-02-23 17:55:21 +0000525 if (cur->URL != NULL) xmlFree((char *) cur->URL);
Owen Taylor3473f882001-02-23 17:55:21 +0000526 xmlFree(cur);
527}
528
529/**
530 * xmlStringLenGetNodeList:
531 * @doc: the document
532 * @value: the value of the text
533 * @len: the length of the string value
534 *
535 * Parse the value string and build the node list associated. Should
536 * produce a flat tree with only TEXTs and ENTITY_REFs.
537 * Returns a pointer to the first child
538 */
539xmlNodePtr
540xmlStringLenGetNodeList(xmlDocPtr doc, const xmlChar *value, int len) {
541 xmlNodePtr ret = NULL, last = NULL;
542 xmlNodePtr node;
543 xmlChar *val;
544 const xmlChar *cur = value;
545 const xmlChar *q;
546 xmlEntityPtr ent;
547
548 if (value == NULL) return(NULL);
549
550 q = cur;
551 while ((*cur != 0) && (cur - value < len)) {
552 if (*cur == '&') {
553 /*
554 * Save the current text.
555 */
556 if (cur != q) {
557 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
558 xmlNodeAddContentLen(last, q, cur - q);
559 } else {
560 node = xmlNewDocTextLen(doc, q, cur - q);
561 if (node == NULL) return(ret);
562 if (last == NULL)
563 last = ret = node;
564 else {
565 last->next = node;
566 node->prev = last;
567 last = node;
568 }
569 }
570 }
571 /*
572 * Read the entity string
573 */
574 cur++;
575 q = cur;
576 while ((*cur != 0) && (cur - value < len) && (*cur != ';')) cur++;
577 if ((*cur == 0) || (cur - value >= len)) {
578#ifdef DEBUG_TREE
579 xmlGenericError(xmlGenericErrorContext,
580 "xmlStringLenGetNodeList: unterminated entity %30s\n", q);
581#endif
582 return(ret);
583 }
584 if (cur != q) {
585 /*
586 * Predefined entities don't generate nodes
587 */
588 val = xmlStrndup(q, cur - q);
589 ent = xmlGetDocEntity(doc, val);
590 if ((ent != NULL) &&
591 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
592 if (last == NULL) {
593 node = xmlNewDocText(doc, ent->content);
594 last = ret = node;
595 } else
596 xmlNodeAddContent(last, ent->content);
597
598 } else {
599 /*
600 * Create a new REFERENCE_REF node
601 */
602 node = xmlNewReference(doc, val);
603 if (node == NULL) {
604 if (val != NULL) xmlFree(val);
605 return(ret);
606 }
607 if (last == NULL)
608 last = ret = node;
609 else {
610 last->next = node;
611 node->prev = last;
612 last = node;
613 }
614 }
615 xmlFree(val);
616 }
617 cur++;
618 q = cur;
619 } else
620 cur++;
621 }
622 if (cur != q) {
623 /*
624 * Handle the last piece of text.
625 */
626 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
627 xmlNodeAddContentLen(last, q, cur - q);
628 } else {
629 node = xmlNewDocTextLen(doc, q, cur - q);
630 if (node == NULL) return(ret);
631 if (last == NULL)
632 last = ret = node;
633 else {
634 last->next = node;
635 node->prev = last;
636 last = node;
637 }
638 }
639 }
640 return(ret);
641}
642
643/**
644 * xmlStringGetNodeList:
645 * @doc: the document
646 * @value: the value of the attribute
647 *
648 * Parse the value string and build the node list associated. Should
649 * produce a flat tree with only TEXTs and ENTITY_REFs.
650 * Returns a pointer to the first child
651 */
652xmlNodePtr
653xmlStringGetNodeList(xmlDocPtr doc, const xmlChar *value) {
654 xmlNodePtr ret = NULL, last = NULL;
655 xmlNodePtr node;
656 xmlChar *val;
657 const xmlChar *cur = value;
658 const xmlChar *q;
659 xmlEntityPtr ent;
660
661 if (value == NULL) return(NULL);
662
663 q = cur;
664 while (*cur != 0) {
Daniel Veillardbdb9ba72001-04-11 11:28:06 +0000665 if (cur[0] == '&') {
666 int charval = 0;
667 xmlChar tmp;
668
Owen Taylor3473f882001-02-23 17:55:21 +0000669 /*
670 * Save the current text.
671 */
672 if (cur != q) {
673 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
674 xmlNodeAddContentLen(last, q, cur - q);
675 } else {
676 node = xmlNewDocTextLen(doc, q, cur - q);
677 if (node == NULL) return(ret);
678 if (last == NULL)
679 last = ret = node;
680 else {
681 last->next = node;
682 node->prev = last;
683 last = node;
684 }
685 }
686 }
Owen Taylor3473f882001-02-23 17:55:21 +0000687 q = cur;
Daniel Veillardbdb9ba72001-04-11 11:28:06 +0000688 if ((cur[1] == '#') && (cur[2] == 'x')) {
689 cur += 3;
690 tmp = *cur;
691 while (tmp != ';') { /* Non input consuming loop */
692 if ((tmp >= '0') && (tmp <= '9'))
693 charval = charval * 16 + (tmp - '0');
694 else if ((tmp >= 'a') && (tmp <= 'f'))
695 charval = charval * 16 + (tmp - 'a') + 10;
696 else if ((tmp >= 'A') && (tmp <= 'F'))
697 charval = charval * 16 + (tmp - 'A') + 10;
Owen Taylor3473f882001-02-23 17:55:21 +0000698 else {
Daniel Veillardbdb9ba72001-04-11 11:28:06 +0000699 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +0000700 "xmlStringGetNodeList: invalid hexadecimal charvalue\n");
Daniel Veillardbdb9ba72001-04-11 11:28:06 +0000701 charval = 0;
702 break;
703 }
704 cur++;
705 tmp = *cur;
706 }
707 if (tmp == ';')
708 cur++;
709 q = cur;
710 } else if (cur[1] == '#') {
711 cur += 2;
712 tmp = *cur;
713 while (tmp != ';') { /* Non input consuming loops */
714 if ((tmp >= '0') && (tmp <= '9'))
715 charval = charval * 10 + (tmp - '0');
716 else {
717 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +0000718 "xmlStringGetNodeList: invalid decimal charvalue\n");
Daniel Veillardbdb9ba72001-04-11 11:28:06 +0000719 charval = 0;
720 break;
721 }
722 cur++;
723 tmp = *cur;
724 }
725 if (tmp == ';')
726 cur++;
727 q = cur;
728 } else {
729 /*
730 * Read the entity string
731 */
732 cur++;
733 q = cur;
734 while ((*cur != 0) && (*cur != ';')) cur++;
735 if (*cur == 0) {
736#ifdef DEBUG_TREE
737 xmlGenericError(xmlGenericErrorContext,
738 "xmlStringGetNodeList: unterminated entity %30s\n", q);
739#endif
740 return(ret);
741 }
742 if (cur != q) {
743 /*
744 * Predefined entities don't generate nodes
745 */
746 val = xmlStrndup(q, cur - q);
747 ent = xmlGetDocEntity(doc, val);
748 if ((ent != NULL) &&
749 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
750 if (last == NULL) {
751 node = xmlNewDocText(doc, ent->content);
752 last = ret = node;
Daniel Veillard6f42c132002-01-06 23:05:13 +0000753 } else if (last->type != XML_TEXT_NODE) {
754 node = xmlNewDocText(doc, ent->content);
755 last = xmlAddNextSibling(last, node);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +0000756 } else
757 xmlNodeAddContent(last, ent->content);
758
759 } else {
760 /*
761 * Create a new REFERENCE_REF node
762 */
763 node = xmlNewReference(doc, val);
764 if (node == NULL) {
765 if (val != NULL) xmlFree(val);
766 return(ret);
767 }
768 if (last == NULL) {
769 last = ret = node;
770 } else {
771 last = xmlAddNextSibling(last, node);
772 }
773 }
774 xmlFree(val);
775 }
776 cur++;
777 q = cur;
778 }
779 if (charval != 0) {
780 xmlChar buf[10];
781 int len;
782
783 len = xmlCopyCharMultiByte(buf, charval);
784 buf[len] = 0;
785 node = xmlNewDocText(doc, buf);
786 if (node != NULL) {
787 if (last == NULL) {
788 last = ret = node;
789 } else {
790 last = xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +0000791 }
792 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +0000793
794 charval = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000795 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +0000796 } else
Owen Taylor3473f882001-02-23 17:55:21 +0000797 cur++;
798 }
Daniel Veillard75bea542001-05-11 17:41:21 +0000799 if ((cur != q) || (ret == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000800 /*
801 * Handle the last piece of text.
802 */
803 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
804 xmlNodeAddContentLen(last, q, cur - q);
805 } else {
806 node = xmlNewDocTextLen(doc, q, cur - q);
807 if (node == NULL) return(ret);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +0000808 if (last == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +0000809 last = ret = node;
Daniel Veillardbdb9ba72001-04-11 11:28:06 +0000810 } else {
811 last = xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +0000812 }
813 }
814 }
815 return(ret);
816}
817
818/**
819 * xmlNodeListGetString:
820 * @doc: the document
821 * @list: a Node list
822 * @inLine: should we replace entity contents or show their external form
823 *
824 * Returns the string equivalent to the text contained in the Node list
825 * made of TEXTs and ENTITY_REFs
Daniel Veillardd1640922001-12-17 15:30:10 +0000826 * Returns a pointer to the string copy, the caller must free it.
Owen Taylor3473f882001-02-23 17:55:21 +0000827 */
828xmlChar *
829xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine) {
830 xmlNodePtr node = list;
831 xmlChar *ret = NULL;
832 xmlEntityPtr ent;
833
834 if (list == NULL) return(NULL);
835
836 while (node != NULL) {
837 if ((node->type == XML_TEXT_NODE) ||
838 (node->type == XML_CDATA_SECTION_NODE)) {
839 if (inLine) {
840#ifndef XML_USE_BUFFER_CONTENT
841 ret = xmlStrcat(ret, node->content);
842#else
843 ret = xmlStrcat(ret, xmlBufferContent(node->content));
844#endif
845 } else {
846 xmlChar *buffer;
847
848#ifndef XML_USE_BUFFER_CONTENT
849 buffer = xmlEncodeEntitiesReentrant(doc, node->content);
850#else
851 buffer = xmlEncodeEntitiesReentrant(doc,
852 xmlBufferContent(node->content));
853#endif
854 if (buffer != NULL) {
855 ret = xmlStrcat(ret, buffer);
856 xmlFree(buffer);
857 }
858 }
859 } else if (node->type == XML_ENTITY_REF_NODE) {
860 if (inLine) {
861 ent = xmlGetDocEntity(doc, node->name);
862 if (ent != NULL)
863 ret = xmlStrcat(ret, ent->content);
864 else {
865#ifndef XML_USE_BUFFER_CONTENT
866 ret = xmlStrcat(ret, node->content);
867#else
868 ret = xmlStrcat(ret, xmlBufferContent(node->content));
869#endif
870 }
871 } else {
872 xmlChar buf[2];
873 buf[0] = '&'; buf[1] = 0;
874 ret = xmlStrncat(ret, buf, 1);
875 ret = xmlStrcat(ret, node->name);
876 buf[0] = ';'; buf[1] = 0;
877 ret = xmlStrncat(ret, buf, 1);
878 }
879 }
880#if 0
881 else {
882 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +0000883 "xmlGetNodeListString : invalid node type %d\n",
Owen Taylor3473f882001-02-23 17:55:21 +0000884 node->type);
885 }
886#endif
887 node = node->next;
888 }
889 return(ret);
890}
891
892/**
893 * xmlNodeListGetRawString:
894 * @doc: the document
895 * @list: a Node list
896 * @inLine: should we replace entity contents or show their external form
897 *
898 * Returns the string equivalent to the text contained in the Node list
899 * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString()
900 * this function doesn't do any character encoding handling.
901 *
Daniel Veillardd1640922001-12-17 15:30:10 +0000902 * Returns a pointer to the string copy, the caller must free it.
Owen Taylor3473f882001-02-23 17:55:21 +0000903 */
904xmlChar *
905xmlNodeListGetRawString(xmlDocPtr doc, xmlNodePtr list, int inLine) {
906 xmlNodePtr node = list;
907 xmlChar *ret = NULL;
908 xmlEntityPtr ent;
909
910 if (list == NULL) return(NULL);
911
912 while (node != NULL) {
Daniel Veillard7db37732001-07-12 01:20:08 +0000913 if ((node->type == XML_TEXT_NODE) ||
914 (node->type == XML_CDATA_SECTION_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000915 if (inLine) {
916#ifndef XML_USE_BUFFER_CONTENT
917 ret = xmlStrcat(ret, node->content);
918#else
919 ret = xmlStrcat(ret, xmlBufferContent(node->content));
920#endif
921 } else {
922 xmlChar *buffer;
923
924#ifndef XML_USE_BUFFER_CONTENT
925 buffer = xmlEncodeSpecialChars(doc, node->content);
926#else
927 buffer = xmlEncodeSpecialChars(doc,
928 xmlBufferContent(node->content));
929#endif
930 if (buffer != NULL) {
931 ret = xmlStrcat(ret, buffer);
932 xmlFree(buffer);
933 }
934 }
935 } else if (node->type == XML_ENTITY_REF_NODE) {
936 if (inLine) {
937 ent = xmlGetDocEntity(doc, node->name);
938 if (ent != NULL)
939 ret = xmlStrcat(ret, ent->content);
940 else {
941#ifndef XML_USE_BUFFER_CONTENT
942 ret = xmlStrcat(ret, node->content);
943#else
944 ret = xmlStrcat(ret, xmlBufferContent(node->content));
945#endif
946 }
947 } else {
948 xmlChar buf[2];
949 buf[0] = '&'; buf[1] = 0;
950 ret = xmlStrncat(ret, buf, 1);
951 ret = xmlStrcat(ret, node->name);
952 buf[0] = ';'; buf[1] = 0;
953 ret = xmlStrncat(ret, buf, 1);
954 }
955 }
956#if 0
957 else {
958 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +0000959 "xmlGetNodeListString : invalid node type %d\n",
Owen Taylor3473f882001-02-23 17:55:21 +0000960 node->type);
961 }
962#endif
963 node = node->next;
964 }
965 return(ret);
966}
967
968/**
969 * xmlNewProp:
970 * @node: the holding node
971 * @name: the name of the attribute
972 * @value: the value of the attribute
973 *
974 * Create a new property carried by a node.
975 * Returns a pointer to the attribute
976 */
977xmlAttrPtr
978xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
979 xmlAttrPtr cur;
980 xmlDocPtr doc = NULL;
981
982 if (name == NULL) {
983#ifdef DEBUG_TREE
984 xmlGenericError(xmlGenericErrorContext,
985 "xmlNewProp : name == NULL\n");
986#endif
987 return(NULL);
988 }
989
990 /*
991 * Allocate a new property and fill the fields.
992 */
993 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
994 if (cur == NULL) {
995 xmlGenericError(xmlGenericErrorContext,
996 "xmlNewProp : malloc failed\n");
997 return(NULL);
998 }
999 memset(cur, 0, sizeof(xmlAttr));
1000 cur->type = XML_ATTRIBUTE_NODE;
1001
1002 cur->parent = node;
1003 if (node != NULL) {
1004 doc = node->doc;
1005 cur->doc = doc;
1006 }
1007 cur->name = xmlStrdup(name);
1008 if (value != NULL) {
1009 xmlChar *buffer;
1010 xmlNodePtr tmp;
1011
1012 buffer = xmlEncodeEntitiesReentrant(doc, value);
1013 cur->children = xmlStringGetNodeList(doc, buffer);
1014 cur->last = NULL;
1015 tmp = cur->children;
1016 while (tmp != NULL) {
1017 tmp->parent = (xmlNodePtr) cur;
1018 tmp->doc = doc;
1019 if (tmp->next == NULL)
1020 cur->last = tmp;
1021 tmp = tmp->next;
1022 }
1023 xmlFree(buffer);
1024 }
1025
1026 /*
1027 * Add it at the end to preserve parsing order ...
1028 */
1029 if (node != NULL) {
1030 if (node->properties == NULL) {
1031 node->properties = cur;
1032 } else {
1033 xmlAttrPtr prev = node->properties;
1034
1035 while (prev->next != NULL) prev = prev->next;
1036 prev->next = cur;
1037 cur->prev = prev;
1038 }
1039 }
1040 return(cur);
1041}
1042
1043/**
1044 * xmlNewNsProp:
1045 * @node: the holding node
1046 * @ns: the namespace
1047 * @name: the name of the attribute
1048 * @value: the value of the attribute
1049 *
1050 * Create a new property tagged with a namespace and carried by a node.
1051 * Returns a pointer to the attribute
1052 */
1053xmlAttrPtr
1054xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
1055 const xmlChar *value) {
1056 xmlAttrPtr cur;
Daniel Veillarda682b212001-06-07 19:59:42 +00001057 xmlDocPtr doc = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001058
1059 if (name == NULL) {
1060#ifdef DEBUG_TREE
1061 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00001062 "xmlNewNsProp : name == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001063#endif
1064 return(NULL);
1065 }
1066
1067 /*
1068 * Allocate a new property and fill the fields.
1069 */
1070 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1071 if (cur == NULL) {
1072 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00001073 "xmlNewNsProp : malloc failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001074 return(NULL);
1075 }
1076 memset(cur, 0, sizeof(xmlAttr));
1077 cur->type = XML_ATTRIBUTE_NODE;
1078
1079 cur->parent = node;
Daniel Veillarda682b212001-06-07 19:59:42 +00001080 if (node != NULL) {
1081 doc = node->doc;
1082 cur->doc = doc;
1083 }
Owen Taylor3473f882001-02-23 17:55:21 +00001084 cur->ns = ns;
1085 cur->name = xmlStrdup(name);
1086 if (value != NULL) {
1087 xmlChar *buffer;
1088 xmlNodePtr tmp;
1089
Daniel Veillarda682b212001-06-07 19:59:42 +00001090 buffer = xmlEncodeEntitiesReentrant(doc, value);
1091 cur->children = xmlStringGetNodeList(doc, buffer);
Owen Taylor3473f882001-02-23 17:55:21 +00001092 cur->last = NULL;
1093 tmp = cur->children;
1094 while (tmp != NULL) {
1095 tmp->parent = (xmlNodePtr) cur;
1096 if (tmp->next == NULL)
1097 cur->last = tmp;
1098 tmp = tmp->next;
1099 }
1100 xmlFree(buffer);
1101 }
1102
1103 /*
1104 * Add it at the end to preserve parsing order ...
1105 */
1106 if (node != NULL) {
1107 if (node->properties == NULL) {
1108 node->properties = cur;
1109 } else {
1110 xmlAttrPtr prev = node->properties;
1111
1112 while (prev->next != NULL) prev = prev->next;
1113 prev->next = cur;
1114 cur->prev = prev;
1115 }
1116 }
1117 return(cur);
1118}
1119
1120/**
1121 * xmlNewDocProp:
1122 * @doc: the document
1123 * @name: the name of the attribute
1124 * @value: the value of the attribute
1125 *
1126 * Create a new property carried by a document.
1127 * Returns a pointer to the attribute
1128 */
1129xmlAttrPtr
1130xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) {
1131 xmlAttrPtr cur;
1132
1133 if (name == NULL) {
1134#ifdef DEBUG_TREE
1135 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00001136 "xmlNewDocProp : name == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001137#endif
1138 return(NULL);
1139 }
1140
1141 /*
1142 * Allocate a new property and fill the fields.
1143 */
1144 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1145 if (cur == NULL) {
1146 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00001147 "xmlNewDocProp : malloc failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001148 return(NULL);
1149 }
1150 memset(cur, 0, sizeof(xmlAttr));
1151 cur->type = XML_ATTRIBUTE_NODE;
1152
1153 cur->name = xmlStrdup(name);
1154 cur->doc = doc;
1155 if (value != NULL) {
1156 xmlNodePtr tmp;
1157
1158 cur->children = xmlStringGetNodeList(doc, value);
1159 cur->last = NULL;
1160
1161 tmp = cur->children;
1162 while (tmp != NULL) {
1163 tmp->parent = (xmlNodePtr) cur;
1164 if (tmp->next == NULL)
1165 cur->last = tmp;
1166 tmp = tmp->next;
1167 }
1168 }
1169 return(cur);
1170}
1171
1172/**
1173 * xmlFreePropList:
1174 * @cur: the first property in the list
1175 *
1176 * Free a property and all its siblings, all the children are freed too.
1177 */
1178void
1179xmlFreePropList(xmlAttrPtr cur) {
1180 xmlAttrPtr next;
1181 if (cur == NULL) {
1182#ifdef DEBUG_TREE
1183 xmlGenericError(xmlGenericErrorContext,
1184 "xmlFreePropList : property == NULL\n");
1185#endif
1186 return;
1187 }
1188 while (cur != NULL) {
1189 next = cur->next;
1190 xmlFreeProp(cur);
1191 cur = next;
1192 }
1193}
1194
1195/**
1196 * xmlFreeProp:
1197 * @cur: an attribute
1198 *
1199 * Free one attribute, all the content is freed too
1200 */
1201void
1202xmlFreeProp(xmlAttrPtr cur) {
1203 if (cur == NULL) {
1204#ifdef DEBUG_TREE
1205 xmlGenericError(xmlGenericErrorContext,
1206 "xmlFreeProp : property == NULL\n");
1207#endif
1208 return;
1209 }
1210 /* Check for ID removal -> leading to invalid references ! */
Daniel Veillard76d66f42001-05-16 21:05:17 +00001211 if ((cur->parent != NULL) && (cur->parent->doc != NULL) &&
1212 ((cur->parent->doc->intSubset != NULL) ||
1213 (cur->parent->doc->extSubset != NULL))) {
1214 if (xmlIsID(cur->parent->doc, cur->parent, cur))
1215 xmlRemoveID(cur->parent->doc, cur);
1216 }
Owen Taylor3473f882001-02-23 17:55:21 +00001217 if (cur->name != NULL) xmlFree((char *) cur->name);
1218 if (cur->children != NULL) xmlFreeNodeList(cur->children);
Owen Taylor3473f882001-02-23 17:55:21 +00001219 xmlFree(cur);
1220}
1221
1222/**
1223 * xmlRemoveProp:
1224 * @cur: an attribute
1225 *
1226 * Unlink and free one attribute, all the content is freed too
1227 * Note this doesn't work for namespace definition attributes
1228 *
1229 * Returns 0 if success and -1 in case of error.
1230 */
1231int
1232xmlRemoveProp(xmlAttrPtr cur) {
1233 xmlAttrPtr tmp;
1234 if (cur == NULL) {
1235#ifdef DEBUG_TREE
1236 xmlGenericError(xmlGenericErrorContext,
1237 "xmlRemoveProp : cur == NULL\n");
1238#endif
1239 return(-1);
1240 }
1241 if (cur->parent == NULL) {
1242#ifdef DEBUG_TREE
1243 xmlGenericError(xmlGenericErrorContext,
1244 "xmlRemoveProp : cur->parent == NULL\n");
1245#endif
1246 return(-1);
1247 }
1248 tmp = cur->parent->properties;
1249 if (tmp == cur) {
1250 cur->parent->properties = cur->next;
1251 xmlFreeProp(cur);
1252 return(0);
1253 }
1254 while (tmp != NULL) {
1255 if (tmp->next == cur) {
1256 tmp->next = cur->next;
1257 if (tmp->next != NULL)
1258 tmp->next->prev = tmp;
1259 xmlFreeProp(cur);
1260 return(0);
1261 }
1262 tmp = tmp->next;
1263 }
1264#ifdef DEBUG_TREE
1265 xmlGenericError(xmlGenericErrorContext,
1266 "xmlRemoveProp : attribute not owned by its node\n");
1267#endif
1268 return(-1);
1269}
1270
1271/**
1272 * xmlNewPI:
1273 * @name: the processing instruction name
1274 * @content: the PI content
1275 *
1276 * Creation of a processing instruction element.
1277 * Returns a pointer to the new node object.
1278 */
1279xmlNodePtr
1280xmlNewPI(const xmlChar *name, const xmlChar *content) {
1281 xmlNodePtr cur;
1282
1283 if (name == NULL) {
1284#ifdef DEBUG_TREE
1285 xmlGenericError(xmlGenericErrorContext,
1286 "xmlNewPI : name == NULL\n");
1287#endif
1288 return(NULL);
1289 }
1290
1291 /*
1292 * Allocate a new node and fill the fields.
1293 */
1294 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1295 if (cur == NULL) {
1296 xmlGenericError(xmlGenericErrorContext,
1297 "xmlNewPI : malloc failed\n");
1298 return(NULL);
1299 }
1300 memset(cur, 0, sizeof(xmlNode));
1301 cur->type = XML_PI_NODE;
1302
1303 cur->name = xmlStrdup(name);
1304 if (content != NULL) {
1305#ifndef XML_USE_BUFFER_CONTENT
1306 cur->content = xmlStrdup(content);
1307#else
1308 cur->content = xmlBufferCreateSize(0);
1309 xmlBufferSetAllocationScheme(cur->content,
1310 xmlGetBufferAllocationScheme());
1311 xmlBufferAdd(cur->content, content, -1);
1312#endif
1313 }
1314 return(cur);
1315}
1316
1317/**
1318 * xmlNewNode:
1319 * @ns: namespace if any
1320 * @name: the node name
1321 *
Daniel Veillardd1640922001-12-17 15:30:10 +00001322 * Creation of a new node element. @ns is optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00001323 *
1324 * Returns a pointer to the new node object.
1325 */
1326xmlNodePtr
1327xmlNewNode(xmlNsPtr ns, const xmlChar *name) {
1328 xmlNodePtr cur;
1329
1330 if (name == NULL) {
1331#ifdef DEBUG_TREE
1332 xmlGenericError(xmlGenericErrorContext,
1333 "xmlNewNode : name == NULL\n");
1334#endif
1335 return(NULL);
1336 }
1337
1338 /*
1339 * Allocate a new node and fill the fields.
1340 */
1341 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1342 if (cur == NULL) {
1343 xmlGenericError(xmlGenericErrorContext,
1344 "xmlNewNode : malloc failed\n");
1345 return(NULL);
1346 }
1347 memset(cur, 0, sizeof(xmlNode));
1348 cur->type = XML_ELEMENT_NODE;
1349
1350 cur->name = xmlStrdup(name);
1351 cur->ns = ns;
1352 return(cur);
1353}
1354
1355/**
1356 * xmlNewDocNode:
1357 * @doc: the document
1358 * @ns: namespace if any
1359 * @name: the node name
1360 * @content: the XML text content if any
1361 *
1362 * Creation of a new node element within a document. @ns and @content
Daniel Veillardd1640922001-12-17 15:30:10 +00001363 * are optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00001364 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
1365 * references, but XML special chars need to be escaped first by using
1366 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
1367 * need entities support.
1368 *
1369 * Returns a pointer to the new node object.
1370 */
1371xmlNodePtr
1372xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns,
1373 const xmlChar *name, const xmlChar *content) {
1374 xmlNodePtr cur;
1375
1376 cur = xmlNewNode(ns, name);
1377 if (cur != NULL) {
1378 cur->doc = doc;
1379 if (content != NULL) {
1380 cur->children = xmlStringGetNodeList(doc, content);
1381 UPDATE_LAST_CHILD_AND_PARENT(cur)
1382 }
1383 }
1384 return(cur);
1385}
1386
1387
1388/**
1389 * xmlNewDocRawNode:
1390 * @doc: the document
1391 * @ns: namespace if any
1392 * @name: the node name
1393 * @content: the text content if any
1394 *
1395 * Creation of a new node element within a document. @ns and @content
Daniel Veillardd1640922001-12-17 15:30:10 +00001396 * are optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00001397 *
1398 * Returns a pointer to the new node object.
1399 */
1400xmlNodePtr
1401xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns,
1402 const xmlChar *name, const xmlChar *content) {
1403 xmlNodePtr cur;
1404
1405 cur = xmlNewNode(ns, name);
1406 if (cur != NULL) {
1407 cur->doc = doc;
1408 if (content != NULL) {
1409 cur->children = xmlNewDocText(doc, content);
1410 UPDATE_LAST_CHILD_AND_PARENT(cur)
1411 }
1412 }
1413 return(cur);
1414}
1415
1416/**
1417 * xmlNewDocFragment:
1418 * @doc: the document owning the fragment
1419 *
1420 * Creation of a new Fragment node.
1421 * Returns a pointer to the new node object.
1422 */
1423xmlNodePtr
1424xmlNewDocFragment(xmlDocPtr doc) {
1425 xmlNodePtr cur;
1426
1427 /*
1428 * Allocate a new DocumentFragment node and fill the fields.
1429 */
1430 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1431 if (cur == NULL) {
1432 xmlGenericError(xmlGenericErrorContext,
1433 "xmlNewDocFragment : malloc failed\n");
1434 return(NULL);
1435 }
1436 memset(cur, 0, sizeof(xmlNode));
1437 cur->type = XML_DOCUMENT_FRAG_NODE;
1438
1439 cur->doc = doc;
1440 return(cur);
1441}
1442
1443/**
1444 * xmlNewText:
1445 * @content: the text content
1446 *
1447 * Creation of a new text node.
1448 * Returns a pointer to the new node object.
1449 */
1450xmlNodePtr
1451xmlNewText(const xmlChar *content) {
1452 xmlNodePtr cur;
1453
1454 /*
1455 * Allocate a new node and fill the fields.
1456 */
1457 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1458 if (cur == NULL) {
1459 xmlGenericError(xmlGenericErrorContext,
1460 "xmlNewText : malloc failed\n");
1461 return(NULL);
1462 }
1463 memset(cur, 0, sizeof(xmlNode));
1464 cur->type = XML_TEXT_NODE;
1465
1466 cur->name = xmlStringText;
1467 if (content != NULL) {
1468#ifndef XML_USE_BUFFER_CONTENT
1469 cur->content = xmlStrdup(content);
1470#else
1471 cur->content = xmlBufferCreateSize(0);
1472 xmlBufferSetAllocationScheme(cur->content,
1473 xmlGetBufferAllocationScheme());
1474 xmlBufferAdd(cur->content, content, -1);
1475#endif
1476 }
1477 return(cur);
1478}
1479
1480/**
1481 * xmlNewTextChild:
1482 * @parent: the parent node
1483 * @ns: a namespace if any
1484 * @name: the name of the child
1485 * @content: the text content of the child if any.
1486 *
1487 * Creation of a new child element, added at the end of @parent children list.
Daniel Veillardd1640922001-12-17 15:30:10 +00001488 * @ns and @content parameters are optional (NULL). If content is non NULL,
Owen Taylor3473f882001-02-23 17:55:21 +00001489 * a child TEXT node will be created containing the string content.
1490 *
1491 * Returns a pointer to the new node object.
1492 */
1493xmlNodePtr
1494xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns,
1495 const xmlChar *name, const xmlChar *content) {
1496 xmlNodePtr cur, prev;
1497
1498 if (parent == NULL) {
1499#ifdef DEBUG_TREE
1500 xmlGenericError(xmlGenericErrorContext,
1501 "xmlNewTextChild : parent == NULL\n");
1502#endif
1503 return(NULL);
1504 }
1505
1506 if (name == NULL) {
1507#ifdef DEBUG_TREE
1508 xmlGenericError(xmlGenericErrorContext,
1509 "xmlNewTextChild : name == NULL\n");
1510#endif
1511 return(NULL);
1512 }
1513
1514 /*
1515 * Allocate a new node
1516 */
1517 if (ns == NULL)
1518 cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content);
1519 else
1520 cur = xmlNewDocRawNode(parent->doc, ns, name, content);
1521 if (cur == NULL) return(NULL);
1522
1523 /*
1524 * add the new element at the end of the children list.
1525 */
1526 cur->type = XML_ELEMENT_NODE;
1527 cur->parent = parent;
1528 cur->doc = parent->doc;
1529 if (parent->children == NULL) {
1530 parent->children = cur;
1531 parent->last = cur;
1532 } else {
1533 prev = parent->last;
1534 prev->next = cur;
1535 cur->prev = prev;
1536 parent->last = cur;
1537 }
1538
1539 return(cur);
1540}
1541
1542/**
1543 * xmlNewCharRef:
1544 * @doc: the document
1545 * @name: the char ref string, starting with # or "&# ... ;"
1546 *
1547 * Creation of a new character reference node.
1548 * Returns a pointer to the new node object.
1549 */
1550xmlNodePtr
1551xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) {
1552 xmlNodePtr cur;
1553
1554 /*
1555 * Allocate a new node and fill the fields.
1556 */
1557 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1558 if (cur == NULL) {
1559 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00001560 "xmlNewCharRef : malloc failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001561 return(NULL);
1562 }
1563 memset(cur, 0, sizeof(xmlNode));
1564 cur->type = XML_ENTITY_REF_NODE;
1565
1566 cur->doc = doc;
1567 if (name[0] == '&') {
1568 int len;
1569 name++;
1570 len = xmlStrlen(name);
1571 if (name[len - 1] == ';')
1572 cur->name = xmlStrndup(name, len - 1);
1573 else
1574 cur->name = xmlStrndup(name, len);
1575 } else
1576 cur->name = xmlStrdup(name);
1577 return(cur);
1578}
1579
1580/**
1581 * xmlNewReference:
1582 * @doc: the document
1583 * @name: the reference name, or the reference string with & and ;
1584 *
1585 * Creation of a new reference node.
1586 * Returns a pointer to the new node object.
1587 */
1588xmlNodePtr
1589xmlNewReference(xmlDocPtr doc, const xmlChar *name) {
1590 xmlNodePtr cur;
1591 xmlEntityPtr ent;
1592
1593 /*
1594 * Allocate a new node and fill the fields.
1595 */
1596 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1597 if (cur == NULL) {
1598 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00001599 "xmlNewReference : malloc failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001600 return(NULL);
1601 }
1602 memset(cur, 0, sizeof(xmlNode));
1603 cur->type = XML_ENTITY_REF_NODE;
1604
1605 cur->doc = doc;
1606 if (name[0] == '&') {
1607 int len;
1608 name++;
1609 len = xmlStrlen(name);
1610 if (name[len - 1] == ';')
1611 cur->name = xmlStrndup(name, len - 1);
1612 else
1613 cur->name = xmlStrndup(name, len);
1614 } else
1615 cur->name = xmlStrdup(name);
1616
1617 ent = xmlGetDocEntity(doc, cur->name);
1618 if (ent != NULL) {
1619#ifndef XML_USE_BUFFER_CONTENT
1620 cur->content = ent->content;
1621#else
1622 /*
1623 * CJN 11.18.99 this might be a problem, since the xmlBuffer gets
1624 * a copy of this pointer. Let's hope we don't manipulate it
1625 * later
1626 */
1627 cur->content = xmlBufferCreateSize(0);
1628 xmlBufferSetAllocationScheme(cur->content,
1629 xmlGetBufferAllocationScheme());
1630 if (ent->content != NULL)
1631 xmlBufferAdd(cur->content, ent->content, -1);
1632#endif
1633 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001634 * The parent pointer in entity is a DTD pointer and thus is NOT
Owen Taylor3473f882001-02-23 17:55:21 +00001635 * updated. Not sure if this is 100% correct.
1636 * -George
1637 */
1638 cur->children = (xmlNodePtr) ent;
1639 cur->last = (xmlNodePtr) ent;
1640 }
1641 return(cur);
1642}
1643
1644/**
1645 * xmlNewDocText:
1646 * @doc: the document
1647 * @content: the text content
1648 *
1649 * Creation of a new text node within a document.
1650 * Returns a pointer to the new node object.
1651 */
1652xmlNodePtr
1653xmlNewDocText(xmlDocPtr doc, const xmlChar *content) {
1654 xmlNodePtr cur;
1655
1656 cur = xmlNewText(content);
1657 if (cur != NULL) cur->doc = doc;
1658 return(cur);
1659}
1660
1661/**
1662 * xmlNewTextLen:
1663 * @content: the text content
1664 * @len: the text len.
1665 *
Daniel Veillard60087f32001-10-10 09:45:09 +00001666 * Creation of a new text node with an extra parameter for the content's length
Owen Taylor3473f882001-02-23 17:55:21 +00001667 * Returns a pointer to the new node object.
1668 */
1669xmlNodePtr
1670xmlNewTextLen(const xmlChar *content, int len) {
1671 xmlNodePtr cur;
1672
1673 /*
1674 * Allocate a new node and fill the fields.
1675 */
1676 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1677 if (cur == NULL) {
1678 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00001679 "xmlNewTextLen : malloc failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001680 return(NULL);
1681 }
1682 memset(cur, 0, sizeof(xmlNode));
1683 cur->type = XML_TEXT_NODE;
1684
1685 cur->name = xmlStringText;
1686 if (content != NULL) {
1687#ifndef XML_USE_BUFFER_CONTENT
1688 cur->content = xmlStrndup(content, len);
1689#else
1690 cur->content = xmlBufferCreateSize(len);
1691 xmlBufferSetAllocationScheme(cur->content,
1692 xmlGetBufferAllocationScheme());
1693 xmlBufferAdd(cur->content, content, len);
1694#endif
1695 }
1696 return(cur);
1697}
1698
1699/**
1700 * xmlNewDocTextLen:
1701 * @doc: the document
1702 * @content: the text content
1703 * @len: the text len.
1704 *
Daniel Veillard60087f32001-10-10 09:45:09 +00001705 * Creation of a new text node with an extra content length parameter. The
Owen Taylor3473f882001-02-23 17:55:21 +00001706 * text node pertain to a given document.
1707 * Returns a pointer to the new node object.
1708 */
1709xmlNodePtr
1710xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) {
1711 xmlNodePtr cur;
1712
1713 cur = xmlNewTextLen(content, len);
1714 if (cur != NULL) cur->doc = doc;
1715 return(cur);
1716}
1717
1718/**
1719 * xmlNewComment:
1720 * @content: the comment content
1721 *
1722 * Creation of a new node containing a comment.
1723 * Returns a pointer to the new node object.
1724 */
1725xmlNodePtr
1726xmlNewComment(const xmlChar *content) {
1727 xmlNodePtr cur;
1728
1729 /*
1730 * Allocate a new node and fill the fields.
1731 */
1732 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1733 if (cur == NULL) {
1734 xmlGenericError(xmlGenericErrorContext,
1735 "xmlNewComment : malloc failed\n");
1736 return(NULL);
1737 }
1738 memset(cur, 0, sizeof(xmlNode));
1739 cur->type = XML_COMMENT_NODE;
1740
1741 cur->name = xmlStringComment;
1742 if (content != NULL) {
1743#ifndef XML_USE_BUFFER_CONTENT
1744 cur->content = xmlStrdup(content);
1745#else
1746 cur->content = xmlBufferCreateSize(0);
1747 xmlBufferSetAllocationScheme(cur->content,
1748 xmlGetBufferAllocationScheme());
1749 xmlBufferAdd(cur->content, content, -1);
1750#endif
1751 }
1752 return(cur);
1753}
1754
1755/**
1756 * xmlNewCDataBlock:
1757 * @doc: the document
Daniel Veillardd1640922001-12-17 15:30:10 +00001758 * @content: the CDATA block content content
Owen Taylor3473f882001-02-23 17:55:21 +00001759 * @len: the length of the block
1760 *
Daniel Veillardd1640922001-12-17 15:30:10 +00001761 * Creation of a new node containing a CDATA block.
Owen Taylor3473f882001-02-23 17:55:21 +00001762 * Returns a pointer to the new node object.
1763 */
1764xmlNodePtr
1765xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) {
1766 xmlNodePtr cur;
1767
1768 /*
1769 * Allocate a new node and fill the fields.
1770 */
1771 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1772 if (cur == NULL) {
1773 xmlGenericError(xmlGenericErrorContext,
1774 "xmlNewCDataBlock : malloc failed\n");
1775 return(NULL);
1776 }
1777 memset(cur, 0, sizeof(xmlNode));
1778 cur->type = XML_CDATA_SECTION_NODE;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001779 cur->doc = doc;
Owen Taylor3473f882001-02-23 17:55:21 +00001780
1781 if (content != NULL) {
1782#ifndef XML_USE_BUFFER_CONTENT
1783 cur->content = xmlStrndup(content, len);
1784#else
1785 cur->content = xmlBufferCreateSize(len);
1786 xmlBufferSetAllocationScheme(cur->content,
1787 xmlGetBufferAllocationScheme());
1788 xmlBufferAdd(cur->content, content, len);
1789#endif
1790 }
1791 return(cur);
1792}
1793
1794/**
1795 * xmlNewDocComment:
1796 * @doc: the document
1797 * @content: the comment content
1798 *
Daniel Veillardd1640922001-12-17 15:30:10 +00001799 * Creation of a new node containing a comment within a document.
Owen Taylor3473f882001-02-23 17:55:21 +00001800 * Returns a pointer to the new node object.
1801 */
1802xmlNodePtr
1803xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) {
1804 xmlNodePtr cur;
1805
1806 cur = xmlNewComment(content);
1807 if (cur != NULL) cur->doc = doc;
1808 return(cur);
1809}
1810
1811/**
1812 * xmlSetTreeDoc:
1813 * @tree: the top element
1814 * @doc: the document
1815 *
1816 * update all nodes under the tree to point to the right document
1817 */
1818void
1819xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) {
Daniel Veillard19e96c32001-07-09 10:32:59 +00001820 xmlAttrPtr prop;
1821
Owen Taylor3473f882001-02-23 17:55:21 +00001822 if (tree == NULL)
1823 return;
1824 if (tree->type == XML_ENTITY_DECL)
1825 return;
1826 if (tree->doc != doc) {
Daniel Veillard19e96c32001-07-09 10:32:59 +00001827 prop = tree->properties;
1828 while (prop != NULL) {
1829 prop->doc = doc;
1830 xmlSetListDoc(prop->children, doc);
1831 prop = prop->next;
1832 }
Owen Taylor3473f882001-02-23 17:55:21 +00001833 if (tree->children != NULL)
1834 xmlSetListDoc(tree->children, doc);
1835 tree->doc = doc;
1836 }
1837}
1838
1839/**
1840 * xmlSetListDoc:
Daniel Veillardd1640922001-12-17 15:30:10 +00001841 * @list: the first element
Owen Taylor3473f882001-02-23 17:55:21 +00001842 * @doc: the document
1843 *
1844 * update all nodes in the list to point to the right document
1845 */
1846void
1847xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) {
1848 xmlNodePtr cur;
1849
1850 if (list == NULL)
1851 return;
1852 cur = list;
1853 while (cur != NULL) {
1854 if (cur->doc != doc)
1855 xmlSetTreeDoc(cur, doc);
1856 cur = cur->next;
1857 }
1858}
1859
1860
1861/**
1862 * xmlNewChild:
1863 * @parent: the parent node
1864 * @ns: a namespace if any
1865 * @name: the name of the child
1866 * @content: the XML content of the child if any.
1867 *
1868 * Creation of a new child element, added at the end of @parent children list.
Daniel Veillardd1640922001-12-17 15:30:10 +00001869 * @ns and @content parameters are optional (NULL). If content is non NULL,
Owen Taylor3473f882001-02-23 17:55:21 +00001870 * a child list containing the TEXTs and ENTITY_REFs node will be created.
1871 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
1872 * references, but XML special chars need to be escaped first by using
1873 * xmlEncodeEntitiesReentrant(). Use xmlNewTextChild() if entities
1874 * support is not needed.
1875 *
1876 * Returns a pointer to the new node object.
1877 */
1878xmlNodePtr
1879xmlNewChild(xmlNodePtr parent, xmlNsPtr ns,
1880 const xmlChar *name, const xmlChar *content) {
1881 xmlNodePtr cur, prev;
1882
1883 if (parent == NULL) {
1884#ifdef DEBUG_TREE
1885 xmlGenericError(xmlGenericErrorContext,
1886 "xmlNewChild : parent == NULL\n");
1887#endif
1888 return(NULL);
1889 }
1890
1891 if (name == NULL) {
1892#ifdef DEBUG_TREE
1893 xmlGenericError(xmlGenericErrorContext,
1894 "xmlNewChild : name == NULL\n");
1895#endif
1896 return(NULL);
1897 }
1898
1899 /*
1900 * Allocate a new node
1901 */
1902 if (ns == NULL)
1903 cur = xmlNewDocNode(parent->doc, parent->ns, name, content);
1904 else
1905 cur = xmlNewDocNode(parent->doc, ns, name, content);
1906 if (cur == NULL) return(NULL);
1907
1908 /*
1909 * add the new element at the end of the children list.
1910 */
1911 cur->type = XML_ELEMENT_NODE;
1912 cur->parent = parent;
1913 cur->doc = parent->doc;
1914 if (parent->children == NULL) {
1915 parent->children = cur;
1916 parent->last = cur;
1917 } else {
1918 prev = parent->last;
1919 prev->next = cur;
1920 cur->prev = prev;
1921 parent->last = cur;
1922 }
1923
1924 return(cur);
1925}
1926
1927/**
1928 * xmlAddNextSibling:
1929 * @cur: the child node
1930 * @elem: the new node
1931 *
1932 * Add a new element @elem as the next siblings of @cur
1933 * If the new element was already inserted in a document it is
1934 * first unlinked from its existing context.
1935 * As a result of text merging @elem may be freed.
1936 *
1937 * Returns the new element or NULL in case of error.
1938 */
1939xmlNodePtr
1940xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) {
1941 if (cur == NULL) {
1942#ifdef DEBUG_TREE
1943 xmlGenericError(xmlGenericErrorContext,
1944 "xmlAddNextSibling : cur == NULL\n");
1945#endif
1946 return(NULL);
1947 }
1948 if (elem == NULL) {
1949#ifdef DEBUG_TREE
1950 xmlGenericError(xmlGenericErrorContext,
1951 "xmlAddNextSibling : elem == NULL\n");
1952#endif
1953 return(NULL);
1954 }
1955
1956 xmlUnlinkNode(elem);
1957
1958 if (elem->type == XML_TEXT_NODE) {
1959 if (cur->type == XML_TEXT_NODE) {
1960#ifndef XML_USE_BUFFER_CONTENT
1961 xmlNodeAddContent(cur, elem->content);
1962#else
1963 xmlNodeAddContent(cur, xmlBufferContent(elem->content));
1964#endif
1965 xmlFreeNode(elem);
1966 return(cur);
1967 }
Daniel Veillard9e1c72d2001-08-31 20:03:19 +00001968 if ((cur->next != NULL) && (cur->next->type == XML_TEXT_NODE) &&
1969 (cur->name == cur->next->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001970#ifndef XML_USE_BUFFER_CONTENT
1971 xmlChar *tmp;
1972
1973 tmp = xmlStrdup(elem->content);
1974 tmp = xmlStrcat(tmp, cur->next->content);
1975 xmlNodeSetContent(cur->next, tmp);
1976 xmlFree(tmp);
1977#else
1978 xmlBufferAddHead(cur->next->content,
1979 xmlBufferContent(elem->content),
1980 xmlBufferLength(elem->content));
1981#endif
1982 xmlFreeNode(elem);
1983 return(cur->next);
1984 }
1985 }
1986
1987 if (elem->doc != cur->doc) {
1988 xmlSetTreeDoc(elem, cur->doc);
1989 }
1990 elem->parent = cur->parent;
1991 elem->prev = cur;
1992 elem->next = cur->next;
1993 cur->next = elem;
1994 if (elem->next != NULL)
1995 elem->next->prev = elem;
1996 if ((elem->parent != NULL) && (elem->parent->last == cur))
1997 elem->parent->last = elem;
1998 return(elem);
1999}
2000
2001/**
2002 * xmlAddPrevSibling:
2003 * @cur: the child node
2004 * @elem: the new node
2005 *
2006 * Add a new element @elem as the previous siblings of @cur
2007 * merging adjacent TEXT nodes (@elem may be freed)
2008 * If the new element was already inserted in a document it is
2009 * first unlinked from its existing context.
2010 *
2011 * Returns the new element or NULL in case of error.
2012 */
2013xmlNodePtr
2014xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) {
2015 if (cur == NULL) {
2016#ifdef DEBUG_TREE
2017 xmlGenericError(xmlGenericErrorContext,
2018 "xmlAddPrevSibling : cur == NULL\n");
2019#endif
2020 return(NULL);
2021 }
2022 if (elem == NULL) {
2023#ifdef DEBUG_TREE
2024 xmlGenericError(xmlGenericErrorContext,
2025 "xmlAddPrevSibling : elem == NULL\n");
2026#endif
2027 return(NULL);
2028 }
2029
2030 xmlUnlinkNode(elem);
2031
2032 if (elem->type == XML_TEXT_NODE) {
2033 if (cur->type == XML_TEXT_NODE) {
2034#ifndef XML_USE_BUFFER_CONTENT
2035 xmlChar *tmp;
2036
2037 tmp = xmlStrdup(elem->content);
2038 tmp = xmlStrcat(tmp, cur->content);
2039 xmlNodeSetContent(cur, tmp);
2040 xmlFree(tmp);
2041#else
2042 xmlBufferAddHead(cur->content, xmlBufferContent(elem->content),
2043 xmlBufferLength(elem->content));
2044#endif
2045 xmlFreeNode(elem);
2046 return(cur);
2047 }
Daniel Veillard9e1c72d2001-08-31 20:03:19 +00002048 if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE) &&
2049 (cur->name == cur->prev->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002050#ifndef XML_USE_BUFFER_CONTENT
2051 xmlNodeAddContent(cur->prev, elem->content);
2052#else
2053 xmlNodeAddContent(cur->prev, xmlBufferContent(elem->content));
2054#endif
2055 xmlFreeNode(elem);
2056 return(cur->prev);
2057 }
2058 }
2059
2060 if (elem->doc != cur->doc) {
2061 xmlSetTreeDoc(elem, cur->doc);
2062 }
2063 elem->parent = cur->parent;
2064 elem->next = cur;
2065 elem->prev = cur->prev;
2066 cur->prev = elem;
2067 if (elem->prev != NULL)
2068 elem->prev->next = elem;
2069 if ((elem->parent != NULL) && (elem->parent->children == cur))
2070 elem->parent->children = elem;
2071 return(elem);
2072}
2073
2074/**
2075 * xmlAddSibling:
2076 * @cur: the child node
2077 * @elem: the new node
2078 *
2079 * Add a new element @elem to the list of siblings of @cur
2080 * merging adjacent TEXT nodes (@elem may be freed)
2081 * If the new element was already inserted in a document it is
2082 * first unlinked from its existing context.
2083 *
2084 * Returns the new element or NULL in case of error.
2085 */
2086xmlNodePtr
2087xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) {
2088 xmlNodePtr parent;
2089
2090 if (cur == NULL) {
2091#ifdef DEBUG_TREE
2092 xmlGenericError(xmlGenericErrorContext,
2093 "xmlAddSibling : cur == NULL\n");
2094#endif
2095 return(NULL);
2096 }
2097
2098 if (elem == NULL) {
2099#ifdef DEBUG_TREE
2100 xmlGenericError(xmlGenericErrorContext,
2101 "xmlAddSibling : elem == NULL\n");
2102#endif
2103 return(NULL);
2104 }
2105
2106 /*
2107 * Constant time is we can rely on the ->parent->last to find
2108 * the last sibling.
2109 */
2110 if ((cur->parent != NULL) &&
2111 (cur->parent->children != NULL) &&
2112 (cur->parent->last != NULL) &&
2113 (cur->parent->last->next == NULL)) {
2114 cur = cur->parent->last;
2115 } else {
2116 while (cur->next != NULL) cur = cur->next;
2117 }
2118
2119 xmlUnlinkNode(elem);
2120
2121 if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE)) {
2122#ifndef XML_USE_BUFFER_CONTENT
2123 xmlNodeAddContent(cur, elem->content);
2124#else
2125 xmlNodeAddContent(cur, xmlBufferContent(elem->content));
2126#endif
2127 xmlFreeNode(elem);
2128 return(cur);
2129 }
2130
2131 if (elem->doc != cur->doc) {
2132 xmlSetTreeDoc(elem, cur->doc);
2133 }
2134 parent = cur->parent;
2135 elem->prev = cur;
2136 elem->next = NULL;
2137 elem->parent = parent;
2138 cur->next = elem;
2139 if (parent != NULL)
2140 parent->last = elem;
2141
2142 return(elem);
2143}
2144
2145/**
2146 * xmlAddChildList:
2147 * @parent: the parent node
2148 * @cur: the first node in the list
2149 *
2150 * Add a list of node at the end of the child list of the parent
2151 * merging adjacent TEXT nodes (@cur may be freed)
2152 *
2153 * Returns the last child or NULL in case of error.
2154 */
2155xmlNodePtr
2156xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) {
2157 xmlNodePtr prev;
2158
2159 if (parent == NULL) {
2160#ifdef DEBUG_TREE
2161 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00002162 "xmlAddChildList : parent == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002163#endif
2164 return(NULL);
2165 }
2166
2167 if (cur == NULL) {
2168#ifdef DEBUG_TREE
2169 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00002170 "xmlAddChildList : child == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002171#endif
2172 return(NULL);
2173 }
2174
2175 if ((cur->doc != NULL) && (parent->doc != NULL) &&
2176 (cur->doc != parent->doc)) {
2177#ifdef DEBUG_TREE
2178 xmlGenericError(xmlGenericErrorContext,
2179 "Elements moved to a different document\n");
2180#endif
2181 }
2182
2183 /*
2184 * add the first element at the end of the children list.
2185 */
2186 if (parent->children == NULL) {
2187 parent->children = cur;
2188 } else {
2189 /*
2190 * If cur and parent->last both are TEXT nodes, then merge them.
2191 */
2192 if ((cur->type == XML_TEXT_NODE) &&
2193 (parent->last->type == XML_TEXT_NODE) &&
2194 (cur->name == parent->last->name)) {
2195#ifndef XML_USE_BUFFER_CONTENT
2196 xmlNodeAddContent(parent->last, cur->content);
2197#else
2198 xmlNodeAddContent(parent->last, xmlBufferContent(cur->content));
2199#endif
2200 /*
2201 * if it's the only child, nothing more to be done.
2202 */
2203 if (cur->next == NULL) {
2204 xmlFreeNode(cur);
2205 return(parent->last);
2206 }
2207 prev = cur;
2208 cur = cur->next;
2209 xmlFreeNode(prev);
2210 }
2211 prev = parent->last;
2212 prev->next = cur;
2213 cur->prev = prev;
2214 }
2215 while (cur->next != NULL) {
2216 cur->parent = parent;
2217 if (cur->doc != parent->doc) {
2218 xmlSetTreeDoc(cur, parent->doc);
2219 }
2220 cur = cur->next;
2221 }
2222 cur->parent = parent;
2223 cur->doc = parent->doc; /* the parent may not be linked to a doc ! */
2224 parent->last = cur;
2225
2226 return(cur);
2227}
2228
2229/**
2230 * xmlAddChild:
2231 * @parent: the parent node
2232 * @cur: the child node
2233 *
2234 * Add a new child element, to @parent, at the end of the child list
2235 * merging adjacent TEXT nodes (in which case @cur is freed)
2236 * Returns the child or NULL in case of error.
2237 */
2238xmlNodePtr
2239xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) {
2240 xmlNodePtr prev;
2241
2242 if (parent == NULL) {
2243#ifdef DEBUG_TREE
2244 xmlGenericError(xmlGenericErrorContext,
2245 "xmlAddChild : parent == NULL\n");
2246#endif
2247 return(NULL);
2248 }
2249
2250 if (cur == NULL) {
2251#ifdef DEBUG_TREE
2252 xmlGenericError(xmlGenericErrorContext,
2253 "xmlAddChild : child == NULL\n");
2254#endif
2255 return(NULL);
2256 }
2257
Owen Taylor3473f882001-02-23 17:55:21 +00002258 /*
2259 * If cur is a TEXT node, merge its content with adjacent TEXT nodes
Owen Taylor3473f882001-02-23 17:55:21 +00002260 * cur is then freed.
2261 */
2262 if (cur->type == XML_TEXT_NODE) {
Daniel Veillard7db37732001-07-12 01:20:08 +00002263 if ((parent->type == XML_TEXT_NODE) &&
Owen Taylor3473f882001-02-23 17:55:21 +00002264 (parent->content != NULL)) {
2265#ifndef XML_USE_BUFFER_CONTENT
2266 xmlNodeAddContent(parent, cur->content);
2267#else
2268 xmlNodeAddContent(parent, xmlBufferContent(cur->content));
2269#endif
2270 xmlFreeNode(cur);
2271 return(parent);
2272 }
2273 if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) &&
2274 (parent->last->name == cur->name)) {
2275#ifndef XML_USE_BUFFER_CONTENT
2276 xmlNodeAddContent(parent->last, cur->content);
2277#else
2278 xmlNodeAddContent(parent->last, xmlBufferContent(cur->content));
2279#endif
2280 xmlFreeNode(cur);
2281 return(parent->last);
2282 }
2283 }
2284
2285 /*
2286 * add the new element at the end of the children list.
2287 */
2288 cur->parent = parent;
2289 if (cur->doc != parent->doc) {
2290 xmlSetTreeDoc(cur, parent->doc);
2291 }
2292
2293 /*
Daniel Veillard7db37732001-07-12 01:20:08 +00002294 * Coalescing
Owen Taylor3473f882001-02-23 17:55:21 +00002295 */
Daniel Veillard7db37732001-07-12 01:20:08 +00002296 if ((parent->type == XML_TEXT_NODE) &&
Owen Taylor3473f882001-02-23 17:55:21 +00002297 (parent->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002298#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard7db37732001-07-12 01:20:08 +00002299 xmlNodeAddContent(parent, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002300#else
Daniel Veillard7db37732001-07-12 01:20:08 +00002301 xmlNodeAddContent(parent, xmlBufferContent(cur->content));
Owen Taylor3473f882001-02-23 17:55:21 +00002302#endif
Daniel Veillard7db37732001-07-12 01:20:08 +00002303 xmlFreeNode(cur);
2304 return(parent);
Owen Taylor3473f882001-02-23 17:55:21 +00002305 }
2306 if (parent->children == NULL) {
2307 parent->children = cur;
2308 parent->last = cur;
2309 } else {
2310 prev = parent->last;
2311 prev->next = cur;
2312 cur->prev = prev;
2313 parent->last = cur;
2314 }
2315
2316 return(cur);
2317}
2318
2319/**
2320 * xmlGetLastChild:
2321 * @parent: the parent node
2322 *
2323 * Search the last child of a node.
2324 * Returns the last child or NULL if none.
2325 */
2326xmlNodePtr
2327xmlGetLastChild(xmlNodePtr parent) {
2328 if (parent == NULL) {
2329#ifdef DEBUG_TREE
2330 xmlGenericError(xmlGenericErrorContext,
2331 "xmlGetLastChild : parent == NULL\n");
2332#endif
2333 return(NULL);
2334 }
2335 return(parent->last);
2336}
2337
2338/**
2339 * xmlFreeNodeList:
2340 * @cur: the first node in the list
2341 *
2342 * Free a node and all its siblings, this is a recursive behaviour, all
2343 * the children are freed too.
2344 */
2345void
2346xmlFreeNodeList(xmlNodePtr cur) {
2347 xmlNodePtr next;
2348 if (cur == NULL) {
2349#ifdef DEBUG_TREE
2350 xmlGenericError(xmlGenericErrorContext,
2351 "xmlFreeNodeList : node == NULL\n");
2352#endif
2353 return;
2354 }
2355 while (cur != NULL) {
2356 next = cur->next;
Daniel Veillard02141ea2001-04-30 11:46:40 +00002357 /* unroll to speed up freeing the document */
2358 if (cur->type != XML_DTD_NODE) {
2359 if ((cur->children != NULL) &&
2360 (cur->type != XML_ENTITY_REF_NODE))
2361 xmlFreeNodeList(cur->children);
2362 if (cur->properties != NULL)
2363 xmlFreePropList(cur->properties);
Daniel Veillard7db37732001-07-12 01:20:08 +00002364 if ((cur->type != XML_ELEMENT_NODE) &&
2365 (cur->type != XML_XINCLUDE_START) &&
2366 (cur->type != XML_XINCLUDE_END) &&
2367 (cur->type != XML_ENTITY_REF_NODE)) {
Daniel Veillard02141ea2001-04-30 11:46:40 +00002368#ifndef XML_USE_BUFFER_CONTENT
2369 if (cur->content != NULL) xmlFree(cur->content);
2370#else
2371 if (cur->content != NULL) xmlBufferFree(cur->content);
2372#endif
Daniel Veillard7db37732001-07-12 01:20:08 +00002373 }
2374 if (((cur->type == XML_ELEMENT_NODE) ||
2375 (cur->type == XML_XINCLUDE_START) ||
2376 (cur->type == XML_XINCLUDE_END)) &&
2377 (cur->nsDef != NULL))
2378 xmlFreeNsList(cur->nsDef);
2379
Daniel Veillard9cc6dc62001-06-11 08:09:20 +00002380 /*
2381 * When a node is a text node or a comment, it uses a global static
2382 * variable for the name of the node.
2383 *
2384 * The xmlStrEqual comparisons need to be done when (happened with
2385 * XML::libXML and XML::libXSLT) the library is included twice
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002386 * statically in the binary and a tree allocated by one occurrence
Daniel Veillardd1640922001-12-17 15:30:10 +00002387 * of the lib gets freed by the other occurrence, in this case
Daniel Veillard9cc6dc62001-06-11 08:09:20 +00002388 * the string addresses compare are not sufficient.
2389 */
Daniel Veillard02141ea2001-04-30 11:46:40 +00002390 if ((cur->name != NULL) &&
2391 (cur->name != xmlStringText) &&
2392 (cur->name != xmlStringTextNoenc) &&
Daniel Veillard9cc6dc62001-06-11 08:09:20 +00002393 (cur->name != xmlStringComment)) {
2394 if (cur->type == XML_TEXT_NODE) {
2395 if ((!xmlStrEqual(cur->name, xmlStringText)) &&
2396 (!xmlStrEqual(cur->name, xmlStringTextNoenc)))
2397 xmlFree((char *) cur->name);
2398 } else if (cur->type == XML_COMMENT_NODE) {
2399 if (!xmlStrEqual(cur->name, xmlStringComment))
2400 xmlFree((char *) cur->name);
2401 } else
2402 xmlFree((char *) cur->name);
2403 }
Daniel Veillard02141ea2001-04-30 11:46:40 +00002404 /* TODO : derecursivate this function */
Daniel Veillard02141ea2001-04-30 11:46:40 +00002405 xmlFree(cur);
2406 }
Owen Taylor3473f882001-02-23 17:55:21 +00002407 cur = next;
2408 }
2409}
2410
2411/**
2412 * xmlFreeNode:
2413 * @cur: the node
2414 *
2415 * Free a node, this is a recursive behaviour, all the children are freed too.
2416 * This doesn't unlink the child from the list, use xmlUnlinkNode() first.
2417 */
2418void
2419xmlFreeNode(xmlNodePtr cur) {
2420 if (cur == NULL) {
2421#ifdef DEBUG_TREE
2422 xmlGenericError(xmlGenericErrorContext,
2423 "xmlFreeNode : node == NULL\n");
2424#endif
2425 return;
2426 }
Daniel Veillard02141ea2001-04-30 11:46:40 +00002427 /* use xmlFreeDtd for DTD nodes */
Owen Taylor3473f882001-02-23 17:55:21 +00002428 if (cur->type == XML_DTD_NODE)
2429 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002430 if ((cur->children != NULL) &&
2431 (cur->type != XML_ENTITY_REF_NODE))
2432 xmlFreeNodeList(cur->children);
Daniel Veillard02141ea2001-04-30 11:46:40 +00002433 if (cur->properties != NULL)
2434 xmlFreePropList(cur->properties);
Daniel Veillard7db37732001-07-12 01:20:08 +00002435 if ((cur->type != XML_ELEMENT_NODE) &&
2436 (cur->content != NULL) &&
2437 (cur->type != XML_ENTITY_REF_NODE) &&
2438 (cur->type != XML_XINCLUDE_END) &&
2439 (cur->type != XML_XINCLUDE_START)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002440#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard7db37732001-07-12 01:20:08 +00002441 xmlFree(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002442#else
Daniel Veillard7db37732001-07-12 01:20:08 +00002443 xmlBufferFree(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002444#endif
Daniel Veillard7db37732001-07-12 01:20:08 +00002445 }
2446
Daniel Veillardacd370f2001-06-09 17:17:51 +00002447 /*
2448 * When a node is a text node or a comment, it uses a global static
2449 * variable for the name of the node.
2450 *
2451 * The xmlStrEqual comparisons need to be done when (happened with
2452 * XML::libXML and XML::libXSLT) the library is included twice statically
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002453 * in the binary and a tree allocated by one occurence of the lib gets
Daniel Veillardd1640922001-12-17 15:30:10 +00002454 * freed by the other occurrence, in this case the string addresses compare
Daniel Veillardacd370f2001-06-09 17:17:51 +00002455 * are not sufficient.
2456 */
Owen Taylor3473f882001-02-23 17:55:21 +00002457 if ((cur->name != NULL) &&
2458 (cur->name != xmlStringText) &&
2459 (cur->name != xmlStringTextNoenc) &&
Daniel Veillardacd370f2001-06-09 17:17:51 +00002460 (cur->name != xmlStringComment)) {
2461 if (cur->type == XML_TEXT_NODE) {
2462 if ((!xmlStrEqual(cur->name, xmlStringText)) &&
2463 (!xmlStrEqual(cur->name, xmlStringTextNoenc)))
2464 xmlFree((char *) cur->name);
2465 } else if (cur->type == XML_COMMENT_NODE) {
2466 if (!xmlStrEqual(cur->name, xmlStringComment))
2467 xmlFree((char *) cur->name);
2468 } else
2469 xmlFree((char *) cur->name);
2470 }
2471
Owen Taylor3473f882001-02-23 17:55:21 +00002472 if (cur->nsDef != NULL) xmlFreeNsList(cur->nsDef);
Owen Taylor3473f882001-02-23 17:55:21 +00002473 xmlFree(cur);
2474}
2475
2476/**
2477 * xmlUnlinkNode:
2478 * @cur: the node
2479 *
2480 * Unlink a node from it's current context, the node is not freed
2481 */
2482void
2483xmlUnlinkNode(xmlNodePtr cur) {
2484 if (cur == NULL) {
2485#ifdef DEBUG_TREE
2486 xmlGenericError(xmlGenericErrorContext,
2487 "xmlUnlinkNode : node == NULL\n");
2488#endif
2489 return;
2490 }
Daniel Veillard29e43992001-12-13 22:21:58 +00002491 if (cur->type == XML_DTD_NODE) {
2492 xmlDocPtr doc;
2493 doc = cur->doc;
2494 if (doc->intSubset == (xmlDtdPtr) cur)
2495 doc->intSubset = NULL;
2496 if (doc->extSubset == (xmlDtdPtr) cur)
2497 doc->extSubset = NULL;
2498 }
Owen Taylor3473f882001-02-23 17:55:21 +00002499 if ((cur->parent != NULL) && (cur->parent->children == cur))
2500 cur->parent->children = cur->next;
2501 if ((cur->parent != NULL) && (cur->parent->last == cur))
2502 cur->parent->last = cur->prev;
2503 if (cur->next != NULL)
2504 cur->next->prev = cur->prev;
2505 if (cur->prev != NULL)
2506 cur->prev->next = cur->next;
2507 cur->next = cur->prev = NULL;
2508 cur->parent = NULL;
2509}
2510
2511/**
2512 * xmlReplaceNode:
2513 * @old: the old node
2514 * @cur: the node
2515 *
2516 * Unlink the old node from it's current context, prune the new one
Daniel Veillardd1640922001-12-17 15:30:10 +00002517 * at the same place. If @cur was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00002518 * first unlinked from its existing context.
2519 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002520 * Returns the @old node
Owen Taylor3473f882001-02-23 17:55:21 +00002521 */
2522xmlNodePtr
2523xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) {
2524 if (old == NULL) {
2525#ifdef DEBUG_TREE
2526 xmlGenericError(xmlGenericErrorContext,
2527 "xmlReplaceNode : old == NULL\n");
2528#endif
2529 return(NULL);
2530 }
2531 if (cur == NULL) {
2532 xmlUnlinkNode(old);
2533 return(old);
2534 }
2535 if (cur == old) {
2536 return(old);
2537 }
2538 xmlUnlinkNode(cur);
2539 cur->doc = old->doc;
2540 cur->parent = old->parent;
2541 cur->next = old->next;
2542 if (cur->next != NULL)
2543 cur->next->prev = cur;
2544 cur->prev = old->prev;
2545 if (cur->prev != NULL)
2546 cur->prev->next = cur;
2547 if (cur->parent != NULL) {
2548 if (cur->parent->children == old)
2549 cur->parent->children = cur;
2550 if (cur->parent->last == old)
2551 cur->parent->last = cur;
2552 }
2553 old->next = old->prev = NULL;
2554 old->parent = NULL;
2555 return(old);
2556}
2557
2558/************************************************************************
2559 * *
2560 * Copy operations *
2561 * *
2562 ************************************************************************/
2563
2564/**
2565 * xmlCopyNamespace:
2566 * @cur: the namespace
2567 *
2568 * Do a copy of the namespace.
2569 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002570 * Returns: a new #xmlNsPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002571 */
2572xmlNsPtr
2573xmlCopyNamespace(xmlNsPtr cur) {
2574 xmlNsPtr ret;
2575
2576 if (cur == NULL) return(NULL);
2577 switch (cur->type) {
2578 case XML_LOCAL_NAMESPACE:
2579 ret = xmlNewNs(NULL, cur->href, cur->prefix);
2580 break;
2581 default:
2582#ifdef DEBUG_TREE
2583 xmlGenericError(xmlGenericErrorContext,
2584 "xmlCopyNamespace: invalid type %d\n", cur->type);
2585#endif
2586 return(NULL);
2587 }
2588 return(ret);
2589}
2590
2591/**
2592 * xmlCopyNamespaceList:
2593 * @cur: the first namespace
2594 *
2595 * Do a copy of an namespace list.
2596 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002597 * Returns: a new #xmlNsPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002598 */
2599xmlNsPtr
2600xmlCopyNamespaceList(xmlNsPtr cur) {
2601 xmlNsPtr ret = NULL;
2602 xmlNsPtr p = NULL,q;
2603
2604 while (cur != NULL) {
2605 q = xmlCopyNamespace(cur);
2606 if (p == NULL) {
2607 ret = p = q;
2608 } else {
2609 p->next = q;
2610 p = q;
2611 }
2612 cur = cur->next;
2613 }
2614 return(ret);
2615}
2616
2617static xmlNodePtr
2618xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent);
2619/**
2620 * xmlCopyProp:
2621 * @target: the element where the attribute will be grafted
2622 * @cur: the attribute
2623 *
2624 * Do a copy of the attribute.
2625 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002626 * Returns: a new #xmlAttrPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002627 */
2628xmlAttrPtr
2629xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) {
2630 xmlAttrPtr ret;
2631
2632 if (cur == NULL) return(NULL);
2633 if (target != NULL)
2634 ret = xmlNewDocProp(target->doc, cur->name, NULL);
2635 else if (cur->parent != NULL)
2636 ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL);
2637 else if (cur->children != NULL)
2638 ret = xmlNewDocProp(cur->children->doc, cur->name, NULL);
2639 else
2640 ret = xmlNewDocProp(NULL, cur->name, NULL);
2641 if (ret == NULL) return(NULL);
2642 ret->parent = target;
2643
2644 if ((cur->ns != NULL) && (target != NULL)) {
2645 xmlNsPtr ns;
2646
2647 ns = xmlSearchNs(target->doc, target, cur->ns->prefix);
2648 ret->ns = ns;
2649 } else
2650 ret->ns = NULL;
2651
2652 if (cur->children != NULL) {
2653 xmlNodePtr tmp;
2654
2655 ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret);
2656 ret->last = NULL;
2657 tmp = ret->children;
2658 while (tmp != NULL) {
2659 /* tmp->parent = (xmlNodePtr)ret; */
2660 if (tmp->next == NULL)
2661 ret->last = tmp;
2662 tmp = tmp->next;
2663 }
2664 }
2665 return(ret);
2666}
2667
2668/**
2669 * xmlCopyPropList:
2670 * @target: the element where the attributes will be grafted
2671 * @cur: the first attribute
2672 *
2673 * Do a copy of an attribute list.
2674 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002675 * Returns: a new #xmlAttrPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002676 */
2677xmlAttrPtr
2678xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) {
2679 xmlAttrPtr ret = NULL;
2680 xmlAttrPtr p = NULL,q;
2681
2682 while (cur != NULL) {
2683 q = xmlCopyProp(target, cur);
2684 if (p == NULL) {
2685 ret = p = q;
2686 } else {
2687 p->next = q;
2688 q->prev = p;
2689 p = q;
2690 }
2691 cur = cur->next;
2692 }
2693 return(ret);
2694}
2695
2696/*
Daniel Veillardd1640922001-12-17 15:30:10 +00002697 * NOTE about the CopyNode operations !
Owen Taylor3473f882001-02-23 17:55:21 +00002698 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002699 * They are split into external and internal parts for one
Owen Taylor3473f882001-02-23 17:55:21 +00002700 * tricky reason: namespaces. Doing a direct copy of a node
2701 * say RPM:Copyright without changing the namespace pointer to
2702 * something else can produce stale links. One way to do it is
2703 * to keep a reference counter but this doesn't work as soon
2704 * as one move the element or the subtree out of the scope of
2705 * the existing namespace. The actual solution seems to add
2706 * a copy of the namespace at the top of the copied tree if
2707 * not available in the subtree.
2708 * Hence two functions, the public front-end call the inner ones
2709 */
2710
2711static xmlNodePtr
2712xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent);
2713
2714static xmlNodePtr
Daniel Veillard3ec4c612001-08-28 20:39:49 +00002715xmlStaticCopyNode(const xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent,
Owen Taylor3473f882001-02-23 17:55:21 +00002716 int recursive) {
2717 xmlNodePtr ret;
2718
2719 if (node == NULL) return(NULL);
Daniel Veillard39196eb2001-06-19 18:09:42 +00002720 switch (node->type) {
2721 case XML_TEXT_NODE:
2722 case XML_CDATA_SECTION_NODE:
2723 case XML_ELEMENT_NODE:
2724 case XML_ENTITY_REF_NODE:
2725 case XML_ENTITY_NODE:
2726 case XML_PI_NODE:
2727 case XML_COMMENT_NODE:
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00002728 case XML_XINCLUDE_START:
2729 case XML_XINCLUDE_END:
2730 break;
2731 case XML_ATTRIBUTE_NODE:
2732 return((xmlNodePtr) xmlCopyProp(parent, (xmlAttrPtr) node));
2733 case XML_NAMESPACE_DECL:
2734 return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node));
2735
Daniel Veillard39196eb2001-06-19 18:09:42 +00002736 case XML_DOCUMENT_NODE:
2737 case XML_HTML_DOCUMENT_NODE:
2738#ifdef LIBXML_DOCB_ENABLED
2739 case XML_DOCB_DOCUMENT_NODE:
2740#endif
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00002741 return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, recursive));
Daniel Veillard39196eb2001-06-19 18:09:42 +00002742 case XML_DOCUMENT_TYPE_NODE:
2743 case XML_DOCUMENT_FRAG_NODE:
2744 case XML_NOTATION_NODE:
2745 case XML_DTD_NODE:
2746 case XML_ELEMENT_DECL:
2747 case XML_ATTRIBUTE_DECL:
2748 case XML_ENTITY_DECL:
2749 return(NULL);
2750 }
Daniel Veillardb33c2012001-04-25 12:59:04 +00002751
Owen Taylor3473f882001-02-23 17:55:21 +00002752 /*
2753 * Allocate a new node and fill the fields.
2754 */
2755 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2756 if (ret == NULL) {
2757 xmlGenericError(xmlGenericErrorContext,
2758 "xmlStaticCopyNode : malloc failed\n");
2759 return(NULL);
2760 }
2761 memset(ret, 0, sizeof(xmlNode));
2762 ret->type = node->type;
2763
2764 ret->doc = doc;
2765 ret->parent = parent;
2766 if (node->name == xmlStringText)
2767 ret->name = xmlStringText;
2768 else if (node->name == xmlStringTextNoenc)
2769 ret->name = xmlStringTextNoenc;
2770 else if (node->name == xmlStringComment)
2771 ret->name = xmlStringComment;
2772 else if (node->name != NULL)
2773 ret->name = xmlStrdup(node->name);
Daniel Veillard7db37732001-07-12 01:20:08 +00002774 if ((node->type != XML_ELEMENT_NODE) &&
2775 (node->content != NULL) &&
2776 (node->type != XML_ENTITY_REF_NODE) &&
2777 (node->type != XML_XINCLUDE_END) &&
2778 (node->type != XML_XINCLUDE_START)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002779#ifndef XML_USE_BUFFER_CONTENT
2780 ret->content = xmlStrdup(node->content);
2781#else
2782 ret->content = xmlBufferCreateSize(xmlBufferLength(node->content));
2783 xmlBufferSetAllocationScheme(ret->content,
2784 xmlGetBufferAllocationScheme());
2785 xmlBufferAdd(ret->content,
2786 xmlBufferContent(node->content),
2787 xmlBufferLength(node->content));
2788#endif
2789 }
2790 if (parent != NULL)
2791 xmlAddChild(parent, ret);
2792
2793 if (!recursive) return(ret);
2794 if (node->nsDef != NULL)
2795 ret->nsDef = xmlCopyNamespaceList(node->nsDef);
2796
2797 if (node->ns != NULL) {
2798 xmlNsPtr ns;
2799
2800 ns = xmlSearchNs(doc, ret, node->ns->prefix);
2801 if (ns == NULL) {
2802 /*
2803 * Humm, we are copying an element whose namespace is defined
2804 * out of the new tree scope. Search it in the original tree
2805 * and add it at the top of the new tree
2806 */
2807 ns = xmlSearchNs(node->doc, node, node->ns->prefix);
2808 if (ns != NULL) {
2809 xmlNodePtr root = ret;
2810
2811 while (root->parent != NULL) root = root->parent;
Daniel Veillarde82a9922001-04-22 12:12:58 +00002812 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00002813 }
2814 } else {
2815 /*
2816 * reference the existing namespace definition in our own tree.
2817 */
2818 ret->ns = ns;
2819 }
2820 }
2821 if (node->properties != NULL)
2822 ret->properties = xmlCopyPropList(ret, node->properties);
Daniel Veillardb33c2012001-04-25 12:59:04 +00002823 if (node->type == XML_ENTITY_REF_NODE) {
2824 if ((doc == NULL) || (node->doc != doc)) {
2825 /*
2826 * The copied node will go into a separate document, so
Daniel Veillardd1640922001-12-17 15:30:10 +00002827 * to avoid dangling references to the ENTITY_DECL node
Daniel Veillardb33c2012001-04-25 12:59:04 +00002828 * we cannot keep the reference. Try to find it in the
2829 * target document.
2830 */
2831 ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name);
2832 } else {
2833 ret->children = node->children;
2834 }
Daniel Veillard0ec98632001-11-14 15:04:32 +00002835 ret->last = ret->children;
2836 } else if (node->children != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002837 ret->children = xmlStaticCopyNodeList(node->children, doc, ret);
Daniel Veillard0ec98632001-11-14 15:04:32 +00002838 UPDATE_LAST_CHILD_AND_PARENT(ret)
2839 }
Owen Taylor3473f882001-02-23 17:55:21 +00002840 return(ret);
2841}
2842
2843static xmlNodePtr
2844xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) {
2845 xmlNodePtr ret = NULL;
2846 xmlNodePtr p = NULL,q;
2847
2848 while (node != NULL) {
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00002849 if (node->type == XML_DTD_NODE ) {
Daniel Veillard4497e692001-06-09 14:19:02 +00002850 if (doc == NULL) {
2851 node = node->next;
2852 continue;
2853 }
Daniel Veillardb33c2012001-04-25 12:59:04 +00002854 if (doc->intSubset == NULL) {
2855 q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node );
2856 q->doc = doc;
2857 q->parent = parent;
2858 doc->intSubset = (xmlDtdPtr) q;
2859 } else {
2860 q = (xmlNodePtr) doc->intSubset;
2861 }
2862 } else
2863 q = xmlStaticCopyNode(node, doc, parent, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00002864 if (ret == NULL) {
2865 q->prev = NULL;
2866 ret = p = q;
2867 } else {
2868 p->next = q;
2869 q->prev = p;
2870 p = q;
2871 }
2872 node = node->next;
2873 }
2874 return(ret);
2875}
2876
2877/**
2878 * xmlCopyNode:
2879 * @node: the node
2880 * @recursive: if 1 do a recursive copy.
2881 *
2882 * Do a copy of the node.
2883 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002884 * Returns: a new #xmlNodePtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002885 */
2886xmlNodePtr
Daniel Veillard3ec4c612001-08-28 20:39:49 +00002887xmlCopyNode(const xmlNodePtr node, int recursive) {
Owen Taylor3473f882001-02-23 17:55:21 +00002888 xmlNodePtr ret;
2889
2890 ret = xmlStaticCopyNode(node, NULL, NULL, recursive);
2891 return(ret);
2892}
2893
2894/**
Daniel Veillard82daa812001-04-12 08:55:36 +00002895 * xmlDocCopyNode:
2896 * @node: the node
Daniel Veillardd1640922001-12-17 15:30:10 +00002897 * @doc: the document
Daniel Veillard82daa812001-04-12 08:55:36 +00002898 * @recursive: if 1 do a recursive copy.
2899 *
2900 * Do a copy of the node to a given document.
2901 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002902 * Returns: a new #xmlNodePtr, or NULL in case of error.
Daniel Veillard82daa812001-04-12 08:55:36 +00002903 */
2904xmlNodePtr
Daniel Veillard3ec4c612001-08-28 20:39:49 +00002905xmlDocCopyNode(const xmlNodePtr node, xmlDocPtr doc, int recursive) {
Daniel Veillard82daa812001-04-12 08:55:36 +00002906 xmlNodePtr ret;
2907
2908 ret = xmlStaticCopyNode(node, doc, NULL, recursive);
2909 return(ret);
2910}
2911
2912/**
Owen Taylor3473f882001-02-23 17:55:21 +00002913 * xmlCopyNodeList:
2914 * @node: the first node in the list.
2915 *
2916 * Do a recursive copy of the node list.
2917 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002918 * Returns: a new #xmlNodePtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002919 */
Daniel Veillard3ec4c612001-08-28 20:39:49 +00002920xmlNodePtr xmlCopyNodeList(const xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00002921 xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL);
2922 return(ret);
2923}
2924
2925/**
Owen Taylor3473f882001-02-23 17:55:21 +00002926 * xmlCopyDtd:
2927 * @dtd: the dtd
2928 *
2929 * Do a copy of the dtd.
2930 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002931 * Returns: a new #xmlDtdPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002932 */
2933xmlDtdPtr
2934xmlCopyDtd(xmlDtdPtr dtd) {
2935 xmlDtdPtr ret;
2936
2937 if (dtd == NULL) return(NULL);
2938 ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID);
2939 if (ret == NULL) return(NULL);
2940 if (dtd->entities != NULL)
2941 ret->entities = (void *) xmlCopyEntitiesTable(
2942 (xmlEntitiesTablePtr) dtd->entities);
2943 if (dtd->notations != NULL)
2944 ret->notations = (void *) xmlCopyNotationTable(
2945 (xmlNotationTablePtr) dtd->notations);
2946 if (dtd->elements != NULL)
2947 ret->elements = (void *) xmlCopyElementTable(
2948 (xmlElementTablePtr) dtd->elements);
2949 if (dtd->attributes != NULL)
2950 ret->attributes = (void *) xmlCopyAttributeTable(
2951 (xmlAttributeTablePtr) dtd->attributes);
2952 return(ret);
2953}
2954
2955/**
2956 * xmlCopyDoc:
2957 * @doc: the document
2958 * @recursive: if 1 do a recursive copy.
2959 *
2960 * Do a copy of the document info. If recursive, the content tree will
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002961 * be copied too as well as DTD, namespaces and entities.
Owen Taylor3473f882001-02-23 17:55:21 +00002962 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002963 * Returns: a new #xmlDocPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002964 */
2965xmlDocPtr
2966xmlCopyDoc(xmlDocPtr doc, int recursive) {
2967 xmlDocPtr ret;
2968
2969 if (doc == NULL) return(NULL);
2970 ret = xmlNewDoc(doc->version);
2971 if (ret == NULL) return(NULL);
2972 if (doc->name != NULL)
2973 ret->name = xmlMemStrdup(doc->name);
2974 if (doc->encoding != NULL)
2975 ret->encoding = xmlStrdup(doc->encoding);
2976 ret->charset = doc->charset;
2977 ret->compression = doc->compression;
2978 ret->standalone = doc->standalone;
2979 if (!recursive) return(ret);
2980
Daniel Veillardb33c2012001-04-25 12:59:04 +00002981 ret->last = NULL;
2982 ret->children = NULL;
2983 if (doc->intSubset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002984 ret->intSubset = xmlCopyDtd(doc->intSubset);
Daniel Veillardb33c2012001-04-25 12:59:04 +00002985 ret->intSubset->doc = ret;
2986 ret->intSubset->parent = ret;
2987 }
Owen Taylor3473f882001-02-23 17:55:21 +00002988 if (doc->oldNs != NULL)
2989 ret->oldNs = xmlCopyNamespaceList(doc->oldNs);
2990 if (doc->children != NULL) {
2991 xmlNodePtr tmp;
Daniel Veillardb33c2012001-04-25 12:59:04 +00002992
2993 ret->children = xmlStaticCopyNodeList(doc->children, ret,
2994 (xmlNodePtr)ret);
Owen Taylor3473f882001-02-23 17:55:21 +00002995 ret->last = NULL;
2996 tmp = ret->children;
2997 while (tmp != NULL) {
2998 if (tmp->next == NULL)
2999 ret->last = tmp;
3000 tmp = tmp->next;
3001 }
3002 }
3003 return(ret);
3004}
3005
3006/************************************************************************
3007 * *
3008 * Content access functions *
3009 * *
3010 ************************************************************************/
3011
3012/**
Daniel Veillard8faa7832001-11-26 15:58:08 +00003013 * xmlGetLineNo:
3014 * @node : valid node
3015 *
3016 * Get line number of node. this requires activation of this option
Daniel Veillardd1640922001-12-17 15:30:10 +00003017 * before invoking the parser by calling xmlLineNumbersDefault(1)
Daniel Veillard8faa7832001-11-26 15:58:08 +00003018 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003019 * Returns the line number if successful, -1 otherwise
Daniel Veillard8faa7832001-11-26 15:58:08 +00003020 */
3021long
3022xmlGetLineNo(xmlNodePtr node)
3023{
3024 long result = -1;
3025
3026 if (!node)
3027 return result;
3028 if (node->type == XML_ELEMENT_NODE)
3029 result = (long) node->content;
3030 else if ((node->prev != NULL) &&
3031 ((node->prev->type == XML_ELEMENT_NODE) ||
3032 (node->prev->type == XML_TEXT_NODE)))
3033 result = xmlGetLineNo(node->prev);
3034 else if ((node->parent != NULL) &&
3035 ((node->parent->type == XML_ELEMENT_NODE) ||
3036 (node->parent->type == XML_TEXT_NODE)))
3037 result = xmlGetLineNo(node->parent);
3038
3039 return result;
3040}
3041
3042/**
3043 * xmlGetNodePath:
3044 * @node: a node
3045 *
3046 * Build a structure based Path for the given node
3047 *
3048 * Returns the new path or NULL in case of error. The caller must free
3049 * the returned string
3050 */
3051xmlChar *
3052xmlGetNodePath(xmlNodePtr node)
3053{
3054 xmlNodePtr cur, tmp, next;
3055 xmlChar *buffer = NULL, *temp;
3056 size_t buf_len;
3057 xmlChar *buf;
3058 char sep;
3059 const char *name;
3060 char nametemp[100];
3061 int occur = 0;
3062
3063 if (node == NULL)
3064 return (NULL);
3065
3066 buf_len = 500;
3067 buffer = (xmlChar *) xmlMalloc(buf_len * sizeof(xmlChar));
3068 if (buffer == NULL)
3069 return (NULL);
3070 buf = (xmlChar *) xmlMalloc(buf_len * sizeof(xmlChar));
3071 if (buf == NULL) {
3072 xmlFree(buffer);
3073 return (NULL);
3074 }
3075
3076 buffer[0] = 0;
3077 cur = node;
3078 do {
3079 name = "";
3080 sep = '?';
3081 occur = 0;
3082 if ((cur->type == XML_DOCUMENT_NODE) ||
3083 (cur->type == XML_HTML_DOCUMENT_NODE)) {
3084 if (buffer[0] == '/')
3085 break;
3086 sep = '/';
3087 next = NULL;
3088 } else if (cur->type == XML_ELEMENT_NODE) {
3089 sep = '/';
3090 name = (const char *) cur->name;
3091 if (cur->ns) {
3092 snprintf(nametemp, sizeof(nametemp) - 1,
3093 "%s:%s", cur->ns->prefix, cur->name);
3094 nametemp[sizeof(nametemp) - 1] = 0;
3095 name = nametemp;
3096 }
3097 next = cur->parent;
3098
3099 /*
3100 * Thumbler index computation
3101 */
3102 tmp = cur->prev;
3103 while (tmp != NULL) {
3104 if (xmlStrEqual(cur->name, tmp->name))
3105 occur++;
3106 tmp = tmp->prev;
3107 }
3108 if (occur == 0) {
3109 tmp = cur->next;
3110 while (tmp != NULL) {
3111 if (xmlStrEqual(cur->name, tmp->name))
3112 occur++;
3113 tmp = tmp->next;
3114 }
3115 if (occur != 0)
3116 occur = 1;
3117 } else
3118 occur++;
3119 } else if (cur->type == XML_ATTRIBUTE_NODE) {
3120 sep = '@';
3121 name = (const char *) (((xmlAttrPtr) cur)->name);
3122 next = ((xmlAttrPtr) cur)->parent;
3123 } else {
3124 next = cur->parent;
3125 }
3126
3127 /*
3128 * Make sure there is enough room
3129 */
3130 if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) {
3131 buf_len =
3132 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20;
3133 temp = (xmlChar *) xmlRealloc(buffer, buf_len);
3134 if (temp == NULL) {
3135 xmlFree(buf);
3136 xmlFree(buffer);
3137 return (NULL);
3138 }
3139 buffer = temp;
3140 temp = (xmlChar *) xmlRealloc(buf, buf_len);
3141 if (temp == NULL) {
3142 xmlFree(buf);
3143 xmlFree(buffer);
3144 return (NULL);
3145 }
3146 buf = temp;
3147 }
3148 if (occur == 0)
3149 snprintf((char *) buf, buf_len, "%c%s%s",
3150 sep, name, (char *) buffer);
3151 else
3152 snprintf((char *) buf, buf_len, "%c%s[%d]%s",
3153 sep, name, occur, (char *) buffer);
3154 snprintf((char *) buffer, buf_len, "%s", buf);
3155 cur = next;
3156 } while (cur != NULL);
3157 xmlFree(buf);
3158 return (buffer);
3159}
3160
3161/**
Owen Taylor3473f882001-02-23 17:55:21 +00003162 * xmlDocGetRootElement:
3163 * @doc: the document
3164 *
3165 * Get the root element of the document (doc->children is a list
3166 * containing possibly comments, PIs, etc ...).
3167 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003168 * Returns the #xmlNodePtr for the root or NULL
Owen Taylor3473f882001-02-23 17:55:21 +00003169 */
3170xmlNodePtr
3171xmlDocGetRootElement(xmlDocPtr doc) {
3172 xmlNodePtr ret;
3173
3174 if (doc == NULL) return(NULL);
3175 ret = doc->children;
3176 while (ret != NULL) {
3177 if (ret->type == XML_ELEMENT_NODE)
3178 return(ret);
3179 ret = ret->next;
3180 }
3181 return(ret);
3182}
3183
3184/**
3185 * xmlDocSetRootElement:
3186 * @doc: the document
3187 * @root: the new document root element
3188 *
3189 * Set the root element of the document (doc->children is a list
3190 * containing possibly comments, PIs, etc ...).
3191 *
3192 * Returns the old root element if any was found
3193 */
3194xmlNodePtr
3195xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) {
3196 xmlNodePtr old = NULL;
3197
3198 if (doc == NULL) return(NULL);
3199 old = doc->children;
3200 while (old != NULL) {
3201 if (old->type == XML_ELEMENT_NODE)
3202 break;
3203 old = old->next;
3204 }
3205 if (old == NULL) {
3206 if (doc->children == NULL) {
3207 doc->children = root;
3208 doc->last = root;
3209 } else {
3210 xmlAddSibling(doc->children, root);
3211 }
3212 } else {
3213 xmlReplaceNode(old, root);
3214 }
3215 return(old);
3216}
3217
3218/**
3219 * xmlNodeSetLang:
3220 * @cur: the node being changed
Daniel Veillardd1640922001-12-17 15:30:10 +00003221 * @lang: the language description
Owen Taylor3473f882001-02-23 17:55:21 +00003222 *
3223 * Set the language of a node, i.e. the values of the xml:lang
3224 * attribute.
3225 */
3226void
3227xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) {
3228 if (cur == NULL) return;
3229 switch(cur->type) {
3230 case XML_TEXT_NODE:
3231 case XML_CDATA_SECTION_NODE:
3232 case XML_COMMENT_NODE:
3233 case XML_DOCUMENT_NODE:
3234 case XML_DOCUMENT_TYPE_NODE:
3235 case XML_DOCUMENT_FRAG_NODE:
3236 case XML_NOTATION_NODE:
3237 case XML_HTML_DOCUMENT_NODE:
3238 case XML_DTD_NODE:
3239 case XML_ELEMENT_DECL:
3240 case XML_ATTRIBUTE_DECL:
3241 case XML_ENTITY_DECL:
3242 case XML_PI_NODE:
3243 case XML_ENTITY_REF_NODE:
3244 case XML_ENTITY_NODE:
3245 case XML_NAMESPACE_DECL:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003246#ifdef LIBXML_DOCB_ENABLED
3247 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003248#endif
3249 case XML_XINCLUDE_START:
3250 case XML_XINCLUDE_END:
3251 return;
3252 case XML_ELEMENT_NODE:
3253 case XML_ATTRIBUTE_NODE:
3254 break;
3255 }
3256 xmlSetProp(cur, BAD_CAST "xml:lang", lang);
3257}
3258
3259/**
3260 * xmlNodeGetLang:
3261 * @cur: the node being checked
3262 *
3263 * Searches the language of a node, i.e. the values of the xml:lang
3264 * attribute or the one carried by the nearest ancestor.
3265 *
3266 * Returns a pointer to the lang value, or NULL if not found
3267 * It's up to the caller to free the memory.
3268 */
3269xmlChar *
3270xmlNodeGetLang(xmlNodePtr cur) {
3271 xmlChar *lang;
3272
3273 while (cur != NULL) {
Daniel Veillardc17337c2001-05-09 10:51:31 +00003274 lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE);
Owen Taylor3473f882001-02-23 17:55:21 +00003275 if (lang != NULL)
3276 return(lang);
3277 cur = cur->parent;
3278 }
3279 return(NULL);
3280}
3281
3282
3283/**
3284 * xmlNodeSetSpacePreserve:
3285 * @cur: the node being changed
3286 * @val: the xml:space value ("0": default, 1: "preserve")
3287 *
3288 * Set (or reset) the space preserving behaviour of a node, i.e. the
3289 * value of the xml:space attribute.
3290 */
3291void
3292xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) {
3293 if (cur == NULL) return;
3294 switch(cur->type) {
3295 case XML_TEXT_NODE:
3296 case XML_CDATA_SECTION_NODE:
3297 case XML_COMMENT_NODE:
3298 case XML_DOCUMENT_NODE:
3299 case XML_DOCUMENT_TYPE_NODE:
3300 case XML_DOCUMENT_FRAG_NODE:
3301 case XML_NOTATION_NODE:
3302 case XML_HTML_DOCUMENT_NODE:
3303 case XML_DTD_NODE:
3304 case XML_ELEMENT_DECL:
3305 case XML_ATTRIBUTE_DECL:
3306 case XML_ENTITY_DECL:
3307 case XML_PI_NODE:
3308 case XML_ENTITY_REF_NODE:
3309 case XML_ENTITY_NODE:
3310 case XML_NAMESPACE_DECL:
3311 case XML_XINCLUDE_START:
3312 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003313#ifdef LIBXML_DOCB_ENABLED
3314 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003315#endif
3316 return;
3317 case XML_ELEMENT_NODE:
3318 case XML_ATTRIBUTE_NODE:
3319 break;
3320 }
3321 switch (val) {
3322 case 0:
3323 xmlSetProp(cur, BAD_CAST "xml:space", BAD_CAST "default");
3324 break;
3325 case 1:
3326 xmlSetProp(cur, BAD_CAST "xml:space",
3327 BAD_CAST "preserve");
3328 break;
3329 }
3330}
3331
3332/**
3333 * xmlNodeGetSpacePreserve:
3334 * @cur: the node being checked
3335 *
3336 * Searches the space preserving behaviour of a node, i.e. the values
3337 * of the xml:space attribute or the one carried by the nearest
3338 * ancestor.
3339 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003340 * Returns -1 if xml:space is not inherited, 0 if "default", 1 if "preserve"
Owen Taylor3473f882001-02-23 17:55:21 +00003341 */
3342int
3343xmlNodeGetSpacePreserve(xmlNodePtr cur) {
3344 xmlChar *space;
3345
3346 while (cur != NULL) {
3347 space = xmlGetProp(cur, BAD_CAST "xml:space");
3348 if (space != NULL) {
3349 if (xmlStrEqual(space, BAD_CAST "preserve")) {
3350 xmlFree(space);
3351 return(1);
3352 }
3353 if (xmlStrEqual(space, BAD_CAST "default")) {
3354 xmlFree(space);
3355 return(0);
3356 }
3357 xmlFree(space);
3358 }
3359 cur = cur->parent;
3360 }
3361 return(-1);
3362}
3363
3364/**
3365 * xmlNodeSetName:
3366 * @cur: the node being changed
3367 * @name: the new tag name
3368 *
3369 * Set (or reset) the name of a node.
3370 */
3371void
3372xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) {
3373 if (cur == NULL) return;
3374 if (name == NULL) return;
3375 switch(cur->type) {
3376 case XML_TEXT_NODE:
3377 case XML_CDATA_SECTION_NODE:
3378 case XML_COMMENT_NODE:
3379 case XML_DOCUMENT_TYPE_NODE:
3380 case XML_DOCUMENT_FRAG_NODE:
3381 case XML_NOTATION_NODE:
3382 case XML_HTML_DOCUMENT_NODE:
3383 case XML_NAMESPACE_DECL:
3384 case XML_XINCLUDE_START:
3385 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003386#ifdef LIBXML_DOCB_ENABLED
3387 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003388#endif
3389 return;
3390 case XML_ELEMENT_NODE:
3391 case XML_ATTRIBUTE_NODE:
3392 case XML_PI_NODE:
3393 case XML_ENTITY_REF_NODE:
3394 case XML_ENTITY_NODE:
3395 case XML_DTD_NODE:
3396 case XML_DOCUMENT_NODE:
3397 case XML_ELEMENT_DECL:
3398 case XML_ATTRIBUTE_DECL:
3399 case XML_ENTITY_DECL:
3400 break;
3401 }
3402 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
3403 cur->name = xmlStrdup(name);
3404}
3405
3406/**
3407 * xmlNodeSetBase:
3408 * @cur: the node being changed
3409 * @uri: the new base URI
3410 *
3411 * Set (or reset) the base URI of a node, i.e. the value of the
3412 * xml:base attribute.
3413 */
3414void
3415xmlNodeSetBase(xmlNodePtr cur, xmlChar* uri) {
3416 if (cur == NULL) return;
3417 switch(cur->type) {
3418 case XML_TEXT_NODE:
3419 case XML_CDATA_SECTION_NODE:
3420 case XML_COMMENT_NODE:
3421 case XML_DOCUMENT_NODE:
3422 case XML_DOCUMENT_TYPE_NODE:
3423 case XML_DOCUMENT_FRAG_NODE:
3424 case XML_NOTATION_NODE:
3425 case XML_HTML_DOCUMENT_NODE:
3426 case XML_DTD_NODE:
3427 case XML_ELEMENT_DECL:
3428 case XML_ATTRIBUTE_DECL:
3429 case XML_ENTITY_DECL:
3430 case XML_PI_NODE:
3431 case XML_ENTITY_REF_NODE:
3432 case XML_ENTITY_NODE:
3433 case XML_NAMESPACE_DECL:
3434 case XML_XINCLUDE_START:
3435 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003436#ifdef LIBXML_DOCB_ENABLED
3437 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003438#endif
3439 return;
3440 case XML_ELEMENT_NODE:
3441 case XML_ATTRIBUTE_NODE:
3442 break;
3443 }
3444 xmlSetProp(cur, BAD_CAST "xml:base", uri);
3445}
3446
3447/**
Owen Taylor3473f882001-02-23 17:55:21 +00003448 * xmlNodeGetBase:
3449 * @doc: the document the node pertains to
3450 * @cur: the node being checked
3451 *
3452 * Searches for the BASE URL. The code should work on both XML
3453 * and HTML document even if base mechanisms are completely different.
3454 * It returns the base as defined in RFC 2396 sections
3455 * 5.1.1. Base URI within Document Content
3456 * and
3457 * 5.1.2. Base URI from the Encapsulating Entity
3458 * However it does not return the document base (5.1.3), use
3459 * xmlDocumentGetBase() for this
3460 *
3461 * Returns a pointer to the base URL, or NULL if not found
3462 * It's up to the caller to free the memory.
3463 */
3464xmlChar *
3465xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00003466 xmlChar *oldbase = NULL;
3467 xmlChar *base, *newbase;
Owen Taylor3473f882001-02-23 17:55:21 +00003468
3469 if ((cur == NULL) && (doc == NULL))
3470 return(NULL);
3471 if (doc == NULL) doc = cur->doc;
3472 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
3473 cur = doc->children;
3474 while ((cur != NULL) && (cur->name != NULL)) {
3475 if (cur->type != XML_ELEMENT_NODE) {
3476 cur = cur->next;
3477 continue;
3478 }
3479 if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) {
3480 cur = cur->children;
3481 continue;
3482 }
3483 if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) {
3484 cur = cur->children;
3485 continue;
3486 }
3487 if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) {
3488 return(xmlGetProp(cur, BAD_CAST "href"));
3489 }
3490 cur = cur->next;
3491 }
3492 return(NULL);
3493 }
3494 while (cur != NULL) {
3495 if (cur->type == XML_ENTITY_DECL) {
3496 xmlEntityPtr ent = (xmlEntityPtr) cur;
3497 return(xmlStrdup(ent->URI));
3498 }
Daniel Veillard42596ad2001-05-22 16:57:14 +00003499 if (cur->type == XML_ELEMENT_NODE) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00003500 base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE);
Daniel Veillard42596ad2001-05-22 16:57:14 +00003501 if (base != NULL) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00003502 if (oldbase != NULL) {
3503 newbase = xmlBuildURI(oldbase, base);
3504 if (newbase != NULL) {
3505 xmlFree(oldbase);
3506 xmlFree(base);
3507 oldbase = newbase;
3508 } else {
3509 xmlFree(oldbase);
3510 xmlFree(base);
3511 return(NULL);
3512 }
3513 } else {
3514 oldbase = base;
3515 }
3516 if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) ||
3517 (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) ||
3518 (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4)))
3519 return(oldbase);
Daniel Veillard42596ad2001-05-22 16:57:14 +00003520 }
3521 }
Owen Taylor3473f882001-02-23 17:55:21 +00003522 cur = cur->parent;
3523 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00003524 if ((doc != NULL) && (doc->URL != NULL)) {
3525 if (oldbase == NULL)
3526 return(xmlStrdup(doc->URL));
3527 newbase = xmlBuildURI(oldbase, doc->URL);
3528 xmlFree(oldbase);
3529 return(newbase);
3530 }
3531 return(oldbase);
Owen Taylor3473f882001-02-23 17:55:21 +00003532}
3533
3534/**
3535 * xmlNodeGetContent:
3536 * @cur: the node being read
3537 *
3538 * Read the value of a node, this can be either the text carried
3539 * directly by this node if it's a TEXT node or the aggregate string
3540 * of the values carried by this node child's (TEXT and ENTITY_REF).
Daniel Veillardd1640922001-12-17 15:30:10 +00003541 * Entity references are substituted.
3542 * Returns a new #xmlChar * or NULL if no content is available.
Owen Taylor3473f882001-02-23 17:55:21 +00003543 * It's up to the caller to free the memory.
3544 */
3545xmlChar *
3546xmlNodeGetContent(xmlNodePtr cur) {
3547 if (cur == NULL) return(NULL);
3548 switch (cur->type) {
3549 case XML_DOCUMENT_FRAG_NODE:
3550 case XML_ELEMENT_NODE: {
3551 xmlNodePtr tmp = cur;
3552 xmlBufferPtr buffer;
3553 xmlChar *ret;
3554
3555 buffer = xmlBufferCreate();
3556 if (buffer == NULL)
3557 return(NULL);
3558 while (tmp != NULL) {
3559 switch (tmp->type) {
Daniel Veillard2d703722001-05-30 18:32:34 +00003560 case XML_CDATA_SECTION_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003561 case XML_TEXT_NODE:
3562 if (tmp->content != NULL)
3563#ifndef XML_USE_BUFFER_CONTENT
3564 xmlBufferCat(buffer, tmp->content);
3565#else
3566 xmlBufferCat(buffer,
3567 xmlBufferContent(tmp->content));
3568#endif
3569 break;
3570 case XML_ENTITY_REF_NODE: {
3571 xmlEntityPtr ent;
3572
3573 ent = xmlGetDocEntity(cur->doc, tmp->name);
3574 if (ent != NULL)
3575 xmlBufferCat(buffer, ent->content);
3576 }
3577 default:
3578 break;
3579 }
3580 /*
3581 * Skip to next node
3582 */
3583 if (tmp->children != NULL) {
3584 if (tmp->children->type != XML_ENTITY_DECL) {
3585 tmp = tmp->children;
3586 continue;
3587 }
3588 }
Daniel Veillard6c831202001-03-07 15:57:53 +00003589 if (tmp == cur)
3590 break;
3591
Owen Taylor3473f882001-02-23 17:55:21 +00003592 if (tmp->next != NULL) {
3593 tmp = tmp->next;
3594 continue;
3595 }
3596
3597 do {
3598 tmp = tmp->parent;
3599 if (tmp == NULL)
3600 break;
Daniel Veillard6c831202001-03-07 15:57:53 +00003601 if (tmp == cur) {
Owen Taylor3473f882001-02-23 17:55:21 +00003602 tmp = NULL;
3603 break;
3604 }
3605 if (tmp->next != NULL) {
3606 tmp = tmp->next;
3607 break;
3608 }
3609 } while (tmp != NULL);
3610 }
3611 ret = buffer->content;
3612 buffer->content = NULL;
3613 xmlBufferFree(buffer);
3614 return(ret);
3615 }
3616 case XML_ATTRIBUTE_NODE: {
3617 xmlAttrPtr attr = (xmlAttrPtr) cur;
3618 if (attr->parent != NULL)
3619 return(xmlNodeListGetString(attr->parent->doc, attr->children, 1));
3620 else
3621 return(xmlNodeListGetString(NULL, attr->children, 1));
3622 break;
3623 }
3624 case XML_COMMENT_NODE:
3625 case XML_PI_NODE:
3626 if (cur->content != NULL)
3627#ifndef XML_USE_BUFFER_CONTENT
3628 return(xmlStrdup(cur->content));
3629#else
3630 return(xmlStrdup(xmlBufferContent(cur->content)));
3631#endif
3632 return(NULL);
3633 case XML_ENTITY_REF_NODE:
3634 /*
3635 * Locate the entity, and get it's content
3636 * @@@
3637 */
3638 return(NULL);
3639 case XML_ENTITY_NODE:
3640 case XML_DOCUMENT_NODE:
3641 case XML_HTML_DOCUMENT_NODE:
3642 case XML_DOCUMENT_TYPE_NODE:
3643 case XML_NOTATION_NODE:
3644 case XML_DTD_NODE:
3645 case XML_XINCLUDE_START:
3646 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003647#ifdef LIBXML_DOCB_ENABLED
3648 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003649#endif
3650 return(NULL);
3651 case XML_NAMESPACE_DECL:
3652 return(xmlStrdup(((xmlNsPtr)cur)->href));
3653 case XML_ELEMENT_DECL:
3654 /* TODO !!! */
3655 return(NULL);
3656 case XML_ATTRIBUTE_DECL:
3657 /* TODO !!! */
3658 return(NULL);
3659 case XML_ENTITY_DECL:
3660 /* TODO !!! */
3661 return(NULL);
3662 case XML_CDATA_SECTION_NODE:
3663 case XML_TEXT_NODE:
3664 if (cur->content != NULL)
3665#ifndef XML_USE_BUFFER_CONTENT
3666 return(xmlStrdup(cur->content));
3667#else
3668 return(xmlStrdup(xmlBufferContent(cur->content)));
3669#endif
3670 return(NULL);
3671 }
3672 return(NULL);
3673}
3674
3675/**
3676 * xmlNodeSetContent:
3677 * @cur: the node being modified
3678 * @content: the new value of the content
3679 *
3680 * Replace the content of a node.
3681 */
3682void
3683xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) {
3684 if (cur == NULL) {
3685#ifdef DEBUG_TREE
3686 xmlGenericError(xmlGenericErrorContext,
3687 "xmlNodeSetContent : node == NULL\n");
3688#endif
3689 return;
3690 }
3691 switch (cur->type) {
3692 case XML_DOCUMENT_FRAG_NODE:
3693 case XML_ELEMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003694 if (cur->children != NULL) xmlFreeNodeList(cur->children);
3695 cur->children = xmlStringGetNodeList(cur->doc, content);
3696 UPDATE_LAST_CHILD_AND_PARENT(cur)
3697 break;
3698 case XML_ATTRIBUTE_NODE:
3699 break;
3700 case XML_TEXT_NODE:
3701 case XML_CDATA_SECTION_NODE:
3702 case XML_ENTITY_REF_NODE:
3703 case XML_ENTITY_NODE:
3704 case XML_PI_NODE:
3705 case XML_COMMENT_NODE:
3706 if (cur->content != NULL) {
3707#ifndef XML_USE_BUFFER_CONTENT
3708 xmlFree(cur->content);
3709#else
3710 xmlBufferFree(cur->content);
3711#endif
3712 }
3713 if (cur->children != NULL) xmlFreeNodeList(cur->children);
3714 cur->last = cur->children = NULL;
3715 if (content != NULL) {
3716#ifndef XML_USE_BUFFER_CONTENT
3717 cur->content = xmlStrdup(content);
3718#else
3719 cur->content = xmlBufferCreateSize(0);
3720 xmlBufferSetAllocationScheme(cur->content,
3721 xmlGetBufferAllocationScheme());
3722 xmlBufferAdd(cur->content, content, -1);
3723#endif
3724 } else
3725 cur->content = NULL;
3726 break;
3727 case XML_DOCUMENT_NODE:
3728 case XML_HTML_DOCUMENT_NODE:
3729 case XML_DOCUMENT_TYPE_NODE:
3730 case XML_XINCLUDE_START:
3731 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003732#ifdef LIBXML_DOCB_ENABLED
3733 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003734#endif
3735 break;
3736 case XML_NOTATION_NODE:
3737 break;
3738 case XML_DTD_NODE:
3739 break;
3740 case XML_NAMESPACE_DECL:
3741 break;
3742 case XML_ELEMENT_DECL:
3743 /* TODO !!! */
3744 break;
3745 case XML_ATTRIBUTE_DECL:
3746 /* TODO !!! */
3747 break;
3748 case XML_ENTITY_DECL:
3749 /* TODO !!! */
3750 break;
3751 }
3752}
3753
3754/**
3755 * xmlNodeSetContentLen:
3756 * @cur: the node being modified
3757 * @content: the new value of the content
3758 * @len: the size of @content
3759 *
3760 * Replace the content of a node.
3761 */
3762void
3763xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
3764 if (cur == NULL) {
3765#ifdef DEBUG_TREE
3766 xmlGenericError(xmlGenericErrorContext,
3767 "xmlNodeSetContentLen : node == NULL\n");
3768#endif
3769 return;
3770 }
3771 switch (cur->type) {
3772 case XML_DOCUMENT_FRAG_NODE:
3773 case XML_ELEMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003774 if (cur->children != NULL) xmlFreeNodeList(cur->children);
3775 cur->children = xmlStringLenGetNodeList(cur->doc, content, len);
3776 UPDATE_LAST_CHILD_AND_PARENT(cur)
3777 break;
3778 case XML_ATTRIBUTE_NODE:
3779 break;
3780 case XML_TEXT_NODE:
3781 case XML_CDATA_SECTION_NODE:
3782 case XML_ENTITY_REF_NODE:
3783 case XML_ENTITY_NODE:
3784 case XML_PI_NODE:
3785 case XML_COMMENT_NODE:
3786 case XML_NOTATION_NODE:
3787 if (cur->content != NULL) {
3788#ifndef XML_USE_BUFFER_CONTENT
3789 xmlFree(cur->content);
3790#else
3791 xmlBufferFree(cur->content);
3792#endif
3793 }
3794 if (cur->children != NULL) xmlFreeNodeList(cur->children);
3795 cur->children = cur->last = NULL;
3796 if (content != NULL) {
3797#ifndef XML_USE_BUFFER_CONTENT
3798 cur->content = xmlStrndup(content, len);
3799#else
3800 cur->content = xmlBufferCreateSize(len);
3801 xmlBufferSetAllocationScheme(cur->content,
3802 xmlGetBufferAllocationScheme());
3803 xmlBufferAdd(cur->content, content, len);
3804#endif
3805 } else
3806 cur->content = NULL;
3807 break;
3808 case XML_DOCUMENT_NODE:
3809 case XML_DTD_NODE:
3810 case XML_HTML_DOCUMENT_NODE:
3811 case XML_DOCUMENT_TYPE_NODE:
3812 case XML_NAMESPACE_DECL:
3813 case XML_XINCLUDE_START:
3814 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003815#ifdef LIBXML_DOCB_ENABLED
3816 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003817#endif
3818 break;
3819 case XML_ELEMENT_DECL:
3820 /* TODO !!! */
3821 break;
3822 case XML_ATTRIBUTE_DECL:
3823 /* TODO !!! */
3824 break;
3825 case XML_ENTITY_DECL:
3826 /* TODO !!! */
3827 break;
3828 }
3829}
3830
3831/**
3832 * xmlNodeAddContentLen:
3833 * @cur: the node being modified
3834 * @content: extra content
3835 * @len: the size of @content
3836 *
3837 * Append the extra substring to the node content.
3838 */
3839void
3840xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
3841 if (cur == NULL) {
3842#ifdef DEBUG_TREE
3843 xmlGenericError(xmlGenericErrorContext,
3844 "xmlNodeAddContentLen : node == NULL\n");
3845#endif
3846 return;
3847 }
3848 if (len <= 0) return;
3849 switch (cur->type) {
3850 case XML_DOCUMENT_FRAG_NODE:
3851 case XML_ELEMENT_NODE: {
Daniel Veillard7db37732001-07-12 01:20:08 +00003852 xmlNodePtr last, newNode;
Owen Taylor3473f882001-02-23 17:55:21 +00003853
Daniel Veillard7db37732001-07-12 01:20:08 +00003854 last = cur->last;
Owen Taylor3473f882001-02-23 17:55:21 +00003855 newNode = xmlNewTextLen(content, len);
3856 if (newNode != NULL) {
3857 xmlAddChild(cur, newNode);
3858 if ((last != NULL) && (last->next == newNode)) {
3859 xmlTextMerge(last, newNode);
3860 }
3861 }
3862 break;
3863 }
3864 case XML_ATTRIBUTE_NODE:
3865 break;
3866 case XML_TEXT_NODE:
3867 case XML_CDATA_SECTION_NODE:
3868 case XML_ENTITY_REF_NODE:
3869 case XML_ENTITY_NODE:
3870 case XML_PI_NODE:
3871 case XML_COMMENT_NODE:
3872 case XML_NOTATION_NODE:
3873 if (content != NULL) {
3874#ifndef XML_USE_BUFFER_CONTENT
3875 cur->content = xmlStrncat(cur->content, content, len);
3876#else
3877 xmlBufferAdd(cur->content, content, len);
3878#endif
3879 }
3880 case XML_DOCUMENT_NODE:
3881 case XML_DTD_NODE:
3882 case XML_HTML_DOCUMENT_NODE:
3883 case XML_DOCUMENT_TYPE_NODE:
3884 case XML_NAMESPACE_DECL:
3885 case XML_XINCLUDE_START:
3886 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003887#ifdef LIBXML_DOCB_ENABLED
3888 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003889#endif
3890 break;
3891 case XML_ELEMENT_DECL:
3892 case XML_ATTRIBUTE_DECL:
3893 case XML_ENTITY_DECL:
3894 break;
3895 }
3896}
3897
3898/**
3899 * xmlNodeAddContent:
3900 * @cur: the node being modified
3901 * @content: extra content
3902 *
3903 * Append the extra substring to the node content.
3904 */
3905void
3906xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) {
3907 int len;
3908
3909 if (cur == NULL) {
3910#ifdef DEBUG_TREE
3911 xmlGenericError(xmlGenericErrorContext,
3912 "xmlNodeAddContent : node == NULL\n");
3913#endif
3914 return;
3915 }
3916 if (content == NULL) return;
3917 len = xmlStrlen(content);
3918 xmlNodeAddContentLen(cur, content, len);
3919}
3920
3921/**
3922 * xmlTextMerge:
3923 * @first: the first text node
3924 * @second: the second text node being merged
3925 *
3926 * Merge two text nodes into one
3927 * Returns the first text node augmented
3928 */
3929xmlNodePtr
3930xmlTextMerge(xmlNodePtr first, xmlNodePtr second) {
3931 if (first == NULL) return(second);
3932 if (second == NULL) return(first);
3933 if (first->type != XML_TEXT_NODE) return(first);
3934 if (second->type != XML_TEXT_NODE) return(first);
3935 if (second->name != first->name)
3936 return(first);
3937#ifndef XML_USE_BUFFER_CONTENT
3938 xmlNodeAddContent(first, second->content);
3939#else
3940 xmlNodeAddContent(first, xmlBufferContent(second->content));
3941#endif
3942 xmlUnlinkNode(second);
3943 xmlFreeNode(second);
3944 return(first);
3945}
3946
3947/**
3948 * xmlGetNsList:
3949 * @doc: the document
3950 * @node: the current node
3951 *
3952 * Search all the namespace applying to a given element.
Daniel Veillardd1640922001-12-17 15:30:10 +00003953 * Returns an NULL terminated array of all the #xmlNsPtr found
Owen Taylor3473f882001-02-23 17:55:21 +00003954 * that need to be freed by the caller or NULL if no
3955 * namespace if defined
3956 */
3957xmlNsPtr *
Daniel Veillard77044732001-06-29 21:31:07 +00003958xmlGetNsList(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node)
3959{
Owen Taylor3473f882001-02-23 17:55:21 +00003960 xmlNsPtr cur;
3961 xmlNsPtr *ret = NULL;
3962 int nbns = 0;
3963 int maxns = 10;
3964 int i;
3965
3966 while (node != NULL) {
Daniel Veillard77044732001-06-29 21:31:07 +00003967 if (node->type == XML_ELEMENT_NODE) {
3968 cur = node->nsDef;
3969 while (cur != NULL) {
3970 if (ret == NULL) {
3971 ret =
3972 (xmlNsPtr *) xmlMalloc((maxns + 1) *
3973 sizeof(xmlNsPtr));
3974 if (ret == NULL) {
3975 xmlGenericError(xmlGenericErrorContext,
3976 "xmlGetNsList : out of memory!\n");
3977 return (NULL);
3978 }
3979 ret[nbns] = NULL;
3980 }
3981 for (i = 0; i < nbns; i++) {
3982 if ((cur->prefix == ret[i]->prefix) ||
3983 (xmlStrEqual(cur->prefix, ret[i]->prefix)))
3984 break;
3985 }
3986 if (i >= nbns) {
3987 if (nbns >= maxns) {
3988 maxns *= 2;
3989 ret = (xmlNsPtr *) xmlRealloc(ret,
3990 (maxns +
3991 1) *
3992 sizeof(xmlNsPtr));
3993 if (ret == NULL) {
3994 xmlGenericError(xmlGenericErrorContext,
3995 "xmlGetNsList : realloc failed!\n");
3996 return (NULL);
3997 }
3998 }
3999 ret[nbns++] = cur;
4000 ret[nbns] = NULL;
4001 }
Owen Taylor3473f882001-02-23 17:55:21 +00004002
Daniel Veillard77044732001-06-29 21:31:07 +00004003 cur = cur->next;
4004 }
4005 }
4006 node = node->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00004007 }
Daniel Veillard77044732001-06-29 21:31:07 +00004008 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00004009}
4010
4011/**
4012 * xmlSearchNs:
4013 * @doc: the document
4014 * @node: the current node
Daniel Veillard77851712001-02-27 21:54:07 +00004015 * @nameSpace: the namespace prefix
Owen Taylor3473f882001-02-23 17:55:21 +00004016 *
4017 * Search a Ns registered under a given name space for a document.
4018 * recurse on the parents until it finds the defined namespace
4019 * or return NULL otherwise.
4020 * @nameSpace can be NULL, this is a search for the default namespace.
4021 * We don't allow to cross entities boundaries. If you don't declare
4022 * the namespace within those you will be in troubles !!! A warning
4023 * is generated to cover this case.
4024 *
4025 * Returns the namespace pointer or NULL.
4026 */
4027xmlNsPtr
4028xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) {
4029 xmlNsPtr cur;
4030
4031 if (node == NULL) return(NULL);
4032 if ((nameSpace != NULL) &&
4033 (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) {
4034 if (doc->oldNs == NULL) {
4035 /*
4036 * Allocate a new Namespace and fill the fields.
4037 */
4038 doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
4039 if (doc->oldNs == NULL) {
4040 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00004041 "xmlSearchNs : malloc failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004042 return(NULL);
4043 }
4044 memset(doc->oldNs, 0, sizeof(xmlNs));
4045 doc->oldNs->type = XML_LOCAL_NAMESPACE;
4046
4047 doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE);
4048 doc->oldNs->prefix = xmlStrdup((const xmlChar *)"xml");
4049 }
4050 return(doc->oldNs);
4051 }
4052 while (node != NULL) {
4053 if ((node->type == XML_ENTITY_REF_NODE) ||
4054 (node->type == XML_ENTITY_NODE) ||
4055 (node->type == XML_ENTITY_DECL))
4056 return(NULL);
4057 if (node->type == XML_ELEMENT_NODE) {
4058 cur = node->nsDef;
4059 while (cur != NULL) {
4060 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
4061 (cur->href != NULL))
4062 return(cur);
4063 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
4064 (cur->href != NULL) &&
4065 (xmlStrEqual(cur->prefix, nameSpace)))
4066 return(cur);
4067 cur = cur->next;
4068 }
4069 }
4070 node = node->parent;
4071 }
4072 return(NULL);
4073}
4074
4075/**
4076 * xmlSearchNsByHref:
4077 * @doc: the document
4078 * @node: the current node
4079 * @href: the namespace value
4080 *
4081 * Search a Ns aliasing a given URI. Recurse on the parents until it finds
4082 * the defined namespace or return NULL otherwise.
4083 * Returns the namespace pointer or NULL.
4084 */
4085xmlNsPtr
4086xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar *href) {
4087 xmlNsPtr cur;
4088 xmlNodePtr orig = node;
4089
4090 if ((node == NULL) || (href == NULL)) return(NULL);
4091 if (xmlStrEqual(href, XML_XML_NAMESPACE)) {
4092 if (doc->oldNs == NULL) {
4093 /*
4094 * Allocate a new Namespace and fill the fields.
4095 */
4096 doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
4097 if (doc->oldNs == NULL) {
4098 xmlGenericError(xmlGenericErrorContext,
4099 "xmlSearchNsByHref : malloc failed\n");
4100 return(NULL);
4101 }
4102 memset(doc->oldNs, 0, sizeof(xmlNs));
4103 doc->oldNs->type = XML_LOCAL_NAMESPACE;
4104
4105 doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE);
4106 doc->oldNs->prefix = xmlStrdup((const xmlChar *)"xml");
4107 }
4108 return(doc->oldNs);
4109 }
4110 while (node != NULL) {
4111 cur = node->nsDef;
4112 while (cur != NULL) {
4113 if ((cur->href != NULL) && (href != NULL) &&
4114 (xmlStrEqual(cur->href, href))) {
4115 /*
4116 * Check that the prefix is not shadowed between orig and node
4117 */
4118 xmlNodePtr check = orig;
4119 xmlNsPtr tst;
4120
4121 while (check != node) {
4122 tst = check->nsDef;
4123 while (tst != NULL) {
4124 if ((tst->prefix == NULL) && (cur->prefix == NULL))
4125 goto shadowed;
4126 if ((tst->prefix != NULL) && (cur->prefix != NULL) &&
4127 (xmlStrEqual(tst->prefix, cur->prefix)))
4128 goto shadowed;
4129 tst = tst->next;
4130 }
4131 check = check->parent;
4132 }
4133 return(cur);
4134 }
4135shadowed:
4136 cur = cur->next;
4137 }
4138 node = node->parent;
4139 }
4140 return(NULL);
4141}
4142
4143/**
4144 * xmlNewReconciliedNs
4145 * @doc: the document
4146 * @tree: a node expected to hold the new namespace
4147 * @ns: the original namespace
4148 *
4149 * This function tries to locate a namespace definition in a tree
4150 * ancestors, or create a new namespace definition node similar to
4151 * @ns trying to reuse the same prefix. However if the given prefix is
4152 * null (default namespace) or reused within the subtree defined by
4153 * @tree or on one of its ancestors then a new prefix is generated.
4154 * Returns the (new) namespace definition or NULL in case of error
4155 */
4156xmlNsPtr
4157xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) {
4158 xmlNsPtr def;
4159 xmlChar prefix[50];
4160 int counter = 1;
4161
4162 if (tree == NULL) {
4163#ifdef DEBUG_TREE
4164 xmlGenericError(xmlGenericErrorContext,
4165 "xmlNewReconciliedNs : tree == NULL\n");
4166#endif
4167 return(NULL);
4168 }
4169 if (ns == NULL) {
4170#ifdef DEBUG_TREE
4171 xmlGenericError(xmlGenericErrorContext,
4172 "xmlNewReconciliedNs : ns == NULL\n");
4173#endif
4174 return(NULL);
4175 }
4176 /*
4177 * Search an existing namespace definition inherited.
4178 */
4179 def = xmlSearchNsByHref(doc, tree, ns->href);
4180 if (def != NULL)
4181 return(def);
4182
4183 /*
4184 * Find a close prefix which is not already in use.
4185 * Let's strip namespace prefixes longer than 20 chars !
4186 */
4187 sprintf((char *) prefix, "%.20s", ns->prefix);
4188 def = xmlSearchNs(doc, tree, prefix);
4189 while (def != NULL) {
4190 if (counter > 1000) return(NULL);
4191 sprintf((char *) prefix, "%.20s%d", ns->prefix, counter++);
4192 def = xmlSearchNs(doc, tree, prefix);
4193 }
4194
4195 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00004196 * OK, now we are ready to create a new one.
Owen Taylor3473f882001-02-23 17:55:21 +00004197 */
4198 def = xmlNewNs(tree, ns->href, prefix);
4199 return(def);
4200}
4201
4202/**
4203 * xmlReconciliateNs
4204 * @doc: the document
4205 * @tree: a node defining the subtree to reconciliate
4206 *
4207 * This function checks that all the namespaces declared within the given
4208 * tree are properly declared. This is needed for example after Copy or Cut
4209 * and then paste operations. The subtree may still hold pointers to
4210 * namespace declarations outside the subtree or invalid/masked. As much
Daniel Veillardd1640922001-12-17 15:30:10 +00004211 * as possible the function try to reuse the existing namespaces found in
Owen Taylor3473f882001-02-23 17:55:21 +00004212 * the new environment. If not possible the new namespaces are redeclared
4213 * on @tree at the top of the given subtree.
4214 * Returns the number of namespace declarations created or -1 in case of error.
4215 */
4216int
4217xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) {
4218 xmlNsPtr *oldNs = NULL;
4219 xmlNsPtr *newNs = NULL;
4220 int sizeCache = 0;
4221 int nbCache = 0;
4222
4223 xmlNsPtr n;
4224 xmlNodePtr node = tree;
4225 xmlAttrPtr attr;
4226 int ret = 0, i;
4227
4228 while (node != NULL) {
4229 /*
4230 * Reconciliate the node namespace
4231 */
4232 if (node->ns != NULL) {
4233 /*
4234 * initialize the cache if needed
4235 */
4236 if (sizeCache == 0) {
4237 sizeCache = 10;
4238 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
4239 sizeof(xmlNsPtr));
4240 if (oldNs == NULL) {
4241 xmlGenericError(xmlGenericErrorContext,
4242 "xmlReconciliateNs : memory pbm\n");
4243 return(-1);
4244 }
4245 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
4246 sizeof(xmlNsPtr));
4247 if (newNs == NULL) {
4248 xmlGenericError(xmlGenericErrorContext,
4249 "xmlReconciliateNs : memory pbm\n");
4250 xmlFree(oldNs);
4251 return(-1);
4252 }
4253 }
4254 for (i = 0;i < nbCache;i++) {
4255 if (oldNs[i] == node->ns) {
4256 node->ns = newNs[i];
4257 break;
4258 }
4259 }
4260 if (i == nbCache) {
4261 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00004262 * OK we need to recreate a new namespace definition
Owen Taylor3473f882001-02-23 17:55:21 +00004263 */
4264 n = xmlNewReconciliedNs(doc, tree, node->ns);
4265 if (n != NULL) { /* :-( what if else ??? */
4266 /*
4267 * check if we need to grow the cache buffers.
4268 */
4269 if (sizeCache <= nbCache) {
4270 sizeCache *= 2;
4271 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
4272 sizeof(xmlNsPtr));
4273 if (oldNs == NULL) {
4274 xmlGenericError(xmlGenericErrorContext,
4275 "xmlReconciliateNs : memory pbm\n");
4276 xmlFree(newNs);
4277 return(-1);
4278 }
4279 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
4280 sizeof(xmlNsPtr));
4281 if (newNs == NULL) {
4282 xmlGenericError(xmlGenericErrorContext,
4283 "xmlReconciliateNs : memory pbm\n");
4284 xmlFree(oldNs);
4285 return(-1);
4286 }
4287 }
4288 newNs[nbCache] = n;
4289 oldNs[nbCache++] = node->ns;
4290 node->ns = n;
4291 }
4292 }
4293 }
4294 /*
4295 * now check for namespace hold by attributes on the node.
4296 */
4297 attr = node->properties;
4298 while (attr != NULL) {
4299 if (attr->ns != NULL) {
4300 /*
4301 * initialize the cache if needed
4302 */
4303 if (sizeCache == 0) {
4304 sizeCache = 10;
4305 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
4306 sizeof(xmlNsPtr));
4307 if (oldNs == NULL) {
4308 xmlGenericError(xmlGenericErrorContext,
4309 "xmlReconciliateNs : memory pbm\n");
4310 return(-1);
4311 }
4312 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
4313 sizeof(xmlNsPtr));
4314 if (newNs == NULL) {
4315 xmlGenericError(xmlGenericErrorContext,
4316 "xmlReconciliateNs : memory pbm\n");
4317 xmlFree(oldNs);
4318 return(-1);
4319 }
4320 }
4321 for (i = 0;i < nbCache;i++) {
4322 if (oldNs[i] == attr->ns) {
4323 node->ns = newNs[i];
4324 break;
4325 }
4326 }
4327 if (i == nbCache) {
4328 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00004329 * OK we need to recreate a new namespace definition
Owen Taylor3473f882001-02-23 17:55:21 +00004330 */
4331 n = xmlNewReconciliedNs(doc, tree, attr->ns);
4332 if (n != NULL) { /* :-( what if else ??? */
4333 /*
4334 * check if we need to grow the cache buffers.
4335 */
4336 if (sizeCache <= nbCache) {
4337 sizeCache *= 2;
4338 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
4339 sizeof(xmlNsPtr));
4340 if (oldNs == NULL) {
4341 xmlGenericError(xmlGenericErrorContext,
4342 "xmlReconciliateNs : memory pbm\n");
4343 xmlFree(newNs);
4344 return(-1);
4345 }
4346 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
4347 sizeof(xmlNsPtr));
4348 if (newNs == NULL) {
4349 xmlGenericError(xmlGenericErrorContext,
4350 "xmlReconciliateNs : memory pbm\n");
4351 xmlFree(oldNs);
4352 return(-1);
4353 }
4354 }
4355 newNs[nbCache] = n;
4356 oldNs[nbCache++] = attr->ns;
4357 attr->ns = n;
4358 }
4359 }
4360 }
4361 attr = attr->next;
4362 }
4363
4364 /*
4365 * Browse the full subtree, deep first
4366 */
4367 if (node->children != NULL) {
4368 /* deep first */
4369 node = node->children;
4370 } else if ((node != tree) && (node->next != NULL)) {
4371 /* then siblings */
4372 node = node->next;
4373 } else if (node != tree) {
4374 /* go up to parents->next if needed */
4375 while (node != tree) {
4376 if (node->parent != NULL)
4377 node = node->parent;
4378 if ((node != tree) && (node->next != NULL)) {
4379 node = node->next;
4380 break;
4381 }
4382 if (node->parent == NULL) {
4383 node = NULL;
4384 break;
4385 }
4386 }
4387 /* exit condition */
4388 if (node == tree)
4389 node = NULL;
4390 }
4391 }
4392 return(ret);
4393}
4394
4395/**
4396 * xmlHasProp:
4397 * @node: the node
4398 * @name: the attribute name
4399 *
4400 * Search an attribute associated to a node
4401 * This function also looks in DTD attribute declaration for #FIXED or
4402 * default declaration values unless DTD use has been turned off.
4403 *
4404 * Returns the attribute or the attribute declaration or NULL if
4405 * neither was found.
4406 */
4407xmlAttrPtr
4408xmlHasProp(xmlNodePtr node, const xmlChar *name) {
4409 xmlAttrPtr prop;
4410 xmlDocPtr doc;
4411
4412 if ((node == NULL) || (name == NULL)) return(NULL);
4413 /*
4414 * Check on the properties attached to the node
4415 */
4416 prop = node->properties;
4417 while (prop != NULL) {
4418 if (xmlStrEqual(prop->name, name)) {
4419 return(prop);
4420 }
4421 prop = prop->next;
4422 }
4423 if (!xmlCheckDTD) return(NULL);
4424
4425 /*
4426 * Check if there is a default declaration in the internal
4427 * or external subsets
4428 */
4429 doc = node->doc;
4430 if (doc != NULL) {
4431 xmlAttributePtr attrDecl;
4432 if (doc->intSubset != NULL) {
4433 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
4434 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4435 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
4436 if (attrDecl != NULL)
4437 return((xmlAttrPtr) attrDecl);
4438 }
4439 }
4440 return(NULL);
4441}
4442
4443/**
Daniel Veillarde95e2392001-06-06 10:46:28 +00004444 * xmlHasNsProp:
4445 * @node: the node
4446 * @name: the attribute name
Daniel Veillardca2366a2001-06-11 12:09:01 +00004447 * @nameSpace: the URI of the namespace
Daniel Veillarde95e2392001-06-06 10:46:28 +00004448 *
4449 * Search for an attribute associated to a node
4450 * This attribute has to be anchored in the namespace specified.
4451 * This does the entity substitution.
4452 * This function looks in DTD attribute declaration for #FIXED or
4453 * default declaration values unless DTD use has been turned off.
4454 *
4455 * Returns the attribute or the attribute declaration or NULL
4456 * if neither was found.
4457 */
4458xmlAttrPtr
Daniel Veillardca2366a2001-06-11 12:09:01 +00004459xmlHasNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
Daniel Veillarde95e2392001-06-06 10:46:28 +00004460 xmlAttrPtr prop;
4461 xmlDocPtr doc;
4462 xmlNsPtr ns;
4463
4464 if (node == NULL)
4465 return(NULL);
4466
4467 prop = node->properties;
Daniel Veillardca2366a2001-06-11 12:09:01 +00004468 if (nameSpace == NULL)
Daniel Veillarde95e2392001-06-06 10:46:28 +00004469 return(xmlHasProp(node, name));
4470 while (prop != NULL) {
4471 /*
4472 * One need to have
4473 * - same attribute names
4474 * - and the attribute carrying that namespace
4475 * or
4476 * no namespace on the attribute and the element carrying it
4477 */
4478 if ((xmlStrEqual(prop->name, name)) &&
Daniel Veillarde3c81b52001-06-17 14:50:34 +00004479 ((prop->ns != NULL) && (xmlStrEqual(prop->ns->href, nameSpace)))) {
4480 return(prop);
Daniel Veillarde95e2392001-06-06 10:46:28 +00004481 }
4482 prop = prop->next;
4483 }
4484 if (!xmlCheckDTD) return(NULL);
4485
4486 /*
4487 * Check if there is a default declaration in the internal
4488 * or external subsets
4489 */
4490 doc = node->doc;
4491 if (doc != NULL) {
4492 if (doc->intSubset != NULL) {
4493 xmlAttributePtr attrDecl;
4494
4495 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
4496 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4497 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
4498
4499 if ((attrDecl != NULL) && (attrDecl->prefix != NULL)) {
4500 /*
4501 * The DTD declaration only allows a prefix search
4502 */
4503 ns = xmlSearchNs(doc, node, attrDecl->prefix);
Daniel Veillardca2366a2001-06-11 12:09:01 +00004504 if ((ns != NULL) && (xmlStrEqual(ns->href, nameSpace)))
Daniel Veillarde95e2392001-06-06 10:46:28 +00004505 return((xmlAttrPtr) attrDecl);
4506 }
4507 }
4508 }
4509 return(NULL);
4510}
4511
4512/**
Owen Taylor3473f882001-02-23 17:55:21 +00004513 * xmlGetProp:
4514 * @node: the node
4515 * @name: the attribute name
4516 *
4517 * Search and get the value of an attribute associated to a node
4518 * This does the entity substitution.
4519 * This function looks in DTD attribute declaration for #FIXED or
4520 * default declaration values unless DTD use has been turned off.
4521 *
4522 * Returns the attribute value or NULL if not found.
4523 * It's up to the caller to free the memory.
4524 */
4525xmlChar *
4526xmlGetProp(xmlNodePtr node, const xmlChar *name) {
4527 xmlAttrPtr prop;
4528 xmlDocPtr doc;
4529
4530 if ((node == NULL) || (name == NULL)) return(NULL);
4531 /*
4532 * Check on the properties attached to the node
4533 */
4534 prop = node->properties;
4535 while (prop != NULL) {
4536 if (xmlStrEqual(prop->name, name)) {
4537 xmlChar *ret;
4538
4539 ret = xmlNodeListGetString(node->doc, prop->children, 1);
4540 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
4541 return(ret);
4542 }
4543 prop = prop->next;
4544 }
4545 if (!xmlCheckDTD) return(NULL);
4546
4547 /*
4548 * Check if there is a default declaration in the internal
4549 * or external subsets
4550 */
4551 doc = node->doc;
4552 if (doc != NULL) {
4553 xmlAttributePtr attrDecl;
4554 if (doc->intSubset != NULL) {
4555 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
4556 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4557 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
4558 if (attrDecl != NULL)
4559 return(xmlStrdup(attrDecl->defaultValue));
4560 }
4561 }
4562 return(NULL);
4563}
4564
4565/**
4566 * xmlGetNsProp:
4567 * @node: the node
4568 * @name: the attribute name
Daniel Veillardca2366a2001-06-11 12:09:01 +00004569 * @nameSpace: the URI of the namespace
Owen Taylor3473f882001-02-23 17:55:21 +00004570 *
4571 * Search and get the value of an attribute associated to a node
4572 * This attribute has to be anchored in the namespace specified.
4573 * This does the entity substitution.
4574 * This function looks in DTD attribute declaration for #FIXED or
4575 * default declaration values unless DTD use has been turned off.
4576 *
4577 * Returns the attribute value or NULL if not found.
4578 * It's up to the caller to free the memory.
4579 */
4580xmlChar *
Daniel Veillardca2366a2001-06-11 12:09:01 +00004581xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
Owen Taylor3473f882001-02-23 17:55:21 +00004582 xmlAttrPtr prop;
4583 xmlDocPtr doc;
4584 xmlNsPtr ns;
4585
4586 if (node == NULL)
4587 return(NULL);
4588
4589 prop = node->properties;
Daniel Veillardca2366a2001-06-11 12:09:01 +00004590 if (nameSpace == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00004591 return(xmlGetProp(node, name));
4592 while (prop != NULL) {
4593 /*
4594 * One need to have
4595 * - same attribute names
4596 * - and the attribute carrying that namespace
Owen Taylor3473f882001-02-23 17:55:21 +00004597 */
4598 if ((xmlStrEqual(prop->name, name)) &&
Daniel Veillarde8fc08e2001-06-07 19:35:47 +00004599 ((prop->ns != NULL) &&
Daniel Veillardca2366a2001-06-11 12:09:01 +00004600 (xmlStrEqual(prop->ns->href, nameSpace)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00004601 xmlChar *ret;
4602
4603 ret = xmlNodeListGetString(node->doc, prop->children, 1);
4604 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
4605 return(ret);
4606 }
4607 prop = prop->next;
4608 }
4609 if (!xmlCheckDTD) return(NULL);
4610
4611 /*
4612 * Check if there is a default declaration in the internal
4613 * or external subsets
4614 */
4615 doc = node->doc;
4616 if (doc != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004617 if (doc->intSubset != NULL) {
Daniel Veillard5792e162001-04-30 17:44:45 +00004618 xmlAttributePtr attrDecl;
4619
Owen Taylor3473f882001-02-23 17:55:21 +00004620 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
4621 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4622 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
4623
4624 if ((attrDecl != NULL) && (attrDecl->prefix != NULL)) {
4625 /*
4626 * The DTD declaration only allows a prefix search
4627 */
4628 ns = xmlSearchNs(doc, node, attrDecl->prefix);
Daniel Veillardca2366a2001-06-11 12:09:01 +00004629 if ((ns != NULL) && (xmlStrEqual(ns->href, nameSpace)))
Owen Taylor3473f882001-02-23 17:55:21 +00004630 return(xmlStrdup(attrDecl->defaultValue));
4631 }
4632 }
4633 }
4634 return(NULL);
4635}
4636
4637/**
4638 * xmlSetProp:
4639 * @node: the node
4640 * @name: the attribute name
4641 * @value: the attribute value
4642 *
4643 * Set (or reset) an attribute carried by a node.
4644 * Returns the attribute pointer.
4645 */
4646xmlAttrPtr
4647xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
Daniel Veillard4855c8c2001-11-25 10:35:25 +00004648 xmlAttrPtr prop;
4649 xmlDocPtr doc;
Owen Taylor3473f882001-02-23 17:55:21 +00004650
4651 if ((node == NULL) || (name == NULL))
4652 return(NULL);
4653 doc = node->doc;
Daniel Veillard4855c8c2001-11-25 10:35:25 +00004654 prop = node->properties;
Owen Taylor3473f882001-02-23 17:55:21 +00004655 while (prop != NULL) {
Daniel Veillard75bea542001-05-11 17:41:21 +00004656 if ((xmlStrEqual(prop->name, name)) &&
4657 (prop->ns == NULL)){
Daniel Veillard4855c8c2001-11-25 10:35:25 +00004658 xmlNodePtr oldprop = prop->children;
4659
Owen Taylor3473f882001-02-23 17:55:21 +00004660 prop->children = NULL;
4661 prop->last = NULL;
4662 if (value != NULL) {
4663 xmlChar *buffer;
4664 xmlNodePtr tmp;
4665
4666 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
4667 prop->children = xmlStringGetNodeList(node->doc, buffer);
4668 prop->last = NULL;
4669 prop->doc = doc;
4670 tmp = prop->children;
4671 while (tmp != NULL) {
4672 tmp->parent = (xmlNodePtr) prop;
4673 tmp->doc = doc;
4674 if (tmp->next == NULL)
4675 prop->last = tmp;
4676 tmp = tmp->next;
4677 }
4678 xmlFree(buffer);
Daniel Veillard75bea542001-05-11 17:41:21 +00004679 }
Daniel Veillard4855c8c2001-11-25 10:35:25 +00004680 if (oldprop != NULL)
4681 xmlFreeNodeList(oldprop);
Owen Taylor3473f882001-02-23 17:55:21 +00004682 return(prop);
4683 }
4684 prop = prop->next;
4685 }
4686 prop = xmlNewProp(node, name, value);
4687 return(prop);
4688}
4689
4690/**
Daniel Veillard75bea542001-05-11 17:41:21 +00004691 * xmlUnsetProp:
4692 * @node: the node
4693 * @name: the attribute name
4694 *
4695 * Remove an attribute carried by a node.
4696 * Returns 0 if successful, -1 if not found
4697 */
4698int
4699xmlUnsetProp(xmlNodePtr node, const xmlChar *name) {
4700 xmlAttrPtr prop = node->properties, prev = NULL;;
4701
4702 if ((node == NULL) || (name == NULL))
4703 return(-1);
4704 while (prop != NULL) {
4705 if ((xmlStrEqual(prop->name, name)) &&
4706 (prop->ns == NULL)) {
4707 if (prev == NULL)
4708 node->properties = prop->next;
4709 else
4710 prev->next = prop->next;
4711 xmlFreeProp(prop);
4712 return(0);
4713 }
4714 prev = prop;
4715 prop = prop->next;
4716 }
4717 return(-1);
4718}
4719
4720/**
Owen Taylor3473f882001-02-23 17:55:21 +00004721 * xmlSetNsProp:
4722 * @node: the node
4723 * @ns: the namespace definition
4724 * @name: the attribute name
4725 * @value: the attribute value
4726 *
4727 * Set (or reset) an attribute carried by a node.
4728 * The ns structure must be in scope, this is not checked.
4729 *
4730 * Returns the attribute pointer.
4731 */
4732xmlAttrPtr
4733xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
4734 const xmlChar *value) {
4735 xmlAttrPtr prop;
4736
4737 if ((node == NULL) || (name == NULL))
4738 return(NULL);
4739
4740 if (ns == NULL)
4741 return(xmlSetProp(node, name, value));
4742 if (ns->href == NULL)
4743 return(NULL);
4744 prop = node->properties;
4745
4746 while (prop != NULL) {
4747 /*
4748 * One need to have
4749 * - same attribute names
4750 * - and the attribute carrying that namespace
4751 * or
4752 * no namespace on the attribute and the element carrying it
4753 */
4754 if ((xmlStrEqual(prop->name, name)) &&
4755 (((prop->ns == NULL) && (node->ns != NULL) &&
4756 (xmlStrEqual(node->ns->href, ns->href))) ||
4757 ((prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))))) {
4758 if (prop->children != NULL)
4759 xmlFreeNodeList(prop->children);
4760 prop->children = NULL;
4761 prop->last = NULL;
4762 prop->ns = ns;
4763 if (value != NULL) {
4764 xmlChar *buffer;
4765 xmlNodePtr tmp;
4766
4767 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
4768 prop->children = xmlStringGetNodeList(node->doc, buffer);
4769 prop->last = NULL;
4770 tmp = prop->children;
4771 while (tmp != NULL) {
4772 tmp->parent = (xmlNodePtr) prop;
4773 if (tmp->next == NULL)
4774 prop->last = tmp;
4775 tmp = tmp->next;
4776 }
4777 xmlFree(buffer);
4778 }
4779 return(prop);
4780 }
4781 prop = prop->next;
4782 }
4783 prop = xmlNewNsProp(node, ns, name, value);
4784 return(prop);
4785}
4786
4787/**
Daniel Veillard75bea542001-05-11 17:41:21 +00004788 * xmlUnsetNsProp:
4789 * @node: the node
4790 * @ns: the namespace definition
4791 * @name: the attribute name
4792 *
4793 * Remove an attribute carried by a node.
4794 * Returns 0 if successful, -1 if not found
4795 */
4796int
4797xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) {
4798 xmlAttrPtr prop = node->properties, prev = NULL;;
4799
4800 if ((node == NULL) || (name == NULL))
4801 return(-1);
4802 if (ns == NULL)
4803 return(xmlUnsetProp(node, name));
4804 if (ns->href == NULL)
4805 return(-1);
4806 while (prop != NULL) {
4807 if ((xmlStrEqual(prop->name, name)) &&
4808 (((prop->ns == NULL) && (node->ns != NULL) &&
4809 (xmlStrEqual(node->ns->href, ns->href))) ||
4810 ((prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))))) {
4811 if (prev == NULL)
4812 node->properties = prop->next;
4813 else
4814 prev->next = prop->next;
4815 xmlFreeProp(prop);
4816 return(0);
4817 }
4818 prev = prop;
4819 prop = prop->next;
4820 }
4821 return(-1);
4822}
4823
4824/**
Owen Taylor3473f882001-02-23 17:55:21 +00004825 * xmlNodeIsText:
4826 * @node: the node
4827 *
4828 * Is this node a Text node ?
4829 * Returns 1 yes, 0 no
4830 */
4831int
4832xmlNodeIsText(xmlNodePtr node) {
4833 if (node == NULL) return(0);
4834
4835 if (node->type == XML_TEXT_NODE) return(1);
4836 return(0);
4837}
4838
4839/**
4840 * xmlIsBlankNode:
4841 * @node: the node
4842 *
4843 * Checks whether this node is an empty or whitespace only
4844 * (and possibly ignorable) text-node.
4845 *
4846 * Returns 1 yes, 0 no
4847 */
4848int
4849xmlIsBlankNode(xmlNodePtr node) {
4850 const xmlChar *cur;
4851 if (node == NULL) return(0);
4852
Daniel Veillard7db37732001-07-12 01:20:08 +00004853 if ((node->type != XML_TEXT_NODE) &&
4854 (node->type != XML_CDATA_SECTION_NODE))
4855 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004856 if (node->content == NULL) return(1);
4857#ifndef XML_USE_BUFFER_CONTENT
4858 cur = node->content;
4859#else
4860 cur = xmlBufferContent(node->content);
4861#endif
4862 while (*cur != 0) {
4863 if (!IS_BLANK(*cur)) return(0);
4864 cur++;
4865 }
4866
4867 return(1);
4868}
4869
4870/**
4871 * xmlTextConcat:
4872 * @node: the node
4873 * @content: the content
Daniel Veillard60087f32001-10-10 09:45:09 +00004874 * @len: @content length
Owen Taylor3473f882001-02-23 17:55:21 +00004875 *
4876 * Concat the given string at the end of the existing node content
4877 */
4878
4879void
4880xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) {
4881 if (node == NULL) return;
4882
4883 if ((node->type != XML_TEXT_NODE) &&
4884 (node->type != XML_CDATA_SECTION_NODE)) {
4885#ifdef DEBUG_TREE
4886 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00004887 "xmlTextConcat: node is not text nor CDATA\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004888#endif
4889 return;
4890 }
4891#ifndef XML_USE_BUFFER_CONTENT
4892 node->content = xmlStrncat(node->content, content, len);
4893#else
4894 xmlBufferAdd(node->content, content, len);
4895#endif
4896}
4897
4898/************************************************************************
4899 * *
4900 * Output : to a FILE or in memory *
4901 * *
4902 ************************************************************************/
4903
Owen Taylor3473f882001-02-23 17:55:21 +00004904/**
4905 * xmlBufferCreate:
4906 *
4907 * routine to create an XML buffer.
4908 * returns the new structure.
4909 */
4910xmlBufferPtr
4911xmlBufferCreate(void) {
4912 xmlBufferPtr ret;
4913
4914 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
4915 if (ret == NULL) {
4916 xmlGenericError(xmlGenericErrorContext,
4917 "xmlBufferCreate : out of memory!\n");
4918 return(NULL);
4919 }
4920 ret->use = 0;
Daniel Veillarde356c282001-03-10 12:32:04 +00004921 ret->size = xmlDefaultBufferSize;
Owen Taylor3473f882001-02-23 17:55:21 +00004922 ret->alloc = xmlBufferAllocScheme;
4923 ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar));
4924 if (ret->content == NULL) {
4925 xmlGenericError(xmlGenericErrorContext,
4926 "xmlBufferCreate : out of memory!\n");
4927 xmlFree(ret);
4928 return(NULL);
4929 }
4930 ret->content[0] = 0;
4931 return(ret);
4932}
4933
4934/**
4935 * xmlBufferCreateSize:
4936 * @size: initial size of buffer
4937 *
4938 * routine to create an XML buffer.
4939 * returns the new structure.
4940 */
4941xmlBufferPtr
4942xmlBufferCreateSize(size_t size) {
4943 xmlBufferPtr ret;
4944
4945 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
4946 if (ret == NULL) {
4947 xmlGenericError(xmlGenericErrorContext,
4948 "xmlBufferCreate : out of memory!\n");
4949 return(NULL);
4950 }
4951 ret->use = 0;
4952 ret->alloc = xmlBufferAllocScheme;
4953 ret->size = (size ? size+2 : 0); /* +1 for ending null */
4954 if (ret->size){
4955 ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar));
4956 if (ret->content == NULL) {
4957 xmlGenericError(xmlGenericErrorContext,
4958 "xmlBufferCreate : out of memory!\n");
4959 xmlFree(ret);
4960 return(NULL);
4961 }
4962 ret->content[0] = 0;
4963 } else
4964 ret->content = NULL;
4965 return(ret);
4966}
4967
4968/**
4969 * xmlBufferSetAllocationScheme:
4970 * @buf: the buffer to free
4971 * @scheme: allocation scheme to use
4972 *
4973 * Sets the allocation scheme for this buffer
4974 */
4975void
4976xmlBufferSetAllocationScheme(xmlBufferPtr buf,
4977 xmlBufferAllocationScheme scheme) {
4978 if (buf == NULL) {
4979#ifdef DEBUG_BUFFER
4980 xmlGenericError(xmlGenericErrorContext,
4981 "xmlBufferSetAllocationScheme: buf == NULL\n");
4982#endif
4983 return;
4984 }
4985
4986 buf->alloc = scheme;
4987}
4988
4989/**
4990 * xmlBufferFree:
4991 * @buf: the buffer to free
4992 *
4993 * Frees an XML buffer.
4994 */
4995void
4996xmlBufferFree(xmlBufferPtr buf) {
4997 if (buf == NULL) {
4998#ifdef DEBUG_BUFFER
4999 xmlGenericError(xmlGenericErrorContext,
5000 "xmlBufferFree: buf == NULL\n");
5001#endif
5002 return;
5003 }
5004 if (buf->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005005 xmlFree(buf->content);
5006 }
Owen Taylor3473f882001-02-23 17:55:21 +00005007 xmlFree(buf);
5008}
5009
5010/**
5011 * xmlBufferEmpty:
5012 * @buf: the buffer
5013 *
5014 * empty a buffer.
5015 */
5016void
5017xmlBufferEmpty(xmlBufferPtr buf) {
5018 if (buf->content == NULL) return;
5019 buf->use = 0;
Daniel Veillard92ad2102001-03-27 12:47:33 +00005020 memset(buf->content, 0, buf->size);
Owen Taylor3473f882001-02-23 17:55:21 +00005021}
5022
5023/**
5024 * xmlBufferShrink:
5025 * @buf: the buffer to dump
5026 * @len: the number of xmlChar to remove
5027 *
5028 * Remove the beginning of an XML buffer.
5029 *
Daniel Veillardd1640922001-12-17 15:30:10 +00005030 * Returns the number of #xmlChar removed, or -1 in case of failure.
Owen Taylor3473f882001-02-23 17:55:21 +00005031 */
5032int
5033xmlBufferShrink(xmlBufferPtr buf, unsigned int len) {
5034 if (len == 0) return(0);
5035 if (len > buf->use) return(-1);
5036
5037 buf->use -= len;
5038 memmove(buf->content, &buf->content[len], buf->use * sizeof(xmlChar));
5039
5040 buf->content[buf->use] = 0;
5041 return(len);
5042}
5043
5044/**
5045 * xmlBufferGrow:
5046 * @buf: the buffer
5047 * @len: the minimum free size to allocate
5048 *
5049 * Grow the available space of an XML buffer.
5050 *
5051 * Returns the new available space or -1 in case of error
5052 */
5053int
5054xmlBufferGrow(xmlBufferPtr buf, unsigned int len) {
5055 int size;
5056 xmlChar *newbuf;
5057
5058 if (len + buf->use < buf->size) return(0);
5059
5060 size = buf->use + len + 100;
5061
5062 newbuf = (xmlChar *) xmlRealloc(buf->content, size);
5063 if (newbuf == NULL) return(-1);
5064 buf->content = newbuf;
5065 buf->size = size;
5066 return(buf->size - buf->use);
5067}
5068
5069/**
5070 * xmlBufferDump:
5071 * @file: the file output
5072 * @buf: the buffer to dump
5073 *
5074 * Dumps an XML buffer to a FILE *.
Daniel Veillardd1640922001-12-17 15:30:10 +00005075 * Returns the number of #xmlChar written
Owen Taylor3473f882001-02-23 17:55:21 +00005076 */
5077int
5078xmlBufferDump(FILE *file, xmlBufferPtr buf) {
5079 int ret;
5080
5081 if (buf == NULL) {
5082#ifdef DEBUG_BUFFER
5083 xmlGenericError(xmlGenericErrorContext,
5084 "xmlBufferDump: buf == NULL\n");
5085#endif
5086 return(0);
5087 }
5088 if (buf->content == NULL) {
5089#ifdef DEBUG_BUFFER
5090 xmlGenericError(xmlGenericErrorContext,
5091 "xmlBufferDump: buf->content == NULL\n");
5092#endif
5093 return(0);
5094 }
Daniel Veillardcd337f02001-11-22 18:20:37 +00005095 if (file == NULL)
5096 file = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00005097 ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
5098 return(ret);
5099}
5100
5101/**
5102 * xmlBufferContent:
5103 * @buf: the buffer
5104 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00005105 * Function to extract the content of a buffer
5106 *
Owen Taylor3473f882001-02-23 17:55:21 +00005107 * Returns the internal content
5108 */
5109
Daniel Veillard5e2dace2001-07-18 19:30:27 +00005110const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00005111xmlBufferContent(const xmlBufferPtr buf)
5112{
5113 if(!buf)
5114 return NULL;
5115
5116 return buf->content;
5117}
5118
5119/**
5120 * xmlBufferLength:
5121 * @buf: the buffer
5122 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00005123 * Function to get the length of a buffer
5124 *
Owen Taylor3473f882001-02-23 17:55:21 +00005125 * Returns the length of data in the internal content
5126 */
5127
5128int
5129xmlBufferLength(const xmlBufferPtr buf)
5130{
5131 if(!buf)
5132 return 0;
5133
5134 return buf->use;
5135}
5136
5137/**
5138 * xmlBufferResize:
5139 * @buf: the buffer to resize
5140 * @size: the desired size
5141 *
Daniel Veillardd1640922001-12-17 15:30:10 +00005142 * Resize a buffer to accommodate minimum size of @size.
Owen Taylor3473f882001-02-23 17:55:21 +00005143 *
5144 * Returns 0 in case of problems, 1 otherwise
5145 */
5146int
5147xmlBufferResize(xmlBufferPtr buf, unsigned int size)
5148{
5149 unsigned int newSize;
5150 xmlChar* rebuf = NULL;
5151
5152 /*take care of empty case*/
5153 newSize = (buf->size ? buf->size*2 : size);
5154
5155 /* Don't resize if we don't have to */
5156 if (size < buf->size)
5157 return 1;
5158
5159 /* figure out new size */
5160 switch (buf->alloc){
5161 case XML_BUFFER_ALLOC_DOUBLEIT:
5162 while (size > newSize) newSize *= 2;
5163 break;
5164 case XML_BUFFER_ALLOC_EXACT:
5165 newSize = size+10;
5166 break;
5167 default:
5168 newSize = size+10;
5169 break;
5170 }
5171
5172 if (buf->content == NULL)
5173 rebuf = (xmlChar *) xmlMalloc(newSize * sizeof(xmlChar));
5174 else
5175 rebuf = (xmlChar *) xmlRealloc(buf->content,
5176 newSize * sizeof(xmlChar));
5177 if (rebuf == NULL) {
5178 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00005179 "xmlBufferResize : out of memory!\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005180 return 0;
5181 }
5182 buf->content = rebuf;
5183 buf->size = newSize;
5184
5185 return 1;
5186}
5187
5188/**
5189 * xmlBufferAdd:
5190 * @buf: the buffer to dump
Daniel Veillardd1640922001-12-17 15:30:10 +00005191 * @str: the #xmlChar string
5192 * @len: the number of #xmlChar to add
Owen Taylor3473f882001-02-23 17:55:21 +00005193 *
Daniel Veillard60087f32001-10-10 09:45:09 +00005194 * Add a string range to an XML buffer. if len == -1, the length of
Owen Taylor3473f882001-02-23 17:55:21 +00005195 * str is recomputed.
5196 */
5197void
5198xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) {
5199 unsigned int needSize;
5200
5201 if (str == NULL) {
5202#ifdef DEBUG_BUFFER
5203 xmlGenericError(xmlGenericErrorContext,
5204 "xmlBufferAdd: str == NULL\n");
5205#endif
5206 return;
5207 }
5208 if (len < -1) {
5209#ifdef DEBUG_BUFFER
5210 xmlGenericError(xmlGenericErrorContext,
5211 "xmlBufferAdd: len < 0\n");
5212#endif
5213 return;
5214 }
5215 if (len == 0) return;
5216
5217 if (len < 0)
5218 len = xmlStrlen(str);
5219
5220 if (len <= 0) return;
5221
5222 needSize = buf->use + len + 2;
5223 if (needSize > buf->size){
5224 if (!xmlBufferResize(buf, needSize)){
5225 xmlGenericError(xmlGenericErrorContext,
5226 "xmlBufferAdd : out of memory!\n");
5227 return;
5228 }
5229 }
5230
5231 memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
5232 buf->use += len;
5233 buf->content[buf->use] = 0;
5234}
5235
5236/**
5237 * xmlBufferAddHead:
5238 * @buf: the buffer
Daniel Veillardd1640922001-12-17 15:30:10 +00005239 * @str: the #xmlChar string
5240 * @len: the number of #xmlChar to add
Owen Taylor3473f882001-02-23 17:55:21 +00005241 *
5242 * Add a string range to the beginning of an XML buffer.
Daniel Veillard60087f32001-10-10 09:45:09 +00005243 * if len == -1, the length of @str is recomputed.
Owen Taylor3473f882001-02-23 17:55:21 +00005244 */
5245void
5246xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) {
5247 unsigned int needSize;
5248
5249 if (str == NULL) {
5250#ifdef DEBUG_BUFFER
5251 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00005252 "xmlBufferAddHead: str == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005253#endif
5254 return;
5255 }
5256 if (len < -1) {
5257#ifdef DEBUG_BUFFER
5258 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00005259 "xmlBufferAddHead: len < 0\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005260#endif
5261 return;
5262 }
5263 if (len == 0) return;
5264
5265 if (len < 0)
5266 len = xmlStrlen(str);
5267
5268 if (len <= 0) return;
5269
5270 needSize = buf->use + len + 2;
5271 if (needSize > buf->size){
5272 if (!xmlBufferResize(buf, needSize)){
5273 xmlGenericError(xmlGenericErrorContext,
5274 "xmlBufferAddHead : out of memory!\n");
5275 return;
5276 }
5277 }
5278
5279 memmove(&buf->content[len], &buf->content[0], buf->use * sizeof(xmlChar));
5280 memmove(&buf->content[0], str, len * sizeof(xmlChar));
5281 buf->use += len;
5282 buf->content[buf->use] = 0;
5283}
5284
5285/**
5286 * xmlBufferCat:
5287 * @buf: the buffer to dump
Daniel Veillardd1640922001-12-17 15:30:10 +00005288 * @str: the #xmlChar string
Owen Taylor3473f882001-02-23 17:55:21 +00005289 *
5290 * Append a zero terminated string to an XML buffer.
5291 */
5292void
5293xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) {
5294 if (str != NULL)
5295 xmlBufferAdd(buf, str, -1);
5296}
5297
5298/**
5299 * xmlBufferCCat:
5300 * @buf: the buffer to dump
5301 * @str: the C char string
5302 *
5303 * Append a zero terminated C string to an XML buffer.
5304 */
5305void
5306xmlBufferCCat(xmlBufferPtr buf, const char *str) {
5307 const char *cur;
5308
5309 if (str == NULL) {
5310#ifdef DEBUG_BUFFER
5311 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00005312 "xmlBufferCCat: str == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005313#endif
5314 return;
5315 }
5316 for (cur = str;*cur != 0;cur++) {
5317 if (buf->use + 10 >= buf->size) {
5318 if (!xmlBufferResize(buf, buf->use+10)){
5319 xmlGenericError(xmlGenericErrorContext,
5320 "xmlBufferCCat : out of memory!\n");
5321 return;
5322 }
5323 }
5324 buf->content[buf->use++] = *cur;
5325 }
5326 buf->content[buf->use] = 0;
5327}
5328
5329/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +00005330 * xmlBufferWriteXmlCHAR:
5331 * @buf: the XML buffer
5332 * @string: the string to add
5333 *
5334 * For VMS only.
5335 * routine which manages and grows an output buffer. This one adds
5336 * xmlChars at the end of the buffer.
5337 */
5338/**
Owen Taylor3473f882001-02-23 17:55:21 +00005339 * xmlBufferWriteCHAR:
5340 * @buf: the XML buffer
5341 * @string: the string to add
5342 *
5343 * routine which manages and grows an output buffer. This one adds
5344 * xmlChars at the end of the buffer.
5345 */
5346void
5347#ifdef VMS
5348xmlBufferWriteXmlCHAR
5349#else
5350xmlBufferWriteCHAR
5351#endif
5352(xmlBufferPtr buf, const xmlChar *string) {
5353 xmlBufferCat(buf, string);
5354}
5355
5356/**
5357 * xmlBufferWriteChar:
5358 * @buf: the XML buffer output
5359 * @string: the string to add
5360 *
5361 * routine which manage and grows an output buffer. This one add
5362 * C chars at the end of the array.
5363 */
5364void
5365xmlBufferWriteChar(xmlBufferPtr buf, const char *string) {
5366 xmlBufferCCat(buf, string);
5367}
5368
5369
5370/**
5371 * xmlBufferWriteQuotedString:
5372 * @buf: the XML buffer output
5373 * @string: the string to add
5374 *
5375 * routine which manage and grows an output buffer. This one writes
Daniel Veillardd1640922001-12-17 15:30:10 +00005376 * a quoted or double quoted #xmlChar string, checking first if it holds
Owen Taylor3473f882001-02-23 17:55:21 +00005377 * quote or double-quotes internally
5378 */
5379void
5380xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) {
5381 if (xmlStrchr(string, '"')) {
5382 if (xmlStrchr(string, '\'')) {
5383#ifdef DEBUG_BUFFER
5384 xmlGenericError(xmlGenericErrorContext,
5385 "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n");
5386#endif
5387 }
5388 xmlBufferCCat(buf, "'");
5389 xmlBufferCat(buf, string);
5390 xmlBufferCCat(buf, "'");
5391 } else {
5392 xmlBufferCCat(buf, "\"");
5393 xmlBufferCat(buf, string);
5394 xmlBufferCCat(buf, "\"");
5395 }
5396}
5397
5398
5399/************************************************************************
5400 * *
5401 * Dumping XML tree content to a simple buffer *
5402 * *
5403 ************************************************************************/
5404
5405void
5406xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
5407 int format);
5408static void
5409xmlNodeListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
5410 int format);
5411void
5412htmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur);
5413
5414/**
5415 * xmlNsDump:
5416 * @buf: the XML buffer output
5417 * @cur: a namespace
5418 *
5419 * Dump a local Namespace definition.
5420 * Should be called in the context of attributes dumps.
5421 */
5422static void
5423xmlNsDump(xmlBufferPtr buf, xmlNsPtr cur) {
5424 if (cur == NULL) {
5425#ifdef DEBUG_TREE
5426 xmlGenericError(xmlGenericErrorContext,
5427 "xmlNsDump : Ns == NULL\n");
5428#endif
5429 return;
5430 }
5431 if (cur->type == XML_LOCAL_NAMESPACE) {
5432 /* Within the context of an element attributes */
5433 if (cur->prefix != NULL) {
5434 xmlBufferWriteChar(buf, " xmlns:");
5435 xmlBufferWriteCHAR(buf, cur->prefix);
5436 } else
5437 xmlBufferWriteChar(buf, " xmlns");
5438 xmlBufferWriteChar(buf, "=");
5439 xmlBufferWriteQuotedString(buf, cur->href);
5440 }
5441}
5442
5443/**
5444 * xmlNsListDump:
5445 * @buf: the XML buffer output
5446 * @cur: the first namespace
5447 *
5448 * Dump a list of local Namespace definitions.
5449 * Should be called in the context of attributes dumps.
5450 */
5451static void
5452xmlNsListDump(xmlBufferPtr buf, xmlNsPtr cur) {
5453 while (cur != NULL) {
5454 xmlNsDump(buf, cur);
5455 cur = cur->next;
5456 }
5457}
5458
5459/**
5460 * xmlDtdDump:
5461 * @buf: the XML buffer output
Daniel Veillardd1640922001-12-17 15:30:10 +00005462 * @dtd: the DTD
Owen Taylor3473f882001-02-23 17:55:21 +00005463 *
5464 * Dump the XML document DTD, if any.
5465 */
5466static void
5467xmlDtdDump(xmlBufferPtr buf, xmlDtdPtr dtd) {
5468 if (dtd == NULL) {
5469#ifdef DEBUG_TREE
5470 xmlGenericError(xmlGenericErrorContext,
5471 "xmlDtdDump : no internal subset\n");
5472#endif
5473 return;
5474 }
5475 xmlBufferWriteChar(buf, "<!DOCTYPE ");
5476 xmlBufferWriteCHAR(buf, dtd->name);
5477 if (dtd->ExternalID != NULL) {
5478 xmlBufferWriteChar(buf, " PUBLIC ");
5479 xmlBufferWriteQuotedString(buf, dtd->ExternalID);
5480 xmlBufferWriteChar(buf, " ");
5481 xmlBufferWriteQuotedString(buf, dtd->SystemID);
5482 } else if (dtd->SystemID != NULL) {
5483 xmlBufferWriteChar(buf, " SYSTEM ");
5484 xmlBufferWriteQuotedString(buf, dtd->SystemID);
5485 }
5486 if ((dtd->entities == NULL) && (dtd->elements == NULL) &&
5487 (dtd->attributes == NULL) && (dtd->notations == NULL)) {
5488 xmlBufferWriteChar(buf, ">");
5489 return;
5490 }
5491 xmlBufferWriteChar(buf, " [\n");
5492 xmlNodeListDump(buf, dtd->doc, dtd->children, -1, 0);
5493#if 0
5494 if (dtd->entities != NULL)
5495 xmlDumpEntitiesTable(buf, (xmlEntitiesTablePtr) dtd->entities);
5496 if (dtd->notations != NULL)
5497 xmlDumpNotationTable(buf, (xmlNotationTablePtr) dtd->notations);
5498 if (dtd->elements != NULL)
5499 xmlDumpElementTable(buf, (xmlElementTablePtr) dtd->elements);
5500 if (dtd->attributes != NULL)
5501 xmlDumpAttributeTable(buf, (xmlAttributeTablePtr) dtd->attributes);
5502#endif
5503 xmlBufferWriteChar(buf, "]>");
5504}
5505
5506/**
5507 * xmlAttrDump:
5508 * @buf: the XML buffer output
5509 * @doc: the document
5510 * @cur: the attribute pointer
5511 *
5512 * Dump an XML attribute
5513 */
5514static void
5515xmlAttrDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) {
5516 xmlChar *value;
5517
5518 if (cur == NULL) {
5519#ifdef DEBUG_TREE
5520 xmlGenericError(xmlGenericErrorContext,
5521 "xmlAttrDump : property == NULL\n");
5522#endif
5523 return;
5524 }
5525 xmlBufferWriteChar(buf, " ");
5526 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
5527 xmlBufferWriteCHAR(buf, cur->ns->prefix);
5528 xmlBufferWriteChar(buf, ":");
5529 }
5530 xmlBufferWriteCHAR(buf, cur->name);
5531 value = xmlNodeListGetString(doc, cur->children, 0);
5532 if (value != NULL) {
5533 xmlBufferWriteChar(buf, "=");
5534 xmlBufferWriteQuotedString(buf, value);
5535 xmlFree(value);
5536 } else {
5537 xmlBufferWriteChar(buf, "=\"\"");
5538 }
5539}
5540
5541/**
5542 * xmlAttrListDump:
5543 * @buf: the XML buffer output
5544 * @doc: the document
5545 * @cur: the first attribute pointer
5546 *
5547 * Dump a list of XML attributes
5548 */
5549static void
5550xmlAttrListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) {
5551 if (cur == NULL) {
5552#ifdef DEBUG_TREE
5553 xmlGenericError(xmlGenericErrorContext,
5554 "xmlAttrListDump : property == NULL\n");
5555#endif
5556 return;
5557 }
5558 while (cur != NULL) {
5559 xmlAttrDump(buf, doc, cur);
5560 cur = cur->next;
5561 }
5562}
5563
5564
5565
5566/**
5567 * xmlNodeListDump:
5568 * @buf: the XML buffer output
5569 * @doc: the document
5570 * @cur: the first node
5571 * @level: the imbrication level for indenting
5572 * @format: is formatting allowed
5573 *
5574 * Dump an XML node list, recursive behaviour,children are printed too.
5575 */
5576static void
5577xmlNodeListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
5578 int format) {
5579 int i;
5580
5581 if (cur == NULL) {
5582#ifdef DEBUG_TREE
5583 xmlGenericError(xmlGenericErrorContext,
5584 "xmlNodeListDump : node == NULL\n");
5585#endif
5586 return;
5587 }
5588 while (cur != NULL) {
5589 if ((format) && (xmlIndentTreeOutput) &&
5590 (cur->type == XML_ELEMENT_NODE))
5591 for (i = 0;i < level;i++)
5592 xmlBufferWriteChar(buf, " ");
5593 xmlNodeDump(buf, doc, cur, level, format);
5594 if (format) {
5595 xmlBufferWriteChar(buf, "\n");
5596 }
5597 cur = cur->next;
5598 }
5599}
5600
5601/**
5602 * xmlNodeDump:
5603 * @buf: the XML buffer output
5604 * @doc: the document
5605 * @cur: the current node
5606 * @level: the imbrication level for indenting
5607 * @format: is formatting allowed
5608 *
5609 * Dump an XML node, recursive behaviour,children are printed too.
5610 */
5611void
5612xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
5613 int format) {
5614 int i;
5615 xmlNodePtr tmp;
5616
5617 if (cur == NULL) {
5618#ifdef DEBUG_TREE
5619 xmlGenericError(xmlGenericErrorContext,
5620 "xmlNodeDump : node == NULL\n");
5621#endif
5622 return;
5623 }
5624 if (cur->type == XML_XINCLUDE_START)
5625 return;
5626 if (cur->type == XML_XINCLUDE_END)
5627 return;
5628 if (cur->type == XML_DTD_NODE) {
5629 xmlDtdDump(buf, (xmlDtdPtr) cur);
5630 return;
5631 }
5632 if (cur->type == XML_ELEMENT_DECL) {
5633 xmlDumpElementDecl(buf, (xmlElementPtr) cur);
5634 return;
5635 }
Daniel Veillard78d12092001-10-11 09:12:24 +00005636 if (cur->type == XML_ATTRIBUTE_NODE){
5637 xmlAttrDump(buf, doc, (xmlAttrPtr)cur);
5638 return;
5639 }
Owen Taylor3473f882001-02-23 17:55:21 +00005640 if (cur->type == XML_ATTRIBUTE_DECL) {
5641 xmlDumpAttributeDecl(buf, (xmlAttributePtr) cur);
5642 return;
5643 }
5644 if (cur->type == XML_ENTITY_DECL) {
5645 xmlDumpEntityDecl(buf, (xmlEntityPtr) cur);
5646 return;
5647 }
5648 if (cur->type == XML_TEXT_NODE) {
5649 if (cur->content != NULL) {
5650 if ((cur->name == xmlStringText) ||
5651 (cur->name != xmlStringTextNoenc)) {
5652 xmlChar *buffer;
5653
5654#ifndef XML_USE_BUFFER_CONTENT
5655 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
5656#else
5657 buffer = xmlEncodeEntitiesReentrant(doc,
5658 xmlBufferContent(cur->content));
5659#endif
5660 if (buffer != NULL) {
5661 xmlBufferWriteCHAR(buf, buffer);
5662 xmlFree(buffer);
5663 }
5664 } else {
5665 /*
5666 * Disable escaping, needed for XSLT
5667 */
5668#ifndef XML_USE_BUFFER_CONTENT
5669 xmlBufferWriteCHAR(buf, cur->content);
5670#else
5671 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
5672#endif
5673 }
5674 }
5675 return;
5676 }
5677 if (cur->type == XML_PI_NODE) {
5678 if (cur->content != NULL) {
5679 xmlBufferWriteChar(buf, "<?");
5680 xmlBufferWriteCHAR(buf, cur->name);
5681 if (cur->content != NULL) {
5682 xmlBufferWriteChar(buf, " ");
5683#ifndef XML_USE_BUFFER_CONTENT
5684 xmlBufferWriteCHAR(buf, cur->content);
5685#else
5686 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
5687#endif
5688 }
5689 xmlBufferWriteChar(buf, "?>");
5690 } else {
5691 xmlBufferWriteChar(buf, "<?");
5692 xmlBufferWriteCHAR(buf, cur->name);
5693 xmlBufferWriteChar(buf, "?>");
5694 }
5695 return;
5696 }
5697 if (cur->type == XML_COMMENT_NODE) {
5698 if (cur->content != NULL) {
5699 xmlBufferWriteChar(buf, "<!--");
5700#ifndef XML_USE_BUFFER_CONTENT
5701 xmlBufferWriteCHAR(buf, cur->content);
5702#else
5703 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
5704#endif
5705 xmlBufferWriteChar(buf, "-->");
5706 }
5707 return;
5708 }
5709 if (cur->type == XML_ENTITY_REF_NODE) {
5710 xmlBufferWriteChar(buf, "&");
5711 xmlBufferWriteCHAR(buf, cur->name);
5712 xmlBufferWriteChar(buf, ";");
5713 return;
5714 }
5715 if (cur->type == XML_CDATA_SECTION_NODE) {
5716 xmlBufferWriteChar(buf, "<![CDATA[");
5717 if (cur->content != NULL)
5718#ifndef XML_USE_BUFFER_CONTENT
5719 xmlBufferWriteCHAR(buf, cur->content);
5720#else
5721 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
5722#endif
5723 xmlBufferWriteChar(buf, "]]>");
5724 return;
5725 }
5726
5727 if (format == 1) {
5728 tmp = cur->children;
5729 while (tmp != NULL) {
5730 if ((tmp->type == XML_TEXT_NODE) ||
5731 (tmp->type == XML_ENTITY_REF_NODE)) {
5732 format = 0;
5733 break;
5734 }
5735 tmp = tmp->next;
5736 }
5737 }
5738 xmlBufferWriteChar(buf, "<");
5739 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
5740 xmlBufferWriteCHAR(buf, cur->ns->prefix);
5741 xmlBufferWriteChar(buf, ":");
5742 }
5743
5744 xmlBufferWriteCHAR(buf, cur->name);
5745 if (cur->nsDef)
5746 xmlNsListDump(buf, cur->nsDef);
5747 if (cur->properties != NULL)
5748 xmlAttrListDump(buf, doc, cur->properties);
5749
Daniel Veillard7db37732001-07-12 01:20:08 +00005750 if (((cur->type == XML_ELEMENT_NODE) || (cur->content == NULL)) &&
5751 (cur->children == NULL) &&
Owen Taylor3473f882001-02-23 17:55:21 +00005752 (!xmlSaveNoEmptyTags)) {
5753 xmlBufferWriteChar(buf, "/>");
5754 return;
5755 }
5756 xmlBufferWriteChar(buf, ">");
Daniel Veillard7db37732001-07-12 01:20:08 +00005757 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005758 xmlChar *buffer;
5759
5760#ifndef XML_USE_BUFFER_CONTENT
5761 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
5762#else
5763 buffer = xmlEncodeEntitiesReentrant(doc,
5764 xmlBufferContent(cur->content));
5765#endif
5766 if (buffer != NULL) {
5767 xmlBufferWriteCHAR(buf, buffer);
5768 xmlFree(buffer);
5769 }
5770 }
5771 if (cur->children != NULL) {
5772 if (format) xmlBufferWriteChar(buf, "\n");
5773 xmlNodeListDump(buf, doc, cur->children,
5774 (level >= 0?level+1:-1), format);
5775 if ((xmlIndentTreeOutput) && (format))
5776 for (i = 0;i < level;i++)
5777 xmlBufferWriteChar(buf, " ");
5778 }
5779 xmlBufferWriteChar(buf, "</");
5780 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
5781 xmlBufferWriteCHAR(buf, cur->ns->prefix);
5782 xmlBufferWriteChar(buf, ":");
5783 }
5784
5785 xmlBufferWriteCHAR(buf, cur->name);
5786 xmlBufferWriteChar(buf, ">");
5787}
5788
5789/**
5790 * xmlElemDump:
5791 * @f: the FILE * for the output
5792 * @doc: the document
5793 * @cur: the current node
5794 *
Daniel Veillardd1640922001-12-17 15:30:10 +00005795 * Dump an XML/HTML node, recursive behaviour, children are printed too.
Owen Taylor3473f882001-02-23 17:55:21 +00005796 */
5797void
5798xmlElemDump(FILE *f, xmlDocPtr doc, xmlNodePtr cur) {
5799 xmlBufferPtr buf;
5800
5801 if (cur == NULL) {
5802#ifdef DEBUG_TREE
5803 xmlGenericError(xmlGenericErrorContext,
5804 "xmlElemDump : cur == NULL\n");
5805#endif
5806 return;
5807 }
Owen Taylor3473f882001-02-23 17:55:21 +00005808#ifdef DEBUG_TREE
Daniel Veillardd79bcd12001-06-21 22:07:42 +00005809 if (doc == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005810 xmlGenericError(xmlGenericErrorContext,
5811 "xmlElemDump : doc == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005812 }
Daniel Veillardd79bcd12001-06-21 22:07:42 +00005813#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00005814
Owen Taylor3473f882001-02-23 17:55:21 +00005815 buf = xmlBufferCreate();
5816 if (buf == NULL) return;
5817 if ((doc != NULL) &&
5818 (doc->type == XML_HTML_DOCUMENT_NODE)) {
5819#ifdef LIBXML_HTML_ENABLED
5820 htmlNodeDump(buf, doc, cur);
5821#else
5822 xmlGenericError(xmlGenericErrorContext,
5823 "HTML support not compiled in\n");
5824#endif /* LIBXML_HTML_ENABLED */
5825 } else
5826 xmlNodeDump(buf, doc, cur, 0, 1);
5827 xmlBufferDump(f, buf);
5828 xmlBufferFree(buf);
5829}
5830
5831/************************************************************************
5832 * *
5833 * Dumping XML tree content to an I/O output buffer *
5834 * *
5835 ************************************************************************/
5836
5837void
5838xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
5839 int level, int format, const char *encoding);
5840static void
5841xmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
5842 int level, int format, const char *encoding);
5843/**
5844 * xmlNsDumpOutput:
5845 * @buf: the XML buffer output
5846 * @cur: a namespace
5847 *
5848 * Dump a local Namespace definition.
5849 * Should be called in the context of attributes dumps.
5850 */
5851static void
5852xmlNsDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) {
5853 if (cur == NULL) {
5854#ifdef DEBUG_TREE
5855 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00005856 "xmlNsDumpOutput : Ns == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005857#endif
5858 return;
5859 }
5860 if ((cur->type == XML_LOCAL_NAMESPACE) && (cur->href != NULL)) {
5861 /* Within the context of an element attributes */
5862 if (cur->prefix != NULL) {
5863 xmlOutputBufferWriteString(buf, " xmlns:");
5864 xmlOutputBufferWriteString(buf, (const char *)cur->prefix);
5865 } else
5866 xmlOutputBufferWriteString(buf, " xmlns");
5867 xmlOutputBufferWriteString(buf, "=");
5868 xmlBufferWriteQuotedString(buf->buffer, cur->href);
5869 }
5870}
5871
5872/**
5873 * xmlNsListDumpOutput:
5874 * @buf: the XML buffer output
5875 * @cur: the first namespace
5876 *
5877 * Dump a list of local Namespace definitions.
5878 * Should be called in the context of attributes dumps.
5879 */
5880static void
5881xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) {
5882 while (cur != NULL) {
5883 xmlNsDumpOutput(buf, cur);
5884 cur = cur->next;
5885 }
5886}
5887
5888/**
5889 * xmlDtdDumpOutput:
5890 * @buf: the XML buffer output
5891 * @doc: the document
5892 * @encoding: an optional encoding string
5893 *
5894 * Dump the XML document DTD, if any.
5895 */
5896static void
5897xmlDtdDumpOutput(xmlOutputBufferPtr buf, xmlDtdPtr dtd, const char *encoding) {
5898 if (dtd == NULL) {
5899#ifdef DEBUG_TREE
5900 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00005901 "xmlDtdDumpOutput : no internal subset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005902#endif
5903 return;
5904 }
5905 xmlOutputBufferWriteString(buf, "<!DOCTYPE ");
5906 xmlOutputBufferWriteString(buf, (const char *)dtd->name);
5907 if (dtd->ExternalID != NULL) {
5908 xmlOutputBufferWriteString(buf, " PUBLIC ");
5909 xmlBufferWriteQuotedString(buf->buffer, dtd->ExternalID);
5910 xmlOutputBufferWriteString(buf, " ");
5911 xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID);
5912 } else if (dtd->SystemID != NULL) {
5913 xmlOutputBufferWriteString(buf, " SYSTEM ");
5914 xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID);
5915 }
5916 if ((dtd->entities == NULL) && (dtd->elements == NULL) &&
5917 (dtd->attributes == NULL) && (dtd->notations == NULL)) {
5918 xmlOutputBufferWriteString(buf, ">");
5919 return;
5920 }
5921 xmlOutputBufferWriteString(buf, " [\n");
5922 xmlNodeListDumpOutput(buf, dtd->doc, dtd->children, -1, 0, encoding);
5923 xmlOutputBufferWriteString(buf, "]>");
5924}
5925
5926/**
5927 * xmlAttrDumpOutput:
5928 * @buf: the XML buffer output
5929 * @doc: the document
5930 * @cur: the attribute pointer
5931 * @encoding: an optional encoding string
5932 *
5933 * Dump an XML attribute
5934 */
5935static void
5936xmlAttrDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00005937 const char *encoding ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00005938 xmlChar *value;
5939
5940 if (cur == NULL) {
5941#ifdef DEBUG_TREE
5942 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00005943 "xmlAttrDumpOutput : property == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005944#endif
5945 return;
5946 }
5947 xmlOutputBufferWriteString(buf, " ");
5948 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
5949 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
5950 xmlOutputBufferWriteString(buf, ":");
5951 }
5952 xmlOutputBufferWriteString(buf, (const char *)cur->name);
5953 value = xmlNodeListGetString(doc, cur->children, 0);
5954 if (value) {
5955 xmlOutputBufferWriteString(buf, "=");
5956 xmlBufferWriteQuotedString(buf->buffer, value);
5957 xmlFree(value);
5958 } else {
5959 xmlOutputBufferWriteString(buf, "=\"\"");
5960 }
5961}
5962
5963/**
5964 * xmlAttrListDumpOutput:
5965 * @buf: the XML buffer output
5966 * @doc: the document
5967 * @cur: the first attribute pointer
5968 * @encoding: an optional encoding string
5969 *
5970 * Dump a list of XML attributes
5971 */
5972static void
5973xmlAttrListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
5974 xmlAttrPtr cur, const char *encoding) {
5975 if (cur == NULL) {
5976#ifdef DEBUG_TREE
5977 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00005978 "xmlAttrListDumpOutput : property == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005979#endif
5980 return;
5981 }
5982 while (cur != NULL) {
5983 xmlAttrDumpOutput(buf, doc, cur, encoding);
5984 cur = cur->next;
5985 }
5986}
5987
5988
5989
5990/**
5991 * xmlNodeListDumpOutput:
5992 * @buf: the XML buffer output
5993 * @doc: the document
5994 * @cur: the first node
5995 * @level: the imbrication level for indenting
5996 * @format: is formatting allowed
5997 * @encoding: an optional encoding string
5998 *
Daniel Veillardd1640922001-12-17 15:30:10 +00005999 * Dump an XML node list, recursive behaviour, children are printed too.
Owen Taylor3473f882001-02-23 17:55:21 +00006000 */
6001static void
6002xmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
6003 xmlNodePtr cur, int level, int format, const char *encoding) {
6004 int i;
6005
6006 if (cur == NULL) {
6007#ifdef DEBUG_TREE
6008 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006009 "xmlNodeListDumpOutput : node == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006010#endif
6011 return;
6012 }
6013 while (cur != NULL) {
6014 if ((format) && (xmlIndentTreeOutput) &&
6015 (cur->type == XML_ELEMENT_NODE))
6016 for (i = 0;i < level;i++)
6017 xmlOutputBufferWriteString(buf, " ");
6018 xmlNodeDumpOutput(buf, doc, cur, level, format, encoding);
6019 if (format) {
6020 xmlOutputBufferWriteString(buf, "\n");
6021 }
6022 cur = cur->next;
6023 }
6024}
6025
6026/**
6027 * xmlNodeDumpOutput:
6028 * @buf: the XML buffer output
6029 * @doc: the document
6030 * @cur: the current node
6031 * @level: the imbrication level for indenting
6032 * @format: is formatting allowed
6033 * @encoding: an optional encoding string
6034 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006035 * Dump an XML node, recursive behaviour, children are printed too.
Owen Taylor3473f882001-02-23 17:55:21 +00006036 */
6037void
6038xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
6039 int level, int format, const char *encoding) {
6040 int i;
6041 xmlNodePtr tmp;
6042
6043 if (cur == NULL) {
6044#ifdef DEBUG_TREE
6045 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006046 "xmlNodeDumpOutput : node == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006047#endif
6048 return;
6049 }
6050 if (cur->type == XML_XINCLUDE_START)
6051 return;
6052 if (cur->type == XML_XINCLUDE_END)
6053 return;
6054 if (cur->type == XML_DTD_NODE) {
6055 xmlDtdDumpOutput(buf, (xmlDtdPtr) cur, encoding);
6056 return;
6057 }
6058 if (cur->type == XML_ELEMENT_DECL) {
6059 xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur);
6060 return;
6061 }
6062 if (cur->type == XML_ATTRIBUTE_DECL) {
6063 xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur);
6064 return;
6065 }
6066 if (cur->type == XML_ENTITY_DECL) {
6067 xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur);
6068 return;
6069 }
6070 if (cur->type == XML_TEXT_NODE) {
6071 if (cur->content != NULL) {
6072 if ((cur->name == xmlStringText) ||
6073 (cur->name != xmlStringTextNoenc)) {
6074 xmlChar *buffer;
6075
6076#ifndef XML_USE_BUFFER_CONTENT
6077 if (encoding == NULL)
6078 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
6079 else
6080 buffer = xmlEncodeSpecialChars(doc, cur->content);
6081#else
6082 if (encoding == NULL)
6083 buffer = xmlEncodeEntitiesReentrant(doc,
6084 xmlBufferContent(cur->content));
6085 else
6086 buffer = xmlEncodeSpecialChars(doc,
6087 xmlBufferContent(cur->content));
6088#endif
6089 if (buffer != NULL) {
6090 xmlOutputBufferWriteString(buf, (const char *)buffer);
6091 xmlFree(buffer);
6092 }
6093 } else {
6094 /*
6095 * Disable escaping, needed for XSLT
6096 */
6097#ifndef XML_USE_BUFFER_CONTENT
6098 xmlOutputBufferWriteString(buf, (const char *) cur->content);
6099#else
6100 xmlOutputBufferWriteString(buf, xmlBufferContent(cur->content));
6101#endif
6102 }
6103 }
6104
6105 return;
6106 }
6107 if (cur->type == XML_PI_NODE) {
6108 if (cur->content != NULL) {
6109 xmlOutputBufferWriteString(buf, "<?");
6110 xmlOutputBufferWriteString(buf, (const char *)cur->name);
6111 if (cur->content != NULL) {
6112 xmlOutputBufferWriteString(buf, " ");
6113#ifndef XML_USE_BUFFER_CONTENT
6114 xmlOutputBufferWriteString(buf, (const char *)cur->content);
6115#else
6116 xmlOutputBufferWriteString(buf, (const char *)xmlBufferContent(cur->content));
6117#endif
6118 }
6119 xmlOutputBufferWriteString(buf, "?>");
6120 } else {
6121 xmlOutputBufferWriteString(buf, "<?");
6122 xmlOutputBufferWriteString(buf, (const char *)cur->name);
6123 xmlOutputBufferWriteString(buf, "?>");
6124 }
6125 return;
6126 }
6127 if (cur->type == XML_COMMENT_NODE) {
6128 if (cur->content != NULL) {
6129 xmlOutputBufferWriteString(buf, "<!--");
6130#ifndef XML_USE_BUFFER_CONTENT
6131 xmlOutputBufferWriteString(buf, (const char *)cur->content);
6132#else
6133 xmlOutputBufferWriteString(buf, (const char *)xmlBufferContent(cur->content));
6134#endif
6135 xmlOutputBufferWriteString(buf, "-->");
6136 }
6137 return;
6138 }
6139 if (cur->type == XML_ENTITY_REF_NODE) {
6140 xmlOutputBufferWriteString(buf, "&");
6141 xmlOutputBufferWriteString(buf, (const char *)cur->name);
6142 xmlOutputBufferWriteString(buf, ";");
6143 return;
6144 }
6145 if (cur->type == XML_CDATA_SECTION_NODE) {
6146 xmlOutputBufferWriteString(buf, "<![CDATA[");
6147 if (cur->content != NULL)
6148#ifndef XML_USE_BUFFER_CONTENT
6149 xmlOutputBufferWriteString(buf, (const char *)cur->content);
6150#else
6151 xmlOutputBufferWriteString(buf, (const char *)xmlBufferContent(cur->content));
6152#endif
6153 xmlOutputBufferWriteString(buf, "]]>");
6154 return;
6155 }
6156
6157 if (format == 1) {
6158 tmp = cur->children;
6159 while (tmp != NULL) {
6160 if ((tmp->type == XML_TEXT_NODE) ||
6161 (tmp->type == XML_ENTITY_REF_NODE)) {
6162 format = 0;
6163 break;
6164 }
6165 tmp = tmp->next;
6166 }
6167 }
6168 xmlOutputBufferWriteString(buf, "<");
6169 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
6170 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
6171 xmlOutputBufferWriteString(buf, ":");
6172 }
6173
6174 xmlOutputBufferWriteString(buf, (const char *)cur->name);
6175 if (cur->nsDef)
6176 xmlNsListDumpOutput(buf, cur->nsDef);
6177 if (cur->properties != NULL)
6178 xmlAttrListDumpOutput(buf, doc, cur->properties, encoding);
6179
Daniel Veillard7db37732001-07-12 01:20:08 +00006180 if (((cur->type == XML_ELEMENT_NODE) || (cur->content == NULL)) &&
6181 (cur->children == NULL) && (!xmlSaveNoEmptyTags)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006182 xmlOutputBufferWriteString(buf, "/>");
6183 return;
6184 }
6185 xmlOutputBufferWriteString(buf, ">");
Daniel Veillard7db37732001-07-12 01:20:08 +00006186 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006187 xmlChar *buffer;
6188
6189#ifndef XML_USE_BUFFER_CONTENT
6190 if (encoding == NULL)
6191 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
6192 else
6193 buffer = xmlEncodeSpecialChars(doc, cur->content);
6194#else
6195 if (encoding == NULL)
6196 buffer = xmlEncodeEntitiesReentrant(doc,
6197 xmlBufferContent(cur->content));
6198 else
6199 buffer = xmlEncodeSpecialChars(doc,
6200 xmlBufferContent(cur->content));
6201#endif
6202 if (buffer != NULL) {
6203 xmlOutputBufferWriteString(buf, (const char *)buffer);
6204 xmlFree(buffer);
6205 }
6206 }
6207 if (cur->children != NULL) {
6208 if (format) xmlOutputBufferWriteString(buf, "\n");
6209 xmlNodeListDumpOutput(buf, doc, cur->children,
6210 (level >= 0?level+1:-1), format, encoding);
6211 if ((xmlIndentTreeOutput) && (format))
6212 for (i = 0;i < level;i++)
6213 xmlOutputBufferWriteString(buf, " ");
6214 }
6215 xmlOutputBufferWriteString(buf, "</");
6216 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
6217 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
6218 xmlOutputBufferWriteString(buf, ":");
6219 }
6220
6221 xmlOutputBufferWriteString(buf, (const char *)cur->name);
6222 xmlOutputBufferWriteString(buf, ">");
6223}
6224
6225/**
6226 * xmlDocContentDumpOutput:
6227 * @buf: the XML buffer output
6228 * @cur: the document
6229 * @encoding: an optional encoding string
6230 * @format: should formatting spaces been added
6231 *
6232 * Dump an XML document.
6233 */
6234static void
6235xmlDocContentDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr cur,
6236 const char *encoding, int format) {
6237 xmlOutputBufferWriteString(buf, "<?xml version=");
6238 if (cur->version != NULL)
6239 xmlBufferWriteQuotedString(buf->buffer, cur->version);
6240 else
6241 xmlOutputBufferWriteString(buf, "\"1.0\"");
6242 if (encoding == NULL) {
6243 if (cur->encoding != NULL)
6244 encoding = (const char *) cur->encoding;
6245 else if (cur->charset != XML_CHAR_ENCODING_UTF8)
6246 encoding = xmlGetCharEncodingName((xmlCharEncoding) cur->charset);
6247 }
6248 if (encoding != NULL) {
6249 xmlOutputBufferWriteString(buf, " encoding=");
6250 xmlBufferWriteQuotedString(buf->buffer, (xmlChar *) encoding);
6251 }
6252 switch (cur->standalone) {
6253 case 0:
6254 xmlOutputBufferWriteString(buf, " standalone=\"no\"");
6255 break;
6256 case 1:
6257 xmlOutputBufferWriteString(buf, " standalone=\"yes\"");
6258 break;
6259 }
6260 xmlOutputBufferWriteString(buf, "?>\n");
6261 if (cur->children != NULL) {
6262 xmlNodePtr child = cur->children;
6263
6264 while (child != NULL) {
6265 xmlNodeDumpOutput(buf, cur, child, 0, format, encoding);
6266 xmlOutputBufferWriteString(buf, "\n");
6267 child = child->next;
6268 }
6269 }
6270}
6271
6272/************************************************************************
6273 * *
6274 * Saving functions front-ends *
6275 * *
6276 ************************************************************************/
6277
6278/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +00006279 * xmlDocDumpFormatMemoryEnc:
Owen Taylor3473f882001-02-23 17:55:21 +00006280 * @out_doc: Document to generate XML text from
6281 * @doc_txt_ptr: Memory pointer for allocated XML text
6282 * @doc_txt_len: Length of the generated XML text
6283 * @txt_encoding: Character encoding to use when generating XML text
6284 * @format: should formatting spaces been added
6285 *
6286 * Dump the current DOM tree into memory using the character encoding specified
6287 * by the caller. Note it is up to the caller of this function to free the
6288 * allocated memory.
6289 */
6290
6291void
6292xmlDocDumpFormatMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00006293 int * doc_txt_len, const char * txt_encoding,
Daniel Veillard1731d6a2001-04-10 16:38:06 +00006294 int format) {
Owen Taylor3473f882001-02-23 17:55:21 +00006295 int dummy = 0;
6296
6297 xmlCharEncoding doc_charset;
6298 xmlOutputBufferPtr out_buff = NULL;
6299 xmlCharEncodingHandlerPtr conv_hdlr = NULL;
6300
6301 if (doc_txt_len == NULL) {
6302 doc_txt_len = &dummy; /* Continue, caller just won't get length */
6303 }
6304
6305 if (doc_txt_ptr == NULL) {
6306 *doc_txt_len = 0;
6307 xmlGenericError(xmlGenericErrorContext,
6308 "xmlDocDumpFormatMemoryEnc: Null return buffer pointer.");
6309 return;
6310 }
6311
6312 *doc_txt_ptr = NULL;
6313 *doc_txt_len = 0;
6314
6315 if (out_doc == NULL) {
6316 /* No document, no output */
6317 xmlGenericError(xmlGenericErrorContext,
6318 "xmlDocDumpFormatMemoryEnc: Null DOM tree document pointer.\n");
6319 return;
6320 }
6321
6322 /*
6323 * Validate the encoding value, if provided.
6324 * This logic is copied from xmlSaveFileEnc.
6325 */
6326
6327 if (txt_encoding == NULL)
6328 txt_encoding = (const char *) out_doc->encoding;
6329 if (txt_encoding != NULL) {
6330 doc_charset = xmlParseCharEncoding(txt_encoding);
6331
6332 if (out_doc->charset != XML_CHAR_ENCODING_UTF8) {
6333 xmlGenericError(xmlGenericErrorContext,
6334 "xmlDocDumpFormatMemoryEnc: Source document not in UTF8\n");
6335 return;
6336
6337 } else if (doc_charset != XML_CHAR_ENCODING_UTF8) {
6338 conv_hdlr = xmlFindCharEncodingHandler(txt_encoding);
6339 if ( conv_hdlr == NULL ) {
6340 xmlGenericError(xmlGenericErrorContext,
6341 "%s: %s %s '%s'\n",
6342 "xmlDocDumpFormatMemoryEnc",
6343 "Failed to identify encoding handler for",
6344 "character set",
6345 txt_encoding);
6346 return;
6347 }
6348 }
6349 }
6350
6351 if ((out_buff = xmlAllocOutputBuffer(conv_hdlr)) == NULL ) {
6352 xmlGenericError(xmlGenericErrorContext,
6353 "xmlDocDumpFormatMemoryEnc: Failed to allocate output buffer.\n");
6354 return;
6355 }
6356
Daniel Veillard1731d6a2001-04-10 16:38:06 +00006357 xmlDocContentDumpOutput(out_buff, out_doc, txt_encoding, format);
Owen Taylor3473f882001-02-23 17:55:21 +00006358 xmlOutputBufferFlush(out_buff);
6359 if (out_buff->conv != NULL) {
6360 *doc_txt_len = out_buff->conv->use;
6361 *doc_txt_ptr = xmlStrndup(out_buff->conv->content, *doc_txt_len);
6362 } else {
6363 *doc_txt_len = out_buff->buffer->use;
6364 *doc_txt_ptr = xmlStrndup(out_buff->buffer->content, *doc_txt_len);
6365 }
6366 (void)xmlOutputBufferClose(out_buff);
6367
6368 if ((*doc_txt_ptr == NULL) && (*doc_txt_len > 0)) {
6369 *doc_txt_len = 0;
6370 xmlGenericError(xmlGenericErrorContext,
6371 "xmlDocDumpFormatMemoryEnc: %s\n",
6372 "Failed to allocate memory for document text representation.");
6373 }
6374
6375 return;
6376}
6377
6378/**
6379 * xmlDocDumpMemory:
6380 * @cur: the document
6381 * @mem: OUT: the memory pointer
Daniel Veillard60087f32001-10-10 09:45:09 +00006382 * @size: OUT: the memory length
Owen Taylor3473f882001-02-23 17:55:21 +00006383 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006384 * Dump an XML document in memory and return the #xmlChar * and it's size.
Owen Taylor3473f882001-02-23 17:55:21 +00006385 * It's up to the caller to free the memory.
6386 */
6387void
6388xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) {
6389 xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, 0);
6390}
6391
6392/**
6393 * xmlDocDumpFormatMemory:
6394 * @cur: the document
6395 * @mem: OUT: the memory pointer
Daniel Veillard60087f32001-10-10 09:45:09 +00006396 * @size: OUT: the memory length
Owen Taylor3473f882001-02-23 17:55:21 +00006397 * @format: should formatting spaces been added
6398 *
6399 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006400 * Dump an XML document in memory and return the #xmlChar * and it's size.
Owen Taylor3473f882001-02-23 17:55:21 +00006401 * It's up to the caller to free the memory.
6402 */
6403void
6404xmlDocDumpFormatMemory(xmlDocPtr cur, xmlChar**mem, int *size, int format) {
6405 xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, format);
6406}
6407
6408/**
6409 * xmlDocDumpMemoryEnc:
6410 * @out_doc: Document to generate XML text from
6411 * @doc_txt_ptr: Memory pointer for allocated XML text
6412 * @doc_txt_len: Length of the generated XML text
6413 * @txt_encoding: Character encoding to use when generating XML text
6414 *
6415 * Dump the current DOM tree into memory using the character encoding specified
6416 * by the caller. Note it is up to the caller of this function to free the
6417 * allocated memory.
6418 */
6419
6420void
6421xmlDocDumpMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
6422 int * doc_txt_len, const char * txt_encoding) {
6423 xmlDocDumpFormatMemoryEnc(out_doc, doc_txt_ptr, doc_txt_len,
Daniel Veillard1731d6a2001-04-10 16:38:06 +00006424 txt_encoding, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00006425}
6426
6427/**
6428 * xmlGetDocCompressMode:
6429 * @doc: the document
6430 *
6431 * get the compression ratio for a document, ZLIB based
6432 * Returns 0 (uncompressed) to 9 (max compression)
6433 */
6434int
6435xmlGetDocCompressMode (xmlDocPtr doc) {
6436 if (doc == NULL) return(-1);
6437 return(doc->compression);
6438}
6439
6440/**
6441 * xmlSetDocCompressMode:
6442 * @doc: the document
6443 * @mode: the compression ratio
6444 *
6445 * set the compression ratio for a document, ZLIB based
6446 * Correct values: 0 (uncompressed) to 9 (max compression)
6447 */
6448void
6449xmlSetDocCompressMode (xmlDocPtr doc, int mode) {
6450 if (doc == NULL) return;
6451 if (mode < 0) doc->compression = 0;
6452 else if (mode > 9) doc->compression = 9;
6453 else doc->compression = mode;
6454}
6455
6456/**
6457 * xmlGetCompressMode:
6458 *
6459 * get the default compression mode used, ZLIB based.
6460 * Returns 0 (uncompressed) to 9 (max compression)
6461 */
6462int
6463 xmlGetCompressMode(void) {
6464 return(xmlCompressMode);
6465}
6466
6467/**
6468 * xmlSetCompressMode:
6469 * @mode: the compression ratio
6470 *
6471 * set the default compression mode used, ZLIB based
6472 * Correct values: 0 (uncompressed) to 9 (max compression)
6473 */
6474void
6475xmlSetCompressMode(int mode) {
6476 if (mode < 0) xmlCompressMode = 0;
6477 else if (mode > 9) xmlCompressMode = 9;
6478 else xmlCompressMode = mode;
6479}
6480
6481/**
6482 * xmlDocDump:
6483 * @f: the FILE*
6484 * @cur: the document
6485 *
6486 * Dump an XML document to an open FILE.
6487 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006488 * returns: the number of bytes written or -1 in case of failure.
Owen Taylor3473f882001-02-23 17:55:21 +00006489 */
6490int
6491xmlDocDump(FILE *f, xmlDocPtr cur) {
6492 xmlOutputBufferPtr buf;
6493 const char * encoding;
6494 xmlCharEncodingHandlerPtr handler = NULL;
6495 int ret;
6496
6497 if (cur == NULL) {
6498#ifdef DEBUG_TREE
6499 xmlGenericError(xmlGenericErrorContext,
6500 "xmlDocDump : document == NULL\n");
6501#endif
6502 return(-1);
6503 }
6504 encoding = (const char *) cur->encoding;
6505
6506 if (encoding != NULL) {
6507 xmlCharEncoding enc;
6508
6509 enc = xmlParseCharEncoding(encoding);
6510
6511 if (cur->charset != XML_CHAR_ENCODING_UTF8) {
6512 xmlGenericError(xmlGenericErrorContext,
6513 "xmlDocDump: document not in UTF8\n");
6514 return(-1);
6515 }
6516 if (enc != XML_CHAR_ENCODING_UTF8) {
6517 handler = xmlFindCharEncodingHandler(encoding);
6518 if (handler == NULL) {
6519 xmlFree((char *) cur->encoding);
6520 cur->encoding = NULL;
6521 }
6522 }
6523 }
6524 buf = xmlOutputBufferCreateFile(f, handler);
6525 if (buf == NULL) return(-1);
Daniel Veillard1731d6a2001-04-10 16:38:06 +00006526 xmlDocContentDumpOutput(buf, cur, NULL, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00006527
6528 ret = xmlOutputBufferClose(buf);
6529 return(ret);
6530}
6531
6532/**
6533 * xmlSaveFileTo:
6534 * @buf: an output I/O buffer
6535 * @cur: the document
Daniel Veillardd1640922001-12-17 15:30:10 +00006536 * @encoding: the encoding if any assuming the I/O layer handles the trancoding
Owen Taylor3473f882001-02-23 17:55:21 +00006537 *
6538 * Dump an XML document to an I/O buffer.
6539 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006540 * returns: the number of bytes written or -1 in case of failure.
Owen Taylor3473f882001-02-23 17:55:21 +00006541 */
6542int
CET 2001 Daniel Veillard5a37bde2001-11-01 14:31:22 +00006543xmlSaveFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding) {
Owen Taylor3473f882001-02-23 17:55:21 +00006544 int ret;
6545
6546 if (buf == NULL) return(0);
Daniel Veillard1731d6a2001-04-10 16:38:06 +00006547 xmlDocContentDumpOutput(buf, cur, encoding, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00006548 ret = xmlOutputBufferClose(buf);
6549 return(ret);
6550}
6551
6552/**
Daniel Veillardeefd4492001-04-28 16:55:50 +00006553 * xmlSaveFormatFileTo:
6554 * @buf: an output I/O buffer
6555 * @cur: the document
Daniel Veillardd1640922001-12-17 15:30:10 +00006556 * @encoding: the encoding if any assuming the I/O layer handles the trancoding
Daniel Veillardeefd4492001-04-28 16:55:50 +00006557 * @format: should formatting spaces been added
6558 *
6559 * Dump an XML document to an I/O buffer.
6560 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006561 * returns: the number of bytes written or -1 in case of failure.
Daniel Veillardeefd4492001-04-28 16:55:50 +00006562 */
6563int
CET 2001 Daniel Veillard5a37bde2001-11-01 14:31:22 +00006564xmlSaveFormatFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding, int format) {
Daniel Veillardeefd4492001-04-28 16:55:50 +00006565 int ret;
6566
6567 if (buf == NULL) return(0);
6568 xmlDocContentDumpOutput(buf, cur, encoding, format);
6569 ret = xmlOutputBufferClose(buf);
6570 return(ret);
6571}
6572
6573/**
Daniel Veillardf012a642001-07-23 19:10:52 +00006574 * xmlSaveFormatFileEnc
6575 * @filename: the filename or URL to output
6576 * @cur: the document being saved
6577 * @encoding: the name of the encoding to use or NULL.
6578 * @format: should formatting spaces be added.
Daniel Veillardd1640922001-12-17 15:30:10 +00006579 *
6580 * Returns the number of bytes written or -1 in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00006581 */
6582int
Daniel Veillardf012a642001-07-23 19:10:52 +00006583xmlSaveFormatFileEnc( const char * filename, xmlDocPtr cur,
6584 const char * encoding, int format ) {
Owen Taylor3473f882001-02-23 17:55:21 +00006585 xmlOutputBufferPtr buf;
6586 xmlCharEncodingHandlerPtr handler = NULL;
Daniel Veillard81418e32001-05-22 15:08:55 +00006587 xmlCharEncoding enc;
Owen Taylor3473f882001-02-23 17:55:21 +00006588 int ret;
6589
6590 if (encoding != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00006591
6592 enc = xmlParseCharEncoding(encoding);
6593 if (cur->charset != XML_CHAR_ENCODING_UTF8) {
6594 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006595 "xmlSaveFormatFileEnc: document not in UTF8\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006596 return(-1);
6597 }
6598 if (enc != XML_CHAR_ENCODING_UTF8) {
6599 handler = xmlFindCharEncodingHandler(encoding);
Daniel Veillard81418e32001-05-22 15:08:55 +00006600 if (handler == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00006601 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006602 }
6603 }
6604
Daniel Veillardf012a642001-07-23 19:10:52 +00006605#ifdef HAVE_ZLIB_H
6606 if (cur->compression < 0) cur->compression = xmlCompressMode;
6607#endif
Owen Taylor3473f882001-02-23 17:55:21 +00006608 /*
6609 * save the content to a temp buffer.
6610 */
Daniel Veillardf012a642001-07-23 19:10:52 +00006611 buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression);
Owen Taylor3473f882001-02-23 17:55:21 +00006612 if (buf == NULL) return(-1);
6613
Daniel Veillardf012a642001-07-23 19:10:52 +00006614 xmlDocContentDumpOutput(buf, cur, encoding, format);
Owen Taylor3473f882001-02-23 17:55:21 +00006615
6616 ret = xmlOutputBufferClose(buf);
6617 return(ret);
6618}
6619
Daniel Veillardf012a642001-07-23 19:10:52 +00006620
6621/**
6622 * xmlSaveFileEnc:
6623 * @filename: the filename (or URL)
6624 * @cur: the document
6625 * @encoding: the name of an encoding (or NULL)
6626 *
6627 * Dump an XML document, converting it to the given encoding
6628 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006629 * returns: the number of bytes written or -1 in case of failure.
Daniel Veillardf012a642001-07-23 19:10:52 +00006630 */
6631int
6632xmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) {
6633 return ( xmlSaveFormatFileEnc( filename, cur, encoding, 0 ) );
6634}
6635
Owen Taylor3473f882001-02-23 17:55:21 +00006636/**
Daniel Veillard67fee942001-04-26 18:59:03 +00006637 * xmlSaveFormatFile:
Owen Taylor3473f882001-02-23 17:55:21 +00006638 * @filename: the filename (or URL)
6639 * @cur: the document
Daniel Veillard67fee942001-04-26 18:59:03 +00006640 * @format: should formatting spaces been added
Owen Taylor3473f882001-02-23 17:55:21 +00006641 *
6642 * Dump an XML document to a file. Will use compression if
6643 * compiled in and enabled. If @filename is "-" the stdout file is
Daniel Veillardd1640922001-12-17 15:30:10 +00006644 * used. If @format is set then the document will be indented on output.
Daniel Veillard67fee942001-04-26 18:59:03 +00006645 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006646 * returns: the number of bytes written or -1 in case of failure.
Owen Taylor3473f882001-02-23 17:55:21 +00006647 */
6648int
Daniel Veillard67fee942001-04-26 18:59:03 +00006649xmlSaveFormatFile(const char *filename, xmlDocPtr cur, int format) {
Daniel Veillardf012a642001-07-23 19:10:52 +00006650 return ( xmlSaveFormatFileEnc( filename, cur, NULL, format ) );
Owen Taylor3473f882001-02-23 17:55:21 +00006651}
6652
Daniel Veillard67fee942001-04-26 18:59:03 +00006653/**
6654 * xmlSaveFile:
6655 * @filename: the filename (or URL)
6656 * @cur: the document
6657 *
6658 * Dump an XML document to a file. Will use compression if
6659 * compiled in and enabled. If @filename is "-" the stdout file is
6660 * used.
Daniel Veillardd1640922001-12-17 15:30:10 +00006661 * returns: the number of bytes written or -1 in case of failure.
Daniel Veillard67fee942001-04-26 18:59:03 +00006662 */
6663int
6664xmlSaveFile(const char *filename, xmlDocPtr cur) {
Daniel Veillardf012a642001-07-23 19:10:52 +00006665 return(xmlSaveFormatFileEnc(filename, cur, NULL, 0));
Daniel Veillard67fee942001-04-26 18:59:03 +00006666}
6667