001: /*______________________________________________________________________________
002: *
003: * Macker http://innig.net/macker/
004: *
005: * Copyright 2002-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.rule;
022:
023: import net.innig.macker.structure.ClassManager;
024: import net.innig.macker.structure.ClassInfo;
025: import net.innig.macker.event.*;
026:
027: import java.util.*;
028:
029: public class ForEach extends Rule {
030: public ForEach(RuleSet parent) {
031: super (parent);
032: }
033:
034: public String getVariableName() {
035: return variableName;
036: }
037:
038: public void setVariableName(String variableName) {
039: this .variableName = variableName;
040: }
041:
042: public String getRegex() {
043: return regexS;
044: }
045:
046: public void setRegex(String regexS)
047: throws MackerRegexSyntaxException {
048: this .regexS = regexS;
049: regexPat = new RegexPattern(regexS);
050: }
051:
052: public RuleSet getRuleSet() {
053: return ruleSet;
054: }
055:
056: public void setRuleSet(RuleSet ruleSet) {
057: this .ruleSet = ruleSet;
058: }
059:
060: public void check(EvaluationContext parentContext,
061: ClassManager classes) throws RulesException,
062: MackerIsMadException, ListenerException {
063: EvaluationContext context = new EvaluationContext(ruleSet,
064: parentContext);
065:
066: Set varValues = new TreeSet();
067: Set pool = new HashSet();
068: for (Iterator p = classes.getPrimaryClasses().iterator(); p
069: .hasNext();) {
070: ClassInfo curClass = (ClassInfo) p.next();
071: if (getParent().isInSubset(context, curClass)) {
072: pool.add(curClass);
073: for (Iterator r = curClass.getReferences().keySet()
074: .iterator(); r.hasNext();)
075: pool.add(r.next());
076: }
077: }
078:
079: for (Iterator i = pool.iterator(); i.hasNext();) {
080: ClassInfo classInfo = (ClassInfo) i.next();
081: String varValue = regexPat.getMatch(parentContext,
082: classInfo);
083: if (varValue != null)
084: varValues.add(varValue);
085: }
086:
087: context.broadcastEvent(new ForEachStarted(this ));
088: for (Iterator i = varValues.iterator(); i.hasNext();) {
089: String varValue = (String) i.next();
090: context.broadcastEvent(new ForEachIterationStarted(this ,
091: varValue));
092:
093: context.setVariableValue(getVariableName(), varValue);
094: ruleSet.check(context, classes);
095:
096: context.broadcastEvent(new ForEachIterationFinished(this ,
097: varValue));
098: }
099: context.broadcastEvent(new ForEachFinished(this ));
100: }
101:
102: private RuleSet ruleSet;
103: private String variableName, regexS;
104: private RegexPattern regexPat;
105: }
|