blob: 76027f6d647004b412daa7ce257d8e1877bddc8a [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 Veillard50cdab52012-07-16 14:52:00 +080022#include "buf.h"
23#include "enc.h"
24#include "save.h"
25
Daniel Veillard1a8741c2004-03-04 13:40:59 +000026/************************************************************************
27 * *
28 * XHTML detection *
29 * *
30 ************************************************************************/
31#define XHTML_STRICT_PUBLIC_ID BAD_CAST \
32 "-//W3C//DTD XHTML 1.0 Strict//EN"
33#define XHTML_STRICT_SYSTEM_ID BAD_CAST \
34 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
35#define XHTML_FRAME_PUBLIC_ID BAD_CAST \
36 "-//W3C//DTD XHTML 1.0 Frameset//EN"
37#define XHTML_FRAME_SYSTEM_ID BAD_CAST \
38 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"
39#define XHTML_TRANS_PUBLIC_ID BAD_CAST \
40 "-//W3C//DTD XHTML 1.0 Transitional//EN"
41#define XHTML_TRANS_SYSTEM_ID BAD_CAST \
42 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
43
44#define XHTML_NS_NAME BAD_CAST "http://www.w3.org/1999/xhtml"
45/**
46 * xmlIsXHTML:
47 * @systemID: the system identifier
48 * @publicID: the public identifier
49 *
50 * Try to find if the document correspond to an XHTML DTD
51 *
52 * Returns 1 if true, 0 if not and -1 in case of error
53 */
54int
55xmlIsXHTML(const xmlChar *systemID, const xmlChar *publicID) {
56 if ((systemID == NULL) && (publicID == NULL))
57 return(-1);
58 if (publicID != NULL) {
59 if (xmlStrEqual(publicID, XHTML_STRICT_PUBLIC_ID)) return(1);
60 if (xmlStrEqual(publicID, XHTML_FRAME_PUBLIC_ID)) return(1);
61 if (xmlStrEqual(publicID, XHTML_TRANS_PUBLIC_ID)) return(1);
62 }
63 if (systemID != NULL) {
64 if (xmlStrEqual(systemID, XHTML_STRICT_SYSTEM_ID)) return(1);
65 if (xmlStrEqual(systemID, XHTML_FRAME_SYSTEM_ID)) return(1);
66 if (xmlStrEqual(systemID, XHTML_TRANS_SYSTEM_ID)) return(1);
67 }
68 return(0);
69}
Daniel Veillard1a8741c2004-03-04 13:40:59 +000070
71#ifdef LIBXML_OUTPUT_ENABLED
72
73#define TODO \
74 xmlGenericError(xmlGenericErrorContext, \
75 "Unimplemented block at %s:%d\n", \
76 __FILE__, __LINE__);
77
78struct _xmlSaveCtxt {
79 void *_private;
80 int type;
81 int fd;
82 const xmlChar *filename;
83 const xmlChar *encoding;
84 xmlCharEncodingHandlerPtr handler;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +000085 xmlOutputBufferPtr buf;
86 xmlDocPtr doc;
Daniel Veillard1a8741c2004-03-04 13:40:59 +000087 int options;
88 int level;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +000089 int format;
Daniel Veillard3995bc32004-05-15 18:57:31 +000090 char indent[MAX_INDENT + 1]; /* array for indenting output */
Daniel Veillard753086a2004-03-28 16:12:44 +000091 int indent_nr;
92 int indent_size;
Daniel Veillard3995bc32004-05-15 18:57:31 +000093 xmlCharEncodingOutputFunc escape; /* used for element content */
94 xmlCharEncodingOutputFunc escapeAttr;/* used for attribute content */
Daniel Veillard1a8741c2004-03-04 13:40:59 +000095};
96
97/************************************************************************
98 * *
99 * Output error handlers *
100 * *
101 ************************************************************************/
102/**
103 * xmlSaveErrMemory:
104 * @extra: extra informations
105 *
106 * Handle an out of memory condition
107 */
108static void
109xmlSaveErrMemory(const char *extra)
110{
111 __xmlSimpleError(XML_FROM_OUTPUT, XML_ERR_NO_MEMORY, NULL, NULL, extra);
112}
113
114/**
115 * xmlSaveErr:
116 * @code: the error number
117 * @node: the location of the error.
118 * @extra: extra informations
119 *
120 * Handle an out of memory condition
121 */
122static void
123xmlSaveErr(int code, xmlNodePtr node, const char *extra)
124{
125 const char *msg = NULL;
126
127 switch(code) {
128 case XML_SAVE_NOT_UTF8:
Rob Richards417b74d2006-08-15 23:14:24 +0000129 msg = "string is not in UTF-8\n";
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000130 break;
131 case XML_SAVE_CHAR_INVALID:
Rob Richards417b74d2006-08-15 23:14:24 +0000132 msg = "invalid character value\n";
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000133 break;
134 case XML_SAVE_UNKNOWN_ENCODING:
Rob Richards417b74d2006-08-15 23:14:24 +0000135 msg = "unknown encoding %s\n";
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000136 break;
137 case XML_SAVE_NO_DOCTYPE:
Rob Richards417b74d2006-08-15 23:14:24 +0000138 msg = "document has no DOCTYPE\n";
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000139 break;
140 default:
Rob Richards417b74d2006-08-15 23:14:24 +0000141 msg = "unexpected error number\n";
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000142 }
143 __xmlSimpleError(XML_FROM_OUTPUT, code, node, msg, extra);
144}
145
146/************************************************************************
147 * *
Daniel Veillard83a75e02004-05-14 21:50:42 +0000148 * Special escaping routines *
149 * *
150 ************************************************************************/
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000151static unsigned char *
152xmlSerializeHexCharRef(unsigned char *out, int val) {
153 unsigned char *ptr;
154
155 *out++ = '&';
156 *out++ = '#';
157 *out++ = 'x';
158 if (val < 0x10) ptr = out;
159 else if (val < 0x100) ptr = out + 1;
160 else if (val < 0x1000) ptr = out + 2;
161 else if (val < 0x10000) ptr = out + 3;
162 else if (val < 0x100000) ptr = out + 4;
163 else ptr = out + 5;
164 out = ptr + 1;
165 while (val > 0) {
166 switch (val & 0xF) {
167 case 0: *ptr-- = '0'; break;
168 case 1: *ptr-- = '1'; break;
169 case 2: *ptr-- = '2'; break;
170 case 3: *ptr-- = '3'; break;
171 case 4: *ptr-- = '4'; break;
172 case 5: *ptr-- = '5'; break;
173 case 6: *ptr-- = '6'; break;
174 case 7: *ptr-- = '7'; break;
175 case 8: *ptr-- = '8'; break;
176 case 9: *ptr-- = '9'; break;
177 case 0xA: *ptr-- = 'A'; break;
178 case 0xB: *ptr-- = 'B'; break;
179 case 0xC: *ptr-- = 'C'; break;
180 case 0xD: *ptr-- = 'D'; break;
181 case 0xE: *ptr-- = 'E'; break;
182 case 0xF: *ptr-- = 'F'; break;
183 default: *ptr-- = '0'; break;
184 }
185 val >>= 4;
186 }
187 *out++ = ';';
188 *out = 0;
189 return(out);
190}
191
Daniel Veillard83a75e02004-05-14 21:50:42 +0000192/**
193 * xmlEscapeEntities:
194 * @out: a pointer to an array of bytes to store the result
195 * @outlen: the length of @out
196 * @in: a pointer to an array of unescaped UTF-8 bytes
197 * @inlen: the length of @in
198 *
199 * Take a block of UTF-8 chars in and escape them. Used when there is no
200 * encoding specified.
201 *
202 * Returns 0 if success, or -1 otherwise
203 * The value of @inlen after return is the number of octets consumed
204 * if the return value is positive, else unpredictable.
205 * The value of @outlen after return is the number of octets consumed.
206 */
207static int
208xmlEscapeEntities(unsigned char* out, int *outlen,
209 const xmlChar* in, int *inlen) {
210 unsigned char* outstart = out;
211 const unsigned char* base = in;
212 unsigned char* outend = out + *outlen;
213 const unsigned char* inend;
214 int val;
215
216 inend = in + (*inlen);
217
218 while ((in < inend) && (out < outend)) {
219 if (*in == '<') {
220 if (outend - out < 4) break;
221 *out++ = '&';
222 *out++ = 'l';
223 *out++ = 't';
224 *out++ = ';';
225 in++;
226 continue;
227 } else if (*in == '>') {
228 if (outend - out < 4) break;
229 *out++ = '&';
230 *out++ = 'g';
231 *out++ = 't';
232 *out++ = ';';
233 in++;
234 continue;
235 } else if (*in == '&') {
236 if (outend - out < 5) break;
237 *out++ = '&';
238 *out++ = 'a';
239 *out++ = 'm';
240 *out++ = 'p';
241 *out++ = ';';
242 in++;
243 continue;
244 } else if (((*in >= 0x20) && (*in < 0x80)) ||
245 (*in == '\n') || (*in == '\t')) {
246 /*
247 * default case, just copy !
248 */
249 *out++ = *in++;
250 continue;
251 } else if (*in >= 0x80) {
252 /*
253 * We assume we have UTF-8 input.
254 */
Daniel Veillard07953482012-01-22 17:42:35 +0800255 if (outend - out < 11) break;
Daniel Veillard83a75e02004-05-14 21:50:42 +0000256
257 if (*in < 0xC0) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000258 xmlSaveErr(XML_SAVE_NOT_UTF8, NULL, NULL);
Daniel Veillard83a75e02004-05-14 21:50:42 +0000259 in++;
260 goto error;
261 } else if (*in < 0xE0) {
262 if (inend - in < 2) break;
263 val = (in[0]) & 0x1F;
264 val <<= 6;
265 val |= (in[1]) & 0x3F;
266 in += 2;
267 } else if (*in < 0xF0) {
268 if (inend - in < 3) break;
269 val = (in[0]) & 0x0F;
270 val <<= 6;
271 val |= (in[1]) & 0x3F;
272 val <<= 6;
273 val |= (in[2]) & 0x3F;
274 in += 3;
275 } else if (*in < 0xF8) {
276 if (inend - in < 4) break;
277 val = (in[0]) & 0x07;
278 val <<= 6;
279 val |= (in[1]) & 0x3F;
280 val <<= 6;
281 val |= (in[2]) & 0x3F;
282 val <<= 6;
283 val |= (in[3]) & 0x3F;
284 in += 4;
285 } else {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000286 xmlSaveErr(XML_SAVE_CHAR_INVALID, NULL, NULL);
Daniel Veillard83a75e02004-05-14 21:50:42 +0000287 in++;
288 goto error;
289 }
290 if (!IS_CHAR(val)) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000291 xmlSaveErr(XML_SAVE_CHAR_INVALID, NULL, NULL);
Daniel Veillard83a75e02004-05-14 21:50:42 +0000292 in++;
293 goto error;
294 }
295
296 /*
297 * We could do multiple things here. Just save as a char ref
298 */
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000299 out = xmlSerializeHexCharRef(out, val);
Daniel Veillard83a75e02004-05-14 21:50:42 +0000300 } else if (IS_BYTE_CHAR(*in)) {
301 if (outend - out < 6) break;
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000302 out = xmlSerializeHexCharRef(out, *in++);
Daniel Veillard83a75e02004-05-14 21:50:42 +0000303 } else {
304 xmlGenericError(xmlGenericErrorContext,
305 "xmlEscapeEntities : char out of range\n");
306 in++;
307 goto error;
308 }
309 }
310 *outlen = out - outstart;
311 *inlen = in - base;
312 return(0);
313error:
314 *outlen = out - outstart;
315 *inlen = in - base;
316 return(-1);
317}
318
319/************************************************************************
320 * *
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000321 * Allocation and deallocation *
322 * *
323 ************************************************************************/
Daniel Veillard753086a2004-03-28 16:12:44 +0000324/**
325 * xmlSaveCtxtInit:
326 * @ctxt: the saving context
327 *
328 * Initialize a saving context
329 */
330static void
331xmlSaveCtxtInit(xmlSaveCtxtPtr ctxt)
332{
333 int i;
William M. Brack12d37ab2005-02-21 13:54:07 +0000334 int len;
Daniel Veillard753086a2004-03-28 16:12:44 +0000335
336 if (ctxt == NULL) return;
Daniel Veillard3995bc32004-05-15 18:57:31 +0000337 if ((ctxt->encoding == NULL) && (ctxt->escape == NULL))
338 ctxt->escape = xmlEscapeEntities;
William M. Brack12d37ab2005-02-21 13:54:07 +0000339 len = xmlStrlen((xmlChar *)xmlTreeIndentString);
340 if ((xmlTreeIndentString == NULL) || (len == 0)) {
Daniel Veillard753086a2004-03-28 16:12:44 +0000341 memset(&ctxt->indent[0], 0, MAX_INDENT + 1);
342 } else {
William M. Brack12d37ab2005-02-21 13:54:07 +0000343 ctxt->indent_size = len;
Daniel Veillard753086a2004-03-28 16:12:44 +0000344 ctxt->indent_nr = MAX_INDENT / ctxt->indent_size;
345 for (i = 0;i < ctxt->indent_nr;i++)
346 memcpy(&ctxt->indent[i * ctxt->indent_size], xmlTreeIndentString,
347 ctxt->indent_size);
348 ctxt->indent[ctxt->indent_nr * ctxt->indent_size] = 0;
349 }
Rob Richards2ce51c02005-09-12 12:16:35 +0000350
Daniel Veillard9a00fd22005-11-09 08:56:26 +0000351 if (xmlSaveNoEmptyTags) {
352 ctxt->options |= XML_SAVE_NO_EMPTY;
353 }
Daniel Veillard753086a2004-03-28 16:12:44 +0000354}
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000355
356/**
357 * xmlFreeSaveCtxt:
358 *
359 * Free a saving context, destroying the ouptut in any remaining buffer
360 */
361static void
362xmlFreeSaveCtxt(xmlSaveCtxtPtr ctxt)
363{
364 if (ctxt == NULL) return;
365 if (ctxt->encoding != NULL)
366 xmlFree((char *) ctxt->encoding);
Daniel Veillarde2161a62004-04-29 17:14:25 +0000367 if (ctxt->buf != NULL)
368 xmlOutputBufferClose(ctxt->buf);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000369 xmlFree(ctxt);
370}
371
372/**
373 * xmlNewSaveCtxt:
374 *
375 * Create a new saving context
376 *
377 * Returns the new structure or NULL in case of error
378 */
379static xmlSaveCtxtPtr
380xmlNewSaveCtxt(const char *encoding, int options)
381{
382 xmlSaveCtxtPtr ret;
383
384 ret = (xmlSaveCtxtPtr) xmlMalloc(sizeof(xmlSaveCtxt));
385 if (ret == NULL) {
386 xmlSaveErrMemory("creating saving context");
387 return ( NULL );
388 }
389 memset(ret, 0, sizeof(xmlSaveCtxt));
Daniel Veillard6fc5db02005-01-16 00:05:58 +0000390
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000391 if (encoding != NULL) {
392 ret->handler = xmlFindCharEncodingHandler(encoding);
393 if (ret->handler == NULL) {
394 xmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL, encoding);
395 xmlFreeSaveCtxt(ret);
396 return(NULL);
397 }
398 ret->encoding = xmlStrdup((const xmlChar *)encoding);
Daniel Veillarddab39b52006-10-16 23:22:10 +0000399 ret->escape = NULL;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000400 }
Daniel Veillard753086a2004-03-28 16:12:44 +0000401 xmlSaveCtxtInit(ret);
402
Rob Richards2ce51c02005-09-12 12:16:35 +0000403 /*
404 * Use the options
405 */
406
Daniel Veillard9a00fd22005-11-09 08:56:26 +0000407 /* Re-check this option as it may already have been set */
408 if ((ret->options & XML_SAVE_NO_EMPTY) && ! (options & XML_SAVE_NO_EMPTY)) {
409 options |= XML_SAVE_NO_EMPTY;
410 }
Rob Richards2ce51c02005-09-12 12:16:35 +0000411
412 ret->options = options;
413 if (options & XML_SAVE_FORMAT)
414 ret->format = 1;
Adam Spraggd2e62312010-11-03 15:33:40 +0100415 else if (options & XML_SAVE_WSNONSIG)
416 ret->format = 2;
Rob Richards2ce51c02005-09-12 12:16:35 +0000417
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000418 return(ret);
419}
420
421/************************************************************************
422 * *
423 * Dumping XML tree content to a simple buffer *
424 * *
425 ************************************************************************/
426/**
427 * xmlAttrSerializeContent:
428 * @buf: the XML buffer output
429 * @doc: the document
430 * @attr: the attribute pointer
431 *
432 * Serialize the attribute in the buffer
433 */
434static void
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000435xmlAttrSerializeContent(xmlOutputBufferPtr buf, xmlAttrPtr attr)
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000436{
437 xmlNodePtr children;
438
439 children = attr->children;
440 while (children != NULL) {
441 switch (children->type) {
442 case XML_TEXT_NODE:
Daniel Veillard50cdab52012-07-16 14:52:00 +0800443 xmlBufAttrSerializeTxtContent(buf->buffer, attr->doc,
444 attr, children->content);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000445 break;
446 case XML_ENTITY_REF_NODE:
Daniel Veillard50cdab52012-07-16 14:52:00 +0800447 xmlBufAdd(buf->buffer, BAD_CAST "&", 1);
448 xmlBufAdd(buf->buffer, children->name,
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000449 xmlStrlen(children->name));
Daniel Veillard50cdab52012-07-16 14:52:00 +0800450 xmlBufAdd(buf->buffer, BAD_CAST ";", 1);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000451 break;
452 default:
453 /* should not happen unless we have a badly built tree */
454 break;
455 }
456 children = children->next;
457 }
458}
459
Daniel Veillard50cdab52012-07-16 14:52:00 +0800460/**
461 * xmlBufDumpNotationTable:
462 * @buf: an xmlBufPtr output
463 * @table: A notation table
464 *
465 * This will dump the content of the notation table as an XML DTD definition
466 */
467void
468xmlBufDumpNotationTable(xmlBufPtr buf, xmlNotationTablePtr table) {
469 xmlBufferPtr buffer;
470
471 buffer = xmlBufferCreate();
472 if (buffer == NULL) {
473 /*
474 * TODO set the error in buf
475 */
476 return;
477 }
478 xmlDumpNotationTable(buffer, table);
479 xmlBufMergeBuffer(buf, buffer);
480}
481
482/**
483 * xmlBufDumpElementDecl:
484 * @buf: an xmlBufPtr output
485 * @elem: An element table
486 *
487 * This will dump the content of the element declaration as an XML
488 * DTD definition
489 */
490void
491xmlBufDumpElementDecl(xmlBufPtr buf, xmlElementPtr elem) {
492 xmlBufferPtr buffer;
493
494 buffer = xmlBufferCreate();
495 if (buffer == NULL) {
496 /*
497 * TODO set the error in buf
498 */
499 return;
500 }
501 xmlDumpElementDecl(buffer, elem);
502 xmlBufMergeBuffer(buf, buffer);
503}
504
505/**
506 * xmlBufDumpAttributeDecl:
507 * @buf: an xmlBufPtr output
508 * @attr: An attribute declaration
509 *
510 * This will dump the content of the attribute declaration as an XML
511 * DTD definition
512 */
513void
514xmlBufDumpAttributeDecl(xmlBufPtr buf, xmlAttributePtr attr) {
515 xmlBufferPtr buffer;
516
517 buffer = xmlBufferCreate();
518 if (buffer == NULL) {
519 /*
520 * TODO set the error in buf
521 */
522 return;
523 }
524 xmlDumpAttributeDecl(buffer, attr);
525 xmlBufMergeBuffer(buf, buffer);
526}
527
528/**
529 * xmlBufDumpEntityDecl:
530 * @buf: an xmlBufPtr output
531 * @ent: An entity table
532 *
533 * This will dump the content of the entity table as an XML DTD definition
534 */
535void
536xmlBufDumpEntityDecl(xmlBufPtr buf, xmlEntityPtr ent) {
537 xmlBufferPtr buffer;
538
539 buffer = xmlBufferCreate();
540 if (buffer == NULL) {
541 /*
542 * TODO set the error in buf
543 */
544 return;
545 }
546 xmlDumpEntityDecl(buffer, ent);
547 xmlBufMergeBuffer(buf, buffer);
548}
549
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000550/************************************************************************
551 * *
552 * Dumping XML tree content to an I/O output buffer *
553 * *
554 ************************************************************************/
555
Daniel Veillardda3fee42008-09-01 13:08:57 +0000556static int xmlSaveSwitchEncoding(xmlSaveCtxtPtr ctxt, const char *encoding) {
557 xmlOutputBufferPtr buf = ctxt->buf;
558
559 if ((encoding != NULL) && (buf->encoder == NULL) && (buf->conv == NULL)) {
560 buf->encoder = xmlFindCharEncodingHandler((const char *)encoding);
561 if (buf->encoder == NULL) {
562 xmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL,
563 (const char *)encoding);
564 return(-1);
565 }
Daniel Veillard50cdab52012-07-16 14:52:00 +0800566 buf->conv = xmlBufCreate();
Daniel Veillardda3fee42008-09-01 13:08:57 +0000567 if (buf->conv == NULL) {
568 xmlCharEncCloseFunc(buf->encoder);
569 xmlSaveErrMemory("creating encoding buffer");
570 return(-1);
571 }
572 /*
573 * initialize the state, e.g. if outputting a BOM
574 */
Daniel Veillard50cdab52012-07-16 14:52:00 +0800575 xmlCharEncOutput(buf, 1);
Daniel Veillardda3fee42008-09-01 13:08:57 +0000576 }
577 return(0);
578}
579
580static int xmlSaveClearEncoding(xmlSaveCtxtPtr ctxt) {
581 xmlOutputBufferPtr buf = ctxt->buf;
582 xmlOutputBufferFlush(buf);
583 xmlCharEncCloseFunc(buf->encoder);
Daniel Veillard50cdab52012-07-16 14:52:00 +0800584 xmlBufFree(buf->conv);
Daniel Veillardda3fee42008-09-01 13:08:57 +0000585 buf->encoder = NULL;
586 buf->conv = NULL;
587 return(0);
588}
589
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000590#ifdef LIBXML_HTML_ENABLED
591static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000592xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000593#endif
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000594static void xmlNodeListDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur);
595static void xmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000596void xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur);
Daniel Veillarddab39b52006-10-16 23:22:10 +0000597static int xmlDocContentDumpOutput(xmlSaveCtxtPtr ctxt, xmlDocPtr cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000598
599/**
Adam Spraggd2e62312010-11-03 15:33:40 +0100600 * xmlOutputBufferWriteWSNonSig:
601 * @ctxt: The save context
602 * @extra: Number of extra indents to apply to ctxt->level
603 *
604 * Write out formatting for non-significant whitespace output.
605 */
606static void
607xmlOutputBufferWriteWSNonSig(xmlSaveCtxtPtr ctxt, int extra)
608{
609 int i;
610 if ((ctxt == NULL) || (ctxt->buf == NULL))
611 return;
612 xmlOutputBufferWrite(ctxt->buf, 1, "\n");
613 for (i = 0; i < (ctxt->level + extra); i += ctxt->indent_nr) {
614 xmlOutputBufferWrite(ctxt->buf, ctxt->indent_size *
615 ((ctxt->level + extra - i) > ctxt->indent_nr ?
616 ctxt->indent_nr : (ctxt->level + extra - i)),
617 ctxt->indent);
618 }
619}
620
621/**
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000622 * xmlNsDumpOutput:
623 * @buf: the XML buffer output
624 * @cur: a namespace
Adam Spraggd2e62312010-11-03 15:33:40 +0100625 * @ctxt: the output save context. Optional.
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000626 *
627 * Dump a local Namespace definition.
628 * Should be called in the context of attributes dumps.
Adam Spraggd2e62312010-11-03 15:33:40 +0100629 * If @ctxt is supplied, @buf should be its buffer.
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000630 */
631static void
Adam Spraggd2e62312010-11-03 15:33:40 +0100632xmlNsDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur, xmlSaveCtxtPtr ctxt) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000633 if ((cur == NULL) || (buf == NULL)) return;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000634 if ((cur->type == XML_LOCAL_NAMESPACE) && (cur->href != NULL)) {
635 if (xmlStrEqual(cur->prefix, BAD_CAST "xml"))
636 return;
637
Adam Spraggd2e62312010-11-03 15:33:40 +0100638 if (ctxt != NULL && ctxt->format == 2)
639 xmlOutputBufferWriteWSNonSig(ctxt, 2);
640 else
641 xmlOutputBufferWrite(buf, 1, " ");
642
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000643 /* Within the context of an element attributes */
644 if (cur->prefix != NULL) {
Adam Spraggd2e62312010-11-03 15:33:40 +0100645 xmlOutputBufferWrite(buf, 6, "xmlns:");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000646 xmlOutputBufferWriteString(buf, (const char *)cur->prefix);
647 } else
Adam Spraggd2e62312010-11-03 15:33:40 +0100648 xmlOutputBufferWrite(buf, 5, "xmlns");
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000649 xmlOutputBufferWrite(buf, 1, "=");
Daniel Veillard50cdab52012-07-16 14:52:00 +0800650 xmlBufWriteQuotedString(buf->buffer, cur->href);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000651 }
652}
653
654/**
Adam Spraggd2e62312010-11-03 15:33:40 +0100655 * xmlNsDumpOutputCtxt
656 * @ctxt: the save context
657 * @cur: a namespace
658 *
659 * Dump a local Namespace definition to a save context.
660 * Should be called in the context of attribute dumps.
661 */
662static void
663xmlNsDumpOutputCtxt(xmlSaveCtxtPtr ctxt, xmlNsPtr cur) {
664 xmlNsDumpOutput(ctxt->buf, cur, ctxt);
665}
666
667/**
668 * xmlNsListDumpOutputCtxt
669 * @ctxt: the save context
670 * @cur: the first namespace
671 *
672 * Dump a list of local namespace definitions to a save context.
673 * Should be called in the context of attribute dumps.
674 */
675static void
676xmlNsListDumpOutputCtxt(xmlSaveCtxtPtr ctxt, xmlNsPtr cur) {
677 while (cur != NULL) {
678 xmlNsDumpOutput(ctxt->buf, cur, ctxt);
679 cur = cur->next;
680 }
681}
682
683/**
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000684 * xmlNsListDumpOutput:
685 * @buf: the XML buffer output
686 * @cur: the first namespace
687 *
688 * Dump a list of local Namespace definitions.
689 * Should be called in the context of attributes dumps.
690 */
691void
692xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) {
693 while (cur != NULL) {
Adam Spraggd2e62312010-11-03 15:33:40 +0100694 xmlNsDumpOutput(buf, cur, NULL);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000695 cur = cur->next;
696 }
697}
698
699/**
700 * xmlDtdDumpOutput:
701 * @buf: the XML buffer output
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000702 * @dtd: the pointer to the DTD
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000703 *
704 * Dump the XML document DTD, if any.
705 */
706static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000707xmlDtdDumpOutput(xmlSaveCtxtPtr ctxt, xmlDtdPtr dtd) {
708 xmlOutputBufferPtr buf;
709 int format, level;
710 xmlDocPtr doc;
711
712 if (dtd == NULL) return;
713 if ((ctxt == NULL) || (ctxt->buf == NULL))
714 return;
715 buf = ctxt->buf;
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000716 xmlOutputBufferWrite(buf, 10, "<!DOCTYPE ");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000717 xmlOutputBufferWriteString(buf, (const char *)dtd->name);
718 if (dtd->ExternalID != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000719 xmlOutputBufferWrite(buf, 8, " PUBLIC ");
Daniel Veillard50cdab52012-07-16 14:52:00 +0800720 xmlBufWriteQuotedString(buf->buffer, dtd->ExternalID);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000721 xmlOutputBufferWrite(buf, 1, " ");
Daniel Veillard50cdab52012-07-16 14:52:00 +0800722 xmlBufWriteQuotedString(buf->buffer, dtd->SystemID);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000723 } else if (dtd->SystemID != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000724 xmlOutputBufferWrite(buf, 8, " SYSTEM ");
Daniel Veillard50cdab52012-07-16 14:52:00 +0800725 xmlBufWriteQuotedString(buf->buffer, dtd->SystemID);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000726 }
727 if ((dtd->entities == NULL) && (dtd->elements == NULL) &&
Daniel Veillard41c4a752004-09-08 20:55:38 +0000728 (dtd->attributes == NULL) && (dtd->notations == NULL) &&
729 (dtd->pentities == NULL)) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000730 xmlOutputBufferWrite(buf, 1, ">");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000731 return;
732 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000733 xmlOutputBufferWrite(buf, 3, " [\n");
Daniel Veillard41c4a752004-09-08 20:55:38 +0000734 /*
735 * Dump the notations first they are not in the DTD children list
736 * Do this only on a standalone DTD or on the internal subset though.
737 */
738 if ((dtd->notations != NULL) && ((dtd->doc == NULL) ||
739 (dtd->doc->intSubset == dtd))) {
Daniel Veillard50cdab52012-07-16 14:52:00 +0800740 xmlBufDumpNotationTable(buf->buffer,
741 (xmlNotationTablePtr) dtd->notations);
Daniel Veillardda3b29a2004-08-14 11:15:13 +0000742 }
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000743 format = ctxt->format;
744 level = ctxt->level;
745 doc = ctxt->doc;
746 ctxt->format = 0;
747 ctxt->level = -1;
748 ctxt->doc = dtd->doc;
749 xmlNodeListDumpOutput(ctxt, dtd->children);
750 ctxt->format = format;
751 ctxt->level = level;
752 ctxt->doc = doc;
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000753 xmlOutputBufferWrite(buf, 2, "]>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000754}
755
756/**
757 * xmlAttrDumpOutput:
758 * @buf: the XML buffer output
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000759 * @cur: the attribute pointer
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000760 *
761 * Dump an XML attribute
762 */
763static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000764xmlAttrDumpOutput(xmlSaveCtxtPtr ctxt, xmlAttrPtr cur) {
765 xmlOutputBufferPtr buf;
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000766
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000767 if (cur == NULL) return;
768 buf = ctxt->buf;
Daniel Veillardce244ad2004-11-05 10:03:46 +0000769 if (buf == NULL) return;
Adam Spraggd2e62312010-11-03 15:33:40 +0100770 if (ctxt->format == 2)
771 xmlOutputBufferWriteWSNonSig(ctxt, 2);
772 else
773 xmlOutputBufferWrite(buf, 1, " ");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000774 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
775 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000776 xmlOutputBufferWrite(buf, 1, ":");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000777 }
778 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000779 xmlOutputBufferWrite(buf, 2, "=\"");
780 xmlAttrSerializeContent(buf, cur);
781 xmlOutputBufferWrite(buf, 1, "\"");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000782}
783
784/**
785 * xmlAttrListDumpOutput:
786 * @buf: the XML buffer output
787 * @doc: the document
788 * @cur: the first attribute pointer
789 * @encoding: an optional encoding string
790 *
791 * Dump a list of XML attributes
792 */
793static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000794xmlAttrListDumpOutput(xmlSaveCtxtPtr ctxt, xmlAttrPtr cur) {
795 if (cur == NULL) return;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000796 while (cur != NULL) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000797 xmlAttrDumpOutput(ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000798 cur = cur->next;
799 }
800}
801
802
803
804/**
805 * xmlNodeListDumpOutput:
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000806 * @cur: the first node
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000807 *
808 * Dump an XML node list, recursive behaviour, children are printed too.
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000809 */
810static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000811xmlNodeListDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000812 xmlOutputBufferPtr buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000813
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000814 if (cur == NULL) return;
815 buf = ctxt->buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000816 while (cur != NULL) {
Adam Spragg8b877132010-11-01 14:24:56 +0100817 if ((ctxt->format == 1) && (xmlIndentTreeOutput) &&
Daniel Veillardbd444842007-03-20 08:47:29 +0000818 ((cur->type == XML_ELEMENT_NODE) ||
819 (cur->type == XML_COMMENT_NODE) ||
820 (cur->type == XML_PI_NODE)))
Daniel Veillard753086a2004-03-28 16:12:44 +0000821 xmlOutputBufferWrite(buf, ctxt->indent_size *
822 (ctxt->level > ctxt->indent_nr ?
823 ctxt->indent_nr : ctxt->level),
824 ctxt->indent);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000825 xmlNodeDumpOutputInternal(ctxt, cur);
Adam Spragg8b877132010-11-01 14:24:56 +0100826 if (ctxt->format == 1) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000827 xmlOutputBufferWrite(buf, 1, "\n");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000828 }
829 cur = cur->next;
830 }
831}
832
Daniel Veillardda3fee42008-09-01 13:08:57 +0000833#ifdef LIBXML_HTML_ENABLED
834/**
835 * xmlNodeDumpOutputInternal:
836 * @cur: the current node
837 *
838 * Dump an HTML node, recursive behaviour, children are printed too.
839 */
840static int
841htmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
842 const xmlChar *oldenc = NULL;
843 const xmlChar *oldctxtenc = ctxt->encoding;
844 const xmlChar *encoding = ctxt->encoding;
845 xmlOutputBufferPtr buf = ctxt->buf;
846 int switched_encoding = 0;
847 xmlDocPtr doc;
848
849 xmlInitParser();
850
Daniel Veillard141ebfa2009-09-02 14:58:13 +0200851 doc = cur->doc;
852 if (doc != NULL) {
Daniel Veillardda3fee42008-09-01 13:08:57 +0000853 oldenc = doc->encoding;
854 if (ctxt->encoding != NULL) {
855 doc->encoding = BAD_CAST ctxt->encoding;
856 } else if (doc->encoding != NULL) {
857 encoding = doc->encoding;
858 }
859 }
860
861 if ((encoding != NULL) && (doc != NULL))
862 htmlSetMetaEncoding(doc, (const xmlChar *) encoding);
863 if ((encoding == NULL) && (doc != NULL))
864 encoding = htmlGetMetaEncoding(doc);
865 if (encoding == NULL)
866 encoding = BAD_CAST "HTML";
867 if ((encoding != NULL) && (oldctxtenc == NULL) &&
868 (buf->encoder == NULL) && (buf->conv == NULL)) {
869 if (xmlSaveSwitchEncoding(ctxt, (const char*) encoding) < 0) {
870 doc->encoding = oldenc;
871 return(-1);
872 }
873 switched_encoding = 1;
874 }
875 if (ctxt->options & XML_SAVE_FORMAT)
876 htmlNodeDumpFormatOutput(buf, doc, cur,
877 (const char *)encoding, 1);
878 else
879 htmlNodeDumpFormatOutput(buf, doc, cur,
880 (const char *)encoding, 0);
881 /*
882 * Restore the state of the saving context at the end of the document
883 */
884 if ((switched_encoding) && (oldctxtenc == NULL)) {
885 xmlSaveClearEncoding(ctxt);
886 }
887 if (doc != NULL)
888 doc->encoding = oldenc;
889 return(0);
890}
891#endif
892
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000893/**
894 * xmlNodeDumpOutputInternal:
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000895 * @cur: the current node
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000896 *
897 * Dump an XML node, recursive behaviour, children are printed too.
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000898 */
899static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000900xmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
Daniel Veillard753086a2004-03-28 16:12:44 +0000901 int format;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000902 xmlNodePtr tmp;
903 xmlChar *start, *end;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000904 xmlOutputBufferPtr buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000905
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000906 if (cur == NULL) return;
907 buf = ctxt->buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000908 if (cur->type == XML_XINCLUDE_START)
909 return;
910 if (cur->type == XML_XINCLUDE_END)
911 return;
Daniel Veillardce244ad2004-11-05 10:03:46 +0000912 if ((cur->type == XML_DOCUMENT_NODE) ||
913 (cur->type == XML_HTML_DOCUMENT_NODE)) {
914 xmlDocContentDumpOutput(ctxt, (xmlDocPtr) cur);
915 return;
916 }
Daniel Veillardda3fee42008-09-01 13:08:57 +0000917#ifdef LIBXML_HTML_ENABLED
Daniel Veillard856d9282008-09-25 14:31:40 +0000918 if (ctxt->options & XML_SAVE_XHTML) {
919 xhtmlNodeDumpOutput(ctxt, cur);
920 return;
921 }
922 if (((cur->type != XML_NAMESPACE_DECL) && (cur->doc != NULL) &&
923 (cur->doc->type == XML_HTML_DOCUMENT_NODE) &&
924 ((ctxt->options & XML_SAVE_AS_XML) == 0)) ||
925 (ctxt->options & XML_SAVE_AS_HTML)) {
Daniel Veillardda3fee42008-09-01 13:08:57 +0000926 htmlNodeDumpOutputInternal(ctxt, cur);
927 return;
928 }
929#endif
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000930 if (cur->type == XML_DTD_NODE) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000931 xmlDtdDumpOutput(ctxt, (xmlDtdPtr) cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000932 return;
933 }
934 if (cur->type == XML_DOCUMENT_FRAG_NODE) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000935 xmlNodeListDumpOutput(ctxt, cur->children);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000936 return;
937 }
938 if (cur->type == XML_ELEMENT_DECL) {
Daniel Veillard50cdab52012-07-16 14:52:00 +0800939 xmlBufDumpElementDecl(buf->buffer, (xmlElementPtr) cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000940 return;
941 }
942 if (cur->type == XML_ATTRIBUTE_DECL) {
Daniel Veillard50cdab52012-07-16 14:52:00 +0800943 xmlBufDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000944 return;
945 }
946 if (cur->type == XML_ENTITY_DECL) {
Daniel Veillard50cdab52012-07-16 14:52:00 +0800947 xmlBufDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000948 return;
949 }
950 if (cur->type == XML_TEXT_NODE) {
951 if (cur->content != NULL) {
William M. Brack4e1c2db2005-02-11 10:58:55 +0000952 if (cur->name != xmlStringTextNoenc) {
Daniel Veillard3995bc32004-05-15 18:57:31 +0000953 xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000954 } else {
955 /*
956 * Disable escaping, needed for XSLT
957 */
958 xmlOutputBufferWriteString(buf, (const char *) cur->content);
959 }
960 }
961
962 return;
963 }
964 if (cur->type == XML_PI_NODE) {
965 if (cur->content != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000966 xmlOutputBufferWrite(buf, 2, "<?");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000967 xmlOutputBufferWriteString(buf, (const char *)cur->name);
968 if (cur->content != NULL) {
Adam Spraggd2e62312010-11-03 15:33:40 +0100969 if (ctxt->format == 2)
970 xmlOutputBufferWriteWSNonSig(ctxt, 0);
971 else
972 xmlOutputBufferWrite(buf, 1, " ");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000973 xmlOutputBufferWriteString(buf, (const char *)cur->content);
974 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000975 xmlOutputBufferWrite(buf, 2, "?>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000976 } else {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000977 xmlOutputBufferWrite(buf, 2, "<?");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000978 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Adam Spraggd2e62312010-11-03 15:33:40 +0100979 if (ctxt->format == 2)
980 xmlOutputBufferWriteWSNonSig(ctxt, 0);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000981 xmlOutputBufferWrite(buf, 2, "?>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000982 }
983 return;
984 }
985 if (cur->type == XML_COMMENT_NODE) {
986 if (cur->content != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000987 xmlOutputBufferWrite(buf, 4, "<!--");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000988 xmlOutputBufferWriteString(buf, (const char *)cur->content);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000989 xmlOutputBufferWrite(buf, 3, "-->");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000990 }
991 return;
992 }
993 if (cur->type == XML_ENTITY_REF_NODE) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000994 xmlOutputBufferWrite(buf, 1, "&");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000995 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000996 xmlOutputBufferWrite(buf, 1, ";");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000997 return;
998 }
999 if (cur->type == XML_CDATA_SECTION_NODE) {
Daniel Veillardd0d2f092008-03-07 16:50:21 +00001000 if (cur->content == NULL || *cur->content == '\0') {
1001 xmlOutputBufferWrite(buf, 12, "<![CDATA[]]>");
Daniel Veillard7cd517c2005-05-20 18:47:22 +00001002 } else {
1003 start = end = cur->content;
1004 while (*end != '\0') {
1005 if ((*end == ']') && (*(end + 1) == ']') &&
1006 (*(end + 2) == '>')) {
1007 end = end + 2;
1008 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
1009 xmlOutputBufferWrite(buf, end - start, (const char *)start);
1010 xmlOutputBufferWrite(buf, 3, "]]>");
1011 start = end;
1012 }
1013 end++;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001014 }
Daniel Veillard7cd517c2005-05-20 18:47:22 +00001015 if (start != end) {
1016 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
1017 xmlOutputBufferWriteString(buf, (const char *)start);
1018 xmlOutputBufferWrite(buf, 3, "]]>");
1019 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001020 }
1021 return;
1022 }
1023 if (cur->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001024 xmlAttrDumpOutput(ctxt, (xmlAttrPtr) cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001025 return;
1026 }
1027 if (cur->type == XML_NAMESPACE_DECL) {
Adam Spraggd2e62312010-11-03 15:33:40 +01001028 xmlNsDumpOutputCtxt(ctxt, (xmlNsPtr) cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001029 return;
1030 }
1031
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001032 format = ctxt->format;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001033 if (format == 1) {
1034 tmp = cur->children;
1035 while (tmp != NULL) {
1036 if ((tmp->type == XML_TEXT_NODE) ||
1037 (tmp->type == XML_CDATA_SECTION_NODE) ||
1038 (tmp->type == XML_ENTITY_REF_NODE)) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001039 ctxt->format = 0;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001040 break;
1041 }
1042 tmp = tmp->next;
1043 }
1044 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001045 xmlOutputBufferWrite(buf, 1, "<");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001046 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1047 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001048 xmlOutputBufferWrite(buf, 1, ":");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001049 }
1050
1051 xmlOutputBufferWriteString(buf, (const char *)cur->name);
1052 if (cur->nsDef)
Adam Spraggd2e62312010-11-03 15:33:40 +01001053 xmlNsListDumpOutputCtxt(ctxt, cur->nsDef);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001054 if (cur->properties != NULL)
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001055 xmlAttrListDumpOutput(ctxt, cur->properties);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001056
1057 if (((cur->type == XML_ELEMENT_NODE) || (cur->content == NULL)) &&
Rob Richards2ce51c02005-09-12 12:16:35 +00001058 (cur->children == NULL) && ((ctxt->options & XML_SAVE_NO_EMPTY) == 0)) {
Adam Spraggd2e62312010-11-03 15:33:40 +01001059 if (ctxt->format == 2)
1060 xmlOutputBufferWriteWSNonSig(ctxt, 0);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001061 xmlOutputBufferWrite(buf, 2, "/>");
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001062 ctxt->format = format;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001063 return;
1064 }
Adam Spraggd2e62312010-11-03 15:33:40 +01001065 if (ctxt->format == 2)
1066 xmlOutputBufferWriteWSNonSig(ctxt, 1);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001067 xmlOutputBufferWrite(buf, 1, ">");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001068 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Daniel Veillard3995bc32004-05-15 18:57:31 +00001069 xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001070 }
1071 if (cur->children != NULL) {
Adam Spragg8b877132010-11-01 14:24:56 +01001072 if (ctxt->format == 1) xmlOutputBufferWrite(buf, 1, "\n");
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001073 if (ctxt->level >= 0) ctxt->level++;
1074 xmlNodeListDumpOutput(ctxt, cur->children);
1075 if (ctxt->level > 0) ctxt->level--;
Adam Spragg8b877132010-11-01 14:24:56 +01001076 if ((xmlIndentTreeOutput) && (ctxt->format == 1))
Daniel Veillard753086a2004-03-28 16:12:44 +00001077 xmlOutputBufferWrite(buf, ctxt->indent_size *
1078 (ctxt->level > ctxt->indent_nr ?
1079 ctxt->indent_nr : ctxt->level),
1080 ctxt->indent);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001081 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001082 xmlOutputBufferWrite(buf, 2, "</");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001083 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1084 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001085 xmlOutputBufferWrite(buf, 1, ":");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001086 }
1087
1088 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Adam Spraggd2e62312010-11-03 15:33:40 +01001089 if (ctxt->format == 2)
1090 xmlOutputBufferWriteWSNonSig(ctxt, 0);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001091 xmlOutputBufferWrite(buf, 1, ">");
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001092 ctxt->format = format;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001093}
1094
1095/**
1096 * xmlDocContentDumpOutput:
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001097 * @cur: the document
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001098 *
1099 * Dump an XML document.
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001100 */
Daniel Veillarddab39b52006-10-16 23:22:10 +00001101static int
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001102xmlDocContentDumpOutput(xmlSaveCtxtPtr ctxt, xmlDocPtr cur) {
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001103#ifdef LIBXML_HTML_ENABLED
1104 xmlDtdPtr dtd;
1105 int is_xhtml = 0;
1106#endif
1107 const xmlChar *oldenc = cur->encoding;
Daniel Veillarddab39b52006-10-16 23:22:10 +00001108 const xmlChar *oldctxtenc = ctxt->encoding;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001109 const xmlChar *encoding = ctxt->encoding;
Daniel Veillarddab39b52006-10-16 23:22:10 +00001110 xmlCharEncodingOutputFunc oldescape = ctxt->escape;
1111 xmlCharEncodingOutputFunc oldescapeAttr = ctxt->escapeAttr;
1112 xmlOutputBufferPtr buf = ctxt->buf;
Daniel Veillarddab39b52006-10-16 23:22:10 +00001113 xmlCharEncoding enc;
Daniel Veillardda3fee42008-09-01 13:08:57 +00001114 int switched_encoding = 0;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001115
1116 xmlInitParser();
1117
Daniel Veillardda3fee42008-09-01 13:08:57 +00001118 if ((cur->type != XML_HTML_DOCUMENT_NODE) &&
1119 (cur->type != XML_DOCUMENT_NODE))
1120 return(-1);
1121
Daniel Veillarddab39b52006-10-16 23:22:10 +00001122 if (ctxt->encoding != NULL) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001123 cur->encoding = BAD_CAST ctxt->encoding;
Daniel Veillarddab39b52006-10-16 23:22:10 +00001124 } else if (cur->encoding != NULL) {
1125 encoding = cur->encoding;
1126 } else if (cur->charset != XML_CHAR_ENCODING_UTF8) {
1127 encoding = (const xmlChar *)
1128 xmlGetCharEncodingName((xmlCharEncoding) cur->charset);
1129 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001130
Daniel Veillard856d9282008-09-25 14:31:40 +00001131 if (((cur->type == XML_HTML_DOCUMENT_NODE) &&
1132 ((ctxt->options & XML_SAVE_AS_XML) == 0) &&
1133 ((ctxt->options & XML_SAVE_XHTML) == 0)) ||
1134 (ctxt->options & XML_SAVE_AS_HTML)) {
Daniel Veillardda3fee42008-09-01 13:08:57 +00001135#ifdef LIBXML_HTML_ENABLED
1136 if (encoding != NULL)
1137 htmlSetMetaEncoding(cur, (const xmlChar *) encoding);
1138 if (encoding == NULL)
1139 encoding = htmlGetMetaEncoding(cur);
1140 if (encoding == NULL)
1141 encoding = BAD_CAST "HTML";
1142 if ((encoding != NULL) && (oldctxtenc == NULL) &&
1143 (buf->encoder == NULL) && (buf->conv == NULL)) {
1144 if (xmlSaveSwitchEncoding(ctxt, (const char*) encoding) < 0) {
1145 cur->encoding = oldenc;
Daniel Veillarddab39b52006-10-16 23:22:10 +00001146 return(-1);
1147 }
Daniel Veillarddab39b52006-10-16 23:22:10 +00001148 }
Daniel Veillardda3fee42008-09-01 13:08:57 +00001149 if (ctxt->options & XML_SAVE_FORMAT)
1150 htmlDocContentDumpFormatOutput(buf, cur,
1151 (const char *)encoding, 1);
Daniel Veillard100e1802005-08-08 14:44:11 +00001152 else
Daniel Veillardda3fee42008-09-01 13:08:57 +00001153 htmlDocContentDumpFormatOutput(buf, cur,
1154 (const char *)encoding, 0);
1155 if (ctxt->encoding != NULL)
1156 cur->encoding = oldenc;
1157 return(0);
1158#else
1159 return(-1);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001160#endif
Daniel Veillard856d9282008-09-25 14:31:40 +00001161 } else if ((cur->type == XML_DOCUMENT_NODE) ||
1162 (ctxt->options & XML_SAVE_AS_XML) ||
1163 (ctxt->options & XML_SAVE_XHTML)) {
Daniel Veillardda3fee42008-09-01 13:08:57 +00001164 enc = xmlParseCharEncoding((const char*) encoding);
1165 if ((encoding != NULL) && (oldctxtenc == NULL) &&
1166 (buf->encoder == NULL) && (buf->conv == NULL) &&
1167 ((ctxt->options & XML_SAVE_NO_DECL) == 0)) {
1168 if ((enc != XML_CHAR_ENCODING_UTF8) &&
1169 (enc != XML_CHAR_ENCODING_NONE) &&
1170 (enc != XML_CHAR_ENCODING_ASCII)) {
1171 /*
1172 * we need to switch to this encoding but just for this
1173 * document since we output the XMLDecl the conversion
1174 * must be done to not generate not well formed documents.
1175 */
1176 if (xmlSaveSwitchEncoding(ctxt, (const char*) encoding) < 0) {
1177 cur->encoding = oldenc;
1178 return(-1);
1179 }
1180 switched_encoding = 1;
1181 }
1182 if (ctxt->escape == xmlEscapeEntities)
1183 ctxt->escape = NULL;
1184 if (ctxt->escapeAttr == xmlEscapeEntities)
1185 ctxt->escapeAttr = NULL;
1186 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001187
Daniel Veillardda3fee42008-09-01 13:08:57 +00001188
1189 /*
1190 * Save the XML declaration
1191 */
1192 if ((ctxt->options & XML_SAVE_NO_DECL) == 0) {
1193 xmlOutputBufferWrite(buf, 14, "<?xml version=");
1194 if (cur->version != NULL)
Daniel Veillard50cdab52012-07-16 14:52:00 +08001195 xmlBufWriteQuotedString(buf->buffer, cur->version);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001196 else
Daniel Veillardda3fee42008-09-01 13:08:57 +00001197 xmlOutputBufferWrite(buf, 5, "\"1.0\"");
1198 if (encoding != NULL) {
1199 xmlOutputBufferWrite(buf, 10, " encoding=");
Daniel Veillard50cdab52012-07-16 14:52:00 +08001200 xmlBufWriteQuotedString(buf->buffer, (xmlChar *) encoding);
Daniel Veillardda3fee42008-09-01 13:08:57 +00001201 }
1202 switch (cur->standalone) {
1203 case 0:
1204 xmlOutputBufferWrite(buf, 16, " standalone=\"no\"");
1205 break;
1206 case 1:
1207 xmlOutputBufferWrite(buf, 17, " standalone=\"yes\"");
1208 break;
1209 }
1210 xmlOutputBufferWrite(buf, 3, "?>\n");
1211 }
1212
1213#ifdef LIBXML_HTML_ENABLED
Daniel Veillard856d9282008-09-25 14:31:40 +00001214 if (ctxt->options & XML_SAVE_XHTML)
1215 is_xhtml = 1;
Daniel Veillardda3fee42008-09-01 13:08:57 +00001216 if ((ctxt->options & XML_SAVE_NO_XHTML) == 0) {
1217 dtd = xmlGetIntSubset(cur);
1218 if (dtd != NULL) {
1219 is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
1220 if (is_xhtml < 0) is_xhtml = 0;
1221 }
1222 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001223#endif
Daniel Veillardda3fee42008-09-01 13:08:57 +00001224 if (cur->children != NULL) {
1225 xmlNodePtr child = cur->children;
1226
1227 while (child != NULL) {
1228 ctxt->level = 0;
1229#ifdef LIBXML_HTML_ENABLED
1230 if (is_xhtml)
1231 xhtmlNodeDumpOutput(ctxt, child);
1232 else
1233#endif
1234 xmlNodeDumpOutputInternal(ctxt, child);
1235 xmlOutputBufferWrite(buf, 1, "\n");
1236 child = child->next;
1237 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001238 }
1239 }
Daniel Veillardda3fee42008-09-01 13:08:57 +00001240
Daniel Veillarddab39b52006-10-16 23:22:10 +00001241 /*
1242 * Restore the state of the saving context at the end of the document
1243 */
Daniel Veillardda3fee42008-09-01 13:08:57 +00001244 if ((switched_encoding) && (oldctxtenc == NULL)) {
1245 xmlSaveClearEncoding(ctxt);
Daniel Veillarddab39b52006-10-16 23:22:10 +00001246 ctxt->escape = oldescape;
1247 ctxt->escapeAttr = oldescapeAttr;
1248 }
Daniel Veillardda3fee42008-09-01 13:08:57 +00001249 cur->encoding = oldenc;
Daniel Veillarddab39b52006-10-16 23:22:10 +00001250 return(0);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001251}
1252
1253#ifdef LIBXML_HTML_ENABLED
1254/************************************************************************
1255 * *
1256 * Functions specific to XHTML serialization *
1257 * *
1258 ************************************************************************/
1259
1260/**
1261 * xhtmlIsEmpty:
1262 * @node: the node
1263 *
1264 * Check if a node is an empty xhtml node
1265 *
1266 * Returns 1 if the node is an empty node, 0 if not and -1 in case of error
1267 */
1268static int
1269xhtmlIsEmpty(xmlNodePtr node) {
1270 if (node == NULL)
1271 return(-1);
1272 if (node->type != XML_ELEMENT_NODE)
1273 return(0);
1274 if ((node->ns != NULL) && (!xmlStrEqual(node->ns->href, XHTML_NS_NAME)))
1275 return(0);
1276 if (node->children != NULL)
1277 return(0);
1278 switch (node->name[0]) {
1279 case 'a':
1280 if (xmlStrEqual(node->name, BAD_CAST "area"))
1281 return(1);
1282 return(0);
1283 case 'b':
1284 if (xmlStrEqual(node->name, BAD_CAST "br"))
1285 return(1);
1286 if (xmlStrEqual(node->name, BAD_CAST "base"))
1287 return(1);
1288 if (xmlStrEqual(node->name, BAD_CAST "basefont"))
1289 return(1);
1290 return(0);
1291 case 'c':
1292 if (xmlStrEqual(node->name, BAD_CAST "col"))
1293 return(1);
1294 return(0);
1295 case 'f':
1296 if (xmlStrEqual(node->name, BAD_CAST "frame"))
1297 return(1);
1298 return(0);
1299 case 'h':
1300 if (xmlStrEqual(node->name, BAD_CAST "hr"))
1301 return(1);
1302 return(0);
1303 case 'i':
1304 if (xmlStrEqual(node->name, BAD_CAST "img"))
1305 return(1);
1306 if (xmlStrEqual(node->name, BAD_CAST "input"))
1307 return(1);
1308 if (xmlStrEqual(node->name, BAD_CAST "isindex"))
1309 return(1);
1310 return(0);
1311 case 'l':
1312 if (xmlStrEqual(node->name, BAD_CAST "link"))
1313 return(1);
1314 return(0);
1315 case 'm':
1316 if (xmlStrEqual(node->name, BAD_CAST "meta"))
1317 return(1);
1318 return(0);
1319 case 'p':
1320 if (xmlStrEqual(node->name, BAD_CAST "param"))
1321 return(1);
1322 return(0);
1323 }
1324 return(0);
1325}
1326
1327/**
1328 * xhtmlAttrListDumpOutput:
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001329 * @cur: the first attribute pointer
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001330 *
1331 * Dump a list of XML attributes
1332 */
1333static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001334xhtmlAttrListDumpOutput(xmlSaveCtxtPtr ctxt, xmlAttrPtr cur) {
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001335 xmlAttrPtr xml_lang = NULL;
1336 xmlAttrPtr lang = NULL;
1337 xmlAttrPtr name = NULL;
1338 xmlAttrPtr id = NULL;
1339 xmlNodePtr parent;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001340 xmlOutputBufferPtr buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001341
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001342 if (cur == NULL) return;
1343 buf = ctxt->buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001344 parent = cur->parent;
1345 while (cur != NULL) {
1346 if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "id")))
1347 id = cur;
1348 else
1349 if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "name")))
1350 name = cur;
1351 else
1352 if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")))
1353 lang = cur;
1354 else
1355 if ((cur->ns != NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")) &&
1356 (xmlStrEqual(cur->ns->prefix, BAD_CAST "xml")))
1357 xml_lang = cur;
1358 else if ((cur->ns == NULL) &&
1359 ((cur->children == NULL) ||
1360 (cur->children->content == NULL) ||
1361 (cur->children->content[0] == 0)) &&
1362 (htmlIsBooleanAttr(cur->name))) {
1363 if (cur->children != NULL)
1364 xmlFreeNode(cur->children);
1365 cur->children = xmlNewText(cur->name);
1366 if (cur->children != NULL)
1367 cur->children->parent = (xmlNodePtr) cur;
1368 }
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001369 xmlAttrDumpOutput(ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001370 cur = cur->next;
1371 }
1372 /*
1373 * C.8
1374 */
1375 if ((name != NULL) && (id == NULL)) {
1376 if ((parent != NULL) && (parent->name != NULL) &&
1377 ((xmlStrEqual(parent->name, BAD_CAST "a")) ||
1378 (xmlStrEqual(parent->name, BAD_CAST "p")) ||
1379 (xmlStrEqual(parent->name, BAD_CAST "div")) ||
1380 (xmlStrEqual(parent->name, BAD_CAST "img")) ||
1381 (xmlStrEqual(parent->name, BAD_CAST "map")) ||
1382 (xmlStrEqual(parent->name, BAD_CAST "applet")) ||
1383 (xmlStrEqual(parent->name, BAD_CAST "form")) ||
1384 (xmlStrEqual(parent->name, BAD_CAST "frame")) ||
1385 (xmlStrEqual(parent->name, BAD_CAST "iframe")))) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001386 xmlOutputBufferWrite(buf, 5, " id=\"");
1387 xmlAttrSerializeContent(buf, name);
1388 xmlOutputBufferWrite(buf, 1, "\"");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001389 }
1390 }
1391 /*
1392 * C.7.
1393 */
1394 if ((lang != NULL) && (xml_lang == NULL)) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001395 xmlOutputBufferWrite(buf, 11, " xml:lang=\"");
1396 xmlAttrSerializeContent(buf, lang);
1397 xmlOutputBufferWrite(buf, 1, "\"");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001398 } else
1399 if ((xml_lang != NULL) && (lang == NULL)) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001400 xmlOutputBufferWrite(buf, 7, " lang=\"");
1401 xmlAttrSerializeContent(buf, xml_lang);
1402 xmlOutputBufferWrite(buf, 1, "\"");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001403 }
1404}
1405
1406/**
1407 * xhtmlNodeListDumpOutput:
1408 * @buf: the XML buffer output
1409 * @doc: the XHTML document
1410 * @cur: the first node
1411 * @level: the imbrication level for indenting
1412 * @format: is formatting allowed
1413 * @encoding: an optional encoding string
1414 *
1415 * Dump an XML node list, recursive behaviour, children are printed too.
1416 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
1417 * or xmlKeepBlanksDefault(0) was called
1418 */
1419static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001420xhtmlNodeListDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001421 xmlOutputBufferPtr buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001422
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001423 if (cur == NULL) return;
1424 buf = ctxt->buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001425 while (cur != NULL) {
Adam Spragg8b877132010-11-01 14:24:56 +01001426 if ((ctxt->format == 1) && (xmlIndentTreeOutput) &&
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001427 (cur->type == XML_ELEMENT_NODE))
Daniel Veillard753086a2004-03-28 16:12:44 +00001428 xmlOutputBufferWrite(buf, ctxt->indent_size *
1429 (ctxt->level > ctxt->indent_nr ?
1430 ctxt->indent_nr : ctxt->level),
1431 ctxt->indent);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001432 xhtmlNodeDumpOutput(ctxt, cur);
Adam Spragg8b877132010-11-01 14:24:56 +01001433 if (ctxt->format == 1) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001434 xmlOutputBufferWrite(buf, 1, "\n");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001435 }
1436 cur = cur->next;
1437 }
1438}
1439
1440/**
1441 * xhtmlNodeDumpOutput:
1442 * @buf: the XML buffer output
1443 * @doc: the XHTML document
1444 * @cur: the current node
1445 * @level: the imbrication level for indenting
1446 * @format: is formatting allowed
1447 * @encoding: an optional encoding string
1448 *
1449 * Dump an XHTML node, recursive behaviour, children are printed too.
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001450 */
1451static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001452xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
Rob Richards31f73022005-08-26 15:33:26 +00001453 int format, addmeta = 0;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001454 xmlNodePtr tmp;
1455 xmlChar *start, *end;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001456 xmlOutputBufferPtr buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001457
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001458 if (cur == NULL) return;
Daniel Veillard60071ae2005-09-12 00:03:43 +00001459 if ((cur->type == XML_DOCUMENT_NODE) ||
1460 (cur->type == XML_HTML_DOCUMENT_NODE)) {
1461 xmlDocContentDumpOutput(ctxt, (xmlDocPtr) cur);
1462 return;
1463 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001464 if (cur->type == XML_XINCLUDE_START)
1465 return;
1466 if (cur->type == XML_XINCLUDE_END)
1467 return;
Daniel Veillard3e62adb2012-08-09 14:24:02 +08001468 if (cur->type == XML_NAMESPACE_DECL) {
1469 xmlNsDumpOutputCtxt(ctxt, (xmlNsPtr) cur);
1470 return;
1471 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001472 if (cur->type == XML_DTD_NODE) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001473 xmlDtdDumpOutput(ctxt, (xmlDtdPtr) cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001474 return;
1475 }
Rob Richards2e2691b2005-10-21 14:45:16 +00001476 if (cur->type == XML_DOCUMENT_FRAG_NODE) {
1477 xhtmlNodeListDumpOutput(ctxt, cur->children);
1478 return;
1479 }
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001480 buf = ctxt->buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001481 if (cur->type == XML_ELEMENT_DECL) {
Daniel Veillard50cdab52012-07-16 14:52:00 +08001482 xmlBufDumpElementDecl(buf->buffer, (xmlElementPtr) cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001483 return;
1484 }
1485 if (cur->type == XML_ATTRIBUTE_DECL) {
Daniel Veillard50cdab52012-07-16 14:52:00 +08001486 xmlBufDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001487 return;
1488 }
1489 if (cur->type == XML_ENTITY_DECL) {
Daniel Veillard50cdab52012-07-16 14:52:00 +08001490 xmlBufDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001491 return;
1492 }
1493 if (cur->type == XML_TEXT_NODE) {
1494 if (cur->content != NULL) {
1495 if ((cur->name == xmlStringText) ||
1496 (cur->name != xmlStringTextNoenc)) {
Daniel Veillard3995bc32004-05-15 18:57:31 +00001497 xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001498 } else {
1499 /*
1500 * Disable escaping, needed for XSLT
1501 */
1502 xmlOutputBufferWriteString(buf, (const char *) cur->content);
1503 }
1504 }
1505
1506 return;
1507 }
1508 if (cur->type == XML_PI_NODE) {
1509 if (cur->content != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001510 xmlOutputBufferWrite(buf, 2, "<?");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001511 xmlOutputBufferWriteString(buf, (const char *)cur->name);
1512 if (cur->content != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001513 xmlOutputBufferWrite(buf, 1, " ");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001514 xmlOutputBufferWriteString(buf, (const char *)cur->content);
1515 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001516 xmlOutputBufferWrite(buf, 2, "?>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001517 } else {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001518 xmlOutputBufferWrite(buf, 2, "<?");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001519 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001520 xmlOutputBufferWrite(buf, 2, "?>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001521 }
1522 return;
1523 }
1524 if (cur->type == XML_COMMENT_NODE) {
1525 if (cur->content != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001526 xmlOutputBufferWrite(buf, 4, "<!--");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001527 xmlOutputBufferWriteString(buf, (const char *)cur->content);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001528 xmlOutputBufferWrite(buf, 3, "-->");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001529 }
1530 return;
1531 }
1532 if (cur->type == XML_ENTITY_REF_NODE) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001533 xmlOutputBufferWrite(buf, 1, "&");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001534 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001535 xmlOutputBufferWrite(buf, 1, ";");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001536 return;
1537 }
1538 if (cur->type == XML_CDATA_SECTION_NODE) {
Daniel Veillardd0d2f092008-03-07 16:50:21 +00001539 if (cur->content == NULL || *cur->content == '\0') {
1540 xmlOutputBufferWrite(buf, 12, "<![CDATA[]]>");
1541 } else {
1542 start = end = cur->content;
1543 while (*end != '\0') {
1544 if (*end == ']' && *(end + 1) == ']' && *(end + 2) == '>') {
1545 end = end + 2;
1546 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
1547 xmlOutputBufferWrite(buf, end - start, (const char *)start);
1548 xmlOutputBufferWrite(buf, 3, "]]>");
1549 start = end;
1550 }
1551 end++;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001552 }
Daniel Veillardd0d2f092008-03-07 16:50:21 +00001553 if (start != end) {
1554 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
1555 xmlOutputBufferWriteString(buf, (const char *)start);
1556 xmlOutputBufferWrite(buf, 3, "]]>");
1557 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001558 }
1559 return;
1560 }
Daniel Veillarda76a81f2007-10-10 08:28:18 +00001561 if (cur->type == XML_ATTRIBUTE_NODE) {
1562 xmlAttrDumpOutput(ctxt, (xmlAttrPtr) cur);
1563 return;
1564 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001565
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001566 format = ctxt->format;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001567 if (format == 1) {
1568 tmp = cur->children;
1569 while (tmp != NULL) {
1570 if ((tmp->type == XML_TEXT_NODE) ||
1571 (tmp->type == XML_ENTITY_REF_NODE)) {
1572 format = 0;
1573 break;
1574 }
1575 tmp = tmp->next;
1576 }
1577 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001578 xmlOutputBufferWrite(buf, 1, "<");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001579 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1580 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001581 xmlOutputBufferWrite(buf, 1, ":");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001582 }
1583
1584 xmlOutputBufferWriteString(buf, (const char *)cur->name);
1585 if (cur->nsDef)
Adam Spraggd2e62312010-11-03 15:33:40 +01001586 xmlNsListDumpOutputCtxt(ctxt, cur->nsDef);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001587 if ((xmlStrEqual(cur->name, BAD_CAST "html") &&
1588 (cur->ns == NULL) && (cur->nsDef == NULL))) {
1589 /*
1590 * 3.1.1. Strictly Conforming Documents A.3.1.1 3/
1591 */
1592 xmlOutputBufferWriteString(buf,
1593 " xmlns=\"http://www.w3.org/1999/xhtml\"");
1594 }
1595 if (cur->properties != NULL)
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001596 xhtmlAttrListDumpOutput(ctxt, cur->properties);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001597
Rob Richards31f73022005-08-26 15:33:26 +00001598 if ((cur->type == XML_ELEMENT_NODE) &&
1599 (cur->parent != NULL) &&
1600 (cur->parent->parent == (xmlNodePtr) cur->doc) &&
1601 xmlStrEqual(cur->name, BAD_CAST"head") &&
1602 xmlStrEqual(cur->parent->name, BAD_CAST"html")) {
1603
1604 tmp = cur->children;
1605 while (tmp != NULL) {
1606 if (xmlStrEqual(tmp->name, BAD_CAST"meta")) {
1607 xmlChar *httpequiv;
1608
1609 httpequiv = xmlGetProp(tmp, BAD_CAST"http-equiv");
Rob Richards07b72002005-09-03 14:56:36 +00001610 if (httpequiv != NULL) {
1611 if (xmlStrcasecmp(httpequiv, BAD_CAST"Content-Type") == 0) {
1612 xmlFree(httpequiv);
1613 break;
1614 }
Rob Richards31f73022005-08-26 15:33:26 +00001615 xmlFree(httpequiv);
Rob Richards31f73022005-08-26 15:33:26 +00001616 }
Rob Richards31f73022005-08-26 15:33:26 +00001617 }
1618 tmp = tmp->next;
1619 }
1620 if (tmp == NULL)
1621 addmeta = 1;
1622 }
1623
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001624 if ((cur->type == XML_ELEMENT_NODE) && (cur->children == NULL)) {
1625 if (((cur->ns == NULL) || (cur->ns->prefix == NULL)) &&
Rob Richards31f73022005-08-26 15:33:26 +00001626 ((xhtmlIsEmpty(cur) == 1) && (addmeta == 0))) {
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001627 /*
1628 * C.2. Empty Elements
1629 */
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001630 xmlOutputBufferWrite(buf, 3, " />");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001631 } else {
Rob Richards31f73022005-08-26 15:33:26 +00001632 if (addmeta == 1) {
1633 xmlOutputBufferWrite(buf, 1, ">");
Adam Spragg8b877132010-11-01 14:24:56 +01001634 if (ctxt->format == 1) {
Rob Richards2ce51c02005-09-12 12:16:35 +00001635 xmlOutputBufferWrite(buf, 1, "\n");
1636 if (xmlIndentTreeOutput)
1637 xmlOutputBufferWrite(buf, ctxt->indent_size *
1638 (ctxt->level + 1 > ctxt->indent_nr ?
1639 ctxt->indent_nr : ctxt->level + 1), ctxt->indent);
1640 }
Rob Richards31f73022005-08-26 15:33:26 +00001641 xmlOutputBufferWriteString(buf,
1642 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=");
1643 if (ctxt->encoding) {
1644 xmlOutputBufferWriteString(buf, (const char *)ctxt->encoding);
1645 } else {
1646 xmlOutputBufferWrite(buf, 5, "UTF-8");
1647 }
Rob Richards2ce51c02005-09-12 12:16:35 +00001648 xmlOutputBufferWrite(buf, 4, "\" />");
Adam Spragg8b877132010-11-01 14:24:56 +01001649 if (ctxt->format == 1)
Rob Richards2ce51c02005-09-12 12:16:35 +00001650 xmlOutputBufferWrite(buf, 1, "\n");
1651 } else {
1652 xmlOutputBufferWrite(buf, 1, ">");
Rob Richards31f73022005-08-26 15:33:26 +00001653 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001654 /*
1655 * C.3. Element Minimization and Empty Element Content
1656 */
Rob Richards2ce51c02005-09-12 12:16:35 +00001657 xmlOutputBufferWrite(buf, 2, "</");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001658 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1659 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001660 xmlOutputBufferWrite(buf, 1, ":");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001661 }
1662 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001663 xmlOutputBufferWrite(buf, 1, ">");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001664 }
1665 return;
1666 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001667 xmlOutputBufferWrite(buf, 1, ">");
Rob Richards31f73022005-08-26 15:33:26 +00001668 if (addmeta == 1) {
Adam Spragg8b877132010-11-01 14:24:56 +01001669 if (ctxt->format == 1) {
Rob Richards2ce51c02005-09-12 12:16:35 +00001670 xmlOutputBufferWrite(buf, 1, "\n");
1671 if (xmlIndentTreeOutput)
1672 xmlOutputBufferWrite(buf, ctxt->indent_size *
1673 (ctxt->level + 1 > ctxt->indent_nr ?
1674 ctxt->indent_nr : ctxt->level + 1), ctxt->indent);
1675 }
Rob Richards31f73022005-08-26 15:33:26 +00001676 xmlOutputBufferWriteString(buf,
1677 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=");
1678 if (ctxt->encoding) {
1679 xmlOutputBufferWriteString(buf, (const char *)ctxt->encoding);
1680 } else {
1681 xmlOutputBufferWrite(buf, 5, "UTF-8");
1682 }
1683 xmlOutputBufferWrite(buf, 4, "\" />");
1684 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001685 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Daniel Veillard3995bc32004-05-15 18:57:31 +00001686 xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001687 }
1688
Kasimier T. Buchcik7b4e2e22006-07-13 13:07:11 +00001689#if 0
1690 /*
1691 * This was removed due to problems with HTML processors.
1692 * See bug #345147.
Daniel Veillarddab39b52006-10-16 23:22:10 +00001693 */
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001694 /*
1695 * 4.8. Script and Style elements
1696 */
1697 if ((cur->type == XML_ELEMENT_NODE) &&
1698 ((xmlStrEqual(cur->name, BAD_CAST "script")) ||
1699 (xmlStrEqual(cur->name, BAD_CAST "style"))) &&
1700 ((cur->ns == NULL) ||
1701 (xmlStrEqual(cur->ns->href, XHTML_NS_NAME)))) {
1702 xmlNodePtr child = cur->children;
1703
1704 while (child != NULL) {
Daniel Veillarddbd61052005-09-12 14:03:26 +00001705 if (child->type == XML_TEXT_NODE) {
1706 if ((xmlStrchr(child->content, '<') == NULL) &&
1707 (xmlStrchr(child->content, '&') == NULL) &&
1708 (xmlStrstr(child->content, BAD_CAST "]]>") == NULL)) {
1709 /* Nothing to escape, so just output as is... */
1710 /* FIXME: Should we do something about "--" also? */
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001711 int level = ctxt->level;
1712 int indent = ctxt->format;
1713
1714 ctxt->level = 0;
1715 ctxt->format = 0;
Daniel Veillarddbd61052005-09-12 14:03:26 +00001716 xmlOutputBufferWriteString(buf, (const char *) child->content);
1717 /* (We cannot use xhtmlNodeDumpOutput() here because
1718 * we wish to leave '>' unescaped!) */
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001719 ctxt->level = level;
1720 ctxt->format = indent;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001721 } else {
Daniel Veillarddbd61052005-09-12 14:03:26 +00001722 /* We must use a CDATA section. Unfortunately,
1723 * this will break CSS and JavaScript when read by
1724 * a browser in HTML4-compliant mode. :-( */
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001725 start = end = child->content;
1726 while (*end != '\0') {
1727 if (*end == ']' &&
1728 *(end + 1) == ']' &&
1729 *(end + 2) == '>') {
1730 end = end + 2;
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001731 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001732 xmlOutputBufferWrite(buf, end - start,
1733 (const char *)start);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001734 xmlOutputBufferWrite(buf, 3, "]]>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001735 start = end;
1736 }
1737 end++;
1738 }
1739 if (start != end) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001740 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
1741 xmlOutputBufferWrite(buf, end - start,
1742 (const char *)start);
1743 xmlOutputBufferWrite(buf, 3, "]]>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001744 }
1745 }
1746 } else {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001747 int level = ctxt->level;
1748 int indent = ctxt->format;
1749
1750 ctxt->level = 0;
1751 ctxt->format = 0;
1752 xhtmlNodeDumpOutput(ctxt, child);
1753 ctxt->level = level;
1754 ctxt->format = indent;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001755 }
1756 child = child->next;
1757 }
Kasimier T. Buchcik7b4e2e22006-07-13 13:07:11 +00001758 }
1759#endif
1760
1761 if (cur->children != NULL) {
Daniel Veillardf0244ce2004-05-09 23:48:39 +00001762 int indent = ctxt->format;
1763
Adam Spragg8b877132010-11-01 14:24:56 +01001764 if (format == 1) xmlOutputBufferWrite(buf, 1, "\n");
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001765 if (ctxt->level >= 0) ctxt->level++;
Daniel Veillardf0244ce2004-05-09 23:48:39 +00001766 ctxt->format = format;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001767 xhtmlNodeListDumpOutput(ctxt, cur->children);
1768 if (ctxt->level > 0) ctxt->level--;
Daniel Veillardf0244ce2004-05-09 23:48:39 +00001769 ctxt->format = indent;
Adam Spragg8b877132010-11-01 14:24:56 +01001770 if ((xmlIndentTreeOutput) && (format == 1))
Daniel Veillard753086a2004-03-28 16:12:44 +00001771 xmlOutputBufferWrite(buf, ctxt->indent_size *
1772 (ctxt->level > ctxt->indent_nr ?
1773 ctxt->indent_nr : ctxt->level),
1774 ctxt->indent);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001775 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001776 xmlOutputBufferWrite(buf, 2, "</");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001777 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1778 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001779 xmlOutputBufferWrite(buf, 1, ":");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001780 }
1781
1782 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001783 xmlOutputBufferWrite(buf, 1, ">");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001784}
1785#endif
1786
1787/************************************************************************
1788 * *
1789 * Public entry points *
1790 * *
1791 ************************************************************************/
1792
1793/**
1794 * xmlSaveToFd:
1795 * @fd: a file descriptor number
1796 * @encoding: the encoding name to use or NULL
1797 * @options: a set of xmlSaveOptions
1798 *
1799 * Create a document saving context serializing to a file descriptor
1800 * with the encoding and the options given.
1801 *
1802 * Returns a new serialization context or NULL in case of error.
1803 */
1804xmlSaveCtxtPtr
1805xmlSaveToFd(int fd, const char *encoding, int options)
1806{
1807 xmlSaveCtxtPtr ret;
1808
1809 ret = xmlNewSaveCtxt(encoding, options);
1810 if (ret == NULL) return(NULL);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001811 ret->buf = xmlOutputBufferCreateFd(fd, ret->handler);
1812 if (ret->buf == NULL) {
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001813 xmlFreeSaveCtxt(ret);
1814 return(NULL);
1815 }
1816 return(ret);
1817}
1818
1819/**
1820 * xmlSaveToFilename:
1821 * @filename: a file name or an URL
1822 * @encoding: the encoding name to use or NULL
1823 * @options: a set of xmlSaveOptions
1824 *
1825 * Create a document saving context serializing to a filename or possibly
1826 * to an URL (but this is less reliable) with the encoding and the options
1827 * given.
1828 *
1829 * Returns a new serialization context or NULL in case of error.
1830 */
1831xmlSaveCtxtPtr
1832xmlSaveToFilename(const char *filename, const char *encoding, int options)
1833{
1834 xmlSaveCtxtPtr ret;
1835 int compression = 0; /* TODO handle compression option */
1836
1837 ret = xmlNewSaveCtxt(encoding, options);
1838 if (ret == NULL) return(NULL);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001839 ret->buf = xmlOutputBufferCreateFilename(filename, ret->handler,
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001840 compression);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001841 if (ret->buf == NULL) {
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001842 xmlFreeSaveCtxt(ret);
1843 return(NULL);
1844 }
1845 return(ret);
1846}
1847
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001848/**
1849 * xmlSaveToBuffer:
1850 * @buffer: a buffer
1851 * @encoding: the encoding name to use or NULL
1852 * @options: a set of xmlSaveOptions
1853 *
1854 * Create a document saving context serializing to a buffer
1855 * with the encoding and the options given
1856 *
1857 * Returns a new serialization context or NULL in case of error.
Daniel Veillard9a00fd22005-11-09 08:56:26 +00001858 */
1859
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001860xmlSaveCtxtPtr
1861xmlSaveToBuffer(xmlBufferPtr buffer, const char *encoding, int options)
1862{
Daniel Veillard9a00fd22005-11-09 08:56:26 +00001863 xmlSaveCtxtPtr ret;
1864 xmlOutputBufferPtr out_buff;
1865 xmlCharEncodingHandlerPtr handler;
1866
1867 ret = xmlNewSaveCtxt(encoding, options);
1868 if (ret == NULL) return(NULL);
1869
1870 if (encoding != NULL) {
1871 handler = xmlFindCharEncodingHandler(encoding);
1872 if (handler == NULL) {
1873 xmlFree(ret);
1874 return(NULL);
1875 }
1876 } else
1877 handler = NULL;
1878 out_buff = xmlOutputBufferCreateBuffer(buffer, handler);
1879 if (out_buff == NULL) {
1880 xmlFree(ret);
1881 if (handler) xmlCharEncCloseFunc(handler);
1882 return(NULL);
1883 }
1884
1885 ret->buf = out_buff;
1886 return(ret);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001887}
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001888
1889/**
1890 * xmlSaveToIO:
1891 * @iowrite: an I/O write function
1892 * @ioclose: an I/O close function
1893 * @ioctx: an I/O handler
1894 * @encoding: the encoding name to use or NULL
1895 * @options: a set of xmlSaveOptions
1896 *
1897 * Create a document saving context serializing to a file descriptor
1898 * with the encoding and the options given
1899 *
1900 * Returns a new serialization context or NULL in case of error.
1901 */
1902xmlSaveCtxtPtr
1903xmlSaveToIO(xmlOutputWriteCallback iowrite,
1904 xmlOutputCloseCallback ioclose,
1905 void *ioctx, const char *encoding, int options)
1906{
1907 xmlSaveCtxtPtr ret;
1908
1909 ret = xmlNewSaveCtxt(encoding, options);
1910 if (ret == NULL) return(NULL);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001911 ret->buf = xmlOutputBufferCreateIO(iowrite, ioclose, ioctx, ret->handler);
1912 if (ret->buf == NULL) {
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001913 xmlFreeSaveCtxt(ret);
1914 return(NULL);
1915 }
1916 return(ret);
1917}
1918
1919/**
1920 * xmlSaveDoc:
1921 * @ctxt: a document saving context
1922 * @doc: a document
1923 *
1924 * Save a full document to a saving context
Daniel Veillard377e1a92004-04-16 16:30:05 +00001925 * TODO: The function is not fully implemented yet as it does not return the
1926 * byte count but 0 instead
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001927 *
1928 * Returns the number of byte written or -1 in case of error
1929 */
1930long
1931xmlSaveDoc(xmlSaveCtxtPtr ctxt, xmlDocPtr doc)
1932{
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001933 long ret = 0;
1934
Daniel Veillardce682bc2004-11-05 17:22:25 +00001935 if ((ctxt == NULL) || (doc == NULL)) return(-1);
Daniel Veillarddab39b52006-10-16 23:22:10 +00001936 if (xmlDocContentDumpOutput(ctxt, doc) < 0)
1937 return(-1);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001938 return(ret);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001939}
1940
1941/**
1942 * xmlSaveTree:
1943 * @ctxt: a document saving context
Daniel Veillard681e9042006-09-29 09:16:00 +00001944 * @node: the top node of the subtree to save
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001945 *
1946 * Save a subtree starting at the node parameter to a saving context
Daniel Veillard377e1a92004-04-16 16:30:05 +00001947 * TODO: The function is not fully implemented yet as it does not return the
1948 * byte count but 0 instead
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001949 *
1950 * Returns the number of byte written or -1 in case of error
1951 */
1952long
1953xmlSaveTree(xmlSaveCtxtPtr ctxt, xmlNodePtr node)
1954{
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001955 long ret = 0;
1956
Daniel Veillardce682bc2004-11-05 17:22:25 +00001957 if ((ctxt == NULL) || (node == NULL)) return(-1);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001958 xmlNodeDumpOutputInternal(ctxt, node);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001959 return(ret);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001960}
1961
1962/**
1963 * xmlSaveFlush:
1964 * @ctxt: a document saving context
1965 *
1966 * Flush a document saving context, i.e. make sure that all bytes have
1967 * been output.
1968 *
1969 * Returns the number of byte written or -1 in case of error.
1970 */
1971int
1972xmlSaveFlush(xmlSaveCtxtPtr ctxt)
1973{
1974 if (ctxt == NULL) return(-1);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001975 if (ctxt->buf == NULL) return(-1);
1976 return(xmlOutputBufferFlush(ctxt->buf));
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001977}
1978
1979/**
1980 * xmlSaveClose:
1981 * @ctxt: a document saving context
1982 *
1983 * Close a document saving context, i.e. make sure that all bytes have
1984 * been output and free the associated data.
1985 *
1986 * Returns the number of byte written or -1 in case of error.
1987 */
1988int
1989xmlSaveClose(xmlSaveCtxtPtr ctxt)
1990{
1991 int ret;
1992
1993 if (ctxt == NULL) return(-1);
1994 ret = xmlSaveFlush(ctxt);
1995 xmlFreeSaveCtxt(ctxt);
1996 return(ret);
1997}
1998
Daniel Veillard3995bc32004-05-15 18:57:31 +00001999/**
2000 * xmlSaveSetEscape:
2001 * @ctxt: a document saving context
2002 * @escape: the escaping function
2003 *
2004 * Set a custom escaping function to be used for text in element content
2005 *
2006 * Returns 0 if successful or -1 in case of error.
2007 */
2008int
2009xmlSaveSetEscape(xmlSaveCtxtPtr ctxt, xmlCharEncodingOutputFunc escape)
2010{
2011 if (ctxt == NULL) return(-1);
2012 ctxt->escape = escape;
2013 return(0);
2014}
2015
2016/**
2017 * xmlSaveSetAttrEscape:
2018 * @ctxt: a document saving context
2019 * @escape: the escaping function
2020 *
2021 * Set a custom escaping function to be used for text in attribute content
2022 *
2023 * Returns 0 if successful or -1 in case of error.
2024 */
2025int
2026xmlSaveSetAttrEscape(xmlSaveCtxtPtr ctxt, xmlCharEncodingOutputFunc escape)
2027{
2028 if (ctxt == NULL) return(-1);
2029 ctxt->escapeAttr = escape;
2030 return(0);
2031}
2032
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002033/************************************************************************
2034 * *
2035 * Public entry points based on buffers *
2036 * *
2037 ************************************************************************/
Daniel Veillard50cdab52012-07-16 14:52:00 +08002038
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002039/**
Daniel Veillard50cdab52012-07-16 14:52:00 +08002040 * xmlBufAttrSerializeTxtContent:
2041 * @buf: and xmlBufPtr output
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002042 * @doc: the document
2043 * @attr: the attribute node
2044 * @string: the text content
2045 *
Daniel Veillard50cdab52012-07-16 14:52:00 +08002046 * Serialize text attribute values to an xmlBufPtr
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002047 */
2048void
Daniel Veillard50cdab52012-07-16 14:52:00 +08002049xmlBufAttrSerializeTxtContent(xmlBufPtr buf, xmlDocPtr doc,
2050 xmlAttrPtr attr, const xmlChar * string)
Daniel Veillard7a6361f2004-05-15 16:37:50 +00002051{
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002052 xmlChar *base, *cur;
2053
Daniel Veillard7a6361f2004-05-15 16:37:50 +00002054 if (string == NULL)
2055 return;
2056 base = cur = (xmlChar *) string;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002057 while (*cur != 0) {
2058 if (*cur == '\n') {
2059 if (base != cur)
Daniel Veillard50cdab52012-07-16 14:52:00 +08002060 xmlBufAdd(buf, base, cur - base);
2061 xmlBufAdd(buf, BAD_CAST "&#10;", 5);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002062 cur++;
2063 base = cur;
Daniel Veillard7a6361f2004-05-15 16:37:50 +00002064 } else if (*cur == '\r') {
2065 if (base != cur)
Daniel Veillard50cdab52012-07-16 14:52:00 +08002066 xmlBufAdd(buf, base, cur - base);
2067 xmlBufAdd(buf, BAD_CAST "&#13;", 5);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00002068 cur++;
2069 base = cur;
2070 } else if (*cur == '\t') {
2071 if (base != cur)
Daniel Veillard50cdab52012-07-16 14:52:00 +08002072 xmlBufAdd(buf, base, cur - base);
2073 xmlBufAdd(buf, BAD_CAST "&#9;", 4);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00002074 cur++;
2075 base = cur;
2076 } else if (*cur == '"') {
2077 if (base != cur)
Daniel Veillard50cdab52012-07-16 14:52:00 +08002078 xmlBufAdd(buf, base, cur - base);
2079 xmlBufAdd(buf, BAD_CAST "&quot;", 6);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00002080 cur++;
2081 base = cur;
2082 } else if (*cur == '<') {
2083 if (base != cur)
Daniel Veillard50cdab52012-07-16 14:52:00 +08002084 xmlBufAdd(buf, base, cur - base);
2085 xmlBufAdd(buf, BAD_CAST "&lt;", 4);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00002086 cur++;
2087 base = cur;
2088 } else if (*cur == '>') {
2089 if (base != cur)
Daniel Veillard50cdab52012-07-16 14:52:00 +08002090 xmlBufAdd(buf, base, cur - base);
2091 xmlBufAdd(buf, BAD_CAST "&gt;", 4);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00002092 cur++;
2093 base = cur;
2094 } else if (*cur == '&') {
2095 if (base != cur)
Daniel Veillard50cdab52012-07-16 14:52:00 +08002096 xmlBufAdd(buf, base, cur - base);
2097 xmlBufAdd(buf, BAD_CAST "&amp;", 5);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00002098 cur++;
2099 base = cur;
2100 } else if ((*cur >= 0x80) && ((doc == NULL) ||
2101 (doc->encoding == NULL))) {
2102 /*
2103 * We assume we have UTF-8 content.
2104 */
Daniel Veillard07953482012-01-22 17:42:35 +08002105 unsigned char tmp[12];
Daniel Veillard7a6361f2004-05-15 16:37:50 +00002106 int val = 0, l = 1;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002107
Daniel Veillard7a6361f2004-05-15 16:37:50 +00002108 if (base != cur)
Daniel Veillard50cdab52012-07-16 14:52:00 +08002109 xmlBufAdd(buf, base, cur - base);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00002110 if (*cur < 0xC0) {
2111 xmlSaveErr(XML_SAVE_NOT_UTF8, (xmlNodePtr) attr, NULL);
2112 if (doc != NULL)
2113 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
2114 xmlSerializeHexCharRef(tmp, *cur);
Daniel Veillard50cdab52012-07-16 14:52:00 +08002115 xmlBufAdd(buf, (xmlChar *) tmp, -1);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002116 cur++;
Daniel Veillard7a6361f2004-05-15 16:37:50 +00002117 base = cur;
2118 continue;
2119 } else if (*cur < 0xE0) {
2120 val = (cur[0]) & 0x1F;
2121 val <<= 6;
2122 val |= (cur[1]) & 0x3F;
2123 l = 2;
2124 } else if (*cur < 0xF0) {
2125 val = (cur[0]) & 0x0F;
2126 val <<= 6;
2127 val |= (cur[1]) & 0x3F;
2128 val <<= 6;
2129 val |= (cur[2]) & 0x3F;
2130 l = 3;
2131 } else if (*cur < 0xF8) {
2132 val = (cur[0]) & 0x07;
2133 val <<= 6;
2134 val |= (cur[1]) & 0x3F;
2135 val <<= 6;
2136 val |= (cur[2]) & 0x3F;
2137 val <<= 6;
2138 val |= (cur[3]) & 0x3F;
2139 l = 4;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002140 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00002141 if ((l == 1) || (!IS_CHAR(val))) {
2142 xmlSaveErr(XML_SAVE_CHAR_INVALID, (xmlNodePtr) attr, NULL);
2143 if (doc != NULL)
2144 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
Daniel Veillard50cdab52012-07-16 14:52:00 +08002145
Daniel Veillard7a6361f2004-05-15 16:37:50 +00002146 xmlSerializeHexCharRef(tmp, *cur);
Daniel Veillard50cdab52012-07-16 14:52:00 +08002147 xmlBufAdd(buf, (xmlChar *) tmp, -1);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00002148 cur++;
2149 base = cur;
2150 continue;
2151 }
2152 /*
2153 * We could do multiple things here. Just save
2154 * as a char ref
2155 */
2156 xmlSerializeHexCharRef(tmp, val);
Daniel Veillard50cdab52012-07-16 14:52:00 +08002157 xmlBufAdd(buf, (xmlChar *) tmp, -1);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00002158 cur += l;
2159 base = cur;
2160 } else {
2161 cur++;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002162 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00002163 }
2164 if (base != cur)
Daniel Veillard50cdab52012-07-16 14:52:00 +08002165 xmlBufAdd(buf, base, cur - base);
2166}
2167
2168/**
2169 * xmlAttrSerializeTxtContent:
2170 * @buf: the XML buffer output
2171 * @doc: the document
2172 * @attr: the attribute node
2173 * @string: the text content
2174 *
2175 * Serialize text attribute values to an xml simple buffer
2176 */
2177void
2178xmlAttrSerializeTxtContent(xmlBufferPtr buf, xmlDocPtr doc,
2179 xmlAttrPtr attr, const xmlChar * string)
2180{
2181 xmlBufPtr buffer;
2182
2183 if ((buf == NULL) || (string == NULL))
2184 return;
2185 buffer = xmlBufFromBuffer(buf);
2186 if (buffer == NULL)
2187 return;
2188 xmlBufAttrSerializeTxtContent(buffer, doc, attr, string);
2189 xmlBufBackToBuffer(buffer);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002190}
2191
2192/**
2193 * xmlNodeDump:
2194 * @buf: the XML buffer output
2195 * @doc: the document
2196 * @cur: the current node
2197 * @level: the imbrication level for indenting
2198 * @format: is formatting allowed
2199 *
2200 * Dump an XML node, recursive behaviour,children are printed too.
2201 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
2202 * or xmlKeepBlanksDefault(0) was called
Daniel Veillard50cdab52012-07-16 14:52:00 +08002203 * Since this is using xmlBuffer structures it is limited to 2GB and somehow
2204 * deprecated, use xmlBufNodeDump() instead.
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002205 *
2206 * Returns the number of bytes written to the buffer or -1 in case of error
2207 */
2208int
2209xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
2210 int format)
2211{
Daniel Veillard50cdab52012-07-16 14:52:00 +08002212 xmlBufPtr buffer;
2213 int ret;
2214
2215 if ((buf == NULL) || (cur == NULL))
2216 return(-1);
2217 buffer = xmlBufFromBuffer(buf);
2218 if (buffer == NULL)
2219 return(-1);
2220 ret = xmlBufNodeDump(buffer, doc, cur, level, format);
2221 xmlBufBackToBuffer(buffer);
2222 if (ret > INT_MAX)
2223 return(-1);
2224 return((int) ret);
2225}
2226
2227/**
2228 * xmlBufNodeDump:
2229 * @buf: the XML buffer output
2230 * @doc: the document
2231 * @cur: the current node
2232 * @level: the imbrication level for indenting
2233 * @format: is formatting allowed
2234 *
2235 * Dump an XML node, recursive behaviour,children are printed too.
2236 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
2237 * or xmlKeepBlanksDefault(0) was called
2238 *
2239 * Returns the number of bytes written to the buffer, in case of error 0
2240 * is returned or @buf stores the error
2241 */
2242
2243size_t
2244xmlBufNodeDump(xmlBufPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
2245 int format)
2246{
2247 size_t use;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002248 int ret;
2249 xmlOutputBufferPtr outbuf;
2250
2251 xmlInitParser();
2252
2253 if (cur == NULL) {
2254#ifdef DEBUG_TREE
2255 xmlGenericError(xmlGenericErrorContext,
2256 "xmlNodeDump : node == NULL\n");
2257#endif
2258 return (-1);
2259 }
2260 if (buf == NULL) {
2261#ifdef DEBUG_TREE
2262 xmlGenericError(xmlGenericErrorContext,
2263 "xmlNodeDump : buf == NULL\n");
2264#endif
2265 return (-1);
2266 }
2267 outbuf = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer));
2268 if (outbuf == NULL) {
2269 xmlSaveErrMemory("creating buffer");
2270 return (-1);
2271 }
2272 memset(outbuf, 0, (size_t) sizeof(xmlOutputBuffer));
2273 outbuf->buffer = buf;
2274 outbuf->encoder = NULL;
2275 outbuf->writecallback = NULL;
2276 outbuf->closecallback = NULL;
2277 outbuf->context = NULL;
2278 outbuf->written = 0;
2279
Daniel Veillard50cdab52012-07-16 14:52:00 +08002280 use = xmlBufUse(buf);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002281 xmlNodeDumpOutput(outbuf, doc, cur, level, format, NULL);
2282 xmlFree(outbuf);
Daniel Veillard50cdab52012-07-16 14:52:00 +08002283 ret = xmlBufUse(buf) - use;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002284 return (ret);
2285}
2286
2287/**
2288 * xmlElemDump:
2289 * @f: the FILE * for the output
2290 * @doc: the document
2291 * @cur: the current node
2292 *
2293 * Dump an XML/HTML node, recursive behaviour, children are printed too.
2294 */
2295void
2296xmlElemDump(FILE * f, xmlDocPtr doc, xmlNodePtr cur)
2297{
2298 xmlOutputBufferPtr outbuf;
2299
2300 xmlInitParser();
2301
2302 if (cur == NULL) {
2303#ifdef DEBUG_TREE
2304 xmlGenericError(xmlGenericErrorContext,
2305 "xmlElemDump : cur == NULL\n");
2306#endif
2307 return;
2308 }
2309#ifdef DEBUG_TREE
2310 if (doc == NULL) {
2311 xmlGenericError(xmlGenericErrorContext,
2312 "xmlElemDump : doc == NULL\n");
2313 }
2314#endif
2315
2316 outbuf = xmlOutputBufferCreateFile(f, NULL);
2317 if (outbuf == NULL)
2318 return;
2319 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
2320#ifdef LIBXML_HTML_ENABLED
2321 htmlNodeDumpOutput(outbuf, doc, cur, NULL);
2322#else
2323 xmlSaveErr(XML_ERR_INTERNAL_ERROR, cur, "HTML support not compiled in\n");
2324#endif /* LIBXML_HTML_ENABLED */
2325 } else
2326 xmlNodeDumpOutput(outbuf, doc, cur, 0, 1, NULL);
2327 xmlOutputBufferClose(outbuf);
2328}
2329
2330/************************************************************************
2331 * *
2332 * Saving functions front-ends *
2333 * *
2334 ************************************************************************/
2335
2336/**
2337 * xmlNodeDumpOutput:
2338 * @buf: the XML buffer output
2339 * @doc: the document
2340 * @cur: the current node
2341 * @level: the imbrication level for indenting
2342 * @format: is formatting allowed
2343 * @encoding: an optional encoding string
2344 *
2345 * Dump an XML node, recursive behaviour, children are printed too.
2346 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
2347 * or xmlKeepBlanksDefault(0) was called
2348 */
2349void
2350xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
2351 int level, int format, const char *encoding)
2352{
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002353 xmlSaveCtxt ctxt;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002354#ifdef LIBXML_HTML_ENABLED
2355 xmlDtdPtr dtd;
2356 int is_xhtml = 0;
2357#endif
2358
2359 xmlInitParser();
2360
Daniel Veillardce244ad2004-11-05 10:03:46 +00002361 if ((buf == NULL) || (cur == NULL)) return;
2362
Daniel Veillard64354ea2005-03-31 15:22:56 +00002363 if (encoding == NULL)
2364 encoding = "UTF-8";
2365
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002366 memset(&ctxt, 0, sizeof(ctxt));
2367 ctxt.doc = doc;
2368 ctxt.buf = buf;
2369 ctxt.level = level;
Adam Spragg8b877132010-11-01 14:24:56 +01002370 ctxt.format = format ? 1 : 0;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002371 ctxt.encoding = (const xmlChar *) encoding;
Daniel Veillard753086a2004-03-28 16:12:44 +00002372 xmlSaveCtxtInit(&ctxt);
Daniel Veillard856d9282008-09-25 14:31:40 +00002373 ctxt.options |= XML_SAVE_AS_XML;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002374
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002375#ifdef LIBXML_HTML_ENABLED
2376 dtd = xmlGetIntSubset(doc);
2377 if (dtd != NULL) {
Daniel Veillard33b20b72005-09-12 21:43:20 +00002378 is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
2379 if (is_xhtml < 0)
2380 is_xhtml = 0;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002381 }
2382
2383 if (is_xhtml)
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002384 xhtmlNodeDumpOutput(&ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002385 else
2386#endif
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002387 xmlNodeDumpOutputInternal(&ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002388}
2389
2390/**
2391 * xmlDocDumpFormatMemoryEnc:
2392 * @out_doc: Document to generate XML text from
2393 * @doc_txt_ptr: Memory pointer for allocated XML text
2394 * @doc_txt_len: Length of the generated XML text
2395 * @txt_encoding: Character encoding to use when generating XML text
2396 * @format: should formatting spaces been added
2397 *
2398 * Dump the current DOM tree into memory using the character encoding specified
2399 * by the caller. Note it is up to the caller of this function to free the
2400 * allocated memory with xmlFree().
2401 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
2402 * or xmlKeepBlanksDefault(0) was called
2403 */
2404
2405void
2406xmlDocDumpFormatMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
2407 int * doc_txt_len, const char * txt_encoding,
2408 int format) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002409 xmlSaveCtxt ctxt;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002410 int dummy = 0;
2411 xmlOutputBufferPtr out_buff = NULL;
2412 xmlCharEncodingHandlerPtr conv_hdlr = NULL;
2413
2414 if (doc_txt_len == NULL) {
2415 doc_txt_len = &dummy; /* Continue, caller just won't get length */
2416 }
2417
2418 if (doc_txt_ptr == NULL) {
2419 *doc_txt_len = 0;
2420 return;
2421 }
2422
2423 *doc_txt_ptr = NULL;
2424 *doc_txt_len = 0;
2425
2426 if (out_doc == NULL) {
2427 /* No document, no output */
2428 return;
2429 }
2430
2431 /*
2432 * Validate the encoding value, if provided.
2433 * This logic is copied from xmlSaveFileEnc.
2434 */
2435
2436 if (txt_encoding == NULL)
2437 txt_encoding = (const char *) out_doc->encoding;
2438 if (txt_encoding != NULL) {
2439 conv_hdlr = xmlFindCharEncodingHandler(txt_encoding);
2440 if ( conv_hdlr == NULL ) {
2441 xmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, (xmlNodePtr) out_doc,
2442 txt_encoding);
2443 return;
2444 }
2445 }
2446
2447 if ((out_buff = xmlAllocOutputBuffer(conv_hdlr)) == NULL ) {
2448 xmlSaveErrMemory("creating buffer");
2449 return;
2450 }
2451
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002452 memset(&ctxt, 0, sizeof(ctxt));
2453 ctxt.doc = out_doc;
2454 ctxt.buf = out_buff;
2455 ctxt.level = 0;
Adam Spragg8b877132010-11-01 14:24:56 +01002456 ctxt.format = format ? 1 : 0;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002457 ctxt.encoding = (const xmlChar *) txt_encoding;
Daniel Veillard753086a2004-03-28 16:12:44 +00002458 xmlSaveCtxtInit(&ctxt);
Daniel Veillard856d9282008-09-25 14:31:40 +00002459 ctxt.options |= XML_SAVE_AS_XML;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002460 xmlDocContentDumpOutput(&ctxt, out_doc);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002461 xmlOutputBufferFlush(out_buff);
2462 if (out_buff->conv != NULL) {
Daniel Veillard50cdab52012-07-16 14:52:00 +08002463 *doc_txt_len = xmlBufUse(out_buff->conv);
2464 *doc_txt_ptr = xmlStrndup(xmlBufContent(out_buff->conv), *doc_txt_len);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002465 } else {
Daniel Veillard50cdab52012-07-16 14:52:00 +08002466 *doc_txt_len = xmlBufUse(out_buff->buffer);
2467 *doc_txt_ptr = xmlStrndup(xmlBufContent(out_buff->buffer),*doc_txt_len);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002468 }
2469 (void)xmlOutputBufferClose(out_buff);
2470
2471 if ((*doc_txt_ptr == NULL) && (*doc_txt_len > 0)) {
2472 *doc_txt_len = 0;
2473 xmlSaveErrMemory("creating output");
2474 }
2475
2476 return;
2477}
2478
2479/**
2480 * xmlDocDumpMemory:
2481 * @cur: the document
2482 * @mem: OUT: the memory pointer
2483 * @size: OUT: the memory length
2484 *
2485 * Dump an XML document in memory and return the #xmlChar * and it's size
2486 * in bytes. It's up to the caller to free the memory with xmlFree().
2487 * The resulting byte array is zero terminated, though the last 0 is not
2488 * included in the returned size.
2489 */
2490void
2491xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) {
2492 xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, 0);
2493}
2494
2495/**
2496 * xmlDocDumpFormatMemory:
2497 * @cur: the document
2498 * @mem: OUT: the memory pointer
2499 * @size: OUT: the memory length
2500 * @format: should formatting spaces been added
2501 *
2502 *
2503 * Dump an XML document in memory and return the #xmlChar * and it's size.
2504 * It's up to the caller to free the memory with xmlFree().
2505 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
2506 * or xmlKeepBlanksDefault(0) was called
2507 */
2508void
2509xmlDocDumpFormatMemory(xmlDocPtr cur, xmlChar**mem, int *size, int format) {
2510 xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, format);
2511}
2512
2513/**
2514 * xmlDocDumpMemoryEnc:
2515 * @out_doc: Document to generate XML text from
2516 * @doc_txt_ptr: Memory pointer for allocated XML text
2517 * @doc_txt_len: Length of the generated XML text
2518 * @txt_encoding: Character encoding to use when generating XML text
2519 *
2520 * Dump the current DOM tree into memory using the character encoding specified
2521 * by the caller. Note it is up to the caller of this function to free the
2522 * allocated memory with xmlFree().
2523 */
2524
2525void
2526xmlDocDumpMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
2527 int * doc_txt_len, const char * txt_encoding) {
2528 xmlDocDumpFormatMemoryEnc(out_doc, doc_txt_ptr, doc_txt_len,
2529 txt_encoding, 0);
2530}
2531
2532/**
2533 * xmlDocFormatDump:
2534 * @f: the FILE*
2535 * @cur: the document
2536 * @format: should formatting spaces been added
2537 *
2538 * Dump an XML document to an open FILE.
2539 *
2540 * returns: the number of bytes written or -1 in case of failure.
2541 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
2542 * or xmlKeepBlanksDefault(0) was called
2543 */
2544int
2545xmlDocFormatDump(FILE *f, xmlDocPtr cur, int format) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002546 xmlSaveCtxt ctxt;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002547 xmlOutputBufferPtr buf;
2548 const char * encoding;
2549 xmlCharEncodingHandlerPtr handler = NULL;
2550 int ret;
2551
2552 if (cur == NULL) {
2553#ifdef DEBUG_TREE
2554 xmlGenericError(xmlGenericErrorContext,
2555 "xmlDocDump : document == NULL\n");
2556#endif
2557 return(-1);
2558 }
2559 encoding = (const char *) cur->encoding;
2560
2561 if (encoding != NULL) {
Daniel Veillard3814a362007-07-26 11:41:46 +00002562 handler = xmlFindCharEncodingHandler(encoding);
2563 if (handler == NULL) {
2564 xmlFree((char *) cur->encoding);
2565 cur->encoding = NULL;
2566 encoding = NULL;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002567 }
Daniel Veillard3814a362007-07-26 11:41:46 +00002568 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002569 buf = xmlOutputBufferCreateFile(f, handler);
2570 if (buf == NULL) return(-1);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002571 memset(&ctxt, 0, sizeof(ctxt));
2572 ctxt.doc = cur;
2573 ctxt.buf = buf;
2574 ctxt.level = 0;
Adam Spragg8b877132010-11-01 14:24:56 +01002575 ctxt.format = format ? 1 : 0;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002576 ctxt.encoding = (const xmlChar *) encoding;
Daniel Veillard753086a2004-03-28 16:12:44 +00002577 xmlSaveCtxtInit(&ctxt);
Daniel Veillard856d9282008-09-25 14:31:40 +00002578 ctxt.options |= XML_SAVE_AS_XML;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002579 xmlDocContentDumpOutput(&ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002580
2581 ret = xmlOutputBufferClose(buf);
2582 return(ret);
2583}
2584
2585/**
2586 * xmlDocDump:
2587 * @f: the FILE*
2588 * @cur: the document
2589 *
2590 * Dump an XML document to an open FILE.
2591 *
2592 * returns: the number of bytes written or -1 in case of failure.
2593 */
2594int
2595xmlDocDump(FILE *f, xmlDocPtr cur) {
2596 return(xmlDocFormatDump (f, cur, 0));
2597}
2598
2599/**
2600 * xmlSaveFileTo:
2601 * @buf: an output I/O buffer
2602 * @cur: the document
2603 * @encoding: the encoding if any assuming the I/O layer handles the trancoding
2604 *
2605 * Dump an XML document to an I/O buffer.
Daniel Veillard3d97e662004-11-04 10:49:00 +00002606 * Warning ! This call xmlOutputBufferClose() on buf which is not available
2607 * after this call.
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002608 *
2609 * returns: the number of bytes written or -1 in case of failure.
2610 */
2611int
2612xmlSaveFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002613 xmlSaveCtxt ctxt;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002614 int ret;
2615
Daniel Veillard3d97e662004-11-04 10:49:00 +00002616 if (buf == NULL) return(-1);
2617 if (cur == NULL) {
2618 xmlOutputBufferClose(buf);
2619 return(-1);
2620 }
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002621 memset(&ctxt, 0, sizeof(ctxt));
2622 ctxt.doc = cur;
2623 ctxt.buf = buf;
2624 ctxt.level = 0;
2625 ctxt.format = 0;
2626 ctxt.encoding = (const xmlChar *) encoding;
Daniel Veillard753086a2004-03-28 16:12:44 +00002627 xmlSaveCtxtInit(&ctxt);
Daniel Veillard856d9282008-09-25 14:31:40 +00002628 ctxt.options |= XML_SAVE_AS_XML;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002629 xmlDocContentDumpOutput(&ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002630 ret = xmlOutputBufferClose(buf);
2631 return(ret);
2632}
2633
2634/**
2635 * xmlSaveFormatFileTo:
2636 * @buf: an output I/O buffer
2637 * @cur: the document
2638 * @encoding: the encoding if any assuming the I/O layer handles the trancoding
2639 * @format: should formatting spaces been added
2640 *
2641 * Dump an XML document to an I/O buffer.
Daniel Veillard3d97e662004-11-04 10:49:00 +00002642 * Warning ! This call xmlOutputBufferClose() on buf which is not available
2643 * after this call.
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002644 *
2645 * returns: the number of bytes written or -1 in case of failure.
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002646 */
2647int
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002648xmlSaveFormatFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur,
2649 const char *encoding, int format)
2650{
2651 xmlSaveCtxt ctxt;
2652 int ret;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002653
Daniel Veillard3d97e662004-11-04 10:49:00 +00002654 if (buf == NULL) return(-1);
Daniel Veillardce244ad2004-11-05 10:03:46 +00002655 if ((cur == NULL) ||
2656 ((cur->type != XML_DOCUMENT_NODE) &&
2657 (cur->type != XML_HTML_DOCUMENT_NODE))) {
Daniel Veillard3d97e662004-11-04 10:49:00 +00002658 xmlOutputBufferClose(buf);
2659 return(-1);
2660 }
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002661 memset(&ctxt, 0, sizeof(ctxt));
2662 ctxt.doc = cur;
2663 ctxt.buf = buf;
2664 ctxt.level = 0;
Adam Spragg8b877132010-11-01 14:24:56 +01002665 ctxt.format = format ? 1 : 0;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002666 ctxt.encoding = (const xmlChar *) encoding;
Daniel Veillard753086a2004-03-28 16:12:44 +00002667 xmlSaveCtxtInit(&ctxt);
Daniel Veillard856d9282008-09-25 14:31:40 +00002668 ctxt.options |= XML_SAVE_AS_XML;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002669 xmlDocContentDumpOutput(&ctxt, cur);
2670 ret = xmlOutputBufferClose(buf);
2671 return (ret);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002672}
2673
2674/**
2675 * xmlSaveFormatFileEnc:
2676 * @filename: the filename or URL to output
2677 * @cur: the document being saved
2678 * @encoding: the name of the encoding to use or NULL.
2679 * @format: should formatting spaces be added.
2680 *
2681 * Dump an XML document to a file or an URL.
2682 *
2683 * Returns the number of bytes written or -1 in case of error.
2684 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
2685 * or xmlKeepBlanksDefault(0) was called
2686 */
2687int
2688xmlSaveFormatFileEnc( const char * filename, xmlDocPtr cur,
2689 const char * encoding, int format ) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002690 xmlSaveCtxt ctxt;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002691 xmlOutputBufferPtr buf;
2692 xmlCharEncodingHandlerPtr handler = NULL;
2693 int ret;
2694
2695 if (cur == NULL)
2696 return(-1);
2697
2698 if (encoding == NULL)
2699 encoding = (const char *) cur->encoding;
2700
2701 if (encoding != NULL) {
2702
2703 handler = xmlFindCharEncodingHandler(encoding);
2704 if (handler == NULL)
2705 return(-1);
2706 }
2707
2708#ifdef HAVE_ZLIB_H
2709 if (cur->compression < 0) cur->compression = xmlGetCompressMode();
2710#endif
2711 /*
2712 * save the content to a temp buffer.
2713 */
2714 buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression);
2715 if (buf == NULL) return(-1);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002716 memset(&ctxt, 0, sizeof(ctxt));
2717 ctxt.doc = cur;
2718 ctxt.buf = buf;
2719 ctxt.level = 0;
Adam Spragg8b877132010-11-01 14:24:56 +01002720 ctxt.format = format ? 1 : 0;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002721 ctxt.encoding = (const xmlChar *) encoding;
Daniel Veillard753086a2004-03-28 16:12:44 +00002722 xmlSaveCtxtInit(&ctxt);
Daniel Veillard856d9282008-09-25 14:31:40 +00002723 ctxt.options |= XML_SAVE_AS_XML;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002724
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002725 xmlDocContentDumpOutput(&ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002726
2727 ret = xmlOutputBufferClose(buf);
2728 return(ret);
2729}
2730
2731
2732/**
2733 * xmlSaveFileEnc:
2734 * @filename: the filename (or URL)
2735 * @cur: the document
2736 * @encoding: the name of an encoding (or NULL)
2737 *
2738 * Dump an XML document, converting it to the given encoding
2739 *
2740 * returns: the number of bytes written or -1 in case of failure.
2741 */
2742int
2743xmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) {
2744 return ( xmlSaveFormatFileEnc( filename, cur, encoding, 0 ) );
2745}
2746
2747/**
2748 * xmlSaveFormatFile:
2749 * @filename: the filename (or URL)
2750 * @cur: the document
2751 * @format: should formatting spaces been added
2752 *
2753 * Dump an XML document to a file. Will use compression if
2754 * compiled in and enabled. If @filename is "-" the stdout file is
2755 * used. If @format is set then the document will be indented on output.
2756 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
2757 * or xmlKeepBlanksDefault(0) was called
2758 *
2759 * returns: the number of bytes written or -1 in case of failure.
2760 */
2761int
2762xmlSaveFormatFile(const char *filename, xmlDocPtr cur, int format) {
2763 return ( xmlSaveFormatFileEnc( filename, cur, NULL, format ) );
2764}
2765
2766/**
2767 * xmlSaveFile:
2768 * @filename: the filename (or URL)
2769 * @cur: the document
2770 *
2771 * Dump an XML document to a file. Will use compression if
2772 * compiled in and enabled. If @filename is "-" the stdout file is
2773 * used.
2774 * returns: the number of bytes written or -1 in case of failure.
2775 */
2776int
2777xmlSaveFile(const char *filename, xmlDocPtr cur) {
2778 return(xmlSaveFormatFileEnc(filename, cur, NULL, 0));
2779}
2780
2781#endif /* LIBXML_OUTPUT_ENABLED */
2782
Daniel Veillard5d4644e2005-04-01 13:11:58 +00002783#define bottom_xmlsave
2784#include "elfgcchack.h"