/[Apache-SVN]/xmlgraphics/fop/trunk/examples/embedding/java/embedding/ExampleObj2PDF.java
ViewVC logotype

Contents of /xmlgraphics/fop/trunk/examples/embedding/java/embedding/ExampleObj2PDF.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 679326 - (show annotations)
Thu Jul 24 09:35:34 2008 UTC (16 months ago) by vhennebert
File size: 4441 byte(s)
Fed up with all those trailing whitespaces. Let's remove them all (once... and for all?)
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$ */
19
20 package embedding;
21
22 // Java
23 import java.io.File;
24 import java.io.OutputStream;
25 import java.io.IOException;
26
27 // JAXP
28 import javax.xml.transform.Transformer;
29 import javax.xml.transform.TransformerFactory;
30 import javax.xml.transform.TransformerException;
31 import javax.xml.transform.Source;
32 import javax.xml.transform.Result;
33 import javax.xml.transform.stream.StreamSource;
34 import javax.xml.transform.sax.SAXResult;
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 xslt, File pdf)
64 throws IOException, FOPException, TransformerException {
65
66 FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
67 // configure foUserAgent as desired
68
69 // Setup output
70 OutputStream out = new java.io.FileOutputStream(pdf);
71 out = new java.io.BufferedOutputStream(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 factory = TransformerFactory.newInstance();
78 Transformer transformer = factory.newTransformer(new StreamSource(xslt));
79
80 // Setup input for XSLT transformation
81 Source src = team.getSourceForProjectTeam();
82
83 // Resulting SAX events (the generated FO) must be piped through to FOP
84 Result res = new SAXResult(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[] args) {
99 try {
100 System.out.println("FOP ExampleObj2PDF\n");
101 System.out.println("Preparing...");
102
103 // Setup directories
104 File baseDir = new File(".");
105 File outDir = new File(baseDir, "out");
106 outDir.mkdirs();
107
108 // Setup input and output
109 File xsltfile = new File(baseDir, "xml/xslt/projectteam2fo.xsl");
110 File pdffile = new File(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 e) {
123 e.printStackTrace(System.err);
124 System.exit(-1);
125 }
126 }
127 }

Properties

Name Value
svn:eol-style native
svn:keywords Id

apache@apache.org
ViewVC Help
Powered by ViewVC 1.1.2