01: // Copyright 2006, 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // 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.tapestry.ioc.internal;
16:
17: import static org.apache.tapestry.ioc.internal.MatchType.ANY;
18: import static org.apache.tapestry.ioc.internal.MatchType.INFIX;
19: import static org.apache.tapestry.ioc.internal.MatchType.PREFIX;
20: import static org.apache.tapestry.ioc.internal.MatchType.SUFFIX;
21:
22: public class GlobPatternMatcher {
23: private String _substring;
24:
25: private MatchType _type;
26:
27: public GlobPatternMatcher(String pattern) {
28: analyze(pattern);
29: }
30:
31: private void analyze(String pattern) {
32: if (pattern.equals("*")) {
33: _type = ANY;
34: return;
35: }
36:
37: boolean globPrefix = pattern.startsWith("*");
38: boolean globSuffix = pattern.endsWith("*");
39:
40: if (globPrefix && globSuffix) {
41: _substring = pattern.substring(1, pattern.length() - 1);
42: _type = INFIX;
43: return;
44: }
45:
46: if (globPrefix) {
47: _substring = pattern.substring(1);
48: _type = SUFFIX;
49: return;
50: }
51:
52: if (globSuffix) {
53: _substring = pattern.substring(0, pattern.length() - 1);
54: _type = PREFIX;
55: return;
56: }
57:
58: _type = MatchType.EXACT;
59: _substring = pattern;
60: }
61:
62: public boolean matches(String input) {
63: switch (_type) {
64: case ANY:
65: return true;
66:
67: case EXACT:
68:
69: return input.equalsIgnoreCase(_substring);
70:
71: case INFIX:
72:
73: return input.toLowerCase().contains(
74: _substring.toLowerCase());
75:
76: case PREFIX:
77:
78: return input.regionMatches(true, 0, _substring, 0,
79: _substring.length());
80:
81: default:
82:
83: return input.regionMatches(true, input.length()
84: - _substring.length(), _substring, 0, _substring
85: .length());
86: }
87: }
88: }
|