1.1 --- a/XSLForms/Resources.py Thu Jul 14 21:39:35 2005 +0000
1.2 +++ b/XSLForms/Resources.py Thu Jul 14 22:24:23 2005 +0000
1.3 @@ -4,13 +4,51 @@
1.4
1.5 import WebStack.Generic
1.6 import XSLForms.Fields
1.7 +import XSLForms.Prepare
1.8 +import XSLForms.Output
1.9 +import XSLOutput
1.10 +import os
1.11
1.12 class XSLFormsResource:
1.13
1.14 - "A generic XSLForms resource for use with WebStack."
1.15 + """
1.16 + A generic XSLForms resource for use with WebStack.
1.17 +
1.18 + When overriding this class, define the following attributes appropriately:
1.19 +
1.20 + * template_resources - a dictionary mapping output identifiers to
1.21 + (template_filename, output_filename) tuples,
1.22 + indicating the template and stylesheet filenames
1.23 + to be employed
1.24 +
1.25 + * in_page_resources - a dictionary mapping fragment identifiers to
1.26 + (output_filename, node_identifier) tuples,
1.27 + indicating the stylesheet filename to be
1.28 + employed, along with the node identifier used in
1.29 + the original template and output documents to
1.30 + mark a region of those documents as the fragment
1.31 + to be updated upon "in-page" requests
1.32 +
1.33 + * resource_dir - the absolute path of the directory in which
1.34 + stylesheet resources are to reside
1.35 +
1.36 + All filenames shall be simple leafnames for files residing in the resource's
1.37 + special resource directory 'resource_dir'.
1.38 +
1.39 + The following attributes may also be specified:
1.40 +
1.41 + * path_encoding - the assumed encoding of characters in request
1.42 + paths
1.43 +
1.44 + * encoding - the assumed encoding of characters in request
1.45 + bodies
1.46 + """
1.47
1.48 path_encoding = "iso-8859-1"
1.49 encoding = "utf-8"
1.50 + template_resources = {}
1.51 + in_page_resources = {}
1.52 + resource_dir = None
1.53
1.54 def get_fields_from_body(self, trans, encoding):
1.55
1.56 @@ -33,14 +71,60 @@
1.57 parameters[text_name].append(text_value)
1.58 return parameters
1.59
1.60 + def prepare_output(self, output_identifier):
1.61 +
1.62 + """
1.63 + Prepare the output stylesheets using the given 'output_identifier' to
1.64 + indicate which templates and stylesheets are to be employed in the
1.65 + production of output from the resource.
1.66 +
1.67 + The 'output_identifier' is used as a key to the 'template_resources'
1.68 + dictionary attribute.
1.69 + """
1.70 +
1.71 + template_filename, output_filename = self.template_resources[output_identifier]
1.72 + output_path = os.path.join(self.resource_dir, output_filename)
1.73 + template_path = os.path.join(self.resource_dir, template_filename)
1.74 + XSLForms.Prepare.ensure_stylesheet(template_path, output_path)
1.75 + return output_path
1.76 +
1.77 + def prepare_fragment(self, output_identifier, fragment_identifier):
1.78 +
1.79 + """
1.80 + Prepare the output stylesheets for the given 'output_identifier' and
1.81 + 'fragment_identifier', indicating which templates and stylesheets are to
1.82 + be employed in the production of output from the resource.
1.83 +
1.84 + The 'output_identifier' is used as a key to the 'template_resources'
1.85 + dictionary attribute; the 'fragment_identifier' is used as a key to the
1.86 + 'in_page_resources' dictionary attribute.
1.87 + """
1.88 +
1.89 + output_path = self.prepare_output(output_identifier)
1.90 + fragment_filename, node_identifier = self.in_page_resources[fragment_identifier]
1.91 + fragment_path = os.path.join(self.resource_dir, fragment_filename)
1.92 + XSLForms.Prepare.ensure_stylesheet_fragment(output_path, fragment_path, node_identifier)
1.93 + return fragment_path
1.94 +
1.95 + def send_output(self, trans, stylesheet_filenames, stylesheet_parameters, document):
1.96 +
1.97 + """
1.98 + Send the output from the resource to the user employing the transaction
1.99 + 'trans', stylesheets having the given 'stylesheet_filenames', parameters
1.100 + as defined in the 'stylesheet_parameters' dictionary, and the 'document'
1.101 + upon which the output will be based.
1.102 + """
1.103 +
1.104 + proc = XSLOutput.Processor(stylesheet_filenames, parameters=stylesheet_parameters)
1.105 + proc.send_output(trans.get_response_stream(), trans.get_response_stream_encoding(),
1.106 + document)
1.107 +
1.108 def respond(self, trans):
1.109
1.110 """
1.111 Respond to the request described by the given transaction 'trans'.
1.112 """
1.113
1.114 - global XSLForms # NOTE: Strangely required to avoid UnboundLocalError!
1.115 -
1.116 # Only obtain field information according to the stated method.
1.117
1.118 method = trans.get_request_method()