1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2024-10-26 12:25:09 +03:00

started to switch xmllint to use xmlSaveDoc to test #342556 fixed #342556

* xmllint.c: started to switch xmllint to use xmlSaveDoc to test
  #342556
* xmlsave.c: fixed #342556 easy and a whole set of problems with
  encodings, BOM and xmlSaveDoc()
Daniel
This commit is contained in:
Daniel Veillard 2006-10-16 23:22:10 +00:00
parent 7e30356556
commit dab39b568a
3 changed files with 148 additions and 52 deletions

View File

@ -1,3 +1,10 @@
Tue Oct 17 01:21:37 CEST 2006 Daniel Veillard <daniel@veillard.com>
* xmllint.c: started to switch xmllint to use xmlSaveDoc to test
#342556
* xmlsave.c: fixed #342556 easy and a whole set of problems with
encodings, BOM and xmlSaveDoc()
Mon Oct 16 15:14:53 CEST 2006 Daniel Veillard <daniel@veillard.com>
* HTMLparser.c: fix #348252 if the document clains to be in a

101
xmllint.c
View File

@ -98,6 +98,9 @@
#ifdef LIBXML_C14N_ENABLED
#include <libxml/c14n.h>
#endif
#ifdef LIBXML_OUTPUT_ENABLED
#include <libxml/xmlsave.h>
#endif
#ifndef XML_XML_DEFAULT_CATALOG
#define XML_XML_DEFAULT_CATALOG "file:///etc/xml/catalog"
@ -133,6 +136,7 @@ static int nowrap = 0;
static int format = 0;
static const char *output = NULL;
static int compress = 0;
static int oldout = 0;
#endif /* LIBXML_OUTPUT_ENABLED */
#ifdef LIBXML_VALID_ENABLED
static int valid = 0;
@ -2464,48 +2468,71 @@ static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
write(1, result, len);
xmlFree(result);
}
} else
#endif /* HAVE_SYS_MMAN_H */
if (compress) {
xmlSaveFile(output ? output : "-", doc);
}
else if (encoding != NULL) {
if ( format ) {
ret = xmlSaveFormatFileEnc(output ? output : "-", doc,
encoding, 1);
}
else {
ret = xmlSaveFileEnc(output ? output : "-", doc, encoding);
}
if (ret < 0) {
fprintf(stderr, "failed save to %s\n",
output ? output : "-");
progresult = XMLLINT_ERR_OUT;
}
}
else if (format) {
ret = xmlSaveFormatFile(output ? output : "-", doc, 1);
if (ret < 0) {
fprintf(stderr, "failed save to %s\n",
output ? output : "-");
progresult = XMLLINT_ERR_OUT;
}
}
else {
FILE *out;
if (output == NULL)
out = stdout;
else {
out = fopen(output,"wb");
}
if (out != NULL) {
if (xmlDocDump(out, doc) < 0)
progresult = XMLLINT_ERR_OUT;
if (output != NULL)
fclose(out);
} else if (oldout) {
if (encoding != NULL) {
if ( format ) {
ret = xmlSaveFormatFileEnc(output ? output : "-", doc,
encoding, 1);
}
else {
ret = xmlSaveFileEnc(output ? output : "-", doc,
encoding);
}
if (ret < 0) {
fprintf(stderr, "failed save to %s\n",
output ? output : "-");
progresult = XMLLINT_ERR_OUT;
}
} else if (format) {
ret = xmlSaveFormatFile(output ? output : "-", doc, 1);
if (ret < 0) {
fprintf(stderr, "failed save to %s\n",
output ? output : "-");
progresult = XMLLINT_ERR_OUT;
}
} else {
FILE *out;
if (output == NULL)
out = stdout;
else {
out = fopen(output,"wb");
}
if (out != NULL) {
if (xmlDocDump(out, doc) < 0)
progresult = XMLLINT_ERR_OUT;
if (output != NULL)
fclose(out);
} else {
fprintf(stderr, "failed to open %s\n", output);
progresult = XMLLINT_ERR_OUT;
}
}
} else {
xmlSaveCtxtPtr ctxt;
int saveOpts = 0;
if (format)
saveOpts |= XML_SAVE_FORMAT;
if (output == NULL)
ctxt = xmlSaveToFd(1, encoding, saveOpts);
else
ctxt = xmlSaveToFilename(output, encoding, saveOpts);
if (ctxt != NULL) {
if (xmlSaveDoc(ctxt, doc) < 0) {
fprintf(stderr, "failed save to %s\n",
output ? output : "-");
progresult = XMLLINT_ERR_OUT;
}
xmlSaveClose(ctxt);
} else {
fprintf(stderr, "failed to open %s\n", output);
progresult = XMLLINT_ERR_OUT;
}
}

View File

@ -392,7 +392,7 @@ xmlNewSaveCtxt(const char *encoding, int options)
return(NULL);
}
ret->encoding = xmlStrdup((const xmlChar *)encoding);
ret->escape = xmlEscapeEntities;
ret->escape = NULL;
}
xmlSaveCtxtInit(ret);
@ -464,7 +464,7 @@ xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur);
static void xmlNodeListDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur);
static void xmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur);
void xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur);
static void xmlDocContentDumpOutput(xmlSaveCtxtPtr ctxt, xmlDocPtr cur);
static int xmlDocContentDumpOutput(xmlSaveCtxtPtr ctxt, xmlDocPtr cur);
/**
* xmlNsDumpOutput:
@ -820,35 +820,77 @@ xmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
*
* Dump an XML document.
*/
static void
static int
xmlDocContentDumpOutput(xmlSaveCtxtPtr ctxt, xmlDocPtr cur) {
#ifdef LIBXML_HTML_ENABLED
xmlDtdPtr dtd;
int is_xhtml = 0;
#endif
const xmlChar *oldenc = cur->encoding;
const xmlChar *oldctxtenc = ctxt->encoding;
const xmlChar *encoding = ctxt->encoding;
xmlOutputBufferPtr buf;
xmlCharEncodingOutputFunc oldescape = ctxt->escape;
xmlCharEncodingOutputFunc oldescapeAttr = ctxt->escapeAttr;
xmlOutputBufferPtr buf = ctxt->buf;
xmlCharEncodingHandlerPtr handler = NULL;
xmlCharEncoding enc;
xmlInitParser();
if (ctxt->encoding != NULL)
if (ctxt->encoding != NULL) {
cur->encoding = BAD_CAST ctxt->encoding;
} else if (cur->encoding != NULL) {
encoding = cur->encoding;
} else if (cur->charset != XML_CHAR_ENCODING_UTF8) {
encoding = (const xmlChar *)
xmlGetCharEncodingName((xmlCharEncoding) cur->charset);
}
buf = ctxt->buf;
enc = xmlParseCharEncoding((const char*) encoding);
if ((encoding != NULL) && (oldctxtenc == NULL) &&
(buf->encoder == NULL) && (buf->conv == NULL) &&
((ctxt->options & XML_SAVE_NO_DECL) == 0)) {
if ((enc != XML_CHAR_ENCODING_UTF8) &&
(enc != XML_CHAR_ENCODING_NONE) &&
(enc != XML_CHAR_ENCODING_ASCII)) {
/*
* we need to switch to this encoding but just for this document
* since we output the XMLDecl the conversion must be done to not
* generate not well formed documents.
*/
buf->encoder = xmlFindCharEncodingHandler((const char *)encoding);
if (buf->encoder == NULL) {
xmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL,
(const char *)encoding);
return(-1);
}
buf->conv = xmlBufferCreate();
if (buf->conv == NULL) {
xmlCharEncCloseFunc(buf->encoder);
xmlSaveErrMemory("creating encoding buffer");
return(-1);
}
/*
* initialize the state, e.g. if outputting a BOM
*/
xmlCharEncOutFunc(buf->encoder, buf->conv, NULL);
}
if (ctxt->escape == xmlEscapeEntities)
ctxt->escape = NULL;
if (ctxt->escapeAttr == xmlEscapeEntities)
ctxt->escapeAttr = NULL;
}
/*
* Save the XML declaration
*/
if ((ctxt->options & XML_SAVE_NO_DECL) == 0) {
xmlOutputBufferWrite(buf, 14, "<?xml version=");
if (cur->version != NULL)
xmlBufferWriteQuotedString(buf->buffer, cur->version);
else
xmlOutputBufferWrite(buf, 5, "\"1.0\"");
if (ctxt->encoding == NULL) {
if (cur->encoding != NULL)
encoding = cur->encoding;
else if (cur->charset != XML_CHAR_ENCODING_UTF8)
encoding = (const xmlChar *)
xmlGetCharEncodingName((xmlCharEncoding) cur->charset);
}
if (encoding != NULL) {
xmlOutputBufferWrite(buf, 10, " encoding=");
xmlBufferWriteQuotedString(buf->buffer, (xmlChar *) encoding);
@ -890,6 +932,25 @@ xmlDocContentDumpOutput(xmlSaveCtxtPtr ctxt, xmlDocPtr cur) {
}
if (ctxt->encoding != NULL)
cur->encoding = oldenc;
/*
* Restore the state of the saving context at the end of the document
*/
if ((encoding != NULL) && (oldctxtenc == NULL) &&
((ctxt->options & XML_SAVE_NO_DECL) == 0)) {
if ((enc != XML_CHAR_ENCODING_UTF8) &&
(enc != XML_CHAR_ENCODING_NONE) &&
(enc != XML_CHAR_ENCODING_ASCII)) {
xmlOutputBufferFlush(buf);
xmlCharEncCloseFunc(buf->encoder);
xmlBufferFree(buf->conv);
buf->encoder = NULL;
buf->conv = NULL;
}
ctxt->escape = oldescape;
ctxt->escapeAttr = oldescapeAttr;
}
return(0);
}
#ifdef LIBXML_HTML_ENABLED
@ -1320,7 +1381,7 @@ xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
/*
* This was removed due to problems with HTML processors.
* See bug #345147.
*
*/
/*
* 4.8. Script and Style elements
*/
@ -1563,7 +1624,8 @@ xmlSaveDoc(xmlSaveCtxtPtr ctxt, xmlDocPtr doc)
long ret = 0;
if ((ctxt == NULL) || (doc == NULL)) return(-1);
xmlDocContentDumpOutput(ctxt, doc);
if (xmlDocContentDumpOutput(ctxt, doc) < 0)
return(-1);
return(ret);
}