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;
21 import java.io.IOException;
22 import java.io.InputStream;
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.dom.DOMResult;
31 import javax.xml.transform.dom.DOMSource;
32 import javax.xml.transform.stream.StreamResult;
33 import javax.xml.transform.stream.StreamSource;
35 import org.w3c.dom.Document;
36 import org.wamblee.io.InputResource;
44 * Constructs the XSLT processor.
52 * Transforms a DOM document into another DOM document using a given XSLT
56 * Document to transform.
59 * @return Transformed document.
61 * In case of problems reading resources.
62 * @throws TransformerException
63 * In case transformation fails.
65 public Document transform(Document aDocument, InputResource aXslt)
66 throws IOException, TransformerException {
67 Source source = new DOMSource(aDocument);
68 DOMResult result = new DOMResult();
69 transform(source, result, aXslt);
70 return (Document) result.getNode();
74 * Transforms a document using XSLT.
77 * Document to transform.
80 * @return Transformed document.
82 * In case of problems reading resources.
83 * @throws TransformerException
84 * In case transformation fails.
86 public Document transform(byte[] aDocument, InputResource aXslt)
87 throws IOException, TransformerException {
88 Source source = new StreamSource(new ByteArrayInputStream(aDocument));
89 DOMResult result = new DOMResult();
90 transform(source, result, aXslt);
91 return (Document) result.getNode();
95 * Transforms a document to a text output. This supports XSLT transformations
96 * that result in text documents.
97 * @param aDocument Document to transform.
98 * @param aXslt XSL transformation.
99 * @return Transformed document.
101 public String textTransform(byte[] aDocument, InputResource aXslt) throws IOException, TransformerException {
102 Source source = new StreamSource(new ByteArrayInputStream(aDocument));
103 ByteArrayOutputStream os = new ByteArrayOutputStream();
104 StreamResult result = new StreamResult(os);
105 transform(source, result, aXslt);
106 return new String(os.toByteArray());
110 * Transforms a document using XSLT.
113 * Document to transform.
115 * Result of the transformation.
118 * @throws IOException
119 * In case of problems reading resources.
120 * @throws TransformerException
121 * In case transformation fails.
123 public void transform(Source aSource, Result aResult, InputResource aXslt)
124 throws IOException, TransformerException {
125 InputStream xslt = null;
127 xslt = aXslt.getInputStream();
128 Source xsltSource = new StreamSource(xslt);
129 Transformer transformer = TransformerFactory.newInstance()
130 .newTransformer(xsltSource);
131 transformer.transform(aSource, aResult);
132 } catch (TransformerConfigurationException e) {
133 throw new RuntimeException(
134 "Configuration problem of XSLT transformation", e);