#!/usr/bin/python # This module contains some helper functions to keep the length of the # files that use this module within reasonable bounds. The functions # all provide standard functionality needed to work with ceXML and # xml.apache.org's Xalan parser and Xerces XSLT processor. import tempfile import os # initialise tempfile's first part of the temporary filenames tempfile.template = "cexmlhelper" # Start a list containing the generated temporary files, allowing for # a clean-up when they're not needed anymore list_of_tempfiles = [] def set_linux_classpath(): # Sets the classpath. This is a specific implementation for my # personal linux system, someday this will be more # generic. Perhaps I'll even write a nice wrapper for more # systems :-) os.putenv("CLASSPATH","/usr/lib/jdk1.1/lib/classes.zip:"+ "/usr/share/java/xerces.jar:/usr/share/java/xalan.jar") def process_with_xslt(in_file, xslt_file, parameters=[]): # feeds "in_file" to "xslt_file" using the optional # "parameters". The "parameters" must be a python list of # key-value tuples. The return value is the name of the temporary # file containing the output. commandline = "java org.apache.xalan.xslt.Process" # Add the in_file commandline = commandline + " -in %s" % in_file # Add the xslt_file commandline = commandline + " -xsl %s" % xslt_file # Generate a temporary filename and add it as the process' output out_file = tempfile.mktemp() list_of_tempfiles.append(out_file) commandline = commandline + " -out %s" % out_file if len(parameters) > 0: i = 0 while i < len(parameters): commandline=commandline+" -param %s %s" % parameters[i] i=i+1 # add some stuff to suppress annoying output commandline = commandline + " -Q -QC" # run the commandline os.system(commandline) # return the output filename (a temporary one) return out_file def print_xml_header(): # Prints the header needed to send the file over HTTP. Normally # not needed, because print_xml_file() calls it automatically, but # you never know... os.system("echo Content-type: text/xml") os.system("echo") def print_html_header(): # Prints the header needed to send the file over HTTP. Normally # not needed, because print_html_file() calls it automatically, but # you never know... os.system("echo Content-type: text/html") os.system("echo") def print_xml_file(xml_file): # Prints the xml header and the xml_file print_xml_header() os.system("cat %s" % xml_file) def print_html_file(html_file): # Prints the html header and the html_file print_html_header() os.system("cat %s" % html_file) def remove_temporary_files(): i = 0 while i < len(list_of_tempfiles): os.system("rm %s" % list_of_tempfiles[i]) i=i+1 |