/*--
$Id: XSLTransformer.java,v 1.5 2007/11/14 04:36:54 jhunter Exp $
Copyright (C) 2001-2007 Jason Hunter & Brett McLaughlin.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions, and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions, and the disclaimer that follows
these conditions in the documentation and/or other materials
provided with the distribution.
3. The name "JDOM" must not be used to endorse or promote products
derived from this software without prior written permission. For
written permission, please contact
*
* JDOM relies on TrAX to perform the transformation.
* The
* XSLTransformer transformer = new XSLTransformer("file.xsl");
*
* Document x2 = transformer.transform(x); // x is a Document
* Document y2 = transformer.transform(y); // y is a Document
*
javax.xml.transform.TransformerFactory
Java system property
* determines which XSLT engine TrAX uses. Its value should be
* the fully qualified name of the implementation of the abstract
* javax.xml.transform.TransformerFactory
class.
* Values of this property for popular XSLT processors include:
*
com.icl.saxon.TransformerFactoryImpl
net.sf.saxon.TransformerFactoryImpl
org.apache.xalan.processor.TransformerFactoryImpl
jd.xml.xslt.trax.TransformerFactoryImpl
oracle.xml.jaxp.JXSAXTransformerFactory
* This property can be set in all the usual ways a Java system property * can be set. TrAX picks from them in this order:
*System.setProperty( "javax.xml.transform.TransformerFactory",
* "classname
")
classname
* option to the java interpreterlib/jaxp.properties
properties file
* in the JRE directory, in a line like this one:
* javax.xml.parsers.DocumentBuilderFactory=classname
META-INF/services/javax.xml.transform.TransformerFactory
file
* in the JAR archives available to the runtimenull
to use the default JDOM classes.
*/
private JDOMFactory factory = null;
// Internal constructor to support the other constructors
private XSLTransformer(Source stylesheet) throws XSLTransformException {
try {
templates = TransformerFactory.newInstance()
.newTemplates(stylesheet);
}
catch (TransformerException e) {
throw new XSLTransformException("Could not construct XSLTransformer", e);
}
}
/**
* Creates a transformer for a given stylesheet system id.
*
* @param stylesheetSystemId source stylesheet as a Source object
* @throws XSLTransformException if there's a problem in the TrAX back-end
*/
public XSLTransformer(String stylesheetSystemId) throws XSLTransformException {
this(new StreamSource(stylesheetSystemId));
}
/**
*
* This will create a new XSLTransformer
by
* reading the stylesheet from the specified
* InputStream
.
*
InputStream
from which the stylesheet is read.
* @throws XSLTransformException when an IOException, format error, or
* something else prevents the stylesheet from being compiled
*/
public XSLTransformer(InputStream stylesheet) throws XSLTransformException {
this(new StreamSource(stylesheet));
}
/**
*
* This will create a new XSLTransformer
by
* reading the stylesheet from the specified
* Reader
.
*
Reader
from which the stylesheet is read.
* @throws XSLTransformException when an IOException, format error, or
* something else prevents the stylesheet from being compiled
*/
public XSLTransformer(Reader stylesheet) throws XSLTransformException {
this(new StreamSource(stylesheet));
}
/**
*
* This will create a new XSLTransformer
by
* reading the stylesheet from the specified
* File
.
*
File
from which the stylesheet is read.
* @throws XSLTransformException when an IOException, format error, or
* something else prevents the stylesheet from being compiled
*/
public XSLTransformer(File stylesheet) throws XSLTransformException {
this(new StreamSource(stylesheet));
}
/**
*
* This will create a new XSLTransformer
by
* reading the stylesheet from the specified
* Document
.
*
Document
containing the stylesheet.
* @throws XSLTransformException when the supplied Document
* is not syntactically correct XSLT
*/
public XSLTransformer(Document stylesheet) throws XSLTransformException {
this(new JDOMSource(stylesheet));
}
/**
* Transforms the given input nodes to a list of output nodes.
*
* @param inputNodes input nodes
* @return transformed output nodes
* @throws XSLTransformException if there's a problem in the transformation
*/
public List transform(List inputNodes) throws XSLTransformException {
JDOMSource source = new JDOMSource(inputNodes);
JDOMResult result = new JDOMResult();
result.setFactory(factory); // null ok
try {
templates.newTransformer().transform(source, result);
return result.getResult();
}
catch (TransformerException e) {
throw new XSLTransformException("Could not perform transformation", e);
}
}
/**
* Transforms the given document to an output document.
*
* @param inputDoc input document
* @return transformed output document
* @throws XSLTransformException if there's a problem in the transformation
*/
public Document transform(Document inputDoc) throws XSLTransformException {
return transform(inputDoc, null);
}
/**
* Transforms the given document to an output document.
*
* @param inputDoc input document
* @param resolver entity resolver for the input document
* @return transformed output document
* @throws XSLTransformException if there's a problem in the transformation
*/
public Document transform(Document inputDoc, EntityResolver resolver) throws XSLTransformException {
JDOMSource source = new JDOMSource(inputDoc, resolver);
JDOMResult result = new JDOMResult();
result.setFactory(factory); // null ok
try {
templates.newTransformer().transform(source, result);
return result.getDocument();
}
catch (TransformerException e) {
throw new XSLTransformException("Could not perform transformation", e);
}
}
/**
* Sets a custom JDOMFactory to use when building the
* transformation result. Use a custom factory to build the tree
* with your own subclasses of the JDOM classes.
*
* @param factory the custom JDOMFactory
to use or
* null
to use the default JDOM
* classes.
*
* @see #getFactory
*/
public void setFactory(JDOMFactory factory) {
this.factory = factory;
}
/**
* Returns the custom JDOMFactory used to build the transformation
* result.
*
* @return the custom JDOMFactory
used to build the
* transformation result or null
if the
* default JDOM classes are being used.
*
* @see #setFactory
*/
public JDOMFactory getFactory() {
return this.factory;
}
}