(no commit message)
[utils] / support / general / src / main / java / org / wamblee / xml / XslTransformer.java
1 /*
2  * Copyright 2005-2010 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 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             }
89             throw new TransformerException("Cannot read '" + aXslt + "'");
90         }
91
92         return resolver.resolve(aXslt, "");
93     }
94
95     /**
96      * Transforms a DOM document into another DOM document using a given XSLT
97      * transformation.
98      * 
99      * @param aDocument
100      *            Document to transform.
101      * @param aXslt
102      *            XSLT to use.
103      * 
104      * @return Transformed document.
105      * 
106      * @throws IOException
107      *             In case of problems reading resources.
108      * @throws TransformerException
109      *             In case transformation fails.
110      */
111     public Document transform(Document aDocument, Source aXslt)
112         throws IOException, TransformerException {
113         Source source = new DOMSource(aDocument);
114         DOMResult result = new DOMResult();
115         transform(source, result, aXslt);
116
117         return (Document) result.getNode();
118     }
119
120     /**
121      * Transforms a document using XSLT.
122      * 
123      * @param aDocument
124      *            Document to transform.
125      * @param aXslt
126      *            XSLT to use.
127      * 
128      * @return Transformed document.
129      * 
130      * @throws IOException
131      *             In case of problems reading resources.
132      * @throws TransformerException
133      *             In case transformation fails.
134      */
135     public Document transform(byte[] aDocument, Source aXslt)
136         throws IOException, TransformerException {
137         Source source = new StreamSource(new ByteArrayInputStream(aDocument));
138         DOMResult result = new DOMResult();
139         transform(source, result, aXslt);
140
141         return (Document) result.getNode();
142     }
143
144     /**
145      * Transforms a document to a text output. This supports XSLT
146      * transformations that result in text documents.
147      * 
148      * @param aDocument
149      *            Document to transform.
150      * @param aXslt
151      *            XSL transformation.
152      * 
153      * @return Transformed document.
154      * 
155      */
156     public String textTransform(byte[] aDocument, Source aXslt)
157         throws IOException, TransformerException {
158         Source source = new StreamSource(new ByteArrayInputStream(aDocument));
159         ByteArrayOutputStream os = new ByteArrayOutputStream();
160         StreamResult result = new StreamResult(os);
161         transform(source, result, aXslt);
162
163         return new String(os.toByteArray());
164     }
165
166     /**
167      * Transforms a document using XSLT.
168      * 
169      * @param aSource
170      *            Document to transform.
171      * @param aResult
172      *            Result of the transformation.
173      * @param aXslt
174      *            XSLT to use.
175      * 
176      * @throws IOException
177      *             In case of problems reading resources.
178      * @throws TransformerException
179      *             In case transformation fails.
180      */
181     public void transform(Source aSource, Result aResult, Source aXslt)
182         throws IOException, TransformerException {
183         try {
184             Transformer transformer = factory.newTransformer(aXslt);
185             transformer.transform(aSource, aResult);
186         } catch (TransformerConfigurationException e) {
187             throw new TransformerException(
188                 "Configuration problem of XSLT transformation", e);
189         }
190     }
191 }