01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.actions;
11:
12: /**
13: * Implements an algorithm for very simple pattern matching in a string.
14: * There is only one feature: "*" may be used at the start or the end of the
15: * pattern to represent "one or more characters".
16: */
17: public final class SimpleWildcardTester {
18: /**
19: * Returns whether a string matches a particular pattern.
20: *
21: * @param pattern the input pattern
22: * @param str the string to test
23: * @return <code>true</code> if a match occurs; <code>false</code>otherwise.
24: */
25: public static boolean testWildcard(String pattern, String str) {
26: if (pattern.equals("*")) {//$NON-NLS-1$
27: return true;
28: } else if (pattern.startsWith("*")) {//$NON-NLS-1$
29: if (pattern.endsWith("*")) {//$NON-NLS-1$
30: if (pattern.length() == 2) {
31: return true;
32: }
33: return str.indexOf(pattern.substring(1, pattern
34: .length() - 1)) >= 0;
35: }
36: return str.endsWith(pattern.substring(1));
37: } else if (pattern.endsWith("*")) {//$NON-NLS-1$
38: return str.startsWith(pattern.substring(0,
39: pattern.length() - 1));
40: } else {
41: return str.equals(pattern);
42: }
43: }
44:
45: /**
46: * Returns whether a string matches a particular pattern. Both string and
47: * pattern are converted to lower case before pattern matching occurs.
48: *
49: * @param pattern the input pattern
50: * @param str the string to test
51: * @return <code>true</code> if a match occurs; <code>false</code>otherwise.
52: */
53: public static boolean testWildcardIgnoreCase(String pattern,
54: String str) {
55:
56: //If str is null there was no extension to test
57: if (str == null) {
58: return false;
59: }
60: pattern = pattern.toLowerCase();
61: str = str.toLowerCase();
62: return testWildcard(pattern, str);
63: }
64: }
|