# HG changeset patch # User paulb # Date 1124380648 0 # Node ID 0e04a13ff6585f7c42e87c72e3d3d434e8d1926f # Parent 7011c24f4bb7c5142eda9262846e8df3e2ef383a [project @ 2005-08-18 15:57:21 by paulb] Moved XSLOutput and XMLTable into a new XSLTools package. diff -r 7011c24f4bb7 -r 0e04a13ff658 XSLForms/Prepare.py --- a/XSLForms/Prepare.py Thu Aug 18 15:57:13 2005 +0000 +++ b/XSLForms/Prepare.py Thu Aug 18 15:57:28 2005 +0000 @@ -20,7 +20,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA """ -import XSLOutput +from XSLTools import XSLOutput import libxml2dom import os diff -r 7011c24f4bb7 -r 0e04a13ff658 XSLForms/Resources.py --- a/XSLForms/Resources.py Thu Aug 18 15:57:13 2005 +0000 +++ b/XSLForms/Resources.py Thu Aug 18 15:57:28 2005 +0000 @@ -24,7 +24,7 @@ import XSLForms.Fields import XSLForms.Prepare import XSLForms.Output -import XSLOutput +from XSLTools import XSLOutput import os class XSLFormsResource: diff -r 7011c24f4bb7 -r 0e04a13ff658 XSLTools/XMLTable.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/XSLTools/XMLTable.py Thu Aug 18 15:57:28 2005 +0000 @@ -0,0 +1,92 @@ +#!/usr/bin/env python + +"A list of tuples to XML document converter." + +import libxml2dom +from UserDict import UserDict + +class OrderedDict(UserDict): + + "A dictionary with keys in the order in which they were given." + + def __init__(self, *args): + UserDict.__init__(self, *args) + self._keys = self.data.keys() + + def __setitem__(self, key, value): + UserDict.__setitem__(self, key, value) + if key not in self._keys: + self._keys.append(key) + + def __delitem__(self, key): + UserDict.__delitem__(self, key) + del self._keys[key] + + def keys(self): + return self._keys + + def items(self): + l = [] + for key in self._keys: + l.append((key, self.data[key])) + return l + + def values(self): + return [value for key, value in self.items()] + +class Converter: + def __init__(self, ns=None, prefix="", doc=None, root=None): + self.ns = ns + self.prefix = prefix + if doc is not None: + self.doc = doc + else: + self.doc = libxml2dom.createDocument(ns, prefix+"table", None) + if root is not None: + self.root = root + else: + self.root = self.doc.xpath("*")[0] + + def add_rows(self, rows): + for row in rows: + self.add_row(row) + + def add_row(self, row, index=-1, row_element_name="row", use_key_as_element_name=0): + row_element = self.doc.createElementNS(self.ns, self.prefix+row_element_name) + if index == -1: + self.root.appendChild(row_element) + else: + self.root.insertBefore(row_element, self.root.xpath("*[%s]" % (index + 1))[0]) + + # Permit dictionaries. + + if hasattr(row, "items"): + for name, value in row.items(): + if use_key_as_element_name: + column_element = self.doc.createElementNS(self.ns, self.prefix+unicode(name)) + else: + column_element = self.doc.createElementNS(self.ns, self.prefix+"column") + column_element.setAttributeNS(self.ns, self.prefix+"name", unicode(name)) + row_element.appendChild(column_element) + text = self.doc.createTextNode(unicode(value)) + column_element.appendChild(text) + + # As well as sequences. + + else: + for column in row: + column_element = self.doc.createElementNS(self.ns, self.prefix+"column") + row_element.appendChild(column_element) + text = self.doc.createTextNode(unicode(column)) + column_element.appendChild(text) + +if __name__ == "__main__": + from rdflib.TripleStore import TripleStore + import sys + s = TripleStore() + s.load(sys.argv[1]) + converter = Converter() + converter.add_rows(s.triples((None, None, None))) + libxml2dom.toStream(converter.doc, sys.stdout) + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 7011c24f4bb7 -r 0e04a13ff658 XSLTools/XSLOutput.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/XSLTools/XSLOutput.py Thu Aug 18 15:57:28 2005 +0000 @@ -0,0 +1,126 @@ +#!/usr/bin/env python + +""" +XSL output classes and functions. + +Copyright (C) 2005 Paul Boddie + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +""" + +# NOTE: Make this use other XSLT implementations, too. + +import libxsltmod +import libxml2dom + +class OutputError(Exception): + pass + +class Processor: + + """ + A handler which can prepare output for an XMLTools2 template. + """ + + def __init__(self, filenames, references=None, parameters=None): + + """ + Initialise the handler with the 'filenames' of stylesheets producing the + final output, a 'references' dictionary indicating related stylesheets. + Additional 'parameters' may also be specified as a dictionary. + """ + + self.references = references or {} + self.parameters = parameters or {} + + # Remember the stylesheet documents. + + self.stylesheets = [] + for filename in filenames: + doc = libxml2dom.macrolib.parseFile(filename) + self.stylesheets.append(libxsltmod.xsltParseStylesheetDoc(doc)) + + def __del__(self): + + """ + Tidy up the stylesheet documents. + """ + + for stylesheet in self.stylesheets: + libxsltmod.xsltFreeStylesheet(stylesheet) + + def send_output(self, stream, encoding, document): + + """ + Send output to the given 'stream' using the given output encoding for + the given 'document'. + """ + + result = self.get_result(document) + + if result is not None: + stream.write(result.toString(encoding)) + else: + raise OutputError, "Transformation failed." + + def get_result(self, document): + + """ + Return a transformed document produced from the object's stylesheets and + the given 'document'. + """ + + result = self._get_result(document) + + if result is not None: + return libxml2dom.adoptNodes([result])[0] + else: + raise OutputError, "Transformation failed." + + def _get_result(self, document): + + """ + Return a transformation of the given 'document'. + """ + + if hasattr(document, "as_native_node"): + document = document.as_native_node() + + # Transform the localised instance into the final output. + + parameters = {} + for name, reference in self.references.items(): + parameters[name.encode("utf-8")] = ("document('%s')" % self._quote(reference)).encode("utf-8") + for name, parameter in self.parameters.items(): + parameters[name.encode("utf-8")] = ("'%s'" % self._quote(parameter)).encode("utf-8") + #print "**", repr(parameters) + + last_result = document + for stylesheet in self.stylesheets: + result = libxsltmod.xsltApplyStylesheet(stylesheet, last_result, parameters) + if last_result is not None: + last_result = result + else: + raise OutputError, "Transformation failed." + + return result + + def _quote(self, s): + + "Make the given parameter string 's' palatable for libxslt." + + return s.replace("'", "%27") + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 7011c24f4bb7 -r 0e04a13ff658 XSLTools/__init__.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/XSLTools/__init__.py Thu Aug 18 15:57:28 2005 +0000 @@ -0,0 +1,25 @@ +#!/usr/bin/env python + +""" +XML/XSL-related helper modules. + +Copyright (C) 2005 Paul Boddie + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +""" + +__version__ = "0.2" + +# vim: tabstop=4 expandtab shiftwidth=4