KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > memory > MemoryEater


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$ */
19
20 package org.apache.fop.memory;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.OutputStream JavaDoc;
26
27 import javax.xml.transform.Result JavaDoc;
28 import javax.xml.transform.Source JavaDoc;
29 import javax.xml.transform.Transformer JavaDoc;
30 import javax.xml.transform.sax.SAXResult JavaDoc;
31 import javax.xml.transform.sax.SAXTransformerFactory JavaDoc;
32 import javax.xml.transform.stream.StreamSource JavaDoc;
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 /**
40  * Debug tool to create and process large FO files by replicating them a specified number of times.
41  */

42 public class MemoryEater {
43
44     private static void eatMemory(File JavaDoc foFile, int replicatorRepeats) throws Exception JavaDoc {
45
46         SAXTransformerFactory JavaDoc tFactory = (SAXTransformerFactory JavaDoc)SAXTransformerFactory.newInstance();
47         FopFactory fopFactory = FopFactory.newInstance();
48         
49         File JavaDoc xsltFile = new File JavaDoc("test/xsl/fo-replicator.xsl");
50         Source JavaDoc xslt = new StreamSource JavaDoc(xsltFile);
51         
52         Source JavaDoc src = new StreamSource JavaDoc(foFile);
53         
54         Transformer JavaDoc transformer = tFactory.newTransformer(xslt);
55         transformer.setParameter("repeats", new Integer JavaDoc(replicatorRepeats));
56         
57         OutputStream JavaDoc out = new NullOutputStream(); //write to /dev/nul
58
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
59         Result JavaDoc res = new SAXResult JavaDoc(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 JavaDoc {
68         BufferedReader JavaDoc in = new BufferedReader JavaDoc(new java.io.InputStreamReader JavaDoc(System.in));
69         System.out.print("Press return to continue...");
70         in.readLine();
71     }
72     
73     /**
74      * Main method.
75      * @param args the command-line arguments
76      */

77     public static void main(String JavaDoc[] args) {
78         boolean doPrompt = true; //true if you want a chance to start the monitoring console
79
try {
80             int replicatorRepeats = 2;
81             if (args.length > 0) {
82                 replicatorRepeats = Integer.parseInt(args[0]);
83             }
84             File JavaDoc testFile = new File JavaDoc("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 JavaDoc e) {
104             e.printStackTrace();
105         }
106     }
107
108 }
109
Popular Tags