blob: db2b5d26142e60ae9ad0db20ebb085795ed7588a [file] [log] [blame]
Daniel Veillard260a68f1998-08-13 03:39:55 +00001/*
2 * tree.c : implemetation of access function for an XML tree.
3 *
4 * See Copyright for the status of this software.
5 *
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00006 * Daniel.Veillard@w3.org
Daniel Veillard260a68f1998-08-13 03:39:55 +00007 */
8
Daniel Veillard7f7d1111999-09-22 09:46:25 +00009#ifdef WIN32
Daniel Veillard3c558c31999-12-22 11:30:41 +000010#include "win32config.h"
Daniel Veillard7f7d1111999-09-22 09:46:25 +000011#else
Daniel Veillard151b1b01998-09-23 00:49:46 +000012#include "config.h"
Daniel Veillard7f7d1111999-09-22 09:46:25 +000013#endif
14
Daniel Veillard260a68f1998-08-13 03:39:55 +000015#include <stdio.h>
Daniel Veillard260a68f1998-08-13 03:39:55 +000016#include <string.h> /* for memset() only ! */
17
Daniel Veillard7f7d1111999-09-22 09:46:25 +000018#ifdef HAVE_CTYPE_H
19#include <ctype.h>
20#endif
21#ifdef HAVE_STDLIB_H
22#include <stdlib.h>
23#endif
Daniel Veillard151b1b01998-09-23 00:49:46 +000024#ifdef HAVE_ZLIB_H
25#include <zlib.h>
26#endif
27
Daniel Veillard6454aec1999-09-02 22:04:43 +000028#include "xmlmemory.h"
Daniel Veillard260a68f1998-08-13 03:39:55 +000029#include "tree.h"
Daniel Veillard37846c61999-12-16 17:52:19 +000030#include "parser.h"
Daniel Veillard260a68f1998-08-13 03:39:55 +000031#include "entities.h"
Daniel Veillard3b9def11999-01-31 22:15:06 +000032#include "valid.h"
Daniel Veillard260a68f1998-08-13 03:39:55 +000033
Daniel Veillarddd6b3671999-09-23 22:19:22 +000034static xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 };
Daniel Veillard260a68f1998-08-13 03:39:55 +000035int oldXMLWDcompatibility = 0;
36int xmlIndentTreeOutput = 1;
Daniel Veillardf5c2c871999-12-01 09:51:45 +000037xmlBufferAllocationScheme xmlBufferAllocScheme = XML_BUFFER_ALLOC_EXACT;
Daniel Veillard260a68f1998-08-13 03:39:55 +000038
Daniel Veillard15a8df41998-09-24 19:15:06 +000039static int xmlCompressMode = 0;
Daniel Veillard10a2c651999-12-12 13:03:50 +000040static int xmlCheckDTD = 1;
Daniel Veillard15a8df41998-09-24 19:15:06 +000041
Daniel Veillard39a1f9a1999-01-17 19:11:59 +000042#define UPDATE_LAST_CHILD(n) if ((n) != NULL) { \
43 xmlNodePtr ulccur = (n)->childs; \
44 if (ulccur == NULL) { \
45 (n)->last = NULL; \
46 } else { \
47 while (ulccur->next != NULL) ulccur = ulccur->next; \
48 (n)->last = ulccur; \
Daniel Veillard1e346af1999-02-22 10:33:01 +000049}}
Daniel Veillard39a1f9a1999-01-17 19:11:59 +000050
Daniel Veillardad8f99d2000-01-15 14:20:03 +000051/* #define DEBUG_BUFFER */
52/* #define DEBUG_TREE */
53
Daniel Veillard260a68f1998-08-13 03:39:55 +000054/************************************************************************
55 * *
56 * Allocation and deallocation of basic structures *
57 * *
58 ************************************************************************/
59
Daniel Veillard97b58771998-10-20 06:14:16 +000060/**
Daniel Veillardf5c2c871999-12-01 09:51:45 +000061 * xmlSetBufferAllocationScheme:
62 * @scheme: allocation method to use
63 *
64 * Set the buffer allocation method. Types are
65 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
66 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
67 * improves performance
68 */
69void
70xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme) {
71 xmlBufferAllocScheme = scheme;
72}
73
74/**
75 * xmlGetBufferAllocationScheme:
76 *
77 * Types are
78 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
79 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
80 * improves performance
81 *
82 * Returns the current allocation scheme
83 */
84xmlBufferAllocationScheme
85xmlGetBufferAllocationScheme() {
86 return xmlBufferAllocScheme;
87}
88
89/**
Daniel Veillard97b58771998-10-20 06:14:16 +000090 * xmlUpgradeOldNs:
91 * @doc: a document pointer
92 *
93 * Upgrade old style Namespaces (PI) and move them to the root of the document.
Daniel Veillard260a68f1998-08-13 03:39:55 +000094 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +000095void
96xmlUpgradeOldNs(xmlDocPtr doc) {
Daniel Veillard260a68f1998-08-13 03:39:55 +000097 xmlNsPtr cur;
98
99 if ((doc == NULL) || (doc->oldNs == NULL)) return;
100 if (doc->root == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000101#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +0000102 fprintf(stderr, "xmlUpgradeOldNs: failed no root !\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000103#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +0000104 return;
105 }
106
107 cur = doc->oldNs;
108 while (cur->next != NULL) {
109 cur->type = XML_LOCAL_NAMESPACE;
110 cur = cur->next;
111 }
112 cur->type = XML_LOCAL_NAMESPACE;
113 cur->next = doc->root->nsDef;
114 doc->root->nsDef = doc->oldNs;
115 doc->oldNs = NULL;
116}
117
Daniel Veillard97b58771998-10-20 06:14:16 +0000118/**
119 * xmlNewNs:
120 * @node: the element carrying the namespace
121 * @href: the URI associated
122 * @prefix: the prefix for the namespace
123 *
Daniel Veillard686d6b62000-01-03 11:08:02 +0000124 * Creation of a new Namespace. This function will refuse to create
125 * a namespace with a similar prefix than an existing one present on this
126 * node.
127 * Returns returns a new namespace pointer or NULL
Daniel Veillard260a68f1998-08-13 03:39:55 +0000128 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000129xmlNsPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000130xmlNewNs(xmlNodePtr node, const xmlChar *href, const xmlChar *prefix) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000131 xmlNsPtr cur;
132
133 if (href == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000134#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +0000135 fprintf(stderr, "xmlNewNs: href == NULL !\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000136#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +0000137 return(NULL);
138 }
139
140 /*
141 * Allocate a new DTD and fill the fields.
142 */
Daniel Veillard6454aec1999-09-02 22:04:43 +0000143 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
Daniel Veillard260a68f1998-08-13 03:39:55 +0000144 if (cur == NULL) {
145 fprintf(stderr, "xmlNewNs : malloc failed\n");
146 return(NULL);
147 }
148
149 cur->type = XML_LOCAL_NAMESPACE;
150 if (href != NULL)
151 cur->href = xmlStrdup(href);
152 else
153 cur->href = NULL;
154 if (prefix != NULL)
155 cur->prefix = xmlStrdup(prefix);
156 else
157 cur->prefix = NULL;
158
159 /*
160 * Add it at the end to preserve parsing order ...
Daniel Veillard686d6b62000-01-03 11:08:02 +0000161 * and checks for existing use of the prefix
Daniel Veillard260a68f1998-08-13 03:39:55 +0000162 */
163 cur->next = NULL;
164 if (node != NULL) {
165 if (node->nsDef == NULL) {
166 node->nsDef = cur;
167 } else {
168 xmlNsPtr prev = node->nsDef;
169
Daniel Veillard0142b842000-01-14 14:45:24 +0000170 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
171 (!xmlStrcmp(prev->prefix, cur->prefix))) {
172 xmlFreeNs(cur);
173 return(NULL);
174 }
Daniel Veillard686d6b62000-01-03 11:08:02 +0000175 while (prev->next != NULL) {
Daniel Veillard0142b842000-01-14 14:45:24 +0000176 prev = prev->next;
Daniel Veillard686d6b62000-01-03 11:08:02 +0000177 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
178 (!xmlStrcmp(prev->prefix, cur->prefix))) {
179 xmlFreeNs(cur);
180 return(NULL);
181 }
Daniel Veillard686d6b62000-01-03 11:08:02 +0000182 }
Daniel Veillard260a68f1998-08-13 03:39:55 +0000183 prev->next = cur;
184 }
185 }
Daniel Veillard260a68f1998-08-13 03:39:55 +0000186 return(cur);
187}
188
Daniel Veillard97b58771998-10-20 06:14:16 +0000189/**
190 * xmlNewGlobalNs:
191 * @doc: the document carrying the namespace
192 * @href: the URI associated
193 * @prefix: the prefix for the namespace
194 *
Daniel Veillard686d6b62000-01-03 11:08:02 +0000195 * Creation of a Namespace, the old way using PI and without scoping
196 * DEPRECATED !!!
Daniel Veillard0142b842000-01-14 14:45:24 +0000197 * It now create a namespace on the root element of the document if found.
Daniel Veillard686d6b62000-01-03 11:08:02 +0000198 * Returns NULL this functionnality had been removed
Daniel Veillard260a68f1998-08-13 03:39:55 +0000199 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000200xmlNsPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000201xmlNewGlobalNs(xmlDocPtr doc, const xmlChar *href, const xmlChar *prefix) {
Daniel Veillard0142b842000-01-14 14:45:24 +0000202 xmlNodePtr root;
203
204 xmlNsPtr cur;
205
206 root = xmlDocGetRootElement(doc);
207 if (root != NULL)
208 return(xmlNewNs(root, href, prefix));
209
210 /*
211 * if there is no root element yet, create an old Namespace type
212 * and it will be moved to the root at save time.
213 */
214 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
215 if (cur == NULL) {
216 fprintf(stderr, "xmlNewGlobalNs : malloc failed\n");
217 return(NULL);
218 }
219
220 cur->type = XML_GLOBAL_NAMESPACE;
221 if (href != NULL)
222 cur->href = xmlStrdup(href);
223 else
224 cur->href = NULL;
225 if (prefix != NULL)
226 cur->prefix = xmlStrdup(prefix);
227 else
228 cur->prefix = NULL;
229
230 /*
231 * Add it at the end to preserve parsing order ...
232 */
233 cur->next = NULL;
234 if (doc != NULL) {
235 if (doc->oldNs == NULL) {
236 doc->oldNs = cur;
237 } else {
238 xmlNsPtr prev = doc->oldNs;
239
240 while (prev->next != NULL) prev = prev->next;
241 prev->next = cur;
242 }
243 }
244
245 return(NULL);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000246}
247
Daniel Veillard97b58771998-10-20 06:14:16 +0000248/**
249 * xmlSetNs:
250 * @node: a node in the document
251 * @ns: a namespace pointer
252 *
253 * Associate a namespace to a node, a posteriori.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000254 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000255void
256xmlSetNs(xmlNodePtr node, xmlNsPtr ns) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000257 if (node == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000258#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +0000259 fprintf(stderr, "xmlSetNs: node == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000260#else
Daniel Veillard260a68f1998-08-13 03:39:55 +0000261 return;
262 }
263 node->ns = ns;
264}
265
Daniel Veillard97b58771998-10-20 06:14:16 +0000266/**
267 * xmlFreeNs:
268 * @cur: the namespace pointer
269 *
270 * Free up the structures associated to a namespace
Daniel Veillard260a68f1998-08-13 03:39:55 +0000271 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000272void
273xmlFreeNs(xmlNsPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000274 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000275#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +0000276 fprintf(stderr, "xmlFreeNs : ns == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000277#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +0000278 return;
279 }
Daniel Veillard6454aec1999-09-02 22:04:43 +0000280 if (cur->href != NULL) xmlFree((char *) cur->href);
281 if (cur->prefix != NULL) xmlFree((char *) cur->prefix);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000282 memset(cur, -1, sizeof(xmlNs));
Daniel Veillard6454aec1999-09-02 22:04:43 +0000283 xmlFree(cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000284}
285
Daniel Veillard97b58771998-10-20 06:14:16 +0000286/**
287 * xmlFreeNsList:
288 * @cur: the first namespace pointer
289 *
290 * Free up all the structures associated to the chained namespaces.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000291 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000292void
293xmlFreeNsList(xmlNsPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000294 xmlNsPtr next;
295 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000296#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +0000297 fprintf(stderr, "xmlFreeNsList : ns == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000298#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +0000299 return;
300 }
301 while (cur != NULL) {
302 next = cur->next;
303 xmlFreeNs(cur);
304 cur = next;
305 }
306}
307
Daniel Veillard97b58771998-10-20 06:14:16 +0000308/**
309 * xmlNewDtd:
310 * @doc: the document pointer
311 * @name: the DTD name
312 * @ExternalID: the external ID
313 * @SystemID: the system ID
314 *
Daniel Veillard260a68f1998-08-13 03:39:55 +0000315 * Creation of a new DTD.
Daniel Veillard1e346af1999-02-22 10:33:01 +0000316 * Returns a pointer to the new DTD structure
Daniel Veillard260a68f1998-08-13 03:39:55 +0000317 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000318xmlDtdPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000319xmlNewDtd(xmlDocPtr doc, const xmlChar *name,
320 const xmlChar *ExternalID, const xmlChar *SystemID) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000321 xmlDtdPtr cur;
322
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000323 if ((doc != NULL) && (doc->extSubset != NULL)) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000324#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +0000325 fprintf(stderr, "xmlNewDtd(%s): document %s already have a DTD %s\n",
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000326 /* !!! */ (char *) name, doc->name,
327 /* !!! */ (char *)doc->extSubset->name);
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000328#endif
329 return(NULL);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000330 }
331
332 /*
333 * Allocate a new DTD and fill the fields.
334 */
Daniel Veillard6454aec1999-09-02 22:04:43 +0000335 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
Daniel Veillard260a68f1998-08-13 03:39:55 +0000336 if (cur == NULL) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000337 fprintf(stderr, "xmlNewDtd : malloc failed\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +0000338 return(NULL);
339 }
340
341 if (name != NULL)
342 cur->name = xmlStrdup(name);
343 else
344 cur->name = NULL;
345 if (ExternalID != NULL)
346 cur->ExternalID = xmlStrdup(ExternalID);
347 else
348 cur->ExternalID = NULL;
349 if (SystemID != NULL)
350 cur->SystemID = xmlStrdup(SystemID);
351 else
352 cur->SystemID = NULL;
Daniel Veillard1e346af1999-02-22 10:33:01 +0000353 cur->notations = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000354 cur->elements = NULL;
Daniel Veillard1e346af1999-02-22 10:33:01 +0000355 cur->attributes = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000356 cur->entities = NULL;
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000357 if (doc != NULL)
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000358 doc->extSubset = cur;
359
360 return(cur);
361}
362
363/**
364 * xmlCreateIntSubset:
365 * @doc: the document pointer
366 * @name: the DTD name
367 * @ExternalID: the external ID
368 * @SystemID: the system ID
369 *
Daniel Veillard1e346af1999-02-22 10:33:01 +0000370 * Create the internal subset of a document
371 * Returns a pointer to the new DTD structure
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000372 */
373xmlDtdPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000374xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name,
375 const xmlChar *ExternalID, const xmlChar *SystemID) {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000376 xmlDtdPtr cur;
377
378 if ((doc != NULL) && (doc->intSubset != NULL)) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000379#ifdef DEBUG_TREE
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000380 fprintf(stderr,
381 "xmlCreateIntSubset(): document %s already have an internal subset\n",
382 doc->name);
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000383#endif
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000384 return(NULL);
385 }
386
387 /*
388 * Allocate a new DTD and fill the fields.
389 */
Daniel Veillard6454aec1999-09-02 22:04:43 +0000390 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000391 if (cur == NULL) {
392 fprintf(stderr, "xmlNewDtd : malloc failed\n");
393 return(NULL);
394 }
395
396 if (name != NULL)
397 cur->name = xmlStrdup(name);
398 else
399 cur->name = NULL;
400 if (ExternalID != NULL)
401 cur->ExternalID = xmlStrdup(ExternalID);
402 else
403 cur->ExternalID = NULL;
404 if (SystemID != NULL)
405 cur->SystemID = xmlStrdup(SystemID);
406 else
407 cur->SystemID = NULL;
Daniel Veillard1e346af1999-02-22 10:33:01 +0000408 cur->notations = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000409 cur->elements = NULL;
Daniel Veillard1e346af1999-02-22 10:33:01 +0000410 cur->attributes = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000411 cur->entities = NULL;
412 if (doc != NULL)
413 doc->intSubset = cur;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000414
415 return(cur);
416}
417
Daniel Veillard97b58771998-10-20 06:14:16 +0000418/**
419 * xmlFreeDtd:
420 * @cur: the DTD structure to free up
421 *
422 * Free a DTD structure.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000423 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000424void
425xmlFreeDtd(xmlDtdPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000426 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000427#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +0000428 fprintf(stderr, "xmlFreeDtd : DTD == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000429#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +0000430 return;
431 }
Daniel Veillard6454aec1999-09-02 22:04:43 +0000432 if (cur->name != NULL) xmlFree((char *) cur->name);
433 if (cur->SystemID != NULL) xmlFree((char *) cur->SystemID);
434 if (cur->ExternalID != NULL) xmlFree((char *) cur->ExternalID);
Daniel Veillard1e346af1999-02-22 10:33:01 +0000435 if (cur->notations != NULL)
436 xmlFreeNotationTable((xmlNotationTablePtr) cur->notations);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000437 if (cur->elements != NULL)
Daniel Veillard3b9def11999-01-31 22:15:06 +0000438 xmlFreeElementTable((xmlElementTablePtr) cur->elements);
Daniel Veillard1e346af1999-02-22 10:33:01 +0000439 if (cur->attributes != NULL)
440 xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000441 if (cur->entities != NULL)
442 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities);
443 memset(cur, -1, sizeof(xmlDtd));
Daniel Veillard6454aec1999-09-02 22:04:43 +0000444 xmlFree(cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000445}
446
Daniel Veillard97b58771998-10-20 06:14:16 +0000447/**
448 * xmlNewDoc:
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000449 * @version: xmlChar string giving the version of XML "1.0"
Daniel Veillard97b58771998-10-20 06:14:16 +0000450 *
Daniel Veillard1e346af1999-02-22 10:33:01 +0000451 * Returns a new document
Daniel Veillard260a68f1998-08-13 03:39:55 +0000452 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000453xmlDocPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000454xmlNewDoc(const xmlChar *version) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000455 xmlDocPtr cur;
456
457 if (version == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000458#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +0000459 fprintf(stderr, "xmlNewDoc : version == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000460#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +0000461 return(NULL);
462 }
463
464 /*
465 * Allocate a new document and fill the fields.
466 */
Daniel Veillard6454aec1999-09-02 22:04:43 +0000467 cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
Daniel Veillard260a68f1998-08-13 03:39:55 +0000468 if (cur == NULL) {
469 fprintf(stderr, "xmlNewDoc : malloc failed\n");
470 return(NULL);
471 }
472
Daniel Veillard33942841998-10-18 19:12:41 +0000473 cur->type = XML_DOCUMENT_NODE;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000474 cur->version = xmlStrdup(version);
475 cur->name = NULL;
476 cur->root = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000477 cur->intSubset = NULL;
478 cur->extSubset = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000479 cur->oldNs = NULL;
480 cur->encoding = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000481 cur->standalone = -1;
Daniel Veillard11a48ec1999-11-23 10:40:46 +0000482 cur->compression = -1; /* not initialized */
Daniel Veillardb96e6431999-08-29 21:02:19 +0000483 cur->ids = NULL;
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000484 cur->refs = NULL;
Daniel Veillard27d88741999-05-29 11:51:49 +0000485#ifndef XML_WITHOUT_CORBA
Daniel Veillard27fb0751998-10-17 06:47:46 +0000486 cur->_private = NULL;
487 cur->vepv = NULL;
488#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +0000489 return(cur);
490}
491
Daniel Veillard97b58771998-10-20 06:14:16 +0000492/**
493 * xmlFreeDoc:
494 * @cur: pointer to the document
495 * @:
496 *
497 * Free up all the structures used by a document, tree included.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000498 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000499void
500xmlFreeDoc(xmlDocPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000501 if (cur == NULL) {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000502#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +0000503 fprintf(stderr, "xmlFreeDoc : document == NULL\n");
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000504#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +0000505 return;
506 }
Daniel Veillard6454aec1999-09-02 22:04:43 +0000507 if (cur->version != NULL) xmlFree((char *) cur->version);
508 if (cur->name != NULL) xmlFree((char *) cur->name);
509 if (cur->encoding != NULL) xmlFree((char *) cur->encoding);
510 if (cur->root != NULL) xmlFreeNodeList(cur->root);
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000511 if (cur->intSubset != NULL) xmlFreeDtd(cur->intSubset);
512 if (cur->extSubset != NULL) xmlFreeDtd(cur->extSubset);
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000513 if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
Daniel Veillardb96e6431999-08-29 21:02:19 +0000514 if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids);
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000515 if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000516 memset(cur, -1, sizeof(xmlDoc));
Daniel Veillard6454aec1999-09-02 22:04:43 +0000517 xmlFree(cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000518}
519
Daniel Veillard97b58771998-10-20 06:14:16 +0000520/**
Daniel Veillard16253641998-10-28 22:58:05 +0000521 * xmlStringLenGetNodeList:
522 * @doc: the document
523 * @value: the value of the text
Daniel Veillard1e346af1999-02-22 10:33:01 +0000524 * @len: the length of the string value
Daniel Veillard16253641998-10-28 22:58:05 +0000525 *
526 * Parse the value string and build the node list associated. Should
527 * produce a flat tree with only TEXTs and ENTITY_REFs.
Daniel Veillard1e346af1999-02-22 10:33:01 +0000528 * Returns a pointer to the first child
Daniel Veillard16253641998-10-28 22:58:05 +0000529 */
530xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000531xmlStringLenGetNodeList(xmlDocPtr doc, const xmlChar *value, int len) {
Daniel Veillard16253641998-10-28 22:58:05 +0000532 xmlNodePtr ret = NULL, last = NULL;
533 xmlNodePtr node;
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000534 xmlChar *val;
535 const xmlChar *cur = value;
536 const xmlChar *q;
Daniel Veillard25940b71998-10-29 05:51:30 +0000537 xmlEntityPtr ent;
Daniel Veillard16253641998-10-28 22:58:05 +0000538
539 if (value == NULL) return(NULL);
540
541 q = cur;
542 while ((*cur != 0) && (cur - value < len)) {
543 if (*cur == '&') {
Daniel Veillard25940b71998-10-29 05:51:30 +0000544 /*
545 * Save the current text.
546 */
Daniel Veillard16253641998-10-28 22:58:05 +0000547 if (cur != q) {
Daniel Veillard25940b71998-10-29 05:51:30 +0000548 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
549 xmlNodeAddContentLen(last, q, cur - q);
550 } else {
551 node = xmlNewDocTextLen(doc, q, cur - q);
552 if (node == NULL) return(ret);
553 if (last == NULL)
554 last = ret = node;
555 else {
556 last->next = node;
557 node->prev = last;
558 last = node;
559 }
Daniel Veillard16253641998-10-28 22:58:05 +0000560 }
561 }
Daniel Veillard25940b71998-10-29 05:51:30 +0000562 /*
563 * Read the entity string
564 */
Daniel Veillard16253641998-10-28 22:58:05 +0000565 cur++;
566 q = cur;
567 while ((*cur != 0) && (cur - value < len) && (*cur != ';')) cur++;
568 if ((*cur == 0) || (cur - value >= len)) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000569#ifdef DEBUG_TREE
Daniel Veillard16253641998-10-28 22:58:05 +0000570 fprintf(stderr,
Daniel Veillard011b63c1999-06-02 17:44:04 +0000571 "xmlStringLenGetNodeList: unterminated entity %30s\n", q);
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000572#endif
Daniel Veillard16253641998-10-28 22:58:05 +0000573 return(ret);
574 }
575 if (cur != q) {
Daniel Veillard25940b71998-10-29 05:51:30 +0000576 /*
577 * Predefined entities don't generate nodes
578 */
Daniel Veillard16253641998-10-28 22:58:05 +0000579 val = xmlStrndup(q, cur - q);
Daniel Veillard25940b71998-10-29 05:51:30 +0000580 ent = xmlGetDocEntity(doc, val);
581 if ((ent != NULL) &&
582 (ent->type == XML_INTERNAL_PREDEFINED_ENTITY)) {
583 if (last == NULL) {
584 node = xmlNewDocText(doc, ent->content);
585 last = ret = node;
586 } else
587 xmlNodeAddContent(last, ent->content);
588
589 } else {
590 /*
591 * Create a new REFERENCE_REF node
592 */
593 node = xmlNewReference(doc, val);
Daniel Veillard242590e1998-11-13 18:04:35 +0000594 if (node == NULL) {
Daniel Veillard6454aec1999-09-02 22:04:43 +0000595 if (val != NULL) xmlFree(val);
Daniel Veillard242590e1998-11-13 18:04:35 +0000596 return(ret);
597 }
Daniel Veillard25940b71998-10-29 05:51:30 +0000598 if (last == NULL)
599 last = ret = node;
600 else {
601 last->next = node;
602 node->prev = last;
603 last = node;
604 }
Daniel Veillard16253641998-10-28 22:58:05 +0000605 }
Daniel Veillard6454aec1999-09-02 22:04:43 +0000606 xmlFree(val);
Daniel Veillard16253641998-10-28 22:58:05 +0000607 }
608 cur++;
609 q = cur;
610 } else
611 cur++;
612 }
613 if (cur != q) {
Daniel Veillard25940b71998-10-29 05:51:30 +0000614 /*
615 * Handle the last piece of text.
616 */
617 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
618 xmlNodeAddContentLen(last, q, cur - q);
619 } else {
620 node = xmlNewDocTextLen(doc, q, cur - q);
621 if (node == NULL) return(ret);
622 if (last == NULL)
623 last = ret = node;
624 else {
625 last->next = node;
626 node->prev = last;
627 last = node;
628 }
Daniel Veillard16253641998-10-28 22:58:05 +0000629 }
630 }
631 return(ret);
632}
633
634/**
Daniel Veillardccb09631998-10-27 06:21:04 +0000635 * xmlStringGetNodeList:
636 * @doc: the document
637 * @value: the value of the attribute
638 *
639 * Parse the value string and build the node list associated. Should
640 * produce a flat tree with only TEXTs and ENTITY_REFs.
Daniel Veillard1e346af1999-02-22 10:33:01 +0000641 * Returns a pointer to the first child
Daniel Veillardccb09631998-10-27 06:21:04 +0000642 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000643xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000644xmlStringGetNodeList(xmlDocPtr doc, const xmlChar *value) {
Daniel Veillardccb09631998-10-27 06:21:04 +0000645 xmlNodePtr ret = NULL, last = NULL;
646 xmlNodePtr node;
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000647 xmlChar *val;
648 const xmlChar *cur = value;
649 const xmlChar *q;
Daniel Veillard25940b71998-10-29 05:51:30 +0000650 xmlEntityPtr ent;
Daniel Veillardccb09631998-10-27 06:21:04 +0000651
652 if (value == NULL) return(NULL);
653
654 q = cur;
655 while (*cur != 0) {
656 if (*cur == '&') {
Daniel Veillard25940b71998-10-29 05:51:30 +0000657 /*
658 * Save the current text.
659 */
Daniel Veillardccb09631998-10-27 06:21:04 +0000660 if (cur != q) {
Daniel Veillard25940b71998-10-29 05:51:30 +0000661 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
662 xmlNodeAddContentLen(last, q, cur - q);
663 } else {
664 node = xmlNewDocTextLen(doc, q, cur - q);
665 if (node == NULL) return(ret);
666 if (last == NULL)
667 last = ret = node;
668 else {
669 last->next = node;
670 node->prev = last;
671 last = node;
672 }
Daniel Veillardccb09631998-10-27 06:21:04 +0000673 }
674 }
Daniel Veillard25940b71998-10-29 05:51:30 +0000675 /*
676 * Read the entity string
677 */
Daniel Veillardccb09631998-10-27 06:21:04 +0000678 cur++;
679 q = cur;
680 while ((*cur != 0) && (*cur != ';')) cur++;
681 if (*cur == 0) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000682#ifdef DEBUG_TREE
Daniel Veillardccb09631998-10-27 06:21:04 +0000683 fprintf(stderr,
684 "xmlStringGetNodeList: unterminated entity %30s\n", q);
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000685#endif
Daniel Veillardccb09631998-10-27 06:21:04 +0000686 return(ret);
687 }
688 if (cur != q) {
Daniel Veillard25940b71998-10-29 05:51:30 +0000689 /*
690 * Predefined entities don't generate nodes
691 */
Daniel Veillardccb09631998-10-27 06:21:04 +0000692 val = xmlStrndup(q, cur - q);
Daniel Veillard25940b71998-10-29 05:51:30 +0000693 ent = xmlGetDocEntity(doc, val);
694 if ((ent != NULL) &&
695 (ent->type == XML_INTERNAL_PREDEFINED_ENTITY)) {
696 if (last == NULL) {
697 node = xmlNewDocText(doc, ent->content);
698 last = ret = node;
699 } else
700 xmlNodeAddContent(last, ent->content);
701
702 } else {
703 /*
704 * Create a new REFERENCE_REF node
705 */
Daniel Veillard25940b71998-10-29 05:51:30 +0000706 node = xmlNewReference(doc, val);
Daniel Veillard242590e1998-11-13 18:04:35 +0000707 if (node == NULL) {
Daniel Veillard6454aec1999-09-02 22:04:43 +0000708 if (val != NULL) xmlFree(val);
Daniel Veillard242590e1998-11-13 18:04:35 +0000709 return(ret);
710 }
Daniel Veillard25940b71998-10-29 05:51:30 +0000711 if (last == NULL)
712 last = ret = node;
713 else {
714 last->next = node;
715 node->prev = last;
716 last = node;
717 }
Daniel Veillardccb09631998-10-27 06:21:04 +0000718 }
Daniel Veillard6454aec1999-09-02 22:04:43 +0000719 xmlFree(val);
Daniel Veillardccb09631998-10-27 06:21:04 +0000720 }
721 cur++;
722 q = cur;
723 } else
724 cur++;
725 }
726 if (cur != q) {
Daniel Veillard25940b71998-10-29 05:51:30 +0000727 /*
728 * Handle the last piece of text.
729 */
730 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
731 xmlNodeAddContentLen(last, q, cur - q);
732 } else {
733 node = xmlNewDocTextLen(doc, q, cur - q);
734 if (node == NULL) return(ret);
735 if (last == NULL)
736 last = ret = node;
737 else {
738 last->next = node;
739 node->prev = last;
740 last = node;
741 }
Daniel Veillardccb09631998-10-27 06:21:04 +0000742 }
743 }
744 return(ret);
745}
746
747/**
748 * xmlNodeListGetString:
749 * @doc: the document
750 * @list: a Node list
751 * @inLine: should we replace entity contents or show their external form
752 *
753 * Returns the string equivalent to the text contained in the Node list
754 * made of TEXTs and ENTITY_REFs
Daniel Veillard1e346af1999-02-22 10:33:01 +0000755 * Returns a pointer to the string copy, the calller must free it.
Daniel Veillardccb09631998-10-27 06:21:04 +0000756 */
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000757xmlChar *
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000758xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine) {
Daniel Veillardccb09631998-10-27 06:21:04 +0000759 xmlNodePtr node = list;
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000760 xmlChar *ret = NULL;
Daniel Veillardccb09631998-10-27 06:21:04 +0000761 xmlEntityPtr ent;
762
763 if (list == NULL) return(NULL);
764
765 while (node != NULL) {
766 if (node->type == XML_TEXT_NODE) {
Daniel Veillard71b656e2000-01-05 14:46:17 +0000767 if (inLine) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000768#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardccb09631998-10-27 06:21:04 +0000769 ret = xmlStrcat(ret, node->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000770#else
771 ret = xmlStrcat(ret, xmlBufferContent(node->content));
772#endif
773 } else {
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000774 xmlChar *buffer;
Daniel Veillard14fff061999-06-22 21:49:07 +0000775
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000776#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard14fff061999-06-22 21:49:07 +0000777 buffer = xmlEncodeEntitiesReentrant(doc, node->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000778#else
779 buffer = xmlEncodeEntitiesReentrant(doc,
780 xmlBufferContent(node->content));
781#endif
Daniel Veillard14fff061999-06-22 21:49:07 +0000782 if (buffer != NULL) {
783 ret = xmlStrcat(ret, buffer);
Daniel Veillard6454aec1999-09-02 22:04:43 +0000784 xmlFree(buffer);
Daniel Veillard14fff061999-06-22 21:49:07 +0000785 }
786 }
Daniel Veillardccb09631998-10-27 06:21:04 +0000787 } else if (node->type == XML_ENTITY_REF_NODE) {
788 if (inLine) {
789 ent = xmlGetDocEntity(doc, node->name);
790 if (ent != NULL)
791 ret = xmlStrcat(ret, ent->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000792 else {
793#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardccb09631998-10-27 06:21:04 +0000794 ret = xmlStrcat(ret, node->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000795#else
796 ret = xmlStrcat(ret, xmlBufferContent(node->content));
797#endif
798 }
Daniel Veillardccb09631998-10-27 06:21:04 +0000799 } else {
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000800 xmlChar buf[2];
Daniel Veillardccb09631998-10-27 06:21:04 +0000801 buf[0] = '&'; buf[1] = 0;
802 ret = xmlStrncat(ret, buf, 1);
803 ret = xmlStrcat(ret, node->name);
804 buf[0] = ';'; buf[1] = 0;
805 ret = xmlStrncat(ret, buf, 1);
806 }
807 }
808#if 0
809 else {
810 fprintf(stderr, "xmlGetNodeListString : invalide node type %d\n",
811 node->type);
812 }
813#endif
814 node = node->next;
815 }
816 return(ret);
817}
818
819/**
Daniel Veillard97b58771998-10-20 06:14:16 +0000820 * xmlNewProp:
821 * @node: the holding node
822 * @name: the name of the attribute
823 * @value: the value of the attribute
824 *
825 * Create a new property carried by a node.
Daniel Veillard1e346af1999-02-22 10:33:01 +0000826 * Returns a pointer to the attribute
Daniel Veillard260a68f1998-08-13 03:39:55 +0000827 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000828xmlAttrPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000829xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000830 xmlAttrPtr cur;
831
832 if (name == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000833#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +0000834 fprintf(stderr, "xmlNewProp : name == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000835#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +0000836 return(NULL);
837 }
838
839 /*
840 * Allocate a new property and fill the fields.
841 */
Daniel Veillard6454aec1999-09-02 22:04:43 +0000842 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
Daniel Veillard260a68f1998-08-13 03:39:55 +0000843 if (cur == NULL) {
844 fprintf(stderr, "xmlNewProp : malloc failed\n");
845 return(NULL);
846 }
847
Daniel Veillard33942841998-10-18 19:12:41 +0000848 cur->type = XML_ATTRIBUTE_NODE;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000849 cur->node = node;
Daniel Veillardb96e6431999-08-29 21:02:19 +0000850 cur->ns = NULL;
851 cur->name = xmlStrdup(name);
Daniel Veillard51e3b151999-11-12 17:02:31 +0000852 if (value != NULL) {
853 xmlChar *buffer;
854 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
855 cur->val = xmlStringGetNodeList(node->doc, buffer);
856 xmlFree(buffer);
857 }
Daniel Veillardb96e6431999-08-29 21:02:19 +0000858 else
859 cur->val = NULL;
860#ifndef XML_WITHOUT_CORBA
861 cur->_private = NULL;
862 cur->vepv = NULL;
863#endif
864
865 /*
866 * Add it at the end to preserve parsing order ...
867 */
868 cur->next = NULL;
869 if (node != NULL) {
870 if (node->properties == NULL) {
871 node->properties = cur;
872 } else {
873 xmlAttrPtr prev = node->properties;
874
875 while (prev->next != NULL) prev = prev->next;
876 prev->next = cur;
877 }
878 }
Daniel Veillard00fdf371999-10-08 09:40:39 +0000879#ifndef XML_WITHOUT_CORBA
880 cur->_private = NULL;
881 cur->vepv = NULL;
882#endif
Daniel Veillardb96e6431999-08-29 21:02:19 +0000883 return(cur);
884}
885
886/**
887 * xmlNewNsProp:
888 * @node: the holding node
889 * @ns: the namespace
890 * @name: the name of the attribute
891 * @value: the value of the attribute
892 *
893 * Create a new property tagged with a namespace and carried by a node.
894 * Returns a pointer to the attribute
895 */
896xmlAttrPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000897xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
898 const xmlChar *value) {
Daniel Veillardb96e6431999-08-29 21:02:19 +0000899 xmlAttrPtr cur;
900
901 if (name == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000902#ifdef DEBUG_TREE
Daniel Veillardb96e6431999-08-29 21:02:19 +0000903 fprintf(stderr, "xmlNewProp : name == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000904#endif
Daniel Veillardb96e6431999-08-29 21:02:19 +0000905 return(NULL);
906 }
907
908 /*
909 * Allocate a new property and fill the fields.
910 */
Daniel Veillard6454aec1999-09-02 22:04:43 +0000911 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
Daniel Veillardb96e6431999-08-29 21:02:19 +0000912 if (cur == NULL) {
913 fprintf(stderr, "xmlNewProp : malloc failed\n");
914 return(NULL);
915 }
916
917 cur->type = XML_ATTRIBUTE_NODE;
918 cur->node = node;
919 cur->ns = ns;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000920 cur->name = xmlStrdup(name);
921 if (value != NULL)
Daniel Veillardccb09631998-10-27 06:21:04 +0000922 cur->val = xmlStringGetNodeList(node->doc, value);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000923 else
Daniel Veillardccb09631998-10-27 06:21:04 +0000924 cur->val = NULL;
Daniel Veillard27d88741999-05-29 11:51:49 +0000925#ifndef XML_WITHOUT_CORBA
Daniel Veillard27fb0751998-10-17 06:47:46 +0000926 cur->_private = NULL;
927 cur->vepv = NULL;
928#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +0000929
930 /*
931 * Add it at the end to preserve parsing order ...
932 */
933 cur->next = NULL;
934 if (node != NULL) {
935 if (node->properties == NULL) {
936 node->properties = cur;
937 } else {
938 xmlAttrPtr prev = node->properties;
939
940 while (prev->next != NULL) prev = prev->next;
941 prev->next = cur;
942 }
943 }
944 return(cur);
945}
946
Daniel Veillard97b58771998-10-20 06:14:16 +0000947/**
Daniel Veillardccb09631998-10-27 06:21:04 +0000948 * xmlNewDocProp:
949 * @doc: the document
950 * @name: the name of the attribute
951 * @value: the value of the attribute
952 *
953 * Create a new property carried by a document.
Daniel Veillard1e346af1999-02-22 10:33:01 +0000954 * Returns a pointer to the attribute
Daniel Veillardccb09631998-10-27 06:21:04 +0000955 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000956xmlAttrPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000957xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) {
Daniel Veillardccb09631998-10-27 06:21:04 +0000958 xmlAttrPtr cur;
959
960 if (name == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000961#ifdef DEBUG_TREE
Daniel Veillardccb09631998-10-27 06:21:04 +0000962 fprintf(stderr, "xmlNewProp : name == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000963#endif
Daniel Veillardccb09631998-10-27 06:21:04 +0000964 return(NULL);
965 }
966
967 /*
968 * Allocate a new property and fill the fields.
969 */
Daniel Veillard6454aec1999-09-02 22:04:43 +0000970 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
Daniel Veillardccb09631998-10-27 06:21:04 +0000971 if (cur == NULL) {
972 fprintf(stderr, "xmlNewProp : malloc failed\n");
973 return(NULL);
974 }
975
976 cur->type = XML_ATTRIBUTE_NODE;
977 cur->node = NULL;
978 cur->name = xmlStrdup(name);
979 if (value != NULL)
980 cur->val = xmlStringGetNodeList(doc, value);
981 else
982 cur->val = NULL;
Daniel Veillard27d88741999-05-29 11:51:49 +0000983#ifndef XML_WITHOUT_CORBA
Daniel Veillardccb09631998-10-27 06:21:04 +0000984 cur->_private = NULL;
985 cur->vepv = NULL;
986#endif
987
988 cur->next = NULL;
989 return(cur);
990}
991
992/**
Daniel Veillard97b58771998-10-20 06:14:16 +0000993 * xmlFreePropList:
994 * @cur: the first property in the list
995 *
996 * Free a property and all its siblings, all the childs are freed too.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000997 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000998void
999xmlFreePropList(xmlAttrPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001000 xmlAttrPtr next;
1001 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001002#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00001003 fprintf(stderr, "xmlFreePropList : property == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001004#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00001005 return;
1006 }
1007 while (cur != NULL) {
1008 next = cur->next;
1009 xmlFreeProp(cur);
1010 cur = next;
1011 }
1012}
1013
Daniel Veillard97b58771998-10-20 06:14:16 +00001014/**
1015 * xmlFreeProp:
Daniel Veillard686d6b62000-01-03 11:08:02 +00001016 * @cur: an attribute
Daniel Veillard97b58771998-10-20 06:14:16 +00001017 *
Daniel Veillard686d6b62000-01-03 11:08:02 +00001018 * Free one attribute, all the content is freed too
Daniel Veillard260a68f1998-08-13 03:39:55 +00001019 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001020void
1021xmlFreeProp(xmlAttrPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001022 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001023#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00001024 fprintf(stderr, "xmlFreeProp : property == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001025#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00001026 return;
1027 }
Daniel Veillard71b656e2000-01-05 14:46:17 +00001028 /* Check for ID removal -> leading to invalid references ! */
1029 if ((cur->node != NULL) &&
1030 (xmlIsID(cur->node->doc, cur->node, cur)))
1031 xmlRemoveID(cur->node->doc, cur);
Daniel Veillard6454aec1999-09-02 22:04:43 +00001032 if (cur->name != NULL) xmlFree((char *) cur->name);
Daniel Veillardccb09631998-10-27 06:21:04 +00001033 if (cur->val != NULL) xmlFreeNodeList(cur->val);
Daniel Veillard260a68f1998-08-13 03:39:55 +00001034 memset(cur, -1, sizeof(xmlAttr));
Daniel Veillard6454aec1999-09-02 22:04:43 +00001035 xmlFree(cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +00001036}
1037
Daniel Veillard97b58771998-10-20 06:14:16 +00001038/**
Daniel Veillard686d6b62000-01-03 11:08:02 +00001039 * xmlRemoveProp:
1040 * @cur: an attribute
1041 *
1042 * Unlink and free one attribute, all the content is freed too
1043 * Note this doesn't work for namespace definition attributes
1044 *
1045 * Returns 0 if success and -1 in case of error.
1046 */
1047int
1048xmlRemoveProp(xmlAttrPtr cur) {
1049 xmlAttrPtr tmp;
1050 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001051#ifdef DEBUG_TREE
Daniel Veillard686d6b62000-01-03 11:08:02 +00001052 fprintf(stderr, "xmlRemoveProp : cur == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001053#endif
Daniel Veillard686d6b62000-01-03 11:08:02 +00001054 return(-1);
1055 }
1056 if (cur->node == NULL) {
1057 fprintf(stderr, "xmlRemoveProp : cur->node == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001058#endif
Daniel Veillard686d6b62000-01-03 11:08:02 +00001059 return(-1);
1060 }
1061 tmp = cur->node->properties;
1062 if (tmp == cur) {
1063 cur->node->properties = cur->next;
1064 xmlFreeProp(cur);
1065 return(0);
1066 }
1067 while (tmp != NULL) {
1068 if (tmp->next == cur) {
1069 tmp->next = cur->next;
1070 xmlFreeProp(cur);
1071 return(0);
1072 }
1073 tmp = tmp->next;
1074 }
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001075#ifdef DEBUG_TREE
Daniel Veillard686d6b62000-01-03 11:08:02 +00001076 fprintf(stderr, "xmlRemoveProp : attribute not owned by its node\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001077#endif
Daniel Veillard686d6b62000-01-03 11:08:02 +00001078 return(-1);
1079}
1080
1081/**
Daniel Veillardb96e6431999-08-29 21:02:19 +00001082 * xmlNewPI:
1083 * @name: the processing instruction name
1084 * @content: the PI content
1085 *
1086 * Creation of a processing instruction element.
1087 * Returns a pointer to the new node object.
1088 */
1089xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001090xmlNewPI(const xmlChar *name, const xmlChar *content) {
Daniel Veillardb96e6431999-08-29 21:02:19 +00001091 xmlNodePtr cur;
1092
1093 if (name == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001094#ifdef DEBUG_TREE
Daniel Veillardb96e6431999-08-29 21:02:19 +00001095 fprintf(stderr, "xmlNewPI : name == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001096#endif
Daniel Veillardb96e6431999-08-29 21:02:19 +00001097 return(NULL);
1098 }
1099
1100 /*
1101 * Allocate a new node and fill the fields.
1102 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00001103 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillardb96e6431999-08-29 21:02:19 +00001104 if (cur == NULL) {
1105 fprintf(stderr, "xmlNewPI : malloc failed\n");
1106 return(NULL);
1107 }
1108
1109 cur->type = XML_PI_NODE;
1110 cur->doc = NULL;
1111 cur->parent = NULL;
1112 cur->next = NULL;
1113 cur->prev = NULL;
1114 cur->childs = NULL;
1115 cur->last = NULL;
1116 cur->properties = NULL;
1117 cur->name = xmlStrdup(name);
1118 cur->ns = NULL;
1119 cur->nsDef = NULL;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001120 if (content != NULL) {
1121#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardb96e6431999-08-29 21:02:19 +00001122 cur->content = xmlStrdup(content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001123#else
1124 cur->content = xmlBufferCreateSize(0);
1125 xmlBufferSetAllocationScheme(cur->content,
1126 xmlGetBufferAllocationScheme());
1127 xmlBufferAdd(cur->content, content, -1);
1128#endif
1129 } else
Daniel Veillardb96e6431999-08-29 21:02:19 +00001130 cur->content = NULL;
1131#ifndef XML_WITHOUT_CORBA
1132 cur->_private = NULL;
1133 cur->vepv = NULL;
1134#endif
1135 return(cur);
1136}
1137
1138/**
Daniel Veillard97b58771998-10-20 06:14:16 +00001139 * xmlNewNode:
1140 * @ns: namespace if any
1141 * @name: the node name
Daniel Veillard97b58771998-10-20 06:14:16 +00001142 *
1143 * Creation of a new node element. @ns and @content are optionnal (NULL).
Daniel Veillardccb09631998-10-27 06:21:04 +00001144 * If content is non NULL, a child list containing the TEXTs and
1145 * ENTITY_REFs node will be created.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001146 * Returns a pointer to the new node object.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001147 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001148xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001149xmlNewNode(xmlNsPtr ns, const xmlChar *name) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001150 xmlNodePtr cur;
1151
1152 if (name == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001153#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00001154 fprintf(stderr, "xmlNewNode : name == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001155#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00001156 return(NULL);
1157 }
1158
1159 /*
1160 * Allocate a new node and fill the fields.
1161 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00001162 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillard260a68f1998-08-13 03:39:55 +00001163 if (cur == NULL) {
1164 fprintf(stderr, "xmlNewNode : malloc failed\n");
1165 return(NULL);
1166 }
1167
Daniel Veillard33942841998-10-18 19:12:41 +00001168 cur->type = XML_ELEMENT_NODE;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001169 cur->doc = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001170 cur->parent = NULL;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001171 cur->next = NULL;
1172 cur->prev = NULL;
1173 cur->childs = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001174 cur->last = NULL;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001175 cur->properties = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001176 cur->name = xmlStrdup(name);
1177 cur->ns = ns;
1178 cur->nsDef = NULL;
Daniel Veillardccb09631998-10-27 06:21:04 +00001179 cur->content = NULL;
Daniel Veillard27d88741999-05-29 11:51:49 +00001180#ifndef XML_WITHOUT_CORBA
Daniel Veillard27fb0751998-10-17 06:47:46 +00001181 cur->_private = NULL;
1182 cur->vepv = NULL;
1183#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00001184 return(cur);
1185}
1186
Daniel Veillard97b58771998-10-20 06:14:16 +00001187/**
1188 * xmlNewDocNode:
1189 * @doc: the document
1190 * @ns: namespace if any
1191 * @name: the node name
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001192 * @content: the XML text content if any
Daniel Veillard97b58771998-10-20 06:14:16 +00001193 *
1194 * Creation of a new node element within a document. @ns and @content
1195 * are optionnal (NULL).
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001196 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
1197 * references, but XML special chars need to be escaped first by using
1198 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
1199 * need entities support.
1200 *
Daniel Veillard1e346af1999-02-22 10:33:01 +00001201 * Returns a pointer to the new node object.
Daniel Veillard97b58771998-10-20 06:14:16 +00001202 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001203xmlNodePtr
1204xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns,
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001205 const xmlChar *name, const xmlChar *content) {
Daniel Veillard0bef1311998-10-14 02:36:47 +00001206 xmlNodePtr cur;
1207
Daniel Veillardccb09631998-10-27 06:21:04 +00001208 cur = xmlNewNode(ns, name);
1209 if (cur != NULL) {
1210 cur->doc = doc;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001211 if (content != NULL) {
Daniel Veillardccb09631998-10-27 06:21:04 +00001212 cur->childs = xmlStringGetNodeList(doc, content);
Daniel Veillard1e346af1999-02-22 10:33:01 +00001213 UPDATE_LAST_CHILD(cur)
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001214 }
Daniel Veillardccb09631998-10-27 06:21:04 +00001215 }
Daniel Veillard0bef1311998-10-14 02:36:47 +00001216 return(cur);
1217}
1218
1219
Daniel Veillard97b58771998-10-20 06:14:16 +00001220/**
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001221 * xmlNewDocRawNode:
1222 * @doc: the document
1223 * @ns: namespace if any
1224 * @name: the node name
1225 * @content: the text content if any
1226 *
1227 * Creation of a new node element within a document. @ns and @content
1228 * are optionnal (NULL).
1229 *
1230 * Returns a pointer to the new node object.
1231 */
1232xmlNodePtr
1233xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns,
1234 const xmlChar *name, const xmlChar *content) {
1235 xmlNodePtr cur;
1236
1237 cur = xmlNewNode(ns, name);
1238 if (cur != NULL) {
1239 cur->doc = doc;
1240 if (content != NULL) {
1241 cur->childs = xmlNewDocText(doc, content);
1242 UPDATE_LAST_CHILD(cur)
1243 }
1244 }
1245 return(cur);
1246}
1247
Daniel Veillard2eac5032000-01-09 21:08:56 +00001248/**
1249 * xmlNewDocFragment:
1250 * @doc: the document owning the fragment
1251 *
1252 * Creation of a new Fragment node.
1253 * Returns a pointer to the new node object.
1254 */
1255xmlNodePtr
1256xmlNewDocFragment(xmlDocPtr doc) {
1257 xmlNodePtr cur;
1258
1259 /*
1260 * Allocate a new DocumentFragment node and fill the fields.
1261 */
1262 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1263 if (cur == NULL) {
1264 fprintf(stderr, "xmlNewDocFragment : malloc failed\n");
1265 return(NULL);
1266 }
1267
1268 cur->type = XML_DOCUMENT_FRAG_NODE;
1269 cur->doc = doc;
1270 cur->parent = NULL;
1271 cur->next = NULL;
1272 cur->prev = NULL;
1273 cur->childs = NULL;
1274 cur->last = NULL;
1275 cur->properties = NULL;
1276 cur->name = NULL;
1277 cur->ns = NULL;
1278 cur->nsDef = NULL;
1279 cur->content = NULL;
1280#ifndef XML_WITHOUT_CORBA
1281 cur->_private = NULL;
1282 cur->vepv = NULL;
1283#endif
1284 return(cur);
1285}
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001286
1287/**
Daniel Veillard97b58771998-10-20 06:14:16 +00001288 * xmlNewText:
1289 * @content: the text content
1290 *
1291 * Creation of a new text node.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001292 * Returns a pointer to the new node object.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001293 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001294xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001295xmlNewText(const xmlChar *content) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001296 xmlNodePtr cur;
1297
1298 /*
1299 * Allocate a new node and fill the fields.
1300 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00001301 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillard260a68f1998-08-13 03:39:55 +00001302 if (cur == NULL) {
1303 fprintf(stderr, "xmlNewText : malloc failed\n");
1304 return(NULL);
1305 }
1306
Daniel Veillard33942841998-10-18 19:12:41 +00001307 cur->type = XML_TEXT_NODE;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001308 cur->doc = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001309 cur->parent = NULL;
1310 cur->next = NULL;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001311 cur->prev = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001312 cur->childs = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001313 cur->last = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001314 cur->properties = NULL;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001315 cur->type = XML_TEXT_NODE;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001316 cur->name = xmlStrdup(xmlStringText);
1317 cur->ns = NULL;
1318 cur->nsDef = NULL;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001319 if (content != NULL) {
1320#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard260a68f1998-08-13 03:39:55 +00001321 cur->content = xmlStrdup(content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001322#else
1323 cur->content = xmlBufferCreateSize(0);
1324 xmlBufferSetAllocationScheme(cur->content,
1325 xmlGetBufferAllocationScheme());
1326 xmlBufferAdd(cur->content, content, -1);
1327#endif
1328 } else
Daniel Veillard260a68f1998-08-13 03:39:55 +00001329 cur->content = NULL;
Daniel Veillard00fdf371999-10-08 09:40:39 +00001330#ifndef XML_WITHOUT_CORBA
1331 cur->_private = NULL;
1332 cur->vepv = NULL;
1333#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00001334 return(cur);
1335}
1336
Daniel Veillard97b58771998-10-20 06:14:16 +00001337/**
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001338 * xmlNewTextChild:
1339 * @parent: the parent node
1340 * @ns: a namespace if any
1341 * @name: the name of the child
1342 * @content: the text content of the child if any.
1343 *
1344 * Creation of a new child element, added at the end of @parent childs list.
1345 * @ns and @content parameters are optionnal (NULL). If content is non NULL,
1346 * a child TEXT node will be created containing the string content.
1347 *
1348 * Returns a pointer to the new node object.
1349 */
1350xmlNodePtr
1351xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns,
1352 const xmlChar *name, const xmlChar *content) {
1353 xmlNodePtr cur, prev;
1354
1355 if (parent == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001356#ifdef DEBUG_TREE
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001357 fprintf(stderr, "xmlNewTextChild : parent == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001358#endif
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001359 return(NULL);
1360 }
1361
1362 if (name == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001363#ifdef DEBUG_TREE
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001364 fprintf(stderr, "xmlNewTextChild : name == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001365#endif
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001366 return(NULL);
1367 }
1368
1369 /*
1370 * Allocate a new node
1371 */
1372 if (ns == NULL)
1373 cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content);
1374 else
1375 cur = xmlNewDocRawNode(parent->doc, ns, name, content);
1376 if (cur == NULL) return(NULL);
1377
1378 /*
1379 * add the new element at the end of the childs list.
1380 */
1381 cur->type = XML_ELEMENT_NODE;
1382 cur->parent = parent;
1383 cur->doc = parent->doc;
1384 if (parent->childs == NULL) {
1385 parent->childs = cur;
1386 parent->last = cur;
1387 } else {
1388 prev = parent->last;
1389 prev->next = cur;
1390 cur->prev = prev;
1391 parent->last = cur;
1392 }
1393
1394 return(cur);
1395}
1396
1397/**
Daniel Veillardccb09631998-10-27 06:21:04 +00001398 * xmlNewReference:
1399 * @doc: the document
1400 * @name: the reference name, or the reference string with & and ;
1401 *
1402 * Creation of a new reference node.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001403 * Returns a pointer to the new node object.
Daniel Veillardccb09631998-10-27 06:21:04 +00001404 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001405xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001406xmlNewReference(xmlDocPtr doc, const xmlChar *name) {
Daniel Veillardccb09631998-10-27 06:21:04 +00001407 xmlNodePtr cur;
1408 xmlEntityPtr ent;
1409
1410 /*
1411 * Allocate a new node and fill the fields.
1412 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00001413 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillardccb09631998-10-27 06:21:04 +00001414 if (cur == NULL) {
1415 fprintf(stderr, "xmlNewText : malloc failed\n");
1416 return(NULL);
1417 }
1418
1419 cur->type = XML_ENTITY_REF_NODE;
Daniel Veillard10c6a8f1998-10-28 01:00:12 +00001420 cur->doc = doc;
Daniel Veillardccb09631998-10-27 06:21:04 +00001421 cur->parent = NULL;
1422 cur->next = NULL;
1423 cur->prev = NULL;
1424 cur->childs = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001425 cur->last = NULL;
Daniel Veillardccb09631998-10-27 06:21:04 +00001426 cur->properties = NULL;
1427 if (name[0] == '&') {
1428 int len;
1429 name++;
1430 len = xmlStrlen(name);
1431 if (name[len - 1] == ';')
1432 cur->name = xmlStrndup(name, len - 1);
1433 else
1434 cur->name = xmlStrndup(name, len);
1435 } else
1436 cur->name = xmlStrdup(name);
1437 cur->ns = NULL;
1438 cur->nsDef = NULL;
1439
1440 ent = xmlGetDocEntity(doc, cur->name);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001441 if (ent != NULL) {
1442#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardccb09631998-10-27 06:21:04 +00001443 cur->content = ent->content;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001444#else
1445 /*
1446 * CJN 11.18.99 this might be a problem, since the xmlBuffer gets
1447 * a copy of this pointer. Let's hope we don't manipulate it
1448 * later
1449 */
1450 cur->content = xmlBufferCreateSize(0);
1451 xmlBufferSetAllocationScheme(cur->content,
1452 xmlGetBufferAllocationScheme());
1453 if (ent->content != NULL)
1454 xmlBufferAdd(cur->content, ent->content, -1);
1455#endif
1456 } else
Daniel Veillardccb09631998-10-27 06:21:04 +00001457 cur->content = NULL;
Daniel Veillard00fdf371999-10-08 09:40:39 +00001458#ifndef XML_WITHOUT_CORBA
1459 cur->_private = NULL;
1460 cur->vepv = NULL;
1461#endif
Daniel Veillardccb09631998-10-27 06:21:04 +00001462 return(cur);
1463}
1464
1465/**
Daniel Veillard97b58771998-10-20 06:14:16 +00001466 * xmlNewDocText:
1467 * @doc: the document
1468 * @content: the text content
1469 *
1470 * Creation of a new text node within a document.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001471 * Returns a pointer to the new node object.
Daniel Veillard97b58771998-10-20 06:14:16 +00001472 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001473xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001474xmlNewDocText(xmlDocPtr doc, const xmlChar *content) {
Daniel Veillard0bef1311998-10-14 02:36:47 +00001475 xmlNodePtr cur;
1476
1477 cur = xmlNewText(content);
1478 if (cur != NULL) cur->doc = doc;
1479 return(cur);
1480}
1481
Daniel Veillard97b58771998-10-20 06:14:16 +00001482/**
Daniel Veillardccb09631998-10-27 06:21:04 +00001483 * xmlNewTextLen:
Daniel Veillard97b58771998-10-20 06:14:16 +00001484 * @content: the text content
1485 * @len: the text len.
1486 *
1487 * Creation of a new text node with an extra parameter for the content's lenght
Daniel Veillard1e346af1999-02-22 10:33:01 +00001488 * Returns a pointer to the new node object.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001489 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001490xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001491xmlNewTextLen(const xmlChar *content, int len) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001492 xmlNodePtr cur;
1493
1494 /*
1495 * Allocate a new node and fill the fields.
1496 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00001497 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillard260a68f1998-08-13 03:39:55 +00001498 if (cur == NULL) {
1499 fprintf(stderr, "xmlNewText : malloc failed\n");
1500 return(NULL);
1501 }
1502
Daniel Veillard33942841998-10-18 19:12:41 +00001503 cur->type = XML_TEXT_NODE;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001504 cur->doc = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001505 cur->parent = NULL;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001506 cur->prev = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001507 cur->next = NULL;
1508 cur->childs = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001509 cur->last = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001510 cur->properties = NULL;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001511 cur->type = XML_TEXT_NODE;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001512 cur->name = xmlStrdup(xmlStringText);
1513 cur->ns = NULL;
1514 cur->nsDef = NULL;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001515 if (content != NULL) {
1516#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard260a68f1998-08-13 03:39:55 +00001517 cur->content = xmlStrndup(content, len);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001518#else
1519 cur->content = xmlBufferCreateSize(len);
1520 xmlBufferSetAllocationScheme(cur->content,
1521 xmlGetBufferAllocationScheme());
1522 xmlBufferAdd(cur->content, content, len);
1523#endif
1524 } else
Daniel Veillard260a68f1998-08-13 03:39:55 +00001525 cur->content = NULL;
Daniel Veillard00fdf371999-10-08 09:40:39 +00001526#ifndef XML_WITHOUT_CORBA
1527 cur->_private = NULL;
1528 cur->vepv = NULL;
1529#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00001530 return(cur);
1531}
1532
Daniel Veillard97b58771998-10-20 06:14:16 +00001533/**
1534 * xmlNewDocTextLen:
1535 * @doc: the document
1536 * @content: the text content
1537 * @len: the text len.
1538 *
1539 * Creation of a new text node with an extra content lenght parameter. The
1540 * text node pertain to a given document.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001541 * Returns a pointer to the new node object.
Daniel Veillard97b58771998-10-20 06:14:16 +00001542 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001543xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001544xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) {
Daniel Veillard0bef1311998-10-14 02:36:47 +00001545 xmlNodePtr cur;
1546
1547 cur = xmlNewTextLen(content, len);
1548 if (cur != NULL) cur->doc = doc;
1549 return(cur);
1550}
1551
Daniel Veillard97b58771998-10-20 06:14:16 +00001552/**
1553 * xmlNewComment:
1554 * @content: the comment content
1555 *
1556 * Creation of a new node containing a comment.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001557 * Returns a pointer to the new node object.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001558 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001559xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001560xmlNewComment(const xmlChar *content) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001561 xmlNodePtr cur;
1562
1563 /*
1564 * Allocate a new node and fill the fields.
1565 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00001566 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillard260a68f1998-08-13 03:39:55 +00001567 if (cur == NULL) {
1568 fprintf(stderr, "xmlNewComment : malloc failed\n");
1569 return(NULL);
1570 }
1571
Daniel Veillard33942841998-10-18 19:12:41 +00001572 cur->type = XML_COMMENT_NODE;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001573 cur->doc = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001574 cur->parent = NULL;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001575 cur->prev = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001576 cur->next = NULL;
1577 cur->childs = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001578 cur->last = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001579 cur->properties = NULL;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001580 cur->type = XML_COMMENT_NODE;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001581 cur->name = xmlStrdup(xmlStringText);
1582 cur->ns = NULL;
1583 cur->nsDef = NULL;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001584 if (content != NULL) {
1585#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard260a68f1998-08-13 03:39:55 +00001586 cur->content = xmlStrdup(content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001587#else
1588 cur->content = xmlBufferCreateSize(0);
1589 xmlBufferSetAllocationScheme(cur->content,
1590 xmlGetBufferAllocationScheme());
1591 xmlBufferAdd(cur->content, content, -1);
1592#endif
1593 } else
Daniel Veillard260a68f1998-08-13 03:39:55 +00001594 cur->content = NULL;
Daniel Veillard00fdf371999-10-08 09:40:39 +00001595#ifndef XML_WITHOUT_CORBA
1596 cur->_private = NULL;
1597 cur->vepv = NULL;
1598#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00001599 return(cur);
1600}
1601
Daniel Veillard97b58771998-10-20 06:14:16 +00001602/**
Daniel Veillardb05deb71999-08-10 19:04:08 +00001603 * xmlNewCDataBlock:
1604 * @doc: the document
1605 * @content: the CData block content content
1606 * @len: the length of the block
1607 *
1608 * Creation of a new node containing a CData block.
1609 * Returns a pointer to the new node object.
1610 */
1611xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001612xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) {
Daniel Veillardb05deb71999-08-10 19:04:08 +00001613 xmlNodePtr cur;
1614
1615 /*
1616 * Allocate a new node and fill the fields.
1617 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00001618 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillardb05deb71999-08-10 19:04:08 +00001619 if (cur == NULL) {
1620 fprintf(stderr, "xmlNewCDataBlock : malloc failed\n");
1621 return(NULL);
1622 }
1623
1624 cur->type = XML_CDATA_SECTION_NODE;
1625 cur->doc = NULL;
1626 cur->parent = NULL;
1627 cur->prev = NULL;
1628 cur->next = NULL;
1629 cur->childs = NULL;
1630 cur->last = NULL;
1631 cur->properties = NULL;
1632 cur->name = xmlStrdup(xmlStringText);
1633 cur->ns = NULL;
1634 cur->nsDef = NULL;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001635 if (content != NULL) {
1636#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardb05deb71999-08-10 19:04:08 +00001637 cur->content = xmlStrndup(content, len);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001638#else
1639 cur->content = xmlBufferCreateSize(len);
1640 xmlBufferSetAllocationScheme(cur->content,
1641 xmlGetBufferAllocationScheme());
1642 xmlBufferAdd(cur->content, content, len);
1643#endif
Daniel Veillardb05deb71999-08-10 19:04:08 +00001644 } else
1645 cur->content = NULL;
Daniel Veillard00fdf371999-10-08 09:40:39 +00001646#ifndef XML_WITHOUT_CORBA
1647 cur->_private = NULL;
1648 cur->vepv = NULL;
1649#endif
Daniel Veillardb05deb71999-08-10 19:04:08 +00001650 return(cur);
1651}
1652
1653/**
Daniel Veillard1e346af1999-02-22 10:33:01 +00001654 * xmlNewDocComment:
Daniel Veillard97b58771998-10-20 06:14:16 +00001655 * @doc: the document
1656 * @content: the comment content
1657 *
1658 * Creation of a new node containing a commentwithin a document.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001659 * Returns a pointer to the new node object.
Daniel Veillard97b58771998-10-20 06:14:16 +00001660 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001661xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001662xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) {
Daniel Veillard0bef1311998-10-14 02:36:47 +00001663 xmlNodePtr cur;
1664
1665 cur = xmlNewComment(content);
1666 if (cur != NULL) cur->doc = doc;
1667 return(cur);
1668}
1669
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001670
Daniel Veillard97b58771998-10-20 06:14:16 +00001671/**
1672 * xmlNewChild:
1673 * @parent: the parent node
1674 * @ns: a namespace if any
1675 * @name: the name of the child
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001676 * @content: the XML content of the child if any.
Daniel Veillard97b58771998-10-20 06:14:16 +00001677 *
Daniel Veillard97b58771998-10-20 06:14:16 +00001678 * Creation of a new child element, added at the end of @parent childs list.
Daniel Veillardccb09631998-10-27 06:21:04 +00001679 * @ns and @content parameters are optionnal (NULL). If content is non NULL,
1680 * a child list containing the TEXTs and ENTITY_REFs node will be created.
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001681 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
1682 * references, but XML special chars need to be escaped first by using
1683 * xmlEncodeEntitiesReentrant(). Use xmlNewTextChild() if entities
1684 * support is not needed.
1685 *
Daniel Veillard1e346af1999-02-22 10:33:01 +00001686 * Returns a pointer to the new node object.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001687 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001688xmlNodePtr
1689xmlNewChild(xmlNodePtr parent, xmlNsPtr ns,
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001690 const xmlChar *name, const xmlChar *content) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001691 xmlNodePtr cur, prev;
1692
1693 if (parent == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001694#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00001695 fprintf(stderr, "xmlNewChild : parent == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001696#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00001697 return(NULL);
1698 }
1699
1700 if (name == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001701#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00001702 fprintf(stderr, "xmlNewChild : name == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001703#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00001704 return(NULL);
1705 }
1706
1707 /*
1708 * Allocate a new node
1709 */
1710 if (ns == NULL)
Daniel Veillardccb09631998-10-27 06:21:04 +00001711 cur = xmlNewDocNode(parent->doc, parent->ns, name, content);
Daniel Veillard260a68f1998-08-13 03:39:55 +00001712 else
Daniel Veillardccb09631998-10-27 06:21:04 +00001713 cur = xmlNewDocNode(parent->doc, ns, name, content);
Daniel Veillard260a68f1998-08-13 03:39:55 +00001714 if (cur == NULL) return(NULL);
1715
1716 /*
1717 * add the new element at the end of the childs list.
1718 */
Daniel Veillardccb09631998-10-27 06:21:04 +00001719 cur->type = XML_ELEMENT_NODE;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001720 cur->parent = parent;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001721 cur->doc = parent->doc;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001722 if (parent->childs == NULL) {
1723 parent->childs = cur;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001724 parent->last = cur;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001725 } else {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001726 prev = parent->last;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001727 prev->next = cur;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001728 cur->prev = prev;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001729 parent->last = cur;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001730 }
1731
1732 return(cur);
1733}
1734
Daniel Veillard97b58771998-10-20 06:14:16 +00001735/**
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001736 * xmlAddNextSibling:
1737 * @cur: the child node
1738 * @elem: the new node
1739 *
1740 * Add a new element @elem as the next siblings of @cur
1741 * If the new element was already inserted in a document it is
1742 * first unlinked from its existing context.
1743 *
1744 * Returns the new element or NULL in case of error.
1745 */
1746xmlNodePtr
1747xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) {
1748 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001749#ifdef DEBUG_TREE
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001750 fprintf(stderr, "xmlAddNextSibling : cur == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001751#endif
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001752 return(NULL);
1753 }
1754 if (elem == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001755#ifdef DEBUG_TREE
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001756 fprintf(stderr, "xmlAddNextSibling : elem == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001757#endif
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001758 return(NULL);
1759 }
1760
1761 xmlUnlinkNode(elem);
1762 elem->doc = cur->doc;
1763 elem->parent = cur->parent;
1764 elem->next = cur;
1765 elem->prev = cur->prev;
1766 cur->prev = elem;
1767 if (elem->prev != NULL)
1768 elem->prev->next = elem;
1769 if ((elem->parent != NULL) && (elem->parent->childs == cur))
1770 elem->parent->childs = elem;
1771 return(elem);
1772}
1773
1774/**
1775 * xmlAddPrevSibling:
1776 * @cur: the child node
1777 * @elem: the new node
1778 *
1779 * Add a new element @elem as the previous siblings of @cur
1780 * If the new element was already inserted in a document it is
1781 * first unlinked from its existing context.
1782 *
1783 * Returns the new element or NULL in case of error.
1784 */
1785xmlNodePtr
1786xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) {
1787 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001788#ifdef DEBUG_TREE
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001789 fprintf(stderr, "xmlAddPrevSibling : cur == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001790#endif
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001791 return(NULL);
1792 }
1793 if (elem == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001794#ifdef DEBUG_TREE
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001795 fprintf(stderr, "xmlAddPrevSibling : elem == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001796#endif
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001797 return(NULL);
1798 }
1799
1800 xmlUnlinkNode(elem);
1801 elem->doc = cur->doc;
1802 elem->parent = cur->parent;
1803 elem->prev = cur;
1804 elem->next = cur->next;
1805 cur->next = elem;
1806 if (elem->next != NULL)
1807 elem->next->prev = elem;
1808 if ((elem->parent != NULL) && (elem->parent->last == cur))
1809 elem->parent->last = elem;
1810 return(elem);
1811}
1812
1813/**
Daniel Veillardb96e6431999-08-29 21:02:19 +00001814 * xmlAddSibling:
1815 * @cur: the child node
1816 * @elem: the new node
1817 *
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001818 * Add a new element @elem to the list of siblings of @cur
1819 * If the new element was already inserted in a document it is
1820 * first unlinked from its existing context.
1821 *
1822 * Returns the new element or NULL in case of error.
Daniel Veillardb96e6431999-08-29 21:02:19 +00001823 */
1824xmlNodePtr
1825xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) {
1826 xmlNodePtr parent;
1827
1828 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001829#ifdef DEBUG_TREE
Daniel Veillardb96e6431999-08-29 21:02:19 +00001830 fprintf(stderr, "xmlAddSibling : cur == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001831#endif
Daniel Veillardb96e6431999-08-29 21:02:19 +00001832 return(NULL);
1833 }
1834
1835 if (elem == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001836#ifdef DEBUG_TREE
Daniel Veillardb96e6431999-08-29 21:02:19 +00001837 fprintf(stderr, "xmlAddSibling : elem == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001838#endif
Daniel Veillardb96e6431999-08-29 21:02:19 +00001839 return(NULL);
1840 }
1841
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001842 /*
1843 * Constant time is we can rely on the ->parent->last to find
1844 * the last sibling.
1845 */
1846 if ((cur->parent != NULL) &&
1847 (cur->parent->childs != NULL) &&
1848 (cur->parent->last != NULL) &&
1849 (cur->parent->last->next == NULL)) {
1850 cur = cur->parent->last;
1851 } else {
1852 while (cur->next != NULL) cur = cur->next;
Daniel Veillardb96e6431999-08-29 21:02:19 +00001853 }
1854
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001855 xmlUnlinkNode(elem);
Daniel Veillardb96e6431999-08-29 21:02:19 +00001856 if (elem->doc == NULL)
1857 elem->doc = cur->doc; /* the parent may not be linked to a doc ! */
1858
1859 parent = cur->parent;
1860 elem->prev = cur;
1861 elem->next = NULL;
1862 elem->parent = parent;
1863 cur->next = elem;
1864 if (parent != NULL)
1865 parent->last = elem;
1866
1867 return(elem);
1868}
1869
1870/**
Daniel Veillard97b58771998-10-20 06:14:16 +00001871 * xmlAddChild:
1872 * @parent: the parent node
1873 * @cur: the child node
1874 *
1875 * Add a new child element, to @parent, at the end of the child list.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001876 * Returns the child or NULL in case of error.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001877 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001878xmlNodePtr
1879xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001880 xmlNodePtr prev;
1881
1882 if (parent == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001883#ifdef DEBUG_TREE
Daniel Veillard10a2c651999-12-12 13:03:50 +00001884 fprintf(stderr, "xmlAddChild : parent == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001885#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00001886 return(NULL);
1887 }
1888
1889 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001890#ifdef DEBUG_TREE
Daniel Veillard10a2c651999-12-12 13:03:50 +00001891 fprintf(stderr, "xmlAddChild : child == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001892#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00001893 return(NULL);
1894 }
1895
Daniel Veillard0bef1311998-10-14 02:36:47 +00001896 if ((cur->doc != NULL) && (parent->doc != NULL) &&
1897 (cur->doc != parent->doc)) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001898#ifdef DEBUG_TREE
Daniel Veillard0bef1311998-10-14 02:36:47 +00001899 fprintf(stderr, "Elements moved to a different document\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001900#endif
Daniel Veillard0bef1311998-10-14 02:36:47 +00001901 }
1902
Daniel Veillard260a68f1998-08-13 03:39:55 +00001903 /*
1904 * add the new element at the end of the childs list.
1905 */
1906 cur->parent = parent;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001907 cur->doc = parent->doc; /* the parent may not be linked to a doc ! */
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001908
Daniel Veillardccb09631998-10-27 06:21:04 +00001909 /*
1910 * Handle the case where parent->content != NULL, in that case it will
1911 * create a intermediate TEXT node.
1912 */
1913 if (parent->content != NULL) {
1914 xmlNodePtr text;
1915
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001916#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardccb09631998-10-27 06:21:04 +00001917 text = xmlNewDocText(parent->doc, parent->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001918#else
1919 text = xmlNewDocText(parent->doc, xmlBufferContent(parent->content));
1920#endif
Daniel Veillardccb09631998-10-27 06:21:04 +00001921 if (text != NULL) {
1922 text->next = parent->childs;
1923 if (text->next != NULL)
1924 text->next->prev = text;
1925 parent->childs = text;
Daniel Veillard1e346af1999-02-22 10:33:01 +00001926 UPDATE_LAST_CHILD(parent)
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001927#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard6454aec1999-09-02 22:04:43 +00001928 xmlFree(parent->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001929#else
1930 xmlBufferFree(parent->content);
1931#endif
Daniel Veillardccb09631998-10-27 06:21:04 +00001932 parent->content = NULL;
1933 }
1934 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00001935 if (parent->childs == NULL) {
1936 parent->childs = cur;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001937 parent->last = cur;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001938 } else {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001939 prev = parent->last;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001940 prev->next = cur;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001941 cur->prev = prev;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001942 parent->last = cur;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001943 }
1944
1945 return(cur);
1946}
1947
Daniel Veillard97b58771998-10-20 06:14:16 +00001948/**
1949 * xmlGetLastChild:
1950 * @parent: the parent node
1951 *
1952 * Search the last child of a node.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001953 * Returns the last child or NULL if none.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001954 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001955xmlNodePtr
1956xmlGetLastChild(xmlNodePtr parent) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001957 if (parent == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001958#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00001959 fprintf(stderr, "xmlGetLastChild : parent == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001960#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00001961 return(NULL);
1962 }
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001963 return(parent->last);
Daniel Veillard260a68f1998-08-13 03:39:55 +00001964}
1965
Daniel Veillard97b58771998-10-20 06:14:16 +00001966/**
1967 * xmlFreeNodeList:
1968 * @cur: the first node in the list
1969 *
1970 * Free a node and all its siblings, this is a recursive behaviour, all
1971 * the childs are freed too.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001972 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001973void
1974xmlFreeNodeList(xmlNodePtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001975 xmlNodePtr next;
1976 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001977#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00001978 fprintf(stderr, "xmlFreeNodeList : node == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001979#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00001980 return;
1981 }
1982 while (cur != NULL) {
1983 next = cur->next;
1984 xmlFreeNode(cur);
1985 cur = next;
1986 }
1987}
1988
Daniel Veillard97b58771998-10-20 06:14:16 +00001989/**
1990 * xmlFreeNode:
1991 * @cur: the node
1992 *
1993 * Free a node, this is a recursive behaviour, all the childs are freed too.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001994 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001995void
1996xmlFreeNode(xmlNodePtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001997 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001998#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00001999 fprintf(stderr, "xmlFreeNode : node == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00002000#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00002001 return;
2002 }
Daniel Veillardccb09631998-10-27 06:21:04 +00002003 cur->doc = NULL;
2004 cur->parent = NULL;
2005 cur->next = NULL;
2006 cur->prev = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00002007 if (cur->childs != NULL) xmlFreeNodeList(cur->childs);
Daniel Veillardccb09631998-10-27 06:21:04 +00002008 if (cur->properties != NULL) xmlFreePropList(cur->properties);
2009 if (cur->type != XML_ENTITY_REF_NODE)
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002010#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard6454aec1999-09-02 22:04:43 +00002011 if (cur->content != NULL) xmlFree(cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002012#else
2013 if (cur->content != NULL) xmlBufferFree(cur->content);
2014#endif
Daniel Veillard6454aec1999-09-02 22:04:43 +00002015 if (cur->name != NULL) xmlFree((char *) cur->name);
Daniel Veillard260a68f1998-08-13 03:39:55 +00002016 if (cur->nsDef != NULL) xmlFreeNsList(cur->nsDef);
2017 memset(cur, -1, sizeof(xmlNode));
Daniel Veillard6454aec1999-09-02 22:04:43 +00002018 xmlFree(cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +00002019}
2020
Daniel Veillard16253641998-10-28 22:58:05 +00002021/**
2022 * xmlUnlinkNode:
2023 * @cur: the node
2024 *
2025 * Unlink a node from it's current context, the node is not freed
2026 */
2027void
2028xmlUnlinkNode(xmlNodePtr cur) {
2029 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00002030#ifdef DEBUG_TREE
Daniel Veillard16253641998-10-28 22:58:05 +00002031 fprintf(stderr, "xmlUnlinkNode : node == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00002032#endif
Daniel Veillard16253641998-10-28 22:58:05 +00002033 return;
2034 }
2035 if ((cur->parent != NULL) && (cur->parent->childs == cur))
2036 cur->parent->childs = cur->next;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002037 if ((cur->parent != NULL) && (cur->parent->last == cur))
2038 cur->parent->last = cur->prev;
Daniel Veillard16253641998-10-28 22:58:05 +00002039 if (cur->next != NULL)
2040 cur->next->prev = cur->prev;
2041 if (cur->prev != NULL)
2042 cur->prev->next = cur->next;
2043 cur->next = cur->prev = NULL;
2044 cur->parent = NULL;
2045}
2046
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002047/**
2048 * xmlReplaceNode:
2049 * @old: the old node
2050 * @cur: the node
2051 *
2052 * Unlink the old node from it's current context, prune the new one
2053 * at the same place. If cur was already inserted in a document it is
2054 * first unlinked from its existing context.
2055 *
2056 * Returns the old node
2057 */
2058xmlNodePtr
2059xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) {
2060 if (old == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00002061#ifdef DEBUG_TREE
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002062 fprintf(stderr, "xmlReplaceNode : old == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00002063#endif
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002064 return(NULL);
2065 }
2066 if (cur == NULL) {
2067 xmlUnlinkNode(old);
2068 return(old);
2069 }
2070 xmlUnlinkNode(cur);
2071 cur->doc = old->doc;
2072 cur->parent = old->parent;
2073 cur->next = old->next;
2074 if (cur->next != NULL)
2075 cur->next->prev = cur;
2076 cur->prev = old->prev;
2077 if (cur->prev != NULL)
2078 cur->prev->next = cur;
2079 if (cur->parent != NULL) {
2080 if (cur->parent->childs == old)
2081 cur->parent->childs = cur;
2082 if (cur->parent->last == old)
2083 cur->parent->last = cur;
2084 }
2085 old->next = old->prev = NULL;
2086 old->parent = NULL;
2087 return(old);
2088}
2089
Daniel Veillard260a68f1998-08-13 03:39:55 +00002090/************************************************************************
2091 * *
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002092 * Copy operations *
2093 * *
2094 ************************************************************************/
2095
2096/**
2097 * xmlCopyNamespace:
2098 * @cur: the namespace
2099 *
2100 * Do a copy of the namespace.
2101 *
2102 * Returns: a new xmlNsPtr, or NULL in case of error.
2103 */
2104xmlNsPtr
2105xmlCopyNamespace(xmlNsPtr cur) {
2106 xmlNsPtr ret;
2107
2108 if (cur == NULL) return(NULL);
2109 switch (cur->type) {
2110 case XML_GLOBAL_NAMESPACE:
2111 ret = xmlNewGlobalNs(NULL, cur->href, cur->prefix);
2112 break;
2113 case XML_LOCAL_NAMESPACE:
2114 ret = xmlNewNs(NULL, cur->href, cur->prefix);
2115 break;
2116 default:
Daniel Veillardad8f99d2000-01-15 14:20:03 +00002117#ifdef DEBUG_TREE
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002118 fprintf(stderr, "xmlCopyNamespace: unknown type %d\n", cur->type);
Daniel Veillardad8f99d2000-01-15 14:20:03 +00002119#endif
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002120 return(NULL);
2121 }
2122 return(ret);
2123}
2124
2125/**
2126 * xmlCopyNamespaceList:
2127 * @cur: the first namespace
2128 *
2129 * Do a copy of an namespace list.
2130 *
2131 * Returns: a new xmlNsPtr, or NULL in case of error.
2132 */
2133xmlNsPtr
2134xmlCopyNamespaceList(xmlNsPtr cur) {
2135 xmlNsPtr ret = NULL;
2136 xmlNsPtr p = NULL,q;
2137
2138 while (cur != NULL) {
2139 q = xmlCopyNamespace(cur);
2140 if (p == NULL) {
2141 ret = p = q;
2142 } else {
2143 p->next = q;
2144 p = q;
2145 }
2146 cur = cur->next;
2147 }
2148 return(ret);
2149}
2150
2151/**
2152 * xmlCopyProp:
Daniel Veillardb96e6431999-08-29 21:02:19 +00002153 * @target: the element where the attribute will be grafted
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002154 * @cur: the attribute
2155 *
2156 * Do a copy of the attribute.
2157 *
2158 * Returns: a new xmlAttrPtr, or NULL in case of error.
2159 */
2160xmlAttrPtr
Daniel Veillardb96e6431999-08-29 21:02:19 +00002161xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002162 xmlAttrPtr ret;
2163
2164 if (cur == NULL) return(NULL);
2165 if (cur->val != NULL)
2166 ret = xmlNewDocProp(cur->val->doc, cur->name, NULL);
2167 else
2168 ret = xmlNewDocProp(NULL, cur->name, NULL);
2169 if (ret == NULL) return(NULL);
Daniel Veillardb96e6431999-08-29 21:02:19 +00002170
2171 if ((cur->ns != NULL) && (target != NULL)) {
2172 xmlNsPtr ns;
2173
2174 ns = xmlSearchNs(target->doc, target, cur->ns->prefix);
2175 ret->ns = ns;
2176 } else
2177 ret->ns = NULL;
2178
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002179 if (cur->val != NULL)
2180 ret->val = xmlCopyNodeList(cur->val);
2181 return(ret);
2182}
2183
2184/**
2185 * xmlCopyPropList:
Daniel Veillardb96e6431999-08-29 21:02:19 +00002186 * @target: the element where the attributes will be grafted
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002187 * @cur: the first attribute
2188 *
2189 * Do a copy of an attribute list.
2190 *
2191 * Returns: a new xmlAttrPtr, or NULL in case of error.
2192 */
2193xmlAttrPtr
Daniel Veillardb96e6431999-08-29 21:02:19 +00002194xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002195 xmlAttrPtr ret = NULL;
2196 xmlAttrPtr p = NULL,q;
2197
2198 while (cur != NULL) {
Daniel Veillardb96e6431999-08-29 21:02:19 +00002199 q = xmlCopyProp(target, cur);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002200 if (p == NULL) {
2201 ret = p = q;
2202 } else {
2203 p->next = q;
2204 p = q;
2205 }
2206 cur = cur->next;
2207 }
2208 return(ret);
2209}
2210
2211/*
Daniel Veillard11a48ec1999-11-23 10:40:46 +00002212 * NOTE abeut the CopyNode operations !
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002213 *
2214 * They are splitted into external and internal parts for one
2215 * tricky reason: namespaces. Doing a direct copy of a node
2216 * say RPM:Copyright without changing the namespace pointer to
2217 * something else can produce stale links. One way to do it is
2218 * to keep a reference counter but this doesn't work as soon
2219 * as one move the element or the subtree out of the scope of
2220 * the existing namespace. The actual solution seems to add
2221 * a copy of the namespace at the top of the copied tree if
2222 * not available in the subtree.
2223 * Hence two functions, the public front-end call the inner ones
2224 */
2225
2226static xmlNodePtr
2227xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent);
2228
2229static xmlNodePtr
2230xmlStaticCopyNode(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent,
2231 int recursive) {
2232 xmlNodePtr ret;
2233
2234 if (node == NULL) return(NULL);
2235 /*
2236 * Allocate a new node and fill the fields.
2237 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00002238 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002239 if (ret == NULL) {
2240 fprintf(stderr, "xmlStaticCopyNode : malloc failed\n");
2241 return(NULL);
2242 }
2243
2244 ret->type = node->type;
2245 ret->doc = doc;
2246 ret->parent = parent;
2247 ret->next = NULL;
2248 ret->prev = NULL;
2249 ret->childs = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002250 ret->last = NULL;
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002251 ret->properties = NULL;
2252 if (node->name != NULL)
2253 ret->name = xmlStrdup(node->name);
2254 else
2255 ret->name = NULL;
2256 ret->ns = NULL;
2257 ret->nsDef = NULL;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002258 if ((node->content != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
2259#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002260 ret->content = xmlStrdup(node->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002261#else
2262 ret->content = xmlBufferCreateSize(xmlBufferLength(node->content));
2263 xmlBufferSetAllocationScheme(ret->content,
2264 xmlGetBufferAllocationScheme());
2265 xmlBufferAdd(ret->content,
2266 xmlBufferContent(node->content),
2267 xmlBufferLength(node->content));
2268#endif
2269 } else
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002270 ret->content = NULL;
Daniel Veillard27d88741999-05-29 11:51:49 +00002271#ifndef XML_WITHOUT_CORBA
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002272 ret->_private = NULL;
2273 ret->vepv = NULL;
2274#endif
2275 if (parent != NULL)
2276 xmlAddChild(parent, ret);
2277
2278 if (!recursive) return(ret);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002279 if (node->nsDef != NULL)
2280 ret->nsDef = xmlCopyNamespaceList(node->nsDef);
2281
2282 if (node->ns != NULL) {
2283 xmlNsPtr ns;
2284
2285 ns = xmlSearchNs(doc, ret, node->ns->prefix);
2286 if (ns == NULL) {
2287 /*
2288 * Humm, we are copying an element whose namespace is defined
2289 * out of the new tree scope. Search it in the original tree
2290 * and add it at the top of the new tree
2291 */
2292 ns = xmlSearchNs(node->doc, node, node->ns->prefix);
2293 if (ns != NULL) {
2294 xmlNodePtr root = ret;
2295
2296 while (root->parent != NULL) root = root->parent;
2297 xmlNewNs(root, ns->href, ns->prefix);
2298 }
2299 } else {
2300 /*
2301 * reference the existing namespace definition in our own tree.
2302 */
2303 ret->ns = ns;
2304 }
2305 }
Daniel Veillardb96e6431999-08-29 21:02:19 +00002306 if (node->properties != NULL)
2307 ret->properties = xmlCopyPropList(ret, node->properties);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002308 if (node->childs != NULL)
2309 ret->childs = xmlStaticCopyNodeList(node->childs, doc, ret);
Daniel Veillard1e346af1999-02-22 10:33:01 +00002310 UPDATE_LAST_CHILD(ret)
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002311 return(ret);
2312}
2313
2314static xmlNodePtr
2315xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) {
2316 xmlNodePtr ret = NULL;
2317 xmlNodePtr p = NULL,q;
2318
2319 while (node != NULL) {
2320 q = xmlStaticCopyNode(node, doc, parent, 1);
2321 if (parent == NULL) {
2322 if (ret == NULL) ret = q;
2323 } else {
2324 if (ret == NULL) {
2325 q->prev = NULL;
2326 ret = p = q;
2327 } else {
2328 p->next = q;
2329 q->prev = p;
2330 p = q;
2331 }
2332 }
2333 node = node->next;
2334 }
2335 return(ret);
2336}
2337
2338/**
2339 * xmlCopyNode:
2340 * @node: the node
2341 * @recursive: if 1 do a recursive copy.
2342 *
2343 * Do a copy of the node.
2344 *
2345 * Returns: a new xmlNodePtr, or NULL in case of error.
2346 */
2347xmlNodePtr
2348xmlCopyNode(xmlNodePtr node, int recursive) {
2349 xmlNodePtr ret;
2350
2351 ret = xmlStaticCopyNode(node, NULL, NULL, recursive);
2352 return(ret);
2353}
2354
2355/**
2356 * xmlCopyNodeList:
2357 * @node: the first node in the list.
2358 *
2359 * Do a recursive copy of the node list.
2360 *
2361 * Returns: a new xmlNodePtr, or NULL in case of error.
2362 */
2363xmlNodePtr xmlCopyNodeList(xmlNodePtr node) {
2364 xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL);
2365 return(ret);
2366}
2367
2368/**
2369 * xmlCopyElement:
2370 * @elem: the element
2371 *
2372 * Do a copy of the element definition.
2373 *
2374 * Returns: a new xmlElementPtr, or NULL in case of error.
2375xmlElementPtr
2376xmlCopyElement(xmlElementPtr elem) {
2377 xmlElementPtr ret;
2378
2379 if (elem == NULL) return(NULL);
2380 ret = xmlNewDocElement(elem->doc, elem->ns, elem->name, elem->content);
2381 if (ret == NULL) return(NULL);
2382 if (!recursive) return(ret);
2383 if (elem->properties != NULL)
2384 ret->properties = xmlCopyPropList(elem->properties);
2385
2386 if (elem->nsDef != NULL)
2387 ret->nsDef = xmlCopyNamespaceList(elem->nsDef);
2388 if (elem->childs != NULL)
2389 ret->childs = xmlCopyElementList(elem->childs);
2390 return(ret);
2391}
2392 */
2393
2394/**
2395 * xmlCopyDtd:
2396 * @dtd: the dtd
2397 *
2398 * Do a copy of the dtd.
2399 *
2400 * Returns: a new xmlDtdPtr, or NULL in case of error.
2401 */
2402xmlDtdPtr
2403xmlCopyDtd(xmlDtdPtr dtd) {
2404 xmlDtdPtr ret;
2405
2406 if (dtd == NULL) return(NULL);
2407 ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID);
2408 if (ret == NULL) return(NULL);
2409 if (dtd->entities != NULL)
2410 ret->entities = (void *) xmlCopyEntitiesTable(
2411 (xmlEntitiesTablePtr) dtd->entities);
Daniel Veillard1e346af1999-02-22 10:33:01 +00002412 if (dtd->notations != NULL)
2413 ret->notations = (void *) xmlCopyNotationTable(
2414 (xmlNotationTablePtr) dtd->notations);
2415 if (dtd->elements != NULL)
2416 ret->elements = (void *) xmlCopyElementTable(
2417 (xmlElementTablePtr) dtd->elements);
2418 if (dtd->attributes != NULL)
2419 ret->attributes = (void *) xmlCopyAttributeTable(
2420 (xmlAttributeTablePtr) dtd->attributes);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002421 return(ret);
2422}
2423
2424/**
2425 * xmlCopyDoc:
2426 * @doc: the document
2427 * @recursive: if 1 do a recursive copy.
2428 *
2429 * Do a copy of the document info. If recursive, the content tree will
2430 * be copied too as well as Dtd, namespaces and entities.
2431 *
2432 * Returns: a new xmlDocPtr, or NULL in case of error.
2433 */
2434xmlDocPtr
2435xmlCopyDoc(xmlDocPtr doc, int recursive) {
2436 xmlDocPtr ret;
2437
2438 if (doc == NULL) return(NULL);
2439 ret = xmlNewDoc(doc->version);
2440 if (ret == NULL) return(NULL);
2441 if (doc->name != NULL)
Daniel Veillard6454aec1999-09-02 22:04:43 +00002442 ret->name = xmlMemStrdup(doc->name);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002443 if (doc->encoding != NULL)
2444 ret->encoding = xmlStrdup(doc->encoding);
2445 ret->compression = doc->compression;
2446 ret->standalone = doc->standalone;
2447 if (!recursive) return(ret);
2448
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002449 if (doc->intSubset != NULL)
2450 ret->intSubset = xmlCopyDtd(doc->intSubset);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002451 if (doc->oldNs != NULL)
2452 ret->oldNs = xmlCopyNamespaceList(doc->oldNs);
2453 if (doc->root != NULL)
2454 ret->root = xmlStaticCopyNodeList(doc->root, ret, NULL);
2455 return(ret);
2456}
2457
2458/************************************************************************
2459 * *
Daniel Veillard260a68f1998-08-13 03:39:55 +00002460 * Content access functions *
2461 * *
2462 ************************************************************************/
2463
Daniel Veillard97b58771998-10-20 06:14:16 +00002464/**
Daniel Veillard944b5ff1999-12-15 19:08:24 +00002465 * xmlDocGetRootElement:
2466 * @doc: the document
2467 *
2468 * Get the root element of the document (doc->root is a list
2469 * containing possibly comments, PIs, etc ...).
2470 *
2471 * Returns the xmlNodePtr for the root or NULL
2472 */
2473xmlNodePtr
2474xmlDocGetRootElement(xmlDocPtr doc) {
2475 xmlNodePtr ret;
2476
2477 if (doc == NULL) return(NULL);
2478 ret = doc->root;
2479 while (ret != NULL) {
2480 if (ret->type == XML_ELEMENT_NODE)
2481 return(ret);
2482 ret = ret->next;
2483 }
2484 return(ret);
2485}
2486
2487/**
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002488 * xmlDocSetRootElement:
2489 * @doc: the document
2490 * @root: the new document root element
2491 *
2492 * Set the root element of the document (doc->root is a list
2493 * containing possibly comments, PIs, etc ...).
2494 *
2495 * Returns the old root element if any was found
2496 */
2497xmlNodePtr
2498xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) {
2499 xmlNodePtr old = NULL;
2500
2501 if (doc == NULL) return(NULL);
2502 old = doc->root;
2503 while (old != NULL) {
2504 if (old->type == XML_ELEMENT_NODE)
2505 break;
2506 old = old->next;
2507 }
2508 if (old == NULL) {
2509 if (doc->root == NULL) {
2510 doc->root = root;
2511 } else {
2512 xmlAddSibling(doc->root, root);
2513 }
2514 } else {
2515 xmlReplaceNode(old, root);
2516 }
2517 return(old);
2518}
2519
2520/**
Daniel Veillardb96e6431999-08-29 21:02:19 +00002521 * xmlNodeSetLang:
2522 * @cur: the node being changed
2523 * @lang: the langage description
2524 *
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002525 * Set the language of a node, i.e. the values of the xml:lang
2526 * attribute.
Daniel Veillardb96e6431999-08-29 21:02:19 +00002527 */
2528void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002529xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) {
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002530 if (cur == NULL) return;
2531 switch(cur->type) {
2532 case XML_TEXT_NODE:
2533 case XML_CDATA_SECTION_NODE:
2534 case XML_COMMENT_NODE:
2535 case XML_DOCUMENT_NODE:
2536 case XML_DOCUMENT_TYPE_NODE:
2537 case XML_DOCUMENT_FRAG_NODE:
2538 case XML_NOTATION_NODE:
2539 case XML_HTML_DOCUMENT_NODE:
2540 return;
2541 case XML_ELEMENT_NODE:
2542 case XML_ATTRIBUTE_NODE:
2543 case XML_PI_NODE:
2544 case XML_ENTITY_REF_NODE:
2545 case XML_ENTITY_NODE:
2546 break;
2547 }
Daniel Veillardb96e6431999-08-29 21:02:19 +00002548 xmlSetProp(cur, BAD_CAST "xml:lang", lang);
2549}
2550
2551/**
2552 * xmlNodeGetLang:
2553 * @cur: the node being checked
2554 *
2555 * Searches the language of a node, i.e. the values of the xml:lang
2556 * attribute or the one carried by the nearest ancestor.
2557 *
2558 * Returns a pointer to the lang value, or NULL if not found
Daniel Veillarda819dac1999-11-24 18:04:22 +00002559 * It's up to the caller to free the memory.
Daniel Veillardb96e6431999-08-29 21:02:19 +00002560 */
Daniel Veillarda819dac1999-11-24 18:04:22 +00002561xmlChar *
Daniel Veillardb96e6431999-08-29 21:02:19 +00002562xmlNodeGetLang(xmlNodePtr cur) {
Daniel Veillarda819dac1999-11-24 18:04:22 +00002563 xmlChar *lang;
Daniel Veillardb96e6431999-08-29 21:02:19 +00002564
2565 while (cur != NULL) {
2566 lang = xmlGetProp(cur, BAD_CAST "xml:lang");
2567 if (lang != NULL)
2568 return(lang);
2569 cur = cur->parent;
2570 }
2571 return(NULL);
2572}
2573
2574/**
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002575 * xmlNodeSetName:
2576 * @cur: the node being changed
2577 * @name: the new tag name
2578 *
2579 * Searches the language of a node, i.e. the values of the xml:lang
2580 * attribute or the one carried by the nearest ancestor.
2581 */
2582void
2583xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) {
2584 if (cur == NULL) return;
2585 if (name == NULL) return;
2586 switch(cur->type) {
2587 case XML_TEXT_NODE:
2588 case XML_CDATA_SECTION_NODE:
2589 case XML_COMMENT_NODE:
2590 case XML_DOCUMENT_NODE:
2591 case XML_DOCUMENT_TYPE_NODE:
2592 case XML_DOCUMENT_FRAG_NODE:
2593 case XML_NOTATION_NODE:
2594 case XML_HTML_DOCUMENT_NODE:
2595 return;
2596 case XML_ELEMENT_NODE:
2597 case XML_ATTRIBUTE_NODE:
2598 case XML_PI_NODE:
2599 case XML_ENTITY_REF_NODE:
2600 case XML_ENTITY_NODE:
2601 break;
2602 }
2603 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
2604 cur->name = xmlStrdup(name);
2605}
2606
2607/**
Daniel Veillard10a2c651999-12-12 13:03:50 +00002608 * xmlNodeGetBase:
2609 * @doc: the document the node pertains to
2610 * @cur: the node being checked
2611 *
2612 * Searches for the BASE URL. The code should work on both XML
2613 * and HTML document even if base mechanisms are completely different.
2614 *
2615 * Returns a pointer to the base URL, or NULL if not found
2616 * It's up to the caller to free the memory.
2617 */
2618xmlChar *
2619xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) {
2620 xmlChar *base;
2621
2622 if ((cur == NULL) && (doc == NULL))
2623 return(NULL);
2624 if (doc == NULL) doc = cur->doc;
2625 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
2626 cur = doc->root;
2627 while ((cur != NULL) && (cur->name != NULL)) {
2628 if (cur->type != XML_ELEMENT_NODE) {
2629 cur = cur->next;
2630 continue;
2631 }
2632 if ((!xmlStrcmp(cur->name, BAD_CAST "html")) ||
2633 (!xmlStrcmp(cur->name, BAD_CAST "HTML"))) {
2634 cur = cur->childs;
2635 continue;
2636 }
2637 if ((!xmlStrcmp(cur->name, BAD_CAST "head")) ||
2638 (!xmlStrcmp(cur->name, BAD_CAST "HEAD"))) {
2639 cur = cur->childs;
2640 continue;
2641 }
2642 if ((!xmlStrcmp(cur->name, BAD_CAST "base")) ||
2643 (!xmlStrcmp(cur->name, BAD_CAST "BASE"))) {
2644 base = xmlGetProp(cur, BAD_CAST "href");
2645 if (base != NULL) return(base);
2646 return(xmlGetProp(cur, BAD_CAST "HREF"));
2647 }
2648 }
2649 return(NULL);
2650 }
2651 while (cur != NULL) {
2652 base = xmlGetProp(cur, BAD_CAST "xml:base");
2653 if (base != NULL)
2654 return(base);
2655 cur = cur->parent;
2656 }
2657 return(NULL);
2658}
2659
2660/**
Daniel Veillard16253641998-10-28 22:58:05 +00002661 * xmlNodeGetContent:
2662 * @cur: the node being read
2663 *
2664 * Read the value of a node, this can be either the text carried
2665 * directly by this node if it's a TEXT node or the aggregate string
2666 * of the values carried by this node child's (TEXT and ENTITY_REF).
2667 * Entity references are substitued.
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002668 * Returns a new xmlChar * or NULL if no content is available.
Daniel Veillard5099ae81999-04-21 20:12:07 +00002669 * It's up to the caller to free the memory.
Daniel Veillard16253641998-10-28 22:58:05 +00002670 */
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002671xmlChar *
Daniel Veillard16253641998-10-28 22:58:05 +00002672xmlNodeGetContent(xmlNodePtr cur) {
2673 if (cur == NULL) return(NULL);
2674 switch (cur->type) {
2675 case XML_DOCUMENT_FRAG_NODE:
2676 case XML_ELEMENT_NODE:
2677 return(xmlNodeListGetString(cur->doc, cur->childs, 1));
2678 break;
Daniel Veillardb96e6431999-08-29 21:02:19 +00002679 case XML_ATTRIBUTE_NODE: {
2680 xmlAttrPtr attr = (xmlAttrPtr) cur;
2681 if (attr->node != NULL)
2682 return(xmlNodeListGetString(attr->node->doc, attr->val, 1));
2683 else
2684 return(xmlNodeListGetString(NULL, attr->val, 1));
2685 break;
2686 }
Daniel Veillarddbfd6411999-12-28 16:35:14 +00002687 case XML_COMMENT_NODE:
Daniel Veillardb96e6431999-08-29 21:02:19 +00002688 case XML_PI_NODE:
2689 if (cur->content != NULL)
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002690#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardb96e6431999-08-29 21:02:19 +00002691 return(xmlStrdup(cur->content));
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002692#else
2693 return(xmlStrdup(xmlBufferContent(cur->content)));
2694#endif
Daniel Veillardb96e6431999-08-29 21:02:19 +00002695 return(NULL);
Daniel Veillard16253641998-10-28 22:58:05 +00002696 case XML_ENTITY_REF_NODE:
Daniel Veillarddbfd6411999-12-28 16:35:14 +00002697 /*
2698 * Locate the entity, and get it's content
2699 * @@@
2700 */
2701 return(NULL);
Daniel Veillard16253641998-10-28 22:58:05 +00002702 case XML_ENTITY_NODE:
Daniel Veillard16253641998-10-28 22:58:05 +00002703 case XML_DOCUMENT_NODE:
Daniel Veillard7c1206f1999-10-14 09:10:25 +00002704 case XML_HTML_DOCUMENT_NODE:
Daniel Veillard16253641998-10-28 22:58:05 +00002705 case XML_DOCUMENT_TYPE_NODE:
2706 case XML_NOTATION_NODE:
2707 return(NULL);
Daniel Veillardb05deb71999-08-10 19:04:08 +00002708 case XML_CDATA_SECTION_NODE:
Daniel Veillard16253641998-10-28 22:58:05 +00002709 case XML_TEXT_NODE:
2710 if (cur->content != NULL)
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002711#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard16253641998-10-28 22:58:05 +00002712 return(xmlStrdup(cur->content));
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002713#else
2714 return(xmlStrdup(xmlBufferContent(cur->content)));
2715#endif
Daniel Veillard16253641998-10-28 22:58:05 +00002716 return(NULL);
2717 }
2718 return(NULL);
2719}
2720
2721/**
Daniel Veillard97b58771998-10-20 06:14:16 +00002722 * xmlNodeSetContent:
2723 * @cur: the node being modified
2724 * @content: the new value of the content
2725 *
2726 * Replace the content of a node.
Daniel Veillard260a68f1998-08-13 03:39:55 +00002727 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00002728void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002729xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00002730 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00002731#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00002732 fprintf(stderr, "xmlNodeSetContent : node == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00002733#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00002734 return;
2735 }
Daniel Veillard16253641998-10-28 22:58:05 +00002736 switch (cur->type) {
2737 case XML_DOCUMENT_FRAG_NODE:
2738 case XML_ELEMENT_NODE:
2739 if (cur->content != NULL) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002740#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard6454aec1999-09-02 22:04:43 +00002741 xmlFree(cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002742#else
2743 xmlBufferFree(cur->content);
2744#endif
Daniel Veillard16253641998-10-28 22:58:05 +00002745 cur->content = NULL;
2746 }
Daniel Veillard6454aec1999-09-02 22:04:43 +00002747 if (cur->childs != NULL) xmlFreeNodeList(cur->childs);
Daniel Veillard16253641998-10-28 22:58:05 +00002748 cur->childs = xmlStringGetNodeList(cur->doc, content);
Daniel Veillard1e346af1999-02-22 10:33:01 +00002749 UPDATE_LAST_CHILD(cur)
Daniel Veillard16253641998-10-28 22:58:05 +00002750 break;
2751 case XML_ATTRIBUTE_NODE:
2752 break;
2753 case XML_TEXT_NODE:
2754 case XML_CDATA_SECTION_NODE:
2755 case XML_ENTITY_REF_NODE:
2756 case XML_ENTITY_NODE:
2757 case XML_PI_NODE:
2758 case XML_COMMENT_NODE:
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002759 if (cur->content != NULL) {
2760#ifndef XML_USE_BUFFER_CONTENT
2761 xmlFree(cur->content);
2762#else
2763 xmlBufferFree(cur->content);
2764#endif
2765 }
Daniel Veillard6454aec1999-09-02 22:04:43 +00002766 if (cur->childs != NULL) xmlFreeNodeList(cur->childs);
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002767 cur->last = cur->childs = NULL;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002768 if (content != NULL) {
2769#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard16253641998-10-28 22:58:05 +00002770 cur->content = xmlStrdup(content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002771#else
2772 cur->content = xmlBufferCreateSize(0);
2773 xmlBufferSetAllocationScheme(cur->content,
2774 xmlGetBufferAllocationScheme());
2775 xmlBufferAdd(cur->content, content, -1);
2776#endif
2777 } else
Daniel Veillard16253641998-10-28 22:58:05 +00002778 cur->content = NULL;
Daniel Veillardb96e6431999-08-29 21:02:19 +00002779 break;
Daniel Veillard16253641998-10-28 22:58:05 +00002780 case XML_DOCUMENT_NODE:
Daniel Veillard7c1206f1999-10-14 09:10:25 +00002781 case XML_HTML_DOCUMENT_NODE:
Daniel Veillard16253641998-10-28 22:58:05 +00002782 case XML_DOCUMENT_TYPE_NODE:
2783 break;
2784 case XML_NOTATION_NODE:
2785 break;
2786 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00002787}
2788
Daniel Veillard97b58771998-10-20 06:14:16 +00002789/**
2790 * xmlNodeSetContentLen:
2791 * @cur: the node being modified
2792 * @content: the new value of the content
2793 * @len: the size of @content
2794 *
2795 * Replace the content of a node.
Daniel Veillard260a68f1998-08-13 03:39:55 +00002796 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00002797void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002798xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00002799 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00002800#ifdef DEBUG_TREE
Daniel Veillard16253641998-10-28 22:58:05 +00002801 fprintf(stderr, "xmlNodeSetContentLen : node == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00002802#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00002803 return;
2804 }
Daniel Veillard16253641998-10-28 22:58:05 +00002805 switch (cur->type) {
2806 case XML_DOCUMENT_FRAG_NODE:
2807 case XML_ELEMENT_NODE:
2808 if (cur->content != NULL) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002809#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard6454aec1999-09-02 22:04:43 +00002810 xmlFree(cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002811#else
2812 xmlBufferFree(cur->content);
2813#endif
Daniel Veillard16253641998-10-28 22:58:05 +00002814 cur->content = NULL;
2815 }
Daniel Veillard6454aec1999-09-02 22:04:43 +00002816 if (cur->childs != NULL) xmlFreeNodeList(cur->childs);
Daniel Veillard16253641998-10-28 22:58:05 +00002817 cur->childs = xmlStringLenGetNodeList(cur->doc, content, len);
Daniel Veillard1e346af1999-02-22 10:33:01 +00002818 UPDATE_LAST_CHILD(cur)
Daniel Veillard16253641998-10-28 22:58:05 +00002819 break;
2820 case XML_ATTRIBUTE_NODE:
2821 break;
2822 case XML_TEXT_NODE:
2823 case XML_CDATA_SECTION_NODE:
2824 case XML_ENTITY_REF_NODE:
2825 case XML_ENTITY_NODE:
2826 case XML_PI_NODE:
2827 case XML_COMMENT_NODE:
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002828 case XML_NOTATION_NODE:
2829 if (cur->content != NULL) {
2830#ifndef XML_USE_BUFFER_CONTENT
2831 xmlFree(cur->content);
2832#else
2833 xmlBufferFree(cur->content);
2834#endif
2835 }
Daniel Veillard6454aec1999-09-02 22:04:43 +00002836 if (cur->childs != NULL) xmlFreeNodeList(cur->childs);
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002837 cur->childs = cur->last = NULL;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002838 if (content != NULL) {
2839#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard16253641998-10-28 22:58:05 +00002840 cur->content = xmlStrndup(content, len);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002841#else
2842 cur->content = xmlBufferCreateSize(len);
2843 xmlBufferSetAllocationScheme(cur->content,
2844 xmlGetBufferAllocationScheme());
2845 xmlBufferAdd(cur->content, content, len);
2846#endif
2847 } else
Daniel Veillard16253641998-10-28 22:58:05 +00002848 cur->content = NULL;
Daniel Veillardb96e6431999-08-29 21:02:19 +00002849 break;
Daniel Veillard16253641998-10-28 22:58:05 +00002850 case XML_DOCUMENT_NODE:
Daniel Veillard7c1206f1999-10-14 09:10:25 +00002851 case XML_HTML_DOCUMENT_NODE:
Daniel Veillard16253641998-10-28 22:58:05 +00002852 case XML_DOCUMENT_TYPE_NODE:
2853 break;
Daniel Veillard260a68f1998-08-13 03:39:55 +00002854 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00002855}
2856
Daniel Veillard97b58771998-10-20 06:14:16 +00002857/**
2858 * xmlNodeAddContentLen:
2859 * @cur: the node being modified
2860 * @content: extra content
2861 * @len: the size of @content
2862 *
2863 * Append the extra substring to the node content.
Daniel Veillard260a68f1998-08-13 03:39:55 +00002864 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00002865void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002866xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00002867 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00002868#ifdef DEBUG_TREE
Daniel Veillard16253641998-10-28 22:58:05 +00002869 fprintf(stderr, "xmlNodeAddContentLen : node == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00002870#endif
Daniel Veillard16253641998-10-28 22:58:05 +00002871 return;
2872 }
2873 if (len <= 0) return;
2874 switch (cur->type) {
2875 case XML_DOCUMENT_FRAG_NODE:
2876 case XML_ELEMENT_NODE: {
2877 xmlNodePtr last = NULL, new;
2878
2879 if (cur->childs != NULL) {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002880 last = cur->last;
Daniel Veillard16253641998-10-28 22:58:05 +00002881 } else {
2882 if (cur->content != NULL) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002883#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard16253641998-10-28 22:58:05 +00002884 cur->childs = xmlStringGetNodeList(cur->doc, cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002885#else
2886 cur->childs = xmlStringGetNodeList(cur->doc,
2887 xmlBufferContent(cur->content));
2888#endif
Daniel Veillard1e346af1999-02-22 10:33:01 +00002889 UPDATE_LAST_CHILD(cur)
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002890#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard6454aec1999-09-02 22:04:43 +00002891 xmlFree(cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002892#else
2893 xmlBufferFree(cur->content);
2894#endif
Daniel Veillard16253641998-10-28 22:58:05 +00002895 cur->content = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002896 last = cur->last;
Daniel Veillard16253641998-10-28 22:58:05 +00002897 }
2898 }
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002899 new = xmlNewTextLen(content, len);
Daniel Veillard16253641998-10-28 22:58:05 +00002900 if (new != NULL) {
2901 xmlAddChild(cur, new);
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002902 if ((last != NULL) && (last->next == new)) {
Daniel Veillard16253641998-10-28 22:58:05 +00002903 xmlTextMerge(last, new);
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002904 }
Daniel Veillard16253641998-10-28 22:58:05 +00002905 }
2906 break;
2907 }
2908 case XML_ATTRIBUTE_NODE:
2909 break;
2910 case XML_TEXT_NODE:
2911 case XML_CDATA_SECTION_NODE:
2912 case XML_ENTITY_REF_NODE:
2913 case XML_ENTITY_NODE:
2914 case XML_PI_NODE:
2915 case XML_COMMENT_NODE:
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002916 case XML_NOTATION_NODE:
2917 if (content != NULL) {
2918#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard16253641998-10-28 22:58:05 +00002919 cur->content = xmlStrncat(cur->content, content, len);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002920#else
2921 xmlBufferAdd(cur->content, content, len);
2922#endif
2923 }
Daniel Veillard16253641998-10-28 22:58:05 +00002924 case XML_DOCUMENT_NODE:
Daniel Veillard7c1206f1999-10-14 09:10:25 +00002925 case XML_HTML_DOCUMENT_NODE:
Daniel Veillard16253641998-10-28 22:58:05 +00002926 case XML_DOCUMENT_TYPE_NODE:
2927 break;
Daniel Veillard16253641998-10-28 22:58:05 +00002928 }
2929}
2930
2931/**
2932 * xmlNodeAddContent:
2933 * @cur: the node being modified
2934 * @content: extra content
2935 *
2936 * Append the extra substring to the node content.
2937 */
2938void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002939xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) {
Daniel Veillard16253641998-10-28 22:58:05 +00002940 int len;
2941
2942 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00002943#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00002944 fprintf(stderr, "xmlNodeAddContent : node == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00002945#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00002946 return;
2947 }
Daniel Veillard16253641998-10-28 22:58:05 +00002948 if (content == NULL) return;
2949 len = xmlStrlen(content);
2950 xmlNodeAddContentLen(cur, content, len);
2951}
2952
2953/**
2954 * xmlTextMerge:
2955 * @first: the first text node
2956 * @second: the second text node being merged
2957 *
2958 * Merge two text nodes into one
Daniel Veillard1e346af1999-02-22 10:33:01 +00002959 * Returns the first text node augmented
Daniel Veillard16253641998-10-28 22:58:05 +00002960 */
2961xmlNodePtr
2962xmlTextMerge(xmlNodePtr first, xmlNodePtr second) {
2963 if (first == NULL) return(second);
2964 if (second == NULL) return(first);
2965 if (first->type != XML_TEXT_NODE) return(first);
2966 if (second->type != XML_TEXT_NODE) return(first);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002967#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard16253641998-10-28 22:58:05 +00002968 xmlNodeAddContent(first, second->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002969#else
2970 xmlNodeAddContent(first, xmlBufferContent(second->content));
2971#endif
Daniel Veillard16253641998-10-28 22:58:05 +00002972 xmlUnlinkNode(second);
2973 xmlFreeNode(second);
2974 return(first);
Daniel Veillard260a68f1998-08-13 03:39:55 +00002975}
2976
Daniel Veillard97b58771998-10-20 06:14:16 +00002977/**
Daniel Veillardb96e6431999-08-29 21:02:19 +00002978 * xmlGetNsList:
2979 * @doc: the document
2980 * @node: the current node
2981 *
2982 * Search all the namespace applying to a given element.
2983 * Returns an NULL terminated array of all the xmlNsPtr found
2984 * that need to be freed by the caller or NULL if no
2985 * namespace if defined
2986 */
2987xmlNsPtr *
2988xmlGetNsList(xmlDocPtr doc, xmlNodePtr node) {
2989 xmlNsPtr cur;
2990 xmlNsPtr *ret = NULL;
2991 int nbns = 0;
2992 int maxns = 10;
2993 int i;
2994
2995 while (node != NULL) {
2996 cur = node->nsDef;
2997 while (cur != NULL) {
2998 if (ret == NULL) {
Daniel Veillard6454aec1999-09-02 22:04:43 +00002999 ret = (xmlNsPtr *) xmlMalloc((maxns + 1) * sizeof(xmlNsPtr));
Daniel Veillardb96e6431999-08-29 21:02:19 +00003000 if (ret == NULL) {
3001 fprintf(stderr, "xmlGetNsList : out of memory!\n");
3002 return(NULL);
3003 }
3004 ret[nbns] = NULL;
3005 }
3006 for (i = 0;i < nbns;i++) {
3007 if ((cur->prefix == ret[i]->prefix) ||
3008 (!xmlStrcmp(cur->prefix, ret[i]->prefix))) break;
3009 }
3010 if (i >= nbns) {
3011 if (nbns >= maxns) {
3012 maxns *= 2;
Daniel Veillard6454aec1999-09-02 22:04:43 +00003013 ret = (xmlNsPtr *) xmlRealloc(ret,
Daniel Veillardb96e6431999-08-29 21:02:19 +00003014 (maxns + 1) * sizeof(xmlNsPtr));
3015 if (ret == NULL) {
3016 fprintf(stderr, "xmlGetNsList : realloc failed!\n");
3017 return(NULL);
3018 }
3019 }
3020 ret[nbns++] = cur;
3021 ret[nbns] = NULL;
3022 }
3023
3024 cur = cur->next;
3025 }
3026 node = node->parent;
3027 }
3028 return(ret);
3029}
3030
3031/**
Daniel Veillard97b58771998-10-20 06:14:16 +00003032 * xmlSearchNs:
3033 * @doc: the document
3034 * @node: the current node
3035 * @nameSpace: the namespace string
Daniel Veillard260a68f1998-08-13 03:39:55 +00003036 *
Daniel Veillard97b58771998-10-20 06:14:16 +00003037 * Search a Ns registered under a given name space for a document.
3038 * recurse on the parents until it finds the defined namespace
3039 * or return NULL otherwise.
3040 * @nameSpace can be NULL, this is a search for the default namespace.
Daniel Veillard1e346af1999-02-22 10:33:01 +00003041 * Returns the namespace pointer or NULL.
Daniel Veillard260a68f1998-08-13 03:39:55 +00003042 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003043xmlNsPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003044xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003045 xmlNsPtr cur;
3046
Daniel Veillard62ba71e1999-12-16 17:52:19 +00003047 if (node == NULL) return(NULL);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003048 while (node != NULL) {
3049 cur = node->nsDef;
3050 while (cur != NULL) {
3051 if ((cur->prefix == NULL) && (nameSpace == NULL))
3052 return(cur);
3053 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
3054 (!xmlStrcmp(cur->prefix, nameSpace)))
3055 return(cur);
3056 cur = cur->next;
3057 }
3058 node = node->parent;
3059 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00003060 return(NULL);
3061}
3062
Daniel Veillard97b58771998-10-20 06:14:16 +00003063/**
3064 * xmlSearchNsByHref:
3065 * @doc: the document
3066 * @node: the current node
3067 * @href: the namespace value
3068 *
3069 * Search a Ns aliasing a given URI. Recurse on the parents until it finds
3070 * the defined namespace or return NULL otherwise.
Daniel Veillard1e346af1999-02-22 10:33:01 +00003071 * Returns the namespace pointer or NULL.
Daniel Veillard260a68f1998-08-13 03:39:55 +00003072 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003073xmlNsPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003074xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar *href) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003075 xmlNsPtr cur;
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003076 xmlNodePtr orig = node;
Daniel Veillard260a68f1998-08-13 03:39:55 +00003077
Daniel Veillard10a2c651999-12-12 13:03:50 +00003078 if ((node == NULL) || (href == NULL)) return(NULL);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003079 while (node != NULL) {
3080 cur = node->nsDef;
3081 while (cur != NULL) {
3082 if ((cur->href != NULL) && (href != NULL) &&
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003083 (!xmlStrcmp(cur->href, href))) {
3084 /*
3085 * Check that the prefix is not shadowed between orig and node
3086 */
3087 xmlNodePtr check = orig;
3088 xmlNsPtr tst;
3089
3090 while (check != node) {
3091 tst = check->nsDef;
3092 while (tst != NULL) {
3093 if ((tst->prefix == NULL) && (cur->prefix == NULL))
3094 goto shadowed;
3095 if ((tst->prefix != NULL) && (cur->prefix != NULL) &&
3096 (!xmlStrcmp(tst->prefix, cur->prefix)))
3097 goto shadowed;
3098 tst = tst->next;
3099 }
3100 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00003101 return(cur);
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003102 }
3103shadowed:
Daniel Veillard260a68f1998-08-13 03:39:55 +00003104 cur = cur->next;
3105 }
3106 node = node->parent;
3107 }
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003108 return(NULL);
3109}
3110
3111/**
3112 * xmlNewReconciliedNs
3113 * @doc: the document
3114 * @tree: a node expected to hold the new namespace
3115 * @ns: the original namespace
3116 *
3117 * This function tries to locate a namespace definition in a tree
3118 * ancestors, or create a new namespace definition node similar to
3119 * @ns trying to reuse the same prefix. However if the given prefix is
3120 * null (default namespace) or reused within the subtree defined by
3121 * @tree or on one of its ancestors then a new prefix is generated.
3122 * Returns the (new) namespace definition or NULL in case of error
3123 */
3124xmlNsPtr
3125xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) {
3126 xmlNsPtr def;
3127 xmlChar prefix[50];
3128 int counter = 1;
3129
3130 if (tree == NULL) {
3131#ifdef DEBUG_TREE
3132 fprintf(stderr, "xmlNewReconciliedNs : tree == NULL\n");
3133#endif
3134 return(NULL);
3135 }
3136 if (ns == NULL) {
3137#ifdef DEBUG_TREE
3138 fprintf(stderr, "xmlNewReconciliedNs : ns == NULL\n");
3139#endif
3140 return(NULL);
3141 }
3142 /*
3143 * Search an existing namespace definition inherited.
3144 */
3145 def = xmlSearchNsByHref(doc, tree, ns->href);
3146 if (def != NULL)
3147 return(def);
3148
3149 /*
3150 * Find a close prefix which is not already in use.
3151 * Let's strip namespace prefixes longer than 20 chars !
3152 */
3153 sprintf((char *) prefix, "%.20s", ns->prefix);
3154 def = xmlSearchNs(doc, tree, prefix);
3155 while (def != NULL) {
3156 if (counter > 1000) return(NULL);
3157 sprintf((char *) prefix, "%.20s%d", ns->prefix, counter++);
3158 def = xmlSearchNs(doc, tree, prefix);
3159 }
3160
3161 /*
3162 * Ok, now we are ready to create a new one.
3163 */
3164 def = xmlNewNs(tree, ns->href, prefix);
3165 return(def);
3166}
3167
3168/**
3169 * xmlReconciliateNs
3170 * @doc: the document
3171 * @tree: a node defining the subtree to reconciliate
3172 *
3173 * This function checks that all the namespaces declared within the given
3174 * tree are properly declared. This is needed for example after Copy or Cut
3175 * and then paste operations. The subtree may still hold pointers to
3176 * namespace declarations outside the subtree or invalid/masked. As much
3177 * as possible the function try tu reuse the existing namespaces found in
3178 * the new environment. If not possible the new namespaces are redeclared
3179 * on @tree at the top of the given subtree.
3180 * Returns the number of namespace declarations created or -1 in case of error.
3181 */
3182int
3183xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) {
3184 xmlNsPtr *oldNs = NULL;
3185 xmlNsPtr *newNs = NULL;
3186 int sizeCache = 0;
3187 int nbCache = 0;
3188
3189 xmlNsPtr n;
3190 xmlNodePtr node = tree;
3191 xmlAttrPtr attr;
3192 int ret = 0, i;
3193
3194 while (node != NULL) {
3195 /*
3196 * Reconciliate the node namespace
3197 */
3198 if (node->ns != NULL) {
3199 /*
3200 * initialize the cache if needed
3201 */
3202 if (sizeCache == 0) {
3203 sizeCache = 10;
3204 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
3205 sizeof(xmlNsPtr));
3206 if (oldNs == NULL) {
3207 fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
3208 return(-1);
3209 }
3210 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
3211 sizeof(xmlNsPtr));
3212 if (newNs == NULL) {
3213 fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
3214 xmlFree(oldNs);
3215 return(-1);
3216 }
3217 }
3218 for (i = 0;i < nbCache;i++) {
3219 if (oldNs[i] == node->ns) {
3220 node->ns = newNs[i];
3221 break;
3222 }
3223 }
3224 if (i == nbCache) {
3225 /*
3226 * Ok we need to recreate a new namespace definition
3227 */
3228 n = xmlNewReconciliedNs(doc, tree, node->ns);
3229 if (n != NULL) { /* :-( what if else ??? */
3230 /*
3231 * check if we need to grow the cache buffers.
3232 */
3233 if (sizeCache <= nbCache) {
3234 sizeCache *= 2;
3235 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
3236 sizeof(xmlNsPtr));
3237 if (oldNs == NULL) {
3238 fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
3239 xmlFree(newNs);
3240 return(-1);
3241 }
3242 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
3243 sizeof(xmlNsPtr));
3244 if (newNs == NULL) {
3245 fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
3246 xmlFree(oldNs);
3247 return(-1);
3248 }
3249 }
3250 newNs[nbCache] = n;
3251 oldNs[nbCache++] = node->ns;
3252 node->ns = n;
3253 }
3254 }
3255 }
3256 /*
3257 * now check for namespace hold by attributes on the node.
3258 */
3259 attr = node->properties;
3260 while (attr != NULL) {
3261 if (attr->ns != NULL) {
3262 /*
3263 * initialize the cache if needed
3264 */
3265 if (sizeCache == 0) {
3266 sizeCache = 10;
3267 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
3268 sizeof(xmlNsPtr));
3269 if (oldNs == NULL) {
3270 fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
3271 return(-1);
3272 }
3273 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
3274 sizeof(xmlNsPtr));
3275 if (newNs == NULL) {
3276 fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
3277 xmlFree(oldNs);
3278 return(-1);
3279 }
3280 }
3281 for (i = 0;i < nbCache;i++) {
3282 if (oldNs[i] == attr->ns) {
3283 node->ns = newNs[i];
3284 break;
3285 }
3286 }
3287 if (i == nbCache) {
3288 /*
3289 * Ok we need to recreate a new namespace definition
3290 */
3291 n = xmlNewReconciliedNs(doc, tree, attr->ns);
3292 if (n != NULL) { /* :-( what if else ??? */
3293 /*
3294 * check if we need to grow the cache buffers.
3295 */
3296 if (sizeCache <= nbCache) {
3297 sizeCache *= 2;
3298 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
3299 sizeof(xmlNsPtr));
3300 if (oldNs == NULL) {
3301 fprintf(stderr,
3302 "xmlReconciliateNs : memory pbm\n");
3303 xmlFree(newNs);
3304 return(-1);
3305 }
3306 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
3307 sizeof(xmlNsPtr));
3308 if (newNs == NULL) {
3309 fprintf(stderr,
3310 "xmlReconciliateNs : memory pbm\n");
3311 xmlFree(oldNs);
3312 return(-1);
3313 }
3314 }
3315 newNs[nbCache] = n;
3316 oldNs[nbCache++] = attr->ns;
3317 attr->ns = n;
3318 }
3319 }
3320 }
3321 attr = attr->next;
3322 }
3323
3324 /*
3325 * Browse the full subtree, deep first
3326 */
3327 if (node->childs != NULL) {
3328 /* deep first */
3329 node = node->childs;
3330 } else if ((node != tree) && (node->next != NULL)) {
3331 /* then siblings */
3332 node = node->next;
3333 } else if (node != tree) {
3334 /* go up to parents->next if needed */
3335 while (node != tree) {
3336 if (node->parent != NULL)
3337 node = node->parent;
3338 if ((node != tree) && (node->next != NULL)) {
3339 node = node->next;
3340 break;
3341 }
3342 if (node->parent == NULL) {
3343 node = NULL;
3344 break;
3345 }
3346 }
3347 /* exit condition */
3348 if (node == tree)
3349 node = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00003350 }
3351 }
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003352 return(ret);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003353}
3354
Daniel Veillard97b58771998-10-20 06:14:16 +00003355/**
3356 * xmlGetProp:
3357 * @node: the node
3358 * @name: the attribute name
3359 *
3360 * Search and get the value of an attribute associated to a node
Daniel Veillardccb09631998-10-27 06:21:04 +00003361 * This does the entity substitution.
Daniel Veillard10a2c651999-12-12 13:03:50 +00003362 * This function looks in DTD attribute declaration for #FIXED or
3363 * default declaration values unless DTD use has been turned off.
3364 *
Daniel Veillard1e346af1999-02-22 10:33:01 +00003365 * Returns the attribute value or NULL if not found.
Daniel Veillarda819dac1999-11-24 18:04:22 +00003366 * It's up to the caller to free the memory.
Daniel Veillard260a68f1998-08-13 03:39:55 +00003367 */
Daniel Veillarda819dac1999-11-24 18:04:22 +00003368xmlChar *
3369xmlGetProp(xmlNodePtr node, const xmlChar *name) {
Daniel Veillard10a2c651999-12-12 13:03:50 +00003370 xmlAttrPtr prop;
3371 xmlDocPtr doc;
Daniel Veillard260a68f1998-08-13 03:39:55 +00003372
Daniel Veillard10a2c651999-12-12 13:03:50 +00003373 if ((node == NULL) || (name == NULL)) return(NULL);
3374 /*
3375 * Check on the properties attached to the node
3376 */
3377 prop = node->properties;
Daniel Veillard260a68f1998-08-13 03:39:55 +00003378 while (prop != NULL) {
Daniel Veillard68178931999-02-08 18:34:36 +00003379 if (!xmlStrcmp(prop->name, name)) {
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003380 xmlChar *ret;
Daniel Veillard6800ef31999-02-08 18:33:22 +00003381
3382 ret = xmlNodeListGetString(node->doc, prop->val, 1);
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003383 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
Daniel Veillard6800ef31999-02-08 18:33:22 +00003384 return(ret);
Daniel Veillard68178931999-02-08 18:34:36 +00003385 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00003386 prop = prop->next;
3387 }
Daniel Veillard10a2c651999-12-12 13:03:50 +00003388 if (!xmlCheckDTD) return(NULL);
3389
3390 /*
3391 * Check if there is a default declaration in the internal
3392 * or external subsets
3393 */
3394 doc = node->doc;
3395 if (doc != NULL) {
3396 xmlAttributePtr attrDecl;
3397 if (doc->intSubset != NULL) {
3398 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
3399 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3400 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
Daniel Veillardf967b902000-01-17 16:06:10 +00003401 if (attrDecl != NULL)
3402 return(xmlStrdup(attrDecl->defaultValue));
Daniel Veillard10a2c651999-12-12 13:03:50 +00003403 }
3404 }
3405 return(NULL);
3406}
3407
3408/**
3409 * xmlGetNsProp:
3410 * @node: the node
3411 * @name: the attribute name
3412 * @namespace: the URI of the namespace
3413 *
3414 * Search and get the value of an attribute associated to a node
3415 * This attribute has to be anchored in the namespace specified.
3416 * This does the entity substitution.
3417 * This function looks in DTD attribute declaration for #FIXED or
3418 * default declaration values unless DTD use has been turned off.
3419 *
3420 * Returns the attribute value or NULL if not found.
3421 * It's up to the caller to free the memory.
3422 */
3423xmlChar *
3424xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *namespace) {
3425 xmlAttrPtr prop = node->properties;
3426 xmlDocPtr doc;
3427 xmlNsPtr ns;
3428
3429 if (namespace == NULL)
3430 return(xmlGetProp(node, name));
3431 while (prop != NULL) {
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00003432 /*
3433 * One need to have
3434 * - same attribute names
3435 * - and the attribute carrying that namespace
3436 * or
3437 * no namespace on the attribute and the element carrying it
3438 */
Daniel Veillard10a2c651999-12-12 13:03:50 +00003439 if ((!xmlStrcmp(prop->name, name)) &&
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00003440 (((prop->ns == NULL) && (node->ns != NULL) &&
3441 (!xmlStrcmp(node->ns->href, namespace))) ||
Daniel Veillard3c558c31999-12-22 11:30:41 +00003442 ((prop->ns != NULL) && (!xmlStrcmp(prop->ns->href, namespace))))) {
Daniel Veillard10a2c651999-12-12 13:03:50 +00003443 xmlChar *ret;
3444
3445 ret = xmlNodeListGetString(node->doc, prop->val, 1);
3446 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
3447 return(ret);
3448 }
3449 prop = prop->next;
3450 }
3451 if (!xmlCheckDTD) return(NULL);
3452
3453 /*
3454 * Check if there is a default declaration in the internal
3455 * or external subsets
3456 */
3457 doc = node->doc;
3458 if (doc != NULL) {
3459 xmlAttributePtr attrDecl;
3460 if (doc->intSubset != NULL) {
3461 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
3462 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3463 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
3464
3465 if (attrDecl->prefix != NULL) {
3466 /*
3467 * The DTD declaration only allows a prefix search
3468 */
3469 ns = xmlSearchNs(doc, node, attrDecl->prefix);
3470 if ((ns != NULL) && (!xmlStrcmp(ns->href, namespace)))
3471 return(xmlStrdup(attrDecl->defaultValue));
3472 }
3473 }
3474 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00003475 return(NULL);
3476}
3477
Daniel Veillard97b58771998-10-20 06:14:16 +00003478/**
Daniel Veillardccb09631998-10-27 06:21:04 +00003479 * xmlSetProp:
Daniel Veillard97b58771998-10-20 06:14:16 +00003480 * @node: the node
3481 * @name: the attribute name
3482 * @value: the attribute value
3483 *
3484 * Set (or reset) an attribute carried by a node.
Daniel Veillard1e346af1999-02-22 10:33:01 +00003485 * Returns the attribute pointer.
Daniel Veillard260a68f1998-08-13 03:39:55 +00003486 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003487xmlAttrPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003488xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003489 xmlAttrPtr prop = node->properties;
3490
3491 while (prop != NULL) {
3492 if (!xmlStrcmp(prop->name, name)) {
Daniel Veillardccb09631998-10-27 06:21:04 +00003493 if (prop->val != NULL)
Daniel Veillard6454aec1999-09-02 22:04:43 +00003494 xmlFreeNodeList(prop->val);
Daniel Veillardccb09631998-10-27 06:21:04 +00003495 prop->val = NULL;
Daniel Veillard51e3b151999-11-12 17:02:31 +00003496 if (value != NULL) {
3497 xmlChar *buffer;
3498 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
3499 prop->val = xmlStringGetNodeList(node->doc, buffer);
3500 xmlFree(buffer);
3501 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00003502 return(prop);
3503 }
3504 prop = prop->next;
3505 }
3506 prop = xmlNewProp(node, name, value);
3507 return(prop);
3508}
3509
Daniel Veillard97b58771998-10-20 06:14:16 +00003510/**
3511 * xmlNodeIsText:
3512 * @node: the node
3513 *
3514 * Is this node a Text node ?
Daniel Veillard1e346af1999-02-22 10:33:01 +00003515 * Returns 1 yes, 0 no
Daniel Veillard260a68f1998-08-13 03:39:55 +00003516 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003517int
3518xmlNodeIsText(xmlNodePtr node) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003519 if (node == NULL) return(0);
3520
Daniel Veillard0bef1311998-10-14 02:36:47 +00003521 if (node->type == XML_TEXT_NODE) return(1);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003522 return(0);
3523}
3524
Daniel Veillard97b58771998-10-20 06:14:16 +00003525/**
Daniel Veillard1e346af1999-02-22 10:33:01 +00003526 * xmlTextConcat:
Daniel Veillard97b58771998-10-20 06:14:16 +00003527 * @node: the node
3528 * @content: the content
3529 * @len: @content lenght
3530 *
3531 * Concat the given string at the end of the existing node content
Daniel Veillard260a68f1998-08-13 03:39:55 +00003532 */
Daniel Veillard97b58771998-10-20 06:14:16 +00003533
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003534void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003535xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003536 if (node == NULL) return;
3537
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003538 if ((node->type != XML_TEXT_NODE) &&
3539 (node->type != XML_CDATA_SECTION_NODE)) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003540#ifdef DEBUG_TREE
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003541 fprintf(stderr, "xmlTextConcat: node is not text nor cdata\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003542#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00003543 return;
3544 }
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003545#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard260a68f1998-08-13 03:39:55 +00003546 node->content = xmlStrncat(node->content, content, len);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003547#else
3548 xmlBufferAdd(node->content, content, len);
3549#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00003550}
3551
3552/************************************************************************
3553 * *
3554 * Output : to a FILE or in memory *
3555 * *
3556 ************************************************************************/
3557
Daniel Veillard5099ae81999-04-21 20:12:07 +00003558#define BASE_BUFFER_SIZE 4000
3559
3560/**
3561 * xmlBufferCreate:
3562 *
3563 * routine to create an XML buffer.
3564 * returns the new structure.
3565 */
3566xmlBufferPtr
3567xmlBufferCreate(void) {
3568 xmlBufferPtr ret;
3569
Daniel Veillard6454aec1999-09-02 22:04:43 +00003570 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
Daniel Veillard5099ae81999-04-21 20:12:07 +00003571 if (ret == NULL) {
3572 fprintf(stderr, "xmlBufferCreate : out of memory!\n");
3573 return(NULL);
3574 }
3575 ret->use = 0;
3576 ret->size = BASE_BUFFER_SIZE;
Daniel Veillard10a2c651999-12-12 13:03:50 +00003577 ret->alloc = xmlBufferAllocScheme;
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003578 ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar));
Daniel Veillard5099ae81999-04-21 20:12:07 +00003579 if (ret->content == NULL) {
3580 fprintf(stderr, "xmlBufferCreate : out of memory!\n");
Daniel Veillard6454aec1999-09-02 22:04:43 +00003581 xmlFree(ret);
Daniel Veillard5099ae81999-04-21 20:12:07 +00003582 return(NULL);
3583 }
3584 ret->content[0] = 0;
3585 return(ret);
3586}
3587
3588/**
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003589 * xmlBufferCreateSize:
3590 * @size: initial size of buffer
3591 *
3592 * routine to create an XML buffer.
3593 * returns the new structure.
3594 */
3595xmlBufferPtr
3596xmlBufferCreateSize(size_t size) {
3597 xmlBufferPtr ret;
3598
3599 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
3600 if (ret == NULL) {
3601 fprintf(stderr, "xmlBufferCreate : out of memory!\n");
3602 return(NULL);
3603 }
3604 ret->use = 0;
Daniel Veillard10a2c651999-12-12 13:03:50 +00003605 ret->alloc = xmlBufferAllocScheme;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003606 ret->size = (size ? size+2 : 0); /* +1 for ending null */
Daniel Veillard10a2c651999-12-12 13:03:50 +00003607 if (ret->size){
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003608 ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar));
3609 if (ret->content == NULL) {
3610 fprintf(stderr, "xmlBufferCreate : out of memory!\n");
3611 xmlFree(ret);
3612 return(NULL);
3613 }
3614 ret->content[0] = 0;
3615 } else
3616 ret->content = NULL;
3617 return(ret);
3618}
3619
3620/**
3621 * xmlBufferAllocationScheme:
3622 * @buf: the buffer to free
3623 * @scheme: allocation scheme to use
3624 *
3625 * Sets the allocation scheme for this buffer
3626 */
3627void
3628xmlBufferSetAllocationScheme(xmlBufferPtr buf,
3629 xmlBufferAllocationScheme scheme) {
3630 if (buf == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003631#ifdef DEBUG_BUFFER
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003632 fprintf(stderr, "xmlBufferSetAllocationScheme: buf == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003633#endif
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003634 return;
3635 }
3636
3637 buf->alloc = scheme;
3638}
3639
3640/**
Daniel Veillard5099ae81999-04-21 20:12:07 +00003641 * xmlBufferFree:
3642 * @buf: the buffer to free
3643 *
3644 * Frees an XML buffer.
3645 */
3646void
3647xmlBufferFree(xmlBufferPtr buf) {
3648 if (buf == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003649#ifdef DEBUG_BUFFER
Daniel Veillard5099ae81999-04-21 20:12:07 +00003650 fprintf(stderr, "xmlBufferFree: buf == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003651#endif
Daniel Veillard5099ae81999-04-21 20:12:07 +00003652 return;
3653 }
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003654 if (buf->content != NULL) {
3655#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard5099ae81999-04-21 20:12:07 +00003656 memset(buf->content, -1, BASE_BUFFER_SIZE);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003657#else
3658 memset(buf->content, -1, buf->size);
3659#endif
Daniel Veillard6454aec1999-09-02 22:04:43 +00003660 xmlFree(buf->content);
Daniel Veillard5099ae81999-04-21 20:12:07 +00003661 }
3662 memset(buf, -1, sizeof(xmlBuffer));
Daniel Veillard6454aec1999-09-02 22:04:43 +00003663 xmlFree(buf);
Daniel Veillard5099ae81999-04-21 20:12:07 +00003664}
3665
3666/**
Daniel Veillarde2d034d1999-07-27 19:52:06 +00003667 * xmlBufferEmpty:
3668 * @buf: the buffer
3669 *
3670 * empty a buffer.
3671 */
3672void
3673xmlBufferEmpty(xmlBufferPtr buf) {
3674 buf->use = 0;
3675 memset(buf->content, -1, buf->size);/* just for debug */
3676}
3677
3678/**
3679 * xmlBufferShrink:
3680 * @buf: the buffer to dump
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003681 * @len: the number of xmlChar to remove
Daniel Veillarde2d034d1999-07-27 19:52:06 +00003682 *
3683 * Remove the beginning of an XML buffer.
3684 *
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003685 * Returns the number of xmlChar removed, or -1 in case of failure.
Daniel Veillarde2d034d1999-07-27 19:52:06 +00003686 */
3687int
3688xmlBufferShrink(xmlBufferPtr buf, int len) {
3689 if (len == 0) return(0);
3690 if (len > buf->use) return(-1);
3691
3692 buf->use -= len;
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003693 memmove(buf->content, &buf->content[len], buf->use * sizeof(xmlChar));
Daniel Veillarde2d034d1999-07-27 19:52:06 +00003694
3695 buf->content[buf->use] = 0;
3696 return(len);
3697}
3698
3699/**
Daniel Veillard5099ae81999-04-21 20:12:07 +00003700 * xmlBufferDump:
3701 * @file: the file output
3702 * @buf: the buffer to dump
3703 *
3704 * Dumps an XML buffer to a FILE *.
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003705 * Returns the number of xmlChar written
Daniel Veillard5099ae81999-04-21 20:12:07 +00003706 */
3707int
3708xmlBufferDump(FILE *file, xmlBufferPtr buf) {
3709 int ret;
3710
3711 if (buf == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003712#ifdef DEBUG_BUFFER
Daniel Veillard5099ae81999-04-21 20:12:07 +00003713 fprintf(stderr, "xmlBufferDump: buf == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003714#endif
Daniel Veillard5099ae81999-04-21 20:12:07 +00003715 return(0);
3716 }
3717 if (buf->content == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003718#ifdef DEBUG_BUFFER
Daniel Veillard5099ae81999-04-21 20:12:07 +00003719 fprintf(stderr, "xmlBufferDump: buf->content == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003720#endif
Daniel Veillard5099ae81999-04-21 20:12:07 +00003721 return(0);
3722 }
3723 if (file == NULL) file = stdout;
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003724 ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
Daniel Veillard5099ae81999-04-21 20:12:07 +00003725 return(ret);
3726}
3727
3728/**
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003729 * xmlBufferContent:
3730 * @buf: the buffer to resize
3731 *
3732 * Returns the internal content
3733 */
3734
3735const xmlChar*
3736xmlBufferContent(const xmlBufferPtr buf)
3737{
3738 if(!buf)
3739 return NULL;
3740
3741 return buf->content;
3742}
3743
3744/**
3745 * xmlBufferLength:
3746 * @buf: the buffer
3747 *
3748 * Returns the length of data in the internal content
3749 */
3750
3751int
3752xmlBufferLength(const xmlBufferPtr buf)
3753{
3754 if(!buf)
3755 return 0;
3756
3757 return buf->use;
3758}
3759
3760/**
3761 * xmlBufferResize:
3762 * @buf: the buffer to resize
3763 * @len: the desired size
3764 *
3765 * Resize a buffer to accomodate minimum size of <len>.
3766 *
3767 * Returns 0 in case of problems, 1 otherwise
3768 */
3769int
3770xmlBufferResize(xmlBufferPtr buf, int size)
3771{
3772 int newSize = (buf->size ? buf->size*2 : size);/*take care of empty case*/
3773 xmlChar* rebuf = NULL;
3774
3775 /* Don't resize if we don't have to */
3776 if(size < buf->size)
3777 return 1;
3778
3779 /* figure out new size */
3780 switch(buf->alloc){
3781 case XML_BUFFER_ALLOC_DOUBLEIT:
3782 while(size > newSize) newSize *= 2;
3783 break;
3784 case XML_BUFFER_ALLOC_EXACT:
3785 newSize = size+10;
3786 break;
3787 default:
3788 newSize = size+10;
3789 break;
3790 }
3791
3792 if (buf->content == NULL)
3793 rebuf = (xmlChar *) xmlMalloc(newSize * sizeof(xmlChar));
3794 else
3795 rebuf = (xmlChar *) xmlRealloc(buf->content,
3796 newSize * sizeof(xmlChar));
3797 if (rebuf == NULL) {
3798 fprintf(stderr, "xmlBufferAdd : out of memory!\n");
3799 return 0;
3800 }
3801 buf->content = rebuf;
3802 buf->size = newSize;
3803
3804 return 1;
3805}
3806/**
Daniel Veillard5099ae81999-04-21 20:12:07 +00003807 * xmlBufferAdd:
3808 * @buf: the buffer to dump
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003809 * @str: the xmlChar string
3810 * @len: the number of xmlChar to add
Daniel Veillard5099ae81999-04-21 20:12:07 +00003811 *
Daniel Veillard10a2c651999-12-12 13:03:50 +00003812 * Add a string range to an XML buffer. if len == -1, the lenght of
3813 * str is recomputed.
Daniel Veillard5099ae81999-04-21 20:12:07 +00003814 */
3815void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003816xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003817 int l, needSize;
Daniel Veillard5099ae81999-04-21 20:12:07 +00003818
3819 if (str == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003820#ifdef DEBUG_BUFFER
Daniel Veillard5099ae81999-04-21 20:12:07 +00003821 fprintf(stderr, "xmlBufferAdd: str == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003822#endif
Daniel Veillard5099ae81999-04-21 20:12:07 +00003823 return;
3824 }
Daniel Veillard10a2c651999-12-12 13:03:50 +00003825 if (len < -1) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003826#ifdef DEBUG_BUFFER
Daniel Veillard10a2c651999-12-12 13:03:50 +00003827 fprintf(stderr, "xmlBufferAdd: len < 0\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003828#endif
Daniel Veillard10a2c651999-12-12 13:03:50 +00003829 return;
3830 }
3831 if (len == 0) return;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003832
3833 /* CJN What's this for??? */
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003834 if (len < 0)
3835 l = xmlStrlen(str);
3836 else
3837 for (l = 0;l < len;l++)
3838 if (str[l] == 0) break;
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003839 if (l < len){
3840 len = l;
3841#ifdef DEBUG_BUFFER
3842 printf("xmlBufferAdd bad length\n");
3843#endif
3844 }
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003845
3846 /* CJN 11.18.99 okay, now I'm using the length */
3847 if(len == -1) len = l;
3848
3849
Daniel Veillarde2d034d1999-07-27 19:52:06 +00003850 if (len <= 0) return;
Daniel Veillard5099ae81999-04-21 20:12:07 +00003851
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003852 needSize = buf->use + len + 2;
3853 if(needSize > buf->size){
3854 if(!xmlBufferResize(buf, needSize)){
3855 fprintf(stderr, "xmlBufferAdd : out of memory!\n");
3856 return;
3857 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00003858 }
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003859
3860 memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
Daniel Veillarde2d034d1999-07-27 19:52:06 +00003861 buf->use += len;
3862 buf->content[buf->use] = 0;
Daniel Veillard5099ae81999-04-21 20:12:07 +00003863}
3864
3865/**
3866 * xmlBufferCat:
3867 * @buf: the buffer to dump
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003868 * @str: the xmlChar string
Daniel Veillard5099ae81999-04-21 20:12:07 +00003869 *
3870 * Append a zero terminated string to an XML buffer.
3871 */
3872void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003873xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003874 if (str != NULL)
3875 xmlBufferAdd(buf, str, -1);
Daniel Veillard5099ae81999-04-21 20:12:07 +00003876}
3877
3878/**
3879 * xmlBufferCCat:
3880 * @buf: the buffer to dump
3881 * @str: the C char string
3882 *
3883 * Append a zero terminated C string to an XML buffer.
3884 */
3885void
3886xmlBufferCCat(xmlBufferPtr buf, const char *str) {
3887 const char *cur;
3888
3889 if (str == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003890#ifdef DEBUG_BUFFER
Daniel Veillard5099ae81999-04-21 20:12:07 +00003891 fprintf(stderr, "xmlBufferAdd: str == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003892#endif
Daniel Veillard5099ae81999-04-21 20:12:07 +00003893 return;
3894 }
3895 for (cur = str;*cur != 0;cur++) {
3896 if (buf->use + 10 >= buf->size) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003897 if(!xmlBufferResize(buf, buf->use+10)){
3898 fprintf(stderr, "xmlBufferCCat : out of memory!\n");
3899 return;
3900 }
3901 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00003902 buf->content[buf->use++] = *cur;
3903 }
3904}
Daniel Veillard260a68f1998-08-13 03:39:55 +00003905
Daniel Veillard97b58771998-10-20 06:14:16 +00003906/**
3907 * xmlBufferWriteCHAR:
Daniel Veillard5099ae81999-04-21 20:12:07 +00003908 * @buf: the XML buffer
Daniel Veillard97b58771998-10-20 06:14:16 +00003909 * @string: the string to add
3910 *
3911 * routine which manage and grows an output buffer. This one add
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003912 * xmlChars at the end of the buffer.
Daniel Veillard97b58771998-10-20 06:14:16 +00003913 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003914void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003915xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00003916 xmlBufferCat(buf, string);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003917}
3918
Daniel Veillard97b58771998-10-20 06:14:16 +00003919/**
3920 * xmlBufferWriteChar:
Daniel Veillard011b63c1999-06-02 17:44:04 +00003921 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00003922 * @string: the string to add
3923 *
3924 * routine which manage and grows an output buffer. This one add
3925 * C chars at the end of the array.
3926 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003927void
Daniel Veillard5099ae81999-04-21 20:12:07 +00003928xmlBufferWriteChar(xmlBufferPtr buf, const char *string) {
3929 xmlBufferCCat(buf, string);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003930}
3931
Daniel Veillard5099ae81999-04-21 20:12:07 +00003932
Daniel Veillard97b58771998-10-20 06:14:16 +00003933/**
Daniel Veillard011b63c1999-06-02 17:44:04 +00003934 * xmlBufferWriteQuotedString:
3935 * @buf: the XML buffer output
3936 * @string: the string to add
3937 *
3938 * routine which manage and grows an output buffer. This one writes
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003939 * a quoted or double quoted xmlChar string, checking first if it holds
Daniel Veillard011b63c1999-06-02 17:44:04 +00003940 * quote or double-quotes internally
3941 */
3942void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003943xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) {
Daniel Veillardb96e6431999-08-29 21:02:19 +00003944 if (xmlStrchr(string, '"')) {
3945 if (xmlStrchr(string, '\'')) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003946#ifdef DEBUG_BUFFER
Daniel Veillard011b63c1999-06-02 17:44:04 +00003947 fprintf(stderr,
3948 "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003949#endif
Daniel Veillard011b63c1999-06-02 17:44:04 +00003950 }
3951 xmlBufferCCat(buf, "'");
3952 xmlBufferCat(buf, string);
3953 xmlBufferCCat(buf, "'");
3954 } else {
3955 xmlBufferCCat(buf, "\"");
3956 xmlBufferCat(buf, string);
3957 xmlBufferCCat(buf, "\"");
3958 }
3959}
3960
3961
3962/**
Daniel Veillard97b58771998-10-20 06:14:16 +00003963 * xmlGlobalNsDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00003964 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00003965 * @cur: a namespace
3966 *
3967 * Dump a global Namespace, this is the old version based on PIs.
Daniel Veillard260a68f1998-08-13 03:39:55 +00003968 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003969static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00003970xmlGlobalNsDump(xmlBufferPtr buf, xmlNsPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003971 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003972#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00003973 fprintf(stderr, "xmlGlobalNsDump : Ns == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003974#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00003975 return;
3976 }
3977 if (cur->type == XML_GLOBAL_NAMESPACE) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00003978 xmlBufferWriteChar(buf, "<?namespace");
Daniel Veillard260a68f1998-08-13 03:39:55 +00003979 if (cur->href != NULL) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00003980 xmlBufferWriteChar(buf, " href=");
3981 xmlBufferWriteQuotedString(buf, cur->href);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003982 }
3983 if (cur->prefix != NULL) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00003984 xmlBufferWriteChar(buf, " AS=");
3985 xmlBufferWriteQuotedString(buf, cur->prefix);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003986 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00003987 xmlBufferWriteChar(buf, "?>\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +00003988 }
3989}
3990
Daniel Veillard97b58771998-10-20 06:14:16 +00003991/**
3992 * xmlGlobalNsListDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00003993 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00003994 * @cur: the first namespace
3995 *
3996 * Dump a list of global Namespace, this is the old version based on PIs.
Daniel Veillard260a68f1998-08-13 03:39:55 +00003997 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003998static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00003999xmlGlobalNsListDump(xmlBufferPtr buf, xmlNsPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00004000 while (cur != NULL) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004001 xmlGlobalNsDump(buf, cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004002 cur = cur->next;
4003 }
4004}
4005
Daniel Veillard97b58771998-10-20 06:14:16 +00004006/**
4007 * xmlNsDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00004008 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00004009 * @cur: a namespace
4010 *
Daniel Veillard260a68f1998-08-13 03:39:55 +00004011 * Dump a local Namespace definition.
Daniel Veillard97b58771998-10-20 06:14:16 +00004012 * Should be called in the context of attributes dumps.
Daniel Veillard260a68f1998-08-13 03:39:55 +00004013 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004014static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00004015xmlNsDump(xmlBufferPtr buf, xmlNsPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00004016 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004017#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00004018 fprintf(stderr, "xmlNsDump : Ns == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004019#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00004020 return;
4021 }
4022 if (cur->type == XML_LOCAL_NAMESPACE) {
4023 /* Within the context of an element attributes */
4024 if (cur->prefix != NULL) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004025 xmlBufferWriteChar(buf, " xmlns:");
4026 xmlBufferWriteCHAR(buf, cur->prefix);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004027 } else
Daniel Veillard5099ae81999-04-21 20:12:07 +00004028 xmlBufferWriteChar(buf, " xmlns");
Daniel Veillard011b63c1999-06-02 17:44:04 +00004029 xmlBufferWriteChar(buf, "=");
4030 xmlBufferWriteQuotedString(buf, cur->href);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004031 }
4032}
4033
Daniel Veillard97b58771998-10-20 06:14:16 +00004034/**
4035 * xmlNsListDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00004036 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00004037 * @cur: the first namespace
4038 *
4039 * Dump a list of local Namespace definitions.
4040 * Should be called in the context of attributes dumps.
Daniel Veillard260a68f1998-08-13 03:39:55 +00004041 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004042static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00004043xmlNsListDump(xmlBufferPtr buf, xmlNsPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00004044 while (cur != NULL) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004045 xmlNsDump(buf, cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004046 cur = cur->next;
4047 }
4048}
4049
Daniel Veillard97b58771998-10-20 06:14:16 +00004050/**
4051 * xmlDtdDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00004052 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00004053 * @doc: the document
4054 *
4055 * Dump the XML document DTD, if any.
Daniel Veillard260a68f1998-08-13 03:39:55 +00004056 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004057static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00004058xmlDtdDump(xmlBufferPtr buf, xmlDocPtr doc) {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00004059 xmlDtdPtr cur = doc->intSubset;
Daniel Veillard260a68f1998-08-13 03:39:55 +00004060
4061 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004062#ifdef DEBUG_TREE
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00004063 fprintf(stderr, "xmlDtdDump : no internal subset\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004064#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00004065 return;
4066 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004067 xmlBufferWriteChar(buf, "<!DOCTYPE ");
4068 xmlBufferWriteCHAR(buf, cur->name);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004069 if (cur->ExternalID != NULL) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00004070 xmlBufferWriteChar(buf, " PUBLIC ");
4071 xmlBufferWriteQuotedString(buf, cur->ExternalID);
4072 xmlBufferWriteChar(buf, " ");
4073 xmlBufferWriteQuotedString(buf, cur->SystemID);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004074 } else if (cur->SystemID != NULL) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00004075 xmlBufferWriteChar(buf, " SYSTEM ");
4076 xmlBufferWriteQuotedString(buf, cur->SystemID);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004077 }
Daniel Veillard1e346af1999-02-22 10:33:01 +00004078 if ((cur->entities == NULL) && (cur->elements == NULL) &&
4079 (cur->attributes == NULL) && (cur->notations == NULL)) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004080 xmlBufferWriteChar(buf, ">\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004081 return;
4082 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004083 xmlBufferWriteChar(buf, " [\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004084 if (cur->entities != NULL)
Daniel Veillard5099ae81999-04-21 20:12:07 +00004085 xmlDumpEntitiesTable(buf, (xmlEntitiesTablePtr) cur->entities);
Daniel Veillard1e346af1999-02-22 10:33:01 +00004086 if (cur->notations != NULL)
Daniel Veillard5099ae81999-04-21 20:12:07 +00004087 xmlDumpNotationTable(buf, (xmlNotationTablePtr) cur->notations);
Daniel Veillard3b9def11999-01-31 22:15:06 +00004088 if (cur->elements != NULL)
Daniel Veillard5099ae81999-04-21 20:12:07 +00004089 xmlDumpElementTable(buf, (xmlElementTablePtr) cur->elements);
Daniel Veillard1e346af1999-02-22 10:33:01 +00004090 if (cur->attributes != NULL)
Daniel Veillard5099ae81999-04-21 20:12:07 +00004091 xmlDumpAttributeTable(buf, (xmlAttributeTablePtr) cur->attributes);
4092 xmlBufferWriteChar(buf, "]");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004093
Daniel Veillard5099ae81999-04-21 20:12:07 +00004094 xmlBufferWriteChar(buf, ">\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004095}
4096
Daniel Veillard97b58771998-10-20 06:14:16 +00004097/**
4098 * xmlAttrDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00004099 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00004100 * @doc: the document
4101 * @cur: the attribute pointer
4102 *
4103 * Dump an XML attribute
Daniel Veillard260a68f1998-08-13 03:39:55 +00004104 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004105static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00004106xmlAttrDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) {
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004107 xmlChar *value;
Daniel Veillardccb09631998-10-27 06:21:04 +00004108
Daniel Veillard260a68f1998-08-13 03:39:55 +00004109 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004110#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00004111 fprintf(stderr, "xmlAttrDump : property == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004112#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00004113 return;
4114 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004115 xmlBufferWriteChar(buf, " ");
Daniel Veillardb96e6431999-08-29 21:02:19 +00004116 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
4117 xmlBufferWriteCHAR(buf, cur->ns->prefix);
4118 xmlBufferWriteChar(buf, ":");
4119 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004120 xmlBufferWriteCHAR(buf, cur->name);
Daniel Veillardccb09631998-10-27 06:21:04 +00004121 value = xmlNodeListGetString(doc, cur->val, 0);
4122 if (value) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00004123 xmlBufferWriteChar(buf, "=");
4124 xmlBufferWriteQuotedString(buf, value);
Daniel Veillard6454aec1999-09-02 22:04:43 +00004125 xmlFree(value);
Daniel Veillard726c7e31999-02-08 15:13:10 +00004126 } else {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004127 xmlBufferWriteChar(buf, "=\"\"");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004128 }
4129}
4130
Daniel Veillard97b58771998-10-20 06:14:16 +00004131/**
4132 * xmlAttrListDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00004133 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00004134 * @doc: the document
4135 * @cur: the first attribute pointer
4136 *
4137 * Dump a list of XML attributes
Daniel Veillard260a68f1998-08-13 03:39:55 +00004138 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004139static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00004140xmlAttrListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00004141 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004142#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00004143 fprintf(stderr, "xmlAttrListDump : property == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004144#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00004145 return;
4146 }
4147 while (cur != NULL) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004148 xmlAttrDump(buf, doc, cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004149 cur = cur->next;
4150 }
4151}
4152
Daniel Veillard260a68f1998-08-13 03:39:55 +00004153
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004154static void
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004155xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
4156 int format);
Daniel Veillarddbfd6411999-12-28 16:35:14 +00004157void
4158htmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur);
4159
Daniel Veillard97b58771998-10-20 06:14:16 +00004160/**
4161 * xmlNodeListDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00004162 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00004163 * @doc: the document
4164 * @cur: the first node
4165 * @level: the imbrication level for indenting
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004166 * @format: is formatting allowed
Daniel Veillard97b58771998-10-20 06:14:16 +00004167 *
4168 * Dump an XML node list, recursive behaviour,children are printed too.
4169 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004170static void
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004171xmlNodeListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
4172 int format) {
4173 int i;
Daniel Veillardccb09631998-10-27 06:21:04 +00004174
Daniel Veillard260a68f1998-08-13 03:39:55 +00004175 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004176#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00004177 fprintf(stderr, "xmlNodeListDump : node == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004178#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00004179 return;
4180 }
4181 while (cur != NULL) {
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004182 if ((format) && (xmlIndentTreeOutput) &&
4183 (cur->type == XML_ELEMENT_NODE))
4184 for (i = 0;i < level;i++)
4185 xmlBufferWriteChar(buf, " ");
4186 xmlNodeDump(buf, doc, cur, level, format);
4187 if (format) {
4188 xmlBufferWriteChar(buf, "\n");
Daniel Veillardccb09631998-10-27 06:21:04 +00004189 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00004190 cur = cur->next;
4191 }
4192}
4193
Daniel Veillard97b58771998-10-20 06:14:16 +00004194/**
Daniel Veillardccb09631998-10-27 06:21:04 +00004195 * xmlNodeDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00004196 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00004197 * @doc: the document
4198 * @cur: the current node
4199 * @level: the imbrication level for indenting
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004200 * @format: is formatting allowed
Daniel Veillard97b58771998-10-20 06:14:16 +00004201 *
4202 * Dump an XML node, recursive behaviour,children are printed too.
Daniel Veillard260a68f1998-08-13 03:39:55 +00004203 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004204static void
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004205xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
4206 int format) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00004207 int i;
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004208 xmlNodePtr tmp;
Daniel Veillard260a68f1998-08-13 03:39:55 +00004209
4210 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004211#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00004212 fprintf(stderr, "xmlNodeDump : node == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004213#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00004214 return;
4215 }
Daniel Veillard0bef1311998-10-14 02:36:47 +00004216 if (cur->type == XML_TEXT_NODE) {
Daniel Veillard14fff061999-06-22 21:49:07 +00004217 if (cur->content != NULL) {
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004218 xmlChar *buffer;
Daniel Veillard14fff061999-06-22 21:49:07 +00004219
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004220#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard14fff061999-06-22 21:49:07 +00004221 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004222#else
4223 buffer = xmlEncodeEntitiesReentrant(doc,
4224 xmlBufferContent(cur->content));
4225#endif
Daniel Veillard14fff061999-06-22 21:49:07 +00004226 if (buffer != NULL) {
4227 xmlBufferWriteCHAR(buf, buffer);
Daniel Veillard6454aec1999-09-02 22:04:43 +00004228 xmlFree(buffer);
Daniel Veillard14fff061999-06-22 21:49:07 +00004229 }
4230 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00004231 return;
4232 }
Daniel Veillardb96e6431999-08-29 21:02:19 +00004233 if (cur->type == XML_PI_NODE) {
4234 if (cur->content != NULL) {
4235 xmlBufferWriteChar(buf, "<?");
4236 xmlBufferWriteCHAR(buf, cur->name);
4237 if (cur->content != NULL) {
4238 xmlBufferWriteChar(buf, " ");
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004239#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardb96e6431999-08-29 21:02:19 +00004240 xmlBufferWriteCHAR(buf, cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004241#else
4242 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
4243#endif
Daniel Veillardb96e6431999-08-29 21:02:19 +00004244 }
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004245 xmlBufferWriteChar(buf, "?>");
Daniel Veillardb96e6431999-08-29 21:02:19 +00004246 }
4247 return;
4248 }
Daniel Veillard0bef1311998-10-14 02:36:47 +00004249 if (cur->type == XML_COMMENT_NODE) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00004250 if (cur->content != NULL) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004251 xmlBufferWriteChar(buf, "<!--");
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004252#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard5099ae81999-04-21 20:12:07 +00004253 xmlBufferWriteCHAR(buf, cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004254#else
4255 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
4256#endif
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004257 xmlBufferWriteChar(buf, "-->");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004258 }
4259 return;
4260 }
Daniel Veillardccb09631998-10-27 06:21:04 +00004261 if (cur->type == XML_ENTITY_REF_NODE) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004262 xmlBufferWriteChar(buf, "&");
4263 xmlBufferWriteCHAR(buf, cur->name);
4264 xmlBufferWriteChar(buf, ";");
Daniel Veillardccb09631998-10-27 06:21:04 +00004265 return;
4266 }
Daniel Veillardb05deb71999-08-10 19:04:08 +00004267 if (cur->type == XML_CDATA_SECTION_NODE) {
4268 xmlBufferWriteChar(buf, "<![CDATA[");
4269 if (cur->content != NULL)
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004270#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardb05deb71999-08-10 19:04:08 +00004271 xmlBufferWriteCHAR(buf, cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004272#else
4273 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
4274#endif
Daniel Veillardb05deb71999-08-10 19:04:08 +00004275 xmlBufferWriteChar(buf, "]]>");
4276 return;
4277 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00004278
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004279 if (format == 1) {
4280 tmp = cur->childs;
4281 while (tmp != NULL) {
4282 if ((tmp->type == XML_TEXT_NODE) ||
4283 (tmp->type == XML_ENTITY_REF_NODE)) {
4284 format = 0;
4285 break;
4286 }
4287 tmp = tmp->next;
4288 }
4289 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004290 xmlBufferWriteChar(buf, "<");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004291 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004292 xmlBufferWriteCHAR(buf, cur->ns->prefix);
4293 xmlBufferWriteChar(buf, ":");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004294 }
4295
Daniel Veillard5099ae81999-04-21 20:12:07 +00004296 xmlBufferWriteCHAR(buf, cur->name);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004297 if (cur->nsDef)
Daniel Veillard5099ae81999-04-21 20:12:07 +00004298 xmlNsListDump(buf, cur->nsDef);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004299 if (cur->properties != NULL)
Daniel Veillard5099ae81999-04-21 20:12:07 +00004300 xmlAttrListDump(buf, doc, cur->properties);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004301
4302 if ((cur->content == NULL) && (cur->childs == NULL)) {
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004303 xmlBufferWriteChar(buf, "/>");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004304 return;
4305 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004306 xmlBufferWriteChar(buf, ">");
Daniel Veillard14fff061999-06-22 21:49:07 +00004307 if (cur->content != NULL) {
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004308 xmlChar *buffer;
Daniel Veillard14fff061999-06-22 21:49:07 +00004309
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004310#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard14fff061999-06-22 21:49:07 +00004311 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004312#else
4313 buffer = xmlEncodeEntitiesReentrant(doc,
4314 xmlBufferContent(cur->content));
4315#endif
Daniel Veillard14fff061999-06-22 21:49:07 +00004316 if (buffer != NULL) {
4317 xmlBufferWriteCHAR(buf, buffer);
Daniel Veillard6454aec1999-09-02 22:04:43 +00004318 xmlFree(buffer);
Daniel Veillard14fff061999-06-22 21:49:07 +00004319 }
4320 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00004321 if (cur->childs != NULL) {
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004322 if (format) xmlBufferWriteChar(buf, "\n");
4323 xmlNodeListDump(buf, doc, cur->childs, level + 1, format);
4324 if ((xmlIndentTreeOutput) && (format))
4325 for (i = 0;i < level;i++)
4326 xmlBufferWriteChar(buf, " ");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004327 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004328 xmlBufferWriteChar(buf, "</");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004329 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004330 xmlBufferWriteCHAR(buf, cur->ns->prefix);
4331 xmlBufferWriteChar(buf, ":");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004332 }
4333
Daniel Veillard5099ae81999-04-21 20:12:07 +00004334 xmlBufferWriteCHAR(buf, cur->name);
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004335 xmlBufferWriteChar(buf, ">");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004336}
4337
Daniel Veillard97b58771998-10-20 06:14:16 +00004338/**
Daniel Veillarddbfd6411999-12-28 16:35:14 +00004339 * xmlElemDump:
4340 * @buf: the XML buffer output
4341 * @doc: the document
4342 * @cur: the current node
4343 *
4344 * Dump an XML/HTML node, recursive behaviour,children are printed too.
4345 */
4346void
4347xmlElemDump(FILE *f, xmlDocPtr doc, xmlNodePtr cur) {
4348 xmlBufferPtr buf;
4349
4350 if (cur == NULL) {
4351#ifdef DEBUG_TREE
4352 fprintf(stderr, "xmlElemDump : cur == NULL\n");
4353#endif
4354 return;
4355 }
4356 if (doc == NULL) {
4357#ifdef DEBUG_TREE
4358 fprintf(stderr, "xmlElemDump : doc == NULL\n");
4359#endif
4360 }
4361 buf = xmlBufferCreate();
4362 if (buf == NULL) return;
4363 if ((doc != NULL) &&
4364 (doc->type == XML_HTML_DOCUMENT_NODE)) {
4365 htmlNodeDump(buf, doc, cur);
4366 } else
4367 xmlNodeDump(buf, doc, cur, 0, 1);
4368 xmlBufferDump(f, buf);
4369 xmlBufferFree(buf);
4370}
4371
4372/**
Daniel Veillard97b58771998-10-20 06:14:16 +00004373 * xmlDocContentDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00004374 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00004375 * @cur: the document
4376 *
4377 * Dump an XML document.
Daniel Veillard260a68f1998-08-13 03:39:55 +00004378 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004379static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00004380xmlDocContentDump(xmlBufferPtr buf, xmlDocPtr cur) {
Daniel Veillardbe70ff71999-07-05 16:50:46 +00004381 xmlBufferWriteChar(buf, "<?xml version=");
4382 if (cur->version != NULL)
4383 xmlBufferWriteQuotedString(buf, cur->version);
4384 else
4385 xmlBufferWriteChar(buf, "\"1.0\"");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004386 if (cur->encoding != NULL) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00004387 xmlBufferWriteChar(buf, " encoding=");
4388 xmlBufferWriteQuotedString(buf, cur->encoding);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004389 }
4390 switch (cur->standalone) {
4391 case 0:
Daniel Veillard5099ae81999-04-21 20:12:07 +00004392 xmlBufferWriteChar(buf, " standalone=\"no\"");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004393 break;
4394 case 1:
Daniel Veillard5099ae81999-04-21 20:12:07 +00004395 xmlBufferWriteChar(buf, " standalone=\"yes\"");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004396 break;
4397 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004398 xmlBufferWriteChar(buf, "?>\n");
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00004399 if (cur->intSubset != NULL)
Daniel Veillard5099ae81999-04-21 20:12:07 +00004400 xmlDtdDump(buf, cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004401 if (cur->root != NULL) {
Daniel Veillardb96e6431999-08-29 21:02:19 +00004402 xmlNodePtr child = cur->root;
4403
Daniel Veillard260a68f1998-08-13 03:39:55 +00004404 /* global namespace definitions, the old way */
4405 if (oldXMLWDcompatibility)
Daniel Veillard5099ae81999-04-21 20:12:07 +00004406 xmlGlobalNsListDump(buf, cur->oldNs);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004407 else
4408 xmlUpgradeOldNs(cur);
Daniel Veillardb96e6431999-08-29 21:02:19 +00004409
4410 while (child != NULL) {
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004411 xmlNodeDump(buf, cur, child, 0, 1);
4412 xmlBufferWriteChar(buf, "\n");
Daniel Veillardb96e6431999-08-29 21:02:19 +00004413 child = child->next;
4414 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00004415 }
4416}
4417
Daniel Veillard97b58771998-10-20 06:14:16 +00004418/**
4419 * xmlDocDumpMemory:
4420 * @cur: the document
4421 * @mem: OUT: the memory pointer
4422 * @size: OUT: the memory lenght
4423 *
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004424 * Dump an XML document in memory and return the xmlChar * and it's size.
Daniel Veillard97b58771998-10-20 06:14:16 +00004425 * It's up to the caller to free the memory.
Daniel Veillard260a68f1998-08-13 03:39:55 +00004426 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004427void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004428xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004429 xmlBufferPtr buf;
4430
Daniel Veillard260a68f1998-08-13 03:39:55 +00004431 if (cur == NULL) {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00004432#ifdef DEBUG_TREE
4433 fprintf(stderr, "xmlDocDumpMemory : document == NULL\n");
4434#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00004435 *mem = NULL;
4436 *size = 0;
4437 return;
4438 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004439 buf = xmlBufferCreate();
4440 if (buf == NULL) {
4441 *mem = NULL;
4442 *size = 0;
4443 return;
4444 }
4445 xmlDocContentDump(buf, cur);
Daniel Veillardb05deb71999-08-10 19:04:08 +00004446 *mem = xmlStrndup(buf->content, buf->use);
Daniel Veillard5099ae81999-04-21 20:12:07 +00004447 *size = buf->use;
Daniel Veillardb05deb71999-08-10 19:04:08 +00004448 xmlBufferFree(buf);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004449}
4450
Daniel Veillard97b58771998-10-20 06:14:16 +00004451/**
4452 * xmlGetDocCompressMode:
4453 * @doc: the document
4454 *
4455 * get the compression ratio for a document, ZLIB based
Daniel Veillard1e346af1999-02-22 10:33:01 +00004456 * Returns 0 (uncompressed) to 9 (max compression)
Daniel Veillard151b1b01998-09-23 00:49:46 +00004457 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004458int
Daniel Veillarddbfd6411999-12-28 16:35:14 +00004459xmlGetDocCompressMode (xmlDocPtr doc) {
Daniel Veillard15a8df41998-09-24 19:15:06 +00004460 if (doc == NULL) return(-1);
4461 return(doc->compression);
4462}
4463
Daniel Veillard97b58771998-10-20 06:14:16 +00004464/**
4465 * xmlSetDocCompressMode:
4466 * @doc: the document
4467 * @mode: the compression ratio
4468 *
4469 * set the compression ratio for a document, ZLIB based
4470 * Correct values: 0 (uncompressed) to 9 (max compression)
4471 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004472void
4473xmlSetDocCompressMode (xmlDocPtr doc, int mode) {
Daniel Veillard15a8df41998-09-24 19:15:06 +00004474 if (doc == NULL) return;
4475 if (mode < 0) doc->compression = 0;
4476 else if (mode > 9) doc->compression = 9;
4477 else doc->compression = mode;
4478}
4479
Daniel Veillard97b58771998-10-20 06:14:16 +00004480/**
4481 * xmlGetCompressMode:
4482 *
4483 * get the default compression mode used, ZLIB based.
Daniel Veillard1e346af1999-02-22 10:33:01 +00004484 * Returns 0 (uncompressed) to 9 (max compression)
Daniel Veillard15a8df41998-09-24 19:15:06 +00004485 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004486int
4487 xmlGetCompressMode(void) {
Daniel Veillard151b1b01998-09-23 00:49:46 +00004488 return(xmlCompressMode);
4489}
Daniel Veillard97b58771998-10-20 06:14:16 +00004490
4491/**
4492 * xmlSetCompressMode:
4493 * @mode: the compression ratio
4494 *
4495 * set the default compression mode used, ZLIB based
4496 * Correct values: 0 (uncompressed) to 9 (max compression)
4497 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004498void
4499xmlSetCompressMode(int mode) {
Daniel Veillard151b1b01998-09-23 00:49:46 +00004500 if (mode < 0) xmlCompressMode = 0;
Daniel Veillard15a8df41998-09-24 19:15:06 +00004501 else if (mode > 9) xmlCompressMode = 9;
Daniel Veillard151b1b01998-09-23 00:49:46 +00004502 else xmlCompressMode = mode;
4503}
4504
Daniel Veillard97b58771998-10-20 06:14:16 +00004505/**
4506 * xmlDocDump:
4507 * @f: the FILE*
4508 * @cur: the document
4509 *
4510 * Dump an XML document to an open FILE.
Daniel Veillard260a68f1998-08-13 03:39:55 +00004511 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004512void
4513xmlDocDump(FILE *f, xmlDocPtr cur) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004514 xmlBufferPtr buf;
4515
Daniel Veillard260a68f1998-08-13 03:39:55 +00004516 if (cur == NULL) {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00004517#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00004518 fprintf(stderr, "xmlDocDump : document == NULL\n");
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00004519#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00004520 return;
4521 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004522 buf = xmlBufferCreate();
4523 if (buf == NULL) return;
4524 xmlDocContentDump(buf, cur);
4525 xmlBufferDump(f, buf);
4526 xmlBufferFree(buf);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004527}
4528
Daniel Veillard97b58771998-10-20 06:14:16 +00004529/**
4530 * xmlSaveFile:
4531 * @filename: the filename
4532 * @cur: the document
4533 *
4534 * Dump an XML document to a file. Will use compression if
Daniel Veillard11a48ec1999-11-23 10:40:46 +00004535 * compiled in and enabled. If @filename is "-" the stdout file is
4536 * used.
Daniel Veillard97b58771998-10-20 06:14:16 +00004537 * returns: the number of file written or -1 in case of failure.
Daniel Veillard151b1b01998-09-23 00:49:46 +00004538 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004539int
4540xmlSaveFile(const char *filename, xmlDocPtr cur) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004541 xmlBufferPtr buf;
Daniel Veillard151b1b01998-09-23 00:49:46 +00004542#ifdef HAVE_ZLIB_H
4543 gzFile zoutput = NULL;
4544 char mode[15];
4545#endif
Daniel Veillardccb09631998-10-27 06:21:04 +00004546 FILE *output = NULL;
Daniel Veillard151b1b01998-09-23 00:49:46 +00004547 int ret;
4548
Daniel Veillard5099ae81999-04-21 20:12:07 +00004549 /*
4550 * save the content to a temp buffer.
4551 */
4552 buf = xmlBufferCreate();
4553 if (buf == NULL) return(0);
4554 xmlDocContentDump(buf, cur);
4555
Daniel Veillard151b1b01998-09-23 00:49:46 +00004556#ifdef HAVE_ZLIB_H
Daniel Veillard11a48ec1999-11-23 10:40:46 +00004557 if (cur->compression < 0) cur->compression = xmlCompressMode;
Daniel Veillarddc3dd9d1998-09-24 19:25:54 +00004558 if ((cur->compression > 0) && (cur->compression <= 9)) {
4559 sprintf(mode, "w%d", cur->compression);
Daniel Veillard11a48ec1999-11-23 10:40:46 +00004560 if (!strcmp(filename, "-"))
4561 zoutput = gzdopen(1, mode);
4562 else
4563 zoutput = gzopen(filename, mode);
Daniel Veillard151b1b01998-09-23 00:49:46 +00004564 }
4565 if (zoutput == NULL) {
4566#endif
4567 output = fopen(filename, "w");
Daniel Veillard10a2c651999-12-12 13:03:50 +00004568 if (output == NULL) {
4569 xmlBufferFree(buf);
4570 return(-1);
4571 }
Daniel Veillard151b1b01998-09-23 00:49:46 +00004572#ifdef HAVE_ZLIB_H
4573 }
Daniel Veillard151b1b01998-09-23 00:49:46 +00004574
Daniel Veillard151b1b01998-09-23 00:49:46 +00004575 if (zoutput != NULL) {
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004576 ret = gzwrite(zoutput, buf->content, sizeof(xmlChar) * buf->use);
Daniel Veillard151b1b01998-09-23 00:49:46 +00004577 gzclose(zoutput);
Daniel Veillard5099ae81999-04-21 20:12:07 +00004578 } else {
4579#endif
4580 ret = xmlBufferDump(output, buf);
4581 fclose(output);
4582#ifdef HAVE_ZLIB_H
Daniel Veillard151b1b01998-09-23 00:49:46 +00004583 }
4584#endif
Manish Vachharajani5e60f5a1999-05-29 03:04:30 +00004585 xmlBufferFree(buf);
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004586 return(ret * sizeof(xmlChar));
Daniel Veillard151b1b01998-09-23 00:49:46 +00004587}
4588