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
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;
41 * XSL transformer for simplified usage of XSL transformations.
43 * @author Erik Brakkee
45 public class XslTransformer {
49 private TransformerFactory factory;
52 * Constructs the URL resolver.
55 * URI resolver to use.
57 public XslTransformer(URIResolver aResolver) {
58 factory = TransformerFactory.newInstance();
59 factory.setURIResolver(aResolver);
63 * Constructs the XSLT processor.
66 public XslTransformer() {
67 factory = TransformerFactory.newInstance();
71 * Resolves an XSLT based on URI.
73 * @param aXslt XSLT to resolve,
75 * @return Source for the XSLT
77 * @throws TransformerException In case the XSLT cannot be found.
79 public Source resolve(String aXslt) throws TransformerException {
80 URIResolver resolver = factory.getURIResolver();
82 if (resolver == null) {
83 if (new File(aXslt).canRead()) {
85 return new StreamSource(new FileResource(new File(aXslt))
87 } catch (IOException e) {
88 throw new TransformerException(e.getMessage(), e);
91 throw new TransformerException("Cannot read '" + aXslt + "'");
95 return resolver.resolve(aXslt, "");
99 * Transforms a DOM document into another DOM document using a
100 * given XSLT transformation.
102 * @param aDocument Document to transform.
103 * @param aXslt XSLT to use.
105 * @return Transformed document.
107 * @throws IOException In case of problems reading resources.
108 * @throws TransformerException In case transformation fails.
110 public Document transform(Document aDocument, Source aXslt)
111 throws IOException, TransformerException {
112 Source source = new DOMSource(aDocument);
113 DOMResult result = new DOMResult();
114 transform(source, result, aXslt);
116 return (Document) result.getNode();
120 * Transforms a document using XSLT.
122 * @param aDocument Document to transform.
123 * @param aXslt XSLT to use.
125 * @return Transformed document.
127 * @throws IOException In case of problems reading resources.
128 * @throws TransformerException In case transformation fails.
130 public Document transform(byte[] aDocument, Source aXslt)
131 throws IOException, TransformerException {
132 Source source = new StreamSource(new ByteArrayInputStream(aDocument));
133 DOMResult result = new DOMResult();
134 transform(source, result, aXslt);
136 return (Document) result.getNode();
140 * Transforms a document to a text output. This supports XSLT
141 * transformations that result in text documents.
143 * @param aDocument Document to transform.
144 * @param aXslt XSL transformation.
146 * @return Transformed document.
148 * @throws IOException DOCUMENT ME!
149 * @throws TransformerException DOCUMENT ME!
151 public String textTransform(byte[] aDocument, Source aXslt)
152 throws IOException, TransformerException {
153 Source source = new StreamSource(new ByteArrayInputStream(
155 ByteArrayOutputStream os = new ByteArrayOutputStream();
156 StreamResult result = new StreamResult(os);
157 transform(source, result, aXslt);
159 return new String(os.toByteArray());
163 * Transforms a document using XSLT.
165 * @param aSource Document to transform.
166 * @param aResult Result of the transformation.
167 * @param aXslt XSLT to use.
169 * @throws IOException In case of problems reading resources.
170 * @throws TransformerException In case transformation fails.
172 public void transform(Source aSource, Result aResult, Source aXslt)
173 throws IOException, TransformerException {
175 Transformer transformer = factory.newTransformer(aXslt);
176 transformer.transform(aSource, aResult);
177 } catch (TransformerConfigurationException e) {
178 throw new TransformerException("Configuration problem of XSLT transformation",