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