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@202 | 6 | Copyright (C) 2005 Paul Boddie <paul@boddie.org.uk> |
paulb@202 | 7 | |
paulb@202 | 8 | This library is free software; you can redistribute it and/or |
paulb@202 | 9 | modify it under the terms of the GNU Lesser General Public |
paulb@202 | 10 | License as published by the Free Software Foundation; either |
paulb@202 | 11 | version 2.1 of the License, or (at your option) any later version. |
paulb@202 | 12 | |
paulb@202 | 13 | This library is distributed in the hope that it will be useful, |
paulb@202 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@202 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
paulb@202 | 16 | Lesser General Public License for more details. |
paulb@202 | 17 | |
paulb@202 | 18 | You should have received a copy of the GNU Lesser General Public |
paulb@202 | 19 | License along with this library; if not, write to the Free Software |
paulb@202 | 20 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
paulb@202 | 21 | """ |
paulb@202 | 22 | |
paulb@202 | 23 | # NOTE: Make this use other XSLT implementations, too. |
paulb@202 | 24 | |
paulb@202 | 25 | import libxsltmod |
paulb@202 | 26 | import libxml2dom |
paulb@202 | 27 | |
paulb@202 | 28 | class OutputError(Exception): |
paulb@202 | 29 | pass |
paulb@202 | 30 | |
paulb@202 | 31 | class Processor: |
paulb@202 | 32 | |
paulb@202 | 33 | """ |
paulb@202 | 34 | A handler which can prepare output for an XMLTools2 template. |
paulb@202 | 35 | """ |
paulb@202 | 36 | |
paulb@202 | 37 | def __init__(self, filenames, references=None, parameters=None): |
paulb@202 | 38 | |
paulb@202 | 39 | """ |
paulb@202 | 40 | Initialise the handler with the 'filenames' of stylesheets producing the |
paulb@202 | 41 | final output, a 'references' dictionary indicating related stylesheets. |
paulb@202 | 42 | Additional 'parameters' may also be specified as a dictionary. |
paulb@202 | 43 | """ |
paulb@202 | 44 | |
paulb@202 | 45 | self.references = references or {} |
paulb@202 | 46 | self.parameters = parameters or {} |
paulb@202 | 47 | |
paulb@202 | 48 | # Remember the stylesheet documents. |
paulb@202 | 49 | |
paulb@202 | 50 | self.stylesheets = [] |
paulb@202 | 51 | for filename in filenames: |
paulb@202 | 52 | doc = libxml2dom.macrolib.parseFile(filename) |
paulb@202 | 53 | self.stylesheets.append(libxsltmod.xsltParseStylesheetDoc(doc)) |
paulb@202 | 54 | |
paulb@202 | 55 | def __del__(self): |
paulb@202 | 56 | |
paulb@202 | 57 | """ |
paulb@202 | 58 | Tidy up the stylesheet documents. |
paulb@202 | 59 | """ |
paulb@202 | 60 | |
paulb@202 | 61 | for stylesheet in self.stylesheets: |
paulb@202 | 62 | libxsltmod.xsltFreeStylesheet(stylesheet) |
paulb@202 | 63 | |
paulb@202 | 64 | def send_output(self, stream, encoding, document): |
paulb@202 | 65 | |
paulb@202 | 66 | """ |
paulb@202 | 67 | Send output to the given 'stream' using the given output encoding for |
paulb@202 | 68 | the given 'document'. |
paulb@202 | 69 | """ |
paulb@202 | 70 | |
paulb@202 | 71 | result = self.get_result(document) |
paulb@202 | 72 | |
paulb@202 | 73 | if result is not None: |
paulb@202 | 74 | stream.write(result.toString(encoding)) |
paulb@202 | 75 | else: |
paulb@202 | 76 | raise OutputError, "Transformation failed." |
paulb@202 | 77 | |
paulb@202 | 78 | def get_result(self, document): |
paulb@202 | 79 | |
paulb@202 | 80 | """ |
paulb@202 | 81 | Return a transformed document produced from the object's stylesheets and |
paulb@202 | 82 | the given 'document'. |
paulb@202 | 83 | """ |
paulb@202 | 84 | |
paulb@202 | 85 | result = self._get_result(document) |
paulb@202 | 86 | |
paulb@202 | 87 | if result is not None: |
paulb@202 | 88 | return libxml2dom.adoptNodes([result])[0] |
paulb@202 | 89 | else: |
paulb@202 | 90 | raise OutputError, "Transformation failed." |
paulb@202 | 91 | |
paulb@202 | 92 | def _get_result(self, document): |
paulb@202 | 93 | |
paulb@202 | 94 | """ |
paulb@202 | 95 | Return a transformation of the given 'document'. |
paulb@202 | 96 | """ |
paulb@202 | 97 | |
paulb@202 | 98 | if hasattr(document, "as_native_node"): |
paulb@202 | 99 | document = document.as_native_node() |
paulb@202 | 100 | |
paulb@202 | 101 | # Transform the localised instance into the final output. |
paulb@202 | 102 | |
paulb@202 | 103 | parameters = {} |
paulb@202 | 104 | for name, reference in self.references.items(): |
paulb@202 | 105 | parameters[name.encode("utf-8")] = ("document('%s')" % self._quote(reference)).encode("utf-8") |
paulb@202 | 106 | for name, parameter in self.parameters.items(): |
paulb@202 | 107 | parameters[name.encode("utf-8")] = ("'%s'" % self._quote(parameter)).encode("utf-8") |
paulb@202 | 108 | #print "**", repr(parameters) |
paulb@202 | 109 | |
paulb@202 | 110 | last_result = document |
paulb@202 | 111 | for stylesheet in self.stylesheets: |
paulb@202 | 112 | result = libxsltmod.xsltApplyStylesheet(stylesheet, last_result, parameters) |
paulb@202 | 113 | if last_result is not None: |
paulb@202 | 114 | last_result = result |
paulb@202 | 115 | else: |
paulb@202 | 116 | raise OutputError, "Transformation failed." |
paulb@202 | 117 | |
paulb@202 | 118 | return result |
paulb@202 | 119 | |
paulb@202 | 120 | def _quote(self, s): |
paulb@202 | 121 | |
paulb@202 | 122 | "Make the given parameter string 's' palatable for libxslt." |
paulb@202 | 123 | |
paulb@202 | 124 | return s.replace("'", "%27") |
paulb@202 | 125 | |
paulb@202 | 126 | # vim: tabstop=4 expandtab shiftwidth=4 |