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.Selectors import EncodingSelector 14 from WebStack.Resources.Static import DirectoryResource 15 16 # Configuration settings. 17 18 encoding = "utf-8" 19 20 # Resource classes. 21 22 class VerySimpleResource(XSLForms.Resources.WebResources.XSLFormsResource): 23 24 "A very simple resource providing a hierarchy of editable fields." 25 26 resource_dir = os.path.join(os.path.split(__file__)[0], "Resources") 27 template_resources = { 28 #"structure" : ("structure_template.xhtml", "structure_output.xsl") 29 #"structure" : ("structure_multivalue_template.xhtml", "structure_output.xsl") 30 "structure" : ("structure_multivalue_label_template.xhtml", "structure_output.xsl") 31 } 32 init_resources = { 33 #"structure" : ("structure_template.xhtml", "structure_input.xsl") 34 #"structure" : ("structure_multivalue_template.xhtml", "structure_input.xsl") 35 "structure" : ("structure_multivalue_label_template.xhtml", "structure_input.xsl") 36 } 37 transform_resources = { 38 "comments" : ["structure_comments.xsl"] 39 } 40 document_resources = { 41 #"types" : "structure_types.xml" 42 "types" : "structure_types_label.xml", 43 "translations" : "translations.xml" 44 } 45 in_page_resources = { 46 "comments" : ("structure", "structure_output_comments.xsl", "comment-node") 47 } 48 49 def respond_to_form(self, trans, form): 50 51 """ 52 Respond to a request having the given transaction 'trans' and the given 53 'form' information. 54 """ 55 56 in_page_resource = self.get_in_page_resource(trans) 57 parameters = form.get_parameters() 58 documents = form.get_documents() 59 60 # Ensure the presence of a document. 61 62 if documents.has_key("structure"): 63 structure = documents["structure"] 64 else: 65 structure = form.new_instance("structure") 66 67 # Add and remove elements according to the selectors found. 68 69 selectors = form.get_selectors() 70 XSLForms.Utils.remove_elements(selectors.get("remove2")) 71 XSLForms.Utils.add_elements(selectors.get("add2"), "subitem") 72 XSLForms.Utils.remove_elements(selectors.get("remove")) 73 XSLForms.Utils.add_elements(selectors.get("add"), "item") 74 75 # Initialise the document, adding enumerations/ranges. 76 77 structure_xsl = self.prepare_initialiser("structure") 78 types_xml = self.prepare_document("types") 79 structure = self.get_result([structure_xsl], structure, references={"type" : types_xml}) 80 81 # Add the comments. 82 83 comments_xsl_list = self.prepare_transform("comments") 84 structure = self.get_result(comments_xsl_list, structure) 85 86 # Start the response. 87 88 trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", encoding)) 89 90 # Ensure that an output stylesheet exists. 91 92 if in_page_resource in self.in_page_resources.keys(): 93 trans_xsl = self.prepare_fragment(in_page_resource) 94 stylesheet_parameters = self.prepare_parameters(parameters) 95 else: 96 trans_xsl = self.prepare_output("structure") 97 stylesheet_parameters = {} 98 99 # Complete the response. 100 101 try: 102 stylesheet_parameters["locale"] = trans.get_content_languages()[0] 103 except IndexError: 104 pass 105 self.send_output(trans, [trans_xsl], structure, stylesheet_parameters, 106 references={"translations" : self.prepare_document("translations")}) 107 108 # Site map initialisation. 109 110 def get_site(): 111 112 "Return a simple Web site resource." 113 114 # Get the main resource and the directory used by the application. 115 116 very_simple_resource = VerySimpleResource() 117 directory = very_simple_resource.resource_dir 118 119 # Make a simple Web site. 120 121 resource = MapResource({ 122 # Static resources: 123 "scripts" : DirectoryResource(os.path.join(directory, "scripts"), {"js" : "text/javascript"}), 124 # Main page and in-page resources: 125 None : very_simple_resource 126 }) 127 128 return EncodingSelector(resource, encoding) 129 130 # Resource preparation ahead of time - useful for making installations. 131 132 def prepare_resources(): 133 for cls in [VerySimpleResource]: 134 XSLForms.Resources.WebResources.prepare_resources(cls) 135 136 # vim: tabstop=4 expandtab shiftwidth=4