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;
44 private TransformerFactory _factory;
47 * Constructs the URL resolver.
50 * URI resolver to use.
52 public XSLT(URIResolver aResolver) {
53 _factory = TransformerFactory.newInstance();
54 _factory.setURIResolver(aResolver);
58 * Constructs the XSLT processor.
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);
83 return resolver.resolve(aXslt, "");
87 * Transforms a DOM document into another DOM document using a given XSLT
91 * Document to transform.
94 * @return Transformed document.
96 * In case of problems reading resources.
97 * @throws TransformerException
98 * In case transformation fails.
100 public Document transform(Document aDocument, Source aXslt)
101 throws IOException, TransformerException {
102 Source source = new DOMSource(aDocument);
103 DOMResult result = new DOMResult();
104 transform(source, result, aXslt);
105 return (Document) result.getNode();
109 * Transforms a document using XSLT.
112 * Document to transform.
115 * @return Transformed document.
116 * @throws IOException
117 * In case of problems reading resources.
118 * @throws TransformerException
119 * In case transformation fails.
121 public Document transform(byte[] aDocument, Source aXslt)
122 throws IOException, TransformerException {
123 Source source = new StreamSource(new ByteArrayInputStream(aDocument));
124 DOMResult result = new DOMResult();
125 transform(source, result, aXslt);
126 return (Document) result.getNode();
130 * Transforms a document to a text output. This supports XSLT
131 * transformations that result in text documents.
134 * Document to transform.
136 * XSL transformation.
137 * @return Transformed document.
139 public String textTransform(byte[] aDocument, Source aXslt)
140 throws IOException, TransformerException {
141 Source source = new StreamSource(new ByteArrayInputStream(aDocument));
142 ByteArrayOutputStream os = new ByteArrayOutputStream();
143 StreamResult result = new StreamResult(os);
144 transform(source, result, aXslt);
145 return new String(os.toByteArray());
149 * Transforms a document using XSLT.
152 * Document to transform.
154 * Result of the transformation.
157 * @throws IOException
158 * In case of problems reading resources.
159 * @throws TransformerException
160 * In case transformation fails.
162 public void transform(Source aSource, Result aResult, Source aXslt)
163 throws IOException, TransformerException {
165 Transformer transformer = _factory.newTransformer(aXslt);
166 transformer.transform(aSource, aResult);
167 } catch (TransformerConfigurationException e) {
168 throw new RuntimeException(
169 "Configuration problem of XSLT transformation", e);