updated libxml2 to 2.5.10
[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                 Driver driver = new Driver();
30         
31                 //Setup logger
32                 Logger logger = new ConsoleLogger(ConsoleLogger.LEVEL_INFO);
33                 driver.setLogger(logger);
34                 MessageHandler.setScreenLogger(logger);
35                 
36                 //Setup Renderer (output format)        
37                 driver.setRenderer(Driver.RENDER_PDF);
38         
39                 //Setup output
40                 ByteArrayOutputStream out = new java.io.ByteArrayOutputStream();
41                 try {
42                         driver.setOutputStream(out);
43                         
44                         //Setup input
45                         InputStream in = new java.io.ByteArrayInputStream(foData.bytes(0, foData.length()));
46                         try {
47                                 driver.setInputSource(new InputSource(in));
48                                 
49                                 //Process FO
50                                 driver.run();
51                         } catch (Exception e) {
52                                 error = e.toString();
53                                 errorOccurred = true;
54                         } finally {
55                                 in.close();
56                         }
57                         
58                 } finally {
59                         out.close();
60                 }
61                 
62                 return new NSData(out.toByteArray());
63                 
64         }
65         
66         public String getErrorMessage() {
67                 return error;
68         }
69         
70         public boolean errorOccurred() {
71                 return errorOccurred;
72         }
73         
74         
75         /*
76     public static void main(String[] args) {
77         try {
78             System.out.println("FOP ExampleFO2PDF\n");
79             System.out.println("Preparing...");
80             
81             //Setup directories
82             File baseDir = new File(".");
83             File outDir = new File(baseDir, "out");
84             outDir.mkdirs();
85                         
86             //Setup input and output files            
87             File fofile = new File(baseDir, "xml/fo/helloworld.fo");
88             File pdffile = new File(outDir, "ResultFO2PDF.pdf");
89                         
90             System.out.println("Input: XSL-FO (" + fofile + ")");
91             System.out.println("Output: PDF (" + pdffile + ")");
92             System.out.println();
93             System.out.println("Transforming...");
94             
95             FOPWrapper app = new FOPWrapper();
96             app.convertFO2PDF(fofile, pdffile);
97             
98             System.out.println("Success!");
99         } catch (Exception e) {
100             System.err.println(ExceptionUtil.printStackTrace(e));
101             System.exit(-1);
102         }
103     }
104          
105          */
106         
107 }