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.stat.Metric; 7 8 import java.util.ArrayList; 9 import java.util.HashMap; 10 import java.util.HashSet; 11 import java.util.Iterator; 12 import java.util.List; 13 import java.util.Map; 14 import java.util.Set; 15 import java.util.TreeSet; 16 17 public class Report { 18 19 public static class ProcessingError { 20 private String msg; 21 private String file; 22 23 public ProcessingError(String msg, String file) { 24 this.msg = msg; 25 this.file = file; 26 } 27 28 public String getMsg() { 29 return msg; 30 } 31 32 public String getFile() { 33 return file; 34 } 35 } 36 37 private Set violations = new TreeSet(new RuleViolation.RuleViolationComparator()); 38 private Set metrics = new HashSet(); 39 private List listeners = new ArrayList(); 40 private List errors = new ArrayList(); 41 42 /*** 43 * 44 * @return a Map summarizing the Report: String (rule name) ->Integer (count of violations) 45 */ 46 public Map getSummary() { 47 Map summary = new HashMap(); 48 for (Iterator i = violations.iterator(); i.hasNext();) { 49 RuleViolation rv = (RuleViolation)i.next(); 50 if (!summary.containsKey(rv.getRule().getName())) { 51 summary.put(rv.getRule().getName(), new Integer(0)); 52 } 53 Integer count = (Integer)summary.get(rv.getRule().getName()); 54 count = new Integer(count.intValue() + 1); 55 summary.put(rv.getRule().getName(), count); 56 } 57 return summary; 58 } 59 60 public void addListener(ReportListener listener) { 61 listeners.add(listener); 62 } 63 64 public void addRuleViolation(RuleViolation violation) { 65 violations.add(violation); 66 for (Iterator i = listeners.iterator(); i.hasNext();) { 67 ReportListener listener = (ReportListener) i.next(); 68 listener.ruleViolationAdded(violation); 69 } 70 } 71 72 public void addMetric(Metric metric) { 73 metrics.add(metric); 74 for (Iterator i = listeners.iterator(); i.hasNext();) { 75 ReportListener listener = (ReportListener) i.next(); 76 listener.metricAdded(metric); 77 } 78 } 79 80 public void addError(ProcessingError error) { 81 errors.add(error); 82 } 83 84 public boolean hasMetrics() { 85 return !metrics.isEmpty(); 86 } 87 88 public Iterator metrics() { 89 return metrics.iterator(); 90 } 91 92 public boolean isEmpty() { 93 return violations.isEmpty(); 94 } 95 96 public Iterator iterator() { 97 return violations.iterator(); 98 } 99 100 public Iterator errors() { 101 return errors.iterator(); 102 } 103 104 public int size() { 105 return violations.size(); 106 } 107 108 }