DLESE Tools
v1.6.0

org.dlese.dpc.xml
Class XSLTransformer

java.lang.Object
  extended by org.dlese.dpc.xml.XSLTransformer

public class XSLTransformer
extends Object

Transforms files or Strings using XSL stylesheets. In general, Transformer objects are not thread safe, so external synchornization should be applied in concurrent environments such as servlets. Note: some members of this class can be run as a stand-alone class that can be invoked from the command line or from a servlet environment.

Version:
$Id: XSLTransformer.java,v 1.20 2009/03/20 23:34:01 jweather Exp $
Author:
John Weatherley

Constructor Summary
XSLTransformer()
          Constructor does nothing.
 
Method Summary
static String encodeCharsInString(String stg)
          Substitutes escape chars for certain sensitive characthers that don't play well in file names or file paths.
static String encodeStringIntoHex(String stg)
          Substitutes hex values for all characters EXCEPT digits, letters, and the chars shown below.
protected static String getDateStamp()
          Return a string for the current time and date, sutiable for display in log files and output to standout:
static Transformer getTransformer(String xslFilePath)
          Gets a Transformer used to transform XML using a given XSL stylesheet.
static Transformer getTransformer(String xslFilePath, String transformerFactoryClass)
          Gets a specific Transformer, used to transform XML using a given XSL stylesheet.
static Transformer getTransformerFromXSLString(String xslString)
          Gets a Transformer used to transform XML using a given XSL stylesheet.
static String localizeXml(String xmlString)
          Removes all namespace information from XML.
protected static void prtln(String s)
          Output a line of text to standard out, with datestamp, if debug is set to true.
protected static void prtlnErr(String s)
          Output a line of text to error out, with datestamp.
protected static void prtlnErr(String s, Throwable t)
          Output a line of text to error out, with datestamp.
protected static void prtlnErr(Throwable t)
          Output a line of text to error out, with datestamp.
static void setDebug(boolean db)
          Sets the debug attribute of the object
 StringBuffer transform(String xslFilePath, String inputFilesDir, String outputFilesDir)
          Perform the transform on each file in the inputFilesDir, using the given xsl stylesheet, placing the resulting transformed files into outpuFilesDir.
 StringBuffer transform(String xslFilePath, String inputFilesDir, String[] inputFileNames, String outputFilesDir, Date modifiedSinceDate)
          Perform the transform files in inputFilesDir corresponding to the inputFileNames passed in, using the given xsl stylesheet, placing the resulting transformed files into outpuFilesDir.
static String transformFile(File inputFile, Transformer transformer)
          Transforms an XML file using a pre-compiled Transformer.
static String transformFile(String inputFilePath, String xslFilePath)
          Transforms an XML file using an XSL stylesheet.
static String transformFile(String inputFilePath, Transformer transformer)
          Transforms an XML file using a pre-compiled Transformer.
static StringWriter transformFileToWriter(String inputFilePath, String xslFilePath)
          Transforms an XML file using an XSL stylesheet.
static StringWriter transformFileToWriter(String inputFilePath, Transformer transformer)
          Transforms an XML file using a pre-compiled Transformer.
static String transformString(String xmlString, String xslFilePath)
          Transforms an XML String using an XSL stylesheet.
static String transformString(String xmlString, Transformer transformer)
          Transforms an XML String using a pre-compiled Transformer.
static StringWriter transformStringToWriter(String xmlString, String xslFilePath)
          Transforms an XML String using an XSL stylesheet.
static StringWriter transformStringToWriter(String xmlString, Transformer transformer)
          Transforms an XML String using a pre-compiled Transformer.
static String transformStringUsingString(String xmlString, String xslString)
          Transforms an XML String using an XSL stylesheet supplied as a String.
static void transformToFile(File inputFile, FileOutputStream fos, Transformer transformer)
          Transform a single file to the given FileOutputStream using the given transformer.
static void transformToFile(File inputFile, File outputFile, Transformer transformer)
          Transform a single file to the given output file using the given transformer.
static void transformToFile(String inputFilePath, String outputFilePath, Transformer transformer)
          Transform a single file to a file at the given output path using the given transformer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XSLTransformer

public XSLTransformer()
Constructor does nothing. All processing done in the run() or static stand-alone methods.

Method Detail

transform

public StringBuffer transform(String xslFilePath,
                              String inputFilesDir,
                              String outputFilesDir)
                       throws Exception
Perform the transform on each file in the inputFilesDir, using the given xsl stylesheet, placing the resulting transformed files into outpuFilesDir.

Parameters:
xslFilePath - Path to an XSL stylesheet.
inputFilesDir - Path to a directory of XML files.
outputFilesDir - Path to a directory where transformed Files will be saved.
Returns:
A StringBuffer containing a descriptive report about the trasform.
Throws:
Exception - If error.

