# HG changeset patch # User paulb # Date 1133535110 0 # Node ID 0b75568433fca249973173afe16ab41a32653f9b # Parent a2fc1849f3aee2decc44fe80f5ed765c8139b3f0 [project @ 2005-12-02 14:51:50 by paulb] Replaced the encoding string literals with a single global. Fixed decoding/encoding in the i18n function. diff -r a2fc1849f3ae -r 0b75568433fc XSLForms/Output.py --- a/XSLForms/Output.py Fri Dec 02 14:51:03 2005 +0000 +++ b/XSLForms/Output.py Fri Dec 02 14:51:50 2005 +0000 @@ -25,6 +25,8 @@ import libxml2dom import urllib +libxml2_encoding = "utf-8" + def path_to_node(node, attribute_ref, name, multivalue=0): """ @@ -109,7 +111,7 @@ multivalue = 1 elif name_var is not None: name = libxml2mod.xmlNodeGetContent(name_var[0]) - name = unicode(name, "utf-8") + name = unicode(name, libxml2_encoding) multivalue = 0 else: name = None @@ -129,7 +131,7 @@ #print "this_element" r = path_to_context(context, 0) - return r.encode("utf-8") + return r.encode(libxml2_encoding) def this_attribute(context): @@ -141,7 +143,7 @@ #print "this_attribute" r = path_to_context(context, 1) - return r.encode("utf-8") + return r.encode(libxml2_encoding) def new_attribute(context, name): @@ -153,9 +155,9 @@ """ #print "new_attribute" - name = unicode(name, "utf-8") + name = unicode(name, libxml2_encoding) r = path_to_context(context, 0) + "/" + name - return r.encode("utf-8") + return r.encode(libxml2_encoding) def other_elements(context, nodes): @@ -173,7 +175,7 @@ if name not in names: names.append(name) r = ",".join(names) - return r.encode("utf-8") + return r.encode(libxml2_encoding) def list_attribute(context, element_name, attribute_name): @@ -186,10 +188,10 @@ """ #print "list_attribute" - element_name = unicode(element_name, "utf-8") - attribute_name = unicode(attribute_name, "utf-8") + element_name = unicode(element_name, libxml2_encoding) + attribute_name = unicode(attribute_name, libxml2_encoding) r = path_to_context(context, 0, (element_name, attribute_name)) - return r.encode("utf-8") + return r.encode(libxml2_encoding) def other_list_attributes(context, element_name, attribute_name, nodes): @@ -203,15 +205,15 @@ """ #print "other_list_attributes" - element_name = unicode(element_name, "utf-8") - attribute_name = unicode(attribute_name, "utf-8") + element_name = unicode(element_name, libxml2_encoding) + attribute_name = unicode(attribute_name, libxml2_encoding) names = [] for node in nodes: name = path_to_node(libxml2dom.Node(node), 0, (element_name, attribute_name), 1) if name not in names: names.append(name) r = ",".join(names) - return r.encode("utf-8") + return r.encode(libxml2_encoding) def other_attributes(context, attribute_name, nodes): @@ -224,7 +226,7 @@ """ #print "other_attributes" - attribute_name = unicode(attribute_name, "utf-8") + attribute_name = unicode(attribute_name, libxml2_encoding) # NOTE: Cannot directly reference attributes in the nodes list because # NOTE: libxml2dom does not yet support parent element discovery on # NOTE: attributes. @@ -234,7 +236,7 @@ if name not in names: names.append(name) r = ",".join(names) - return r.encode("utf-8") + return r.encode(libxml2_encoding) def child_element(context, element_name, position, node_paths): @@ -248,12 +250,12 @@ template:child-element('comment', 1, template:this-element()) -> '.../comment$1' """ - element_name = unicode(element_name, "utf-8") + element_name = unicode(element_name, libxml2_encoding) l = [] for node_path in node_paths.split(","): l.append(node_path + Constants.path_separator + element_name + Constants.pair_separator + str(int(position))) - return ",".join(l).encode("utf-8") + return ",".join(l).encode(libxml2_encoding) def child_attribute(context, attribute_name, node_paths): @@ -267,11 +269,11 @@ template:child-attribute('value', template:this-element()) -> '.../value' """ - attribute_name = unicode(attribute_name, "utf-8") + attribute_name = unicode(attribute_name, libxml2_encoding) l = [] for node_path in node_paths.split(","): l.append(node_path + Constants.path_separator + attribute_name) - return ",".join(l).encode("utf-8") + return ",".join(l).encode(libxml2_encoding) def selector_name(context, field_name, nodes): @@ -293,26 +295,26 @@ if name not in names: names.append(field_name + "=" + name) r = ",".join(names) - return r.encode("utf-8") + return r.encode(libxml2_encoding) # Old implementations. def multi_field_name(context, multivalue_name): #print "multi_field_name" - multivalue_name = unicode(multivalue_name, "utf-8") + multivalue_name = unicode(multivalue_name, libxml2_encoding) r = path_to_context(context, 1, multivalue_name) - return r.encode("utf-8") + return r.encode(libxml2_encoding) def other_multi_field_names(context, multivalue_name, nodes): #print "other_multi_field_names" - multivalue_name = unicode(multivalue_name, "utf-8") + multivalue_name = unicode(multivalue_name, libxml2_encoding) names = [] for node in nodes: name = path_to_node(libxml2dom.Node(node), 1, multivalue_name, 1) if name not in names: names.append(name) r = ",".join(names) - return r.encode("utf-8") + return r.encode(libxml2_encoding) # Utility functions. @@ -325,6 +327,7 @@ 'locale' variables defined in the output stylesheet. """ + value = unicode(value, libxml2_encoding) context = libxml2mod.xmlXPathParserGetContext(context) transform_context = libxsltmod.xsltXPathGetTransformContext(context) translations_var = libxsltmod.xsltVariableLookup(transform_context, "translations", None) @@ -333,8 +336,8 @@ translations = libxml2dom.Node(translations_var[0]) results = translations.xpath("/translations/locale[code/@value='%s']/translation[@value='%s']/text()" % (locale_var, value)) if len(results) > 0: - return results[0].nodeValue.encode("utf-8") - return value + return results[0].nodeValue.encode(libxml2_encoding) + return value.encode(libxml2_encoding) def choice(context, value, true_string, false_string=None): @@ -352,7 +355,7 @@ else: return false_string or "" -def url_encode(context, nodes, charset="utf-8"): +def url_encode(context, nodes, charset=libxml2_encoding): """ Exposed as {template:url-encode(nodes)}. @@ -369,11 +372,11 @@ l = [] if isinstance(nodes, str): - return urllib.quote(nodes.encode("utf-8")).replace("/", "%2F").replace("#", "%23") + return urllib.quote(nodes.encode(libxml2_encoding)).replace("/", "%2F").replace("#", "%23") for node in nodes: s = libxml2dom.Node(node).nodeValue - l.append(urllib.quote(s.encode("utf-8")).replace("/", "%2F").replace("#", "%23")) + l.append(urllib.quote(s.encode(libxml2_encoding)).replace("/", "%2F").replace("#", "%23")) output = "".join(l) return output