--- 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/11 17:23:54 1.6 @@ -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.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.4 $"; + public static final String REVISION = "$Revision: 1.6 $"; //---STATIC METHODS--- @@ -64,6 +64,91 @@ public class StringUtils { } } return -1; + } + + /** + * Checks if a given string matches a + * wildcard expression. For example:
+ *
+ * Given "testingstring"
+ * And "test*ing"
+ *
+ * This method would return true as there is a match. + * + * @param in the string to check + * @param expression the wildcard expression to match against + * + * @return if there is a match + */ + 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(); + } + 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; + } + } + 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---