transform

public StringBuffer transform(String xslFilePath,
                              String inputFilesDir,
                              String[] inputFileNames,
                              String outputFilesDir,
                              Date modifiedSinceDate)
                       throws Exception
Perform the transform files in inputFilesDir corresponding to the inputFileNames passed in, using the given xsl stylesheet, placing the resulting transformed files into outpuFilesDir. If inputFileNames is null, all .xml files found in the inputFilesDir are processed.

Parameters:
xslFilePath - Path to an XSL stylesheet.
inputFilesDir - Path to a directory of XML files.
inputFileNames - An array of file names or null to get the file names from the files found in inputFilesDir
outputFilesDir - Path to a directory where transformed Files will be saved.
modifiedSinceDate - Indicates to perform the transform only if the original file has been modified since the given date, or null to perform the transform regardless of modification date.
Returns:
A StringBuffer containing a descriptive report about the trasform.
Throws:
Exception - If error.

transformToFile

public static final void transformToFile(String inputFilePath,
                                         String outputFilePath,
                                         Transformer transformer)
                                  throws Exception
Transform a single file to a file at the given output path using the given transformer.

Parameters:
inputFilePath - The input file to transform.
outputFilePath - The output file where transformed content will be saved.
transformer - The Transformer used to perform the transform.
Throws:
Exception - If unable to perform the transform.

transformToFile

public static final void transformToFile(File inputFile,
                                         File outputFile,
                                         Transformer transformer)
                                  throws Exception
Transform a single file to the given output file using the given transformer.

Parameters:
inputFile - The input file to transform.
outputFile - The output file where transformed content will be saved.
transformer - The Transformer used to perform the transform.
Throws:
Exception - If unable to perform the transform.

transformToFile

public static final void transformToFile(File inputFile,
                                         FileOutputStream fos,
                                         Transformer transformer)
                                  throws Exception
Transform a single file to the given FileOutputStream using the given transformer.

Parameters:
inputFile - The input file to transform.
fos - The FileOutputStream where transformed content will be saved.
transformer - The Transformer used to perform the transform.
Throws:
Exception - If unable to perform the transform.

transformFile

public static final String transformFile(String inputFilePath,
                                         Transformer transformer)
Transforms an XML file using a pre-compiled Transformer. Use getTransformer(String xslFilePath) to produce a reusable Transformer for a given XSL stylesheet.

Parameters:
inputFilePath - The XML file to transform.
transformer - A pre-compiled Transformer used to produce transformed output.
Returns:
A String containing the transformed content.

transformFile

public static final String transformFile(File inputFile,
                                         Transformer transformer)
Transforms an XML file using a pre-compiled Transformer. Use getTransformer(String xslFilePath) to produce a reusable Transformer for a given XSL stylesheet.

Parameters:
inputFile - The XML file to transform.
transformer - A pre-compiled Transformer used to produce transformed output.
Returns:
A String containing the transformed content.

transformFileToWriter

public static final StringWriter transformFileToWriter(String inputFilePath,
                                                       Transformer transformer)
Transforms an XML file using a pre-compiled Transformer. Use getTransformer(String xslFilePath) to produce a reusable Transformer for a given XSL stylesheet. To convert the resulting StringWriter to a String, call StringWriter.toString().

Parameters:
inputFilePath - The XML file to transform.
transformer - A pre-compiled Transformer used to produce transformed output.
Returns:
A StringWriter containing the transformed content.

transformString

public static final String transformString(String xmlString,
                                           Transformer transformer)
Transforms an XML String using a pre-compiled Transformer. Use getTransformer(String xslFilePath) to produce a reusable Transformer for a given XSL stylesheet.

Parameters:
xmlString - The XML String to transform.
transformer - A pre-compiled Transformer used to produce transformed output.
Returns:
A String containing the transformed content.

transformStringToWriter

public static final StringWriter transformStringToWriter(String xmlString,
                                                         Transformer transformer)
Transforms an XML String using a pre-compiled Transformer. Use getTransformer(String xslFilePath) to produce a reusable Transformer for a given XSL stylesheet. To convert the resulting StringWriter to a String, call StringWriter.toString().

Parameters:
xmlString - The XML String to transform.
transformer - A pre-compiled Transformer used to produce transformed output.
Returns:
A StringWriter containing the transformed content.

transformFile

public static final String transformFile(String inputFilePath,
                                         String xslFilePath)
Transforms an XML file using an XSL stylesheet.

Parameters:
inputFilePath - The XML file to transform.
xslFilePath - The XSL file used to perform the transform.
Returns:
A String containing the transformed content.

transformFileToWriter

public static final StringWriter transformFileToWriter(String inputFilePath,
                                                       String xslFilePath)
Transforms an XML file using an XSL stylesheet. To convert the resulting StringWriter to a String, call StringWriter.toString().

