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: ExampleFO2PDFUsingSAXParser.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.parsers.SAXParserFactory;
031: import javax.xml.parsers.FactoryConfigurationError;
032: import javax.xml.parsers.SAXParser;
033: import javax.xml.parsers.ParserConfigurationException;
034:
035: //SAX
036: import org.xml.sax.helpers.DefaultHandler;
037: import org.xml.sax.SAXException;
038:
039: // FOP
040: import org.apache.fop.apps.FOUserAgent;
041: import org.apache.fop.apps.Fop;
042: import org.apache.fop.apps.FopFactory;
043: import org.apache.fop.apps.MimeConstants;
044:
045: /**
046: * This class demonstrates the conversion of an FO file to PDF using FOP.
047: * It uses a SAXParser with FOP as the DefaultHandler
048: */
049: public class ExampleFO2PDFUsingSAXParser {
050:
051: // configure fopFactory as desired
052: private FopFactory fopFactory = FopFactory.newInstance();
053:
054: /**
055: * Converts an FO file to a PDF file using FOP
056: * @param fo the FO file
057: * @param pdf the target PDF file
058: * @throws FactoryConfigurationError In case of a problem with the JAXP factory configuration
059: * @throws ParserConfigurationException In case of a problem with the parser configuration
060: * @throws SAXException In case of a problem during XML processing
061: * @throws IOException In case of an I/O problem
062: */
063: public void convertFO2PDF(File fo, File pdf)
064: throws FactoryConfigurationError,
065: ParserConfigurationException, SAXException, IOException {
066:
067: FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
068: // configure foUserAgent as desired
069:
070: OutputStream out = null;
071:
072: try {
073: // Setup output stream. Note: Using BufferedOutputStream
074: // for performance reasons (helpful with FileOutputStreams).
075: out = new FileOutputStream(pdf);
076: out = new BufferedOutputStream(out);
077:
078: // Construct fop and setup output format
079: Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF,
080: foUserAgent, out);
081:
082: // Setup SAX parser
083: // throws FactoryConfigurationError
084: SAXParserFactory factory = SAXParserFactory.newInstance();
085: factory.setNamespaceAware(true);
086: // throws ParserConfigurationException
087: SAXParser parser = factory.newSAXParser();
088:
089: // Obtain FOP's DefaultHandler
090: // throws FOPException
091: DefaultHandler dh = fop.getDefaultHandler();
092:
093: // Start parsing and FOP processing
094: // throws SAXException, IOException
095: parser.parse(fo, dh);
096:
097: } finally {
098: out.close();
099: }
100: }
101:
102: /**
103: * Main method.
104: * @param args command-line arguments
105: */
106: public static void main(String[] args) {
107: try {
108: System.out.println("FOP ExampleFO2PDFUsingSAXParser\n");
109: System.out.println("Preparing...");
110:
111: //Setup directories
112: File baseDir = new File(".");
113: File outDir = new File(baseDir, "out");
114: outDir.mkdirs();
115:
116: //Setup input and output files
117: File fofile = new File(baseDir, "xml/fo/helloworld.fo");
118: File pdffile = new File(outDir,
119: "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 e) {
131: e.printStackTrace(System.err);
132: System.exit(-1);
133: }
134: }
135: }
|