blob: ab8532b08a4bbe29075e30fe5f41093916f5fd29 [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)) {
Daniel Veillard8107a222002-01-13 14:10:10 +00002645 xmlNsPtr ns;
2646 if (target->doc)
Owen Taylor3473f882001-02-23 17:55:21 +00002647 ns = xmlSearchNs(target->doc, target, cur->ns->prefix);
Daniel Veillard8107a222002-01-13 14:10:10 +00002648 else if (cur->doc) /* target may not yet have a doc : KPI */
2649 ns = xmlSearchNs(cur->doc, target, cur->ns->prefix);
2650 else
2651 ns = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002652 ret->ns = ns;
2653 } else
2654 ret->ns = NULL;
2655
2656 if (cur->children != NULL) {
2657 xmlNodePtr tmp;
2658
2659 ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret);
2660 ret->last = NULL;
2661 tmp = ret->children;
2662 while (tmp != NULL) {
2663 /* tmp->parent = (xmlNodePtr)ret; */
2664 if (tmp->next == NULL)
2665 ret->last = tmp;
2666 tmp = tmp->next;
2667 }
2668 }
2669 return(ret);
2670}
2671
2672/**
2673 * xmlCopyPropList:
2674 * @target: the element where the attributes will be grafted
2675 * @cur: the first attribute
2676 *
2677 * Do a copy of an attribute list.
2678 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002679 * Returns: a new #xmlAttrPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002680 */
2681xmlAttrPtr
2682xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) {
2683 xmlAttrPtr ret = NULL;
2684 xmlAttrPtr p = NULL,q;
2685
2686 while (cur != NULL) {
2687 q = xmlCopyProp(target, cur);
2688 if (p == NULL) {
2689 ret = p = q;
2690 } else {
2691 p->next = q;
2692 q->prev = p;
2693 p = q;
2694 }
2695 cur = cur->next;
2696 }
2697 return(ret);
2698}
2699
2700/*
Daniel Veillardd1640922001-12-17 15:30:10 +00002701 * NOTE about the CopyNode operations !
Owen Taylor3473f882001-02-23 17:55:21 +00002702 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002703 * They are split into external and internal parts for one
Owen Taylor3473f882001-02-23 17:55:21 +00002704 * tricky reason: namespaces. Doing a direct copy of a node
2705 * say RPM:Copyright without changing the namespace pointer to
2706 * something else can produce stale links. One way to do it is
2707 * to keep a reference counter but this doesn't work as soon
2708 * as one move the element or the subtree out of the scope of
2709 * the existing namespace. The actual solution seems to add
2710 * a copy of the namespace at the top of the copied tree if
2711 * not available in the subtree.
2712 * Hence two functions, the public front-end call the inner ones
2713 */
2714
2715static xmlNodePtr
2716xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent);
2717
2718static xmlNodePtr
Daniel Veillard3ec4c612001-08-28 20:39:49 +00002719xmlStaticCopyNode(const xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent,
Owen Taylor3473f882001-02-23 17:55:21 +00002720 int recursive) {
2721 xmlNodePtr ret;
2722
2723 if (node == NULL) return(NULL);
Daniel Veillard39196eb2001-06-19 18:09:42 +00002724 switch (node->type) {
2725 case XML_TEXT_NODE:
2726 case XML_CDATA_SECTION_NODE:
2727 case XML_ELEMENT_NODE:
2728 case XML_ENTITY_REF_NODE:
2729 case XML_ENTITY_NODE:
2730 case XML_PI_NODE:
2731 case XML_COMMENT_NODE:
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00002732 case XML_XINCLUDE_START:
2733 case XML_XINCLUDE_END:
2734 break;
2735 case XML_ATTRIBUTE_NODE:
2736 return((xmlNodePtr) xmlCopyProp(parent, (xmlAttrPtr) node));
2737 case XML_NAMESPACE_DECL:
2738 return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node));
2739
Daniel Veillard39196eb2001-06-19 18:09:42 +00002740 case XML_DOCUMENT_NODE:
2741 case XML_HTML_DOCUMENT_NODE:
2742#ifdef LIBXML_DOCB_ENABLED
2743 case XML_DOCB_DOCUMENT_NODE:
2744#endif
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00002745 return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, recursive));
Daniel Veillard39196eb2001-06-19 18:09:42 +00002746 case XML_DOCUMENT_TYPE_NODE:
2747 case XML_DOCUMENT_FRAG_NODE:
2748 case XML_NOTATION_NODE:
2749 case XML_DTD_NODE:
2750 case XML_ELEMENT_DECL:
2751 case XML_ATTRIBUTE_DECL:
2752 case XML_ENTITY_DECL:
2753 return(NULL);
2754 }
Daniel Veillardb33c2012001-04-25 12:59:04 +00002755
Owen Taylor3473f882001-02-23 17:55:21 +00002756 /*
2757 * Allocate a new node and fill the fields.
2758 */
2759 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2760 if (ret == NULL) {
2761 xmlGenericError(xmlGenericErrorContext,
2762 "xmlStaticCopyNode : malloc failed\n");
2763 return(NULL);
2764 }
2765 memset(ret, 0, sizeof(xmlNode));
2766 ret->type = node->type;
2767
2768 ret->doc = doc;
2769 ret->parent = parent;
2770 if (node->name == xmlStringText)
2771 ret->name = xmlStringText;
2772 else if (node->name == xmlStringTextNoenc)
2773 ret->name = xmlStringTextNoenc;
2774 else if (node->name == xmlStringComment)
2775 ret->name = xmlStringComment;
2776 else if (node->name != NULL)
2777 ret->name = xmlStrdup(node->name);
Daniel Veillard7db37732001-07-12 01:20:08 +00002778 if ((node->type != XML_ELEMENT_NODE) &&
2779 (node->content != NULL) &&
2780 (node->type != XML_ENTITY_REF_NODE) &&
2781 (node->type != XML_XINCLUDE_END) &&
2782 (node->type != XML_XINCLUDE_START)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002783#ifndef XML_USE_BUFFER_CONTENT
2784 ret->content = xmlStrdup(node->content);
2785#else
2786 ret->content = xmlBufferCreateSize(xmlBufferLength(node->content));
2787 xmlBufferSetAllocationScheme(ret->content,
2788 xmlGetBufferAllocationScheme());
2789 xmlBufferAdd(ret->content,
2790 xmlBufferContent(node->content),
2791 xmlBufferLength(node->content));
2792#endif
Daniel Veillard8107a222002-01-13 14:10:10 +00002793 }else{
2794 if (node->type == XML_ELEMENT_NODE)
2795 ret->content = (void*)(long) node->content;
Owen Taylor3473f882001-02-23 17:55:21 +00002796 }
Daniel Veillardacb2bda2002-01-13 16:15:43 +00002797 if (parent != NULL) {
2798 xmlNodePtr tmp;
2799
2800 tmp = xmlAddChild(parent, ret);
2801 /* node could have coalesced */
2802 if (tmp != ret)
2803 return(tmp);
2804 }
Owen Taylor3473f882001-02-23 17:55:21 +00002805
2806 if (!recursive) return(ret);
2807 if (node->nsDef != NULL)
2808 ret->nsDef = xmlCopyNamespaceList(node->nsDef);
2809
2810 if (node->ns != NULL) {
2811 xmlNsPtr ns;
2812
2813 ns = xmlSearchNs(doc, ret, node->ns->prefix);
2814 if (ns == NULL) {
2815 /*
2816 * Humm, we are copying an element whose namespace is defined
2817 * out of the new tree scope. Search it in the original tree
2818 * and add it at the top of the new tree
2819 */
2820 ns = xmlSearchNs(node->doc, node, node->ns->prefix);
2821 if (ns != NULL) {
2822 xmlNodePtr root = ret;
2823
2824 while (root->parent != NULL) root = root->parent;
Daniel Veillarde82a9922001-04-22 12:12:58 +00002825 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00002826 }
2827 } else {
2828 /*
2829 * reference the existing namespace definition in our own tree.
2830 */
2831 ret->ns = ns;
2832 }
2833 }
2834 if (node->properties != NULL)
2835 ret->properties = xmlCopyPropList(ret, node->properties);
Daniel Veillardb33c2012001-04-25 12:59:04 +00002836 if (node->type == XML_ENTITY_REF_NODE) {
2837 if ((doc == NULL) || (node->doc != doc)) {
2838 /*
2839 * The copied node will go into a separate document, so
Daniel Veillardd1640922001-12-17 15:30:10 +00002840 * to avoid dangling references to the ENTITY_DECL node
Daniel Veillardb33c2012001-04-25 12:59:04 +00002841 * we cannot keep the reference. Try to find it in the
2842 * target document.
2843 */
2844 ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name);
2845 } else {
2846 ret->children = node->children;
2847 }
Daniel Veillard0ec98632001-11-14 15:04:32 +00002848 ret->last = ret->children;
2849 } else if (node->children != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002850 ret->children = xmlStaticCopyNodeList(node->children, doc, ret);
Daniel Veillard0ec98632001-11-14 15:04:32 +00002851 UPDATE_LAST_CHILD_AND_PARENT(ret)
2852 }
Owen Taylor3473f882001-02-23 17:55:21 +00002853 return(ret);
2854}
2855
2856static xmlNodePtr
2857xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) {
2858 xmlNodePtr ret = NULL;
2859 xmlNodePtr p = NULL,q;
2860
2861 while (node != NULL) {
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00002862 if (node->type == XML_DTD_NODE ) {
Daniel Veillard4497e692001-06-09 14:19:02 +00002863 if (doc == NULL) {
2864 node = node->next;
2865 continue;
2866 }
Daniel Veillardb33c2012001-04-25 12:59:04 +00002867 if (doc->intSubset == NULL) {
2868 q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node );
2869 q->doc = doc;
2870 q->parent = parent;
2871 doc->intSubset = (xmlDtdPtr) q;
2872 } else {
2873 q = (xmlNodePtr) doc->intSubset;
2874 }
2875 } else
2876 q = xmlStaticCopyNode(node, doc, parent, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00002877 if (ret == NULL) {
2878 q->prev = NULL;
2879 ret = p = q;
Daniel Veillardacb2bda2002-01-13 16:15:43 +00002880 } else if (p != q) {
2881 /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */
Owen Taylor3473f882001-02-23 17:55:21 +00002882 p->next = q;
2883 q->prev = p;
2884 p = q;
2885 }
2886 node = node->next;
2887 }
2888 return(ret);
2889}
2890
2891/**
2892 * xmlCopyNode:
2893 * @node: the node
2894 * @recursive: if 1 do a recursive copy.
2895 *
2896 * Do a copy of the node.
2897 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002898 * Returns: a new #xmlNodePtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002899 */
2900xmlNodePtr
Daniel Veillard3ec4c612001-08-28 20:39:49 +00002901xmlCopyNode(const xmlNodePtr node, int recursive) {
Owen Taylor3473f882001-02-23 17:55:21 +00002902 xmlNodePtr ret;
2903
2904 ret = xmlStaticCopyNode(node, NULL, NULL, recursive);
2905 return(ret);
2906}
2907
2908/**
Daniel Veillard82daa812001-04-12 08:55:36 +00002909 * xmlDocCopyNode:
2910 * @node: the node
Daniel Veillardd1640922001-12-17 15:30:10 +00002911 * @doc: the document
Daniel Veillard82daa812001-04-12 08:55:36 +00002912 * @recursive: if 1 do a recursive copy.
2913 *
2914 * Do a copy of the node to a given document.
2915 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002916 * Returns: a new #xmlNodePtr, or NULL in case of error.
Daniel Veillard82daa812001-04-12 08:55:36 +00002917 */
2918xmlNodePtr
Daniel Veillard3ec4c612001-08-28 20:39:49 +00002919xmlDocCopyNode(const xmlNodePtr node, xmlDocPtr doc, int recursive) {
Daniel Veillard82daa812001-04-12 08:55:36 +00002920 xmlNodePtr ret;
2921
2922 ret = xmlStaticCopyNode(node, doc, NULL, recursive);
2923 return(ret);
2924}
2925
2926/**
Owen Taylor3473f882001-02-23 17:55:21 +00002927 * xmlCopyNodeList:
2928 * @node: the first node in the list.
2929 *
2930 * Do a recursive copy of the node list.
2931 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002932 * Returns: a new #xmlNodePtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002933 */
Daniel Veillard3ec4c612001-08-28 20:39:49 +00002934xmlNodePtr xmlCopyNodeList(const xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00002935 xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL);
2936 return(ret);
2937}
2938
2939/**
Owen Taylor3473f882001-02-23 17:55:21 +00002940 * xmlCopyDtd:
2941 * @dtd: the dtd
2942 *
2943 * Do a copy of the dtd.
2944 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002945 * Returns: a new #xmlDtdPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002946 */
2947xmlDtdPtr
2948xmlCopyDtd(xmlDtdPtr dtd) {
2949 xmlDtdPtr ret;
2950
2951 if (dtd == NULL) return(NULL);
2952 ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID);
2953 if (ret == NULL) return(NULL);
2954 if (dtd->entities != NULL)
2955 ret->entities = (void *) xmlCopyEntitiesTable(
2956 (xmlEntitiesTablePtr) dtd->entities);
2957 if (dtd->notations != NULL)
2958 ret->notations = (void *) xmlCopyNotationTable(
2959 (xmlNotationTablePtr) dtd->notations);
2960 if (dtd->elements != NULL)
2961 ret->elements = (void *) xmlCopyElementTable(
2962 (xmlElementTablePtr) dtd->elements);
2963 if (dtd->attributes != NULL)
2964 ret->attributes = (void *) xmlCopyAttributeTable(
2965 (xmlAttributeTablePtr) dtd->attributes);
2966 return(ret);
2967}
2968
2969/**
2970 * xmlCopyDoc:
2971 * @doc: the document
2972 * @recursive: if 1 do a recursive copy.
2973 *
2974 * Do a copy of the document info. If recursive, the content tree will
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002975 * be copied too as well as DTD, namespaces and entities.
Owen Taylor3473f882001-02-23 17:55:21 +00002976 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002977 * Returns: a new #xmlDocPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002978 */
2979xmlDocPtr
2980xmlCopyDoc(xmlDocPtr doc, int recursive) {
2981 xmlDocPtr ret;
2982
2983 if (doc == NULL) return(NULL);
2984 ret = xmlNewDoc(doc->version);
2985 if (ret == NULL) return(NULL);
2986 if (doc->name != NULL)
2987 ret->name = xmlMemStrdup(doc->name);
2988 if (doc->encoding != NULL)
2989 ret->encoding = xmlStrdup(doc->encoding);
2990 ret->charset = doc->charset;
2991 ret->compression = doc->compression;
2992 ret->standalone = doc->standalone;
2993 if (!recursive) return(ret);
2994
Daniel Veillardb33c2012001-04-25 12:59:04 +00002995 ret->last = NULL;
2996 ret->children = NULL;
2997 if (doc->intSubset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002998 ret->intSubset = xmlCopyDtd(doc->intSubset);
Daniel Veillardb33c2012001-04-25 12:59:04 +00002999 ret->intSubset->doc = ret;
3000 ret->intSubset->parent = ret;
3001 }
Owen Taylor3473f882001-02-23 17:55:21 +00003002 if (doc->oldNs != NULL)
3003 ret->oldNs = xmlCopyNamespaceList(doc->oldNs);
3004 if (doc->children != NULL) {
3005 xmlNodePtr tmp;
Daniel Veillardb33c2012001-04-25 12:59:04 +00003006
3007 ret->children = xmlStaticCopyNodeList(doc->children, ret,
3008 (xmlNodePtr)ret);
Owen Taylor3473f882001-02-23 17:55:21 +00003009 ret->last = NULL;
3010 tmp = ret->children;
3011 while (tmp != NULL) {
3012 if (tmp->next == NULL)
3013 ret->last = tmp;
3014 tmp = tmp->next;
3015 }
3016 }
3017 return(ret);
3018}
3019
3020/************************************************************************
3021 * *
3022 * Content access functions *
3023 * *
3024 ************************************************************************/
3025
3026/**
Daniel Veillard8faa7832001-11-26 15:58:08 +00003027 * xmlGetLineNo:
3028 * @node : valid node
3029 *
3030 * Get line number of node. this requires activation of this option
Daniel Veillardd1640922001-12-17 15:30:10 +00003031 * before invoking the parser by calling xmlLineNumbersDefault(1)
Daniel Veillard8faa7832001-11-26 15:58:08 +00003032 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003033 * Returns the line number if successful, -1 otherwise
Daniel Veillard8faa7832001-11-26 15:58:08 +00003034 */
3035long
3036xmlGetLineNo(xmlNodePtr node)
3037{
3038 long result = -1;
3039
3040 if (!node)
3041 return result;
3042 if (node->type == XML_ELEMENT_NODE)
3043 result = (long) node->content;
3044 else if ((node->prev != NULL) &&
3045 ((node->prev->type == XML_ELEMENT_NODE) ||
3046 (node->prev->type == XML_TEXT_NODE)))
3047 result = xmlGetLineNo(node->prev);
3048 else if ((node->parent != NULL) &&
3049 ((node->parent->type == XML_ELEMENT_NODE) ||
3050 (node->parent->type == XML_TEXT_NODE)))
3051 result = xmlGetLineNo(node->parent);
3052
3053 return result;
3054}
3055
3056/**
3057 * xmlGetNodePath:
3058 * @node: a node
3059 *
3060 * Build a structure based Path for the given node
3061 *
3062 * Returns the new path or NULL in case of error. The caller must free
3063 * the returned string
3064 */
3065xmlChar *
3066xmlGetNodePath(xmlNodePtr node)
3067{
3068 xmlNodePtr cur, tmp, next;
3069 xmlChar *buffer = NULL, *temp;
3070 size_t buf_len;
3071 xmlChar *buf;
3072 char sep;
3073 const char *name;
3074 char nametemp[100];
3075 int occur = 0;
3076
3077 if (node == NULL)
3078 return (NULL);
3079
3080 buf_len = 500;
3081 buffer = (xmlChar *) xmlMalloc(buf_len * sizeof(xmlChar));
3082 if (buffer == NULL)
3083 return (NULL);
3084 buf = (xmlChar *) xmlMalloc(buf_len * sizeof(xmlChar));
3085 if (buf == NULL) {
3086 xmlFree(buffer);
3087 return (NULL);
3088 }
3089
3090 buffer[0] = 0;
3091 cur = node;
3092 do {
3093 name = "";
3094 sep = '?';
3095 occur = 0;
3096 if ((cur->type == XML_DOCUMENT_NODE) ||
3097 (cur->type == XML_HTML_DOCUMENT_NODE)) {
3098 if (buffer[0] == '/')
3099 break;
3100 sep = '/';
3101 next = NULL;
3102 } else if (cur->type == XML_ELEMENT_NODE) {
3103 sep = '/';
3104 name = (const char *) cur->name;
3105 if (cur->ns) {
3106 snprintf(nametemp, sizeof(nametemp) - 1,
3107 "%s:%s", cur->ns->prefix, cur->name);
3108 nametemp[sizeof(nametemp) - 1] = 0;
3109 name = nametemp;
3110 }
3111 next = cur->parent;
3112
3113 /*
3114 * Thumbler index computation
3115 */
3116 tmp = cur->prev;
3117 while (tmp != NULL) {
3118 if (xmlStrEqual(cur->name, tmp->name))
3119 occur++;
3120 tmp = tmp->prev;
3121 }
3122 if (occur == 0) {
3123 tmp = cur->next;
3124 while (tmp != NULL) {
3125 if (xmlStrEqual(cur->name, tmp->name))
3126 occur++;
3127 tmp = tmp->next;
3128 }
3129 if (occur != 0)
3130 occur = 1;
3131 } else
3132 occur++;
3133 } else if (cur->type == XML_ATTRIBUTE_NODE) {
3134 sep = '@';
3135 name = (const char *) (((xmlAttrPtr) cur)->name);
3136 next = ((xmlAttrPtr) cur)->parent;
3137 } else {
3138 next = cur->parent;
3139 }
3140
3141 /*
3142 * Make sure there is enough room
3143 */
3144 if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) {
3145 buf_len =
3146 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20;
3147 temp = (xmlChar *) xmlRealloc(buffer, buf_len);
3148 if (temp == NULL) {
3149 xmlFree(buf);
3150 xmlFree(buffer);
3151 return (NULL);
3152 }
3153 buffer = temp;
3154 temp = (xmlChar *) xmlRealloc(buf, buf_len);
3155 if (temp == NULL) {
3156 xmlFree(buf);
3157 xmlFree(buffer);
3158 return (NULL);
3159 }
3160 buf = temp;
3161 }
3162 if (occur == 0)
3163 snprintf((char *) buf, buf_len, "%c%s%s",
3164 sep, name, (char *) buffer);
3165 else
3166 snprintf((char *) buf, buf_len, "%c%s[%d]%s",
3167 sep, name, occur, (char *) buffer);
3168 snprintf((char *) buffer, buf_len, "%s", buf);
3169 cur = next;
3170 } while (cur != NULL);
3171 xmlFree(buf);
3172 return (buffer);
3173}
3174
3175/**
Owen Taylor3473f882001-02-23 17:55:21 +00003176 * xmlDocGetRootElement:
3177 * @doc: the document
3178 *
3179 * Get the root element of the document (doc->children is a list
3180 * containing possibly comments, PIs, etc ...).
3181 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003182 * Returns the #xmlNodePtr for the root or NULL
Owen Taylor3473f882001-02-23 17:55:21 +00003183 */
3184xmlNodePtr
3185xmlDocGetRootElement(xmlDocPtr doc) {
3186 xmlNodePtr ret;
3187
3188 if (doc == NULL) return(NULL);
3189 ret = doc->children;
3190 while (ret != NULL) {
3191 if (ret->type == XML_ELEMENT_NODE)
3192 return(ret);
3193 ret = ret->next;
3194 }
3195 return(ret);
3196}
3197
3198/**
3199 * xmlDocSetRootElement:
3200 * @doc: the document
3201 * @root: the new document root element
3202 *
3203 * Set the root element of the document (doc->children is a list
3204 * containing possibly comments, PIs, etc ...).
3205 *
3206 * Returns the old root element if any was found
3207 */
3208xmlNodePtr
3209xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) {
3210 xmlNodePtr old = NULL;
3211
3212 if (doc == NULL) return(NULL);
3213 old = doc->children;
3214 while (old != NULL) {
3215 if (old->type == XML_ELEMENT_NODE)
3216 break;
3217 old = old->next;
3218 }
3219 if (old == NULL) {
3220 if (doc->children == NULL) {
3221 doc->children = root;
3222 doc->last = root;
3223 } else {
3224 xmlAddSibling(doc->children, root);
3225 }
3226 } else {
3227 xmlReplaceNode(old, root);
3228 }
3229 return(old);
3230}
3231
3232/**
3233 * xmlNodeSetLang:
3234 * @cur: the node being changed
Daniel Veillardd1640922001-12-17 15:30:10 +00003235 * @lang: the language description
Owen Taylor3473f882001-02-23 17:55:21 +00003236 *
3237 * Set the language of a node, i.e. the values of the xml:lang
3238 * attribute.
3239 */
3240void
3241xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) {
3242 if (cur == NULL) return;
3243 switch(cur->type) {
3244 case XML_TEXT_NODE:
3245 case XML_CDATA_SECTION_NODE:
3246 case XML_COMMENT_NODE:
3247 case XML_DOCUMENT_NODE:
3248 case XML_DOCUMENT_TYPE_NODE:
3249 case XML_DOCUMENT_FRAG_NODE:
3250 case XML_NOTATION_NODE:
3251 case XML_HTML_DOCUMENT_NODE:
3252 case XML_DTD_NODE:
3253 case XML_ELEMENT_DECL:
3254 case XML_ATTRIBUTE_DECL:
3255 case XML_ENTITY_DECL:
3256 case XML_PI_NODE:
3257 case XML_ENTITY_REF_NODE:
3258 case XML_ENTITY_NODE:
3259 case XML_NAMESPACE_DECL:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003260#ifdef LIBXML_DOCB_ENABLED
3261 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003262#endif
3263 case XML_XINCLUDE_START:
3264 case XML_XINCLUDE_END:
3265 return;
3266 case XML_ELEMENT_NODE:
3267 case XML_ATTRIBUTE_NODE:
3268 break;
3269 }
3270 xmlSetProp(cur, BAD_CAST "xml:lang", lang);
3271}
3272
3273/**
3274 * xmlNodeGetLang:
3275 * @cur: the node being checked
3276 *
3277 * Searches the language of a node, i.e. the values of the xml:lang
3278 * attribute or the one carried by the nearest ancestor.
3279 *
3280 * Returns a pointer to the lang value, or NULL if not found
3281 * It's up to the caller to free the memory.
3282 */
3283xmlChar *
3284xmlNodeGetLang(xmlNodePtr cur) {
3285 xmlChar *lang;
3286
3287 while (cur != NULL) {
Daniel Veillardc17337c2001-05-09 10:51:31 +00003288 lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE);
Owen Taylor3473f882001-02-23 17:55:21 +00003289 if (lang != NULL)
3290 return(lang);
3291 cur = cur->parent;
3292 }
3293 return(NULL);
3294}
3295
3296
3297/**
3298 * xmlNodeSetSpacePreserve:
3299 * @cur: the node being changed
3300 * @val: the xml:space value ("0": default, 1: "preserve")
3301 *
3302 * Set (or reset) the space preserving behaviour of a node, i.e. the
3303 * value of the xml:space attribute.
3304 */
3305void
3306xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) {
3307 if (cur == NULL) return;
3308 switch(cur->type) {
3309 case XML_TEXT_NODE:
3310 case XML_CDATA_SECTION_NODE:
3311 case XML_COMMENT_NODE:
3312 case XML_DOCUMENT_NODE:
3313 case XML_DOCUMENT_TYPE_NODE:
3314 case XML_DOCUMENT_FRAG_NODE:
3315 case XML_NOTATION_NODE:
3316 case XML_HTML_DOCUMENT_NODE:
3317 case XML_DTD_NODE:
3318 case XML_ELEMENT_DECL:
3319 case XML_ATTRIBUTE_DECL:
3320 case XML_ENTITY_DECL:
3321 case XML_PI_NODE:
3322 case XML_ENTITY_REF_NODE:
3323 case XML_ENTITY_NODE:
3324 case XML_NAMESPACE_DECL:
3325 case XML_XINCLUDE_START:
3326 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003327#ifdef LIBXML_DOCB_ENABLED
3328 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003329#endif
3330 return;
3331 case XML_ELEMENT_NODE:
3332 case XML_ATTRIBUTE_NODE:
3333 break;
3334 }
3335 switch (val) {
3336 case 0:
3337 xmlSetProp(cur, BAD_CAST "xml:space", BAD_CAST "default");
3338 break;
3339 case 1:
3340 xmlSetProp(cur, BAD_CAST "xml:space",
3341 BAD_CAST "preserve");
3342 break;
3343 }
3344}
3345
3346/**
3347 * xmlNodeGetSpacePreserve:
3348 * @cur: the node being checked
3349 *
3350 * Searches the space preserving behaviour of a node, i.e. the values
3351 * of the xml:space attribute or the one carried by the nearest
3352 * ancestor.
3353 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003354 * Returns -1 if xml:space is not inherited, 0 if "default", 1 if "preserve"
Owen Taylor3473f882001-02-23 17:55:21 +00003355 */
3356int
3357xmlNodeGetSpacePreserve(xmlNodePtr cur) {
3358 xmlChar *space;
3359
3360 while (cur != NULL) {
3361 space = xmlGetProp(cur, BAD_CAST "xml:space");
3362 if (space != NULL) {
3363 if (xmlStrEqual(space, BAD_CAST "preserve")) {
3364 xmlFree(space);
3365 return(1);
3366 }
3367 if (xmlStrEqual(space, BAD_CAST "default")) {
3368 xmlFree(space);
3369 return(0);
3370 }
3371 xmlFree(space);
3372 }
3373 cur = cur->parent;
3374 }
3375 return(-1);
3376}
3377
3378/**
3379 * xmlNodeSetName:
3380 * @cur: the node being changed
3381 * @name: the new tag name
3382 *
3383 * Set (or reset) the name of a node.
3384 */
3385void
3386xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) {
3387 if (cur == NULL) return;
3388 if (name == NULL) return;
3389 switch(cur->type) {
3390 case XML_TEXT_NODE:
3391 case XML_CDATA_SECTION_NODE:
3392 case XML_COMMENT_NODE:
3393 case XML_DOCUMENT_TYPE_NODE:
3394 case XML_DOCUMENT_FRAG_NODE:
3395 case XML_NOTATION_NODE:
3396 case XML_HTML_DOCUMENT_NODE:
3397 case XML_NAMESPACE_DECL:
3398 case XML_XINCLUDE_START:
3399 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003400#ifdef LIBXML_DOCB_ENABLED
3401 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003402#endif
3403 return;
3404 case XML_ELEMENT_NODE:
3405 case XML_ATTRIBUTE_NODE:
3406 case XML_PI_NODE:
3407 case XML_ENTITY_REF_NODE:
3408 case XML_ENTITY_NODE:
3409 case XML_DTD_NODE:
3410 case XML_DOCUMENT_NODE:
3411 case XML_ELEMENT_DECL:
3412 case XML_ATTRIBUTE_DECL:
3413 case XML_ENTITY_DECL:
3414 break;
3415 }
3416 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
3417 cur->name = xmlStrdup(name);
3418}
3419
3420/**
3421 * xmlNodeSetBase:
3422 * @cur: the node being changed
3423 * @uri: the new base URI
3424 *
3425 * Set (or reset) the base URI of a node, i.e. the value of the
3426 * xml:base attribute.
3427 */
3428void
3429xmlNodeSetBase(xmlNodePtr cur, xmlChar* uri) {
3430 if (cur == NULL) return;
3431 switch(cur->type) {
3432 case XML_TEXT_NODE:
3433 case XML_CDATA_SECTION_NODE:
3434 case XML_COMMENT_NODE:
3435 case XML_DOCUMENT_NODE:
3436 case XML_DOCUMENT_TYPE_NODE:
3437 case XML_DOCUMENT_FRAG_NODE:
3438 case XML_NOTATION_NODE:
3439 case XML_HTML_DOCUMENT_NODE:
3440 case XML_DTD_NODE:
3441 case XML_ELEMENT_DECL:
3442 case XML_ATTRIBUTE_DECL:
3443 case XML_ENTITY_DECL:
3444 case XML_PI_NODE:
3445 case XML_ENTITY_REF_NODE:
3446 case XML_ENTITY_NODE:
3447 case XML_NAMESPACE_DECL:
3448 case XML_XINCLUDE_START:
3449 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003450#ifdef LIBXML_DOCB_ENABLED
3451 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003452#endif
3453 return;
3454 case XML_ELEMENT_NODE:
3455 case XML_ATTRIBUTE_NODE:
3456 break;
3457 }
3458 xmlSetProp(cur, BAD_CAST "xml:base", uri);
3459}
3460
3461/**
Owen Taylor3473f882001-02-23 17:55:21 +00003462 * xmlNodeGetBase:
3463 * @doc: the document the node pertains to
3464 * @cur: the node being checked
3465 *
3466 * Searches for the BASE URL. The code should work on both XML
3467 * and HTML document even if base mechanisms are completely different.
3468 * It returns the base as defined in RFC 2396 sections
3469 * 5.1.1. Base URI within Document Content
3470 * and
3471 * 5.1.2. Base URI from the Encapsulating Entity
3472 * However it does not return the document base (5.1.3), use
3473 * xmlDocumentGetBase() for this
3474 *
3475 * Returns a pointer to the base URL, or NULL if not found
3476 * It's up to the caller to free the memory.
3477 */
3478xmlChar *
3479xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00003480 xmlChar *oldbase = NULL;
3481 xmlChar *base, *newbase;
Owen Taylor3473f882001-02-23 17:55:21 +00003482
3483 if ((cur == NULL) && (doc == NULL))
3484 return(NULL);
3485 if (doc == NULL) doc = cur->doc;
3486 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
3487 cur = doc->children;
3488 while ((cur != NULL) && (cur->name != NULL)) {
3489 if (cur->type != XML_ELEMENT_NODE) {
3490 cur = cur->next;
3491 continue;
3492 }
3493 if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) {
3494 cur = cur->children;
3495 continue;
3496 }
3497 if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) {
3498 cur = cur->children;
3499 continue;
3500 }
3501 if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) {
3502 return(xmlGetProp(cur, BAD_CAST "href"));
3503 }
3504 cur = cur->next;
3505 }
3506 return(NULL);
3507 }
3508 while (cur != NULL) {
3509 if (cur->type == XML_ENTITY_DECL) {
3510 xmlEntityPtr ent = (xmlEntityPtr) cur;
3511 return(xmlStrdup(ent->URI));
3512 }
Daniel Veillard42596ad2001-05-22 16:57:14 +00003513 if (cur->type == XML_ELEMENT_NODE) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00003514 base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE);
Daniel Veillard42596ad2001-05-22 16:57:14 +00003515 if (base != NULL) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00003516 if (oldbase != NULL) {
3517 newbase = xmlBuildURI(oldbase, base);
3518 if (newbase != NULL) {
3519 xmlFree(oldbase);
3520 xmlFree(base);
3521 oldbase = newbase;
3522 } else {
3523 xmlFree(oldbase);
3524 xmlFree(base);
3525 return(NULL);
3526 }
3527 } else {
3528 oldbase = base;
3529 }
3530 if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) ||
3531 (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) ||
3532 (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4)))
3533 return(oldbase);
Daniel Veillard42596ad2001-05-22 16:57:14 +00003534 }
3535 }
Owen Taylor3473f882001-02-23 17:55:21 +00003536 cur = cur->parent;
3537 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00003538 if ((doc != NULL) && (doc->URL != NULL)) {
3539 if (oldbase == NULL)
3540 return(xmlStrdup(doc->URL));
3541 newbase = xmlBuildURI(oldbase, doc->URL);
3542 xmlFree(oldbase);
3543 return(newbase);
3544 }
3545 return(oldbase);
Owen Taylor3473f882001-02-23 17:55:21 +00003546}
3547
3548/**
3549 * xmlNodeGetContent:
3550 * @cur: the node being read
3551 *
3552 * Read the value of a node, this can be either the text carried
3553 * directly by this node if it's a TEXT node or the aggregate string
3554 * of the values carried by this node child's (TEXT and ENTITY_REF).
Daniel Veillardd1640922001-12-17 15:30:10 +00003555 * Entity references are substituted.
3556 * Returns a new #xmlChar * or NULL if no content is available.
Owen Taylor3473f882001-02-23 17:55:21 +00003557 * It's up to the caller to free the memory.
3558 */
3559xmlChar *
3560xmlNodeGetContent(xmlNodePtr cur) {
3561 if (cur == NULL) return(NULL);
3562 switch (cur->type) {
3563 case XML_DOCUMENT_FRAG_NODE:
3564 case XML_ELEMENT_NODE: {
3565 xmlNodePtr tmp = cur;
3566 xmlBufferPtr buffer;
3567 xmlChar *ret;
3568
3569 buffer = xmlBufferCreate();
3570 if (buffer == NULL)
3571 return(NULL);
3572 while (tmp != NULL) {
3573 switch (tmp->type) {
Daniel Veillard2d703722001-05-30 18:32:34 +00003574 case XML_CDATA_SECTION_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003575 case XML_TEXT_NODE:
3576 if (tmp->content != NULL)
3577#ifndef XML_USE_BUFFER_CONTENT
3578 xmlBufferCat(buffer, tmp->content);
3579#else
3580 xmlBufferCat(buffer,
3581 xmlBufferContent(tmp->content));
3582#endif
3583 break;
3584 case XML_ENTITY_REF_NODE: {
3585 xmlEntityPtr ent;
3586
3587 ent = xmlGetDocEntity(cur->doc, tmp->name);
3588 if (ent != NULL)
3589 xmlBufferCat(buffer, ent->content);
3590 }
3591 default:
3592 break;
3593 }
3594 /*
3595 * Skip to next node
3596 */
3597 if (tmp->children != NULL) {
3598 if (tmp->children->type != XML_ENTITY_DECL) {
3599 tmp = tmp->children;
3600 continue;
3601 }
3602 }
Daniel Veillard6c831202001-03-07 15:57:53 +00003603 if (tmp == cur)
3604 break;
3605
Owen Taylor3473f882001-02-23 17:55:21 +00003606 if (tmp->next != NULL) {
3607 tmp = tmp->next;
3608 continue;
3609 }
3610
3611 do {
3612 tmp = tmp->parent;
3613 if (tmp == NULL)
3614 break;
Daniel Veillard6c831202001-03-07 15:57:53 +00003615 if (tmp == cur) {
Owen Taylor3473f882001-02-23 17:55:21 +00003616 tmp = NULL;
3617 break;
3618 }
3619 if (tmp->next != NULL) {
3620 tmp = tmp->next;
3621 break;
3622 }
3623 } while (tmp != NULL);
3624 }
3625 ret = buffer->content;
3626 buffer->content = NULL;
3627 xmlBufferFree(buffer);
3628 return(ret);
3629 }
3630 case XML_ATTRIBUTE_NODE: {
3631 xmlAttrPtr attr = (xmlAttrPtr) cur;
3632 if (attr->parent != NULL)
3633 return(xmlNodeListGetString(attr->parent->doc, attr->children, 1));
3634 else
3635 return(xmlNodeListGetString(NULL, attr->children, 1));
3636 break;
3637 }
3638 case XML_COMMENT_NODE:
3639 case XML_PI_NODE:
3640 if (cur->content != NULL)
3641#ifndef XML_USE_BUFFER_CONTENT
3642 return(xmlStrdup(cur->content));
3643#else
3644 return(xmlStrdup(xmlBufferContent(cur->content)));
3645#endif
3646 return(NULL);
3647 case XML_ENTITY_REF_NODE:
3648 /*
3649 * Locate the entity, and get it's content
3650 * @@@
3651 */
3652 return(NULL);
3653 case XML_ENTITY_NODE:
3654 case XML_DOCUMENT_NODE:
3655 case XML_HTML_DOCUMENT_NODE:
3656 case XML_DOCUMENT_TYPE_NODE:
3657 case XML_NOTATION_NODE:
3658 case XML_DTD_NODE:
3659 case XML_XINCLUDE_START:
3660 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003661#ifdef LIBXML_DOCB_ENABLED
3662 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003663#endif
3664 return(NULL);
3665 case XML_NAMESPACE_DECL:
3666 return(xmlStrdup(((xmlNsPtr)cur)->href));
3667 case XML_ELEMENT_DECL:
3668 /* TODO !!! */
3669 return(NULL);
3670 case XML_ATTRIBUTE_DECL:
3671 /* TODO !!! */
3672 return(NULL);
3673 case XML_ENTITY_DECL:
3674 /* TODO !!! */
3675 return(NULL);
3676 case XML_CDATA_SECTION_NODE:
3677 case XML_TEXT_NODE:
3678 if (cur->content != NULL)
3679#ifndef XML_USE_BUFFER_CONTENT
3680 return(xmlStrdup(cur->content));
3681#else
3682 return(xmlStrdup(xmlBufferContent(cur->content)));
3683#endif
3684 return(NULL);
3685 }
3686 return(NULL);
3687}
3688
3689/**
3690 * xmlNodeSetContent:
3691 * @cur: the node being modified
3692 * @content: the new value of the content
3693 *
3694 * Replace the content of a node.
3695 */
3696void
3697xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) {
3698 if (cur == NULL) {
3699#ifdef DEBUG_TREE
3700 xmlGenericError(xmlGenericErrorContext,
3701 "xmlNodeSetContent : node == NULL\n");
3702#endif
3703 return;
3704 }
3705 switch (cur->type) {
3706 case XML_DOCUMENT_FRAG_NODE:
3707 case XML_ELEMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003708 if (cur->children != NULL) xmlFreeNodeList(cur->children);
3709 cur->children = xmlStringGetNodeList(cur->doc, content);
3710 UPDATE_LAST_CHILD_AND_PARENT(cur)
3711 break;
3712 case XML_ATTRIBUTE_NODE:
3713 break;
3714 case XML_TEXT_NODE:
3715 case XML_CDATA_SECTION_NODE:
3716 case XML_ENTITY_REF_NODE:
3717 case XML_ENTITY_NODE:
3718 case XML_PI_NODE:
3719 case XML_COMMENT_NODE:
3720 if (cur->content != NULL) {
3721#ifndef XML_USE_BUFFER_CONTENT
3722 xmlFree(cur->content);
3723#else
3724 xmlBufferFree(cur->content);
3725#endif
3726 }
3727 if (cur->children != NULL) xmlFreeNodeList(cur->children);
3728 cur->last = cur->children = NULL;
3729 if (content != NULL) {
3730#ifndef XML_USE_BUFFER_CONTENT
3731 cur->content = xmlStrdup(content);
3732#else
3733 cur->content = xmlBufferCreateSize(0);
3734 xmlBufferSetAllocationScheme(cur->content,
3735 xmlGetBufferAllocationScheme());
3736 xmlBufferAdd(cur->content, content, -1);
3737#endif
3738 } else
3739 cur->content = NULL;
3740 break;
3741 case XML_DOCUMENT_NODE:
3742 case XML_HTML_DOCUMENT_NODE:
3743 case XML_DOCUMENT_TYPE_NODE:
3744 case XML_XINCLUDE_START:
3745 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003746#ifdef LIBXML_DOCB_ENABLED
3747 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003748#endif
3749 break;
3750 case XML_NOTATION_NODE:
3751 break;
3752 case XML_DTD_NODE:
3753 break;
3754 case XML_NAMESPACE_DECL:
3755 break;
3756 case XML_ELEMENT_DECL:
3757 /* TODO !!! */
3758 break;
3759 case XML_ATTRIBUTE_DECL:
3760 /* TODO !!! */
3761 break;
3762 case XML_ENTITY_DECL:
3763 /* TODO !!! */
3764 break;
3765 }
3766}
3767
3768/**
3769 * xmlNodeSetContentLen:
3770 * @cur: the node being modified
3771 * @content: the new value of the content
3772 * @len: the size of @content
3773 *
3774 * Replace the content of a node.
3775 */
3776void
3777xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
3778 if (cur == NULL) {
3779#ifdef DEBUG_TREE
3780 xmlGenericError(xmlGenericErrorContext,
3781 "xmlNodeSetContentLen : node == NULL\n");
3782#endif
3783 return;
3784 }
3785 switch (cur->type) {
3786 case XML_DOCUMENT_FRAG_NODE:
3787 case XML_ELEMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003788 if (cur->children != NULL) xmlFreeNodeList(cur->children);
3789 cur->children = xmlStringLenGetNodeList(cur->doc, content, len);
3790 UPDATE_LAST_CHILD_AND_PARENT(cur)
3791 break;
3792 case XML_ATTRIBUTE_NODE:
3793 break;
3794 case XML_TEXT_NODE:
3795 case XML_CDATA_SECTION_NODE:
3796 case XML_ENTITY_REF_NODE:
3797 case XML_ENTITY_NODE:
3798 case XML_PI_NODE:
3799 case XML_COMMENT_NODE:
3800 case XML_NOTATION_NODE:
3801 if (cur->content != NULL) {
3802#ifndef XML_USE_BUFFER_CONTENT
3803 xmlFree(cur->content);
3804#else
3805 xmlBufferFree(cur->content);
3806#endif
3807 }
3808 if (cur->children != NULL) xmlFreeNodeList(cur->children);
3809 cur->children = cur->last = NULL;
3810 if (content != NULL) {
3811#ifndef XML_USE_BUFFER_CONTENT
3812 cur->content = xmlStrndup(content, len);
3813#else
3814 cur->content = xmlBufferCreateSize(len);
3815 xmlBufferSetAllocationScheme(cur->content,
3816 xmlGetBufferAllocationScheme());
3817 xmlBufferAdd(cur->content, content, len);
3818#endif
3819 } else
3820 cur->content = NULL;
3821 break;
3822 case XML_DOCUMENT_NODE:
3823 case XML_DTD_NODE:
3824 case XML_HTML_DOCUMENT_NODE:
3825 case XML_DOCUMENT_TYPE_NODE:
3826 case XML_NAMESPACE_DECL:
3827 case XML_XINCLUDE_START:
3828 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003829#ifdef LIBXML_DOCB_ENABLED
3830 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003831#endif
3832 break;
3833 case XML_ELEMENT_DECL:
3834 /* TODO !!! */
3835 break;
3836 case XML_ATTRIBUTE_DECL:
3837 /* TODO !!! */
3838 break;
3839 case XML_ENTITY_DECL:
3840 /* TODO !!! */
3841 break;
3842 }
3843}
3844
3845/**
3846 * xmlNodeAddContentLen:
3847 * @cur: the node being modified
3848 * @content: extra content
3849 * @len: the size of @content
3850 *
3851 * Append the extra substring to the node content.
3852 */
3853void
3854xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
3855 if (cur == NULL) {
3856#ifdef DEBUG_TREE
3857 xmlGenericError(xmlGenericErrorContext,
3858 "xmlNodeAddContentLen : node == NULL\n");
3859#endif
3860 return;
3861 }
3862 if (len <= 0) return;
3863 switch (cur->type) {
3864 case XML_DOCUMENT_FRAG_NODE:
3865 case XML_ELEMENT_NODE: {
Daniel Veillardacb2bda2002-01-13 16:15:43 +00003866 xmlNodePtr last, newNode, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00003867
Daniel Veillard7db37732001-07-12 01:20:08 +00003868 last = cur->last;
Owen Taylor3473f882001-02-23 17:55:21 +00003869 newNode = xmlNewTextLen(content, len);
3870 if (newNode != NULL) {
Daniel Veillardacb2bda2002-01-13 16:15:43 +00003871 tmp = xmlAddChild(cur, newNode);
3872 if (tmp != newNode)
3873 return;
Owen Taylor3473f882001-02-23 17:55:21 +00003874 if ((last != NULL) && (last->next == newNode)) {
3875 xmlTextMerge(last, newNode);
3876 }
3877 }
3878 break;
3879 }
3880 case XML_ATTRIBUTE_NODE:
3881 break;
3882 case XML_TEXT_NODE:
3883 case XML_CDATA_SECTION_NODE:
3884 case XML_ENTITY_REF_NODE:
3885 case XML_ENTITY_NODE:
3886 case XML_PI_NODE:
3887 case XML_COMMENT_NODE:
3888 case XML_NOTATION_NODE:
3889 if (content != NULL) {
3890#ifndef XML_USE_BUFFER_CONTENT
3891 cur->content = xmlStrncat(cur->content, content, len);
3892#else
3893 xmlBufferAdd(cur->content, content, len);
3894#endif
3895 }
3896 case XML_DOCUMENT_NODE:
3897 case XML_DTD_NODE:
3898 case XML_HTML_DOCUMENT_NODE:
3899 case XML_DOCUMENT_TYPE_NODE:
3900 case XML_NAMESPACE_DECL:
3901 case XML_XINCLUDE_START:
3902 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003903#ifdef LIBXML_DOCB_ENABLED
3904 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003905#endif
3906 break;
3907 case XML_ELEMENT_DECL:
3908 case XML_ATTRIBUTE_DECL:
3909 case XML_ENTITY_DECL:
3910 break;
3911 }
3912}
3913
3914/**
3915 * xmlNodeAddContent:
3916 * @cur: the node being modified
3917 * @content: extra content
3918 *
3919 * Append the extra substring to the node content.
3920 */
3921void
3922xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) {
3923 int len;
3924
3925 if (cur == NULL) {
3926#ifdef DEBUG_TREE
3927 xmlGenericError(xmlGenericErrorContext,
3928 "xmlNodeAddContent : node == NULL\n");
3929#endif
3930 return;
3931 }
3932 if (content == NULL) return;
3933 len = xmlStrlen(content);
3934 xmlNodeAddContentLen(cur, content, len);
3935}
3936
3937/**
3938 * xmlTextMerge:
3939 * @first: the first text node
3940 * @second: the second text node being merged
3941 *
3942 * Merge two text nodes into one
3943 * Returns the first text node augmented
3944 */
3945xmlNodePtr
3946xmlTextMerge(xmlNodePtr first, xmlNodePtr second) {
3947 if (first == NULL) return(second);
3948 if (second == NULL) return(first);
3949 if (first->type != XML_TEXT_NODE) return(first);
3950 if (second->type != XML_TEXT_NODE) return(first);
3951 if (second->name != first->name)
3952 return(first);
3953#ifndef XML_USE_BUFFER_CONTENT
3954 xmlNodeAddContent(first, second->content);
3955#else
3956 xmlNodeAddContent(first, xmlBufferContent(second->content));
3957#endif
3958 xmlUnlinkNode(second);
3959 xmlFreeNode(second);
3960 return(first);
3961}
3962
3963/**
3964 * xmlGetNsList:
3965 * @doc: the document
3966 * @node: the current node
3967 *
3968 * Search all the namespace applying to a given element.
Daniel Veillardd1640922001-12-17 15:30:10 +00003969 * Returns an NULL terminated array of all the #xmlNsPtr found
Owen Taylor3473f882001-02-23 17:55:21 +00003970 * that need to be freed by the caller or NULL if no
3971 * namespace if defined
3972 */
3973xmlNsPtr *
Daniel Veillard77044732001-06-29 21:31:07 +00003974xmlGetNsList(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node)
3975{
Owen Taylor3473f882001-02-23 17:55:21 +00003976 xmlNsPtr cur;
3977 xmlNsPtr *ret = NULL;
3978 int nbns = 0;
3979 int maxns = 10;
3980 int i;
3981
3982 while (node != NULL) {
Daniel Veillard77044732001-06-29 21:31:07 +00003983 if (node->type == XML_ELEMENT_NODE) {
3984 cur = node->nsDef;
3985 while (cur != NULL) {
3986 if (ret == NULL) {
3987 ret =
3988 (xmlNsPtr *) xmlMalloc((maxns + 1) *
3989 sizeof(xmlNsPtr));
3990 if (ret == NULL) {
3991 xmlGenericError(xmlGenericErrorContext,
3992 "xmlGetNsList : out of memory!\n");
3993 return (NULL);
3994 }
3995 ret[nbns] = NULL;
3996 }
3997 for (i = 0; i < nbns; i++) {
3998 if ((cur->prefix == ret[i]->prefix) ||
3999 (xmlStrEqual(cur->prefix, ret[i]->prefix)))
4000 break;
4001 }
4002 if (i >= nbns) {
4003 if (nbns >= maxns) {
4004 maxns *= 2;
4005 ret = (xmlNsPtr *) xmlRealloc(ret,
4006 (maxns +
4007 1) *
4008 sizeof(xmlNsPtr));
4009 if (ret == NULL) {
4010 xmlGenericError(xmlGenericErrorContext,
4011 "xmlGetNsList : realloc failed!\n");
4012 return (NULL);
4013 }
4014 }
4015 ret[nbns++] = cur;
4016 ret[nbns] = NULL;
4017 }
Owen Taylor3473f882001-02-23 17:55:21 +00004018
Daniel Veillard77044732001-06-29 21:31:07 +00004019 cur = cur->next;
4020 }
4021 }
4022 node = node->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00004023 }
Daniel Veillard77044732001-06-29 21:31:07 +00004024 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00004025}
4026
4027/**
4028 * xmlSearchNs:
4029 * @doc: the document
4030 * @node: the current node
Daniel Veillard77851712001-02-27 21:54:07 +00004031 * @nameSpace: the namespace prefix
Owen Taylor3473f882001-02-23 17:55:21 +00004032 *
4033 * Search a Ns registered under a given name space for a document.
4034 * recurse on the parents until it finds the defined namespace
4035 * or return NULL otherwise.
4036 * @nameSpace can be NULL, this is a search for the default namespace.
4037 * We don't allow to cross entities boundaries. If you don't declare
4038 * the namespace within those you will be in troubles !!! A warning
4039 * is generated to cover this case.
4040 *
4041 * Returns the namespace pointer or NULL.
4042 */
4043xmlNsPtr
4044xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) {
4045 xmlNsPtr cur;
4046
4047 if (node == NULL) return(NULL);
4048 if ((nameSpace != NULL) &&
4049 (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) {
4050 if (doc->oldNs == NULL) {
4051 /*
4052 * Allocate a new Namespace and fill the fields.
4053 */
4054 doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
4055 if (doc->oldNs == NULL) {
4056 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00004057 "xmlSearchNs : malloc failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004058 return(NULL);
4059 }
4060 memset(doc->oldNs, 0, sizeof(xmlNs));
4061 doc->oldNs->type = XML_LOCAL_NAMESPACE;
4062
4063 doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE);
4064 doc->oldNs->prefix = xmlStrdup((const xmlChar *)"xml");
4065 }
4066 return(doc->oldNs);
4067 }
4068 while (node != NULL) {
4069 if ((node->type == XML_ENTITY_REF_NODE) ||
4070 (node->type == XML_ENTITY_NODE) ||
4071 (node->type == XML_ENTITY_DECL))
4072 return(NULL);
4073 if (node->type == XML_ELEMENT_NODE) {
4074 cur = node->nsDef;
4075 while (cur != NULL) {
4076 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
4077 (cur->href != NULL))
4078 return(cur);
4079 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
4080 (cur->href != NULL) &&
4081 (xmlStrEqual(cur->prefix, nameSpace)))
4082 return(cur);
4083 cur = cur->next;
4084 }
4085 }
4086 node = node->parent;
4087 }
4088 return(NULL);
4089}
4090
4091/**
4092 * xmlSearchNsByHref:
4093 * @doc: the document
4094 * @node: the current node
4095 * @href: the namespace value
4096 *
4097 * Search a Ns aliasing a given URI. Recurse on the parents until it finds
4098 * the defined namespace or return NULL otherwise.
4099 * Returns the namespace pointer or NULL.
4100 */
4101xmlNsPtr
4102xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar *href) {
4103 xmlNsPtr cur;
4104 xmlNodePtr orig = node;
4105
4106 if ((node == NULL) || (href == NULL)) return(NULL);
4107 if (xmlStrEqual(href, XML_XML_NAMESPACE)) {
4108 if (doc->oldNs == NULL) {
4109 /*
4110 * Allocate a new Namespace and fill the fields.
4111 */
4112 doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
4113 if (doc->oldNs == NULL) {
4114 xmlGenericError(xmlGenericErrorContext,
4115 "xmlSearchNsByHref : malloc failed\n");
4116 return(NULL);
4117 }
4118 memset(doc->oldNs, 0, sizeof(xmlNs));
4119 doc->oldNs->type = XML_LOCAL_NAMESPACE;
4120
4121 doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE);
4122 doc->oldNs->prefix = xmlStrdup((const xmlChar *)"xml");
4123 }
4124 return(doc->oldNs);
4125 }
4126 while (node != NULL) {
4127 cur = node->nsDef;
4128 while (cur != NULL) {
4129 if ((cur->href != NULL) && (href != NULL) &&
4130 (xmlStrEqual(cur->href, href))) {
4131 /*
4132 * Check that the prefix is not shadowed between orig and node
4133 */
4134 xmlNodePtr check = orig;
4135 xmlNsPtr tst;
4136
4137 while (check != node) {
4138 tst = check->nsDef;
4139 while (tst != NULL) {
4140 if ((tst->prefix == NULL) && (cur->prefix == NULL))
4141 goto shadowed;
4142 if ((tst->prefix != NULL) && (cur->prefix != NULL) &&
4143 (xmlStrEqual(tst->prefix, cur->prefix)))
4144 goto shadowed;
4145 tst = tst->next;
4146 }
4147 check = check->parent;
4148 }
4149 return(cur);
4150 }
4151shadowed:
4152 cur = cur->next;
4153 }
4154 node = node->parent;
4155 }
4156 return(NULL);
4157}
4158
4159/**
4160 * xmlNewReconciliedNs
4161 * @doc: the document
4162 * @tree: a node expected to hold the new namespace
4163 * @ns: the original namespace
4164 *
4165 * This function tries to locate a namespace definition in a tree
4166 * ancestors, or create a new namespace definition node similar to
4167 * @ns trying to reuse the same prefix. However if the given prefix is
4168 * null (default namespace) or reused within the subtree defined by
4169 * @tree or on one of its ancestors then a new prefix is generated.
4170 * Returns the (new) namespace definition or NULL in case of error
4171 */
4172xmlNsPtr
4173xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) {
4174 xmlNsPtr def;
4175 xmlChar prefix[50];
4176 int counter = 1;
4177
4178 if (tree == NULL) {
4179#ifdef DEBUG_TREE
4180 xmlGenericError(xmlGenericErrorContext,
4181 "xmlNewReconciliedNs : tree == NULL\n");
4182#endif
4183 return(NULL);
4184 }
4185 if (ns == NULL) {
4186#ifdef DEBUG_TREE
4187 xmlGenericError(xmlGenericErrorContext,
4188 "xmlNewReconciliedNs : ns == NULL\n");
4189#endif
4190 return(NULL);
4191 }
4192 /*
4193 * Search an existing namespace definition inherited.
4194 */
4195 def = xmlSearchNsByHref(doc, tree, ns->href);
4196 if (def != NULL)
4197 return(def);
4198
4199 /*
4200 * Find a close prefix which is not already in use.
4201 * Let's strip namespace prefixes longer than 20 chars !
4202 */
4203 sprintf((char *) prefix, "%.20s", ns->prefix);
4204 def = xmlSearchNs(doc, tree, prefix);
4205 while (def != NULL) {
4206 if (counter > 1000) return(NULL);
4207 sprintf((char *) prefix, "%.20s%d", ns->prefix, counter++);
4208 def = xmlSearchNs(doc, tree, prefix);
4209 }
4210
4211 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00004212 * OK, now we are ready to create a new one.
Owen Taylor3473f882001-02-23 17:55:21 +00004213 */
4214 def = xmlNewNs(tree, ns->href, prefix);
4215 return(def);
4216}
4217
4218/**
4219 * xmlReconciliateNs
4220 * @doc: the document
4221 * @tree: a node defining the subtree to reconciliate
4222 *
4223 * This function checks that all the namespaces declared within the given
4224 * tree are properly declared. This is needed for example after Copy or Cut
4225 * and then paste operations. The subtree may still hold pointers to
4226 * namespace declarations outside the subtree or invalid/masked. As much
Daniel Veillardd1640922001-12-17 15:30:10 +00004227 * as possible the function try to reuse the existing namespaces found in
Owen Taylor3473f882001-02-23 17:55:21 +00004228 * the new environment. If not possible the new namespaces are redeclared
4229 * on @tree at the top of the given subtree.
4230 * Returns the number of namespace declarations created or -1 in case of error.
4231 */
4232int
4233xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) {
4234 xmlNsPtr *oldNs = NULL;
4235 xmlNsPtr *newNs = NULL;
4236 int sizeCache = 0;
4237 int nbCache = 0;
4238
4239 xmlNsPtr n;
4240 xmlNodePtr node = tree;
4241 xmlAttrPtr attr;
4242 int ret = 0, i;
4243
4244 while (node != NULL) {
4245 /*
4246 * Reconciliate the node namespace
4247 */
4248 if (node->ns != NULL) {
4249 /*
4250 * initialize the cache if needed
4251 */
4252 if (sizeCache == 0) {
4253 sizeCache = 10;
4254 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
4255 sizeof(xmlNsPtr));
4256 if (oldNs == NULL) {
4257 xmlGenericError(xmlGenericErrorContext,
4258 "xmlReconciliateNs : memory pbm\n");
4259 return(-1);
4260 }
4261 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
4262 sizeof(xmlNsPtr));
4263 if (newNs == NULL) {
4264 xmlGenericError(xmlGenericErrorContext,
4265 "xmlReconciliateNs : memory pbm\n");
4266 xmlFree(oldNs);
4267 return(-1);
4268 }
4269 }
4270 for (i = 0;i < nbCache;i++) {
4271 if (oldNs[i] == node->ns) {
4272 node->ns = newNs[i];
4273 break;
4274 }
4275 }
4276 if (i == nbCache) {
4277 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00004278 * OK we need to recreate a new namespace definition
Owen Taylor3473f882001-02-23 17:55:21 +00004279 */
4280 n = xmlNewReconciliedNs(doc, tree, node->ns);
4281 if (n != NULL) { /* :-( what if else ??? */
4282 /*
4283 * check if we need to grow the cache buffers.
4284 */
4285 if (sizeCache <= nbCache) {
4286 sizeCache *= 2;
4287 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
4288 sizeof(xmlNsPtr));
4289 if (oldNs == NULL) {
4290 xmlGenericError(xmlGenericErrorContext,
4291 "xmlReconciliateNs : memory pbm\n");
4292 xmlFree(newNs);
4293 return(-1);
4294 }
4295 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
4296 sizeof(xmlNsPtr));
4297 if (newNs == NULL) {
4298 xmlGenericError(xmlGenericErrorContext,
4299 "xmlReconciliateNs : memory pbm\n");
4300 xmlFree(oldNs);
4301 return(-1);
4302 }
4303 }
4304 newNs[nbCache] = n;
4305 oldNs[nbCache++] = node->ns;
4306 node->ns = n;
4307 }
4308 }
4309 }
4310 /*
4311 * now check for namespace hold by attributes on the node.
4312 */
4313 attr = node->properties;
4314 while (attr != NULL) {
4315 if (attr->ns != NULL) {
4316 /*
4317 * initialize the cache if needed
4318 */
4319 if (sizeCache == 0) {
4320 sizeCache = 10;
4321 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
4322 sizeof(xmlNsPtr));
4323 if (oldNs == NULL) {
4324 xmlGenericError(xmlGenericErrorContext,
4325 "xmlReconciliateNs : memory pbm\n");
4326 return(-1);
4327 }
4328 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
4329 sizeof(xmlNsPtr));
4330 if (newNs == NULL) {
4331 xmlGenericError(xmlGenericErrorContext,
4332 "xmlReconciliateNs : memory pbm\n");
4333 xmlFree(oldNs);
4334 return(-1);
4335 }
4336 }
4337 for (i = 0;i < nbCache;i++) {
4338 if (oldNs[i] == attr->ns) {
4339 node->ns = newNs[i];
4340 break;
4341 }
4342 }
4343 if (i == nbCache) {
4344 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00004345 * OK we need to recreate a new namespace definition
Owen Taylor3473f882001-02-23 17:55:21 +00004346 */
4347 n = xmlNewReconciliedNs(doc, tree, attr->ns);
4348 if (n != NULL) { /* :-( what if else ??? */
4349 /*
4350 * check if we need to grow the cache buffers.
4351 */
4352 if (sizeCache <= nbCache) {
4353 sizeCache *= 2;
4354 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
4355 sizeof(xmlNsPtr));
4356 if (oldNs == NULL) {
4357 xmlGenericError(xmlGenericErrorContext,
4358 "xmlReconciliateNs : memory pbm\n");
4359 xmlFree(newNs);
4360 return(-1);
4361 }
4362 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
4363 sizeof(xmlNsPtr));
4364 if (newNs == NULL) {
4365 xmlGenericError(xmlGenericErrorContext,
4366 "xmlReconciliateNs : memory pbm\n");
4367 xmlFree(oldNs);
4368 return(-1);
4369 }
4370 }
4371 newNs[nbCache] = n;
4372 oldNs[nbCache++] = attr->ns;
4373 attr->ns = n;
4374 }
4375 }
4376 }
4377 attr = attr->next;
4378 }
4379
4380 /*
4381 * Browse the full subtree, deep first
4382 */
4383 if (node->children != NULL) {
4384 /* deep first */
4385 node = node->children;
4386 } else if ((node != tree) && (node->next != NULL)) {
4387 /* then siblings */
4388 node = node->next;
4389 } else if (node != tree) {
4390 /* go up to parents->next if needed */
4391 while (node != tree) {
4392 if (node->parent != NULL)
4393 node = node->parent;
4394 if ((node != tree) && (node->next != NULL)) {
4395 node = node->next;
4396 break;
4397 }
4398 if (node->parent == NULL) {
4399 node = NULL;
4400 break;
4401 }
4402 }
4403 /* exit condition */
4404 if (node == tree)
4405 node = NULL;
4406 }
4407 }
4408 return(ret);
4409}
4410
4411/**
4412 * xmlHasProp:
4413 * @node: the node
4414 * @name: the attribute name
4415 *
4416 * Search an attribute associated to a node
4417 * This function also looks in DTD attribute declaration for #FIXED or
4418 * default declaration values unless DTD use has been turned off.
4419 *
4420 * Returns the attribute or the attribute declaration or NULL if
4421 * neither was found.
4422 */
4423xmlAttrPtr
4424xmlHasProp(xmlNodePtr node, const xmlChar *name) {
4425 xmlAttrPtr prop;
4426 xmlDocPtr doc;
4427
4428 if ((node == NULL) || (name == NULL)) return(NULL);
4429 /*
4430 * Check on the properties attached to the node
4431 */
4432 prop = node->properties;
4433 while (prop != NULL) {
4434 if (xmlStrEqual(prop->name, name)) {
4435 return(prop);
4436 }
4437 prop = prop->next;
4438 }
4439 if (!xmlCheckDTD) return(NULL);
4440
4441 /*
4442 * Check if there is a default declaration in the internal
4443 * or external subsets
4444 */
4445 doc = node->doc;
4446 if (doc != NULL) {
4447 xmlAttributePtr attrDecl;
4448 if (doc->intSubset != NULL) {
4449 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
4450 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4451 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
4452 if (attrDecl != NULL)
4453 return((xmlAttrPtr) attrDecl);
4454 }
4455 }
4456 return(NULL);
4457}
4458
4459/**
Daniel Veillarde95e2392001-06-06 10:46:28 +00004460 * xmlHasNsProp:
4461 * @node: the node
4462 * @name: the attribute name
Daniel Veillardca2366a2001-06-11 12:09:01 +00004463 * @nameSpace: the URI of the namespace
Daniel Veillarde95e2392001-06-06 10:46:28 +00004464 *
4465 * Search for an attribute associated to a node
4466 * This attribute has to be anchored in the namespace specified.
4467 * This does the entity substitution.
4468 * This function looks in DTD attribute declaration for #FIXED or
4469 * default declaration values unless DTD use has been turned off.
4470 *
4471 * Returns the attribute or the attribute declaration or NULL
4472 * if neither was found.
4473 */
4474xmlAttrPtr
Daniel Veillardca2366a2001-06-11 12:09:01 +00004475xmlHasNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
Daniel Veillarde95e2392001-06-06 10:46:28 +00004476 xmlAttrPtr prop;
4477 xmlDocPtr doc;
4478 xmlNsPtr ns;
4479
4480 if (node == NULL)
4481 return(NULL);
4482
4483 prop = node->properties;
Daniel Veillardca2366a2001-06-11 12:09:01 +00004484 if (nameSpace == NULL)
Daniel Veillarde95e2392001-06-06 10:46:28 +00004485 return(xmlHasProp(node, name));
4486 while (prop != NULL) {
4487 /*
4488 * One need to have
4489 * - same attribute names
4490 * - and the attribute carrying that namespace
4491 * or
4492 * no namespace on the attribute and the element carrying it
4493 */
4494 if ((xmlStrEqual(prop->name, name)) &&
Daniel Veillarde3c81b52001-06-17 14:50:34 +00004495 ((prop->ns != NULL) && (xmlStrEqual(prop->ns->href, nameSpace)))) {
4496 return(prop);
Daniel Veillarde95e2392001-06-06 10:46:28 +00004497 }
4498 prop = prop->next;
4499 }
4500 if (!xmlCheckDTD) return(NULL);
4501
4502 /*
4503 * Check if there is a default declaration in the internal
4504 * or external subsets
4505 */
4506 doc = node->doc;
4507 if (doc != NULL) {
4508 if (doc->intSubset != NULL) {
4509 xmlAttributePtr attrDecl;
4510
4511 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
4512 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4513 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
4514
4515 if ((attrDecl != NULL) && (attrDecl->prefix != NULL)) {
4516 /*
4517 * The DTD declaration only allows a prefix search
4518 */
4519 ns = xmlSearchNs(doc, node, attrDecl->prefix);
Daniel Veillardca2366a2001-06-11 12:09:01 +00004520 if ((ns != NULL) && (xmlStrEqual(ns->href, nameSpace)))
Daniel Veillarde95e2392001-06-06 10:46:28 +00004521 return((xmlAttrPtr) attrDecl);
4522 }
4523 }
4524 }
4525 return(NULL);
4526}
4527
4528/**
Owen Taylor3473f882001-02-23 17:55:21 +00004529 * xmlGetProp:
4530 * @node: the node
4531 * @name: the attribute name
4532 *
4533 * Search and get the value of an attribute associated to a node
4534 * This does the entity substitution.
4535 * This function looks in DTD attribute declaration for #FIXED or
4536 * default declaration values unless DTD use has been turned off.
4537 *
4538 * Returns the attribute value or NULL if not found.
4539 * It's up to the caller to free the memory.
4540 */
4541xmlChar *
4542xmlGetProp(xmlNodePtr node, const xmlChar *name) {
4543 xmlAttrPtr prop;
4544 xmlDocPtr doc;
4545
4546 if ((node == NULL) || (name == NULL)) return(NULL);
4547 /*
4548 * Check on the properties attached to the node
4549 */
4550 prop = node->properties;
4551 while (prop != NULL) {
4552 if (xmlStrEqual(prop->name, name)) {
4553 xmlChar *ret;
4554
4555 ret = xmlNodeListGetString(node->doc, prop->children, 1);
4556 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
4557 return(ret);
4558 }
4559 prop = prop->next;
4560 }
4561 if (!xmlCheckDTD) return(NULL);
4562
4563 /*
4564 * Check if there is a default declaration in the internal
4565 * or external subsets
4566 */
4567 doc = node->doc;
4568 if (doc != NULL) {
4569 xmlAttributePtr attrDecl;
4570 if (doc->intSubset != NULL) {
4571 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
4572 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4573 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
4574 if (attrDecl != NULL)
4575 return(xmlStrdup(attrDecl->defaultValue));
4576 }
4577 }
4578 return(NULL);
4579}
4580
4581/**
4582 * xmlGetNsProp:
4583 * @node: the node
4584 * @name: the attribute name
Daniel Veillardca2366a2001-06-11 12:09:01 +00004585 * @nameSpace: the URI of the namespace
Owen Taylor3473f882001-02-23 17:55:21 +00004586 *
4587 * Search and get the value of an attribute associated to a node
4588 * This attribute has to be anchored in the namespace specified.
4589 * This does the entity substitution.
4590 * This function looks in DTD attribute declaration for #FIXED or
4591 * default declaration values unless DTD use has been turned off.
4592 *
4593 * Returns the attribute value or NULL if not found.
4594 * It's up to the caller to free the memory.
4595 */
4596xmlChar *
Daniel Veillardca2366a2001-06-11 12:09:01 +00004597xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
Owen Taylor3473f882001-02-23 17:55:21 +00004598 xmlAttrPtr prop;
4599 xmlDocPtr doc;
4600 xmlNsPtr ns;
4601
4602 if (node == NULL)
4603 return(NULL);
4604
4605 prop = node->properties;
Daniel Veillardca2366a2001-06-11 12:09:01 +00004606 if (nameSpace == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00004607 return(xmlGetProp(node, name));
4608 while (prop != NULL) {
4609 /*
4610 * One need to have
4611 * - same attribute names
4612 * - and the attribute carrying that namespace
Owen Taylor3473f882001-02-23 17:55:21 +00004613 */
4614 if ((xmlStrEqual(prop->name, name)) &&
Daniel Veillarde8fc08e2001-06-07 19:35:47 +00004615 ((prop->ns != NULL) &&
Daniel Veillardca2366a2001-06-11 12:09:01 +00004616 (xmlStrEqual(prop->ns->href, nameSpace)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00004617 xmlChar *ret;
4618
4619 ret = xmlNodeListGetString(node->doc, prop->children, 1);
4620 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
4621 return(ret);
4622 }
4623 prop = prop->next;
4624 }
4625 if (!xmlCheckDTD) return(NULL);
4626
4627 /*
4628 * Check if there is a default declaration in the internal
4629 * or external subsets
4630 */
4631 doc = node->doc;
4632 if (doc != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004633 if (doc->intSubset != NULL) {
Daniel Veillard5792e162001-04-30 17:44:45 +00004634 xmlAttributePtr attrDecl;
4635
Owen Taylor3473f882001-02-23 17:55:21 +00004636 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
4637 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4638 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
4639
4640 if ((attrDecl != NULL) && (attrDecl->prefix != NULL)) {
4641 /*
4642 * The DTD declaration only allows a prefix search
4643 */
4644 ns = xmlSearchNs(doc, node, attrDecl->prefix);
Daniel Veillardca2366a2001-06-11 12:09:01 +00004645 if ((ns != NULL) && (xmlStrEqual(ns->href, nameSpace)))
Owen Taylor3473f882001-02-23 17:55:21 +00004646 return(xmlStrdup(attrDecl->defaultValue));
4647 }
4648 }
4649 }
4650 return(NULL);
4651}
4652
4653/**
4654 * xmlSetProp:
4655 * @node: the node
4656 * @name: the attribute name
4657 * @value: the attribute value
4658 *
4659 * Set (or reset) an attribute carried by a node.
4660 * Returns the attribute pointer.
4661 */
4662xmlAttrPtr
4663xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
Daniel Veillard4855c8c2001-11-25 10:35:25 +00004664 xmlAttrPtr prop;
4665 xmlDocPtr doc;
Owen Taylor3473f882001-02-23 17:55:21 +00004666
4667 if ((node == NULL) || (name == NULL))
4668 return(NULL);
4669 doc = node->doc;
Daniel Veillard4855c8c2001-11-25 10:35:25 +00004670 prop = node->properties;
Owen Taylor3473f882001-02-23 17:55:21 +00004671 while (prop != NULL) {
Daniel Veillard75bea542001-05-11 17:41:21 +00004672 if ((xmlStrEqual(prop->name, name)) &&
4673 (prop->ns == NULL)){
Daniel Veillard4855c8c2001-11-25 10:35:25 +00004674 xmlNodePtr oldprop = prop->children;
4675
Owen Taylor3473f882001-02-23 17:55:21 +00004676 prop->children = NULL;
4677 prop->last = NULL;
4678 if (value != NULL) {
4679 xmlChar *buffer;
4680 xmlNodePtr tmp;
4681
4682 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
4683 prop->children = xmlStringGetNodeList(node->doc, buffer);
4684 prop->last = NULL;
4685 prop->doc = doc;
4686 tmp = prop->children;
4687 while (tmp != NULL) {
4688 tmp->parent = (xmlNodePtr) prop;
4689 tmp->doc = doc;
4690 if (tmp->next == NULL)
4691 prop->last = tmp;
4692 tmp = tmp->next;
4693 }
4694 xmlFree(buffer);
Daniel Veillard75bea542001-05-11 17:41:21 +00004695 }
Daniel Veillard4855c8c2001-11-25 10:35:25 +00004696 if (oldprop != NULL)
4697 xmlFreeNodeList(oldprop);
Owen Taylor3473f882001-02-23 17:55:21 +00004698 return(prop);
4699 }
4700 prop = prop->next;
4701 }
4702 prop = xmlNewProp(node, name, value);
4703 return(prop);
4704}
4705
4706/**
Daniel Veillard75bea542001-05-11 17:41:21 +00004707 * xmlUnsetProp:
4708 * @node: the node
4709 * @name: the attribute name
4710 *
4711 * Remove an attribute carried by a node.
4712 * Returns 0 if successful, -1 if not found
4713 */
4714int
4715xmlUnsetProp(xmlNodePtr node, const xmlChar *name) {
4716 xmlAttrPtr prop = node->properties, prev = NULL;;
4717
4718 if ((node == NULL) || (name == NULL))
4719 return(-1);
4720 while (prop != NULL) {
4721 if ((xmlStrEqual(prop->name, name)) &&
4722 (prop->ns == NULL)) {
4723 if (prev == NULL)
4724 node->properties = prop->next;
4725 else
4726 prev->next = prop->next;
4727 xmlFreeProp(prop);
4728 return(0);
4729 }
4730 prev = prop;
4731 prop = prop->next;
4732 }
4733 return(-1);
4734}
4735
4736/**
Owen Taylor3473f882001-02-23 17:55:21 +00004737 * xmlSetNsProp:
4738 * @node: the node
4739 * @ns: the namespace definition
4740 * @name: the attribute name
4741 * @value: the attribute value
4742 *
4743 * Set (or reset) an attribute carried by a node.
4744 * The ns structure must be in scope, this is not checked.
4745 *
4746 * Returns the attribute pointer.
4747 */
4748xmlAttrPtr
4749xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
4750 const xmlChar *value) {
4751 xmlAttrPtr prop;
4752
4753 if ((node == NULL) || (name == NULL))
4754 return(NULL);
4755
4756 if (ns == NULL)
4757 return(xmlSetProp(node, name, value));
4758 if (ns->href == NULL)
4759 return(NULL);
4760 prop = node->properties;
4761
4762 while (prop != NULL) {
4763 /*
4764 * One need to have
4765 * - same attribute names
4766 * - and the attribute carrying that namespace
4767 * or
4768 * no namespace on the attribute and the element carrying it
4769 */
4770 if ((xmlStrEqual(prop->name, name)) &&
4771 (((prop->ns == NULL) && (node->ns != NULL) &&
4772 (xmlStrEqual(node->ns->href, ns->href))) ||
4773 ((prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))))) {
4774 if (prop->children != NULL)
4775 xmlFreeNodeList(prop->children);
4776 prop->children = NULL;
4777 prop->last = NULL;
4778 prop->ns = ns;
4779 if (value != NULL) {
4780 xmlChar *buffer;
4781 xmlNodePtr tmp;
4782
4783 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
4784 prop->children = xmlStringGetNodeList(node->doc, buffer);
4785 prop->last = NULL;
4786 tmp = prop->children;
4787 while (tmp != NULL) {
4788 tmp->parent = (xmlNodePtr) prop;
4789 if (tmp->next == NULL)
4790 prop->last = tmp;
4791 tmp = tmp->next;
4792 }
4793 xmlFree(buffer);
4794 }
4795 return(prop);
4796 }
4797 prop = prop->next;
4798 }
4799 prop = xmlNewNsProp(node, ns, name, value);
4800 return(prop);
4801}
4802
4803/**
Daniel Veillard75bea542001-05-11 17:41:21 +00004804 * xmlUnsetNsProp:
4805 * @node: the node
4806 * @ns: the namespace definition
4807 * @name: the attribute name
4808 *
4809 * Remove an attribute carried by a node.
4810 * Returns 0 if successful, -1 if not found
4811 */
4812int
4813xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) {
4814 xmlAttrPtr prop = node->properties, prev = NULL;;
4815
4816 if ((node == NULL) || (name == NULL))
4817 return(-1);
4818 if (ns == NULL)
4819 return(xmlUnsetProp(node, name));
4820 if (ns->href == NULL)
4821 return(-1);
4822 while (prop != NULL) {
4823 if ((xmlStrEqual(prop->name, name)) &&
4824 (((prop->ns == NULL) && (node->ns != NULL) &&
4825 (xmlStrEqual(node->ns->href, ns->href))) ||
4826 ((prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))))) {
4827 if (prev == NULL)
4828 node->properties = prop->next;
4829 else
4830 prev->next = prop->next;
4831 xmlFreeProp(prop);
4832 return(0);
4833 }
4834 prev = prop;
4835 prop = prop->next;
4836 }
4837 return(-1);
4838}
4839
4840/**
Owen Taylor3473f882001-02-23 17:55:21 +00004841 * xmlNodeIsText:
4842 * @node: the node
4843 *
4844 * Is this node a Text node ?
4845 * Returns 1 yes, 0 no
4846 */
4847int
4848xmlNodeIsText(xmlNodePtr node) {
4849 if (node == NULL) return(0);
4850
4851 if (node->type == XML_TEXT_NODE) return(1);
4852 return(0);
4853}
4854
4855/**
4856 * xmlIsBlankNode:
4857 * @node: the node
4858 *
4859 * Checks whether this node is an empty or whitespace only
4860 * (and possibly ignorable) text-node.
4861 *
4862 * Returns 1 yes, 0 no
4863 */
4864int
4865xmlIsBlankNode(xmlNodePtr node) {
4866 const xmlChar *cur;
4867 if (node == NULL) return(0);
4868
Daniel Veillard7db37732001-07-12 01:20:08 +00004869 if ((node->type != XML_TEXT_NODE) &&
4870 (node->type != XML_CDATA_SECTION_NODE))
4871 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004872 if (node->content == NULL) return(1);
4873#ifndef XML_USE_BUFFER_CONTENT
4874 cur = node->content;
4875#else
4876 cur = xmlBufferContent(node->content);
4877#endif
4878 while (*cur != 0) {
4879 if (!IS_BLANK(*cur)) return(0);
4880 cur++;
4881 }
4882
4883 return(1);
4884}
4885
4886/**
4887 * xmlTextConcat:
4888 * @node: the node
4889 * @content: the content
Daniel Veillard60087f32001-10-10 09:45:09 +00004890 * @len: @content length
Owen Taylor3473f882001-02-23 17:55:21 +00004891 *
4892 * Concat the given string at the end of the existing node content
4893 */
4894
4895void
4896xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) {
4897 if (node == NULL) return;
4898
4899 if ((node->type != XML_TEXT_NODE) &&
4900 (node->type != XML_CDATA_SECTION_NODE)) {
4901#ifdef DEBUG_TREE
4902 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00004903 "xmlTextConcat: node is not text nor CDATA\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004904#endif
4905 return;
4906 }
4907#ifndef XML_USE_BUFFER_CONTENT
4908 node->content = xmlStrncat(node->content, content, len);
4909#else
4910 xmlBufferAdd(node->content, content, len);
4911#endif
4912}
4913
4914/************************************************************************
4915 * *
4916 * Output : to a FILE or in memory *
4917 * *
4918 ************************************************************************/
4919
Owen Taylor3473f882001-02-23 17:55:21 +00004920/**
4921 * xmlBufferCreate:
4922 *
4923 * routine to create an XML buffer.
4924 * returns the new structure.
4925 */
4926xmlBufferPtr
4927xmlBufferCreate(void) {
4928 xmlBufferPtr ret;
4929
4930 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
4931 if (ret == NULL) {
4932 xmlGenericError(xmlGenericErrorContext,
4933 "xmlBufferCreate : out of memory!\n");
4934 return(NULL);
4935 }
4936 ret->use = 0;
Daniel Veillarde356c282001-03-10 12:32:04 +00004937 ret->size = xmlDefaultBufferSize;
Owen Taylor3473f882001-02-23 17:55:21 +00004938 ret->alloc = xmlBufferAllocScheme;
4939 ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar));
4940 if (ret->content == NULL) {
4941 xmlGenericError(xmlGenericErrorContext,
4942 "xmlBufferCreate : out of memory!\n");
4943 xmlFree(ret);
4944 return(NULL);
4945 }
4946 ret->content[0] = 0;
4947 return(ret);
4948}
4949
4950/**
4951 * xmlBufferCreateSize:
4952 * @size: initial size of buffer
4953 *
4954 * routine to create an XML buffer.
4955 * returns the new structure.
4956 */
4957xmlBufferPtr
4958xmlBufferCreateSize(size_t size) {
4959 xmlBufferPtr ret;
4960
4961 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
4962 if (ret == NULL) {
4963 xmlGenericError(xmlGenericErrorContext,
4964 "xmlBufferCreate : out of memory!\n");
4965 return(NULL);
4966 }
4967 ret->use = 0;
4968 ret->alloc = xmlBufferAllocScheme;
4969 ret->size = (size ? size+2 : 0); /* +1 for ending null */
4970 if (ret->size){
4971 ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar));
4972 if (ret->content == NULL) {
4973 xmlGenericError(xmlGenericErrorContext,
4974 "xmlBufferCreate : out of memory!\n");
4975 xmlFree(ret);
4976 return(NULL);
4977 }
4978 ret->content[0] = 0;
4979 } else
4980 ret->content = NULL;
4981 return(ret);
4982}
4983
4984/**
4985 * xmlBufferSetAllocationScheme:
4986 * @buf: the buffer to free
4987 * @scheme: allocation scheme to use
4988 *
4989 * Sets the allocation scheme for this buffer
4990 */
4991void
4992xmlBufferSetAllocationScheme(xmlBufferPtr buf,
4993 xmlBufferAllocationScheme scheme) {
4994 if (buf == NULL) {
4995#ifdef DEBUG_BUFFER
4996 xmlGenericError(xmlGenericErrorContext,
4997 "xmlBufferSetAllocationScheme: buf == NULL\n");
4998#endif
4999 return;
5000 }
5001
5002 buf->alloc = scheme;
5003}
5004
5005/**
5006 * xmlBufferFree:
5007 * @buf: the buffer to free
5008 *
5009 * Frees an XML buffer.
5010 */
5011void
5012xmlBufferFree(xmlBufferPtr buf) {
5013 if (buf == NULL) {
5014#ifdef DEBUG_BUFFER
5015 xmlGenericError(xmlGenericErrorContext,
5016 "xmlBufferFree: buf == NULL\n");
5017#endif
5018 return;
5019 }
5020 if (buf->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005021 xmlFree(buf->content);
5022 }
Owen Taylor3473f882001-02-23 17:55:21 +00005023 xmlFree(buf);
5024}
5025
5026/**
5027 * xmlBufferEmpty:
5028 * @buf: the buffer
5029 *
5030 * empty a buffer.
5031 */
5032void
5033xmlBufferEmpty(xmlBufferPtr buf) {
5034 if (buf->content == NULL) return;
5035 buf->use = 0;
Daniel Veillard92ad2102001-03-27 12:47:33 +00005036 memset(buf->content, 0, buf->size);
Owen Taylor3473f882001-02-23 17:55:21 +00005037}
5038
5039/**
5040 * xmlBufferShrink:
5041 * @buf: the buffer to dump
5042 * @len: the number of xmlChar to remove
5043 *
5044 * Remove the beginning of an XML buffer.
5045 *
Daniel Veillardd1640922001-12-17 15:30:10 +00005046 * Returns the number of #xmlChar removed, or -1 in case of failure.
Owen Taylor3473f882001-02-23 17:55:21 +00005047 */
5048int
5049xmlBufferShrink(xmlBufferPtr buf, unsigned int len) {
5050 if (len == 0) return(0);
5051 if (len > buf->use) return(-1);
5052
5053 buf->use -= len;
5054 memmove(buf->content, &buf->content[len], buf->use * sizeof(xmlChar));
5055
5056 buf->content[buf->use] = 0;
5057 return(len);
5058}
5059
5060/**
5061 * xmlBufferGrow:
5062 * @buf: the buffer
5063 * @len: the minimum free size to allocate
5064 *
5065 * Grow the available space of an XML buffer.
5066 *
5067 * Returns the new available space or -1 in case of error
5068 */
5069int
5070xmlBufferGrow(xmlBufferPtr buf, unsigned int len) {
5071 int size;
5072 xmlChar *newbuf;
5073
5074 if (len + buf->use < buf->size) return(0);
5075
5076 size = buf->use + len + 100;
5077
5078 newbuf = (xmlChar *) xmlRealloc(buf->content, size);
5079 if (newbuf == NULL) return(-1);
5080 buf->content = newbuf;
5081 buf->size = size;
5082 return(buf->size - buf->use);
5083}
5084
5085/**
5086 * xmlBufferDump:
5087 * @file: the file output
5088 * @buf: the buffer to dump
5089 *
5090 * Dumps an XML buffer to a FILE *.
Daniel Veillardd1640922001-12-17 15:30:10 +00005091 * Returns the number of #xmlChar written
Owen Taylor3473f882001-02-23 17:55:21 +00005092 */
5093int
5094xmlBufferDump(FILE *file, xmlBufferPtr buf) {
5095 int ret;
5096
5097 if (buf == NULL) {
5098#ifdef DEBUG_BUFFER
5099 xmlGenericError(xmlGenericErrorContext,
5100 "xmlBufferDump: buf == NULL\n");
5101#endif
5102 return(0);
5103 }
5104 if (buf->content == NULL) {
5105#ifdef DEBUG_BUFFER
5106 xmlGenericError(xmlGenericErrorContext,
5107 "xmlBufferDump: buf->content == NULL\n");
5108#endif
5109 return(0);
5110 }
Daniel Veillardcd337f02001-11-22 18:20:37 +00005111 if (file == NULL)
5112 file = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00005113 ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
5114 return(ret);
5115}
5116
5117/**
5118 * xmlBufferContent:
5119 * @buf: the buffer
5120 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00005121 * Function to extract the content of a buffer
5122 *
Owen Taylor3473f882001-02-23 17:55:21 +00005123 * Returns the internal content
5124 */
5125
Daniel Veillard5e2dace2001-07-18 19:30:27 +00005126const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00005127xmlBufferContent(const xmlBufferPtr buf)
5128{
5129 if(!buf)
5130 return NULL;
5131
5132 return buf->content;
5133}
5134
5135/**
5136 * xmlBufferLength:
5137 * @buf: the buffer
5138 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00005139 * Function to get the length of a buffer
5140 *
Owen Taylor3473f882001-02-23 17:55:21 +00005141 * Returns the length of data in the internal content
5142 */
5143
5144int
5145xmlBufferLength(const xmlBufferPtr buf)
5146{
5147 if(!buf)
5148 return 0;
5149
5150 return buf->use;
5151}
5152
5153/**
5154 * xmlBufferResize:
5155 * @buf: the buffer to resize
5156 * @size: the desired size
5157 *
Daniel Veillardd1640922001-12-17 15:30:10 +00005158 * Resize a buffer to accommodate minimum size of @size.
Owen Taylor3473f882001-02-23 17:55:21 +00005159 *
5160 * Returns 0 in case of problems, 1 otherwise
5161 */
5162int
5163xmlBufferResize(xmlBufferPtr buf, unsigned int size)
5164{
5165 unsigned int newSize;
5166 xmlChar* rebuf = NULL;
5167
5168 /*take care of empty case*/
5169 newSize = (buf->size ? buf->size*2 : size);
5170
5171 /* Don't resize if we don't have to */
5172 if (size < buf->size)
5173 return 1;
5174
5175 /* figure out new size */
5176 switch (buf->alloc){
5177 case XML_BUFFER_ALLOC_DOUBLEIT:
5178 while (size > newSize) newSize *= 2;
5179 break;
5180 case XML_BUFFER_ALLOC_EXACT:
5181 newSize = size+10;
5182 break;
5183 default:
5184 newSize = size+10;
5185 break;
5186 }
5187
5188 if (buf->content == NULL)
5189 rebuf = (xmlChar *) xmlMalloc(newSize * sizeof(xmlChar));
5190 else
5191 rebuf = (xmlChar *) xmlRealloc(buf->content,
5192 newSize * sizeof(xmlChar));
5193 if (rebuf == NULL) {
5194 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00005195 "xmlBufferResize : out of memory!\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005196 return 0;
5197 }
5198 buf->content = rebuf;
5199 buf->size = newSize;
5200
5201 return 1;
5202}
5203
5204/**
5205 * xmlBufferAdd:
5206 * @buf: the buffer to dump
Daniel Veillardd1640922001-12-17 15:30:10 +00005207 * @str: the #xmlChar string
5208 * @len: the number of #xmlChar to add
Owen Taylor3473f882001-02-23 17:55:21 +00005209 *
Daniel Veillard60087f32001-10-10 09:45:09 +00005210 * Add a string range to an XML buffer. if len == -1, the length of
Owen Taylor3473f882001-02-23 17:55:21 +00005211 * str is recomputed.
5212 */
5213void
5214xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) {
5215 unsigned int needSize;
5216
5217 if (str == NULL) {
5218#ifdef DEBUG_BUFFER
5219 xmlGenericError(xmlGenericErrorContext,
5220 "xmlBufferAdd: str == NULL\n");
5221#endif
5222 return;
5223 }
5224 if (len < -1) {
5225#ifdef DEBUG_BUFFER
5226 xmlGenericError(xmlGenericErrorContext,
5227 "xmlBufferAdd: len < 0\n");
5228#endif
5229 return;
5230 }
5231 if (len == 0) return;
5232
5233 if (len < 0)
5234 len = xmlStrlen(str);
5235
5236 if (len <= 0) return;
5237
5238 needSize = buf->use + len + 2;
5239 if (needSize > buf->size){
5240 if (!xmlBufferResize(buf, needSize)){
5241 xmlGenericError(xmlGenericErrorContext,
5242 "xmlBufferAdd : out of memory!\n");
5243 return;
5244 }
5245 }
5246
5247 memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
5248 buf->use += len;
5249 buf->content[buf->use] = 0;
5250}
5251
5252/**
5253 * xmlBufferAddHead:
5254 * @buf: the buffer
Daniel Veillardd1640922001-12-17 15:30:10 +00005255 * @str: the #xmlChar string
5256 * @len: the number of #xmlChar to add
Owen Taylor3473f882001-02-23 17:55:21 +00005257 *
5258 * Add a string range to the beginning of an XML buffer.
Daniel Veillard60087f32001-10-10 09:45:09 +00005259 * if len == -1, the length of @str is recomputed.
Owen Taylor3473f882001-02-23 17:55:21 +00005260 */
5261void
5262xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) {
5263 unsigned int needSize;
5264
5265 if (str == NULL) {
5266#ifdef DEBUG_BUFFER
5267 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00005268 "xmlBufferAddHead: str == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005269#endif
5270 return;
5271 }
5272 if (len < -1) {
5273#ifdef DEBUG_BUFFER
5274 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00005275 "xmlBufferAddHead: len < 0\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005276#endif
5277 return;
5278 }
5279 if (len == 0) return;
5280
5281 if (len < 0)
5282 len = xmlStrlen(str);
5283
5284 if (len <= 0) return;
5285
5286 needSize = buf->use + len + 2;
5287 if (needSize > buf->size){
5288 if (!xmlBufferResize(buf, needSize)){
5289 xmlGenericError(xmlGenericErrorContext,
5290 "xmlBufferAddHead : out of memory!\n");
5291 return;
5292 }
5293 }
5294
5295 memmove(&buf->content[len], &buf->content[0], buf->use * sizeof(xmlChar));
5296 memmove(&buf->content[0], str, len * sizeof(xmlChar));
5297 buf->use += len;
5298 buf->content[buf->use] = 0;
5299}
5300
5301/**
5302 * xmlBufferCat:
5303 * @buf: the buffer to dump
Daniel Veillardd1640922001-12-17 15:30:10 +00005304 * @str: the #xmlChar string
Owen Taylor3473f882001-02-23 17:55:21 +00005305 *
5306 * Append a zero terminated string to an XML buffer.
5307 */
5308void
5309xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) {
5310 if (str != NULL)
5311 xmlBufferAdd(buf, str, -1);
5312}
5313
5314/**
5315 * xmlBufferCCat:
5316 * @buf: the buffer to dump
5317 * @str: the C char string
5318 *
5319 * Append a zero terminated C string to an XML buffer.
5320 */
5321void
5322xmlBufferCCat(xmlBufferPtr buf, const char *str) {
5323 const char *cur;
5324
5325 if (str == NULL) {
5326#ifdef DEBUG_BUFFER
5327 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00005328 "xmlBufferCCat: str == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005329#endif
5330 return;
5331 }
5332 for (cur = str;*cur != 0;cur++) {
5333 if (buf->use + 10 >= buf->size) {
5334 if (!xmlBufferResize(buf, buf->use+10)){
5335 xmlGenericError(xmlGenericErrorContext,
5336 "xmlBufferCCat : out of memory!\n");
5337 return;
5338 }
5339 }
5340 buf->content[buf->use++] = *cur;
5341 }
5342 buf->content[buf->use] = 0;
5343}
5344
5345/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +00005346 * xmlBufferWriteXmlCHAR:
5347 * @buf: the XML buffer
5348 * @string: the string to add
5349 *
5350 * For VMS only.
5351 * routine which manages and grows an output buffer. This one adds
5352 * xmlChars at the end of the buffer.
5353 */
5354/**
Owen Taylor3473f882001-02-23 17:55:21 +00005355 * xmlBufferWriteCHAR:
5356 * @buf: the XML buffer
5357 * @string: the string to add
5358 *
5359 * routine which manages and grows an output buffer. This one adds
5360 * xmlChars at the end of the buffer.
5361 */
5362void
5363#ifdef VMS
5364xmlBufferWriteXmlCHAR
5365#else
5366xmlBufferWriteCHAR
5367#endif
5368(xmlBufferPtr buf, const xmlChar *string) {
5369 xmlBufferCat(buf, string);
5370}
5371
5372/**
5373 * xmlBufferWriteChar:
5374 * @buf: the XML buffer output
5375 * @string: the string to add
5376 *
5377 * routine which manage and grows an output buffer. This one add
5378 * C chars at the end of the array.
5379 */
5380void
5381xmlBufferWriteChar(xmlBufferPtr buf, const char *string) {
5382 xmlBufferCCat(buf, string);
5383}
5384
5385
5386/**
5387 * xmlBufferWriteQuotedString:
5388 * @buf: the XML buffer output
5389 * @string: the string to add
5390 *
5391 * routine which manage and grows an output buffer. This one writes
Daniel Veillardd1640922001-12-17 15:30:10 +00005392 * a quoted or double quoted #xmlChar string, checking first if it holds
Owen Taylor3473f882001-02-23 17:55:21 +00005393 * quote or double-quotes internally
5394 */
5395void
5396xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) {
5397 if (xmlStrchr(string, '"')) {
5398 if (xmlStrchr(string, '\'')) {
5399#ifdef DEBUG_BUFFER
5400 xmlGenericError(xmlGenericErrorContext,
5401 "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n");
5402#endif
5403 }
5404 xmlBufferCCat(buf, "'");
5405 xmlBufferCat(buf, string);
5406 xmlBufferCCat(buf, "'");
5407 } else {
5408 xmlBufferCCat(buf, "\"");
5409 xmlBufferCat(buf, string);
5410 xmlBufferCCat(buf, "\"");
5411 }
5412}
5413
5414
5415/************************************************************************
5416 * *
5417 * Dumping XML tree content to a simple buffer *
5418 * *
5419 ************************************************************************/
5420
5421void
5422xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
5423 int format);
5424static void
5425xmlNodeListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
5426 int format);
5427void
5428htmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur);
5429
5430/**
5431 * xmlNsDump:
5432 * @buf: the XML buffer output
5433 * @cur: a namespace
5434 *
5435 * Dump a local Namespace definition.
5436 * Should be called in the context of attributes dumps.
5437 */
5438static void
5439xmlNsDump(xmlBufferPtr buf, xmlNsPtr cur) {
5440 if (cur == NULL) {
5441#ifdef DEBUG_TREE
5442 xmlGenericError(xmlGenericErrorContext,
5443 "xmlNsDump : Ns == NULL\n");
5444#endif
5445 return;
5446 }
5447 if (cur->type == XML_LOCAL_NAMESPACE) {
5448 /* Within the context of an element attributes */
5449 if (cur->prefix != NULL) {
5450 xmlBufferWriteChar(buf, " xmlns:");
5451 xmlBufferWriteCHAR(buf, cur->prefix);
5452 } else
5453 xmlBufferWriteChar(buf, " xmlns");
5454 xmlBufferWriteChar(buf, "=");
5455 xmlBufferWriteQuotedString(buf, cur->href);
5456 }
5457}
5458
5459/**
5460 * xmlNsListDump:
5461 * @buf: the XML buffer output
5462 * @cur: the first namespace
5463 *
5464 * Dump a list of local Namespace definitions.
5465 * Should be called in the context of attributes dumps.
5466 */
5467static void
5468xmlNsListDump(xmlBufferPtr buf, xmlNsPtr cur) {
5469 while (cur != NULL) {
5470 xmlNsDump(buf, cur);
5471 cur = cur->next;
5472 }
5473}
5474
5475/**
5476 * xmlDtdDump:
5477 * @buf: the XML buffer output
Daniel Veillardd1640922001-12-17 15:30:10 +00005478 * @dtd: the DTD
Owen Taylor3473f882001-02-23 17:55:21 +00005479 *
5480 * Dump the XML document DTD, if any.
5481 */
5482static void
5483xmlDtdDump(xmlBufferPtr buf, xmlDtdPtr dtd) {
5484 if (dtd == NULL) {
5485#ifdef DEBUG_TREE
5486 xmlGenericError(xmlGenericErrorContext,
5487 "xmlDtdDump : no internal subset\n");
5488#endif
5489 return;
5490 }
5491 xmlBufferWriteChar(buf, "<!DOCTYPE ");
5492 xmlBufferWriteCHAR(buf, dtd->name);
5493 if (dtd->ExternalID != NULL) {
5494 xmlBufferWriteChar(buf, " PUBLIC ");
5495 xmlBufferWriteQuotedString(buf, dtd->ExternalID);
5496 xmlBufferWriteChar(buf, " ");
5497 xmlBufferWriteQuotedString(buf, dtd->SystemID);
5498 } else if (dtd->SystemID != NULL) {
5499 xmlBufferWriteChar(buf, " SYSTEM ");
5500 xmlBufferWriteQuotedString(buf, dtd->SystemID);
5501 }
5502 if ((dtd->entities == NULL) && (dtd->elements == NULL) &&
5503 (dtd->attributes == NULL) && (dtd->notations == NULL)) {
5504 xmlBufferWriteChar(buf, ">");
5505 return;
5506 }
5507 xmlBufferWriteChar(buf, " [\n");
5508 xmlNodeListDump(buf, dtd->doc, dtd->children, -1, 0);
5509#if 0
5510 if (dtd->entities != NULL)
5511 xmlDumpEntitiesTable(buf, (xmlEntitiesTablePtr) dtd->entities);
5512 if (dtd->notations != NULL)
5513 xmlDumpNotationTable(buf, (xmlNotationTablePtr) dtd->notations);
5514 if (dtd->elements != NULL)
5515 xmlDumpElementTable(buf, (xmlElementTablePtr) dtd->elements);
5516 if (dtd->attributes != NULL)
5517 xmlDumpAttributeTable(buf, (xmlAttributeTablePtr) dtd->attributes);
5518#endif
5519 xmlBufferWriteChar(buf, "]>");
5520}
5521
5522/**
5523 * xmlAttrDump:
5524 * @buf: the XML buffer output
5525 * @doc: the document
5526 * @cur: the attribute pointer
5527 *
5528 * Dump an XML attribute
5529 */
5530static void
5531xmlAttrDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) {
5532 xmlChar *value;
5533
5534 if (cur == NULL) {
5535#ifdef DEBUG_TREE
5536 xmlGenericError(xmlGenericErrorContext,
5537 "xmlAttrDump : property == NULL\n");
5538#endif
5539 return;
5540 }
5541 xmlBufferWriteChar(buf, " ");
5542 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
5543 xmlBufferWriteCHAR(buf, cur->ns->prefix);
5544 xmlBufferWriteChar(buf, ":");
5545 }
5546 xmlBufferWriteCHAR(buf, cur->name);
5547 value = xmlNodeListGetString(doc, cur->children, 0);
5548 if (value != NULL) {
5549 xmlBufferWriteChar(buf, "=");
5550 xmlBufferWriteQuotedString(buf, value);
5551 xmlFree(value);
5552 } else {
5553 xmlBufferWriteChar(buf, "=\"\"");
5554 }
5555}
5556
5557/**
5558 * xmlAttrListDump:
5559 * @buf: the XML buffer output
5560 * @doc: the document
5561 * @cur: the first attribute pointer
5562 *
5563 * Dump a list of XML attributes
5564 */
5565static void
5566xmlAttrListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) {
5567 if (cur == NULL) {
5568#ifdef DEBUG_TREE
5569 xmlGenericError(xmlGenericErrorContext,
5570 "xmlAttrListDump : property == NULL\n");
5571#endif
5572 return;
5573 }
5574 while (cur != NULL) {
5575 xmlAttrDump(buf, doc, cur);
5576 cur = cur->next;
5577 }
5578}
5579
5580
5581
5582/**
5583 * xmlNodeListDump:
5584 * @buf: the XML buffer output
5585 * @doc: the document
5586 * @cur: the first node
5587 * @level: the imbrication level for indenting
5588 * @format: is formatting allowed
5589 *
5590 * Dump an XML node list, recursive behaviour,children are printed too.
5591 */
5592static void
5593xmlNodeListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
5594 int format) {
5595 int i;
5596
5597 if (cur == NULL) {
5598#ifdef DEBUG_TREE
5599 xmlGenericError(xmlGenericErrorContext,
5600 "xmlNodeListDump : node == NULL\n");
5601#endif
5602 return;
5603 }
5604 while (cur != NULL) {
5605 if ((format) && (xmlIndentTreeOutput) &&
5606 (cur->type == XML_ELEMENT_NODE))
5607 for (i = 0;i < level;i++)
5608 xmlBufferWriteChar(buf, " ");
5609 xmlNodeDump(buf, doc, cur, level, format);
5610 if (format) {
5611 xmlBufferWriteChar(buf, "\n");
5612 }
5613 cur = cur->next;
5614 }
5615}
5616
5617/**
5618 * xmlNodeDump:
5619 * @buf: the XML buffer output
5620 * @doc: the document
5621 * @cur: the current node
5622 * @level: the imbrication level for indenting
5623 * @format: is formatting allowed
5624 *
5625 * Dump an XML node, recursive behaviour,children are printed too.
5626 */
5627void
5628xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
5629 int format) {
5630 int i;
5631 xmlNodePtr tmp;
5632
5633 if (cur == NULL) {
5634#ifdef DEBUG_TREE
5635 xmlGenericError(xmlGenericErrorContext,
5636 "xmlNodeDump : node == NULL\n");
5637#endif
5638 return;
5639 }
5640 if (cur->type == XML_XINCLUDE_START)
5641 return;
5642 if (cur->type == XML_XINCLUDE_END)
5643 return;
5644 if (cur->type == XML_DTD_NODE) {
5645 xmlDtdDump(buf, (xmlDtdPtr) cur);
5646 return;
5647 }
5648 if (cur->type == XML_ELEMENT_DECL) {
5649 xmlDumpElementDecl(buf, (xmlElementPtr) cur);
5650 return;
5651 }
Daniel Veillard78d12092001-10-11 09:12:24 +00005652 if (cur->type == XML_ATTRIBUTE_NODE){
5653 xmlAttrDump(buf, doc, (xmlAttrPtr)cur);
5654 return;
5655 }
Owen Taylor3473f882001-02-23 17:55:21 +00005656 if (cur->type == XML_ATTRIBUTE_DECL) {
5657 xmlDumpAttributeDecl(buf, (xmlAttributePtr) cur);
5658 return;
5659 }
5660 if (cur->type == XML_ENTITY_DECL) {
5661 xmlDumpEntityDecl(buf, (xmlEntityPtr) cur);
5662 return;
5663 }
5664 if (cur->type == XML_TEXT_NODE) {
5665 if (cur->content != NULL) {
5666 if ((cur->name == xmlStringText) ||
5667 (cur->name != xmlStringTextNoenc)) {
5668 xmlChar *buffer;
5669
5670#ifndef XML_USE_BUFFER_CONTENT
5671 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
5672#else
5673 buffer = xmlEncodeEntitiesReentrant(doc,
5674 xmlBufferContent(cur->content));
5675#endif
5676 if (buffer != NULL) {
5677 xmlBufferWriteCHAR(buf, buffer);
5678 xmlFree(buffer);
5679 }
5680 } else {
5681 /*
5682 * Disable escaping, needed for XSLT
5683 */
5684#ifndef XML_USE_BUFFER_CONTENT
5685 xmlBufferWriteCHAR(buf, cur->content);
5686#else
5687 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
5688#endif
5689 }
5690 }
5691 return;
5692 }
5693 if (cur->type == XML_PI_NODE) {
5694 if (cur->content != NULL) {
5695 xmlBufferWriteChar(buf, "<?");
5696 xmlBufferWriteCHAR(buf, cur->name);
5697 if (cur->content != NULL) {
5698 xmlBufferWriteChar(buf, " ");
5699#ifndef XML_USE_BUFFER_CONTENT
5700 xmlBufferWriteCHAR(buf, cur->content);
5701#else
5702 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
5703#endif
5704 }
5705 xmlBufferWriteChar(buf, "?>");
5706 } else {
5707 xmlBufferWriteChar(buf, "<?");
5708 xmlBufferWriteCHAR(buf, cur->name);
5709 xmlBufferWriteChar(buf, "?>");
5710 }
5711 return;
5712 }
5713 if (cur->type == XML_COMMENT_NODE) {
5714 if (cur->content != NULL) {
5715 xmlBufferWriteChar(buf, "<!--");
5716#ifndef XML_USE_BUFFER_CONTENT
5717 xmlBufferWriteCHAR(buf, cur->content);
5718#else
5719 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
5720#endif
5721 xmlBufferWriteChar(buf, "-->");
5722 }
5723 return;
5724 }
5725 if (cur->type == XML_ENTITY_REF_NODE) {
5726 xmlBufferWriteChar(buf, "&");
5727 xmlBufferWriteCHAR(buf, cur->name);
5728 xmlBufferWriteChar(buf, ";");
5729 return;
5730 }
5731 if (cur->type == XML_CDATA_SECTION_NODE) {
5732 xmlBufferWriteChar(buf, "<![CDATA[");
5733 if (cur->content != NULL)
5734#ifndef XML_USE_BUFFER_CONTENT
5735 xmlBufferWriteCHAR(buf, cur->content);
5736#else
5737 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
5738#endif
5739 xmlBufferWriteChar(buf, "]]>");
5740 return;
5741 }
5742
5743 if (format == 1) {
5744 tmp = cur->children;
5745 while (tmp != NULL) {
5746 if ((tmp->type == XML_TEXT_NODE) ||
5747 (tmp->type == XML_ENTITY_REF_NODE)) {
5748 format = 0;
5749 break;
5750 }
5751 tmp = tmp->next;
5752 }
5753 }
5754 xmlBufferWriteChar(buf, "<");
5755 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
5756 xmlBufferWriteCHAR(buf, cur->ns->prefix);
5757 xmlBufferWriteChar(buf, ":");
5758 }
5759
5760 xmlBufferWriteCHAR(buf, cur->name);
5761 if (cur->nsDef)
5762 xmlNsListDump(buf, cur->nsDef);
5763 if (cur->properties != NULL)
5764 xmlAttrListDump(buf, doc, cur->properties);
5765
Daniel Veillard7db37732001-07-12 01:20:08 +00005766 if (((cur->type == XML_ELEMENT_NODE) || (cur->content == NULL)) &&
5767 (cur->children == NULL) &&
Owen Taylor3473f882001-02-23 17:55:21 +00005768 (!xmlSaveNoEmptyTags)) {
5769 xmlBufferWriteChar(buf, "/>");
5770 return;
5771 }
5772 xmlBufferWriteChar(buf, ">");
Daniel Veillard7db37732001-07-12 01:20:08 +00005773 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005774 xmlChar *buffer;
5775
5776#ifndef XML_USE_BUFFER_CONTENT
5777 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
5778#else
5779 buffer = xmlEncodeEntitiesReentrant(doc,
5780 xmlBufferContent(cur->content));
5781#endif
5782 if (buffer != NULL) {
5783 xmlBufferWriteCHAR(buf, buffer);
5784 xmlFree(buffer);
5785 }
5786 }
5787 if (cur->children != NULL) {
5788 if (format) xmlBufferWriteChar(buf, "\n");
5789 xmlNodeListDump(buf, doc, cur->children,
5790 (level >= 0?level+1:-1), format);
5791 if ((xmlIndentTreeOutput) && (format))
5792 for (i = 0;i < level;i++)
5793 xmlBufferWriteChar(buf, " ");
5794 }
5795 xmlBufferWriteChar(buf, "</");
5796 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
5797 xmlBufferWriteCHAR(buf, cur->ns->prefix);
5798 xmlBufferWriteChar(buf, ":");
5799 }
5800
5801 xmlBufferWriteCHAR(buf, cur->name);
5802 xmlBufferWriteChar(buf, ">");
5803}
5804
5805/**
5806 * xmlElemDump:
5807 * @f: the FILE * for the output
5808 * @doc: the document
5809 * @cur: the current node
5810 *
Daniel Veillardd1640922001-12-17 15:30:10 +00005811 * Dump an XML/HTML node, recursive behaviour, children are printed too.
Owen Taylor3473f882001-02-23 17:55:21 +00005812 */
5813void
5814xmlElemDump(FILE *f, xmlDocPtr doc, xmlNodePtr cur) {
5815 xmlBufferPtr buf;
5816
5817 if (cur == NULL) {
5818#ifdef DEBUG_TREE
5819 xmlGenericError(xmlGenericErrorContext,
5820 "xmlElemDump : cur == NULL\n");
5821#endif
5822 return;
5823 }
Owen Taylor3473f882001-02-23 17:55:21 +00005824#ifdef DEBUG_TREE
Daniel Veillardd79bcd12001-06-21 22:07:42 +00005825 if (doc == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005826 xmlGenericError(xmlGenericErrorContext,
5827 "xmlElemDump : doc == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005828 }
Daniel Veillardd79bcd12001-06-21 22:07:42 +00005829#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00005830
Owen Taylor3473f882001-02-23 17:55:21 +00005831 buf = xmlBufferCreate();
5832 if (buf == NULL) return;
5833 if ((doc != NULL) &&
5834 (doc->type == XML_HTML_DOCUMENT_NODE)) {
5835#ifdef LIBXML_HTML_ENABLED
5836 htmlNodeDump(buf, doc, cur);
5837#else
5838 xmlGenericError(xmlGenericErrorContext,
5839 "HTML support not compiled in\n");
5840#endif /* LIBXML_HTML_ENABLED */
5841 } else
5842 xmlNodeDump(buf, doc, cur, 0, 1);
5843 xmlBufferDump(f, buf);
5844 xmlBufferFree(buf);
5845}
5846
5847/************************************************************************
5848 * *
5849 * Dumping XML tree content to an I/O output buffer *
5850 * *
5851 ************************************************************************/
5852
5853void
5854xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
5855 int level, int format, const char *encoding);
5856static void
5857xmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
5858 int level, int format, const char *encoding);
5859/**
5860 * xmlNsDumpOutput:
5861 * @buf: the XML buffer output
5862 * @cur: a namespace
5863 *
5864 * Dump a local Namespace definition.
5865 * Should be called in the context of attributes dumps.
5866 */
5867static void
5868xmlNsDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) {
5869 if (cur == NULL) {
5870#ifdef DEBUG_TREE
5871 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00005872 "xmlNsDumpOutput : Ns == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005873#endif
5874 return;
5875 }
5876 if ((cur->type == XML_LOCAL_NAMESPACE) && (cur->href != NULL)) {
5877 /* Within the context of an element attributes */
5878 if (cur->prefix != NULL) {
5879 xmlOutputBufferWriteString(buf, " xmlns:");
5880 xmlOutputBufferWriteString(buf, (const char *)cur->prefix);
5881 } else
5882 xmlOutputBufferWriteString(buf, " xmlns");
5883 xmlOutputBufferWriteString(buf, "=");
5884 xmlBufferWriteQuotedString(buf->buffer, cur->href);
5885 }
5886}
5887
5888/**
5889 * xmlNsListDumpOutput:
5890 * @buf: the XML buffer output
5891 * @cur: the first namespace
5892 *
5893 * Dump a list of local Namespace definitions.
5894 * Should be called in the context of attributes dumps.
5895 */
5896static void
5897xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) {
5898 while (cur != NULL) {
5899 xmlNsDumpOutput(buf, cur);
5900 cur = cur->next;
5901 }
5902}
5903
5904/**
5905 * xmlDtdDumpOutput:
5906 * @buf: the XML buffer output
5907 * @doc: the document
5908 * @encoding: an optional encoding string
5909 *
5910 * Dump the XML document DTD, if any.
5911 */
5912static void
5913xmlDtdDumpOutput(xmlOutputBufferPtr buf, xmlDtdPtr dtd, const char *encoding) {
5914 if (dtd == NULL) {
5915#ifdef DEBUG_TREE
5916 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00005917 "xmlDtdDumpOutput : no internal subset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005918#endif
5919 return;
5920 }
5921 xmlOutputBufferWriteString(buf, "<!DOCTYPE ");
5922 xmlOutputBufferWriteString(buf, (const char *)dtd->name);
5923 if (dtd->ExternalID != NULL) {
5924 xmlOutputBufferWriteString(buf, " PUBLIC ");
5925 xmlBufferWriteQuotedString(buf->buffer, dtd->ExternalID);
5926 xmlOutputBufferWriteString(buf, " ");
5927 xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID);
5928 } else if (dtd->SystemID != NULL) {
5929 xmlOutputBufferWriteString(buf, " SYSTEM ");
5930 xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID);
5931 }
5932 if ((dtd->entities == NULL) && (dtd->elements == NULL) &&
5933 (dtd->attributes == NULL) && (dtd->notations == NULL)) {
5934 xmlOutputBufferWriteString(buf, ">");
5935 return;
5936 }
5937 xmlOutputBufferWriteString(buf, " [\n");
5938 xmlNodeListDumpOutput(buf, dtd->doc, dtd->children, -1, 0, encoding);
5939 xmlOutputBufferWriteString(buf, "]>");
5940}
5941
5942/**
5943 * xmlAttrDumpOutput:
5944 * @buf: the XML buffer output
5945 * @doc: the document
5946 * @cur: the attribute pointer
5947 * @encoding: an optional encoding string
5948 *
5949 * Dump an XML attribute
5950 */
5951static void
5952xmlAttrDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00005953 const char *encoding ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00005954 xmlChar *value;
5955
5956 if (cur == NULL) {
5957#ifdef DEBUG_TREE
5958 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00005959 "xmlAttrDumpOutput : property == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005960#endif
5961 return;
5962 }
5963 xmlOutputBufferWriteString(buf, " ");
5964 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
5965 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
5966 xmlOutputBufferWriteString(buf, ":");
5967 }
5968 xmlOutputBufferWriteString(buf, (const char *)cur->name);
5969 value = xmlNodeListGetString(doc, cur->children, 0);
5970 if (value) {
5971 xmlOutputBufferWriteString(buf, "=");
5972 xmlBufferWriteQuotedString(buf->buffer, value);
5973 xmlFree(value);
5974 } else {
5975 xmlOutputBufferWriteString(buf, "=\"\"");
5976 }
5977}
5978
5979/**
5980 * xmlAttrListDumpOutput:
5981 * @buf: the XML buffer output
5982 * @doc: the document
5983 * @cur: the first attribute pointer
5984 * @encoding: an optional encoding string
5985 *
5986 * Dump a list of XML attributes
5987 */
5988static void
5989xmlAttrListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
5990 xmlAttrPtr cur, const char *encoding) {
5991 if (cur == NULL) {
5992#ifdef DEBUG_TREE
5993 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00005994 "xmlAttrListDumpOutput : property == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005995#endif
5996 return;
5997 }
5998 while (cur != NULL) {
5999 xmlAttrDumpOutput(buf, doc, cur, encoding);
6000 cur = cur->next;
6001 }
6002}
6003
6004
6005
6006/**
6007 * xmlNodeListDumpOutput:
6008 * @buf: the XML buffer output
6009 * @doc: the document
6010 * @cur: the first node
6011 * @level: the imbrication level for indenting
6012 * @format: is formatting allowed
6013 * @encoding: an optional encoding string
6014 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006015 * Dump an XML node list, recursive behaviour, children are printed too.
Owen Taylor3473f882001-02-23 17:55:21 +00006016 */
6017static void
6018xmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
6019 xmlNodePtr cur, int level, int format, const char *encoding) {
6020 int i;
6021
6022 if (cur == NULL) {
6023#ifdef DEBUG_TREE
6024 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006025 "xmlNodeListDumpOutput : node == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006026#endif
6027 return;
6028 }
6029 while (cur != NULL) {
6030 if ((format) && (xmlIndentTreeOutput) &&
6031 (cur->type == XML_ELEMENT_NODE))
6032 for (i = 0;i < level;i++)
6033 xmlOutputBufferWriteString(buf, " ");
6034 xmlNodeDumpOutput(buf, doc, cur, level, format, encoding);
6035 if (format) {
6036 xmlOutputBufferWriteString(buf, "\n");
6037 }
6038 cur = cur->next;
6039 }
6040}
6041
6042/**
6043 * xmlNodeDumpOutput:
6044 * @buf: the XML buffer output
6045 * @doc: the document
6046 * @cur: the current node
6047 * @level: the imbrication level for indenting
6048 * @format: is formatting allowed
6049 * @encoding: an optional encoding string
6050 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006051 * Dump an XML node, recursive behaviour, children are printed too.
Owen Taylor3473f882001-02-23 17:55:21 +00006052 */
6053void
6054xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
6055 int level, int format, const char *encoding) {
6056 int i;
6057 xmlNodePtr tmp;
6058
6059 if (cur == NULL) {
6060#ifdef DEBUG_TREE
6061 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006062 "xmlNodeDumpOutput : node == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006063#endif
6064 return;
6065 }
6066 if (cur->type == XML_XINCLUDE_START)
6067 return;
6068 if (cur->type == XML_XINCLUDE_END)
6069 return;
6070 if (cur->type == XML_DTD_NODE) {
6071 xmlDtdDumpOutput(buf, (xmlDtdPtr) cur, encoding);
6072 return;
6073 }
6074 if (cur->type == XML_ELEMENT_DECL) {
6075 xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur);
6076 return;
6077 }
6078 if (cur->type == XML_ATTRIBUTE_DECL) {
6079 xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur);
6080 return;
6081 }
6082 if (cur->type == XML_ENTITY_DECL) {
6083 xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur);
6084 return;
6085 }
6086 if (cur->type == XML_TEXT_NODE) {
6087 if (cur->content != NULL) {
6088 if ((cur->name == xmlStringText) ||
6089 (cur->name != xmlStringTextNoenc)) {
6090 xmlChar *buffer;
6091
6092#ifndef XML_USE_BUFFER_CONTENT
6093 if (encoding == NULL)
6094 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
6095 else
6096 buffer = xmlEncodeSpecialChars(doc, cur->content);
6097#else
6098 if (encoding == NULL)
6099 buffer = xmlEncodeEntitiesReentrant(doc,
6100 xmlBufferContent(cur->content));
6101 else
6102 buffer = xmlEncodeSpecialChars(doc,
6103 xmlBufferContent(cur->content));
6104#endif
6105 if (buffer != NULL) {
6106 xmlOutputBufferWriteString(buf, (const char *)buffer);
6107 xmlFree(buffer);
6108 }
6109 } else {
6110 /*
6111 * Disable escaping, needed for XSLT
6112 */
6113#ifndef XML_USE_BUFFER_CONTENT
6114 xmlOutputBufferWriteString(buf, (const char *) cur->content);
6115#else
6116 xmlOutputBufferWriteString(buf, xmlBufferContent(cur->content));
6117#endif
6118 }
6119 }
6120
6121 return;
6122 }
6123 if (cur->type == XML_PI_NODE) {
6124 if (cur->content != NULL) {
6125 xmlOutputBufferWriteString(buf, "<?");
6126 xmlOutputBufferWriteString(buf, (const char *)cur->name);
6127 if (cur->content != NULL) {
6128 xmlOutputBufferWriteString(buf, " ");
6129#ifndef XML_USE_BUFFER_CONTENT
6130 xmlOutputBufferWriteString(buf, (const char *)cur->content);
6131#else
6132 xmlOutputBufferWriteString(buf, (const char *)xmlBufferContent(cur->content));
6133#endif
6134 }
6135 xmlOutputBufferWriteString(buf, "?>");
6136 } else {
6137 xmlOutputBufferWriteString(buf, "<?");
6138 xmlOutputBufferWriteString(buf, (const char *)cur->name);
6139 xmlOutputBufferWriteString(buf, "?>");
6140 }
6141 return;
6142 }
6143 if (cur->type == XML_COMMENT_NODE) {
6144 if (cur->content != NULL) {
6145 xmlOutputBufferWriteString(buf, "<!--");
6146#ifndef XML_USE_BUFFER_CONTENT
6147 xmlOutputBufferWriteString(buf, (const char *)cur->content);
6148#else
6149 xmlOutputBufferWriteString(buf, (const char *)xmlBufferContent(cur->content));
6150#endif
6151 xmlOutputBufferWriteString(buf, "-->");
6152 }
6153 return;
6154 }
6155 if (cur->type == XML_ENTITY_REF_NODE) {
6156 xmlOutputBufferWriteString(buf, "&");
6157 xmlOutputBufferWriteString(buf, (const char *)cur->name);
6158 xmlOutputBufferWriteString(buf, ";");
6159 return;
6160 }
6161 if (cur->type == XML_CDATA_SECTION_NODE) {
6162 xmlOutputBufferWriteString(buf, "<![CDATA[");
6163 if (cur->content != NULL)
6164#ifndef XML_USE_BUFFER_CONTENT
6165 xmlOutputBufferWriteString(buf, (const char *)cur->content);
6166#else
6167 xmlOutputBufferWriteString(buf, (const char *)xmlBufferContent(cur->content));
6168#endif
6169 xmlOutputBufferWriteString(buf, "]]>");
6170 return;
6171 }
6172
6173 if (format == 1) {
6174 tmp = cur->children;
6175 while (tmp != NULL) {
6176 if ((tmp->type == XML_TEXT_NODE) ||
6177 (tmp->type == XML_ENTITY_REF_NODE)) {
6178 format = 0;
6179 break;
6180 }
6181 tmp = tmp->next;
6182 }
6183 }
6184 xmlOutputBufferWriteString(buf, "<");
6185 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
6186 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
6187 xmlOutputBufferWriteString(buf, ":");
6188 }
6189
6190 xmlOutputBufferWriteString(buf, (const char *)cur->name);
6191 if (cur->nsDef)
6192 xmlNsListDumpOutput(buf, cur->nsDef);
6193 if (cur->properties != NULL)
6194 xmlAttrListDumpOutput(buf, doc, cur->properties, encoding);
6195
Daniel Veillard7db37732001-07-12 01:20:08 +00006196 if (((cur->type == XML_ELEMENT_NODE) || (cur->content == NULL)) &&
6197 (cur->children == NULL) && (!xmlSaveNoEmptyTags)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006198 xmlOutputBufferWriteString(buf, "/>");
6199 return;
6200 }
6201 xmlOutputBufferWriteString(buf, ">");
Daniel Veillard7db37732001-07-12 01:20:08 +00006202 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006203 xmlChar *buffer;
6204
6205#ifndef XML_USE_BUFFER_CONTENT
6206 if (encoding == NULL)
6207 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
6208 else
6209 buffer = xmlEncodeSpecialChars(doc, cur->content);
6210#else
6211 if (encoding == NULL)
6212 buffer = xmlEncodeEntitiesReentrant(doc,
6213 xmlBufferContent(cur->content));
6214 else
6215 buffer = xmlEncodeSpecialChars(doc,
6216 xmlBufferContent(cur->content));
6217#endif
6218 if (buffer != NULL) {
6219 xmlOutputBufferWriteString(buf, (const char *)buffer);
6220 xmlFree(buffer);
6221 }
6222 }
6223 if (cur->children != NULL) {
6224 if (format) xmlOutputBufferWriteString(buf, "\n");
6225 xmlNodeListDumpOutput(buf, doc, cur->children,
6226 (level >= 0?level+1:-1), format, encoding);
6227 if ((xmlIndentTreeOutput) && (format))
6228 for (i = 0;i < level;i++)
6229 xmlOutputBufferWriteString(buf, " ");
6230 }
6231 xmlOutputBufferWriteString(buf, "</");
6232 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
6233 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
6234 xmlOutputBufferWriteString(buf, ":");
6235 }
6236
6237 xmlOutputBufferWriteString(buf, (const char *)cur->name);
6238 xmlOutputBufferWriteString(buf, ">");
6239}
6240
6241/**
6242 * xmlDocContentDumpOutput:
6243 * @buf: the XML buffer output
6244 * @cur: the document
6245 * @encoding: an optional encoding string
6246 * @format: should formatting spaces been added
6247 *
6248 * Dump an XML document.
6249 */
6250static void
6251xmlDocContentDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr cur,
6252 const char *encoding, int format) {
6253 xmlOutputBufferWriteString(buf, "<?xml version=");
6254 if (cur->version != NULL)
6255 xmlBufferWriteQuotedString(buf->buffer, cur->version);
6256 else
6257 xmlOutputBufferWriteString(buf, "\"1.0\"");
6258 if (encoding == NULL) {
6259 if (cur->encoding != NULL)
6260 encoding = (const char *) cur->encoding;
6261 else if (cur->charset != XML_CHAR_ENCODING_UTF8)
6262 encoding = xmlGetCharEncodingName((xmlCharEncoding) cur->charset);
6263 }
6264 if (encoding != NULL) {
6265 xmlOutputBufferWriteString(buf, " encoding=");
6266 xmlBufferWriteQuotedString(buf->buffer, (xmlChar *) encoding);
6267 }
6268 switch (cur->standalone) {
6269 case 0:
6270 xmlOutputBufferWriteString(buf, " standalone=\"no\"");
6271 break;
6272 case 1:
6273 xmlOutputBufferWriteString(buf, " standalone=\"yes\"");
6274 break;
6275 }
6276 xmlOutputBufferWriteString(buf, "?>\n");
6277 if (cur->children != NULL) {
6278 xmlNodePtr child = cur->children;
6279
6280 while (child != NULL) {
6281 xmlNodeDumpOutput(buf, cur, child, 0, format, encoding);
6282 xmlOutputBufferWriteString(buf, "\n");
6283 child = child->next;
6284 }
6285 }
6286}
6287
6288/************************************************************************
6289 * *
6290 * Saving functions front-ends *
6291 * *
6292 ************************************************************************/
6293
6294/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +00006295 * xmlDocDumpFormatMemoryEnc:
Owen Taylor3473f882001-02-23 17:55:21 +00006296 * @out_doc: Document to generate XML text from
6297 * @doc_txt_ptr: Memory pointer for allocated XML text
6298 * @doc_txt_len: Length of the generated XML text
6299 * @txt_encoding: Character encoding to use when generating XML text
6300 * @format: should formatting spaces been added
6301 *
6302 * Dump the current DOM tree into memory using the character encoding specified
6303 * by the caller. Note it is up to the caller of this function to free the
6304 * allocated memory.
6305 */
6306
6307void
6308xmlDocDumpFormatMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00006309 int * doc_txt_len, const char * txt_encoding,
Daniel Veillard1731d6a2001-04-10 16:38:06 +00006310 int format) {
Owen Taylor3473f882001-02-23 17:55:21 +00006311 int dummy = 0;
6312
6313 xmlCharEncoding doc_charset;
6314 xmlOutputBufferPtr out_buff = NULL;
6315 xmlCharEncodingHandlerPtr conv_hdlr = NULL;
6316
6317 if (doc_txt_len == NULL) {
6318 doc_txt_len = &dummy; /* Continue, caller just won't get length */
6319 }
6320
6321 if (doc_txt_ptr == NULL) {
6322 *doc_txt_len = 0;
6323 xmlGenericError(xmlGenericErrorContext,
6324 "xmlDocDumpFormatMemoryEnc: Null return buffer pointer.");
6325 return;
6326 }
6327
6328 *doc_txt_ptr = NULL;
6329 *doc_txt_len = 0;
6330
6331 if (out_doc == NULL) {
6332 /* No document, no output */
6333 xmlGenericError(xmlGenericErrorContext,
6334 "xmlDocDumpFormatMemoryEnc: Null DOM tree document pointer.\n");
6335 return;
6336 }
6337
6338 /*
6339 * Validate the encoding value, if provided.
6340 * This logic is copied from xmlSaveFileEnc.
6341 */
6342
6343 if (txt_encoding == NULL)
6344 txt_encoding = (const char *) out_doc->encoding;
6345 if (txt_encoding != NULL) {
6346 doc_charset = xmlParseCharEncoding(txt_encoding);
6347
6348 if (out_doc->charset != XML_CHAR_ENCODING_UTF8) {
6349 xmlGenericError(xmlGenericErrorContext,
6350 "xmlDocDumpFormatMemoryEnc: Source document not in UTF8\n");
6351 return;
6352
6353 } else if (doc_charset != XML_CHAR_ENCODING_UTF8) {
6354 conv_hdlr = xmlFindCharEncodingHandler(txt_encoding);
6355 if ( conv_hdlr == NULL ) {
6356 xmlGenericError(xmlGenericErrorContext,
6357 "%s: %s %s '%s'\n",
6358 "xmlDocDumpFormatMemoryEnc",
6359 "Failed to identify encoding handler for",
6360 "character set",
6361 txt_encoding);
6362 return;
6363 }
6364 }
6365 }
6366
6367 if ((out_buff = xmlAllocOutputBuffer(conv_hdlr)) == NULL ) {
6368 xmlGenericError(xmlGenericErrorContext,
6369 "xmlDocDumpFormatMemoryEnc: Failed to allocate output buffer.\n");
6370 return;
6371 }
6372
Daniel Veillard1731d6a2001-04-10 16:38:06 +00006373 xmlDocContentDumpOutput(out_buff, out_doc, txt_encoding, format);
Owen Taylor3473f882001-02-23 17:55:21 +00006374 xmlOutputBufferFlush(out_buff);
6375 if (out_buff->conv != NULL) {
6376 *doc_txt_len = out_buff->conv->use;
6377 *doc_txt_ptr = xmlStrndup(out_buff->conv->content, *doc_txt_len);
6378 } else {
6379 *doc_txt_len = out_buff->buffer->use;
6380 *doc_txt_ptr = xmlStrndup(out_buff->buffer->content, *doc_txt_len);
6381 }
6382 (void)xmlOutputBufferClose(out_buff);
6383
6384 if ((*doc_txt_ptr == NULL) && (*doc_txt_len > 0)) {
6385 *doc_txt_len = 0;
6386 xmlGenericError(xmlGenericErrorContext,
6387 "xmlDocDumpFormatMemoryEnc: %s\n",
6388 "Failed to allocate memory for document text representation.");
6389 }
6390
6391 return;
6392}
6393
6394/**
6395 * xmlDocDumpMemory:
6396 * @cur: the document
6397 * @mem: OUT: the memory pointer
Daniel Veillard60087f32001-10-10 09:45:09 +00006398 * @size: OUT: the memory length
Owen Taylor3473f882001-02-23 17:55:21 +00006399 *
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
6404xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) {
6405 xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, 0);
6406}
6407
6408/**
6409 * xmlDocDumpFormatMemory:
6410 * @cur: the document
6411 * @mem: OUT: the memory pointer
Daniel Veillard60087f32001-10-10 09:45:09 +00006412 * @size: OUT: the memory length
Owen Taylor3473f882001-02-23 17:55:21 +00006413 * @format: should formatting spaces been added
6414 *
6415 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006416 * Dump an XML document in memory and return the #xmlChar * and it's size.
Owen Taylor3473f882001-02-23 17:55:21 +00006417 * It's up to the caller to free the memory.
6418 */
6419void
6420xmlDocDumpFormatMemory(xmlDocPtr cur, xmlChar**mem, int *size, int format) {
6421 xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, format);
6422}
6423
6424/**
6425 * xmlDocDumpMemoryEnc:
6426 * @out_doc: Document to generate XML text from
6427 * @doc_txt_ptr: Memory pointer for allocated XML text
6428 * @doc_txt_len: Length of the generated XML text
6429 * @txt_encoding: Character encoding to use when generating XML text
6430 *
6431 * Dump the current DOM tree into memory using the character encoding specified
6432 * by the caller. Note it is up to the caller of this function to free the
6433 * allocated memory.
6434 */
6435
6436void
6437xmlDocDumpMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
6438 int * doc_txt_len, const char * txt_encoding) {
6439 xmlDocDumpFormatMemoryEnc(out_doc, doc_txt_ptr, doc_txt_len,
Daniel Veillard1731d6a2001-04-10 16:38:06 +00006440 txt_encoding, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00006441}
6442
6443/**
6444 * xmlGetDocCompressMode:
6445 * @doc: the document
6446 *
6447 * get the compression ratio for a document, ZLIB based
6448 * Returns 0 (uncompressed) to 9 (max compression)
6449 */
6450int
6451xmlGetDocCompressMode (xmlDocPtr doc) {
6452 if (doc == NULL) return(-1);
6453 return(doc->compression);
6454}
6455
6456/**
6457 * xmlSetDocCompressMode:
6458 * @doc: the document
6459 * @mode: the compression ratio
6460 *
6461 * set the compression ratio for a document, ZLIB based
6462 * Correct values: 0 (uncompressed) to 9 (max compression)
6463 */
6464void
6465xmlSetDocCompressMode (xmlDocPtr doc, int mode) {
6466 if (doc == NULL) return;
6467 if (mode < 0) doc->compression = 0;
6468 else if (mode > 9) doc->compression = 9;
6469 else doc->compression = mode;
6470}
6471
6472/**
6473 * xmlGetCompressMode:
6474 *
6475 * get the default compression mode used, ZLIB based.
6476 * Returns 0 (uncompressed) to 9 (max compression)
6477 */
6478int
6479 xmlGetCompressMode(void) {
6480 return(xmlCompressMode);
6481}
6482
6483/**
6484 * xmlSetCompressMode:
6485 * @mode: the compression ratio
6486 *
6487 * set the default compression mode used, ZLIB based
6488 * Correct values: 0 (uncompressed) to 9 (max compression)
6489 */
6490void
6491xmlSetCompressMode(int mode) {
6492 if (mode < 0) xmlCompressMode = 0;
6493 else if (mode > 9) xmlCompressMode = 9;
6494 else xmlCompressMode = mode;
6495}
6496
6497/**
6498 * xmlDocDump:
6499 * @f: the FILE*
6500 * @cur: the document
6501 *
6502 * Dump an XML document to an open FILE.
6503 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006504 * returns: the number of bytes written or -1 in case of failure.
Owen Taylor3473f882001-02-23 17:55:21 +00006505 */
6506int
6507xmlDocDump(FILE *f, xmlDocPtr cur) {
6508 xmlOutputBufferPtr buf;
6509 const char * encoding;
6510 xmlCharEncodingHandlerPtr handler = NULL;
6511 int ret;
6512
6513 if (cur == NULL) {
6514#ifdef DEBUG_TREE
6515 xmlGenericError(xmlGenericErrorContext,
6516 "xmlDocDump : document == NULL\n");
6517#endif
6518 return(-1);
6519 }
6520 encoding = (const char *) cur->encoding;
6521
6522 if (encoding != NULL) {
6523 xmlCharEncoding enc;
6524
6525 enc = xmlParseCharEncoding(encoding);
6526
6527 if (cur->charset != XML_CHAR_ENCODING_UTF8) {
6528 xmlGenericError(xmlGenericErrorContext,
6529 "xmlDocDump: document not in UTF8\n");
6530 return(-1);
6531 }
6532 if (enc != XML_CHAR_ENCODING_UTF8) {
6533 handler = xmlFindCharEncodingHandler(encoding);
6534 if (handler == NULL) {
6535 xmlFree((char *) cur->encoding);
6536 cur->encoding = NULL;
6537 }
6538 }
6539 }
6540 buf = xmlOutputBufferCreateFile(f, handler);
6541 if (buf == NULL) return(-1);
Daniel Veillard1731d6a2001-04-10 16:38:06 +00006542 xmlDocContentDumpOutput(buf, cur, NULL, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00006543
6544 ret = xmlOutputBufferClose(buf);
6545 return(ret);
6546}
6547
6548/**
6549 * xmlSaveFileTo:
6550 * @buf: an output I/O buffer
6551 * @cur: the document
Daniel Veillardd1640922001-12-17 15:30:10 +00006552 * @encoding: the encoding if any assuming the I/O layer handles the trancoding
Owen Taylor3473f882001-02-23 17:55:21 +00006553 *
6554 * Dump an XML document to an I/O buffer.
6555 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006556 * returns: the number of bytes written or -1 in case of failure.
Owen Taylor3473f882001-02-23 17:55:21 +00006557 */
6558int
CET 2001 Daniel Veillard5a37bde2001-11-01 14:31:22 +00006559xmlSaveFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding) {
Owen Taylor3473f882001-02-23 17:55:21 +00006560 int ret;
6561
6562 if (buf == NULL) return(0);
Daniel Veillard1731d6a2001-04-10 16:38:06 +00006563 xmlDocContentDumpOutput(buf, cur, encoding, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00006564 ret = xmlOutputBufferClose(buf);
6565 return(ret);
6566}
6567
6568/**
Daniel Veillardeefd4492001-04-28 16:55:50 +00006569 * xmlSaveFormatFileTo:
6570 * @buf: an output I/O buffer
6571 * @cur: the document
Daniel Veillardd1640922001-12-17 15:30:10 +00006572 * @encoding: the encoding if any assuming the I/O layer handles the trancoding
Daniel Veillardeefd4492001-04-28 16:55:50 +00006573 * @format: should formatting spaces been added
6574 *
6575 * Dump an XML document to an I/O buffer.
6576 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006577 * returns: the number of bytes written or -1 in case of failure.
Daniel Veillardeefd4492001-04-28 16:55:50 +00006578 */
6579int
CET 2001 Daniel Veillard5a37bde2001-11-01 14:31:22 +00006580xmlSaveFormatFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding, int format) {
Daniel Veillardeefd4492001-04-28 16:55:50 +00006581 int ret;
6582
6583 if (buf == NULL) return(0);
6584 xmlDocContentDumpOutput(buf, cur, encoding, format);
6585 ret = xmlOutputBufferClose(buf);
6586 return(ret);
6587}
6588
6589/**
Daniel Veillardf012a642001-07-23 19:10:52 +00006590 * xmlSaveFormatFileEnc
6591 * @filename: the filename or URL to output
6592 * @cur: the document being saved
6593 * @encoding: the name of the encoding to use or NULL.
6594 * @format: should formatting spaces be added.
Daniel Veillardd1640922001-12-17 15:30:10 +00006595 *
6596 * Returns the number of bytes written or -1 in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00006597 */
6598int
Daniel Veillardf012a642001-07-23 19:10:52 +00006599xmlSaveFormatFileEnc( const char * filename, xmlDocPtr cur,
6600 const char * encoding, int format ) {
Owen Taylor3473f882001-02-23 17:55:21 +00006601 xmlOutputBufferPtr buf;
6602 xmlCharEncodingHandlerPtr handler = NULL;
Daniel Veillard81418e32001-05-22 15:08:55 +00006603 xmlCharEncoding enc;
Owen Taylor3473f882001-02-23 17:55:21 +00006604 int ret;
6605
6606 if (encoding != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00006607
6608 enc = xmlParseCharEncoding(encoding);
6609 if (cur->charset != XML_CHAR_ENCODING_UTF8) {
6610 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006611 "xmlSaveFormatFileEnc: document not in UTF8\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006612 return(-1);
6613 }
6614 if (enc != XML_CHAR_ENCODING_UTF8) {
6615 handler = xmlFindCharEncodingHandler(encoding);
Daniel Veillard81418e32001-05-22 15:08:55 +00006616 if (handler == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00006617 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006618 }
6619 }
6620
Daniel Veillardf012a642001-07-23 19:10:52 +00006621#ifdef HAVE_ZLIB_H
6622 if (cur->compression < 0) cur->compression = xmlCompressMode;
6623#endif
Owen Taylor3473f882001-02-23 17:55:21 +00006624 /*
6625 * save the content to a temp buffer.
6626 */
Daniel Veillardf012a642001-07-23 19:10:52 +00006627 buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression);
Owen Taylor3473f882001-02-23 17:55:21 +00006628 if (buf == NULL) return(-1);
6629
Daniel Veillardf012a642001-07-23 19:10:52 +00006630 xmlDocContentDumpOutput(buf, cur, encoding, format);
Owen Taylor3473f882001-02-23 17:55:21 +00006631
6632 ret = xmlOutputBufferClose(buf);
6633 return(ret);
6634}
6635
Daniel Veillardf012a642001-07-23 19:10:52 +00006636
6637/**
6638 * xmlSaveFileEnc:
6639 * @filename: the filename (or URL)
6640 * @cur: the document
6641 * @encoding: the name of an encoding (or NULL)
6642 *
6643 * Dump an XML document, converting it to the given encoding
6644 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006645 * returns: the number of bytes written or -1 in case of failure.
Daniel Veillardf012a642001-07-23 19:10:52 +00006646 */
6647int
6648xmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) {
6649 return ( xmlSaveFormatFileEnc( filename, cur, encoding, 0 ) );
6650}
6651
Owen Taylor3473f882001-02-23 17:55:21 +00006652/**
Daniel Veillard67fee942001-04-26 18:59:03 +00006653 * xmlSaveFormatFile:
Owen Taylor3473f882001-02-23 17:55:21 +00006654 * @filename: the filename (or URL)
6655 * @cur: the document
Daniel Veillard67fee942001-04-26 18:59:03 +00006656 * @format: should formatting spaces been added
Owen Taylor3473f882001-02-23 17:55:21 +00006657 *
6658 * Dump an XML document to a file. Will use compression if
6659 * compiled in and enabled. If @filename is "-" the stdout file is
Daniel Veillardd1640922001-12-17 15:30:10 +00006660 * used. If @format is set then the document will be indented on output.
Daniel Veillard67fee942001-04-26 18:59:03 +00006661 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006662 * returns: the number of bytes written or -1 in case of failure.
Owen Taylor3473f882001-02-23 17:55:21 +00006663 */
6664int
Daniel Veillard67fee942001-04-26 18:59:03 +00006665xmlSaveFormatFile(const char *filename, xmlDocPtr cur, int format) {
Daniel Veillardf012a642001-07-23 19:10:52 +00006666 return ( xmlSaveFormatFileEnc( filename, cur, NULL, format ) );
Owen Taylor3473f882001-02-23 17:55:21 +00006667}
6668
Daniel Veillard67fee942001-04-26 18:59:03 +00006669/**
6670 * xmlSaveFile:
6671 * @filename: the filename (or URL)
6672 * @cur: the document
6673 *
6674 * Dump an XML document to a file. Will use compression if
6675 * compiled in and enabled. If @filename is "-" the stdout file is
6676 * used.
Daniel Veillardd1640922001-12-17 15:30:10 +00006677 * returns: the number of bytes written or -1 in case of failure.
Daniel Veillard67fee942001-04-26 18:59:03 +00006678 */
6679int
6680xmlSaveFile(const char *filename, xmlDocPtr cur) {
Daniel Veillardf012a642001-07-23 19:10:52 +00006681 return(xmlSaveFormatFileEnc(filename, cur, NULL, 0));
Daniel Veillard67fee942001-04-26 18:59:03 +00006682}
6683