blob: 2cc4b51d8365c0bcfe1b721261166f6dca56be76 [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);
Daniel Veillard06047432000-04-24 11:33:38 +00002285 if (ret == NULL) {
2286 q->prev = NULL;
2287 ret = p = q;
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002288 } else {
Daniel Veillard06047432000-04-24 11:33:38 +00002289 p->next = q;
2290 q->prev = p;
2291 p = q;
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002292 }
2293 node = node->next;
2294 }
2295 return(ret);
2296}
2297
2298/**
2299 * xmlCopyNode:
2300 * @node: the node
2301 * @recursive: if 1 do a recursive copy.
2302 *
2303 * Do a copy of the node.
2304 *
2305 * Returns: a new xmlNodePtr, or NULL in case of error.
2306 */
2307xmlNodePtr
2308xmlCopyNode(xmlNodePtr node, int recursive) {
2309 xmlNodePtr ret;
2310
2311 ret = xmlStaticCopyNode(node, NULL, NULL, recursive);
2312 return(ret);
2313}
2314
2315/**
2316 * xmlCopyNodeList:
2317 * @node: the first node in the list.
2318 *
2319 * Do a recursive copy of the node list.
2320 *
2321 * Returns: a new xmlNodePtr, or NULL in case of error.
2322 */
2323xmlNodePtr xmlCopyNodeList(xmlNodePtr node) {
2324 xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL);
2325 return(ret);
2326}
2327
2328/**
2329 * xmlCopyElement:
2330 * @elem: the element
2331 *
2332 * Do a copy of the element definition.
2333 *
2334 * Returns: a new xmlElementPtr, or NULL in case of error.
2335xmlElementPtr
2336xmlCopyElement(xmlElementPtr elem) {
2337 xmlElementPtr ret;
2338
2339 if (elem == NULL) return(NULL);
2340 ret = xmlNewDocElement(elem->doc, elem->ns, elem->name, elem->content);
2341 if (ret == NULL) return(NULL);
2342 if (!recursive) return(ret);
2343 if (elem->properties != NULL)
2344 ret->properties = xmlCopyPropList(elem->properties);
2345
2346 if (elem->nsDef != NULL)
2347 ret->nsDef = xmlCopyNamespaceList(elem->nsDef);
Daniel Veillardcf461992000-03-14 18:30:20 +00002348 if (elem->children != NULL)
2349 ret->children = xmlCopyElementList(elem->children);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002350 return(ret);
2351}
2352 */
2353
2354/**
2355 * xmlCopyDtd:
2356 * @dtd: the dtd
2357 *
2358 * Do a copy of the dtd.
2359 *
2360 * Returns: a new xmlDtdPtr, or NULL in case of error.
2361 */
2362xmlDtdPtr
2363xmlCopyDtd(xmlDtdPtr dtd) {
2364 xmlDtdPtr ret;
2365
2366 if (dtd == NULL) return(NULL);
2367 ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID);
2368 if (ret == NULL) return(NULL);
2369 if (dtd->entities != NULL)
2370 ret->entities = (void *) xmlCopyEntitiesTable(
2371 (xmlEntitiesTablePtr) dtd->entities);
Daniel Veillard1e346af1999-02-22 10:33:01 +00002372 if (dtd->notations != NULL)
2373 ret->notations = (void *) xmlCopyNotationTable(
2374 (xmlNotationTablePtr) dtd->notations);
2375 if (dtd->elements != NULL)
2376 ret->elements = (void *) xmlCopyElementTable(
2377 (xmlElementTablePtr) dtd->elements);
2378 if (dtd->attributes != NULL)
2379 ret->attributes = (void *) xmlCopyAttributeTable(
2380 (xmlAttributeTablePtr) dtd->attributes);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002381 return(ret);
2382}
2383
2384/**
2385 * xmlCopyDoc:
2386 * @doc: the document
2387 * @recursive: if 1 do a recursive copy.
2388 *
2389 * Do a copy of the document info. If recursive, the content tree will
2390 * be copied too as well as Dtd, namespaces and entities.
2391 *
2392 * Returns: a new xmlDocPtr, or NULL in case of error.
2393 */
2394xmlDocPtr
2395xmlCopyDoc(xmlDocPtr doc, int recursive) {
2396 xmlDocPtr ret;
2397
2398 if (doc == NULL) return(NULL);
2399 ret = xmlNewDoc(doc->version);
2400 if (ret == NULL) return(NULL);
2401 if (doc->name != NULL)
Daniel Veillard6454aec1999-09-02 22:04:43 +00002402 ret->name = xmlMemStrdup(doc->name);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002403 if (doc->encoding != NULL)
2404 ret->encoding = xmlStrdup(doc->encoding);
2405 ret->compression = doc->compression;
2406 ret->standalone = doc->standalone;
2407 if (!recursive) return(ret);
2408
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002409 if (doc->intSubset != NULL)
2410 ret->intSubset = xmlCopyDtd(doc->intSubset);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002411 if (doc->oldNs != NULL)
2412 ret->oldNs = xmlCopyNamespaceList(doc->oldNs);
Daniel Veillardcf461992000-03-14 18:30:20 +00002413 if (doc->children != NULL)
Daniel Veillard06047432000-04-24 11:33:38 +00002414 ret->children = xmlStaticCopyNodeList(doc->children, ret,
2415 (xmlNodePtr)ret);
Daniel Veillardbe36afe1998-11-27 06:39:50 +00002416 return(ret);
2417}
2418
2419/************************************************************************
2420 * *
Daniel Veillard260a68f1998-08-13 03:39:55 +00002421 * Content access functions *
2422 * *
2423 ************************************************************************/
2424
Daniel Veillard97b58771998-10-20 06:14:16 +00002425/**
Daniel Veillard944b5ff1999-12-15 19:08:24 +00002426 * xmlDocGetRootElement:
2427 * @doc: the document
2428 *
Daniel Veillardcf461992000-03-14 18:30:20 +00002429 * Get the root element of the document (doc->children is a list
Daniel Veillard944b5ff1999-12-15 19:08:24 +00002430 * containing possibly comments, PIs, etc ...).
2431 *
2432 * Returns the xmlNodePtr for the root or NULL
2433 */
2434xmlNodePtr
2435xmlDocGetRootElement(xmlDocPtr doc) {
2436 xmlNodePtr ret;
2437
2438 if (doc == NULL) return(NULL);
Daniel Veillardcf461992000-03-14 18:30:20 +00002439 ret = doc->children;
Daniel Veillard944b5ff1999-12-15 19:08:24 +00002440 while (ret != NULL) {
2441 if (ret->type == XML_ELEMENT_NODE)
2442 return(ret);
2443 ret = ret->next;
2444 }
2445 return(ret);
2446}
2447
2448/**
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002449 * xmlDocSetRootElement:
2450 * @doc: the document
2451 * @root: the new document root element
2452 *
Daniel Veillardcf461992000-03-14 18:30:20 +00002453 * Set the root element of the document (doc->children is a list
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002454 * containing possibly comments, PIs, etc ...).
2455 *
2456 * Returns the old root element if any was found
2457 */
2458xmlNodePtr
2459xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) {
2460 xmlNodePtr old = NULL;
2461
2462 if (doc == NULL) return(NULL);
Daniel Veillardcf461992000-03-14 18:30:20 +00002463 old = doc->children;
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002464 while (old != NULL) {
2465 if (old->type == XML_ELEMENT_NODE)
2466 break;
2467 old = old->next;
2468 }
2469 if (old == NULL) {
Daniel Veillardcf461992000-03-14 18:30:20 +00002470 if (doc->children == NULL) {
2471 doc->children = root;
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002472 } else {
Daniel Veillardcf461992000-03-14 18:30:20 +00002473 xmlAddSibling(doc->children, root);
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002474 }
2475 } else {
2476 xmlReplaceNode(old, root);
2477 }
2478 return(old);
2479}
2480
2481/**
Daniel Veillardb96e6431999-08-29 21:02:19 +00002482 * xmlNodeSetLang:
2483 * @cur: the node being changed
2484 * @lang: the langage description
2485 *
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002486 * Set the language of a node, i.e. the values of the xml:lang
2487 * attribute.
Daniel Veillardb96e6431999-08-29 21:02:19 +00002488 */
2489void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002490xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) {
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002491 if (cur == NULL) return;
2492 switch(cur->type) {
2493 case XML_TEXT_NODE:
2494 case XML_CDATA_SECTION_NODE:
2495 case XML_COMMENT_NODE:
2496 case XML_DOCUMENT_NODE:
2497 case XML_DOCUMENT_TYPE_NODE:
2498 case XML_DOCUMENT_FRAG_NODE:
2499 case XML_NOTATION_NODE:
2500 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardcf461992000-03-14 18:30:20 +00002501 case XML_DTD_NODE:
2502 case XML_ELEMENT_DECL:
2503 case XML_ATTRIBUTE_DECL:
2504 case XML_ENTITY_DECL:
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002505 return;
2506 case XML_ELEMENT_NODE:
2507 case XML_ATTRIBUTE_NODE:
2508 case XML_PI_NODE:
2509 case XML_ENTITY_REF_NODE:
2510 case XML_ENTITY_NODE:
2511 break;
2512 }
Daniel Veillardb96e6431999-08-29 21:02:19 +00002513 xmlSetProp(cur, BAD_CAST "xml:lang", lang);
2514}
2515
2516/**
2517 * xmlNodeGetLang:
2518 * @cur: the node being checked
2519 *
2520 * Searches the language of a node, i.e. the values of the xml:lang
2521 * attribute or the one carried by the nearest ancestor.
2522 *
2523 * Returns a pointer to the lang value, or NULL if not found
Daniel Veillarda819dac1999-11-24 18:04:22 +00002524 * It's up to the caller to free the memory.
Daniel Veillardb96e6431999-08-29 21:02:19 +00002525 */
Daniel Veillarda819dac1999-11-24 18:04:22 +00002526xmlChar *
Daniel Veillardb96e6431999-08-29 21:02:19 +00002527xmlNodeGetLang(xmlNodePtr cur) {
Daniel Veillarda819dac1999-11-24 18:04:22 +00002528 xmlChar *lang;
Daniel Veillardb96e6431999-08-29 21:02:19 +00002529
2530 while (cur != NULL) {
2531 lang = xmlGetProp(cur, BAD_CAST "xml:lang");
2532 if (lang != NULL)
2533 return(lang);
2534 cur = cur->parent;
2535 }
2536 return(NULL);
2537}
2538
2539/**
Daniel Veillardcf461992000-03-14 18:30:20 +00002540 * xmlNodeGetSpacePreserve:
2541 * @cur: the node being checked
2542 *
2543 * Searches the language of a node, i.e. the values of the xml:space
2544 * attribute or the one carried by the nearest ancestor.
2545 *
2546 * Returns -1 if xml:space is not inheried, 0 if "default", 1 if "preserve"
2547 */
2548int
2549xmlNodeGetSpacePreserve(xmlNodePtr cur) {
2550 xmlChar *space;
2551
2552 while (cur != NULL) {
2553 space = xmlGetProp(cur, BAD_CAST "xml:space");
2554 if (space != NULL) {
2555 if (!xmlStrcmp(space, BAD_CAST "preserve")) {
2556 xmlFree(space);
2557 return(1);
2558 }
2559 if (!xmlStrcmp(space, BAD_CAST "default")) {
2560 xmlFree(space);
2561 return(0);
2562 }
2563 xmlFree(space);
2564 }
2565 cur = cur->parent;
2566 }
2567 return(-1);
2568}
2569
2570/**
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002571 * xmlNodeSetName:
2572 * @cur: the node being changed
2573 * @name: the new tag name
2574 *
2575 * Searches the language of a node, i.e. the values of the xml:lang
2576 * attribute or the one carried by the nearest ancestor.
2577 */
2578void
2579xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) {
2580 if (cur == NULL) return;
2581 if (name == NULL) return;
2582 switch(cur->type) {
2583 case XML_TEXT_NODE:
2584 case XML_CDATA_SECTION_NODE:
2585 case XML_COMMENT_NODE:
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002586 case XML_DOCUMENT_TYPE_NODE:
2587 case XML_DOCUMENT_FRAG_NODE:
2588 case XML_NOTATION_NODE:
2589 case XML_HTML_DOCUMENT_NODE:
2590 return;
2591 case XML_ELEMENT_NODE:
2592 case XML_ATTRIBUTE_NODE:
2593 case XML_PI_NODE:
2594 case XML_ENTITY_REF_NODE:
2595 case XML_ENTITY_NODE:
Daniel Veillardcf461992000-03-14 18:30:20 +00002596 case XML_DTD_NODE:
2597 case XML_DOCUMENT_NODE:
2598 case XML_ELEMENT_DECL:
2599 case XML_ATTRIBUTE_DECL:
2600 case XML_ENTITY_DECL:
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00002601 break;
2602 }
2603 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
2604 cur->name = xmlStrdup(name);
2605}
2606
2607/**
Daniel Veillard10a2c651999-12-12 13:03:50 +00002608 * xmlNodeGetBase:
2609 * @doc: the document the node pertains to
2610 * @cur: the node being checked
2611 *
2612 * Searches for the BASE URL. The code should work on both XML
2613 * and HTML document even if base mechanisms are completely different.
2614 *
2615 * Returns a pointer to the base URL, or NULL if not found
2616 * It's up to the caller to free the memory.
2617 */
2618xmlChar *
2619xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) {
2620 xmlChar *base;
2621
2622 if ((cur == NULL) && (doc == NULL))
2623 return(NULL);
2624 if (doc == NULL) doc = cur->doc;
2625 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillardcf461992000-03-14 18:30:20 +00002626 cur = doc->children;
Daniel Veillard10a2c651999-12-12 13:03:50 +00002627 while ((cur != NULL) && (cur->name != NULL)) {
2628 if (cur->type != XML_ELEMENT_NODE) {
2629 cur = cur->next;
2630 continue;
2631 }
2632 if ((!xmlStrcmp(cur->name, BAD_CAST "html")) ||
2633 (!xmlStrcmp(cur->name, BAD_CAST "HTML"))) {
Daniel Veillardcf461992000-03-14 18:30:20 +00002634 cur = cur->children;
Daniel Veillard10a2c651999-12-12 13:03:50 +00002635 continue;
2636 }
2637 if ((!xmlStrcmp(cur->name, BAD_CAST "head")) ||
2638 (!xmlStrcmp(cur->name, BAD_CAST "HEAD"))) {
Daniel Veillardcf461992000-03-14 18:30:20 +00002639 cur = cur->children;
Daniel Veillard10a2c651999-12-12 13:03:50 +00002640 continue;
2641 }
2642 if ((!xmlStrcmp(cur->name, BAD_CAST "base")) ||
2643 (!xmlStrcmp(cur->name, BAD_CAST "BASE"))) {
2644 base = xmlGetProp(cur, BAD_CAST "href");
2645 if (base != NULL) return(base);
2646 return(xmlGetProp(cur, BAD_CAST "HREF"));
2647 }
Daniel Veillardcf461992000-03-14 18:30:20 +00002648 cur = cur->next;
Daniel Veillard10a2c651999-12-12 13:03:50 +00002649 }
Daniel Veillardcf461992000-03-14 18:30:20 +00002650 if ((doc != NULL) && (doc->URL != NULL))
2651 return(xmlStrdup(doc->URL));
Daniel Veillard10a2c651999-12-12 13:03:50 +00002652 return(NULL);
2653 }
2654 while (cur != NULL) {
2655 base = xmlGetProp(cur, BAD_CAST "xml:base");
2656 if (base != NULL)
2657 return(base);
2658 cur = cur->parent;
2659 }
Daniel Veillardcf461992000-03-14 18:30:20 +00002660 if ((doc != NULL) && (doc->URL != NULL))
2661 return(xmlStrdup(doc->URL));
Daniel Veillard10a2c651999-12-12 13:03:50 +00002662 return(NULL);
2663}
2664
2665/**
Daniel Veillard16253641998-10-28 22:58:05 +00002666 * xmlNodeGetContent:
2667 * @cur: the node being read
2668 *
2669 * Read the value of a node, this can be either the text carried
2670 * directly by this node if it's a TEXT node or the aggregate string
2671 * of the values carried by this node child's (TEXT and ENTITY_REF).
2672 * Entity references are substitued.
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002673 * Returns a new xmlChar * or NULL if no content is available.
Daniel Veillard5099ae81999-04-21 20:12:07 +00002674 * It's up to the caller to free the memory.
Daniel Veillard16253641998-10-28 22:58:05 +00002675 */
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002676xmlChar *
Daniel Veillard16253641998-10-28 22:58:05 +00002677xmlNodeGetContent(xmlNodePtr cur) {
2678 if (cur == NULL) return(NULL);
2679 switch (cur->type) {
2680 case XML_DOCUMENT_FRAG_NODE:
2681 case XML_ELEMENT_NODE:
Daniel Veillardcf461992000-03-14 18:30:20 +00002682 return(xmlNodeListGetString(cur->doc, cur->children, 1));
Daniel Veillard16253641998-10-28 22:58:05 +00002683 break;
Daniel Veillardb96e6431999-08-29 21:02:19 +00002684 case XML_ATTRIBUTE_NODE: {
2685 xmlAttrPtr attr = (xmlAttrPtr) cur;
Daniel Veillardcf461992000-03-14 18:30:20 +00002686 if (attr->parent != NULL)
2687 return(xmlNodeListGetString(attr->parent->doc, attr->children, 1));
Daniel Veillardb96e6431999-08-29 21:02:19 +00002688 else
Daniel Veillardcf461992000-03-14 18:30:20 +00002689 return(xmlNodeListGetString(NULL, attr->children, 1));
Daniel Veillardb96e6431999-08-29 21:02:19 +00002690 break;
2691 }
Daniel Veillarddbfd6411999-12-28 16:35:14 +00002692 case XML_COMMENT_NODE:
Daniel Veillardb96e6431999-08-29 21:02:19 +00002693 case XML_PI_NODE:
2694 if (cur->content != NULL)
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002695#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardb96e6431999-08-29 21:02:19 +00002696 return(xmlStrdup(cur->content));
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002697#else
2698 return(xmlStrdup(xmlBufferContent(cur->content)));
2699#endif
Daniel Veillardb96e6431999-08-29 21:02:19 +00002700 return(NULL);
Daniel Veillard16253641998-10-28 22:58:05 +00002701 case XML_ENTITY_REF_NODE:
Daniel Veillarddbfd6411999-12-28 16:35:14 +00002702 /*
2703 * Locate the entity, and get it's content
2704 * @@@
2705 */
2706 return(NULL);
Daniel Veillard16253641998-10-28 22:58:05 +00002707 case XML_ENTITY_NODE:
Daniel Veillard16253641998-10-28 22:58:05 +00002708 case XML_DOCUMENT_NODE:
Daniel Veillard7c1206f1999-10-14 09:10:25 +00002709 case XML_HTML_DOCUMENT_NODE:
Daniel Veillard16253641998-10-28 22:58:05 +00002710 case XML_DOCUMENT_TYPE_NODE:
2711 case XML_NOTATION_NODE:
Daniel Veillardcf461992000-03-14 18:30:20 +00002712 case XML_DTD_NODE:
2713 return(NULL);
2714 case XML_ELEMENT_DECL:
2715 /* TODO !!! */
2716 return(NULL);
2717 case XML_ATTRIBUTE_DECL:
2718 /* TODO !!! */
2719 return(NULL);
2720 case XML_ENTITY_DECL:
2721 /* TODO !!! */
Daniel Veillard16253641998-10-28 22:58:05 +00002722 return(NULL);
Daniel Veillardb05deb71999-08-10 19:04:08 +00002723 case XML_CDATA_SECTION_NODE:
Daniel Veillard16253641998-10-28 22:58:05 +00002724 case XML_TEXT_NODE:
2725 if (cur->content != NULL)
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002726#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard16253641998-10-28 22:58:05 +00002727 return(xmlStrdup(cur->content));
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002728#else
2729 return(xmlStrdup(xmlBufferContent(cur->content)));
2730#endif
Daniel Veillard16253641998-10-28 22:58:05 +00002731 return(NULL);
2732 }
2733 return(NULL);
2734}
2735
2736/**
Daniel Veillard97b58771998-10-20 06:14:16 +00002737 * xmlNodeSetContent:
2738 * @cur: the node being modified
2739 * @content: the new value of the content
2740 *
2741 * Replace the content of a node.
Daniel Veillard260a68f1998-08-13 03:39:55 +00002742 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00002743void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002744xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00002745 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00002746#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00002747 fprintf(stderr, "xmlNodeSetContent : node == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00002748#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00002749 return;
2750 }
Daniel Veillard16253641998-10-28 22:58:05 +00002751 switch (cur->type) {
2752 case XML_DOCUMENT_FRAG_NODE:
2753 case XML_ELEMENT_NODE:
2754 if (cur->content != NULL) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002755#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard6454aec1999-09-02 22:04:43 +00002756 xmlFree(cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002757#else
2758 xmlBufferFree(cur->content);
2759#endif
Daniel Veillard16253641998-10-28 22:58:05 +00002760 cur->content = NULL;
2761 }
Daniel Veillardcf461992000-03-14 18:30:20 +00002762 if (cur->children != NULL) xmlFreeNodeList(cur->children);
2763 cur->children = xmlStringGetNodeList(cur->doc, content);
Daniel Veillard1e346af1999-02-22 10:33:01 +00002764 UPDATE_LAST_CHILD(cur)
Daniel Veillard16253641998-10-28 22:58:05 +00002765 break;
2766 case XML_ATTRIBUTE_NODE:
2767 break;
2768 case XML_TEXT_NODE:
2769 case XML_CDATA_SECTION_NODE:
2770 case XML_ENTITY_REF_NODE:
2771 case XML_ENTITY_NODE:
2772 case XML_PI_NODE:
2773 case XML_COMMENT_NODE:
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002774 if (cur->content != NULL) {
2775#ifndef XML_USE_BUFFER_CONTENT
2776 xmlFree(cur->content);
2777#else
2778 xmlBufferFree(cur->content);
2779#endif
2780 }
Daniel Veillardcf461992000-03-14 18:30:20 +00002781 if (cur->children != NULL) xmlFreeNodeList(cur->children);
2782 cur->last = cur->children = NULL;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002783 if (content != NULL) {
2784#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard16253641998-10-28 22:58:05 +00002785 cur->content = xmlStrdup(content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002786#else
2787 cur->content = xmlBufferCreateSize(0);
2788 xmlBufferSetAllocationScheme(cur->content,
2789 xmlGetBufferAllocationScheme());
2790 xmlBufferAdd(cur->content, content, -1);
2791#endif
2792 } else
Daniel Veillard16253641998-10-28 22:58:05 +00002793 cur->content = NULL;
Daniel Veillardb96e6431999-08-29 21:02:19 +00002794 break;
Daniel Veillard16253641998-10-28 22:58:05 +00002795 case XML_DOCUMENT_NODE:
Daniel Veillard7c1206f1999-10-14 09:10:25 +00002796 case XML_HTML_DOCUMENT_NODE:
Daniel Veillard16253641998-10-28 22:58:05 +00002797 case XML_DOCUMENT_TYPE_NODE:
2798 break;
2799 case XML_NOTATION_NODE:
2800 break;
Daniel Veillardcf461992000-03-14 18:30:20 +00002801 case XML_DTD_NODE:
2802 break;
2803 case XML_ELEMENT_DECL:
2804 /* TODO !!! */
2805 break;
2806 case XML_ATTRIBUTE_DECL:
2807 /* TODO !!! */
2808 break;
2809 case XML_ENTITY_DECL:
2810 /* TODO !!! */
2811 break;
Daniel Veillard16253641998-10-28 22:58:05 +00002812 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00002813}
2814
Daniel Veillard97b58771998-10-20 06:14:16 +00002815/**
2816 * xmlNodeSetContentLen:
2817 * @cur: the node being modified
2818 * @content: the new value of the content
2819 * @len: the size of @content
2820 *
2821 * Replace the content of a node.
Daniel Veillard260a68f1998-08-13 03:39:55 +00002822 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00002823void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002824xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00002825 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00002826#ifdef DEBUG_TREE
Daniel Veillard16253641998-10-28 22:58:05 +00002827 fprintf(stderr, "xmlNodeSetContentLen : node == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00002828#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00002829 return;
2830 }
Daniel Veillard16253641998-10-28 22:58:05 +00002831 switch (cur->type) {
2832 case XML_DOCUMENT_FRAG_NODE:
2833 case XML_ELEMENT_NODE:
2834 if (cur->content != NULL) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002835#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard6454aec1999-09-02 22:04:43 +00002836 xmlFree(cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002837#else
2838 xmlBufferFree(cur->content);
2839#endif
Daniel Veillard16253641998-10-28 22:58:05 +00002840 cur->content = NULL;
2841 }
Daniel Veillardcf461992000-03-14 18:30:20 +00002842 if (cur->children != NULL) xmlFreeNodeList(cur->children);
2843 cur->children = xmlStringLenGetNodeList(cur->doc, content, len);
Daniel Veillard1e346af1999-02-22 10:33:01 +00002844 UPDATE_LAST_CHILD(cur)
Daniel Veillard16253641998-10-28 22:58:05 +00002845 break;
2846 case XML_ATTRIBUTE_NODE:
2847 break;
2848 case XML_TEXT_NODE:
2849 case XML_CDATA_SECTION_NODE:
2850 case XML_ENTITY_REF_NODE:
2851 case XML_ENTITY_NODE:
2852 case XML_PI_NODE:
2853 case XML_COMMENT_NODE:
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002854 case XML_NOTATION_NODE:
2855 if (cur->content != NULL) {
2856#ifndef XML_USE_BUFFER_CONTENT
2857 xmlFree(cur->content);
2858#else
2859 xmlBufferFree(cur->content);
2860#endif
2861 }
Daniel Veillardcf461992000-03-14 18:30:20 +00002862 if (cur->children != NULL) xmlFreeNodeList(cur->children);
2863 cur->children = cur->last = NULL;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002864 if (content != NULL) {
2865#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard16253641998-10-28 22:58:05 +00002866 cur->content = xmlStrndup(content, len);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002867#else
2868 cur->content = xmlBufferCreateSize(len);
2869 xmlBufferSetAllocationScheme(cur->content,
2870 xmlGetBufferAllocationScheme());
2871 xmlBufferAdd(cur->content, content, len);
2872#endif
2873 } else
Daniel Veillard16253641998-10-28 22:58:05 +00002874 cur->content = NULL;
Daniel Veillardb96e6431999-08-29 21:02:19 +00002875 break;
Daniel Veillard16253641998-10-28 22:58:05 +00002876 case XML_DOCUMENT_NODE:
Daniel Veillardcf461992000-03-14 18:30:20 +00002877 case XML_DTD_NODE:
Daniel Veillard7c1206f1999-10-14 09:10:25 +00002878 case XML_HTML_DOCUMENT_NODE:
Daniel Veillard16253641998-10-28 22:58:05 +00002879 case XML_DOCUMENT_TYPE_NODE:
2880 break;
Daniel Veillardcf461992000-03-14 18:30:20 +00002881 case XML_ELEMENT_DECL:
2882 /* TODO !!! */
2883 break;
2884 case XML_ATTRIBUTE_DECL:
2885 /* TODO !!! */
2886 break;
2887 case XML_ENTITY_DECL:
2888 /* TODO !!! */
2889 break;
Daniel Veillard260a68f1998-08-13 03:39:55 +00002890 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00002891}
2892
Daniel Veillard97b58771998-10-20 06:14:16 +00002893/**
2894 * xmlNodeAddContentLen:
2895 * @cur: the node being modified
2896 * @content: extra content
2897 * @len: the size of @content
2898 *
2899 * Append the extra substring to the node content.
Daniel Veillard260a68f1998-08-13 03:39:55 +00002900 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00002901void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002902xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00002903 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00002904#ifdef DEBUG_TREE
Daniel Veillard16253641998-10-28 22:58:05 +00002905 fprintf(stderr, "xmlNodeAddContentLen : node == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00002906#endif
Daniel Veillard16253641998-10-28 22:58:05 +00002907 return;
2908 }
2909 if (len <= 0) return;
2910 switch (cur->type) {
2911 case XML_DOCUMENT_FRAG_NODE:
2912 case XML_ELEMENT_NODE: {
2913 xmlNodePtr last = NULL, new;
2914
Daniel Veillardcf461992000-03-14 18:30:20 +00002915 if (cur->children != NULL) {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002916 last = cur->last;
Daniel Veillard16253641998-10-28 22:58:05 +00002917 } else {
2918 if (cur->content != NULL) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002919#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardcf461992000-03-14 18:30:20 +00002920 cur->children = xmlStringGetNodeList(cur->doc, cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002921#else
Daniel Veillardcf461992000-03-14 18:30:20 +00002922 cur->children = xmlStringGetNodeList(cur->doc,
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002923 xmlBufferContent(cur->content));
2924#endif
Daniel Veillard1e346af1999-02-22 10:33:01 +00002925 UPDATE_LAST_CHILD(cur)
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002926#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard6454aec1999-09-02 22:04:43 +00002927 xmlFree(cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002928#else
2929 xmlBufferFree(cur->content);
2930#endif
Daniel Veillard16253641998-10-28 22:58:05 +00002931 cur->content = NULL;
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002932 last = cur->last;
Daniel Veillard16253641998-10-28 22:58:05 +00002933 }
2934 }
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002935 new = xmlNewTextLen(content, len);
Daniel Veillard16253641998-10-28 22:58:05 +00002936 if (new != NULL) {
2937 xmlAddChild(cur, new);
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002938 if ((last != NULL) && (last->next == new)) {
Daniel Veillard16253641998-10-28 22:58:05 +00002939 xmlTextMerge(last, new);
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00002940 }
Daniel Veillard16253641998-10-28 22:58:05 +00002941 }
2942 break;
2943 }
2944 case XML_ATTRIBUTE_NODE:
2945 break;
2946 case XML_TEXT_NODE:
2947 case XML_CDATA_SECTION_NODE:
2948 case XML_ENTITY_REF_NODE:
2949 case XML_ENTITY_NODE:
2950 case XML_PI_NODE:
2951 case XML_COMMENT_NODE:
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002952 case XML_NOTATION_NODE:
2953 if (content != NULL) {
2954#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard16253641998-10-28 22:58:05 +00002955 cur->content = xmlStrncat(cur->content, content, len);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00002956#else
2957 xmlBufferAdd(cur->content, content, len);
2958#endif
2959 }
Daniel Veillard16253641998-10-28 22:58:05 +00002960 case XML_DOCUMENT_NODE:
Daniel Veillardcf461992000-03-14 18:30:20 +00002961 case XML_DTD_NODE:
Daniel Veillard7c1206f1999-10-14 09:10:25 +00002962 case XML_HTML_DOCUMENT_NODE:
Daniel Veillard16253641998-10-28 22:58:05 +00002963 case XML_DOCUMENT_TYPE_NODE:
2964 break;
Daniel Veillardcf461992000-03-14 18:30:20 +00002965 case XML_ELEMENT_DECL:
2966 case XML_ATTRIBUTE_DECL:
2967 case XML_ENTITY_DECL:
2968 break;
Daniel Veillard16253641998-10-28 22:58:05 +00002969 }
2970}
2971
2972/**
2973 * xmlNodeAddContent:
2974 * @cur: the node being modified
2975 * @content: extra content
2976 *
2977 * Append the extra substring to the node content.
2978 */
2979void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00002980xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) {
Daniel Veillard16253641998-10-28 22:58:05 +00002981 int len;
2982
2983 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00002984#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00002985 fprintf(stderr, "xmlNodeAddContent : node == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00002986#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00002987 return;
2988 }
Daniel Veillard16253641998-10-28 22:58:05 +00002989 if (content == NULL) return;
2990 len = xmlStrlen(content);
2991 xmlNodeAddContentLen(cur, content, len);
2992}
2993
2994/**
2995 * xmlTextMerge:
2996 * @first: the first text node
2997 * @second: the second text node being merged
2998 *
2999 * Merge two text nodes into one
Daniel Veillard1e346af1999-02-22 10:33:01 +00003000 * Returns the first text node augmented
Daniel Veillard16253641998-10-28 22:58:05 +00003001 */
3002xmlNodePtr
3003xmlTextMerge(xmlNodePtr first, xmlNodePtr second) {
3004 if (first == NULL) return(second);
3005 if (second == NULL) return(first);
3006 if (first->type != XML_TEXT_NODE) return(first);
3007 if (second->type != XML_TEXT_NODE) return(first);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003008#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard16253641998-10-28 22:58:05 +00003009 xmlNodeAddContent(first, second->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003010#else
3011 xmlNodeAddContent(first, xmlBufferContent(second->content));
3012#endif
Daniel Veillard16253641998-10-28 22:58:05 +00003013 xmlUnlinkNode(second);
3014 xmlFreeNode(second);
3015 return(first);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003016}
3017
Daniel Veillard97b58771998-10-20 06:14:16 +00003018/**
Daniel Veillardb96e6431999-08-29 21:02:19 +00003019 * xmlGetNsList:
3020 * @doc: the document
3021 * @node: the current node
3022 *
3023 * Search all the namespace applying to a given element.
3024 * Returns an NULL terminated array of all the xmlNsPtr found
3025 * that need to be freed by the caller or NULL if no
3026 * namespace if defined
3027 */
3028xmlNsPtr *
3029xmlGetNsList(xmlDocPtr doc, xmlNodePtr node) {
3030 xmlNsPtr cur;
3031 xmlNsPtr *ret = NULL;
3032 int nbns = 0;
3033 int maxns = 10;
3034 int i;
3035
3036 while (node != NULL) {
3037 cur = node->nsDef;
3038 while (cur != NULL) {
3039 if (ret == NULL) {
Daniel Veillard6454aec1999-09-02 22:04:43 +00003040 ret = (xmlNsPtr *) xmlMalloc((maxns + 1) * sizeof(xmlNsPtr));
Daniel Veillardb96e6431999-08-29 21:02:19 +00003041 if (ret == NULL) {
3042 fprintf(stderr, "xmlGetNsList : out of memory!\n");
3043 return(NULL);
3044 }
3045 ret[nbns] = NULL;
3046 }
3047 for (i = 0;i < nbns;i++) {
3048 if ((cur->prefix == ret[i]->prefix) ||
3049 (!xmlStrcmp(cur->prefix, ret[i]->prefix))) break;
3050 }
3051 if (i >= nbns) {
3052 if (nbns >= maxns) {
3053 maxns *= 2;
Daniel Veillard6454aec1999-09-02 22:04:43 +00003054 ret = (xmlNsPtr *) xmlRealloc(ret,
Daniel Veillardb96e6431999-08-29 21:02:19 +00003055 (maxns + 1) * sizeof(xmlNsPtr));
3056 if (ret == NULL) {
3057 fprintf(stderr, "xmlGetNsList : realloc failed!\n");
3058 return(NULL);
3059 }
3060 }
3061 ret[nbns++] = cur;
3062 ret[nbns] = NULL;
3063 }
3064
3065 cur = cur->next;
3066 }
3067 node = node->parent;
3068 }
3069 return(ret);
3070}
3071
3072/**
Daniel Veillard97b58771998-10-20 06:14:16 +00003073 * xmlSearchNs:
3074 * @doc: the document
3075 * @node: the current node
3076 * @nameSpace: the namespace string
Daniel Veillard260a68f1998-08-13 03:39:55 +00003077 *
Daniel Veillard97b58771998-10-20 06:14:16 +00003078 * Search a Ns registered under a given name space for a document.
3079 * recurse on the parents until it finds the defined namespace
3080 * or return NULL otherwise.
3081 * @nameSpace can be NULL, this is a search for the default namespace.
Daniel Veillard1e346af1999-02-22 10:33:01 +00003082 * Returns the namespace pointer or NULL.
Daniel Veillard260a68f1998-08-13 03:39:55 +00003083 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003084xmlNsPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003085xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003086 xmlNsPtr cur;
3087
Daniel Veillard62ba71e1999-12-16 17:52:19 +00003088 if (node == NULL) return(NULL);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003089 while (node != NULL) {
Daniel Veillardcf461992000-03-14 18:30:20 +00003090 if (node->type == XML_ELEMENT_NODE) {
3091 cur = node->nsDef;
3092 while (cur != NULL) {
3093 if ((cur->prefix == NULL) && (nameSpace == NULL))
3094 return(cur);
3095 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
3096 (!xmlStrcmp(cur->prefix, nameSpace)))
3097 return(cur);
3098 cur = cur->next;
3099 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00003100 }
3101 node = node->parent;
3102 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00003103 return(NULL);
3104}
3105
Daniel Veillard97b58771998-10-20 06:14:16 +00003106/**
3107 * xmlSearchNsByHref:
3108 * @doc: the document
3109 * @node: the current node
3110 * @href: the namespace value
3111 *
3112 * Search a Ns aliasing a given URI. Recurse on the parents until it finds
3113 * the defined namespace or return NULL otherwise.
Daniel Veillard1e346af1999-02-22 10:33:01 +00003114 * Returns the namespace pointer or NULL.
Daniel Veillard260a68f1998-08-13 03:39:55 +00003115 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003116xmlNsPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003117xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar *href) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003118 xmlNsPtr cur;
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003119 xmlNodePtr orig = node;
Daniel Veillard260a68f1998-08-13 03:39:55 +00003120
Daniel Veillard10a2c651999-12-12 13:03:50 +00003121 if ((node == NULL) || (href == NULL)) return(NULL);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003122 while (node != NULL) {
3123 cur = node->nsDef;
3124 while (cur != NULL) {
3125 if ((cur->href != NULL) && (href != NULL) &&
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003126 (!xmlStrcmp(cur->href, href))) {
3127 /*
3128 * Check that the prefix is not shadowed between orig and node
3129 */
3130 xmlNodePtr check = orig;
3131 xmlNsPtr tst;
3132
3133 while (check != node) {
3134 tst = check->nsDef;
3135 while (tst != NULL) {
3136 if ((tst->prefix == NULL) && (cur->prefix == NULL))
3137 goto shadowed;
3138 if ((tst->prefix != NULL) && (cur->prefix != NULL) &&
3139 (!xmlStrcmp(tst->prefix, cur->prefix)))
3140 goto shadowed;
3141 tst = tst->next;
3142 }
3143 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00003144 return(cur);
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003145 }
3146shadowed:
Daniel Veillard260a68f1998-08-13 03:39:55 +00003147 cur = cur->next;
3148 }
3149 node = node->parent;
3150 }
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003151 return(NULL);
3152}
3153
3154/**
3155 * xmlNewReconciliedNs
3156 * @doc: the document
3157 * @tree: a node expected to hold the new namespace
3158 * @ns: the original namespace
3159 *
3160 * This function tries to locate a namespace definition in a tree
3161 * ancestors, or create a new namespace definition node similar to
3162 * @ns trying to reuse the same prefix. However if the given prefix is
3163 * null (default namespace) or reused within the subtree defined by
3164 * @tree or on one of its ancestors then a new prefix is generated.
3165 * Returns the (new) namespace definition or NULL in case of error
3166 */
3167xmlNsPtr
3168xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) {
3169 xmlNsPtr def;
3170 xmlChar prefix[50];
3171 int counter = 1;
3172
3173 if (tree == NULL) {
3174#ifdef DEBUG_TREE
3175 fprintf(stderr, "xmlNewReconciliedNs : tree == NULL\n");
3176#endif
3177 return(NULL);
3178 }
3179 if (ns == NULL) {
3180#ifdef DEBUG_TREE
3181 fprintf(stderr, "xmlNewReconciliedNs : ns == NULL\n");
3182#endif
3183 return(NULL);
3184 }
3185 /*
3186 * Search an existing namespace definition inherited.
3187 */
3188 def = xmlSearchNsByHref(doc, tree, ns->href);
3189 if (def != NULL)
3190 return(def);
3191
3192 /*
3193 * Find a close prefix which is not already in use.
3194 * Let's strip namespace prefixes longer than 20 chars !
3195 */
3196 sprintf((char *) prefix, "%.20s", ns->prefix);
3197 def = xmlSearchNs(doc, tree, prefix);
3198 while (def != NULL) {
3199 if (counter > 1000) return(NULL);
3200 sprintf((char *) prefix, "%.20s%d", ns->prefix, counter++);
3201 def = xmlSearchNs(doc, tree, prefix);
3202 }
3203
3204 /*
3205 * Ok, now we are ready to create a new one.
3206 */
3207 def = xmlNewNs(tree, ns->href, prefix);
3208 return(def);
3209}
3210
3211/**
3212 * xmlReconciliateNs
3213 * @doc: the document
3214 * @tree: a node defining the subtree to reconciliate
3215 *
3216 * This function checks that all the namespaces declared within the given
3217 * tree are properly declared. This is needed for example after Copy or Cut
3218 * and then paste operations. The subtree may still hold pointers to
3219 * namespace declarations outside the subtree or invalid/masked. As much
3220 * as possible the function try tu reuse the existing namespaces found in
3221 * the new environment. If not possible the new namespaces are redeclared
3222 * on @tree at the top of the given subtree.
3223 * Returns the number of namespace declarations created or -1 in case of error.
3224 */
3225int
3226xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) {
3227 xmlNsPtr *oldNs = NULL;
3228 xmlNsPtr *newNs = NULL;
3229 int sizeCache = 0;
3230 int nbCache = 0;
3231
3232 xmlNsPtr n;
3233 xmlNodePtr node = tree;
3234 xmlAttrPtr attr;
3235 int ret = 0, i;
3236
3237 while (node != NULL) {
3238 /*
3239 * Reconciliate the node namespace
3240 */
3241 if (node->ns != NULL) {
3242 /*
3243 * initialize the cache if needed
3244 */
3245 if (sizeCache == 0) {
3246 sizeCache = 10;
3247 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
3248 sizeof(xmlNsPtr));
3249 if (oldNs == NULL) {
3250 fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
3251 return(-1);
3252 }
3253 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
3254 sizeof(xmlNsPtr));
3255 if (newNs == NULL) {
3256 fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
3257 xmlFree(oldNs);
3258 return(-1);
3259 }
3260 }
3261 for (i = 0;i < nbCache;i++) {
3262 if (oldNs[i] == node->ns) {
3263 node->ns = newNs[i];
3264 break;
3265 }
3266 }
3267 if (i == nbCache) {
3268 /*
3269 * Ok we need to recreate a new namespace definition
3270 */
3271 n = xmlNewReconciliedNs(doc, tree, node->ns);
3272 if (n != NULL) { /* :-( what if else ??? */
3273 /*
3274 * check if we need to grow the cache buffers.
3275 */
3276 if (sizeCache <= nbCache) {
3277 sizeCache *= 2;
3278 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
3279 sizeof(xmlNsPtr));
3280 if (oldNs == NULL) {
3281 fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
3282 xmlFree(newNs);
3283 return(-1);
3284 }
3285 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
3286 sizeof(xmlNsPtr));
3287 if (newNs == NULL) {
3288 fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
3289 xmlFree(oldNs);
3290 return(-1);
3291 }
3292 }
3293 newNs[nbCache] = n;
3294 oldNs[nbCache++] = node->ns;
3295 node->ns = n;
3296 }
3297 }
3298 }
3299 /*
3300 * now check for namespace hold by attributes on the node.
3301 */
3302 attr = node->properties;
3303 while (attr != NULL) {
3304 if (attr->ns != NULL) {
3305 /*
3306 * initialize the cache if needed
3307 */
3308 if (sizeCache == 0) {
3309 sizeCache = 10;
3310 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
3311 sizeof(xmlNsPtr));
3312 if (oldNs == NULL) {
3313 fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
3314 return(-1);
3315 }
3316 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
3317 sizeof(xmlNsPtr));
3318 if (newNs == NULL) {
3319 fprintf(stderr, "xmlReconciliateNs : memory pbm\n");
3320 xmlFree(oldNs);
3321 return(-1);
3322 }
3323 }
3324 for (i = 0;i < nbCache;i++) {
3325 if (oldNs[i] == attr->ns) {
3326 node->ns = newNs[i];
3327 break;
3328 }
3329 }
3330 if (i == nbCache) {
3331 /*
3332 * Ok we need to recreate a new namespace definition
3333 */
3334 n = xmlNewReconciliedNs(doc, tree, attr->ns);
3335 if (n != NULL) { /* :-( what if else ??? */
3336 /*
3337 * check if we need to grow the cache buffers.
3338 */
3339 if (sizeCache <= nbCache) {
3340 sizeCache *= 2;
3341 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
3342 sizeof(xmlNsPtr));
3343 if (oldNs == NULL) {
3344 fprintf(stderr,
3345 "xmlReconciliateNs : memory pbm\n");
3346 xmlFree(newNs);
3347 return(-1);
3348 }
3349 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
3350 sizeof(xmlNsPtr));
3351 if (newNs == NULL) {
3352 fprintf(stderr,
3353 "xmlReconciliateNs : memory pbm\n");
3354 xmlFree(oldNs);
3355 return(-1);
3356 }
3357 }
3358 newNs[nbCache] = n;
3359 oldNs[nbCache++] = attr->ns;
3360 attr->ns = n;
3361 }
3362 }
3363 }
3364 attr = attr->next;
3365 }
3366
3367 /*
3368 * Browse the full subtree, deep first
3369 */
Daniel Veillardcf461992000-03-14 18:30:20 +00003370 if (node->children != NULL) {
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003371 /* deep first */
Daniel Veillardcf461992000-03-14 18:30:20 +00003372 node = node->children;
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003373 } else if ((node != tree) && (node->next != NULL)) {
3374 /* then siblings */
3375 node = node->next;
3376 } else if (node != tree) {
3377 /* go up to parents->next if needed */
3378 while (node != tree) {
3379 if (node->parent != NULL)
3380 node = node->parent;
3381 if ((node != tree) && (node->next != NULL)) {
3382 node = node->next;
3383 break;
3384 }
3385 if (node->parent == NULL) {
3386 node = NULL;
3387 break;
3388 }
3389 }
3390 /* exit condition */
3391 if (node == tree)
3392 node = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +00003393 }
3394 }
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003395 return(ret);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003396}
3397
Daniel Veillard97b58771998-10-20 06:14:16 +00003398/**
3399 * xmlGetProp:
3400 * @node: the node
3401 * @name: the attribute name
3402 *
3403 * Search and get the value of an attribute associated to a node
Daniel Veillardccb09631998-10-27 06:21:04 +00003404 * This does the entity substitution.
Daniel Veillard10a2c651999-12-12 13:03:50 +00003405 * This function looks in DTD attribute declaration for #FIXED or
3406 * default declaration values unless DTD use has been turned off.
3407 *
Daniel Veillard1e346af1999-02-22 10:33:01 +00003408 * Returns the attribute value or NULL if not found.
Daniel Veillarda819dac1999-11-24 18:04:22 +00003409 * It's up to the caller to free the memory.
Daniel Veillard260a68f1998-08-13 03:39:55 +00003410 */
Daniel Veillarda819dac1999-11-24 18:04:22 +00003411xmlChar *
3412xmlGetProp(xmlNodePtr node, const xmlChar *name) {
Daniel Veillard10a2c651999-12-12 13:03:50 +00003413 xmlAttrPtr prop;
3414 xmlDocPtr doc;
Daniel Veillard260a68f1998-08-13 03:39:55 +00003415
Daniel Veillard10a2c651999-12-12 13:03:50 +00003416 if ((node == NULL) || (name == NULL)) return(NULL);
3417 /*
3418 * Check on the properties attached to the node
3419 */
3420 prop = node->properties;
Daniel Veillard260a68f1998-08-13 03:39:55 +00003421 while (prop != NULL) {
Daniel Veillard68178931999-02-08 18:34:36 +00003422 if (!xmlStrcmp(prop->name, name)) {
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003423 xmlChar *ret;
Daniel Veillard6800ef31999-02-08 18:33:22 +00003424
Daniel Veillardcf461992000-03-14 18:30:20 +00003425 ret = xmlNodeListGetString(node->doc, prop->children, 1);
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003426 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
Daniel Veillard6800ef31999-02-08 18:33:22 +00003427 return(ret);
Daniel Veillard68178931999-02-08 18:34:36 +00003428 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00003429 prop = prop->next;
3430 }
Daniel Veillard10a2c651999-12-12 13:03:50 +00003431 if (!xmlCheckDTD) return(NULL);
3432
3433 /*
3434 * Check if there is a default declaration in the internal
3435 * or external subsets
3436 */
3437 doc = node->doc;
3438 if (doc != NULL) {
3439 xmlAttributePtr attrDecl;
3440 if (doc->intSubset != NULL) {
3441 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
3442 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3443 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
Daniel Veillardf967b902000-01-17 16:06:10 +00003444 if (attrDecl != NULL)
3445 return(xmlStrdup(attrDecl->defaultValue));
Daniel Veillard10a2c651999-12-12 13:03:50 +00003446 }
3447 }
3448 return(NULL);
3449}
3450
3451/**
3452 * xmlGetNsProp:
3453 * @node: the node
3454 * @name: the attribute name
3455 * @namespace: the URI of the namespace
3456 *
3457 * Search and get the value of an attribute associated to a node
3458 * This attribute has to be anchored in the namespace specified.
3459 * This does the entity substitution.
3460 * This function looks in DTD attribute declaration for #FIXED or
3461 * default declaration values unless DTD use has been turned off.
3462 *
3463 * Returns the attribute value or NULL if not found.
3464 * It's up to the caller to free the memory.
3465 */
3466xmlChar *
3467xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *namespace) {
3468 xmlAttrPtr prop = node->properties;
3469 xmlDocPtr doc;
3470 xmlNsPtr ns;
3471
3472 if (namespace == NULL)
3473 return(xmlGetProp(node, name));
3474 while (prop != NULL) {
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00003475 /*
3476 * One need to have
3477 * - same attribute names
3478 * - and the attribute carrying that namespace
3479 * or
3480 * no namespace on the attribute and the element carrying it
3481 */
Daniel Veillard10a2c651999-12-12 13:03:50 +00003482 if ((!xmlStrcmp(prop->name, name)) &&
Daniel Veillard5cb5ab81999-12-21 15:35:29 +00003483 (((prop->ns == NULL) && (node->ns != NULL) &&
3484 (!xmlStrcmp(node->ns->href, namespace))) ||
Daniel Veillard3c558c31999-12-22 11:30:41 +00003485 ((prop->ns != NULL) && (!xmlStrcmp(prop->ns->href, namespace))))) {
Daniel Veillard10a2c651999-12-12 13:03:50 +00003486 xmlChar *ret;
3487
Daniel Veillardcf461992000-03-14 18:30:20 +00003488 ret = xmlNodeListGetString(node->doc, prop->children, 1);
Daniel Veillard10a2c651999-12-12 13:03:50 +00003489 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
3490 return(ret);
3491 }
3492 prop = prop->next;
3493 }
3494 if (!xmlCheckDTD) return(NULL);
3495
3496 /*
3497 * Check if there is a default declaration in the internal
3498 * or external subsets
3499 */
3500 doc = node->doc;
3501 if (doc != NULL) {
3502 xmlAttributePtr attrDecl;
3503 if (doc->intSubset != NULL) {
3504 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
3505 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3506 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
3507
3508 if (attrDecl->prefix != NULL) {
3509 /*
3510 * The DTD declaration only allows a prefix search
3511 */
3512 ns = xmlSearchNs(doc, node, attrDecl->prefix);
3513 if ((ns != NULL) && (!xmlStrcmp(ns->href, namespace)))
3514 return(xmlStrdup(attrDecl->defaultValue));
3515 }
3516 }
3517 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00003518 return(NULL);
3519}
3520
Daniel Veillard97b58771998-10-20 06:14:16 +00003521/**
Daniel Veillardccb09631998-10-27 06:21:04 +00003522 * xmlSetProp:
Daniel Veillard97b58771998-10-20 06:14:16 +00003523 * @node: the node
3524 * @name: the attribute name
3525 * @value: the attribute value
3526 *
3527 * Set (or reset) an attribute carried by a node.
Daniel Veillard1e346af1999-02-22 10:33:01 +00003528 * Returns the attribute pointer.
Daniel Veillard260a68f1998-08-13 03:39:55 +00003529 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003530xmlAttrPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003531xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003532 xmlAttrPtr prop = node->properties;
3533
3534 while (prop != NULL) {
3535 if (!xmlStrcmp(prop->name, name)) {
Daniel Veillardcf461992000-03-14 18:30:20 +00003536 if (prop->children != NULL)
3537 xmlFreeNodeList(prop->children);
3538 prop->children = NULL;
Daniel Veillard51e3b151999-11-12 17:02:31 +00003539 if (value != NULL) {
3540 xmlChar *buffer;
Daniel Veillardcf461992000-03-14 18:30:20 +00003541 xmlNodePtr tmp;
3542
Daniel Veillard51e3b151999-11-12 17:02:31 +00003543 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
Daniel Veillardcf461992000-03-14 18:30:20 +00003544 prop->children = xmlStringGetNodeList(node->doc, buffer);
3545 tmp = prop->children;
3546 while (tmp != NULL) {
3547 tmp->parent = (xmlNodePtr) prop;
3548 if (tmp->next == NULL)
3549 prop->last = tmp;
3550 tmp = tmp->next;
3551 }
Daniel Veillard51e3b151999-11-12 17:02:31 +00003552 xmlFree(buffer);
3553 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00003554 return(prop);
3555 }
3556 prop = prop->next;
3557 }
3558 prop = xmlNewProp(node, name, value);
3559 return(prop);
3560}
3561
Daniel Veillard97b58771998-10-20 06:14:16 +00003562/**
3563 * xmlNodeIsText:
3564 * @node: the node
3565 *
3566 * Is this node a Text node ?
Daniel Veillard1e346af1999-02-22 10:33:01 +00003567 * Returns 1 yes, 0 no
Daniel Veillard260a68f1998-08-13 03:39:55 +00003568 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003569int
3570xmlNodeIsText(xmlNodePtr node) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003571 if (node == NULL) return(0);
3572
Daniel Veillard0bef1311998-10-14 02:36:47 +00003573 if (node->type == XML_TEXT_NODE) return(1);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003574 return(0);
3575}
3576
Daniel Veillard97b58771998-10-20 06:14:16 +00003577/**
Daniel Veillard3e6d2372000-03-04 11:39:43 +00003578 * xmlIsBlankNode:
3579 * @node: the node
3580 *
3581 * Is this node a Text node ?
3582 * Returns 1 yes, 0 no
3583 */
3584int
3585xmlIsBlankNode(xmlNodePtr node) {
3586 xmlChar *cur;
3587 if (node == NULL) return(0);
3588
3589 if (node->type != XML_TEXT_NODE) return(0);
3590 if (node->content == NULL) return(0);
3591 cur = node->content;
3592 while (*cur != 0) {
3593 if (!IS_BLANK(*cur)) return(0);
3594 cur++;
3595 }
3596
3597 return(1);
3598}
3599
3600/**
Daniel Veillard1e346af1999-02-22 10:33:01 +00003601 * xmlTextConcat:
Daniel Veillard97b58771998-10-20 06:14:16 +00003602 * @node: the node
3603 * @content: the content
3604 * @len: @content lenght
3605 *
3606 * Concat the given string at the end of the existing node content
Daniel Veillard260a68f1998-08-13 03:39:55 +00003607 */
Daniel Veillard97b58771998-10-20 06:14:16 +00003608
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003609void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003610xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00003611 if (node == NULL) return;
3612
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003613 if ((node->type != XML_TEXT_NODE) &&
3614 (node->type != XML_CDATA_SECTION_NODE)) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003615#ifdef DEBUG_TREE
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003616 fprintf(stderr, "xmlTextConcat: node is not text nor cdata\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003617#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00003618 return;
3619 }
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003620#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard260a68f1998-08-13 03:39:55 +00003621 node->content = xmlStrncat(node->content, content, len);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003622#else
3623 xmlBufferAdd(node->content, content, len);
3624#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00003625}
3626
3627/************************************************************************
3628 * *
3629 * Output : to a FILE or in memory *
3630 * *
3631 ************************************************************************/
3632
Daniel Veillard5099ae81999-04-21 20:12:07 +00003633#define BASE_BUFFER_SIZE 4000
3634
3635/**
3636 * xmlBufferCreate:
3637 *
3638 * routine to create an XML buffer.
3639 * returns the new structure.
3640 */
3641xmlBufferPtr
3642xmlBufferCreate(void) {
3643 xmlBufferPtr ret;
3644
Daniel Veillard6454aec1999-09-02 22:04:43 +00003645 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
Daniel Veillard5099ae81999-04-21 20:12:07 +00003646 if (ret == NULL) {
3647 fprintf(stderr, "xmlBufferCreate : out of memory!\n");
3648 return(NULL);
3649 }
3650 ret->use = 0;
3651 ret->size = BASE_BUFFER_SIZE;
Daniel Veillard10a2c651999-12-12 13:03:50 +00003652 ret->alloc = xmlBufferAllocScheme;
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003653 ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar));
Daniel Veillard5099ae81999-04-21 20:12:07 +00003654 if (ret->content == NULL) {
3655 fprintf(stderr, "xmlBufferCreate : out of memory!\n");
Daniel Veillard6454aec1999-09-02 22:04:43 +00003656 xmlFree(ret);
Daniel Veillard5099ae81999-04-21 20:12:07 +00003657 return(NULL);
3658 }
3659 ret->content[0] = 0;
3660 return(ret);
3661}
3662
3663/**
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003664 * xmlBufferCreateSize:
3665 * @size: initial size of buffer
3666 *
3667 * routine to create an XML buffer.
3668 * returns the new structure.
3669 */
3670xmlBufferPtr
3671xmlBufferCreateSize(size_t size) {
3672 xmlBufferPtr ret;
3673
3674 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
3675 if (ret == NULL) {
3676 fprintf(stderr, "xmlBufferCreate : out of memory!\n");
3677 return(NULL);
3678 }
3679 ret->use = 0;
Daniel Veillard10a2c651999-12-12 13:03:50 +00003680 ret->alloc = xmlBufferAllocScheme;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003681 ret->size = (size ? size+2 : 0); /* +1 for ending null */
Daniel Veillard10a2c651999-12-12 13:03:50 +00003682 if (ret->size){
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003683 ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar));
3684 if (ret->content == NULL) {
3685 fprintf(stderr, "xmlBufferCreate : out of memory!\n");
3686 xmlFree(ret);
3687 return(NULL);
3688 }
3689 ret->content[0] = 0;
3690 } else
3691 ret->content = NULL;
3692 return(ret);
3693}
3694
3695/**
Daniel Veillard06047432000-04-24 11:33:38 +00003696 * xmlBufferSetAllocationScheme:
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003697 * @buf: the buffer to free
3698 * @scheme: allocation scheme to use
3699 *
3700 * Sets the allocation scheme for this buffer
3701 */
3702void
3703xmlBufferSetAllocationScheme(xmlBufferPtr buf,
3704 xmlBufferAllocationScheme scheme) {
3705 if (buf == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003706#ifdef DEBUG_BUFFER
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003707 fprintf(stderr, "xmlBufferSetAllocationScheme: buf == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003708#endif
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003709 return;
3710 }
3711
3712 buf->alloc = scheme;
3713}
3714
3715/**
Daniel Veillard5099ae81999-04-21 20:12:07 +00003716 * xmlBufferFree:
3717 * @buf: the buffer to free
3718 *
3719 * Frees an XML buffer.
3720 */
3721void
3722xmlBufferFree(xmlBufferPtr buf) {
3723 if (buf == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003724#ifdef DEBUG_BUFFER
Daniel Veillard5099ae81999-04-21 20:12:07 +00003725 fprintf(stderr, "xmlBufferFree: buf == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003726#endif
Daniel Veillard5099ae81999-04-21 20:12:07 +00003727 return;
3728 }
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003729 if (buf->content != NULL) {
3730#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard5099ae81999-04-21 20:12:07 +00003731 memset(buf->content, -1, BASE_BUFFER_SIZE);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003732#else
3733 memset(buf->content, -1, buf->size);
3734#endif
Daniel Veillard6454aec1999-09-02 22:04:43 +00003735 xmlFree(buf->content);
Daniel Veillard5099ae81999-04-21 20:12:07 +00003736 }
3737 memset(buf, -1, sizeof(xmlBuffer));
Daniel Veillard6454aec1999-09-02 22:04:43 +00003738 xmlFree(buf);
Daniel Veillard5099ae81999-04-21 20:12:07 +00003739}
3740
3741/**
Daniel Veillarde2d034d1999-07-27 19:52:06 +00003742 * xmlBufferEmpty:
3743 * @buf: the buffer
3744 *
3745 * empty a buffer.
3746 */
3747void
3748xmlBufferEmpty(xmlBufferPtr buf) {
3749 buf->use = 0;
3750 memset(buf->content, -1, buf->size);/* just for debug */
3751}
3752
3753/**
3754 * xmlBufferShrink:
3755 * @buf: the buffer to dump
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003756 * @len: the number of xmlChar to remove
Daniel Veillarde2d034d1999-07-27 19:52:06 +00003757 *
3758 * Remove the beginning of an XML buffer.
3759 *
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003760 * Returns the number of xmlChar removed, or -1 in case of failure.
Daniel Veillarde2d034d1999-07-27 19:52:06 +00003761 */
3762int
3763xmlBufferShrink(xmlBufferPtr buf, int len) {
3764 if (len == 0) return(0);
3765 if (len > buf->use) return(-1);
3766
3767 buf->use -= len;
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003768 memmove(buf->content, &buf->content[len], buf->use * sizeof(xmlChar));
Daniel Veillarde2d034d1999-07-27 19:52:06 +00003769
3770 buf->content[buf->use] = 0;
3771 return(len);
3772}
3773
3774/**
Daniel Veillard5099ae81999-04-21 20:12:07 +00003775 * xmlBufferDump:
3776 * @file: the file output
3777 * @buf: the buffer to dump
3778 *
3779 * Dumps an XML buffer to a FILE *.
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003780 * Returns the number of xmlChar written
Daniel Veillard5099ae81999-04-21 20:12:07 +00003781 */
3782int
3783xmlBufferDump(FILE *file, xmlBufferPtr buf) {
3784 int ret;
3785
3786 if (buf == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003787#ifdef DEBUG_BUFFER
Daniel Veillard5099ae81999-04-21 20:12:07 +00003788 fprintf(stderr, "xmlBufferDump: buf == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003789#endif
Daniel Veillard5099ae81999-04-21 20:12:07 +00003790 return(0);
3791 }
3792 if (buf->content == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003793#ifdef DEBUG_BUFFER
Daniel Veillard5099ae81999-04-21 20:12:07 +00003794 fprintf(stderr, "xmlBufferDump: buf->content == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003795#endif
Daniel Veillard5099ae81999-04-21 20:12:07 +00003796 return(0);
3797 }
3798 if (file == NULL) file = stdout;
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003799 ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
Daniel Veillard5099ae81999-04-21 20:12:07 +00003800 return(ret);
3801}
3802
3803/**
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003804 * xmlBufferContent:
3805 * @buf: the buffer to resize
3806 *
3807 * Returns the internal content
3808 */
3809
3810const xmlChar*
3811xmlBufferContent(const xmlBufferPtr buf)
3812{
3813 if(!buf)
3814 return NULL;
3815
3816 return buf->content;
3817}
3818
3819/**
3820 * xmlBufferLength:
3821 * @buf: the buffer
3822 *
3823 * Returns the length of data in the internal content
3824 */
3825
3826int
3827xmlBufferLength(const xmlBufferPtr buf)
3828{
3829 if(!buf)
3830 return 0;
3831
3832 return buf->use;
3833}
3834
3835/**
3836 * xmlBufferResize:
3837 * @buf: the buffer to resize
3838 * @len: the desired size
3839 *
3840 * Resize a buffer to accomodate minimum size of <len>.
3841 *
3842 * Returns 0 in case of problems, 1 otherwise
3843 */
3844int
3845xmlBufferResize(xmlBufferPtr buf, int size)
3846{
3847 int newSize = (buf->size ? buf->size*2 : size);/*take care of empty case*/
3848 xmlChar* rebuf = NULL;
3849
3850 /* Don't resize if we don't have to */
3851 if(size < buf->size)
3852 return 1;
3853
3854 /* figure out new size */
3855 switch(buf->alloc){
3856 case XML_BUFFER_ALLOC_DOUBLEIT:
3857 while(size > newSize) newSize *= 2;
3858 break;
3859 case XML_BUFFER_ALLOC_EXACT:
3860 newSize = size+10;
3861 break;
3862 default:
3863 newSize = size+10;
3864 break;
3865 }
3866
3867 if (buf->content == NULL)
3868 rebuf = (xmlChar *) xmlMalloc(newSize * sizeof(xmlChar));
3869 else
3870 rebuf = (xmlChar *) xmlRealloc(buf->content,
3871 newSize * sizeof(xmlChar));
3872 if (rebuf == NULL) {
3873 fprintf(stderr, "xmlBufferAdd : out of memory!\n");
3874 return 0;
3875 }
3876 buf->content = rebuf;
3877 buf->size = newSize;
3878
3879 return 1;
3880}
3881/**
Daniel Veillard5099ae81999-04-21 20:12:07 +00003882 * xmlBufferAdd:
3883 * @buf: the buffer to dump
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003884 * @str: the xmlChar string
3885 * @len: the number of xmlChar to add
Daniel Veillard5099ae81999-04-21 20:12:07 +00003886 *
Daniel Veillard10a2c651999-12-12 13:03:50 +00003887 * Add a string range to an XML buffer. if len == -1, the lenght of
3888 * str is recomputed.
Daniel Veillard5099ae81999-04-21 20:12:07 +00003889 */
3890void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003891xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) {
Daniel Veillardcf461992000-03-14 18:30:20 +00003892 int needSize;
Daniel Veillard5099ae81999-04-21 20:12:07 +00003893
3894 if (str == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003895#ifdef DEBUG_BUFFER
Daniel Veillard5099ae81999-04-21 20:12:07 +00003896 fprintf(stderr, "xmlBufferAdd: str == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003897#endif
Daniel Veillard5099ae81999-04-21 20:12:07 +00003898 return;
3899 }
Daniel Veillard10a2c651999-12-12 13:03:50 +00003900 if (len < -1) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003901#ifdef DEBUG_BUFFER
Daniel Veillard10a2c651999-12-12 13:03:50 +00003902 fprintf(stderr, "xmlBufferAdd: len < 0\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003903#endif
Daniel Veillard10a2c651999-12-12 13:03:50 +00003904 return;
3905 }
3906 if (len == 0) return;
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003907
Daniel Veillarddbfd6411999-12-28 16:35:14 +00003908 if (len < 0)
Daniel Veillardcf461992000-03-14 18:30:20 +00003909 len = xmlStrlen(str);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003910
Daniel Veillarde2d034d1999-07-27 19:52:06 +00003911 if (len <= 0) return;
Daniel Veillard5099ae81999-04-21 20:12:07 +00003912
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003913 needSize = buf->use + len + 2;
3914 if(needSize > buf->size){
3915 if(!xmlBufferResize(buf, needSize)){
3916 fprintf(stderr, "xmlBufferAdd : out of memory!\n");
3917 return;
3918 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00003919 }
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003920
3921 memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
Daniel Veillarde2d034d1999-07-27 19:52:06 +00003922 buf->use += len;
3923 buf->content[buf->use] = 0;
Daniel Veillard5099ae81999-04-21 20:12:07 +00003924}
3925
3926/**
3927 * xmlBufferCat:
3928 * @buf: the buffer to dump
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003929 * @str: the xmlChar string
Daniel Veillard5099ae81999-04-21 20:12:07 +00003930 *
3931 * Append a zero terminated string to an XML buffer.
3932 */
3933void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003934xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003935 if (str != NULL)
3936 xmlBufferAdd(buf, str, -1);
Daniel Veillard5099ae81999-04-21 20:12:07 +00003937}
3938
3939/**
3940 * xmlBufferCCat:
3941 * @buf: the buffer to dump
3942 * @str: the C char string
3943 *
3944 * Append a zero terminated C string to an XML buffer.
3945 */
3946void
3947xmlBufferCCat(xmlBufferPtr buf, const char *str) {
3948 const char *cur;
3949
3950 if (str == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003951#ifdef DEBUG_BUFFER
Daniel Veillard5099ae81999-04-21 20:12:07 +00003952 fprintf(stderr, "xmlBufferAdd: str == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00003953#endif
Daniel Veillard5099ae81999-04-21 20:12:07 +00003954 return;
3955 }
3956 for (cur = str;*cur != 0;cur++) {
3957 if (buf->use + 10 >= buf->size) {
Daniel Veillardf5c2c871999-12-01 09:51:45 +00003958 if(!xmlBufferResize(buf, buf->use+10)){
3959 fprintf(stderr, "xmlBufferCCat : out of memory!\n");
3960 return;
3961 }
3962 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00003963 buf->content[buf->use++] = *cur;
3964 }
3965}
Daniel Veillard260a68f1998-08-13 03:39:55 +00003966
Daniel Veillard97b58771998-10-20 06:14:16 +00003967/**
3968 * xmlBufferWriteCHAR:
Daniel Veillard5099ae81999-04-21 20:12:07 +00003969 * @buf: the XML buffer
Daniel Veillard97b58771998-10-20 06:14:16 +00003970 * @string: the string to add
3971 *
3972 * routine which manage and grows an output buffer. This one add
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003973 * xmlChars at the end of the buffer.
Daniel Veillard97b58771998-10-20 06:14:16 +00003974 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003975void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00003976xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00003977 xmlBufferCat(buf, string);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003978}
3979
Daniel Veillard97b58771998-10-20 06:14:16 +00003980/**
3981 * xmlBufferWriteChar:
Daniel Veillard011b63c1999-06-02 17:44:04 +00003982 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00003983 * @string: the string to add
3984 *
3985 * routine which manage and grows an output buffer. This one add
3986 * C chars at the end of the array.
3987 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00003988void
Daniel Veillard5099ae81999-04-21 20:12:07 +00003989xmlBufferWriteChar(xmlBufferPtr buf, const char *string) {
3990 xmlBufferCCat(buf, string);
Daniel Veillard260a68f1998-08-13 03:39:55 +00003991}
3992
Daniel Veillard5099ae81999-04-21 20:12:07 +00003993
Daniel Veillard97b58771998-10-20 06:14:16 +00003994/**
Daniel Veillard011b63c1999-06-02 17:44:04 +00003995 * xmlBufferWriteQuotedString:
3996 * @buf: the XML buffer output
3997 * @string: the string to add
3998 *
3999 * routine which manage and grows an output buffer. This one writes
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004000 * a quoted or double quoted xmlChar string, checking first if it holds
Daniel Veillard011b63c1999-06-02 17:44:04 +00004001 * quote or double-quotes internally
4002 */
4003void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004004xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) {
Daniel Veillardb96e6431999-08-29 21:02:19 +00004005 if (xmlStrchr(string, '"')) {
4006 if (xmlStrchr(string, '\'')) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004007#ifdef DEBUG_BUFFER
Daniel Veillard011b63c1999-06-02 17:44:04 +00004008 fprintf(stderr,
4009 "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004010#endif
Daniel Veillard011b63c1999-06-02 17:44:04 +00004011 }
4012 xmlBufferCCat(buf, "'");
4013 xmlBufferCat(buf, string);
4014 xmlBufferCCat(buf, "'");
4015 } else {
4016 xmlBufferCCat(buf, "\"");
4017 xmlBufferCat(buf, string);
4018 xmlBufferCCat(buf, "\"");
4019 }
4020}
4021
4022
Daniel Veillardcf461992000-03-14 18:30:20 +00004023static void
4024xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
4025 int format);
4026static void
4027xmlNodeListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
4028 int format);
4029void
4030htmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur);
4031
Daniel Veillard011b63c1999-06-02 17:44:04 +00004032/**
Daniel Veillard97b58771998-10-20 06:14:16 +00004033 * xmlGlobalNsDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00004034 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00004035 * @cur: a namespace
4036 *
4037 * Dump a global Namespace, this is the old version based on PIs.
Daniel Veillard260a68f1998-08-13 03:39:55 +00004038 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004039static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00004040xmlGlobalNsDump(xmlBufferPtr buf, xmlNsPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00004041 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004042#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00004043 fprintf(stderr, "xmlGlobalNsDump : Ns == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004044#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00004045 return;
4046 }
4047 if (cur->type == XML_GLOBAL_NAMESPACE) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004048 xmlBufferWriteChar(buf, "<?namespace");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004049 if (cur->href != NULL) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00004050 xmlBufferWriteChar(buf, " href=");
4051 xmlBufferWriteQuotedString(buf, cur->href);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004052 }
4053 if (cur->prefix != NULL) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00004054 xmlBufferWriteChar(buf, " AS=");
4055 xmlBufferWriteQuotedString(buf, cur->prefix);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004056 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004057 xmlBufferWriteChar(buf, "?>\n");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004058 }
4059}
4060
Daniel Veillard97b58771998-10-20 06:14:16 +00004061/**
4062 * xmlGlobalNsListDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00004063 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00004064 * @cur: the first namespace
4065 *
4066 * Dump a list of global Namespace, this is the old version based on PIs.
Daniel Veillard260a68f1998-08-13 03:39:55 +00004067 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004068static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00004069xmlGlobalNsListDump(xmlBufferPtr buf, xmlNsPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00004070 while (cur != NULL) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004071 xmlGlobalNsDump(buf, cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004072 cur = cur->next;
4073 }
4074}
4075
Daniel Veillard97b58771998-10-20 06:14:16 +00004076/**
4077 * xmlNsDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00004078 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00004079 * @cur: a namespace
4080 *
Daniel Veillard260a68f1998-08-13 03:39:55 +00004081 * Dump a local Namespace definition.
Daniel Veillard97b58771998-10-20 06:14:16 +00004082 * Should be called in the context of attributes dumps.
Daniel Veillard260a68f1998-08-13 03:39:55 +00004083 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004084static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00004085xmlNsDump(xmlBufferPtr buf, xmlNsPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00004086 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004087#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00004088 fprintf(stderr, "xmlNsDump : Ns == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004089#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00004090 return;
4091 }
4092 if (cur->type == XML_LOCAL_NAMESPACE) {
4093 /* Within the context of an element attributes */
4094 if (cur->prefix != NULL) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004095 xmlBufferWriteChar(buf, " xmlns:");
4096 xmlBufferWriteCHAR(buf, cur->prefix);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004097 } else
Daniel Veillard5099ae81999-04-21 20:12:07 +00004098 xmlBufferWriteChar(buf, " xmlns");
Daniel Veillard011b63c1999-06-02 17:44:04 +00004099 xmlBufferWriteChar(buf, "=");
4100 xmlBufferWriteQuotedString(buf, cur->href);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004101 }
4102}
4103
Daniel Veillard97b58771998-10-20 06:14:16 +00004104/**
4105 * xmlNsListDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00004106 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00004107 * @cur: the first namespace
4108 *
4109 * Dump a list of local Namespace definitions.
4110 * Should be called in the context of attributes dumps.
Daniel Veillard260a68f1998-08-13 03:39:55 +00004111 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004112static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00004113xmlNsListDump(xmlBufferPtr buf, xmlNsPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00004114 while (cur != NULL) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004115 xmlNsDump(buf, cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004116 cur = cur->next;
4117 }
4118}
4119
Daniel Veillard97b58771998-10-20 06:14:16 +00004120/**
4121 * xmlDtdDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00004122 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00004123 * @doc: the document
4124 *
4125 * Dump the XML document DTD, if any.
Daniel Veillard260a68f1998-08-13 03:39:55 +00004126 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004127static void
Daniel Veillardcf461992000-03-14 18:30:20 +00004128xmlDtdDump(xmlBufferPtr buf, xmlDtdPtr dtd) {
4129 if (dtd == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004130#ifdef DEBUG_TREE
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00004131 fprintf(stderr, "xmlDtdDump : no internal subset\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004132#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00004133 return;
4134 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004135 xmlBufferWriteChar(buf, "<!DOCTYPE ");
Daniel Veillardcf461992000-03-14 18:30:20 +00004136 xmlBufferWriteCHAR(buf, dtd->name);
4137 if (dtd->ExternalID != NULL) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00004138 xmlBufferWriteChar(buf, " PUBLIC ");
Daniel Veillardcf461992000-03-14 18:30:20 +00004139 xmlBufferWriteQuotedString(buf, dtd->ExternalID);
Daniel Veillard011b63c1999-06-02 17:44:04 +00004140 xmlBufferWriteChar(buf, " ");
Daniel Veillardcf461992000-03-14 18:30:20 +00004141 xmlBufferWriteQuotedString(buf, dtd->SystemID);
4142 } else if (dtd->SystemID != NULL) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00004143 xmlBufferWriteChar(buf, " SYSTEM ");
Daniel Veillardcf461992000-03-14 18:30:20 +00004144 xmlBufferWriteQuotedString(buf, dtd->SystemID);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004145 }
Daniel Veillardcf461992000-03-14 18:30:20 +00004146 if ((dtd->entities == NULL) && (dtd->elements == NULL) &&
4147 (dtd->attributes == NULL) && (dtd->notations == NULL)) {
4148 xmlBufferWriteChar(buf, ">");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004149 return;
4150 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004151 xmlBufferWriteChar(buf, " [\n");
Daniel Veillardcf461992000-03-14 18:30:20 +00004152 xmlNodeListDump(buf, dtd->doc, dtd->children, -1, 0);
4153#if 0
4154 if (dtd->entities != NULL)
4155 xmlDumpEntitiesTable(buf, (xmlEntitiesTablePtr) dtd->entities);
4156 if (dtd->notations != NULL)
4157 xmlDumpNotationTable(buf, (xmlNotationTablePtr) dtd->notations);
4158 if (dtd->elements != NULL)
4159 xmlDumpElementTable(buf, (xmlElementTablePtr) dtd->elements);
4160 if (dtd->attributes != NULL)
4161 xmlDumpAttributeTable(buf, (xmlAttributeTablePtr) dtd->attributes);
4162#endif
4163 xmlBufferWriteChar(buf, "]>");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004164}
4165
Daniel Veillard97b58771998-10-20 06:14:16 +00004166/**
4167 * xmlAttrDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00004168 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00004169 * @doc: the document
4170 * @cur: the attribute pointer
4171 *
4172 * Dump an XML attribute
Daniel Veillard260a68f1998-08-13 03:39:55 +00004173 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004174static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00004175xmlAttrDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) {
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004176 xmlChar *value;
Daniel Veillardccb09631998-10-27 06:21:04 +00004177
Daniel Veillard260a68f1998-08-13 03:39:55 +00004178 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004179#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00004180 fprintf(stderr, "xmlAttrDump : property == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004181#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00004182 return;
4183 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004184 xmlBufferWriteChar(buf, " ");
Daniel Veillardb96e6431999-08-29 21:02:19 +00004185 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
4186 xmlBufferWriteCHAR(buf, cur->ns->prefix);
4187 xmlBufferWriteChar(buf, ":");
4188 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004189 xmlBufferWriteCHAR(buf, cur->name);
Daniel Veillardcf461992000-03-14 18:30:20 +00004190 value = xmlNodeListGetString(doc, cur->children, 0);
Daniel Veillardccb09631998-10-27 06:21:04 +00004191 if (value) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00004192 xmlBufferWriteChar(buf, "=");
4193 xmlBufferWriteQuotedString(buf, value);
Daniel Veillard6454aec1999-09-02 22:04:43 +00004194 xmlFree(value);
Daniel Veillard726c7e31999-02-08 15:13:10 +00004195 } else {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004196 xmlBufferWriteChar(buf, "=\"\"");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004197 }
4198}
4199
Daniel Veillard97b58771998-10-20 06:14:16 +00004200/**
4201 * xmlAttrListDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00004202 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00004203 * @doc: the document
4204 * @cur: the first attribute pointer
4205 *
4206 * Dump a list of XML attributes
Daniel Veillard260a68f1998-08-13 03:39:55 +00004207 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004208static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00004209xmlAttrListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00004210 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004211#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00004212 fprintf(stderr, "xmlAttrListDump : property == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004213#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00004214 return;
4215 }
4216 while (cur != NULL) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004217 xmlAttrDump(buf, doc, cur);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004218 cur = cur->next;
4219 }
4220}
4221
Daniel Veillard260a68f1998-08-13 03:39:55 +00004222
Daniel Veillarddbfd6411999-12-28 16:35:14 +00004223
Daniel Veillard97b58771998-10-20 06:14:16 +00004224/**
4225 * xmlNodeListDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00004226 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00004227 * @doc: the document
4228 * @cur: the first node
Daniel Veillardcf461992000-03-14 18:30:20 +00004229 * @level: the imbrication level for indenting
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004230 * @format: is formatting allowed
Daniel Veillard97b58771998-10-20 06:14:16 +00004231 *
4232 * Dump an XML node list, recursive behaviour,children are printed too.
4233 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004234static void
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004235xmlNodeListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
4236 int format) {
4237 int i;
Daniel Veillardccb09631998-10-27 06:21:04 +00004238
Daniel Veillard260a68f1998-08-13 03:39:55 +00004239 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004240#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00004241 fprintf(stderr, "xmlNodeListDump : node == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004242#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00004243 return;
4244 }
4245 while (cur != NULL) {
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004246 if ((format) && (xmlIndentTreeOutput) &&
4247 (cur->type == XML_ELEMENT_NODE))
4248 for (i = 0;i < level;i++)
4249 xmlBufferWriteChar(buf, " ");
4250 xmlNodeDump(buf, doc, cur, level, format);
4251 if (format) {
4252 xmlBufferWriteChar(buf, "\n");
Daniel Veillardccb09631998-10-27 06:21:04 +00004253 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00004254 cur = cur->next;
4255 }
4256}
4257
Daniel Veillard97b58771998-10-20 06:14:16 +00004258/**
Daniel Veillardccb09631998-10-27 06:21:04 +00004259 * xmlNodeDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00004260 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00004261 * @doc: the document
4262 * @cur: the current node
Daniel Veillardcf461992000-03-14 18:30:20 +00004263 * @level: the imbrication level for indenting
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004264 * @format: is formatting allowed
Daniel Veillard97b58771998-10-20 06:14:16 +00004265 *
4266 * Dump an XML node, recursive behaviour,children are printed too.
Daniel Veillard260a68f1998-08-13 03:39:55 +00004267 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004268static void
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004269xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
4270 int format) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00004271 int i;
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004272 xmlNodePtr tmp;
Daniel Veillard260a68f1998-08-13 03:39:55 +00004273
4274 if (cur == NULL) {
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004275#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00004276 fprintf(stderr, "xmlNodeDump : node == NULL\n");
Daniel Veillardad8f99d2000-01-15 14:20:03 +00004277#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00004278 return;
4279 }
Daniel Veillardcf461992000-03-14 18:30:20 +00004280 if (cur->type == XML_DTD_NODE) {
4281 xmlDtdDump(buf, (xmlDtdPtr) cur);
4282 return;
4283 }
4284 if (cur->type == XML_ELEMENT_DECL) {
4285 xmlDumpElementDecl(buf, (xmlElementPtr) cur);
4286 return;
4287 }
4288 if (cur->type == XML_ATTRIBUTE_DECL) {
4289 xmlDumpAttributeDecl(buf, (xmlAttributePtr) cur);
4290 return;
4291 }
4292 if (cur->type == XML_ENTITY_DECL) {
4293 xmlDumpEntityDecl(buf, (xmlEntityPtr) cur);
4294 return;
4295 }
Daniel Veillard0bef1311998-10-14 02:36:47 +00004296 if (cur->type == XML_TEXT_NODE) {
Daniel Veillard14fff061999-06-22 21:49:07 +00004297 if (cur->content != NULL) {
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004298 xmlChar *buffer;
Daniel Veillard14fff061999-06-22 21:49:07 +00004299
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004300#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard14fff061999-06-22 21:49:07 +00004301 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004302#else
4303 buffer = xmlEncodeEntitiesReentrant(doc,
4304 xmlBufferContent(cur->content));
4305#endif
Daniel Veillard14fff061999-06-22 21:49:07 +00004306 if (buffer != NULL) {
4307 xmlBufferWriteCHAR(buf, buffer);
Daniel Veillard6454aec1999-09-02 22:04:43 +00004308 xmlFree(buffer);
Daniel Veillard14fff061999-06-22 21:49:07 +00004309 }
4310 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00004311 return;
4312 }
Daniel Veillardb96e6431999-08-29 21:02:19 +00004313 if (cur->type == XML_PI_NODE) {
4314 if (cur->content != NULL) {
4315 xmlBufferWriteChar(buf, "<?");
4316 xmlBufferWriteCHAR(buf, cur->name);
4317 if (cur->content != NULL) {
4318 xmlBufferWriteChar(buf, " ");
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004319#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardb96e6431999-08-29 21:02:19 +00004320 xmlBufferWriteCHAR(buf, cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004321#else
4322 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
4323#endif
Daniel Veillardb96e6431999-08-29 21:02:19 +00004324 }
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004325 xmlBufferWriteChar(buf, "?>");
Daniel Veillardcf461992000-03-14 18:30:20 +00004326 } else {
4327 xmlBufferWriteChar(buf, "<?");
4328 xmlBufferWriteCHAR(buf, cur->name);
4329 xmlBufferWriteChar(buf, "?>");
Daniel Veillardb96e6431999-08-29 21:02:19 +00004330 }
4331 return;
4332 }
Daniel Veillard0bef1311998-10-14 02:36:47 +00004333 if (cur->type == XML_COMMENT_NODE) {
Daniel Veillard260a68f1998-08-13 03:39:55 +00004334 if (cur->content != NULL) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004335 xmlBufferWriteChar(buf, "<!--");
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004336#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard5099ae81999-04-21 20:12:07 +00004337 xmlBufferWriteCHAR(buf, cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004338#else
4339 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
4340#endif
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004341 xmlBufferWriteChar(buf, "-->");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004342 }
4343 return;
4344 }
Daniel Veillardccb09631998-10-27 06:21:04 +00004345 if (cur->type == XML_ENTITY_REF_NODE) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004346 xmlBufferWriteChar(buf, "&");
4347 xmlBufferWriteCHAR(buf, cur->name);
4348 xmlBufferWriteChar(buf, ";");
Daniel Veillardccb09631998-10-27 06:21:04 +00004349 return;
4350 }
Daniel Veillardb05deb71999-08-10 19:04:08 +00004351 if (cur->type == XML_CDATA_SECTION_NODE) {
4352 xmlBufferWriteChar(buf, "<![CDATA[");
4353 if (cur->content != NULL)
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004354#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillardb05deb71999-08-10 19:04:08 +00004355 xmlBufferWriteCHAR(buf, cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004356#else
4357 xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content));
4358#endif
Daniel Veillardb05deb71999-08-10 19:04:08 +00004359 xmlBufferWriteChar(buf, "]]>");
4360 return;
4361 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00004362
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004363 if (format == 1) {
Daniel Veillardcf461992000-03-14 18:30:20 +00004364 tmp = cur->children;
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004365 while (tmp != NULL) {
4366 if ((tmp->type == XML_TEXT_NODE) ||
4367 (tmp->type == XML_ENTITY_REF_NODE)) {
4368 format = 0;
4369 break;
4370 }
4371 tmp = tmp->next;
4372 }
4373 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004374 xmlBufferWriteChar(buf, "<");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004375 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004376 xmlBufferWriteCHAR(buf, cur->ns->prefix);
4377 xmlBufferWriteChar(buf, ":");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004378 }
4379
Daniel Veillard5099ae81999-04-21 20:12:07 +00004380 xmlBufferWriteCHAR(buf, cur->name);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004381 if (cur->nsDef)
Daniel Veillard5099ae81999-04-21 20:12:07 +00004382 xmlNsListDump(buf, cur->nsDef);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004383 if (cur->properties != NULL)
Daniel Veillard5099ae81999-04-21 20:12:07 +00004384 xmlAttrListDump(buf, doc, cur->properties);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004385
Daniel Veillardcf461992000-03-14 18:30:20 +00004386 if ((cur->content == NULL) && (cur->children == NULL) &&
Daniel Veillarde41f2b72000-01-30 20:00:07 +00004387 (!xmlSaveNoEmptyTags)) {
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004388 xmlBufferWriteChar(buf, "/>");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004389 return;
4390 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004391 xmlBufferWriteChar(buf, ">");
Daniel Veillard14fff061999-06-22 21:49:07 +00004392 if (cur->content != NULL) {
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004393 xmlChar *buffer;
Daniel Veillard14fff061999-06-22 21:49:07 +00004394
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004395#ifndef XML_USE_BUFFER_CONTENT
Daniel Veillard14fff061999-06-22 21:49:07 +00004396 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
Daniel Veillardf5c2c871999-12-01 09:51:45 +00004397#else
4398 buffer = xmlEncodeEntitiesReentrant(doc,
4399 xmlBufferContent(cur->content));
4400#endif
Daniel Veillard14fff061999-06-22 21:49:07 +00004401 if (buffer != NULL) {
4402 xmlBufferWriteCHAR(buf, buffer);
Daniel Veillard6454aec1999-09-02 22:04:43 +00004403 xmlFree(buffer);
Daniel Veillard14fff061999-06-22 21:49:07 +00004404 }
4405 }
Daniel Veillardcf461992000-03-14 18:30:20 +00004406 if (cur->children != NULL) {
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004407 if (format) xmlBufferWriteChar(buf, "\n");
Daniel Veillardcf461992000-03-14 18:30:20 +00004408 xmlNodeListDump(buf, doc, cur->children,
Daniel Veillard3e6d2372000-03-04 11:39:43 +00004409 (level >= 0?level+1:-1), format);
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004410 if ((xmlIndentTreeOutput) && (format))
4411 for (i = 0;i < level;i++)
4412 xmlBufferWriteChar(buf, " ");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004413 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004414 xmlBufferWriteChar(buf, "</");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004415 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004416 xmlBufferWriteCHAR(buf, cur->ns->prefix);
4417 xmlBufferWriteChar(buf, ":");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004418 }
4419
Daniel Veillard5099ae81999-04-21 20:12:07 +00004420 xmlBufferWriteCHAR(buf, cur->name);
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004421 xmlBufferWriteChar(buf, ">");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004422}
4423
Daniel Veillard97b58771998-10-20 06:14:16 +00004424/**
Daniel Veillarddbfd6411999-12-28 16:35:14 +00004425 * xmlElemDump:
Daniel Veillard06047432000-04-24 11:33:38 +00004426 * @f: the FILE * for the output
Daniel Veillarddbfd6411999-12-28 16:35:14 +00004427 * @doc: the document
4428 * @cur: the current node
4429 *
4430 * Dump an XML/HTML node, recursive behaviour,children are printed too.
4431 */
4432void
4433xmlElemDump(FILE *f, xmlDocPtr doc, xmlNodePtr cur) {
4434 xmlBufferPtr buf;
4435
4436 if (cur == NULL) {
4437#ifdef DEBUG_TREE
4438 fprintf(stderr, "xmlElemDump : cur == NULL\n");
4439#endif
4440 return;
4441 }
4442 if (doc == NULL) {
4443#ifdef DEBUG_TREE
4444 fprintf(stderr, "xmlElemDump : doc == NULL\n");
4445#endif
4446 }
4447 buf = xmlBufferCreate();
4448 if (buf == NULL) return;
4449 if ((doc != NULL) &&
4450 (doc->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard361d8452000-04-03 19:48:13 +00004451#ifdef LIBXML_HTML_ENABLED
Daniel Veillarddbfd6411999-12-28 16:35:14 +00004452 htmlNodeDump(buf, doc, cur);
Daniel Veillard361d8452000-04-03 19:48:13 +00004453#else
4454 printf("HTML support not compiled in\n");
4455#endif /* LIBXML_HTML_ENABLED */
Daniel Veillarddbfd6411999-12-28 16:35:14 +00004456 } else
4457 xmlNodeDump(buf, doc, cur, 0, 1);
4458 xmlBufferDump(f, buf);
4459 xmlBufferFree(buf);
4460}
4461
4462/**
Daniel Veillard97b58771998-10-20 06:14:16 +00004463 * xmlDocContentDump:
Daniel Veillard011b63c1999-06-02 17:44:04 +00004464 * @buf: the XML buffer output
Daniel Veillard97b58771998-10-20 06:14:16 +00004465 * @cur: the document
4466 *
4467 * Dump an XML document.
Daniel Veillard260a68f1998-08-13 03:39:55 +00004468 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004469static void
Daniel Veillard5099ae81999-04-21 20:12:07 +00004470xmlDocContentDump(xmlBufferPtr buf, xmlDocPtr cur) {
Daniel Veillardbe70ff71999-07-05 16:50:46 +00004471 xmlBufferWriteChar(buf, "<?xml version=");
4472 if (cur->version != NULL)
4473 xmlBufferWriteQuotedString(buf, cur->version);
4474 else
4475 xmlBufferWriteChar(buf, "\"1.0\"");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004476 if (cur->encoding != NULL) {
Daniel Veillard011b63c1999-06-02 17:44:04 +00004477 xmlBufferWriteChar(buf, " encoding=");
4478 xmlBufferWriteQuotedString(buf, cur->encoding);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004479 }
4480 switch (cur->standalone) {
4481 case 0:
Daniel Veillard5099ae81999-04-21 20:12:07 +00004482 xmlBufferWriteChar(buf, " standalone=\"no\"");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004483 break;
4484 case 1:
Daniel Veillard5099ae81999-04-21 20:12:07 +00004485 xmlBufferWriteChar(buf, " standalone=\"yes\"");
Daniel Veillard260a68f1998-08-13 03:39:55 +00004486 break;
4487 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004488 xmlBufferWriteChar(buf, "?>\n");
Daniel Veillardcf461992000-03-14 18:30:20 +00004489 if (cur->children != NULL) {
4490 xmlNodePtr child = cur->children;
Daniel Veillardb96e6431999-08-29 21:02:19 +00004491
Daniel Veillard260a68f1998-08-13 03:39:55 +00004492 /* global namespace definitions, the old way */
4493 if (oldXMLWDcompatibility)
Daniel Veillard5099ae81999-04-21 20:12:07 +00004494 xmlGlobalNsListDump(buf, cur->oldNs);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004495 else
4496 xmlUpgradeOldNs(cur);
Daniel Veillardb96e6431999-08-29 21:02:19 +00004497
4498 while (child != NULL) {
Daniel Veillard7d2c2761999-10-11 15:09:51 +00004499 xmlNodeDump(buf, cur, child, 0, 1);
4500 xmlBufferWriteChar(buf, "\n");
Daniel Veillardb96e6431999-08-29 21:02:19 +00004501 child = child->next;
4502 }
Daniel Veillard260a68f1998-08-13 03:39:55 +00004503 }
4504}
4505
Daniel Veillard97b58771998-10-20 06:14:16 +00004506/**
4507 * xmlDocDumpMemory:
4508 * @cur: the document
4509 * @mem: OUT: the memory pointer
4510 * @size: OUT: the memory lenght
4511 *
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004512 * Dump an XML document in memory and return the xmlChar * and it's size.
Daniel Veillard97b58771998-10-20 06:14:16 +00004513 * It's up to the caller to free the memory.
Daniel Veillard260a68f1998-08-13 03:39:55 +00004514 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004515void
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004516xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004517 xmlBufferPtr buf;
4518
Daniel Veillard260a68f1998-08-13 03:39:55 +00004519 if (cur == NULL) {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00004520#ifdef DEBUG_TREE
4521 fprintf(stderr, "xmlDocDumpMemory : document == NULL\n");
4522#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00004523 *mem = NULL;
4524 *size = 0;
4525 return;
4526 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004527 buf = xmlBufferCreate();
4528 if (buf == NULL) {
4529 *mem = NULL;
4530 *size = 0;
4531 return;
4532 }
4533 xmlDocContentDump(buf, cur);
Daniel Veillardb05deb71999-08-10 19:04:08 +00004534 *mem = xmlStrndup(buf->content, buf->use);
Daniel Veillard5099ae81999-04-21 20:12:07 +00004535 *size = buf->use;
Daniel Veillardb05deb71999-08-10 19:04:08 +00004536 xmlBufferFree(buf);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004537}
4538
Daniel Veillard97b58771998-10-20 06:14:16 +00004539/**
4540 * xmlGetDocCompressMode:
4541 * @doc: the document
4542 *
4543 * get the compression ratio for a document, ZLIB based
Daniel Veillard1e346af1999-02-22 10:33:01 +00004544 * Returns 0 (uncompressed) to 9 (max compression)
Daniel Veillard151b1b01998-09-23 00:49:46 +00004545 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004546int
Daniel Veillarddbfd6411999-12-28 16:35:14 +00004547xmlGetDocCompressMode (xmlDocPtr doc) {
Daniel Veillard15a8df41998-09-24 19:15:06 +00004548 if (doc == NULL) return(-1);
4549 return(doc->compression);
4550}
4551
Daniel Veillard97b58771998-10-20 06:14:16 +00004552/**
4553 * xmlSetDocCompressMode:
4554 * @doc: the document
4555 * @mode: the compression ratio
4556 *
4557 * set the compression ratio for a document, ZLIB based
4558 * Correct values: 0 (uncompressed) to 9 (max compression)
4559 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004560void
4561xmlSetDocCompressMode (xmlDocPtr doc, int mode) {
Daniel Veillard15a8df41998-09-24 19:15:06 +00004562 if (doc == NULL) return;
4563 if (mode < 0) doc->compression = 0;
4564 else if (mode > 9) doc->compression = 9;
4565 else doc->compression = mode;
4566}
4567
Daniel Veillard97b58771998-10-20 06:14:16 +00004568/**
4569 * xmlGetCompressMode:
4570 *
4571 * get the default compression mode used, ZLIB based.
Daniel Veillard1e346af1999-02-22 10:33:01 +00004572 * Returns 0 (uncompressed) to 9 (max compression)
Daniel Veillard15a8df41998-09-24 19:15:06 +00004573 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004574int
4575 xmlGetCompressMode(void) {
Daniel Veillard151b1b01998-09-23 00:49:46 +00004576 return(xmlCompressMode);
4577}
Daniel Veillard97b58771998-10-20 06:14:16 +00004578
4579/**
4580 * xmlSetCompressMode:
4581 * @mode: the compression ratio
4582 *
4583 * set the default compression mode used, ZLIB based
4584 * Correct values: 0 (uncompressed) to 9 (max compression)
4585 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004586void
4587xmlSetCompressMode(int mode) {
Daniel Veillard151b1b01998-09-23 00:49:46 +00004588 if (mode < 0) xmlCompressMode = 0;
Daniel Veillard15a8df41998-09-24 19:15:06 +00004589 else if (mode > 9) xmlCompressMode = 9;
Daniel Veillard151b1b01998-09-23 00:49:46 +00004590 else xmlCompressMode = mode;
4591}
4592
Daniel Veillard97b58771998-10-20 06:14:16 +00004593/**
4594 * xmlDocDump:
4595 * @f: the FILE*
4596 * @cur: the document
4597 *
4598 * Dump an XML document to an open FILE.
Daniel Veillard260a68f1998-08-13 03:39:55 +00004599 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004600void
4601xmlDocDump(FILE *f, xmlDocPtr cur) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004602 xmlBufferPtr buf;
4603
Daniel Veillard260a68f1998-08-13 03:39:55 +00004604 if (cur == NULL) {
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00004605#ifdef DEBUG_TREE
Daniel Veillard260a68f1998-08-13 03:39:55 +00004606 fprintf(stderr, "xmlDocDump : document == NULL\n");
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00004607#endif
Daniel Veillard260a68f1998-08-13 03:39:55 +00004608 return;
4609 }
Daniel Veillard5099ae81999-04-21 20:12:07 +00004610 buf = xmlBufferCreate();
4611 if (buf == NULL) return;
4612 xmlDocContentDump(buf, cur);
4613 xmlBufferDump(f, buf);
4614 xmlBufferFree(buf);
Daniel Veillard260a68f1998-08-13 03:39:55 +00004615}
4616
Daniel Veillard97b58771998-10-20 06:14:16 +00004617/**
4618 * xmlSaveFile:
4619 * @filename: the filename
4620 * @cur: the document
4621 *
4622 * Dump an XML document to a file. Will use compression if
Daniel Veillard11a48ec1999-11-23 10:40:46 +00004623 * compiled in and enabled. If @filename is "-" the stdout file is
4624 * used.
Daniel Veillard97b58771998-10-20 06:14:16 +00004625 * returns: the number of file written or -1 in case of failure.
Daniel Veillard151b1b01998-09-23 00:49:46 +00004626 */
Daniel Veillardbaf4cd51998-10-27 22:56:57 +00004627int
4628xmlSaveFile(const char *filename, xmlDocPtr cur) {
Daniel Veillard5099ae81999-04-21 20:12:07 +00004629 xmlBufferPtr buf;
Daniel Veillard151b1b01998-09-23 00:49:46 +00004630#ifdef HAVE_ZLIB_H
4631 gzFile zoutput = NULL;
4632 char mode[15];
4633#endif
Daniel Veillardccb09631998-10-27 06:21:04 +00004634 FILE *output = NULL;
Daniel Veillard151b1b01998-09-23 00:49:46 +00004635 int ret;
4636
Daniel Veillard5099ae81999-04-21 20:12:07 +00004637 /*
4638 * save the content to a temp buffer.
4639 */
4640 buf = xmlBufferCreate();
4641 if (buf == NULL) return(0);
4642 xmlDocContentDump(buf, cur);
4643
Daniel Veillard151b1b01998-09-23 00:49:46 +00004644#ifdef HAVE_ZLIB_H
Daniel Veillard11a48ec1999-11-23 10:40:46 +00004645 if (cur->compression < 0) cur->compression = xmlCompressMode;
Daniel Veillarddc3dd9d1998-09-24 19:25:54 +00004646 if ((cur->compression > 0) && (cur->compression <= 9)) {
4647 sprintf(mode, "w%d", cur->compression);
Daniel Veillard11a48ec1999-11-23 10:40:46 +00004648 if (!strcmp(filename, "-"))
4649 zoutput = gzdopen(1, mode);
4650 else
4651 zoutput = gzopen(filename, mode);
Daniel Veillard151b1b01998-09-23 00:49:46 +00004652 }
4653 if (zoutput == NULL) {
4654#endif
4655 output = fopen(filename, "w");
Daniel Veillard10a2c651999-12-12 13:03:50 +00004656 if (output == NULL) {
4657 xmlBufferFree(buf);
4658 return(-1);
4659 }
Daniel Veillard151b1b01998-09-23 00:49:46 +00004660#ifdef HAVE_ZLIB_H
4661 }
Daniel Veillard151b1b01998-09-23 00:49:46 +00004662
Daniel Veillard151b1b01998-09-23 00:49:46 +00004663 if (zoutput != NULL) {
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004664 ret = gzwrite(zoutput, buf->content, sizeof(xmlChar) * buf->use);
Daniel Veillard151b1b01998-09-23 00:49:46 +00004665 gzclose(zoutput);
Daniel Veillard5099ae81999-04-21 20:12:07 +00004666 } else {
4667#endif
4668 ret = xmlBufferDump(output, buf);
4669 fclose(output);
4670#ifdef HAVE_ZLIB_H
Daniel Veillard151b1b01998-09-23 00:49:46 +00004671 }
4672#endif
Manish Vachharajani5e60f5a1999-05-29 03:04:30 +00004673 xmlBufferFree(buf);
Daniel Veillarddd6b3671999-09-23 22:19:22 +00004674 return(ret * sizeof(xmlChar));
Daniel Veillard151b1b01998-09-23 00:49:46 +00004675}
4676