blob: 29619c7d7d620507fd8b3d59b5b465fbc0f170e0 [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:
125 msg = "string is not in UTF-8";
126 break;
127 case XML_SAVE_CHAR_INVALID:
128 msg = "invalid character value";
129 break;
130 case XML_SAVE_UNKNOWN_ENCODING:
131 msg = "unknown encoding %s";
132 break;
133 case XML_SAVE_NO_DOCTYPE:
134 msg = "document has no DOCTYPE";
135 break;
136 default:
137 msg = "unexpected error number";
138 }
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 }
346}
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000347
348/**
349 * xmlFreeSaveCtxt:
350 *
351 * Free a saving context, destroying the ouptut in any remaining buffer
352 */
353static void
354xmlFreeSaveCtxt(xmlSaveCtxtPtr ctxt)
355{
356 if (ctxt == NULL) return;
357 if (ctxt->encoding != NULL)
358 xmlFree((char *) ctxt->encoding);
Daniel Veillarde2161a62004-04-29 17:14:25 +0000359 if (ctxt->buf != NULL)
360 xmlOutputBufferClose(ctxt->buf);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000361 xmlFree(ctxt);
362}
363
364/**
365 * xmlNewSaveCtxt:
366 *
367 * Create a new saving context
368 *
369 * Returns the new structure or NULL in case of error
370 */
371static xmlSaveCtxtPtr
372xmlNewSaveCtxt(const char *encoding, int options)
373{
374 xmlSaveCtxtPtr ret;
375
376 ret = (xmlSaveCtxtPtr) xmlMalloc(sizeof(xmlSaveCtxt));
377 if (ret == NULL) {
378 xmlSaveErrMemory("creating saving context");
379 return ( NULL );
380 }
381 memset(ret, 0, sizeof(xmlSaveCtxt));
Daniel Veillard6fc5db02005-01-16 00:05:58 +0000382
383 /*
384 * Use the options
385 */
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000386 ret->options = options;
Daniel Veillard6fc5db02005-01-16 00:05:58 +0000387 if (options & XML_SAVE_FORMAT)
388 ret->format = 1;
389
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000390 if (encoding != NULL) {
391 ret->handler = xmlFindCharEncodingHandler(encoding);
392 if (ret->handler == NULL) {
393 xmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL, encoding);
394 xmlFreeSaveCtxt(ret);
395 return(NULL);
396 }
397 ret->encoding = xmlStrdup((const xmlChar *)encoding);
Daniel Veillard3995bc32004-05-15 18:57:31 +0000398 ret->escape = xmlEscapeEntities;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000399 }
Daniel Veillard753086a2004-03-28 16:12:44 +0000400 xmlSaveCtxtInit(ret);
401
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000402 return(ret);
403}
404
405/************************************************************************
406 * *
407 * Dumping XML tree content to a simple buffer *
408 * *
409 ************************************************************************/
410/**
411 * xmlAttrSerializeContent:
412 * @buf: the XML buffer output
413 * @doc: the document
414 * @attr: the attribute pointer
415 *
416 * Serialize the attribute in the buffer
417 */
418static void
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000419xmlAttrSerializeContent(xmlOutputBufferPtr buf, xmlAttrPtr attr)
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000420{
421 xmlNodePtr children;
422
423 children = attr->children;
424 while (children != NULL) {
425 switch (children->type) {
426 case XML_TEXT_NODE:
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000427 xmlAttrSerializeTxtContent(buf->buffer, attr->doc,
428 attr, children->content);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000429 break;
430 case XML_ENTITY_REF_NODE:
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000431 xmlBufferAdd(buf->buffer, BAD_CAST "&", 1);
432 xmlBufferAdd(buf->buffer, children->name,
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000433 xmlStrlen(children->name));
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000434 xmlBufferAdd(buf->buffer, BAD_CAST ";", 1);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000435 break;
436 default:
437 /* should not happen unless we have a badly built tree */
438 break;
439 }
440 children = children->next;
441 }
442}
443
444/************************************************************************
445 * *
446 * Dumping XML tree content to an I/O output buffer *
447 * *
448 ************************************************************************/
449
450#ifdef LIBXML_HTML_ENABLED
451static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000452xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000453#endif
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000454static void xmlNodeListDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur);
455static void xmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000456void xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur);
Daniel Veillardce244ad2004-11-05 10:03:46 +0000457static void xmlDocContentDumpOutput(xmlSaveCtxtPtr ctxt, xmlDocPtr cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000458
459/**
460 * xmlNsDumpOutput:
461 * @buf: the XML buffer output
462 * @cur: a namespace
463 *
464 * Dump a local Namespace definition.
465 * Should be called in the context of attributes dumps.
466 */
467static void
468xmlNsDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000469 if ((cur == NULL) || (buf == NULL)) return;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000470 if ((cur->type == XML_LOCAL_NAMESPACE) && (cur->href != NULL)) {
471 if (xmlStrEqual(cur->prefix, BAD_CAST "xml"))
472 return;
473
474 /* Within the context of an element attributes */
475 if (cur->prefix != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000476 xmlOutputBufferWrite(buf, 7, " xmlns:");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000477 xmlOutputBufferWriteString(buf, (const char *)cur->prefix);
478 } else
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000479 xmlOutputBufferWrite(buf, 6, " xmlns");
480 xmlOutputBufferWrite(buf, 1, "=");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000481 xmlBufferWriteQuotedString(buf->buffer, cur->href);
482 }
483}
484
485/**
486 * xmlNsListDumpOutput:
487 * @buf: the XML buffer output
488 * @cur: the first namespace
489 *
490 * Dump a list of local Namespace definitions.
491 * Should be called in the context of attributes dumps.
492 */
493void
494xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) {
495 while (cur != NULL) {
496 xmlNsDumpOutput(buf, cur);
497 cur = cur->next;
498 }
499}
500
501/**
502 * xmlDtdDumpOutput:
503 * @buf: the XML buffer output
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000504 * @dtd: the pointer to the DTD
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000505 *
506 * Dump the XML document DTD, if any.
507 */
508static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000509xmlDtdDumpOutput(xmlSaveCtxtPtr ctxt, xmlDtdPtr dtd) {
510 xmlOutputBufferPtr buf;
511 int format, level;
512 xmlDocPtr doc;
513
514 if (dtd == NULL) return;
515 if ((ctxt == NULL) || (ctxt->buf == NULL))
516 return;
517 buf = ctxt->buf;
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000518 xmlOutputBufferWrite(buf, 10, "<!DOCTYPE ");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000519 xmlOutputBufferWriteString(buf, (const char *)dtd->name);
520 if (dtd->ExternalID != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000521 xmlOutputBufferWrite(buf, 8, " PUBLIC ");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000522 xmlBufferWriteQuotedString(buf->buffer, dtd->ExternalID);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000523 xmlOutputBufferWrite(buf, 1, " ");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000524 xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID);
525 } else if (dtd->SystemID != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000526 xmlOutputBufferWrite(buf, 8, " SYSTEM ");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000527 xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID);
528 }
529 if ((dtd->entities == NULL) && (dtd->elements == NULL) &&
Daniel Veillard41c4a752004-09-08 20:55:38 +0000530 (dtd->attributes == NULL) && (dtd->notations == NULL) &&
531 (dtd->pentities == NULL)) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000532 xmlOutputBufferWrite(buf, 1, ">");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000533 return;
534 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000535 xmlOutputBufferWrite(buf, 3, " [\n");
Daniel Veillard41c4a752004-09-08 20:55:38 +0000536 /*
537 * Dump the notations first they are not in the DTD children list
538 * Do this only on a standalone DTD or on the internal subset though.
539 */
540 if ((dtd->notations != NULL) && ((dtd->doc == NULL) ||
541 (dtd->doc->intSubset == dtd))) {
Daniel Veillardda3b29a2004-08-14 11:15:13 +0000542 xmlDumpNotationTable(buf->buffer, (xmlNotationTablePtr) dtd->notations);
543 }
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000544 format = ctxt->format;
545 level = ctxt->level;
546 doc = ctxt->doc;
547 ctxt->format = 0;
548 ctxt->level = -1;
549 ctxt->doc = dtd->doc;
550 xmlNodeListDumpOutput(ctxt, dtd->children);
551 ctxt->format = format;
552 ctxt->level = level;
553 ctxt->doc = doc;
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000554 xmlOutputBufferWrite(buf, 2, "]>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000555}
556
557/**
558 * xmlAttrDumpOutput:
559 * @buf: the XML buffer output
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000560 * @cur: the attribute pointer
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000561 *
562 * Dump an XML attribute
563 */
564static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000565xmlAttrDumpOutput(xmlSaveCtxtPtr ctxt, xmlAttrPtr cur) {
566 xmlOutputBufferPtr buf;
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000567
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000568 if (cur == NULL) return;
569 buf = ctxt->buf;
Daniel Veillardce244ad2004-11-05 10:03:46 +0000570 if (buf == NULL) return;
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000571 xmlOutputBufferWrite(buf, 1, " ");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000572 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
573 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000574 xmlOutputBufferWrite(buf, 1, ":");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000575 }
576 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000577 xmlOutputBufferWrite(buf, 2, "=\"");
578 xmlAttrSerializeContent(buf, cur);
579 xmlOutputBufferWrite(buf, 1, "\"");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000580}
581
582/**
583 * xmlAttrListDumpOutput:
584 * @buf: the XML buffer output
585 * @doc: the document
586 * @cur: the first attribute pointer
587 * @encoding: an optional encoding string
588 *
589 * Dump a list of XML attributes
590 */
591static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000592xmlAttrListDumpOutput(xmlSaveCtxtPtr ctxt, xmlAttrPtr cur) {
593 if (cur == NULL) return;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000594 while (cur != NULL) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000595 xmlAttrDumpOutput(ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000596 cur = cur->next;
597 }
598}
599
600
601
602/**
603 * xmlNodeListDumpOutput:
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000604 * @cur: the first node
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000605 *
606 * Dump an XML node list, recursive behaviour, children are printed too.
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000607 */
608static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000609xmlNodeListDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000610 xmlOutputBufferPtr buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000611
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000612 if (cur == NULL) return;
613 buf = ctxt->buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000614 while (cur != NULL) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000615 if ((ctxt->format) && (xmlIndentTreeOutput) &&
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000616 (cur->type == XML_ELEMENT_NODE))
Daniel Veillard753086a2004-03-28 16:12:44 +0000617 xmlOutputBufferWrite(buf, ctxt->indent_size *
618 (ctxt->level > ctxt->indent_nr ?
619 ctxt->indent_nr : ctxt->level),
620 ctxt->indent);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000621 xmlNodeDumpOutputInternal(ctxt, cur);
622 if (ctxt->format) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000623 xmlOutputBufferWrite(buf, 1, "\n");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000624 }
625 cur = cur->next;
626 }
627}
628
629/**
630 * xmlNodeDumpOutputInternal:
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000631 * @cur: the current node
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000632 *
633 * Dump an XML node, recursive behaviour, children are printed too.
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000634 */
635static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000636xmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
Daniel Veillard753086a2004-03-28 16:12:44 +0000637 int format;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000638 xmlNodePtr tmp;
639 xmlChar *start, *end;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000640 xmlOutputBufferPtr buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000641
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000642 if (cur == NULL) return;
643 buf = ctxt->buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000644 if (cur->type == XML_XINCLUDE_START)
645 return;
646 if (cur->type == XML_XINCLUDE_END)
647 return;
Daniel Veillardce244ad2004-11-05 10:03:46 +0000648 if ((cur->type == XML_DOCUMENT_NODE) ||
649 (cur->type == XML_HTML_DOCUMENT_NODE)) {
650 xmlDocContentDumpOutput(ctxt, (xmlDocPtr) cur);
651 return;
652 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000653 if (cur->type == XML_DTD_NODE) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000654 xmlDtdDumpOutput(ctxt, (xmlDtdPtr) cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000655 return;
656 }
657 if (cur->type == XML_DOCUMENT_FRAG_NODE) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000658 xmlNodeListDumpOutput(ctxt, cur->children);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000659 return;
660 }
661 if (cur->type == XML_ELEMENT_DECL) {
662 xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur);
663 return;
664 }
665 if (cur->type == XML_ATTRIBUTE_DECL) {
666 xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur);
667 return;
668 }
669 if (cur->type == XML_ENTITY_DECL) {
670 xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur);
671 return;
672 }
673 if (cur->type == XML_TEXT_NODE) {
674 if (cur->content != NULL) {
William M. Brack4e1c2db2005-02-11 10:58:55 +0000675 if (cur->name != xmlStringTextNoenc) {
Daniel Veillard3995bc32004-05-15 18:57:31 +0000676 xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000677 } else {
678 /*
679 * Disable escaping, needed for XSLT
680 */
681 xmlOutputBufferWriteString(buf, (const char *) cur->content);
682 }
683 }
684
685 return;
686 }
687 if (cur->type == XML_PI_NODE) {
688 if (cur->content != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000689 xmlOutputBufferWrite(buf, 2, "<?");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000690 xmlOutputBufferWriteString(buf, (const char *)cur->name);
691 if (cur->content != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000692 xmlOutputBufferWrite(buf, 1, " ");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000693 xmlOutputBufferWriteString(buf, (const char *)cur->content);
694 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000695 xmlOutputBufferWrite(buf, 2, "?>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000696 } else {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000697 xmlOutputBufferWrite(buf, 2, "<?");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000698 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000699 xmlOutputBufferWrite(buf, 2, "?>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000700 }
701 return;
702 }
703 if (cur->type == XML_COMMENT_NODE) {
704 if (cur->content != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000705 xmlOutputBufferWrite(buf, 4, "<!--");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000706 xmlOutputBufferWriteString(buf, (const char *)cur->content);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000707 xmlOutputBufferWrite(buf, 3, "-->");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000708 }
709 return;
710 }
711 if (cur->type == XML_ENTITY_REF_NODE) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000712 xmlOutputBufferWrite(buf, 1, "&");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000713 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000714 xmlOutputBufferWrite(buf, 1, ";");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000715 return;
716 }
717 if (cur->type == XML_CDATA_SECTION_NODE) {
Daniel Veillard7cd517c2005-05-20 18:47:22 +0000718 if (cur->content == NULL) {
719 xmlOutputBufferWrite(buf, 12, "<![CDATA[]]>");
720 } else {
721 start = end = cur->content;
722 while (*end != '\0') {
723 if ((*end == ']') && (*(end + 1) == ']') &&
724 (*(end + 2) == '>')) {
725 end = end + 2;
726 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
727 xmlOutputBufferWrite(buf, end - start, (const char *)start);
728 xmlOutputBufferWrite(buf, 3, "]]>");
729 start = end;
730 }
731 end++;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000732 }
Daniel Veillard7cd517c2005-05-20 18:47:22 +0000733 if (start != end) {
734 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
735 xmlOutputBufferWriteString(buf, (const char *)start);
736 xmlOutputBufferWrite(buf, 3, "]]>");
737 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000738 }
739 return;
740 }
741 if (cur->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000742 xmlAttrDumpOutput(ctxt, (xmlAttrPtr) cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000743 return;
744 }
745 if (cur->type == XML_NAMESPACE_DECL) {
746 xmlNsDumpOutput(buf, (xmlNsPtr) cur);
747 return;
748 }
749
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000750 format = ctxt->format;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000751 if (format == 1) {
752 tmp = cur->children;
753 while (tmp != NULL) {
754 if ((tmp->type == XML_TEXT_NODE) ||
755 (tmp->type == XML_CDATA_SECTION_NODE) ||
756 (tmp->type == XML_ENTITY_REF_NODE)) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000757 ctxt->format = 0;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000758 break;
759 }
760 tmp = tmp->next;
761 }
762 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000763 xmlOutputBufferWrite(buf, 1, "<");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000764 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
765 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000766 xmlOutputBufferWrite(buf, 1, ":");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000767 }
768
769 xmlOutputBufferWriteString(buf, (const char *)cur->name);
770 if (cur->nsDef)
771 xmlNsListDumpOutput(buf, cur->nsDef);
772 if (cur->properties != NULL)
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000773 xmlAttrListDumpOutput(ctxt, cur->properties);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000774
775 if (((cur->type == XML_ELEMENT_NODE) || (cur->content == NULL)) &&
776 (cur->children == NULL) && (!xmlSaveNoEmptyTags)) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000777 xmlOutputBufferWrite(buf, 2, "/>");
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000778 ctxt->format = format;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000779 return;
780 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000781 xmlOutputBufferWrite(buf, 1, ">");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000782 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Daniel Veillard3995bc32004-05-15 18:57:31 +0000783 xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000784 }
785 if (cur->children != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000786 if (ctxt->format) xmlOutputBufferWrite(buf, 1, "\n");
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000787 if (ctxt->level >= 0) ctxt->level++;
788 xmlNodeListDumpOutput(ctxt, cur->children);
789 if (ctxt->level > 0) ctxt->level--;
790 if ((xmlIndentTreeOutput) && (ctxt->format))
Daniel Veillard753086a2004-03-28 16:12:44 +0000791 xmlOutputBufferWrite(buf, ctxt->indent_size *
792 (ctxt->level > ctxt->indent_nr ?
793 ctxt->indent_nr : ctxt->level),
794 ctxt->indent);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000795 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000796 xmlOutputBufferWrite(buf, 2, "</");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000797 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
798 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000799 xmlOutputBufferWrite(buf, 1, ":");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000800 }
801
802 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000803 xmlOutputBufferWrite(buf, 1, ">");
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000804 ctxt->format = format;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000805}
806
807/**
808 * xmlDocContentDumpOutput:
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000809 * @cur: the document
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000810 *
811 * Dump an XML document.
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000812 */
813static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000814xmlDocContentDumpOutput(xmlSaveCtxtPtr ctxt, xmlDocPtr cur) {
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000815#ifdef LIBXML_HTML_ENABLED
816 xmlDtdPtr dtd;
817 int is_xhtml = 0;
818#endif
819 const xmlChar *oldenc = cur->encoding;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000820 const xmlChar *encoding = ctxt->encoding;
821 xmlOutputBufferPtr buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000822
823 xmlInitParser();
824
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000825 if (ctxt->encoding != NULL)
826 cur->encoding = BAD_CAST ctxt->encoding;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000827
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000828 buf = ctxt->buf;
Daniel Veillard100e1802005-08-08 14:44:11 +0000829 if ((ctxt->options & XML_SAVE_NO_DECL) == 0) {
830 xmlOutputBufferWrite(buf, 14, "<?xml version=");
831 if (cur->version != NULL)
832 xmlBufferWriteQuotedString(buf->buffer, cur->version);
833 else
834 xmlOutputBufferWrite(buf, 5, "\"1.0\"");
835 if (ctxt->encoding == NULL) {
836 if (cur->encoding != NULL)
837 encoding = cur->encoding;
838 else if (cur->charset != XML_CHAR_ENCODING_UTF8)
839 encoding = (const xmlChar *)
840 xmlGetCharEncodingName((xmlCharEncoding) cur->charset);
841 }
842 if (encoding != NULL) {
843 xmlOutputBufferWrite(buf, 10, " encoding=");
844 xmlBufferWriteQuotedString(buf->buffer, (xmlChar *) encoding);
845 }
846 switch (cur->standalone) {
847 case 0:
848 xmlOutputBufferWrite(buf, 16, " standalone=\"no\"");
849 break;
850 case 1:
851 xmlOutputBufferWrite(buf, 17, " standalone=\"yes\"");
852 break;
853 }
854 xmlOutputBufferWrite(buf, 3, "?>\n");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000855 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000856
857#ifdef LIBXML_HTML_ENABLED
858 dtd = xmlGetIntSubset(cur);
859 if (dtd != NULL) {
860 is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
861 if (is_xhtml < 0) is_xhtml = 0;
862 }
863 if (is_xhtml) {
864 if (encoding != NULL)
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000865 htmlSetMetaEncoding(cur, (const xmlChar *) ctxt->encoding);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000866 else
867 htmlSetMetaEncoding(cur, BAD_CAST "UTF-8");
868 }
869#endif
870 if (cur->children != NULL) {
871 xmlNodePtr child = cur->children;
872
873 while (child != NULL) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000874 ctxt->level = 0;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000875#ifdef LIBXML_HTML_ENABLED
876 if (is_xhtml)
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000877 xhtmlNodeDumpOutput(ctxt, child);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000878 else
879#endif
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000880 xmlNodeDumpOutputInternal(ctxt, child);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000881 xmlOutputBufferWrite(buf, 1, "\n");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000882 child = child->next;
883 }
884 }
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000885 if (ctxt->encoding != NULL)
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000886 cur->encoding = oldenc;
887}
888
889#ifdef LIBXML_HTML_ENABLED
890/************************************************************************
891 * *
892 * Functions specific to XHTML serialization *
893 * *
894 ************************************************************************/
895
896/**
897 * xhtmlIsEmpty:
898 * @node: the node
899 *
900 * Check if a node is an empty xhtml node
901 *
902 * Returns 1 if the node is an empty node, 0 if not and -1 in case of error
903 */
904static int
905xhtmlIsEmpty(xmlNodePtr node) {
906 if (node == NULL)
907 return(-1);
908 if (node->type != XML_ELEMENT_NODE)
909 return(0);
910 if ((node->ns != NULL) && (!xmlStrEqual(node->ns->href, XHTML_NS_NAME)))
911 return(0);
912 if (node->children != NULL)
913 return(0);
914 switch (node->name[0]) {
915 case 'a':
916 if (xmlStrEqual(node->name, BAD_CAST "area"))
917 return(1);
918 return(0);
919 case 'b':
920 if (xmlStrEqual(node->name, BAD_CAST "br"))
921 return(1);
922 if (xmlStrEqual(node->name, BAD_CAST "base"))
923 return(1);
924 if (xmlStrEqual(node->name, BAD_CAST "basefont"))
925 return(1);
926 return(0);
927 case 'c':
928 if (xmlStrEqual(node->name, BAD_CAST "col"))
929 return(1);
930 return(0);
931 case 'f':
932 if (xmlStrEqual(node->name, BAD_CAST "frame"))
933 return(1);
934 return(0);
935 case 'h':
936 if (xmlStrEqual(node->name, BAD_CAST "hr"))
937 return(1);
938 return(0);
939 case 'i':
940 if (xmlStrEqual(node->name, BAD_CAST "img"))
941 return(1);
942 if (xmlStrEqual(node->name, BAD_CAST "input"))
943 return(1);
944 if (xmlStrEqual(node->name, BAD_CAST "isindex"))
945 return(1);
946 return(0);
947 case 'l':
948 if (xmlStrEqual(node->name, BAD_CAST "link"))
949 return(1);
950 return(0);
951 case 'm':
952 if (xmlStrEqual(node->name, BAD_CAST "meta"))
953 return(1);
954 return(0);
955 case 'p':
956 if (xmlStrEqual(node->name, BAD_CAST "param"))
957 return(1);
958 return(0);
959 }
960 return(0);
961}
962
963/**
964 * xhtmlAttrListDumpOutput:
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000965 * @cur: the first attribute pointer
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000966 *
967 * Dump a list of XML attributes
968 */
969static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000970xhtmlAttrListDumpOutput(xmlSaveCtxtPtr ctxt, xmlAttrPtr cur) {
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000971 xmlAttrPtr xml_lang = NULL;
972 xmlAttrPtr lang = NULL;
973 xmlAttrPtr name = NULL;
974 xmlAttrPtr id = NULL;
975 xmlNodePtr parent;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000976 xmlOutputBufferPtr buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000977
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000978 if (cur == NULL) return;
979 buf = ctxt->buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000980 parent = cur->parent;
981 while (cur != NULL) {
982 if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "id")))
983 id = cur;
984 else
985 if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "name")))
986 name = cur;
987 else
988 if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")))
989 lang = cur;
990 else
991 if ((cur->ns != NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")) &&
992 (xmlStrEqual(cur->ns->prefix, BAD_CAST "xml")))
993 xml_lang = cur;
994 else if ((cur->ns == NULL) &&
995 ((cur->children == NULL) ||
996 (cur->children->content == NULL) ||
997 (cur->children->content[0] == 0)) &&
998 (htmlIsBooleanAttr(cur->name))) {
999 if (cur->children != NULL)
1000 xmlFreeNode(cur->children);
1001 cur->children = xmlNewText(cur->name);
1002 if (cur->children != NULL)
1003 cur->children->parent = (xmlNodePtr) cur;
1004 }
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001005 xmlAttrDumpOutput(ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001006 cur = cur->next;
1007 }
1008 /*
1009 * C.8
1010 */
1011 if ((name != NULL) && (id == NULL)) {
1012 if ((parent != NULL) && (parent->name != NULL) &&
1013 ((xmlStrEqual(parent->name, BAD_CAST "a")) ||
1014 (xmlStrEqual(parent->name, BAD_CAST "p")) ||
1015 (xmlStrEqual(parent->name, BAD_CAST "div")) ||
1016 (xmlStrEqual(parent->name, BAD_CAST "img")) ||
1017 (xmlStrEqual(parent->name, BAD_CAST "map")) ||
1018 (xmlStrEqual(parent->name, BAD_CAST "applet")) ||
1019 (xmlStrEqual(parent->name, BAD_CAST "form")) ||
1020 (xmlStrEqual(parent->name, BAD_CAST "frame")) ||
1021 (xmlStrEqual(parent->name, BAD_CAST "iframe")))) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001022 xmlOutputBufferWrite(buf, 5, " id=\"");
1023 xmlAttrSerializeContent(buf, name);
1024 xmlOutputBufferWrite(buf, 1, "\"");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001025 }
1026 }
1027 /*
1028 * C.7.
1029 */
1030 if ((lang != NULL) && (xml_lang == NULL)) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001031 xmlOutputBufferWrite(buf, 11, " xml:lang=\"");
1032 xmlAttrSerializeContent(buf, lang);
1033 xmlOutputBufferWrite(buf, 1, "\"");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001034 } else
1035 if ((xml_lang != NULL) && (lang == NULL)) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001036 xmlOutputBufferWrite(buf, 7, " lang=\"");
1037 xmlAttrSerializeContent(buf, xml_lang);
1038 xmlOutputBufferWrite(buf, 1, "\"");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001039 }
1040}
1041
1042/**
1043 * xhtmlNodeListDumpOutput:
1044 * @buf: the XML buffer output
1045 * @doc: the XHTML document
1046 * @cur: the first node
1047 * @level: the imbrication level for indenting
1048 * @format: is formatting allowed
1049 * @encoding: an optional encoding string
1050 *
1051 * Dump an XML node list, recursive behaviour, children are printed too.
1052 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
1053 * or xmlKeepBlanksDefault(0) was called
1054 */
1055static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001056xhtmlNodeListDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001057 xmlOutputBufferPtr buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001058
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001059 if (cur == NULL) return;
1060 buf = ctxt->buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001061 while (cur != NULL) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001062 if ((ctxt->format) && (xmlIndentTreeOutput) &&
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001063 (cur->type == XML_ELEMENT_NODE))
Daniel Veillard753086a2004-03-28 16:12:44 +00001064 xmlOutputBufferWrite(buf, ctxt->indent_size *
1065 (ctxt->level > ctxt->indent_nr ?
1066 ctxt->indent_nr : ctxt->level),
1067 ctxt->indent);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001068 xhtmlNodeDumpOutput(ctxt, cur);
1069 if (ctxt->format) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001070 xmlOutputBufferWrite(buf, 1, "\n");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001071 }
1072 cur = cur->next;
1073 }
1074}
1075
1076/**
1077 * xhtmlNodeDumpOutput:
1078 * @buf: the XML buffer output
1079 * @doc: the XHTML document
1080 * @cur: the current node
1081 * @level: the imbrication level for indenting
1082 * @format: is formatting allowed
1083 * @encoding: an optional encoding string
1084 *
1085 * Dump an XHTML node, recursive behaviour, children are printed too.
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001086 */
1087static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001088xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
Daniel Veillard753086a2004-03-28 16:12:44 +00001089 int format;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001090 xmlNodePtr tmp;
1091 xmlChar *start, *end;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001092 xmlOutputBufferPtr buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001093
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001094 if (cur == NULL) return;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001095 if (cur->type == XML_XINCLUDE_START)
1096 return;
1097 if (cur->type == XML_XINCLUDE_END)
1098 return;
1099 if (cur->type == XML_DTD_NODE) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001100 xmlDtdDumpOutput(ctxt, (xmlDtdPtr) cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001101 return;
1102 }
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001103 buf = ctxt->buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001104 if (cur->type == XML_ELEMENT_DECL) {
1105 xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur);
1106 return;
1107 }
1108 if (cur->type == XML_ATTRIBUTE_DECL) {
1109 xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur);
1110 return;
1111 }
1112 if (cur->type == XML_ENTITY_DECL) {
1113 xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur);
1114 return;
1115 }
1116 if (cur->type == XML_TEXT_NODE) {
1117 if (cur->content != NULL) {
1118 if ((cur->name == xmlStringText) ||
1119 (cur->name != xmlStringTextNoenc)) {
Daniel Veillard3995bc32004-05-15 18:57:31 +00001120 xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001121 } else {
1122 /*
1123 * Disable escaping, needed for XSLT
1124 */
1125 xmlOutputBufferWriteString(buf, (const char *) cur->content);
1126 }
1127 }
1128
1129 return;
1130 }
1131 if (cur->type == XML_PI_NODE) {
1132 if (cur->content != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001133 xmlOutputBufferWrite(buf, 2, "<?");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001134 xmlOutputBufferWriteString(buf, (const char *)cur->name);
1135 if (cur->content != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001136 xmlOutputBufferWrite(buf, 1, " ");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001137 xmlOutputBufferWriteString(buf, (const char *)cur->content);
1138 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001139 xmlOutputBufferWrite(buf, 2, "?>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001140 } else {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001141 xmlOutputBufferWrite(buf, 2, "<?");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001142 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001143 xmlOutputBufferWrite(buf, 2, "?>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001144 }
1145 return;
1146 }
1147 if (cur->type == XML_COMMENT_NODE) {
1148 if (cur->content != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001149 xmlOutputBufferWrite(buf, 4, "<!--");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001150 xmlOutputBufferWriteString(buf, (const char *)cur->content);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001151 xmlOutputBufferWrite(buf, 3, "-->");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001152 }
1153 return;
1154 }
1155 if (cur->type == XML_ENTITY_REF_NODE) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001156 xmlOutputBufferWrite(buf, 1, "&");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001157 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001158 xmlOutputBufferWrite(buf, 1, ";");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001159 return;
1160 }
1161 if (cur->type == XML_CDATA_SECTION_NODE) {
1162 start = end = cur->content;
1163 while (*end != '\0') {
1164 if (*end == ']' && *(end + 1) == ']' && *(end + 2) == '>') {
1165 end = end + 2;
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001166 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001167 xmlOutputBufferWrite(buf, end - start, (const char *)start);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001168 xmlOutputBufferWrite(buf, 3, "]]>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001169 start = end;
1170 }
1171 end++;
1172 }
1173 if (start != end) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001174 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001175 xmlOutputBufferWriteString(buf, (const char *)start);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001176 xmlOutputBufferWrite(buf, 3, "]]>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001177 }
1178 return;
1179 }
1180
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001181 format = ctxt->format;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001182 if (format == 1) {
1183 tmp = cur->children;
1184 while (tmp != NULL) {
1185 if ((tmp->type == XML_TEXT_NODE) ||
1186 (tmp->type == XML_ENTITY_REF_NODE)) {
1187 format = 0;
1188 break;
1189 }
1190 tmp = tmp->next;
1191 }
1192 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001193 xmlOutputBufferWrite(buf, 1, "<");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001194 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1195 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001196 xmlOutputBufferWrite(buf, 1, ":");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001197 }
1198
1199 xmlOutputBufferWriteString(buf, (const char *)cur->name);
1200 if (cur->nsDef)
1201 xmlNsListDumpOutput(buf, cur->nsDef);
1202 if ((xmlStrEqual(cur->name, BAD_CAST "html") &&
1203 (cur->ns == NULL) && (cur->nsDef == NULL))) {
1204 /*
1205 * 3.1.1. Strictly Conforming Documents A.3.1.1 3/
1206 */
1207 xmlOutputBufferWriteString(buf,
1208 " xmlns=\"http://www.w3.org/1999/xhtml\"");
1209 }
1210 if (cur->properties != NULL)
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001211 xhtmlAttrListDumpOutput(ctxt, cur->properties);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001212
1213 if ((cur->type == XML_ELEMENT_NODE) && (cur->children == NULL)) {
1214 if (((cur->ns == NULL) || (cur->ns->prefix == NULL)) &&
1215 (xhtmlIsEmpty(cur) == 1)) {
1216 /*
1217 * C.2. Empty Elements
1218 */
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001219 xmlOutputBufferWrite(buf, 3, " />");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001220 } else {
1221 /*
1222 * C.3. Element Minimization and Empty Element Content
1223 */
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001224 xmlOutputBufferWrite(buf, 3, "></");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001225 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1226 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001227 xmlOutputBufferWrite(buf, 1, ":");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001228 }
1229 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001230 xmlOutputBufferWrite(buf, 1, ">");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001231 }
1232 return;
1233 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001234 xmlOutputBufferWrite(buf, 1, ">");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001235 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Daniel Veillard3995bc32004-05-15 18:57:31 +00001236 xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001237 }
1238
1239 /*
1240 * 4.8. Script and Style elements
1241 */
1242 if ((cur->type == XML_ELEMENT_NODE) &&
1243 ((xmlStrEqual(cur->name, BAD_CAST "script")) ||
1244 (xmlStrEqual(cur->name, BAD_CAST "style"))) &&
1245 ((cur->ns == NULL) ||
1246 (xmlStrEqual(cur->ns->href, XHTML_NS_NAME)))) {
1247 xmlNodePtr child = cur->children;
1248
1249 while (child != NULL) {
1250 if ((child->type == XML_TEXT_NODE) ||
1251 (child->type == XML_CDATA_SECTION_NODE)) {
1252 /*
1253 * Apparently CDATA escaping for style just break on IE,
1254 * mozilla and galeon, so ...
1255 */
1256 if (xmlStrEqual(cur->name, BAD_CAST "style") &&
1257 (xmlStrchr(child->content, '<') == NULL) &&
1258 (xmlStrchr(child->content, '>') == NULL) &&
1259 (xmlStrchr(child->content, '&') == NULL)) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001260 int level = ctxt->level;
1261 int indent = ctxt->format;
1262
1263 ctxt->level = 0;
1264 ctxt->format = 0;
1265 xhtmlNodeDumpOutput(ctxt, child);
1266 ctxt->level = level;
1267 ctxt->format = indent;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001268 } else {
1269 start = end = child->content;
1270 while (*end != '\0') {
1271 if (*end == ']' &&
1272 *(end + 1) == ']' &&
1273 *(end + 2) == '>') {
1274 end = end + 2;
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001275 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001276 xmlOutputBufferWrite(buf, end - start,
1277 (const char *)start);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001278 xmlOutputBufferWrite(buf, 3, "]]>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001279 start = end;
1280 }
1281 end++;
1282 }
1283 if (start != end) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001284 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
1285 xmlOutputBufferWrite(buf, end - start,
1286 (const char *)start);
1287 xmlOutputBufferWrite(buf, 3, "]]>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001288 }
1289 }
1290 } else {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001291 int level = ctxt->level;
1292 int indent = ctxt->format;
1293
1294 ctxt->level = 0;
1295 ctxt->format = 0;
1296 xhtmlNodeDumpOutput(ctxt, child);
1297 ctxt->level = level;
1298 ctxt->format = indent;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001299 }
1300 child = child->next;
1301 }
1302 } else if (cur->children != NULL) {
Daniel Veillardf0244ce2004-05-09 23:48:39 +00001303 int indent = ctxt->format;
1304
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001305 if (format) xmlOutputBufferWrite(buf, 1, "\n");
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001306 if (ctxt->level >= 0) ctxt->level++;
Daniel Veillardf0244ce2004-05-09 23:48:39 +00001307 ctxt->format = format;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001308 xhtmlNodeListDumpOutput(ctxt, cur->children);
1309 if (ctxt->level > 0) ctxt->level--;
Daniel Veillardf0244ce2004-05-09 23:48:39 +00001310 ctxt->format = indent;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001311 if ((xmlIndentTreeOutput) && (format))
Daniel Veillard753086a2004-03-28 16:12:44 +00001312 xmlOutputBufferWrite(buf, ctxt->indent_size *
1313 (ctxt->level > ctxt->indent_nr ?
1314 ctxt->indent_nr : ctxt->level),
1315 ctxt->indent);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001316 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001317 xmlOutputBufferWrite(buf, 2, "</");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001318 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1319 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001320 xmlOutputBufferWrite(buf, 1, ":");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001321 }
1322
1323 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001324 xmlOutputBufferWrite(buf, 1, ">");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001325}
1326#endif
1327
1328/************************************************************************
1329 * *
1330 * Public entry points *
1331 * *
1332 ************************************************************************/
1333
1334/**
1335 * xmlSaveToFd:
1336 * @fd: a file descriptor number
1337 * @encoding: the encoding name to use or NULL
1338 * @options: a set of xmlSaveOptions
1339 *
1340 * Create a document saving context serializing to a file descriptor
1341 * with the encoding and the options given.
1342 *
1343 * Returns a new serialization context or NULL in case of error.
1344 */
1345xmlSaveCtxtPtr
1346xmlSaveToFd(int fd, const char *encoding, int options)
1347{
1348 xmlSaveCtxtPtr ret;
1349
1350 ret = xmlNewSaveCtxt(encoding, options);
1351 if (ret == NULL) return(NULL);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001352 ret->buf = xmlOutputBufferCreateFd(fd, ret->handler);
1353 if (ret->buf == NULL) {
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001354 xmlFreeSaveCtxt(ret);
1355 return(NULL);
1356 }
1357 return(ret);
1358}
1359
1360/**
1361 * xmlSaveToFilename:
1362 * @filename: a file name or an URL
1363 * @encoding: the encoding name to use or NULL
1364 * @options: a set of xmlSaveOptions
1365 *
1366 * Create a document saving context serializing to a filename or possibly
1367 * to an URL (but this is less reliable) with the encoding and the options
1368 * given.
1369 *
1370 * Returns a new serialization context or NULL in case of error.
1371 */
1372xmlSaveCtxtPtr
1373xmlSaveToFilename(const char *filename, const char *encoding, int options)
1374{
1375 xmlSaveCtxtPtr ret;
1376 int compression = 0; /* TODO handle compression option */
1377
1378 ret = xmlNewSaveCtxt(encoding, options);
1379 if (ret == NULL) return(NULL);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001380 ret->buf = xmlOutputBufferCreateFilename(filename, ret->handler,
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001381 compression);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001382 if (ret->buf == NULL) {
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001383 xmlFreeSaveCtxt(ret);
1384 return(NULL);
1385 }
1386 return(ret);
1387}
1388
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001389/**
1390 * xmlSaveToBuffer:
1391 * @buffer: a buffer
1392 * @encoding: the encoding name to use or NULL
1393 * @options: a set of xmlSaveOptions
1394 *
1395 * Create a document saving context serializing to a buffer
1396 * with the encoding and the options given
1397 *
1398 * Returns a new serialization context or NULL in case of error.
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001399xmlSaveCtxtPtr
1400xmlSaveToBuffer(xmlBufferPtr buffer, const char *encoding, int options)
1401{
1402 TODO
1403 return(NULL);
1404}
Daniel Veillarda2351322004-06-27 12:08:10 +00001405 */
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001406
1407/**
1408 * xmlSaveToIO:
1409 * @iowrite: an I/O write function
1410 * @ioclose: an I/O close function
1411 * @ioctx: an I/O handler
1412 * @encoding: the encoding name to use or NULL
1413 * @options: a set of xmlSaveOptions
1414 *
1415 * Create a document saving context serializing to a file descriptor
1416 * with the encoding and the options given
1417 *
1418 * Returns a new serialization context or NULL in case of error.
1419 */
1420xmlSaveCtxtPtr
1421xmlSaveToIO(xmlOutputWriteCallback iowrite,
1422 xmlOutputCloseCallback ioclose,
1423 void *ioctx, const char *encoding, int options)
1424{
1425 xmlSaveCtxtPtr ret;
1426
1427 ret = xmlNewSaveCtxt(encoding, options);
1428 if (ret == NULL) return(NULL);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001429 ret->buf = xmlOutputBufferCreateIO(iowrite, ioclose, ioctx, ret->handler);
1430 if (ret->buf == NULL) {
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001431 xmlFreeSaveCtxt(ret);
1432 return(NULL);
1433 }
1434 return(ret);
1435}
1436
1437/**
1438 * xmlSaveDoc:
1439 * @ctxt: a document saving context
1440 * @doc: a document
1441 *
1442 * Save a full document to a saving context
Daniel Veillard377e1a92004-04-16 16:30:05 +00001443 * TODO: The function is not fully implemented yet as it does not return the
1444 * byte count but 0 instead
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001445 *
1446 * Returns the number of byte written or -1 in case of error
1447 */
1448long
1449xmlSaveDoc(xmlSaveCtxtPtr ctxt, xmlDocPtr doc)
1450{
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001451 long ret = 0;
1452
Daniel Veillardce682bc2004-11-05 17:22:25 +00001453 if ((ctxt == NULL) || (doc == NULL)) return(-1);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001454 xmlDocContentDumpOutput(ctxt, doc);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001455 return(ret);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001456}
1457
1458/**
1459 * xmlSaveTree:
1460 * @ctxt: a document saving context
1461 * @node: a document
1462 *
1463 * Save a subtree starting at the node parameter to a saving context
Daniel Veillard377e1a92004-04-16 16:30:05 +00001464 * TODO: The function is not fully implemented yet as it does not return the
1465 * byte count but 0 instead
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001466 *
1467 * Returns the number of byte written or -1 in case of error
1468 */
1469long
1470xmlSaveTree(xmlSaveCtxtPtr ctxt, xmlNodePtr node)
1471{
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001472 long ret = 0;
1473
Daniel Veillardce682bc2004-11-05 17:22:25 +00001474 if ((ctxt == NULL) || (node == NULL)) return(-1);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001475 xmlNodeDumpOutputInternal(ctxt, node);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001476 return(ret);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001477}
1478
1479/**
1480 * xmlSaveFlush:
1481 * @ctxt: a document saving context
1482 *
1483 * Flush a document saving context, i.e. make sure that all bytes have
1484 * been output.
1485 *
1486 * Returns the number of byte written or -1 in case of error.
1487 */
1488int
1489xmlSaveFlush(xmlSaveCtxtPtr ctxt)
1490{
1491 if (ctxt == NULL) return(-1);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001492 if (ctxt->buf == NULL) return(-1);
1493 return(xmlOutputBufferFlush(ctxt->buf));
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001494}
1495
1496/**
1497 * xmlSaveClose:
1498 * @ctxt: a document saving context
1499 *
1500 * Close a document saving context, i.e. make sure that all bytes have
1501 * been output and free the associated data.
1502 *
1503 * Returns the number of byte written or -1 in case of error.
1504 */
1505int
1506xmlSaveClose(xmlSaveCtxtPtr ctxt)
1507{
1508 int ret;
1509
1510 if (ctxt == NULL) return(-1);
1511 ret = xmlSaveFlush(ctxt);
1512 xmlFreeSaveCtxt(ctxt);
1513 return(ret);
1514}
1515
Daniel Veillard3995bc32004-05-15 18:57:31 +00001516/**
1517 * xmlSaveSetEscape:
1518 * @ctxt: a document saving context
1519 * @escape: the escaping function
1520 *
1521 * Set a custom escaping function to be used for text in element content
1522 *
1523 * Returns 0 if successful or -1 in case of error.
1524 */
1525int
1526xmlSaveSetEscape(xmlSaveCtxtPtr ctxt, xmlCharEncodingOutputFunc escape)
1527{
1528 if (ctxt == NULL) return(-1);
1529 ctxt->escape = escape;
1530 return(0);
1531}
1532
1533/**
1534 * xmlSaveSetAttrEscape:
1535 * @ctxt: a document saving context
1536 * @escape: the escaping function
1537 *
1538 * Set a custom escaping function to be used for text in attribute content
1539 *
1540 * Returns 0 if successful or -1 in case of error.
1541 */
1542int
1543xmlSaveSetAttrEscape(xmlSaveCtxtPtr ctxt, xmlCharEncodingOutputFunc escape)
1544{
1545 if (ctxt == NULL) return(-1);
1546 ctxt->escapeAttr = escape;
1547 return(0);
1548}
1549
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001550/************************************************************************
1551 * *
1552 * Public entry points based on buffers *
1553 * *
1554 ************************************************************************/
1555/**
1556 * xmlAttrSerializeTxtContent:
1557 * @buf: the XML buffer output
1558 * @doc: the document
1559 * @attr: the attribute node
1560 * @string: the text content
1561 *
1562 * Serialize text attribute values to an xml simple buffer
1563 */
1564void
1565xmlAttrSerializeTxtContent(xmlBufferPtr buf, xmlDocPtr doc,
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001566 xmlAttrPtr attr, const xmlChar * string)
1567{
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001568 xmlChar *base, *cur;
1569
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001570 if (string == NULL)
1571 return;
1572 base = cur = (xmlChar *) string;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001573 while (*cur != 0) {
1574 if (*cur == '\n') {
1575 if (base != cur)
1576 xmlBufferAdd(buf, base, cur - base);
1577 xmlBufferAdd(buf, BAD_CAST "&#10;", 5);
1578 cur++;
1579 base = cur;
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001580 } else if (*cur == '\r') {
1581 if (base != cur)
1582 xmlBufferAdd(buf, base, cur - base);
1583 xmlBufferAdd(buf, BAD_CAST "&#13;", 5);
1584 cur++;
1585 base = cur;
1586 } else if (*cur == '\t') {
1587 if (base != cur)
1588 xmlBufferAdd(buf, base, cur - base);
1589 xmlBufferAdd(buf, BAD_CAST "&#9;", 4);
1590 cur++;
1591 base = cur;
1592 } else if (*cur == '"') {
1593 if (base != cur)
1594 xmlBufferAdd(buf, base, cur - base);
1595 xmlBufferAdd(buf, BAD_CAST "&quot;", 6);
1596 cur++;
1597 base = cur;
1598 } else if (*cur == '<') {
1599 if (base != cur)
1600 xmlBufferAdd(buf, base, cur - base);
1601 xmlBufferAdd(buf, BAD_CAST "&lt;", 4);
1602 cur++;
1603 base = cur;
1604 } else if (*cur == '>') {
1605 if (base != cur)
1606 xmlBufferAdd(buf, base, cur - base);
1607 xmlBufferAdd(buf, BAD_CAST "&gt;", 4);
1608 cur++;
1609 base = cur;
1610 } else if (*cur == '&') {
1611 if (base != cur)
1612 xmlBufferAdd(buf, base, cur - base);
1613 xmlBufferAdd(buf, BAD_CAST "&amp;", 5);
1614 cur++;
1615 base = cur;
1616 } else if ((*cur >= 0x80) && ((doc == NULL) ||
1617 (doc->encoding == NULL))) {
1618 /*
1619 * We assume we have UTF-8 content.
1620 */
1621 unsigned char tmp[10];
1622 int val = 0, l = 1;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001623
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001624 if (base != cur)
1625 xmlBufferAdd(buf, base, cur - base);
1626 if (*cur < 0xC0) {
1627 xmlSaveErr(XML_SAVE_NOT_UTF8, (xmlNodePtr) attr, NULL);
1628 if (doc != NULL)
1629 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
1630 xmlSerializeHexCharRef(tmp, *cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001631 xmlBufferAdd(buf, (xmlChar *) tmp, -1);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001632 cur++;
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001633 base = cur;
1634 continue;
1635 } else if (*cur < 0xE0) {
1636 val = (cur[0]) & 0x1F;
1637 val <<= 6;
1638 val |= (cur[1]) & 0x3F;
1639 l = 2;
1640 } else if (*cur < 0xF0) {
1641 val = (cur[0]) & 0x0F;
1642 val <<= 6;
1643 val |= (cur[1]) & 0x3F;
1644 val <<= 6;
1645 val |= (cur[2]) & 0x3F;
1646 l = 3;
1647 } else if (*cur < 0xF8) {
1648 val = (cur[0]) & 0x07;
1649 val <<= 6;
1650 val |= (cur[1]) & 0x3F;
1651 val <<= 6;
1652 val |= (cur[2]) & 0x3F;
1653 val <<= 6;
1654 val |= (cur[3]) & 0x3F;
1655 l = 4;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001656 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001657 if ((l == 1) || (!IS_CHAR(val))) {
1658 xmlSaveErr(XML_SAVE_CHAR_INVALID, (xmlNodePtr) attr, NULL);
1659 if (doc != NULL)
1660 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
1661
1662 xmlSerializeHexCharRef(tmp, *cur);
1663 xmlBufferAdd(buf, (xmlChar *) tmp, -1);
1664 cur++;
1665 base = cur;
1666 continue;
1667 }
1668 /*
1669 * We could do multiple things here. Just save
1670 * as a char ref
1671 */
1672 xmlSerializeHexCharRef(tmp, val);
1673 xmlBufferAdd(buf, (xmlChar *) tmp, -1);
1674 cur += l;
1675 base = cur;
1676 } else {
1677 cur++;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001678 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001679 }
1680 if (base != cur)
1681 xmlBufferAdd(buf, base, cur - base);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001682}
1683
1684/**
1685 * xmlNodeDump:
1686 * @buf: the XML buffer output
1687 * @doc: the document
1688 * @cur: the current node
1689 * @level: the imbrication level for indenting
1690 * @format: is formatting allowed
1691 *
1692 * Dump an XML node, recursive behaviour,children are printed too.
1693 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
1694 * or xmlKeepBlanksDefault(0) was called
1695 *
1696 * Returns the number of bytes written to the buffer or -1 in case of error
1697 */
1698int
1699xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
1700 int format)
1701{
1702 unsigned int use;
1703 int ret;
1704 xmlOutputBufferPtr outbuf;
1705
1706 xmlInitParser();
1707
1708 if (cur == NULL) {
1709#ifdef DEBUG_TREE
1710 xmlGenericError(xmlGenericErrorContext,
1711 "xmlNodeDump : node == NULL\n");
1712#endif
1713 return (-1);
1714 }
1715 if (buf == NULL) {
1716#ifdef DEBUG_TREE
1717 xmlGenericError(xmlGenericErrorContext,
1718 "xmlNodeDump : buf == NULL\n");
1719#endif
1720 return (-1);
1721 }
1722 outbuf = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer));
1723 if (outbuf == NULL) {
1724 xmlSaveErrMemory("creating buffer");
1725 return (-1);
1726 }
1727 memset(outbuf, 0, (size_t) sizeof(xmlOutputBuffer));
1728 outbuf->buffer = buf;
1729 outbuf->encoder = NULL;
1730 outbuf->writecallback = NULL;
1731 outbuf->closecallback = NULL;
1732 outbuf->context = NULL;
1733 outbuf->written = 0;
1734
1735 use = buf->use;
1736 xmlNodeDumpOutput(outbuf, doc, cur, level, format, NULL);
1737 xmlFree(outbuf);
1738 ret = buf->use - use;
1739 return (ret);
1740}
1741
1742/**
1743 * xmlElemDump:
1744 * @f: the FILE * for the output
1745 * @doc: the document
1746 * @cur: the current node
1747 *
1748 * Dump an XML/HTML node, recursive behaviour, children are printed too.
1749 */
1750void
1751xmlElemDump(FILE * f, xmlDocPtr doc, xmlNodePtr cur)
1752{
1753 xmlOutputBufferPtr outbuf;
1754
1755 xmlInitParser();
1756
1757 if (cur == NULL) {
1758#ifdef DEBUG_TREE
1759 xmlGenericError(xmlGenericErrorContext,
1760 "xmlElemDump : cur == NULL\n");
1761#endif
1762 return;
1763 }
1764#ifdef DEBUG_TREE
1765 if (doc == NULL) {
1766 xmlGenericError(xmlGenericErrorContext,
1767 "xmlElemDump : doc == NULL\n");
1768 }
1769#endif
1770
1771 outbuf = xmlOutputBufferCreateFile(f, NULL);
1772 if (outbuf == NULL)
1773 return;
1774 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
1775#ifdef LIBXML_HTML_ENABLED
1776 htmlNodeDumpOutput(outbuf, doc, cur, NULL);
1777#else
1778 xmlSaveErr(XML_ERR_INTERNAL_ERROR, cur, "HTML support not compiled in\n");
1779#endif /* LIBXML_HTML_ENABLED */
1780 } else
1781 xmlNodeDumpOutput(outbuf, doc, cur, 0, 1, NULL);
1782 xmlOutputBufferClose(outbuf);
1783}
1784
1785/************************************************************************
1786 * *
1787 * Saving functions front-ends *
1788 * *
1789 ************************************************************************/
1790
1791/**
1792 * xmlNodeDumpOutput:
1793 * @buf: the XML buffer output
1794 * @doc: the document
1795 * @cur: the current node
1796 * @level: the imbrication level for indenting
1797 * @format: is formatting allowed
1798 * @encoding: an optional encoding string
1799 *
1800 * Dump an XML node, recursive behaviour, children are printed too.
1801 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
1802 * or xmlKeepBlanksDefault(0) was called
1803 */
1804void
1805xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
1806 int level, int format, const char *encoding)
1807{
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001808 xmlSaveCtxt ctxt;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001809#ifdef LIBXML_HTML_ENABLED
1810 xmlDtdPtr dtd;
1811 int is_xhtml = 0;
1812#endif
1813
1814 xmlInitParser();
1815
Daniel Veillardce244ad2004-11-05 10:03:46 +00001816 if ((buf == NULL) || (cur == NULL)) return;
1817
Daniel Veillard64354ea2005-03-31 15:22:56 +00001818 if (encoding == NULL)
1819 encoding = "UTF-8";
1820
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001821 memset(&ctxt, 0, sizeof(ctxt));
1822 ctxt.doc = doc;
1823 ctxt.buf = buf;
1824 ctxt.level = level;
1825 ctxt.format = format;
1826 ctxt.encoding = (const xmlChar *) encoding;
Daniel Veillard753086a2004-03-28 16:12:44 +00001827 xmlSaveCtxtInit(&ctxt);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001828
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001829#ifdef LIBXML_HTML_ENABLED
1830 dtd = xmlGetIntSubset(doc);
1831 if (dtd != NULL) {
1832 is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
1833 if (is_xhtml < 0)
1834 is_xhtml = 0;
1835 if ((is_xhtml) && (cur->parent == (xmlNodePtr) doc) &&
1836 (cur->type == XML_ELEMENT_NODE) &&
1837 (xmlStrEqual(cur->name, BAD_CAST "html"))) {
1838 if (encoding != NULL)
1839 htmlSetMetaEncoding((htmlDocPtr) doc,
1840 (const xmlChar *) encoding);
1841 else
1842 htmlSetMetaEncoding((htmlDocPtr) doc, BAD_CAST "UTF-8");
1843 }
1844 }
1845
1846 if (is_xhtml)
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001847 xhtmlNodeDumpOutput(&ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001848 else
1849#endif
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001850 xmlNodeDumpOutputInternal(&ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001851}
1852
1853/**
1854 * xmlDocDumpFormatMemoryEnc:
1855 * @out_doc: Document to generate XML text from
1856 * @doc_txt_ptr: Memory pointer for allocated XML text
1857 * @doc_txt_len: Length of the generated XML text
1858 * @txt_encoding: Character encoding to use when generating XML text
1859 * @format: should formatting spaces been added
1860 *
1861 * Dump the current DOM tree into memory using the character encoding specified
1862 * by the caller. Note it is up to the caller of this function to free the
1863 * allocated memory with xmlFree().
1864 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
1865 * or xmlKeepBlanksDefault(0) was called
1866 */
1867
1868void
1869xmlDocDumpFormatMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
1870 int * doc_txt_len, const char * txt_encoding,
1871 int format) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001872 xmlSaveCtxt ctxt;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001873 int dummy = 0;
1874 xmlOutputBufferPtr out_buff = NULL;
1875 xmlCharEncodingHandlerPtr conv_hdlr = NULL;
1876
1877 if (doc_txt_len == NULL) {
1878 doc_txt_len = &dummy; /* Continue, caller just won't get length */
1879 }
1880
1881 if (doc_txt_ptr == NULL) {
1882 *doc_txt_len = 0;
1883 return;
1884 }
1885
1886 *doc_txt_ptr = NULL;
1887 *doc_txt_len = 0;
1888
1889 if (out_doc == NULL) {
1890 /* No document, no output */
1891 return;
1892 }
1893
1894 /*
1895 * Validate the encoding value, if provided.
1896 * This logic is copied from xmlSaveFileEnc.
1897 */
1898
1899 if (txt_encoding == NULL)
1900 txt_encoding = (const char *) out_doc->encoding;
1901 if (txt_encoding != NULL) {
1902 conv_hdlr = xmlFindCharEncodingHandler(txt_encoding);
1903 if ( conv_hdlr == NULL ) {
1904 xmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, (xmlNodePtr) out_doc,
1905 txt_encoding);
1906 return;
1907 }
1908 }
1909
1910 if ((out_buff = xmlAllocOutputBuffer(conv_hdlr)) == NULL ) {
1911 xmlSaveErrMemory("creating buffer");
1912 return;
1913 }
1914
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001915 memset(&ctxt, 0, sizeof(ctxt));
1916 ctxt.doc = out_doc;
1917 ctxt.buf = out_buff;
1918 ctxt.level = 0;
1919 ctxt.format = format;
1920 ctxt.encoding = (const xmlChar *) txt_encoding;
Daniel Veillard753086a2004-03-28 16:12:44 +00001921 xmlSaveCtxtInit(&ctxt);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001922 xmlDocContentDumpOutput(&ctxt, out_doc);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001923 xmlOutputBufferFlush(out_buff);
1924 if (out_buff->conv != NULL) {
1925 *doc_txt_len = out_buff->conv->use;
1926 *doc_txt_ptr = xmlStrndup(out_buff->conv->content, *doc_txt_len);
1927 } else {
1928 *doc_txt_len = out_buff->buffer->use;
1929 *doc_txt_ptr = xmlStrndup(out_buff->buffer->content, *doc_txt_len);
1930 }
1931 (void)xmlOutputBufferClose(out_buff);
1932
1933 if ((*doc_txt_ptr == NULL) && (*doc_txt_len > 0)) {
1934 *doc_txt_len = 0;
1935 xmlSaveErrMemory("creating output");
1936 }
1937
1938 return;
1939}
1940
1941/**
1942 * xmlDocDumpMemory:
1943 * @cur: the document
1944 * @mem: OUT: the memory pointer
1945 * @size: OUT: the memory length
1946 *
1947 * Dump an XML document in memory and return the #xmlChar * and it's size
1948 * in bytes. It's up to the caller to free the memory with xmlFree().
1949 * The resulting byte array is zero terminated, though the last 0 is not
1950 * included in the returned size.
1951 */
1952void
1953xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) {
1954 xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, 0);
1955}
1956
1957/**
1958 * xmlDocDumpFormatMemory:
1959 * @cur: the document
1960 * @mem: OUT: the memory pointer
1961 * @size: OUT: the memory length
1962 * @format: should formatting spaces been added
1963 *
1964 *
1965 * Dump an XML document in memory and return the #xmlChar * and it's size.
1966 * It's up to the caller to free the memory with xmlFree().
1967 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
1968 * or xmlKeepBlanksDefault(0) was called
1969 */
1970void
1971xmlDocDumpFormatMemory(xmlDocPtr cur, xmlChar**mem, int *size, int format) {
1972 xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, format);
1973}
1974
1975/**
1976 * xmlDocDumpMemoryEnc:
1977 * @out_doc: Document to generate XML text from
1978 * @doc_txt_ptr: Memory pointer for allocated XML text
1979 * @doc_txt_len: Length of the generated XML text
1980 * @txt_encoding: Character encoding to use when generating XML text
1981 *
1982 * Dump the current DOM tree into memory using the character encoding specified
1983 * by the caller. Note it is up to the caller of this function to free the
1984 * allocated memory with xmlFree().
1985 */
1986
1987void
1988xmlDocDumpMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
1989 int * doc_txt_len, const char * txt_encoding) {
1990 xmlDocDumpFormatMemoryEnc(out_doc, doc_txt_ptr, doc_txt_len,
1991 txt_encoding, 0);
1992}
1993
1994/**
1995 * xmlDocFormatDump:
1996 * @f: the FILE*
1997 * @cur: the document
1998 * @format: should formatting spaces been added
1999 *
2000 * Dump an XML document to an open FILE.
2001 *
2002 * returns: the number of bytes written or -1 in case of failure.
2003 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
2004 * or xmlKeepBlanksDefault(0) was called
2005 */
2006int
2007xmlDocFormatDump(FILE *f, xmlDocPtr cur, int format) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002008 xmlSaveCtxt ctxt;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002009 xmlOutputBufferPtr buf;
2010 const char * encoding;
2011 xmlCharEncodingHandlerPtr handler = NULL;
2012 int ret;
2013
2014 if (cur == NULL) {
2015#ifdef DEBUG_TREE
2016 xmlGenericError(xmlGenericErrorContext,
2017 "xmlDocDump : document == NULL\n");
2018#endif
2019 return(-1);
2020 }
2021 encoding = (const char *) cur->encoding;
2022
2023 if (encoding != NULL) {
2024 handler = xmlFindCharEncodingHandler(encoding);
2025 if (handler == NULL) {
2026 xmlFree((char *) cur->encoding);
2027 cur->encoding = NULL;
2028 }
2029 }
2030 buf = xmlOutputBufferCreateFile(f, handler);
2031 if (buf == NULL) return(-1);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002032 memset(&ctxt, 0, sizeof(ctxt));
2033 ctxt.doc = cur;
2034 ctxt.buf = buf;
2035 ctxt.level = 0;
2036 ctxt.format = format;
2037 ctxt.encoding = (const xmlChar *) encoding;
Daniel Veillard753086a2004-03-28 16:12:44 +00002038 xmlSaveCtxtInit(&ctxt);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002039 xmlDocContentDumpOutput(&ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002040
2041 ret = xmlOutputBufferClose(buf);
2042 return(ret);
2043}
2044
2045/**
2046 * xmlDocDump:
2047 * @f: the FILE*
2048 * @cur: the document
2049 *
2050 * Dump an XML document to an open FILE.
2051 *
2052 * returns: the number of bytes written or -1 in case of failure.
2053 */
2054int
2055xmlDocDump(FILE *f, xmlDocPtr cur) {
2056 return(xmlDocFormatDump (f, cur, 0));
2057}
2058
2059/**
2060 * xmlSaveFileTo:
2061 * @buf: an output I/O buffer
2062 * @cur: the document
2063 * @encoding: the encoding if any assuming the I/O layer handles the trancoding
2064 *
2065 * Dump an XML document to an I/O buffer.
Daniel Veillard3d97e662004-11-04 10:49:00 +00002066 * Warning ! This call xmlOutputBufferClose() on buf which is not available
2067 * after this call.
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002068 *
2069 * returns: the number of bytes written or -1 in case of failure.
2070 */
2071int
2072xmlSaveFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002073 xmlSaveCtxt ctxt;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002074 int ret;
2075
Daniel Veillard3d97e662004-11-04 10:49:00 +00002076 if (buf == NULL) return(-1);
2077 if (cur == NULL) {
2078 xmlOutputBufferClose(buf);
2079 return(-1);
2080 }
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002081 memset(&ctxt, 0, sizeof(ctxt));
2082 ctxt.doc = cur;
2083 ctxt.buf = buf;
2084 ctxt.level = 0;
2085 ctxt.format = 0;
2086 ctxt.encoding = (const xmlChar *) encoding;
Daniel Veillard753086a2004-03-28 16:12:44 +00002087 xmlSaveCtxtInit(&ctxt);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002088 xmlDocContentDumpOutput(&ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002089 ret = xmlOutputBufferClose(buf);
2090 return(ret);
2091}
2092
2093/**
2094 * xmlSaveFormatFileTo:
2095 * @buf: an output I/O buffer
2096 * @cur: the document
2097 * @encoding: the encoding if any assuming the I/O layer handles the trancoding
2098 * @format: should formatting spaces been added
2099 *
2100 * Dump an XML document to an I/O buffer.
Daniel Veillard3d97e662004-11-04 10:49:00 +00002101 * Warning ! This call xmlOutputBufferClose() on buf which is not available
2102 * after this call.
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002103 *
2104 * returns: the number of bytes written or -1 in case of failure.
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002105 */
2106int
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002107xmlSaveFormatFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur,
2108 const char *encoding, int format)
2109{
2110 xmlSaveCtxt ctxt;
2111 int ret;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002112
Daniel Veillard3d97e662004-11-04 10:49:00 +00002113 if (buf == NULL) return(-1);
Daniel Veillardce244ad2004-11-05 10:03:46 +00002114 if ((cur == NULL) ||
2115 ((cur->type != XML_DOCUMENT_NODE) &&
2116 (cur->type != XML_HTML_DOCUMENT_NODE))) {
Daniel Veillard3d97e662004-11-04 10:49:00 +00002117 xmlOutputBufferClose(buf);
2118 return(-1);
2119 }
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002120 memset(&ctxt, 0, sizeof(ctxt));
2121 ctxt.doc = cur;
2122 ctxt.buf = buf;
2123 ctxt.level = 0;
2124 ctxt.format = format;
2125 ctxt.encoding = (const xmlChar *) encoding;
Daniel Veillard753086a2004-03-28 16:12:44 +00002126 xmlSaveCtxtInit(&ctxt);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002127 xmlDocContentDumpOutput(&ctxt, cur);
2128 ret = xmlOutputBufferClose(buf);
2129 return (ret);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002130}
2131
2132/**
2133 * xmlSaveFormatFileEnc:
2134 * @filename: the filename or URL to output
2135 * @cur: the document being saved
2136 * @encoding: the name of the encoding to use or NULL.
2137 * @format: should formatting spaces be added.
2138 *
2139 * Dump an XML document to a file or an URL.
2140 *
2141 * Returns the number of bytes written or -1 in case of error.
2142 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
2143 * or xmlKeepBlanksDefault(0) was called
2144 */
2145int
2146xmlSaveFormatFileEnc( const char * filename, xmlDocPtr cur,
2147 const char * encoding, int format ) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002148 xmlSaveCtxt ctxt;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002149 xmlOutputBufferPtr buf;
2150 xmlCharEncodingHandlerPtr handler = NULL;
2151 int ret;
2152
2153 if (cur == NULL)
2154 return(-1);
2155
2156 if (encoding == NULL)
2157 encoding = (const char *) cur->encoding;
2158
2159 if (encoding != NULL) {
2160
2161 handler = xmlFindCharEncodingHandler(encoding);
2162 if (handler == NULL)
2163 return(-1);
2164 }
2165
2166#ifdef HAVE_ZLIB_H
2167 if (cur->compression < 0) cur->compression = xmlGetCompressMode();
2168#endif
2169 /*
2170 * save the content to a temp buffer.
2171 */
2172 buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression);
2173 if (buf == NULL) return(-1);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002174 memset(&ctxt, 0, sizeof(ctxt));
2175 ctxt.doc = cur;
2176 ctxt.buf = buf;
2177 ctxt.level = 0;
2178 ctxt.format = format;
2179 ctxt.encoding = (const xmlChar *) encoding;
Daniel Veillard753086a2004-03-28 16:12:44 +00002180 xmlSaveCtxtInit(&ctxt);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002181
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002182 xmlDocContentDumpOutput(&ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002183
2184 ret = xmlOutputBufferClose(buf);
2185 return(ret);
2186}
2187
2188
2189/**
2190 * xmlSaveFileEnc:
2191 * @filename: the filename (or URL)
2192 * @cur: the document
2193 * @encoding: the name of an encoding (or NULL)
2194 *
2195 * Dump an XML document, converting it to the given encoding
2196 *
2197 * returns: the number of bytes written or -1 in case of failure.
2198 */
2199int
2200xmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) {
2201 return ( xmlSaveFormatFileEnc( filename, cur, encoding, 0 ) );
2202}
2203
2204/**
2205 * xmlSaveFormatFile:
2206 * @filename: the filename (or URL)
2207 * @cur: the document
2208 * @format: should formatting spaces been added
2209 *
2210 * Dump an XML document to a file. Will use compression if
2211 * compiled in and enabled. If @filename is "-" the stdout file is
2212 * used. If @format is set then the document will be indented on output.
2213 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
2214 * or xmlKeepBlanksDefault(0) was called
2215 *
2216 * returns: the number of bytes written or -1 in case of failure.
2217 */
2218int
2219xmlSaveFormatFile(const char *filename, xmlDocPtr cur, int format) {
2220 return ( xmlSaveFormatFileEnc( filename, cur, NULL, format ) );
2221}
2222
2223/**
2224 * xmlSaveFile:
2225 * @filename: the filename (or URL)
2226 * @cur: the document
2227 *
2228 * Dump an XML document to a file. Will use compression if
2229 * compiled in and enabled. If @filename is "-" the stdout file is
2230 * used.
2231 * returns: the number of bytes written or -1 in case of failure.
2232 */
2233int
2234xmlSaveFile(const char *filename, xmlDocPtr cur) {
2235 return(xmlSaveFormatFileEnc(filename, cur, NULL, 0));
2236}
2237
2238#endif /* LIBXML_OUTPUT_ENABLED */
2239
Daniel Veillard5d4644e2005-04-01 13:11:58 +00002240#define bottom_xmlsave
2241#include "elfgcchack.h"