summaryrefslogtreecommitdiff
path: root/python/libxml.c
diff options
context:
space:
mode:
authorDaniel Veillard <veillard@src.gnome.org>2008-09-01 13:08:57 +0000
committerDaniel Veillard <veillard@src.gnome.org>2008-09-01 13:08:57 +0000
commitda3fee406da96c344d7b5f4e0631faee74f7e11d (patch)
treeeb4ae2773cbdbd1ead5b13eb997fc204b93303d6 /python/libxml.c
parent1572425c2774b3fef6af062b87351a0e42c3e172 (diff)
downloadlibxml2-da3fee406da96c344d7b5f4e0631faee74f7e11d.tar.gz
Borland C fix from Moritz Both regenerate, workaround a problem for buffer
* trionan.c: Borland C fix from Moritz Both * testapi.c: regenerate, workaround a problem for buffer testing * xmlIO.c HTMLtree.c: new internal entry point to hide even better xmlAllocOutputBufferInternal * tree.c: harden the code around buffer allocation schemes * parser.c: restore the warning when namespace names are not absolute URIs * runxmlconf.c: continue regression tests if we get the expected number of errors * Makefile.am: run the python tests on make check * xmlsave.c: handle the HTML documents and trees * python/libxml.c: convert python serialization to the xmlSave APIs and avoid some horrible hacks Daniel svn path=/trunk/; revision=3790
Diffstat (limited to 'python/libxml.c')
-rw-r--r--python/libxml.c147
1 files changed, 33 insertions, 114 deletions
diff --git a/python/libxml.c b/python/libxml.c
index 95b3cb92..3f2ede7b 100644
--- a/python/libxml.c
+++ b/python/libxml.c
@@ -24,6 +24,7 @@
#include <libxml/xmlIO.h>
#include <libxml/c14n.h>
#include <libxml/xmlreader.h>
+#include <libxml/xmlsave.h>
#include "libxml_wrap.h"
#include "libxml2-py.h"
@@ -2761,6 +2762,9 @@ libxml_serializeNode(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
const char *encoding;
int format;
int len;
+ xmlSaveCtxtPtr ctxt;
+ xmlBufferPtr buf;
+ int options = 0;
if (!PyArg_ParseTuple(args, (char *) "Ozi:serializeNode", &pyobj_node,
&encoding, &format))
@@ -2773,137 +2777,52 @@ libxml_serializeNode(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
}
if (node->type == XML_DOCUMENT_NODE) {
doc = (xmlDocPtr) node;
- xmlDocDumpFormatMemoryEnc(doc, &c_retval, &len,
- (const char *) encoding, format);
- py_retval = libxml_charPtrWrap((char *) c_retval);
+ node = NULL;
#ifdef LIBXML_HTML_ENABLED
} else if (node->type == XML_HTML_DOCUMENT_NODE) {
- xmlOutputBufferPtr buf;
- xmlCharEncodingHandlerPtr handler = NULL;
-
doc = (xmlDocPtr) node;
- if (encoding != NULL)
- htmlSetMetaEncoding(doc, (const xmlChar *) encoding);
- encoding = (const char *) htmlGetMetaEncoding(doc);
-
- if (encoding != NULL) {
- handler = xmlFindCharEncodingHandler(encoding);
- if (handler == NULL) {
- Py_INCREF(Py_None);
- return (Py_None);
- }
- }
-
- /*
- * Fallback to HTML or ASCII when the encoding is unspecified
- */
- if (handler == NULL)
- handler = xmlFindCharEncodingHandler("HTML");
- if (handler == NULL)
- handler = xmlFindCharEncodingHandler("ascii");
-
- buf = xmlAllocOutputBuffer(handler);
- if (buf == NULL) {
- Py_INCREF(Py_None);
- return (Py_None);
- }
- htmlDocContentDumpFormatOutput(buf, doc, encoding, format);
- xmlOutputBufferFlush(buf);
- if (buf->conv != NULL) {
- len = buf->conv->use;
- c_retval = buf->conv->content;
- buf->conv->content = NULL;
- } else {
- len = buf->buffer->use;
- c_retval = buf->buffer->content;
- buf->buffer->content = NULL;
- }
- (void) xmlOutputBufferClose(buf);
- py_retval = libxml_charPtrWrap((char *) c_retval);
-#endif /* LIBXML_HTML_ENABLED */
+ node = NULL;
+#endif
} else {
if (node->type == XML_NAMESPACE_DECL)
doc = NULL;
else
doc = node->doc;
if ((doc == NULL) || (doc->type == XML_DOCUMENT_NODE)) {
- xmlOutputBufferPtr buf;
- xmlCharEncodingHandlerPtr handler = NULL;
-
- if (encoding != NULL) {
- handler = xmlFindCharEncodingHandler(encoding);
- if (handler == NULL) {
- Py_INCREF(Py_None);
- return (Py_None);
- }
- }
-
- buf = xmlAllocOutputBuffer(handler);
- if (buf == NULL) {
- Py_INCREF(Py_None);
- return (Py_None);
- }
- xmlNodeDumpOutput(buf, doc, node, 0, format, encoding);
- xmlOutputBufferFlush(buf);
- if (buf->conv != NULL) {
- len = buf->conv->use;
- c_retval = buf->conv->content;
- buf->conv->content = NULL;
- } else {
- len = buf->buffer->use;
- c_retval = buf->buffer->content;
- buf->buffer->content = NULL;
- }
- (void) xmlOutputBufferClose(buf);
- py_retval = libxml_charPtrWrap((char *) c_retval);
#ifdef LIBXML_HTML_ENABLED
} else if (doc->type == XML_HTML_DOCUMENT_NODE) {
- xmlOutputBufferPtr buf;
- xmlCharEncodingHandlerPtr handler = NULL;
-
- if (encoding != NULL)
- htmlSetMetaEncoding(doc, (const xmlChar *) encoding);
- encoding = (const char *) htmlGetMetaEncoding(doc);
- if (encoding != NULL) {
- handler = xmlFindCharEncodingHandler(encoding);
- if (handler == NULL) {
- Py_INCREF(Py_None);
- return (Py_None);
- }
- }
-
- /*
- * Fallback to HTML or ASCII when the encoding is unspecified
- */
- if (handler == NULL)
- handler = xmlFindCharEncodingHandler("HTML");
- if (handler == NULL)
- handler = xmlFindCharEncodingHandler("ascii");
-
- buf = xmlAllocOutputBuffer(handler);
- if (buf == NULL) {
- Py_INCREF(Py_None);
- return (Py_None);
- }
- htmlNodeDumpFormatOutput(buf, doc, node, encoding, format);
- xmlOutputBufferFlush(buf);
- if (buf->conv != NULL) {
- len = buf->conv->use;
- c_retval = buf->conv->content;
- buf->conv->content = NULL;
- } else {
- len = buf->buffer->use;
- c_retval = buf->buffer->content;
- buf->buffer->content = NULL;
- }
- (void) xmlOutputBufferClose(buf);
- py_retval = libxml_charPtrWrap((char *) c_retval);
#endif /* LIBXML_HTML_ENABLED */
} else {
Py_INCREF(Py_None);
return (Py_None);
}
}
+
+
+ buf = xmlBufferCreate();
+ if (buf == NULL) {
+ Py_INCREF(Py_None);
+ return (Py_None);
+ }
+ if (format) options |= XML_SAVE_FORMAT;
+ ctxt = xmlSaveToBuffer(buf, encoding, options);
+ if (ctxt == NULL) {
+ xmlBufferFree(buf);
+ Py_INCREF(Py_None);
+ return (Py_None);
+ }
+ if (node == NULL)
+ xmlSaveDoc(ctxt, doc);
+ else
+ xmlSaveTree(ctxt, node);
+ xmlSaveClose(ctxt);
+
+ c_retval = buf->content;
+ buf->content = NULL;
+
+ xmlBufferFree(buf);
+ py_retval = libxml_charPtrWrap((char *) c_retval);
+
return (py_retval);
}