# HG changeset patch # User paulb # Date 1203276148 0 # Node ID 6484f8ab022059fbceaad8c749bf5237f5b34426 # Parent 0c70a001d6e83ae5b928a3b046b91821501dd3d9 [project @ 2008-02-17 19:22:24 by paulb] Converted to use the modular API. diff -r 0c70a001d6e8 -r 6484f8ab0220 examples/Common/Dictionary/__init__.py --- a/examples/Common/Dictionary/__init__.py Sun Feb 17 19:21:57 2008 +0000 +++ b/examples/Common/Dictionary/__init__.py Sun Feb 17 19:22:28 2008 +0000 @@ -3,8 +3,8 @@ "A dictionary example application." import WebStack.Generic -import XSLForms.Resources.WebResources -import XSLForms.Utils +from XSLForms.Resources.WebResources import \ + XSLFormsResource, output, resources, prepare_resources as xslforms_prepare_resources import os # Site map imports. @@ -19,13 +19,13 @@ # Resource classes. -class DictionaryResource(XSLForms.Resources.WebResources.XSLFormsResource): +class DictionaryResource(XSLFormsResource): "A simple resource providing dictionary lookup." - resource_dir = os.path.join(os.path.split(__file__)[0], "Resources") + resource_dir = resources(__file__) template_resources = { - "words" : ("words_template.xhtml", "words_output.xsl") + "words" : output("words_template.xhtml") } in_page_resources = { "matches" : ("words", "words_output_entry.xsl", "matches-node"), @@ -38,34 +38,25 @@ self.dict = dict - def respond_to_form(self, trans, form): - - """ - Respond to a request having the given transaction 'trans' and the given - 'form' information. - """ + def select_activity(self, trans, form): + form.set_activity("words") - in_page_resource = self.get_in_page_resource(trans) - parameters = form.get_parameters() - documents = form.get_documents() - - # Ensure the presence of a document. - - if documents.has_key("words"): - words = documents["words"] - else: - words = form.new_instance("words") + def respond_to_input(self, trans, form): # Add and remove elements according to the selectors found. selectors = form.get_selectors() - XSLForms.Utils.remove_elements(selectors.get("remove")) - XSLForms.Utils.add_elements(selectors.get("add"), "entry") + self.remove_elements(selectors.get("remove")) + self.add_elements(selectors.get("add"), "entry") + + def init_document(self, trans, form): # Ensure all entries have a matches element. # Ensure all matches elements have at least one choice. # Copy selected matches to their corresponding text field. + words = form.get_document() + all_entries = words.xpath("words/entry") for entry in all_entries: @@ -83,6 +74,9 @@ # Find requested search locations. + selectors = form.get_selectors() + in_page_resource = self.get_in_page_resource(trans) + if selectors.has_key("search"): entries = selectors["search"] elif in_page_resource == "matches": @@ -113,27 +107,6 @@ if word != "" and word.startswith(entry.getAttribute("word")): entry.setAttribute("word", word) - # Start the response. - - trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", encoding)) - - # Ensure that an output stylesheet exists. - - if in_page_resource in self.in_page_resources.keys(): - trans_xsl = self.prepare_fragment(in_page_resource) - stylesheet_parameters = self.prepare_parameters(parameters) - else: - trans_xsl = self.prepare_output("words") - stylesheet_parameters = {} - - # Complete the response. - - self.send_output(trans, [trans_xsl], words, stylesheet_parameters) - #from XSLTools import XSLOutput - #import sys - #proc = XSLOutput.Processor([trans_xsl], parameters=stylesheet_parameters) - #proc.send_output(sys.stderr, "iso-8859-1", words) - # Site map initialisation. def get_site(dict): @@ -164,6 +137,6 @@ def prepare_resources(): for cls in [DictionaryResource]: - XSLForms.Resources.WebResources.prepare_resources(cls) + xslforms_prepare_resources(cls) # vim: tabstop=4 expandtab shiftwidth=4 diff -r 0c70a001d6e8 -r 6484f8ab0220 examples/Common/Questionnaire/__init__.py --- a/examples/Common/Questionnaire/__init__.py Sun Feb 17 19:21:57 2008 +0000 +++ b/examples/Common/Questionnaire/__init__.py Sun Feb 17 19:22:28 2008 +0000 @@ -3,8 +3,8 @@ "A WebStack questionnaire application." import WebStack.Generic -import XSLForms.Resources.WebResources -import XSLForms.Utils +from XSLForms.Resources.WebResources import \ + XSLFormsResource, output, resources, prepare_resources as xslforms_prepare_resources import os # Site map imports. @@ -19,38 +19,27 @@ # Resource classes. -class QuestionnaireEditorResource(XSLForms.Resources.WebResources.XSLFormsResource): +class QuestionnaireEditorResource(XSLFormsResource): "A resource providing a questionnaire editor." - resource_dir = os.path.join(os.path.split(__file__)[0], "Resources") + resource_dir = resources(__file__) template_resources = { - "question" : ("question_template.xhtml", "question_output.xsl") + "questionnaire" : output("question_template.xhtml") } - def respond_to_form(self, trans, form): + def select_activity(self, trans, form): + form.set_activity("questionnaire") - """ - Respond to a request having the given transaction 'trans' and the given - 'form' information. - """ + def respond_to_input(self, trans, form): parameters = form.get_parameters() - documents = form.get_documents() selectors = form.get_selectors() - - # Ensure the presence of a document. + questionnaire = form.get_document() - if documents.has_key("questionnaire"): - questionnaire = documents["questionnaire"] - else: - questionnaire = form.new_instance("questionnaire") - - # Add and remove elements according to the selectors found. - - XSLForms.Utils.remove_elements(selectors.get("remove-question")) - XSLForms.Utils.add_elements(selectors.get("add-choice"), "choice") - XSLForms.Utils.remove_elements(selectors.get("remove-choice")) + self.remove_elements(selectors.get("remove-question")) + self.add_elements(selectors.get("add-choice"), "choice") + self.remove_elements(selectors.get("remove-choice")) # Add questions using the normal request parameter. @@ -64,23 +53,7 @@ if parameters.has_key("export"): trans.set_content_type(WebStack.Generic.ContentType("text/xml", encoding)) questionnaire.toStream(trans.get_response_stream(), trans.get_response_stream_encoding()) - - # When not exported, the data is transformed to produce a normal Web - # page. - - else: - - # Start the response. - - trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", encoding)) - - # Ensure that an output stylesheet exists. - - trans_xsl = self.prepare_output("question") - - # Complete the response. - - self.send_output(trans, [trans_xsl], questionnaire) + raise WebStack.Generic.EndOfResponse # Site map initialisation. @@ -108,6 +81,6 @@ def prepare_resources(): for cls in [QuestionnaireEditorResource]: - XSLForms.Resources.WebResources.prepare_resources(cls) + xslforms_prepare_resources(cls) # vim: tabstop=4 expandtab shiftwidth=4 diff -r 0c70a001d6e8 -r 6484f8ab0220 examples/Common/Recursive/__init__.py --- a/examples/Common/Recursive/__init__.py Sun Feb 17 19:21:57 2008 +0000 +++ b/examples/Common/Recursive/__init__.py Sun Feb 17 19:22:28 2008 +0000 @@ -3,9 +3,8 @@ "An example of recursive templates." import WebStack.Generic -import XSLForms.Resources.WebResources -import XSLForms.Utils -import os +from XSLForms.Resources.WebResources import \ + XSLFormsResource, input, output, resources, prepare_resources as xslforms_prepare_resources # Site map imports. @@ -17,60 +16,29 @@ # Resource classes. -class RecursiveResource(XSLForms.Resources.WebResources.XSLFormsResource): +class RecursiveResource(XSLFormsResource): "A resource providing a recursive hierarchy of editable fields." - resource_dir = os.path.join(os.path.split(__file__)[0], "Resources") + resource_dir = resources(__file__) template_resources = { - "recursive" : ("recursive_template.xhtml", "recursive_output.xsl") + "recursive" : output("recursive_template.xhtml") } init_resources = { - "recursive" : ("recursive_template.xhtml", "recursive_input.xsl") + "recursive" : input("recursive_template.xhtml") } - def respond_to_form(self, trans, form): - - """ - Respond to a request having the given transaction 'trans' and the given - 'form' information. - """ + def select_activity(self, trans, form): + form.set_activity("recursive") - parameters = form.get_parameters() - documents = form.get_documents() - - # Ensure the presence of a document. - - if documents.has_key("recursive"): - recursive = documents["recursive"] - else: - recursive = form.new_instance("recursive") + def respond_to_input(self, trans, form): # Add and remove elements according to the selectors found. selectors = form.get_selectors() - XSLForms.Utils.remove_elements(selectors.get("remove")) - XSLForms.Utils.add_elements(selectors.get("add-list"), "list") - XSLForms.Utils.add_elements(selectors.get("add-item"), "item") - - # Initialise the document, adding enumerations/ranges. - - init_xsl = self.prepare_initialiser("recursive") - recursive = self.get_result([init_xsl], recursive) - #print recursive.toString("iso-8859-1") - - # Start the response. - - trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", encoding)) - - # Ensure that an output stylesheet exists. - - trans_xsl = self.prepare_output("recursive") - stylesheet_parameters = {} - - # Complete the response. - - self.send_output(trans, [trans_xsl], recursive, stylesheet_parameters) + self.remove_elements(selectors.get("remove")) + self.add_elements(selectors.get("add-list"), "list") + self.add_elements(selectors.get("add-item"), "item") # Site map initialisation.