--- projects/cms/source/util/uk/org/iscream/cms/util/StringUtils.java 2001/12/10 22:49:19 1.5
+++ projects/cms/source/util/uk/org/iscream/cms/util/StringUtils.java 2001/12/11 17:23:54 1.6
@@ -9,7 +9,7 @@ import java.util.*;
* String objects.
*
* @author $Author: tdb $
- * @version $Id: StringUtils.java,v 1.5 2001/12/10 22:49:19 tdb Exp $
+ * @version $Id: StringUtils.java,v 1.6 2001/12/11 17:23:54 tdb Exp $
*/
public class StringUtils {
@@ -18,7 +18,7 @@ public class StringUtils {
/**
* The current CVS revision of this class
*/
- public static final String REVISION = "$Revision: 1.5 $";
+ public static final String REVISION = "$Revision: 1.6 $";
//---STATIC METHODS---
@@ -67,80 +67,88 @@ public class StringUtils {
}
/**
- * Checks that a given string is not matched within the
- * given list of strings. For example:
+ * Checks if a given string matches a
+ * wildcard expression. For example:
*
- * Given "stue5de.ukc.ac.uk"
- * And "raptor.ukc.ac.uk;stue*.ukc.ac.uk
+ * Given "testingstring"
+ * And "test*ing"
*
- * This method would return true as there is a match
+ * This method would return true as there is a match.
*
- * @param source the string to look for
- * @param group the group to search for a wilcard entry
+ * @param in the string to check
+ * @param expression the wildcard expression to match against
*
* @return if there is a match
*/
- public static boolean wildcardCheck(String source, String group) {
- boolean foundMatch = false;
- StringTokenizer st = new StringTokenizer(group, ";");
- // go through all the hosts in the group
- while (st.hasMoreTokens()) {
- String host = st.nextToken();
- // this host has wildcards
- if(host.indexOf("*") != -1) {
- StringTokenizer hostst = new StringTokenizer(host, "*");
- String part = "";
- int index = 0;
- // the first token will be everything at the start
- // unless it starts with a wildcard
- if(!host.startsWith("*")) {
- part = hostst.nextToken();
- if (source.startsWith(part)) {
- foundMatch = true;
- index = part.length();
- }
- // all of the start of the string is matched
- } else {
- foundMatch = true;
+ public static boolean wildcardMatch(String in, String expression) {
+ // check actually has wildcards
+ if(expression.indexOf("*") != -1) {
+ StringTokenizer st = new StringTokenizer(expression, "*");
+ String part = "";
+ int index = 0;
+ // the first token will be everything at the start
+ // unless it starts with a wildcard
+ // -- if it starts with a wildcard the start
+ // will always match
+ if(!expression.startsWith("*")) {
+ part = st.nextToken();
+ if (in.startsWith(part)) {
+ index = part.length();
}
- // if the start matched, we want to check the rest...
- if (foundMatch) {
- while (hostst.hasMoreTokens()) {
- part = hostst.nextToken();
- // if the next section can't be matched
- // then this isn't in the source
- if(source.substring(index).indexOf(part) == -1) {
- foundMatch = false;
- // we don't want to look through any more of it
- break;
- } else {
- foundMatch = true;
- index += source.substring(index).indexOf(part) + part.length();
- }
- }
- // if we reach here and we've found a match
- // we want to check that the last part
- // of the wildcard string is the last part
- // of the source, if it is, we break out
- // and finish as we've found a match.
- // if the end of the wildcard is a *, then
- // we don't care
- if (!host.endsWith("*") && foundMatch) {
- if ((source.endsWith(part))) {
- //_logger.write(toString(), Logger.DEBUG, "wildcard match found for - " + source + " in - " + host);
- break;
- // if there is no match, say so so we go round again
- } else {
- foundMatch = false;
- }
- } else if (foundMatch) {
- //_logger.write(toString(), Logger.DEBUG, "wildcard match found for - " + source + " in - " + host);
- break;
- }
+ else {
+ // doesn't start with the first part
+ // can't be a match
+ return false;
}
}
+ while (st.hasMoreTokens()) {
+ part = st.nextToken();
+ // if the next section can't be matched
+ // then this isn't in the input string
+ if(in.substring(index).indexOf(part) == -1) {
+ // we don't want to look through any more of it
+ // can't be a match if this part isn't there
+ return false;
+ }
+ else {
+ // move index pointer to start of the "rest"
+ // where the rest is everything after what we just matched
+ index += in.substring(index).indexOf(part) + part.length();
+ }
+ }
+ // if we reach here and we've found a match
+ // we want to check that the last part
+ // of the wildcard string is the last part
+ // of the input string, if it is, we break out
+ // and finish as we've found a match.
+ // if the end of the wildcard is a *, then
+ // we don't care
+ if (!expression.endsWith("*")) {
+ if ((in.endsWith(part))) {
+ return true;
+ }
+ else {
+ // doesn't end with the first part
+ // can't be a match
+ return false;
+ }
+ // ends with a *, so it matches the
+ // end regardless
+ }
+ else {
+ return true;
+ }
}
- return foundMatch;
+ else if(in.equals(expression)) {
+ // might as well do a check if they're the same
+ // bit daft to call this whole method if they are though :)
+ return true;
+ }
+ else {
+ // obviously doesn't match if it's not a wildcard
+ // expression or isn't equal :)
+ return false;
+ }
}
//---CONSTRUCTORS---