1 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 getVariableName() 36 { return variableName; } 37 38 public void setVariableName(String variableName) 39 { this.variableName = variableName; } 40 41 public String getRegex() 42 { return regexS; } 43 44 public void setRegex(String 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 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 varValue = (String ) 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 variableName, regexS; 101 private RegexPattern regexPat; 102 } 103 | Popular Tags |