[jdom-interest] xsl transformation on jdom trees

Jakob Schwendner jschwendner at picturesafe.de
Wed Jun 7 01:58:55 PDT 2000


> Jakob Schwendner wrote:
> >
> > Hi there,
> >
> > We've been using JDOM for a while now and have included it into our
> > framework.
> > I have now encountered a point at where i need to Transform my
> XML documents
> > vi XSL. Therefore i have used the Apache Xalan xslt.
> > For having a straight JDOM oriented environment i encapsuled
> the conversion
> > process in a helper class which takes a JDOM Document and a
> XSLT Sheet as
> > input and returns the processed JDOM Document.
>
> Very cool ;-) Post it, and we can see about including it in JDOM!
>
> Thanks,
> Brett

Hello,

This is the class I use for xsl transform of JDOM documents.
It has some major problems which concern error handling... I havent really
thought about what happens if some of the steps fail or block.
Anyway, maybe you find it usefull for the jdom package. :)

Jakob Schwendner

--------------- class XSLTransform ---------------------

package org.jdom.transform;

import java.io.*;
import org.jdom.*;
import org.apache.xalan.xslt.*;

/**
* public interface for XSLTransformations on org.jdom.Documents
*/

public interface XSLTransform {
	/**
	* transform an org.jdom.Document object via a precompiled stylesheet (xsl)
into another org.jdom.Document object.
	* @param doc Document to be transformed
	* @param ssr precompiled StylesheetRoot
	* @return the Transformed Document
	*/
	public Document process( Document doc, StylesheetRoot ssr ) throws
JDOMException, IOException;

	/**
	* transform an org.jdom.Document object via a stylesheet (xsl) into another
org.jdom.Document object.
	* @param doc Document to be transformed
	* @param xsl InputStream which holds the xsl data
	* @return the Transformed Document
	*/
	public Document process( Document doc, InputStream xsl) throws
JDOMException, IOException;
}



--------------- class XalanTransform --------------------

package org.jdom.transform;

import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;

import java.io.*;
import java.util.*;
import org.apache.xalan.xslt.*;
import org.xml.sax.*;


public class XalanTransform implements XSLTransform {

	public Document process( Document xmlin, StylesheetRoot ssr ) throws
JDOMException, IOException {
		// jdom -> xalan -> jdom
		// this method pipes a jdom tree into the xslt processor and
		// the resulting stream is piped into a jdom SAXBuilder again

		// pipe JDOM output to Xalan input
		final Document xml = xmlin;
		final StylesheetRoot xslprocessor = ssr;

		final PipedInputStream xis = new PipedInputStream();
		final PipedOutputStream pos = new PipedOutputStream(xis);

		// call a new thread which pipes the outputstream given to the
		// JDOM outputter into our inputstream.
		new Thread() {
			public void run() {
				try {
					new XMLOutputter("", false, "ISO-8859-1").output( xml, pos );
					pos.close();
				}
				catch(Exception e) {
				// FIXME: do some error handling that fits :)
				}
			}
		}
		.start();

		// now do the xsl transformation
		final PipedInputStream pis = new PipedInputStream();
		final PipedOutputStream xos = new PipedOutputStream(pis);

		// make a new thread which calls the xalan processor
		new Thread() {
			public void run() {
				try {
					xslprocessor.process(
						new XSLTInputSource( xis ),
						new XSLTResultTarget( xos ) );
					xos.close();
				}
				catch(Exception e) {
				// FIXME: do some error handling
				}
			}
		}
		.start();

		// and finaly build your resulting JDOM tree from the pipe.
		Document xmlout = new SAXBuilder().build( pis );

		return xmlout;
	}

	public Document process( Document doc, InputStream xsl) throws
JDOMException, IOException {
		// compile StylesheetRoot object and call next process method.
		// could use XSLTProcessor.process(..) as well. but too lazy by now :)
		try {
			StylesheetRoot ssr = XSLTProcessorFactory
				.getProcessor()
				.processStylesheet(
					new XSLTInputSource( xsl ));

			return process( doc, ssr );
		}
		catch( SAXException e ) {
			throw new JDOMException("could not get StylesheetRoot:"+e.toString());
		}
	}
}




More information about the jdom-interest mailing list