Search code examples
xsltxpageslotus-dominoxsl-fo

Getting 'WrappedRuntimeException' when using XSL-FO in an Xpage


I am trying to create a PDF from the contents of an xpage. I am following the format that Paul Calhoun used in Notes in 9 #102. I am able to create PDF's for views, but having trouble creating one for a document. I do not think the error is in Paul's code so I am not including it here, although I can if need be.

To generate the XML to display I use the generateXML() method of the document class in java. I get a handle to the backend document and then return the XML. The XML appears well formed, the top level tab is <document>. I pass this XML to the transformer which is using Apache FOP. All of my code is contained in the beforeRenderResponse of an xAgent.

The XSL stylesheet that I am using is a stripped down version to just get a proof of concept to work. I am going to include it, because the problem likely resides with this code. I am totally new to XSL.

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet
    version="1.1"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format"
    exclude-result-prefixes="fo">
    <xsl:output
        method="xml"
        version="1.0"
        omit-xml-declaration="no"
        indent="yes" />
    <xsl:param
        name="versionParam"
        select="'1.0'" />

    <xsl:template match="document">
        <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
            <fo:layout-master-set>
                <fo:simple-page-master
                    master-name="outpage"
                    page-height="11in"
                    page-width="8.5in"
                    margin-top="1in"
                    margin-bottom="1in"
                    margin-left="1in"
                    margin-right="1in">
                    <fo:region-body />
                </fo:simple-page-master>
            </fo:layout-master-set>
            <fo:page-sequence master-reference="A4">
                <fo:block
                        font-size="16pt"
                        font-weight="bold"
                        space-after="5mm">
                        Apache FOP Proof of Concept.
                    </fo:block>
            </fo:page-sequence>
            </fo:root>
    </xsl:template>
</xsl:stylesheet>

In the log file I get the message:

FATAL ERROR:  'com.ibm.xtq.common.utils.WrappedRuntimeException: D:\Program Files\IBM\Domino\<document form='PO'>

The error echos the entire XML in the log that it is trying to transform and ends with:

 (The filename, directory name, or volume label syntax is incorrect.)'

Notice that Domino is trying to include the XML in a path. I know this is wrong, but don't know what to do to fix it.

EDIT: This is the Java class that runs the transformation. This code is from Paul Calhoun's demo.

import java.io.OutputStream;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;

public class DominoXMLFO2PDF {
    public static void getPDF(OutputStream pdfout,String xml,String xslt, Boolean authReq, String usernamepass) {
        try {
            //System.out.println("Transforming...");
            Source xmlSource,xsltSource;  //Source xmlSource = new StreamSource("http://localhost/APCC.nsf/Main?ReadViewEntries&count=999&ResortAscending=2");
            xmlSource = new StreamSource(xml);  //Source xsltSource = new StreamSource("http://localhost/APCC.nsf/viewdata.xsl");
            xsltSource = new StreamSource(xslt);// configure fopFactory as desired              

            final FopFactory fopFactory = FopFactory.newInstance();

            FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
            // configure foUserAgent as desired

            // Setup output
            // OutputStream out = pdfout;
            // out = new java.io.BufferedOutputStream(out);

            try {
                // Construct fop with desired output format
                Fop fop = fopFactory.newFop(org.apache.xmlgraphics.util.MimeConstants.MIME_PDF,foUserAgent, pdfout);
                // Setup XSLT
                TransformerFactory factory = TransformerFactory.newInstance();
                Transformer transformer = factory.newTransformer(xsltSource);
                //transformer.setParameter("versionParam", "Company List"); // Set the value of a <param> in the stylesheet
                Source src = xmlSource; // Setup input for XSLT transformation
                Result res = new SAXResult(fop.getDefaultHandler()); // Resulting SAX events (the generated FO) must be piped through to FOP
                transformer.transform(src, res); // Start XSLT transformation and FOP processing

            } catch (Exception e) {

            } finally {
            }
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
    }
}

This code is called in the xAgent using this line:

var retOutput = jce.getPDF(pageOutput, xmlsource, xsltsource, authReq, usernamepass);

The xmlsource is set with this line where the method returns XML using Document.generateXML():

var xmlsource = statusBean.generateXML(POdata, sessionScope.unidPDF);

Solution

  • Your problem is the XMLSource! When you look at Paul's code:

    Source xmlSource = new StreamSource("http://localhost/APCC.nsf/Main?ReadViewEntries");
    

    This points to an URL where to retrieve XML.On the other hand your code:

     xmlsource = statusBean.generateXML(POdata, sessionScope.unidPDF);
    

    contains the XML. So you need to change to:

       String xmlstring = statusBean.generateXML(POdata, sessionScope.unidPDF);
       Source xmlsource = new StreamSource(new java.io.StringReader(xmlstring));
    

    I strongly suggest you try to keep all the Java in a java class, so you don't need to wrap/upwrap the objects in SSJS. Have a look at my series on FO too.