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