KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > embedding > ExampleFO2RTF


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: ExampleFO2RTF.java 426576 2006-07-28 15:44:37Z jeremias $ */
19  
20 package embedding;
21
22 // Java
23
import java.io.BufferedOutputStream JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.OutputStream JavaDoc;
28
29 //JAXP
30
import javax.xml.transform.Transformer JavaDoc;
31 import javax.xml.transform.TransformerFactory JavaDoc;
32 import javax.xml.transform.Source JavaDoc;
33 import javax.xml.transform.Result JavaDoc;
34 import javax.xml.transform.stream.StreamSource JavaDoc;
35 import javax.xml.transform.sax.SAXResult JavaDoc;
36
37 // FOP
38
import org.apache.fop.apps.FOUserAgent;
39 import org.apache.fop.apps.Fop;
40 import org.apache.fop.apps.FOPException;
41 import org.apache.fop.apps.FopFactory;
42 import org.apache.fop.apps.MimeConstants;
43
44 /**
45  * This class demonstrates the conversion of an FO file to RTF using FOP.
46  * <p>
47  * Please note that this is practically the same as the ExampleFO2PDF example. Only
48  * the MIME parameter to the newFop() method is different!
49  */

50 public class ExampleFO2RTF {
51
52     // configure fopFactory as desired
53
private FopFactory fopFactory = FopFactory.newInstance();
54
55     /**
56      * Converts an FO file to a RTF file using FOP
57      * @param fo the FO file
58      * @param rtf the target RTF file
59      * @throws IOException In case of an I/O problem
60      * @throws FOPException In case of a FOP problem
61      */

62     public void convertFO2RTF(File JavaDoc fo, File JavaDoc rtf) throws IOException JavaDoc, FOPException {
63         
64         FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
65         // configure foUserAgent as desired
66

67         OutputStream JavaDoc out = null;
68         
69         try {
70             // Setup output stream. Note: Using BufferedOutputStream
71
// for performance reasons (helpful with FileOutputStreams).
72
out = new FileOutputStream JavaDoc(rtf);
73             out = new BufferedOutputStream JavaDoc(out);
74
75             // Construct fop with desired output format
76
Fop fop = fopFactory.newFop(MimeConstants.MIME_RTF, foUserAgent, out);
77     
78             // Setup JAXP using identity transformer
79
TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
80             Transformer JavaDoc transformer = factory.newTransformer(); // identity transformer
81

82             // Setup input stream
83
Source JavaDoc src = new StreamSource JavaDoc(fo);
84
85             // Resulting SAX events (the generated FO) must be piped through to FOP
86
Result JavaDoc res = new SAXResult JavaDoc(fop.getDefaultHandler());
87             
88             // Start XSLT transformation and FOP processing
89
transformer.transform(src, res);
90             
91             // Please note: getResults() won't work for RTF and other flow formats (like MIF)
92
// as the layout engine is not involved in the conversion. The page-breaking
93
// is done by the application opening the generated file (like MS Word).
94
//FormattingResults foResults = fop.getResults();
95

96         } catch (Exception JavaDoc e) {
97             e.printStackTrace(System.err);
98             System.exit(-1);
99         } finally {
100             out.close();
101         }
102     }
103
104
105     /**
106      * Main method.
107      * @param args command-line arguments
108      */

109     public static void main(String JavaDoc[] args) {
110         try {
111             System.out.println("FOP ExampleFO2RTF\n");
112             System.out.println("Preparing...");
113             
114             //Setup directories
115
File JavaDoc baseDir = new File JavaDoc(".");
116             File JavaDoc outDir = new File JavaDoc(baseDir, "out");
117             outDir.mkdirs();
118
119             //Setup input and output files
120
File JavaDoc fofile = new File JavaDoc(baseDir, "xml/fo/helloworld.fo");
121             File JavaDoc rtffile = new File JavaDoc(outDir, "ResultFO2RTF.rtf");
122
123             System.out.println("Input: XSL-FO (" + fofile + ")");
124             System.out.println("Output: PDF (" + rtffile + ")");
125             System.out.println();
126             System.out.println("Transforming...");
127             
128             ExampleFO2RTF app = new ExampleFO2RTF();
129             app.convertFO2RTF(fofile, rtffile);
130             
131             System.out.println("Success!");
132         } catch (Exception JavaDoc e) {
133             e.printStackTrace(System.err);
134             System.exit(-1);
135         }
136     }
137 }
138
Popular Tags