KickJava   Java API By Example, From Geeks To Geeks.

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


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: BitmapProducerJava2D.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.OutputStream JavaDoc;
27
28 import javax.xml.transform.Transformer JavaDoc;
29 import javax.xml.transform.sax.SAXResult JavaDoc;
30 import javax.xml.transform.stream.StreamSource JavaDoc;
31
32 import org.apache.avalon.framework.configuration.Configurable;
33 import org.apache.avalon.framework.configuration.Configuration;
34 import org.apache.avalon.framework.configuration.ConfigurationException;
35 import org.apache.commons.io.IOUtils;
36 import org.apache.fop.apps.FOUserAgent;
37 import org.apache.fop.apps.Fop;
38 import org.apache.fop.apps.FopFactory;
39 import org.apache.fop.apps.MimeConstants;
40
41 /**
42  * BitmapProducer implementation that uses the Java2DRenderer to create bitmaps.
43  * <p>
44  * Here's what the configuration element looks like for the class:
45  * <p>
46  * <pre>
47  * <producer classname="org.apache.fop.visual.BitmapProducerJava2D">
48  * <delete-temp-files>false</delete-temp-files>
49  * </producer>
50  * </pre>
51  * <p>
52  * The "delete-temp-files" element is optional and defaults to true.
53  */

54 public class BitmapProducerJava2D extends AbstractBitmapProducer implements Configurable {
55
56     // configure fopFactory as desired
57
private FopFactory fopFactory = FopFactory.newInstance();
58     
59     private boolean deleteTempFiles;
60
61     /** @see org.apache.avalon.framework.configuration.Configurable */
62     public void configure(Configuration cfg) throws ConfigurationException {
63         this.deleteTempFiles = cfg.getChild("delete-temp-files").getValueAsBoolean(true);
64     }
65
66     /** @see org.apache.fop.visual.BitmapProducer */
67     public BufferedImage JavaDoc produce(File JavaDoc src, ProducerContext context) {
68         try {
69             FOUserAgent userAgent = fopFactory.newFOUserAgent();
70             userAgent.setTargetResolution(context.getTargetResolution());
71             userAgent.setBaseURL(src.getParentFile().toURL().toString());
72             
73             File JavaDoc outputFile = new File JavaDoc(context.getTargetDir(), src.getName() + ".java2d.png");
74             OutputStream JavaDoc out = new FileOutputStream JavaDoc(outputFile);
75             out = new BufferedOutputStream JavaDoc(out);
76             try {
77                 Fop fop = fopFactory.newFop(MimeConstants.MIME_PNG, userAgent, out);
78                 SAXResult JavaDoc res = new SAXResult JavaDoc(fop.getDefaultHandler());
79                 
80                 Transformer JavaDoc transformer = getTransformer(context);
81                 transformer.transform(new StreamSource JavaDoc(src), res);
82             } finally {
83                 IOUtils.closeQuietly(out);
84             }
85             
86             BufferedImage JavaDoc img = BitmapComparator.getImage(outputFile);
87             if (deleteTempFiles) {
88                 if (!outputFile.delete()) {
89                     log.warn("Cannot delete " + outputFile);
90                     outputFile.deleteOnExit();
91                 }
92             }
93             return img;
94         } catch (Exception JavaDoc e) {
95             e.printStackTrace();
96             log.error(e);
97             return null;
98         }
99     }
100
101 }
102
Popular Tags