Generate an areatree file from Java

Pieter Masereeuw <[email protected]>
Newsgroups gmane.text.xml.fop.user
Message-ID <[email protected]>
Hi -

I am trying to create some XMLCalabash (XProc) extensions that work with
Fop. One of them is that I would like to generate an areatree file from
a standard FO file.

Although not being excellent in Java, I managed to get this running. But
as soon as the FO file contains an instream-foreign-object, I get a
NullPointerException.

If I try do do the same with the Fop command line, everyting works fine,
so I think that the way I invoke Fop from Java is not correct. Maybe I
need to pass some handler for SVN objects to the FopFactoryBuilder, but
I neither know how to do that, nor if this is really the right approach.

The problem exists in Fop 2.2 and Fop 2.3. I am using JDK  1.8.

So the question is: *how do I use Fop to convert an FO file to an
areatree (and maybe an intermediate file too) from Java?*

Below is my Java code plus the top of the stacktrace.

Any help will be greatly appreciated.

Pieter

------------------------------

ERROR: Error while rendering page 1. Reason: java.lang.NullPointerException
java.lang.NullPointerException
    at
org.apache.fop.render.AbstractRenderer.renderXML(AbstractRenderer.java:930)
~[fop.jar:?]
    at
org.apache.fop.render.xml.XMLRenderer.renderForeignObject(XMLRenderer.java:785)
~[fop.jar:?]
    at
org.apache.fop.render.AbstractRenderer.renderInlineViewport(AbstractRenderer.java:864)
~[fop.jar:?]
    at
org.apache.fop.render.xml.XMLRenderer.renderInlineViewport(XMLRenderer.java:741)
~[fop.jar:?]
    at
org.apache.fop.render.AbstractRenderer.renderInlineArea(AbstractRenderer.java:717)
~[fop.jar:?]
    at
org.apache.fop.render.xml.XMLRenderer.renderInlineArea(XMLRenderer.java:722)
~[fop.jar:?]
    at
org.apache.fop.render.AbstractRenderer.renderLineArea(AbstractRenderer.java:692)
~[fop.jar:?]

------------------------------

package nl.masereeuw.calabash.steps;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URI;
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 net.sf.saxon.s9api.SaxonApiException;
import org.apache.avalon.framework.configuration.Configuration;
import
org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.FopFactoryBuilder;
import org.apache.fop.apps.MimeConstants;


public class TestAreatree {

    File INPUTFILE_WITH_INSTREAM_FOREIGN_OBJECTS = new
File("testareatree-with-instream-foreign-objects.fo");
    File INPUTFILE_WITHOUT_INSTREAM_FOREIGN_OBJECTS = new
File("testareatree-without-instream-foreign-objects.fo");
    File INPUTFILE_TO_BE_USED = INPUTFILE_WITH_INSTREAM_FOREIGN_OBJECTS;
// Set either to INPUTFILE_WITH_INSTREAM_FOREIGN_OBJECTS or
INPUTFILE_WITHOUT_INSTREAM_FOREIGN_OBJECTS

    File OUTPUTFILE = new File("testareatree.xml");
    File FOP_XCONF = new File("fop.xconf");

    public void run() throws SaxonApiException {
        try {
            FopFactory fopFactory;
            if (false) {
                // Under construction, perhaps we should somehow
register some kind of handler for SVG inline-foreign-object elements?
                URI baseUri = null;  // TODO Replace null with correct
base-uri, but what should be its value?
                FopFactoryBuilder fopFactoryBuilder = new
FopFactoryBuilder(baseUri);
                fopFactoryBuilder.setSourceResolution(72);
                Configuration configuration = new
DefaultConfigurationBuilder(true).buildFromFile(FOP_XCONF);
                fopFactoryBuilder.setConfiguration(configuration);
                fopFactory = fopFactoryBuilder.build();
            } else {
                fopFactory = FopFactory.newInstance(FOP_XCONF);
            }

            Source source = new StreamSource(INPUTFILE_TO_BE_USED);

            try (OutputStream outputStream = new
BufferedOutputStream(new FileOutputStream(OUTPUTFILE))) {
                Fop fop =
fopFactory.newFop(MimeConstants.MIME_FOP_AREA_TREE, outputStream);

                TransformerFactory factory =
TransformerFactory.newInstance();
                Transformer transformer = factory.newTransformer();
                Result result = new SAXResult(fop.getDefaultHandler());
                transformer.transform(source, result);
            }
        } catch (Exception ex) {
            throw new SaxonApiException(ex);
        }
    }

    public static void main(String args[]) {
        try {
            new TestAreatree().run();
        } catch (SaxonApiException ex) {
            ex.printStackTrace(System.err);
        }
    }
}
signature.asc (application/pgp-signature, 819 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQIcBAEBCAAGBQJbDBtRAAoJEJeiKWBjGlmPADYP/1fMhVi/2pFWuFb5nDA/AZk6
1Slq+XQiLFtjYfv7B6AyPSU1i7/RZWdw0+MHumC5klAso+UIUaLs+NlgD3x/7GuD
Hfx+aMiC0gOmv3jXfsNGQ005ts8yBwqOHzh3JgzLwdJTipXrMIufaYey1em/qReL
oTDJFORrMb48dpf3lJvdvhLGp2yGEQeWe/b7jxe7AKgV1a98tLbrv+9HYPjBgVKO
USIbHmPisdKF+4ApHFkd1CyxUkFLEPxm0OxoApXUibChxlUjfNV7sBtzgXq8PQNg
oa84dhIVgQ44AErGvER/HUGCo2HCow65rbynCreQxIe8/cQS8IOQbsFtD6LjXfsF
sYS9tB2IJ63YSY168kgG59YL5kwfRAzagS9AJaMHqhxnF9sn9+ccf+3lB9h494DS
zu+75B438ox9DSHq8WdckLhkGFypt8J9VrbxZMb8PBM1dR11HWCtpyN9hdXEr9ZR
0qU/ECrIgPHUzNxnGw3Dm06aGm5w/5HVv1mG6wWvEmAG6qexqFsou9Ll8zo9KGmj
u59x6hG84/+dVw+RlLwSxlgcEQ5vryKJvSt6FBF9eE8WghInHLWM48zeEnvUOQyH
yKxiy25lhtNMXzONiu2S6EdV29Ssr2bAH5i4vG5hDTdgYKf0zaCj5FgWKoXo6M5x
bn/I+LGjHQjyH/t+qZfF
=CmE+
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.