--- projects/cms/source/util/uk/org/iscream/cms/util/StringUtils.java 2001/05/29 17:02:35 1.4 +++ projects/cms/source/util/uk/org/iscream/cms/util/StringUtils.java 2001/12/10 22:49:19 1.5 @@ -9,7 +9,7 @@ import java.util.*; * String objects. * * @author $Author: tdb $ - * @version $Id: StringUtils.java,v 1.4 2001/05/29 17:02:35 tdb Exp $ + * @version $Id: StringUtils.java,v 1.5 2001/12/10 22:49:19 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.4 $"; + public static final String REVISION = "$Revision: 1.5 $"; //---STATIC METHODS--- @@ -64,6 +64,83 @@ public class StringUtils { } } return -1; + } + + /** + * Checks that a given string is not matched within the + * given list of strings. For example:
+ *
+ * Given "stue5de.ukc.ac.uk"
+ * And "raptor.ukc.ac.uk;stue*.ukc.ac.uk
+ *
+ * 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 + * + * @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; + } + // 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; + } + } + } + } + return foundMatch; } //---CONSTRUCTORS---