paulb@202 | 1 | #!/usr/bin/env python |
paulb@202 | 2 | |
paulb@202 | 3 | """ |
paulb@202 | 4 | XSL output classes and functions. |
paulb@202 | 5 | |
paulb@570 | 6 | Copyright (C) 2005, 2006, 2007 Paul Boddie <paul@boddie.org.uk> |
paulb@202 | 7 | |
paulb@600 | 8 | This program is free software; you can redistribute it and/or modify it under |
paulb@600 | 9 | the terms of the GNU Lesser General Public License as published by the Free |
paulb@600 | 10 | Software Foundation; either version 3 of the License, or (at your option) any |
paulb@600 | 11 | later version. |
paulb@202 | 12 | |
paulb@600 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paulb@600 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paulb@600 | 15 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
paulb@600 | 16 | details. |
paulb@202 | 17 | |
paulb@600 | 18 | You should have received a copy of the GNU Lesser General Public License along |
paulb@600 | 19 | with this program. If not, see <http://www.gnu.org/licenses/>. |
paulb@202 | 20 | """ |
paulb@202 | 21 | |
paulb@202 | 22 | # NOTE: Make this use other XSLT implementations, too. |
paulb@202 | 23 | |
paulb@539 | 24 | # Try the conventional import first. |
paulb@539 | 25 | |
paulb@539 | 26 | try: |
paulb@539 | 27 | import libxsltmod |
paulb@539 | 28 | except ImportError: |
paulb@539 | 29 | from libxmlmods import libxsltmod |
paulb@539 | 30 | |
paulb@202 | 31 | import libxml2dom |
paulb@243 | 32 | import os, tempfile |
paulb@202 | 33 | |
paulb@202 | 34 | class OutputError(Exception): |
paulb@202 | 35 | pass |
paulb@202 | 36 | |
paulb@202 | 37 | class Processor: |
paulb@202 | 38 | |
paulb@202 | 39 | """ |
paulb@202 | 40 | A handler which can prepare output for an XMLTools2 template. |
paulb@202 | 41 | """ |
paulb@202 | 42 | |
paulb@278 | 43 | def __init__(self, filenames, references=None, parameters=None, expressions=None): |
paulb@202 | 44 | |
paulb@202 | 45 | """ |
paulb@202 | 46 | Initialise the handler with the 'filenames' of stylesheets producing the |
paulb@202 | 47 | final output, a 'references' dictionary indicating related stylesheets. |
paulb@278 | 48 | Additional 'parameters' may be specified as a dictionary whose values |
paulb@278 | 49 | are converted to string values; additional 'expressions' may also be |
paulb@278 | 50 | specified as a dictionary - such expressions are actually evaluated in |
paulb@278 | 51 | stylesheet. |
paulb@202 | 52 | """ |
paulb@202 | 53 | |
paulb@202 | 54 | self.references = references or {} |
paulb@202 | 55 | self.parameters = parameters or {} |
paulb@278 | 56 | self.expressions = expressions or {} |
paulb@202 | 57 | |
paulb@202 | 58 | # Remember the stylesheet documents. |
paulb@202 | 59 | |
paulb@202 | 60 | self.stylesheets = [] |
paulb@202 | 61 | for filename in filenames: |
paulb@243 | 62 | #doc = libxml2dom.macrolib.parseFile(filename) |
paulb@243 | 63 | #self.stylesheets.append(libxsltmod.xsltParseStylesheetDoc(doc)) |
paulb@243 | 64 | self.stylesheets.append(libxsltmod.xsltParseStylesheetFile(filename)) |
paulb@202 | 65 | |
paulb@202 | 66 | def __del__(self): |
paulb@202 | 67 | |
paulb@202 | 68 | """ |
paulb@202 | 69 | Tidy up the stylesheet documents. |
paulb@202 | 70 | """ |
paulb@202 | 71 | |
paulb@202 | 72 | for stylesheet in self.stylesheets: |
paulb@202 | 73 | libxsltmod.xsltFreeStylesheet(stylesheet) |
paulb@202 | 74 | |
paulb@202 | 75 | def send_output(self, stream, encoding, document): |
paulb@202 | 76 | |
paulb@202 | 77 | """ |
paulb@202 | 78 | Send output to the given 'stream' using the given output encoding for |
paulb@202 | 79 | the given 'document'. |
paulb@202 | 80 | """ |
paulb@202 | 81 | |
paulb@243 | 82 | # Where an encoding is specified, get an libxml2dom document and |
paulb@243 | 83 | # serialise it. |
paulb@243 | 84 | # NOTE: This is not satisfactory where indented output is desired. |
paulb@243 | 85 | |
paulb@243 | 86 | if encoding: |
paulb@243 | 87 | result = self.get_result(document) |
paulb@243 | 88 | stream.write(result.toString(encoding)) |
paulb@243 | 89 | return |
paulb@243 | 90 | |
paulb@243 | 91 | # Otherwise, get a libxml2mod document and use the libxsltmod API. |
paulb@202 | 92 | |
paulb@243 | 93 | result = self._get_result(document) |
paulb@243 | 94 | if result is None: |
paulb@243 | 95 | raise OutputError, "Transformation failed." |
paulb@243 | 96 | |
paulb@243 | 97 | # Attempt to get the correctly formatted document. |
paulb@243 | 98 | |
paulb@243 | 99 | if hasattr(stream, "fileno"): |
paulb@278 | 100 | stream.flush() |
paulb@243 | 101 | fd = stream.fileno() |
paulb@243 | 102 | str_result = libxsltmod.xsltSaveResultToFd(fd, result, self.stylesheets[-1]) |
paulb@202 | 103 | else: |
paulb@243 | 104 | fd, name = tempfile.mkstemp() |
paulb@243 | 105 | str_result = libxsltmod.xsltSaveResultToFd(fd, result, self.stylesheets[-1]) |
paulb@243 | 106 | f = os.fdopen(fd) |
paulb@243 | 107 | f.seek(0) |
paulb@243 | 108 | stream.write(f.read()) |
paulb@243 | 109 | f.close() |
paulb@243 | 110 | os.remove(name) |
paulb@202 | 111 | |
paulb@202 | 112 | def get_result(self, document): |
paulb@202 | 113 | |
paulb@202 | 114 | """ |
paulb@202 | 115 | Return a transformed document produced from the object's stylesheets and |
paulb@202 | 116 | the given 'document'. |
paulb@202 | 117 | """ |
paulb@202 | 118 | |
paulb@202 | 119 | result = self._get_result(document) |
paulb@202 | 120 | |
paulb@202 | 121 | if result is not None: |
paulb@570 | 122 | return libxml2dom.getDOMImplementation().adoptDocument(result) |
paulb@202 | 123 | else: |
paulb@202 | 124 | raise OutputError, "Transformation failed." |
paulb@202 | 125 | |
paulb@202 | 126 | def _get_result(self, document): |
paulb@202 | 127 | |
paulb@202 | 128 | """ |
paulb@202 | 129 | Return a transformation of the given 'document'. |
paulb@202 | 130 | """ |
paulb@202 | 131 | |
paulb@202 | 132 | if hasattr(document, "as_native_node"): |
paulb@202 | 133 | document = document.as_native_node() |
paulb@202 | 134 | |
paulb@202 | 135 | # Transform the localised instance into the final output. |
paulb@202 | 136 | |
paulb@202 | 137 | parameters = {} |
paulb@202 | 138 | for name, reference in self.references.items(): |
paulb@202 | 139 | parameters[name.encode("utf-8")] = ("document('%s')" % self._quote(reference)).encode("utf-8") |
paulb@202 | 140 | for name, parameter in self.parameters.items(): |
paulb@202 | 141 | parameters[name.encode("utf-8")] = ("'%s'" % self._quote(parameter)).encode("utf-8") |
paulb@278 | 142 | for name, parameter in self.expressions.items(): |
paulb@278 | 143 | parameters[name.encode("utf-8")] = self._quote(parameter).encode("utf-8") |
paulb@202 | 144 | |
paulb@202 | 145 | last_result = document |
paulb@202 | 146 | for stylesheet in self.stylesheets: |
paulb@202 | 147 | result = libxsltmod.xsltApplyStylesheet(stylesheet, last_result, parameters) |
paulb@202 | 148 | if last_result is not None: |
paulb@202 | 149 | last_result = result |
paulb@202 | 150 | else: |
paulb@202 | 151 | raise OutputError, "Transformation failed." |
paulb@202 | 152 | |
paulb@202 | 153 | return result |
paulb@202 | 154 | |
paulb@202 | 155 | def _quote(self, s): |
paulb@202 | 156 | |
paulb@202 | 157 | "Make the given parameter string 's' palatable for libxslt." |
paulb@202 | 158 | |
paulb@202 | 159 | return s.replace("'", "%27") |
paulb@202 | 160 | |
paulb@202 | 161 | # vim: tabstop=4 expandtab shiftwidth=4 |