paulb@1 | 1 | #!/usr/bin/env python |
paulb@1 | 2 | |
paulb@1 | 3 | """ |
paulb@1 | 4 | XSL-based form templating. |
paulb@1 | 5 | """ |
paulb@1 | 6 | |
paulb@16 | 7 | import Constants |
paulb@66 | 8 | import libxsltmod, libxml2mod |
paulb@66 | 9 | import libxml2dom |
paulb@1 | 10 | |
paulb@1 | 11 | """ |
paulb@1 | 12 | import libxml2 |
paulb@1 | 13 | |
paulb@1 | 14 | def quiet(context, s): |
paulb@1 | 15 | pass |
paulb@1 | 16 | |
paulb@1 | 17 | libxml2.registerErrorHandler(quiet, None) |
paulb@1 | 18 | """ |
paulb@1 | 19 | |
paulb@23 | 20 | def path_to_node(node, attribute_ref, name, multivalue=0): |
paulb@1 | 21 | |
paulb@9 | 22 | """ |
paulb@16 | 23 | Generate an XSLForms path to the given 'node', producing an attribute |
paulb@16 | 24 | reference if 'attribute_ref' is true; for example: |
paulb@16 | 25 | |
paulb@16 | 26 | /package#1/discriminators#5/discriminator#1/category |
paulb@16 | 27 | |
paulb@16 | 28 | Otherwise an element reference is produced; for example: |
paulb@16 | 29 | |
paulb@16 | 30 | /package#1/discriminators#5/discriminator#1 |
paulb@16 | 31 | |
paulb@16 | 32 | Use the given 'name' to complete the path if an attribute reference is |
paulb@16 | 33 | required (and if a genuine attribute is found at the context node - |
paulb@16 | 34 | otherwise 'name' will be None and the context node will be treated like an |
paulb@16 | 35 | attribute). |
paulb@23 | 36 | |
paulb@23 | 37 | If 'multivalue' is true, produce an attribute reference using the given |
paulb@23 | 38 | 'name' of the following form: |
paulb@23 | 39 | |
paulb@23 | 40 | /package#1/categories#1/category##name |
paulb@9 | 41 | """ |
paulb@1 | 42 | |
paulb@1 | 43 | l = [] |
paulb@6 | 44 | # Skip attribute reference. |
paulb@66 | 45 | if node.nodeType == node.ATTRIBUTE_NODE: |
paulb@66 | 46 | node = node.parentNode |
paulb@9 | 47 | # Manually insert the attribute name if defined. |
paulb@16 | 48 | if attribute_ref: |
paulb@23 | 49 | # A real attribute is referenced. |
paulb@16 | 50 | if name is not None: |
paulb@16 | 51 | l.insert(0, name) |
paulb@23 | 52 | if multivalue: |
paulb@49 | 53 | l.insert(0, Constants.multi_separator) |
paulb@66 | 54 | l.insert(0, node.nodeName) |
paulb@66 | 55 | node = node.parentNode |
paulb@16 | 56 | l.insert(0, Constants.path_separator) |
paulb@23 | 57 | # Otherwise, treat the element name as an attribute name. |
paulb@16 | 58 | else: |
paulb@66 | 59 | l.insert(0, node.nodeName) |
paulb@16 | 60 | l.insert(0, Constants.path_separator) |
paulb@66 | 61 | node = node.parentNode |
paulb@1 | 62 | # Element references. |
paulb@66 | 63 | while node is not None and node.nodeType != node.DOCUMENT_NODE: |
paulb@66 | 64 | l.insert(0, str(int(node.xpath("count(preceding-sibling::*) + 1")))) |
paulb@16 | 65 | l.insert(0, Constants.pair_separator) |
paulb@66 | 66 | l.insert(0, node.nodeName) |
paulb@16 | 67 | l.insert(0, Constants.path_separator) |
paulb@66 | 68 | node = node.parentNode |
paulb@1 | 69 | return "".join(l) |
paulb@1 | 70 | |
paulb@23 | 71 | def path_to_context(context, attribute_ref, multivalue_name=None): |
paulb@1 | 72 | |
paulb@1 | 73 | """ |
paulb@1 | 74 | As a libxslt extension function, return a string containing the XSLForms |
paulb@9 | 75 | path to the 'context' node, using the special "this-name" variable to |
paulb@16 | 76 | complete the path if an attribute reference is required (as indicated by |
paulb@23 | 77 | 'attribute_ref' being set to true). If 'multivalue_name' is set, produce a |
paulb@23 | 78 | reference to a multivalued field using the given string as the attribute |
paulb@23 | 79 | name. |
paulb@1 | 80 | """ |
paulb@1 | 81 | |
paulb@66 | 82 | context = libxml2mod.xmlXPathParserGetContext(context) |
paulb@66 | 83 | transform_context = libxsltmod.xsltXPathGetTransformContext(context) |
paulb@66 | 84 | name_var = libxsltmod.xsltVariableLookup(transform_context, "this-name", None) |
paulb@23 | 85 | if multivalue_name is not None: |
paulb@23 | 86 | name = multivalue_name |
paulb@23 | 87 | multivalue = 1 |
paulb@23 | 88 | elif name_var is not None: |
paulb@67 | 89 | name = libxml2mod.xmlNodeGetContent(name_var[0]) |
paulb@23 | 90 | multivalue = 0 |
paulb@9 | 91 | else: |
paulb@9 | 92 | name = None |
paulb@23 | 93 | multivalue = 0 |
paulb@66 | 94 | node = libxml2dom.Node(libxml2mod.xmlXPathGetContextNode(context)) |
paulb@23 | 95 | return path_to_node(node, attribute_ref, name, multivalue) |
paulb@16 | 96 | |
paulb@16 | 97 | def this_position(context): |
paulb@67 | 98 | #print "this_position" |
paulb@66 | 99 | r = path_to_context(context, 0) |
paulb@66 | 100 | return r.encode("utf-8") |
paulb@16 | 101 | |
paulb@16 | 102 | def field_name(context): |
paulb@67 | 103 | #print "field_name" |
paulb@66 | 104 | r = path_to_context(context, 1) |
paulb@66 | 105 | return r.encode("utf-8") |
paulb@1 | 106 | |
paulb@23 | 107 | def multi_field_name(context, multivalue_name): |
paulb@67 | 108 | #print "multi_field_name" |
paulb@66 | 109 | r = path_to_context(context, 1, multivalue_name) |
paulb@66 | 110 | return r.encode("utf-8") |
paulb@23 | 111 | |
paulb@34 | 112 | def new_field(context, name): |
paulb@67 | 113 | #print "new_field" |
paulb@66 | 114 | r = path_to_context(context, 0) + "/" + name |
paulb@66 | 115 | return r.encode("utf-8") |
paulb@34 | 116 | |
paulb@57 | 117 | def other_field_names(context, nodes): |
paulb@67 | 118 | #print "other_field_names" |
paulb@57 | 119 | names = [] |
paulb@57 | 120 | for node in nodes: |
paulb@66 | 121 | name = path_to_node(libxml2dom.Node(node), 0, None, 0) |
paulb@57 | 122 | if name not in names: |
paulb@57 | 123 | names.append(name) |
paulb@66 | 124 | r = ",".join(names) |
paulb@66 | 125 | return r.encode("utf-8") |
paulb@49 | 126 | |
paulb@57 | 127 | def other_multi_field_names(context, multivalue_name, nodes): |
paulb@67 | 128 | #print "other_multi_field_names" |
paulb@57 | 129 | names = [] |
paulb@57 | 130 | for node in nodes: |
paulb@66 | 131 | name = path_to_node(libxml2dom.Node(node), 1, multivalue_name, 1) |
paulb@57 | 132 | if name not in names: |
paulb@57 | 133 | names.append(name) |
paulb@66 | 134 | r = ",".join(names) |
paulb@66 | 135 | return r.encode("utf-8") |
paulb@49 | 136 | |
paulb@66 | 137 | libxsltmod.xsltRegisterExtModuleFunction("this-position", "http://www.boddie.org.uk/ns/xmltools/template", this_position) |
paulb@66 | 138 | libxsltmod.xsltRegisterExtModuleFunction("field-name", "http://www.boddie.org.uk/ns/xmltools/template", field_name) |
paulb@66 | 139 | libxsltmod.xsltRegisterExtModuleFunction("multi-field-name", "http://www.boddie.org.uk/ns/xmltools/template", multi_field_name) |
paulb@66 | 140 | libxsltmod.xsltRegisterExtModuleFunction("new-field", "http://www.boddie.org.uk/ns/xmltools/template", new_field) |
paulb@66 | 141 | libxsltmod.xsltRegisterExtModuleFunction("other-field-names", "http://www.boddie.org.uk/ns/xmltools/template", other_field_names) |
paulb@66 | 142 | libxsltmod.xsltRegisterExtModuleFunction("other-multi-field-names", "http://www.boddie.org.uk/ns/xmltools/template", other_multi_field_names) |
paulb@49 | 143 | |
paulb@49 | 144 | def get_field_name(field_or_multi_name): |
paulb@49 | 145 | return field_or_multi_name.split(Constants.multi_separator)[0] |
paulb@49 | 146 | |
paulb@49 | 147 | def get_element_path(field_or_multi_name): |
paulb@49 | 148 | |
paulb@49 | 149 | """ |
paulb@49 | 150 | Convert the given 'field_or_multi_name' back to an XPath reference. |
paulb@49 | 151 | For example: |
paulb@57 | 152 | /configuration#1/details#1/base-system##value -> /*[position() = 1]/*[position() = 1]/base-system |
paulb@49 | 153 | """ |
paulb@49 | 154 | |
paulb@49 | 155 | field_name = get_field_name(field_or_multi_name) |
paulb@49 | 156 | parts = field_name.split(Constants.path_separator) |
paulb@49 | 157 | new_parts = [] |
paulb@49 | 158 | for part in parts: |
paulb@49 | 159 | path_parts = part.split(Constants.pair_separator) |
paulb@49 | 160 | if len(path_parts) == 2: |
paulb@57 | 161 | new_parts.append("*[position() = " + path_parts[1] + "]") |
paulb@49 | 162 | else: |
paulb@49 | 163 | new_parts.append(path_parts[0]) |
paulb@49 | 164 | return "/".join(new_parts) |
paulb@1 | 165 | |
paulb@1 | 166 | # vim: tabstop=4 expandtab shiftwidth=4 |