[jdom-interest] making SAXHandler class public?
Jan Peter Hecking
jan at netgaroo.com
Wed Feb 14 15:12:03 PST 2001
Hi!
A while ago I asked whether it would be possible to turn the SAXHandler
class used by the SAXBuilder internaly into a public class. Well, I
went ahead and did so in my JDOM source tree. In the attached program
one can see why I think this change is useful: there's no need any more
for those pesky Piped(Input|Output)Streams when trying to transform a
JDOM document into another JDOM document using the TrAX API. As the
J2SE API docs state:
------------------------------ snip --------------------------------
[...] Typically, data is read from a PipedInputStream object by one
thread and data is written to the corresponding PipedOutputStream
by some other thread. Attempting to use both objects from a single
thread is not recommended, as it may deadlock the thread. [...]
------------------------------ snip --------------------------------
I had this problem and using the SAXHandler directly is the most elegant
solution to this problem I think. It is probably a lot faster, too.
If there is no fundamental problem with turning the SAXHandler into a
public class I would like to suggest this change for the JDOM API.
No changes to the SAXHandler class or the SAXBuilder class are
necessary!
bye,
Jan
--
Jan Peter Hecking jhecking at netgaroo.com
University of Rostock Department of Computer Science
Homepage: http://www.informatik.uni-rostock.de/~jhecking
-------------- next part --------------
import org.jdom.Document;
import org.jdom.input.SAXHandler;
import org.jdom.output.SAXOutputter;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
import javax.xml.transform.Templates;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TemplatesHandler;
import javax.xml.transform.sax.TransformerHandler;
/**
* Helper class with one static method to allow clients to transform JDOM
* documents using XSLT.
*/
public class XsltTransformer
{
private static SAXOutputter saxOutputter;
private static SAXTransformerFactory saxTFactory;
private static TemplatesHandler templatesHandler;
private static XMLReader reader;
static{
try{
// don't forget to set the ContentHandler before using
// the outputter!
saxOutputter = new SAXOutputter( null );
saxTFactory = (SAXTransformerFactory) TransformerFactory.newInstance();
templatesHandler = saxTFactory.newTemplatesHandler();
reader = XMLReaderFactory.createXMLReader();
reader.setContentHandler( templatesHandler );
}
catch( Exception e ){
e.printStackTrace();
//System.exit( -1 );
}
}
/**
* Transforms a JDOM document into another JDOM document using the given
* stylesheet.
*/
public static Document transform( Document sourceDoc, String stylesheet )
throws XsltTransformerException
{
try{
reader.parse( stylesheet );
Templates templates = templatesHandler.getTemplates();
TransformerHandler handler = saxTFactory.newTransformerHandler(
templates );
Document targetDoc = new Document( null );
// had to turn SAXHandler into a public class to use it here:
handler.setResult( new SAXResult( new SAXHandler( targetDoc ) ) );
saxOutputter.setContentHandler( handler );
saxOutputter.output( sourceDoc );
return targetDoc;
}
catch( Exception e ){
throw new XsltTransformerException( e );
}
}//transform()
}//XsltTransformer
-------------- next part --------------
/**
* Encapsulates all Exceptions that can be thrown by the
* XsltTransformer.transform() method.
*/
public class XsltTransformerException
extends Exception
{
private Exception nestedException;
public XsltTransformerException( Exception e )
{
super( e.getMessage() );
nestedException = e;
}//XsltTransformerException
public Exception getNestedException()
{
return nestedException;
}//getNestedException()
}//XsltTransformerException
More information about the jdom-interest
mailing list