1 17 18 19 20 package org.apache.fop.memory; 21 22 import java.io.BufferedReader ; 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.io.OutputStream ; 26 27 import javax.xml.transform.Result ; 28 import javax.xml.transform.Source ; 29 import javax.xml.transform.Transformer ; 30 import javax.xml.transform.sax.SAXResult ; 31 import javax.xml.transform.sax.SAXTransformerFactory ; 32 import javax.xml.transform.stream.StreamSource ; 33 34 import org.apache.commons.io.output.NullOutputStream; 35 import org.apache.fop.apps.Fop; 36 import org.apache.fop.apps.FopFactory; 37 import org.apache.fop.apps.MimeConstants; 38 39 42 public class MemoryEater { 43 44 private static void eatMemory(File foFile, int replicatorRepeats) throws Exception { 45 46 SAXTransformerFactory tFactory = (SAXTransformerFactory )SAXTransformerFactory.newInstance(); 47 FopFactory fopFactory = FopFactory.newInstance(); 48 49 File xsltFile = new File ("test/xsl/fo-replicator.xsl"); 50 Source xslt = new StreamSource (xsltFile); 51 52 Source src = new StreamSource (foFile); 53 54 Transformer transformer = tFactory.newTransformer(xslt); 55 transformer.setParameter("repeats", new Integer (replicatorRepeats)); 56 57 OutputStream out = new NullOutputStream(); Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out); 59 Result res = new SAXResult (fop.getDefaultHandler()); 60 61 transformer.transform(src, res); 62 63 System.out.println("Generated " + fop.getResults().getPageCount() + " pages."); 64 65 } 66 67 private static void prompt() throws IOException { 68 BufferedReader in = new BufferedReader (new java.io.InputStreamReader (System.in)); 69 System.out.print("Press return to continue..."); 70 in.readLine(); 71 } 72 73 77 public static void main(String [] args) { 78 boolean doPrompt = true; try { 80 int replicatorRepeats = 2; 81 if (args.length > 0) { 82 replicatorRepeats = Integer.parseInt(args[0]); 83 } 84 File testFile = new File ("examples/fo/basic/readme.fo"); 85 86 System.out.println("MemoryEater! About to replicate the test file " 87 + replicatorRepeats + " times..."); 88 if (doPrompt) { 89 prompt(); 90 } 91 92 System.out.println("Processing..."); 93 long start = System.currentTimeMillis(); 94 95 eatMemory(testFile, replicatorRepeats); 96 97 long duration = System.currentTimeMillis() - start; 98 System.out.println("Success! Job took " + duration + " ms"); 99 100 if (doPrompt) { 101 prompt(); 102 } 103 } catch (Exception e) { 104 e.printStackTrace(); 105 } 106 } 107 108 } 109 | Popular Tags |