XSLTools

Annotated XSLForms/Prepare.py

178:7e7d9dbcec62
2005-07-22 paulb [project @ 2005-07-22 18:26:38 by paulb] Tidied up the function names and added some API documentation. Added child-element and child-attribute functions for coherent references to potentially non-existent nodes in the form data.
paulb@1 1
#!/usr/bin/env python
paulb@1 2
paulb@1 3
"""
paulb@1 4
Preparation of templating stylesheets.
paulb@116 5
paulb@116 6
Copyright (C) 2005 Paul Boddie <paul@boddie.org.uk>
paulb@116 7
paulb@116 8
This library is free software; you can redistribute it and/or
paulb@116 9
modify it under the terms of the GNU Lesser General Public
paulb@116 10
License as published by the Free Software Foundation; either
paulb@116 11
version 2.1 of the License, or (at your option) any later version.
paulb@116 12
paulb@116 13
This library is distributed in the hope that it will be useful,
paulb@116 14
but WITHOUT ANY WARRANTY; without even the implied warranty of
paulb@116 15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
paulb@116 16
Lesser General Public License for more details.
paulb@116 17
paulb@116 18
You should have received a copy of the GNU Lesser General Public
paulb@116 19
License along with this library; if not, write to the Free Software
paulb@116 20
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
paulb@1 21
"""
paulb@1 22
paulb@1 23
import XSLOutput
paulb@1 24
import libxml2dom
paulb@1 25
import os
paulb@1 26
paulb@1 27
resource_dir = os.path.join(os.path.split(__file__)[0], "XSL")
paulb@1 28
paulb@1 29
def make_stylesheet(template_name, output_name, stylesheet_name="Prepare.xsl", encoding="utf-8"):
paulb@1 30
    global resource_dir
paulb@1 31
    proc = XSLOutput.Processor([os.path.join(resource_dir, stylesheet_name)])
paulb@1 32
    template = libxml2dom.parse(template_name)
paulb@1 33
    proc.send_output(open(output_name, "wb"), encoding, template)
paulb@1 34
paulb@49 35
def make_stylesheet_fragment(template_name, output_name, element_id, stylesheet_name="Extract.xsl", encoding="utf-8"):
paulb@49 36
    global resource_dir
paulb@49 37
    proc = XSLOutput.Processor([os.path.join(resource_dir, stylesheet_name)], parameters={"element-id" : element_id})
paulb@49 38
    template = libxml2dom.parse(template_name)
paulb@49 39
    proc.send_output(open(output_name, "wb"), encoding, template)
paulb@49 40
paulb@49 41
def ensure_stylesheet(template_name, output_name):
paulb@49 42
    if not os.path.exists(output_name) or \
paulb@49 43
        os.path.getmtime(output_name) < os.path.getmtime(template_name):
paulb@49 44
paulb@49 45
        make_stylesheet(template_name, output_name)
paulb@49 46
paulb@49 47
def ensure_stylesheet_fragment(template_name, output_name, element_id):
paulb@49 48
    if not os.path.exists(output_name) or \
paulb@49 49
        os.path.getmtime(output_name) < os.path.getmtime(template_name):
paulb@49 50
paulb@49 51
        make_stylesheet_fragment(template_name, output_name, element_id)
paulb@49 52
paulb@1 53
# vim: tabstop=4 expandtab shiftwidth=4