KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > innig > macker > rule > filter > AccessFilter


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.filter;
22
23 import net.innig.macker.rule.*;
24 import net.innig.macker.structure.ClassInfo;
25 import net.innig.macker.structure.AccessModifier;
26 import java.util.Map JavaDoc;
27 import java.util.List JavaDoc;
28
29 public class AccessFilter
30     implements Filter
31     {
32     public Pattern createPattern(
33             RuleSet ruleSet,
34             List JavaDoc/*<Pattern>*/ params,
35             Map JavaDoc/*<String,String>*/ options)
36         throws RulesException
37         {
38         if(params.size() != 0)
39             throw new FilterSyntaxException(
40                 this,
41                 "Filter \"" + options.get("filter") + "\" expects no parameters, but has " + params.size());
42
43         String JavaDoc
44             maxS = (String JavaDoc) options.get("max"),
45             minS = (String JavaDoc) options.get("min");
46         final AccessModifier
47             max = (maxS != null) ? AccessModifier.fromName(maxS) : AccessModifier.PUBLIC,
48             min = (minS != null) ? AccessModifier.fromName(minS) : AccessModifier.PRIVATE;
49             
50         if(maxS == null && minS == null)
51             throw new FilterSyntaxException(
52                 this, options.get("filter") + " requires a \"max\" or \"min\" option (or both)");
53         if(max == null && maxS != null)
54             throw new FilterSyntaxException(
55                 this,
56                 '"' + maxS + "\" is not a valid access level; expected one of: "
57                 + AccessModifier.allTypesSorted(AccessModifier.class));
58         if(min == null && minS != null)
59             throw new FilterSyntaxException(
60                 this,
61                 '"' + minS + "\" is not a valid access level; expected one of: "
62                 + AccessModifier.allTypesSorted(AccessModifier.class));
63         
64         return new Pattern()
65             {
66             public boolean matches(EvaluationContext context, ClassInfo classInfo)
67                 throws RulesException
68                 {
69                 return classInfo.getAccessModifier().greaterThanEq(min)
70                     && classInfo.getAccessModifier(). lessThanEq(max);
71                 }
72             };
73         }
74     }
75
Popular Tags