# HG changeset patch # User paulb # Date 1116186853 0 # Node ID 9185ed6e5bd4343667861aedf6f48e28301ac30a # Parent 4d1f2d5dde87b888a4c88914be33c78d13bc9da8 [project @ 2005-05-15 19:54:05 by paulb] Migrated to libxml2dom and libxsltmod. diff -r 4d1f2d5dde87 -r 9185ed6e5bd4 XSLForms/Fields.py --- a/XSLForms/Fields.py Sun May 08 23:13:39 2005 +0000 +++ b/XSLForms/Fields.py Sun May 15 19:54:13 2005 +0000 @@ -369,7 +369,7 @@ print "Building time", time.time() - t t = time.time() - libxml2dom.toStream(documents[args["instance-name"]], stream=open(args["plain-output"], "wb"), encoding="utf-8") + documents[args["instance-name"]].toStream(stream=open(args["plain-output"], "wb"), encoding="utf-8") print "Prettyprinting time", time.time() - t print "Selectors", repr(fields.get_selectors(items, documents)) diff -r 4d1f2d5dde87 -r 9185ed6e5bd4 XSLForms/Output.py --- a/XSLForms/Output.py Sun May 08 23:13:39 2005 +0000 +++ b/XSLForms/Output.py Sun May 15 19:54:13 2005 +0000 @@ -5,8 +5,8 @@ """ import Constants -import libxslt -import libxml2 +import libxsltmod, libxml2mod +import libxml2dom """ import libxml2 @@ -42,8 +42,8 @@ l = [] # Skip attribute reference. - if node.type == "attribute": - node = node.parent + if node.nodeType == node.ATTRIBUTE_NODE: + node = node.parentNode # Manually insert the attribute name if defined. if attribute_ref: # A real attribute is referenced. @@ -51,21 +51,21 @@ l.insert(0, name) if multivalue: l.insert(0, Constants.multi_separator) - l.insert(0, node.name) - node = node.parent + l.insert(0, node.nodeName) + node = node.parentNode l.insert(0, Constants.path_separator) # Otherwise, treat the element name as an attribute name. else: - l.insert(0, node.name) + l.insert(0, node.nodeName) l.insert(0, Constants.path_separator) - node = node.parent + node = node.parentNode # Element references. - while node is not None and node.type != "document_xml": - l.insert(0, str(int(node.xpathEval("count(preceding-sibling::*) + 1")))) + while node is not None and node.nodeType != node.DOCUMENT_NODE: + l.insert(0, str(int(node.xpath("count(preceding-sibling::*) + 1")))) l.insert(0, Constants.pair_separator) - l.insert(0, node.name) + l.insert(0, node.nodeName) l.insert(0, Constants.path_separator) - node = node.parent + node = node.parentNode return "".join(l) def path_to_context(context, attribute_ref, multivalue_name=None): @@ -79,11 +79,10 @@ name. """ - pctxt = libxslt.xpathParserContext(_obj=context) - context = pctxt.context() - node = context.contextNode() - transform_context = context.transformContext() - name_var = transform_context.variableLookup("this-name", None) + context = libxml2mod.xmlXPathParserGetContext(context) + #context = libxsltmod.xsltXPathGetTransformContext(pctxt) + transform_context = libxsltmod.xsltXPathGetTransformContext(context) + name_var = libxsltmod.xsltVariableLookup(transform_context, "this-name", None) if multivalue_name is not None: name = multivalue_name multivalue = 1 @@ -93,44 +92,49 @@ else: name = None multivalue = 0 + node = libxml2dom.Node(libxml2mod.xmlXPathGetContextNode(context)) return path_to_node(node, attribute_ref, name, multivalue) def this_position(context): - return path_to_context(context, 0) + r = path_to_context(context, 0) + return r.encode("utf-8") def field_name(context): - return path_to_context(context, 1) + r = path_to_context(context, 1) + return r.encode("utf-8") def multi_field_name(context, multivalue_name): - return path_to_context(context, 1, multivalue_name) + r = path_to_context(context, 1, multivalue_name) + return r.encode("utf-8") def new_field(context, name): - return path_to_context(context, 0) + "/" + name + r = path_to_context(context, 0) + "/" + name + return r.encode("utf-8") def other_field_names(context, nodes): names = [] for node in nodes: - n = libxml2.xmlNode(node) - name = path_to_node(n, 0, None, 0) + name = path_to_node(libxml2dom.Node(node), 0, None, 0) if name not in names: names.append(name) - return ",".join(names) + r = ",".join(names) + return r.encode("utf-8") def other_multi_field_names(context, multivalue_name, nodes): names = [] for node in nodes: - n = libxml2.xmlNode(node) - name = path_to_node(n, 1, multivalue_name, 1) + name = path_to_node(libxml2dom.Node(node), 1, multivalue_name, 1) if name not in names: names.append(name) - return ",".join(names) + r = ",".join(names) + return r.encode("utf-8") -libxslt.registerExtModuleFunction("this-position", "http://www.boddie.org.uk/ns/xmltools/template", this_position) -libxslt.registerExtModuleFunction("field-name", "http://www.boddie.org.uk/ns/xmltools/template", field_name) -libxslt.registerExtModuleFunction("multi-field-name", "http://www.boddie.org.uk/ns/xmltools/template", multi_field_name) -libxslt.registerExtModuleFunction("new-field", "http://www.boddie.org.uk/ns/xmltools/template", new_field) -libxslt.registerExtModuleFunction("other-field-names", "http://www.boddie.org.uk/ns/xmltools/template", other_field_names) -libxslt.registerExtModuleFunction("other-multi-field-names", "http://www.boddie.org.uk/ns/xmltools/template", other_multi_field_names) +libxsltmod.xsltRegisterExtModuleFunction("this-position", "http://www.boddie.org.uk/ns/xmltools/template", this_position) +libxsltmod.xsltRegisterExtModuleFunction("field-name", "http://www.boddie.org.uk/ns/xmltools/template", field_name) +libxsltmod.xsltRegisterExtModuleFunction("multi-field-name", "http://www.boddie.org.uk/ns/xmltools/template", multi_field_name) +libxsltmod.xsltRegisterExtModuleFunction("new-field", "http://www.boddie.org.uk/ns/xmltools/template", new_field) +libxsltmod.xsltRegisterExtModuleFunction("other-field-names", "http://www.boddie.org.uk/ns/xmltools/template", other_field_names) +libxsltmod.xsltRegisterExtModuleFunction("other-multi-field-names", "http://www.boddie.org.uk/ns/xmltools/template", other_multi_field_names) def get_field_name(field_or_multi_name): return field_or_multi_name.split(Constants.multi_separator)[0] diff -r 4d1f2d5dde87 -r 9185ed6e5bd4 XSLOutput.py --- a/XSLOutput.py Sun May 08 23:13:39 2005 +0000 +++ b/XSLOutput.py Sun May 15 19:54:13 2005 +0000 @@ -6,7 +6,7 @@ # NOTE: Make this use other implementations, too. -import libxslt +import libxsltmod import libxml2dom class OutputError(Exception): @@ -33,7 +33,7 @@ self.stylesheets = [] for filename in filenames: - self.stylesheets.append(libxslt.parseStylesheetFile(filename)) + self.stylesheets.append(libxsltmod.xsltParseStylesheetFile(filename)) def __del__(self): @@ -42,7 +42,7 @@ """ for stylesheet in self.stylesheets: - stylesheet.freeStylesheet() + libxsltmod.xsltFreeStylesheet(stylesheet) def send_output(self, stream, encoding, document): @@ -51,12 +51,10 @@ the given 'document'. """ - result = self._get_result(document) + result = self.get_result(document) if result is not None: - # Since result is a native node, use the serialize method. - stream.write(result.serialize(encoding)) - result.freeDoc() + stream.write(result.toString(encoding)) else: raise OutputError, "Transformation failed." @@ -70,7 +68,7 @@ result = self._get_result(document) if result is not None: - return libxml2dom.Node(result) + return libxml2dom.adoptNodes([result])[0] else: raise OutputError, "Transformation failed." @@ -93,10 +91,8 @@ last_result = document for stylesheet in self.stylesheets: - result = stylesheet.applyStylesheet(last_result, parameters) + result = libxsltmod.xsltApplyStylesheet(stylesheet, last_result, parameters) if last_result is not None: - if last_result is not document: - last_result.freeDoc() last_result = result else: raise OutputError, "Transformation failed." diff -r 4d1f2d5dde87 -r 9185ed6e5bd4 examples/Common/Configurator/__init__.py --- a/examples/Common/Configurator/__init__.py Sun May 08 23:13:39 2005 +0000 +++ b/examples/Common/Configurator/__init__.py Sun May 15 19:54:13 2005 +0000 @@ -73,7 +73,6 @@ # Get the XML representation of the request. documents = fields.make_documents(parameters.items()) - print "*", libxml2dom.toString(documents["configuration"]) else: trans.set_response_code(405) raise WebStack.Generic.EndOfResponse @@ -84,6 +83,7 @@ configuration = documents["configuration"] else: configuration = fields.new_instance("configuration") + print "*", configuration.toString() # Add and remove elements according to the selectors found. @@ -100,7 +100,7 @@ if parameters.has_key("export"): trans.set_content_type(WebStack.Generic.ContentType("text/xml", self.encoding)) - libxml2dom.toStream(configuration, trans.get_response_stream(), trans.get_response_stream_encoding()) + configuration.toStream(trans.get_response_stream(), trans.get_response_stream_encoding()) # When not exported, the data is transformed to produce a normal Web # page.