2 * Copyright 2005 the original author or authors.
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
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, 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
17 package org.wamblee.xml;
19 import java.io.ByteArrayInputStream;
20 import java.io.ByteArrayOutputStream;
22 import java.io.IOException;
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;
36 import org.w3c.dom.Document;
37 import org.wamblee.io.FileResource;
40 * XSL transformer for simplified usage of XSL transformations.
42 * @author Erik Brakkee
44 public class XslTransformer {
46 private TransformerFactory _factory;
49 * Constructs the URL resolver.
52 * URI resolver to use.
54 public XslTransformer(URIResolver aResolver) {
55 _factory = TransformerFactory.newInstance();
56 _factory.setURIResolver(aResolver);
60 * Constructs the XSLT processor.
63 public XslTransformer() {
64 _factory = TransformerFactory.newInstance();
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.
73 public Source resolve(String aXslt) throws TransformerException {
74 URIResolver resolver = _factory.getURIResolver();
75 if (resolver == null) {
76 if (new File(aXslt).canRead()) {
78 return new StreamSource(new FileResource(new File(aXslt))
80 } catch (IOException e) {
81 throw new TransformerException(e.getMessage(), e);
84 throw new TransformerException("Cannot read '" + aXslt + "'");
87 return resolver.resolve(aXslt, "");
91 * Transforms a DOM document into another DOM document using a given XSLT
95 * Document to transform.
98 * @return Transformed document.
100 * In case of problems reading resources.
101 * @throws TransformerException
102 * In case transformation fails.
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();
113 * Transforms a document using XSLT.
116 * Document to transform.
119 * @return Transformed document.
120 * @throws IOException
121 * In case of problems reading resources.
122 * @throws TransformerException
123 * In case transformation fails.
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();
134 * Transforms a document to a text output. This supports XSLT
135 * transformations that result in text documents.
138 * Document to transform.
140 * XSL transformation.
141 * @return Transformed document.
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());
153 * Transforms a document using XSLT.
156 * Document to transform.
158 * Result of the transformation.
161 * @throws IOException
162 * In case of problems reading resources.
163 * @throws TransformerException
164 * In case transformation fails.
166 public void transform(Source aSource, Result aResult, Source aXslt)
167 throws IOException, TransformerException {
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);