开发者

Updating to the newest FOP API

I am l开发者_如何学Pythonooking to update an application to use the newest fop API. The application uses version 0.20 of the library, for example the Driver class is still being used.

I do not seem to be able to find any sensible information on how to update to the newest fop version. I found some snippets, eg that the FOPFactory should be used.

So I was wondering if anyone could give me a push to resources on updating to the new(est) fop API? Or is it not really possible to update and do I need to rewrite this part of the application?

PS I have found the fop upgrading page (of course) but it does not seem to point out the similarities between classes/APIs of both versions.


The answer is that it really depends on how much you use of FOP.

When we updated from a .2* version to the .9, then 1.0 version, we had to revise quite a bit, but we generated a large variety of FO (we transformed FO to PostScript) and we needed to generate a variety of PostScripts to see what worked and what failed in the newer versions. You will likely find an easy replacement for anything that might have vanished based on what you used to do.

We had, luckily, consolidated references to the Driver and Fop classes to a custom utility and therefore it wasn't so bad a transition to simply swap out a new utility reference that used the FopFactory.

Your first step should be to write an abstract Facade as @Wivani suggests to consolidate your older fop reference calls, make sure that works in your code, then move forward by replacing your older Facade with an implementation that uses the newer code.

The only thing that has displeased me about Fop is that the community takes quite a while to apply bug patches and generate new releases. We used v1 in production with XML Graphics 1.4 with no problems (basic FO to generate PostScript and TIFF files).

I will supplement this post on Monday with some helper code if you need it.

UPDATE Here is what we used to use to convert a FO file to PostScript:

String foAsString = "your_fo_as_string";
File destination = new File("path_to_file"");

BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(destination));
Driver driver = new Driver(new InputSource(new StringReader(foAsString)), out);
driver.setRenderer(Driver.RENDER_PS);
driver.run();

out.close();

return destination;

Here is an abstract of what we use now.

FopFactory is created as instance-level as

this.fopFactory = FopFactory.newInstance();

code:

import javax.xml.transform.Source;
import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

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

// ...

String foAsString = "your_fo_as_string";
File destination = new File("path_to_file"");

OutputStream outStream = new BufferedOutputStream(new FileOutputStream(destination));
Fop fop = fopFactory.newFop(MimeConstants.MIME_POSTSCRIPT, outStream);

Transformer transformer = transformerFactory.newTransformer();
Source source = new StreamSource(new StringReader(foAsString));
Result result = new SAXResult(fop.getDefaultHandler());

transformer.transform(source, result);

outStream.close();

return destination;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