Search code examples
saxonchainingapache-fop

Chaining Saxon XsltTransformer output into FOP


I need to pipe the output of several XSL transformations into a FopFactory object, but I can't figure out how to code this. I have the piping working, but the last step is a mystery.

DOMResult xmlRequest = new DOMResult();
marshaller.marshal(req,xmlRequest);

FopFactory fopFactory = FopFactory.newInstance();
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
ByteArrayOutputStream out = new ByteArrayOutputStream();
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);

XdmNode source = processor.newDocumentBuilder()
   .build(new DOMSource(xmlRequest.getNode()));

XsltTransformer step1 = templateCache.get("foo1.xsl").load();
XsltTransformer step2 = templateCache.get("foo2.xsl").load();
XsltTransformer step3 = templateCache.get("foo3_fo.xsl").load();

step1.setInitialContextNode(source);
step1.setDestination(step2);
step2.setDestination(step3);

// Here's what I really need to do -- pipe the output of step3 into the FO processor.
// Of course, the following statement doesn't work, but I don't know what I need
// to do to make the output of step3 piped into the fop, so the 
step3.setDestination(fop.getDefaultHandler());

step1.transform();

// at this point, the results of the FOP processor would be in the
// ByteArrayOutputStream "out".

How can I make this work?


Solution

  • Of course after posting the question and doing more searching, I found the answer. Here's what I came up with (omitting much of the code from above):

    Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
    SAXDestination fopDest = new SAXDestination(fop.getDefaultHandler());
    .
    step2.setDestination(step3);
    step3.setDestination(fopDest);
    
    step1.transform();
    .
    // Output is now in ByteArrayOutputStream out;