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