1.1 --- a/XSLTools/XSLOutput.py Sat Oct 01 16:28:00 2005 +0000
1.2 +++ b/XSLTools/XSLOutput.py Sun Oct 02 01:08:34 2005 +0000
1.3 @@ -24,6 +24,7 @@
1.4
1.5 import libxsltmod
1.6 import libxml2dom
1.7 +import os, tempfile
1.8
1.9 class OutputError(Exception):
1.10 pass
1.11 @@ -49,8 +50,9 @@
1.12
1.13 self.stylesheets = []
1.14 for filename in filenames:
1.15 - doc = libxml2dom.macrolib.parseFile(filename)
1.16 - self.stylesheets.append(libxsltmod.xsltParseStylesheetDoc(doc))
1.17 + #doc = libxml2dom.macrolib.parseFile(filename)
1.18 + #self.stylesheets.append(libxsltmod.xsltParseStylesheetDoc(doc))
1.19 + self.stylesheets.append(libxsltmod.xsltParseStylesheetFile(filename))
1.20
1.21 def __del__(self):
1.22
1.23 @@ -68,12 +70,34 @@
1.24 the given 'document'.
1.25 """
1.26
1.27 - result = self.get_result(document)
1.28 + # Where an encoding is specified, get an libxml2dom document and
1.29 + # serialise it.
1.30 + # NOTE: This is not satisfactory where indented output is desired.
1.31 +
1.32 + if encoding:
1.33 + result = self.get_result(document)
1.34 + stream.write(result.toString(encoding))
1.35 + return
1.36 +
1.37 + # Otherwise, get a libxml2mod document and use the libxsltmod API.
1.38
1.39 - if result is not None:
1.40 - stream.write(result.toString(encoding))
1.41 + result = self._get_result(document)
1.42 + if result is None:
1.43 + raise OutputError, "Transformation failed."
1.44 +
1.45 + # Attempt to get the correctly formatted document.
1.46 +
1.47 + if hasattr(stream, "fileno"):
1.48 + fd = stream.fileno()
1.49 + str_result = libxsltmod.xsltSaveResultToFd(fd, result, self.stylesheets[-1])
1.50 else:
1.51 - raise OutputError, "Transformation failed."
1.52 + fd, name = tempfile.mkstemp()
1.53 + str_result = libxsltmod.xsltSaveResultToFd(fd, result, self.stylesheets[-1])
1.54 + f = os.fdopen(fd)
1.55 + f.seek(0)
1.56 + stream.write(f.read())
1.57 + f.close()
1.58 + os.remove(name)
1.59
1.60 def get_result(self, document):
1.61