KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > innig > macker > rule > ForEach


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.rule;
22
23 import net.innig.macker.structure.ClassManager;
24 import net.innig.macker.structure.ClassInfo;
25 import net.innig.macker.event.*;
26
27 import java.util.*;
28
29 public class ForEach
30     extends Rule
31     {
32     public ForEach(RuleSet parent)
33         { super(parent); }
34         
35     public String JavaDoc getVariableName()
36         { return variableName; }
37     
38     public void setVariableName(String JavaDoc variableName)
39         { this.variableName = variableName; }
40     
41     public String JavaDoc getRegex()
42         { return regexS; }
43     
44     public void setRegex(String JavaDoc regexS)
45         throws MackerRegexSyntaxException
46         {
47         this.regexS = regexS;
48         regexPat = new RegexPattern(regexS);
49         }
50     
51     public RuleSet getRuleSet()
52         { return ruleSet; }
53     
54     public void setRuleSet(RuleSet ruleSet)
55          { this.ruleSet = ruleSet; }
56
57     public void check(
58             EvaluationContext parentContext,
59             ClassManager classes)
60         throws RulesException, MackerIsMadException, ListenerException
61         {
62         EvaluationContext context = new EvaluationContext(ruleSet, parentContext);
63         
64         Set varValues = new TreeSet();
65         Set pool = new HashSet();
66         for(Iterator p = classes.getPrimaryClasses().iterator(); p.hasNext(); )
67             {
68             ClassInfo curClass = (ClassInfo) p.next();
69             if(getParent().isInSubset(context, curClass))
70                 {
71                 pool.add(curClass);
72                 for(Iterator r = curClass.getReferences().keySet().iterator(); r.hasNext(); )
73                     pool.add(r.next());
74                 }
75             }
76         
77         for(Iterator i = pool.iterator(); i.hasNext(); )
78             {
79             ClassInfo classInfo = (ClassInfo) i.next();
80             String JavaDoc varValue = regexPat.getMatch(parentContext, classInfo);
81             if(varValue != null)
82                 varValues.add(varValue);
83             }
84         
85         context.broadcastEvent(new ForEachStarted(this));
86         for(Iterator i = varValues.iterator(); i.hasNext(); )
87             {
88             String JavaDoc varValue = (String JavaDoc) i.next();
89             context.broadcastEvent(new ForEachIterationStarted(this, varValue));
90             
91             context.setVariableValue(getVariableName(), varValue);
92             ruleSet.check(context, classes);
93
94             context.broadcastEvent(new ForEachIterationFinished(this, varValue));
95             }
96         context.broadcastEvent(new ForEachFinished(this));
97         }
98     
99     private RuleSet ruleSet;
100     private String JavaDoc variableName, regexS;
101     private RegexPattern regexPat;
102     }
103
Popular Tags