KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > visual > AbstractPSPDFBitmapProducer


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: AbstractPSPDFBitmapProducer.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.visual;
21
22 import java.awt.image.BufferedImage JavaDoc;
23 import java.io.BufferedOutputStream JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.text.MessageFormat JavaDoc;
29
30 import javax.xml.transform.Transformer JavaDoc;
31 import javax.xml.transform.sax.SAXResult JavaDoc;
32 import javax.xml.transform.stream.StreamSource JavaDoc;
33
34 import org.apache.avalon.framework.configuration.Configurable;
35 import org.apache.avalon.framework.configuration.Configuration;
36 import org.apache.avalon.framework.configuration.ConfigurationException;
37 import org.apache.commons.io.IOUtils;
38 import org.apache.fop.apps.FOUserAgent;
39 import org.apache.fop.apps.Fop;
40 import org.apache.fop.apps.FopFactory;
41
42 /**
43  * BitmapProducer implementation that uses the PS or PDF renderer and an external converter
44  * to create bitmaps.
45  * <p>
46  * Here's what the configuration element looks like for the class:
47  * <p>
48  * <pre>
49  * <producer classname="<concrete class>">
50  * <converter>mypdf2bmp {0} {1} {2}</converter>
51  * <delete-temp-files>false</delete-temp-files>
52  * </producer>
53  * </pre>
54  * <p>
55  * You will notice the three parameters in curly braces (java.util.MessageFormat style) to the
56  * converter call:
57  * <ul>
58  * <li>0: the input file
59  * <li>1: the output bitmap file
60  * <li>2: the requested resolution in dpi for the generated bitmap
61  * </ul>
62  * <p>
63  * The "delete-temp-files" element is optional and defaults to true.
64  */

65 public abstract class AbstractPSPDFBitmapProducer extends AbstractBitmapProducer
66             implements Configurable {
67
68     // configure fopFactory as desired
69
private FopFactory fopFactory = FopFactory.newInstance();
70     
71     private String JavaDoc converter;
72     private boolean deleteTempFiles;
73     
74     /** @see org.apache.avalon.framework.configuration.Configurable */
75     public void configure(Configuration cfg) throws ConfigurationException {
76         this.converter = cfg.getChild("converter").getValue();
77         this.deleteTempFiles = cfg.getChild("delete-temp-files").getValueAsBoolean(true);
78     }
79
80     /**
81      * Calls an external converter to convert the file generated by FOP to a bitmap.
82      * @param inFile the generated output file to be converted
83      * @param outFile the target filename for the bitmap
84      * @param context context information (required bitmap resolution etc.)
85      * @throws IOException in case the conversion fails
86      */

87     public void convert(File JavaDoc inFile, File JavaDoc outFile, ProducerContext context) throws IOException JavaDoc {
88         outFile.delete();
89         
90         //Build command-line
91
String JavaDoc cmd = MessageFormat.format(converter,
92                 new Object JavaDoc[] {inFile.toString(), outFile.toString(),
93                     Integer.toString(context.getTargetResolution())});
94         ConvertUtils.convert(cmd, null, null, log);
95
96         if (!outFile.exists()) {
97             throw new IOException JavaDoc("The target file has not been generated");
98         }
99     }
100     
101     /**
102      * @return the native extension generated by the output format, ex. "ps" or "pdf".
103      */

104     protected abstract String JavaDoc getTargetExtension();
105     
106     /**
107      * @return the output format for the FOP renderer, i.e. a MIME type.
108      */

109     protected abstract String JavaDoc getTargetFormat();
110     
111     /** @see org.apache.fop.visual.BitmapProducer */
112     public BufferedImage JavaDoc produce(File JavaDoc src, ProducerContext context) {
113         try {
114             FOUserAgent userAgent = fopFactory.newFOUserAgent();
115             userAgent.setTargetResolution(context.getTargetResolution());
116             userAgent.setBaseURL(src.getParentFile().toURL().toString());
117
118             File JavaDoc tempOut = new File JavaDoc(context.getTargetDir(),
119                     src.getName() + "." + getTargetExtension());
120             File JavaDoc tempPNG = new File JavaDoc(context.getTargetDir(),
121                     src.getName() + "." + getTargetExtension() + ".png");
122             try {
123                 OutputStream JavaDoc out = new FileOutputStream JavaDoc(tempOut);
124                 out = new BufferedOutputStream JavaDoc(out);
125                 try {
126                     Fop fop = fopFactory.newFop(getTargetFormat(), userAgent, out);
127                     SAXResult JavaDoc res = new SAXResult JavaDoc(fop.getDefaultHandler());
128                     
129                     Transformer JavaDoc transformer = getTransformer(context);
130                     transformer.transform(new StreamSource JavaDoc(src), res);
131                 } finally {
132                     IOUtils.closeQuietly(out);
133                 }
134                 
135                 convert(tempOut, tempPNG, context);
136                 BufferedImage JavaDoc img = BitmapComparator.getImage(tempPNG);
137                 return img;
138             } finally {
139                 if (deleteTempFiles) {
140                     if (!tempOut.delete()) {
141                         log.warn("Can't delete " + tempOut);
142                         tempOut.deleteOnExit();
143                     }
144                     if (!tempPNG.delete()) {
145                         log.warn("Can't delete " + tempPNG);
146                         tempPNG.deleteOnExit();
147                     }
148                 }
149             }
150         } catch (Exception JavaDoc e) {
151             e.printStackTrace();
152             log.error(e);
153             return null;
154         }
155     }
156
157 }
158
Popular Tags