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 + "'");
92 return resolver.resolve(aXslt, "");
96 * Transforms a DOM document into another DOM document using a given XSLT
100 * Document to transform.
104 * @return Transformed document.
106 * @throws IOException
107 * In case of problems reading resources.
108 * @throws TransformerException
109 * In case transformation fails.
111 public Document transform(Document aDocument, Source aXslt)
112 throws IOException, TransformerException {
113 Source source = new DOMSource(aDocument);
114 DOMResult result = new DOMResult();
115 transform(source, result, aXslt);
117 return (Document) result.getNode();
121 * Transforms a document using XSLT.
124 * Document to transform.
128 * @return Transformed document.
130 * @throws IOException
131 * In case of problems reading resources.
132 * @throws TransformerException
133 * In case transformation fails.
135 public Document transform(byte[] aDocument, Source aXslt)
136 throws IOException, TransformerException {
137 Source source = new StreamSource(new ByteArrayInputStream(aDocument));
138 DOMResult result = new DOMResult();
139 transform(source, result, aXslt);
141 return (Document) result.getNode();
145 * Transforms a document to a text output. This supports XSLT
146 * transformations that result in text documents.
149 * Document to transform.
151 * XSL transformation.
153 * @return Transformed document.
156 public String textTransform(byte[] aDocument, Source aXslt)
157 throws IOException, TransformerException {
158 Source source = new StreamSource(new ByteArrayInputStream(aDocument));
159 ByteArrayOutputStream os = new ByteArrayOutputStream();
160 StreamResult result = new StreamResult(os);
161 transform(source, result, aXslt);
163 return new String(os.toByteArray());
167 * Transforms a document using XSLT.
170 * Document to transform.
172 * Result of the transformation.
176 * @throws IOException
177 * In case of problems reading resources.
178 * @throws TransformerException
179 * In case transformation fails.
181 public void transform(Source aSource, Result aResult, Source aXslt)
182 throws IOException, TransformerException {
184 Transformer transformer = factory.newTransformer(aXslt);
185 transformer.transform(aSource, aResult);
186 } catch (TransformerConfigurationException e) {
187 throw new TransformerException(
188 "Configuration problem of XSLT transformation", e);