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@156 | 53 | If 'multivalue' is true and 'attribute_ref' is set, produce an attribute |
paulb@156 | 54 | reference using the given 'name': |
paulb@23 | 55 | |
paulb@139 | 56 | /package$1/categories$1/category$$name |
paulb@156 | 57 | |
paulb@156 | 58 | If 'multivalue' is true and 'attribute_ref' is not set, produce an attribute |
paulb@156 | 59 | reference using the given 'name' of form (element, attribute): |
paulb@156 | 60 | |
paulb@156 | 61 | /package$1/categories$1/element$$attribute |
paulb@9 | 62 | """ |
paulb@1 | 63 | |
paulb@1 | 64 | l = [] |
paulb@6 | 65 | # Skip attribute reference. |
paulb@66 | 66 | if node.nodeType == node.ATTRIBUTE_NODE: |
paulb@66 | 67 | node = node.parentNode |
paulb@9 | 68 | # Manually insert the attribute name if defined. |
paulb@16 | 69 | if attribute_ref: |
paulb@23 | 70 | # A real attribute is referenced. |
paulb@16 | 71 | if name is not None: |
paulb@16 | 72 | l.insert(0, name) |
paulb@23 | 73 | if multivalue: |
paulb@49 | 74 | l.insert(0, Constants.multi_separator) |
paulb@66 | 75 | l.insert(0, node.nodeName) |
paulb@66 | 76 | node = node.parentNode |
paulb@16 | 77 | l.insert(0, Constants.path_separator) |
paulb@23 | 78 | # Otherwise, treat the element name as an attribute name. |
paulb@156 | 79 | # NOTE: Not sure how useful this is. |
paulb@16 | 80 | else: |
paulb@66 | 81 | l.insert(0, node.nodeName) |
paulb@16 | 82 | l.insert(0, Constants.path_separator) |
paulb@66 | 83 | node = node.parentNode |
paulb@156 | 84 | # Otherwise insert any multivalue references (eg. list-attribute). |
paulb@156 | 85 | elif multivalue: |
paulb@156 | 86 | element_name, attribute_name = name |
paulb@156 | 87 | l.insert(0, attribute_name) |
paulb@156 | 88 | l.insert(0, Constants.multi_separator) |
paulb@156 | 89 | l.insert(0, element_name) |
paulb@156 | 90 | l.insert(0, Constants.path_separator) |
paulb@156 | 91 | |
paulb@1 | 92 | # Element references. |
paulb@66 | 93 | while node is not None and node.nodeType != node.DOCUMENT_NODE: |
paulb@66 | 94 | l.insert(0, str(int(node.xpath("count(preceding-sibling::*) + 1")))) |
paulb@16 | 95 | l.insert(0, Constants.pair_separator) |
paulb@66 | 96 | l.insert(0, node.nodeName) |
paulb@16 | 97 | l.insert(0, Constants.path_separator) |
paulb@66 | 98 | node = node.parentNode |
paulb@1 | 99 | return "".join(l) |
paulb@1 | 100 | |
paulb@23 | 101 | def path_to_context(context, attribute_ref, multivalue_name=None): |
paulb@1 | 102 | |
paulb@1 | 103 | """ |
paulb@1 | 104 | As a libxslt extension function, return a string containing the XSLForms |
paulb@9 | 105 | path to the 'context' node, using the special "this-name" variable to |
paulb@16 | 106 | complete the path if an attribute reference is required (as indicated by |
paulb@23 | 107 | 'attribute_ref' being set to true). If 'multivalue_name' is set, produce a |
paulb@23 | 108 | reference to a multivalued field using the given string as the attribute |
paulb@23 | 109 | name. |
paulb@1 | 110 | """ |
paulb@1 | 111 | |
paulb@66 | 112 | context = libxml2mod.xmlXPathParserGetContext(context) |
paulb@66 | 113 | transform_context = libxsltmod.xsltXPathGetTransformContext(context) |
paulb@66 | 114 | name_var = libxsltmod.xsltVariableLookup(transform_context, "this-name", None) |
paulb@23 | 115 | if multivalue_name is not None: |
paulb@23 | 116 | name = multivalue_name |
paulb@23 | 117 | multivalue = 1 |
paulb@23 | 118 | elif name_var is not None: |
paulb@67 | 119 | name = libxml2mod.xmlNodeGetContent(name_var[0]) |
paulb@23 | 120 | multivalue = 0 |
paulb@9 | 121 | else: |
paulb@9 | 122 | name = None |
paulb@23 | 123 | multivalue = 0 |
paulb@66 | 124 | node = libxml2dom.Node(libxml2mod.xmlXPathGetContextNode(context)) |
paulb@23 | 125 | return path_to_node(node, attribute_ref, name, multivalue) |
paulb@16 | 126 | |
paulb@16 | 127 | def this_position(context): |
paulb@67 | 128 | #print "this_position" |
paulb@66 | 129 | r = path_to_context(context, 0) |
paulb@66 | 130 | return r.encode("utf-8") |
paulb@16 | 131 | |
paulb@16 | 132 | def field_name(context): |
paulb@67 | 133 | #print "field_name" |
paulb@66 | 134 | r = path_to_context(context, 1) |
paulb@66 | 135 | return r.encode("utf-8") |
paulb@1 | 136 | |
paulb@23 | 137 | def multi_field_name(context, multivalue_name): |
paulb@67 | 138 | #print "multi_field_name" |
paulb@66 | 139 | r = path_to_context(context, 1, multivalue_name) |
paulb@66 | 140 | return r.encode("utf-8") |
paulb@23 | 141 | |
paulb@34 | 142 | def new_field(context, name): |
paulb@67 | 143 | #print "new_field" |
paulb@66 | 144 | r = path_to_context(context, 0) + "/" + name |
paulb@66 | 145 | return r.encode("utf-8") |
paulb@34 | 146 | |
paulb@57 | 147 | def other_field_names(context, nodes): |
paulb@67 | 148 | #print "other_field_names" |
paulb@57 | 149 | names = [] |
paulb@57 | 150 | for node in nodes: |
paulb@66 | 151 | name = path_to_node(libxml2dom.Node(node), 0, None, 0) |
paulb@57 | 152 | if name not in names: |
paulb@57 | 153 | names.append(name) |
paulb@66 | 154 | r = ",".join(names) |
paulb@66 | 155 | return r.encode("utf-8") |
paulb@49 | 156 | |
paulb@57 | 157 | def other_multi_field_names(context, multivalue_name, nodes): |
paulb@67 | 158 | #print "other_multi_field_names" |
paulb@57 | 159 | names = [] |
paulb@57 | 160 | for node in nodes: |
paulb@66 | 161 | name = path_to_node(libxml2dom.Node(node), 1, multivalue_name, 1) |
paulb@57 | 162 | if name not in names: |
paulb@57 | 163 | names.append(name) |
paulb@66 | 164 | r = ",".join(names) |
paulb@66 | 165 | return r.encode("utf-8") |
paulb@49 | 166 | |
paulb@156 | 167 | # New implementations. |
paulb@156 | 168 | |
paulb@156 | 169 | def list_attribute(context, element_name, attribute_name): |
paulb@156 | 170 | #print "list_attribute" |
paulb@156 | 171 | r = path_to_context(context, 0, (element_name, attribute_name)) |
paulb@156 | 172 | return r.encode("utf-8") |
paulb@156 | 173 | |
paulb@156 | 174 | def other_list_attributes(context, element_name, attribute_name, nodes): |
paulb@156 | 175 | #print "other_list_attributes" |
paulb@156 | 176 | names = [] |
paulb@156 | 177 | for node in nodes: |
paulb@156 | 178 | name = path_to_node(libxml2dom.Node(node), 0, (element_name, attribute_name), 1) |
paulb@156 | 179 | if name not in names: |
paulb@156 | 180 | names.append(name) |
paulb@156 | 181 | r = ",".join(names) |
paulb@156 | 182 | return r.encode("utf-8") |
paulb@156 | 183 | |
paulb@156 | 184 | def other_attributes(context, attribute_name, nodes): |
paulb@156 | 185 | #print "other_attributes" |
paulb@156 | 186 | # NOTE: Cannot directly reference attributes in the nodes list because |
paulb@156 | 187 | # NOTE: libxml2dom does not yet support parent element discovery on |
paulb@156 | 188 | # NOTE: attributes. |
paulb@156 | 189 | names = [] |
paulb@156 | 190 | for node in nodes: |
paulb@156 | 191 | name = path_to_node(libxml2dom.Node(node), 1, attribute_name, 0) |
paulb@156 | 192 | if name not in names: |
paulb@156 | 193 | names.append(name) |
paulb@156 | 194 | r = ",".join(names) |
paulb@156 | 195 | return r.encode("utf-8") |
paulb@156 | 196 | |
paulb@156 | 197 | # New functions. |
paulb@156 | 198 | |
paulb@156 | 199 | libxsltmod.xsltRegisterExtModuleFunction("list-attribute", "http://www.boddie.org.uk/ns/xmltools/template", list_attribute) |
paulb@156 | 200 | libxsltmod.xsltRegisterExtModuleFunction("other-list-attributes", "http://www.boddie.org.uk/ns/xmltools/template", other_list_attributes) |
paulb@156 | 201 | libxsltmod.xsltRegisterExtModuleFunction("other-attributes", "http://www.boddie.org.uk/ns/xmltools/template", other_attributes) |
paulb@156 | 202 | |
paulb@144 | 203 | # New names. |
paulb@144 | 204 | |
paulb@144 | 205 | libxsltmod.xsltRegisterExtModuleFunction("this-element", "http://www.boddie.org.uk/ns/xmltools/template", this_position) |
paulb@144 | 206 | libxsltmod.xsltRegisterExtModuleFunction("this-attribute", "http://www.boddie.org.uk/ns/xmltools/template", field_name) |
paulb@144 | 207 | libxsltmod.xsltRegisterExtModuleFunction("new-attribute", "http://www.boddie.org.uk/ns/xmltools/template", new_field) |
paulb@156 | 208 | libxsltmod.xsltRegisterExtModuleFunction("other-elements", "http://www.boddie.org.uk/ns/xmltools/template", other_field_names) |
paulb@144 | 209 | |
paulb@144 | 210 | # Old names. |
paulb@144 | 211 | |
paulb@66 | 212 | libxsltmod.xsltRegisterExtModuleFunction("this-position", "http://www.boddie.org.uk/ns/xmltools/template", this_position) |
paulb@66 | 213 | libxsltmod.xsltRegisterExtModuleFunction("field-name", "http://www.boddie.org.uk/ns/xmltools/template", field_name) |
paulb@66 | 214 | libxsltmod.xsltRegisterExtModuleFunction("multi-field-name", "http://www.boddie.org.uk/ns/xmltools/template", multi_field_name) |
paulb@66 | 215 | libxsltmod.xsltRegisterExtModuleFunction("new-field", "http://www.boddie.org.uk/ns/xmltools/template", new_field) |
paulb@66 | 216 | libxsltmod.xsltRegisterExtModuleFunction("other-field-names", "http://www.boddie.org.uk/ns/xmltools/template", other_field_names) |
paulb@66 | 217 | libxsltmod.xsltRegisterExtModuleFunction("other-multi-field-names", "http://www.boddie.org.uk/ns/xmltools/template", other_multi_field_names) |
paulb@49 | 218 | |
paulb@49 | 219 | def get_field_name(field_or_multi_name): |
paulb@49 | 220 | return field_or_multi_name.split(Constants.multi_separator)[0] |
paulb@49 | 221 | |
paulb@49 | 222 | def get_element_path(field_or_multi_name): |
paulb@49 | 223 | |
paulb@49 | 224 | """ |
paulb@49 | 225 | Convert the given 'field_or_multi_name' back to an XPath reference. |
paulb@49 | 226 | For example: |
paulb@139 | 227 | /configuration$1/details$1/base-system$$value -> /*[position() = 1]/*[position() = 1]/base-system |
paulb@49 | 228 | """ |
paulb@49 | 229 | |
paulb@49 | 230 | field_name = get_field_name(field_or_multi_name) |
paulb@49 | 231 | parts = field_name.split(Constants.path_separator) |
paulb@49 | 232 | new_parts = [] |
paulb@49 | 233 | for part in parts: |
paulb@49 | 234 | path_parts = part.split(Constants.pair_separator) |
paulb@49 | 235 | if len(path_parts) == 2: |
paulb@57 | 236 | new_parts.append("*[position() = " + path_parts[1] + "]") |
paulb@49 | 237 | else: |
paulb@49 | 238 | new_parts.append(path_parts[0]) |
paulb@49 | 239 | return "/".join(new_parts) |
paulb@1 | 240 | |
paulb@1 | 241 | # vim: tabstop=4 expandtab shiftwidth=4 |