Generate an areatree file from Java

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Generate an areatree file from Java

Pieter Masereeuw

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 (836 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

RE: Generate an areatree file from Java

Simon Steiner

Hi,

 

PDFVTTestCase.java has a example for fo to IF

 

Thanks

 

From: Pieter Masereeuw [mailto:[hidden email]]
Sent: 28 May 2018 16:08
To: [hidden email]
Subject: Generate an areatree file from Java

 

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);
        }
    }
}

Reply | Threaded
Open this post in threaded view
|

Re: Generate an areatree file from Java

Pieter Masereeuw

Thank you, that has put me on the way. I got it working.

Pieter


On 05/28/2018 07:44 PM, Simon Steiner wrote:

Hi,

 

PDFVTTestCase.java has a example for fo to IF

 

Thanks

 

From: Pieter Masereeuw [[hidden email]]
Sent: 28 May 2018 16:08
To: [hidden email]
Subject: Generate an areatree file from Java

 

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 (836 bytes) Download Attachment