01: /*
02: * Copyright 2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
45:
46: static {
47: transformerFactory = TransformerFactory.newInstance();
48: }
49:
50: /** Returns the <code>TransformerFactory</code>. */
51: protected TransformerFactory getTransformerFactory() {
52: return transformerFactory;
53: }
54:
55: /**
56: * Creates a new <code>Transformer</code>. Must be called per request, as transformers are not thread-safe.
57: *
58: * @return the created transformer
59: * @throws TransformerConfigurationException
60: * if thrown by JAXP methods
61: */
62: protected final Transformer createTransformer()
63: throws TransformerConfigurationException {
64: return transformerFactory.newTransformer();
65: }
66:
67: /**
68: * Transforms the given {@link Source} to the given {@link Result}. Creates a new {@link Transformer} for every
69: * call, as transformers are not thread-safe.
70: *
71: * @param source the source to transform from
72: * @param result the result to transform to
73: * @throws TransformerException if thrown by JAXP methods
74: */
75: protected final void transform(Source source, Result result)
76: throws TransformerException {
77: Transformer transformer = createTransformer();
78: transformer.transform(source, result);
79: }
80:
81: }
|