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.IOException ; 27 import java.io.OutputStream ; 28 import java.text.MessageFormat ; 29 30 import javax.xml.transform.Transformer ; 31 import javax.xml.transform.sax.SAXResult ; 32 import javax.xml.transform.stream.StreamSource ; 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 65 public abstract class AbstractPSPDFBitmapProducer extends AbstractBitmapProducer 66 implements Configurable { 67 68 private FopFactory fopFactory = FopFactory.newInstance(); 70 71 private String converter; 72 private boolean deleteTempFiles; 73 74 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 87 public void convert(File inFile, File outFile, ProducerContext context) throws IOException { 88 outFile.delete(); 89 90 String cmd = MessageFormat.format(converter, 92 new Object [] {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 ("The target file has not been generated"); 98 } 99 } 100 101 104 protected abstract String getTargetExtension(); 105 106 109 protected abstract String getTargetFormat(); 110 111 112 public BufferedImage produce(File 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 tempOut = new File (context.getTargetDir(), 119 src.getName() + "." + getTargetExtension()); 120 File tempPNG = new File (context.getTargetDir(), 121 src.getName() + "." + getTargetExtension() + ".png"); 122 try { 123 OutputStream out = new FileOutputStream (tempOut); 124 out = new BufferedOutputStream (out); 125 try { 126 Fop fop = fopFactory.newFop(getTargetFormat(), userAgent, out); 127 SAXResult res = new SAXResult (fop.getDefaultHandler()); 128 129 Transformer transformer = getTransformer(context); 130 transformer.transform(new StreamSource (src), res); 131 } finally { 132 IOUtils.closeQuietly(out); 133 } 134 135 convert(tempOut, tempPNG, context); 136 BufferedImage 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 e) { 151 e.printStackTrace(); 152 log.error(e); 153 return null; 154 } 155 } 156 157 } 158 | Popular Tags |