KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > methodmatch > MethodPatternParser


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.methodmatch;
16
17 import java.util.ArrayList JavaDoc;
18 import java.util.List JavaDoc;
19
20 import org.apache.hivemind.ApplicationRuntimeException;
21 import org.apache.hivemind.util.StringUtils;
22
23 /**
24  * Parses a method pattern (consisting of a name pattern, followed by an optional parameters
25  * pattern) into a {@link org.apache.hivemind.methodmatch.MethodFilter}. In most cases, the
26  * patterns will require several checks (i.e., match against name, match against parameters) in
27  * which case a {@link org.apache.hivemind.methodmatch.CompositeFilter} is returned.
28  *
29  * @author Howard Lewis Ship
30  */

31
32 public class MethodPatternParser
33 {
34     private List JavaDoc _filters;
35
36     public MethodFilter parseMethodPattern(String JavaDoc pattern)
37     {
38         _filters = new ArrayList JavaDoc();
39
40         int parenx = pattern.indexOf('(');
41
42         String JavaDoc namePattern = parenx < 0 ? pattern : pattern.substring(0, parenx);
43
44         parseNamePattern(pattern, namePattern);
45
46         if (parenx >= 0)
47             parseParametersPattern(pattern, pattern.substring(parenx));
48
49         switch (_filters.size())
50         {
51             case 0:
52                 return new MatchAllFilter();
53
54             case 1:
55
56                 return (MethodFilter) _filters.get(0);
57
58             default:
59                 return new CompositeFilter(_filters);
60         }
61     }
62
63     private void parseNamePattern(String JavaDoc methodPattern, String JavaDoc namePattern)
64     {
65         if (namePattern.equals("*"))
66             return;
67
68         if (namePattern.length() == 0)
69             throw new ApplicationRuntimeException(MethodMatchMessages
70                     .missingNamePattern(methodPattern));
71
72         if (namePattern.startsWith("*") && namePattern.endsWith("*"))
73         {
74             String JavaDoc substring = namePattern.substring(1, namePattern.length() - 1);
75
76             validateNamePattern(methodPattern, substring);
77
78             _filters.add(new InfixNameFilter(substring));
79             return;
80         }
81
82         if (namePattern.startsWith("*"))
83         {
84             String JavaDoc suffix = namePattern.substring(1);
85
86             validateNamePattern(methodPattern, suffix);
87
88             _filters.add(new NameSuffixFilter(suffix));
89             return;
90         }
91
92         if (namePattern.endsWith("*"))
93         {
94             String JavaDoc prefix = namePattern.substring(0, namePattern.length() - 1);
95
96             validateNamePattern(methodPattern, prefix);
97
98             _filters.add(new NamePrefixFilter(prefix));
99             return;
100         }
101
102         validateNamePattern(methodPattern, namePattern);
103
104         _filters.add(new ExactNameFilter(namePattern));
105     }
106
107     private void parseParametersPattern(String JavaDoc methodPattern, String JavaDoc pattern)
108     {
109         if (pattern.equals("()"))
110         {
111             addParameterCountFilter(0);
112             return;
113         }
114
115         if (!pattern.endsWith(")"))
116             throw new ApplicationRuntimeException(MethodMatchMessages
117                     .invalidParametersPattern(methodPattern));
118
119         // Trim off leading and trailing parens.
120

121         pattern = pattern.substring(1, pattern.length() - 1);
122
123         char ch = pattern.charAt(0);
124
125         if (Character.isDigit(ch))
126         {
127             addParameterCountFilter(methodPattern, pattern);
128             return;
129         }
130
131         String JavaDoc[] names = StringUtils.split(pattern);
132
133         // Would be nice to do some kind of validation here, to prove
134
// that the provided class names exist, and that
135
// primitive types names are valid.
136

137         addParameterCountFilter(names.length);
138         for (int i = 0; i < names.length; i++)
139             _filters.add(new ParameterFilter(i, names[i].trim()));
140
141     }
142
143     private void addParameterCountFilter(String JavaDoc methodPattern, String JavaDoc pattern)
144     {
145         try
146         {
147             int count = Integer.parseInt(pattern);
148             addParameterCountFilter(count);
149         }
150         catch (NumberFormatException JavaDoc ex)
151         {
152             throw new ApplicationRuntimeException(MethodMatchMessages
153                     .invalidParametersPattern(methodPattern));
154         }
155     }
156
157     private void addParameterCountFilter(int count)
158     {
159         // Add the count filter first, since it is always the least expensive test.
160
_filters.add(0, new ParameterCountFilter(count));
161     }
162
163     private void validateNamePattern(String JavaDoc methodPattern, String JavaDoc nameSubstring)
164     {
165         if (nameSubstring.indexOf('*') >= 0)
166             throw new ApplicationRuntimeException(MethodMatchMessages
167                     .invalidNamePattern(methodPattern));
168     }
169 }
Popular Tags