XSLTools

Changeset

213:a191a456cc5c
2005-08-26 paulb raw files shortlog changelog graph [project @ 2005-08-26 23:19:28 by paulb] Fixed incoming strings so that Unicode objects are created as soon as possible - previously UTF-8 strings were being erroneously passed around. Added a url-encode extension function.
XSLForms/Output.py (file)
     1.1 --- a/XSLForms/Output.py	Thu Aug 25 14:46:57 2005 +0000
     1.2 +++ b/XSLForms/Output.py	Fri Aug 26 23:19:28 2005 +0000
     1.3 @@ -23,6 +23,7 @@
     1.4  import Constants
     1.5  import libxsltmod, libxml2mod
     1.6  import libxml2dom
     1.7 +import urllib
     1.8  
     1.9  """
    1.10  import libxml2
    1.11 @@ -117,6 +118,7 @@
    1.12          multivalue = 1
    1.13      elif name_var is not None:
    1.14          name = libxml2mod.xmlNodeGetContent(name_var[0])
    1.15 +        name = unicode(name, "utf-8")
    1.16          multivalue = 0
    1.17      else:
    1.18          name = None
    1.19 @@ -157,6 +159,7 @@
    1.20      """
    1.21  
    1.22      #print "new_attribute"
    1.23 +    name = unicode(name, "utf-8")
    1.24      r = path_to_context(context, 0) + "/" + name
    1.25      return r.encode("utf-8")
    1.26  
    1.27 @@ -187,6 +190,8 @@
    1.28      """
    1.29  
    1.30      #print "list_attribute"
    1.31 +    element_name = unicode(element_name, "utf-8")
    1.32 +    attribute_name = unicode(attribute_name, "utf-8")
    1.33      r = path_to_context(context, 0, (element_name, attribute_name))
    1.34      return r.encode("utf-8")
    1.35  
    1.36 @@ -201,6 +206,8 @@
    1.37      """
    1.38  
    1.39      #print "other_list_attributes"
    1.40 +    element_name = unicode(element_name, "utf-8")
    1.41 +    attribute_name = unicode(attribute_name, "utf-8")
    1.42      names = []
    1.43      for node in nodes:
    1.44          name = path_to_node(libxml2dom.Node(node), 0, (element_name, attribute_name), 1)
    1.45 @@ -219,6 +226,7 @@
    1.46      """
    1.47  
    1.48      #print "other_attributes"
    1.49 +    attribute_name = unicode(attribute_name, "utf-8")
    1.50      # NOTE: Cannot directly reference attributes in the nodes list because
    1.51      # NOTE: libxml2dom does not yet support parent element discovery on
    1.52      # NOTE: attributes.
    1.53 @@ -241,6 +249,7 @@
    1.54      template:child-element('comment', 1, template:this-element()) -> '.../comment$1'
    1.55      """
    1.56  
    1.57 +    element_name = unicode(element_name, "utf-8")
    1.58      l = []
    1.59      for node_path in node_paths.split(","):
    1.60          l.append(node_path + Constants.path_separator + element_name
    1.61 @@ -258,6 +267,7 @@
    1.62      template:child-attribute('value', template:this-element()) -> '.../value'
    1.63      """
    1.64  
    1.65 +    attribute_name = unicode(attribute_name, "utf-8")
    1.66      l = []
    1.67      for node_path in node_paths.split(","):
    1.68          l.append(node_path + Constants.path_separator + attribute_name)
    1.69 @@ -267,11 +277,13 @@
    1.70  
    1.71  def multi_field_name(context, multivalue_name):
    1.72      #print "multi_field_name"
    1.73 +    multivalue_name = unicode(multivalue_name, "utf-8")
    1.74      r = path_to_context(context, 1, multivalue_name)
    1.75      return r.encode("utf-8")
    1.76  
    1.77  def other_multi_field_names(context, multivalue_name, nodes):
    1.78      #print "other_multi_field_names"
    1.79 +    multivalue_name = unicode(multivalue_name, "utf-8")
    1.80      names = []
    1.81      for node in nodes:
    1.82          name = path_to_node(libxml2dom.Node(node), 1, multivalue_name, 1)
    1.83 @@ -280,6 +292,26 @@
    1.84      r = ",".join(names)
    1.85      return r.encode("utf-8")
    1.86  
    1.87 +# Utility functions.
    1.88 +
    1.89 +def url_encode(context, nodes, charset="utf-8"):
    1.90 +
    1.91 +    """
    1.92 +    Exposed as {template:url-encode(nodes)}.
    1.93 +    Provides a "URL encoded" string created from the merged textual contents of
    1.94 +    the given 'nodes', with the encoded character values representing characters
    1.95 +    in the optional 'charset' (UTF-8 if not specified).
    1.96 +
    1.97 +    template:url-encode(./text(), 'iso-8859-1')
    1.98 +    """
    1.99 +
   1.100 +    l = []
   1.101 +    for node in nodes:
   1.102 +        s = libxml2dom.Node(node).nodeValue
   1.103 +        l.append(urllib.quote(s.encode("utf-8")).replace("/", "%2F"))
   1.104 +    output = "".join(l)
   1.105 +    return output
   1.106 +
   1.107  # New functions.
   1.108  
   1.109  libxsltmod.xsltRegisterExtModuleFunction("list-attribute", "http://www.boddie.org.uk/ns/xmltools/template", list_attribute)
   1.110 @@ -307,6 +339,10 @@
   1.111  libxsltmod.xsltRegisterExtModuleFunction("multi-field-name", "http://www.boddie.org.uk/ns/xmltools/template", multi_field_name)
   1.112  libxsltmod.xsltRegisterExtModuleFunction("other-multi-field-names", "http://www.boddie.org.uk/ns/xmltools/template", other_multi_field_names)
   1.113  
   1.114 +# Utility functions.
   1.115 +
   1.116 +libxsltmod.xsltRegisterExtModuleFunction("url-encode", "http://www.boddie.org.uk/ns/xmltools/template", url_encode)
   1.117 +
   1.118  def get_field_name(field_or_multi_name):
   1.119      return field_or_multi_name.split(Constants.multi_separator)[0]
   1.120