KickJava   Java API By Example, From Geeks To Geeks.

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


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.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); } //! what else to throw?
45
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 JavaDoc("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 JavaDoc 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 JavaDoc name)
76         { this.name = name; }
77     
78     public boolean hasName()
79         { return name != null; }
80     
81     public boolean declaresPattern(String JavaDoc name)
82         { return patterns.keySet().contains(name); }
83     
84     public Pattern getPattern(String JavaDoc 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 JavaDoc name, Pattern pattern)
95         {
96         if(name == null)
97             throw new NullPointerException JavaDoc("name cannot be null");
98         if(pattern == null)
99             throw new NullPointerException JavaDoc("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 JavaDoc 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 JavaDoc toString()
157         { return getClass().getName() + '[' + name + ", parent=" + getParent() + ']'; }
158     
159     private String JavaDoc name;
160     private Map/*<String,Pattern>*/ patterns;
161     private Collection rules;
162     private Pattern subsetPat;
163     }
164
165
166
Popular Tags