XSLTools

examples/Common/Configurator/__init__.py

82:541381504c4d
2005-07-14 paulb [project @ 2005-07-14 21:39:35 by paulb] Changed the resource to be a higher-level XSLForms resource employing form objects and implementing the respond_to_form method.
     1 #!/usr/bin/env python     2      3 "A WebStack application for a system configurator."     4      5 import WebStack.Generic     6 import XSLForms.Output     7 import XSLForms.Prepare     8 import XSLForms.Utils     9 import XSLForms.Resources    10 import XSLOutput    11 import libxml2dom    12 import os    13     14 class ConfiguratorResource(XSLForms.Resources.XSLFormsResource):    15     16     "A resource providing a system configurator."    17     18     resource_dir = os.path.join(os.path.split(__file__)[0], "Resources")    19     encoding = "utf-8"    20     in_page_resources = {    21         "/cpu" : ("config_output_cpu.xsl", "cpu-node"),    22         "/memory" : ("config_output_memory.xsl", "memory-node"),    23         "/hard-disks" : ("config_output_harddisks.xsl", "hard-disks-node")    24         }    25     26     def respond_to_form(self, trans, form):    27     28         """    29         Respond to a request having the given transaction 'trans' and the given    30         'form' information.    31         """    32     33         path_info = trans.get_path_info()    34         parameters = form.get_parameters()    35         documents = form.get_documents()    36         selectors = form.get_selectors()    37     38         # Ensure the presence of a document.    39     40         if documents.has_key("configuration"):    41             configuration = documents["configuration"]    42         else:    43             configuration = form.new_instance("configuration")    44         #print "*", configuration.toString()    45     46         # Add and remove elements according to the selectors found.    47     48         XSLForms.Utils.add_elements(selectors.get("add-memory-unit"), "memory-unit")    49         XSLForms.Utils.remove_elements(selectors.get("remove-memory-unit"))    50         XSLForms.Utils.add_elements(selectors.get("add-storage-unit"), "storage-unit")    51         XSLForms.Utils.remove_elements(selectors.get("remove-storage-unit"))    52         XSLForms.Utils.add_elements(selectors.get("add-hard-disk"), "hard-disk")    53         XSLForms.Utils.remove_elements(selectors.get("remove-hard-disk"))    54     55         # Send a response according to certain parameters.    56         # When exported, an XML version of the data is returned.    57     58         if parameters.has_key("export"):    59             trans.set_content_type(WebStack.Generic.ContentType("text/xml", self.encoding))    60             configuration.toStream(trans.get_response_stream(), trans.get_response_stream_encoding())    61     62         # When not exported, the data is transformed to produce a normal Web    63         # page.    64     65         else:    66     67             # Transform, adding enumerations/ranges.    68     69             database_xsl = os.path.join(self.resource_dir, "config_database.xsl")    70             database_xml = os.path.join(self.resource_dir, "config_database.xml")    71             proc = XSLOutput.Processor([database_xsl], references={"database" : database_xml})    72             configuration = proc.get_result(configuration)    73     74             # Start the response.    75     76             trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", self.encoding))    77     78             # Define the stylesheet parameters.    79     80             stylesheet_parameters = {}    81     82             # Ensure that an output stylesheet exists.    83     84             if path_info in self.in_page_resources.keys():    85                 trans_xsl_filename, trans_xsl_region = self.in_page_resources[path_info]    86                 template_xml = os.path.join(self.resource_dir, "config_output.xsl")    87                 trans_xsl = os.path.join(self.resource_dir, trans_xsl_filename)    88                 XSLForms.Prepare.ensure_stylesheet_fragment(template_xml, trans_xsl, trans_xsl_region)    89                 element_path = parameters.get("element-path", [""])[0]    90                 stylesheet_parameters["element-path"] = element_path    91                 #print "*", repr(stylesheet_parameters["element-path"])    92             else:    93                 trans_xsl = os.path.join(self.resource_dir, "config_output.xsl")    94                 template_xml = os.path.join(self.resource_dir, "config_template.xhtml")    95                 XSLForms.Prepare.ensure_stylesheet(template_xml, trans_xsl)    96     97             stylesheet_parameters["application-url"] = \    98                 "http://%s:%s%s" % (trans.get_server_name(), trans.get_server_port(), trans.get_path_without_query())    99    100             # Complete the response.   101    102             proc = XSLOutput.Processor([trans_xsl], parameters=stylesheet_parameters)   103             proc.send_output(trans.get_response_stream(), trans.get_response_stream_encoding(),   104                 configuration)   105    106             #import sys   107             #proc = XSLOutput.Processor([trans_xsl], parameters=stylesheet_parameters)   108             #proc.send_output(sys.stderr, "iso-8859-1", configuration)   109    110 # vim: tabstop=4 expandtab shiftwidth=4