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@213 | 26 | import urllib |
paulb@1 | 27 | |
paulb@1 | 28 | """ |
paulb@1 | 29 | import libxml2 |
paulb@1 | 30 | |
paulb@1 | 31 | def quiet(context, s): |
paulb@1 | 32 | pass |
paulb@1 | 33 | |
paulb@1 | 34 | libxml2.registerErrorHandler(quiet, None) |
paulb@1 | 35 | """ |
paulb@1 | 36 | |
paulb@23 | 37 | def path_to_node(node, attribute_ref, name, multivalue=0): |
paulb@1 | 38 | |
paulb@9 | 39 | """ |
paulb@16 | 40 | Generate an XSLForms path to the given 'node', producing an attribute |
paulb@16 | 41 | reference if 'attribute_ref' is true; for example: |
paulb@16 | 42 | |
paulb@139 | 43 | /package$1/discriminators$5/discriminator$1/category |
paulb@16 | 44 | |
paulb@16 | 45 | Otherwise an element reference is produced; for example: |
paulb@16 | 46 | |
paulb@139 | 47 | /package$1/discriminators$5/discriminator$1 |
paulb@16 | 48 | |
paulb@16 | 49 | Use the given 'name' to complete the path if an attribute reference is |
paulb@16 | 50 | required (and if a genuine attribute is found at the context node - |
paulb@16 | 51 | otherwise 'name' will be None and the context node will be treated like an |
paulb@16 | 52 | attribute). |
paulb@23 | 53 | |
paulb@156 | 54 | If 'multivalue' is true and 'attribute_ref' is set, produce an attribute |
paulb@156 | 55 | reference using the given 'name': |
paulb@23 | 56 | |
paulb@139 | 57 | /package$1/categories$1/category$$name |
paulb@156 | 58 | |
paulb@156 | 59 | If 'multivalue' is true and 'attribute_ref' is not set, produce an attribute |
paulb@156 | 60 | reference using the given 'name' of form (element, attribute): |
paulb@156 | 61 | |
paulb@156 | 62 | /package$1/categories$1/element$$attribute |
paulb@9 | 63 | """ |
paulb@1 | 64 | |
paulb@1 | 65 | l = [] |
paulb@6 | 66 | # Skip attribute reference. |
paulb@66 | 67 | if node.nodeType == node.ATTRIBUTE_NODE: |
paulb@66 | 68 | node = node.parentNode |
paulb@9 | 69 | # Manually insert the attribute name if defined. |
paulb@16 | 70 | if attribute_ref: |
paulb@23 | 71 | # A real attribute is referenced. |
paulb@16 | 72 | if name is not None: |
paulb@16 | 73 | l.insert(0, name) |
paulb@23 | 74 | if multivalue: |
paulb@49 | 75 | l.insert(0, Constants.multi_separator) |
paulb@66 | 76 | l.insert(0, node.nodeName) |
paulb@66 | 77 | node = node.parentNode |
paulb@16 | 78 | l.insert(0, Constants.path_separator) |
paulb@23 | 79 | # Otherwise, treat the element name as an attribute name. |
paulb@156 | 80 | # NOTE: Not sure how useful this is. |
paulb@16 | 81 | else: |
paulb@66 | 82 | l.insert(0, node.nodeName) |
paulb@16 | 83 | l.insert(0, Constants.path_separator) |
paulb@66 | 84 | node = node.parentNode |
paulb@156 | 85 | # Otherwise insert any multivalue references (eg. list-attribute). |
paulb@156 | 86 | elif multivalue: |
paulb@156 | 87 | element_name, attribute_name = name |
paulb@156 | 88 | l.insert(0, attribute_name) |
paulb@156 | 89 | l.insert(0, Constants.multi_separator) |
paulb@156 | 90 | l.insert(0, element_name) |
paulb@156 | 91 | l.insert(0, Constants.path_separator) |
paulb@156 | 92 | |
paulb@1 | 93 | # Element references. |
paulb@66 | 94 | while node is not None and node.nodeType != node.DOCUMENT_NODE: |
paulb@66 | 95 | l.insert(0, str(int(node.xpath("count(preceding-sibling::*) + 1")))) |
paulb@16 | 96 | l.insert(0, Constants.pair_separator) |
paulb@66 | 97 | l.insert(0, node.nodeName) |
paulb@16 | 98 | l.insert(0, Constants.path_separator) |
paulb@66 | 99 | node = node.parentNode |
paulb@1 | 100 | return "".join(l) |
paulb@1 | 101 | |
paulb@23 | 102 | def path_to_context(context, attribute_ref, multivalue_name=None): |
paulb@1 | 103 | |
paulb@1 | 104 | """ |
paulb@1 | 105 | As a libxslt extension function, return a string containing the XSLForms |
paulb@9 | 106 | path to the 'context' node, using the special "this-name" variable to |
paulb@16 | 107 | complete the path if an attribute reference is required (as indicated by |
paulb@23 | 108 | 'attribute_ref' being set to true). If 'multivalue_name' is set, produce a |
paulb@23 | 109 | reference to a multivalued field using the given string as the attribute |
paulb@23 | 110 | name. |
paulb@1 | 111 | """ |
paulb@1 | 112 | |
paulb@66 | 113 | context = libxml2mod.xmlXPathParserGetContext(context) |
paulb@66 | 114 | transform_context = libxsltmod.xsltXPathGetTransformContext(context) |
paulb@66 | 115 | name_var = libxsltmod.xsltVariableLookup(transform_context, "this-name", None) |
paulb@23 | 116 | if multivalue_name is not None: |
paulb@23 | 117 | name = multivalue_name |
paulb@23 | 118 | multivalue = 1 |
paulb@23 | 119 | elif name_var is not None: |
paulb@67 | 120 | name = libxml2mod.xmlNodeGetContent(name_var[0]) |
paulb@213 | 121 | name = unicode(name, "utf-8") |
paulb@23 | 122 | multivalue = 0 |
paulb@9 | 123 | else: |
paulb@9 | 124 | name = None |
paulb@23 | 125 | multivalue = 0 |
paulb@66 | 126 | node = libxml2dom.Node(libxml2mod.xmlXPathGetContextNode(context)) |
paulb@23 | 127 | return path_to_node(node, attribute_ref, name, multivalue) |
paulb@16 | 128 | |
paulb@178 | 129 | # Exposed extension functions. |
paulb@178 | 130 | |
paulb@178 | 131 | def this_element(context): |
paulb@178 | 132 | |
paulb@178 | 133 | """ |
paulb@178 | 134 | Exposed as {template:this-element()}. |
paulb@178 | 135 | Provides a reference to the current element in the form data structure. |
paulb@178 | 136 | """ |
paulb@178 | 137 | |
paulb@178 | 138 | #print "this_element" |
paulb@66 | 139 | r = path_to_context(context, 0) |
paulb@66 | 140 | return r.encode("utf-8") |
paulb@16 | 141 | |
paulb@178 | 142 | def this_attribute(context): |
paulb@178 | 143 | |
paulb@178 | 144 | """ |
paulb@178 | 145 | Exposed as {template:this-attribute()}. |
paulb@178 | 146 | Provides a reference to the current attribute in the form data structure. |
paulb@178 | 147 | """ |
paulb@178 | 148 | |
paulb@178 | 149 | #print "this_attribute" |
paulb@66 | 150 | r = path_to_context(context, 1) |
paulb@66 | 151 | return r.encode("utf-8") |
paulb@1 | 152 | |
paulb@178 | 153 | def new_attribute(context, name): |
paulb@23 | 154 | |
paulb@178 | 155 | """ |
paulb@178 | 156 | Exposed as {template:new-attribute(name)}. |
paulb@178 | 157 | Provides a reference to a new attribute of the given 'name' on the current |
paulb@178 | 158 | element in the form data structure. |
paulb@178 | 159 | """ |
paulb@178 | 160 | |
paulb@178 | 161 | #print "new_attribute" |
paulb@213 | 162 | name = unicode(name, "utf-8") |
paulb@66 | 163 | r = path_to_context(context, 0) + "/" + name |
paulb@66 | 164 | return r.encode("utf-8") |
paulb@34 | 165 | |
paulb@178 | 166 | def other_elements(context, nodes): |
paulb@178 | 167 | |
paulb@178 | 168 | """ |
paulb@178 | 169 | Exposed as {template:other-elements(nodes)}. |
paulb@178 | 170 | Provides a reference to other elements in the form data structure according |
paulb@178 | 171 | to the specified 'nodes' parameter (an XPath expression in the template). |
paulb@178 | 172 | """ |
paulb@178 | 173 | |
paulb@178 | 174 | #print "other_elements" |
paulb@57 | 175 | names = [] |
paulb@57 | 176 | for node in nodes: |
paulb@66 | 177 | name = path_to_node(libxml2dom.Node(node), 0, None, 0) |
paulb@57 | 178 | if name not in names: |
paulb@57 | 179 | names.append(name) |
paulb@66 | 180 | r = ",".join(names) |
paulb@66 | 181 | return r.encode("utf-8") |
paulb@49 | 182 | |
paulb@178 | 183 | def list_attribute(context, element_name, attribute_name): |
paulb@178 | 184 | |
paulb@178 | 185 | """ |
paulb@178 | 186 | Exposed as {template:list-attribute(element_name, attribute_name)}. |
paulb@178 | 187 | Provides a reference to one or many elements of the given 'element_name' |
paulb@178 | 188 | found under the current element in the form data structure having |
paulb@178 | 189 | attributes with the given 'attribute_name'. |
paulb@178 | 190 | """ |
paulb@178 | 191 | |
paulb@178 | 192 | #print "list_attribute" |
paulb@213 | 193 | element_name = unicode(element_name, "utf-8") |
paulb@213 | 194 | attribute_name = unicode(attribute_name, "utf-8") |
paulb@178 | 195 | r = path_to_context(context, 0, (element_name, attribute_name)) |
paulb@178 | 196 | return r.encode("utf-8") |
paulb@178 | 197 | |
paulb@178 | 198 | def other_list_attributes(context, element_name, attribute_name, nodes): |
paulb@178 | 199 | |
paulb@178 | 200 | """ |
paulb@178 | 201 | Exposed as {template:other-list-attributes(element_name, attribute_name, nodes)}. |
paulb@178 | 202 | Provides a reference to other elements in the form data structure, found |
paulb@178 | 203 | under the specified 'nodes' (described using an XPath expression in the |
paulb@178 | 204 | template) having the given 'element_name' and bearing attributes of the |
paulb@178 | 205 | given 'attribute_name'. |
paulb@178 | 206 | """ |
paulb@178 | 207 | |
paulb@178 | 208 | #print "other_list_attributes" |
paulb@213 | 209 | element_name = unicode(element_name, "utf-8") |
paulb@213 | 210 | attribute_name = unicode(attribute_name, "utf-8") |
paulb@178 | 211 | names = [] |
paulb@178 | 212 | for node in nodes: |
paulb@178 | 213 | name = path_to_node(libxml2dom.Node(node), 0, (element_name, attribute_name), 1) |
paulb@178 | 214 | if name not in names: |
paulb@178 | 215 | names.append(name) |
paulb@178 | 216 | r = ",".join(names) |
paulb@178 | 217 | return r.encode("utf-8") |
paulb@178 | 218 | |
paulb@178 | 219 | def other_attributes(context, attribute_name, nodes): |
paulb@178 | 220 | |
paulb@178 | 221 | """ |
paulb@178 | 222 | Exposed as {template:other-attributes(name, nodes)}. |
paulb@178 | 223 | Provides a reference to attributes in the form data structure of the given |
paulb@178 | 224 | 'attribute_name' residing on the specified 'nodes' (described using an XPath |
paulb@178 | 225 | expression in the template). |
paulb@178 | 226 | """ |
paulb@178 | 227 | |
paulb@178 | 228 | #print "other_attributes" |
paulb@213 | 229 | attribute_name = unicode(attribute_name, "utf-8") |
paulb@178 | 230 | # NOTE: Cannot directly reference attributes in the nodes list because |
paulb@178 | 231 | # NOTE: libxml2dom does not yet support parent element discovery on |
paulb@178 | 232 | # NOTE: attributes. |
paulb@178 | 233 | names = [] |
paulb@178 | 234 | for node in nodes: |
paulb@178 | 235 | name = path_to_node(libxml2dom.Node(node), 1, attribute_name, 0) |
paulb@178 | 236 | if name not in names: |
paulb@178 | 237 | names.append(name) |
paulb@178 | 238 | r = ",".join(names) |
paulb@178 | 239 | return r.encode("utf-8") |
paulb@178 | 240 | |
paulb@178 | 241 | def child_element(context, element_name, position, node_paths): |
paulb@178 | 242 | |
paulb@178 | 243 | """ |
paulb@178 | 244 | Exposed as {template:child-element(element_name, position, node_paths)}. |
paulb@178 | 245 | Provides relative paths to the specifed 'element_name', having the given |
paulb@178 | 246 | 'position' (1-based) under each element specified in 'node_paths' (provided |
paulb@178 | 247 | by calls to other extension functions in the template). For example: |
paulb@178 | 248 | |
paulb@178 | 249 | template:child-element('comment', 1, template:this-element()) -> '.../comment$1' |
paulb@178 | 250 | """ |
paulb@178 | 251 | |
paulb@213 | 252 | element_name = unicode(element_name, "utf-8") |
paulb@178 | 253 | l = [] |
paulb@178 | 254 | for node_path in node_paths.split(","): |
paulb@178 | 255 | l.append(node_path + Constants.path_separator + element_name |
paulb@178 | 256 | + Constants.pair_separator + str(int(position))) |
paulb@178 | 257 | return ",".join(l).encode("utf-8") |
paulb@178 | 258 | |
paulb@178 | 259 | def child_attribute(context, attribute_name, node_paths): |
paulb@178 | 260 | |
paulb@178 | 261 | """ |
paulb@178 | 262 | Exposed as {template:child-attribute(attribute_name, node_paths)}. |
paulb@178 | 263 | Provides a relative path to the specifed 'attribute_name' for each element |
paulb@178 | 264 | specified in 'node_paths' (provided by calls to other extension functions in |
paulb@178 | 265 | the template). For example: |
paulb@178 | 266 | |
paulb@178 | 267 | template:child-attribute('value', template:this-element()) -> '.../value' |
paulb@178 | 268 | """ |
paulb@178 | 269 | |
paulb@213 | 270 | attribute_name = unicode(attribute_name, "utf-8") |
paulb@178 | 271 | l = [] |
paulb@178 | 272 | for node_path in node_paths.split(","): |
paulb@178 | 273 | l.append(node_path + Constants.path_separator + attribute_name) |
paulb@178 | 274 | return ",".join(l).encode("utf-8") |
paulb@178 | 275 | |
paulb@178 | 276 | # Old implementations. |
paulb@178 | 277 | |
paulb@178 | 278 | def multi_field_name(context, multivalue_name): |
paulb@178 | 279 | #print "multi_field_name" |
paulb@213 | 280 | multivalue_name = unicode(multivalue_name, "utf-8") |
paulb@178 | 281 | r = path_to_context(context, 1, multivalue_name) |
paulb@178 | 282 | return r.encode("utf-8") |
paulb@178 | 283 | |
paulb@57 | 284 | def other_multi_field_names(context, multivalue_name, nodes): |
paulb@67 | 285 | #print "other_multi_field_names" |
paulb@213 | 286 | multivalue_name = unicode(multivalue_name, "utf-8") |
paulb@57 | 287 | names = [] |
paulb@57 | 288 | for node in nodes: |
paulb@66 | 289 | name = path_to_node(libxml2dom.Node(node), 1, multivalue_name, 1) |
paulb@57 | 290 | if name not in names: |
paulb@57 | 291 | names.append(name) |
paulb@66 | 292 | r = ",".join(names) |
paulb@66 | 293 | return r.encode("utf-8") |
paulb@49 | 294 | |
paulb@213 | 295 | # Utility functions. |
paulb@213 | 296 | |
paulb@213 | 297 | def url_encode(context, nodes, charset="utf-8"): |
paulb@213 | 298 | |
paulb@213 | 299 | """ |
paulb@213 | 300 | Exposed as {template:url-encode(nodes)}. |
paulb@213 | 301 | Provides a "URL encoded" string created from the merged textual contents of |
paulb@213 | 302 | the given 'nodes', with the encoded character values representing characters |
paulb@213 | 303 | in the optional 'charset' (UTF-8 if not specified). |
paulb@213 | 304 | |
paulb@213 | 305 | template:url-encode(./text(), 'iso-8859-1') |
paulb@213 | 306 | """ |
paulb@213 | 307 | |
paulb@213 | 308 | l = [] |
paulb@213 | 309 | for node in nodes: |
paulb@213 | 310 | s = libxml2dom.Node(node).nodeValue |
paulb@213 | 311 | l.append(urllib.quote(s.encode("utf-8")).replace("/", "%2F")) |
paulb@213 | 312 | output = "".join(l) |
paulb@213 | 313 | return output |
paulb@213 | 314 | |
paulb@156 | 315 | # New functions. |
paulb@156 | 316 | |
paulb@156 | 317 | libxsltmod.xsltRegisterExtModuleFunction("list-attribute", "http://www.boddie.org.uk/ns/xmltools/template", list_attribute) |
paulb@156 | 318 | libxsltmod.xsltRegisterExtModuleFunction("other-list-attributes", "http://www.boddie.org.uk/ns/xmltools/template", other_list_attributes) |
paulb@156 | 319 | libxsltmod.xsltRegisterExtModuleFunction("other-attributes", "http://www.boddie.org.uk/ns/xmltools/template", other_attributes) |
paulb@178 | 320 | libxsltmod.xsltRegisterExtModuleFunction("child-element", "http://www.boddie.org.uk/ns/xmltools/template", child_element) |
paulb@178 | 321 | libxsltmod.xsltRegisterExtModuleFunction("child-attribute", "http://www.boddie.org.uk/ns/xmltools/template", child_attribute) |
paulb@156 | 322 | |
paulb@144 | 323 | # New names. |
paulb@144 | 324 | |
paulb@178 | 325 | libxsltmod.xsltRegisterExtModuleFunction("this-element", "http://www.boddie.org.uk/ns/xmltools/template", this_element) |
paulb@178 | 326 | libxsltmod.xsltRegisterExtModuleFunction("this-attribute", "http://www.boddie.org.uk/ns/xmltools/template", this_attribute) |
paulb@178 | 327 | libxsltmod.xsltRegisterExtModuleFunction("new-attribute", "http://www.boddie.org.uk/ns/xmltools/template", new_attribute) |
paulb@178 | 328 | libxsltmod.xsltRegisterExtModuleFunction("other-elements", "http://www.boddie.org.uk/ns/xmltools/template", other_elements) |
paulb@144 | 329 | |
paulb@144 | 330 | # Old names. |
paulb@144 | 331 | |
paulb@178 | 332 | libxsltmod.xsltRegisterExtModuleFunction("this-position", "http://www.boddie.org.uk/ns/xmltools/template", this_element) |
paulb@178 | 333 | libxsltmod.xsltRegisterExtModuleFunction("field-name", "http://www.boddie.org.uk/ns/xmltools/template", this_attribute) |
paulb@178 | 334 | libxsltmod.xsltRegisterExtModuleFunction("new-field", "http://www.boddie.org.uk/ns/xmltools/template", new_attribute) |
paulb@178 | 335 | libxsltmod.xsltRegisterExtModuleFunction("other-field-names", "http://www.boddie.org.uk/ns/xmltools/template", other_elements) |
paulb@178 | 336 | |
paulb@178 | 337 | # Old functions. |
paulb@178 | 338 | |
paulb@66 | 339 | libxsltmod.xsltRegisterExtModuleFunction("multi-field-name", "http://www.boddie.org.uk/ns/xmltools/template", multi_field_name) |
paulb@66 | 340 | libxsltmod.xsltRegisterExtModuleFunction("other-multi-field-names", "http://www.boddie.org.uk/ns/xmltools/template", other_multi_field_names) |
paulb@49 | 341 | |
paulb@213 | 342 | # Utility functions. |
paulb@213 | 343 | |
paulb@213 | 344 | libxsltmod.xsltRegisterExtModuleFunction("url-encode", "http://www.boddie.org.uk/ns/xmltools/template", url_encode) |
paulb@213 | 345 | |
paulb@49 | 346 | def get_field_name(field_or_multi_name): |
paulb@49 | 347 | return field_or_multi_name.split(Constants.multi_separator)[0] |
paulb@49 | 348 | |
paulb@49 | 349 | def get_element_path(field_or_multi_name): |
paulb@49 | 350 | |
paulb@49 | 351 | """ |
paulb@49 | 352 | Convert the given 'field_or_multi_name' back to an XPath reference. |
paulb@49 | 353 | For example: |
paulb@139 | 354 | /configuration$1/details$1/base-system$$value -> /*[position() = 1]/*[position() = 1]/base-system |
paulb@49 | 355 | """ |
paulb@49 | 356 | |
paulb@49 | 357 | field_name = get_field_name(field_or_multi_name) |
paulb@49 | 358 | parts = field_name.split(Constants.path_separator) |
paulb@49 | 359 | new_parts = [] |
paulb@49 | 360 | for part in parts: |
paulb@49 | 361 | path_parts = part.split(Constants.pair_separator) |
paulb@49 | 362 | if len(path_parts) == 2: |
paulb@57 | 363 | new_parts.append("*[position() = " + path_parts[1] + "]") |
paulb@49 | 364 | else: |
paulb@49 | 365 | new_parts.append(path_parts[0]) |
paulb@49 | 366 | return "/".join(new_parts) |
paulb@1 | 367 | |
paulb@1 | 368 | # vim: tabstop=4 expandtab shiftwidth=4 |