View Javadoc

1   /***
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd;
5   
6   import net.sourceforge.pmd.dfa.report.ReportTree;
7   import net.sourceforge.pmd.stat.Metric;
8   
9   import java.util.ArrayList;
10  import java.util.HashMap;
11  import java.util.HashSet;
12  import java.util.Iterator;
13  import java.util.List;
14  import java.util.Map;
15  import java.util.Set;
16  import java.util.TreeSet;
17  
18  public class Report {
19  
20      public static class ReadableDuration {
21          private long duration;
22          public ReadableDuration(long duration) {
23              this.duration = duration;
24          }
25          public String getTime() {
26              long seconds = 0;
27              long minutes = 0;
28              long hours = 0;
29  
30              if (duration > 1000) {
31                  seconds = duration / 1000;
32              }
33  
34              if (seconds > 60) {
35                  minutes = seconds / 60;
36                  seconds = seconds % 60;
37              }
38  
39              if (minutes > 60) {
40                  hours = minutes / 60;
41                  minutes = minutes % 60;
42              }
43  
44              StringBuffer res = new StringBuffer();
45              if (hours > 0) {
46                  res.append(hours + "h ");
47              }
48              if (hours > 0 || minutes > 0) {
49                  res.append(minutes + "m ");
50              }
51              res.append(seconds + "s");
52              return res.toString();
53          }
54      }
55  
56      public static class ProcessingError {
57          private String msg;
58          private String file;
59  
60          public ProcessingError(String msg, String file) {
61              this.msg = msg;
62              this.file = file;
63          }
64  
65          public String getMsg() {
66              return msg;
67          }
68  
69          public String getFile() {
70              return file;
71          }
72      }
73  
74      /*
75       * The idea is to store the violations in a tree instead of a list, to do
76       * better and faster sort and filter mechanism and to visualize the result
77       * als tree. (ide plugins).
78       * */
79      private ReportTree violationTree = new ReportTree();
80  
81      // Note that this and the above data structure are both being maintained for a bit
82      private Set violations = new TreeSet(new RuleViolation.RuleViolationComparator());
83      private Set metrics = new HashSet();
84      private List listeners = new ArrayList();
85      private List errors = new ArrayList();
86      private Set linesToExclude = new HashSet();
87      private long start;
88      private long end;
89  
90      public void exclude(Set lines) {
91          linesToExclude = lines;
92      }
93  
94      public Map getCountSummary() {
95          Map summary = new HashMap();
96          for (Iterator iter = violationTree.iterator(); iter.hasNext();) {
97              RuleViolation rv = (RuleViolation) iter.next();
98              String key = (rv.getPackageName()  == "" ? "" : (rv.getPackageName() + ".")) + rv.getClassName();
99              Object o = summary.get(key);
100             if (o == null) {
101                 Integer value = new Integer(1);
102                 summary.put(key, value);
103             } else {
104                 Integer value = (Integer) o;
105                 summary.put(key, new Integer(value.intValue() + 1));
106             }
107         }
108         return summary;
109     }
110 
111     public ReportTree getViolationTree() {
112         return this.violationTree;
113     }
114 
115 
116     /***
117      * @return a Map summarizing the Report: String (rule name) ->Integer (count of violations)
118      */
119     public Map getSummary() {
120         Map summary = new HashMap();
121         for (Iterator i = violations.iterator(); i.hasNext();) {
122             RuleViolation rv = (RuleViolation) i.next();
123             if (!summary.containsKey(rv.getRule().getName())) {
124                 summary.put(rv.getRule().getName(), new Integer(0));
125             }
126             Integer count = (Integer) summary.get(rv.getRule().getName());
127             count = new Integer(count.intValue() + 1);
128             summary.put(rv.getRule().getName(), count);
129         }
130         return summary;
131     }
132 
133     public void addListener(ReportListener listener) {
134         listeners.add(listener);
135     }
136 
137     public void addRuleViolation(RuleViolation violation) {
138         if (linesToExclude.contains(new Integer(violation.getLine()))) {
139             return;
140         }
141         violations.add(violation);
142         violationTree.addRuleViolation(violation);
143         for (Iterator i = listeners.iterator(); i.hasNext();) {
144             ReportListener listener = (ReportListener) i.next();
145             listener.ruleViolationAdded(violation);
146         }
147     }
148 
149     public void addMetric(Metric metric) {
150         metrics.add(metric);
151         for (Iterator i = listeners.iterator(); i.hasNext();) {
152             ReportListener listener = (ReportListener) i.next();
153             listener.metricAdded(metric);
154         }
155     }
156 
157     public void addError(ProcessingError error) {
158         errors.add(error);
159     }
160 
161     public boolean hasMetrics() {
162         return !metrics.isEmpty();
163     }
164 
165     public Iterator metrics() {
166         return metrics.iterator();
167     }
168 
169     public boolean isEmpty() {
170         return !violations.iterator().hasNext() && errors.isEmpty();
171     }
172 
173     public boolean treeIsEmpty() {
174         return !violationTree.iterator().hasNext();
175     }
176 
177     public Iterator treeIterator() {
178         return violationTree.iterator();
179     }
180 
181     public Iterator iterator() {
182         return violations.iterator();
183     }
184 
185     public Iterator errors() {
186         return errors.iterator();
187     }
188 
189     public int treeSize() {
190         return violationTree.size();
191     }
192 
193     public int size() {
194         return violations.size();
195     }
196 
197     public void start() {
198         start = System.currentTimeMillis();
199     }
200 
201     public void end() {
202         end = System.currentTimeMillis();
203     }
204 
205     public long getElapsedTimeInMillis() {
206         return end-start;
207     }
208 }