Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


[xsl] TransformerHandler Chaining with Java and Saxon

From: "Scott Lynch" <slynch@------.--->
To: <xsl-list@-----.------------.--->
Date: 6/2/2009 10:53:00 PM
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)

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe@l...>
--~--



transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent