KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > embedding > intermediate > ExampleStamp


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: ExampleDOM2PDF.java 332791 2005-11-12 15:58:07Z jeremias $ */
19  
20 package embedding.intermediate;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25
26 import javax.xml.transform.Source JavaDoc;
27 import javax.xml.transform.Transformer JavaDoc;
28 import javax.xml.transform.TransformerException JavaDoc;
29 import javax.xml.transform.TransformerFactory JavaDoc;
30 import javax.xml.transform.sax.SAXResult JavaDoc;
31 import javax.xml.transform.stream.StreamSource JavaDoc;
32
33 import org.apache.fop.apps.FOUserAgent;
34 import org.apache.fop.apps.FopFactory;
35 import org.apache.fop.apps.MimeConstants;
36 import org.apache.fop.area.AreaTreeModel;
37 import org.apache.fop.area.AreaTreeParser;
38 import org.apache.fop.area.RenderPagesModel;
39 import org.apache.fop.fonts.FontInfo;
40 import org.xml.sax.SAXException JavaDoc;
41
42 import embedding.ExampleObj2XML;
43 import embedding.model.ProjectTeam;
44
45 /**
46  * Example for the intermediate format that demonstrates the stamping of a document with some
47  * kind of watermark. The resulting document is then rendered to a PDF file.
48  */

49 public class ExampleStamp {
50
51     // configure fopFactory as desired
52
private FopFactory fopFactory = FopFactory.newInstance();
53     
54     /**
55      * Stamps an intermediate file and renders it to a PDF file.
56      * @param atfile the intermediate file (area tree XML)
57      * @param stampSheet the stylesheet that does the stamping
58      * @param pdffile the target PDF file
59      * @throws IOException In case of an I/O problem
60      * @throws TransformerException In case of a XSL transformation problem
61      * @throws SAXException In case of an XML-related problem
62      */

63     public void stampToPDF(File JavaDoc atfile, File JavaDoc stampSheet, File JavaDoc pdffile)
64             throws IOException JavaDoc, TransformerException JavaDoc, SAXException JavaDoc {
65         // Setup output
66
OutputStream JavaDoc out = new java.io.FileOutputStream JavaDoc(pdffile);
67         out = new java.io.BufferedOutputStream JavaDoc(out);
68         try {
69             //Setup fonts and user agent
70
FontInfo fontInfo = new FontInfo();
71             FOUserAgent userAgent = fopFactory.newFOUserAgent();
72
73             //Construct the AreaTreeModel that will received the individual pages
74
AreaTreeModel treeModel = new RenderPagesModel(userAgent,
75                     MimeConstants.MIME_PDF, fontInfo, out);
76             
77             //Iterate over all intermediate files
78
AreaTreeParser parser = new AreaTreeParser();
79             Source JavaDoc src = new StreamSource JavaDoc(atfile);
80             Source JavaDoc xslt = new StreamSource JavaDoc(stampSheet);
81             
82             //Setup Transformer for XSLT processing
83
TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
84             Transformer JavaDoc transformer = tFactory.newTransformer(xslt);
85             
86             //Send XSLT result to AreaTreeParser
87
SAXResult JavaDoc res = new SAXResult JavaDoc(parser.getContentHandler(treeModel, userAgent));
88             
89             //Start XSLT transformation and area tree parsing
90
transformer.transform(src, res);
91             
92             //Signal the end of the processing. The renderer can finalize the target document.
93
treeModel.endDocument();
94         } finally {
95             out.close();
96         }
97     }
98     
99     /**
100      * Main method.
101      * @param args command-line arguments
102      */

103     public static void main(String JavaDoc[] args) {
104         try {
105             System.out.println("FOP ExampleConcat\n");
106             
107             //Setup directories
108
File JavaDoc baseDir = new File JavaDoc(".");
109             File JavaDoc outDir = new File JavaDoc(baseDir, "out");
110             outDir.mkdirs();
111             
112             //Setup output file
113
File JavaDoc xsltfile = new File JavaDoc(baseDir, "xml/xslt/projectteam2fo.xsl");
114             File JavaDoc atfile = new File JavaDoc(outDir, "team.at.xml");
115             File JavaDoc stampxsltfile = new File JavaDoc(baseDir, "xml/xslt/atstamp.xsl");
116             File JavaDoc pdffile = new File JavaDoc(outDir, "ResultStamped.pdf");
117             System.out.println("Intermediate file : " + atfile.getCanonicalPath());
118             System.out.println("Stamp XSLT: " + stampxsltfile.getCanonicalPath());
119             System.out.println("PDF Output File: " + pdffile.getCanonicalPath());
120             System.out.println();
121
122             ProjectTeam team1 = ExampleObj2XML.createSampleProjectTeam();
123             
124             //Create intermediate file
125
ExampleConcat concatapp = new ExampleConcat();
126             concatapp.convertToIntermediate(
127                     team1.getSourceForProjectTeam(),
128                     new StreamSource JavaDoc(xsltfile), atfile);
129             
130             //Stamp document and produce a PDF from the intermediate format
131
ExampleStamp app = new ExampleStamp();
132             app.stampToPDF(atfile, stampxsltfile, pdffile);
133             
134             System.out.println("Success!");
135             
136         } catch (Exception JavaDoc e) {
137             e.printStackTrace(System.err);
138             System.exit(-1);
139         }
140     }
141
142 }
143
Popular Tags