KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > stats > StatisticsManager


1 /*
2
3 [The "BSD licence"]
4 Copyright (c) 2005 Jean Bovet
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16 3. The name of the author may not be used to endorse or promote products
17 derived from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 */

31
32 package org.antlr.works.stats;
33
34 import org.antlr.runtime.debug.Profiler;
35 import org.antlr.runtime.misc.Stats;
36 import org.antlr.tool.GrammarReport;
37
38 import java.io.BufferedReader JavaDoc;
39 import java.io.File JavaDoc;
40 import java.io.FileInputStream JavaDoc;
41 import java.io.InputStreamReader JavaDoc;
42 import java.util.ArrayList JavaDoc;
43 import java.util.List JavaDoc;
44
45 public class StatisticsManager {
46
47     public static final int MAX_REPORTS = 1000;
48
49     protected String JavaDoc type;
50     protected List JavaDoc<String JavaDoc> rawLines = new ArrayList JavaDoc<String JavaDoc>();
51
52     public StatisticsManager(String JavaDoc type) {
53         this.type = type;
54         load();
55     }
56
57     public int getStatsCount() {
58         return rawLines.size();
59     }
60
61     public String JavaDoc getReadableString(int index) {
62         if(index < 0 || index >= rawLines.size())
63             return null;
64         else {
65             String JavaDoc rawLine = rawLines.get(index);
66             if(type.equals(StatisticsReporter.TYPE_GRAMMAR))
67                 return GrammarReport.toString(rawLine);
68             else if(type.equals(StatisticsReporter.TYPE_RUNTIME))
69                 return Profiler.toString(rawLine);
70             else
71                 return StatisticsAW.shared().getReadableString();
72         }
73     }
74
75     public String JavaDoc getRawString(int index) {
76         if(index < 0 || index >= rawLines.size())
77             return null;
78         else
79             return rawLines.get(index);
80     }
81
82     public boolean load() {
83         rawLines.clear();
84         if(type.equals(StatisticsReporter.TYPE_GRAMMAR))
85             return loadGrammar();
86         else if(type.equals(StatisticsReporter.TYPE_RUNTIME))
87             return loadRuntime();
88         else if(type.equals(StatisticsReporter.TYPE_GUI))
89             return loadGUI();
90         else
91             return false;
92     }
93
94     protected boolean loadGUI() {
95         addRawLine(StatisticsAW.shared().getRawString());
96         return true;
97     }
98
99     protected boolean loadGrammar() {
100         return loadFromFile(getAbsoluteFileName(GrammarReport.GRAMMAR_STATS_FILENAME));
101     }
102
103     protected boolean loadRuntime() {
104         return loadFromFile(getAbsoluteFileName(Profiler.RUNTIME_STATS_FILENAME));
105     }
106
107     protected boolean loadFromFile(String JavaDoc file) {
108         if(file == null)
109             return false;
110
111         File JavaDoc f = new File JavaDoc(file);
112         if(!f.exists())
113             return false;
114
115         try {
116             BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(new FileInputStream JavaDoc(f)));
117             String JavaDoc line;
118             while((line = br.readLine()) != null) {
119                 addRawLine(line);
120             }
121             br.close();
122         } catch (Exception JavaDoc e) {
123             e.printStackTrace();
124             return false;
125         }
126
127         return true;
128     }
129
130     protected void addRawLine(String JavaDoc line) {
131         rawLines.add(line);
132         if(rawLines.size() > MAX_REPORTS)
133             rawLines.remove(0);
134     }
135
136     public void reset() {
137         reset(type);
138     }
139
140     public static void reset(String JavaDoc type) {
141         if(type.equals(StatisticsReporter.TYPE_GRAMMAR)) {
142             String JavaDoc file = getAbsoluteFileName(GrammarReport.GRAMMAR_STATS_FILENAME);
143             new File JavaDoc(file).delete();
144         } else if(type.equals(StatisticsReporter.TYPE_RUNTIME)) {
145             String JavaDoc file = getAbsoluteFileName(Profiler.RUNTIME_STATS_FILENAME);
146             new File JavaDoc(file).delete();
147         } else if(type.equals(StatisticsReporter.TYPE_GUI))
148             StatisticsAW.shared().reset();
149     }
150
151     public static String JavaDoc getAbsoluteFileName(String JavaDoc filename) {
152         return System.getProperty("user.home")+File.separator+
153                     Stats.ANTLRWORKS_DIR+File.separator+
154                     filename;
155     }
156
157 }
158
Popular Tags