1 /* 2 * Copyright 2005-2010 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.springframework.xml.transform; 18 19 import javax.xml.transform.Result; 20 import javax.xml.transform.Source; 21 import javax.xml.transform.Transformer; 22 import javax.xml.transform.TransformerConfigurationException; 23 import javax.xml.transform.TransformerException; 24 import javax.xml.transform.TransformerFactory; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 /** 30 * Convenient base class for objects that use a <code>Transformer</code>. Subclasses can call {@link 31 * #createTransformer()} or {@link #transform(Source, Result)}. This should be done per thread (i.e. per incoming 32 * request), because <code>Transformer</code> instances are not thread-safe. 33 * 34 * @author Arjen Poutsma 35 * @see Transformer 36 * @since 1.0.0 37 */ 38 public abstract class TransformerObjectSupport { 39 40 /** 41 * Logger available to subclasses. 42 */ 43 protected final Log logger = LogFactory.getLog(getClass()); 44 45 private TransformerHelper transformerHelper = new TransformerHelper(); 46 47 /** 48 * Specify the {@code TransformerFactory} class to use. 49 */ 50 public void setTransformerFactoryClass(Class<? extends TransformerFactory> transformerFactoryClass) { 51 transformerHelper.setTransformerFactoryClass(transformerFactoryClass); 52 } 53 54 /** 55 * Instantiate a new TransformerFactory. <p>The default implementation simply calls {@link 56 * TransformerFactory#newInstance()}. If a {@link #setTransformerFactoryClass "transformerFactoryClass"} has been 57 * specified explicitly, the default constructor of the specified class will be called instead. <p>Can be overridden 58 * in subclasses. 59 * 60 * @param transformerFactoryClass the specified factory class (if any) 61 * @return the new TransactionFactory instance 62 * @see #setTransformerFactoryClass 63 * @see #getTransformerFactory() 64 */ 65 protected TransformerFactory newTransformerFactory(Class<? extends TransformerFactory> transformerFactoryClass) { 66 return transformerHelper.newTransformerFactory(transformerFactoryClass); 67 } 68 69 /** 70 * Returns the <code>TransformerFactory</code>. 71 */ 72 protected TransformerFactory getTransformerFactory() { 73 return transformerHelper.getTransformerFactory(); 74 } 75 76 /** 77 * Creates a new <code>Transformer</code>. Must be called per request, as transformers are not thread-safe. 78 * 79 * @return the created transformer 80 * @throws TransformerConfigurationException 81 * if thrown by JAXP methods 82 */ 83 protected final Transformer createTransformer() throws TransformerConfigurationException { 84 return transformerHelper.createTransformer(); 85 } 86 87 /** 88 * Transforms the given {@link Source} to the given {@link Result}. Creates a new {@link Transformer} for every 89 * call, as transformers are not thread-safe. 90 * 91 * @param source the source to transform from 92 * @param result the result to transform to 93 * @throws TransformerException if thrown by JAXP methods 94 */ 95 protected final void transform(Source source, Result result) throws TransformerException { 96 transformerHelper.transform(source, result); 97 } 98 99 }