blob: f21c76e95a92c95ca125216f9122518dd1b85dcf [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 Veillard71b656e2000-01-05 14:46:17 +0000693 if (inLine) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000694#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 Veillard71b656e2000-01-05 14:46:17 +0000944 /* Check for ID removal -> leading to invalid references ! */
945 if ((cur->node != NULL) &&
946 (xmlIsID(cur->node->doc, cur->node, cur)))
947 xmlRemoveID(cur->node->doc, cur);
Daniel Veillard6454aec1999-09-02 22:04:43 +0000948 if (cur->name != NULL) xmlFree((char *) cur->name);
Daniel Veillardccb09631998-10-27 06:21:04 +0000949 if (cur->val != NULL) xmlFreeNodeList(cur->val);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000950 memset(cur, -1, sizeof(xmlAttr));
Daniel Veillard6454aec1999-09-02 22:04:43 +0000951 xmlFree(cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000952}
953
Daniel Veillard97b58771998-10-20 06:14:16 +0000954/**
Daniel Veillard686d6b62000-01-03 11:08:02 +0000955 * xmlRemoveProp:
956 * @cur: an attribute
957 *
958 * Unlink and free one attribute, all the content is freed too
959 * Note this doesn't work for namespace definition attributes
960 *
961 * Returns 0 if success and -1 in case of error.
962 */
963int
964xmlRemoveProp(xmlAttrPtr cur) {
965 xmlAttrPtr tmp;
966 if (cur == NULL) {
967 fprintf(stderr, "xmlRemoveProp : cur == NULL\n");
968 return(-1);
969 }
970 if (cur->node == NULL) {
971 fprintf(stderr, "xmlRemoveProp : cur->node == NULL\n");
972 return(-1);
973 }
974 tmp = cur->node->properties;
975 if (tmp == cur) {
976 cur->node->properties = cur->next;
977 xmlFreeProp(cur);
978 return(0);
979 }
980 while (tmp != NULL) {
981 if (tmp->next == cur) {
982 tmp->next = cur->next;
983 xmlFreeProp(cur);
984 return(0);
985 }
986 tmp = tmp->next;
987 }
988 fprintf(stderr, "xmlRemoveProp : attribute not owned by its node\n");
989 return(-1);
990}
991
992/**
Daniel Veillardb96e6431999-08-29 21:02:19 +0000993 * xmlNewPI:
994 * @name: the processing instruction name
995 * @content: the PI content
996 *
997 * Creation of a processing instruction element.
998 * Returns a pointer to the new node object.
999 */
1000xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001001xmlNewPI(const xmlChar *name, const xmlChar *content) {
Daniel Veillardb96e6431999-08-29 21:02:19 +00001002 xmlNodePtr cur;
1003
1004 if (name == NULL) {
1005 fprintf(stderr, "xmlNewPI : name == NULL\n");
1006 return(NULL);
1007 }
1008
1009 /*
1010 * Allocate a new node and fill the fields.
1011 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00001012 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillardb96e6431999-08-29 21:02:19 +00001013 if (cur == NULL) {
1014 fprintf(stderr, "xmlNewPI : malloc failed\n");
1015 return(NULL);
1016 }
1017
1018 cur->type = XML_PI_NODE;
1019 cur->doc = NULL;
1020 cur->parent = NULL;
1021 cur->next = NULL;
1022 cur->prev = NULL;
1023 cur->childs = NULL;
1024 cur->last = NULL;
1025 cur->properties = NULL;
1026 cur->name = xmlStrdup(name);
1027 cur->ns = NULL;
1028 cur->nsDef = NULL;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001029 if (content != NULL) {
1030#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardb96e6431999-08-29 21:02:19 +00001031 cur->content = xmlStrdup(content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001032#else
1033 cur->content = xmlBufferCreateSize(0);
1034 xmlBufferSetAllocationScheme(cur->content,
1035 xmlGetBufferAllocationScheme());
1036 xmlBufferAdd(cur->content, content, -1);
1037#endif
1038 } else
Daniel Veillardb96e6431999-08-29 21:02:19 +00001039 cur->content = NULL;
1040#ifndef XML_WITHOUT_CORBA
1041 cur->_private = NULL;
1042 cur->vepv = NULL;
1043#endif
1044 return(cur);
1045}
1046
1047/**
Daniel Veillard97b58771998-10-20 06:14:16 +00001048 * xmlNewNode:
1049 * @ns: namespace if any
1050 * @name: the node name
Daniel Veillard97b58771998-10-20 06:14:16 +00001051 *
1052 * Creation of a new node element. @ns and @content are optionnal (NULL).
Daniel Veillardccb09631998-10-27 06:21:04 +00001053 * If content is non NULL, a child list containing the TEXTs and
1054 * ENTITY_REFs node will be created.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001055 * Returns a pointer to the new node object.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001056 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001057xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001058xmlNewNode(xmlNsPtr ns, const xmlChar *name) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001059 xmlNodePtr cur;
1060
1061 if (name == NULL) {
1062 fprintf(stderr, "xmlNewNode : name == NULL\n");
1063 return(NULL);
1064 }
1065
1066 /*
1067 * Allocate a new node and fill the fields.
1068 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00001069 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillard260a68f1998-08-13 03:39:55 +00001070 if (cur == NULL) {
1071 fprintf(stderr, "xmlNewNode : malloc failed\n");
1072 return(NULL);
1073 }
1074
Daniel Veillard33942841998-10-18 19:12:41 +00001075 cur->type = XML_ELEMENT_NODE;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001076 cur->doc = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001077 cur->parent = NULL;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001078 cur->next = NULL;
1079 cur->prev = NULL;
1080 cur->childs = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001081 cur->last = NULL;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001082 cur->properties = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001083 cur->name = xmlStrdup(name);
1084 cur->ns = ns;
1085 cur->nsDef = NULL;
Daniel Veillardccb09631998-10-27 06:21:04 +00001086 cur->content = NULL;
Daniel Veillard27d88741999-05-29 11:51:49 +00001087#ifndef XML_WITHOUT_CORBA
Daniel Veillard27fb0751998-10-17 06:47:46 +00001088 cur->_private = NULL;
1089 cur->vepv = NULL;
1090#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00001091 return(cur);
1092}
1093
Daniel Veillard97b58771998-10-20 06:14:16 +00001094/**
1095 * xmlNewDocNode:
1096 * @doc: the document
1097 * @ns: namespace if any
1098 * @name: the node name
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001099 * @content: the XML text content if any
Daniel Veillard97b58771998-10-20 06:14:16 +00001100 *
1101 * Creation of a new node element within a document. @ns and @content
1102 * are optionnal (NULL).
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001103 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
1104 * references, but XML special chars need to be escaped first by using
1105 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
1106 * need entities support.
1107 *
Daniel Veillard1e346af1999-02-22 10:33:01 +00001108 * Returns a pointer to the new node object.
Daniel Veillard97b58771998-10-20 06:14:16 +00001109 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001110xmlNodePtr
1111xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns,
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001112 const xmlChar *name, const xmlChar *content) {
Daniel Veillard0bef1311998-10-14 02:36:47 +00001113 xmlNodePtr cur;
1114
Daniel Veillardccb09631998-10-27 06:21:04 +00001115 cur = xmlNewNode(ns, name);
1116 if (cur != NULL) {
1117 cur->doc = doc;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001118 if (content != NULL) {
Daniel Veillardccb09631998-10-27 06:21:04 +00001119 cur->childs = xmlStringGetNodeList(doc, content);
Daniel Veillard1e346af1999-02-22 10:33:01 +00001120 UPDATE_LAST_CHILD(cur)
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001121 }
Daniel Veillardccb09631998-10-27 06:21:04 +00001122 }
Daniel Veillard0bef1311998-10-14 02:36:47 +00001123 return(cur);
1124}
1125
1126
Daniel Veillard97b58771998-10-20 06:14:16 +00001127/**
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001128 * xmlNewDocRawNode:
1129 * @doc: the document
1130 * @ns: namespace if any
1131 * @name: the node name
1132 * @content: the text content if any
1133 *
1134 * Creation of a new node element within a document. @ns and @content
1135 * are optionnal (NULL).
1136 *
1137 * Returns a pointer to the new node object.
1138 */
1139xmlNodePtr
1140xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns,
1141 const xmlChar *name, const xmlChar *content) {
1142 xmlNodePtr cur;
1143
1144 cur = xmlNewNode(ns, name);
1145 if (cur != NULL) {
1146 cur->doc = doc;
1147 if (content != NULL) {
1148 cur->childs = xmlNewDocText(doc, content);
1149 UPDATE_LAST_CHILD(cur)
1150 }
1151 }
1152 return(cur);
1153}
1154
1155
1156/**
Daniel Veillard97b58771998-10-20 06:14:16 +00001157 * xmlNewText:
1158 * @content: the text content
1159 *
1160 * Creation of a new text node.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001161 * Returns a pointer to the new node object.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001162 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001163xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001164xmlNewText(const xmlChar *content) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001165 xmlNodePtr cur;
1166
1167 /*
1168 * Allocate a new node and fill the fields.
1169 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00001170 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillard260a68f1998-08-13 03:39:55 +00001171 if (cur == NULL) {
1172 fprintf(stderr, "xmlNewText : malloc failed\n");
1173 return(NULL);
1174 }
1175
Daniel Veillard33942841998-10-18 19:12:41 +00001176 cur->type = XML_TEXT_NODE;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001177 cur->doc = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001178 cur->parent = NULL;
1179 cur->next = NULL;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001180 cur->prev = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001181 cur->childs = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001182 cur->last = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001183 cur->properties = NULL;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001184 cur->type = XML_TEXT_NODE;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001185 cur->name = xmlStrdup(xmlStringText);
1186 cur->ns = NULL;
1187 cur->nsDef = NULL;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001188 if (content != NULL) {
1189#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard260a68f1998-08-13 03:39:55 +00001190 cur->content = xmlStrdup(content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001191#else
1192 cur->content = xmlBufferCreateSize(0);
1193 xmlBufferSetAllocationScheme(cur->content,
1194 xmlGetBufferAllocationScheme());
1195 xmlBufferAdd(cur->content, content, -1);
1196#endif
1197 } else
Daniel Veillard260a68f1998-08-13 03:39:55 +00001198 cur->content = NULL;
Daniel Veillard00fdf371999-10-08 09:40:39 +00001199#ifndef XML_WITHOUT_CORBA
1200 cur->_private = NULL;
1201 cur->vepv = NULL;
1202#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00001203 return(cur);
1204}
1205
Daniel Veillard97b58771998-10-20 06:14:16 +00001206/**
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001207 * xmlNewTextChild:
1208 * @parent: the parent node
1209 * @ns: a namespace if any
1210 * @name: the name of the child
1211 * @content: the text content of the child if any.
1212 *
1213 * Creation of a new child element, added at the end of @parent childs list.
1214 * @ns and @content parameters are optionnal (NULL). If content is non NULL,
1215 * a child TEXT node will be created containing the string content.
1216 *
1217 * Returns a pointer to the new node object.
1218 */
1219xmlNodePtr
1220xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns,
1221 const xmlChar *name, const xmlChar *content) {
1222 xmlNodePtr cur, prev;
1223
1224 if (parent == NULL) {
1225 fprintf(stderr, "xmlNewTextChild : parent == NULL\n");
1226 return(NULL);
1227 }
1228
1229 if (name == NULL) {
1230 fprintf(stderr, "xmlNewTextChild : name == NULL\n");
1231 return(NULL);
1232 }
1233
1234 /*
1235 * Allocate a new node
1236 */
1237 if (ns == NULL)
1238 cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content);
1239 else
1240 cur = xmlNewDocRawNode(parent->doc, ns, name, content);
1241 if (cur == NULL) return(NULL);
1242
1243 /*
1244 * add the new element at the end of the childs list.
1245 */
1246 cur->type = XML_ELEMENT_NODE;
1247 cur->parent = parent;
1248 cur->doc = parent->doc;
1249 if (parent->childs == NULL) {
1250 parent->childs = cur;
1251 parent->last = cur;
1252 } else {
1253 prev = parent->last;
1254 prev->next = cur;
1255 cur->prev = prev;
1256 parent->last = cur;
1257 }
1258
1259 return(cur);
1260}
1261
1262/**
Daniel Veillardccb09631998-10-27 06:21:04 +00001263 * xmlNewReference:
1264 * @doc: the document
1265 * @name: the reference name, or the reference string with & and ;
1266 *
1267 * Creation of a new reference node.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001268 * Returns a pointer to the new node object.
Daniel Veillardccb09631998-10-27 06:21:04 +00001269 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001270xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001271xmlNewReference(xmlDocPtr doc, const xmlChar *name) {
Daniel Veillardccb09631998-10-27 06:21:04 +00001272 xmlNodePtr cur;
1273 xmlEntityPtr ent;
1274
1275 /*
1276 * Allocate a new node and fill the fields.
1277 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00001278 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillardccb09631998-10-27 06:21:04 +00001279 if (cur == NULL) {
1280 fprintf(stderr, "xmlNewText : malloc failed\n");
1281 return(NULL);
1282 }
1283
1284 cur->type = XML_ENTITY_REF_NODE;
Daniel Veillard10c6a8f1998-10-28 01:00:12 +00001285 cur->doc = doc;
Daniel Veillardccb09631998-10-27 06:21:04 +00001286 cur->parent = NULL;
1287 cur->next = NULL;
1288 cur->prev = NULL;
1289 cur->childs = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001290 cur->last = NULL;
Daniel Veillardccb09631998-10-27 06:21:04 +00001291 cur->properties = NULL;
1292 if (name[0] == '&') {
1293 int len;
1294 name++;
1295 len = xmlStrlen(name);
1296 if (name[len - 1] == ';')
1297 cur->name = xmlStrndup(name, len - 1);
1298 else
1299 cur->name = xmlStrndup(name, len);
1300 } else
1301 cur->name = xmlStrdup(name);
1302 cur->ns = NULL;
1303 cur->nsDef = NULL;
1304
1305 ent = xmlGetDocEntity(doc, cur->name);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001306 if (ent != NULL) {
1307#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardccb09631998-10-27 06:21:04 +00001308 cur->content = ent->content;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001309#else
1310 /*
1311 * CJN 11.18.99 this might be a problem, since the xmlBuffer gets
1312 * a copy of this pointer. Let's hope we don't manipulate it
1313 * later
1314 */
1315 cur->content = xmlBufferCreateSize(0);
1316 xmlBufferSetAllocationScheme(cur->content,
1317 xmlGetBufferAllocationScheme());
1318 if (ent->content != NULL)
1319 xmlBufferAdd(cur->content, ent->content, -1);
1320#endif
1321 } else
Daniel Veillardccb09631998-10-27 06:21:04 +00001322 cur->content = NULL;
Daniel Veillard00fdf371999-10-08 09:40:39 +00001323#ifndef XML_WITHOUT_CORBA
1324 cur->_private = NULL;
1325 cur->vepv = NULL;
1326#endif
Daniel Veillardccb09631998-10-27 06:21:04 +00001327 return(cur);
1328}
1329
1330/**
Daniel Veillard97b58771998-10-20 06:14:16 +00001331 * xmlNewDocText:
1332 * @doc: the document
1333 * @content: the text content
1334 *
1335 * Creation of a new text node within a document.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001336 * Returns a pointer to the new node object.
Daniel Veillard97b58771998-10-20 06:14:16 +00001337 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001338xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001339xmlNewDocText(xmlDocPtr doc, const xmlChar *content) {
Daniel Veillard0bef1311998-10-14 02:36:47 +00001340 xmlNodePtr cur;
1341
1342 cur = xmlNewText(content);
1343 if (cur != NULL) cur->doc = doc;
1344 return(cur);
1345}
1346
Daniel Veillard97b58771998-10-20 06:14:16 +00001347/**
Daniel Veillardccb09631998-10-27 06:21:04 +00001348 * xmlNewTextLen:
Daniel Veillard97b58771998-10-20 06:14:16 +00001349 * @content: the text content
1350 * @len: the text len.
1351 *
1352 * Creation of a new text node with an extra parameter for the content's lenght
Daniel Veillard1e346af1999-02-22 10:33:01 +00001353 * Returns a pointer to the new node object.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001354 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001355xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001356xmlNewTextLen(const xmlChar *content, int len) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001357 xmlNodePtr cur;
1358
1359 /*
1360 * Allocate a new node and fill the fields.
1361 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00001362 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillard260a68f1998-08-13 03:39:55 +00001363 if (cur == NULL) {
1364 fprintf(stderr, "xmlNewText : malloc failed\n");
1365 return(NULL);
1366 }
1367
Daniel Veillard33942841998-10-18 19:12:41 +00001368 cur->type = XML_TEXT_NODE;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001369 cur->doc = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001370 cur->parent = NULL;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001371 cur->prev = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001372 cur->next = NULL;
1373 cur->childs = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001374 cur->last = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001375 cur->properties = NULL;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001376 cur->type = XML_TEXT_NODE;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001377 cur->name = xmlStrdup(xmlStringText);
1378 cur->ns = NULL;
1379 cur->nsDef = NULL;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001380 if (content != NULL) {
1381#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard260a68f1998-08-13 03:39:55 +00001382 cur->content = xmlStrndup(content, len);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001383#else
1384 cur->content = xmlBufferCreateSize(len);
1385 xmlBufferSetAllocationScheme(cur->content,
1386 xmlGetBufferAllocationScheme());
1387 xmlBufferAdd(cur->content, content, len);
1388#endif
1389 } else
Daniel Veillard260a68f1998-08-13 03:39:55 +00001390 cur->content = NULL;
Daniel Veillard00fdf371999-10-08 09:40:39 +00001391#ifndef XML_WITHOUT_CORBA
1392 cur->_private = NULL;
1393 cur->vepv = NULL;
1394#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00001395 return(cur);
1396}
1397
Daniel Veillard97b58771998-10-20 06:14:16 +00001398/**
1399 * xmlNewDocTextLen:
1400 * @doc: the document
1401 * @content: the text content
1402 * @len: the text len.
1403 *
1404 * Creation of a new text node with an extra content lenght parameter. The
1405 * text node pertain to a given document.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001406 * Returns a pointer to the new node object.
Daniel Veillard97b58771998-10-20 06:14:16 +00001407 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001408xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001409xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) {
Daniel Veillard0bef1311998-10-14 02:36:47 +00001410 xmlNodePtr cur;
1411
1412 cur = xmlNewTextLen(content, len);
1413 if (cur != NULL) cur->doc = doc;
1414 return(cur);
1415}
1416
Daniel Veillard97b58771998-10-20 06:14:16 +00001417/**
1418 * xmlNewComment:
1419 * @content: the comment content
1420 *
1421 * Creation of a new node containing a comment.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001422 * Returns a pointer to the new node object.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001423 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001424xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001425xmlNewComment(const xmlChar *content) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001426 xmlNodePtr cur;
1427
1428 /*
1429 * Allocate a new node and fill the fields.
1430 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00001431 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillard260a68f1998-08-13 03:39:55 +00001432 if (cur == NULL) {
1433 fprintf(stderr, "xmlNewComment : malloc failed\n");
1434 return(NULL);
1435 }
1436
Daniel Veillard33942841998-10-18 19:12:41 +00001437 cur->type = XML_COMMENT_NODE;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001438 cur->doc = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001439 cur->parent = NULL;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001440 cur->prev = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001441 cur->next = NULL;
1442 cur->childs = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001443 cur->last = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001444 cur->properties = NULL;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001445 cur->type = XML_COMMENT_NODE;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001446 cur->name = xmlStrdup(xmlStringText);
1447 cur->ns = NULL;
1448 cur->nsDef = NULL;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001449 if (content != NULL) {
1450#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard260a68f1998-08-13 03:39:55 +00001451 cur->content = xmlStrdup(content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001452#else
1453 cur->content = xmlBufferCreateSize(0);
1454 xmlBufferSetAllocationScheme(cur->content,
1455 xmlGetBufferAllocationScheme());
1456 xmlBufferAdd(cur->content, content, -1);
1457#endif
1458 } else
Daniel Veillard260a68f1998-08-13 03:39:55 +00001459 cur->content = NULL;
Daniel Veillard00fdf371999-10-08 09:40:39 +00001460#ifndef XML_WITHOUT_CORBA
1461 cur->_private = NULL;
1462 cur->vepv = NULL;
1463#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00001464 return(cur);
1465}
1466
Daniel Veillard97b58771998-10-20 06:14:16 +00001467/**
Daniel Veillardb05deb71999-08-10 19:04:08 +00001468 * xmlNewCDataBlock:
1469 * @doc: the document
1470 * @content: the CData block content content
1471 * @len: the length of the block
1472 *
1473 * Creation of a new node containing a CData block.
1474 * Returns a pointer to the new node object.
1475 */
1476xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001477xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) {
Daniel Veillardb05deb71999-08-10 19:04:08 +00001478 xmlNodePtr cur;
1479
1480 /*
1481 * Allocate a new node and fill the fields.
1482 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00001483 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillardb05deb71999-08-10 19:04:08 +00001484 if (cur == NULL) {
1485 fprintf(stderr, "xmlNewCDataBlock : malloc failed\n");
1486 return(NULL);
1487 }
1488
1489 cur->type = XML_CDATA_SECTION_NODE;
1490 cur->doc = NULL;
1491 cur->parent = NULL;
1492 cur->prev = NULL;
1493 cur->next = NULL;
1494 cur->childs = NULL;
1495 cur->last = NULL;
1496 cur->properties = NULL;
1497 cur->name = xmlStrdup(xmlStringText);
1498 cur->ns = NULL;
1499 cur->nsDef = NULL;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001500 if (content != NULL) {
1501#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardb05deb71999-08-10 19:04:08 +00001502 cur->content = xmlStrndup(content, len);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001503#else
1504 cur->content = xmlBufferCreateSize(len);
1505 xmlBufferSetAllocationScheme(cur->content,
1506 xmlGetBufferAllocationScheme());
1507 xmlBufferAdd(cur->content, content, len);
1508#endif
Daniel Veillardb05deb71999-08-10 19:04:08 +00001509 } else
1510 cur->content = NULL;
Daniel Veillard00fdf371999-10-08 09:40:39 +00001511#ifndef XML_WITHOUT_CORBA
1512 cur->_private = NULL;
1513 cur->vepv = NULL;
1514#endif
Daniel Veillardb05deb71999-08-10 19:04:08 +00001515 return(cur);
1516}
1517
1518/**
Daniel Veillard1e346af1999-02-22 10:33:01 +00001519 * xmlNewDocComment:
Daniel Veillard97b58771998-10-20 06:14:16 +00001520 * @doc: the document
1521 * @content: the comment content
1522 *
1523 * Creation of a new node containing a commentwithin a document.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001524 * Returns a pointer to the new node object.
Daniel Veillard97b58771998-10-20 06:14:16 +00001525 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001526xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001527xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) {
Daniel Veillard0bef1311998-10-14 02:36:47 +00001528 xmlNodePtr cur;
1529
1530 cur = xmlNewComment(content);
1531 if (cur != NULL) cur->doc = doc;
1532 return(cur);
1533}
1534
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001535
Daniel Veillard97b58771998-10-20 06:14:16 +00001536/**
1537 * xmlNewChild:
1538 * @parent: the parent node
1539 * @ns: a namespace if any
1540 * @name: the name of the child
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001541 * @content: the XML content of the child if any.
Daniel Veillard97b58771998-10-20 06:14:16 +00001542 *
Daniel Veillard97b58771998-10-20 06:14:16 +00001543 * Creation of a new child element, added at the end of @parent childs list.
Daniel Veillardccb09631998-10-27 06:21:04 +00001544 * @ns and @content parameters are optionnal (NULL). If content is non NULL,
1545 * a child list containing the TEXTs and ENTITY_REFs node will be created.
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001546 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
1547 * references, but XML special chars need to be escaped first by using
1548 * xmlEncodeEntitiesReentrant(). Use xmlNewTextChild() if entities
1549 * support is not needed.
1550 *
Daniel Veillard1e346af1999-02-22 10:33:01 +00001551 * Returns a pointer to the new node object.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001552 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001553xmlNodePtr
1554xmlNewChild(xmlNodePtr parent, xmlNsPtr ns,
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001555 const xmlChar *name, const xmlChar *content) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001556 xmlNodePtr cur, prev;
1557
1558 if (parent == NULL) {
1559 fprintf(stderr, "xmlNewChild : parent == NULL\n");
1560 return(NULL);
1561 }
1562
1563 if (name == NULL) {
1564 fprintf(stderr, "xmlNewChild : name == NULL\n");
1565 return(NULL);
1566 }
1567
1568 /*
1569 * Allocate a new node
1570 */
1571 if (ns == NULL)
Daniel Veillardccb09631998-10-27 06:21:04 +00001572 cur = xmlNewDocNode(parent->doc, parent->ns, name, content);
Daniel Veillard260a68f1998-08-13 03:39:55 +00001573 else
Daniel Veillardccb09631998-10-27 06:21:04 +00001574 cur = xmlNewDocNode(parent->doc, ns, name, content);
Daniel Veillard260a68f1998-08-13 03:39:55 +00001575 if (cur == NULL) return(NULL);
1576
1577 /*
1578 * add the new element at the end of the childs list.
1579 */
Daniel Veillardccb09631998-10-27 06:21:04 +00001580 cur->type = XML_ELEMENT_NODE;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001581 cur->parent = parent;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001582 cur->doc = parent->doc;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001583 if (parent->childs == NULL) {
1584 parent->childs = cur;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001585 parent->last = cur;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001586 } else {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001587 prev = parent->last;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001588 prev->next = cur;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001589 cur->prev = prev;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001590 parent->last = cur;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001591 }
1592
1593 return(cur);
1594}
1595
Daniel Veillard97b58771998-10-20 06:14:16 +00001596/**
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001597 * xmlAddNextSibling:
1598 * @cur: the child node
1599 * @elem: the new node
1600 *
1601 * Add a new element @elem as the next siblings of @cur
1602 * If the new element was already inserted in a document it is
1603 * first unlinked from its existing context.
1604 *
1605 * Returns the new element or NULL in case of error.
1606 */
1607xmlNodePtr
1608xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) {
1609 if (cur == NULL) {
1610 fprintf(stderr, "xmlAddNextSibling : cur == NULL\n");
1611 return(NULL);
1612 }
1613 if (elem == NULL) {
1614 fprintf(stderr, "xmlAddNextSibling : elem == NULL\n");
1615 return(NULL);
1616 }
1617
1618 xmlUnlinkNode(elem);
1619 elem->doc = cur->doc;
1620 elem->parent = cur->parent;
1621 elem->next = cur;
1622 elem->prev = cur->prev;
1623 cur->prev = elem;
1624 if (elem->prev != NULL)
1625 elem->prev->next = elem;
1626 if ((elem->parent != NULL) && (elem->parent->childs == cur))
1627 elem->parent->childs = elem;
1628 return(elem);
1629}
1630
1631/**
1632 * xmlAddPrevSibling:
1633 * @cur: the child node
1634 * @elem: the new node
1635 *
1636 * Add a new element @elem as the previous siblings of @cur
1637 * If the new element was already inserted in a document it is
1638 * first unlinked from its existing context.
1639 *
1640 * Returns the new element or NULL in case of error.
1641 */
1642xmlNodePtr
1643xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) {
1644 if (cur == NULL) {
1645 fprintf(stderr, "xmlAddPrevSibling : cur == NULL\n");
1646 return(NULL);
1647 }
1648 if (elem == NULL) {
1649 fprintf(stderr, "xmlAddPrevSibling : elem == NULL\n");
1650 return(NULL);
1651 }
1652
1653 xmlUnlinkNode(elem);
1654 elem->doc = cur->doc;
1655 elem->parent = cur->parent;
1656 elem->prev = cur;
1657 elem->next = cur->next;
1658 cur->next = elem;
1659 if (elem->next != NULL)
1660 elem->next->prev = elem;
1661 if ((elem->parent != NULL) && (elem->parent->last == cur))
1662 elem->parent->last = elem;
1663 return(elem);
1664}
1665
1666/**
Daniel Veillardb96e6431999-08-29 21:02:19 +00001667 * xmlAddSibling:
1668 * @cur: the child node
1669 * @elem: the new node
1670 *
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001671 * Add a new element @elem to the list of siblings of @cur
1672 * If the new element was already inserted in a document it is
1673 * first unlinked from its existing context.
1674 *
1675 * Returns the new element or NULL in case of error.
Daniel Veillardb96e6431999-08-29 21:02:19 +00001676 */
1677xmlNodePtr
1678xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) {
1679 xmlNodePtr parent;
1680
1681 if (cur == NULL) {
1682 fprintf(stderr, "xmlAddSibling : cur == NULL\n");
1683 return(NULL);
1684 }
1685
1686 if (elem == NULL) {
1687 fprintf(stderr, "xmlAddSibling : elem == NULL\n");
1688 return(NULL);
1689 }
1690
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001691 /*
1692 * Constant time is we can rely on the ->parent->last to find
1693 * the last sibling.
1694 */
1695 if ((cur->parent != NULL) &&
1696 (cur->parent->childs != NULL) &&
1697 (cur->parent->last != NULL) &&
1698 (cur->parent->last->next == NULL)) {
1699 cur = cur->parent->last;
1700 } else {
1701 while (cur->next != NULL) cur = cur->next;
Daniel Veillardb96e6431999-08-29 21:02:19 +00001702 }
1703
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001704 xmlUnlinkNode(elem);
Daniel Veillardb96e6431999-08-29 21:02:19 +00001705 if (elem->doc == NULL)
1706 elem->doc = cur->doc; /* the parent may not be linked to a doc ! */
1707
1708 parent = cur->parent;
1709 elem->prev = cur;
1710 elem->next = NULL;
1711 elem->parent = parent;
1712 cur->next = elem;
1713 if (parent != NULL)
1714 parent->last = elem;
1715
1716 return(elem);
1717}
1718
1719/**
Daniel Veillard97b58771998-10-20 06:14:16 +00001720 * xmlAddChild:
1721 * @parent: the parent node
1722 * @cur: the child node
1723 *
1724 * Add a new child element, to @parent, at the end of the child list.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001725 * Returns the child or NULL in case of error.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001726 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001727xmlNodePtr
1728xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001729 xmlNodePtr prev;
1730
1731 if (parent == NULL) {
Daniel Veillard10a2c651999-12-12 13:03:50 +00001732 fprintf(stderr, "xmlAddChild : parent == NULL\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +00001733 return(NULL);
1734 }
1735
1736 if (cur == NULL) {
Daniel Veillard10a2c651999-12-12 13:03:50 +00001737 fprintf(stderr, "xmlAddChild : child == NULL\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +00001738 return(NULL);
1739 }
1740
Daniel Veillard0bef1311998-10-14 02:36:47 +00001741 if ((cur->doc != NULL) && (parent->doc != NULL) &&
1742 (cur->doc != parent->doc)) {
1743 fprintf(stderr, "Elements moved to a different document\n");
1744 }
1745
Daniel Veillard260a68f1998-08-13 03:39:55 +00001746 /*
1747 * add the new element at the end of the childs list.
1748 */
1749 cur->parent = parent;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001750 cur->doc = parent->doc; /* the parent may not be linked to a doc ! */
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001751
Daniel Veillardccb09631998-10-27 06:21:04 +00001752 /*
1753 * Handle the case where parent->content != NULL, in that case it will
1754 * create a intermediate TEXT node.
1755 */
1756 if (parent->content != NULL) {
1757 xmlNodePtr text;
1758
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001759#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardccb09631998-10-27 06:21:04 +00001760 text = xmlNewDocText(parent->doc, parent->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001761#else
1762 text = xmlNewDocText(parent->doc, xmlBufferContent(parent->content));
1763#endif
Daniel Veillardccb09631998-10-27 06:21:04 +00001764 if (text != NULL) {
1765 text->next = parent->childs;
1766 if (text->next != NULL)
1767 text->next->prev = text;
1768 parent->childs = text;
Daniel Veillard1e346af1999-02-22 10:33:01 +00001769 UPDATE_LAST_CHILD(parent)
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001770#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard6454aec1999-09-02 22:04:43 +00001771 xmlFree(parent->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001772#else
1773 xmlBufferFree(parent->content);
1774#endif
Daniel Veillardccb09631998-10-27 06:21:04 +00001775 parent->content = NULL;
1776 }
1777 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00001778 if (parent->childs == NULL) {
1779 parent->childs = cur;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001780 parent->last = cur;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001781 } else {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001782 prev = parent->last;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001783 prev->next = cur;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001784 cur->prev = prev;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001785 parent->last = cur;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001786 }
1787
1788 return(cur);
1789}
1790
Daniel Veillard97b58771998-10-20 06:14:16 +00001791/**
1792 * xmlGetLastChild:
1793 * @parent: the parent node
1794 *
1795 * Search the last child of a node.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001796 * Returns the last child or NULL if none.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001797 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001798xmlNodePtr
1799xmlGetLastChild(xmlNodePtr parent) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001800 if (parent == NULL) {
1801 fprintf(stderr, "xmlGetLastChild : parent == NULL\n");
1802 return(NULL);
1803 }
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001804 return(parent->last);
Daniel Veillard260a68f1998-08-13 03:39:55 +00001805}
1806
Daniel Veillard97b58771998-10-20 06:14:16 +00001807/**
1808 * xmlFreeNodeList:
1809 * @cur: the first node in the list
1810 *
1811 * Free a node and all its siblings, this is a recursive behaviour, all
1812 * the childs are freed too.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001813 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001814void
1815xmlFreeNodeList(xmlNodePtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001816 xmlNodePtr next;
1817 if (cur == NULL) {
1818 fprintf(stderr, "xmlFreeNodeList : node == NULL\n");
1819 return;
1820 }
1821 while (cur != NULL) {
1822 next = cur->next;
1823 xmlFreeNode(cur);
1824 cur = next;
1825 }
1826}
1827
Daniel Veillard97b58771998-10-20 06:14:16 +00001828/**
1829 * xmlFreeNode:
1830 * @cur: the node
1831 *
1832 * Free a node, this is a recursive behaviour, all the childs are freed too.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001833 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001834void
1835xmlFreeNode(xmlNodePtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001836 if (cur == NULL) {
1837 fprintf(stderr, "xmlFreeNode : node == NULL\n");
1838 return;
1839 }
Daniel Veillardccb09631998-10-27 06:21:04 +00001840 cur->doc = NULL;
1841 cur->parent = NULL;
1842 cur->next = NULL;
1843 cur->prev = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001844 if (cur->childs != NULL) xmlFreeNodeList(cur->childs);
Daniel Veillardccb09631998-10-27 06:21:04 +00001845 if (cur->properties != NULL) xmlFreePropList(cur->properties);
1846 if (cur->type != XML_ENTITY_REF_NODE)
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001847#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard6454aec1999-09-02 22:04:43 +00001848 if (cur->content != NULL) xmlFree(cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001849#else
1850 if (cur->content != NULL) xmlBufferFree(cur->content);
1851#endif
Daniel Veillard6454aec1999-09-02 22:04:43 +00001852 if (cur->name != NULL) xmlFree((char *) cur->name);
Daniel Veillard260a68f1998-08-13 03:39:55 +00001853 if (cur->nsDef != NULL) xmlFreeNsList(cur->nsDef);
1854 memset(cur, -1, sizeof(xmlNode));
Daniel Veillard6454aec1999-09-02 22:04:43 +00001855 xmlFree(cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +00001856}
1857
Daniel Veillard16253641998-10-28 22:58:05 +00001858/**
1859 * xmlUnlinkNode:
1860 * @cur: the node
1861 *
1862 * Unlink a node from it's current context, the node is not freed
1863 */
1864void
1865xmlUnlinkNode(xmlNodePtr cur) {
1866 if (cur == NULL) {
1867 fprintf(stderr, "xmlUnlinkNode : node == NULL\n");
1868 return;
1869 }
1870 if ((cur->parent != NULL) && (cur->parent->childs == cur))
1871 cur->parent->childs = cur->next;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001872 if ((cur->parent != NULL) && (cur->parent->last == cur))
1873 cur->parent->last = cur->prev;
Daniel Veillard16253641998-10-28 22:58:05 +00001874 if (cur->next != NULL)
1875 cur->next->prev = cur->prev;
1876 if (cur->prev != NULL)
1877 cur->prev->next = cur->next;
1878 cur->next = cur->prev = NULL;
1879 cur->parent = NULL;
1880}
1881
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001882/**
1883 * xmlReplaceNode:
1884 * @old: the old node
1885 * @cur: the node
1886 *
1887 * Unlink the old node from it's current context, prune the new one
1888 * at the same place. If cur was already inserted in a document it is
1889 * first unlinked from its existing context.
1890 *
1891 * Returns the old node
1892 */
1893xmlNodePtr
1894xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) {
1895 if (old == NULL) {
1896 fprintf(stderr, "xmlReplaceNode : old == NULL\n");
1897 return(NULL);
1898 }
1899 if (cur == NULL) {
1900 xmlUnlinkNode(old);
1901 return(old);
1902 }
1903 xmlUnlinkNode(cur);
1904 cur->doc = old->doc;
1905 cur->parent = old->parent;
1906 cur->next = old->next;
1907 if (cur->next != NULL)
1908 cur->next->prev = cur;
1909 cur->prev = old->prev;
1910 if (cur->prev != NULL)
1911 cur->prev->next = cur;
1912 if (cur->parent != NULL) {
1913 if (cur->parent->childs == old)
1914 cur->parent->childs = cur;
1915 if (cur->parent->last == old)
1916 cur->parent->last = cur;
1917 }
1918 old->next = old->prev = NULL;
1919 old->parent = NULL;
1920 return(old);
1921}
1922
Daniel Veillard260a68f1998-08-13 03:39:55 +00001923/************************************************************************
1924 * *
Daniel Veillardbe36afe1998-11-27 06:39:50 +00001925 * Copy operations *
1926 * *
1927 ************************************************************************/
1928
1929/**
1930 * xmlCopyNamespace:
1931 * @cur: the namespace
1932 *
1933 * Do a copy of the namespace.
1934 *
1935 * Returns: a new xmlNsPtr, or NULL in case of error.
1936 */
1937xmlNsPtr
1938xmlCopyNamespace(xmlNsPtr cur) {
1939 xmlNsPtr ret;
1940
1941 if (cur == NULL) return(NULL);
1942 switch (cur->type) {
1943 case XML_GLOBAL_NAMESPACE:
1944 ret = xmlNewGlobalNs(NULL, cur->href, cur->prefix);
1945 break;
1946 case XML_LOCAL_NAMESPACE:
1947 ret = xmlNewNs(NULL, cur->href, cur->prefix);
1948 break;
1949 default:
1950 fprintf(stderr, "xmlCopyNamespace: unknown type %d\n", cur->type);
1951 return(NULL);
1952 }
1953 return(ret);
1954}
1955
1956/**
1957 * xmlCopyNamespaceList:
1958 * @cur: the first namespace
1959 *
1960 * Do a copy of an namespace list.
1961 *
1962 * Returns: a new xmlNsPtr, or NULL in case of error.
1963 */
1964xmlNsPtr
1965xmlCopyNamespaceList(xmlNsPtr cur) {
1966 xmlNsPtr ret = NULL;
1967 xmlNsPtr p = NULL,q;
1968
1969 while (cur != NULL) {
1970 q = xmlCopyNamespace(cur);
1971 if (p == NULL) {
1972 ret = p = q;
1973 } else {
1974 p->next = q;
1975 p = q;
1976 }
1977 cur = cur->next;
1978 }
1979 return(ret);
1980}
1981
1982/**
1983 * xmlCopyProp:
Daniel Veillardb96e6431999-08-29 21:02:19 +00001984 * @target: the element where the attribute will be grafted
Daniel Veillardbe36afe1998-11-27 06:39:50 +00001985 * @cur: the attribute
1986 *
1987 * Do a copy of the attribute.
1988 *
1989 * Returns: a new xmlAttrPtr, or NULL in case of error.
1990 */
1991xmlAttrPtr
Daniel Veillardb96e6431999-08-29 21:02:19 +00001992xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +00001993 xmlAttrPtr ret;
1994
1995 if (cur == NULL) return(NULL);
1996 if (cur->val != NULL)
1997 ret = xmlNewDocProp(cur->val->doc, cur->name, NULL);
1998 else
1999 ret = xmlNewDocProp(NULL, cur->name, NULL);
2000 if (ret == NULL) return(NULL);
Daniel Veillardb96e6431999-08-29 21:02:19 +00002001
2002 if ((cur->ns != NULL) && (target != NULL)) {
2003 xmlNsPtr ns;
2004
2005 ns = xmlSearchNs(target->doc, target, cur->ns->prefix);
2006 ret->ns = ns;
2007 } else
2008 ret->ns = NULL;
2009
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002010 if (cur->val != NULL)
2011 ret->val = xmlCopyNodeList(cur->val);
2012 return(ret);
2013}
2014
2015/**
2016 * xmlCopyPropList:
Daniel Veillardb96e6431999-08-29 21:02:19 +00002017 * @target: the element where the attributes will be grafted
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002018 * @cur: the first attribute
2019 *
2020 * Do a copy of an attribute list.
2021 *
2022 * Returns: a new xmlAttrPtr, or NULL in case of error.
2023 */
2024xmlAttrPtr
Daniel Veillardb96e6431999-08-29 21:02:19 +00002025xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002026 xmlAttrPtr ret = NULL;
2027 xmlAttrPtr p = NULL,q;
2028
2029 while (cur != NULL) {
Daniel Veillardb96e6431999-08-29 21:02:19 +00002030 q = xmlCopyProp(target, cur);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002031 if (p == NULL) {
2032 ret = p = q;
2033 } else {
2034 p->next = q;
2035 p = q;
2036 }
2037 cur = cur->next;
2038 }
2039 return(ret);
2040}
2041
2042/*
Daniel Veillard11a48ec1999-11-23 10:40:46 +00002043 * NOTE abeut the CopyNode operations !
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002044 *
2045 * They are splitted into external and internal parts for one
2046 * tricky reason: namespaces. Doing a direct copy of a node
2047 * say RPM:Copyright without changing the namespace pointer to
2048 * something else can produce stale links. One way to do it is
2049 * to keep a reference counter but this doesn't work as soon
2050 * as one move the element or the subtree out of the scope of
2051 * the existing namespace. The actual solution seems to add
2052 * a copy of the namespace at the top of the copied tree if
2053 * not available in the subtree.
2054 * Hence two functions, the public front-end call the inner ones
2055 */
2056
2057static xmlNodePtr
2058xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent);
2059
2060static xmlNodePtr
2061xmlStaticCopyNode(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent,
2062 int recursive) {
2063 xmlNodePtr ret;
2064
2065 if (node == NULL) return(NULL);
2066 /*
2067 * Allocate a new node and fill the fields.
2068 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00002069 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002070 if (ret == NULL) {
2071 fprintf(stderr, "xmlStaticCopyNode : malloc failed\n");
2072 return(NULL);
2073 }
2074
2075 ret->type = node->type;
2076 ret->doc = doc;
2077 ret->parent = parent;
2078 ret->next = NULL;
2079 ret->prev = NULL;
2080 ret->childs = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002081 ret->last = NULL;
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002082 ret->properties = NULL;
2083 if (node->name != NULL)
2084 ret->name = xmlStrdup(node->name);
2085 else
2086 ret->name = NULL;
2087 ret->ns = NULL;
2088 ret->nsDef = NULL;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002089 if ((node->content != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
2090#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002091 ret->content = xmlStrdup(node->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002092#else
2093 ret->content = xmlBufferCreateSize(xmlBufferLength(node->content));
2094 xmlBufferSetAllocationScheme(ret->content,
2095 xmlGetBufferAllocationScheme());
2096 xmlBufferAdd(ret->content,
2097 xmlBufferContent(node->content),
2098 xmlBufferLength(node->content));
2099#endif
2100 } else
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002101 ret->content = NULL;
Daniel Veillard27d88741999-05-29 11:51:49 +00002102#ifndef XML_WITHOUT_CORBA
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002103 ret->_private = NULL;
2104 ret->vepv = NULL;
2105#endif
2106 if (parent != NULL)
2107 xmlAddChild(parent, ret);
2108
2109 if (!recursive) return(ret);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002110 if (node->nsDef != NULL)
2111 ret->nsDef = xmlCopyNamespaceList(node->nsDef);
2112
2113 if (node->ns != NULL) {
2114 xmlNsPtr ns;
2115
2116 ns = xmlSearchNs(doc, ret, node->ns->prefix);
2117 if (ns == NULL) {
2118 /*
2119 * Humm, we are copying an element whose namespace is defined
2120 * out of the new tree scope. Search it in the original tree
2121 * and add it at the top of the new tree
2122 */
2123 ns = xmlSearchNs(node->doc, node, node->ns->prefix);
2124 if (ns != NULL) {
2125 xmlNodePtr root = ret;
2126
2127 while (root->parent != NULL) root = root->parent;
2128 xmlNewNs(root, ns->href, ns->prefix);
2129 }
2130 } else {
2131 /*
2132 * reference the existing namespace definition in our own tree.
2133 */
2134 ret->ns = ns;
2135 }
2136 }
Daniel Veillardb96e6431999-08-29 21:02:19 +00002137 if (node->properties != NULL)
2138 ret->properties = xmlCopyPropList(ret, node->properties);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002139 if (node->childs != NULL)
2140 ret->childs = xmlStaticCopyNodeList(node->childs, doc, ret);
Daniel Veillard1e346af1999-02-22 10:33:01 +00002141 UPDATE_LAST_CHILD(ret)
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002142 return(ret);
2143}
2144
2145static xmlNodePtr
2146xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) {
2147 xmlNodePtr ret = NULL;
2148 xmlNodePtr p = NULL,q;
2149
2150 while (node != NULL) {
2151 q = xmlStaticCopyNode(node, doc, parent, 1);
2152 if (parent == NULL) {
2153 if (ret == NULL) ret = q;
2154 } else {
2155 if (ret == NULL) {
2156 q->prev = NULL;
2157 ret = p = q;
2158 } else {
2159 p->next = q;
2160 q->prev = p;
2161 p = q;
2162 }
2163 }
2164 node = node->next;
2165 }
2166 return(ret);
2167}
2168
2169/**
2170 * xmlCopyNode:
2171 * @node: the node
2172 * @recursive: if 1 do a recursive copy.
2173 *
2174 * Do a copy of the node.
2175 *
2176 * Returns: a new xmlNodePtr, or NULL in case of error.
2177 */
2178xmlNodePtr
2179xmlCopyNode(xmlNodePtr node, int recursive) {
2180 xmlNodePtr ret;
2181
2182 ret = xmlStaticCopyNode(node, NULL, NULL, recursive);
2183 return(ret);
2184}
2185
2186/**
2187 * xmlCopyNodeList:
2188 * @node: the first node in the list.
2189 *
2190 * Do a recursive copy of the node list.
2191 *
2192 * Returns: a new xmlNodePtr, or NULL in case of error.
2193 */
2194xmlNodePtr xmlCopyNodeList(xmlNodePtr node) {
2195 xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL);
2196 return(ret);
2197}
2198
2199/**
2200 * xmlCopyElement:
2201 * @elem: the element
2202 *
2203 * Do a copy of the element definition.
2204 *
2205 * Returns: a new xmlElementPtr, or NULL in case of error.
2206xmlElementPtr
2207xmlCopyElement(xmlElementPtr elem) {
2208 xmlElementPtr ret;
2209
2210 if (elem == NULL) return(NULL);
2211 ret = xmlNewDocElement(elem->doc, elem->ns, elem->name, elem->content);
2212 if (ret == NULL) return(NULL);
2213 if (!recursive) return(ret);
2214 if (elem->properties != NULL)
2215 ret->properties = xmlCopyPropList(elem->properties);
2216
2217 if (elem->nsDef != NULL)
2218 ret->nsDef = xmlCopyNamespaceList(elem->nsDef);
2219 if (elem->childs != NULL)
2220 ret->childs = xmlCopyElementList(elem->childs);
2221 return(ret);
2222}
2223 */
2224
2225/**
2226 * xmlCopyDtd:
2227 * @dtd: the dtd
2228 *
2229 * Do a copy of the dtd.
2230 *
2231 * Returns: a new xmlDtdPtr, or NULL in case of error.
2232 */
2233xmlDtdPtr
2234xmlCopyDtd(xmlDtdPtr dtd) {
2235 xmlDtdPtr ret;
2236
2237 if (dtd == NULL) return(NULL);
2238 ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID);
2239 if (ret == NULL) return(NULL);
2240 if (dtd->entities != NULL)
2241 ret->entities = (void *) xmlCopyEntitiesTable(
2242 (xmlEntitiesTablePtr) dtd->entities);
Daniel Veillard1e346af1999-02-22 10:33:01 +00002243 if (dtd->notations != NULL)
2244 ret->notations = (void *) xmlCopyNotationTable(
2245 (xmlNotationTablePtr) dtd->notations);
2246 if (dtd->elements != NULL)
2247 ret->elements = (void *) xmlCopyElementTable(
2248 (xmlElementTablePtr) dtd->elements);
2249 if (dtd->attributes != NULL)
2250 ret->attributes = (void *) xmlCopyAttributeTable(
2251 (xmlAttributeTablePtr) dtd->attributes);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002252 return(ret);
2253}
2254
2255/**
2256 * xmlCopyDoc:
2257 * @doc: the document
2258 * @recursive: if 1 do a recursive copy.
2259 *
2260 * Do a copy of the document info. If recursive, the content tree will
2261 * be copied too as well as Dtd, namespaces and entities.
2262 *
2263 * Returns: a new xmlDocPtr, or NULL in case of error.
2264 */
2265xmlDocPtr
2266xmlCopyDoc(xmlDocPtr doc, int recursive) {
2267 xmlDocPtr ret;
2268
2269 if (doc == NULL) return(NULL);
2270 ret = xmlNewDoc(doc->version);
2271 if (ret == NULL) return(NULL);
2272 if (doc->name != NULL)
Daniel Veillard6454aec1999-09-02 22:04:43 +00002273 ret->name = xmlMemStrdup(doc->name);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002274 if (doc->encoding != NULL)
2275 ret->encoding = xmlStrdup(doc->encoding);
2276 ret->compression = doc->compression;
2277 ret->standalone = doc->standalone;
2278 if (!recursive) return(ret);
2279
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002280 if (doc->intSubset != NULL)
2281 ret->intSubset = xmlCopyDtd(doc->intSubset);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002282 if (doc->oldNs != NULL)
2283 ret->oldNs = xmlCopyNamespaceList(doc->oldNs);
2284 if (doc->root != NULL)
2285 ret->root = xmlStaticCopyNodeList(doc->root, ret, NULL);
2286 return(ret);
2287}
2288
2289/************************************************************************
2290 * *
Daniel Veillard260a68f1998-08-13 03:39:55 +00002291 * Content access functions *
2292 * *
2293 ************************************************************************/
2294
Daniel Veillard97b58771998-10-20 06:14:16 +00002295/**
Daniel Veillard944b5ff1999-12-15 19:08:24 +00002296 * xmlDocGetRootElement:
2297 * @doc: the document
2298 *
2299 * Get the root element of the document (doc->root is a list
2300 * containing possibly comments, PIs, etc ...).
2301 *
2302 * Returns the xmlNodePtr for the root or NULL
2303 */
2304xmlNodePtr
2305xmlDocGetRootElement(xmlDocPtr doc) {
2306 xmlNodePtr ret;
2307
2308 if (doc == NULL) return(NULL);
2309 ret = doc->root;
2310 while (ret != NULL) {
2311 if (ret->type == XML_ELEMENT_NODE)
2312 return(ret);
2313 ret = ret->next;
2314 }
2315 return(ret);
2316}
2317
2318/**
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002319 * xmlDocSetRootElement:
2320 * @doc: the document
2321 * @root: the new document root element
2322 *
2323 * Set the root element of the document (doc->root is a list
2324 * containing possibly comments, PIs, etc ...).
2325 *
2326 * Returns the old root element if any was found
2327 */
2328xmlNodePtr
2329xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) {
2330 xmlNodePtr old = NULL;
2331
2332 if (doc == NULL) return(NULL);
2333 old = doc->root;
2334 while (old != NULL) {
2335 if (old->type == XML_ELEMENT_NODE)
2336 break;
2337 old = old->next;
2338 }
2339 if (old == NULL) {
2340 if (doc->root == NULL) {
2341 doc->root = root;
2342 } else {
2343 xmlAddSibling(doc->root, root);
2344 }
2345 } else {
2346 xmlReplaceNode(old, root);
2347 }
2348 return(old);
2349}
2350
2351/**
Daniel Veillardb96e6431999-08-29 21:02:19 +00002352 * xmlNodeSetLang:
2353 * @cur: the node being changed
2354 * @lang: the langage description
2355 *
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002356 * Set the language of a node, i.e. the values of the xml:lang
2357 * attribute.
Daniel Veillardb96e6431999-08-29 21:02:19 +00002358 */
2359void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002360xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) {
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002361 if (cur == NULL) return;
2362 switch(cur->type) {
2363 case XML_TEXT_NODE:
2364 case XML_CDATA_SECTION_NODE:
2365 case XML_COMMENT_NODE:
2366 case XML_DOCUMENT_NODE:
2367 case XML_DOCUMENT_TYPE_NODE:
2368 case XML_DOCUMENT_FRAG_NODE:
2369 case XML_NOTATION_NODE:
2370 case XML_HTML_DOCUMENT_NODE:
2371 return;
2372 case XML_ELEMENT_NODE:
2373 case XML_ATTRIBUTE_NODE:
2374 case XML_PI_NODE:
2375 case XML_ENTITY_REF_NODE:
2376 case XML_ENTITY_NODE:
2377 break;
2378 }
Daniel Veillardb96e6431999-08-29 21:02:19 +00002379 xmlSetProp(cur, BAD_CAST "xml:lang", lang);
2380}
2381
2382/**
2383 * xmlNodeGetLang:
2384 * @cur: the node being checked
2385 *
2386 * Searches the language of a node, i.e. the values of the xml:lang
2387 * attribute or the one carried by the nearest ancestor.
2388 *
2389 * Returns a pointer to the lang value, or NULL if not found
Daniel Veillarda819dac1999-11-24 18:04:22 +00002390 * It's up to the caller to free the memory.
Daniel Veillardb96e6431999-08-29 21:02:19 +00002391 */
Daniel Veillarda819dac1999-11-24 18:04:22 +00002392xmlChar *
Daniel Veillardb96e6431999-08-29 21:02:19 +00002393xmlNodeGetLang(xmlNodePtr cur) {
Daniel Veillarda819dac1999-11-24 18:04:22 +00002394 xmlChar *lang;
Daniel Veillardb96e6431999-08-29 21:02:19 +00002395
2396 while (cur != NULL) {
2397 lang = xmlGetProp(cur, BAD_CAST "xml:lang");
2398 if (lang != NULL)
2399 return(lang);
2400 cur = cur->parent;
2401 }
2402 return(NULL);
2403}
2404
2405/**
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002406 * xmlNodeSetName:
2407 * @cur: the node being changed
2408 * @name: the new tag name
2409 *
2410 * Searches the language of a node, i.e. the values of the xml:lang
2411 * attribute or the one carried by the nearest ancestor.
2412 */
2413void
2414xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) {
2415 if (cur == NULL) return;
2416 if (name == NULL) return;
2417 switch(cur->type) {
2418 case XML_TEXT_NODE:
2419 case XML_CDATA_SECTION_NODE:
2420 case XML_COMMENT_NODE:
2421 case XML_DOCUMENT_NODE:
2422 case XML_DOCUMENT_TYPE_NODE:
2423 case XML_DOCUMENT_FRAG_NODE:
2424 case XML_NOTATION_NODE:
2425 case XML_HTML_DOCUMENT_NODE:
2426 return;
2427 case XML_ELEMENT_NODE:
2428 case XML_ATTRIBUTE_NODE:
2429 case XML_PI_NODE:
2430 case XML_ENTITY_REF_NODE:
2431 case XML_ENTITY_NODE:
2432 break;
2433 }
2434 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
2435 cur->name = xmlStrdup(name);
2436}
2437
2438/**
Daniel Veillard10a2c651999-12-12 13:03:50 +00002439 * xmlNodeGetBase:
2440 * @doc: the document the node pertains to
2441 * @cur: the node being checked
2442 *
2443 * Searches for the BASE URL. The code should work on both XML
2444 * and HTML document even if base mechanisms are completely different.
2445 *
2446 * Returns a pointer to the base URL, or NULL if not found
2447 * It's up to the caller to free the memory.
2448 */
2449xmlChar *
2450xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) {
2451 xmlChar *base;
2452
2453 if ((cur == NULL) && (doc == NULL))
2454 return(NULL);
2455 if (doc == NULL) doc = cur->doc;
2456 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
2457 cur = doc->root;
2458 while ((cur != NULL) && (cur->name != NULL)) {
2459 if (cur->type != XML_ELEMENT_NODE) {
2460 cur = cur->next;
2461 continue;
2462 }
2463 if ((!xmlStrcmp(cur->name, BAD_CAST "html")) ||
2464 (!xmlStrcmp(cur->name, BAD_CAST "HTML"))) {
2465 cur = cur->childs;
2466 continue;
2467 }
2468 if ((!xmlStrcmp(cur->name, BAD_CAST "head")) ||
2469 (!xmlStrcmp(cur->name, BAD_CAST "HEAD"))) {
2470 cur = cur->childs;
2471 continue;
2472 }
2473 if ((!xmlStrcmp(cur->name, BAD_CAST "base")) ||
2474 (!xmlStrcmp(cur->name, BAD_CAST "BASE"))) {
2475 base = xmlGetProp(cur, BAD_CAST "href");
2476 if (base != NULL) return(base);
2477 return(xmlGetProp(cur, BAD_CAST "HREF"));
2478 }
2479 }
2480 return(NULL);
2481 }
2482 while (cur != NULL) {
2483 base = xmlGetProp(cur, BAD_CAST "xml:base");
2484 if (base != NULL)
2485 return(base);
2486 cur = cur->parent;
2487 }
2488 return(NULL);
2489}
2490
2491/**
Daniel Veillard16253641998-10-28 22:58:05 +00002492 * xmlNodeGetContent:
2493 * @cur: the node being read
2494 *
2495 * Read the value of a node, this can be either the text carried
2496 * directly by this node if it's a TEXT node or the aggregate string
2497 * of the values carried by this node child's (TEXT and ENTITY_REF).
2498 * Entity references are substitued.
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002499 * Returns a new xmlChar * or NULL if no content is available.
Daniel Veillard5099ae81999-04-21 20:12:07 +00002500 * It's up to the caller to free the memory.
Daniel Veillard16253641998-10-28 22:58:05 +00002501 */
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002502xmlChar *
Daniel Veillard16253641998-10-28 22:58:05 +00002503xmlNodeGetContent(xmlNodePtr cur) {
2504 if (cur == NULL) return(NULL);
2505 switch (cur->type) {
2506 case XML_DOCUMENT_FRAG_NODE:
2507 case XML_ELEMENT_NODE:
2508 return(xmlNodeListGetString(cur->doc, cur->childs, 1));
2509 break;
Daniel Veillardb96e6431999-08-29 21:02:19 +00002510 case XML_ATTRIBUTE_NODE: {
2511 xmlAttrPtr attr = (xmlAttrPtr) cur;
2512 if (attr->node != NULL)
2513 return(xmlNodeListGetString(attr->node->doc, attr->val, 1));
2514 else
2515 return(xmlNodeListGetString(NULL, attr->val, 1));
2516 break;
2517 }
Daniel Veillarddbfd6411999-12-28 16:35:14 +00002518 case XML_COMMENT_NODE:
Daniel Veillardb96e6431999-08-29 21:02:19 +00002519 case XML_PI_NODE:
2520 if (cur->content != NULL)
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002521#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardb96e6431999-08-29 21:02:19 +00002522 return(xmlStrdup(cur->content));
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002523#else
2524 return(xmlStrdup(xmlBufferContent(cur->content)));
2525#endif
Daniel Veillardb96e6431999-08-29 21:02:19 +00002526 return(NULL);
Daniel Veillard16253641998-10-28 22:58:05 +00002527 case XML_ENTITY_REF_NODE:
Daniel Veillarddbfd6411999-12-28 16:35:14 +00002528 /*
2529 * Locate the entity, and get it's content
2530 * @@@
2531 */
2532 return(NULL);
Daniel Veillard16253641998-10-28 22:58:05 +00002533 case XML_ENTITY_NODE:
Daniel Veillard16253641998-10-28 22:58:05 +00002534 case XML_DOCUMENT_NODE:
Daniel Veillard7c1206f1999-10-14 09:10:25 +00002535 case XML_HTML_DOCUMENT_NODE:
Daniel Veillard16253641998-10-28 22:58:05 +00002536 case XML_DOCUMENT_TYPE_NODE:
2537 case XML_NOTATION_NODE:
2538 return(NULL);
Daniel Veillardb05deb71999-08-10 19:04:08 +00002539 case XML_CDATA_SECTION_NODE:
Daniel Veillard16253641998-10-28 22:58:05 +00002540 case XML_TEXT_NODE:
2541 if (cur->content != NULL)
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002542#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard16253641998-10-28 22:58:05 +00002543 return(xmlStrdup(cur->content));
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002544#else
2545 return(xmlStrdup(xmlBufferContent(cur->content)));
2546#endif
Daniel Veillard16253641998-10-28 22:58:05 +00002547 return(NULL);
2548 }
2549 return(NULL);
2550}
2551
2552/**
Daniel Veillard97b58771998-10-20 06:14:16 +00002553 * xmlNodeSetContent:
2554 * @cur: the node being modified
2555 * @content: the new value of the content
2556 *
2557 * Replace the content of a node.
Daniel Veillard260a68f1998-08-13 03:39:55 +00002558 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00002559void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002560xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00002561 if (cur == NULL) {
2562 fprintf(stderr, "xmlNodeSetContent : node == NULL\n");
2563 return;
2564 }
Daniel Veillard16253641998-10-28 22:58:05 +00002565 switch (cur->type) {
2566 case XML_DOCUMENT_FRAG_NODE:
2567 case XML_ELEMENT_NODE:
2568 if (cur->content != NULL) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002569#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard6454aec1999-09-02 22:04:43 +00002570 xmlFree(cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002571#else
2572 xmlBufferFree(cur->content);
2573#endif
Daniel Veillard16253641998-10-28 22:58:05 +00002574 cur->content = NULL;
2575 }
Daniel Veillard6454aec1999-09-02 22:04:43 +00002576 if (cur->childs != NULL) xmlFreeNodeList(cur->childs);
Daniel Veillard16253641998-10-28 22:58:05 +00002577 cur->childs = xmlStringGetNodeList(cur->doc, content);
Daniel Veillard1e346af1999-02-22 10:33:01 +00002578 UPDATE_LAST_CHILD(cur)
Daniel Veillard16253641998-10-28 22:58:05 +00002579 break;
2580 case XML_ATTRIBUTE_NODE:
2581 break;
2582 case XML_TEXT_NODE:
2583 case XML_CDATA_SECTION_NODE:
2584 case XML_ENTITY_REF_NODE:
2585 case XML_ENTITY_NODE:
2586 case XML_PI_NODE:
2587 case XML_COMMENT_NODE:
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002588 if (cur->content != NULL) {
2589#ifndef XML_USE_BUFFER_CONTENT
2590 xmlFree(cur->content);
2591#else
2592 xmlBufferFree(cur->content);
2593#endif
2594 }
Daniel Veillard6454aec1999-09-02 22:04:43 +00002595 if (cur->childs != NULL) xmlFreeNodeList(cur->childs);
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002596 cur->last = cur->childs = NULL;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002597 if (content != NULL) {
2598#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard16253641998-10-28 22:58:05 +00002599 cur->content = xmlStrdup(content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002600#else
2601 cur->content = xmlBufferCreateSize(0);
2602 xmlBufferSetAllocationScheme(cur->content,
2603 xmlGetBufferAllocationScheme());
2604 xmlBufferAdd(cur->content, content, -1);
2605#endif
2606 } else
Daniel Veillard16253641998-10-28 22:58:05 +00002607 cur->content = NULL;
Daniel Veillardb96e6431999-08-29 21:02:19 +00002608 break;
Daniel Veillard16253641998-10-28 22:58:05 +00002609 case XML_DOCUMENT_NODE:
Daniel Veillard7c1206f1999-10-14 09:10:25 +00002610 case XML_HTML_DOCUMENT_NODE:
Daniel Veillard16253641998-10-28 22:58:05 +00002611 case XML_DOCUMENT_TYPE_NODE:
2612 break;
2613 case XML_NOTATION_NODE:
2614 break;
2615 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00002616}
2617
Daniel Veillard97b58771998-10-20 06:14:16 +00002618/**
2619 * xmlNodeSetContentLen:
2620 * @cur: the node being modified
2621 * @content: the new value of the content
2622 * @len: the size of @content
2623 *
2624 * Replace the content of a node.
Daniel Veillard260a68f1998-08-13 03:39:55 +00002625 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00002626void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002627xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00002628 if (cur == NULL) {
Daniel Veillard16253641998-10-28 22:58:05 +00002629 fprintf(stderr, "xmlNodeSetContentLen : node == NULL\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +00002630 return;
2631 }
Daniel Veillard16253641998-10-28 22:58:05 +00002632 switch (cur->type) {
2633 case XML_DOCUMENT_FRAG_NODE:
2634 case XML_ELEMENT_NODE:
2635 if (cur->content != NULL) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002636#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard6454aec1999-09-02 22:04:43 +00002637 xmlFree(cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002638#else
2639 xmlBufferFree(cur->content);
2640#endif
Daniel Veillard16253641998-10-28 22:58:05 +00002641 cur->content = NULL;
2642 }
Daniel Veillard6454aec1999-09-02 22:04:43 +00002643 if (cur->childs != NULL) xmlFreeNodeList(cur->childs);
Daniel Veillard16253641998-10-28 22:58:05 +00002644 cur->childs = xmlStringLenGetNodeList(cur->doc, content, len);
Daniel Veillard1e346af1999-02-22 10:33:01 +00002645 UPDATE_LAST_CHILD(cur)
Daniel Veillard16253641998-10-28 22:58:05 +00002646 break;
2647 case XML_ATTRIBUTE_NODE:
2648 break;
2649 case XML_TEXT_NODE:
2650 case XML_CDATA_SECTION_NODE:
2651 case XML_ENTITY_REF_NODE:
2652 case XML_ENTITY_NODE:
2653 case XML_PI_NODE:
2654 case XML_COMMENT_NODE:
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002655 case XML_NOTATION_NODE:
2656 if (cur->content != NULL) {
2657#ifndef XML_USE_BUFFER_CONTENT
2658 xmlFree(cur->content);
2659#else
2660 xmlBufferFree(cur->content);
2661#endif
2662 }
Daniel Veillard6454aec1999-09-02 22:04:43 +00002663 if (cur->childs != NULL) xmlFreeNodeList(cur->childs);
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002664 cur->childs = cur->last = NULL;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002665 if (content != NULL) {
2666#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard16253641998-10-28 22:58:05 +00002667 cur->content = xmlStrndup(content, len);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002668#else
2669 cur->content = xmlBufferCreateSize(len);
2670 xmlBufferSetAllocationScheme(cur->content,
2671 xmlGetBufferAllocationScheme());
2672 xmlBufferAdd(cur->content, content, len);
2673#endif
2674 } else
Daniel Veillard16253641998-10-28 22:58:05 +00002675 cur->content = NULL;
Daniel Veillardb96e6431999-08-29 21:02:19 +00002676 break;
Daniel Veillard16253641998-10-28 22:58:05 +00002677 case XML_DOCUMENT_NODE:
Daniel Veillard7c1206f1999-10-14 09:10:25 +00002678 case XML_HTML_DOCUMENT_NODE:
Daniel Veillard16253641998-10-28 22:58:05 +00002679 case XML_DOCUMENT_TYPE_NODE:
2680 break;
Daniel Veillard260a68f1998-08-13 03:39:55 +00002681 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00002682}
2683
Daniel Veillard97b58771998-10-20 06:14:16 +00002684/**
2685 * xmlNodeAddContentLen:
2686 * @cur: the node being modified
2687 * @content: extra content
2688 * @len: the size of @content
2689 *
2690 * Append the extra substring to the node content.
Daniel Veillard260a68f1998-08-13 03:39:55 +00002691 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00002692void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002693xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00002694 if (cur == NULL) {
Daniel Veillard16253641998-10-28 22:58:05 +00002695 fprintf(stderr, "xmlNodeAddContentLen : node == NULL\n");
2696 return;
2697 }
2698 if (len <= 0) return;
2699 switch (cur->type) {
2700 case XML_DOCUMENT_FRAG_NODE:
2701 case XML_ELEMENT_NODE: {
2702 xmlNodePtr last = NULL, new;
2703
2704 if (cur->childs != NULL) {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002705 last = cur->last;
Daniel Veillard16253641998-10-28 22:58:05 +00002706 } else {
2707 if (cur->content != NULL) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002708#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard16253641998-10-28 22:58:05 +00002709 cur->childs = xmlStringGetNodeList(cur->doc, cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002710#else
2711 cur->childs = xmlStringGetNodeList(cur->doc,
2712 xmlBufferContent(cur->content));
2713#endif
Daniel Veillard1e346af1999-02-22 10:33:01 +00002714 UPDATE_LAST_CHILD(cur)
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002715#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard6454aec1999-09-02 22:04:43 +00002716 xmlFree(cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002717#else
2718 xmlBufferFree(cur->content);
2719#endif
Daniel Veillard16253641998-10-28 22:58:05 +00002720 cur->content = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002721 last = cur->last;
Daniel Veillard16253641998-10-28 22:58:05 +00002722 }
2723 }
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002724 new = xmlNewTextLen(content, len);
Daniel Veillard16253641998-10-28 22:58:05 +00002725 if (new != NULL) {
2726 xmlAddChild(cur, new);
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002727 if ((last != NULL) && (last->next == new)) {
Daniel Veillard16253641998-10-28 22:58:05 +00002728 xmlTextMerge(last, new);
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002729 }
Daniel Veillard16253641998-10-28 22:58:05 +00002730 }
2731 break;
2732 }
2733 case XML_ATTRIBUTE_NODE:
2734 break;
2735 case XML_TEXT_NODE:
2736 case XML_CDATA_SECTION_NODE:
2737 case XML_ENTITY_REF_NODE:
2738 case XML_ENTITY_NODE:
2739 case XML_PI_NODE:
2740 case XML_COMMENT_NODE:
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002741 case XML_NOTATION_NODE:
2742 if (content != NULL) {
2743#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard16253641998-10-28 22:58:05 +00002744 cur->content = xmlStrncat(cur->content, content, len);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002745#else
2746 xmlBufferAdd(cur->content, content, len);
2747#endif
2748 }
Daniel Veillard16253641998-10-28 22:58:05 +00002749 case XML_DOCUMENT_NODE:
Daniel Veillard7c1206f1999-10-14 09:10:25 +00002750 case XML_HTML_DOCUMENT_NODE:
Daniel Veillard16253641998-10-28 22:58:05 +00002751 case XML_DOCUMENT_TYPE_NODE:
2752 break;
Daniel Veillard16253641998-10-28 22:58:05 +00002753 }
2754}
2755
2756/**
2757 * xmlNodeAddContent:
2758 * @cur: the node being modified
2759 * @content: extra content
2760 *
2761 * Append the extra substring to the node content.
2762 */
2763void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002764xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) {
Daniel Veillard16253641998-10-28 22:58:05 +00002765 int len;
2766
2767 if (cur == NULL) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00002768 fprintf(stderr, "xmlNodeAddContent : node == NULL\n");
2769 return;
2770 }
Daniel Veillard16253641998-10-28 22:58:05 +00002771 if (content == NULL) return;
2772 len = xmlStrlen(content);
2773 xmlNodeAddContentLen(cur, content, len);
2774}
2775
2776/**
2777 * xmlTextMerge:
2778 * @first: the first text node
2779 * @second: the second text node being merged
2780 *
2781 * Merge two text nodes into one
Daniel Veillard1e346af1999-02-22 10:33:01 +00002782 * Returns the first text node augmented
Daniel Veillard16253641998-10-28 22:58:05 +00002783 */
2784xmlNodePtr
2785xmlTextMerge(xmlNodePtr first, xmlNodePtr second) {
2786 if (first == NULL) return(second);
2787 if (second == NULL) return(first);
2788 if (first->type != XML_TEXT_NODE) return(first);
2789 if (second->type != XML_TEXT_NODE) return(first);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002790#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard16253641998-10-28 22:58:05 +00002791 xmlNodeAddContent(first, second->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002792#else
2793 xmlNodeAddContent(first, xmlBufferContent(second->content));
2794#endif
Daniel Veillard16253641998-10-28 22:58:05 +00002795 xmlUnlinkNode(second);
2796 xmlFreeNode(second);
2797 return(first);
Daniel Veillard260a68f1998-08-13 03:39:55 +00002798}
2799
Daniel Veillard97b58771998-10-20 06:14:16 +00002800/**
Daniel Veillardb96e6431999-08-29 21:02:19 +00002801 * xmlGetNsList:
2802 * @doc: the document
2803 * @node: the current node
2804 *
2805 * Search all the namespace applying to a given element.
2806 * Returns an NULL terminated array of all the xmlNsPtr found
2807 * that need to be freed by the caller or NULL if no
2808 * namespace if defined
2809 */
2810xmlNsPtr *
2811xmlGetNsList(xmlDocPtr doc, xmlNodePtr node) {
2812 xmlNsPtr cur;
2813 xmlNsPtr *ret = NULL;
2814 int nbns = 0;
2815 int maxns = 10;
2816 int i;
2817
2818 while (node != NULL) {
2819 cur = node->nsDef;
2820 while (cur != NULL) {
2821 if (ret == NULL) {
Daniel Veillard6454aec1999-09-02 22:04:43 +00002822 ret = (xmlNsPtr *) xmlMalloc((maxns + 1) * sizeof(xmlNsPtr));
Daniel Veillardb96e6431999-08-29 21:02:19 +00002823 if (ret == NULL) {
2824 fprintf(stderr, "xmlGetNsList : out of memory!\n");
2825 return(NULL);
2826 }
2827 ret[nbns] = NULL;
2828 }
2829 for (i = 0;i < nbns;i++) {
2830 if ((cur->prefix == ret[i]->prefix) ||
2831 (!xmlStrcmp(cur->prefix, ret[i]->prefix))) break;
2832 }
2833 if (i >= nbns) {
2834 if (nbns >= maxns) {
2835 maxns *= 2;
Daniel Veillard6454aec1999-09-02 22:04:43 +00002836 ret = (xmlNsPtr *) xmlRealloc(ret,
Daniel Veillardb96e6431999-08-29 21:02:19 +00002837 (maxns + 1) * sizeof(xmlNsPtr));
2838 if (ret == NULL) {
2839 fprintf(stderr, "xmlGetNsList : realloc failed!\n");
2840 return(NULL);
2841 }
2842 }
2843 ret[nbns++] = cur;
2844 ret[nbns] = NULL;
2845 }
2846
2847 cur = cur->next;
2848 }
2849 node = node->parent;
2850 }
2851 return(ret);
2852}
2853
2854/**
Daniel Veillard97b58771998-10-20 06:14:16 +00002855 * xmlSearchNs:
2856 * @doc: the document
2857 * @node: the current node
2858 * @nameSpace: the namespace string
Daniel Veillard260a68f1998-08-13 03:39:55 +00002859 *
Daniel Veillard97b58771998-10-20 06:14:16 +00002860 * Search a Ns registered under a given name space for a document.
2861 * recurse on the parents until it finds the defined namespace
2862 * or return NULL otherwise.
2863 * @nameSpace can be NULL, this is a search for the default namespace.
Daniel Veillard1e346af1999-02-22 10:33:01 +00002864 * Returns the namespace pointer or NULL.
Daniel Veillard260a68f1998-08-13 03:39:55 +00002865 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00002866xmlNsPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002867xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00002868 xmlNsPtr cur;
2869
Daniel Veillard62ba71e1999-12-16 17:52:19 +00002870 if (node == NULL) return(NULL);
Daniel Veillard260a68f1998-08-13 03:39:55 +00002871 while (node != NULL) {
2872 cur = node->nsDef;
2873 while (cur != NULL) {
2874 if ((cur->prefix == NULL) && (nameSpace == NULL))
2875 return(cur);
2876 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
2877 (!xmlStrcmp(cur->prefix, nameSpace)))
2878 return(cur);
2879 cur = cur->next;
2880 }
2881 node = node->parent;
2882 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00002883 return(NULL);
2884}
2885
Daniel Veillard97b58771998-10-20 06:14:16 +00002886/**
2887 * xmlSearchNsByHref:
2888 * @doc: the document
2889 * @node: the current node
2890 * @href: the namespace value
2891 *
2892 * Search a Ns aliasing a given URI. Recurse on the parents until it finds
2893 * the defined namespace or return NULL otherwise.
Daniel Veillard1e346af1999-02-22 10:33:01 +00002894 * Returns the namespace pointer or NULL.
Daniel Veillard260a68f1998-08-13 03:39:55 +00002895 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00002896xmlNsPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002897xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar *href) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00002898 xmlNsPtr cur;
Daniel Veillarddbfd6411999-12-28 16:35:14 +00002899 xmlNodePtr orig = node;
Daniel Veillard260a68f1998-08-13 03:39:55 +00002900
Daniel Veillard10a2c651999-12-12 13:03:50 +00002901 if ((node == NULL) || (href == NULL)) return(NULL);
Daniel Veillard260a68f1998-08-13 03:39:55 +00002902 while (node != NULL) {
2903 cur = node->nsDef;
2904 while (cur != NULL) {
2905 if ((cur->href != NULL) && (href != NULL) &&
Daniel Veillarddbfd6411999-12-28 16:35:14 +00002906 (!xmlStrcmp(cur->href, href))) {
2907 /*
2908 * Check that the prefix is not shadowed between orig and node
2909 */
2910 xmlNodePtr check = orig;
2911 xmlNsPtr tst;
2912
2913 while (check != node) {
2914 tst = check->nsDef;
2915 while (tst != NULL) {
2916 if ((tst->prefix == NULL) && (cur->prefix == NULL))
2917 goto shadowed;
2918 if ((tst->prefix != NULL) && (cur->prefix != NULL) &&
2919 (!xmlStrcmp(tst->prefix, cur->prefix)))
2920 goto shadowed;
2921 tst = tst->next;
2922 }
2923 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00002924 return(cur);
Daniel Veillarddbfd6411999-12-28 16:35:14 +00002925 }
2926shadowed:
Daniel Veillard260a68f1998-08-13 03:39:55 +00002927 cur = cur->next;
2928 }
2929 node = node->parent;
2930 }
Daniel Veillarddbfd6411999-12-28 16:35:14 +00002931 return(NULL);
2932}
2933
2934/**
2935 * xmlNewReconciliedNs
2936 * @doc: the document
2937 * @tree: a node expected to hold the new namespace
2938 * @ns: the original namespace
2939 *
2940 * This function tries to locate a namespace definition in a tree
2941 * ancestors, or create a new namespace definition node similar to
2942 * @ns trying to reuse the same prefix. However if the given prefix is
2943 * null (default namespace) or reused within the subtree defined by
2944 * @tree or on one of its ancestors then a new prefix is generated.
2945 * Returns the (new) namespace definition or NULL in case of error
2946 */
2947xmlNsPtr
2948xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) {
2949 xmlNsPtr def;
2950 xmlChar prefix[50];
2951 int counter = 1;
2952
2953 if (tree == NULL) {
2954#ifdef DEBUG_TREE
2955 fprintf(stderr, "xmlNewReconciliedNs : tree == NULL\n");
2956#endif
2957 return(NULL);
2958 }
2959 if (ns == NULL) {
2960#ifdef DEBUG_TREE
2961 fprintf(stderr, "xmlNewReconciliedNs : ns == NULL\n");
2962#endif
2963 return(NULL);
2964 }
2965 /*
2966 * Search an existing namespace definition inherited.
2967 */
2968 def = xmlSearchNsByHref(doc, tree, ns->href);
2969 if (def != NULL)
2970 return(def);
2971
2972 /*
2973 * Find a close prefix which is not already in use.
2974 * Let's strip namespace prefixes longer than 20 chars !
2975 */
2976 sprintf((char *) prefix, "%.20s", ns->prefix);
2977 def = xmlSearchNs(doc, tree, prefix);
2978 while (def != NULL) {
2979 if (counter > 1000) return(NULL);
2980 sprintf((char *) prefix, "%.20s%d", ns->prefix, counter++);
2981 def = xmlSearchNs(doc, tree, prefix);
2982 }
2983
2984 /*
2985 * Ok, now we are ready to create a new one.
2986 */
2987 def = xmlNewNs(tree, ns->href, prefix);
2988 return(def);
2989}
2990
2991/**
2992 * xmlReconciliateNs
2993 * @doc: the document
2994 * @tree: a node defining the subtree to reconciliate
2995 *
2996 * This function checks that all the namespaces declared within the given
2997 * tree are properly declared. This is needed for example after Copy or Cut
2998 * and then paste operations. The subtree may still hold pointers to
2999 * namespace declarations outside the subtree or invalid/masked. As much
3000 * as possible the function try tu reuse the existing namespaces found in
3001 * the new environment. If not possible the new namespaces are redeclared
3002 * on @tree at the top of the given subtree.
3003 * Returns the number of namespace declarations created or -1 in case of error.
3004 */
3005int
3006xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) {
3007 xmlNsPtr *oldNs = NULL;
3008 xmlNsPtr *newNs = NULL;
3009 int sizeCache = 0;
3010 int nbCache = 0;
3011
3012 xmlNsPtr n;
3013 xmlNodePtr node = tree;
3014 xmlAttrPtr attr;
3015 int ret = 0, i;
3016
3017 while (node != NULL) {
3018 /*
3019 * Reconciliate the node namespace
3020 */
3021 if (node->ns != NULL) {
3022 /*
3023 * initialize the cache if needed
3024 */
3025 if (sizeCache == 0) {
3026 sizeCache = 10;
3027 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
3028 sizeof(xmlNsPtr));
3029 if (oldNs == NULL) {
3030 fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
3031 return(-1);
3032 }
3033 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
3034 sizeof(xmlNsPtr));
3035 if (newNs == NULL) {
3036 fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
3037 xmlFree(oldNs);
3038 return(-1);
3039 }
3040 }
3041 for (i = 0;i < nbCache;i++) {
3042 if (oldNs[i] == node->ns) {
3043 node->ns = newNs[i];
3044 break;
3045 }
3046 }
3047 if (i == nbCache) {
3048 /*
3049 * Ok we need to recreate a new namespace definition
3050 */
3051 n = xmlNewReconciliedNs(doc, tree, node->ns);
3052 if (n != NULL) { /* :-( what if else ??? */
3053 /*
3054 * check if we need to grow the cache buffers.
3055 */
3056 if (sizeCache <= nbCache) {
3057 sizeCache *= 2;
3058 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
3059 sizeof(xmlNsPtr));
3060 if (oldNs == NULL) {
3061 fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
3062 xmlFree(newNs);
3063 return(-1);
3064 }
3065 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
3066 sizeof(xmlNsPtr));
3067 if (newNs == NULL) {
3068 fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
3069 xmlFree(oldNs);
3070 return(-1);
3071 }
3072 }
3073 newNs[nbCache] = n;
3074 oldNs[nbCache++] = node->ns;
3075 node->ns = n;
3076 }
3077 }
3078 }
3079 /*
3080 * now check for namespace hold by attributes on the node.
3081 */
3082 attr = node->properties;
3083 while (attr != NULL) {
3084 if (attr->ns != NULL) {
3085 /*
3086 * initialize the cache if needed
3087 */
3088 if (sizeCache == 0) {
3089 sizeCache = 10;
3090 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
3091 sizeof(xmlNsPtr));
3092 if (oldNs == NULL) {
3093 fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
3094 return(-1);
3095 }
3096 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
3097 sizeof(xmlNsPtr));
3098 if (newNs == NULL) {
3099 fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
3100 xmlFree(oldNs);
3101 return(-1);
3102 }
3103 }
3104 for (i = 0;i < nbCache;i++) {
3105 if (oldNs[i] == attr->ns) {
3106 node->ns = newNs[i];
3107 break;
3108 }
3109 }
3110 if (i == nbCache) {
3111 /*
3112 * Ok we need to recreate a new namespace definition
3113 */
3114 n = xmlNewReconciliedNs(doc, tree, attr->ns);
3115 if (n != NULL) { /* :-( what if else ??? */
3116 /*
3117 * check if we need to grow the cache buffers.
3118 */
3119 if (sizeCache <= nbCache) {
3120 sizeCache *= 2;
3121 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
3122 sizeof(xmlNsPtr));
3123 if (oldNs == NULL) {
3124 fprintf(stderr,
3125 "xmlReconciliateNs : memory pbm\n");
3126 xmlFree(newNs);
3127 return(-1);
3128 }
3129 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
3130 sizeof(xmlNsPtr));
3131 if (newNs == NULL) {
3132 fprintf(stderr,
3133 "xmlReconciliateNs : memory pbm\n");
3134 xmlFree(oldNs);
3135 return(-1);
3136 }
3137 }
3138 newNs[nbCache] = n;
3139 oldNs[nbCache++] = attr->ns;
3140 attr->ns = n;
3141 }
3142 }
3143 }
3144 attr = attr->next;
3145 }
3146
3147 /*
3148 * Browse the full subtree, deep first
3149 */
3150 if (node->childs != NULL) {
3151 /* deep first */
3152 node = node->childs;
3153 } else if ((node != tree) && (node->next != NULL)) {
3154 /* then siblings */
3155 node = node->next;
3156 } else if (node != tree) {
3157 /* go up to parents->next if needed */
3158 while (node != tree) {
3159 if (node->parent != NULL)
3160 node = node->parent;
3161 if ((node != tree) && (node->next != NULL)) {
3162 node = node->next;
3163 break;
3164 }
3165 if (node->parent == NULL) {
3166 node = NULL;
3167 break;
3168 }
3169 }
3170 /* exit condition */
3171 if (node == tree)
3172 node = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00003173 }
3174 }
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003175 return(ret);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003176}
3177
Daniel Veillard97b58771998-10-20 06:14:16 +00003178/**
3179 * xmlGetProp:
3180 * @node: the node
3181 * @name: the attribute name
3182 *
3183 * Search and get the value of an attribute associated to a node
Daniel Veillardccb09631998-10-27 06:21:04 +00003184 * This does the entity substitution.
Daniel Veillard10a2c651999-12-12 13:03:50 +00003185 * This function looks in DTD attribute declaration for #FIXED or
3186 * default declaration values unless DTD use has been turned off.
3187 *
Daniel Veillard1e346af1999-02-22 10:33:01 +00003188 * Returns the attribute value or NULL if not found.
Daniel Veillarda819dac1999-11-24 18:04:22 +00003189 * It's up to the caller to free the memory.
Daniel Veillard260a68f1998-08-13 03:39:55 +00003190 */
Daniel Veillarda819dac1999-11-24 18:04:22 +00003191xmlChar *
3192xmlGetProp(xmlNodePtr node, const xmlChar *name) {
Daniel Veillard10a2c651999-12-12 13:03:50 +00003193 xmlAttrPtr prop;
3194 xmlDocPtr doc;
Daniel Veillard260a68f1998-08-13 03:39:55 +00003195
Daniel Veillard10a2c651999-12-12 13:03:50 +00003196 if ((node == NULL) || (name == NULL)) return(NULL);
3197 /*
3198 * Check on the properties attached to the node
3199 */
3200 prop = node->properties;
Daniel Veillard260a68f1998-08-13 03:39:55 +00003201 while (prop != NULL) {
Daniel Veillard68178931999-02-08 18:34:36 +00003202 if (!xmlStrcmp(prop->name, name)) {
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003203 xmlChar *ret;
Daniel Veillard6800ef31999-02-08 18:33:22 +00003204
3205 ret = xmlNodeListGetString(node->doc, prop->val, 1);
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003206 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
Daniel Veillard6800ef31999-02-08 18:33:22 +00003207 return(ret);
Daniel Veillard68178931999-02-08 18:34:36 +00003208 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00003209 prop = prop->next;
3210 }
Daniel Veillard10a2c651999-12-12 13:03:50 +00003211 if (!xmlCheckDTD) return(NULL);
3212
3213 /*
3214 * Check if there is a default declaration in the internal
3215 * or external subsets
3216 */
3217 doc = node->doc;
3218 if (doc != NULL) {
3219 xmlAttributePtr attrDecl;
3220 if (doc->intSubset != NULL) {
3221 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
3222 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3223 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
3224 return(xmlStrdup(attrDecl->defaultValue));
3225 }
3226 }
3227 return(NULL);
3228}
3229
3230/**
3231 * xmlGetNsProp:
3232 * @node: the node
3233 * @name: the attribute name
3234 * @namespace: the URI of the namespace
3235 *
3236 * Search and get the value of an attribute associated to a node
3237 * This attribute has to be anchored in the namespace specified.
3238 * This does the entity substitution.
3239 * This function looks in DTD attribute declaration for #FIXED or
3240 * default declaration values unless DTD use has been turned off.
3241 *
3242 * Returns the attribute value or NULL if not found.
3243 * It's up to the caller to free the memory.
3244 */
3245xmlChar *
3246xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *namespace) {
3247 xmlAttrPtr prop = node->properties;
3248 xmlDocPtr doc;
3249 xmlNsPtr ns;
3250
3251 if (namespace == NULL)
3252 return(xmlGetProp(node, name));
3253 while (prop != NULL) {
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00003254 /*
3255 * One need to have
3256 * - same attribute names
3257 * - and the attribute carrying that namespace
3258 * or
3259 * no namespace on the attribute and the element carrying it
3260 */
Daniel Veillard10a2c651999-12-12 13:03:50 +00003261 if ((!xmlStrcmp(prop->name, name)) &&
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00003262 (((prop->ns == NULL) && (node->ns != NULL) &&
3263 (!xmlStrcmp(node->ns->href, namespace))) ||
Daniel Veillard3c558c31999-12-22 11:30:41 +00003264 ((prop->ns != NULL) && (!xmlStrcmp(prop->ns->href, namespace))))) {
Daniel Veillard10a2c651999-12-12 13:03:50 +00003265 xmlChar *ret;
3266
3267 ret = xmlNodeListGetString(node->doc, prop->val, 1);
3268 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
3269 return(ret);
3270 }
3271 prop = prop->next;
3272 }
3273 if (!xmlCheckDTD) return(NULL);
3274
3275 /*
3276 * Check if there is a default declaration in the internal
3277 * or external subsets
3278 */
3279 doc = node->doc;
3280 if (doc != NULL) {
3281 xmlAttributePtr attrDecl;
3282 if (doc->intSubset != NULL) {
3283 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
3284 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3285 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
3286
3287 if (attrDecl->prefix != NULL) {
3288 /*
3289 * The DTD declaration only allows a prefix search
3290 */
3291 ns = xmlSearchNs(doc, node, attrDecl->prefix);
3292 if ((ns != NULL) && (!xmlStrcmp(ns->href, namespace)))
3293 return(xmlStrdup(attrDecl->defaultValue));
3294 }
3295 }
3296 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00003297 return(NULL);
3298}
3299
Daniel Veillard97b58771998-10-20 06:14:16 +00003300/**
Daniel Veillardccb09631998-10-27 06:21:04 +00003301 * xmlSetProp:
Daniel Veillard97b58771998-10-20 06:14:16 +00003302 * @node: the node
3303 * @name: the attribute name
3304 * @value: the attribute value
3305 *
3306 * Set (or reset) an attribute carried by a node.
Daniel Veillard1e346af1999-02-22 10:33:01 +00003307 * Returns the attribute pointer.
Daniel Veillard260a68f1998-08-13 03:39:55 +00003308 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003309xmlAttrPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003310xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003311 xmlAttrPtr prop = node->properties;
3312
3313 while (prop != NULL) {
3314 if (!xmlStrcmp(prop->name, name)) {
Daniel Veillardccb09631998-10-27 06:21:04 +00003315 if (prop->val != NULL)
Daniel Veillard6454aec1999-09-02 22:04:43 +00003316 xmlFreeNodeList(prop->val);
Daniel Veillardccb09631998-10-27 06:21:04 +00003317 prop->val = NULL;
Daniel Veillard51e3b151999-11-12 17:02:31 +00003318 if (value != NULL) {
3319 xmlChar *buffer;
3320 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
3321 prop->val = xmlStringGetNodeList(node->doc, buffer);
3322 xmlFree(buffer);
3323 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00003324 return(prop);
3325 }
3326 prop = prop->next;
3327 }
3328 prop = xmlNewProp(node, name, value);
3329 return(prop);
3330}
3331
Daniel Veillard97b58771998-10-20 06:14:16 +00003332/**
3333 * xmlNodeIsText:
3334 * @node: the node
3335 *
3336 * Is this node a Text node ?
Daniel Veillard1e346af1999-02-22 10:33:01 +00003337 * Returns 1 yes, 0 no
Daniel Veillard260a68f1998-08-13 03:39:55 +00003338 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003339int
3340xmlNodeIsText(xmlNodePtr node) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003341 if (node == NULL) return(0);
3342
Daniel Veillard0bef1311998-10-14 02:36:47 +00003343 if (node->type == XML_TEXT_NODE) return(1);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003344 return(0);
3345}
3346
Daniel Veillard97b58771998-10-20 06:14:16 +00003347/**
Daniel Veillard1e346af1999-02-22 10:33:01 +00003348 * xmlTextConcat:
Daniel Veillard97b58771998-10-20 06:14:16 +00003349 * @node: the node
3350 * @content: the content
3351 * @len: @content lenght
3352 *
3353 * Concat the given string at the end of the existing node content
Daniel Veillard260a68f1998-08-13 03:39:55 +00003354 */
Daniel Veillard97b58771998-10-20 06:14:16 +00003355
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003356void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003357xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003358 if (node == NULL) return;
3359
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003360 if ((node->type != XML_TEXT_NODE) &&
3361 (node->type != XML_CDATA_SECTION_NODE)) {
3362 fprintf(stderr, "xmlTextConcat: node is not text nor cdata\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +00003363 return;
3364 }
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003365#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard260a68f1998-08-13 03:39:55 +00003366 node->content = xmlStrncat(node->content, content, len);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003367#else
3368 xmlBufferAdd(node->content, content, len);
3369#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00003370}
3371
3372/************************************************************************
3373 * *
3374 * Output : to a FILE or in memory *
3375 * *
3376 ************************************************************************/
3377
Daniel Veillard5099ae81999-04-21 20:12:07 +00003378#define BASE_BUFFER_SIZE 4000
3379
3380/**
3381 * xmlBufferCreate:
3382 *
3383 * routine to create an XML buffer.
3384 * returns the new structure.
3385 */
3386xmlBufferPtr
3387xmlBufferCreate(void) {
3388 xmlBufferPtr ret;
3389
Daniel Veillard6454aec1999-09-02 22:04:43 +00003390 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
Daniel Veillard5099ae81999-04-21 20:12:07 +00003391 if (ret == NULL) {
3392 fprintf(stderr, "xmlBufferCreate : out of memory!\n");
3393 return(NULL);
3394 }
3395 ret->use = 0;
3396 ret->size = BASE_BUFFER_SIZE;
Daniel Veillard10a2c651999-12-12 13:03:50 +00003397 ret->alloc = xmlBufferAllocScheme;
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003398 ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar));
Daniel Veillard5099ae81999-04-21 20:12:07 +00003399 if (ret->content == NULL) {
3400 fprintf(stderr, "xmlBufferCreate : out of memory!\n");
Daniel Veillard6454aec1999-09-02 22:04:43 +00003401 xmlFree(ret);
Daniel Veillard5099ae81999-04-21 20:12:07 +00003402 return(NULL);
3403 }
3404 ret->content[0] = 0;
3405 return(ret);
3406}
3407
3408/**
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003409 * xmlBufferCreateSize:
3410 * @size: initial size of buffer
3411 *
3412 * routine to create an XML buffer.
3413 * returns the new structure.
3414 */
3415xmlBufferPtr
3416xmlBufferCreateSize(size_t size) {
3417 xmlBufferPtr ret;
3418
3419 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
3420 if (ret == NULL) {
3421 fprintf(stderr, "xmlBufferCreate : out of memory!\n");
3422 return(NULL);
3423 }
3424 ret->use = 0;
Daniel Veillard10a2c651999-12-12 13:03:50 +00003425 ret->alloc = xmlBufferAllocScheme;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003426 ret->size = (size ? size+2 : 0); /* +1 for ending null */
Daniel Veillard10a2c651999-12-12 13:03:50 +00003427 if (ret->size){
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003428 ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar));
3429 if (ret->content == NULL) {
3430 fprintf(stderr, "xmlBufferCreate : out of memory!\n");
3431 xmlFree(ret);
3432 return(NULL);
3433 }
3434 ret->content[0] = 0;
3435 } else
3436 ret->content = NULL;
3437 return(ret);
3438}
3439
3440/**
3441 * xmlBufferAllocationScheme:
3442 * @buf: the buffer to free
3443 * @scheme: allocation scheme to use
3444 *
3445 * Sets the allocation scheme for this buffer
3446 */
3447void
3448xmlBufferSetAllocationScheme(xmlBufferPtr buf,
3449 xmlBufferAllocationScheme scheme) {
3450 if (buf == NULL) {
3451 fprintf(stderr, "xmlBufferSetAllocationScheme: buf == NULL\n");
3452 return;
3453 }
3454
3455 buf->alloc = scheme;
3456}
3457
3458/**
Daniel Veillard5099ae81999-04-21 20:12:07 +00003459 * xmlBufferFree:
3460 * @buf: the buffer to free
3461 *
3462 * Frees an XML buffer.
3463 */
3464void
3465xmlBufferFree(xmlBufferPtr buf) {
3466 if (buf == NULL) {
3467 fprintf(stderr, "xmlBufferFree: buf == NULL\n");
3468 return;
3469 }
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003470 if (buf->content != NULL) {
3471#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard5099ae81999-04-21 20:12:07 +00003472 memset(buf->content, -1, BASE_BUFFER_SIZE);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003473#else
3474 memset(buf->content, -1, buf->size);
3475#endif
Daniel Veillard6454aec1999-09-02 22:04:43 +00003476 xmlFree(buf->content);
Daniel Veillard5099ae81999-04-21 20:12:07 +00003477 }
3478 memset(buf, -1, sizeof(xmlBuffer));
Daniel Veillard6454aec1999-09-02 22:04:43 +00003479 xmlFree(buf);
Daniel Veillard5099ae81999-04-21 20:12:07 +00003480}
3481
3482/**
Daniel Veillarde2d034d1999-07-27 19:52:06 +00003483 * xmlBufferEmpty:
3484 * @buf: the buffer
3485 *
3486 * empty a buffer.
3487 */
3488void
3489xmlBufferEmpty(xmlBufferPtr buf) {
3490 buf->use = 0;
3491 memset(buf->content, -1, buf->size);/* just for debug */
3492}
3493
3494/**
3495 * xmlBufferShrink:
3496 * @buf: the buffer to dump
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003497 * @len: the number of xmlChar to remove
Daniel Veillarde2d034d1999-07-27 19:52:06 +00003498 *
3499 * Remove the beginning of an XML buffer.
3500 *
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003501 * Returns the number of xmlChar removed, or -1 in case of failure.
Daniel Veillarde2d034d1999-07-27 19:52:06 +00003502 */
3503int
3504xmlBufferShrink(xmlBufferPtr buf, int len) {
3505 if (len == 0) return(0);
3506 if (len > buf->use) return(-1);
3507
3508 buf->use -= len;
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003509 memmove(buf->content, &buf->content[len], buf->use * sizeof(xmlChar));
Daniel Veillarde2d034d1999-07-27 19:52:06 +00003510
3511 buf->content[buf->use] = 0;
3512 return(len);
3513}
3514
3515/**
Daniel Veillard5099ae81999-04-21 20:12:07 +00003516 * xmlBufferDump:
3517 * @file: the file output
3518 * @buf: the buffer to dump
3519 *
3520 * Dumps an XML buffer to a FILE *.
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003521 * Returns the number of xmlChar written
Daniel Veillard5099ae81999-04-21 20:12:07 +00003522 */
3523int
3524xmlBufferDump(FILE *file, xmlBufferPtr buf) {
3525 int ret;
3526
3527 if (buf == NULL) {
3528 fprintf(stderr, "xmlBufferDump: buf == NULL\n");
3529 return(0);
3530 }
3531 if (buf->content == NULL) {
3532 fprintf(stderr, "xmlBufferDump: buf->content == NULL\n");
3533 return(0);
3534 }
3535 if (file == NULL) file = stdout;
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003536 ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
Daniel Veillard5099ae81999-04-21 20:12:07 +00003537 return(ret);
3538}
3539
3540/**
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003541 * xmlBufferContent:
3542 * @buf: the buffer to resize
3543 *
3544 * Returns the internal content
3545 */
3546
3547const xmlChar*
3548xmlBufferContent(const xmlBufferPtr buf)
3549{
3550 if(!buf)
3551 return NULL;
3552
3553 return buf->content;
3554}
3555
3556/**
3557 * xmlBufferLength:
3558 * @buf: the buffer
3559 *
3560 * Returns the length of data in the internal content
3561 */
3562
3563int
3564xmlBufferLength(const xmlBufferPtr buf)
3565{
3566 if(!buf)
3567 return 0;
3568
3569 return buf->use;
3570}
3571
3572/**
3573 * xmlBufferResize:
3574 * @buf: the buffer to resize
3575 * @len: the desired size
3576 *
3577 * Resize a buffer to accomodate minimum size of <len>.
3578 *
3579 * Returns 0 in case of problems, 1 otherwise
3580 */
3581int
3582xmlBufferResize(xmlBufferPtr buf, int size)
3583{
3584 int newSize = (buf->size ? buf->size*2 : size);/*take care of empty case*/
3585 xmlChar* rebuf = NULL;
3586
3587 /* Don't resize if we don't have to */
3588 if(size < buf->size)
3589 return 1;
3590
3591 /* figure out new size */
3592 switch(buf->alloc){
3593 case XML_BUFFER_ALLOC_DOUBLEIT:
3594 while(size > newSize) newSize *= 2;
3595 break;
3596 case XML_BUFFER_ALLOC_EXACT:
3597 newSize = size+10;
3598 break;
3599 default:
3600 newSize = size+10;
3601 break;
3602 }
3603
3604 if (buf->content == NULL)
3605 rebuf = (xmlChar *) xmlMalloc(newSize * sizeof(xmlChar));
3606 else
3607 rebuf = (xmlChar *) xmlRealloc(buf->content,
3608 newSize * sizeof(xmlChar));
3609 if (rebuf == NULL) {
3610 fprintf(stderr, "xmlBufferAdd : out of memory!\n");
3611 return 0;
3612 }
3613 buf->content = rebuf;
3614 buf->size = newSize;
3615
3616 return 1;
3617}
3618/**
Daniel Veillard5099ae81999-04-21 20:12:07 +00003619 * xmlBufferAdd:
3620 * @buf: the buffer to dump
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003621 * @str: the xmlChar string
3622 * @len: the number of xmlChar to add
Daniel Veillard5099ae81999-04-21 20:12:07 +00003623 *
Daniel Veillard10a2c651999-12-12 13:03:50 +00003624 * Add a string range to an XML buffer. if len == -1, the lenght of
3625 * str is recomputed.
Daniel Veillard5099ae81999-04-21 20:12:07 +00003626 */
3627void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003628xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003629 int l, needSize;
Daniel Veillard5099ae81999-04-21 20:12:07 +00003630
3631 if (str == NULL) {
3632 fprintf(stderr, "xmlBufferAdd: str == NULL\n");
3633 return;
3634 }
Daniel Veillard10a2c651999-12-12 13:03:50 +00003635 if (len < -1) {
3636 fprintf(stderr, "xmlBufferAdd: len < 0\n");
3637 return;
3638 }
3639 if (len == 0) return;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003640
3641 /* CJN What's this for??? */
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003642 if (len < 0)
3643 l = xmlStrlen(str);
3644 else
3645 for (l = 0;l < len;l++)
3646 if (str[l] == 0) break;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003647 if (l < len){ len = l; printf("xmlBufferAdd bad length\n"); }
3648
3649 /* CJN 11.18.99 okay, now I'm using the length */
3650 if(len == -1) len = l;
3651
3652
Daniel Veillarde2d034d1999-07-27 19:52:06 +00003653 if (len <= 0) return;
Daniel Veillard5099ae81999-04-21 20:12:07 +00003654
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003655 needSize = buf->use + len + 2;
3656 if(needSize > buf->size){
3657 if(!xmlBufferResize(buf, needSize)){
3658 fprintf(stderr, "xmlBufferAdd : out of memory!\n");
3659 return;
3660 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00003661 }
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003662
3663 memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
Daniel Veillarde2d034d1999-07-27 19:52:06 +00003664 buf->use += len;
3665 buf->content[buf->use] = 0;
Daniel Veillard5099ae81999-04-21 20:12:07 +00003666}
3667
3668/**
3669 * xmlBufferCat:
3670 * @buf: the buffer to dump
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003671 * @str: the xmlChar string
Daniel Veillard5099ae81999-04-21 20:12:07 +00003672 *
3673 * Append a zero terminated string to an XML buffer.
3674 */
3675void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003676xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003677 if (str != NULL)
3678 xmlBufferAdd(buf, str, -1);
Daniel Veillard5099ae81999-04-21 20:12:07 +00003679}
3680
3681/**
3682 * xmlBufferCCat:
3683 * @buf: the buffer to dump
3684 * @str: the C char string
3685 *
3686 * Append a zero terminated C string to an XML buffer.
3687 */
3688void
3689xmlBufferCCat(xmlBufferPtr buf, const char *str) {
3690 const char *cur;
3691
3692 if (str == NULL) {
3693 fprintf(stderr, "xmlBufferAdd: str == NULL\n");
3694 return;
3695 }
3696 for (cur = str;*cur != 0;cur++) {
3697 if (buf->use + 10 >= buf->size) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003698 if(!xmlBufferResize(buf, buf->use+10)){
3699 fprintf(stderr, "xmlBufferCCat : out of memory!\n");
3700 return;
3701 }
3702 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00003703 buf->content[buf->use++] = *cur;
3704 }
3705}
Daniel Veillard260a68f1998-08-13 03:39:55 +00003706
Daniel Veillard97b58771998-10-20 06:14:16 +00003707/**
3708 * xmlBufferWriteCHAR:
Daniel Veillard5099ae81999-04-21 20:12:07 +00003709 * @buf: the XML buffer
Daniel Veillard97b58771998-10-20 06:14:16 +00003710 * @string: the string to add
3711 *
3712 * routine which manage and grows an output buffer. This one add
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003713 * xmlChars at the end of the buffer.
Daniel Veillard97b58771998-10-20 06:14:16 +00003714 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003715void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003716xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00003717 xmlBufferCat(buf, string);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003718}
3719
Daniel Veillard97b58771998-10-20 06:14:16 +00003720/**
3721 * xmlBufferWriteChar:
Daniel Veillard011b63c1999-06-02 17:44:04 +00003722 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00003723 * @string: the string to add
3724 *
3725 * routine which manage and grows an output buffer. This one add
3726 * C chars at the end of the array.
3727 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003728void
Daniel Veillard5099ae81999-04-21 20:12:07 +00003729xmlBufferWriteChar(xmlBufferPtr buf, const char *string) {
3730 xmlBufferCCat(buf, string);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003731}
3732
Daniel Veillard5099ae81999-04-21 20:12:07 +00003733
Daniel Veillard97b58771998-10-20 06:14:16 +00003734/**
Daniel Veillard011b63c1999-06-02 17:44:04 +00003735 * xmlBufferWriteQuotedString:
3736 * @buf: the XML buffer output
3737 * @string: the string to add
3738 *
3739 * routine which manage and grows an output buffer. This one writes
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003740 * a quoted or double quoted xmlChar string, checking first if it holds
Daniel Veillard011b63c1999-06-02 17:44:04 +00003741 * quote or double-quotes internally
3742 */
3743void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003744xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) {
Daniel Veillardb96e6431999-08-29 21:02:19 +00003745 if (xmlStrchr(string, '"')) {
3746 if (xmlStrchr(string, '\'')) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00003747 fprintf(stderr,
3748 "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n");
3749 }
3750 xmlBufferCCat(buf, "'");
3751 xmlBufferCat(buf, string);
3752 xmlBufferCCat(buf, "'");
3753 } else {
3754 xmlBufferCCat(buf, "\"");
3755 xmlBufferCat(buf, string);
3756 xmlBufferCCat(buf, "\"");
3757 }
3758}
3759
3760
3761/**
Daniel Veillard97b58771998-10-20 06:14:16 +00003762 * xmlGlobalNsDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00003763 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00003764 * @cur: a namespace
3765 *
3766 * Dump a global Namespace, this is the old version based on PIs.
Daniel Veillard260a68f1998-08-13 03:39:55 +00003767 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003768static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00003769xmlGlobalNsDump(xmlBufferPtr buf, xmlNsPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003770 if (cur == NULL) {
3771 fprintf(stderr, "xmlGlobalNsDump : Ns == NULL\n");
3772 return;
3773 }
3774 if (cur->type == XML_GLOBAL_NAMESPACE) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00003775 xmlBufferWriteChar(buf, "<?namespace");
Daniel Veillard260a68f1998-08-13 03:39:55 +00003776 if (cur->href != NULL) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00003777 xmlBufferWriteChar(buf, " href=");
3778 xmlBufferWriteQuotedString(buf, cur->href);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003779 }
3780 if (cur->prefix != NULL) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00003781 xmlBufferWriteChar(buf, " AS=");
3782 xmlBufferWriteQuotedString(buf, cur->prefix);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003783 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00003784 xmlBufferWriteChar(buf, "?>\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +00003785 }
3786}
3787
Daniel Veillard97b58771998-10-20 06:14:16 +00003788/**
3789 * xmlGlobalNsListDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00003790 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00003791 * @cur: the first namespace
3792 *
3793 * Dump a list of global Namespace, this is the old version based on PIs.
Daniel Veillard260a68f1998-08-13 03:39:55 +00003794 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003795static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00003796xmlGlobalNsListDump(xmlBufferPtr buf, xmlNsPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003797 while (cur != NULL) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00003798 xmlGlobalNsDump(buf, cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003799 cur = cur->next;
3800 }
3801}
3802
Daniel Veillard97b58771998-10-20 06:14:16 +00003803/**
3804 * xmlNsDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00003805 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00003806 * @cur: a namespace
3807 *
Daniel Veillard260a68f1998-08-13 03:39:55 +00003808 * Dump a local Namespace definition.
Daniel Veillard97b58771998-10-20 06:14:16 +00003809 * Should be called in the context of attributes dumps.
Daniel Veillard260a68f1998-08-13 03:39:55 +00003810 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003811static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00003812xmlNsDump(xmlBufferPtr buf, xmlNsPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003813 if (cur == NULL) {
3814 fprintf(stderr, "xmlNsDump : Ns == NULL\n");
3815 return;
3816 }
3817 if (cur->type == XML_LOCAL_NAMESPACE) {
3818 /* Within the context of an element attributes */
3819 if (cur->prefix != NULL) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00003820 xmlBufferWriteChar(buf, " xmlns:");
3821 xmlBufferWriteCHAR(buf, cur->prefix);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003822 } else
Daniel Veillard5099ae81999-04-21 20:12:07 +00003823 xmlBufferWriteChar(buf, " xmlns");
Daniel Veillard011b63c1999-06-02 17:44:04 +00003824 xmlBufferWriteChar(buf, "=");
3825 xmlBufferWriteQuotedString(buf, cur->href);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003826 }
3827}
3828
Daniel Veillard97b58771998-10-20 06:14:16 +00003829/**
3830 * xmlNsListDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00003831 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00003832 * @cur: the first namespace
3833 *
3834 * Dump a list of local Namespace definitions.
3835 * Should be called in the context of attributes dumps.
Daniel Veillard260a68f1998-08-13 03:39:55 +00003836 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003837static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00003838xmlNsListDump(xmlBufferPtr buf, xmlNsPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003839 while (cur != NULL) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00003840 xmlNsDump(buf, cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003841 cur = cur->next;
3842 }
3843}
3844
Daniel Veillard97b58771998-10-20 06:14:16 +00003845/**
3846 * xmlDtdDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00003847 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00003848 * @doc: the document
3849 *
3850 * Dump the XML document DTD, if any.
Daniel Veillard260a68f1998-08-13 03:39:55 +00003851 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003852static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00003853xmlDtdDump(xmlBufferPtr buf, xmlDocPtr doc) {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00003854 xmlDtdPtr cur = doc->intSubset;
Daniel Veillard260a68f1998-08-13 03:39:55 +00003855
3856 if (cur == NULL) {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00003857 fprintf(stderr, "xmlDtdDump : no internal subset\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +00003858 return;
3859 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00003860 xmlBufferWriteChar(buf, "<!DOCTYPE ");
3861 xmlBufferWriteCHAR(buf, cur->name);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003862 if (cur->ExternalID != NULL) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00003863 xmlBufferWriteChar(buf, " PUBLIC ");
3864 xmlBufferWriteQuotedString(buf, cur->ExternalID);
3865 xmlBufferWriteChar(buf, " ");
3866 xmlBufferWriteQuotedString(buf, cur->SystemID);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003867 } else if (cur->SystemID != NULL) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00003868 xmlBufferWriteChar(buf, " SYSTEM ");
3869 xmlBufferWriteQuotedString(buf, cur->SystemID);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003870 }
Daniel Veillard1e346af1999-02-22 10:33:01 +00003871 if ((cur->entities == NULL) && (cur->elements == NULL) &&
3872 (cur->attributes == NULL) && (cur->notations == NULL)) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00003873 xmlBufferWriteChar(buf, ">\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +00003874 return;
3875 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00003876 xmlBufferWriteChar(buf, " [\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +00003877 if (cur->entities != NULL)
Daniel Veillard5099ae81999-04-21 20:12:07 +00003878 xmlDumpEntitiesTable(buf, (xmlEntitiesTablePtr) cur->entities);
Daniel Veillard1e346af1999-02-22 10:33:01 +00003879 if (cur->notations != NULL)
Daniel Veillard5099ae81999-04-21 20:12:07 +00003880 xmlDumpNotationTable(buf, (xmlNotationTablePtr) cur->notations);
Daniel Veillard3b9def11999-01-31 22:15:06 +00003881 if (cur->elements != NULL)
Daniel Veillard5099ae81999-04-21 20:12:07 +00003882 xmlDumpElementTable(buf, (xmlElementTablePtr) cur->elements);
Daniel Veillard1e346af1999-02-22 10:33:01 +00003883 if (cur->attributes != NULL)
Daniel Veillard5099ae81999-04-21 20:12:07 +00003884 xmlDumpAttributeTable(buf, (xmlAttributeTablePtr) cur->attributes);
3885 xmlBufferWriteChar(buf, "]");
Daniel Veillard260a68f1998-08-13 03:39:55 +00003886
Daniel Veillard5099ae81999-04-21 20:12:07 +00003887 xmlBufferWriteChar(buf, ">\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +00003888}
3889
Daniel Veillard97b58771998-10-20 06:14:16 +00003890/**
3891 * xmlAttrDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00003892 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00003893 * @doc: the document
3894 * @cur: the attribute pointer
3895 *
3896 * Dump an XML attribute
Daniel Veillard260a68f1998-08-13 03:39:55 +00003897 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003898static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00003899xmlAttrDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) {
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003900 xmlChar *value;
Daniel Veillardccb09631998-10-27 06:21:04 +00003901
Daniel Veillard260a68f1998-08-13 03:39:55 +00003902 if (cur == NULL) {
3903 fprintf(stderr, "xmlAttrDump : property == NULL\n");
3904 return;
3905 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00003906 xmlBufferWriteChar(buf, " ");
Daniel Veillardb96e6431999-08-29 21:02:19 +00003907 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
3908 xmlBufferWriteCHAR(buf, cur->ns->prefix);
3909 xmlBufferWriteChar(buf, ":");
3910 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00003911 xmlBufferWriteCHAR(buf, cur->name);
Daniel Veillardccb09631998-10-27 06:21:04 +00003912 value = xmlNodeListGetString(doc, cur->val, 0);
3913 if (value) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00003914 xmlBufferWriteChar(buf, "=");
3915 xmlBufferWriteQuotedString(buf, value);
Daniel Veillard6454aec1999-09-02 22:04:43 +00003916 xmlFree(value);
Daniel Veillard726c7e31999-02-08 15:13:10 +00003917 } else {
Daniel Veillard5099ae81999-04-21 20:12:07 +00003918 xmlBufferWriteChar(buf, "=\"\"");
Daniel Veillard260a68f1998-08-13 03:39:55 +00003919 }
3920}
3921
Daniel Veillard97b58771998-10-20 06:14:16 +00003922/**
3923 * xmlAttrListDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00003924 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00003925 * @doc: the document
3926 * @cur: the first attribute pointer
3927 *
3928 * Dump a list of XML attributes
Daniel Veillard260a68f1998-08-13 03:39:55 +00003929 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003930static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00003931xmlAttrListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003932 if (cur == NULL) {
3933 fprintf(stderr, "xmlAttrListDump : property == NULL\n");
3934 return;
3935 }
3936 while (cur != NULL) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00003937 xmlAttrDump(buf, doc, cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003938 cur = cur->next;
3939 }
3940}
3941
Daniel Veillard260a68f1998-08-13 03:39:55 +00003942
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003943static void
Daniel Veillard7d2c2761999-10-11 15:09:51 +00003944xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
3945 int format);
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003946void
3947htmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur);
3948
Daniel Veillard97b58771998-10-20 06:14:16 +00003949/**
3950 * xmlNodeListDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00003951 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00003952 * @doc: the document
3953 * @cur: the first node
3954 * @level: the imbrication level for indenting
Daniel Veillard7d2c2761999-10-11 15:09:51 +00003955 * @format: is formatting allowed
Daniel Veillard97b58771998-10-20 06:14:16 +00003956 *
3957 * Dump an XML node list, recursive behaviour,children are printed too.
3958 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003959static void
Daniel Veillard7d2c2761999-10-11 15:09:51 +00003960xmlNodeListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
3961 int format) {
3962 int i;
Daniel Veillardccb09631998-10-27 06:21:04 +00003963
Daniel Veillard260a68f1998-08-13 03:39:55 +00003964 if (cur == NULL) {
3965 fprintf(stderr, "xmlNodeListDump : node == NULL\n");
3966 return;
3967 }
3968 while (cur != NULL) {
Daniel Veillard7d2c2761999-10-11 15:09:51 +00003969 if ((format) && (xmlIndentTreeOutput) &&
3970 (cur->type == XML_ELEMENT_NODE))
3971 for (i = 0;i < level;i++)
3972 xmlBufferWriteChar(buf, " ");
3973 xmlNodeDump(buf, doc, cur, level, format);
3974 if (format) {
3975 xmlBufferWriteChar(buf, "\n");
Daniel Veillardccb09631998-10-27 06:21:04 +00003976 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00003977 cur = cur->next;
3978 }
3979}
3980
Daniel Veillard97b58771998-10-20 06:14:16 +00003981/**
Daniel Veillardccb09631998-10-27 06:21:04 +00003982 * xmlNodeDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00003983 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00003984 * @doc: the document
3985 * @cur: the current node
3986 * @level: the imbrication level for indenting
Daniel Veillard7d2c2761999-10-11 15:09:51 +00003987 * @format: is formatting allowed
Daniel Veillard97b58771998-10-20 06:14:16 +00003988 *
3989 * Dump an XML node, recursive behaviour,children are printed too.
Daniel Veillard260a68f1998-08-13 03:39:55 +00003990 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003991static void
Daniel Veillard7d2c2761999-10-11 15:09:51 +00003992xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
3993 int format) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003994 int i;
Daniel Veillard7d2c2761999-10-11 15:09:51 +00003995 xmlNodePtr tmp;
Daniel Veillard260a68f1998-08-13 03:39:55 +00003996
3997 if (cur == NULL) {
3998 fprintf(stderr, "xmlNodeDump : node == NULL\n");
3999 return;
4000 }
Daniel Veillard0bef1311998-10-14 02:36:47 +00004001 if (cur->type == XML_TEXT_NODE) {
Daniel Veillard14fff061999-06-22 21:49:07 +00004002 if (cur->content != NULL) {
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004003 xmlChar *buffer;
Daniel Veillard14fff061999-06-22 21:49:07 +00004004
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004005#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard14fff061999-06-22 21:49:07 +00004006 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004007#else
4008 buffer = xmlEncodeEntitiesReentrant(doc,
4009 xmlBufferContent(cur->content));
4010#endif
Daniel Veillard14fff061999-06-22 21:49:07 +00004011 if (buffer != NULL) {
4012 xmlBufferWriteCHAR(buf, buffer);
Daniel Veillard6454aec1999-09-02 22:04:43 +00004013 xmlFree(buffer);
Daniel Veillard14fff061999-06-22 21:49:07 +00004014 }
4015 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00004016 return;
4017 }
Daniel Veillardb96e6431999-08-29 21:02:19 +00004018 if (cur->type == XML_PI_NODE) {
4019 if (cur->content != NULL) {
4020 xmlBufferWriteChar(buf, "<?");
4021 xmlBufferWriteCHAR(buf, cur->name);
4022 if (cur->content != NULL) {
4023 xmlBufferWriteChar(buf, " ");
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004024#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardb96e6431999-08-29 21:02:19 +00004025 xmlBufferWriteCHAR(buf, cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004026#else
4027 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
4028#endif
Daniel Veillardb96e6431999-08-29 21:02:19 +00004029 }
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004030 xmlBufferWriteChar(buf, "?>");
Daniel Veillardb96e6431999-08-29 21:02:19 +00004031 }
4032 return;
4033 }
Daniel Veillard0bef1311998-10-14 02:36:47 +00004034 if (cur->type == XML_COMMENT_NODE) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00004035 if (cur->content != NULL) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004036 xmlBufferWriteChar(buf, "<!--");
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004037#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard5099ae81999-04-21 20:12:07 +00004038 xmlBufferWriteCHAR(buf, cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004039#else
4040 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
4041#endif
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004042 xmlBufferWriteChar(buf, "-->");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004043 }
4044 return;
4045 }
Daniel Veillardccb09631998-10-27 06:21:04 +00004046 if (cur->type == XML_ENTITY_REF_NODE) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004047 xmlBufferWriteChar(buf, "&");
4048 xmlBufferWriteCHAR(buf, cur->name);
4049 xmlBufferWriteChar(buf, ";");
Daniel Veillardccb09631998-10-27 06:21:04 +00004050 return;
4051 }
Daniel Veillardb05deb71999-08-10 19:04:08 +00004052 if (cur->type == XML_CDATA_SECTION_NODE) {
4053 xmlBufferWriteChar(buf, "<![CDATA[");
4054 if (cur->content != NULL)
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004055#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardb05deb71999-08-10 19:04:08 +00004056 xmlBufferWriteCHAR(buf, cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004057#else
4058 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
4059#endif
Daniel Veillardb05deb71999-08-10 19:04:08 +00004060 xmlBufferWriteChar(buf, "]]>");
4061 return;
4062 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00004063
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004064 if (format == 1) {
4065 tmp = cur->childs;
4066 while (tmp != NULL) {
4067 if ((tmp->type == XML_TEXT_NODE) ||
4068 (tmp->type == XML_ENTITY_REF_NODE)) {
4069 format = 0;
4070 break;
4071 }
4072 tmp = tmp->next;
4073 }
4074 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004075 xmlBufferWriteChar(buf, "<");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004076 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004077 xmlBufferWriteCHAR(buf, cur->ns->prefix);
4078 xmlBufferWriteChar(buf, ":");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004079 }
4080
Daniel Veillard5099ae81999-04-21 20:12:07 +00004081 xmlBufferWriteCHAR(buf, cur->name);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004082 if (cur->nsDef)
Daniel Veillard5099ae81999-04-21 20:12:07 +00004083 xmlNsListDump(buf, cur->nsDef);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004084 if (cur->properties != NULL)
Daniel Veillard5099ae81999-04-21 20:12:07 +00004085 xmlAttrListDump(buf, doc, cur->properties);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004086
4087 if ((cur->content == NULL) && (cur->childs == NULL)) {
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004088 xmlBufferWriteChar(buf, "/>");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004089 return;
4090 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004091 xmlBufferWriteChar(buf, ">");
Daniel Veillard14fff061999-06-22 21:49:07 +00004092 if (cur->content != NULL) {
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004093 xmlChar *buffer;
Daniel Veillard14fff061999-06-22 21:49:07 +00004094
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004095#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard14fff061999-06-22 21:49:07 +00004096 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004097#else
4098 buffer = xmlEncodeEntitiesReentrant(doc,
4099 xmlBufferContent(cur->content));
4100#endif
Daniel Veillard14fff061999-06-22 21:49:07 +00004101 if (buffer != NULL) {
4102 xmlBufferWriteCHAR(buf, buffer);
Daniel Veillard6454aec1999-09-02 22:04:43 +00004103 xmlFree(buffer);
Daniel Veillard14fff061999-06-22 21:49:07 +00004104 }
4105 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00004106 if (cur->childs != NULL) {
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004107 if (format) xmlBufferWriteChar(buf, "\n");
4108 xmlNodeListDump(buf, doc, cur->childs, level + 1, format);
4109 if ((xmlIndentTreeOutput) && (format))
4110 for (i = 0;i < level;i++)
4111 xmlBufferWriteChar(buf, " ");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004112 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004113 xmlBufferWriteChar(buf, "</");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004114 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004115 xmlBufferWriteCHAR(buf, cur->ns->prefix);
4116 xmlBufferWriteChar(buf, ":");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004117 }
4118
Daniel Veillard5099ae81999-04-21 20:12:07 +00004119 xmlBufferWriteCHAR(buf, cur->name);
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004120 xmlBufferWriteChar(buf, ">");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004121}
4122
Daniel Veillard97b58771998-10-20 06:14:16 +00004123/**
Daniel Veillarddbfd6411999-12-28 16:35:14 +00004124 * xmlElemDump:
4125 * @buf: the XML buffer output
4126 * @doc: the document
4127 * @cur: the current node
4128 *
4129 * Dump an XML/HTML node, recursive behaviour,children are printed too.
4130 */
4131void
4132xmlElemDump(FILE *f, xmlDocPtr doc, xmlNodePtr cur) {
4133 xmlBufferPtr buf;
4134
4135 if (cur == NULL) {
4136#ifdef DEBUG_TREE
4137 fprintf(stderr, "xmlElemDump : cur == NULL\n");
4138#endif
4139 return;
4140 }
4141 if (doc == NULL) {
4142#ifdef DEBUG_TREE
4143 fprintf(stderr, "xmlElemDump : doc == NULL\n");
4144#endif
4145 }
4146 buf = xmlBufferCreate();
4147 if (buf == NULL) return;
4148 if ((doc != NULL) &&
4149 (doc->type == XML_HTML_DOCUMENT_NODE)) {
4150 htmlNodeDump(buf, doc, cur);
4151 } else
4152 xmlNodeDump(buf, doc, cur, 0, 1);
4153 xmlBufferDump(f, buf);
4154 xmlBufferFree(buf);
4155}
4156
4157/**
Daniel Veillard97b58771998-10-20 06:14:16 +00004158 * xmlDocContentDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00004159 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00004160 * @cur: the document
4161 *
4162 * Dump an XML document.
Daniel Veillard260a68f1998-08-13 03:39:55 +00004163 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004164static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00004165xmlDocContentDump(xmlBufferPtr buf, xmlDocPtr cur) {
Daniel Veillardbe70ff71999-07-05 16:50:46 +00004166 xmlBufferWriteChar(buf, "<?xml version=");
4167 if (cur->version != NULL)
4168 xmlBufferWriteQuotedString(buf, cur->version);
4169 else
4170 xmlBufferWriteChar(buf, "\"1.0\"");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004171 if (cur->encoding != NULL) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00004172 xmlBufferWriteChar(buf, " encoding=");
4173 xmlBufferWriteQuotedString(buf, cur->encoding);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004174 }
4175 switch (cur->standalone) {
4176 case 0:
Daniel Veillard5099ae81999-04-21 20:12:07 +00004177 xmlBufferWriteChar(buf, " standalone=\"no\"");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004178 break;
4179 case 1:
Daniel Veillard5099ae81999-04-21 20:12:07 +00004180 xmlBufferWriteChar(buf, " standalone=\"yes\"");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004181 break;
4182 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004183 xmlBufferWriteChar(buf, "?>\n");
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00004184 if (cur->intSubset != NULL)
Daniel Veillard5099ae81999-04-21 20:12:07 +00004185 xmlDtdDump(buf, cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004186 if (cur->root != NULL) {
Daniel Veillardb96e6431999-08-29 21:02:19 +00004187 xmlNodePtr child = cur->root;
4188
Daniel Veillard260a68f1998-08-13 03:39:55 +00004189 /* global namespace definitions, the old way */
4190 if (oldXMLWDcompatibility)
Daniel Veillard5099ae81999-04-21 20:12:07 +00004191 xmlGlobalNsListDump(buf, cur->oldNs);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004192 else
4193 xmlUpgradeOldNs(cur);
Daniel Veillardb96e6431999-08-29 21:02:19 +00004194
4195 while (child != NULL) {
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004196 xmlNodeDump(buf, cur, child, 0, 1);
4197 xmlBufferWriteChar(buf, "\n");
Daniel Veillardb96e6431999-08-29 21:02:19 +00004198 child = child->next;
4199 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00004200 }
4201}
4202
Daniel Veillard97b58771998-10-20 06:14:16 +00004203/**
4204 * xmlDocDumpMemory:
4205 * @cur: the document
4206 * @mem: OUT: the memory pointer
4207 * @size: OUT: the memory lenght
4208 *
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004209 * Dump an XML document in memory and return the xmlChar * and it's size.
Daniel Veillard97b58771998-10-20 06:14:16 +00004210 * It's up to the caller to free the memory.
Daniel Veillard260a68f1998-08-13 03:39:55 +00004211 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004212void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004213xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004214 xmlBufferPtr buf;
4215
Daniel Veillard260a68f1998-08-13 03:39:55 +00004216 if (cur == NULL) {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00004217#ifdef DEBUG_TREE
4218 fprintf(stderr, "xmlDocDumpMemory : document == NULL\n");
4219#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00004220 *mem = NULL;
4221 *size = 0;
4222 return;
4223 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004224 buf = xmlBufferCreate();
4225 if (buf == NULL) {
4226 *mem = NULL;
4227 *size = 0;
4228 return;
4229 }
4230 xmlDocContentDump(buf, cur);
Daniel Veillardb05deb71999-08-10 19:04:08 +00004231 *mem = xmlStrndup(buf->content, buf->use);
Daniel Veillard5099ae81999-04-21 20:12:07 +00004232 *size = buf->use;
Daniel Veillardb05deb71999-08-10 19:04:08 +00004233 xmlBufferFree(buf);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004234}
4235
Daniel Veillard97b58771998-10-20 06:14:16 +00004236/**
4237 * xmlGetDocCompressMode:
4238 * @doc: the document
4239 *
4240 * get the compression ratio for a document, ZLIB based
Daniel Veillard1e346af1999-02-22 10:33:01 +00004241 * Returns 0 (uncompressed) to 9 (max compression)
Daniel Veillard151b1b01998-09-23 00:49:46 +00004242 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004243int
Daniel Veillarddbfd6411999-12-28 16:35:14 +00004244xmlGetDocCompressMode (xmlDocPtr doc) {
Daniel Veillard15a8df41998-09-24 19:15:06 +00004245 if (doc == NULL) return(-1);
4246 return(doc->compression);
4247}
4248
Daniel Veillard97b58771998-10-20 06:14:16 +00004249/**
4250 * xmlSetDocCompressMode:
4251 * @doc: the document
4252 * @mode: the compression ratio
4253 *
4254 * set the compression ratio for a document, ZLIB based
4255 * Correct values: 0 (uncompressed) to 9 (max compression)
4256 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004257void
4258xmlSetDocCompressMode (xmlDocPtr doc, int mode) {
Daniel Veillard15a8df41998-09-24 19:15:06 +00004259 if (doc == NULL) return;
4260 if (mode < 0) doc->compression = 0;
4261 else if (mode > 9) doc->compression = 9;
4262 else doc->compression = mode;
4263}
4264
Daniel Veillard97b58771998-10-20 06:14:16 +00004265/**
4266 * xmlGetCompressMode:
4267 *
4268 * get the default compression mode used, ZLIB based.
Daniel Veillard1e346af1999-02-22 10:33:01 +00004269 * Returns 0 (uncompressed) to 9 (max compression)
Daniel Veillard15a8df41998-09-24 19:15:06 +00004270 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004271int
4272 xmlGetCompressMode(void) {
Daniel Veillard151b1b01998-09-23 00:49:46 +00004273 return(xmlCompressMode);
4274}
Daniel Veillard97b58771998-10-20 06:14:16 +00004275
4276/**
4277 * xmlSetCompressMode:
4278 * @mode: the compression ratio
4279 *
4280 * set the default compression mode used, ZLIB based
4281 * Correct values: 0 (uncompressed) to 9 (max compression)
4282 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004283void
4284xmlSetCompressMode(int mode) {
Daniel Veillard151b1b01998-09-23 00:49:46 +00004285 if (mode < 0) xmlCompressMode = 0;
Daniel Veillard15a8df41998-09-24 19:15:06 +00004286 else if (mode > 9) xmlCompressMode = 9;
Daniel Veillard151b1b01998-09-23 00:49:46 +00004287 else xmlCompressMode = mode;
4288}
4289
Daniel Veillard97b58771998-10-20 06:14:16 +00004290/**
4291 * xmlDocDump:
4292 * @f: the FILE*
4293 * @cur: the document
4294 *
4295 * Dump an XML document to an open FILE.
Daniel Veillard260a68f1998-08-13 03:39:55 +00004296 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004297void
4298xmlDocDump(FILE *f, xmlDocPtr cur) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004299 xmlBufferPtr buf;
4300
Daniel Veillard260a68f1998-08-13 03:39:55 +00004301 if (cur == NULL) {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00004302#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00004303 fprintf(stderr, "xmlDocDump : document == NULL\n");
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00004304#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00004305 return;
4306 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004307 buf = xmlBufferCreate();
4308 if (buf == NULL) return;
4309 xmlDocContentDump(buf, cur);
4310 xmlBufferDump(f, buf);
4311 xmlBufferFree(buf);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004312}
4313
Daniel Veillard97b58771998-10-20 06:14:16 +00004314/**
4315 * xmlSaveFile:
4316 * @filename: the filename
4317 * @cur: the document
4318 *
4319 * Dump an XML document to a file. Will use compression if
Daniel Veillard11a48ec1999-11-23 10:40:46 +00004320 * compiled in and enabled. If @filename is "-" the stdout file is
4321 * used.
Daniel Veillard97b58771998-10-20 06:14:16 +00004322 * returns: the number of file written or -1 in case of failure.
Daniel Veillard151b1b01998-09-23 00:49:46 +00004323 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004324int
4325xmlSaveFile(const char *filename, xmlDocPtr cur) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004326 xmlBufferPtr buf;
Daniel Veillard151b1b01998-09-23 00:49:46 +00004327#ifdef HAVE_ZLIB_H
4328 gzFile zoutput = NULL;
4329 char mode[15];
4330#endif
Daniel Veillardccb09631998-10-27 06:21:04 +00004331 FILE *output = NULL;
Daniel Veillard151b1b01998-09-23 00:49:46 +00004332 int ret;
4333
Daniel Veillard5099ae81999-04-21 20:12:07 +00004334 /*
4335 * save the content to a temp buffer.
4336 */
4337 buf = xmlBufferCreate();
4338 if (buf == NULL) return(0);
4339 xmlDocContentDump(buf, cur);
4340
Daniel Veillard151b1b01998-09-23 00:49:46 +00004341#ifdef HAVE_ZLIB_H
Daniel Veillard11a48ec1999-11-23 10:40:46 +00004342 if (cur->compression < 0) cur->compression = xmlCompressMode;
Daniel Veillarddc3dd9d1998-09-24 19:25:54 +00004343 if ((cur->compression > 0) && (cur->compression <= 9)) {
4344 sprintf(mode, "w%d", cur->compression);
Daniel Veillard11a48ec1999-11-23 10:40:46 +00004345 if (!strcmp(filename, "-"))
4346 zoutput = gzdopen(1, mode);
4347 else
4348 zoutput = gzopen(filename, mode);
Daniel Veillard151b1b01998-09-23 00:49:46 +00004349 }
4350 if (zoutput == NULL) {
4351#endif
4352 output = fopen(filename, "w");
Daniel Veillard10a2c651999-12-12 13:03:50 +00004353 if (output == NULL) {
4354 xmlBufferFree(buf);
4355 return(-1);
4356 }
Daniel Veillard151b1b01998-09-23 00:49:46 +00004357#ifdef HAVE_ZLIB_H
4358 }
Daniel Veillard151b1b01998-09-23 00:49:46 +00004359
Daniel Veillard151b1b01998-09-23 00:49:46 +00004360 if (zoutput != NULL) {
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004361 ret = gzwrite(zoutput, buf->content, sizeof(xmlChar) * buf->use);
Daniel Veillard151b1b01998-09-23 00:49:46 +00004362 gzclose(zoutput);
Daniel Veillard5099ae81999-04-21 20:12:07 +00004363 } else {
4364#endif
4365 ret = xmlBufferDump(output, buf);
4366 fclose(output);
4367#ifdef HAVE_ZLIB_H
Daniel Veillard151b1b01998-09-23 00:49:46 +00004368 }
4369#endif
Manish Vachharajani5e60f5a1999-05-29 03:04:30 +00004370 xmlBufferFree(buf);
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004371 return(ret * sizeof(xmlChar));
Daniel Veillard151b1b01998-09-23 00:49:46 +00004372}
4373