Parameters:
inputFilePath - The XML file to transform.
xslFilePath - The XSL file used to perform the transform.
Returns:
A StringWriter containing the transformed content.

transformString

public static final String transformString(String xmlString,
                                           String xslFilePath)
Transforms an XML String using an XSL stylesheet.

Parameters:
xmlString - The XML String to transform.
xslFilePath - The XSL file used to perform the transform.
Returns:
A String containing the transformed content.

transformStringUsingString

public static final String transformStringUsingString(String xmlString,
                                                      String xslString)
Transforms an XML String using an XSL stylesheet supplied as a String.

Parameters:
xmlString - The XML String to transform.
xslString - The XSL String used to define the transform.
Returns:
A String containing the transformed content.

localizeXml

public static final String localizeXml(String xmlString)
Removes all namespace information from XML.

Parameters:
xmlString - The XML String to strip of namespaces
Returns:
A String containing XML without namespaces
See Also:
#XSLUtils.getRemoveNamespacesXSL()

transformStringToWriter

public static final StringWriter transformStringToWriter(String xmlString,
                                                         String xslFilePath)
Transforms an XML String using an XSL stylesheet. To convert the resulting StringWriter to a String, call StringWriter.toString().

Parameters:
xmlString - The XML String to transform.
xslFilePath - The XSL file used to perform the transform.
Returns:
A StringWriter containing the transformed content.

getTransformer

public static Transformer getTransformer(String xslFilePath)
                                  throws TransformerConfigurationException,
                                         FileNotFoundException
Gets a Transformer used to transform XML using a given XSL stylesheet. For efficiency, one Transformer should be used to transform multiple XMLs from a single stylesheet.

Parameters:
xslFilePath - A path to an XSL stylesheet file.
Returns:
A Transformer used to transform XML using a given stylesheet.
Throws:
TransformerConfigurationException - If error.
FileNotFoundException - If file can not be found.

getTransformer

public static Transformer getTransformer(String xslFilePath,
                                         String transformerFactoryClass)
                                  throws TransformerConfigurationException,
                                         FileNotFoundException
Gets a specific Transformer, used to transform XML using a given XSL stylesheet.

Parameters:
xslFilePath - A path to an XSL stylesheet file.
transformerFactoryClass - TransformerFactory class (e.g. "net.sf.saxon.TransformerFactoryImpl")
Returns:
A Transformer used to transform XML using a given stylesheet.
Throws:
TransformerConfigurationException - If error.
FileNotFoundException - If file can not be found.

getTransformerFromXSLString

public static final Transformer getTransformerFromXSLString(String xslString)
                                                     throws TransformerConfigurationException
Gets a Transformer used to transform XML using a given XSL stylesheet. For efficiency, one Transformer should be used to transform multiple XMLs from a single stylesheet.

Parameters:
xslString - XSL supplied as a String
Returns:
A Transformer used to transform XML using a given stylesheet.
Throws:
TransformerConfigurationException - If error.

encodeStringIntoHex

public static String encodeStringIntoHex(String stg)
Substitutes hex values for all characters EXCEPT digits, letters, and the chars shown below. Output is of the form %HEX.

Excepted chars include digits, letters and: - _ . %

Note: Unix commands cannot include the following chars: * ? ! | \ / ' " { } < > ; , ^ ( ) $ ~ Windows file names may not contain: \ / : * ? " < > |

Parameters:
stg - A String to encode.
Returns:
An encoded String.

encodeCharsInString

public static String encodeCharsInString(String stg)
Substitutes escape chars for certain sensitive characthers that don't play well in file names or file paths. Note:

Unix commands cannot include the following chars: * ? ! | \ / ' " { } < > ; , ^ ( ) $ ~

Windows file names may not contain: \ / : * ? " < > |

Parameters:
stg - A String to encode.
Returns:
An encoded String.

getDateStamp

protected static final String getDateStamp()
Return a string for the current time and date, sutiable for display in log files and output to standout:

Returns:
The dateStamp value

prtlnErr

protected static final void prtlnErr(String s)
Output a line of text to error out, with datestamp.

Parameters:
s - The text that will be output to error out.

prtlnErr

protected static final void prtlnErr(String s,
                                     Throwable t)
Output a line of text to error out, with datestamp.

Parameters:
s - The text that will be output to error out.
t - The Throwable to output with message

prtlnErr

protected static final void prtlnErr(Throwable t)
Output a line of text to error out, with datestamp.

Parameters:
t - The Throwable to output with message

prtln

protected static void prtln(String s)
Output a line of text to standard out, with datestamp, if debug is set to true.

Parameters:
s - The String that will be output.

setDebug

public static void setDebug(boolean db)
Sets the debug attribute of the object

Parameters:
db - The new debug value

DLESE Tools
v1.6.0