VirtualBox

source: vbox/trunk/src/libs/libxml2-2.6.30/xmlsave.c@ 24363

Last change on this file since 24363 was 6076, checked in by vboxsync, 17 years ago

Merged dmik/s2 branch (r25959:26751) to the trunk.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Revision Author Id
File size: 65.8 KB
Line 
1/*
2 * xmlsave.c: Implemetation of the document serializer
3 *
4 * See Copyright for the status of this software.
5 *
6 * [email protected]
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>
17
18#define MAX_INDENT 60
19
20#include <libxml/HTMLtree.h>
21
22/************************************************************************
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}
66
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;
81 xmlOutputBufferPtr buf;
82 xmlDocPtr doc;
83 int options;
84 int level;
85 int format;
86 char indent[MAX_INDENT + 1]; /* array for indenting output */
87 int indent_nr;
88 int indent_size;
89 xmlCharEncodingOutputFunc escape; /* used for element content */
90 xmlCharEncodingOutputFunc escapeAttr;/* used for attribute content */
91};
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\n";
126 break;
127 case XML_SAVE_CHAR_INVALID:
128 msg = "invalid character value\n";
129 break;
130 case XML_SAVE_UNKNOWN_ENCODING:
131 msg = "unknown encoding %s\n";
132 break;
133 case XML_SAVE_NO_DOCTYPE:
134 msg = "document has no DOCTYPE\n";
135 break;
136 default:
137 msg = "unexpected error number\n";
138 }
139 __xmlSimpleError(XML_FROM_OUTPUT, code, node, msg, extra);
140}
141
142/************************************************************************
143 * *
144 * Special escaping routines *
145 * *
146 ************************************************************************/
147static 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
188/**
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 */
251 if (outend - out < 10) break;
252
253 if (*in < 0xC0) {
254 xmlSaveErr(XML_SAVE_NOT_UTF8, NULL, NULL);
255 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 {
282 xmlSaveErr(XML_SAVE_CHAR_INVALID, NULL, NULL);
283 in++;
284 goto error;
285 }
286 if (!IS_CHAR(val)) {
287 xmlSaveErr(XML_SAVE_CHAR_INVALID, NULL, NULL);
288 in++;
289 goto error;
290 }
291
292 /*
293 * We could do multiple things here. Just save as a char ref
294 */
295 out = xmlSerializeHexCharRef(out, val);
296 } else if (IS_BYTE_CHAR(*in)) {
297 if (outend - out < 6) break;
298 out = xmlSerializeHexCharRef(out, *in++);
299 } 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 * *
317 * Allocation and deallocation *
318 * *
319 ************************************************************************/
320/**
321 * xmlSaveCtxtInit:
322 * @ctxt: the saving context
323 *
324 * Initialize a saving context
325 */
326static void
327xmlSaveCtxtInit(xmlSaveCtxtPtr ctxt)
328{
329 int i;
330 int len;
331
332 if (ctxt == NULL) return;
333 if ((ctxt->encoding == NULL) && (ctxt->escape == NULL))
334 ctxt->escape = xmlEscapeEntities;
335 len = xmlStrlen((xmlChar *)xmlTreeIndentString);
336 if ((xmlTreeIndentString == NULL) || (len == 0)) {
337 memset(&ctxt->indent[0], 0, MAX_INDENT + 1);
338 } else {
339 ctxt->indent_size = len;
340 ctxt->indent_nr = MAX_INDENT / ctxt->indent_size;
341 for (i = 0;i < ctxt->indent_nr;i++)
342 memcpy(&ctxt->indent[i * ctxt->indent_size], xmlTreeIndentString,
343 ctxt->indent_size);
344 ctxt->indent[ctxt->indent_nr * ctxt->indent_size] = 0;
345 }
346
347 if (xmlSaveNoEmptyTags) {
348 ctxt->options |= XML_SAVE_NO_EMPTY;
349 }
350}
351
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);
363 if (ctxt->buf != NULL)
364 xmlOutputBufferClose(ctxt->buf);
365 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));
386
387 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);
395 ret->escape = NULL;
396 }
397 xmlSaveCtxtInit(ret);
398
399 /*
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
412 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
429xmlAttrSerializeContent(xmlOutputBufferPtr buf, xmlAttrPtr attr)
430{
431 xmlNodePtr children;
432
433 children = attr->children;
434 while (children != NULL) {
435 switch (children->type) {
436 case XML_TEXT_NODE:
437 xmlAttrSerializeTxtContent(buf->buffer, attr->doc,
438 attr, children->content);
439 break;
440 case XML_ENTITY_REF_NODE:
441 xmlBufferAdd(buf->buffer, BAD_CAST "&", 1);
442 xmlBufferAdd(buf->buffer, children->name,
443 xmlStrlen(children->name));
444 xmlBufferAdd(buf->buffer, BAD_CAST ";", 1);
445 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
462xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur);
463#endif
464static void xmlNodeListDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur);
465static void xmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur);
466void xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur);
467static int xmlDocContentDumpOutput(xmlSaveCtxtPtr ctxt, xmlDocPtr cur);
468
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) {
479 if ((cur == NULL) || (buf == NULL)) return;
480 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) {
486 xmlOutputBufferWrite(buf, 7, " xmlns:");
487 xmlOutputBufferWriteString(buf, (const char *)cur->prefix);
488 } else
489 xmlOutputBufferWrite(buf, 6, " xmlns");
490 xmlOutputBufferWrite(buf, 1, "=");
491 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
514 * @dtd: the pointer to the DTD
515 *
516 * Dump the XML document DTD, if any.
517 */
518static void
519xmlDtdDumpOutput(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;
528 xmlOutputBufferWrite(buf, 10, "<!DOCTYPE ");
529 xmlOutputBufferWriteString(buf, (const char *)dtd->name);
530 if (dtd->ExternalID != NULL) {
531 xmlOutputBufferWrite(buf, 8, " PUBLIC ");
532 xmlBufferWriteQuotedString(buf->buffer, dtd->ExternalID);
533 xmlOutputBufferWrite(buf, 1, " ");
534 xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID);
535 } else if (dtd->SystemID != NULL) {
536 xmlOutputBufferWrite(buf, 8, " SYSTEM ");
537 xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID);
538 }
539 if ((dtd->entities == NULL) && (dtd->elements == NULL) &&
540 (dtd->attributes == NULL) && (dtd->notations == NULL) &&
541 (dtd->pentities == NULL)) {
542 xmlOutputBufferWrite(buf, 1, ">");
543 return;
544 }
545 xmlOutputBufferWrite(buf, 3, " [\n");
546 /*
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))) {
552 xmlDumpNotationTable(buf->buffer, (xmlNotationTablePtr) dtd->notations);
553 }
554 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;
564 xmlOutputBufferWrite(buf, 2, "]>");
565}
566
567/**
568 * xmlAttrDumpOutput:
569 * @buf: the XML buffer output
570 * @cur: the attribute pointer
571 *
572 * Dump an XML attribute
573 */
574static void
575xmlAttrDumpOutput(xmlSaveCtxtPtr ctxt, xmlAttrPtr cur) {
576 xmlOutputBufferPtr buf;
577
578 if (cur == NULL) return;
579 buf = ctxt->buf;
580 if (buf == NULL) return;
581 xmlOutputBufferWrite(buf, 1, " ");
582 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
583 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
584 xmlOutputBufferWrite(buf, 1, ":");
585 }
586 xmlOutputBufferWriteString(buf, (const char *)cur->name);
587 xmlOutputBufferWrite(buf, 2, "=\"");
588 xmlAttrSerializeContent(buf, cur);
589 xmlOutputBufferWrite(buf, 1, "\"");
590}
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
602xmlAttrListDumpOutput(xmlSaveCtxtPtr ctxt, xmlAttrPtr cur) {
603 if (cur == NULL) return;
604 while (cur != NULL) {
605 xmlAttrDumpOutput(ctxt, cur);
606 cur = cur->next;
607 }
608}
609
610
611
612/**
613 * xmlNodeListDumpOutput:
614 * @cur: the first node
615 *
616 * Dump an XML node list, recursive behaviour, children are printed too.
617 */
618static void
619xmlNodeListDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
620 xmlOutputBufferPtr buf;
621
622 if (cur == NULL) return;
623 buf = ctxt->buf;
624 while (cur != NULL) {
625 if ((ctxt->format) && (xmlIndentTreeOutput) &&
626 ((cur->type == XML_ELEMENT_NODE) ||
627 (cur->type == XML_COMMENT_NODE) ||
628 (cur->type == XML_PI_NODE)))
629 xmlOutputBufferWrite(buf, ctxt->indent_size *
630 (ctxt->level > ctxt->indent_nr ?
631 ctxt->indent_nr : ctxt->level),
632 ctxt->indent);
633 xmlNodeDumpOutputInternal(ctxt, cur);
634 if (ctxt->format) {
635 xmlOutputBufferWrite(buf, 1, "\n");
636 }
637 cur = cur->next;
638 }
639}
640
641/**
642 * xmlNodeDumpOutputInternal:
643 * @cur: the current node
644 *
645 * Dump an XML node, recursive behaviour, children are printed too.
646 */
647static void
648xmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
649 int format;
650 xmlNodePtr tmp;
651 xmlChar *start, *end;
652 xmlOutputBufferPtr buf;
653
654 if (cur == NULL) return;
655 buf = ctxt->buf;
656 if (cur->type == XML_XINCLUDE_START)
657 return;
658 if (cur->type == XML_XINCLUDE_END)
659 return;
660 if ((cur->type == XML_DOCUMENT_NODE) ||
661 (cur->type == XML_HTML_DOCUMENT_NODE)) {
662 xmlDocContentDumpOutput(ctxt, (xmlDocPtr) cur);
663 return;
664 }
665 if (cur->type == XML_DTD_NODE) {
666 xmlDtdDumpOutput(ctxt, (xmlDtdPtr) cur);
667 return;
668 }
669 if (cur->type == XML_DOCUMENT_FRAG_NODE) {
670 xmlNodeListDumpOutput(ctxt, cur->children);
671 return;
672 }
673 if (cur->type == XML_ELEMENT_DECL) {
674 xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur);
675 return;
676 }
677 if (cur->type == XML_ATTRIBUTE_DECL) {
678 xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur);
679 return;
680 }
681 if (cur->type == XML_ENTITY_DECL) {
682 xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur);
683 return;
684 }
685 if (cur->type == XML_TEXT_NODE) {
686 if (cur->content != NULL) {
687 if (cur->name != xmlStringTextNoenc) {
688 xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape);
689 } else {
690 /*
691 * Disable escaping, needed for XSLT
692 */
693 xmlOutputBufferWriteString(buf, (const char *) cur->content);
694 }
695 }
696
697 return;
698 }
699 if (cur->type == XML_PI_NODE) {
700 if (cur->content != NULL) {
701 xmlOutputBufferWrite(buf, 2, "<?");
702 xmlOutputBufferWriteString(buf, (const char *)cur->name);
703 if (cur->content != NULL) {
704 xmlOutputBufferWrite(buf, 1, " ");
705 xmlOutputBufferWriteString(buf, (const char *)cur->content);
706 }
707 xmlOutputBufferWrite(buf, 2, "?>");
708 } else {
709 xmlOutputBufferWrite(buf, 2, "<?");
710 xmlOutputBufferWriteString(buf, (const char *)cur->name);
711 xmlOutputBufferWrite(buf, 2, "?>");
712 }
713 return;
714 }
715 if (cur->type == XML_COMMENT_NODE) {
716 if (cur->content != NULL) {
717 xmlOutputBufferWrite(buf, 4, "<!--");
718 xmlOutputBufferWriteString(buf, (const char *)cur->content);
719 xmlOutputBufferWrite(buf, 3, "-->");
720 }
721 return;
722 }
723 if (cur->type == XML_ENTITY_REF_NODE) {
724 xmlOutputBufferWrite(buf, 1, "&");
725 xmlOutputBufferWriteString(buf, (const char *)cur->name);
726 xmlOutputBufferWrite(buf, 1, ";");
727 return;
728 }
729 if (cur->type == XML_CDATA_SECTION_NODE) {
730 if (cur->content == NULL) {
731 xmlOutputBufferWrite(buf, 12, "<![CDATA[]]>");
732 } else {
733 start = end = cur->content;
734 while (*end != '\0') {
735 if ((*end == ']') && (*(end + 1) == ']') &&
736 (*(end + 2) == '>')) {
737 end = end + 2;
738 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
739 xmlOutputBufferWrite(buf, end - start, (const char *)start);
740 xmlOutputBufferWrite(buf, 3, "]]>");
741 start = end;
742 }
743 end++;
744 }
745 if (start != end) {
746 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
747 xmlOutputBufferWriteString(buf, (const char *)start);
748 xmlOutputBufferWrite(buf, 3, "]]>");
749 }
750 }
751 return;
752 }
753 if (cur->type == XML_ATTRIBUTE_NODE) {
754 xmlAttrDumpOutput(ctxt, (xmlAttrPtr) cur);
755 return;
756 }
757 if (cur->type == XML_NAMESPACE_DECL) {
758 xmlNsDumpOutput(buf, (xmlNsPtr) cur);
759 return;
760 }
761
762 format = ctxt->format;
763 if (format == 1) {
764 tmp = cur->children;
765 while (tmp != NULL) {
766 if ((tmp->type == XML_TEXT_NODE) ||
767 (tmp->type == XML_CDATA_SECTION_NODE) ||
768 (tmp->type == XML_ENTITY_REF_NODE)) {
769 ctxt->format = 0;
770 break;
771 }
772 tmp = tmp->next;
773 }
774 }
775 xmlOutputBufferWrite(buf, 1, "<");
776 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
777 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
778 xmlOutputBufferWrite(buf, 1, ":");
779 }
780
781 xmlOutputBufferWriteString(buf, (const char *)cur->name);
782 if (cur->nsDef)
783 xmlNsListDumpOutput(buf, cur->nsDef);
784 if (cur->properties != NULL)
785 xmlAttrListDumpOutput(ctxt, cur->properties);
786
787 if (((cur->type == XML_ELEMENT_NODE) || (cur->content == NULL)) &&
788 (cur->children == NULL) && ((ctxt->options & XML_SAVE_NO_EMPTY) == 0)) {
789 xmlOutputBufferWrite(buf, 2, "/>");
790 ctxt->format = format;
791 return;
792 }
793 xmlOutputBufferWrite(buf, 1, ">");
794 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
795 xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape);
796 }
797 if (cur->children != NULL) {
798 if (ctxt->format) xmlOutputBufferWrite(buf, 1, "\n");
799 if (ctxt->level >= 0) ctxt->level++;
800 xmlNodeListDumpOutput(ctxt, cur->children);
801 if (ctxt->level > 0) ctxt->level--;
802 if ((xmlIndentTreeOutput) && (ctxt->format))
803 xmlOutputBufferWrite(buf, ctxt->indent_size *
804 (ctxt->level > ctxt->indent_nr ?
805 ctxt->indent_nr : ctxt->level),
806 ctxt->indent);
807 }
808 xmlOutputBufferWrite(buf, 2, "</");
809 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
810 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
811 xmlOutputBufferWrite(buf, 1, ":");
812 }
813
814 xmlOutputBufferWriteString(buf, (const char *)cur->name);
815 xmlOutputBufferWrite(buf, 1, ">");
816 ctxt->format = format;
817}
818
819/**
820 * xmlDocContentDumpOutput:
821 * @cur: the document
822 *
823 * Dump an XML document.
824 */
825static int
826xmlDocContentDumpOutput(xmlSaveCtxtPtr ctxt, xmlDocPtr cur) {
827#ifdef LIBXML_HTML_ENABLED
828 xmlDtdPtr dtd;
829 int is_xhtml = 0;
830#endif
831 const xmlChar *oldenc = cur->encoding;
832 const xmlChar *oldctxtenc = ctxt->encoding;
833 const xmlChar *encoding = ctxt->encoding;
834 xmlCharEncodingOutputFunc oldescape = ctxt->escape;
835 xmlCharEncodingOutputFunc oldescapeAttr = ctxt->escapeAttr;
836 xmlOutputBufferPtr buf = ctxt->buf;
837 xmlCharEncoding enc;
838
839 xmlInitParser();
840
841 if (ctxt->encoding != NULL) {
842 cur->encoding = BAD_CAST ctxt->encoding;
843 } else if (cur->encoding != NULL) {
844 encoding = cur->encoding;
845 } else if (cur->charset != XML_CHAR_ENCODING_UTF8) {
846 encoding = (const xmlChar *)
847 xmlGetCharEncodingName((xmlCharEncoding) cur->charset);
848 }
849
850 enc = xmlParseCharEncoding((const char*) encoding);
851 if ((encoding != NULL) && (oldctxtenc == NULL) &&
852 (buf->encoder == NULL) && (buf->conv == NULL) &&
853 ((ctxt->options & XML_SAVE_NO_DECL) == 0)) {
854 if ((enc != XML_CHAR_ENCODING_UTF8) &&
855 (enc != XML_CHAR_ENCODING_NONE) &&
856 (enc != XML_CHAR_ENCODING_ASCII)) {
857 /*
858 * we need to switch to this encoding but just for this document
859 * since we output the XMLDecl the conversion must be done to not
860 * generate not well formed documents.
861 */
862 buf->encoder = xmlFindCharEncodingHandler((const char *)encoding);
863 if (buf->encoder == NULL) {
864 xmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL,
865 (const char *)encoding);
866 return(-1);
867 }
868 buf->conv = xmlBufferCreate();
869 if (buf->conv == NULL) {
870 xmlCharEncCloseFunc(buf->encoder);
871 xmlSaveErrMemory("creating encoding buffer");
872 return(-1);
873 }
874 /*
875 * initialize the state, e.g. if outputting a BOM
876 */
877 xmlCharEncOutFunc(buf->encoder, buf->conv, NULL);
878 }
879 if (ctxt->escape == xmlEscapeEntities)
880 ctxt->escape = NULL;
881 if (ctxt->escapeAttr == xmlEscapeEntities)
882 ctxt->escapeAttr = NULL;
883 }
884
885
886 /*
887 * Save the XML declaration
888 */
889 if ((ctxt->options & XML_SAVE_NO_DECL) == 0) {
890 xmlOutputBufferWrite(buf, 14, "<?xml version=");
891 if (cur->version != NULL)
892 xmlBufferWriteQuotedString(buf->buffer, cur->version);
893 else
894 xmlOutputBufferWrite(buf, 5, "\"1.0\"");
895 if (encoding != NULL) {
896 xmlOutputBufferWrite(buf, 10, " encoding=");
897 xmlBufferWriteQuotedString(buf->buffer, (xmlChar *) encoding);
898 }
899 switch (cur->standalone) {
900 case 0:
901 xmlOutputBufferWrite(buf, 16, " standalone=\"no\"");
902 break;
903 case 1:
904 xmlOutputBufferWrite(buf, 17, " standalone=\"yes\"");
905 break;
906 }
907 xmlOutputBufferWrite(buf, 3, "?>\n");
908 }
909
910#ifdef LIBXML_HTML_ENABLED
911 if ((ctxt->options & XML_SAVE_NO_XHTML) == 0) {
912 dtd = xmlGetIntSubset(cur);
913 if (dtd != NULL) {
914 is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
915 if (is_xhtml < 0) is_xhtml = 0;
916 }
917 }
918#endif
919 if (cur->children != NULL) {
920 xmlNodePtr child = cur->children;
921
922 while (child != NULL) {
923 ctxt->level = 0;
924#ifdef LIBXML_HTML_ENABLED
925 if (is_xhtml)
926 xhtmlNodeDumpOutput(ctxt, child);
927 else
928#endif
929 xmlNodeDumpOutputInternal(ctxt, child);
930 xmlOutputBufferWrite(buf, 1, "\n");
931 child = child->next;
932 }
933 }
934 if (ctxt->encoding != NULL)
935 cur->encoding = oldenc;
936
937 /*
938 * Restore the state of the saving context at the end of the document
939 */
940 if ((encoding != NULL) && (oldctxtenc == NULL) &&
941 ((ctxt->options & XML_SAVE_NO_DECL) == 0)) {
942 if ((enc != XML_CHAR_ENCODING_UTF8) &&
943 (enc != XML_CHAR_ENCODING_NONE) &&
944 (enc != XML_CHAR_ENCODING_ASCII)) {
945 xmlOutputBufferFlush(buf);
946 xmlCharEncCloseFunc(buf->encoder);
947 xmlBufferFree(buf->conv);
948 buf->encoder = NULL;
949 buf->conv = NULL;
950 }
951 ctxt->escape = oldescape;
952 ctxt->escapeAttr = oldescapeAttr;
953 }
954 return(0);
955}
956
957#ifdef LIBXML_HTML_ENABLED
958/************************************************************************
959 * *
960 * Functions specific to XHTML serialization *
961 * *
962 ************************************************************************/
963
964/**
965 * xhtmlIsEmpty:
966 * @node: the node
967 *
968 * Check if a node is an empty xhtml node
969 *
970 * Returns 1 if the node is an empty node, 0 if not and -1 in case of error
971 */
972static int
973xhtmlIsEmpty(xmlNodePtr node) {
974 if (node == NULL)
975 return(-1);
976 if (node->type != XML_ELEMENT_NODE)
977 return(0);
978 if ((node->ns != NULL) && (!xmlStrEqual(node->ns->href, XHTML_NS_NAME)))
979 return(0);
980 if (node->children != NULL)
981 return(0);
982 switch (node->name[0]) {
983 case 'a':
984 if (xmlStrEqual(node->name, BAD_CAST "area"))
985 return(1);
986 return(0);
987 case 'b':
988 if (xmlStrEqual(node->name, BAD_CAST "br"))
989 return(1);
990 if (xmlStrEqual(node->name, BAD_CAST "base"))
991 return(1);
992 if (xmlStrEqual(node->name, BAD_CAST "basefont"))
993 return(1);
994 return(0);
995 case 'c':
996 if (xmlStrEqual(node->name, BAD_CAST "col"))
997 return(1);
998 return(0);
999 case 'f':
1000 if (xmlStrEqual(node->name, BAD_CAST "frame"))
1001 return(1);
1002 return(0);
1003 case 'h':
1004 if (xmlStrEqual(node->name, BAD_CAST "hr"))
1005 return(1);
1006 return(0);
1007 case 'i':
1008 if (xmlStrEqual(node->name, BAD_CAST "img"))
1009 return(1);
1010 if (xmlStrEqual(node->name, BAD_CAST "input"))
1011 return(1);
1012 if (xmlStrEqual(node->name, BAD_CAST "isindex"))
1013 return(1);
1014 return(0);
1015 case 'l':
1016 if (xmlStrEqual(node->name, BAD_CAST "link"))
1017 return(1);
1018 return(0);
1019 case 'm':
1020 if (xmlStrEqual(node->name, BAD_CAST "meta"))
1021 return(1);
1022 return(0);
1023 case 'p':
1024 if (xmlStrEqual(node->name, BAD_CAST "param"))
1025 return(1);
1026 return(0);
1027 }
1028 return(0);
1029}
1030
1031/**
1032 * xhtmlAttrListDumpOutput:
1033 * @cur: the first attribute pointer
1034 *
1035 * Dump a list of XML attributes
1036 */
1037static void
1038xhtmlAttrListDumpOutput(xmlSaveCtxtPtr ctxt, xmlAttrPtr cur) {
1039 xmlAttrPtr xml_lang = NULL;
1040 xmlAttrPtr lang = NULL;
1041 xmlAttrPtr name = NULL;
1042 xmlAttrPtr id = NULL;
1043 xmlNodePtr parent;
1044 xmlOutputBufferPtr buf;
1045
1046 if (cur == NULL) return;
1047 buf = ctxt->buf;
1048 parent = cur->parent;
1049 while (cur != NULL) {
1050 if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "id")))
1051 id = cur;
1052 else
1053 if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "name")))
1054 name = cur;
1055 else
1056 if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")))
1057 lang = cur;
1058 else
1059 if ((cur->ns != NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")) &&
1060 (xmlStrEqual(cur->ns->prefix, BAD_CAST "xml")))
1061 xml_lang = cur;
1062 else if ((cur->ns == NULL) &&
1063 ((cur->children == NULL) ||
1064 (cur->children->content == NULL) ||
1065 (cur->children->content[0] == 0)) &&
1066 (htmlIsBooleanAttr(cur->name))) {
1067 if (cur->children != NULL)
1068 xmlFreeNode(cur->children);
1069 cur->children = xmlNewText(cur->name);
1070 if (cur->children != NULL)
1071 cur->children->parent = (xmlNodePtr) cur;
1072 }
1073 xmlAttrDumpOutput(ctxt, cur);
1074 cur = cur->next;
1075 }
1076 /*
1077 * C.8
1078 */
1079 if ((name != NULL) && (id == NULL)) {
1080 if ((parent != NULL) && (parent->name != NULL) &&
1081 ((xmlStrEqual(parent->name, BAD_CAST "a")) ||
1082 (xmlStrEqual(parent->name, BAD_CAST "p")) ||
1083 (xmlStrEqual(parent->name, BAD_CAST "div")) ||
1084 (xmlStrEqual(parent->name, BAD_CAST "img")) ||
1085 (xmlStrEqual(parent->name, BAD_CAST "map")) ||
1086 (xmlStrEqual(parent->name, BAD_CAST "applet")) ||
1087 (xmlStrEqual(parent->name, BAD_CAST "form")) ||
1088 (xmlStrEqual(parent->name, BAD_CAST "frame")) ||
1089 (xmlStrEqual(parent->name, BAD_CAST "iframe")))) {
1090 xmlOutputBufferWrite(buf, 5, " id=\"");
1091 xmlAttrSerializeContent(buf, name);
1092 xmlOutputBufferWrite(buf, 1, "\"");
1093 }
1094 }
1095 /*
1096 * C.7.
1097 */
1098 if ((lang != NULL) && (xml_lang == NULL)) {
1099 xmlOutputBufferWrite(buf, 11, " xml:lang=\"");
1100 xmlAttrSerializeContent(buf, lang);
1101 xmlOutputBufferWrite(buf, 1, "\"");
1102 } else
1103 if ((xml_lang != NULL) && (lang == NULL)) {
1104 xmlOutputBufferWrite(buf, 7, " lang=\"");
1105 xmlAttrSerializeContent(buf, xml_lang);
1106 xmlOutputBufferWrite(buf, 1, "\"");
1107 }
1108}
1109
1110/**
1111 * xhtmlNodeListDumpOutput:
1112 * @buf: the XML buffer output
1113 * @doc: the XHTML document
1114 * @cur: the first node
1115 * @level: the imbrication level for indenting
1116 * @format: is formatting allowed
1117 * @encoding: an optional encoding string
1118 *
1119 * Dump an XML node list, recursive behaviour, children are printed too.
1120 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
1121 * or xmlKeepBlanksDefault(0) was called
1122 */
1123static void
1124xhtmlNodeListDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
1125 xmlOutputBufferPtr buf;
1126
1127 if (cur == NULL) return;
1128 buf = ctxt->buf;
1129 while (cur != NULL) {
1130 if ((ctxt->format) && (xmlIndentTreeOutput) &&
1131 (cur->type == XML_ELEMENT_NODE))
1132 xmlOutputBufferWrite(buf, ctxt->indent_size *
1133 (ctxt->level > ctxt->indent_nr ?
1134 ctxt->indent_nr : ctxt->level),
1135 ctxt->indent);
1136 xhtmlNodeDumpOutput(ctxt, cur);
1137 if (ctxt->format) {
1138 xmlOutputBufferWrite(buf, 1, "\n");
1139 }
1140 cur = cur->next;
1141 }
1142}
1143
1144/**
1145 * xhtmlNodeDumpOutput:
1146 * @buf: the XML buffer output
1147 * @doc: the XHTML document
1148 * @cur: the current node
1149 * @level: the imbrication level for indenting
1150 * @format: is formatting allowed
1151 * @encoding: an optional encoding string
1152 *
1153 * Dump an XHTML node, recursive behaviour, children are printed too.
1154 */
1155static void
1156xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
1157 int format, addmeta = 0;
1158 xmlNodePtr tmp;
1159 xmlChar *start, *end;
1160 xmlOutputBufferPtr buf;
1161
1162 if (cur == NULL) return;
1163 if ((cur->type == XML_DOCUMENT_NODE) ||
1164 (cur->type == XML_HTML_DOCUMENT_NODE)) {
1165 xmlDocContentDumpOutput(ctxt, (xmlDocPtr) cur);
1166 return;
1167 }
1168 if (cur->type == XML_XINCLUDE_START)
1169 return;
1170 if (cur->type == XML_XINCLUDE_END)
1171 return;
1172 if (cur->type == XML_DTD_NODE) {
1173 xmlDtdDumpOutput(ctxt, (xmlDtdPtr) cur);
1174 return;
1175 }
1176 if (cur->type == XML_DOCUMENT_FRAG_NODE) {
1177 xhtmlNodeListDumpOutput(ctxt, cur->children);
1178 return;
1179 }
1180 buf = ctxt->buf;
1181 if (cur->type == XML_ELEMENT_DECL) {
1182 xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur);
1183 return;
1184 }
1185 if (cur->type == XML_ATTRIBUTE_DECL) {
1186 xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur);
1187 return;
1188 }
1189 if (cur->type == XML_ENTITY_DECL) {
1190 xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur);
1191 return;
1192 }
1193 if (cur->type == XML_TEXT_NODE) {
1194 if (cur->content != NULL) {
1195 if ((cur->name == xmlStringText) ||
1196 (cur->name != xmlStringTextNoenc)) {
1197 xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape);
1198 } else {
1199 /*
1200 * Disable escaping, needed for XSLT
1201 */
1202 xmlOutputBufferWriteString(buf, (const char *) cur->content);
1203 }
1204 }
1205
1206 return;
1207 }
1208 if (cur->type == XML_PI_NODE) {
1209 if (cur->content != NULL) {
1210 xmlOutputBufferWrite(buf, 2, "<?");
1211 xmlOutputBufferWriteString(buf, (const char *)cur->name);
1212 if (cur->content != NULL) {
1213 xmlOutputBufferWrite(buf, 1, " ");
1214 xmlOutputBufferWriteString(buf, (const char *)cur->content);
1215 }
1216 xmlOutputBufferWrite(buf, 2, "?>");
1217 } else {
1218 xmlOutputBufferWrite(buf, 2, "<?");
1219 xmlOutputBufferWriteString(buf, (const char *)cur->name);
1220 xmlOutputBufferWrite(buf, 2, "?>");
1221 }
1222 return;
1223 }
1224 if (cur->type == XML_COMMENT_NODE) {
1225 if (cur->content != NULL) {
1226 xmlOutputBufferWrite(buf, 4, "<!--");
1227 xmlOutputBufferWriteString(buf, (const char *)cur->content);
1228 xmlOutputBufferWrite(buf, 3, "-->");
1229 }
1230 return;
1231 }
1232 if (cur->type == XML_ENTITY_REF_NODE) {
1233 xmlOutputBufferWrite(buf, 1, "&");
1234 xmlOutputBufferWriteString(buf, (const char *)cur->name);
1235 xmlOutputBufferWrite(buf, 1, ";");
1236 return;
1237 }
1238 if (cur->type == XML_CDATA_SECTION_NODE) {
1239 start = end = cur->content;
1240 while (*end != '\0') {
1241 if (*end == ']' && *(end + 1) == ']' && *(end + 2) == '>') {
1242 end = end + 2;
1243 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
1244 xmlOutputBufferWrite(buf, end - start, (const char *)start);
1245 xmlOutputBufferWrite(buf, 3, "]]>");
1246 start = end;
1247 }
1248 end++;
1249 }
1250 if (start != end) {
1251 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
1252 xmlOutputBufferWriteString(buf, (const char *)start);
1253 xmlOutputBufferWrite(buf, 3, "]]>");
1254 }
1255 return;
1256 }
1257
1258 format = ctxt->format;
1259 if (format == 1) {
1260 tmp = cur->children;
1261 while (tmp != NULL) {
1262 if ((tmp->type == XML_TEXT_NODE) ||
1263 (tmp->type == XML_ENTITY_REF_NODE)) {
1264 format = 0;
1265 break;
1266 }
1267 tmp = tmp->next;
1268 }
1269 }
1270 xmlOutputBufferWrite(buf, 1, "<");
1271 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1272 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
1273 xmlOutputBufferWrite(buf, 1, ":");
1274 }
1275
1276 xmlOutputBufferWriteString(buf, (const char *)cur->name);
1277 if (cur->nsDef)
1278 xmlNsListDumpOutput(buf, cur->nsDef);
1279 if ((xmlStrEqual(cur->name, BAD_CAST "html") &&
1280 (cur->ns == NULL) && (cur->nsDef == NULL))) {
1281 /*
1282 * 3.1.1. Strictly Conforming Documents A.3.1.1 3/
1283 */
1284 xmlOutputBufferWriteString(buf,
1285 " xmlns=\"http://www.w3.org/1999/xhtml\"");
1286 }
1287 if (cur->properties != NULL)
1288 xhtmlAttrListDumpOutput(ctxt, cur->properties);
1289
1290 if ((cur->type == XML_ELEMENT_NODE) &&
1291 (cur->parent != NULL) &&
1292 (cur->parent->parent == (xmlNodePtr) cur->doc) &&
1293 xmlStrEqual(cur->name, BAD_CAST"head") &&
1294 xmlStrEqual(cur->parent->name, BAD_CAST"html")) {
1295
1296 tmp = cur->children;
1297 while (tmp != NULL) {
1298 if (xmlStrEqual(tmp->name, BAD_CAST"meta")) {
1299 xmlChar *httpequiv;
1300
1301 httpequiv = xmlGetProp(tmp, BAD_CAST"http-equiv");
1302 if (httpequiv != NULL) {
1303 if (xmlStrcasecmp(httpequiv, BAD_CAST"Content-Type") == 0) {
1304 xmlFree(httpequiv);
1305 break;
1306 }
1307 xmlFree(httpequiv);
1308 }
1309 }
1310 tmp = tmp->next;
1311 }
1312 if (tmp == NULL)
1313 addmeta = 1;
1314 }
1315
1316 if ((cur->type == XML_ELEMENT_NODE) && (cur->children == NULL)) {
1317 if (((cur->ns == NULL) || (cur->ns->prefix == NULL)) &&
1318 ((xhtmlIsEmpty(cur) == 1) && (addmeta == 0))) {
1319 /*
1320 * C.2. Empty Elements
1321 */
1322 xmlOutputBufferWrite(buf, 3, " />");
1323 } else {
1324 if (addmeta == 1) {
1325 xmlOutputBufferWrite(buf, 1, ">");
1326 if (ctxt->format) {
1327 xmlOutputBufferWrite(buf, 1, "\n");
1328 if (xmlIndentTreeOutput)
1329 xmlOutputBufferWrite(buf, ctxt->indent_size *
1330 (ctxt->level + 1 > ctxt->indent_nr ?
1331 ctxt->indent_nr : ctxt->level + 1), ctxt->indent);
1332 }
1333 xmlOutputBufferWriteString(buf,
1334 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=");
1335 if (ctxt->encoding) {
1336 xmlOutputBufferWriteString(buf, (const char *)ctxt->encoding);
1337 } else {
1338 xmlOutputBufferWrite(buf, 5, "UTF-8");
1339 }
1340 xmlOutputBufferWrite(buf, 4, "\" />");
1341 if (ctxt->format)
1342 xmlOutputBufferWrite(buf, 1, "\n");
1343 } else {
1344 xmlOutputBufferWrite(buf, 1, ">");
1345 }
1346 /*
1347 * C.3. Element Minimization and Empty Element Content
1348 */
1349 xmlOutputBufferWrite(buf, 2, "</");
1350 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1351 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
1352 xmlOutputBufferWrite(buf, 1, ":");
1353 }
1354 xmlOutputBufferWriteString(buf, (const char *)cur->name);
1355 xmlOutputBufferWrite(buf, 1, ">");
1356 }
1357 return;
1358 }
1359 xmlOutputBufferWrite(buf, 1, ">");
1360 if (addmeta == 1) {
1361 if (ctxt->format) {
1362 xmlOutputBufferWrite(buf, 1, "\n");
1363 if (xmlIndentTreeOutput)
1364 xmlOutputBufferWrite(buf, ctxt->indent_size *
1365 (ctxt->level + 1 > ctxt->indent_nr ?
1366 ctxt->indent_nr : ctxt->level + 1), ctxt->indent);
1367 }
1368 xmlOutputBufferWriteString(buf,
1369 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=");
1370 if (ctxt->encoding) {
1371 xmlOutputBufferWriteString(buf, (const char *)ctxt->encoding);
1372 } else {
1373 xmlOutputBufferWrite(buf, 5, "UTF-8");
1374 }
1375 xmlOutputBufferWrite(buf, 4, "\" />");
1376 }
1377 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
1378 xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape);
1379 }
1380
1381#if 0
1382 /*
1383 * This was removed due to problems with HTML processors.
1384 * See bug #345147.
1385 */
1386 /*
1387 * 4.8. Script and Style elements
1388 */
1389 if ((cur->type == XML_ELEMENT_NODE) &&
1390 ((xmlStrEqual(cur->name, BAD_CAST "script")) ||
1391 (xmlStrEqual(cur->name, BAD_CAST "style"))) &&
1392 ((cur->ns == NULL) ||
1393 (xmlStrEqual(cur->ns->href, XHTML_NS_NAME)))) {
1394 xmlNodePtr child = cur->children;
1395
1396 while (child != NULL) {
1397 if (child->type == XML_TEXT_NODE) {
1398 if ((xmlStrchr(child->content, '<') == NULL) &&
1399 (xmlStrchr(child->content, '&') == NULL) &&
1400 (xmlStrstr(child->content, BAD_CAST "]]>") == NULL)) {
1401 /* Nothing to escape, so just output as is... */
1402 /* FIXME: Should we do something about "--" also? */
1403 int level = ctxt->level;
1404 int indent = ctxt->format;
1405
1406 ctxt->level = 0;
1407 ctxt->format = 0;
1408 xmlOutputBufferWriteString(buf, (const char *) child->content);
1409 /* (We cannot use xhtmlNodeDumpOutput() here because
1410 * we wish to leave '>' unescaped!) */
1411 ctxt->level = level;
1412 ctxt->format = indent;
1413 } else {
1414 /* We must use a CDATA section. Unfortunately,
1415 * this will break CSS and JavaScript when read by
1416 * a browser in HTML4-compliant mode. :-( */
1417 start = end = child->content;
1418 while (*end != '\0') {
1419 if (*end == ']' &&
1420 *(end + 1) == ']' &&
1421 *(end + 2) == '>') {
1422 end = end + 2;
1423 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
1424 xmlOutputBufferWrite(buf, end - start,
1425 (const char *)start);
1426 xmlOutputBufferWrite(buf, 3, "]]>");
1427 start = end;
1428 }
1429 end++;
1430 }
1431 if (start != end) {
1432 xmlOutputBufferWrite(buf, 9, "<![CDATA[");
1433 xmlOutputBufferWrite(buf, end - start,
1434 (const char *)start);
1435 xmlOutputBufferWrite(buf, 3, "]]>");
1436 }
1437 }
1438 } else {
1439 int level = ctxt->level;
1440 int indent = ctxt->format;
1441
1442 ctxt->level = 0;
1443 ctxt->format = 0;
1444 xhtmlNodeDumpOutput(ctxt, child);
1445 ctxt->level = level;
1446 ctxt->format = indent;
1447 }
1448 child = child->next;
1449 }
1450 }
1451#endif
1452
1453 if (cur->children != NULL) {
1454 int indent = ctxt->format;
1455
1456 if (format) xmlOutputBufferWrite(buf, 1, "\n");
1457 if (ctxt->level >= 0) ctxt->level++;
1458 ctxt->format = format;
1459 xhtmlNodeListDumpOutput(ctxt, cur->children);
1460 if (ctxt->level > 0) ctxt->level--;
1461 ctxt->format = indent;
1462 if ((xmlIndentTreeOutput) && (format))
1463 xmlOutputBufferWrite(buf, ctxt->indent_size *
1464 (ctxt->level > ctxt->indent_nr ?
1465 ctxt->indent_nr : ctxt->level),
1466 ctxt->indent);
1467 }
1468 xmlOutputBufferWrite(buf, 2, "</");
1469 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1470 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
1471 xmlOutputBufferWrite(buf, 1, ":");
1472 }
1473
1474 xmlOutputBufferWriteString(buf, (const char *)cur->name);
1475 xmlOutputBufferWrite(buf, 1, ">");
1476}
1477#endif
1478
1479/************************************************************************
1480 * *
1481 * Public entry points *
1482 * *
1483 ************************************************************************/
1484
1485/**
1486 * xmlSaveToFd:
1487 * @fd: a file descriptor number
1488 * @encoding: the encoding name to use or NULL
1489 * @options: a set of xmlSaveOptions
1490 *
1491 * Create a document saving context serializing to a file descriptor
1492 * with the encoding and the options given.
1493 *
1494 * Returns a new serialization context or NULL in case of error.
1495 */
1496xmlSaveCtxtPtr
1497xmlSaveToFd(int fd, const char *encoding, int options)
1498{
1499 xmlSaveCtxtPtr ret;
1500
1501 ret = xmlNewSaveCtxt(encoding, options);
1502 if (ret == NULL) return(NULL);
1503 ret->buf = xmlOutputBufferCreateFd(fd, ret->handler);
1504 if (ret->buf == NULL) {
1505 xmlFreeSaveCtxt(ret);
1506 return(NULL);
1507 }
1508 return(ret);
1509}
1510
1511/**
1512 * xmlSaveToFilename:
1513 * @filename: a file name or an URL
1514 * @encoding: the encoding name to use or NULL
1515 * @options: a set of xmlSaveOptions
1516 *
1517 * Create a document saving context serializing to a filename or possibly
1518 * to an URL (but this is less reliable) with the encoding and the options
1519 * given.
1520 *
1521 * Returns a new serialization context or NULL in case of error.
1522 */
1523xmlSaveCtxtPtr
1524xmlSaveToFilename(const char *filename, const char *encoding, int options)
1525{
1526 xmlSaveCtxtPtr ret;
1527 int compression = 0; /* TODO handle compression option */
1528
1529 ret = xmlNewSaveCtxt(encoding, options);
1530 if (ret == NULL) return(NULL);
1531 ret->buf = xmlOutputBufferCreateFilename(filename, ret->handler,
1532 compression);
1533 if (ret->buf == NULL) {
1534 xmlFreeSaveCtxt(ret);
1535 return(NULL);
1536 }
1537 return(ret);
1538}
1539
1540/**
1541 * xmlSaveToBuffer:
1542 * @buffer: a buffer
1543 * @encoding: the encoding name to use or NULL
1544 * @options: a set of xmlSaveOptions
1545 *
1546 * Create a document saving context serializing to a buffer
1547 * with the encoding and the options given
1548 *
1549 * Returns a new serialization context or NULL in case of error.
1550 */
1551
1552xmlSaveCtxtPtr
1553xmlSaveToBuffer(xmlBufferPtr buffer, const char *encoding, int options)
1554{
1555 xmlSaveCtxtPtr ret;
1556 xmlOutputBufferPtr out_buff;
1557 xmlCharEncodingHandlerPtr handler;
1558
1559 ret = xmlNewSaveCtxt(encoding, options);
1560 if (ret == NULL) return(NULL);
1561
1562 if (encoding != NULL) {
1563 handler = xmlFindCharEncodingHandler(encoding);
1564 if (handler == NULL) {
1565 xmlFree(ret);
1566 return(NULL);
1567 }
1568 } else
1569 handler = NULL;
1570 out_buff = xmlOutputBufferCreateBuffer(buffer, handler);
1571 if (out_buff == NULL) {
1572 xmlFree(ret);
1573 if (handler) xmlCharEncCloseFunc(handler);
1574 return(NULL);
1575 }
1576
1577 ret->buf = out_buff;
1578 return(ret);
1579}
1580
1581/**
1582 * xmlSaveToIO:
1583 * @iowrite: an I/O write function
1584 * @ioclose: an I/O close function
1585 * @ioctx: an I/O handler
1586 * @encoding: the encoding name to use or NULL
1587 * @options: a set of xmlSaveOptions
1588 *
1589 * Create a document saving context serializing to a file descriptor
1590 * with the encoding and the options given
1591 *
1592 * Returns a new serialization context or NULL in case of error.
1593 */
1594xmlSaveCtxtPtr
1595xmlSaveToIO(xmlOutputWriteCallback iowrite,
1596 xmlOutputCloseCallback ioclose,
1597 void *ioctx, const char *encoding, int options)
1598{
1599 xmlSaveCtxtPtr ret;
1600
1601 ret = xmlNewSaveCtxt(encoding, options);
1602 if (ret == NULL) return(NULL);
1603 ret->buf = xmlOutputBufferCreateIO(iowrite, ioclose, ioctx, ret->handler);
1604 if (ret->buf == NULL) {
1605 xmlFreeSaveCtxt(ret);
1606 return(NULL);
1607 }
1608 return(ret);
1609}
1610
1611/**
1612 * xmlSaveDoc:
1613 * @ctxt: a document saving context
1614 * @doc: a document
1615 *
1616 * Save a full document to a saving context
1617 * TODO: The function is not fully implemented yet as it does not return the
1618 * byte count but 0 instead
1619 *
1620 * Returns the number of byte written or -1 in case of error
1621 */
1622long
1623xmlSaveDoc(xmlSaveCtxtPtr ctxt, xmlDocPtr doc)
1624{
1625 long ret = 0;
1626
1627 if ((ctxt == NULL) || (doc == NULL)) return(-1);
1628 if (xmlDocContentDumpOutput(ctxt, doc) < 0)
1629 return(-1);
1630 return(ret);
1631}
1632
1633/**
1634 * xmlSaveTree:
1635 * @ctxt: a document saving context
1636 * @node: the top node of the subtree to save
1637 *
1638 * Save a subtree starting at the node parameter to a saving context
1639 * TODO: The function is not fully implemented yet as it does not return the
1640 * byte count but 0 instead
1641 *
1642 * Returns the number of byte written or -1 in case of error
1643 */
1644long
1645xmlSaveTree(xmlSaveCtxtPtr ctxt, xmlNodePtr node)
1646{
1647 long ret = 0;
1648
1649 if ((ctxt == NULL) || (node == NULL)) return(-1);
1650 xmlNodeDumpOutputInternal(ctxt, node);
1651 return(ret);
1652}
1653
1654/**
1655 * xmlSaveFlush:
1656 * @ctxt: a document saving context
1657 *
1658 * Flush a document saving context, i.e. make sure that all bytes have
1659 * been output.
1660 *
1661 * Returns the number of byte written or -1 in case of error.
1662 */
1663int
1664xmlSaveFlush(xmlSaveCtxtPtr ctxt)
1665{
1666 if (ctxt == NULL) return(-1);
1667 if (ctxt->buf == NULL) return(-1);
1668 return(xmlOutputBufferFlush(ctxt->buf));
1669}
1670
1671/**
1672 * xmlSaveClose:
1673 * @ctxt: a document saving context
1674 *
1675 * Close a document saving context, i.e. make sure that all bytes have
1676 * been output and free the associated data.
1677 *
1678 * Returns the number of byte written or -1 in case of error.
1679 */
1680int
1681xmlSaveClose(xmlSaveCtxtPtr ctxt)
1682{
1683 int ret;
1684
1685 if (ctxt == NULL) return(-1);
1686 ret = xmlSaveFlush(ctxt);
1687 xmlFreeSaveCtxt(ctxt);
1688 return(ret);
1689}
1690
1691/**
1692 * xmlSaveSetEscape:
1693 * @ctxt: a document saving context
1694 * @escape: the escaping function
1695 *
1696 * Set a custom escaping function to be used for text in element content
1697 *
1698 * Returns 0 if successful or -1 in case of error.
1699 */
1700int
1701xmlSaveSetEscape(xmlSaveCtxtPtr ctxt, xmlCharEncodingOutputFunc escape)
1702{
1703 if (ctxt == NULL) return(-1);
1704 ctxt->escape = escape;
1705 return(0);
1706}
1707
1708/**
1709 * xmlSaveSetAttrEscape:
1710 * @ctxt: a document saving context
1711 * @escape: the escaping function
1712 *
1713 * Set a custom escaping function to be used for text in attribute content
1714 *
1715 * Returns 0 if successful or -1 in case of error.
1716 */
1717int
1718xmlSaveSetAttrEscape(xmlSaveCtxtPtr ctxt, xmlCharEncodingOutputFunc escape)
1719{
1720 if (ctxt == NULL) return(-1);
1721 ctxt->escapeAttr = escape;
1722 return(0);
1723}
1724
1725/************************************************************************
1726 * *
1727 * Public entry points based on buffers *
1728 * *
1729 ************************************************************************/
1730/**
1731 * xmlAttrSerializeTxtContent:
1732 * @buf: the XML buffer output
1733 * @doc: the document
1734 * @attr: the attribute node
1735 * @string: the text content
1736 *
1737 * Serialize text attribute values to an xml simple buffer
1738 */
1739void
1740xmlAttrSerializeTxtContent(xmlBufferPtr buf, xmlDocPtr doc,
1741 xmlAttrPtr attr, const xmlChar * string)
1742{
1743 xmlChar *base, *cur;
1744
1745 if (string == NULL)
1746 return;
1747 base = cur = (xmlChar *) string;
1748 while (*cur != 0) {
1749 if (*cur == '\n') {
1750 if (base != cur)
1751 xmlBufferAdd(buf, base, cur - base);
1752 xmlBufferAdd(buf, BAD_CAST "&#10;", 5);
1753 cur++;
1754 base = cur;
1755 } else if (*cur == '\r') {
1756 if (base != cur)
1757 xmlBufferAdd(buf, base, cur - base);
1758 xmlBufferAdd(buf, BAD_CAST "&#13;", 5);
1759 cur++;
1760 base = cur;
1761 } else if (*cur == '\t') {
1762 if (base != cur)
1763 xmlBufferAdd(buf, base, cur - base);
1764 xmlBufferAdd(buf, BAD_CAST "&#9;", 4);
1765 cur++;
1766 base = cur;
1767 } else if (*cur == '"') {
1768 if (base != cur)
1769 xmlBufferAdd(buf, base, cur - base);
1770 xmlBufferAdd(buf, BAD_CAST "&quot;", 6);
1771 cur++;
1772 base = cur;
1773 } else if (*cur == '<') {
1774 if (base != cur)
1775 xmlBufferAdd(buf, base, cur - base);
1776 xmlBufferAdd(buf, BAD_CAST "&lt;", 4);
1777 cur++;
1778 base = cur;
1779 } else if (*cur == '>') {
1780 if (base != cur)
1781 xmlBufferAdd(buf, base, cur - base);
1782 xmlBufferAdd(buf, BAD_CAST "&gt;", 4);
1783 cur++;
1784 base = cur;
1785 } else if (*cur == '&') {
1786 if (base != cur)
1787 xmlBufferAdd(buf, base, cur - base);
1788 xmlBufferAdd(buf, BAD_CAST "&amp;", 5);
1789 cur++;
1790 base = cur;
1791 } else if ((*cur >= 0x80) && ((doc == NULL) ||
1792 (doc->encoding == NULL))) {
1793 /*
1794 * We assume we have UTF-8 content.
1795 */
1796 unsigned char tmp[10];
1797 int val = 0, l = 1;
1798
1799 if (base != cur)
1800 xmlBufferAdd(buf, base, cur - base);
1801 if (*cur < 0xC0) {
1802 xmlSaveErr(XML_SAVE_NOT_UTF8, (xmlNodePtr) attr, NULL);
1803 if (doc != NULL)
1804 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
1805 xmlSerializeHexCharRef(tmp, *cur);
1806 xmlBufferAdd(buf, (xmlChar *) tmp, -1);
1807 cur++;
1808 base = cur;
1809 continue;
1810 } else if (*cur < 0xE0) {
1811 val = (cur[0]) & 0x1F;
1812 val <<= 6;
1813 val |= (cur[1]) & 0x3F;
1814 l = 2;
1815 } else if (*cur < 0xF0) {
1816 val = (cur[0]) & 0x0F;
1817 val <<= 6;
1818 val |= (cur[1]) & 0x3F;
1819 val <<= 6;
1820 val |= (cur[2]) & 0x3F;
1821 l = 3;
1822 } else if (*cur < 0xF8) {
1823 val = (cur[0]) & 0x07;
1824 val <<= 6;
1825 val |= (cur[1]) & 0x3F;
1826 val <<= 6;
1827 val |= (cur[2]) & 0x3F;
1828 val <<= 6;
1829 val |= (cur[3]) & 0x3F;
1830 l = 4;
1831 }
1832 if ((l == 1) || (!IS_CHAR(val))) {
1833 xmlSaveErr(XML_SAVE_CHAR_INVALID, (xmlNodePtr) attr, NULL);
1834 if (doc != NULL)
1835 doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
1836
1837 xmlSerializeHexCharRef(tmp, *cur);
1838 xmlBufferAdd(buf, (xmlChar *) tmp, -1);
1839 cur++;
1840 base = cur;
1841 continue;
1842 }
1843 /*
1844 * We could do multiple things here. Just save
1845 * as a char ref
1846 */
1847 xmlSerializeHexCharRef(tmp, val);
1848 xmlBufferAdd(buf, (xmlChar *) tmp, -1);
1849 cur += l;
1850 base = cur;
1851 } else {
1852 cur++;
1853 }
1854 }
1855 if (base != cur)
1856 xmlBufferAdd(buf, base, cur - base);
1857}
1858
1859/**
1860 * xmlNodeDump:
1861 * @buf: the XML buffer output
1862 * @doc: the document
1863 * @cur: the current node
1864 * @level: the imbrication level for indenting
1865 * @format: is formatting allowed
1866 *
1867 * Dump an XML node, recursive behaviour,children are printed too.
1868 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
1869 * or xmlKeepBlanksDefault(0) was called
1870 *
1871 * Returns the number of bytes written to the buffer or -1 in case of error
1872 */
1873int
1874xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
1875 int format)
1876{
1877 unsigned int use;
1878 int ret;
1879 xmlOutputBufferPtr outbuf;
1880
1881 xmlInitParser();
1882
1883 if (cur == NULL) {
1884#ifdef DEBUG_TREE
1885 xmlGenericError(xmlGenericErrorContext,
1886 "xmlNodeDump : node == NULL\n");
1887#endif
1888 return (-1);
1889 }
1890 if (buf == NULL) {
1891#ifdef DEBUG_TREE
1892 xmlGenericError(xmlGenericErrorContext,
1893 "xmlNodeDump : buf == NULL\n");
1894#endif
1895 return (-1);
1896 }
1897 outbuf = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer));
1898 if (outbuf == NULL) {
1899 xmlSaveErrMemory("creating buffer");
1900 return (-1);
1901 }
1902 memset(outbuf, 0, (size_t) sizeof(xmlOutputBuffer));
1903 outbuf->buffer = buf;
1904 outbuf->encoder = NULL;
1905 outbuf->writecallback = NULL;
1906 outbuf->closecallback = NULL;
1907 outbuf->context = NULL;
1908 outbuf->written = 0;
1909
1910 use = buf->use;
1911 xmlNodeDumpOutput(outbuf, doc, cur, level, format, NULL);
1912 xmlFree(outbuf);
1913 ret = buf->use - use;
1914 return (ret);
1915}
1916
1917/**
1918 * xmlElemDump:
1919 * @f: the FILE * for the output
1920 * @doc: the document
1921 * @cur: the current node
1922 *
1923 * Dump an XML/HTML node, recursive behaviour, children are printed too.
1924 */
1925void
1926xmlElemDump(FILE * f, xmlDocPtr doc, xmlNodePtr cur)
1927{
1928 xmlOutputBufferPtr outbuf;
1929
1930 xmlInitParser();
1931
1932 if (cur == NULL) {
1933#ifdef DEBUG_TREE
1934 xmlGenericError(xmlGenericErrorContext,
1935 "xmlElemDump : cur == NULL\n");
1936#endif
1937 return;
1938 }
1939#ifdef DEBUG_TREE
1940 if (doc == NULL) {
1941 xmlGenericError(xmlGenericErrorContext,
1942 "xmlElemDump : doc == NULL\n");
1943 }
1944#endif
1945
1946 outbuf = xmlOutputBufferCreateFile(f, NULL);
1947 if (outbuf == NULL)
1948 return;
1949 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
1950#ifdef LIBXML_HTML_ENABLED
1951 htmlNodeDumpOutput(outbuf, doc, cur, NULL);
1952#else
1953 xmlSaveErr(XML_ERR_INTERNAL_ERROR, cur, "HTML support not compiled in\n");
1954#endif /* LIBXML_HTML_ENABLED */
1955 } else
1956 xmlNodeDumpOutput(outbuf, doc, cur, 0, 1, NULL);
1957 xmlOutputBufferClose(outbuf);
1958}
1959
1960/************************************************************************
1961 * *
1962 * Saving functions front-ends *
1963 * *
1964 ************************************************************************/
1965
1966/**
1967 * xmlNodeDumpOutput:
1968 * @buf: the XML buffer output
1969 * @doc: the document
1970 * @cur: the current node
1971 * @level: the imbrication level for indenting
1972 * @format: is formatting allowed
1973 * @encoding: an optional encoding string
1974 *
1975 * Dump an XML node, recursive behaviour, children are printed too.
1976 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
1977 * or xmlKeepBlanksDefault(0) was called
1978 */
1979void
1980xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
1981 int level, int format, const char *encoding)
1982{
1983 xmlSaveCtxt ctxt;
1984#ifdef LIBXML_HTML_ENABLED
1985 xmlDtdPtr dtd;
1986 int is_xhtml = 0;
1987#endif
1988
1989 xmlInitParser();
1990
1991 if ((buf == NULL) || (cur == NULL)) return;
1992
1993 if (encoding == NULL)
1994 encoding = "UTF-8";
1995
1996 memset(&ctxt, 0, sizeof(ctxt));
1997 ctxt.doc = doc;
1998 ctxt.buf = buf;
1999 ctxt.level = level;
2000 ctxt.format = format;
2001 ctxt.encoding = (const xmlChar *) encoding;
2002 xmlSaveCtxtInit(&ctxt);
2003
2004#ifdef LIBXML_HTML_ENABLED
2005 dtd = xmlGetIntSubset(doc);
2006 if (dtd != NULL) {
2007 is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
2008 if (is_xhtml < 0)
2009 is_xhtml = 0;
2010 }
2011
2012 if (is_xhtml)
2013 xhtmlNodeDumpOutput(&ctxt, cur);
2014 else
2015#endif
2016 xmlNodeDumpOutputInternal(&ctxt, cur);
2017}
2018
2019/**
2020 * xmlDocDumpFormatMemoryEnc:
2021 * @out_doc: Document to generate XML text from
2022 * @doc_txt_ptr: Memory pointer for allocated XML text
2023 * @doc_txt_len: Length of the generated XML text
2024 * @txt_encoding: Character encoding to use when generating XML text
2025 * @format: should formatting spaces been added
2026 *
2027 * Dump the current DOM tree into memory using the character encoding specified
2028 * by the caller. Note it is up to the caller of this function to free the
2029 * allocated memory with xmlFree().
2030 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
2031 * or xmlKeepBlanksDefault(0) was called
2032 */
2033
2034void
2035xmlDocDumpFormatMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
2036 int * doc_txt_len, const char * txt_encoding,
2037 int format) {
2038 xmlSaveCtxt ctxt;
2039 int dummy = 0;
2040 xmlOutputBufferPtr out_buff = NULL;
2041 xmlCharEncodingHandlerPtr conv_hdlr = NULL;
2042
2043 if (doc_txt_len == NULL) {
2044 doc_txt_len = &dummy; /* Continue, caller just won't get length */
2045 }
2046
2047 if (doc_txt_ptr == NULL) {
2048 *doc_txt_len = 0;
2049 return;
2050 }
2051
2052 *doc_txt_ptr = NULL;
2053 *doc_txt_len = 0;
2054
2055 if (out_doc == NULL) {
2056 /* No document, no output */
2057 return;
2058 }
2059
2060 /*
2061 * Validate the encoding value, if provided.
2062 * This logic is copied from xmlSaveFileEnc.
2063 */
2064
2065 if (txt_encoding == NULL)
2066 txt_encoding = (const char *) out_doc->encoding;
2067 if (txt_encoding != NULL) {
2068 conv_hdlr = xmlFindCharEncodingHandler(txt_encoding);
2069 if ( conv_hdlr == NULL ) {
2070 xmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, (xmlNodePtr) out_doc,
2071 txt_encoding);
2072 return;
2073 }
2074 }
2075
2076 if ((out_buff = xmlAllocOutputBuffer(conv_hdlr)) == NULL ) {
2077 xmlSaveErrMemory("creating buffer");
2078 return;
2079 }
2080
2081 memset(&ctxt, 0, sizeof(ctxt));
2082 ctxt.doc = out_doc;
2083 ctxt.buf = out_buff;
2084 ctxt.level = 0;
2085 ctxt.format = format;
2086 ctxt.encoding = (const xmlChar *) txt_encoding;
2087 xmlSaveCtxtInit(&ctxt);
2088 xmlDocContentDumpOutput(&ctxt, out_doc);
2089 xmlOutputBufferFlush(out_buff);
2090 if (out_buff->conv != NULL) {
2091 *doc_txt_len = out_buff->conv->use;
2092 *doc_txt_ptr = xmlStrndup(out_buff->conv->content, *doc_txt_len);
2093 } else {
2094 *doc_txt_len = out_buff->buffer->use;
2095 *doc_txt_ptr = xmlStrndup(out_buff->buffer->content, *doc_txt_len);
2096 }
2097 (void)xmlOutputBufferClose(out_buff);
2098
2099 if ((*doc_txt_ptr == NULL) && (*doc_txt_len > 0)) {
2100 *doc_txt_len = 0;
2101 xmlSaveErrMemory("creating output");
2102 }
2103
2104 return;
2105}
2106
2107/**
2108 * xmlDocDumpMemory:
2109 * @cur: the document
2110 * @mem: OUT: the memory pointer
2111 * @size: OUT: the memory length
2112 *
2113 * Dump an XML document in memory and return the #xmlChar * and it's size
2114 * in bytes. It's up to the caller to free the memory with xmlFree().
2115 * The resulting byte array is zero terminated, though the last 0 is not
2116 * included in the returned size.
2117 */
2118void
2119xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) {
2120 xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, 0);
2121}
2122
2123/**
2124 * xmlDocDumpFormatMemory:
2125 * @cur: the document
2126 * @mem: OUT: the memory pointer
2127 * @size: OUT: the memory length
2128 * @format: should formatting spaces been added
2129 *
2130 *
2131 * Dump an XML document in memory and return the #xmlChar * and it's size.
2132 * It's up to the caller to free the memory with xmlFree().
2133 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
2134 * or xmlKeepBlanksDefault(0) was called
2135 */
2136void
2137xmlDocDumpFormatMemory(xmlDocPtr cur, xmlChar**mem, int *size, int format) {
2138 xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, format);
2139}
2140
2141/**
2142 * xmlDocDumpMemoryEnc:
2143 * @out_doc: Document to generate XML text from
2144 * @doc_txt_ptr: Memory pointer for allocated XML text
2145 * @doc_txt_len: Length of the generated XML text
2146 * @txt_encoding: Character encoding to use when generating XML text
2147 *
2148 * Dump the current DOM tree into memory using the character encoding specified
2149 * by the caller. Note it is up to the caller of this function to free the
2150 * allocated memory with xmlFree().
2151 */
2152
2153void
2154xmlDocDumpMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
2155 int * doc_txt_len, const char * txt_encoding) {
2156 xmlDocDumpFormatMemoryEnc(out_doc, doc_txt_ptr, doc_txt_len,
2157 txt_encoding, 0);
2158}
2159
2160/**
2161 * xmlDocFormatDump:
2162 * @f: the FILE*
2163 * @cur: the document
2164 * @format: should formatting spaces been added
2165 *
2166 * Dump an XML document to an open FILE.
2167 *
2168 * returns: the number of bytes written or -1 in case of failure.
2169 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
2170 * or xmlKeepBlanksDefault(0) was called
2171 */
2172int
2173xmlDocFormatDump(FILE *f, xmlDocPtr cur, int format) {
2174 xmlSaveCtxt ctxt;
2175 xmlOutputBufferPtr buf;
2176 const char * encoding;
2177 xmlCharEncodingHandlerPtr handler = NULL;
2178 int ret;
2179
2180 if (cur == NULL) {
2181#ifdef DEBUG_TREE
2182 xmlGenericError(xmlGenericErrorContext,
2183 "xmlDocDump : document == NULL\n");
2184#endif
2185 return(-1);
2186 }
2187 encoding = (const char *) cur->encoding;
2188
2189 if (encoding != NULL) {
2190 handler = xmlFindCharEncodingHandler(encoding);
2191 if (handler == NULL) {
2192 xmlFree((char *) cur->encoding);
2193 cur->encoding = NULL;
2194 encoding = NULL;
2195 }
2196 }
2197 buf = xmlOutputBufferCreateFile(f, handler);
2198 if (buf == NULL) return(-1);
2199 memset(&ctxt, 0, sizeof(ctxt));
2200 ctxt.doc = cur;
2201 ctxt.buf = buf;
2202 ctxt.level = 0;
2203 ctxt.format = format;
2204 ctxt.encoding = (const xmlChar *) encoding;
2205 xmlSaveCtxtInit(&ctxt);
2206 xmlDocContentDumpOutput(&ctxt, cur);
2207
2208 ret = xmlOutputBufferClose(buf);
2209 return(ret);
2210}
2211
2212/**
2213 * xmlDocDump:
2214 * @f: the FILE*
2215 * @cur: the document
2216 *
2217 * Dump an XML document to an open FILE.
2218 *
2219 * returns: the number of bytes written or -1 in case of failure.
2220 */
2221int
2222xmlDocDump(FILE *f, xmlDocPtr cur) {
2223 return(xmlDocFormatDump (f, cur, 0));
2224}
2225
2226/**
2227 * xmlSaveFileTo:
2228 * @buf: an output I/O buffer
2229 * @cur: the document
2230 * @encoding: the encoding if any assuming the I/O layer handles the trancoding
2231 *
2232 * Dump an XML document to an I/O buffer.
2233 * Warning ! This call xmlOutputBufferClose() on buf which is not available
2234 * after this call.
2235 *
2236 * returns: the number of bytes written or -1 in case of failure.
2237 */
2238int
2239xmlSaveFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding) {
2240 xmlSaveCtxt ctxt;
2241 int ret;
2242
2243 if (buf == NULL) return(-1);
2244 if (cur == NULL) {
2245 xmlOutputBufferClose(buf);
2246 return(-1);
2247 }
2248 memset(&ctxt, 0, sizeof(ctxt));
2249 ctxt.doc = cur;
2250 ctxt.buf = buf;
2251 ctxt.level = 0;
2252 ctxt.format = 0;
2253 ctxt.encoding = (const xmlChar *) encoding;
2254 xmlSaveCtxtInit(&ctxt);
2255 xmlDocContentDumpOutput(&ctxt, cur);
2256 ret = xmlOutputBufferClose(buf);
2257 return(ret);
2258}
2259
2260/**
2261 * xmlSaveFormatFileTo:
2262 * @buf: an output I/O buffer
2263 * @cur: the document
2264 * @encoding: the encoding if any assuming the I/O layer handles the trancoding
2265 * @format: should formatting spaces been added
2266 *
2267 * Dump an XML document to an I/O buffer.
2268 * Warning ! This call xmlOutputBufferClose() on buf which is not available
2269 * after this call.
2270 *
2271 * returns: the number of bytes written or -1 in case of failure.
2272 */
2273int
2274xmlSaveFormatFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur,
2275 const char *encoding, int format)
2276{
2277 xmlSaveCtxt ctxt;
2278 int ret;
2279
2280 if (buf == NULL) return(-1);
2281 if ((cur == NULL) ||
2282 ((cur->type != XML_DOCUMENT_NODE) &&
2283 (cur->type != XML_HTML_DOCUMENT_NODE))) {
2284 xmlOutputBufferClose(buf);
2285 return(-1);
2286 }
2287 memset(&ctxt, 0, sizeof(ctxt));
2288 ctxt.doc = cur;
2289 ctxt.buf = buf;
2290 ctxt.level = 0;
2291 ctxt.format = format;
2292 ctxt.encoding = (const xmlChar *) encoding;
2293 xmlSaveCtxtInit(&ctxt);
2294 xmlDocContentDumpOutput(&ctxt, cur);
2295 ret = xmlOutputBufferClose(buf);
2296 return (ret);
2297}
2298
2299/**
2300 * xmlSaveFormatFileEnc:
2301 * @filename: the filename or URL to output
2302 * @cur: the document being saved
2303 * @encoding: the name of the encoding to use or NULL.
2304 * @format: should formatting spaces be added.
2305 *
2306 * Dump an XML document to a file or an URL.
2307 *
2308 * Returns the number of bytes written or -1 in case of error.
2309 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
2310 * or xmlKeepBlanksDefault(0) was called
2311 */
2312int
2313xmlSaveFormatFileEnc( const char * filename, xmlDocPtr cur,
2314 const char * encoding, int format ) {
2315 xmlSaveCtxt ctxt;
2316 xmlOutputBufferPtr buf;
2317 xmlCharEncodingHandlerPtr handler = NULL;
2318 int ret;
2319
2320 if (cur == NULL)
2321 return(-1);
2322
2323 if (encoding == NULL)
2324 encoding = (const char *) cur->encoding;
2325
2326 if (encoding != NULL) {
2327
2328 handler = xmlFindCharEncodingHandler(encoding);
2329 if (handler == NULL)
2330 return(-1);
2331 }
2332
2333#ifdef HAVE_ZLIB_H
2334 if (cur->compression < 0) cur->compression = xmlGetCompressMode();
2335#endif
2336 /*
2337 * save the content to a temp buffer.
2338 */
2339 buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression);
2340 if (buf == NULL) return(-1);
2341 memset(&ctxt, 0, sizeof(ctxt));
2342 ctxt.doc = cur;
2343 ctxt.buf = buf;
2344 ctxt.level = 0;
2345 ctxt.format = format;
2346 ctxt.encoding = (const xmlChar *) encoding;
2347 xmlSaveCtxtInit(&ctxt);
2348
2349 xmlDocContentDumpOutput(&ctxt, cur);
2350
2351 ret = xmlOutputBufferClose(buf);
2352 return(ret);
2353}
2354
2355
2356/**
2357 * xmlSaveFileEnc:
2358 * @filename: the filename (or URL)
2359 * @cur: the document
2360 * @encoding: the name of an encoding (or NULL)
2361 *
2362 * Dump an XML document, converting it to the given encoding
2363 *
2364 * returns: the number of bytes written or -1 in case of failure.
2365 */
2366int
2367xmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) {
2368 return ( xmlSaveFormatFileEnc( filename, cur, encoding, 0 ) );
2369}
2370
2371/**
2372 * xmlSaveFormatFile:
2373 * @filename: the filename (or URL)
2374 * @cur: the document
2375 * @format: should formatting spaces been added
2376 *
2377 * Dump an XML document to a file. Will use compression if
2378 * compiled in and enabled. If @filename is "-" the stdout file is
2379 * used. If @format is set then the document will be indented on output.
2380 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
2381 * or xmlKeepBlanksDefault(0) was called
2382 *
2383 * returns: the number of bytes written or -1 in case of failure.
2384 */
2385int
2386xmlSaveFormatFile(const char *filename, xmlDocPtr cur, int format) {
2387 return ( xmlSaveFormatFileEnc( filename, cur, NULL, format ) );
2388}
2389
2390/**
2391 * xmlSaveFile:
2392 * @filename: the filename (or URL)
2393 * @cur: the document
2394 *
2395 * Dump an XML document to a file. Will use compression if
2396 * compiled in and enabled. If @filename is "-" the stdout file is
2397 * used.
2398 * returns: the number of bytes written or -1 in case of failure.
2399 */
2400int
2401xmlSaveFile(const char *filename, xmlDocPtr cur) {
2402 return(xmlSaveFormatFileEnc(filename, cur, NULL, 0));
2403}
2404
2405#endif /* LIBXML_OUTPUT_ENABLED */
2406
2407#define bottom_xmlsave
2408#include "elfgcchack.h"
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette