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: ExampleFO2OldStylePrint.java 441942 2006-09-10 11:38:03Z jeremias $ */
019:
020: package embedding;
021:
022: // Java
023: import java.awt.print.PrinterJob;
024: import java.io.File;
025: import java.io.IOException;
026:
027: //JAXP
028: import javax.xml.transform.Transformer;
029: import javax.xml.transform.TransformerFactory;
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: // FOP
036: import org.apache.fop.apps.FOUserAgent;
037: import org.apache.fop.apps.Fop;
038: import org.apache.fop.apps.FOPException;
039: import org.apache.fop.apps.FopFactory;
040: import org.apache.fop.render.print.PrintRenderer;
041:
042: /**
043: * This class demonstrates printing an FO file to a PrinterJob instance.
044: */
045: public class ExampleFO2OldStylePrint {
046:
047: // configure fopFactory as desired
048: private FopFactory fopFactory = FopFactory.newInstance();
049:
050: /**
051: * Prints an FO file using an old-style PrinterJob.
052: * @param fo the FO file
053: * @throws IOException In case of an I/O problem
054: * @throws FOPException In case of a FOP problem
055: */
056: public void printFO(File fo) throws IOException, FOPException {
057:
058: //Set up PrinterJob instance
059: PrinterJob printerJob = PrinterJob.getPrinterJob();
060: printerJob.setJobName("FOP Printing Example");
061:
062: try {
063: //Set up a custom user agent so we can supply our own renderer instance
064: FOUserAgent userAgent = fopFactory.newFOUserAgent();
065:
066: //Set up our own PrintRenderer instance so we can supply a special PrinterJob instance.
067: PrintRenderer renderer = new PrintRenderer(printerJob);
068: renderer.setUserAgent(userAgent);
069:
070: userAgent.setRendererOverride(renderer);
071:
072: // Construct fop with desired output format (here, it is set through the user agent)
073: Fop fop = fopFactory.newFop(userAgent);
074:
075: // Setup JAXP using identity transformer
076: TransformerFactory factory = TransformerFactory
077: .newInstance();
078: Transformer transformer = factory.newTransformer(); // identity transformer
079:
080: // Setup input stream
081: Source src = new StreamSource(fo);
082:
083: // Resulting SAX events (the generated FO) must be piped through to FOP
084: Result res = new SAXResult(fop.getDefaultHandler());
085:
086: // Start XSLT transformation and FOP processing
087: transformer.transform(src, res);
088:
089: } catch (Exception e) {
090: e.printStackTrace(System.err);
091: System.exit(-1);
092: }
093: }
094:
095: /**
096: * Main method.
097: * @param args command-line arguments
098: */
099: public static void main(String[] args) {
100: try {
101: System.out.println("FOP ExampleFO2OldStylePrint\n");
102: System.out.println("Preparing...");
103:
104: //Setup directories
105: File baseDir = new File(".");
106: File outDir = new File(baseDir, "out");
107: outDir.mkdirs();
108:
109: //Setup input and output files
110: File fofile = new File(baseDir, "xml/fo/helloworld.fo");
111:
112: System.out.println("Input: XSL-FO (" + fofile + ")");
113: System.out
114: .println("Output: old-style printing using PrinterJob");
115: System.out.println();
116: System.out.println("Transforming...");
117:
118: ExampleFO2OldStylePrint app = new ExampleFO2OldStylePrint();
119: app.printFO(fofile);
120:
121: System.out.println("Success!");
122: } catch (Exception e) {
123: e.printStackTrace(System.err);
124: System.exit(-1);
125: }
126: }
127: }
|