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;
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;
32 import org.w3c.dom.Document;
40 * Transforms a DOM document into another DOM document using a given XSLT
44 * Document to transform.
47 * @return Transformed document.
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( );
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( );
64 * Transforms a DOM document into another DOM document using a given XSLT
68 * Document to transform.
71 * @return Transformed document.
73 public static void transform( Source aSource, Result aResult,
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 );