KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > embedding > ExampleAWTViewer


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 426576 2006-07-28 15:44:37Z jeremias $ */
19  
20 package embedding;
21
22 //Java
23
import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25
26 //JAXP
27
import javax.xml.transform.Transformer JavaDoc;
28 import javax.xml.transform.TransformerFactory JavaDoc;
29 import javax.xml.transform.TransformerException JavaDoc;
30 import javax.xml.transform.Source JavaDoc;
31 import javax.xml.transform.Result JavaDoc;
32 import javax.xml.transform.stream.StreamSource JavaDoc;
33 import javax.xml.transform.sax.SAXResult JavaDoc;
34
35 //Avalon
36
import org.apache.avalon.framework.ExceptionUtil;
37
38 //FOP
39
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 /**
45  * This class demonstrates the use of the AWT Viewer.
46  */

47 public class ExampleAWTViewer {
48
49     // configure fopFactory as desired
50
private FopFactory fopFactory = FopFactory.newInstance();
51
52     /**
53      * Display an FO file in the AWT Preview.
54      * @param fo the FO file
55      * @throws IOException In case of an I/O problem
56      * @throws FOPException In case of a problem during layout
57      * @throws TransformerException In case of a problem during XML processing
58      */

59     public void viewFO(File JavaDoc fo)
60                 throws IOException JavaDoc, FOPException, TransformerException JavaDoc {
61
62         //Setup FOP
63
Fop fop = fopFactory.newFop(MimeConstants.MIME_FOP_AWT_PREVIEW);
64
65         try {
66
67             //Load XSL-FO file (you can also do an XSL transformation here)
68
TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
69             Transformer JavaDoc transformer = factory.newTransformer();
70             Source JavaDoc src = new StreamSource JavaDoc(fo);
71             Result JavaDoc res = new SAXResult JavaDoc(fop.getDefaultHandler());
72             transformer.transform(src, res);
73
74         } catch (Exception JavaDoc e) {
75             if (e instanceof FOPException) {
76                 throw (FOPException)e;
77             }
78             throw new FOPException(e);
79         }
80     }
81
82     /**
83      * Main method.
84      * @param args the command-line arguments
85      */

86     public static void main(String JavaDoc[] args) {
87         try {
88             System.out.println("FOP ExampleAWTViewer\n");
89             System.out.println("Preparing...");
90
91             //Setup directories
92
File JavaDoc baseDir = new File JavaDoc(".");
93             File JavaDoc outDir = new File JavaDoc(baseDir, "out");
94             outDir.mkdirs();
95
96             //Setup input and output files
97
File JavaDoc fofile = new File JavaDoc(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 JavaDoc e) {
109             System.err.println(ExceptionUtil.printStackTrace(e));
110             System.exit(-1);
111         }
112     }
113 }
114
Popular Tags