KickJava   Java API By Example, From Geeks To Geeks.

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


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
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 JavaDoc("Unknown element: " + childElem);
74             child.read(childElem);
75             children.add(child);
76             }
77         }
78     
79     public boolean compare(EventRecording actual, PrintWriter JavaDoc 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 JavaDoc toString()
109         { return (name == null) ? "[ruleset]" : "[ruleset:" + name + "]"; }
110     
111     public void dump(PrintWriter JavaDoc 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 JavaDoc name;
122     private List/*<EventRecording>*/ children;
123     }
124
125
126
127
128
Popular Tags