Removed DOCUMENT ME comments that were generated and applied source code
[utils] / support / general / src / main / java / org / wamblee / xml / XslTransformer.java
1 /*
2  * Copyright 2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 package org.wamblee.xml;
17
18 import org.w3c.dom.Document;
19
20 import org.wamblee.io.FileResource;
21
22 import java.io.ByteArrayInputStream;
23 import java.io.ByteArrayOutputStream;
24 import java.io.File;
25 import java.io.IOException;
26
27 import javax.xml.transform.Result;
28 import javax.xml.transform.Source;
29 import javax.xml.transform.Transformer;
30 import javax.xml.transform.TransformerConfigurationException;
31 import javax.xml.transform.TransformerException;
32 import javax.xml.transform.TransformerFactory;
33 import javax.xml.transform.URIResolver;
34 import javax.xml.transform.dom.DOMResult;
35 import javax.xml.transform.dom.DOMSource;
36 import javax.xml.transform.stream.StreamResult;
37 import javax.xml.transform.stream.StreamSource;
38
39 /**
40  * XSL transformer for simplified usage of XSL transformations.
41  * 
42  * @author Erik Brakkee
43  */
44 public class XslTransformer {
45     private TransformerFactory factory;
46
47     /**
48      * Constructs the URL resolver.
49      * 
50      * @param aResolver
51      *            URI resolver to use.
52      */
53     public XslTransformer(URIResolver aResolver) {
54         factory = TransformerFactory.newInstance();
55         factory.setURIResolver(aResolver);
56     }
57
58     /**
59      * Constructs the XSLT processor.
60      * 
61      */
62     public XslTransformer() {
63         factory = TransformerFactory.newInstance();
64     }
65
66     /**
67      * Resolves an XSLT based on URI.
68      * 
69      * @param aXslt
70      *            XSLT to resolve,
71      * 
72      * @return Source for the XSLT
73      * 
74      * @throws TransformerException
75      *             In case the XSLT cannot be found.
76      */
77     public Source resolve(String aXslt) throws TransformerException {
78         URIResolver resolver = factory.getURIResolver();
79
80         if (resolver == null) {
81             if (new File(aXslt).canRead()) {
82                 try {
83                     return new StreamSource(new FileResource(new File(aXslt))
84                         .getInputStream());
85                 } catch (IOException e) {
86                     throw new TransformerException(e.getMessage(), e);
87                 }
88             } else {
89                 throw new TransformerException("Cannot read '" + aXslt + "'");
90             }
91         }
92
93         return resolver.resolve(aXslt, "");
94     }
95
96     /**
97      * Transforms a DOM document into another DOM document using a given XSLT
98      * transformation.
99      * 
100      * @param aDocument
101      *            Document to transform.
102      * @param aXslt
103      *            XSLT to use.
104      * 
105      * @return Transformed document.
106      * 
107      * @throws IOException
108      *             In case of problems reading resources.
109      * @throws TransformerException
110      *             In case transformation fails.
111      */
112     public Document transform(Document aDocument, Source aXslt)
113         throws IOException, TransformerException {
114         Source source = new DOMSource(aDocument);
115         DOMResult result = new DOMResult();
116         transform(source, result, aXslt);
117
118         return (Document) result.getNode();
119     }
120
121     /**
122      * Transforms a document using XSLT.
123      * 
124      * @param aDocument
125      *            Document to transform.
126      * @param aXslt
127      *            XSLT to use.
128      * 
129      * @return Transformed document.
130      * 
131      * @throws IOException
132      *             In case of problems reading resources.
133      * @throws TransformerException
134      *             In case transformation fails.
135      */
136     public Document transform(byte[] aDocument, Source aXslt)
137         throws IOException, TransformerException {
138         Source source = new StreamSource(new ByteArrayInputStream(aDocument));
139         DOMResult result = new DOMResult();
140         transform(source, result, aXslt);
141
142         return (Document) result.getNode();
143     }
144
145     /**
146      * Transforms a document to a text output. This supports XSLT
147      * transformations that result in text documents.
148      * 
149      * @param aDocument
150      *            Document to transform.
151      * @param aXslt
152      *            XSL transformation.
153      * 
154      * @return Transformed document.
155      * 
156      */
157     public String textTransform(byte[] aDocument, Source aXslt)
158         throws IOException, TransformerException {
159         Source source = new StreamSource(new ByteArrayInputStream(aDocument));
160         ByteArrayOutputStream os = new ByteArrayOutputStream();
161         StreamResult result = new StreamResult(os);
162         transform(source, result, aXslt);
163
164         return new String(os.toByteArray());
165     }
166
167     /**
168      * Transforms a document using XSLT.
169      * 
170      * @param aSource
171      *            Document to transform.
172      * @param aResult
173      *            Result of the transformation.
174      * @param aXslt
175      *            XSLT to use.
176      * 
177      * @throws IOException
178      *             In case of problems reading resources.
179      * @throws TransformerException
180      *             In case transformation fails.
181      */
182     public void transform(Source aSource, Result aResult, Source aXslt)
183         throws IOException, TransformerException {
184         try {
185             Transformer transformer = factory.newTransformer(aXslt);
186             transformer.transform(aSource, aResult);
187         } catch (TransformerConfigurationException e) {
188             throw new TransformerException(
189                 "Configuration problem of XSLT transformation", e);
190         }
191     }
192 }