(no commit message)
[utils] / support / src / org / wamblee / xml / XSLT.java
index 14789e8649a3f3cf232febed299af1a6c63e9d33..e7c5fc7643c76c046e06c69c401f67a435df5618 100644 (file)
@@ -17,7 +17,8 @@
 package org.wamblee.xml;
 
 import java.io.ByteArrayInputStream;
-import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
 
 import javax.xml.transform.Result;
 import javax.xml.transform.Source;
@@ -30,12 +31,21 @@ import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.stream.StreamSource;
 
 import org.w3c.dom.Document;
+import org.wamblee.io.InputResource;
 
 /**
  * XSLT utilities.
  */
 public class XSLT {
 
+    /**
+     * Constructs the XSLT processor.
+     * 
+     */
+    public XSLT() {
+        // Empty.
+    }
+
     /**
      * Transforms a DOM document into another DOM document using a given XSLT
      * transformation.
@@ -45,43 +55,70 @@ public class XSLT {
      * @param aXslt
      *            XSLT to use.
      * @return Transformed document.
+     * @throws IOException
+     *             In case of problems reading resources.
+     * @throws TransformerException
+     *             In case transformation fails.
      */
-    public static Document transform( Document aDocument, File aXslt ) {
-        Source source = new DOMSource( aDocument );
-        DOMResult result = new DOMResult( );
-        transform( source, result, aXslt );
-        return (Document) result.getNode( );
-    }
-    
-    public static Document transform(byte[] aDocument, File aXslt ) {
-        Source source = new StreamSource( new ByteArrayInputStream(aDocument) );
-        DOMResult result = new DOMResult( );
-        transform( source, result, aXslt );
-        return (Document) result.getNode( );
+    public Document transform(Document aDocument, InputResource aXslt)
+            throws IOException, TransformerException {
+        Source source = new DOMSource(aDocument);
+        DOMResult result = new DOMResult();
+        transform(source, result, aXslt);
+        return (Document) result.getNode();
     }
 
     /**
-     * Transforms a DOM document into another DOM document using a given XSLT
-     * transformation.
+     * Transforms a document using XSLT.
      * 
      * @param aDocument
      *            Document to transform.
      * @param aXslt
      *            XSLT to use.
      * @return Transformed document.
+     * @throws IOException
+     *             In case of problems reading resources.
+     * @throws TransformerException
+     *             In case transformation fails.
+     */
+    public Document transform(byte[] aDocument, InputResource aXslt)
+            throws IOException, TransformerException {
+        Source source = new StreamSource(new ByteArrayInputStream(aDocument));
+        DOMResult result = new DOMResult();
+        transform(source, result, aXslt);
+        return (Document) result.getNode();
+    }
+
+    /**
+     * Transforms a document using XSLT.
+     * 
+     * @param aSource
+     *            Document to transform.
+     * @param aResult
+     *            Result of the transformation.
+     * @param aXslt
+     *            XSLT to use.
+     * @throws IOException
+     *             In case of problems reading resources.
+     * @throws TransformerException
+     *             In case transformation fails.
      */
-    public static void transform( Source aSource, Result aResult,
-            File aXslt ) {
+    public void transform(Source aSource, Result aResult, InputResource aXslt)
+            throws IOException, TransformerException {
+        InputStream xslt = null;
         try {
-            Source xslt = new StreamSource( aXslt );
-            Transformer transformer = TransformerFactory.newInstance( )
-                    .newTransformer( xslt );
-            transformer.transform( aSource, aResult );
-        } catch ( TransformerConfigurationException e ) {
+            xslt = aXslt.getInputStream();
+            Source xsltSource = new StreamSource(xslt);
+            Transformer transformer = TransformerFactory.newInstance()
+                    .newTransformer(xsltSource);
+            transformer.transform(aSource, aResult);
+        } catch (TransformerConfigurationException e) {
             throw new RuntimeException(
-                    "Configuration problem of XSLT transformation", e );
-        } catch ( TransformerException e ) {
-            throw new RuntimeException( "Error transforming document", e );
+                    "Configuration problem of XSLT transformation", e);
+        } finally {
+            if (xslt != null) {
+                xslt.close();
+            }
         }
     }
 }