XSLTools

Changeset

438:0b75568433fc
2005-12-02 paulb raw files shortlog changelog graph [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.
XSLForms/Output.py (file)
     1.1 --- a/XSLForms/Output.py	Fri Dec 02 14:51:03 2005 +0000
     1.2 +++ b/XSLForms/Output.py	Fri Dec 02 14:51:50 2005 +0000
     1.3 @@ -25,6 +25,8 @@
     1.4  import libxml2dom
     1.5  import urllib
     1.6  
     1.7 +libxml2_encoding = "utf-8"
     1.8 +
     1.9  def path_to_node(node, attribute_ref, name, multivalue=0):
    1.10  
    1.11      """
    1.12 @@ -109,7 +111,7 @@
    1.13          multivalue = 1
    1.14      elif name_var is not None:
    1.15          name = libxml2mod.xmlNodeGetContent(name_var[0])
    1.16 -        name = unicode(name, "utf-8")
    1.17 +        name = unicode(name, libxml2_encoding)
    1.18          multivalue = 0
    1.19      else:
    1.20          name = None
    1.21 @@ -129,7 +131,7 @@
    1.22  
    1.23      #print "this_element"
    1.24      r = path_to_context(context, 0)
    1.25 -    return r.encode("utf-8")
    1.26 +    return r.encode(libxml2_encoding)
    1.27  
    1.28  def this_attribute(context):
    1.29  
    1.30 @@ -141,7 +143,7 @@
    1.31  
    1.32      #print "this_attribute"
    1.33      r = path_to_context(context, 1)
    1.34 -    return r.encode("utf-8")
    1.35 +    return r.encode(libxml2_encoding)
    1.36  
    1.37  def new_attribute(context, name):
    1.38  
    1.39 @@ -153,9 +155,9 @@
    1.40      """
    1.41  
    1.42      #print "new_attribute"
    1.43 -    name = unicode(name, "utf-8")
    1.44 +    name = unicode(name, libxml2_encoding)
    1.45      r = path_to_context(context, 0) + "/" + name
    1.46 -    return r.encode("utf-8")
    1.47 +    return r.encode(libxml2_encoding)
    1.48  
    1.49  def other_elements(context, nodes):
    1.50  
    1.51 @@ -173,7 +175,7 @@
    1.52          if name not in names:
    1.53              names.append(name)
    1.54      r = ",".join(names)
    1.55 -    return r.encode("utf-8")
    1.56 +    return r.encode(libxml2_encoding)
    1.57  
    1.58  def list_attribute(context, element_name, attribute_name):
    1.59  
    1.60 @@ -186,10 +188,10 @@
    1.61      """
    1.62  
    1.63      #print "list_attribute"
    1.64 -    element_name = unicode(element_name, "utf-8")
    1.65 -    attribute_name = unicode(attribute_name, "utf-8")
    1.66 +    element_name = unicode(element_name, libxml2_encoding)
    1.67 +    attribute_name = unicode(attribute_name, libxml2_encoding)
    1.68      r = path_to_context(context, 0, (element_name, attribute_name))
    1.69 -    return r.encode("utf-8")
    1.70 +    return r.encode(libxml2_encoding)
    1.71  
    1.72  def other_list_attributes(context, element_name, attribute_name, nodes):
    1.73  
    1.74 @@ -203,15 +205,15 @@
    1.75      """
    1.76  
    1.77      #print "other_list_attributes"
    1.78 -    element_name = unicode(element_name, "utf-8")
    1.79 -    attribute_name = unicode(attribute_name, "utf-8")
    1.80 +    element_name = unicode(element_name, libxml2_encoding)
    1.81 +    attribute_name = unicode(attribute_name, libxml2_encoding)
    1.82      names = []
    1.83      for node in nodes:
    1.84          name = path_to_node(libxml2dom.Node(node), 0, (element_name, attribute_name), 1)
    1.85          if name not in names:
    1.86              names.append(name)
    1.87      r = ",".join(names)
    1.88 -    return r.encode("utf-8")
    1.89 +    return r.encode(libxml2_encoding)
    1.90  
    1.91  def other_attributes(context, attribute_name, nodes):
    1.92  
    1.93 @@ -224,7 +226,7 @@
    1.94      """
    1.95  
    1.96      #print "other_attributes"
    1.97 -    attribute_name = unicode(attribute_name, "utf-8")
    1.98 +    attribute_name = unicode(attribute_name, libxml2_encoding)
    1.99      # NOTE: Cannot directly reference attributes in the nodes list because
   1.100      # NOTE: libxml2dom does not yet support parent element discovery on
   1.101      # NOTE: attributes.
   1.102 @@ -234,7 +236,7 @@
   1.103          if name not in names:
   1.104              names.append(name)
   1.105      r = ",".join(names)
   1.106 -    return r.encode("utf-8")
   1.107 +    return r.encode(libxml2_encoding)
   1.108  
   1.109  def child_element(context, element_name, position, node_paths):
   1.110  
   1.111 @@ -248,12 +250,12 @@
   1.112      template:child-element('comment', 1, template:this-element()) -> '.../comment$1'
   1.113      """
   1.114  
   1.115 -    element_name = unicode(element_name, "utf-8")
   1.116 +    element_name = unicode(element_name, libxml2_encoding)
   1.117      l = []
   1.118      for node_path in node_paths.split(","):
   1.119          l.append(node_path + Constants.path_separator + element_name
   1.120              + Constants.pair_separator + str(int(position)))
   1.121 -    return ",".join(l).encode("utf-8")
   1.122 +    return ",".join(l).encode(libxml2_encoding)
   1.123  
   1.124  def child_attribute(context, attribute_name, node_paths):
   1.125  
   1.126 @@ -267,11 +269,11 @@
   1.127      template:child-attribute('value', template:this-element()) -> '.../value'
   1.128      """
   1.129  
   1.130 -    attribute_name = unicode(attribute_name, "utf-8")
   1.131 +    attribute_name = unicode(attribute_name, libxml2_encoding)
   1.132      l = []
   1.133      for node_path in node_paths.split(","):
   1.134          l.append(node_path + Constants.path_separator + attribute_name)
   1.135 -    return ",".join(l).encode("utf-8")
   1.136 +    return ",".join(l).encode(libxml2_encoding)
   1.137  
   1.138  def selector_name(context, field_name, nodes):
   1.139  
   1.140 @@ -293,26 +295,26 @@
   1.141          if name not in names:
   1.142              names.append(field_name + "=" + name)
   1.143      r = ",".join(names)
   1.144 -    return r.encode("utf-8")
   1.145 +    return r.encode(libxml2_encoding)
   1.146  
   1.147  # Old implementations.
   1.148  
   1.149  def multi_field_name(context, multivalue_name):
   1.150      #print "multi_field_name"
   1.151 -    multivalue_name = unicode(multivalue_name, "utf-8")
   1.152 +    multivalue_name = unicode(multivalue_name, libxml2_encoding)
   1.153      r = path_to_context(context, 1, multivalue_name)
   1.154 -    return r.encode("utf-8")
   1.155 +    return r.encode(libxml2_encoding)
   1.156  
   1.157  def other_multi_field_names(context, multivalue_name, nodes):
   1.158      #print "other_multi_field_names"
   1.159 -    multivalue_name = unicode(multivalue_name, "utf-8")
   1.160 +    multivalue_name = unicode(multivalue_name, libxml2_encoding)
   1.161      names = []
   1.162      for node in nodes:
   1.163          name = path_to_node(libxml2dom.Node(node), 1, multivalue_name, 1)
   1.164          if name not in names:
   1.165              names.append(name)
   1.166      r = ",".join(names)
   1.167 -    return r.encode("utf-8")
   1.168 +    return r.encode(libxml2_encoding)
   1.169  
   1.170  # Utility functions.
   1.171  
   1.172 @@ -325,6 +327,7 @@
   1.173      'locale' variables defined in the output stylesheet.
   1.174      """
   1.175  
   1.176 +    value = unicode(value, libxml2_encoding)
   1.177      context = libxml2mod.xmlXPathParserGetContext(context)
   1.178      transform_context = libxsltmod.xsltXPathGetTransformContext(context)
   1.179      translations_var = libxsltmod.xsltVariableLookup(transform_context, "translations", None)
   1.180 @@ -333,8 +336,8 @@
   1.181          translations = libxml2dom.Node(translations_var[0])
   1.182          results = translations.xpath("/translations/locale[code/@value='%s']/translation[@value='%s']/text()" % (locale_var, value))
   1.183          if len(results) > 0:
   1.184 -            return results[0].nodeValue.encode("utf-8")
   1.185 -    return value
   1.186 +            return results[0].nodeValue.encode(libxml2_encoding)
   1.187 +    return value.encode(libxml2_encoding)
   1.188  
   1.189  def choice(context, value, true_string, false_string=None):
   1.190  
   1.191 @@ -352,7 +355,7 @@
   1.192      else:
   1.193          return false_string or ""
   1.194  
   1.195 -def url_encode(context, nodes, charset="utf-8"):
   1.196 +def url_encode(context, nodes, charset=libxml2_encoding):
   1.197  
   1.198      """
   1.199      Exposed as {template:url-encode(nodes)}.
   1.200 @@ -369,11 +372,11 @@
   1.201  
   1.202      l = []
   1.203      if isinstance(nodes, str):
   1.204 -        return urllib.quote(nodes.encode("utf-8")).replace("/", "%2F").replace("#", "%23")
   1.205 +        return urllib.quote(nodes.encode(libxml2_encoding)).replace("/", "%2F").replace("#", "%23")
   1.206  
   1.207      for node in nodes:
   1.208          s = libxml2dom.Node(node).nodeValue
   1.209 -        l.append(urllib.quote(s.encode("utf-8")).replace("/", "%2F").replace("#", "%23"))
   1.210 +        l.append(urllib.quote(s.encode(libxml2_encoding)).replace("/", "%2F").replace("#", "%23"))
   1.211      output = "".join(l)
   1.212      return output
   1.213