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.IOException;
21 import java.io.InputStream;
23 import javax.xml.transform.Result;
24 import javax.xml.transform.Source;
25 import javax.xml.transform.Transformer;
26 import javax.xml.transform.TransformerConfigurationException;
27 import javax.xml.transform.TransformerException;
28 import javax.xml.transform.TransformerFactory;
29 import javax.xml.transform.dom.DOMResult;
30 import javax.xml.transform.dom.DOMSource;
31 import javax.xml.transform.stream.StreamSource;
33 import org.w3c.dom.Document;
34 import org.wamblee.io.InputResource;
42 * Constructs the XSLT processor.
50 * Transforms a DOM document into another DOM document using a given XSLT
54 * Document to transform.
57 * @return Transformed document.
59 * In case of problems reading resources.
60 * @throws TransformerException
61 * In case transformation fails.
63 public Document transform(Document aDocument, InputResource aXslt)
64 throws IOException, TransformerException {
65 Source source = new DOMSource(aDocument);
66 DOMResult result = new DOMResult();
67 transform(source, result, aXslt);
68 return (Document) result.getNode();
72 * Transforms a document using XSLT.
75 * Document to transform.
78 * @return Transformed document.
80 * In case of problems reading resources.
81 * @throws TransformerException
82 * In case transformation fails.
84 public Document transform(byte[] aDocument, InputResource aXslt)
85 throws IOException, TransformerException {
86 Source source = new StreamSource(new ByteArrayInputStream(aDocument));
87 DOMResult result = new DOMResult();
88 transform(source, result, aXslt);
89 return (Document) result.getNode();
93 * Transforms a document using XSLT.
96 * Document to transform.
98 * Result of the transformation.
101 * @throws IOException
102 * In case of problems reading resources.
103 * @throws TransformerException
104 * In case transformation fails.
106 public void transform(Source aSource, Result aResult, InputResource aXslt)
107 throws IOException, TransformerException {
108 InputStream xslt = null;
110 xslt = aXslt.getInputStream();
111 Source xsltSource = new StreamSource(xslt);
112 Transformer transformer = TransformerFactory.newInstance()
113 .newTransformer(xsltSource);
114 transformer.transform(aSource, aResult);
115 } catch (TransformerConfigurationException e) {
116 throw new RuntimeException(
117 "Configuration problem of XSLT transformation", e);