blob: bbeb90b1de53367af5cd3784635ea6a3c4def91e [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 Veillard260a68f1998-08-13 03:39:55 +000051/************************************************************************
52 * *
53 * Allocation and deallocation of basic structures *
54 * *
55 ************************************************************************/
56
Daniel Veillard97b58771998-10-20 06:14:16 +000057/**
Daniel Veillardf5c2c871999-12-01 09:51:45 +000058 * xmlSetBufferAllocationScheme:
59 * @scheme: allocation method to use
60 *
61 * Set the buffer allocation method. Types are
62 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
63 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
64 * improves performance
65 */
66void
67xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme) {
68 xmlBufferAllocScheme = scheme;
69}
70
71/**
72 * xmlGetBufferAllocationScheme:
73 *
74 * Types are
75 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
76 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
77 * improves performance
78 *
79 * Returns the current allocation scheme
80 */
81xmlBufferAllocationScheme
82xmlGetBufferAllocationScheme() {
83 return xmlBufferAllocScheme;
84}
85
86/**
Daniel Veillard97b58771998-10-20 06:14:16 +000087 * xmlUpgradeOldNs:
88 * @doc: a document pointer
89 *
90 * Upgrade old style Namespaces (PI) and move them to the root of the document.
Daniel Veillard260a68f1998-08-13 03:39:55 +000091 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +000092void
93xmlUpgradeOldNs(xmlDocPtr doc) {
Daniel Veillard260a68f1998-08-13 03:39:55 +000094 xmlNsPtr cur;
95
96 if ((doc == NULL) || (doc->oldNs == NULL)) return;
97 if (doc->root == NULL) {
98 fprintf(stderr, "xmlUpgradeOldNs: failed no root !\n");
99 return;
100 }
101
102 cur = doc->oldNs;
103 while (cur->next != NULL) {
104 cur->type = XML_LOCAL_NAMESPACE;
105 cur = cur->next;
106 }
107 cur->type = XML_LOCAL_NAMESPACE;
108 cur->next = doc->root->nsDef;
109 doc->root->nsDef = doc->oldNs;
110 doc->oldNs = NULL;
111}
112
Daniel Veillard97b58771998-10-20 06:14:16 +0000113/**
114 * xmlNewNs:
115 * @node: the element carrying the namespace
116 * @href: the URI associated
117 * @prefix: the prefix for the namespace
118 *
Daniel Veillard686d6b62000-01-03 11:08:02 +0000119 * Creation of a new Namespace. This function will refuse to create
120 * a namespace with a similar prefix than an existing one present on this
121 * node.
122 * Returns returns a new namespace pointer or NULL
Daniel Veillard260a68f1998-08-13 03:39:55 +0000123 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000124xmlNsPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000125xmlNewNs(xmlNodePtr node, const xmlChar *href, const xmlChar *prefix) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000126 xmlNsPtr cur;
127
128 if (href == NULL) {
129 fprintf(stderr, "xmlNewNs: href == NULL !\n");
130 return(NULL);
131 }
132
133 /*
134 * Allocate a new DTD and fill the fields.
135 */
Daniel Veillard6454aec1999-09-02 22:04:43 +0000136 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
Daniel Veillard260a68f1998-08-13 03:39:55 +0000137 if (cur == NULL) {
138 fprintf(stderr, "xmlNewNs : malloc failed\n");
139 return(NULL);
140 }
141
142 cur->type = XML_LOCAL_NAMESPACE;
143 if (href != NULL)
144 cur->href = xmlStrdup(href);
145 else
146 cur->href = NULL;
147 if (prefix != NULL)
148 cur->prefix = xmlStrdup(prefix);
149 else
150 cur->prefix = NULL;
151
152 /*
153 * Add it at the end to preserve parsing order ...
Daniel Veillard686d6b62000-01-03 11:08:02 +0000154 * and checks for existing use of the prefix
Daniel Veillard260a68f1998-08-13 03:39:55 +0000155 */
156 cur->next = NULL;
157 if (node != NULL) {
158 if (node->nsDef == NULL) {
159 node->nsDef = cur;
160 } else {
161 xmlNsPtr prev = node->nsDef;
162
Daniel Veillard686d6b62000-01-03 11:08:02 +0000163 while (prev->next != NULL) {
164 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
165 (!xmlStrcmp(prev->prefix, cur->prefix))) {
166 xmlFreeNs(cur);
167 return(NULL);
168 }
169 prev = prev->next;
170 }
Daniel Veillard260a68f1998-08-13 03:39:55 +0000171 prev->next = cur;
172 }
173 }
Daniel Veillard260a68f1998-08-13 03:39:55 +0000174 return(cur);
175}
176
Daniel Veillard97b58771998-10-20 06:14:16 +0000177/**
178 * xmlNewGlobalNs:
179 * @doc: the document carrying the namespace
180 * @href: the URI associated
181 * @prefix: the prefix for the namespace
182 *
Daniel Veillard686d6b62000-01-03 11:08:02 +0000183 * Creation of a Namespace, the old way using PI and without scoping
184 * DEPRECATED !!!
185 * Will be removed at next major release !
186 * Returns NULL this functionnality had been removed
Daniel Veillard260a68f1998-08-13 03:39:55 +0000187 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000188xmlNsPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000189xmlNewGlobalNs(xmlDocPtr doc, const xmlChar *href, const xmlChar *prefix) {
Daniel Veillard686d6b62000-01-03 11:08:02 +0000190 return(NULL);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000191}
192
Daniel Veillard97b58771998-10-20 06:14:16 +0000193/**
194 * xmlSetNs:
195 * @node: a node in the document
196 * @ns: a namespace pointer
197 *
198 * Associate a namespace to a node, a posteriori.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000199 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000200void
201xmlSetNs(xmlNodePtr node, xmlNsPtr ns) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000202 if (node == NULL) {
203 fprintf(stderr, "xmlSetNs: node == NULL\n");
204 return;
205 }
206 node->ns = ns;
207}
208
Daniel Veillard97b58771998-10-20 06:14:16 +0000209/**
210 * xmlFreeNs:
211 * @cur: the namespace pointer
212 *
213 * Free up the structures associated to a namespace
Daniel Veillard260a68f1998-08-13 03:39:55 +0000214 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000215void
216xmlFreeNs(xmlNsPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000217 if (cur == NULL) {
218 fprintf(stderr, "xmlFreeNs : ns == NULL\n");
219 return;
220 }
Daniel Veillard6454aec1999-09-02 22:04:43 +0000221 if (cur->href != NULL) xmlFree((char *) cur->href);
222 if (cur->prefix != NULL) xmlFree((char *) cur->prefix);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000223 memset(cur, -1, sizeof(xmlNs));
Daniel Veillard6454aec1999-09-02 22:04:43 +0000224 xmlFree(cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000225}
226
Daniel Veillard97b58771998-10-20 06:14:16 +0000227/**
228 * xmlFreeNsList:
229 * @cur: the first namespace pointer
230 *
231 * Free up all the structures associated to the chained namespaces.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000232 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000233void
234xmlFreeNsList(xmlNsPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000235 xmlNsPtr next;
236 if (cur == NULL) {
237 fprintf(stderr, "xmlFreeNsList : ns == NULL\n");
238 return;
239 }
240 while (cur != NULL) {
241 next = cur->next;
242 xmlFreeNs(cur);
243 cur = next;
244 }
245}
246
Daniel Veillard97b58771998-10-20 06:14:16 +0000247/**
248 * xmlNewDtd:
249 * @doc: the document pointer
250 * @name: the DTD name
251 * @ExternalID: the external ID
252 * @SystemID: the system ID
253 *
Daniel Veillard260a68f1998-08-13 03:39:55 +0000254 * Creation of a new DTD.
Daniel Veillard1e346af1999-02-22 10:33:01 +0000255 * Returns a pointer to the new DTD structure
Daniel Veillard260a68f1998-08-13 03:39:55 +0000256 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000257xmlDtdPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000258xmlNewDtd(xmlDocPtr doc, const xmlChar *name,
259 const xmlChar *ExternalID, const xmlChar *SystemID) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000260 xmlDtdPtr cur;
261
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000262 if ((doc != NULL) && (doc->extSubset != NULL)) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000263 fprintf(stderr, "xmlNewDtd(%s): document %s already have a DTD %s\n",
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000264 /* !!! */ (char *) name, doc->name,
265 /* !!! */ (char *)doc->extSubset->name);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000266 }
267
268 /*
269 * Allocate a new DTD and fill the fields.
270 */
Daniel Veillard6454aec1999-09-02 22:04:43 +0000271 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
Daniel Veillard260a68f1998-08-13 03:39:55 +0000272 if (cur == NULL) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000273 fprintf(stderr, "xmlNewDtd : malloc failed\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +0000274 return(NULL);
275 }
276
277 if (name != NULL)
278 cur->name = xmlStrdup(name);
279 else
280 cur->name = NULL;
281 if (ExternalID != NULL)
282 cur->ExternalID = xmlStrdup(ExternalID);
283 else
284 cur->ExternalID = NULL;
285 if (SystemID != NULL)
286 cur->SystemID = xmlStrdup(SystemID);
287 else
288 cur->SystemID = NULL;
Daniel Veillard1e346af1999-02-22 10:33:01 +0000289 cur->notations = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000290 cur->elements = NULL;
Daniel Veillard1e346af1999-02-22 10:33:01 +0000291 cur->attributes = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000292 cur->entities = NULL;
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000293 if (doc != NULL)
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000294 doc->extSubset = cur;
295
296 return(cur);
297}
298
299/**
300 * xmlCreateIntSubset:
301 * @doc: the document pointer
302 * @name: the DTD name
303 * @ExternalID: the external ID
304 * @SystemID: the system ID
305 *
Daniel Veillard1e346af1999-02-22 10:33:01 +0000306 * Create the internal subset of a document
307 * Returns a pointer to the new DTD structure
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000308 */
309xmlDtdPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000310xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name,
311 const xmlChar *ExternalID, const xmlChar *SystemID) {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000312 xmlDtdPtr cur;
313
314 if ((doc != NULL) && (doc->intSubset != NULL)) {
315 fprintf(stderr,
316 "xmlCreateIntSubset(): document %s already have an internal subset\n",
317 doc->name);
318 return(NULL);
319 }
320
321 /*
322 * Allocate a new DTD and fill the fields.
323 */
Daniel Veillard6454aec1999-09-02 22:04:43 +0000324 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000325 if (cur == NULL) {
326 fprintf(stderr, "xmlNewDtd : malloc failed\n");
327 return(NULL);
328 }
329
330 if (name != NULL)
331 cur->name = xmlStrdup(name);
332 else
333 cur->name = NULL;
334 if (ExternalID != NULL)
335 cur->ExternalID = xmlStrdup(ExternalID);
336 else
337 cur->ExternalID = NULL;
338 if (SystemID != NULL)
339 cur->SystemID = xmlStrdup(SystemID);
340 else
341 cur->SystemID = NULL;
Daniel Veillard1e346af1999-02-22 10:33:01 +0000342 cur->notations = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000343 cur->elements = NULL;
Daniel Veillard1e346af1999-02-22 10:33:01 +0000344 cur->attributes = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000345 cur->entities = NULL;
346 if (doc != NULL)
347 doc->intSubset = cur;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000348
349 return(cur);
350}
351
Daniel Veillard97b58771998-10-20 06:14:16 +0000352/**
353 * xmlFreeDtd:
354 * @cur: the DTD structure to free up
355 *
356 * Free a DTD structure.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000357 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000358void
359xmlFreeDtd(xmlDtdPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000360 if (cur == NULL) {
361 fprintf(stderr, "xmlFreeDtd : DTD == NULL\n");
362 return;
363 }
Daniel Veillard6454aec1999-09-02 22:04:43 +0000364 if (cur->name != NULL) xmlFree((char *) cur->name);
365 if (cur->SystemID != NULL) xmlFree((char *) cur->SystemID);
366 if (cur->ExternalID != NULL) xmlFree((char *) cur->ExternalID);
Daniel Veillard1e346af1999-02-22 10:33:01 +0000367 if (cur->notations != NULL)
368 xmlFreeNotationTable((xmlNotationTablePtr) cur->notations);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000369 if (cur->elements != NULL)
Daniel Veillard3b9def11999-01-31 22:15:06 +0000370 xmlFreeElementTable((xmlElementTablePtr) cur->elements);
Daniel Veillard1e346af1999-02-22 10:33:01 +0000371 if (cur->attributes != NULL)
372 xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000373 if (cur->entities != NULL)
374 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities);
375 memset(cur, -1, sizeof(xmlDtd));
Daniel Veillard6454aec1999-09-02 22:04:43 +0000376 xmlFree(cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000377}
378
Daniel Veillard97b58771998-10-20 06:14:16 +0000379/**
380 * xmlNewDoc:
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000381 * @version: xmlChar string giving the version of XML "1.0"
Daniel Veillard97b58771998-10-20 06:14:16 +0000382 *
Daniel Veillard1e346af1999-02-22 10:33:01 +0000383 * Returns a new document
Daniel Veillard260a68f1998-08-13 03:39:55 +0000384 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000385xmlDocPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000386xmlNewDoc(const xmlChar *version) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000387 xmlDocPtr cur;
388
389 if (version == NULL) {
390 fprintf(stderr, "xmlNewDoc : version == NULL\n");
391 return(NULL);
392 }
393
394 /*
395 * Allocate a new document and fill the fields.
396 */
Daniel Veillard6454aec1999-09-02 22:04:43 +0000397 cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
Daniel Veillard260a68f1998-08-13 03:39:55 +0000398 if (cur == NULL) {
399 fprintf(stderr, "xmlNewDoc : malloc failed\n");
400 return(NULL);
401 }
402
Daniel Veillard33942841998-10-18 19:12:41 +0000403 cur->type = XML_DOCUMENT_NODE;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000404 cur->version = xmlStrdup(version);
405 cur->name = NULL;
406 cur->root = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000407 cur->intSubset = NULL;
408 cur->extSubset = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000409 cur->oldNs = NULL;
410 cur->encoding = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000411 cur->standalone = -1;
Daniel Veillard11a48ec1999-11-23 10:40:46 +0000412 cur->compression = -1; /* not initialized */
Daniel Veillardb96e6431999-08-29 21:02:19 +0000413 cur->ids = NULL;
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000414 cur->refs = NULL;
Daniel Veillard27d88741999-05-29 11:51:49 +0000415#ifndef XML_WITHOUT_CORBA
Daniel Veillard27fb0751998-10-17 06:47:46 +0000416 cur->_private = NULL;
417 cur->vepv = NULL;
418#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +0000419 return(cur);
420}
421
Daniel Veillard97b58771998-10-20 06:14:16 +0000422/**
423 * xmlFreeDoc:
424 * @cur: pointer to the document
425 * @:
426 *
427 * Free up all the structures used by a document, tree included.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000428 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000429void
430xmlFreeDoc(xmlDocPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000431 if (cur == NULL) {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000432#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +0000433 fprintf(stderr, "xmlFreeDoc : document == NULL\n");
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000434#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +0000435 return;
436 }
Daniel Veillard6454aec1999-09-02 22:04:43 +0000437 if (cur->version != NULL) xmlFree((char *) cur->version);
438 if (cur->name != NULL) xmlFree((char *) cur->name);
439 if (cur->encoding != NULL) xmlFree((char *) cur->encoding);
440 if (cur->root != NULL) xmlFreeNodeList(cur->root);
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000441 if (cur->intSubset != NULL) xmlFreeDtd(cur->intSubset);
442 if (cur->extSubset != NULL) xmlFreeDtd(cur->extSubset);
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000443 if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
Daniel Veillardb96e6431999-08-29 21:02:19 +0000444 if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids);
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000445 if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000446 memset(cur, -1, sizeof(xmlDoc));
Daniel Veillard6454aec1999-09-02 22:04:43 +0000447 xmlFree(cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000448}
449
Daniel Veillard97b58771998-10-20 06:14:16 +0000450/**
Daniel Veillard16253641998-10-28 22:58:05 +0000451 * xmlStringLenGetNodeList:
452 * @doc: the document
453 * @value: the value of the text
Daniel Veillard1e346af1999-02-22 10:33:01 +0000454 * @len: the length of the string value
Daniel Veillard16253641998-10-28 22:58:05 +0000455 *
456 * Parse the value string and build the node list associated. Should
457 * produce a flat tree with only TEXTs and ENTITY_REFs.
Daniel Veillard1e346af1999-02-22 10:33:01 +0000458 * Returns a pointer to the first child
Daniel Veillard16253641998-10-28 22:58:05 +0000459 */
460xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000461xmlStringLenGetNodeList(xmlDocPtr doc, const xmlChar *value, int len) {
Daniel Veillard16253641998-10-28 22:58:05 +0000462 xmlNodePtr ret = NULL, last = NULL;
463 xmlNodePtr node;
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000464 xmlChar *val;
465 const xmlChar *cur = value;
466 const xmlChar *q;
Daniel Veillard25940b71998-10-29 05:51:30 +0000467 xmlEntityPtr ent;
Daniel Veillard16253641998-10-28 22:58:05 +0000468
469 if (value == NULL) return(NULL);
470
471 q = cur;
472 while ((*cur != 0) && (cur - value < len)) {
473 if (*cur == '&') {
Daniel Veillard25940b71998-10-29 05:51:30 +0000474 /*
475 * Save the current text.
476 */
Daniel Veillard16253641998-10-28 22:58:05 +0000477 if (cur != q) {
Daniel Veillard25940b71998-10-29 05:51:30 +0000478 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
479 xmlNodeAddContentLen(last, q, cur - q);
480 } else {
481 node = xmlNewDocTextLen(doc, q, cur - q);
482 if (node == NULL) return(ret);
483 if (last == NULL)
484 last = ret = node;
485 else {
486 last->next = node;
487 node->prev = last;
488 last = node;
489 }
Daniel Veillard16253641998-10-28 22:58:05 +0000490 }
491 }
Daniel Veillard25940b71998-10-29 05:51:30 +0000492 /*
493 * Read the entity string
494 */
Daniel Veillard16253641998-10-28 22:58:05 +0000495 cur++;
496 q = cur;
497 while ((*cur != 0) && (cur - value < len) && (*cur != ';')) cur++;
498 if ((*cur == 0) || (cur - value >= len)) {
499 fprintf(stderr,
Daniel Veillard011b63c1999-06-02 17:44:04 +0000500 "xmlStringLenGetNodeList: unterminated entity %30s\n", q);
Daniel Veillard16253641998-10-28 22:58:05 +0000501 return(ret);
502 }
503 if (cur != q) {
Daniel Veillard25940b71998-10-29 05:51:30 +0000504 /*
505 * Predefined entities don't generate nodes
506 */
Daniel Veillard16253641998-10-28 22:58:05 +0000507 val = xmlStrndup(q, cur - q);
Daniel Veillard25940b71998-10-29 05:51:30 +0000508 ent = xmlGetDocEntity(doc, val);
509 if ((ent != NULL) &&
510 (ent->type == XML_INTERNAL_PREDEFINED_ENTITY)) {
511 if (last == NULL) {
512 node = xmlNewDocText(doc, ent->content);
513 last = ret = node;
514 } else
515 xmlNodeAddContent(last, ent->content);
516
517 } else {
518 /*
519 * Create a new REFERENCE_REF node
520 */
521 node = xmlNewReference(doc, val);
Daniel Veillard242590e1998-11-13 18:04:35 +0000522 if (node == NULL) {
Daniel Veillard6454aec1999-09-02 22:04:43 +0000523 if (val != NULL) xmlFree(val);
Daniel Veillard242590e1998-11-13 18:04:35 +0000524 return(ret);
525 }
Daniel Veillard25940b71998-10-29 05:51:30 +0000526 if (last == NULL)
527 last = ret = node;
528 else {
529 last->next = node;
530 node->prev = last;
531 last = node;
532 }
Daniel Veillard16253641998-10-28 22:58:05 +0000533 }
Daniel Veillard6454aec1999-09-02 22:04:43 +0000534 xmlFree(val);
Daniel Veillard16253641998-10-28 22:58:05 +0000535 }
536 cur++;
537 q = cur;
538 } else
539 cur++;
540 }
541 if (cur != q) {
Daniel Veillard25940b71998-10-29 05:51:30 +0000542 /*
543 * Handle the last piece of text.
544 */
545 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
546 xmlNodeAddContentLen(last, q, cur - q);
547 } else {
548 node = xmlNewDocTextLen(doc, q, cur - q);
549 if (node == NULL) return(ret);
550 if (last == NULL)
551 last = ret = node;
552 else {
553 last->next = node;
554 node->prev = last;
555 last = node;
556 }
Daniel Veillard16253641998-10-28 22:58:05 +0000557 }
558 }
559 return(ret);
560}
561
562/**
Daniel Veillardccb09631998-10-27 06:21:04 +0000563 * xmlStringGetNodeList:
564 * @doc: the document
565 * @value: the value of the attribute
566 *
567 * Parse the value string and build the node list associated. Should
568 * produce a flat tree with only TEXTs and ENTITY_REFs.
Daniel Veillard1e346af1999-02-22 10:33:01 +0000569 * Returns a pointer to the first child
Daniel Veillardccb09631998-10-27 06:21:04 +0000570 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000571xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000572xmlStringGetNodeList(xmlDocPtr doc, const xmlChar *value) {
Daniel Veillardccb09631998-10-27 06:21:04 +0000573 xmlNodePtr ret = NULL, last = NULL;
574 xmlNodePtr node;
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000575 xmlChar *val;
576 const xmlChar *cur = value;
577 const xmlChar *q;
Daniel Veillard25940b71998-10-29 05:51:30 +0000578 xmlEntityPtr ent;
Daniel Veillardccb09631998-10-27 06:21:04 +0000579
580 if (value == NULL) return(NULL);
581
582 q = cur;
583 while (*cur != 0) {
584 if (*cur == '&') {
Daniel Veillard25940b71998-10-29 05:51:30 +0000585 /*
586 * Save the current text.
587 */
Daniel Veillardccb09631998-10-27 06:21:04 +0000588 if (cur != q) {
Daniel Veillard25940b71998-10-29 05:51:30 +0000589 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
590 xmlNodeAddContentLen(last, q, cur - q);
591 } else {
592 node = xmlNewDocTextLen(doc, q, cur - q);
593 if (node == NULL) return(ret);
594 if (last == NULL)
595 last = ret = node;
596 else {
597 last->next = node;
598 node->prev = last;
599 last = node;
600 }
Daniel Veillardccb09631998-10-27 06:21:04 +0000601 }
602 }
Daniel Veillard25940b71998-10-29 05:51:30 +0000603 /*
604 * Read the entity string
605 */
Daniel Veillardccb09631998-10-27 06:21:04 +0000606 cur++;
607 q = cur;
608 while ((*cur != 0) && (*cur != ';')) cur++;
609 if (*cur == 0) {
610 fprintf(stderr,
611 "xmlStringGetNodeList: unterminated entity %30s\n", q);
612 return(ret);
613 }
614 if (cur != q) {
Daniel Veillard25940b71998-10-29 05:51:30 +0000615 /*
616 * Predefined entities don't generate nodes
617 */
Daniel Veillardccb09631998-10-27 06:21:04 +0000618 val = xmlStrndup(q, cur - q);
Daniel Veillard25940b71998-10-29 05:51:30 +0000619 ent = xmlGetDocEntity(doc, val);
620 if ((ent != NULL) &&
621 (ent->type == XML_INTERNAL_PREDEFINED_ENTITY)) {
622 if (last == NULL) {
623 node = xmlNewDocText(doc, ent->content);
624 last = ret = node;
625 } else
626 xmlNodeAddContent(last, ent->content);
627
628 } else {
629 /*
630 * Create a new REFERENCE_REF node
631 */
Daniel Veillard25940b71998-10-29 05:51:30 +0000632 node = xmlNewReference(doc, val);
Daniel Veillard242590e1998-11-13 18:04:35 +0000633 if (node == NULL) {
Daniel Veillard6454aec1999-09-02 22:04:43 +0000634 if (val != NULL) xmlFree(val);
Daniel Veillard242590e1998-11-13 18:04:35 +0000635 return(ret);
636 }
Daniel Veillard25940b71998-10-29 05:51:30 +0000637 if (last == NULL)
638 last = ret = node;
639 else {
640 last->next = node;
641 node->prev = last;
642 last = node;
643 }
Daniel Veillardccb09631998-10-27 06:21:04 +0000644 }
Daniel Veillard6454aec1999-09-02 22:04:43 +0000645 xmlFree(val);
Daniel Veillardccb09631998-10-27 06:21:04 +0000646 }
647 cur++;
648 q = cur;
649 } else
650 cur++;
651 }
652 if (cur != q) {
Daniel Veillard25940b71998-10-29 05:51:30 +0000653 /*
654 * Handle the last piece of text.
655 */
656 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
657 xmlNodeAddContentLen(last, q, cur - q);
658 } else {
659 node = xmlNewDocTextLen(doc, q, cur - q);
660 if (node == NULL) return(ret);
661 if (last == NULL)
662 last = ret = node;
663 else {
664 last->next = node;
665 node->prev = last;
666 last = node;
667 }
Daniel Veillardccb09631998-10-27 06:21:04 +0000668 }
669 }
670 return(ret);
671}
672
673/**
674 * xmlNodeListGetString:
675 * @doc: the document
676 * @list: a Node list
677 * @inLine: should we replace entity contents or show their external form
678 *
679 * Returns the string equivalent to the text contained in the Node list
680 * made of TEXTs and ENTITY_REFs
Daniel Veillard1e346af1999-02-22 10:33:01 +0000681 * Returns a pointer to the string copy, the calller must free it.
Daniel Veillardccb09631998-10-27 06:21:04 +0000682 */
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000683xmlChar *
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000684xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine) {
Daniel Veillardccb09631998-10-27 06:21:04 +0000685 xmlNodePtr node = list;
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000686 xmlChar *ret = NULL;
Daniel Veillardccb09631998-10-27 06:21:04 +0000687 xmlEntityPtr ent;
688
689 if (list == NULL) return(NULL);
690
691 while (node != NULL) {
692 if (node->type == XML_TEXT_NODE) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000693 if ((inLine) || (doc->type == XML_HTML_DOCUMENT_NODE)) {
694#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardccb09631998-10-27 06:21:04 +0000695 ret = xmlStrcat(ret, node->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000696#else
697 ret = xmlStrcat(ret, xmlBufferContent(node->content));
698#endif
699 } else {
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000700 xmlChar *buffer;
Daniel Veillard14fff061999-06-22 21:49:07 +0000701
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000702#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard14fff061999-06-22 21:49:07 +0000703 buffer = xmlEncodeEntitiesReentrant(doc, node->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000704#else
705 buffer = xmlEncodeEntitiesReentrant(doc,
706 xmlBufferContent(node->content));
707#endif
Daniel Veillard14fff061999-06-22 21:49:07 +0000708 if (buffer != NULL) {
709 ret = xmlStrcat(ret, buffer);
Daniel Veillard6454aec1999-09-02 22:04:43 +0000710 xmlFree(buffer);
Daniel Veillard14fff061999-06-22 21:49:07 +0000711 }
712 }
Daniel Veillardccb09631998-10-27 06:21:04 +0000713 } else if (node->type == XML_ENTITY_REF_NODE) {
714 if (inLine) {
715 ent = xmlGetDocEntity(doc, node->name);
716 if (ent != NULL)
717 ret = xmlStrcat(ret, ent->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000718 else {
719#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardccb09631998-10-27 06:21:04 +0000720 ret = xmlStrcat(ret, node->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000721#else
722 ret = xmlStrcat(ret, xmlBufferContent(node->content));
723#endif
724 }
Daniel Veillardccb09631998-10-27 06:21:04 +0000725 } else {
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000726 xmlChar buf[2];
Daniel Veillardccb09631998-10-27 06:21:04 +0000727 buf[0] = '&'; buf[1] = 0;
728 ret = xmlStrncat(ret, buf, 1);
729 ret = xmlStrcat(ret, node->name);
730 buf[0] = ';'; buf[1] = 0;
731 ret = xmlStrncat(ret, buf, 1);
732 }
733 }
734#if 0
735 else {
736 fprintf(stderr, "xmlGetNodeListString : invalide node type %d\n",
737 node->type);
738 }
739#endif
740 node = node->next;
741 }
742 return(ret);
743}
744
745/**
Daniel Veillard97b58771998-10-20 06:14:16 +0000746 * xmlNewProp:
747 * @node: the holding node
748 * @name: the name of the attribute
749 * @value: the value of the attribute
750 *
751 * Create a new property carried by a node.
Daniel Veillard1e346af1999-02-22 10:33:01 +0000752 * Returns a pointer to the attribute
Daniel Veillard260a68f1998-08-13 03:39:55 +0000753 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000754xmlAttrPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000755xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000756 xmlAttrPtr cur;
757
758 if (name == NULL) {
759 fprintf(stderr, "xmlNewProp : name == NULL\n");
760 return(NULL);
761 }
762
763 /*
764 * Allocate a new property and fill the fields.
765 */
Daniel Veillard6454aec1999-09-02 22:04:43 +0000766 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
Daniel Veillard260a68f1998-08-13 03:39:55 +0000767 if (cur == NULL) {
768 fprintf(stderr, "xmlNewProp : malloc failed\n");
769 return(NULL);
770 }
771
Daniel Veillard33942841998-10-18 19:12:41 +0000772 cur->type = XML_ATTRIBUTE_NODE;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000773 cur->node = node;
Daniel Veillardb96e6431999-08-29 21:02:19 +0000774 cur->ns = NULL;
775 cur->name = xmlStrdup(name);
Daniel Veillard51e3b151999-11-12 17:02:31 +0000776 if (value != NULL) {
777 xmlChar *buffer;
778 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
779 cur->val = xmlStringGetNodeList(node->doc, buffer);
780 xmlFree(buffer);
781 }
Daniel Veillardb96e6431999-08-29 21:02:19 +0000782 else
783 cur->val = NULL;
784#ifndef XML_WITHOUT_CORBA
785 cur->_private = NULL;
786 cur->vepv = NULL;
787#endif
788
789 /*
790 * Add it at the end to preserve parsing order ...
791 */
792 cur->next = NULL;
793 if (node != NULL) {
794 if (node->properties == NULL) {
795 node->properties = cur;
796 } else {
797 xmlAttrPtr prev = node->properties;
798
799 while (prev->next != NULL) prev = prev->next;
800 prev->next = cur;
801 }
802 }
Daniel Veillard00fdf371999-10-08 09:40:39 +0000803#ifndef XML_WITHOUT_CORBA
804 cur->_private = NULL;
805 cur->vepv = NULL;
806#endif
Daniel Veillardb96e6431999-08-29 21:02:19 +0000807 return(cur);
808}
809
810/**
811 * xmlNewNsProp:
812 * @node: the holding node
813 * @ns: the namespace
814 * @name: the name of the attribute
815 * @value: the value of the attribute
816 *
817 * Create a new property tagged with a namespace and carried by a node.
818 * Returns a pointer to the attribute
819 */
820xmlAttrPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000821xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
822 const xmlChar *value) {
Daniel Veillardb96e6431999-08-29 21:02:19 +0000823 xmlAttrPtr cur;
824
825 if (name == NULL) {
826 fprintf(stderr, "xmlNewProp : name == NULL\n");
827 return(NULL);
828 }
829
830 /*
831 * Allocate a new property and fill the fields.
832 */
Daniel Veillard6454aec1999-09-02 22:04:43 +0000833 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
Daniel Veillardb96e6431999-08-29 21:02:19 +0000834 if (cur == NULL) {
835 fprintf(stderr, "xmlNewProp : malloc failed\n");
836 return(NULL);
837 }
838
839 cur->type = XML_ATTRIBUTE_NODE;
840 cur->node = node;
841 cur->ns = ns;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000842 cur->name = xmlStrdup(name);
843 if (value != NULL)
Daniel Veillardccb09631998-10-27 06:21:04 +0000844 cur->val = xmlStringGetNodeList(node->doc, value);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000845 else
Daniel Veillardccb09631998-10-27 06:21:04 +0000846 cur->val = NULL;
Daniel Veillard27d88741999-05-29 11:51:49 +0000847#ifndef XML_WITHOUT_CORBA
Daniel Veillard27fb0751998-10-17 06:47:46 +0000848 cur->_private = NULL;
849 cur->vepv = NULL;
850#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +0000851
852 /*
853 * Add it at the end to preserve parsing order ...
854 */
855 cur->next = NULL;
856 if (node != NULL) {
857 if (node->properties == NULL) {
858 node->properties = cur;
859 } else {
860 xmlAttrPtr prev = node->properties;
861
862 while (prev->next != NULL) prev = prev->next;
863 prev->next = cur;
864 }
865 }
866 return(cur);
867}
868
Daniel Veillard97b58771998-10-20 06:14:16 +0000869/**
Daniel Veillardccb09631998-10-27 06:21:04 +0000870 * xmlNewDocProp:
871 * @doc: the document
872 * @name: the name of the attribute
873 * @value: the value of the attribute
874 *
875 * Create a new property carried by a document.
Daniel Veillard1e346af1999-02-22 10:33:01 +0000876 * Returns a pointer to the attribute
Daniel Veillardccb09631998-10-27 06:21:04 +0000877 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000878xmlAttrPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000879xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) {
Daniel Veillardccb09631998-10-27 06:21:04 +0000880 xmlAttrPtr cur;
881
882 if (name == NULL) {
883 fprintf(stderr, "xmlNewProp : name == NULL\n");
884 return(NULL);
885 }
886
887 /*
888 * Allocate a new property and fill the fields.
889 */
Daniel Veillard6454aec1999-09-02 22:04:43 +0000890 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
Daniel Veillardccb09631998-10-27 06:21:04 +0000891 if (cur == NULL) {
892 fprintf(stderr, "xmlNewProp : malloc failed\n");
893 return(NULL);
894 }
895
896 cur->type = XML_ATTRIBUTE_NODE;
897 cur->node = NULL;
898 cur->name = xmlStrdup(name);
899 if (value != NULL)
900 cur->val = xmlStringGetNodeList(doc, value);
901 else
902 cur->val = NULL;
Daniel Veillard27d88741999-05-29 11:51:49 +0000903#ifndef XML_WITHOUT_CORBA
Daniel Veillardccb09631998-10-27 06:21:04 +0000904 cur->_private = NULL;
905 cur->vepv = NULL;
906#endif
907
908 cur->next = NULL;
909 return(cur);
910}
911
912/**
Daniel Veillard97b58771998-10-20 06:14:16 +0000913 * xmlFreePropList:
914 * @cur: the first property in the list
915 *
916 * Free a property and all its siblings, all the childs are freed too.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000917 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000918void
919xmlFreePropList(xmlAttrPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000920 xmlAttrPtr next;
921 if (cur == NULL) {
922 fprintf(stderr, "xmlFreePropList : property == NULL\n");
923 return;
924 }
925 while (cur != NULL) {
926 next = cur->next;
927 xmlFreeProp(cur);
928 cur = next;
929 }
930}
931
Daniel Veillard97b58771998-10-20 06:14:16 +0000932/**
933 * xmlFreeProp:
Daniel Veillard686d6b62000-01-03 11:08:02 +0000934 * @cur: an attribute
Daniel Veillard97b58771998-10-20 06:14:16 +0000935 *
Daniel Veillard686d6b62000-01-03 11:08:02 +0000936 * Free one attribute, all the content is freed too
Daniel Veillard260a68f1998-08-13 03:39:55 +0000937 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000938void
939xmlFreeProp(xmlAttrPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000940 if (cur == NULL) {
941 fprintf(stderr, "xmlFreeProp : property == NULL\n");
942 return;
943 }
Daniel Veillard6454aec1999-09-02 22:04:43 +0000944 if (cur->name != NULL) xmlFree((char *) cur->name);
Daniel Veillardccb09631998-10-27 06:21:04 +0000945 if (cur->val != NULL) xmlFreeNodeList(cur->val);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000946 memset(cur, -1, sizeof(xmlAttr));
Daniel Veillard6454aec1999-09-02 22:04:43 +0000947 xmlFree(cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000948}
949
Daniel Veillard97b58771998-10-20 06:14:16 +0000950/**
Daniel Veillard686d6b62000-01-03 11:08:02 +0000951 * xmlRemoveProp:
952 * @cur: an attribute
953 *
954 * Unlink and free one attribute, all the content is freed too
955 * Note this doesn't work for namespace definition attributes
956 *
957 * Returns 0 if success and -1 in case of error.
958 */
959int
960xmlRemoveProp(xmlAttrPtr cur) {
961 xmlAttrPtr tmp;
962 if (cur == NULL) {
963 fprintf(stderr, "xmlRemoveProp : cur == NULL\n");
964 return(-1);
965 }
966 if (cur->node == NULL) {
967 fprintf(stderr, "xmlRemoveProp : cur->node == NULL\n");
968 return(-1);
969 }
970 tmp = cur->node->properties;
971 if (tmp == cur) {
972 cur->node->properties = cur->next;
973 xmlFreeProp(cur);
974 return(0);
975 }
976 while (tmp != NULL) {
977 if (tmp->next == cur) {
978 tmp->next = cur->next;
979 xmlFreeProp(cur);
980 return(0);
981 }
982 tmp = tmp->next;
983 }
984 fprintf(stderr, "xmlRemoveProp : attribute not owned by its node\n");
985 return(-1);
986}
987
988/**
Daniel Veillardb96e6431999-08-29 21:02:19 +0000989 * xmlNewPI:
990 * @name: the processing instruction name
991 * @content: the PI content
992 *
993 * Creation of a processing instruction element.
994 * Returns a pointer to the new node object.
995 */
996xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000997xmlNewPI(const xmlChar *name, const xmlChar *content) {
Daniel Veillardb96e6431999-08-29 21:02:19 +0000998 xmlNodePtr cur;
999
1000 if (name == NULL) {
1001 fprintf(stderr, "xmlNewPI : name == NULL\n");
1002 return(NULL);
1003 }
1004
1005 /*
1006 * Allocate a new node and fill the fields.
1007 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00001008 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillardb96e6431999-08-29 21:02:19 +00001009 if (cur == NULL) {
1010 fprintf(stderr, "xmlNewPI : malloc failed\n");
1011 return(NULL);
1012 }
1013
1014 cur->type = XML_PI_NODE;
1015 cur->doc = NULL;
1016 cur->parent = NULL;
1017 cur->next = NULL;
1018 cur->prev = NULL;
1019 cur->childs = NULL;
1020 cur->last = NULL;
1021 cur->properties = NULL;
1022 cur->name = xmlStrdup(name);
1023 cur->ns = NULL;
1024 cur->nsDef = NULL;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001025 if (content != NULL) {
1026#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardb96e6431999-08-29 21:02:19 +00001027 cur->content = xmlStrdup(content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001028#else
1029 cur->content = xmlBufferCreateSize(0);
1030 xmlBufferSetAllocationScheme(cur->content,
1031 xmlGetBufferAllocationScheme());
1032 xmlBufferAdd(cur->content, content, -1);
1033#endif
1034 } else
Daniel Veillardb96e6431999-08-29 21:02:19 +00001035 cur->content = NULL;
1036#ifndef XML_WITHOUT_CORBA
1037 cur->_private = NULL;
1038 cur->vepv = NULL;
1039#endif
1040 return(cur);
1041}
1042
1043/**
Daniel Veillard97b58771998-10-20 06:14:16 +00001044 * xmlNewNode:
1045 * @ns: namespace if any
1046 * @name: the node name
Daniel Veillard97b58771998-10-20 06:14:16 +00001047 *
1048 * Creation of a new node element. @ns and @content are optionnal (NULL).
Daniel Veillardccb09631998-10-27 06:21:04 +00001049 * If content is non NULL, a child list containing the TEXTs and
1050 * ENTITY_REFs node will be created.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001051 * Returns a pointer to the new node object.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001052 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001053xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001054xmlNewNode(xmlNsPtr ns, const xmlChar *name) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001055 xmlNodePtr cur;
1056
1057 if (name == NULL) {
1058 fprintf(stderr, "xmlNewNode : name == NULL\n");
1059 return(NULL);
1060 }
1061
1062 /*
1063 * Allocate a new node and fill the fields.
1064 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00001065 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillard260a68f1998-08-13 03:39:55 +00001066 if (cur == NULL) {
1067 fprintf(stderr, "xmlNewNode : malloc failed\n");
1068 return(NULL);
1069 }
1070
Daniel Veillard33942841998-10-18 19:12:41 +00001071 cur->type = XML_ELEMENT_NODE;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001072 cur->doc = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001073 cur->parent = NULL;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001074 cur->next = NULL;
1075 cur->prev = NULL;
1076 cur->childs = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001077 cur->last = NULL;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001078 cur->properties = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001079 cur->name = xmlStrdup(name);
1080 cur->ns = ns;
1081 cur->nsDef = NULL;
Daniel Veillardccb09631998-10-27 06:21:04 +00001082 cur->content = NULL;
Daniel Veillard27d88741999-05-29 11:51:49 +00001083#ifndef XML_WITHOUT_CORBA
Daniel Veillard27fb0751998-10-17 06:47:46 +00001084 cur->_private = NULL;
1085 cur->vepv = NULL;
1086#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00001087 return(cur);
1088}
1089
Daniel Veillard97b58771998-10-20 06:14:16 +00001090/**
1091 * xmlNewDocNode:
1092 * @doc: the document
1093 * @ns: namespace if any
1094 * @name: the node name
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001095 * @content: the XML text content if any
Daniel Veillard97b58771998-10-20 06:14:16 +00001096 *
1097 * Creation of a new node element within a document. @ns and @content
1098 * are optionnal (NULL).
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001099 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
1100 * references, but XML special chars need to be escaped first by using
1101 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
1102 * need entities support.
1103 *
Daniel Veillard1e346af1999-02-22 10:33:01 +00001104 * Returns a pointer to the new node object.
Daniel Veillard97b58771998-10-20 06:14:16 +00001105 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001106xmlNodePtr
1107xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns,
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001108 const xmlChar *name, const xmlChar *content) {
Daniel Veillard0bef1311998-10-14 02:36:47 +00001109 xmlNodePtr cur;
1110
Daniel Veillardccb09631998-10-27 06:21:04 +00001111 cur = xmlNewNode(ns, name);
1112 if (cur != NULL) {
1113 cur->doc = doc;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001114 if (content != NULL) {
Daniel Veillardccb09631998-10-27 06:21:04 +00001115 cur->childs = xmlStringGetNodeList(doc, content);
Daniel Veillard1e346af1999-02-22 10:33:01 +00001116 UPDATE_LAST_CHILD(cur)
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001117 }
Daniel Veillardccb09631998-10-27 06:21:04 +00001118 }
Daniel Veillard0bef1311998-10-14 02:36:47 +00001119 return(cur);
1120}
1121
1122
Daniel Veillard97b58771998-10-20 06:14:16 +00001123/**
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001124 * xmlNewDocRawNode:
1125 * @doc: the document
1126 * @ns: namespace if any
1127 * @name: the node name
1128 * @content: the text content if any
1129 *
1130 * Creation of a new node element within a document. @ns and @content
1131 * are optionnal (NULL).
1132 *
1133 * Returns a pointer to the new node object.
1134 */
1135xmlNodePtr
1136xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns,
1137 const xmlChar *name, const xmlChar *content) {
1138 xmlNodePtr cur;
1139
1140 cur = xmlNewNode(ns, name);
1141 if (cur != NULL) {
1142 cur->doc = doc;
1143 if (content != NULL) {
1144 cur->childs = xmlNewDocText(doc, content);
1145 UPDATE_LAST_CHILD(cur)
1146 }
1147 }
1148 return(cur);
1149}
1150
1151
1152/**
Daniel Veillard97b58771998-10-20 06:14:16 +00001153 * xmlNewText:
1154 * @content: the text content
1155 *
1156 * Creation of a new text node.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001157 * Returns a pointer to the new node object.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001158 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001159xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001160xmlNewText(const xmlChar *content) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001161 xmlNodePtr cur;
1162
1163 /*
1164 * Allocate a new node and fill the fields.
1165 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00001166 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillard260a68f1998-08-13 03:39:55 +00001167 if (cur == NULL) {
1168 fprintf(stderr, "xmlNewText : malloc failed\n");
1169 return(NULL);
1170 }
1171
Daniel Veillard33942841998-10-18 19:12:41 +00001172 cur->type = XML_TEXT_NODE;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001173 cur->doc = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001174 cur->parent = NULL;
1175 cur->next = NULL;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001176 cur->prev = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001177 cur->childs = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001178 cur->last = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001179 cur->properties = NULL;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001180 cur->type = XML_TEXT_NODE;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001181 cur->name = xmlStrdup(xmlStringText);
1182 cur->ns = NULL;
1183 cur->nsDef = NULL;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001184 if (content != NULL) {
1185#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard260a68f1998-08-13 03:39:55 +00001186 cur->content = xmlStrdup(content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001187#else
1188 cur->content = xmlBufferCreateSize(0);
1189 xmlBufferSetAllocationScheme(cur->content,
1190 xmlGetBufferAllocationScheme());
1191 xmlBufferAdd(cur->content, content, -1);
1192#endif
1193 } else
Daniel Veillard260a68f1998-08-13 03:39:55 +00001194 cur->content = NULL;
Daniel Veillard00fdf371999-10-08 09:40:39 +00001195#ifndef XML_WITHOUT_CORBA
1196 cur->_private = NULL;
1197 cur->vepv = NULL;
1198#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00001199 return(cur);
1200}
1201
Daniel Veillard97b58771998-10-20 06:14:16 +00001202/**
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001203 * xmlNewTextChild:
1204 * @parent: the parent node
1205 * @ns: a namespace if any
1206 * @name: the name of the child
1207 * @content: the text content of the child if any.
1208 *
1209 * Creation of a new child element, added at the end of @parent childs list.
1210 * @ns and @content parameters are optionnal (NULL). If content is non NULL,
1211 * a child TEXT node will be created containing the string content.
1212 *
1213 * Returns a pointer to the new node object.
1214 */
1215xmlNodePtr
1216xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns,
1217 const xmlChar *name, const xmlChar *content) {
1218 xmlNodePtr cur, prev;
1219
1220 if (parent == NULL) {
1221 fprintf(stderr, "xmlNewTextChild : parent == NULL\n");
1222 return(NULL);
1223 }
1224
1225 if (name == NULL) {
1226 fprintf(stderr, "xmlNewTextChild : name == NULL\n");
1227 return(NULL);
1228 }
1229
1230 /*
1231 * Allocate a new node
1232 */
1233 if (ns == NULL)
1234 cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content);
1235 else
1236 cur = xmlNewDocRawNode(parent->doc, ns, name, content);
1237 if (cur == NULL) return(NULL);
1238
1239 /*
1240 * add the new element at the end of the childs list.
1241 */
1242 cur->type = XML_ELEMENT_NODE;
1243 cur->parent = parent;
1244 cur->doc = parent->doc;
1245 if (parent->childs == NULL) {
1246 parent->childs = cur;
1247 parent->last = cur;
1248 } else {
1249 prev = parent->last;
1250 prev->next = cur;
1251 cur->prev = prev;
1252 parent->last = cur;
1253 }
1254
1255 return(cur);
1256}
1257
1258/**
Daniel Veillardccb09631998-10-27 06:21:04 +00001259 * xmlNewReference:
1260 * @doc: the document
1261 * @name: the reference name, or the reference string with & and ;
1262 *
1263 * Creation of a new reference node.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001264 * Returns a pointer to the new node object.
Daniel Veillardccb09631998-10-27 06:21:04 +00001265 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001266xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001267xmlNewReference(xmlDocPtr doc, const xmlChar *name) {
Daniel Veillardccb09631998-10-27 06:21:04 +00001268 xmlNodePtr cur;
1269 xmlEntityPtr ent;
1270
1271 /*
1272 * Allocate a new node and fill the fields.
1273 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00001274 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillardccb09631998-10-27 06:21:04 +00001275 if (cur == NULL) {
1276 fprintf(stderr, "xmlNewText : malloc failed\n");
1277 return(NULL);
1278 }
1279
1280 cur->type = XML_ENTITY_REF_NODE;
Daniel Veillard10c6a8f1998-10-28 01:00:12 +00001281 cur->doc = doc;
Daniel Veillardccb09631998-10-27 06:21:04 +00001282 cur->parent = NULL;
1283 cur->next = NULL;
1284 cur->prev = NULL;
1285 cur->childs = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001286 cur->last = NULL;
Daniel Veillardccb09631998-10-27 06:21:04 +00001287 cur->properties = NULL;
1288 if (name[0] == '&') {
1289 int len;
1290 name++;
1291 len = xmlStrlen(name);
1292 if (name[len - 1] == ';')
1293 cur->name = xmlStrndup(name, len - 1);
1294 else
1295 cur->name = xmlStrndup(name, len);
1296 } else
1297 cur->name = xmlStrdup(name);
1298 cur->ns = NULL;
1299 cur->nsDef = NULL;
1300
1301 ent = xmlGetDocEntity(doc, cur->name);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001302 if (ent != NULL) {
1303#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardccb09631998-10-27 06:21:04 +00001304 cur->content = ent->content;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001305#else
1306 /*
1307 * CJN 11.18.99 this might be a problem, since the xmlBuffer gets
1308 * a copy of this pointer. Let's hope we don't manipulate it
1309 * later
1310 */
1311 cur->content = xmlBufferCreateSize(0);
1312 xmlBufferSetAllocationScheme(cur->content,
1313 xmlGetBufferAllocationScheme());
1314 if (ent->content != NULL)
1315 xmlBufferAdd(cur->content, ent->content, -1);
1316#endif
1317 } else
Daniel Veillardccb09631998-10-27 06:21:04 +00001318 cur->content = NULL;
Daniel Veillard00fdf371999-10-08 09:40:39 +00001319#ifndef XML_WITHOUT_CORBA
1320 cur->_private = NULL;
1321 cur->vepv = NULL;
1322#endif
Daniel Veillardccb09631998-10-27 06:21:04 +00001323 return(cur);
1324}
1325
1326/**
Daniel Veillard97b58771998-10-20 06:14:16 +00001327 * xmlNewDocText:
1328 * @doc: the document
1329 * @content: the text content
1330 *
1331 * Creation of a new text node within a document.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001332 * Returns a pointer to the new node object.
Daniel Veillard97b58771998-10-20 06:14:16 +00001333 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001334xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001335xmlNewDocText(xmlDocPtr doc, const xmlChar *content) {
Daniel Veillard0bef1311998-10-14 02:36:47 +00001336 xmlNodePtr cur;
1337
1338 cur = xmlNewText(content);
1339 if (cur != NULL) cur->doc = doc;
1340 return(cur);
1341}
1342
Daniel Veillard97b58771998-10-20 06:14:16 +00001343/**
Daniel Veillardccb09631998-10-27 06:21:04 +00001344 * xmlNewTextLen:
Daniel Veillard97b58771998-10-20 06:14:16 +00001345 * @content: the text content
1346 * @len: the text len.
1347 *
1348 * Creation of a new text node with an extra parameter for the content's lenght
Daniel Veillard1e346af1999-02-22 10:33:01 +00001349 * Returns a pointer to the new node object.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001350 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001351xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001352xmlNewTextLen(const xmlChar *content, int len) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001353 xmlNodePtr cur;
1354
1355 /*
1356 * Allocate a new node and fill the fields.
1357 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00001358 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillard260a68f1998-08-13 03:39:55 +00001359 if (cur == NULL) {
1360 fprintf(stderr, "xmlNewText : malloc failed\n");
1361 return(NULL);
1362 }
1363
Daniel Veillard33942841998-10-18 19:12:41 +00001364 cur->type = XML_TEXT_NODE;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001365 cur->doc = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001366 cur->parent = NULL;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001367 cur->prev = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001368 cur->next = NULL;
1369 cur->childs = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001370 cur->last = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001371 cur->properties = NULL;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001372 cur->type = XML_TEXT_NODE;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001373 cur->name = xmlStrdup(xmlStringText);
1374 cur->ns = NULL;
1375 cur->nsDef = NULL;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001376 if (content != NULL) {
1377#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard260a68f1998-08-13 03:39:55 +00001378 cur->content = xmlStrndup(content, len);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001379#else
1380 cur->content = xmlBufferCreateSize(len);
1381 xmlBufferSetAllocationScheme(cur->content,
1382 xmlGetBufferAllocationScheme());
1383 xmlBufferAdd(cur->content, content, len);
1384#endif
1385 } else
Daniel Veillard260a68f1998-08-13 03:39:55 +00001386 cur->content = NULL;
Daniel Veillard00fdf371999-10-08 09:40:39 +00001387#ifndef XML_WITHOUT_CORBA
1388 cur->_private = NULL;
1389 cur->vepv = NULL;
1390#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00001391 return(cur);
1392}
1393
Daniel Veillard97b58771998-10-20 06:14:16 +00001394/**
1395 * xmlNewDocTextLen:
1396 * @doc: the document
1397 * @content: the text content
1398 * @len: the text len.
1399 *
1400 * Creation of a new text node with an extra content lenght parameter. The
1401 * text node pertain to a given document.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001402 * Returns a pointer to the new node object.
Daniel Veillard97b58771998-10-20 06:14:16 +00001403 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001404xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001405xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) {
Daniel Veillard0bef1311998-10-14 02:36:47 +00001406 xmlNodePtr cur;
1407
1408 cur = xmlNewTextLen(content, len);
1409 if (cur != NULL) cur->doc = doc;
1410 return(cur);
1411}
1412
Daniel Veillard97b58771998-10-20 06:14:16 +00001413/**
1414 * xmlNewComment:
1415 * @content: the comment content
1416 *
1417 * Creation of a new node containing a comment.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001418 * Returns a pointer to the new node object.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001419 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001420xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001421xmlNewComment(const xmlChar *content) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001422 xmlNodePtr cur;
1423
1424 /*
1425 * Allocate a new node and fill the fields.
1426 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00001427 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillard260a68f1998-08-13 03:39:55 +00001428 if (cur == NULL) {
1429 fprintf(stderr, "xmlNewComment : malloc failed\n");
1430 return(NULL);
1431 }
1432
Daniel Veillard33942841998-10-18 19:12:41 +00001433 cur->type = XML_COMMENT_NODE;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001434 cur->doc = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001435 cur->parent = NULL;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001436 cur->prev = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001437 cur->next = NULL;
1438 cur->childs = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001439 cur->last = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001440 cur->properties = NULL;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001441 cur->type = XML_COMMENT_NODE;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001442 cur->name = xmlStrdup(xmlStringText);
1443 cur->ns = NULL;
1444 cur->nsDef = NULL;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001445 if (content != NULL) {
1446#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard260a68f1998-08-13 03:39:55 +00001447 cur->content = xmlStrdup(content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001448#else
1449 cur->content = xmlBufferCreateSize(0);
1450 xmlBufferSetAllocationScheme(cur->content,
1451 xmlGetBufferAllocationScheme());
1452 xmlBufferAdd(cur->content, content, -1);
1453#endif
1454 } else
Daniel Veillard260a68f1998-08-13 03:39:55 +00001455 cur->content = NULL;
Daniel Veillard00fdf371999-10-08 09:40:39 +00001456#ifndef XML_WITHOUT_CORBA
1457 cur->_private = NULL;
1458 cur->vepv = NULL;
1459#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00001460 return(cur);
1461}
1462
Daniel Veillard97b58771998-10-20 06:14:16 +00001463/**
Daniel Veillardb05deb71999-08-10 19:04:08 +00001464 * xmlNewCDataBlock:
1465 * @doc: the document
1466 * @content: the CData block content content
1467 * @len: the length of the block
1468 *
1469 * Creation of a new node containing a CData block.
1470 * Returns a pointer to the new node object.
1471 */
1472xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001473xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) {
Daniel Veillardb05deb71999-08-10 19:04:08 +00001474 xmlNodePtr cur;
1475
1476 /*
1477 * Allocate a new node and fill the fields.
1478 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00001479 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillardb05deb71999-08-10 19:04:08 +00001480 if (cur == NULL) {
1481 fprintf(stderr, "xmlNewCDataBlock : malloc failed\n");
1482 return(NULL);
1483 }
1484
1485 cur->type = XML_CDATA_SECTION_NODE;
1486 cur->doc = NULL;
1487 cur->parent = NULL;
1488 cur->prev = NULL;
1489 cur->next = NULL;
1490 cur->childs = NULL;
1491 cur->last = NULL;
1492 cur->properties = NULL;
1493 cur->name = xmlStrdup(xmlStringText);
1494 cur->ns = NULL;
1495 cur->nsDef = NULL;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001496 if (content != NULL) {
1497#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardb05deb71999-08-10 19:04:08 +00001498 cur->content = xmlStrndup(content, len);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001499#else
1500 cur->content = xmlBufferCreateSize(len);
1501 xmlBufferSetAllocationScheme(cur->content,
1502 xmlGetBufferAllocationScheme());
1503 xmlBufferAdd(cur->content, content, len);
1504#endif
Daniel Veillardb05deb71999-08-10 19:04:08 +00001505 } else
1506 cur->content = NULL;
Daniel Veillard00fdf371999-10-08 09:40:39 +00001507#ifndef XML_WITHOUT_CORBA
1508 cur->_private = NULL;
1509 cur->vepv = NULL;
1510#endif
Daniel Veillardb05deb71999-08-10 19:04:08 +00001511 return(cur);
1512}
1513
1514/**
Daniel Veillard1e346af1999-02-22 10:33:01 +00001515 * xmlNewDocComment:
Daniel Veillard97b58771998-10-20 06:14:16 +00001516 * @doc: the document
1517 * @content: the comment content
1518 *
1519 * Creation of a new node containing a commentwithin a document.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001520 * Returns a pointer to the new node object.
Daniel Veillard97b58771998-10-20 06:14:16 +00001521 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001522xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001523xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) {
Daniel Veillard0bef1311998-10-14 02:36:47 +00001524 xmlNodePtr cur;
1525
1526 cur = xmlNewComment(content);
1527 if (cur != NULL) cur->doc = doc;
1528 return(cur);
1529}
1530
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001531
Daniel Veillard97b58771998-10-20 06:14:16 +00001532/**
1533 * xmlNewChild:
1534 * @parent: the parent node
1535 * @ns: a namespace if any
1536 * @name: the name of the child
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001537 * @content: the XML content of the child if any.
Daniel Veillard97b58771998-10-20 06:14:16 +00001538 *
Daniel Veillard97b58771998-10-20 06:14:16 +00001539 * Creation of a new child element, added at the end of @parent childs list.
Daniel Veillardccb09631998-10-27 06:21:04 +00001540 * @ns and @content parameters are optionnal (NULL). If content is non NULL,
1541 * a child list containing the TEXTs and ENTITY_REFs node will be created.
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001542 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
1543 * references, but XML special chars need to be escaped first by using
1544 * xmlEncodeEntitiesReentrant(). Use xmlNewTextChild() if entities
1545 * support is not needed.
1546 *
Daniel Veillard1e346af1999-02-22 10:33:01 +00001547 * Returns a pointer to the new node object.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001548 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001549xmlNodePtr
1550xmlNewChild(xmlNodePtr parent, xmlNsPtr ns,
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001551 const xmlChar *name, const xmlChar *content) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001552 xmlNodePtr cur, prev;
1553
1554 if (parent == NULL) {
1555 fprintf(stderr, "xmlNewChild : parent == NULL\n");
1556 return(NULL);
1557 }
1558
1559 if (name == NULL) {
1560 fprintf(stderr, "xmlNewChild : name == NULL\n");
1561 return(NULL);
1562 }
1563
1564 /*
1565 * Allocate a new node
1566 */
1567 if (ns == NULL)
Daniel Veillardccb09631998-10-27 06:21:04 +00001568 cur = xmlNewDocNode(parent->doc, parent->ns, name, content);
Daniel Veillard260a68f1998-08-13 03:39:55 +00001569 else
Daniel Veillardccb09631998-10-27 06:21:04 +00001570 cur = xmlNewDocNode(parent->doc, ns, name, content);
Daniel Veillard260a68f1998-08-13 03:39:55 +00001571 if (cur == NULL) return(NULL);
1572
1573 /*
1574 * add the new element at the end of the childs list.
1575 */
Daniel Veillardccb09631998-10-27 06:21:04 +00001576 cur->type = XML_ELEMENT_NODE;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001577 cur->parent = parent;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001578 cur->doc = parent->doc;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001579 if (parent->childs == NULL) {
1580 parent->childs = cur;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001581 parent->last = cur;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001582 } else {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001583 prev = parent->last;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001584 prev->next = cur;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001585 cur->prev = prev;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001586 parent->last = cur;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001587 }
1588
1589 return(cur);
1590}
1591
Daniel Veillard97b58771998-10-20 06:14:16 +00001592/**
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001593 * xmlAddNextSibling:
1594 * @cur: the child node
1595 * @elem: the new node
1596 *
1597 * Add a new element @elem as the next siblings of @cur
1598 * If the new element was already inserted in a document it is
1599 * first unlinked from its existing context.
1600 *
1601 * Returns the new element or NULL in case of error.
1602 */
1603xmlNodePtr
1604xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) {
1605 if (cur == NULL) {
1606 fprintf(stderr, "xmlAddNextSibling : cur == NULL\n");
1607 return(NULL);
1608 }
1609 if (elem == NULL) {
1610 fprintf(stderr, "xmlAddNextSibling : elem == NULL\n");
1611 return(NULL);
1612 }
1613
1614 xmlUnlinkNode(elem);
1615 elem->doc = cur->doc;
1616 elem->parent = cur->parent;
1617 elem->next = cur;
1618 elem->prev = cur->prev;
1619 cur->prev = elem;
1620 if (elem->prev != NULL)
1621 elem->prev->next = elem;
1622 if ((elem->parent != NULL) && (elem->parent->childs == cur))
1623 elem->parent->childs = elem;
1624 return(elem);
1625}
1626
1627/**
1628 * xmlAddPrevSibling:
1629 * @cur: the child node
1630 * @elem: the new node
1631 *
1632 * Add a new element @elem as the previous siblings of @cur
1633 * If the new element was already inserted in a document it is
1634 * first unlinked from its existing context.
1635 *
1636 * Returns the new element or NULL in case of error.
1637 */
1638xmlNodePtr
1639xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) {
1640 if (cur == NULL) {
1641 fprintf(stderr, "xmlAddPrevSibling : cur == NULL\n");
1642 return(NULL);
1643 }
1644 if (elem == NULL) {
1645 fprintf(stderr, "xmlAddPrevSibling : elem == NULL\n");
1646 return(NULL);
1647 }
1648
1649 xmlUnlinkNode(elem);
1650 elem->doc = cur->doc;
1651 elem->parent = cur->parent;
1652 elem->prev = cur;
1653 elem->next = cur->next;
1654 cur->next = elem;
1655 if (elem->next != NULL)
1656 elem->next->prev = elem;
1657 if ((elem->parent != NULL) && (elem->parent->last == cur))
1658 elem->parent->last = elem;
1659 return(elem);
1660}
1661
1662/**
Daniel Veillardb96e6431999-08-29 21:02:19 +00001663 * xmlAddSibling:
1664 * @cur: the child node
1665 * @elem: the new node
1666 *
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001667 * Add a new element @elem to the list of siblings of @cur
1668 * If the new element was already inserted in a document it is
1669 * first unlinked from its existing context.
1670 *
1671 * Returns the new element or NULL in case of error.
Daniel Veillardb96e6431999-08-29 21:02:19 +00001672 */
1673xmlNodePtr
1674xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) {
1675 xmlNodePtr parent;
1676
1677 if (cur == NULL) {
1678 fprintf(stderr, "xmlAddSibling : cur == NULL\n");
1679 return(NULL);
1680 }
1681
1682 if (elem == NULL) {
1683 fprintf(stderr, "xmlAddSibling : elem == NULL\n");
1684 return(NULL);
1685 }
1686
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001687 /*
1688 * Constant time is we can rely on the ->parent->last to find
1689 * the last sibling.
1690 */
1691 if ((cur->parent != NULL) &&
1692 (cur->parent->childs != NULL) &&
1693 (cur->parent->last != NULL) &&
1694 (cur->parent->last->next == NULL)) {
1695 cur = cur->parent->last;
1696 } else {
1697 while (cur->next != NULL) cur = cur->next;
Daniel Veillardb96e6431999-08-29 21:02:19 +00001698 }
1699
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001700 xmlUnlinkNode(elem);
Daniel Veillardb96e6431999-08-29 21:02:19 +00001701 if (elem->doc == NULL)
1702 elem->doc = cur->doc; /* the parent may not be linked to a doc ! */
1703
1704 parent = cur->parent;
1705 elem->prev = cur;
1706 elem->next = NULL;
1707 elem->parent = parent;
1708 cur->next = elem;
1709 if (parent != NULL)
1710 parent->last = elem;
1711
1712 return(elem);
1713}
1714
1715/**
Daniel Veillard97b58771998-10-20 06:14:16 +00001716 * xmlAddChild:
1717 * @parent: the parent node
1718 * @cur: the child node
1719 *
1720 * Add a new child element, to @parent, at the end of the child list.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001721 * Returns the child or NULL in case of error.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001722 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001723xmlNodePtr
1724xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001725 xmlNodePtr prev;
1726
1727 if (parent == NULL) {
Daniel Veillard10a2c651999-12-12 13:03:50 +00001728 fprintf(stderr, "xmlAddChild : parent == NULL\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +00001729 return(NULL);
1730 }
1731
1732 if (cur == NULL) {
Daniel Veillard10a2c651999-12-12 13:03:50 +00001733 fprintf(stderr, "xmlAddChild : child == NULL\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +00001734 return(NULL);
1735 }
1736
Daniel Veillard0bef1311998-10-14 02:36:47 +00001737 if ((cur->doc != NULL) && (parent->doc != NULL) &&
1738 (cur->doc != parent->doc)) {
1739 fprintf(stderr, "Elements moved to a different document\n");
1740 }
1741
Daniel Veillard260a68f1998-08-13 03:39:55 +00001742 /*
1743 * add the new element at the end of the childs list.
1744 */
1745 cur->parent = parent;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001746 cur->doc = parent->doc; /* the parent may not be linked to a doc ! */
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001747
Daniel Veillardccb09631998-10-27 06:21:04 +00001748 /*
1749 * Handle the case where parent->content != NULL, in that case it will
1750 * create a intermediate TEXT node.
1751 */
1752 if (parent->content != NULL) {
1753 xmlNodePtr text;
1754
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001755#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardccb09631998-10-27 06:21:04 +00001756 text = xmlNewDocText(parent->doc, parent->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001757#else
1758 text = xmlNewDocText(parent->doc, xmlBufferContent(parent->content));
1759#endif
Daniel Veillardccb09631998-10-27 06:21:04 +00001760 if (text != NULL) {
1761 text->next = parent->childs;
1762 if (text->next != NULL)
1763 text->next->prev = text;
1764 parent->childs = text;
Daniel Veillard1e346af1999-02-22 10:33:01 +00001765 UPDATE_LAST_CHILD(parent)
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001766#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard6454aec1999-09-02 22:04:43 +00001767 xmlFree(parent->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001768#else
1769 xmlBufferFree(parent->content);
1770#endif
Daniel Veillardccb09631998-10-27 06:21:04 +00001771 parent->content = NULL;
1772 }
1773 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00001774 if (parent->childs == NULL) {
1775 parent->childs = cur;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001776 parent->last = cur;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001777 } else {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001778 prev = parent->last;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001779 prev->next = cur;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001780 cur->prev = prev;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001781 parent->last = cur;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001782 }
1783
1784 return(cur);
1785}
1786
Daniel Veillard97b58771998-10-20 06:14:16 +00001787/**
1788 * xmlGetLastChild:
1789 * @parent: the parent node
1790 *
1791 * Search the last child of a node.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001792 * Returns the last child or NULL if none.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001793 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001794xmlNodePtr
1795xmlGetLastChild(xmlNodePtr parent) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001796 if (parent == NULL) {
1797 fprintf(stderr, "xmlGetLastChild : parent == NULL\n");
1798 return(NULL);
1799 }
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001800 return(parent->last);
Daniel Veillard260a68f1998-08-13 03:39:55 +00001801}
1802
Daniel Veillard97b58771998-10-20 06:14:16 +00001803/**
1804 * xmlFreeNodeList:
1805 * @cur: the first node in the list
1806 *
1807 * Free a node and all its siblings, this is a recursive behaviour, all
1808 * the childs are freed too.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001809 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001810void
1811xmlFreeNodeList(xmlNodePtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001812 xmlNodePtr next;
1813 if (cur == NULL) {
1814 fprintf(stderr, "xmlFreeNodeList : node == NULL\n");
1815 return;
1816 }
1817 while (cur != NULL) {
1818 next = cur->next;
1819 xmlFreeNode(cur);
1820 cur = next;
1821 }
1822}
1823
Daniel Veillard97b58771998-10-20 06:14:16 +00001824/**
1825 * xmlFreeNode:
1826 * @cur: the node
1827 *
1828 * Free a node, this is a recursive behaviour, all the childs are freed too.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001829 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001830void
1831xmlFreeNode(xmlNodePtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001832 if (cur == NULL) {
1833 fprintf(stderr, "xmlFreeNode : node == NULL\n");
1834 return;
1835 }
Daniel Veillardccb09631998-10-27 06:21:04 +00001836 cur->doc = NULL;
1837 cur->parent = NULL;
1838 cur->next = NULL;
1839 cur->prev = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001840 if (cur->childs != NULL) xmlFreeNodeList(cur->childs);
Daniel Veillardccb09631998-10-27 06:21:04 +00001841 if (cur->properties != NULL) xmlFreePropList(cur->properties);
1842 if (cur->type != XML_ENTITY_REF_NODE)
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001843#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard6454aec1999-09-02 22:04:43 +00001844 if (cur->content != NULL) xmlFree(cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001845#else
1846 if (cur->content != NULL) xmlBufferFree(cur->content);
1847#endif
Daniel Veillard6454aec1999-09-02 22:04:43 +00001848 if (cur->name != NULL) xmlFree((char *) cur->name);
Daniel Veillard260a68f1998-08-13 03:39:55 +00001849 if (cur->nsDef != NULL) xmlFreeNsList(cur->nsDef);
1850 memset(cur, -1, sizeof(xmlNode));
Daniel Veillard6454aec1999-09-02 22:04:43 +00001851 xmlFree(cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +00001852}
1853
Daniel Veillard16253641998-10-28 22:58:05 +00001854/**
1855 * xmlUnlinkNode:
1856 * @cur: the node
1857 *
1858 * Unlink a node from it's current context, the node is not freed
1859 */
1860void
1861xmlUnlinkNode(xmlNodePtr cur) {
1862 if (cur == NULL) {
1863 fprintf(stderr, "xmlUnlinkNode : node == NULL\n");
1864 return;
1865 }
1866 if ((cur->parent != NULL) && (cur->parent->childs == cur))
1867 cur->parent->childs = cur->next;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001868 if ((cur->parent != NULL) && (cur->parent->last == cur))
1869 cur->parent->last = cur->prev;
Daniel Veillard16253641998-10-28 22:58:05 +00001870 if (cur->next != NULL)
1871 cur->next->prev = cur->prev;
1872 if (cur->prev != NULL)
1873 cur->prev->next = cur->next;
1874 cur->next = cur->prev = NULL;
1875 cur->parent = NULL;
1876}
1877
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001878/**
1879 * xmlReplaceNode:
1880 * @old: the old node
1881 * @cur: the node
1882 *
1883 * Unlink the old node from it's current context, prune the new one
1884 * at the same place. If cur was already inserted in a document it is
1885 * first unlinked from its existing context.
1886 *
1887 * Returns the old node
1888 */
1889xmlNodePtr
1890xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) {
1891 if (old == NULL) {
1892 fprintf(stderr, "xmlReplaceNode : old == NULL\n");
1893 return(NULL);
1894 }
1895 if (cur == NULL) {
1896 xmlUnlinkNode(old);
1897 return(old);
1898 }
1899 xmlUnlinkNode(cur);
1900 cur->doc = old->doc;
1901 cur->parent = old->parent;
1902 cur->next = old->next;
1903 if (cur->next != NULL)
1904 cur->next->prev = cur;
1905 cur->prev = old->prev;
1906 if (cur->prev != NULL)
1907 cur->prev->next = cur;
1908 if (cur->parent != NULL) {
1909 if (cur->parent->childs == old)
1910 cur->parent->childs = cur;
1911 if (cur->parent->last == old)
1912 cur->parent->last = cur;
1913 }
1914 old->next = old->prev = NULL;
1915 old->parent = NULL;
1916 return(old);
1917}
1918
Daniel Veillard260a68f1998-08-13 03:39:55 +00001919/************************************************************************
1920 * *
Daniel Veillardbe36afe1998-11-27 06:39:50 +00001921 * Copy operations *
1922 * *
1923 ************************************************************************/
1924
1925/**
1926 * xmlCopyNamespace:
1927 * @cur: the namespace
1928 *
1929 * Do a copy of the namespace.
1930 *
1931 * Returns: a new xmlNsPtr, or NULL in case of error.
1932 */
1933xmlNsPtr
1934xmlCopyNamespace(xmlNsPtr cur) {
1935 xmlNsPtr ret;
1936
1937 if (cur == NULL) return(NULL);
1938 switch (cur->type) {
1939 case XML_GLOBAL_NAMESPACE:
1940 ret = xmlNewGlobalNs(NULL, cur->href, cur->prefix);
1941 break;
1942 case XML_LOCAL_NAMESPACE:
1943 ret = xmlNewNs(NULL, cur->href, cur->prefix);
1944 break;
1945 default:
1946 fprintf(stderr, "xmlCopyNamespace: unknown type %d\n", cur->type);
1947 return(NULL);
1948 }
1949 return(ret);
1950}
1951
1952/**
1953 * xmlCopyNamespaceList:
1954 * @cur: the first namespace
1955 *
1956 * Do a copy of an namespace list.
1957 *
1958 * Returns: a new xmlNsPtr, or NULL in case of error.
1959 */
1960xmlNsPtr
1961xmlCopyNamespaceList(xmlNsPtr cur) {
1962 xmlNsPtr ret = NULL;
1963 xmlNsPtr p = NULL,q;
1964
1965 while (cur != NULL) {
1966 q = xmlCopyNamespace(cur);
1967 if (p == NULL) {
1968 ret = p = q;
1969 } else {
1970 p->next = q;
1971 p = q;
1972 }
1973 cur = cur->next;
1974 }
1975 return(ret);
1976}
1977
1978/**
1979 * xmlCopyProp:
Daniel Veillardb96e6431999-08-29 21:02:19 +00001980 * @target: the element where the attribute will be grafted
Daniel Veillardbe36afe1998-11-27 06:39:50 +00001981 * @cur: the attribute
1982 *
1983 * Do a copy of the attribute.
1984 *
1985 * Returns: a new xmlAttrPtr, or NULL in case of error.
1986 */
1987xmlAttrPtr
Daniel Veillardb96e6431999-08-29 21:02:19 +00001988xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +00001989 xmlAttrPtr ret;
1990
1991 if (cur == NULL) return(NULL);
1992 if (cur->val != NULL)
1993 ret = xmlNewDocProp(cur->val->doc, cur->name, NULL);
1994 else
1995 ret = xmlNewDocProp(NULL, cur->name, NULL);
1996 if (ret == NULL) return(NULL);
Daniel Veillardb96e6431999-08-29 21:02:19 +00001997
1998 if ((cur->ns != NULL) && (target != NULL)) {
1999 xmlNsPtr ns;
2000
2001 ns = xmlSearchNs(target->doc, target, cur->ns->prefix);
2002 ret->ns = ns;
2003 } else
2004 ret->ns = NULL;
2005
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002006 if (cur->val != NULL)
2007 ret->val = xmlCopyNodeList(cur->val);
2008 return(ret);
2009}
2010
2011/**
2012 * xmlCopyPropList:
Daniel Veillardb96e6431999-08-29 21:02:19 +00002013 * @target: the element where the attributes will be grafted
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002014 * @cur: the first attribute
2015 *
2016 * Do a copy of an attribute list.
2017 *
2018 * Returns: a new xmlAttrPtr, or NULL in case of error.
2019 */
2020xmlAttrPtr
Daniel Veillardb96e6431999-08-29 21:02:19 +00002021xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002022 xmlAttrPtr ret = NULL;
2023 xmlAttrPtr p = NULL,q;
2024
2025 while (cur != NULL) {
Daniel Veillardb96e6431999-08-29 21:02:19 +00002026 q = xmlCopyProp(target, cur);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002027 if (p == NULL) {
2028 ret = p = q;
2029 } else {
2030 p->next = q;
2031 p = q;
2032 }
2033 cur = cur->next;
2034 }
2035 return(ret);
2036}
2037
2038/*
Daniel Veillard11a48ec1999-11-23 10:40:46 +00002039 * NOTE abeut the CopyNode operations !
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002040 *
2041 * They are splitted into external and internal parts for one
2042 * tricky reason: namespaces. Doing a direct copy of a node
2043 * say RPM:Copyright without changing the namespace pointer to
2044 * something else can produce stale links. One way to do it is
2045 * to keep a reference counter but this doesn't work as soon
2046 * as one move the element or the subtree out of the scope of
2047 * the existing namespace. The actual solution seems to add
2048 * a copy of the namespace at the top of the copied tree if
2049 * not available in the subtree.
2050 * Hence two functions, the public front-end call the inner ones
2051 */
2052
2053static xmlNodePtr
2054xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent);
2055
2056static xmlNodePtr
2057xmlStaticCopyNode(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent,
2058 int recursive) {
2059 xmlNodePtr ret;
2060
2061 if (node == NULL) return(NULL);
2062 /*
2063 * Allocate a new node and fill the fields.
2064 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00002065 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002066 if (ret == NULL) {
2067 fprintf(stderr, "xmlStaticCopyNode : malloc failed\n");
2068 return(NULL);
2069 }
2070
2071 ret->type = node->type;
2072 ret->doc = doc;
2073 ret->parent = parent;
2074 ret->next = NULL;
2075 ret->prev = NULL;
2076 ret->childs = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002077 ret->last = NULL;
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002078 ret->properties = NULL;
2079 if (node->name != NULL)
2080 ret->name = xmlStrdup(node->name);
2081 else
2082 ret->name = NULL;
2083 ret->ns = NULL;
2084 ret->nsDef = NULL;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002085 if ((node->content != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
2086#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002087 ret->content = xmlStrdup(node->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002088#else
2089 ret->content = xmlBufferCreateSize(xmlBufferLength(node->content));
2090 xmlBufferSetAllocationScheme(ret->content,
2091 xmlGetBufferAllocationScheme());
2092 xmlBufferAdd(ret->content,
2093 xmlBufferContent(node->content),
2094 xmlBufferLength(node->content));
2095#endif
2096 } else
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002097 ret->content = NULL;
Daniel Veillard27d88741999-05-29 11:51:49 +00002098#ifndef XML_WITHOUT_CORBA
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002099 ret->_private = NULL;
2100 ret->vepv = NULL;
2101#endif
2102 if (parent != NULL)
2103 xmlAddChild(parent, ret);
2104
2105 if (!recursive) return(ret);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002106 if (node->nsDef != NULL)
2107 ret->nsDef = xmlCopyNamespaceList(node->nsDef);
2108
2109 if (node->ns != NULL) {
2110 xmlNsPtr ns;
2111
2112 ns = xmlSearchNs(doc, ret, node->ns->prefix);
2113 if (ns == NULL) {
2114 /*
2115 * Humm, we are copying an element whose namespace is defined
2116 * out of the new tree scope. Search it in the original tree
2117 * and add it at the top of the new tree
2118 */
2119 ns = xmlSearchNs(node->doc, node, node->ns->prefix);
2120 if (ns != NULL) {
2121 xmlNodePtr root = ret;
2122
2123 while (root->parent != NULL) root = root->parent;
2124 xmlNewNs(root, ns->href, ns->prefix);
2125 }
2126 } else {
2127 /*
2128 * reference the existing namespace definition in our own tree.
2129 */
2130 ret->ns = ns;
2131 }
2132 }
Daniel Veillardb96e6431999-08-29 21:02:19 +00002133 if (node->properties != NULL)
2134 ret->properties = xmlCopyPropList(ret, node->properties);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002135 if (node->childs != NULL)
2136 ret->childs = xmlStaticCopyNodeList(node->childs, doc, ret);
Daniel Veillard1e346af1999-02-22 10:33:01 +00002137 UPDATE_LAST_CHILD(ret)
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002138 return(ret);
2139}
2140
2141static xmlNodePtr
2142xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) {
2143 xmlNodePtr ret = NULL;
2144 xmlNodePtr p = NULL,q;
2145
2146 while (node != NULL) {
2147 q = xmlStaticCopyNode(node, doc, parent, 1);
2148 if (parent == NULL) {
2149 if (ret == NULL) ret = q;
2150 } else {
2151 if (ret == NULL) {
2152 q->prev = NULL;
2153 ret = p = q;
2154 } else {
2155 p->next = q;
2156 q->prev = p;
2157 p = q;
2158 }
2159 }
2160 node = node->next;
2161 }
2162 return(ret);
2163}
2164
2165/**
2166 * xmlCopyNode:
2167 * @node: the node
2168 * @recursive: if 1 do a recursive copy.
2169 *
2170 * Do a copy of the node.
2171 *
2172 * Returns: a new xmlNodePtr, or NULL in case of error.
2173 */
2174xmlNodePtr
2175xmlCopyNode(xmlNodePtr node, int recursive) {
2176 xmlNodePtr ret;
2177
2178 ret = xmlStaticCopyNode(node, NULL, NULL, recursive);
2179 return(ret);
2180}
2181
2182/**
2183 * xmlCopyNodeList:
2184 * @node: the first node in the list.
2185 *
2186 * Do a recursive copy of the node list.
2187 *
2188 * Returns: a new xmlNodePtr, or NULL in case of error.
2189 */
2190xmlNodePtr xmlCopyNodeList(xmlNodePtr node) {
2191 xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL);
2192 return(ret);
2193}
2194
2195/**
2196 * xmlCopyElement:
2197 * @elem: the element
2198 *
2199 * Do a copy of the element definition.
2200 *
2201 * Returns: a new xmlElementPtr, or NULL in case of error.
2202xmlElementPtr
2203xmlCopyElement(xmlElementPtr elem) {
2204 xmlElementPtr ret;
2205
2206 if (elem == NULL) return(NULL);
2207 ret = xmlNewDocElement(elem->doc, elem->ns, elem->name, elem->content);
2208 if (ret == NULL) return(NULL);
2209 if (!recursive) return(ret);
2210 if (elem->properties != NULL)
2211 ret->properties = xmlCopyPropList(elem->properties);
2212
2213 if (elem->nsDef != NULL)
2214 ret->nsDef = xmlCopyNamespaceList(elem->nsDef);
2215 if (elem->childs != NULL)
2216 ret->childs = xmlCopyElementList(elem->childs);
2217 return(ret);
2218}
2219 */
2220
2221/**
2222 * xmlCopyDtd:
2223 * @dtd: the dtd
2224 *
2225 * Do a copy of the dtd.
2226 *
2227 * Returns: a new xmlDtdPtr, or NULL in case of error.
2228 */
2229xmlDtdPtr
2230xmlCopyDtd(xmlDtdPtr dtd) {
2231 xmlDtdPtr ret;
2232
2233 if (dtd == NULL) return(NULL);
2234 ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID);
2235 if (ret == NULL) return(NULL);
2236 if (dtd->entities != NULL)
2237 ret->entities = (void *) xmlCopyEntitiesTable(
2238 (xmlEntitiesTablePtr) dtd->entities);
Daniel Veillard1e346af1999-02-22 10:33:01 +00002239 if (dtd->notations != NULL)
2240 ret->notations = (void *) xmlCopyNotationTable(
2241 (xmlNotationTablePtr) dtd->notations);
2242 if (dtd->elements != NULL)
2243 ret->elements = (void *) xmlCopyElementTable(
2244 (xmlElementTablePtr) dtd->elements);
2245 if (dtd->attributes != NULL)
2246 ret->attributes = (void *) xmlCopyAttributeTable(
2247 (xmlAttributeTablePtr) dtd->attributes);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002248 return(ret);
2249}
2250
2251/**
2252 * xmlCopyDoc:
2253 * @doc: the document
2254 * @recursive: if 1 do a recursive copy.
2255 *
2256 * Do a copy of the document info. If recursive, the content tree will
2257 * be copied too as well as Dtd, namespaces and entities.
2258 *
2259 * Returns: a new xmlDocPtr, or NULL in case of error.
2260 */
2261xmlDocPtr
2262xmlCopyDoc(xmlDocPtr doc, int recursive) {
2263 xmlDocPtr ret;
2264
2265 if (doc == NULL) return(NULL);
2266 ret = xmlNewDoc(doc->version);
2267 if (ret == NULL) return(NULL);
2268 if (doc->name != NULL)
Daniel Veillard6454aec1999-09-02 22:04:43 +00002269 ret->name = xmlMemStrdup(doc->name);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002270 if (doc->encoding != NULL)
2271 ret->encoding = xmlStrdup(doc->encoding);
2272 ret->compression = doc->compression;
2273 ret->standalone = doc->standalone;
2274 if (!recursive) return(ret);
2275
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002276 if (doc->intSubset != NULL)
2277 ret->intSubset = xmlCopyDtd(doc->intSubset);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002278 if (doc->oldNs != NULL)
2279 ret->oldNs = xmlCopyNamespaceList(doc->oldNs);
2280 if (doc->root != NULL)
2281 ret->root = xmlStaticCopyNodeList(doc->root, ret, NULL);
2282 return(ret);
2283}
2284
2285/************************************************************************
2286 * *
Daniel Veillard260a68f1998-08-13 03:39:55 +00002287 * Content access functions *
2288 * *
2289 ************************************************************************/
2290
Daniel Veillard97b58771998-10-20 06:14:16 +00002291/**
Daniel Veillard944b5ff1999-12-15 19:08:24 +00002292 * xmlDocGetRootElement:
2293 * @doc: the document
2294 *
2295 * Get the root element of the document (doc->root is a list
2296 * containing possibly comments, PIs, etc ...).
2297 *
2298 * Returns the xmlNodePtr for the root or NULL
2299 */
2300xmlNodePtr
2301xmlDocGetRootElement(xmlDocPtr doc) {
2302 xmlNodePtr ret;
2303
2304 if (doc == NULL) return(NULL);
2305 ret = doc->root;
2306 while (ret != NULL) {
2307 if (ret->type == XML_ELEMENT_NODE)
2308 return(ret);
2309 ret = ret->next;
2310 }
2311 return(ret);
2312}
2313
2314/**
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002315 * xmlDocSetRootElement:
2316 * @doc: the document
2317 * @root: the new document root element
2318 *
2319 * Set the root element of the document (doc->root is a list
2320 * containing possibly comments, PIs, etc ...).
2321 *
2322 * Returns the old root element if any was found
2323 */
2324xmlNodePtr
2325xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) {
2326 xmlNodePtr old = NULL;
2327
2328 if (doc == NULL) return(NULL);
2329 old = doc->root;
2330 while (old != NULL) {
2331 if (old->type == XML_ELEMENT_NODE)
2332 break;
2333 old = old->next;
2334 }
2335 if (old == NULL) {
2336 if (doc->root == NULL) {
2337 doc->root = root;
2338 } else {
2339 xmlAddSibling(doc->root, root);
2340 }
2341 } else {
2342 xmlReplaceNode(old, root);
2343 }
2344 return(old);
2345}
2346
2347/**
Daniel Veillardb96e6431999-08-29 21:02:19 +00002348 * xmlNodeSetLang:
2349 * @cur: the node being changed
2350 * @lang: the langage description
2351 *
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002352 * Set the language of a node, i.e. the values of the xml:lang
2353 * attribute.
Daniel Veillardb96e6431999-08-29 21:02:19 +00002354 */
2355void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002356xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) {
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002357 if (cur == NULL) return;
2358 switch(cur->type) {
2359 case XML_TEXT_NODE:
2360 case XML_CDATA_SECTION_NODE:
2361 case XML_COMMENT_NODE:
2362 case XML_DOCUMENT_NODE:
2363 case XML_DOCUMENT_TYPE_NODE:
2364 case XML_DOCUMENT_FRAG_NODE:
2365 case XML_NOTATION_NODE:
2366 case XML_HTML_DOCUMENT_NODE:
2367 return;
2368 case XML_ELEMENT_NODE:
2369 case XML_ATTRIBUTE_NODE:
2370 case XML_PI_NODE:
2371 case XML_ENTITY_REF_NODE:
2372 case XML_ENTITY_NODE:
2373 break;
2374 }
Daniel Veillardb96e6431999-08-29 21:02:19 +00002375 xmlSetProp(cur, BAD_CAST "xml:lang", lang);
2376}
2377
2378/**
2379 * xmlNodeGetLang:
2380 * @cur: the node being checked
2381 *
2382 * Searches the language of a node, i.e. the values of the xml:lang
2383 * attribute or the one carried by the nearest ancestor.
2384 *
2385 * Returns a pointer to the lang value, or NULL if not found
Daniel Veillarda819dac1999-11-24 18:04:22 +00002386 * It's up to the caller to free the memory.
Daniel Veillardb96e6431999-08-29 21:02:19 +00002387 */
Daniel Veillarda819dac1999-11-24 18:04:22 +00002388xmlChar *
Daniel Veillardb96e6431999-08-29 21:02:19 +00002389xmlNodeGetLang(xmlNodePtr cur) {
Daniel Veillarda819dac1999-11-24 18:04:22 +00002390 xmlChar *lang;
Daniel Veillardb96e6431999-08-29 21:02:19 +00002391
2392 while (cur != NULL) {
2393 lang = xmlGetProp(cur, BAD_CAST "xml:lang");
2394 if (lang != NULL)
2395 return(lang);
2396 cur = cur->parent;
2397 }
2398 return(NULL);
2399}
2400
2401/**
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002402 * xmlNodeSetName:
2403 * @cur: the node being changed
2404 * @name: the new tag name
2405 *
2406 * Searches the language of a node, i.e. the values of the xml:lang
2407 * attribute or the one carried by the nearest ancestor.
2408 */
2409void
2410xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) {
2411 if (cur == NULL) return;
2412 if (name == NULL) return;
2413 switch(cur->type) {
2414 case XML_TEXT_NODE:
2415 case XML_CDATA_SECTION_NODE:
2416 case XML_COMMENT_NODE:
2417 case XML_DOCUMENT_NODE:
2418 case XML_DOCUMENT_TYPE_NODE:
2419 case XML_DOCUMENT_FRAG_NODE:
2420 case XML_NOTATION_NODE:
2421 case XML_HTML_DOCUMENT_NODE:
2422 return;
2423 case XML_ELEMENT_NODE:
2424 case XML_ATTRIBUTE_NODE:
2425 case XML_PI_NODE:
2426 case XML_ENTITY_REF_NODE:
2427 case XML_ENTITY_NODE:
2428 break;
2429 }
2430 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
2431 cur->name = xmlStrdup(name);
2432}
2433
2434/**
Daniel Veillard10a2c651999-12-12 13:03:50 +00002435 * xmlNodeGetBase:
2436 * @doc: the document the node pertains to
2437 * @cur: the node being checked
2438 *
2439 * Searches for the BASE URL. The code should work on both XML
2440 * and HTML document even if base mechanisms are completely different.
2441 *
2442 * Returns a pointer to the base URL, or NULL if not found
2443 * It's up to the caller to free the memory.
2444 */
2445xmlChar *
2446xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) {
2447 xmlChar *base;
2448
2449 if ((cur == NULL) && (doc == NULL))
2450 return(NULL);
2451 if (doc == NULL) doc = cur->doc;
2452 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
2453 cur = doc->root;
2454 while ((cur != NULL) && (cur->name != NULL)) {
2455 if (cur->type != XML_ELEMENT_NODE) {
2456 cur = cur->next;
2457 continue;
2458 }
2459 if ((!xmlStrcmp(cur->name, BAD_CAST "html")) ||
2460 (!xmlStrcmp(cur->name, BAD_CAST "HTML"))) {
2461 cur = cur->childs;
2462 continue;
2463 }
2464 if ((!xmlStrcmp(cur->name, BAD_CAST "head")) ||
2465 (!xmlStrcmp(cur->name, BAD_CAST "HEAD"))) {
2466 cur = cur->childs;
2467 continue;
2468 }
2469 if ((!xmlStrcmp(cur->name, BAD_CAST "base")) ||
2470 (!xmlStrcmp(cur->name, BAD_CAST "BASE"))) {
2471 base = xmlGetProp(cur, BAD_CAST "href");
2472 if (base != NULL) return(base);
2473 return(xmlGetProp(cur, BAD_CAST "HREF"));
2474 }
2475 }
2476 return(NULL);
2477 }
2478 while (cur != NULL) {
2479 base = xmlGetProp(cur, BAD_CAST "xml:base");
2480 if (base != NULL)
2481 return(base);
2482 cur = cur->parent;
2483 }
2484 return(NULL);
2485}
2486
2487/**
Daniel Veillard16253641998-10-28 22:58:05 +00002488 * xmlNodeGetContent:
2489 * @cur: the node being read
2490 *
2491 * Read the value of a node, this can be either the text carried
2492 * directly by this node if it's a TEXT node or the aggregate string
2493 * of the values carried by this node child's (TEXT and ENTITY_REF).
2494 * Entity references are substitued.
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002495 * Returns a new xmlChar * or NULL if no content is available.
Daniel Veillard5099ae81999-04-21 20:12:07 +00002496 * It's up to the caller to free the memory.
Daniel Veillard16253641998-10-28 22:58:05 +00002497 */
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002498xmlChar *
Daniel Veillard16253641998-10-28 22:58:05 +00002499xmlNodeGetContent(xmlNodePtr cur) {
2500 if (cur == NULL) return(NULL);
2501 switch (cur->type) {
2502 case XML_DOCUMENT_FRAG_NODE:
2503 case XML_ELEMENT_NODE:
2504 return(xmlNodeListGetString(cur->doc, cur->childs, 1));
2505 break;
Daniel Veillardb96e6431999-08-29 21:02:19 +00002506 case XML_ATTRIBUTE_NODE: {
2507 xmlAttrPtr attr = (xmlAttrPtr) cur;
2508 if (attr->node != NULL)
2509 return(xmlNodeListGetString(attr->node->doc, attr->val, 1));
2510 else
2511 return(xmlNodeListGetString(NULL, attr->val, 1));
2512 break;
2513 }
Daniel Veillarddbfd6411999-12-28 16:35:14 +00002514 case XML_COMMENT_NODE:
Daniel Veillardb96e6431999-08-29 21:02:19 +00002515 case XML_PI_NODE:
2516 if (cur->content != NULL)
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002517#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardb96e6431999-08-29 21:02:19 +00002518 return(xmlStrdup(cur->content));
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002519#else
2520 return(xmlStrdup(xmlBufferContent(cur->content)));
2521#endif
Daniel Veillardb96e6431999-08-29 21:02:19 +00002522 return(NULL);
Daniel Veillard16253641998-10-28 22:58:05 +00002523 case XML_ENTITY_REF_NODE:
Daniel Veillarddbfd6411999-12-28 16:35:14 +00002524 /*
2525 * Locate the entity, and get it's content
2526 * @@@
2527 */
2528 return(NULL);
Daniel Veillard16253641998-10-28 22:58:05 +00002529 case XML_ENTITY_NODE:
Daniel Veillard16253641998-10-28 22:58:05 +00002530 case XML_DOCUMENT_NODE:
Daniel Veillard7c1206f1999-10-14 09:10:25 +00002531 case XML_HTML_DOCUMENT_NODE:
Daniel Veillard16253641998-10-28 22:58:05 +00002532 case XML_DOCUMENT_TYPE_NODE:
2533 case XML_NOTATION_NODE:
2534 return(NULL);
Daniel Veillardb05deb71999-08-10 19:04:08 +00002535 case XML_CDATA_SECTION_NODE:
Daniel Veillard16253641998-10-28 22:58:05 +00002536 case XML_TEXT_NODE:
2537 if (cur->content != NULL)
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002538#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard16253641998-10-28 22:58:05 +00002539 return(xmlStrdup(cur->content));
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002540#else
2541 return(xmlStrdup(xmlBufferContent(cur->content)));
2542#endif
Daniel Veillard16253641998-10-28 22:58:05 +00002543 return(NULL);
2544 }
2545 return(NULL);
2546}
2547
2548/**
Daniel Veillard97b58771998-10-20 06:14:16 +00002549 * xmlNodeSetContent:
2550 * @cur: the node being modified
2551 * @content: the new value of the content
2552 *
2553 * Replace the content of a node.
Daniel Veillard260a68f1998-08-13 03:39:55 +00002554 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00002555void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002556xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00002557 if (cur == NULL) {
2558 fprintf(stderr, "xmlNodeSetContent : node == NULL\n");
2559 return;
2560 }
Daniel Veillard16253641998-10-28 22:58:05 +00002561 switch (cur->type) {
2562 case XML_DOCUMENT_FRAG_NODE:
2563 case XML_ELEMENT_NODE:
2564 if (cur->content != NULL) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002565#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard6454aec1999-09-02 22:04:43 +00002566 xmlFree(cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002567#else
2568 xmlBufferFree(cur->content);
2569#endif
Daniel Veillard16253641998-10-28 22:58:05 +00002570 cur->content = NULL;
2571 }
Daniel Veillard6454aec1999-09-02 22:04:43 +00002572 if (cur->childs != NULL) xmlFreeNodeList(cur->childs);
Daniel Veillard16253641998-10-28 22:58:05 +00002573 cur->childs = xmlStringGetNodeList(cur->doc, content);
Daniel Veillard1e346af1999-02-22 10:33:01 +00002574 UPDATE_LAST_CHILD(cur)
Daniel Veillard16253641998-10-28 22:58:05 +00002575 break;
2576 case XML_ATTRIBUTE_NODE:
2577 break;
2578 case XML_TEXT_NODE:
2579 case XML_CDATA_SECTION_NODE:
2580 case XML_ENTITY_REF_NODE:
2581 case XML_ENTITY_NODE:
2582 case XML_PI_NODE:
2583 case XML_COMMENT_NODE:
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002584 if (cur->content != NULL) {
2585#ifndef XML_USE_BUFFER_CONTENT
2586 xmlFree(cur->content);
2587#else
2588 xmlBufferFree(cur->content);
2589#endif
2590 }
Daniel Veillard6454aec1999-09-02 22:04:43 +00002591 if (cur->childs != NULL) xmlFreeNodeList(cur->childs);
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002592 cur->last = cur->childs = NULL;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002593 if (content != NULL) {
2594#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard16253641998-10-28 22:58:05 +00002595 cur->content = xmlStrdup(content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002596#else
2597 cur->content = xmlBufferCreateSize(0);
2598 xmlBufferSetAllocationScheme(cur->content,
2599 xmlGetBufferAllocationScheme());
2600 xmlBufferAdd(cur->content, content, -1);
2601#endif
2602 } else
Daniel Veillard16253641998-10-28 22:58:05 +00002603 cur->content = NULL;
Daniel Veillardb96e6431999-08-29 21:02:19 +00002604 break;
Daniel Veillard16253641998-10-28 22:58:05 +00002605 case XML_DOCUMENT_NODE:
Daniel Veillard7c1206f1999-10-14 09:10:25 +00002606 case XML_HTML_DOCUMENT_NODE:
Daniel Veillard16253641998-10-28 22:58:05 +00002607 case XML_DOCUMENT_TYPE_NODE:
2608 break;
2609 case XML_NOTATION_NODE:
2610 break;
2611 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00002612}
2613
Daniel Veillard97b58771998-10-20 06:14:16 +00002614/**
2615 * xmlNodeSetContentLen:
2616 * @cur: the node being modified
2617 * @content: the new value of the content
2618 * @len: the size of @content
2619 *
2620 * Replace the content of a node.
Daniel Veillard260a68f1998-08-13 03:39:55 +00002621 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00002622void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002623xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00002624 if (cur == NULL) {
Daniel Veillard16253641998-10-28 22:58:05 +00002625 fprintf(stderr, "xmlNodeSetContentLen : node == NULL\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +00002626 return;
2627 }
Daniel Veillard16253641998-10-28 22:58:05 +00002628 switch (cur->type) {
2629 case XML_DOCUMENT_FRAG_NODE:
2630 case XML_ELEMENT_NODE:
2631 if (cur->content != NULL) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002632#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard6454aec1999-09-02 22:04:43 +00002633 xmlFree(cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002634#else
2635 xmlBufferFree(cur->content);
2636#endif
Daniel Veillard16253641998-10-28 22:58:05 +00002637 cur->content = NULL;
2638 }
Daniel Veillard6454aec1999-09-02 22:04:43 +00002639 if (cur->childs != NULL) xmlFreeNodeList(cur->childs);
Daniel Veillard16253641998-10-28 22:58:05 +00002640 cur->childs = xmlStringLenGetNodeList(cur->doc, content, len);
Daniel Veillard1e346af1999-02-22 10:33:01 +00002641 UPDATE_LAST_CHILD(cur)
Daniel Veillard16253641998-10-28 22:58:05 +00002642 break;
2643 case XML_ATTRIBUTE_NODE:
2644 break;
2645 case XML_TEXT_NODE:
2646 case XML_CDATA_SECTION_NODE:
2647 case XML_ENTITY_REF_NODE:
2648 case XML_ENTITY_NODE:
2649 case XML_PI_NODE:
2650 case XML_COMMENT_NODE:
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002651 case XML_NOTATION_NODE:
2652 if (cur->content != NULL) {
2653#ifndef XML_USE_BUFFER_CONTENT
2654 xmlFree(cur->content);
2655#else
2656 xmlBufferFree(cur->content);
2657#endif
2658 }
Daniel Veillard6454aec1999-09-02 22:04:43 +00002659 if (cur->childs != NULL) xmlFreeNodeList(cur->childs);
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002660 cur->childs = cur->last = NULL;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002661 if (content != NULL) {
2662#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard16253641998-10-28 22:58:05 +00002663 cur->content = xmlStrndup(content, len);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002664#else
2665 cur->content = xmlBufferCreateSize(len);
2666 xmlBufferSetAllocationScheme(cur->content,
2667 xmlGetBufferAllocationScheme());
2668 xmlBufferAdd(cur->content, content, len);
2669#endif
2670 } else
Daniel Veillard16253641998-10-28 22:58:05 +00002671 cur->content = NULL;
Daniel Veillardb96e6431999-08-29 21:02:19 +00002672 break;
Daniel Veillard16253641998-10-28 22:58:05 +00002673 case XML_DOCUMENT_NODE:
Daniel Veillard7c1206f1999-10-14 09:10:25 +00002674 case XML_HTML_DOCUMENT_NODE:
Daniel Veillard16253641998-10-28 22:58:05 +00002675 case XML_DOCUMENT_TYPE_NODE:
2676 break;
Daniel Veillard260a68f1998-08-13 03:39:55 +00002677 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00002678}
2679
Daniel Veillard97b58771998-10-20 06:14:16 +00002680/**
2681 * xmlNodeAddContentLen:
2682 * @cur: the node being modified
2683 * @content: extra content
2684 * @len: the size of @content
2685 *
2686 * Append the extra substring to the node content.
Daniel Veillard260a68f1998-08-13 03:39:55 +00002687 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00002688void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002689xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00002690 if (cur == NULL) {
Daniel Veillard16253641998-10-28 22:58:05 +00002691 fprintf(stderr, "xmlNodeAddContentLen : node == NULL\n");
2692 return;
2693 }
2694 if (len <= 0) return;
2695 switch (cur->type) {
2696 case XML_DOCUMENT_FRAG_NODE:
2697 case XML_ELEMENT_NODE: {
2698 xmlNodePtr last = NULL, new;
2699
2700 if (cur->childs != NULL) {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002701 last = cur->last;
Daniel Veillard16253641998-10-28 22:58:05 +00002702 } else {
2703 if (cur->content != NULL) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002704#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard16253641998-10-28 22:58:05 +00002705 cur->childs = xmlStringGetNodeList(cur->doc, cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002706#else
2707 cur->childs = xmlStringGetNodeList(cur->doc,
2708 xmlBufferContent(cur->content));
2709#endif
Daniel Veillard1e346af1999-02-22 10:33:01 +00002710 UPDATE_LAST_CHILD(cur)
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002711#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard6454aec1999-09-02 22:04:43 +00002712 xmlFree(cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002713#else
2714 xmlBufferFree(cur->content);
2715#endif
Daniel Veillard16253641998-10-28 22:58:05 +00002716 cur->content = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002717 last = cur->last;
Daniel Veillard16253641998-10-28 22:58:05 +00002718 }
2719 }
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002720 new = xmlNewTextLen(content, len);
Daniel Veillard16253641998-10-28 22:58:05 +00002721 if (new != NULL) {
2722 xmlAddChild(cur, new);
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002723 if ((last != NULL) && (last->next == new)) {
Daniel Veillard16253641998-10-28 22:58:05 +00002724 xmlTextMerge(last, new);
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002725 }
Daniel Veillard16253641998-10-28 22:58:05 +00002726 }
2727 break;
2728 }
2729 case XML_ATTRIBUTE_NODE:
2730 break;
2731 case XML_TEXT_NODE:
2732 case XML_CDATA_SECTION_NODE:
2733 case XML_ENTITY_REF_NODE:
2734 case XML_ENTITY_NODE:
2735 case XML_PI_NODE:
2736 case XML_COMMENT_NODE:
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002737 case XML_NOTATION_NODE:
2738 if (content != NULL) {
2739#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard16253641998-10-28 22:58:05 +00002740 cur->content = xmlStrncat(cur->content, content, len);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002741#else
2742 xmlBufferAdd(cur->content, content, len);
2743#endif
2744 }
Daniel Veillard16253641998-10-28 22:58:05 +00002745 case XML_DOCUMENT_NODE:
Daniel Veillard7c1206f1999-10-14 09:10:25 +00002746 case XML_HTML_DOCUMENT_NODE:
Daniel Veillard16253641998-10-28 22:58:05 +00002747 case XML_DOCUMENT_TYPE_NODE:
2748 break;
Daniel Veillard16253641998-10-28 22:58:05 +00002749 }
2750}
2751
2752/**
2753 * xmlNodeAddContent:
2754 * @cur: the node being modified
2755 * @content: extra content
2756 *
2757 * Append the extra substring to the node content.
2758 */
2759void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002760xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) {
Daniel Veillard16253641998-10-28 22:58:05 +00002761 int len;
2762
2763 if (cur == NULL) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00002764 fprintf(stderr, "xmlNodeAddContent : node == NULL\n");
2765 return;
2766 }
Daniel Veillard16253641998-10-28 22:58:05 +00002767 if (content == NULL) return;
2768 len = xmlStrlen(content);
2769 xmlNodeAddContentLen(cur, content, len);
2770}
2771
2772/**
2773 * xmlTextMerge:
2774 * @first: the first text node
2775 * @second: the second text node being merged
2776 *
2777 * Merge two text nodes into one
Daniel Veillard1e346af1999-02-22 10:33:01 +00002778 * Returns the first text node augmented
Daniel Veillard16253641998-10-28 22:58:05 +00002779 */
2780xmlNodePtr
2781xmlTextMerge(xmlNodePtr first, xmlNodePtr second) {
2782 if (first == NULL) return(second);
2783 if (second == NULL) return(first);
2784 if (first->type != XML_TEXT_NODE) return(first);
2785 if (second->type != XML_TEXT_NODE) return(first);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002786#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard16253641998-10-28 22:58:05 +00002787 xmlNodeAddContent(first, second->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002788#else
2789 xmlNodeAddContent(first, xmlBufferContent(second->content));
2790#endif
Daniel Veillard16253641998-10-28 22:58:05 +00002791 xmlUnlinkNode(second);
2792 xmlFreeNode(second);
2793 return(first);
Daniel Veillard260a68f1998-08-13 03:39:55 +00002794}
2795
Daniel Veillard97b58771998-10-20 06:14:16 +00002796/**
Daniel Veillardb96e6431999-08-29 21:02:19 +00002797 * xmlGetNsList:
2798 * @doc: the document
2799 * @node: the current node
2800 *
2801 * Search all the namespace applying to a given element.
2802 * Returns an NULL terminated array of all the xmlNsPtr found
2803 * that need to be freed by the caller or NULL if no
2804 * namespace if defined
2805 */
2806xmlNsPtr *
2807xmlGetNsList(xmlDocPtr doc, xmlNodePtr node) {
2808 xmlNsPtr cur;
2809 xmlNsPtr *ret = NULL;
2810 int nbns = 0;
2811 int maxns = 10;
2812 int i;
2813
2814 while (node != NULL) {
2815 cur = node->nsDef;
2816 while (cur != NULL) {
2817 if (ret == NULL) {
Daniel Veillard6454aec1999-09-02 22:04:43 +00002818 ret = (xmlNsPtr *) xmlMalloc((maxns + 1) * sizeof(xmlNsPtr));
Daniel Veillardb96e6431999-08-29 21:02:19 +00002819 if (ret == NULL) {
2820 fprintf(stderr, "xmlGetNsList : out of memory!\n");
2821 return(NULL);
2822 }
2823 ret[nbns] = NULL;
2824 }
2825 for (i = 0;i < nbns;i++) {
2826 if ((cur->prefix == ret[i]->prefix) ||
2827 (!xmlStrcmp(cur->prefix, ret[i]->prefix))) break;
2828 }
2829 if (i >= nbns) {
2830 if (nbns >= maxns) {
2831 maxns *= 2;
Daniel Veillard6454aec1999-09-02 22:04:43 +00002832 ret = (xmlNsPtr *) xmlRealloc(ret,
Daniel Veillardb96e6431999-08-29 21:02:19 +00002833 (maxns + 1) * sizeof(xmlNsPtr));
2834 if (ret == NULL) {
2835 fprintf(stderr, "xmlGetNsList : realloc failed!\n");
2836 return(NULL);
2837 }
2838 }
2839 ret[nbns++] = cur;
2840 ret[nbns] = NULL;
2841 }
2842
2843 cur = cur->next;
2844 }
2845 node = node->parent;
2846 }
2847 return(ret);
2848}
2849
2850/**
Daniel Veillard97b58771998-10-20 06:14:16 +00002851 * xmlSearchNs:
2852 * @doc: the document
2853 * @node: the current node
2854 * @nameSpace: the namespace string
Daniel Veillard260a68f1998-08-13 03:39:55 +00002855 *
Daniel Veillard97b58771998-10-20 06:14:16 +00002856 * Search a Ns registered under a given name space for a document.
2857 * recurse on the parents until it finds the defined namespace
2858 * or return NULL otherwise.
2859 * @nameSpace can be NULL, this is a search for the default namespace.
Daniel Veillard1e346af1999-02-22 10:33:01 +00002860 * Returns the namespace pointer or NULL.
Daniel Veillard260a68f1998-08-13 03:39:55 +00002861 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00002862xmlNsPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002863xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00002864 xmlNsPtr cur;
2865
Daniel Veillard62ba71e1999-12-16 17:52:19 +00002866 if (node == NULL) return(NULL);
Daniel Veillard260a68f1998-08-13 03:39:55 +00002867 while (node != NULL) {
2868 cur = node->nsDef;
2869 while (cur != NULL) {
2870 if ((cur->prefix == NULL) && (nameSpace == NULL))
2871 return(cur);
2872 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
2873 (!xmlStrcmp(cur->prefix, nameSpace)))
2874 return(cur);
2875 cur = cur->next;
2876 }
2877 node = node->parent;
2878 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00002879 return(NULL);
2880}
2881
Daniel Veillard97b58771998-10-20 06:14:16 +00002882/**
2883 * xmlSearchNsByHref:
2884 * @doc: the document
2885 * @node: the current node
2886 * @href: the namespace value
2887 *
2888 * Search a Ns aliasing a given URI. Recurse on the parents until it finds
2889 * the defined namespace or return NULL otherwise.
Daniel Veillard1e346af1999-02-22 10:33:01 +00002890 * Returns the namespace pointer or NULL.
Daniel Veillard260a68f1998-08-13 03:39:55 +00002891 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00002892xmlNsPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002893xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar *href) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00002894 xmlNsPtr cur;
Daniel Veillarddbfd6411999-12-28 16:35:14 +00002895 xmlNodePtr orig = node;
Daniel Veillard260a68f1998-08-13 03:39:55 +00002896
Daniel Veillard10a2c651999-12-12 13:03:50 +00002897 if ((node == NULL) || (href == NULL)) return(NULL);
Daniel Veillard260a68f1998-08-13 03:39:55 +00002898 while (node != NULL) {
2899 cur = node->nsDef;
2900 while (cur != NULL) {
2901 if ((cur->href != NULL) && (href != NULL) &&
Daniel Veillarddbfd6411999-12-28 16:35:14 +00002902 (!xmlStrcmp(cur->href, href))) {
2903 /*
2904 * Check that the prefix is not shadowed between orig and node
2905 */
2906 xmlNodePtr check = orig;
2907 xmlNsPtr tst;
2908
2909 while (check != node) {
2910 tst = check->nsDef;
2911 while (tst != NULL) {
2912 if ((tst->prefix == NULL) && (cur->prefix == NULL))
2913 goto shadowed;
2914 if ((tst->prefix != NULL) && (cur->prefix != NULL) &&
2915 (!xmlStrcmp(tst->prefix, cur->prefix)))
2916 goto shadowed;
2917 tst = tst->next;
2918 }
2919 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00002920 return(cur);
Daniel Veillarddbfd6411999-12-28 16:35:14 +00002921 }
2922shadowed:
Daniel Veillard260a68f1998-08-13 03:39:55 +00002923 cur = cur->next;
2924 }
2925 node = node->parent;
2926 }
Daniel Veillarddbfd6411999-12-28 16:35:14 +00002927 return(NULL);
2928}
2929
2930/**
2931 * xmlNewReconciliedNs
2932 * @doc: the document
2933 * @tree: a node expected to hold the new namespace
2934 * @ns: the original namespace
2935 *
2936 * This function tries to locate a namespace definition in a tree
2937 * ancestors, or create a new namespace definition node similar to
2938 * @ns trying to reuse the same prefix. However if the given prefix is
2939 * null (default namespace) or reused within the subtree defined by
2940 * @tree or on one of its ancestors then a new prefix is generated.
2941 * Returns the (new) namespace definition or NULL in case of error
2942 */
2943xmlNsPtr
2944xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) {
2945 xmlNsPtr def;
2946 xmlChar prefix[50];
2947 int counter = 1;
2948
2949 if (tree == NULL) {
2950#ifdef DEBUG_TREE
2951 fprintf(stderr, "xmlNewReconciliedNs : tree == NULL\n");
2952#endif
2953 return(NULL);
2954 }
2955 if (ns == NULL) {
2956#ifdef DEBUG_TREE
2957 fprintf(stderr, "xmlNewReconciliedNs : ns == NULL\n");
2958#endif
2959 return(NULL);
2960 }
2961 /*
2962 * Search an existing namespace definition inherited.
2963 */
2964 def = xmlSearchNsByHref(doc, tree, ns->href);
2965 if (def != NULL)
2966 return(def);
2967
2968 /*
2969 * Find a close prefix which is not already in use.
2970 * Let's strip namespace prefixes longer than 20 chars !
2971 */
2972 sprintf((char *) prefix, "%.20s", ns->prefix);
2973 def = xmlSearchNs(doc, tree, prefix);
2974 while (def != NULL) {
2975 if (counter > 1000) return(NULL);
2976 sprintf((char *) prefix, "%.20s%d", ns->prefix, counter++);
2977 def = xmlSearchNs(doc, tree, prefix);
2978 }
2979
2980 /*
2981 * Ok, now we are ready to create a new one.
2982 */
2983 def = xmlNewNs(tree, ns->href, prefix);
2984 return(def);
2985}
2986
2987/**
2988 * xmlReconciliateNs
2989 * @doc: the document
2990 * @tree: a node defining the subtree to reconciliate
2991 *
2992 * This function checks that all the namespaces declared within the given
2993 * tree are properly declared. This is needed for example after Copy or Cut
2994 * and then paste operations. The subtree may still hold pointers to
2995 * namespace declarations outside the subtree or invalid/masked. As much
2996 * as possible the function try tu reuse the existing namespaces found in
2997 * the new environment. If not possible the new namespaces are redeclared
2998 * on @tree at the top of the given subtree.
2999 * Returns the number of namespace declarations created or -1 in case of error.
3000 */
3001int
3002xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) {
3003 xmlNsPtr *oldNs = NULL;
3004 xmlNsPtr *newNs = NULL;
3005 int sizeCache = 0;
3006 int nbCache = 0;
3007
3008 xmlNsPtr n;
3009 xmlNodePtr node = tree;
3010 xmlAttrPtr attr;
3011 int ret = 0, i;
3012
3013 while (node != NULL) {
3014 /*
3015 * Reconciliate the node namespace
3016 */
3017 if (node->ns != NULL) {
3018 /*
3019 * initialize the cache if needed
3020 */
3021 if (sizeCache == 0) {
3022 sizeCache = 10;
3023 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
3024 sizeof(xmlNsPtr));
3025 if (oldNs == NULL) {
3026 fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
3027 return(-1);
3028 }
3029 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
3030 sizeof(xmlNsPtr));
3031 if (newNs == NULL) {
3032 fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
3033 xmlFree(oldNs);
3034 return(-1);
3035 }
3036 }
3037 for (i = 0;i < nbCache;i++) {
3038 if (oldNs[i] == node->ns) {
3039 node->ns = newNs[i];
3040 break;
3041 }
3042 }
3043 if (i == nbCache) {
3044 /*
3045 * Ok we need to recreate a new namespace definition
3046 */
3047 n = xmlNewReconciliedNs(doc, tree, node->ns);
3048 if (n != NULL) { /* :-( what if else ??? */
3049 /*
3050 * check if we need to grow the cache buffers.
3051 */
3052 if (sizeCache <= nbCache) {
3053 sizeCache *= 2;
3054 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
3055 sizeof(xmlNsPtr));
3056 if (oldNs == NULL) {
3057 fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
3058 xmlFree(newNs);
3059 return(-1);
3060 }
3061 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
3062 sizeof(xmlNsPtr));
3063 if (newNs == NULL) {
3064 fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
3065 xmlFree(oldNs);
3066 return(-1);
3067 }
3068 }
3069 newNs[nbCache] = n;
3070 oldNs[nbCache++] = node->ns;
3071 node->ns = n;
3072 }
3073 }
3074 }
3075 /*
3076 * now check for namespace hold by attributes on the node.
3077 */
3078 attr = node->properties;
3079 while (attr != NULL) {
3080 if (attr->ns != NULL) {
3081 /*
3082 * initialize the cache if needed
3083 */
3084 if (sizeCache == 0) {
3085 sizeCache = 10;
3086 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
3087 sizeof(xmlNsPtr));
3088 if (oldNs == NULL) {
3089 fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
3090 return(-1);
3091 }
3092 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
3093 sizeof(xmlNsPtr));
3094 if (newNs == NULL) {
3095 fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
3096 xmlFree(oldNs);
3097 return(-1);
3098 }
3099 }
3100 for (i = 0;i < nbCache;i++) {
3101 if (oldNs[i] == attr->ns) {
3102 node->ns = newNs[i];
3103 break;
3104 }
3105 }
3106 if (i == nbCache) {
3107 /*
3108 * Ok we need to recreate a new namespace definition
3109 */
3110 n = xmlNewReconciliedNs(doc, tree, attr->ns);
3111 if (n != NULL) { /* :-( what if else ??? */
3112 /*
3113 * check if we need to grow the cache buffers.
3114 */
3115 if (sizeCache <= nbCache) {
3116 sizeCache *= 2;
3117 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
3118 sizeof(xmlNsPtr));
3119 if (oldNs == NULL) {
3120 fprintf(stderr,
3121 "xmlReconciliateNs : memory pbm\n");
3122 xmlFree(newNs);
3123 return(-1);
3124 }
3125 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
3126 sizeof(xmlNsPtr));
3127 if (newNs == NULL) {
3128 fprintf(stderr,
3129 "xmlReconciliateNs : memory pbm\n");
3130 xmlFree(oldNs);
3131 return(-1);
3132 }
3133 }
3134 newNs[nbCache] = n;
3135 oldNs[nbCache++] = attr->ns;
3136 attr->ns = n;
3137 }
3138 }
3139 }
3140 attr = attr->next;
3141 }
3142
3143 /*
3144 * Browse the full subtree, deep first
3145 */
3146 if (node->childs != NULL) {
3147 /* deep first */
3148 node = node->childs;
3149 } else if ((node != tree) && (node->next != NULL)) {
3150 /* then siblings */
3151 node = node->next;
3152 } else if (node != tree) {
3153 /* go up to parents->next if needed */
3154 while (node != tree) {
3155 if (node->parent != NULL)
3156 node = node->parent;
3157 if ((node != tree) && (node->next != NULL)) {
3158 node = node->next;
3159 break;
3160 }
3161 if (node->parent == NULL) {
3162 node = NULL;
3163 break;
3164 }
3165 }
3166 /* exit condition */
3167 if (node == tree)
3168 node = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00003169 }
3170 }
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003171 return(ret);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003172}
3173
Daniel Veillard97b58771998-10-20 06:14:16 +00003174/**
3175 * xmlGetProp:
3176 * @node: the node
3177 * @name: the attribute name
3178 *
3179 * Search and get the value of an attribute associated to a node
Daniel Veillardccb09631998-10-27 06:21:04 +00003180 * This does the entity substitution.
Daniel Veillard10a2c651999-12-12 13:03:50 +00003181 * This function looks in DTD attribute declaration for #FIXED or
3182 * default declaration values unless DTD use has been turned off.
3183 *
Daniel Veillard1e346af1999-02-22 10:33:01 +00003184 * Returns the attribute value or NULL if not found.
Daniel Veillarda819dac1999-11-24 18:04:22 +00003185 * It's up to the caller to free the memory.
Daniel Veillard260a68f1998-08-13 03:39:55 +00003186 */
Daniel Veillarda819dac1999-11-24 18:04:22 +00003187xmlChar *
3188xmlGetProp(xmlNodePtr node, const xmlChar *name) {
Daniel Veillard10a2c651999-12-12 13:03:50 +00003189 xmlAttrPtr prop;
3190 xmlDocPtr doc;
Daniel Veillard260a68f1998-08-13 03:39:55 +00003191
Daniel Veillard10a2c651999-12-12 13:03:50 +00003192 if ((node == NULL) || (name == NULL)) return(NULL);
3193 /*
3194 * Check on the properties attached to the node
3195 */
3196 prop = node->properties;
Daniel Veillard260a68f1998-08-13 03:39:55 +00003197 while (prop != NULL) {
Daniel Veillard68178931999-02-08 18:34:36 +00003198 if (!xmlStrcmp(prop->name, name)) {
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003199 xmlChar *ret;
Daniel Veillard6800ef31999-02-08 18:33:22 +00003200
3201 ret = xmlNodeListGetString(node->doc, prop->val, 1);
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003202 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
Daniel Veillard6800ef31999-02-08 18:33:22 +00003203 return(ret);
Daniel Veillard68178931999-02-08 18:34:36 +00003204 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00003205 prop = prop->next;
3206 }
Daniel Veillard10a2c651999-12-12 13:03:50 +00003207 if (!xmlCheckDTD) return(NULL);
3208
3209 /*
3210 * Check if there is a default declaration in the internal
3211 * or external subsets
3212 */
3213 doc = node->doc;
3214 if (doc != NULL) {
3215 xmlAttributePtr attrDecl;
3216 if (doc->intSubset != NULL) {
3217 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
3218 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3219 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
3220 return(xmlStrdup(attrDecl->defaultValue));
3221 }
3222 }
3223 return(NULL);
3224}
3225
3226/**
3227 * xmlGetNsProp:
3228 * @node: the node
3229 * @name: the attribute name
3230 * @namespace: the URI of the namespace
3231 *
3232 * Search and get the value of an attribute associated to a node
3233 * This attribute has to be anchored in the namespace specified.
3234 * This does the entity substitution.
3235 * This function looks in DTD attribute declaration for #FIXED or
3236 * default declaration values unless DTD use has been turned off.
3237 *
3238 * Returns the attribute value or NULL if not found.
3239 * It's up to the caller to free the memory.
3240 */
3241xmlChar *
3242xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *namespace) {
3243 xmlAttrPtr prop = node->properties;
3244 xmlDocPtr doc;
3245 xmlNsPtr ns;
3246
3247 if (namespace == NULL)
3248 return(xmlGetProp(node, name));
3249 while (prop != NULL) {
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00003250 /*
3251 * One need to have
3252 * - same attribute names
3253 * - and the attribute carrying that namespace
3254 * or
3255 * no namespace on the attribute and the element carrying it
3256 */
Daniel Veillard10a2c651999-12-12 13:03:50 +00003257 if ((!xmlStrcmp(prop->name, name)) &&
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00003258 (((prop->ns == NULL) && (node->ns != NULL) &&
3259 (!xmlStrcmp(node->ns->href, namespace))) ||
Daniel Veillard3c558c31999-12-22 11:30:41 +00003260 ((prop->ns != NULL) && (!xmlStrcmp(prop->ns->href, namespace))))) {
Daniel Veillard10a2c651999-12-12 13:03:50 +00003261 xmlChar *ret;
3262
3263 ret = xmlNodeListGetString(node->doc, prop->val, 1);
3264 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
3265 return(ret);
3266 }
3267 prop = prop->next;
3268 }
3269 if (!xmlCheckDTD) return(NULL);
3270
3271 /*
3272 * Check if there is a default declaration in the internal
3273 * or external subsets
3274 */
3275 doc = node->doc;
3276 if (doc != NULL) {
3277 xmlAttributePtr attrDecl;
3278 if (doc->intSubset != NULL) {
3279 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
3280 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3281 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
3282
3283 if (attrDecl->prefix != NULL) {
3284 /*
3285 * The DTD declaration only allows a prefix search
3286 */
3287 ns = xmlSearchNs(doc, node, attrDecl->prefix);
3288 if ((ns != NULL) && (!xmlStrcmp(ns->href, namespace)))
3289 return(xmlStrdup(attrDecl->defaultValue));
3290 }
3291 }
3292 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00003293 return(NULL);
3294}
3295
Daniel Veillard97b58771998-10-20 06:14:16 +00003296/**
Daniel Veillardccb09631998-10-27 06:21:04 +00003297 * xmlSetProp:
Daniel Veillard97b58771998-10-20 06:14:16 +00003298 * @node: the node
3299 * @name: the attribute name
3300 * @value: the attribute value
3301 *
3302 * Set (or reset) an attribute carried by a node.
Daniel Veillard1e346af1999-02-22 10:33:01 +00003303 * Returns the attribute pointer.
Daniel Veillard260a68f1998-08-13 03:39:55 +00003304 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003305xmlAttrPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003306xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003307 xmlAttrPtr prop = node->properties;
3308
3309 while (prop != NULL) {
3310 if (!xmlStrcmp(prop->name, name)) {
Daniel Veillardccb09631998-10-27 06:21:04 +00003311 if (prop->val != NULL)
Daniel Veillard6454aec1999-09-02 22:04:43 +00003312 xmlFreeNodeList(prop->val);
Daniel Veillardccb09631998-10-27 06:21:04 +00003313 prop->val = NULL;
Daniel Veillard51e3b151999-11-12 17:02:31 +00003314 if (value != NULL) {
3315 xmlChar *buffer;
3316 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
3317 prop->val = xmlStringGetNodeList(node->doc, buffer);
3318 xmlFree(buffer);
3319 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00003320 return(prop);
3321 }
3322 prop = prop->next;
3323 }
3324 prop = xmlNewProp(node, name, value);
3325 return(prop);
3326}
3327
Daniel Veillard97b58771998-10-20 06:14:16 +00003328/**
3329 * xmlNodeIsText:
3330 * @node: the node
3331 *
3332 * Is this node a Text node ?
Daniel Veillard1e346af1999-02-22 10:33:01 +00003333 * Returns 1 yes, 0 no
Daniel Veillard260a68f1998-08-13 03:39:55 +00003334 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003335int
3336xmlNodeIsText(xmlNodePtr node) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003337 if (node == NULL) return(0);
3338
Daniel Veillard0bef1311998-10-14 02:36:47 +00003339 if (node->type == XML_TEXT_NODE) return(1);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003340 return(0);
3341}
3342
Daniel Veillard97b58771998-10-20 06:14:16 +00003343/**
Daniel Veillard1e346af1999-02-22 10:33:01 +00003344 * xmlTextConcat:
Daniel Veillard97b58771998-10-20 06:14:16 +00003345 * @node: the node
3346 * @content: the content
3347 * @len: @content lenght
3348 *
3349 * Concat the given string at the end of the existing node content
Daniel Veillard260a68f1998-08-13 03:39:55 +00003350 */
Daniel Veillard97b58771998-10-20 06:14:16 +00003351
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003352void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003353xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003354 if (node == NULL) return;
3355
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003356 if ((node->type != XML_TEXT_NODE) &&
3357 (node->type != XML_CDATA_SECTION_NODE)) {
3358 fprintf(stderr, "xmlTextConcat: node is not text nor cdata\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +00003359 return;
3360 }
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003361#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard260a68f1998-08-13 03:39:55 +00003362 node->content = xmlStrncat(node->content, content, len);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003363#else
3364 xmlBufferAdd(node->content, content, len);
3365#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00003366}
3367
3368/************************************************************************
3369 * *
3370 * Output : to a FILE or in memory *
3371 * *
3372 ************************************************************************/
3373
Daniel Veillard5099ae81999-04-21 20:12:07 +00003374#define BASE_BUFFER_SIZE 4000
3375
3376/**
3377 * xmlBufferCreate:
3378 *
3379 * routine to create an XML buffer.
3380 * returns the new structure.
3381 */
3382xmlBufferPtr
3383xmlBufferCreate(void) {
3384 xmlBufferPtr ret;
3385
Daniel Veillard6454aec1999-09-02 22:04:43 +00003386 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
Daniel Veillard5099ae81999-04-21 20:12:07 +00003387 if (ret == NULL) {
3388 fprintf(stderr, "xmlBufferCreate : out of memory!\n");
3389 return(NULL);
3390 }
3391 ret->use = 0;
3392 ret->size = BASE_BUFFER_SIZE;
Daniel Veillard10a2c651999-12-12 13:03:50 +00003393 ret->alloc = xmlBufferAllocScheme;
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003394 ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar));
Daniel Veillard5099ae81999-04-21 20:12:07 +00003395 if (ret->content == NULL) {
3396 fprintf(stderr, "xmlBufferCreate : out of memory!\n");
Daniel Veillard6454aec1999-09-02 22:04:43 +00003397 xmlFree(ret);
Daniel Veillard5099ae81999-04-21 20:12:07 +00003398 return(NULL);
3399 }
3400 ret->content[0] = 0;
3401 return(ret);
3402}
3403
3404/**
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003405 * xmlBufferCreateSize:
3406 * @size: initial size of buffer
3407 *
3408 * routine to create an XML buffer.
3409 * returns the new structure.
3410 */
3411xmlBufferPtr
3412xmlBufferCreateSize(size_t size) {
3413 xmlBufferPtr ret;
3414
3415 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
3416 if (ret == NULL) {
3417 fprintf(stderr, "xmlBufferCreate : out of memory!\n");
3418 return(NULL);
3419 }
3420 ret->use = 0;
Daniel Veillard10a2c651999-12-12 13:03:50 +00003421 ret->alloc = xmlBufferAllocScheme;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003422 ret->size = (size ? size+2 : 0); /* +1 for ending null */
Daniel Veillard10a2c651999-12-12 13:03:50 +00003423 if (ret->size){
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003424 ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar));
3425 if (ret->content == NULL) {
3426 fprintf(stderr, "xmlBufferCreate : out of memory!\n");
3427 xmlFree(ret);
3428 return(NULL);
3429 }
3430 ret->content[0] = 0;
3431 } else
3432 ret->content = NULL;
3433 return(ret);
3434}
3435
3436/**
3437 * xmlBufferAllocationScheme:
3438 * @buf: the buffer to free
3439 * @scheme: allocation scheme to use
3440 *
3441 * Sets the allocation scheme for this buffer
3442 */
3443void
3444xmlBufferSetAllocationScheme(xmlBufferPtr buf,
3445 xmlBufferAllocationScheme scheme) {
3446 if (buf == NULL) {
3447 fprintf(stderr, "xmlBufferSetAllocationScheme: buf == NULL\n");
3448 return;
3449 }
3450
3451 buf->alloc = scheme;
3452}
3453
3454/**
Daniel Veillard5099ae81999-04-21 20:12:07 +00003455 * xmlBufferFree:
3456 * @buf: the buffer to free
3457 *
3458 * Frees an XML buffer.
3459 */
3460void
3461xmlBufferFree(xmlBufferPtr buf) {
3462 if (buf == NULL) {
3463 fprintf(stderr, "xmlBufferFree: buf == NULL\n");
3464 return;
3465 }
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003466 if (buf->content != NULL) {
3467#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard5099ae81999-04-21 20:12:07 +00003468 memset(buf->content, -1, BASE_BUFFER_SIZE);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003469#else
3470 memset(buf->content, -1, buf->size);
3471#endif
Daniel Veillard6454aec1999-09-02 22:04:43 +00003472 xmlFree(buf->content);
Daniel Veillard5099ae81999-04-21 20:12:07 +00003473 }
3474 memset(buf, -1, sizeof(xmlBuffer));
Daniel Veillard6454aec1999-09-02 22:04:43 +00003475 xmlFree(buf);
Daniel Veillard5099ae81999-04-21 20:12:07 +00003476}
3477
3478/**
Daniel Veillarde2d034d1999-07-27 19:52:06 +00003479 * xmlBufferEmpty:
3480 * @buf: the buffer
3481 *
3482 * empty a buffer.
3483 */
3484void
3485xmlBufferEmpty(xmlBufferPtr buf) {
3486 buf->use = 0;
3487 memset(buf->content, -1, buf->size);/* just for debug */
3488}
3489
3490/**
3491 * xmlBufferShrink:
3492 * @buf: the buffer to dump
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003493 * @len: the number of xmlChar to remove
Daniel Veillarde2d034d1999-07-27 19:52:06 +00003494 *
3495 * Remove the beginning of an XML buffer.
3496 *
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003497 * Returns the number of xmlChar removed, or -1 in case of failure.
Daniel Veillarde2d034d1999-07-27 19:52:06 +00003498 */
3499int
3500xmlBufferShrink(xmlBufferPtr buf, int len) {
3501 if (len == 0) return(0);
3502 if (len > buf->use) return(-1);
3503
3504 buf->use -= len;
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003505 memmove(buf->content, &buf->content[len], buf->use * sizeof(xmlChar));
Daniel Veillarde2d034d1999-07-27 19:52:06 +00003506
3507 buf->content[buf->use] = 0;
3508 return(len);
3509}
3510
3511/**
Daniel Veillard5099ae81999-04-21 20:12:07 +00003512 * xmlBufferDump:
3513 * @file: the file output
3514 * @buf: the buffer to dump
3515 *
3516 * Dumps an XML buffer to a FILE *.
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003517 * Returns the number of xmlChar written
Daniel Veillard5099ae81999-04-21 20:12:07 +00003518 */
3519int
3520xmlBufferDump(FILE *file, xmlBufferPtr buf) {
3521 int ret;
3522
3523 if (buf == NULL) {
3524 fprintf(stderr, "xmlBufferDump: buf == NULL\n");
3525 return(0);
3526 }
3527 if (buf->content == NULL) {
3528 fprintf(stderr, "xmlBufferDump: buf->content == NULL\n");
3529 return(0);
3530 }
3531 if (file == NULL) file = stdout;
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003532 ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
Daniel Veillard5099ae81999-04-21 20:12:07 +00003533 return(ret);
3534}
3535
3536/**
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003537 * xmlBufferContent:
3538 * @buf: the buffer to resize
3539 *
3540 * Returns the internal content
3541 */
3542
3543const xmlChar*
3544xmlBufferContent(const xmlBufferPtr buf)
3545{
3546 if(!buf)
3547 return NULL;
3548
3549 return buf->content;
3550}
3551
3552/**
3553 * xmlBufferLength:
3554 * @buf: the buffer
3555 *
3556 * Returns the length of data in the internal content
3557 */
3558
3559int
3560xmlBufferLength(const xmlBufferPtr buf)
3561{
3562 if(!buf)
3563 return 0;
3564
3565 return buf->use;
3566}
3567
3568/**
3569 * xmlBufferResize:
3570 * @buf: the buffer to resize
3571 * @len: the desired size
3572 *
3573 * Resize a buffer to accomodate minimum size of <len>.
3574 *
3575 * Returns 0 in case of problems, 1 otherwise
3576 */
3577int
3578xmlBufferResize(xmlBufferPtr buf, int size)
3579{
3580 int newSize = (buf->size ? buf->size*2 : size);/*take care of empty case*/
3581 xmlChar* rebuf = NULL;
3582
3583 /* Don't resize if we don't have to */
3584 if(size < buf->size)
3585 return 1;
3586
3587 /* figure out new size */
3588 switch(buf->alloc){
3589 case XML_BUFFER_ALLOC_DOUBLEIT:
3590 while(size > newSize) newSize *= 2;
3591 break;
3592 case XML_BUFFER_ALLOC_EXACT:
3593 newSize = size+10;
3594 break;
3595 default:
3596 newSize = size+10;
3597 break;
3598 }
3599
3600 if (buf->content == NULL)
3601 rebuf = (xmlChar *) xmlMalloc(newSize * sizeof(xmlChar));
3602 else
3603 rebuf = (xmlChar *) xmlRealloc(buf->content,
3604 newSize * sizeof(xmlChar));
3605 if (rebuf == NULL) {
3606 fprintf(stderr, "xmlBufferAdd : out of memory!\n");
3607 return 0;
3608 }
3609 buf->content = rebuf;
3610 buf->size = newSize;
3611
3612 return 1;
3613}
3614/**
Daniel Veillard5099ae81999-04-21 20:12:07 +00003615 * xmlBufferAdd:
3616 * @buf: the buffer to dump
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003617 * @str: the xmlChar string
3618 * @len: the number of xmlChar to add
Daniel Veillard5099ae81999-04-21 20:12:07 +00003619 *
Daniel Veillard10a2c651999-12-12 13:03:50 +00003620 * Add a string range to an XML buffer. if len == -1, the lenght of
3621 * str is recomputed.
Daniel Veillard5099ae81999-04-21 20:12:07 +00003622 */
3623void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003624xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003625 int l, needSize;
Daniel Veillard5099ae81999-04-21 20:12:07 +00003626
3627 if (str == NULL) {
3628 fprintf(stderr, "xmlBufferAdd: str == NULL\n");
3629 return;
3630 }
Daniel Veillard10a2c651999-12-12 13:03:50 +00003631 if (len < -1) {
3632 fprintf(stderr, "xmlBufferAdd: len < 0\n");
3633 return;
3634 }
3635 if (len == 0) return;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003636
3637 /* CJN What's this for??? */
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003638 if (len < 0)
3639 l = xmlStrlen(str);
3640 else
3641 for (l = 0;l < len;l++)
3642 if (str[l] == 0) break;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003643 if (l < len){ len = l; printf("xmlBufferAdd bad length\n"); }
3644
3645 /* CJN 11.18.99 okay, now I'm using the length */
3646 if(len == -1) len = l;
3647
3648
Daniel Veillarde2d034d1999-07-27 19:52:06 +00003649 if (len <= 0) return;
Daniel Veillard5099ae81999-04-21 20:12:07 +00003650
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003651 needSize = buf->use + len + 2;
3652 if(needSize > buf->size){
3653 if(!xmlBufferResize(buf, needSize)){
3654 fprintf(stderr, "xmlBufferAdd : out of memory!\n");
3655 return;
3656 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00003657 }
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003658
3659 memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
Daniel Veillarde2d034d1999-07-27 19:52:06 +00003660 buf->use += len;
3661 buf->content[buf->use] = 0;
Daniel Veillard5099ae81999-04-21 20:12:07 +00003662}
3663
3664/**
3665 * xmlBufferCat:
3666 * @buf: the buffer to dump
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003667 * @str: the xmlChar string
Daniel Veillard5099ae81999-04-21 20:12:07 +00003668 *
3669 * Append a zero terminated string to an XML buffer.
3670 */
3671void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003672xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003673 if (str != NULL)
3674 xmlBufferAdd(buf, str, -1);
Daniel Veillard5099ae81999-04-21 20:12:07 +00003675}
3676
3677/**
3678 * xmlBufferCCat:
3679 * @buf: the buffer to dump
3680 * @str: the C char string
3681 *
3682 * Append a zero terminated C string to an XML buffer.
3683 */
3684void
3685xmlBufferCCat(xmlBufferPtr buf, const char *str) {
3686 const char *cur;
3687
3688 if (str == NULL) {
3689 fprintf(stderr, "xmlBufferAdd: str == NULL\n");
3690 return;
3691 }
3692 for (cur = str;*cur != 0;cur++) {
3693 if (buf->use + 10 >= buf->size) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003694 if(!xmlBufferResize(buf, buf->use+10)){
3695 fprintf(stderr, "xmlBufferCCat : out of memory!\n");
3696 return;
3697 }
3698 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00003699 buf->content[buf->use++] = *cur;
3700 }
3701}
Daniel Veillard260a68f1998-08-13 03:39:55 +00003702
Daniel Veillard97b58771998-10-20 06:14:16 +00003703/**
3704 * xmlBufferWriteCHAR:
Daniel Veillard5099ae81999-04-21 20:12:07 +00003705 * @buf: the XML buffer
Daniel Veillard97b58771998-10-20 06:14:16 +00003706 * @string: the string to add
3707 *
3708 * routine which manage and grows an output buffer. This one add
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003709 * xmlChars at the end of the buffer.
Daniel Veillard97b58771998-10-20 06:14:16 +00003710 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003711void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003712xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00003713 xmlBufferCat(buf, string);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003714}
3715
Daniel Veillard97b58771998-10-20 06:14:16 +00003716/**
3717 * xmlBufferWriteChar:
Daniel Veillard011b63c1999-06-02 17:44:04 +00003718 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00003719 * @string: the string to add
3720 *
3721 * routine which manage and grows an output buffer. This one add
3722 * C chars at the end of the array.
3723 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003724void
Daniel Veillard5099ae81999-04-21 20:12:07 +00003725xmlBufferWriteChar(xmlBufferPtr buf, const char *string) {
3726 xmlBufferCCat(buf, string);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003727}
3728
Daniel Veillard5099ae81999-04-21 20:12:07 +00003729
Daniel Veillard97b58771998-10-20 06:14:16 +00003730/**
Daniel Veillard011b63c1999-06-02 17:44:04 +00003731 * xmlBufferWriteQuotedString:
3732 * @buf: the XML buffer output
3733 * @string: the string to add
3734 *
3735 * routine which manage and grows an output buffer. This one writes
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003736 * a quoted or double quoted xmlChar string, checking first if it holds
Daniel Veillard011b63c1999-06-02 17:44:04 +00003737 * quote or double-quotes internally
3738 */
3739void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003740xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) {
Daniel Veillardb96e6431999-08-29 21:02:19 +00003741 if (xmlStrchr(string, '"')) {
3742 if (xmlStrchr(string, '\'')) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00003743 fprintf(stderr,
3744 "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n");
3745 }
3746 xmlBufferCCat(buf, "'");
3747 xmlBufferCat(buf, string);
3748 xmlBufferCCat(buf, "'");
3749 } else {
3750 xmlBufferCCat(buf, "\"");
3751 xmlBufferCat(buf, string);
3752 xmlBufferCCat(buf, "\"");
3753 }
3754}
3755
3756
3757/**
Daniel Veillard97b58771998-10-20 06:14:16 +00003758 * xmlGlobalNsDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00003759 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00003760 * @cur: a namespace
3761 *
3762 * Dump a global Namespace, this is the old version based on PIs.
Daniel Veillard260a68f1998-08-13 03:39:55 +00003763 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003764static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00003765xmlGlobalNsDump(xmlBufferPtr buf, xmlNsPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003766 if (cur == NULL) {
3767 fprintf(stderr, "xmlGlobalNsDump : Ns == NULL\n");
3768 return;
3769 }
3770 if (cur->type == XML_GLOBAL_NAMESPACE) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00003771 xmlBufferWriteChar(buf, "<?namespace");
Daniel Veillard260a68f1998-08-13 03:39:55 +00003772 if (cur->href != NULL) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00003773 xmlBufferWriteChar(buf, " href=");
3774 xmlBufferWriteQuotedString(buf, cur->href);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003775 }
3776 if (cur->prefix != NULL) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00003777 xmlBufferWriteChar(buf, " AS=");
3778 xmlBufferWriteQuotedString(buf, cur->prefix);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003779 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00003780 xmlBufferWriteChar(buf, "?>\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +00003781 }
3782}
3783
Daniel Veillard97b58771998-10-20 06:14:16 +00003784/**
3785 * xmlGlobalNsListDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00003786 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00003787 * @cur: the first namespace
3788 *
3789 * Dump a list of global Namespace, this is the old version based on PIs.
Daniel Veillard260a68f1998-08-13 03:39:55 +00003790 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003791static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00003792xmlGlobalNsListDump(xmlBufferPtr buf, xmlNsPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003793 while (cur != NULL) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00003794 xmlGlobalNsDump(buf, cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003795 cur = cur->next;
3796 }
3797}
3798
Daniel Veillard97b58771998-10-20 06:14:16 +00003799/**
3800 * xmlNsDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00003801 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00003802 * @cur: a namespace
3803 *
Daniel Veillard260a68f1998-08-13 03:39:55 +00003804 * Dump a local Namespace definition.
Daniel Veillard97b58771998-10-20 06:14:16 +00003805 * Should be called in the context of attributes dumps.
Daniel Veillard260a68f1998-08-13 03:39:55 +00003806 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003807static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00003808xmlNsDump(xmlBufferPtr buf, xmlNsPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003809 if (cur == NULL) {
3810 fprintf(stderr, "xmlNsDump : Ns == NULL\n");
3811 return;
3812 }
3813 if (cur->type == XML_LOCAL_NAMESPACE) {
3814 /* Within the context of an element attributes */
3815 if (cur->prefix != NULL) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00003816 xmlBufferWriteChar(buf, " xmlns:");
3817 xmlBufferWriteCHAR(buf, cur->prefix);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003818 } else
Daniel Veillard5099ae81999-04-21 20:12:07 +00003819 xmlBufferWriteChar(buf, " xmlns");
Daniel Veillard011b63c1999-06-02 17:44:04 +00003820 xmlBufferWriteChar(buf, "=");
3821 xmlBufferWriteQuotedString(buf, cur->href);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003822 }
3823}
3824
Daniel Veillard97b58771998-10-20 06:14:16 +00003825/**
3826 * xmlNsListDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00003827 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00003828 * @cur: the first namespace
3829 *
3830 * Dump a list of local Namespace definitions.
3831 * Should be called in the context of attributes dumps.
Daniel Veillard260a68f1998-08-13 03:39:55 +00003832 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003833static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00003834xmlNsListDump(xmlBufferPtr buf, xmlNsPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003835 while (cur != NULL) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00003836 xmlNsDump(buf, cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003837 cur = cur->next;
3838 }
3839}
3840
Daniel Veillard97b58771998-10-20 06:14:16 +00003841/**
3842 * xmlDtdDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00003843 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00003844 * @doc: the document
3845 *
3846 * Dump the XML document DTD, if any.
Daniel Veillard260a68f1998-08-13 03:39:55 +00003847 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003848static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00003849xmlDtdDump(xmlBufferPtr buf, xmlDocPtr doc) {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00003850 xmlDtdPtr cur = doc->intSubset;
Daniel Veillard260a68f1998-08-13 03:39:55 +00003851
3852 if (cur == NULL) {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00003853 fprintf(stderr, "xmlDtdDump : no internal subset\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +00003854 return;
3855 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00003856 xmlBufferWriteChar(buf, "<!DOCTYPE ");
3857 xmlBufferWriteCHAR(buf, cur->name);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003858 if (cur->ExternalID != NULL) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00003859 xmlBufferWriteChar(buf, " PUBLIC ");
3860 xmlBufferWriteQuotedString(buf, cur->ExternalID);
3861 xmlBufferWriteChar(buf, " ");
3862 xmlBufferWriteQuotedString(buf, cur->SystemID);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003863 } else if (cur->SystemID != NULL) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00003864 xmlBufferWriteChar(buf, " SYSTEM ");
3865 xmlBufferWriteQuotedString(buf, cur->SystemID);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003866 }
Daniel Veillard1e346af1999-02-22 10:33:01 +00003867 if ((cur->entities == NULL) && (cur->elements == NULL) &&
3868 (cur->attributes == NULL) && (cur->notations == NULL)) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00003869 xmlBufferWriteChar(buf, ">\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +00003870 return;
3871 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00003872 xmlBufferWriteChar(buf, " [\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +00003873 if (cur->entities != NULL)
Daniel Veillard5099ae81999-04-21 20:12:07 +00003874 xmlDumpEntitiesTable(buf, (xmlEntitiesTablePtr) cur->entities);
Daniel Veillard1e346af1999-02-22 10:33:01 +00003875 if (cur->notations != NULL)
Daniel Veillard5099ae81999-04-21 20:12:07 +00003876 xmlDumpNotationTable(buf, (xmlNotationTablePtr) cur->notations);
Daniel Veillard3b9def11999-01-31 22:15:06 +00003877 if (cur->elements != NULL)
Daniel Veillard5099ae81999-04-21 20:12:07 +00003878 xmlDumpElementTable(buf, (xmlElementTablePtr) cur->elements);
Daniel Veillard1e346af1999-02-22 10:33:01 +00003879 if (cur->attributes != NULL)
Daniel Veillard5099ae81999-04-21 20:12:07 +00003880 xmlDumpAttributeTable(buf, (xmlAttributeTablePtr) cur->attributes);
3881 xmlBufferWriteChar(buf, "]");
Daniel Veillard260a68f1998-08-13 03:39:55 +00003882
Daniel Veillard5099ae81999-04-21 20:12:07 +00003883 xmlBufferWriteChar(buf, ">\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +00003884}
3885
Daniel Veillard97b58771998-10-20 06:14:16 +00003886/**
3887 * xmlAttrDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00003888 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00003889 * @doc: the document
3890 * @cur: the attribute pointer
3891 *
3892 * Dump an XML attribute
Daniel Veillard260a68f1998-08-13 03:39:55 +00003893 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003894static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00003895xmlAttrDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) {
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003896 xmlChar *value;
Daniel Veillardccb09631998-10-27 06:21:04 +00003897
Daniel Veillard260a68f1998-08-13 03:39:55 +00003898 if (cur == NULL) {
3899 fprintf(stderr, "xmlAttrDump : property == NULL\n");
3900 return;
3901 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00003902 xmlBufferWriteChar(buf, " ");
Daniel Veillardb96e6431999-08-29 21:02:19 +00003903 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
3904 xmlBufferWriteCHAR(buf, cur->ns->prefix);
3905 xmlBufferWriteChar(buf, ":");
3906 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00003907 xmlBufferWriteCHAR(buf, cur->name);
Daniel Veillardccb09631998-10-27 06:21:04 +00003908 value = xmlNodeListGetString(doc, cur->val, 0);
3909 if (value) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00003910 xmlBufferWriteChar(buf, "=");
3911 xmlBufferWriteQuotedString(buf, value);
Daniel Veillard6454aec1999-09-02 22:04:43 +00003912 xmlFree(value);
Daniel Veillard726c7e31999-02-08 15:13:10 +00003913 } else {
Daniel Veillard5099ae81999-04-21 20:12:07 +00003914 xmlBufferWriteChar(buf, "=\"\"");
Daniel Veillard260a68f1998-08-13 03:39:55 +00003915 }
3916}
3917
Daniel Veillard97b58771998-10-20 06:14:16 +00003918/**
3919 * xmlAttrListDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00003920 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00003921 * @doc: the document
3922 * @cur: the first attribute pointer
3923 *
3924 * Dump a list of XML attributes
Daniel Veillard260a68f1998-08-13 03:39:55 +00003925 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003926static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00003927xmlAttrListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003928 if (cur == NULL) {
3929 fprintf(stderr, "xmlAttrListDump : property == NULL\n");
3930 return;
3931 }
3932 while (cur != NULL) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00003933 xmlAttrDump(buf, doc, cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003934 cur = cur->next;
3935 }
3936}
3937
Daniel Veillard260a68f1998-08-13 03:39:55 +00003938
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003939static void
Daniel Veillard7d2c2761999-10-11 15:09:51 +00003940xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
3941 int format);
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003942void
3943htmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur);
3944
Daniel Veillard97b58771998-10-20 06:14:16 +00003945/**
3946 * xmlNodeListDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00003947 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00003948 * @doc: the document
3949 * @cur: the first node
3950 * @level: the imbrication level for indenting
Daniel Veillard7d2c2761999-10-11 15:09:51 +00003951 * @format: is formatting allowed
Daniel Veillard97b58771998-10-20 06:14:16 +00003952 *
3953 * Dump an XML node list, recursive behaviour,children are printed too.
3954 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003955static void
Daniel Veillard7d2c2761999-10-11 15:09:51 +00003956xmlNodeListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
3957 int format) {
3958 int i;
Daniel Veillardccb09631998-10-27 06:21:04 +00003959
Daniel Veillard260a68f1998-08-13 03:39:55 +00003960 if (cur == NULL) {
3961 fprintf(stderr, "xmlNodeListDump : node == NULL\n");
3962 return;
3963 }
3964 while (cur != NULL) {
Daniel Veillard7d2c2761999-10-11 15:09:51 +00003965 if ((format) && (xmlIndentTreeOutput) &&
3966 (cur->type == XML_ELEMENT_NODE))
3967 for (i = 0;i < level;i++)
3968 xmlBufferWriteChar(buf, " ");
3969 xmlNodeDump(buf, doc, cur, level, format);
3970 if (format) {
3971 xmlBufferWriteChar(buf, "\n");
Daniel Veillardccb09631998-10-27 06:21:04 +00003972 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00003973 cur = cur->next;
3974 }
3975}
3976
Daniel Veillard97b58771998-10-20 06:14:16 +00003977/**
Daniel Veillardccb09631998-10-27 06:21:04 +00003978 * xmlNodeDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00003979 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00003980 * @doc: the document
3981 * @cur: the current node
3982 * @level: the imbrication level for indenting
Daniel Veillard7d2c2761999-10-11 15:09:51 +00003983 * @format: is formatting allowed
Daniel Veillard97b58771998-10-20 06:14:16 +00003984 *
3985 * Dump an XML node, recursive behaviour,children are printed too.
Daniel Veillard260a68f1998-08-13 03:39:55 +00003986 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003987static void
Daniel Veillard7d2c2761999-10-11 15:09:51 +00003988xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
3989 int format) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003990 int i;
Daniel Veillard7d2c2761999-10-11 15:09:51 +00003991 xmlNodePtr tmp;
Daniel Veillard260a68f1998-08-13 03:39:55 +00003992
3993 if (cur == NULL) {
3994 fprintf(stderr, "xmlNodeDump : node == NULL\n");
3995 return;
3996 }
Daniel Veillard0bef1311998-10-14 02:36:47 +00003997 if (cur->type == XML_TEXT_NODE) {
Daniel Veillard14fff061999-06-22 21:49:07 +00003998 if (cur->content != NULL) {
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003999 xmlChar *buffer;
Daniel Veillard14fff061999-06-22 21:49:07 +00004000
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004001#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard14fff061999-06-22 21:49:07 +00004002 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004003#else
4004 buffer = xmlEncodeEntitiesReentrant(doc,
4005 xmlBufferContent(cur->content));
4006#endif
Daniel Veillard14fff061999-06-22 21:49:07 +00004007 if (buffer != NULL) {
4008 xmlBufferWriteCHAR(buf, buffer);
Daniel Veillard6454aec1999-09-02 22:04:43 +00004009 xmlFree(buffer);
Daniel Veillard14fff061999-06-22 21:49:07 +00004010 }
4011 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00004012 return;
4013 }
Daniel Veillardb96e6431999-08-29 21:02:19 +00004014 if (cur->type == XML_PI_NODE) {
4015 if (cur->content != NULL) {
4016 xmlBufferWriteChar(buf, "<?");
4017 xmlBufferWriteCHAR(buf, cur->name);
4018 if (cur->content != NULL) {
4019 xmlBufferWriteChar(buf, " ");
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004020#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardb96e6431999-08-29 21:02:19 +00004021 xmlBufferWriteCHAR(buf, cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004022#else
4023 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
4024#endif
Daniel Veillardb96e6431999-08-29 21:02:19 +00004025 }
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004026 xmlBufferWriteChar(buf, "?>");
Daniel Veillardb96e6431999-08-29 21:02:19 +00004027 }
4028 return;
4029 }
Daniel Veillard0bef1311998-10-14 02:36:47 +00004030 if (cur->type == XML_COMMENT_NODE) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00004031 if (cur->content != NULL) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004032 xmlBufferWriteChar(buf, "<!--");
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004033#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard5099ae81999-04-21 20:12:07 +00004034 xmlBufferWriteCHAR(buf, cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004035#else
4036 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
4037#endif
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004038 xmlBufferWriteChar(buf, "-->");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004039 }
4040 return;
4041 }
Daniel Veillardccb09631998-10-27 06:21:04 +00004042 if (cur->type == XML_ENTITY_REF_NODE) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004043 xmlBufferWriteChar(buf, "&");
4044 xmlBufferWriteCHAR(buf, cur->name);
4045 xmlBufferWriteChar(buf, ";");
Daniel Veillardccb09631998-10-27 06:21:04 +00004046 return;
4047 }
Daniel Veillardb05deb71999-08-10 19:04:08 +00004048 if (cur->type == XML_CDATA_SECTION_NODE) {
4049 xmlBufferWriteChar(buf, "<![CDATA[");
4050 if (cur->content != NULL)
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004051#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardb05deb71999-08-10 19:04:08 +00004052 xmlBufferWriteCHAR(buf, cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004053#else
4054 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
4055#endif
Daniel Veillardb05deb71999-08-10 19:04:08 +00004056 xmlBufferWriteChar(buf, "]]>");
4057 return;
4058 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00004059
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004060 if (format == 1) {
4061 tmp = cur->childs;
4062 while (tmp != NULL) {
4063 if ((tmp->type == XML_TEXT_NODE) ||
4064 (tmp->type == XML_ENTITY_REF_NODE)) {
4065 format = 0;
4066 break;
4067 }
4068 tmp = tmp->next;
4069 }
4070 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004071 xmlBufferWriteChar(buf, "<");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004072 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004073 xmlBufferWriteCHAR(buf, cur->ns->prefix);
4074 xmlBufferWriteChar(buf, ":");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004075 }
4076
Daniel Veillard5099ae81999-04-21 20:12:07 +00004077 xmlBufferWriteCHAR(buf, cur->name);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004078 if (cur->nsDef)
Daniel Veillard5099ae81999-04-21 20:12:07 +00004079 xmlNsListDump(buf, cur->nsDef);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004080 if (cur->properties != NULL)
Daniel Veillard5099ae81999-04-21 20:12:07 +00004081 xmlAttrListDump(buf, doc, cur->properties);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004082
4083 if ((cur->content == NULL) && (cur->childs == NULL)) {
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004084 xmlBufferWriteChar(buf, "/>");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004085 return;
4086 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004087 xmlBufferWriteChar(buf, ">");
Daniel Veillard14fff061999-06-22 21:49:07 +00004088 if (cur->content != NULL) {
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004089 xmlChar *buffer;
Daniel Veillard14fff061999-06-22 21:49:07 +00004090
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004091#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard14fff061999-06-22 21:49:07 +00004092 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004093#else
4094 buffer = xmlEncodeEntitiesReentrant(doc,
4095 xmlBufferContent(cur->content));
4096#endif
Daniel Veillard14fff061999-06-22 21:49:07 +00004097 if (buffer != NULL) {
4098 xmlBufferWriteCHAR(buf, buffer);
Daniel Veillard6454aec1999-09-02 22:04:43 +00004099 xmlFree(buffer);
Daniel Veillard14fff061999-06-22 21:49:07 +00004100 }
4101 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00004102 if (cur->childs != NULL) {
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004103 if (format) xmlBufferWriteChar(buf, "\n");
4104 xmlNodeListDump(buf, doc, cur->childs, level + 1, format);
4105 if ((xmlIndentTreeOutput) && (format))
4106 for (i = 0;i < level;i++)
4107 xmlBufferWriteChar(buf, " ");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004108 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004109 xmlBufferWriteChar(buf, "</");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004110 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004111 xmlBufferWriteCHAR(buf, cur->ns->prefix);
4112 xmlBufferWriteChar(buf, ":");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004113 }
4114
Daniel Veillard5099ae81999-04-21 20:12:07 +00004115 xmlBufferWriteCHAR(buf, cur->name);
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004116 xmlBufferWriteChar(buf, ">");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004117}
4118
Daniel Veillard97b58771998-10-20 06:14:16 +00004119/**
Daniel Veillarddbfd6411999-12-28 16:35:14 +00004120 * xmlElemDump:
4121 * @buf: the XML buffer output
4122 * @doc: the document
4123 * @cur: the current node
4124 *
4125 * Dump an XML/HTML node, recursive behaviour,children are printed too.
4126 */
4127void
4128xmlElemDump(FILE *f, xmlDocPtr doc, xmlNodePtr cur) {
4129 xmlBufferPtr buf;
4130
4131 if (cur == NULL) {
4132#ifdef DEBUG_TREE
4133 fprintf(stderr, "xmlElemDump : cur == NULL\n");
4134#endif
4135 return;
4136 }
4137 if (doc == NULL) {
4138#ifdef DEBUG_TREE
4139 fprintf(stderr, "xmlElemDump : doc == NULL\n");
4140#endif
4141 }
4142 buf = xmlBufferCreate();
4143 if (buf == NULL) return;
4144 if ((doc != NULL) &&
4145 (doc->type == XML_HTML_DOCUMENT_NODE)) {
4146 htmlNodeDump(buf, doc, cur);
4147 } else
4148 xmlNodeDump(buf, doc, cur, 0, 1);
4149 xmlBufferDump(f, buf);
4150 xmlBufferFree(buf);
4151}
4152
4153/**
Daniel Veillard97b58771998-10-20 06:14:16 +00004154 * xmlDocContentDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00004155 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00004156 * @cur: the document
4157 *
4158 * Dump an XML document.
Daniel Veillard260a68f1998-08-13 03:39:55 +00004159 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004160static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00004161xmlDocContentDump(xmlBufferPtr buf, xmlDocPtr cur) {
Daniel Veillardbe70ff71999-07-05 16:50:46 +00004162 xmlBufferWriteChar(buf, "<?xml version=");
4163 if (cur->version != NULL)
4164 xmlBufferWriteQuotedString(buf, cur->version);
4165 else
4166 xmlBufferWriteChar(buf, "\"1.0\"");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004167 if (cur->encoding != NULL) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00004168 xmlBufferWriteChar(buf, " encoding=");
4169 xmlBufferWriteQuotedString(buf, cur->encoding);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004170 }
4171 switch (cur->standalone) {
4172 case 0:
Daniel Veillard5099ae81999-04-21 20:12:07 +00004173 xmlBufferWriteChar(buf, " standalone=\"no\"");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004174 break;
4175 case 1:
Daniel Veillard5099ae81999-04-21 20:12:07 +00004176 xmlBufferWriteChar(buf, " standalone=\"yes\"");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004177 break;
4178 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004179 xmlBufferWriteChar(buf, "?>\n");
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00004180 if (cur->intSubset != NULL)
Daniel Veillard5099ae81999-04-21 20:12:07 +00004181 xmlDtdDump(buf, cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004182 if (cur->root != NULL) {
Daniel Veillardb96e6431999-08-29 21:02:19 +00004183 xmlNodePtr child = cur->root;
4184
Daniel Veillard260a68f1998-08-13 03:39:55 +00004185 /* global namespace definitions, the old way */
4186 if (oldXMLWDcompatibility)
Daniel Veillard5099ae81999-04-21 20:12:07 +00004187 xmlGlobalNsListDump(buf, cur->oldNs);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004188 else
4189 xmlUpgradeOldNs(cur);
Daniel Veillardb96e6431999-08-29 21:02:19 +00004190
4191 while (child != NULL) {
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004192 xmlNodeDump(buf, cur, child, 0, 1);
4193 xmlBufferWriteChar(buf, "\n");
Daniel Veillardb96e6431999-08-29 21:02:19 +00004194 child = child->next;
4195 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00004196 }
4197}
4198
Daniel Veillard97b58771998-10-20 06:14:16 +00004199/**
4200 * xmlDocDumpMemory:
4201 * @cur: the document
4202 * @mem: OUT: the memory pointer
4203 * @size: OUT: the memory lenght
4204 *
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004205 * Dump an XML document in memory and return the xmlChar * and it's size.
Daniel Veillard97b58771998-10-20 06:14:16 +00004206 * It's up to the caller to free the memory.
Daniel Veillard260a68f1998-08-13 03:39:55 +00004207 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004208void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004209xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004210 xmlBufferPtr buf;
4211
Daniel Veillard260a68f1998-08-13 03:39:55 +00004212 if (cur == NULL) {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00004213#ifdef DEBUG_TREE
4214 fprintf(stderr, "xmlDocDumpMemory : document == NULL\n");
4215#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00004216 *mem = NULL;
4217 *size = 0;
4218 return;
4219 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004220 buf = xmlBufferCreate();
4221 if (buf == NULL) {
4222 *mem = NULL;
4223 *size = 0;
4224 return;
4225 }
4226 xmlDocContentDump(buf, cur);
Daniel Veillardb05deb71999-08-10 19:04:08 +00004227 *mem = xmlStrndup(buf->content, buf->use);
Daniel Veillard5099ae81999-04-21 20:12:07 +00004228 *size = buf->use;
Daniel Veillardb05deb71999-08-10 19:04:08 +00004229 xmlBufferFree(buf);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004230}
4231
Daniel Veillard97b58771998-10-20 06:14:16 +00004232/**
4233 * xmlGetDocCompressMode:
4234 * @doc: the document
4235 *
4236 * get the compression ratio for a document, ZLIB based
Daniel Veillard1e346af1999-02-22 10:33:01 +00004237 * Returns 0 (uncompressed) to 9 (max compression)
Daniel Veillard151b1b01998-09-23 00:49:46 +00004238 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004239int
Daniel Veillarddbfd6411999-12-28 16:35:14 +00004240xmlGetDocCompressMode (xmlDocPtr doc) {
Daniel Veillard15a8df41998-09-24 19:15:06 +00004241 if (doc == NULL) return(-1);
4242 return(doc->compression);
4243}
4244
Daniel Veillard97b58771998-10-20 06:14:16 +00004245/**
4246 * xmlSetDocCompressMode:
4247 * @doc: the document
4248 * @mode: the compression ratio
4249 *
4250 * set the compression ratio for a document, ZLIB based
4251 * Correct values: 0 (uncompressed) to 9 (max compression)
4252 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004253void
4254xmlSetDocCompressMode (xmlDocPtr doc, int mode) {
Daniel Veillard15a8df41998-09-24 19:15:06 +00004255 if (doc == NULL) return;
4256 if (mode < 0) doc->compression = 0;
4257 else if (mode > 9) doc->compression = 9;
4258 else doc->compression = mode;
4259}
4260
Daniel Veillard97b58771998-10-20 06:14:16 +00004261/**
4262 * xmlGetCompressMode:
4263 *
4264 * get the default compression mode used, ZLIB based.
Daniel Veillard1e346af1999-02-22 10:33:01 +00004265 * Returns 0 (uncompressed) to 9 (max compression)
Daniel Veillard15a8df41998-09-24 19:15:06 +00004266 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004267int
4268 xmlGetCompressMode(void) {
Daniel Veillard151b1b01998-09-23 00:49:46 +00004269 return(xmlCompressMode);
4270}
Daniel Veillard97b58771998-10-20 06:14:16 +00004271
4272/**
4273 * xmlSetCompressMode:
4274 * @mode: the compression ratio
4275 *
4276 * set the default compression mode used, ZLIB based
4277 * Correct values: 0 (uncompressed) to 9 (max compression)
4278 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004279void
4280xmlSetCompressMode(int mode) {
Daniel Veillard151b1b01998-09-23 00:49:46 +00004281 if (mode < 0) xmlCompressMode = 0;
Daniel Veillard15a8df41998-09-24 19:15:06 +00004282 else if (mode > 9) xmlCompressMode = 9;
Daniel Veillard151b1b01998-09-23 00:49:46 +00004283 else xmlCompressMode = mode;
4284}
4285
Daniel Veillard97b58771998-10-20 06:14:16 +00004286/**
4287 * xmlDocDump:
4288 * @f: the FILE*
4289 * @cur: the document
4290 *
4291 * Dump an XML document to an open FILE.
Daniel Veillard260a68f1998-08-13 03:39:55 +00004292 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004293void
4294xmlDocDump(FILE *f, xmlDocPtr cur) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004295 xmlBufferPtr buf;
4296
Daniel Veillard260a68f1998-08-13 03:39:55 +00004297 if (cur == NULL) {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00004298#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00004299 fprintf(stderr, "xmlDocDump : document == NULL\n");
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00004300#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00004301 return;
4302 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004303 buf = xmlBufferCreate();
4304 if (buf == NULL) return;
4305 xmlDocContentDump(buf, cur);
4306 xmlBufferDump(f, buf);
4307 xmlBufferFree(buf);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004308}
4309
Daniel Veillard97b58771998-10-20 06:14:16 +00004310/**
4311 * xmlSaveFile:
4312 * @filename: the filename
4313 * @cur: the document
4314 *
4315 * Dump an XML document to a file. Will use compression if
Daniel Veillard11a48ec1999-11-23 10:40:46 +00004316 * compiled in and enabled. If @filename is "-" the stdout file is
4317 * used.
Daniel Veillard97b58771998-10-20 06:14:16 +00004318 * returns: the number of file written or -1 in case of failure.
Daniel Veillard151b1b01998-09-23 00:49:46 +00004319 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004320int
4321xmlSaveFile(const char *filename, xmlDocPtr cur) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004322 xmlBufferPtr buf;
Daniel Veillard151b1b01998-09-23 00:49:46 +00004323#ifdef HAVE_ZLIB_H
4324 gzFile zoutput = NULL;
4325 char mode[15];
4326#endif
Daniel Veillardccb09631998-10-27 06:21:04 +00004327 FILE *output = NULL;
Daniel Veillard151b1b01998-09-23 00:49:46 +00004328 int ret;
4329
Daniel Veillard5099ae81999-04-21 20:12:07 +00004330 /*
4331 * save the content to a temp buffer.
4332 */
4333 buf = xmlBufferCreate();
4334 if (buf == NULL) return(0);
4335 xmlDocContentDump(buf, cur);
4336
Daniel Veillard151b1b01998-09-23 00:49:46 +00004337#ifdef HAVE_ZLIB_H
Daniel Veillard11a48ec1999-11-23 10:40:46 +00004338 if (cur->compression < 0) cur->compression = xmlCompressMode;
Daniel Veillarddc3dd9d1998-09-24 19:25:54 +00004339 if ((cur->compression > 0) && (cur->compression <= 9)) {
4340 sprintf(mode, "w%d", cur->compression);
Daniel Veillard11a48ec1999-11-23 10:40:46 +00004341 if (!strcmp(filename, "-"))
4342 zoutput = gzdopen(1, mode);
4343 else
4344 zoutput = gzopen(filename, mode);
Daniel Veillard151b1b01998-09-23 00:49:46 +00004345 }
4346 if (zoutput == NULL) {
4347#endif
4348 output = fopen(filename, "w");
Daniel Veillard10a2c651999-12-12 13:03:50 +00004349 if (output == NULL) {
4350 xmlBufferFree(buf);
4351 return(-1);
4352 }
Daniel Veillard151b1b01998-09-23 00:49:46 +00004353#ifdef HAVE_ZLIB_H
4354 }
Daniel Veillard151b1b01998-09-23 00:49:46 +00004355
Daniel Veillard151b1b01998-09-23 00:49:46 +00004356 if (zoutput != NULL) {
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004357 ret = gzwrite(zoutput, buf->content, sizeof(xmlChar) * buf->use);
Daniel Veillard151b1b01998-09-23 00:49:46 +00004358 gzclose(zoutput);
Daniel Veillard5099ae81999-04-21 20:12:07 +00004359 } else {
4360#endif
4361 ret = xmlBufferDump(output, buf);
4362 fclose(output);
4363#ifdef HAVE_ZLIB_H
Daniel Veillard151b1b01998-09-23 00:49:46 +00004364 }
4365#endif
Manish Vachharajani5e60f5a1999-05-29 03:04:30 +00004366 xmlBufferFree(buf);
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004367 return(ret * sizeof(xmlChar));
Daniel Veillard151b1b01998-09-23 00:49:46 +00004368}
4369