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@178 | 127 | # Exposed extension functions. |
paulb@178 | 128 | |
paulb@178 | 129 | def this_element(context): |
paulb@178 | 130 | |
paulb@178 | 131 | """ |
paulb@178 | 132 | Exposed as {template:this-element()}. |
paulb@178 | 133 | Provides a reference to the current element in the form data structure. |
paulb@178 | 134 | """ |
paulb@178 | 135 | |
paulb@178 | 136 | #print "this_element" |
paulb@66 | 137 | r = path_to_context(context, 0) |
paulb@66 | 138 | return r.encode("utf-8") |
paulb@16 | 139 | |
paulb@178 | 140 | def this_attribute(context): |
paulb@178 | 141 | |
paulb@178 | 142 | """ |
paulb@178 | 143 | Exposed as {template:this-attribute()}. |
paulb@178 | 144 | Provides a reference to the current attribute in the form data structure. |
paulb@178 | 145 | """ |
paulb@178 | 146 | |
paulb@178 | 147 | #print "this_attribute" |
paulb@66 | 148 | r = path_to_context(context, 1) |
paulb@66 | 149 | return r.encode("utf-8") |
paulb@1 | 150 | |
paulb@178 | 151 | def new_attribute(context, name): |
paulb@23 | 152 | |
paulb@178 | 153 | """ |
paulb@178 | 154 | Exposed as {template:new-attribute(name)}. |
paulb@178 | 155 | Provides a reference to a new attribute of the given 'name' on the current |
paulb@178 | 156 | element in the form data structure. |
paulb@178 | 157 | """ |
paulb@178 | 158 | |
paulb@178 | 159 | #print "new_attribute" |
paulb@66 | 160 | r = path_to_context(context, 0) + "/" + name |
paulb@66 | 161 | return r.encode("utf-8") |
paulb@34 | 162 | |
paulb@178 | 163 | def other_elements(context, nodes): |
paulb@178 | 164 | |
paulb@178 | 165 | """ |
paulb@178 | 166 | Exposed as {template:other-elements(nodes)}. |
paulb@178 | 167 | Provides a reference to other elements in the form data structure according |
paulb@178 | 168 | to the specified 'nodes' parameter (an XPath expression in the template). |
paulb@178 | 169 | """ |
paulb@178 | 170 | |
paulb@178 | 171 | #print "other_elements" |
paulb@57 | 172 | names = [] |
paulb@57 | 173 | for node in nodes: |
paulb@66 | 174 | name = path_to_node(libxml2dom.Node(node), 0, None, 0) |
paulb@57 | 175 | if name not in names: |
paulb@57 | 176 | names.append(name) |
paulb@66 | 177 | r = ",".join(names) |
paulb@66 | 178 | return r.encode("utf-8") |
paulb@49 | 179 | |
paulb@178 | 180 | def list_attribute(context, element_name, attribute_name): |
paulb@178 | 181 | |
paulb@178 | 182 | """ |
paulb@178 | 183 | Exposed as {template:list-attribute(element_name, attribute_name)}. |
paulb@178 | 184 | Provides a reference to one or many elements of the given 'element_name' |
paulb@178 | 185 | found under the current element in the form data structure having |
paulb@178 | 186 | attributes with the given 'attribute_name'. |
paulb@178 | 187 | """ |
paulb@178 | 188 | |
paulb@178 | 189 | #print "list_attribute" |
paulb@178 | 190 | r = path_to_context(context, 0, (element_name, attribute_name)) |
paulb@178 | 191 | return r.encode("utf-8") |
paulb@178 | 192 | |
paulb@178 | 193 | def other_list_attributes(context, element_name, attribute_name, nodes): |
paulb@178 | 194 | |
paulb@178 | 195 | """ |
paulb@178 | 196 | Exposed as {template:other-list-attributes(element_name, attribute_name, nodes)}. |
paulb@178 | 197 | Provides a reference to other elements in the form data structure, found |
paulb@178 | 198 | under the specified 'nodes' (described using an XPath expression in the |
paulb@178 | 199 | template) having the given 'element_name' and bearing attributes of the |
paulb@178 | 200 | given 'attribute_name'. |
paulb@178 | 201 | """ |
paulb@178 | 202 | |
paulb@178 | 203 | #print "other_list_attributes" |
paulb@178 | 204 | names = [] |
paulb@178 | 205 | for node in nodes: |
paulb@178 | 206 | name = path_to_node(libxml2dom.Node(node), 0, (element_name, attribute_name), 1) |
paulb@178 | 207 | if name not in names: |
paulb@178 | 208 | names.append(name) |
paulb@178 | 209 | r = ",".join(names) |
paulb@178 | 210 | return r.encode("utf-8") |
paulb@178 | 211 | |
paulb@178 | 212 | def other_attributes(context, attribute_name, nodes): |
paulb@178 | 213 | |
paulb@178 | 214 | """ |
paulb@178 | 215 | Exposed as {template:other-attributes(name, nodes)}. |
paulb@178 | 216 | Provides a reference to attributes in the form data structure of the given |
paulb@178 | 217 | 'attribute_name' residing on the specified 'nodes' (described using an XPath |
paulb@178 | 218 | expression in the template). |
paulb@178 | 219 | """ |
paulb@178 | 220 | |
paulb@178 | 221 | #print "other_attributes" |
paulb@178 | 222 | # NOTE: Cannot directly reference attributes in the nodes list because |
paulb@178 | 223 | # NOTE: libxml2dom does not yet support parent element discovery on |
paulb@178 | 224 | # NOTE: attributes. |
paulb@178 | 225 | names = [] |
paulb@178 | 226 | for node in nodes: |
paulb@178 | 227 | name = path_to_node(libxml2dom.Node(node), 1, attribute_name, 0) |
paulb@178 | 228 | if name not in names: |
paulb@178 | 229 | names.append(name) |
paulb@178 | 230 | r = ",".join(names) |
paulb@178 | 231 | return r.encode("utf-8") |
paulb@178 | 232 | |
paulb@178 | 233 | def child_element(context, element_name, position, node_paths): |
paulb@178 | 234 | |
paulb@178 | 235 | """ |
paulb@178 | 236 | Exposed as {template:child-element(element_name, position, node_paths)}. |
paulb@178 | 237 | Provides relative paths to the specifed 'element_name', having the given |
paulb@178 | 238 | 'position' (1-based) under each element specified in 'node_paths' (provided |
paulb@178 | 239 | by calls to other extension functions in the template). For example: |
paulb@178 | 240 | |
paulb@178 | 241 | template:child-element('comment', 1, template:this-element()) -> '.../comment$1' |
paulb@178 | 242 | """ |
paulb@178 | 243 | |
paulb@178 | 244 | l = [] |
paulb@178 | 245 | for node_path in node_paths.split(","): |
paulb@178 | 246 | l.append(node_path + Constants.path_separator + element_name |
paulb@178 | 247 | + Constants.pair_separator + str(int(position))) |
paulb@178 | 248 | return ",".join(l).encode("utf-8") |
paulb@178 | 249 | |
paulb@178 | 250 | def child_attribute(context, attribute_name, node_paths): |
paulb@178 | 251 | |
paulb@178 | 252 | """ |
paulb@178 | 253 | Exposed as {template:child-attribute(attribute_name, node_paths)}. |
paulb@178 | 254 | Provides a relative path to the specifed 'attribute_name' for each element |
paulb@178 | 255 | specified in 'node_paths' (provided by calls to other extension functions in |
paulb@178 | 256 | the template). For example: |
paulb@178 | 257 | |
paulb@178 | 258 | template:child-attribute('value', template:this-element()) -> '.../value' |
paulb@178 | 259 | """ |
paulb@178 | 260 | |
paulb@178 | 261 | l = [] |
paulb@178 | 262 | for node_path in node_paths.split(","): |
paulb@178 | 263 | l.append(node_path + Constants.path_separator + attribute_name) |
paulb@178 | 264 | return ",".join(l).encode("utf-8") |
paulb@178 | 265 | |
paulb@178 | 266 | # Old implementations. |
paulb@178 | 267 | |
paulb@178 | 268 | def multi_field_name(context, multivalue_name): |
paulb@178 | 269 | #print "multi_field_name" |
paulb@178 | 270 | r = path_to_context(context, 1, multivalue_name) |
paulb@178 | 271 | return r.encode("utf-8") |
paulb@178 | 272 | |
paulb@57 | 273 | def other_multi_field_names(context, multivalue_name, nodes): |
paulb@67 | 274 | #print "other_multi_field_names" |
paulb@57 | 275 | names = [] |
paulb@57 | 276 | for node in nodes: |
paulb@66 | 277 | name = path_to_node(libxml2dom.Node(node), 1, multivalue_name, 1) |
paulb@57 | 278 | if name not in names: |
paulb@57 | 279 | names.append(name) |
paulb@66 | 280 | r = ",".join(names) |
paulb@66 | 281 | return r.encode("utf-8") |
paulb@49 | 282 | |
paulb@156 | 283 | # New functions. |
paulb@156 | 284 | |
paulb@156 | 285 | libxsltmod.xsltRegisterExtModuleFunction("list-attribute", "http://www.boddie.org.uk/ns/xmltools/template", list_attribute) |
paulb@156 | 286 | libxsltmod.xsltRegisterExtModuleFunction("other-list-attributes", "http://www.boddie.org.uk/ns/xmltools/template", other_list_attributes) |
paulb@156 | 287 | libxsltmod.xsltRegisterExtModuleFunction("other-attributes", "http://www.boddie.org.uk/ns/xmltools/template", other_attributes) |
paulb@178 | 288 | libxsltmod.xsltRegisterExtModuleFunction("child-element", "http://www.boddie.org.uk/ns/xmltools/template", child_element) |
paulb@178 | 289 | libxsltmod.xsltRegisterExtModuleFunction("child-attribute", "http://www.boddie.org.uk/ns/xmltools/template", child_attribute) |
paulb@156 | 290 | |
paulb@144 | 291 | # New names. |
paulb@144 | 292 | |
paulb@178 | 293 | libxsltmod.xsltRegisterExtModuleFunction("this-element", "http://www.boddie.org.uk/ns/xmltools/template", this_element) |
paulb@178 | 294 | libxsltmod.xsltRegisterExtModuleFunction("this-attribute", "http://www.boddie.org.uk/ns/xmltools/template", this_attribute) |
paulb@178 | 295 | libxsltmod.xsltRegisterExtModuleFunction("new-attribute", "http://www.boddie.org.uk/ns/xmltools/template", new_attribute) |
paulb@178 | 296 | libxsltmod.xsltRegisterExtModuleFunction("other-elements", "http://www.boddie.org.uk/ns/xmltools/template", other_elements) |
paulb@144 | 297 | |
paulb@144 | 298 | # Old names. |
paulb@144 | 299 | |
paulb@178 | 300 | libxsltmod.xsltRegisterExtModuleFunction("this-position", "http://www.boddie.org.uk/ns/xmltools/template", this_element) |
paulb@178 | 301 | libxsltmod.xsltRegisterExtModuleFunction("field-name", "http://www.boddie.org.uk/ns/xmltools/template", this_attribute) |
paulb@178 | 302 | libxsltmod.xsltRegisterExtModuleFunction("new-field", "http://www.boddie.org.uk/ns/xmltools/template", new_attribute) |
paulb@178 | 303 | libxsltmod.xsltRegisterExtModuleFunction("other-field-names", "http://www.boddie.org.uk/ns/xmltools/template", other_elements) |
paulb@178 | 304 | |
paulb@178 | 305 | # Old functions. |
paulb@178 | 306 | |
paulb@66 | 307 | libxsltmod.xsltRegisterExtModuleFunction("multi-field-name", "http://www.boddie.org.uk/ns/xmltools/template", multi_field_name) |
paulb@66 | 308 | libxsltmod.xsltRegisterExtModuleFunction("other-multi-field-names", "http://www.boddie.org.uk/ns/xmltools/template", other_multi_field_names) |
paulb@49 | 309 | |
paulb@49 | 310 | def get_field_name(field_or_multi_name): |
paulb@49 | 311 | return field_or_multi_name.split(Constants.multi_separator)[0] |
paulb@49 | 312 | |
paulb@49 | 313 | def get_element_path(field_or_multi_name): |
paulb@49 | 314 | |
paulb@49 | 315 | """ |
paulb@49 | 316 | Convert the given 'field_or_multi_name' back to an XPath reference. |
paulb@49 | 317 | For example: |
paulb@139 | 318 | /configuration$1/details$1/base-system$$value -> /*[position() = 1]/*[position() = 1]/base-system |
paulb@49 | 319 | """ |
paulb@49 | 320 | |
paulb@49 | 321 | field_name = get_field_name(field_or_multi_name) |
paulb@49 | 322 | parts = field_name.split(Constants.path_separator) |
paulb@49 | 323 | new_parts = [] |
paulb@49 | 324 | for part in parts: |
paulb@49 | 325 | path_parts = part.split(Constants.pair_separator) |
paulb@49 | 326 | if len(path_parts) == 2: |
paulb@57 | 327 | new_parts.append("*[position() = " + path_parts[1] + "]") |
paulb@49 | 328 | else: |
paulb@49 | 329 | new_parts.append(path_parts[0]) |
paulb@49 | 330 | return "/".join(new_parts) |
paulb@1 | 331 | |
paulb@1 | 332 | # vim: tabstop=4 expandtab shiftwidth=4 |