1 |
+ |
/* |
2 |
+ |
* i-scream central monitoring system |
3 |
+ |
* Copyright (C) 2000-2002 i-scream |
4 |
+ |
* |
5 |
+ |
* This program is free software; you can redistribute it and/or |
6 |
+ |
* modify it under the terms of the GNU General Public License |
7 |
+ |
* as published by the Free Software Foundation; either version 2 |
8 |
+ |
* of the License, or (at your option) any later version. |
9 |
+ |
* |
10 |
+ |
* This program is distributed in the hope that it will be useful, |
11 |
+ |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
+ |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
+ |
* GNU General Public License for more details. |
14 |
+ |
* |
15 |
+ |
* You should have received a copy of the GNU General Public License |
16 |
+ |
* along with this program; if not, write to the Free Software |
17 |
+ |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
18 |
+ |
*/ |
19 |
+ |
|
20 |
|
//---PACKAGE DECLARATION--- |
21 |
< |
package uk.ac.ukc.iscream.util; |
21 |
> |
package uk.org.iscream.cms.server.util; |
22 |
|
|
23 |
|
//---IMPORTS--- |
24 |
|
import java.util.*; |
84 |
|
} |
85 |
|
return -1; |
86 |
|
} |
87 |
+ |
|
88 |
+ |
/** |
89 |
+ |
* Checks if a given string matches a |
90 |
+ |
* wildcard expression. For example:<br> |
91 |
+ |
* <br> |
92 |
+ |
* Given "testingstring"<br> |
93 |
+ |
* And "test*ing"<br> |
94 |
+ |
* <br> |
95 |
+ |
* This method would return true as there is a match. |
96 |
+ |
* |
97 |
+ |
* @param in the string to check |
98 |
+ |
* @param expression the wildcard expression to match against |
99 |
+ |
* |
100 |
+ |
* @return if there is a match |
101 |
+ |
*/ |
102 |
+ |
public static boolean wildcardMatch(String in, String expression) { |
103 |
+ |
// check actually has wildcards |
104 |
+ |
if(expression.indexOf("*") != -1) { |
105 |
+ |
StringTokenizer st = new StringTokenizer(expression, "*"); |
106 |
+ |
String part = ""; |
107 |
+ |
int index = 0; |
108 |
+ |
// the first token will be everything at the start |
109 |
+ |
// unless it starts with a wildcard |
110 |
+ |
// -- if it starts with a wildcard the start |
111 |
+ |
// will always match |
112 |
+ |
if(!expression.startsWith("*")) { |
113 |
+ |
part = st.nextToken(); |
114 |
+ |
if (in.startsWith(part)) { |
115 |
+ |
index = part.length(); |
116 |
+ |
} |
117 |
+ |
else { |
118 |
+ |
// doesn't start with the first part |
119 |
+ |
// can't be a match |
120 |
+ |
return false; |
121 |
+ |
} |
122 |
+ |
} |
123 |
+ |
while (st.hasMoreTokens()) { |
124 |
+ |
part = st.nextToken(); |
125 |
+ |
// if the next section can't be matched |
126 |
+ |
// then this isn't in the input string |
127 |
+ |
if(in.substring(index).indexOf(part) == -1) { |
128 |
+ |
// we don't want to look through any more of it |
129 |
+ |
// can't be a match if this part isn't there |
130 |
+ |
return false; |
131 |
+ |
} |
132 |
+ |
else { |
133 |
+ |
// move index pointer to start of the "rest" |
134 |
+ |
// where the rest is everything after what we just matched |
135 |
+ |
index += in.substring(index).indexOf(part) + part.length(); |
136 |
+ |
} |
137 |
+ |
} |
138 |
+ |
// if we reach here and we've found a match |
139 |
+ |
// we want to check that the last part |
140 |
+ |
// of the wildcard string is the last part |
141 |
+ |
// of the input string, if it is, we break out |
142 |
+ |
// and finish as we've found a match. |
143 |
+ |
// if the end of the wildcard is a *, then |
144 |
+ |
// we don't care |
145 |
+ |
if (!expression.endsWith("*")) { |
146 |
+ |
if ((in.endsWith(part))) { |
147 |
+ |
return true; |
148 |
+ |
} |
149 |
+ |
else { |
150 |
+ |
// doesn't end with the first part |
151 |
+ |
// can't be a match |
152 |
+ |
return false; |
153 |
+ |
} |
154 |
+ |
// ends with a *, so it matches the |
155 |
+ |
// end regardless |
156 |
+ |
} |
157 |
+ |
else { |
158 |
+ |
return true; |
159 |
+ |
} |
160 |
+ |
} |
161 |
+ |
else if(in.equals(expression)) { |
162 |
+ |
// might as well do a check if they're the same |
163 |
+ |
// bit daft to call this whole method if they are though :) |
164 |
+ |
return true; |
165 |
+ |
} |
166 |
+ |
else { |
167 |
+ |
// obviously doesn't match if it's not a wildcard |
168 |
+ |
// expression or isn't equal :) |
169 |
+ |
return false; |
170 |
+ |
} |
171 |
+ |
} |
172 |
|
|
173 |
|
//---CONSTRUCTORS--- |
174 |
|
|
178 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
179 |
|
* method to provide clean logging (every class should have this). |
180 |
|
* |
181 |
< |
* This uses the uk.ac.ukc.iscream.util.FormatName class |
181 |
> |
* This uses the uk.org.iscream.cms.server.util.FormatName class |
182 |
|
* to format the toString() |
183 |
|
* |
184 |
|
* @return the name of this class and its CVS revision |