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