KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > embedding > ExampleFO2OldStylePrint


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: ExampleFO2OldStylePrint.java 441942 2006-09-10 11:38:03Z jeremias $ */
19  
20 package embedding;
21
22 // Java
23
import java.awt.print.PrinterJob JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26
27 //JAXP
28
import javax.xml.transform.Transformer JavaDoc;
29 import javax.xml.transform.TransformerFactory 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
36 // FOP
37
import org.apache.fop.apps.FOUserAgent;
38 import org.apache.fop.apps.Fop;
39 import org.apache.fop.apps.FOPException;
40 import org.apache.fop.apps.FopFactory;
41 import org.apache.fop.render.print.PrintRenderer;
42
43 /**
44  * This class demonstrates printing an FO file to a PrinterJob instance.
45  */

46 public class ExampleFO2OldStylePrint {
47
48     // configure fopFactory as desired
49
private FopFactory fopFactory = FopFactory.newInstance();
50     
51     /**
52      * Prints an FO file using an old-style PrinterJob.
53      * @param fo the FO file
54      * @throws IOException In case of an I/O problem
55      * @throws FOPException In case of a FOP problem
56      */

57     public void printFO(File JavaDoc fo) throws IOException JavaDoc, FOPException {
58         
59         //Set up PrinterJob instance
60
PrinterJob JavaDoc printerJob = PrinterJob.getPrinterJob();
61         printerJob.setJobName("FOP Printing Example");
62
63         try {
64             //Set up a custom user agent so we can supply our own renderer instance
65
FOUserAgent userAgent = fopFactory.newFOUserAgent();
66
67             //Set up our own PrintRenderer instance so we can supply a special PrinterJob instance.
68
PrintRenderer renderer = new PrintRenderer(printerJob);
69             renderer.setUserAgent(userAgent);
70             
71             userAgent.setRendererOverride(renderer);
72             
73             // Construct fop with desired output format (here, it is set through the user agent)
74
Fop fop = fopFactory.newFop(userAgent);
75
76             // Setup JAXP using identity transformer
77
TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
78             Transformer JavaDoc transformer = factory.newTransformer(); // identity transformer
79

80             // Setup input stream
81
Source JavaDoc src = new StreamSource JavaDoc(fo);
82
83             // Resulting SAX events (the generated FO) must be piped through to FOP
84
Result JavaDoc res = new SAXResult JavaDoc(fop.getDefaultHandler());
85             
86             // Start XSLT transformation and FOP processing
87
transformer.transform(src, res);
88
89         } catch (Exception JavaDoc e) {
90             e.printStackTrace(System.err);
91             System.exit(-1);
92         }
93     }
94
95
96     /**
97      * Main method.
98      * @param args command-line arguments
99      */

100     public static void main(String JavaDoc[] args) {
101         try {
102             System.out.println("FOP ExampleFO2OldStylePrint\n");
103             System.out.println("Preparing...");
104             
105             //Setup directories
106
File JavaDoc baseDir = new File JavaDoc(".");
107             File JavaDoc outDir = new File JavaDoc(baseDir, "out");
108             outDir.mkdirs();
109
110             //Setup input and output files
111
File JavaDoc fofile = new File JavaDoc(baseDir, "xml/fo/helloworld.fo");
112
113             System.out.println("Input: XSL-FO (" + fofile + ")");
114             System.out.println("Output: old-style printing using PrinterJob");
115             System.out.println();
116             System.out.println("Transforming...");
117             
118             ExampleFO2OldStylePrint app = new ExampleFO2OldStylePrint();
119             app.printFO(fofile);
120             
121             System.out.println("Success!");
122         } catch (Exception JavaDoc e) {
123             e.printStackTrace(System.err);
124             System.exit(-1);
125         }
126     }
127 }
128
Popular Tags