Renamed XSLT to XslTransformer.
[utils] / support / src / 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  * XSLT utilities.
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             }
82         }
83         return resolver.resolve(aXslt, "");
84     }
85
86     /**
87      * Transforms a DOM document into another DOM document using a given XSLT
88      * transformation.
89      * 
90      * @param aDocument
91      *            Document to transform.
92      * @param aXslt
93      *            XSLT to use.
94      * @return Transformed document.
95      * @throws IOException
96      *             In case of problems reading resources.
97      * @throws TransformerException
98      *             In case transformation fails.
99      */
100     public Document transform(Document aDocument, Source aXslt)
101             throws IOException, TransformerException {
102         Source source = new DOMSource(aDocument);
103         DOMResult result = new DOMResult();
104         transform(source, result, aXslt);
105         return (Document) result.getNode();
106     }
107
108     /**
109      * Transforms a document using XSLT.
110      * 
111      * @param aDocument
112      *            Document to transform.
113      * @param aXslt
114      *            XSLT to use.
115      * @return Transformed document.
116      * @throws IOException
117      *             In case of problems reading resources.
118      * @throws TransformerException
119      *             In case transformation fails.
120      */
121     public Document transform(byte[] aDocument, Source aXslt)
122             throws IOException, TransformerException {
123         Source source = new StreamSource(new ByteArrayInputStream(aDocument));
124         DOMResult result = new DOMResult();
125         transform(source, result, aXslt);
126         return (Document) result.getNode();
127     }
128
129     /**
130      * Transforms a document to a text output. This supports XSLT
131      * transformations that result in text documents.
132      * 
133      * @param aDocument
134      *            Document to transform.
135      * @param aXslt
136      *            XSL transformation.
137      * @return Transformed document.
138      */
139     public String textTransform(byte[] aDocument, Source aXslt)
140             throws IOException, TransformerException {
141         Source source = new StreamSource(new ByteArrayInputStream(aDocument));
142         ByteArrayOutputStream os = new ByteArrayOutputStream();
143         StreamResult result = new StreamResult(os);
144         transform(source, result, aXslt);
145         return new String(os.toByteArray());
146     }
147
148     /**
149      * Transforms a document using XSLT.
150      * 
151      * @param aSource
152      *            Document to transform.
153      * @param aResult
154      *            Result of the transformation.
155      * @param aXslt
156      *            XSLT to use.
157      * @throws IOException
158      *             In case of problems reading resources.
159      * @throws TransformerException
160      *             In case transformation fails.
161      */
162     public void transform(Source aSource, Result aResult, Source aXslt)
163             throws IOException, TransformerException {
164         try {
165             Transformer transformer = _factory.newTransformer(aXslt);
166             transformer.transform(aSource, aResult);
167         } catch (TransformerConfigurationException e) {
168             throw new RuntimeException(
169                     "Configuration problem of XSLT transformation", e);
170         }
171     }
172 }