blob: d6e3e65aa5a94f5e2912708880e91acf7fa9c665 [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 Veillard7a6361f2004-05-15 16:37:50 +0000829 xmlOutputBufferWrite(buf, 14, "<?xml version=");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000830 if (cur->version != NULL)
831 xmlBufferWriteQuotedString(buf->buffer, cur->version);
832 else
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000833 xmlOutputBufferWrite(buf, 5, "\"1.0\"");
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000834 if (ctxt->encoding == NULL) {
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000835 if (cur->encoding != NULL)
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000836 encoding = cur->encoding;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000837 else if (cur->charset != XML_CHAR_ENCODING_UTF8)
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000838 encoding = (const xmlChar *)
839 xmlGetCharEncodingName((xmlCharEncoding) cur->charset);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000840 }
841 if (encoding != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000842 xmlOutputBufferWrite(buf, 10, " encoding=");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000843 xmlBufferWriteQuotedString(buf->buffer, (xmlChar *) encoding);
844 }
845 switch (cur->standalone) {
846 case 0:
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000847 xmlOutputBufferWrite(buf, 16, " standalone=\"no\"");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000848 break;
849 case 1:
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000850 xmlOutputBufferWrite(buf, 17, " standalone=\"yes\"");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000851 break;
852 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000853 xmlOutputBufferWrite(buf, 3, "?>\n");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000854
855#ifdef LIBXML_HTML_ENABLED
856 dtd = xmlGetIntSubset(cur);
857 if (dtd != NULL) {
858 is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
859 if (is_xhtml < 0) is_xhtml = 0;
860 }
861 if (is_xhtml) {
862 if (encoding != NULL)
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000863 htmlSetMetaEncoding(cur, (const xmlChar *) ctxt->encoding);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000864 else
865 htmlSetMetaEncoding(cur, BAD_CAST "UTF-8");
866 }
867#endif
868 if (cur->children != NULL) {
869 xmlNodePtr child = cur->children;
870
871 while (child != NULL) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000872 ctxt->level = 0;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000873#ifdef LIBXML_HTML_ENABLED
874 if (is_xhtml)
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000875 xhtmlNodeDumpOutput(ctxt, child);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000876 else
877#endif
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000878 xmlNodeDumpOutputInternal(ctxt, child);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000879 xmlOutputBufferWrite(buf, 1, "\n");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000880 child = child->next;
881 }
882 }
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000883 if (ctxt->encoding != NULL)
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000884 cur->encoding = oldenc;
885}
886
887#ifdef LIBXML_HTML_ENABLED
888/************************************************************************
889 * *
890 * Functions specific to XHTML serialization *
891 * *
892 ************************************************************************/
893
894/**
895 * xhtmlIsEmpty:
896 * @node: the node
897 *
898 * Check if a node is an empty xhtml node
899 *
900 * Returns 1 if the node is an empty node, 0 if not and -1 in case of error
901 */
902static int
903xhtmlIsEmpty(xmlNodePtr node) {
904 if (node == NULL)
905 return(-1);
906 if (node->type != XML_ELEMENT_NODE)
907 return(0);
908 if ((node->ns != NULL) && (!xmlStrEqual(node->ns->href, XHTML_NS_NAME)))
909 return(0);
910 if (node->children != NULL)
911 return(0);
912 switch (node->name[0]) {
913 case 'a':
914 if (xmlStrEqual(node->name, BAD_CAST "area"))
915 return(1);
916 return(0);
917 case 'b':
918 if (xmlStrEqual(node->name, BAD_CAST "br"))
919 return(1);
920 if (xmlStrEqual(node->name, BAD_CAST "base"))
921 return(1);
922 if (xmlStrEqual(node->name, BAD_CAST "basefont"))
923 return(1);
924 return(0);
925 case 'c':
926 if (xmlStrEqual(node->name, BAD_CAST "col"))
927 return(1);
928 return(0);
929 case 'f':
930 if (xmlStrEqual(node->name, BAD_CAST "frame"))
931 return(1);
932 return(0);
933 case 'h':
934 if (xmlStrEqual(node->name, BAD_CAST "hr"))
935 return(1);
936 return(0);
937 case 'i':
938 if (xmlStrEqual(node->name, BAD_CAST "img"))
939 return(1);
940 if (xmlStrEqual(node->name, BAD_CAST "input"))
941 return(1);
942 if (xmlStrEqual(node->name, BAD_CAST "isindex"))
943 return(1);
944 return(0);
945 case 'l':
946 if (xmlStrEqual(node->name, BAD_CAST "link"))
947 return(1);
948 return(0);
949 case 'm':
950 if (xmlStrEqual(node->name, BAD_CAST "meta"))
951 return(1);
952 return(0);
953 case 'p':
954 if (xmlStrEqual(node->name, BAD_CAST "param"))
955 return(1);
956 return(0);
957 }
958 return(0);
959}
960
961/**
962 * xhtmlAttrListDumpOutput:
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000963 * @cur: the first attribute pointer
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000964 *
965 * Dump a list of XML attributes
966 */
967static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000968xhtmlAttrListDumpOutput(xmlSaveCtxtPtr ctxt, xmlAttrPtr cur) {
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000969 xmlAttrPtr xml_lang = NULL;
970 xmlAttrPtr lang = NULL;
971 xmlAttrPtr name = NULL;
972 xmlAttrPtr id = NULL;
973 xmlNodePtr parent;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000974 xmlOutputBufferPtr buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000975
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000976 if (cur == NULL) return;
977 buf = ctxt->buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000978 parent = cur->parent;
979 while (cur != NULL) {
980 if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "id")))
981 id = cur;
982 else
983 if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "name")))
984 name = cur;
985 else
986 if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")))
987 lang = cur;
988 else
989 if ((cur->ns != NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")) &&
990 (xmlStrEqual(cur->ns->prefix, BAD_CAST "xml")))
991 xml_lang = cur;
992 else if ((cur->ns == NULL) &&
993 ((cur->children == NULL) ||
994 (cur->children->content == NULL) ||
995 (cur->children->content[0] == 0)) &&
996 (htmlIsBooleanAttr(cur->name))) {
997 if (cur->children != NULL)
998 xmlFreeNode(cur->children);
999 cur->children = xmlNewText(cur->name);
1000 if (cur->children != NULL)
1001 cur->children->parent = (xmlNodePtr) cur;
1002 }
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001003 xmlAttrDumpOutput(ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001004 cur = cur->next;
1005 }
1006 /*
1007 * C.8
1008 */
1009 if ((name != NULL) && (id == NULL)) {
1010 if ((parent != NULL) && (parent->name != NULL) &&
1011 ((xmlStrEqual(parent->name, BAD_CAST "a")) ||
1012 (xmlStrEqual(parent->name, BAD_CAST "p")) ||
1013 (xmlStrEqual(parent->name, BAD_CAST "div")) ||
1014 (xmlStrEqual(parent->name, BAD_CAST "img")) ||
1015 (xmlStrEqual(parent->name, BAD_CAST "map")) ||
1016 (xmlStrEqual(parent->name, BAD_CAST "applet")) ||
1017 (xmlStrEqual(parent->name, BAD_CAST "form")) ||
1018 (xmlStrEqual(parent->name, BAD_CAST "frame")) ||
1019 (xmlStrEqual(parent->name, BAD_CAST "iframe")))) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001020 xmlOutputBufferWrite(buf, 5, " id=\"");
1021 xmlAttrSerializeContent(buf, name);
1022 xmlOutputBufferWrite(buf, 1, "\"");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001023 }
1024 }
1025 /*
1026 * C.7.
1027 */
1028 if ((lang != NULL) && (xml_lang == NULL)) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001029 xmlOutputBufferWrite(buf, 11, " xml:lang=\"");
1030 xmlAttrSerializeContent(buf, lang);
1031 xmlOutputBufferWrite(buf, 1, "\"");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001032 } else
1033 if ((xml_lang != NULL) && (lang == NULL)) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001034 xmlOutputBufferWrite(buf, 7, " lang=\"");
1035 xmlAttrSerializeContent(buf, xml_lang);
1036 xmlOutputBufferWrite(buf, 1, "\"");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001037 }
1038}
1039
1040/**
1041 * xhtmlNodeListDumpOutput:
1042 * @buf: the XML buffer output
1043 * @doc: the XHTML document
1044 * @cur: the first node
1045 * @level: the imbrication level for indenting
1046 * @format: is formatting allowed
1047 * @encoding: an optional encoding string
1048 *
1049 * Dump an XML node list, recursive behaviour, children are printed too.
1050 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
1051 * or xmlKeepBlanksDefault(0) was called
1052 */
1053static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001054xhtmlNodeListDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001055 xmlOutputBufferPtr buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001056
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001057 if (cur == NULL) return;
1058 buf = ctxt->buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001059 while (cur != NULL) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001060 if ((ctxt->format) && (xmlIndentTreeOutput) &&
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001061 (cur->type == XML_ELEMENT_NODE))
Daniel Veillard753086a2004-03-28 16:12:44 +00001062 xmlOutputBufferWrite(buf, ctxt->indent_size *
1063 (ctxt->level > ctxt->indent_nr ?
1064 ctxt->indent_nr : ctxt->level),
1065 ctxt->indent);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001066 xhtmlNodeDumpOutput(ctxt, cur);
1067 if (ctxt->format) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001068 xmlOutputBufferWrite(buf, 1, "\n");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001069 }
1070 cur = cur->next;
1071 }
1072}
1073
1074/**
1075 * xhtmlNodeDumpOutput:
1076 * @buf: the XML buffer output
1077 * @doc: the XHTML document
1078 * @cur: the current node
1079 * @level: the imbrication level for indenting
1080 * @format: is formatting allowed
1081 * @encoding: an optional encoding string
1082 *
1083 * Dump an XHTML node, recursive behaviour, children are printed too.
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001084 */
1085static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001086xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
Daniel Veillard753086a2004-03-28 16:12:44 +00001087 int format;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001088 xmlNodePtr tmp;
1089 xmlChar *start, *end;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001090 xmlOutputBufferPtr buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001091
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001092 if (cur == NULL) return;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001093 if (cur->type == XML_XINCLUDE_START)
1094 return;
1095 if (cur->type == XML_XINCLUDE_END)
1096 return;
1097 if (cur->type == XML_DTD_NODE) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001098 xmlDtdDumpOutput(ctxt, (xmlDtdPtr) cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001099 return;
1100 }
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001101 buf = ctxt->buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001102 if (cur->type == XML_ELEMENT_DECL) {
1103 xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur);
1104 return;
1105 }
1106 if (cur->type == XML_ATTRIBUTE_DECL) {
1107 xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur);
1108 return;
1109 }
1110 if (cur->type == XML_ENTITY_DECL) {
1111 xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur);
1112 return;
1113 }
1114 if (cur->type == XML_TEXT_NODE) {
1115 if (cur->content != NULL) {
1116 if ((cur->name == xmlStringText) ||
1117 (cur->name != xmlStringTextNoenc)) {
Daniel Veillard3995bc32004-05-15 18:57:31 +00001118 xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001119 } else {
1120 /*
1121 * Disable escaping, needed for XSLT
1122 */
1123 xmlOutputBufferWriteString(buf, (const char *) cur->content);
1124 }
1125 }
1126
1127 return;
1128 }
1129 if (cur->type == XML_PI_NODE) {
1130 if (cur->content != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001131 xmlOutputBufferWrite(buf, 2, "<?");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001132 xmlOutputBufferWriteString(buf, (const char *)cur->name);
1133 if (cur->content != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001134 xmlOutputBufferWrite(buf, 1, " ");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001135 xmlOutputBufferWriteString(buf, (const char *)cur->content);
1136 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001137 xmlOutputBufferWrite(buf, 2, "?>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001138 } else {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001139 xmlOutputBufferWrite(buf, 2, "<?");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001140 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001141 xmlOutputBufferWrite(buf, 2, "?>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001142 }
1143 return;
1144 }
1145 if (cur->type == XML_COMMENT_NODE) {
1146 if (cur->content != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001147 xmlOutputBufferWrite(buf, 4, "<!--");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001148 xmlOutputBufferWriteString(buf, (const char *)cur->content);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001149 xmlOutputBufferWrite(buf, 3, "-->");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001150 }
1151 return;
1152 }
1153 if (cur->type == XML_ENTITY_REF_NODE) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001154 xmlOutputBufferWrite(buf, 1, "&");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001155 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001156 xmlOutputBufferWrite(buf, 1, ";");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001157 return;
1158 }
1159 if (cur->type == XML_CDATA_SECTION_NODE) {
1160 start = end = cur->content;
1161 while (*end != '\0') {
1162 if (*end == ']' && *(end + 1) == ']' && *(end + 2) == '>') {
1163 end = end + 2;
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001164 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001165 xmlOutputBufferWrite(buf, end - start, (const char *)start);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001166 xmlOutputBufferWrite(buf, 3, "]]>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001167 start = end;
1168 }
1169 end++;
1170 }
1171 if (start != end) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001172 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001173 xmlOutputBufferWriteString(buf, (const char *)start);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001174 xmlOutputBufferWrite(buf, 3, "]]>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001175 }
1176 return;
1177 }
1178
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001179 format = ctxt->format;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001180 if (format == 1) {
1181 tmp = cur->children;
1182 while (tmp != NULL) {
1183 if ((tmp->type == XML_TEXT_NODE) ||
1184 (tmp->type == XML_ENTITY_REF_NODE)) {
1185 format = 0;
1186 break;
1187 }
1188 tmp = tmp->next;
1189 }
1190 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001191 xmlOutputBufferWrite(buf, 1, "<");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001192 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1193 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001194 xmlOutputBufferWrite(buf, 1, ":");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001195 }
1196
1197 xmlOutputBufferWriteString(buf, (const char *)cur->name);
1198 if (cur->nsDef)
1199 xmlNsListDumpOutput(buf, cur->nsDef);
1200 if ((xmlStrEqual(cur->name, BAD_CAST "html") &&
1201 (cur->ns == NULL) && (cur->nsDef == NULL))) {
1202 /*
1203 * 3.1.1. Strictly Conforming Documents A.3.1.1 3/
1204 */
1205 xmlOutputBufferWriteString(buf,
1206 " xmlns=\"http://www.w3.org/1999/xhtml\"");
1207 }
1208 if (cur->properties != NULL)
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001209 xhtmlAttrListDumpOutput(ctxt, cur->properties);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001210
1211 if ((cur->type == XML_ELEMENT_NODE) && (cur->children == NULL)) {
1212 if (((cur->ns == NULL) || (cur->ns->prefix == NULL)) &&
1213 (xhtmlIsEmpty(cur) == 1)) {
1214 /*
1215 * C.2. Empty Elements
1216 */
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001217 xmlOutputBufferWrite(buf, 3, " />");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001218 } else {
1219 /*
1220 * C.3. Element Minimization and Empty Element Content
1221 */
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001222 xmlOutputBufferWrite(buf, 3, "></");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001223 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1224 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001225 xmlOutputBufferWrite(buf, 1, ":");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001226 }
1227 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001228 xmlOutputBufferWrite(buf, 1, ">");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001229 }
1230 return;
1231 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001232 xmlOutputBufferWrite(buf, 1, ">");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001233 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Daniel Veillard3995bc32004-05-15 18:57:31 +00001234 xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001235 }
1236
1237 /*
1238 * 4.8. Script and Style elements
1239 */
1240 if ((cur->type == XML_ELEMENT_NODE) &&
1241 ((xmlStrEqual(cur->name, BAD_CAST "script")) ||
1242 (xmlStrEqual(cur->name, BAD_CAST "style"))) &&
1243 ((cur->ns == NULL) ||
1244 (xmlStrEqual(cur->ns->href, XHTML_NS_NAME)))) {
1245 xmlNodePtr child = cur->children;
1246
1247 while (child != NULL) {
1248 if ((child->type == XML_TEXT_NODE) ||
1249 (child->type == XML_CDATA_SECTION_NODE)) {
1250 /*
1251 * Apparently CDATA escaping for style just break on IE,
1252 * mozilla and galeon, so ...
1253 */
1254 if (xmlStrEqual(cur->name, BAD_CAST "style") &&
1255 (xmlStrchr(child->content, '<') == NULL) &&
1256 (xmlStrchr(child->content, '>') == NULL) &&
1257 (xmlStrchr(child->content, '&') == NULL)) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001258 int level = ctxt->level;
1259 int indent = ctxt->format;
1260
1261 ctxt->level = 0;
1262 ctxt->format = 0;
1263 xhtmlNodeDumpOutput(ctxt, child);
1264 ctxt->level = level;
1265 ctxt->format = indent;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001266 } else {
1267 start = end = child->content;
1268 while (*end != '\0') {
1269 if (*end == ']' &&
1270 *(end + 1) == ']' &&
1271 *(end + 2) == '>') {
1272 end = end + 2;
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001273 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001274 xmlOutputBufferWrite(buf, end - start,
1275 (const char *)start);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001276 xmlOutputBufferWrite(buf, 3, "]]>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001277 start = end;
1278 }
1279 end++;
1280 }
1281 if (start != end) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001282 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
1283 xmlOutputBufferWrite(buf, end - start,
1284 (const char *)start);
1285 xmlOutputBufferWrite(buf, 3, "]]>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001286 }
1287 }
1288 } else {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001289 int level = ctxt->level;
1290 int indent = ctxt->format;
1291
1292 ctxt->level = 0;
1293 ctxt->format = 0;
1294 xhtmlNodeDumpOutput(ctxt, child);
1295 ctxt->level = level;
1296 ctxt->format = indent;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001297 }
1298 child = child->next;
1299 }
1300 } else if (cur->children != NULL) {
Daniel Veillardf0244ce2004-05-09 23:48:39 +00001301 int indent = ctxt->format;
1302
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001303 if (format) xmlOutputBufferWrite(buf, 1, "\n");
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001304 if (ctxt->level >= 0) ctxt->level++;
Daniel Veillardf0244ce2004-05-09 23:48:39 +00001305 ctxt->format = format;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001306 xhtmlNodeListDumpOutput(ctxt, cur->children);
1307 if (ctxt->level > 0) ctxt->level--;
Daniel Veillardf0244ce2004-05-09 23:48:39 +00001308 ctxt->format = indent;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001309 if ((xmlIndentTreeOutput) && (format))
Daniel Veillard753086a2004-03-28 16:12:44 +00001310 xmlOutputBufferWrite(buf, ctxt->indent_size *
1311 (ctxt->level > ctxt->indent_nr ?
1312 ctxt->indent_nr : ctxt->level),
1313 ctxt->indent);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001314 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001315 xmlOutputBufferWrite(buf, 2, "</");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001316 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1317 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001318 xmlOutputBufferWrite(buf, 1, ":");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001319 }
1320
1321 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001322 xmlOutputBufferWrite(buf, 1, ">");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001323}
1324#endif
1325
1326/************************************************************************
1327 * *
1328 * Public entry points *
1329 * *
1330 ************************************************************************/
1331
1332/**
1333 * xmlSaveToFd:
1334 * @fd: a file descriptor number
1335 * @encoding: the encoding name to use or NULL
1336 * @options: a set of xmlSaveOptions
1337 *
1338 * Create a document saving context serializing to a file descriptor
1339 * with the encoding and the options given.
1340 *
1341 * Returns a new serialization context or NULL in case of error.
1342 */
1343xmlSaveCtxtPtr
1344xmlSaveToFd(int fd, const char *encoding, int options)
1345{
1346 xmlSaveCtxtPtr ret;
1347
1348 ret = xmlNewSaveCtxt(encoding, options);
1349 if (ret == NULL) return(NULL);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001350 ret->buf = xmlOutputBufferCreateFd(fd, ret->handler);
1351 if (ret->buf == NULL) {
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001352 xmlFreeSaveCtxt(ret);
1353 return(NULL);
1354 }
1355 return(ret);
1356}
1357
1358/**
1359 * xmlSaveToFilename:
1360 * @filename: a file name or an URL
1361 * @encoding: the encoding name to use or NULL
1362 * @options: a set of xmlSaveOptions
1363 *
1364 * Create a document saving context serializing to a filename or possibly
1365 * to an URL (but this is less reliable) with the encoding and the options
1366 * given.
1367 *
1368 * Returns a new serialization context or NULL in case of error.
1369 */
1370xmlSaveCtxtPtr
1371xmlSaveToFilename(const char *filename, const char *encoding, int options)
1372{
1373 xmlSaveCtxtPtr ret;
1374 int compression = 0; /* TODO handle compression option */
1375
1376 ret = xmlNewSaveCtxt(encoding, options);
1377 if (ret == NULL) return(NULL);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001378 ret->buf = xmlOutputBufferCreateFilename(filename, ret->handler,
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001379 compression);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001380 if (ret->buf == NULL) {
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001381 xmlFreeSaveCtxt(ret);
1382 return(NULL);
1383 }
1384 return(ret);
1385}
1386
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001387/**
1388 * xmlSaveToBuffer:
1389 * @buffer: a buffer
1390 * @encoding: the encoding name to use or NULL
1391 * @options: a set of xmlSaveOptions
1392 *
1393 * Create a document saving context serializing to a buffer
1394 * with the encoding and the options given
1395 *
1396 * Returns a new serialization context or NULL in case of error.
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001397xmlSaveCtxtPtr
1398xmlSaveToBuffer(xmlBufferPtr buffer, const char *encoding, int options)
1399{
1400 TODO
1401 return(NULL);
1402}
Daniel Veillarda2351322004-06-27 12:08:10 +00001403 */
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001404
1405/**
1406 * xmlSaveToIO:
1407 * @iowrite: an I/O write function
1408 * @ioclose: an I/O close function
1409 * @ioctx: an I/O handler
1410 * @encoding: the encoding name to use or NULL
1411 * @options: a set of xmlSaveOptions
1412 *
1413 * Create a document saving context serializing to a file descriptor
1414 * with the encoding and the options given
1415 *
1416 * Returns a new serialization context or NULL in case of error.
1417 */
1418xmlSaveCtxtPtr
1419xmlSaveToIO(xmlOutputWriteCallback iowrite,
1420 xmlOutputCloseCallback ioclose,
1421 void *ioctx, const char *encoding, int options)
1422{
1423 xmlSaveCtxtPtr ret;
1424
1425 ret = xmlNewSaveCtxt(encoding, options);
1426 if (ret == NULL) return(NULL);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001427 ret->buf = xmlOutputBufferCreateIO(iowrite, ioclose, ioctx, ret->handler);
1428 if (ret->buf == NULL) {
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001429 xmlFreeSaveCtxt(ret);
1430 return(NULL);
1431 }
1432 return(ret);
1433}
1434
1435/**
1436 * xmlSaveDoc:
1437 * @ctxt: a document saving context
1438 * @doc: a document
1439 *
1440 * Save a full document to a saving context
Daniel Veillard377e1a92004-04-16 16:30:05 +00001441 * TODO: The function is not fully implemented yet as it does not return the
1442 * byte count but 0 instead
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001443 *
1444 * Returns the number of byte written or -1 in case of error
1445 */
1446long
1447xmlSaveDoc(xmlSaveCtxtPtr ctxt, xmlDocPtr doc)
1448{
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001449 long ret = 0;
1450
Daniel Veillardce682bc2004-11-05 17:22:25 +00001451 if ((ctxt == NULL) || (doc == NULL)) return(-1);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001452 xmlDocContentDumpOutput(ctxt, doc);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001453 return(ret);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001454}
1455
1456/**
1457 * xmlSaveTree:
1458 * @ctxt: a document saving context
1459 * @node: a document
1460 *
1461 * Save a subtree starting at the node parameter to a saving context
Daniel Veillard377e1a92004-04-16 16:30:05 +00001462 * TODO: The function is not fully implemented yet as it does not return the
1463 * byte count but 0 instead
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001464 *
1465 * Returns the number of byte written or -1 in case of error
1466 */
1467long
1468xmlSaveTree(xmlSaveCtxtPtr ctxt, xmlNodePtr node)
1469{
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001470 long ret = 0;
1471
Daniel Veillardce682bc2004-11-05 17:22:25 +00001472 if ((ctxt == NULL) || (node == NULL)) return(-1);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001473 xmlNodeDumpOutputInternal(ctxt, node);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001474 return(ret);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001475}
1476
1477/**
1478 * xmlSaveFlush:
1479 * @ctxt: a document saving context
1480 *
1481 * Flush a document saving context, i.e. make sure that all bytes have
1482 * been output.
1483 *
1484 * Returns the number of byte written or -1 in case of error.
1485 */
1486int
1487xmlSaveFlush(xmlSaveCtxtPtr ctxt)
1488{
1489 if (ctxt == NULL) return(-1);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001490 if (ctxt->buf == NULL) return(-1);
1491 return(xmlOutputBufferFlush(ctxt->buf));
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001492}
1493
1494/**
1495 * xmlSaveClose:
1496 * @ctxt: a document saving context
1497 *
1498 * Close a document saving context, i.e. make sure that all bytes have
1499 * been output and free the associated data.
1500 *
1501 * Returns the number of byte written or -1 in case of error.
1502 */
1503int
1504xmlSaveClose(xmlSaveCtxtPtr ctxt)
1505{
1506 int ret;
1507
1508 if (ctxt == NULL) return(-1);
1509 ret = xmlSaveFlush(ctxt);
1510 xmlFreeSaveCtxt(ctxt);
1511 return(ret);
1512}
1513
Daniel Veillard3995bc32004-05-15 18:57:31 +00001514/**
1515 * xmlSaveSetEscape:
1516 * @ctxt: a document saving context
1517 * @escape: the escaping function
1518 *
1519 * Set a custom escaping function to be used for text in element content
1520 *
1521 * Returns 0 if successful or -1 in case of error.
1522 */
1523int
1524xmlSaveSetEscape(xmlSaveCtxtPtr ctxt, xmlCharEncodingOutputFunc escape)
1525{
1526 if (ctxt == NULL) return(-1);
1527 ctxt->escape = escape;
1528 return(0);
1529}
1530
1531/**
1532 * xmlSaveSetAttrEscape:
1533 * @ctxt: a document saving context
1534 * @escape: the escaping function
1535 *
1536 * Set a custom escaping function to be used for text in attribute content
1537 *
1538 * Returns 0 if successful or -1 in case of error.
1539 */
1540int
1541xmlSaveSetAttrEscape(xmlSaveCtxtPtr ctxt, xmlCharEncodingOutputFunc escape)
1542{
1543 if (ctxt == NULL) return(-1);
1544 ctxt->escapeAttr = escape;
1545 return(0);
1546}
1547
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001548/************************************************************************
1549 * *
1550 * Public entry points based on buffers *
1551 * *
1552 ************************************************************************/
1553/**
1554 * xmlAttrSerializeTxtContent:
1555 * @buf: the XML buffer output
1556 * @doc: the document
1557 * @attr: the attribute node
1558 * @string: the text content
1559 *
1560 * Serialize text attribute values to an xml simple buffer
1561 */
1562void
1563xmlAttrSerializeTxtContent(xmlBufferPtr buf, xmlDocPtr doc,
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001564 xmlAttrPtr attr, const xmlChar * string)
1565{
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001566 xmlChar *base, *cur;
1567
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001568 if (string == NULL)
1569 return;
1570 base = cur = (xmlChar *) string;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001571 while (*cur != 0) {
1572 if (*cur == '\n') {
1573 if (base != cur)
1574 xmlBufferAdd(buf, base, cur - base);
1575 xmlBufferAdd(buf, BAD_CAST "&#10;", 5);
1576 cur++;
1577 base = cur;
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001578 } else if (*cur == '\r') {
1579 if (base != cur)
1580 xmlBufferAdd(buf, base, cur - base);
1581 xmlBufferAdd(buf, BAD_CAST "&#13;", 5);
1582 cur++;
1583 base = cur;
1584 } else if (*cur == '\t') {
1585 if (base != cur)
1586 xmlBufferAdd(buf, base, cur - base);
1587 xmlBufferAdd(buf, BAD_CAST "&#9;", 4);
1588 cur++;
1589 base = cur;
1590 } else if (*cur == '"') {
1591 if (base != cur)
1592 xmlBufferAdd(buf, base, cur - base);
1593 xmlBufferAdd(buf, BAD_CAST "&quot;", 6);
1594 cur++;
1595 base = cur;
1596 } else if (*cur == '<') {
1597 if (base != cur)
1598 xmlBufferAdd(buf, base, cur - base);
1599 xmlBufferAdd(buf, BAD_CAST "&lt;", 4);
1600 cur++;
1601 base = cur;
1602 } else if (*cur == '>') {
1603 if (base != cur)
1604 xmlBufferAdd(buf, base, cur - base);
1605 xmlBufferAdd(buf, BAD_CAST "&gt;", 4);
1606 cur++;
1607 base = cur;
1608 } else if (*cur == '&') {
1609 if (base != cur)
1610 xmlBufferAdd(buf, base, cur - base);
1611 xmlBufferAdd(buf, BAD_CAST "&amp;", 5);
1612 cur++;
1613 base = cur;
1614 } else if ((*cur >= 0x80) && ((doc == NULL) ||
1615 (doc->encoding == NULL))) {
1616 /*
1617 * We assume we have UTF-8 content.
1618 */
1619 unsigned char tmp[10];
1620 int val = 0, l = 1;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001621
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001622 if (base != cur)
1623 xmlBufferAdd(buf, base, cur - base);
1624 if (*cur < 0xC0) {
1625 xmlSaveErr(XML_SAVE_NOT_UTF8, (xmlNodePtr) attr, NULL);
1626 if (doc != NULL)
1627 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
1628 xmlSerializeHexCharRef(tmp, *cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001629 xmlBufferAdd(buf, (xmlChar *) tmp, -1);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001630 cur++;
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001631 base = cur;
1632 continue;
1633 } else if (*cur < 0xE0) {
1634 val = (cur[0]) & 0x1F;
1635 val <<= 6;
1636 val |= (cur[1]) & 0x3F;
1637 l = 2;
1638 } else if (*cur < 0xF0) {
1639 val = (cur[0]) & 0x0F;
1640 val <<= 6;
1641 val |= (cur[1]) & 0x3F;
1642 val <<= 6;
1643 val |= (cur[2]) & 0x3F;
1644 l = 3;
1645 } else if (*cur < 0xF8) {
1646 val = (cur[0]) & 0x07;
1647 val <<= 6;
1648 val |= (cur[1]) & 0x3F;
1649 val <<= 6;
1650 val |= (cur[2]) & 0x3F;
1651 val <<= 6;
1652 val |= (cur[3]) & 0x3F;
1653 l = 4;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001654 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001655 if ((l == 1) || (!IS_CHAR(val))) {
1656 xmlSaveErr(XML_SAVE_CHAR_INVALID, (xmlNodePtr) attr, NULL);
1657 if (doc != NULL)
1658 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
1659
1660 xmlSerializeHexCharRef(tmp, *cur);
1661 xmlBufferAdd(buf, (xmlChar *) tmp, -1);
1662 cur++;
1663 base = cur;
1664 continue;
1665 }
1666 /*
1667 * We could do multiple things here. Just save
1668 * as a char ref
1669 */
1670 xmlSerializeHexCharRef(tmp, val);
1671 xmlBufferAdd(buf, (xmlChar *) tmp, -1);
1672 cur += l;
1673 base = cur;
1674 } else {
1675 cur++;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001676 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001677 }
1678 if (base != cur)
1679 xmlBufferAdd(buf, base, cur - base);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001680}
1681
1682/**
1683 * xmlNodeDump:
1684 * @buf: the XML buffer output
1685 * @doc: the document
1686 * @cur: the current node
1687 * @level: the imbrication level for indenting
1688 * @format: is formatting allowed
1689 *
1690 * Dump an XML node, recursive behaviour,children are printed too.
1691 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
1692 * or xmlKeepBlanksDefault(0) was called
1693 *
1694 * Returns the number of bytes written to the buffer or -1 in case of error
1695 */
1696int
1697xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
1698 int format)
1699{
1700 unsigned int use;
1701 int ret;
1702 xmlOutputBufferPtr outbuf;
1703
1704 xmlInitParser();
1705
1706 if (cur == NULL) {
1707#ifdef DEBUG_TREE
1708 xmlGenericError(xmlGenericErrorContext,
1709 "xmlNodeDump : node == NULL\n");
1710#endif
1711 return (-1);
1712 }
1713 if (buf == NULL) {
1714#ifdef DEBUG_TREE
1715 xmlGenericError(xmlGenericErrorContext,
1716 "xmlNodeDump : buf == NULL\n");
1717#endif
1718 return (-1);
1719 }
1720 outbuf = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer));
1721 if (outbuf == NULL) {
1722 xmlSaveErrMemory("creating buffer");
1723 return (-1);
1724 }
1725 memset(outbuf, 0, (size_t) sizeof(xmlOutputBuffer));
1726 outbuf->buffer = buf;
1727 outbuf->encoder = NULL;
1728 outbuf->writecallback = NULL;
1729 outbuf->closecallback = NULL;
1730 outbuf->context = NULL;
1731 outbuf->written = 0;
1732
1733 use = buf->use;
1734 xmlNodeDumpOutput(outbuf, doc, cur, level, format, NULL);
1735 xmlFree(outbuf);
1736 ret = buf->use - use;
1737 return (ret);
1738}
1739
1740/**
1741 * xmlElemDump:
1742 * @f: the FILE * for the output
1743 * @doc: the document
1744 * @cur: the current node
1745 *
1746 * Dump an XML/HTML node, recursive behaviour, children are printed too.
1747 */
1748void
1749xmlElemDump(FILE * f, xmlDocPtr doc, xmlNodePtr cur)
1750{
1751 xmlOutputBufferPtr outbuf;
1752
1753 xmlInitParser();
1754
1755 if (cur == NULL) {
1756#ifdef DEBUG_TREE
1757 xmlGenericError(xmlGenericErrorContext,
1758 "xmlElemDump : cur == NULL\n");
1759#endif
1760 return;
1761 }
1762#ifdef DEBUG_TREE
1763 if (doc == NULL) {
1764 xmlGenericError(xmlGenericErrorContext,
1765 "xmlElemDump : doc == NULL\n");
1766 }
1767#endif
1768
1769 outbuf = xmlOutputBufferCreateFile(f, NULL);
1770 if (outbuf == NULL)
1771 return;
1772 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
1773#ifdef LIBXML_HTML_ENABLED
1774 htmlNodeDumpOutput(outbuf, doc, cur, NULL);
1775#else
1776 xmlSaveErr(XML_ERR_INTERNAL_ERROR, cur, "HTML support not compiled in\n");
1777#endif /* LIBXML_HTML_ENABLED */
1778 } else
1779 xmlNodeDumpOutput(outbuf, doc, cur, 0, 1, NULL);
1780 xmlOutputBufferClose(outbuf);
1781}
1782
1783/************************************************************************
1784 * *
1785 * Saving functions front-ends *
1786 * *
1787 ************************************************************************/
1788
1789/**
1790 * xmlNodeDumpOutput:
1791 * @buf: the XML buffer output
1792 * @doc: the document
1793 * @cur: the current node
1794 * @level: the imbrication level for indenting
1795 * @format: is formatting allowed
1796 * @encoding: an optional encoding string
1797 *
1798 * Dump an XML node, recursive behaviour, children are printed too.
1799 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
1800 * or xmlKeepBlanksDefault(0) was called
1801 */
1802void
1803xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
1804 int level, int format, const char *encoding)
1805{
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001806 xmlSaveCtxt ctxt;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001807#ifdef LIBXML_HTML_ENABLED
1808 xmlDtdPtr dtd;
1809 int is_xhtml = 0;
1810#endif
1811
1812 xmlInitParser();
1813
Daniel Veillardce244ad2004-11-05 10:03:46 +00001814 if ((buf == NULL) || (cur == NULL)) return;
1815
Daniel Veillard64354ea2005-03-31 15:22:56 +00001816 if (encoding == NULL)
1817 encoding = "UTF-8";
1818
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001819 memset(&ctxt, 0, sizeof(ctxt));
1820 ctxt.doc = doc;
1821 ctxt.buf = buf;
1822 ctxt.level = level;
1823 ctxt.format = format;
1824 ctxt.encoding = (const xmlChar *) encoding;
Daniel Veillard753086a2004-03-28 16:12:44 +00001825 xmlSaveCtxtInit(&ctxt);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001826
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001827#ifdef LIBXML_HTML_ENABLED
1828 dtd = xmlGetIntSubset(doc);
1829 if (dtd != NULL) {
1830 is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
1831 if (is_xhtml < 0)
1832 is_xhtml = 0;
1833 if ((is_xhtml) && (cur->parent == (xmlNodePtr) doc) &&
1834 (cur->type == XML_ELEMENT_NODE) &&
1835 (xmlStrEqual(cur->name, BAD_CAST "html"))) {
1836 if (encoding != NULL)
1837 htmlSetMetaEncoding((htmlDocPtr) doc,
1838 (const xmlChar *) encoding);
1839 else
1840 htmlSetMetaEncoding((htmlDocPtr) doc, BAD_CAST "UTF-8");
1841 }
1842 }
1843
1844 if (is_xhtml)
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001845 xhtmlNodeDumpOutput(&ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001846 else
1847#endif
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001848 xmlNodeDumpOutputInternal(&ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001849}
1850
1851/**
1852 * xmlDocDumpFormatMemoryEnc:
1853 * @out_doc: Document to generate XML text from
1854 * @doc_txt_ptr: Memory pointer for allocated XML text
1855 * @doc_txt_len: Length of the generated XML text
1856 * @txt_encoding: Character encoding to use when generating XML text
1857 * @format: should formatting spaces been added
1858 *
1859 * Dump the current DOM tree into memory using the character encoding specified
1860 * by the caller. Note it is up to the caller of this function to free the
1861 * allocated memory with xmlFree().
1862 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
1863 * or xmlKeepBlanksDefault(0) was called
1864 */
1865
1866void
1867xmlDocDumpFormatMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
1868 int * doc_txt_len, const char * txt_encoding,
1869 int format) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001870 xmlSaveCtxt ctxt;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001871 int dummy = 0;
1872 xmlOutputBufferPtr out_buff = NULL;
1873 xmlCharEncodingHandlerPtr conv_hdlr = NULL;
1874
1875 if (doc_txt_len == NULL) {
1876 doc_txt_len = &dummy; /* Continue, caller just won't get length */
1877 }
1878
1879 if (doc_txt_ptr == NULL) {
1880 *doc_txt_len = 0;
1881 return;
1882 }
1883
1884 *doc_txt_ptr = NULL;
1885 *doc_txt_len = 0;
1886
1887 if (out_doc == NULL) {
1888 /* No document, no output */
1889 return;
1890 }
1891
1892 /*
1893 * Validate the encoding value, if provided.
1894 * This logic is copied from xmlSaveFileEnc.
1895 */
1896
1897 if (txt_encoding == NULL)
1898 txt_encoding = (const char *) out_doc->encoding;
1899 if (txt_encoding != NULL) {
1900 conv_hdlr = xmlFindCharEncodingHandler(txt_encoding);
1901 if ( conv_hdlr == NULL ) {
1902 xmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, (xmlNodePtr) out_doc,
1903 txt_encoding);
1904 return;
1905 }
1906 }
1907
1908 if ((out_buff = xmlAllocOutputBuffer(conv_hdlr)) == NULL ) {
1909 xmlSaveErrMemory("creating buffer");
1910 return;
1911 }
1912
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001913 memset(&ctxt, 0, sizeof(ctxt));
1914 ctxt.doc = out_doc;
1915 ctxt.buf = out_buff;
1916 ctxt.level = 0;
1917 ctxt.format = format;
1918 ctxt.encoding = (const xmlChar *) txt_encoding;
Daniel Veillard753086a2004-03-28 16:12:44 +00001919 xmlSaveCtxtInit(&ctxt);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001920 xmlDocContentDumpOutput(&ctxt, out_doc);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001921 xmlOutputBufferFlush(out_buff);
1922 if (out_buff->conv != NULL) {
1923 *doc_txt_len = out_buff->conv->use;
1924 *doc_txt_ptr = xmlStrndup(out_buff->conv->content, *doc_txt_len);
1925 } else {
1926 *doc_txt_len = out_buff->buffer->use;
1927 *doc_txt_ptr = xmlStrndup(out_buff->buffer->content, *doc_txt_len);
1928 }
1929 (void)xmlOutputBufferClose(out_buff);
1930
1931 if ((*doc_txt_ptr == NULL) && (*doc_txt_len > 0)) {
1932 *doc_txt_len = 0;
1933 xmlSaveErrMemory("creating output");
1934 }
1935
1936 return;
1937}
1938
1939/**
1940 * xmlDocDumpMemory:
1941 * @cur: the document
1942 * @mem: OUT: the memory pointer
1943 * @size: OUT: the memory length
1944 *
1945 * Dump an XML document in memory and return the #xmlChar * and it's size
1946 * in bytes. It's up to the caller to free the memory with xmlFree().
1947 * The resulting byte array is zero terminated, though the last 0 is not
1948 * included in the returned size.
1949 */
1950void
1951xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) {
1952 xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, 0);
1953}
1954
1955/**
1956 * xmlDocDumpFormatMemory:
1957 * @cur: the document
1958 * @mem: OUT: the memory pointer
1959 * @size: OUT: the memory length
1960 * @format: should formatting spaces been added
1961 *
1962 *
1963 * Dump an XML document in memory and return the #xmlChar * and it's size.
1964 * It's up to the caller to free the memory with xmlFree().
1965 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
1966 * or xmlKeepBlanksDefault(0) was called
1967 */
1968void
1969xmlDocDumpFormatMemory(xmlDocPtr cur, xmlChar**mem, int *size, int format) {
1970 xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, format);
1971}
1972
1973/**
1974 * xmlDocDumpMemoryEnc:
1975 * @out_doc: Document to generate XML text from
1976 * @doc_txt_ptr: Memory pointer for allocated XML text
1977 * @doc_txt_len: Length of the generated XML text
1978 * @txt_encoding: Character encoding to use when generating XML text
1979 *
1980 * Dump the current DOM tree into memory using the character encoding specified
1981 * by the caller. Note it is up to the caller of this function to free the
1982 * allocated memory with xmlFree().
1983 */
1984
1985void
1986xmlDocDumpMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
1987 int * doc_txt_len, const char * txt_encoding) {
1988 xmlDocDumpFormatMemoryEnc(out_doc, doc_txt_ptr, doc_txt_len,
1989 txt_encoding, 0);
1990}
1991
1992/**
1993 * xmlDocFormatDump:
1994 * @f: the FILE*
1995 * @cur: the document
1996 * @format: should formatting spaces been added
1997 *
1998 * Dump an XML document to an open FILE.
1999 *
2000 * returns: the number of bytes written or -1 in case of failure.
2001 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
2002 * or xmlKeepBlanksDefault(0) was called
2003 */
2004int
2005xmlDocFormatDump(FILE *f, xmlDocPtr cur, int format) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002006 xmlSaveCtxt ctxt;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002007 xmlOutputBufferPtr buf;
2008 const char * encoding;
2009 xmlCharEncodingHandlerPtr handler = NULL;
2010 int ret;
2011
2012 if (cur == NULL) {
2013#ifdef DEBUG_TREE
2014 xmlGenericError(xmlGenericErrorContext,
2015 "xmlDocDump : document == NULL\n");
2016#endif
2017 return(-1);
2018 }
2019 encoding = (const char *) cur->encoding;
2020
2021 if (encoding != NULL) {
2022 handler = xmlFindCharEncodingHandler(encoding);
2023 if (handler == NULL) {
2024 xmlFree((char *) cur->encoding);
2025 cur->encoding = NULL;
2026 }
2027 }
2028 buf = xmlOutputBufferCreateFile(f, handler);
2029 if (buf == NULL) return(-1);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002030 memset(&ctxt, 0, sizeof(ctxt));
2031 ctxt.doc = cur;
2032 ctxt.buf = buf;
2033 ctxt.level = 0;
2034 ctxt.format = format;
2035 ctxt.encoding = (const xmlChar *) encoding;
Daniel Veillard753086a2004-03-28 16:12:44 +00002036 xmlSaveCtxtInit(&ctxt);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002037 xmlDocContentDumpOutput(&ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002038
2039 ret = xmlOutputBufferClose(buf);
2040 return(ret);
2041}
2042
2043/**
2044 * xmlDocDump:
2045 * @f: the FILE*
2046 * @cur: the document
2047 *
2048 * Dump an XML document to an open FILE.
2049 *
2050 * returns: the number of bytes written or -1 in case of failure.
2051 */
2052int
2053xmlDocDump(FILE *f, xmlDocPtr cur) {
2054 return(xmlDocFormatDump (f, cur, 0));
2055}
2056
2057/**
2058 * xmlSaveFileTo:
2059 * @buf: an output I/O buffer
2060 * @cur: the document
2061 * @encoding: the encoding if any assuming the I/O layer handles the trancoding
2062 *
2063 * Dump an XML document to an I/O buffer.
Daniel Veillard3d97e662004-11-04 10:49:00 +00002064 * Warning ! This call xmlOutputBufferClose() on buf which is not available
2065 * after this call.
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002066 *
2067 * returns: the number of bytes written or -1 in case of failure.
2068 */
2069int
2070xmlSaveFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002071 xmlSaveCtxt ctxt;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002072 int ret;
2073
Daniel Veillard3d97e662004-11-04 10:49:00 +00002074 if (buf == NULL) return(-1);
2075 if (cur == NULL) {
2076 xmlOutputBufferClose(buf);
2077 return(-1);
2078 }
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002079 memset(&ctxt, 0, sizeof(ctxt));
2080 ctxt.doc = cur;
2081 ctxt.buf = buf;
2082 ctxt.level = 0;
2083 ctxt.format = 0;
2084 ctxt.encoding = (const xmlChar *) encoding;
Daniel Veillard753086a2004-03-28 16:12:44 +00002085 xmlSaveCtxtInit(&ctxt);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002086 xmlDocContentDumpOutput(&ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002087 ret = xmlOutputBufferClose(buf);
2088 return(ret);
2089}
2090
2091/**
2092 * xmlSaveFormatFileTo:
2093 * @buf: an output I/O buffer
2094 * @cur: the document
2095 * @encoding: the encoding if any assuming the I/O layer handles the trancoding
2096 * @format: should formatting spaces been added
2097 *
2098 * Dump an XML document to an I/O buffer.
Daniel Veillard3d97e662004-11-04 10:49:00 +00002099 * Warning ! This call xmlOutputBufferClose() on buf which is not available
2100 * after this call.
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002101 *
2102 * returns: the number of bytes written or -1 in case of failure.
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002103 */
2104int
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002105xmlSaveFormatFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur,
2106 const char *encoding, int format)
2107{
2108 xmlSaveCtxt ctxt;
2109 int ret;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002110
Daniel Veillard3d97e662004-11-04 10:49:00 +00002111 if (buf == NULL) return(-1);
Daniel Veillardce244ad2004-11-05 10:03:46 +00002112 if ((cur == NULL) ||
2113 ((cur->type != XML_DOCUMENT_NODE) &&
2114 (cur->type != XML_HTML_DOCUMENT_NODE))) {
Daniel Veillard3d97e662004-11-04 10:49:00 +00002115 xmlOutputBufferClose(buf);
2116 return(-1);
2117 }
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002118 memset(&ctxt, 0, sizeof(ctxt));
2119 ctxt.doc = cur;
2120 ctxt.buf = buf;
2121 ctxt.level = 0;
2122 ctxt.format = format;
2123 ctxt.encoding = (const xmlChar *) encoding;
Daniel Veillard753086a2004-03-28 16:12:44 +00002124 xmlSaveCtxtInit(&ctxt);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002125 xmlDocContentDumpOutput(&ctxt, cur);
2126 ret = xmlOutputBufferClose(buf);
2127 return (ret);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002128}
2129
2130/**
2131 * xmlSaveFormatFileEnc:
2132 * @filename: the filename or URL to output
2133 * @cur: the document being saved
2134 * @encoding: the name of the encoding to use or NULL.
2135 * @format: should formatting spaces be added.
2136 *
2137 * Dump an XML document to a file or an URL.
2138 *
2139 * Returns the number of bytes written or -1 in case of error.
2140 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
2141 * or xmlKeepBlanksDefault(0) was called
2142 */
2143int
2144xmlSaveFormatFileEnc( const char * filename, xmlDocPtr cur,
2145 const char * encoding, int format ) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002146 xmlSaveCtxt ctxt;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002147 xmlOutputBufferPtr buf;
2148 xmlCharEncodingHandlerPtr handler = NULL;
2149 int ret;
2150
2151 if (cur == NULL)
2152 return(-1);
2153
2154 if (encoding == NULL)
2155 encoding = (const char *) cur->encoding;
2156
2157 if (encoding != NULL) {
2158
2159 handler = xmlFindCharEncodingHandler(encoding);
2160 if (handler == NULL)
2161 return(-1);
2162 }
2163
2164#ifdef HAVE_ZLIB_H
2165 if (cur->compression < 0) cur->compression = xmlGetCompressMode();
2166#endif
2167 /*
2168 * save the content to a temp buffer.
2169 */
2170 buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression);
2171 if (buf == NULL) return(-1);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002172 memset(&ctxt, 0, sizeof(ctxt));
2173 ctxt.doc = cur;
2174 ctxt.buf = buf;
2175 ctxt.level = 0;
2176 ctxt.format = format;
2177 ctxt.encoding = (const xmlChar *) encoding;
Daniel Veillard753086a2004-03-28 16:12:44 +00002178 xmlSaveCtxtInit(&ctxt);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002179
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002180 xmlDocContentDumpOutput(&ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002181
2182 ret = xmlOutputBufferClose(buf);
2183 return(ret);
2184}
2185
2186
2187/**
2188 * xmlSaveFileEnc:
2189 * @filename: the filename (or URL)
2190 * @cur: the document
2191 * @encoding: the name of an encoding (or NULL)
2192 *
2193 * Dump an XML document, converting it to the given encoding
2194 *
2195 * returns: the number of bytes written or -1 in case of failure.
2196 */
2197int
2198xmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) {
2199 return ( xmlSaveFormatFileEnc( filename, cur, encoding, 0 ) );
2200}
2201
2202/**
2203 * xmlSaveFormatFile:
2204 * @filename: the filename (or URL)
2205 * @cur: the document
2206 * @format: should formatting spaces been added
2207 *
2208 * Dump an XML document to a file. Will use compression if
2209 * compiled in and enabled. If @filename is "-" the stdout file is
2210 * used. If @format is set then the document will be indented on output.
2211 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
2212 * or xmlKeepBlanksDefault(0) was called
2213 *
2214 * returns: the number of bytes written or -1 in case of failure.
2215 */
2216int
2217xmlSaveFormatFile(const char *filename, xmlDocPtr cur, int format) {
2218 return ( xmlSaveFormatFileEnc( filename, cur, NULL, format ) );
2219}
2220
2221/**
2222 * xmlSaveFile:
2223 * @filename: the filename (or URL)
2224 * @cur: the document
2225 *
2226 * Dump an XML document to a file. Will use compression if
2227 * compiled in and enabled. If @filename is "-" the stdout file is
2228 * used.
2229 * returns: the number of bytes written or -1 in case of failure.
2230 */
2231int
2232xmlSaveFile(const char *filename, xmlDocPtr cur) {
2233 return(xmlSaveFormatFileEnc(filename, cur, NULL, 0));
2234}
2235
2236#endif /* LIBXML_OUTPUT_ENABLED */
2237
Daniel Veillard5d4644e2005-04-01 13:11:58 +00002238#define bottom_xmlsave
2239#include "elfgcchack.h"