1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/examples/Common/Dictionary/__init__.py Fri Sep 09 17:33:19 2005 +0000
1.3 @@ -0,0 +1,148 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"A dictionary example application."
1.7 +
1.8 +import WebStack.Generic
1.9 +import XSLForms.Resources
1.10 +import XSLForms.Utils
1.11 +import os
1.12 +
1.13 +# Site map imports.
1.14 +
1.15 +from WebStack.Resources.ResourceMap import MapResource
1.16 +from WebStack.Resources.Static import DirectoryResource
1.17 +
1.18 +# Resource classes.
1.19 +
1.20 +class DictionaryResource(XSLForms.Resources.XSLFormsResource):
1.21 +
1.22 + "A simple resource providing dictionary lookup."
1.23 +
1.24 + resource_dir = os.path.join(os.path.split(__file__)[0], "Resources")
1.25 + encoding = "utf-8"
1.26 + template_resources = {
1.27 + "words" : ("words_template.xhtml", "words_output.xsl")
1.28 + }
1.29 + in_page_resources = {
1.30 + "entry" : ("words_output_entry.xsl", "entry-node")
1.31 + }
1.32 +
1.33 + def __init__(self, dict):
1.34 +
1.35 + "Initialise the resource with the given 'dict'."
1.36 +
1.37 + self.dict = dict
1.38 +
1.39 + def respond_to_form(self, trans, form):
1.40 +
1.41 + """
1.42 + Respond to a request having the given transaction 'trans' and the given
1.43 + 'form' information.
1.44 + """
1.45 +
1.46 + in_page_resource = self.get_in_page_resource(trans)
1.47 + parameters = form.get_parameters()
1.48 + documents = form.get_documents()
1.49 +
1.50 + # Ensure the presence of a document.
1.51 +
1.52 + if documents.has_key("words"):
1.53 + words = documents["words"]
1.54 + else:
1.55 + words = form.new_instance("words")
1.56 +
1.57 + # Add and remove elements according to the selectors found.
1.58 +
1.59 + selectors = form.get_selectors()
1.60 + XSLForms.Utils.remove_elements(selectors.get("remove"))
1.61 + XSLForms.Utils.add_elements(selectors.get("add"), "entry")
1.62 +
1.63 + # Ensure all entries have a matches element.
1.64 + # Ensure all matches elements have at least one choice.
1.65 + # Copy selected matches to their corresponding text field.
1.66 +
1.67 + for entry in words.xpath("words/entry"):
1.68 + matches_list = entry.xpath("matches")
1.69 + if len(matches_list) == 0:
1.70 + matches = words.createElement("matches")
1.71 + entry.appendChild(matches)
1.72 + else:
1.73 + matches = matches_list[0]
1.74 +
1.75 + if len(entry.xpath("matches/match-enum")) == 0:
1.76 + match_enum = words.createElement("match-enum")
1.77 + match_enum.setAttribute("word", "")
1.78 + matches.appendChild(match_enum)
1.79 +
1.80 + if matches.hasAttribute("word"):
1.81 + word = matches.getAttribute("word")
1.82 + if word != "":
1.83 + entry.setAttribute("word", word)
1.84 +
1.85 + # Find requested search locations.
1.86 +
1.87 + if selectors.has_key("search"):
1.88 + entries = selectors["search"]
1.89 + elif in_page_resource == "entry":
1.90 + entries = words.xpath("words/entry")
1.91 + else:
1.92 + entries = []
1.93 +
1.94 + # Transform, adding dictionary information.
1.95 +
1.96 + for entry in entries:
1.97 + word = entry.getAttribute("word")
1.98 + if word != "":
1.99 + matches = entry.xpath("matches")[0]
1.100 + for found_word in self.dict.find(word):
1.101 + match_enum = words.createElement("match-enum")
1.102 + match_enum.setAttribute("word", found_word)
1.103 + matches.appendChild(match_enum)
1.104 +
1.105 + # Start the response.
1.106 +
1.107 + trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", self.encoding))
1.108 +
1.109 + # Define the stylesheet parameters.
1.110 +
1.111 + stylesheet_parameters = {}
1.112 +
1.113 + # Ensure that an output stylesheet exists.
1.114 +
1.115 + if in_page_resource in self.in_page_resources.keys():
1.116 + trans_xsl = self.prepare_fragment("words", in_page_resource)
1.117 + element_path = parameters.get("element-path", [""])[0]
1.118 + stylesheet_parameters["element-path"] = element_path
1.119 + else:
1.120 + trans_xsl = self.prepare_output("words")
1.121 +
1.122 + # Complete the response.
1.123 +
1.124 + self.send_output(trans, [trans_xsl], words, stylesheet_parameters)
1.125 +
1.126 +# Site map initialisation.
1.127 +
1.128 +def get_site(dict):
1.129 +
1.130 + """
1.131 + Return a simple Web site resource using the given 'dict' - a dictionary
1.132 + employed by the application.
1.133 + """
1.134 +
1.135 + # Get the main resource and the directory used by the application.
1.136 +
1.137 + dictionary_resource = DictionaryResource(dict)
1.138 + directory = dictionary_resource.resource_dir
1.139 +
1.140 + # Make a simple Web site.
1.141 +
1.142 + resource = MapResource({
1.143 + # Static resources:
1.144 + "scripts" : DirectoryResource(os.path.join(directory, "scripts"), {"js" : "text/javascript"}),
1.145 + # Main page and in-page resources:
1.146 + None : dictionary_resource
1.147 + })
1.148 +
1.149 + return resource
1.150 +
1.151 +# vim: tabstop=4 expandtab shiftwidth=4