1 20 21 package edu.umd.cs.findbugs; 22 23 import java.io.ByteArrayInputStream ; 24 import java.io.ByteArrayOutputStream ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.OutputStream ; 28 import java.io.Reader ; 29 import java.io.Writer ; 30 import java.text.ParseException ; 31 import java.text.SimpleDateFormat ; 32 import java.text.NumberFormat ; 33 import java.util.Collection ; 34 import java.util.Date ; 35 import java.util.Locale ; 36 import java.util.SortedMap ; 37 import java.util.TreeMap ; 38 39 import javax.xml.transform.Transformer ; 40 import javax.xml.transform.TransformerException ; 41 import javax.xml.transform.TransformerFactory ; 42 import javax.xml.transform.stream.StreamResult ; 43 import javax.xml.transform.stream.StreamSource ; 44 45 import edu.umd.cs.findbugs.PackageStats.ClassStats; 46 import edu.umd.cs.findbugs.xml.OutputStreamXMLOutput; 47 import edu.umd.cs.findbugs.xml.XMLOutput; 48 import edu.umd.cs.findbugs.xml.XMLWriteable; 49 50 53 public class ProjectStats implements XMLWriteable, Cloneable { 54 private static final String TIMESTAMP_FORMAT = "EEE, d MMM yyyy HH:mm:ss Z"; 55 private SortedMap <String , PackageStats> packageStatsMap; 56 private int[] totalErrors = new int[] { 0, 0, 0, 0, 0 }; 57 private int totalClasses; 58 private int totalSize; 59 private Date timestamp; 60 private Footprint baseFootprint; 61 62 65 public ProjectStats() { 66 this.packageStatsMap = new TreeMap <String , PackageStats>(); 67 this.totalClasses = 0; 68 this.timestamp = new Date (); 69 this.baseFootprint = new Footprint(); 70 } 71 72 @Override 73 public Object clone() { 74 try { 75 return super.clone(); 76 } catch (CloneNotSupportedException e) { 77 throw new AssertionError (e); 79 } 80 } 81 82 public int getCodeSize() { 83 return totalSize; 84 } 85 public int getTotalBugs() { 86 return totalErrors[0]; 87 } 88 public int getBugsOfPriority(int priority) { 89 return totalErrors[priority]; 90 } 91 98 public void setTimestamp(String timestamp) throws ParseException { 99 this.timestamp = new SimpleDateFormat (TIMESTAMP_FORMAT, Locale.ENGLISH).parse(timestamp); 100 } 101 102 public void setTimestamp(long timestamp) { 103 this.timestamp = new Date (timestamp); 104 } 105 106 109 public int getNumClasses() { 110 return totalClasses; 111 } 112 113 121 public void addClass(String className, boolean isInterface, int size) { 122 String packageName; 123 int lastDot = className.lastIndexOf('.'); 124 if (lastDot < 0) 125 packageName = ""; 126 else 127 packageName = className.substring(0, lastDot); 128 PackageStats stat = getPackageStats(packageName); 129 stat.addClass(className, isInterface, size); 130 totalClasses++; 131 totalSize += size; 132 } 133 134 139 public ClassStats getClassStats(String className) { 140 String packageName; 141 int lastDot = className.lastIndexOf('.'); 142 if (lastDot < 0) 143 packageName = ""; 144 else 145 packageName = className.substring(0, lastDot); 146 PackageStats stat = getPackageStats(packageName); 147 return stat.getClassStatsOrNull(className); 148 } 149 150 153 public void addBug(BugInstance bug) { 154 PackageStats stat = getPackageStats(bug.getPrimaryClass().getPackageName()); 155 stat.addError(bug); 156 ++totalErrors[0]; 157 int priority = bug.getPriority(); 158 if (priority >= 1) { 159 ++totalErrors[Math.min(priority, totalErrors.length - 1)]; 160 } 161 } 162 163 166 public void clearBugCounts() { 167 for(int i = 0; i < totalErrors.length; i++) 168 totalErrors[i] = 0; 169 for (PackageStats stats : packageStatsMap.values()) { 170 stats.clearBugCounts(); 171 } 172 } 173 176 public void writeXML(XMLOutput xmlOutput) throws IOException { 177 xmlOutput.startTag("FindBugsSummary"); 178 179 xmlOutput.addAttribute("timestamp", 180 new SimpleDateFormat (TIMESTAMP_FORMAT, Locale.ENGLISH).format(timestamp)); 181 xmlOutput.addAttribute("total_classes", String.valueOf(totalClasses)); 182 xmlOutput.addAttribute("total_bugs", String.valueOf(totalErrors[0])); 183 xmlOutput.addAttribute("total_size", String.valueOf(totalSize)); 184 xmlOutput.addAttribute("num_packages", String.valueOf(packageStatsMap.size())); 185 186 Footprint delta = new Footprint(baseFootprint); 187 NumberFormat twoPlaces = NumberFormat.getInstance(Locale.ENGLISH); 188 twoPlaces.setMinimumFractionDigits(2); 189 twoPlaces.setMaximumFractionDigits(2); 190 twoPlaces.setGroupingUsed(false); 191 long cpuTime = delta.getCpuTime(); if (cpuTime >= 0) { 193 xmlOutput.addAttribute("cpu_seconds", twoPlaces.format(cpuTime / 1000000000.0)); 194 } 195 long clockTime = delta.getClockTime(); if (clockTime >= 0) { 197 xmlOutput.addAttribute("clock_seconds", twoPlaces.format(clockTime / 1000.0)); 198 } 199 long peakMemory = delta.getPeakMemory(); if (peakMemory >= 0) { 201 xmlOutput.addAttribute("peak_mbytes", twoPlaces.format(peakMemory / (1024.0*1024))); 202 } 203 long gcTime = delta.getCollectionTime(); if (gcTime >= 0) { 205 xmlOutput.addAttribute("gc_seconds", twoPlaces.format(gcTime / 1000.0)); 206 } 207 208 PackageStats.writeBugPriorities(xmlOutput, totalErrors); 209 210 xmlOutput.stopTag(false); 211 212 for (PackageStats stats : packageStatsMap.values()) { 213 stats.writeXML(xmlOutput); 214 } 215 216 xmlOutput.closeTag("FindBugsSummary"); 217 } 218 219 222 public void reportSummary(OutputStream out) throws IOException { 223 XMLOutput xmlOutput = new OutputStreamXMLOutput(out); 224 writeXML(xmlOutput); 225 xmlOutput.finish(); 226 } 227 228 233 public void transformSummaryToHTML(Writer htmlWriter) 234 throws IOException , TransformerException { 235 236 ByteArrayOutputStream summaryOut = new ByteArrayOutputStream (8096); 237 reportSummary(summaryOut); 238 239 StreamSource in = new StreamSource (new ByteArrayInputStream (summaryOut.toByteArray())); 240 StreamResult out = new StreamResult (htmlWriter); 241 InputStream xslInputStream = this.getClass().getClassLoader().getResourceAsStream("summary.xsl"); 242 if (xslInputStream == null) 243 throw new IOException ("Could not load summary stylesheet"); 244 StreamSource xsl = new StreamSource (xslInputStream); 245 246 TransformerFactory tf = TransformerFactory.newInstance(); 247 Transformer transformer = tf.newTransformer(xsl); 248 transformer.transform(in, out); 249 250 Reader rdr = in.getReader(); 251 if (rdr != null) 252 rdr.close(); 253 htmlWriter.close(); 254 InputStream is = xsl.getInputStream(); 255 if (is != null) 256 is.close(); 257 } 258 259 public Collection <PackageStats> getPackageStats() { 260 return packageStatsMap.values(); 261 } 262 private PackageStats getPackageStats(String packageName) { 263 PackageStats stat = packageStatsMap.get(packageName); 264 if (stat == null) { 265 stat = new PackageStats(packageName); 266 packageStatsMap.put(packageName, stat); 267 } 268 return stat; 269 } 270 271 274 public void addStats(ProjectStats stats2) { 275 totalSize += stats2.totalSize; 276 totalClasses += stats2.totalClasses; 277 for(int i = 0; i < totalErrors.length; i++) 278 totalErrors[i] += stats2.totalErrors[i]; 279 packageStatsMap.putAll(stats2.packageStatsMap); 280 } 281 } 282 | Popular Tags |