1 20 21 package net.innig.macker.rule; 22 23 import net.innig.macker.event.MackerIsMadException; 24 import net.innig.macker.event.ListenerException; 25 import net.innig.macker.structure.ClassManager; 26 import net.innig.macker.structure.ClassInfo; 27 28 import java.util.*; 29 30 import org.apache.commons.lang.exception.NestableRuntimeException; 31 32 public class RuleSet 33 extends Rule 34 { 35 public static RuleSet getMackerDefaults() 36 { 37 if(defaults == null) 38 try { 39 defaults = new RuleSet(); 40 defaults.setPattern("from", new RegexPattern("${from-full}")); 41 defaults.setPattern("to", new RegexPattern("${to-full}")); 42 } 43 catch(MackerRegexSyntaxException mrse) 44 { throw new NestableRuntimeException("Macker built-ins are broken", mrse); } return defaults; 46 } 47 private static RuleSet defaults; 48 49 public RuleSet(RuleSet parent) 50 { 51 super(parent); 52 if(parent == null) 53 throw new IllegalArgumentException ("parent == null"); 54 55 patterns = new HashMap(); 56 rules = new ArrayList(); 57 } 58 59 private RuleSet() 60 { 61 super(null); 62 rules = Collections.EMPTY_LIST; 63 patterns = new HashMap(); 64 } 65 66 public String getName() 67 { 68 if(name == null) 69 return (getParent() != null) 70 ? getParent().getName() 71 : "<anonymous ruleset>"; 72 return name; 73 } 74 75 public void setName(String name) 76 { this.name = name; } 77 78 public boolean hasName() 79 { return name != null; } 80 81 public boolean declaresPattern(String name) 82 { return patterns.keySet().contains(name); } 83 84 public Pattern getPattern(String name) 85 { 86 Pattern pat = (Pattern) patterns.get(name); 87 if(pat != null) 88 return pat; 89 if(getParent() != null) 90 return getParent().getPattern(name); 91 return null; 92 } 93 94 public void setPattern(String name, Pattern pattern) 95 { 96 if(name == null) 97 throw new NullPointerException ("name cannot be null"); 98 if(pattern == null) 99 throw new NullPointerException ("pattern cannot be null"); 100 patterns.put(name, pattern); 101 } 102 103 public Collection getAllPatterns() 104 { return patterns.values(); } 105 106 public void clearPattern(String name) 107 { patterns.remove(name); } 108 109 public Collection getRules() 110 { return rules; } 111 112 public void addRule(Rule rule) 113 { rules.add(rule); } 114 115 public Pattern getSubsetPattern() 116 { return subsetPat; } 117 118 public void setSubsetPattern(Pattern subsetPat) 119 { this.subsetPat = subsetPat; } 120 121 public boolean isInSubset(EvaluationContext context, ClassInfo classInfo) 122 throws RulesException 123 { 124 if(subsetPat != null && !subsetPat.matches(context, classInfo)) 125 return false; 126 if(getParent() != null) 127 return getParent().isInSubset(context, classInfo); 128 return true; 129 } 130 131 public void check( 132 EvaluationContext parentContext, 133 ClassManager classes) 134 throws RulesException, MackerIsMadException, ListenerException 135 { 136 EvaluationContext context = new EvaluationContext(this, parentContext); 137 context.broadcastStarted(); 138 boolean finished = false; 139 try 140 { 141 for(Iterator ruleIter = rules.iterator(); ruleIter.hasNext(); ) 142 { 143 Rule rule = (Rule) ruleIter.next(); 144 rule.check(context, classes); 145 } 146 context.broadcastFinished(); 147 finished = true; 148 } 149 finally 150 { 151 if(!finished) 152 context.broadcastAborted(); 153 } 154 } 155 156 public String toString() 157 { return getClass().getName() + '[' + name + ", parent=" + getParent() + ']'; } 158 159 private String name; 160 private Map patterns; 161 private Collection rules; 162 private Pattern subsetPat; 163 } 164 165 166 | Popular Tags |