[Opensrf-commits] r1170 - in trunk/src/java/org/opensrf: test util

svn at svn.open-ils.org svn at svn.open-ils.org
Sun Dec 16 11:40:44 EST 2007


Author: erickson
Date: 2007-12-16 11:19:27 -0500 (Sun, 16 Dec 2007)
New Revision: 1170

Added:
   trunk/src/java/org/opensrf/test/TestXMLTransformer.java
   trunk/src/java/org/opensrf/util/XMLTransformer.java
Log:
created a simplified XSL processing class with test

Added: trunk/src/java/org/opensrf/test/TestXMLTransformer.java
===================================================================
--- trunk/src/java/org/opensrf/test/TestXMLTransformer.java	                        (rev 0)
+++ trunk/src/java/org/opensrf/test/TestXMLTransformer.java	2007-12-16 16:19:27 UTC (rev 1170)
@@ -0,0 +1,22 @@
+package org.opensrf.test;
+import org.opensrf.util.XMLTransformer;
+import java.io.File;
+
+public class TestXMLTransformer {
+    /**
+     * arg[0] path to an XML file
+     * arg[1] path to the XSL file to apply
+     */
+    public static void main(String[] args) {
+        try {
+            File xmlFile = new File(args[0]);
+            File xslFile = new File(args[1]);
+            XMLTransformer t = new XMLTransformer(xmlFile, xslFile);
+            System.out.println(t.apply());
+        } catch(Exception e) {
+            e.printStackTrace();
+        }
+    }
+}
+
+

Added: trunk/src/java/org/opensrf/util/XMLTransformer.java
===================================================================
--- trunk/src/java/org/opensrf/util/XMLTransformer.java	                        (rev 0)
+++ trunk/src/java/org/opensrf/util/XMLTransformer.java	2007-12-16 16:19:27 UTC (rev 1170)
@@ -0,0 +1,59 @@
+package org.opensrf.util;
+import javax.xml.transform.*;
+import javax.xml.transform.stream.*;
+import javax.xml.parsers.*;
+import java.io.File;
+import java.io.ByteArrayInputStream;
+import java.io.OutputStream;
+import java.io.ByteArrayOutputStream;
+
+
+/**
+ * Performs XSL transformations.  
+ * TODO: Add ability to pass in XSL variables
+ */
+public class XMLTransformer {
+
+    /** The XML to transform */
+    private Source xmlSource;
+    /** The stylesheet to apply */
+    private Source xslSource;
+
+    public XMLTransformer(Source xmlSource, Source xslSource) {
+        this.xmlSource = xmlSource;
+        this.xslSource = xslSource;
+    }
+
+    public XMLTransformer(String xmlString, File xslFile) {
+        this(
+            new StreamSource(new ByteArrayInputStream(xmlString.getBytes())),
+            new StreamSource(xslFile));
+    }
+
+    public XMLTransformer(File xmlFile, File xslFile) {
+        this(
+            new StreamSource(xmlFile),
+            new StreamSource(xslFile));
+    }
+
+    /** 
+     * Applies the transformation and puts the result into the provided output stream
+     */
+    public void apply(OutputStream outStream) throws TransformerException, TransformerConfigurationException {
+        Result result = new StreamResult(outStream);
+        Transformer trans = TransformerFactory.newInstance().newTransformer(xslSource);
+        trans.transform(xmlSource, result);
+    }
+
+    /**
+     * Applies the transformation and return the resulting string
+     * @return The String created by the XSL transformation
+     */
+    public String apply() throws TransformerException, TransformerConfigurationException {
+        OutputStream outStream = new ByteArrayOutputStream();
+        this.apply(outStream);
+        return outStream.toString();
+    }
+}
+
+



More information about the opensrf-commits mailing list