001: /*______________________________________________________________________________
002: *
003: * Macker http://innig.net/macker/
004: *
005: * Copyright 2003 Paul Cantrell
006: *
007: * This program is free software; you can redistribute it and/or modify it under
008: * the terms of the GNU General Public License version 2, as published by the
009: * Free Software Foundation. See the file LICENSE.html for more information.
010: *
011: * This program is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the license for more details.
014: *
015: * You should have received a copy of the GNU General Public License along with
016: * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
017: * Place, Suite 330 / Boston, MA 02111-1307 / USA.
018: *______________________________________________________________________________
019: */
020:
021: package net.innig.macker.recording;
022:
023: import net.innig.macker.event.*;
024: import net.innig.macker.rule.Rule;
025: import net.innig.macker.rule.RuleSet;
026:
027: import java.io.PrintWriter;
028: import java.util.*;
029:
030: import org.jdom.Element;
031: import org.jdom.Attribute;
032:
033: import net.innig.collect.CollectionDiff;
034:
035: public class GenericRuleRecording extends EventRecording {
036: public GenericRuleRecording(EventRecording parent) {
037: super (parent);
038: events = new HashSet();
039: }
040:
041: public EventRecording record(MackerEvent event) {
042: if (rule == null)
043: rule = event.getRule();
044: if (event.getRule() != rule)
045: return getParent().record(event);
046:
047: Map eventAttributes = new TreeMap();
048: eventType = event.getClass().getName();
049: if (eventType.startsWith(DEFAULT_EVENT_PACKAGE))
050: eventType = eventType.substring(DEFAULT_EVENT_PACKAGE
051: .length());
052: eventAttributes.put("type", eventType);
053: eventAttributes.put("severity", event.getRule().getSeverity()
054: .getName());
055: int msgNum = 0;
056: for (Iterator msgIter = event.getMessages().iterator(); msgIter
057: .hasNext(); msgNum++)
058: eventAttributes.put("message" + msgNum, msgIter.next());
059:
060: if (event instanceof MessageEvent) {
061: } // done already!
062: else if (event instanceof AccessRuleViolation) {
063: AccessRuleViolation arv = (AccessRuleViolation) event;
064: eventAttributes.put("from", arv.getFrom().getFullName());
065: eventAttributes.put("to", arv.getTo().getFullName());
066: } else
067: throw new IllegalArgumentException("Unknown event type: "
068: + event);
069:
070: events.add(eventAttributes);
071:
072: return this ;
073: }
074:
075: public void read(Element elem) {
076: Set eventSet = new HashSet();
077: Map baseAtt = getAttributeValueMap(elem);
078: for (Iterator evtIter = elem.getChildren("event").iterator(); evtIter
079: .hasNext();) {
080: Element eventElem = (Element) evtIter.next();
081: Map eventAtt = new TreeMap(baseAtt);
082: eventAtt.putAll(getAttributeValueMap(eventElem));
083: eventType = (String) eventAtt.get("type");
084: events.add(eventAtt);
085: }
086: }
087:
088: private Map getAttributeValueMap(Element elem) {
089: Map attValues = new TreeMap();
090: for (Iterator i = elem.getAttributes().iterator(); i.hasNext();) {
091: Attribute attr = (Attribute) i.next();
092: attValues.put(attr.getName(), attr.getValue());
093: }
094: return attValues;
095: }
096:
097: public boolean compare(EventRecording actual, PrintWriter out) {
098: if (!super .compare(actual, out))
099: return false;
100:
101: boolean match = true;
102: GenericRuleRecording actualGRR = (GenericRuleRecording) actual;
103: Set expectedSet = events;
104: Set actualSet = actualGRR.events;
105: CollectionDiff diff = new CollectionDiff(expectedSet, actualSet);
106: if (!diff.getRemoved().isEmpty()) {
107: out.println(this + ": missing events:");
108: dump(out, diff.getRemoved());
109: match = false;
110: }
111: if (!diff.getAdded().isEmpty()) {
112: out.println(this + ": unexpected events:");
113: dump(out, diff.getAdded());
114: match = false;
115: }
116: return match;
117: }
118:
119: private void dump(PrintWriter out, Collection events) {
120: for (Iterator i = events.iterator(); i.hasNext();)
121: out.println(" " + i.next());
122: }
123:
124: public String toString() {
125: return "[rule:" + eventType + "]";
126: }
127:
128: public void dump(PrintWriter out, int indent) {
129: super .dump(out, indent);
130: for (Iterator eventIter = events.iterator(); eventIter
131: .hasNext();) {
132: Map event = (Map) eventIter.next();
133: for (int n = -3; n < indent; n++)
134: out.print(' ');
135: out.println(event);
136: }
137: }
138:
139: private Rule rule;
140: private String eventType;
141: private Set/*<Map<String,String>>*/events;
142: private static final String DEFAULT_EVENT_PACKAGE = "net.innig.macker.event.";
143: }
|