blob: cf50bcee574a62b3551e682a70058b12e2b71458 [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 *
Daniel Veillardd5c2f922002-11-21 14:10:52 +00004 * References:
5 * XHTML 1.0 W3C REC: http://www.w3.org/TR/2002/REC-xhtml1-20020801/
6 *
Owen Taylor3473f882001-02-23 17:55:21 +00007 * See Copyright for the status of this software.
8 *
Daniel Veillardc5d64342001-06-24 12:13:24 +00009 * daniel@veillard.com
Owen Taylor3473f882001-02-23 17:55:21 +000010 *
Owen Taylor3473f882001-02-23 17:55:21 +000011 */
12
Daniel Veillard34ce8be2002-03-18 19:37:11 +000013#define IN_LIBXML
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>
Daniel Veillardd5c2f922002-11-21 14:10:52 +000037#ifdef LIBXML_HTML_ENABLED
38#include <libxml/HTMLtree.h>
39#endif
Owen Taylor3473f882001-02-23 17:55:21 +000040
Daniel Veillard56a4cb82001-03-24 17:00:36 +000041xmlNsPtr xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns);
42
43/************************************************************************
44 * *
45 * A few static variables and macros *
46 * *
47 ************************************************************************/
Daniel Veillardd0463562001-10-13 09:15:48 +000048/* #undef xmlStringText */
Daniel Veillard22090732001-07-16 00:06:07 +000049const xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 };
Daniel Veillardd0463562001-10-13 09:15:48 +000050/* #undef xmlStringTextNoenc */
Daniel Veillard22090732001-07-16 00:06:07 +000051const xmlChar xmlStringTextNoenc[] =
Owen Taylor3473f882001-02-23 17:55:21 +000052 { 't', 'e', 'x', 't', 'n', 'o', 'e', 'n', 'c', 0 };
Daniel Veillardd0463562001-10-13 09:15:48 +000053/* #undef xmlStringComment */
Daniel Veillard22090732001-07-16 00:06:07 +000054const xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 };
55
Owen Taylor3473f882001-02-23 17:55:21 +000056static int xmlCompressMode = 0;
57static int xmlCheckDTD = 1;
Owen Taylor3473f882001-02-23 17:55:21 +000058
Owen Taylor3473f882001-02-23 17:55:21 +000059#define UPDATE_LAST_CHILD_AND_PARENT(n) if ((n) != NULL) { \
60 xmlNodePtr ulccur = (n)->children; \
61 if (ulccur == NULL) { \
62 (n)->last = NULL; \
63 } else { \
64 while (ulccur->next != NULL) { \
65 ulccur->parent = (n); \
66 ulccur = ulccur->next; \
67 } \
68 ulccur->parent = (n); \
69 (n)->last = ulccur; \
70}}
71
72/* #define DEBUG_BUFFER */
73/* #define DEBUG_TREE */
74
75/************************************************************************
76 * *
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +000077 * Functions to move to entities.c once the *
78 * API freeze is smoothen and they can be made public. *
79 * *
80 ************************************************************************/
81#include <libxml/hash.h>
82
83/**
84 * xmlGetEntityFromDtd:
85 * @dtd: A pointer to the DTD to search
86 * @name: The entity name
87 *
88 * Do an entity lookup in the DTD entity hash table and
89 * return the corresponding entity, if found.
90 *
91 * Returns A pointer to the entity structure or NULL if not found.
92 */
93static xmlEntityPtr
94xmlGetEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) {
95 xmlEntitiesTablePtr table;
96
97 if((dtd != NULL) && (dtd->entities != NULL)) {
98 table = (xmlEntitiesTablePtr) dtd->entities;
99 return((xmlEntityPtr) xmlHashLookup(table, name));
100 /* return(xmlGetEntityFromTable(table, name)); */
101 }
102 return(NULL);
103}
104/**
105 * xmlGetParameterEntityFromDtd:
106 * @dtd: A pointer to the DTD to search
107 * @name: The entity name
108 *
109 * Do an entity lookup in the DTD pararmeter entity hash table and
110 * return the corresponding entity, if found.
111 *
112 * Returns A pointer to the entity structure or NULL if not found.
113 */
114static xmlEntityPtr
115xmlGetParameterEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) {
116 xmlEntitiesTablePtr table;
117
118 if ((dtd != NULL) && (dtd->pentities != NULL)) {
119 table = (xmlEntitiesTablePtr) dtd->pentities;
120 return((xmlEntityPtr) xmlHashLookup(table, name));
121 /* return(xmlGetEntityFromTable(table, name)); */
122 }
123 return(NULL);
124}
125
126/************************************************************************
127 * *
Owen Taylor3473f882001-02-23 17:55:21 +0000128 * Allocation and deallocation of basic structures *
129 * *
130 ************************************************************************/
131
132/**
133 * xmlSetBufferAllocationScheme:
134 * @scheme: allocation method to use
135 *
136 * Set the buffer allocation method. Types are
137 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
138 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
139 * improves performance
140 */
141void
142xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme) {
143 xmlBufferAllocScheme = scheme;
144}
145
146/**
147 * xmlGetBufferAllocationScheme:
148 *
149 * Types are
150 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
151 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
152 * improves performance
153 *
154 * Returns the current allocation scheme
155 */
156xmlBufferAllocationScheme
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000157xmlGetBufferAllocationScheme(void) {
Daniel Veillarde043ee12001-04-16 14:08:07 +0000158 return(xmlBufferAllocScheme);
Owen Taylor3473f882001-02-23 17:55:21 +0000159}
160
161/**
162 * xmlNewNs:
163 * @node: the element carrying the namespace
164 * @href: the URI associated
165 * @prefix: the prefix for the namespace
166 *
167 * Creation of a new Namespace. This function will refuse to create
168 * a namespace with a similar prefix than an existing one present on this
169 * node.
170 * We use href==NULL in the case of an element creation where the namespace
171 * was not defined.
Daniel Veillardd1640922001-12-17 15:30:10 +0000172 * Returns a new namespace pointer or NULL
Owen Taylor3473f882001-02-23 17:55:21 +0000173 */
174xmlNsPtr
175xmlNewNs(xmlNodePtr node, const xmlChar *href, const xmlChar *prefix) {
176 xmlNsPtr cur;
177
178 if ((node != NULL) && (node->type != XML_ELEMENT_NODE))
179 return(NULL);
180
Daniel Veillard20ee8c02001-10-05 09:18:14 +0000181 if ((prefix != NULL) && (xmlStrEqual(prefix, BAD_CAST "xml")))
182 return(NULL);
183
Owen Taylor3473f882001-02-23 17:55:21 +0000184 /*
185 * Allocate a new Namespace and fill the fields.
186 */
187 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
188 if (cur == NULL) {
189 xmlGenericError(xmlGenericErrorContext,
190 "xmlNewNs : malloc failed\n");
191 return(NULL);
192 }
193 memset(cur, 0, sizeof(xmlNs));
194 cur->type = XML_LOCAL_NAMESPACE;
195
196 if (href != NULL)
197 cur->href = xmlStrdup(href);
198 if (prefix != NULL)
199 cur->prefix = xmlStrdup(prefix);
200
201 /*
202 * Add it at the end to preserve parsing order ...
203 * and checks for existing use of the prefix
204 */
205 if (node != NULL) {
206 if (node->nsDef == NULL) {
207 node->nsDef = cur;
208 } else {
209 xmlNsPtr prev = node->nsDef;
210
211 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
212 (xmlStrEqual(prev->prefix, cur->prefix))) {
213 xmlFreeNs(cur);
214 return(NULL);
215 }
216 while (prev->next != NULL) {
217 prev = prev->next;
218 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
219 (xmlStrEqual(prev->prefix, cur->prefix))) {
220 xmlFreeNs(cur);
221 return(NULL);
222 }
223 }
224 prev->next = cur;
225 }
226 }
227 return(cur);
228}
229
230/**
231 * xmlSetNs:
232 * @node: a node in the document
233 * @ns: a namespace pointer
234 *
235 * Associate a namespace to a node, a posteriori.
236 */
237void
238xmlSetNs(xmlNodePtr node, xmlNsPtr ns) {
239 if (node == NULL) {
240#ifdef DEBUG_TREE
241 xmlGenericError(xmlGenericErrorContext,
242 "xmlSetNs: node == NULL\n");
243#endif
244 return;
245 }
246 node->ns = ns;
247}
248
249/**
250 * xmlFreeNs:
251 * @cur: the namespace pointer
252 *
253 * Free up the structures associated to a namespace
254 */
255void
256xmlFreeNs(xmlNsPtr cur) {
257 if (cur == NULL) {
258#ifdef DEBUG_TREE
259 xmlGenericError(xmlGenericErrorContext,
260 "xmlFreeNs : ns == NULL\n");
261#endif
262 return;
263 }
264 if (cur->href != NULL) xmlFree((char *) cur->href);
265 if (cur->prefix != NULL) xmlFree((char *) cur->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000266 xmlFree(cur);
267}
268
269/**
270 * xmlFreeNsList:
271 * @cur: the first namespace pointer
272 *
273 * Free up all the structures associated to the chained namespaces.
274 */
275void
276xmlFreeNsList(xmlNsPtr cur) {
277 xmlNsPtr next;
278 if (cur == NULL) {
279#ifdef DEBUG_TREE
280 xmlGenericError(xmlGenericErrorContext,
281 "xmlFreeNsList : ns == NULL\n");
282#endif
283 return;
284 }
285 while (cur != NULL) {
286 next = cur->next;
287 xmlFreeNs(cur);
288 cur = next;
289 }
290}
291
292/**
293 * xmlNewDtd:
294 * @doc: the document pointer
295 * @name: the DTD name
296 * @ExternalID: the external ID
297 * @SystemID: the system ID
298 *
299 * Creation of a new DTD for the external subset. To create an
300 * internal subset, use xmlCreateIntSubset().
301 *
302 * Returns a pointer to the new DTD structure
303 */
304xmlDtdPtr
305xmlNewDtd(xmlDocPtr doc, const xmlChar *name,
306 const xmlChar *ExternalID, const xmlChar *SystemID) {
307 xmlDtdPtr cur;
308
309 if ((doc != NULL) && (doc->extSubset != NULL)) {
310#ifdef DEBUG_TREE
311 xmlGenericError(xmlGenericErrorContext,
312 "xmlNewDtd(%s): document %s already have a DTD %s\n",
313 /* !!! */ (char *) name, doc->name,
314 /* !!! */ (char *)doc->extSubset->name);
315#endif
316 return(NULL);
317 }
318
319 /*
320 * Allocate a new DTD and fill the fields.
321 */
322 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
323 if (cur == NULL) {
324 xmlGenericError(xmlGenericErrorContext,
325 "xmlNewDtd : malloc failed\n");
326 return(NULL);
327 }
328 memset(cur, 0 , sizeof(xmlDtd));
329 cur->type = XML_DTD_NODE;
330
331 if (name != NULL)
332 cur->name = xmlStrdup(name);
333 if (ExternalID != NULL)
334 cur->ExternalID = xmlStrdup(ExternalID);
335 if (SystemID != NULL)
336 cur->SystemID = xmlStrdup(SystemID);
337 if (doc != NULL)
338 doc->extSubset = cur;
339 cur->doc = doc;
340
341 return(cur);
342}
343
344/**
345 * xmlGetIntSubset:
346 * @doc: the document pointer
347 *
348 * Get the internal subset of a document
349 * Returns a pointer to the DTD structure or NULL if not found
350 */
351
352xmlDtdPtr
353xmlGetIntSubset(xmlDocPtr doc) {
354 xmlNodePtr cur;
355
356 if (doc == NULL)
357 return(NULL);
358 cur = doc->children;
359 while (cur != NULL) {
360 if (cur->type == XML_DTD_NODE)
361 return((xmlDtdPtr) cur);
362 cur = cur->next;
363 }
364 return((xmlDtdPtr) doc->intSubset);
365}
366
367/**
368 * xmlCreateIntSubset:
369 * @doc: the document pointer
370 * @name: the DTD name
Daniel Veillarde356c282001-03-10 12:32:04 +0000371 * @ExternalID: the external (PUBLIC) ID
Owen Taylor3473f882001-02-23 17:55:21 +0000372 * @SystemID: the system ID
373 *
374 * Create the internal subset of a document
375 * Returns a pointer to the new DTD structure
376 */
377xmlDtdPtr
378xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name,
379 const xmlChar *ExternalID, const xmlChar *SystemID) {
380 xmlDtdPtr cur;
381
382 if ((doc != NULL) && (xmlGetIntSubset(doc) != NULL)) {
383#ifdef DEBUG_TREE
384 xmlGenericError(xmlGenericErrorContext,
385
386 "xmlCreateIntSubset(): document %s already have an internal subset\n",
387 doc->name);
388#endif
389 return(NULL);
390 }
391
392 /*
393 * Allocate a new DTD and fill the fields.
394 */
395 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
396 if (cur == NULL) {
397 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +0000398 "xmlCreateIntSubset : malloc failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000399 return(NULL);
400 }
401 memset(cur, 0, sizeof(xmlDtd));
402 cur->type = XML_DTD_NODE;
403
404 if (name != NULL)
405 cur->name = xmlStrdup(name);
406 if (ExternalID != NULL)
407 cur->ExternalID = xmlStrdup(ExternalID);
408 if (SystemID != NULL)
409 cur->SystemID = xmlStrdup(SystemID);
410 if (doc != NULL) {
411 doc->intSubset = cur;
412 cur->parent = doc;
413 cur->doc = doc;
414 if (doc->children == NULL) {
415 doc->children = (xmlNodePtr) cur;
416 doc->last = (xmlNodePtr) cur;
417 } else {
Owen Taylor3473f882001-02-23 17:55:21 +0000418 if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillarde356c282001-03-10 12:32:04 +0000419 xmlNodePtr prev;
420
Owen Taylor3473f882001-02-23 17:55:21 +0000421 prev = doc->children;
422 prev->prev = (xmlNodePtr) cur;
423 cur->next = prev;
424 doc->children = (xmlNodePtr) cur;
425 } else {
Daniel Veillarde356c282001-03-10 12:32:04 +0000426 xmlNodePtr next;
427
428 next = doc->children;
429 while ((next != NULL) && (next->type != XML_ELEMENT_NODE))
430 next = next->next;
431 if (next == NULL) {
432 cur->prev = doc->last;
433 cur->prev->next = (xmlNodePtr) cur;
434 cur->next = NULL;
435 doc->last = (xmlNodePtr) cur;
436 } else {
437 cur->next = next;
438 cur->prev = next->prev;
439 if (cur->prev == NULL)
440 doc->children = (xmlNodePtr) cur;
441 else
442 cur->prev->next = (xmlNodePtr) cur;
443 next->prev = (xmlNodePtr) cur;
444 }
Owen Taylor3473f882001-02-23 17:55:21 +0000445 }
446 }
447 }
448 return(cur);
449}
450
451/**
452 * xmlFreeDtd:
453 * @cur: the DTD structure to free up
454 *
455 * Free a DTD structure.
456 */
457void
458xmlFreeDtd(xmlDtdPtr cur) {
459 if (cur == NULL) {
460#ifdef DEBUG_TREE
461 xmlGenericError(xmlGenericErrorContext,
462 "xmlFreeDtd : DTD == NULL\n");
463#endif
464 return;
465 }
466 if (cur->children != NULL) {
467 xmlNodePtr next, c = cur->children;
468
469 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000470 * Cleanup all the DTD comments they are not in the DTD
Owen Taylor3473f882001-02-23 17:55:21 +0000471 * indexes.
472 */
473 while (c != NULL) {
474 next = c->next;
475 if (c->type == XML_COMMENT_NODE) {
476 xmlUnlinkNode(c);
477 xmlFreeNode(c);
478 }
479 c = next;
480 }
481 }
482 if (cur->name != NULL) xmlFree((char *) cur->name);
483 if (cur->SystemID != NULL) xmlFree((char *) cur->SystemID);
484 if (cur->ExternalID != NULL) xmlFree((char *) cur->ExternalID);
485 /* TODO !!! */
486 if (cur->notations != NULL)
487 xmlFreeNotationTable((xmlNotationTablePtr) cur->notations);
488
489 if (cur->elements != NULL)
490 xmlFreeElementTable((xmlElementTablePtr) cur->elements);
491 if (cur->attributes != NULL)
492 xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes);
493 if (cur->entities != NULL)
494 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities);
495 if (cur->pentities != NULL)
496 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->pentities);
497
Owen Taylor3473f882001-02-23 17:55:21 +0000498 xmlFree(cur);
499}
500
501/**
502 * xmlNewDoc:
503 * @version: xmlChar string giving the version of XML "1.0"
504 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000505 * Creates a new XML document
506 *
Owen Taylor3473f882001-02-23 17:55:21 +0000507 * Returns a new document
508 */
509xmlDocPtr
510xmlNewDoc(const xmlChar *version) {
511 xmlDocPtr cur;
512
513 if (version == NULL)
514 version = (const xmlChar *) "1.0";
515
516 /*
517 * Allocate a new document and fill the fields.
518 */
519 cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
520 if (cur == NULL) {
521 xmlGenericError(xmlGenericErrorContext,
522 "xmlNewDoc : malloc failed\n");
523 return(NULL);
524 }
525 memset(cur, 0, sizeof(xmlDoc));
526 cur->type = XML_DOCUMENT_NODE;
527
528 cur->version = xmlStrdup(version);
529 cur->standalone = -1;
530 cur->compression = -1; /* not initialized */
531 cur->doc = cur;
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000532 cur->charset = XML_CHAR_ENCODING_UTF8;
Owen Taylor3473f882001-02-23 17:55:21 +0000533 return(cur);
534}
535
536/**
537 * xmlFreeDoc:
538 * @cur: pointer to the document
Owen Taylor3473f882001-02-23 17:55:21 +0000539 *
540 * Free up all the structures used by a document, tree included.
541 */
542void
543xmlFreeDoc(xmlDocPtr cur) {
Daniel Veillarda9142e72001-06-19 11:07:54 +0000544 xmlDtdPtr extSubset, intSubset;
545
Owen Taylor3473f882001-02-23 17:55:21 +0000546 if (cur == NULL) {
547#ifdef DEBUG_TREE
548 xmlGenericError(xmlGenericErrorContext,
549 "xmlFreeDoc : document == NULL\n");
550#endif
551 return;
552 }
Daniel Veillard76d66f42001-05-16 21:05:17 +0000553 /*
554 * Do this before freeing the children list to avoid ID lookups
555 */
556 if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids);
557 cur->ids = NULL;
558 if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
559 cur->refs = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +0000560 extSubset = cur->extSubset;
561 intSubset = cur->intSubset;
Daniel Veillard5997aca2002-03-18 18:36:20 +0000562 if (intSubset == extSubset)
563 extSubset = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +0000564 if (extSubset != NULL) {
Daniel Veillard76d66f42001-05-16 21:05:17 +0000565 xmlUnlinkNode((xmlNodePtr) cur->extSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +0000566 cur->extSubset = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +0000567 xmlFreeDtd(extSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +0000568 }
Daniel Veillarda9142e72001-06-19 11:07:54 +0000569 if (intSubset != NULL) {
Daniel Veillard76d66f42001-05-16 21:05:17 +0000570 xmlUnlinkNode((xmlNodePtr) cur->intSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +0000571 cur->intSubset = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +0000572 xmlFreeDtd(intSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +0000573 }
574
575 if (cur->children != NULL) xmlFreeNodeList(cur->children);
576
Owen Taylor3473f882001-02-23 17:55:21 +0000577 if (cur->version != NULL) xmlFree((char *) cur->version);
578 if (cur->name != NULL) xmlFree((char *) cur->name);
579 if (cur->encoding != NULL) xmlFree((char *) cur->encoding);
Owen Taylor3473f882001-02-23 17:55:21 +0000580 if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
Owen Taylor3473f882001-02-23 17:55:21 +0000581 if (cur->URL != NULL) xmlFree((char *) cur->URL);
Owen Taylor3473f882001-02-23 17:55:21 +0000582 xmlFree(cur);
583}
584
585/**
586 * xmlStringLenGetNodeList:
587 * @doc: the document
588 * @value: the value of the text
589 * @len: the length of the string value
590 *
591 * Parse the value string and build the node list associated. Should
592 * produce a flat tree with only TEXTs and ENTITY_REFs.
593 * Returns a pointer to the first child
594 */
595xmlNodePtr
596xmlStringLenGetNodeList(xmlDocPtr doc, const xmlChar *value, int len) {
597 xmlNodePtr ret = NULL, last = NULL;
598 xmlNodePtr node;
599 xmlChar *val;
600 const xmlChar *cur = value;
601 const xmlChar *q;
602 xmlEntityPtr ent;
603
604 if (value == NULL) return(NULL);
605
606 q = cur;
607 while ((*cur != 0) && (cur - value < len)) {
608 if (*cur == '&') {
609 /*
610 * Save the current text.
611 */
612 if (cur != q) {
613 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
614 xmlNodeAddContentLen(last, q, cur - q);
615 } else {
616 node = xmlNewDocTextLen(doc, q, cur - q);
617 if (node == NULL) return(ret);
618 if (last == NULL)
619 last = ret = node;
620 else {
621 last->next = node;
622 node->prev = last;
623 last = node;
624 }
625 }
626 }
627 /*
628 * Read the entity string
629 */
630 cur++;
631 q = cur;
632 while ((*cur != 0) && (cur - value < len) && (*cur != ';')) cur++;
633 if ((*cur == 0) || (cur - value >= len)) {
634#ifdef DEBUG_TREE
635 xmlGenericError(xmlGenericErrorContext,
636 "xmlStringLenGetNodeList: unterminated entity %30s\n", q);
637#endif
638 return(ret);
639 }
640 if (cur != q) {
641 /*
642 * Predefined entities don't generate nodes
643 */
644 val = xmlStrndup(q, cur - q);
645 ent = xmlGetDocEntity(doc, val);
646 if ((ent != NULL) &&
647 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
648 if (last == NULL) {
649 node = xmlNewDocText(doc, ent->content);
650 last = ret = node;
651 } else
652 xmlNodeAddContent(last, ent->content);
653
654 } else {
655 /*
656 * Create a new REFERENCE_REF node
657 */
658 node = xmlNewReference(doc, val);
659 if (node == NULL) {
660 if (val != NULL) xmlFree(val);
661 return(ret);
662 }
Daniel Veillardbf8dae82002-04-18 16:39:10 +0000663 else if ((ent != NULL) && (ent->children == NULL)) {
664 xmlNodePtr tmp;
665
666 ent->children =
667 xmlStringGetNodeList(doc, (const xmlChar*)node->content);
668 tmp = ent->children;
669 while (tmp) {
670 tmp->parent = (xmlNodePtr)ent;
671 tmp = tmp->next;
672 }
673 }
Owen Taylor3473f882001-02-23 17:55:21 +0000674 if (last == NULL)
675 last = ret = node;
676 else {
677 last->next = node;
678 node->prev = last;
679 last = node;
680 }
681 }
682 xmlFree(val);
683 }
684 cur++;
685 q = cur;
686 } else
687 cur++;
688 }
689 if (cur != q) {
690 /*
691 * Handle the last piece of text.
692 */
693 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
694 xmlNodeAddContentLen(last, q, cur - q);
695 } else {
696 node = xmlNewDocTextLen(doc, q, cur - q);
697 if (node == NULL) return(ret);
698 if (last == NULL)
699 last = ret = node;
700 else {
701 last->next = node;
702 node->prev = last;
703 last = node;
704 }
705 }
706 }
707 return(ret);
708}
709
710/**
711 * xmlStringGetNodeList:
712 * @doc: the document
713 * @value: the value of the attribute
714 *
715 * Parse the value string and build the node list associated. Should
716 * produce a flat tree with only TEXTs and ENTITY_REFs.
717 * Returns a pointer to the first child
718 */
719xmlNodePtr
720xmlStringGetNodeList(xmlDocPtr doc, const xmlChar *value) {
721 xmlNodePtr ret = NULL, last = NULL;
722 xmlNodePtr node;
723 xmlChar *val;
724 const xmlChar *cur = value;
725 const xmlChar *q;
726 xmlEntityPtr ent;
727
728 if (value == NULL) return(NULL);
729
730 q = cur;
731 while (*cur != 0) {
Daniel Veillardbdb9ba72001-04-11 11:28:06 +0000732 if (cur[0] == '&') {
733 int charval = 0;
734 xmlChar tmp;
735
Owen Taylor3473f882001-02-23 17:55:21 +0000736 /*
737 * Save the current text.
738 */
739 if (cur != q) {
740 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
741 xmlNodeAddContentLen(last, q, cur - q);
742 } else {
743 node = xmlNewDocTextLen(doc, q, cur - q);
744 if (node == NULL) return(ret);
745 if (last == NULL)
746 last = ret = node;
747 else {
748 last->next = node;
749 node->prev = last;
750 last = node;
751 }
752 }
753 }
Owen Taylor3473f882001-02-23 17:55:21 +0000754 q = cur;
Daniel Veillardbdb9ba72001-04-11 11:28:06 +0000755 if ((cur[1] == '#') && (cur[2] == 'x')) {
756 cur += 3;
757 tmp = *cur;
758 while (tmp != ';') { /* Non input consuming loop */
759 if ((tmp >= '0') && (tmp <= '9'))
760 charval = charval * 16 + (tmp - '0');
761 else if ((tmp >= 'a') && (tmp <= 'f'))
762 charval = charval * 16 + (tmp - 'a') + 10;
763 else if ((tmp >= 'A') && (tmp <= 'F'))
764 charval = charval * 16 + (tmp - 'A') + 10;
Owen Taylor3473f882001-02-23 17:55:21 +0000765 else {
Daniel Veillardbdb9ba72001-04-11 11:28:06 +0000766 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +0000767 "xmlStringGetNodeList: invalid hexadecimal charvalue\n");
Daniel Veillardbdb9ba72001-04-11 11:28:06 +0000768 charval = 0;
769 break;
770 }
771 cur++;
772 tmp = *cur;
773 }
774 if (tmp == ';')
775 cur++;
776 q = cur;
777 } else if (cur[1] == '#') {
778 cur += 2;
779 tmp = *cur;
780 while (tmp != ';') { /* Non input consuming loops */
781 if ((tmp >= '0') && (tmp <= '9'))
782 charval = charval * 10 + (tmp - '0');
783 else {
784 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +0000785 "xmlStringGetNodeList: invalid decimal charvalue\n");
Daniel Veillardbdb9ba72001-04-11 11:28:06 +0000786 charval = 0;
787 break;
788 }
789 cur++;
790 tmp = *cur;
791 }
792 if (tmp == ';')
793 cur++;
794 q = cur;
795 } else {
796 /*
797 * Read the entity string
798 */
799 cur++;
800 q = cur;
801 while ((*cur != 0) && (*cur != ';')) cur++;
802 if (*cur == 0) {
803#ifdef DEBUG_TREE
804 xmlGenericError(xmlGenericErrorContext,
805 "xmlStringGetNodeList: unterminated entity %30s\n", q);
806#endif
807 return(ret);
808 }
809 if (cur != q) {
810 /*
811 * Predefined entities don't generate nodes
812 */
813 val = xmlStrndup(q, cur - q);
814 ent = xmlGetDocEntity(doc, val);
815 if ((ent != NULL) &&
816 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
817 if (last == NULL) {
818 node = xmlNewDocText(doc, ent->content);
819 last = ret = node;
Daniel Veillard6f42c132002-01-06 23:05:13 +0000820 } else if (last->type != XML_TEXT_NODE) {
821 node = xmlNewDocText(doc, ent->content);
822 last = xmlAddNextSibling(last, node);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +0000823 } else
824 xmlNodeAddContent(last, ent->content);
825
826 } else {
827 /*
828 * Create a new REFERENCE_REF node
829 */
830 node = xmlNewReference(doc, val);
831 if (node == NULL) {
832 if (val != NULL) xmlFree(val);
833 return(ret);
834 }
Daniel Veillardbf8dae82002-04-18 16:39:10 +0000835 else if ((ent != NULL) && (ent->children == NULL)) {
836 xmlNodePtr temp;
837
838 ent->children = xmlStringGetNodeList(doc,
839 (const xmlChar*)node->content);
840 temp = ent->children;
841 while (temp) {
842 temp->parent = (xmlNodePtr)ent;
843 temp = temp->next;
844 }
845 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +0000846 if (last == NULL) {
847 last = ret = node;
848 } else {
849 last = xmlAddNextSibling(last, node);
850 }
851 }
852 xmlFree(val);
853 }
854 cur++;
855 q = cur;
856 }
857 if (charval != 0) {
858 xmlChar buf[10];
859 int len;
860
861 len = xmlCopyCharMultiByte(buf, charval);
862 buf[len] = 0;
863 node = xmlNewDocText(doc, buf);
864 if (node != NULL) {
865 if (last == NULL) {
866 last = ret = node;
867 } else {
868 last = xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +0000869 }
870 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +0000871
872 charval = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000873 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +0000874 } else
Owen Taylor3473f882001-02-23 17:55:21 +0000875 cur++;
876 }
Daniel Veillard75bea542001-05-11 17:41:21 +0000877 if ((cur != q) || (ret == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000878 /*
879 * Handle the last piece of text.
880 */
881 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
882 xmlNodeAddContentLen(last, q, cur - q);
883 } else {
884 node = xmlNewDocTextLen(doc, q, cur - q);
885 if (node == NULL) return(ret);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +0000886 if (last == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +0000887 last = ret = node;
Daniel Veillardbdb9ba72001-04-11 11:28:06 +0000888 } else {
889 last = xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +0000890 }
891 }
892 }
893 return(ret);
894}
895
896/**
897 * xmlNodeListGetString:
898 * @doc: the document
899 * @list: a Node list
900 * @inLine: should we replace entity contents or show their external form
901 *
902 * Returns the string equivalent to the text contained in the Node list
903 * made of TEXTs and ENTITY_REFs
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000904 * Returns a pointer to the string copy, the caller must free it with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +0000905 */
906xmlChar *
Daniel Veillard7646b182002-04-20 06:41:40 +0000907xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine)
908{
Owen Taylor3473f882001-02-23 17:55:21 +0000909 xmlNodePtr node = list;
910 xmlChar *ret = NULL;
911 xmlEntityPtr ent;
912
Daniel Veillard7646b182002-04-20 06:41:40 +0000913 if (list == NULL)
914 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000915
916 while (node != NULL) {
917 if ((node->type == XML_TEXT_NODE) ||
Daniel Veillard7646b182002-04-20 06:41:40 +0000918 (node->type == XML_CDATA_SECTION_NODE)) {
919 if (inLine) {
920 ret = xmlStrcat(ret, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +0000921 } else {
Daniel Veillard7646b182002-04-20 06:41:40 +0000922 xmlChar *buffer;
Owen Taylor3473f882001-02-23 17:55:21 +0000923
Daniel Veillard7646b182002-04-20 06:41:40 +0000924 buffer = xmlEncodeEntitiesReentrant(doc, node->content);
925 if (buffer != NULL) {
926 ret = xmlStrcat(ret, buffer);
927 xmlFree(buffer);
928 }
929 }
930 } else if (node->type == XML_ENTITY_REF_NODE) {
931 if (inLine) {
932 ent = xmlGetDocEntity(doc, node->name);
933 if (ent != NULL) {
934 xmlChar *buffer;
935
936 /* an entity content can be any "well balanced chunk",
937 * i.e. the result of the content [43] production:
938 * http://www.w3.org/TR/REC-xml#NT-content.
939 * So it can contain text, CDATA section or nested
940 * entity reference nodes (among others).
941 * -> we recursive call xmlNodeListGetString()
942 * which handles these types */
943 buffer = xmlNodeListGetString(doc, ent->children, 1);
944 if (buffer != NULL) {
945 ret = xmlStrcat(ret, buffer);
946 xmlFree(buffer);
947 }
948 } else {
949 ret = xmlStrcat(ret, node->content);
950 }
951 } else {
952 xmlChar buf[2];
953
954 buf[0] = '&';
955 buf[1] = 0;
956 ret = xmlStrncat(ret, buf, 1);
957 ret = xmlStrcat(ret, node->name);
958 buf[0] = ';';
959 buf[1] = 0;
960 ret = xmlStrncat(ret, buf, 1);
961 }
962 }
963#if 0
964 else {
965 xmlGenericError(xmlGenericErrorContext,
966 "xmlGetNodeListString : invalid node type %d\n",
967 node->type);
968 }
969#endif
970 node = node->next;
971 }
972 return (ret);
973}
Owen Taylor3473f882001-02-23 17:55:21 +0000974/**
975 * xmlNodeListGetRawString:
976 * @doc: the document
977 * @list: a Node list
978 * @inLine: should we replace entity contents or show their external form
979 *
980 * Returns the string equivalent to the text contained in the Node list
981 * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString()
982 * this function doesn't do any character encoding handling.
983 *
Daniel Veillardbd9afb52002-09-25 22:25:35 +0000984 * Returns a pointer to the string copy, the caller must free it with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +0000985 */
986xmlChar *
Daniel Veillard7646b182002-04-20 06:41:40 +0000987xmlNodeListGetRawString(xmlDocPtr doc, xmlNodePtr list, int inLine)
988{
Owen Taylor3473f882001-02-23 17:55:21 +0000989 xmlNodePtr node = list;
990 xmlChar *ret = NULL;
991 xmlEntityPtr ent;
992
Daniel Veillard7646b182002-04-20 06:41:40 +0000993 if (list == NULL)
994 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000995
996 while (node != NULL) {
Daniel Veillard7db37732001-07-12 01:20:08 +0000997 if ((node->type == XML_TEXT_NODE) ||
Daniel Veillard7646b182002-04-20 06:41:40 +0000998 (node->type == XML_CDATA_SECTION_NODE)) {
999 if (inLine) {
1000 ret = xmlStrcat(ret, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001001 } else {
Daniel Veillard7646b182002-04-20 06:41:40 +00001002 xmlChar *buffer;
1003
1004 buffer = xmlEncodeSpecialChars(doc, node->content);
1005 if (buffer != NULL) {
1006 ret = xmlStrcat(ret, buffer);
1007 xmlFree(buffer);
1008 }
1009 }
1010 } else if (node->type == XML_ENTITY_REF_NODE) {
1011 if (inLine) {
1012 ent = xmlGetDocEntity(doc, node->name);
1013 if (ent != NULL) {
1014 xmlChar *buffer;
1015
1016 /* an entity content can be any "well balanced chunk",
1017 * i.e. the result of the content [43] production:
1018 * http://www.w3.org/TR/REC-xml#NT-content.
1019 * So it can contain text, CDATA section or nested
1020 * entity reference nodes (among others).
1021 * -> we recursive call xmlNodeListGetRawString()
1022 * which handles these types */
1023 buffer =
1024 xmlNodeListGetRawString(doc, ent->children, 1);
1025 if (buffer != NULL) {
1026 ret = xmlStrcat(ret, buffer);
1027 xmlFree(buffer);
1028 }
1029 } else {
1030 ret = xmlStrcat(ret, node->content);
1031 }
1032 } else {
1033 xmlChar buf[2];
1034
1035 buf[0] = '&';
1036 buf[1] = 0;
1037 ret = xmlStrncat(ret, buf, 1);
1038 ret = xmlStrcat(ret, node->name);
1039 buf[0] = ';';
1040 buf[1] = 0;
1041 ret = xmlStrncat(ret, buf, 1);
1042 }
1043 }
Owen Taylor3473f882001-02-23 17:55:21 +00001044#if 0
Daniel Veillard7646b182002-04-20 06:41:40 +00001045 else {
1046 xmlGenericError(xmlGenericErrorContext,
1047 "xmlGetNodeListString : invalid node type %d\n",
1048 node->type);
1049 }
Owen Taylor3473f882001-02-23 17:55:21 +00001050#endif
Daniel Veillard7646b182002-04-20 06:41:40 +00001051 node = node->next;
Owen Taylor3473f882001-02-23 17:55:21 +00001052 }
Daniel Veillard7646b182002-04-20 06:41:40 +00001053 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00001054}
1055
1056/**
1057 * xmlNewProp:
1058 * @node: the holding node
1059 * @name: the name of the attribute
1060 * @value: the value of the attribute
1061 *
1062 * Create a new property carried by a node.
1063 * Returns a pointer to the attribute
1064 */
1065xmlAttrPtr
1066xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
1067 xmlAttrPtr cur;
1068 xmlDocPtr doc = NULL;
1069
1070 if (name == NULL) {
1071#ifdef DEBUG_TREE
1072 xmlGenericError(xmlGenericErrorContext,
1073 "xmlNewProp : name == NULL\n");
1074#endif
1075 return(NULL);
1076 }
1077
1078 /*
1079 * Allocate a new property and fill the fields.
1080 */
1081 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1082 if (cur == NULL) {
1083 xmlGenericError(xmlGenericErrorContext,
1084 "xmlNewProp : malloc failed\n");
1085 return(NULL);
1086 }
1087 memset(cur, 0, sizeof(xmlAttr));
1088 cur->type = XML_ATTRIBUTE_NODE;
1089
1090 cur->parent = node;
1091 if (node != NULL) {
1092 doc = node->doc;
1093 cur->doc = doc;
1094 }
1095 cur->name = xmlStrdup(name);
1096 if (value != NULL) {
1097 xmlChar *buffer;
1098 xmlNodePtr tmp;
1099
1100 buffer = xmlEncodeEntitiesReentrant(doc, value);
1101 cur->children = xmlStringGetNodeList(doc, buffer);
1102 cur->last = NULL;
1103 tmp = cur->children;
1104 while (tmp != NULL) {
1105 tmp->parent = (xmlNodePtr) cur;
1106 tmp->doc = doc;
1107 if (tmp->next == NULL)
1108 cur->last = tmp;
1109 tmp = tmp->next;
1110 }
1111 xmlFree(buffer);
1112 }
1113
1114 /*
1115 * Add it at the end to preserve parsing order ...
1116 */
1117 if (node != NULL) {
1118 if (node->properties == NULL) {
1119 node->properties = cur;
1120 } else {
1121 xmlAttrPtr prev = node->properties;
1122
1123 while (prev->next != NULL) prev = prev->next;
1124 prev->next = cur;
1125 cur->prev = prev;
1126 }
1127 }
1128 return(cur);
1129}
1130
1131/**
1132 * xmlNewNsProp:
1133 * @node: the holding node
1134 * @ns: the namespace
1135 * @name: the name of the attribute
1136 * @value: the value of the attribute
1137 *
1138 * Create a new property tagged with a namespace and carried by a node.
1139 * Returns a pointer to the attribute
1140 */
1141xmlAttrPtr
1142xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
1143 const xmlChar *value) {
1144 xmlAttrPtr cur;
Daniel Veillarda682b212001-06-07 19:59:42 +00001145 xmlDocPtr doc = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001146
1147 if (name == NULL) {
1148#ifdef DEBUG_TREE
1149 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00001150 "xmlNewNsProp : name == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001151#endif
1152 return(NULL);
1153 }
1154
1155 /*
1156 * Allocate a new property and fill the fields.
1157 */
1158 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1159 if (cur == NULL) {
1160 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00001161 "xmlNewNsProp : malloc failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001162 return(NULL);
1163 }
1164 memset(cur, 0, sizeof(xmlAttr));
1165 cur->type = XML_ATTRIBUTE_NODE;
1166
1167 cur->parent = node;
Daniel Veillarda682b212001-06-07 19:59:42 +00001168 if (node != NULL) {
1169 doc = node->doc;
1170 cur->doc = doc;
1171 }
Owen Taylor3473f882001-02-23 17:55:21 +00001172 cur->ns = ns;
1173 cur->name = xmlStrdup(name);
1174 if (value != NULL) {
1175 xmlChar *buffer;
1176 xmlNodePtr tmp;
1177
Daniel Veillarda682b212001-06-07 19:59:42 +00001178 buffer = xmlEncodeEntitiesReentrant(doc, value);
1179 cur->children = xmlStringGetNodeList(doc, buffer);
Owen Taylor3473f882001-02-23 17:55:21 +00001180 cur->last = NULL;
1181 tmp = cur->children;
1182 while (tmp != NULL) {
1183 tmp->parent = (xmlNodePtr) cur;
1184 if (tmp->next == NULL)
1185 cur->last = tmp;
1186 tmp = tmp->next;
1187 }
1188 xmlFree(buffer);
1189 }
1190
1191 /*
1192 * Add it at the end to preserve parsing order ...
1193 */
1194 if (node != NULL) {
1195 if (node->properties == NULL) {
1196 node->properties = cur;
1197 } else {
1198 xmlAttrPtr prev = node->properties;
1199
1200 while (prev->next != NULL) prev = prev->next;
1201 prev->next = cur;
1202 cur->prev = prev;
1203 }
1204 }
1205 return(cur);
1206}
1207
1208/**
Daniel Veillard46de64e2002-05-29 08:21:33 +00001209 * xmlNewNsPropEatName:
1210 * @node: the holding node
1211 * @ns: the namespace
1212 * @name: the name of the attribute
1213 * @value: the value of the attribute
1214 *
1215 * Create a new property tagged with a namespace and carried by a node.
1216 * Returns a pointer to the attribute
1217 */
1218xmlAttrPtr
1219xmlNewNsPropEatName(xmlNodePtr node, xmlNsPtr ns, xmlChar *name,
1220 const xmlChar *value) {
1221 xmlAttrPtr cur;
1222 xmlDocPtr doc = NULL;
1223
1224 if (name == NULL) {
1225#ifdef DEBUG_TREE
1226 xmlGenericError(xmlGenericErrorContext,
1227 "xmlNewNsPropEatName : name == NULL\n");
1228#endif
1229 return(NULL);
1230 }
1231
1232 /*
1233 * Allocate a new property and fill the fields.
1234 */
1235 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1236 if (cur == NULL) {
1237 xmlGenericError(xmlGenericErrorContext,
1238 "xmlNewNsPropEatName : malloc failed\n");
1239 return(NULL);
1240 }
1241 memset(cur, 0, sizeof(xmlAttr));
1242 cur->type = XML_ATTRIBUTE_NODE;
1243
1244 cur->parent = node;
1245 if (node != NULL) {
1246 doc = node->doc;
1247 cur->doc = doc;
1248 }
1249 cur->ns = ns;
1250 cur->name = name;
1251 if (value != NULL) {
1252 xmlChar *buffer;
1253 xmlNodePtr tmp;
1254
1255 buffer = xmlEncodeEntitiesReentrant(doc, value);
1256 cur->children = xmlStringGetNodeList(doc, buffer);
1257 cur->last = NULL;
1258 tmp = cur->children;
1259 while (tmp != NULL) {
1260 tmp->parent = (xmlNodePtr) cur;
1261 if (tmp->next == NULL)
1262 cur->last = tmp;
1263 tmp = tmp->next;
1264 }
1265 xmlFree(buffer);
1266 }
1267
1268 /*
1269 * Add it at the end to preserve parsing order ...
1270 */
1271 if (node != NULL) {
1272 if (node->properties == NULL) {
1273 node->properties = cur;
1274 } else {
1275 xmlAttrPtr prev = node->properties;
1276
1277 while (prev->next != NULL) prev = prev->next;
1278 prev->next = cur;
1279 cur->prev = prev;
1280 }
1281 }
1282 return(cur);
1283}
1284
1285/**
Owen Taylor3473f882001-02-23 17:55:21 +00001286 * xmlNewDocProp:
1287 * @doc: the document
1288 * @name: the name of the attribute
1289 * @value: the value of the attribute
1290 *
1291 * Create a new property carried by a document.
1292 * Returns a pointer to the attribute
1293 */
1294xmlAttrPtr
1295xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) {
1296 xmlAttrPtr cur;
1297
1298 if (name == NULL) {
1299#ifdef DEBUG_TREE
1300 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00001301 "xmlNewDocProp : name == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001302#endif
1303 return(NULL);
1304 }
1305
1306 /*
1307 * Allocate a new property and fill the fields.
1308 */
1309 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1310 if (cur == NULL) {
1311 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00001312 "xmlNewDocProp : malloc failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001313 return(NULL);
1314 }
1315 memset(cur, 0, sizeof(xmlAttr));
1316 cur->type = XML_ATTRIBUTE_NODE;
1317
1318 cur->name = xmlStrdup(name);
1319 cur->doc = doc;
1320 if (value != NULL) {
1321 xmlNodePtr tmp;
1322
1323 cur->children = xmlStringGetNodeList(doc, value);
1324 cur->last = NULL;
1325
1326 tmp = cur->children;
1327 while (tmp != NULL) {
1328 tmp->parent = (xmlNodePtr) cur;
1329 if (tmp->next == NULL)
1330 cur->last = tmp;
1331 tmp = tmp->next;
1332 }
1333 }
1334 return(cur);
1335}
1336
1337/**
1338 * xmlFreePropList:
1339 * @cur: the first property in the list
1340 *
1341 * Free a property and all its siblings, all the children are freed too.
1342 */
1343void
1344xmlFreePropList(xmlAttrPtr cur) {
1345 xmlAttrPtr next;
1346 if (cur == NULL) {
1347#ifdef DEBUG_TREE
1348 xmlGenericError(xmlGenericErrorContext,
1349 "xmlFreePropList : property == NULL\n");
1350#endif
1351 return;
1352 }
1353 while (cur != NULL) {
1354 next = cur->next;
1355 xmlFreeProp(cur);
1356 cur = next;
1357 }
1358}
1359
1360/**
1361 * xmlFreeProp:
1362 * @cur: an attribute
1363 *
1364 * Free one attribute, all the content is freed too
1365 */
1366void
1367xmlFreeProp(xmlAttrPtr cur) {
1368 if (cur == NULL) {
1369#ifdef DEBUG_TREE
1370 xmlGenericError(xmlGenericErrorContext,
1371 "xmlFreeProp : property == NULL\n");
1372#endif
1373 return;
1374 }
1375 /* Check for ID removal -> leading to invalid references ! */
Daniel Veillard76d66f42001-05-16 21:05:17 +00001376 if ((cur->parent != NULL) && (cur->parent->doc != NULL) &&
1377 ((cur->parent->doc->intSubset != NULL) ||
1378 (cur->parent->doc->extSubset != NULL))) {
1379 if (xmlIsID(cur->parent->doc, cur->parent, cur))
1380 xmlRemoveID(cur->parent->doc, cur);
1381 }
Owen Taylor3473f882001-02-23 17:55:21 +00001382 if (cur->name != NULL) xmlFree((char *) cur->name);
1383 if (cur->children != NULL) xmlFreeNodeList(cur->children);
Owen Taylor3473f882001-02-23 17:55:21 +00001384 xmlFree(cur);
1385}
1386
1387/**
1388 * xmlRemoveProp:
1389 * @cur: an attribute
1390 *
1391 * Unlink and free one attribute, all the content is freed too
1392 * Note this doesn't work for namespace definition attributes
1393 *
1394 * Returns 0 if success and -1 in case of error.
1395 */
1396int
1397xmlRemoveProp(xmlAttrPtr cur) {
1398 xmlAttrPtr tmp;
1399 if (cur == NULL) {
1400#ifdef DEBUG_TREE
1401 xmlGenericError(xmlGenericErrorContext,
1402 "xmlRemoveProp : cur == NULL\n");
1403#endif
1404 return(-1);
1405 }
1406 if (cur->parent == NULL) {
1407#ifdef DEBUG_TREE
1408 xmlGenericError(xmlGenericErrorContext,
1409 "xmlRemoveProp : cur->parent == NULL\n");
1410#endif
1411 return(-1);
1412 }
1413 tmp = cur->parent->properties;
1414 if (tmp == cur) {
1415 cur->parent->properties = cur->next;
1416 xmlFreeProp(cur);
1417 return(0);
1418 }
1419 while (tmp != NULL) {
1420 if (tmp->next == cur) {
1421 tmp->next = cur->next;
1422 if (tmp->next != NULL)
1423 tmp->next->prev = tmp;
1424 xmlFreeProp(cur);
1425 return(0);
1426 }
1427 tmp = tmp->next;
1428 }
1429#ifdef DEBUG_TREE
1430 xmlGenericError(xmlGenericErrorContext,
1431 "xmlRemoveProp : attribute not owned by its node\n");
1432#endif
1433 return(-1);
1434}
1435
1436/**
1437 * xmlNewPI:
1438 * @name: the processing instruction name
1439 * @content: the PI content
1440 *
1441 * Creation of a processing instruction element.
1442 * Returns a pointer to the new node object.
1443 */
1444xmlNodePtr
1445xmlNewPI(const xmlChar *name, const xmlChar *content) {
1446 xmlNodePtr cur;
1447
1448 if (name == NULL) {
1449#ifdef DEBUG_TREE
1450 xmlGenericError(xmlGenericErrorContext,
1451 "xmlNewPI : name == NULL\n");
1452#endif
1453 return(NULL);
1454 }
1455
1456 /*
1457 * Allocate a new node and fill the fields.
1458 */
1459 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1460 if (cur == NULL) {
1461 xmlGenericError(xmlGenericErrorContext,
1462 "xmlNewPI : malloc failed\n");
1463 return(NULL);
1464 }
1465 memset(cur, 0, sizeof(xmlNode));
1466 cur->type = XML_PI_NODE;
1467
1468 cur->name = xmlStrdup(name);
1469 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001470 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00001471 }
1472 return(cur);
1473}
1474
1475/**
1476 * xmlNewNode:
1477 * @ns: namespace if any
1478 * @name: the node name
1479 *
Daniel Veillardd1640922001-12-17 15:30:10 +00001480 * Creation of a new node element. @ns is optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00001481 *
1482 * Returns a pointer to the new node object.
1483 */
1484xmlNodePtr
1485xmlNewNode(xmlNsPtr ns, const xmlChar *name) {
1486 xmlNodePtr cur;
1487
1488 if (name == NULL) {
1489#ifdef DEBUG_TREE
1490 xmlGenericError(xmlGenericErrorContext,
1491 "xmlNewNode : name == NULL\n");
1492#endif
1493 return(NULL);
1494 }
1495
1496 /*
1497 * Allocate a new node and fill the fields.
1498 */
1499 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1500 if (cur == NULL) {
1501 xmlGenericError(xmlGenericErrorContext,
1502 "xmlNewNode : malloc failed\n");
1503 return(NULL);
1504 }
1505 memset(cur, 0, sizeof(xmlNode));
1506 cur->type = XML_ELEMENT_NODE;
1507
1508 cur->name = xmlStrdup(name);
1509 cur->ns = ns;
1510 return(cur);
1511}
1512
1513/**
Daniel Veillard46de64e2002-05-29 08:21:33 +00001514 * xmlNewNodeEatName:
1515 * @ns: namespace if any
1516 * @name: the node name
1517 *
1518 * Creation of a new node element. @ns is optional (NULL).
1519 *
1520 * Returns a pointer to the new node object.
1521 */
1522xmlNodePtr
1523xmlNewNodeEatName(xmlNsPtr ns, xmlChar *name) {
1524 xmlNodePtr cur;
1525
1526 if (name == NULL) {
1527#ifdef DEBUG_TREE
1528 xmlGenericError(xmlGenericErrorContext,
1529 "xmlNewNode : name == NULL\n");
1530#endif
1531 return(NULL);
1532 }
1533
1534 /*
1535 * Allocate a new node and fill the fields.
1536 */
1537 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1538 if (cur == NULL) {
1539 xmlGenericError(xmlGenericErrorContext,
1540 "xmlNewNode : malloc failed\n");
1541 return(NULL);
1542 }
1543 memset(cur, 0, sizeof(xmlNode));
1544 cur->type = XML_ELEMENT_NODE;
1545
1546 cur->name = name;
1547 cur->ns = ns;
1548 return(cur);
1549}
1550
1551/**
Owen Taylor3473f882001-02-23 17:55:21 +00001552 * xmlNewDocNode:
1553 * @doc: the document
1554 * @ns: namespace if any
1555 * @name: the node name
1556 * @content: the XML text content if any
1557 *
1558 * Creation of a new node element within a document. @ns and @content
Daniel Veillardd1640922001-12-17 15:30:10 +00001559 * are optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00001560 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
1561 * references, but XML special chars need to be escaped first by using
1562 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
1563 * need entities support.
1564 *
1565 * Returns a pointer to the new node object.
1566 */
1567xmlNodePtr
1568xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns,
1569 const xmlChar *name, const xmlChar *content) {
1570 xmlNodePtr cur;
1571
1572 cur = xmlNewNode(ns, name);
1573 if (cur != NULL) {
1574 cur->doc = doc;
1575 if (content != NULL) {
1576 cur->children = xmlStringGetNodeList(doc, content);
1577 UPDATE_LAST_CHILD_AND_PARENT(cur)
1578 }
1579 }
1580 return(cur);
1581}
1582
Daniel Veillard46de64e2002-05-29 08:21:33 +00001583/**
1584 * xmlNewDocNodeEatName:
1585 * @doc: the document
1586 * @ns: namespace if any
1587 * @name: the node name
1588 * @content: the XML text content if any
1589 *
1590 * Creation of a new node element within a document. @ns and @content
1591 * are optional (NULL).
1592 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
1593 * references, but XML special chars need to be escaped first by using
1594 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
1595 * need entities support.
1596 *
1597 * Returns a pointer to the new node object.
1598 */
1599xmlNodePtr
1600xmlNewDocNodeEatName(xmlDocPtr doc, xmlNsPtr ns,
1601 xmlChar *name, const xmlChar *content) {
1602 xmlNodePtr cur;
1603
1604 cur = xmlNewNodeEatName(ns, name);
1605 if (cur != NULL) {
1606 cur->doc = doc;
1607 if (content != NULL) {
1608 cur->children = xmlStringGetNodeList(doc, content);
1609 UPDATE_LAST_CHILD_AND_PARENT(cur)
1610 }
1611 }
1612 return(cur);
1613}
1614
Owen Taylor3473f882001-02-23 17:55:21 +00001615
1616/**
1617 * xmlNewDocRawNode:
1618 * @doc: the document
1619 * @ns: namespace if any
1620 * @name: the node name
1621 * @content: the text content if any
1622 *
1623 * Creation of a new node element within a document. @ns and @content
Daniel Veillardd1640922001-12-17 15:30:10 +00001624 * are optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00001625 *
1626 * Returns a pointer to the new node object.
1627 */
1628xmlNodePtr
1629xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns,
1630 const xmlChar *name, const xmlChar *content) {
1631 xmlNodePtr cur;
1632
1633 cur = xmlNewNode(ns, name);
1634 if (cur != NULL) {
1635 cur->doc = doc;
1636 if (content != NULL) {
1637 cur->children = xmlNewDocText(doc, content);
1638 UPDATE_LAST_CHILD_AND_PARENT(cur)
1639 }
1640 }
1641 return(cur);
1642}
1643
1644/**
1645 * xmlNewDocFragment:
1646 * @doc: the document owning the fragment
1647 *
1648 * Creation of a new Fragment node.
1649 * Returns a pointer to the new node object.
1650 */
1651xmlNodePtr
1652xmlNewDocFragment(xmlDocPtr doc) {
1653 xmlNodePtr cur;
1654
1655 /*
1656 * Allocate a new DocumentFragment node and fill the fields.
1657 */
1658 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1659 if (cur == NULL) {
1660 xmlGenericError(xmlGenericErrorContext,
1661 "xmlNewDocFragment : malloc failed\n");
1662 return(NULL);
1663 }
1664 memset(cur, 0, sizeof(xmlNode));
1665 cur->type = XML_DOCUMENT_FRAG_NODE;
1666
1667 cur->doc = doc;
1668 return(cur);
1669}
1670
1671/**
1672 * xmlNewText:
1673 * @content: the text content
1674 *
1675 * Creation of a new text node.
1676 * Returns a pointer to the new node object.
1677 */
1678xmlNodePtr
1679xmlNewText(const xmlChar *content) {
1680 xmlNodePtr cur;
1681
1682 /*
1683 * Allocate a new node and fill the fields.
1684 */
1685 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1686 if (cur == NULL) {
1687 xmlGenericError(xmlGenericErrorContext,
1688 "xmlNewText : malloc failed\n");
1689 return(NULL);
1690 }
1691 memset(cur, 0, sizeof(xmlNode));
1692 cur->type = XML_TEXT_NODE;
1693
1694 cur->name = xmlStringText;
1695 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001696 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00001697 }
1698 return(cur);
1699}
1700
1701/**
1702 * xmlNewTextChild:
1703 * @parent: the parent node
1704 * @ns: a namespace if any
1705 * @name: the name of the child
1706 * @content: the text content of the child if any.
1707 *
1708 * Creation of a new child element, added at the end of @parent children list.
Daniel Veillardd1640922001-12-17 15:30:10 +00001709 * @ns and @content parameters are optional (NULL). If content is non NULL,
Owen Taylor3473f882001-02-23 17:55:21 +00001710 * a child TEXT node will be created containing the string content.
1711 *
1712 * Returns a pointer to the new node object.
1713 */
1714xmlNodePtr
1715xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns,
1716 const xmlChar *name, const xmlChar *content) {
1717 xmlNodePtr cur, prev;
1718
1719 if (parent == NULL) {
1720#ifdef DEBUG_TREE
1721 xmlGenericError(xmlGenericErrorContext,
1722 "xmlNewTextChild : parent == NULL\n");
1723#endif
1724 return(NULL);
1725 }
1726
1727 if (name == NULL) {
1728#ifdef DEBUG_TREE
1729 xmlGenericError(xmlGenericErrorContext,
1730 "xmlNewTextChild : name == NULL\n");
1731#endif
1732 return(NULL);
1733 }
1734
1735 /*
1736 * Allocate a new node
1737 */
1738 if (ns == NULL)
1739 cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content);
1740 else
1741 cur = xmlNewDocRawNode(parent->doc, ns, name, content);
1742 if (cur == NULL) return(NULL);
1743
1744 /*
1745 * add the new element at the end of the children list.
1746 */
1747 cur->type = XML_ELEMENT_NODE;
1748 cur->parent = parent;
1749 cur->doc = parent->doc;
1750 if (parent->children == NULL) {
1751 parent->children = cur;
1752 parent->last = cur;
1753 } else {
1754 prev = parent->last;
1755 prev->next = cur;
1756 cur->prev = prev;
1757 parent->last = cur;
1758 }
1759
1760 return(cur);
1761}
1762
1763/**
1764 * xmlNewCharRef:
1765 * @doc: the document
1766 * @name: the char ref string, starting with # or "&# ... ;"
1767 *
1768 * Creation of a new character reference node.
1769 * Returns a pointer to the new node object.
1770 */
1771xmlNodePtr
1772xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) {
1773 xmlNodePtr cur;
1774
1775 /*
1776 * Allocate a new node and fill the fields.
1777 */
1778 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1779 if (cur == NULL) {
1780 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00001781 "xmlNewCharRef : malloc failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001782 return(NULL);
1783 }
1784 memset(cur, 0, sizeof(xmlNode));
1785 cur->type = XML_ENTITY_REF_NODE;
1786
1787 cur->doc = doc;
1788 if (name[0] == '&') {
1789 int len;
1790 name++;
1791 len = xmlStrlen(name);
1792 if (name[len - 1] == ';')
1793 cur->name = xmlStrndup(name, len - 1);
1794 else
1795 cur->name = xmlStrndup(name, len);
1796 } else
1797 cur->name = xmlStrdup(name);
1798 return(cur);
1799}
1800
1801/**
1802 * xmlNewReference:
1803 * @doc: the document
1804 * @name: the reference name, or the reference string with & and ;
1805 *
1806 * Creation of a new reference node.
1807 * Returns a pointer to the new node object.
1808 */
1809xmlNodePtr
1810xmlNewReference(xmlDocPtr doc, const xmlChar *name) {
1811 xmlNodePtr cur;
1812 xmlEntityPtr ent;
1813
1814 /*
1815 * Allocate a new node and fill the fields.
1816 */
1817 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1818 if (cur == NULL) {
1819 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00001820 "xmlNewReference : malloc failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001821 return(NULL);
1822 }
1823 memset(cur, 0, sizeof(xmlNode));
1824 cur->type = XML_ENTITY_REF_NODE;
1825
1826 cur->doc = doc;
1827 if (name[0] == '&') {
1828 int len;
1829 name++;
1830 len = xmlStrlen(name);
1831 if (name[len - 1] == ';')
1832 cur->name = xmlStrndup(name, len - 1);
1833 else
1834 cur->name = xmlStrndup(name, len);
1835 } else
1836 cur->name = xmlStrdup(name);
1837
1838 ent = xmlGetDocEntity(doc, cur->name);
1839 if (ent != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001840 cur->content = ent->content;
Owen Taylor3473f882001-02-23 17:55:21 +00001841 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001842 * The parent pointer in entity is a DTD pointer and thus is NOT
Owen Taylor3473f882001-02-23 17:55:21 +00001843 * updated. Not sure if this is 100% correct.
1844 * -George
1845 */
1846 cur->children = (xmlNodePtr) ent;
1847 cur->last = (xmlNodePtr) ent;
1848 }
1849 return(cur);
1850}
1851
1852/**
1853 * xmlNewDocText:
1854 * @doc: the document
1855 * @content: the text content
1856 *
1857 * Creation of a new text node within a document.
1858 * Returns a pointer to the new node object.
1859 */
1860xmlNodePtr
1861xmlNewDocText(xmlDocPtr doc, const xmlChar *content) {
1862 xmlNodePtr cur;
1863
1864 cur = xmlNewText(content);
1865 if (cur != NULL) cur->doc = doc;
1866 return(cur);
1867}
1868
1869/**
1870 * xmlNewTextLen:
1871 * @content: the text content
1872 * @len: the text len.
1873 *
Daniel Veillard60087f32001-10-10 09:45:09 +00001874 * Creation of a new text node with an extra parameter for the content's length
Owen Taylor3473f882001-02-23 17:55:21 +00001875 * Returns a pointer to the new node object.
1876 */
1877xmlNodePtr
1878xmlNewTextLen(const xmlChar *content, int len) {
1879 xmlNodePtr cur;
1880
1881 /*
1882 * Allocate a new node and fill the fields.
1883 */
1884 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1885 if (cur == NULL) {
1886 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00001887 "xmlNewTextLen : malloc failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001888 return(NULL);
1889 }
1890 memset(cur, 0, sizeof(xmlNode));
1891 cur->type = XML_TEXT_NODE;
1892
1893 cur->name = xmlStringText;
1894 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001895 cur->content = xmlStrndup(content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00001896 }
1897 return(cur);
1898}
1899
1900/**
1901 * xmlNewDocTextLen:
1902 * @doc: the document
1903 * @content: the text content
1904 * @len: the text len.
1905 *
Daniel Veillard60087f32001-10-10 09:45:09 +00001906 * Creation of a new text node with an extra content length parameter. The
Owen Taylor3473f882001-02-23 17:55:21 +00001907 * text node pertain to a given document.
1908 * Returns a pointer to the new node object.
1909 */
1910xmlNodePtr
1911xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) {
1912 xmlNodePtr cur;
1913
1914 cur = xmlNewTextLen(content, len);
1915 if (cur != NULL) cur->doc = doc;
1916 return(cur);
1917}
1918
1919/**
1920 * xmlNewComment:
1921 * @content: the comment content
1922 *
1923 * Creation of a new node containing a comment.
1924 * Returns a pointer to the new node object.
1925 */
1926xmlNodePtr
1927xmlNewComment(const xmlChar *content) {
1928 xmlNodePtr cur;
1929
1930 /*
1931 * Allocate a new node and fill the fields.
1932 */
1933 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1934 if (cur == NULL) {
1935 xmlGenericError(xmlGenericErrorContext,
1936 "xmlNewComment : malloc failed\n");
1937 return(NULL);
1938 }
1939 memset(cur, 0, sizeof(xmlNode));
1940 cur->type = XML_COMMENT_NODE;
1941
1942 cur->name = xmlStringComment;
1943 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001944 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00001945 }
1946 return(cur);
1947}
1948
1949/**
1950 * xmlNewCDataBlock:
1951 * @doc: the document
Daniel Veillardd1640922001-12-17 15:30:10 +00001952 * @content: the CDATA block content content
Owen Taylor3473f882001-02-23 17:55:21 +00001953 * @len: the length of the block
1954 *
Daniel Veillardd1640922001-12-17 15:30:10 +00001955 * Creation of a new node containing a CDATA block.
Owen Taylor3473f882001-02-23 17:55:21 +00001956 * Returns a pointer to the new node object.
1957 */
1958xmlNodePtr
1959xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) {
1960 xmlNodePtr cur;
1961
1962 /*
1963 * Allocate a new node and fill the fields.
1964 */
1965 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1966 if (cur == NULL) {
1967 xmlGenericError(xmlGenericErrorContext,
1968 "xmlNewCDataBlock : malloc failed\n");
1969 return(NULL);
1970 }
1971 memset(cur, 0, sizeof(xmlNode));
1972 cur->type = XML_CDATA_SECTION_NODE;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001973 cur->doc = doc;
Owen Taylor3473f882001-02-23 17:55:21 +00001974
1975 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001976 cur->content = xmlStrndup(content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00001977 }
1978 return(cur);
1979}
1980
1981/**
1982 * xmlNewDocComment:
1983 * @doc: the document
1984 * @content: the comment content
1985 *
Daniel Veillardd1640922001-12-17 15:30:10 +00001986 * Creation of a new node containing a comment within a document.
Owen Taylor3473f882001-02-23 17:55:21 +00001987 * Returns a pointer to the new node object.
1988 */
1989xmlNodePtr
1990xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) {
1991 xmlNodePtr cur;
1992
1993 cur = xmlNewComment(content);
1994 if (cur != NULL) cur->doc = doc;
1995 return(cur);
1996}
1997
1998/**
1999 * xmlSetTreeDoc:
2000 * @tree: the top element
2001 * @doc: the document
2002 *
2003 * update all nodes under the tree to point to the right document
2004 */
2005void
2006xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) {
Daniel Veillard19e96c32001-07-09 10:32:59 +00002007 xmlAttrPtr prop;
2008
Owen Taylor3473f882001-02-23 17:55:21 +00002009 if (tree == NULL)
2010 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002011 if (tree->doc != doc) {
Daniel Veillard36065812002-01-24 15:02:46 +00002012 if(tree->type == XML_ELEMENT_NODE) {
2013 prop = tree->properties;
2014 while (prop != NULL) {
2015 prop->doc = doc;
2016 xmlSetListDoc(prop->children, doc);
2017 prop = prop->next;
2018 }
Daniel Veillard19e96c32001-07-09 10:32:59 +00002019 }
Owen Taylor3473f882001-02-23 17:55:21 +00002020 if (tree->children != NULL)
2021 xmlSetListDoc(tree->children, doc);
2022 tree->doc = doc;
2023 }
2024}
2025
2026/**
2027 * xmlSetListDoc:
Daniel Veillardd1640922001-12-17 15:30:10 +00002028 * @list: the first element
Owen Taylor3473f882001-02-23 17:55:21 +00002029 * @doc: the document
2030 *
2031 * update all nodes in the list to point to the right document
2032 */
2033void
2034xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) {
2035 xmlNodePtr cur;
2036
2037 if (list == NULL)
2038 return;
2039 cur = list;
2040 while (cur != NULL) {
2041 if (cur->doc != doc)
2042 xmlSetTreeDoc(cur, doc);
2043 cur = cur->next;
2044 }
2045}
2046
2047
2048/**
2049 * xmlNewChild:
2050 * @parent: the parent node
2051 * @ns: a namespace if any
2052 * @name: the name of the child
2053 * @content: the XML content of the child if any.
2054 *
2055 * Creation of a new child element, added at the end of @parent children list.
Daniel Veillardd1640922001-12-17 15:30:10 +00002056 * @ns and @content parameters are optional (NULL). If content is non NULL,
Owen Taylor3473f882001-02-23 17:55:21 +00002057 * a child list containing the TEXTs and ENTITY_REFs node will be created.
2058 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2059 * references, but XML special chars need to be escaped first by using
2060 * xmlEncodeEntitiesReentrant(). Use xmlNewTextChild() if entities
2061 * support is not needed.
2062 *
2063 * Returns a pointer to the new node object.
2064 */
2065xmlNodePtr
2066xmlNewChild(xmlNodePtr parent, xmlNsPtr ns,
2067 const xmlChar *name, const xmlChar *content) {
2068 xmlNodePtr cur, prev;
2069
2070 if (parent == NULL) {
2071#ifdef DEBUG_TREE
2072 xmlGenericError(xmlGenericErrorContext,
2073 "xmlNewChild : parent == NULL\n");
2074#endif
2075 return(NULL);
2076 }
2077
2078 if (name == NULL) {
2079#ifdef DEBUG_TREE
2080 xmlGenericError(xmlGenericErrorContext,
2081 "xmlNewChild : name == NULL\n");
2082#endif
2083 return(NULL);
2084 }
2085
2086 /*
2087 * Allocate a new node
2088 */
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002089 if (parent->type == XML_ELEMENT_NODE) {
2090 if (ns == NULL)
2091 cur = xmlNewDocNode(parent->doc, parent->ns, name, content);
2092 else
2093 cur = xmlNewDocNode(parent->doc, ns, name, content);
2094 } else if ((parent->type == XML_DOCUMENT_NODE) ||
2095 (parent->type == XML_HTML_DOCUMENT_NODE)) {
2096 if (ns == NULL)
2097 cur = xmlNewDocNode((xmlDocPtr) parent, NULL, name, content);
2098 else
2099 cur = xmlNewDocNode((xmlDocPtr) parent, ns, name, content);
Daniel Veillard7e3f1402002-10-28 18:52:57 +00002100 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2101 cur = xmlNewDocNode( parent->doc, ns, name, content);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002102 } else {
2103 return(NULL);
2104 }
Owen Taylor3473f882001-02-23 17:55:21 +00002105 if (cur == NULL) return(NULL);
2106
2107 /*
2108 * add the new element at the end of the children list.
2109 */
2110 cur->type = XML_ELEMENT_NODE;
2111 cur->parent = parent;
2112 cur->doc = parent->doc;
2113 if (parent->children == NULL) {
2114 parent->children = cur;
2115 parent->last = cur;
2116 } else {
2117 prev = parent->last;
2118 prev->next = cur;
2119 cur->prev = prev;
2120 parent->last = cur;
2121 }
2122
2123 return(cur);
2124}
2125
2126/**
2127 * xmlAddNextSibling:
2128 * @cur: the child node
2129 * @elem: the new node
2130 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002131 * Add a new node @elem as the next sibling of @cur
2132 * If the new node was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00002133 * first unlinked from its existing context.
2134 * As a result of text merging @elem may be freed.
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002135 * If the new node is ATTRIBUTE, it is added into properties instead of children.
2136 * If there is an attribute with equal name, it is first destroyed.
Owen Taylor3473f882001-02-23 17:55:21 +00002137 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002138 * Returns the new node or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002139 */
2140xmlNodePtr
2141xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) {
2142 if (cur == NULL) {
2143#ifdef DEBUG_TREE
2144 xmlGenericError(xmlGenericErrorContext,
2145 "xmlAddNextSibling : cur == NULL\n");
2146#endif
2147 return(NULL);
2148 }
2149 if (elem == NULL) {
2150#ifdef DEBUG_TREE
2151 xmlGenericError(xmlGenericErrorContext,
2152 "xmlAddNextSibling : elem == NULL\n");
2153#endif
2154 return(NULL);
2155 }
2156
2157 xmlUnlinkNode(elem);
2158
2159 if (elem->type == XML_TEXT_NODE) {
2160 if (cur->type == XML_TEXT_NODE) {
Owen Taylor3473f882001-02-23 17:55:21 +00002161 xmlNodeAddContent(cur, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002162 xmlFreeNode(elem);
2163 return(cur);
2164 }
Daniel Veillard9e1c72d2001-08-31 20:03:19 +00002165 if ((cur->next != NULL) && (cur->next->type == XML_TEXT_NODE) &&
2166 (cur->name == cur->next->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002167 xmlChar *tmp;
2168
2169 tmp = xmlStrdup(elem->content);
2170 tmp = xmlStrcat(tmp, cur->next->content);
2171 xmlNodeSetContent(cur->next, tmp);
2172 xmlFree(tmp);
Owen Taylor3473f882001-02-23 17:55:21 +00002173 xmlFreeNode(elem);
2174 return(cur->next);
2175 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002176 } else if (elem->type == XML_ATTRIBUTE_NODE) {
2177 /* check if an attribute with the same name exists */
2178 xmlAttrPtr attr;
2179
2180 if (elem->ns == NULL)
2181 attr = xmlHasProp(cur->parent, elem->name);
2182 else
2183 attr = xmlHasNsProp(cur->parent, elem->name, elem->ns->href);
2184 if ((attr != NULL) && (attr != (xmlAttrPtr) elem)) {
2185 /* different instance, destroy it (attributes must be unique) */
2186 xmlFreeProp(attr);
2187 }
Owen Taylor3473f882001-02-23 17:55:21 +00002188 }
2189
2190 if (elem->doc != cur->doc) {
2191 xmlSetTreeDoc(elem, cur->doc);
2192 }
2193 elem->parent = cur->parent;
2194 elem->prev = cur;
2195 elem->next = cur->next;
2196 cur->next = elem;
2197 if (elem->next != NULL)
2198 elem->next->prev = elem;
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002199 if ((elem->parent != NULL) && (elem->parent->last == cur) && (elem->type != XML_ATTRIBUTE_NODE))
Owen Taylor3473f882001-02-23 17:55:21 +00002200 elem->parent->last = elem;
2201 return(elem);
2202}
2203
2204/**
2205 * xmlAddPrevSibling:
2206 * @cur: the child node
2207 * @elem: the new node
2208 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002209 * Add a new node @elem as the previous sibling of @cur
Owen Taylor3473f882001-02-23 17:55:21 +00002210 * merging adjacent TEXT nodes (@elem may be freed)
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002211 * If the new node was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00002212 * first unlinked from its existing context.
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002213 * If the new node is ATTRIBUTE, it is added into properties instead of children.
2214 * If there is an attribute with equal name, it is first destroyed.
Owen Taylor3473f882001-02-23 17:55:21 +00002215 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002216 * Returns the new node or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002217 */
2218xmlNodePtr
2219xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) {
2220 if (cur == NULL) {
2221#ifdef DEBUG_TREE
2222 xmlGenericError(xmlGenericErrorContext,
2223 "xmlAddPrevSibling : cur == NULL\n");
2224#endif
2225 return(NULL);
2226 }
2227 if (elem == NULL) {
2228#ifdef DEBUG_TREE
2229 xmlGenericError(xmlGenericErrorContext,
2230 "xmlAddPrevSibling : elem == NULL\n");
2231#endif
2232 return(NULL);
2233 }
2234
2235 xmlUnlinkNode(elem);
2236
2237 if (elem->type == XML_TEXT_NODE) {
2238 if (cur->type == XML_TEXT_NODE) {
Owen Taylor3473f882001-02-23 17:55:21 +00002239 xmlChar *tmp;
2240
2241 tmp = xmlStrdup(elem->content);
2242 tmp = xmlStrcat(tmp, cur->content);
2243 xmlNodeSetContent(cur, tmp);
2244 xmlFree(tmp);
Owen Taylor3473f882001-02-23 17:55:21 +00002245 xmlFreeNode(elem);
2246 return(cur);
2247 }
Daniel Veillard9e1c72d2001-08-31 20:03:19 +00002248 if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE) &&
2249 (cur->name == cur->prev->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002250 xmlNodeAddContent(cur->prev, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002251 xmlFreeNode(elem);
2252 return(cur->prev);
2253 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002254 } else if (elem->type == XML_ATTRIBUTE_NODE) {
2255 /* check if an attribute with the same name exists */
2256 xmlAttrPtr attr;
2257
2258 if (elem->ns == NULL)
2259 attr = xmlHasProp(cur->parent, elem->name);
2260 else
2261 attr = xmlHasNsProp(cur->parent, elem->name, elem->ns->href);
2262 if ((attr != NULL) && (attr != (xmlAttrPtr) elem)) {
2263 /* different instance, destroy it (attributes must be unique) */
2264 xmlFreeProp(attr);
2265 }
Owen Taylor3473f882001-02-23 17:55:21 +00002266 }
2267
2268 if (elem->doc != cur->doc) {
2269 xmlSetTreeDoc(elem, cur->doc);
2270 }
2271 elem->parent = cur->parent;
2272 elem->next = cur;
2273 elem->prev = cur->prev;
2274 cur->prev = elem;
2275 if (elem->prev != NULL)
2276 elem->prev->next = elem;
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002277 if (elem->parent != NULL) {
2278 if (elem->type == XML_ATTRIBUTE_NODE) {
2279 if (elem->parent->properties == (xmlAttrPtr) cur) {
2280 elem->parent->properties = (xmlAttrPtr) elem;
2281 }
2282 } else {
2283 if (elem->parent->children == cur) {
2284 elem->parent->children = elem;
2285 }
2286 }
2287 }
Owen Taylor3473f882001-02-23 17:55:21 +00002288 return(elem);
2289}
2290
2291/**
2292 * xmlAddSibling:
2293 * @cur: the child node
2294 * @elem: the new node
2295 *
2296 * Add a new element @elem to the list of siblings of @cur
2297 * merging adjacent TEXT nodes (@elem may be freed)
2298 * If the new element was already inserted in a document it is
2299 * first unlinked from its existing context.
2300 *
2301 * Returns the new element or NULL in case of error.
2302 */
2303xmlNodePtr
2304xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) {
2305 xmlNodePtr parent;
2306
2307 if (cur == NULL) {
2308#ifdef DEBUG_TREE
2309 xmlGenericError(xmlGenericErrorContext,
2310 "xmlAddSibling : cur == NULL\n");
2311#endif
2312 return(NULL);
2313 }
2314
2315 if (elem == NULL) {
2316#ifdef DEBUG_TREE
2317 xmlGenericError(xmlGenericErrorContext,
2318 "xmlAddSibling : elem == NULL\n");
2319#endif
2320 return(NULL);
2321 }
2322
2323 /*
2324 * Constant time is we can rely on the ->parent->last to find
2325 * the last sibling.
2326 */
2327 if ((cur->parent != NULL) &&
2328 (cur->parent->children != NULL) &&
2329 (cur->parent->last != NULL) &&
2330 (cur->parent->last->next == NULL)) {
2331 cur = cur->parent->last;
2332 } else {
2333 while (cur->next != NULL) cur = cur->next;
2334 }
2335
2336 xmlUnlinkNode(elem);
2337
2338 if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002339 xmlNodeAddContent(cur, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002340 xmlFreeNode(elem);
2341 return(cur);
2342 }
2343
2344 if (elem->doc != cur->doc) {
2345 xmlSetTreeDoc(elem, cur->doc);
2346 }
2347 parent = cur->parent;
2348 elem->prev = cur;
2349 elem->next = NULL;
2350 elem->parent = parent;
2351 cur->next = elem;
2352 if (parent != NULL)
2353 parent->last = elem;
2354
2355 return(elem);
2356}
2357
2358/**
2359 * xmlAddChildList:
2360 * @parent: the parent node
2361 * @cur: the first node in the list
2362 *
2363 * Add a list of node at the end of the child list of the parent
2364 * merging adjacent TEXT nodes (@cur may be freed)
2365 *
2366 * Returns the last child or NULL in case of error.
2367 */
2368xmlNodePtr
2369xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) {
2370 xmlNodePtr prev;
2371
2372 if (parent == NULL) {
2373#ifdef DEBUG_TREE
2374 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00002375 "xmlAddChildList : parent == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002376#endif
2377 return(NULL);
2378 }
2379
2380 if (cur == NULL) {
2381#ifdef DEBUG_TREE
2382 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00002383 "xmlAddChildList : child == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002384#endif
2385 return(NULL);
2386 }
2387
2388 if ((cur->doc != NULL) && (parent->doc != NULL) &&
2389 (cur->doc != parent->doc)) {
2390#ifdef DEBUG_TREE
2391 xmlGenericError(xmlGenericErrorContext,
2392 "Elements moved to a different document\n");
2393#endif
2394 }
2395
2396 /*
2397 * add the first element at the end of the children list.
2398 */
2399 if (parent->children == NULL) {
2400 parent->children = cur;
2401 } else {
2402 /*
2403 * If cur and parent->last both are TEXT nodes, then merge them.
2404 */
2405 if ((cur->type == XML_TEXT_NODE) &&
2406 (parent->last->type == XML_TEXT_NODE) &&
2407 (cur->name == parent->last->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002408 xmlNodeAddContent(parent->last, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002409 /*
2410 * if it's the only child, nothing more to be done.
2411 */
2412 if (cur->next == NULL) {
2413 xmlFreeNode(cur);
2414 return(parent->last);
2415 }
2416 prev = cur;
2417 cur = cur->next;
2418 xmlFreeNode(prev);
2419 }
2420 prev = parent->last;
2421 prev->next = cur;
2422 cur->prev = prev;
2423 }
2424 while (cur->next != NULL) {
2425 cur->parent = parent;
2426 if (cur->doc != parent->doc) {
2427 xmlSetTreeDoc(cur, parent->doc);
2428 }
2429 cur = cur->next;
2430 }
2431 cur->parent = parent;
2432 cur->doc = parent->doc; /* the parent may not be linked to a doc ! */
2433 parent->last = cur;
2434
2435 return(cur);
2436}
2437
2438/**
2439 * xmlAddChild:
2440 * @parent: the parent node
2441 * @cur: the child node
2442 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002443 * Add a new node to @parent, at the end of the child (or property) list
Owen Taylor3473f882001-02-23 17:55:21 +00002444 * merging adjacent TEXT nodes (in which case @cur is freed)
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002445 * If the new node was already inserted in a document it is
2446 * first unlinked from its existing context.
2447 * If the new node is ATTRIBUTE, it is added into properties instead of children.
2448 * If there is an attribute with equal name, it is first destroyed.
2449 *
Owen Taylor3473f882001-02-23 17:55:21 +00002450 * Returns the child or NULL in case of error.
2451 */
2452xmlNodePtr
2453xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) {
2454 xmlNodePtr prev;
2455
2456 if (parent == NULL) {
2457#ifdef DEBUG_TREE
2458 xmlGenericError(xmlGenericErrorContext,
2459 "xmlAddChild : parent == NULL\n");
2460#endif
2461 return(NULL);
2462 }
2463
2464 if (cur == NULL) {
2465#ifdef DEBUG_TREE
2466 xmlGenericError(xmlGenericErrorContext,
2467 "xmlAddChild : child == NULL\n");
2468#endif
2469 return(NULL);
2470 }
2471
Owen Taylor3473f882001-02-23 17:55:21 +00002472 /*
2473 * If cur is a TEXT node, merge its content with adjacent TEXT nodes
Owen Taylor3473f882001-02-23 17:55:21 +00002474 * cur is then freed.
2475 */
2476 if (cur->type == XML_TEXT_NODE) {
Daniel Veillard7db37732001-07-12 01:20:08 +00002477 if ((parent->type == XML_TEXT_NODE) &&
Owen Taylor3473f882001-02-23 17:55:21 +00002478 (parent->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002479 xmlNodeAddContent(parent, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002480 xmlFreeNode(cur);
2481 return(parent);
2482 }
2483 if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) &&
2484 (parent->last->name == cur->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002485 xmlNodeAddContent(parent->last, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002486 xmlFreeNode(cur);
2487 return(parent->last);
2488 }
2489 }
2490
2491 /*
2492 * add the new element at the end of the children list.
2493 */
2494 cur->parent = parent;
2495 if (cur->doc != parent->doc) {
2496 xmlSetTreeDoc(cur, parent->doc);
2497 }
2498
2499 /*
Daniel Veillard7db37732001-07-12 01:20:08 +00002500 * Coalescing
Owen Taylor3473f882001-02-23 17:55:21 +00002501 */
Daniel Veillard7db37732001-07-12 01:20:08 +00002502 if ((parent->type == XML_TEXT_NODE) &&
Owen Taylor3473f882001-02-23 17:55:21 +00002503 (parent->content != NULL)) {
Daniel Veillard7db37732001-07-12 01:20:08 +00002504 xmlNodeAddContent(parent, cur->content);
Daniel Veillard7db37732001-07-12 01:20:08 +00002505 xmlFreeNode(cur);
2506 return(parent);
Owen Taylor3473f882001-02-23 17:55:21 +00002507 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002508 if (cur->type == XML_ATTRIBUTE_NODE) {
2509 if (parent->properties == NULL) {
2510 parent->properties = (xmlAttrPtr) cur;
2511 } else {
2512 /* check if an attribute with the same name exists */
2513 xmlAttrPtr lastattr;
Owen Taylor3473f882001-02-23 17:55:21 +00002514
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002515 if (cur->ns == NULL)
2516 lastattr = xmlHasProp(parent, cur->name);
2517 else
2518 lastattr = xmlHasNsProp(parent, cur->name, cur->ns->href);
2519 if ((lastattr != NULL) && (lastattr != (xmlAttrPtr) cur)) {
2520 /* different instance, destroy it (attributes must be unique) */
2521 xmlFreeProp(lastattr);
2522 }
2523 /* find the end */
2524 lastattr = parent->properties;
2525 while (lastattr->next != NULL) {
2526 lastattr = lastattr->next;
2527 }
2528 lastattr->next = (xmlAttrPtr) cur;
2529 ((xmlAttrPtr) cur)->prev = lastattr;
2530 }
2531 } else {
2532 if (parent->children == NULL) {
2533 parent->children = cur;
2534 parent->last = cur;
2535 } else {
2536 prev = parent->last;
2537 prev->next = cur;
2538 cur->prev = prev;
2539 parent->last = cur;
2540 }
2541 }
Owen Taylor3473f882001-02-23 17:55:21 +00002542 return(cur);
2543}
2544
2545/**
2546 * xmlGetLastChild:
2547 * @parent: the parent node
2548 *
2549 * Search the last child of a node.
2550 * Returns the last child or NULL if none.
2551 */
2552xmlNodePtr
2553xmlGetLastChild(xmlNodePtr parent) {
2554 if (parent == NULL) {
2555#ifdef DEBUG_TREE
2556 xmlGenericError(xmlGenericErrorContext,
2557 "xmlGetLastChild : parent == NULL\n");
2558#endif
2559 return(NULL);
2560 }
2561 return(parent->last);
2562}
2563
2564/**
2565 * xmlFreeNodeList:
2566 * @cur: the first node in the list
2567 *
2568 * Free a node and all its siblings, this is a recursive behaviour, all
2569 * the children are freed too.
2570 */
2571void
2572xmlFreeNodeList(xmlNodePtr cur) {
2573 xmlNodePtr next;
2574 if (cur == NULL) {
2575#ifdef DEBUG_TREE
2576 xmlGenericError(xmlGenericErrorContext,
2577 "xmlFreeNodeList : node == NULL\n");
2578#endif
2579 return;
2580 }
Daniel Veillarde6a55192002-01-14 17:11:53 +00002581 if (cur->type == XML_NAMESPACE_DECL) {
2582 xmlFreeNsList((xmlNsPtr) cur);
2583 return;
2584 }
Owen Taylor3473f882001-02-23 17:55:21 +00002585 while (cur != NULL) {
2586 next = cur->next;
Daniel Veillard02141ea2001-04-30 11:46:40 +00002587 /* unroll to speed up freeing the document */
2588 if (cur->type != XML_DTD_NODE) {
2589 if ((cur->children != NULL) &&
2590 (cur->type != XML_ENTITY_REF_NODE))
2591 xmlFreeNodeList(cur->children);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002592 if ((cur->type == XML_ELEMENT_NODE) &&
2593 (cur->properties != NULL))
Daniel Veillard02141ea2001-04-30 11:46:40 +00002594 xmlFreePropList(cur->properties);
Daniel Veillard7db37732001-07-12 01:20:08 +00002595 if ((cur->type != XML_ELEMENT_NODE) &&
2596 (cur->type != XML_XINCLUDE_START) &&
2597 (cur->type != XML_XINCLUDE_END) &&
2598 (cur->type != XML_ENTITY_REF_NODE)) {
Daniel Veillard02141ea2001-04-30 11:46:40 +00002599 if (cur->content != NULL) xmlFree(cur->content);
Daniel Veillard7db37732001-07-12 01:20:08 +00002600 }
2601 if (((cur->type == XML_ELEMENT_NODE) ||
2602 (cur->type == XML_XINCLUDE_START) ||
2603 (cur->type == XML_XINCLUDE_END)) &&
2604 (cur->nsDef != NULL))
2605 xmlFreeNsList(cur->nsDef);
2606
Daniel Veillard9cc6dc62001-06-11 08:09:20 +00002607 /*
2608 * When a node is a text node or a comment, it uses a global static
2609 * variable for the name of the node.
2610 *
2611 * The xmlStrEqual comparisons need to be done when (happened with
2612 * XML::libXML and XML::libXSLT) the library is included twice
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002613 * statically in the binary and a tree allocated by one occurrence
Daniel Veillardd1640922001-12-17 15:30:10 +00002614 * of the lib gets freed by the other occurrence, in this case
Daniel Veillard9cc6dc62001-06-11 08:09:20 +00002615 * the string addresses compare are not sufficient.
2616 */
Daniel Veillard02141ea2001-04-30 11:46:40 +00002617 if ((cur->name != NULL) &&
2618 (cur->name != xmlStringText) &&
2619 (cur->name != xmlStringTextNoenc) &&
Daniel Veillard9cc6dc62001-06-11 08:09:20 +00002620 (cur->name != xmlStringComment)) {
2621 if (cur->type == XML_TEXT_NODE) {
2622 if ((!xmlStrEqual(cur->name, xmlStringText)) &&
2623 (!xmlStrEqual(cur->name, xmlStringTextNoenc)))
2624 xmlFree((char *) cur->name);
2625 } else if (cur->type == XML_COMMENT_NODE) {
2626 if (!xmlStrEqual(cur->name, xmlStringComment))
2627 xmlFree((char *) cur->name);
2628 } else
2629 xmlFree((char *) cur->name);
2630 }
Daniel Veillard02141ea2001-04-30 11:46:40 +00002631 /* TODO : derecursivate this function */
Daniel Veillard02141ea2001-04-30 11:46:40 +00002632 xmlFree(cur);
2633 }
Owen Taylor3473f882001-02-23 17:55:21 +00002634 cur = next;
2635 }
2636}
2637
2638/**
2639 * xmlFreeNode:
2640 * @cur: the node
2641 *
2642 * Free a node, this is a recursive behaviour, all the children are freed too.
2643 * This doesn't unlink the child from the list, use xmlUnlinkNode() first.
2644 */
2645void
2646xmlFreeNode(xmlNodePtr cur) {
2647 if (cur == NULL) {
2648#ifdef DEBUG_TREE
2649 xmlGenericError(xmlGenericErrorContext,
2650 "xmlFreeNode : node == NULL\n");
2651#endif
2652 return;
2653 }
Daniel Veillard02141ea2001-04-30 11:46:40 +00002654 /* use xmlFreeDtd for DTD nodes */
Daniel Veillarde6a55192002-01-14 17:11:53 +00002655 if (cur->type == XML_DTD_NODE) {
2656 xmlFreeDtd((xmlDtdPtr) cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002657 return;
Daniel Veillarde6a55192002-01-14 17:11:53 +00002658 }
2659 if (cur->type == XML_NAMESPACE_DECL) {
2660 xmlFreeNs((xmlNsPtr) cur);
2661 return;
2662 }
Daniel Veillarda70d62f2002-11-07 14:18:03 +00002663 if (cur->type == XML_ATTRIBUTE_NODE) {
2664 xmlFreeProp((xmlAttrPtr) cur);
2665 return;
2666 }
Owen Taylor3473f882001-02-23 17:55:21 +00002667 if ((cur->children != NULL) &&
2668 (cur->type != XML_ENTITY_REF_NODE))
2669 xmlFreeNodeList(cur->children);
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002670 if ((cur->type == XML_ELEMENT_NODE) && (cur->properties != NULL))
Daniel Veillard02141ea2001-04-30 11:46:40 +00002671 xmlFreePropList(cur->properties);
Daniel Veillard7db37732001-07-12 01:20:08 +00002672 if ((cur->type != XML_ELEMENT_NODE) &&
2673 (cur->content != NULL) &&
2674 (cur->type != XML_ENTITY_REF_NODE) &&
2675 (cur->type != XML_XINCLUDE_END) &&
2676 (cur->type != XML_XINCLUDE_START)) {
Daniel Veillard7db37732001-07-12 01:20:08 +00002677 xmlFree(cur->content);
Daniel Veillard7db37732001-07-12 01:20:08 +00002678 }
2679
Daniel Veillardacd370f2001-06-09 17:17:51 +00002680 /*
2681 * When a node is a text node or a comment, it uses a global static
2682 * variable for the name of the node.
2683 *
2684 * The xmlStrEqual comparisons need to be done when (happened with
2685 * XML::libXML and XML::libXSLT) the library is included twice statically
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002686 * in the binary and a tree allocated by one occurence of the lib gets
Daniel Veillardd1640922001-12-17 15:30:10 +00002687 * freed by the other occurrence, in this case the string addresses compare
Daniel Veillardacd370f2001-06-09 17:17:51 +00002688 * are not sufficient.
2689 */
Owen Taylor3473f882001-02-23 17:55:21 +00002690 if ((cur->name != NULL) &&
2691 (cur->name != xmlStringText) &&
2692 (cur->name != xmlStringTextNoenc) &&
Daniel Veillardacd370f2001-06-09 17:17:51 +00002693 (cur->name != xmlStringComment)) {
2694 if (cur->type == XML_TEXT_NODE) {
2695 if ((!xmlStrEqual(cur->name, xmlStringText)) &&
2696 (!xmlStrEqual(cur->name, xmlStringTextNoenc)))
2697 xmlFree((char *) cur->name);
2698 } else if (cur->type == XML_COMMENT_NODE) {
2699 if (!xmlStrEqual(cur->name, xmlStringComment))
2700 xmlFree((char *) cur->name);
2701 } else
2702 xmlFree((char *) cur->name);
2703 }
2704
Daniel Veillarde1ca5032002-12-09 14:13:43 +00002705 if (((cur->type == XML_ELEMENT_NODE) ||
2706 (cur->type == XML_XINCLUDE_START) ||
2707 (cur->type == XML_XINCLUDE_END)) &&
2708 (cur->nsDef != NULL))
2709 xmlFreeNsList(cur->nsDef);
Owen Taylor3473f882001-02-23 17:55:21 +00002710 xmlFree(cur);
2711}
2712
2713/**
2714 * xmlUnlinkNode:
2715 * @cur: the node
2716 *
2717 * Unlink a node from it's current context, the node is not freed
2718 */
2719void
2720xmlUnlinkNode(xmlNodePtr cur) {
2721 if (cur == NULL) {
2722#ifdef DEBUG_TREE
2723 xmlGenericError(xmlGenericErrorContext,
2724 "xmlUnlinkNode : node == NULL\n");
2725#endif
2726 return;
2727 }
Daniel Veillard29e43992001-12-13 22:21:58 +00002728 if (cur->type == XML_DTD_NODE) {
2729 xmlDocPtr doc;
2730 doc = cur->doc;
2731 if (doc->intSubset == (xmlDtdPtr) cur)
2732 doc->intSubset = NULL;
2733 if (doc->extSubset == (xmlDtdPtr) cur)
2734 doc->extSubset = NULL;
2735 }
Daniel Veillardc169f8b2002-01-22 21:40:13 +00002736 if (cur->parent != NULL) {
2737 xmlNodePtr parent;
2738 parent = cur->parent;
2739 if (cur->type == XML_ATTRIBUTE_NODE) {
2740 if (parent->properties == (xmlAttrPtr) cur)
2741 parent->properties = ((xmlAttrPtr) cur)->next;
2742 } else {
2743 if (parent->children == cur)
2744 parent->children = cur->next;
2745 if (parent->last == cur)
2746 parent->last = cur->prev;
2747 }
2748 cur->parent = NULL;
2749 }
Owen Taylor3473f882001-02-23 17:55:21 +00002750 if (cur->next != NULL)
2751 cur->next->prev = cur->prev;
2752 if (cur->prev != NULL)
2753 cur->prev->next = cur->next;
2754 cur->next = cur->prev = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002755}
2756
2757/**
2758 * xmlReplaceNode:
2759 * @old: the old node
2760 * @cur: the node
2761 *
2762 * Unlink the old node from it's current context, prune the new one
Daniel Veillardd1640922001-12-17 15:30:10 +00002763 * at the same place. If @cur was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00002764 * first unlinked from its existing context.
2765 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002766 * Returns the @old node
Owen Taylor3473f882001-02-23 17:55:21 +00002767 */
2768xmlNodePtr
2769xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) {
2770 if (old == NULL) {
2771#ifdef DEBUG_TREE
2772 xmlGenericError(xmlGenericErrorContext,
2773 "xmlReplaceNode : old == NULL\n");
2774#endif
2775 return(NULL);
2776 }
2777 if (cur == NULL) {
2778 xmlUnlinkNode(old);
2779 return(old);
2780 }
2781 if (cur == old) {
2782 return(old);
2783 }
Daniel Veillardc169f8b2002-01-22 21:40:13 +00002784 if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) {
2785#ifdef DEBUG_TREE
2786 xmlGenericError(xmlGenericErrorContext,
2787 "xmlReplaceNode : Trying to replace attribute node with other node type\n");
2788#endif
2789 return(old);
2790 }
2791 if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) {
2792#ifdef DEBUG_TREE
2793 xmlGenericError(xmlGenericErrorContext,
2794 "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n");
2795#endif
2796 return(old);
2797 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002798 if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) {
2799#ifdef DEBUG_TREE
2800 xmlGenericError(xmlGenericErrorContext,
2801 "xmlReplaceNode : Trying to replace attribute node with other node type\n");
2802#endif
2803 return(old);
2804 }
2805 if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) {
2806#ifdef DEBUG_TREE
2807 xmlGenericError(xmlGenericErrorContext,
2808 "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n");
2809#endif
2810 return(old);
2811 }
Owen Taylor3473f882001-02-23 17:55:21 +00002812 xmlUnlinkNode(cur);
2813 cur->doc = old->doc;
2814 cur->parent = old->parent;
2815 cur->next = old->next;
2816 if (cur->next != NULL)
2817 cur->next->prev = cur;
2818 cur->prev = old->prev;
2819 if (cur->prev != NULL)
2820 cur->prev->next = cur;
2821 if (cur->parent != NULL) {
Daniel Veillardc169f8b2002-01-22 21:40:13 +00002822 if (cur->type == XML_ATTRIBUTE_NODE) {
2823 if (cur->parent->properties == (xmlAttrPtr)old)
2824 cur->parent->properties = ((xmlAttrPtr) cur);
2825 } else {
2826 if (cur->parent->children == old)
2827 cur->parent->children = cur;
2828 if (cur->parent->last == old)
2829 cur->parent->last = cur;
2830 }
Owen Taylor3473f882001-02-23 17:55:21 +00002831 }
2832 old->next = old->prev = NULL;
2833 old->parent = NULL;
2834 return(old);
2835}
2836
2837/************************************************************************
2838 * *
2839 * Copy operations *
2840 * *
2841 ************************************************************************/
2842
2843/**
2844 * xmlCopyNamespace:
2845 * @cur: the namespace
2846 *
2847 * Do a copy of the namespace.
2848 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002849 * Returns: a new #xmlNsPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002850 */
2851xmlNsPtr
2852xmlCopyNamespace(xmlNsPtr cur) {
2853 xmlNsPtr ret;
2854
2855 if (cur == NULL) return(NULL);
2856 switch (cur->type) {
2857 case XML_LOCAL_NAMESPACE:
2858 ret = xmlNewNs(NULL, cur->href, cur->prefix);
2859 break;
2860 default:
2861#ifdef DEBUG_TREE
2862 xmlGenericError(xmlGenericErrorContext,
2863 "xmlCopyNamespace: invalid type %d\n", cur->type);
2864#endif
2865 return(NULL);
2866 }
2867 return(ret);
2868}
2869
2870/**
2871 * xmlCopyNamespaceList:
2872 * @cur: the first namespace
2873 *
2874 * Do a copy of an namespace list.
2875 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002876 * Returns: a new #xmlNsPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002877 */
2878xmlNsPtr
2879xmlCopyNamespaceList(xmlNsPtr cur) {
2880 xmlNsPtr ret = NULL;
2881 xmlNsPtr p = NULL,q;
2882
2883 while (cur != NULL) {
2884 q = xmlCopyNamespace(cur);
2885 if (p == NULL) {
2886 ret = p = q;
2887 } else {
2888 p->next = q;
2889 p = q;
2890 }
2891 cur = cur->next;
2892 }
2893 return(ret);
2894}
2895
2896static xmlNodePtr
2897xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent);
2898/**
2899 * xmlCopyProp:
2900 * @target: the element where the attribute will be grafted
2901 * @cur: the attribute
2902 *
2903 * Do a copy of the attribute.
2904 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002905 * Returns: a new #xmlAttrPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002906 */
2907xmlAttrPtr
2908xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) {
2909 xmlAttrPtr ret;
2910
2911 if (cur == NULL) return(NULL);
2912 if (target != NULL)
2913 ret = xmlNewDocProp(target->doc, cur->name, NULL);
2914 else if (cur->parent != NULL)
2915 ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL);
2916 else if (cur->children != NULL)
2917 ret = xmlNewDocProp(cur->children->doc, cur->name, NULL);
2918 else
2919 ret = xmlNewDocProp(NULL, cur->name, NULL);
2920 if (ret == NULL) return(NULL);
2921 ret->parent = target;
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00002922
Owen Taylor3473f882001-02-23 17:55:21 +00002923 if ((cur->ns != NULL) && (target != NULL)) {
Daniel Veillard8107a222002-01-13 14:10:10 +00002924 xmlNsPtr ns;
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00002925/*
2926 * if (target->doc)
2927 * ns = xmlSearchNs(target->doc, target, cur->ns->prefix);
2928 * else if (cur->doc) / * target may not yet have a doc : KPI * /
2929 * ns = xmlSearchNs(cur->doc, target, cur->ns->prefix);
2930 * else
2931 * ns = NULL;
2932 * ret->ns = ns;
2933 */
2934 ns = xmlSearchNs(target->doc, target, cur->ns->prefix);
2935 if (ns == NULL) {
2936 /*
2937 * Humm, we are copying an element whose namespace is defined
2938 * out of the new tree scope. Search it in the original tree
2939 * and add it at the top of the new tree
2940 */
2941 ns = xmlSearchNs(cur->doc, cur->parent, cur->ns->prefix);
2942 if (ns != NULL) {
2943 xmlNodePtr root = target;
2944 xmlNodePtr pred = NULL;
2945
2946 while (root->parent != NULL) {
2947 pred = root;
2948 root = root->parent;
2949 }
2950 if (root == (xmlNodePtr) target->doc) {
2951 /* correct possibly cycling above the document elt */
2952 root = pred;
2953 }
2954 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
2955 }
2956 } else {
2957 /*
2958 * we have to find something appropriate here since
2959 * we cant be sure, that the namespce we found is identified
2960 * by the prefix
2961 */
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002962 if (xmlStrEqual(ns->href, cur->ns->href)) {
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00002963 /* this is the nice case */
2964 ret->ns = ns;
2965 } else {
2966 /*
2967 * we are in trouble: we need a new reconcilied namespace.
2968 * This is expensive
2969 */
2970 ret->ns = xmlNewReconciliedNs(target->doc, target, cur->ns);
2971 }
2972 }
2973
Owen Taylor3473f882001-02-23 17:55:21 +00002974 } else
2975 ret->ns = NULL;
2976
2977 if (cur->children != NULL) {
2978 xmlNodePtr tmp;
2979
2980 ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret);
2981 ret->last = NULL;
2982 tmp = ret->children;
2983 while (tmp != NULL) {
2984 /* tmp->parent = (xmlNodePtr)ret; */
2985 if (tmp->next == NULL)
2986 ret->last = tmp;
2987 tmp = tmp->next;
2988 }
2989 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00002990 /*
2991 * Try to handle IDs
2992 */
Daniel Veillarda3db2e32002-03-08 15:46:57 +00002993 if ((target!= NULL) && (cur!= NULL) &&
2994 (target->doc != NULL) && (cur->doc != NULL) &&
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00002995 (cur->doc->ids != NULL) && (cur->parent != NULL)) {
2996 if (xmlIsID(cur->doc, cur->parent, cur)) {
2997 xmlChar *id;
2998
2999 id = xmlNodeListGetString(cur->doc, cur->children, 1);
3000 if (id != NULL) {
3001 xmlAddID(NULL, target->doc, id, ret);
3002 xmlFree(id);
3003 }
3004 }
3005 }
Owen Taylor3473f882001-02-23 17:55:21 +00003006 return(ret);
3007}
3008
3009/**
3010 * xmlCopyPropList:
3011 * @target: the element where the attributes will be grafted
3012 * @cur: the first attribute
3013 *
3014 * Do a copy of an attribute list.
3015 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003016 * Returns: a new #xmlAttrPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003017 */
3018xmlAttrPtr
3019xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) {
3020 xmlAttrPtr ret = NULL;
3021 xmlAttrPtr p = NULL,q;
3022
3023 while (cur != NULL) {
3024 q = xmlCopyProp(target, cur);
3025 if (p == NULL) {
3026 ret = p = q;
3027 } else {
3028 p->next = q;
3029 q->prev = p;
3030 p = q;
3031 }
3032 cur = cur->next;
3033 }
3034 return(ret);
3035}
3036
3037/*
Daniel Veillardd1640922001-12-17 15:30:10 +00003038 * NOTE about the CopyNode operations !
Owen Taylor3473f882001-02-23 17:55:21 +00003039 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003040 * They are split into external and internal parts for one
Owen Taylor3473f882001-02-23 17:55:21 +00003041 * tricky reason: namespaces. Doing a direct copy of a node
3042 * say RPM:Copyright without changing the namespace pointer to
3043 * something else can produce stale links. One way to do it is
3044 * to keep a reference counter but this doesn't work as soon
3045 * as one move the element or the subtree out of the scope of
3046 * the existing namespace. The actual solution seems to add
3047 * a copy of the namespace at the top of the copied tree if
3048 * not available in the subtree.
3049 * Hence two functions, the public front-end call the inner ones
3050 */
3051
3052static xmlNodePtr
Daniel Veillard3ec4c612001-08-28 20:39:49 +00003053xmlStaticCopyNode(const xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent,
Owen Taylor3473f882001-02-23 17:55:21 +00003054 int recursive) {
3055 xmlNodePtr ret;
3056
3057 if (node == NULL) return(NULL);
Daniel Veillard39196eb2001-06-19 18:09:42 +00003058 switch (node->type) {
3059 case XML_TEXT_NODE:
3060 case XML_CDATA_SECTION_NODE:
3061 case XML_ELEMENT_NODE:
Daniel Veillardec6725e2002-09-05 11:12:45 +00003062 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00003063 case XML_ENTITY_REF_NODE:
3064 case XML_ENTITY_NODE:
3065 case XML_PI_NODE:
3066 case XML_COMMENT_NODE:
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00003067 case XML_XINCLUDE_START:
3068 case XML_XINCLUDE_END:
3069 break;
3070 case XML_ATTRIBUTE_NODE:
3071 return((xmlNodePtr) xmlCopyProp(parent, (xmlAttrPtr) node));
3072 case XML_NAMESPACE_DECL:
3073 return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node));
3074
Daniel Veillard39196eb2001-06-19 18:09:42 +00003075 case XML_DOCUMENT_NODE:
3076 case XML_HTML_DOCUMENT_NODE:
3077#ifdef LIBXML_DOCB_ENABLED
3078 case XML_DOCB_DOCUMENT_NODE:
3079#endif
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00003080 return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, recursive));
Daniel Veillard39196eb2001-06-19 18:09:42 +00003081 case XML_DOCUMENT_TYPE_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00003082 case XML_NOTATION_NODE:
3083 case XML_DTD_NODE:
3084 case XML_ELEMENT_DECL:
3085 case XML_ATTRIBUTE_DECL:
3086 case XML_ENTITY_DECL:
3087 return(NULL);
3088 }
Daniel Veillardb33c2012001-04-25 12:59:04 +00003089
Owen Taylor3473f882001-02-23 17:55:21 +00003090 /*
3091 * Allocate a new node and fill the fields.
3092 */
3093 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
3094 if (ret == NULL) {
3095 xmlGenericError(xmlGenericErrorContext,
3096 "xmlStaticCopyNode : malloc failed\n");
3097 return(NULL);
3098 }
3099 memset(ret, 0, sizeof(xmlNode));
3100 ret->type = node->type;
3101
3102 ret->doc = doc;
3103 ret->parent = parent;
3104 if (node->name == xmlStringText)
3105 ret->name = xmlStringText;
3106 else if (node->name == xmlStringTextNoenc)
3107 ret->name = xmlStringTextNoenc;
3108 else if (node->name == xmlStringComment)
3109 ret->name = xmlStringComment;
3110 else if (node->name != NULL)
3111 ret->name = xmlStrdup(node->name);
Daniel Veillard7db37732001-07-12 01:20:08 +00003112 if ((node->type != XML_ELEMENT_NODE) &&
3113 (node->content != NULL) &&
3114 (node->type != XML_ENTITY_REF_NODE) &&
3115 (node->type != XML_XINCLUDE_END) &&
3116 (node->type != XML_XINCLUDE_START)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003117 ret->content = xmlStrdup(node->content);
Daniel Veillard8107a222002-01-13 14:10:10 +00003118 }else{
3119 if (node->type == XML_ELEMENT_NODE)
3120 ret->content = (void*)(long) node->content;
Owen Taylor3473f882001-02-23 17:55:21 +00003121 }
Daniel Veillardacb2bda2002-01-13 16:15:43 +00003122 if (parent != NULL) {
3123 xmlNodePtr tmp;
3124
3125 tmp = xmlAddChild(parent, ret);
3126 /* node could have coalesced */
3127 if (tmp != ret)
3128 return(tmp);
3129 }
Owen Taylor3473f882001-02-23 17:55:21 +00003130
3131 if (!recursive) return(ret);
3132 if (node->nsDef != NULL)
3133 ret->nsDef = xmlCopyNamespaceList(node->nsDef);
3134
3135 if (node->ns != NULL) {
3136 xmlNsPtr ns;
3137
3138 ns = xmlSearchNs(doc, ret, node->ns->prefix);
3139 if (ns == NULL) {
3140 /*
3141 * Humm, we are copying an element whose namespace is defined
3142 * out of the new tree scope. Search it in the original tree
3143 * and add it at the top of the new tree
3144 */
3145 ns = xmlSearchNs(node->doc, node, node->ns->prefix);
3146 if (ns != NULL) {
3147 xmlNodePtr root = ret;
3148
3149 while (root->parent != NULL) root = root->parent;
Daniel Veillarde82a9922001-04-22 12:12:58 +00003150 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00003151 }
3152 } else {
3153 /*
3154 * reference the existing namespace definition in our own tree.
3155 */
3156 ret->ns = ns;
3157 }
3158 }
3159 if (node->properties != NULL)
3160 ret->properties = xmlCopyPropList(ret, node->properties);
Daniel Veillardb33c2012001-04-25 12:59:04 +00003161 if (node->type == XML_ENTITY_REF_NODE) {
3162 if ((doc == NULL) || (node->doc != doc)) {
3163 /*
3164 * The copied node will go into a separate document, so
Daniel Veillardd1640922001-12-17 15:30:10 +00003165 * to avoid dangling references to the ENTITY_DECL node
Daniel Veillardb33c2012001-04-25 12:59:04 +00003166 * we cannot keep the reference. Try to find it in the
3167 * target document.
3168 */
3169 ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name);
3170 } else {
3171 ret->children = node->children;
3172 }
Daniel Veillard0ec98632001-11-14 15:04:32 +00003173 ret->last = ret->children;
3174 } else if (node->children != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00003175 ret->children = xmlStaticCopyNodeList(node->children, doc, ret);
Daniel Veillard0ec98632001-11-14 15:04:32 +00003176 UPDATE_LAST_CHILD_AND_PARENT(ret)
3177 }
Owen Taylor3473f882001-02-23 17:55:21 +00003178 return(ret);
3179}
3180
3181static xmlNodePtr
3182xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) {
3183 xmlNodePtr ret = NULL;
3184 xmlNodePtr p = NULL,q;
3185
3186 while (node != NULL) {
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00003187 if (node->type == XML_DTD_NODE ) {
Daniel Veillard4497e692001-06-09 14:19:02 +00003188 if (doc == NULL) {
3189 node = node->next;
3190 continue;
3191 }
Daniel Veillardb33c2012001-04-25 12:59:04 +00003192 if (doc->intSubset == NULL) {
3193 q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node );
3194 q->doc = doc;
3195 q->parent = parent;
3196 doc->intSubset = (xmlDtdPtr) q;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00003197 xmlAddChild(parent, q);
Daniel Veillardb33c2012001-04-25 12:59:04 +00003198 } else {
3199 q = (xmlNodePtr) doc->intSubset;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00003200 xmlAddChild(parent, q);
Daniel Veillardb33c2012001-04-25 12:59:04 +00003201 }
3202 } else
3203 q = xmlStaticCopyNode(node, doc, parent, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00003204 if (ret == NULL) {
3205 q->prev = NULL;
3206 ret = p = q;
Daniel Veillardacb2bda2002-01-13 16:15:43 +00003207 } else if (p != q) {
3208 /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */
Owen Taylor3473f882001-02-23 17:55:21 +00003209 p->next = q;
3210 q->prev = p;
3211 p = q;
3212 }
3213 node = node->next;
3214 }
3215 return(ret);
3216}
3217
3218/**
3219 * xmlCopyNode:
3220 * @node: the node
3221 * @recursive: if 1 do a recursive copy.
3222 *
3223 * Do a copy of the node.
3224 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003225 * Returns: a new #xmlNodePtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003226 */
3227xmlNodePtr
Daniel Veillard3ec4c612001-08-28 20:39:49 +00003228xmlCopyNode(const xmlNodePtr node, int recursive) {
Owen Taylor3473f882001-02-23 17:55:21 +00003229 xmlNodePtr ret;
3230
3231 ret = xmlStaticCopyNode(node, NULL, NULL, recursive);
3232 return(ret);
3233}
3234
3235/**
Daniel Veillard82daa812001-04-12 08:55:36 +00003236 * xmlDocCopyNode:
3237 * @node: the node
Daniel Veillardd1640922001-12-17 15:30:10 +00003238 * @doc: the document
Daniel Veillard82daa812001-04-12 08:55:36 +00003239 * @recursive: if 1 do a recursive copy.
3240 *
3241 * Do a copy of the node to a given document.
3242 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003243 * Returns: a new #xmlNodePtr, or NULL in case of error.
Daniel Veillard82daa812001-04-12 08:55:36 +00003244 */
3245xmlNodePtr
Daniel Veillard3ec4c612001-08-28 20:39:49 +00003246xmlDocCopyNode(const xmlNodePtr node, xmlDocPtr doc, int recursive) {
Daniel Veillard82daa812001-04-12 08:55:36 +00003247 xmlNodePtr ret;
3248
3249 ret = xmlStaticCopyNode(node, doc, NULL, recursive);
3250 return(ret);
3251}
3252
3253/**
Owen Taylor3473f882001-02-23 17:55:21 +00003254 * xmlCopyNodeList:
3255 * @node: the first node in the list.
3256 *
3257 * Do a recursive copy of the node list.
3258 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003259 * Returns: a new #xmlNodePtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003260 */
Daniel Veillard3ec4c612001-08-28 20:39:49 +00003261xmlNodePtr xmlCopyNodeList(const xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00003262 xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL);
3263 return(ret);
3264}
3265
3266/**
Owen Taylor3473f882001-02-23 17:55:21 +00003267 * xmlCopyDtd:
3268 * @dtd: the dtd
3269 *
3270 * Do a copy of the dtd.
3271 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003272 * Returns: a new #xmlDtdPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003273 */
3274xmlDtdPtr
3275xmlCopyDtd(xmlDtdPtr dtd) {
3276 xmlDtdPtr ret;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00003277 xmlNodePtr cur, p = NULL, q;
Owen Taylor3473f882001-02-23 17:55:21 +00003278
3279 if (dtd == NULL) return(NULL);
3280 ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID);
3281 if (ret == NULL) return(NULL);
3282 if (dtd->entities != NULL)
3283 ret->entities = (void *) xmlCopyEntitiesTable(
3284 (xmlEntitiesTablePtr) dtd->entities);
3285 if (dtd->notations != NULL)
3286 ret->notations = (void *) xmlCopyNotationTable(
3287 (xmlNotationTablePtr) dtd->notations);
3288 if (dtd->elements != NULL)
3289 ret->elements = (void *) xmlCopyElementTable(
3290 (xmlElementTablePtr) dtd->elements);
3291 if (dtd->attributes != NULL)
3292 ret->attributes = (void *) xmlCopyAttributeTable(
3293 (xmlAttributeTablePtr) dtd->attributes);
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00003294 if (dtd->pentities != NULL)
3295 ret->pentities = (void *) xmlCopyEntitiesTable(
3296 (xmlEntitiesTablePtr) dtd->pentities);
3297
3298 cur = dtd->children;
3299 while (cur != NULL) {
3300 q = NULL;
3301
3302 if (cur->type == XML_ENTITY_DECL) {
3303 xmlEntityPtr tmp = (xmlEntityPtr) cur;
3304 switch (tmp->etype) {
3305 case XML_INTERNAL_GENERAL_ENTITY:
3306 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
3307 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
3308 q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name);
3309 break;
3310 case XML_INTERNAL_PARAMETER_ENTITY:
3311 case XML_EXTERNAL_PARAMETER_ENTITY:
3312 q = (xmlNodePtr)
3313 xmlGetParameterEntityFromDtd(ret, tmp->name);
3314 break;
3315 case XML_INTERNAL_PREDEFINED_ENTITY:
3316 break;
3317 }
3318 } else if (cur->type == XML_ELEMENT_DECL) {
3319 xmlElementPtr tmp = (xmlElementPtr) cur;
3320 q = (xmlNodePtr)
3321 xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix);
3322 } else if (cur->type == XML_ATTRIBUTE_DECL) {
3323 xmlAttributePtr tmp = (xmlAttributePtr) cur;
3324 q = (xmlNodePtr)
3325 xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix);
3326 } else if (cur->type == XML_COMMENT_NODE) {
3327 q = xmlCopyNode(cur, 0);
3328 }
3329
3330 if (q == NULL) {
3331 cur = cur->next;
3332 continue;
3333 }
3334
3335 if (p == NULL)
3336 ret->children = q;
3337 else
3338 p->next = q;
3339
3340 q->prev = p;
3341 q->parent = (xmlNodePtr) ret;
3342 q->next = NULL;
3343 ret->last = q;
3344 p = q;
3345 cur = cur->next;
3346 }
3347
Owen Taylor3473f882001-02-23 17:55:21 +00003348 return(ret);
3349}
3350
3351/**
3352 * xmlCopyDoc:
3353 * @doc: the document
3354 * @recursive: if 1 do a recursive copy.
3355 *
3356 * Do a copy of the document info. If recursive, the content tree will
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003357 * be copied too as well as DTD, namespaces and entities.
Owen Taylor3473f882001-02-23 17:55:21 +00003358 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003359 * Returns: a new #xmlDocPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003360 */
3361xmlDocPtr
3362xmlCopyDoc(xmlDocPtr doc, int recursive) {
3363 xmlDocPtr ret;
3364
3365 if (doc == NULL) return(NULL);
3366 ret = xmlNewDoc(doc->version);
3367 if (ret == NULL) return(NULL);
3368 if (doc->name != NULL)
3369 ret->name = xmlMemStrdup(doc->name);
3370 if (doc->encoding != NULL)
3371 ret->encoding = xmlStrdup(doc->encoding);
3372 ret->charset = doc->charset;
3373 ret->compression = doc->compression;
3374 ret->standalone = doc->standalone;
3375 if (!recursive) return(ret);
3376
Daniel Veillardb33c2012001-04-25 12:59:04 +00003377 ret->last = NULL;
3378 ret->children = NULL;
3379 if (doc->intSubset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00003380 ret->intSubset = xmlCopyDtd(doc->intSubset);
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00003381 xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret);
Daniel Veillardb33c2012001-04-25 12:59:04 +00003382 ret->intSubset->parent = ret;
3383 }
Owen Taylor3473f882001-02-23 17:55:21 +00003384 if (doc->oldNs != NULL)
3385 ret->oldNs = xmlCopyNamespaceList(doc->oldNs);
3386 if (doc->children != NULL) {
3387 xmlNodePtr tmp;
Daniel Veillardb33c2012001-04-25 12:59:04 +00003388
3389 ret->children = xmlStaticCopyNodeList(doc->children, ret,
3390 (xmlNodePtr)ret);
Owen Taylor3473f882001-02-23 17:55:21 +00003391 ret->last = NULL;
3392 tmp = ret->children;
3393 while (tmp != NULL) {
3394 if (tmp->next == NULL)
3395 ret->last = tmp;
3396 tmp = tmp->next;
3397 }
3398 }
3399 return(ret);
3400}
3401
3402/************************************************************************
3403 * *
3404 * Content access functions *
3405 * *
3406 ************************************************************************/
3407
3408/**
Daniel Veillard8faa7832001-11-26 15:58:08 +00003409 * xmlGetLineNo:
3410 * @node : valid node
3411 *
3412 * Get line number of node. this requires activation of this option
Daniel Veillardd1640922001-12-17 15:30:10 +00003413 * before invoking the parser by calling xmlLineNumbersDefault(1)
Daniel Veillard8faa7832001-11-26 15:58:08 +00003414 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003415 * Returns the line number if successful, -1 otherwise
Daniel Veillard8faa7832001-11-26 15:58:08 +00003416 */
3417long
3418xmlGetLineNo(xmlNodePtr node)
3419{
3420 long result = -1;
3421
3422 if (!node)
3423 return result;
3424 if (node->type == XML_ELEMENT_NODE)
3425 result = (long) node->content;
3426 else if ((node->prev != NULL) &&
3427 ((node->prev->type == XML_ELEMENT_NODE) ||
3428 (node->prev->type == XML_TEXT_NODE)))
3429 result = xmlGetLineNo(node->prev);
3430 else if ((node->parent != NULL) &&
3431 ((node->parent->type == XML_ELEMENT_NODE) ||
3432 (node->parent->type == XML_TEXT_NODE)))
3433 result = xmlGetLineNo(node->parent);
3434
3435 return result;
3436}
3437
3438/**
3439 * xmlGetNodePath:
3440 * @node: a node
3441 *
3442 * Build a structure based Path for the given node
3443 *
3444 * Returns the new path or NULL in case of error. The caller must free
3445 * the returned string
3446 */
3447xmlChar *
3448xmlGetNodePath(xmlNodePtr node)
3449{
3450 xmlNodePtr cur, tmp, next;
3451 xmlChar *buffer = NULL, *temp;
3452 size_t buf_len;
3453 xmlChar *buf;
Daniel Veillard96c3a3b2002-10-14 15:39:04 +00003454 const char *sep;
Daniel Veillard8faa7832001-11-26 15:58:08 +00003455 const char *name;
3456 char nametemp[100];
3457 int occur = 0;
3458
3459 if (node == NULL)
3460 return (NULL);
3461
3462 buf_len = 500;
3463 buffer = (xmlChar *) xmlMalloc(buf_len * sizeof(xmlChar));
3464 if (buffer == NULL)
3465 return (NULL);
3466 buf = (xmlChar *) xmlMalloc(buf_len * sizeof(xmlChar));
3467 if (buf == NULL) {
3468 xmlFree(buffer);
3469 return (NULL);
3470 }
3471
3472 buffer[0] = 0;
3473 cur = node;
3474 do {
3475 name = "";
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00003476 sep = "?";
Daniel Veillard8faa7832001-11-26 15:58:08 +00003477 occur = 0;
3478 if ((cur->type == XML_DOCUMENT_NODE) ||
3479 (cur->type == XML_HTML_DOCUMENT_NODE)) {
3480 if (buffer[0] == '/')
3481 break;
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00003482 sep = "/";
Daniel Veillard8faa7832001-11-26 15:58:08 +00003483 next = NULL;
3484 } else if (cur->type == XML_ELEMENT_NODE) {
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00003485 sep = "/";
Daniel Veillard8faa7832001-11-26 15:58:08 +00003486 name = (const char *) cur->name;
3487 if (cur->ns) {
3488 snprintf(nametemp, sizeof(nametemp) - 1,
3489 "%s:%s", cur->ns->prefix, cur->name);
3490 nametemp[sizeof(nametemp) - 1] = 0;
3491 name = nametemp;
3492 }
3493 next = cur->parent;
3494
3495 /*
3496 * Thumbler index computation
3497 */
3498 tmp = cur->prev;
3499 while (tmp != NULL) {
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00003500 if ((tmp->type == XML_ELEMENT_NODE) &&
3501 (xmlStrEqual(cur->name, tmp->name)))
Daniel Veillard8faa7832001-11-26 15:58:08 +00003502 occur++;
3503 tmp = tmp->prev;
3504 }
3505 if (occur == 0) {
3506 tmp = cur->next;
Daniel Veillard8606bbb2002-11-12 12:36:52 +00003507 while (tmp != NULL && occur == 0) {
3508 if ((tmp->type == XML_ELEMENT_NODE) &&
3509 (xmlStrEqual(cur->name, tmp->name)))
Daniel Veillard8faa7832001-11-26 15:58:08 +00003510 occur++;
3511 tmp = tmp->next;
3512 }
3513 if (occur != 0)
3514 occur = 1;
3515 } else
3516 occur++;
Daniel Veillard8606bbb2002-11-12 12:36:52 +00003517 } else if (cur->type == XML_COMMENT_NODE) {
3518 sep = "/";
3519 name = "comment()";
3520 next = cur->parent;
3521
3522 /*
3523 * Thumbler index computation
3524 */
3525 tmp = cur->prev;
3526 while (tmp != NULL) {
3527 if (tmp->type == XML_COMMENT_NODE)
3528 occur++;
3529 tmp = tmp->prev;
3530 }
3531 if (occur == 0) {
3532 tmp = cur->next;
3533 while (tmp != NULL && occur == 0) {
3534 if (tmp->type == XML_COMMENT_NODE)
3535 occur++;
3536 tmp = tmp->next;
3537 }
3538 if (occur != 0)
3539 occur = 1;
3540 } else
3541 occur++;
3542 } else if ((cur->type == XML_TEXT_NODE) ||
3543 (cur->type == XML_CDATA_SECTION_NODE)) {
3544 sep = "/";
3545 name = "text()";
3546 next = cur->parent;
3547
3548 /*
3549 * Thumbler index computation
3550 */
3551 tmp = cur->prev;
3552 while (tmp != NULL) {
3553 if ((cur->type == XML_TEXT_NODE) ||
3554 (cur->type == XML_CDATA_SECTION_NODE))
3555 occur++;
3556 tmp = tmp->prev;
3557 }
3558 if (occur == 0) {
3559 tmp = cur->next;
3560 while (tmp != NULL && occur == 0) {
3561 if ((cur->type == XML_TEXT_NODE) ||
3562 (cur->type == XML_CDATA_SECTION_NODE))
3563 occur++;
3564 tmp = tmp->next;
3565 }
3566 if (occur != 0)
3567 occur = 1;
3568 } else
3569 occur++;
3570 } else if (cur->type == XML_PI_NODE) {
3571 sep = "/";
3572 snprintf(nametemp, sizeof(nametemp) - 1,
3573 "processing-instruction('%s')", cur->name);
3574 nametemp[sizeof(nametemp) - 1] = 0;
3575 name = nametemp;
3576
3577 next = cur->parent;
3578
3579 /*
3580 * Thumbler index computation
3581 */
3582 tmp = cur->prev;
3583 while (tmp != NULL) {
3584 if ((tmp->type == XML_PI_NODE) &&
3585 (xmlStrEqual(cur->name, tmp->name)))
3586 occur++;
3587 tmp = tmp->prev;
3588 }
3589 if (occur == 0) {
3590 tmp = cur->next;
3591 while (tmp != NULL && occur == 0) {
3592 if ((tmp->type == XML_PI_NODE) &&
3593 (xmlStrEqual(cur->name, tmp->name)))
3594 occur++;
3595 tmp = tmp->next;
3596 }
3597 if (occur != 0)
3598 occur = 1;
3599 } else
3600 occur++;
3601
Daniel Veillard8faa7832001-11-26 15:58:08 +00003602 } else if (cur->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00003603 sep = "/@";
Daniel Veillard8faa7832001-11-26 15:58:08 +00003604 name = (const char *) (((xmlAttrPtr) cur)->name);
3605 next = ((xmlAttrPtr) cur)->parent;
3606 } else {
3607 next = cur->parent;
3608 }
3609
3610 /*
3611 * Make sure there is enough room
3612 */
3613 if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) {
3614 buf_len =
3615 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20;
3616 temp = (xmlChar *) xmlRealloc(buffer, buf_len);
3617 if (temp == NULL) {
3618 xmlFree(buf);
3619 xmlFree(buffer);
3620 return (NULL);
3621 }
3622 buffer = temp;
3623 temp = (xmlChar *) xmlRealloc(buf, buf_len);
3624 if (temp == NULL) {
3625 xmlFree(buf);
3626 xmlFree(buffer);
3627 return (NULL);
3628 }
3629 buf = temp;
3630 }
3631 if (occur == 0)
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00003632 snprintf((char *) buf, buf_len, "%s%s%s",
Daniel Veillard8faa7832001-11-26 15:58:08 +00003633 sep, name, (char *) buffer);
3634 else
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00003635 snprintf((char *) buf, buf_len, "%s%s[%d]%s",
Daniel Veillard8faa7832001-11-26 15:58:08 +00003636 sep, name, occur, (char *) buffer);
3637 snprintf((char *) buffer, buf_len, "%s", buf);
3638 cur = next;
3639 } while (cur != NULL);
3640 xmlFree(buf);
3641 return (buffer);
3642}
3643
3644/**
Owen Taylor3473f882001-02-23 17:55:21 +00003645 * xmlDocGetRootElement:
3646 * @doc: the document
3647 *
3648 * Get the root element of the document (doc->children is a list
3649 * containing possibly comments, PIs, etc ...).
3650 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003651 * Returns the #xmlNodePtr for the root or NULL
Owen Taylor3473f882001-02-23 17:55:21 +00003652 */
3653xmlNodePtr
3654xmlDocGetRootElement(xmlDocPtr doc) {
3655 xmlNodePtr ret;
3656
3657 if (doc == NULL) return(NULL);
3658 ret = doc->children;
3659 while (ret != NULL) {
3660 if (ret->type == XML_ELEMENT_NODE)
3661 return(ret);
3662 ret = ret->next;
3663 }
3664 return(ret);
3665}
3666
3667/**
3668 * xmlDocSetRootElement:
3669 * @doc: the document
3670 * @root: the new document root element
3671 *
3672 * Set the root element of the document (doc->children is a list
3673 * containing possibly comments, PIs, etc ...).
3674 *
3675 * Returns the old root element if any was found
3676 */
3677xmlNodePtr
3678xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) {
3679 xmlNodePtr old = NULL;
3680
3681 if (doc == NULL) return(NULL);
Daniel Veillardc575b992002-02-08 13:28:40 +00003682 if (root == NULL)
3683 return(NULL);
3684 xmlUnlinkNode(root);
3685 root->doc = doc;
3686 root->parent = (xmlNodePtr) doc;
Owen Taylor3473f882001-02-23 17:55:21 +00003687 old = doc->children;
3688 while (old != NULL) {
3689 if (old->type == XML_ELEMENT_NODE)
3690 break;
3691 old = old->next;
3692 }
3693 if (old == NULL) {
3694 if (doc->children == NULL) {
3695 doc->children = root;
3696 doc->last = root;
3697 } else {
3698 xmlAddSibling(doc->children, root);
3699 }
3700 } else {
3701 xmlReplaceNode(old, root);
3702 }
3703 return(old);
3704}
3705
3706/**
3707 * xmlNodeSetLang:
3708 * @cur: the node being changed
Daniel Veillardd1640922001-12-17 15:30:10 +00003709 * @lang: the language description
Owen Taylor3473f882001-02-23 17:55:21 +00003710 *
3711 * Set the language of a node, i.e. the values of the xml:lang
3712 * attribute.
3713 */
3714void
3715xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00003716 xmlNsPtr ns;
3717
Owen Taylor3473f882001-02-23 17:55:21 +00003718 if (cur == NULL) return;
3719 switch(cur->type) {
3720 case XML_TEXT_NODE:
3721 case XML_CDATA_SECTION_NODE:
3722 case XML_COMMENT_NODE:
3723 case XML_DOCUMENT_NODE:
3724 case XML_DOCUMENT_TYPE_NODE:
3725 case XML_DOCUMENT_FRAG_NODE:
3726 case XML_NOTATION_NODE:
3727 case XML_HTML_DOCUMENT_NODE:
3728 case XML_DTD_NODE:
3729 case XML_ELEMENT_DECL:
3730 case XML_ATTRIBUTE_DECL:
3731 case XML_ENTITY_DECL:
3732 case XML_PI_NODE:
3733 case XML_ENTITY_REF_NODE:
3734 case XML_ENTITY_NODE:
3735 case XML_NAMESPACE_DECL:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003736#ifdef LIBXML_DOCB_ENABLED
3737 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003738#endif
3739 case XML_XINCLUDE_START:
3740 case XML_XINCLUDE_END:
3741 return;
3742 case XML_ELEMENT_NODE:
3743 case XML_ATTRIBUTE_NODE:
3744 break;
3745 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00003746 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
3747 if (ns == NULL)
3748 return;
3749 xmlSetNsProp(cur, ns, BAD_CAST "lang", lang);
Owen Taylor3473f882001-02-23 17:55:21 +00003750}
3751
3752/**
3753 * xmlNodeGetLang:
3754 * @cur: the node being checked
3755 *
3756 * Searches the language of a node, i.e. the values of the xml:lang
3757 * attribute or the one carried by the nearest ancestor.
3758 *
3759 * Returns a pointer to the lang value, or NULL if not found
Daniel Veillardbd9afb52002-09-25 22:25:35 +00003760 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00003761 */
3762xmlChar *
3763xmlNodeGetLang(xmlNodePtr cur) {
3764 xmlChar *lang;
3765
3766 while (cur != NULL) {
Daniel Veillardc17337c2001-05-09 10:51:31 +00003767 lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE);
Owen Taylor3473f882001-02-23 17:55:21 +00003768 if (lang != NULL)
3769 return(lang);
3770 cur = cur->parent;
3771 }
3772 return(NULL);
3773}
3774
3775
3776/**
3777 * xmlNodeSetSpacePreserve:
3778 * @cur: the node being changed
3779 * @val: the xml:space value ("0": default, 1: "preserve")
3780 *
3781 * Set (or reset) the space preserving behaviour of a node, i.e. the
3782 * value of the xml:space attribute.
3783 */
3784void
3785xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00003786 xmlNsPtr ns;
3787
Owen Taylor3473f882001-02-23 17:55:21 +00003788 if (cur == NULL) return;
3789 switch(cur->type) {
3790 case XML_TEXT_NODE:
3791 case XML_CDATA_SECTION_NODE:
3792 case XML_COMMENT_NODE:
3793 case XML_DOCUMENT_NODE:
3794 case XML_DOCUMENT_TYPE_NODE:
3795 case XML_DOCUMENT_FRAG_NODE:
3796 case XML_NOTATION_NODE:
3797 case XML_HTML_DOCUMENT_NODE:
3798 case XML_DTD_NODE:
3799 case XML_ELEMENT_DECL:
3800 case XML_ATTRIBUTE_DECL:
3801 case XML_ENTITY_DECL:
3802 case XML_PI_NODE:
3803 case XML_ENTITY_REF_NODE:
3804 case XML_ENTITY_NODE:
3805 case XML_NAMESPACE_DECL:
3806 case XML_XINCLUDE_START:
3807 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003808#ifdef LIBXML_DOCB_ENABLED
3809 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003810#endif
3811 return;
3812 case XML_ELEMENT_NODE:
3813 case XML_ATTRIBUTE_NODE:
3814 break;
3815 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00003816 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
3817 if (ns == NULL)
3818 return;
Owen Taylor3473f882001-02-23 17:55:21 +00003819 switch (val) {
3820 case 0:
Daniel Veillardcfa0d812002-01-17 08:46:58 +00003821 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "default");
Owen Taylor3473f882001-02-23 17:55:21 +00003822 break;
3823 case 1:
Daniel Veillardcfa0d812002-01-17 08:46:58 +00003824 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "preserve");
Owen Taylor3473f882001-02-23 17:55:21 +00003825 break;
3826 }
3827}
3828
3829/**
3830 * xmlNodeGetSpacePreserve:
3831 * @cur: the node being checked
3832 *
3833 * Searches the space preserving behaviour of a node, i.e. the values
3834 * of the xml:space attribute or the one carried by the nearest
3835 * ancestor.
3836 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003837 * Returns -1 if xml:space is not inherited, 0 if "default", 1 if "preserve"
Owen Taylor3473f882001-02-23 17:55:21 +00003838 */
3839int
3840xmlNodeGetSpacePreserve(xmlNodePtr cur) {
3841 xmlChar *space;
3842
3843 while (cur != NULL) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00003844 space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE);
Owen Taylor3473f882001-02-23 17:55:21 +00003845 if (space != NULL) {
3846 if (xmlStrEqual(space, BAD_CAST "preserve")) {
3847 xmlFree(space);
3848 return(1);
3849 }
3850 if (xmlStrEqual(space, BAD_CAST "default")) {
3851 xmlFree(space);
3852 return(0);
3853 }
3854 xmlFree(space);
3855 }
3856 cur = cur->parent;
3857 }
3858 return(-1);
3859}
3860
3861/**
3862 * xmlNodeSetName:
3863 * @cur: the node being changed
3864 * @name: the new tag name
3865 *
3866 * Set (or reset) the name of a node.
3867 */
3868void
3869xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) {
3870 if (cur == NULL) return;
3871 if (name == NULL) return;
3872 switch(cur->type) {
3873 case XML_TEXT_NODE:
3874 case XML_CDATA_SECTION_NODE:
3875 case XML_COMMENT_NODE:
3876 case XML_DOCUMENT_TYPE_NODE:
3877 case XML_DOCUMENT_FRAG_NODE:
3878 case XML_NOTATION_NODE:
3879 case XML_HTML_DOCUMENT_NODE:
3880 case XML_NAMESPACE_DECL:
3881 case XML_XINCLUDE_START:
3882 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003883#ifdef LIBXML_DOCB_ENABLED
3884 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003885#endif
3886 return;
3887 case XML_ELEMENT_NODE:
3888 case XML_ATTRIBUTE_NODE:
3889 case XML_PI_NODE:
3890 case XML_ENTITY_REF_NODE:
3891 case XML_ENTITY_NODE:
3892 case XML_DTD_NODE:
3893 case XML_DOCUMENT_NODE:
3894 case XML_ELEMENT_DECL:
3895 case XML_ATTRIBUTE_DECL:
3896 case XML_ENTITY_DECL:
3897 break;
3898 }
3899 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
3900 cur->name = xmlStrdup(name);
3901}
3902
3903/**
3904 * xmlNodeSetBase:
3905 * @cur: the node being changed
3906 * @uri: the new base URI
3907 *
3908 * Set (or reset) the base URI of a node, i.e. the value of the
3909 * xml:base attribute.
3910 */
3911void
3912xmlNodeSetBase(xmlNodePtr cur, xmlChar* uri) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00003913 xmlNsPtr ns;
3914
Owen Taylor3473f882001-02-23 17:55:21 +00003915 if (cur == NULL) return;
3916 switch(cur->type) {
3917 case XML_TEXT_NODE:
3918 case XML_CDATA_SECTION_NODE:
3919 case XML_COMMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003920 case XML_DOCUMENT_TYPE_NODE:
3921 case XML_DOCUMENT_FRAG_NODE:
3922 case XML_NOTATION_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00003923 case XML_DTD_NODE:
3924 case XML_ELEMENT_DECL:
3925 case XML_ATTRIBUTE_DECL:
3926 case XML_ENTITY_DECL:
3927 case XML_PI_NODE:
3928 case XML_ENTITY_REF_NODE:
3929 case XML_ENTITY_NODE:
3930 case XML_NAMESPACE_DECL:
3931 case XML_XINCLUDE_START:
3932 case XML_XINCLUDE_END:
Owen Taylor3473f882001-02-23 17:55:21 +00003933 return;
3934 case XML_ELEMENT_NODE:
3935 case XML_ATTRIBUTE_NODE:
3936 break;
Daniel Veillard4cbe4702002-05-05 06:57:27 +00003937 case XML_DOCUMENT_NODE:
3938#ifdef LIBXML_DOCB_ENABLED
3939 case XML_DOCB_DOCUMENT_NODE:
3940#endif
3941 case XML_HTML_DOCUMENT_NODE: {
3942 xmlDocPtr doc = (xmlDocPtr) cur;
3943
3944 if (doc->URL != NULL)
3945 xmlFree((xmlChar *) doc->URL);
3946 if (uri == NULL)
3947 doc->URL = NULL;
3948 else
3949 doc->URL = xmlStrdup(uri);
3950 return;
3951 }
Owen Taylor3473f882001-02-23 17:55:21 +00003952 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00003953
3954 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
3955 if (ns == NULL)
3956 return;
3957 xmlSetNsProp(cur, ns, BAD_CAST "base", uri);
Owen Taylor3473f882001-02-23 17:55:21 +00003958}
3959
3960/**
Owen Taylor3473f882001-02-23 17:55:21 +00003961 * xmlNodeGetBase:
3962 * @doc: the document the node pertains to
3963 * @cur: the node being checked
3964 *
3965 * Searches for the BASE URL. The code should work on both XML
3966 * and HTML document even if base mechanisms are completely different.
3967 * It returns the base as defined in RFC 2396 sections
3968 * 5.1.1. Base URI within Document Content
3969 * and
3970 * 5.1.2. Base URI from the Encapsulating Entity
3971 * However it does not return the document base (5.1.3), use
3972 * xmlDocumentGetBase() for this
3973 *
3974 * Returns a pointer to the base URL, or NULL if not found
Daniel Veillardbd9afb52002-09-25 22:25:35 +00003975 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00003976 */
3977xmlChar *
3978xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00003979 xmlChar *oldbase = NULL;
3980 xmlChar *base, *newbase;
Owen Taylor3473f882001-02-23 17:55:21 +00003981
3982 if ((cur == NULL) && (doc == NULL))
3983 return(NULL);
3984 if (doc == NULL) doc = cur->doc;
3985 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
3986 cur = doc->children;
3987 while ((cur != NULL) && (cur->name != NULL)) {
3988 if (cur->type != XML_ELEMENT_NODE) {
3989 cur = cur->next;
3990 continue;
3991 }
3992 if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) {
3993 cur = cur->children;
3994 continue;
3995 }
3996 if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) {
3997 cur = cur->children;
3998 continue;
3999 }
4000 if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) {
4001 return(xmlGetProp(cur, BAD_CAST "href"));
4002 }
4003 cur = cur->next;
4004 }
4005 return(NULL);
4006 }
4007 while (cur != NULL) {
4008 if (cur->type == XML_ENTITY_DECL) {
4009 xmlEntityPtr ent = (xmlEntityPtr) cur;
4010 return(xmlStrdup(ent->URI));
4011 }
Daniel Veillard42596ad2001-05-22 16:57:14 +00004012 if (cur->type == XML_ELEMENT_NODE) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00004013 base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE);
Daniel Veillard42596ad2001-05-22 16:57:14 +00004014 if (base != NULL) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00004015 if (oldbase != NULL) {
4016 newbase = xmlBuildURI(oldbase, base);
4017 if (newbase != NULL) {
4018 xmlFree(oldbase);
4019 xmlFree(base);
4020 oldbase = newbase;
4021 } else {
4022 xmlFree(oldbase);
4023 xmlFree(base);
4024 return(NULL);
4025 }
4026 } else {
4027 oldbase = base;
4028 }
4029 if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) ||
4030 (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) ||
4031 (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4)))
4032 return(oldbase);
Daniel Veillard42596ad2001-05-22 16:57:14 +00004033 }
4034 }
Owen Taylor3473f882001-02-23 17:55:21 +00004035 cur = cur->parent;
4036 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00004037 if ((doc != NULL) && (doc->URL != NULL)) {
4038 if (oldbase == NULL)
4039 return(xmlStrdup(doc->URL));
4040 newbase = xmlBuildURI(oldbase, doc->URL);
4041 xmlFree(oldbase);
4042 return(newbase);
4043 }
4044 return(oldbase);
Owen Taylor3473f882001-02-23 17:55:21 +00004045}
4046
4047/**
4048 * xmlNodeGetContent:
4049 * @cur: the node being read
4050 *
4051 * Read the value of a node, this can be either the text carried
4052 * directly by this node if it's a TEXT node or the aggregate string
4053 * of the values carried by this node child's (TEXT and ENTITY_REF).
Daniel Veillardd1640922001-12-17 15:30:10 +00004054 * Entity references are substituted.
4055 * Returns a new #xmlChar * or NULL if no content is available.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004056 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00004057 */
4058xmlChar *
Daniel Veillard7646b182002-04-20 06:41:40 +00004059xmlNodeGetContent(xmlNodePtr cur)
4060{
4061 if (cur == NULL)
4062 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004063 switch (cur->type) {
4064 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00004065 case XML_ELEMENT_NODE:{
4066 xmlNodePtr tmp = cur;
4067 xmlBufferPtr buffer;
4068 xmlChar *ret;
Owen Taylor3473f882001-02-23 17:55:21 +00004069
Daniel Veillard7646b182002-04-20 06:41:40 +00004070 buffer = xmlBufferCreate();
4071 if (buffer == NULL)
4072 return (NULL);
4073 while (tmp != NULL) {
4074 switch (tmp->type) {
4075 case XML_CDATA_SECTION_NODE:
4076 case XML_TEXT_NODE:
4077 if (tmp->content != NULL)
4078 xmlBufferCat(buffer, tmp->content);
4079 break;
4080 case XML_ENTITY_REF_NODE:{
4081 /* recursive substitution of entity references */
4082 xmlChar *cont = xmlNodeGetContent(tmp);
Owen Taylor3473f882001-02-23 17:55:21 +00004083
Daniel Veillard7646b182002-04-20 06:41:40 +00004084 if (cont) {
4085 xmlBufferCat(buffer,
4086 (const xmlChar *) cont);
4087 xmlFree(cont);
4088 }
4089 break;
4090 }
4091 default:
4092 break;
4093 }
4094 /*
4095 * Skip to next node
4096 */
4097 if (tmp->children != NULL) {
4098 if (tmp->children->type != XML_ENTITY_DECL) {
4099 tmp = tmp->children;
4100 continue;
4101 }
4102 }
4103 if (tmp == cur)
4104 break;
Daniel Veillard6c831202001-03-07 15:57:53 +00004105
Daniel Veillard7646b182002-04-20 06:41:40 +00004106 if (tmp->next != NULL) {
4107 tmp = tmp->next;
4108 continue;
4109 }
4110
4111 do {
4112 tmp = tmp->parent;
4113 if (tmp == NULL)
4114 break;
4115 if (tmp == cur) {
4116 tmp = NULL;
4117 break;
4118 }
4119 if (tmp->next != NULL) {
4120 tmp = tmp->next;
4121 break;
4122 }
4123 } while (tmp != NULL);
4124 }
4125 ret = buffer->content;
4126 buffer->content = NULL;
4127 xmlBufferFree(buffer);
4128 return (ret);
4129 }
4130 case XML_ATTRIBUTE_NODE:{
4131 xmlAttrPtr attr = (xmlAttrPtr) cur;
4132
4133 if (attr->parent != NULL)
4134 return (xmlNodeListGetString
4135 (attr->parent->doc, attr->children, 1));
4136 else
4137 return (xmlNodeListGetString(NULL, attr->children, 1));
4138 break;
4139 }
Owen Taylor3473f882001-02-23 17:55:21 +00004140 case XML_COMMENT_NODE:
4141 case XML_PI_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00004142 if (cur->content != NULL)
4143 return (xmlStrdup(cur->content));
4144 return (NULL);
4145 case XML_ENTITY_REF_NODE:{
4146 xmlEntityPtr ent;
4147 xmlNodePtr tmp;
4148 xmlBufferPtr buffer;
4149 xmlChar *ret;
4150
4151 /* lookup entity declaration */
4152 ent = xmlGetDocEntity(cur->doc, cur->name);
4153 if (ent == NULL)
4154 return (NULL);
4155
4156 buffer = xmlBufferCreate();
4157 if (buffer == NULL)
4158 return (NULL);
4159
4160 /* an entity content can be any "well balanced chunk",
4161 * i.e. the result of the content [43] production:
4162 * http://www.w3.org/TR/REC-xml#NT-content
4163 * -> we iterate through child nodes and recursive call
4164 * xmlNodeGetContent() which handles all possible node types */
4165 tmp = ent->children;
4166 while (tmp) {
4167 xmlChar *cont = xmlNodeGetContent(tmp);
4168
4169 if (cont) {
4170 xmlBufferCat(buffer, (const xmlChar *) cont);
4171 xmlFree(cont);
4172 }
4173 tmp = tmp->next;
4174 }
4175
4176 ret = buffer->content;
4177 buffer->content = NULL;
4178 xmlBufferFree(buffer);
4179 return (ret);
4180 }
Owen Taylor3473f882001-02-23 17:55:21 +00004181 case XML_ENTITY_NODE:
4182 case XML_DOCUMENT_NODE:
4183 case XML_HTML_DOCUMENT_NODE:
4184 case XML_DOCUMENT_TYPE_NODE:
4185 case XML_NOTATION_NODE:
4186 case XML_DTD_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00004187 case XML_XINCLUDE_START:
4188 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004189#ifdef LIBXML_DOCB_ENABLED
Daniel Veillard7646b182002-04-20 06:41:40 +00004190 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004191#endif
Daniel Veillard7646b182002-04-20 06:41:40 +00004192 return (NULL);
Daniel Veillard96c3a3b2002-10-14 15:39:04 +00004193 case XML_NAMESPACE_DECL: {
4194 xmlChar *tmp;
4195
4196 tmp = xmlStrdup(((xmlNsPtr) cur)->href);
4197 return (tmp);
4198 }
Owen Taylor3473f882001-02-23 17:55:21 +00004199 case XML_ELEMENT_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00004200 /* TODO !!! */
4201 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004202 case XML_ATTRIBUTE_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00004203 /* TODO !!! */
4204 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004205 case XML_ENTITY_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00004206 /* TODO !!! */
4207 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004208 case XML_CDATA_SECTION_NODE:
4209 case XML_TEXT_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00004210 if (cur->content != NULL)
4211 return (xmlStrdup(cur->content));
4212 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004213 }
Daniel Veillard7646b182002-04-20 06:41:40 +00004214 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004215}
Owen Taylor3473f882001-02-23 17:55:21 +00004216/**
4217 * xmlNodeSetContent:
4218 * @cur: the node being modified
4219 * @content: the new value of the content
4220 *
4221 * Replace the content of a node.
4222 */
4223void
4224xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) {
4225 if (cur == NULL) {
4226#ifdef DEBUG_TREE
4227 xmlGenericError(xmlGenericErrorContext,
4228 "xmlNodeSetContent : node == NULL\n");
4229#endif
4230 return;
4231 }
4232 switch (cur->type) {
4233 case XML_DOCUMENT_FRAG_NODE:
4234 case XML_ELEMENT_NODE:
Daniel Veillard2c748c62002-01-16 15:37:50 +00004235 case XML_ATTRIBUTE_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004236 if (cur->children != NULL) xmlFreeNodeList(cur->children);
4237 cur->children = xmlStringGetNodeList(cur->doc, content);
4238 UPDATE_LAST_CHILD_AND_PARENT(cur)
4239 break;
Owen Taylor3473f882001-02-23 17:55:21 +00004240 case XML_TEXT_NODE:
4241 case XML_CDATA_SECTION_NODE:
4242 case XML_ENTITY_REF_NODE:
4243 case XML_ENTITY_NODE:
4244 case XML_PI_NODE:
4245 case XML_COMMENT_NODE:
4246 if (cur->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004247 xmlFree(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00004248 }
4249 if (cur->children != NULL) xmlFreeNodeList(cur->children);
4250 cur->last = cur->children = NULL;
4251 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004252 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00004253 } else
4254 cur->content = NULL;
4255 break;
4256 case XML_DOCUMENT_NODE:
4257 case XML_HTML_DOCUMENT_NODE:
4258 case XML_DOCUMENT_TYPE_NODE:
4259 case XML_XINCLUDE_START:
4260 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004261#ifdef LIBXML_DOCB_ENABLED
4262 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004263#endif
4264 break;
4265 case XML_NOTATION_NODE:
4266 break;
4267 case XML_DTD_NODE:
4268 break;
4269 case XML_NAMESPACE_DECL:
4270 break;
4271 case XML_ELEMENT_DECL:
4272 /* TODO !!! */
4273 break;
4274 case XML_ATTRIBUTE_DECL:
4275 /* TODO !!! */
4276 break;
4277 case XML_ENTITY_DECL:
4278 /* TODO !!! */
4279 break;
4280 }
4281}
4282
4283/**
4284 * xmlNodeSetContentLen:
4285 * @cur: the node being modified
4286 * @content: the new value of the content
4287 * @len: the size of @content
4288 *
4289 * Replace the content of a node.
4290 */
4291void
4292xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
4293 if (cur == NULL) {
4294#ifdef DEBUG_TREE
4295 xmlGenericError(xmlGenericErrorContext,
4296 "xmlNodeSetContentLen : node == NULL\n");
4297#endif
4298 return;
4299 }
4300 switch (cur->type) {
4301 case XML_DOCUMENT_FRAG_NODE:
4302 case XML_ELEMENT_NODE:
Daniel Veillard2c748c62002-01-16 15:37:50 +00004303 case XML_ATTRIBUTE_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004304 if (cur->children != NULL) xmlFreeNodeList(cur->children);
4305 cur->children = xmlStringLenGetNodeList(cur->doc, content, len);
4306 UPDATE_LAST_CHILD_AND_PARENT(cur)
4307 break;
Owen Taylor3473f882001-02-23 17:55:21 +00004308 case XML_TEXT_NODE:
4309 case XML_CDATA_SECTION_NODE:
4310 case XML_ENTITY_REF_NODE:
4311 case XML_ENTITY_NODE:
4312 case XML_PI_NODE:
4313 case XML_COMMENT_NODE:
4314 case XML_NOTATION_NODE:
4315 if (cur->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004316 xmlFree(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00004317 }
4318 if (cur->children != NULL) xmlFreeNodeList(cur->children);
4319 cur->children = cur->last = NULL;
4320 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004321 cur->content = xmlStrndup(content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00004322 } else
4323 cur->content = NULL;
4324 break;
4325 case XML_DOCUMENT_NODE:
4326 case XML_DTD_NODE:
4327 case XML_HTML_DOCUMENT_NODE:
4328 case XML_DOCUMENT_TYPE_NODE:
4329 case XML_NAMESPACE_DECL:
4330 case XML_XINCLUDE_START:
4331 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004332#ifdef LIBXML_DOCB_ENABLED
4333 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004334#endif
4335 break;
4336 case XML_ELEMENT_DECL:
4337 /* TODO !!! */
4338 break;
4339 case XML_ATTRIBUTE_DECL:
4340 /* TODO !!! */
4341 break;
4342 case XML_ENTITY_DECL:
4343 /* TODO !!! */
4344 break;
4345 }
4346}
4347
4348/**
4349 * xmlNodeAddContentLen:
4350 * @cur: the node being modified
4351 * @content: extra content
4352 * @len: the size of @content
4353 *
4354 * Append the extra substring to the node content.
4355 */
4356void
4357xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
4358 if (cur == NULL) {
4359#ifdef DEBUG_TREE
4360 xmlGenericError(xmlGenericErrorContext,
4361 "xmlNodeAddContentLen : node == NULL\n");
4362#endif
4363 return;
4364 }
4365 if (len <= 0) return;
4366 switch (cur->type) {
4367 case XML_DOCUMENT_FRAG_NODE:
4368 case XML_ELEMENT_NODE: {
Daniel Veillardacb2bda2002-01-13 16:15:43 +00004369 xmlNodePtr last, newNode, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00004370
Daniel Veillard7db37732001-07-12 01:20:08 +00004371 last = cur->last;
Owen Taylor3473f882001-02-23 17:55:21 +00004372 newNode = xmlNewTextLen(content, len);
4373 if (newNode != NULL) {
Daniel Veillardacb2bda2002-01-13 16:15:43 +00004374 tmp = xmlAddChild(cur, newNode);
4375 if (tmp != newNode)
4376 return;
Owen Taylor3473f882001-02-23 17:55:21 +00004377 if ((last != NULL) && (last->next == newNode)) {
4378 xmlTextMerge(last, newNode);
4379 }
4380 }
4381 break;
4382 }
4383 case XML_ATTRIBUTE_NODE:
4384 break;
4385 case XML_TEXT_NODE:
4386 case XML_CDATA_SECTION_NODE:
4387 case XML_ENTITY_REF_NODE:
4388 case XML_ENTITY_NODE:
4389 case XML_PI_NODE:
4390 case XML_COMMENT_NODE:
4391 case XML_NOTATION_NODE:
4392 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004393 cur->content = xmlStrncat(cur->content, content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00004394 }
4395 case XML_DOCUMENT_NODE:
4396 case XML_DTD_NODE:
4397 case XML_HTML_DOCUMENT_NODE:
4398 case XML_DOCUMENT_TYPE_NODE:
4399 case XML_NAMESPACE_DECL:
4400 case XML_XINCLUDE_START:
4401 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004402#ifdef LIBXML_DOCB_ENABLED
4403 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004404#endif
4405 break;
4406 case XML_ELEMENT_DECL:
4407 case XML_ATTRIBUTE_DECL:
4408 case XML_ENTITY_DECL:
4409 break;
4410 }
4411}
4412
4413/**
4414 * xmlNodeAddContent:
4415 * @cur: the node being modified
4416 * @content: extra content
4417 *
4418 * Append the extra substring to the node content.
4419 */
4420void
4421xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) {
4422 int len;
4423
4424 if (cur == NULL) {
4425#ifdef DEBUG_TREE
4426 xmlGenericError(xmlGenericErrorContext,
4427 "xmlNodeAddContent : node == NULL\n");
4428#endif
4429 return;
4430 }
4431 if (content == NULL) return;
4432 len = xmlStrlen(content);
4433 xmlNodeAddContentLen(cur, content, len);
4434}
4435
4436/**
4437 * xmlTextMerge:
4438 * @first: the first text node
4439 * @second: the second text node being merged
4440 *
4441 * Merge two text nodes into one
4442 * Returns the first text node augmented
4443 */
4444xmlNodePtr
4445xmlTextMerge(xmlNodePtr first, xmlNodePtr second) {
4446 if (first == NULL) return(second);
4447 if (second == NULL) return(first);
4448 if (first->type != XML_TEXT_NODE) return(first);
4449 if (second->type != XML_TEXT_NODE) return(first);
4450 if (second->name != first->name)
4451 return(first);
Owen Taylor3473f882001-02-23 17:55:21 +00004452 xmlNodeAddContent(first, second->content);
Owen Taylor3473f882001-02-23 17:55:21 +00004453 xmlUnlinkNode(second);
4454 xmlFreeNode(second);
4455 return(first);
4456}
4457
4458/**
4459 * xmlGetNsList:
4460 * @doc: the document
4461 * @node: the current node
4462 *
4463 * Search all the namespace applying to a given element.
Daniel Veillardd1640922001-12-17 15:30:10 +00004464 * Returns an NULL terminated array of all the #xmlNsPtr found
Owen Taylor3473f882001-02-23 17:55:21 +00004465 * that need to be freed by the caller or NULL if no
4466 * namespace if defined
4467 */
4468xmlNsPtr *
Daniel Veillard77044732001-06-29 21:31:07 +00004469xmlGetNsList(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node)
4470{
Owen Taylor3473f882001-02-23 17:55:21 +00004471 xmlNsPtr cur;
4472 xmlNsPtr *ret = NULL;
4473 int nbns = 0;
4474 int maxns = 10;
4475 int i;
4476
4477 while (node != NULL) {
Daniel Veillard77044732001-06-29 21:31:07 +00004478 if (node->type == XML_ELEMENT_NODE) {
4479 cur = node->nsDef;
4480 while (cur != NULL) {
4481 if (ret == NULL) {
4482 ret =
4483 (xmlNsPtr *) xmlMalloc((maxns + 1) *
4484 sizeof(xmlNsPtr));
4485 if (ret == NULL) {
4486 xmlGenericError(xmlGenericErrorContext,
4487 "xmlGetNsList : out of memory!\n");
4488 return (NULL);
4489 }
4490 ret[nbns] = NULL;
4491 }
4492 for (i = 0; i < nbns; i++) {
4493 if ((cur->prefix == ret[i]->prefix) ||
4494 (xmlStrEqual(cur->prefix, ret[i]->prefix)))
4495 break;
4496 }
4497 if (i >= nbns) {
4498 if (nbns >= maxns) {
4499 maxns *= 2;
4500 ret = (xmlNsPtr *) xmlRealloc(ret,
4501 (maxns +
4502 1) *
4503 sizeof(xmlNsPtr));
4504 if (ret == NULL) {
4505 xmlGenericError(xmlGenericErrorContext,
4506 "xmlGetNsList : realloc failed!\n");
4507 return (NULL);
4508 }
4509 }
4510 ret[nbns++] = cur;
4511 ret[nbns] = NULL;
4512 }
Owen Taylor3473f882001-02-23 17:55:21 +00004513
Daniel Veillard77044732001-06-29 21:31:07 +00004514 cur = cur->next;
4515 }
4516 }
4517 node = node->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00004518 }
Daniel Veillard77044732001-06-29 21:31:07 +00004519 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00004520}
4521
4522/**
4523 * xmlSearchNs:
4524 * @doc: the document
4525 * @node: the current node
Daniel Veillard77851712001-02-27 21:54:07 +00004526 * @nameSpace: the namespace prefix
Owen Taylor3473f882001-02-23 17:55:21 +00004527 *
4528 * Search a Ns registered under a given name space for a document.
4529 * recurse on the parents until it finds the defined namespace
4530 * or return NULL otherwise.
4531 * @nameSpace can be NULL, this is a search for the default namespace.
4532 * We don't allow to cross entities boundaries. If you don't declare
4533 * the namespace within those you will be in troubles !!! A warning
4534 * is generated to cover this case.
4535 *
4536 * Returns the namespace pointer or NULL.
4537 */
4538xmlNsPtr
4539xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) {
4540 xmlNsPtr cur;
4541
4542 if (node == NULL) return(NULL);
4543 if ((nameSpace != NULL) &&
4544 (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) {
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00004545 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
4546 /*
4547 * The XML-1.0 namespace is normally held on the root
4548 * element. In this case exceptionally create it on the
4549 * node element.
4550 */
4551 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
4552 if (cur == NULL) {
4553 xmlGenericError(xmlGenericErrorContext,
4554 "xmlSearchNs : malloc failed\n");
4555 return(NULL);
4556 }
4557 memset(cur, 0, sizeof(xmlNs));
4558 cur->type = XML_LOCAL_NAMESPACE;
4559 cur->href = xmlStrdup(XML_XML_NAMESPACE);
4560 cur->prefix = xmlStrdup((const xmlChar *)"xml");
4561 cur->next = node->nsDef;
4562 node->nsDef = cur;
4563 return(cur);
4564 }
Owen Taylor3473f882001-02-23 17:55:21 +00004565 if (doc->oldNs == NULL) {
4566 /*
4567 * Allocate a new Namespace and fill the fields.
4568 */
4569 doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
4570 if (doc->oldNs == NULL) {
4571 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00004572 "xmlSearchNs : malloc failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004573 return(NULL);
4574 }
4575 memset(doc->oldNs, 0, sizeof(xmlNs));
4576 doc->oldNs->type = XML_LOCAL_NAMESPACE;
4577
4578 doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE);
4579 doc->oldNs->prefix = xmlStrdup((const xmlChar *)"xml");
4580 }
4581 return(doc->oldNs);
4582 }
4583 while (node != NULL) {
4584 if ((node->type == XML_ENTITY_REF_NODE) ||
4585 (node->type == XML_ENTITY_NODE) ||
4586 (node->type == XML_ENTITY_DECL))
4587 return(NULL);
4588 if (node->type == XML_ELEMENT_NODE) {
4589 cur = node->nsDef;
4590 while (cur != NULL) {
4591 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
4592 (cur->href != NULL))
4593 return(cur);
4594 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
4595 (cur->href != NULL) &&
4596 (xmlStrEqual(cur->prefix, nameSpace)))
4597 return(cur);
4598 cur = cur->next;
4599 }
4600 }
4601 node = node->parent;
4602 }
4603 return(NULL);
4604}
4605
4606/**
4607 * xmlSearchNsByHref:
4608 * @doc: the document
4609 * @node: the current node
4610 * @href: the namespace value
4611 *
4612 * Search a Ns aliasing a given URI. Recurse on the parents until it finds
4613 * the defined namespace or return NULL otherwise.
4614 * Returns the namespace pointer or NULL.
4615 */
4616xmlNsPtr
4617xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar *href) {
4618 xmlNsPtr cur;
4619 xmlNodePtr orig = node;
4620
4621 if ((node == NULL) || (href == NULL)) return(NULL);
4622 if (xmlStrEqual(href, XML_XML_NAMESPACE)) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004623 /*
4624 * Only the document can hold the XML spec namespace.
4625 */
Daniel Veillardc1a0da32002-08-14 08:32:18 +00004626 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
4627 /*
4628 * The XML-1.0 namespace is normally held on the root
4629 * element. In this case exceptionally create it on the
4630 * node element.
4631 */
4632 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
4633 if (cur == NULL) {
4634 xmlGenericError(xmlGenericErrorContext,
4635 "xmlSearchNs : malloc failed\n");
4636 return(NULL);
4637 }
4638 memset(cur, 0, sizeof(xmlNs));
4639 cur->type = XML_LOCAL_NAMESPACE;
4640 cur->href = xmlStrdup(XML_XML_NAMESPACE);
4641 cur->prefix = xmlStrdup((const xmlChar *)"xml");
4642 cur->next = node->nsDef;
4643 node->nsDef = cur;
4644 return(cur);
4645 }
Owen Taylor3473f882001-02-23 17:55:21 +00004646 if (doc->oldNs == NULL) {
4647 /*
4648 * Allocate a new Namespace and fill the fields.
4649 */
4650 doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
4651 if (doc->oldNs == NULL) {
4652 xmlGenericError(xmlGenericErrorContext,
4653 "xmlSearchNsByHref : malloc failed\n");
4654 return(NULL);
4655 }
4656 memset(doc->oldNs, 0, sizeof(xmlNs));
4657 doc->oldNs->type = XML_LOCAL_NAMESPACE;
4658
4659 doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE);
4660 doc->oldNs->prefix = xmlStrdup((const xmlChar *)"xml");
4661 }
4662 return(doc->oldNs);
4663 }
4664 while (node != NULL) {
4665 cur = node->nsDef;
4666 while (cur != NULL) {
4667 if ((cur->href != NULL) && (href != NULL) &&
4668 (xmlStrEqual(cur->href, href))) {
4669 /*
4670 * Check that the prefix is not shadowed between orig and node
4671 */
4672 xmlNodePtr check = orig;
4673 xmlNsPtr tst;
4674
4675 while (check != node) {
4676 tst = check->nsDef;
4677 while (tst != NULL) {
4678 if ((tst->prefix == NULL) && (cur->prefix == NULL))
4679 goto shadowed;
4680 if ((tst->prefix != NULL) && (cur->prefix != NULL) &&
4681 (xmlStrEqual(tst->prefix, cur->prefix)))
4682 goto shadowed;
4683 tst = tst->next;
4684 }
4685 check = check->parent;
4686 }
4687 return(cur);
4688 }
4689shadowed:
4690 cur = cur->next;
4691 }
4692 node = node->parent;
4693 }
4694 return(NULL);
4695}
4696
4697/**
4698 * xmlNewReconciliedNs
4699 * @doc: the document
4700 * @tree: a node expected to hold the new namespace
4701 * @ns: the original namespace
4702 *
4703 * This function tries to locate a namespace definition in a tree
4704 * ancestors, or create a new namespace definition node similar to
4705 * @ns trying to reuse the same prefix. However if the given prefix is
4706 * null (default namespace) or reused within the subtree defined by
4707 * @tree or on one of its ancestors then a new prefix is generated.
4708 * Returns the (new) namespace definition or NULL in case of error
4709 */
4710xmlNsPtr
4711xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) {
4712 xmlNsPtr def;
4713 xmlChar prefix[50];
4714 int counter = 1;
4715
4716 if (tree == NULL) {
4717#ifdef DEBUG_TREE
4718 xmlGenericError(xmlGenericErrorContext,
4719 "xmlNewReconciliedNs : tree == NULL\n");
4720#endif
4721 return(NULL);
4722 }
4723 if (ns == NULL) {
4724#ifdef DEBUG_TREE
4725 xmlGenericError(xmlGenericErrorContext,
4726 "xmlNewReconciliedNs : ns == NULL\n");
4727#endif
4728 return(NULL);
4729 }
4730 /*
4731 * Search an existing namespace definition inherited.
4732 */
4733 def = xmlSearchNsByHref(doc, tree, ns->href);
4734 if (def != NULL)
4735 return(def);
4736
4737 /*
4738 * Find a close prefix which is not already in use.
4739 * Let's strip namespace prefixes longer than 20 chars !
4740 */
Daniel Veillardf742d342002-03-07 00:05:35 +00004741 if (ns->prefix == NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00004742 snprintf((char *) prefix, sizeof(prefix), "default");
Daniel Veillardf742d342002-03-07 00:05:35 +00004743 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00004744 snprintf((char *) prefix, sizeof(prefix), "%.20s", ns->prefix);
Daniel Veillardf742d342002-03-07 00:05:35 +00004745
Owen Taylor3473f882001-02-23 17:55:21 +00004746 def = xmlSearchNs(doc, tree, prefix);
4747 while (def != NULL) {
4748 if (counter > 1000) return(NULL);
Daniel Veillardf742d342002-03-07 00:05:35 +00004749 if (ns->prefix == NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00004750 snprintf((char *) prefix, sizeof(prefix), "default%d", counter++);
Daniel Veillardf742d342002-03-07 00:05:35 +00004751 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00004752 snprintf((char *) prefix, sizeof(prefix), "%.20s%d", ns->prefix, counter++);
Owen Taylor3473f882001-02-23 17:55:21 +00004753 def = xmlSearchNs(doc, tree, prefix);
4754 }
4755
4756 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00004757 * OK, now we are ready to create a new one.
Owen Taylor3473f882001-02-23 17:55:21 +00004758 */
4759 def = xmlNewNs(tree, ns->href, prefix);
4760 return(def);
4761}
4762
4763/**
4764 * xmlReconciliateNs
4765 * @doc: the document
4766 * @tree: a node defining the subtree to reconciliate
4767 *
4768 * This function checks that all the namespaces declared within the given
4769 * tree are properly declared. This is needed for example after Copy or Cut
4770 * and then paste operations. The subtree may still hold pointers to
4771 * namespace declarations outside the subtree or invalid/masked. As much
Daniel Veillardd1640922001-12-17 15:30:10 +00004772 * as possible the function try to reuse the existing namespaces found in
Owen Taylor3473f882001-02-23 17:55:21 +00004773 * the new environment. If not possible the new namespaces are redeclared
4774 * on @tree at the top of the given subtree.
4775 * Returns the number of namespace declarations created or -1 in case of error.
4776 */
4777int
4778xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) {
4779 xmlNsPtr *oldNs = NULL;
4780 xmlNsPtr *newNs = NULL;
4781 int sizeCache = 0;
4782 int nbCache = 0;
4783
4784 xmlNsPtr n;
4785 xmlNodePtr node = tree;
4786 xmlAttrPtr attr;
4787 int ret = 0, i;
4788
4789 while (node != NULL) {
4790 /*
4791 * Reconciliate the node namespace
4792 */
4793 if (node->ns != NULL) {
4794 /*
4795 * initialize the cache if needed
4796 */
4797 if (sizeCache == 0) {
4798 sizeCache = 10;
4799 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
4800 sizeof(xmlNsPtr));
4801 if (oldNs == NULL) {
4802 xmlGenericError(xmlGenericErrorContext,
4803 "xmlReconciliateNs : memory pbm\n");
4804 return(-1);
4805 }
4806 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
4807 sizeof(xmlNsPtr));
4808 if (newNs == NULL) {
4809 xmlGenericError(xmlGenericErrorContext,
4810 "xmlReconciliateNs : memory pbm\n");
4811 xmlFree(oldNs);
4812 return(-1);
4813 }
4814 }
4815 for (i = 0;i < nbCache;i++) {
4816 if (oldNs[i] == node->ns) {
4817 node->ns = newNs[i];
4818 break;
4819 }
4820 }
4821 if (i == nbCache) {
4822 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00004823 * OK we need to recreate a new namespace definition
Owen Taylor3473f882001-02-23 17:55:21 +00004824 */
4825 n = xmlNewReconciliedNs(doc, tree, node->ns);
4826 if (n != NULL) { /* :-( what if else ??? */
4827 /*
4828 * check if we need to grow the cache buffers.
4829 */
4830 if (sizeCache <= nbCache) {
4831 sizeCache *= 2;
4832 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
4833 sizeof(xmlNsPtr));
4834 if (oldNs == NULL) {
4835 xmlGenericError(xmlGenericErrorContext,
4836 "xmlReconciliateNs : memory pbm\n");
4837 xmlFree(newNs);
4838 return(-1);
4839 }
4840 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
4841 sizeof(xmlNsPtr));
4842 if (newNs == NULL) {
4843 xmlGenericError(xmlGenericErrorContext,
4844 "xmlReconciliateNs : memory pbm\n");
4845 xmlFree(oldNs);
4846 return(-1);
4847 }
4848 }
4849 newNs[nbCache] = n;
4850 oldNs[nbCache++] = node->ns;
4851 node->ns = n;
4852 }
4853 }
4854 }
4855 /*
4856 * now check for namespace hold by attributes on the node.
4857 */
4858 attr = node->properties;
4859 while (attr != NULL) {
4860 if (attr->ns != NULL) {
4861 /*
4862 * initialize the cache if needed
4863 */
4864 if (sizeCache == 0) {
4865 sizeCache = 10;
4866 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
4867 sizeof(xmlNsPtr));
4868 if (oldNs == NULL) {
4869 xmlGenericError(xmlGenericErrorContext,
4870 "xmlReconciliateNs : memory pbm\n");
4871 return(-1);
4872 }
4873 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
4874 sizeof(xmlNsPtr));
4875 if (newNs == NULL) {
4876 xmlGenericError(xmlGenericErrorContext,
4877 "xmlReconciliateNs : memory pbm\n");
4878 xmlFree(oldNs);
4879 return(-1);
4880 }
4881 }
4882 for (i = 0;i < nbCache;i++) {
4883 if (oldNs[i] == attr->ns) {
Daniel Veillardce66ce12002-10-28 19:01:59 +00004884 attr->ns = newNs[i];
Owen Taylor3473f882001-02-23 17:55:21 +00004885 break;
4886 }
4887 }
4888 if (i == nbCache) {
4889 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00004890 * OK we need to recreate a new namespace definition
Owen Taylor3473f882001-02-23 17:55:21 +00004891 */
4892 n = xmlNewReconciliedNs(doc, tree, attr->ns);
4893 if (n != NULL) { /* :-( what if else ??? */
4894 /*
4895 * check if we need to grow the cache buffers.
4896 */
4897 if (sizeCache <= nbCache) {
4898 sizeCache *= 2;
4899 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
4900 sizeof(xmlNsPtr));
4901 if (oldNs == NULL) {
4902 xmlGenericError(xmlGenericErrorContext,
4903 "xmlReconciliateNs : memory pbm\n");
4904 xmlFree(newNs);
4905 return(-1);
4906 }
4907 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
4908 sizeof(xmlNsPtr));
4909 if (newNs == NULL) {
4910 xmlGenericError(xmlGenericErrorContext,
4911 "xmlReconciliateNs : memory pbm\n");
4912 xmlFree(oldNs);
4913 return(-1);
4914 }
4915 }
4916 newNs[nbCache] = n;
4917 oldNs[nbCache++] = attr->ns;
4918 attr->ns = n;
4919 }
4920 }
4921 }
4922 attr = attr->next;
4923 }
4924
4925 /*
4926 * Browse the full subtree, deep first
4927 */
4928 if (node->children != NULL) {
4929 /* deep first */
4930 node = node->children;
4931 } else if ((node != tree) && (node->next != NULL)) {
4932 /* then siblings */
4933 node = node->next;
4934 } else if (node != tree) {
4935 /* go up to parents->next if needed */
4936 while (node != tree) {
4937 if (node->parent != NULL)
4938 node = node->parent;
4939 if ((node != tree) && (node->next != NULL)) {
4940 node = node->next;
4941 break;
4942 }
4943 if (node->parent == NULL) {
4944 node = NULL;
4945 break;
4946 }
4947 }
4948 /* exit condition */
4949 if (node == tree)
4950 node = NULL;
Daniel Veillard1e774382002-03-06 17:35:40 +00004951 } else
4952 break;
Owen Taylor3473f882001-02-23 17:55:21 +00004953 }
Daniel Veillardf742d342002-03-07 00:05:35 +00004954 if (oldNs != NULL)
4955 xmlFree(oldNs);
4956 if (newNs != NULL)
4957 xmlFree(newNs);
Owen Taylor3473f882001-02-23 17:55:21 +00004958 return(ret);
4959}
4960
4961/**
4962 * xmlHasProp:
4963 * @node: the node
4964 * @name: the attribute name
4965 *
4966 * Search an attribute associated to a node
4967 * This function also looks in DTD attribute declaration for #FIXED or
4968 * default declaration values unless DTD use has been turned off.
4969 *
4970 * Returns the attribute or the attribute declaration or NULL if
4971 * neither was found.
4972 */
4973xmlAttrPtr
4974xmlHasProp(xmlNodePtr node, const xmlChar *name) {
4975 xmlAttrPtr prop;
4976 xmlDocPtr doc;
4977
4978 if ((node == NULL) || (name == NULL)) return(NULL);
4979 /*
4980 * Check on the properties attached to the node
4981 */
4982 prop = node->properties;
4983 while (prop != NULL) {
4984 if (xmlStrEqual(prop->name, name)) {
4985 return(prop);
4986 }
4987 prop = prop->next;
4988 }
4989 if (!xmlCheckDTD) return(NULL);
4990
4991 /*
4992 * Check if there is a default declaration in the internal
4993 * or external subsets
4994 */
4995 doc = node->doc;
4996 if (doc != NULL) {
4997 xmlAttributePtr attrDecl;
4998 if (doc->intSubset != NULL) {
4999 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
5000 if ((attrDecl == NULL) && (doc->extSubset != NULL))
5001 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
5002 if (attrDecl != NULL)
5003 return((xmlAttrPtr) attrDecl);
5004 }
5005 }
5006 return(NULL);
5007}
5008
5009/**
Daniel Veillarde95e2392001-06-06 10:46:28 +00005010 * xmlHasNsProp:
5011 * @node: the node
5012 * @name: the attribute name
Daniel Veillardca2366a2001-06-11 12:09:01 +00005013 * @nameSpace: the URI of the namespace
Daniel Veillarde95e2392001-06-06 10:46:28 +00005014 *
5015 * Search for an attribute associated to a node
5016 * This attribute has to be anchored in the namespace specified.
5017 * This does the entity substitution.
5018 * This function looks in DTD attribute declaration for #FIXED or
5019 * default declaration values unless DTD use has been turned off.
5020 *
5021 * Returns the attribute or the attribute declaration or NULL
5022 * if neither was found.
5023 */
5024xmlAttrPtr
Daniel Veillardca2366a2001-06-11 12:09:01 +00005025xmlHasNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
Daniel Veillarde95e2392001-06-06 10:46:28 +00005026 xmlAttrPtr prop;
5027 xmlDocPtr doc;
Daniel Veillarde95e2392001-06-06 10:46:28 +00005028
5029 if (node == NULL)
5030 return(NULL);
5031
5032 prop = node->properties;
Daniel Veillardca2366a2001-06-11 12:09:01 +00005033 if (nameSpace == NULL)
Daniel Veillarde95e2392001-06-06 10:46:28 +00005034 return(xmlHasProp(node, name));
5035 while (prop != NULL) {
5036 /*
5037 * One need to have
5038 * - same attribute names
5039 * - and the attribute carrying that namespace
Daniel Veillarde95e2392001-06-06 10:46:28 +00005040 */
5041 if ((xmlStrEqual(prop->name, name)) &&
Daniel Veillarde3c81b52001-06-17 14:50:34 +00005042 ((prop->ns != NULL) && (xmlStrEqual(prop->ns->href, nameSpace)))) {
5043 return(prop);
Daniel Veillarde95e2392001-06-06 10:46:28 +00005044 }
5045 prop = prop->next;
5046 }
5047 if (!xmlCheckDTD) return(NULL);
5048
5049 /*
5050 * Check if there is a default declaration in the internal
5051 * or external subsets
5052 */
5053 doc = node->doc;
5054 if (doc != NULL) {
5055 if (doc->intSubset != NULL) {
Daniel Veillardef6c46f2002-03-07 22:21:56 +00005056 xmlAttributePtr attrDecl = NULL;
5057 xmlNsPtr *nsList, *cur;
5058 xmlChar *ename;
Daniel Veillarde95e2392001-06-06 10:46:28 +00005059
Daniel Veillardef6c46f2002-03-07 22:21:56 +00005060 nsList = xmlGetNsList(node->doc, node);
5061 if (nsList == NULL)
5062 return(NULL);
5063 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
5064 ename = xmlStrdup(node->ns->prefix);
5065 ename = xmlStrcat(ename, BAD_CAST ":");
5066 ename = xmlStrcat(ename, node->name);
5067 } else {
5068 ename = xmlStrdup(node->name);
Daniel Veillarde95e2392001-06-06 10:46:28 +00005069 }
Daniel Veillardef6c46f2002-03-07 22:21:56 +00005070 if (ename == NULL) {
5071 xmlFree(nsList);
5072 return(NULL);
5073 }
5074
5075 cur = nsList;
5076 while (*cur != NULL) {
5077 if (xmlStrEqual((*cur)->href, nameSpace)) {
5078 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, ename,
5079 name, (*cur)->prefix);
5080 if ((attrDecl == NULL) && (doc->extSubset != NULL))
5081 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, ename,
5082 name, (*cur)->prefix);
5083 }
5084 cur++;
5085 }
5086 xmlFree(nsList);
5087 xmlFree(ename);
5088 return((xmlAttrPtr) attrDecl);
Daniel Veillarde95e2392001-06-06 10:46:28 +00005089 }
5090 }
5091 return(NULL);
5092}
5093
5094/**
Owen Taylor3473f882001-02-23 17:55:21 +00005095 * xmlGetProp:
5096 * @node: the node
5097 * @name: the attribute name
5098 *
5099 * Search and get the value of an attribute associated to a node
5100 * This does the entity substitution.
5101 * This function looks in DTD attribute declaration for #FIXED or
5102 * default declaration values unless DTD use has been turned off.
5103 *
5104 * Returns the attribute value or NULL if not found.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00005105 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00005106 */
5107xmlChar *
5108xmlGetProp(xmlNodePtr node, const xmlChar *name) {
5109 xmlAttrPtr prop;
5110 xmlDocPtr doc;
5111
5112 if ((node == NULL) || (name == NULL)) return(NULL);
5113 /*
5114 * Check on the properties attached to the node
5115 */
5116 prop = node->properties;
5117 while (prop != NULL) {
5118 if (xmlStrEqual(prop->name, name)) {
5119 xmlChar *ret;
5120
5121 ret = xmlNodeListGetString(node->doc, prop->children, 1);
5122 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
5123 return(ret);
5124 }
5125 prop = prop->next;
5126 }
5127 if (!xmlCheckDTD) return(NULL);
5128
5129 /*
5130 * Check if there is a default declaration in the internal
5131 * or external subsets
5132 */
5133 doc = node->doc;
5134 if (doc != NULL) {
5135 xmlAttributePtr attrDecl;
5136 if (doc->intSubset != NULL) {
5137 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
5138 if ((attrDecl == NULL) && (doc->extSubset != NULL))
5139 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
5140 if (attrDecl != NULL)
5141 return(xmlStrdup(attrDecl->defaultValue));
5142 }
5143 }
5144 return(NULL);
5145}
5146
5147/**
5148 * xmlGetNsProp:
5149 * @node: the node
5150 * @name: the attribute name
Daniel Veillardca2366a2001-06-11 12:09:01 +00005151 * @nameSpace: the URI of the namespace
Owen Taylor3473f882001-02-23 17:55:21 +00005152 *
5153 * Search and get the value of an attribute associated to a node
5154 * This attribute has to be anchored in the namespace specified.
5155 * This does the entity substitution.
5156 * This function looks in DTD attribute declaration for #FIXED or
5157 * default declaration values unless DTD use has been turned off.
5158 *
5159 * Returns the attribute value or NULL if not found.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00005160 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00005161 */
5162xmlChar *
Daniel Veillardca2366a2001-06-11 12:09:01 +00005163xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
Owen Taylor3473f882001-02-23 17:55:21 +00005164 xmlAttrPtr prop;
5165 xmlDocPtr doc;
5166 xmlNsPtr ns;
5167
5168 if (node == NULL)
5169 return(NULL);
5170
5171 prop = node->properties;
Daniel Veillardca2366a2001-06-11 12:09:01 +00005172 if (nameSpace == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00005173 return(xmlGetProp(node, name));
5174 while (prop != NULL) {
5175 /*
5176 * One need to have
5177 * - same attribute names
5178 * - and the attribute carrying that namespace
Owen Taylor3473f882001-02-23 17:55:21 +00005179 */
5180 if ((xmlStrEqual(prop->name, name)) &&
Daniel Veillarde8fc08e2001-06-07 19:35:47 +00005181 ((prop->ns != NULL) &&
Daniel Veillardca2366a2001-06-11 12:09:01 +00005182 (xmlStrEqual(prop->ns->href, nameSpace)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00005183 xmlChar *ret;
5184
5185 ret = xmlNodeListGetString(node->doc, prop->children, 1);
5186 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
5187 return(ret);
5188 }
5189 prop = prop->next;
5190 }
5191 if (!xmlCheckDTD) return(NULL);
5192
5193 /*
5194 * Check if there is a default declaration in the internal
5195 * or external subsets
5196 */
5197 doc = node->doc;
5198 if (doc != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005199 if (doc->intSubset != NULL) {
Daniel Veillard5792e162001-04-30 17:44:45 +00005200 xmlAttributePtr attrDecl;
5201
Owen Taylor3473f882001-02-23 17:55:21 +00005202 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
5203 if ((attrDecl == NULL) && (doc->extSubset != NULL))
5204 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
5205
5206 if ((attrDecl != NULL) && (attrDecl->prefix != NULL)) {
5207 /*
5208 * The DTD declaration only allows a prefix search
5209 */
5210 ns = xmlSearchNs(doc, node, attrDecl->prefix);
Daniel Veillardca2366a2001-06-11 12:09:01 +00005211 if ((ns != NULL) && (xmlStrEqual(ns->href, nameSpace)))
Owen Taylor3473f882001-02-23 17:55:21 +00005212 return(xmlStrdup(attrDecl->defaultValue));
5213 }
5214 }
5215 }
5216 return(NULL);
5217}
5218
5219/**
5220 * xmlSetProp:
5221 * @node: the node
5222 * @name: the attribute name
5223 * @value: the attribute value
5224 *
5225 * Set (or reset) an attribute carried by a node.
5226 * Returns the attribute pointer.
5227 */
5228xmlAttrPtr
5229xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
Daniel Veillard4855c8c2001-11-25 10:35:25 +00005230 xmlAttrPtr prop;
5231 xmlDocPtr doc;
Owen Taylor3473f882001-02-23 17:55:21 +00005232
5233 if ((node == NULL) || (name == NULL))
5234 return(NULL);
5235 doc = node->doc;
Daniel Veillard4855c8c2001-11-25 10:35:25 +00005236 prop = node->properties;
Owen Taylor3473f882001-02-23 17:55:21 +00005237 while (prop != NULL) {
Daniel Veillard75bea542001-05-11 17:41:21 +00005238 if ((xmlStrEqual(prop->name, name)) &&
5239 (prop->ns == NULL)){
Daniel Veillard4855c8c2001-11-25 10:35:25 +00005240 xmlNodePtr oldprop = prop->children;
5241
Owen Taylor3473f882001-02-23 17:55:21 +00005242 prop->children = NULL;
5243 prop->last = NULL;
5244 if (value != NULL) {
5245 xmlChar *buffer;
5246 xmlNodePtr tmp;
5247
5248 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
5249 prop->children = xmlStringGetNodeList(node->doc, buffer);
5250 prop->last = NULL;
5251 prop->doc = doc;
5252 tmp = prop->children;
5253 while (tmp != NULL) {
5254 tmp->parent = (xmlNodePtr) prop;
5255 tmp->doc = doc;
5256 if (tmp->next == NULL)
5257 prop->last = tmp;
5258 tmp = tmp->next;
5259 }
5260 xmlFree(buffer);
Daniel Veillard75bea542001-05-11 17:41:21 +00005261 }
Daniel Veillard4855c8c2001-11-25 10:35:25 +00005262 if (oldprop != NULL)
5263 xmlFreeNodeList(oldprop);
Owen Taylor3473f882001-02-23 17:55:21 +00005264 return(prop);
5265 }
5266 prop = prop->next;
5267 }
5268 prop = xmlNewProp(node, name, value);
5269 return(prop);
5270}
5271
5272/**
Daniel Veillard75bea542001-05-11 17:41:21 +00005273 * xmlUnsetProp:
5274 * @node: the node
5275 * @name: the attribute name
5276 *
5277 * Remove an attribute carried by a node.
5278 * Returns 0 if successful, -1 if not found
5279 */
5280int
5281xmlUnsetProp(xmlNodePtr node, const xmlChar *name) {
5282 xmlAttrPtr prop = node->properties, prev = NULL;;
5283
5284 if ((node == NULL) || (name == NULL))
5285 return(-1);
5286 while (prop != NULL) {
5287 if ((xmlStrEqual(prop->name, name)) &&
5288 (prop->ns == NULL)) {
5289 if (prev == NULL)
5290 node->properties = prop->next;
5291 else
5292 prev->next = prop->next;
5293 xmlFreeProp(prop);
5294 return(0);
5295 }
5296 prev = prop;
5297 prop = prop->next;
5298 }
5299 return(-1);
5300}
5301
5302/**
Owen Taylor3473f882001-02-23 17:55:21 +00005303 * xmlSetNsProp:
5304 * @node: the node
5305 * @ns: the namespace definition
5306 * @name: the attribute name
5307 * @value: the attribute value
5308 *
5309 * Set (or reset) an attribute carried by a node.
5310 * The ns structure must be in scope, this is not checked.
5311 *
5312 * Returns the attribute pointer.
5313 */
5314xmlAttrPtr
5315xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
5316 const xmlChar *value) {
5317 xmlAttrPtr prop;
5318
5319 if ((node == NULL) || (name == NULL))
5320 return(NULL);
5321
5322 if (ns == NULL)
5323 return(xmlSetProp(node, name, value));
5324 if (ns->href == NULL)
5325 return(NULL);
5326 prop = node->properties;
5327
5328 while (prop != NULL) {
5329 /*
5330 * One need to have
5331 * - same attribute names
5332 * - and the attribute carrying that namespace
Owen Taylor3473f882001-02-23 17:55:21 +00005333 */
5334 if ((xmlStrEqual(prop->name, name)) &&
Daniel Veillarda57c26e2002-08-01 12:52:24 +00005335 (prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))) {
Owen Taylor3473f882001-02-23 17:55:21 +00005336 if (prop->children != NULL)
5337 xmlFreeNodeList(prop->children);
5338 prop->children = NULL;
5339 prop->last = NULL;
5340 prop->ns = ns;
5341 if (value != NULL) {
5342 xmlChar *buffer;
5343 xmlNodePtr tmp;
5344
5345 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
5346 prop->children = xmlStringGetNodeList(node->doc, buffer);
5347 prop->last = NULL;
5348 tmp = prop->children;
5349 while (tmp != NULL) {
5350 tmp->parent = (xmlNodePtr) prop;
5351 if (tmp->next == NULL)
5352 prop->last = tmp;
5353 tmp = tmp->next;
5354 }
5355 xmlFree(buffer);
5356 }
5357 return(prop);
5358 }
5359 prop = prop->next;
5360 }
5361 prop = xmlNewNsProp(node, ns, name, value);
5362 return(prop);
5363}
5364
5365/**
Daniel Veillard75bea542001-05-11 17:41:21 +00005366 * xmlUnsetNsProp:
5367 * @node: the node
5368 * @ns: the namespace definition
5369 * @name: the attribute name
5370 *
5371 * Remove an attribute carried by a node.
5372 * Returns 0 if successful, -1 if not found
5373 */
5374int
5375xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) {
5376 xmlAttrPtr prop = node->properties, prev = NULL;;
5377
5378 if ((node == NULL) || (name == NULL))
5379 return(-1);
5380 if (ns == NULL)
5381 return(xmlUnsetProp(node, name));
5382 if (ns->href == NULL)
5383 return(-1);
5384 while (prop != NULL) {
5385 if ((xmlStrEqual(prop->name, name)) &&
Daniel Veillard0bf29002002-08-01 12:54:11 +00005386 (prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))) {
Daniel Veillard75bea542001-05-11 17:41:21 +00005387 if (prev == NULL)
5388 node->properties = prop->next;
5389 else
5390 prev->next = prop->next;
5391 xmlFreeProp(prop);
5392 return(0);
5393 }
5394 prev = prop;
5395 prop = prop->next;
5396 }
5397 return(-1);
5398}
5399
5400/**
Owen Taylor3473f882001-02-23 17:55:21 +00005401 * xmlNodeIsText:
5402 * @node: the node
5403 *
5404 * Is this node a Text node ?
5405 * Returns 1 yes, 0 no
5406 */
5407int
5408xmlNodeIsText(xmlNodePtr node) {
5409 if (node == NULL) return(0);
5410
5411 if (node->type == XML_TEXT_NODE) return(1);
5412 return(0);
5413}
5414
5415/**
5416 * xmlIsBlankNode:
5417 * @node: the node
5418 *
5419 * Checks whether this node is an empty or whitespace only
5420 * (and possibly ignorable) text-node.
5421 *
5422 * Returns 1 yes, 0 no
5423 */
5424int
5425xmlIsBlankNode(xmlNodePtr node) {
5426 const xmlChar *cur;
5427 if (node == NULL) return(0);
5428
Daniel Veillard7db37732001-07-12 01:20:08 +00005429 if ((node->type != XML_TEXT_NODE) &&
5430 (node->type != XML_CDATA_SECTION_NODE))
5431 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005432 if (node->content == NULL) return(1);
Owen Taylor3473f882001-02-23 17:55:21 +00005433 cur = node->content;
Owen Taylor3473f882001-02-23 17:55:21 +00005434 while (*cur != 0) {
5435 if (!IS_BLANK(*cur)) return(0);
5436 cur++;
5437 }
5438
5439 return(1);
5440}
5441
5442/**
5443 * xmlTextConcat:
5444 * @node: the node
5445 * @content: the content
Daniel Veillard60087f32001-10-10 09:45:09 +00005446 * @len: @content length
Owen Taylor3473f882001-02-23 17:55:21 +00005447 *
5448 * Concat the given string at the end of the existing node content
5449 */
5450
5451void
5452xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) {
5453 if (node == NULL) return;
5454
5455 if ((node->type != XML_TEXT_NODE) &&
5456 (node->type != XML_CDATA_SECTION_NODE)) {
5457#ifdef DEBUG_TREE
5458 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00005459 "xmlTextConcat: node is not text nor CDATA\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005460#endif
5461 return;
5462 }
Owen Taylor3473f882001-02-23 17:55:21 +00005463 node->content = xmlStrncat(node->content, content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00005464}
5465
5466/************************************************************************
5467 * *
5468 * Output : to a FILE or in memory *
5469 * *
5470 ************************************************************************/
5471
Owen Taylor3473f882001-02-23 17:55:21 +00005472/**
5473 * xmlBufferCreate:
5474 *
5475 * routine to create an XML buffer.
5476 * returns the new structure.
5477 */
5478xmlBufferPtr
5479xmlBufferCreate(void) {
5480 xmlBufferPtr ret;
5481
5482 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
5483 if (ret == NULL) {
5484 xmlGenericError(xmlGenericErrorContext,
5485 "xmlBufferCreate : out of memory!\n");
5486 return(NULL);
5487 }
5488 ret->use = 0;
Daniel Veillarde356c282001-03-10 12:32:04 +00005489 ret->size = xmlDefaultBufferSize;
Owen Taylor3473f882001-02-23 17:55:21 +00005490 ret->alloc = xmlBufferAllocScheme;
5491 ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar));
5492 if (ret->content == NULL) {
5493 xmlGenericError(xmlGenericErrorContext,
5494 "xmlBufferCreate : out of memory!\n");
5495 xmlFree(ret);
5496 return(NULL);
5497 }
5498 ret->content[0] = 0;
5499 return(ret);
5500}
5501
5502/**
5503 * xmlBufferCreateSize:
5504 * @size: initial size of buffer
5505 *
5506 * routine to create an XML buffer.
5507 * returns the new structure.
5508 */
5509xmlBufferPtr
5510xmlBufferCreateSize(size_t size) {
5511 xmlBufferPtr ret;
5512
5513 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
5514 if (ret == NULL) {
5515 xmlGenericError(xmlGenericErrorContext,
5516 "xmlBufferCreate : out of memory!\n");
5517 return(NULL);
5518 }
5519 ret->use = 0;
5520 ret->alloc = xmlBufferAllocScheme;
5521 ret->size = (size ? size+2 : 0); /* +1 for ending null */
5522 if (ret->size){
5523 ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar));
5524 if (ret->content == NULL) {
5525 xmlGenericError(xmlGenericErrorContext,
5526 "xmlBufferCreate : out of memory!\n");
5527 xmlFree(ret);
5528 return(NULL);
5529 }
5530 ret->content[0] = 0;
5531 } else
5532 ret->content = NULL;
5533 return(ret);
5534}
5535
5536/**
5537 * xmlBufferSetAllocationScheme:
Daniel Veillardbd9afb52002-09-25 22:25:35 +00005538 * @buf: the buffer to tune
Owen Taylor3473f882001-02-23 17:55:21 +00005539 * @scheme: allocation scheme to use
5540 *
5541 * Sets the allocation scheme for this buffer
5542 */
5543void
5544xmlBufferSetAllocationScheme(xmlBufferPtr buf,
5545 xmlBufferAllocationScheme scheme) {
5546 if (buf == NULL) {
5547#ifdef DEBUG_BUFFER
5548 xmlGenericError(xmlGenericErrorContext,
5549 "xmlBufferSetAllocationScheme: buf == NULL\n");
5550#endif
5551 return;
5552 }
5553
5554 buf->alloc = scheme;
5555}
5556
5557/**
5558 * xmlBufferFree:
5559 * @buf: the buffer to free
5560 *
Daniel Veillard9d06d302002-01-22 18:15:52 +00005561 * Frees an XML buffer. It frees both the content and the structure which
5562 * encapsulate it.
Owen Taylor3473f882001-02-23 17:55:21 +00005563 */
5564void
5565xmlBufferFree(xmlBufferPtr buf) {
5566 if (buf == NULL) {
5567#ifdef DEBUG_BUFFER
5568 xmlGenericError(xmlGenericErrorContext,
5569 "xmlBufferFree: buf == NULL\n");
5570#endif
5571 return;
5572 }
Daniel Veillard561b7f82002-03-20 21:55:57 +00005573 if (buf->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005574 xmlFree(buf->content);
5575 }
Owen Taylor3473f882001-02-23 17:55:21 +00005576 xmlFree(buf);
5577}
5578
5579/**
5580 * xmlBufferEmpty:
5581 * @buf: the buffer
5582 *
5583 * empty a buffer.
5584 */
5585void
5586xmlBufferEmpty(xmlBufferPtr buf) {
5587 if (buf->content == NULL) return;
5588 buf->use = 0;
Daniel Veillard92ad2102001-03-27 12:47:33 +00005589 memset(buf->content, 0, buf->size);
Owen Taylor3473f882001-02-23 17:55:21 +00005590}
5591
5592/**
5593 * xmlBufferShrink:
5594 * @buf: the buffer to dump
5595 * @len: the number of xmlChar to remove
5596 *
5597 * Remove the beginning of an XML buffer.
5598 *
Daniel Veillardd1640922001-12-17 15:30:10 +00005599 * Returns the number of #xmlChar removed, or -1 in case of failure.
Owen Taylor3473f882001-02-23 17:55:21 +00005600 */
5601int
5602xmlBufferShrink(xmlBufferPtr buf, unsigned int len) {
5603 if (len == 0) return(0);
5604 if (len > buf->use) return(-1);
5605
5606 buf->use -= len;
5607 memmove(buf->content, &buf->content[len], buf->use * sizeof(xmlChar));
5608
5609 buf->content[buf->use] = 0;
5610 return(len);
5611}
5612
5613/**
5614 * xmlBufferGrow:
5615 * @buf: the buffer
5616 * @len: the minimum free size to allocate
5617 *
5618 * Grow the available space of an XML buffer.
5619 *
5620 * Returns the new available space or -1 in case of error
5621 */
5622int
5623xmlBufferGrow(xmlBufferPtr buf, unsigned int len) {
5624 int size;
5625 xmlChar *newbuf;
5626
5627 if (len + buf->use < buf->size) return(0);
5628
5629 size = buf->use + len + 100;
5630
5631 newbuf = (xmlChar *) xmlRealloc(buf->content, size);
5632 if (newbuf == NULL) return(-1);
5633 buf->content = newbuf;
5634 buf->size = size;
5635 return(buf->size - buf->use);
5636}
5637
5638/**
5639 * xmlBufferDump:
5640 * @file: the file output
5641 * @buf: the buffer to dump
5642 *
5643 * Dumps an XML buffer to a FILE *.
Daniel Veillardd1640922001-12-17 15:30:10 +00005644 * Returns the number of #xmlChar written
Owen Taylor3473f882001-02-23 17:55:21 +00005645 */
5646int
5647xmlBufferDump(FILE *file, xmlBufferPtr buf) {
5648 int ret;
5649
5650 if (buf == NULL) {
5651#ifdef DEBUG_BUFFER
5652 xmlGenericError(xmlGenericErrorContext,
5653 "xmlBufferDump: buf == NULL\n");
5654#endif
5655 return(0);
5656 }
5657 if (buf->content == NULL) {
5658#ifdef DEBUG_BUFFER
5659 xmlGenericError(xmlGenericErrorContext,
5660 "xmlBufferDump: buf->content == NULL\n");
5661#endif
5662 return(0);
5663 }
Daniel Veillardcd337f02001-11-22 18:20:37 +00005664 if (file == NULL)
5665 file = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00005666 ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
5667 return(ret);
5668}
5669
5670/**
5671 * xmlBufferContent:
5672 * @buf: the buffer
5673 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00005674 * Function to extract the content of a buffer
5675 *
Owen Taylor3473f882001-02-23 17:55:21 +00005676 * Returns the internal content
5677 */
5678
Daniel Veillard5e2dace2001-07-18 19:30:27 +00005679const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00005680xmlBufferContent(const xmlBufferPtr buf)
5681{
5682 if(!buf)
5683 return NULL;
5684
5685 return buf->content;
5686}
5687
5688/**
5689 * xmlBufferLength:
5690 * @buf: the buffer
5691 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00005692 * Function to get the length of a buffer
5693 *
Owen Taylor3473f882001-02-23 17:55:21 +00005694 * Returns the length of data in the internal content
5695 */
5696
5697int
5698xmlBufferLength(const xmlBufferPtr buf)
5699{
5700 if(!buf)
5701 return 0;
5702
5703 return buf->use;
5704}
5705
5706/**
5707 * xmlBufferResize:
5708 * @buf: the buffer to resize
5709 * @size: the desired size
5710 *
Daniel Veillardd1640922001-12-17 15:30:10 +00005711 * Resize a buffer to accommodate minimum size of @size.
Owen Taylor3473f882001-02-23 17:55:21 +00005712 *
5713 * Returns 0 in case of problems, 1 otherwise
5714 */
5715int
5716xmlBufferResize(xmlBufferPtr buf, unsigned int size)
5717{
5718 unsigned int newSize;
5719 xmlChar* rebuf = NULL;
5720
5721 /*take care of empty case*/
5722 newSize = (buf->size ? buf->size*2 : size);
5723
5724 /* Don't resize if we don't have to */
5725 if (size < buf->size)
5726 return 1;
5727
5728 /* figure out new size */
5729 switch (buf->alloc){
5730 case XML_BUFFER_ALLOC_DOUBLEIT:
5731 while (size > newSize) newSize *= 2;
5732 break;
5733 case XML_BUFFER_ALLOC_EXACT:
5734 newSize = size+10;
5735 break;
5736 default:
5737 newSize = size+10;
5738 break;
5739 }
5740
5741 if (buf->content == NULL)
5742 rebuf = (xmlChar *) xmlMalloc(newSize * sizeof(xmlChar));
5743 else
5744 rebuf = (xmlChar *) xmlRealloc(buf->content,
5745 newSize * sizeof(xmlChar));
5746 if (rebuf == NULL) {
5747 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00005748 "xmlBufferResize : out of memory!\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005749 return 0;
5750 }
5751 buf->content = rebuf;
5752 buf->size = newSize;
5753
5754 return 1;
5755}
5756
5757/**
5758 * xmlBufferAdd:
5759 * @buf: the buffer to dump
Daniel Veillardd1640922001-12-17 15:30:10 +00005760 * @str: the #xmlChar string
5761 * @len: the number of #xmlChar to add
Owen Taylor3473f882001-02-23 17:55:21 +00005762 *
Daniel Veillard60087f32001-10-10 09:45:09 +00005763 * Add a string range to an XML buffer. if len == -1, the length of
Owen Taylor3473f882001-02-23 17:55:21 +00005764 * str is recomputed.
5765 */
5766void
5767xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) {
5768 unsigned int needSize;
5769
5770 if (str == NULL) {
5771#ifdef DEBUG_BUFFER
5772 xmlGenericError(xmlGenericErrorContext,
5773 "xmlBufferAdd: str == NULL\n");
5774#endif
5775 return;
5776 }
5777 if (len < -1) {
5778#ifdef DEBUG_BUFFER
5779 xmlGenericError(xmlGenericErrorContext,
5780 "xmlBufferAdd: len < 0\n");
5781#endif
5782 return;
5783 }
5784 if (len == 0) return;
5785
5786 if (len < 0)
5787 len = xmlStrlen(str);
5788
5789 if (len <= 0) return;
5790
5791 needSize = buf->use + len + 2;
5792 if (needSize > buf->size){
5793 if (!xmlBufferResize(buf, needSize)){
5794 xmlGenericError(xmlGenericErrorContext,
5795 "xmlBufferAdd : out of memory!\n");
5796 return;
5797 }
5798 }
5799
5800 memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
5801 buf->use += len;
5802 buf->content[buf->use] = 0;
5803}
5804
5805/**
5806 * xmlBufferAddHead:
5807 * @buf: the buffer
Daniel Veillardd1640922001-12-17 15:30:10 +00005808 * @str: the #xmlChar string
5809 * @len: the number of #xmlChar to add
Owen Taylor3473f882001-02-23 17:55:21 +00005810 *
5811 * Add a string range to the beginning of an XML buffer.
Daniel Veillard60087f32001-10-10 09:45:09 +00005812 * if len == -1, the length of @str is recomputed.
Owen Taylor3473f882001-02-23 17:55:21 +00005813 */
5814void
5815xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) {
5816 unsigned int needSize;
5817
5818 if (str == NULL) {
5819#ifdef DEBUG_BUFFER
5820 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00005821 "xmlBufferAddHead: str == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005822#endif
5823 return;
5824 }
5825 if (len < -1) {
5826#ifdef DEBUG_BUFFER
5827 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00005828 "xmlBufferAddHead: len < 0\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005829#endif
5830 return;
5831 }
5832 if (len == 0) return;
5833
5834 if (len < 0)
5835 len = xmlStrlen(str);
5836
5837 if (len <= 0) return;
5838
5839 needSize = buf->use + len + 2;
5840 if (needSize > buf->size){
5841 if (!xmlBufferResize(buf, needSize)){
5842 xmlGenericError(xmlGenericErrorContext,
5843 "xmlBufferAddHead : out of memory!\n");
5844 return;
5845 }
5846 }
5847
5848 memmove(&buf->content[len], &buf->content[0], buf->use * sizeof(xmlChar));
5849 memmove(&buf->content[0], str, len * sizeof(xmlChar));
5850 buf->use += len;
5851 buf->content[buf->use] = 0;
5852}
5853
5854/**
5855 * xmlBufferCat:
5856 * @buf: the buffer to dump
Daniel Veillardd1640922001-12-17 15:30:10 +00005857 * @str: the #xmlChar string
Owen Taylor3473f882001-02-23 17:55:21 +00005858 *
5859 * Append a zero terminated string to an XML buffer.
5860 */
5861void
5862xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) {
5863 if (str != NULL)
5864 xmlBufferAdd(buf, str, -1);
5865}
5866
5867/**
5868 * xmlBufferCCat:
5869 * @buf: the buffer to dump
5870 * @str: the C char string
5871 *
5872 * Append a zero terminated C string to an XML buffer.
5873 */
5874void
5875xmlBufferCCat(xmlBufferPtr buf, const char *str) {
5876 const char *cur;
5877
5878 if (str == NULL) {
5879#ifdef DEBUG_BUFFER
5880 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00005881 "xmlBufferCCat: str == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005882#endif
5883 return;
5884 }
5885 for (cur = str;*cur != 0;cur++) {
5886 if (buf->use + 10 >= buf->size) {
5887 if (!xmlBufferResize(buf, buf->use+10)){
5888 xmlGenericError(xmlGenericErrorContext,
5889 "xmlBufferCCat : out of memory!\n");
5890 return;
5891 }
5892 }
5893 buf->content[buf->use++] = *cur;
5894 }
5895 buf->content[buf->use] = 0;
5896}
5897
5898/**
5899 * xmlBufferWriteCHAR:
5900 * @buf: the XML buffer
5901 * @string: the string to add
5902 *
5903 * routine which manages and grows an output buffer. This one adds
5904 * xmlChars at the end of the buffer.
5905 */
5906void
Owen Taylor3473f882001-02-23 17:55:21 +00005907xmlBufferWriteCHAR
Owen Taylor3473f882001-02-23 17:55:21 +00005908(xmlBufferPtr buf, const xmlChar *string) {
5909 xmlBufferCat(buf, string);
5910}
5911
5912/**
5913 * xmlBufferWriteChar:
5914 * @buf: the XML buffer output
5915 * @string: the string to add
5916 *
5917 * routine which manage and grows an output buffer. This one add
5918 * C chars at the end of the array.
5919 */
5920void
5921xmlBufferWriteChar(xmlBufferPtr buf, const char *string) {
5922 xmlBufferCCat(buf, string);
5923}
5924
5925
5926/**
5927 * xmlBufferWriteQuotedString:
5928 * @buf: the XML buffer output
5929 * @string: the string to add
5930 *
5931 * routine which manage and grows an output buffer. This one writes
Daniel Veillardd1640922001-12-17 15:30:10 +00005932 * a quoted or double quoted #xmlChar string, checking first if it holds
Owen Taylor3473f882001-02-23 17:55:21 +00005933 * quote or double-quotes internally
5934 */
5935void
5936xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) {
5937 if (xmlStrchr(string, '"')) {
5938 if (xmlStrchr(string, '\'')) {
5939#ifdef DEBUG_BUFFER
5940 xmlGenericError(xmlGenericErrorContext,
5941 "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n");
5942#endif
5943 }
5944 xmlBufferCCat(buf, "'");
5945 xmlBufferCat(buf, string);
5946 xmlBufferCCat(buf, "'");
5947 } else {
5948 xmlBufferCCat(buf, "\"");
5949 xmlBufferCat(buf, string);
5950 xmlBufferCCat(buf, "\"");
5951 }
5952}
5953
5954
5955/************************************************************************
5956 * *
5957 * Dumping XML tree content to a simple buffer *
5958 * *
5959 ************************************************************************/
5960
Owen Taylor3473f882001-02-23 17:55:21 +00005961/**
Daniel Veillarda6d05382002-02-13 13:07:41 +00005962 * xmlAttrSerializeContent:
5963 * @buf: the XML buffer output
5964 * @doc: the document
5965 * @attr: the attribute pointer
5966 *
5967 * Serialize the attribute in the buffer
5968 */
5969static void
Daniel Veillardebc4ca92002-11-27 11:43:05 +00005970xmlAttrSerializeContent(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr attr)
5971{
Daniel Veillarda6d05382002-02-13 13:07:41 +00005972 const xmlChar *cur, *base;
5973 xmlNodePtr children;
5974
5975 children = attr->children;
5976 while (children != NULL) {
Daniel Veillardebc4ca92002-11-27 11:43:05 +00005977 switch (children->type) {
5978 case XML_TEXT_NODE:
5979 base = cur = children->content;
5980 while (*cur != 0) {
5981 if (*cur == '\n') {
5982 if (base != cur)
5983 xmlBufferAdd(buf, base, cur - base);
5984 xmlBufferAdd(buf, BAD_CAST "&#10;", 5);
5985 cur++;
5986 base = cur;
Daniel Veillarda6d05382002-02-13 13:07:41 +00005987#if 0
Daniel Veillardebc4ca92002-11-27 11:43:05 +00005988 } else if (*cur == '\'') {
5989 if (base != cur)
5990 xmlBufferAdd(buf, base, cur - base);
5991 xmlBufferAdd(buf, BAD_CAST "&apos;", 6);
5992 cur++;
5993 base = cur;
Daniel Veillarda6d05382002-02-13 13:07:41 +00005994#endif
Daniel Veillardebc4ca92002-11-27 11:43:05 +00005995 } else if (*cur == '"') {
5996 if (base != cur)
5997 xmlBufferAdd(buf, base, cur - base);
5998 xmlBufferAdd(buf, BAD_CAST "&quot;", 6);
5999 cur++;
6000 base = cur;
6001 } else if (*cur == '<') {
6002 if (base != cur)
6003 xmlBufferAdd(buf, base, cur - base);
6004 xmlBufferAdd(buf, BAD_CAST "&lt;", 4);
6005 cur++;
6006 base = cur;
6007 } else if (*cur == '>') {
6008 if (base != cur)
6009 xmlBufferAdd(buf, base, cur - base);
6010 xmlBufferAdd(buf, BAD_CAST "&gt;", 4);
6011 cur++;
6012 base = cur;
6013 } else if (*cur == '&') {
6014 if (base != cur)
6015 xmlBufferAdd(buf, base, cur - base);
6016 xmlBufferAdd(buf, BAD_CAST "&amp;", 5);
6017 cur++;
6018 base = cur;
6019 } else if ((*cur >= 0x80) && ((doc == NULL) ||
6020 (doc->encoding ==
6021 NULL))) {
6022 /*
6023 * We assume we have UTF-8 content.
6024 */
6025 char tmp[10];
6026 int val = 0, l = 1;
Daniel Veillarda6d05382002-02-13 13:07:41 +00006027
Daniel Veillardebc4ca92002-11-27 11:43:05 +00006028 if (base != cur)
6029 xmlBufferAdd(buf, base, cur - base);
6030 if (*cur < 0xC0) {
6031 xmlGenericError(xmlGenericErrorContext,
Daniel Veillarda6d05382002-02-13 13:07:41 +00006032 "xmlAttrSerializeContent : input not UTF-8\n");
Daniel Veillardebc4ca92002-11-27 11:43:05 +00006033 if (doc != NULL)
6034 doc->encoding =
6035 xmlStrdup(BAD_CAST "ISO-8859-1");
6036 snprintf(tmp, sizeof(tmp), "&#%d;", *cur);
6037 tmp[sizeof(tmp) - 1] = 0;
6038 xmlBufferAdd(buf, (xmlChar *) tmp, -1);
6039 cur++;
6040 base = cur;
6041 continue;
6042 } else if (*cur < 0xE0) {
6043 val = (cur[0]) & 0x1F;
6044 val <<= 6;
6045 val |= (cur[1]) & 0x3F;
6046 l = 2;
6047 } else if (*cur < 0xF0) {
6048 val = (cur[0]) & 0x0F;
6049 val <<= 6;
6050 val |= (cur[1]) & 0x3F;
6051 val <<= 6;
6052 val |= (cur[2]) & 0x3F;
6053 l = 3;
6054 } else if (*cur < 0xF8) {
6055 val = (cur[0]) & 0x07;
6056 val <<= 6;
6057 val |= (cur[1]) & 0x3F;
6058 val <<= 6;
6059 val |= (cur[2]) & 0x3F;
6060 val <<= 6;
6061 val |= (cur[3]) & 0x3F;
6062 l = 4;
6063 }
6064 if ((l == 1) || (!IS_CHAR(val))) {
6065 xmlGenericError(xmlGenericErrorContext,
6066 "xmlAttrSerializeContent : char out of range\n");
6067 if (doc != NULL)
6068 doc->encoding =
6069 xmlStrdup(BAD_CAST "ISO-8859-1");
6070 snprintf(tmp, sizeof(tmp), "&#%d;", *cur);
6071 tmp[sizeof(tmp) - 1] = 0;
6072 xmlBufferAdd(buf, (xmlChar *) tmp, -1);
6073 cur++;
6074 base = cur;
6075 continue;
6076 }
6077 /*
6078 * We could do multiple things here. Just save
6079 * as a char ref
6080 */
6081 snprintf(tmp, sizeof(tmp), "&#x%X;", val);
6082 tmp[sizeof(tmp) - 1] = 0;
6083 xmlBufferAdd(buf, (xmlChar *) tmp, -1);
6084 cur += l;
6085 base = cur;
6086 } else {
6087 cur++;
6088 }
6089 }
6090 if (base != cur)
6091 xmlBufferAdd(buf, base, cur - base);
6092 break;
6093 case XML_ENTITY_REF_NODE:
6094 xmlBufferAdd(buf, BAD_CAST "&", 1);
6095 xmlBufferAdd(buf, children->name,
6096 xmlStrlen(children->name));
6097 xmlBufferAdd(buf, BAD_CAST ";", 1);
6098 break;
6099 default:
6100 /* should not happen unless we have a badly built tree */
6101 break;
6102 }
6103 children = children->next;
Owen Taylor3473f882001-02-23 17:55:21 +00006104 }
6105}
6106
6107/**
6108 * xmlNodeDump:
6109 * @buf: the XML buffer output
6110 * @doc: the document
6111 * @cur: the current node
6112 * @level: the imbrication level for indenting
6113 * @format: is formatting allowed
6114 *
6115 * Dump an XML node, recursive behaviour,children are printed too.
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00006116 * Note that format = 1 provide node indenting only if xmlIndentTreeOutput = 1
6117 * or xmlKeepBlanksDefault(0) was called
Daniel Veillardebc4ca92002-11-27 11:43:05 +00006118 *
6119 * Returns the number of bytes written to the buffer or -1 in case of error
Owen Taylor3473f882001-02-23 17:55:21 +00006120 */
Daniel Veillardebc4ca92002-11-27 11:43:05 +00006121int
Owen Taylor3473f882001-02-23 17:55:21 +00006122xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
Daniel Veillardebc4ca92002-11-27 11:43:05 +00006123 int format)
6124{
6125 unsigned int use;
6126 int ret;
6127 xmlOutputBufferPtr outbuf;
Owen Taylor3473f882001-02-23 17:55:21 +00006128
6129 if (cur == NULL) {
6130#ifdef DEBUG_TREE
6131 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardebc4ca92002-11-27 11:43:05 +00006132 "xmlNodeDump : node == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006133#endif
Daniel Veillardebc4ca92002-11-27 11:43:05 +00006134 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006135 }
Daniel Veillardebc4ca92002-11-27 11:43:05 +00006136 if (buf == NULL) {
6137#ifdef DEBUG_TREE
6138 xmlGenericError(xmlGenericErrorContext,
6139 "xmlNodeDump : buf == NULL\n");
6140#endif
6141 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006142 }
Daniel Veillardebc4ca92002-11-27 11:43:05 +00006143 outbuf = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer));
6144 if (outbuf == NULL) {
6145 xmlGenericError(xmlGenericErrorContext,
6146 "xmlNodeDump: out of memory!\n");
6147 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006148 }
Daniel Veillardebc4ca92002-11-27 11:43:05 +00006149 memset(outbuf, 0, (size_t) sizeof(xmlOutputBuffer));
6150 outbuf->buffer = buf;
6151 outbuf->encoder = NULL;
6152 outbuf->writecallback = NULL;
6153 outbuf->closecallback = NULL;
6154 outbuf->context = NULL;
6155 outbuf->written = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006156
Daniel Veillardebc4ca92002-11-27 11:43:05 +00006157 use = buf->use;
6158 xmlNodeDumpOutput(outbuf, doc, cur, level, format, NULL);
6159 xmlFree(outbuf);
6160 ret = buf->use - use;
6161 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00006162}
6163
6164/**
6165 * xmlElemDump:
6166 * @f: the FILE * for the output
6167 * @doc: the document
6168 * @cur: the current node
6169 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006170 * Dump an XML/HTML node, recursive behaviour, children are printed too.
Owen Taylor3473f882001-02-23 17:55:21 +00006171 */
6172void
Daniel Veillardebc4ca92002-11-27 11:43:05 +00006173xmlElemDump(FILE * f, xmlDocPtr doc, xmlNodePtr cur)
6174{
6175 xmlOutputBufferPtr outbuf;
Owen Taylor3473f882001-02-23 17:55:21 +00006176
6177 if (cur == NULL) {
6178#ifdef DEBUG_TREE
6179 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardebc4ca92002-11-27 11:43:05 +00006180 "xmlElemDump : cur == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006181#endif
Daniel Veillardebc4ca92002-11-27 11:43:05 +00006182 return;
Owen Taylor3473f882001-02-23 17:55:21 +00006183 }
Owen Taylor3473f882001-02-23 17:55:21 +00006184#ifdef DEBUG_TREE
Daniel Veillardd79bcd12001-06-21 22:07:42 +00006185 if (doc == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00006186 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardebc4ca92002-11-27 11:43:05 +00006187 "xmlElemDump : doc == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006188 }
Daniel Veillardd79bcd12001-06-21 22:07:42 +00006189#endif
Daniel Veillardebc4ca92002-11-27 11:43:05 +00006190
6191 outbuf = xmlOutputBufferCreateFile(f, NULL);
6192 if (outbuf == NULL)
6193 return;
6194 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006195#ifdef LIBXML_HTML_ENABLED
Daniel Veillardebc4ca92002-11-27 11:43:05 +00006196 htmlNodeDumpOutput(outbuf, doc, cur, NULL);
6197#else
6198 xmlGenericError(xmlGenericErrorContext,
6199 "HTML support not compiled in\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006200#endif /* LIBXML_HTML_ENABLED */
6201 } else
Daniel Veillardebc4ca92002-11-27 11:43:05 +00006202 xmlNodeDumpOutput(outbuf, doc, cur, 0, 1, NULL);
6203 xmlOutputBufferClose(outbuf);
Owen Taylor3473f882001-02-23 17:55:21 +00006204}
6205
6206/************************************************************************
6207 * *
6208 * Dumping XML tree content to an I/O output buffer *
6209 * *
6210 ************************************************************************/
6211
Owen Taylor3473f882001-02-23 17:55:21 +00006212static void
Daniel Veillardd5c2f922002-11-21 14:10:52 +00006213xhtmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
6214 int level, int format, const char *encoding);
6215static void
Owen Taylor3473f882001-02-23 17:55:21 +00006216xmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
6217 int level, int format, const char *encoding);
Daniel Veillardd5c2f922002-11-21 14:10:52 +00006218static void
6219xmlNodeDumpOutputInternal(xmlOutputBufferPtr buf, xmlDocPtr doc,
6220 xmlNodePtr cur, int level, int format, const char *encoding);
6221
Owen Taylor3473f882001-02-23 17:55:21 +00006222/**
6223 * xmlNsDumpOutput:
6224 * @buf: the XML buffer output
6225 * @cur: a namespace
6226 *
6227 * Dump a local Namespace definition.
6228 * Should be called in the context of attributes dumps.
6229 */
6230static void
6231xmlNsDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) {
6232 if (cur == NULL) {
6233#ifdef DEBUG_TREE
6234 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006235 "xmlNsDumpOutput : Ns == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006236#endif
6237 return;
6238 }
6239 if ((cur->type == XML_LOCAL_NAMESPACE) && (cur->href != NULL)) {
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00006240 if (xmlStrEqual(cur->prefix, BAD_CAST "xml"))
6241 return;
6242
Owen Taylor3473f882001-02-23 17:55:21 +00006243 /* Within the context of an element attributes */
6244 if (cur->prefix != NULL) {
6245 xmlOutputBufferWriteString(buf, " xmlns:");
6246 xmlOutputBufferWriteString(buf, (const char *)cur->prefix);
6247 } else
6248 xmlOutputBufferWriteString(buf, " xmlns");
6249 xmlOutputBufferWriteString(buf, "=");
6250 xmlBufferWriteQuotedString(buf->buffer, cur->href);
6251 }
6252}
6253
6254/**
6255 * xmlNsListDumpOutput:
6256 * @buf: the XML buffer output
6257 * @cur: the first namespace
6258 *
6259 * Dump a list of local Namespace definitions.
6260 * Should be called in the context of attributes dumps.
6261 */
6262static void
6263xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) {
6264 while (cur != NULL) {
6265 xmlNsDumpOutput(buf, cur);
6266 cur = cur->next;
6267 }
6268}
6269
6270/**
6271 * xmlDtdDumpOutput:
6272 * @buf: the XML buffer output
6273 * @doc: the document
6274 * @encoding: an optional encoding string
6275 *
6276 * Dump the XML document DTD, if any.
6277 */
6278static void
6279xmlDtdDumpOutput(xmlOutputBufferPtr buf, xmlDtdPtr dtd, const char *encoding) {
6280 if (dtd == NULL) {
6281#ifdef DEBUG_TREE
6282 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006283 "xmlDtdDumpOutput : no internal subset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006284#endif
6285 return;
6286 }
6287 xmlOutputBufferWriteString(buf, "<!DOCTYPE ");
6288 xmlOutputBufferWriteString(buf, (const char *)dtd->name);
6289 if (dtd->ExternalID != NULL) {
6290 xmlOutputBufferWriteString(buf, " PUBLIC ");
6291 xmlBufferWriteQuotedString(buf->buffer, dtd->ExternalID);
6292 xmlOutputBufferWriteString(buf, " ");
6293 xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID);
6294 } else if (dtd->SystemID != NULL) {
6295 xmlOutputBufferWriteString(buf, " SYSTEM ");
6296 xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID);
6297 }
6298 if ((dtd->entities == NULL) && (dtd->elements == NULL) &&
6299 (dtd->attributes == NULL) && (dtd->notations == NULL)) {
6300 xmlOutputBufferWriteString(buf, ">");
6301 return;
6302 }
6303 xmlOutputBufferWriteString(buf, " [\n");
6304 xmlNodeListDumpOutput(buf, dtd->doc, dtd->children, -1, 0, encoding);
6305 xmlOutputBufferWriteString(buf, "]>");
6306}
6307
6308/**
6309 * xmlAttrDumpOutput:
6310 * @buf: the XML buffer output
6311 * @doc: the document
6312 * @cur: the attribute pointer
6313 * @encoding: an optional encoding string
6314 *
6315 * Dump an XML attribute
6316 */
6317static void
6318xmlAttrDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00006319 const char *encoding ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00006320 if (cur == NULL) {
6321#ifdef DEBUG_TREE
6322 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006323 "xmlAttrDumpOutput : property == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006324#endif
6325 return;
6326 }
6327 xmlOutputBufferWriteString(buf, " ");
6328 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
6329 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
6330 xmlOutputBufferWriteString(buf, ":");
6331 }
6332 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillarda6d05382002-02-13 13:07:41 +00006333 xmlOutputBufferWriteString(buf, "=\"");
6334 xmlAttrSerializeContent(buf->buffer, doc, cur);
6335 xmlOutputBufferWriteString(buf, "\"");
Owen Taylor3473f882001-02-23 17:55:21 +00006336}
6337
6338/**
6339 * xmlAttrListDumpOutput:
6340 * @buf: the XML buffer output
6341 * @doc: the document
6342 * @cur: the first attribute pointer
6343 * @encoding: an optional encoding string
6344 *
6345 * Dump a list of XML attributes
6346 */
6347static void
6348xmlAttrListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
6349 xmlAttrPtr cur, const char *encoding) {
6350 if (cur == NULL) {
6351#ifdef DEBUG_TREE
6352 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006353 "xmlAttrListDumpOutput : property == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006354#endif
6355 return;
6356 }
6357 while (cur != NULL) {
6358 xmlAttrDumpOutput(buf, doc, cur, encoding);
6359 cur = cur->next;
6360 }
6361}
6362
6363
6364
6365/**
6366 * xmlNodeListDumpOutput:
6367 * @buf: the XML buffer output
6368 * @doc: the document
6369 * @cur: the first node
6370 * @level: the imbrication level for indenting
6371 * @format: is formatting allowed
6372 * @encoding: an optional encoding string
6373 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006374 * Dump an XML node list, recursive behaviour, children are printed too.
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00006375 * Note that format = 1 provide node indenting only if xmlIndentTreeOutput = 1
6376 * or xmlKeepBlanksDefault(0) was called
Owen Taylor3473f882001-02-23 17:55:21 +00006377 */
6378static void
6379xmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
6380 xmlNodePtr cur, int level, int format, const char *encoding) {
6381 int i;
6382
6383 if (cur == NULL) {
6384#ifdef DEBUG_TREE
6385 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006386 "xmlNodeListDumpOutput : node == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006387#endif
6388 return;
6389 }
6390 while (cur != NULL) {
6391 if ((format) && (xmlIndentTreeOutput) &&
6392 (cur->type == XML_ELEMENT_NODE))
6393 for (i = 0;i < level;i++)
Aleksey Sanin23002562002-05-24 07:18:40 +00006394 xmlOutputBufferWriteString(buf, xmlTreeIndentString);
Daniel Veillardd5c2f922002-11-21 14:10:52 +00006395 xmlNodeDumpOutputInternal(buf, doc, cur, level, format, encoding);
Owen Taylor3473f882001-02-23 17:55:21 +00006396 if (format) {
6397 xmlOutputBufferWriteString(buf, "\n");
6398 }
6399 cur = cur->next;
6400 }
6401}
6402
6403/**
Daniel Veillardd5c2f922002-11-21 14:10:52 +00006404 * xmlNodeDumpOutputInternal:
Owen Taylor3473f882001-02-23 17:55:21 +00006405 * @buf: the XML buffer output
6406 * @doc: the document
6407 * @cur: the current node
6408 * @level: the imbrication level for indenting
6409 * @format: is formatting allowed
6410 * @encoding: an optional encoding string
6411 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006412 * Dump an XML node, recursive behaviour, children are printed too.
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00006413 * Note that format = 1 provide node indenting only if xmlIndentTreeOutput = 1
6414 * or xmlKeepBlanksDefault(0) was called
Owen Taylor3473f882001-02-23 17:55:21 +00006415 */
Daniel Veillardd5c2f922002-11-21 14:10:52 +00006416static void
6417xmlNodeDumpOutputInternal(xmlOutputBufferPtr buf, xmlDocPtr doc,
6418 xmlNodePtr cur, int level, int format, const char *encoding) {
Owen Taylor3473f882001-02-23 17:55:21 +00006419 int i;
6420 xmlNodePtr tmp;
6421
6422 if (cur == NULL) {
6423#ifdef DEBUG_TREE
6424 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006425 "xmlNodeDumpOutput : node == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006426#endif
6427 return;
6428 }
6429 if (cur->type == XML_XINCLUDE_START)
6430 return;
6431 if (cur->type == XML_XINCLUDE_END)
6432 return;
6433 if (cur->type == XML_DTD_NODE) {
6434 xmlDtdDumpOutput(buf, (xmlDtdPtr) cur, encoding);
6435 return;
6436 }
6437 if (cur->type == XML_ELEMENT_DECL) {
6438 xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur);
6439 return;
6440 }
6441 if (cur->type == XML_ATTRIBUTE_DECL) {
6442 xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur);
6443 return;
6444 }
6445 if (cur->type == XML_ENTITY_DECL) {
6446 xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur);
6447 return;
6448 }
6449 if (cur->type == XML_TEXT_NODE) {
6450 if (cur->content != NULL) {
6451 if ((cur->name == xmlStringText) ||
6452 (cur->name != xmlStringTextNoenc)) {
6453 xmlChar *buffer;
6454
Owen Taylor3473f882001-02-23 17:55:21 +00006455 if (encoding == NULL)
6456 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
6457 else
6458 buffer = xmlEncodeSpecialChars(doc, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00006459 if (buffer != NULL) {
6460 xmlOutputBufferWriteString(buf, (const char *)buffer);
6461 xmlFree(buffer);
6462 }
6463 } else {
6464 /*
6465 * Disable escaping, needed for XSLT
6466 */
Owen Taylor3473f882001-02-23 17:55:21 +00006467 xmlOutputBufferWriteString(buf, (const char *) cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00006468 }
6469 }
6470
6471 return;
6472 }
6473 if (cur->type == XML_PI_NODE) {
6474 if (cur->content != NULL) {
6475 xmlOutputBufferWriteString(buf, "<?");
6476 xmlOutputBufferWriteString(buf, (const char *)cur->name);
6477 if (cur->content != NULL) {
6478 xmlOutputBufferWriteString(buf, " ");
Owen Taylor3473f882001-02-23 17:55:21 +00006479 xmlOutputBufferWriteString(buf, (const char *)cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00006480 }
6481 xmlOutputBufferWriteString(buf, "?>");
6482 } else {
6483 xmlOutputBufferWriteString(buf, "<?");
6484 xmlOutputBufferWriteString(buf, (const char *)cur->name);
6485 xmlOutputBufferWriteString(buf, "?>");
6486 }
6487 return;
6488 }
6489 if (cur->type == XML_COMMENT_NODE) {
6490 if (cur->content != NULL) {
6491 xmlOutputBufferWriteString(buf, "<!--");
Owen Taylor3473f882001-02-23 17:55:21 +00006492 xmlOutputBufferWriteString(buf, (const char *)cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00006493 xmlOutputBufferWriteString(buf, "-->");
6494 }
6495 return;
6496 }
6497 if (cur->type == XML_ENTITY_REF_NODE) {
6498 xmlOutputBufferWriteString(buf, "&");
6499 xmlOutputBufferWriteString(buf, (const char *)cur->name);
6500 xmlOutputBufferWriteString(buf, ";");
6501 return;
6502 }
6503 if (cur->type == XML_CDATA_SECTION_NODE) {
6504 xmlOutputBufferWriteString(buf, "<![CDATA[");
6505 if (cur->content != NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00006506 xmlOutputBufferWriteString(buf, (const char *)cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00006507 xmlOutputBufferWriteString(buf, "]]>");
6508 return;
6509 }
6510
6511 if (format == 1) {
6512 tmp = cur->children;
6513 while (tmp != NULL) {
6514 if ((tmp->type == XML_TEXT_NODE) ||
6515 (tmp->type == XML_ENTITY_REF_NODE)) {
6516 format = 0;
6517 break;
6518 }
6519 tmp = tmp->next;
6520 }
6521 }
6522 xmlOutputBufferWriteString(buf, "<");
6523 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
6524 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
6525 xmlOutputBufferWriteString(buf, ":");
6526 }
6527
6528 xmlOutputBufferWriteString(buf, (const char *)cur->name);
6529 if (cur->nsDef)
6530 xmlNsListDumpOutput(buf, cur->nsDef);
6531 if (cur->properties != NULL)
6532 xmlAttrListDumpOutput(buf, doc, cur->properties, encoding);
6533
Daniel Veillard7db37732001-07-12 01:20:08 +00006534 if (((cur->type == XML_ELEMENT_NODE) || (cur->content == NULL)) &&
6535 (cur->children == NULL) && (!xmlSaveNoEmptyTags)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006536 xmlOutputBufferWriteString(buf, "/>");
6537 return;
6538 }
6539 xmlOutputBufferWriteString(buf, ">");
Daniel Veillard7db37732001-07-12 01:20:08 +00006540 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006541 xmlChar *buffer;
6542
Owen Taylor3473f882001-02-23 17:55:21 +00006543 if (encoding == NULL)
6544 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
6545 else
6546 buffer = xmlEncodeSpecialChars(doc, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00006547 if (buffer != NULL) {
6548 xmlOutputBufferWriteString(buf, (const char *)buffer);
6549 xmlFree(buffer);
6550 }
6551 }
6552 if (cur->children != NULL) {
6553 if (format) xmlOutputBufferWriteString(buf, "\n");
6554 xmlNodeListDumpOutput(buf, doc, cur->children,
6555 (level >= 0?level+1:-1), format, encoding);
6556 if ((xmlIndentTreeOutput) && (format))
6557 for (i = 0;i < level;i++)
Aleksey Sanin23002562002-05-24 07:18:40 +00006558 xmlOutputBufferWriteString(buf, xmlTreeIndentString);
Owen Taylor3473f882001-02-23 17:55:21 +00006559 }
6560 xmlOutputBufferWriteString(buf, "</");
6561 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
6562 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
6563 xmlOutputBufferWriteString(buf, ":");
6564 }
6565
6566 xmlOutputBufferWriteString(buf, (const char *)cur->name);
6567 xmlOutputBufferWriteString(buf, ">");
6568}
6569
6570/**
Daniel Veillardd5c2f922002-11-21 14:10:52 +00006571 * xmlNodeDumpOutput:
6572 * @buf: the XML buffer output
6573 * @doc: the document
6574 * @cur: the current node
6575 * @level: the imbrication level for indenting
6576 * @format: is formatting allowed
6577 * @encoding: an optional encoding string
6578 *
6579 * Dump an XML node, recursive behaviour, children are printed too.
6580 * Note that format = 1 provide node indenting only if xmlIndentTreeOutput = 1
6581 * or xmlKeepBlanksDefault(0) was called
6582 */
6583void
6584xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
Daniel Veillardebc4ca92002-11-27 11:43:05 +00006585 int level, int format, const char *encoding)
6586{
Daniel Veillardd5c2f922002-11-21 14:10:52 +00006587#ifdef LIBXML_HTML_ENABLED
6588 xmlDtdPtr dtd;
6589 int is_xhtml = 0;
6590
6591 dtd = xmlGetIntSubset(doc);
6592 if (dtd != NULL) {
Daniel Veillardebc4ca92002-11-27 11:43:05 +00006593 is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
6594 if (is_xhtml < 0)
6595 is_xhtml = 0;
6596 if ((is_xhtml) && (cur->parent == (xmlNodePtr) doc) &&
6597 (cur->type == XML_ELEMENT_NODE) &&
6598 (xmlStrEqual(cur->name, BAD_CAST "html"))) {
6599 if (encoding != NULL)
6600 htmlSetMetaEncoding((htmlDocPtr) cur,
6601 (const xmlChar *) encoding);
6602 else
6603 htmlSetMetaEncoding((htmlDocPtr) cur, BAD_CAST "UTF-8");
6604 }
Daniel Veillardd5c2f922002-11-21 14:10:52 +00006605 }
6606
6607 if (is_xhtml)
Daniel Veillardebc4ca92002-11-27 11:43:05 +00006608 xhtmlNodeDumpOutput(buf, doc, cur, level, format, encoding);
Daniel Veillardd5c2f922002-11-21 14:10:52 +00006609 else
6610#endif
Daniel Veillardebc4ca92002-11-27 11:43:05 +00006611 xmlNodeDumpOutputInternal(buf, doc, cur, level, format, encoding);
Daniel Veillardd5c2f922002-11-21 14:10:52 +00006612}
6613
6614/**
Owen Taylor3473f882001-02-23 17:55:21 +00006615 * xmlDocContentDumpOutput:
6616 * @buf: the XML buffer output
6617 * @cur: the document
6618 * @encoding: an optional encoding string
6619 * @format: should formatting spaces been added
6620 *
6621 * Dump an XML document.
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00006622 * Note that format = 1 provide node indenting only if xmlIndentTreeOutput = 1
6623 * or xmlKeepBlanksDefault(0) was called
Owen Taylor3473f882001-02-23 17:55:21 +00006624 */
6625static void
6626xmlDocContentDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr cur,
6627 const char *encoding, int format) {
Daniel Veillardd5c2f922002-11-21 14:10:52 +00006628#ifdef LIBXML_HTML_ENABLED
6629 xmlDtdPtr dtd;
6630 int is_xhtml = 0;
6631#endif
6632
Owen Taylor3473f882001-02-23 17:55:21 +00006633 xmlOutputBufferWriteString(buf, "<?xml version=");
6634 if (cur->version != NULL)
6635 xmlBufferWriteQuotedString(buf->buffer, cur->version);
6636 else
6637 xmlOutputBufferWriteString(buf, "\"1.0\"");
6638 if (encoding == NULL) {
6639 if (cur->encoding != NULL)
6640 encoding = (const char *) cur->encoding;
6641 else if (cur->charset != XML_CHAR_ENCODING_UTF8)
6642 encoding = xmlGetCharEncodingName((xmlCharEncoding) cur->charset);
6643 }
6644 if (encoding != NULL) {
6645 xmlOutputBufferWriteString(buf, " encoding=");
6646 xmlBufferWriteQuotedString(buf->buffer, (xmlChar *) encoding);
6647 }
6648 switch (cur->standalone) {
6649 case 0:
6650 xmlOutputBufferWriteString(buf, " standalone=\"no\"");
6651 break;
6652 case 1:
6653 xmlOutputBufferWriteString(buf, " standalone=\"yes\"");
6654 break;
6655 }
6656 xmlOutputBufferWriteString(buf, "?>\n");
Daniel Veillardd5c2f922002-11-21 14:10:52 +00006657
6658#ifdef LIBXML_HTML_ENABLED
6659 dtd = xmlGetIntSubset(cur);
6660 if (dtd != NULL) {
6661 is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
6662 if (is_xhtml < 0) is_xhtml = 0;
6663 }
6664 if (is_xhtml) {
6665 if (encoding != NULL)
6666 htmlSetMetaEncoding(cur, (const xmlChar *) encoding);
6667 else
6668 htmlSetMetaEncoding(cur, BAD_CAST "UTF-8");
6669 }
6670#endif
Owen Taylor3473f882001-02-23 17:55:21 +00006671 if (cur->children != NULL) {
6672 xmlNodePtr child = cur->children;
6673
6674 while (child != NULL) {
Daniel Veillardd5c2f922002-11-21 14:10:52 +00006675#ifdef LIBXML_HTML_ENABLED
6676 if (is_xhtml)
6677 xhtmlNodeDumpOutput(buf, cur, child, 0, format, encoding);
6678 else
6679#endif
6680 xmlNodeDumpOutputInternal(buf, cur, child, 0, format, encoding);
Owen Taylor3473f882001-02-23 17:55:21 +00006681 xmlOutputBufferWriteString(buf, "\n");
6682 child = child->next;
6683 }
6684 }
6685}
6686
Daniel Veillardd5c2f922002-11-21 14:10:52 +00006687#ifdef LIBXML_HTML_ENABLED
6688/************************************************************************
6689 * *
6690 * Functions specific to XHTML serialization *
6691 * *
6692 ************************************************************************/
6693
6694#define XHTML_STRICT_PUBLIC_ID BAD_CAST \
6695 "-//W3C//DTD XHTML 1.0 Strict//EN"
6696#define XHTML_STRICT_SYSTEM_ID BAD_CAST \
6697 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
6698#define XHTML_FRAME_PUBLIC_ID BAD_CAST \
6699 "-//W3C//DTD XHTML 1.0 Frameset//EN"
6700#define XHTML_FRAME_SYSTEM_ID BAD_CAST \
6701 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"
6702#define XHTML_TRANS_PUBLIC_ID BAD_CAST \
6703 "-//W3C//DTD XHTML 1.0 Transitional//EN"
6704#define XHTML_TRANS_SYSTEM_ID BAD_CAST \
6705 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
6706
6707#define XHTML_NS_NAME BAD_CAST "http://www.w3.org/1999/xhtml"
6708/**
6709 * xmlIsXHTML:
6710 * @systemID: the system identifier
6711 * @publicID: the public identifier
6712 *
6713 * Try to find if the document correspond to an XHTML DTD
6714 *
6715 * Returns 1 if true, 0 if not and -1 in case of error
6716 */
6717int
6718xmlIsXHTML(const xmlChar *systemID, const xmlChar *publicID) {
6719 if ((systemID == NULL) && (publicID == NULL))
6720 return(-1);
6721 if (publicID != NULL) {
6722 if (xmlStrEqual(publicID, XHTML_STRICT_PUBLIC_ID)) return(1);
6723 if (xmlStrEqual(publicID, XHTML_FRAME_PUBLIC_ID)) return(1);
6724 if (xmlStrEqual(publicID, XHTML_TRANS_PUBLIC_ID)) return(1);
6725 }
6726 if (systemID != NULL) {
6727 if (xmlStrEqual(systemID, XHTML_STRICT_SYSTEM_ID)) return(1);
6728 if (xmlStrEqual(systemID, XHTML_FRAME_SYSTEM_ID)) return(1);
6729 if (xmlStrEqual(systemID, XHTML_TRANS_SYSTEM_ID)) return(1);
6730 }
6731 return(0);
6732}
6733
6734/**
6735 * xhtmlIsEmpty:
6736 * @node: the node
6737 *
6738 * Check if a node is an empty xhtml node
6739 *
6740 * Returns 1 if the node is an empty node, 0 if not and -1 in case of error
6741 */
6742static int
6743xhtmlIsEmpty(xmlNodePtr node) {
6744 if (node == NULL)
6745 return(-1);
6746 if (node->type != XML_ELEMENT_NODE)
6747 return(0);
6748 if ((node->ns != NULL) && (!xmlStrEqual(node->ns->href, XHTML_NS_NAME)))
6749 return(0);
6750 if (node->children != NULL)
6751 return(0);
6752 switch (node->name[0]) {
6753 case 'a':
6754 if (xmlStrEqual(node->name, BAD_CAST "area"))
6755 return(1);
6756 return(0);
6757 case 'b':
6758 if (xmlStrEqual(node->name, BAD_CAST "br"))
6759 return(1);
6760 if (xmlStrEqual(node->name, BAD_CAST "base"))
6761 return(1);
6762 if (xmlStrEqual(node->name, BAD_CAST "basefont"))
6763 return(1);
6764 return(0);
6765 case 'c':
6766 if (xmlStrEqual(node->name, BAD_CAST "col"))
6767 return(1);
6768 return(0);
6769 case 'f':
6770 if (xmlStrEqual(node->name, BAD_CAST "frame"))
6771 return(1);
6772 return(0);
6773 case 'h':
6774 if (xmlStrEqual(node->name, BAD_CAST "hr"))
6775 return(1);
6776 return(0);
6777 case 'i':
6778 if (xmlStrEqual(node->name, BAD_CAST "img"))
6779 return(1);
6780 if (xmlStrEqual(node->name, BAD_CAST "input"))
6781 return(1);
6782 if (xmlStrEqual(node->name, BAD_CAST "isindex"))
6783 return(1);
6784 return(0);
6785 case 'l':
6786 if (xmlStrEqual(node->name, BAD_CAST "link"))
6787 return(1);
6788 return(0);
6789 case 'm':
6790 if (xmlStrEqual(node->name, BAD_CAST "meta"))
6791 return(1);
6792 return(0);
6793 case 'p':
6794 if (xmlStrEqual(node->name, BAD_CAST "param"))
6795 return(1);
6796 return(0);
6797 }
6798 return(0);
6799}
6800
6801/**
6802 * xhtmlAttrListDumpOutput:
6803 * @buf: the XML buffer output
6804 * @doc: the document
6805 * @cur: the first attribute pointer
6806 * @encoding: an optional encoding string
6807 *
6808 * Dump a list of XML attributes
6809 */
6810static void
6811xhtmlAttrListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
6812 xmlAttrPtr cur, const char *encoding) {
6813 xmlAttrPtr xml_lang = NULL;
6814 xmlAttrPtr lang = NULL;
6815 xmlAttrPtr name = NULL;
6816 xmlAttrPtr id = NULL;
6817
6818 if (cur == NULL) {
6819#ifdef DEBUG_TREE
6820 xmlGenericError(xmlGenericErrorContext,
6821 "xmlAttrListDumpOutput : property == NULL\n");
6822#endif
6823 return;
6824 }
6825 while (cur != NULL) {
6826 if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "id")))
6827 id = cur;
6828 else
6829 if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "name")))
6830 name = cur;
6831 else
6832 if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")))
6833 lang = cur;
6834 else
6835 if ((cur->ns != NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")) &&
6836 (xmlStrEqual(cur->ns->prefix, BAD_CAST "xml")))
6837 xml_lang = cur;
6838 else if ((cur->ns == NULL) &&
6839 ((cur->children == NULL) ||
6840 (cur->children->content == NULL) ||
6841 (cur->children->content[0] == 0)) &&
6842 (htmlIsBooleanAttr(cur->name))) {
6843 if (cur->children != NULL)
6844 xmlFreeNode(cur->children);
6845 cur->children = xmlNewText(cur->name);
6846 if (cur->children != NULL)
6847 cur->children->parent = (xmlNodePtr) cur;
6848 }
6849 xmlAttrDumpOutput(buf, doc, cur, encoding);
6850 cur = cur->next;
6851 }
6852 /*
6853 * C.8
6854 */
6855 if ((name != NULL) && (id == NULL)) {
6856 xmlOutputBufferWriteString(buf, " id=\"");
6857 xmlAttrSerializeContent(buf->buffer, doc, name);
6858 xmlOutputBufferWriteString(buf, "\"");
6859 }
6860 /*
6861 * C.7.
6862 */
6863 if ((lang != NULL) && (xml_lang == NULL)) {
6864 xmlOutputBufferWriteString(buf, " xml:lang=\"");
6865 xmlAttrSerializeContent(buf->buffer, doc, lang);
6866 xmlOutputBufferWriteString(buf, "\"");
6867 } else
6868 if ((xml_lang != NULL) && (lang == NULL)) {
6869 xmlOutputBufferWriteString(buf, " lang=\"");
6870 xmlAttrSerializeContent(buf->buffer, doc, xml_lang);
6871 xmlOutputBufferWriteString(buf, "\"");
6872 }
6873}
6874
6875/**
6876 * xhtmlNodeListDumpOutput:
6877 * @buf: the XML buffer output
6878 * @doc: the XHTML document
6879 * @cur: the first node
6880 * @level: the imbrication level for indenting
6881 * @format: is formatting allowed
6882 * @encoding: an optional encoding string
6883 *
6884 * Dump an XML node list, recursive behaviour, children are printed too.
6885 * Note that format = 1 provide node indenting only if xmlIndentTreeOutput = 1
6886 * or xmlKeepBlanksDefault(0) was called
6887 */
6888static void
6889xhtmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
6890 xmlNodePtr cur, int level, int format, const char *encoding) {
6891 int i;
6892
6893 if (cur == NULL) {
6894#ifdef DEBUG_TREE
6895 xmlGenericError(xmlGenericErrorContext,
6896 "xhtmlNodeListDumpOutput : node == NULL\n");
6897#endif
6898 return;
6899 }
6900 while (cur != NULL) {
6901 if ((format) && (xmlIndentTreeOutput) &&
6902 (cur->type == XML_ELEMENT_NODE))
6903 for (i = 0;i < level;i++)
6904 xmlOutputBufferWriteString(buf, xmlTreeIndentString);
6905 xhtmlNodeDumpOutput(buf, doc, cur, level, format, encoding);
6906 if (format) {
6907 xmlOutputBufferWriteString(buf, "\n");
6908 }
6909 cur = cur->next;
6910 }
6911}
6912
6913/**
6914 * xhtmlNodeDumpOutput:
6915 * @buf: the XML buffer output
6916 * @doc: the XHTML document
6917 * @cur: the current node
6918 * @level: the imbrication level for indenting
6919 * @format: is formatting allowed
6920 * @encoding: an optional encoding string
6921 *
6922 * Dump an XHTML node, recursive behaviour, children are printed too.
6923 * Note that format = 1 provide node indenting only if xmlIndentTreeOutput = 1
6924 * or xmlKeepBlanksDefault(0) was called
6925 */
6926static void
6927xhtmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
6928 int level, int format, const char *encoding) {
6929 int i;
6930 xmlNodePtr tmp;
6931
6932 if (cur == NULL) {
6933#ifdef DEBUG_TREE
6934 xmlGenericError(xmlGenericErrorContext,
6935 "xmlNodeDumpOutput : node == NULL\n");
6936#endif
6937 return;
6938 }
6939 if (cur->type == XML_XINCLUDE_START)
6940 return;
6941 if (cur->type == XML_XINCLUDE_END)
6942 return;
6943 if (cur->type == XML_DTD_NODE) {
6944 xmlDtdDumpOutput(buf, (xmlDtdPtr) cur, encoding);
6945 return;
6946 }
6947 if (cur->type == XML_ELEMENT_DECL) {
6948 xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur);
6949 return;
6950 }
6951 if (cur->type == XML_ATTRIBUTE_DECL) {
6952 xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur);
6953 return;
6954 }
6955 if (cur->type == XML_ENTITY_DECL) {
6956 xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur);
6957 return;
6958 }
6959 if (cur->type == XML_TEXT_NODE) {
6960 if (cur->content != NULL) {
6961 if ((cur->name == xmlStringText) ||
6962 (cur->name != xmlStringTextNoenc)) {
6963 xmlChar *buffer;
6964
6965 if (encoding == NULL)
6966 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
6967 else
6968 buffer = xmlEncodeSpecialChars(doc, cur->content);
6969 if (buffer != NULL) {
6970 xmlOutputBufferWriteString(buf, (const char *)buffer);
6971 xmlFree(buffer);
6972 }
6973 } else {
6974 /*
6975 * Disable escaping, needed for XSLT
6976 */
6977 xmlOutputBufferWriteString(buf, (const char *) cur->content);
6978 }
6979 }
6980
6981 return;
6982 }
6983 if (cur->type == XML_PI_NODE) {
6984 if (cur->content != NULL) {
6985 xmlOutputBufferWriteString(buf, "<?");
6986 xmlOutputBufferWriteString(buf, (const char *)cur->name);
6987 if (cur->content != NULL) {
6988 xmlOutputBufferWriteString(buf, " ");
6989 xmlOutputBufferWriteString(buf, (const char *)cur->content);
6990 }
6991 xmlOutputBufferWriteString(buf, "?>");
6992 } else {
6993 xmlOutputBufferWriteString(buf, "<?");
6994 xmlOutputBufferWriteString(buf, (const char *)cur->name);
6995 xmlOutputBufferWriteString(buf, "?>");
6996 }
6997 return;
6998 }
6999 if (cur->type == XML_COMMENT_NODE) {
7000 if (cur->content != NULL) {
7001 xmlOutputBufferWriteString(buf, "<!--");
7002 xmlOutputBufferWriteString(buf, (const char *)cur->content);
7003 xmlOutputBufferWriteString(buf, "-->");
7004 }
7005 return;
7006 }
7007 if (cur->type == XML_ENTITY_REF_NODE) {
7008 xmlOutputBufferWriteString(buf, "&");
7009 xmlOutputBufferWriteString(buf, (const char *)cur->name);
7010 xmlOutputBufferWriteString(buf, ";");
7011 return;
7012 }
7013 if (cur->type == XML_CDATA_SECTION_NODE) {
7014 xmlOutputBufferWriteString(buf, "<![CDATA[");
7015 if (cur->content != NULL)
7016 xmlOutputBufferWriteString(buf, (const char *)cur->content);
7017 xmlOutputBufferWriteString(buf, "]]>");
7018 return;
7019 }
7020
7021 if (format == 1) {
7022 tmp = cur->children;
7023 while (tmp != NULL) {
7024 if ((tmp->type == XML_TEXT_NODE) ||
7025 (tmp->type == XML_ENTITY_REF_NODE)) {
7026 format = 0;
7027 break;
7028 }
7029 tmp = tmp->next;
7030 }
7031 }
7032 xmlOutputBufferWriteString(buf, "<");
7033 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
7034 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
7035 xmlOutputBufferWriteString(buf, ":");
7036 }
7037
7038 xmlOutputBufferWriteString(buf, (const char *)cur->name);
7039 if (cur->nsDef)
7040 xmlNsListDumpOutput(buf, cur->nsDef);
7041 if ((xmlStrEqual(cur->name, BAD_CAST "html") &&
7042 (cur->ns == NULL) && (cur->nsDef == NULL))) {
7043 /*
7044 * 3.1.1. Strictly Conforming Documents A.3.1.1 3/
7045 */
7046 xmlOutputBufferWriteString(buf,
7047 " xmlns=\"http://www.w3.org/1999/xhtml\"");
7048 }
7049 if (cur->properties != NULL)
7050 xhtmlAttrListDumpOutput(buf, doc, cur->properties, encoding);
7051
7052 if ((cur->type == XML_ELEMENT_NODE) && (cur->children == NULL)) {
7053 if (((cur->ns == NULL) || (cur->ns->prefix == NULL)) &&
7054 (xhtmlIsEmpty(cur) == 1)) {
7055 /*
7056 * C.2. Empty Elements
7057 */
7058 xmlOutputBufferWriteString(buf, " />");
7059 } else {
7060 /*
7061 * C.3. Element Minimization and Empty Element Content
7062 */
7063 xmlOutputBufferWriteString(buf, "></");
7064 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
7065 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
7066 xmlOutputBufferWriteString(buf, ":");
7067 }
7068 xmlOutputBufferWriteString(buf, (const char *)cur->name);
7069 xmlOutputBufferWriteString(buf, ">");
7070 }
7071 return;
7072 }
7073 xmlOutputBufferWriteString(buf, ">");
7074 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
7075 xmlChar *buffer;
7076
7077 if (encoding == NULL)
7078 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
7079 else
7080 buffer = xmlEncodeSpecialChars(doc, cur->content);
7081 if (buffer != NULL) {
7082 xmlOutputBufferWriteString(buf, (const char *)buffer);
7083 xmlFree(buffer);
7084 }
7085 }
7086
7087 /*
7088 * 4.8. Script and Style elements
7089 */
7090 if ((cur->type == XML_ELEMENT_NODE) &&
7091 ((xmlStrEqual(cur->name, BAD_CAST "script")) ||
7092 (xmlStrEqual(cur->name, BAD_CAST "style"))) &&
7093 ((cur->ns == NULL) ||
7094 (xmlStrEqual(cur->ns->href, XHTML_NS_NAME)))) {
7095 xmlNodePtr child = cur->children;
7096
7097 while (child != NULL) {
7098 if ((child->type == XML_TEXT_NODE) ||
7099 (child->type == XML_CDATA_SECTION_NODE)) {
Daniel Veillard64b35282002-12-04 15:10:40 +00007100 /*
7101 * Apparently CDATA escaping for style just break on IE,
7102 * mozilla and galeon, so ...
7103 */
7104 if (xmlStrEqual(cur->name, BAD_CAST "style") &&
7105 (xmlStrchr(child->content, '<') == NULL) &&
7106 (xmlStrchr(child->content, '>') == NULL) &&
7107 (xmlStrchr(child->content, '&') == NULL)) {
7108 xhtmlNodeDumpOutput(buf, doc, child, 0, 0, encoding);
7109 } else {
7110 xmlOutputBufferWriteString(buf, "<![CDATA[");
7111 if (child->content != NULL)
7112 xmlOutputBufferWriteString(buf,
7113 (const char *)child->content);
7114 xmlOutputBufferWriteString(buf, "]]>");
7115 }
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007116 } else {
7117 xhtmlNodeDumpOutput(buf, doc, child, 0, 0, encoding);
7118 }
7119 child = child->next;
7120 }
7121 } else if (cur->children != NULL) {
7122 if (format) xmlOutputBufferWriteString(buf, "\n");
7123 xhtmlNodeListDumpOutput(buf, doc, cur->children,
7124 (level >= 0?level+1:-1), format, encoding);
7125 if ((xmlIndentTreeOutput) && (format))
7126 for (i = 0;i < level;i++)
7127 xmlOutputBufferWriteString(buf, xmlTreeIndentString);
7128 }
7129 xmlOutputBufferWriteString(buf, "</");
7130 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
7131 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
7132 xmlOutputBufferWriteString(buf, ":");
7133 }
7134
7135 xmlOutputBufferWriteString(buf, (const char *)cur->name);
7136 xmlOutputBufferWriteString(buf, ">");
7137}
7138#endif
7139
Owen Taylor3473f882001-02-23 17:55:21 +00007140/************************************************************************
7141 * *
7142 * Saving functions front-ends *
7143 * *
7144 ************************************************************************/
7145
7146/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +00007147 * xmlDocDumpFormatMemoryEnc:
Owen Taylor3473f882001-02-23 17:55:21 +00007148 * @out_doc: Document to generate XML text from
7149 * @doc_txt_ptr: Memory pointer for allocated XML text
7150 * @doc_txt_len: Length of the generated XML text
7151 * @txt_encoding: Character encoding to use when generating XML text
7152 * @format: should formatting spaces been added
7153 *
7154 * Dump the current DOM tree into memory using the character encoding specified
7155 * by the caller. Note it is up to the caller of this function to free the
Daniel Veillardbd9afb52002-09-25 22:25:35 +00007156 * allocated memory with xmlFree().
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00007157 * Note that format = 1 provide node indenting only if xmlIndentTreeOutput = 1
7158 * or xmlKeepBlanksDefault(0) was called
Owen Taylor3473f882001-02-23 17:55:21 +00007159 */
7160
7161void
7162xmlDocDumpFormatMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00007163 int * doc_txt_len, const char * txt_encoding,
Daniel Veillard1731d6a2001-04-10 16:38:06 +00007164 int format) {
Owen Taylor3473f882001-02-23 17:55:21 +00007165 int dummy = 0;
7166
7167 xmlCharEncoding doc_charset;
7168 xmlOutputBufferPtr out_buff = NULL;
7169 xmlCharEncodingHandlerPtr conv_hdlr = NULL;
7170
7171 if (doc_txt_len == NULL) {
7172 doc_txt_len = &dummy; /* Continue, caller just won't get length */
7173 }
7174
7175 if (doc_txt_ptr == NULL) {
7176 *doc_txt_len = 0;
7177 xmlGenericError(xmlGenericErrorContext,
7178 "xmlDocDumpFormatMemoryEnc: Null return buffer pointer.");
7179 return;
7180 }
7181
7182 *doc_txt_ptr = NULL;
7183 *doc_txt_len = 0;
7184
7185 if (out_doc == NULL) {
7186 /* No document, no output */
7187 xmlGenericError(xmlGenericErrorContext,
7188 "xmlDocDumpFormatMemoryEnc: Null DOM tree document pointer.\n");
7189 return;
7190 }
7191
7192 /*
7193 * Validate the encoding value, if provided.
7194 * This logic is copied from xmlSaveFileEnc.
7195 */
7196
7197 if (txt_encoding == NULL)
7198 txt_encoding = (const char *) out_doc->encoding;
7199 if (txt_encoding != NULL) {
7200 doc_charset = xmlParseCharEncoding(txt_encoding);
7201
7202 if (out_doc->charset != XML_CHAR_ENCODING_UTF8) {
7203 xmlGenericError(xmlGenericErrorContext,
7204 "xmlDocDumpFormatMemoryEnc: Source document not in UTF8\n");
7205 return;
7206
7207 } else if (doc_charset != XML_CHAR_ENCODING_UTF8) {
7208 conv_hdlr = xmlFindCharEncodingHandler(txt_encoding);
7209 if ( conv_hdlr == NULL ) {
7210 xmlGenericError(xmlGenericErrorContext,
7211 "%s: %s %s '%s'\n",
7212 "xmlDocDumpFormatMemoryEnc",
7213 "Failed to identify encoding handler for",
7214 "character set",
7215 txt_encoding);
7216 return;
7217 }
7218 }
7219 }
7220
7221 if ((out_buff = xmlAllocOutputBuffer(conv_hdlr)) == NULL ) {
7222 xmlGenericError(xmlGenericErrorContext,
7223 "xmlDocDumpFormatMemoryEnc: Failed to allocate output buffer.\n");
7224 return;
7225 }
7226
Daniel Veillard1731d6a2001-04-10 16:38:06 +00007227 xmlDocContentDumpOutput(out_buff, out_doc, txt_encoding, format);
Owen Taylor3473f882001-02-23 17:55:21 +00007228 xmlOutputBufferFlush(out_buff);
7229 if (out_buff->conv != NULL) {
7230 *doc_txt_len = out_buff->conv->use;
7231 *doc_txt_ptr = xmlStrndup(out_buff->conv->content, *doc_txt_len);
7232 } else {
7233 *doc_txt_len = out_buff->buffer->use;
7234 *doc_txt_ptr = xmlStrndup(out_buff->buffer->content, *doc_txt_len);
7235 }
7236 (void)xmlOutputBufferClose(out_buff);
7237
7238 if ((*doc_txt_ptr == NULL) && (*doc_txt_len > 0)) {
7239 *doc_txt_len = 0;
7240 xmlGenericError(xmlGenericErrorContext,
7241 "xmlDocDumpFormatMemoryEnc: %s\n",
7242 "Failed to allocate memory for document text representation.");
7243 }
7244
7245 return;
7246}
7247
7248/**
7249 * xmlDocDumpMemory:
7250 * @cur: the document
7251 * @mem: OUT: the memory pointer
Daniel Veillard60087f32001-10-10 09:45:09 +00007252 * @size: OUT: the memory length
Owen Taylor3473f882001-02-23 17:55:21 +00007253 *
Daniel Veillardd1640922001-12-17 15:30:10 +00007254 * Dump an XML document in memory and return the #xmlChar * and it's size.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00007255 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00007256 */
7257void
7258xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) {
7259 xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, 0);
7260}
7261
7262/**
7263 * xmlDocDumpFormatMemory:
7264 * @cur: the document
7265 * @mem: OUT: the memory pointer
Daniel Veillard60087f32001-10-10 09:45:09 +00007266 * @size: OUT: the memory length
Owen Taylor3473f882001-02-23 17:55:21 +00007267 * @format: should formatting spaces been added
7268 *
7269 *
Daniel Veillardd1640922001-12-17 15:30:10 +00007270 * Dump an XML document in memory and return the #xmlChar * and it's size.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00007271 * It's up to the caller to free the memory with xmlFree().
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00007272 * Note that format = 1 provide node indenting only if xmlIndentTreeOutput = 1
7273 * or xmlKeepBlanksDefault(0) was called
Owen Taylor3473f882001-02-23 17:55:21 +00007274 */
7275void
7276xmlDocDumpFormatMemory(xmlDocPtr cur, xmlChar**mem, int *size, int format) {
7277 xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, format);
7278}
7279
7280/**
7281 * xmlDocDumpMemoryEnc:
7282 * @out_doc: Document to generate XML text from
7283 * @doc_txt_ptr: Memory pointer for allocated XML text
7284 * @doc_txt_len: Length of the generated XML text
7285 * @txt_encoding: Character encoding to use when generating XML text
7286 *
7287 * Dump the current DOM tree into memory using the character encoding specified
7288 * by the caller. Note it is up to the caller of this function to free the
Daniel Veillardbd9afb52002-09-25 22:25:35 +00007289 * allocated memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00007290 */
7291
7292void
7293xmlDocDumpMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
7294 int * doc_txt_len, const char * txt_encoding) {
7295 xmlDocDumpFormatMemoryEnc(out_doc, doc_txt_ptr, doc_txt_len,
Daniel Veillard1731d6a2001-04-10 16:38:06 +00007296 txt_encoding, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00007297}
7298
7299/**
7300 * xmlGetDocCompressMode:
7301 * @doc: the document
7302 *
7303 * get the compression ratio for a document, ZLIB based
7304 * Returns 0 (uncompressed) to 9 (max compression)
7305 */
7306int
7307xmlGetDocCompressMode (xmlDocPtr doc) {
7308 if (doc == NULL) return(-1);
7309 return(doc->compression);
7310}
7311
7312/**
7313 * xmlSetDocCompressMode:
7314 * @doc: the document
7315 * @mode: the compression ratio
7316 *
7317 * set the compression ratio for a document, ZLIB based
7318 * Correct values: 0 (uncompressed) to 9 (max compression)
7319 */
7320void
7321xmlSetDocCompressMode (xmlDocPtr doc, int mode) {
7322 if (doc == NULL) return;
7323 if (mode < 0) doc->compression = 0;
7324 else if (mode > 9) doc->compression = 9;
7325 else doc->compression = mode;
7326}
7327
7328/**
7329 * xmlGetCompressMode:
7330 *
7331 * get the default compression mode used, ZLIB based.
7332 * Returns 0 (uncompressed) to 9 (max compression)
7333 */
7334int
Daniel Veillard044fc6b2002-03-04 17:09:44 +00007335xmlGetCompressMode(void)
7336{
7337 return (xmlCompressMode);
Owen Taylor3473f882001-02-23 17:55:21 +00007338}
7339
7340/**
7341 * xmlSetCompressMode:
7342 * @mode: the compression ratio
7343 *
7344 * set the default compression mode used, ZLIB based
7345 * Correct values: 0 (uncompressed) to 9 (max compression)
7346 */
7347void
7348xmlSetCompressMode(int mode) {
7349 if (mode < 0) xmlCompressMode = 0;
7350 else if (mode > 9) xmlCompressMode = 9;
7351 else xmlCompressMode = mode;
7352}
7353
7354/**
Daniel Veillard9e412302002-06-10 15:59:44 +00007355 * xmlDocFormatDump:
Owen Taylor3473f882001-02-23 17:55:21 +00007356 * @f: the FILE*
7357 * @cur: the document
Daniel Veillard9e412302002-06-10 15:59:44 +00007358 * @format: should formatting spaces been added
Owen Taylor3473f882001-02-23 17:55:21 +00007359 *
7360 * Dump an XML document to an open FILE.
7361 *
Daniel Veillardd1640922001-12-17 15:30:10 +00007362 * returns: the number of bytes written or -1 in case of failure.
Owen Taylor3473f882001-02-23 17:55:21 +00007363 */
7364int
Daniel Veillard9e412302002-06-10 15:59:44 +00007365xmlDocFormatDump(FILE *f, xmlDocPtr cur, int format) {
Owen Taylor3473f882001-02-23 17:55:21 +00007366 xmlOutputBufferPtr buf;
7367 const char * encoding;
7368 xmlCharEncodingHandlerPtr handler = NULL;
7369 int ret;
7370
7371 if (cur == NULL) {
7372#ifdef DEBUG_TREE
7373 xmlGenericError(xmlGenericErrorContext,
7374 "xmlDocDump : document == NULL\n");
7375#endif
7376 return(-1);
7377 }
7378 encoding = (const char *) cur->encoding;
7379
7380 if (encoding != NULL) {
7381 xmlCharEncoding enc;
7382
7383 enc = xmlParseCharEncoding(encoding);
7384
7385 if (cur->charset != XML_CHAR_ENCODING_UTF8) {
7386 xmlGenericError(xmlGenericErrorContext,
7387 "xmlDocDump: document not in UTF8\n");
7388 return(-1);
7389 }
7390 if (enc != XML_CHAR_ENCODING_UTF8) {
7391 handler = xmlFindCharEncodingHandler(encoding);
7392 if (handler == NULL) {
7393 xmlFree((char *) cur->encoding);
7394 cur->encoding = NULL;
7395 }
7396 }
7397 }
7398 buf = xmlOutputBufferCreateFile(f, handler);
7399 if (buf == NULL) return(-1);
Daniel Veillard9e412302002-06-10 15:59:44 +00007400 xmlDocContentDumpOutput(buf, cur, NULL, format);
Owen Taylor3473f882001-02-23 17:55:21 +00007401
7402 ret = xmlOutputBufferClose(buf);
7403 return(ret);
7404}
7405
7406/**
Daniel Veillard9e412302002-06-10 15:59:44 +00007407 * xmlDocDump:
7408 * @f: the FILE*
7409 * @cur: the document
7410 *
7411 * Dump an XML document to an open FILE.
7412 *
7413 * returns: the number of bytes written or -1 in case of failure.
7414 */
7415int
7416xmlDocDump(FILE *f, xmlDocPtr cur) {
7417 return(xmlDocFormatDump (f, cur, 0));
7418}
7419
7420/**
Owen Taylor3473f882001-02-23 17:55:21 +00007421 * xmlSaveFileTo:
7422 * @buf: an output I/O buffer
7423 * @cur: the document
Daniel Veillardd1640922001-12-17 15:30:10 +00007424 * @encoding: the encoding if any assuming the I/O layer handles the trancoding
Owen Taylor3473f882001-02-23 17:55:21 +00007425 *
7426 * Dump an XML document to an I/O buffer.
7427 *
Daniel Veillardd1640922001-12-17 15:30:10 +00007428 * returns: the number of bytes written or -1 in case of failure.
Owen Taylor3473f882001-02-23 17:55:21 +00007429 */
7430int
CET 2001 Daniel Veillard5a37bde2001-11-01 14:31:22 +00007431xmlSaveFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding) {
Owen Taylor3473f882001-02-23 17:55:21 +00007432 int ret;
7433
7434 if (buf == NULL) return(0);
Daniel Veillard1731d6a2001-04-10 16:38:06 +00007435 xmlDocContentDumpOutput(buf, cur, encoding, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00007436 ret = xmlOutputBufferClose(buf);
7437 return(ret);
7438}
7439
7440/**
Daniel Veillardeefd4492001-04-28 16:55:50 +00007441 * xmlSaveFormatFileTo:
7442 * @buf: an output I/O buffer
7443 * @cur: the document
Daniel Veillardd1640922001-12-17 15:30:10 +00007444 * @encoding: the encoding if any assuming the I/O layer handles the trancoding
Daniel Veillardeefd4492001-04-28 16:55:50 +00007445 * @format: should formatting spaces been added
7446 *
7447 * Dump an XML document to an I/O buffer.
7448 *
Daniel Veillardd1640922001-12-17 15:30:10 +00007449 * returns: the number of bytes written or -1 in case of failure.
Daniel Veillardeefd4492001-04-28 16:55:50 +00007450 */
7451int
CET 2001 Daniel Veillard5a37bde2001-11-01 14:31:22 +00007452xmlSaveFormatFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding, int format) {
Daniel Veillardeefd4492001-04-28 16:55:50 +00007453 int ret;
7454
7455 if (buf == NULL) return(0);
7456 xmlDocContentDumpOutput(buf, cur, encoding, format);
7457 ret = xmlOutputBufferClose(buf);
7458 return(ret);
7459}
7460
7461/**
Daniel Veillardf012a642001-07-23 19:10:52 +00007462 * xmlSaveFormatFileEnc
7463 * @filename: the filename or URL to output
7464 * @cur: the document being saved
7465 * @encoding: the name of the encoding to use or NULL.
7466 * @format: should formatting spaces be added.
Daniel Veillardd1640922001-12-17 15:30:10 +00007467 *
7468 * Returns the number of bytes written or -1 in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00007469 */
7470int
Daniel Veillardf012a642001-07-23 19:10:52 +00007471xmlSaveFormatFileEnc( const char * filename, xmlDocPtr cur,
7472 const char * encoding, int format ) {
Owen Taylor3473f882001-02-23 17:55:21 +00007473 xmlOutputBufferPtr buf;
7474 xmlCharEncodingHandlerPtr handler = NULL;
Daniel Veillard81418e32001-05-22 15:08:55 +00007475 xmlCharEncoding enc;
Owen Taylor3473f882001-02-23 17:55:21 +00007476 int ret;
7477
Daniel Veillardfb25a512002-01-13 20:32:08 +00007478 if (encoding == NULL)
7479 encoding = (const char *) cur->encoding;
7480
Owen Taylor3473f882001-02-23 17:55:21 +00007481 if (encoding != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00007482
7483 enc = xmlParseCharEncoding(encoding);
7484 if (cur->charset != XML_CHAR_ENCODING_UTF8) {
7485 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007486 "xmlSaveFormatFileEnc: document not in UTF8\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007487 return(-1);
7488 }
7489 if (enc != XML_CHAR_ENCODING_UTF8) {
7490 handler = xmlFindCharEncodingHandler(encoding);
Daniel Veillard81418e32001-05-22 15:08:55 +00007491 if (handler == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00007492 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00007493 }
7494 }
7495
Daniel Veillardf012a642001-07-23 19:10:52 +00007496#ifdef HAVE_ZLIB_H
7497 if (cur->compression < 0) cur->compression = xmlCompressMode;
7498#endif
Owen Taylor3473f882001-02-23 17:55:21 +00007499 /*
7500 * save the content to a temp buffer.
7501 */
Daniel Veillardf012a642001-07-23 19:10:52 +00007502 buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression);
Owen Taylor3473f882001-02-23 17:55:21 +00007503 if (buf == NULL) return(-1);
7504
Daniel Veillardf012a642001-07-23 19:10:52 +00007505 xmlDocContentDumpOutput(buf, cur, encoding, format);
Owen Taylor3473f882001-02-23 17:55:21 +00007506
7507 ret = xmlOutputBufferClose(buf);
7508 return(ret);
7509}
7510
Daniel Veillardf012a642001-07-23 19:10:52 +00007511
7512/**
7513 * xmlSaveFileEnc:
7514 * @filename: the filename (or URL)
7515 * @cur: the document
7516 * @encoding: the name of an encoding (or NULL)
7517 *
7518 * Dump an XML document, converting it to the given encoding
7519 *
Daniel Veillardd1640922001-12-17 15:30:10 +00007520 * returns: the number of bytes written or -1 in case of failure.
Daniel Veillardf012a642001-07-23 19:10:52 +00007521 */
7522int
7523xmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) {
7524 return ( xmlSaveFormatFileEnc( filename, cur, encoding, 0 ) );
7525}
7526
Owen Taylor3473f882001-02-23 17:55:21 +00007527/**
Daniel Veillard67fee942001-04-26 18:59:03 +00007528 * xmlSaveFormatFile:
Owen Taylor3473f882001-02-23 17:55:21 +00007529 * @filename: the filename (or URL)
7530 * @cur: the document
Daniel Veillard67fee942001-04-26 18:59:03 +00007531 * @format: should formatting spaces been added
Owen Taylor3473f882001-02-23 17:55:21 +00007532 *
7533 * Dump an XML document to a file. Will use compression if
7534 * compiled in and enabled. If @filename is "-" the stdout file is
Daniel Veillardd1640922001-12-17 15:30:10 +00007535 * used. If @format is set then the document will be indented on output.
Daniel Veillard67fee942001-04-26 18:59:03 +00007536 *
Daniel Veillardd1640922001-12-17 15:30:10 +00007537 * returns: the number of bytes written or -1 in case of failure.
Owen Taylor3473f882001-02-23 17:55:21 +00007538 */
7539int
Daniel Veillard67fee942001-04-26 18:59:03 +00007540xmlSaveFormatFile(const char *filename, xmlDocPtr cur, int format) {
Daniel Veillardf012a642001-07-23 19:10:52 +00007541 return ( xmlSaveFormatFileEnc( filename, cur, NULL, format ) );
Owen Taylor3473f882001-02-23 17:55:21 +00007542}
7543
Daniel Veillard67fee942001-04-26 18:59:03 +00007544/**
7545 * xmlSaveFile:
7546 * @filename: the filename (or URL)
7547 * @cur: the document
7548 *
7549 * Dump an XML document to a file. Will use compression if
7550 * compiled in and enabled. If @filename is "-" the stdout file is
7551 * used.
Daniel Veillardd1640922001-12-17 15:30:10 +00007552 * returns: the number of bytes written or -1 in case of failure.
Daniel Veillard67fee942001-04-26 18:59:03 +00007553 */
7554int
7555xmlSaveFile(const char *filename, xmlDocPtr cur) {
Daniel Veillardf012a642001-07-23 19:10:52 +00007556 return(xmlSaveFormatFileEnc(filename, cur, NULL, 0));
Daniel Veillard67fee942001-04-26 18:59:03 +00007557}
7558