View Javadoc

1   /*
2    * Copyright 2006 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  import javax.xml.transform.TransformerFactoryConfigurationError;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  
30  import org.springframework.util.Assert;
31  
32  /**
33   * Convenient base class for objects that use a <code>Transformer</code>. Subclasses can call {@link
34   * #createTransformer()} or {@link #transform(Source, Result)}. This should be done per thread (i.e. per incoming
35   * request), because <code>Transformer</code> instances are not thread-safe.
36   *
37   * @author Arjen Poutsma
38   * @see Transformer
39   * @since 1.0.0
40   */
41  public abstract class TransformerObjectSupport {
42  
43      /** Logger available to subclasses. */
44      protected final Log logger = LogFactory.getLog(getClass());
45  
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      }
82  
83      /** Returns the <code>TransformerFactory</code>. */
84      protected TransformerFactory getTransformerFactory() {
85          if (transformerFactory == null) {
86              transformerFactory = newTransformerFactory(transformerFactoryClass);
87          }
88          return transformerFactory;
89      }
90  
91      /**
92       * Creates a new <code>Transformer</code>. Must be called per request, as transformers are not thread-safe.
93       *
94       * @return the created transformer
95       * @throws TransformerConfigurationException
96       *          if thrown by JAXP methods
97       */
98      protected final Transformer createTransformer() throws TransformerConfigurationException {
99          return getTransformerFactory().newTransformer();
100     }
101 
102     /**
103      * Transforms the given {@link Source} to the given {@link Result}. Creates a new {@link Transformer} for every
104      * call, as transformers are not thread-safe.
105      *
106      * @param source the source to transform from
107      * @param result the result to transform to
108      * @throws TransformerException if thrown by JAXP methods
109      */
110     protected final void transform(Source source, Result result) throws TransformerException {
111         Transformer transformer = createTransformer();
112         transformer.transform(source, result);
113     }
114 
115 }