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  
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
31   * <code>createTransformer</code> to obtain a transformer. This should be done per thread (i.e. per incoming request),
32   * because <code>Transformer</code> instances are not thread-safe.
33   *
34   * @author Arjen Poutsma
35   * @see Transformer
36   * @see #createTransformer()
37   * @since 1.0.0
38   */
39  public abstract class TransformerObjectSupport {
40  
41      /** Logger available to subclasses. */
42      protected final Log logger = LogFactory.getLog(getClass());
43  
44      private static TransformerFactory transformerFactory = TransformerFactory.newInstance();
45  
46      /** Returns the <code>TransformerFactory</code>. */
47      protected TransformerFactory getTransformerFactory() {
48          return transformerFactory;
49      }
50  
51      /**
52       * Creates a new <code>Transformer</code>. Must be called per request, as transformers are not thread-safe.
53       *
54       * @return the created transformer
55       * @throws TransformerConfigurationException
56       *          if thrown by JAXP methods
57       */
58      protected final Transformer createTransformer() throws TransformerConfigurationException {
59          return transformerFactory.newTransformer();
60      }
61  
62      /**
63       * Transforms the given {@link Source} to the given {@link Result}. Creates a new {@link Transformer} for every
64       * call, as transformers are not thread-safe.
65       *
66       * @param source the source to transform from
67       * @param result the result to transform to
68       * @throws TransformerException if thrown by JAXP methods
69       */
70      protected final void transform(Source source, Result result) throws TransformerException {
71          Transformer transformer = createTransformer();
72          transformer.transform(source, result);
73      }
74  
75  }