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 public class XslTransformer {
44 private TransformerFactory _factory;
47 * Constructs the URL resolver.
50 * URI resolver to use.
52 public XslTransformer(URIResolver aResolver) {
53 _factory = TransformerFactory.newInstance();
54 _factory.setURIResolver(aResolver);
58 * Constructs the XSLT processor.
61 public XslTransformer() {
62 _factory = TransformerFactory.newInstance();
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.
71 public Source resolve(String aXslt) throws TransformerException {
72 URIResolver resolver = _factory.getURIResolver();
73 if (resolver == null) {
74 if (new File(aXslt).canRead()) {
76 return new StreamSource(new FileResource(new File(aXslt))
78 } catch (IOException e) {
79 throw new TransformerException(e.getMessage(), e);
82 throw new TransformerException("Cannot read '" + aXslt + "'");
85 return resolver.resolve(aXslt, "");
89 * Transforms a DOM document into another DOM document using a given XSLT
93 * Document to transform.
96 * @return Transformed document.
98 * In case of problems reading resources.
99 * @throws TransformerException
100 * In case transformation fails.
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();
111 * Transforms a document using XSLT.
114 * Document to transform.
117 * @return Transformed document.
118 * @throws IOException
119 * In case of problems reading resources.
120 * @throws TransformerException
121 * In case transformation fails.
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();
132 * Transforms a document to a text output. This supports XSLT
133 * transformations that result in text documents.
136 * Document to transform.
138 * XSL transformation.
139 * @return Transformed document.
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());
151 * Transforms a document using XSLT.
154 * Document to transform.
156 * Result of the transformation.
159 * @throws IOException
160 * In case of problems reading resources.
161 * @throws TransformerException
162 * In case transformation fails.
164 public void transform(Source aSource, Result aResult, Source aXslt)
165 throws IOException, TransformerException {
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);