paulb@122 | 1 | #!/usr/bin/env python |
paulb@122 | 2 | |
paulb@122 | 3 | "A very simple example application." |
paulb@122 | 4 | |
paulb@122 | 5 | import WebStack.Generic |
paulb@122 | 6 | import XSLForms.Resources |
paulb@122 | 7 | import XSLForms.Utils |
paulb@136 | 8 | import XSLOutput |
paulb@122 | 9 | import os |
paulb@122 | 10 | |
paulb@122 | 11 | # Resource classes. |
paulb@122 | 12 | |
paulb@122 | 13 | class VerySimpleResource(XSLForms.Resources.XSLFormsResource): |
paulb@122 | 14 | |
paulb@122 | 15 | "A very simple resource providing a hierarchy of editable fields." |
paulb@122 | 16 | |
paulb@122 | 17 | resource_dir = os.path.join(os.path.split(__file__)[0], "Resources") |
paulb@122 | 18 | encoding = "utf-8" |
paulb@122 | 19 | template_resources = { |
paulb@122 | 20 | "structure" : ("structure_template.xhtml", "structure_output.xsl") |
paulb@122 | 21 | } |
paulb@122 | 22 | |
paulb@122 | 23 | def respond_to_form(self, trans, form): |
paulb@122 | 24 | |
paulb@122 | 25 | """ |
paulb@122 | 26 | Respond to a request having the given transaction 'trans' and the given |
paulb@122 | 27 | 'form' information. |
paulb@122 | 28 | """ |
paulb@122 | 29 | |
paulb@122 | 30 | documents = form.get_documents() |
paulb@122 | 31 | |
paulb@122 | 32 | # Ensure the presence of a document. |
paulb@122 | 33 | |
paulb@122 | 34 | if documents.has_key("structure"): |
paulb@122 | 35 | structure = documents["structure"] |
paulb@122 | 36 | else: |
paulb@122 | 37 | structure = form.new_instance("structure") |
paulb@122 | 38 | |
paulb@122 | 39 | # Add and remove elements according to the selectors found. |
paulb@122 | 40 | |
paulb@122 | 41 | selectors = form.get_selectors() |
paulb@122 | 42 | XSLForms.Utils.remove_elements(selectors.get("remove2")) |
paulb@122 | 43 | XSLForms.Utils.add_elements(selectors.get("add2"), "subitem") |
paulb@122 | 44 | XSLForms.Utils.remove_elements(selectors.get("remove")) |
paulb@122 | 45 | XSLForms.Utils.add_elements(selectors.get("add"), "item") |
paulb@122 | 46 | |
paulb@136 | 47 | # Transform, adding enumerations/ranges. |
paulb@136 | 48 | |
paulb@136 | 49 | types_xsl = os.path.join(self.resource_dir, "structure_types.xsl") |
paulb@136 | 50 | types_xml = os.path.join(self.resource_dir, "structure_types.xml") |
paulb@136 | 51 | proc = XSLOutput.Processor([types_xsl], references={"types" : types_xml}) |
paulb@136 | 52 | structure = proc.get_result(structure) |
paulb@136 | 53 | |
paulb@122 | 54 | # Start the response. |
paulb@122 | 55 | |
paulb@122 | 56 | trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", self.encoding)) |
paulb@122 | 57 | |
paulb@122 | 58 | # Ensure that an output stylesheet exists. |
paulb@122 | 59 | |
paulb@122 | 60 | trans_xsl = self.prepare_output("structure") |
paulb@122 | 61 | |
paulb@122 | 62 | # Complete the response. |
paulb@122 | 63 | |
paulb@122 | 64 | self.send_output(trans, [trans_xsl], structure) |
paulb@122 | 65 | |
paulb@122 | 66 | # Site map initialisation. |
paulb@122 | 67 | |
paulb@122 | 68 | def get_site(): |
paulb@122 | 69 | |
paulb@122 | 70 | "Return a simple Web site resource." |
paulb@122 | 71 | |
paulb@122 | 72 | return VerySimpleResource() |
paulb@122 | 73 | |
paulb@122 | 74 | # vim: tabstop=4 expandtab shiftwidth=4 |