001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: ExampleFO2RTF.java 426576 2006-07-28 15:44:37Z jeremias $ */
019:
020: package embedding;
021:
022: // Java
023: import java.io.BufferedOutputStream;
024: import java.io.File;
025: import java.io.FileOutputStream;
026: import java.io.IOException;
027: import java.io.OutputStream;
028:
029: //JAXP
030: import javax.xml.transform.Transformer;
031: import javax.xml.transform.TransformerFactory;
032: import javax.xml.transform.Source;
033: import javax.xml.transform.Result;
034: import javax.xml.transform.stream.StreamSource;
035: import javax.xml.transform.sax.SAXResult;
036:
037: // FOP
038: import org.apache.fop.apps.FOUserAgent;
039: import org.apache.fop.apps.Fop;
040: import org.apache.fop.apps.FOPException;
041: import org.apache.fop.apps.FopFactory;
042: import org.apache.fop.apps.MimeConstants;
043:
044: /**
045: * This class demonstrates the conversion of an FO file to RTF using FOP.
046: * <p>
047: * Please note that this is practically the same as the ExampleFO2PDF example. Only
048: * the MIME parameter to the newFop() method is different!
049: */
050: public class ExampleFO2RTF {
051:
052: // configure fopFactory as desired
053: private FopFactory fopFactory = FopFactory.newInstance();
054:
055: /**
056: * Converts an FO file to a RTF file using FOP
057: * @param fo the FO file
058: * @param rtf the target RTF file
059: * @throws IOException In case of an I/O problem
060: * @throws FOPException In case of a FOP problem
061: */
062: public void convertFO2RTF(File fo, File rtf) throws IOException,
063: FOPException {
064:
065: FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
066: // configure foUserAgent as desired
067:
068: OutputStream out = null;
069:
070: try {
071: // Setup output stream. Note: Using BufferedOutputStream
072: // for performance reasons (helpful with FileOutputStreams).
073: out = new FileOutputStream(rtf);
074: out = new BufferedOutputStream(out);
075:
076: // Construct fop with desired output format
077: Fop fop = fopFactory.newFop(MimeConstants.MIME_RTF,
078: foUserAgent, out);
079:
080: // Setup JAXP using identity transformer
081: TransformerFactory factory = TransformerFactory
082: .newInstance();
083: Transformer transformer = factory.newTransformer(); // identity transformer
084:
085: // Setup input stream
086: Source src = new StreamSource(fo);
087:
088: // Resulting SAX events (the generated FO) must be piped through to FOP
089: Result res = new SAXResult(fop.getDefaultHandler());
090:
091: // Start XSLT transformation and FOP processing
092: transformer.transform(src, res);
093:
094: // Please note: getResults() won't work for RTF and other flow formats (like MIF)
095: // as the layout engine is not involved in the conversion. The page-breaking
096: // is done by the application opening the generated file (like MS Word).
097: //FormattingResults foResults = fop.getResults();
098:
099: } catch (Exception e) {
100: e.printStackTrace(System.err);
101: System.exit(-1);
102: } finally {
103: out.close();
104: }
105: }
106:
107: /**
108: * Main method.
109: * @param args command-line arguments
110: */
111: public static void main(String[] args) {
112: try {
113: System.out.println("FOP ExampleFO2RTF\n");
114: System.out.println("Preparing...");
115:
116: //Setup directories
117: File baseDir = new File(".");
118: File outDir = new File(baseDir, "out");
119: outDir.mkdirs();
120:
121: //Setup input and output files
122: File fofile = new File(baseDir, "xml/fo/helloworld.fo");
123: File rtffile = new File(outDir, "ResultFO2RTF.rtf");
124:
125: System.out.println("Input: XSL-FO (" + fofile + ")");
126: System.out.println("Output: PDF (" + rtffile + ")");
127: System.out.println();
128: System.out.println("Transforming...");
129:
130: ExampleFO2RTF app = new ExampleFO2RTF();
131: app.convertFO2RTF(fofile, rtffile);
132:
133: System.out.println("Success!");
134: } catch (Exception e) {
135: e.printStackTrace(System.err);
136: System.exit(-1);
137: }
138: }
139: }
|