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@66 | 9 | import libxsltmod |
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@66 | 36 | self.stylesheets.append(libxsltmod.xsltParseStylesheetFile(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@66 | 45 | libxsltmod.xsltFreeStylesheet(stylesheet) |
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@66 | 54 | result = self.get_result(document) |
paulb@1 | 55 | |
paulb@1 | 56 | if result is not None: |
paulb@66 | 57 | stream.write(result.toString(encoding)) |
paulb@1 | 58 | else: |
paulb@1 | 59 | raise OutputError, "Transformation failed." |
paulb@1 | 60 | |
paulb@1 | 61 | def get_result(self, document): |
paulb@1 | 62 | |
paulb@1 | 63 | """ |
paulb@1 | 64 | Return a transformed document produced from the object's stylesheets and |
paulb@1 | 65 | the given 'document'. |
paulb@1 | 66 | """ |
paulb@1 | 67 | |
paulb@1 | 68 | result = self._get_result(document) |
paulb@1 | 69 | |
paulb@1 | 70 | if result is not None: |
paulb@66 | 71 | return libxml2dom.adoptNodes([result])[0] |
paulb@1 | 72 | else: |
paulb@1 | 73 | raise OutputError, "Transformation failed." |
paulb@1 | 74 | |
paulb@1 | 75 | def _get_result(self, document): |
paulb@1 | 76 | |
paulb@1 | 77 | """ |
paulb@1 | 78 | Return a transformation of the given 'document'. |
paulb@1 | 79 | """ |
paulb@1 | 80 | |
paulb@1 | 81 | if hasattr(document, "as_native_node"): |
paulb@1 | 82 | document = document.as_native_node() |
paulb@1 | 83 | |
paulb@1 | 84 | # Transform the localised instance into the final output. |
paulb@1 | 85 | |
paulb@1 | 86 | parameters = {} |
paulb@1 | 87 | for name, reference in self.references.items(): |
paulb@35 | 88 | parameters[name.encode("utf-8")] = ("document('%s')" % self._quote(reference)).encode("utf-8") |
paulb@32 | 89 | for name, parameter in self.parameters.items(): |
paulb@35 | 90 | parameters[name.encode("utf-8")] = ("'%s'" % self._quote(parameter)).encode("utf-8") |
paulb@77 | 91 | #print "**", repr(parameters) |
paulb@1 | 92 | |
paulb@1 | 93 | last_result = document |
paulb@1 | 94 | for stylesheet in self.stylesheets: |
paulb@66 | 95 | result = libxsltmod.xsltApplyStylesheet(stylesheet, last_result, parameters) |
paulb@1 | 96 | if last_result is not None: |
paulb@1 | 97 | last_result = result |
paulb@1 | 98 | else: |
paulb@1 | 99 | raise OutputError, "Transformation failed." |
paulb@1 | 100 | |
paulb@1 | 101 | return result |
paulb@1 | 102 | |
paulb@35 | 103 | def _quote(self, s): |
paulb@35 | 104 | |
paulb@35 | 105 | "Make the given parameter string 's' palatable for libxslt." |
paulb@35 | 106 | |
paulb@35 | 107 | return s.replace("'", "%27") |
paulb@35 | 108 | |
paulb@1 | 109 | # vim: tabstop=4 expandtab shiftwidth=4 |