1.1 --- a/XSLForms/Prepare.py Thu Aug 18 15:57:13 2005 +0000
1.2 +++ b/XSLForms/Prepare.py Thu Aug 18 15:57:28 2005 +0000
1.3 @@ -20,7 +20,7 @@
1.4 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1.5 """
1.6
1.7 -import XSLOutput
1.8 +from XSLTools import XSLOutput
1.9 import libxml2dom
1.10 import os
1.11
2.1 --- a/XSLForms/Resources.py Thu Aug 18 15:57:13 2005 +0000
2.2 +++ b/XSLForms/Resources.py Thu Aug 18 15:57:28 2005 +0000
2.3 @@ -24,7 +24,7 @@
2.4 import XSLForms.Fields
2.5 import XSLForms.Prepare
2.6 import XSLForms.Output
2.7 -import XSLOutput
2.8 +from XSLTools import XSLOutput
2.9 import os
2.10
2.11 class XSLFormsResource:
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/XSLTools/XMLTable.py Thu Aug 18 15:57:28 2005 +0000
3.3 @@ -0,0 +1,92 @@
3.4 +#!/usr/bin/env python
3.5 +
3.6 +"A list of tuples to XML document converter."
3.7 +
3.8 +import libxml2dom
3.9 +from UserDict import UserDict
3.10 +
3.11 +class OrderedDict(UserDict):
3.12 +
3.13 + "A dictionary with keys in the order in which they were given."
3.14 +
3.15 + def __init__(self, *args):
3.16 + UserDict.__init__(self, *args)
3.17 + self._keys = self.data.keys()
3.18 +
3.19 + def __setitem__(self, key, value):
3.20 + UserDict.__setitem__(self, key, value)
3.21 + if key not in self._keys:
3.22 + self._keys.append(key)
3.23 +
3.24 + def __delitem__(self, key):
3.25 + UserDict.__delitem__(self, key)
3.26 + del self._keys[key]
3.27 +
3.28 + def keys(self):
3.29 + return self._keys
3.30 +
3.31 + def items(self):
3.32 + l = []
3.33 + for key in self._keys:
3.34 + l.append((key, self.data[key]))
3.35 + return l
3.36 +
3.37 + def values(self):
3.38 + return [value for key, value in self.items()]
3.39 +
3.40 +class Converter:
3.41 + def __init__(self, ns=None, prefix="", doc=None, root=None):
3.42 + self.ns = ns
3.43 + self.prefix = prefix
3.44 + if doc is not None:
3.45 + self.doc = doc
3.46 + else:
3.47 + self.doc = libxml2dom.createDocument(ns, prefix+"table", None)
3.48 + if root is not None:
3.49 + self.root = root
3.50 + else:
3.51 + self.root = self.doc.xpath("*")[0]
3.52 +
3.53 + def add_rows(self, rows):
3.54 + for row in rows:
3.55 + self.add_row(row)
3.56 +
3.57 + def add_row(self, row, index=-1, row_element_name="row", use_key_as_element_name=0):
3.58 + row_element = self.doc.createElementNS(self.ns, self.prefix+row_element_name)
3.59 + if index == -1:
3.60 + self.root.appendChild(row_element)
3.61 + else:
3.62 + self.root.insertBefore(row_element, self.root.xpath("*[%s]" % (index + 1))[0])
3.63 +
3.64 + # Permit dictionaries.
3.65 +
3.66 + if hasattr(row, "items"):
3.67 + for name, value in row.items():
3.68 + if use_key_as_element_name:
3.69 + column_element = self.doc.createElementNS(self.ns, self.prefix+unicode(name))
3.70 + else:
3.71 + column_element = self.doc.createElementNS(self.ns, self.prefix+"column")
3.72 + column_element.setAttributeNS(self.ns, self.prefix+"name", unicode(name))
3.73 + row_element.appendChild(column_element)
3.74 + text = self.doc.createTextNode(unicode(value))
3.75 + column_element.appendChild(text)
3.76 +
3.77 + # As well as sequences.
3.78 +
3.79 + else:
3.80 + for column in row:
3.81 + column_element = self.doc.createElementNS(self.ns, self.prefix+"column")
3.82 + row_element.appendChild(column_element)
3.83 + text = self.doc.createTextNode(unicode(column))
3.84 + column_element.appendChild(text)
3.85 +
3.86 +if __name__ == "__main__":
3.87 + from rdflib.TripleStore import TripleStore
3.88 + import sys
3.89 + s = TripleStore()
3.90 + s.load(sys.argv[1])
3.91 + converter = Converter()
3.92 + converter.add_rows(s.triples((None, None, None)))
3.93 + libxml2dom.toStream(converter.doc, sys.stdout)
3.94 +
3.95 +# vim: tabstop=4 expandtab shiftwidth=4
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/XSLTools/XSLOutput.py Thu Aug 18 15:57:28 2005 +0000
4.3 @@ -0,0 +1,126 @@
4.4 +#!/usr/bin/env python
4.5 +
4.6 +"""
4.7 +XSL output classes and functions.
4.8 +
4.9 +Copyright (C) 2005 Paul Boddie <paul@boddie.org.uk>
4.10 +
4.11 +This library is free software; you can redistribute it and/or
4.12 +modify it under the terms of the GNU Lesser General Public
4.13 +License as published by the Free Software Foundation; either
4.14 +version 2.1 of the License, or (at your option) any later version.
4.15 +
4.16 +This library is distributed in the hope that it will be useful,
4.17 +but WITHOUT ANY WARRANTY; without even the implied warranty of
4.18 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4.19 +Lesser General Public License for more details.
4.20 +
4.21 +You should have received a copy of the GNU Lesser General Public
4.22 +License along with this library; if not, write to the Free Software
4.23 +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
4.24 +"""
4.25 +
4.26 +# NOTE: Make this use other XSLT implementations, too.
4.27 +
4.28 +import libxsltmod
4.29 +import libxml2dom
4.30 +
4.31 +class OutputError(Exception):
4.32 + pass
4.33 +
4.34 +class Processor:
4.35 +
4.36 + """
4.37 + A handler which can prepare output for an XMLTools2 template.
4.38 + """
4.39 +
4.40 + def __init__(self, filenames, references=None, parameters=None):
4.41 +
4.42 + """
4.43 + Initialise the handler with the 'filenames' of stylesheets producing the
4.44 + final output, a 'references' dictionary indicating related stylesheets.
4.45 + Additional 'parameters' may also be specified as a dictionary.
4.46 + """
4.47 +
4.48 + self.references = references or {}
4.49 + self.parameters = parameters or {}
4.50 +
4.51 + # Remember the stylesheet documents.
4.52 +
4.53 + self.stylesheets = []
4.54 + for filename in filenames:
4.55 + doc = libxml2dom.macrolib.parseFile(filename)
4.56 + self.stylesheets.append(libxsltmod.xsltParseStylesheetDoc(doc))
4.57 +
4.58 + def __del__(self):
4.59 +
4.60 + """
4.61 + Tidy up the stylesheet documents.
4.62 + """
4.63 +
4.64 + for stylesheet in self.stylesheets:
4.65 + libxsltmod.xsltFreeStylesheet(stylesheet)
4.66 +
4.67 + def send_output(self, stream, encoding, document):
4.68 +
4.69 + """
4.70 + Send output to the given 'stream' using the given output encoding for
4.71 + the given 'document'.
4.72 + """
4.73 +
4.74 + result = self.get_result(document)
4.75 +
4.76 + if result is not None:
4.77 + stream.write(result.toString(encoding))
4.78 + else:
4.79 + raise OutputError, "Transformation failed."
4.80 +
4.81 + def get_result(self, document):
4.82 +
4.83 + """
4.84 + Return a transformed document produced from the object's stylesheets and
4.85 + the given 'document'.
4.86 + """
4.87 +
4.88 + result = self._get_result(document)
4.89 +
4.90 + if result is not None:
4.91 + return libxml2dom.adoptNodes([result])[0]
4.92 + else:
4.93 + raise OutputError, "Transformation failed."
4.94 +
4.95 + def _get_result(self, document):
4.96 +
4.97 + """
4.98 + Return a transformation of the given 'document'.
4.99 + """
4.100 +
4.101 + if hasattr(document, "as_native_node"):
4.102 + document = document.as_native_node()
4.103 +
4.104 + # Transform the localised instance into the final output.
4.105 +
4.106 + parameters = {}
4.107 + for name, reference in self.references.items():
4.108 + parameters[name.encode("utf-8")] = ("document('%s')" % self._quote(reference)).encode("utf-8")
4.109 + for name, parameter in self.parameters.items():
4.110 + parameters[name.encode("utf-8")] = ("'%s'" % self._quote(parameter)).encode("utf-8")
4.111 + #print "**", repr(parameters)
4.112 +
4.113 + last_result = document
4.114 + for stylesheet in self.stylesheets:
4.115 + result = libxsltmod.xsltApplyStylesheet(stylesheet, last_result, parameters)
4.116 + if last_result is not None:
4.117 + last_result = result
4.118 + else:
4.119 + raise OutputError, "Transformation failed."
4.120 +
4.121 + return result
4.122 +
4.123 + def _quote(self, s):
4.124 +
4.125 + "Make the given parameter string 's' palatable for libxslt."
4.126 +
4.127 + return s.replace("'", "%27")
4.128 +
4.129 +# vim: tabstop=4 expandtab shiftwidth=4
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/XSLTools/__init__.py Thu Aug 18 15:57:28 2005 +0000
5.3 @@ -0,0 +1,25 @@
5.4 +#!/usr/bin/env python
5.5 +
5.6 +"""
5.7 +XML/XSL-related helper modules.
5.8 +
5.9 +Copyright (C) 2005 Paul Boddie <paul@boddie.org.uk>
5.10 +
5.11 +This library is free software; you can redistribute it and/or
5.12 +modify it under the terms of the GNU Lesser General Public
5.13 +License as published by the Free Software Foundation; either
5.14 +version 2.1 of the License, or (at your option) any later version.
5.15 +
5.16 +This library is distributed in the hope that it will be useful,
5.17 +but WITHOUT ANY WARRANTY; without even the implied warranty of
5.18 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5.19 +Lesser General Public License for more details.
5.20 +
5.21 +You should have received a copy of the GNU Lesser General Public
5.22 +License along with this library; if not, write to the Free Software
5.23 +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
5.24 +"""
5.25 +
5.26 +__version__ = "0.2"
5.27 +
5.28 +# vim: tabstop=4 expandtab shiftwidth=4