checking in version 3.0
[TestXSLT.git] / FOPWrapper.java
1
2 import java.io.*;
3
4 import org.xml.sax.*;
5 import com.apple.cocoa.foundation.NSData;
6
7 //Avalon
8 import org.apache.avalon.framework.ExceptionUtil;
9 import org.apache.avalon.framework.logger.Logger;
10 import org.apache.avalon.framework.logger.ConsoleLogger;
11
12 //FOP
13 import org.apache.fop.apps.Driver;
14 import org.apache.fop.apps.FOPException;
15 import org.apache.fop.messaging.MessageHandler;
16
17 /**
18  * This class helps calling FOP from the Obj-C side.
19  * Based on "ExampleFO2PDF.java" from the FOP project.
20  */
21 public class FOPWrapper {
22
23     String error;
24     boolean errorOccurred = false;
25         
26         public NSData convert(NSData foData) throws IOException, FOPException {
27         
28                 //Construct driver
29         System.setProperty("java.awt.headless", "true");
30                 Driver driver = new Driver();
31                 
32                 //Setup logger
33                 Logger logger = new ConsoleLogger(ConsoleLogger.LEVEL_INFO);
34                 driver.setLogger(logger);
35                 MessageHandler.setScreenLogger(logger);
36                 
37                 //Setup Renderer (output format)        
38                 driver.setRenderer(Driver.RENDER_PDF);
39         
40                 //Setup output
41                 ByteArrayOutputStream out = new java.io.ByteArrayOutputStream();
42                 try {
43                         driver.setOutputStream(out);
44                         
45                         //Setup input
46                         InputStream in = new java.io.ByteArrayInputStream(foData.bytes(0, foData.length()));
47                         try {
48                                 driver.setInputSource(new InputSource(in));
49                                 
50                                 //Process FO
51                                 driver.run();
52                         } catch (Exception e) {
53                                 error = e.toString();
54                                 errorOccurred = true;
55                         } finally {
56                                 in.close();
57                         }
58                         
59                 } finally {
60                         out.close();
61                 }
62                 
63                 if (errorOccurred) {
64                         return null;
65                 }
66                 return new NSData(out.toByteArray());
67                 
68         }
69         
70         public String getErrorMessage() {
71                 return error;
72         }
73         
74         public boolean errorOccurred() {
75                 return errorOccurred;
76         }
77         
78         
79         /*
80     public static void main(String[] args) {
81         try {
82             System.out.println("FOP ExampleFO2PDF\n");
83             System.out.println("Preparing...");
84             
85             //Setup directories
86             File baseDir = new File(".");
87             File outDir = new File(baseDir, "out");
88             outDir.mkdirs();
89                         
90             //Setup input and output files            
91             File fofile = new File(baseDir, "xml/fo/helloworld.fo");
92             File pdffile = new File(outDir, "ResultFO2PDF.pdf");
93                         
94             System.out.println("Input: XSL-FO (" + fofile + ")");
95             System.out.println("Output: PDF (" + pdffile + ")");
96             System.out.println();
97             System.out.println("Transforming...");
98             
99             FOPWrapper app = new FOPWrapper();
100             app.convertFO2PDF(fofile, pdffile);
101             
102             System.out.println("Success!");
103         } catch (Exception e) {
104             System.err.println(ExceptionUtil.printStackTrace(e));
105             System.exit(-1);
106         }
107     }
108          
109          */
110         
111 }