1 17 18 19 20 package embedding; 21 22 import java.io.File ; 24 import java.io.IOException ; 25 26 import javax.xml.transform.Transformer ; 28 import javax.xml.transform.TransformerFactory ; 29 import javax.xml.transform.TransformerException ; 30 import javax.xml.transform.Source ; 31 import javax.xml.transform.Result ; 32 import javax.xml.transform.stream.StreamSource ; 33 import javax.xml.transform.sax.SAXResult ; 34 35 import org.apache.avalon.framework.ExceptionUtil; 37 38 import org.apache.fop.apps.FOPException; 40 import org.apache.fop.apps.Fop; 41 import org.apache.fop.apps.FopFactory; 42 import org.apache.fop.apps.MimeConstants; 43 44 47 public class ExampleAWTViewer { 48 49 private FopFactory fopFactory = FopFactory.newInstance(); 51 52 59 public void viewFO(File fo) 60 throws IOException , FOPException, TransformerException { 61 62 Fop fop = fopFactory.newFop(MimeConstants.MIME_FOP_AWT_PREVIEW); 64 65 try { 66 67 TransformerFactory factory = TransformerFactory.newInstance(); 69 Transformer transformer = factory.newTransformer(); 70 Source src = new StreamSource (fo); 71 Result res = new SAXResult (fop.getDefaultHandler()); 72 transformer.transform(src, res); 73 74 } catch (Exception e) { 75 if (e instanceof FOPException) { 76 throw (FOPException)e; 77 } 78 throw new FOPException(e); 79 } 80 } 81 82 86 public static void main(String [] args) { 87 try { 88 System.out.println("FOP ExampleAWTViewer\n"); 89 System.out.println("Preparing..."); 90 91 File baseDir = new File ("."); 93 File outDir = new File (baseDir, "out"); 94 outDir.mkdirs(); 95 96 File fofile = new File (baseDir, "xml/fo/helloworld.fo"); 98 99 System.out.println("Input: XSL-FO (" + fofile + ")"); 100 System.out.println("Output: AWT Viewer"); 101 System.out.println(); 102 System.out.println("Starting AWT Viewer..."); 103 104 ExampleAWTViewer app = new ExampleAWTViewer(); 105 app.viewFO(fofile); 106 107 System.out.println("Success!"); 108 } catch (Exception e) { 109 System.err.println(ExceptionUtil.printStackTrace(e)); 110 System.exit(-1); 111 } 112 } 113 } 114 | Popular Tags |