Apache防在类型FopFactory该方法的newInstance(FopFactoryConfig)是不适用的参数() [英] Apache FOP the method newInstance(FopFactoryConfig) in the type FopFactory is not applicable for the arguments ()
本文介绍了Apache防在类型FopFactory该方法的newInstance(FopFactoryConfig)是不适用的参数()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到以下错误:
异常线程mainjava.lang.Error:未解决问题的编译:该方法的newInstance(FopFactoryConfig)在类型FopFactory不适用于()的参数 在fopdemo.fopvass.PDFHandler.createPDFFile(PDFHandler.java:42)
在fopdemo.fopvass.TestPDF.main(TestPDF.java:40)
这是我的code:
包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 {
公共静态最后弦乐延长=.PDF;
公共字符串preSCRIPTION_URL =template.xsl; 公共字符串createPDFFile(ByteArrayOutputStream xmlSource,字符串templateFilePath)抛出IOException
文件file = File.createTempFile(+ System.currentTimeMillis的(),续期);
网址URL =新的文件(templateFilePath + preSCRIPTION_URL).toURI()的toURL()。
//创建变换源
StreamSource的transformSource =新的StreamSource(url.openStream());
//创建花花公子工厂的实例
FopFactory fopFactory = FopFactory.newInstance();
//需要进行改造的用户代理
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
//保存输出
ByteArrayOutputStream pdfoutStream =新ByteArrayOutputStream();
StreamSource的源=新的StreamSource(新ByteArrayInputStream进行(xmlSource.toByteArray()));
变压器xslfoTransformer;
尝试{
TransformerFactory的transfact = TransformerFactory.newInstance(); xslfoTransformer = transfact.newTransformer(transformSource);
//构造与FOP所需的输出格式
FOP FOP;
尝试{
FOP = fopFactory.newFop(MimeConstants.MIME_PDF,foUserAgent,pdfoutStream);
//致使SAX事件(生成的FO)
//必须通过对FOP通过管道输送
结果解析度=新的SAXResult(fop.getDefaultHandler()); //开始XSLT转换和FOP处理
尝试{
//一切都将发生在这里..
xslfoTransformer.transform(源,RES); //如果你想保存的PDF文件使用下面的code
出的OutputStream =新java.io.FileOutputStream中(文件);
OUT =新java.io.BufferedOutputStream(出);
的FileOutputStream海峡=新的FileOutputStream(文件);
str.write(pdfoutStream.toByteArray());
str.close();
out.close(); }赶上(TransformerException中五){
e.printStackTrace();
}
}赶上(FOPException E){
e.printStackTrace();
}
}赶上(TransformerConfigurationException E){
e.printStackTrace();
}赶上(TransformerFactoryConfigurationError E){
e.printStackTrace();
}
返回file.getPath();
} 公共ByteArrayOutputStream getXMLSource(EmployeeData工作数据)抛出异常{
JAXBContext而背景; ByteArrayOutputStream outStream =新ByteArrayOutputStream(); 尝试{
上下文= JAXBContext.newInstance(EmployeeData.class);
编组M = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,Boolean.TRUE);
m.marshal(数据下,System.out);
m.marshal(数据,outStream);
}赶上(JAXBException E){ e.printStackTrace();
}
返回outStream; }
}
这是它被称为:
包fopdemo.fopvass;进口java.io.ByteArrayOutputStream中;
进口的java.util.ArrayList;
/ **
* @author Debasmita.Sahoo
*
* /
公共类TestPDF {
公共静态无效的主要(字符串ARGS []){
的System.out.println(嗨测试);
ArrayList的EmployeeList的=新的ArrayList();
字符串templateFilePath =C:/保拉/ PROYECTOS / FOP / fopvass /资源/; 员工E1 =新员工();
e1.setName(Debasmita1 Sahoo);
e1.setEmployeeId(10001);
e1.setAddress(普纳);
employeeList.add(E1); 员工E2 =新员工();
e2.setName(Debasmita2 Sahoo);
e2.setEmployeeId(10002);
e2.setAddress(测试);
employeeList.add(E2); 员工E3 =新员工();
e3.setName(Debasmita3 Sahoo);
e3.setEmployeeId(10003);
e3.setAddress(孟买);
employeeList.add(E3); EmployeeData工作数据=新EmployeeData工作();
data.setEemployeeList(EmployeeList的);
PDFHandler处理程序=新PDFHandler(); 尝试{
ByteArrayOutputStream的StreamSource = handler.getXMLSource(数据); handler.createPDFFile(StreamSource的,templateFilePath);
}赶上(例外五){
// TODO自动生成catch块
e.printStackTrace();
}
}
}
解决方案
这是你的问题:
FopFactory fopFactory = FopFactory.newInstance();
如果你看一下code表示法,的newInstance()
,它需要一个参数。您需要提供该参数。
从文档(由马蒂亚斯·穆勒提供),在基本用法
FopFactory fopFactory = FopFactory.newInstance(新文件(C:/Temp/fop.xconf));
您需要提供它文件
对象。
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();
}
}
}
解决方案
This is your problem:
FopFactory fopFactory = FopFactory.newInstance();
If you look at the code for that method, newInstance()
, it takes a parameter. You need to supply that parameter.
From the documentation (provided by Mathias Muller), under 'Basic Usage':
FopFactory fopFactory = FopFactory.newInstance(new File("C:/Temp/fop.xconf"));
You need to provide it a File
object.
这篇关于Apache防在类型FopFactory该方法的newInstance(FopFactoryConfig)是不适用的参数()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文