KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > util > Wildcard


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.util;
4
5 /**
6  * Checks whether a string matches a given wildcard pattern.
7  * Possible patterns allow to match single characters ('?') or any count of
8  * characters ('*'). Wildcard characters can be escaped (by an '\').
9  * <p>
10  * This method uses recursive matching, as in linux or windows. regexp works the same.
11  * This method is very fast, comparing to similar implementations.
12  */

13 public class Wildcard {
14
15     /**
16      * Checks whether a string matches a given wildcard pattern.
17      *
18      * @param string input string
19      * @param pattern pattern to match
20      * @return <code>true</code> if string matches the pattern, otherwise <code>false</code>
21      */

22     public static boolean match(String JavaDoc string, String JavaDoc pattern) {
23         return match(string, pattern, 0, 0);
24     }
25
26     /**
27      * Internal matching recursive function.
28      */

29     private static boolean match(String JavaDoc string, String JavaDoc pattern, int stringStartNdx, int patternStartNdx) {
30         int pNdx = patternStartNdx;
31         int sNdx = stringStartNdx;
32         int pLen = pattern.length();
33         int sLen = string.length();
34         boolean nextIsNotWildcard = false;
35
36         while (true) {
37
38             // check if end of string and/or pattern occurred
39
if ((sNdx >= sLen) == true) { // end of string still may have pending '*' in pattern
40
while ((pNdx < pLen) && (pattern.charAt(pNdx) == '*')) {
41                     pNdx++;
42                 }
43                 return pNdx >= pLen;
44             }
45             if (pNdx >= pLen) { // end of pattern, but not end of the string
46
return false;
47             }
48             char p = pattern.charAt(pNdx); // pattern char
49

50             // perform logic
51
if (nextIsNotWildcard == false) {
52
53                 if (p == '\\') {
54                     pNdx++;
55                     nextIsNotWildcard = true;
56                     continue;
57                 }
58                 if (p == '?') {
59                     sNdx++; pNdx++;
60                     continue;
61                 }
62                 if (p == '*') {
63                     char pnext = 0; // next pattern char
64
if (pNdx + 1 < pLen) {
65                         pnext = pattern.charAt(pNdx + 1);
66                     }
67                     if (pnext == '*') { // double '*' have the same effect as one '*'
68
pNdx++;
69                         continue;
70                     }
71                     int i;
72                     pNdx++;
73
74                     // find recursively if there is any substring from the end of the
75
// line that matches the rest of the pattern !!!
76
for (i = string.length(); i >= sNdx; i--) {
77                         if (match(string, pattern, i, pNdx) == true) {
78                             return true;
79                         }
80                     }
81                     return false;
82                 }
83             } else {
84                 nextIsNotWildcard = false;
85             }
86
87             // check if pattern char and string char are equals
88
if (p != string.charAt(sNdx)) {
89                 return false;
90             }
91
92             // everything matches for now, continue
93
sNdx++; pNdx++;
94         }
95     }
96
97
98     // ---------------------------------------------------------------- utilities
99

100     /**
101      * Matches string to at least one pattern.
102      * Returns index of matched pattern, or <code>-1</code> otherwise.
103      * @see #match(String, String)
104      */

105     public static int matchOne(String JavaDoc src, String JavaDoc[] patterns) {
106         for (int i = 0; i < patterns.length; i++) {
107             if (match(src, patterns[i]) == true) {
108                 return i;
109             }
110         }
111         return -1;
112     }
113
114 }
115
Popular Tags