KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > embedding > ExampleFO2PDFUsingSAXParser


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: ExampleFO2PDFUsingSAXParser.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.parsers.SAXParserFactory JavaDoc;
31 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
32 import javax.xml.parsers.SAXParser JavaDoc;
33 import javax.xml.parsers.ParserConfigurationException JavaDoc;
34
35 //SAX
36
import org.xml.sax.helpers.DefaultHandler JavaDoc;
37 import org.xml.sax.SAXException JavaDoc;
38
39 // FOP
40
import org.apache.fop.apps.FOUserAgent;
41 import org.apache.fop.apps.Fop;
42 import org.apache.fop.apps.FopFactory;
43 import org.apache.fop.apps.MimeConstants;
44
45 /**
46  * This class demonstrates the conversion of an FO file to PDF using FOP.
47  * It uses a SAXParser with FOP as the DefaultHandler
48  */

49 public class ExampleFO2PDFUsingSAXParser {
50
51     // configure fopFactory as desired
52
private FopFactory fopFactory = FopFactory.newInstance();
53
54     /**
55      * Converts an FO file to a PDF file using FOP
56      * @param fo the FO file
57      * @param pdf the target PDF file
58      * @throws FactoryConfigurationError In case of a problem with the JAXP factory configuration
59      * @throws ParserConfigurationException In case of a problem with the parser configuration
60      * @throws SAXException In case of a problem during XML processing
61      * @throws IOException In case of an I/O problem
62      */

63     public void convertFO2PDF(File JavaDoc fo, File JavaDoc pdf)
64         throws FactoryConfigurationError JavaDoc,
65                ParserConfigurationException JavaDoc,
66                SAXException JavaDoc, IOException JavaDoc {
67
68         FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
69         // configure foUserAgent as desired
70

71         OutputStream JavaDoc out = null;
72         
73         try {
74             // Setup output stream. Note: Using BufferedOutputStream
75
// for performance reasons (helpful with FileOutputStreams).
76
out = new FileOutputStream JavaDoc(pdf);
77             out = new BufferedOutputStream JavaDoc(out);
78             
79             // Construct fop and setup output format
80
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
81
82             // Setup SAX parser
83
// throws FactoryConfigurationError
84
SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
85             factory.setNamespaceAware(true);
86             // throws ParserConfigurationException
87
SAXParser JavaDoc parser = factory.newSAXParser();
88                 
89             // Obtain FOP's DefaultHandler
90
// throws FOPException
91
DefaultHandler JavaDoc dh = fop.getDefaultHandler();
92
93             // Start parsing and FOP processing
94
// throws SAXException, IOException
95
parser.parse(fo, dh);
96
97         } finally {
98             out.close();
99         }
100     }
101
102
103     /**
104      * Main method.
105      * @param args command-line arguments
106      */

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