1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 /* $Id: ExampleAWTViewer.java 1851806 2019-01-22 11:53:31Z ssteiner $ */ 19 20 package embedding; 21 22 //Java 23 import java.io.File; 24 import java.io.IOException; 25 26 import javax.xml.transform.Result; 27 import javax.xml.transform.Source; 28 import javax.xml.transform.Transformer; 29 import javax.xml.transform.TransformerException; 30 import javax.xml.transform.TransformerFactory; 31 import javax.xml.transform.sax.SAXResult; 32 import javax.xml.transform.stream.StreamSource; 33 34 import org.apache.fop.apps.FOPException; 35 import org.apache.fop.apps.Fop; 36 import org.apache.fop.apps.FopFactory; 37 import org.apache.fop.apps.MimeConstants; 38 39 /** 40 * This class demonstrates the use of the AWT Viewer. 41 */ 42 public class ExampleAWTViewer { 43 44 // configure fopFactory as desired 45 private final FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI()); 46 47 /** 48 * Display an FO file in the AWT Preview. 49 * @param fo the FO file 50 * @throws IOException In case of an I/O problem 51 * @throws FOPException In case of a problem during layout 52 * @throws TransformerException In case of a problem during XML processing 53 */ viewFO(File fo)54 public void viewFO(File fo) 55 throws IOException, FOPException, TransformerException { 56 57 //Setup FOP 58 Fop fop = fopFactory.newFop(MimeConstants.MIME_FOP_AWT_PREVIEW); 59 60 try { 61 62 //Load XSL-FO file (you can also do an XSL transformation here) 63 TransformerFactory factory = TransformerFactory.newInstance(); 64 Transformer transformer = factory.newTransformer(); 65 Source src = new StreamSource(fo); 66 Result res = new SAXResult(fop.getDefaultHandler()); 67 transformer.transform(src, res); 68 69 } catch (Exception e) { 70 if (e instanceof FOPException) { 71 throw (FOPException)e; 72 } 73 throw new FOPException(e); 74 } 75 } 76 77 /** 78 * Main method. 79 * @param args the command-line arguments 80 */ main(String[] args)81 public static void main(String[] args) { 82 try { 83 System.out.println("FOP ExampleAWTViewer\n"); 84 System.out.println("Preparing..."); 85 86 //Setup directories 87 File baseDir = new File("."); 88 File outDir = new File(baseDir, "out"); 89 outDir.mkdirs(); 90 91 //Setup input and output files 92 File fofile = new File(baseDir, "xml/fo/helloworld.fo"); 93 94 System.out.println("Input: XSL-FO (" + fofile + ")"); 95 System.out.println("Output: AWT Viewer"); 96 System.out.println(); 97 System.out.println("Starting AWT Viewer..."); 98 99 ExampleAWTViewer app = new ExampleAWTViewer(); 100 app.viewFO(fofile); 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