--- projects/cms/source/util/uk/org/iscream/cms/util/StringUtils.java 2001/03/04 02:38:00 1.2
+++ projects/cms/source/util/uk/org/iscream/cms/util/StringUtils.java 2001/12/11 17:23:54 1.6
@@ -1,5 +1,5 @@
//---PACKAGE DECLARATION---
-package uk.ac.ukc.iscream.util;
+package uk.org.iscream.cms.server.util;
//---IMPORTS---
import java.util.*;
@@ -8,8 +8,8 @@ import java.util.*;
* A class containing useful methods for manipulating
* String objects.
*
- * @author $Author: ajm $
- * @version $Id: StringUtils.java,v 1.2 2001/03/04 02:38:00 ajm Exp $
+ * @author $Author: tdb $
+ * @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.2 $";
+ public static final String REVISION = "$Revision: 1.6 $";
//---STATIC METHODS---
@@ -65,6 +65,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---
@@ -74,7 +159,7 @@ public class StringUtils {
* Overrides the {@link java.lang.Object#toString() Object.toString()}
* method to provide clean logging (every class should have this).
*
- * This uses the uk.ac.ukc.iscream.util.FormatName class
+ * This uses the uk.org.iscream.cms.server.util.FormatName class
* to format the toString()
*
* @return the name of this class and its CVS revision