blob: 2b145582985798f52a713b3680a7e954c1012aa4 [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 }
Daniel Veillarde6a55192002-01-14 17:11:53 +00002355 if (cur->type == XML_NAMESPACE_DECL) {
2356 xmlFreeNsList((xmlNsPtr) cur);
2357 return;
2358 }
Owen Taylor3473f882001-02-23 17:55:21 +00002359 while (cur != NULL) {
2360 next = cur->next;
Daniel Veillard02141ea2001-04-30 11:46:40 +00002361 /* unroll to speed up freeing the document */
2362 if (cur->type != XML_DTD_NODE) {
2363 if ((cur->children != NULL) &&
2364 (cur->type != XML_ENTITY_REF_NODE))
2365 xmlFreeNodeList(cur->children);
2366 if (cur->properties != NULL)
2367 xmlFreePropList(cur->properties);
Daniel Veillard7db37732001-07-12 01:20:08 +00002368 if ((cur->type != XML_ELEMENT_NODE) &&
2369 (cur->type != XML_XINCLUDE_START) &&
2370 (cur->type != XML_XINCLUDE_END) &&
2371 (cur->type != XML_ENTITY_REF_NODE)) {
Daniel Veillard02141ea2001-04-30 11:46:40 +00002372#ifndef XML_USE_BUFFER_CONTENT
2373 if (cur->content != NULL) xmlFree(cur->content);
2374#else
2375 if (cur->content != NULL) xmlBufferFree(cur->content);
2376#endif
Daniel Veillard7db37732001-07-12 01:20:08 +00002377 }
2378 if (((cur->type == XML_ELEMENT_NODE) ||
2379 (cur->type == XML_XINCLUDE_START) ||
2380 (cur->type == XML_XINCLUDE_END)) &&
2381 (cur->nsDef != NULL))
2382 xmlFreeNsList(cur->nsDef);
2383
Daniel Veillard9cc6dc62001-06-11 08:09:20 +00002384 /*
2385 * When a node is a text node or a comment, it uses a global static
2386 * variable for the name of the node.
2387 *
2388 * The xmlStrEqual comparisons need to be done when (happened with
2389 * XML::libXML and XML::libXSLT) the library is included twice
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002390 * statically in the binary and a tree allocated by one occurrence
Daniel Veillardd1640922001-12-17 15:30:10 +00002391 * of the lib gets freed by the other occurrence, in this case
Daniel Veillard9cc6dc62001-06-11 08:09:20 +00002392 * the string addresses compare are not sufficient.
2393 */
Daniel Veillard02141ea2001-04-30 11:46:40 +00002394 if ((cur->name != NULL) &&
2395 (cur->name != xmlStringText) &&
2396 (cur->name != xmlStringTextNoenc) &&
Daniel Veillard9cc6dc62001-06-11 08:09:20 +00002397 (cur->name != xmlStringComment)) {
2398 if (cur->type == XML_TEXT_NODE) {
2399 if ((!xmlStrEqual(cur->name, xmlStringText)) &&
2400 (!xmlStrEqual(cur->name, xmlStringTextNoenc)))
2401 xmlFree((char *) cur->name);
2402 } else if (cur->type == XML_COMMENT_NODE) {
2403 if (!xmlStrEqual(cur->name, xmlStringComment))
2404 xmlFree((char *) cur->name);
2405 } else
2406 xmlFree((char *) cur->name);
2407 }
Daniel Veillard02141ea2001-04-30 11:46:40 +00002408 /* TODO : derecursivate this function */
Daniel Veillard02141ea2001-04-30 11:46:40 +00002409 xmlFree(cur);
2410 }
Owen Taylor3473f882001-02-23 17:55:21 +00002411 cur = next;
2412 }
2413}
2414
2415/**
2416 * xmlFreeNode:
2417 * @cur: the node
2418 *
2419 * Free a node, this is a recursive behaviour, all the children are freed too.
2420 * This doesn't unlink the child from the list, use xmlUnlinkNode() first.
2421 */
2422void
2423xmlFreeNode(xmlNodePtr cur) {
2424 if (cur == NULL) {
2425#ifdef DEBUG_TREE
2426 xmlGenericError(xmlGenericErrorContext,
2427 "xmlFreeNode : node == NULL\n");
2428#endif
2429 return;
2430 }
Daniel Veillard02141ea2001-04-30 11:46:40 +00002431 /* use xmlFreeDtd for DTD nodes */
Daniel Veillarde6a55192002-01-14 17:11:53 +00002432 if (cur->type == XML_DTD_NODE) {
2433 xmlFreeDtd((xmlDtdPtr) cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002434 return;
Daniel Veillarde6a55192002-01-14 17:11:53 +00002435 }
2436 if (cur->type == XML_NAMESPACE_DECL) {
2437 xmlFreeNs((xmlNsPtr) cur);
2438 return;
2439 }
Owen Taylor3473f882001-02-23 17:55:21 +00002440 if ((cur->children != NULL) &&
2441 (cur->type != XML_ENTITY_REF_NODE))
2442 xmlFreeNodeList(cur->children);
Daniel Veillard02141ea2001-04-30 11:46:40 +00002443 if (cur->properties != NULL)
2444 xmlFreePropList(cur->properties);
Daniel Veillard7db37732001-07-12 01:20:08 +00002445 if ((cur->type != XML_ELEMENT_NODE) &&
2446 (cur->content != NULL) &&
2447 (cur->type != XML_ENTITY_REF_NODE) &&
2448 (cur->type != XML_XINCLUDE_END) &&
2449 (cur->type != XML_XINCLUDE_START)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002450#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard7db37732001-07-12 01:20:08 +00002451 xmlFree(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002452#else
Daniel Veillard7db37732001-07-12 01:20:08 +00002453 xmlBufferFree(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002454#endif
Daniel Veillard7db37732001-07-12 01:20:08 +00002455 }
2456
Daniel Veillardacd370f2001-06-09 17:17:51 +00002457 /*
2458 * When a node is a text node or a comment, it uses a global static
2459 * variable for the name of the node.
2460 *
2461 * The xmlStrEqual comparisons need to be done when (happened with
2462 * XML::libXML and XML::libXSLT) the library is included twice statically
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002463 * in the binary and a tree allocated by one occurence of the lib gets
Daniel Veillardd1640922001-12-17 15:30:10 +00002464 * freed by the other occurrence, in this case the string addresses compare
Daniel Veillardacd370f2001-06-09 17:17:51 +00002465 * are not sufficient.
2466 */
Owen Taylor3473f882001-02-23 17:55:21 +00002467 if ((cur->name != NULL) &&
2468 (cur->name != xmlStringText) &&
2469 (cur->name != xmlStringTextNoenc) &&
Daniel Veillardacd370f2001-06-09 17:17:51 +00002470 (cur->name != xmlStringComment)) {
2471 if (cur->type == XML_TEXT_NODE) {
2472 if ((!xmlStrEqual(cur->name, xmlStringText)) &&
2473 (!xmlStrEqual(cur->name, xmlStringTextNoenc)))
2474 xmlFree((char *) cur->name);
2475 } else if (cur->type == XML_COMMENT_NODE) {
2476 if (!xmlStrEqual(cur->name, xmlStringComment))
2477 xmlFree((char *) cur->name);
2478 } else
2479 xmlFree((char *) cur->name);
2480 }
2481
Owen Taylor3473f882001-02-23 17:55:21 +00002482 if (cur->nsDef != NULL) xmlFreeNsList(cur->nsDef);
Owen Taylor3473f882001-02-23 17:55:21 +00002483 xmlFree(cur);
2484}
2485
2486/**
2487 * xmlUnlinkNode:
2488 * @cur: the node
2489 *
2490 * Unlink a node from it's current context, the node is not freed
2491 */
2492void
2493xmlUnlinkNode(xmlNodePtr cur) {
2494 if (cur == NULL) {
2495#ifdef DEBUG_TREE
2496 xmlGenericError(xmlGenericErrorContext,
2497 "xmlUnlinkNode : node == NULL\n");
2498#endif
2499 return;
2500 }
Daniel Veillard29e43992001-12-13 22:21:58 +00002501 if (cur->type == XML_DTD_NODE) {
2502 xmlDocPtr doc;
2503 doc = cur->doc;
2504 if (doc->intSubset == (xmlDtdPtr) cur)
2505 doc->intSubset = NULL;
2506 if (doc->extSubset == (xmlDtdPtr) cur)
2507 doc->extSubset = NULL;
2508 }
Owen Taylor3473f882001-02-23 17:55:21 +00002509 if ((cur->parent != NULL) && (cur->parent->children == cur))
2510 cur->parent->children = cur->next;
2511 if ((cur->parent != NULL) && (cur->parent->last == cur))
2512 cur->parent->last = cur->prev;
2513 if (cur->next != NULL)
2514 cur->next->prev = cur->prev;
2515 if (cur->prev != NULL)
2516 cur->prev->next = cur->next;
2517 cur->next = cur->prev = NULL;
2518 cur->parent = NULL;
2519}
2520
2521/**
2522 * xmlReplaceNode:
2523 * @old: the old node
2524 * @cur: the node
2525 *
2526 * Unlink the old node from it's current context, prune the new one
Daniel Veillardd1640922001-12-17 15:30:10 +00002527 * at the same place. If @cur was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00002528 * first unlinked from its existing context.
2529 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002530 * Returns the @old node
Owen Taylor3473f882001-02-23 17:55:21 +00002531 */
2532xmlNodePtr
2533xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) {
2534 if (old == NULL) {
2535#ifdef DEBUG_TREE
2536 xmlGenericError(xmlGenericErrorContext,
2537 "xmlReplaceNode : old == NULL\n");
2538#endif
2539 return(NULL);
2540 }
2541 if (cur == NULL) {
2542 xmlUnlinkNode(old);
2543 return(old);
2544 }
2545 if (cur == old) {
2546 return(old);
2547 }
2548 xmlUnlinkNode(cur);
2549 cur->doc = old->doc;
2550 cur->parent = old->parent;
2551 cur->next = old->next;
2552 if (cur->next != NULL)
2553 cur->next->prev = cur;
2554 cur->prev = old->prev;
2555 if (cur->prev != NULL)
2556 cur->prev->next = cur;
2557 if (cur->parent != NULL) {
2558 if (cur->parent->children == old)
2559 cur->parent->children = cur;
2560 if (cur->parent->last == old)
2561 cur->parent->last = cur;
2562 }
2563 old->next = old->prev = NULL;
2564 old->parent = NULL;
2565 return(old);
2566}
2567
2568/************************************************************************
2569 * *
2570 * Copy operations *
2571 * *
2572 ************************************************************************/
2573
2574/**
2575 * xmlCopyNamespace:
2576 * @cur: the namespace
2577 *
2578 * Do a copy of the namespace.
2579 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002580 * Returns: a new #xmlNsPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002581 */
2582xmlNsPtr
2583xmlCopyNamespace(xmlNsPtr cur) {
2584 xmlNsPtr ret;
2585
2586 if (cur == NULL) return(NULL);
2587 switch (cur->type) {
2588 case XML_LOCAL_NAMESPACE:
2589 ret = xmlNewNs(NULL, cur->href, cur->prefix);
2590 break;
2591 default:
2592#ifdef DEBUG_TREE
2593 xmlGenericError(xmlGenericErrorContext,
2594 "xmlCopyNamespace: invalid type %d\n", cur->type);
2595#endif
2596 return(NULL);
2597 }
2598 return(ret);
2599}
2600
2601/**
2602 * xmlCopyNamespaceList:
2603 * @cur: the first namespace
2604 *
2605 * Do a copy of an namespace list.
2606 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002607 * Returns: a new #xmlNsPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002608 */
2609xmlNsPtr
2610xmlCopyNamespaceList(xmlNsPtr cur) {
2611 xmlNsPtr ret = NULL;
2612 xmlNsPtr p = NULL,q;
2613
2614 while (cur != NULL) {
2615 q = xmlCopyNamespace(cur);
2616 if (p == NULL) {
2617 ret = p = q;
2618 } else {
2619 p->next = q;
2620 p = q;
2621 }
2622 cur = cur->next;
2623 }
2624 return(ret);
2625}
2626
2627static xmlNodePtr
2628xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent);
2629/**
2630 * xmlCopyProp:
2631 * @target: the element where the attribute will be grafted
2632 * @cur: the attribute
2633 *
2634 * Do a copy of the attribute.
2635 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002636 * Returns: a new #xmlAttrPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002637 */
2638xmlAttrPtr
2639xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) {
2640 xmlAttrPtr ret;
2641
2642 if (cur == NULL) return(NULL);
2643 if (target != NULL)
2644 ret = xmlNewDocProp(target->doc, cur->name, NULL);
2645 else if (cur->parent != NULL)
2646 ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL);
2647 else if (cur->children != NULL)
2648 ret = xmlNewDocProp(cur->children->doc, cur->name, NULL);
2649 else
2650 ret = xmlNewDocProp(NULL, cur->name, NULL);
2651 if (ret == NULL) return(NULL);
2652 ret->parent = target;
2653
2654 if ((cur->ns != NULL) && (target != NULL)) {
Daniel Veillard8107a222002-01-13 14:10:10 +00002655 xmlNsPtr ns;
2656 if (target->doc)
Owen Taylor3473f882001-02-23 17:55:21 +00002657 ns = xmlSearchNs(target->doc, target, cur->ns->prefix);
Daniel Veillard8107a222002-01-13 14:10:10 +00002658 else if (cur->doc) /* target may not yet have a doc : KPI */
2659 ns = xmlSearchNs(cur->doc, target, cur->ns->prefix);
2660 else
2661 ns = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002662 ret->ns = ns;
2663 } else
2664 ret->ns = NULL;
2665
2666 if (cur->children != NULL) {
2667 xmlNodePtr tmp;
2668
2669 ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret);
2670 ret->last = NULL;
2671 tmp = ret->children;
2672 while (tmp != NULL) {
2673 /* tmp->parent = (xmlNodePtr)ret; */
2674 if (tmp->next == NULL)
2675 ret->last = tmp;
2676 tmp = tmp->next;
2677 }
2678 }
2679 return(ret);
2680}
2681
2682/**
2683 * xmlCopyPropList:
2684 * @target: the element where the attributes will be grafted
2685 * @cur: the first attribute
2686 *
2687 * Do a copy of an attribute list.
2688 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002689 * Returns: a new #xmlAttrPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002690 */
2691xmlAttrPtr
2692xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) {
2693 xmlAttrPtr ret = NULL;
2694 xmlAttrPtr p = NULL,q;
2695
2696 while (cur != NULL) {
2697 q = xmlCopyProp(target, cur);
2698 if (p == NULL) {
2699 ret = p = q;
2700 } else {
2701 p->next = q;
2702 q->prev = p;
2703 p = q;
2704 }
2705 cur = cur->next;
2706 }
2707 return(ret);
2708}
2709
2710/*
Daniel Veillardd1640922001-12-17 15:30:10 +00002711 * NOTE about the CopyNode operations !
Owen Taylor3473f882001-02-23 17:55:21 +00002712 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002713 * They are split into external and internal parts for one
Owen Taylor3473f882001-02-23 17:55:21 +00002714 * tricky reason: namespaces. Doing a direct copy of a node
2715 * say RPM:Copyright without changing the namespace pointer to
2716 * something else can produce stale links. One way to do it is
2717 * to keep a reference counter but this doesn't work as soon
2718 * as one move the element or the subtree out of the scope of
2719 * the existing namespace. The actual solution seems to add
2720 * a copy of the namespace at the top of the copied tree if
2721 * not available in the subtree.
2722 * Hence two functions, the public front-end call the inner ones
2723 */
2724
2725static xmlNodePtr
2726xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent);
2727
2728static xmlNodePtr
Daniel Veillard3ec4c612001-08-28 20:39:49 +00002729xmlStaticCopyNode(const xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent,
Owen Taylor3473f882001-02-23 17:55:21 +00002730 int recursive) {
2731 xmlNodePtr ret;
2732
2733 if (node == NULL) return(NULL);
Daniel Veillard39196eb2001-06-19 18:09:42 +00002734 switch (node->type) {
2735 case XML_TEXT_NODE:
2736 case XML_CDATA_SECTION_NODE:
2737 case XML_ELEMENT_NODE:
2738 case XML_ENTITY_REF_NODE:
2739 case XML_ENTITY_NODE:
2740 case XML_PI_NODE:
2741 case XML_COMMENT_NODE:
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00002742 case XML_XINCLUDE_START:
2743 case XML_XINCLUDE_END:
2744 break;
2745 case XML_ATTRIBUTE_NODE:
2746 return((xmlNodePtr) xmlCopyProp(parent, (xmlAttrPtr) node));
2747 case XML_NAMESPACE_DECL:
2748 return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node));
2749
Daniel Veillard39196eb2001-06-19 18:09:42 +00002750 case XML_DOCUMENT_NODE:
2751 case XML_HTML_DOCUMENT_NODE:
2752#ifdef LIBXML_DOCB_ENABLED
2753 case XML_DOCB_DOCUMENT_NODE:
2754#endif
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00002755 return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, recursive));
Daniel Veillard39196eb2001-06-19 18:09:42 +00002756 case XML_DOCUMENT_TYPE_NODE:
2757 case XML_DOCUMENT_FRAG_NODE:
2758 case XML_NOTATION_NODE:
2759 case XML_DTD_NODE:
2760 case XML_ELEMENT_DECL:
2761 case XML_ATTRIBUTE_DECL:
2762 case XML_ENTITY_DECL:
2763 return(NULL);
2764 }
Daniel Veillardb33c2012001-04-25 12:59:04 +00002765
Owen Taylor3473f882001-02-23 17:55:21 +00002766 /*
2767 * Allocate a new node and fill the fields.
2768 */
2769 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2770 if (ret == NULL) {
2771 xmlGenericError(xmlGenericErrorContext,
2772 "xmlStaticCopyNode : malloc failed\n");
2773 return(NULL);
2774 }
2775 memset(ret, 0, sizeof(xmlNode));
2776 ret->type = node->type;
2777
2778 ret->doc = doc;
2779 ret->parent = parent;
2780 if (node->name == xmlStringText)
2781 ret->name = xmlStringText;
2782 else if (node->name == xmlStringTextNoenc)
2783 ret->name = xmlStringTextNoenc;
2784 else if (node->name == xmlStringComment)
2785 ret->name = xmlStringComment;
2786 else if (node->name != NULL)
2787 ret->name = xmlStrdup(node->name);
Daniel Veillard7db37732001-07-12 01:20:08 +00002788 if ((node->type != XML_ELEMENT_NODE) &&
2789 (node->content != NULL) &&
2790 (node->type != XML_ENTITY_REF_NODE) &&
2791 (node->type != XML_XINCLUDE_END) &&
2792 (node->type != XML_XINCLUDE_START)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002793#ifndef XML_USE_BUFFER_CONTENT
2794 ret->content = xmlStrdup(node->content);
2795#else
2796 ret->content = xmlBufferCreateSize(xmlBufferLength(node->content));
2797 xmlBufferSetAllocationScheme(ret->content,
2798 xmlGetBufferAllocationScheme());
2799 xmlBufferAdd(ret->content,
2800 xmlBufferContent(node->content),
2801 xmlBufferLength(node->content));
2802#endif
Daniel Veillard8107a222002-01-13 14:10:10 +00002803 }else{
2804 if (node->type == XML_ELEMENT_NODE)
2805 ret->content = (void*)(long) node->content;
Owen Taylor3473f882001-02-23 17:55:21 +00002806 }
Daniel Veillardacb2bda2002-01-13 16:15:43 +00002807 if (parent != NULL) {
2808 xmlNodePtr tmp;
2809
2810 tmp = xmlAddChild(parent, ret);
2811 /* node could have coalesced */
2812 if (tmp != ret)
2813 return(tmp);
2814 }
Owen Taylor3473f882001-02-23 17:55:21 +00002815
2816 if (!recursive) return(ret);
2817 if (node->nsDef != NULL)
2818 ret->nsDef = xmlCopyNamespaceList(node->nsDef);
2819
2820 if (node->ns != NULL) {
2821 xmlNsPtr ns;
2822
2823 ns = xmlSearchNs(doc, ret, node->ns->prefix);
2824 if (ns == NULL) {
2825 /*
2826 * Humm, we are copying an element whose namespace is defined
2827 * out of the new tree scope. Search it in the original tree
2828 * and add it at the top of the new tree
2829 */
2830 ns = xmlSearchNs(node->doc, node, node->ns->prefix);
2831 if (ns != NULL) {
2832 xmlNodePtr root = ret;
2833
2834 while (root->parent != NULL) root = root->parent;
Daniel Veillarde82a9922001-04-22 12:12:58 +00002835 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00002836 }
2837 } else {
2838 /*
2839 * reference the existing namespace definition in our own tree.
2840 */
2841 ret->ns = ns;
2842 }
2843 }
2844 if (node->properties != NULL)
2845 ret->properties = xmlCopyPropList(ret, node->properties);
Daniel Veillardb33c2012001-04-25 12:59:04 +00002846 if (node->type == XML_ENTITY_REF_NODE) {
2847 if ((doc == NULL) || (node->doc != doc)) {
2848 /*
2849 * The copied node will go into a separate document, so
Daniel Veillardd1640922001-12-17 15:30:10 +00002850 * to avoid dangling references to the ENTITY_DECL node
Daniel Veillardb33c2012001-04-25 12:59:04 +00002851 * we cannot keep the reference. Try to find it in the
2852 * target document.
2853 */
2854 ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name);
2855 } else {
2856 ret->children = node->children;
2857 }
Daniel Veillard0ec98632001-11-14 15:04:32 +00002858 ret->last = ret->children;
2859 } else if (node->children != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002860 ret->children = xmlStaticCopyNodeList(node->children, doc, ret);
Daniel Veillard0ec98632001-11-14 15:04:32 +00002861 UPDATE_LAST_CHILD_AND_PARENT(ret)
2862 }
Owen Taylor3473f882001-02-23 17:55:21 +00002863 return(ret);
2864}
2865
2866static xmlNodePtr
2867xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) {
2868 xmlNodePtr ret = NULL;
2869 xmlNodePtr p = NULL,q;
2870
2871 while (node != NULL) {
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00002872 if (node->type == XML_DTD_NODE ) {
Daniel Veillard4497e692001-06-09 14:19:02 +00002873 if (doc == NULL) {
2874 node = node->next;
2875 continue;
2876 }
Daniel Veillardb33c2012001-04-25 12:59:04 +00002877 if (doc->intSubset == NULL) {
2878 q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node );
2879 q->doc = doc;
2880 q->parent = parent;
2881 doc->intSubset = (xmlDtdPtr) q;
2882 } else {
2883 q = (xmlNodePtr) doc->intSubset;
2884 }
2885 } else
2886 q = xmlStaticCopyNode(node, doc, parent, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00002887 if (ret == NULL) {
2888 q->prev = NULL;
2889 ret = p = q;
Daniel Veillardacb2bda2002-01-13 16:15:43 +00002890 } else if (p != q) {
2891 /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */
Owen Taylor3473f882001-02-23 17:55:21 +00002892 p->next = q;
2893 q->prev = p;
2894 p = q;
2895 }
2896 node = node->next;
2897 }
2898 return(ret);
2899}
2900
2901/**
2902 * xmlCopyNode:
2903 * @node: the node
2904 * @recursive: if 1 do a recursive copy.
2905 *
2906 * Do a copy of the node.
2907 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002908 * Returns: a new #xmlNodePtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002909 */
2910xmlNodePtr
Daniel Veillard3ec4c612001-08-28 20:39:49 +00002911xmlCopyNode(const xmlNodePtr node, int recursive) {
Owen Taylor3473f882001-02-23 17:55:21 +00002912 xmlNodePtr ret;
2913
2914 ret = xmlStaticCopyNode(node, NULL, NULL, recursive);
2915 return(ret);
2916}
2917
2918/**
Daniel Veillard82daa812001-04-12 08:55:36 +00002919 * xmlDocCopyNode:
2920 * @node: the node
Daniel Veillardd1640922001-12-17 15:30:10 +00002921 * @doc: the document
Daniel Veillard82daa812001-04-12 08:55:36 +00002922 * @recursive: if 1 do a recursive copy.
2923 *
2924 * Do a copy of the node to a given document.
2925 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002926 * Returns: a new #xmlNodePtr, or NULL in case of error.
Daniel Veillard82daa812001-04-12 08:55:36 +00002927 */
2928xmlNodePtr
Daniel Veillard3ec4c612001-08-28 20:39:49 +00002929xmlDocCopyNode(const xmlNodePtr node, xmlDocPtr doc, int recursive) {
Daniel Veillard82daa812001-04-12 08:55:36 +00002930 xmlNodePtr ret;
2931
2932 ret = xmlStaticCopyNode(node, doc, NULL, recursive);
2933 return(ret);
2934}
2935
2936/**
Owen Taylor3473f882001-02-23 17:55:21 +00002937 * xmlCopyNodeList:
2938 * @node: the first node in the list.
2939 *
2940 * Do a recursive copy of the node list.
2941 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002942 * Returns: a new #xmlNodePtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002943 */
Daniel Veillard3ec4c612001-08-28 20:39:49 +00002944xmlNodePtr xmlCopyNodeList(const xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00002945 xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL);
2946 return(ret);
2947}
2948
2949/**
Owen Taylor3473f882001-02-23 17:55:21 +00002950 * xmlCopyDtd:
2951 * @dtd: the dtd
2952 *
2953 * Do a copy of the dtd.
2954 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002955 * Returns: a new #xmlDtdPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002956 */
2957xmlDtdPtr
2958xmlCopyDtd(xmlDtdPtr dtd) {
2959 xmlDtdPtr ret;
2960
2961 if (dtd == NULL) return(NULL);
2962 ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID);
2963 if (ret == NULL) return(NULL);
2964 if (dtd->entities != NULL)
2965 ret->entities = (void *) xmlCopyEntitiesTable(
2966 (xmlEntitiesTablePtr) dtd->entities);
2967 if (dtd->notations != NULL)
2968 ret->notations = (void *) xmlCopyNotationTable(
2969 (xmlNotationTablePtr) dtd->notations);
2970 if (dtd->elements != NULL)
2971 ret->elements = (void *) xmlCopyElementTable(
2972 (xmlElementTablePtr) dtd->elements);
2973 if (dtd->attributes != NULL)
2974 ret->attributes = (void *) xmlCopyAttributeTable(
2975 (xmlAttributeTablePtr) dtd->attributes);
2976 return(ret);
2977}
2978
2979/**
2980 * xmlCopyDoc:
2981 * @doc: the document
2982 * @recursive: if 1 do a recursive copy.
2983 *
2984 * Do a copy of the document info. If recursive, the content tree will
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002985 * be copied too as well as DTD, namespaces and entities.
Owen Taylor3473f882001-02-23 17:55:21 +00002986 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002987 * Returns: a new #xmlDocPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002988 */
2989xmlDocPtr
2990xmlCopyDoc(xmlDocPtr doc, int recursive) {
2991 xmlDocPtr ret;
2992
2993 if (doc == NULL) return(NULL);
2994 ret = xmlNewDoc(doc->version);
2995 if (ret == NULL) return(NULL);
2996 if (doc->name != NULL)
2997 ret->name = xmlMemStrdup(doc->name);
2998 if (doc->encoding != NULL)
2999 ret->encoding = xmlStrdup(doc->encoding);
3000 ret->charset = doc->charset;
3001 ret->compression = doc->compression;
3002 ret->standalone = doc->standalone;
3003 if (!recursive) return(ret);
3004
Daniel Veillardb33c2012001-04-25 12:59:04 +00003005 ret->last = NULL;
3006 ret->children = NULL;
3007 if (doc->intSubset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00003008 ret->intSubset = xmlCopyDtd(doc->intSubset);
Daniel Veillardb33c2012001-04-25 12:59:04 +00003009 ret->intSubset->doc = ret;
3010 ret->intSubset->parent = ret;
3011 }
Owen Taylor3473f882001-02-23 17:55:21 +00003012 if (doc->oldNs != NULL)
3013 ret->oldNs = xmlCopyNamespaceList(doc->oldNs);
3014 if (doc->children != NULL) {
3015 xmlNodePtr tmp;
Daniel Veillardb33c2012001-04-25 12:59:04 +00003016
3017 ret->children = xmlStaticCopyNodeList(doc->children, ret,
3018 (xmlNodePtr)ret);
Owen Taylor3473f882001-02-23 17:55:21 +00003019 ret->last = NULL;
3020 tmp = ret->children;
3021 while (tmp != NULL) {
3022 if (tmp->next == NULL)
3023 ret->last = tmp;
3024 tmp = tmp->next;
3025 }
3026 }
3027 return(ret);
3028}
3029
3030/************************************************************************
3031 * *
3032 * Content access functions *
3033 * *
3034 ************************************************************************/
3035
3036/**
Daniel Veillard8faa7832001-11-26 15:58:08 +00003037 * xmlGetLineNo:
3038 * @node : valid node
3039 *
3040 * Get line number of node. this requires activation of this option
Daniel Veillardd1640922001-12-17 15:30:10 +00003041 * before invoking the parser by calling xmlLineNumbersDefault(1)
Daniel Veillard8faa7832001-11-26 15:58:08 +00003042 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003043 * Returns the line number if successful, -1 otherwise
Daniel Veillard8faa7832001-11-26 15:58:08 +00003044 */
3045long
3046xmlGetLineNo(xmlNodePtr node)
3047{
3048 long result = -1;
3049
3050 if (!node)
3051 return result;
3052 if (node->type == XML_ELEMENT_NODE)
3053 result = (long) node->content;
3054 else if ((node->prev != NULL) &&
3055 ((node->prev->type == XML_ELEMENT_NODE) ||
3056 (node->prev->type == XML_TEXT_NODE)))
3057 result = xmlGetLineNo(node->prev);
3058 else if ((node->parent != NULL) &&
3059 ((node->parent->type == XML_ELEMENT_NODE) ||
3060 (node->parent->type == XML_TEXT_NODE)))
3061 result = xmlGetLineNo(node->parent);
3062
3063 return result;
3064}
3065
3066/**
3067 * xmlGetNodePath:
3068 * @node: a node
3069 *
3070 * Build a structure based Path for the given node
3071 *
3072 * Returns the new path or NULL in case of error. The caller must free
3073 * the returned string
3074 */
3075xmlChar *
3076xmlGetNodePath(xmlNodePtr node)
3077{
3078 xmlNodePtr cur, tmp, next;
3079 xmlChar *buffer = NULL, *temp;
3080 size_t buf_len;
3081 xmlChar *buf;
3082 char sep;
3083 const char *name;
3084 char nametemp[100];
3085 int occur = 0;
3086
3087 if (node == NULL)
3088 return (NULL);
3089
3090 buf_len = 500;
3091 buffer = (xmlChar *) xmlMalloc(buf_len * sizeof(xmlChar));
3092 if (buffer == NULL)
3093 return (NULL);
3094 buf = (xmlChar *) xmlMalloc(buf_len * sizeof(xmlChar));
3095 if (buf == NULL) {
3096 xmlFree(buffer);
3097 return (NULL);
3098 }
3099
3100 buffer[0] = 0;
3101 cur = node;
3102 do {
3103 name = "";
3104 sep = '?';
3105 occur = 0;
3106 if ((cur->type == XML_DOCUMENT_NODE) ||
3107 (cur->type == XML_HTML_DOCUMENT_NODE)) {
3108 if (buffer[0] == '/')
3109 break;
3110 sep = '/';
3111 next = NULL;
3112 } else if (cur->type == XML_ELEMENT_NODE) {
3113 sep = '/';
3114 name = (const char *) cur->name;
3115 if (cur->ns) {
3116 snprintf(nametemp, sizeof(nametemp) - 1,
3117 "%s:%s", cur->ns->prefix, cur->name);
3118 nametemp[sizeof(nametemp) - 1] = 0;
3119 name = nametemp;
3120 }
3121 next = cur->parent;
3122
3123 /*
3124 * Thumbler index computation
3125 */
3126 tmp = cur->prev;
3127 while (tmp != NULL) {
3128 if (xmlStrEqual(cur->name, tmp->name))
3129 occur++;
3130 tmp = tmp->prev;
3131 }
3132 if (occur == 0) {
3133 tmp = cur->next;
3134 while (tmp != NULL) {
3135 if (xmlStrEqual(cur->name, tmp->name))
3136 occur++;
3137 tmp = tmp->next;
3138 }
3139 if (occur != 0)
3140 occur = 1;
3141 } else
3142 occur++;
3143 } else if (cur->type == XML_ATTRIBUTE_NODE) {
3144 sep = '@';
3145 name = (const char *) (((xmlAttrPtr) cur)->name);
3146 next = ((xmlAttrPtr) cur)->parent;
3147 } else {
3148 next = cur->parent;
3149 }
3150
3151 /*
3152 * Make sure there is enough room
3153 */
3154 if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) {
3155 buf_len =
3156 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20;
3157 temp = (xmlChar *) xmlRealloc(buffer, buf_len);
3158 if (temp == NULL) {
3159 xmlFree(buf);
3160 xmlFree(buffer);
3161 return (NULL);
3162 }
3163 buffer = temp;
3164 temp = (xmlChar *) xmlRealloc(buf, buf_len);
3165 if (temp == NULL) {
3166 xmlFree(buf);
3167 xmlFree(buffer);
3168 return (NULL);
3169 }
3170 buf = temp;
3171 }
3172 if (occur == 0)
3173 snprintf((char *) buf, buf_len, "%c%s%s",
3174 sep, name, (char *) buffer);
3175 else
3176 snprintf((char *) buf, buf_len, "%c%s[%d]%s",
3177 sep, name, occur, (char *) buffer);
3178 snprintf((char *) buffer, buf_len, "%s", buf);
3179 cur = next;
3180 } while (cur != NULL);
3181 xmlFree(buf);
3182 return (buffer);
3183}
3184
3185/**
Owen Taylor3473f882001-02-23 17:55:21 +00003186 * xmlDocGetRootElement:
3187 * @doc: the document
3188 *
3189 * Get the root element of the document (doc->children is a list
3190 * containing possibly comments, PIs, etc ...).
3191 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003192 * Returns the #xmlNodePtr for the root or NULL
Owen Taylor3473f882001-02-23 17:55:21 +00003193 */
3194xmlNodePtr
3195xmlDocGetRootElement(xmlDocPtr doc) {
3196 xmlNodePtr ret;
3197
3198 if (doc == NULL) return(NULL);
3199 ret = doc->children;
3200 while (ret != NULL) {
3201 if (ret->type == XML_ELEMENT_NODE)
3202 return(ret);
3203 ret = ret->next;
3204 }
3205 return(ret);
3206}
3207
3208/**
3209 * xmlDocSetRootElement:
3210 * @doc: the document
3211 * @root: the new document root element
3212 *
3213 * Set the root element of the document (doc->children is a list
3214 * containing possibly comments, PIs, etc ...).
3215 *
3216 * Returns the old root element if any was found
3217 */
3218xmlNodePtr
3219xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) {
3220 xmlNodePtr old = NULL;
3221
3222 if (doc == NULL) return(NULL);
3223 old = doc->children;
3224 while (old != NULL) {
3225 if (old->type == XML_ELEMENT_NODE)
3226 break;
3227 old = old->next;
3228 }
3229 if (old == NULL) {
3230 if (doc->children == NULL) {
3231 doc->children = root;
3232 doc->last = root;
3233 } else {
3234 xmlAddSibling(doc->children, root);
3235 }
3236 } else {
3237 xmlReplaceNode(old, root);
3238 }
3239 return(old);
3240}
3241
3242/**
3243 * xmlNodeSetLang:
3244 * @cur: the node being changed
Daniel Veillardd1640922001-12-17 15:30:10 +00003245 * @lang: the language description
Owen Taylor3473f882001-02-23 17:55:21 +00003246 *
3247 * Set the language of a node, i.e. the values of the xml:lang
3248 * attribute.
3249 */
3250void
3251xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) {
3252 if (cur == NULL) return;
3253 switch(cur->type) {
3254 case XML_TEXT_NODE:
3255 case XML_CDATA_SECTION_NODE:
3256 case XML_COMMENT_NODE:
3257 case XML_DOCUMENT_NODE:
3258 case XML_DOCUMENT_TYPE_NODE:
3259 case XML_DOCUMENT_FRAG_NODE:
3260 case XML_NOTATION_NODE:
3261 case XML_HTML_DOCUMENT_NODE:
3262 case XML_DTD_NODE:
3263 case XML_ELEMENT_DECL:
3264 case XML_ATTRIBUTE_DECL:
3265 case XML_ENTITY_DECL:
3266 case XML_PI_NODE:
3267 case XML_ENTITY_REF_NODE:
3268 case XML_ENTITY_NODE:
3269 case XML_NAMESPACE_DECL:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003270#ifdef LIBXML_DOCB_ENABLED
3271 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003272#endif
3273 case XML_XINCLUDE_START:
3274 case XML_XINCLUDE_END:
3275 return;
3276 case XML_ELEMENT_NODE:
3277 case XML_ATTRIBUTE_NODE:
3278 break;
3279 }
3280 xmlSetProp(cur, BAD_CAST "xml:lang", lang);
3281}
3282
3283/**
3284 * xmlNodeGetLang:
3285 * @cur: the node being checked
3286 *
3287 * Searches the language of a node, i.e. the values of the xml:lang
3288 * attribute or the one carried by the nearest ancestor.
3289 *
3290 * Returns a pointer to the lang value, or NULL if not found
3291 * It's up to the caller to free the memory.
3292 */
3293xmlChar *
3294xmlNodeGetLang(xmlNodePtr cur) {
3295 xmlChar *lang;
3296
3297 while (cur != NULL) {
Daniel Veillardc17337c2001-05-09 10:51:31 +00003298 lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE);
Owen Taylor3473f882001-02-23 17:55:21 +00003299 if (lang != NULL)
3300 return(lang);
3301 cur = cur->parent;
3302 }
3303 return(NULL);
3304}
3305
3306
3307/**
3308 * xmlNodeSetSpacePreserve:
3309 * @cur: the node being changed
3310 * @val: the xml:space value ("0": default, 1: "preserve")
3311 *
3312 * Set (or reset) the space preserving behaviour of a node, i.e. the
3313 * value of the xml:space attribute.
3314 */
3315void
3316xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) {
3317 if (cur == NULL) return;
3318 switch(cur->type) {
3319 case XML_TEXT_NODE:
3320 case XML_CDATA_SECTION_NODE:
3321 case XML_COMMENT_NODE:
3322 case XML_DOCUMENT_NODE:
3323 case XML_DOCUMENT_TYPE_NODE:
3324 case XML_DOCUMENT_FRAG_NODE:
3325 case XML_NOTATION_NODE:
3326 case XML_HTML_DOCUMENT_NODE:
3327 case XML_DTD_NODE:
3328 case XML_ELEMENT_DECL:
3329 case XML_ATTRIBUTE_DECL:
3330 case XML_ENTITY_DECL:
3331 case XML_PI_NODE:
3332 case XML_ENTITY_REF_NODE:
3333 case XML_ENTITY_NODE:
3334 case XML_NAMESPACE_DECL:
3335 case XML_XINCLUDE_START:
3336 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003337#ifdef LIBXML_DOCB_ENABLED
3338 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003339#endif
3340 return;
3341 case XML_ELEMENT_NODE:
3342 case XML_ATTRIBUTE_NODE:
3343 break;
3344 }
3345 switch (val) {
3346 case 0:
3347 xmlSetProp(cur, BAD_CAST "xml:space", BAD_CAST "default");
3348 break;
3349 case 1:
3350 xmlSetProp(cur, BAD_CAST "xml:space",
3351 BAD_CAST "preserve");
3352 break;
3353 }
3354}
3355
3356/**
3357 * xmlNodeGetSpacePreserve:
3358 * @cur: the node being checked
3359 *
3360 * Searches the space preserving behaviour of a node, i.e. the values
3361 * of the xml:space attribute or the one carried by the nearest
3362 * ancestor.
3363 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003364 * Returns -1 if xml:space is not inherited, 0 if "default", 1 if "preserve"
Owen Taylor3473f882001-02-23 17:55:21 +00003365 */
3366int
3367xmlNodeGetSpacePreserve(xmlNodePtr cur) {
3368 xmlChar *space;
3369
3370 while (cur != NULL) {
3371 space = xmlGetProp(cur, BAD_CAST "xml:space");
3372 if (space != NULL) {
3373 if (xmlStrEqual(space, BAD_CAST "preserve")) {
3374 xmlFree(space);
3375 return(1);
3376 }
3377 if (xmlStrEqual(space, BAD_CAST "default")) {
3378 xmlFree(space);
3379 return(0);
3380 }
3381 xmlFree(space);
3382 }
3383 cur = cur->parent;
3384 }
3385 return(-1);
3386}
3387
3388/**
3389 * xmlNodeSetName:
3390 * @cur: the node being changed
3391 * @name: the new tag name
3392 *
3393 * Set (or reset) the name of a node.
3394 */
3395void
3396xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) {
3397 if (cur == NULL) return;
3398 if (name == NULL) return;
3399 switch(cur->type) {
3400 case XML_TEXT_NODE:
3401 case XML_CDATA_SECTION_NODE:
3402 case XML_COMMENT_NODE:
3403 case XML_DOCUMENT_TYPE_NODE:
3404 case XML_DOCUMENT_FRAG_NODE:
3405 case XML_NOTATION_NODE:
3406 case XML_HTML_DOCUMENT_NODE:
3407 case XML_NAMESPACE_DECL:
3408 case XML_XINCLUDE_START:
3409 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003410#ifdef LIBXML_DOCB_ENABLED
3411 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003412#endif
3413 return;
3414 case XML_ELEMENT_NODE:
3415 case XML_ATTRIBUTE_NODE:
3416 case XML_PI_NODE:
3417 case XML_ENTITY_REF_NODE:
3418 case XML_ENTITY_NODE:
3419 case XML_DTD_NODE:
3420 case XML_DOCUMENT_NODE:
3421 case XML_ELEMENT_DECL:
3422 case XML_ATTRIBUTE_DECL:
3423 case XML_ENTITY_DECL:
3424 break;
3425 }
3426 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
3427 cur->name = xmlStrdup(name);
3428}
3429
3430/**
3431 * xmlNodeSetBase:
3432 * @cur: the node being changed
3433 * @uri: the new base URI
3434 *
3435 * Set (or reset) the base URI of a node, i.e. the value of the
3436 * xml:base attribute.
3437 */
3438void
3439xmlNodeSetBase(xmlNodePtr cur, xmlChar* uri) {
3440 if (cur == NULL) return;
3441 switch(cur->type) {
3442 case XML_TEXT_NODE:
3443 case XML_CDATA_SECTION_NODE:
3444 case XML_COMMENT_NODE:
3445 case XML_DOCUMENT_NODE:
3446 case XML_DOCUMENT_TYPE_NODE:
3447 case XML_DOCUMENT_FRAG_NODE:
3448 case XML_NOTATION_NODE:
3449 case XML_HTML_DOCUMENT_NODE:
3450 case XML_DTD_NODE:
3451 case XML_ELEMENT_DECL:
3452 case XML_ATTRIBUTE_DECL:
3453 case XML_ENTITY_DECL:
3454 case XML_PI_NODE:
3455 case XML_ENTITY_REF_NODE:
3456 case XML_ENTITY_NODE:
3457 case XML_NAMESPACE_DECL:
3458 case XML_XINCLUDE_START:
3459 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003460#ifdef LIBXML_DOCB_ENABLED
3461 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003462#endif
3463 return;
3464 case XML_ELEMENT_NODE:
3465 case XML_ATTRIBUTE_NODE:
3466 break;
3467 }
3468 xmlSetProp(cur, BAD_CAST "xml:base", uri);
3469}
3470
3471/**
Owen Taylor3473f882001-02-23 17:55:21 +00003472 * xmlNodeGetBase:
3473 * @doc: the document the node pertains to
3474 * @cur: the node being checked
3475 *
3476 * Searches for the BASE URL. The code should work on both XML
3477 * and HTML document even if base mechanisms are completely different.
3478 * It returns the base as defined in RFC 2396 sections
3479 * 5.1.1. Base URI within Document Content
3480 * and
3481 * 5.1.2. Base URI from the Encapsulating Entity
3482 * However it does not return the document base (5.1.3), use
3483 * xmlDocumentGetBase() for this
3484 *
3485 * Returns a pointer to the base URL, or NULL if not found
3486 * It's up to the caller to free the memory.
3487 */
3488xmlChar *
3489xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00003490 xmlChar *oldbase = NULL;
3491 xmlChar *base, *newbase;
Owen Taylor3473f882001-02-23 17:55:21 +00003492
3493 if ((cur == NULL) && (doc == NULL))
3494 return(NULL);
3495 if (doc == NULL) doc = cur->doc;
3496 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
3497 cur = doc->children;
3498 while ((cur != NULL) && (cur->name != NULL)) {
3499 if (cur->type != XML_ELEMENT_NODE) {
3500 cur = cur->next;
3501 continue;
3502 }
3503 if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) {
3504 cur = cur->children;
3505 continue;
3506 }
3507 if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) {
3508 cur = cur->children;
3509 continue;
3510 }
3511 if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) {
3512 return(xmlGetProp(cur, BAD_CAST "href"));
3513 }
3514 cur = cur->next;
3515 }
3516 return(NULL);
3517 }
3518 while (cur != NULL) {
3519 if (cur->type == XML_ENTITY_DECL) {
3520 xmlEntityPtr ent = (xmlEntityPtr) cur;
3521 return(xmlStrdup(ent->URI));
3522 }
Daniel Veillard42596ad2001-05-22 16:57:14 +00003523 if (cur->type == XML_ELEMENT_NODE) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00003524 base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE);
Daniel Veillard42596ad2001-05-22 16:57:14 +00003525 if (base != NULL) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00003526 if (oldbase != NULL) {
3527 newbase = xmlBuildURI(oldbase, base);
3528 if (newbase != NULL) {
3529 xmlFree(oldbase);
3530 xmlFree(base);
3531 oldbase = newbase;
3532 } else {
3533 xmlFree(oldbase);
3534 xmlFree(base);
3535 return(NULL);
3536 }
3537 } else {
3538 oldbase = base;
3539 }
3540 if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) ||
3541 (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) ||
3542 (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4)))
3543 return(oldbase);
Daniel Veillard42596ad2001-05-22 16:57:14 +00003544 }
3545 }
Owen Taylor3473f882001-02-23 17:55:21 +00003546 cur = cur->parent;
3547 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00003548 if ((doc != NULL) && (doc->URL != NULL)) {
3549 if (oldbase == NULL)
3550 return(xmlStrdup(doc->URL));
3551 newbase = xmlBuildURI(oldbase, doc->URL);
3552 xmlFree(oldbase);
3553 return(newbase);
3554 }
3555 return(oldbase);
Owen Taylor3473f882001-02-23 17:55:21 +00003556}
3557
3558/**
3559 * xmlNodeGetContent:
3560 * @cur: the node being read
3561 *
3562 * Read the value of a node, this can be either the text carried
3563 * directly by this node if it's a TEXT node or the aggregate string
3564 * of the values carried by this node child's (TEXT and ENTITY_REF).
Daniel Veillardd1640922001-12-17 15:30:10 +00003565 * Entity references are substituted.
3566 * Returns a new #xmlChar * or NULL if no content is available.
Owen Taylor3473f882001-02-23 17:55:21 +00003567 * It's up to the caller to free the memory.
3568 */
3569xmlChar *
3570xmlNodeGetContent(xmlNodePtr cur) {
3571 if (cur == NULL) return(NULL);
3572 switch (cur->type) {
3573 case XML_DOCUMENT_FRAG_NODE:
3574 case XML_ELEMENT_NODE: {
3575 xmlNodePtr tmp = cur;
3576 xmlBufferPtr buffer;
3577 xmlChar *ret;
3578
3579 buffer = xmlBufferCreate();
3580 if (buffer == NULL)
3581 return(NULL);
3582 while (tmp != NULL) {
3583 switch (tmp->type) {
Daniel Veillard2d703722001-05-30 18:32:34 +00003584 case XML_CDATA_SECTION_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003585 case XML_TEXT_NODE:
3586 if (tmp->content != NULL)
3587#ifndef XML_USE_BUFFER_CONTENT
3588 xmlBufferCat(buffer, tmp->content);
3589#else
3590 xmlBufferCat(buffer,
3591 xmlBufferContent(tmp->content));
3592#endif
3593 break;
3594 case XML_ENTITY_REF_NODE: {
3595 xmlEntityPtr ent;
3596
3597 ent = xmlGetDocEntity(cur->doc, tmp->name);
3598 if (ent != NULL)
3599 xmlBufferCat(buffer, ent->content);
3600 }
3601 default:
3602 break;
3603 }
3604 /*
3605 * Skip to next node
3606 */
3607 if (tmp->children != NULL) {
3608 if (tmp->children->type != XML_ENTITY_DECL) {
3609 tmp = tmp->children;
3610 continue;
3611 }
3612 }
Daniel Veillard6c831202001-03-07 15:57:53 +00003613 if (tmp == cur)
3614 break;
3615
Owen Taylor3473f882001-02-23 17:55:21 +00003616 if (tmp->next != NULL) {
3617 tmp = tmp->next;
3618 continue;
3619 }
3620
3621 do {
3622 tmp = tmp->parent;
3623 if (tmp == NULL)
3624 break;
Daniel Veillard6c831202001-03-07 15:57:53 +00003625 if (tmp == cur) {
Owen Taylor3473f882001-02-23 17:55:21 +00003626 tmp = NULL;
3627 break;
3628 }
3629 if (tmp->next != NULL) {
3630 tmp = tmp->next;
3631 break;
3632 }
3633 } while (tmp != NULL);
3634 }
3635 ret = buffer->content;
3636 buffer->content = NULL;
3637 xmlBufferFree(buffer);
3638 return(ret);
3639 }
3640 case XML_ATTRIBUTE_NODE: {
3641 xmlAttrPtr attr = (xmlAttrPtr) cur;
3642 if (attr->parent != NULL)
3643 return(xmlNodeListGetString(attr->parent->doc, attr->children, 1));
3644 else
3645 return(xmlNodeListGetString(NULL, attr->children, 1));
3646 break;
3647 }
3648 case XML_COMMENT_NODE:
3649 case XML_PI_NODE:
3650 if (cur->content != NULL)
3651#ifndef XML_USE_BUFFER_CONTENT
3652 return(xmlStrdup(cur->content));
3653#else
3654 return(xmlStrdup(xmlBufferContent(cur->content)));
3655#endif
3656 return(NULL);
3657 case XML_ENTITY_REF_NODE:
3658 /*
3659 * Locate the entity, and get it's content
3660 * @@@
3661 */
3662 return(NULL);
3663 case XML_ENTITY_NODE:
3664 case XML_DOCUMENT_NODE:
3665 case XML_HTML_DOCUMENT_NODE:
3666 case XML_DOCUMENT_TYPE_NODE:
3667 case XML_NOTATION_NODE:
3668 case XML_DTD_NODE:
3669 case XML_XINCLUDE_START:
3670 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003671#ifdef LIBXML_DOCB_ENABLED
3672 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003673#endif
3674 return(NULL);
3675 case XML_NAMESPACE_DECL:
3676 return(xmlStrdup(((xmlNsPtr)cur)->href));
3677 case XML_ELEMENT_DECL:
3678 /* TODO !!! */
3679 return(NULL);
3680 case XML_ATTRIBUTE_DECL:
3681 /* TODO !!! */
3682 return(NULL);
3683 case XML_ENTITY_DECL:
3684 /* TODO !!! */
3685 return(NULL);
3686 case XML_CDATA_SECTION_NODE:
3687 case XML_TEXT_NODE:
3688 if (cur->content != NULL)
3689#ifndef XML_USE_BUFFER_CONTENT
3690 return(xmlStrdup(cur->content));
3691#else
3692 return(xmlStrdup(xmlBufferContent(cur->content)));
3693#endif
3694 return(NULL);
3695 }
3696 return(NULL);
3697}
3698
3699/**
3700 * xmlNodeSetContent:
3701 * @cur: the node being modified
3702 * @content: the new value of the content
3703 *
3704 * Replace the content of a node.
3705 */
3706void
3707xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) {
3708 if (cur == NULL) {
3709#ifdef DEBUG_TREE
3710 xmlGenericError(xmlGenericErrorContext,
3711 "xmlNodeSetContent : node == NULL\n");
3712#endif
3713 return;
3714 }
3715 switch (cur->type) {
3716 case XML_DOCUMENT_FRAG_NODE:
3717 case XML_ELEMENT_NODE:
Daniel Veillard2c748c62002-01-16 15:37:50 +00003718 case XML_ATTRIBUTE_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003719 if (cur->children != NULL) xmlFreeNodeList(cur->children);
3720 cur->children = xmlStringGetNodeList(cur->doc, content);
3721 UPDATE_LAST_CHILD_AND_PARENT(cur)
3722 break;
Owen Taylor3473f882001-02-23 17:55:21 +00003723 case XML_TEXT_NODE:
3724 case XML_CDATA_SECTION_NODE:
3725 case XML_ENTITY_REF_NODE:
3726 case XML_ENTITY_NODE:
3727 case XML_PI_NODE:
3728 case XML_COMMENT_NODE:
3729 if (cur->content != NULL) {
3730#ifndef XML_USE_BUFFER_CONTENT
3731 xmlFree(cur->content);
3732#else
3733 xmlBufferFree(cur->content);
3734#endif
3735 }
3736 if (cur->children != NULL) xmlFreeNodeList(cur->children);
3737 cur->last = cur->children = NULL;
3738 if (content != NULL) {
3739#ifndef XML_USE_BUFFER_CONTENT
3740 cur->content = xmlStrdup(content);
3741#else
3742 cur->content = xmlBufferCreateSize(0);
3743 xmlBufferSetAllocationScheme(cur->content,
3744 xmlGetBufferAllocationScheme());
3745 xmlBufferAdd(cur->content, content, -1);
3746#endif
3747 } else
3748 cur->content = NULL;
3749 break;
3750 case XML_DOCUMENT_NODE:
3751 case XML_HTML_DOCUMENT_NODE:
3752 case XML_DOCUMENT_TYPE_NODE:
3753 case XML_XINCLUDE_START:
3754 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003755#ifdef LIBXML_DOCB_ENABLED
3756 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003757#endif
3758 break;
3759 case XML_NOTATION_NODE:
3760 break;
3761 case XML_DTD_NODE:
3762 break;
3763 case XML_NAMESPACE_DECL:
3764 break;
3765 case XML_ELEMENT_DECL:
3766 /* TODO !!! */
3767 break;
3768 case XML_ATTRIBUTE_DECL:
3769 /* TODO !!! */
3770 break;
3771 case XML_ENTITY_DECL:
3772 /* TODO !!! */
3773 break;
3774 }
3775}
3776
3777/**
3778 * xmlNodeSetContentLen:
3779 * @cur: the node being modified
3780 * @content: the new value of the content
3781 * @len: the size of @content
3782 *
3783 * Replace the content of a node.
3784 */
3785void
3786xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
3787 if (cur == NULL) {
3788#ifdef DEBUG_TREE
3789 xmlGenericError(xmlGenericErrorContext,
3790 "xmlNodeSetContentLen : node == NULL\n");
3791#endif
3792 return;
3793 }
3794 switch (cur->type) {
3795 case XML_DOCUMENT_FRAG_NODE:
3796 case XML_ELEMENT_NODE:
Daniel Veillard2c748c62002-01-16 15:37:50 +00003797 case XML_ATTRIBUTE_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003798 if (cur->children != NULL) xmlFreeNodeList(cur->children);
3799 cur->children = xmlStringLenGetNodeList(cur->doc, content, len);
3800 UPDATE_LAST_CHILD_AND_PARENT(cur)
3801 break;
Owen Taylor3473f882001-02-23 17:55:21 +00003802 case XML_TEXT_NODE:
3803 case XML_CDATA_SECTION_NODE:
3804 case XML_ENTITY_REF_NODE:
3805 case XML_ENTITY_NODE:
3806 case XML_PI_NODE:
3807 case XML_COMMENT_NODE:
3808 case XML_NOTATION_NODE:
3809 if (cur->content != NULL) {
3810#ifndef XML_USE_BUFFER_CONTENT
3811 xmlFree(cur->content);
3812#else
3813 xmlBufferFree(cur->content);
3814#endif
3815 }
3816 if (cur->children != NULL) xmlFreeNodeList(cur->children);
3817 cur->children = cur->last = NULL;
3818 if (content != NULL) {
3819#ifndef XML_USE_BUFFER_CONTENT
3820 cur->content = xmlStrndup(content, len);
3821#else
3822 cur->content = xmlBufferCreateSize(len);
3823 xmlBufferSetAllocationScheme(cur->content,
3824 xmlGetBufferAllocationScheme());
3825 xmlBufferAdd(cur->content, content, len);
3826#endif
3827 } else
3828 cur->content = NULL;
3829 break;
3830 case XML_DOCUMENT_NODE:
3831 case XML_DTD_NODE:
3832 case XML_HTML_DOCUMENT_NODE:
3833 case XML_DOCUMENT_TYPE_NODE:
3834 case XML_NAMESPACE_DECL:
3835 case XML_XINCLUDE_START:
3836 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003837#ifdef LIBXML_DOCB_ENABLED
3838 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003839#endif
3840 break;
3841 case XML_ELEMENT_DECL:
3842 /* TODO !!! */
3843 break;
3844 case XML_ATTRIBUTE_DECL:
3845 /* TODO !!! */
3846 break;
3847 case XML_ENTITY_DECL:
3848 /* TODO !!! */
3849 break;
3850 }
3851}
3852
3853/**
3854 * xmlNodeAddContentLen:
3855 * @cur: the node being modified
3856 * @content: extra content
3857 * @len: the size of @content
3858 *
3859 * Append the extra substring to the node content.
3860 */
3861void
3862xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
3863 if (cur == NULL) {
3864#ifdef DEBUG_TREE
3865 xmlGenericError(xmlGenericErrorContext,
3866 "xmlNodeAddContentLen : node == NULL\n");
3867#endif
3868 return;
3869 }
3870 if (len <= 0) return;
3871 switch (cur->type) {
3872 case XML_DOCUMENT_FRAG_NODE:
3873 case XML_ELEMENT_NODE: {
Daniel Veillardacb2bda2002-01-13 16:15:43 +00003874 xmlNodePtr last, newNode, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00003875
Daniel Veillard7db37732001-07-12 01:20:08 +00003876 last = cur->last;
Owen Taylor3473f882001-02-23 17:55:21 +00003877 newNode = xmlNewTextLen(content, len);
3878 if (newNode != NULL) {
Daniel Veillardacb2bda2002-01-13 16:15:43 +00003879 tmp = xmlAddChild(cur, newNode);
3880 if (tmp != newNode)
3881 return;
Owen Taylor3473f882001-02-23 17:55:21 +00003882 if ((last != NULL) && (last->next == newNode)) {
3883 xmlTextMerge(last, newNode);
3884 }
3885 }
3886 break;
3887 }
3888 case XML_ATTRIBUTE_NODE:
3889 break;
3890 case XML_TEXT_NODE:
3891 case XML_CDATA_SECTION_NODE:
3892 case XML_ENTITY_REF_NODE:
3893 case XML_ENTITY_NODE:
3894 case XML_PI_NODE:
3895 case XML_COMMENT_NODE:
3896 case XML_NOTATION_NODE:
3897 if (content != NULL) {
3898#ifndef XML_USE_BUFFER_CONTENT
3899 cur->content = xmlStrncat(cur->content, content, len);
3900#else
3901 xmlBufferAdd(cur->content, content, len);
3902#endif
3903 }
3904 case XML_DOCUMENT_NODE:
3905 case XML_DTD_NODE:
3906 case XML_HTML_DOCUMENT_NODE:
3907 case XML_DOCUMENT_TYPE_NODE:
3908 case XML_NAMESPACE_DECL:
3909 case XML_XINCLUDE_START:
3910 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003911#ifdef LIBXML_DOCB_ENABLED
3912 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003913#endif
3914 break;
3915 case XML_ELEMENT_DECL:
3916 case XML_ATTRIBUTE_DECL:
3917 case XML_ENTITY_DECL:
3918 break;
3919 }
3920}
3921
3922/**
3923 * xmlNodeAddContent:
3924 * @cur: the node being modified
3925 * @content: extra content
3926 *
3927 * Append the extra substring to the node content.
3928 */
3929void
3930xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) {
3931 int len;
3932
3933 if (cur == NULL) {
3934#ifdef DEBUG_TREE
3935 xmlGenericError(xmlGenericErrorContext,
3936 "xmlNodeAddContent : node == NULL\n");
3937#endif
3938 return;
3939 }
3940 if (content == NULL) return;
3941 len = xmlStrlen(content);
3942 xmlNodeAddContentLen(cur, content, len);
3943}
3944
3945/**
3946 * xmlTextMerge:
3947 * @first: the first text node
3948 * @second: the second text node being merged
3949 *
3950 * Merge two text nodes into one
3951 * Returns the first text node augmented
3952 */
3953xmlNodePtr
3954xmlTextMerge(xmlNodePtr first, xmlNodePtr second) {
3955 if (first == NULL) return(second);
3956 if (second == NULL) return(first);
3957 if (first->type != XML_TEXT_NODE) return(first);
3958 if (second->type != XML_TEXT_NODE) return(first);
3959 if (second->name != first->name)
3960 return(first);
3961#ifndef XML_USE_BUFFER_CONTENT
3962 xmlNodeAddContent(first, second->content);
3963#else
3964 xmlNodeAddContent(first, xmlBufferContent(second->content));
3965#endif
3966 xmlUnlinkNode(second);
3967 xmlFreeNode(second);
3968 return(first);
3969}
3970
3971/**
3972 * xmlGetNsList:
3973 * @doc: the document
3974 * @node: the current node
3975 *
3976 * Search all the namespace applying to a given element.
Daniel Veillardd1640922001-12-17 15:30:10 +00003977 * Returns an NULL terminated array of all the #xmlNsPtr found
Owen Taylor3473f882001-02-23 17:55:21 +00003978 * that need to be freed by the caller or NULL if no
3979 * namespace if defined
3980 */
3981xmlNsPtr *
Daniel Veillard77044732001-06-29 21:31:07 +00003982xmlGetNsList(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node)
3983{
Owen Taylor3473f882001-02-23 17:55:21 +00003984 xmlNsPtr cur;
3985 xmlNsPtr *ret = NULL;
3986 int nbns = 0;
3987 int maxns = 10;
3988 int i;
3989
3990 while (node != NULL) {
Daniel Veillard77044732001-06-29 21:31:07 +00003991 if (node->type == XML_ELEMENT_NODE) {
3992 cur = node->nsDef;
3993 while (cur != NULL) {
3994 if (ret == NULL) {
3995 ret =
3996 (xmlNsPtr *) xmlMalloc((maxns + 1) *
3997 sizeof(xmlNsPtr));
3998 if (ret == NULL) {
3999 xmlGenericError(xmlGenericErrorContext,
4000 "xmlGetNsList : out of memory!\n");
4001 return (NULL);
4002 }
4003 ret[nbns] = NULL;
4004 }
4005 for (i = 0; i < nbns; i++) {
4006 if ((cur->prefix == ret[i]->prefix) ||
4007 (xmlStrEqual(cur->prefix, ret[i]->prefix)))
4008 break;
4009 }
4010 if (i >= nbns) {
4011 if (nbns >= maxns) {
4012 maxns *= 2;
4013 ret = (xmlNsPtr *) xmlRealloc(ret,
4014 (maxns +
4015 1) *
4016 sizeof(xmlNsPtr));
4017 if (ret == NULL) {
4018 xmlGenericError(xmlGenericErrorContext,
4019 "xmlGetNsList : realloc failed!\n");
4020 return (NULL);
4021 }
4022 }
4023 ret[nbns++] = cur;
4024 ret[nbns] = NULL;
4025 }
Owen Taylor3473f882001-02-23 17:55:21 +00004026
Daniel Veillard77044732001-06-29 21:31:07 +00004027 cur = cur->next;
4028 }
4029 }
4030 node = node->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00004031 }
Daniel Veillard77044732001-06-29 21:31:07 +00004032 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00004033}
4034
4035/**
4036 * xmlSearchNs:
4037 * @doc: the document
4038 * @node: the current node
Daniel Veillard77851712001-02-27 21:54:07 +00004039 * @nameSpace: the namespace prefix
Owen Taylor3473f882001-02-23 17:55:21 +00004040 *
4041 * Search a Ns registered under a given name space for a document.
4042 * recurse on the parents until it finds the defined namespace
4043 * or return NULL otherwise.
4044 * @nameSpace can be NULL, this is a search for the default namespace.
4045 * We don't allow to cross entities boundaries. If you don't declare
4046 * the namespace within those you will be in troubles !!! A warning
4047 * is generated to cover this case.
4048 *
4049 * Returns the namespace pointer or NULL.
4050 */
4051xmlNsPtr
4052xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) {
4053 xmlNsPtr cur;
4054
4055 if (node == NULL) return(NULL);
4056 if ((nameSpace != NULL) &&
4057 (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) {
4058 if (doc->oldNs == NULL) {
4059 /*
4060 * Allocate a new Namespace and fill the fields.
4061 */
4062 doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
4063 if (doc->oldNs == NULL) {
4064 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00004065 "xmlSearchNs : malloc failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004066 return(NULL);
4067 }
4068 memset(doc->oldNs, 0, sizeof(xmlNs));
4069 doc->oldNs->type = XML_LOCAL_NAMESPACE;
4070
4071 doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE);
4072 doc->oldNs->prefix = xmlStrdup((const xmlChar *)"xml");
4073 }
4074 return(doc->oldNs);
4075 }
4076 while (node != NULL) {
4077 if ((node->type == XML_ENTITY_REF_NODE) ||
4078 (node->type == XML_ENTITY_NODE) ||
4079 (node->type == XML_ENTITY_DECL))
4080 return(NULL);
4081 if (node->type == XML_ELEMENT_NODE) {
4082 cur = node->nsDef;
4083 while (cur != NULL) {
4084 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
4085 (cur->href != NULL))
4086 return(cur);
4087 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
4088 (cur->href != NULL) &&
4089 (xmlStrEqual(cur->prefix, nameSpace)))
4090 return(cur);
4091 cur = cur->next;
4092 }
4093 }
4094 node = node->parent;
4095 }
4096 return(NULL);
4097}
4098
4099/**
4100 * xmlSearchNsByHref:
4101 * @doc: the document
4102 * @node: the current node
4103 * @href: the namespace value
4104 *
4105 * Search a Ns aliasing a given URI. Recurse on the parents until it finds
4106 * the defined namespace or return NULL otherwise.
4107 * Returns the namespace pointer or NULL.
4108 */
4109xmlNsPtr
4110xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar *href) {
4111 xmlNsPtr cur;
4112 xmlNodePtr orig = node;
4113
4114 if ((node == NULL) || (href == NULL)) return(NULL);
4115 if (xmlStrEqual(href, XML_XML_NAMESPACE)) {
4116 if (doc->oldNs == NULL) {
4117 /*
4118 * Allocate a new Namespace and fill the fields.
4119 */
4120 doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
4121 if (doc->oldNs == NULL) {
4122 xmlGenericError(xmlGenericErrorContext,
4123 "xmlSearchNsByHref : malloc failed\n");
4124 return(NULL);
4125 }
4126 memset(doc->oldNs, 0, sizeof(xmlNs));
4127 doc->oldNs->type = XML_LOCAL_NAMESPACE;
4128
4129 doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE);
4130 doc->oldNs->prefix = xmlStrdup((const xmlChar *)"xml");
4131 }
4132 return(doc->oldNs);
4133 }
4134 while (node != NULL) {
4135 cur = node->nsDef;
4136 while (cur != NULL) {
4137 if ((cur->href != NULL) && (href != NULL) &&
4138 (xmlStrEqual(cur->href, href))) {
4139 /*
4140 * Check that the prefix is not shadowed between orig and node
4141 */
4142 xmlNodePtr check = orig;
4143 xmlNsPtr tst;
4144
4145 while (check != node) {
4146 tst = check->nsDef;
4147 while (tst != NULL) {
4148 if ((tst->prefix == NULL) && (cur->prefix == NULL))
4149 goto shadowed;
4150 if ((tst->prefix != NULL) && (cur->prefix != NULL) &&
4151 (xmlStrEqual(tst->prefix, cur->prefix)))
4152 goto shadowed;
4153 tst = tst->next;
4154 }
4155 check = check->parent;
4156 }
4157 return(cur);
4158 }
4159shadowed:
4160 cur = cur->next;
4161 }
4162 node = node->parent;
4163 }
4164 return(NULL);
4165}
4166
4167/**
4168 * xmlNewReconciliedNs
4169 * @doc: the document
4170 * @tree: a node expected to hold the new namespace
4171 * @ns: the original namespace
4172 *
4173 * This function tries to locate a namespace definition in a tree
4174 * ancestors, or create a new namespace definition node similar to
4175 * @ns trying to reuse the same prefix. However if the given prefix is
4176 * null (default namespace) or reused within the subtree defined by
4177 * @tree or on one of its ancestors then a new prefix is generated.
4178 * Returns the (new) namespace definition or NULL in case of error
4179 */
4180xmlNsPtr
4181xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) {
4182 xmlNsPtr def;
4183 xmlChar prefix[50];
4184 int counter = 1;
4185
4186 if (tree == NULL) {
4187#ifdef DEBUG_TREE
4188 xmlGenericError(xmlGenericErrorContext,
4189 "xmlNewReconciliedNs : tree == NULL\n");
4190#endif
4191 return(NULL);
4192 }
4193 if (ns == NULL) {
4194#ifdef DEBUG_TREE
4195 xmlGenericError(xmlGenericErrorContext,
4196 "xmlNewReconciliedNs : ns == NULL\n");
4197#endif
4198 return(NULL);
4199 }
4200 /*
4201 * Search an existing namespace definition inherited.
4202 */
4203 def = xmlSearchNsByHref(doc, tree, ns->href);
4204 if (def != NULL)
4205 return(def);
4206
4207 /*
4208 * Find a close prefix which is not already in use.
4209 * Let's strip namespace prefixes longer than 20 chars !
4210 */
4211 sprintf((char *) prefix, "%.20s", ns->prefix);
4212 def = xmlSearchNs(doc, tree, prefix);
4213 while (def != NULL) {
4214 if (counter > 1000) return(NULL);
4215 sprintf((char *) prefix, "%.20s%d", ns->prefix, counter++);
4216 def = xmlSearchNs(doc, tree, prefix);
4217 }
4218
4219 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00004220 * OK, now we are ready to create a new one.
Owen Taylor3473f882001-02-23 17:55:21 +00004221 */
4222 def = xmlNewNs(tree, ns->href, prefix);
4223 return(def);
4224}
4225
4226/**
4227 * xmlReconciliateNs
4228 * @doc: the document
4229 * @tree: a node defining the subtree to reconciliate
4230 *
4231 * This function checks that all the namespaces declared within the given
4232 * tree are properly declared. This is needed for example after Copy or Cut
4233 * and then paste operations. The subtree may still hold pointers to
4234 * namespace declarations outside the subtree or invalid/masked. As much
Daniel Veillardd1640922001-12-17 15:30:10 +00004235 * as possible the function try to reuse the existing namespaces found in
Owen Taylor3473f882001-02-23 17:55:21 +00004236 * the new environment. If not possible the new namespaces are redeclared
4237 * on @tree at the top of the given subtree.
4238 * Returns the number of namespace declarations created or -1 in case of error.
4239 */
4240int
4241xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) {
4242 xmlNsPtr *oldNs = NULL;
4243 xmlNsPtr *newNs = NULL;
4244 int sizeCache = 0;
4245 int nbCache = 0;
4246
4247 xmlNsPtr n;
4248 xmlNodePtr node = tree;
4249 xmlAttrPtr attr;
4250 int ret = 0, i;
4251
4252 while (node != NULL) {
4253 /*
4254 * Reconciliate the node namespace
4255 */
4256 if (node->ns != NULL) {
4257 /*
4258 * initialize the cache if needed
4259 */
4260 if (sizeCache == 0) {
4261 sizeCache = 10;
4262 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
4263 sizeof(xmlNsPtr));
4264 if (oldNs == NULL) {
4265 xmlGenericError(xmlGenericErrorContext,
4266 "xmlReconciliateNs : memory pbm\n");
4267 return(-1);
4268 }
4269 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
4270 sizeof(xmlNsPtr));
4271 if (newNs == NULL) {
4272 xmlGenericError(xmlGenericErrorContext,
4273 "xmlReconciliateNs : memory pbm\n");
4274 xmlFree(oldNs);
4275 return(-1);
4276 }
4277 }
4278 for (i = 0;i < nbCache;i++) {
4279 if (oldNs[i] == node->ns) {
4280 node->ns = newNs[i];
4281 break;
4282 }
4283 }
4284 if (i == nbCache) {
4285 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00004286 * OK we need to recreate a new namespace definition
Owen Taylor3473f882001-02-23 17:55:21 +00004287 */
4288 n = xmlNewReconciliedNs(doc, tree, node->ns);
4289 if (n != NULL) { /* :-( what if else ??? */
4290 /*
4291 * check if we need to grow the cache buffers.
4292 */
4293 if (sizeCache <= nbCache) {
4294 sizeCache *= 2;
4295 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
4296 sizeof(xmlNsPtr));
4297 if (oldNs == NULL) {
4298 xmlGenericError(xmlGenericErrorContext,
4299 "xmlReconciliateNs : memory pbm\n");
4300 xmlFree(newNs);
4301 return(-1);
4302 }
4303 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
4304 sizeof(xmlNsPtr));
4305 if (newNs == NULL) {
4306 xmlGenericError(xmlGenericErrorContext,
4307 "xmlReconciliateNs : memory pbm\n");
4308 xmlFree(oldNs);
4309 return(-1);
4310 }
4311 }
4312 newNs[nbCache] = n;
4313 oldNs[nbCache++] = node->ns;
4314 node->ns = n;
4315 }
4316 }
4317 }
4318 /*
4319 * now check for namespace hold by attributes on the node.
4320 */
4321 attr = node->properties;
4322 while (attr != NULL) {
4323 if (attr->ns != NULL) {
4324 /*
4325 * initialize the cache if needed
4326 */
4327 if (sizeCache == 0) {
4328 sizeCache = 10;
4329 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
4330 sizeof(xmlNsPtr));
4331 if (oldNs == NULL) {
4332 xmlGenericError(xmlGenericErrorContext,
4333 "xmlReconciliateNs : memory pbm\n");
4334 return(-1);
4335 }
4336 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
4337 sizeof(xmlNsPtr));
4338 if (newNs == NULL) {
4339 xmlGenericError(xmlGenericErrorContext,
4340 "xmlReconciliateNs : memory pbm\n");
4341 xmlFree(oldNs);
4342 return(-1);
4343 }
4344 }
4345 for (i = 0;i < nbCache;i++) {
4346 if (oldNs[i] == attr->ns) {
4347 node->ns = newNs[i];
4348 break;
4349 }
4350 }
4351 if (i == nbCache) {
4352 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00004353 * OK we need to recreate a new namespace definition
Owen Taylor3473f882001-02-23 17:55:21 +00004354 */
4355 n = xmlNewReconciliedNs(doc, tree, attr->ns);
4356 if (n != NULL) { /* :-( what if else ??? */
4357 /*
4358 * check if we need to grow the cache buffers.
4359 */
4360 if (sizeCache <= nbCache) {
4361 sizeCache *= 2;
4362 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
4363 sizeof(xmlNsPtr));
4364 if (oldNs == NULL) {
4365 xmlGenericError(xmlGenericErrorContext,
4366 "xmlReconciliateNs : memory pbm\n");
4367 xmlFree(newNs);
4368 return(-1);
4369 }
4370 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
4371 sizeof(xmlNsPtr));
4372 if (newNs == NULL) {
4373 xmlGenericError(xmlGenericErrorContext,
4374 "xmlReconciliateNs : memory pbm\n");
4375 xmlFree(oldNs);
4376 return(-1);
4377 }
4378 }
4379 newNs[nbCache] = n;
4380 oldNs[nbCache++] = attr->ns;
4381 attr->ns = n;
4382 }
4383 }
4384 }
4385 attr = attr->next;
4386 }
4387
4388 /*
4389 * Browse the full subtree, deep first
4390 */
4391 if (node->children != NULL) {
4392 /* deep first */
4393 node = node->children;
4394 } else if ((node != tree) && (node->next != NULL)) {
4395 /* then siblings */
4396 node = node->next;
4397 } else if (node != tree) {
4398 /* go up to parents->next if needed */
4399 while (node != tree) {
4400 if (node->parent != NULL)
4401 node = node->parent;
4402 if ((node != tree) && (node->next != NULL)) {
4403 node = node->next;
4404 break;
4405 }
4406 if (node->parent == NULL) {
4407 node = NULL;
4408 break;
4409 }
4410 }
4411 /* exit condition */
4412 if (node == tree)
4413 node = NULL;
4414 }
4415 }
4416 return(ret);
4417}
4418
4419/**
4420 * xmlHasProp:
4421 * @node: the node
4422 * @name: the attribute name
4423 *
4424 * Search an attribute associated to a node
4425 * This function also looks in DTD attribute declaration for #FIXED or
4426 * default declaration values unless DTD use has been turned off.
4427 *
4428 * Returns the attribute or the attribute declaration or NULL if
4429 * neither was found.
4430 */
4431xmlAttrPtr
4432xmlHasProp(xmlNodePtr node, const xmlChar *name) {
4433 xmlAttrPtr prop;
4434 xmlDocPtr doc;
4435
4436 if ((node == NULL) || (name == NULL)) return(NULL);
4437 /*
4438 * Check on the properties attached to the node
4439 */
4440 prop = node->properties;
4441 while (prop != NULL) {
4442 if (xmlStrEqual(prop->name, name)) {
4443 return(prop);
4444 }
4445 prop = prop->next;
4446 }
4447 if (!xmlCheckDTD) return(NULL);
4448
4449 /*
4450 * Check if there is a default declaration in the internal
4451 * or external subsets
4452 */
4453 doc = node->doc;
4454 if (doc != NULL) {
4455 xmlAttributePtr attrDecl;
4456 if (doc->intSubset != NULL) {
4457 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
4458 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4459 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
4460 if (attrDecl != NULL)
4461 return((xmlAttrPtr) attrDecl);
4462 }
4463 }
4464 return(NULL);
4465}
4466
4467/**
Daniel Veillarde95e2392001-06-06 10:46:28 +00004468 * xmlHasNsProp:
4469 * @node: the node
4470 * @name: the attribute name
Daniel Veillardca2366a2001-06-11 12:09:01 +00004471 * @nameSpace: the URI of the namespace
Daniel Veillarde95e2392001-06-06 10:46:28 +00004472 *
4473 * Search for an attribute associated to a node
4474 * This attribute has to be anchored in the namespace specified.
4475 * This does the entity substitution.
4476 * This function looks in DTD attribute declaration for #FIXED or
4477 * default declaration values unless DTD use has been turned off.
4478 *
4479 * Returns the attribute or the attribute declaration or NULL
4480 * if neither was found.
4481 */
4482xmlAttrPtr
Daniel Veillardca2366a2001-06-11 12:09:01 +00004483xmlHasNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
Daniel Veillarde95e2392001-06-06 10:46:28 +00004484 xmlAttrPtr prop;
4485 xmlDocPtr doc;
4486 xmlNsPtr ns;
4487
4488 if (node == NULL)
4489 return(NULL);
4490
4491 prop = node->properties;
Daniel Veillardca2366a2001-06-11 12:09:01 +00004492 if (nameSpace == NULL)
Daniel Veillarde95e2392001-06-06 10:46:28 +00004493 return(xmlHasProp(node, name));
4494 while (prop != NULL) {
4495 /*
4496 * One need to have
4497 * - same attribute names
4498 * - and the attribute carrying that namespace
4499 * or
4500 * no namespace on the attribute and the element carrying it
4501 */
4502 if ((xmlStrEqual(prop->name, name)) &&
Daniel Veillarde3c81b52001-06-17 14:50:34 +00004503 ((prop->ns != NULL) && (xmlStrEqual(prop->ns->href, nameSpace)))) {
4504 return(prop);
Daniel Veillarde95e2392001-06-06 10:46:28 +00004505 }
4506 prop = prop->next;
4507 }
4508 if (!xmlCheckDTD) return(NULL);
4509
4510 /*
4511 * Check if there is a default declaration in the internal
4512 * or external subsets
4513 */
4514 doc = node->doc;
4515 if (doc != NULL) {
4516 if (doc->intSubset != NULL) {
4517 xmlAttributePtr attrDecl;
4518
4519 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
4520 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4521 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
4522
4523 if ((attrDecl != NULL) && (attrDecl->prefix != NULL)) {
4524 /*
4525 * The DTD declaration only allows a prefix search
4526 */
4527 ns = xmlSearchNs(doc, node, attrDecl->prefix);
Daniel Veillardca2366a2001-06-11 12:09:01 +00004528 if ((ns != NULL) && (xmlStrEqual(ns->href, nameSpace)))
Daniel Veillarde95e2392001-06-06 10:46:28 +00004529 return((xmlAttrPtr) attrDecl);
4530 }
4531 }
4532 }
4533 return(NULL);
4534}
4535
4536/**
Owen Taylor3473f882001-02-23 17:55:21 +00004537 * xmlGetProp:
4538 * @node: the node
4539 * @name: the attribute name
4540 *
4541 * Search and get the value of an attribute associated to a node
4542 * This does the entity substitution.
4543 * This function looks in DTD attribute declaration for #FIXED or
4544 * default declaration values unless DTD use has been turned off.
4545 *
4546 * Returns the attribute value or NULL if not found.
4547 * It's up to the caller to free the memory.
4548 */
4549xmlChar *
4550xmlGetProp(xmlNodePtr node, const xmlChar *name) {
4551 xmlAttrPtr prop;
4552 xmlDocPtr doc;
4553
4554 if ((node == NULL) || (name == NULL)) return(NULL);
4555 /*
4556 * Check on the properties attached to the node
4557 */
4558 prop = node->properties;
4559 while (prop != NULL) {
4560 if (xmlStrEqual(prop->name, name)) {
4561 xmlChar *ret;
4562
4563 ret = xmlNodeListGetString(node->doc, prop->children, 1);
4564 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
4565 return(ret);
4566 }
4567 prop = prop->next;
4568 }
4569 if (!xmlCheckDTD) return(NULL);
4570
4571 /*
4572 * Check if there is a default declaration in the internal
4573 * or external subsets
4574 */
4575 doc = node->doc;
4576 if (doc != NULL) {
4577 xmlAttributePtr attrDecl;
4578 if (doc->intSubset != NULL) {
4579 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
4580 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4581 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
4582 if (attrDecl != NULL)
4583 return(xmlStrdup(attrDecl->defaultValue));
4584 }
4585 }
4586 return(NULL);
4587}
4588
4589/**
4590 * xmlGetNsProp:
4591 * @node: the node
4592 * @name: the attribute name
Daniel Veillardca2366a2001-06-11 12:09:01 +00004593 * @nameSpace: the URI of the namespace
Owen Taylor3473f882001-02-23 17:55:21 +00004594 *
4595 * Search and get the value of an attribute associated to a node
4596 * This attribute has to be anchored in the namespace specified.
4597 * This does the entity substitution.
4598 * This function looks in DTD attribute declaration for #FIXED or
4599 * default declaration values unless DTD use has been turned off.
4600 *
4601 * Returns the attribute value or NULL if not found.
4602 * It's up to the caller to free the memory.
4603 */
4604xmlChar *
Daniel Veillardca2366a2001-06-11 12:09:01 +00004605xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
Owen Taylor3473f882001-02-23 17:55:21 +00004606 xmlAttrPtr prop;
4607 xmlDocPtr doc;
4608 xmlNsPtr ns;
4609
4610 if (node == NULL)
4611 return(NULL);
4612
4613 prop = node->properties;
Daniel Veillardca2366a2001-06-11 12:09:01 +00004614 if (nameSpace == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00004615 return(xmlGetProp(node, name));
4616 while (prop != NULL) {
4617 /*
4618 * One need to have
4619 * - same attribute names
4620 * - and the attribute carrying that namespace
Owen Taylor3473f882001-02-23 17:55:21 +00004621 */
4622 if ((xmlStrEqual(prop->name, name)) &&
Daniel Veillarde8fc08e2001-06-07 19:35:47 +00004623 ((prop->ns != NULL) &&
Daniel Veillardca2366a2001-06-11 12:09:01 +00004624 (xmlStrEqual(prop->ns->href, nameSpace)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00004625 xmlChar *ret;
4626
4627 ret = xmlNodeListGetString(node->doc, prop->children, 1);
4628 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
4629 return(ret);
4630 }
4631 prop = prop->next;
4632 }
4633 if (!xmlCheckDTD) return(NULL);
4634
4635 /*
4636 * Check if there is a default declaration in the internal
4637 * or external subsets
4638 */
4639 doc = node->doc;
4640 if (doc != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004641 if (doc->intSubset != NULL) {
Daniel Veillard5792e162001-04-30 17:44:45 +00004642 xmlAttributePtr attrDecl;
4643
Owen Taylor3473f882001-02-23 17:55:21 +00004644 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
4645 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4646 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
4647
4648 if ((attrDecl != NULL) && (attrDecl->prefix != NULL)) {
4649 /*
4650 * The DTD declaration only allows a prefix search
4651 */
4652 ns = xmlSearchNs(doc, node, attrDecl->prefix);
Daniel Veillardca2366a2001-06-11 12:09:01 +00004653 if ((ns != NULL) && (xmlStrEqual(ns->href, nameSpace)))
Owen Taylor3473f882001-02-23 17:55:21 +00004654 return(xmlStrdup(attrDecl->defaultValue));
4655 }
4656 }
4657 }
4658 return(NULL);
4659}
4660
4661/**
4662 * xmlSetProp:
4663 * @node: the node
4664 * @name: the attribute name
4665 * @value: the attribute value
4666 *
4667 * Set (or reset) an attribute carried by a node.
4668 * Returns the attribute pointer.
4669 */
4670xmlAttrPtr
4671xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
Daniel Veillard4855c8c2001-11-25 10:35:25 +00004672 xmlAttrPtr prop;
4673 xmlDocPtr doc;
Owen Taylor3473f882001-02-23 17:55:21 +00004674
4675 if ((node == NULL) || (name == NULL))
4676 return(NULL);
4677 doc = node->doc;
Daniel Veillard4855c8c2001-11-25 10:35:25 +00004678 prop = node->properties;
Owen Taylor3473f882001-02-23 17:55:21 +00004679 while (prop != NULL) {
Daniel Veillard75bea542001-05-11 17:41:21 +00004680 if ((xmlStrEqual(prop->name, name)) &&
4681 (prop->ns == NULL)){
Daniel Veillard4855c8c2001-11-25 10:35:25 +00004682 xmlNodePtr oldprop = prop->children;
4683
Owen Taylor3473f882001-02-23 17:55:21 +00004684 prop->children = NULL;
4685 prop->last = NULL;
4686 if (value != NULL) {
4687 xmlChar *buffer;
4688 xmlNodePtr tmp;
4689
4690 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
4691 prop->children = xmlStringGetNodeList(node->doc, buffer);
4692 prop->last = NULL;
4693 prop->doc = doc;
4694 tmp = prop->children;
4695 while (tmp != NULL) {
4696 tmp->parent = (xmlNodePtr) prop;
4697 tmp->doc = doc;
4698 if (tmp->next == NULL)
4699 prop->last = tmp;
4700 tmp = tmp->next;
4701 }
4702 xmlFree(buffer);
Daniel Veillard75bea542001-05-11 17:41:21 +00004703 }
Daniel Veillard4855c8c2001-11-25 10:35:25 +00004704 if (oldprop != NULL)
4705 xmlFreeNodeList(oldprop);
Owen Taylor3473f882001-02-23 17:55:21 +00004706 return(prop);
4707 }
4708 prop = prop->next;
4709 }
4710 prop = xmlNewProp(node, name, value);
4711 return(prop);
4712}
4713
4714/**
Daniel Veillard75bea542001-05-11 17:41:21 +00004715 * xmlUnsetProp:
4716 * @node: the node
4717 * @name: the attribute name
4718 *
4719 * Remove an attribute carried by a node.
4720 * Returns 0 if successful, -1 if not found
4721 */
4722int
4723xmlUnsetProp(xmlNodePtr node, const xmlChar *name) {
4724 xmlAttrPtr prop = node->properties, prev = NULL;;
4725
4726 if ((node == NULL) || (name == NULL))
4727 return(-1);
4728 while (prop != NULL) {
4729 if ((xmlStrEqual(prop->name, name)) &&
4730 (prop->ns == NULL)) {
4731 if (prev == NULL)
4732 node->properties = prop->next;
4733 else
4734 prev->next = prop->next;
4735 xmlFreeProp(prop);
4736 return(0);
4737 }
4738 prev = prop;
4739 prop = prop->next;
4740 }
4741 return(-1);
4742}
4743
4744/**
Owen Taylor3473f882001-02-23 17:55:21 +00004745 * xmlSetNsProp:
4746 * @node: the node
4747 * @ns: the namespace definition
4748 * @name: the attribute name
4749 * @value: the attribute value
4750 *
4751 * Set (or reset) an attribute carried by a node.
4752 * The ns structure must be in scope, this is not checked.
4753 *
4754 * Returns the attribute pointer.
4755 */
4756xmlAttrPtr
4757xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
4758 const xmlChar *value) {
4759 xmlAttrPtr prop;
4760
4761 if ((node == NULL) || (name == NULL))
4762 return(NULL);
4763
4764 if (ns == NULL)
4765 return(xmlSetProp(node, name, value));
4766 if (ns->href == NULL)
4767 return(NULL);
4768 prop = node->properties;
4769
4770 while (prop != NULL) {
4771 /*
4772 * One need to have
4773 * - same attribute names
4774 * - and the attribute carrying that namespace
4775 * or
4776 * no namespace on the attribute and the element carrying it
4777 */
4778 if ((xmlStrEqual(prop->name, name)) &&
4779 (((prop->ns == NULL) && (node->ns != NULL) &&
4780 (xmlStrEqual(node->ns->href, ns->href))) ||
4781 ((prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))))) {
4782 if (prop->children != NULL)
4783 xmlFreeNodeList(prop->children);
4784 prop->children = NULL;
4785 prop->last = NULL;
4786 prop->ns = ns;
4787 if (value != NULL) {
4788 xmlChar *buffer;
4789 xmlNodePtr tmp;
4790
4791 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
4792 prop->children = xmlStringGetNodeList(node->doc, buffer);
4793 prop->last = NULL;
4794 tmp = prop->children;
4795 while (tmp != NULL) {
4796 tmp->parent = (xmlNodePtr) prop;
4797 if (tmp->next == NULL)
4798 prop->last = tmp;
4799 tmp = tmp->next;
4800 }
4801 xmlFree(buffer);
4802 }
4803 return(prop);
4804 }
4805 prop = prop->next;
4806 }
4807 prop = xmlNewNsProp(node, ns, name, value);
4808 return(prop);
4809}
4810
4811/**
Daniel Veillard75bea542001-05-11 17:41:21 +00004812 * xmlUnsetNsProp:
4813 * @node: the node
4814 * @ns: the namespace definition
4815 * @name: the attribute name
4816 *
4817 * Remove an attribute carried by a node.
4818 * Returns 0 if successful, -1 if not found
4819 */
4820int
4821xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) {
4822 xmlAttrPtr prop = node->properties, prev = NULL;;
4823
4824 if ((node == NULL) || (name == NULL))
4825 return(-1);
4826 if (ns == NULL)
4827 return(xmlUnsetProp(node, name));
4828 if (ns->href == NULL)
4829 return(-1);
4830 while (prop != NULL) {
4831 if ((xmlStrEqual(prop->name, name)) &&
4832 (((prop->ns == NULL) && (node->ns != NULL) &&
4833 (xmlStrEqual(node->ns->href, ns->href))) ||
4834 ((prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))))) {
4835 if (prev == NULL)
4836 node->properties = prop->next;
4837 else
4838 prev->next = prop->next;
4839 xmlFreeProp(prop);
4840 return(0);
4841 }
4842 prev = prop;
4843 prop = prop->next;
4844 }
4845 return(-1);
4846}
4847
4848/**
Owen Taylor3473f882001-02-23 17:55:21 +00004849 * xmlNodeIsText:
4850 * @node: the node
4851 *
4852 * Is this node a Text node ?
4853 * Returns 1 yes, 0 no
4854 */
4855int
4856xmlNodeIsText(xmlNodePtr node) {
4857 if (node == NULL) return(0);
4858
4859 if (node->type == XML_TEXT_NODE) return(1);
4860 return(0);
4861}
4862
4863/**
4864 * xmlIsBlankNode:
4865 * @node: the node
4866 *
4867 * Checks whether this node is an empty or whitespace only
4868 * (and possibly ignorable) text-node.
4869 *
4870 * Returns 1 yes, 0 no
4871 */
4872int
4873xmlIsBlankNode(xmlNodePtr node) {
4874 const xmlChar *cur;
4875 if (node == NULL) return(0);
4876
Daniel Veillard7db37732001-07-12 01:20:08 +00004877 if ((node->type != XML_TEXT_NODE) &&
4878 (node->type != XML_CDATA_SECTION_NODE))
4879 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004880 if (node->content == NULL) return(1);
4881#ifndef XML_USE_BUFFER_CONTENT
4882 cur = node->content;
4883#else
4884 cur = xmlBufferContent(node->content);
4885#endif
4886 while (*cur != 0) {
4887 if (!IS_BLANK(*cur)) return(0);
4888 cur++;
4889 }
4890
4891 return(1);
4892}
4893
4894/**
4895 * xmlTextConcat:
4896 * @node: the node
4897 * @content: the content
Daniel Veillard60087f32001-10-10 09:45:09 +00004898 * @len: @content length
Owen Taylor3473f882001-02-23 17:55:21 +00004899 *
4900 * Concat the given string at the end of the existing node content
4901 */
4902
4903void
4904xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) {
4905 if (node == NULL) return;
4906
4907 if ((node->type != XML_TEXT_NODE) &&
4908 (node->type != XML_CDATA_SECTION_NODE)) {
4909#ifdef DEBUG_TREE
4910 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00004911 "xmlTextConcat: node is not text nor CDATA\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004912#endif
4913 return;
4914 }
4915#ifndef XML_USE_BUFFER_CONTENT
4916 node->content = xmlStrncat(node->content, content, len);
4917#else
4918 xmlBufferAdd(node->content, content, len);
4919#endif
4920}
4921
4922/************************************************************************
4923 * *
4924 * Output : to a FILE or in memory *
4925 * *
4926 ************************************************************************/
4927
Owen Taylor3473f882001-02-23 17:55:21 +00004928/**
4929 * xmlBufferCreate:
4930 *
4931 * routine to create an XML buffer.
4932 * returns the new structure.
4933 */
4934xmlBufferPtr
4935xmlBufferCreate(void) {
4936 xmlBufferPtr ret;
4937
4938 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
4939 if (ret == NULL) {
4940 xmlGenericError(xmlGenericErrorContext,
4941 "xmlBufferCreate : out of memory!\n");
4942 return(NULL);
4943 }
4944 ret->use = 0;
Daniel Veillarde356c282001-03-10 12:32:04 +00004945 ret->size = xmlDefaultBufferSize;
Owen Taylor3473f882001-02-23 17:55:21 +00004946 ret->alloc = xmlBufferAllocScheme;
4947 ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar));
4948 if (ret->content == NULL) {
4949 xmlGenericError(xmlGenericErrorContext,
4950 "xmlBufferCreate : out of memory!\n");
4951 xmlFree(ret);
4952 return(NULL);
4953 }
4954 ret->content[0] = 0;
4955 return(ret);
4956}
4957
4958/**
4959 * xmlBufferCreateSize:
4960 * @size: initial size of buffer
4961 *
4962 * routine to create an XML buffer.
4963 * returns the new structure.
4964 */
4965xmlBufferPtr
4966xmlBufferCreateSize(size_t size) {
4967 xmlBufferPtr ret;
4968
4969 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
4970 if (ret == NULL) {
4971 xmlGenericError(xmlGenericErrorContext,
4972 "xmlBufferCreate : out of memory!\n");
4973 return(NULL);
4974 }
4975 ret->use = 0;
4976 ret->alloc = xmlBufferAllocScheme;
4977 ret->size = (size ? size+2 : 0); /* +1 for ending null */
4978 if (ret->size){
4979 ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar));
4980 if (ret->content == NULL) {
4981 xmlGenericError(xmlGenericErrorContext,
4982 "xmlBufferCreate : out of memory!\n");
4983 xmlFree(ret);
4984 return(NULL);
4985 }
4986 ret->content[0] = 0;
4987 } else
4988 ret->content = NULL;
4989 return(ret);
4990}
4991
4992/**
4993 * xmlBufferSetAllocationScheme:
4994 * @buf: the buffer to free
4995 * @scheme: allocation scheme to use
4996 *
4997 * Sets the allocation scheme for this buffer
4998 */
4999void
5000xmlBufferSetAllocationScheme(xmlBufferPtr buf,
5001 xmlBufferAllocationScheme scheme) {
5002 if (buf == NULL) {
5003#ifdef DEBUG_BUFFER
5004 xmlGenericError(xmlGenericErrorContext,
5005 "xmlBufferSetAllocationScheme: buf == NULL\n");
5006#endif
5007 return;
5008 }
5009
5010 buf->alloc = scheme;
5011}
5012
5013/**
5014 * xmlBufferFree:
5015 * @buf: the buffer to free
5016 *
5017 * Frees an XML buffer.
5018 */
5019void
5020xmlBufferFree(xmlBufferPtr buf) {
5021 if (buf == NULL) {
5022#ifdef DEBUG_BUFFER
5023 xmlGenericError(xmlGenericErrorContext,
5024 "xmlBufferFree: buf == NULL\n");
5025#endif
5026 return;
5027 }
5028 if (buf->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005029 xmlFree(buf->content);
5030 }
Owen Taylor3473f882001-02-23 17:55:21 +00005031 xmlFree(buf);
5032}
5033
5034/**
5035 * xmlBufferEmpty:
5036 * @buf: the buffer
5037 *
5038 * empty a buffer.
5039 */
5040void
5041xmlBufferEmpty(xmlBufferPtr buf) {
5042 if (buf->content == NULL) return;
5043 buf->use = 0;
Daniel Veillard92ad2102001-03-27 12:47:33 +00005044 memset(buf->content, 0, buf->size);
Owen Taylor3473f882001-02-23 17:55:21 +00005045}
5046
5047/**
5048 * xmlBufferShrink:
5049 * @buf: the buffer to dump
5050 * @len: the number of xmlChar to remove
5051 *
5052 * Remove the beginning of an XML buffer.
5053 *
Daniel Veillardd1640922001-12-17 15:30:10 +00005054 * Returns the number of #xmlChar removed, or -1 in case of failure.
Owen Taylor3473f882001-02-23 17:55:21 +00005055 */
5056int
5057xmlBufferShrink(xmlBufferPtr buf, unsigned int len) {
5058 if (len == 0) return(0);
5059 if (len > buf->use) return(-1);
5060
5061 buf->use -= len;
5062 memmove(buf->content, &buf->content[len], buf->use * sizeof(xmlChar));
5063
5064 buf->content[buf->use] = 0;
5065 return(len);
5066}
5067
5068/**
5069 * xmlBufferGrow:
5070 * @buf: the buffer
5071 * @len: the minimum free size to allocate
5072 *
5073 * Grow the available space of an XML buffer.
5074 *
5075 * Returns the new available space or -1 in case of error
5076 */
5077int
5078xmlBufferGrow(xmlBufferPtr buf, unsigned int len) {
5079 int size;
5080 xmlChar *newbuf;
5081
5082 if (len + buf->use < buf->size) return(0);
5083
5084 size = buf->use + len + 100;
5085
5086 newbuf = (xmlChar *) xmlRealloc(buf->content, size);
5087 if (newbuf == NULL) return(-1);
5088 buf->content = newbuf;
5089 buf->size = size;
5090 return(buf->size - buf->use);
5091}
5092
5093/**
5094 * xmlBufferDump:
5095 * @file: the file output
5096 * @buf: the buffer to dump
5097 *
5098 * Dumps an XML buffer to a FILE *.
Daniel Veillardd1640922001-12-17 15:30:10 +00005099 * Returns the number of #xmlChar written
Owen Taylor3473f882001-02-23 17:55:21 +00005100 */
5101int
5102xmlBufferDump(FILE *file, xmlBufferPtr buf) {
5103 int ret;
5104
5105 if (buf == NULL) {
5106#ifdef DEBUG_BUFFER
5107 xmlGenericError(xmlGenericErrorContext,
5108 "xmlBufferDump: buf == NULL\n");
5109#endif
5110 return(0);
5111 }
5112 if (buf->content == NULL) {
5113#ifdef DEBUG_BUFFER
5114 xmlGenericError(xmlGenericErrorContext,
5115 "xmlBufferDump: buf->content == NULL\n");
5116#endif
5117 return(0);
5118 }
Daniel Veillardcd337f02001-11-22 18:20:37 +00005119 if (file == NULL)
5120 file = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00005121 ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
5122 return(ret);
5123}
5124
5125/**
5126 * xmlBufferContent:
5127 * @buf: the buffer
5128 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00005129 * Function to extract the content of a buffer
5130 *
Owen Taylor3473f882001-02-23 17:55:21 +00005131 * Returns the internal content
5132 */
5133
Daniel Veillard5e2dace2001-07-18 19:30:27 +00005134const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00005135xmlBufferContent(const xmlBufferPtr buf)
5136{
5137 if(!buf)
5138 return NULL;
5139
5140 return buf->content;
5141}
5142
5143/**
5144 * xmlBufferLength:
5145 * @buf: the buffer
5146 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00005147 * Function to get the length of a buffer
5148 *
Owen Taylor3473f882001-02-23 17:55:21 +00005149 * Returns the length of data in the internal content
5150 */
5151
5152int
5153xmlBufferLength(const xmlBufferPtr buf)
5154{
5155 if(!buf)
5156 return 0;
5157
5158 return buf->use;
5159}
5160
5161/**
5162 * xmlBufferResize:
5163 * @buf: the buffer to resize
5164 * @size: the desired size
5165 *
Daniel Veillardd1640922001-12-17 15:30:10 +00005166 * Resize a buffer to accommodate minimum size of @size.
Owen Taylor3473f882001-02-23 17:55:21 +00005167 *
5168 * Returns 0 in case of problems, 1 otherwise
5169 */
5170int
5171xmlBufferResize(xmlBufferPtr buf, unsigned int size)
5172{
5173 unsigned int newSize;
5174 xmlChar* rebuf = NULL;
5175
5176 /*take care of empty case*/
5177 newSize = (buf->size ? buf->size*2 : size);
5178
5179 /* Don't resize if we don't have to */
5180 if (size < buf->size)
5181 return 1;
5182
5183 /* figure out new size */
5184 switch (buf->alloc){
5185 case XML_BUFFER_ALLOC_DOUBLEIT:
5186 while (size > newSize) newSize *= 2;
5187 break;
5188 case XML_BUFFER_ALLOC_EXACT:
5189 newSize = size+10;
5190 break;
5191 default:
5192 newSize = size+10;
5193 break;
5194 }
5195
5196 if (buf->content == NULL)
5197 rebuf = (xmlChar *) xmlMalloc(newSize * sizeof(xmlChar));
5198 else
5199 rebuf = (xmlChar *) xmlRealloc(buf->content,
5200 newSize * sizeof(xmlChar));
5201 if (rebuf == NULL) {
5202 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00005203 "xmlBufferResize : out of memory!\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005204 return 0;
5205 }
5206 buf->content = rebuf;
5207 buf->size = newSize;
5208
5209 return 1;
5210}
5211
5212/**
5213 * xmlBufferAdd:
5214 * @buf: the buffer to dump
Daniel Veillardd1640922001-12-17 15:30:10 +00005215 * @str: the #xmlChar string
5216 * @len: the number of #xmlChar to add
Owen Taylor3473f882001-02-23 17:55:21 +00005217 *
Daniel Veillard60087f32001-10-10 09:45:09 +00005218 * Add a string range to an XML buffer. if len == -1, the length of
Owen Taylor3473f882001-02-23 17:55:21 +00005219 * str is recomputed.
5220 */
5221void
5222xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) {
5223 unsigned int needSize;
5224
5225 if (str == NULL) {
5226#ifdef DEBUG_BUFFER
5227 xmlGenericError(xmlGenericErrorContext,
5228 "xmlBufferAdd: str == NULL\n");
5229#endif
5230 return;
5231 }
5232 if (len < -1) {
5233#ifdef DEBUG_BUFFER
5234 xmlGenericError(xmlGenericErrorContext,
5235 "xmlBufferAdd: len < 0\n");
5236#endif
5237 return;
5238 }
5239 if (len == 0) return;
5240
5241 if (len < 0)
5242 len = xmlStrlen(str);
5243
5244 if (len <= 0) return;
5245
5246 needSize = buf->use + len + 2;
5247 if (needSize > buf->size){
5248 if (!xmlBufferResize(buf, needSize)){
5249 xmlGenericError(xmlGenericErrorContext,
5250 "xmlBufferAdd : out of memory!\n");
5251 return;
5252 }
5253 }
5254
5255 memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
5256 buf->use += len;
5257 buf->content[buf->use] = 0;
5258}
5259
5260/**
5261 * xmlBufferAddHead:
5262 * @buf: the buffer
Daniel Veillardd1640922001-12-17 15:30:10 +00005263 * @str: the #xmlChar string
5264 * @len: the number of #xmlChar to add
Owen Taylor3473f882001-02-23 17:55:21 +00005265 *
5266 * Add a string range to the beginning of an XML buffer.
Daniel Veillard60087f32001-10-10 09:45:09 +00005267 * if len == -1, the length of @str is recomputed.
Owen Taylor3473f882001-02-23 17:55:21 +00005268 */
5269void
5270xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) {
5271 unsigned int needSize;
5272
5273 if (str == NULL) {
5274#ifdef DEBUG_BUFFER
5275 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00005276 "xmlBufferAddHead: str == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005277#endif
5278 return;
5279 }
5280 if (len < -1) {
5281#ifdef DEBUG_BUFFER
5282 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00005283 "xmlBufferAddHead: len < 0\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005284#endif
5285 return;
5286 }
5287 if (len == 0) return;
5288
5289 if (len < 0)
5290 len = xmlStrlen(str);
5291
5292 if (len <= 0) return;
5293
5294 needSize = buf->use + len + 2;
5295 if (needSize > buf->size){
5296 if (!xmlBufferResize(buf, needSize)){
5297 xmlGenericError(xmlGenericErrorContext,
5298 "xmlBufferAddHead : out of memory!\n");
5299 return;
5300 }
5301 }
5302
5303 memmove(&buf->content[len], &buf->content[0], buf->use * sizeof(xmlChar));
5304 memmove(&buf->content[0], str, len * sizeof(xmlChar));
5305 buf->use += len;
5306 buf->content[buf->use] = 0;
5307}
5308
5309/**
5310 * xmlBufferCat:
5311 * @buf: the buffer to dump
Daniel Veillardd1640922001-12-17 15:30:10 +00005312 * @str: the #xmlChar string
Owen Taylor3473f882001-02-23 17:55:21 +00005313 *
5314 * Append a zero terminated string to an XML buffer.
5315 */
5316void
5317xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) {
5318 if (str != NULL)
5319 xmlBufferAdd(buf, str, -1);
5320}
5321
5322/**
5323 * xmlBufferCCat:
5324 * @buf: the buffer to dump
5325 * @str: the C char string
5326 *
5327 * Append a zero terminated C string to an XML buffer.
5328 */
5329void
5330xmlBufferCCat(xmlBufferPtr buf, const char *str) {
5331 const char *cur;
5332
5333 if (str == NULL) {
5334#ifdef DEBUG_BUFFER
5335 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00005336 "xmlBufferCCat: str == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005337#endif
5338 return;
5339 }
5340 for (cur = str;*cur != 0;cur++) {
5341 if (buf->use + 10 >= buf->size) {
5342 if (!xmlBufferResize(buf, buf->use+10)){
5343 xmlGenericError(xmlGenericErrorContext,
5344 "xmlBufferCCat : out of memory!\n");
5345 return;
5346 }
5347 }
5348 buf->content[buf->use++] = *cur;
5349 }
5350 buf->content[buf->use] = 0;
5351}
5352
5353/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +00005354 * xmlBufferWriteXmlCHAR:
5355 * @buf: the XML buffer
5356 * @string: the string to add
5357 *
5358 * For VMS only.
5359 * routine which manages and grows an output buffer. This one adds
5360 * xmlChars at the end of the buffer.
5361 */
5362/**
Owen Taylor3473f882001-02-23 17:55:21 +00005363 * xmlBufferWriteCHAR:
5364 * @buf: the XML buffer
5365 * @string: the string to add
5366 *
5367 * routine which manages and grows an output buffer. This one adds
5368 * xmlChars at the end of the buffer.
5369 */
5370void
5371#ifdef VMS
5372xmlBufferWriteXmlCHAR
5373#else
5374xmlBufferWriteCHAR
5375#endif
5376(xmlBufferPtr buf, const xmlChar *string) {
5377 xmlBufferCat(buf, string);
5378}
5379
5380/**
5381 * xmlBufferWriteChar:
5382 * @buf: the XML buffer output
5383 * @string: the string to add
5384 *
5385 * routine which manage and grows an output buffer. This one add
5386 * C chars at the end of the array.
5387 */
5388void
5389xmlBufferWriteChar(xmlBufferPtr buf, const char *string) {
5390 xmlBufferCCat(buf, string);
5391}
5392
5393
5394/**
5395 * xmlBufferWriteQuotedString:
5396 * @buf: the XML buffer output
5397 * @string: the string to add
5398 *
5399 * routine which manage and grows an output buffer. This one writes
Daniel Veillardd1640922001-12-17 15:30:10 +00005400 * a quoted or double quoted #xmlChar string, checking first if it holds
Owen Taylor3473f882001-02-23 17:55:21 +00005401 * quote or double-quotes internally
5402 */
5403void
5404xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) {
5405 if (xmlStrchr(string, '"')) {
5406 if (xmlStrchr(string, '\'')) {
5407#ifdef DEBUG_BUFFER
5408 xmlGenericError(xmlGenericErrorContext,
5409 "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n");
5410#endif
5411 }
5412 xmlBufferCCat(buf, "'");
5413 xmlBufferCat(buf, string);
5414 xmlBufferCCat(buf, "'");
5415 } else {
5416 xmlBufferCCat(buf, "\"");
5417 xmlBufferCat(buf, string);
5418 xmlBufferCCat(buf, "\"");
5419 }
5420}
5421
5422
5423/************************************************************************
5424 * *
5425 * Dumping XML tree content to a simple buffer *
5426 * *
5427 ************************************************************************/
5428
5429void
5430xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
5431 int format);
5432static void
5433xmlNodeListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
5434 int format);
5435void
5436htmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur);
5437
5438/**
5439 * xmlNsDump:
5440 * @buf: the XML buffer output
5441 * @cur: a namespace
5442 *
5443 * Dump a local Namespace definition.
5444 * Should be called in the context of attributes dumps.
5445 */
5446static void
5447xmlNsDump(xmlBufferPtr buf, xmlNsPtr cur) {
5448 if (cur == NULL) {
5449#ifdef DEBUG_TREE
5450 xmlGenericError(xmlGenericErrorContext,
5451 "xmlNsDump : Ns == NULL\n");
5452#endif
5453 return;
5454 }
5455 if (cur->type == XML_LOCAL_NAMESPACE) {
5456 /* Within the context of an element attributes */
5457 if (cur->prefix != NULL) {
5458 xmlBufferWriteChar(buf, " xmlns:");
5459 xmlBufferWriteCHAR(buf, cur->prefix);
5460 } else
5461 xmlBufferWriteChar(buf, " xmlns");
5462 xmlBufferWriteChar(buf, "=");
5463 xmlBufferWriteQuotedString(buf, cur->href);
5464 }
5465}
5466
5467/**
5468 * xmlNsListDump:
5469 * @buf: the XML buffer output
5470 * @cur: the first namespace
5471 *
5472 * Dump a list of local Namespace definitions.
5473 * Should be called in the context of attributes dumps.
5474 */
5475static void
5476xmlNsListDump(xmlBufferPtr buf, xmlNsPtr cur) {
5477 while (cur != NULL) {
5478 xmlNsDump(buf, cur);
5479 cur = cur->next;
5480 }
5481}
5482
5483/**
5484 * xmlDtdDump:
5485 * @buf: the XML buffer output
Daniel Veillardd1640922001-12-17 15:30:10 +00005486 * @dtd: the DTD
Owen Taylor3473f882001-02-23 17:55:21 +00005487 *
5488 * Dump the XML document DTD, if any.
5489 */
5490static void
5491xmlDtdDump(xmlBufferPtr buf, xmlDtdPtr dtd) {
5492 if (dtd == NULL) {
5493#ifdef DEBUG_TREE
5494 xmlGenericError(xmlGenericErrorContext,
5495 "xmlDtdDump : no internal subset\n");
5496#endif
5497 return;
5498 }
5499 xmlBufferWriteChar(buf, "<!DOCTYPE ");
5500 xmlBufferWriteCHAR(buf, dtd->name);
5501 if (dtd->ExternalID != NULL) {
5502 xmlBufferWriteChar(buf, " PUBLIC ");
5503 xmlBufferWriteQuotedString(buf, dtd->ExternalID);
5504 xmlBufferWriteChar(buf, " ");
5505 xmlBufferWriteQuotedString(buf, dtd->SystemID);
5506 } else if (dtd->SystemID != NULL) {
5507 xmlBufferWriteChar(buf, " SYSTEM ");
5508 xmlBufferWriteQuotedString(buf, dtd->SystemID);
5509 }
5510 if ((dtd->entities == NULL) && (dtd->elements == NULL) &&
5511 (dtd->attributes == NULL) && (dtd->notations == NULL)) {
5512 xmlBufferWriteChar(buf, ">");
5513 return;
5514 }
5515 xmlBufferWriteChar(buf, " [\n");
5516 xmlNodeListDump(buf, dtd->doc, dtd->children, -1, 0);
5517#if 0
5518 if (dtd->entities != NULL)
5519 xmlDumpEntitiesTable(buf, (xmlEntitiesTablePtr) dtd->entities);
5520 if (dtd->notations != NULL)
5521 xmlDumpNotationTable(buf, (xmlNotationTablePtr) dtd->notations);
5522 if (dtd->elements != NULL)
5523 xmlDumpElementTable(buf, (xmlElementTablePtr) dtd->elements);
5524 if (dtd->attributes != NULL)
5525 xmlDumpAttributeTable(buf, (xmlAttributeTablePtr) dtd->attributes);
5526#endif
5527 xmlBufferWriteChar(buf, "]>");
5528}
5529
5530/**
5531 * xmlAttrDump:
5532 * @buf: the XML buffer output
5533 * @doc: the document
5534 * @cur: the attribute pointer
5535 *
5536 * Dump an XML attribute
5537 */
5538static void
5539xmlAttrDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) {
5540 xmlChar *value;
5541
5542 if (cur == NULL) {
5543#ifdef DEBUG_TREE
5544 xmlGenericError(xmlGenericErrorContext,
5545 "xmlAttrDump : property == NULL\n");
5546#endif
5547 return;
5548 }
5549 xmlBufferWriteChar(buf, " ");
5550 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
5551 xmlBufferWriteCHAR(buf, cur->ns->prefix);
5552 xmlBufferWriteChar(buf, ":");
5553 }
5554 xmlBufferWriteCHAR(buf, cur->name);
5555 value = xmlNodeListGetString(doc, cur->children, 0);
5556 if (value != NULL) {
5557 xmlBufferWriteChar(buf, "=");
5558 xmlBufferWriteQuotedString(buf, value);
5559 xmlFree(value);
5560 } else {
5561 xmlBufferWriteChar(buf, "=\"\"");
5562 }
5563}
5564
5565/**
5566 * xmlAttrListDump:
5567 * @buf: the XML buffer output
5568 * @doc: the document
5569 * @cur: the first attribute pointer
5570 *
5571 * Dump a list of XML attributes
5572 */
5573static void
5574xmlAttrListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) {
5575 if (cur == NULL) {
5576#ifdef DEBUG_TREE
5577 xmlGenericError(xmlGenericErrorContext,
5578 "xmlAttrListDump : property == NULL\n");
5579#endif
5580 return;
5581 }
5582 while (cur != NULL) {
5583 xmlAttrDump(buf, doc, cur);
5584 cur = cur->next;
5585 }
5586}
5587
5588
5589
5590/**
5591 * xmlNodeListDump:
5592 * @buf: the XML buffer output
5593 * @doc: the document
5594 * @cur: the first node
5595 * @level: the imbrication level for indenting
5596 * @format: is formatting allowed
5597 *
5598 * Dump an XML node list, recursive behaviour,children are printed too.
5599 */
5600static void
5601xmlNodeListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
5602 int format) {
5603 int i;
5604
5605 if (cur == NULL) {
5606#ifdef DEBUG_TREE
5607 xmlGenericError(xmlGenericErrorContext,
5608 "xmlNodeListDump : node == NULL\n");
5609#endif
5610 return;
5611 }
5612 while (cur != NULL) {
5613 if ((format) && (xmlIndentTreeOutput) &&
5614 (cur->type == XML_ELEMENT_NODE))
5615 for (i = 0;i < level;i++)
5616 xmlBufferWriteChar(buf, " ");
5617 xmlNodeDump(buf, doc, cur, level, format);
5618 if (format) {
5619 xmlBufferWriteChar(buf, "\n");
5620 }
5621 cur = cur->next;
5622 }
5623}
5624
5625/**
5626 * xmlNodeDump:
5627 * @buf: the XML buffer output
5628 * @doc: the document
5629 * @cur: the current node
5630 * @level: the imbrication level for indenting
5631 * @format: is formatting allowed
5632 *
5633 * Dump an XML node, recursive behaviour,children are printed too.
5634 */
5635void
5636xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
5637 int format) {
5638 int i;
5639 xmlNodePtr tmp;
5640
5641 if (cur == NULL) {
5642#ifdef DEBUG_TREE
5643 xmlGenericError(xmlGenericErrorContext,
5644 "xmlNodeDump : node == NULL\n");
5645#endif
5646 return;
5647 }
5648 if (cur->type == XML_XINCLUDE_START)
5649 return;
5650 if (cur->type == XML_XINCLUDE_END)
5651 return;
5652 if (cur->type == XML_DTD_NODE) {
5653 xmlDtdDump(buf, (xmlDtdPtr) cur);
5654 return;
5655 }
5656 if (cur->type == XML_ELEMENT_DECL) {
5657 xmlDumpElementDecl(buf, (xmlElementPtr) cur);
5658 return;
5659 }
Daniel Veillard78d12092001-10-11 09:12:24 +00005660 if (cur->type == XML_ATTRIBUTE_NODE){
5661 xmlAttrDump(buf, doc, (xmlAttrPtr)cur);
5662 return;
5663 }
Owen Taylor3473f882001-02-23 17:55:21 +00005664 if (cur->type == XML_ATTRIBUTE_DECL) {
5665 xmlDumpAttributeDecl(buf, (xmlAttributePtr) cur);
5666 return;
5667 }
5668 if (cur->type == XML_ENTITY_DECL) {
5669 xmlDumpEntityDecl(buf, (xmlEntityPtr) cur);
5670 return;
5671 }
5672 if (cur->type == XML_TEXT_NODE) {
5673 if (cur->content != NULL) {
5674 if ((cur->name == xmlStringText) ||
5675 (cur->name != xmlStringTextNoenc)) {
5676 xmlChar *buffer;
5677
5678#ifndef XML_USE_BUFFER_CONTENT
5679 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
5680#else
5681 buffer = xmlEncodeEntitiesReentrant(doc,
5682 xmlBufferContent(cur->content));
5683#endif
5684 if (buffer != NULL) {
5685 xmlBufferWriteCHAR(buf, buffer);
5686 xmlFree(buffer);
5687 }
5688 } else {
5689 /*
5690 * Disable escaping, needed for XSLT
5691 */
5692#ifndef XML_USE_BUFFER_CONTENT
5693 xmlBufferWriteCHAR(buf, cur->content);
5694#else
5695 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
5696#endif
5697 }
5698 }
5699 return;
5700 }
5701 if (cur->type == XML_PI_NODE) {
Daniel Veillard2c748c62002-01-16 15:37:50 +00005702 xmlBufferWriteChar(buf, "<?");
5703 xmlBufferWriteCHAR(buf, cur->name);
Owen Taylor3473f882001-02-23 17:55:21 +00005704 if (cur->content != NULL) {
Daniel Veillard2c748c62002-01-16 15:37:50 +00005705 xmlBufferWriteChar(buf, " ");
Owen Taylor3473f882001-02-23 17:55:21 +00005706#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard2c748c62002-01-16 15:37:50 +00005707 xmlBufferWriteCHAR(buf, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00005708#else
Daniel Veillard2c748c62002-01-16 15:37:50 +00005709 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
Owen Taylor3473f882001-02-23 17:55:21 +00005710#endif
Owen Taylor3473f882001-02-23 17:55:21 +00005711 }
Daniel Veillard2c748c62002-01-16 15:37:50 +00005712 xmlBufferWriteChar(buf, "?>");
Owen Taylor3473f882001-02-23 17:55:21 +00005713 return;
5714 }
5715 if (cur->type == XML_COMMENT_NODE) {
5716 if (cur->content != NULL) {
5717 xmlBufferWriteChar(buf, "<!--");
5718#ifndef XML_USE_BUFFER_CONTENT
5719 xmlBufferWriteCHAR(buf, cur->content);
5720#else
5721 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
5722#endif
5723 xmlBufferWriteChar(buf, "-->");
5724 }
5725 return;
5726 }
5727 if (cur->type == XML_ENTITY_REF_NODE) {
5728 xmlBufferWriteChar(buf, "&");
5729 xmlBufferWriteCHAR(buf, cur->name);
5730 xmlBufferWriteChar(buf, ";");
5731 return;
5732 }
5733 if (cur->type == XML_CDATA_SECTION_NODE) {
5734 xmlBufferWriteChar(buf, "<![CDATA[");
5735 if (cur->content != NULL)
5736#ifndef XML_USE_BUFFER_CONTENT
5737 xmlBufferWriteCHAR(buf, cur->content);
5738#else
5739 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
5740#endif
5741 xmlBufferWriteChar(buf, "]]>");
5742 return;
5743 }
5744
5745 if (format == 1) {
5746 tmp = cur->children;
5747 while (tmp != NULL) {
5748 if ((tmp->type == XML_TEXT_NODE) ||
5749 (tmp->type == XML_ENTITY_REF_NODE)) {
5750 format = 0;
5751 break;
5752 }
5753 tmp = tmp->next;
5754 }
5755 }
5756 xmlBufferWriteChar(buf, "<");
5757 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
5758 xmlBufferWriteCHAR(buf, cur->ns->prefix);
5759 xmlBufferWriteChar(buf, ":");
5760 }
5761
5762 xmlBufferWriteCHAR(buf, cur->name);
5763 if (cur->nsDef)
5764 xmlNsListDump(buf, cur->nsDef);
5765 if (cur->properties != NULL)
5766 xmlAttrListDump(buf, doc, cur->properties);
5767
Daniel Veillard7db37732001-07-12 01:20:08 +00005768 if (((cur->type == XML_ELEMENT_NODE) || (cur->content == NULL)) &&
5769 (cur->children == NULL) &&
Owen Taylor3473f882001-02-23 17:55:21 +00005770 (!xmlSaveNoEmptyTags)) {
5771 xmlBufferWriteChar(buf, "/>");
5772 return;
5773 }
5774 xmlBufferWriteChar(buf, ">");
Daniel Veillard7db37732001-07-12 01:20:08 +00005775 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005776 xmlChar *buffer;
5777
5778#ifndef XML_USE_BUFFER_CONTENT
5779 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
5780#else
5781 buffer = xmlEncodeEntitiesReentrant(doc,
5782 xmlBufferContent(cur->content));
5783#endif
5784 if (buffer != NULL) {
5785 xmlBufferWriteCHAR(buf, buffer);
5786 xmlFree(buffer);
5787 }
5788 }
5789 if (cur->children != NULL) {
5790 if (format) xmlBufferWriteChar(buf, "\n");
5791 xmlNodeListDump(buf, doc, cur->children,
5792 (level >= 0?level+1:-1), format);
5793 if ((xmlIndentTreeOutput) && (format))
5794 for (i = 0;i < level;i++)
5795 xmlBufferWriteChar(buf, " ");
5796 }
5797 xmlBufferWriteChar(buf, "</");
5798 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
5799 xmlBufferWriteCHAR(buf, cur->ns->prefix);
5800 xmlBufferWriteChar(buf, ":");
5801 }
5802
5803 xmlBufferWriteCHAR(buf, cur->name);
5804 xmlBufferWriteChar(buf, ">");
5805}
5806
5807/**
5808 * xmlElemDump:
5809 * @f: the FILE * for the output
5810 * @doc: the document
5811 * @cur: the current node
5812 *
Daniel Veillardd1640922001-12-17 15:30:10 +00005813 * Dump an XML/HTML node, recursive behaviour, children are printed too.
Owen Taylor3473f882001-02-23 17:55:21 +00005814 */
5815void
5816xmlElemDump(FILE *f, xmlDocPtr doc, xmlNodePtr cur) {
5817 xmlBufferPtr buf;
5818
5819 if (cur == NULL) {
5820#ifdef DEBUG_TREE
5821 xmlGenericError(xmlGenericErrorContext,
5822 "xmlElemDump : cur == NULL\n");
5823#endif
5824 return;
5825 }
Owen Taylor3473f882001-02-23 17:55:21 +00005826#ifdef DEBUG_TREE
Daniel Veillardd79bcd12001-06-21 22:07:42 +00005827 if (doc == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005828 xmlGenericError(xmlGenericErrorContext,
5829 "xmlElemDump : doc == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005830 }
Daniel Veillardd79bcd12001-06-21 22:07:42 +00005831#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00005832
Owen Taylor3473f882001-02-23 17:55:21 +00005833 buf = xmlBufferCreate();
5834 if (buf == NULL) return;
5835 if ((doc != NULL) &&
5836 (doc->type == XML_HTML_DOCUMENT_NODE)) {
5837#ifdef LIBXML_HTML_ENABLED
5838 htmlNodeDump(buf, doc, cur);
5839#else
5840 xmlGenericError(xmlGenericErrorContext,
5841 "HTML support not compiled in\n");
5842#endif /* LIBXML_HTML_ENABLED */
5843 } else
5844 xmlNodeDump(buf, doc, cur, 0, 1);
5845 xmlBufferDump(f, buf);
5846 xmlBufferFree(buf);
5847}
5848
5849/************************************************************************
5850 * *
5851 * Dumping XML tree content to an I/O output buffer *
5852 * *
5853 ************************************************************************/
5854
5855void
5856xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
5857 int level, int format, const char *encoding);
5858static void
5859xmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
5860 int level, int format, const char *encoding);
5861/**
5862 * xmlNsDumpOutput:
5863 * @buf: the XML buffer output
5864 * @cur: a namespace
5865 *
5866 * Dump a local Namespace definition.
5867 * Should be called in the context of attributes dumps.
5868 */
5869static void
5870xmlNsDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) {
5871 if (cur == NULL) {
5872#ifdef DEBUG_TREE
5873 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00005874 "xmlNsDumpOutput : Ns == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005875#endif
5876 return;
5877 }
5878 if ((cur->type == XML_LOCAL_NAMESPACE) && (cur->href != NULL)) {
5879 /* Within the context of an element attributes */
5880 if (cur->prefix != NULL) {
5881 xmlOutputBufferWriteString(buf, " xmlns:");
5882 xmlOutputBufferWriteString(buf, (const char *)cur->prefix);
5883 } else
5884 xmlOutputBufferWriteString(buf, " xmlns");
5885 xmlOutputBufferWriteString(buf, "=");
5886 xmlBufferWriteQuotedString(buf->buffer, cur->href);
5887 }
5888}
5889
5890/**
5891 * xmlNsListDumpOutput:
5892 * @buf: the XML buffer output
5893 * @cur: the first namespace
5894 *
5895 * Dump a list of local Namespace definitions.
5896 * Should be called in the context of attributes dumps.
5897 */
5898static void
5899xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) {
5900 while (cur != NULL) {
5901 xmlNsDumpOutput(buf, cur);
5902 cur = cur->next;
5903 }
5904}
5905
5906/**
5907 * xmlDtdDumpOutput:
5908 * @buf: the XML buffer output
5909 * @doc: the document
5910 * @encoding: an optional encoding string
5911 *
5912 * Dump the XML document DTD, if any.
5913 */
5914static void
5915xmlDtdDumpOutput(xmlOutputBufferPtr buf, xmlDtdPtr dtd, const char *encoding) {
5916 if (dtd == NULL) {
5917#ifdef DEBUG_TREE
5918 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00005919 "xmlDtdDumpOutput : no internal subset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005920#endif
5921 return;
5922 }
5923 xmlOutputBufferWriteString(buf, "<!DOCTYPE ");
5924 xmlOutputBufferWriteString(buf, (const char *)dtd->name);
5925 if (dtd->ExternalID != NULL) {
5926 xmlOutputBufferWriteString(buf, " PUBLIC ");
5927 xmlBufferWriteQuotedString(buf->buffer, dtd->ExternalID);
5928 xmlOutputBufferWriteString(buf, " ");
5929 xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID);
5930 } else if (dtd->SystemID != NULL) {
5931 xmlOutputBufferWriteString(buf, " SYSTEM ");
5932 xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID);
5933 }
5934 if ((dtd->entities == NULL) && (dtd->elements == NULL) &&
5935 (dtd->attributes == NULL) && (dtd->notations == NULL)) {
5936 xmlOutputBufferWriteString(buf, ">");
5937 return;
5938 }
5939 xmlOutputBufferWriteString(buf, " [\n");
5940 xmlNodeListDumpOutput(buf, dtd->doc, dtd->children, -1, 0, encoding);
5941 xmlOutputBufferWriteString(buf, "]>");
5942}
5943
5944/**
5945 * xmlAttrDumpOutput:
5946 * @buf: the XML buffer output
5947 * @doc: the document
5948 * @cur: the attribute pointer
5949 * @encoding: an optional encoding string
5950 *
5951 * Dump an XML attribute
5952 */
5953static void
5954xmlAttrDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00005955 const char *encoding ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00005956 xmlChar *value;
5957
5958 if (cur == NULL) {
5959#ifdef DEBUG_TREE
5960 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00005961 "xmlAttrDumpOutput : property == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005962#endif
5963 return;
5964 }
5965 xmlOutputBufferWriteString(buf, " ");
5966 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
5967 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
5968 xmlOutputBufferWriteString(buf, ":");
5969 }
5970 xmlOutputBufferWriteString(buf, (const char *)cur->name);
5971 value = xmlNodeListGetString(doc, cur->children, 0);
5972 if (value) {
5973 xmlOutputBufferWriteString(buf, "=");
5974 xmlBufferWriteQuotedString(buf->buffer, value);
5975 xmlFree(value);
5976 } else {
5977 xmlOutputBufferWriteString(buf, "=\"\"");
5978 }
5979}
5980
5981/**
5982 * xmlAttrListDumpOutput:
5983 * @buf: the XML buffer output
5984 * @doc: the document
5985 * @cur: the first attribute pointer
5986 * @encoding: an optional encoding string
5987 *
5988 * Dump a list of XML attributes
5989 */
5990static void
5991xmlAttrListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
5992 xmlAttrPtr cur, const char *encoding) {
5993 if (cur == NULL) {
5994#ifdef DEBUG_TREE
5995 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00005996 "xmlAttrListDumpOutput : property == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005997#endif
5998 return;
5999 }
6000 while (cur != NULL) {
6001 xmlAttrDumpOutput(buf, doc, cur, encoding);
6002 cur = cur->next;
6003 }
6004}
6005
6006
6007
6008/**
6009 * xmlNodeListDumpOutput:
6010 * @buf: the XML buffer output
6011 * @doc: the document
6012 * @cur: the first node
6013 * @level: the imbrication level for indenting
6014 * @format: is formatting allowed
6015 * @encoding: an optional encoding string
6016 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006017 * Dump an XML node list, recursive behaviour, children are printed too.
Owen Taylor3473f882001-02-23 17:55:21 +00006018 */
6019static void
6020xmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
6021 xmlNodePtr cur, int level, int format, const char *encoding) {
6022 int i;
6023
6024 if (cur == NULL) {
6025#ifdef DEBUG_TREE
6026 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006027 "xmlNodeListDumpOutput : node == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006028#endif
6029 return;
6030 }
6031 while (cur != NULL) {
6032 if ((format) && (xmlIndentTreeOutput) &&
6033 (cur->type == XML_ELEMENT_NODE))
6034 for (i = 0;i < level;i++)
6035 xmlOutputBufferWriteString(buf, " ");
6036 xmlNodeDumpOutput(buf, doc, cur, level, format, encoding);
6037 if (format) {
6038 xmlOutputBufferWriteString(buf, "\n");
6039 }
6040 cur = cur->next;
6041 }
6042}
6043
6044/**
6045 * xmlNodeDumpOutput:
6046 * @buf: the XML buffer output
6047 * @doc: the document
6048 * @cur: the current node
6049 * @level: the imbrication level for indenting
6050 * @format: is formatting allowed
6051 * @encoding: an optional encoding string
6052 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006053 * Dump an XML node, recursive behaviour, children are printed too.
Owen Taylor3473f882001-02-23 17:55:21 +00006054 */
6055void
6056xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
6057 int level, int format, const char *encoding) {
6058 int i;
6059 xmlNodePtr tmp;
6060
6061 if (cur == NULL) {
6062#ifdef DEBUG_TREE
6063 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006064 "xmlNodeDumpOutput : node == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006065#endif
6066 return;
6067 }
6068 if (cur->type == XML_XINCLUDE_START)
6069 return;
6070 if (cur->type == XML_XINCLUDE_END)
6071 return;
6072 if (cur->type == XML_DTD_NODE) {
6073 xmlDtdDumpOutput(buf, (xmlDtdPtr) cur, encoding);
6074 return;
6075 }
6076 if (cur->type == XML_ELEMENT_DECL) {
6077 xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur);
6078 return;
6079 }
6080 if (cur->type == XML_ATTRIBUTE_DECL) {
6081 xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur);
6082 return;
6083 }
6084 if (cur->type == XML_ENTITY_DECL) {
6085 xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur);
6086 return;
6087 }
6088 if (cur->type == XML_TEXT_NODE) {
6089 if (cur->content != NULL) {
6090 if ((cur->name == xmlStringText) ||
6091 (cur->name != xmlStringTextNoenc)) {
6092 xmlChar *buffer;
6093
6094#ifndef XML_USE_BUFFER_CONTENT
6095 if (encoding == NULL)
6096 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
6097 else
6098 buffer = xmlEncodeSpecialChars(doc, cur->content);
6099#else
6100 if (encoding == NULL)
6101 buffer = xmlEncodeEntitiesReentrant(doc,
6102 xmlBufferContent(cur->content));
6103 else
6104 buffer = xmlEncodeSpecialChars(doc,
6105 xmlBufferContent(cur->content));
6106#endif
6107 if (buffer != NULL) {
6108 xmlOutputBufferWriteString(buf, (const char *)buffer);
6109 xmlFree(buffer);
6110 }
6111 } else {
6112 /*
6113 * Disable escaping, needed for XSLT
6114 */
6115#ifndef XML_USE_BUFFER_CONTENT
6116 xmlOutputBufferWriteString(buf, (const char *) cur->content);
6117#else
6118 xmlOutputBufferWriteString(buf, xmlBufferContent(cur->content));
6119#endif
6120 }
6121 }
6122
6123 return;
6124 }
6125 if (cur->type == XML_PI_NODE) {
6126 if (cur->content != NULL) {
6127 xmlOutputBufferWriteString(buf, "<?");
6128 xmlOutputBufferWriteString(buf, (const char *)cur->name);
6129 if (cur->content != NULL) {
6130 xmlOutputBufferWriteString(buf, " ");
6131#ifndef XML_USE_BUFFER_CONTENT
6132 xmlOutputBufferWriteString(buf, (const char *)cur->content);
6133#else
6134 xmlOutputBufferWriteString(buf, (const char *)xmlBufferContent(cur->content));
6135#endif
6136 }
6137 xmlOutputBufferWriteString(buf, "?>");
6138 } else {
6139 xmlOutputBufferWriteString(buf, "<?");
6140 xmlOutputBufferWriteString(buf, (const char *)cur->name);
6141 xmlOutputBufferWriteString(buf, "?>");
6142 }
6143 return;
6144 }
6145 if (cur->type == XML_COMMENT_NODE) {
6146 if (cur->content != NULL) {
6147 xmlOutputBufferWriteString(buf, "<!--");
6148#ifndef XML_USE_BUFFER_CONTENT
6149 xmlOutputBufferWriteString(buf, (const char *)cur->content);
6150#else
6151 xmlOutputBufferWriteString(buf, (const char *)xmlBufferContent(cur->content));
6152#endif
6153 xmlOutputBufferWriteString(buf, "-->");
6154 }
6155 return;
6156 }
6157 if (cur->type == XML_ENTITY_REF_NODE) {
6158 xmlOutputBufferWriteString(buf, "&");
6159 xmlOutputBufferWriteString(buf, (const char *)cur->name);
6160 xmlOutputBufferWriteString(buf, ";");
6161 return;
6162 }
6163 if (cur->type == XML_CDATA_SECTION_NODE) {
6164 xmlOutputBufferWriteString(buf, "<![CDATA[");
6165 if (cur->content != NULL)
6166#ifndef XML_USE_BUFFER_CONTENT
6167 xmlOutputBufferWriteString(buf, (const char *)cur->content);
6168#else
6169 xmlOutputBufferWriteString(buf, (const char *)xmlBufferContent(cur->content));
6170#endif
6171 xmlOutputBufferWriteString(buf, "]]>");
6172 return;
6173 }
6174
6175 if (format == 1) {
6176 tmp = cur->children;
6177 while (tmp != NULL) {
6178 if ((tmp->type == XML_TEXT_NODE) ||
6179 (tmp->type == XML_ENTITY_REF_NODE)) {
6180 format = 0;
6181 break;
6182 }
6183 tmp = tmp->next;
6184 }
6185 }
6186 xmlOutputBufferWriteString(buf, "<");
6187 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
6188 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
6189 xmlOutputBufferWriteString(buf, ":");
6190 }
6191
6192 xmlOutputBufferWriteString(buf, (const char *)cur->name);
6193 if (cur->nsDef)
6194 xmlNsListDumpOutput(buf, cur->nsDef);
6195 if (cur->properties != NULL)
6196 xmlAttrListDumpOutput(buf, doc, cur->properties, encoding);
6197
Daniel Veillard7db37732001-07-12 01:20:08 +00006198 if (((cur->type == XML_ELEMENT_NODE) || (cur->content == NULL)) &&
6199 (cur->children == NULL) && (!xmlSaveNoEmptyTags)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006200 xmlOutputBufferWriteString(buf, "/>");
6201 return;
6202 }
6203 xmlOutputBufferWriteString(buf, ">");
Daniel Veillard7db37732001-07-12 01:20:08 +00006204 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006205 xmlChar *buffer;
6206
6207#ifndef XML_USE_BUFFER_CONTENT
6208 if (encoding == NULL)
6209 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
6210 else
6211 buffer = xmlEncodeSpecialChars(doc, cur->content);
6212#else
6213 if (encoding == NULL)
6214 buffer = xmlEncodeEntitiesReentrant(doc,
6215 xmlBufferContent(cur->content));
6216 else
6217 buffer = xmlEncodeSpecialChars(doc,
6218 xmlBufferContent(cur->content));
6219#endif
6220 if (buffer != NULL) {
6221 xmlOutputBufferWriteString(buf, (const char *)buffer);
6222 xmlFree(buffer);
6223 }
6224 }
6225 if (cur->children != NULL) {
6226 if (format) xmlOutputBufferWriteString(buf, "\n");
6227 xmlNodeListDumpOutput(buf, doc, cur->children,
6228 (level >= 0?level+1:-1), format, encoding);
6229 if ((xmlIndentTreeOutput) && (format))
6230 for (i = 0;i < level;i++)
6231 xmlOutputBufferWriteString(buf, " ");
6232 }
6233 xmlOutputBufferWriteString(buf, "</");
6234 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
6235 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
6236 xmlOutputBufferWriteString(buf, ":");
6237 }
6238
6239 xmlOutputBufferWriteString(buf, (const char *)cur->name);
6240 xmlOutputBufferWriteString(buf, ">");
6241}
6242
6243/**
6244 * xmlDocContentDumpOutput:
6245 * @buf: the XML buffer output
6246 * @cur: the document
6247 * @encoding: an optional encoding string
6248 * @format: should formatting spaces been added
6249 *
6250 * Dump an XML document.
6251 */
6252static void
6253xmlDocContentDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr cur,
6254 const char *encoding, int format) {
6255 xmlOutputBufferWriteString(buf, "<?xml version=");
6256 if (cur->version != NULL)
6257 xmlBufferWriteQuotedString(buf->buffer, cur->version);
6258 else
6259 xmlOutputBufferWriteString(buf, "\"1.0\"");
6260 if (encoding == NULL) {
6261 if (cur->encoding != NULL)
6262 encoding = (const char *) cur->encoding;
6263 else if (cur->charset != XML_CHAR_ENCODING_UTF8)
6264 encoding = xmlGetCharEncodingName((xmlCharEncoding) cur->charset);
6265 }
6266 if (encoding != NULL) {
6267 xmlOutputBufferWriteString(buf, " encoding=");
6268 xmlBufferWriteQuotedString(buf->buffer, (xmlChar *) encoding);
6269 }
6270 switch (cur->standalone) {
6271 case 0:
6272 xmlOutputBufferWriteString(buf, " standalone=\"no\"");
6273 break;
6274 case 1:
6275 xmlOutputBufferWriteString(buf, " standalone=\"yes\"");
6276 break;
6277 }
6278 xmlOutputBufferWriteString(buf, "?>\n");
6279 if (cur->children != NULL) {
6280 xmlNodePtr child = cur->children;
6281
6282 while (child != NULL) {
6283 xmlNodeDumpOutput(buf, cur, child, 0, format, encoding);
6284 xmlOutputBufferWriteString(buf, "\n");
6285 child = child->next;
6286 }
6287 }
6288}
6289
6290/************************************************************************
6291 * *
6292 * Saving functions front-ends *
6293 * *
6294 ************************************************************************/
6295
6296/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +00006297 * xmlDocDumpFormatMemoryEnc:
Owen Taylor3473f882001-02-23 17:55:21 +00006298 * @out_doc: Document to generate XML text from
6299 * @doc_txt_ptr: Memory pointer for allocated XML text
6300 * @doc_txt_len: Length of the generated XML text
6301 * @txt_encoding: Character encoding to use when generating XML text
6302 * @format: should formatting spaces been added
6303 *
6304 * Dump the current DOM tree into memory using the character encoding specified
6305 * by the caller. Note it is up to the caller of this function to free the
6306 * allocated memory.
6307 */
6308
6309void
6310xmlDocDumpFormatMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00006311 int * doc_txt_len, const char * txt_encoding,
Daniel Veillard1731d6a2001-04-10 16:38:06 +00006312 int format) {
Owen Taylor3473f882001-02-23 17:55:21 +00006313 int dummy = 0;
6314
6315 xmlCharEncoding doc_charset;
6316 xmlOutputBufferPtr out_buff = NULL;
6317 xmlCharEncodingHandlerPtr conv_hdlr = NULL;
6318
6319 if (doc_txt_len == NULL) {
6320 doc_txt_len = &dummy; /* Continue, caller just won't get length */
6321 }
6322
6323 if (doc_txt_ptr == NULL) {
6324 *doc_txt_len = 0;
6325 xmlGenericError(xmlGenericErrorContext,
6326 "xmlDocDumpFormatMemoryEnc: Null return buffer pointer.");
6327 return;
6328 }
6329
6330 *doc_txt_ptr = NULL;
6331 *doc_txt_len = 0;
6332
6333 if (out_doc == NULL) {
6334 /* No document, no output */
6335 xmlGenericError(xmlGenericErrorContext,
6336 "xmlDocDumpFormatMemoryEnc: Null DOM tree document pointer.\n");
6337 return;
6338 }
6339
6340 /*
6341 * Validate the encoding value, if provided.
6342 * This logic is copied from xmlSaveFileEnc.
6343 */
6344
6345 if (txt_encoding == NULL)
6346 txt_encoding = (const char *) out_doc->encoding;
6347 if (txt_encoding != NULL) {
6348 doc_charset = xmlParseCharEncoding(txt_encoding);
6349
6350 if (out_doc->charset != XML_CHAR_ENCODING_UTF8) {
6351 xmlGenericError(xmlGenericErrorContext,
6352 "xmlDocDumpFormatMemoryEnc: Source document not in UTF8\n");
6353 return;
6354
6355 } else if (doc_charset != XML_CHAR_ENCODING_UTF8) {
6356 conv_hdlr = xmlFindCharEncodingHandler(txt_encoding);
6357 if ( conv_hdlr == NULL ) {
6358 xmlGenericError(xmlGenericErrorContext,
6359 "%s: %s %s '%s'\n",
6360 "xmlDocDumpFormatMemoryEnc",
6361 "Failed to identify encoding handler for",
6362 "character set",
6363 txt_encoding);
6364 return;
6365 }
6366 }
6367 }
6368
6369 if ((out_buff = xmlAllocOutputBuffer(conv_hdlr)) == NULL ) {
6370 xmlGenericError(xmlGenericErrorContext,
6371 "xmlDocDumpFormatMemoryEnc: Failed to allocate output buffer.\n");
6372 return;
6373 }
6374
Daniel Veillard1731d6a2001-04-10 16:38:06 +00006375 xmlDocContentDumpOutput(out_buff, out_doc, txt_encoding, format);
Owen Taylor3473f882001-02-23 17:55:21 +00006376 xmlOutputBufferFlush(out_buff);
6377 if (out_buff->conv != NULL) {
6378 *doc_txt_len = out_buff->conv->use;
6379 *doc_txt_ptr = xmlStrndup(out_buff->conv->content, *doc_txt_len);
6380 } else {
6381 *doc_txt_len = out_buff->buffer->use;
6382 *doc_txt_ptr = xmlStrndup(out_buff->buffer->content, *doc_txt_len);
6383 }
6384 (void)xmlOutputBufferClose(out_buff);
6385
6386 if ((*doc_txt_ptr == NULL) && (*doc_txt_len > 0)) {
6387 *doc_txt_len = 0;
6388 xmlGenericError(xmlGenericErrorContext,
6389 "xmlDocDumpFormatMemoryEnc: %s\n",
6390 "Failed to allocate memory for document text representation.");
6391 }
6392
6393 return;
6394}
6395
6396/**
6397 * xmlDocDumpMemory:
6398 * @cur: the document
6399 * @mem: OUT: the memory pointer
Daniel Veillard60087f32001-10-10 09:45:09 +00006400 * @size: OUT: the memory length
Owen Taylor3473f882001-02-23 17:55:21 +00006401 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006402 * Dump an XML document in memory and return the #xmlChar * and it's size.
Owen Taylor3473f882001-02-23 17:55:21 +00006403 * It's up to the caller to free the memory.
6404 */
6405void
6406xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) {
6407 xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, 0);
6408}
6409
6410/**
6411 * xmlDocDumpFormatMemory:
6412 * @cur: the document
6413 * @mem: OUT: the memory pointer
Daniel Veillard60087f32001-10-10 09:45:09 +00006414 * @size: OUT: the memory length
Owen Taylor3473f882001-02-23 17:55:21 +00006415 * @format: should formatting spaces been added
6416 *
6417 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006418 * Dump an XML document in memory and return the #xmlChar * and it's size.
Owen Taylor3473f882001-02-23 17:55:21 +00006419 * It's up to the caller to free the memory.
6420 */
6421void
6422xmlDocDumpFormatMemory(xmlDocPtr cur, xmlChar**mem, int *size, int format) {
6423 xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, format);
6424}
6425
6426/**
6427 * xmlDocDumpMemoryEnc:
6428 * @out_doc: Document to generate XML text from
6429 * @doc_txt_ptr: Memory pointer for allocated XML text
6430 * @doc_txt_len: Length of the generated XML text
6431 * @txt_encoding: Character encoding to use when generating XML text
6432 *
6433 * Dump the current DOM tree into memory using the character encoding specified
6434 * by the caller. Note it is up to the caller of this function to free the
6435 * allocated memory.
6436 */
6437
6438void
6439xmlDocDumpMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
6440 int * doc_txt_len, const char * txt_encoding) {
6441 xmlDocDumpFormatMemoryEnc(out_doc, doc_txt_ptr, doc_txt_len,
Daniel Veillard1731d6a2001-04-10 16:38:06 +00006442 txt_encoding, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00006443}
6444
6445/**
6446 * xmlGetDocCompressMode:
6447 * @doc: the document
6448 *
6449 * get the compression ratio for a document, ZLIB based
6450 * Returns 0 (uncompressed) to 9 (max compression)
6451 */
6452int
6453xmlGetDocCompressMode (xmlDocPtr doc) {
6454 if (doc == NULL) return(-1);
6455 return(doc->compression);
6456}
6457
6458/**
6459 * xmlSetDocCompressMode:
6460 * @doc: the document
6461 * @mode: the compression ratio
6462 *
6463 * set the compression ratio for a document, ZLIB based
6464 * Correct values: 0 (uncompressed) to 9 (max compression)
6465 */
6466void
6467xmlSetDocCompressMode (xmlDocPtr doc, int mode) {
6468 if (doc == NULL) return;
6469 if (mode < 0) doc->compression = 0;
6470 else if (mode > 9) doc->compression = 9;
6471 else doc->compression = mode;
6472}
6473
6474/**
6475 * xmlGetCompressMode:
6476 *
6477 * get the default compression mode used, ZLIB based.
6478 * Returns 0 (uncompressed) to 9 (max compression)
6479 */
6480int
6481 xmlGetCompressMode(void) {
6482 return(xmlCompressMode);
6483}
6484
6485/**
6486 * xmlSetCompressMode:
6487 * @mode: the compression ratio
6488 *
6489 * set the default compression mode used, ZLIB based
6490 * Correct values: 0 (uncompressed) to 9 (max compression)
6491 */
6492void
6493xmlSetCompressMode(int mode) {
6494 if (mode < 0) xmlCompressMode = 0;
6495 else if (mode > 9) xmlCompressMode = 9;
6496 else xmlCompressMode = mode;
6497}
6498
6499/**
6500 * xmlDocDump:
6501 * @f: the FILE*
6502 * @cur: the document
6503 *
6504 * Dump an XML document to an open FILE.
6505 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006506 * returns: the number of bytes written or -1 in case of failure.
Owen Taylor3473f882001-02-23 17:55:21 +00006507 */
6508int
6509xmlDocDump(FILE *f, xmlDocPtr cur) {
6510 xmlOutputBufferPtr buf;
6511 const char * encoding;
6512 xmlCharEncodingHandlerPtr handler = NULL;
6513 int ret;
6514
6515 if (cur == NULL) {
6516#ifdef DEBUG_TREE
6517 xmlGenericError(xmlGenericErrorContext,
6518 "xmlDocDump : document == NULL\n");
6519#endif
6520 return(-1);
6521 }
6522 encoding = (const char *) cur->encoding;
6523
6524 if (encoding != NULL) {
6525 xmlCharEncoding enc;
6526
6527 enc = xmlParseCharEncoding(encoding);
6528
6529 if (cur->charset != XML_CHAR_ENCODING_UTF8) {
6530 xmlGenericError(xmlGenericErrorContext,
6531 "xmlDocDump: document not in UTF8\n");
6532 return(-1);
6533 }
6534 if (enc != XML_CHAR_ENCODING_UTF8) {
6535 handler = xmlFindCharEncodingHandler(encoding);
6536 if (handler == NULL) {
6537 xmlFree((char *) cur->encoding);
6538 cur->encoding = NULL;
6539 }
6540 }
6541 }
6542 buf = xmlOutputBufferCreateFile(f, handler);
6543 if (buf == NULL) return(-1);
Daniel Veillard1731d6a2001-04-10 16:38:06 +00006544 xmlDocContentDumpOutput(buf, cur, NULL, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00006545
6546 ret = xmlOutputBufferClose(buf);
6547 return(ret);
6548}
6549
6550/**
6551 * xmlSaveFileTo:
6552 * @buf: an output I/O buffer
6553 * @cur: the document
Daniel Veillardd1640922001-12-17 15:30:10 +00006554 * @encoding: the encoding if any assuming the I/O layer handles the trancoding
Owen Taylor3473f882001-02-23 17:55:21 +00006555 *
6556 * Dump an XML document to an I/O buffer.
6557 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006558 * returns: the number of bytes written or -1 in case of failure.
Owen Taylor3473f882001-02-23 17:55:21 +00006559 */
6560int
CET 2001 Daniel Veillard5a37bde2001-11-01 14:31:22 +00006561xmlSaveFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding) {
Owen Taylor3473f882001-02-23 17:55:21 +00006562 int ret;
6563
6564 if (buf == NULL) return(0);
Daniel Veillard1731d6a2001-04-10 16:38:06 +00006565 xmlDocContentDumpOutput(buf, cur, encoding, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00006566 ret = xmlOutputBufferClose(buf);
6567 return(ret);
6568}
6569
6570/**
Daniel Veillardeefd4492001-04-28 16:55:50 +00006571 * xmlSaveFormatFileTo:
6572 * @buf: an output I/O buffer
6573 * @cur: the document
Daniel Veillardd1640922001-12-17 15:30:10 +00006574 * @encoding: the encoding if any assuming the I/O layer handles the trancoding
Daniel Veillardeefd4492001-04-28 16:55:50 +00006575 * @format: should formatting spaces been added
6576 *
6577 * Dump an XML document to an I/O buffer.
6578 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006579 * returns: the number of bytes written or -1 in case of failure.
Daniel Veillardeefd4492001-04-28 16:55:50 +00006580 */
6581int
CET 2001 Daniel Veillard5a37bde2001-11-01 14:31:22 +00006582xmlSaveFormatFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding, int format) {
Daniel Veillardeefd4492001-04-28 16:55:50 +00006583 int ret;
6584
6585 if (buf == NULL) return(0);
6586 xmlDocContentDumpOutput(buf, cur, encoding, format);
6587 ret = xmlOutputBufferClose(buf);
6588 return(ret);
6589}
6590
6591/**
Daniel Veillardf012a642001-07-23 19:10:52 +00006592 * xmlSaveFormatFileEnc
6593 * @filename: the filename or URL to output
6594 * @cur: the document being saved
6595 * @encoding: the name of the encoding to use or NULL.
6596 * @format: should formatting spaces be added.
Daniel Veillardd1640922001-12-17 15:30:10 +00006597 *
6598 * Returns the number of bytes written or -1 in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00006599 */
6600int
Daniel Veillardf012a642001-07-23 19:10:52 +00006601xmlSaveFormatFileEnc( const char * filename, xmlDocPtr cur,
6602 const char * encoding, int format ) {
Owen Taylor3473f882001-02-23 17:55:21 +00006603 xmlOutputBufferPtr buf;
6604 xmlCharEncodingHandlerPtr handler = NULL;
Daniel Veillard81418e32001-05-22 15:08:55 +00006605 xmlCharEncoding enc;
Owen Taylor3473f882001-02-23 17:55:21 +00006606 int ret;
6607
Daniel Veillardfb25a512002-01-13 20:32:08 +00006608 if (encoding == NULL)
6609 encoding = (const char *) cur->encoding;
6610
Owen Taylor3473f882001-02-23 17:55:21 +00006611 if (encoding != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00006612
6613 enc = xmlParseCharEncoding(encoding);
6614 if (cur->charset != XML_CHAR_ENCODING_UTF8) {
6615 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006616 "xmlSaveFormatFileEnc: document not in UTF8\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006617 return(-1);
6618 }
6619 if (enc != XML_CHAR_ENCODING_UTF8) {
6620 handler = xmlFindCharEncodingHandler(encoding);
Daniel Veillard81418e32001-05-22 15:08:55 +00006621 if (handler == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00006622 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006623 }
6624 }
6625
Daniel Veillardf012a642001-07-23 19:10:52 +00006626#ifdef HAVE_ZLIB_H
6627 if (cur->compression < 0) cur->compression = xmlCompressMode;
6628#endif
Owen Taylor3473f882001-02-23 17:55:21 +00006629 /*
6630 * save the content to a temp buffer.
6631 */
Daniel Veillardf012a642001-07-23 19:10:52 +00006632 buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression);
Owen Taylor3473f882001-02-23 17:55:21 +00006633 if (buf == NULL) return(-1);
6634
Daniel Veillardf012a642001-07-23 19:10:52 +00006635 xmlDocContentDumpOutput(buf, cur, encoding, format);
Owen Taylor3473f882001-02-23 17:55:21 +00006636
6637 ret = xmlOutputBufferClose(buf);
6638 return(ret);
6639}
6640
Daniel Veillardf012a642001-07-23 19:10:52 +00006641
6642/**
6643 * xmlSaveFileEnc:
6644 * @filename: the filename (or URL)
6645 * @cur: the document
6646 * @encoding: the name of an encoding (or NULL)
6647 *
6648 * Dump an XML document, converting it to the given encoding
6649 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006650 * returns: the number of bytes written or -1 in case of failure.
Daniel Veillardf012a642001-07-23 19:10:52 +00006651 */
6652int
6653xmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) {
6654 return ( xmlSaveFormatFileEnc( filename, cur, encoding, 0 ) );
6655}
6656
Owen Taylor3473f882001-02-23 17:55:21 +00006657/**
Daniel Veillard67fee942001-04-26 18:59:03 +00006658 * xmlSaveFormatFile:
Owen Taylor3473f882001-02-23 17:55:21 +00006659 * @filename: the filename (or URL)
6660 * @cur: the document
Daniel Veillard67fee942001-04-26 18:59:03 +00006661 * @format: should formatting spaces been added
Owen Taylor3473f882001-02-23 17:55:21 +00006662 *
6663 * Dump an XML document to a file. Will use compression if
6664 * compiled in and enabled. If @filename is "-" the stdout file is
Daniel Veillardd1640922001-12-17 15:30:10 +00006665 * used. If @format is set then the document will be indented on output.
Daniel Veillard67fee942001-04-26 18:59:03 +00006666 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006667 * returns: the number of bytes written or -1 in case of failure.
Owen Taylor3473f882001-02-23 17:55:21 +00006668 */
6669int
Daniel Veillard67fee942001-04-26 18:59:03 +00006670xmlSaveFormatFile(const char *filename, xmlDocPtr cur, int format) {
Daniel Veillardf012a642001-07-23 19:10:52 +00006671 return ( xmlSaveFormatFileEnc( filename, cur, NULL, format ) );
Owen Taylor3473f882001-02-23 17:55:21 +00006672}
6673
Daniel Veillard67fee942001-04-26 18:59:03 +00006674/**
6675 * xmlSaveFile:
6676 * @filename: the filename (or URL)
6677 * @cur: the document
6678 *
6679 * Dump an XML document to a file. Will use compression if
6680 * compiled in and enabled. If @filename is "-" the stdout file is
6681 * used.
Daniel Veillardd1640922001-12-17 15:30:10 +00006682 * returns: the number of bytes written or -1 in case of failure.
Daniel Veillard67fee942001-04-26 18:59:03 +00006683 */
6684int
6685xmlSaveFile(const char *filename, xmlDocPtr cur) {
Daniel Veillardf012a642001-07-23 19:10:52 +00006686 return(xmlSaveFormatFileEnc(filename, cur, NULL, 0));
Daniel Veillard67fee942001-04-26 18:59:03 +00006687}
6688