1   package com.stateofflow.eclipse.metrics.export.html;
2   
3   import java.io.IOException;
4   import java.util.HashMap;
5   import java.util.HashSet;
6   import java.util.Iterator;
7   import java.util.Map;
8   import java.util.Set;
9   
10  import com.stateofflow.eclipse.metrics.calculators.linesofcode.LinesOfCodeCalculator;
11  import com.stateofflow.eclipse.metrics.collator.MetricsCollator;
12  import com.stateofflow.eclipse.metrics.collator.MetricsCollatorSet;
13  import com.stateofflow.eclipse.metrics.location.MetricLocation;
14  import com.stateofflow.eclipse.metrics.metric.Metric;
15  import com.stateofflow.eclipse.metrics.metric.MetricId;
16  
17  public class SummaryPageWriter {
18      MetricsCollatorSet collatorSet;
19      HtmlExportFactory factory;
20  
21  
22      SummaryPageWriter( HtmlExportFactory factory, MetricsCollatorSet collatorSet ) {
23          this.collatorSet = collatorSet;
24          this.factory = factory;
25      }
26  
27      public void write()  {
28          HtmlPageBuilder pageBuilder = factory.createHtmlPageBuilder();
29          try {
30              pageBuilder.openPage("Project.1", "Summary Statistics" ).openElement("P").printLinkAndNewline("index", "Index").openElementAndNewline("P");
31              GlobalMetricsCollator collator = new GlobalMetricsCollator(collatorSet.getMethodMetrics());
32              pageBuilder.openElementAndNewline("TABLE border=\"1\" cellpadding=\"2\"").openElementAndNewline("TR").createElementAndNewline("TH", "Statistic").createElementAndNewline("TH", "Value").closeElementAndNewline("TR");
33              writeSummaryTableRow(pageBuilder, "# packages", collator.getNumOfPackages() );
34              writeSummaryTableRow(pageBuilder, "# classes", collator.getNumOfClasses() );
35              writeSummaryTableRow(pageBuilder, "TLOC", collator.getTotalLOC() );
36              
37              pageBuilder.closeElement("TABLE");
38              pageBuilder.closePage();
39          } catch (IOException e) {       
40          }
41      }
42  
43      private void writeSummaryTableRow(HtmlPageBuilder pageBuilder,
44              String rowLabel, int metricValue) {
45          pageBuilder.openElementAndNewline("TR").createElementAndNewline("TD", rowLabel ).createElementAndNewline("TD", metricValue + "").closeElementAndNewline("TR");
46      }
47  
48  
49      private class GlobalMetricsCollator {
50          private Set<String> packageSet;
51          private Set<String> classSet;
52          private Map<String, Metric> locMap;
53  
54          GlobalMetricsCollator( MetricsCollator collator ) {
55              packageSet = new HashSet<String>();
56              classSet = new HashSet<String>();
57              locMap = new HashMap<String, Metric>();
58              collator.forEach( new SummaryMetricsClosure(collator) );
59          }
60  
61          int getNumOfPackages() {
62              return packageSet.size();
63          }
64          
65          int getNumOfClasses() {
66              return classSet.size();
67          }
68          
69          int getTotalLOC() {
70              int loc = 0;
71              for ( Iterator<Metric> it = locMap.values().iterator(); it.hasNext(); ) {
72                  Metric metric = it.next(); 
73                  loc += metric.getValue();
74              }                       
75              return loc;
76          }
77          
78          private class SummaryMetricsClosure implements MetricLocation.Closure {
79              MetricsCollator collator;
80              
81              SummaryMetricsClosure( MetricsCollator collator ) {
82                  this.collator = collator;
83              }
84              
85              public void execute( final MetricLocation location ) {
86                  packageSet.add( getPackagename(location) );
87                  classSet.add( getTypeName(location) );
88                  Metric metric = collator.getMetric(location, MetricId.create( LinesOfCodeCalculator.class, "method" ) );
89                  if ( metric != null ) locMap.put( getMethodName(location), metric);
90              }
91              private String getPackagename( MetricLocation location ) {
92                  return (location.getPackageName() == null ? "" : location.getPackageName() + "." );
93              }
94              
95              private String getTypeName( MetricLocation location ) {
96                  return getPackagename(location) + location.getTypeInfo().getName(); 
97              }
98              
99              private String getMethodName( MetricLocation location ) {
100                 return getTypeName(location) + "." + location.getMethodInfo().getName();
101             }
102         }
103     }
104 }