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.org.iscream.cms.server.util; |
22 |
|
|
86 |
|
} |
87 |
|
|
88 |
|
/** |
89 |
< |
* Checks that a given string is not matched within the |
90 |
< |
* given list of strings. For example:<br> |
89 |
> |
* Checks if a given string matches a |
90 |
> |
* wildcard expression. For example:<br> |
91 |
|
* <br> |
92 |
< |
* Given "stue5de.ukc.ac.uk"<br> |
93 |
< |
* And "raptor.ukc.ac.uk;stue*.ukc.ac.uk<br> |
92 |
> |
* Given "testingstring"<br> |
93 |
> |
* And "test*ing"<br> |
94 |
|
* <br> |
95 |
< |
* This method would return true as there is a match |
95 |
> |
* This method would return true as there is a match. |
96 |
|
* |
97 |
< |
* @param source the string to look for |
98 |
< |
* @param group the group to search for a wilcard entry |
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 wildcardCheck(String source, String group) { |
103 |
< |
boolean foundMatch = false; |
104 |
< |
StringTokenizer st = new StringTokenizer(group, ";"); |
105 |
< |
// go through all the hosts in the group |
106 |
< |
while (st.hasMoreTokens()) { |
107 |
< |
String host = st.nextToken(); |
108 |
< |
// this host has wildcards |
109 |
< |
if(host.indexOf("*") != -1) { |
110 |
< |
StringTokenizer hostst = new StringTokenizer(host, "*"); |
111 |
< |
String part = ""; |
112 |
< |
int index = 0; |
113 |
< |
// the first token will be everything at the start |
114 |
< |
// unless it starts with a wildcard |
115 |
< |
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; |
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 |
< |
// if the start matched, we want to check the rest... |
118 |
< |
if (foundMatch) { |
119 |
< |
while (hostst.hasMoreTokens()) { |
120 |
< |
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 |
< |
} |
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 |
< |
return foundMatch; |
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--- |