2 * Copyright 2005-2010 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.xml;
18 import org.w3c.dom.Document;
20 import org.wamblee.io.FileResource;
22 import java.io.ByteArrayInputStream;
23 import java.io.ByteArrayOutputStream;
25 import java.io.IOException;
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;
40 * XSL transformer for simplified usage of XSL transformations.
42 * @author Erik Brakkee
44 public class XslTransformer {
45 private TransformerFactory factory;
48 * Constructs the URL resolver.
51 * URI resolver to use.
53 public XslTransformer(URIResolver aResolver) {
54 factory = TransformerFactory.newInstance();
55 factory.setURIResolver(aResolver);
59 * Constructs the XSLT processor.
62 public XslTransformer() {
63 factory = TransformerFactory.newInstance();
67 * Resolves an XSLT based on URI.
72 * @return Source for the XSLT
74 * @throws TransformerException
75 * In case the XSLT cannot be found.
77 public Source resolve(String aXslt) throws TransformerException {
78 URIResolver resolver = factory.getURIResolver();
80 if (resolver == null) {
81 if (new File(aXslt).canRead()) {
83 return new StreamSource(new FileResource(new File(aXslt))
85 } catch (IOException e) {
86 throw new TransformerException(e.getMessage(), e);
89 throw new TransformerException("Cannot read '" + aXslt + "'");
93 return resolver.resolve(aXslt, "");
97 * Transforms a DOM document into another DOM document using a given XSLT
101 * Document to transform.
105 * @return Transformed document.
107 * @throws IOException
108 * In case of problems reading resources.
109 * @throws TransformerException
110 * In case transformation fails.
112 public Document transform(Document aDocument, Source aXslt)
113 throws IOException, TransformerException {
114 Source source = new DOMSource(aDocument);
115 DOMResult result = new DOMResult();
116 transform(source, result, aXslt);
118 return (Document) result.getNode();
122 * Transforms a document using XSLT.
125 * Document to transform.
129 * @return Transformed document.
131 * @throws IOException
132 * In case of problems reading resources.
133 * @throws TransformerException
134 * In case transformation fails.
136 public Document transform(byte[] aDocument, Source aXslt)
137 throws IOException, TransformerException {
138 Source source = new StreamSource(new ByteArrayInputStream(aDocument));
139 DOMResult result = new DOMResult();
140 transform(source, result, aXslt);
142 return (Document) result.getNode();
146 * Transforms a document to a text output. This supports XSLT
147 * transformations that result in text documents.
150 * Document to transform.
152 * XSL transformation.
154 * @return Transformed document.
157 public String textTransform(byte[] aDocument, Source aXslt)
158 throws IOException, TransformerException {
159 Source source = new StreamSource(new ByteArrayInputStream(aDocument));
160 ByteArrayOutputStream os = new ByteArrayOutputStream();
161 StreamResult result = new StreamResult(os);
162 transform(source, result, aXslt);
164 return new String(os.toByteArray());
168 * Transforms a document using XSLT.
171 * Document to transform.
173 * Result of the transformation.
177 * @throws IOException
178 * In case of problems reading resources.
179 * @throws TransformerException
180 * In case transformation fails.
182 public void transform(Source aSource, Result aResult, Source aXslt)
183 throws IOException, TransformerException {
185 Transformer transformer = factory.newTransformer(aXslt);
186 transformer.transform(aSource, aResult);
187 } catch (TransformerConfigurationException e) {
188 throw new TransformerException(
189 "Configuration problem of XSLT transformation", e);