(no commit message)
[utils] / support / src / org / wamblee / xml / XSLT.java
1 /*
2  * Copyright 2005 the original author or authors.
3  * 
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
6  * the License at
7  * 
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * 
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
14  * the License.
15  */
16
17 package org.wamblee.xml;
18
19 import java.io.ByteArrayInputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22
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;
32
33 import org.w3c.dom.Document;
34 import org.wamblee.io.InputResource;
35
36 /**
37  * XSLT utilities.
38  */
39 public class XSLT {
40
41     /**
42      * Constructs the XSLT processor.
43      * 
44      */
45     public XSLT() {
46         // Empty.
47     }
48
49     /**
50      * Transforms a DOM document into another DOM document using a given XSLT
51      * transformation.
52      * 
53      * @param aDocument
54      *            Document to transform.
55      * @param aXslt
56      *            XSLT to use.
57      * @return Transformed document.
58      * @throws IOException
59      *             In case of problems reading resources.
60      * @throws TransformerException
61      *             In case transformation fails.
62      */
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();
69     }
70
71     /**
72      * Transforms a document using XSLT.
73      * 
74      * @param aDocument
75      *            Document to transform.
76      * @param aXslt
77      *            XSLT to use.
78      * @return Transformed document.
79      * @throws IOException
80      *             In case of problems reading resources.
81      * @throws TransformerException
82      *             In case transformation fails.
83      */
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();
90     }
91
92     /**
93      * Transforms a document using XSLT.
94      * 
95      * @param aSource
96      *            Document to transform.
97      * @param aResult
98      *            Result of the transformation.
99      * @param aXslt
100      *            XSLT to use.
101      * @throws IOException
102      *             In case of problems reading resources.
103      * @throws TransformerException
104      *             In case transformation fails.
105      */
106     public void transform(Source aSource, Result aResult, InputResource aXslt)
107             throws IOException, TransformerException {
108         InputStream xslt = null;
109         try {
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);
118         } finally {
119             if (xslt != null) {
120                 xslt.close();
121             }
122         }
123     }
124 }