Apache FOP 1.1 > 2.1 Migration

1k Views Asked by At

I have been tasked with migrating an old Java application which uses Apache FOP 1.1. I'd like to use the latest FOP version, 2.1, but am having issues with the code that sets up FOP.

Here is the original code we have, using the 1.1 API:

FopFactory fopFactory = FopFactory.newInstance();
ByteArrayOutputStream out = new ByteArrayOutputStream();
ConfigurationDataUtility configurationData = new ConfigurationDataUtility();

    try
    {
        Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);

Here is the new code, using the 2.1 API:

    InputStream is = getClass().getClassLoader().getResourceAsStream("fop.xconf");
    FopConfParser parser = new FopConfParser(is, new java.net.URI(".")); //parsing configuration  
    FopFactoryBuilder builder = parser.getFopFactoryBuilder(); //building the factory with the user options
    FopFactory fopFactory = builder.build();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ConfigurationDataUtility configurationData = new ConfigurationDataUtility();

    try
    {
        Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);

Sadly, FOP has very little documentation on its website and what is there is mostly for version 1.1. The above code was taken directory from their page in the section stating how to migrate from 1.1 to 2.1 so I think it is correct.

https://xmlgraphics.apache.org/fop/2.1/upgrading.html

The problem is that when this new code is executed, the following exception is thrown on the line that constructs the new FopConfParser:

Caused by: java.lang.Error: Unable to setup SAX parserjava.lang.ClassCastException: __redirected.__SAXParserFactory cannot be cast to javax.xml.parsers.SAXParserFactory
    at org.apache.avalon.framework.configuration.DefaultConfigurationBuilder.<init>(DefaultConfigurationBuilder.java:112)
    at org.apache.avalon.framework.configuration.DefaultConfigurationBuilder.<init>(DefaultConfigurationBuilder.java:83)
    at org.apache.fop.apps.FopConfParser.<init>(FopConfParser.java:146)
    at org.apache.fop.apps.FopConfParser.<init>(FopConfParser.java:88)
    at org.apache.fop.apps.FopConfParser.<init>(FopConfParser.java:103)
    at xmltools.PDFGenerator.generatePDF(PDFGenerator.java:73)
    at defect.DefectServlet.generateDefectPrintablePdf(DefectServlet.java:703)
    ... 35 more

We are running Wildfly 9 as our server. As such, I've added the following to FOP's module.xml file because I was getting ClassNotFound exceptions without them:

    <dependencies>
        <module name="org.apache.avalon" />
        <module name="org.xml.sax" />
        <module name="org.apache.xmlgraphics" />
    </dependencies>

I've added the following to my JVM startup, hoping it would shed more light on the situation but it did not:

-Djaxp.debug=1

I've been looking at this for the better part of 3 days now with no progress made. Any suggestions would be much appreciated.

Thanks.

1

There are 1 best solutions below

0
On

It works for me changing few lines related to parser this way:

File xconf = new File("\\myapp\\conf\\fop.xconf"); 
FopConfParser parser = new FopConfParser(xconf);
FopFactoryBuilder builder = parser.getFopFactoryBuilder();