[jdom-interest] AW: xsl-transform on document

Jakob Schwendner jschwendner at picturesafe.de
Thu Jul 6 00:57:03 PDT 2000


> -----Ursprüngliche Nachricht-----
> Von: Umut Ceyhan (Garanti Teknoloji) [mailto:UmutCe at garanti.com.tr]
> Gesendet: Donnerstag, 6. Juli 2000 09:08
> An: 'jschwendner at picturesafe.de'
> Betreff:
>
>
> Hello,
> Could you send me your helper class ?  I have JDOM documents and I want to
> process them with convenient xsl sheets and produce html/wml files.
> But I couldn't integrate xalan package into JDOM (my JDK 1.1.7a).
>
> Thanks in advance.
> Umut Ceyhan
> ------------------------------------------------------------------
> ----------
> -------------------------------------------------
> 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. My helper class is far from
> being finished but i think it is usable. I dont know wether there
> have been
> actions in a similar direction, so i just wanted to know your
> oppinion about
> maybe including such a transform class into the jdom package. best wishes,
> jakob schwendner picturesafe gmbh
>

here is what i've been using:

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