67 |
|
} |
68 |
|
|
69 |
|
/** |
70 |
< |
* Checks that a given string is not matched within the |
71 |
< |
* given list of strings. For example:<br> |
70 |
> |
* Checks if a given string matches a |
71 |
> |
* wildcard expression. For example:<br> |
72 |
|
* <br> |
73 |
< |
* Given "stue5de.ukc.ac.uk"<br> |
74 |
< |
* And "raptor.ukc.ac.uk;stue*.ukc.ac.uk<br> |
73 |
> |
* Given "testingstring"<br> |
74 |
> |
* And "test*ing"<br> |
75 |
|
* <br> |
76 |
< |
* This method would return true as there is a match |
76 |
> |
* This method would return true as there is a match. |
77 |
|
* |
78 |
< |
* @param source the string to look for |
79 |
< |
* @param group the group to search for a wilcard entry |
78 |
> |
* @param in the string to check |
79 |
> |
* @param expression the wildcard expression to match against |
80 |
|
* |
81 |
|
* @return if there is a match |
82 |
|
*/ |
83 |
< |
public static boolean wildcardCheck(String source, String group) { |
84 |
< |
boolean foundMatch = false; |
85 |
< |
StringTokenizer st = new StringTokenizer(group, ";"); |
86 |
< |
// go through all the hosts in the group |
87 |
< |
while (st.hasMoreTokens()) { |
88 |
< |
String host = st.nextToken(); |
89 |
< |
// this host has wildcards |
90 |
< |
if(host.indexOf("*") != -1) { |
91 |
< |
StringTokenizer hostst = new StringTokenizer(host, "*"); |
92 |
< |
String part = ""; |
93 |
< |
int index = 0; |
94 |
< |
// the first token will be everything at the start |
95 |
< |
// unless it starts with a wildcard |
96 |
< |
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; |
83 |
> |
public static boolean wildcardMatch(String in, String expression) { |
84 |
> |
// check actually has wildcards |
85 |
> |
if(expression.indexOf("*") != -1) { |
86 |
> |
StringTokenizer st = new StringTokenizer(expression, "*"); |
87 |
> |
String part = ""; |
88 |
> |
int index = 0; |
89 |
> |
// the first token will be everything at the start |
90 |
> |
// unless it starts with a wildcard |
91 |
> |
// -- if it starts with a wildcard the start |
92 |
> |
// will always match |
93 |
> |
if(!expression.startsWith("*")) { |
94 |
> |
part = st.nextToken(); |
95 |
> |
if (in.startsWith(part)) { |
96 |
> |
index = part.length(); |
97 |
|
} |
98 |
< |
// if the start matched, we want to check the rest... |
99 |
< |
if (foundMatch) { |
100 |
< |
while (hostst.hasMoreTokens()) { |
101 |
< |
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 |
< |
} |
98 |
> |
else { |
99 |
> |
// doesn't start with the first part |
100 |
> |
// can't be a match |
101 |
> |
return false; |
102 |
|
} |
103 |
|
} |
104 |
+ |
while (st.hasMoreTokens()) { |
105 |
+ |
part = st.nextToken(); |
106 |
+ |
// if the next section can't be matched |
107 |
+ |
// then this isn't in the input string |
108 |
+ |
if(in.substring(index).indexOf(part) == -1) { |
109 |
+ |
// we don't want to look through any more of it |
110 |
+ |
// can't be a match if this part isn't there |
111 |
+ |
return false; |
112 |
+ |
} |
113 |
+ |
else { |
114 |
+ |
// move index pointer to start of the "rest" |
115 |
+ |
// where the rest is everything after what we just matched |
116 |
+ |
index += in.substring(index).indexOf(part) + part.length(); |
117 |
+ |
} |
118 |
+ |
} |
119 |
+ |
// if we reach here and we've found a match |
120 |
+ |
// we want to check that the last part |
121 |
+ |
// of the wildcard string is the last part |
122 |
+ |
// of the input string, if it is, we break out |
123 |
+ |
// and finish as we've found a match. |
124 |
+ |
// if the end of the wildcard is a *, then |
125 |
+ |
// we don't care |
126 |
+ |
if (!expression.endsWith("*")) { |
127 |
+ |
if ((in.endsWith(part))) { |
128 |
+ |
return true; |
129 |
+ |
} |
130 |
+ |
else { |
131 |
+ |
// doesn't end with the first part |
132 |
+ |
// can't be a match |
133 |
+ |
return false; |
134 |
+ |
} |
135 |
+ |
// ends with a *, so it matches the |
136 |
+ |
// end regardless |
137 |
+ |
} |
138 |
+ |
else { |
139 |
+ |
return true; |
140 |
+ |
} |
141 |
|
} |
142 |
< |
return foundMatch; |
142 |
> |
else if(in.equals(expression)) { |
143 |
> |
// might as well do a check if they're the same |
144 |
> |
// bit daft to call this whole method if they are though :) |
145 |
> |
return true; |
146 |
> |
} |
147 |
> |
else { |
148 |
> |
// obviously doesn't match if it's not a wildcard |
149 |
> |
// expression or isn't equal :) |
150 |
> |
return false; |
151 |
> |
} |
152 |
|
} |
153 |
|
|
154 |
|
//---CONSTRUCTORS--- |