1 20 21 package net.innig.macker.event; 22 23 import net.innig.macker.rule.RuleSet; 24 import net.innig.macker.rule.RuleSeverity; 25 26 import java.io.Writer ; 27 import java.io.PrintWriter ; 28 import java.io.OutputStream ; 29 import java.util.*; 30 import net.innig.collect.MultiMap; 31 import net.innig.collect.CompositeMultiMap; 32 33 public class PrintingListener 34 implements MackerEventListener 35 { 36 public PrintingListener(PrintWriter out) 37 { this.out = out; } 38 39 public PrintingListener(Writer out) 40 { this.out = new PrintWriter (out, true); } 41 42 public PrintingListener(OutputStream out) 43 { this.out = new PrintWriter (out, true); } 44 45 public void setThreshold(RuleSeverity threshold) 46 { this.threshold = threshold; } 47 48 public void setMaxMessages(int maxMessages) 49 { this.maxMessages = maxMessages; } 50 51 public void mackerStarted(RuleSet ruleSet) 52 { 53 if(ruleSet.getParent() == null || ruleSet.hasName()) 54 { 55 out.println(); 56 out.println("(Checking ruleset: " + ruleSet.getName() + " ...)"); 57 first = true; 58 } 59 } 60 61 public void mackerFinished(RuleSet ruleSet) 62 throws MackerIsMadException 63 { } 64 65 public void mackerAborted(RuleSet ruleSet) 66 { } 68 public void handleMackerEvent(RuleSet ruleSet, MackerEvent event) 69 throws MackerIsMadException 70 { 71 if(event instanceof ForEachEvent) 72 { 73 if(event instanceof ForEachIterationStarted) 74 { 75 ForEachIterationStarted iterStart = (ForEachIterationStarted) event; 76 out.print('('); 77 out.print(iterStart.getForEach().getVariableName()); 78 out.print(": "); 79 out.print(iterStart.getVariableValue()); 80 out.println(")"); 81 } 82 } 84 else 85 { 86 eventsBySeverity.put(event.getRule().getSeverity(), event); 87 if(event.getRule().getSeverity().compareTo(threshold) >= 0) 88 { 89 if(messagesPrinted < maxMessages) 90 { 91 if(first) 92 { 93 out.println(); 94 first = false; 95 } 96 out.println(event.toStringVerbose()); 97 } 98 if(messagesPrinted == maxMessages) 99 out.println("WARNING: Exceeded the limit of " + maxMessages + " message" 100 + (maxMessages==1 ? "" : "s") + "; further messages surpressed"); 101 messagesPrinted++; 102 } 103 104 } 105 } 106 107 public void printSummary() 108 { 109 boolean firstSeverity = true; 111 List severities = new ArrayList(eventsBySeverity.keySet()); 112 Collections.reverse(severities); 113 for(Iterator i = severities.iterator(); i.hasNext(); ) 114 { 115 RuleSeverity severity = (RuleSeverity) i.next(); 116 Collection eventsForSev = eventsBySeverity.get(severity); 117 if(eventsForSev.size() > 0) 118 { 119 if(firstSeverity) 120 out.print("("); 121 else 122 out.print(", "); 123 firstSeverity = false; 124 out.print(eventsForSev.size()); 125 out.print(' '); 126 out.print((eventsForSev.size() == 1) 127 ? severity.getName() 128 : severity.getNamePlural()); 129 } 130 } 131 if(!firstSeverity) 132 out.println(')'); 133 } 134 135 public String toString() 136 { return "PrintingListener"; } 137 138 private boolean first; 139 private PrintWriter out; 140 private int maxMessages = Integer.MAX_VALUE, messagesPrinted = 0; 141 private RuleSeverity threshold = RuleSeverity.INFO; 142 private final MultiMap eventsBySeverity = new CompositeMultiMap(TreeMap.class, HashSet.class); 143 } 144 145 | Popular Tags |