1 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 ; 28 import java.util.*; 29 30 import org.jdom.Element; 31 32 import net.innig.collect.CollectionDiff; 33 34 public class RuleSetRecording 35 extends EventRecording 36 { 37 public RuleSetRecording(EventRecording parent) 38 { 39 super(parent); 40 children = new ArrayList(); 41 } 42 43 public EventRecording record(MackerEvent event) 44 { 45 if(name == null) 46 { 47 RuleSet ruleSet = event.getRule().getParent(); 48 name = ruleSet.hasName() ? ruleSet.getName() : null; 49 } 50 51 EventRecording child; 52 if(event instanceof ForEachStarted) 53 child = new ForEachRecording(this); 54 else if(event instanceof ForEachIterationFinished) 55 return getParent().record(event); 56 else 57 child = new GenericRuleRecording(this); 58 children.add(child); 59 return child.record(event); 60 } 61 62 public void read(Element elem) 63 { 64 for(Iterator childIter = elem.getChildren().iterator(); childIter.hasNext(); ) 65 { 66 Element childElem = (Element) childIter.next(); 67 EventRecording child; 68 if(childElem.getName().equals("rule")) 69 child = new GenericRuleRecording(this); 70 else if(childElem.getName().equals("foreach")) 71 child = new ForEachRecording(this); 72 else 73 throw new RuntimeException ("Unknown element: " + childElem); 74 child.read(childElem); 75 children.add(child); 76 } 77 } 78 79 public boolean compare(EventRecording actual, PrintWriter out) 80 { 81 if(!super.compare(actual, out)) 82 return false; 83 84 RuleSetRecording actualRuleSet = (RuleSetRecording) actual; 85 86 if(children.size() != actualRuleSet.children.size()) 87 { 88 out.println( 89 "expected " + children.size() 90 + " rules generating events, but got " + actualRuleSet.children.size() 91 + ": " + actualRuleSet.children); 92 return false; 93 } 94 95 boolean match = true; 96 Iterator 97 expectedIter = children.iterator(), 98 actualIter = actualRuleSet.children.iterator(); 99 while(expectedIter.hasNext()) 100 { 101 EventRecording expectedChild = (EventRecording) expectedIter.next(); 102 EventRecording actualChild = (EventRecording) actualIter.next(); 103 match = expectedChild.compare(actualChild, out) && match; 104 } 105 return match; 106 } 107 108 public String toString() 109 { return (name == null) ? "[ruleset]" : "[ruleset:" + name + "]"; } 110 111 public void dump(PrintWriter out, int indent) 112 { 113 super.dump(out, indent); 114 for(Iterator childIter = children.iterator(); childIter.hasNext(); ) 115 { 116 EventRecording child = (EventRecording) childIter.next(); 117 child.dump(out, indent+3); 118 } 119 } 120 121 private String name; 122 private List children; 123 } 124 125 126 127 128 | Popular Tags |