DLESE Tools
v1.2

org.dlese.dpc.xml
Class XSLTransformer

java.lang.Object
  extended byorg.dlese.dpc.xml.XSLTransformer

public class XSLTransformer
extends Object

Transforms files or Strings using XSL stylesheets. 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.

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.
static Transformer getTransformer(String xslFilePath)
          Gets a Transformer used to transform XML using a given XSL stylesheet.
 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)
          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 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)
                       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.
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.

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.

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 final 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.

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.

DLESE Tools
v1.2