blob: 231ee7b9e617913ae3382216688c0b38dcfab6d7 [file] [log] [blame]
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001/*
2 * xmlsave.c: Implemetation of the document serializer
3 *
4 * See Copyright for the status of this software.
5 *
6 * daniel@veillard.com
7 */
8
9#define IN_LIBXML
10#include "libxml.h"
11
12#include <string.h>
13#include <libxml/xmlmemory.h>
14#include <libxml/parserInternals.h>
15#include <libxml/tree.h>
16#include <libxml/xmlsave.h>
Daniel Veillard1a8741c2004-03-04 13:40:59 +000017
Daniel Veillard753086a2004-03-28 16:12:44 +000018#define MAX_INDENT 60
19
Daniel Veillard656ce942004-04-30 23:11:45 +000020#include <libxml/HTMLtree.h>
21
Daniel Veillard1a8741c2004-03-04 13:40:59 +000022/************************************************************************
23 * *
24 * XHTML detection *
25 * *
26 ************************************************************************/
27#define XHTML_STRICT_PUBLIC_ID BAD_CAST \
28 "-//W3C//DTD XHTML 1.0 Strict//EN"
29#define XHTML_STRICT_SYSTEM_ID BAD_CAST \
30 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
31#define XHTML_FRAME_PUBLIC_ID BAD_CAST \
32 "-//W3C//DTD XHTML 1.0 Frameset//EN"
33#define XHTML_FRAME_SYSTEM_ID BAD_CAST \
34 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"
35#define XHTML_TRANS_PUBLIC_ID BAD_CAST \
36 "-//W3C//DTD XHTML 1.0 Transitional//EN"
37#define XHTML_TRANS_SYSTEM_ID BAD_CAST \
38 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
39
40#define XHTML_NS_NAME BAD_CAST "http://www.w3.org/1999/xhtml"
41/**
42 * xmlIsXHTML:
43 * @systemID: the system identifier
44 * @publicID: the public identifier
45 *
46 * Try to find if the document correspond to an XHTML DTD
47 *
48 * Returns 1 if true, 0 if not and -1 in case of error
49 */
50int
51xmlIsXHTML(const xmlChar *systemID, const xmlChar *publicID) {
52 if ((systemID == NULL) && (publicID == NULL))
53 return(-1);
54 if (publicID != NULL) {
55 if (xmlStrEqual(publicID, XHTML_STRICT_PUBLIC_ID)) return(1);
56 if (xmlStrEqual(publicID, XHTML_FRAME_PUBLIC_ID)) return(1);
57 if (xmlStrEqual(publicID, XHTML_TRANS_PUBLIC_ID)) return(1);
58 }
59 if (systemID != NULL) {
60 if (xmlStrEqual(systemID, XHTML_STRICT_SYSTEM_ID)) return(1);
61 if (xmlStrEqual(systemID, XHTML_FRAME_SYSTEM_ID)) return(1);
62 if (xmlStrEqual(systemID, XHTML_TRANS_SYSTEM_ID)) return(1);
63 }
64 return(0);
65}
Daniel Veillard1a8741c2004-03-04 13:40:59 +000066
67#ifdef LIBXML_OUTPUT_ENABLED
68
69#define TODO \
70 xmlGenericError(xmlGenericErrorContext, \
71 "Unimplemented block at %s:%d\n", \
72 __FILE__, __LINE__);
73
74struct _xmlSaveCtxt {
75 void *_private;
76 int type;
77 int fd;
78 const xmlChar *filename;
79 const xmlChar *encoding;
80 xmlCharEncodingHandlerPtr handler;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +000081 xmlOutputBufferPtr buf;
82 xmlDocPtr doc;
Daniel Veillard1a8741c2004-03-04 13:40:59 +000083 int options;
84 int level;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +000085 int format;
Daniel Veillard3995bc32004-05-15 18:57:31 +000086 char indent[MAX_INDENT + 1]; /* array for indenting output */
Daniel Veillard753086a2004-03-28 16:12:44 +000087 int indent_nr;
88 int indent_size;
Daniel Veillard3995bc32004-05-15 18:57:31 +000089 xmlCharEncodingOutputFunc escape; /* used for element content */
90 xmlCharEncodingOutputFunc escapeAttr;/* used for attribute content */
Daniel Veillard1a8741c2004-03-04 13:40:59 +000091};
92
93/************************************************************************
94 * *
95 * Output error handlers *
96 * *
97 ************************************************************************/
98/**
99 * xmlSaveErrMemory:
100 * @extra: extra informations
101 *
102 * Handle an out of memory condition
103 */
104static void
105xmlSaveErrMemory(const char *extra)
106{
107 __xmlSimpleError(XML_FROM_OUTPUT, XML_ERR_NO_MEMORY, NULL, NULL, extra);
108}
109
110/**
111 * xmlSaveErr:
112 * @code: the error number
113 * @node: the location of the error.
114 * @extra: extra informations
115 *
116 * Handle an out of memory condition
117 */
118static void
119xmlSaveErr(int code, xmlNodePtr node, const char *extra)
120{
121 const char *msg = NULL;
122
123 switch(code) {
124 case XML_SAVE_NOT_UTF8:
Rob Richards417b74d2006-08-15 23:14:24 +0000125 msg = "string is not in UTF-8\n";
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000126 break;
127 case XML_SAVE_CHAR_INVALID:
Rob Richards417b74d2006-08-15 23:14:24 +0000128 msg = "invalid character value\n";
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000129 break;
130 case XML_SAVE_UNKNOWN_ENCODING:
Rob Richards417b74d2006-08-15 23:14:24 +0000131 msg = "unknown encoding %s\n";
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000132 break;
133 case XML_SAVE_NO_DOCTYPE:
Rob Richards417b74d2006-08-15 23:14:24 +0000134 msg = "document has no DOCTYPE\n";
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000135 break;
136 default:
Rob Richards417b74d2006-08-15 23:14:24 +0000137 msg = "unexpected error number\n";
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000138 }
139 __xmlSimpleError(XML_FROM_OUTPUT, code, node, msg, extra);
140}
141
142/************************************************************************
143 * *
Daniel Veillard83a75e02004-05-14 21:50:42 +0000144 * Special escaping routines *
145 * *
146 ************************************************************************/
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000147static unsigned char *
148xmlSerializeHexCharRef(unsigned char *out, int val) {
149 unsigned char *ptr;
150
151 *out++ = '&';
152 *out++ = '#';
153 *out++ = 'x';
154 if (val < 0x10) ptr = out;
155 else if (val < 0x100) ptr = out + 1;
156 else if (val < 0x1000) ptr = out + 2;
157 else if (val < 0x10000) ptr = out + 3;
158 else if (val < 0x100000) ptr = out + 4;
159 else ptr = out + 5;
160 out = ptr + 1;
161 while (val > 0) {
162 switch (val & 0xF) {
163 case 0: *ptr-- = '0'; break;
164 case 1: *ptr-- = '1'; break;
165 case 2: *ptr-- = '2'; break;
166 case 3: *ptr-- = '3'; break;
167 case 4: *ptr-- = '4'; break;
168 case 5: *ptr-- = '5'; break;
169 case 6: *ptr-- = '6'; break;
170 case 7: *ptr-- = '7'; break;
171 case 8: *ptr-- = '8'; break;
172 case 9: *ptr-- = '9'; break;
173 case 0xA: *ptr-- = 'A'; break;
174 case 0xB: *ptr-- = 'B'; break;
175 case 0xC: *ptr-- = 'C'; break;
176 case 0xD: *ptr-- = 'D'; break;
177 case 0xE: *ptr-- = 'E'; break;
178 case 0xF: *ptr-- = 'F'; break;
179 default: *ptr-- = '0'; break;
180 }
181 val >>= 4;
182 }
183 *out++ = ';';
184 *out = 0;
185 return(out);
186}
187
Daniel Veillard83a75e02004-05-14 21:50:42 +0000188/**
189 * xmlEscapeEntities:
190 * @out: a pointer to an array of bytes to store the result
191 * @outlen: the length of @out
192 * @in: a pointer to an array of unescaped UTF-8 bytes
193 * @inlen: the length of @in
194 *
195 * Take a block of UTF-8 chars in and escape them. Used when there is no
196 * encoding specified.
197 *
198 * Returns 0 if success, or -1 otherwise
199 * The value of @inlen after return is the number of octets consumed
200 * if the return value is positive, else unpredictable.
201 * The value of @outlen after return is the number of octets consumed.
202 */
203static int
204xmlEscapeEntities(unsigned char* out, int *outlen,
205 const xmlChar* in, int *inlen) {
206 unsigned char* outstart = out;
207 const unsigned char* base = in;
208 unsigned char* outend = out + *outlen;
209 const unsigned char* inend;
210 int val;
211
212 inend = in + (*inlen);
213
214 while ((in < inend) && (out < outend)) {
215 if (*in == '<') {
216 if (outend - out < 4) break;
217 *out++ = '&';
218 *out++ = 'l';
219 *out++ = 't';
220 *out++ = ';';
221 in++;
222 continue;
223 } else if (*in == '>') {
224 if (outend - out < 4) break;
225 *out++ = '&';
226 *out++ = 'g';
227 *out++ = 't';
228 *out++ = ';';
229 in++;
230 continue;
231 } else if (*in == '&') {
232 if (outend - out < 5) break;
233 *out++ = '&';
234 *out++ = 'a';
235 *out++ = 'm';
236 *out++ = 'p';
237 *out++ = ';';
238 in++;
239 continue;
240 } else if (((*in >= 0x20) && (*in < 0x80)) ||
241 (*in == '\n') || (*in == '\t')) {
242 /*
243 * default case, just copy !
244 */
245 *out++ = *in++;
246 continue;
247 } else if (*in >= 0x80) {
248 /*
249 * We assume we have UTF-8 input.
250 */
Daniel Veillard83a75e02004-05-14 21:50:42 +0000251 if (outend - out < 10) break;
252
253 if (*in < 0xC0) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000254 xmlSaveErr(XML_SAVE_NOT_UTF8, NULL, NULL);
Daniel Veillard83a75e02004-05-14 21:50:42 +0000255 in++;
256 goto error;
257 } else if (*in < 0xE0) {
258 if (inend - in < 2) break;
259 val = (in[0]) & 0x1F;
260 val <<= 6;
261 val |= (in[1]) & 0x3F;
262 in += 2;
263 } else if (*in < 0xF0) {
264 if (inend - in < 3) break;
265 val = (in[0]) & 0x0F;
266 val <<= 6;
267 val |= (in[1]) & 0x3F;
268 val <<= 6;
269 val |= (in[2]) & 0x3F;
270 in += 3;
271 } else if (*in < 0xF8) {
272 if (inend - in < 4) break;
273 val = (in[0]) & 0x07;
274 val <<= 6;
275 val |= (in[1]) & 0x3F;
276 val <<= 6;
277 val |= (in[2]) & 0x3F;
278 val <<= 6;
279 val |= (in[3]) & 0x3F;
280 in += 4;
281 } else {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000282 xmlSaveErr(XML_SAVE_CHAR_INVALID, NULL, NULL);
Daniel Veillard83a75e02004-05-14 21:50:42 +0000283 in++;
284 goto error;
285 }
286 if (!IS_CHAR(val)) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000287 xmlSaveErr(XML_SAVE_CHAR_INVALID, NULL, NULL);
Daniel Veillard83a75e02004-05-14 21:50:42 +0000288 in++;
289 goto error;
290 }
291
292 /*
293 * We could do multiple things here. Just save as a char ref
294 */
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000295 out = xmlSerializeHexCharRef(out, val);
Daniel Veillard83a75e02004-05-14 21:50:42 +0000296 } else if (IS_BYTE_CHAR(*in)) {
297 if (outend - out < 6) break;
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000298 out = xmlSerializeHexCharRef(out, *in++);
Daniel Veillard83a75e02004-05-14 21:50:42 +0000299 } else {
300 xmlGenericError(xmlGenericErrorContext,
301 "xmlEscapeEntities : char out of range\n");
302 in++;
303 goto error;
304 }
305 }
306 *outlen = out - outstart;
307 *inlen = in - base;
308 return(0);
309error:
310 *outlen = out - outstart;
311 *inlen = in - base;
312 return(-1);
313}
314
315/************************************************************************
316 * *
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000317 * Allocation and deallocation *
318 * *
319 ************************************************************************/
Daniel Veillard753086a2004-03-28 16:12:44 +0000320/**
321 * xmlSaveCtxtInit:
322 * @ctxt: the saving context
323 *
324 * Initialize a saving context
325 */
326static void
327xmlSaveCtxtInit(xmlSaveCtxtPtr ctxt)
328{
329 int i;
William M. Brack12d37ab2005-02-21 13:54:07 +0000330 int len;
Daniel Veillard753086a2004-03-28 16:12:44 +0000331
332 if (ctxt == NULL) return;
Daniel Veillard3995bc32004-05-15 18:57:31 +0000333 if ((ctxt->encoding == NULL) && (ctxt->escape == NULL))
334 ctxt->escape = xmlEscapeEntities;
William M. Brack12d37ab2005-02-21 13:54:07 +0000335 len = xmlStrlen((xmlChar *)xmlTreeIndentString);
336 if ((xmlTreeIndentString == NULL) || (len == 0)) {
Daniel Veillard753086a2004-03-28 16:12:44 +0000337 memset(&ctxt->indent[0], 0, MAX_INDENT + 1);
338 } else {
William M. Brack12d37ab2005-02-21 13:54:07 +0000339 ctxt->indent_size = len;
Daniel Veillard753086a2004-03-28 16:12:44 +0000340 ctxt->indent_nr = MAX_INDENT / ctxt->indent_size;
341 for (i = 0;i < ctxt->indent_nr;i++)
342 memcpy(&ctxt->indent[i * ctxt->indent_size], xmlTreeIndentString,
343 ctxt->indent_size);
344 ctxt->indent[ctxt->indent_nr * ctxt->indent_size] = 0;
345 }
Rob Richards2ce51c02005-09-12 12:16:35 +0000346
Daniel Veillard9a00fd22005-11-09 08:56:26 +0000347 if (xmlSaveNoEmptyTags) {
348 ctxt->options |= XML_SAVE_NO_EMPTY;
349 }
Daniel Veillard753086a2004-03-28 16:12:44 +0000350}
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000351
352/**
353 * xmlFreeSaveCtxt:
354 *
355 * Free a saving context, destroying the ouptut in any remaining buffer
356 */
357static void
358xmlFreeSaveCtxt(xmlSaveCtxtPtr ctxt)
359{
360 if (ctxt == NULL) return;
361 if (ctxt->encoding != NULL)
362 xmlFree((char *) ctxt->encoding);
Daniel Veillarde2161a62004-04-29 17:14:25 +0000363 if (ctxt->buf != NULL)
364 xmlOutputBufferClose(ctxt->buf);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000365 xmlFree(ctxt);
366}
367
368/**
369 * xmlNewSaveCtxt:
370 *
371 * Create a new saving context
372 *
373 * Returns the new structure or NULL in case of error
374 */
375static xmlSaveCtxtPtr
376xmlNewSaveCtxt(const char *encoding, int options)
377{
378 xmlSaveCtxtPtr ret;
379
380 ret = (xmlSaveCtxtPtr) xmlMalloc(sizeof(xmlSaveCtxt));
381 if (ret == NULL) {
382 xmlSaveErrMemory("creating saving context");
383 return ( NULL );
384 }
385 memset(ret, 0, sizeof(xmlSaveCtxt));
Daniel Veillard6fc5db02005-01-16 00:05:58 +0000386
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000387 if (encoding != NULL) {
388 ret->handler = xmlFindCharEncodingHandler(encoding);
389 if (ret->handler == NULL) {
390 xmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL, encoding);
391 xmlFreeSaveCtxt(ret);
392 return(NULL);
393 }
394 ret->encoding = xmlStrdup((const xmlChar *)encoding);
Daniel Veillarddab39b52006-10-16 23:22:10 +0000395 ret->escape = NULL;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000396 }
Daniel Veillard753086a2004-03-28 16:12:44 +0000397 xmlSaveCtxtInit(ret);
398
Rob Richards2ce51c02005-09-12 12:16:35 +0000399 /*
400 * Use the options
401 */
402
Daniel Veillard9a00fd22005-11-09 08:56:26 +0000403 /* Re-check this option as it may already have been set */
404 if ((ret->options & XML_SAVE_NO_EMPTY) && ! (options & XML_SAVE_NO_EMPTY)) {
405 options |= XML_SAVE_NO_EMPTY;
406 }
Rob Richards2ce51c02005-09-12 12:16:35 +0000407
408 ret->options = options;
409 if (options & XML_SAVE_FORMAT)
410 ret->format = 1;
411
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000412 return(ret);
413}
414
415/************************************************************************
416 * *
417 * Dumping XML tree content to a simple buffer *
418 * *
419 ************************************************************************/
420/**
421 * xmlAttrSerializeContent:
422 * @buf: the XML buffer output
423 * @doc: the document
424 * @attr: the attribute pointer
425 *
426 * Serialize the attribute in the buffer
427 */
428static void
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000429xmlAttrSerializeContent(xmlOutputBufferPtr buf, xmlAttrPtr attr)
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000430{
431 xmlNodePtr children;
432
433 children = attr->children;
434 while (children != NULL) {
435 switch (children->type) {
436 case XML_TEXT_NODE:
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000437 xmlAttrSerializeTxtContent(buf->buffer, attr->doc,
438 attr, children->content);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000439 break;
440 case XML_ENTITY_REF_NODE:
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000441 xmlBufferAdd(buf->buffer, BAD_CAST "&", 1);
442 xmlBufferAdd(buf->buffer, children->name,
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000443 xmlStrlen(children->name));
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000444 xmlBufferAdd(buf->buffer, BAD_CAST ";", 1);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000445 break;
446 default:
447 /* should not happen unless we have a badly built tree */
448 break;
449 }
450 children = children->next;
451 }
452}
453
454/************************************************************************
455 * *
456 * Dumping XML tree content to an I/O output buffer *
457 * *
458 ************************************************************************/
459
460#ifdef LIBXML_HTML_ENABLED
461static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000462xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000463#endif
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000464static void xmlNodeListDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur);
465static void xmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000466void xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur);
Daniel Veillarddab39b52006-10-16 23:22:10 +0000467static int xmlDocContentDumpOutput(xmlSaveCtxtPtr ctxt, xmlDocPtr cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000468
469/**
470 * xmlNsDumpOutput:
471 * @buf: the XML buffer output
472 * @cur: a namespace
473 *
474 * Dump a local Namespace definition.
475 * Should be called in the context of attributes dumps.
476 */
477static void
478xmlNsDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000479 if ((cur == NULL) || (buf == NULL)) return;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000480 if ((cur->type == XML_LOCAL_NAMESPACE) && (cur->href != NULL)) {
481 if (xmlStrEqual(cur->prefix, BAD_CAST "xml"))
482 return;
483
484 /* Within the context of an element attributes */
485 if (cur->prefix != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000486 xmlOutputBufferWrite(buf, 7, " xmlns:");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000487 xmlOutputBufferWriteString(buf, (const char *)cur->prefix);
488 } else
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000489 xmlOutputBufferWrite(buf, 6, " xmlns");
490 xmlOutputBufferWrite(buf, 1, "=");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000491 xmlBufferWriteQuotedString(buf->buffer, cur->href);
492 }
493}
494
495/**
496 * xmlNsListDumpOutput:
497 * @buf: the XML buffer output
498 * @cur: the first namespace
499 *
500 * Dump a list of local Namespace definitions.
501 * Should be called in the context of attributes dumps.
502 */
503void
504xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) {
505 while (cur != NULL) {
506 xmlNsDumpOutput(buf, cur);
507 cur = cur->next;
508 }
509}
510
511/**
512 * xmlDtdDumpOutput:
513 * @buf: the XML buffer output
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000514 * @dtd: the pointer to the DTD
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000515 *
516 * Dump the XML document DTD, if any.
517 */
518static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000519xmlDtdDumpOutput(xmlSaveCtxtPtr ctxt, xmlDtdPtr dtd) {
520 xmlOutputBufferPtr buf;
521 int format, level;
522 xmlDocPtr doc;
523
524 if (dtd == NULL) return;
525 if ((ctxt == NULL) || (ctxt->buf == NULL))
526 return;
527 buf = ctxt->buf;
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000528 xmlOutputBufferWrite(buf, 10, "<!DOCTYPE ");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000529 xmlOutputBufferWriteString(buf, (const char *)dtd->name);
530 if (dtd->ExternalID != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000531 xmlOutputBufferWrite(buf, 8, " PUBLIC ");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000532 xmlBufferWriteQuotedString(buf->buffer, dtd->ExternalID);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000533 xmlOutputBufferWrite(buf, 1, " ");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000534 xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID);
535 } else if (dtd->SystemID != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000536 xmlOutputBufferWrite(buf, 8, " SYSTEM ");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000537 xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID);
538 }
539 if ((dtd->entities == NULL) && (dtd->elements == NULL) &&
Daniel Veillard41c4a752004-09-08 20:55:38 +0000540 (dtd->attributes == NULL) && (dtd->notations == NULL) &&
541 (dtd->pentities == NULL)) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000542 xmlOutputBufferWrite(buf, 1, ">");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000543 return;
544 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000545 xmlOutputBufferWrite(buf, 3, " [\n");
Daniel Veillard41c4a752004-09-08 20:55:38 +0000546 /*
547 * Dump the notations first they are not in the DTD children list
548 * Do this only on a standalone DTD or on the internal subset though.
549 */
550 if ((dtd->notations != NULL) && ((dtd->doc == NULL) ||
551 (dtd->doc->intSubset == dtd))) {
Daniel Veillardda3b29a2004-08-14 11:15:13 +0000552 xmlDumpNotationTable(buf->buffer, (xmlNotationTablePtr) dtd->notations);
553 }
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000554 format = ctxt->format;
555 level = ctxt->level;
556 doc = ctxt->doc;
557 ctxt->format = 0;
558 ctxt->level = -1;
559 ctxt->doc = dtd->doc;
560 xmlNodeListDumpOutput(ctxt, dtd->children);
561 ctxt->format = format;
562 ctxt->level = level;
563 ctxt->doc = doc;
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000564 xmlOutputBufferWrite(buf, 2, "]>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000565}
566
567/**
568 * xmlAttrDumpOutput:
569 * @buf: the XML buffer output
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000570 * @cur: the attribute pointer
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000571 *
572 * Dump an XML attribute
573 */
574static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000575xmlAttrDumpOutput(xmlSaveCtxtPtr ctxt, xmlAttrPtr cur) {
576 xmlOutputBufferPtr buf;
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000577
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000578 if (cur == NULL) return;
579 buf = ctxt->buf;
Daniel Veillardce244ad2004-11-05 10:03:46 +0000580 if (buf == NULL) return;
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000581 xmlOutputBufferWrite(buf, 1, " ");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000582 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
583 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000584 xmlOutputBufferWrite(buf, 1, ":");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000585 }
586 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000587 xmlOutputBufferWrite(buf, 2, "=\"");
588 xmlAttrSerializeContent(buf, cur);
589 xmlOutputBufferWrite(buf, 1, "\"");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000590}
591
592/**
593 * xmlAttrListDumpOutput:
594 * @buf: the XML buffer output
595 * @doc: the document
596 * @cur: the first attribute pointer
597 * @encoding: an optional encoding string
598 *
599 * Dump a list of XML attributes
600 */
601static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000602xmlAttrListDumpOutput(xmlSaveCtxtPtr ctxt, xmlAttrPtr cur) {
603 if (cur == NULL) return;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000604 while (cur != NULL) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000605 xmlAttrDumpOutput(ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000606 cur = cur->next;
607 }
608}
609
610
611
612/**
613 * xmlNodeListDumpOutput:
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000614 * @cur: the first node
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000615 *
616 * Dump an XML node list, recursive behaviour, children are printed too.
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000617 */
618static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000619xmlNodeListDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000620 xmlOutputBufferPtr buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000621
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000622 if (cur == NULL) return;
623 buf = ctxt->buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000624 while (cur != NULL) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000625 if ((ctxt->format) && (xmlIndentTreeOutput) &&
Daniel Veillardbd444842007-03-20 08:47:29 +0000626 ((cur->type == XML_ELEMENT_NODE) ||
627 (cur->type == XML_COMMENT_NODE) ||
628 (cur->type == XML_PI_NODE)))
Daniel Veillard753086a2004-03-28 16:12:44 +0000629 xmlOutputBufferWrite(buf, ctxt->indent_size *
630 (ctxt->level > ctxt->indent_nr ?
631 ctxt->indent_nr : ctxt->level),
632 ctxt->indent);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000633 xmlNodeDumpOutputInternal(ctxt, cur);
634 if (ctxt->format) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000635 xmlOutputBufferWrite(buf, 1, "\n");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000636 }
637 cur = cur->next;
638 }
639}
640
641/**
642 * xmlNodeDumpOutputInternal:
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000643 * @cur: the current node
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000644 *
645 * Dump an XML node, recursive behaviour, children are printed too.
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000646 */
647static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000648xmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
Daniel Veillard753086a2004-03-28 16:12:44 +0000649 int format;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000650 xmlNodePtr tmp;
651 xmlChar *start, *end;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000652 xmlOutputBufferPtr buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000653
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000654 if (cur == NULL) return;
655 buf = ctxt->buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000656 if (cur->type == XML_XINCLUDE_START)
657 return;
658 if (cur->type == XML_XINCLUDE_END)
659 return;
Daniel Veillardce244ad2004-11-05 10:03:46 +0000660 if ((cur->type == XML_DOCUMENT_NODE) ||
661 (cur->type == XML_HTML_DOCUMENT_NODE)) {
662 xmlDocContentDumpOutput(ctxt, (xmlDocPtr) cur);
663 return;
664 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000665 if (cur->type == XML_DTD_NODE) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000666 xmlDtdDumpOutput(ctxt, (xmlDtdPtr) cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000667 return;
668 }
669 if (cur->type == XML_DOCUMENT_FRAG_NODE) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000670 xmlNodeListDumpOutput(ctxt, cur->children);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000671 return;
672 }
673 if (cur->type == XML_ELEMENT_DECL) {
674 xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur);
675 return;
676 }
677 if (cur->type == XML_ATTRIBUTE_DECL) {
678 xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur);
679 return;
680 }
681 if (cur->type == XML_ENTITY_DECL) {
682 xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur);
683 return;
684 }
685 if (cur->type == XML_TEXT_NODE) {
686 if (cur->content != NULL) {
William M. Brack4e1c2db2005-02-11 10:58:55 +0000687 if (cur->name != xmlStringTextNoenc) {
Daniel Veillard3995bc32004-05-15 18:57:31 +0000688 xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000689 } else {
690 /*
691 * Disable escaping, needed for XSLT
692 */
693 xmlOutputBufferWriteString(buf, (const char *) cur->content);
694 }
695 }
696
697 return;
698 }
699 if (cur->type == XML_PI_NODE) {
700 if (cur->content != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000701 xmlOutputBufferWrite(buf, 2, "<?");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000702 xmlOutputBufferWriteString(buf, (const char *)cur->name);
703 if (cur->content != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000704 xmlOutputBufferWrite(buf, 1, " ");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000705 xmlOutputBufferWriteString(buf, (const char *)cur->content);
706 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000707 xmlOutputBufferWrite(buf, 2, "?>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000708 } else {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000709 xmlOutputBufferWrite(buf, 2, "<?");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000710 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000711 xmlOutputBufferWrite(buf, 2, "?>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000712 }
713 return;
714 }
715 if (cur->type == XML_COMMENT_NODE) {
716 if (cur->content != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000717 xmlOutputBufferWrite(buf, 4, "<!--");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000718 xmlOutputBufferWriteString(buf, (const char *)cur->content);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000719 xmlOutputBufferWrite(buf, 3, "-->");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000720 }
721 return;
722 }
723 if (cur->type == XML_ENTITY_REF_NODE) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000724 xmlOutputBufferWrite(buf, 1, "&");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000725 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000726 xmlOutputBufferWrite(buf, 1, ";");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000727 return;
728 }
729 if (cur->type == XML_CDATA_SECTION_NODE) {
Daniel Veillard7cd517c2005-05-20 18:47:22 +0000730 if (cur->content == NULL) {
731 xmlOutputBufferWrite(buf, 12, "<![CDATA[]]>");
732 } else {
733 start = end = cur->content;
734 while (*end != '\0') {
735 if ((*end == ']') && (*(end + 1) == ']') &&
736 (*(end + 2) == '>')) {
737 end = end + 2;
738 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
739 xmlOutputBufferWrite(buf, end - start, (const char *)start);
740 xmlOutputBufferWrite(buf, 3, "]]>");
741 start = end;
742 }
743 end++;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000744 }
Daniel Veillard7cd517c2005-05-20 18:47:22 +0000745 if (start != end) {
746 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
747 xmlOutputBufferWriteString(buf, (const char *)start);
748 xmlOutputBufferWrite(buf, 3, "]]>");
749 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000750 }
751 return;
752 }
753 if (cur->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000754 xmlAttrDumpOutput(ctxt, (xmlAttrPtr) cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000755 return;
756 }
757 if (cur->type == XML_NAMESPACE_DECL) {
758 xmlNsDumpOutput(buf, (xmlNsPtr) cur);
759 return;
760 }
761
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000762 format = ctxt->format;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000763 if (format == 1) {
764 tmp = cur->children;
765 while (tmp != NULL) {
766 if ((tmp->type == XML_TEXT_NODE) ||
767 (tmp->type == XML_CDATA_SECTION_NODE) ||
768 (tmp->type == XML_ENTITY_REF_NODE)) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000769 ctxt->format = 0;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000770 break;
771 }
772 tmp = tmp->next;
773 }
774 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000775 xmlOutputBufferWrite(buf, 1, "<");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000776 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
777 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000778 xmlOutputBufferWrite(buf, 1, ":");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000779 }
780
781 xmlOutputBufferWriteString(buf, (const char *)cur->name);
782 if (cur->nsDef)
783 xmlNsListDumpOutput(buf, cur->nsDef);
784 if (cur->properties != NULL)
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000785 xmlAttrListDumpOutput(ctxt, cur->properties);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000786
787 if (((cur->type == XML_ELEMENT_NODE) || (cur->content == NULL)) &&
Rob Richards2ce51c02005-09-12 12:16:35 +0000788 (cur->children == NULL) && ((ctxt->options & XML_SAVE_NO_EMPTY) == 0)) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000789 xmlOutputBufferWrite(buf, 2, "/>");
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000790 ctxt->format = format;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000791 return;
792 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000793 xmlOutputBufferWrite(buf, 1, ">");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000794 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Daniel Veillard3995bc32004-05-15 18:57:31 +0000795 xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000796 }
797 if (cur->children != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000798 if (ctxt->format) xmlOutputBufferWrite(buf, 1, "\n");
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000799 if (ctxt->level >= 0) ctxt->level++;
800 xmlNodeListDumpOutput(ctxt, cur->children);
801 if (ctxt->level > 0) ctxt->level--;
802 if ((xmlIndentTreeOutput) && (ctxt->format))
Daniel Veillard753086a2004-03-28 16:12:44 +0000803 xmlOutputBufferWrite(buf, ctxt->indent_size *
804 (ctxt->level > ctxt->indent_nr ?
805 ctxt->indent_nr : ctxt->level),
806 ctxt->indent);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000807 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000808 xmlOutputBufferWrite(buf, 2, "</");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000809 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
810 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000811 xmlOutputBufferWrite(buf, 1, ":");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000812 }
813
814 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000815 xmlOutputBufferWrite(buf, 1, ">");
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000816 ctxt->format = format;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000817}
818
819/**
820 * xmlDocContentDumpOutput:
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000821 * @cur: the document
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000822 *
823 * Dump an XML document.
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000824 */
Daniel Veillarddab39b52006-10-16 23:22:10 +0000825static int
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000826xmlDocContentDumpOutput(xmlSaveCtxtPtr ctxt, xmlDocPtr cur) {
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000827#ifdef LIBXML_HTML_ENABLED
828 xmlDtdPtr dtd;
829 int is_xhtml = 0;
830#endif
831 const xmlChar *oldenc = cur->encoding;
Daniel Veillarddab39b52006-10-16 23:22:10 +0000832 const xmlChar *oldctxtenc = ctxt->encoding;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000833 const xmlChar *encoding = ctxt->encoding;
Daniel Veillarddab39b52006-10-16 23:22:10 +0000834 xmlCharEncodingOutputFunc oldescape = ctxt->escape;
835 xmlCharEncodingOutputFunc oldescapeAttr = ctxt->escapeAttr;
836 xmlOutputBufferPtr buf = ctxt->buf;
837 xmlCharEncodingHandlerPtr handler = NULL;
838 xmlCharEncoding enc;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000839
840 xmlInitParser();
841
Daniel Veillarddab39b52006-10-16 23:22:10 +0000842 if (ctxt->encoding != NULL) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000843 cur->encoding = BAD_CAST ctxt->encoding;
Daniel Veillarddab39b52006-10-16 23:22:10 +0000844 } else if (cur->encoding != NULL) {
845 encoding = cur->encoding;
846 } else if (cur->charset != XML_CHAR_ENCODING_UTF8) {
847 encoding = (const xmlChar *)
848 xmlGetCharEncodingName((xmlCharEncoding) cur->charset);
849 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000850
Daniel Veillarddab39b52006-10-16 23:22:10 +0000851 enc = xmlParseCharEncoding((const char*) encoding);
852 if ((encoding != NULL) && (oldctxtenc == NULL) &&
853 (buf->encoder == NULL) && (buf->conv == NULL) &&
854 ((ctxt->options & XML_SAVE_NO_DECL) == 0)) {
855 if ((enc != XML_CHAR_ENCODING_UTF8) &&
856 (enc != XML_CHAR_ENCODING_NONE) &&
857 (enc != XML_CHAR_ENCODING_ASCII)) {
858 /*
859 * we need to switch to this encoding but just for this document
860 * since we output the XMLDecl the conversion must be done to not
861 * generate not well formed documents.
862 */
863 buf->encoder = xmlFindCharEncodingHandler((const char *)encoding);
864 if (buf->encoder == NULL) {
865 xmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL,
866 (const char *)encoding);
867 return(-1);
868 }
869 buf->conv = xmlBufferCreate();
870 if (buf->conv == NULL) {
871 xmlCharEncCloseFunc(buf->encoder);
872 xmlSaveErrMemory("creating encoding buffer");
873 return(-1);
874 }
875 /*
876 * initialize the state, e.g. if outputting a BOM
877 */
878 xmlCharEncOutFunc(buf->encoder, buf->conv, NULL);
879 }
880 if (ctxt->escape == xmlEscapeEntities)
881 ctxt->escape = NULL;
882 if (ctxt->escapeAttr == xmlEscapeEntities)
883 ctxt->escapeAttr = NULL;
884 }
885
886
887 /*
888 * Save the XML declaration
889 */
Daniel Veillard100e1802005-08-08 14:44:11 +0000890 if ((ctxt->options & XML_SAVE_NO_DECL) == 0) {
891 xmlOutputBufferWrite(buf, 14, "<?xml version=");
892 if (cur->version != NULL)
893 xmlBufferWriteQuotedString(buf->buffer, cur->version);
894 else
895 xmlOutputBufferWrite(buf, 5, "\"1.0\"");
Daniel Veillard100e1802005-08-08 14:44:11 +0000896 if (encoding != NULL) {
897 xmlOutputBufferWrite(buf, 10, " encoding=");
898 xmlBufferWriteQuotedString(buf->buffer, (xmlChar *) encoding);
899 }
900 switch (cur->standalone) {
901 case 0:
902 xmlOutputBufferWrite(buf, 16, " standalone=\"no\"");
903 break;
904 case 1:
905 xmlOutputBufferWrite(buf, 17, " standalone=\"yes\"");
906 break;
907 }
908 xmlOutputBufferWrite(buf, 3, "?>\n");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000909 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000910
911#ifdef LIBXML_HTML_ENABLED
Daniel Veillard33b20b72005-09-12 21:43:20 +0000912 if ((ctxt->options & XML_SAVE_NO_XHTML) == 0) {
913 dtd = xmlGetIntSubset(cur);
914 if (dtd != NULL) {
915 is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
916 if (is_xhtml < 0) is_xhtml = 0;
917 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000918 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000919#endif
920 if (cur->children != NULL) {
921 xmlNodePtr child = cur->children;
922
923 while (child != NULL) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000924 ctxt->level = 0;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000925#ifdef LIBXML_HTML_ENABLED
926 if (is_xhtml)
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000927 xhtmlNodeDumpOutput(ctxt, child);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000928 else
929#endif
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000930 xmlNodeDumpOutputInternal(ctxt, child);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000931 xmlOutputBufferWrite(buf, 1, "\n");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000932 child = child->next;
933 }
934 }
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000935 if (ctxt->encoding != NULL)
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000936 cur->encoding = oldenc;
Daniel Veillarddab39b52006-10-16 23:22:10 +0000937
938 /*
939 * Restore the state of the saving context at the end of the document
940 */
941 if ((encoding != NULL) && (oldctxtenc == NULL) &&
942 ((ctxt->options & XML_SAVE_NO_DECL) == 0)) {
943 if ((enc != XML_CHAR_ENCODING_UTF8) &&
944 (enc != XML_CHAR_ENCODING_NONE) &&
945 (enc != XML_CHAR_ENCODING_ASCII)) {
946 xmlOutputBufferFlush(buf);
947 xmlCharEncCloseFunc(buf->encoder);
948 xmlBufferFree(buf->conv);
949 buf->encoder = NULL;
950 buf->conv = NULL;
951 }
952 ctxt->escape = oldescape;
953 ctxt->escapeAttr = oldescapeAttr;
954 }
955 return(0);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000956}
957
958#ifdef LIBXML_HTML_ENABLED
959/************************************************************************
960 * *
961 * Functions specific to XHTML serialization *
962 * *
963 ************************************************************************/
964
965/**
966 * xhtmlIsEmpty:
967 * @node: the node
968 *
969 * Check if a node is an empty xhtml node
970 *
971 * Returns 1 if the node is an empty node, 0 if not and -1 in case of error
972 */
973static int
974xhtmlIsEmpty(xmlNodePtr node) {
975 if (node == NULL)
976 return(-1);
977 if (node->type != XML_ELEMENT_NODE)
978 return(0);
979 if ((node->ns != NULL) && (!xmlStrEqual(node->ns->href, XHTML_NS_NAME)))
980 return(0);
981 if (node->children != NULL)
982 return(0);
983 switch (node->name[0]) {
984 case 'a':
985 if (xmlStrEqual(node->name, BAD_CAST "area"))
986 return(1);
987 return(0);
988 case 'b':
989 if (xmlStrEqual(node->name, BAD_CAST "br"))
990 return(1);
991 if (xmlStrEqual(node->name, BAD_CAST "base"))
992 return(1);
993 if (xmlStrEqual(node->name, BAD_CAST "basefont"))
994 return(1);
995 return(0);
996 case 'c':
997 if (xmlStrEqual(node->name, BAD_CAST "col"))
998 return(1);
999 return(0);
1000 case 'f':
1001 if (xmlStrEqual(node->name, BAD_CAST "frame"))
1002 return(1);
1003 return(0);
1004 case 'h':
1005 if (xmlStrEqual(node->name, BAD_CAST "hr"))
1006 return(1);
1007 return(0);
1008 case 'i':
1009 if (xmlStrEqual(node->name, BAD_CAST "img"))
1010 return(1);
1011 if (xmlStrEqual(node->name, BAD_CAST "input"))
1012 return(1);
1013 if (xmlStrEqual(node->name, BAD_CAST "isindex"))
1014 return(1);
1015 return(0);
1016 case 'l':
1017 if (xmlStrEqual(node->name, BAD_CAST "link"))
1018 return(1);
1019 return(0);
1020 case 'm':
1021 if (xmlStrEqual(node->name, BAD_CAST "meta"))
1022 return(1);
1023 return(0);
1024 case 'p':
1025 if (xmlStrEqual(node->name, BAD_CAST "param"))
1026 return(1);
1027 return(0);
1028 }
1029 return(0);
1030}
1031
1032/**
1033 * xhtmlAttrListDumpOutput:
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001034 * @cur: the first attribute pointer
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001035 *
1036 * Dump a list of XML attributes
1037 */
1038static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001039xhtmlAttrListDumpOutput(xmlSaveCtxtPtr ctxt, xmlAttrPtr cur) {
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001040 xmlAttrPtr xml_lang = NULL;
1041 xmlAttrPtr lang = NULL;
1042 xmlAttrPtr name = NULL;
1043 xmlAttrPtr id = NULL;
1044 xmlNodePtr parent;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001045 xmlOutputBufferPtr buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001046
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001047 if (cur == NULL) return;
1048 buf = ctxt->buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001049 parent = cur->parent;
1050 while (cur != NULL) {
1051 if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "id")))
1052 id = cur;
1053 else
1054 if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "name")))
1055 name = cur;
1056 else
1057 if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")))
1058 lang = cur;
1059 else
1060 if ((cur->ns != NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")) &&
1061 (xmlStrEqual(cur->ns->prefix, BAD_CAST "xml")))
1062 xml_lang = cur;
1063 else if ((cur->ns == NULL) &&
1064 ((cur->children == NULL) ||
1065 (cur->children->content == NULL) ||
1066 (cur->children->content[0] == 0)) &&
1067 (htmlIsBooleanAttr(cur->name))) {
1068 if (cur->children != NULL)
1069 xmlFreeNode(cur->children);
1070 cur->children = xmlNewText(cur->name);
1071 if (cur->children != NULL)
1072 cur->children->parent = (xmlNodePtr) cur;
1073 }
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001074 xmlAttrDumpOutput(ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001075 cur = cur->next;
1076 }
1077 /*
1078 * C.8
1079 */
1080 if ((name != NULL) && (id == NULL)) {
1081 if ((parent != NULL) && (parent->name != NULL) &&
1082 ((xmlStrEqual(parent->name, BAD_CAST "a")) ||
1083 (xmlStrEqual(parent->name, BAD_CAST "p")) ||
1084 (xmlStrEqual(parent->name, BAD_CAST "div")) ||
1085 (xmlStrEqual(parent->name, BAD_CAST "img")) ||
1086 (xmlStrEqual(parent->name, BAD_CAST "map")) ||
1087 (xmlStrEqual(parent->name, BAD_CAST "applet")) ||
1088 (xmlStrEqual(parent->name, BAD_CAST "form")) ||
1089 (xmlStrEqual(parent->name, BAD_CAST "frame")) ||
1090 (xmlStrEqual(parent->name, BAD_CAST "iframe")))) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001091 xmlOutputBufferWrite(buf, 5, " id=\"");
1092 xmlAttrSerializeContent(buf, name);
1093 xmlOutputBufferWrite(buf, 1, "\"");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001094 }
1095 }
1096 /*
1097 * C.7.
1098 */
1099 if ((lang != NULL) && (xml_lang == NULL)) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001100 xmlOutputBufferWrite(buf, 11, " xml:lang=\"");
1101 xmlAttrSerializeContent(buf, lang);
1102 xmlOutputBufferWrite(buf, 1, "\"");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001103 } else
1104 if ((xml_lang != NULL) && (lang == NULL)) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001105 xmlOutputBufferWrite(buf, 7, " lang=\"");
1106 xmlAttrSerializeContent(buf, xml_lang);
1107 xmlOutputBufferWrite(buf, 1, "\"");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001108 }
1109}
1110
1111/**
1112 * xhtmlNodeListDumpOutput:
1113 * @buf: the XML buffer output
1114 * @doc: the XHTML document
1115 * @cur: the first node
1116 * @level: the imbrication level for indenting
1117 * @format: is formatting allowed
1118 * @encoding: an optional encoding string
1119 *
1120 * Dump an XML node list, recursive behaviour, children are printed too.
1121 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
1122 * or xmlKeepBlanksDefault(0) was called
1123 */
1124static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001125xhtmlNodeListDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001126 xmlOutputBufferPtr buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001127
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001128 if (cur == NULL) return;
1129 buf = ctxt->buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001130 while (cur != NULL) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001131 if ((ctxt->format) && (xmlIndentTreeOutput) &&
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001132 (cur->type == XML_ELEMENT_NODE))
Daniel Veillard753086a2004-03-28 16:12:44 +00001133 xmlOutputBufferWrite(buf, ctxt->indent_size *
1134 (ctxt->level > ctxt->indent_nr ?
1135 ctxt->indent_nr : ctxt->level),
1136 ctxt->indent);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001137 xhtmlNodeDumpOutput(ctxt, cur);
1138 if (ctxt->format) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001139 xmlOutputBufferWrite(buf, 1, "\n");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001140 }
1141 cur = cur->next;
1142 }
1143}
1144
1145/**
1146 * xhtmlNodeDumpOutput:
1147 * @buf: the XML buffer output
1148 * @doc: the XHTML document
1149 * @cur: the current node
1150 * @level: the imbrication level for indenting
1151 * @format: is formatting allowed
1152 * @encoding: an optional encoding string
1153 *
1154 * Dump an XHTML node, recursive behaviour, children are printed too.
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001155 */
1156static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001157xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
Rob Richards31f73022005-08-26 15:33:26 +00001158 int format, addmeta = 0;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001159 xmlNodePtr tmp;
1160 xmlChar *start, *end;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001161 xmlOutputBufferPtr buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001162
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001163 if (cur == NULL) return;
Daniel Veillard60071ae2005-09-12 00:03:43 +00001164 if ((cur->type == XML_DOCUMENT_NODE) ||
1165 (cur->type == XML_HTML_DOCUMENT_NODE)) {
1166 xmlDocContentDumpOutput(ctxt, (xmlDocPtr) cur);
1167 return;
1168 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001169 if (cur->type == XML_XINCLUDE_START)
1170 return;
1171 if (cur->type == XML_XINCLUDE_END)
1172 return;
1173 if (cur->type == XML_DTD_NODE) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001174 xmlDtdDumpOutput(ctxt, (xmlDtdPtr) cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001175 return;
1176 }
Rob Richards2e2691b2005-10-21 14:45:16 +00001177 if (cur->type == XML_DOCUMENT_FRAG_NODE) {
1178 xhtmlNodeListDumpOutput(ctxt, cur->children);
1179 return;
1180 }
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001181 buf = ctxt->buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001182 if (cur->type == XML_ELEMENT_DECL) {
1183 xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur);
1184 return;
1185 }
1186 if (cur->type == XML_ATTRIBUTE_DECL) {
1187 xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur);
1188 return;
1189 }
1190 if (cur->type == XML_ENTITY_DECL) {
1191 xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur);
1192 return;
1193 }
1194 if (cur->type == XML_TEXT_NODE) {
1195 if (cur->content != NULL) {
1196 if ((cur->name == xmlStringText) ||
1197 (cur->name != xmlStringTextNoenc)) {
Daniel Veillard3995bc32004-05-15 18:57:31 +00001198 xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001199 } else {
1200 /*
1201 * Disable escaping, needed for XSLT
1202 */
1203 xmlOutputBufferWriteString(buf, (const char *) cur->content);
1204 }
1205 }
1206
1207 return;
1208 }
1209 if (cur->type == XML_PI_NODE) {
1210 if (cur->content != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001211 xmlOutputBufferWrite(buf, 2, "<?");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001212 xmlOutputBufferWriteString(buf, (const char *)cur->name);
1213 if (cur->content != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001214 xmlOutputBufferWrite(buf, 1, " ");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001215 xmlOutputBufferWriteString(buf, (const char *)cur->content);
1216 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001217 xmlOutputBufferWrite(buf, 2, "?>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001218 } else {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001219 xmlOutputBufferWrite(buf, 2, "<?");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001220 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001221 xmlOutputBufferWrite(buf, 2, "?>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001222 }
1223 return;
1224 }
1225 if (cur->type == XML_COMMENT_NODE) {
1226 if (cur->content != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001227 xmlOutputBufferWrite(buf, 4, "<!--");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001228 xmlOutputBufferWriteString(buf, (const char *)cur->content);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001229 xmlOutputBufferWrite(buf, 3, "-->");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001230 }
1231 return;
1232 }
1233 if (cur->type == XML_ENTITY_REF_NODE) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001234 xmlOutputBufferWrite(buf, 1, "&");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001235 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001236 xmlOutputBufferWrite(buf, 1, ";");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001237 return;
1238 }
1239 if (cur->type == XML_CDATA_SECTION_NODE) {
1240 start = end = cur->content;
1241 while (*end != '\0') {
1242 if (*end == ']' && *(end + 1) == ']' && *(end + 2) == '>') {
1243 end = end + 2;
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001244 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001245 xmlOutputBufferWrite(buf, end - start, (const char *)start);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001246 xmlOutputBufferWrite(buf, 3, "]]>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001247 start = end;
1248 }
1249 end++;
1250 }
1251 if (start != end) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001252 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001253 xmlOutputBufferWriteString(buf, (const char *)start);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001254 xmlOutputBufferWrite(buf, 3, "]]>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001255 }
1256 return;
1257 }
1258
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001259 format = ctxt->format;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001260 if (format == 1) {
1261 tmp = cur->children;
1262 while (tmp != NULL) {
1263 if ((tmp->type == XML_TEXT_NODE) ||
1264 (tmp->type == XML_ENTITY_REF_NODE)) {
1265 format = 0;
1266 break;
1267 }
1268 tmp = tmp->next;
1269 }
1270 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001271 xmlOutputBufferWrite(buf, 1, "<");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001272 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1273 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001274 xmlOutputBufferWrite(buf, 1, ":");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001275 }
1276
1277 xmlOutputBufferWriteString(buf, (const char *)cur->name);
1278 if (cur->nsDef)
1279 xmlNsListDumpOutput(buf, cur->nsDef);
1280 if ((xmlStrEqual(cur->name, BAD_CAST "html") &&
1281 (cur->ns == NULL) && (cur->nsDef == NULL))) {
1282 /*
1283 * 3.1.1. Strictly Conforming Documents A.3.1.1 3/
1284 */
1285 xmlOutputBufferWriteString(buf,
1286 " xmlns=\"http://www.w3.org/1999/xhtml\"");
1287 }
1288 if (cur->properties != NULL)
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001289 xhtmlAttrListDumpOutput(ctxt, cur->properties);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001290
Rob Richards31f73022005-08-26 15:33:26 +00001291 if ((cur->type == XML_ELEMENT_NODE) &&
1292 (cur->parent != NULL) &&
1293 (cur->parent->parent == (xmlNodePtr) cur->doc) &&
1294 xmlStrEqual(cur->name, BAD_CAST"head") &&
1295 xmlStrEqual(cur->parent->name, BAD_CAST"html")) {
1296
1297 tmp = cur->children;
1298 while (tmp != NULL) {
1299 if (xmlStrEqual(tmp->name, BAD_CAST"meta")) {
1300 xmlChar *httpequiv;
1301
1302 httpequiv = xmlGetProp(tmp, BAD_CAST"http-equiv");
Rob Richards07b72002005-09-03 14:56:36 +00001303 if (httpequiv != NULL) {
1304 if (xmlStrcasecmp(httpequiv, BAD_CAST"Content-Type") == 0) {
1305 xmlFree(httpequiv);
1306 break;
1307 }
Rob Richards31f73022005-08-26 15:33:26 +00001308 xmlFree(httpequiv);
Rob Richards31f73022005-08-26 15:33:26 +00001309 }
Rob Richards31f73022005-08-26 15:33:26 +00001310 }
1311 tmp = tmp->next;
1312 }
1313 if (tmp == NULL)
1314 addmeta = 1;
1315 }
1316
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001317 if ((cur->type == XML_ELEMENT_NODE) && (cur->children == NULL)) {
1318 if (((cur->ns == NULL) || (cur->ns->prefix == NULL)) &&
Rob Richards31f73022005-08-26 15:33:26 +00001319 ((xhtmlIsEmpty(cur) == 1) && (addmeta == 0))) {
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001320 /*
1321 * C.2. Empty Elements
1322 */
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001323 xmlOutputBufferWrite(buf, 3, " />");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001324 } else {
Rob Richards31f73022005-08-26 15:33:26 +00001325 if (addmeta == 1) {
1326 xmlOutputBufferWrite(buf, 1, ">");
Rob Richards2ce51c02005-09-12 12:16:35 +00001327 if (ctxt->format) {
1328 xmlOutputBufferWrite(buf, 1, "\n");
1329 if (xmlIndentTreeOutput)
1330 xmlOutputBufferWrite(buf, ctxt->indent_size *
1331 (ctxt->level + 1 > ctxt->indent_nr ?
1332 ctxt->indent_nr : ctxt->level + 1), ctxt->indent);
1333 }
Rob Richards31f73022005-08-26 15:33:26 +00001334 xmlOutputBufferWriteString(buf,
1335 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=");
1336 if (ctxt->encoding) {
1337 xmlOutputBufferWriteString(buf, (const char *)ctxt->encoding);
1338 } else {
1339 xmlOutputBufferWrite(buf, 5, "UTF-8");
1340 }
Rob Richards2ce51c02005-09-12 12:16:35 +00001341 xmlOutputBufferWrite(buf, 4, "\" />");
1342 if (ctxt->format)
1343 xmlOutputBufferWrite(buf, 1, "\n");
1344 } else {
1345 xmlOutputBufferWrite(buf, 1, ">");
Rob Richards31f73022005-08-26 15:33:26 +00001346 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001347 /*
1348 * C.3. Element Minimization and Empty Element Content
1349 */
Rob Richards2ce51c02005-09-12 12:16:35 +00001350 xmlOutputBufferWrite(buf, 2, "</");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001351 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1352 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001353 xmlOutputBufferWrite(buf, 1, ":");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001354 }
1355 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001356 xmlOutputBufferWrite(buf, 1, ">");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001357 }
1358 return;
1359 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001360 xmlOutputBufferWrite(buf, 1, ">");
Rob Richards31f73022005-08-26 15:33:26 +00001361 if (addmeta == 1) {
Rob Richards2ce51c02005-09-12 12:16:35 +00001362 if (ctxt->format) {
1363 xmlOutputBufferWrite(buf, 1, "\n");
1364 if (xmlIndentTreeOutput)
1365 xmlOutputBufferWrite(buf, ctxt->indent_size *
1366 (ctxt->level + 1 > ctxt->indent_nr ?
1367 ctxt->indent_nr : ctxt->level + 1), ctxt->indent);
1368 }
Rob Richards31f73022005-08-26 15:33:26 +00001369 xmlOutputBufferWriteString(buf,
1370 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=");
1371 if (ctxt->encoding) {
1372 xmlOutputBufferWriteString(buf, (const char *)ctxt->encoding);
1373 } else {
1374 xmlOutputBufferWrite(buf, 5, "UTF-8");
1375 }
1376 xmlOutputBufferWrite(buf, 4, "\" />");
1377 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001378 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Daniel Veillard3995bc32004-05-15 18:57:31 +00001379 xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001380 }
1381
Kasimier T. Buchcik7b4e2e22006-07-13 13:07:11 +00001382#if 0
1383 /*
1384 * This was removed due to problems with HTML processors.
1385 * See bug #345147.
Daniel Veillarddab39b52006-10-16 23:22:10 +00001386 */
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001387 /*
1388 * 4.8. Script and Style elements
1389 */
1390 if ((cur->type == XML_ELEMENT_NODE) &&
1391 ((xmlStrEqual(cur->name, BAD_CAST "script")) ||
1392 (xmlStrEqual(cur->name, BAD_CAST "style"))) &&
1393 ((cur->ns == NULL) ||
1394 (xmlStrEqual(cur->ns->href, XHTML_NS_NAME)))) {
1395 xmlNodePtr child = cur->children;
1396
1397 while (child != NULL) {
Daniel Veillarddbd61052005-09-12 14:03:26 +00001398 if (child->type == XML_TEXT_NODE) {
1399 if ((xmlStrchr(child->content, '<') == NULL) &&
1400 (xmlStrchr(child->content, '&') == NULL) &&
1401 (xmlStrstr(child->content, BAD_CAST "]]>") == NULL)) {
1402 /* Nothing to escape, so just output as is... */
1403 /* FIXME: Should we do something about "--" also? */
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001404 int level = ctxt->level;
1405 int indent = ctxt->format;
1406
1407 ctxt->level = 0;
1408 ctxt->format = 0;
Daniel Veillarddbd61052005-09-12 14:03:26 +00001409 xmlOutputBufferWriteString(buf, (const char *) child->content);
1410 /* (We cannot use xhtmlNodeDumpOutput() here because
1411 * we wish to leave '>' unescaped!) */
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001412 ctxt->level = level;
1413 ctxt->format = indent;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001414 } else {
Daniel Veillarddbd61052005-09-12 14:03:26 +00001415 /* We must use a CDATA section. Unfortunately,
1416 * this will break CSS and JavaScript when read by
1417 * a browser in HTML4-compliant mode. :-( */
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001418 start = end = child->content;
1419 while (*end != '\0') {
1420 if (*end == ']' &&
1421 *(end + 1) == ']' &&
1422 *(end + 2) == '>') {
1423 end = end + 2;
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001424 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001425 xmlOutputBufferWrite(buf, end - start,
1426 (const char *)start);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001427 xmlOutputBufferWrite(buf, 3, "]]>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001428 start = end;
1429 }
1430 end++;
1431 }
1432 if (start != end) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001433 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
1434 xmlOutputBufferWrite(buf, end - start,
1435 (const char *)start);
1436 xmlOutputBufferWrite(buf, 3, "]]>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001437 }
1438 }
1439 } else {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001440 int level = ctxt->level;
1441 int indent = ctxt->format;
1442
1443 ctxt->level = 0;
1444 ctxt->format = 0;
1445 xhtmlNodeDumpOutput(ctxt, child);
1446 ctxt->level = level;
1447 ctxt->format = indent;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001448 }
1449 child = child->next;
1450 }
Kasimier T. Buchcik7b4e2e22006-07-13 13:07:11 +00001451 }
1452#endif
1453
1454 if (cur->children != NULL) {
Daniel Veillardf0244ce2004-05-09 23:48:39 +00001455 int indent = ctxt->format;
1456
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001457 if (format) xmlOutputBufferWrite(buf, 1, "\n");
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001458 if (ctxt->level >= 0) ctxt->level++;
Daniel Veillardf0244ce2004-05-09 23:48:39 +00001459 ctxt->format = format;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001460 xhtmlNodeListDumpOutput(ctxt, cur->children);
1461 if (ctxt->level > 0) ctxt->level--;
Daniel Veillardf0244ce2004-05-09 23:48:39 +00001462 ctxt->format = indent;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001463 if ((xmlIndentTreeOutput) && (format))
Daniel Veillard753086a2004-03-28 16:12:44 +00001464 xmlOutputBufferWrite(buf, ctxt->indent_size *
1465 (ctxt->level > ctxt->indent_nr ?
1466 ctxt->indent_nr : ctxt->level),
1467 ctxt->indent);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001468 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001469 xmlOutputBufferWrite(buf, 2, "</");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001470 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1471 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001472 xmlOutputBufferWrite(buf, 1, ":");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001473 }
1474
1475 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001476 xmlOutputBufferWrite(buf, 1, ">");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001477}
1478#endif
1479
1480/************************************************************************
1481 * *
1482 * Public entry points *
1483 * *
1484 ************************************************************************/
1485
1486/**
1487 * xmlSaveToFd:
1488 * @fd: a file descriptor number
1489 * @encoding: the encoding name to use or NULL
1490 * @options: a set of xmlSaveOptions
1491 *
1492 * Create a document saving context serializing to a file descriptor
1493 * with the encoding and the options given.
1494 *
1495 * Returns a new serialization context or NULL in case of error.
1496 */
1497xmlSaveCtxtPtr
1498xmlSaveToFd(int fd, const char *encoding, int options)
1499{
1500 xmlSaveCtxtPtr ret;
1501
1502 ret = xmlNewSaveCtxt(encoding, options);
1503 if (ret == NULL) return(NULL);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001504 ret->buf = xmlOutputBufferCreateFd(fd, ret->handler);
1505 if (ret->buf == NULL) {
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001506 xmlFreeSaveCtxt(ret);
1507 return(NULL);
1508 }
1509 return(ret);
1510}
1511
1512/**
1513 * xmlSaveToFilename:
1514 * @filename: a file name or an URL
1515 * @encoding: the encoding name to use or NULL
1516 * @options: a set of xmlSaveOptions
1517 *
1518 * Create a document saving context serializing to a filename or possibly
1519 * to an URL (but this is less reliable) with the encoding and the options
1520 * given.
1521 *
1522 * Returns a new serialization context or NULL in case of error.
1523 */
1524xmlSaveCtxtPtr
1525xmlSaveToFilename(const char *filename, const char *encoding, int options)
1526{
1527 xmlSaveCtxtPtr ret;
1528 int compression = 0; /* TODO handle compression option */
1529
1530 ret = xmlNewSaveCtxt(encoding, options);
1531 if (ret == NULL) return(NULL);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001532 ret->buf = xmlOutputBufferCreateFilename(filename, ret->handler,
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001533 compression);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001534 if (ret->buf == NULL) {
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001535 xmlFreeSaveCtxt(ret);
1536 return(NULL);
1537 }
1538 return(ret);
1539}
1540
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001541/**
1542 * xmlSaveToBuffer:
1543 * @buffer: a buffer
1544 * @encoding: the encoding name to use or NULL
1545 * @options: a set of xmlSaveOptions
1546 *
1547 * Create a document saving context serializing to a buffer
1548 * with the encoding and the options given
1549 *
1550 * Returns a new serialization context or NULL in case of error.
Daniel Veillard9a00fd22005-11-09 08:56:26 +00001551 */
1552
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001553xmlSaveCtxtPtr
1554xmlSaveToBuffer(xmlBufferPtr buffer, const char *encoding, int options)
1555{
Daniel Veillard9a00fd22005-11-09 08:56:26 +00001556 xmlSaveCtxtPtr ret;
1557 xmlOutputBufferPtr out_buff;
1558 xmlCharEncodingHandlerPtr handler;
1559
1560 ret = xmlNewSaveCtxt(encoding, options);
1561 if (ret == NULL) return(NULL);
1562
1563 if (encoding != NULL) {
1564 handler = xmlFindCharEncodingHandler(encoding);
1565 if (handler == NULL) {
1566 xmlFree(ret);
1567 return(NULL);
1568 }
1569 } else
1570 handler = NULL;
1571 out_buff = xmlOutputBufferCreateBuffer(buffer, handler);
1572 if (out_buff == NULL) {
1573 xmlFree(ret);
1574 if (handler) xmlCharEncCloseFunc(handler);
1575 return(NULL);
1576 }
1577
1578 ret->buf = out_buff;
1579 return(ret);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001580}
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001581
1582/**
1583 * xmlSaveToIO:
1584 * @iowrite: an I/O write function
1585 * @ioclose: an I/O close function
1586 * @ioctx: an I/O handler
1587 * @encoding: the encoding name to use or NULL
1588 * @options: a set of xmlSaveOptions
1589 *
1590 * Create a document saving context serializing to a file descriptor
1591 * with the encoding and the options given
1592 *
1593 * Returns a new serialization context or NULL in case of error.
1594 */
1595xmlSaveCtxtPtr
1596xmlSaveToIO(xmlOutputWriteCallback iowrite,
1597 xmlOutputCloseCallback ioclose,
1598 void *ioctx, const char *encoding, int options)
1599{
1600 xmlSaveCtxtPtr ret;
1601
1602 ret = xmlNewSaveCtxt(encoding, options);
1603 if (ret == NULL) return(NULL);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001604 ret->buf = xmlOutputBufferCreateIO(iowrite, ioclose, ioctx, ret->handler);
1605 if (ret->buf == NULL) {
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001606 xmlFreeSaveCtxt(ret);
1607 return(NULL);
1608 }
1609 return(ret);
1610}
1611
1612/**
1613 * xmlSaveDoc:
1614 * @ctxt: a document saving context
1615 * @doc: a document
1616 *
1617 * Save a full document to a saving context
Daniel Veillard377e1a92004-04-16 16:30:05 +00001618 * TODO: The function is not fully implemented yet as it does not return the
1619 * byte count but 0 instead
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001620 *
1621 * Returns the number of byte written or -1 in case of error
1622 */
1623long
1624xmlSaveDoc(xmlSaveCtxtPtr ctxt, xmlDocPtr doc)
1625{
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001626 long ret = 0;
1627
Daniel Veillardce682bc2004-11-05 17:22:25 +00001628 if ((ctxt == NULL) || (doc == NULL)) return(-1);
Daniel Veillarddab39b52006-10-16 23:22:10 +00001629 if (xmlDocContentDumpOutput(ctxt, doc) < 0)
1630 return(-1);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001631 return(ret);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001632}
1633
1634/**
1635 * xmlSaveTree:
1636 * @ctxt: a document saving context
Daniel Veillard681e9042006-09-29 09:16:00 +00001637 * @node: the top node of the subtree to save
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001638 *
1639 * Save a subtree starting at the node parameter to a saving context
Daniel Veillard377e1a92004-04-16 16:30:05 +00001640 * TODO: The function is not fully implemented yet as it does not return the
1641 * byte count but 0 instead
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001642 *
1643 * Returns the number of byte written or -1 in case of error
1644 */
1645long
1646xmlSaveTree(xmlSaveCtxtPtr ctxt, xmlNodePtr node)
1647{
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001648 long ret = 0;
1649
Daniel Veillardce682bc2004-11-05 17:22:25 +00001650 if ((ctxt == NULL) || (node == NULL)) return(-1);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001651 xmlNodeDumpOutputInternal(ctxt, node);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001652 return(ret);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001653}
1654
1655/**
1656 * xmlSaveFlush:
1657 * @ctxt: a document saving context
1658 *
1659 * Flush a document saving context, i.e. make sure that all bytes have
1660 * been output.
1661 *
1662 * Returns the number of byte written or -1 in case of error.
1663 */
1664int
1665xmlSaveFlush(xmlSaveCtxtPtr ctxt)
1666{
1667 if (ctxt == NULL) return(-1);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001668 if (ctxt->buf == NULL) return(-1);
1669 return(xmlOutputBufferFlush(ctxt->buf));
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001670}
1671
1672/**
1673 * xmlSaveClose:
1674 * @ctxt: a document saving context
1675 *
1676 * Close a document saving context, i.e. make sure that all bytes have
1677 * been output and free the associated data.
1678 *
1679 * Returns the number of byte written or -1 in case of error.
1680 */
1681int
1682xmlSaveClose(xmlSaveCtxtPtr ctxt)
1683{
1684 int ret;
1685
1686 if (ctxt == NULL) return(-1);
1687 ret = xmlSaveFlush(ctxt);
1688 xmlFreeSaveCtxt(ctxt);
1689 return(ret);
1690}
1691
Daniel Veillard3995bc32004-05-15 18:57:31 +00001692/**
1693 * xmlSaveSetEscape:
1694 * @ctxt: a document saving context
1695 * @escape: the escaping function
1696 *
1697 * Set a custom escaping function to be used for text in element content
1698 *
1699 * Returns 0 if successful or -1 in case of error.
1700 */
1701int
1702xmlSaveSetEscape(xmlSaveCtxtPtr ctxt, xmlCharEncodingOutputFunc escape)
1703{
1704 if (ctxt == NULL) return(-1);
1705 ctxt->escape = escape;
1706 return(0);
1707}
1708
1709/**
1710 * xmlSaveSetAttrEscape:
1711 * @ctxt: a document saving context
1712 * @escape: the escaping function
1713 *
1714 * Set a custom escaping function to be used for text in attribute content
1715 *
1716 * Returns 0 if successful or -1 in case of error.
1717 */
1718int
1719xmlSaveSetAttrEscape(xmlSaveCtxtPtr ctxt, xmlCharEncodingOutputFunc escape)
1720{
1721 if (ctxt == NULL) return(-1);
1722 ctxt->escapeAttr = escape;
1723 return(0);
1724}
1725
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001726/************************************************************************
1727 * *
1728 * Public entry points based on buffers *
1729 * *
1730 ************************************************************************/
1731/**
1732 * xmlAttrSerializeTxtContent:
1733 * @buf: the XML buffer output
1734 * @doc: the document
1735 * @attr: the attribute node
1736 * @string: the text content
1737 *
1738 * Serialize text attribute values to an xml simple buffer
1739 */
1740void
1741xmlAttrSerializeTxtContent(xmlBufferPtr buf, xmlDocPtr doc,
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001742 xmlAttrPtr attr, const xmlChar * string)
1743{
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001744 xmlChar *base, *cur;
1745
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001746 if (string == NULL)
1747 return;
1748 base = cur = (xmlChar *) string;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001749 while (*cur != 0) {
1750 if (*cur == '\n') {
1751 if (base != cur)
1752 xmlBufferAdd(buf, base, cur - base);
1753 xmlBufferAdd(buf, BAD_CAST "&#10;", 5);
1754 cur++;
1755 base = cur;
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001756 } else if (*cur == '\r') {
1757 if (base != cur)
1758 xmlBufferAdd(buf, base, cur - base);
1759 xmlBufferAdd(buf, BAD_CAST "&#13;", 5);
1760 cur++;
1761 base = cur;
1762 } else if (*cur == '\t') {
1763 if (base != cur)
1764 xmlBufferAdd(buf, base, cur - base);
1765 xmlBufferAdd(buf, BAD_CAST "&#9;", 4);
1766 cur++;
1767 base = cur;
1768 } else if (*cur == '"') {
1769 if (base != cur)
1770 xmlBufferAdd(buf, base, cur - base);
1771 xmlBufferAdd(buf, BAD_CAST "&quot;", 6);
1772 cur++;
1773 base = cur;
1774 } else if (*cur == '<') {
1775 if (base != cur)
1776 xmlBufferAdd(buf, base, cur - base);
1777 xmlBufferAdd(buf, BAD_CAST "&lt;", 4);
1778 cur++;
1779 base = cur;
1780 } else if (*cur == '>') {
1781 if (base != cur)
1782 xmlBufferAdd(buf, base, cur - base);
1783 xmlBufferAdd(buf, BAD_CAST "&gt;", 4);
1784 cur++;
1785 base = cur;
1786 } else if (*cur == '&') {
1787 if (base != cur)
1788 xmlBufferAdd(buf, base, cur - base);
1789 xmlBufferAdd(buf, BAD_CAST "&amp;", 5);
1790 cur++;
1791 base = cur;
1792 } else if ((*cur >= 0x80) && ((doc == NULL) ||
1793 (doc->encoding == NULL))) {
1794 /*
1795 * We assume we have UTF-8 content.
1796 */
1797 unsigned char tmp[10];
1798 int val = 0, l = 1;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001799
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001800 if (base != cur)
1801 xmlBufferAdd(buf, base, cur - base);
1802 if (*cur < 0xC0) {
1803 xmlSaveErr(XML_SAVE_NOT_UTF8, (xmlNodePtr) attr, NULL);
1804 if (doc != NULL)
1805 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
1806 xmlSerializeHexCharRef(tmp, *cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001807 xmlBufferAdd(buf, (xmlChar *) tmp, -1);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001808 cur++;
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001809 base = cur;
1810 continue;
1811 } else if (*cur < 0xE0) {
1812 val = (cur[0]) & 0x1F;
1813 val <<= 6;
1814 val |= (cur[1]) & 0x3F;
1815 l = 2;
1816 } else if (*cur < 0xF0) {
1817 val = (cur[0]) & 0x0F;
1818 val <<= 6;
1819 val |= (cur[1]) & 0x3F;
1820 val <<= 6;
1821 val |= (cur[2]) & 0x3F;
1822 l = 3;
1823 } else if (*cur < 0xF8) {
1824 val = (cur[0]) & 0x07;
1825 val <<= 6;
1826 val |= (cur[1]) & 0x3F;
1827 val <<= 6;
1828 val |= (cur[2]) & 0x3F;
1829 val <<= 6;
1830 val |= (cur[3]) & 0x3F;
1831 l = 4;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001832 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001833 if ((l == 1) || (!IS_CHAR(val))) {
1834 xmlSaveErr(XML_SAVE_CHAR_INVALID, (xmlNodePtr) attr, NULL);
1835 if (doc != NULL)
1836 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
1837
1838 xmlSerializeHexCharRef(tmp, *cur);
1839 xmlBufferAdd(buf, (xmlChar *) tmp, -1);
1840 cur++;
1841 base = cur;
1842 continue;
1843 }
1844 /*
1845 * We could do multiple things here. Just save
1846 * as a char ref
1847 */
1848 xmlSerializeHexCharRef(tmp, val);
1849 xmlBufferAdd(buf, (xmlChar *) tmp, -1);
1850 cur += l;
1851 base = cur;
1852 } else {
1853 cur++;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001854 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001855 }
1856 if (base != cur)
1857 xmlBufferAdd(buf, base, cur - base);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001858}
1859
1860/**
1861 * xmlNodeDump:
1862 * @buf: the XML buffer output
1863 * @doc: the document
1864 * @cur: the current node
1865 * @level: the imbrication level for indenting
1866 * @format: is formatting allowed
1867 *
1868 * Dump an XML node, recursive behaviour,children are printed too.
1869 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
1870 * or xmlKeepBlanksDefault(0) was called
1871 *
1872 * Returns the number of bytes written to the buffer or -1 in case of error
1873 */
1874int
1875xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
1876 int format)
1877{
1878 unsigned int use;
1879 int ret;
1880 xmlOutputBufferPtr outbuf;
1881
1882 xmlInitParser();
1883
1884 if (cur == NULL) {
1885#ifdef DEBUG_TREE
1886 xmlGenericError(xmlGenericErrorContext,
1887 "xmlNodeDump : node == NULL\n");
1888#endif
1889 return (-1);
1890 }
1891 if (buf == NULL) {
1892#ifdef DEBUG_TREE
1893 xmlGenericError(xmlGenericErrorContext,
1894 "xmlNodeDump : buf == NULL\n");
1895#endif
1896 return (-1);
1897 }
1898 outbuf = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer));
1899 if (outbuf == NULL) {
1900 xmlSaveErrMemory("creating buffer");
1901 return (-1);
1902 }
1903 memset(outbuf, 0, (size_t) sizeof(xmlOutputBuffer));
1904 outbuf->buffer = buf;
1905 outbuf->encoder = NULL;
1906 outbuf->writecallback = NULL;
1907 outbuf->closecallback = NULL;
1908 outbuf->context = NULL;
1909 outbuf->written = 0;
1910
1911 use = buf->use;
1912 xmlNodeDumpOutput(outbuf, doc, cur, level, format, NULL);
1913 xmlFree(outbuf);
1914 ret = buf->use - use;
1915 return (ret);
1916}
1917
1918/**
1919 * xmlElemDump:
1920 * @f: the FILE * for the output
1921 * @doc: the document
1922 * @cur: the current node
1923 *
1924 * Dump an XML/HTML node, recursive behaviour, children are printed too.
1925 */
1926void
1927xmlElemDump(FILE * f, xmlDocPtr doc, xmlNodePtr cur)
1928{
1929 xmlOutputBufferPtr outbuf;
1930
1931 xmlInitParser();
1932
1933 if (cur == NULL) {
1934#ifdef DEBUG_TREE
1935 xmlGenericError(xmlGenericErrorContext,
1936 "xmlElemDump : cur == NULL\n");
1937#endif
1938 return;
1939 }
1940#ifdef DEBUG_TREE
1941 if (doc == NULL) {
1942 xmlGenericError(xmlGenericErrorContext,
1943 "xmlElemDump : doc == NULL\n");
1944 }
1945#endif
1946
1947 outbuf = xmlOutputBufferCreateFile(f, NULL);
1948 if (outbuf == NULL)
1949 return;
1950 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
1951#ifdef LIBXML_HTML_ENABLED
1952 htmlNodeDumpOutput(outbuf, doc, cur, NULL);
1953#else
1954 xmlSaveErr(XML_ERR_INTERNAL_ERROR, cur, "HTML support not compiled in\n");
1955#endif /* LIBXML_HTML_ENABLED */
1956 } else
1957 xmlNodeDumpOutput(outbuf, doc, cur, 0, 1, NULL);
1958 xmlOutputBufferClose(outbuf);
1959}
1960
1961/************************************************************************
1962 * *
1963 * Saving functions front-ends *
1964 * *
1965 ************************************************************************/
1966
1967/**
1968 * xmlNodeDumpOutput:
1969 * @buf: the XML buffer output
1970 * @doc: the document
1971 * @cur: the current node
1972 * @level: the imbrication level for indenting
1973 * @format: is formatting allowed
1974 * @encoding: an optional encoding string
1975 *
1976 * Dump an XML node, recursive behaviour, children are printed too.
1977 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
1978 * or xmlKeepBlanksDefault(0) was called
1979 */
1980void
1981xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
1982 int level, int format, const char *encoding)
1983{
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001984 xmlSaveCtxt ctxt;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001985#ifdef LIBXML_HTML_ENABLED
1986 xmlDtdPtr dtd;
1987 int is_xhtml = 0;
1988#endif
1989
1990 xmlInitParser();
1991
Daniel Veillardce244ad2004-11-05 10:03:46 +00001992 if ((buf == NULL) || (cur == NULL)) return;
1993
Daniel Veillard64354ea2005-03-31 15:22:56 +00001994 if (encoding == NULL)
1995 encoding = "UTF-8";
1996
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001997 memset(&ctxt, 0, sizeof(ctxt));
1998 ctxt.doc = doc;
1999 ctxt.buf = buf;
2000 ctxt.level = level;
2001 ctxt.format = format;
2002 ctxt.encoding = (const xmlChar *) encoding;
Daniel Veillard753086a2004-03-28 16:12:44 +00002003 xmlSaveCtxtInit(&ctxt);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002004
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002005#ifdef LIBXML_HTML_ENABLED
2006 dtd = xmlGetIntSubset(doc);
2007 if (dtd != NULL) {
Daniel Veillard33b20b72005-09-12 21:43:20 +00002008 is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
2009 if (is_xhtml < 0)
2010 is_xhtml = 0;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002011 }
2012
2013 if (is_xhtml)
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002014 xhtmlNodeDumpOutput(&ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002015 else
2016#endif
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002017 xmlNodeDumpOutputInternal(&ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002018}
2019
2020/**
2021 * xmlDocDumpFormatMemoryEnc:
2022 * @out_doc: Document to generate XML text from
2023 * @doc_txt_ptr: Memory pointer for allocated XML text
2024 * @doc_txt_len: Length of the generated XML text
2025 * @txt_encoding: Character encoding to use when generating XML text
2026 * @format: should formatting spaces been added
2027 *
2028 * Dump the current DOM tree into memory using the character encoding specified
2029 * by the caller. Note it is up to the caller of this function to free the
2030 * allocated memory with xmlFree().
2031 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
2032 * or xmlKeepBlanksDefault(0) was called
2033 */
2034
2035void
2036xmlDocDumpFormatMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
2037 int * doc_txt_len, const char * txt_encoding,
2038 int format) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002039 xmlSaveCtxt ctxt;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002040 int dummy = 0;
2041 xmlOutputBufferPtr out_buff = NULL;
2042 xmlCharEncodingHandlerPtr conv_hdlr = NULL;
2043
2044 if (doc_txt_len == NULL) {
2045 doc_txt_len = &dummy; /* Continue, caller just won't get length */
2046 }
2047
2048 if (doc_txt_ptr == NULL) {
2049 *doc_txt_len = 0;
2050 return;
2051 }
2052
2053 *doc_txt_ptr = NULL;
2054 *doc_txt_len = 0;
2055
2056 if (out_doc == NULL) {
2057 /* No document, no output */
2058 return;
2059 }
2060
2061 /*
2062 * Validate the encoding value, if provided.
2063 * This logic is copied from xmlSaveFileEnc.
2064 */
2065
2066 if (txt_encoding == NULL)
2067 txt_encoding = (const char *) out_doc->encoding;
2068 if (txt_encoding != NULL) {
2069 conv_hdlr = xmlFindCharEncodingHandler(txt_encoding);
2070 if ( conv_hdlr == NULL ) {
2071 xmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, (xmlNodePtr) out_doc,
2072 txt_encoding);
2073 return;
2074 }
2075 }
2076
2077 if ((out_buff = xmlAllocOutputBuffer(conv_hdlr)) == NULL ) {
2078 xmlSaveErrMemory("creating buffer");
2079 return;
2080 }
2081
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002082 memset(&ctxt, 0, sizeof(ctxt));
2083 ctxt.doc = out_doc;
2084 ctxt.buf = out_buff;
2085 ctxt.level = 0;
2086 ctxt.format = format;
2087 ctxt.encoding = (const xmlChar *) txt_encoding;
Daniel Veillard753086a2004-03-28 16:12:44 +00002088 xmlSaveCtxtInit(&ctxt);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002089 xmlDocContentDumpOutput(&ctxt, out_doc);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002090 xmlOutputBufferFlush(out_buff);
2091 if (out_buff->conv != NULL) {
2092 *doc_txt_len = out_buff->conv->use;
2093 *doc_txt_ptr = xmlStrndup(out_buff->conv->content, *doc_txt_len);
2094 } else {
2095 *doc_txt_len = out_buff->buffer->use;
2096 *doc_txt_ptr = xmlStrndup(out_buff->buffer->content, *doc_txt_len);
2097 }
2098 (void)xmlOutputBufferClose(out_buff);
2099
2100 if ((*doc_txt_ptr == NULL) && (*doc_txt_len > 0)) {
2101 *doc_txt_len = 0;
2102 xmlSaveErrMemory("creating output");
2103 }
2104
2105 return;
2106}
2107
2108/**
2109 * xmlDocDumpMemory:
2110 * @cur: the document
2111 * @mem: OUT: the memory pointer
2112 * @size: OUT: the memory length
2113 *
2114 * Dump an XML document in memory and return the #xmlChar * and it's size
2115 * in bytes. It's up to the caller to free the memory with xmlFree().
2116 * The resulting byte array is zero terminated, though the last 0 is not
2117 * included in the returned size.
2118 */
2119void
2120xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) {
2121 xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, 0);
2122}
2123
2124/**
2125 * xmlDocDumpFormatMemory:
2126 * @cur: the document
2127 * @mem: OUT: the memory pointer
2128 * @size: OUT: the memory length
2129 * @format: should formatting spaces been added
2130 *
2131 *
2132 * Dump an XML document in memory and return the #xmlChar * and it's size.
2133 * It's up to the caller to free the memory with xmlFree().
2134 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
2135 * or xmlKeepBlanksDefault(0) was called
2136 */
2137void
2138xmlDocDumpFormatMemory(xmlDocPtr cur, xmlChar**mem, int *size, int format) {
2139 xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, format);
2140}
2141
2142/**
2143 * xmlDocDumpMemoryEnc:
2144 * @out_doc: Document to generate XML text from
2145 * @doc_txt_ptr: Memory pointer for allocated XML text
2146 * @doc_txt_len: Length of the generated XML text
2147 * @txt_encoding: Character encoding to use when generating XML text
2148 *
2149 * Dump the current DOM tree into memory using the character encoding specified
2150 * by the caller. Note it is up to the caller of this function to free the
2151 * allocated memory with xmlFree().
2152 */
2153
2154void
2155xmlDocDumpMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
2156 int * doc_txt_len, const char * txt_encoding) {
2157 xmlDocDumpFormatMemoryEnc(out_doc, doc_txt_ptr, doc_txt_len,
2158 txt_encoding, 0);
2159}
2160
2161/**
2162 * xmlDocFormatDump:
2163 * @f: the FILE*
2164 * @cur: the document
2165 * @format: should formatting spaces been added
2166 *
2167 * Dump an XML document to an open FILE.
2168 *
2169 * returns: the number of bytes written or -1 in case of failure.
2170 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
2171 * or xmlKeepBlanksDefault(0) was called
2172 */
2173int
2174xmlDocFormatDump(FILE *f, xmlDocPtr cur, int format) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002175 xmlSaveCtxt ctxt;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002176 xmlOutputBufferPtr buf;
2177 const char * encoding;
2178 xmlCharEncodingHandlerPtr handler = NULL;
2179 int ret;
2180
2181 if (cur == NULL) {
2182#ifdef DEBUG_TREE
2183 xmlGenericError(xmlGenericErrorContext,
2184 "xmlDocDump : document == NULL\n");
2185#endif
2186 return(-1);
2187 }
2188 encoding = (const char *) cur->encoding;
2189
2190 if (encoding != NULL) {
2191 handler = xmlFindCharEncodingHandler(encoding);
2192 if (handler == NULL) {
2193 xmlFree((char *) cur->encoding);
2194 cur->encoding = NULL;
2195 }
2196 }
2197 buf = xmlOutputBufferCreateFile(f, handler);
2198 if (buf == NULL) return(-1);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002199 memset(&ctxt, 0, sizeof(ctxt));
2200 ctxt.doc = cur;
2201 ctxt.buf = buf;
2202 ctxt.level = 0;
2203 ctxt.format = format;
2204 ctxt.encoding = (const xmlChar *) encoding;
Daniel Veillard753086a2004-03-28 16:12:44 +00002205 xmlSaveCtxtInit(&ctxt);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002206 xmlDocContentDumpOutput(&ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002207
2208 ret = xmlOutputBufferClose(buf);
2209 return(ret);
2210}
2211
2212/**
2213 * xmlDocDump:
2214 * @f: the FILE*
2215 * @cur: the document
2216 *
2217 * Dump an XML document to an open FILE.
2218 *
2219 * returns: the number of bytes written or -1 in case of failure.
2220 */
2221int
2222xmlDocDump(FILE *f, xmlDocPtr cur) {
2223 return(xmlDocFormatDump (f, cur, 0));
2224}
2225
2226/**
2227 * xmlSaveFileTo:
2228 * @buf: an output I/O buffer
2229 * @cur: the document
2230 * @encoding: the encoding if any assuming the I/O layer handles the trancoding
2231 *
2232 * Dump an XML document to an I/O buffer.
Daniel Veillard3d97e662004-11-04 10:49:00 +00002233 * Warning ! This call xmlOutputBufferClose() on buf which is not available
2234 * after this call.
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002235 *
2236 * returns: the number of bytes written or -1 in case of failure.
2237 */
2238int
2239xmlSaveFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002240 xmlSaveCtxt ctxt;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002241 int ret;
2242
Daniel Veillard3d97e662004-11-04 10:49:00 +00002243 if (buf == NULL) return(-1);
2244 if (cur == NULL) {
2245 xmlOutputBufferClose(buf);
2246 return(-1);
2247 }
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002248 memset(&ctxt, 0, sizeof(ctxt));
2249 ctxt.doc = cur;
2250 ctxt.buf = buf;
2251 ctxt.level = 0;
2252 ctxt.format = 0;
2253 ctxt.encoding = (const xmlChar *) encoding;
Daniel Veillard753086a2004-03-28 16:12:44 +00002254 xmlSaveCtxtInit(&ctxt);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002255 xmlDocContentDumpOutput(&ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002256 ret = xmlOutputBufferClose(buf);
2257 return(ret);
2258}
2259
2260/**
2261 * xmlSaveFormatFileTo:
2262 * @buf: an output I/O buffer
2263 * @cur: the document
2264 * @encoding: the encoding if any assuming the I/O layer handles the trancoding
2265 * @format: should formatting spaces been added
2266 *
2267 * Dump an XML document to an I/O buffer.
Daniel Veillard3d97e662004-11-04 10:49:00 +00002268 * Warning ! This call xmlOutputBufferClose() on buf which is not available
2269 * after this call.
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002270 *
2271 * returns: the number of bytes written or -1 in case of failure.
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002272 */
2273int
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002274xmlSaveFormatFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur,
2275 const char *encoding, int format)
2276{
2277 xmlSaveCtxt ctxt;
2278 int ret;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002279
Daniel Veillard3d97e662004-11-04 10:49:00 +00002280 if (buf == NULL) return(-1);
Daniel Veillardce244ad2004-11-05 10:03:46 +00002281 if ((cur == NULL) ||
2282 ((cur->type != XML_DOCUMENT_NODE) &&
2283 (cur->type != XML_HTML_DOCUMENT_NODE))) {
Daniel Veillard3d97e662004-11-04 10:49:00 +00002284 xmlOutputBufferClose(buf);
2285 return(-1);
2286 }
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002287 memset(&ctxt, 0, sizeof(ctxt));
2288 ctxt.doc = cur;
2289 ctxt.buf = buf;
2290 ctxt.level = 0;
2291 ctxt.format = format;
2292 ctxt.encoding = (const xmlChar *) encoding;
Daniel Veillard753086a2004-03-28 16:12:44 +00002293 xmlSaveCtxtInit(&ctxt);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002294 xmlDocContentDumpOutput(&ctxt, cur);
2295 ret = xmlOutputBufferClose(buf);
2296 return (ret);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002297}
2298
2299/**
2300 * xmlSaveFormatFileEnc:
2301 * @filename: the filename or URL to output
2302 * @cur: the document being saved
2303 * @encoding: the name of the encoding to use or NULL.
2304 * @format: should formatting spaces be added.
2305 *
2306 * Dump an XML document to a file or an URL.
2307 *
2308 * Returns the number of bytes written or -1 in case of error.
2309 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
2310 * or xmlKeepBlanksDefault(0) was called
2311 */
2312int
2313xmlSaveFormatFileEnc( const char * filename, xmlDocPtr cur,
2314 const char * encoding, int format ) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002315 xmlSaveCtxt ctxt;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002316 xmlOutputBufferPtr buf;
2317 xmlCharEncodingHandlerPtr handler = NULL;
2318 int ret;
2319
2320 if (cur == NULL)
2321 return(-1);
2322
2323 if (encoding == NULL)
2324 encoding = (const char *) cur->encoding;
2325
2326 if (encoding != NULL) {
2327
2328 handler = xmlFindCharEncodingHandler(encoding);
2329 if (handler == NULL)
2330 return(-1);
2331 }
2332
2333#ifdef HAVE_ZLIB_H
2334 if (cur->compression < 0) cur->compression = xmlGetCompressMode();
2335#endif
2336 /*
2337 * save the content to a temp buffer.
2338 */
2339 buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression);
2340 if (buf == NULL) return(-1);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002341 memset(&ctxt, 0, sizeof(ctxt));
2342 ctxt.doc = cur;
2343 ctxt.buf = buf;
2344 ctxt.level = 0;
2345 ctxt.format = format;
2346 ctxt.encoding = (const xmlChar *) encoding;
Daniel Veillard753086a2004-03-28 16:12:44 +00002347 xmlSaveCtxtInit(&ctxt);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002348
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002349 xmlDocContentDumpOutput(&ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002350
2351 ret = xmlOutputBufferClose(buf);
2352 return(ret);
2353}
2354
2355
2356/**
2357 * xmlSaveFileEnc:
2358 * @filename: the filename (or URL)
2359 * @cur: the document
2360 * @encoding: the name of an encoding (or NULL)
2361 *
2362 * Dump an XML document, converting it to the given encoding
2363 *
2364 * returns: the number of bytes written or -1 in case of failure.
2365 */
2366int
2367xmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) {
2368 return ( xmlSaveFormatFileEnc( filename, cur, encoding, 0 ) );
2369}
2370
2371/**
2372 * xmlSaveFormatFile:
2373 * @filename: the filename (or URL)
2374 * @cur: the document
2375 * @format: should formatting spaces been added
2376 *
2377 * Dump an XML document to a file. Will use compression if
2378 * compiled in and enabled. If @filename is "-" the stdout file is
2379 * used. If @format is set then the document will be indented on output.
2380 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
2381 * or xmlKeepBlanksDefault(0) was called
2382 *
2383 * returns: the number of bytes written or -1 in case of failure.
2384 */
2385int
2386xmlSaveFormatFile(const char *filename, xmlDocPtr cur, int format) {
2387 return ( xmlSaveFormatFileEnc( filename, cur, NULL, format ) );
2388}
2389
2390/**
2391 * xmlSaveFile:
2392 * @filename: the filename (or URL)
2393 * @cur: the document
2394 *
2395 * Dump an XML document to a file. Will use compression if
2396 * compiled in and enabled. If @filename is "-" the stdout file is
2397 * used.
2398 * returns: the number of bytes written or -1 in case of failure.
2399 */
2400int
2401xmlSaveFile(const char *filename, xmlDocPtr cur) {
2402 return(xmlSaveFormatFileEnc(filename, cur, NULL, 0));
2403}
2404
2405#endif /* LIBXML_OUTPUT_ENABLED */
2406
Daniel Veillard5d4644e2005-04-01 13:11:58 +00002407#define bottom_xmlsave
2408#include "elfgcchack.h"