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: ExampleAWTViewer.java 426576 2006-07-28 15:44:37Z jeremias $ */
019:
020: package embedding;
021:
022: //Java
023: import java.io.File;
024: import java.io.IOException;
025:
026: //JAXP
027: import javax.xml.transform.Transformer;
028: import javax.xml.transform.TransformerFactory;
029: import javax.xml.transform.TransformerException;
030: import javax.xml.transform.Source;
031: import javax.xml.transform.Result;
032: import javax.xml.transform.stream.StreamSource;
033: import javax.xml.transform.sax.SAXResult;
034:
035: //Avalon
036: import org.apache.avalon.framework.ExceptionUtil;
037:
038: //FOP
039: import org.apache.fop.apps.FOPException;
040: import org.apache.fop.apps.Fop;
041: import org.apache.fop.apps.FopFactory;
042: import org.apache.fop.apps.MimeConstants;
043:
044: /**
045: * This class demonstrates the use of the AWT Viewer.
046: */
047: public class ExampleAWTViewer {
048:
049: // configure fopFactory as desired
050: private FopFactory fopFactory = FopFactory.newInstance();
051:
052: /**
053: * Display an FO file in the AWT Preview.
054: * @param fo the FO file
055: * @throws IOException In case of an I/O problem
056: * @throws FOPException In case of a problem during layout
057: * @throws TransformerException In case of a problem during XML processing
058: */
059: public void viewFO(File fo) throws IOException, FOPException,
060: TransformerException {
061:
062: //Setup FOP
063: Fop fop = fopFactory.newFop(MimeConstants.MIME_FOP_AWT_PREVIEW);
064:
065: try {
066:
067: //Load XSL-FO file (you can also do an XSL transformation here)
068: TransformerFactory factory = TransformerFactory
069: .newInstance();
070: Transformer transformer = factory.newTransformer();
071: Source src = new StreamSource(fo);
072: Result res = new SAXResult(fop.getDefaultHandler());
073: transformer.transform(src, res);
074:
075: } catch (Exception e) {
076: if (e instanceof FOPException) {
077: throw (FOPException) e;
078: }
079: throw new FOPException(e);
080: }
081: }
082:
083: /**
084: * Main method.
085: * @param args the command-line arguments
086: */
087: public static void main(String[] args) {
088: try {
089: System.out.println("FOP ExampleAWTViewer\n");
090: System.out.println("Preparing...");
091:
092: //Setup directories
093: File baseDir = new File(".");
094: File outDir = new File(baseDir, "out");
095: outDir.mkdirs();
096:
097: //Setup input and output files
098: File fofile = new File(baseDir, "xml/fo/helloworld.fo");
099:
100: System.out.println("Input: XSL-FO (" + fofile + ")");
101: System.out.println("Output: AWT Viewer");
102: System.out.println();
103: System.out.println("Starting AWT Viewer...");
104:
105: ExampleAWTViewer app = new ExampleAWTViewer();
106: app.viewFO(fofile);
107:
108: System.out.println("Success!");
109: } catch (Exception e) {
110: System.err.println(ExceptionUtil.printStackTrace(e));
111: System.exit(-1);
112: }
113: }
114: }
|