(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.ByteArrayOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23
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;
34
35 import org.w3c.dom.Document;
36 import org.wamblee.io.InputResource;
37
38 /**
39  * XSLT utilities.
40  */
41 public class XSLT {
42
43     /**
44      * Constructs the XSLT processor.
45      * 
46      */
47     public XSLT() {
48         // Empty.
49     }
50
51     /**
52      * Transforms a DOM document into another DOM document using a given XSLT
53      * transformation.
54      * 
55      * @param aDocument
56      *            Document to transform.
57      * @param aXslt
58      *            XSLT to use.
59      * @return Transformed document.
60      * @throws IOException
61      *             In case of problems reading resources.
62      * @throws TransformerException
63      *             In case transformation fails.
64      */
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();
71     }
72
73     /**
74      * Transforms a document using XSLT.
75      * 
76      * @param aDocument
77      *            Document to transform.
78      * @param aXslt
79      *            XSLT to use.
80      * @return Transformed document.
81      * @throws IOException
82      *             In case of problems reading resources.
83      * @throws TransformerException
84      *             In case transformation fails.
85      */
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();
92     }
93     
94     /**
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.  
100      */
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());
107     }
108
109     /**
110      * Transforms a document using XSLT.
111      * 
112      * @param aSource
113      *            Document to transform.
114      * @param aResult
115      *            Result of the transformation.
116      * @param aXslt
117      *            XSLT to use.
118      * @throws IOException
119      *             In case of problems reading resources.
120      * @throws TransformerException
121      *             In case transformation fails.
122      */
123     public void transform(Source aSource, Result aResult, InputResource aXslt)
124             throws IOException, TransformerException {
125         InputStream xslt = null;
126         try {
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);
135         } finally {
136             if (xslt != null) {
137                 xslt.close();
138             }
139         }
140     }
141 }