blob: 425b45433b98c6fa95f400dd37d5c94de7c29d07 [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 Veillard0142b842000-01-14 14:45:24 +0000163 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
164 (!xmlStrcmp(prev->prefix, cur->prefix))) {
165 xmlFreeNs(cur);
166 return(NULL);
167 }
Daniel Veillard686d6b62000-01-03 11:08:02 +0000168 while (prev->next != NULL) {
Daniel Veillard0142b842000-01-14 14:45:24 +0000169 prev = prev->next;
Daniel Veillard686d6b62000-01-03 11:08:02 +0000170 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
171 (!xmlStrcmp(prev->prefix, cur->prefix))) {
172 xmlFreeNs(cur);
173 return(NULL);
174 }
Daniel Veillard686d6b62000-01-03 11:08:02 +0000175 }
Daniel Veillard260a68f1998-08-13 03:39:55 +0000176 prev->next = cur;
177 }
178 }
Daniel Veillard260a68f1998-08-13 03:39:55 +0000179 return(cur);
180}
181
Daniel Veillard97b58771998-10-20 06:14:16 +0000182/**
183 * xmlNewGlobalNs:
184 * @doc: the document carrying the namespace
185 * @href: the URI associated
186 * @prefix: the prefix for the namespace
187 *
Daniel Veillard686d6b62000-01-03 11:08:02 +0000188 * Creation of a Namespace, the old way using PI and without scoping
189 * DEPRECATED !!!
Daniel Veillard0142b842000-01-14 14:45:24 +0000190 * It now create a namespace on the root element of the document if found.
Daniel Veillard686d6b62000-01-03 11:08:02 +0000191 * Returns NULL this functionnality had been removed
Daniel Veillard260a68f1998-08-13 03:39:55 +0000192 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000193xmlNsPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000194xmlNewGlobalNs(xmlDocPtr doc, const xmlChar *href, const xmlChar *prefix) {
Daniel Veillard0142b842000-01-14 14:45:24 +0000195 xmlNodePtr root;
196
197 xmlNsPtr cur;
198
199 root = xmlDocGetRootElement(doc);
200 if (root != NULL)
201 return(xmlNewNs(root, href, prefix));
202
203 /*
204 * if there is no root element yet, create an old Namespace type
205 * and it will be moved to the root at save time.
206 */
207 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
208 if (cur == NULL) {
209 fprintf(stderr, "xmlNewGlobalNs : malloc failed\n");
210 return(NULL);
211 }
212
213 cur->type = XML_GLOBAL_NAMESPACE;
214 if (href != NULL)
215 cur->href = xmlStrdup(href);
216 else
217 cur->href = NULL;
218 if (prefix != NULL)
219 cur->prefix = xmlStrdup(prefix);
220 else
221 cur->prefix = NULL;
222
223 /*
224 * Add it at the end to preserve parsing order ...
225 */
226 cur->next = NULL;
227 if (doc != NULL) {
228 if (doc->oldNs == NULL) {
229 doc->oldNs = cur;
230 } else {
231 xmlNsPtr prev = doc->oldNs;
232
233 while (prev->next != NULL) prev = prev->next;
234 prev->next = cur;
235 }
236 }
237
238 return(NULL);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000239}
240
Daniel Veillard97b58771998-10-20 06:14:16 +0000241/**
242 * xmlSetNs:
243 * @node: a node in the document
244 * @ns: a namespace pointer
245 *
246 * Associate a namespace to a node, a posteriori.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000247 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000248void
249xmlSetNs(xmlNodePtr node, xmlNsPtr ns) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000250 if (node == NULL) {
251 fprintf(stderr, "xmlSetNs: node == NULL\n");
252 return;
253 }
254 node->ns = ns;
255}
256
Daniel Veillard97b58771998-10-20 06:14:16 +0000257/**
258 * xmlFreeNs:
259 * @cur: the namespace pointer
260 *
261 * Free up the structures associated to a namespace
Daniel Veillard260a68f1998-08-13 03:39:55 +0000262 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000263void
264xmlFreeNs(xmlNsPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000265 if (cur == NULL) {
266 fprintf(stderr, "xmlFreeNs : ns == NULL\n");
267 return;
268 }
Daniel Veillard6454aec1999-09-02 22:04:43 +0000269 if (cur->href != NULL) xmlFree((char *) cur->href);
270 if (cur->prefix != NULL) xmlFree((char *) cur->prefix);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000271 memset(cur, -1, sizeof(xmlNs));
Daniel Veillard6454aec1999-09-02 22:04:43 +0000272 xmlFree(cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000273}
274
Daniel Veillard97b58771998-10-20 06:14:16 +0000275/**
276 * xmlFreeNsList:
277 * @cur: the first namespace pointer
278 *
279 * Free up all the structures associated to the chained namespaces.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000280 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000281void
282xmlFreeNsList(xmlNsPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000283 xmlNsPtr next;
284 if (cur == NULL) {
285 fprintf(stderr, "xmlFreeNsList : ns == NULL\n");
286 return;
287 }
288 while (cur != NULL) {
289 next = cur->next;
290 xmlFreeNs(cur);
291 cur = next;
292 }
293}
294
Daniel Veillard97b58771998-10-20 06:14:16 +0000295/**
296 * xmlNewDtd:
297 * @doc: the document pointer
298 * @name: the DTD name
299 * @ExternalID: the external ID
300 * @SystemID: the system ID
301 *
Daniel Veillard260a68f1998-08-13 03:39:55 +0000302 * Creation of a new DTD.
Daniel Veillard1e346af1999-02-22 10:33:01 +0000303 * Returns a pointer to the new DTD structure
Daniel Veillard260a68f1998-08-13 03:39:55 +0000304 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000305xmlDtdPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000306xmlNewDtd(xmlDocPtr doc, const xmlChar *name,
307 const xmlChar *ExternalID, const xmlChar *SystemID) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000308 xmlDtdPtr cur;
309
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000310 if ((doc != NULL) && (doc->extSubset != NULL)) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000311 fprintf(stderr, "xmlNewDtd(%s): document %s already have a DTD %s\n",
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000312 /* !!! */ (char *) name, doc->name,
313 /* !!! */ (char *)doc->extSubset->name);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000314 }
315
316 /*
317 * Allocate a new DTD and fill the fields.
318 */
Daniel Veillard6454aec1999-09-02 22:04:43 +0000319 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
Daniel Veillard260a68f1998-08-13 03:39:55 +0000320 if (cur == NULL) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000321 fprintf(stderr, "xmlNewDtd : malloc failed\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +0000322 return(NULL);
323 }
324
325 if (name != NULL)
326 cur->name = xmlStrdup(name);
327 else
328 cur->name = NULL;
329 if (ExternalID != NULL)
330 cur->ExternalID = xmlStrdup(ExternalID);
331 else
332 cur->ExternalID = NULL;
333 if (SystemID != NULL)
334 cur->SystemID = xmlStrdup(SystemID);
335 else
336 cur->SystemID = NULL;
Daniel Veillard1e346af1999-02-22 10:33:01 +0000337 cur->notations = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000338 cur->elements = NULL;
Daniel Veillard1e346af1999-02-22 10:33:01 +0000339 cur->attributes = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000340 cur->entities = NULL;
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000341 if (doc != NULL)
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000342 doc->extSubset = cur;
343
344 return(cur);
345}
346
347/**
348 * xmlCreateIntSubset:
349 * @doc: the document pointer
350 * @name: the DTD name
351 * @ExternalID: the external ID
352 * @SystemID: the system ID
353 *
Daniel Veillard1e346af1999-02-22 10:33:01 +0000354 * Create the internal subset of a document
355 * Returns a pointer to the new DTD structure
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000356 */
357xmlDtdPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000358xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name,
359 const xmlChar *ExternalID, const xmlChar *SystemID) {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000360 xmlDtdPtr cur;
361
362 if ((doc != NULL) && (doc->intSubset != NULL)) {
363 fprintf(stderr,
364 "xmlCreateIntSubset(): document %s already have an internal subset\n",
365 doc->name);
366 return(NULL);
367 }
368
369 /*
370 * Allocate a new DTD and fill the fields.
371 */
Daniel Veillard6454aec1999-09-02 22:04:43 +0000372 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000373 if (cur == NULL) {
374 fprintf(stderr, "xmlNewDtd : malloc failed\n");
375 return(NULL);
376 }
377
378 if (name != NULL)
379 cur->name = xmlStrdup(name);
380 else
381 cur->name = NULL;
382 if (ExternalID != NULL)
383 cur->ExternalID = xmlStrdup(ExternalID);
384 else
385 cur->ExternalID = NULL;
386 if (SystemID != NULL)
387 cur->SystemID = xmlStrdup(SystemID);
388 else
389 cur->SystemID = NULL;
Daniel Veillard1e346af1999-02-22 10:33:01 +0000390 cur->notations = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000391 cur->elements = NULL;
Daniel Veillard1e346af1999-02-22 10:33:01 +0000392 cur->attributes = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000393 cur->entities = NULL;
394 if (doc != NULL)
395 doc->intSubset = cur;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000396
397 return(cur);
398}
399
Daniel Veillard97b58771998-10-20 06:14:16 +0000400/**
401 * xmlFreeDtd:
402 * @cur: the DTD structure to free up
403 *
404 * Free a DTD structure.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000405 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000406void
407xmlFreeDtd(xmlDtdPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000408 if (cur == NULL) {
409 fprintf(stderr, "xmlFreeDtd : DTD == NULL\n");
410 return;
411 }
Daniel Veillard6454aec1999-09-02 22:04:43 +0000412 if (cur->name != NULL) xmlFree((char *) cur->name);
413 if (cur->SystemID != NULL) xmlFree((char *) cur->SystemID);
414 if (cur->ExternalID != NULL) xmlFree((char *) cur->ExternalID);
Daniel Veillard1e346af1999-02-22 10:33:01 +0000415 if (cur->notations != NULL)
416 xmlFreeNotationTable((xmlNotationTablePtr) cur->notations);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000417 if (cur->elements != NULL)
Daniel Veillard3b9def11999-01-31 22:15:06 +0000418 xmlFreeElementTable((xmlElementTablePtr) cur->elements);
Daniel Veillard1e346af1999-02-22 10:33:01 +0000419 if (cur->attributes != NULL)
420 xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000421 if (cur->entities != NULL)
422 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities);
423 memset(cur, -1, sizeof(xmlDtd));
Daniel Veillard6454aec1999-09-02 22:04:43 +0000424 xmlFree(cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000425}
426
Daniel Veillard97b58771998-10-20 06:14:16 +0000427/**
428 * xmlNewDoc:
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000429 * @version: xmlChar string giving the version of XML "1.0"
Daniel Veillard97b58771998-10-20 06:14:16 +0000430 *
Daniel Veillard1e346af1999-02-22 10:33:01 +0000431 * Returns a new document
Daniel Veillard260a68f1998-08-13 03:39:55 +0000432 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000433xmlDocPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000434xmlNewDoc(const xmlChar *version) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000435 xmlDocPtr cur;
436
437 if (version == NULL) {
438 fprintf(stderr, "xmlNewDoc : version == NULL\n");
439 return(NULL);
440 }
441
442 /*
443 * Allocate a new document and fill the fields.
444 */
Daniel Veillard6454aec1999-09-02 22:04:43 +0000445 cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
Daniel Veillard260a68f1998-08-13 03:39:55 +0000446 if (cur == NULL) {
447 fprintf(stderr, "xmlNewDoc : malloc failed\n");
448 return(NULL);
449 }
450
Daniel Veillard33942841998-10-18 19:12:41 +0000451 cur->type = XML_DOCUMENT_NODE;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000452 cur->version = xmlStrdup(version);
453 cur->name = NULL;
454 cur->root = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000455 cur->intSubset = NULL;
456 cur->extSubset = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000457 cur->oldNs = NULL;
458 cur->encoding = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000459 cur->standalone = -1;
Daniel Veillard11a48ec1999-11-23 10:40:46 +0000460 cur->compression = -1; /* not initialized */
Daniel Veillardb96e6431999-08-29 21:02:19 +0000461 cur->ids = NULL;
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000462 cur->refs = NULL;
Daniel Veillard27d88741999-05-29 11:51:49 +0000463#ifndef XML_WITHOUT_CORBA
Daniel Veillard27fb0751998-10-17 06:47:46 +0000464 cur->_private = NULL;
465 cur->vepv = NULL;
466#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +0000467 return(cur);
468}
469
Daniel Veillard97b58771998-10-20 06:14:16 +0000470/**
471 * xmlFreeDoc:
472 * @cur: pointer to the document
473 * @:
474 *
475 * Free up all the structures used by a document, tree included.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000476 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000477void
478xmlFreeDoc(xmlDocPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000479 if (cur == NULL) {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000480#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +0000481 fprintf(stderr, "xmlFreeDoc : document == NULL\n");
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000482#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +0000483 return;
484 }
Daniel Veillard6454aec1999-09-02 22:04:43 +0000485 if (cur->version != NULL) xmlFree((char *) cur->version);
486 if (cur->name != NULL) xmlFree((char *) cur->name);
487 if (cur->encoding != NULL) xmlFree((char *) cur->encoding);
488 if (cur->root != NULL) xmlFreeNodeList(cur->root);
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000489 if (cur->intSubset != NULL) xmlFreeDtd(cur->intSubset);
490 if (cur->extSubset != NULL) xmlFreeDtd(cur->extSubset);
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000491 if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
Daniel Veillardb96e6431999-08-29 21:02:19 +0000492 if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids);
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000493 if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000494 memset(cur, -1, sizeof(xmlDoc));
Daniel Veillard6454aec1999-09-02 22:04:43 +0000495 xmlFree(cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000496}
497
Daniel Veillard97b58771998-10-20 06:14:16 +0000498/**
Daniel Veillard16253641998-10-28 22:58:05 +0000499 * xmlStringLenGetNodeList:
500 * @doc: the document
501 * @value: the value of the text
Daniel Veillard1e346af1999-02-22 10:33:01 +0000502 * @len: the length of the string value
Daniel Veillard16253641998-10-28 22:58:05 +0000503 *
504 * Parse the value string and build the node list associated. Should
505 * produce a flat tree with only TEXTs and ENTITY_REFs.
Daniel Veillard1e346af1999-02-22 10:33:01 +0000506 * Returns a pointer to the first child
Daniel Veillard16253641998-10-28 22:58:05 +0000507 */
508xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000509xmlStringLenGetNodeList(xmlDocPtr doc, const xmlChar *value, int len) {
Daniel Veillard16253641998-10-28 22:58:05 +0000510 xmlNodePtr ret = NULL, last = NULL;
511 xmlNodePtr node;
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000512 xmlChar *val;
513 const xmlChar *cur = value;
514 const xmlChar *q;
Daniel Veillard25940b71998-10-29 05:51:30 +0000515 xmlEntityPtr ent;
Daniel Veillard16253641998-10-28 22:58:05 +0000516
517 if (value == NULL) return(NULL);
518
519 q = cur;
520 while ((*cur != 0) && (cur - value < len)) {
521 if (*cur == '&') {
Daniel Veillard25940b71998-10-29 05:51:30 +0000522 /*
523 * Save the current text.
524 */
Daniel Veillard16253641998-10-28 22:58:05 +0000525 if (cur != q) {
Daniel Veillard25940b71998-10-29 05:51:30 +0000526 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
527 xmlNodeAddContentLen(last, q, cur - q);
528 } else {
529 node = xmlNewDocTextLen(doc, q, cur - q);
530 if (node == NULL) return(ret);
531 if (last == NULL)
532 last = ret = node;
533 else {
534 last->next = node;
535 node->prev = last;
536 last = node;
537 }
Daniel Veillard16253641998-10-28 22:58:05 +0000538 }
539 }
Daniel Veillard25940b71998-10-29 05:51:30 +0000540 /*
541 * Read the entity string
542 */
Daniel Veillard16253641998-10-28 22:58:05 +0000543 cur++;
544 q = cur;
545 while ((*cur != 0) && (cur - value < len) && (*cur != ';')) cur++;
546 if ((*cur == 0) || (cur - value >= len)) {
547 fprintf(stderr,
Daniel Veillard011b63c1999-06-02 17:44:04 +0000548 "xmlStringLenGetNodeList: unterminated entity %30s\n", q);
Daniel Veillard16253641998-10-28 22:58:05 +0000549 return(ret);
550 }
551 if (cur != q) {
Daniel Veillard25940b71998-10-29 05:51:30 +0000552 /*
553 * Predefined entities don't generate nodes
554 */
Daniel Veillard16253641998-10-28 22:58:05 +0000555 val = xmlStrndup(q, cur - q);
Daniel Veillard25940b71998-10-29 05:51:30 +0000556 ent = xmlGetDocEntity(doc, val);
557 if ((ent != NULL) &&
558 (ent->type == XML_INTERNAL_PREDEFINED_ENTITY)) {
559 if (last == NULL) {
560 node = xmlNewDocText(doc, ent->content);
561 last = ret = node;
562 } else
563 xmlNodeAddContent(last, ent->content);
564
565 } else {
566 /*
567 * Create a new REFERENCE_REF node
568 */
569 node = xmlNewReference(doc, val);
Daniel Veillard242590e1998-11-13 18:04:35 +0000570 if (node == NULL) {
Daniel Veillard6454aec1999-09-02 22:04:43 +0000571 if (val != NULL) xmlFree(val);
Daniel Veillard242590e1998-11-13 18:04:35 +0000572 return(ret);
573 }
Daniel Veillard25940b71998-10-29 05:51:30 +0000574 if (last == NULL)
575 last = ret = node;
576 else {
577 last->next = node;
578 node->prev = last;
579 last = node;
580 }
Daniel Veillard16253641998-10-28 22:58:05 +0000581 }
Daniel Veillard6454aec1999-09-02 22:04:43 +0000582 xmlFree(val);
Daniel Veillard16253641998-10-28 22:58:05 +0000583 }
584 cur++;
585 q = cur;
586 } else
587 cur++;
588 }
589 if (cur != q) {
Daniel Veillard25940b71998-10-29 05:51:30 +0000590 /*
591 * Handle the last piece of text.
592 */
593 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
594 xmlNodeAddContentLen(last, q, cur - q);
595 } else {
596 node = xmlNewDocTextLen(doc, q, cur - q);
597 if (node == NULL) return(ret);
598 if (last == NULL)
599 last = ret = node;
600 else {
601 last->next = node;
602 node->prev = last;
603 last = node;
604 }
Daniel Veillard16253641998-10-28 22:58:05 +0000605 }
606 }
607 return(ret);
608}
609
610/**
Daniel Veillardccb09631998-10-27 06:21:04 +0000611 * xmlStringGetNodeList:
612 * @doc: the document
613 * @value: the value of the attribute
614 *
615 * Parse the value string and build the node list associated. Should
616 * produce a flat tree with only TEXTs and ENTITY_REFs.
Daniel Veillard1e346af1999-02-22 10:33:01 +0000617 * Returns a pointer to the first child
Daniel Veillardccb09631998-10-27 06:21:04 +0000618 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000619xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000620xmlStringGetNodeList(xmlDocPtr doc, const xmlChar *value) {
Daniel Veillardccb09631998-10-27 06:21:04 +0000621 xmlNodePtr ret = NULL, last = NULL;
622 xmlNodePtr node;
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000623 xmlChar *val;
624 const xmlChar *cur = value;
625 const xmlChar *q;
Daniel Veillard25940b71998-10-29 05:51:30 +0000626 xmlEntityPtr ent;
Daniel Veillardccb09631998-10-27 06:21:04 +0000627
628 if (value == NULL) return(NULL);
629
630 q = cur;
631 while (*cur != 0) {
632 if (*cur == '&') {
Daniel Veillard25940b71998-10-29 05:51:30 +0000633 /*
634 * Save the current text.
635 */
Daniel Veillardccb09631998-10-27 06:21:04 +0000636 if (cur != q) {
Daniel Veillard25940b71998-10-29 05:51:30 +0000637 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
638 xmlNodeAddContentLen(last, q, cur - q);
639 } else {
640 node = xmlNewDocTextLen(doc, q, cur - q);
641 if (node == NULL) return(ret);
642 if (last == NULL)
643 last = ret = node;
644 else {
645 last->next = node;
646 node->prev = last;
647 last = node;
648 }
Daniel Veillardccb09631998-10-27 06:21:04 +0000649 }
650 }
Daniel Veillard25940b71998-10-29 05:51:30 +0000651 /*
652 * Read the entity string
653 */
Daniel Veillardccb09631998-10-27 06:21:04 +0000654 cur++;
655 q = cur;
656 while ((*cur != 0) && (*cur != ';')) cur++;
657 if (*cur == 0) {
658 fprintf(stderr,
659 "xmlStringGetNodeList: unterminated entity %30s\n", q);
660 return(ret);
661 }
662 if (cur != q) {
Daniel Veillard25940b71998-10-29 05:51:30 +0000663 /*
664 * Predefined entities don't generate nodes
665 */
Daniel Veillardccb09631998-10-27 06:21:04 +0000666 val = xmlStrndup(q, cur - q);
Daniel Veillard25940b71998-10-29 05:51:30 +0000667 ent = xmlGetDocEntity(doc, val);
668 if ((ent != NULL) &&
669 (ent->type == XML_INTERNAL_PREDEFINED_ENTITY)) {
670 if (last == NULL) {
671 node = xmlNewDocText(doc, ent->content);
672 last = ret = node;
673 } else
674 xmlNodeAddContent(last, ent->content);
675
676 } else {
677 /*
678 * Create a new REFERENCE_REF node
679 */
Daniel Veillard25940b71998-10-29 05:51:30 +0000680 node = xmlNewReference(doc, val);
Daniel Veillard242590e1998-11-13 18:04:35 +0000681 if (node == NULL) {
Daniel Veillard6454aec1999-09-02 22:04:43 +0000682 if (val != NULL) xmlFree(val);
Daniel Veillard242590e1998-11-13 18:04:35 +0000683 return(ret);
684 }
Daniel Veillard25940b71998-10-29 05:51:30 +0000685 if (last == NULL)
686 last = ret = node;
687 else {
688 last->next = node;
689 node->prev = last;
690 last = node;
691 }
Daniel Veillardccb09631998-10-27 06:21:04 +0000692 }
Daniel Veillard6454aec1999-09-02 22:04:43 +0000693 xmlFree(val);
Daniel Veillardccb09631998-10-27 06:21:04 +0000694 }
695 cur++;
696 q = cur;
697 } else
698 cur++;
699 }
700 if (cur != q) {
Daniel Veillard25940b71998-10-29 05:51:30 +0000701 /*
702 * Handle the last piece of text.
703 */
704 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
705 xmlNodeAddContentLen(last, q, cur - q);
706 } else {
707 node = xmlNewDocTextLen(doc, q, cur - q);
708 if (node == NULL) return(ret);
709 if (last == NULL)
710 last = ret = node;
711 else {
712 last->next = node;
713 node->prev = last;
714 last = node;
715 }
Daniel Veillardccb09631998-10-27 06:21:04 +0000716 }
717 }
718 return(ret);
719}
720
721/**
722 * xmlNodeListGetString:
723 * @doc: the document
724 * @list: a Node list
725 * @inLine: should we replace entity contents or show their external form
726 *
727 * Returns the string equivalent to the text contained in the Node list
728 * made of TEXTs and ENTITY_REFs
Daniel Veillard1e346af1999-02-22 10:33:01 +0000729 * Returns a pointer to the string copy, the calller must free it.
Daniel Veillardccb09631998-10-27 06:21:04 +0000730 */
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000731xmlChar *
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000732xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine) {
Daniel Veillardccb09631998-10-27 06:21:04 +0000733 xmlNodePtr node = list;
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000734 xmlChar *ret = NULL;
Daniel Veillardccb09631998-10-27 06:21:04 +0000735 xmlEntityPtr ent;
736
737 if (list == NULL) return(NULL);
738
739 while (node != NULL) {
740 if (node->type == XML_TEXT_NODE) {
Daniel Veillard71b656e2000-01-05 14:46:17 +0000741 if (inLine) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000742#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardccb09631998-10-27 06:21:04 +0000743 ret = xmlStrcat(ret, node->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000744#else
745 ret = xmlStrcat(ret, xmlBufferContent(node->content));
746#endif
747 } else {
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000748 xmlChar *buffer;
Daniel Veillard14fff061999-06-22 21:49:07 +0000749
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000750#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard14fff061999-06-22 21:49:07 +0000751 buffer = xmlEncodeEntitiesReentrant(doc, node->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000752#else
753 buffer = xmlEncodeEntitiesReentrant(doc,
754 xmlBufferContent(node->content));
755#endif
Daniel Veillard14fff061999-06-22 21:49:07 +0000756 if (buffer != NULL) {
757 ret = xmlStrcat(ret, buffer);
Daniel Veillard6454aec1999-09-02 22:04:43 +0000758 xmlFree(buffer);
Daniel Veillard14fff061999-06-22 21:49:07 +0000759 }
760 }
Daniel Veillardccb09631998-10-27 06:21:04 +0000761 } else if (node->type == XML_ENTITY_REF_NODE) {
762 if (inLine) {
763 ent = xmlGetDocEntity(doc, node->name);
764 if (ent != NULL)
765 ret = xmlStrcat(ret, ent->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000766 else {
767#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardccb09631998-10-27 06:21:04 +0000768 ret = xmlStrcat(ret, node->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000769#else
770 ret = xmlStrcat(ret, xmlBufferContent(node->content));
771#endif
772 }
Daniel Veillardccb09631998-10-27 06:21:04 +0000773 } else {
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000774 xmlChar buf[2];
Daniel Veillardccb09631998-10-27 06:21:04 +0000775 buf[0] = '&'; buf[1] = 0;
776 ret = xmlStrncat(ret, buf, 1);
777 ret = xmlStrcat(ret, node->name);
778 buf[0] = ';'; buf[1] = 0;
779 ret = xmlStrncat(ret, buf, 1);
780 }
781 }
782#if 0
783 else {
784 fprintf(stderr, "xmlGetNodeListString : invalide node type %d\n",
785 node->type);
786 }
787#endif
788 node = node->next;
789 }
790 return(ret);
791}
792
793/**
Daniel Veillard97b58771998-10-20 06:14:16 +0000794 * xmlNewProp:
795 * @node: the holding node
796 * @name: the name of the attribute
797 * @value: the value of the attribute
798 *
799 * Create a new property carried by a node.
Daniel Veillard1e346af1999-02-22 10:33:01 +0000800 * Returns a pointer to the attribute
Daniel Veillard260a68f1998-08-13 03:39:55 +0000801 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000802xmlAttrPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000803xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000804 xmlAttrPtr cur;
805
806 if (name == NULL) {
807 fprintf(stderr, "xmlNewProp : name == NULL\n");
808 return(NULL);
809 }
810
811 /*
812 * Allocate a new property and fill the fields.
813 */
Daniel Veillard6454aec1999-09-02 22:04:43 +0000814 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
Daniel Veillard260a68f1998-08-13 03:39:55 +0000815 if (cur == NULL) {
816 fprintf(stderr, "xmlNewProp : malloc failed\n");
817 return(NULL);
818 }
819
Daniel Veillard33942841998-10-18 19:12:41 +0000820 cur->type = XML_ATTRIBUTE_NODE;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000821 cur->node = node;
Daniel Veillardb96e6431999-08-29 21:02:19 +0000822 cur->ns = NULL;
823 cur->name = xmlStrdup(name);
Daniel Veillard51e3b151999-11-12 17:02:31 +0000824 if (value != NULL) {
825 xmlChar *buffer;
826 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
827 cur->val = xmlStringGetNodeList(node->doc, buffer);
828 xmlFree(buffer);
829 }
Daniel Veillardb96e6431999-08-29 21:02:19 +0000830 else
831 cur->val = NULL;
832#ifndef XML_WITHOUT_CORBA
833 cur->_private = NULL;
834 cur->vepv = NULL;
835#endif
836
837 /*
838 * Add it at the end to preserve parsing order ...
839 */
840 cur->next = NULL;
841 if (node != NULL) {
842 if (node->properties == NULL) {
843 node->properties = cur;
844 } else {
845 xmlAttrPtr prev = node->properties;
846
847 while (prev->next != NULL) prev = prev->next;
848 prev->next = cur;
849 }
850 }
Daniel Veillard00fdf371999-10-08 09:40:39 +0000851#ifndef XML_WITHOUT_CORBA
852 cur->_private = NULL;
853 cur->vepv = NULL;
854#endif
Daniel Veillardb96e6431999-08-29 21:02:19 +0000855 return(cur);
856}
857
858/**
859 * xmlNewNsProp:
860 * @node: the holding node
861 * @ns: the namespace
862 * @name: the name of the attribute
863 * @value: the value of the attribute
864 *
865 * Create a new property tagged with a namespace and carried by a node.
866 * Returns a pointer to the attribute
867 */
868xmlAttrPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000869xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
870 const xmlChar *value) {
Daniel Veillardb96e6431999-08-29 21:02:19 +0000871 xmlAttrPtr cur;
872
873 if (name == NULL) {
874 fprintf(stderr, "xmlNewProp : name == NULL\n");
875 return(NULL);
876 }
877
878 /*
879 * Allocate a new property and fill the fields.
880 */
Daniel Veillard6454aec1999-09-02 22:04:43 +0000881 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
Daniel Veillardb96e6431999-08-29 21:02:19 +0000882 if (cur == NULL) {
883 fprintf(stderr, "xmlNewProp : malloc failed\n");
884 return(NULL);
885 }
886
887 cur->type = XML_ATTRIBUTE_NODE;
888 cur->node = node;
889 cur->ns = ns;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000890 cur->name = xmlStrdup(name);
891 if (value != NULL)
Daniel Veillardccb09631998-10-27 06:21:04 +0000892 cur->val = xmlStringGetNodeList(node->doc, value);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000893 else
Daniel Veillardccb09631998-10-27 06:21:04 +0000894 cur->val = NULL;
Daniel Veillard27d88741999-05-29 11:51:49 +0000895#ifndef XML_WITHOUT_CORBA
Daniel Veillard27fb0751998-10-17 06:47:46 +0000896 cur->_private = NULL;
897 cur->vepv = NULL;
898#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +0000899
900 /*
901 * Add it at the end to preserve parsing order ...
902 */
903 cur->next = NULL;
904 if (node != NULL) {
905 if (node->properties == NULL) {
906 node->properties = cur;
907 } else {
908 xmlAttrPtr prev = node->properties;
909
910 while (prev->next != NULL) prev = prev->next;
911 prev->next = cur;
912 }
913 }
914 return(cur);
915}
916
Daniel Veillard97b58771998-10-20 06:14:16 +0000917/**
Daniel Veillardccb09631998-10-27 06:21:04 +0000918 * xmlNewDocProp:
919 * @doc: the document
920 * @name: the name of the attribute
921 * @value: the value of the attribute
922 *
923 * Create a new property carried by a document.
Daniel Veillard1e346af1999-02-22 10:33:01 +0000924 * Returns a pointer to the attribute
Daniel Veillardccb09631998-10-27 06:21:04 +0000925 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000926xmlAttrPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000927xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) {
Daniel Veillardccb09631998-10-27 06:21:04 +0000928 xmlAttrPtr cur;
929
930 if (name == NULL) {
931 fprintf(stderr, "xmlNewProp : name == NULL\n");
932 return(NULL);
933 }
934
935 /*
936 * Allocate a new property and fill the fields.
937 */
Daniel Veillard6454aec1999-09-02 22:04:43 +0000938 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
Daniel Veillardccb09631998-10-27 06:21:04 +0000939 if (cur == NULL) {
940 fprintf(stderr, "xmlNewProp : malloc failed\n");
941 return(NULL);
942 }
943
944 cur->type = XML_ATTRIBUTE_NODE;
945 cur->node = NULL;
946 cur->name = xmlStrdup(name);
947 if (value != NULL)
948 cur->val = xmlStringGetNodeList(doc, value);
949 else
950 cur->val = NULL;
Daniel Veillard27d88741999-05-29 11:51:49 +0000951#ifndef XML_WITHOUT_CORBA
Daniel Veillardccb09631998-10-27 06:21:04 +0000952 cur->_private = NULL;
953 cur->vepv = NULL;
954#endif
955
956 cur->next = NULL;
957 return(cur);
958}
959
960/**
Daniel Veillard97b58771998-10-20 06:14:16 +0000961 * xmlFreePropList:
962 * @cur: the first property in the list
963 *
964 * Free a property and all its siblings, all the childs are freed too.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000965 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000966void
967xmlFreePropList(xmlAttrPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000968 xmlAttrPtr next;
969 if (cur == NULL) {
970 fprintf(stderr, "xmlFreePropList : property == NULL\n");
971 return;
972 }
973 while (cur != NULL) {
974 next = cur->next;
975 xmlFreeProp(cur);
976 cur = next;
977 }
978}
979
Daniel Veillard97b58771998-10-20 06:14:16 +0000980/**
981 * xmlFreeProp:
Daniel Veillard686d6b62000-01-03 11:08:02 +0000982 * @cur: an attribute
Daniel Veillard97b58771998-10-20 06:14:16 +0000983 *
Daniel Veillard686d6b62000-01-03 11:08:02 +0000984 * Free one attribute, all the content is freed too
Daniel Veillard260a68f1998-08-13 03:39:55 +0000985 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000986void
987xmlFreeProp(xmlAttrPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000988 if (cur == NULL) {
989 fprintf(stderr, "xmlFreeProp : property == NULL\n");
990 return;
991 }
Daniel Veillard71b656e2000-01-05 14:46:17 +0000992 /* Check for ID removal -> leading to invalid references ! */
993 if ((cur->node != NULL) &&
994 (xmlIsID(cur->node->doc, cur->node, cur)))
995 xmlRemoveID(cur->node->doc, cur);
Daniel Veillard6454aec1999-09-02 22:04:43 +0000996 if (cur->name != NULL) xmlFree((char *) cur->name);
Daniel Veillardccb09631998-10-27 06:21:04 +0000997 if (cur->val != NULL) xmlFreeNodeList(cur->val);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000998 memset(cur, -1, sizeof(xmlAttr));
Daniel Veillard6454aec1999-09-02 22:04:43 +0000999 xmlFree(cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +00001000}
1001
Daniel Veillard97b58771998-10-20 06:14:16 +00001002/**
Daniel Veillard686d6b62000-01-03 11:08:02 +00001003 * xmlRemoveProp:
1004 * @cur: an attribute
1005 *
1006 * Unlink and free one attribute, all the content is freed too
1007 * Note this doesn't work for namespace definition attributes
1008 *
1009 * Returns 0 if success and -1 in case of error.
1010 */
1011int
1012xmlRemoveProp(xmlAttrPtr cur) {
1013 xmlAttrPtr tmp;
1014 if (cur == NULL) {
1015 fprintf(stderr, "xmlRemoveProp : cur == NULL\n");
1016 return(-1);
1017 }
1018 if (cur->node == NULL) {
1019 fprintf(stderr, "xmlRemoveProp : cur->node == NULL\n");
1020 return(-1);
1021 }
1022 tmp = cur->node->properties;
1023 if (tmp == cur) {
1024 cur->node->properties = cur->next;
1025 xmlFreeProp(cur);
1026 return(0);
1027 }
1028 while (tmp != NULL) {
1029 if (tmp->next == cur) {
1030 tmp->next = cur->next;
1031 xmlFreeProp(cur);
1032 return(0);
1033 }
1034 tmp = tmp->next;
1035 }
1036 fprintf(stderr, "xmlRemoveProp : attribute not owned by its node\n");
1037 return(-1);
1038}
1039
1040/**
Daniel Veillardb96e6431999-08-29 21:02:19 +00001041 * xmlNewPI:
1042 * @name: the processing instruction name
1043 * @content: the PI content
1044 *
1045 * Creation of a processing instruction element.
1046 * Returns a pointer to the new node object.
1047 */
1048xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001049xmlNewPI(const xmlChar *name, const xmlChar *content) {
Daniel Veillardb96e6431999-08-29 21:02:19 +00001050 xmlNodePtr cur;
1051
1052 if (name == NULL) {
1053 fprintf(stderr, "xmlNewPI : name == NULL\n");
1054 return(NULL);
1055 }
1056
1057 /*
1058 * Allocate a new node and fill the fields.
1059 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00001060 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillardb96e6431999-08-29 21:02:19 +00001061 if (cur == NULL) {
1062 fprintf(stderr, "xmlNewPI : malloc failed\n");
1063 return(NULL);
1064 }
1065
1066 cur->type = XML_PI_NODE;
1067 cur->doc = NULL;
1068 cur->parent = NULL;
1069 cur->next = NULL;
1070 cur->prev = NULL;
1071 cur->childs = NULL;
1072 cur->last = NULL;
1073 cur->properties = NULL;
1074 cur->name = xmlStrdup(name);
1075 cur->ns = NULL;
1076 cur->nsDef = NULL;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001077 if (content != NULL) {
1078#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardb96e6431999-08-29 21:02:19 +00001079 cur->content = xmlStrdup(content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001080#else
1081 cur->content = xmlBufferCreateSize(0);
1082 xmlBufferSetAllocationScheme(cur->content,
1083 xmlGetBufferAllocationScheme());
1084 xmlBufferAdd(cur->content, content, -1);
1085#endif
1086 } else
Daniel Veillardb96e6431999-08-29 21:02:19 +00001087 cur->content = NULL;
1088#ifndef XML_WITHOUT_CORBA
1089 cur->_private = NULL;
1090 cur->vepv = NULL;
1091#endif
1092 return(cur);
1093}
1094
1095/**
Daniel Veillard97b58771998-10-20 06:14:16 +00001096 * xmlNewNode:
1097 * @ns: namespace if any
1098 * @name: the node name
Daniel Veillard97b58771998-10-20 06:14:16 +00001099 *
1100 * Creation of a new node element. @ns and @content are optionnal (NULL).
Daniel Veillardccb09631998-10-27 06:21:04 +00001101 * If content is non NULL, a child list containing the TEXTs and
1102 * ENTITY_REFs node will be created.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001103 * Returns a pointer to the new node object.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001104 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001105xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001106xmlNewNode(xmlNsPtr ns, const xmlChar *name) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001107 xmlNodePtr cur;
1108
1109 if (name == NULL) {
1110 fprintf(stderr, "xmlNewNode : name == NULL\n");
1111 return(NULL);
1112 }
1113
1114 /*
1115 * Allocate a new node and fill the fields.
1116 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00001117 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillard260a68f1998-08-13 03:39:55 +00001118 if (cur == NULL) {
1119 fprintf(stderr, "xmlNewNode : malloc failed\n");
1120 return(NULL);
1121 }
1122
Daniel Veillard33942841998-10-18 19:12:41 +00001123 cur->type = XML_ELEMENT_NODE;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001124 cur->doc = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001125 cur->parent = NULL;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001126 cur->next = NULL;
1127 cur->prev = NULL;
1128 cur->childs = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001129 cur->last = NULL;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001130 cur->properties = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001131 cur->name = xmlStrdup(name);
1132 cur->ns = ns;
1133 cur->nsDef = NULL;
Daniel Veillardccb09631998-10-27 06:21:04 +00001134 cur->content = NULL;
Daniel Veillard27d88741999-05-29 11:51:49 +00001135#ifndef XML_WITHOUT_CORBA
Daniel Veillard27fb0751998-10-17 06:47:46 +00001136 cur->_private = NULL;
1137 cur->vepv = NULL;
1138#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00001139 return(cur);
1140}
1141
Daniel Veillard97b58771998-10-20 06:14:16 +00001142/**
1143 * xmlNewDocNode:
1144 * @doc: the document
1145 * @ns: namespace if any
1146 * @name: the node name
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001147 * @content: the XML text content if any
Daniel Veillard97b58771998-10-20 06:14:16 +00001148 *
1149 * Creation of a new node element within a document. @ns and @content
1150 * are optionnal (NULL).
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001151 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
1152 * references, but XML special chars need to be escaped first by using
1153 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
1154 * need entities support.
1155 *
Daniel Veillard1e346af1999-02-22 10:33:01 +00001156 * Returns a pointer to the new node object.
Daniel Veillard97b58771998-10-20 06:14:16 +00001157 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001158xmlNodePtr
1159xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns,
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001160 const xmlChar *name, const xmlChar *content) {
Daniel Veillard0bef1311998-10-14 02:36:47 +00001161 xmlNodePtr cur;
1162
Daniel Veillardccb09631998-10-27 06:21:04 +00001163 cur = xmlNewNode(ns, name);
1164 if (cur != NULL) {
1165 cur->doc = doc;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001166 if (content != NULL) {
Daniel Veillardccb09631998-10-27 06:21:04 +00001167 cur->childs = xmlStringGetNodeList(doc, content);
Daniel Veillard1e346af1999-02-22 10:33:01 +00001168 UPDATE_LAST_CHILD(cur)
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001169 }
Daniel Veillardccb09631998-10-27 06:21:04 +00001170 }
Daniel Veillard0bef1311998-10-14 02:36:47 +00001171 return(cur);
1172}
1173
1174
Daniel Veillard97b58771998-10-20 06:14:16 +00001175/**
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001176 * xmlNewDocRawNode:
1177 * @doc: the document
1178 * @ns: namespace if any
1179 * @name: the node name
1180 * @content: the text content if any
1181 *
1182 * Creation of a new node element within a document. @ns and @content
1183 * are optionnal (NULL).
1184 *
1185 * Returns a pointer to the new node object.
1186 */
1187xmlNodePtr
1188xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns,
1189 const xmlChar *name, const xmlChar *content) {
1190 xmlNodePtr cur;
1191
1192 cur = xmlNewNode(ns, name);
1193 if (cur != NULL) {
1194 cur->doc = doc;
1195 if (content != NULL) {
1196 cur->childs = xmlNewDocText(doc, content);
1197 UPDATE_LAST_CHILD(cur)
1198 }
1199 }
1200 return(cur);
1201}
1202
Daniel Veillard2eac5032000-01-09 21:08:56 +00001203/**
1204 * xmlNewDocFragment:
1205 * @doc: the document owning the fragment
1206 *
1207 * Creation of a new Fragment node.
1208 * Returns a pointer to the new node object.
1209 */
1210xmlNodePtr
1211xmlNewDocFragment(xmlDocPtr doc) {
1212 xmlNodePtr cur;
1213
1214 /*
1215 * Allocate a new DocumentFragment node and fill the fields.
1216 */
1217 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1218 if (cur == NULL) {
1219 fprintf(stderr, "xmlNewDocFragment : malloc failed\n");
1220 return(NULL);
1221 }
1222
1223 cur->type = XML_DOCUMENT_FRAG_NODE;
1224 cur->doc = doc;
1225 cur->parent = NULL;
1226 cur->next = NULL;
1227 cur->prev = NULL;
1228 cur->childs = NULL;
1229 cur->last = NULL;
1230 cur->properties = NULL;
1231 cur->name = NULL;
1232 cur->ns = NULL;
1233 cur->nsDef = NULL;
1234 cur->content = NULL;
1235#ifndef XML_WITHOUT_CORBA
1236 cur->_private = NULL;
1237 cur->vepv = NULL;
1238#endif
1239 return(cur);
1240}
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001241
1242/**
Daniel Veillard97b58771998-10-20 06:14:16 +00001243 * xmlNewText:
1244 * @content: the text content
1245 *
1246 * Creation of a new text node.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001247 * Returns a pointer to the new node object.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001248 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001249xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001250xmlNewText(const xmlChar *content) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001251 xmlNodePtr cur;
1252
1253 /*
1254 * Allocate a new node and fill the fields.
1255 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00001256 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillard260a68f1998-08-13 03:39:55 +00001257 if (cur == NULL) {
1258 fprintf(stderr, "xmlNewText : malloc failed\n");
1259 return(NULL);
1260 }
1261
Daniel Veillard33942841998-10-18 19:12:41 +00001262 cur->type = XML_TEXT_NODE;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001263 cur->doc = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001264 cur->parent = NULL;
1265 cur->next = NULL;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001266 cur->prev = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001267 cur->childs = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001268 cur->last = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001269 cur->properties = NULL;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001270 cur->type = XML_TEXT_NODE;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001271 cur->name = xmlStrdup(xmlStringText);
1272 cur->ns = NULL;
1273 cur->nsDef = NULL;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001274 if (content != NULL) {
1275#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard260a68f1998-08-13 03:39:55 +00001276 cur->content = xmlStrdup(content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001277#else
1278 cur->content = xmlBufferCreateSize(0);
1279 xmlBufferSetAllocationScheme(cur->content,
1280 xmlGetBufferAllocationScheme());
1281 xmlBufferAdd(cur->content, content, -1);
1282#endif
1283 } else
Daniel Veillard260a68f1998-08-13 03:39:55 +00001284 cur->content = NULL;
Daniel Veillard00fdf371999-10-08 09:40:39 +00001285#ifndef XML_WITHOUT_CORBA
1286 cur->_private = NULL;
1287 cur->vepv = NULL;
1288#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00001289 return(cur);
1290}
1291
Daniel Veillard97b58771998-10-20 06:14:16 +00001292/**
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001293 * xmlNewTextChild:
1294 * @parent: the parent node
1295 * @ns: a namespace if any
1296 * @name: the name of the child
1297 * @content: the text content of the child if any.
1298 *
1299 * Creation of a new child element, added at the end of @parent childs list.
1300 * @ns and @content parameters are optionnal (NULL). If content is non NULL,
1301 * a child TEXT node will be created containing the string content.
1302 *
1303 * Returns a pointer to the new node object.
1304 */
1305xmlNodePtr
1306xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns,
1307 const xmlChar *name, const xmlChar *content) {
1308 xmlNodePtr cur, prev;
1309
1310 if (parent == NULL) {
1311 fprintf(stderr, "xmlNewTextChild : parent == NULL\n");
1312 return(NULL);
1313 }
1314
1315 if (name == NULL) {
1316 fprintf(stderr, "xmlNewTextChild : name == NULL\n");
1317 return(NULL);
1318 }
1319
1320 /*
1321 * Allocate a new node
1322 */
1323 if (ns == NULL)
1324 cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content);
1325 else
1326 cur = xmlNewDocRawNode(parent->doc, ns, name, content);
1327 if (cur == NULL) return(NULL);
1328
1329 /*
1330 * add the new element at the end of the childs list.
1331 */
1332 cur->type = XML_ELEMENT_NODE;
1333 cur->parent = parent;
1334 cur->doc = parent->doc;
1335 if (parent->childs == NULL) {
1336 parent->childs = cur;
1337 parent->last = cur;
1338 } else {
1339 prev = parent->last;
1340 prev->next = cur;
1341 cur->prev = prev;
1342 parent->last = cur;
1343 }
1344
1345 return(cur);
1346}
1347
1348/**
Daniel Veillardccb09631998-10-27 06:21:04 +00001349 * xmlNewReference:
1350 * @doc: the document
1351 * @name: the reference name, or the reference string with & and ;
1352 *
1353 * Creation of a new reference node.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001354 * Returns a pointer to the new node object.
Daniel Veillardccb09631998-10-27 06:21:04 +00001355 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001356xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001357xmlNewReference(xmlDocPtr doc, const xmlChar *name) {
Daniel Veillardccb09631998-10-27 06:21:04 +00001358 xmlNodePtr cur;
1359 xmlEntityPtr ent;
1360
1361 /*
1362 * Allocate a new node and fill the fields.
1363 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00001364 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillardccb09631998-10-27 06:21:04 +00001365 if (cur == NULL) {
1366 fprintf(stderr, "xmlNewText : malloc failed\n");
1367 return(NULL);
1368 }
1369
1370 cur->type = XML_ENTITY_REF_NODE;
Daniel Veillard10c6a8f1998-10-28 01:00:12 +00001371 cur->doc = doc;
Daniel Veillardccb09631998-10-27 06:21:04 +00001372 cur->parent = NULL;
1373 cur->next = NULL;
1374 cur->prev = NULL;
1375 cur->childs = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001376 cur->last = NULL;
Daniel Veillardccb09631998-10-27 06:21:04 +00001377 cur->properties = NULL;
1378 if (name[0] == '&') {
1379 int len;
1380 name++;
1381 len = xmlStrlen(name);
1382 if (name[len - 1] == ';')
1383 cur->name = xmlStrndup(name, len - 1);
1384 else
1385 cur->name = xmlStrndup(name, len);
1386 } else
1387 cur->name = xmlStrdup(name);
1388 cur->ns = NULL;
1389 cur->nsDef = NULL;
1390
1391 ent = xmlGetDocEntity(doc, cur->name);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001392 if (ent != NULL) {
1393#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardccb09631998-10-27 06:21:04 +00001394 cur->content = ent->content;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001395#else
1396 /*
1397 * CJN 11.18.99 this might be a problem, since the xmlBuffer gets
1398 * a copy of this pointer. Let's hope we don't manipulate it
1399 * later
1400 */
1401 cur->content = xmlBufferCreateSize(0);
1402 xmlBufferSetAllocationScheme(cur->content,
1403 xmlGetBufferAllocationScheme());
1404 if (ent->content != NULL)
1405 xmlBufferAdd(cur->content, ent->content, -1);
1406#endif
1407 } else
Daniel Veillardccb09631998-10-27 06:21:04 +00001408 cur->content = NULL;
Daniel Veillard00fdf371999-10-08 09:40:39 +00001409#ifndef XML_WITHOUT_CORBA
1410 cur->_private = NULL;
1411 cur->vepv = NULL;
1412#endif
Daniel Veillardccb09631998-10-27 06:21:04 +00001413 return(cur);
1414}
1415
1416/**
Daniel Veillard97b58771998-10-20 06:14:16 +00001417 * xmlNewDocText:
1418 * @doc: the document
1419 * @content: the text content
1420 *
1421 * Creation of a new text node within a document.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001422 * Returns a pointer to the new node object.
Daniel Veillard97b58771998-10-20 06:14:16 +00001423 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001424xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001425xmlNewDocText(xmlDocPtr doc, const xmlChar *content) {
Daniel Veillard0bef1311998-10-14 02:36:47 +00001426 xmlNodePtr cur;
1427
1428 cur = xmlNewText(content);
1429 if (cur != NULL) cur->doc = doc;
1430 return(cur);
1431}
1432
Daniel Veillard97b58771998-10-20 06:14:16 +00001433/**
Daniel Veillardccb09631998-10-27 06:21:04 +00001434 * xmlNewTextLen:
Daniel Veillard97b58771998-10-20 06:14:16 +00001435 * @content: the text content
1436 * @len: the text len.
1437 *
1438 * Creation of a new text node with an extra parameter for the content's lenght
Daniel Veillard1e346af1999-02-22 10:33:01 +00001439 * Returns a pointer to the new node object.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001440 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001441xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001442xmlNewTextLen(const xmlChar *content, int len) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001443 xmlNodePtr cur;
1444
1445 /*
1446 * Allocate a new node and fill the fields.
1447 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00001448 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillard260a68f1998-08-13 03:39:55 +00001449 if (cur == NULL) {
1450 fprintf(stderr, "xmlNewText : malloc failed\n");
1451 return(NULL);
1452 }
1453
Daniel Veillard33942841998-10-18 19:12:41 +00001454 cur->type = XML_TEXT_NODE;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001455 cur->doc = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001456 cur->parent = NULL;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001457 cur->prev = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001458 cur->next = NULL;
1459 cur->childs = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001460 cur->last = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001461 cur->properties = NULL;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001462 cur->type = XML_TEXT_NODE;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001463 cur->name = xmlStrdup(xmlStringText);
1464 cur->ns = NULL;
1465 cur->nsDef = NULL;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001466 if (content != NULL) {
1467#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard260a68f1998-08-13 03:39:55 +00001468 cur->content = xmlStrndup(content, len);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001469#else
1470 cur->content = xmlBufferCreateSize(len);
1471 xmlBufferSetAllocationScheme(cur->content,
1472 xmlGetBufferAllocationScheme());
1473 xmlBufferAdd(cur->content, content, len);
1474#endif
1475 } else
Daniel Veillard260a68f1998-08-13 03:39:55 +00001476 cur->content = NULL;
Daniel Veillard00fdf371999-10-08 09:40:39 +00001477#ifndef XML_WITHOUT_CORBA
1478 cur->_private = NULL;
1479 cur->vepv = NULL;
1480#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00001481 return(cur);
1482}
1483
Daniel Veillard97b58771998-10-20 06:14:16 +00001484/**
1485 * xmlNewDocTextLen:
1486 * @doc: the document
1487 * @content: the text content
1488 * @len: the text len.
1489 *
1490 * Creation of a new text node with an extra content lenght parameter. The
1491 * text node pertain to a given document.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001492 * Returns a pointer to the new node object.
Daniel Veillard97b58771998-10-20 06:14:16 +00001493 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001494xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001495xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) {
Daniel Veillard0bef1311998-10-14 02:36:47 +00001496 xmlNodePtr cur;
1497
1498 cur = xmlNewTextLen(content, len);
1499 if (cur != NULL) cur->doc = doc;
1500 return(cur);
1501}
1502
Daniel Veillard97b58771998-10-20 06:14:16 +00001503/**
1504 * xmlNewComment:
1505 * @content: the comment content
1506 *
1507 * Creation of a new node containing a comment.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001508 * Returns a pointer to the new node object.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001509 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001510xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001511xmlNewComment(const xmlChar *content) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001512 xmlNodePtr cur;
1513
1514 /*
1515 * Allocate a new node and fill the fields.
1516 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00001517 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillard260a68f1998-08-13 03:39:55 +00001518 if (cur == NULL) {
1519 fprintf(stderr, "xmlNewComment : malloc failed\n");
1520 return(NULL);
1521 }
1522
Daniel Veillard33942841998-10-18 19:12:41 +00001523 cur->type = XML_COMMENT_NODE;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001524 cur->doc = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001525 cur->parent = NULL;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001526 cur->prev = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001527 cur->next = NULL;
1528 cur->childs = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001529 cur->last = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001530 cur->properties = NULL;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001531 cur->type = XML_COMMENT_NODE;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001532 cur->name = xmlStrdup(xmlStringText);
1533 cur->ns = NULL;
1534 cur->nsDef = NULL;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001535 if (content != NULL) {
1536#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard260a68f1998-08-13 03:39:55 +00001537 cur->content = xmlStrdup(content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001538#else
1539 cur->content = xmlBufferCreateSize(0);
1540 xmlBufferSetAllocationScheme(cur->content,
1541 xmlGetBufferAllocationScheme());
1542 xmlBufferAdd(cur->content, content, -1);
1543#endif
1544 } else
Daniel Veillard260a68f1998-08-13 03:39:55 +00001545 cur->content = NULL;
Daniel Veillard00fdf371999-10-08 09:40:39 +00001546#ifndef XML_WITHOUT_CORBA
1547 cur->_private = NULL;
1548 cur->vepv = NULL;
1549#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00001550 return(cur);
1551}
1552
Daniel Veillard97b58771998-10-20 06:14:16 +00001553/**
Daniel Veillardb05deb71999-08-10 19:04:08 +00001554 * xmlNewCDataBlock:
1555 * @doc: the document
1556 * @content: the CData block content content
1557 * @len: the length of the block
1558 *
1559 * Creation of a new node containing a CData block.
1560 * Returns a pointer to the new node object.
1561 */
1562xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001563xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) {
Daniel Veillardb05deb71999-08-10 19:04:08 +00001564 xmlNodePtr cur;
1565
1566 /*
1567 * Allocate a new node and fill the fields.
1568 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00001569 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillardb05deb71999-08-10 19:04:08 +00001570 if (cur == NULL) {
1571 fprintf(stderr, "xmlNewCDataBlock : malloc failed\n");
1572 return(NULL);
1573 }
1574
1575 cur->type = XML_CDATA_SECTION_NODE;
1576 cur->doc = NULL;
1577 cur->parent = NULL;
1578 cur->prev = NULL;
1579 cur->next = NULL;
1580 cur->childs = NULL;
1581 cur->last = NULL;
1582 cur->properties = NULL;
1583 cur->name = xmlStrdup(xmlStringText);
1584 cur->ns = NULL;
1585 cur->nsDef = NULL;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001586 if (content != NULL) {
1587#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardb05deb71999-08-10 19:04:08 +00001588 cur->content = xmlStrndup(content, len);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001589#else
1590 cur->content = xmlBufferCreateSize(len);
1591 xmlBufferSetAllocationScheme(cur->content,
1592 xmlGetBufferAllocationScheme());
1593 xmlBufferAdd(cur->content, content, len);
1594#endif
Daniel Veillardb05deb71999-08-10 19:04:08 +00001595 } else
1596 cur->content = NULL;
Daniel Veillard00fdf371999-10-08 09:40:39 +00001597#ifndef XML_WITHOUT_CORBA
1598 cur->_private = NULL;
1599 cur->vepv = NULL;
1600#endif
Daniel Veillardb05deb71999-08-10 19:04:08 +00001601 return(cur);
1602}
1603
1604/**
Daniel Veillard1e346af1999-02-22 10:33:01 +00001605 * xmlNewDocComment:
Daniel Veillard97b58771998-10-20 06:14:16 +00001606 * @doc: the document
1607 * @content: the comment content
1608 *
1609 * Creation of a new node containing a commentwithin a document.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001610 * Returns a pointer to the new node object.
Daniel Veillard97b58771998-10-20 06:14:16 +00001611 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001612xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001613xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) {
Daniel Veillard0bef1311998-10-14 02:36:47 +00001614 xmlNodePtr cur;
1615
1616 cur = xmlNewComment(content);
1617 if (cur != NULL) cur->doc = doc;
1618 return(cur);
1619}
1620
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001621
Daniel Veillard97b58771998-10-20 06:14:16 +00001622/**
1623 * xmlNewChild:
1624 * @parent: the parent node
1625 * @ns: a namespace if any
1626 * @name: the name of the child
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001627 * @content: the XML content of the child if any.
Daniel Veillard97b58771998-10-20 06:14:16 +00001628 *
Daniel Veillard97b58771998-10-20 06:14:16 +00001629 * Creation of a new child element, added at the end of @parent childs list.
Daniel Veillardccb09631998-10-27 06:21:04 +00001630 * @ns and @content parameters are optionnal (NULL). If content is non NULL,
1631 * a child list containing the TEXTs and ENTITY_REFs node will be created.
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001632 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
1633 * references, but XML special chars need to be escaped first by using
1634 * xmlEncodeEntitiesReentrant(). Use xmlNewTextChild() if entities
1635 * support is not needed.
1636 *
Daniel Veillard1e346af1999-02-22 10:33:01 +00001637 * Returns a pointer to the new node object.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001638 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001639xmlNodePtr
1640xmlNewChild(xmlNodePtr parent, xmlNsPtr ns,
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001641 const xmlChar *name, const xmlChar *content) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001642 xmlNodePtr cur, prev;
1643
1644 if (parent == NULL) {
1645 fprintf(stderr, "xmlNewChild : parent == NULL\n");
1646 return(NULL);
1647 }
1648
1649 if (name == NULL) {
1650 fprintf(stderr, "xmlNewChild : name == NULL\n");
1651 return(NULL);
1652 }
1653
1654 /*
1655 * Allocate a new node
1656 */
1657 if (ns == NULL)
Daniel Veillardccb09631998-10-27 06:21:04 +00001658 cur = xmlNewDocNode(parent->doc, parent->ns, name, content);
Daniel Veillard260a68f1998-08-13 03:39:55 +00001659 else
Daniel Veillardccb09631998-10-27 06:21:04 +00001660 cur = xmlNewDocNode(parent->doc, ns, name, content);
Daniel Veillard260a68f1998-08-13 03:39:55 +00001661 if (cur == NULL) return(NULL);
1662
1663 /*
1664 * add the new element at the end of the childs list.
1665 */
Daniel Veillardccb09631998-10-27 06:21:04 +00001666 cur->type = XML_ELEMENT_NODE;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001667 cur->parent = parent;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001668 cur->doc = parent->doc;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001669 if (parent->childs == NULL) {
1670 parent->childs = cur;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001671 parent->last = cur;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001672 } else {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001673 prev = parent->last;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001674 prev->next = cur;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001675 cur->prev = prev;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001676 parent->last = cur;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001677 }
1678
1679 return(cur);
1680}
1681
Daniel Veillard97b58771998-10-20 06:14:16 +00001682/**
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001683 * xmlAddNextSibling:
1684 * @cur: the child node
1685 * @elem: the new node
1686 *
1687 * Add a new element @elem as the next siblings of @cur
1688 * If the new element was already inserted in a document it is
1689 * first unlinked from its existing context.
1690 *
1691 * Returns the new element or NULL in case of error.
1692 */
1693xmlNodePtr
1694xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) {
1695 if (cur == NULL) {
1696 fprintf(stderr, "xmlAddNextSibling : cur == NULL\n");
1697 return(NULL);
1698 }
1699 if (elem == NULL) {
1700 fprintf(stderr, "xmlAddNextSibling : elem == NULL\n");
1701 return(NULL);
1702 }
1703
1704 xmlUnlinkNode(elem);
1705 elem->doc = cur->doc;
1706 elem->parent = cur->parent;
1707 elem->next = cur;
1708 elem->prev = cur->prev;
1709 cur->prev = elem;
1710 if (elem->prev != NULL)
1711 elem->prev->next = elem;
1712 if ((elem->parent != NULL) && (elem->parent->childs == cur))
1713 elem->parent->childs = elem;
1714 return(elem);
1715}
1716
1717/**
1718 * xmlAddPrevSibling:
1719 * @cur: the child node
1720 * @elem: the new node
1721 *
1722 * Add a new element @elem as the previous siblings of @cur
1723 * If the new element was already inserted in a document it is
1724 * first unlinked from its existing context.
1725 *
1726 * Returns the new element or NULL in case of error.
1727 */
1728xmlNodePtr
1729xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) {
1730 if (cur == NULL) {
1731 fprintf(stderr, "xmlAddPrevSibling : cur == NULL\n");
1732 return(NULL);
1733 }
1734 if (elem == NULL) {
1735 fprintf(stderr, "xmlAddPrevSibling : elem == NULL\n");
1736 return(NULL);
1737 }
1738
1739 xmlUnlinkNode(elem);
1740 elem->doc = cur->doc;
1741 elem->parent = cur->parent;
1742 elem->prev = cur;
1743 elem->next = cur->next;
1744 cur->next = elem;
1745 if (elem->next != NULL)
1746 elem->next->prev = elem;
1747 if ((elem->parent != NULL) && (elem->parent->last == cur))
1748 elem->parent->last = elem;
1749 return(elem);
1750}
1751
1752/**
Daniel Veillardb96e6431999-08-29 21:02:19 +00001753 * xmlAddSibling:
1754 * @cur: the child node
1755 * @elem: the new node
1756 *
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001757 * Add a new element @elem to the list of siblings of @cur
1758 * If the new element was already inserted in a document it is
1759 * first unlinked from its existing context.
1760 *
1761 * Returns the new element or NULL in case of error.
Daniel Veillardb96e6431999-08-29 21:02:19 +00001762 */
1763xmlNodePtr
1764xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) {
1765 xmlNodePtr parent;
1766
1767 if (cur == NULL) {
1768 fprintf(stderr, "xmlAddSibling : cur == NULL\n");
1769 return(NULL);
1770 }
1771
1772 if (elem == NULL) {
1773 fprintf(stderr, "xmlAddSibling : elem == NULL\n");
1774 return(NULL);
1775 }
1776
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001777 /*
1778 * Constant time is we can rely on the ->parent->last to find
1779 * the last sibling.
1780 */
1781 if ((cur->parent != NULL) &&
1782 (cur->parent->childs != NULL) &&
1783 (cur->parent->last != NULL) &&
1784 (cur->parent->last->next == NULL)) {
1785 cur = cur->parent->last;
1786 } else {
1787 while (cur->next != NULL) cur = cur->next;
Daniel Veillardb96e6431999-08-29 21:02:19 +00001788 }
1789
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001790 xmlUnlinkNode(elem);
Daniel Veillardb96e6431999-08-29 21:02:19 +00001791 if (elem->doc == NULL)
1792 elem->doc = cur->doc; /* the parent may not be linked to a doc ! */
1793
1794 parent = cur->parent;
1795 elem->prev = cur;
1796 elem->next = NULL;
1797 elem->parent = parent;
1798 cur->next = elem;
1799 if (parent != NULL)
1800 parent->last = elem;
1801
1802 return(elem);
1803}
1804
1805/**
Daniel Veillard97b58771998-10-20 06:14:16 +00001806 * xmlAddChild:
1807 * @parent: the parent node
1808 * @cur: the child node
1809 *
1810 * Add a new child element, to @parent, at the end of the child list.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001811 * Returns the child or NULL in case of error.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001812 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001813xmlNodePtr
1814xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001815 xmlNodePtr prev;
1816
1817 if (parent == NULL) {
Daniel Veillard10a2c651999-12-12 13:03:50 +00001818 fprintf(stderr, "xmlAddChild : parent == NULL\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +00001819 return(NULL);
1820 }
1821
1822 if (cur == NULL) {
Daniel Veillard10a2c651999-12-12 13:03:50 +00001823 fprintf(stderr, "xmlAddChild : child == NULL\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +00001824 return(NULL);
1825 }
1826
Daniel Veillard0bef1311998-10-14 02:36:47 +00001827 if ((cur->doc != NULL) && (parent->doc != NULL) &&
1828 (cur->doc != parent->doc)) {
1829 fprintf(stderr, "Elements moved to a different document\n");
1830 }
1831
Daniel Veillard260a68f1998-08-13 03:39:55 +00001832 /*
1833 * add the new element at the end of the childs list.
1834 */
1835 cur->parent = parent;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001836 cur->doc = parent->doc; /* the parent may not be linked to a doc ! */
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001837
Daniel Veillardccb09631998-10-27 06:21:04 +00001838 /*
1839 * Handle the case where parent->content != NULL, in that case it will
1840 * create a intermediate TEXT node.
1841 */
1842 if (parent->content != NULL) {
1843 xmlNodePtr text;
1844
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001845#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardccb09631998-10-27 06:21:04 +00001846 text = xmlNewDocText(parent->doc, parent->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001847#else
1848 text = xmlNewDocText(parent->doc, xmlBufferContent(parent->content));
1849#endif
Daniel Veillardccb09631998-10-27 06:21:04 +00001850 if (text != NULL) {
1851 text->next = parent->childs;
1852 if (text->next != NULL)
1853 text->next->prev = text;
1854 parent->childs = text;
Daniel Veillard1e346af1999-02-22 10:33:01 +00001855 UPDATE_LAST_CHILD(parent)
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001856#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard6454aec1999-09-02 22:04:43 +00001857 xmlFree(parent->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001858#else
1859 xmlBufferFree(parent->content);
1860#endif
Daniel Veillardccb09631998-10-27 06:21:04 +00001861 parent->content = NULL;
1862 }
1863 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00001864 if (parent->childs == NULL) {
1865 parent->childs = cur;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001866 parent->last = cur;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001867 } else {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001868 prev = parent->last;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001869 prev->next = cur;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001870 cur->prev = prev;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001871 parent->last = cur;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001872 }
1873
1874 return(cur);
1875}
1876
Daniel Veillard97b58771998-10-20 06:14:16 +00001877/**
1878 * xmlGetLastChild:
1879 * @parent: the parent node
1880 *
1881 * Search the last child of a node.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001882 * Returns the last child or NULL if none.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001883 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001884xmlNodePtr
1885xmlGetLastChild(xmlNodePtr parent) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001886 if (parent == NULL) {
1887 fprintf(stderr, "xmlGetLastChild : parent == NULL\n");
1888 return(NULL);
1889 }
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001890 return(parent->last);
Daniel Veillard260a68f1998-08-13 03:39:55 +00001891}
1892
Daniel Veillard97b58771998-10-20 06:14:16 +00001893/**
1894 * xmlFreeNodeList:
1895 * @cur: the first node in the list
1896 *
1897 * Free a node and all its siblings, this is a recursive behaviour, all
1898 * the childs are freed too.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001899 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001900void
1901xmlFreeNodeList(xmlNodePtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001902 xmlNodePtr next;
1903 if (cur == NULL) {
1904 fprintf(stderr, "xmlFreeNodeList : node == NULL\n");
1905 return;
1906 }
1907 while (cur != NULL) {
1908 next = cur->next;
1909 xmlFreeNode(cur);
1910 cur = next;
1911 }
1912}
1913
Daniel Veillard97b58771998-10-20 06:14:16 +00001914/**
1915 * xmlFreeNode:
1916 * @cur: the node
1917 *
1918 * Free a node, this is a recursive behaviour, all the childs are freed too.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001919 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001920void
1921xmlFreeNode(xmlNodePtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001922 if (cur == NULL) {
1923 fprintf(stderr, "xmlFreeNode : node == NULL\n");
1924 return;
1925 }
Daniel Veillardccb09631998-10-27 06:21:04 +00001926 cur->doc = NULL;
1927 cur->parent = NULL;
1928 cur->next = NULL;
1929 cur->prev = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001930 if (cur->childs != NULL) xmlFreeNodeList(cur->childs);
Daniel Veillardccb09631998-10-27 06:21:04 +00001931 if (cur->properties != NULL) xmlFreePropList(cur->properties);
1932 if (cur->type != XML_ENTITY_REF_NODE)
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001933#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard6454aec1999-09-02 22:04:43 +00001934 if (cur->content != NULL) xmlFree(cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001935#else
1936 if (cur->content != NULL) xmlBufferFree(cur->content);
1937#endif
Daniel Veillard6454aec1999-09-02 22:04:43 +00001938 if (cur->name != NULL) xmlFree((char *) cur->name);
Daniel Veillard260a68f1998-08-13 03:39:55 +00001939 if (cur->nsDef != NULL) xmlFreeNsList(cur->nsDef);
1940 memset(cur, -1, sizeof(xmlNode));
Daniel Veillard6454aec1999-09-02 22:04:43 +00001941 xmlFree(cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +00001942}
1943
Daniel Veillard16253641998-10-28 22:58:05 +00001944/**
1945 * xmlUnlinkNode:
1946 * @cur: the node
1947 *
1948 * Unlink a node from it's current context, the node is not freed
1949 */
1950void
1951xmlUnlinkNode(xmlNodePtr cur) {
1952 if (cur == NULL) {
1953 fprintf(stderr, "xmlUnlinkNode : node == NULL\n");
1954 return;
1955 }
1956 if ((cur->parent != NULL) && (cur->parent->childs == cur))
1957 cur->parent->childs = cur->next;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001958 if ((cur->parent != NULL) && (cur->parent->last == cur))
1959 cur->parent->last = cur->prev;
Daniel Veillard16253641998-10-28 22:58:05 +00001960 if (cur->next != NULL)
1961 cur->next->prev = cur->prev;
1962 if (cur->prev != NULL)
1963 cur->prev->next = cur->next;
1964 cur->next = cur->prev = NULL;
1965 cur->parent = NULL;
1966}
1967
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001968/**
1969 * xmlReplaceNode:
1970 * @old: the old node
1971 * @cur: the node
1972 *
1973 * Unlink the old node from it's current context, prune the new one
1974 * at the same place. If cur was already inserted in a document it is
1975 * first unlinked from its existing context.
1976 *
1977 * Returns the old node
1978 */
1979xmlNodePtr
1980xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) {
1981 if (old == NULL) {
1982 fprintf(stderr, "xmlReplaceNode : old == NULL\n");
1983 return(NULL);
1984 }
1985 if (cur == NULL) {
1986 xmlUnlinkNode(old);
1987 return(old);
1988 }
1989 xmlUnlinkNode(cur);
1990 cur->doc = old->doc;
1991 cur->parent = old->parent;
1992 cur->next = old->next;
1993 if (cur->next != NULL)
1994 cur->next->prev = cur;
1995 cur->prev = old->prev;
1996 if (cur->prev != NULL)
1997 cur->prev->next = cur;
1998 if (cur->parent != NULL) {
1999 if (cur->parent->childs == old)
2000 cur->parent->childs = cur;
2001 if (cur->parent->last == old)
2002 cur->parent->last = cur;
2003 }
2004 old->next = old->prev = NULL;
2005 old->parent = NULL;
2006 return(old);
2007}
2008
Daniel Veillard260a68f1998-08-13 03:39:55 +00002009/************************************************************************
2010 * *
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002011 * Copy operations *
2012 * *
2013 ************************************************************************/
2014
2015/**
2016 * xmlCopyNamespace:
2017 * @cur: the namespace
2018 *
2019 * Do a copy of the namespace.
2020 *
2021 * Returns: a new xmlNsPtr, or NULL in case of error.
2022 */
2023xmlNsPtr
2024xmlCopyNamespace(xmlNsPtr cur) {
2025 xmlNsPtr ret;
2026
2027 if (cur == NULL) return(NULL);
2028 switch (cur->type) {
2029 case XML_GLOBAL_NAMESPACE:
2030 ret = xmlNewGlobalNs(NULL, cur->href, cur->prefix);
2031 break;
2032 case XML_LOCAL_NAMESPACE:
2033 ret = xmlNewNs(NULL, cur->href, cur->prefix);
2034 break;
2035 default:
2036 fprintf(stderr, "xmlCopyNamespace: unknown type %d\n", cur->type);
2037 return(NULL);
2038 }
2039 return(ret);
2040}
2041
2042/**
2043 * xmlCopyNamespaceList:
2044 * @cur: the first namespace
2045 *
2046 * Do a copy of an namespace list.
2047 *
2048 * Returns: a new xmlNsPtr, or NULL in case of error.
2049 */
2050xmlNsPtr
2051xmlCopyNamespaceList(xmlNsPtr cur) {
2052 xmlNsPtr ret = NULL;
2053 xmlNsPtr p = NULL,q;
2054
2055 while (cur != NULL) {
2056 q = xmlCopyNamespace(cur);
2057 if (p == NULL) {
2058 ret = p = q;
2059 } else {
2060 p->next = q;
2061 p = q;
2062 }
2063 cur = cur->next;
2064 }
2065 return(ret);
2066}
2067
2068/**
2069 * xmlCopyProp:
Daniel Veillardb96e6431999-08-29 21:02:19 +00002070 * @target: the element where the attribute will be grafted
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002071 * @cur: the attribute
2072 *
2073 * Do a copy of the attribute.
2074 *
2075 * Returns: a new xmlAttrPtr, or NULL in case of error.
2076 */
2077xmlAttrPtr
Daniel Veillardb96e6431999-08-29 21:02:19 +00002078xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002079 xmlAttrPtr ret;
2080
2081 if (cur == NULL) return(NULL);
2082 if (cur->val != NULL)
2083 ret = xmlNewDocProp(cur->val->doc, cur->name, NULL);
2084 else
2085 ret = xmlNewDocProp(NULL, cur->name, NULL);
2086 if (ret == NULL) return(NULL);
Daniel Veillardb96e6431999-08-29 21:02:19 +00002087
2088 if ((cur->ns != NULL) && (target != NULL)) {
2089 xmlNsPtr ns;
2090
2091 ns = xmlSearchNs(target->doc, target, cur->ns->prefix);
2092 ret->ns = ns;
2093 } else
2094 ret->ns = NULL;
2095
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002096 if (cur->val != NULL)
2097 ret->val = xmlCopyNodeList(cur->val);
2098 return(ret);
2099}
2100
2101/**
2102 * xmlCopyPropList:
Daniel Veillardb96e6431999-08-29 21:02:19 +00002103 * @target: the element where the attributes will be grafted
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002104 * @cur: the first attribute
2105 *
2106 * Do a copy of an attribute list.
2107 *
2108 * Returns: a new xmlAttrPtr, or NULL in case of error.
2109 */
2110xmlAttrPtr
Daniel Veillardb96e6431999-08-29 21:02:19 +00002111xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002112 xmlAttrPtr ret = NULL;
2113 xmlAttrPtr p = NULL,q;
2114
2115 while (cur != NULL) {
Daniel Veillardb96e6431999-08-29 21:02:19 +00002116 q = xmlCopyProp(target, cur);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002117 if (p == NULL) {
2118 ret = p = q;
2119 } else {
2120 p->next = q;
2121 p = q;
2122 }
2123 cur = cur->next;
2124 }
2125 return(ret);
2126}
2127
2128/*
Daniel Veillard11a48ec1999-11-23 10:40:46 +00002129 * NOTE abeut the CopyNode operations !
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002130 *
2131 * They are splitted into external and internal parts for one
2132 * tricky reason: namespaces. Doing a direct copy of a node
2133 * say RPM:Copyright without changing the namespace pointer to
2134 * something else can produce stale links. One way to do it is
2135 * to keep a reference counter but this doesn't work as soon
2136 * as one move the element or the subtree out of the scope of
2137 * the existing namespace. The actual solution seems to add
2138 * a copy of the namespace at the top of the copied tree if
2139 * not available in the subtree.
2140 * Hence two functions, the public front-end call the inner ones
2141 */
2142
2143static xmlNodePtr
2144xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent);
2145
2146static xmlNodePtr
2147xmlStaticCopyNode(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent,
2148 int recursive) {
2149 xmlNodePtr ret;
2150
2151 if (node == NULL) return(NULL);
2152 /*
2153 * Allocate a new node and fill the fields.
2154 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00002155 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002156 if (ret == NULL) {
2157 fprintf(stderr, "xmlStaticCopyNode : malloc failed\n");
2158 return(NULL);
2159 }
2160
2161 ret->type = node->type;
2162 ret->doc = doc;
2163 ret->parent = parent;
2164 ret->next = NULL;
2165 ret->prev = NULL;
2166 ret->childs = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002167 ret->last = NULL;
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002168 ret->properties = NULL;
2169 if (node->name != NULL)
2170 ret->name = xmlStrdup(node->name);
2171 else
2172 ret->name = NULL;
2173 ret->ns = NULL;
2174 ret->nsDef = NULL;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002175 if ((node->content != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
2176#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002177 ret->content = xmlStrdup(node->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002178#else
2179 ret->content = xmlBufferCreateSize(xmlBufferLength(node->content));
2180 xmlBufferSetAllocationScheme(ret->content,
2181 xmlGetBufferAllocationScheme());
2182 xmlBufferAdd(ret->content,
2183 xmlBufferContent(node->content),
2184 xmlBufferLength(node->content));
2185#endif
2186 } else
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002187 ret->content = NULL;
Daniel Veillard27d88741999-05-29 11:51:49 +00002188#ifndef XML_WITHOUT_CORBA
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002189 ret->_private = NULL;
2190 ret->vepv = NULL;
2191#endif
2192 if (parent != NULL)
2193 xmlAddChild(parent, ret);
2194
2195 if (!recursive) return(ret);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002196 if (node->nsDef != NULL)
2197 ret->nsDef = xmlCopyNamespaceList(node->nsDef);
2198
2199 if (node->ns != NULL) {
2200 xmlNsPtr ns;
2201
2202 ns = xmlSearchNs(doc, ret, node->ns->prefix);
2203 if (ns == NULL) {
2204 /*
2205 * Humm, we are copying an element whose namespace is defined
2206 * out of the new tree scope. Search it in the original tree
2207 * and add it at the top of the new tree
2208 */
2209 ns = xmlSearchNs(node->doc, node, node->ns->prefix);
2210 if (ns != NULL) {
2211 xmlNodePtr root = ret;
2212
2213 while (root->parent != NULL) root = root->parent;
2214 xmlNewNs(root, ns->href, ns->prefix);
2215 }
2216 } else {
2217 /*
2218 * reference the existing namespace definition in our own tree.
2219 */
2220 ret->ns = ns;
2221 }
2222 }
Daniel Veillardb96e6431999-08-29 21:02:19 +00002223 if (node->properties != NULL)
2224 ret->properties = xmlCopyPropList(ret, node->properties);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002225 if (node->childs != NULL)
2226 ret->childs = xmlStaticCopyNodeList(node->childs, doc, ret);
Daniel Veillard1e346af1999-02-22 10:33:01 +00002227 UPDATE_LAST_CHILD(ret)
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002228 return(ret);
2229}
2230
2231static xmlNodePtr
2232xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) {
2233 xmlNodePtr ret = NULL;
2234 xmlNodePtr p = NULL,q;
2235
2236 while (node != NULL) {
2237 q = xmlStaticCopyNode(node, doc, parent, 1);
2238 if (parent == NULL) {
2239 if (ret == NULL) ret = q;
2240 } else {
2241 if (ret == NULL) {
2242 q->prev = NULL;
2243 ret = p = q;
2244 } else {
2245 p->next = q;
2246 q->prev = p;
2247 p = q;
2248 }
2249 }
2250 node = node->next;
2251 }
2252 return(ret);
2253}
2254
2255/**
2256 * xmlCopyNode:
2257 * @node: the node
2258 * @recursive: if 1 do a recursive copy.
2259 *
2260 * Do a copy of the node.
2261 *
2262 * Returns: a new xmlNodePtr, or NULL in case of error.
2263 */
2264xmlNodePtr
2265xmlCopyNode(xmlNodePtr node, int recursive) {
2266 xmlNodePtr ret;
2267
2268 ret = xmlStaticCopyNode(node, NULL, NULL, recursive);
2269 return(ret);
2270}
2271
2272/**
2273 * xmlCopyNodeList:
2274 * @node: the first node in the list.
2275 *
2276 * Do a recursive copy of the node list.
2277 *
2278 * Returns: a new xmlNodePtr, or NULL in case of error.
2279 */
2280xmlNodePtr xmlCopyNodeList(xmlNodePtr node) {
2281 xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL);
2282 return(ret);
2283}
2284
2285/**
2286 * xmlCopyElement:
2287 * @elem: the element
2288 *
2289 * Do a copy of the element definition.
2290 *
2291 * Returns: a new xmlElementPtr, or NULL in case of error.
2292xmlElementPtr
2293xmlCopyElement(xmlElementPtr elem) {
2294 xmlElementPtr ret;
2295
2296 if (elem == NULL) return(NULL);
2297 ret = xmlNewDocElement(elem->doc, elem->ns, elem->name, elem->content);
2298 if (ret == NULL) return(NULL);
2299 if (!recursive) return(ret);
2300 if (elem->properties != NULL)
2301 ret->properties = xmlCopyPropList(elem->properties);
2302
2303 if (elem->nsDef != NULL)
2304 ret->nsDef = xmlCopyNamespaceList(elem->nsDef);
2305 if (elem->childs != NULL)
2306 ret->childs = xmlCopyElementList(elem->childs);
2307 return(ret);
2308}
2309 */
2310
2311/**
2312 * xmlCopyDtd:
2313 * @dtd: the dtd
2314 *
2315 * Do a copy of the dtd.
2316 *
2317 * Returns: a new xmlDtdPtr, or NULL in case of error.
2318 */
2319xmlDtdPtr
2320xmlCopyDtd(xmlDtdPtr dtd) {
2321 xmlDtdPtr ret;
2322
2323 if (dtd == NULL) return(NULL);
2324 ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID);
2325 if (ret == NULL) return(NULL);
2326 if (dtd->entities != NULL)
2327 ret->entities = (void *) xmlCopyEntitiesTable(
2328 (xmlEntitiesTablePtr) dtd->entities);
Daniel Veillard1e346af1999-02-22 10:33:01 +00002329 if (dtd->notations != NULL)
2330 ret->notations = (void *) xmlCopyNotationTable(
2331 (xmlNotationTablePtr) dtd->notations);
2332 if (dtd->elements != NULL)
2333 ret->elements = (void *) xmlCopyElementTable(
2334 (xmlElementTablePtr) dtd->elements);
2335 if (dtd->attributes != NULL)
2336 ret->attributes = (void *) xmlCopyAttributeTable(
2337 (xmlAttributeTablePtr) dtd->attributes);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002338 return(ret);
2339}
2340
2341/**
2342 * xmlCopyDoc:
2343 * @doc: the document
2344 * @recursive: if 1 do a recursive copy.
2345 *
2346 * Do a copy of the document info. If recursive, the content tree will
2347 * be copied too as well as Dtd, namespaces and entities.
2348 *
2349 * Returns: a new xmlDocPtr, or NULL in case of error.
2350 */
2351xmlDocPtr
2352xmlCopyDoc(xmlDocPtr doc, int recursive) {
2353 xmlDocPtr ret;
2354
2355 if (doc == NULL) return(NULL);
2356 ret = xmlNewDoc(doc->version);
2357 if (ret == NULL) return(NULL);
2358 if (doc->name != NULL)
Daniel Veillard6454aec1999-09-02 22:04:43 +00002359 ret->name = xmlMemStrdup(doc->name);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002360 if (doc->encoding != NULL)
2361 ret->encoding = xmlStrdup(doc->encoding);
2362 ret->compression = doc->compression;
2363 ret->standalone = doc->standalone;
2364 if (!recursive) return(ret);
2365
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002366 if (doc->intSubset != NULL)
2367 ret->intSubset = xmlCopyDtd(doc->intSubset);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002368 if (doc->oldNs != NULL)
2369 ret->oldNs = xmlCopyNamespaceList(doc->oldNs);
2370 if (doc->root != NULL)
2371 ret->root = xmlStaticCopyNodeList(doc->root, ret, NULL);
2372 return(ret);
2373}
2374
2375/************************************************************************
2376 * *
Daniel Veillard260a68f1998-08-13 03:39:55 +00002377 * Content access functions *
2378 * *
2379 ************************************************************************/
2380
Daniel Veillard97b58771998-10-20 06:14:16 +00002381/**
Daniel Veillard944b5ff1999-12-15 19:08:24 +00002382 * xmlDocGetRootElement:
2383 * @doc: the document
2384 *
2385 * Get the root element of the document (doc->root is a list
2386 * containing possibly comments, PIs, etc ...).
2387 *
2388 * Returns the xmlNodePtr for the root or NULL
2389 */
2390xmlNodePtr
2391xmlDocGetRootElement(xmlDocPtr doc) {
2392 xmlNodePtr ret;
2393
2394 if (doc == NULL) return(NULL);
2395 ret = doc->root;
2396 while (ret != NULL) {
2397 if (ret->type == XML_ELEMENT_NODE)
2398 return(ret);
2399 ret = ret->next;
2400 }
2401 return(ret);
2402}
2403
2404/**
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002405 * xmlDocSetRootElement:
2406 * @doc: the document
2407 * @root: the new document root element
2408 *
2409 * Set the root element of the document (doc->root is a list
2410 * containing possibly comments, PIs, etc ...).
2411 *
2412 * Returns the old root element if any was found
2413 */
2414xmlNodePtr
2415xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) {
2416 xmlNodePtr old = NULL;
2417
2418 if (doc == NULL) return(NULL);
2419 old = doc->root;
2420 while (old != NULL) {
2421 if (old->type == XML_ELEMENT_NODE)
2422 break;
2423 old = old->next;
2424 }
2425 if (old == NULL) {
2426 if (doc->root == NULL) {
2427 doc->root = root;
2428 } else {
2429 xmlAddSibling(doc->root, root);
2430 }
2431 } else {
2432 xmlReplaceNode(old, root);
2433 }
2434 return(old);
2435}
2436
2437/**
Daniel Veillardb96e6431999-08-29 21:02:19 +00002438 * xmlNodeSetLang:
2439 * @cur: the node being changed
2440 * @lang: the langage description
2441 *
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002442 * Set the language of a node, i.e. the values of the xml:lang
2443 * attribute.
Daniel Veillardb96e6431999-08-29 21:02:19 +00002444 */
2445void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002446xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) {
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002447 if (cur == NULL) return;
2448 switch(cur->type) {
2449 case XML_TEXT_NODE:
2450 case XML_CDATA_SECTION_NODE:
2451 case XML_COMMENT_NODE:
2452 case XML_DOCUMENT_NODE:
2453 case XML_DOCUMENT_TYPE_NODE:
2454 case XML_DOCUMENT_FRAG_NODE:
2455 case XML_NOTATION_NODE:
2456 case XML_HTML_DOCUMENT_NODE:
2457 return;
2458 case XML_ELEMENT_NODE:
2459 case XML_ATTRIBUTE_NODE:
2460 case XML_PI_NODE:
2461 case XML_ENTITY_REF_NODE:
2462 case XML_ENTITY_NODE:
2463 break;
2464 }
Daniel Veillardb96e6431999-08-29 21:02:19 +00002465 xmlSetProp(cur, BAD_CAST "xml:lang", lang);
2466}
2467
2468/**
2469 * xmlNodeGetLang:
2470 * @cur: the node being checked
2471 *
2472 * Searches the language of a node, i.e. the values of the xml:lang
2473 * attribute or the one carried by the nearest ancestor.
2474 *
2475 * Returns a pointer to the lang value, or NULL if not found
Daniel Veillarda819dac1999-11-24 18:04:22 +00002476 * It's up to the caller to free the memory.
Daniel Veillardb96e6431999-08-29 21:02:19 +00002477 */
Daniel Veillarda819dac1999-11-24 18:04:22 +00002478xmlChar *
Daniel Veillardb96e6431999-08-29 21:02:19 +00002479xmlNodeGetLang(xmlNodePtr cur) {
Daniel Veillarda819dac1999-11-24 18:04:22 +00002480 xmlChar *lang;
Daniel Veillardb96e6431999-08-29 21:02:19 +00002481
2482 while (cur != NULL) {
2483 lang = xmlGetProp(cur, BAD_CAST "xml:lang");
2484 if (lang != NULL)
2485 return(lang);
2486 cur = cur->parent;
2487 }
2488 return(NULL);
2489}
2490
2491/**
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002492 * xmlNodeSetName:
2493 * @cur: the node being changed
2494 * @name: the new tag name
2495 *
2496 * Searches the language of a node, i.e. the values of the xml:lang
2497 * attribute or the one carried by the nearest ancestor.
2498 */
2499void
2500xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) {
2501 if (cur == NULL) return;
2502 if (name == NULL) return;
2503 switch(cur->type) {
2504 case XML_TEXT_NODE:
2505 case XML_CDATA_SECTION_NODE:
2506 case XML_COMMENT_NODE:
2507 case XML_DOCUMENT_NODE:
2508 case XML_DOCUMENT_TYPE_NODE:
2509 case XML_DOCUMENT_FRAG_NODE:
2510 case XML_NOTATION_NODE:
2511 case XML_HTML_DOCUMENT_NODE:
2512 return;
2513 case XML_ELEMENT_NODE:
2514 case XML_ATTRIBUTE_NODE:
2515 case XML_PI_NODE:
2516 case XML_ENTITY_REF_NODE:
2517 case XML_ENTITY_NODE:
2518 break;
2519 }
2520 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
2521 cur->name = xmlStrdup(name);
2522}
2523
2524/**
Daniel Veillard10a2c651999-12-12 13:03:50 +00002525 * xmlNodeGetBase:
2526 * @doc: the document the node pertains to
2527 * @cur: the node being checked
2528 *
2529 * Searches for the BASE URL. The code should work on both XML
2530 * and HTML document even if base mechanisms are completely different.
2531 *
2532 * Returns a pointer to the base URL, or NULL if not found
2533 * It's up to the caller to free the memory.
2534 */
2535xmlChar *
2536xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) {
2537 xmlChar *base;
2538
2539 if ((cur == NULL) && (doc == NULL))
2540 return(NULL);
2541 if (doc == NULL) doc = cur->doc;
2542 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
2543 cur = doc->root;
2544 while ((cur != NULL) && (cur->name != NULL)) {
2545 if (cur->type != XML_ELEMENT_NODE) {
2546 cur = cur->next;
2547 continue;
2548 }
2549 if ((!xmlStrcmp(cur->name, BAD_CAST "html")) ||
2550 (!xmlStrcmp(cur->name, BAD_CAST "HTML"))) {
2551 cur = cur->childs;
2552 continue;
2553 }
2554 if ((!xmlStrcmp(cur->name, BAD_CAST "head")) ||
2555 (!xmlStrcmp(cur->name, BAD_CAST "HEAD"))) {
2556 cur = cur->childs;
2557 continue;
2558 }
2559 if ((!xmlStrcmp(cur->name, BAD_CAST "base")) ||
2560 (!xmlStrcmp(cur->name, BAD_CAST "BASE"))) {
2561 base = xmlGetProp(cur, BAD_CAST "href");
2562 if (base != NULL) return(base);
2563 return(xmlGetProp(cur, BAD_CAST "HREF"));
2564 }
2565 }
2566 return(NULL);
2567 }
2568 while (cur != NULL) {
2569 base = xmlGetProp(cur, BAD_CAST "xml:base");
2570 if (base != NULL)
2571 return(base);
2572 cur = cur->parent;
2573 }
2574 return(NULL);
2575}
2576
2577/**
Daniel Veillard16253641998-10-28 22:58:05 +00002578 * xmlNodeGetContent:
2579 * @cur: the node being read
2580 *
2581 * Read the value of a node, this can be either the text carried
2582 * directly by this node if it's a TEXT node or the aggregate string
2583 * of the values carried by this node child's (TEXT and ENTITY_REF).
2584 * Entity references are substitued.
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002585 * Returns a new xmlChar * or NULL if no content is available.
Daniel Veillard5099ae81999-04-21 20:12:07 +00002586 * It's up to the caller to free the memory.
Daniel Veillard16253641998-10-28 22:58:05 +00002587 */
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002588xmlChar *
Daniel Veillard16253641998-10-28 22:58:05 +00002589xmlNodeGetContent(xmlNodePtr cur) {
2590 if (cur == NULL) return(NULL);
2591 switch (cur->type) {
2592 case XML_DOCUMENT_FRAG_NODE:
2593 case XML_ELEMENT_NODE:
2594 return(xmlNodeListGetString(cur->doc, cur->childs, 1));
2595 break;
Daniel Veillardb96e6431999-08-29 21:02:19 +00002596 case XML_ATTRIBUTE_NODE: {
2597 xmlAttrPtr attr = (xmlAttrPtr) cur;
2598 if (attr->node != NULL)
2599 return(xmlNodeListGetString(attr->node->doc, attr->val, 1));
2600 else
2601 return(xmlNodeListGetString(NULL, attr->val, 1));
2602 break;
2603 }
Daniel Veillarddbfd6411999-12-28 16:35:14 +00002604 case XML_COMMENT_NODE:
Daniel Veillardb96e6431999-08-29 21:02:19 +00002605 case XML_PI_NODE:
2606 if (cur->content != NULL)
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002607#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardb96e6431999-08-29 21:02:19 +00002608 return(xmlStrdup(cur->content));
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002609#else
2610 return(xmlStrdup(xmlBufferContent(cur->content)));
2611#endif
Daniel Veillardb96e6431999-08-29 21:02:19 +00002612 return(NULL);
Daniel Veillard16253641998-10-28 22:58:05 +00002613 case XML_ENTITY_REF_NODE:
Daniel Veillarddbfd6411999-12-28 16:35:14 +00002614 /*
2615 * Locate the entity, and get it's content
2616 * @@@
2617 */
2618 return(NULL);
Daniel Veillard16253641998-10-28 22:58:05 +00002619 case XML_ENTITY_NODE:
Daniel Veillard16253641998-10-28 22:58:05 +00002620 case XML_DOCUMENT_NODE:
Daniel Veillard7c1206f1999-10-14 09:10:25 +00002621 case XML_HTML_DOCUMENT_NODE:
Daniel Veillard16253641998-10-28 22:58:05 +00002622 case XML_DOCUMENT_TYPE_NODE:
2623 case XML_NOTATION_NODE:
2624 return(NULL);
Daniel Veillardb05deb71999-08-10 19:04:08 +00002625 case XML_CDATA_SECTION_NODE:
Daniel Veillard16253641998-10-28 22:58:05 +00002626 case XML_TEXT_NODE:
2627 if (cur->content != NULL)
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002628#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard16253641998-10-28 22:58:05 +00002629 return(xmlStrdup(cur->content));
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002630#else
2631 return(xmlStrdup(xmlBufferContent(cur->content)));
2632#endif
Daniel Veillard16253641998-10-28 22:58:05 +00002633 return(NULL);
2634 }
2635 return(NULL);
2636}
2637
2638/**
Daniel Veillard97b58771998-10-20 06:14:16 +00002639 * xmlNodeSetContent:
2640 * @cur: the node being modified
2641 * @content: the new value of the content
2642 *
2643 * Replace the content of a node.
Daniel Veillard260a68f1998-08-13 03:39:55 +00002644 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00002645void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002646xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00002647 if (cur == NULL) {
2648 fprintf(stderr, "xmlNodeSetContent : node == NULL\n");
2649 return;
2650 }
Daniel Veillard16253641998-10-28 22:58:05 +00002651 switch (cur->type) {
2652 case XML_DOCUMENT_FRAG_NODE:
2653 case XML_ELEMENT_NODE:
2654 if (cur->content != NULL) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002655#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard6454aec1999-09-02 22:04:43 +00002656 xmlFree(cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002657#else
2658 xmlBufferFree(cur->content);
2659#endif
Daniel Veillard16253641998-10-28 22:58:05 +00002660 cur->content = NULL;
2661 }
Daniel Veillard6454aec1999-09-02 22:04:43 +00002662 if (cur->childs != NULL) xmlFreeNodeList(cur->childs);
Daniel Veillard16253641998-10-28 22:58:05 +00002663 cur->childs = xmlStringGetNodeList(cur->doc, content);
Daniel Veillard1e346af1999-02-22 10:33:01 +00002664 UPDATE_LAST_CHILD(cur)
Daniel Veillard16253641998-10-28 22:58:05 +00002665 break;
2666 case XML_ATTRIBUTE_NODE:
2667 break;
2668 case XML_TEXT_NODE:
2669 case XML_CDATA_SECTION_NODE:
2670 case XML_ENTITY_REF_NODE:
2671 case XML_ENTITY_NODE:
2672 case XML_PI_NODE:
2673 case XML_COMMENT_NODE:
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002674 if (cur->content != NULL) {
2675#ifndef XML_USE_BUFFER_CONTENT
2676 xmlFree(cur->content);
2677#else
2678 xmlBufferFree(cur->content);
2679#endif
2680 }
Daniel Veillard6454aec1999-09-02 22:04:43 +00002681 if (cur->childs != NULL) xmlFreeNodeList(cur->childs);
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002682 cur->last = cur->childs = NULL;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002683 if (content != NULL) {
2684#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard16253641998-10-28 22:58:05 +00002685 cur->content = xmlStrdup(content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002686#else
2687 cur->content = xmlBufferCreateSize(0);
2688 xmlBufferSetAllocationScheme(cur->content,
2689 xmlGetBufferAllocationScheme());
2690 xmlBufferAdd(cur->content, content, -1);
2691#endif
2692 } else
Daniel Veillard16253641998-10-28 22:58:05 +00002693 cur->content = NULL;
Daniel Veillardb96e6431999-08-29 21:02:19 +00002694 break;
Daniel Veillard16253641998-10-28 22:58:05 +00002695 case XML_DOCUMENT_NODE:
Daniel Veillard7c1206f1999-10-14 09:10:25 +00002696 case XML_HTML_DOCUMENT_NODE:
Daniel Veillard16253641998-10-28 22:58:05 +00002697 case XML_DOCUMENT_TYPE_NODE:
2698 break;
2699 case XML_NOTATION_NODE:
2700 break;
2701 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00002702}
2703
Daniel Veillard97b58771998-10-20 06:14:16 +00002704/**
2705 * xmlNodeSetContentLen:
2706 * @cur: the node being modified
2707 * @content: the new value of the content
2708 * @len: the size of @content
2709 *
2710 * Replace the content of a node.
Daniel Veillard260a68f1998-08-13 03:39:55 +00002711 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00002712void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002713xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00002714 if (cur == NULL) {
Daniel Veillard16253641998-10-28 22:58:05 +00002715 fprintf(stderr, "xmlNodeSetContentLen : node == NULL\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +00002716 return;
2717 }
Daniel Veillard16253641998-10-28 22:58:05 +00002718 switch (cur->type) {
2719 case XML_DOCUMENT_FRAG_NODE:
2720 case XML_ELEMENT_NODE:
2721 if (cur->content != NULL) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002722#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard6454aec1999-09-02 22:04:43 +00002723 xmlFree(cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002724#else
2725 xmlBufferFree(cur->content);
2726#endif
Daniel Veillard16253641998-10-28 22:58:05 +00002727 cur->content = NULL;
2728 }
Daniel Veillard6454aec1999-09-02 22:04:43 +00002729 if (cur->childs != NULL) xmlFreeNodeList(cur->childs);
Daniel Veillard16253641998-10-28 22:58:05 +00002730 cur->childs = xmlStringLenGetNodeList(cur->doc, content, len);
Daniel Veillard1e346af1999-02-22 10:33:01 +00002731 UPDATE_LAST_CHILD(cur)
Daniel Veillard16253641998-10-28 22:58:05 +00002732 break;
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 (cur->content != NULL) {
2743#ifndef XML_USE_BUFFER_CONTENT
2744 xmlFree(cur->content);
2745#else
2746 xmlBufferFree(cur->content);
2747#endif
2748 }
Daniel Veillard6454aec1999-09-02 22:04:43 +00002749 if (cur->childs != NULL) xmlFreeNodeList(cur->childs);
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002750 cur->childs = cur->last = NULL;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002751 if (content != NULL) {
2752#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard16253641998-10-28 22:58:05 +00002753 cur->content = xmlStrndup(content, len);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002754#else
2755 cur->content = xmlBufferCreateSize(len);
2756 xmlBufferSetAllocationScheme(cur->content,
2757 xmlGetBufferAllocationScheme());
2758 xmlBufferAdd(cur->content, content, len);
2759#endif
2760 } else
Daniel Veillard16253641998-10-28 22:58:05 +00002761 cur->content = NULL;
Daniel Veillardb96e6431999-08-29 21:02:19 +00002762 break;
Daniel Veillard16253641998-10-28 22:58:05 +00002763 case XML_DOCUMENT_NODE:
Daniel Veillard7c1206f1999-10-14 09:10:25 +00002764 case XML_HTML_DOCUMENT_NODE:
Daniel Veillard16253641998-10-28 22:58:05 +00002765 case XML_DOCUMENT_TYPE_NODE:
2766 break;
Daniel Veillard260a68f1998-08-13 03:39:55 +00002767 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00002768}
2769
Daniel Veillard97b58771998-10-20 06:14:16 +00002770/**
2771 * xmlNodeAddContentLen:
2772 * @cur: the node being modified
2773 * @content: extra content
2774 * @len: the size of @content
2775 *
2776 * Append the extra substring to the node content.
Daniel Veillard260a68f1998-08-13 03:39:55 +00002777 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00002778void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002779xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00002780 if (cur == NULL) {
Daniel Veillard16253641998-10-28 22:58:05 +00002781 fprintf(stderr, "xmlNodeAddContentLen : node == NULL\n");
2782 return;
2783 }
2784 if (len <= 0) return;
2785 switch (cur->type) {
2786 case XML_DOCUMENT_FRAG_NODE:
2787 case XML_ELEMENT_NODE: {
2788 xmlNodePtr last = NULL, new;
2789
2790 if (cur->childs != NULL) {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002791 last = cur->last;
Daniel Veillard16253641998-10-28 22:58:05 +00002792 } else {
2793 if (cur->content != NULL) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002794#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard16253641998-10-28 22:58:05 +00002795 cur->childs = xmlStringGetNodeList(cur->doc, cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002796#else
2797 cur->childs = xmlStringGetNodeList(cur->doc,
2798 xmlBufferContent(cur->content));
2799#endif
Daniel Veillard1e346af1999-02-22 10:33:01 +00002800 UPDATE_LAST_CHILD(cur)
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002801#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard6454aec1999-09-02 22:04:43 +00002802 xmlFree(cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002803#else
2804 xmlBufferFree(cur->content);
2805#endif
Daniel Veillard16253641998-10-28 22:58:05 +00002806 cur->content = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002807 last = cur->last;
Daniel Veillard16253641998-10-28 22:58:05 +00002808 }
2809 }
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002810 new = xmlNewTextLen(content, len);
Daniel Veillard16253641998-10-28 22:58:05 +00002811 if (new != NULL) {
2812 xmlAddChild(cur, new);
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002813 if ((last != NULL) && (last->next == new)) {
Daniel Veillard16253641998-10-28 22:58:05 +00002814 xmlTextMerge(last, new);
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002815 }
Daniel Veillard16253641998-10-28 22:58:05 +00002816 }
2817 break;
2818 }
2819 case XML_ATTRIBUTE_NODE:
2820 break;
2821 case XML_TEXT_NODE:
2822 case XML_CDATA_SECTION_NODE:
2823 case XML_ENTITY_REF_NODE:
2824 case XML_ENTITY_NODE:
2825 case XML_PI_NODE:
2826 case XML_COMMENT_NODE:
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002827 case XML_NOTATION_NODE:
2828 if (content != NULL) {
2829#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard16253641998-10-28 22:58:05 +00002830 cur->content = xmlStrncat(cur->content, content, len);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002831#else
2832 xmlBufferAdd(cur->content, content, len);
2833#endif
2834 }
Daniel Veillard16253641998-10-28 22:58:05 +00002835 case XML_DOCUMENT_NODE:
Daniel Veillard7c1206f1999-10-14 09:10:25 +00002836 case XML_HTML_DOCUMENT_NODE:
Daniel Veillard16253641998-10-28 22:58:05 +00002837 case XML_DOCUMENT_TYPE_NODE:
2838 break;
Daniel Veillard16253641998-10-28 22:58:05 +00002839 }
2840}
2841
2842/**
2843 * xmlNodeAddContent:
2844 * @cur: the node being modified
2845 * @content: extra content
2846 *
2847 * Append the extra substring to the node content.
2848 */
2849void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002850xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) {
Daniel Veillard16253641998-10-28 22:58:05 +00002851 int len;
2852
2853 if (cur == NULL) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00002854 fprintf(stderr, "xmlNodeAddContent : node == NULL\n");
2855 return;
2856 }
Daniel Veillard16253641998-10-28 22:58:05 +00002857 if (content == NULL) return;
2858 len = xmlStrlen(content);
2859 xmlNodeAddContentLen(cur, content, len);
2860}
2861
2862/**
2863 * xmlTextMerge:
2864 * @first: the first text node
2865 * @second: the second text node being merged
2866 *
2867 * Merge two text nodes into one
Daniel Veillard1e346af1999-02-22 10:33:01 +00002868 * Returns the first text node augmented
Daniel Veillard16253641998-10-28 22:58:05 +00002869 */
2870xmlNodePtr
2871xmlTextMerge(xmlNodePtr first, xmlNodePtr second) {
2872 if (first == NULL) return(second);
2873 if (second == NULL) return(first);
2874 if (first->type != XML_TEXT_NODE) return(first);
2875 if (second->type != XML_TEXT_NODE) return(first);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002876#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard16253641998-10-28 22:58:05 +00002877 xmlNodeAddContent(first, second->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002878#else
2879 xmlNodeAddContent(first, xmlBufferContent(second->content));
2880#endif
Daniel Veillard16253641998-10-28 22:58:05 +00002881 xmlUnlinkNode(second);
2882 xmlFreeNode(second);
2883 return(first);
Daniel Veillard260a68f1998-08-13 03:39:55 +00002884}
2885
Daniel Veillard97b58771998-10-20 06:14:16 +00002886/**
Daniel Veillardb96e6431999-08-29 21:02:19 +00002887 * xmlGetNsList:
2888 * @doc: the document
2889 * @node: the current node
2890 *
2891 * Search all the namespace applying to a given element.
2892 * Returns an NULL terminated array of all the xmlNsPtr found
2893 * that need to be freed by the caller or NULL if no
2894 * namespace if defined
2895 */
2896xmlNsPtr *
2897xmlGetNsList(xmlDocPtr doc, xmlNodePtr node) {
2898 xmlNsPtr cur;
2899 xmlNsPtr *ret = NULL;
2900 int nbns = 0;
2901 int maxns = 10;
2902 int i;
2903
2904 while (node != NULL) {
2905 cur = node->nsDef;
2906 while (cur != NULL) {
2907 if (ret == NULL) {
Daniel Veillard6454aec1999-09-02 22:04:43 +00002908 ret = (xmlNsPtr *) xmlMalloc((maxns + 1) * sizeof(xmlNsPtr));
Daniel Veillardb96e6431999-08-29 21:02:19 +00002909 if (ret == NULL) {
2910 fprintf(stderr, "xmlGetNsList : out of memory!\n");
2911 return(NULL);
2912 }
2913 ret[nbns] = NULL;
2914 }
2915 for (i = 0;i < nbns;i++) {
2916 if ((cur->prefix == ret[i]->prefix) ||
2917 (!xmlStrcmp(cur->prefix, ret[i]->prefix))) break;
2918 }
2919 if (i >= nbns) {
2920 if (nbns >= maxns) {
2921 maxns *= 2;
Daniel Veillard6454aec1999-09-02 22:04:43 +00002922 ret = (xmlNsPtr *) xmlRealloc(ret,
Daniel Veillardb96e6431999-08-29 21:02:19 +00002923 (maxns + 1) * sizeof(xmlNsPtr));
2924 if (ret == NULL) {
2925 fprintf(stderr, "xmlGetNsList : realloc failed!\n");
2926 return(NULL);
2927 }
2928 }
2929 ret[nbns++] = cur;
2930 ret[nbns] = NULL;
2931 }
2932
2933 cur = cur->next;
2934 }
2935 node = node->parent;
2936 }
2937 return(ret);
2938}
2939
2940/**
Daniel Veillard97b58771998-10-20 06:14:16 +00002941 * xmlSearchNs:
2942 * @doc: the document
2943 * @node: the current node
2944 * @nameSpace: the namespace string
Daniel Veillard260a68f1998-08-13 03:39:55 +00002945 *
Daniel Veillard97b58771998-10-20 06:14:16 +00002946 * Search a Ns registered under a given name space for a document.
2947 * recurse on the parents until it finds the defined namespace
2948 * or return NULL otherwise.
2949 * @nameSpace can be NULL, this is a search for the default namespace.
Daniel Veillard1e346af1999-02-22 10:33:01 +00002950 * Returns the namespace pointer or NULL.
Daniel Veillard260a68f1998-08-13 03:39:55 +00002951 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00002952xmlNsPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002953xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00002954 xmlNsPtr cur;
2955
Daniel Veillard62ba71e1999-12-16 17:52:19 +00002956 if (node == NULL) return(NULL);
Daniel Veillard260a68f1998-08-13 03:39:55 +00002957 while (node != NULL) {
2958 cur = node->nsDef;
2959 while (cur != NULL) {
2960 if ((cur->prefix == NULL) && (nameSpace == NULL))
2961 return(cur);
2962 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
2963 (!xmlStrcmp(cur->prefix, nameSpace)))
2964 return(cur);
2965 cur = cur->next;
2966 }
2967 node = node->parent;
2968 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00002969 return(NULL);
2970}
2971
Daniel Veillard97b58771998-10-20 06:14:16 +00002972/**
2973 * xmlSearchNsByHref:
2974 * @doc: the document
2975 * @node: the current node
2976 * @href: the namespace value
2977 *
2978 * Search a Ns aliasing a given URI. Recurse on the parents until it finds
2979 * the defined namespace or return NULL otherwise.
Daniel Veillard1e346af1999-02-22 10:33:01 +00002980 * Returns the namespace pointer or NULL.
Daniel Veillard260a68f1998-08-13 03:39:55 +00002981 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00002982xmlNsPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002983xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar *href) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00002984 xmlNsPtr cur;
Daniel Veillarddbfd6411999-12-28 16:35:14 +00002985 xmlNodePtr orig = node;
Daniel Veillard260a68f1998-08-13 03:39:55 +00002986
Daniel Veillard10a2c651999-12-12 13:03:50 +00002987 if ((node == NULL) || (href == NULL)) return(NULL);
Daniel Veillard260a68f1998-08-13 03:39:55 +00002988 while (node != NULL) {
2989 cur = node->nsDef;
2990 while (cur != NULL) {
2991 if ((cur->href != NULL) && (href != NULL) &&
Daniel Veillarddbfd6411999-12-28 16:35:14 +00002992 (!xmlStrcmp(cur->href, href))) {
2993 /*
2994 * Check that the prefix is not shadowed between orig and node
2995 */
2996 xmlNodePtr check = orig;
2997 xmlNsPtr tst;
2998
2999 while (check != node) {
3000 tst = check->nsDef;
3001 while (tst != NULL) {
3002 if ((tst->prefix == NULL) && (cur->prefix == NULL))
3003 goto shadowed;
3004 if ((tst->prefix != NULL) && (cur->prefix != NULL) &&
3005 (!xmlStrcmp(tst->prefix, cur->prefix)))
3006 goto shadowed;
3007 tst = tst->next;
3008 }
3009 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00003010 return(cur);
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003011 }
3012shadowed:
Daniel Veillard260a68f1998-08-13 03:39:55 +00003013 cur = cur->next;
3014 }
3015 node = node->parent;
3016 }
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003017 return(NULL);
3018}
3019
3020/**
3021 * xmlNewReconciliedNs
3022 * @doc: the document
3023 * @tree: a node expected to hold the new namespace
3024 * @ns: the original namespace
3025 *
3026 * This function tries to locate a namespace definition in a tree
3027 * ancestors, or create a new namespace definition node similar to
3028 * @ns trying to reuse the same prefix. However if the given prefix is
3029 * null (default namespace) or reused within the subtree defined by
3030 * @tree or on one of its ancestors then a new prefix is generated.
3031 * Returns the (new) namespace definition or NULL in case of error
3032 */
3033xmlNsPtr
3034xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) {
3035 xmlNsPtr def;
3036 xmlChar prefix[50];
3037 int counter = 1;
3038
3039 if (tree == NULL) {
3040#ifdef DEBUG_TREE
3041 fprintf(stderr, "xmlNewReconciliedNs : tree == NULL\n");
3042#endif
3043 return(NULL);
3044 }
3045 if (ns == NULL) {
3046#ifdef DEBUG_TREE
3047 fprintf(stderr, "xmlNewReconciliedNs : ns == NULL\n");
3048#endif
3049 return(NULL);
3050 }
3051 /*
3052 * Search an existing namespace definition inherited.
3053 */
3054 def = xmlSearchNsByHref(doc, tree, ns->href);
3055 if (def != NULL)
3056 return(def);
3057
3058 /*
3059 * Find a close prefix which is not already in use.
3060 * Let's strip namespace prefixes longer than 20 chars !
3061 */
3062 sprintf((char *) prefix, "%.20s", ns->prefix);
3063 def = xmlSearchNs(doc, tree, prefix);
3064 while (def != NULL) {
3065 if (counter > 1000) return(NULL);
3066 sprintf((char *) prefix, "%.20s%d", ns->prefix, counter++);
3067 def = xmlSearchNs(doc, tree, prefix);
3068 }
3069
3070 /*
3071 * Ok, now we are ready to create a new one.
3072 */
3073 def = xmlNewNs(tree, ns->href, prefix);
3074 return(def);
3075}
3076
3077/**
3078 * xmlReconciliateNs
3079 * @doc: the document
3080 * @tree: a node defining the subtree to reconciliate
3081 *
3082 * This function checks that all the namespaces declared within the given
3083 * tree are properly declared. This is needed for example after Copy or Cut
3084 * and then paste operations. The subtree may still hold pointers to
3085 * namespace declarations outside the subtree or invalid/masked. As much
3086 * as possible the function try tu reuse the existing namespaces found in
3087 * the new environment. If not possible the new namespaces are redeclared
3088 * on @tree at the top of the given subtree.
3089 * Returns the number of namespace declarations created or -1 in case of error.
3090 */
3091int
3092xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) {
3093 xmlNsPtr *oldNs = NULL;
3094 xmlNsPtr *newNs = NULL;
3095 int sizeCache = 0;
3096 int nbCache = 0;
3097
3098 xmlNsPtr n;
3099 xmlNodePtr node = tree;
3100 xmlAttrPtr attr;
3101 int ret = 0, i;
3102
3103 while (node != NULL) {
3104 /*
3105 * Reconciliate the node namespace
3106 */
3107 if (node->ns != NULL) {
3108 /*
3109 * initialize the cache if needed
3110 */
3111 if (sizeCache == 0) {
3112 sizeCache = 10;
3113 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
3114 sizeof(xmlNsPtr));
3115 if (oldNs == NULL) {
3116 fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
3117 return(-1);
3118 }
3119 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
3120 sizeof(xmlNsPtr));
3121 if (newNs == NULL) {
3122 fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
3123 xmlFree(oldNs);
3124 return(-1);
3125 }
3126 }
3127 for (i = 0;i < nbCache;i++) {
3128 if (oldNs[i] == node->ns) {
3129 node->ns = newNs[i];
3130 break;
3131 }
3132 }
3133 if (i == nbCache) {
3134 /*
3135 * Ok we need to recreate a new namespace definition
3136 */
3137 n = xmlNewReconciliedNs(doc, tree, node->ns);
3138 if (n != NULL) { /* :-( what if else ??? */
3139 /*
3140 * check if we need to grow the cache buffers.
3141 */
3142 if (sizeCache <= nbCache) {
3143 sizeCache *= 2;
3144 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
3145 sizeof(xmlNsPtr));
3146 if (oldNs == NULL) {
3147 fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
3148 xmlFree(newNs);
3149 return(-1);
3150 }
3151 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
3152 sizeof(xmlNsPtr));
3153 if (newNs == NULL) {
3154 fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
3155 xmlFree(oldNs);
3156 return(-1);
3157 }
3158 }
3159 newNs[nbCache] = n;
3160 oldNs[nbCache++] = node->ns;
3161 node->ns = n;
3162 }
3163 }
3164 }
3165 /*
3166 * now check for namespace hold by attributes on the node.
3167 */
3168 attr = node->properties;
3169 while (attr != NULL) {
3170 if (attr->ns != NULL) {
3171 /*
3172 * initialize the cache if needed
3173 */
3174 if (sizeCache == 0) {
3175 sizeCache = 10;
3176 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
3177 sizeof(xmlNsPtr));
3178 if (oldNs == NULL) {
3179 fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
3180 return(-1);
3181 }
3182 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
3183 sizeof(xmlNsPtr));
3184 if (newNs == NULL) {
3185 fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
3186 xmlFree(oldNs);
3187 return(-1);
3188 }
3189 }
3190 for (i = 0;i < nbCache;i++) {
3191 if (oldNs[i] == attr->ns) {
3192 node->ns = newNs[i];
3193 break;
3194 }
3195 }
3196 if (i == nbCache) {
3197 /*
3198 * Ok we need to recreate a new namespace definition
3199 */
3200 n = xmlNewReconciliedNs(doc, tree, attr->ns);
3201 if (n != NULL) { /* :-( what if else ??? */
3202 /*
3203 * check if we need to grow the cache buffers.
3204 */
3205 if (sizeCache <= nbCache) {
3206 sizeCache *= 2;
3207 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
3208 sizeof(xmlNsPtr));
3209 if (oldNs == NULL) {
3210 fprintf(stderr,
3211 "xmlReconciliateNs : memory pbm\n");
3212 xmlFree(newNs);
3213 return(-1);
3214 }
3215 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
3216 sizeof(xmlNsPtr));
3217 if (newNs == NULL) {
3218 fprintf(stderr,
3219 "xmlReconciliateNs : memory pbm\n");
3220 xmlFree(oldNs);
3221 return(-1);
3222 }
3223 }
3224 newNs[nbCache] = n;
3225 oldNs[nbCache++] = attr->ns;
3226 attr->ns = n;
3227 }
3228 }
3229 }
3230 attr = attr->next;
3231 }
3232
3233 /*
3234 * Browse the full subtree, deep first
3235 */
3236 if (node->childs != NULL) {
3237 /* deep first */
3238 node = node->childs;
3239 } else if ((node != tree) && (node->next != NULL)) {
3240 /* then siblings */
3241 node = node->next;
3242 } else if (node != tree) {
3243 /* go up to parents->next if needed */
3244 while (node != tree) {
3245 if (node->parent != NULL)
3246 node = node->parent;
3247 if ((node != tree) && (node->next != NULL)) {
3248 node = node->next;
3249 break;
3250 }
3251 if (node->parent == NULL) {
3252 node = NULL;
3253 break;
3254 }
3255 }
3256 /* exit condition */
3257 if (node == tree)
3258 node = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00003259 }
3260 }
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003261 return(ret);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003262}
3263
Daniel Veillard97b58771998-10-20 06:14:16 +00003264/**
3265 * xmlGetProp:
3266 * @node: the node
3267 * @name: the attribute name
3268 *
3269 * Search and get the value of an attribute associated to a node
Daniel Veillardccb09631998-10-27 06:21:04 +00003270 * This does the entity substitution.
Daniel Veillard10a2c651999-12-12 13:03:50 +00003271 * This function looks in DTD attribute declaration for #FIXED or
3272 * default declaration values unless DTD use has been turned off.
3273 *
Daniel Veillard1e346af1999-02-22 10:33:01 +00003274 * Returns the attribute value or NULL if not found.
Daniel Veillarda819dac1999-11-24 18:04:22 +00003275 * It's up to the caller to free the memory.
Daniel Veillard260a68f1998-08-13 03:39:55 +00003276 */
Daniel Veillarda819dac1999-11-24 18:04:22 +00003277xmlChar *
3278xmlGetProp(xmlNodePtr node, const xmlChar *name) {
Daniel Veillard10a2c651999-12-12 13:03:50 +00003279 xmlAttrPtr prop;
3280 xmlDocPtr doc;
Daniel Veillard260a68f1998-08-13 03:39:55 +00003281
Daniel Veillard10a2c651999-12-12 13:03:50 +00003282 if ((node == NULL) || (name == NULL)) return(NULL);
3283 /*
3284 * Check on the properties attached to the node
3285 */
3286 prop = node->properties;
Daniel Veillard260a68f1998-08-13 03:39:55 +00003287 while (prop != NULL) {
Daniel Veillard68178931999-02-08 18:34:36 +00003288 if (!xmlStrcmp(prop->name, name)) {
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003289 xmlChar *ret;
Daniel Veillard6800ef31999-02-08 18:33:22 +00003290
3291 ret = xmlNodeListGetString(node->doc, prop->val, 1);
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003292 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
Daniel Veillard6800ef31999-02-08 18:33:22 +00003293 return(ret);
Daniel Veillard68178931999-02-08 18:34:36 +00003294 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00003295 prop = prop->next;
3296 }
Daniel Veillard10a2c651999-12-12 13:03:50 +00003297 if (!xmlCheckDTD) return(NULL);
3298
3299 /*
3300 * Check if there is a default declaration in the internal
3301 * or external subsets
3302 */
3303 doc = node->doc;
3304 if (doc != NULL) {
3305 xmlAttributePtr attrDecl;
3306 if (doc->intSubset != NULL) {
3307 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
3308 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3309 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
3310 return(xmlStrdup(attrDecl->defaultValue));
3311 }
3312 }
3313 return(NULL);
3314}
3315
3316/**
3317 * xmlGetNsProp:
3318 * @node: the node
3319 * @name: the attribute name
3320 * @namespace: the URI of the namespace
3321 *
3322 * Search and get the value of an attribute associated to a node
3323 * This attribute has to be anchored in the namespace specified.
3324 * This does the entity substitution.
3325 * This function looks in DTD attribute declaration for #FIXED or
3326 * default declaration values unless DTD use has been turned off.
3327 *
3328 * Returns the attribute value or NULL if not found.
3329 * It's up to the caller to free the memory.
3330 */
3331xmlChar *
3332xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *namespace) {
3333 xmlAttrPtr prop = node->properties;
3334 xmlDocPtr doc;
3335 xmlNsPtr ns;
3336
3337 if (namespace == NULL)
3338 return(xmlGetProp(node, name));
3339 while (prop != NULL) {
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00003340 /*
3341 * One need to have
3342 * - same attribute names
3343 * - and the attribute carrying that namespace
3344 * or
3345 * no namespace on the attribute and the element carrying it
3346 */
Daniel Veillard10a2c651999-12-12 13:03:50 +00003347 if ((!xmlStrcmp(prop->name, name)) &&
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00003348 (((prop->ns == NULL) && (node->ns != NULL) &&
3349 (!xmlStrcmp(node->ns->href, namespace))) ||
Daniel Veillard3c558c31999-12-22 11:30:41 +00003350 ((prop->ns != NULL) && (!xmlStrcmp(prop->ns->href, namespace))))) {
Daniel Veillard10a2c651999-12-12 13:03:50 +00003351 xmlChar *ret;
3352
3353 ret = xmlNodeListGetString(node->doc, prop->val, 1);
3354 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
3355 return(ret);
3356 }
3357 prop = prop->next;
3358 }
3359 if (!xmlCheckDTD) return(NULL);
3360
3361 /*
3362 * Check if there is a default declaration in the internal
3363 * or external subsets
3364 */
3365 doc = node->doc;
3366 if (doc != NULL) {
3367 xmlAttributePtr attrDecl;
3368 if (doc->intSubset != NULL) {
3369 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
3370 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3371 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
3372
3373 if (attrDecl->prefix != NULL) {
3374 /*
3375 * The DTD declaration only allows a prefix search
3376 */
3377 ns = xmlSearchNs(doc, node, attrDecl->prefix);
3378 if ((ns != NULL) && (!xmlStrcmp(ns->href, namespace)))
3379 return(xmlStrdup(attrDecl->defaultValue));
3380 }
3381 }
3382 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00003383 return(NULL);
3384}
3385
Daniel Veillard97b58771998-10-20 06:14:16 +00003386/**
Daniel Veillardccb09631998-10-27 06:21:04 +00003387 * xmlSetProp:
Daniel Veillard97b58771998-10-20 06:14:16 +00003388 * @node: the node
3389 * @name: the attribute name
3390 * @value: the attribute value
3391 *
3392 * Set (or reset) an attribute carried by a node.
Daniel Veillard1e346af1999-02-22 10:33:01 +00003393 * Returns the attribute pointer.
Daniel Veillard260a68f1998-08-13 03:39:55 +00003394 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003395xmlAttrPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003396xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003397 xmlAttrPtr prop = node->properties;
3398
3399 while (prop != NULL) {
3400 if (!xmlStrcmp(prop->name, name)) {
Daniel Veillardccb09631998-10-27 06:21:04 +00003401 if (prop->val != NULL)
Daniel Veillard6454aec1999-09-02 22:04:43 +00003402 xmlFreeNodeList(prop->val);
Daniel Veillardccb09631998-10-27 06:21:04 +00003403 prop->val = NULL;
Daniel Veillard51e3b151999-11-12 17:02:31 +00003404 if (value != NULL) {
3405 xmlChar *buffer;
3406 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
3407 prop->val = xmlStringGetNodeList(node->doc, buffer);
3408 xmlFree(buffer);
3409 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00003410 return(prop);
3411 }
3412 prop = prop->next;
3413 }
3414 prop = xmlNewProp(node, name, value);
3415 return(prop);
3416}
3417
Daniel Veillard97b58771998-10-20 06:14:16 +00003418/**
3419 * xmlNodeIsText:
3420 * @node: the node
3421 *
3422 * Is this node a Text node ?
Daniel Veillard1e346af1999-02-22 10:33:01 +00003423 * Returns 1 yes, 0 no
Daniel Veillard260a68f1998-08-13 03:39:55 +00003424 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003425int
3426xmlNodeIsText(xmlNodePtr node) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003427 if (node == NULL) return(0);
3428
Daniel Veillard0bef1311998-10-14 02:36:47 +00003429 if (node->type == XML_TEXT_NODE) return(1);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003430 return(0);
3431}
3432
Daniel Veillard97b58771998-10-20 06:14:16 +00003433/**
Daniel Veillard1e346af1999-02-22 10:33:01 +00003434 * xmlTextConcat:
Daniel Veillard97b58771998-10-20 06:14:16 +00003435 * @node: the node
3436 * @content: the content
3437 * @len: @content lenght
3438 *
3439 * Concat the given string at the end of the existing node content
Daniel Veillard260a68f1998-08-13 03:39:55 +00003440 */
Daniel Veillard97b58771998-10-20 06:14:16 +00003441
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003442void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003443xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003444 if (node == NULL) return;
3445
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003446 if ((node->type != XML_TEXT_NODE) &&
3447 (node->type != XML_CDATA_SECTION_NODE)) {
3448 fprintf(stderr, "xmlTextConcat: node is not text nor cdata\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +00003449 return;
3450 }
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003451#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard260a68f1998-08-13 03:39:55 +00003452 node->content = xmlStrncat(node->content, content, len);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003453#else
3454 xmlBufferAdd(node->content, content, len);
3455#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00003456}
3457
3458/************************************************************************
3459 * *
3460 * Output : to a FILE or in memory *
3461 * *
3462 ************************************************************************/
3463
Daniel Veillard5099ae81999-04-21 20:12:07 +00003464#define BASE_BUFFER_SIZE 4000
3465
3466/**
3467 * xmlBufferCreate:
3468 *
3469 * routine to create an XML buffer.
3470 * returns the new structure.
3471 */
3472xmlBufferPtr
3473xmlBufferCreate(void) {
3474 xmlBufferPtr ret;
3475
Daniel Veillard6454aec1999-09-02 22:04:43 +00003476 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
Daniel Veillard5099ae81999-04-21 20:12:07 +00003477 if (ret == NULL) {
3478 fprintf(stderr, "xmlBufferCreate : out of memory!\n");
3479 return(NULL);
3480 }
3481 ret->use = 0;
3482 ret->size = BASE_BUFFER_SIZE;
Daniel Veillard10a2c651999-12-12 13:03:50 +00003483 ret->alloc = xmlBufferAllocScheme;
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003484 ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar));
Daniel Veillard5099ae81999-04-21 20:12:07 +00003485 if (ret->content == NULL) {
3486 fprintf(stderr, "xmlBufferCreate : out of memory!\n");
Daniel Veillard6454aec1999-09-02 22:04:43 +00003487 xmlFree(ret);
Daniel Veillard5099ae81999-04-21 20:12:07 +00003488 return(NULL);
3489 }
3490 ret->content[0] = 0;
3491 return(ret);
3492}
3493
3494/**
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003495 * xmlBufferCreateSize:
3496 * @size: initial size of buffer
3497 *
3498 * routine to create an XML buffer.
3499 * returns the new structure.
3500 */
3501xmlBufferPtr
3502xmlBufferCreateSize(size_t size) {
3503 xmlBufferPtr ret;
3504
3505 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
3506 if (ret == NULL) {
3507 fprintf(stderr, "xmlBufferCreate : out of memory!\n");
3508 return(NULL);
3509 }
3510 ret->use = 0;
Daniel Veillard10a2c651999-12-12 13:03:50 +00003511 ret->alloc = xmlBufferAllocScheme;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003512 ret->size = (size ? size+2 : 0); /* +1 for ending null */
Daniel Veillard10a2c651999-12-12 13:03:50 +00003513 if (ret->size){
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003514 ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar));
3515 if (ret->content == NULL) {
3516 fprintf(stderr, "xmlBufferCreate : out of memory!\n");
3517 xmlFree(ret);
3518 return(NULL);
3519 }
3520 ret->content[0] = 0;
3521 } else
3522 ret->content = NULL;
3523 return(ret);
3524}
3525
3526/**
3527 * xmlBufferAllocationScheme:
3528 * @buf: the buffer to free
3529 * @scheme: allocation scheme to use
3530 *
3531 * Sets the allocation scheme for this buffer
3532 */
3533void
3534xmlBufferSetAllocationScheme(xmlBufferPtr buf,
3535 xmlBufferAllocationScheme scheme) {
3536 if (buf == NULL) {
3537 fprintf(stderr, "xmlBufferSetAllocationScheme: buf == NULL\n");
3538 return;
3539 }
3540
3541 buf->alloc = scheme;
3542}
3543
3544/**
Daniel Veillard5099ae81999-04-21 20:12:07 +00003545 * xmlBufferFree:
3546 * @buf: the buffer to free
3547 *
3548 * Frees an XML buffer.
3549 */
3550void
3551xmlBufferFree(xmlBufferPtr buf) {
3552 if (buf == NULL) {
3553 fprintf(stderr, "xmlBufferFree: buf == NULL\n");
3554 return;
3555 }
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003556 if (buf->content != NULL) {
3557#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard5099ae81999-04-21 20:12:07 +00003558 memset(buf->content, -1, BASE_BUFFER_SIZE);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003559#else
3560 memset(buf->content, -1, buf->size);
3561#endif
Daniel Veillard6454aec1999-09-02 22:04:43 +00003562 xmlFree(buf->content);
Daniel Veillard5099ae81999-04-21 20:12:07 +00003563 }
3564 memset(buf, -1, sizeof(xmlBuffer));
Daniel Veillard6454aec1999-09-02 22:04:43 +00003565 xmlFree(buf);
Daniel Veillard5099ae81999-04-21 20:12:07 +00003566}
3567
3568/**
Daniel Veillarde2d034d1999-07-27 19:52:06 +00003569 * xmlBufferEmpty:
3570 * @buf: the buffer
3571 *
3572 * empty a buffer.
3573 */
3574void
3575xmlBufferEmpty(xmlBufferPtr buf) {
3576 buf->use = 0;
3577 memset(buf->content, -1, buf->size);/* just for debug */
3578}
3579
3580/**
3581 * xmlBufferShrink:
3582 * @buf: the buffer to dump
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003583 * @len: the number of xmlChar to remove
Daniel Veillarde2d034d1999-07-27 19:52:06 +00003584 *
3585 * Remove the beginning of an XML buffer.
3586 *
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003587 * Returns the number of xmlChar removed, or -1 in case of failure.
Daniel Veillarde2d034d1999-07-27 19:52:06 +00003588 */
3589int
3590xmlBufferShrink(xmlBufferPtr buf, int len) {
3591 if (len == 0) return(0);
3592 if (len > buf->use) return(-1);
3593
3594 buf->use -= len;
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003595 memmove(buf->content, &buf->content[len], buf->use * sizeof(xmlChar));
Daniel Veillarde2d034d1999-07-27 19:52:06 +00003596
3597 buf->content[buf->use] = 0;
3598 return(len);
3599}
3600
3601/**
Daniel Veillard5099ae81999-04-21 20:12:07 +00003602 * xmlBufferDump:
3603 * @file: the file output
3604 * @buf: the buffer to dump
3605 *
3606 * Dumps an XML buffer to a FILE *.
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003607 * Returns the number of xmlChar written
Daniel Veillard5099ae81999-04-21 20:12:07 +00003608 */
3609int
3610xmlBufferDump(FILE *file, xmlBufferPtr buf) {
3611 int ret;
3612
3613 if (buf == NULL) {
3614 fprintf(stderr, "xmlBufferDump: buf == NULL\n");
3615 return(0);
3616 }
3617 if (buf->content == NULL) {
3618 fprintf(stderr, "xmlBufferDump: buf->content == NULL\n");
3619 return(0);
3620 }
3621 if (file == NULL) file = stdout;
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003622 ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
Daniel Veillard5099ae81999-04-21 20:12:07 +00003623 return(ret);
3624}
3625
3626/**
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003627 * xmlBufferContent:
3628 * @buf: the buffer to resize
3629 *
3630 * Returns the internal content
3631 */
3632
3633const xmlChar*
3634xmlBufferContent(const xmlBufferPtr buf)
3635{
3636 if(!buf)
3637 return NULL;
3638
3639 return buf->content;
3640}
3641
3642/**
3643 * xmlBufferLength:
3644 * @buf: the buffer
3645 *
3646 * Returns the length of data in the internal content
3647 */
3648
3649int
3650xmlBufferLength(const xmlBufferPtr buf)
3651{
3652 if(!buf)
3653 return 0;
3654
3655 return buf->use;
3656}
3657
3658/**
3659 * xmlBufferResize:
3660 * @buf: the buffer to resize
3661 * @len: the desired size
3662 *
3663 * Resize a buffer to accomodate minimum size of <len>.
3664 *
3665 * Returns 0 in case of problems, 1 otherwise
3666 */
3667int
3668xmlBufferResize(xmlBufferPtr buf, int size)
3669{
3670 int newSize = (buf->size ? buf->size*2 : size);/*take care of empty case*/
3671 xmlChar* rebuf = NULL;
3672
3673 /* Don't resize if we don't have to */
3674 if(size < buf->size)
3675 return 1;
3676
3677 /* figure out new size */
3678 switch(buf->alloc){
3679 case XML_BUFFER_ALLOC_DOUBLEIT:
3680 while(size > newSize) newSize *= 2;
3681 break;
3682 case XML_BUFFER_ALLOC_EXACT:
3683 newSize = size+10;
3684 break;
3685 default:
3686 newSize = size+10;
3687 break;
3688 }
3689
3690 if (buf->content == NULL)
3691 rebuf = (xmlChar *) xmlMalloc(newSize * sizeof(xmlChar));
3692 else
3693 rebuf = (xmlChar *) xmlRealloc(buf->content,
3694 newSize * sizeof(xmlChar));
3695 if (rebuf == NULL) {
3696 fprintf(stderr, "xmlBufferAdd : out of memory!\n");
3697 return 0;
3698 }
3699 buf->content = rebuf;
3700 buf->size = newSize;
3701
3702 return 1;
3703}
3704/**
Daniel Veillard5099ae81999-04-21 20:12:07 +00003705 * xmlBufferAdd:
3706 * @buf: the buffer to dump
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003707 * @str: the xmlChar string
3708 * @len: the number of xmlChar to add
Daniel Veillard5099ae81999-04-21 20:12:07 +00003709 *
Daniel Veillard10a2c651999-12-12 13:03:50 +00003710 * Add a string range to an XML buffer. if len == -1, the lenght of
3711 * str is recomputed.
Daniel Veillard5099ae81999-04-21 20:12:07 +00003712 */
3713void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003714xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003715 int l, needSize;
Daniel Veillard5099ae81999-04-21 20:12:07 +00003716
3717 if (str == NULL) {
3718 fprintf(stderr, "xmlBufferAdd: str == NULL\n");
3719 return;
3720 }
Daniel Veillard10a2c651999-12-12 13:03:50 +00003721 if (len < -1) {
3722 fprintf(stderr, "xmlBufferAdd: len < 0\n");
3723 return;
3724 }
3725 if (len == 0) return;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003726
3727 /* CJN What's this for??? */
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003728 if (len < 0)
3729 l = xmlStrlen(str);
3730 else
3731 for (l = 0;l < len;l++)
3732 if (str[l] == 0) break;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003733 if (l < len){ len = l; printf("xmlBufferAdd bad length\n"); }
3734
3735 /* CJN 11.18.99 okay, now I'm using the length */
3736 if(len == -1) len = l;
3737
3738
Daniel Veillarde2d034d1999-07-27 19:52:06 +00003739 if (len <= 0) return;
Daniel Veillard5099ae81999-04-21 20:12:07 +00003740
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003741 needSize = buf->use + len + 2;
3742 if(needSize > buf->size){
3743 if(!xmlBufferResize(buf, needSize)){
3744 fprintf(stderr, "xmlBufferAdd : out of memory!\n");
3745 return;
3746 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00003747 }
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003748
3749 memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
Daniel Veillarde2d034d1999-07-27 19:52:06 +00003750 buf->use += len;
3751 buf->content[buf->use] = 0;
Daniel Veillard5099ae81999-04-21 20:12:07 +00003752}
3753
3754/**
3755 * xmlBufferCat:
3756 * @buf: the buffer to dump
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003757 * @str: the xmlChar string
Daniel Veillard5099ae81999-04-21 20:12:07 +00003758 *
3759 * Append a zero terminated string to an XML buffer.
3760 */
3761void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003762xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003763 if (str != NULL)
3764 xmlBufferAdd(buf, str, -1);
Daniel Veillard5099ae81999-04-21 20:12:07 +00003765}
3766
3767/**
3768 * xmlBufferCCat:
3769 * @buf: the buffer to dump
3770 * @str: the C char string
3771 *
3772 * Append a zero terminated C string to an XML buffer.
3773 */
3774void
3775xmlBufferCCat(xmlBufferPtr buf, const char *str) {
3776 const char *cur;
3777
3778 if (str == NULL) {
3779 fprintf(stderr, "xmlBufferAdd: str == NULL\n");
3780 return;
3781 }
3782 for (cur = str;*cur != 0;cur++) {
3783 if (buf->use + 10 >= buf->size) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003784 if(!xmlBufferResize(buf, buf->use+10)){
3785 fprintf(stderr, "xmlBufferCCat : out of memory!\n");
3786 return;
3787 }
3788 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00003789 buf->content[buf->use++] = *cur;
3790 }
3791}
Daniel Veillard260a68f1998-08-13 03:39:55 +00003792
Daniel Veillard97b58771998-10-20 06:14:16 +00003793/**
3794 * xmlBufferWriteCHAR:
Daniel Veillard5099ae81999-04-21 20:12:07 +00003795 * @buf: the XML buffer
Daniel Veillard97b58771998-10-20 06:14:16 +00003796 * @string: the string to add
3797 *
3798 * routine which manage and grows an output buffer. This one add
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003799 * xmlChars at the end of the buffer.
Daniel Veillard97b58771998-10-20 06:14:16 +00003800 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003801void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003802xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00003803 xmlBufferCat(buf, string);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003804}
3805
Daniel Veillard97b58771998-10-20 06:14:16 +00003806/**
3807 * xmlBufferWriteChar:
Daniel Veillard011b63c1999-06-02 17:44:04 +00003808 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00003809 * @string: the string to add
3810 *
3811 * routine which manage and grows an output buffer. This one add
3812 * C chars at the end of the array.
3813 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003814void
Daniel Veillard5099ae81999-04-21 20:12:07 +00003815xmlBufferWriteChar(xmlBufferPtr buf, const char *string) {
3816 xmlBufferCCat(buf, string);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003817}
3818
Daniel Veillard5099ae81999-04-21 20:12:07 +00003819
Daniel Veillard97b58771998-10-20 06:14:16 +00003820/**
Daniel Veillard011b63c1999-06-02 17:44:04 +00003821 * xmlBufferWriteQuotedString:
3822 * @buf: the XML buffer output
3823 * @string: the string to add
3824 *
3825 * routine which manage and grows an output buffer. This one writes
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003826 * a quoted or double quoted xmlChar string, checking first if it holds
Daniel Veillard011b63c1999-06-02 17:44:04 +00003827 * quote or double-quotes internally
3828 */
3829void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003830xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) {
Daniel Veillardb96e6431999-08-29 21:02:19 +00003831 if (xmlStrchr(string, '"')) {
3832 if (xmlStrchr(string, '\'')) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00003833 fprintf(stderr,
3834 "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n");
3835 }
3836 xmlBufferCCat(buf, "'");
3837 xmlBufferCat(buf, string);
3838 xmlBufferCCat(buf, "'");
3839 } else {
3840 xmlBufferCCat(buf, "\"");
3841 xmlBufferCat(buf, string);
3842 xmlBufferCCat(buf, "\"");
3843 }
3844}
3845
3846
3847/**
Daniel Veillard97b58771998-10-20 06:14:16 +00003848 * xmlGlobalNsDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00003849 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00003850 * @cur: a namespace
3851 *
3852 * Dump a global Namespace, this is the old version based on PIs.
Daniel Veillard260a68f1998-08-13 03:39:55 +00003853 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003854static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00003855xmlGlobalNsDump(xmlBufferPtr buf, xmlNsPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003856 if (cur == NULL) {
3857 fprintf(stderr, "xmlGlobalNsDump : Ns == NULL\n");
3858 return;
3859 }
3860 if (cur->type == XML_GLOBAL_NAMESPACE) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00003861 xmlBufferWriteChar(buf, "<?namespace");
Daniel Veillard260a68f1998-08-13 03:39:55 +00003862 if (cur->href != NULL) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00003863 xmlBufferWriteChar(buf, " href=");
3864 xmlBufferWriteQuotedString(buf, cur->href);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003865 }
3866 if (cur->prefix != NULL) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00003867 xmlBufferWriteChar(buf, " AS=");
3868 xmlBufferWriteQuotedString(buf, cur->prefix);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003869 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00003870 xmlBufferWriteChar(buf, "?>\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +00003871 }
3872}
3873
Daniel Veillard97b58771998-10-20 06:14:16 +00003874/**
3875 * xmlGlobalNsListDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00003876 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00003877 * @cur: the first namespace
3878 *
3879 * Dump a list of global Namespace, this is the old version based on PIs.
Daniel Veillard260a68f1998-08-13 03:39:55 +00003880 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003881static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00003882xmlGlobalNsListDump(xmlBufferPtr buf, xmlNsPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003883 while (cur != NULL) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00003884 xmlGlobalNsDump(buf, cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003885 cur = cur->next;
3886 }
3887}
3888
Daniel Veillard97b58771998-10-20 06:14:16 +00003889/**
3890 * xmlNsDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00003891 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00003892 * @cur: a namespace
3893 *
Daniel Veillard260a68f1998-08-13 03:39:55 +00003894 * Dump a local Namespace definition.
Daniel Veillard97b58771998-10-20 06:14:16 +00003895 * Should be called in the context of attributes dumps.
Daniel Veillard260a68f1998-08-13 03:39:55 +00003896 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003897static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00003898xmlNsDump(xmlBufferPtr buf, xmlNsPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003899 if (cur == NULL) {
3900 fprintf(stderr, "xmlNsDump : Ns == NULL\n");
3901 return;
3902 }
3903 if (cur->type == XML_LOCAL_NAMESPACE) {
3904 /* Within the context of an element attributes */
3905 if (cur->prefix != NULL) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00003906 xmlBufferWriteChar(buf, " xmlns:");
3907 xmlBufferWriteCHAR(buf, cur->prefix);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003908 } else
Daniel Veillard5099ae81999-04-21 20:12:07 +00003909 xmlBufferWriteChar(buf, " xmlns");
Daniel Veillard011b63c1999-06-02 17:44:04 +00003910 xmlBufferWriteChar(buf, "=");
3911 xmlBufferWriteQuotedString(buf, cur->href);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003912 }
3913}
3914
Daniel Veillard97b58771998-10-20 06:14:16 +00003915/**
3916 * xmlNsListDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00003917 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00003918 * @cur: the first namespace
3919 *
3920 * Dump a list of local Namespace definitions.
3921 * Should be called in the context of attributes dumps.
Daniel Veillard260a68f1998-08-13 03:39:55 +00003922 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003923static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00003924xmlNsListDump(xmlBufferPtr buf, xmlNsPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003925 while (cur != NULL) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00003926 xmlNsDump(buf, cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003927 cur = cur->next;
3928 }
3929}
3930
Daniel Veillard97b58771998-10-20 06:14:16 +00003931/**
3932 * xmlDtdDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00003933 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00003934 * @doc: the document
3935 *
3936 * Dump the XML document DTD, if any.
Daniel Veillard260a68f1998-08-13 03:39:55 +00003937 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003938static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00003939xmlDtdDump(xmlBufferPtr buf, xmlDocPtr doc) {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00003940 xmlDtdPtr cur = doc->intSubset;
Daniel Veillard260a68f1998-08-13 03:39:55 +00003941
3942 if (cur == NULL) {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00003943 fprintf(stderr, "xmlDtdDump : no internal subset\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +00003944 return;
3945 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00003946 xmlBufferWriteChar(buf, "<!DOCTYPE ");
3947 xmlBufferWriteCHAR(buf, cur->name);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003948 if (cur->ExternalID != NULL) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00003949 xmlBufferWriteChar(buf, " PUBLIC ");
3950 xmlBufferWriteQuotedString(buf, cur->ExternalID);
3951 xmlBufferWriteChar(buf, " ");
3952 xmlBufferWriteQuotedString(buf, cur->SystemID);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003953 } else if (cur->SystemID != NULL) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00003954 xmlBufferWriteChar(buf, " SYSTEM ");
3955 xmlBufferWriteQuotedString(buf, cur->SystemID);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003956 }
Daniel Veillard1e346af1999-02-22 10:33:01 +00003957 if ((cur->entities == NULL) && (cur->elements == NULL) &&
3958 (cur->attributes == NULL) && (cur->notations == NULL)) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00003959 xmlBufferWriteChar(buf, ">\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +00003960 return;
3961 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00003962 xmlBufferWriteChar(buf, " [\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +00003963 if (cur->entities != NULL)
Daniel Veillard5099ae81999-04-21 20:12:07 +00003964 xmlDumpEntitiesTable(buf, (xmlEntitiesTablePtr) cur->entities);
Daniel Veillard1e346af1999-02-22 10:33:01 +00003965 if (cur->notations != NULL)
Daniel Veillard5099ae81999-04-21 20:12:07 +00003966 xmlDumpNotationTable(buf, (xmlNotationTablePtr) cur->notations);
Daniel Veillard3b9def11999-01-31 22:15:06 +00003967 if (cur->elements != NULL)
Daniel Veillard5099ae81999-04-21 20:12:07 +00003968 xmlDumpElementTable(buf, (xmlElementTablePtr) cur->elements);
Daniel Veillard1e346af1999-02-22 10:33:01 +00003969 if (cur->attributes != NULL)
Daniel Veillard5099ae81999-04-21 20:12:07 +00003970 xmlDumpAttributeTable(buf, (xmlAttributeTablePtr) cur->attributes);
3971 xmlBufferWriteChar(buf, "]");
Daniel Veillard260a68f1998-08-13 03:39:55 +00003972
Daniel Veillard5099ae81999-04-21 20:12:07 +00003973 xmlBufferWriteChar(buf, ">\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +00003974}
3975
Daniel Veillard97b58771998-10-20 06:14:16 +00003976/**
3977 * xmlAttrDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00003978 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00003979 * @doc: the document
3980 * @cur: the attribute pointer
3981 *
3982 * Dump an XML attribute
Daniel Veillard260a68f1998-08-13 03:39:55 +00003983 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003984static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00003985xmlAttrDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) {
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003986 xmlChar *value;
Daniel Veillardccb09631998-10-27 06:21:04 +00003987
Daniel Veillard260a68f1998-08-13 03:39:55 +00003988 if (cur == NULL) {
3989 fprintf(stderr, "xmlAttrDump : property == NULL\n");
3990 return;
3991 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00003992 xmlBufferWriteChar(buf, " ");
Daniel Veillardb96e6431999-08-29 21:02:19 +00003993 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
3994 xmlBufferWriteCHAR(buf, cur->ns->prefix);
3995 xmlBufferWriteChar(buf, ":");
3996 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00003997 xmlBufferWriteCHAR(buf, cur->name);
Daniel Veillardccb09631998-10-27 06:21:04 +00003998 value = xmlNodeListGetString(doc, cur->val, 0);
3999 if (value) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00004000 xmlBufferWriteChar(buf, "=");
4001 xmlBufferWriteQuotedString(buf, value);
Daniel Veillard6454aec1999-09-02 22:04:43 +00004002 xmlFree(value);
Daniel Veillard726c7e31999-02-08 15:13:10 +00004003 } else {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004004 xmlBufferWriteChar(buf, "=\"\"");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004005 }
4006}
4007
Daniel Veillard97b58771998-10-20 06:14:16 +00004008/**
4009 * xmlAttrListDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00004010 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00004011 * @doc: the document
4012 * @cur: the first attribute pointer
4013 *
4014 * Dump a list of XML attributes
Daniel Veillard260a68f1998-08-13 03:39:55 +00004015 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004016static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00004017xmlAttrListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00004018 if (cur == NULL) {
4019 fprintf(stderr, "xmlAttrListDump : property == NULL\n");
4020 return;
4021 }
4022 while (cur != NULL) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004023 xmlAttrDump(buf, doc, cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004024 cur = cur->next;
4025 }
4026}
4027
Daniel Veillard260a68f1998-08-13 03:39:55 +00004028
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004029static void
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004030xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
4031 int format);
Daniel Veillarddbfd6411999-12-28 16:35:14 +00004032void
4033htmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur);
4034
Daniel Veillard97b58771998-10-20 06:14:16 +00004035/**
4036 * xmlNodeListDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00004037 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00004038 * @doc: the document
4039 * @cur: the first node
4040 * @level: the imbrication level for indenting
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004041 * @format: is formatting allowed
Daniel Veillard97b58771998-10-20 06:14:16 +00004042 *
4043 * Dump an XML node list, recursive behaviour,children are printed too.
4044 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004045static void
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004046xmlNodeListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
4047 int format) {
4048 int i;
Daniel Veillardccb09631998-10-27 06:21:04 +00004049
Daniel Veillard260a68f1998-08-13 03:39:55 +00004050 if (cur == NULL) {
4051 fprintf(stderr, "xmlNodeListDump : node == NULL\n");
4052 return;
4053 }
4054 while (cur != NULL) {
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004055 if ((format) && (xmlIndentTreeOutput) &&
4056 (cur->type == XML_ELEMENT_NODE))
4057 for (i = 0;i < level;i++)
4058 xmlBufferWriteChar(buf, " ");
4059 xmlNodeDump(buf, doc, cur, level, format);
4060 if (format) {
4061 xmlBufferWriteChar(buf, "\n");
Daniel Veillardccb09631998-10-27 06:21:04 +00004062 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00004063 cur = cur->next;
4064 }
4065}
4066
Daniel Veillard97b58771998-10-20 06:14:16 +00004067/**
Daniel Veillardccb09631998-10-27 06:21:04 +00004068 * xmlNodeDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00004069 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00004070 * @doc: the document
4071 * @cur: the current node
4072 * @level: the imbrication level for indenting
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004073 * @format: is formatting allowed
Daniel Veillard97b58771998-10-20 06:14:16 +00004074 *
4075 * Dump an XML node, recursive behaviour,children are printed too.
Daniel Veillard260a68f1998-08-13 03:39:55 +00004076 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004077static void
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004078xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
4079 int format) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00004080 int i;
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004081 xmlNodePtr tmp;
Daniel Veillard260a68f1998-08-13 03:39:55 +00004082
4083 if (cur == NULL) {
4084 fprintf(stderr, "xmlNodeDump : node == NULL\n");
4085 return;
4086 }
Daniel Veillard0bef1311998-10-14 02:36:47 +00004087 if (cur->type == XML_TEXT_NODE) {
Daniel Veillard14fff061999-06-22 21:49:07 +00004088 if (cur->content != NULL) {
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004089 xmlChar *buffer;
Daniel Veillard14fff061999-06-22 21:49:07 +00004090
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004091#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard14fff061999-06-22 21:49:07 +00004092 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004093#else
4094 buffer = xmlEncodeEntitiesReentrant(doc,
4095 xmlBufferContent(cur->content));
4096#endif
Daniel Veillard14fff061999-06-22 21:49:07 +00004097 if (buffer != NULL) {
4098 xmlBufferWriteCHAR(buf, buffer);
Daniel Veillard6454aec1999-09-02 22:04:43 +00004099 xmlFree(buffer);
Daniel Veillard14fff061999-06-22 21:49:07 +00004100 }
4101 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00004102 return;
4103 }
Daniel Veillardb96e6431999-08-29 21:02:19 +00004104 if (cur->type == XML_PI_NODE) {
4105 if (cur->content != NULL) {
4106 xmlBufferWriteChar(buf, "<?");
4107 xmlBufferWriteCHAR(buf, cur->name);
4108 if (cur->content != NULL) {
4109 xmlBufferWriteChar(buf, " ");
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004110#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardb96e6431999-08-29 21:02:19 +00004111 xmlBufferWriteCHAR(buf, cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004112#else
4113 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
4114#endif
Daniel Veillardb96e6431999-08-29 21:02:19 +00004115 }
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004116 xmlBufferWriteChar(buf, "?>");
Daniel Veillardb96e6431999-08-29 21:02:19 +00004117 }
4118 return;
4119 }
Daniel Veillard0bef1311998-10-14 02:36:47 +00004120 if (cur->type == XML_COMMENT_NODE) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00004121 if (cur->content != NULL) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004122 xmlBufferWriteChar(buf, "<!--");
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004123#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard5099ae81999-04-21 20:12:07 +00004124 xmlBufferWriteCHAR(buf, cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004125#else
4126 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
4127#endif
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004128 xmlBufferWriteChar(buf, "-->");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004129 }
4130 return;
4131 }
Daniel Veillardccb09631998-10-27 06:21:04 +00004132 if (cur->type == XML_ENTITY_REF_NODE) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004133 xmlBufferWriteChar(buf, "&");
4134 xmlBufferWriteCHAR(buf, cur->name);
4135 xmlBufferWriteChar(buf, ";");
Daniel Veillardccb09631998-10-27 06:21:04 +00004136 return;
4137 }
Daniel Veillardb05deb71999-08-10 19:04:08 +00004138 if (cur->type == XML_CDATA_SECTION_NODE) {
4139 xmlBufferWriteChar(buf, "<![CDATA[");
4140 if (cur->content != NULL)
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004141#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardb05deb71999-08-10 19:04:08 +00004142 xmlBufferWriteCHAR(buf, cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004143#else
4144 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
4145#endif
Daniel Veillardb05deb71999-08-10 19:04:08 +00004146 xmlBufferWriteChar(buf, "]]>");
4147 return;
4148 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00004149
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004150 if (format == 1) {
4151 tmp = cur->childs;
4152 while (tmp != NULL) {
4153 if ((tmp->type == XML_TEXT_NODE) ||
4154 (tmp->type == XML_ENTITY_REF_NODE)) {
4155 format = 0;
4156 break;
4157 }
4158 tmp = tmp->next;
4159 }
4160 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004161 xmlBufferWriteChar(buf, "<");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004162 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004163 xmlBufferWriteCHAR(buf, cur->ns->prefix);
4164 xmlBufferWriteChar(buf, ":");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004165 }
4166
Daniel Veillard5099ae81999-04-21 20:12:07 +00004167 xmlBufferWriteCHAR(buf, cur->name);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004168 if (cur->nsDef)
Daniel Veillard5099ae81999-04-21 20:12:07 +00004169 xmlNsListDump(buf, cur->nsDef);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004170 if (cur->properties != NULL)
Daniel Veillard5099ae81999-04-21 20:12:07 +00004171 xmlAttrListDump(buf, doc, cur->properties);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004172
4173 if ((cur->content == NULL) && (cur->childs == NULL)) {
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004174 xmlBufferWriteChar(buf, "/>");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004175 return;
4176 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004177 xmlBufferWriteChar(buf, ">");
Daniel Veillard14fff061999-06-22 21:49:07 +00004178 if (cur->content != NULL) {
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004179 xmlChar *buffer;
Daniel Veillard14fff061999-06-22 21:49:07 +00004180
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004181#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard14fff061999-06-22 21:49:07 +00004182 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004183#else
4184 buffer = xmlEncodeEntitiesReentrant(doc,
4185 xmlBufferContent(cur->content));
4186#endif
Daniel Veillard14fff061999-06-22 21:49:07 +00004187 if (buffer != NULL) {
4188 xmlBufferWriteCHAR(buf, buffer);
Daniel Veillard6454aec1999-09-02 22:04:43 +00004189 xmlFree(buffer);
Daniel Veillard14fff061999-06-22 21:49:07 +00004190 }
4191 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00004192 if (cur->childs != NULL) {
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004193 if (format) xmlBufferWriteChar(buf, "\n");
4194 xmlNodeListDump(buf, doc, cur->childs, level + 1, format);
4195 if ((xmlIndentTreeOutput) && (format))
4196 for (i = 0;i < level;i++)
4197 xmlBufferWriteChar(buf, " ");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004198 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004199 xmlBufferWriteChar(buf, "</");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004200 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004201 xmlBufferWriteCHAR(buf, cur->ns->prefix);
4202 xmlBufferWriteChar(buf, ":");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004203 }
4204
Daniel Veillard5099ae81999-04-21 20:12:07 +00004205 xmlBufferWriteCHAR(buf, cur->name);
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004206 xmlBufferWriteChar(buf, ">");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004207}
4208
Daniel Veillard97b58771998-10-20 06:14:16 +00004209/**
Daniel Veillarddbfd6411999-12-28 16:35:14 +00004210 * xmlElemDump:
4211 * @buf: the XML buffer output
4212 * @doc: the document
4213 * @cur: the current node
4214 *
4215 * Dump an XML/HTML node, recursive behaviour,children are printed too.
4216 */
4217void
4218xmlElemDump(FILE *f, xmlDocPtr doc, xmlNodePtr cur) {
4219 xmlBufferPtr buf;
4220
4221 if (cur == NULL) {
4222#ifdef DEBUG_TREE
4223 fprintf(stderr, "xmlElemDump : cur == NULL\n");
4224#endif
4225 return;
4226 }
4227 if (doc == NULL) {
4228#ifdef DEBUG_TREE
4229 fprintf(stderr, "xmlElemDump : doc == NULL\n");
4230#endif
4231 }
4232 buf = xmlBufferCreate();
4233 if (buf == NULL) return;
4234 if ((doc != NULL) &&
4235 (doc->type == XML_HTML_DOCUMENT_NODE)) {
4236 htmlNodeDump(buf, doc, cur);
4237 } else
4238 xmlNodeDump(buf, doc, cur, 0, 1);
4239 xmlBufferDump(f, buf);
4240 xmlBufferFree(buf);
4241}
4242
4243/**
Daniel Veillard97b58771998-10-20 06:14:16 +00004244 * xmlDocContentDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00004245 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00004246 * @cur: the document
4247 *
4248 * Dump an XML document.
Daniel Veillard260a68f1998-08-13 03:39:55 +00004249 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004250static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00004251xmlDocContentDump(xmlBufferPtr buf, xmlDocPtr cur) {
Daniel Veillardbe70ff71999-07-05 16:50:46 +00004252 xmlBufferWriteChar(buf, "<?xml version=");
4253 if (cur->version != NULL)
4254 xmlBufferWriteQuotedString(buf, cur->version);
4255 else
4256 xmlBufferWriteChar(buf, "\"1.0\"");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004257 if (cur->encoding != NULL) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00004258 xmlBufferWriteChar(buf, " encoding=");
4259 xmlBufferWriteQuotedString(buf, cur->encoding);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004260 }
4261 switch (cur->standalone) {
4262 case 0:
Daniel Veillard5099ae81999-04-21 20:12:07 +00004263 xmlBufferWriteChar(buf, " standalone=\"no\"");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004264 break;
4265 case 1:
Daniel Veillard5099ae81999-04-21 20:12:07 +00004266 xmlBufferWriteChar(buf, " standalone=\"yes\"");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004267 break;
4268 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004269 xmlBufferWriteChar(buf, "?>\n");
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00004270 if (cur->intSubset != NULL)
Daniel Veillard5099ae81999-04-21 20:12:07 +00004271 xmlDtdDump(buf, cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004272 if (cur->root != NULL) {
Daniel Veillardb96e6431999-08-29 21:02:19 +00004273 xmlNodePtr child = cur->root;
4274
Daniel Veillard260a68f1998-08-13 03:39:55 +00004275 /* global namespace definitions, the old way */
4276 if (oldXMLWDcompatibility)
Daniel Veillard5099ae81999-04-21 20:12:07 +00004277 xmlGlobalNsListDump(buf, cur->oldNs);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004278 else
4279 xmlUpgradeOldNs(cur);
Daniel Veillardb96e6431999-08-29 21:02:19 +00004280
4281 while (child != NULL) {
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004282 xmlNodeDump(buf, cur, child, 0, 1);
4283 xmlBufferWriteChar(buf, "\n");
Daniel Veillardb96e6431999-08-29 21:02:19 +00004284 child = child->next;
4285 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00004286 }
4287}
4288
Daniel Veillard97b58771998-10-20 06:14:16 +00004289/**
4290 * xmlDocDumpMemory:
4291 * @cur: the document
4292 * @mem: OUT: the memory pointer
4293 * @size: OUT: the memory lenght
4294 *
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004295 * Dump an XML document in memory and return the xmlChar * and it's size.
Daniel Veillard97b58771998-10-20 06:14:16 +00004296 * It's up to the caller to free the memory.
Daniel Veillard260a68f1998-08-13 03:39:55 +00004297 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004298void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004299xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004300 xmlBufferPtr buf;
4301
Daniel Veillard260a68f1998-08-13 03:39:55 +00004302 if (cur == NULL) {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00004303#ifdef DEBUG_TREE
4304 fprintf(stderr, "xmlDocDumpMemory : document == NULL\n");
4305#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00004306 *mem = NULL;
4307 *size = 0;
4308 return;
4309 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004310 buf = xmlBufferCreate();
4311 if (buf == NULL) {
4312 *mem = NULL;
4313 *size = 0;
4314 return;
4315 }
4316 xmlDocContentDump(buf, cur);
Daniel Veillardb05deb71999-08-10 19:04:08 +00004317 *mem = xmlStrndup(buf->content, buf->use);
Daniel Veillard5099ae81999-04-21 20:12:07 +00004318 *size = buf->use;
Daniel Veillardb05deb71999-08-10 19:04:08 +00004319 xmlBufferFree(buf);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004320}
4321
Daniel Veillard97b58771998-10-20 06:14:16 +00004322/**
4323 * xmlGetDocCompressMode:
4324 * @doc: the document
4325 *
4326 * get the compression ratio for a document, ZLIB based
Daniel Veillard1e346af1999-02-22 10:33:01 +00004327 * Returns 0 (uncompressed) to 9 (max compression)
Daniel Veillard151b1b01998-09-23 00:49:46 +00004328 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004329int
Daniel Veillarddbfd6411999-12-28 16:35:14 +00004330xmlGetDocCompressMode (xmlDocPtr doc) {
Daniel Veillard15a8df41998-09-24 19:15:06 +00004331 if (doc == NULL) return(-1);
4332 return(doc->compression);
4333}
4334
Daniel Veillard97b58771998-10-20 06:14:16 +00004335/**
4336 * xmlSetDocCompressMode:
4337 * @doc: the document
4338 * @mode: the compression ratio
4339 *
4340 * set the compression ratio for a document, ZLIB based
4341 * Correct values: 0 (uncompressed) to 9 (max compression)
4342 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004343void
4344xmlSetDocCompressMode (xmlDocPtr doc, int mode) {
Daniel Veillard15a8df41998-09-24 19:15:06 +00004345 if (doc == NULL) return;
4346 if (mode < 0) doc->compression = 0;
4347 else if (mode > 9) doc->compression = 9;
4348 else doc->compression = mode;
4349}
4350
Daniel Veillard97b58771998-10-20 06:14:16 +00004351/**
4352 * xmlGetCompressMode:
4353 *
4354 * get the default compression mode used, ZLIB based.
Daniel Veillard1e346af1999-02-22 10:33:01 +00004355 * Returns 0 (uncompressed) to 9 (max compression)
Daniel Veillard15a8df41998-09-24 19:15:06 +00004356 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004357int
4358 xmlGetCompressMode(void) {
Daniel Veillard151b1b01998-09-23 00:49:46 +00004359 return(xmlCompressMode);
4360}
Daniel Veillard97b58771998-10-20 06:14:16 +00004361
4362/**
4363 * xmlSetCompressMode:
4364 * @mode: the compression ratio
4365 *
4366 * set the default compression mode used, ZLIB based
4367 * Correct values: 0 (uncompressed) to 9 (max compression)
4368 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004369void
4370xmlSetCompressMode(int mode) {
Daniel Veillard151b1b01998-09-23 00:49:46 +00004371 if (mode < 0) xmlCompressMode = 0;
Daniel Veillard15a8df41998-09-24 19:15:06 +00004372 else if (mode > 9) xmlCompressMode = 9;
Daniel Veillard151b1b01998-09-23 00:49:46 +00004373 else xmlCompressMode = mode;
4374}
4375
Daniel Veillard97b58771998-10-20 06:14:16 +00004376/**
4377 * xmlDocDump:
4378 * @f: the FILE*
4379 * @cur: the document
4380 *
4381 * Dump an XML document to an open FILE.
Daniel Veillard260a68f1998-08-13 03:39:55 +00004382 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004383void
4384xmlDocDump(FILE *f, xmlDocPtr cur) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004385 xmlBufferPtr buf;
4386
Daniel Veillard260a68f1998-08-13 03:39:55 +00004387 if (cur == NULL) {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00004388#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00004389 fprintf(stderr, "xmlDocDump : document == NULL\n");
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00004390#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00004391 return;
4392 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004393 buf = xmlBufferCreate();
4394 if (buf == NULL) return;
4395 xmlDocContentDump(buf, cur);
4396 xmlBufferDump(f, buf);
4397 xmlBufferFree(buf);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004398}
4399
Daniel Veillard97b58771998-10-20 06:14:16 +00004400/**
4401 * xmlSaveFile:
4402 * @filename: the filename
4403 * @cur: the document
4404 *
4405 * Dump an XML document to a file. Will use compression if
Daniel Veillard11a48ec1999-11-23 10:40:46 +00004406 * compiled in and enabled. If @filename is "-" the stdout file is
4407 * used.
Daniel Veillard97b58771998-10-20 06:14:16 +00004408 * returns: the number of file written or -1 in case of failure.
Daniel Veillard151b1b01998-09-23 00:49:46 +00004409 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004410int
4411xmlSaveFile(const char *filename, xmlDocPtr cur) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004412 xmlBufferPtr buf;
Daniel Veillard151b1b01998-09-23 00:49:46 +00004413#ifdef HAVE_ZLIB_H
4414 gzFile zoutput = NULL;
4415 char mode[15];
4416#endif
Daniel Veillardccb09631998-10-27 06:21:04 +00004417 FILE *output = NULL;
Daniel Veillard151b1b01998-09-23 00:49:46 +00004418 int ret;
4419
Daniel Veillard5099ae81999-04-21 20:12:07 +00004420 /*
4421 * save the content to a temp buffer.
4422 */
4423 buf = xmlBufferCreate();
4424 if (buf == NULL) return(0);
4425 xmlDocContentDump(buf, cur);
4426
Daniel Veillard151b1b01998-09-23 00:49:46 +00004427#ifdef HAVE_ZLIB_H
Daniel Veillard11a48ec1999-11-23 10:40:46 +00004428 if (cur->compression < 0) cur->compression = xmlCompressMode;
Daniel Veillarddc3dd9d1998-09-24 19:25:54 +00004429 if ((cur->compression > 0) && (cur->compression <= 9)) {
4430 sprintf(mode, "w%d", cur->compression);
Daniel Veillard11a48ec1999-11-23 10:40:46 +00004431 if (!strcmp(filename, "-"))
4432 zoutput = gzdopen(1, mode);
4433 else
4434 zoutput = gzopen(filename, mode);
Daniel Veillard151b1b01998-09-23 00:49:46 +00004435 }
4436 if (zoutput == NULL) {
4437#endif
4438 output = fopen(filename, "w");
Daniel Veillard10a2c651999-12-12 13:03:50 +00004439 if (output == NULL) {
4440 xmlBufferFree(buf);
4441 return(-1);
4442 }
Daniel Veillard151b1b01998-09-23 00:49:46 +00004443#ifdef HAVE_ZLIB_H
4444 }
Daniel Veillard151b1b01998-09-23 00:49:46 +00004445
Daniel Veillard151b1b01998-09-23 00:49:46 +00004446 if (zoutput != NULL) {
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004447 ret = gzwrite(zoutput, buf->content, sizeof(xmlChar) * buf->use);
Daniel Veillard151b1b01998-09-23 00:49:46 +00004448 gzclose(zoutput);
Daniel Veillard5099ae81999-04-21 20:12:07 +00004449 } else {
4450#endif
4451 ret = xmlBufferDump(output, buf);
4452 fclose(output);
4453#ifdef HAVE_ZLIB_H
Daniel Veillard151b1b01998-09-23 00:49:46 +00004454 }
4455#endif
Manish Vachharajani5e60f5a1999-05-29 03:04:30 +00004456 xmlBufferFree(buf);
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004457 return(ret * sizeof(xmlChar));
Daniel Veillard151b1b01998-09-23 00:49:46 +00004458}
4459