blob: b0678760985fa78edc5fc65afd6531203db40339 [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 Veillard361d8452000-04-03 19:48:13 +000028#include <libxml/xmlmemory.h>
29#include <libxml/tree.h>
30#include <libxml/parser.h>
31#include <libxml/entities.h>
32#include <libxml/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 Veillardcf461992000-03-14 18:30:20 +000035static xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 };
Daniel Veillard260a68f1998-08-13 03:39:55 +000036int oldXMLWDcompatibility = 0;
Daniel Veillardcf461992000-03-14 18:30:20 +000037int xmlIndentTreeOutput = 0;
Daniel Veillardf5c2c871999-12-01 09:51:45 +000038xmlBufferAllocationScheme xmlBufferAllocScheme = XML_BUFFER_ALLOC_EXACT;
Daniel Veillard260a68f1998-08-13 03:39:55 +000039
Daniel Veillard15a8df41998-09-24 19:15:06 +000040static int xmlCompressMode = 0;
Daniel Veillard10a2c651999-12-12 13:03:50 +000041static int xmlCheckDTD = 1;
Daniel Veillarde41f2b72000-01-30 20:00:07 +000042int xmlSaveNoEmptyTags = 0;
Daniel Veillard3e6d2372000-03-04 11:39:43 +000043
44#define IS_BLANK(c) \
45 (((c) == '\n') || ((c) == '\r') || ((c) == '\t') || ((c) == ' '))
Daniel Veillard15a8df41998-09-24 19:15:06 +000046
Daniel Veillard39a1f9a1999-01-17 19:11:59 +000047#define UPDATE_LAST_CHILD(n) if ((n) != NULL) { \
Daniel Veillardcf461992000-03-14 18:30:20 +000048 xmlNodePtr ulccur = (n)->children; \
Daniel Veillard39a1f9a1999-01-17 19:11:59 +000049 if (ulccur == NULL) { \
50 (n)->last = NULL; \
51 } else { \
52 while (ulccur->next != NULL) ulccur = ulccur->next; \
53 (n)->last = ulccur; \
Daniel Veillard1e346af1999-02-22 10:33:01 +000054}}
Daniel Veillard39a1f9a1999-01-17 19:11:59 +000055
Daniel Veillardad8f99d2000-01-15 14:20:03 +000056/* #define DEBUG_BUFFER */
57/* #define DEBUG_TREE */
58
Daniel Veillard260a68f1998-08-13 03:39:55 +000059/************************************************************************
60 * *
61 * Allocation and deallocation of basic structures *
62 * *
63 ************************************************************************/
64
Daniel Veillard97b58771998-10-20 06:14:16 +000065/**
Daniel Veillardf5c2c871999-12-01 09:51:45 +000066 * xmlSetBufferAllocationScheme:
67 * @scheme: allocation method to use
68 *
69 * Set the buffer allocation method. Types are
70 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
71 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
72 * improves performance
73 */
74void
75xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme) {
76 xmlBufferAllocScheme = scheme;
77}
78
79/**
80 * xmlGetBufferAllocationScheme:
81 *
82 * Types are
83 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
84 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
85 * improves performance
86 *
87 * Returns the current allocation scheme
88 */
89xmlBufferAllocationScheme
90xmlGetBufferAllocationScheme() {
91 return xmlBufferAllocScheme;
92}
93
94/**
Daniel Veillard97b58771998-10-20 06:14:16 +000095 * xmlUpgradeOldNs:
96 * @doc: a document pointer
97 *
98 * Upgrade old style Namespaces (PI) and move them to the root of the document.
Daniel Veillard260a68f1998-08-13 03:39:55 +000099 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000100void
101xmlUpgradeOldNs(xmlDocPtr doc) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000102 xmlNsPtr cur;
103
104 if ((doc == NULL) || (doc->oldNs == NULL)) return;
Daniel Veillardcf461992000-03-14 18:30:20 +0000105 if (doc->children == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000106#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +0000107 fprintf(stderr, "xmlUpgradeOldNs: failed no root !\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000108#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +0000109 return;
110 }
111
112 cur = doc->oldNs;
113 while (cur->next != NULL) {
114 cur->type = XML_LOCAL_NAMESPACE;
115 cur = cur->next;
116 }
117 cur->type = XML_LOCAL_NAMESPACE;
Daniel Veillardcf461992000-03-14 18:30:20 +0000118 cur->next = doc->children->nsDef;
119 doc->children->nsDef = doc->oldNs;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000120 doc->oldNs = NULL;
121}
122
Daniel Veillard97b58771998-10-20 06:14:16 +0000123/**
124 * xmlNewNs:
125 * @node: the element carrying the namespace
126 * @href: the URI associated
127 * @prefix: the prefix for the namespace
128 *
Daniel Veillard686d6b62000-01-03 11:08:02 +0000129 * Creation of a new Namespace. This function will refuse to create
130 * a namespace with a similar prefix than an existing one present on this
131 * node.
132 * Returns returns a new namespace pointer or NULL
Daniel Veillard260a68f1998-08-13 03:39:55 +0000133 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000134xmlNsPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000135xmlNewNs(xmlNodePtr node, const xmlChar *href, const xmlChar *prefix) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000136 xmlNsPtr cur;
137
138 if (href == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000139#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +0000140 fprintf(stderr, "xmlNewNs: href == NULL !\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000141#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +0000142 return(NULL);
143 }
144
145 /*
Daniel Veillardcf461992000-03-14 18:30:20 +0000146 * Allocate a new Namespace and fill the fields.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000147 */
Daniel Veillard6454aec1999-09-02 22:04:43 +0000148 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
Daniel Veillard260a68f1998-08-13 03:39:55 +0000149 if (cur == NULL) {
150 fprintf(stderr, "xmlNewNs : malloc failed\n");
151 return(NULL);
152 }
Daniel Veillardcf461992000-03-14 18:30:20 +0000153 memset(cur, 0, sizeof(xmlNs));
Daniel Veillard260a68f1998-08-13 03:39:55 +0000154 cur->type = XML_LOCAL_NAMESPACE;
Daniel Veillardcf461992000-03-14 18:30:20 +0000155
Daniel Veillard260a68f1998-08-13 03:39:55 +0000156 if (href != NULL)
157 cur->href = xmlStrdup(href);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000158 if (prefix != NULL)
159 cur->prefix = xmlStrdup(prefix);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000160
161 /*
162 * Add it at the end to preserve parsing order ...
Daniel Veillard686d6b62000-01-03 11:08:02 +0000163 * and checks for existing use of the prefix
Daniel Veillard260a68f1998-08-13 03:39:55 +0000164 */
Daniel Veillard260a68f1998-08-13 03:39:55 +0000165 if (node != NULL) {
166 if (node->nsDef == NULL) {
167 node->nsDef = cur;
168 } else {
169 xmlNsPtr prev = node->nsDef;
170
Daniel Veillard0142b842000-01-14 14:45:24 +0000171 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
172 (!xmlStrcmp(prev->prefix, cur->prefix))) {
173 xmlFreeNs(cur);
174 return(NULL);
175 }
Daniel Veillard686d6b62000-01-03 11:08:02 +0000176 while (prev->next != NULL) {
Daniel Veillard0142b842000-01-14 14:45:24 +0000177 prev = prev->next;
Daniel Veillard686d6b62000-01-03 11:08:02 +0000178 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
179 (!xmlStrcmp(prev->prefix, cur->prefix))) {
180 xmlFreeNs(cur);
181 return(NULL);
182 }
Daniel Veillard686d6b62000-01-03 11:08:02 +0000183 }
Daniel Veillard260a68f1998-08-13 03:39:55 +0000184 prev->next = cur;
185 }
186 }
Daniel Veillard260a68f1998-08-13 03:39:55 +0000187 return(cur);
188}
189
Daniel Veillard97b58771998-10-20 06:14:16 +0000190/**
191 * xmlNewGlobalNs:
192 * @doc: the document carrying the namespace
193 * @href: the URI associated
194 * @prefix: the prefix for the namespace
195 *
Daniel Veillard686d6b62000-01-03 11:08:02 +0000196 * Creation of a Namespace, the old way using PI and without scoping
197 * DEPRECATED !!!
Daniel Veillard0142b842000-01-14 14:45:24 +0000198 * It now create a namespace on the root element of the document if found.
Daniel Veillard686d6b62000-01-03 11:08:02 +0000199 * Returns NULL this functionnality had been removed
Daniel Veillard260a68f1998-08-13 03:39:55 +0000200 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000201xmlNsPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000202xmlNewGlobalNs(xmlDocPtr doc, const xmlChar *href, const xmlChar *prefix) {
Daniel Veillard0142b842000-01-14 14:45:24 +0000203 xmlNodePtr root;
204
205 xmlNsPtr cur;
206
207 root = xmlDocGetRootElement(doc);
208 if (root != NULL)
209 return(xmlNewNs(root, href, prefix));
210
211 /*
212 * if there is no root element yet, create an old Namespace type
213 * and it will be moved to the root at save time.
214 */
215 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
216 if (cur == NULL) {
217 fprintf(stderr, "xmlNewGlobalNs : malloc failed\n");
218 return(NULL);
219 }
Daniel Veillardcf461992000-03-14 18:30:20 +0000220 memset(cur, 0, sizeof(xmlNs));
Daniel Veillard0142b842000-01-14 14:45:24 +0000221 cur->type = XML_GLOBAL_NAMESPACE;
Daniel Veillardcf461992000-03-14 18:30:20 +0000222
Daniel Veillard0142b842000-01-14 14:45:24 +0000223 if (href != NULL)
224 cur->href = xmlStrdup(href);
Daniel Veillard0142b842000-01-14 14:45:24 +0000225 if (prefix != NULL)
226 cur->prefix = xmlStrdup(prefix);
Daniel Veillard0142b842000-01-14 14:45:24 +0000227
228 /*
229 * Add it at the end to preserve parsing order ...
230 */
Daniel Veillard0142b842000-01-14 14:45:24 +0000231 if (doc != NULL) {
232 if (doc->oldNs == NULL) {
233 doc->oldNs = cur;
234 } else {
235 xmlNsPtr prev = doc->oldNs;
236
237 while (prev->next != NULL) prev = prev->next;
238 prev->next = cur;
239 }
240 }
241
242 return(NULL);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000243}
244
Daniel Veillard97b58771998-10-20 06:14:16 +0000245/**
246 * xmlSetNs:
247 * @node: a node in the document
248 * @ns: a namespace pointer
249 *
250 * Associate a namespace to a node, a posteriori.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000251 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000252void
253xmlSetNs(xmlNodePtr node, xmlNsPtr ns) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000254 if (node == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000255#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +0000256 fprintf(stderr, "xmlSetNs: node == NULL\n");
Daniel Veillardcf461992000-03-14 18:30:20 +0000257#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +0000258 return;
259 }
260 node->ns = ns;
261}
262
Daniel Veillard97b58771998-10-20 06:14:16 +0000263/**
264 * xmlFreeNs:
265 * @cur: the namespace pointer
266 *
267 * Free up the structures associated to a namespace
Daniel Veillard260a68f1998-08-13 03:39:55 +0000268 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000269void
270xmlFreeNs(xmlNsPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000271 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000272#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +0000273 fprintf(stderr, "xmlFreeNs : ns == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000274#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +0000275 return;
276 }
Daniel Veillard6454aec1999-09-02 22:04:43 +0000277 if (cur->href != NULL) xmlFree((char *) cur->href);
278 if (cur->prefix != NULL) xmlFree((char *) cur->prefix);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000279 memset(cur, -1, sizeof(xmlNs));
Daniel Veillard6454aec1999-09-02 22:04:43 +0000280 xmlFree(cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000281}
282
Daniel Veillard97b58771998-10-20 06:14:16 +0000283/**
284 * xmlFreeNsList:
285 * @cur: the first namespace pointer
286 *
287 * Free up all the structures associated to the chained namespaces.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000288 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000289void
290xmlFreeNsList(xmlNsPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000291 xmlNsPtr next;
292 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000293#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +0000294 fprintf(stderr, "xmlFreeNsList : ns == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000295#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +0000296 return;
297 }
298 while (cur != NULL) {
299 next = cur->next;
300 xmlFreeNs(cur);
301 cur = next;
302 }
303}
304
Daniel Veillard97b58771998-10-20 06:14:16 +0000305/**
306 * xmlNewDtd:
307 * @doc: the document pointer
308 * @name: the DTD name
309 * @ExternalID: the external ID
310 * @SystemID: the system ID
311 *
Daniel Veillardcf461992000-03-14 18:30:20 +0000312 * Creation of a new DTD for the external subset. To create an
313 * internal subset, use xmlCreateIntSubset().
314 *
Daniel Veillard1e346af1999-02-22 10:33:01 +0000315 * Returns a pointer to the new DTD structure
Daniel Veillard260a68f1998-08-13 03:39:55 +0000316 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000317xmlDtdPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000318xmlNewDtd(xmlDocPtr doc, const xmlChar *name,
319 const xmlChar *ExternalID, const xmlChar *SystemID) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000320 xmlDtdPtr cur;
321
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000322 if ((doc != NULL) && (doc->extSubset != NULL)) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000323#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +0000324 fprintf(stderr, "xmlNewDtd(%s): document %s already have a DTD %s\n",
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000325 /* !!! */ (char *) name, doc->name,
326 /* !!! */ (char *)doc->extSubset->name);
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000327#endif
328 return(NULL);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000329 }
330
331 /*
332 * Allocate a new DTD and fill the fields.
333 */
Daniel Veillard6454aec1999-09-02 22:04:43 +0000334 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
Daniel Veillard260a68f1998-08-13 03:39:55 +0000335 if (cur == NULL) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000336 fprintf(stderr, "xmlNewDtd : malloc failed\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +0000337 return(NULL);
338 }
Daniel Veillardcf461992000-03-14 18:30:20 +0000339 memset(cur, 0 , sizeof(xmlDtd));
340 cur->type = XML_DTD_NODE;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000341
342 if (name != NULL)
343 cur->name = xmlStrdup(name);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000344 if (ExternalID != NULL)
345 cur->ExternalID = xmlStrdup(ExternalID);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000346 if (SystemID != NULL)
347 cur->SystemID = xmlStrdup(SystemID);
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000348 if (doc != NULL)
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000349 doc->extSubset = cur;
Daniel Veillardcf461992000-03-14 18:30:20 +0000350 cur->doc = doc;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000351
352 return(cur);
353}
354
355/**
Daniel Veillardcf461992000-03-14 18:30:20 +0000356 * xmlGetIntSubset:
357 * @doc: the document pointer
358 *
359 * Get the internal subset of a document
360 * Returns a pointer to the DTD structure or NULL if not found
361 */
362
363xmlDtdPtr
364xmlGetIntSubset(xmlDocPtr doc) {
365 xmlNodePtr cur;
366
367 if (doc == NULL)
368 return(NULL);
369 cur = doc->children;
370 while (cur != NULL) {
371 if (cur->type == XML_DTD_NODE)
372 return((xmlDtdPtr) cur);
373 cur = cur->next;
374 }
375 return((xmlDtdPtr) doc->intSubset);
376}
377
378/**
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000379 * xmlCreateIntSubset:
380 * @doc: the document pointer
381 * @name: the DTD name
382 * @ExternalID: the external ID
383 * @SystemID: the system ID
384 *
Daniel Veillard1e346af1999-02-22 10:33:01 +0000385 * Create the internal subset of a document
386 * Returns a pointer to the new DTD structure
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000387 */
388xmlDtdPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000389xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name,
390 const xmlChar *ExternalID, const xmlChar *SystemID) {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000391 xmlDtdPtr cur;
392
Daniel Veillardcf461992000-03-14 18:30:20 +0000393 if ((doc != NULL) && (xmlGetIntSubset(doc) != NULL)) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000394#ifdef DEBUG_TREE
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000395 fprintf(stderr,
396 "xmlCreateIntSubset(): document %s already have an internal subset\n",
397 doc->name);
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000398#endif
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000399 return(NULL);
400 }
401
402 /*
403 * Allocate a new DTD and fill the fields.
404 */
Daniel Veillard6454aec1999-09-02 22:04:43 +0000405 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000406 if (cur == NULL) {
407 fprintf(stderr, "xmlNewDtd : malloc failed\n");
408 return(NULL);
409 }
Daniel Veillardcf461992000-03-14 18:30:20 +0000410 memset(cur, 0, sizeof(xmlDtd));
411 cur->type = XML_DTD_NODE;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000412
413 if (name != NULL)
414 cur->name = xmlStrdup(name);
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000415 if (ExternalID != NULL)
416 cur->ExternalID = xmlStrdup(ExternalID);
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000417 if (SystemID != NULL)
418 cur->SystemID = xmlStrdup(SystemID);
Daniel Veillardcf461992000-03-14 18:30:20 +0000419 if (doc != NULL) {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000420 doc->intSubset = cur;
Daniel Veillardcf461992000-03-14 18:30:20 +0000421 cur->parent = doc;
422 cur->doc = doc;
423 if (doc->children == NULL) {
424 doc->children = (xmlNodePtr) cur;
425 doc->last = (xmlNodePtr) cur;
426 } else {
427 xmlNodePtr prev;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000428
Daniel Veillardcf461992000-03-14 18:30:20 +0000429 prev = doc->last;
430 prev->next = (xmlNodePtr) cur;
431 cur->prev = prev;
432 doc->last = (xmlNodePtr) cur;
433 }
434 }
Daniel Veillard260a68f1998-08-13 03:39:55 +0000435 return(cur);
436}
437
Daniel Veillard97b58771998-10-20 06:14:16 +0000438/**
439 * xmlFreeDtd:
440 * @cur: the DTD structure to free up
441 *
442 * Free a DTD structure.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000443 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000444void
445xmlFreeDtd(xmlDtdPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000446 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000447#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +0000448 fprintf(stderr, "xmlFreeDtd : DTD == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000449#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +0000450 return;
451 }
Daniel Veillardcf461992000-03-14 18:30:20 +0000452 if (cur->children != NULL) {
453 xmlNodePtr next, c = cur->children;
454
455 /*
456 * Cleanup all the DTD comments they are not in the Dtd
457 * indexes.
458 */
459 while (c != NULL) {
460 next = c->next;
461 if (c->type == XML_COMMENT_NODE) {
462 xmlUnlinkNode(c);
463 xmlFreeNode(c);
464 }
465 c = next;
466 }
467 }
Daniel Veillard6454aec1999-09-02 22:04:43 +0000468 if (cur->name != NULL) xmlFree((char *) cur->name);
469 if (cur->SystemID != NULL) xmlFree((char *) cur->SystemID);
470 if (cur->ExternalID != NULL) xmlFree((char *) cur->ExternalID);
Daniel Veillardcf461992000-03-14 18:30:20 +0000471 /* TODO !!! */
Daniel Veillard1e346af1999-02-22 10:33:01 +0000472 if (cur->notations != NULL)
473 xmlFreeNotationTable((xmlNotationTablePtr) cur->notations);
Daniel Veillardcf461992000-03-14 18:30:20 +0000474
Daniel Veillard260a68f1998-08-13 03:39:55 +0000475 if (cur->elements != NULL)
Daniel Veillard3b9def11999-01-31 22:15:06 +0000476 xmlFreeElementTable((xmlElementTablePtr) cur->elements);
Daniel Veillard1e346af1999-02-22 10:33:01 +0000477 if (cur->attributes != NULL)
478 xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000479 if (cur->entities != NULL)
480 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities);
Daniel Veillardcf461992000-03-14 18:30:20 +0000481
Daniel Veillard260a68f1998-08-13 03:39:55 +0000482 memset(cur, -1, sizeof(xmlDtd));
Daniel Veillard6454aec1999-09-02 22:04:43 +0000483 xmlFree(cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000484}
485
Daniel Veillard97b58771998-10-20 06:14:16 +0000486/**
487 * xmlNewDoc:
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000488 * @version: xmlChar string giving the version of XML "1.0"
Daniel Veillard97b58771998-10-20 06:14:16 +0000489 *
Daniel Veillard1e346af1999-02-22 10:33:01 +0000490 * Returns a new document
Daniel Veillard260a68f1998-08-13 03:39:55 +0000491 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000492xmlDocPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000493xmlNewDoc(const xmlChar *version) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000494 xmlDocPtr cur;
495
496 if (version == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000497#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +0000498 fprintf(stderr, "xmlNewDoc : version == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000499#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +0000500 return(NULL);
501 }
502
503 /*
504 * Allocate a new document and fill the fields.
505 */
Daniel Veillard6454aec1999-09-02 22:04:43 +0000506 cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
Daniel Veillard260a68f1998-08-13 03:39:55 +0000507 if (cur == NULL) {
508 fprintf(stderr, "xmlNewDoc : malloc failed\n");
509 return(NULL);
510 }
Daniel Veillardcf461992000-03-14 18:30:20 +0000511 memset(cur, 0, sizeof(xmlDoc));
Daniel Veillard33942841998-10-18 19:12:41 +0000512 cur->type = XML_DOCUMENT_NODE;
Daniel Veillardcf461992000-03-14 18:30:20 +0000513
Daniel Veillard260a68f1998-08-13 03:39:55 +0000514 cur->version = xmlStrdup(version);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000515 cur->standalone = -1;
Daniel Veillard11a48ec1999-11-23 10:40:46 +0000516 cur->compression = -1; /* not initialized */
Daniel Veillardcf461992000-03-14 18:30:20 +0000517 cur->doc = cur;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000518 return(cur);
519}
520
Daniel Veillard97b58771998-10-20 06:14:16 +0000521/**
522 * xmlFreeDoc:
523 * @cur: pointer to the document
524 * @:
525 *
526 * Free up all the structures used by a document, tree included.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000527 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000528void
529xmlFreeDoc(xmlDocPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000530 if (cur == NULL) {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000531#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +0000532 fprintf(stderr, "xmlFreeDoc : document == NULL\n");
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000533#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +0000534 return;
535 }
Daniel Veillard6454aec1999-09-02 22:04:43 +0000536 if (cur->version != NULL) xmlFree((char *) cur->version);
537 if (cur->name != NULL) xmlFree((char *) cur->name);
538 if (cur->encoding != NULL) xmlFree((char *) cur->encoding);
Daniel Veillardcf461992000-03-14 18:30:20 +0000539 if (cur->children != NULL) xmlFreeNodeList(cur->children);
Daniel Veillard39a1f9a1999-01-17 19:11:59 +0000540 if (cur->intSubset != NULL) xmlFreeDtd(cur->intSubset);
541 if (cur->extSubset != NULL) xmlFreeDtd(cur->extSubset);
Daniel Veillardbe36afe1998-11-27 06:39:50 +0000542 if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
Daniel Veillardb96e6431999-08-29 21:02:19 +0000543 if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids);
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000544 if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
Daniel Veillardcf461992000-03-14 18:30:20 +0000545 if (cur->URL != NULL) xmlFree((char *) cur->URL);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000546 memset(cur, -1, sizeof(xmlDoc));
Daniel Veillard6454aec1999-09-02 22:04:43 +0000547 xmlFree(cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000548}
549
Daniel Veillard97b58771998-10-20 06:14:16 +0000550/**
Daniel Veillard16253641998-10-28 22:58:05 +0000551 * xmlStringLenGetNodeList:
552 * @doc: the document
553 * @value: the value of the text
Daniel Veillard1e346af1999-02-22 10:33:01 +0000554 * @len: the length of the string value
Daniel Veillard16253641998-10-28 22:58:05 +0000555 *
556 * Parse the value string and build the node list associated. Should
557 * produce a flat tree with only TEXTs and ENTITY_REFs.
Daniel Veillard1e346af1999-02-22 10:33:01 +0000558 * Returns a pointer to the first child
Daniel Veillard16253641998-10-28 22:58:05 +0000559 */
560xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000561xmlStringLenGetNodeList(xmlDocPtr doc, const xmlChar *value, int len) {
Daniel Veillard16253641998-10-28 22:58:05 +0000562 xmlNodePtr ret = NULL, last = NULL;
563 xmlNodePtr node;
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000564 xmlChar *val;
565 const xmlChar *cur = value;
566 const xmlChar *q;
Daniel Veillard25940b71998-10-29 05:51:30 +0000567 xmlEntityPtr ent;
Daniel Veillard16253641998-10-28 22:58:05 +0000568
569 if (value == NULL) return(NULL);
570
571 q = cur;
572 while ((*cur != 0) && (cur - value < len)) {
573 if (*cur == '&') {
Daniel Veillard25940b71998-10-29 05:51:30 +0000574 /*
575 * Save the current text.
576 */
Daniel Veillard16253641998-10-28 22:58:05 +0000577 if (cur != q) {
Daniel Veillard25940b71998-10-29 05:51:30 +0000578 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
579 xmlNodeAddContentLen(last, q, cur - q);
580 } else {
581 node = xmlNewDocTextLen(doc, q, cur - q);
582 if (node == NULL) return(ret);
583 if (last == NULL)
584 last = ret = node;
585 else {
586 last->next = node;
587 node->prev = last;
588 last = node;
589 }
Daniel Veillard16253641998-10-28 22:58:05 +0000590 }
591 }
Daniel Veillard25940b71998-10-29 05:51:30 +0000592 /*
593 * Read the entity string
594 */
Daniel Veillard16253641998-10-28 22:58:05 +0000595 cur++;
596 q = cur;
597 while ((*cur != 0) && (cur - value < len) && (*cur != ';')) cur++;
598 if ((*cur == 0) || (cur - value >= len)) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000599#ifdef DEBUG_TREE
Daniel Veillard16253641998-10-28 22:58:05 +0000600 fprintf(stderr,
Daniel Veillard011b63c1999-06-02 17:44:04 +0000601 "xmlStringLenGetNodeList: unterminated entity %30s\n", q);
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000602#endif
Daniel Veillard16253641998-10-28 22:58:05 +0000603 return(ret);
604 }
605 if (cur != q) {
Daniel Veillard25940b71998-10-29 05:51:30 +0000606 /*
607 * Predefined entities don't generate nodes
608 */
Daniel Veillard16253641998-10-28 22:58:05 +0000609 val = xmlStrndup(q, cur - q);
Daniel Veillard25940b71998-10-29 05:51:30 +0000610 ent = xmlGetDocEntity(doc, val);
611 if ((ent != NULL) &&
Daniel Veillardcf461992000-03-14 18:30:20 +0000612 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
Daniel Veillard25940b71998-10-29 05:51:30 +0000613 if (last == NULL) {
614 node = xmlNewDocText(doc, ent->content);
615 last = ret = node;
616 } else
617 xmlNodeAddContent(last, ent->content);
618
619 } else {
620 /*
621 * Create a new REFERENCE_REF node
622 */
623 node = xmlNewReference(doc, val);
Daniel Veillard242590e1998-11-13 18:04:35 +0000624 if (node == NULL) {
Daniel Veillard6454aec1999-09-02 22:04:43 +0000625 if (val != NULL) xmlFree(val);
Daniel Veillard242590e1998-11-13 18:04:35 +0000626 return(ret);
627 }
Daniel Veillard25940b71998-10-29 05:51:30 +0000628 if (last == NULL)
629 last = ret = node;
630 else {
631 last->next = node;
632 node->prev = last;
633 last = node;
634 }
Daniel Veillard16253641998-10-28 22:58:05 +0000635 }
Daniel Veillard6454aec1999-09-02 22:04:43 +0000636 xmlFree(val);
Daniel Veillard16253641998-10-28 22:58:05 +0000637 }
638 cur++;
639 q = cur;
640 } else
641 cur++;
642 }
643 if (cur != q) {
Daniel Veillard25940b71998-10-29 05:51:30 +0000644 /*
645 * Handle the last piece of text.
646 */
647 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
648 xmlNodeAddContentLen(last, q, cur - q);
649 } else {
650 node = xmlNewDocTextLen(doc, q, cur - q);
651 if (node == NULL) return(ret);
652 if (last == NULL)
653 last = ret = node;
654 else {
655 last->next = node;
656 node->prev = last;
657 last = node;
658 }
Daniel Veillard16253641998-10-28 22:58:05 +0000659 }
660 }
661 return(ret);
662}
663
664/**
Daniel Veillardccb09631998-10-27 06:21:04 +0000665 * xmlStringGetNodeList:
666 * @doc: the document
667 * @value: the value of the attribute
668 *
669 * Parse the value string and build the node list associated. Should
670 * produce a flat tree with only TEXTs and ENTITY_REFs.
Daniel Veillard1e346af1999-02-22 10:33:01 +0000671 * Returns a pointer to the first child
Daniel Veillardccb09631998-10-27 06:21:04 +0000672 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000673xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000674xmlStringGetNodeList(xmlDocPtr doc, const xmlChar *value) {
Daniel Veillardccb09631998-10-27 06:21:04 +0000675 xmlNodePtr ret = NULL, last = NULL;
676 xmlNodePtr node;
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000677 xmlChar *val;
678 const xmlChar *cur = value;
679 const xmlChar *q;
Daniel Veillard25940b71998-10-29 05:51:30 +0000680 xmlEntityPtr ent;
Daniel Veillardccb09631998-10-27 06:21:04 +0000681
682 if (value == NULL) return(NULL);
683
684 q = cur;
685 while (*cur != 0) {
686 if (*cur == '&') {
Daniel Veillard25940b71998-10-29 05:51:30 +0000687 /*
688 * Save the current text.
689 */
Daniel Veillardccb09631998-10-27 06:21:04 +0000690 if (cur != q) {
Daniel Veillard25940b71998-10-29 05:51:30 +0000691 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
692 xmlNodeAddContentLen(last, q, cur - q);
693 } else {
694 node = xmlNewDocTextLen(doc, q, cur - q);
695 if (node == NULL) return(ret);
696 if (last == NULL)
697 last = ret = node;
698 else {
699 last->next = node;
700 node->prev = last;
701 last = node;
702 }
Daniel Veillardccb09631998-10-27 06:21:04 +0000703 }
704 }
Daniel Veillard25940b71998-10-29 05:51:30 +0000705 /*
706 * Read the entity string
707 */
Daniel Veillardccb09631998-10-27 06:21:04 +0000708 cur++;
709 q = cur;
710 while ((*cur != 0) && (*cur != ';')) cur++;
711 if (*cur == 0) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000712#ifdef DEBUG_TREE
Daniel Veillardccb09631998-10-27 06:21:04 +0000713 fprintf(stderr,
714 "xmlStringGetNodeList: unterminated entity %30s\n", q);
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000715#endif
Daniel Veillardccb09631998-10-27 06:21:04 +0000716 return(ret);
717 }
718 if (cur != q) {
Daniel Veillard25940b71998-10-29 05:51:30 +0000719 /*
720 * Predefined entities don't generate nodes
721 */
Daniel Veillardccb09631998-10-27 06:21:04 +0000722 val = xmlStrndup(q, cur - q);
Daniel Veillard25940b71998-10-29 05:51:30 +0000723 ent = xmlGetDocEntity(doc, val);
724 if ((ent != NULL) &&
Daniel Veillardcf461992000-03-14 18:30:20 +0000725 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
Daniel Veillard25940b71998-10-29 05:51:30 +0000726 if (last == NULL) {
727 node = xmlNewDocText(doc, ent->content);
728 last = ret = node;
729 } else
730 xmlNodeAddContent(last, ent->content);
731
732 } else {
733 /*
734 * Create a new REFERENCE_REF node
735 */
Daniel Veillard25940b71998-10-29 05:51:30 +0000736 node = xmlNewReference(doc, val);
Daniel Veillard242590e1998-11-13 18:04:35 +0000737 if (node == NULL) {
Daniel Veillard6454aec1999-09-02 22:04:43 +0000738 if (val != NULL) xmlFree(val);
Daniel Veillard242590e1998-11-13 18:04:35 +0000739 return(ret);
740 }
Daniel Veillard25940b71998-10-29 05:51:30 +0000741 if (last == NULL)
742 last = ret = node;
743 else {
744 last->next = node;
745 node->prev = last;
746 last = node;
747 }
Daniel Veillardccb09631998-10-27 06:21:04 +0000748 }
Daniel Veillard6454aec1999-09-02 22:04:43 +0000749 xmlFree(val);
Daniel Veillardccb09631998-10-27 06:21:04 +0000750 }
751 cur++;
752 q = cur;
753 } else
754 cur++;
755 }
756 if (cur != q) {
Daniel Veillard25940b71998-10-29 05:51:30 +0000757 /*
758 * Handle the last piece of text.
759 */
760 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
761 xmlNodeAddContentLen(last, q, cur - q);
762 } else {
763 node = xmlNewDocTextLen(doc, q, cur - q);
764 if (node == NULL) return(ret);
765 if (last == NULL)
766 last = ret = node;
767 else {
768 last->next = node;
769 node->prev = last;
770 last = node;
771 }
Daniel Veillardccb09631998-10-27 06:21:04 +0000772 }
773 }
774 return(ret);
775}
776
777/**
778 * xmlNodeListGetString:
779 * @doc: the document
780 * @list: a Node list
781 * @inLine: should we replace entity contents or show their external form
782 *
783 * Returns the string equivalent to the text contained in the Node list
784 * made of TEXTs and ENTITY_REFs
Daniel Veillard1e346af1999-02-22 10:33:01 +0000785 * Returns a pointer to the string copy, the calller must free it.
Daniel Veillardccb09631998-10-27 06:21:04 +0000786 */
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000787xmlChar *
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000788xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine) {
Daniel Veillardccb09631998-10-27 06:21:04 +0000789 xmlNodePtr node = list;
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000790 xmlChar *ret = NULL;
Daniel Veillardccb09631998-10-27 06:21:04 +0000791 xmlEntityPtr ent;
792
793 if (list == NULL) return(NULL);
794
795 while (node != NULL) {
796 if (node->type == XML_TEXT_NODE) {
Daniel Veillard71b656e2000-01-05 14:46:17 +0000797 if (inLine) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000798#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardccb09631998-10-27 06:21:04 +0000799 ret = xmlStrcat(ret, node->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000800#else
801 ret = xmlStrcat(ret, xmlBufferContent(node->content));
802#endif
803 } else {
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000804 xmlChar *buffer;
Daniel Veillard14fff061999-06-22 21:49:07 +0000805
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000806#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard14fff061999-06-22 21:49:07 +0000807 buffer = xmlEncodeEntitiesReentrant(doc, node->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000808#else
809 buffer = xmlEncodeEntitiesReentrant(doc,
810 xmlBufferContent(node->content));
811#endif
Daniel Veillard14fff061999-06-22 21:49:07 +0000812 if (buffer != NULL) {
813 ret = xmlStrcat(ret, buffer);
Daniel Veillard6454aec1999-09-02 22:04:43 +0000814 xmlFree(buffer);
Daniel Veillard14fff061999-06-22 21:49:07 +0000815 }
816 }
Daniel Veillardccb09631998-10-27 06:21:04 +0000817 } else if (node->type == XML_ENTITY_REF_NODE) {
818 if (inLine) {
819 ent = xmlGetDocEntity(doc, node->name);
820 if (ent != NULL)
821 ret = xmlStrcat(ret, ent->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000822 else {
823#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardccb09631998-10-27 06:21:04 +0000824 ret = xmlStrcat(ret, node->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000825#else
826 ret = xmlStrcat(ret, xmlBufferContent(node->content));
827#endif
828 }
Daniel Veillardccb09631998-10-27 06:21:04 +0000829 } else {
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000830 xmlChar buf[2];
Daniel Veillardccb09631998-10-27 06:21:04 +0000831 buf[0] = '&'; buf[1] = 0;
832 ret = xmlStrncat(ret, buf, 1);
833 ret = xmlStrcat(ret, node->name);
834 buf[0] = ';'; buf[1] = 0;
835 ret = xmlStrncat(ret, buf, 1);
836 }
837 }
838#if 0
839 else {
840 fprintf(stderr, "xmlGetNodeListString : invalide node type %d\n",
841 node->type);
842 }
843#endif
844 node = node->next;
845 }
846 return(ret);
847}
848
849/**
Daniel Veillard97b58771998-10-20 06:14:16 +0000850 * xmlNewProp:
851 * @node: the holding node
852 * @name: the name of the attribute
853 * @value: the value of the attribute
854 *
855 * Create a new property carried by a node.
Daniel Veillard1e346af1999-02-22 10:33:01 +0000856 * Returns a pointer to the attribute
Daniel Veillard260a68f1998-08-13 03:39:55 +0000857 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000858xmlAttrPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000859xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
Daniel Veillard260a68f1998-08-13 03:39:55 +0000860 xmlAttrPtr cur;
861
862 if (name == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000863#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +0000864 fprintf(stderr, "xmlNewProp : name == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000865#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +0000866 return(NULL);
867 }
868
869 /*
870 * Allocate a new property and fill the fields.
871 */
Daniel Veillard6454aec1999-09-02 22:04:43 +0000872 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
Daniel Veillard260a68f1998-08-13 03:39:55 +0000873 if (cur == NULL) {
874 fprintf(stderr, "xmlNewProp : malloc failed\n");
875 return(NULL);
876 }
Daniel Veillardcf461992000-03-14 18:30:20 +0000877 memset(cur, 0, sizeof(xmlAttr));
Daniel Veillard33942841998-10-18 19:12:41 +0000878 cur->type = XML_ATTRIBUTE_NODE;
Daniel Veillardcf461992000-03-14 18:30:20 +0000879
880 cur->parent = node;
Daniel Veillardb96e6431999-08-29 21:02:19 +0000881 cur->name = xmlStrdup(name);
Daniel Veillard51e3b151999-11-12 17:02:31 +0000882 if (value != NULL) {
883 xmlChar *buffer;
Daniel Veillardcf461992000-03-14 18:30:20 +0000884 xmlNodePtr tmp;
885
Daniel Veillard51e3b151999-11-12 17:02:31 +0000886 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
Daniel Veillardcf461992000-03-14 18:30:20 +0000887 cur->children = xmlStringGetNodeList(node->doc, buffer);
888 tmp = cur->children;
889 while (tmp != NULL) {
890 tmp->parent = (xmlNodePtr) cur;
891 if (tmp->next == NULL)
892 cur->last = tmp;
893 tmp = tmp->next;
894 }
Daniel Veillard51e3b151999-11-12 17:02:31 +0000895 xmlFree(buffer);
896 }
Daniel Veillardb96e6431999-08-29 21:02:19 +0000897
898 /*
899 * Add it at the end to preserve parsing order ...
900 */
Daniel Veillardb96e6431999-08-29 21:02:19 +0000901 if (node != NULL) {
902 if (node->properties == NULL) {
903 node->properties = cur;
904 } else {
905 xmlAttrPtr prev = node->properties;
906
907 while (prev->next != NULL) prev = prev->next;
908 prev->next = cur;
Daniel Veillardcf461992000-03-14 18:30:20 +0000909 cur->prev = prev;
Daniel Veillardb96e6431999-08-29 21:02:19 +0000910 }
911 }
912 return(cur);
913}
914
915/**
916 * xmlNewNsProp:
917 * @node: the holding node
918 * @ns: the namespace
919 * @name: the name of the attribute
920 * @value: the value of the attribute
921 *
922 * Create a new property tagged with a namespace and carried by a node.
923 * Returns a pointer to the attribute
924 */
925xmlAttrPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000926xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
927 const xmlChar *value) {
Daniel Veillardb96e6431999-08-29 21:02:19 +0000928 xmlAttrPtr cur;
929
930 if (name == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000931#ifdef DEBUG_TREE
Daniel Veillardb96e6431999-08-29 21:02:19 +0000932 fprintf(stderr, "xmlNewProp : name == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +0000933#endif
Daniel Veillardb96e6431999-08-29 21:02:19 +0000934 return(NULL);
935 }
936
937 /*
938 * Allocate a new property and fill the fields.
939 */
Daniel Veillard6454aec1999-09-02 22:04:43 +0000940 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
Daniel Veillardb96e6431999-08-29 21:02:19 +0000941 if (cur == NULL) {
942 fprintf(stderr, "xmlNewProp : malloc failed\n");
943 return(NULL);
944 }
Daniel Veillardcf461992000-03-14 18:30:20 +0000945 memset(cur, 0, sizeof(xmlAttr));
Daniel Veillardb96e6431999-08-29 21:02:19 +0000946 cur->type = XML_ATTRIBUTE_NODE;
Daniel Veillardcf461992000-03-14 18:30:20 +0000947
948 cur->parent = node;
949 if (node != NULL)
950 cur->doc = node->doc;
Daniel Veillardb96e6431999-08-29 21:02:19 +0000951 cur->ns = ns;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000952 cur->name = xmlStrdup(name);
Daniel Veillardcf461992000-03-14 18:30:20 +0000953 if (value != NULL) {
954 xmlChar *buffer;
955 xmlNodePtr tmp;
956
957 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
958 cur->children = xmlStringGetNodeList(node->doc, buffer);
959 tmp = cur->children;
960 while (tmp != NULL) {
961 tmp->parent = (xmlNodePtr) cur;
962 if (tmp->next == NULL)
963 cur->last = tmp;
964 tmp = tmp->next;
965 }
966 xmlFree(buffer);
967 }
Daniel Veillard260a68f1998-08-13 03:39:55 +0000968
969 /*
970 * Add it at the end to preserve parsing order ...
971 */
Daniel Veillard260a68f1998-08-13 03:39:55 +0000972 if (node != NULL) {
973 if (node->properties == NULL) {
974 node->properties = cur;
975 } else {
976 xmlAttrPtr prev = node->properties;
977
978 while (prev->next != NULL) prev = prev->next;
979 prev->next = cur;
Daniel Veillardcf461992000-03-14 18:30:20 +0000980 cur->prev = prev;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000981 }
982 }
983 return(cur);
984}
985
Daniel Veillard97b58771998-10-20 06:14:16 +0000986/**
Daniel Veillardccb09631998-10-27 06:21:04 +0000987 * xmlNewDocProp:
988 * @doc: the document
989 * @name: the name of the attribute
990 * @value: the value of the attribute
991 *
992 * Create a new property carried by a document.
Daniel Veillard1e346af1999-02-22 10:33:01 +0000993 * Returns a pointer to the attribute
Daniel Veillardccb09631998-10-27 06:21:04 +0000994 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +0000995xmlAttrPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000996xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) {
Daniel Veillardccb09631998-10-27 06:21:04 +0000997 xmlAttrPtr cur;
998
999 if (name == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001000#ifdef DEBUG_TREE
Daniel Veillardccb09631998-10-27 06:21:04 +00001001 fprintf(stderr, "xmlNewProp : name == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001002#endif
Daniel Veillardccb09631998-10-27 06:21:04 +00001003 return(NULL);
1004 }
1005
1006 /*
1007 * Allocate a new property and fill the fields.
1008 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00001009 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
Daniel Veillardccb09631998-10-27 06:21:04 +00001010 if (cur == NULL) {
1011 fprintf(stderr, "xmlNewProp : malloc failed\n");
1012 return(NULL);
1013 }
Daniel Veillardcf461992000-03-14 18:30:20 +00001014 memset(cur, 0, sizeof(xmlAttr));
Daniel Veillardccb09631998-10-27 06:21:04 +00001015 cur->type = XML_ATTRIBUTE_NODE;
Daniel Veillardccb09631998-10-27 06:21:04 +00001016
Daniel Veillardcf461992000-03-14 18:30:20 +00001017 cur->name = xmlStrdup(name);
1018 cur->doc = doc;
1019 if (value != NULL)
1020 cur->children = xmlStringGetNodeList(doc, value);
Daniel Veillardccb09631998-10-27 06:21:04 +00001021 return(cur);
1022}
1023
1024/**
Daniel Veillard97b58771998-10-20 06:14:16 +00001025 * xmlFreePropList:
1026 * @cur: the first property in the list
1027 *
Daniel Veillardcf461992000-03-14 18:30:20 +00001028 * Free a property and all its siblings, all the children are freed too.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001029 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001030void
1031xmlFreePropList(xmlAttrPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001032 xmlAttrPtr next;
1033 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001034#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00001035 fprintf(stderr, "xmlFreePropList : property == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001036#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00001037 return;
1038 }
1039 while (cur != NULL) {
1040 next = cur->next;
1041 xmlFreeProp(cur);
1042 cur = next;
1043 }
1044}
1045
Daniel Veillard97b58771998-10-20 06:14:16 +00001046/**
1047 * xmlFreeProp:
Daniel Veillard686d6b62000-01-03 11:08:02 +00001048 * @cur: an attribute
Daniel Veillard97b58771998-10-20 06:14:16 +00001049 *
Daniel Veillard686d6b62000-01-03 11:08:02 +00001050 * Free one attribute, all the content is freed too
Daniel Veillard260a68f1998-08-13 03:39:55 +00001051 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001052void
1053xmlFreeProp(xmlAttrPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001054 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001055#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00001056 fprintf(stderr, "xmlFreeProp : property == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001057#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00001058 return;
1059 }
Daniel Veillard71b656e2000-01-05 14:46:17 +00001060 /* Check for ID removal -> leading to invalid references ! */
Daniel Veillardcf461992000-03-14 18:30:20 +00001061 if ((cur->parent != NULL) &&
1062 (xmlIsID(cur->parent->doc, cur->parent, cur)))
1063 xmlRemoveID(cur->parent->doc, cur);
Daniel Veillard6454aec1999-09-02 22:04:43 +00001064 if (cur->name != NULL) xmlFree((char *) cur->name);
Daniel Veillardcf461992000-03-14 18:30:20 +00001065 if (cur->children != NULL) xmlFreeNodeList(cur->children);
Daniel Veillard260a68f1998-08-13 03:39:55 +00001066 memset(cur, -1, sizeof(xmlAttr));
Daniel Veillard6454aec1999-09-02 22:04:43 +00001067 xmlFree(cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +00001068}
1069
Daniel Veillard97b58771998-10-20 06:14:16 +00001070/**
Daniel Veillard686d6b62000-01-03 11:08:02 +00001071 * xmlRemoveProp:
1072 * @cur: an attribute
1073 *
1074 * Unlink and free one attribute, all the content is freed too
1075 * Note this doesn't work for namespace definition attributes
1076 *
1077 * Returns 0 if success and -1 in case of error.
1078 */
1079int
1080xmlRemoveProp(xmlAttrPtr cur) {
1081 xmlAttrPtr tmp;
1082 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001083#ifdef DEBUG_TREE
Daniel Veillard686d6b62000-01-03 11:08:02 +00001084 fprintf(stderr, "xmlRemoveProp : cur == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001085#endif
Daniel Veillard686d6b62000-01-03 11:08:02 +00001086 return(-1);
1087 }
Daniel Veillardcf461992000-03-14 18:30:20 +00001088 if (cur->parent == NULL) {
1089#ifdef DEBUG_TREE
1090 fprintf(stderr, "xmlRemoveProp : cur->parent == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001091#endif
Daniel Veillard686d6b62000-01-03 11:08:02 +00001092 return(-1);
1093 }
Daniel Veillardcf461992000-03-14 18:30:20 +00001094 tmp = cur->parent->properties;
Daniel Veillard686d6b62000-01-03 11:08:02 +00001095 if (tmp == cur) {
Daniel Veillardcf461992000-03-14 18:30:20 +00001096 cur->parent->properties = cur->next;
Daniel Veillard686d6b62000-01-03 11:08:02 +00001097 xmlFreeProp(cur);
1098 return(0);
1099 }
1100 while (tmp != NULL) {
1101 if (tmp->next == cur) {
1102 tmp->next = cur->next;
Daniel Veillardcf461992000-03-14 18:30:20 +00001103 if (tmp->next != NULL)
1104 tmp->next->prev = tmp;
Daniel Veillard686d6b62000-01-03 11:08:02 +00001105 xmlFreeProp(cur);
1106 return(0);
1107 }
1108 tmp = tmp->next;
1109 }
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001110#ifdef DEBUG_TREE
Daniel Veillard686d6b62000-01-03 11:08:02 +00001111 fprintf(stderr, "xmlRemoveProp : attribute not owned by its node\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001112#endif
Daniel Veillard686d6b62000-01-03 11:08:02 +00001113 return(-1);
1114}
1115
1116/**
Daniel Veillardb96e6431999-08-29 21:02:19 +00001117 * xmlNewPI:
1118 * @name: the processing instruction name
1119 * @content: the PI content
1120 *
1121 * Creation of a processing instruction element.
1122 * Returns a pointer to the new node object.
1123 */
1124xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001125xmlNewPI(const xmlChar *name, const xmlChar *content) {
Daniel Veillardb96e6431999-08-29 21:02:19 +00001126 xmlNodePtr cur;
1127
1128 if (name == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001129#ifdef DEBUG_TREE
Daniel Veillardb96e6431999-08-29 21:02:19 +00001130 fprintf(stderr, "xmlNewPI : name == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001131#endif
Daniel Veillardb96e6431999-08-29 21:02:19 +00001132 return(NULL);
1133 }
1134
1135 /*
1136 * Allocate a new node and fill the fields.
1137 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00001138 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillardb96e6431999-08-29 21:02:19 +00001139 if (cur == NULL) {
1140 fprintf(stderr, "xmlNewPI : malloc failed\n");
1141 return(NULL);
1142 }
Daniel Veillardcf461992000-03-14 18:30:20 +00001143 memset(cur, 0, sizeof(xmlNode));
Daniel Veillardb96e6431999-08-29 21:02:19 +00001144 cur->type = XML_PI_NODE;
Daniel Veillardcf461992000-03-14 18:30:20 +00001145
Daniel Veillardb96e6431999-08-29 21:02:19 +00001146 cur->name = xmlStrdup(name);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001147 if (content != NULL) {
1148#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardb96e6431999-08-29 21:02:19 +00001149 cur->content = xmlStrdup(content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001150#else
1151 cur->content = xmlBufferCreateSize(0);
1152 xmlBufferSetAllocationScheme(cur->content,
1153 xmlGetBufferAllocationScheme());
1154 xmlBufferAdd(cur->content, content, -1);
1155#endif
Daniel Veillardcf461992000-03-14 18:30:20 +00001156 }
Daniel Veillardb96e6431999-08-29 21:02:19 +00001157 return(cur);
1158}
1159
1160/**
Daniel Veillard97b58771998-10-20 06:14:16 +00001161 * xmlNewNode:
1162 * @ns: namespace if any
1163 * @name: the node name
Daniel Veillard97b58771998-10-20 06:14:16 +00001164 *
1165 * Creation of a new node element. @ns and @content are optionnal (NULL).
Daniel Veillardccb09631998-10-27 06:21:04 +00001166 * If content is non NULL, a child list containing the TEXTs and
1167 * ENTITY_REFs node will be created.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001168 * Returns a pointer to the new node object.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001169 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001170xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001171xmlNewNode(xmlNsPtr ns, const xmlChar *name) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001172 xmlNodePtr cur;
1173
1174 if (name == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001175#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00001176 fprintf(stderr, "xmlNewNode : name == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001177#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00001178 return(NULL);
1179 }
1180
1181 /*
1182 * Allocate a new node and fill the fields.
1183 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00001184 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillard260a68f1998-08-13 03:39:55 +00001185 if (cur == NULL) {
1186 fprintf(stderr, "xmlNewNode : malloc failed\n");
1187 return(NULL);
1188 }
Daniel Veillardcf461992000-03-14 18:30:20 +00001189 memset(cur, 0, sizeof(xmlNode));
Daniel Veillard33942841998-10-18 19:12:41 +00001190 cur->type = XML_ELEMENT_NODE;
Daniel Veillardcf461992000-03-14 18:30:20 +00001191
Daniel Veillard260a68f1998-08-13 03:39:55 +00001192 cur->name = xmlStrdup(name);
1193 cur->ns = ns;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001194 return(cur);
1195}
1196
Daniel Veillard97b58771998-10-20 06:14:16 +00001197/**
1198 * xmlNewDocNode:
1199 * @doc: the document
1200 * @ns: namespace if any
1201 * @name: the node name
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001202 * @content: the XML text content if any
Daniel Veillard97b58771998-10-20 06:14:16 +00001203 *
1204 * Creation of a new node element within a document. @ns and @content
1205 * are optionnal (NULL).
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001206 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
1207 * references, but XML special chars need to be escaped first by using
1208 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
1209 * need entities support.
1210 *
Daniel Veillard1e346af1999-02-22 10:33:01 +00001211 * Returns a pointer to the new node object.
Daniel Veillard97b58771998-10-20 06:14:16 +00001212 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001213xmlNodePtr
1214xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns,
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001215 const xmlChar *name, const xmlChar *content) {
Daniel Veillard0bef1311998-10-14 02:36:47 +00001216 xmlNodePtr cur;
1217
Daniel Veillardccb09631998-10-27 06:21:04 +00001218 cur = xmlNewNode(ns, name);
1219 if (cur != NULL) {
1220 cur->doc = doc;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001221 if (content != NULL) {
Daniel Veillardcf461992000-03-14 18:30:20 +00001222 cur->children = xmlStringGetNodeList(doc, content);
Daniel Veillard1e346af1999-02-22 10:33:01 +00001223 UPDATE_LAST_CHILD(cur)
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001224 }
Daniel Veillardccb09631998-10-27 06:21:04 +00001225 }
Daniel Veillard0bef1311998-10-14 02:36:47 +00001226 return(cur);
1227}
1228
1229
Daniel Veillard97b58771998-10-20 06:14:16 +00001230/**
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001231 * xmlNewDocRawNode:
1232 * @doc: the document
1233 * @ns: namespace if any
1234 * @name: the node name
1235 * @content: the text content if any
1236 *
1237 * Creation of a new node element within a document. @ns and @content
1238 * are optionnal (NULL).
1239 *
1240 * Returns a pointer to the new node object.
1241 */
1242xmlNodePtr
1243xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns,
1244 const xmlChar *name, const xmlChar *content) {
1245 xmlNodePtr cur;
1246
1247 cur = xmlNewNode(ns, name);
1248 if (cur != NULL) {
1249 cur->doc = doc;
1250 if (content != NULL) {
Daniel Veillardcf461992000-03-14 18:30:20 +00001251 cur->children = xmlNewDocText(doc, content);
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001252 UPDATE_LAST_CHILD(cur)
1253 }
1254 }
1255 return(cur);
1256}
1257
Daniel Veillard2eac5032000-01-09 21:08:56 +00001258/**
1259 * xmlNewDocFragment:
1260 * @doc: the document owning the fragment
1261 *
1262 * Creation of a new Fragment node.
1263 * Returns a pointer to the new node object.
1264 */
1265xmlNodePtr
1266xmlNewDocFragment(xmlDocPtr doc) {
1267 xmlNodePtr cur;
1268
1269 /*
1270 * Allocate a new DocumentFragment node and fill the fields.
1271 */
1272 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1273 if (cur == NULL) {
1274 fprintf(stderr, "xmlNewDocFragment : malloc failed\n");
1275 return(NULL);
1276 }
Daniel Veillardcf461992000-03-14 18:30:20 +00001277 memset(cur, 0, sizeof(xmlNode));
Daniel Veillard2eac5032000-01-09 21:08:56 +00001278 cur->type = XML_DOCUMENT_FRAG_NODE;
Daniel Veillardcf461992000-03-14 18:30:20 +00001279
Daniel Veillard2eac5032000-01-09 21:08:56 +00001280 cur->doc = doc;
Daniel Veillard2eac5032000-01-09 21:08:56 +00001281 return(cur);
1282}
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001283
1284/**
Daniel Veillard97b58771998-10-20 06:14:16 +00001285 * xmlNewText:
1286 * @content: the text content
1287 *
1288 * Creation of a new text node.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001289 * Returns a pointer to the new node object.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001290 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001291xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001292xmlNewText(const xmlChar *content) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001293 xmlNodePtr cur;
1294
1295 /*
1296 * Allocate a new node and fill the fields.
1297 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00001298 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillard260a68f1998-08-13 03:39:55 +00001299 if (cur == NULL) {
1300 fprintf(stderr, "xmlNewText : malloc failed\n");
1301 return(NULL);
1302 }
Daniel Veillardcf461992000-03-14 18:30:20 +00001303 memset(cur, 0, sizeof(xmlNode));
1304 cur->type = XML_TEXT_NODE;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001305
Daniel Veillard260a68f1998-08-13 03:39:55 +00001306 cur->name = xmlStrdup(xmlStringText);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001307 if (content != NULL) {
1308#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard260a68f1998-08-13 03:39:55 +00001309 cur->content = xmlStrdup(content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001310#else
1311 cur->content = xmlBufferCreateSize(0);
1312 xmlBufferSetAllocationScheme(cur->content,
1313 xmlGetBufferAllocationScheme());
1314 xmlBufferAdd(cur->content, content, -1);
1315#endif
Daniel Veillardcf461992000-03-14 18:30:20 +00001316 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00001317 return(cur);
1318}
1319
Daniel Veillard97b58771998-10-20 06:14:16 +00001320/**
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001321 * xmlNewTextChild:
1322 * @parent: the parent node
1323 * @ns: a namespace if any
1324 * @name: the name of the child
1325 * @content: the text content of the child if any.
1326 *
Daniel Veillardcf461992000-03-14 18:30:20 +00001327 * Creation of a new child element, added at the end of @parent children list.
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001328 * @ns and @content parameters are optionnal (NULL). If content is non NULL,
1329 * a child TEXT node will be created containing the string content.
1330 *
1331 * Returns a pointer to the new node object.
1332 */
1333xmlNodePtr
1334xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns,
1335 const xmlChar *name, const xmlChar *content) {
1336 xmlNodePtr cur, prev;
1337
1338 if (parent == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001339#ifdef DEBUG_TREE
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001340 fprintf(stderr, "xmlNewTextChild : parent == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001341#endif
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001342 return(NULL);
1343 }
1344
1345 if (name == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001346#ifdef DEBUG_TREE
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001347 fprintf(stderr, "xmlNewTextChild : name == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001348#endif
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001349 return(NULL);
1350 }
1351
1352 /*
1353 * Allocate a new node
1354 */
1355 if (ns == NULL)
1356 cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content);
1357 else
1358 cur = xmlNewDocRawNode(parent->doc, ns, name, content);
1359 if (cur == NULL) return(NULL);
1360
1361 /*
Daniel Veillardcf461992000-03-14 18:30:20 +00001362 * add the new element at the end of the children list.
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001363 */
1364 cur->type = XML_ELEMENT_NODE;
1365 cur->parent = parent;
1366 cur->doc = parent->doc;
Daniel Veillardcf461992000-03-14 18:30:20 +00001367 if (parent->children == NULL) {
1368 parent->children = cur;
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001369 parent->last = cur;
1370 } else {
1371 prev = parent->last;
1372 prev->next = cur;
1373 cur->prev = prev;
1374 parent->last = cur;
1375 }
1376
1377 return(cur);
1378}
1379
1380/**
Daniel Veillardcf461992000-03-14 18:30:20 +00001381 * xmlNewCharRef:
1382 * @doc: the document
1383 * @name: the char ref string, starting with # or "&# ... ;"
1384 *
1385 * Creation of a new character reference node.
1386 * Returns a pointer to the new node object.
1387 */
1388xmlNodePtr
1389xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) {
1390 xmlNodePtr cur;
1391
1392 /*
1393 * Allocate a new node and fill the fields.
1394 */
1395 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1396 if (cur == NULL) {
1397 fprintf(stderr, "xmlNewText : malloc failed\n");
1398 return(NULL);
1399 }
1400 memset(cur, 0, sizeof(xmlNode));
1401 cur->type = XML_ENTITY_REF_NODE;
1402
1403 cur->doc = doc;
1404 if (name[0] == '&') {
1405 int len;
1406 name++;
1407 len = xmlStrlen(name);
1408 if (name[len - 1] == ';')
1409 cur->name = xmlStrndup(name, len - 1);
1410 else
1411 cur->name = xmlStrndup(name, len);
1412 } else
1413 cur->name = xmlStrdup(name);
1414 return(cur);
1415}
1416
1417/**
Daniel Veillardccb09631998-10-27 06:21:04 +00001418 * xmlNewReference:
1419 * @doc: the document
1420 * @name: the reference name, or the reference string with & and ;
1421 *
1422 * Creation of a new reference node.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001423 * Returns a pointer to the new node object.
Daniel Veillardccb09631998-10-27 06:21:04 +00001424 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001425xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001426xmlNewReference(xmlDocPtr doc, const xmlChar *name) {
Daniel Veillardccb09631998-10-27 06:21:04 +00001427 xmlNodePtr cur;
1428 xmlEntityPtr ent;
1429
1430 /*
1431 * Allocate a new node and fill the fields.
1432 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00001433 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillardccb09631998-10-27 06:21:04 +00001434 if (cur == NULL) {
1435 fprintf(stderr, "xmlNewText : malloc failed\n");
1436 return(NULL);
1437 }
Daniel Veillardcf461992000-03-14 18:30:20 +00001438 memset(cur, 0, sizeof(xmlNode));
Daniel Veillardccb09631998-10-27 06:21:04 +00001439 cur->type = XML_ENTITY_REF_NODE;
Daniel Veillardcf461992000-03-14 18:30:20 +00001440
Daniel Veillard10c6a8f1998-10-28 01:00:12 +00001441 cur->doc = doc;
Daniel Veillardccb09631998-10-27 06:21:04 +00001442 if (name[0] == '&') {
1443 int len;
1444 name++;
1445 len = xmlStrlen(name);
1446 if (name[len - 1] == ';')
1447 cur->name = xmlStrndup(name, len - 1);
1448 else
1449 cur->name = xmlStrndup(name, len);
1450 } else
1451 cur->name = xmlStrdup(name);
Daniel Veillardccb09631998-10-27 06:21:04 +00001452
1453 ent = xmlGetDocEntity(doc, cur->name);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001454 if (ent != NULL) {
1455#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardccb09631998-10-27 06:21:04 +00001456 cur->content = ent->content;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001457#else
1458 /*
1459 * CJN 11.18.99 this might be a problem, since the xmlBuffer gets
1460 * a copy of this pointer. Let's hope we don't manipulate it
1461 * later
1462 */
1463 cur->content = xmlBufferCreateSize(0);
1464 xmlBufferSetAllocationScheme(cur->content,
1465 xmlGetBufferAllocationScheme());
1466 if (ent->content != NULL)
1467 xmlBufferAdd(cur->content, ent->content, -1);
1468#endif
Daniel Veillardcf461992000-03-14 18:30:20 +00001469 cur->children = (xmlNodePtr) ent;
1470 }
Daniel Veillardccb09631998-10-27 06:21:04 +00001471 return(cur);
1472}
1473
1474/**
Daniel Veillard97b58771998-10-20 06:14:16 +00001475 * xmlNewDocText:
1476 * @doc: the document
1477 * @content: the text content
1478 *
1479 * Creation of a new text node within a document.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001480 * Returns a pointer to the new node object.
Daniel Veillard97b58771998-10-20 06:14:16 +00001481 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001482xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001483xmlNewDocText(xmlDocPtr doc, const xmlChar *content) {
Daniel Veillard0bef1311998-10-14 02:36:47 +00001484 xmlNodePtr cur;
1485
1486 cur = xmlNewText(content);
1487 if (cur != NULL) cur->doc = doc;
1488 return(cur);
1489}
1490
Daniel Veillard97b58771998-10-20 06:14:16 +00001491/**
Daniel Veillardccb09631998-10-27 06:21:04 +00001492 * xmlNewTextLen:
Daniel Veillard97b58771998-10-20 06:14:16 +00001493 * @content: the text content
1494 * @len: the text len.
1495 *
1496 * Creation of a new text node with an extra parameter for the content's lenght
Daniel Veillard1e346af1999-02-22 10:33:01 +00001497 * Returns a pointer to the new node object.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001498 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001499xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001500xmlNewTextLen(const xmlChar *content, int len) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001501 xmlNodePtr cur;
1502
1503 /*
1504 * Allocate a new node and fill the fields.
1505 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00001506 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillard260a68f1998-08-13 03:39:55 +00001507 if (cur == NULL) {
1508 fprintf(stderr, "xmlNewText : malloc failed\n");
1509 return(NULL);
1510 }
Daniel Veillardcf461992000-03-14 18:30:20 +00001511 memset(cur, 0, sizeof(xmlNode));
1512 cur->type = XML_TEXT_NODE;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001513
Daniel Veillard260a68f1998-08-13 03:39:55 +00001514 cur->name = xmlStrdup(xmlStringText);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001515 if (content != NULL) {
1516#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard260a68f1998-08-13 03:39:55 +00001517 cur->content = xmlStrndup(content, len);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001518#else
1519 cur->content = xmlBufferCreateSize(len);
1520 xmlBufferSetAllocationScheme(cur->content,
1521 xmlGetBufferAllocationScheme());
1522 xmlBufferAdd(cur->content, content, len);
1523#endif
Daniel Veillardcf461992000-03-14 18:30:20 +00001524 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00001525 return(cur);
1526}
1527
Daniel Veillard97b58771998-10-20 06:14:16 +00001528/**
1529 * xmlNewDocTextLen:
1530 * @doc: the document
1531 * @content: the text content
1532 * @len: the text len.
1533 *
1534 * Creation of a new text node with an extra content lenght parameter. The
1535 * text node pertain to a given document.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001536 * Returns a pointer to the new node object.
Daniel Veillard97b58771998-10-20 06:14:16 +00001537 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001538xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001539xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) {
Daniel Veillard0bef1311998-10-14 02:36:47 +00001540 xmlNodePtr cur;
1541
1542 cur = xmlNewTextLen(content, len);
1543 if (cur != NULL) cur->doc = doc;
1544 return(cur);
1545}
1546
Daniel Veillard97b58771998-10-20 06:14:16 +00001547/**
1548 * xmlNewComment:
1549 * @content: the comment content
1550 *
1551 * Creation of a new node containing a comment.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001552 * Returns a pointer to the new node object.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001553 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001554xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001555xmlNewComment(const xmlChar *content) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001556 xmlNodePtr cur;
1557
1558 /*
1559 * Allocate a new node and fill the fields.
1560 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00001561 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillard260a68f1998-08-13 03:39:55 +00001562 if (cur == NULL) {
1563 fprintf(stderr, "xmlNewComment : malloc failed\n");
1564 return(NULL);
1565 }
Daniel Veillardcf461992000-03-14 18:30:20 +00001566 memset(cur, 0, sizeof(xmlNode));
1567 cur->type = XML_COMMENT_NODE;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001568
Daniel Veillardcf461992000-03-14 18:30:20 +00001569 cur->name = xmlStrdup(xmlStringComment);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001570 if (content != NULL) {
1571#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard260a68f1998-08-13 03:39:55 +00001572 cur->content = xmlStrdup(content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001573#else
1574 cur->content = xmlBufferCreateSize(0);
1575 xmlBufferSetAllocationScheme(cur->content,
1576 xmlGetBufferAllocationScheme());
1577 xmlBufferAdd(cur->content, content, -1);
1578#endif
Daniel Veillardcf461992000-03-14 18:30:20 +00001579 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00001580 return(cur);
1581}
1582
Daniel Veillard97b58771998-10-20 06:14:16 +00001583/**
Daniel Veillardb05deb71999-08-10 19:04:08 +00001584 * xmlNewCDataBlock:
1585 * @doc: the document
1586 * @content: the CData block content content
1587 * @len: the length of the block
1588 *
1589 * Creation of a new node containing a CData block.
1590 * Returns a pointer to the new node object.
1591 */
1592xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001593xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) {
Daniel Veillardb05deb71999-08-10 19:04:08 +00001594 xmlNodePtr cur;
1595
1596 /*
1597 * Allocate a new node and fill the fields.
1598 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00001599 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillardb05deb71999-08-10 19:04:08 +00001600 if (cur == NULL) {
1601 fprintf(stderr, "xmlNewCDataBlock : malloc failed\n");
1602 return(NULL);
1603 }
Daniel Veillardcf461992000-03-14 18:30:20 +00001604 memset(cur, 0, sizeof(xmlNode));
Daniel Veillardb05deb71999-08-10 19:04:08 +00001605 cur->type = XML_CDATA_SECTION_NODE;
Daniel Veillardcf461992000-03-14 18:30:20 +00001606
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001607 if (content != NULL) {
1608#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardb05deb71999-08-10 19:04:08 +00001609 cur->content = xmlStrndup(content, len);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001610#else
1611 cur->content = xmlBufferCreateSize(len);
1612 xmlBufferSetAllocationScheme(cur->content,
1613 xmlGetBufferAllocationScheme());
1614 xmlBufferAdd(cur->content, content, len);
1615#endif
Daniel Veillardcf461992000-03-14 18:30:20 +00001616 }
Daniel Veillardb05deb71999-08-10 19:04:08 +00001617 return(cur);
1618}
1619
1620/**
Daniel Veillard1e346af1999-02-22 10:33:01 +00001621 * xmlNewDocComment:
Daniel Veillard97b58771998-10-20 06:14:16 +00001622 * @doc: the document
1623 * @content: the comment content
1624 *
1625 * Creation of a new node containing a commentwithin a document.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001626 * Returns a pointer to the new node object.
Daniel Veillard97b58771998-10-20 06:14:16 +00001627 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001628xmlNodePtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00001629xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) {
Daniel Veillard0bef1311998-10-14 02:36:47 +00001630 xmlNodePtr cur;
1631
1632 cur = xmlNewComment(content);
1633 if (cur != NULL) cur->doc = doc;
1634 return(cur);
1635}
1636
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001637
Daniel Veillard97b58771998-10-20 06:14:16 +00001638/**
1639 * xmlNewChild:
1640 * @parent: the parent node
1641 * @ns: a namespace if any
1642 * @name: the name of the child
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001643 * @content: the XML content of the child if any.
Daniel Veillard97b58771998-10-20 06:14:16 +00001644 *
Daniel Veillardcf461992000-03-14 18:30:20 +00001645 * Creation of a new child element, added at the end of @parent children list.
Daniel Veillardccb09631998-10-27 06:21:04 +00001646 * @ns and @content parameters are optionnal (NULL). If content is non NULL,
1647 * a child list containing the TEXTs and ENTITY_REFs node will be created.
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001648 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
1649 * references, but XML special chars need to be escaped first by using
1650 * xmlEncodeEntitiesReentrant(). Use xmlNewTextChild() if entities
1651 * support is not needed.
1652 *
Daniel Veillard1e346af1999-02-22 10:33:01 +00001653 * Returns a pointer to the new node object.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001654 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001655xmlNodePtr
1656xmlNewChild(xmlNodePtr parent, xmlNsPtr ns,
Daniel Veillard11a48ec1999-11-23 10:40:46 +00001657 const xmlChar *name, const xmlChar *content) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001658 xmlNodePtr cur, prev;
1659
1660 if (parent == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001661#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00001662 fprintf(stderr, "xmlNewChild : parent == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001663#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00001664 return(NULL);
1665 }
1666
1667 if (name == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001668#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00001669 fprintf(stderr, "xmlNewChild : name == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001670#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00001671 return(NULL);
1672 }
1673
1674 /*
1675 * Allocate a new node
1676 */
1677 if (ns == NULL)
Daniel Veillardccb09631998-10-27 06:21:04 +00001678 cur = xmlNewDocNode(parent->doc, parent->ns, name, content);
Daniel Veillard260a68f1998-08-13 03:39:55 +00001679 else
Daniel Veillardccb09631998-10-27 06:21:04 +00001680 cur = xmlNewDocNode(parent->doc, ns, name, content);
Daniel Veillard260a68f1998-08-13 03:39:55 +00001681 if (cur == NULL) return(NULL);
1682
1683 /*
Daniel Veillardcf461992000-03-14 18:30:20 +00001684 * add the new element at the end of the children list.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001685 */
Daniel Veillardccb09631998-10-27 06:21:04 +00001686 cur->type = XML_ELEMENT_NODE;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001687 cur->parent = parent;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001688 cur->doc = parent->doc;
Daniel Veillardcf461992000-03-14 18:30:20 +00001689 if (parent->children == NULL) {
1690 parent->children = cur;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001691 parent->last = cur;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001692 } else {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001693 prev = parent->last;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001694 prev->next = cur;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001695 cur->prev = prev;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001696 parent->last = cur;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001697 }
1698
1699 return(cur);
1700}
1701
Daniel Veillard97b58771998-10-20 06:14:16 +00001702/**
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001703 * xmlAddNextSibling:
1704 * @cur: the child node
1705 * @elem: the new node
1706 *
1707 * Add a new element @elem as the next siblings of @cur
1708 * If the new element was already inserted in a document it is
1709 * first unlinked from its existing context.
1710 *
1711 * Returns the new element or NULL in case of error.
1712 */
1713xmlNodePtr
1714xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) {
1715 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001716#ifdef DEBUG_TREE
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001717 fprintf(stderr, "xmlAddNextSibling : cur == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001718#endif
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001719 return(NULL);
1720 }
1721 if (elem == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001722#ifdef DEBUG_TREE
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001723 fprintf(stderr, "xmlAddNextSibling : elem == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001724#endif
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001725 return(NULL);
1726 }
1727
1728 xmlUnlinkNode(elem);
1729 elem->doc = cur->doc;
1730 elem->parent = cur->parent;
Daniel Veillard5d211f42000-04-07 17:00:24 +00001731 elem->prev = cur;
1732 elem->next = cur->next;
1733 cur->next = elem;
1734 if (elem->next != NULL)
1735 elem->next->prev = elem;
1736 if ((elem->parent != NULL) && (elem->parent->last == cur))
1737 elem->parent->last = elem;
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001738 return(elem);
1739}
1740
1741/**
1742 * xmlAddPrevSibling:
1743 * @cur: the child node
1744 * @elem: the new node
1745 *
1746 * Add a new element @elem as the previous siblings of @cur
1747 * If the new element was already inserted in a document it is
1748 * first unlinked from its existing context.
1749 *
1750 * Returns the new element or NULL in case of error.
1751 */
1752xmlNodePtr
1753xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) {
1754 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001755#ifdef DEBUG_TREE
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001756 fprintf(stderr, "xmlAddPrevSibling : cur == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001757#endif
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001758 return(NULL);
1759 }
1760 if (elem == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001761#ifdef DEBUG_TREE
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001762 fprintf(stderr, "xmlAddPrevSibling : elem == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001763#endif
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001764 return(NULL);
1765 }
1766
1767 xmlUnlinkNode(elem);
1768 elem->doc = cur->doc;
1769 elem->parent = cur->parent;
Daniel Veillard5d211f42000-04-07 17:00:24 +00001770 elem->next = cur;
1771 elem->prev = cur->prev;
1772 cur->prev = elem;
1773 if (elem->prev != NULL)
1774 elem->prev->next = elem;
1775 if ((elem->parent != NULL) && (elem->parent->children == cur))
1776 elem->parent->children = elem;
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001777 return(elem);
1778}
1779
1780/**
Daniel Veillardb96e6431999-08-29 21:02:19 +00001781 * xmlAddSibling:
1782 * @cur: the child node
1783 * @elem: the new node
1784 *
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001785 * Add a new element @elem to the list of siblings of @cur
1786 * If the new element was already inserted in a document it is
1787 * first unlinked from its existing context.
1788 *
1789 * Returns the new element or NULL in case of error.
Daniel Veillardb96e6431999-08-29 21:02:19 +00001790 */
1791xmlNodePtr
1792xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) {
1793 xmlNodePtr parent;
1794
1795 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001796#ifdef DEBUG_TREE
Daniel Veillardb96e6431999-08-29 21:02:19 +00001797 fprintf(stderr, "xmlAddSibling : cur == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001798#endif
Daniel Veillardb96e6431999-08-29 21:02:19 +00001799 return(NULL);
1800 }
1801
1802 if (elem == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001803#ifdef DEBUG_TREE
Daniel Veillardb96e6431999-08-29 21:02:19 +00001804 fprintf(stderr, "xmlAddSibling : elem == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001805#endif
Daniel Veillardb96e6431999-08-29 21:02:19 +00001806 return(NULL);
1807 }
1808
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001809 /*
1810 * Constant time is we can rely on the ->parent->last to find
1811 * the last sibling.
1812 */
1813 if ((cur->parent != NULL) &&
Daniel Veillardcf461992000-03-14 18:30:20 +00001814 (cur->parent->children != NULL) &&
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001815 (cur->parent->last != NULL) &&
1816 (cur->parent->last->next == NULL)) {
1817 cur = cur->parent->last;
1818 } else {
1819 while (cur->next != NULL) cur = cur->next;
Daniel Veillardb96e6431999-08-29 21:02:19 +00001820 }
1821
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00001822 xmlUnlinkNode(elem);
Daniel Veillardb96e6431999-08-29 21:02:19 +00001823 if (elem->doc == NULL)
1824 elem->doc = cur->doc; /* the parent may not be linked to a doc ! */
1825
1826 parent = cur->parent;
1827 elem->prev = cur;
1828 elem->next = NULL;
1829 elem->parent = parent;
1830 cur->next = elem;
1831 if (parent != NULL)
1832 parent->last = elem;
1833
1834 return(elem);
1835}
1836
1837/**
Daniel Veillard97b58771998-10-20 06:14:16 +00001838 * xmlAddChild:
1839 * @parent: the parent node
1840 * @cur: the child node
1841 *
1842 * Add a new child element, to @parent, at the end of the child list.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001843 * Returns the child or NULL in case of error.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001844 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001845xmlNodePtr
1846xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001847 xmlNodePtr prev;
1848
1849 if (parent == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001850#ifdef DEBUG_TREE
Daniel Veillard10a2c651999-12-12 13:03:50 +00001851 fprintf(stderr, "xmlAddChild : parent == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001852#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00001853 return(NULL);
1854 }
1855
1856 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001857#ifdef DEBUG_TREE
Daniel Veillard10a2c651999-12-12 13:03:50 +00001858 fprintf(stderr, "xmlAddChild : child == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001859#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00001860 return(NULL);
1861 }
1862
Daniel Veillard0bef1311998-10-14 02:36:47 +00001863 if ((cur->doc != NULL) && (parent->doc != NULL) &&
1864 (cur->doc != parent->doc)) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001865#ifdef DEBUG_TREE
Daniel Veillard0bef1311998-10-14 02:36:47 +00001866 fprintf(stderr, "Elements moved to a different document\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001867#endif
Daniel Veillard0bef1311998-10-14 02:36:47 +00001868 }
1869
Daniel Veillard260a68f1998-08-13 03:39:55 +00001870 /*
Daniel Veillardcf461992000-03-14 18:30:20 +00001871 * add the new element at the end of the children list.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001872 */
1873 cur->parent = parent;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001874 cur->doc = parent->doc; /* the parent may not be linked to a doc ! */
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001875
Daniel Veillardccb09631998-10-27 06:21:04 +00001876 /*
1877 * Handle the case where parent->content != NULL, in that case it will
1878 * create a intermediate TEXT node.
1879 */
Daniel Veillardcf461992000-03-14 18:30:20 +00001880 if (((parent->type == XML_ELEMENT_NODE) || (parent->type == XML_TEXT_NODE)) &&
1881 (parent->content != NULL)) {
Daniel Veillardccb09631998-10-27 06:21:04 +00001882 xmlNodePtr text;
1883
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001884#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardccb09631998-10-27 06:21:04 +00001885 text = xmlNewDocText(parent->doc, parent->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001886#else
1887 text = xmlNewDocText(parent->doc, xmlBufferContent(parent->content));
1888#endif
Daniel Veillardccb09631998-10-27 06:21:04 +00001889 if (text != NULL) {
Daniel Veillardcf461992000-03-14 18:30:20 +00001890 text->next = parent->children;
Daniel Veillardccb09631998-10-27 06:21:04 +00001891 if (text->next != NULL)
1892 text->next->prev = text;
Daniel Veillardcf461992000-03-14 18:30:20 +00001893 parent->children = text;
Daniel Veillard1e346af1999-02-22 10:33:01 +00001894 UPDATE_LAST_CHILD(parent)
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001895#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard6454aec1999-09-02 22:04:43 +00001896 xmlFree(parent->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001897#else
1898 xmlBufferFree(parent->content);
1899#endif
Daniel Veillardccb09631998-10-27 06:21:04 +00001900 parent->content = NULL;
1901 }
1902 }
Daniel Veillardcf461992000-03-14 18:30:20 +00001903 if (parent->children == NULL) {
1904 parent->children = cur;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001905 parent->last = cur;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001906 } else {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001907 prev = parent->last;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001908 prev->next = cur;
Daniel Veillard0bef1311998-10-14 02:36:47 +00001909 cur->prev = prev;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001910 parent->last = cur;
Daniel Veillard260a68f1998-08-13 03:39:55 +00001911 }
1912
1913 return(cur);
1914}
1915
Daniel Veillard97b58771998-10-20 06:14:16 +00001916/**
1917 * xmlGetLastChild:
1918 * @parent: the parent node
1919 *
1920 * Search the last child of a node.
Daniel Veillard1e346af1999-02-22 10:33:01 +00001921 * Returns the last child or NULL if none.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001922 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001923xmlNodePtr
1924xmlGetLastChild(xmlNodePtr parent) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001925 if (parent == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001926#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00001927 fprintf(stderr, "xmlGetLastChild : parent == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001928#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00001929 return(NULL);
1930 }
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00001931 return(parent->last);
Daniel Veillard260a68f1998-08-13 03:39:55 +00001932}
1933
Daniel Veillard97b58771998-10-20 06:14:16 +00001934/**
1935 * xmlFreeNodeList:
1936 * @cur: the first node in the list
1937 *
1938 * Free a node and all its siblings, this is a recursive behaviour, all
Daniel Veillardcf461992000-03-14 18:30:20 +00001939 * the children are freed too.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001940 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001941void
1942xmlFreeNodeList(xmlNodePtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001943 xmlNodePtr next;
1944 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001945#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00001946 fprintf(stderr, "xmlFreeNodeList : node == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001947#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00001948 return;
1949 }
1950 while (cur != NULL) {
1951 next = cur->next;
1952 xmlFreeNode(cur);
1953 cur = next;
1954 }
1955}
1956
Daniel Veillard97b58771998-10-20 06:14:16 +00001957/**
1958 * xmlFreeNode:
1959 * @cur: the node
1960 *
Daniel Veillardcf461992000-03-14 18:30:20 +00001961 * Free a node, this is a recursive behaviour, all the children are freed too.
1962 * This doesn't unlink the child from the list, use xmlUnlinkNode() first.
Daniel Veillard260a68f1998-08-13 03:39:55 +00001963 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00001964void
1965xmlFreeNode(xmlNodePtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00001966 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001967#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00001968 fprintf(stderr, "xmlFreeNode : node == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00001969#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00001970 return;
1971 }
Daniel Veillardcf461992000-03-14 18:30:20 +00001972 if (cur->type == XML_DTD_NODE)
1973 return;
Daniel Veillardccb09631998-10-27 06:21:04 +00001974 cur->doc = NULL;
1975 cur->parent = NULL;
1976 cur->next = NULL;
1977 cur->prev = NULL;
Daniel Veillardcf461992000-03-14 18:30:20 +00001978 if ((cur->children != NULL) &&
1979 (cur->type != XML_ENTITY_REF_NODE))
1980 xmlFreeNodeList(cur->children);
Daniel Veillardccb09631998-10-27 06:21:04 +00001981 if (cur->properties != NULL) xmlFreePropList(cur->properties);
1982 if (cur->type != XML_ENTITY_REF_NODE)
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001983#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard6454aec1999-09-02 22:04:43 +00001984 if (cur->content != NULL) xmlFree(cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00001985#else
1986 if (cur->content != NULL) xmlBufferFree(cur->content);
1987#endif
Daniel Veillard6454aec1999-09-02 22:04:43 +00001988 if (cur->name != NULL) xmlFree((char *) cur->name);
Daniel Veillard260a68f1998-08-13 03:39:55 +00001989 if (cur->nsDef != NULL) xmlFreeNsList(cur->nsDef);
1990 memset(cur, -1, sizeof(xmlNode));
Daniel Veillard6454aec1999-09-02 22:04:43 +00001991 xmlFree(cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +00001992}
1993
Daniel Veillard16253641998-10-28 22:58:05 +00001994/**
1995 * xmlUnlinkNode:
1996 * @cur: the node
1997 *
1998 * Unlink a node from it's current context, the node is not freed
1999 */
2000void
2001xmlUnlinkNode(xmlNodePtr cur) {
2002 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00002003#ifdef DEBUG_TREE
Daniel Veillard16253641998-10-28 22:58:05 +00002004 fprintf(stderr, "xmlUnlinkNode : node == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00002005#endif
Daniel Veillard16253641998-10-28 22:58:05 +00002006 return;
2007 }
Daniel Veillardcf461992000-03-14 18:30:20 +00002008 if ((cur->parent != NULL) && (cur->parent->children == cur))
2009 cur->parent->children = cur->next;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002010 if ((cur->parent != NULL) && (cur->parent->last == cur))
2011 cur->parent->last = cur->prev;
Daniel Veillard16253641998-10-28 22:58:05 +00002012 if (cur->next != NULL)
2013 cur->next->prev = cur->prev;
2014 if (cur->prev != NULL)
2015 cur->prev->next = cur->next;
2016 cur->next = cur->prev = NULL;
2017 cur->parent = NULL;
2018}
2019
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002020/**
2021 * xmlReplaceNode:
2022 * @old: the old node
2023 * @cur: the node
2024 *
2025 * Unlink the old node from it's current context, prune the new one
2026 * at the same place. If cur was already inserted in a document it is
2027 * first unlinked from its existing context.
2028 *
2029 * Returns the old node
2030 */
2031xmlNodePtr
2032xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) {
2033 if (old == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00002034#ifdef DEBUG_TREE
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002035 fprintf(stderr, "xmlReplaceNode : old == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00002036#endif
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002037 return(NULL);
2038 }
2039 if (cur == NULL) {
2040 xmlUnlinkNode(old);
2041 return(old);
2042 }
2043 xmlUnlinkNode(cur);
2044 cur->doc = old->doc;
2045 cur->parent = old->parent;
2046 cur->next = old->next;
2047 if (cur->next != NULL)
2048 cur->next->prev = cur;
2049 cur->prev = old->prev;
2050 if (cur->prev != NULL)
2051 cur->prev->next = cur;
2052 if (cur->parent != NULL) {
Daniel Veillardcf461992000-03-14 18:30:20 +00002053 if (cur->parent->children == old)
2054 cur->parent->children = cur;
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002055 if (cur->parent->last == old)
2056 cur->parent->last = cur;
2057 }
2058 old->next = old->prev = NULL;
2059 old->parent = NULL;
2060 return(old);
2061}
2062
Daniel Veillard260a68f1998-08-13 03:39:55 +00002063/************************************************************************
2064 * *
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002065 * Copy operations *
2066 * *
2067 ************************************************************************/
2068
2069/**
2070 * xmlCopyNamespace:
2071 * @cur: the namespace
2072 *
2073 * Do a copy of the namespace.
2074 *
2075 * Returns: a new xmlNsPtr, or NULL in case of error.
2076 */
2077xmlNsPtr
2078xmlCopyNamespace(xmlNsPtr cur) {
2079 xmlNsPtr ret;
2080
2081 if (cur == NULL) return(NULL);
2082 switch (cur->type) {
2083 case XML_GLOBAL_NAMESPACE:
2084 ret = xmlNewGlobalNs(NULL, cur->href, cur->prefix);
2085 break;
2086 case XML_LOCAL_NAMESPACE:
2087 ret = xmlNewNs(NULL, cur->href, cur->prefix);
2088 break;
2089 default:
Daniel Veillardad8f99d2000-01-15 14:20:03 +00002090#ifdef DEBUG_TREE
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002091 fprintf(stderr, "xmlCopyNamespace: unknown type %d\n", cur->type);
Daniel Veillardad8f99d2000-01-15 14:20:03 +00002092#endif
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002093 return(NULL);
2094 }
2095 return(ret);
2096}
2097
2098/**
2099 * xmlCopyNamespaceList:
2100 * @cur: the first namespace
2101 *
2102 * Do a copy of an namespace list.
2103 *
2104 * Returns: a new xmlNsPtr, or NULL in case of error.
2105 */
2106xmlNsPtr
2107xmlCopyNamespaceList(xmlNsPtr cur) {
2108 xmlNsPtr ret = NULL;
2109 xmlNsPtr p = NULL,q;
2110
2111 while (cur != NULL) {
2112 q = xmlCopyNamespace(cur);
2113 if (p == NULL) {
2114 ret = p = q;
2115 } else {
2116 p->next = q;
2117 p = q;
2118 }
2119 cur = cur->next;
2120 }
2121 return(ret);
2122}
2123
2124/**
2125 * xmlCopyProp:
Daniel Veillardb96e6431999-08-29 21:02:19 +00002126 * @target: the element where the attribute will be grafted
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002127 * @cur: the attribute
2128 *
2129 * Do a copy of the attribute.
2130 *
2131 * Returns: a new xmlAttrPtr, or NULL in case of error.
2132 */
2133xmlAttrPtr
Daniel Veillardb96e6431999-08-29 21:02:19 +00002134xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002135 xmlAttrPtr ret;
2136
2137 if (cur == NULL) return(NULL);
Daniel Veillardcf461992000-03-14 18:30:20 +00002138 if (cur->parent != NULL)
2139 ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL);
2140 else if (cur->children != NULL)
2141 ret = xmlNewDocProp(cur->children->doc, cur->name, NULL);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002142 else
2143 ret = xmlNewDocProp(NULL, cur->name, NULL);
2144 if (ret == NULL) return(NULL);
Daniel Veillardcf461992000-03-14 18:30:20 +00002145 ret->parent = target;
Daniel Veillardb96e6431999-08-29 21:02:19 +00002146
2147 if ((cur->ns != NULL) && (target != NULL)) {
2148 xmlNsPtr ns;
2149
2150 ns = xmlSearchNs(target->doc, target, cur->ns->prefix);
2151 ret->ns = ns;
2152 } else
2153 ret->ns = NULL;
2154
Daniel Veillardcf461992000-03-14 18:30:20 +00002155 if (cur->children != NULL)
2156 ret->children = xmlCopyNodeList(cur->children);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002157 return(ret);
2158}
2159
2160/**
2161 * xmlCopyPropList:
Daniel Veillardb96e6431999-08-29 21:02:19 +00002162 * @target: the element where the attributes will be grafted
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002163 * @cur: the first attribute
2164 *
2165 * Do a copy of an attribute list.
2166 *
2167 * Returns: a new xmlAttrPtr, or NULL in case of error.
2168 */
2169xmlAttrPtr
Daniel Veillardb96e6431999-08-29 21:02:19 +00002170xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) {
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002171 xmlAttrPtr ret = NULL;
2172 xmlAttrPtr p = NULL,q;
2173
2174 while (cur != NULL) {
Daniel Veillardb96e6431999-08-29 21:02:19 +00002175 q = xmlCopyProp(target, cur);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002176 if (p == NULL) {
2177 ret = p = q;
2178 } else {
2179 p->next = q;
Daniel Veillardcf461992000-03-14 18:30:20 +00002180 q->prev = p;
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002181 p = q;
2182 }
2183 cur = cur->next;
2184 }
2185 return(ret);
2186}
2187
2188/*
Daniel Veillard11a48ec1999-11-23 10:40:46 +00002189 * NOTE abeut the CopyNode operations !
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002190 *
2191 * They are splitted into external and internal parts for one
2192 * tricky reason: namespaces. Doing a direct copy of a node
2193 * say RPM:Copyright without changing the namespace pointer to
2194 * something else can produce stale links. One way to do it is
2195 * to keep a reference counter but this doesn't work as soon
2196 * as one move the element or the subtree out of the scope of
2197 * the existing namespace. The actual solution seems to add
2198 * a copy of the namespace at the top of the copied tree if
2199 * not available in the subtree.
2200 * Hence two functions, the public front-end call the inner ones
2201 */
2202
2203static xmlNodePtr
2204xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent);
2205
2206static xmlNodePtr
2207xmlStaticCopyNode(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent,
2208 int recursive) {
2209 xmlNodePtr ret;
2210
2211 if (node == NULL) return(NULL);
2212 /*
2213 * Allocate a new node and fill the fields.
2214 */
Daniel Veillard6454aec1999-09-02 22:04:43 +00002215 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002216 if (ret == NULL) {
2217 fprintf(stderr, "xmlStaticCopyNode : malloc failed\n");
2218 return(NULL);
2219 }
Daniel Veillardcf461992000-03-14 18:30:20 +00002220 memset(ret, 0, sizeof(xmlNode));
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002221 ret->type = node->type;
Daniel Veillardcf461992000-03-14 18:30:20 +00002222
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002223 ret->doc = doc;
2224 ret->parent = parent;
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002225 if (node->name != NULL)
2226 ret->name = xmlStrdup(node->name);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002227 if ((node->content != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
2228#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002229 ret->content = xmlStrdup(node->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002230#else
2231 ret->content = xmlBufferCreateSize(xmlBufferLength(node->content));
2232 xmlBufferSetAllocationScheme(ret->content,
2233 xmlGetBufferAllocationScheme());
2234 xmlBufferAdd(ret->content,
2235 xmlBufferContent(node->content),
2236 xmlBufferLength(node->content));
2237#endif
Daniel Veillardcf461992000-03-14 18:30:20 +00002238 }
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002239 if (parent != NULL)
2240 xmlAddChild(parent, ret);
2241
2242 if (!recursive) return(ret);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002243 if (node->nsDef != NULL)
2244 ret->nsDef = xmlCopyNamespaceList(node->nsDef);
2245
2246 if (node->ns != NULL) {
2247 xmlNsPtr ns;
2248
2249 ns = xmlSearchNs(doc, ret, node->ns->prefix);
2250 if (ns == NULL) {
2251 /*
2252 * Humm, we are copying an element whose namespace is defined
2253 * out of the new tree scope. Search it in the original tree
2254 * and add it at the top of the new tree
2255 */
2256 ns = xmlSearchNs(node->doc, node, node->ns->prefix);
2257 if (ns != NULL) {
2258 xmlNodePtr root = ret;
2259
2260 while (root->parent != NULL) root = root->parent;
2261 xmlNewNs(root, ns->href, ns->prefix);
2262 }
2263 } else {
2264 /*
2265 * reference the existing namespace definition in our own tree.
2266 */
2267 ret->ns = ns;
2268 }
2269 }
Daniel Veillardb96e6431999-08-29 21:02:19 +00002270 if (node->properties != NULL)
2271 ret->properties = xmlCopyPropList(ret, node->properties);
Daniel Veillardcf461992000-03-14 18:30:20 +00002272 if (node->children != NULL)
2273 ret->children = xmlStaticCopyNodeList(node->children, doc, ret);
Daniel Veillard1e346af1999-02-22 10:33:01 +00002274 UPDATE_LAST_CHILD(ret)
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002275 return(ret);
2276}
2277
2278static xmlNodePtr
2279xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) {
2280 xmlNodePtr ret = NULL;
2281 xmlNodePtr p = NULL,q;
2282
2283 while (node != NULL) {
2284 q = xmlStaticCopyNode(node, doc, parent, 1);
2285 if (parent == NULL) {
2286 if (ret == NULL) ret = q;
2287 } else {
2288 if (ret == NULL) {
2289 q->prev = NULL;
2290 ret = p = q;
2291 } else {
2292 p->next = q;
2293 q->prev = p;
2294 p = q;
2295 }
2296 }
2297 node = node->next;
2298 }
2299 return(ret);
2300}
2301
2302/**
2303 * xmlCopyNode:
2304 * @node: the node
2305 * @recursive: if 1 do a recursive copy.
2306 *
2307 * Do a copy of the node.
2308 *
2309 * Returns: a new xmlNodePtr, or NULL in case of error.
2310 */
2311xmlNodePtr
2312xmlCopyNode(xmlNodePtr node, int recursive) {
2313 xmlNodePtr ret;
2314
2315 ret = xmlStaticCopyNode(node, NULL, NULL, recursive);
2316 return(ret);
2317}
2318
2319/**
2320 * xmlCopyNodeList:
2321 * @node: the first node in the list.
2322 *
2323 * Do a recursive copy of the node list.
2324 *
2325 * Returns: a new xmlNodePtr, or NULL in case of error.
2326 */
2327xmlNodePtr xmlCopyNodeList(xmlNodePtr node) {
2328 xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL);
2329 return(ret);
2330}
2331
2332/**
2333 * xmlCopyElement:
2334 * @elem: the element
2335 *
2336 * Do a copy of the element definition.
2337 *
2338 * Returns: a new xmlElementPtr, or NULL in case of error.
2339xmlElementPtr
2340xmlCopyElement(xmlElementPtr elem) {
2341 xmlElementPtr ret;
2342
2343 if (elem == NULL) return(NULL);
2344 ret = xmlNewDocElement(elem->doc, elem->ns, elem->name, elem->content);
2345 if (ret == NULL) return(NULL);
2346 if (!recursive) return(ret);
2347 if (elem->properties != NULL)
2348 ret->properties = xmlCopyPropList(elem->properties);
2349
2350 if (elem->nsDef != NULL)
2351 ret->nsDef = xmlCopyNamespaceList(elem->nsDef);
Daniel Veillardcf461992000-03-14 18:30:20 +00002352 if (elem->children != NULL)
2353 ret->children = xmlCopyElementList(elem->children);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002354 return(ret);
2355}
2356 */
2357
2358/**
2359 * xmlCopyDtd:
2360 * @dtd: the dtd
2361 *
2362 * Do a copy of the dtd.
2363 *
2364 * Returns: a new xmlDtdPtr, or NULL in case of error.
2365 */
2366xmlDtdPtr
2367xmlCopyDtd(xmlDtdPtr dtd) {
2368 xmlDtdPtr ret;
2369
2370 if (dtd == NULL) return(NULL);
2371 ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID);
2372 if (ret == NULL) return(NULL);
2373 if (dtd->entities != NULL)
2374 ret->entities = (void *) xmlCopyEntitiesTable(
2375 (xmlEntitiesTablePtr) dtd->entities);
Daniel Veillard1e346af1999-02-22 10:33:01 +00002376 if (dtd->notations != NULL)
2377 ret->notations = (void *) xmlCopyNotationTable(
2378 (xmlNotationTablePtr) dtd->notations);
2379 if (dtd->elements != NULL)
2380 ret->elements = (void *) xmlCopyElementTable(
2381 (xmlElementTablePtr) dtd->elements);
2382 if (dtd->attributes != NULL)
2383 ret->attributes = (void *) xmlCopyAttributeTable(
2384 (xmlAttributeTablePtr) dtd->attributes);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002385 return(ret);
2386}
2387
2388/**
2389 * xmlCopyDoc:
2390 * @doc: the document
2391 * @recursive: if 1 do a recursive copy.
2392 *
2393 * Do a copy of the document info. If recursive, the content tree will
2394 * be copied too as well as Dtd, namespaces and entities.
2395 *
2396 * Returns: a new xmlDocPtr, or NULL in case of error.
2397 */
2398xmlDocPtr
2399xmlCopyDoc(xmlDocPtr doc, int recursive) {
2400 xmlDocPtr ret;
2401
2402 if (doc == NULL) return(NULL);
2403 ret = xmlNewDoc(doc->version);
2404 if (ret == NULL) return(NULL);
2405 if (doc->name != NULL)
Daniel Veillard6454aec1999-09-02 22:04:43 +00002406 ret->name = xmlMemStrdup(doc->name);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002407 if (doc->encoding != NULL)
2408 ret->encoding = xmlStrdup(doc->encoding);
2409 ret->compression = doc->compression;
2410 ret->standalone = doc->standalone;
2411 if (!recursive) return(ret);
2412
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002413 if (doc->intSubset != NULL)
2414 ret->intSubset = xmlCopyDtd(doc->intSubset);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002415 if (doc->oldNs != NULL)
2416 ret->oldNs = xmlCopyNamespaceList(doc->oldNs);
Daniel Veillardcf461992000-03-14 18:30:20 +00002417 if (doc->children != NULL)
2418 ret->children = xmlStaticCopyNodeList(doc->children, ret, NULL);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002419 return(ret);
2420}
2421
2422/************************************************************************
2423 * *
Daniel Veillard260a68f1998-08-13 03:39:55 +00002424 * Content access functions *
2425 * *
2426 ************************************************************************/
2427
Daniel Veillard97b58771998-10-20 06:14:16 +00002428/**
Daniel Veillard944b5ff1999-12-15 19:08:24 +00002429 * xmlDocGetRootElement:
2430 * @doc: the document
2431 *
Daniel Veillardcf461992000-03-14 18:30:20 +00002432 * Get the root element of the document (doc->children is a list
Daniel Veillard944b5ff1999-12-15 19:08:24 +00002433 * containing possibly comments, PIs, etc ...).
2434 *
2435 * Returns the xmlNodePtr for the root or NULL
2436 */
2437xmlNodePtr
2438xmlDocGetRootElement(xmlDocPtr doc) {
2439 xmlNodePtr ret;
2440
2441 if (doc == NULL) return(NULL);
Daniel Veillardcf461992000-03-14 18:30:20 +00002442 ret = doc->children;
Daniel Veillard944b5ff1999-12-15 19:08:24 +00002443 while (ret != NULL) {
2444 if (ret->type == XML_ELEMENT_NODE)
2445 return(ret);
2446 ret = ret->next;
2447 }
2448 return(ret);
2449}
2450
2451/**
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002452 * xmlDocSetRootElement:
2453 * @doc: the document
2454 * @root: the new document root element
2455 *
Daniel Veillardcf461992000-03-14 18:30:20 +00002456 * Set the root element of the document (doc->children is a list
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002457 * containing possibly comments, PIs, etc ...).
2458 *
2459 * Returns the old root element if any was found
2460 */
2461xmlNodePtr
2462xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) {
2463 xmlNodePtr old = NULL;
2464
2465 if (doc == NULL) return(NULL);
Daniel Veillardcf461992000-03-14 18:30:20 +00002466 old = doc->children;
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002467 while (old != NULL) {
2468 if (old->type == XML_ELEMENT_NODE)
2469 break;
2470 old = old->next;
2471 }
2472 if (old == NULL) {
Daniel Veillardcf461992000-03-14 18:30:20 +00002473 if (doc->children == NULL) {
2474 doc->children = root;
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002475 } else {
Daniel Veillardcf461992000-03-14 18:30:20 +00002476 xmlAddSibling(doc->children, root);
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002477 }
2478 } else {
2479 xmlReplaceNode(old, root);
2480 }
2481 return(old);
2482}
2483
2484/**
Daniel Veillardb96e6431999-08-29 21:02:19 +00002485 * xmlNodeSetLang:
2486 * @cur: the node being changed
2487 * @lang: the langage description
2488 *
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002489 * Set the language of a node, i.e. the values of the xml:lang
2490 * attribute.
Daniel Veillardb96e6431999-08-29 21:02:19 +00002491 */
2492void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002493xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) {
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002494 if (cur == NULL) return;
2495 switch(cur->type) {
2496 case XML_TEXT_NODE:
2497 case XML_CDATA_SECTION_NODE:
2498 case XML_COMMENT_NODE:
2499 case XML_DOCUMENT_NODE:
2500 case XML_DOCUMENT_TYPE_NODE:
2501 case XML_DOCUMENT_FRAG_NODE:
2502 case XML_NOTATION_NODE:
2503 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardcf461992000-03-14 18:30:20 +00002504 case XML_DTD_NODE:
2505 case XML_ELEMENT_DECL:
2506 case XML_ATTRIBUTE_DECL:
2507 case XML_ENTITY_DECL:
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002508 return;
2509 case XML_ELEMENT_NODE:
2510 case XML_ATTRIBUTE_NODE:
2511 case XML_PI_NODE:
2512 case XML_ENTITY_REF_NODE:
2513 case XML_ENTITY_NODE:
2514 break;
2515 }
Daniel Veillardb96e6431999-08-29 21:02:19 +00002516 xmlSetProp(cur, BAD_CAST "xml:lang", lang);
2517}
2518
2519/**
2520 * xmlNodeGetLang:
2521 * @cur: the node being checked
2522 *
2523 * Searches the language of a node, i.e. the values of the xml:lang
2524 * attribute or the one carried by the nearest ancestor.
2525 *
2526 * Returns a pointer to the lang value, or NULL if not found
Daniel Veillarda819dac1999-11-24 18:04:22 +00002527 * It's up to the caller to free the memory.
Daniel Veillardb96e6431999-08-29 21:02:19 +00002528 */
Daniel Veillarda819dac1999-11-24 18:04:22 +00002529xmlChar *
Daniel Veillardb96e6431999-08-29 21:02:19 +00002530xmlNodeGetLang(xmlNodePtr cur) {
Daniel Veillarda819dac1999-11-24 18:04:22 +00002531 xmlChar *lang;
Daniel Veillardb96e6431999-08-29 21:02:19 +00002532
2533 while (cur != NULL) {
2534 lang = xmlGetProp(cur, BAD_CAST "xml:lang");
2535 if (lang != NULL)
2536 return(lang);
2537 cur = cur->parent;
2538 }
2539 return(NULL);
2540}
2541
2542/**
Daniel Veillardcf461992000-03-14 18:30:20 +00002543 * xmlNodeGetSpacePreserve:
2544 * @cur: the node being checked
2545 *
2546 * Searches the language of a node, i.e. the values of the xml:space
2547 * attribute or the one carried by the nearest ancestor.
2548 *
2549 * Returns -1 if xml:space is not inheried, 0 if "default", 1 if "preserve"
2550 */
2551int
2552xmlNodeGetSpacePreserve(xmlNodePtr cur) {
2553 xmlChar *space;
2554
2555 while (cur != NULL) {
2556 space = xmlGetProp(cur, BAD_CAST "xml:space");
2557 if (space != NULL) {
2558 if (!xmlStrcmp(space, BAD_CAST "preserve")) {
2559 xmlFree(space);
2560 return(1);
2561 }
2562 if (!xmlStrcmp(space, BAD_CAST "default")) {
2563 xmlFree(space);
2564 return(0);
2565 }
2566 xmlFree(space);
2567 }
2568 cur = cur->parent;
2569 }
2570 return(-1);
2571}
2572
2573/**
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002574 * xmlNodeSetName:
2575 * @cur: the node being changed
2576 * @name: the new tag name
2577 *
2578 * Searches the language of a node, i.e. the values of the xml:lang
2579 * attribute or the one carried by the nearest ancestor.
2580 */
2581void
2582xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) {
2583 if (cur == NULL) return;
2584 if (name == NULL) return;
2585 switch(cur->type) {
2586 case XML_TEXT_NODE:
2587 case XML_CDATA_SECTION_NODE:
2588 case XML_COMMENT_NODE:
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002589 case XML_DOCUMENT_TYPE_NODE:
2590 case XML_DOCUMENT_FRAG_NODE:
2591 case XML_NOTATION_NODE:
2592 case XML_HTML_DOCUMENT_NODE:
2593 return;
2594 case XML_ELEMENT_NODE:
2595 case XML_ATTRIBUTE_NODE:
2596 case XML_PI_NODE:
2597 case XML_ENTITY_REF_NODE:
2598 case XML_ENTITY_NODE:
Daniel Veillardcf461992000-03-14 18:30:20 +00002599 case XML_DTD_NODE:
2600 case XML_DOCUMENT_NODE:
2601 case XML_ELEMENT_DECL:
2602 case XML_ATTRIBUTE_DECL:
2603 case XML_ENTITY_DECL:
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002604 break;
2605 }
2606 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
2607 cur->name = xmlStrdup(name);
2608}
2609
2610/**
Daniel Veillard10a2c651999-12-12 13:03:50 +00002611 * xmlNodeGetBase:
2612 * @doc: the document the node pertains to
2613 * @cur: the node being checked
2614 *
2615 * Searches for the BASE URL. The code should work on both XML
2616 * and HTML document even if base mechanisms are completely different.
2617 *
2618 * Returns a pointer to the base URL, or NULL if not found
2619 * It's up to the caller to free the memory.
2620 */
2621xmlChar *
2622xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) {
2623 xmlChar *base;
2624
2625 if ((cur == NULL) && (doc == NULL))
2626 return(NULL);
2627 if (doc == NULL) doc = cur->doc;
2628 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillardcf461992000-03-14 18:30:20 +00002629 cur = doc->children;
Daniel Veillard10a2c651999-12-12 13:03:50 +00002630 while ((cur != NULL) && (cur->name != NULL)) {
2631 if (cur->type != XML_ELEMENT_NODE) {
2632 cur = cur->next;
2633 continue;
2634 }
2635 if ((!xmlStrcmp(cur->name, BAD_CAST "html")) ||
2636 (!xmlStrcmp(cur->name, BAD_CAST "HTML"))) {
Daniel Veillardcf461992000-03-14 18:30:20 +00002637 cur = cur->children;
Daniel Veillard10a2c651999-12-12 13:03:50 +00002638 continue;
2639 }
2640 if ((!xmlStrcmp(cur->name, BAD_CAST "head")) ||
2641 (!xmlStrcmp(cur->name, BAD_CAST "HEAD"))) {
Daniel Veillardcf461992000-03-14 18:30:20 +00002642 cur = cur->children;
Daniel Veillard10a2c651999-12-12 13:03:50 +00002643 continue;
2644 }
2645 if ((!xmlStrcmp(cur->name, BAD_CAST "base")) ||
2646 (!xmlStrcmp(cur->name, BAD_CAST "BASE"))) {
2647 base = xmlGetProp(cur, BAD_CAST "href");
2648 if (base != NULL) return(base);
2649 return(xmlGetProp(cur, BAD_CAST "HREF"));
2650 }
Daniel Veillardcf461992000-03-14 18:30:20 +00002651 cur = cur->next;
Daniel Veillard10a2c651999-12-12 13:03:50 +00002652 }
Daniel Veillardcf461992000-03-14 18:30:20 +00002653 if ((doc != NULL) && (doc->URL != NULL))
2654 return(xmlStrdup(doc->URL));
Daniel Veillard10a2c651999-12-12 13:03:50 +00002655 return(NULL);
2656 }
2657 while (cur != NULL) {
2658 base = xmlGetProp(cur, BAD_CAST "xml:base");
2659 if (base != NULL)
2660 return(base);
2661 cur = cur->parent;
2662 }
Daniel Veillardcf461992000-03-14 18:30:20 +00002663 if ((doc != NULL) && (doc->URL != NULL))
2664 return(xmlStrdup(doc->URL));
Daniel Veillard10a2c651999-12-12 13:03:50 +00002665 return(NULL);
2666}
2667
2668/**
Daniel Veillard16253641998-10-28 22:58:05 +00002669 * xmlNodeGetContent:
2670 * @cur: the node being read
2671 *
2672 * Read the value of a node, this can be either the text carried
2673 * directly by this node if it's a TEXT node or the aggregate string
2674 * of the values carried by this node child's (TEXT and ENTITY_REF).
2675 * Entity references are substitued.
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002676 * Returns a new xmlChar * or NULL if no content is available.
Daniel Veillard5099ae81999-04-21 20:12:07 +00002677 * It's up to the caller to free the memory.
Daniel Veillard16253641998-10-28 22:58:05 +00002678 */
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002679xmlChar *
Daniel Veillard16253641998-10-28 22:58:05 +00002680xmlNodeGetContent(xmlNodePtr cur) {
2681 if (cur == NULL) return(NULL);
2682 switch (cur->type) {
2683 case XML_DOCUMENT_FRAG_NODE:
2684 case XML_ELEMENT_NODE:
Daniel Veillardcf461992000-03-14 18:30:20 +00002685 return(xmlNodeListGetString(cur->doc, cur->children, 1));
Daniel Veillard16253641998-10-28 22:58:05 +00002686 break;
Daniel Veillardb96e6431999-08-29 21:02:19 +00002687 case XML_ATTRIBUTE_NODE: {
2688 xmlAttrPtr attr = (xmlAttrPtr) cur;
Daniel Veillardcf461992000-03-14 18:30:20 +00002689 if (attr->parent != NULL)
2690 return(xmlNodeListGetString(attr->parent->doc, attr->children, 1));
Daniel Veillardb96e6431999-08-29 21:02:19 +00002691 else
Daniel Veillardcf461992000-03-14 18:30:20 +00002692 return(xmlNodeListGetString(NULL, attr->children, 1));
Daniel Veillardb96e6431999-08-29 21:02:19 +00002693 break;
2694 }
Daniel Veillarddbfd6411999-12-28 16:35:14 +00002695 case XML_COMMENT_NODE:
Daniel Veillardb96e6431999-08-29 21:02:19 +00002696 case XML_PI_NODE:
2697 if (cur->content != NULL)
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002698#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardb96e6431999-08-29 21:02:19 +00002699 return(xmlStrdup(cur->content));
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002700#else
2701 return(xmlStrdup(xmlBufferContent(cur->content)));
2702#endif
Daniel Veillardb96e6431999-08-29 21:02:19 +00002703 return(NULL);
Daniel Veillard16253641998-10-28 22:58:05 +00002704 case XML_ENTITY_REF_NODE:
Daniel Veillarddbfd6411999-12-28 16:35:14 +00002705 /*
2706 * Locate the entity, and get it's content
2707 * @@@
2708 */
2709 return(NULL);
Daniel Veillard16253641998-10-28 22:58:05 +00002710 case XML_ENTITY_NODE:
Daniel Veillard16253641998-10-28 22:58:05 +00002711 case XML_DOCUMENT_NODE:
Daniel Veillard7c1206f1999-10-14 09:10:25 +00002712 case XML_HTML_DOCUMENT_NODE:
Daniel Veillard16253641998-10-28 22:58:05 +00002713 case XML_DOCUMENT_TYPE_NODE:
2714 case XML_NOTATION_NODE:
Daniel Veillardcf461992000-03-14 18:30:20 +00002715 case XML_DTD_NODE:
2716 return(NULL);
2717 case XML_ELEMENT_DECL:
2718 /* TODO !!! */
2719 return(NULL);
2720 case XML_ATTRIBUTE_DECL:
2721 /* TODO !!! */
2722 return(NULL);
2723 case XML_ENTITY_DECL:
2724 /* TODO !!! */
Daniel Veillard16253641998-10-28 22:58:05 +00002725 return(NULL);
Daniel Veillardb05deb71999-08-10 19:04:08 +00002726 case XML_CDATA_SECTION_NODE:
Daniel Veillard16253641998-10-28 22:58:05 +00002727 case XML_TEXT_NODE:
2728 if (cur->content != NULL)
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002729#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard16253641998-10-28 22:58:05 +00002730 return(xmlStrdup(cur->content));
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002731#else
2732 return(xmlStrdup(xmlBufferContent(cur->content)));
2733#endif
Daniel Veillard16253641998-10-28 22:58:05 +00002734 return(NULL);
2735 }
2736 return(NULL);
2737}
2738
2739/**
Daniel Veillard97b58771998-10-20 06:14:16 +00002740 * xmlNodeSetContent:
2741 * @cur: the node being modified
2742 * @content: the new value of the content
2743 *
2744 * Replace the content of a node.
Daniel Veillard260a68f1998-08-13 03:39:55 +00002745 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00002746void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002747xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00002748 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00002749#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00002750 fprintf(stderr, "xmlNodeSetContent : node == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00002751#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00002752 return;
2753 }
Daniel Veillard16253641998-10-28 22:58:05 +00002754 switch (cur->type) {
2755 case XML_DOCUMENT_FRAG_NODE:
2756 case XML_ELEMENT_NODE:
2757 if (cur->content != NULL) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002758#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard6454aec1999-09-02 22:04:43 +00002759 xmlFree(cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002760#else
2761 xmlBufferFree(cur->content);
2762#endif
Daniel Veillard16253641998-10-28 22:58:05 +00002763 cur->content = NULL;
2764 }
Daniel Veillardcf461992000-03-14 18:30:20 +00002765 if (cur->children != NULL) xmlFreeNodeList(cur->children);
2766 cur->children = xmlStringGetNodeList(cur->doc, content);
Daniel Veillard1e346af1999-02-22 10:33:01 +00002767 UPDATE_LAST_CHILD(cur)
Daniel Veillard16253641998-10-28 22:58:05 +00002768 break;
2769 case XML_ATTRIBUTE_NODE:
2770 break;
2771 case XML_TEXT_NODE:
2772 case XML_CDATA_SECTION_NODE:
2773 case XML_ENTITY_REF_NODE:
2774 case XML_ENTITY_NODE:
2775 case XML_PI_NODE:
2776 case XML_COMMENT_NODE:
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002777 if (cur->content != NULL) {
2778#ifndef XML_USE_BUFFER_CONTENT
2779 xmlFree(cur->content);
2780#else
2781 xmlBufferFree(cur->content);
2782#endif
2783 }
Daniel Veillardcf461992000-03-14 18:30:20 +00002784 if (cur->children != NULL) xmlFreeNodeList(cur->children);
2785 cur->last = cur->children = NULL;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002786 if (content != NULL) {
2787#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard16253641998-10-28 22:58:05 +00002788 cur->content = xmlStrdup(content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002789#else
2790 cur->content = xmlBufferCreateSize(0);
2791 xmlBufferSetAllocationScheme(cur->content,
2792 xmlGetBufferAllocationScheme());
2793 xmlBufferAdd(cur->content, content, -1);
2794#endif
2795 } else
Daniel Veillard16253641998-10-28 22:58:05 +00002796 cur->content = NULL;
Daniel Veillardb96e6431999-08-29 21:02:19 +00002797 break;
Daniel Veillard16253641998-10-28 22:58:05 +00002798 case XML_DOCUMENT_NODE:
Daniel Veillard7c1206f1999-10-14 09:10:25 +00002799 case XML_HTML_DOCUMENT_NODE:
Daniel Veillard16253641998-10-28 22:58:05 +00002800 case XML_DOCUMENT_TYPE_NODE:
2801 break;
2802 case XML_NOTATION_NODE:
2803 break;
Daniel Veillardcf461992000-03-14 18:30:20 +00002804 case XML_DTD_NODE:
2805 break;
2806 case XML_ELEMENT_DECL:
2807 /* TODO !!! */
2808 break;
2809 case XML_ATTRIBUTE_DECL:
2810 /* TODO !!! */
2811 break;
2812 case XML_ENTITY_DECL:
2813 /* TODO !!! */
2814 break;
Daniel Veillard16253641998-10-28 22:58:05 +00002815 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00002816}
2817
Daniel Veillard97b58771998-10-20 06:14:16 +00002818/**
2819 * xmlNodeSetContentLen:
2820 * @cur: the node being modified
2821 * @content: the new value of the content
2822 * @len: the size of @content
2823 *
2824 * Replace the content of a node.
Daniel Veillard260a68f1998-08-13 03:39:55 +00002825 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00002826void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002827xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00002828 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00002829#ifdef DEBUG_TREE
Daniel Veillard16253641998-10-28 22:58:05 +00002830 fprintf(stderr, "xmlNodeSetContentLen : node == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00002831#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00002832 return;
2833 }
Daniel Veillard16253641998-10-28 22:58:05 +00002834 switch (cur->type) {
2835 case XML_DOCUMENT_FRAG_NODE:
2836 case XML_ELEMENT_NODE:
2837 if (cur->content != NULL) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002838#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard6454aec1999-09-02 22:04:43 +00002839 xmlFree(cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002840#else
2841 xmlBufferFree(cur->content);
2842#endif
Daniel Veillard16253641998-10-28 22:58:05 +00002843 cur->content = NULL;
2844 }
Daniel Veillardcf461992000-03-14 18:30:20 +00002845 if (cur->children != NULL) xmlFreeNodeList(cur->children);
2846 cur->children = xmlStringLenGetNodeList(cur->doc, content, len);
Daniel Veillard1e346af1999-02-22 10:33:01 +00002847 UPDATE_LAST_CHILD(cur)
Daniel Veillard16253641998-10-28 22:58:05 +00002848 break;
2849 case XML_ATTRIBUTE_NODE:
2850 break;
2851 case XML_TEXT_NODE:
2852 case XML_CDATA_SECTION_NODE:
2853 case XML_ENTITY_REF_NODE:
2854 case XML_ENTITY_NODE:
2855 case XML_PI_NODE:
2856 case XML_COMMENT_NODE:
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002857 case XML_NOTATION_NODE:
2858 if (cur->content != NULL) {
2859#ifndef XML_USE_BUFFER_CONTENT
2860 xmlFree(cur->content);
2861#else
2862 xmlBufferFree(cur->content);
2863#endif
2864 }
Daniel Veillardcf461992000-03-14 18:30:20 +00002865 if (cur->children != NULL) xmlFreeNodeList(cur->children);
2866 cur->children = cur->last = NULL;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002867 if (content != NULL) {
2868#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard16253641998-10-28 22:58:05 +00002869 cur->content = xmlStrndup(content, len);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002870#else
2871 cur->content = xmlBufferCreateSize(len);
2872 xmlBufferSetAllocationScheme(cur->content,
2873 xmlGetBufferAllocationScheme());
2874 xmlBufferAdd(cur->content, content, len);
2875#endif
2876 } else
Daniel Veillard16253641998-10-28 22:58:05 +00002877 cur->content = NULL;
Daniel Veillardb96e6431999-08-29 21:02:19 +00002878 break;
Daniel Veillard16253641998-10-28 22:58:05 +00002879 case XML_DOCUMENT_NODE:
Daniel Veillardcf461992000-03-14 18:30:20 +00002880 case XML_DTD_NODE:
Daniel Veillard7c1206f1999-10-14 09:10:25 +00002881 case XML_HTML_DOCUMENT_NODE:
Daniel Veillard16253641998-10-28 22:58:05 +00002882 case XML_DOCUMENT_TYPE_NODE:
2883 break;
Daniel Veillardcf461992000-03-14 18:30:20 +00002884 case XML_ELEMENT_DECL:
2885 /* TODO !!! */
2886 break;
2887 case XML_ATTRIBUTE_DECL:
2888 /* TODO !!! */
2889 break;
2890 case XML_ENTITY_DECL:
2891 /* TODO !!! */
2892 break;
Daniel Veillard260a68f1998-08-13 03:39:55 +00002893 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00002894}
2895
Daniel Veillard97b58771998-10-20 06:14:16 +00002896/**
2897 * xmlNodeAddContentLen:
2898 * @cur: the node being modified
2899 * @content: extra content
2900 * @len: the size of @content
2901 *
2902 * Append the extra substring to the node content.
Daniel Veillard260a68f1998-08-13 03:39:55 +00002903 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00002904void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002905xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00002906 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00002907#ifdef DEBUG_TREE
Daniel Veillard16253641998-10-28 22:58:05 +00002908 fprintf(stderr, "xmlNodeAddContentLen : node == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00002909#endif
Daniel Veillard16253641998-10-28 22:58:05 +00002910 return;
2911 }
2912 if (len <= 0) return;
2913 switch (cur->type) {
2914 case XML_DOCUMENT_FRAG_NODE:
2915 case XML_ELEMENT_NODE: {
2916 xmlNodePtr last = NULL, new;
2917
Daniel Veillardcf461992000-03-14 18:30:20 +00002918 if (cur->children != NULL) {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002919 last = cur->last;
Daniel Veillard16253641998-10-28 22:58:05 +00002920 } else {
2921 if (cur->content != NULL) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002922#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardcf461992000-03-14 18:30:20 +00002923 cur->children = xmlStringGetNodeList(cur->doc, cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002924#else
Daniel Veillardcf461992000-03-14 18:30:20 +00002925 cur->children = xmlStringGetNodeList(cur->doc,
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002926 xmlBufferContent(cur->content));
2927#endif
Daniel Veillard1e346af1999-02-22 10:33:01 +00002928 UPDATE_LAST_CHILD(cur)
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002929#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard6454aec1999-09-02 22:04:43 +00002930 xmlFree(cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002931#else
2932 xmlBufferFree(cur->content);
2933#endif
Daniel Veillard16253641998-10-28 22:58:05 +00002934 cur->content = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002935 last = cur->last;
Daniel Veillard16253641998-10-28 22:58:05 +00002936 }
2937 }
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002938 new = xmlNewTextLen(content, len);
Daniel Veillard16253641998-10-28 22:58:05 +00002939 if (new != NULL) {
2940 xmlAddChild(cur, new);
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002941 if ((last != NULL) && (last->next == new)) {
Daniel Veillard16253641998-10-28 22:58:05 +00002942 xmlTextMerge(last, new);
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002943 }
Daniel Veillard16253641998-10-28 22:58:05 +00002944 }
2945 break;
2946 }
2947 case XML_ATTRIBUTE_NODE:
2948 break;
2949 case XML_TEXT_NODE:
2950 case XML_CDATA_SECTION_NODE:
2951 case XML_ENTITY_REF_NODE:
2952 case XML_ENTITY_NODE:
2953 case XML_PI_NODE:
2954 case XML_COMMENT_NODE:
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002955 case XML_NOTATION_NODE:
2956 if (content != NULL) {
2957#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard16253641998-10-28 22:58:05 +00002958 cur->content = xmlStrncat(cur->content, content, len);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002959#else
2960 xmlBufferAdd(cur->content, content, len);
2961#endif
2962 }
Daniel Veillard16253641998-10-28 22:58:05 +00002963 case XML_DOCUMENT_NODE:
Daniel Veillardcf461992000-03-14 18:30:20 +00002964 case XML_DTD_NODE:
Daniel Veillard7c1206f1999-10-14 09:10:25 +00002965 case XML_HTML_DOCUMENT_NODE:
Daniel Veillard16253641998-10-28 22:58:05 +00002966 case XML_DOCUMENT_TYPE_NODE:
2967 break;
Daniel Veillardcf461992000-03-14 18:30:20 +00002968 case XML_ELEMENT_DECL:
2969 case XML_ATTRIBUTE_DECL:
2970 case XML_ENTITY_DECL:
2971 break;
Daniel Veillard16253641998-10-28 22:58:05 +00002972 }
2973}
2974
2975/**
2976 * xmlNodeAddContent:
2977 * @cur: the node being modified
2978 * @content: extra content
2979 *
2980 * Append the extra substring to the node content.
2981 */
2982void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002983xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) {
Daniel Veillard16253641998-10-28 22:58:05 +00002984 int len;
2985
2986 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00002987#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00002988 fprintf(stderr, "xmlNodeAddContent : node == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00002989#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00002990 return;
2991 }
Daniel Veillard16253641998-10-28 22:58:05 +00002992 if (content == NULL) return;
2993 len = xmlStrlen(content);
2994 xmlNodeAddContentLen(cur, content, len);
2995}
2996
2997/**
2998 * xmlTextMerge:
2999 * @first: the first text node
3000 * @second: the second text node being merged
3001 *
3002 * Merge two text nodes into one
Daniel Veillard1e346af1999-02-22 10:33:01 +00003003 * Returns the first text node augmented
Daniel Veillard16253641998-10-28 22:58:05 +00003004 */
3005xmlNodePtr
3006xmlTextMerge(xmlNodePtr first, xmlNodePtr second) {
3007 if (first == NULL) return(second);
3008 if (second == NULL) return(first);
3009 if (first->type != XML_TEXT_NODE) return(first);
3010 if (second->type != XML_TEXT_NODE) return(first);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003011#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard16253641998-10-28 22:58:05 +00003012 xmlNodeAddContent(first, second->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003013#else
3014 xmlNodeAddContent(first, xmlBufferContent(second->content));
3015#endif
Daniel Veillard16253641998-10-28 22:58:05 +00003016 xmlUnlinkNode(second);
3017 xmlFreeNode(second);
3018 return(first);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003019}
3020
Daniel Veillard97b58771998-10-20 06:14:16 +00003021/**
Daniel Veillardb96e6431999-08-29 21:02:19 +00003022 * xmlGetNsList:
3023 * @doc: the document
3024 * @node: the current node
3025 *
3026 * Search all the namespace applying to a given element.
3027 * Returns an NULL terminated array of all the xmlNsPtr found
3028 * that need to be freed by the caller or NULL if no
3029 * namespace if defined
3030 */
3031xmlNsPtr *
3032xmlGetNsList(xmlDocPtr doc, xmlNodePtr node) {
3033 xmlNsPtr cur;
3034 xmlNsPtr *ret = NULL;
3035 int nbns = 0;
3036 int maxns = 10;
3037 int i;
3038
3039 while (node != NULL) {
3040 cur = node->nsDef;
3041 while (cur != NULL) {
3042 if (ret == NULL) {
Daniel Veillard6454aec1999-09-02 22:04:43 +00003043 ret = (xmlNsPtr *) xmlMalloc((maxns + 1) * sizeof(xmlNsPtr));
Daniel Veillardb96e6431999-08-29 21:02:19 +00003044 if (ret == NULL) {
3045 fprintf(stderr, "xmlGetNsList : out of memory!\n");
3046 return(NULL);
3047 }
3048 ret[nbns] = NULL;
3049 }
3050 for (i = 0;i < nbns;i++) {
3051 if ((cur->prefix == ret[i]->prefix) ||
3052 (!xmlStrcmp(cur->prefix, ret[i]->prefix))) break;
3053 }
3054 if (i >= nbns) {
3055 if (nbns >= maxns) {
3056 maxns *= 2;
Daniel Veillard6454aec1999-09-02 22:04:43 +00003057 ret = (xmlNsPtr *) xmlRealloc(ret,
Daniel Veillardb96e6431999-08-29 21:02:19 +00003058 (maxns + 1) * sizeof(xmlNsPtr));
3059 if (ret == NULL) {
3060 fprintf(stderr, "xmlGetNsList : realloc failed!\n");
3061 return(NULL);
3062 }
3063 }
3064 ret[nbns++] = cur;
3065 ret[nbns] = NULL;
3066 }
3067
3068 cur = cur->next;
3069 }
3070 node = node->parent;
3071 }
3072 return(ret);
3073}
3074
3075/**
Daniel Veillard97b58771998-10-20 06:14:16 +00003076 * xmlSearchNs:
3077 * @doc: the document
3078 * @node: the current node
3079 * @nameSpace: the namespace string
Daniel Veillard260a68f1998-08-13 03:39:55 +00003080 *
Daniel Veillard97b58771998-10-20 06:14:16 +00003081 * Search a Ns registered under a given name space for a document.
3082 * recurse on the parents until it finds the defined namespace
3083 * or return NULL otherwise.
3084 * @nameSpace can be NULL, this is a search for the default namespace.
Daniel Veillard1e346af1999-02-22 10:33:01 +00003085 * Returns the namespace pointer or NULL.
Daniel Veillard260a68f1998-08-13 03:39:55 +00003086 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003087xmlNsPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003088xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003089 xmlNsPtr cur;
3090
Daniel Veillard62ba71e1999-12-16 17:52:19 +00003091 if (node == NULL) return(NULL);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003092 while (node != NULL) {
Daniel Veillardcf461992000-03-14 18:30:20 +00003093 if (node->type == XML_ELEMENT_NODE) {
3094 cur = node->nsDef;
3095 while (cur != NULL) {
3096 if ((cur->prefix == NULL) && (nameSpace == NULL))
3097 return(cur);
3098 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
3099 (!xmlStrcmp(cur->prefix, nameSpace)))
3100 return(cur);
3101 cur = cur->next;
3102 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00003103 }
3104 node = node->parent;
3105 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00003106 return(NULL);
3107}
3108
Daniel Veillard97b58771998-10-20 06:14:16 +00003109/**
3110 * xmlSearchNsByHref:
3111 * @doc: the document
3112 * @node: the current node
3113 * @href: the namespace value
3114 *
3115 * Search a Ns aliasing a given URI. Recurse on the parents until it finds
3116 * the defined namespace or return NULL otherwise.
Daniel Veillard1e346af1999-02-22 10:33:01 +00003117 * Returns the namespace pointer or NULL.
Daniel Veillard260a68f1998-08-13 03:39:55 +00003118 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003119xmlNsPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003120xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar *href) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003121 xmlNsPtr cur;
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003122 xmlNodePtr orig = node;
Daniel Veillard260a68f1998-08-13 03:39:55 +00003123
Daniel Veillard10a2c651999-12-12 13:03:50 +00003124 if ((node == NULL) || (href == NULL)) return(NULL);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003125 while (node != NULL) {
3126 cur = node->nsDef;
3127 while (cur != NULL) {
3128 if ((cur->href != NULL) && (href != NULL) &&
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003129 (!xmlStrcmp(cur->href, href))) {
3130 /*
3131 * Check that the prefix is not shadowed between orig and node
3132 */
3133 xmlNodePtr check = orig;
3134 xmlNsPtr tst;
3135
3136 while (check != node) {
3137 tst = check->nsDef;
3138 while (tst != NULL) {
3139 if ((tst->prefix == NULL) && (cur->prefix == NULL))
3140 goto shadowed;
3141 if ((tst->prefix != NULL) && (cur->prefix != NULL) &&
3142 (!xmlStrcmp(tst->prefix, cur->prefix)))
3143 goto shadowed;
3144 tst = tst->next;
3145 }
3146 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00003147 return(cur);
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003148 }
3149shadowed:
Daniel Veillard260a68f1998-08-13 03:39:55 +00003150 cur = cur->next;
3151 }
3152 node = node->parent;
3153 }
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003154 return(NULL);
3155}
3156
3157/**
3158 * xmlNewReconciliedNs
3159 * @doc: the document
3160 * @tree: a node expected to hold the new namespace
3161 * @ns: the original namespace
3162 *
3163 * This function tries to locate a namespace definition in a tree
3164 * ancestors, or create a new namespace definition node similar to
3165 * @ns trying to reuse the same prefix. However if the given prefix is
3166 * null (default namespace) or reused within the subtree defined by
3167 * @tree or on one of its ancestors then a new prefix is generated.
3168 * Returns the (new) namespace definition or NULL in case of error
3169 */
3170xmlNsPtr
3171xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) {
3172 xmlNsPtr def;
3173 xmlChar prefix[50];
3174 int counter = 1;
3175
3176 if (tree == NULL) {
3177#ifdef DEBUG_TREE
3178 fprintf(stderr, "xmlNewReconciliedNs : tree == NULL\n");
3179#endif
3180 return(NULL);
3181 }
3182 if (ns == NULL) {
3183#ifdef DEBUG_TREE
3184 fprintf(stderr, "xmlNewReconciliedNs : ns == NULL\n");
3185#endif
3186 return(NULL);
3187 }
3188 /*
3189 * Search an existing namespace definition inherited.
3190 */
3191 def = xmlSearchNsByHref(doc, tree, ns->href);
3192 if (def != NULL)
3193 return(def);
3194
3195 /*
3196 * Find a close prefix which is not already in use.
3197 * Let's strip namespace prefixes longer than 20 chars !
3198 */
3199 sprintf((char *) prefix, "%.20s", ns->prefix);
3200 def = xmlSearchNs(doc, tree, prefix);
3201 while (def != NULL) {
3202 if (counter > 1000) return(NULL);
3203 sprintf((char *) prefix, "%.20s%d", ns->prefix, counter++);
3204 def = xmlSearchNs(doc, tree, prefix);
3205 }
3206
3207 /*
3208 * Ok, now we are ready to create a new one.
3209 */
3210 def = xmlNewNs(tree, ns->href, prefix);
3211 return(def);
3212}
3213
3214/**
3215 * xmlReconciliateNs
3216 * @doc: the document
3217 * @tree: a node defining the subtree to reconciliate
3218 *
3219 * This function checks that all the namespaces declared within the given
3220 * tree are properly declared. This is needed for example after Copy or Cut
3221 * and then paste operations. The subtree may still hold pointers to
3222 * namespace declarations outside the subtree or invalid/masked. As much
3223 * as possible the function try tu reuse the existing namespaces found in
3224 * the new environment. If not possible the new namespaces are redeclared
3225 * on @tree at the top of the given subtree.
3226 * Returns the number of namespace declarations created or -1 in case of error.
3227 */
3228int
3229xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) {
3230 xmlNsPtr *oldNs = NULL;
3231 xmlNsPtr *newNs = NULL;
3232 int sizeCache = 0;
3233 int nbCache = 0;
3234
3235 xmlNsPtr n;
3236 xmlNodePtr node = tree;
3237 xmlAttrPtr attr;
3238 int ret = 0, i;
3239
3240 while (node != NULL) {
3241 /*
3242 * Reconciliate the node namespace
3243 */
3244 if (node->ns != NULL) {
3245 /*
3246 * initialize the cache if needed
3247 */
3248 if (sizeCache == 0) {
3249 sizeCache = 10;
3250 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
3251 sizeof(xmlNsPtr));
3252 if (oldNs == NULL) {
3253 fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
3254 return(-1);
3255 }
3256 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
3257 sizeof(xmlNsPtr));
3258 if (newNs == NULL) {
3259 fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
3260 xmlFree(oldNs);
3261 return(-1);
3262 }
3263 }
3264 for (i = 0;i < nbCache;i++) {
3265 if (oldNs[i] == node->ns) {
3266 node->ns = newNs[i];
3267 break;
3268 }
3269 }
3270 if (i == nbCache) {
3271 /*
3272 * Ok we need to recreate a new namespace definition
3273 */
3274 n = xmlNewReconciliedNs(doc, tree, node->ns);
3275 if (n != NULL) { /* :-( what if else ??? */
3276 /*
3277 * check if we need to grow the cache buffers.
3278 */
3279 if (sizeCache <= nbCache) {
3280 sizeCache *= 2;
3281 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
3282 sizeof(xmlNsPtr));
3283 if (oldNs == NULL) {
3284 fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
3285 xmlFree(newNs);
3286 return(-1);
3287 }
3288 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
3289 sizeof(xmlNsPtr));
3290 if (newNs == NULL) {
3291 fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
3292 xmlFree(oldNs);
3293 return(-1);
3294 }
3295 }
3296 newNs[nbCache] = n;
3297 oldNs[nbCache++] = node->ns;
3298 node->ns = n;
3299 }
3300 }
3301 }
3302 /*
3303 * now check for namespace hold by attributes on the node.
3304 */
3305 attr = node->properties;
3306 while (attr != NULL) {
3307 if (attr->ns != NULL) {
3308 /*
3309 * initialize the cache if needed
3310 */
3311 if (sizeCache == 0) {
3312 sizeCache = 10;
3313 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
3314 sizeof(xmlNsPtr));
3315 if (oldNs == NULL) {
3316 fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
3317 return(-1);
3318 }
3319 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
3320 sizeof(xmlNsPtr));
3321 if (newNs == NULL) {
3322 fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
3323 xmlFree(oldNs);
3324 return(-1);
3325 }
3326 }
3327 for (i = 0;i < nbCache;i++) {
3328 if (oldNs[i] == attr->ns) {
3329 node->ns = newNs[i];
3330 break;
3331 }
3332 }
3333 if (i == nbCache) {
3334 /*
3335 * Ok we need to recreate a new namespace definition
3336 */
3337 n = xmlNewReconciliedNs(doc, tree, attr->ns);
3338 if (n != NULL) { /* :-( what if else ??? */
3339 /*
3340 * check if we need to grow the cache buffers.
3341 */
3342 if (sizeCache <= nbCache) {
3343 sizeCache *= 2;
3344 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
3345 sizeof(xmlNsPtr));
3346 if (oldNs == NULL) {
3347 fprintf(stderr,
3348 "xmlReconciliateNs : memory pbm\n");
3349 xmlFree(newNs);
3350 return(-1);
3351 }
3352 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
3353 sizeof(xmlNsPtr));
3354 if (newNs == NULL) {
3355 fprintf(stderr,
3356 "xmlReconciliateNs : memory pbm\n");
3357 xmlFree(oldNs);
3358 return(-1);
3359 }
3360 }
3361 newNs[nbCache] = n;
3362 oldNs[nbCache++] = attr->ns;
3363 attr->ns = n;
3364 }
3365 }
3366 }
3367 attr = attr->next;
3368 }
3369
3370 /*
3371 * Browse the full subtree, deep first
3372 */
Daniel Veillardcf461992000-03-14 18:30:20 +00003373 if (node->children != NULL) {
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003374 /* deep first */
Daniel Veillardcf461992000-03-14 18:30:20 +00003375 node = node->children;
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003376 } else if ((node != tree) && (node->next != NULL)) {
3377 /* then siblings */
3378 node = node->next;
3379 } else if (node != tree) {
3380 /* go up to parents->next if needed */
3381 while (node != tree) {
3382 if (node->parent != NULL)
3383 node = node->parent;
3384 if ((node != tree) && (node->next != NULL)) {
3385 node = node->next;
3386 break;
3387 }
3388 if (node->parent == NULL) {
3389 node = NULL;
3390 break;
3391 }
3392 }
3393 /* exit condition */
3394 if (node == tree)
3395 node = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00003396 }
3397 }
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003398 return(ret);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003399}
3400
Daniel Veillard97b58771998-10-20 06:14:16 +00003401/**
3402 * xmlGetProp:
3403 * @node: the node
3404 * @name: the attribute name
3405 *
3406 * Search and get the value of an attribute associated to a node
Daniel Veillardccb09631998-10-27 06:21:04 +00003407 * This does the entity substitution.
Daniel Veillard10a2c651999-12-12 13:03:50 +00003408 * This function looks in DTD attribute declaration for #FIXED or
3409 * default declaration values unless DTD use has been turned off.
3410 *
Daniel Veillard1e346af1999-02-22 10:33:01 +00003411 * Returns the attribute value or NULL if not found.
Daniel Veillarda819dac1999-11-24 18:04:22 +00003412 * It's up to the caller to free the memory.
Daniel Veillard260a68f1998-08-13 03:39:55 +00003413 */
Daniel Veillarda819dac1999-11-24 18:04:22 +00003414xmlChar *
3415xmlGetProp(xmlNodePtr node, const xmlChar *name) {
Daniel Veillard10a2c651999-12-12 13:03:50 +00003416 xmlAttrPtr prop;
3417 xmlDocPtr doc;
Daniel Veillard260a68f1998-08-13 03:39:55 +00003418
Daniel Veillard10a2c651999-12-12 13:03:50 +00003419 if ((node == NULL) || (name == NULL)) return(NULL);
3420 /*
3421 * Check on the properties attached to the node
3422 */
3423 prop = node->properties;
Daniel Veillard260a68f1998-08-13 03:39:55 +00003424 while (prop != NULL) {
Daniel Veillard68178931999-02-08 18:34:36 +00003425 if (!xmlStrcmp(prop->name, name)) {
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003426 xmlChar *ret;
Daniel Veillard6800ef31999-02-08 18:33:22 +00003427
Daniel Veillardcf461992000-03-14 18:30:20 +00003428 ret = xmlNodeListGetString(node->doc, prop->children, 1);
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003429 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
Daniel Veillard6800ef31999-02-08 18:33:22 +00003430 return(ret);
Daniel Veillard68178931999-02-08 18:34:36 +00003431 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00003432 prop = prop->next;
3433 }
Daniel Veillard10a2c651999-12-12 13:03:50 +00003434 if (!xmlCheckDTD) return(NULL);
3435
3436 /*
3437 * Check if there is a default declaration in the internal
3438 * or external subsets
3439 */
3440 doc = node->doc;
3441 if (doc != NULL) {
3442 xmlAttributePtr attrDecl;
3443 if (doc->intSubset != NULL) {
3444 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
3445 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3446 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
Daniel Veillardf967b902000-01-17 16:06:10 +00003447 if (attrDecl != NULL)
3448 return(xmlStrdup(attrDecl->defaultValue));
Daniel Veillard10a2c651999-12-12 13:03:50 +00003449 }
3450 }
3451 return(NULL);
3452}
3453
3454/**
3455 * xmlGetNsProp:
3456 * @node: the node
3457 * @name: the attribute name
3458 * @namespace: the URI of the namespace
3459 *
3460 * Search and get the value of an attribute associated to a node
3461 * This attribute has to be anchored in the namespace specified.
3462 * This does the entity substitution.
3463 * This function looks in DTD attribute declaration for #FIXED or
3464 * default declaration values unless DTD use has been turned off.
3465 *
3466 * Returns the attribute value or NULL if not found.
3467 * It's up to the caller to free the memory.
3468 */
3469xmlChar *
3470xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *namespace) {
3471 xmlAttrPtr prop = node->properties;
3472 xmlDocPtr doc;
3473 xmlNsPtr ns;
3474
3475 if (namespace == NULL)
3476 return(xmlGetProp(node, name));
3477 while (prop != NULL) {
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00003478 /*
3479 * One need to have
3480 * - same attribute names
3481 * - and the attribute carrying that namespace
3482 * or
3483 * no namespace on the attribute and the element carrying it
3484 */
Daniel Veillard10a2c651999-12-12 13:03:50 +00003485 if ((!xmlStrcmp(prop->name, name)) &&
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00003486 (((prop->ns == NULL) && (node->ns != NULL) &&
3487 (!xmlStrcmp(node->ns->href, namespace))) ||
Daniel Veillard3c558c31999-12-22 11:30:41 +00003488 ((prop->ns != NULL) && (!xmlStrcmp(prop->ns->href, namespace))))) {
Daniel Veillard10a2c651999-12-12 13:03:50 +00003489 xmlChar *ret;
3490
Daniel Veillardcf461992000-03-14 18:30:20 +00003491 ret = xmlNodeListGetString(node->doc, prop->children, 1);
Daniel Veillard10a2c651999-12-12 13:03:50 +00003492 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
3493 return(ret);
3494 }
3495 prop = prop->next;
3496 }
3497 if (!xmlCheckDTD) return(NULL);
3498
3499 /*
3500 * Check if there is a default declaration in the internal
3501 * or external subsets
3502 */
3503 doc = node->doc;
3504 if (doc != NULL) {
3505 xmlAttributePtr attrDecl;
3506 if (doc->intSubset != NULL) {
3507 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
3508 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3509 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
3510
3511 if (attrDecl->prefix != NULL) {
3512 /*
3513 * The DTD declaration only allows a prefix search
3514 */
3515 ns = xmlSearchNs(doc, node, attrDecl->prefix);
3516 if ((ns != NULL) && (!xmlStrcmp(ns->href, namespace)))
3517 return(xmlStrdup(attrDecl->defaultValue));
3518 }
3519 }
3520 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00003521 return(NULL);
3522}
3523
Daniel Veillard97b58771998-10-20 06:14:16 +00003524/**
Daniel Veillardccb09631998-10-27 06:21:04 +00003525 * xmlSetProp:
Daniel Veillard97b58771998-10-20 06:14:16 +00003526 * @node: the node
3527 * @name: the attribute name
3528 * @value: the attribute value
3529 *
3530 * Set (or reset) an attribute carried by a node.
Daniel Veillard1e346af1999-02-22 10:33:01 +00003531 * Returns the attribute pointer.
Daniel Veillard260a68f1998-08-13 03:39:55 +00003532 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003533xmlAttrPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003534xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003535 xmlAttrPtr prop = node->properties;
3536
3537 while (prop != NULL) {
3538 if (!xmlStrcmp(prop->name, name)) {
Daniel Veillardcf461992000-03-14 18:30:20 +00003539 if (prop->children != NULL)
3540 xmlFreeNodeList(prop->children);
3541 prop->children = NULL;
Daniel Veillard51e3b151999-11-12 17:02:31 +00003542 if (value != NULL) {
3543 xmlChar *buffer;
Daniel Veillardcf461992000-03-14 18:30:20 +00003544 xmlNodePtr tmp;
3545
Daniel Veillard51e3b151999-11-12 17:02:31 +00003546 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
Daniel Veillardcf461992000-03-14 18:30:20 +00003547 prop->children = xmlStringGetNodeList(node->doc, buffer);
3548 tmp = prop->children;
3549 while (tmp != NULL) {
3550 tmp->parent = (xmlNodePtr) prop;
3551 if (tmp->next == NULL)
3552 prop->last = tmp;
3553 tmp = tmp->next;
3554 }
Daniel Veillard51e3b151999-11-12 17:02:31 +00003555 xmlFree(buffer);
3556 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00003557 return(prop);
3558 }
3559 prop = prop->next;
3560 }
3561 prop = xmlNewProp(node, name, value);
3562 return(prop);
3563}
3564
Daniel Veillard97b58771998-10-20 06:14:16 +00003565/**
3566 * xmlNodeIsText:
3567 * @node: the node
3568 *
3569 * Is this node a Text node ?
Daniel Veillard1e346af1999-02-22 10:33:01 +00003570 * Returns 1 yes, 0 no
Daniel Veillard260a68f1998-08-13 03:39:55 +00003571 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003572int
3573xmlNodeIsText(xmlNodePtr node) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003574 if (node == NULL) return(0);
3575
Daniel Veillard0bef1311998-10-14 02:36:47 +00003576 if (node->type == XML_TEXT_NODE) return(1);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003577 return(0);
3578}
3579
Daniel Veillard97b58771998-10-20 06:14:16 +00003580/**
Daniel Veillard3e6d2372000-03-04 11:39:43 +00003581 * xmlIsBlankNode:
3582 * @node: the node
3583 *
3584 * Is this node a Text node ?
3585 * Returns 1 yes, 0 no
3586 */
3587int
3588xmlIsBlankNode(xmlNodePtr node) {
3589 xmlChar *cur;
3590 if (node == NULL) return(0);
3591
3592 if (node->type != XML_TEXT_NODE) return(0);
3593 if (node->content == NULL) return(0);
3594 cur = node->content;
3595 while (*cur != 0) {
3596 if (!IS_BLANK(*cur)) return(0);
3597 cur++;
3598 }
3599
3600 return(1);
3601}
3602
3603/**
Daniel Veillard1e346af1999-02-22 10:33:01 +00003604 * xmlTextConcat:
Daniel Veillard97b58771998-10-20 06:14:16 +00003605 * @node: the node
3606 * @content: the content
3607 * @len: @content lenght
3608 *
3609 * Concat the given string at the end of the existing node content
Daniel Veillard260a68f1998-08-13 03:39:55 +00003610 */
Daniel Veillard97b58771998-10-20 06:14:16 +00003611
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003612void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003613xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003614 if (node == NULL) return;
3615
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003616 if ((node->type != XML_TEXT_NODE) &&
3617 (node->type != XML_CDATA_SECTION_NODE)) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003618#ifdef DEBUG_TREE
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003619 fprintf(stderr, "xmlTextConcat: node is not text nor cdata\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003620#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00003621 return;
3622 }
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003623#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard260a68f1998-08-13 03:39:55 +00003624 node->content = xmlStrncat(node->content, content, len);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003625#else
3626 xmlBufferAdd(node->content, content, len);
3627#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00003628}
3629
3630/************************************************************************
3631 * *
3632 * Output : to a FILE or in memory *
3633 * *
3634 ************************************************************************/
3635
Daniel Veillard5099ae81999-04-21 20:12:07 +00003636#define BASE_BUFFER_SIZE 4000
3637
3638/**
3639 * xmlBufferCreate:
3640 *
3641 * routine to create an XML buffer.
3642 * returns the new structure.
3643 */
3644xmlBufferPtr
3645xmlBufferCreate(void) {
3646 xmlBufferPtr ret;
3647
Daniel Veillard6454aec1999-09-02 22:04:43 +00003648 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
Daniel Veillard5099ae81999-04-21 20:12:07 +00003649 if (ret == NULL) {
3650 fprintf(stderr, "xmlBufferCreate : out of memory!\n");
3651 return(NULL);
3652 }
3653 ret->use = 0;
3654 ret->size = BASE_BUFFER_SIZE;
Daniel Veillard10a2c651999-12-12 13:03:50 +00003655 ret->alloc = xmlBufferAllocScheme;
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003656 ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar));
Daniel Veillard5099ae81999-04-21 20:12:07 +00003657 if (ret->content == NULL) {
3658 fprintf(stderr, "xmlBufferCreate : out of memory!\n");
Daniel Veillard6454aec1999-09-02 22:04:43 +00003659 xmlFree(ret);
Daniel Veillard5099ae81999-04-21 20:12:07 +00003660 return(NULL);
3661 }
3662 ret->content[0] = 0;
3663 return(ret);
3664}
3665
3666/**
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003667 * xmlBufferCreateSize:
3668 * @size: initial size of buffer
3669 *
3670 * routine to create an XML buffer.
3671 * returns the new structure.
3672 */
3673xmlBufferPtr
3674xmlBufferCreateSize(size_t size) {
3675 xmlBufferPtr ret;
3676
3677 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
3678 if (ret == NULL) {
3679 fprintf(stderr, "xmlBufferCreate : out of memory!\n");
3680 return(NULL);
3681 }
3682 ret->use = 0;
Daniel Veillard10a2c651999-12-12 13:03:50 +00003683 ret->alloc = xmlBufferAllocScheme;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003684 ret->size = (size ? size+2 : 0); /* +1 for ending null */
Daniel Veillard10a2c651999-12-12 13:03:50 +00003685 if (ret->size){
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003686 ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar));
3687 if (ret->content == NULL) {
3688 fprintf(stderr, "xmlBufferCreate : out of memory!\n");
3689 xmlFree(ret);
3690 return(NULL);
3691 }
3692 ret->content[0] = 0;
3693 } else
3694 ret->content = NULL;
3695 return(ret);
3696}
3697
3698/**
3699 * xmlBufferAllocationScheme:
3700 * @buf: the buffer to free
3701 * @scheme: allocation scheme to use
3702 *
3703 * Sets the allocation scheme for this buffer
3704 */
3705void
3706xmlBufferSetAllocationScheme(xmlBufferPtr buf,
3707 xmlBufferAllocationScheme scheme) {
3708 if (buf == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003709#ifdef DEBUG_BUFFER
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003710 fprintf(stderr, "xmlBufferSetAllocationScheme: buf == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003711#endif
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003712 return;
3713 }
3714
3715 buf->alloc = scheme;
3716}
3717
3718/**
Daniel Veillard5099ae81999-04-21 20:12:07 +00003719 * xmlBufferFree:
3720 * @buf: the buffer to free
3721 *
3722 * Frees an XML buffer.
3723 */
3724void
3725xmlBufferFree(xmlBufferPtr buf) {
3726 if (buf == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003727#ifdef DEBUG_BUFFER
Daniel Veillard5099ae81999-04-21 20:12:07 +00003728 fprintf(stderr, "xmlBufferFree: buf == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003729#endif
Daniel Veillard5099ae81999-04-21 20:12:07 +00003730 return;
3731 }
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003732 if (buf->content != NULL) {
3733#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard5099ae81999-04-21 20:12:07 +00003734 memset(buf->content, -1, BASE_BUFFER_SIZE);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003735#else
3736 memset(buf->content, -1, buf->size);
3737#endif
Daniel Veillard6454aec1999-09-02 22:04:43 +00003738 xmlFree(buf->content);
Daniel Veillard5099ae81999-04-21 20:12:07 +00003739 }
3740 memset(buf, -1, sizeof(xmlBuffer));
Daniel Veillard6454aec1999-09-02 22:04:43 +00003741 xmlFree(buf);
Daniel Veillard5099ae81999-04-21 20:12:07 +00003742}
3743
3744/**
Daniel Veillarde2d034d1999-07-27 19:52:06 +00003745 * xmlBufferEmpty:
3746 * @buf: the buffer
3747 *
3748 * empty a buffer.
3749 */
3750void
3751xmlBufferEmpty(xmlBufferPtr buf) {
3752 buf->use = 0;
3753 memset(buf->content, -1, buf->size);/* just for debug */
3754}
3755
3756/**
3757 * xmlBufferShrink:
3758 * @buf: the buffer to dump
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003759 * @len: the number of xmlChar to remove
Daniel Veillarde2d034d1999-07-27 19:52:06 +00003760 *
3761 * Remove the beginning of an XML buffer.
3762 *
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003763 * Returns the number of xmlChar removed, or -1 in case of failure.
Daniel Veillarde2d034d1999-07-27 19:52:06 +00003764 */
3765int
3766xmlBufferShrink(xmlBufferPtr buf, int len) {
3767 if (len == 0) return(0);
3768 if (len > buf->use) return(-1);
3769
3770 buf->use -= len;
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003771 memmove(buf->content, &buf->content[len], buf->use * sizeof(xmlChar));
Daniel Veillarde2d034d1999-07-27 19:52:06 +00003772
3773 buf->content[buf->use] = 0;
3774 return(len);
3775}
3776
3777/**
Daniel Veillard5099ae81999-04-21 20:12:07 +00003778 * xmlBufferDump:
3779 * @file: the file output
3780 * @buf: the buffer to dump
3781 *
3782 * Dumps an XML buffer to a FILE *.
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003783 * Returns the number of xmlChar written
Daniel Veillard5099ae81999-04-21 20:12:07 +00003784 */
3785int
3786xmlBufferDump(FILE *file, xmlBufferPtr buf) {
3787 int ret;
3788
3789 if (buf == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003790#ifdef DEBUG_BUFFER
Daniel Veillard5099ae81999-04-21 20:12:07 +00003791 fprintf(stderr, "xmlBufferDump: buf == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003792#endif
Daniel Veillard5099ae81999-04-21 20:12:07 +00003793 return(0);
3794 }
3795 if (buf->content == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003796#ifdef DEBUG_BUFFER
Daniel Veillard5099ae81999-04-21 20:12:07 +00003797 fprintf(stderr, "xmlBufferDump: buf->content == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003798#endif
Daniel Veillard5099ae81999-04-21 20:12:07 +00003799 return(0);
3800 }
3801 if (file == NULL) file = stdout;
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003802 ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
Daniel Veillard5099ae81999-04-21 20:12:07 +00003803 return(ret);
3804}
3805
3806/**
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003807 * xmlBufferContent:
3808 * @buf: the buffer to resize
3809 *
3810 * Returns the internal content
3811 */
3812
3813const xmlChar*
3814xmlBufferContent(const xmlBufferPtr buf)
3815{
3816 if(!buf)
3817 return NULL;
3818
3819 return buf->content;
3820}
3821
3822/**
3823 * xmlBufferLength:
3824 * @buf: the buffer
3825 *
3826 * Returns the length of data in the internal content
3827 */
3828
3829int
3830xmlBufferLength(const xmlBufferPtr buf)
3831{
3832 if(!buf)
3833 return 0;
3834
3835 return buf->use;
3836}
3837
3838/**
3839 * xmlBufferResize:
3840 * @buf: the buffer to resize
3841 * @len: the desired size
3842 *
3843 * Resize a buffer to accomodate minimum size of <len>.
3844 *
3845 * Returns 0 in case of problems, 1 otherwise
3846 */
3847int
3848xmlBufferResize(xmlBufferPtr buf, int size)
3849{
3850 int newSize = (buf->size ? buf->size*2 : size);/*take care of empty case*/
3851 xmlChar* rebuf = NULL;
3852
3853 /* Don't resize if we don't have to */
3854 if(size < buf->size)
3855 return 1;
3856
3857 /* figure out new size */
3858 switch(buf->alloc){
3859 case XML_BUFFER_ALLOC_DOUBLEIT:
3860 while(size > newSize) newSize *= 2;
3861 break;
3862 case XML_BUFFER_ALLOC_EXACT:
3863 newSize = size+10;
3864 break;
3865 default:
3866 newSize = size+10;
3867 break;
3868 }
3869
3870 if (buf->content == NULL)
3871 rebuf = (xmlChar *) xmlMalloc(newSize * sizeof(xmlChar));
3872 else
3873 rebuf = (xmlChar *) xmlRealloc(buf->content,
3874 newSize * sizeof(xmlChar));
3875 if (rebuf == NULL) {
3876 fprintf(stderr, "xmlBufferAdd : out of memory!\n");
3877 return 0;
3878 }
3879 buf->content = rebuf;
3880 buf->size = newSize;
3881
3882 return 1;
3883}
3884/**
Daniel Veillard5099ae81999-04-21 20:12:07 +00003885 * xmlBufferAdd:
3886 * @buf: the buffer to dump
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003887 * @str: the xmlChar string
3888 * @len: the number of xmlChar to add
Daniel Veillard5099ae81999-04-21 20:12:07 +00003889 *
Daniel Veillard10a2c651999-12-12 13:03:50 +00003890 * Add a string range to an XML buffer. if len == -1, the lenght of
3891 * str is recomputed.
Daniel Veillard5099ae81999-04-21 20:12:07 +00003892 */
3893void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003894xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) {
Daniel Veillardcf461992000-03-14 18:30:20 +00003895 int needSize;
Daniel Veillard5099ae81999-04-21 20:12:07 +00003896
3897 if (str == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003898#ifdef DEBUG_BUFFER
Daniel Veillard5099ae81999-04-21 20:12:07 +00003899 fprintf(stderr, "xmlBufferAdd: str == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003900#endif
Daniel Veillard5099ae81999-04-21 20:12:07 +00003901 return;
3902 }
Daniel Veillard10a2c651999-12-12 13:03:50 +00003903 if (len < -1) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003904#ifdef DEBUG_BUFFER
Daniel Veillard10a2c651999-12-12 13:03:50 +00003905 fprintf(stderr, "xmlBufferAdd: len < 0\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003906#endif
Daniel Veillard10a2c651999-12-12 13:03:50 +00003907 return;
3908 }
3909 if (len == 0) return;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003910
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003911 if (len < 0)
Daniel Veillardcf461992000-03-14 18:30:20 +00003912 len = xmlStrlen(str);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003913
Daniel Veillarde2d034d1999-07-27 19:52:06 +00003914 if (len <= 0) return;
Daniel Veillard5099ae81999-04-21 20:12:07 +00003915
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003916 needSize = buf->use + len + 2;
3917 if(needSize > buf->size){
3918 if(!xmlBufferResize(buf, needSize)){
3919 fprintf(stderr, "xmlBufferAdd : out of memory!\n");
3920 return;
3921 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00003922 }
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003923
3924 memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
Daniel Veillarde2d034d1999-07-27 19:52:06 +00003925 buf->use += len;
3926 buf->content[buf->use] = 0;
Daniel Veillard5099ae81999-04-21 20:12:07 +00003927}
3928
3929/**
3930 * xmlBufferCat:
3931 * @buf: the buffer to dump
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003932 * @str: the xmlChar string
Daniel Veillard5099ae81999-04-21 20:12:07 +00003933 *
3934 * Append a zero terminated string to an XML buffer.
3935 */
3936void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003937xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003938 if (str != NULL)
3939 xmlBufferAdd(buf, str, -1);
Daniel Veillard5099ae81999-04-21 20:12:07 +00003940}
3941
3942/**
3943 * xmlBufferCCat:
3944 * @buf: the buffer to dump
3945 * @str: the C char string
3946 *
3947 * Append a zero terminated C string to an XML buffer.
3948 */
3949void
3950xmlBufferCCat(xmlBufferPtr buf, const char *str) {
3951 const char *cur;
3952
3953 if (str == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003954#ifdef DEBUG_BUFFER
Daniel Veillard5099ae81999-04-21 20:12:07 +00003955 fprintf(stderr, "xmlBufferAdd: str == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003956#endif
Daniel Veillard5099ae81999-04-21 20:12:07 +00003957 return;
3958 }
3959 for (cur = str;*cur != 0;cur++) {
3960 if (buf->use + 10 >= buf->size) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003961 if(!xmlBufferResize(buf, buf->use+10)){
3962 fprintf(stderr, "xmlBufferCCat : out of memory!\n");
3963 return;
3964 }
3965 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00003966 buf->content[buf->use++] = *cur;
3967 }
3968}
Daniel Veillard260a68f1998-08-13 03:39:55 +00003969
Daniel Veillard97b58771998-10-20 06:14:16 +00003970/**
3971 * xmlBufferWriteCHAR:
Daniel Veillard5099ae81999-04-21 20:12:07 +00003972 * @buf: the XML buffer
Daniel Veillard97b58771998-10-20 06:14:16 +00003973 * @string: the string to add
3974 *
3975 * routine which manage and grows an output buffer. This one add
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003976 * xmlChars at the end of the buffer.
Daniel Veillard97b58771998-10-20 06:14:16 +00003977 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003978void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003979xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00003980 xmlBufferCat(buf, string);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003981}
3982
Daniel Veillard97b58771998-10-20 06:14:16 +00003983/**
3984 * xmlBufferWriteChar:
Daniel Veillard011b63c1999-06-02 17:44:04 +00003985 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00003986 * @string: the string to add
3987 *
3988 * routine which manage and grows an output buffer. This one add
3989 * C chars at the end of the array.
3990 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003991void
Daniel Veillard5099ae81999-04-21 20:12:07 +00003992xmlBufferWriteChar(xmlBufferPtr buf, const char *string) {
3993 xmlBufferCCat(buf, string);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003994}
3995
Daniel Veillard5099ae81999-04-21 20:12:07 +00003996
Daniel Veillard97b58771998-10-20 06:14:16 +00003997/**
Daniel Veillard011b63c1999-06-02 17:44:04 +00003998 * xmlBufferWriteQuotedString:
3999 * @buf: the XML buffer output
4000 * @string: the string to add
4001 *
4002 * routine which manage and grows an output buffer. This one writes
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004003 * a quoted or double quoted xmlChar string, checking first if it holds
Daniel Veillard011b63c1999-06-02 17:44:04 +00004004 * quote or double-quotes internally
4005 */
4006void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004007xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) {
Daniel Veillardb96e6431999-08-29 21:02:19 +00004008 if (xmlStrchr(string, '"')) {
4009 if (xmlStrchr(string, '\'')) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004010#ifdef DEBUG_BUFFER
Daniel Veillard011b63c1999-06-02 17:44:04 +00004011 fprintf(stderr,
4012 "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004013#endif
Daniel Veillard011b63c1999-06-02 17:44:04 +00004014 }
4015 xmlBufferCCat(buf, "'");
4016 xmlBufferCat(buf, string);
4017 xmlBufferCCat(buf, "'");
4018 } else {
4019 xmlBufferCCat(buf, "\"");
4020 xmlBufferCat(buf, string);
4021 xmlBufferCCat(buf, "\"");
4022 }
4023}
4024
4025
Daniel Veillardcf461992000-03-14 18:30:20 +00004026static void
4027xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
4028 int format);
4029static void
4030xmlNodeListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
4031 int format);
4032void
4033htmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur);
4034
Daniel Veillard011b63c1999-06-02 17:44:04 +00004035/**
Daniel Veillard97b58771998-10-20 06:14:16 +00004036 * xmlGlobalNsDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00004037 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00004038 * @cur: a namespace
4039 *
4040 * Dump a global Namespace, this is the old version based on PIs.
Daniel Veillard260a68f1998-08-13 03:39:55 +00004041 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004042static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00004043xmlGlobalNsDump(xmlBufferPtr buf, xmlNsPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00004044 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004045#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00004046 fprintf(stderr, "xmlGlobalNsDump : Ns == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004047#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00004048 return;
4049 }
4050 if (cur->type == XML_GLOBAL_NAMESPACE) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004051 xmlBufferWriteChar(buf, "<?namespace");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004052 if (cur->href != NULL) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00004053 xmlBufferWriteChar(buf, " href=");
4054 xmlBufferWriteQuotedString(buf, cur->href);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004055 }
4056 if (cur->prefix != NULL) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00004057 xmlBufferWriteChar(buf, " AS=");
4058 xmlBufferWriteQuotedString(buf, cur->prefix);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004059 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004060 xmlBufferWriteChar(buf, "?>\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004061 }
4062}
4063
Daniel Veillard97b58771998-10-20 06:14:16 +00004064/**
4065 * xmlGlobalNsListDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00004066 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00004067 * @cur: the first namespace
4068 *
4069 * Dump a list of global Namespace, this is the old version based on PIs.
Daniel Veillard260a68f1998-08-13 03:39:55 +00004070 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004071static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00004072xmlGlobalNsListDump(xmlBufferPtr buf, xmlNsPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00004073 while (cur != NULL) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004074 xmlGlobalNsDump(buf, cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004075 cur = cur->next;
4076 }
4077}
4078
Daniel Veillard97b58771998-10-20 06:14:16 +00004079/**
4080 * xmlNsDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00004081 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00004082 * @cur: a namespace
4083 *
Daniel Veillard260a68f1998-08-13 03:39:55 +00004084 * Dump a local Namespace definition.
Daniel Veillard97b58771998-10-20 06:14:16 +00004085 * Should be called in the context of attributes dumps.
Daniel Veillard260a68f1998-08-13 03:39:55 +00004086 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004087static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00004088xmlNsDump(xmlBufferPtr buf, xmlNsPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00004089 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004090#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00004091 fprintf(stderr, "xmlNsDump : Ns == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004092#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00004093 return;
4094 }
4095 if (cur->type == XML_LOCAL_NAMESPACE) {
4096 /* Within the context of an element attributes */
4097 if (cur->prefix != NULL) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004098 xmlBufferWriteChar(buf, " xmlns:");
4099 xmlBufferWriteCHAR(buf, cur->prefix);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004100 } else
Daniel Veillard5099ae81999-04-21 20:12:07 +00004101 xmlBufferWriteChar(buf, " xmlns");
Daniel Veillard011b63c1999-06-02 17:44:04 +00004102 xmlBufferWriteChar(buf, "=");
4103 xmlBufferWriteQuotedString(buf, cur->href);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004104 }
4105}
4106
Daniel Veillard97b58771998-10-20 06:14:16 +00004107/**
4108 * xmlNsListDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00004109 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00004110 * @cur: the first namespace
4111 *
4112 * Dump a list of local Namespace definitions.
4113 * Should be called in the context of attributes dumps.
Daniel Veillard260a68f1998-08-13 03:39:55 +00004114 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004115static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00004116xmlNsListDump(xmlBufferPtr buf, xmlNsPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00004117 while (cur != NULL) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004118 xmlNsDump(buf, cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004119 cur = cur->next;
4120 }
4121}
4122
Daniel Veillard97b58771998-10-20 06:14:16 +00004123/**
4124 * xmlDtdDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00004125 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00004126 * @doc: the document
4127 *
4128 * Dump the XML document DTD, if any.
Daniel Veillard260a68f1998-08-13 03:39:55 +00004129 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004130static void
Daniel Veillardcf461992000-03-14 18:30:20 +00004131xmlDtdDump(xmlBufferPtr buf, xmlDtdPtr dtd) {
4132 if (dtd == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004133#ifdef DEBUG_TREE
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00004134 fprintf(stderr, "xmlDtdDump : no internal subset\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004135#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00004136 return;
4137 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004138 xmlBufferWriteChar(buf, "<!DOCTYPE ");
Daniel Veillardcf461992000-03-14 18:30:20 +00004139 xmlBufferWriteCHAR(buf, dtd->name);
4140 if (dtd->ExternalID != NULL) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00004141 xmlBufferWriteChar(buf, " PUBLIC ");
Daniel Veillardcf461992000-03-14 18:30:20 +00004142 xmlBufferWriteQuotedString(buf, dtd->ExternalID);
Daniel Veillard011b63c1999-06-02 17:44:04 +00004143 xmlBufferWriteChar(buf, " ");
Daniel Veillardcf461992000-03-14 18:30:20 +00004144 xmlBufferWriteQuotedString(buf, dtd->SystemID);
4145 } else if (dtd->SystemID != NULL) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00004146 xmlBufferWriteChar(buf, " SYSTEM ");
Daniel Veillardcf461992000-03-14 18:30:20 +00004147 xmlBufferWriteQuotedString(buf, dtd->SystemID);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004148 }
Daniel Veillardcf461992000-03-14 18:30:20 +00004149 if ((dtd->entities == NULL) && (dtd->elements == NULL) &&
4150 (dtd->attributes == NULL) && (dtd->notations == NULL)) {
4151 xmlBufferWriteChar(buf, ">");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004152 return;
4153 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004154 xmlBufferWriteChar(buf, " [\n");
Daniel Veillardcf461992000-03-14 18:30:20 +00004155 xmlNodeListDump(buf, dtd->doc, dtd->children, -1, 0);
4156#if 0
4157 if (dtd->entities != NULL)
4158 xmlDumpEntitiesTable(buf, (xmlEntitiesTablePtr) dtd->entities);
4159 if (dtd->notations != NULL)
4160 xmlDumpNotationTable(buf, (xmlNotationTablePtr) dtd->notations);
4161 if (dtd->elements != NULL)
4162 xmlDumpElementTable(buf, (xmlElementTablePtr) dtd->elements);
4163 if (dtd->attributes != NULL)
4164 xmlDumpAttributeTable(buf, (xmlAttributeTablePtr) dtd->attributes);
4165#endif
4166 xmlBufferWriteChar(buf, "]>");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004167}
4168
Daniel Veillard97b58771998-10-20 06:14:16 +00004169/**
4170 * xmlAttrDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00004171 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00004172 * @doc: the document
4173 * @cur: the attribute pointer
4174 *
4175 * Dump an XML attribute
Daniel Veillard260a68f1998-08-13 03:39:55 +00004176 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004177static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00004178xmlAttrDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) {
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004179 xmlChar *value;
Daniel Veillardccb09631998-10-27 06:21:04 +00004180
Daniel Veillard260a68f1998-08-13 03:39:55 +00004181 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004182#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00004183 fprintf(stderr, "xmlAttrDump : property == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004184#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00004185 return;
4186 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004187 xmlBufferWriteChar(buf, " ");
Daniel Veillardb96e6431999-08-29 21:02:19 +00004188 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
4189 xmlBufferWriteCHAR(buf, cur->ns->prefix);
4190 xmlBufferWriteChar(buf, ":");
4191 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004192 xmlBufferWriteCHAR(buf, cur->name);
Daniel Veillardcf461992000-03-14 18:30:20 +00004193 value = xmlNodeListGetString(doc, cur->children, 0);
Daniel Veillardccb09631998-10-27 06:21:04 +00004194 if (value) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00004195 xmlBufferWriteChar(buf, "=");
4196 xmlBufferWriteQuotedString(buf, value);
Daniel Veillard6454aec1999-09-02 22:04:43 +00004197 xmlFree(value);
Daniel Veillard726c7e31999-02-08 15:13:10 +00004198 } else {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004199 xmlBufferWriteChar(buf, "=\"\"");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004200 }
4201}
4202
Daniel Veillard97b58771998-10-20 06:14:16 +00004203/**
4204 * xmlAttrListDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00004205 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00004206 * @doc: the document
4207 * @cur: the first attribute pointer
4208 *
4209 * Dump a list of XML attributes
Daniel Veillard260a68f1998-08-13 03:39:55 +00004210 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004211static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00004212xmlAttrListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00004213 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004214#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00004215 fprintf(stderr, "xmlAttrListDump : property == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004216#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00004217 return;
4218 }
4219 while (cur != NULL) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004220 xmlAttrDump(buf, doc, cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004221 cur = cur->next;
4222 }
4223}
4224
Daniel Veillard260a68f1998-08-13 03:39:55 +00004225
Daniel Veillarddbfd6411999-12-28 16:35:14 +00004226
Daniel Veillard97b58771998-10-20 06:14:16 +00004227/**
4228 * xmlNodeListDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00004229 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00004230 * @doc: the document
4231 * @cur: the first node
Daniel Veillardcf461992000-03-14 18:30:20 +00004232 * @level: the imbrication level for indenting
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004233 * @format: is formatting allowed
Daniel Veillard97b58771998-10-20 06:14:16 +00004234 *
4235 * Dump an XML node list, recursive behaviour,children are printed too.
4236 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004237static void
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004238xmlNodeListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
4239 int format) {
4240 int i;
Daniel Veillardccb09631998-10-27 06:21:04 +00004241
Daniel Veillard260a68f1998-08-13 03:39:55 +00004242 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004243#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00004244 fprintf(stderr, "xmlNodeListDump : node == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004245#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00004246 return;
4247 }
4248 while (cur != NULL) {
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004249 if ((format) && (xmlIndentTreeOutput) &&
4250 (cur->type == XML_ELEMENT_NODE))
4251 for (i = 0;i < level;i++)
4252 xmlBufferWriteChar(buf, " ");
4253 xmlNodeDump(buf, doc, cur, level, format);
4254 if (format) {
4255 xmlBufferWriteChar(buf, "\n");
Daniel Veillardccb09631998-10-27 06:21:04 +00004256 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00004257 cur = cur->next;
4258 }
4259}
4260
Daniel Veillard97b58771998-10-20 06:14:16 +00004261/**
Daniel Veillardccb09631998-10-27 06:21:04 +00004262 * xmlNodeDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00004263 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00004264 * @doc: the document
4265 * @cur: the current node
Daniel Veillardcf461992000-03-14 18:30:20 +00004266 * @level: the imbrication level for indenting
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004267 * @format: is formatting allowed
Daniel Veillard97b58771998-10-20 06:14:16 +00004268 *
4269 * Dump an XML node, recursive behaviour,children are printed too.
Daniel Veillard260a68f1998-08-13 03:39:55 +00004270 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004271static void
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004272xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
4273 int format) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00004274 int i;
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004275 xmlNodePtr tmp;
Daniel Veillard260a68f1998-08-13 03:39:55 +00004276
4277 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004278#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00004279 fprintf(stderr, "xmlNodeDump : node == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004280#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00004281 return;
4282 }
Daniel Veillardcf461992000-03-14 18:30:20 +00004283 if (cur->type == XML_DTD_NODE) {
4284 xmlDtdDump(buf, (xmlDtdPtr) cur);
4285 return;
4286 }
4287 if (cur->type == XML_ELEMENT_DECL) {
4288 xmlDumpElementDecl(buf, (xmlElementPtr) cur);
4289 return;
4290 }
4291 if (cur->type == XML_ATTRIBUTE_DECL) {
4292 xmlDumpAttributeDecl(buf, (xmlAttributePtr) cur);
4293 return;
4294 }
4295 if (cur->type == XML_ENTITY_DECL) {
4296 xmlDumpEntityDecl(buf, (xmlEntityPtr) cur);
4297 return;
4298 }
Daniel Veillard0bef1311998-10-14 02:36:47 +00004299 if (cur->type == XML_TEXT_NODE) {
Daniel Veillard14fff061999-06-22 21:49:07 +00004300 if (cur->content != NULL) {
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004301 xmlChar *buffer;
Daniel Veillard14fff061999-06-22 21:49:07 +00004302
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004303#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard14fff061999-06-22 21:49:07 +00004304 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004305#else
4306 buffer = xmlEncodeEntitiesReentrant(doc,
4307 xmlBufferContent(cur->content));
4308#endif
Daniel Veillard14fff061999-06-22 21:49:07 +00004309 if (buffer != NULL) {
4310 xmlBufferWriteCHAR(buf, buffer);
Daniel Veillard6454aec1999-09-02 22:04:43 +00004311 xmlFree(buffer);
Daniel Veillard14fff061999-06-22 21:49:07 +00004312 }
4313 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00004314 return;
4315 }
Daniel Veillardb96e6431999-08-29 21:02:19 +00004316 if (cur->type == XML_PI_NODE) {
4317 if (cur->content != NULL) {
4318 xmlBufferWriteChar(buf, "<?");
4319 xmlBufferWriteCHAR(buf, cur->name);
4320 if (cur->content != NULL) {
4321 xmlBufferWriteChar(buf, " ");
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004322#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardb96e6431999-08-29 21:02:19 +00004323 xmlBufferWriteCHAR(buf, cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004324#else
4325 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
4326#endif
Daniel Veillardb96e6431999-08-29 21:02:19 +00004327 }
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004328 xmlBufferWriteChar(buf, "?>");
Daniel Veillardcf461992000-03-14 18:30:20 +00004329 } else {
4330 xmlBufferWriteChar(buf, "<?");
4331 xmlBufferWriteCHAR(buf, cur->name);
4332 xmlBufferWriteChar(buf, "?>");
Daniel Veillardb96e6431999-08-29 21:02:19 +00004333 }
4334 return;
4335 }
Daniel Veillard0bef1311998-10-14 02:36:47 +00004336 if (cur->type == XML_COMMENT_NODE) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00004337 if (cur->content != NULL) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004338 xmlBufferWriteChar(buf, "<!--");
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004339#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard5099ae81999-04-21 20:12:07 +00004340 xmlBufferWriteCHAR(buf, cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004341#else
4342 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
4343#endif
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004344 xmlBufferWriteChar(buf, "-->");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004345 }
4346 return;
4347 }
Daniel Veillardccb09631998-10-27 06:21:04 +00004348 if (cur->type == XML_ENTITY_REF_NODE) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004349 xmlBufferWriteChar(buf, "&");
4350 xmlBufferWriteCHAR(buf, cur->name);
4351 xmlBufferWriteChar(buf, ";");
Daniel Veillardccb09631998-10-27 06:21:04 +00004352 return;
4353 }
Daniel Veillardb05deb71999-08-10 19:04:08 +00004354 if (cur->type == XML_CDATA_SECTION_NODE) {
4355 xmlBufferWriteChar(buf, "<![CDATA[");
4356 if (cur->content != NULL)
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004357#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardb05deb71999-08-10 19:04:08 +00004358 xmlBufferWriteCHAR(buf, cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004359#else
4360 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
4361#endif
Daniel Veillardb05deb71999-08-10 19:04:08 +00004362 xmlBufferWriteChar(buf, "]]>");
4363 return;
4364 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00004365
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004366 if (format == 1) {
Daniel Veillardcf461992000-03-14 18:30:20 +00004367 tmp = cur->children;
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004368 while (tmp != NULL) {
4369 if ((tmp->type == XML_TEXT_NODE) ||
4370 (tmp->type == XML_ENTITY_REF_NODE)) {
4371 format = 0;
4372 break;
4373 }
4374 tmp = tmp->next;
4375 }
4376 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004377 xmlBufferWriteChar(buf, "<");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004378 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004379 xmlBufferWriteCHAR(buf, cur->ns->prefix);
4380 xmlBufferWriteChar(buf, ":");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004381 }
4382
Daniel Veillard5099ae81999-04-21 20:12:07 +00004383 xmlBufferWriteCHAR(buf, cur->name);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004384 if (cur->nsDef)
Daniel Veillard5099ae81999-04-21 20:12:07 +00004385 xmlNsListDump(buf, cur->nsDef);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004386 if (cur->properties != NULL)
Daniel Veillard5099ae81999-04-21 20:12:07 +00004387 xmlAttrListDump(buf, doc, cur->properties);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004388
Daniel Veillardcf461992000-03-14 18:30:20 +00004389 if ((cur->content == NULL) && (cur->children == NULL) &&
Daniel Veillarde41f2b72000-01-30 20:00:07 +00004390 (!xmlSaveNoEmptyTags)) {
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004391 xmlBufferWriteChar(buf, "/>");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004392 return;
4393 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004394 xmlBufferWriteChar(buf, ">");
Daniel Veillard14fff061999-06-22 21:49:07 +00004395 if (cur->content != NULL) {
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004396 xmlChar *buffer;
Daniel Veillard14fff061999-06-22 21:49:07 +00004397
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004398#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard14fff061999-06-22 21:49:07 +00004399 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004400#else
4401 buffer = xmlEncodeEntitiesReentrant(doc,
4402 xmlBufferContent(cur->content));
4403#endif
Daniel Veillard14fff061999-06-22 21:49:07 +00004404 if (buffer != NULL) {
4405 xmlBufferWriteCHAR(buf, buffer);
Daniel Veillard6454aec1999-09-02 22:04:43 +00004406 xmlFree(buffer);
Daniel Veillard14fff061999-06-22 21:49:07 +00004407 }
4408 }
Daniel Veillardcf461992000-03-14 18:30:20 +00004409 if (cur->children != NULL) {
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004410 if (format) xmlBufferWriteChar(buf, "\n");
Daniel Veillardcf461992000-03-14 18:30:20 +00004411 xmlNodeListDump(buf, doc, cur->children,
Daniel Veillard3e6d2372000-03-04 11:39:43 +00004412 (level >= 0?level+1:-1), format);
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004413 if ((xmlIndentTreeOutput) && (format))
4414 for (i = 0;i < level;i++)
4415 xmlBufferWriteChar(buf, " ");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004416 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004417 xmlBufferWriteChar(buf, "</");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004418 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004419 xmlBufferWriteCHAR(buf, cur->ns->prefix);
4420 xmlBufferWriteChar(buf, ":");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004421 }
4422
Daniel Veillard5099ae81999-04-21 20:12:07 +00004423 xmlBufferWriteCHAR(buf, cur->name);
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004424 xmlBufferWriteChar(buf, ">");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004425}
4426
Daniel Veillard97b58771998-10-20 06:14:16 +00004427/**
Daniel Veillarddbfd6411999-12-28 16:35:14 +00004428 * xmlElemDump:
4429 * @buf: the XML buffer output
4430 * @doc: the document
4431 * @cur: the current node
4432 *
4433 * Dump an XML/HTML node, recursive behaviour,children are printed too.
4434 */
4435void
4436xmlElemDump(FILE *f, xmlDocPtr doc, xmlNodePtr cur) {
4437 xmlBufferPtr buf;
4438
4439 if (cur == NULL) {
4440#ifdef DEBUG_TREE
4441 fprintf(stderr, "xmlElemDump : cur == NULL\n");
4442#endif
4443 return;
4444 }
4445 if (doc == NULL) {
4446#ifdef DEBUG_TREE
4447 fprintf(stderr, "xmlElemDump : doc == NULL\n");
4448#endif
4449 }
4450 buf = xmlBufferCreate();
4451 if (buf == NULL) return;
4452 if ((doc != NULL) &&
4453 (doc->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard361d8452000-04-03 19:48:13 +00004454#ifdef LIBXML_HTML_ENABLED
Daniel Veillarddbfd6411999-12-28 16:35:14 +00004455 htmlNodeDump(buf, doc, cur);
Daniel Veillard361d8452000-04-03 19:48:13 +00004456#else
4457 printf("HTML support not compiled in\n");
4458#endif /* LIBXML_HTML_ENABLED */
Daniel Veillarddbfd6411999-12-28 16:35:14 +00004459 } else
4460 xmlNodeDump(buf, doc, cur, 0, 1);
4461 xmlBufferDump(f, buf);
4462 xmlBufferFree(buf);
4463}
4464
4465/**
Daniel Veillard97b58771998-10-20 06:14:16 +00004466 * xmlDocContentDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00004467 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00004468 * @cur: the document
4469 *
4470 * Dump an XML document.
Daniel Veillard260a68f1998-08-13 03:39:55 +00004471 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004472static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00004473xmlDocContentDump(xmlBufferPtr buf, xmlDocPtr cur) {
Daniel Veillardbe70ff71999-07-05 16:50:46 +00004474 xmlBufferWriteChar(buf, "<?xml version=");
4475 if (cur->version != NULL)
4476 xmlBufferWriteQuotedString(buf, cur->version);
4477 else
4478 xmlBufferWriteChar(buf, "\"1.0\"");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004479 if (cur->encoding != NULL) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00004480 xmlBufferWriteChar(buf, " encoding=");
4481 xmlBufferWriteQuotedString(buf, cur->encoding);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004482 }
4483 switch (cur->standalone) {
4484 case 0:
Daniel Veillard5099ae81999-04-21 20:12:07 +00004485 xmlBufferWriteChar(buf, " standalone=\"no\"");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004486 break;
4487 case 1:
Daniel Veillard5099ae81999-04-21 20:12:07 +00004488 xmlBufferWriteChar(buf, " standalone=\"yes\"");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004489 break;
4490 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004491 xmlBufferWriteChar(buf, "?>\n");
Daniel Veillardcf461992000-03-14 18:30:20 +00004492 if (cur->children != NULL) {
4493 xmlNodePtr child = cur->children;
Daniel Veillardb96e6431999-08-29 21:02:19 +00004494
Daniel Veillard260a68f1998-08-13 03:39:55 +00004495 /* global namespace definitions, the old way */
4496 if (oldXMLWDcompatibility)
Daniel Veillard5099ae81999-04-21 20:12:07 +00004497 xmlGlobalNsListDump(buf, cur->oldNs);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004498 else
4499 xmlUpgradeOldNs(cur);
Daniel Veillardb96e6431999-08-29 21:02:19 +00004500
4501 while (child != NULL) {
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004502 xmlNodeDump(buf, cur, child, 0, 1);
4503 xmlBufferWriteChar(buf, "\n");
Daniel Veillardb96e6431999-08-29 21:02:19 +00004504 child = child->next;
4505 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00004506 }
4507}
4508
Daniel Veillard97b58771998-10-20 06:14:16 +00004509/**
4510 * xmlDocDumpMemory:
4511 * @cur: the document
4512 * @mem: OUT: the memory pointer
4513 * @size: OUT: the memory lenght
4514 *
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004515 * Dump an XML document in memory and return the xmlChar * and it's size.
Daniel Veillard97b58771998-10-20 06:14:16 +00004516 * It's up to the caller to free the memory.
Daniel Veillard260a68f1998-08-13 03:39:55 +00004517 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004518void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004519xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004520 xmlBufferPtr buf;
4521
Daniel Veillard260a68f1998-08-13 03:39:55 +00004522 if (cur == NULL) {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00004523#ifdef DEBUG_TREE
4524 fprintf(stderr, "xmlDocDumpMemory : document == NULL\n");
4525#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00004526 *mem = NULL;
4527 *size = 0;
4528 return;
4529 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004530 buf = xmlBufferCreate();
4531 if (buf == NULL) {
4532 *mem = NULL;
4533 *size = 0;
4534 return;
4535 }
4536 xmlDocContentDump(buf, cur);
Daniel Veillardb05deb71999-08-10 19:04:08 +00004537 *mem = xmlStrndup(buf->content, buf->use);
Daniel Veillard5099ae81999-04-21 20:12:07 +00004538 *size = buf->use;
Daniel Veillardb05deb71999-08-10 19:04:08 +00004539 xmlBufferFree(buf);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004540}
4541
Daniel Veillard97b58771998-10-20 06:14:16 +00004542/**
4543 * xmlGetDocCompressMode:
4544 * @doc: the document
4545 *
4546 * get the compression ratio for a document, ZLIB based
Daniel Veillard1e346af1999-02-22 10:33:01 +00004547 * Returns 0 (uncompressed) to 9 (max compression)
Daniel Veillard151b1b01998-09-23 00:49:46 +00004548 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004549int
Daniel Veillarddbfd6411999-12-28 16:35:14 +00004550xmlGetDocCompressMode (xmlDocPtr doc) {
Daniel Veillard15a8df41998-09-24 19:15:06 +00004551 if (doc == NULL) return(-1);
4552 return(doc->compression);
4553}
4554
Daniel Veillard97b58771998-10-20 06:14:16 +00004555/**
4556 * xmlSetDocCompressMode:
4557 * @doc: the document
4558 * @mode: the compression ratio
4559 *
4560 * set the compression ratio for a document, ZLIB based
4561 * Correct values: 0 (uncompressed) to 9 (max compression)
4562 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004563void
4564xmlSetDocCompressMode (xmlDocPtr doc, int mode) {
Daniel Veillard15a8df41998-09-24 19:15:06 +00004565 if (doc == NULL) return;
4566 if (mode < 0) doc->compression = 0;
4567 else if (mode > 9) doc->compression = 9;
4568 else doc->compression = mode;
4569}
4570
Daniel Veillard97b58771998-10-20 06:14:16 +00004571/**
4572 * xmlGetCompressMode:
4573 *
4574 * get the default compression mode used, ZLIB based.
Daniel Veillard1e346af1999-02-22 10:33:01 +00004575 * Returns 0 (uncompressed) to 9 (max compression)
Daniel Veillard15a8df41998-09-24 19:15:06 +00004576 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004577int
4578 xmlGetCompressMode(void) {
Daniel Veillard151b1b01998-09-23 00:49:46 +00004579 return(xmlCompressMode);
4580}
Daniel Veillard97b58771998-10-20 06:14:16 +00004581
4582/**
4583 * xmlSetCompressMode:
4584 * @mode: the compression ratio
4585 *
4586 * set the default compression mode used, ZLIB based
4587 * Correct values: 0 (uncompressed) to 9 (max compression)
4588 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004589void
4590xmlSetCompressMode(int mode) {
Daniel Veillard151b1b01998-09-23 00:49:46 +00004591 if (mode < 0) xmlCompressMode = 0;
Daniel Veillard15a8df41998-09-24 19:15:06 +00004592 else if (mode > 9) xmlCompressMode = 9;
Daniel Veillard151b1b01998-09-23 00:49:46 +00004593 else xmlCompressMode = mode;
4594}
4595
Daniel Veillard97b58771998-10-20 06:14:16 +00004596/**
4597 * xmlDocDump:
4598 * @f: the FILE*
4599 * @cur: the document
4600 *
4601 * Dump an XML document to an open FILE.
Daniel Veillard260a68f1998-08-13 03:39:55 +00004602 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004603void
4604xmlDocDump(FILE *f, xmlDocPtr cur) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004605 xmlBufferPtr buf;
4606
Daniel Veillard260a68f1998-08-13 03:39:55 +00004607 if (cur == NULL) {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00004608#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00004609 fprintf(stderr, "xmlDocDump : document == NULL\n");
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00004610#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00004611 return;
4612 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004613 buf = xmlBufferCreate();
4614 if (buf == NULL) return;
4615 xmlDocContentDump(buf, cur);
4616 xmlBufferDump(f, buf);
4617 xmlBufferFree(buf);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004618}
4619
Daniel Veillard97b58771998-10-20 06:14:16 +00004620/**
4621 * xmlSaveFile:
4622 * @filename: the filename
4623 * @cur: the document
4624 *
4625 * Dump an XML document to a file. Will use compression if
Daniel Veillard11a48ec1999-11-23 10:40:46 +00004626 * compiled in and enabled. If @filename is "-" the stdout file is
4627 * used.
Daniel Veillard97b58771998-10-20 06:14:16 +00004628 * returns: the number of file written or -1 in case of failure.
Daniel Veillard151b1b01998-09-23 00:49:46 +00004629 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004630int
4631xmlSaveFile(const char *filename, xmlDocPtr cur) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004632 xmlBufferPtr buf;
Daniel Veillard151b1b01998-09-23 00:49:46 +00004633#ifdef HAVE_ZLIB_H
4634 gzFile zoutput = NULL;
4635 char mode[15];
4636#endif
Daniel Veillardccb09631998-10-27 06:21:04 +00004637 FILE *output = NULL;
Daniel Veillard151b1b01998-09-23 00:49:46 +00004638 int ret;
4639
Daniel Veillard5099ae81999-04-21 20:12:07 +00004640 /*
4641 * save the content to a temp buffer.
4642 */
4643 buf = xmlBufferCreate();
4644 if (buf == NULL) return(0);
4645 xmlDocContentDump(buf, cur);
4646
Daniel Veillard151b1b01998-09-23 00:49:46 +00004647#ifdef HAVE_ZLIB_H
Daniel Veillard11a48ec1999-11-23 10:40:46 +00004648 if (cur->compression < 0) cur->compression = xmlCompressMode;
Daniel Veillarddc3dd9d1998-09-24 19:25:54 +00004649 if ((cur->compression > 0) && (cur->compression <= 9)) {
4650 sprintf(mode, "w%d", cur->compression);
Daniel Veillard11a48ec1999-11-23 10:40:46 +00004651 if (!strcmp(filename, "-"))
4652 zoutput = gzdopen(1, mode);
4653 else
4654 zoutput = gzopen(filename, mode);
Daniel Veillard151b1b01998-09-23 00:49:46 +00004655 }
4656 if (zoutput == NULL) {
4657#endif
4658 output = fopen(filename, "w");
Daniel Veillard10a2c651999-12-12 13:03:50 +00004659 if (output == NULL) {
4660 xmlBufferFree(buf);
4661 return(-1);
4662 }
Daniel Veillard151b1b01998-09-23 00:49:46 +00004663#ifdef HAVE_ZLIB_H
4664 }
Daniel Veillard151b1b01998-09-23 00:49:46 +00004665
Daniel Veillard151b1b01998-09-23 00:49:46 +00004666 if (zoutput != NULL) {
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004667 ret = gzwrite(zoutput, buf->content, sizeof(xmlChar) * buf->use);
Daniel Veillard151b1b01998-09-23 00:49:46 +00004668 gzclose(zoutput);
Daniel Veillard5099ae81999-04-21 20:12:07 +00004669 } else {
4670#endif
4671 ret = xmlBufferDump(output, buf);
4672 fclose(output);
4673#ifdef HAVE_ZLIB_H
Daniel Veillard151b1b01998-09-23 00:49:46 +00004674 }
4675#endif
Manish Vachharajani5e60f5a1999-05-29 03:04:30 +00004676 xmlBufferFree(buf);
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004677 return(ret * sizeof(xmlChar));
Daniel Veillard151b1b01998-09-23 00:49:46 +00004678}
4679