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 1356646 2012-07-03 09:46:41Z mehdi $ */
19 
20 package embedding;
21 
22 // Java
23 import java.io.BufferedOutputStream;
24 import java.io.File;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.OutputStream;
28 
29 import javax.xml.transform.Result;
30 import javax.xml.transform.Source;
31 import javax.xml.transform.Transformer;
32 import javax.xml.transform.TransformerFactory;
33 import javax.xml.transform.sax.SAXResult;
34 import javax.xml.transform.stream.StreamSource;
35 
36 import org.apache.fop.apps.FOPException;
37 import org.apache.fop.apps.FOUserAgent;
38 import org.apache.fop.apps.Fop;
39 import org.apache.fop.apps.FopFactory;
40 import org.apache.fop.apps.MimeConstants;
41 
42 /**
43  * This class demonstrates the conversion of an FO file to RTF using FOP.
44  * <p>
45  * Please note that this is practically the same as the ExampleFO2PDF example. Only
46  * the MIME parameter to the newFop() method is different!
47  */
48 public class ExampleFO2RTF {
49 
50     // configure fopFactory as desired
51     private final FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
52 
53     /**
54      * Converts an FO file to a RTF file using FOP
55      * @param fo the FO file
56      * @param rtf the target RTF file
57      * @throws IOException In case of an I/O problem
58      * @throws FOPException In case of a FOP problem
59      */
convertFO2RTF(File fo, File rtf)60     public void convertFO2RTF(File fo, File rtf) throws IOException, FOPException {
61 
62         FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
63         // configure foUserAgent as desired
64 
65         OutputStream out = null;
66 
67         try {
68             // Setup output stream.  Note: Using BufferedOutputStream
69             // for performance reasons (helpful with FileOutputStreams).
70             out = new FileOutputStream(rtf);
71             out = new BufferedOutputStream(out);
72 
73             // Construct fop with desired output format
74             Fop fop = fopFactory.newFop(MimeConstants.MIME_RTF, foUserAgent, out);
75 
76             // Setup JAXP using identity transformer
77             TransformerFactory factory = TransformerFactory.newInstance();
78             Transformer transformer = factory.newTransformer(); // identity transformer
79 
80             // Setup input stream
81             Source src = new StreamSource(fo);
82 
83             // Resulting SAX events (the generated FO) must be piped through to FOP
84             Result res = new SAXResult(fop.getDefaultHandler());
85 
86             // Start XSLT transformation and FOP processing
87             transformer.transform(src, res);
88 
89             // Please note: getResults() won't work for RTF and other flow formats (like MIF)
90             // as the layout engine is not involved in the conversion. The page-breaking
91             // is done by the application opening the generated file (like MS Word).
92             //FormattingResults foResults = fop.getResults();
93 
94         } catch (Exception e) {
95             e.printStackTrace(System.err);
96             System.exit(-1);
97         } finally {
98             out.close();
99         }
100     }
101 
102 
103     /**
104      * Main method.
105      * @param args command-line arguments
106      */
main(String[] args)107     public static void main(String[] args) {
108         try {
109             System.out.println("FOP ExampleFO2RTF\n");
110             System.out.println("Preparing...");
111 
112             //Setup directories
113             File baseDir = new File(".");
114             File outDir = new File(baseDir, "out");
115             outDir.mkdirs();
116 
117             //Setup input and output files
118             File fofile = new File(baseDir, "xml/fo/helloworld.fo");
119             File rtffile = new File(outDir, "ResultFO2RTF.rtf");
120 
121             System.out.println("Input: XSL-FO (" + fofile + ")");
122             System.out.println("Output: PDF (" + rtffile + ")");
123             System.out.println();
124             System.out.println("Transforming...");
125 
126             ExampleFO2RTF app = new ExampleFO2RTF();
127             app.convertFO2RTF(fofile, rtffile);
128 
129             System.out.println("Success!");
130         } catch (Exception e) {
131             e.printStackTrace(System.err);
132             System.exit(-1);
133         }
134     }
135 }
136