1.1 --- a/XSLOutput.py Thu Aug 18 15:56:06 2005 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,128 +0,0 @@
1.4 -#!/usr/bin/env python
1.5 -
1.6 -"""
1.7 -XSL output classes and functions.
1.8 -
1.9 -Copyright (C) 2005 Paul Boddie <paul@boddie.org.uk>
1.10 -
1.11 -This library is free software; you can redistribute it and/or
1.12 -modify it under the terms of the GNU Lesser General Public
1.13 -License as published by the Free Software Foundation; either
1.14 -version 2.1 of the License, or (at your option) any later version.
1.15 -
1.16 -This library is distributed in the hope that it will be useful,
1.17 -but WITHOUT ANY WARRANTY; without even the implied warranty of
1.18 -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1.19 -Lesser General Public License for more details.
1.20 -
1.21 -You should have received a copy of the GNU Lesser General Public
1.22 -License along with this library; if not, write to the Free Software
1.23 -Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1.24 -"""
1.25 -
1.26 -__version__ = "0.2"
1.27 -
1.28 -# NOTE: Make this use other implementations, too.
1.29 -
1.30 -import libxsltmod
1.31 -import libxml2dom
1.32 -
1.33 -class OutputError(Exception):
1.34 - pass
1.35 -
1.36 -class Processor:
1.37 -
1.38 - """
1.39 - A handler which can prepare output for an XMLTools2 template.
1.40 - """
1.41 -
1.42 - def __init__(self, filenames, references=None, parameters=None):
1.43 -
1.44 - """
1.45 - Initialise the handler with the 'filenames' of stylesheets producing the
1.46 - final output, a 'references' dictionary indicating related stylesheets.
1.47 - Additional 'parameters' may also be specified as a dictionary.
1.48 - """
1.49 -
1.50 - self.references = references or {}
1.51 - self.parameters = parameters or {}
1.52 -
1.53 - # Remember the stylesheet documents.
1.54 -
1.55 - self.stylesheets = []
1.56 - for filename in filenames:
1.57 - doc = libxml2dom.macrolib.parseFile(filename)
1.58 - self.stylesheets.append(libxsltmod.xsltParseStylesheetDoc(doc))
1.59 -
1.60 - def __del__(self):
1.61 -
1.62 - """
1.63 - Tidy up the stylesheet documents.
1.64 - """
1.65 -
1.66 - for stylesheet in self.stylesheets:
1.67 - libxsltmod.xsltFreeStylesheet(stylesheet)
1.68 -
1.69 - def send_output(self, stream, encoding, document):
1.70 -
1.71 - """
1.72 - Send output to the given 'stream' using the given output encoding for
1.73 - the given 'document'.
1.74 - """
1.75 -
1.76 - result = self.get_result(document)
1.77 -
1.78 - if result is not None:
1.79 - stream.write(result.toString(encoding))
1.80 - else:
1.81 - raise OutputError, "Transformation failed."
1.82 -
1.83 - def get_result(self, document):
1.84 -
1.85 - """
1.86 - Return a transformed document produced from the object's stylesheets and
1.87 - the given 'document'.
1.88 - """
1.89 -
1.90 - result = self._get_result(document)
1.91 -
1.92 - if result is not None:
1.93 - return libxml2dom.adoptNodes([result])[0]
1.94 - else:
1.95 - raise OutputError, "Transformation failed."
1.96 -
1.97 - def _get_result(self, document):
1.98 -
1.99 - """
1.100 - Return a transformation of the given 'document'.
1.101 - """
1.102 -
1.103 - if hasattr(document, "as_native_node"):
1.104 - document = document.as_native_node()
1.105 -
1.106 - # Transform the localised instance into the final output.
1.107 -
1.108 - parameters = {}
1.109 - for name, reference in self.references.items():
1.110 - parameters[name.encode("utf-8")] = ("document('%s')" % self._quote(reference)).encode("utf-8")
1.111 - for name, parameter in self.parameters.items():
1.112 - parameters[name.encode("utf-8")] = ("'%s'" % self._quote(parameter)).encode("utf-8")
1.113 - #print "**", repr(parameters)
1.114 -
1.115 - last_result = document
1.116 - for stylesheet in self.stylesheets:
1.117 - result = libxsltmod.xsltApplyStylesheet(stylesheet, last_result, parameters)
1.118 - if last_result is not None:
1.119 - last_result = result
1.120 - else:
1.121 - raise OutputError, "Transformation failed."
1.122 -
1.123 - return result
1.124 -
1.125 - def _quote(self, s):
1.126 -
1.127 - "Make the given parameter string 's' palatable for libxslt."
1.128 -
1.129 - return s.replace("'", "%27")
1.130 -
1.131 -# vim: tabstop=4 expandtab shiftwidth=4