Package org.dlese.dpc.xml
Class XSLTransformer
java.lang.Object
org.dlese.dpc.xml.XSLTransformer
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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringSubstitutes escape chars for certain sensitive characthers that don't play well in file names or file paths.static StringSubstitutes hex values for all characters EXCEPT digits, letters, and the chars shown below.protected static final StringReturn a string for the current time and date, sutiable for display in log files and output to standout:static TransformergetTransformer(String xslFilePath) Gets aTransformerused to transform XML using a given XSL stylesheet.static TransformergetTransformer(String xslFilePath, String transformerFactoryClass) Gets a specificTransformer, used to transform XML using a given XSL stylesheet.static final TransformergetTransformerFromXSLString(String xslString) Gets aTransformerused to transform XML using a given XSL stylesheet.static final StringlocalizeXml(String xmlString) Removes all namespace information from XML.protected static voidOutput a line of text to standard out, with datestamp, if debug is set to true.protected static final voidOutput a line of text to error out, with datestamp.protected static final voidOutput a line of text to error out, with datestamp.protected static final voidOutput a line of text to error out, with datestamp.static voidsetDebug(boolean db) Sets the debug attribute of the objectPerform the transform on each file in the inputFilesDir, using the given xsl stylesheet, placing the resulting transformed files into outpuFilesDir.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 final StringtransformFile(File inputFile, Transformer transformer) Transforms an XML file using a pre-compiledTransformer.static final StringtransformFile(String inputFilePath, String xslFilePath) Transforms an XML file using an XSL stylesheet.static final StringtransformFile(String inputFilePath, Transformer transformer) Transforms an XML file using a pre-compiledTransformer.static final StringWritertransformFileToWriter(String inputFilePath, String xslFilePath) Transforms an XML file using an XSL stylesheet.static final StringWritertransformFileToWriter(String inputFilePath, Transformer transformer) Transforms an XML file using a pre-compiledTransformer.static final StringtransformString(String xmlString, String xslFilePath) Transforms an XML String using an XSL stylesheet.static final StringtransformString(String xmlString, Transformer transformer) Transforms an XML String using a pre-compiledTransformer.static final StringWritertransformStringToWriter(String xmlString, String xslFilePath) Transforms an XML String using an XSL stylesheet.static final StringWritertransformStringToWriter(String xmlString, Transformer transformer) Transforms an XML String using a pre-compiledTransformer.static final StringtransformStringUsingString(String xmlString, String xslString) Transforms an XML String using an XSL stylesheet supplied as a String.static final voidtransformToFile(File inputFile, FileOutputStream fos, Transformer transformer) Transform a single file to the given FileOutputStream using the given transformer.static final voidtransformToFile(File inputFile, File outputFile, Transformer transformer) Transform a single file to the given output file using the given transformer.static final voidtransformToFile(String inputFilePath, String outputFilePath, Transformer transformer) Transform a single file to a file at the given output path using the given transformer.
-
Constructor Details
-
XSLTransformer
public XSLTransformer()Constructor does nothing. All processing done in the run() or static stand-alone methods.
-
-
Method Details
-
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 inputFilesDiroutputFilesDir- 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
Transforms an XML file using a pre-compiledTransformer. UsegetTransformer(String xslFilePath)to produce a reusableTransformerfor a given XSL stylesheet.- Parameters:
inputFilePath- The XML file to transform.transformer- A pre-compiledTransformerused to produce transformed output.- Returns:
- A String containing the transformed content.
-
transformFile
Transforms an XML file using a pre-compiledTransformer. UsegetTransformer(String xslFilePath)to produce a reusableTransformerfor a given XSL stylesheet.- Parameters:
inputFile- The XML file to transform.transformer- A pre-compiledTransformerused 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-compiledTransformer. UsegetTransformer(String xslFilePath)to produce a reusableTransformerfor 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-compiledTransformerused to produce transformed output.- Returns:
- A StringWriter containing the transformed content.
-
transformString
Transforms an XML String using a pre-compiledTransformer. UsegetTransformer(String xslFilePath)to produce a reusableTransformerfor a given XSL stylesheet.- Parameters:
xmlString- The XML String to transform.transformer- A pre-compiledTransformerused to produce transformed output.- Returns:
- A String containing the transformed content.
-
transformStringToWriter
Transforms an XML String using a pre-compiledTransformer. UsegetTransformer(String xslFilePath)to produce a reusableTransformerfor 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-compiledTransformerused to produce transformed output.- Returns:
- A StringWriter containing the transformed content.
-
transformFile
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
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
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
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
Removes all namespace information from XML.- Parameters:
xmlString- The XML String to strip of namespaces- Returns:
- A String containing XML without namespaces
-
transformStringToWriter
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 aTransformerused to transform XML using a given XSL stylesheet. For efficiency, oneTransformershould 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 specificTransformer, 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 aTransformerused to transform XML using a given XSL stylesheet. For efficiency, oneTransformershould 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
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: * ? ! | \ / ' " { } invalid input: '<' > ; , ^ ( ) $ ~ Windows file names may not contain: \ / : * ? " invalid input: '<' > |
- Parameters:
stg- A String to encode.- Returns:
- An encoded String.
-
encodeCharsInString
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: * ? ! | \ / ' " { } invalid input: '<' > ; , ^ ( ) $ ~
Windows file names may not contain: \ / : * ? " invalid input: '<' > |
- Parameters:
stg- A String to encode.- Returns:
- An encoded 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
Output a line of text to error out, with datestamp.- Parameters:
s- The text that will be output to error out.
-
prtlnErr
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
Output a line of text to error out, with datestamp.- Parameters:
t- The Throwable to output with message
-
prtln
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
-