1 #!/usr/bin/env python 2 3 "A dictionary example application." 4 5 import WebStack.Generic 6 import XSLForms.Resources.WebResources 7 import XSLForms.Utils 8 import os 9 10 # Site map imports. 11 12 from WebStack.Resources.ResourceMap import MapResource 13 from WebStack.Resources.Static import DirectoryResource 14 15 # Resource classes. 16 17 class DictionaryResource(XSLForms.Resources.WebResources.XSLFormsResource): 18 19 "A simple resource providing dictionary lookup." 20 21 resource_dir = os.path.join(os.path.split(__file__)[0], "Resources") 22 encoding = "utf-8" 23 template_resources = { 24 "words" : ("words_template.xhtml", "words_output.xsl") 25 } 26 in_page_resources = { 27 "matches" : ("words_output_entry.xsl", "matches-node"), 28 #"word" : ("words_output_word.xsl", "word-node") 29 } 30 31 def __init__(self, dict): 32 33 "Initialise the resource with the given 'dict'." 34 35 self.dict = dict 36 37 def respond_to_form(self, trans, form): 38 39 """ 40 Respond to a request having the given transaction 'trans' and the given 41 'form' information. 42 """ 43 44 in_page_resource = self.get_in_page_resource(trans) 45 parameters = form.get_parameters() 46 documents = form.get_documents() 47 48 # Ensure the presence of a document. 49 50 if documents.has_key("words"): 51 words = documents["words"] 52 else: 53 words = form.new_instance("words") 54 55 # Add and remove elements according to the selectors found. 56 57 selectors = form.get_selectors() 58 XSLForms.Utils.remove_elements(selectors.get("remove")) 59 XSLForms.Utils.add_elements(selectors.get("add"), "entry") 60 61 # Ensure all entries have a matches element. 62 # Ensure all matches elements have at least one choice. 63 # Copy selected matches to their corresponding text field. 64 65 all_entries = words.xpath("words/entry") 66 67 for entry in all_entries: 68 matches_list = entry.xpath("matches") 69 if len(matches_list) == 0: 70 matches = words.createElement("matches") 71 entry.appendChild(matches) 72 else: 73 matches = matches_list[0] 74 75 if len(entry.xpath("matches/match-enum")) == 0: 76 match_enum = words.createElement("match-enum") 77 match_enum.setAttribute("word", "") 78 matches.appendChild(match_enum) 79 80 # Find requested search locations. 81 82 if selectors.has_key("search"): 83 entries = selectors["search"] 84 elif in_page_resource == "matches": 85 entries = all_entries 86 else: 87 entries = [] 88 89 # Transform, adding dictionary information. 90 91 for entry in entries: 92 word = entry.getAttribute("word") 93 if word != "": 94 matches = entry.xpath("matches")[0] 95 for found_word in self.dict.find(word): 96 match_enum = words.createElement("match-enum") 97 match_enum.setAttribute("word", found_word) 98 matches.appendChild(match_enum) 99 100 # Copy selected values into text fields. 101 # NOTE: Since libxml2dom does not guarantee node equality for two nodes 102 # NOTE: referring to the same thing, we cannot just loop over all the 103 # NOTE: entries and query whether they reside in the search locations. 104 105 for entry in all_entries: 106 matches = entry.xpath("matches")[0] 107 if matches.hasAttribute("word"): 108 word = matches.getAttribute("word") 109 if word != "" and word.startswith(entry.getAttribute("word")): 110 entry.setAttribute("word", word) 111 112 # Start the response. 113 114 trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", self.encoding)) 115 116 # Ensure that an output stylesheet exists. 117 118 if in_page_resource in self.in_page_resources.keys(): 119 trans_xsl = self.prepare_fragment("words", in_page_resource) 120 stylesheet_parameters = self.prepare_parameters(parameters) 121 else: 122 trans_xsl = self.prepare_output("words") 123 stylesheet_parameters = {} 124 125 # Complete the response. 126 127 self.send_output(trans, [trans_xsl], words, stylesheet_parameters) 128 129 # Site map initialisation. 130 131 def get_site(dict): 132 133 """ 134 Return a simple Web site resource using the given 'dict' - a dictionary 135 employed by the application. 136 """ 137 138 # Get the main resource and the directory used by the application. 139 140 dictionary_resource = DictionaryResource(dict) 141 directory = dictionary_resource.resource_dir 142 143 # Make a simple Web site. 144 145 resource = MapResource({ 146 # Static resources: 147 "scripts" : DirectoryResource(os.path.join(directory, "scripts"), {"js" : "text/javascript"}), 148 # Main page and in-page resources: 149 "" : dictionary_resource, 150 "matches" : dictionary_resource 151 }) 152 153 return resource 154 155 # vim: tabstop=4 expandtab shiftwidth=4