created a simplified XSL processing class with test
authorerickson <erickson@9efc2488-bf62-4759-914b-345cdb29e865>
Sun, 16 Dec 2007 16:19:27 +0000 (16:19 +0000)
committererickson <erickson@9efc2488-bf62-4759-914b-345cdb29e865>
Sun, 16 Dec 2007 16:19:27 +0000 (16:19 +0000)
git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@1170 9efc2488-bf62-4759-914b-345cdb29e865

src/java/org/opensrf/test/TestXMLTransformer.java [new file with mode: 0644]
src/java/org/opensrf/util/XMLTransformer.java [new file with mode: 0644]

diff --git a/src/java/org/opensrf/test/TestXMLTransformer.java b/src/java/org/opensrf/test/TestXMLTransformer.java
new file mode 100644 (file)
index 0000000..9768372
--- /dev/null
@@ -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();
+        }
+    }
+}
+
+
diff --git a/src/java/org/opensrf/util/XMLTransformer.java b/src/java/org/opensrf/util/XMLTransformer.java
new file mode 100644 (file)
index 0000000..f8bc0d3
--- /dev/null
@@ -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();
+    }
+}
+
+