blob: fe5795e136df526bcb3c9f006a340535926bf942 [file] [log] [blame]
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001/*
2 * xmlsave.c: Implemetation of the document serializer
3 *
4 * See Copyright for the status of this software.
5 *
6 * daniel@veillard.com
7 */
8
9#define IN_LIBXML
10#include "libxml.h"
11
12#include <string.h>
13#include <libxml/xmlmemory.h>
14#include <libxml/parserInternals.h>
15#include <libxml/tree.h>
16#include <libxml/xmlsave.h>
Daniel Veillard1a8741c2004-03-04 13:40:59 +000017
Daniel Veillard753086a2004-03-28 16:12:44 +000018#define MAX_INDENT 60
19
Daniel Veillard656ce942004-04-30 23:11:45 +000020#include <libxml/HTMLtree.h>
21
Daniel Veillard1a8741c2004-03-04 13:40:59 +000022/************************************************************************
23 * *
24 * XHTML detection *
25 * *
26 ************************************************************************/
27#define XHTML_STRICT_PUBLIC_ID BAD_CAST \
28 "-//W3C//DTD XHTML 1.0 Strict//EN"
29#define XHTML_STRICT_SYSTEM_ID BAD_CAST \
30 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
31#define XHTML_FRAME_PUBLIC_ID BAD_CAST \
32 "-//W3C//DTD XHTML 1.0 Frameset//EN"
33#define XHTML_FRAME_SYSTEM_ID BAD_CAST \
34 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"
35#define XHTML_TRANS_PUBLIC_ID BAD_CAST \
36 "-//W3C//DTD XHTML 1.0 Transitional//EN"
37#define XHTML_TRANS_SYSTEM_ID BAD_CAST \
38 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
39
40#define XHTML_NS_NAME BAD_CAST "http://www.w3.org/1999/xhtml"
41/**
42 * xmlIsXHTML:
43 * @systemID: the system identifier
44 * @publicID: the public identifier
45 *
46 * Try to find if the document correspond to an XHTML DTD
47 *
48 * Returns 1 if true, 0 if not and -1 in case of error
49 */
50int
51xmlIsXHTML(const xmlChar *systemID, const xmlChar *publicID) {
52 if ((systemID == NULL) && (publicID == NULL))
53 return(-1);
54 if (publicID != NULL) {
55 if (xmlStrEqual(publicID, XHTML_STRICT_PUBLIC_ID)) return(1);
56 if (xmlStrEqual(publicID, XHTML_FRAME_PUBLIC_ID)) return(1);
57 if (xmlStrEqual(publicID, XHTML_TRANS_PUBLIC_ID)) return(1);
58 }
59 if (systemID != NULL) {
60 if (xmlStrEqual(systemID, XHTML_STRICT_SYSTEM_ID)) return(1);
61 if (xmlStrEqual(systemID, XHTML_FRAME_SYSTEM_ID)) return(1);
62 if (xmlStrEqual(systemID, XHTML_TRANS_SYSTEM_ID)) return(1);
63 }
64 return(0);
65}
Daniel Veillard1a8741c2004-03-04 13:40:59 +000066
67#ifdef LIBXML_OUTPUT_ENABLED
68
69#define TODO \
70 xmlGenericError(xmlGenericErrorContext, \
71 "Unimplemented block at %s:%d\n", \
72 __FILE__, __LINE__);
73
74struct _xmlSaveCtxt {
75 void *_private;
76 int type;
77 int fd;
78 const xmlChar *filename;
79 const xmlChar *encoding;
80 xmlCharEncodingHandlerPtr handler;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +000081 xmlOutputBufferPtr buf;
82 xmlDocPtr doc;
Daniel Veillard1a8741c2004-03-04 13:40:59 +000083 int options;
84 int level;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +000085 int format;
Daniel Veillard3995bc32004-05-15 18:57:31 +000086 char indent[MAX_INDENT + 1]; /* array for indenting output */
Daniel Veillard753086a2004-03-28 16:12:44 +000087 int indent_nr;
88 int indent_size;
Daniel Veillard3995bc32004-05-15 18:57:31 +000089 xmlCharEncodingOutputFunc escape; /* used for element content */
90 xmlCharEncodingOutputFunc escapeAttr;/* used for attribute content */
Daniel Veillard1a8741c2004-03-04 13:40:59 +000091};
92
93/************************************************************************
94 * *
95 * Output error handlers *
96 * *
97 ************************************************************************/
98/**
99 * xmlSaveErrMemory:
100 * @extra: extra informations
101 *
102 * Handle an out of memory condition
103 */
104static void
105xmlSaveErrMemory(const char *extra)
106{
107 __xmlSimpleError(XML_FROM_OUTPUT, XML_ERR_NO_MEMORY, NULL, NULL, extra);
108}
109
110/**
111 * xmlSaveErr:
112 * @code: the error number
113 * @node: the location of the error.
114 * @extra: extra informations
115 *
116 * Handle an out of memory condition
117 */
118static void
119xmlSaveErr(int code, xmlNodePtr node, const char *extra)
120{
121 const char *msg = NULL;
122
123 switch(code) {
124 case XML_SAVE_NOT_UTF8:
125 msg = "string is not in UTF-8";
126 break;
127 case XML_SAVE_CHAR_INVALID:
128 msg = "invalid character value";
129 break;
130 case XML_SAVE_UNKNOWN_ENCODING:
131 msg = "unknown encoding %s";
132 break;
133 case XML_SAVE_NO_DOCTYPE:
134 msg = "document has no DOCTYPE";
135 break;
136 default:
137 msg = "unexpected error number";
138 }
139 __xmlSimpleError(XML_FROM_OUTPUT, code, node, msg, extra);
140}
141
142/************************************************************************
143 * *
Daniel Veillard83a75e02004-05-14 21:50:42 +0000144 * Special escaping routines *
145 * *
146 ************************************************************************/
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000147static unsigned char *
148xmlSerializeHexCharRef(unsigned char *out, int val) {
149 unsigned char *ptr;
150
151 *out++ = '&';
152 *out++ = '#';
153 *out++ = 'x';
154 if (val < 0x10) ptr = out;
155 else if (val < 0x100) ptr = out + 1;
156 else if (val < 0x1000) ptr = out + 2;
157 else if (val < 0x10000) ptr = out + 3;
158 else if (val < 0x100000) ptr = out + 4;
159 else ptr = out + 5;
160 out = ptr + 1;
161 while (val > 0) {
162 switch (val & 0xF) {
163 case 0: *ptr-- = '0'; break;
164 case 1: *ptr-- = '1'; break;
165 case 2: *ptr-- = '2'; break;
166 case 3: *ptr-- = '3'; break;
167 case 4: *ptr-- = '4'; break;
168 case 5: *ptr-- = '5'; break;
169 case 6: *ptr-- = '6'; break;
170 case 7: *ptr-- = '7'; break;
171 case 8: *ptr-- = '8'; break;
172 case 9: *ptr-- = '9'; break;
173 case 0xA: *ptr-- = 'A'; break;
174 case 0xB: *ptr-- = 'B'; break;
175 case 0xC: *ptr-- = 'C'; break;
176 case 0xD: *ptr-- = 'D'; break;
177 case 0xE: *ptr-- = 'E'; break;
178 case 0xF: *ptr-- = 'F'; break;
179 default: *ptr-- = '0'; break;
180 }
181 val >>= 4;
182 }
183 *out++ = ';';
184 *out = 0;
185 return(out);
186}
187
Daniel Veillard83a75e02004-05-14 21:50:42 +0000188/**
189 * xmlEscapeEntities:
190 * @out: a pointer to an array of bytes to store the result
191 * @outlen: the length of @out
192 * @in: a pointer to an array of unescaped UTF-8 bytes
193 * @inlen: the length of @in
194 *
195 * Take a block of UTF-8 chars in and escape them. Used when there is no
196 * encoding specified.
197 *
198 * Returns 0 if success, or -1 otherwise
199 * The value of @inlen after return is the number of octets consumed
200 * if the return value is positive, else unpredictable.
201 * The value of @outlen after return is the number of octets consumed.
202 */
203static int
204xmlEscapeEntities(unsigned char* out, int *outlen,
205 const xmlChar* in, int *inlen) {
206 unsigned char* outstart = out;
207 const unsigned char* base = in;
208 unsigned char* outend = out + *outlen;
209 const unsigned char* inend;
210 int val;
211
212 inend = in + (*inlen);
213
214 while ((in < inend) && (out < outend)) {
215 if (*in == '<') {
216 if (outend - out < 4) break;
217 *out++ = '&';
218 *out++ = 'l';
219 *out++ = 't';
220 *out++ = ';';
221 in++;
222 continue;
223 } else if (*in == '>') {
224 if (outend - out < 4) break;
225 *out++ = '&';
226 *out++ = 'g';
227 *out++ = 't';
228 *out++ = ';';
229 in++;
230 continue;
231 } else if (*in == '&') {
232 if (outend - out < 5) break;
233 *out++ = '&';
234 *out++ = 'a';
235 *out++ = 'm';
236 *out++ = 'p';
237 *out++ = ';';
238 in++;
239 continue;
240 } else if (((*in >= 0x20) && (*in < 0x80)) ||
241 (*in == '\n') || (*in == '\t')) {
242 /*
243 * default case, just copy !
244 */
245 *out++ = *in++;
246 continue;
247 } else if (*in >= 0x80) {
248 /*
249 * We assume we have UTF-8 input.
250 */
Daniel Veillard83a75e02004-05-14 21:50:42 +0000251 if (outend - out < 10) break;
252
253 if (*in < 0xC0) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000254 xmlSaveErr(XML_SAVE_NOT_UTF8, NULL, NULL);
Daniel Veillard83a75e02004-05-14 21:50:42 +0000255 in++;
256 goto error;
257 } else if (*in < 0xE0) {
258 if (inend - in < 2) break;
259 val = (in[0]) & 0x1F;
260 val <<= 6;
261 val |= (in[1]) & 0x3F;
262 in += 2;
263 } else if (*in < 0xF0) {
264 if (inend - in < 3) break;
265 val = (in[0]) & 0x0F;
266 val <<= 6;
267 val |= (in[1]) & 0x3F;
268 val <<= 6;
269 val |= (in[2]) & 0x3F;
270 in += 3;
271 } else if (*in < 0xF8) {
272 if (inend - in < 4) break;
273 val = (in[0]) & 0x07;
274 val <<= 6;
275 val |= (in[1]) & 0x3F;
276 val <<= 6;
277 val |= (in[2]) & 0x3F;
278 val <<= 6;
279 val |= (in[3]) & 0x3F;
280 in += 4;
281 } else {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000282 xmlSaveErr(XML_SAVE_CHAR_INVALID, NULL, NULL);
Daniel Veillard83a75e02004-05-14 21:50:42 +0000283 in++;
284 goto error;
285 }
286 if (!IS_CHAR(val)) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000287 xmlSaveErr(XML_SAVE_CHAR_INVALID, NULL, NULL);
Daniel Veillard83a75e02004-05-14 21:50:42 +0000288 in++;
289 goto error;
290 }
291
292 /*
293 * We could do multiple things here. Just save as a char ref
294 */
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000295 out = xmlSerializeHexCharRef(out, val);
Daniel Veillard83a75e02004-05-14 21:50:42 +0000296 } else if (IS_BYTE_CHAR(*in)) {
297 if (outend - out < 6) break;
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000298 out = xmlSerializeHexCharRef(out, *in++);
Daniel Veillard83a75e02004-05-14 21:50:42 +0000299 } else {
300 xmlGenericError(xmlGenericErrorContext,
301 "xmlEscapeEntities : char out of range\n");
302 in++;
303 goto error;
304 }
305 }
306 *outlen = out - outstart;
307 *inlen = in - base;
308 return(0);
309error:
310 *outlen = out - outstart;
311 *inlen = in - base;
312 return(-1);
313}
314
315/************************************************************************
316 * *
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000317 * Allocation and deallocation *
318 * *
319 ************************************************************************/
Daniel Veillard753086a2004-03-28 16:12:44 +0000320/**
321 * xmlSaveCtxtInit:
322 * @ctxt: the saving context
323 *
324 * Initialize a saving context
325 */
326static void
327xmlSaveCtxtInit(xmlSaveCtxtPtr ctxt)
328{
329 int i;
William M. Brack12d37ab2005-02-21 13:54:07 +0000330 int len;
Daniel Veillard753086a2004-03-28 16:12:44 +0000331
332 if (ctxt == NULL) return;
Daniel Veillard3995bc32004-05-15 18:57:31 +0000333 if ((ctxt->encoding == NULL) && (ctxt->escape == NULL))
334 ctxt->escape = xmlEscapeEntities;
William M. Brack12d37ab2005-02-21 13:54:07 +0000335 len = xmlStrlen((xmlChar *)xmlTreeIndentString);
336 if ((xmlTreeIndentString == NULL) || (len == 0)) {
Daniel Veillard753086a2004-03-28 16:12:44 +0000337 memset(&ctxt->indent[0], 0, MAX_INDENT + 1);
338 } else {
William M. Brack12d37ab2005-02-21 13:54:07 +0000339 ctxt->indent_size = len;
Daniel Veillard753086a2004-03-28 16:12:44 +0000340 ctxt->indent_nr = MAX_INDENT / ctxt->indent_size;
341 for (i = 0;i < ctxt->indent_nr;i++)
342 memcpy(&ctxt->indent[i * ctxt->indent_size], xmlTreeIndentString,
343 ctxt->indent_size);
344 ctxt->indent[ctxt->indent_nr * ctxt->indent_size] = 0;
345 }
Rob Richards2ce51c02005-09-12 12:16:35 +0000346
347 if (xmlSaveNoEmptyTags) {
348 ctxt->options |= XML_SAVE_NO_EMPTY;
349 }
Daniel Veillard753086a2004-03-28 16:12:44 +0000350}
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000351
352/**
353 * xmlFreeSaveCtxt:
354 *
355 * Free a saving context, destroying the ouptut in any remaining buffer
356 */
357static void
358xmlFreeSaveCtxt(xmlSaveCtxtPtr ctxt)
359{
360 if (ctxt == NULL) return;
361 if (ctxt->encoding != NULL)
362 xmlFree((char *) ctxt->encoding);
Daniel Veillarde2161a62004-04-29 17:14:25 +0000363 if (ctxt->buf != NULL)
364 xmlOutputBufferClose(ctxt->buf);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000365 xmlFree(ctxt);
366}
367
368/**
369 * xmlNewSaveCtxt:
370 *
371 * Create a new saving context
372 *
373 * Returns the new structure or NULL in case of error
374 */
375static xmlSaveCtxtPtr
376xmlNewSaveCtxt(const char *encoding, int options)
377{
378 xmlSaveCtxtPtr ret;
379
380 ret = (xmlSaveCtxtPtr) xmlMalloc(sizeof(xmlSaveCtxt));
381 if (ret == NULL) {
382 xmlSaveErrMemory("creating saving context");
383 return ( NULL );
384 }
385 memset(ret, 0, sizeof(xmlSaveCtxt));
Daniel Veillard6fc5db02005-01-16 00:05:58 +0000386
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000387 if (encoding != NULL) {
388 ret->handler = xmlFindCharEncodingHandler(encoding);
389 if (ret->handler == NULL) {
390 xmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL, encoding);
391 xmlFreeSaveCtxt(ret);
392 return(NULL);
393 }
394 ret->encoding = xmlStrdup((const xmlChar *)encoding);
Daniel Veillard3995bc32004-05-15 18:57:31 +0000395 ret->escape = xmlEscapeEntities;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000396 }
Daniel Veillard753086a2004-03-28 16:12:44 +0000397 xmlSaveCtxtInit(ret);
398
Rob Richards2ce51c02005-09-12 12:16:35 +0000399 /*
400 * Use the options
401 */
402
403 /* Re-check this option as it may already have been set */
404 if ((ret->options & XML_SAVE_NO_EMPTY) && ! (options & XML_SAVE_NO_EMPTY)) {
405 options |= XML_SAVE_NO_EMPTY;
406 }
407
408 ret->options = options;
409 if (options & XML_SAVE_FORMAT)
410 ret->format = 1;
411
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000412 return(ret);
413}
414
415/************************************************************************
416 * *
417 * Dumping XML tree content to a simple buffer *
418 * *
419 ************************************************************************/
420/**
421 * xmlAttrSerializeContent:
422 * @buf: the XML buffer output
423 * @doc: the document
424 * @attr: the attribute pointer
425 *
426 * Serialize the attribute in the buffer
427 */
428static void
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000429xmlAttrSerializeContent(xmlOutputBufferPtr buf, xmlAttrPtr attr)
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000430{
431 xmlNodePtr children;
432
433 children = attr->children;
434 while (children != NULL) {
435 switch (children->type) {
436 case XML_TEXT_NODE:
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000437 xmlAttrSerializeTxtContent(buf->buffer, attr->doc,
438 attr, children->content);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000439 break;
440 case XML_ENTITY_REF_NODE:
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000441 xmlBufferAdd(buf->buffer, BAD_CAST "&", 1);
442 xmlBufferAdd(buf->buffer, children->name,
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000443 xmlStrlen(children->name));
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000444 xmlBufferAdd(buf->buffer, BAD_CAST ";", 1);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000445 break;
446 default:
447 /* should not happen unless we have a badly built tree */
448 break;
449 }
450 children = children->next;
451 }
452}
453
454/************************************************************************
455 * *
456 * Dumping XML tree content to an I/O output buffer *
457 * *
458 ************************************************************************/
459
460#ifdef LIBXML_HTML_ENABLED
461static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000462xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000463#endif
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000464static void xmlNodeListDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur);
465static void xmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000466void xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur);
Daniel Veillardce244ad2004-11-05 10:03:46 +0000467static void xmlDocContentDumpOutput(xmlSaveCtxtPtr ctxt, xmlDocPtr cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000468
469/**
470 * xmlNsDumpOutput:
471 * @buf: the XML buffer output
472 * @cur: a namespace
473 *
474 * Dump a local Namespace definition.
475 * Should be called in the context of attributes dumps.
476 */
477static void
478xmlNsDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) {
Daniel Veillardce244ad2004-11-05 10:03:46 +0000479 if ((cur == NULL) || (buf == NULL)) return;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000480 if ((cur->type == XML_LOCAL_NAMESPACE) && (cur->href != NULL)) {
481 if (xmlStrEqual(cur->prefix, BAD_CAST "xml"))
482 return;
483
484 /* Within the context of an element attributes */
485 if (cur->prefix != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000486 xmlOutputBufferWrite(buf, 7, " xmlns:");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000487 xmlOutputBufferWriteString(buf, (const char *)cur->prefix);
488 } else
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000489 xmlOutputBufferWrite(buf, 6, " xmlns");
490 xmlOutputBufferWrite(buf, 1, "=");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000491 xmlBufferWriteQuotedString(buf->buffer, cur->href);
492 }
493}
494
495/**
496 * xmlNsListDumpOutput:
497 * @buf: the XML buffer output
498 * @cur: the first namespace
499 *
500 * Dump a list of local Namespace definitions.
501 * Should be called in the context of attributes dumps.
502 */
503void
504xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) {
505 while (cur != NULL) {
506 xmlNsDumpOutput(buf, cur);
507 cur = cur->next;
508 }
509}
510
511/**
512 * xmlDtdDumpOutput:
513 * @buf: the XML buffer output
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000514 * @dtd: the pointer to the DTD
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000515 *
516 * Dump the XML document DTD, if any.
517 */
518static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000519xmlDtdDumpOutput(xmlSaveCtxtPtr ctxt, xmlDtdPtr dtd) {
520 xmlOutputBufferPtr buf;
521 int format, level;
522 xmlDocPtr doc;
523
524 if (dtd == NULL) return;
525 if ((ctxt == NULL) || (ctxt->buf == NULL))
526 return;
527 buf = ctxt->buf;
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000528 xmlOutputBufferWrite(buf, 10, "<!DOCTYPE ");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000529 xmlOutputBufferWriteString(buf, (const char *)dtd->name);
530 if (dtd->ExternalID != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000531 xmlOutputBufferWrite(buf, 8, " PUBLIC ");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000532 xmlBufferWriteQuotedString(buf->buffer, dtd->ExternalID);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000533 xmlOutputBufferWrite(buf, 1, " ");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000534 xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID);
535 } else if (dtd->SystemID != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000536 xmlOutputBufferWrite(buf, 8, " SYSTEM ");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000537 xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID);
538 }
539 if ((dtd->entities == NULL) && (dtd->elements == NULL) &&
Daniel Veillard41c4a752004-09-08 20:55:38 +0000540 (dtd->attributes == NULL) && (dtd->notations == NULL) &&
541 (dtd->pentities == NULL)) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000542 xmlOutputBufferWrite(buf, 1, ">");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000543 return;
544 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000545 xmlOutputBufferWrite(buf, 3, " [\n");
Daniel Veillard41c4a752004-09-08 20:55:38 +0000546 /*
547 * Dump the notations first they are not in the DTD children list
548 * Do this only on a standalone DTD or on the internal subset though.
549 */
550 if ((dtd->notations != NULL) && ((dtd->doc == NULL) ||
551 (dtd->doc->intSubset == dtd))) {
Daniel Veillardda3b29a2004-08-14 11:15:13 +0000552 xmlDumpNotationTable(buf->buffer, (xmlNotationTablePtr) dtd->notations);
553 }
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000554 format = ctxt->format;
555 level = ctxt->level;
556 doc = ctxt->doc;
557 ctxt->format = 0;
558 ctxt->level = -1;
559 ctxt->doc = dtd->doc;
560 xmlNodeListDumpOutput(ctxt, dtd->children);
561 ctxt->format = format;
562 ctxt->level = level;
563 ctxt->doc = doc;
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000564 xmlOutputBufferWrite(buf, 2, "]>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000565}
566
567/**
568 * xmlAttrDumpOutput:
569 * @buf: the XML buffer output
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000570 * @cur: the attribute pointer
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000571 *
572 * Dump an XML attribute
573 */
574static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000575xmlAttrDumpOutput(xmlSaveCtxtPtr ctxt, xmlAttrPtr cur) {
576 xmlOutputBufferPtr buf;
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000577
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000578 if (cur == NULL) return;
579 buf = ctxt->buf;
Daniel Veillardce244ad2004-11-05 10:03:46 +0000580 if (buf == NULL) return;
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000581 xmlOutputBufferWrite(buf, 1, " ");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000582 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
583 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000584 xmlOutputBufferWrite(buf, 1, ":");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000585 }
586 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000587 xmlOutputBufferWrite(buf, 2, "=\"");
588 xmlAttrSerializeContent(buf, cur);
589 xmlOutputBufferWrite(buf, 1, "\"");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000590}
591
592/**
593 * xmlAttrListDumpOutput:
594 * @buf: the XML buffer output
595 * @doc: the document
596 * @cur: the first attribute pointer
597 * @encoding: an optional encoding string
598 *
599 * Dump a list of XML attributes
600 */
601static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000602xmlAttrListDumpOutput(xmlSaveCtxtPtr ctxt, xmlAttrPtr cur) {
603 if (cur == NULL) return;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000604 while (cur != NULL) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000605 xmlAttrDumpOutput(ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000606 cur = cur->next;
607 }
608}
609
610
611
612/**
613 * xmlNodeListDumpOutput:
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000614 * @cur: the first node
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000615 *
616 * Dump an XML node list, recursive behaviour, children are printed too.
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000617 */
618static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000619xmlNodeListDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000620 xmlOutputBufferPtr buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000621
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000622 if (cur == NULL) return;
623 buf = ctxt->buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000624 while (cur != NULL) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000625 if ((ctxt->format) && (xmlIndentTreeOutput) &&
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000626 (cur->type == XML_ELEMENT_NODE))
Daniel Veillard753086a2004-03-28 16:12:44 +0000627 xmlOutputBufferWrite(buf, ctxt->indent_size *
628 (ctxt->level > ctxt->indent_nr ?
629 ctxt->indent_nr : ctxt->level),
630 ctxt->indent);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000631 xmlNodeDumpOutputInternal(ctxt, cur);
632 if (ctxt->format) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000633 xmlOutputBufferWrite(buf, 1, "\n");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000634 }
635 cur = cur->next;
636 }
637}
638
639/**
640 * xmlNodeDumpOutputInternal:
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000641 * @cur: the current node
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000642 *
643 * Dump an XML node, recursive behaviour, children are printed too.
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000644 */
645static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000646xmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
Daniel Veillard753086a2004-03-28 16:12:44 +0000647 int format;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000648 xmlNodePtr tmp;
649 xmlChar *start, *end;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000650 xmlOutputBufferPtr buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000651
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000652 if (cur == NULL) return;
653 buf = ctxt->buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000654 if (cur->type == XML_XINCLUDE_START)
655 return;
656 if (cur->type == XML_XINCLUDE_END)
657 return;
Daniel Veillardce244ad2004-11-05 10:03:46 +0000658 if ((cur->type == XML_DOCUMENT_NODE) ||
659 (cur->type == XML_HTML_DOCUMENT_NODE)) {
660 xmlDocContentDumpOutput(ctxt, (xmlDocPtr) cur);
661 return;
662 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000663 if (cur->type == XML_DTD_NODE) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000664 xmlDtdDumpOutput(ctxt, (xmlDtdPtr) cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000665 return;
666 }
667 if (cur->type == XML_DOCUMENT_FRAG_NODE) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000668 xmlNodeListDumpOutput(ctxt, cur->children);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000669 return;
670 }
671 if (cur->type == XML_ELEMENT_DECL) {
672 xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur);
673 return;
674 }
675 if (cur->type == XML_ATTRIBUTE_DECL) {
676 xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur);
677 return;
678 }
679 if (cur->type == XML_ENTITY_DECL) {
680 xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur);
681 return;
682 }
683 if (cur->type == XML_TEXT_NODE) {
684 if (cur->content != NULL) {
William M. Brack4e1c2db2005-02-11 10:58:55 +0000685 if (cur->name != xmlStringTextNoenc) {
Daniel Veillard3995bc32004-05-15 18:57:31 +0000686 xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000687 } else {
688 /*
689 * Disable escaping, needed for XSLT
690 */
691 xmlOutputBufferWriteString(buf, (const char *) cur->content);
692 }
693 }
694
695 return;
696 }
697 if (cur->type == XML_PI_NODE) {
698 if (cur->content != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000699 xmlOutputBufferWrite(buf, 2, "<?");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000700 xmlOutputBufferWriteString(buf, (const char *)cur->name);
701 if (cur->content != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000702 xmlOutputBufferWrite(buf, 1, " ");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000703 xmlOutputBufferWriteString(buf, (const char *)cur->content);
704 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000705 xmlOutputBufferWrite(buf, 2, "?>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000706 } else {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000707 xmlOutputBufferWrite(buf, 2, "<?");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000708 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000709 xmlOutputBufferWrite(buf, 2, "?>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000710 }
711 return;
712 }
713 if (cur->type == XML_COMMENT_NODE) {
714 if (cur->content != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000715 xmlOutputBufferWrite(buf, 4, "<!--");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000716 xmlOutputBufferWriteString(buf, (const char *)cur->content);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000717 xmlOutputBufferWrite(buf, 3, "-->");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000718 }
719 return;
720 }
721 if (cur->type == XML_ENTITY_REF_NODE) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000722 xmlOutputBufferWrite(buf, 1, "&");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000723 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000724 xmlOutputBufferWrite(buf, 1, ";");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000725 return;
726 }
727 if (cur->type == XML_CDATA_SECTION_NODE) {
Daniel Veillard7cd517c2005-05-20 18:47:22 +0000728 if (cur->content == NULL) {
729 xmlOutputBufferWrite(buf, 12, "<![CDATA[]]>");
730 } else {
731 start = end = cur->content;
732 while (*end != '\0') {
733 if ((*end == ']') && (*(end + 1) == ']') &&
734 (*(end + 2) == '>')) {
735 end = end + 2;
736 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
737 xmlOutputBufferWrite(buf, end - start, (const char *)start);
738 xmlOutputBufferWrite(buf, 3, "]]>");
739 start = end;
740 }
741 end++;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000742 }
Daniel Veillard7cd517c2005-05-20 18:47:22 +0000743 if (start != end) {
744 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
745 xmlOutputBufferWriteString(buf, (const char *)start);
746 xmlOutputBufferWrite(buf, 3, "]]>");
747 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000748 }
749 return;
750 }
751 if (cur->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000752 xmlAttrDumpOutput(ctxt, (xmlAttrPtr) cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000753 return;
754 }
755 if (cur->type == XML_NAMESPACE_DECL) {
756 xmlNsDumpOutput(buf, (xmlNsPtr) cur);
757 return;
758 }
759
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000760 format = ctxt->format;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000761 if (format == 1) {
762 tmp = cur->children;
763 while (tmp != NULL) {
764 if ((tmp->type == XML_TEXT_NODE) ||
765 (tmp->type == XML_CDATA_SECTION_NODE) ||
766 (tmp->type == XML_ENTITY_REF_NODE)) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000767 ctxt->format = 0;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000768 break;
769 }
770 tmp = tmp->next;
771 }
772 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000773 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
779 xmlOutputBufferWriteString(buf, (const char *)cur->name);
780 if (cur->nsDef)
781 xmlNsListDumpOutput(buf, cur->nsDef);
782 if (cur->properties != NULL)
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000783 xmlAttrListDumpOutput(ctxt, cur->properties);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000784
785 if (((cur->type == XML_ELEMENT_NODE) || (cur->content == NULL)) &&
Rob Richards2ce51c02005-09-12 12:16:35 +0000786 (cur->children == NULL) && ((ctxt->options & XML_SAVE_NO_EMPTY) == 0)) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000787 xmlOutputBufferWrite(buf, 2, "/>");
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000788 ctxt->format = format;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000789 return;
790 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000791 xmlOutputBufferWrite(buf, 1, ">");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000792 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Daniel Veillard3995bc32004-05-15 18:57:31 +0000793 xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000794 }
795 if (cur->children != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000796 if (ctxt->format) xmlOutputBufferWrite(buf, 1, "\n");
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000797 if (ctxt->level >= 0) ctxt->level++;
798 xmlNodeListDumpOutput(ctxt, cur->children);
799 if (ctxt->level > 0) ctxt->level--;
800 if ((xmlIndentTreeOutput) && (ctxt->format))
Daniel Veillard753086a2004-03-28 16:12:44 +0000801 xmlOutputBufferWrite(buf, ctxt->indent_size *
802 (ctxt->level > ctxt->indent_nr ?
803 ctxt->indent_nr : ctxt->level),
804 ctxt->indent);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000805 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000806 xmlOutputBufferWrite(buf, 2, "</");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000807 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
808 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000809 xmlOutputBufferWrite(buf, 1, ":");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000810 }
811
812 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000813 xmlOutputBufferWrite(buf, 1, ">");
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000814 ctxt->format = format;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000815}
816
817/**
818 * xmlDocContentDumpOutput:
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000819 * @cur: the document
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000820 *
821 * Dump an XML document.
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000822 */
823static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000824xmlDocContentDumpOutput(xmlSaveCtxtPtr ctxt, xmlDocPtr cur) {
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000825#ifdef LIBXML_HTML_ENABLED
826 xmlDtdPtr dtd;
827 int is_xhtml = 0;
828#endif
829 const xmlChar *oldenc = cur->encoding;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000830 const xmlChar *encoding = ctxt->encoding;
831 xmlOutputBufferPtr buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000832
833 xmlInitParser();
834
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000835 if (ctxt->encoding != NULL)
836 cur->encoding = BAD_CAST ctxt->encoding;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000837
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000838 buf = ctxt->buf;
Daniel Veillard100e1802005-08-08 14:44:11 +0000839 if ((ctxt->options & XML_SAVE_NO_DECL) == 0) {
840 xmlOutputBufferWrite(buf, 14, "<?xml version=");
841 if (cur->version != NULL)
842 xmlBufferWriteQuotedString(buf->buffer, cur->version);
843 else
844 xmlOutputBufferWrite(buf, 5, "\"1.0\"");
845 if (ctxt->encoding == NULL) {
846 if (cur->encoding != NULL)
847 encoding = cur->encoding;
848 else if (cur->charset != XML_CHAR_ENCODING_UTF8)
849 encoding = (const xmlChar *)
850 xmlGetCharEncodingName((xmlCharEncoding) cur->charset);
851 }
852 if (encoding != NULL) {
853 xmlOutputBufferWrite(buf, 10, " encoding=");
854 xmlBufferWriteQuotedString(buf->buffer, (xmlChar *) encoding);
855 }
856 switch (cur->standalone) {
857 case 0:
858 xmlOutputBufferWrite(buf, 16, " standalone=\"no\"");
859 break;
860 case 1:
861 xmlOutputBufferWrite(buf, 17, " standalone=\"yes\"");
862 break;
863 }
864 xmlOutputBufferWrite(buf, 3, "?>\n");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000865 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000866
867#ifdef LIBXML_HTML_ENABLED
868 dtd = xmlGetIntSubset(cur);
869 if (dtd != NULL) {
870 is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
871 if (is_xhtml < 0) is_xhtml = 0;
872 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000873#endif
874 if (cur->children != NULL) {
875 xmlNodePtr child = cur->children;
876
877 while (child != NULL) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000878 ctxt->level = 0;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000879#ifdef LIBXML_HTML_ENABLED
880 if (is_xhtml)
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000881 xhtmlNodeDumpOutput(ctxt, child);
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000882 else
883#endif
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000884 xmlNodeDumpOutputInternal(ctxt, child);
Daniel Veillard7a6361f2004-05-15 16:37:50 +0000885 xmlOutputBufferWrite(buf, 1, "\n");
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000886 child = child->next;
887 }
888 }
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000889 if (ctxt->encoding != NULL)
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000890 cur->encoding = oldenc;
891}
892
893#ifdef LIBXML_HTML_ENABLED
894/************************************************************************
895 * *
896 * Functions specific to XHTML serialization *
897 * *
898 ************************************************************************/
899
900/**
901 * xhtmlIsEmpty:
902 * @node: the node
903 *
904 * Check if a node is an empty xhtml node
905 *
906 * Returns 1 if the node is an empty node, 0 if not and -1 in case of error
907 */
908static int
909xhtmlIsEmpty(xmlNodePtr node) {
910 if (node == NULL)
911 return(-1);
912 if (node->type != XML_ELEMENT_NODE)
913 return(0);
914 if ((node->ns != NULL) && (!xmlStrEqual(node->ns->href, XHTML_NS_NAME)))
915 return(0);
916 if (node->children != NULL)
917 return(0);
918 switch (node->name[0]) {
919 case 'a':
920 if (xmlStrEqual(node->name, BAD_CAST "area"))
921 return(1);
922 return(0);
923 case 'b':
924 if (xmlStrEqual(node->name, BAD_CAST "br"))
925 return(1);
926 if (xmlStrEqual(node->name, BAD_CAST "base"))
927 return(1);
928 if (xmlStrEqual(node->name, BAD_CAST "basefont"))
929 return(1);
930 return(0);
931 case 'c':
932 if (xmlStrEqual(node->name, BAD_CAST "col"))
933 return(1);
934 return(0);
935 case 'f':
936 if (xmlStrEqual(node->name, BAD_CAST "frame"))
937 return(1);
938 return(0);
939 case 'h':
940 if (xmlStrEqual(node->name, BAD_CAST "hr"))
941 return(1);
942 return(0);
943 case 'i':
944 if (xmlStrEqual(node->name, BAD_CAST "img"))
945 return(1);
946 if (xmlStrEqual(node->name, BAD_CAST "input"))
947 return(1);
948 if (xmlStrEqual(node->name, BAD_CAST "isindex"))
949 return(1);
950 return(0);
951 case 'l':
952 if (xmlStrEqual(node->name, BAD_CAST "link"))
953 return(1);
954 return(0);
955 case 'm':
956 if (xmlStrEqual(node->name, BAD_CAST "meta"))
957 return(1);
958 return(0);
959 case 'p':
960 if (xmlStrEqual(node->name, BAD_CAST "param"))
961 return(1);
962 return(0);
963 }
964 return(0);
965}
966
967/**
968 * xhtmlAttrListDumpOutput:
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000969 * @cur: the first attribute pointer
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000970 *
971 * Dump a list of XML attributes
972 */
973static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000974xhtmlAttrListDumpOutput(xmlSaveCtxtPtr ctxt, xmlAttrPtr cur) {
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000975 xmlAttrPtr xml_lang = NULL;
976 xmlAttrPtr lang = NULL;
977 xmlAttrPtr name = NULL;
978 xmlAttrPtr id = NULL;
979 xmlNodePtr parent;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000980 xmlOutputBufferPtr buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000981
Daniel Veillard32b7cdb2004-03-15 13:46:37 +0000982 if (cur == NULL) return;
983 buf = ctxt->buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +0000984 parent = cur->parent;
985 while (cur != NULL) {
986 if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "id")))
987 id = cur;
988 else
989 if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "name")))
990 name = cur;
991 else
992 if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")))
993 lang = cur;
994 else
995 if ((cur->ns != NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")) &&
996 (xmlStrEqual(cur->ns->prefix, BAD_CAST "xml")))
997 xml_lang = cur;
998 else if ((cur->ns == NULL) &&
999 ((cur->children == NULL) ||
1000 (cur->children->content == NULL) ||
1001 (cur->children->content[0] == 0)) &&
1002 (htmlIsBooleanAttr(cur->name))) {
1003 if (cur->children != NULL)
1004 xmlFreeNode(cur->children);
1005 cur->children = xmlNewText(cur->name);
1006 if (cur->children != NULL)
1007 cur->children->parent = (xmlNodePtr) cur;
1008 }
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001009 xmlAttrDumpOutput(ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001010 cur = cur->next;
1011 }
1012 /*
1013 * C.8
1014 */
1015 if ((name != NULL) && (id == NULL)) {
1016 if ((parent != NULL) && (parent->name != NULL) &&
1017 ((xmlStrEqual(parent->name, BAD_CAST "a")) ||
1018 (xmlStrEqual(parent->name, BAD_CAST "p")) ||
1019 (xmlStrEqual(parent->name, BAD_CAST "div")) ||
1020 (xmlStrEqual(parent->name, BAD_CAST "img")) ||
1021 (xmlStrEqual(parent->name, BAD_CAST "map")) ||
1022 (xmlStrEqual(parent->name, BAD_CAST "applet")) ||
1023 (xmlStrEqual(parent->name, BAD_CAST "form")) ||
1024 (xmlStrEqual(parent->name, BAD_CAST "frame")) ||
1025 (xmlStrEqual(parent->name, BAD_CAST "iframe")))) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001026 xmlOutputBufferWrite(buf, 5, " id=\"");
1027 xmlAttrSerializeContent(buf, name);
1028 xmlOutputBufferWrite(buf, 1, "\"");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001029 }
1030 }
1031 /*
1032 * C.7.
1033 */
1034 if ((lang != NULL) && (xml_lang == NULL)) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001035 xmlOutputBufferWrite(buf, 11, " xml:lang=\"");
1036 xmlAttrSerializeContent(buf, lang);
1037 xmlOutputBufferWrite(buf, 1, "\"");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001038 } else
1039 if ((xml_lang != NULL) && (lang == NULL)) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001040 xmlOutputBufferWrite(buf, 7, " lang=\"");
1041 xmlAttrSerializeContent(buf, xml_lang);
1042 xmlOutputBufferWrite(buf, 1, "\"");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001043 }
1044}
1045
1046/**
1047 * xhtmlNodeListDumpOutput:
1048 * @buf: the XML buffer output
1049 * @doc: the XHTML document
1050 * @cur: the first node
1051 * @level: the imbrication level for indenting
1052 * @format: is formatting allowed
1053 * @encoding: an optional encoding string
1054 *
1055 * Dump an XML node list, recursive behaviour, children are printed too.
1056 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
1057 * or xmlKeepBlanksDefault(0) was called
1058 */
1059static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001060xhtmlNodeListDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001061 xmlOutputBufferPtr buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001062
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001063 if (cur == NULL) return;
1064 buf = ctxt->buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001065 while (cur != NULL) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001066 if ((ctxt->format) && (xmlIndentTreeOutput) &&
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001067 (cur->type == XML_ELEMENT_NODE))
Daniel Veillard753086a2004-03-28 16:12:44 +00001068 xmlOutputBufferWrite(buf, ctxt->indent_size *
1069 (ctxt->level > ctxt->indent_nr ?
1070 ctxt->indent_nr : ctxt->level),
1071 ctxt->indent);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001072 xhtmlNodeDumpOutput(ctxt, cur);
1073 if (ctxt->format) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001074 xmlOutputBufferWrite(buf, 1, "\n");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001075 }
1076 cur = cur->next;
1077 }
1078}
1079
1080/**
1081 * xhtmlNodeDumpOutput:
1082 * @buf: the XML buffer output
1083 * @doc: the XHTML document
1084 * @cur: the current node
1085 * @level: the imbrication level for indenting
1086 * @format: is formatting allowed
1087 * @encoding: an optional encoding string
1088 *
1089 * Dump an XHTML node, recursive behaviour, children are printed too.
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001090 */
1091static void
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001092xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
Rob Richards31f73022005-08-26 15:33:26 +00001093 int format, addmeta = 0;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001094 xmlNodePtr tmp;
1095 xmlChar *start, *end;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001096 xmlOutputBufferPtr buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001097
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001098 if (cur == NULL) return;
Daniel Veillard60071ae2005-09-12 00:03:43 +00001099 if ((cur->type == XML_DOCUMENT_NODE) ||
1100 (cur->type == XML_HTML_DOCUMENT_NODE)) {
1101 xmlDocContentDumpOutput(ctxt, (xmlDocPtr) cur);
1102 return;
1103 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001104 if (cur->type == XML_XINCLUDE_START)
1105 return;
1106 if (cur->type == XML_XINCLUDE_END)
1107 return;
1108 if (cur->type == XML_DTD_NODE) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001109 xmlDtdDumpOutput(ctxt, (xmlDtdPtr) cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001110 return;
1111 }
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001112 buf = ctxt->buf;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001113 if (cur->type == XML_ELEMENT_DECL) {
1114 xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur);
1115 return;
1116 }
1117 if (cur->type == XML_ATTRIBUTE_DECL) {
1118 xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur);
1119 return;
1120 }
1121 if (cur->type == XML_ENTITY_DECL) {
1122 xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur);
1123 return;
1124 }
1125 if (cur->type == XML_TEXT_NODE) {
1126 if (cur->content != NULL) {
1127 if ((cur->name == xmlStringText) ||
1128 (cur->name != xmlStringTextNoenc)) {
Daniel Veillard3995bc32004-05-15 18:57:31 +00001129 xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001130 } else {
1131 /*
1132 * Disable escaping, needed for XSLT
1133 */
1134 xmlOutputBufferWriteString(buf, (const char *) cur->content);
1135 }
1136 }
1137
1138 return;
1139 }
1140 if (cur->type == XML_PI_NODE) {
1141 if (cur->content != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001142 xmlOutputBufferWrite(buf, 2, "<?");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001143 xmlOutputBufferWriteString(buf, (const char *)cur->name);
1144 if (cur->content != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001145 xmlOutputBufferWrite(buf, 1, " ");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001146 xmlOutputBufferWriteString(buf, (const char *)cur->content);
1147 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001148 xmlOutputBufferWrite(buf, 2, "?>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001149 } else {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001150 xmlOutputBufferWrite(buf, 2, "<?");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001151 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001152 xmlOutputBufferWrite(buf, 2, "?>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001153 }
1154 return;
1155 }
1156 if (cur->type == XML_COMMENT_NODE) {
1157 if (cur->content != NULL) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001158 xmlOutputBufferWrite(buf, 4, "<!--");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001159 xmlOutputBufferWriteString(buf, (const char *)cur->content);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001160 xmlOutputBufferWrite(buf, 3, "-->");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001161 }
1162 return;
1163 }
1164 if (cur->type == XML_ENTITY_REF_NODE) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001165 xmlOutputBufferWrite(buf, 1, "&");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001166 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001167 xmlOutputBufferWrite(buf, 1, ";");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001168 return;
1169 }
1170 if (cur->type == XML_CDATA_SECTION_NODE) {
1171 start = end = cur->content;
1172 while (*end != '\0') {
1173 if (*end == ']' && *(end + 1) == ']' && *(end + 2) == '>') {
1174 end = end + 2;
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001175 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001176 xmlOutputBufferWrite(buf, end - start, (const char *)start);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001177 xmlOutputBufferWrite(buf, 3, "]]>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001178 start = end;
1179 }
1180 end++;
1181 }
1182 if (start != end) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001183 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001184 xmlOutputBufferWriteString(buf, (const char *)start);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001185 xmlOutputBufferWrite(buf, 3, "]]>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001186 }
1187 return;
1188 }
1189
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001190 format = ctxt->format;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001191 if (format == 1) {
1192 tmp = cur->children;
1193 while (tmp != NULL) {
1194 if ((tmp->type == XML_TEXT_NODE) ||
1195 (tmp->type == XML_ENTITY_REF_NODE)) {
1196 format = 0;
1197 break;
1198 }
1199 tmp = tmp->next;
1200 }
1201 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001202 xmlOutputBufferWrite(buf, 1, "<");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001203 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1204 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001205 xmlOutputBufferWrite(buf, 1, ":");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001206 }
1207
1208 xmlOutputBufferWriteString(buf, (const char *)cur->name);
1209 if (cur->nsDef)
1210 xmlNsListDumpOutput(buf, cur->nsDef);
1211 if ((xmlStrEqual(cur->name, BAD_CAST "html") &&
1212 (cur->ns == NULL) && (cur->nsDef == NULL))) {
1213 /*
1214 * 3.1.1. Strictly Conforming Documents A.3.1.1 3/
1215 */
1216 xmlOutputBufferWriteString(buf,
1217 " xmlns=\"http://www.w3.org/1999/xhtml\"");
1218 }
1219 if (cur->properties != NULL)
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001220 xhtmlAttrListDumpOutput(ctxt, cur->properties);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001221
Rob Richards31f73022005-08-26 15:33:26 +00001222 if ((cur->type == XML_ELEMENT_NODE) &&
1223 (cur->parent != NULL) &&
1224 (cur->parent->parent == (xmlNodePtr) cur->doc) &&
1225 xmlStrEqual(cur->name, BAD_CAST"head") &&
1226 xmlStrEqual(cur->parent->name, BAD_CAST"html")) {
1227
1228 tmp = cur->children;
1229 while (tmp != NULL) {
1230 if (xmlStrEqual(tmp->name, BAD_CAST"meta")) {
1231 xmlChar *httpequiv;
1232
1233 httpequiv = xmlGetProp(tmp, BAD_CAST"http-equiv");
Rob Richards07b72002005-09-03 14:56:36 +00001234 if (httpequiv != NULL) {
1235 if (xmlStrcasecmp(httpequiv, BAD_CAST"Content-Type") == 0) {
1236 xmlFree(httpequiv);
1237 break;
1238 }
Rob Richards31f73022005-08-26 15:33:26 +00001239 xmlFree(httpequiv);
Rob Richards31f73022005-08-26 15:33:26 +00001240 }
Rob Richards31f73022005-08-26 15:33:26 +00001241 }
1242 tmp = tmp->next;
1243 }
1244 if (tmp == NULL)
1245 addmeta = 1;
1246 }
1247
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001248 if ((cur->type == XML_ELEMENT_NODE) && (cur->children == NULL)) {
1249 if (((cur->ns == NULL) || (cur->ns->prefix == NULL)) &&
Rob Richards31f73022005-08-26 15:33:26 +00001250 ((xhtmlIsEmpty(cur) == 1) && (addmeta == 0))) {
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001251 /*
1252 * C.2. Empty Elements
1253 */
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001254 xmlOutputBufferWrite(buf, 3, " />");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001255 } else {
Rob Richards31f73022005-08-26 15:33:26 +00001256 if (addmeta == 1) {
1257 xmlOutputBufferWrite(buf, 1, ">");
Rob Richards2ce51c02005-09-12 12:16:35 +00001258 if (ctxt->format) {
1259 xmlOutputBufferWrite(buf, 1, "\n");
1260 if (xmlIndentTreeOutput)
1261 xmlOutputBufferWrite(buf, ctxt->indent_size *
1262 (ctxt->level + 1 > ctxt->indent_nr ?
1263 ctxt->indent_nr : ctxt->level + 1), ctxt->indent);
1264 }
Rob Richards31f73022005-08-26 15:33:26 +00001265 xmlOutputBufferWriteString(buf,
1266 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=");
1267 if (ctxt->encoding) {
1268 xmlOutputBufferWriteString(buf, (const char *)ctxt->encoding);
1269 } else {
1270 xmlOutputBufferWrite(buf, 5, "UTF-8");
1271 }
Rob Richards2ce51c02005-09-12 12:16:35 +00001272 xmlOutputBufferWrite(buf, 4, "\" />");
1273 if (ctxt->format)
1274 xmlOutputBufferWrite(buf, 1, "\n");
1275 } else {
1276 xmlOutputBufferWrite(buf, 1, ">");
Rob Richards31f73022005-08-26 15:33:26 +00001277 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001278 /*
1279 * C.3. Element Minimization and Empty Element Content
1280 */
Rob Richards2ce51c02005-09-12 12:16:35 +00001281 xmlOutputBufferWrite(buf, 2, "</");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001282 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1283 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001284 xmlOutputBufferWrite(buf, 1, ":");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001285 }
1286 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001287 xmlOutputBufferWrite(buf, 1, ">");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001288 }
1289 return;
1290 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001291 xmlOutputBufferWrite(buf, 1, ">");
Rob Richards31f73022005-08-26 15:33:26 +00001292 if (addmeta == 1) {
Rob Richards2ce51c02005-09-12 12:16:35 +00001293 if (ctxt->format) {
1294 xmlOutputBufferWrite(buf, 1, "\n");
1295 if (xmlIndentTreeOutput)
1296 xmlOutputBufferWrite(buf, ctxt->indent_size *
1297 (ctxt->level + 1 > ctxt->indent_nr ?
1298 ctxt->indent_nr : ctxt->level + 1), ctxt->indent);
1299 }
Rob Richards31f73022005-08-26 15:33:26 +00001300 xmlOutputBufferWriteString(buf,
1301 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=");
1302 if (ctxt->encoding) {
1303 xmlOutputBufferWriteString(buf, (const char *)ctxt->encoding);
1304 } else {
1305 xmlOutputBufferWrite(buf, 5, "UTF-8");
1306 }
1307 xmlOutputBufferWrite(buf, 4, "\" />");
1308 }
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001309 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Daniel Veillard3995bc32004-05-15 18:57:31 +00001310 xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001311 }
1312
1313 /*
1314 * 4.8. Script and Style elements
1315 */
1316 if ((cur->type == XML_ELEMENT_NODE) &&
1317 ((xmlStrEqual(cur->name, BAD_CAST "script")) ||
1318 (xmlStrEqual(cur->name, BAD_CAST "style"))) &&
1319 ((cur->ns == NULL) ||
1320 (xmlStrEqual(cur->ns->href, XHTML_NS_NAME)))) {
1321 xmlNodePtr child = cur->children;
1322
1323 while (child != NULL) {
1324 if ((child->type == XML_TEXT_NODE) ||
1325 (child->type == XML_CDATA_SECTION_NODE)) {
1326 /*
1327 * Apparently CDATA escaping for style just break on IE,
1328 * mozilla and galeon, so ...
1329 */
1330 if (xmlStrEqual(cur->name, BAD_CAST "style") &&
1331 (xmlStrchr(child->content, '<') == NULL) &&
1332 (xmlStrchr(child->content, '>') == NULL) &&
1333 (xmlStrchr(child->content, '&') == NULL)) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001334 int level = ctxt->level;
1335 int indent = ctxt->format;
1336
1337 ctxt->level = 0;
1338 ctxt->format = 0;
1339 xhtmlNodeDumpOutput(ctxt, child);
1340 ctxt->level = level;
1341 ctxt->format = indent;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001342 } else {
1343 start = end = child->content;
1344 while (*end != '\0') {
1345 if (*end == ']' &&
1346 *(end + 1) == ']' &&
1347 *(end + 2) == '>') {
1348 end = end + 2;
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001349 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001350 xmlOutputBufferWrite(buf, end - start,
1351 (const char *)start);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001352 xmlOutputBufferWrite(buf, 3, "]]>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001353 start = end;
1354 }
1355 end++;
1356 }
1357 if (start != end) {
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001358 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
1359 xmlOutputBufferWrite(buf, end - start,
1360 (const char *)start);
1361 xmlOutputBufferWrite(buf, 3, "]]>");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001362 }
1363 }
1364 } else {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001365 int level = ctxt->level;
1366 int indent = ctxt->format;
1367
1368 ctxt->level = 0;
1369 ctxt->format = 0;
1370 xhtmlNodeDumpOutput(ctxt, child);
1371 ctxt->level = level;
1372 ctxt->format = indent;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001373 }
1374 child = child->next;
1375 }
1376 } else if (cur->children != NULL) {
Daniel Veillardf0244ce2004-05-09 23:48:39 +00001377 int indent = ctxt->format;
1378
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001379 if (format) xmlOutputBufferWrite(buf, 1, "\n");
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001380 if (ctxt->level >= 0) ctxt->level++;
Daniel Veillardf0244ce2004-05-09 23:48:39 +00001381 ctxt->format = format;
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001382 xhtmlNodeListDumpOutput(ctxt, cur->children);
1383 if (ctxt->level > 0) ctxt->level--;
Daniel Veillardf0244ce2004-05-09 23:48:39 +00001384 ctxt->format = indent;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001385 if ((xmlIndentTreeOutput) && (format))
Daniel Veillard753086a2004-03-28 16:12:44 +00001386 xmlOutputBufferWrite(buf, ctxt->indent_size *
1387 (ctxt->level > ctxt->indent_nr ?
1388 ctxt->indent_nr : ctxt->level),
1389 ctxt->indent);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001390 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001391 xmlOutputBufferWrite(buf, 2, "</");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001392 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1393 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001394 xmlOutputBufferWrite(buf, 1, ":");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001395 }
1396
1397 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001398 xmlOutputBufferWrite(buf, 1, ">");
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001399}
1400#endif
1401
1402/************************************************************************
1403 * *
1404 * Public entry points *
1405 * *
1406 ************************************************************************/
1407
1408/**
1409 * xmlSaveToFd:
1410 * @fd: a file descriptor number
1411 * @encoding: the encoding name to use or NULL
1412 * @options: a set of xmlSaveOptions
1413 *
1414 * Create a document saving context serializing to a file descriptor
1415 * with the encoding and the options given.
1416 *
1417 * Returns a new serialization context or NULL in case of error.
1418 */
1419xmlSaveCtxtPtr
1420xmlSaveToFd(int fd, const char *encoding, int options)
1421{
1422 xmlSaveCtxtPtr ret;
1423
1424 ret = xmlNewSaveCtxt(encoding, options);
1425 if (ret == NULL) return(NULL);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001426 ret->buf = xmlOutputBufferCreateFd(fd, ret->handler);
1427 if (ret->buf == NULL) {
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001428 xmlFreeSaveCtxt(ret);
1429 return(NULL);
1430 }
1431 return(ret);
1432}
1433
1434/**
1435 * xmlSaveToFilename:
1436 * @filename: a file name or an URL
1437 * @encoding: the encoding name to use or NULL
1438 * @options: a set of xmlSaveOptions
1439 *
1440 * Create a document saving context serializing to a filename or possibly
1441 * to an URL (but this is less reliable) with the encoding and the options
1442 * given.
1443 *
1444 * Returns a new serialization context or NULL in case of error.
1445 */
1446xmlSaveCtxtPtr
1447xmlSaveToFilename(const char *filename, const char *encoding, int options)
1448{
1449 xmlSaveCtxtPtr ret;
1450 int compression = 0; /* TODO handle compression option */
1451
1452 ret = xmlNewSaveCtxt(encoding, options);
1453 if (ret == NULL) return(NULL);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001454 ret->buf = xmlOutputBufferCreateFilename(filename, ret->handler,
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001455 compression);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001456 if (ret->buf == NULL) {
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001457 xmlFreeSaveCtxt(ret);
1458 return(NULL);
1459 }
1460 return(ret);
1461}
1462
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001463/**
1464 * xmlSaveToBuffer:
1465 * @buffer: a buffer
1466 * @encoding: the encoding name to use or NULL
1467 * @options: a set of xmlSaveOptions
1468 *
1469 * Create a document saving context serializing to a buffer
1470 * with the encoding and the options given
1471 *
1472 * Returns a new serialization context or NULL in case of error.
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001473xmlSaveCtxtPtr
1474xmlSaveToBuffer(xmlBufferPtr buffer, const char *encoding, int options)
1475{
1476 TODO
1477 return(NULL);
1478}
Daniel Veillarda2351322004-06-27 12:08:10 +00001479 */
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001480
1481/**
1482 * xmlSaveToIO:
1483 * @iowrite: an I/O write function
1484 * @ioclose: an I/O close function
1485 * @ioctx: an I/O handler
1486 * @encoding: the encoding name to use or NULL
1487 * @options: a set of xmlSaveOptions
1488 *
1489 * Create a document saving context serializing to a file descriptor
1490 * with the encoding and the options given
1491 *
1492 * Returns a new serialization context or NULL in case of error.
1493 */
1494xmlSaveCtxtPtr
1495xmlSaveToIO(xmlOutputWriteCallback iowrite,
1496 xmlOutputCloseCallback ioclose,
1497 void *ioctx, const char *encoding, int options)
1498{
1499 xmlSaveCtxtPtr ret;
1500
1501 ret = xmlNewSaveCtxt(encoding, options);
1502 if (ret == NULL) return(NULL);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001503 ret->buf = xmlOutputBufferCreateIO(iowrite, ioclose, ioctx, ret->handler);
1504 if (ret->buf == NULL) {
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001505 xmlFreeSaveCtxt(ret);
1506 return(NULL);
1507 }
1508 return(ret);
1509}
1510
1511/**
1512 * xmlSaveDoc:
1513 * @ctxt: a document saving context
1514 * @doc: a document
1515 *
1516 * Save a full document to a saving context
Daniel Veillard377e1a92004-04-16 16:30:05 +00001517 * TODO: The function is not fully implemented yet as it does not return the
1518 * byte count but 0 instead
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001519 *
1520 * Returns the number of byte written or -1 in case of error
1521 */
1522long
1523xmlSaveDoc(xmlSaveCtxtPtr ctxt, xmlDocPtr doc)
1524{
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001525 long ret = 0;
1526
Daniel Veillardce682bc2004-11-05 17:22:25 +00001527 if ((ctxt == NULL) || (doc == NULL)) return(-1);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001528 xmlDocContentDumpOutput(ctxt, doc);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001529 return(ret);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001530}
1531
1532/**
1533 * xmlSaveTree:
1534 * @ctxt: a document saving context
1535 * @node: a document
1536 *
1537 * Save a subtree starting at the node parameter to a saving context
Daniel Veillard377e1a92004-04-16 16:30:05 +00001538 * TODO: The function is not fully implemented yet as it does not return the
1539 * byte count but 0 instead
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001540 *
1541 * Returns the number of byte written or -1 in case of error
1542 */
1543long
1544xmlSaveTree(xmlSaveCtxtPtr ctxt, xmlNodePtr node)
1545{
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001546 long ret = 0;
1547
Daniel Veillardce682bc2004-11-05 17:22:25 +00001548 if ((ctxt == NULL) || (node == NULL)) return(-1);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001549 xmlNodeDumpOutputInternal(ctxt, node);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001550 return(ret);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001551}
1552
1553/**
1554 * xmlSaveFlush:
1555 * @ctxt: a document saving context
1556 *
1557 * Flush a document saving context, i.e. make sure that all bytes have
1558 * been output.
1559 *
1560 * Returns the number of byte written or -1 in case of error.
1561 */
1562int
1563xmlSaveFlush(xmlSaveCtxtPtr ctxt)
1564{
1565 if (ctxt == NULL) return(-1);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001566 if (ctxt->buf == NULL) return(-1);
1567 return(xmlOutputBufferFlush(ctxt->buf));
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001568}
1569
1570/**
1571 * xmlSaveClose:
1572 * @ctxt: a document saving context
1573 *
1574 * Close a document saving context, i.e. make sure that all bytes have
1575 * been output and free the associated data.
1576 *
1577 * Returns the number of byte written or -1 in case of error.
1578 */
1579int
1580xmlSaveClose(xmlSaveCtxtPtr ctxt)
1581{
1582 int ret;
1583
1584 if (ctxt == NULL) return(-1);
1585 ret = xmlSaveFlush(ctxt);
1586 xmlFreeSaveCtxt(ctxt);
1587 return(ret);
1588}
1589
Daniel Veillard3995bc32004-05-15 18:57:31 +00001590/**
1591 * xmlSaveSetEscape:
1592 * @ctxt: a document saving context
1593 * @escape: the escaping function
1594 *
1595 * Set a custom escaping function to be used for text in element content
1596 *
1597 * Returns 0 if successful or -1 in case of error.
1598 */
1599int
1600xmlSaveSetEscape(xmlSaveCtxtPtr ctxt, xmlCharEncodingOutputFunc escape)
1601{
1602 if (ctxt == NULL) return(-1);
1603 ctxt->escape = escape;
1604 return(0);
1605}
1606
1607/**
1608 * xmlSaveSetAttrEscape:
1609 * @ctxt: a document saving context
1610 * @escape: the escaping function
1611 *
1612 * Set a custom escaping function to be used for text in attribute content
1613 *
1614 * Returns 0 if successful or -1 in case of error.
1615 */
1616int
1617xmlSaveSetAttrEscape(xmlSaveCtxtPtr ctxt, xmlCharEncodingOutputFunc escape)
1618{
1619 if (ctxt == NULL) return(-1);
1620 ctxt->escapeAttr = escape;
1621 return(0);
1622}
1623
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001624/************************************************************************
1625 * *
1626 * Public entry points based on buffers *
1627 * *
1628 ************************************************************************/
1629/**
1630 * xmlAttrSerializeTxtContent:
1631 * @buf: the XML buffer output
1632 * @doc: the document
1633 * @attr: the attribute node
1634 * @string: the text content
1635 *
1636 * Serialize text attribute values to an xml simple buffer
1637 */
1638void
1639xmlAttrSerializeTxtContent(xmlBufferPtr buf, xmlDocPtr doc,
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001640 xmlAttrPtr attr, const xmlChar * string)
1641{
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001642 xmlChar *base, *cur;
1643
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001644 if (string == NULL)
1645 return;
1646 base = cur = (xmlChar *) string;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001647 while (*cur != 0) {
1648 if (*cur == '\n') {
1649 if (base != cur)
1650 xmlBufferAdd(buf, base, cur - base);
1651 xmlBufferAdd(buf, BAD_CAST "&#10;", 5);
1652 cur++;
1653 base = cur;
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001654 } else if (*cur == '\r') {
1655 if (base != cur)
1656 xmlBufferAdd(buf, base, cur - base);
1657 xmlBufferAdd(buf, BAD_CAST "&#13;", 5);
1658 cur++;
1659 base = cur;
1660 } else if (*cur == '\t') {
1661 if (base != cur)
1662 xmlBufferAdd(buf, base, cur - base);
1663 xmlBufferAdd(buf, BAD_CAST "&#9;", 4);
1664 cur++;
1665 base = cur;
1666 } else if (*cur == '"') {
1667 if (base != cur)
1668 xmlBufferAdd(buf, base, cur - base);
1669 xmlBufferAdd(buf, BAD_CAST "&quot;", 6);
1670 cur++;
1671 base = cur;
1672 } else if (*cur == '<') {
1673 if (base != cur)
1674 xmlBufferAdd(buf, base, cur - base);
1675 xmlBufferAdd(buf, BAD_CAST "&lt;", 4);
1676 cur++;
1677 base = cur;
1678 } else if (*cur == '>') {
1679 if (base != cur)
1680 xmlBufferAdd(buf, base, cur - base);
1681 xmlBufferAdd(buf, BAD_CAST "&gt;", 4);
1682 cur++;
1683 base = cur;
1684 } else if (*cur == '&') {
1685 if (base != cur)
1686 xmlBufferAdd(buf, base, cur - base);
1687 xmlBufferAdd(buf, BAD_CAST "&amp;", 5);
1688 cur++;
1689 base = cur;
1690 } else if ((*cur >= 0x80) && ((doc == NULL) ||
1691 (doc->encoding == NULL))) {
1692 /*
1693 * We assume we have UTF-8 content.
1694 */
1695 unsigned char tmp[10];
1696 int val = 0, l = 1;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001697
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001698 if (base != cur)
1699 xmlBufferAdd(buf, base, cur - base);
1700 if (*cur < 0xC0) {
1701 xmlSaveErr(XML_SAVE_NOT_UTF8, (xmlNodePtr) attr, NULL);
1702 if (doc != NULL)
1703 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
1704 xmlSerializeHexCharRef(tmp, *cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001705 xmlBufferAdd(buf, (xmlChar *) tmp, -1);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001706 cur++;
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001707 base = cur;
1708 continue;
1709 } else if (*cur < 0xE0) {
1710 val = (cur[0]) & 0x1F;
1711 val <<= 6;
1712 val |= (cur[1]) & 0x3F;
1713 l = 2;
1714 } else if (*cur < 0xF0) {
1715 val = (cur[0]) & 0x0F;
1716 val <<= 6;
1717 val |= (cur[1]) & 0x3F;
1718 val <<= 6;
1719 val |= (cur[2]) & 0x3F;
1720 l = 3;
1721 } else if (*cur < 0xF8) {
1722 val = (cur[0]) & 0x07;
1723 val <<= 6;
1724 val |= (cur[1]) & 0x3F;
1725 val <<= 6;
1726 val |= (cur[2]) & 0x3F;
1727 val <<= 6;
1728 val |= (cur[3]) & 0x3F;
1729 l = 4;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001730 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001731 if ((l == 1) || (!IS_CHAR(val))) {
1732 xmlSaveErr(XML_SAVE_CHAR_INVALID, (xmlNodePtr) attr, NULL);
1733 if (doc != NULL)
1734 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
1735
1736 xmlSerializeHexCharRef(tmp, *cur);
1737 xmlBufferAdd(buf, (xmlChar *) tmp, -1);
1738 cur++;
1739 base = cur;
1740 continue;
1741 }
1742 /*
1743 * We could do multiple things here. Just save
1744 * as a char ref
1745 */
1746 xmlSerializeHexCharRef(tmp, val);
1747 xmlBufferAdd(buf, (xmlChar *) tmp, -1);
1748 cur += l;
1749 base = cur;
1750 } else {
1751 cur++;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001752 }
Daniel Veillard7a6361f2004-05-15 16:37:50 +00001753 }
1754 if (base != cur)
1755 xmlBufferAdd(buf, base, cur - base);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001756}
1757
1758/**
1759 * xmlNodeDump:
1760 * @buf: the XML buffer output
1761 * @doc: the document
1762 * @cur: the current node
1763 * @level: the imbrication level for indenting
1764 * @format: is formatting allowed
1765 *
1766 * Dump an XML node, recursive behaviour,children are printed too.
1767 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
1768 * or xmlKeepBlanksDefault(0) was called
1769 *
1770 * Returns the number of bytes written to the buffer or -1 in case of error
1771 */
1772int
1773xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
1774 int format)
1775{
1776 unsigned int use;
1777 int ret;
1778 xmlOutputBufferPtr outbuf;
1779
1780 xmlInitParser();
1781
1782 if (cur == NULL) {
1783#ifdef DEBUG_TREE
1784 xmlGenericError(xmlGenericErrorContext,
1785 "xmlNodeDump : node == NULL\n");
1786#endif
1787 return (-1);
1788 }
1789 if (buf == NULL) {
1790#ifdef DEBUG_TREE
1791 xmlGenericError(xmlGenericErrorContext,
1792 "xmlNodeDump : buf == NULL\n");
1793#endif
1794 return (-1);
1795 }
1796 outbuf = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer));
1797 if (outbuf == NULL) {
1798 xmlSaveErrMemory("creating buffer");
1799 return (-1);
1800 }
1801 memset(outbuf, 0, (size_t) sizeof(xmlOutputBuffer));
1802 outbuf->buffer = buf;
1803 outbuf->encoder = NULL;
1804 outbuf->writecallback = NULL;
1805 outbuf->closecallback = NULL;
1806 outbuf->context = NULL;
1807 outbuf->written = 0;
1808
1809 use = buf->use;
1810 xmlNodeDumpOutput(outbuf, doc, cur, level, format, NULL);
1811 xmlFree(outbuf);
1812 ret = buf->use - use;
1813 return (ret);
1814}
1815
1816/**
1817 * xmlElemDump:
1818 * @f: the FILE * for the output
1819 * @doc: the document
1820 * @cur: the current node
1821 *
1822 * Dump an XML/HTML node, recursive behaviour, children are printed too.
1823 */
1824void
1825xmlElemDump(FILE * f, xmlDocPtr doc, xmlNodePtr cur)
1826{
1827 xmlOutputBufferPtr outbuf;
1828
1829 xmlInitParser();
1830
1831 if (cur == NULL) {
1832#ifdef DEBUG_TREE
1833 xmlGenericError(xmlGenericErrorContext,
1834 "xmlElemDump : cur == NULL\n");
1835#endif
1836 return;
1837 }
1838#ifdef DEBUG_TREE
1839 if (doc == NULL) {
1840 xmlGenericError(xmlGenericErrorContext,
1841 "xmlElemDump : doc == NULL\n");
1842 }
1843#endif
1844
1845 outbuf = xmlOutputBufferCreateFile(f, NULL);
1846 if (outbuf == NULL)
1847 return;
1848 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
1849#ifdef LIBXML_HTML_ENABLED
1850 htmlNodeDumpOutput(outbuf, doc, cur, NULL);
1851#else
1852 xmlSaveErr(XML_ERR_INTERNAL_ERROR, cur, "HTML support not compiled in\n");
1853#endif /* LIBXML_HTML_ENABLED */
1854 } else
1855 xmlNodeDumpOutput(outbuf, doc, cur, 0, 1, NULL);
1856 xmlOutputBufferClose(outbuf);
1857}
1858
1859/************************************************************************
1860 * *
1861 * Saving functions front-ends *
1862 * *
1863 ************************************************************************/
1864
1865/**
1866 * xmlNodeDumpOutput:
1867 * @buf: the XML buffer output
1868 * @doc: the document
1869 * @cur: the current node
1870 * @level: the imbrication level for indenting
1871 * @format: is formatting allowed
1872 * @encoding: an optional encoding string
1873 *
1874 * Dump an XML node, recursive behaviour, children are printed too.
1875 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
1876 * or xmlKeepBlanksDefault(0) was called
1877 */
1878void
1879xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
1880 int level, int format, const char *encoding)
1881{
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001882 xmlSaveCtxt ctxt;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001883#ifdef LIBXML_HTML_ENABLED
1884 xmlDtdPtr dtd;
1885 int is_xhtml = 0;
1886#endif
1887
1888 xmlInitParser();
1889
Daniel Veillardce244ad2004-11-05 10:03:46 +00001890 if ((buf == NULL) || (cur == NULL)) return;
1891
Daniel Veillard64354ea2005-03-31 15:22:56 +00001892 if (encoding == NULL)
1893 encoding = "UTF-8";
1894
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001895 memset(&ctxt, 0, sizeof(ctxt));
1896 ctxt.doc = doc;
1897 ctxt.buf = buf;
1898 ctxt.level = level;
1899 ctxt.format = format;
1900 ctxt.encoding = (const xmlChar *) encoding;
Daniel Veillard753086a2004-03-28 16:12:44 +00001901 xmlSaveCtxtInit(&ctxt);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001902
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001903#ifdef LIBXML_HTML_ENABLED
1904 dtd = xmlGetIntSubset(doc);
1905 if (dtd != NULL) {
1906 is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
1907 if (is_xhtml < 0)
1908 is_xhtml = 0;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001909 }
1910
1911 if (is_xhtml)
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001912 xhtmlNodeDumpOutput(&ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001913 else
1914#endif
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001915 xmlNodeDumpOutputInternal(&ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001916}
1917
1918/**
1919 * xmlDocDumpFormatMemoryEnc:
1920 * @out_doc: Document to generate XML text from
1921 * @doc_txt_ptr: Memory pointer for allocated XML text
1922 * @doc_txt_len: Length of the generated XML text
1923 * @txt_encoding: Character encoding to use when generating XML text
1924 * @format: should formatting spaces been added
1925 *
1926 * Dump the current DOM tree into memory using the character encoding specified
1927 * by the caller. Note it is up to the caller of this function to free the
1928 * allocated memory with xmlFree().
1929 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
1930 * or xmlKeepBlanksDefault(0) was called
1931 */
1932
1933void
1934xmlDocDumpFormatMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
1935 int * doc_txt_len, const char * txt_encoding,
1936 int format) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001937 xmlSaveCtxt ctxt;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001938 int dummy = 0;
1939 xmlOutputBufferPtr out_buff = NULL;
1940 xmlCharEncodingHandlerPtr conv_hdlr = NULL;
1941
1942 if (doc_txt_len == NULL) {
1943 doc_txt_len = &dummy; /* Continue, caller just won't get length */
1944 }
1945
1946 if (doc_txt_ptr == NULL) {
1947 *doc_txt_len = 0;
1948 return;
1949 }
1950
1951 *doc_txt_ptr = NULL;
1952 *doc_txt_len = 0;
1953
1954 if (out_doc == NULL) {
1955 /* No document, no output */
1956 return;
1957 }
1958
1959 /*
1960 * Validate the encoding value, if provided.
1961 * This logic is copied from xmlSaveFileEnc.
1962 */
1963
1964 if (txt_encoding == NULL)
1965 txt_encoding = (const char *) out_doc->encoding;
1966 if (txt_encoding != NULL) {
1967 conv_hdlr = xmlFindCharEncodingHandler(txt_encoding);
1968 if ( conv_hdlr == NULL ) {
1969 xmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, (xmlNodePtr) out_doc,
1970 txt_encoding);
1971 return;
1972 }
1973 }
1974
1975 if ((out_buff = xmlAllocOutputBuffer(conv_hdlr)) == NULL ) {
1976 xmlSaveErrMemory("creating buffer");
1977 return;
1978 }
1979
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001980 memset(&ctxt, 0, sizeof(ctxt));
1981 ctxt.doc = out_doc;
1982 ctxt.buf = out_buff;
1983 ctxt.level = 0;
1984 ctxt.format = format;
1985 ctxt.encoding = (const xmlChar *) txt_encoding;
Daniel Veillard753086a2004-03-28 16:12:44 +00001986 xmlSaveCtxtInit(&ctxt);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00001987 xmlDocContentDumpOutput(&ctxt, out_doc);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00001988 xmlOutputBufferFlush(out_buff);
1989 if (out_buff->conv != NULL) {
1990 *doc_txt_len = out_buff->conv->use;
1991 *doc_txt_ptr = xmlStrndup(out_buff->conv->content, *doc_txt_len);
1992 } else {
1993 *doc_txt_len = out_buff->buffer->use;
1994 *doc_txt_ptr = xmlStrndup(out_buff->buffer->content, *doc_txt_len);
1995 }
1996 (void)xmlOutputBufferClose(out_buff);
1997
1998 if ((*doc_txt_ptr == NULL) && (*doc_txt_len > 0)) {
1999 *doc_txt_len = 0;
2000 xmlSaveErrMemory("creating output");
2001 }
2002
2003 return;
2004}
2005
2006/**
2007 * xmlDocDumpMemory:
2008 * @cur: the document
2009 * @mem: OUT: the memory pointer
2010 * @size: OUT: the memory length
2011 *
2012 * Dump an XML document in memory and return the #xmlChar * and it's size
2013 * in bytes. It's up to the caller to free the memory with xmlFree().
2014 * The resulting byte array is zero terminated, though the last 0 is not
2015 * included in the returned size.
2016 */
2017void
2018xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) {
2019 xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, 0);
2020}
2021
2022/**
2023 * xmlDocDumpFormatMemory:
2024 * @cur: the document
2025 * @mem: OUT: the memory pointer
2026 * @size: OUT: the memory length
2027 * @format: should formatting spaces been added
2028 *
2029 *
2030 * Dump an XML document in memory and return the #xmlChar * and it's size.
2031 * It's up to the caller to free the memory with xmlFree().
2032 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
2033 * or xmlKeepBlanksDefault(0) was called
2034 */
2035void
2036xmlDocDumpFormatMemory(xmlDocPtr cur, xmlChar**mem, int *size, int format) {
2037 xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, format);
2038}
2039
2040/**
2041 * xmlDocDumpMemoryEnc:
2042 * @out_doc: Document to generate XML text from
2043 * @doc_txt_ptr: Memory pointer for allocated XML text
2044 * @doc_txt_len: Length of the generated XML text
2045 * @txt_encoding: Character encoding to use when generating XML text
2046 *
2047 * Dump the current DOM tree into memory using the character encoding specified
2048 * by the caller. Note it is up to the caller of this function to free the
2049 * allocated memory with xmlFree().
2050 */
2051
2052void
2053xmlDocDumpMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
2054 int * doc_txt_len, const char * txt_encoding) {
2055 xmlDocDumpFormatMemoryEnc(out_doc, doc_txt_ptr, doc_txt_len,
2056 txt_encoding, 0);
2057}
2058
2059/**
2060 * xmlDocFormatDump:
2061 * @f: the FILE*
2062 * @cur: the document
2063 * @format: should formatting spaces been added
2064 *
2065 * Dump an XML document to an open FILE.
2066 *
2067 * returns: the number of bytes written or -1 in case of failure.
2068 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
2069 * or xmlKeepBlanksDefault(0) was called
2070 */
2071int
2072xmlDocFormatDump(FILE *f, xmlDocPtr cur, int format) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002073 xmlSaveCtxt ctxt;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002074 xmlOutputBufferPtr buf;
2075 const char * encoding;
2076 xmlCharEncodingHandlerPtr handler = NULL;
2077 int ret;
2078
2079 if (cur == NULL) {
2080#ifdef DEBUG_TREE
2081 xmlGenericError(xmlGenericErrorContext,
2082 "xmlDocDump : document == NULL\n");
2083#endif
2084 return(-1);
2085 }
2086 encoding = (const char *) cur->encoding;
2087
2088 if (encoding != NULL) {
2089 handler = xmlFindCharEncodingHandler(encoding);
2090 if (handler == NULL) {
2091 xmlFree((char *) cur->encoding);
2092 cur->encoding = NULL;
2093 }
2094 }
2095 buf = xmlOutputBufferCreateFile(f, handler);
2096 if (buf == NULL) return(-1);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002097 memset(&ctxt, 0, sizeof(ctxt));
2098 ctxt.doc = cur;
2099 ctxt.buf = buf;
2100 ctxt.level = 0;
2101 ctxt.format = format;
2102 ctxt.encoding = (const xmlChar *) encoding;
Daniel Veillard753086a2004-03-28 16:12:44 +00002103 xmlSaveCtxtInit(&ctxt);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002104 xmlDocContentDumpOutput(&ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002105
2106 ret = xmlOutputBufferClose(buf);
2107 return(ret);
2108}
2109
2110/**
2111 * xmlDocDump:
2112 * @f: the FILE*
2113 * @cur: the document
2114 *
2115 * Dump an XML document to an open FILE.
2116 *
2117 * returns: the number of bytes written or -1 in case of failure.
2118 */
2119int
2120xmlDocDump(FILE *f, xmlDocPtr cur) {
2121 return(xmlDocFormatDump (f, cur, 0));
2122}
2123
2124/**
2125 * xmlSaveFileTo:
2126 * @buf: an output I/O buffer
2127 * @cur: the document
2128 * @encoding: the encoding if any assuming the I/O layer handles the trancoding
2129 *
2130 * Dump an XML document to an I/O buffer.
Daniel Veillard3d97e662004-11-04 10:49:00 +00002131 * Warning ! This call xmlOutputBufferClose() on buf which is not available
2132 * after this call.
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002133 *
2134 * returns: the number of bytes written or -1 in case of failure.
2135 */
2136int
2137xmlSaveFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002138 xmlSaveCtxt ctxt;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002139 int ret;
2140
Daniel Veillard3d97e662004-11-04 10:49:00 +00002141 if (buf == NULL) return(-1);
2142 if (cur == NULL) {
2143 xmlOutputBufferClose(buf);
2144 return(-1);
2145 }
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002146 memset(&ctxt, 0, sizeof(ctxt));
2147 ctxt.doc = cur;
2148 ctxt.buf = buf;
2149 ctxt.level = 0;
2150 ctxt.format = 0;
2151 ctxt.encoding = (const xmlChar *) encoding;
Daniel Veillard753086a2004-03-28 16:12:44 +00002152 xmlSaveCtxtInit(&ctxt);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002153 xmlDocContentDumpOutput(&ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002154 ret = xmlOutputBufferClose(buf);
2155 return(ret);
2156}
2157
2158/**
2159 * xmlSaveFormatFileTo:
2160 * @buf: an output I/O buffer
2161 * @cur: the document
2162 * @encoding: the encoding if any assuming the I/O layer handles the trancoding
2163 * @format: should formatting spaces been added
2164 *
2165 * Dump an XML document to an I/O buffer.
Daniel Veillard3d97e662004-11-04 10:49:00 +00002166 * Warning ! This call xmlOutputBufferClose() on buf which is not available
2167 * after this call.
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002168 *
2169 * returns: the number of bytes written or -1 in case of failure.
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002170 */
2171int
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002172xmlSaveFormatFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur,
2173 const char *encoding, int format)
2174{
2175 xmlSaveCtxt ctxt;
2176 int ret;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002177
Daniel Veillard3d97e662004-11-04 10:49:00 +00002178 if (buf == NULL) return(-1);
Daniel Veillardce244ad2004-11-05 10:03:46 +00002179 if ((cur == NULL) ||
2180 ((cur->type != XML_DOCUMENT_NODE) &&
2181 (cur->type != XML_HTML_DOCUMENT_NODE))) {
Daniel Veillard3d97e662004-11-04 10:49:00 +00002182 xmlOutputBufferClose(buf);
2183 return(-1);
2184 }
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002185 memset(&ctxt, 0, sizeof(ctxt));
2186 ctxt.doc = cur;
2187 ctxt.buf = buf;
2188 ctxt.level = 0;
2189 ctxt.format = format;
2190 ctxt.encoding = (const xmlChar *) encoding;
Daniel Veillard753086a2004-03-28 16:12:44 +00002191 xmlSaveCtxtInit(&ctxt);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002192 xmlDocContentDumpOutput(&ctxt, cur);
2193 ret = xmlOutputBufferClose(buf);
2194 return (ret);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002195}
2196
2197/**
2198 * xmlSaveFormatFileEnc:
2199 * @filename: the filename or URL to output
2200 * @cur: the document being saved
2201 * @encoding: the name of the encoding to use or NULL.
2202 * @format: should formatting spaces be added.
2203 *
2204 * Dump an XML document to a file or an URL.
2205 *
2206 * Returns the number of bytes written or -1 in case of error.
2207 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
2208 * or xmlKeepBlanksDefault(0) was called
2209 */
2210int
2211xmlSaveFormatFileEnc( const char * filename, xmlDocPtr cur,
2212 const char * encoding, int format ) {
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002213 xmlSaveCtxt ctxt;
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002214 xmlOutputBufferPtr buf;
2215 xmlCharEncodingHandlerPtr handler = NULL;
2216 int ret;
2217
2218 if (cur == NULL)
2219 return(-1);
2220
2221 if (encoding == NULL)
2222 encoding = (const char *) cur->encoding;
2223
2224 if (encoding != NULL) {
2225
2226 handler = xmlFindCharEncodingHandler(encoding);
2227 if (handler == NULL)
2228 return(-1);
2229 }
2230
2231#ifdef HAVE_ZLIB_H
2232 if (cur->compression < 0) cur->compression = xmlGetCompressMode();
2233#endif
2234 /*
2235 * save the content to a temp buffer.
2236 */
2237 buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression);
2238 if (buf == NULL) return(-1);
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002239 memset(&ctxt, 0, sizeof(ctxt));
2240 ctxt.doc = cur;
2241 ctxt.buf = buf;
2242 ctxt.level = 0;
2243 ctxt.format = format;
2244 ctxt.encoding = (const xmlChar *) encoding;
Daniel Veillard753086a2004-03-28 16:12:44 +00002245 xmlSaveCtxtInit(&ctxt);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002246
Daniel Veillard32b7cdb2004-03-15 13:46:37 +00002247 xmlDocContentDumpOutput(&ctxt, cur);
Daniel Veillard1a8741c2004-03-04 13:40:59 +00002248
2249 ret = xmlOutputBufferClose(buf);
2250 return(ret);
2251}
2252
2253
2254/**
2255 * xmlSaveFileEnc:
2256 * @filename: the filename (or URL)
2257 * @cur: the document
2258 * @encoding: the name of an encoding (or NULL)
2259 *
2260 * Dump an XML document, converting it to the given encoding
2261 *
2262 * returns: the number of bytes written or -1 in case of failure.
2263 */
2264int
2265xmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) {
2266 return ( xmlSaveFormatFileEnc( filename, cur, encoding, 0 ) );
2267}
2268
2269/**
2270 * xmlSaveFormatFile:
2271 * @filename: the filename (or URL)
2272 * @cur: the document
2273 * @format: should formatting spaces been added
2274 *
2275 * Dump an XML document to a file. Will use compression if
2276 * compiled in and enabled. If @filename is "-" the stdout file is
2277 * used. If @format is set then the document will be indented on output.
2278 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
2279 * or xmlKeepBlanksDefault(0) was called
2280 *
2281 * returns: the number of bytes written or -1 in case of failure.
2282 */
2283int
2284xmlSaveFormatFile(const char *filename, xmlDocPtr cur, int format) {
2285 return ( xmlSaveFormatFileEnc( filename, cur, NULL, format ) );
2286}
2287
2288/**
2289 * xmlSaveFile:
2290 * @filename: the filename (or URL)
2291 * @cur: the document
2292 *
2293 * Dump an XML document to a file. Will use compression if
2294 * compiled in and enabled. If @filename is "-" the stdout file is
2295 * used.
2296 * returns: the number of bytes written or -1 in case of failure.
2297 */
2298int
2299xmlSaveFile(const char *filename, xmlDocPtr cur) {
2300 return(xmlSaveFormatFileEnc(filename, cur, NULL, 0));
2301}
2302
2303#endif /* LIBXML_OUTPUT_ENABLED */
2304
Daniel Veillard5d4644e2005-04-01 13:11:58 +00002305#define bottom_xmlsave
2306#include "elfgcchack.h"