Apache FOP 类型 FopFactory 中的方法 newInstance(FopFactoryConfig) 不适用于参数 ()

Apache FOP the method newInstance(FopFactoryConfig) in the type FopFactory is not applicable for the arguments ()

我收到以下错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:     The method newInstance(FopFactoryConfig) in the type FopFactory is not applicable for the arguments ()

    at fopdemo.fopvass.PDFHandler.createPDFFile(PDFHandler.java:42)
    at fopdemo.fopvass.TestPDF.main(TestPDF.java:40)

这是我的代码:

package fopdemo.fopvass;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
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.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;

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

public class PDFHandler {
    public static final String EXTENSION = ".pdf";
    public String PRESCRIPTION_URL = "template.xsl";

    public String createPDFFile(ByteArrayOutputStream xmlSource, String templateFilePath) throws IOException {
        File file = File.createTempFile("" + System.currentTimeMillis(), EXTENSION);
        URL url = new File(templateFilePath + PRESCRIPTION_URL).toURI().toURL();
        // creation of transform source
        StreamSource transformSource = new StreamSource(url.openStream());
        // create an instance of fop factory
        FopFactory fopFactory = FopFactory.newInstance();
        // a user agent is needed for transformation
        FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
        // to store output
        ByteArrayOutputStream pdfoutStream = new ByteArrayOutputStream();
        StreamSource source = new StreamSource(new ByteArrayInputStream(xmlSource.toByteArray()));
        Transformer xslfoTransformer;
        try {
            TransformerFactory transfact = TransformerFactory.newInstance();

            xslfoTransformer = transfact.newTransformer(transformSource);
            // Construct fop with desired output format
            Fop fop;
            try {
                fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, pdfoutStream);
                // Resulting SAX events (the generated FO)
                // must be piped through to FOP
                Result res = new SAXResult(fop.getDefaultHandler());

                // Start XSLT transformation and FOP processing
                try {
                    // everything will happen here..
                    xslfoTransformer.transform(source, res);

                    // if you want to save PDF file use the following code
                    OutputStream out = new java.io.FileOutputStream(file);
                    out = new java.io.BufferedOutputStream(out);
                    FileOutputStream str = new FileOutputStream(file);
                    str.write(pdfoutStream.toByteArray());
                    str.close();
                    out.close();

                } catch (TransformerException e) {
                    e.printStackTrace();
                }
            } catch (FOPException e) {
                e.printStackTrace();
            }
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerFactoryConfigurationError e) {
            e.printStackTrace();
        }
        return file.getPath();
    }

    public ByteArrayOutputStream getXMLSource(EmployeeData data) throws Exception {
        JAXBContext context;

        ByteArrayOutputStream outStream = new ByteArrayOutputStream();

        try {
            context = JAXBContext.newInstance(EmployeeData.class);
            Marshaller m = context.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            m.marshal(data, System.out);
            m.marshal(data, outStream);
        } catch (JAXBException e) {

            e.printStackTrace();
        }
        return outStream;

    }
}

这就是它的名字:

package fopdemo.fopvass;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
/**
 * @author Debasmita.Sahoo
 *
 */
public class TestPDF {
    public static void main(String args[]){
        System.out.println("Hi Testing");
        ArrayList employeeList = new ArrayList();
        String templateFilePath ="C:/Paula/Proyectos/fop/fopvass/resources/";

        Employee e1= new Employee();
        e1.setName("Debasmita1 Sahoo");
        e1.setEmployeeId("10001");
        e1.setAddress("Pune");
        employeeList.add(e1);

        Employee e2= new Employee();
        e2.setName("Debasmita2 Sahoo");
        e2.setEmployeeId("10002");
        e2.setAddress("Test");
        employeeList.add(e2);

        Employee e3= new Employee();
        e3.setName("Debasmita3 Sahoo");
        e3.setEmployeeId("10003");
        e3.setAddress("Mumbai");
        employeeList.add(e3);

        EmployeeData data = new EmployeeData();
        data.setEemployeeList(employeeList);
        PDFHandler handler = new PDFHandler();

        try {
            ByteArrayOutputStream streamSource = handler.getXMLSource(data);

            handler.createPDFFile(streamSource,templateFilePath);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

以下行无法编译:

FopFactory fopFactory = FopFactory.newInstance();

方法newInstance()需要一个参数。来自 documentation(由 Mathias Muller 提供),在 'Basic Usage' 下,该参数引用配置文件:

FopFactory fopFactory = FopFactory.newInstance(
  new File( "C:/Temp/fop.xconf" ) );

您需要为其提供一个 File 对象。或者,也可以通过提供 URI 来解析输入文件中的相对 URI 来创建 FopFactory 实例(因为 SVG 文件引用其他 SVG 文件)。正如 documentation 所暗示的那样:

FopFactory fopFactory = FopFactory.newInstance(
  new File(".").toURI() );

The user agent is the entity that allows you to interact with a single rendering run, i.e. the processing of a single document. If you wish to customize the user agent's behaviour, the first step is to create your own instance of FOUserAgent using the appropriate factory method on FopFactory and pass that to the factory method that will create a new Fop instance.

FopFactory.newInstance() 支持

FopFactory.newInstance(File)
FopFactory.newInstance(Uri)
FopFactory.newInstance(Uri, InputStream)
FopFactory.newInstance(FopFactoryConfig)

我在 Windows 和 Mac OS(即 Unix\Linux 类似 NFS)上成功实现了以下代码

URI uri = new File(config.getRoot()).toPath().toUri();
FopFactory fopFactory = FopFactory.newInstance(uri);

另一方面,这不起作用(我不明白为什么)

URI uri = new File(config.getRoot()).toUri();