blob: 2a61577f697c8e0abd15d5fe2a2de5ab12049e74 [file] [log] [blame]
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001/*
2 * xmlsave.c: Implemetation of the document serializer
3 *
4 * See Copyright for the status of this software.
5 *
6 * daniel@veillard.com
7 */
8
9#define IN_LIBXML
10#include "libxml.h"
11
12#include <string.h>
13#include <libxml/xmlmemory.h>
14#include <libxml/parserInternals.h>
15#include <libxml/tree.h>
16#include <libxml/xmlsave.h>
Daniel Veillard1a8741c2004-03-04 13:40:59 +000017
Daniel Veillard753086a2004-03-28 16:12:44 +000018#define MAX_INDENT 60
19
Daniel Veillard656ce942004-04-30 23:11:45 +000020#include <libxml/HTMLtree.h>
21
Daniel Veillard1a8741c2004-03-04 13:40:59 +000022/************************************************************************
23 * *
24 * XHTML detection *
25 * *
26 ************************************************************************/
27#define XHTML_STRICT_PUBLIC_ID BAD_CAST \
28 "-//W3C//DTD XHTML 1.0 Strict//EN"
29#define XHTML_STRICT_SYSTEM_ID BAD_CAST \
30 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
31#define XHTML_FRAME_PUBLIC_ID BAD_CAST \
32 "-//W3C//DTD XHTML 1.0 Frameset//EN"
33#define XHTML_FRAME_SYSTEM_ID BAD_CAST \
34 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"
35#define XHTML_TRANS_PUBLIC_ID BAD_CAST \
36 "-//W3C//DTD XHTML 1.0 Transitional//EN"
37#define XHTML_TRANS_SYSTEM_ID BAD_CAST \
38 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
39
40#define XHTML_NS_NAME BAD_CAST "http://www.w3.org/1999/xhtml"
41/**
42 * xmlIsXHTML:
43 * @systemID: the system identifier
44 * @publicID: the public identifier
45 *
46 * Try to find if the document correspond to an XHTML DTD
47 *
48 * Returns 1 if true, 0 if not and -1 in case of error
49 */
50int
51xmlIsXHTML(const xmlChar *systemID, const xmlChar *publicID) {
52 if ((systemID == NULL) && (publicID == NULL))
53 return(-1);
54 if (publicID != NULL) {
55 if (xmlStrEqual(publicID, XHTML_STRICT_PUBLIC_ID)) return(1);
56 if (xmlStrEqual(publicID, XHTML_FRAME_PUBLIC_ID)) return(1);
57 if (xmlStrEqual(publicID, XHTML_TRANS_PUBLIC_ID)) return(1);
58 }
59 if (systemID != NULL) {
60 if (xmlStrEqual(systemID, XHTML_STRICT_SYSTEM_ID)) return(1);
61 if (xmlStrEqual(systemID, XHTML_FRAME_SYSTEM_ID)) return(1);
62 if (xmlStrEqual(systemID, XHTML_TRANS_SYSTEM_ID)) return(1);
63 }
64 return(0);
65}
Daniel Veillard1a8741c2004-03-04 13:40:59 +000066
67#ifdef LIBXML_OUTPUT_ENABLED
68
69#define TODO \
70 xmlGenericError(xmlGenericErrorContext, \
71 "Unimplemented block at %s:%d\n", \
72 __FILE__, __LINE__);
73
74struct _xmlSaveCtxt {
75 void *_private;
76 int type;
77 int fd;
78 const xmlChar *filename;
79 const xmlChar *encoding;
80 xmlCharEncodingHandlerPtr handler;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +000081 xmlOutputBufferPtr buf;
82 xmlDocPtr doc;
Daniel Veillard1a8741c2004-03-04 13:40:59 +000083 int options;
84 int level;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +000085 int format;
Daniel Veillard3995bc32004-05-15 18:57:31 +000086 char indent[MAX_INDENT + 1]; /* array for indenting output */
Daniel Veillard753086a2004-03-28 16:12:44 +000087 int indent_nr;
88 int indent_size;
Daniel Veillard3995bc32004-05-15 18:57:31 +000089 xmlCharEncodingOutputFunc escape; /* used for element content */
90 xmlCharEncodingOutputFunc escapeAttr;/* used for attribute content */
Daniel Veillard1a8741c2004-03-04 13:40:59 +000091};
92
93/************************************************************************
94 * *
95 * Output error handlers *
96 * *
97 ************************************************************************/
98/**
99 * xmlSaveErrMemory:
100 * @extra: extra informations
101 *
102 * Handle an out of memory condition
103 */
104static void
105xmlSaveErrMemory(const char *extra)
106{
107 __xmlSimpleError(XML_FROM_OUTPUT, XML_ERR_NO_MEMORY, NULL, NULL, extra);
108}
109
110/**
111 * xmlSaveErr:
112 * @code: the error number
113 * @node: the location of the error.
114 * @extra: extra informations
115 *
116 * Handle an out of memory condition
117 */
118static void
119xmlSaveErr(int code, xmlNodePtr node, const char *extra)
120{
121 const char *msg = NULL;
122
123 switch(code) {
124 case XML_SAVE_NOT_UTF8:
Rob Richards417b74d2006-08-15 23:14:24 +0000125 msg = "string is not in UTF-8\n";
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000126 break;
127 case XML_SAVE_CHAR_INVALID:
Rob Richards417b74d2006-08-15 23:14:24 +0000128 msg = "invalid character value\n";
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000129 break;
130 case XML_SAVE_UNKNOWN_ENCODING:
Rob Richards417b74d2006-08-15 23:14:24 +0000131 msg = "unknown encoding %s\n";
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000132 break;
133 case XML_SAVE_NO_DOCTYPE:
Rob Richards417b74d2006-08-15 23:14:24 +0000134 msg = "document has no DOCTYPE\n";
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000135 break;
136 default:
Rob Richards417b74d2006-08-15 23:14:24 +0000137 msg = "unexpected error number\n";
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000138 }
139 __xmlSimpleError(XML_FROM_OUTPUT, code, node, msg, extra);
140}
141
142/************************************************************************
143 * *
Daniel Veillard83a75e02004-05-14 21:50:42 +0000144 * Special escaping routines *
145 * *
146 ************************************************************************/
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000147static unsigned char *
148xmlSerializeHexCharRef(unsigned char *out, int val) {
149 unsigned char *ptr;
150
151 *out++ = '&';
152 *out++ = '#';
153 *out++ = 'x';
154 if (val < 0x10) ptr = out;
155 else if (val < 0x100) ptr = out + 1;
156 else if (val < 0x1000) ptr = out + 2;
157 else if (val < 0x10000) ptr = out + 3;
158 else if (val < 0x100000) ptr = out + 4;
159 else ptr = out + 5;
160 out = ptr + 1;
161 while (val > 0) {
162 switch (val & 0xF) {
163 case 0: *ptr-- = '0'; break;
164 case 1: *ptr-- = '1'; break;
165 case 2: *ptr-- = '2'; break;
166 case 3: *ptr-- = '3'; break;
167 case 4: *ptr-- = '4'; break;
168 case 5: *ptr-- = '5'; break;
169 case 6: *ptr-- = '6'; break;
170 case 7: *ptr-- = '7'; break;
171 case 8: *ptr-- = '8'; break;
172 case 9: *ptr-- = '9'; break;
173 case 0xA: *ptr-- = 'A'; break;
174 case 0xB: *ptr-- = 'B'; break;
175 case 0xC: *ptr-- = 'C'; break;
176 case 0xD: *ptr-- = 'D'; break;
177 case 0xE: *ptr-- = 'E'; break;
178 case 0xF: *ptr-- = 'F'; break;
179 default: *ptr-- = '0'; break;
180 }
181 val >>= 4;
182 }
183 *out++ = ';';
184 *out = 0;
185 return(out);
186}
187
Daniel Veillard83a75e02004-05-14 21:50:42 +0000188/**
189 * xmlEscapeEntities:
190 * @out: a pointer to an array of bytes to store the result
191 * @outlen: the length of @out
192 * @in: a pointer to an array of unescaped UTF-8 bytes
193 * @inlen: the length of @in
194 *
195 * Take a block of UTF-8 chars in and escape them. Used when there is no
196 * encoding specified.
197 *
198 * Returns 0 if success, or -1 otherwise
199 * The value of @inlen after return is the number of octets consumed
200 * if the return value is positive, else unpredictable.
201 * The value of @outlen after return is the number of octets consumed.
202 */
203static int
204xmlEscapeEntities(unsigned char* out, int *outlen,
205 const xmlChar* in, int *inlen) {
206 unsigned char* outstart = out;
207 const unsigned char* base = in;
208 unsigned char* outend = out + *outlen;
209 const unsigned char* inend;
210 int val;
211
212 inend = in + (*inlen);
213
214 while ((in < inend) && (out < outend)) {
215 if (*in == '<') {
216 if (outend - out < 4) break;
217 *out++ = '&';
218 *out++ = 'l';
219 *out++ = 't';
220 *out++ = ';';
221 in++;
222 continue;
223 } else if (*in == '>') {
224 if (outend - out < 4) break;
225 *out++ = '&';
226 *out++ = 'g';
227 *out++ = 't';
228 *out++ = ';';
229 in++;
230 continue;
231 } else if (*in == '&') {
232 if (outend - out < 5) break;
233 *out++ = '&';
234 *out++ = 'a';
235 *out++ = 'm';
236 *out++ = 'p';
237 *out++ = ';';
238 in++;
239 continue;
240 } else if (((*in >= 0x20) && (*in < 0x80)) ||
241 (*in == '\n') || (*in == '\t')) {
242 /*
243 * default case, just copy !
244 */
245 *out++ = *in++;
246 continue;
247 } else if (*in >= 0x80) {
248 /*
249 * We assume we have UTF-8 input.
250 */
Daniel Veillard83a75e02004-05-14 21:50:42 +0000251 if (outend - out < 10) break;
252
253 if (*in < 0xC0) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000254 xmlSaveErr(XML_SAVE_NOT_UTF8, NULL, NULL);
Daniel Veillard83a75e02004-05-14 21:50:42 +0000255 in++;
256 goto error;
257 } else if (*in < 0xE0) {
258 if (inend - in < 2) break;
259 val = (in[0]) & 0x1F;
260 val <<= 6;
261 val |= (in[1]) & 0x3F;
262 in += 2;
263 } else if (*in < 0xF0) {
264 if (inend - in < 3) break;
265 val = (in[0]) & 0x0F;
266 val <<= 6;
267 val |= (in[1]) & 0x3F;
268 val <<= 6;
269 val |= (in[2]) & 0x3F;
270 in += 3;
271 } else if (*in < 0xF8) {
272 if (inend - in < 4) break;
273 val = (in[0]) & 0x07;
274 val <<= 6;
275 val |= (in[1]) & 0x3F;
276 val <<= 6;
277 val |= (in[2]) & 0x3F;
278 val <<= 6;
279 val |= (in[3]) & 0x3F;
280 in += 4;
281 } else {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000282 xmlSaveErr(XML_SAVE_CHAR_INVALID, NULL, NULL);
Daniel Veillard83a75e02004-05-14 21:50:42 +0000283 in++;
284 goto error;
285 }
286 if (!IS_CHAR(val)) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000287 xmlSaveErr(XML_SAVE_CHAR_INVALID, NULL, NULL);
Daniel Veillard83a75e02004-05-14 21:50:42 +0000288 in++;
289 goto error;
290 }
291
292 /*
293 * We could do multiple things here. Just save as a char ref
294 */
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000295 out = xmlSerializeHexCharRef(out, val);
Daniel Veillard83a75e02004-05-14 21:50:42 +0000296 } else if (IS_BYTE_CHAR(*in)) {
297 if (outend - out < 6) break;
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000298 out = xmlSerializeHexCharRef(out, *in++);
Daniel Veillard83a75e02004-05-14 21:50:42 +0000299 } else {
300 xmlGenericError(xmlGenericErrorContext,
301 "xmlEscapeEntities : char out of range\n");
302 in++;
303 goto error;
304 }
305 }
306 *outlen = out - outstart;
307 *inlen = in - base;
308 return(0);
309error:
310 *outlen = out - outstart;
311 *inlen = in - base;
312 return(-1);
313}
314
315/************************************************************************
316 * *
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000317 * Allocation and deallocation *
318 * *
319 ************************************************************************/
Daniel Veillard753086a2004-03-28 16:12:44 +0000320/**
321 * xmlSaveCtxtInit:
322 * @ctxt: the saving context
323 *
324 * Initialize a saving context
325 */
326static void
327xmlSaveCtxtInit(xmlSaveCtxtPtr ctxt)
328{
329 int i;
William M. Brack12d37ab2005-02-21 13:54:07 +0000330 int len;
Daniel Veillard753086a2004-03-28 16:12:44 +0000331
332 if (ctxt == NULL) return;
Daniel Veillard3995bc32004-05-15 18:57:31 +0000333 if ((ctxt->encoding == NULL) && (ctxt->escape == NULL))
334 ctxt->escape = xmlEscapeEntities;
William M. Brack12d37ab2005-02-21 13:54:07 +0000335 len = xmlStrlen((xmlChar *)xmlTreeIndentString);
336 if ((xmlTreeIndentString == NULL) || (len == 0)) {
Daniel Veillard753086a2004-03-28 16:12:44 +0000337 memset(&ctxt->indent[0], 0, MAX_INDENT + 1);
338 } else {
William M. Brack12d37ab2005-02-21 13:54:07 +0000339 ctxt->indent_size = len;
Daniel Veillard753086a2004-03-28 16:12:44 +0000340 ctxt->indent_nr = MAX_INDENT / ctxt->indent_size;
341 for (i = 0;i < ctxt->indent_nr;i++)
342 memcpy(&ctxt->indent[i * ctxt->indent_size], xmlTreeIndentString,
343 ctxt->indent_size);
344 ctxt->indent[ctxt->indent_nr * ctxt->indent_size] = 0;
345 }
Rob Richards2ce51c02005-09-12 12:16:35 +0000346
Daniel Veillard9a00fd22005-11-09 08:56:26 +0000347 if (xmlSaveNoEmptyTags) {
348 ctxt->options |= XML_SAVE_NO_EMPTY;
349 }
Daniel Veillard753086a2004-03-28 16:12:44 +0000350}
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000351
352/**
353 * xmlFreeSaveCtxt:
354 *
355 * Free a saving context, destroying the ouptut in any remaining buffer
356 */
357static void
358xmlFreeSaveCtxt(xmlSaveCtxtPtr ctxt)
359{
360 if (ctxt == NULL) return;
361 if (ctxt->encoding != NULL)
362 xmlFree((char *) ctxt->encoding);
Daniel Veillarde2161a62004-04-29 17:14:25 +0000363 if (ctxt->buf != NULL)
364 xmlOutputBufferClose(ctxt->buf);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000365 xmlFree(ctxt);
366}
367
368/**
369 * xmlNewSaveCtxt:
370 *
371 * Create a new saving context
372 *
373 * Returns the new structure or NULL in case of error
374 */
375static xmlSaveCtxtPtr
376xmlNewSaveCtxt(const char *encoding, int options)
377{
378 xmlSaveCtxtPtr ret;
379
380 ret = (xmlSaveCtxtPtr) xmlMalloc(sizeof(xmlSaveCtxt));
381 if (ret == NULL) {
382 xmlSaveErrMemory("creating saving context");
383 return ( NULL );
384 }
385 memset(ret, 0, sizeof(xmlSaveCtxt));
Daniel Veillard6fc5db02005-01-16 00:05:58 +0000386
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000387 if (encoding != NULL) {
388 ret->handler = xmlFindCharEncodingHandler(encoding);
389 if (ret->handler == NULL) {
390 xmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL, encoding);
391 xmlFreeSaveCtxt(ret);
392 return(NULL);
393 }
394 ret->encoding = xmlStrdup((const xmlChar *)encoding);
Daniel Veillarddab39b52006-10-16 23:22:10 +0000395 ret->escape = NULL;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000396 }
Daniel Veillard753086a2004-03-28 16:12:44 +0000397 xmlSaveCtxtInit(ret);
398
Rob Richards2ce51c02005-09-12 12:16:35 +0000399 /*
400 * Use the options
401 */
402
Daniel Veillard9a00fd22005-11-09 08:56:26 +0000403 /* Re-check this option as it may already have been set */
404 if ((ret->options & XML_SAVE_NO_EMPTY) && ! (options & XML_SAVE_NO_EMPTY)) {
405 options |= XML_SAVE_NO_EMPTY;
406 }
Rob Richards2ce51c02005-09-12 12:16:35 +0000407
408 ret->options = options;
409 if (options & XML_SAVE_FORMAT)
410 ret->format = 1;
411
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000412 return(ret);
413}
414
415/************************************************************************
416 * *
417 * Dumping XML tree content to a simple buffer *
418 * *
419 ************************************************************************/
420/**
421 * xmlAttrSerializeContent:
422 * @buf: the XML buffer output
423 * @doc: the document
424 * @attr: the attribute pointer
425 *
426 * Serialize the attribute in the buffer
427 */
428static void
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000429xmlAttrSerializeContent(xmlOutputBufferPtr buf, xmlAttrPtr attr)
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000430{
431 xmlNodePtr children;
432
433 children = attr->children;
434 while (children != NULL) {
435 switch (children->type) {
436 case XML_TEXT_NODE:
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000437 xmlAttrSerializeTxtContent(buf->buffer, attr->doc,
438 attr, children->content);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000439 break;
440 case XML_ENTITY_REF_NODE:
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000441 xmlBufferAdd(buf->buffer, BAD_CAST "&", 1);
442 xmlBufferAdd(buf->buffer, children->name,
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000443 xmlStrlen(children->name));
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000444 xmlBufferAdd(buf->buffer, BAD_CAST ";", 1);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000445 break;
446 default:
447 /* should not happen unless we have a badly built tree */
448 break;
449 }
450 children = children->next;
451 }
452}
453
454/************************************************************************
455 * *
456 * Dumping XML tree content to an I/O output buffer *
457 * *
458 ************************************************************************/
459
460#ifdef LIBXML_HTML_ENABLED
461static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000462xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000463#endif
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000464static void xmlNodeListDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur);
465static void xmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000466void xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur);
Daniel Veillarddab39b52006-10-16 23:22:10 +0000467static int xmlDocContentDumpOutput(xmlSaveCtxtPtr ctxt, xmlDocPtr cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000468
469/**
470 * xmlNsDumpOutput:
471 * @buf: the XML buffer output
472 * @cur: a namespace
473 *
474 * Dump a local Namespace definition.
475 * Should be called in the context of attributes dumps.
476 */
477static void
478xmlNsDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000479 if ((cur == NULL) || (buf == NULL)) return;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000480 if ((cur->type == XML_LOCAL_NAMESPACE) && (cur->href != NULL)) {
481 if (xmlStrEqual(cur->prefix, BAD_CAST "xml"))
482 return;
483
484 /* Within the context of an element attributes */
485 if (cur->prefix != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000486 xmlOutputBufferWrite(buf, 7, " xmlns:");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000487 xmlOutputBufferWriteString(buf, (const char *)cur->prefix);
488 } else
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000489 xmlOutputBufferWrite(buf, 6, " xmlns");
490 xmlOutputBufferWrite(buf, 1, "=");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000491 xmlBufferWriteQuotedString(buf->buffer, cur->href);
492 }
493}
494
495/**
496 * xmlNsListDumpOutput:
497 * @buf: the XML buffer output
498 * @cur: the first namespace
499 *
500 * Dump a list of local Namespace definitions.
501 * Should be called in the context of attributes dumps.
502 */
503void
504xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) {
505 while (cur != NULL) {
506 xmlNsDumpOutput(buf, cur);
507 cur = cur->next;
508 }
509}
510
511/**
512 * xmlDtdDumpOutput:
513 * @buf: the XML buffer output
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000514 * @dtd: the pointer to the DTD
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000515 *
516 * Dump the XML document DTD, if any.
517 */
518static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000519xmlDtdDumpOutput(xmlSaveCtxtPtr ctxt, xmlDtdPtr dtd) {
520 xmlOutputBufferPtr buf;
521 int format, level;
522 xmlDocPtr doc;
523
524 if (dtd == NULL) return;
525 if ((ctxt == NULL) || (ctxt->buf == NULL))
526 return;
527 buf = ctxt->buf;
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000528 xmlOutputBufferWrite(buf, 10, "<!DOCTYPE ");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000529 xmlOutputBufferWriteString(buf, (const char *)dtd->name);
530 if (dtd->ExternalID != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000531 xmlOutputBufferWrite(buf, 8, " PUBLIC ");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000532 xmlBufferWriteQuotedString(buf->buffer, dtd->ExternalID);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000533 xmlOutputBufferWrite(buf, 1, " ");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000534 xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID);
535 } else if (dtd->SystemID != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000536 xmlOutputBufferWrite(buf, 8, " SYSTEM ");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000537 xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID);
538 }
539 if ((dtd->entities == NULL) && (dtd->elements == NULL) &&
Daniel Veillard41c4a752004-09-08 20:55:38 +0000540 (dtd->attributes == NULL) && (dtd->notations == NULL) &&
541 (dtd->pentities == NULL)) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000542 xmlOutputBufferWrite(buf, 1, ">");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000543 return;
544 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000545 xmlOutputBufferWrite(buf, 3, " [\n");
Daniel Veillard41c4a752004-09-08 20:55:38 +0000546 /*
547 * Dump the notations first they are not in the DTD children list
548 * Do this only on a standalone DTD or on the internal subset though.
549 */
550 if ((dtd->notations != NULL) && ((dtd->doc == NULL) ||
551 (dtd->doc->intSubset == dtd))) {
Daniel Veillardda3b29a2004-08-14 11:15:13 +0000552 xmlDumpNotationTable(buf->buffer, (xmlNotationTablePtr) dtd->notations);
553 }
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000554 format = ctxt->format;
555 level = ctxt->level;
556 doc = ctxt->doc;
557 ctxt->format = 0;
558 ctxt->level = -1;
559 ctxt->doc = dtd->doc;
560 xmlNodeListDumpOutput(ctxt, dtd->children);
561 ctxt->format = format;
562 ctxt->level = level;
563 ctxt->doc = doc;
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000564 xmlOutputBufferWrite(buf, 2, "]>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000565}
566
567/**
568 * xmlAttrDumpOutput:
569 * @buf: the XML buffer output
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000570 * @cur: the attribute pointer
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000571 *
572 * Dump an XML attribute
573 */
574static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000575xmlAttrDumpOutput(xmlSaveCtxtPtr ctxt, xmlAttrPtr cur) {
576 xmlOutputBufferPtr buf;
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000577
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000578 if (cur == NULL) return;
579 buf = ctxt->buf;
Daniel Veillardce244ad2004-11-05 10:03:46 +0000580 if (buf == NULL) return;
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000581 xmlOutputBufferWrite(buf, 1, " ");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000582 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
583 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000584 xmlOutputBufferWrite(buf, 1, ":");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000585 }
586 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000587 xmlOutputBufferWrite(buf, 2, "=\"");
588 xmlAttrSerializeContent(buf, cur);
589 xmlOutputBufferWrite(buf, 1, "\"");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000590}
591
592/**
593 * xmlAttrListDumpOutput:
594 * @buf: the XML buffer output
595 * @doc: the document
596 * @cur: the first attribute pointer
597 * @encoding: an optional encoding string
598 *
599 * Dump a list of XML attributes
600 */
601static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000602xmlAttrListDumpOutput(xmlSaveCtxtPtr ctxt, xmlAttrPtr cur) {
603 if (cur == NULL) return;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000604 while (cur != NULL) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000605 xmlAttrDumpOutput(ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000606 cur = cur->next;
607 }
608}
609
610
611
612/**
613 * xmlNodeListDumpOutput:
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000614 * @cur: the first node
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000615 *
616 * Dump an XML node list, recursive behaviour, children are printed too.
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000617 */
618static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000619xmlNodeListDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000620 xmlOutputBufferPtr buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000621
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000622 if (cur == NULL) return;
623 buf = ctxt->buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000624 while (cur != NULL) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000625 if ((ctxt->format) && (xmlIndentTreeOutput) &&
Daniel Veillardbd444842007-03-20 08:47:29 +0000626 ((cur->type == XML_ELEMENT_NODE) ||
627 (cur->type == XML_COMMENT_NODE) ||
628 (cur->type == XML_PI_NODE)))
Daniel Veillard753086a2004-03-28 16:12:44 +0000629 xmlOutputBufferWrite(buf, ctxt->indent_size *
630 (ctxt->level > ctxt->indent_nr ?
631 ctxt->indent_nr : ctxt->level),
632 ctxt->indent);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000633 xmlNodeDumpOutputInternal(ctxt, cur);
634 if (ctxt->format) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000635 xmlOutputBufferWrite(buf, 1, "\n");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000636 }
637 cur = cur->next;
638 }
639}
640
641/**
642 * xmlNodeDumpOutputInternal:
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000643 * @cur: the current node
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000644 *
645 * Dump an XML node, recursive behaviour, children are printed too.
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000646 */
647static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000648xmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
Daniel Veillard753086a2004-03-28 16:12:44 +0000649 int format;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000650 xmlNodePtr tmp;
651 xmlChar *start, *end;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000652 xmlOutputBufferPtr buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000653
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000654 if (cur == NULL) return;
655 buf = ctxt->buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000656 if (cur->type == XML_XINCLUDE_START)
657 return;
658 if (cur->type == XML_XINCLUDE_END)
659 return;
Daniel Veillardce244ad2004-11-05 10:03:46 +0000660 if ((cur->type == XML_DOCUMENT_NODE) ||
661 (cur->type == XML_HTML_DOCUMENT_NODE)) {
662 xmlDocContentDumpOutput(ctxt, (xmlDocPtr) cur);
663 return;
664 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000665 if (cur->type == XML_DTD_NODE) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000666 xmlDtdDumpOutput(ctxt, (xmlDtdPtr) cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000667 return;
668 }
669 if (cur->type == XML_DOCUMENT_FRAG_NODE) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000670 xmlNodeListDumpOutput(ctxt, cur->children);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000671 return;
672 }
673 if (cur->type == XML_ELEMENT_DECL) {
674 xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur);
675 return;
676 }
677 if (cur->type == XML_ATTRIBUTE_DECL) {
678 xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur);
679 return;
680 }
681 if (cur->type == XML_ENTITY_DECL) {
682 xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur);
683 return;
684 }
685 if (cur->type == XML_TEXT_NODE) {
686 if (cur->content != NULL) {
William M. Brack4e1c2db2005-02-11 10:58:55 +0000687 if (cur->name != xmlStringTextNoenc) {
Daniel Veillard3995bc32004-05-15 18:57:31 +0000688 xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000689 } else {
690 /*
691 * Disable escaping, needed for XSLT
692 */
693 xmlOutputBufferWriteString(buf, (const char *) cur->content);
694 }
695 }
696
697 return;
698 }
699 if (cur->type == XML_PI_NODE) {
700 if (cur->content != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000701 xmlOutputBufferWrite(buf, 2, "<?");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000702 xmlOutputBufferWriteString(buf, (const char *)cur->name);
703 if (cur->content != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000704 xmlOutputBufferWrite(buf, 1, " ");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000705 xmlOutputBufferWriteString(buf, (const char *)cur->content);
706 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000707 xmlOutputBufferWrite(buf, 2, "?>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000708 } else {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000709 xmlOutputBufferWrite(buf, 2, "<?");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000710 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000711 xmlOutputBufferWrite(buf, 2, "?>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000712 }
713 return;
714 }
715 if (cur->type == XML_COMMENT_NODE) {
716 if (cur->content != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000717 xmlOutputBufferWrite(buf, 4, "<!--");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000718 xmlOutputBufferWriteString(buf, (const char *)cur->content);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000719 xmlOutputBufferWrite(buf, 3, "-->");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000720 }
721 return;
722 }
723 if (cur->type == XML_ENTITY_REF_NODE) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000724 xmlOutputBufferWrite(buf, 1, "&");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000725 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000726 xmlOutputBufferWrite(buf, 1, ";");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000727 return;
728 }
729 if (cur->type == XML_CDATA_SECTION_NODE) {
Daniel Veillardd0d2f092008-03-07 16:50:21 +0000730 if (cur->content == NULL || *cur->content == '\0') {
731 xmlOutputBufferWrite(buf, 12, "<![CDATA[]]>");
Daniel Veillard7cd517c2005-05-20 18:47:22 +0000732 } else {
733 start = end = cur->content;
734 while (*end != '\0') {
735 if ((*end == ']') && (*(end + 1) == ']') &&
736 (*(end + 2) == '>')) {
737 end = end + 2;
738 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
739 xmlOutputBufferWrite(buf, end - start, (const char *)start);
740 xmlOutputBufferWrite(buf, 3, "]]>");
741 start = end;
742 }
743 end++;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000744 }
Daniel Veillard7cd517c2005-05-20 18:47:22 +0000745 if (start != end) {
746 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
747 xmlOutputBufferWriteString(buf, (const char *)start);
748 xmlOutputBufferWrite(buf, 3, "]]>");
749 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000750 }
751 return;
752 }
753 if (cur->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000754 xmlAttrDumpOutput(ctxt, (xmlAttrPtr) cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000755 return;
756 }
757 if (cur->type == XML_NAMESPACE_DECL) {
758 xmlNsDumpOutput(buf, (xmlNsPtr) cur);
759 return;
760 }
761
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000762 format = ctxt->format;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000763 if (format == 1) {
764 tmp = cur->children;
765 while (tmp != NULL) {
766 if ((tmp->type == XML_TEXT_NODE) ||
767 (tmp->type == XML_CDATA_SECTION_NODE) ||
768 (tmp->type == XML_ENTITY_REF_NODE)) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000769 ctxt->format = 0;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000770 break;
771 }
772 tmp = tmp->next;
773 }
774 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000775 xmlOutputBufferWrite(buf, 1, "<");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000776 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
777 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000778 xmlOutputBufferWrite(buf, 1, ":");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000779 }
780
781 xmlOutputBufferWriteString(buf, (const char *)cur->name);
782 if (cur->nsDef)
783 xmlNsListDumpOutput(buf, cur->nsDef);
784 if (cur->properties != NULL)
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000785 xmlAttrListDumpOutput(ctxt, cur->properties);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000786
787 if (((cur->type == XML_ELEMENT_NODE) || (cur->content == NULL)) &&
Rob Richards2ce51c02005-09-12 12:16:35 +0000788 (cur->children == NULL) && ((ctxt->options & XML_SAVE_NO_EMPTY) == 0)) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000789 xmlOutputBufferWrite(buf, 2, "/>");
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000790 ctxt->format = format;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000791 return;
792 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000793 xmlOutputBufferWrite(buf, 1, ">");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000794 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Daniel Veillard3995bc32004-05-15 18:57:31 +0000795 xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000796 }
797 if (cur->children != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000798 if (ctxt->format) xmlOutputBufferWrite(buf, 1, "\n");
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000799 if (ctxt->level >= 0) ctxt->level++;
800 xmlNodeListDumpOutput(ctxt, cur->children);
801 if (ctxt->level > 0) ctxt->level--;
802 if ((xmlIndentTreeOutput) && (ctxt->format))
Daniel Veillard753086a2004-03-28 16:12:44 +0000803 xmlOutputBufferWrite(buf, ctxt->indent_size *
804 (ctxt->level > ctxt->indent_nr ?
805 ctxt->indent_nr : ctxt->level),
806 ctxt->indent);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000807 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000808 xmlOutputBufferWrite(buf, 2, "</");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000809 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
810 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000811 xmlOutputBufferWrite(buf, 1, ":");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000812 }
813
814 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000815 xmlOutputBufferWrite(buf, 1, ">");
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000816 ctxt->format = format;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000817}
818
819/**
820 * xmlDocContentDumpOutput:
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000821 * @cur: the document
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000822 *
823 * Dump an XML document.
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000824 */
Daniel Veillarddab39b52006-10-16 23:22:10 +0000825static int
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000826xmlDocContentDumpOutput(xmlSaveCtxtPtr ctxt, xmlDocPtr cur) {
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000827#ifdef LIBXML_HTML_ENABLED
828 xmlDtdPtr dtd;
829 int is_xhtml = 0;
830#endif
831 const xmlChar *oldenc = cur->encoding;
Daniel Veillarddab39b52006-10-16 23:22:10 +0000832 const xmlChar *oldctxtenc = ctxt->encoding;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000833 const xmlChar *encoding = ctxt->encoding;
Daniel Veillarddab39b52006-10-16 23:22:10 +0000834 xmlCharEncodingOutputFunc oldescape = ctxt->escape;
835 xmlCharEncodingOutputFunc oldescapeAttr = ctxt->escapeAttr;
836 xmlOutputBufferPtr buf = ctxt->buf;
Daniel Veillarddab39b52006-10-16 23:22:10 +0000837 xmlCharEncoding enc;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000838
839 xmlInitParser();
840
Daniel Veillarddab39b52006-10-16 23:22:10 +0000841 if (ctxt->encoding != NULL) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000842 cur->encoding = BAD_CAST ctxt->encoding;
Daniel Veillarddab39b52006-10-16 23:22:10 +0000843 } else if (cur->encoding != NULL) {
844 encoding = cur->encoding;
845 } else if (cur->charset != XML_CHAR_ENCODING_UTF8) {
846 encoding = (const xmlChar *)
847 xmlGetCharEncodingName((xmlCharEncoding) cur->charset);
848 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000849
Daniel Veillarddab39b52006-10-16 23:22:10 +0000850 enc = xmlParseCharEncoding((const char*) encoding);
851 if ((encoding != NULL) && (oldctxtenc == NULL) &&
852 (buf->encoder == NULL) && (buf->conv == NULL) &&
853 ((ctxt->options & XML_SAVE_NO_DECL) == 0)) {
854 if ((enc != XML_CHAR_ENCODING_UTF8) &&
855 (enc != XML_CHAR_ENCODING_NONE) &&
856 (enc != XML_CHAR_ENCODING_ASCII)) {
857 /*
858 * we need to switch to this encoding but just for this document
859 * since we output the XMLDecl the conversion must be done to not
860 * generate not well formed documents.
861 */
862 buf->encoder = xmlFindCharEncodingHandler((const char *)encoding);
863 if (buf->encoder == NULL) {
864 xmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL,
865 (const char *)encoding);
866 return(-1);
867 }
868 buf->conv = xmlBufferCreate();
869 if (buf->conv == NULL) {
870 xmlCharEncCloseFunc(buf->encoder);
871 xmlSaveErrMemory("creating encoding buffer");
872 return(-1);
873 }
874 /*
875 * initialize the state, e.g. if outputting a BOM
876 */
877 xmlCharEncOutFunc(buf->encoder, buf->conv, NULL);
878 }
879 if (ctxt->escape == xmlEscapeEntities)
880 ctxt->escape = NULL;
881 if (ctxt->escapeAttr == xmlEscapeEntities)
882 ctxt->escapeAttr = NULL;
883 }
884
885
886 /*
887 * Save the XML declaration
888 */
Daniel Veillard100e1802005-08-08 14:44:11 +0000889 if ((ctxt->options & XML_SAVE_NO_DECL) == 0) {
890 xmlOutputBufferWrite(buf, 14, "<?xml version=");
891 if (cur->version != NULL)
892 xmlBufferWriteQuotedString(buf->buffer, cur->version);
893 else
894 xmlOutputBufferWrite(buf, 5, "\"1.0\"");
Daniel Veillard100e1802005-08-08 14:44:11 +0000895 if (encoding != NULL) {
896 xmlOutputBufferWrite(buf, 10, " encoding=");
897 xmlBufferWriteQuotedString(buf->buffer, (xmlChar *) encoding);
898 }
899 switch (cur->standalone) {
900 case 0:
901 xmlOutputBufferWrite(buf, 16, " standalone=\"no\"");
902 break;
903 case 1:
904 xmlOutputBufferWrite(buf, 17, " standalone=\"yes\"");
905 break;
906 }
907 xmlOutputBufferWrite(buf, 3, "?>\n");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000908 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000909
910#ifdef LIBXML_HTML_ENABLED
Daniel Veillard33b20b72005-09-12 21:43:20 +0000911 if ((ctxt->options & XML_SAVE_NO_XHTML) == 0) {
912 dtd = xmlGetIntSubset(cur);
913 if (dtd != NULL) {
914 is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
915 if (is_xhtml < 0) is_xhtml = 0;
916 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000917 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000918#endif
919 if (cur->children != NULL) {
920 xmlNodePtr child = cur->children;
921
922 while (child != NULL) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000923 ctxt->level = 0;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000924#ifdef LIBXML_HTML_ENABLED
925 if (is_xhtml)
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000926 xhtmlNodeDumpOutput(ctxt, child);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000927 else
928#endif
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000929 xmlNodeDumpOutputInternal(ctxt, child);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000930 xmlOutputBufferWrite(buf, 1, "\n");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000931 child = child->next;
932 }
933 }
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000934 if (ctxt->encoding != NULL)
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000935 cur->encoding = oldenc;
Daniel Veillarddab39b52006-10-16 23:22:10 +0000936
937 /*
938 * Restore the state of the saving context at the end of the document
939 */
940 if ((encoding != NULL) && (oldctxtenc == NULL) &&
941 ((ctxt->options & XML_SAVE_NO_DECL) == 0)) {
942 if ((enc != XML_CHAR_ENCODING_UTF8) &&
943 (enc != XML_CHAR_ENCODING_NONE) &&
944 (enc != XML_CHAR_ENCODING_ASCII)) {
945 xmlOutputBufferFlush(buf);
946 xmlCharEncCloseFunc(buf->encoder);
947 xmlBufferFree(buf->conv);
948 buf->encoder = NULL;
949 buf->conv = NULL;
950 }
951 ctxt->escape = oldescape;
952 ctxt->escapeAttr = oldescapeAttr;
953 }
954 return(0);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000955}
956
957#ifdef LIBXML_HTML_ENABLED
958/************************************************************************
959 * *
960 * Functions specific to XHTML serialization *
961 * *
962 ************************************************************************/
963
964/**
965 * xhtmlIsEmpty:
966 * @node: the node
967 *
968 * Check if a node is an empty xhtml node
969 *
970 * Returns 1 if the node is an empty node, 0 if not and -1 in case of error
971 */
972static int
973xhtmlIsEmpty(xmlNodePtr node) {
974 if (node == NULL)
975 return(-1);
976 if (node->type != XML_ELEMENT_NODE)
977 return(0);
978 if ((node->ns != NULL) && (!xmlStrEqual(node->ns->href, XHTML_NS_NAME)))
979 return(0);
980 if (node->children != NULL)
981 return(0);
982 switch (node->name[0]) {
983 case 'a':
984 if (xmlStrEqual(node->name, BAD_CAST "area"))
985 return(1);
986 return(0);
987 case 'b':
988 if (xmlStrEqual(node->name, BAD_CAST "br"))
989 return(1);
990 if (xmlStrEqual(node->name, BAD_CAST "base"))
991 return(1);
992 if (xmlStrEqual(node->name, BAD_CAST "basefont"))
993 return(1);
994 return(0);
995 case 'c':
996 if (xmlStrEqual(node->name, BAD_CAST "col"))
997 return(1);
998 return(0);
999 case 'f':
1000 if (xmlStrEqual(node->name, BAD_CAST "frame"))
1001 return(1);
1002 return(0);
1003 case 'h':
1004 if (xmlStrEqual(node->name, BAD_CAST "hr"))
1005 return(1);
1006 return(0);
1007 case 'i':
1008 if (xmlStrEqual(node->name, BAD_CAST "img"))
1009 return(1);
1010 if (xmlStrEqual(node->name, BAD_CAST "input"))
1011 return(1);
1012 if (xmlStrEqual(node->name, BAD_CAST "isindex"))
1013 return(1);
1014 return(0);
1015 case 'l':
1016 if (xmlStrEqual(node->name, BAD_CAST "link"))
1017 return(1);
1018 return(0);
1019 case 'm':
1020 if (xmlStrEqual(node->name, BAD_CAST "meta"))
1021 return(1);
1022 return(0);
1023 case 'p':
1024 if (xmlStrEqual(node->name, BAD_CAST "param"))
1025 return(1);
1026 return(0);
1027 }
1028 return(0);
1029}
1030
1031/**
1032 * xhtmlAttrListDumpOutput:
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001033 * @cur: the first attribute pointer
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001034 *
1035 * Dump a list of XML attributes
1036 */
1037static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001038xhtmlAttrListDumpOutput(xmlSaveCtxtPtr ctxt, xmlAttrPtr cur) {
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001039 xmlAttrPtr xml_lang = NULL;
1040 xmlAttrPtr lang = NULL;
1041 xmlAttrPtr name = NULL;
1042 xmlAttrPtr id = NULL;
1043 xmlNodePtr parent;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001044 xmlOutputBufferPtr buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001045
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001046 if (cur == NULL) return;
1047 buf = ctxt->buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001048 parent = cur->parent;
1049 while (cur != NULL) {
1050 if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "id")))
1051 id = cur;
1052 else
1053 if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "name")))
1054 name = cur;
1055 else
1056 if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")))
1057 lang = cur;
1058 else
1059 if ((cur->ns != NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")) &&
1060 (xmlStrEqual(cur->ns->prefix, BAD_CAST "xml")))
1061 xml_lang = cur;
1062 else if ((cur->ns == NULL) &&
1063 ((cur->children == NULL) ||
1064 (cur->children->content == NULL) ||
1065 (cur->children->content[0] == 0)) &&
1066 (htmlIsBooleanAttr(cur->name))) {
1067 if (cur->children != NULL)
1068 xmlFreeNode(cur->children);
1069 cur->children = xmlNewText(cur->name);
1070 if (cur->children != NULL)
1071 cur->children->parent = (xmlNodePtr) cur;
1072 }
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001073 xmlAttrDumpOutput(ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001074 cur = cur->next;
1075 }
1076 /*
1077 * C.8
1078 */
1079 if ((name != NULL) && (id == NULL)) {
1080 if ((parent != NULL) && (parent->name != NULL) &&
1081 ((xmlStrEqual(parent->name, BAD_CAST "a")) ||
1082 (xmlStrEqual(parent->name, BAD_CAST "p")) ||
1083 (xmlStrEqual(parent->name, BAD_CAST "div")) ||
1084 (xmlStrEqual(parent->name, BAD_CAST "img")) ||
1085 (xmlStrEqual(parent->name, BAD_CAST "map")) ||
1086 (xmlStrEqual(parent->name, BAD_CAST "applet")) ||
1087 (xmlStrEqual(parent->name, BAD_CAST "form")) ||
1088 (xmlStrEqual(parent->name, BAD_CAST "frame")) ||
1089 (xmlStrEqual(parent->name, BAD_CAST "iframe")))) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001090 xmlOutputBufferWrite(buf, 5, " id=\"");
1091 xmlAttrSerializeContent(buf, name);
1092 xmlOutputBufferWrite(buf, 1, "\"");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001093 }
1094 }
1095 /*
1096 * C.7.
1097 */
1098 if ((lang != NULL) && (xml_lang == NULL)) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001099 xmlOutputBufferWrite(buf, 11, " xml:lang=\"");
1100 xmlAttrSerializeContent(buf, lang);
1101 xmlOutputBufferWrite(buf, 1, "\"");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001102 } else
1103 if ((xml_lang != NULL) && (lang == NULL)) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001104 xmlOutputBufferWrite(buf, 7, " lang=\"");
1105 xmlAttrSerializeContent(buf, xml_lang);
1106 xmlOutputBufferWrite(buf, 1, "\"");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001107 }
1108}
1109
1110/**
1111 * xhtmlNodeListDumpOutput:
1112 * @buf: the XML buffer output
1113 * @doc: the XHTML document
1114 * @cur: the first node
1115 * @level: the imbrication level for indenting
1116 * @format: is formatting allowed
1117 * @encoding: an optional encoding string
1118 *
1119 * Dump an XML node list, recursive behaviour, children are printed too.
1120 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
1121 * or xmlKeepBlanksDefault(0) was called
1122 */
1123static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001124xhtmlNodeListDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001125 xmlOutputBufferPtr buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001126
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001127 if (cur == NULL) return;
1128 buf = ctxt->buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001129 while (cur != NULL) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001130 if ((ctxt->format) && (xmlIndentTreeOutput) &&
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001131 (cur->type == XML_ELEMENT_NODE))
Daniel Veillard753086a2004-03-28 16:12:44 +00001132 xmlOutputBufferWrite(buf, ctxt->indent_size *
1133 (ctxt->level > ctxt->indent_nr ?
1134 ctxt->indent_nr : ctxt->level),
1135 ctxt->indent);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001136 xhtmlNodeDumpOutput(ctxt, cur);
1137 if (ctxt->format) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001138 xmlOutputBufferWrite(buf, 1, "\n");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001139 }
1140 cur = cur->next;
1141 }
1142}
1143
1144/**
1145 * xhtmlNodeDumpOutput:
1146 * @buf: the XML buffer output
1147 * @doc: the XHTML document
1148 * @cur: the current node
1149 * @level: the imbrication level for indenting
1150 * @format: is formatting allowed
1151 * @encoding: an optional encoding string
1152 *
1153 * Dump an XHTML node, recursive behaviour, children are printed too.
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001154 */
1155static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001156xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
Rob Richards31f73022005-08-26 15:33:26 +00001157 int format, addmeta = 0;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001158 xmlNodePtr tmp;
1159 xmlChar *start, *end;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001160 xmlOutputBufferPtr buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001161
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001162 if (cur == NULL) return;
Daniel Veillard60071ae2005-09-12 00:03:43 +00001163 if ((cur->type == XML_DOCUMENT_NODE) ||
1164 (cur->type == XML_HTML_DOCUMENT_NODE)) {
1165 xmlDocContentDumpOutput(ctxt, (xmlDocPtr) cur);
1166 return;
1167 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001168 if (cur->type == XML_XINCLUDE_START)
1169 return;
1170 if (cur->type == XML_XINCLUDE_END)
1171 return;
1172 if (cur->type == XML_DTD_NODE) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001173 xmlDtdDumpOutput(ctxt, (xmlDtdPtr) cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001174 return;
1175 }
Rob Richards2e2691b2005-10-21 14:45:16 +00001176 if (cur->type == XML_DOCUMENT_FRAG_NODE) {
1177 xhtmlNodeListDumpOutput(ctxt, cur->children);
1178 return;
1179 }
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001180 buf = ctxt->buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001181 if (cur->type == XML_ELEMENT_DECL) {
1182 xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur);
1183 return;
1184 }
1185 if (cur->type == XML_ATTRIBUTE_DECL) {
1186 xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur);
1187 return;
1188 }
1189 if (cur->type == XML_ENTITY_DECL) {
1190 xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur);
1191 return;
1192 }
1193 if (cur->type == XML_TEXT_NODE) {
1194 if (cur->content != NULL) {
1195 if ((cur->name == xmlStringText) ||
1196 (cur->name != xmlStringTextNoenc)) {
Daniel Veillard3995bc32004-05-15 18:57:31 +00001197 xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001198 } else {
1199 /*
1200 * Disable escaping, needed for XSLT
1201 */
1202 xmlOutputBufferWriteString(buf, (const char *) cur->content);
1203 }
1204 }
1205
1206 return;
1207 }
1208 if (cur->type == XML_PI_NODE) {
1209 if (cur->content != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001210 xmlOutputBufferWrite(buf, 2, "<?");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001211 xmlOutputBufferWriteString(buf, (const char *)cur->name);
1212 if (cur->content != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001213 xmlOutputBufferWrite(buf, 1, " ");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001214 xmlOutputBufferWriteString(buf, (const char *)cur->content);
1215 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001216 xmlOutputBufferWrite(buf, 2, "?>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001217 } else {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001218 xmlOutputBufferWrite(buf, 2, "<?");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001219 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001220 xmlOutputBufferWrite(buf, 2, "?>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001221 }
1222 return;
1223 }
1224 if (cur->type == XML_COMMENT_NODE) {
1225 if (cur->content != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001226 xmlOutputBufferWrite(buf, 4, "<!--");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001227 xmlOutputBufferWriteString(buf, (const char *)cur->content);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001228 xmlOutputBufferWrite(buf, 3, "-->");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001229 }
1230 return;
1231 }
1232 if (cur->type == XML_ENTITY_REF_NODE) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001233 xmlOutputBufferWrite(buf, 1, "&");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001234 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001235 xmlOutputBufferWrite(buf, 1, ";");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001236 return;
1237 }
1238 if (cur->type == XML_CDATA_SECTION_NODE) {
Daniel Veillardd0d2f092008-03-07 16:50:21 +00001239 if (cur->content == NULL || *cur->content == '\0') {
1240 xmlOutputBufferWrite(buf, 12, "<![CDATA[]]>");
1241 } else {
1242 start = end = cur->content;
1243 while (*end != '\0') {
1244 if (*end == ']' && *(end + 1) == ']' && *(end + 2) == '>') {
1245 end = end + 2;
1246 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
1247 xmlOutputBufferWrite(buf, end - start, (const char *)start);
1248 xmlOutputBufferWrite(buf, 3, "]]>");
1249 start = end;
1250 }
1251 end++;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001252 }
Daniel Veillardd0d2f092008-03-07 16:50:21 +00001253 if (start != end) {
1254 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
1255 xmlOutputBufferWriteString(buf, (const char *)start);
1256 xmlOutputBufferWrite(buf, 3, "]]>");
1257 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001258 }
1259 return;
1260 }
Daniel Veillarda76a81f2007-10-10 08:28:18 +00001261 if (cur->type == XML_ATTRIBUTE_NODE) {
1262 xmlAttrDumpOutput(ctxt, (xmlAttrPtr) cur);
1263 return;
1264 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001265
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001266 format = ctxt->format;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001267 if (format == 1) {
1268 tmp = cur->children;
1269 while (tmp != NULL) {
1270 if ((tmp->type == XML_TEXT_NODE) ||
1271 (tmp->type == XML_ENTITY_REF_NODE)) {
1272 format = 0;
1273 break;
1274 }
1275 tmp = tmp->next;
1276 }
1277 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001278 xmlOutputBufferWrite(buf, 1, "<");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001279 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1280 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001281 xmlOutputBufferWrite(buf, 1, ":");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001282 }
1283
1284 xmlOutputBufferWriteString(buf, (const char *)cur->name);
1285 if (cur->nsDef)
1286 xmlNsListDumpOutput(buf, cur->nsDef);
1287 if ((xmlStrEqual(cur->name, BAD_CAST "html") &&
1288 (cur->ns == NULL) && (cur->nsDef == NULL))) {
1289 /*
1290 * 3.1.1. Strictly Conforming Documents A.3.1.1 3/
1291 */
1292 xmlOutputBufferWriteString(buf,
1293 " xmlns=\"http://www.w3.org/1999/xhtml\"");
1294 }
1295 if (cur->properties != NULL)
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001296 xhtmlAttrListDumpOutput(ctxt, cur->properties);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001297
Rob Richards31f73022005-08-26 15:33:26 +00001298 if ((cur->type == XML_ELEMENT_NODE) &&
1299 (cur->parent != NULL) &&
1300 (cur->parent->parent == (xmlNodePtr) cur->doc) &&
1301 xmlStrEqual(cur->name, BAD_CAST"head") &&
1302 xmlStrEqual(cur->parent->name, BAD_CAST"html")) {
1303
1304 tmp = cur->children;
1305 while (tmp != NULL) {
1306 if (xmlStrEqual(tmp->name, BAD_CAST"meta")) {
1307 xmlChar *httpequiv;
1308
1309 httpequiv = xmlGetProp(tmp, BAD_CAST"http-equiv");
Rob Richards07b72002005-09-03 14:56:36 +00001310 if (httpequiv != NULL) {
1311 if (xmlStrcasecmp(httpequiv, BAD_CAST"Content-Type") == 0) {
1312 xmlFree(httpequiv);
1313 break;
1314 }
Rob Richards31f73022005-08-26 15:33:26 +00001315 xmlFree(httpequiv);
Rob Richards31f73022005-08-26 15:33:26 +00001316 }
Rob Richards31f73022005-08-26 15:33:26 +00001317 }
1318 tmp = tmp->next;
1319 }
1320 if (tmp == NULL)
1321 addmeta = 1;
1322 }
1323
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001324 if ((cur->type == XML_ELEMENT_NODE) && (cur->children == NULL)) {
1325 if (((cur->ns == NULL) || (cur->ns->prefix == NULL)) &&
Rob Richards31f73022005-08-26 15:33:26 +00001326 ((xhtmlIsEmpty(cur) == 1) && (addmeta == 0))) {
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001327 /*
1328 * C.2. Empty Elements
1329 */
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001330 xmlOutputBufferWrite(buf, 3, " />");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001331 } else {
Rob Richards31f73022005-08-26 15:33:26 +00001332 if (addmeta == 1) {
1333 xmlOutputBufferWrite(buf, 1, ">");
Rob Richards2ce51c02005-09-12 12:16:35 +00001334 if (ctxt->format) {
1335 xmlOutputBufferWrite(buf, 1, "\n");
1336 if (xmlIndentTreeOutput)
1337 xmlOutputBufferWrite(buf, ctxt->indent_size *
1338 (ctxt->level + 1 > ctxt->indent_nr ?
1339 ctxt->indent_nr : ctxt->level + 1), ctxt->indent);
1340 }
Rob Richards31f73022005-08-26 15:33:26 +00001341 xmlOutputBufferWriteString(buf,
1342 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=");
1343 if (ctxt->encoding) {
1344 xmlOutputBufferWriteString(buf, (const char *)ctxt->encoding);
1345 } else {
1346 xmlOutputBufferWrite(buf, 5, "UTF-8");
1347 }
Rob Richards2ce51c02005-09-12 12:16:35 +00001348 xmlOutputBufferWrite(buf, 4, "\" />");
1349 if (ctxt->format)
1350 xmlOutputBufferWrite(buf, 1, "\n");
1351 } else {
1352 xmlOutputBufferWrite(buf, 1, ">");
Rob Richards31f73022005-08-26 15:33:26 +00001353 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001354 /*
1355 * C.3. Element Minimization and Empty Element Content
1356 */
Rob Richards2ce51c02005-09-12 12:16:35 +00001357 xmlOutputBufferWrite(buf, 2, "</");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001358 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1359 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001360 xmlOutputBufferWrite(buf, 1, ":");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001361 }
1362 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001363 xmlOutputBufferWrite(buf, 1, ">");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001364 }
1365 return;
1366 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001367 xmlOutputBufferWrite(buf, 1, ">");
Rob Richards31f73022005-08-26 15:33:26 +00001368 if (addmeta == 1) {
Rob Richards2ce51c02005-09-12 12:16:35 +00001369 if (ctxt->format) {
1370 xmlOutputBufferWrite(buf, 1, "\n");
1371 if (xmlIndentTreeOutput)
1372 xmlOutputBufferWrite(buf, ctxt->indent_size *
1373 (ctxt->level + 1 > ctxt->indent_nr ?
1374 ctxt->indent_nr : ctxt->level + 1), ctxt->indent);
1375 }
Rob Richards31f73022005-08-26 15:33:26 +00001376 xmlOutputBufferWriteString(buf,
1377 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=");
1378 if (ctxt->encoding) {
1379 xmlOutputBufferWriteString(buf, (const char *)ctxt->encoding);
1380 } else {
1381 xmlOutputBufferWrite(buf, 5, "UTF-8");
1382 }
1383 xmlOutputBufferWrite(buf, 4, "\" />");
1384 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001385 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Daniel Veillard3995bc32004-05-15 18:57:31 +00001386 xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001387 }
1388
Kasimier T. Buchcik7b4e2e22006-07-13 13:07:11 +00001389#if 0
1390 /*
1391 * This was removed due to problems with HTML processors.
1392 * See bug #345147.
Daniel Veillarddab39b52006-10-16 23:22:10 +00001393 */
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001394 /*
1395 * 4.8. Script and Style elements
1396 */
1397 if ((cur->type == XML_ELEMENT_NODE) &&
1398 ((xmlStrEqual(cur->name, BAD_CAST "script")) ||
1399 (xmlStrEqual(cur->name, BAD_CAST "style"))) &&
1400 ((cur->ns == NULL) ||
1401 (xmlStrEqual(cur->ns->href, XHTML_NS_NAME)))) {
1402 xmlNodePtr child = cur->children;
1403
1404 while (child != NULL) {
Daniel Veillarddbd61052005-09-12 14:03:26 +00001405 if (child->type == XML_TEXT_NODE) {
1406 if ((xmlStrchr(child->content, '<') == NULL) &&
1407 (xmlStrchr(child->content, '&') == NULL) &&
1408 (xmlStrstr(child->content, BAD_CAST "]]>") == NULL)) {
1409 /* Nothing to escape, so just output as is... */
1410 /* FIXME: Should we do something about "--" also? */
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001411 int level = ctxt->level;
1412 int indent = ctxt->format;
1413
1414 ctxt->level = 0;
1415 ctxt->format = 0;
Daniel Veillarddbd61052005-09-12 14:03:26 +00001416 xmlOutputBufferWriteString(buf, (const char *) child->content);
1417 /* (We cannot use xhtmlNodeDumpOutput() here because
1418 * we wish to leave '>' unescaped!) */
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001419 ctxt->level = level;
1420 ctxt->format = indent;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001421 } else {
Daniel Veillarddbd61052005-09-12 14:03:26 +00001422 /* We must use a CDATA section. Unfortunately,
1423 * this will break CSS and JavaScript when read by
1424 * a browser in HTML4-compliant mode. :-( */
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001425 start = end = child->content;
1426 while (*end != '\0') {
1427 if (*end == ']' &&
1428 *(end + 1) == ']' &&
1429 *(end + 2) == '>') {
1430 end = end + 2;
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001431 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001432 xmlOutputBufferWrite(buf, end - start,
1433 (const char *)start);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001434 xmlOutputBufferWrite(buf, 3, "]]>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001435 start = end;
1436 }
1437 end++;
1438 }
1439 if (start != end) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001440 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
1441 xmlOutputBufferWrite(buf, end - start,
1442 (const char *)start);
1443 xmlOutputBufferWrite(buf, 3, "]]>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001444 }
1445 }
1446 } else {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001447 int level = ctxt->level;
1448 int indent = ctxt->format;
1449
1450 ctxt->level = 0;
1451 ctxt->format = 0;
1452 xhtmlNodeDumpOutput(ctxt, child);
1453 ctxt->level = level;
1454 ctxt->format = indent;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001455 }
1456 child = child->next;
1457 }
Kasimier T. Buchcik7b4e2e22006-07-13 13:07:11 +00001458 }
1459#endif
1460
1461 if (cur->children != NULL) {
Daniel Veillardf0244ce2004-05-09 23:48:39 +00001462 int indent = ctxt->format;
1463
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001464 if (format) xmlOutputBufferWrite(buf, 1, "\n");
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001465 if (ctxt->level >= 0) ctxt->level++;
Daniel Veillardf0244ce2004-05-09 23:48:39 +00001466 ctxt->format = format;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001467 xhtmlNodeListDumpOutput(ctxt, cur->children);
1468 if (ctxt->level > 0) ctxt->level--;
Daniel Veillardf0244ce2004-05-09 23:48:39 +00001469 ctxt->format = indent;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001470 if ((xmlIndentTreeOutput) && (format))
Daniel Veillard753086a2004-03-28 16:12:44 +00001471 xmlOutputBufferWrite(buf, ctxt->indent_size *
1472 (ctxt->level > ctxt->indent_nr ?
1473 ctxt->indent_nr : ctxt->level),
1474 ctxt->indent);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001475 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001476 xmlOutputBufferWrite(buf, 2, "</");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001477 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1478 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001479 xmlOutputBufferWrite(buf, 1, ":");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001480 }
1481
1482 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001483 xmlOutputBufferWrite(buf, 1, ">");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001484}
1485#endif
1486
1487/************************************************************************
1488 * *
1489 * Public entry points *
1490 * *
1491 ************************************************************************/
1492
1493/**
1494 * xmlSaveToFd:
1495 * @fd: a file descriptor number
1496 * @encoding: the encoding name to use or NULL
1497 * @options: a set of xmlSaveOptions
1498 *
1499 * Create a document saving context serializing to a file descriptor
1500 * with the encoding and the options given.
1501 *
1502 * Returns a new serialization context or NULL in case of error.
1503 */
1504xmlSaveCtxtPtr
1505xmlSaveToFd(int fd, const char *encoding, int options)
1506{
1507 xmlSaveCtxtPtr ret;
1508
1509 ret = xmlNewSaveCtxt(encoding, options);
1510 if (ret == NULL) return(NULL);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001511 ret->buf = xmlOutputBufferCreateFd(fd, ret->handler);
1512 if (ret->buf == NULL) {
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001513 xmlFreeSaveCtxt(ret);
1514 return(NULL);
1515 }
1516 return(ret);
1517}
1518
1519/**
1520 * xmlSaveToFilename:
1521 * @filename: a file name or an URL
1522 * @encoding: the encoding name to use or NULL
1523 * @options: a set of xmlSaveOptions
1524 *
1525 * Create a document saving context serializing to a filename or possibly
1526 * to an URL (but this is less reliable) with the encoding and the options
1527 * given.
1528 *
1529 * Returns a new serialization context or NULL in case of error.
1530 */
1531xmlSaveCtxtPtr
1532xmlSaveToFilename(const char *filename, const char *encoding, int options)
1533{
1534 xmlSaveCtxtPtr ret;
1535 int compression = 0; /* TODO handle compression option */
1536
1537 ret = xmlNewSaveCtxt(encoding, options);
1538 if (ret == NULL) return(NULL);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001539 ret->buf = xmlOutputBufferCreateFilename(filename, ret->handler,
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001540 compression);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001541 if (ret->buf == NULL) {
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001542 xmlFreeSaveCtxt(ret);
1543 return(NULL);
1544 }
1545 return(ret);
1546}
1547
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001548/**
1549 * xmlSaveToBuffer:
1550 * @buffer: a buffer
1551 * @encoding: the encoding name to use or NULL
1552 * @options: a set of xmlSaveOptions
1553 *
1554 * Create a document saving context serializing to a buffer
1555 * with the encoding and the options given
1556 *
1557 * Returns a new serialization context or NULL in case of error.
Daniel Veillard9a00fd22005-11-09 08:56:26 +00001558 */
1559
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001560xmlSaveCtxtPtr
1561xmlSaveToBuffer(xmlBufferPtr buffer, const char *encoding, int options)
1562{
Daniel Veillard9a00fd22005-11-09 08:56:26 +00001563 xmlSaveCtxtPtr ret;
1564 xmlOutputBufferPtr out_buff;
1565 xmlCharEncodingHandlerPtr handler;
1566
1567 ret = xmlNewSaveCtxt(encoding, options);
1568 if (ret == NULL) return(NULL);
1569
1570 if (encoding != NULL) {
1571 handler = xmlFindCharEncodingHandler(encoding);
1572 if (handler == NULL) {
1573 xmlFree(ret);
1574 return(NULL);
1575 }
1576 } else
1577 handler = NULL;
1578 out_buff = xmlOutputBufferCreateBuffer(buffer, handler);
1579 if (out_buff == NULL) {
1580 xmlFree(ret);
1581 if (handler) xmlCharEncCloseFunc(handler);
1582 return(NULL);
1583 }
1584
1585 ret->buf = out_buff;
1586 return(ret);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001587}
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001588
1589/**
1590 * xmlSaveToIO:
1591 * @iowrite: an I/O write function
1592 * @ioclose: an I/O close function
1593 * @ioctx: an I/O handler
1594 * @encoding: the encoding name to use or NULL
1595 * @options: a set of xmlSaveOptions
1596 *
1597 * Create a document saving context serializing to a file descriptor
1598 * with the encoding and the options given
1599 *
1600 * Returns a new serialization context or NULL in case of error.
1601 */
1602xmlSaveCtxtPtr
1603xmlSaveToIO(xmlOutputWriteCallback iowrite,
1604 xmlOutputCloseCallback ioclose,
1605 void *ioctx, const char *encoding, int options)
1606{
1607 xmlSaveCtxtPtr ret;
1608
1609 ret = xmlNewSaveCtxt(encoding, options);
1610 if (ret == NULL) return(NULL);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001611 ret->buf = xmlOutputBufferCreateIO(iowrite, ioclose, ioctx, ret->handler);
1612 if (ret->buf == NULL) {
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001613 xmlFreeSaveCtxt(ret);
1614 return(NULL);
1615 }
1616 return(ret);
1617}
1618
1619/**
1620 * xmlSaveDoc:
1621 * @ctxt: a document saving context
1622 * @doc: a document
1623 *
1624 * Save a full document to a saving context
Daniel Veillard377e1a92004-04-16 16:30:05 +00001625 * TODO: The function is not fully implemented yet as it does not return the
1626 * byte count but 0 instead
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001627 *
1628 * Returns the number of byte written or -1 in case of error
1629 */
1630long
1631xmlSaveDoc(xmlSaveCtxtPtr ctxt, xmlDocPtr doc)
1632{
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001633 long ret = 0;
1634
Daniel Veillardce682bc2004-11-05 17:22:25 +00001635 if ((ctxt == NULL) || (doc == NULL)) return(-1);
Daniel Veillarddab39b52006-10-16 23:22:10 +00001636 if (xmlDocContentDumpOutput(ctxt, doc) < 0)
1637 return(-1);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001638 return(ret);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001639}
1640
1641/**
1642 * xmlSaveTree:
1643 * @ctxt: a document saving context
Daniel Veillard681e9042006-09-29 09:16:00 +00001644 * @node: the top node of the subtree to save
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001645 *
1646 * Save a subtree starting at the node parameter to a saving context
Daniel Veillard377e1a92004-04-16 16:30:05 +00001647 * TODO: The function is not fully implemented yet as it does not return the
1648 * byte count but 0 instead
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001649 *
1650 * Returns the number of byte written or -1 in case of error
1651 */
1652long
1653xmlSaveTree(xmlSaveCtxtPtr ctxt, xmlNodePtr node)
1654{
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001655 long ret = 0;
1656
Daniel Veillardce682bc2004-11-05 17:22:25 +00001657 if ((ctxt == NULL) || (node == NULL)) return(-1);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001658 xmlNodeDumpOutputInternal(ctxt, node);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001659 return(ret);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001660}
1661
1662/**
1663 * xmlSaveFlush:
1664 * @ctxt: a document saving context
1665 *
1666 * Flush a document saving context, i.e. make sure that all bytes have
1667 * been output.
1668 *
1669 * Returns the number of byte written or -1 in case of error.
1670 */
1671int
1672xmlSaveFlush(xmlSaveCtxtPtr ctxt)
1673{
1674 if (ctxt == NULL) return(-1);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001675 if (ctxt->buf == NULL) return(-1);
1676 return(xmlOutputBufferFlush(ctxt->buf));
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001677}
1678
1679/**
1680 * xmlSaveClose:
1681 * @ctxt: a document saving context
1682 *
1683 * Close a document saving context, i.e. make sure that all bytes have
1684 * been output and free the associated data.
1685 *
1686 * Returns the number of byte written or -1 in case of error.
1687 */
1688int
1689xmlSaveClose(xmlSaveCtxtPtr ctxt)
1690{
1691 int ret;
1692
1693 if (ctxt == NULL) return(-1);
1694 ret = xmlSaveFlush(ctxt);
1695 xmlFreeSaveCtxt(ctxt);
1696 return(ret);
1697}
1698
Daniel Veillard3995bc32004-05-15 18:57:31 +00001699/**
1700 * xmlSaveSetEscape:
1701 * @ctxt: a document saving context
1702 * @escape: the escaping function
1703 *
1704 * Set a custom escaping function to be used for text in element content
1705 *
1706 * Returns 0 if successful or -1 in case of error.
1707 */
1708int
1709xmlSaveSetEscape(xmlSaveCtxtPtr ctxt, xmlCharEncodingOutputFunc escape)
1710{
1711 if (ctxt == NULL) return(-1);
1712 ctxt->escape = escape;
1713 return(0);
1714}
1715
1716/**
1717 * xmlSaveSetAttrEscape:
1718 * @ctxt: a document saving context
1719 * @escape: the escaping function
1720 *
1721 * Set a custom escaping function to be used for text in attribute content
1722 *
1723 * Returns 0 if successful or -1 in case of error.
1724 */
1725int
1726xmlSaveSetAttrEscape(xmlSaveCtxtPtr ctxt, xmlCharEncodingOutputFunc escape)
1727{
1728 if (ctxt == NULL) return(-1);
1729 ctxt->escapeAttr = escape;
1730 return(0);
1731}
1732
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001733/************************************************************************
1734 * *
1735 * Public entry points based on buffers *
1736 * *
1737 ************************************************************************/
1738/**
1739 * xmlAttrSerializeTxtContent:
1740 * @buf: the XML buffer output
1741 * @doc: the document
1742 * @attr: the attribute node
1743 * @string: the text content
1744 *
1745 * Serialize text attribute values to an xml simple buffer
1746 */
1747void
1748xmlAttrSerializeTxtContent(xmlBufferPtr buf, xmlDocPtr doc,
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001749 xmlAttrPtr attr, const xmlChar * string)
1750{
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001751 xmlChar *base, *cur;
1752
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001753 if (string == NULL)
1754 return;
1755 base = cur = (xmlChar *) string;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001756 while (*cur != 0) {
1757 if (*cur == '\n') {
1758 if (base != cur)
1759 xmlBufferAdd(buf, base, cur - base);
1760 xmlBufferAdd(buf, BAD_CAST "&#10;", 5);
1761 cur++;
1762 base = cur;
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001763 } else if (*cur == '\r') {
1764 if (base != cur)
1765 xmlBufferAdd(buf, base, cur - base);
1766 xmlBufferAdd(buf, BAD_CAST "&#13;", 5);
1767 cur++;
1768 base = cur;
1769 } else if (*cur == '\t') {
1770 if (base != cur)
1771 xmlBufferAdd(buf, base, cur - base);
1772 xmlBufferAdd(buf, BAD_CAST "&#9;", 4);
1773 cur++;
1774 base = cur;
1775 } else if (*cur == '"') {
1776 if (base != cur)
1777 xmlBufferAdd(buf, base, cur - base);
1778 xmlBufferAdd(buf, BAD_CAST "&quot;", 6);
1779 cur++;
1780 base = cur;
1781 } else if (*cur == '<') {
1782 if (base != cur)
1783 xmlBufferAdd(buf, base, cur - base);
1784 xmlBufferAdd(buf, BAD_CAST "&lt;", 4);
1785 cur++;
1786 base = cur;
1787 } else if (*cur == '>') {
1788 if (base != cur)
1789 xmlBufferAdd(buf, base, cur - base);
1790 xmlBufferAdd(buf, BAD_CAST "&gt;", 4);
1791 cur++;
1792 base = cur;
1793 } else if (*cur == '&') {
1794 if (base != cur)
1795 xmlBufferAdd(buf, base, cur - base);
1796 xmlBufferAdd(buf, BAD_CAST "&amp;", 5);
1797 cur++;
1798 base = cur;
1799 } else if ((*cur >= 0x80) && ((doc == NULL) ||
1800 (doc->encoding == NULL))) {
1801 /*
1802 * We assume we have UTF-8 content.
1803 */
1804 unsigned char tmp[10];
1805 int val = 0, l = 1;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001806
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001807 if (base != cur)
1808 xmlBufferAdd(buf, base, cur - base);
1809 if (*cur < 0xC0) {
1810 xmlSaveErr(XML_SAVE_NOT_UTF8, (xmlNodePtr) attr, NULL);
1811 if (doc != NULL)
1812 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
1813 xmlSerializeHexCharRef(tmp, *cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001814 xmlBufferAdd(buf, (xmlChar *) tmp, -1);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001815 cur++;
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001816 base = cur;
1817 continue;
1818 } else if (*cur < 0xE0) {
1819 val = (cur[0]) & 0x1F;
1820 val <<= 6;
1821 val |= (cur[1]) & 0x3F;
1822 l = 2;
1823 } else if (*cur < 0xF0) {
1824 val = (cur[0]) & 0x0F;
1825 val <<= 6;
1826 val |= (cur[1]) & 0x3F;
1827 val <<= 6;
1828 val |= (cur[2]) & 0x3F;
1829 l = 3;
1830 } else if (*cur < 0xF8) {
1831 val = (cur[0]) & 0x07;
1832 val <<= 6;
1833 val |= (cur[1]) & 0x3F;
1834 val <<= 6;
1835 val |= (cur[2]) & 0x3F;
1836 val <<= 6;
1837 val |= (cur[3]) & 0x3F;
1838 l = 4;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001839 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001840 if ((l == 1) || (!IS_CHAR(val))) {
1841 xmlSaveErr(XML_SAVE_CHAR_INVALID, (xmlNodePtr) attr, NULL);
1842 if (doc != NULL)
1843 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
1844
1845 xmlSerializeHexCharRef(tmp, *cur);
1846 xmlBufferAdd(buf, (xmlChar *) tmp, -1);
1847 cur++;
1848 base = cur;
1849 continue;
1850 }
1851 /*
1852 * We could do multiple things here. Just save
1853 * as a char ref
1854 */
1855 xmlSerializeHexCharRef(tmp, val);
1856 xmlBufferAdd(buf, (xmlChar *) tmp, -1);
1857 cur += l;
1858 base = cur;
1859 } else {
1860 cur++;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001861 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001862 }
1863 if (base != cur)
1864 xmlBufferAdd(buf, base, cur - base);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001865}
1866
1867/**
1868 * xmlNodeDump:
1869 * @buf: the XML buffer output
1870 * @doc: the document
1871 * @cur: the current node
1872 * @level: the imbrication level for indenting
1873 * @format: is formatting allowed
1874 *
1875 * Dump an XML node, recursive behaviour,children are printed too.
1876 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
1877 * or xmlKeepBlanksDefault(0) was called
1878 *
1879 * Returns the number of bytes written to the buffer or -1 in case of error
1880 */
1881int
1882xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
1883 int format)
1884{
1885 unsigned int use;
1886 int ret;
1887 xmlOutputBufferPtr outbuf;
1888
1889 xmlInitParser();
1890
1891 if (cur == NULL) {
1892#ifdef DEBUG_TREE
1893 xmlGenericError(xmlGenericErrorContext,
1894 "xmlNodeDump : node == NULL\n");
1895#endif
1896 return (-1);
1897 }
1898 if (buf == NULL) {
1899#ifdef DEBUG_TREE
1900 xmlGenericError(xmlGenericErrorContext,
1901 "xmlNodeDump : buf == NULL\n");
1902#endif
1903 return (-1);
1904 }
1905 outbuf = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer));
1906 if (outbuf == NULL) {
1907 xmlSaveErrMemory("creating buffer");
1908 return (-1);
1909 }
1910 memset(outbuf, 0, (size_t) sizeof(xmlOutputBuffer));
1911 outbuf->buffer = buf;
1912 outbuf->encoder = NULL;
1913 outbuf->writecallback = NULL;
1914 outbuf->closecallback = NULL;
1915 outbuf->context = NULL;
1916 outbuf->written = 0;
1917
1918 use = buf->use;
1919 xmlNodeDumpOutput(outbuf, doc, cur, level, format, NULL);
1920 xmlFree(outbuf);
1921 ret = buf->use - use;
1922 return (ret);
1923}
1924
1925/**
1926 * xmlElemDump:
1927 * @f: the FILE * for the output
1928 * @doc: the document
1929 * @cur: the current node
1930 *
1931 * Dump an XML/HTML node, recursive behaviour, children are printed too.
1932 */
1933void
1934xmlElemDump(FILE * f, xmlDocPtr doc, xmlNodePtr cur)
1935{
1936 xmlOutputBufferPtr outbuf;
1937
1938 xmlInitParser();
1939
1940 if (cur == NULL) {
1941#ifdef DEBUG_TREE
1942 xmlGenericError(xmlGenericErrorContext,
1943 "xmlElemDump : cur == NULL\n");
1944#endif
1945 return;
1946 }
1947#ifdef DEBUG_TREE
1948 if (doc == NULL) {
1949 xmlGenericError(xmlGenericErrorContext,
1950 "xmlElemDump : doc == NULL\n");
1951 }
1952#endif
1953
1954 outbuf = xmlOutputBufferCreateFile(f, NULL);
1955 if (outbuf == NULL)
1956 return;
1957 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
1958#ifdef LIBXML_HTML_ENABLED
1959 htmlNodeDumpOutput(outbuf, doc, cur, NULL);
1960#else
1961 xmlSaveErr(XML_ERR_INTERNAL_ERROR, cur, "HTML support not compiled in\n");
1962#endif /* LIBXML_HTML_ENABLED */
1963 } else
1964 xmlNodeDumpOutput(outbuf, doc, cur, 0, 1, NULL);
1965 xmlOutputBufferClose(outbuf);
1966}
1967
1968/************************************************************************
1969 * *
1970 * Saving functions front-ends *
1971 * *
1972 ************************************************************************/
1973
1974/**
1975 * xmlNodeDumpOutput:
1976 * @buf: the XML buffer output
1977 * @doc: the document
1978 * @cur: the current node
1979 * @level: the imbrication level for indenting
1980 * @format: is formatting allowed
1981 * @encoding: an optional encoding string
1982 *
1983 * Dump an XML node, recursive behaviour, children are printed too.
1984 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
1985 * or xmlKeepBlanksDefault(0) was called
1986 */
1987void
1988xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
1989 int level, int format, const char *encoding)
1990{
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001991 xmlSaveCtxt ctxt;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001992#ifdef LIBXML_HTML_ENABLED
1993 xmlDtdPtr dtd;
1994 int is_xhtml = 0;
1995#endif
1996
1997 xmlInitParser();
1998
Daniel Veillardce244ad2004-11-05 10:03:46 +00001999 if ((buf == NULL) || (cur == NULL)) return;
2000
Daniel Veillard64354ea2005-03-31 15:22:56 +00002001 if (encoding == NULL)
2002 encoding = "UTF-8";
2003
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002004 memset(&ctxt, 0, sizeof(ctxt));
2005 ctxt.doc = doc;
2006 ctxt.buf = buf;
2007 ctxt.level = level;
2008 ctxt.format = format;
2009 ctxt.encoding = (const xmlChar *) encoding;
Daniel Veillard753086a2004-03-28 16:12:44 +00002010 xmlSaveCtxtInit(&ctxt);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002011
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002012#ifdef LIBXML_HTML_ENABLED
2013 dtd = xmlGetIntSubset(doc);
2014 if (dtd != NULL) {
Daniel Veillard33b20b72005-09-12 21:43:20 +00002015 is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
2016 if (is_xhtml < 0)
2017 is_xhtml = 0;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002018 }
2019
2020 if (is_xhtml)
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002021 xhtmlNodeDumpOutput(&ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002022 else
2023#endif
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002024 xmlNodeDumpOutputInternal(&ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002025}
2026
2027/**
2028 * xmlDocDumpFormatMemoryEnc:
2029 * @out_doc: Document to generate XML text from
2030 * @doc_txt_ptr: Memory pointer for allocated XML text
2031 * @doc_txt_len: Length of the generated XML text
2032 * @txt_encoding: Character encoding to use when generating XML text
2033 * @format: should formatting spaces been added
2034 *
2035 * Dump the current DOM tree into memory using the character encoding specified
2036 * by the caller. Note it is up to the caller of this function to free the
2037 * allocated memory with xmlFree().
2038 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
2039 * or xmlKeepBlanksDefault(0) was called
2040 */
2041
2042void
2043xmlDocDumpFormatMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
2044 int * doc_txt_len, const char * txt_encoding,
2045 int format) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002046 xmlSaveCtxt ctxt;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002047 int dummy = 0;
2048 xmlOutputBufferPtr out_buff = NULL;
2049 xmlCharEncodingHandlerPtr conv_hdlr = NULL;
2050
2051 if (doc_txt_len == NULL) {
2052 doc_txt_len = &dummy; /* Continue, caller just won't get length */
2053 }
2054
2055 if (doc_txt_ptr == NULL) {
2056 *doc_txt_len = 0;
2057 return;
2058 }
2059
2060 *doc_txt_ptr = NULL;
2061 *doc_txt_len = 0;
2062
2063 if (out_doc == NULL) {
2064 /* No document, no output */
2065 return;
2066 }
2067
2068 /*
2069 * Validate the encoding value, if provided.
2070 * This logic is copied from xmlSaveFileEnc.
2071 */
2072
2073 if (txt_encoding == NULL)
2074 txt_encoding = (const char *) out_doc->encoding;
2075 if (txt_encoding != NULL) {
2076 conv_hdlr = xmlFindCharEncodingHandler(txt_encoding);
2077 if ( conv_hdlr == NULL ) {
2078 xmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, (xmlNodePtr) out_doc,
2079 txt_encoding);
2080 return;
2081 }
2082 }
2083
2084 if ((out_buff = xmlAllocOutputBuffer(conv_hdlr)) == NULL ) {
2085 xmlSaveErrMemory("creating buffer");
2086 return;
2087 }
2088
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002089 memset(&ctxt, 0, sizeof(ctxt));
2090 ctxt.doc = out_doc;
2091 ctxt.buf = out_buff;
2092 ctxt.level = 0;
2093 ctxt.format = format;
2094 ctxt.encoding = (const xmlChar *) txt_encoding;
Daniel Veillard753086a2004-03-28 16:12:44 +00002095 xmlSaveCtxtInit(&ctxt);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002096 xmlDocContentDumpOutput(&ctxt, out_doc);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002097 xmlOutputBufferFlush(out_buff);
2098 if (out_buff->conv != NULL) {
2099 *doc_txt_len = out_buff->conv->use;
2100 *doc_txt_ptr = xmlStrndup(out_buff->conv->content, *doc_txt_len);
2101 } else {
2102 *doc_txt_len = out_buff->buffer->use;
2103 *doc_txt_ptr = xmlStrndup(out_buff->buffer->content, *doc_txt_len);
2104 }
2105 (void)xmlOutputBufferClose(out_buff);
2106
2107 if ((*doc_txt_ptr == NULL) && (*doc_txt_len > 0)) {
2108 *doc_txt_len = 0;
2109 xmlSaveErrMemory("creating output");
2110 }
2111
2112 return;
2113}
2114
2115/**
2116 * xmlDocDumpMemory:
2117 * @cur: the document
2118 * @mem: OUT: the memory pointer
2119 * @size: OUT: the memory length
2120 *
2121 * Dump an XML document in memory and return the #xmlChar * and it's size
2122 * in bytes. It's up to the caller to free the memory with xmlFree().
2123 * The resulting byte array is zero terminated, though the last 0 is not
2124 * included in the returned size.
2125 */
2126void
2127xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) {
2128 xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, 0);
2129}
2130
2131/**
2132 * xmlDocDumpFormatMemory:
2133 * @cur: the document
2134 * @mem: OUT: the memory pointer
2135 * @size: OUT: the memory length
2136 * @format: should formatting spaces been added
2137 *
2138 *
2139 * Dump an XML document in memory and return the #xmlChar * and it's size.
2140 * It's up to the caller to free the memory with xmlFree().
2141 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
2142 * or xmlKeepBlanksDefault(0) was called
2143 */
2144void
2145xmlDocDumpFormatMemory(xmlDocPtr cur, xmlChar**mem, int *size, int format) {
2146 xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, format);
2147}
2148
2149/**
2150 * xmlDocDumpMemoryEnc:
2151 * @out_doc: Document to generate XML text from
2152 * @doc_txt_ptr: Memory pointer for allocated XML text
2153 * @doc_txt_len: Length of the generated XML text
2154 * @txt_encoding: Character encoding to use when generating XML text
2155 *
2156 * Dump the current DOM tree into memory using the character encoding specified
2157 * by the caller. Note it is up to the caller of this function to free the
2158 * allocated memory with xmlFree().
2159 */
2160
2161void
2162xmlDocDumpMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
2163 int * doc_txt_len, const char * txt_encoding) {
2164 xmlDocDumpFormatMemoryEnc(out_doc, doc_txt_ptr, doc_txt_len,
2165 txt_encoding, 0);
2166}
2167
2168/**
2169 * xmlDocFormatDump:
2170 * @f: the FILE*
2171 * @cur: the document
2172 * @format: should formatting spaces been added
2173 *
2174 * Dump an XML document to an open FILE.
2175 *
2176 * returns: the number of bytes written or -1 in case of failure.
2177 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
2178 * or xmlKeepBlanksDefault(0) was called
2179 */
2180int
2181xmlDocFormatDump(FILE *f, xmlDocPtr cur, int format) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002182 xmlSaveCtxt ctxt;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002183 xmlOutputBufferPtr buf;
2184 const char * encoding;
2185 xmlCharEncodingHandlerPtr handler = NULL;
2186 int ret;
2187
2188 if (cur == NULL) {
2189#ifdef DEBUG_TREE
2190 xmlGenericError(xmlGenericErrorContext,
2191 "xmlDocDump : document == NULL\n");
2192#endif
2193 return(-1);
2194 }
2195 encoding = (const char *) cur->encoding;
2196
2197 if (encoding != NULL) {
Daniel Veillard3814a362007-07-26 11:41:46 +00002198 handler = xmlFindCharEncodingHandler(encoding);
2199 if (handler == NULL) {
2200 xmlFree((char *) cur->encoding);
2201 cur->encoding = NULL;
2202 encoding = NULL;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002203 }
Daniel Veillard3814a362007-07-26 11:41:46 +00002204 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002205 buf = xmlOutputBufferCreateFile(f, handler);
2206 if (buf == NULL) return(-1);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002207 memset(&ctxt, 0, sizeof(ctxt));
2208 ctxt.doc = cur;
2209 ctxt.buf = buf;
2210 ctxt.level = 0;
2211 ctxt.format = format;
2212 ctxt.encoding = (const xmlChar *) encoding;
Daniel Veillard753086a2004-03-28 16:12:44 +00002213 xmlSaveCtxtInit(&ctxt);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002214 xmlDocContentDumpOutput(&ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002215
2216 ret = xmlOutputBufferClose(buf);
2217 return(ret);
2218}
2219
2220/**
2221 * xmlDocDump:
2222 * @f: the FILE*
2223 * @cur: the document
2224 *
2225 * Dump an XML document to an open FILE.
2226 *
2227 * returns: the number of bytes written or -1 in case of failure.
2228 */
2229int
2230xmlDocDump(FILE *f, xmlDocPtr cur) {
2231 return(xmlDocFormatDump (f, cur, 0));
2232}
2233
2234/**
2235 * xmlSaveFileTo:
2236 * @buf: an output I/O buffer
2237 * @cur: the document
2238 * @encoding: the encoding if any assuming the I/O layer handles the trancoding
2239 *
2240 * Dump an XML document to an I/O buffer.
Daniel Veillard3d97e662004-11-04 10:49:00 +00002241 * Warning ! This call xmlOutputBufferClose() on buf which is not available
2242 * after this call.
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002243 *
2244 * returns: the number of bytes written or -1 in case of failure.
2245 */
2246int
2247xmlSaveFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002248 xmlSaveCtxt ctxt;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002249 int ret;
2250
Daniel Veillard3d97e662004-11-04 10:49:00 +00002251 if (buf == NULL) return(-1);
2252 if (cur == NULL) {
2253 xmlOutputBufferClose(buf);
2254 return(-1);
2255 }
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002256 memset(&ctxt, 0, sizeof(ctxt));
2257 ctxt.doc = cur;
2258 ctxt.buf = buf;
2259 ctxt.level = 0;
2260 ctxt.format = 0;
2261 ctxt.encoding = (const xmlChar *) encoding;
Daniel Veillard753086a2004-03-28 16:12:44 +00002262 xmlSaveCtxtInit(&ctxt);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002263 xmlDocContentDumpOutput(&ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002264 ret = xmlOutputBufferClose(buf);
2265 return(ret);
2266}
2267
2268/**
2269 * xmlSaveFormatFileTo:
2270 * @buf: an output I/O buffer
2271 * @cur: the document
2272 * @encoding: the encoding if any assuming the I/O layer handles the trancoding
2273 * @format: should formatting spaces been added
2274 *
2275 * Dump an XML document to an I/O buffer.
Daniel Veillard3d97e662004-11-04 10:49:00 +00002276 * Warning ! This call xmlOutputBufferClose() on buf which is not available
2277 * after this call.
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002278 *
2279 * returns: the number of bytes written or -1 in case of failure.
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002280 */
2281int
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002282xmlSaveFormatFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur,
2283 const char *encoding, int format)
2284{
2285 xmlSaveCtxt ctxt;
2286 int ret;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002287
Daniel Veillard3d97e662004-11-04 10:49:00 +00002288 if (buf == NULL) return(-1);
Daniel Veillardce244ad2004-11-05 10:03:46 +00002289 if ((cur == NULL) ||
2290 ((cur->type != XML_DOCUMENT_NODE) &&
2291 (cur->type != XML_HTML_DOCUMENT_NODE))) {
Daniel Veillard3d97e662004-11-04 10:49:00 +00002292 xmlOutputBufferClose(buf);
2293 return(-1);
2294 }
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002295 memset(&ctxt, 0, sizeof(ctxt));
2296 ctxt.doc = cur;
2297 ctxt.buf = buf;
2298 ctxt.level = 0;
2299 ctxt.format = format;
2300 ctxt.encoding = (const xmlChar *) encoding;
Daniel Veillard753086a2004-03-28 16:12:44 +00002301 xmlSaveCtxtInit(&ctxt);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002302 xmlDocContentDumpOutput(&ctxt, cur);
2303 ret = xmlOutputBufferClose(buf);
2304 return (ret);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002305}
2306
2307/**
2308 * xmlSaveFormatFileEnc:
2309 * @filename: the filename or URL to output
2310 * @cur: the document being saved
2311 * @encoding: the name of the encoding to use or NULL.
2312 * @format: should formatting spaces be added.
2313 *
2314 * Dump an XML document to a file or an URL.
2315 *
2316 * Returns the number of bytes written or -1 in case of error.
2317 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
2318 * or xmlKeepBlanksDefault(0) was called
2319 */
2320int
2321xmlSaveFormatFileEnc( const char * filename, xmlDocPtr cur,
2322 const char * encoding, int format ) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002323 xmlSaveCtxt ctxt;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002324 xmlOutputBufferPtr buf;
2325 xmlCharEncodingHandlerPtr handler = NULL;
2326 int ret;
2327
2328 if (cur == NULL)
2329 return(-1);
2330
2331 if (encoding == NULL)
2332 encoding = (const char *) cur->encoding;
2333
2334 if (encoding != NULL) {
2335
2336 handler = xmlFindCharEncodingHandler(encoding);
2337 if (handler == NULL)
2338 return(-1);
2339 }
2340
2341#ifdef HAVE_ZLIB_H
2342 if (cur->compression < 0) cur->compression = xmlGetCompressMode();
2343#endif
2344 /*
2345 * save the content to a temp buffer.
2346 */
2347 buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression);
2348 if (buf == NULL) return(-1);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002349 memset(&ctxt, 0, sizeof(ctxt));
2350 ctxt.doc = cur;
2351 ctxt.buf = buf;
2352 ctxt.level = 0;
2353 ctxt.format = format;
2354 ctxt.encoding = (const xmlChar *) encoding;
Daniel Veillard753086a2004-03-28 16:12:44 +00002355 xmlSaveCtxtInit(&ctxt);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002356
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002357 xmlDocContentDumpOutput(&ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002358
2359 ret = xmlOutputBufferClose(buf);
2360 return(ret);
2361}
2362
2363
2364/**
2365 * xmlSaveFileEnc:
2366 * @filename: the filename (or URL)
2367 * @cur: the document
2368 * @encoding: the name of an encoding (or NULL)
2369 *
2370 * Dump an XML document, converting it to the given encoding
2371 *
2372 * returns: the number of bytes written or -1 in case of failure.
2373 */
2374int
2375xmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) {
2376 return ( xmlSaveFormatFileEnc( filename, cur, encoding, 0 ) );
2377}
2378
2379/**
2380 * xmlSaveFormatFile:
2381 * @filename: the filename (or URL)
2382 * @cur: the document
2383 * @format: should formatting spaces been added
2384 *
2385 * Dump an XML document to a file. Will use compression if
2386 * compiled in and enabled. If @filename is "-" the stdout file is
2387 * used. If @format is set then the document will be indented on output.
2388 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
2389 * or xmlKeepBlanksDefault(0) was called
2390 *
2391 * returns: the number of bytes written or -1 in case of failure.
2392 */
2393int
2394xmlSaveFormatFile(const char *filename, xmlDocPtr cur, int format) {
2395 return ( xmlSaveFormatFileEnc( filename, cur, NULL, format ) );
2396}
2397
2398/**
2399 * xmlSaveFile:
2400 * @filename: the filename (or URL)
2401 * @cur: the document
2402 *
2403 * Dump an XML document to a file. Will use compression if
2404 * compiled in and enabled. If @filename is "-" the stdout file is
2405 * used.
2406 * returns: the number of bytes written or -1 in case of failure.
2407 */
2408int
2409xmlSaveFile(const char *filename, xmlDocPtr cur) {
2410 return(xmlSaveFormatFileEnc(filename, cur, NULL, 0));
2411}
2412
2413#endif /* LIBXML_OUTPUT_ENABLED */
2414
Daniel Veillard5d4644e2005-04-01 13:11:58 +00002415#define bottom_xmlsave
2416#include "elfgcchack.h"