--- projects/cms/source/util/uk/org/iscream/cms/util/StringUtils.java 2001/03/14 23:25:29 1.3
+++ projects/cms/source/util/uk/org/iscream/cms/util/StringUtils.java 2004/08/01 10:41:08 1.10
@@ -1,5 +1,25 @@
+/*
+ * i-scream central monitoring system
+ * http://www.i-scream.org
+ * Copyright (C) 2000-2002 i-scream
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
//---PACKAGE DECLARATION---
-package uk.org.iscream.util;
+package uk.org.iscream.cms.util;
//---IMPORTS---
import java.util.*;
@@ -9,7 +29,7 @@ import java.util.*;
* String objects.
*
* @author $Author: tdb $
- * @version $Id: StringUtils.java,v 1.3 2001/03/14 23:25:29 tdb Exp $
+ * @version $Id: StringUtils.java,v 1.10 2004/08/01 10:41:08 tdb Exp $
*/
public class StringUtils {
@@ -18,7 +38,7 @@ public class StringUtils {
/**
* The current CVS revision of this class
*/
- public static final String REVISION = "$Revision: 1.3 $";
+ public static final String REVISION = "$Revision: 1.10 $";
//---STATIC METHODS---
@@ -65,6 +85,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 +179,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.org.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