From: erickson Date: Sun, 16 Dec 2007 16:19:27 +0000 (+0000) Subject: created a simplified XSL processing class with test X-Git-Tag: osrf_rel_2_0_1~797 X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=c0a820449aee57ed7395b48119871b923a216f88;p=OpenSRF.git created a simplified XSL processing class with test git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@1170 9efc2488-bf62-4759-914b-345cdb29e865 --- diff --git a/src/java/org/opensrf/test/TestXMLTransformer.java b/src/java/org/opensrf/test/TestXMLTransformer.java new file mode 100644 index 0000000..9768372 --- /dev/null +++ b/src/java/org/opensrf/test/TestXMLTransformer.java @@ -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 index 0000000..f8bc0d3 --- /dev/null +++ b/src/java/org/opensrf/util/XMLTransformer.java @@ -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(); + } +} + +