KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > innig > macker > event > PrintingListener


1 /*______________________________________________________________________________
2  *
3  * Macker http://innig.net/macker/
4  *
5  * Copyright 2002-2003 Paul Cantrell
6  *
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License version 2, as published by the
9  * Free Software Foundation. See the file LICENSE.html for more information.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the license for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
17  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
18  *______________________________________________________________________________
19  */

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 JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28 import java.io.OutputStream JavaDoc;
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 JavaDoc out)
37         { this.out = out; }
38     
39     public PrintingListener(Writer JavaDoc out)
40         { this.out = new PrintWriter JavaDoc(out, true); }
41     
42     public PrintingListener(OutputStream JavaDoc out)
43         { this.out = new PrintWriter JavaDoc(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         { } // don't care
67

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             // ignore other ForEachEvents
83
}
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         // output looks like: "(2 errors, 1 warning)"
110
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 JavaDoc toString()
136         { return "PrintingListener"; }
137         
138     private boolean first;
139     private PrintWriter JavaDoc 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