# HG changeset patch # User paulb # Date 1128215314 0 # Node ID 00428442ed0368e4a4ec2b369f5c2de2f7f9be66 # Parent 8d7927673e80016b3d5f4284a88ef24b4118472a [project @ 2005-10-02 01:08:34 by paulb] Removed unnecessary libxml2dom parsing of the stylesheets. Introduced libxsltmod-based serialisation to ensure that the output options are observed (for documents whose encoding is not set). diff -r 8d7927673e80 -r 00428442ed03 XSLTools/XSLOutput.py --- a/XSLTools/XSLOutput.py Sat Oct 01 16:28:00 2005 +0000 +++ b/XSLTools/XSLOutput.py Sun Oct 02 01:08:34 2005 +0000 @@ -24,6 +24,7 @@ import libxsltmod import libxml2dom +import os, tempfile class OutputError(Exception): pass @@ -49,8 +50,9 @@ self.stylesheets = [] for filename in filenames: - doc = libxml2dom.macrolib.parseFile(filename) - self.stylesheets.append(libxsltmod.xsltParseStylesheetDoc(doc)) + #doc = libxml2dom.macrolib.parseFile(filename) + #self.stylesheets.append(libxsltmod.xsltParseStylesheetDoc(doc)) + self.stylesheets.append(libxsltmod.xsltParseStylesheetFile(filename)) def __del__(self): @@ -68,12 +70,34 @@ the given 'document'. """ - result = self.get_result(document) + # Where an encoding is specified, get an libxml2dom document and + # serialise it. + # NOTE: This is not satisfactory where indented output is desired. + + if encoding: + result = self.get_result(document) + stream.write(result.toString(encoding)) + return + + # Otherwise, get a libxml2mod document and use the libxsltmod API. - if result is not None: - stream.write(result.toString(encoding)) + result = self._get_result(document) + if result is None: + raise OutputError, "Transformation failed." + + # Attempt to get the correctly formatted document. + + if hasattr(stream, "fileno"): + fd = stream.fileno() + str_result = libxsltmod.xsltSaveResultToFd(fd, result, self.stylesheets[-1]) else: - raise OutputError, "Transformation failed." + fd, name = tempfile.mkstemp() + str_result = libxsltmod.xsltSaveResultToFd(fd, result, self.stylesheets[-1]) + f = os.fdopen(fd) + f.seek(0) + stream.write(f.read()) + f.close() + os.remove(name) def get_result(self, document):