01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.springframework.util;
18:
19: /**
20: * Utility methods for simple pattern matching, in particular for
21: * Spring's typical "xxx*", "*xxx" and "*xxx*" pattern styles.
22: *
23: * @author Juergen Hoeller
24: * @since 2.0
25: */
26: public abstract class PatternMatchUtils {
27:
28: /**
29: * Match a String against the given pattern, supporting the following simple
30: * pattern styles: "xxx*", "*xxx", "*xxx*" and "xxx*yyy" matches (with an
31: * arbitrary number of pattern parts), as well as direct equality.
32: * @param pattern the pattern to match against
33: * @param str the String to match
34: * @return whether the String matches the given pattern
35: */
36: public static boolean simpleMatch(String pattern, String str) {
37: if (pattern == null || str == null) {
38: return false;
39: }
40: int firstIndex = pattern.indexOf('*');
41: if (firstIndex == -1) {
42: return pattern.equals(str);
43: }
44: if (firstIndex == 0) {
45: if (pattern.length() == 1) {
46: return true;
47: }
48: int nextIndex = pattern.indexOf('*', firstIndex + 1);
49: if (nextIndex == -1) {
50: return str.endsWith(pattern.substring(1));
51: }
52: String part = pattern.substring(1, nextIndex);
53: int partIndex = str.indexOf(part);
54: while (partIndex != -1) {
55: if (simpleMatch(pattern.substring(nextIndex), str
56: .substring(partIndex + part.length()))) {
57: return true;
58: }
59: partIndex = str.indexOf(part, partIndex + 1);
60: }
61: return false;
62: }
63: return (str.length() >= firstIndex
64: && pattern.substring(0, firstIndex).equals(
65: str.substring(0, firstIndex)) && simpleMatch(
66: pattern.substring(firstIndex), str
67: .substring(firstIndex)));
68: }
69:
70: /**
71: * Match a String against the given patterns, supporting the following simple
72: * pattern styles: "xxx*", "*xxx", "*xxx*" and "xxx*yyy" matches (with an
73: * arbitrary number of pattern parts), as well as direct equality.
74: * @param patterns the patterns to match against
75: * @param str the String to match
76: * @return whether the String matches any of the given patterns
77: */
78: public static boolean simpleMatch(String[] patterns, String str) {
79: if (patterns != null) {
80: for (int i = 0; i < patterns.length; i++) {
81: if (simpleMatch(patterns[i], str)) {
82: return true;
83: }
84: }
85: }
86: return false;
87: }
88:
89: }
|