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