XSLTools

XSLForms/Prepare.py

260:03cee0e46a02
2005-10-03 paulb [project @ 2005-10-03 00:53:24 by paulb] Introduced input/initialiser stylesheets in place of the types stylesheets previously used.
     1 #!/usr/bin/env python     2      3 """     4 Preparation of templating stylesheets.     5      6 Copyright (C) 2005 Paul Boddie <paul@boddie.org.uk>     7      8 This library is free software; you can redistribute it and/or     9 modify it under the terms of the GNU Lesser General Public    10 License as published by the Free Software Foundation; either    11 version 2.1 of the License, or (at your option) any later version.    12     13 This library is distributed in the hope that it will be useful,    14 but WITHOUT ANY WARRANTY; without even the implied warranty of    15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    16 Lesser General Public License for more details.    17     18 You should have received a copy of the GNU Lesser General Public    19 License along with this library; if not, write to the Free Software    20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA    21 """    22     23 from XSLTools import XSLOutput    24 import libxml2dom    25 import os    26     27 resource_dir = os.path.join(os.path.split(__file__)[0], "XSL")    28     29 def make_stylesheet(template_name, output_name, stylesheet_names=["PrepareMacro.xsl", "Prepare.xsl"], encoding=None):    30     31     """    32     Make an output stylesheet using the file with the given 'template_name',    33     producing a file with the given 'output_name'.    34     """    35     36     stylesheets = [os.path.join(resource_dir, stylesheet_name) for stylesheet_name in stylesheet_names]    37     proc = XSLOutput.Processor(stylesheets)    38     template = libxml2dom.parse(template_name)    39     proc.send_output(open(output_name, "wb"), encoding, template)    40     41 def make_stylesheet_fragment(template_name, output_name, element_id, stylesheet_name="Extract.xsl", encoding=None):    42     43     """    44     Make an output stylesheet using the file with the given 'template_name',    45     producing a file with the given 'output_name', capturing the fragment    46     identified by the given 'element_id'.    47     """    48     49     proc = XSLOutput.Processor([os.path.join(resource_dir, stylesheet_name)], parameters={"element-id" : element_id})    50     template = libxml2dom.parse(template_name)    51     proc.send_output(open(output_name, "wb"), encoding, template)    52     53 def ensure_stylesheet(template_name, output_name):    54     55     """    56     Ensure the presence of an output stylesheet, preparing it if necessary    57     using the file with the given 'template_name', producing a file with the    58     given 'output_name'.    59     """    60     61     if not os.path.exists(output_name) or \    62         os.path.getmtime(output_name) < os.path.getmtime(template_name):    63     64         make_stylesheet(template_name, output_name)    65     66 def ensure_stylesheet_fragment(template_name, output_name, element_id):    67     68     """    69     Ensure the presence of an output stylesheet, preparing it if necessary    70     using the file with the given 'template_name', producing a file with the    71     given 'output_name', capturing the fragment identified by the given    72     'element_id'.    73     """    74     75     if not os.path.exists(output_name) or \    76         os.path.getmtime(output_name) < os.path.getmtime(template_name):    77     78         make_stylesheet_fragment(template_name, output_name, element_id)    79     80 def make_input_stylesheet(template_name, input_name, stylesheet_names=["Schema.xsl", "Input.xsl"], encoding=None):    81     82     """    83     Make an input stylesheet using the file with the given 'template_name',    84     producing a file with the given 'input_name'. Such stylesheets are used to    85     ensure the general structure of an input document.    86     """    87     88     stylesheets = [os.path.join(resource_dir, stylesheet_name) for stylesheet_name in stylesheet_names]    89     proc = XSLOutput.Processor(stylesheets)    90     template = libxml2dom.parse(template_name)    91     proc.send_output(open(input_name, "wb"), encoding, template)    92     93 def ensure_input_stylesheet(template_name, input_name):    94     95     """    96     Ensure the presence of an input stylesheet, preparing it if necessary    97     using the file with the given 'template_name', producing a file with the    98     given 'input_name'.    99     """   100    101     if not os.path.exists(input_name) or \   102         os.path.getmtime(input_name) < os.path.getmtime(template_name):   103    104         make_input_stylesheet(template_name, input_name)   105    106 # vim: tabstop=4 expandtab shiftwidth=4