5 import com.apple.cocoa.foundation.NSData;
8 import org.apache.avalon.framework.ExceptionUtil;
9 import org.apache.avalon.framework.logger.Logger;
10 import org.apache.avalon.framework.logger.ConsoleLogger;
13 import org.apache.fop.apps.Driver;
14 import org.apache.fop.apps.FOPException;
15 import org.apache.fop.messaging.MessageHandler;
18 * This class helps calling FOP from the Obj-C side.
19 * Based on "ExampleFO2PDF.java" from the FOP project.
21 public class FOPWrapper {
24 boolean errorOccurred = false;
26 public NSData convert(NSData foData) throws IOException, FOPException {
29 Driver driver = new Driver();
32 Logger logger = new ConsoleLogger(ConsoleLogger.LEVEL_INFO);
33 driver.setLogger(logger);
34 MessageHandler.setScreenLogger(logger);
36 //Setup Renderer (output format)
37 driver.setRenderer(Driver.RENDER_PDF);
40 ByteArrayOutputStream out = new java.io.ByteArrayOutputStream();
42 driver.setOutputStream(out);
45 InputStream in = new java.io.ByteArrayInputStream(foData.bytes(0, foData.length()));
47 driver.setInputSource(new InputSource(in));
51 } catch (Exception e) {
62 return new NSData(out.toByteArray());
66 public String getErrorMessage() {
70 public boolean errorOccurred() {
76 public static void main(String[] args) {
78 System.out.println("FOP ExampleFO2PDF\n");
79 System.out.println("Preparing...");
82 File baseDir = new File(".");
83 File outDir = new File(baseDir, "out");
86 //Setup input and output files
87 File fofile = new File(baseDir, "xml/fo/helloworld.fo");
88 File pdffile = new File(outDir, "ResultFO2PDF.pdf");
90 System.out.println("Input: XSL-FO (" + fofile + ")");
91 System.out.println("Output: PDF (" + pdffile + ")");
93 System.out.println("Transforming...");
95 FOPWrapper app = new FOPWrapper();
96 app.convertFO2PDF(fofile, pdffile);
98 System.out.println("Success!");
99 } catch (Exception e) {
100 System.err.println(ExceptionUtil.printStackTrace(e));