KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > embedding > ExampleObj2PDF


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: ExampleObj2PDF.java 426576 2006-07-28 15:44:37Z jeremias $ */
19  
20 package embedding;
21
22 // Java
23
import java.io.File JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26
27 // JAXP
28
import javax.xml.transform.Transformer JavaDoc;
29 import javax.xml.transform.TransformerFactory JavaDoc;
30 import javax.xml.transform.TransformerException JavaDoc;
31 import javax.xml.transform.Source JavaDoc;
32 import javax.xml.transform.Result JavaDoc;
33 import javax.xml.transform.stream.StreamSource JavaDoc;
34 import javax.xml.transform.sax.SAXResult JavaDoc;
35
36 // FOP
37
import org.apache.fop.apps.FOUserAgent;
38 import org.apache.fop.apps.Fop;
39 import org.apache.fop.apps.FOPException;
40 import org.apache.fop.apps.FopFactory;
41 import org.apache.fop.apps.MimeConstants;
42
43 import embedding.model.ProjectTeam;
44
45 /**
46  * This class demonstrates the conversion of an arbitrary object file to a
47  * PDF using JAXP (XSLT) and FOP (XSL:FO).
48  */

49 public class ExampleObj2PDF {
50
51     // configure fopFactory as desired
52
private FopFactory fopFactory = FopFactory.newInstance();
53
54     /**
55      * Converts a ProjectTeam object to a PDF file.
56      * @param team the ProjectTeam object
57      * @param xslt the stylesheet file
58      * @param pdf the target PDF file
59      * @throws IOException In case of an I/O problem
60      * @throws FOPException In case of a FOP problem
61      * @throws TransformerException In case of a XSL transformation problem
62      */

63     public void convertProjectTeam2PDF(ProjectTeam team, File JavaDoc xslt, File JavaDoc pdf)
64                 throws IOException JavaDoc, FOPException, TransformerException JavaDoc {
65                     
66         FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
67         // configure foUserAgent as desired
68

69         // Setup output
70
OutputStream JavaDoc out = new java.io.FileOutputStream JavaDoc(pdf);
71         out = new java.io.BufferedOutputStream JavaDoc(out);
72         try {
73             // Construct fop with desired output format
74
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
75
76             // Setup XSLT
77
TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
78             Transformer JavaDoc transformer = factory.newTransformer(new StreamSource JavaDoc(xslt));
79         
80             // Setup input for XSLT transformation
81
Source JavaDoc src = team.getSourceForProjectTeam();
82         
83             // Resulting SAX events (the generated FO) must be piped through to FOP
84
Result JavaDoc res = new SAXResult JavaDoc(fop.getDefaultHandler());
85
86             // Start XSLT transformation and FOP processing
87
transformer.transform(src, res);
88         } finally {
89             out.close();
90         }
91     }
92
93
94     /**
95      * Main method.
96      * @param args command-line arguments
97      */

98     public static void main(String JavaDoc[] args) {
99         try {
100             System.out.println("FOP ExampleObj2PDF\n");
101             System.out.println("Preparing...");
102             
103             // Setup directories
104
File JavaDoc baseDir = new File JavaDoc(".");
105             File JavaDoc outDir = new File JavaDoc(baseDir, "out");
106             outDir.mkdirs();
107
108             // Setup input and output
109
File JavaDoc xsltfile = new File JavaDoc(baseDir, "xml/xslt/projectteam2fo.xsl");
110             File JavaDoc pdffile = new File JavaDoc(outDir, "ResultObj2PDF.pdf");
111
112             System.out.println("Input: a ProjectTeam object");
113             System.out.println("Stylesheet: " + xsltfile);
114             System.out.println("Output: PDF (" + pdffile + ")");
115             System.out.println();
116             System.out.println("Transforming...");
117
118             ExampleObj2PDF app = new ExampleObj2PDF();
119             app.convertProjectTeam2PDF(ExampleObj2XML.createSampleProjectTeam(), xsltfile, pdffile);
120             
121             System.out.println("Success!");
122         } catch (Exception JavaDoc e) {
123             e.printStackTrace(System.err);
124             System.exit(-1);
125         }
126     }
127 }
128
Popular Tags