paulb@1 | 1 | #!/usr/bin/env python |
paulb@1 | 2 | |
paulb@1 | 3 | """ |
paulb@1 | 4 | XSL output classes and functions. |
paulb@1 | 5 | """ |
paulb@1 | 6 | |
paulb@1 | 7 | # NOTE: Make this use other implementations, too. |
paulb@1 | 8 | |
paulb@1 | 9 | import libxslt |
paulb@1 | 10 | import libxml2dom |
paulb@1 | 11 | |
paulb@1 | 12 | class OutputError(Exception): |
paulb@1 | 13 | pass |
paulb@1 | 14 | |
paulb@1 | 15 | class Processor: |
paulb@1 | 16 | |
paulb@1 | 17 | """ |
paulb@1 | 18 | A handler which can prepare output for an XMLTools2 template. |
paulb@1 | 19 | """ |
paulb@1 | 20 | |
paulb@32 | 21 | def __init__(self, filenames, references=None, parameters=None): |
paulb@1 | 22 | |
paulb@1 | 23 | """ |
paulb@1 | 24 | Initialise the handler with the 'filenames' of stylesheets producing the |
paulb@1 | 25 | final output, a 'references' dictionary indicating related stylesheets. |
paulb@32 | 26 | Additional 'parameters' may also be specified as a dictionary. |
paulb@1 | 27 | """ |
paulb@1 | 28 | |
paulb@1 | 29 | self.references = references or {} |
paulb@32 | 30 | self.parameters = parameters or {} |
paulb@1 | 31 | |
paulb@1 | 32 | # Remember the stylesheet documents. |
paulb@1 | 33 | |
paulb@1 | 34 | self.stylesheets = [] |
paulb@1 | 35 | for filename in filenames: |
paulb@1 | 36 | self.stylesheets.append(libxslt.parseStylesheetFile(filename)) |
paulb@1 | 37 | |
paulb@1 | 38 | def __del__(self): |
paulb@1 | 39 | |
paulb@1 | 40 | """ |
paulb@1 | 41 | Tidy up the stylesheet documents. |
paulb@1 | 42 | """ |
paulb@1 | 43 | |
paulb@1 | 44 | for stylesheet in self.stylesheets: |
paulb@1 | 45 | stylesheet.freeStylesheet() |
paulb@1 | 46 | |
paulb@1 | 47 | def send_output(self, stream, encoding, document): |
paulb@1 | 48 | |
paulb@1 | 49 | """ |
paulb@1 | 50 | Send output to the given 'stream' using the given output encoding for |
paulb@1 | 51 | the given 'document'. |
paulb@1 | 52 | """ |
paulb@1 | 53 | |
paulb@1 | 54 | result = self._get_result(document) |
paulb@1 | 55 | |
paulb@1 | 56 | if result is not None: |
paulb@1 | 57 | # Since result is a native node, use the serialize method. |
paulb@1 | 58 | stream.write(result.serialize(encoding)) |
paulb@1 | 59 | result.freeDoc() |
paulb@1 | 60 | else: |
paulb@1 | 61 | raise OutputError, "Transformation failed." |
paulb@1 | 62 | |
paulb@1 | 63 | def get_result(self, document): |
paulb@1 | 64 | |
paulb@1 | 65 | """ |
paulb@1 | 66 | Return a transformed document produced from the object's stylesheets and |
paulb@1 | 67 | the given 'document'. |
paulb@1 | 68 | """ |
paulb@1 | 69 | |
paulb@1 | 70 | result = self._get_result(document) |
paulb@1 | 71 | |
paulb@1 | 72 | if result is not None: |
paulb@1 | 73 | return libxml2dom.Node(result) |
paulb@1 | 74 | else: |
paulb@1 | 75 | raise OutputError, "Transformation failed." |
paulb@1 | 76 | |
paulb@1 | 77 | def _get_result(self, document): |
paulb@1 | 78 | |
paulb@1 | 79 | """ |
paulb@1 | 80 | Return a transformation of the given 'document'. |
paulb@1 | 81 | """ |
paulb@1 | 82 | |
paulb@1 | 83 | if hasattr(document, "as_native_node"): |
paulb@1 | 84 | document = document.as_native_node() |
paulb@1 | 85 | |
paulb@1 | 86 | # Transform the localised instance into the final output. |
paulb@1 | 87 | |
paulb@1 | 88 | parameters = {} |
paulb@1 | 89 | for name, reference in self.references.items(): |
paulb@1 | 90 | parameters[name.encode("utf-8")] = ("document('%s')" % reference).encode("utf-8") |
paulb@32 | 91 | for name, parameter in self.parameters.items(): |
paulb@32 | 92 | parameters[name.encode("utf-8")] = ("'%s'" % parameter).encode("utf-8") |
paulb@1 | 93 | |
paulb@1 | 94 | last_result = document |
paulb@1 | 95 | for stylesheet in self.stylesheets: |
paulb@1 | 96 | result = stylesheet.applyStylesheet(last_result, parameters) |
paulb@1 | 97 | if last_result is not None: |
paulb@1 | 98 | if last_result != document: |
paulb@1 | 99 | last_result.freeDoc() |
paulb@1 | 100 | last_result = result |
paulb@1 | 101 | else: |
paulb@1 | 102 | raise OutputError, "Transformation failed." |
paulb@1 | 103 | |
paulb@1 | 104 | return result |
paulb@1 | 105 | |
paulb@1 | 106 | # vim: tabstop=4 expandtab shiftwidth=4 |