Apache FOP 类型 FopFactory 中的方法 newInstance(FopFactoryConfig) 不适用于参数 () [英] Apache FOP the method newInstance(FopFactoryConfig) in the type FopFactory is not applicable for the arguments ()

查看:61
本文介绍了Apache FOP 类型 FopFactory 中的方法 newInstance(FopFactoryConfig) 不适用于参数 ()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误:

线程main"中的异常java.lang.Error:未解决的编译问题:FopFactory类型中的方法newInstance(FopFactoryConfig)不适用于参数()在 fopdemo.fopvass.PDFHandler.createPDFFile(PDFHandler.java:42)在 fopdemo.fopvass.TestPDF.main(TestPDF.java:40)

这是我的代码:

包 fopdemo.fopvass;导入 java.io.ByteArrayInputStream;导入 java.io.ByteArrayOutputStream;导入 java.io.File;导入 java.io.FileOutputStream;导入 java.io.IOException;导入 java.io.OutputStream;导入 java.net.URL;导入 javax.xml.bind.JAXBContext;导入 javax.xml.bind.JAXBException;导入 javax.xml.bind.Marshaller;导入 javax.xml.transform.Result;导入 javax.xml.transform.Source;导入 javax.xml.transform.Transformer;导入 javax.xml.transform.TransformerConfigurationException;导入 javax.xml.transform.TransformerException;导入 javax.xml.transform.TransformerFactory;导入 javax.xml.transform.TransformerFactoryConfigurationError;导入 javax.xml.transform.dom.DOMResult;导入 javax.xml.transform.dom.DOMSource;导入 javax.xml.transform.sax.SAXResult;导入 javax.xml.transform.stream.StreamSource;导入 org.apache.fop.apps.FOPException;导入 org.apache.fop.apps.FOUserAgent;导入 org.apache.fop.apps.Fop;导入 org.apache.fop.apps.FopFactory;导入 org.apache.fop.apps.MimeConstants;公共类 PDFHandler {public static final String EXTENSION = ".pdf";public String PRESCRIPTION_URL = "template.xsl";公共字符串 createPDFFile(ByteArrayOutputStream xmlSource, String templateFilePath) 抛出 IOException {File file = File.createTempFile("" + System.currentTimeMillis(), EXTENSION);URL url = 新文件(模板文件路径 + PRESCRIPTION_URL).toURI().toURL();//创建变换源StreamSource transformSource = new StreamSource(url.openStream());//创建一个 fop 工厂的实例FopFactory fopFactory = FopFactory.newInstance();//转换需要用户代理FOUserAgent foUserAgent = fopFactory.newFOUserAgent();//存储输出ByteArrayOutputStream pdfoutStream = new ByteArrayOutputStream();StreamSource source = new StreamSource(new ByteArrayInputStream(xmlSource.toByteArray()));变压器 xslfoTransformer;尝试 {TransformerFactory transfact = TransformerFactory.newInstance();xslfoTransformer = transfact.newTransformer(transformSource);//用所需的输出格式构造 fop扑扑;尝试 {fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, pdfoutStream);//结果 SAX 事件(生成的 FO)//必须通过管道传送到 FOP结果 res = new SAXResult(fop.getDefaultHandler());//启动 XSLT 转换和 FOP 处理尝试 {//一切都会在这里发生..xslfoTransformer.transform(source, res);//如果你想保存PDF文件使用下面的代码OutputStream out = new java.io.FileOutputStream(file);out = new java.io.BufferedOutputStream(out);FileOutputStream str = new FileOutputStream(file);str.write(pdfoutStream.toByteArray());str.close();关闭();} catch (TransformerException e) {e.printStackTrace();}} catch (FOPException e) {e.printStackTrace();}} catch (TransformerConfigurationException e) {e.printStackTrace();} catch (TransformerFactoryConfigurationError e) {e.printStackTrace();}返回文件.getPath();}public ByteArrayOutputStream getXMLSource(EmployeeData data) 抛出异常 {JAXBContext 上下文;ByteArrayOutputStream outStream = new ByteArrayOutputStream();尝试 {context = JAXBContext.newInstance(EmployeeData.class);Marshaller m = context.createMarshaller();m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);m.marshal(数据,System.out);m.marshal(数据,outStream);} catch (JAXBException e) {e.printStackTrace();}返回输出流;}}

这就是它的名字:

包 fopdemo.fopvass;导入 java.io.ByteArrayOutputStream;导入 java.util.ArrayList;/*** @author Debasmita.Sahoo**/公共类 TestPDF {公共静态无效主(字符串参数[]){System.out.println("嗨测试");ArrayList 员工列表 = 新的 ArrayList();String templateFilePath ="C:/Paula/Proyectos/fop/fopvass/resources/";员工 e1= 新员工();e1.setName("Debasmita1 Sahoo");e1.setEmployeeId("10001");e1.setAddress("浦那");员工名单.add(e1);员工 e2= 新员工();e2.setName("Debasmita2 Sahoo");e2.setEmployeeId("10002");e2.setAddress("测试");员工名单.add(e2);员工 e3= 新员工();e3.setName("Debasmita3 Sahoo");e3.setEmployeeId("10003");e3.setAddress("孟买");员工名单.add(e3);员工数据数据 = 新员工数据();data.setEemployeeList(employeeList);PDFHandler handler = new PDFHandler();尝试 {ByteArrayOutputStream streamSource = handler.getXMLSource(data);handler.createPDFFile(streamSource,templateFilePath);} 捕获(异常 e){//TODO 自动生成的 catch 块e.printStackTrace();}}}

解决方案

以下行无法编译:

FopFactory fopFactory = FopFactory.newInstance();

方法 newInstance() 需要一个参数.来自 文档(由 Mathias Muller 提供),在基本用法"下,该参数指的是一个配置文件:

FopFactory fopFactory = FopFactory.newInstance(新文件(C:/Temp/fop.xconf"));

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

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

<块引用>

用户代理是允许您与单个渲染运行进行交互的实体,即处理单个文档.如果您希望自定义用户代理的行为,第一步是使用 FopFactory 上的适当工厂方法创建您自己的 FOUserAgent 实例并将其传递给工厂方法将创建一个新的 Fop 实例.

I am getting the following error:

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)

This is my code:

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;

    }
}

This is where it is called:

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

解决方案

The following line won't compile:

FopFactory fopFactory = FopFactory.newInstance();

The method newInstance() requires a parameter. From the documentation (provided by Mathias Muller), under 'Basic Usage', that parameter refers to a configuration file:

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

You need to provide it a File object. Alternatively, it is also possible to create a FopFactory instance by providing a URI to resolve relative URIs in the input file (because SVG files reference other SVG files). As the documentation suggests:

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.

这篇关于Apache FOP 类型 FopFactory 中的方法 newInstance(FopFactoryConfig) 不适用于参数 ()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