|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| package org.apache.hivemind.methodmatch; |
|
16 |
| |
|
17 |
| import java.util.ArrayList; |
|
18 |
| import java.util.List; |
|
19 |
| |
|
20 |
| import org.apache.hivemind.ApplicationRuntimeException; |
|
21 |
| import org.apache.hivemind.util.StringUtils; |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| public class MethodPatternParser |
|
33 |
| { |
|
34 |
| private List _filters; |
|
35 |
| |
|
36 |
20
| public MethodFilter parseMethodPattern(String pattern)
|
|
37 |
| { |
|
38 |
20
| _filters = new ArrayList();
|
|
39 |
| |
|
40 |
20
| int parenx = pattern.indexOf('(');
|
|
41 |
| |
|
42 |
20
| String namePattern = parenx < 0 ? pattern : pattern.substring(0, parenx);
|
|
43 |
| |
|
44 |
20
| parseNamePattern(pattern, namePattern);
|
|
45 |
| |
|
46 |
18
| if (parenx >= 0)
|
|
47 |
9
| parseParametersPattern(pattern, pattern.substring(parenx));
|
|
48 |
| |
|
49 |
14
| switch (_filters.size())
|
|
50 |
| { |
|
51 |
2
| case 0:
|
|
52 |
2
| return new MatchAllFilter();
|
|
53 |
| |
|
54 |
9
| case 1:
|
|
55 |
| |
|
56 |
9
| return (MethodFilter) _filters.get(0);
|
|
57 |
| |
|
58 |
3
| default:
|
|
59 |
3
| return new CompositeFilter(_filters);
|
|
60 |
| } |
|
61 |
| } |
|
62 |
| |
|
63 |
20
| private void parseNamePattern(String methodPattern, String namePattern)
|
|
64 |
| { |
|
65 |
20
| if (namePattern.equals("*"))
|
|
66 |
8
| return;
|
|
67 |
| |
|
68 |
12
| if (namePattern.length() == 0)
|
|
69 |
1
| throw new ApplicationRuntimeException(MethodMatchMessages
|
|
70 |
| .missingNamePattern(methodPattern)); |
|
71 |
| |
|
72 |
11
| if (namePattern.startsWith("*") && namePattern.endsWith("*"))
|
|
73 |
| { |
|
74 |
2
| String substring = namePattern.substring(1, namePattern.length() - 1);
|
|
75 |
| |
|
76 |
2
| validateNamePattern(methodPattern, substring);
|
|
77 |
| |
|
78 |
1
| _filters.add(new InfixNameFilter(substring));
|
|
79 |
1
| return;
|
|
80 |
| } |
|
81 |
| |
|
82 |
9
| if (namePattern.startsWith("*"))
|
|
83 |
| { |
|
84 |
2
| String suffix = namePattern.substring(1);
|
|
85 |
| |
|
86 |
2
| validateNamePattern(methodPattern, suffix);
|
|
87 |
| |
|
88 |
2
| _filters.add(new NameSuffixFilter(suffix));
|
|
89 |
2
| return;
|
|
90 |
| } |
|
91 |
| |
|
92 |
7
| if (namePattern.endsWith("*"))
|
|
93 |
| { |
|
94 |
3
| String prefix = namePattern.substring(0, namePattern.length() - 1);
|
|
95 |
| |
|
96 |
3
| validateNamePattern(methodPattern, prefix);
|
|
97 |
| |
|
98 |
3
| _filters.add(new NamePrefixFilter(prefix));
|
|
99 |
3
| return;
|
|
100 |
| } |
|
101 |
| |
|
102 |
4
| validateNamePattern(methodPattern, namePattern);
|
|
103 |
| |
|
104 |
4
| _filters.add(new ExactNameFilter(namePattern));
|
|
105 |
| } |
|
106 |
| |
|
107 |
9
| private void parseParametersPattern(String methodPattern, String pattern)
|
|
108 |
| { |
|
109 |
9
| if (pattern.equals("()"))
|
|
110 |
| { |
|
111 |
1
| addParameterCountFilter(0);
|
|
112 |
1
| return;
|
|
113 |
| } |
|
114 |
| |
|
115 |
8
| if (!pattern.endsWith(")"))
|
|
116 |
3
| throw new ApplicationRuntimeException(MethodMatchMessages
|
|
117 |
| .invalidParametersPattern(methodPattern)); |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
5
| pattern = pattern.substring(1, pattern.length() - 1);
|
|
122 |
| |
|
123 |
5
| char ch = pattern.charAt(0);
|
|
124 |
| |
|
125 |
5
| if (Character.isDigit(ch))
|
|
126 |
| { |
|
127 |
2
| addParameterCountFilter(methodPattern, pattern);
|
|
128 |
1
| return;
|
|
129 |
| } |
|
130 |
| |
|
131 |
3
| String[] names = StringUtils.split(pattern);
|
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
3
| addParameterCountFilter(names.length);
|
|
138 |
3
| for (int i = 0; i < names.length; i++)
|
|
139 |
4
| _filters.add(new ParameterFilter(i, names[i].trim()));
|
|
140 |
| |
|
141 |
| } |
|
142 |
| |
|
143 |
2
| private void addParameterCountFilter(String methodPattern, String pattern)
|
|
144 |
| { |
|
145 |
2
| try
|
|
146 |
| { |
|
147 |
2
| int count = Integer.parseInt(pattern);
|
|
148 |
1
| addParameterCountFilter(count);
|
|
149 |
| } |
|
150 |
| catch (NumberFormatException ex) |
|
151 |
| { |
|
152 |
1
| throw new ApplicationRuntimeException(MethodMatchMessages
|
|
153 |
| .invalidParametersPattern(methodPattern)); |
|
154 |
| } |
|
155 |
| } |
|
156 |
| |
|
157 |
5
| private void addParameterCountFilter(int count)
|
|
158 |
| { |
|
159 |
| |
|
160 |
5
| _filters.add(0, new ParameterCountFilter(count));
|
|
161 |
| } |
|
162 |
| |
|
163 |
11
| private void validateNamePattern(String methodPattern, String nameSubstring)
|
|
164 |
| { |
|
165 |
11
| if (nameSubstring.indexOf('*') >= 0)
|
|
166 |
1
| throw new ApplicationRuntimeException(MethodMatchMessages
|
|
167 |
| .invalidNamePattern(methodPattern)); |
|
168 |
| } |
|
169 |
| } |