/* File: JDOMSimpleXSLTProcessor.java */ package org.jdom.contrib.xslt; import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.io.IOException; import java.lang.Thread; import org.jdom.Document; import org.jdom.output.XMLOutputter; import org.apache.trax.Processor; import org.apache.trax.Templates; import org.apache.trax.Transformer; import org.apache.trax.Result; import org.apache.trax.ProcessorException; import org.apache.trax.ProcessorFactoryException; import org.apache.trax.TransformException; import org.xml.sax.InputSource; import org.xml.sax.SAXException; /** *

* JDOMSimpleXSLTProcessor provides a simple way to process * XSLT transformations using the * Apache Xalan 2 * processor. *

*

Requirements:

*

* Add Xalan 2 and Xerces to the classpath. You will find the latest versions * at xml.apache.org. *

*

Sample:

*

*

 *     import org.jdom.Document;
 *     import org.jdom.Element;
 *     import org.jdom.contrib.xslt.JDOMSimpleXSLTProcessor;
 *     import org.xml.sax.InputSource;
 *     import org.apache.trax.Result;
 *     
 *     public class TestJDOMSimpleXSLTProcessor {
 *       public static void main(String[] args) {
 *         Document document;
 *         Element element;
 *         InputSource xsl = new InputSource("foo.xsl");
 *         Result out = new Result(System.out);
 *         
 *         element = new Element("doc");
 *         element.setText("Hello");
 *         document = new Document(element);
 *         
 *         try {
 *           JDOMSimpleXSLTProcessor.process(document, xsl, out);
 *         }
 *         catch(Exception e) {
 *           e.printStackTrace();
 *         }
 *       }
 *     }
 *   
*

*

foo.xsl:

*

*

 *     <?xml version="1.0"?> 
 *     <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
 *       <xsl:template match="doc">
 *         <out><xsl:value-of select="."/></out>
 *       </xsl:template>
 *     </xsl:stylesheet>
 *   
*

*

* I recommend not to use this class in a production environment. *

* * @author Markus Tripp * @version 1.0 */ public class JDOMSimpleXSLTProcessor { /** *

* Processes the XSLT transformation. *

*

* I hope this method is thread safe. I did no extensive testing. *

* * @param document {@link Document} JDOM document which you * would like to transform. * @param xsl {@link InputSource} XSL stylesheet. * @param out {@link Result} Output of the transformation. */ public static void process( Document document, InputSource xsl, Result out) throws ProcessorException, ProcessorFactoryException, TransformException, SAXException, IOException { PipedOutputStream outputStream = new PipedOutputStream(); PipedInputStream inputStream = new PipedInputStream(outputStream); XMLOutputThread xmlThread = new XMLOutputThread(document, outputStream); XSLTProcessorThread xsltThread = new XSLTProcessorThread(inputStream, xsl, out); xmlThread.start(); xsltThread.start(); } } class XMLOutputThread extends Thread { private Document document; private PipedOutputStream outputStream; public XMLOutputThread( Document document, PipedOutputStream outputStream) { this.document = document; this.outputStream = outputStream; } public void run() { try { XMLOutputter outputter = new XMLOutputter(); outputter.output(document, outputStream); outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } class XSLTProcessorThread extends Thread { private PipedInputStream inputStream; private InputSource xsl; private Result out; public XSLTProcessorThread( PipedInputStream inputStream, InputSource xsl, Result out) { this.inputStream = inputStream; this.xsl = xsl; this.out = out; } public void run() { try { Processor processor = Processor.newInstance("xslt"); Templates templates = processor.process(xsl); Transformer transformer = templates.newTransformer(); transformer.transform(new InputSource(inputStream), out); } catch (Exception e) { e.printStackTrace(); } } }