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@1 | 8 | import libxslt |
paulb@49 | 9 | import libxml2 |
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@6 | 45 | if node.type == "attribute": |
paulb@6 | 46 | node = node.parent |
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@23 | 54 | l.insert(0, node.name) |
paulb@23 | 55 | node = node.parent |
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@16 | 59 | l.insert(0, node.name) |
paulb@16 | 60 | l.insert(0, Constants.path_separator) |
paulb@16 | 61 | node = node.parent |
paulb@1 | 62 | # Element references. |
paulb@1 | 63 | while node is not None and node.type != "document_xml": |
paulb@1 | 64 | l.insert(0, str(int(node.xpathEval("count(preceding-sibling::*) + 1")))) |
paulb@16 | 65 | l.insert(0, Constants.pair_separator) |
paulb@1 | 66 | l.insert(0, node.name) |
paulb@16 | 67 | l.insert(0, Constants.path_separator) |
paulb@1 | 68 | node = node.parent |
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@1 | 82 | pctxt = libxslt.xpathParserContext(_obj=context) |
paulb@1 | 83 | context = pctxt.context() |
paulb@1 | 84 | node = context.contextNode() |
paulb@9 | 85 | transform_context = context.transformContext() |
paulb@9 | 86 | name_var = transform_context.variableLookup("this-name", None) |
paulb@23 | 87 | if multivalue_name is not None: |
paulb@23 | 88 | name = multivalue_name |
paulb@23 | 89 | multivalue = 1 |
paulb@23 | 90 | elif name_var is not None: |
paulb@9 | 91 | name = name_var[0].content |
paulb@23 | 92 | multivalue = 0 |
paulb@9 | 93 | else: |
paulb@9 | 94 | name = None |
paulb@23 | 95 | multivalue = 0 |
paulb@23 | 96 | return path_to_node(node, attribute_ref, name, multivalue) |
paulb@16 | 97 | |
paulb@16 | 98 | def this_position(context): |
paulb@16 | 99 | return path_to_context(context, 0) |
paulb@16 | 100 | |
paulb@16 | 101 | def field_name(context): |
paulb@16 | 102 | return path_to_context(context, 1) |
paulb@1 | 103 | |
paulb@23 | 104 | def multi_field_name(context, multivalue_name): |
paulb@23 | 105 | return path_to_context(context, 1, multivalue_name) |
paulb@23 | 106 | |
paulb@34 | 107 | def new_field(context, name): |
paulb@34 | 108 | return path_to_context(context, 0) + "/" + name |
paulb@34 | 109 | |
paulb@57 | 110 | def other_field_names(context, nodes): |
paulb@57 | 111 | names = [] |
paulb@57 | 112 | for node in nodes: |
paulb@57 | 113 | n = libxml2.xmlNode(node) |
paulb@57 | 114 | name = path_to_node(n, 0, None, 0) |
paulb@57 | 115 | if name not in names: |
paulb@57 | 116 | names.append(name) |
paulb@57 | 117 | return ",".join(names) |
paulb@49 | 118 | |
paulb@57 | 119 | def other_multi_field_names(context, multivalue_name, nodes): |
paulb@57 | 120 | names = [] |
paulb@57 | 121 | for node in nodes: |
paulb@57 | 122 | n = libxml2.xmlNode(node) |
paulb@57 | 123 | name = path_to_node(n, 1, multivalue_name, 1) |
paulb@57 | 124 | if name not in names: |
paulb@57 | 125 | names.append(name) |
paulb@57 | 126 | return ",".join(names) |
paulb@49 | 127 | |
paulb@1 | 128 | libxslt.registerExtModuleFunction("this-position", "http://www.boddie.org.uk/ns/xmltools/template", this_position) |
paulb@16 | 129 | libxslt.registerExtModuleFunction("field-name", "http://www.boddie.org.uk/ns/xmltools/template", field_name) |
paulb@23 | 130 | libxslt.registerExtModuleFunction("multi-field-name", "http://www.boddie.org.uk/ns/xmltools/template", multi_field_name) |
paulb@34 | 131 | libxslt.registerExtModuleFunction("new-field", "http://www.boddie.org.uk/ns/xmltools/template", new_field) |
paulb@57 | 132 | libxslt.registerExtModuleFunction("other-field-names", "http://www.boddie.org.uk/ns/xmltools/template", other_field_names) |
paulb@57 | 133 | libxslt.registerExtModuleFunction("other-multi-field-names", "http://www.boddie.org.uk/ns/xmltools/template", other_multi_field_names) |
paulb@49 | 134 | |
paulb@49 | 135 | def get_field_name(field_or_multi_name): |
paulb@49 | 136 | return field_or_multi_name.split(Constants.multi_separator)[0] |
paulb@49 | 137 | |
paulb@49 | 138 | def get_element_path(field_or_multi_name): |
paulb@49 | 139 | |
paulb@49 | 140 | """ |
paulb@49 | 141 | Convert the given 'field_or_multi_name' back to an XPath reference. |
paulb@49 | 142 | For example: |
paulb@57 | 143 | /configuration#1/details#1/base-system##value -> /*[position() = 1]/*[position() = 1]/base-system |
paulb@49 | 144 | """ |
paulb@49 | 145 | |
paulb@49 | 146 | field_name = get_field_name(field_or_multi_name) |
paulb@49 | 147 | parts = field_name.split(Constants.path_separator) |
paulb@49 | 148 | new_parts = [] |
paulb@49 | 149 | for part in parts: |
paulb@49 | 150 | path_parts = part.split(Constants.pair_separator) |
paulb@49 | 151 | if len(path_parts) == 2: |
paulb@57 | 152 | new_parts.append("*[position() = " + path_parts[1] + "]") |
paulb@49 | 153 | else: |
paulb@49 | 154 | new_parts.append(path_parts[0]) |
paulb@49 | 155 | return "/".join(new_parts) |
paulb@1 | 156 | |
paulb@1 | 157 | # vim: tabstop=4 expandtab shiftwidth=4 |