1 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 ; 39 import java.io.File ; 40 import java.io.FileInputStream ; 41 import java.io.InputStreamReader ; 42 import java.util.ArrayList ; 43 import java.util.List ; 44 45 public class StatisticsManager { 46 47 public static final int MAX_REPORTS = 1000; 48 49 protected String type; 50 protected List <String > rawLines = new ArrayList <String >(); 51 52 public StatisticsManager(String type) { 53 this.type = type; 54 load(); 55 } 56 57 public int getStatsCount() { 58 return rawLines.size(); 59 } 60 61 public String getReadableString(int index) { 62 if(index < 0 || index >= rawLines.size()) 63 return null; 64 else { 65 String 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 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 file) { 108 if(file == null) 109 return false; 110 111 File f = new File (file); 112 if(!f.exists()) 113 return false; 114 115 try { 116 BufferedReader br = new BufferedReader (new InputStreamReader (new FileInputStream (f))); 117 String line; 118 while((line = br.readLine()) != null) { 119 addRawLine(line); 120 } 121 br.close(); 122 } catch (Exception e) { 123 e.printStackTrace(); 124 return false; 125 } 126 127 return true; 128 } 129 130 protected void addRawLine(String 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 type) { 141 if(type.equals(StatisticsReporter.TYPE_GRAMMAR)) { 142 String file = getAbsoluteFileName(GrammarReport.GRAMMAR_STATS_FILENAME); 143 new File (file).delete(); 144 } else if(type.equals(StatisticsReporter.TYPE_RUNTIME)) { 145 String file = getAbsoluteFileName(Profiler.RUNTIME_STATS_FILENAME); 146 new File (file).delete(); 147 } else if(type.equals(StatisticsReporter.TYPE_GUI)) 148 StatisticsAW.shared().reset(); 149 } 150 151 public static String getAbsoluteFileName(String filename) { 152 return System.getProperty("user.home")+File.separator+ 153 Stats.ANTLRWORKS_DIR+File.separator+ 154 filename; 155 } 156 157 } 158 | Popular Tags |