640aece320e28e3b9a9bc853dd0399cf186854a3
[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 /**
41  * XSL transformer for simplified usage of XSL transformations.
42  *
43  * @author Erik Brakkee
44  */
45 public class XslTransformer {
46     /**
47      * DOCUMENT ME!
48      */
49     private TransformerFactory factory;
50
51 /**
52      * Constructs the URL resolver.
53      *
54      * @param aResolver
55      *            URI resolver to use.
56      */
57     public XslTransformer(URIResolver aResolver) {
58         factory = TransformerFactory.newInstance();
59         factory.setURIResolver(aResolver);
60     }
61
62 /**
63      * Constructs the XSLT processor.
64      *
65      */
66     public XslTransformer() {
67         factory = TransformerFactory.newInstance();
68     }
69
70     /**
71      * Resolves an XSLT based on URI.
72      *
73      * @param aXslt XSLT to resolve,
74      *
75      * @return Source for the XSLT
76      *
77      * @throws TransformerException In case the XSLT cannot be found.
78      */
79     public Source resolve(String aXslt) throws TransformerException {
80         URIResolver resolver = factory.getURIResolver();
81
82         if (resolver == null) {
83             if (new File(aXslt).canRead()) {
84                 try {
85                     return new StreamSource(new FileResource(new File(aXslt))
86                         .getInputStream());
87                 } catch (IOException e) {
88                     throw new TransformerException(e.getMessage(), e);
89                 }
90             } else {
91                 throw new TransformerException("Cannot read '" + aXslt + "'");
92             }
93         }
94
95         return resolver.resolve(aXslt, "");
96     }
97
98     /**
99      * Transforms a DOM document into another DOM document using a
100      * given XSLT transformation.
101      *
102      * @param aDocument Document to transform.
103      * @param aXslt XSLT to use.
104      *
105      * @return Transformed document.
106      *
107      * @throws IOException In case of problems reading resources.
108      * @throws TransformerException In case transformation fails.
109      */
110     public Document transform(Document aDocument, Source aXslt)
111         throws IOException, TransformerException {
112         Source    source = new DOMSource(aDocument);
113         DOMResult result = new DOMResult();
114         transform(source, result, aXslt);
115
116         return (Document) result.getNode();
117     }
118
119     /**
120      * Transforms a document using XSLT.
121      *
122      * @param aDocument Document to transform.
123      * @param aXslt XSLT to use.
124      *
125      * @return Transformed document.
126      *
127      * @throws IOException In case of problems reading resources.
128      * @throws TransformerException In case transformation fails.
129      */
130     public Document transform(byte[] aDocument, Source aXslt)
131         throws IOException, TransformerException {
132         Source    source = new StreamSource(new ByteArrayInputStream(aDocument));
133         DOMResult result = new DOMResult();
134         transform(source, result, aXslt);
135
136         return (Document) result.getNode();
137     }
138
139     /**
140      * Transforms a document to a text output. This supports XSLT
141      * transformations that result in text documents.
142      *
143      * @param aDocument Document to transform.
144      * @param aXslt XSL transformation.
145      *
146      * @return Transformed document.
147      *
148      * @throws IOException DOCUMENT ME!
149      * @throws TransformerException DOCUMENT ME!
150      */
151     public String textTransform(byte[] aDocument, Source aXslt)
152         throws IOException, TransformerException {
153         Source                source = new StreamSource(new ByteArrayInputStream(
154                     aDocument));
155         ByteArrayOutputStream os     = new ByteArrayOutputStream();
156         StreamResult          result = new StreamResult(os);
157         transform(source, result, aXslt);
158
159         return new String(os.toByteArray());
160     }
161
162     /**
163      * Transforms a document using XSLT.
164      *
165      * @param aSource Document to transform.
166      * @param aResult Result of the transformation.
167      * @param aXslt XSLT to use.
168      *
169      * @throws IOException In case of problems reading resources.
170      * @throws TransformerException In case transformation fails.
171      */
172     public void transform(Source aSource, Result aResult, Source aXslt)
173         throws IOException, TransformerException {
174         try {
175             Transformer transformer = factory.newTransformer(aXslt);
176             transformer.transform(aSource, aResult);
177         } catch (TransformerConfigurationException e) {
178             throw new TransformerException("Configuration problem of XSLT transformation",
179                 e);
180         }
181     }
182 }