Skip to content

Commit 9fd6a22

Browse files
committed
SWS-586 - specify custom TransformerFactory in org.springframework.xml.transform.TransformerObjectSupport
1 parent b900c0c commit 9fd6a22

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

xml/src/main/java/org/springframework/xml/transform/TransformerObjectSupport.java

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@
2222
import javax.xml.transform.TransformerConfigurationException;
2323
import javax.xml.transform.TransformerException;
2424
import javax.xml.transform.TransformerFactory;
25+
import javax.xml.transform.TransformerFactoryConfigurationError;
2526

2627
import org.apache.commons.logging.Log;
2728
import org.apache.commons.logging.LogFactory;
2829

30+
import org.springframework.util.Assert;
31+
2932
/**
3033
* Convenient base class for objects that use a <code>Transformer</code>. Subclasses can call {@link
3134
* #createTransformer()} or {@link #transform(Source, Result)}. This should be done per thread (i.e. per incoming
@@ -40,10 +43,48 @@ public abstract class TransformerObjectSupport {
4043
/** Logger available to subclasses. */
4144
protected final Log logger = LogFactory.getLog(getClass());
4245

43-
private static TransformerFactory transformerFactory = TransformerFactory.newInstance();
46+
private TransformerFactory transformerFactory;
47+
48+
private Class transformerFactoryClass;
49+
50+
/**
51+
* Specify the {@code TransformerFactory} class to use.
52+
*/
53+
public void setTransformerFactoryClass(Class transformerFactoryClass) {
54+
Assert.isAssignable(TransformerFactory.class, transformerFactoryClass);
55+
this.transformerFactoryClass = transformerFactoryClass;
56+
}
57+
58+
/**
59+
* Instantiate a new TransformerFactory.
60+
* <p>The default implementation simply calls {@link TransformerFactory#newInstance()}.
61+
* If a {@link #setTransformerFactoryClass "transformerFactoryClass"} has been specified explicitly,
62+
* the default constructor of the specified class will be called instead.
63+
* <p>Can be overridden in subclasses.
64+
* @param transformerFactoryClass the specified factory class (if any)
65+
* @return the new TransactionFactory instance
66+
* @see #setTransformerFactoryClass
67+
* @see #getTransformerFactory()
68+
*/
69+
protected TransformerFactory newTransformerFactory(Class transformerFactoryClass) {
70+
if (transformerFactoryClass != null) {
71+
try {
72+
return (TransformerFactory) transformerFactoryClass.newInstance();
73+
}
74+
catch (Exception ex) {
75+
throw new TransformerFactoryConfigurationError(ex, "Could not instantiate TransformerFactory");
76+
}
77+
}
78+
else {
79+
return TransformerFactory.newInstance();
80+
}
81+
}
4482

4583
/** Returns the <code>TransformerFactory</code>. */
4684
protected TransformerFactory getTransformerFactory() {
85+
if (transformerFactory == null) {
86+
transformerFactory = newTransformerFactory(transformerFactoryClass);
87+
}
4788
return transformerFactory;
4889
}
4990

@@ -55,7 +96,7 @@ protected TransformerFactory getTransformerFactory() {
5596
* if thrown by JAXP methods
5697
*/
5798
protected final Transformer createTransformer() throws TransformerConfigurationException {
58-
return transformerFactory.newTransformer();
99+
return getTransformerFactory().newTransformer();
59100
}
60101

61102
/**

0 commit comments

Comments
 (0)