View Javadoc

1   /*
2    * File:    XsltTransformer.java
3    * Created: 13.10.2006 8:52:33 
4    *
5    * Copyright 2006 Michal Burda.
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20   */
21  package net.sf.webmancer.util.xml.xslt;
22  
23  import java.io.InputStream;
24  import java.io.OutputStream;
25  import java.io.Reader;
26  import java.io.Writer;
27  
28  import javax.xml.transform.Result;
29  import javax.xml.transform.Source;
30  import javax.xml.transform.Templates;
31  import javax.xml.transform.Transformer;
32  import javax.xml.transform.TransformerConfigurationException;
33  import javax.xml.transform.TransformerException;
34  import javax.xml.transform.TransformerFactory;
35  import javax.xml.transform.stream.StreamResult;
36  import javax.xml.transform.stream.StreamSource;
37  
38  /**
39   * @author Michal Burda
40   */
41  public class XsltTransformer {
42      /**
43       * 
44       */
45      private Templates templates;
46  
47      /**
48       * @throws TransformerConfigurationException
49       */
50      public XsltTransformer(final InputStream transformationDefinition) throws TransformerConfigurationException {
51          super();
52          TransformerFactory transformerFactory = TransformerFactory.newInstance();
53          this.templates = transformerFactory.newTemplates(new StreamSource(transformationDefinition));
54      }
55  
56      /**
57       * Factory method for new {@link XsltTransformer} instance creation. This method is equivalent to
58       * <tt>return templates.newTransformer()</tt>. Method is called everytime the transformation is requested
59       * (calling <tt>transform(...)</tt>). Subclasses may redefine this method to implement various XSLT parameter
60       * settings etc.
61       * 
62       * @return New instance of {@link XsltTransformer} ready to perform transformation.
63       * @throws TransformerConfigurationException
64       *             When an error occurs during the transformer creation.
65       */
66      protected Transformer getTransformer() throws TransformerConfigurationException {
67          Transformer transformer = this.templates.newTransformer();
68          return transformer;
69      }
70  
71      /**
72       * @param source
73       * @param target
74       * @throws TransformerException
75       */
76      public void transform(final Source source, final Result target) throws TransformerException {
77          getTransformer().transform(source, target);
78      }
79  
80      /**
81       * @param source
82       * @param target
83       * @throws TransformerException
84       */
85      public void transform(final InputStream source, final OutputStream target) throws TransformerException {
86          StreamSource transformSource = new StreamSource(source);
87          StreamResult transformResult = new StreamResult(target);
88          this.transform(transformSource, transformResult);
89      }
90  
91      /**
92       * @param source
93       * @param target
94       * @throws TransformerException
95       */
96      public void transform(final Reader source, final Writer target) throws TransformerException {
97          StreamSource transformSource = new StreamSource(source);
98          StreamResult transformResult = new StreamResult(target);
99          this.transform(transformSource, transformResult);
100     }
101 
102     /**
103      * @param source
104      * @param target
105      * @throws TransformerException
106      */
107     public void transform(final InputStream source, final Writer target) throws TransformerException {
108         StreamSource transformSource = new StreamSource(source);
109         StreamResult transformResult = new StreamResult(target);
110         this.transform(transformSource, transformResult);
111     }
112 
113     /**
114      * @param source
115      * @param target
116      * @throws TransformerException
117      */
118     public void transform(final Reader source, final OutputStream target) throws TransformerException {
119         StreamSource transformSource = new StreamSource(source);
120         StreamResult transformResult = new StreamResult(target);
121         this.transform(transformSource, transformResult);
122     }
123 
124 }