1 17 18 19 20 package org.apache.fop.visual; 21 22 import java.awt.image.BufferedImage ; 23 import java.io.BufferedOutputStream ; 24 import java.io.File ; 25 import java.io.FileOutputStream ; 26 import java.io.OutputStream ; 27 28 import javax.xml.transform.Transformer ; 29 import javax.xml.transform.sax.SAXResult ; 30 import javax.xml.transform.stream.StreamSource ; 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 54 public class BitmapProducerJava2D extends AbstractBitmapProducer implements Configurable { 55 56 private FopFactory fopFactory = FopFactory.newInstance(); 58 59 private boolean deleteTempFiles; 60 61 62 public void configure(Configuration cfg) throws ConfigurationException { 63 this.deleteTempFiles = cfg.getChild("delete-temp-files").getValueAsBoolean(true); 64 } 65 66 67 public BufferedImage produce(File 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 outputFile = new File (context.getTargetDir(), src.getName() + ".java2d.png"); 74 OutputStream out = new FileOutputStream (outputFile); 75 out = new BufferedOutputStream (out); 76 try { 77 Fop fop = fopFactory.newFop(MimeConstants.MIME_PNG, userAgent, out); 78 SAXResult res = new SAXResult (fop.getDefaultHandler()); 79 80 Transformer transformer = getTransformer(context); 81 transformer.transform(new StreamSource (src), res); 82 } finally { 83 IOUtils.closeQuietly(out); 84 } 85 86 BufferedImage 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 e) { 95 e.printStackTrace(); 96 log.error(e); 97 return null; 98 } 99 } 100 101 } 102 | Popular Tags |