[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

TransformerHandler Chaining with Java and Saxon

Subject: TransformerHandler Chaining with Java and Saxon
From: "Scott Lynch" <slynch@xxxxxxxxxx>
Date: Tue, 2 Jun 2009 18:50:15 -0400
 TransformerHandler Chaining with Java and Saxon
All,

This is not quite an xsl question, but hopefully someone here has seen
this before and can help me, because I've been banging my head on my
keyboard for hours now. I'm trying to chain together several transforms
using the TransformerHandler chaining design pattern in Java with the
Saxon (8.9) engine. The code below invariable throws an exception
complaining about "The TransformerHandler is not serially reusable. The
startDocument() method must be called once only." when the
reader.parse(source) method is invoked.

Does anyone know why this would be happening and how to prevent it? I am
able to individually transform my file with the transforms by using a
set of rotating temporary files, but I would like to increase the speed
of the transformation (I'm transforming about 17 files comprising 50meg
of data through about 10 transforms and this is taking several minutes)
using chaining if possible.

thanks,
Scott


    private String multiTransform(String sourceFileName, List<String>
transformFiles, String startKey)
    {
        String outFileName = "transformOutput.xml";
        try
        {
            TransformerFactory factory =
TransformerFactory.newInstance();
            SAXTransformerFactory saxTFactory = ((SAXTransformerFactory)
factory);
            List<TransformerHandler> handlers = new
ArrayList<TransformerHandler>();
            TransformerHandler previousHandler = null;
            for (String transformFile : transformFiles)
            {
                StreamSource source = new
StreamSource(Resources.getResource(transformFile).openStream());
                TransformerHandler handler =
saxTFactory.newTransformerHandler(source);
                handlers.add(handler);
                if (previousHandler != null)
                {
                    previousHandler.setResult(new SAXResult(handler));
                    break;
                }
                previousHandler = handler;
            }

            XMLReader reader = XMLReaderFactory.createXMLReader();
            reader.setContentHandler(handlers.get(0));

            TransformerHandler lastHandler =
handlers.get(handlers.size() - 1);
            OutputStream resultOutStream = new
FileOutputStream(outFileName);
            StreamResult xformOutResult = new
StreamResult(resultOutStream);
            lastHandler.setResult(xformOutResult);
            FileInputStream fileInStrm = new
FileInputStream(sourceFileName);
            InputSource source = new InputSource(fileInStrm);
            reader.parse(source);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return outFileName;
    }



The exception...

java.lang.UnsupportedOperationException: The TransformerHandler is not
serially reusable. The startDocument() method must be called once only.
	at
net.sf.saxon.TransformerHandlerImpl.startDocument(TransformerHandlerImpl
.java:69)
	at
net.sf.saxon.event.ContentHandlerProxy.open(ContentHandlerProxy.java:242
)
	at net.sf.saxon.event.ProxyReceiver.open(ProxyReceiver.java:76)
	at
net.sf.saxon.event.ImplicitResultChecker.open(ImplicitResultChecker.java
:24)
	at
net.sf.saxon.event.ImplicitResultChecker.firstContent(ImplicitResultChec
ker.java:59)
	at
net.sf.saxon.event.ImplicitResultChecker.startElement(ImplicitResultChec
ker.java:30)
	at
net.sf.saxon.event.NamespaceReducer.startElement(NamespaceReducer.java:5
4)
	at
net.sf.saxon.event.ComplexContentOutputter.startContent(ComplexContentOu
tputter.java:481)
	at
net.sf.saxon.event.ComplexContentOutputter.characters(ComplexContentOutp
utter.java:136)
	at net.sf.saxon.instruct.Copy.processLeavingTail(Copy.java:164)
	at
net.sf.saxon.instruct.Choose.processLeavingTail(Choose.java:338)
	at
net.sf.saxon.instruct.Template.applyLeavingTail(Template.java:99)
	at
net.sf.saxon.instruct.ApplyTemplates.applyTemplates(ApplyTemplates.java:
319)
	at
net.sf.saxon.instruct.ApplyTemplates.apply(ApplyTemplates.java:189)
	at
net.sf.saxon.instruct.ApplyTemplates.process(ApplyTemplates.java:149)
	at
net.sf.saxon.instruct.ElementCreator.processLeavingTail(ElementCreator.j
ava:250)
	at net.sf.saxon.instruct.Copy.processLeavingTail(Copy.java:152)
	at
net.sf.saxon.instruct.Choose.processLeavingTail(Choose.java:338)
	at
net.sf.saxon.instruct.Template.applyLeavingTail(Template.java:99)
	at
net.sf.saxon.instruct.ApplyTemplates.applyTemplates(ApplyTemplates.java:
319)
	at
net.sf.saxon.instruct.ApplyTemplates.defaultAction(ApplyTemplates.java:3
51)
	at
net.sf.saxon.instruct.ApplyTemplates.applyTemplates(ApplyTemplates.java:
312)
	at
net.sf.saxon.Controller.transformDocument(Controller.java:1602)
	at
net.sf.saxon.TransformerHandlerImpl.endDocument(TransformerHandlerImpl.j
ava:133)
	at
com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endDocument
(AbstractSAXParser.java:737)
	at
com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.s
canDocument(XMLDocumentFragmentScannerImpl.java:515)
	at
com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML1
1Configuration.java:807)
	at
com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML1
1Configuration.java:737)
	at
com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.jav
a:107)
	at
com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Abstr
actSAXParser.java:1205)

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.