1 17 18 19 20 package org.apache.fop.visual; 21 22 import java.awt.image.BufferedImage ; 23 import java.io.File ; 24 import java.io.FileOutputStream ; 25 import java.io.IOException ; 26 import java.io.OutputStream ; 27 import java.util.Collection ; 28 import java.util.Iterator ; 29 30 import javax.xml.transform.TransformerConfigurationException ; 31 import javax.xml.transform.stream.StreamSource ; 32 33 import org.apache.avalon.framework.configuration.Configuration; 34 import org.apache.avalon.framework.configuration.ConfigurationException; 35 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; 36 import org.apache.avalon.framework.container.ContainerUtil; 37 38 import org.apache.batik.ext.awt.image.codec.PNGEncodeParam; 39 import org.apache.batik.ext.awt.image.codec.PNGImageEncoder; 40 41 import org.apache.commons.io.FileUtils; 42 import org.apache.commons.io.IOUtils; 43 import org.apache.commons.io.filefilter.IOFileFilter; 44 import org.apache.commons.io.filefilter.SuffixFileFilter; 45 46 import org.apache.commons.logging.Log; 47 import org.apache.commons.logging.LogFactory; 48 49 import org.apache.fop.layoutengine.LayoutEngineTestSuite; 50 51 import org.xml.sax.SAXException ; 52 53 100 public class BatchDiffer { 101 102 103 protected static Log log = LogFactory.getLog(BatchDiffer.class); 104 105 108 public static void printUsage() { 109 System.out.println("Usage:"); 110 System.out.println("java " + BatchDiffer.class.getName() + " <cfgfile>"); 111 System.out.println(); 112 System.out.println("<cfgfile>: Path to an XML file with the configuration" 113 + " for the batch run."); 114 } 115 116 122 public static void saveAsPNG(BufferedImage bitmap, File outputFile) throws IOException { 123 OutputStream out = new FileOutputStream (outputFile); 124 try { 125 PNGImageEncoder encoder = new PNGImageEncoder( 126 out, 127 PNGEncodeParam.getDefaultEncodeParam(bitmap)); 128 encoder.encode(bitmap); 129 } finally { 130 IOUtils.closeQuietly(out); 131 } 132 } 133 134 141 public void runBatch(File cfgFile) 142 throws ConfigurationException , SAXException , IOException { 143 DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder(); 144 Configuration cfg = cfgBuilder.buildFromFile(cfgFile); 145 runBatch(cfg); 146 } 147 148 153 public void runBatch(Configuration cfg) { 154 try { 155 ProducerContext context = new ProducerContext(); 156 context.setTargetResolution(cfg.getChild("resolution").getValueAsInteger(72)); 157 String xslt = cfg.getChild("stylesheet").getValue(null); 158 if (xslt != null) { 159 try { 160 context.setTemplates(context.getTransformerFactory().newTemplates( 161 new StreamSource (xslt))); 162 } catch (TransformerConfigurationException e) { 163 log.error("Error setting up stylesheet", e); 164 throw new RuntimeException ("Error setting up stylesheet"); 165 } 166 } 167 BitmapProducer[] producers = getProducers(cfg.getChild("producers")); 168 169 File srcDir = new File (cfg.getChild("source-directory").getValue()); 171 if (!srcDir.exists()) { 172 throw new RuntimeException ("source-directory does not exist: " + srcDir); 173 } 174 File targetDir = new File (cfg.getChild("target-directory").getValue()); 175 targetDir.mkdirs(); 176 if (!targetDir.exists()) { 177 throw new RuntimeException ("target-directory is invalid: " + targetDir); 178 } 179 context.setTargetDir(targetDir); 180 181 boolean stopOnException = cfg.getChild("stop-on-exception").getValueAsBoolean(true); 182 boolean createDiffs = cfg.getChild("create-diffs").getValueAsBoolean(true); 183 184 BufferedImage [] bitmaps = new BufferedImage [producers.length]; 186 187 IOFileFilter filter = new SuffixFileFilter(new String [] {".xml", ".fo"}); 188 if (cfg.getChild("filter-disabled").getValueAsBoolean(true)) { 190 filter = LayoutEngineTestSuite.decorateWithDisabledList(filter); 191 } 192 193 int maxfiles = cfg.getChild("max-files").getValueAsInteger(-1); 194 Collection files = FileUtils.listFiles(srcDir, filter, null); 195 Iterator i = files.iterator(); 196 while (i.hasNext()) { 197 try { 198 File f = (File )i.next(); 199 log.info("---=== " + f + " ===---"); 200 for (int j = 0; j < producers.length; j++) { 201 bitmaps[j] = producers[j].produce(f, context); 202 } 203 if (bitmaps[0] == null) { 205 throw new RuntimeException ("First producer didn't return a bitmap." 206 + " Cannot continue."); 207 } 208 BufferedImage combined = BitmapComparator.buildCompareImage(bitmaps); 209 210 File outputFile = new File (targetDir, f.getName() + "._combined.png"); 212 saveAsPNG(combined, outputFile); 213 214 if (createDiffs) { 215 for (int k = 1; k < bitmaps.length; k++) { 216 BufferedImage diff = BitmapComparator.buildDiffImage( 217 bitmaps[0], bitmaps[k]); 218 outputFile = new File (targetDir, f.getName() + "._diff" + k + ".png"); 219 saveAsPNG(diff, outputFile); 220 } 221 } 222 for (int k = 0; k < bitmaps.length; k++) { 224 bitmaps[k] = null; 225 } 226 } catch (RuntimeException e) { 227 System.out.println("Catching RE: " + e.getMessage()); 228 if (stopOnException) { 229 System.out.println("rethrowing..."); 230 throw e; 231 } 232 } 233 maxfiles = (maxfiles < 0 ? maxfiles : maxfiles - 1); 234 if (maxfiles == 0) { 235 break; 236 } 237 } 238 } catch (IOException ioe) { 239 log.error("I/O problem while processing", ioe); 240 throw new RuntimeException ("I/O problem: " + ioe.getMessage()); 241 } catch (ConfigurationException e) { 242 log.error("Error while configuring BatchDiffer", e); 243 throw new RuntimeException ("Error while configuring BatchDiffer: " + e.getMessage()); 244 } 245 } 246 247 private BitmapProducer[] getProducers(Configuration cfg) { 248 Configuration[] children = cfg.getChildren("producer"); 249 BitmapProducer[] producers = new BitmapProducer[children.length]; 250 for (int i = 0; i < children.length; i++) { 251 try { 252 Class clazz = Class.forName(children[i].getAttribute("classname")); 253 producers[i] = (BitmapProducer)clazz.newInstance(); 254 ContainerUtil.configure(producers[i], children[i]); 255 } catch (Exception e) { 256 log.error("Error setting up producers", e); 257 throw new RuntimeException ("Error while setting up producers"); 258 } 259 } 260 return producers; 261 } 262 263 267 public static void main(String [] args) { 268 try { 269 if (args.length == 0) { 270 System.err.println("Configuration file is missing!"); 271 printUsage(); 272 System.exit(-1); 273 } 274 File cfgFile = new File (args[0]); 275 if (!cfgFile.exists()) { 276 System.err.println("Configuration file cannot be found: " + args[0]); 277 printUsage(); 278 System.exit(-1); 279 } 280 281 Thread.currentThread().setPriority(Thread.MIN_PRIORITY); 282 BatchDiffer differ = new BatchDiffer(); 283 differ.runBatch(cfgFile); 284 285 System.out.println("Regular exit..."); 286 } catch (Exception e) { 287 System.out.println("Exception caugth..."); 288 e.printStackTrace(); 289 } 290 } 291 292 } 293 | Popular Tags |