paulb@1 | 1 | #!/usr/bin/env python |
paulb@1 | 2 | |
paulb@1 | 3 | """ |
paulb@1 | 4 | XSL-based form templating. |
paulb@116 | 5 | |
paul@669 | 6 | Copyright (C) 2005, 2007, 2008 Paul Boddie <paul@boddie.org.uk> |
paulb@116 | 7 | |
paulb@600 | 8 | This program is free software; you can redistribute it and/or modify it under |
paulb@600 | 9 | the terms of the GNU Lesser General Public License as published by the Free |
paulb@600 | 10 | Software Foundation; either version 3 of the License, or (at your option) any |
paulb@600 | 11 | later version. |
paulb@116 | 12 | |
paulb@600 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paulb@600 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paulb@600 | 15 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
paulb@600 | 16 | details. |
paulb@116 | 17 | |
paulb@600 | 18 | You should have received a copy of the GNU Lesser General Public License along |
paulb@600 | 19 | with this program. If not, see <http://www.gnu.org/licenses/>. |
paulb@1 | 20 | """ |
paulb@1 | 21 | |
paulb@16 | 22 | import Constants |
paulb@539 | 23 | |
paulb@539 | 24 | # Try the conventional import first. |
paulb@539 | 25 | |
paulb@539 | 26 | try: |
paulb@539 | 27 | import libxsltmod, libxml2mod |
paulb@539 | 28 | except ImportError: |
paulb@539 | 29 | from libxmlmods import libxml2mod |
paulb@539 | 30 | from libxmlmods import libxsltmod |
paulb@539 | 31 | |
paul@670 | 32 | try: |
paul@670 | 33 | set |
paul@670 | 34 | except NameError: |
paul@670 | 35 | from sets import Set as set |
paul@670 | 36 | |
paulb@66 | 37 | import libxml2dom |
paulb@213 | 38 | import urllib |
paulb@1 | 39 | |
paulb@438 | 40 | libxml2_encoding = "utf-8" |
paulb@438 | 41 | |
paul@669 | 42 | def path_to_node(node, attribute_ref=None, name=None, multivalue=0): |
paulb@1 | 43 | |
paulb@9 | 44 | """ |
paulb@16 | 45 | Generate an XSLForms path to the given 'node', producing an attribute |
paulb@16 | 46 | reference if 'attribute_ref' is true; for example: |
paulb@16 | 47 | |
paulb@139 | 48 | /package$1/discriminators$5/discriminator$1/category |
paulb@16 | 49 | |
paulb@16 | 50 | Otherwise an element reference is produced; for example: |
paulb@16 | 51 | |
paulb@139 | 52 | /package$1/discriminators$5/discriminator$1 |
paulb@16 | 53 | |
paulb@16 | 54 | Use the given 'name' to complete the path if an attribute reference is |
paulb@16 | 55 | required (and if a genuine attribute is found at the context node - |
paulb@16 | 56 | otherwise 'name' will be None and the context node will be treated like an |
paulb@16 | 57 | attribute). |
paulb@23 | 58 | |
paulb@156 | 59 | If 'multivalue' is true and 'attribute_ref' is set, produce an attribute |
paulb@156 | 60 | reference using the given 'name': |
paulb@23 | 61 | |
paulb@139 | 62 | /package$1/categories$1/category$$name |
paulb@156 | 63 | |
paulb@156 | 64 | If 'multivalue' is true and 'attribute_ref' is not set, produce an attribute |
paulb@156 | 65 | reference using the given 'name' of form (element, attribute): |
paulb@156 | 66 | |
paulb@156 | 67 | /package$1/categories$1/element$$attribute |
paulb@9 | 68 | """ |
paulb@1 | 69 | |
paulb@1 | 70 | l = [] |
paul@669 | 71 | |
paul@669 | 72 | # Skip attribute references. |
paul@669 | 73 | # Where a node reference has been requested, initialise the name and |
paul@669 | 74 | # attribute reference settings. |
paul@669 | 75 | |
paulb@66 | 76 | if node.nodeType == node.ATTRIBUTE_NODE: |
paul@669 | 77 | if name is None: |
paul@669 | 78 | name = node.localName |
paul@669 | 79 | if attribute_ref is None: |
paul@669 | 80 | attribute_ref = 1 |
paulb@66 | 81 | node = node.parentNode |
paul@669 | 82 | else: |
paul@669 | 83 | if attribute_ref is None: |
paul@669 | 84 | attribute_ref = 0 |
paul@669 | 85 | |
paulb@9 | 86 | # Manually insert the attribute name if defined. |
paul@669 | 87 | |
paulb@16 | 88 | if attribute_ref: |
paul@669 | 89 | |
paulb@23 | 90 | # A real attribute is referenced. |
paul@669 | 91 | |
paulb@16 | 92 | if name is not None: |
paulb@16 | 93 | l.insert(0, name) |
paulb@23 | 94 | if multivalue: |
paulb@49 | 95 | l.insert(0, Constants.multi_separator) |
paulb@66 | 96 | l.insert(0, node.nodeName) |
paulb@66 | 97 | node = node.parentNode |
paulb@16 | 98 | l.insert(0, Constants.path_separator) |
paul@669 | 99 | |
paulb@23 | 100 | # Otherwise, treat the element name as an attribute name. |
paulb@156 | 101 | # NOTE: Not sure how useful this is. |
paul@669 | 102 | |
paulb@16 | 103 | else: |
paulb@66 | 104 | l.insert(0, node.nodeName) |
paulb@16 | 105 | l.insert(0, Constants.path_separator) |
paulb@66 | 106 | node = node.parentNode |
paul@669 | 107 | |
paulb@156 | 108 | # Otherwise insert any multivalue references (eg. list-attribute). |
paul@669 | 109 | |
paulb@156 | 110 | elif multivalue: |
paulb@156 | 111 | element_name, attribute_name = name |
paulb@156 | 112 | l.insert(0, attribute_name) |
paulb@156 | 113 | l.insert(0, Constants.multi_separator) |
paulb@156 | 114 | l.insert(0, element_name) |
paulb@156 | 115 | l.insert(0, Constants.path_separator) |
paulb@156 | 116 | |
paulb@1 | 117 | # Element references. |
paul@669 | 118 | |
paulb@66 | 119 | while node is not None and node.nodeType != node.DOCUMENT_NODE: |
paulb@66 | 120 | l.insert(0, str(int(node.xpath("count(preceding-sibling::*) + 1")))) |
paulb@16 | 121 | l.insert(0, Constants.pair_separator) |
paulb@66 | 122 | l.insert(0, node.nodeName) |
paulb@16 | 123 | l.insert(0, Constants.path_separator) |
paulb@66 | 124 | node = node.parentNode |
paul@669 | 125 | |
paulb@1 | 126 | return "".join(l) |
paulb@1 | 127 | |
paulb@23 | 128 | def path_to_context(context, attribute_ref, multivalue_name=None): |
paulb@1 | 129 | |
paulb@1 | 130 | """ |
paulb@1 | 131 | As a libxslt extension function, return a string containing the XSLForms |
paulb@9 | 132 | path to the 'context' node, using the special "this-name" variable to |
paulb@16 | 133 | complete the path if an attribute reference is required (as indicated by |
paulb@23 | 134 | 'attribute_ref' being set to true). If 'multivalue_name' is set, produce a |
paulb@23 | 135 | reference to a multivalued field using the given string as the attribute |
paulb@23 | 136 | name. |
paulb@1 | 137 | """ |
paulb@1 | 138 | |
paulb@66 | 139 | context = libxml2mod.xmlXPathParserGetContext(context) |
paulb@66 | 140 | transform_context = libxsltmod.xsltXPathGetTransformContext(context) |
paulb@66 | 141 | name_var = libxsltmod.xsltVariableLookup(transform_context, "this-name", None) |
paulb@23 | 142 | if multivalue_name is not None: |
paulb@23 | 143 | name = multivalue_name |
paulb@23 | 144 | multivalue = 1 |
paulb@23 | 145 | elif name_var is not None: |
paulb@67 | 146 | name = libxml2mod.xmlNodeGetContent(name_var[0]) |
paulb@438 | 147 | name = unicode(name, libxml2_encoding) |
paulb@23 | 148 | multivalue = 0 |
paulb@9 | 149 | else: |
paulb@9 | 150 | name = None |
paulb@23 | 151 | multivalue = 0 |
paulb@66 | 152 | node = libxml2dom.Node(libxml2mod.xmlXPathGetContextNode(context)) |
paulb@23 | 153 | return path_to_node(node, attribute_ref, name, multivalue) |
paulb@16 | 154 | |
paulb@178 | 155 | # Exposed extension functions. |
paulb@178 | 156 | |
paulb@178 | 157 | def this_element(context): |
paulb@178 | 158 | |
paulb@178 | 159 | """ |
paulb@178 | 160 | Exposed as {template:this-element()}. |
paulb@382 | 161 | |
paulb@178 | 162 | Provides a reference to the current element in the form data structure. |
paulb@178 | 163 | """ |
paulb@178 | 164 | |
paulb@178 | 165 | #print "this_element" |
paulb@66 | 166 | r = path_to_context(context, 0) |
paulb@438 | 167 | return r.encode(libxml2_encoding) |
paulb@16 | 168 | |
paulb@178 | 169 | def this_attribute(context): |
paulb@178 | 170 | |
paulb@178 | 171 | """ |
paulb@178 | 172 | Exposed as {template:this-attribute()}. |
paulb@382 | 173 | |
paulb@178 | 174 | Provides a reference to the current attribute in the form data structure. |
paulb@178 | 175 | """ |
paulb@178 | 176 | |
paulb@178 | 177 | #print "this_attribute" |
paulb@66 | 178 | r = path_to_context(context, 1) |
paulb@438 | 179 | return r.encode(libxml2_encoding) |
paulb@1 | 180 | |
paulb@178 | 181 | def new_attribute(context, name): |
paulb@23 | 182 | |
paulb@178 | 183 | """ |
paulb@178 | 184 | Exposed as {template:new-attribute(name)}. |
paulb@382 | 185 | |
paulb@178 | 186 | Provides a reference to a new attribute of the given 'name' on the current |
paulb@178 | 187 | element in the form data structure. |
paulb@178 | 188 | """ |
paulb@178 | 189 | |
paulb@178 | 190 | #print "new_attribute" |
paulb@438 | 191 | name = unicode(name, libxml2_encoding) |
paulb@66 | 192 | r = path_to_context(context, 0) + "/" + name |
paulb@438 | 193 | return r.encode(libxml2_encoding) |
paulb@34 | 194 | |
paulb@178 | 195 | def other_elements(context, nodes): |
paulb@178 | 196 | |
paulb@178 | 197 | """ |
paulb@178 | 198 | Exposed as {template:other-elements(nodes)}. |
paulb@382 | 199 | |
paulb@178 | 200 | Provides a reference to other elements in the form data structure according |
paulb@178 | 201 | to the specified 'nodes' parameter (an XPath expression in the template). |
paulb@178 | 202 | """ |
paulb@178 | 203 | |
paulb@178 | 204 | #print "other_elements" |
paulb@57 | 205 | names = [] |
paulb@57 | 206 | for node in nodes: |
paulb@66 | 207 | name = path_to_node(libxml2dom.Node(node), 0, None, 0) |
paulb@57 | 208 | if name not in names: |
paulb@57 | 209 | names.append(name) |
paulb@66 | 210 | r = ",".join(names) |
paulb@438 | 211 | return r.encode(libxml2_encoding) |
paulb@49 | 212 | |
paulb@178 | 213 | def list_attribute(context, element_name, attribute_name): |
paulb@178 | 214 | |
paulb@178 | 215 | """ |
paulb@178 | 216 | Exposed as {template:list-attribute(element_name, attribute_name)}. |
paulb@382 | 217 | |
paulb@178 | 218 | Provides a reference to one or many elements of the given 'element_name' |
paulb@178 | 219 | found under the current element in the form data structure having |
paulb@178 | 220 | attributes with the given 'attribute_name'. |
paulb@178 | 221 | """ |
paulb@178 | 222 | |
paulb@178 | 223 | #print "list_attribute" |
paulb@438 | 224 | element_name = unicode(element_name, libxml2_encoding) |
paulb@438 | 225 | attribute_name = unicode(attribute_name, libxml2_encoding) |
paulb@178 | 226 | r = path_to_context(context, 0, (element_name, attribute_name)) |
paulb@438 | 227 | return r.encode(libxml2_encoding) |
paulb@178 | 228 | |
paulb@178 | 229 | def other_list_attributes(context, element_name, attribute_name, nodes): |
paulb@178 | 230 | |
paulb@178 | 231 | """ |
paulb@178 | 232 | Exposed as {template:other-list-attributes(element_name, attribute_name, nodes)}. |
paulb@382 | 233 | |
paulb@178 | 234 | Provides a reference to other elements in the form data structure, found |
paulb@178 | 235 | under the specified 'nodes' (described using an XPath expression in the |
paulb@178 | 236 | template) having the given 'element_name' and bearing attributes of the |
paulb@178 | 237 | given 'attribute_name'. |
paulb@178 | 238 | """ |
paulb@178 | 239 | |
paulb@178 | 240 | #print "other_list_attributes" |
paulb@438 | 241 | element_name = unicode(element_name, libxml2_encoding) |
paulb@438 | 242 | attribute_name = unicode(attribute_name, libxml2_encoding) |
paulb@178 | 243 | names = [] |
paulb@178 | 244 | for node in nodes: |
paulb@178 | 245 | name = path_to_node(libxml2dom.Node(node), 0, (element_name, attribute_name), 1) |
paulb@178 | 246 | if name not in names: |
paulb@178 | 247 | names.append(name) |
paulb@178 | 248 | r = ",".join(names) |
paulb@438 | 249 | return r.encode(libxml2_encoding) |
paulb@178 | 250 | |
paulb@178 | 251 | def other_attributes(context, attribute_name, nodes): |
paulb@178 | 252 | |
paulb@178 | 253 | """ |
paulb@178 | 254 | Exposed as {template:other-attributes(name, nodes)}. |
paulb@382 | 255 | |
paulb@178 | 256 | Provides a reference to attributes in the form data structure of the given |
paulb@178 | 257 | 'attribute_name' residing on the specified 'nodes' (described using an XPath |
paulb@178 | 258 | expression in the template). |
paulb@178 | 259 | """ |
paulb@178 | 260 | |
paulb@178 | 261 | #print "other_attributes" |
paulb@438 | 262 | attribute_name = unicode(attribute_name, libxml2_encoding) |
paul@669 | 263 | # NOTE: Could not directly reference attributes in the nodes list because |
paul@669 | 264 | # NOTE: libxml2dom did not yet support parent element discovery on |
paul@669 | 265 | # NOTE: attributes. The nodes function below remedies this. |
paul@670 | 266 | names = set() |
paulb@178 | 267 | for node in nodes: |
paulb@178 | 268 | name = path_to_node(libxml2dom.Node(node), 1, attribute_name, 0) |
paul@670 | 269 | names.add(name) |
paulb@178 | 270 | r = ",".join(names) |
paulb@438 | 271 | return r.encode(libxml2_encoding) |
paulb@178 | 272 | |
paul@669 | 273 | def nodes(context, nodes): |
paul@669 | 274 | |
paul@669 | 275 | """ |
paul@669 | 276 | Exposed as {template:nodes(nodes)}. |
paul@669 | 277 | |
paul@669 | 278 | Provides a reference to 'nodes' in the form data structure, described using |
paul@669 | 279 | an XPath expression in the template. |
paul@669 | 280 | """ |
paul@669 | 281 | |
paul@670 | 282 | names = set() |
paul@669 | 283 | for node in nodes: |
paul@669 | 284 | name = path_to_node(libxml2dom.Node(node)) |
paul@670 | 285 | names.add(name) |
paul@669 | 286 | r = ",".join(names) |
paul@669 | 287 | return r.encode(libxml2_encoding) |
paul@669 | 288 | |
paulb@178 | 289 | def child_element(context, element_name, position, node_paths): |
paulb@178 | 290 | |
paulb@178 | 291 | """ |
paulb@178 | 292 | Exposed as {template:child-element(element_name, position, node_paths)}. |
paulb@382 | 293 | |
paulb@178 | 294 | Provides relative paths to the specifed 'element_name', having the given |
paulb@178 | 295 | 'position' (1-based) under each element specified in 'node_paths' (provided |
paulb@178 | 296 | by calls to other extension functions in the template). For example: |
paulb@178 | 297 | |
paulb@178 | 298 | template:child-element('comment', 1, template:this-element()) -> '.../comment$1' |
paulb@178 | 299 | """ |
paulb@178 | 300 | |
paulb@438 | 301 | element_name = unicode(element_name, libxml2_encoding) |
paulb@178 | 302 | l = [] |
paulb@178 | 303 | for node_path in node_paths.split(","): |
paulb@178 | 304 | l.append(node_path + Constants.path_separator + element_name |
paulb@178 | 305 | + Constants.pair_separator + str(int(position))) |
paulb@438 | 306 | return ",".join(l).encode(libxml2_encoding) |
paulb@178 | 307 | |
paulb@178 | 308 | def child_attribute(context, attribute_name, node_paths): |
paulb@178 | 309 | |
paulb@178 | 310 | """ |
paulb@178 | 311 | Exposed as {template:child-attribute(attribute_name, node_paths)}. |
paulb@382 | 312 | |
paulb@178 | 313 | Provides a relative path to the specifed 'attribute_name' for each element |
paulb@178 | 314 | specified in 'node_paths' (provided by calls to other extension functions in |
paulb@178 | 315 | the template). For example: |
paulb@178 | 316 | |
paulb@178 | 317 | template:child-attribute('value', template:this-element()) -> '.../value' |
paulb@178 | 318 | """ |
paulb@178 | 319 | |
paulb@438 | 320 | attribute_name = unicode(attribute_name, libxml2_encoding) |
paulb@178 | 321 | l = [] |
paulb@178 | 322 | for node_path in node_paths.split(","): |
paulb@178 | 323 | l.append(node_path + Constants.path_separator + attribute_name) |
paulb@438 | 324 | return ",".join(l).encode(libxml2_encoding) |
paulb@178 | 325 | |
paulb@301 | 326 | def selector_name(context, field_name, nodes): |
paulb@301 | 327 | |
paulb@301 | 328 | """ |
paulb@301 | 329 | Exposed as {template:selector-name(field_name, nodes)}. |
paulb@382 | 330 | |
paulb@301 | 331 | Provides a selector field name defined using 'field_name' and referring to |
paulb@301 | 332 | the given 'nodes'. For example: |
paulb@301 | 333 | |
paulb@301 | 334 | template:selector-name('add-platform', package/platforms) -> 'add-platform=/package$1/platforms$1' |
paulb@301 | 335 | |
paulb@301 | 336 | NOTE: The 'nodes' must be element references. |
paulb@301 | 337 | """ |
paulb@301 | 338 | |
paulb@301 | 339 | #print "selector_name" |
paulb@301 | 340 | names = [] |
paulb@301 | 341 | for node in nodes: |
paulb@301 | 342 | name = path_to_node(libxml2dom.Node(node), 0, None, 0) |
paulb@301 | 343 | if name not in names: |
paulb@301 | 344 | names.append(field_name + "=" + name) |
paulb@301 | 345 | r = ",".join(names) |
paulb@438 | 346 | return r.encode(libxml2_encoding) |
paulb@301 | 347 | |
paulb@178 | 348 | # Old implementations. |
paulb@178 | 349 | |
paulb@178 | 350 | def multi_field_name(context, multivalue_name): |
paulb@178 | 351 | #print "multi_field_name" |
paulb@438 | 352 | multivalue_name = unicode(multivalue_name, libxml2_encoding) |
paulb@178 | 353 | r = path_to_context(context, 1, multivalue_name) |
paulb@438 | 354 | return r.encode(libxml2_encoding) |
paulb@178 | 355 | |
paulb@57 | 356 | def other_multi_field_names(context, multivalue_name, nodes): |
paulb@67 | 357 | #print "other_multi_field_names" |
paulb@438 | 358 | multivalue_name = unicode(multivalue_name, libxml2_encoding) |
paulb@57 | 359 | names = [] |
paulb@57 | 360 | for node in nodes: |
paulb@66 | 361 | name = path_to_node(libxml2dom.Node(node), 1, multivalue_name, 1) |
paulb@57 | 362 | if name not in names: |
paulb@57 | 363 | names.append(name) |
paulb@66 | 364 | r = ",".join(names) |
paulb@438 | 365 | return r.encode(libxml2_encoding) |
paulb@49 | 366 | |
paulb@213 | 367 | # Utility functions. |
paulb@213 | 368 | |
paulb@454 | 369 | def xslforms_range(context, range_spec): |
paulb@454 | 370 | |
paulb@454 | 371 | """ |
paulb@454 | 372 | Exposed as {template:range(range_spec)}. |
paulb@454 | 373 | |
paulb@454 | 374 | The 'range_spec' is split up into 'start', 'finish' and 'step' according to |
paulb@454 | 375 | the following format: |
paulb@454 | 376 | |
paulb@454 | 377 | start...finish...step |
paulb@454 | 378 | |
paulb@454 | 379 | Provides the Python range function by producing a list of numbers, starting |
paulb@454 | 380 | at 'start', ending one step before 'finish', and employing the optional |
paulb@454 | 381 | 'step' to indicate the magnitude of the difference between successive |
paulb@454 | 382 | elements in the list as well as the "direction" of the sequence. By default, |
paulb@454 | 383 | 'step' is set to 1. |
paulb@454 | 384 | |
paulb@454 | 385 | NOTE: This uses a single string because template:element and other |
paulb@454 | 386 | NOTE: annotations use commas to separate fields, thus making the usage of |
paulb@454 | 387 | NOTE: this function impossible if each range parameter is exposed as a |
paulb@454 | 388 | NOTE: function parameter. |
paulb@454 | 389 | NOTE: The returning of values from this function is not fully verified, and |
paulb@454 | 390 | NOTE: it is probably better to use other extension functions instead of this |
paulb@454 | 391 | NOTE: one to achieve simple results (such as str:split from EXSLT). |
paulb@454 | 392 | """ |
paulb@454 | 393 | |
paulb@454 | 394 | parts = range_spec.split("...") |
paulb@454 | 395 | start, finish = parts[:2] |
paulb@454 | 396 | if len(parts) > 2: |
paulb@454 | 397 | step = parts[2] |
paulb@454 | 398 | else: |
paulb@454 | 399 | step = None |
paulb@454 | 400 | |
paulb@454 | 401 | start = int(start) |
paulb@454 | 402 | finish = int(finish) |
paulb@454 | 403 | if step is not None: |
paulb@454 | 404 | step = int(step) |
paulb@454 | 405 | else: |
paulb@454 | 406 | step = 1 |
paulb@454 | 407 | |
paulb@454 | 408 | # Create a list of elements. |
paulb@454 | 409 | # NOTE: libxslt complains: "Got a CObject" |
paulb@454 | 410 | |
paulb@454 | 411 | range_elements = libxml2mod.xmlXPathNewNodeSet(None) |
paulb@454 | 412 | for i in range(start, finish, step): |
paulb@454 | 413 | range_elements.append(libxml2mod.xmlNewText(str(i))) |
paulb@454 | 414 | return range_elements |
paulb@454 | 415 | |
paulb@434 | 416 | def i18n(context, value): |
paulb@434 | 417 | |
paulb@434 | 418 | """ |
paulb@434 | 419 | Exposed as {template:i18n(value)}. |
paulb@434 | 420 | |
paulb@434 | 421 | Provides a translation of the given 'value' using the 'translations' and |
paulb@443 | 422 | 'locale' variables defined in the output stylesheet. The 'value' may be a |
paulb@443 | 423 | string or a collection of nodes, each having a textual value, where such |
paulb@443 | 424 | values are then concatenated to produce a single string value. |
paulb@434 | 425 | """ |
paulb@434 | 426 | |
paulb@443 | 427 | if isinstance(value, str): |
paulb@443 | 428 | value = unicode(value, libxml2_encoding) |
paulb@443 | 429 | else: |
paulb@443 | 430 | l = [] |
paulb@443 | 431 | for node in value: |
paulb@443 | 432 | s = libxml2dom.Node(node).nodeValue |
paulb@443 | 433 | l.append(s) |
paulb@443 | 434 | value = "".join(l) |
paulb@443 | 435 | |
paulb@434 | 436 | context = libxml2mod.xmlXPathParserGetContext(context) |
paulb@434 | 437 | transform_context = libxsltmod.xsltXPathGetTransformContext(context) |
paulb@434 | 438 | translations_var = libxsltmod.xsltVariableLookup(transform_context, "translations", None) |
paulb@434 | 439 | locale_var = libxsltmod.xsltVariableLookup(transform_context, "locale", None) |
paulb@545 | 440 | if translations_var is not None and translations_var and locale_var is not None: |
paulb@434 | 441 | translations = libxml2dom.Node(translations_var[0]) |
paulb@434 | 442 | results = translations.xpath("/translations/locale[code/@value='%s']/translation[@value='%s']/text()" % (locale_var, value)) |
paulb@545 | 443 | if not results: |
paulb@545 | 444 | results = translations.xpath("/translations/locale[1]/translation[@value='%s']/text()" % value) |
paulb@545 | 445 | if results: |
paulb@438 | 446 | return results[0].nodeValue.encode(libxml2_encoding) |
paulb@438 | 447 | return value.encode(libxml2_encoding) |
paulb@434 | 448 | |
paulb@420 | 449 | def choice(context, value, true_string, false_string=None): |
paulb@420 | 450 | |
paulb@420 | 451 | """ |
paulb@420 | 452 | Exposed as {template:choice(value, true_string, false_string)}. |
paulb@420 | 453 | |
paulb@420 | 454 | Using the given boolean 'value', which may itself be an expression evaluated |
paulb@420 | 455 | by the XSLT processor, return the 'true_string' if 'value' is true or the |
paulb@420 | 456 | 'false_string' if 'value' is false. If 'false_string' is omitted and if |
paulb@420 | 457 | 'value' evaluates to a false value, an empty string is returned. |
paulb@420 | 458 | """ |
paulb@420 | 459 | |
paulb@420 | 460 | if value: |
paulb@420 | 461 | return true_string |
paulb@420 | 462 | else: |
paulb@420 | 463 | return false_string or "" |
paulb@420 | 464 | |
paulb@438 | 465 | def url_encode(context, nodes, charset=libxml2_encoding): |
paulb@213 | 466 | |
paulb@213 | 467 | """ |
paulb@213 | 468 | Exposed as {template:url-encode(nodes)}. |
paulb@382 | 469 | |
paulb@213 | 470 | Provides a "URL encoded" string created from the merged textual contents of |
paulb@213 | 471 | the given 'nodes', with the encoded character values representing characters |
paulb@417 | 472 | in the optional 'charset' (UTF-8 if not specified). Note that / and # |
paulb@417 | 473 | characters are replaced with their "URL encoded" character values. |
paulb@213 | 474 | |
paulb@418 | 475 | If a string value is supplied for 'nodes', this will be translated instead. |
paulb@418 | 476 | |
paulb@213 | 477 | template:url-encode(./text(), 'iso-8859-1') |
paulb@213 | 478 | """ |
paulb@213 | 479 | |
paulb@213 | 480 | l = [] |
paulb@418 | 481 | if isinstance(nodes, str): |
paulb@438 | 482 | return urllib.quote(nodes.encode(libxml2_encoding)).replace("/", "%2F").replace("#", "%23") |
paulb@418 | 483 | |
paulb@213 | 484 | for node in nodes: |
paulb@213 | 485 | s = libxml2dom.Node(node).nodeValue |
paulb@438 | 486 | l.append(urllib.quote(s.encode(libxml2_encoding)).replace("/", "%2F").replace("#", "%23")) |
paulb@213 | 487 | output = "".join(l) |
paulb@213 | 488 | return output |
paulb@213 | 489 | |
paulb@222 | 490 | def element_path(context, field_names): |
paulb@222 | 491 | |
paulb@222 | 492 | """ |
paulb@382 | 493 | Exposed as {template:element-path(field_names)}. |
paulb@382 | 494 | |
paulb@222 | 495 | Convert the given 'field_names' back to XPath references. |
paulb@222 | 496 | For example: |
paulb@382 | 497 | |
paulb@222 | 498 | /configuration$1/details$1/base-system$$value -> /*[position() = 1]/*[position() = 1]/base-system |
paulb@382 | 499 | |
paulb@222 | 500 | If more than one field name is given - ie. 'field_names' contains a |
paulb@222 | 501 | comma-separated list of names - then only the first name is used. |
paulb@382 | 502 | |
paulb@382 | 503 | To use this function effectively, use the result of another function as the |
paulb@382 | 504 | argument. For example: |
paulb@382 | 505 | |
paulb@382 | 506 | template:element-path(template:this-element()) |
paulb@382 | 507 | template:element-path(template:other-elements(matches)) |
paulb@382 | 508 | template:element-path(template:other-elements(..)) |
paulb@222 | 509 | """ |
paulb@222 | 510 | |
paulb@222 | 511 | field_name = field_names.split(",")[0] |
paulb@222 | 512 | |
paulb@222 | 513 | # Get the main part of the name (where a multivalue reference was given). |
paulb@222 | 514 | |
paulb@222 | 515 | field_name = get_field_name(field_name) |
paulb@222 | 516 | |
paulb@222 | 517 | # Build the XPath expression. |
paulb@222 | 518 | |
paulb@222 | 519 | parts = field_name.split(Constants.path_separator) |
paulb@222 | 520 | new_parts = [] |
paulb@222 | 521 | for part in parts: |
paulb@222 | 522 | path_parts = part.split(Constants.pair_separator) |
paulb@222 | 523 | if len(path_parts) == 2: |
paulb@222 | 524 | new_parts.append("*[position() = " + path_parts[1] + "]") |
paulb@222 | 525 | else: |
paulb@222 | 526 | new_parts.append(path_parts[0]) |
paulb@222 | 527 | return "/".join(new_parts) |
paulb@222 | 528 | |
paulb@156 | 529 | # New functions. |
paulb@156 | 530 | |
paulb@156 | 531 | libxsltmod.xsltRegisterExtModuleFunction("list-attribute", "http://www.boddie.org.uk/ns/xmltools/template", list_attribute) |
paulb@156 | 532 | libxsltmod.xsltRegisterExtModuleFunction("other-list-attributes", "http://www.boddie.org.uk/ns/xmltools/template", other_list_attributes) |
paulb@156 | 533 | libxsltmod.xsltRegisterExtModuleFunction("other-attributes", "http://www.boddie.org.uk/ns/xmltools/template", other_attributes) |
paulb@178 | 534 | libxsltmod.xsltRegisterExtModuleFunction("child-element", "http://www.boddie.org.uk/ns/xmltools/template", child_element) |
paulb@178 | 535 | libxsltmod.xsltRegisterExtModuleFunction("child-attribute", "http://www.boddie.org.uk/ns/xmltools/template", child_attribute) |
paulb@301 | 536 | libxsltmod.xsltRegisterExtModuleFunction("selector-name", "http://www.boddie.org.uk/ns/xmltools/template", selector_name) |
paul@669 | 537 | libxsltmod.xsltRegisterExtModuleFunction("nodes", "http://www.boddie.org.uk/ns/xmltools/template", nodes) |
paulb@156 | 538 | |
paulb@144 | 539 | # New names. |
paulb@144 | 540 | |
paulb@178 | 541 | libxsltmod.xsltRegisterExtModuleFunction("this-element", "http://www.boddie.org.uk/ns/xmltools/template", this_element) |
paulb@178 | 542 | libxsltmod.xsltRegisterExtModuleFunction("this-attribute", "http://www.boddie.org.uk/ns/xmltools/template", this_attribute) |
paulb@178 | 543 | libxsltmod.xsltRegisterExtModuleFunction("new-attribute", "http://www.boddie.org.uk/ns/xmltools/template", new_attribute) |
paulb@178 | 544 | libxsltmod.xsltRegisterExtModuleFunction("other-elements", "http://www.boddie.org.uk/ns/xmltools/template", other_elements) |
paulb@144 | 545 | |
paulb@144 | 546 | # Old names. |
paulb@144 | 547 | |
paulb@178 | 548 | libxsltmod.xsltRegisterExtModuleFunction("this-position", "http://www.boddie.org.uk/ns/xmltools/template", this_element) |
paulb@178 | 549 | libxsltmod.xsltRegisterExtModuleFunction("field-name", "http://www.boddie.org.uk/ns/xmltools/template", this_attribute) |
paulb@178 | 550 | libxsltmod.xsltRegisterExtModuleFunction("new-field", "http://www.boddie.org.uk/ns/xmltools/template", new_attribute) |
paulb@178 | 551 | libxsltmod.xsltRegisterExtModuleFunction("other-field-names", "http://www.boddie.org.uk/ns/xmltools/template", other_elements) |
paulb@178 | 552 | |
paulb@178 | 553 | # Old functions. |
paulb@178 | 554 | |
paulb@66 | 555 | libxsltmod.xsltRegisterExtModuleFunction("multi-field-name", "http://www.boddie.org.uk/ns/xmltools/template", multi_field_name) |
paulb@66 | 556 | libxsltmod.xsltRegisterExtModuleFunction("other-multi-field-names", "http://www.boddie.org.uk/ns/xmltools/template", other_multi_field_names) |
paulb@49 | 557 | |
paulb@213 | 558 | # Utility functions. |
paulb@213 | 559 | |
paulb@454 | 560 | libxsltmod.xsltRegisterExtModuleFunction("range", "http://www.boddie.org.uk/ns/xmltools/template", xslforms_range) |
paulb@434 | 561 | libxsltmod.xsltRegisterExtModuleFunction("i18n", "http://www.boddie.org.uk/ns/xmltools/template", i18n) |
paulb@420 | 562 | libxsltmod.xsltRegisterExtModuleFunction("choice", "http://www.boddie.org.uk/ns/xmltools/template", choice) |
paulb@213 | 563 | libxsltmod.xsltRegisterExtModuleFunction("url-encode", "http://www.boddie.org.uk/ns/xmltools/template", url_encode) |
paulb@222 | 564 | libxsltmod.xsltRegisterExtModuleFunction("element-path", "http://www.boddie.org.uk/ns/xmltools/template", element_path) |
paulb@213 | 565 | |
paulb@49 | 566 | def get_field_name(field_or_multi_name): |
paulb@49 | 567 | return field_or_multi_name.split(Constants.multi_separator)[0] |
paulb@49 | 568 | |
paulb@1 | 569 | # vim: tabstop=4 expandtab shiftwidth=4 |