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: ExampleObj2PDF.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.OutputStream;
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.TransformerException;
031: import javax.xml.transform.Source;
032: import javax.xml.transform.Result;
033: import javax.xml.transform.stream.StreamSource;
034: import javax.xml.transform.sax.SAXResult;
035:
036: // FOP
037: import org.apache.fop.apps.FOUserAgent;
038: import org.apache.fop.apps.Fop;
039: import org.apache.fop.apps.FOPException;
040: import org.apache.fop.apps.FopFactory;
041: import org.apache.fop.apps.MimeConstants;
042:
043: import embedding.model.ProjectTeam;
044:
045: /**
046: * This class demonstrates the conversion of an arbitrary object file to a
047: * PDF using JAXP (XSLT) and FOP (XSL:FO).
048: */
049: public class ExampleObj2PDF {
050:
051: // configure fopFactory as desired
052: private FopFactory fopFactory = FopFactory.newInstance();
053:
054: /**
055: * Converts a ProjectTeam object to a PDF file.
056: * @param team the ProjectTeam object
057: * @param xslt the stylesheet file
058: * @param pdf the target PDF file
059: * @throws IOException In case of an I/O problem
060: * @throws FOPException In case of a FOP problem
061: * @throws TransformerException In case of a XSL transformation problem
062: */
063: public void convertProjectTeam2PDF(ProjectTeam team, File xslt,
064: File pdf) throws IOException, FOPException,
065: TransformerException {
066:
067: FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
068: // configure foUserAgent as desired
069:
070: // Setup output
071: OutputStream out = new java.io.FileOutputStream(pdf);
072: out = new java.io.BufferedOutputStream(out);
073: try {
074: // Construct fop with desired output format
075: Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF,
076: foUserAgent, out);
077:
078: // Setup XSLT
079: TransformerFactory factory = TransformerFactory
080: .newInstance();
081: Transformer transformer = factory
082: .newTransformer(new StreamSource(xslt));
083:
084: // Setup input for XSLT transformation
085: Source src = team.getSourceForProjectTeam();
086:
087: // Resulting SAX events (the generated FO) must be piped through to FOP
088: Result res = new SAXResult(fop.getDefaultHandler());
089:
090: // Start XSLT transformation and FOP processing
091: transformer.transform(src, res);
092: } finally {
093: out.close();
094: }
095: }
096:
097: /**
098: * Main method.
099: * @param args command-line arguments
100: */
101: public static void main(String[] args) {
102: try {
103: System.out.println("FOP ExampleObj2PDF\n");
104: System.out.println("Preparing...");
105:
106: // Setup directories
107: File baseDir = new File(".");
108: File outDir = new File(baseDir, "out");
109: outDir.mkdirs();
110:
111: // Setup input and output
112: File xsltfile = new File(baseDir,
113: "xml/xslt/projectteam2fo.xsl");
114: File pdffile = new File(outDir, "ResultObj2PDF.pdf");
115:
116: System.out.println("Input: a ProjectTeam object");
117: System.out.println("Stylesheet: " + xsltfile);
118: System.out.println("Output: PDF (" + pdffile + ")");
119: System.out.println();
120: System.out.println("Transforming...");
121:
122: ExampleObj2PDF app = new ExampleObj2PDF();
123: app.convertProjectTeam2PDF(ExampleObj2XML
124: .createSampleProjectTeam(), xsltfile, pdffile);
125:
126: System.out.println("Success!");
127: } catch (Exception e) {
128: e.printStackTrace(System.err);
129: System.exit(-1);
130: }
131: }
132: }
|