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