Use Apache Fop with the Oracle XSQL servlet


The Oracle XSQL-Servlet is an easy-to-use tool for simple sql queries and statements within servlet containers. To render the XML output of the servlet with Apache FOP a serializer is required.

Unfortunately the Oracle documentation describes only, how to write such a serializer with FOP 0.20.x. As of the vanished "Driver" class this description is not useful for current FOP versions.


I myself wrote a new serializer, which is now tested with Oracle XDK 10.0 and FOP 0.94. Compile this code using fop.jar and the jars came with Oracle XDK and install it in the classpath of your application.

package oracle.xml.xsql.serializers;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXResult;

import oracle.xml.xsql.XSQLDocumentSerializer;
import oracle.xml.xsql.XSQLPageRequest;

import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
import org.w3c.dom.Document;

// ----------------------------------
// Tested with the FOP 0.94 Release
// ----------------------------------
public class XSQLFOPSerializer
        implements XSQLDocumentSerializer
  public void serialize(Document doc, XSQLPageRequest env)
    try {
        // Step 3: Construct fop with desired output format
        Fop fop = C_FOP_FACTORY.newFop(
              MimeConstants.MIME_PDF, env.getOutputStream() );

        // Step 4: Setup JAXP using identity transformer
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();

        // Step 5: Setup input and output for XSLT transformation 
        Source src = new DOMSource( doc );
        // Resulting SAX events (the generated FO) must be piped
        // through to FOP
        Result res = new SAXResult( fop.getDefaultHandler() );
        // Step 6: Start XSLT transformation and FOP processing
        transformer.transform( src, res );
    catch ( FOPException ex ) {
        ex.printStackTrace( System.err );
    catch ( TransformerConfigurationException ex ) {
        ex.printStackTrace( System.err );
    catch ( TransformerException ex ) {
        ex.printStackTrace( System.err );
    catch ( RuntimeException ex ) {
        ex.printStackTrace( System.err );
  private static final FopFactory C_FOP_FACTORY
        = FopFactory.newInstance();

Good luck.