(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.File;
21
22 import javax.xml.transform.Result;
23 import javax.xml.transform.Source;
24 import javax.xml.transform.Transformer;
25 import javax.xml.transform.TransformerConfigurationException;
26 import javax.xml.transform.TransformerException;
27 import javax.xml.transform.TransformerFactory;
28 import javax.xml.transform.dom.DOMResult;
29 import javax.xml.transform.dom.DOMSource;
30 import javax.xml.transform.stream.StreamSource;
31
32 import org.w3c.dom.Document;
33
34 /**
35  * XSLT utilities.
36  */
37 public class XSLT {
38
39     /**
40      * Transforms a DOM document into another DOM document using a given XSLT
41      * transformation.
42      * 
43      * @param aDocument
44      *            Document to transform.
45      * @param aXslt
46      *            XSLT to use.
47      * @return Transformed document.
48      */
49     public static Document transform( Document aDocument, File aXslt ) {
50         Source source = new DOMSource( aDocument );
51         DOMResult result = new DOMResult( );
52         transform( source, result, aXslt );
53         return (Document) result.getNode( );
54     }
55     
56     public static Document transform(byte[] aDocument, File aXslt ) {
57         Source source = new StreamSource( new ByteArrayInputStream(aDocument) );
58         DOMResult result = new DOMResult( );
59         transform( source, result, aXslt );
60         return (Document) result.getNode( );
61     }
62
63     /**
64      * Transforms a DOM document into another DOM document using a given XSLT
65      * transformation.
66      * 
67      * @param aDocument
68      *            Document to transform.
69      * @param aXslt
70      *            XSLT to use.
71      * @return Transformed document.
72      */
73     public static void transform( Source aSource, Result aResult,
74             File aXslt ) {
75         try {
76             Source xslt = new StreamSource( aXslt );
77             Transformer transformer = TransformerFactory.newInstance( )
78                     .newTransformer( xslt );
79             transformer.transform( aSource, aResult );
80         } catch ( TransformerConfigurationException e ) {
81             throw new RuntimeException(
82                     "Configuration problem of XSLT transformation", e );
83         } catch ( TransformerException e ) {
84             throw new RuntimeException( "Error transforming document", e );
85         }
86     }
87 }