1 #!/usr/bin/env python 2 3 "A very simple 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 VerySimpleResource(XSLForms.Resources.WebResources.XSLFormsResource): 18 19 "A very simple resource providing a hierarchy of editable fields." 20 21 resource_dir = os.path.join(os.path.split(__file__)[0], "Resources") 22 encoding = "utf-8" 23 template_resources = { 24 #"structure" : ("structure_template.xhtml", "structure_output.xsl") 25 "structure" : ("structure_multivalue_template.xhtml", "structure_output.xsl") 26 } 27 init_resources = { 28 #"structure" : ("structure_template.xhtml", "structure_input.xsl") 29 "structure" : ("structure_multivalue_template.xhtml", "structure_input.xsl") 30 } 31 transform_resources = { 32 "comments" : ["structure_comments.xsl"] 33 } 34 document_resources = { 35 "types" : "structure_types.xml" 36 } 37 in_page_resources = { 38 "comments" : ("structure_output_comments.xsl", "comment-node") 39 } 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("structure"): 55 structure = documents["structure"] 56 else: 57 structure = form.new_instance("structure") 58 59 # Add and remove elements according to the selectors found. 60 61 selectors = form.get_selectors() 62 XSLForms.Utils.remove_elements(selectors.get("remove2")) 63 XSLForms.Utils.add_elements(selectors.get("add2"), "subitem") 64 XSLForms.Utils.remove_elements(selectors.get("remove")) 65 XSLForms.Utils.add_elements(selectors.get("add"), "item") 66 67 # Initialise the document, adding enumerations/ranges. 68 69 structure_xsl = self.prepare_initialiser("structure") 70 types_xml = self.prepare_document("types") 71 structure = self.get_result([structure_xsl], structure, references={"type" : types_xml}) 72 73 # Add the comments. 74 75 comments_xsl_list = self.prepare_transform("comments") 76 structure = self.get_result(comments_xsl_list, structure) 77 78 # Start the response. 79 80 trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", self.encoding)) 81 82 # Ensure that an output stylesheet exists. 83 84 if in_page_resource in self.in_page_resources.keys(): 85 trans_xsl = self.prepare_fragment("structure", in_page_resource) 86 stylesheet_parameters = self.prepare_parameters(parameters) 87 else: 88 trans_xsl = self.prepare_output("structure") 89 stylesheet_parameters = {} 90 91 # Complete the response. 92 93 self.send_output(trans, [trans_xsl], structure, stylesheet_parameters) 94 95 # Site map initialisation. 96 97 def get_site(): 98 99 "Return a simple Web site resource." 100 101 # Get the main resource and the directory used by the application. 102 103 very_simple_resource = VerySimpleResource() 104 directory = very_simple_resource.resource_dir 105 106 # Make a simple Web site. 107 108 resource = MapResource({ 109 # Static resources: 110 "scripts" : DirectoryResource(os.path.join(directory, "scripts"), {"js" : "text/javascript"}), 111 # Main page and in-page resources: 112 None : very_simple_resource 113 }) 114 115 return resource 116 117 # vim: tabstop=4 expandtab shiftwidth=4