KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > innig > macker > recording > GenericRuleRecording


1 /*______________________________________________________________________________
2  *
3  * Macker http://innig.net/macker/
4  *
5  * Copyright 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.recording;
22
23 import net.innig.macker.event.*;
24 import net.innig.macker.rule.Rule;
25 import net.innig.macker.rule.RuleSet;
26
27 import java.io.PrintWriter JavaDoc;
28 import java.util.*;
29
30 import org.jdom.Element;
31 import org.jdom.Attribute;
32
33 import net.innig.collect.CollectionDiff;
34
35 public class GenericRuleRecording
36     extends EventRecording
37     {
38     public GenericRuleRecording(EventRecording parent)
39         {
40         super(parent);
41         events = new HashSet();
42         }
43     
44     public EventRecording record(MackerEvent event)
45         {
46         if(rule == null)
47             rule = event.getRule();
48         if(event.getRule() != rule)
49             return getParent().record(event);
50         
51         Map eventAttributes = new TreeMap();
52         eventType = event.getClass().getName();
53         if(eventType.startsWith(DEFAULT_EVENT_PACKAGE))
54             eventType = eventType.substring(DEFAULT_EVENT_PACKAGE.length());
55         eventAttributes.put("type", eventType);
56         eventAttributes.put("severity", event.getRule().getSeverity().getName());
57         int msgNum = 0;
58         for(Iterator msgIter = event.getMessages().iterator(); msgIter.hasNext(); msgNum++)
59             eventAttributes.put("message" + msgNum, msgIter.next());
60
61         if(event instanceof MessageEvent)
62             { } // done already!
63
else if(event instanceof AccessRuleViolation)
64             {
65             AccessRuleViolation arv = (AccessRuleViolation) event;
66             eventAttributes.put("from", arv.getFrom().getFullName());
67             eventAttributes.put("to", arv.getTo().getFullName());
68             }
69         else
70             throw new IllegalArgumentException JavaDoc("Unknown event type: " + event);
71         
72         events.add(eventAttributes);
73         
74         return this;
75         }
76     
77     public void read(Element elem)
78         {
79         Set eventSet = new HashSet();
80         Map baseAtt = getAttributeValueMap(elem);
81         for(Iterator evtIter = elem.getChildren("event").iterator(); evtIter.hasNext(); )
82             {
83             Element eventElem = (Element) evtIter.next();
84             Map eventAtt = new TreeMap(baseAtt);
85             eventAtt.putAll(getAttributeValueMap(eventElem));
86             eventType = (String JavaDoc) eventAtt.get("type");
87             events.add(eventAtt);
88             }
89         }
90     
91     private Map getAttributeValueMap(Element elem)
92         {
93         Map attValues = new TreeMap();
94         for(Iterator i = elem.getAttributes().iterator(); i.hasNext(); )
95             {
96             Attribute attr = (Attribute) i.next();
97             attValues.put(attr.getName(), attr.getValue());
98             }
99         return attValues;
100         }
101     
102     public boolean compare(EventRecording actual, PrintWriter JavaDoc out)
103         {
104         if(!super.compare(actual, out))
105             return false;
106
107         boolean match = true;
108         GenericRuleRecording actualGRR = (GenericRuleRecording) actual;
109         Set expectedSet = events;
110         Set actualSet = actualGRR.events;
111         CollectionDiff diff = new CollectionDiff(expectedSet, actualSet);
112         if(!diff.getRemoved().isEmpty())
113             {
114             out.println(this + ": missing events:");
115             dump(out, diff.getRemoved());
116             match = false;
117             }
118         if(!diff.getAdded().isEmpty())
119             {
120             out.println(this + ": unexpected events:");
121             dump(out, diff.getAdded());
122             match = false;
123             }
124         return match;
125         }
126     
127     private void dump(PrintWriter JavaDoc out, Collection events)
128         {
129         for(Iterator i = events.iterator(); i.hasNext(); )
130             out.println(" " + i.next());
131         }
132     
133     public String JavaDoc toString()
134         { return "[rule:" + eventType + "]"; }
135     
136     public void dump(PrintWriter JavaDoc out, int indent)
137         {
138         super.dump(out, indent);
139         for(Iterator eventIter = events.iterator(); eventIter.hasNext(); )
140             {
141             Map event = (Map) eventIter.next();
142             for(int n = -3; n < indent; n++)
143                 out.print(' ');
144             out.println(event);
145             }
146         }
147     
148     private Rule rule;
149     private String JavaDoc eventType;
150     private Set/*<Map<String,String>>*/ events;
151     private static final String JavaDoc DEFAULT_EVENT_PACKAGE = "net.innig.macker.event.";
152     }
153
154
155
Popular Tags