ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/util/uk/org/iscream/cms/util/StringUtils.java
(Generate patch)

Comparing projects/cms/source/util/uk/org/iscream/cms/util/StringUtils.java (file contents):
Revision 1.2 by ajm, Sun Mar 4 02:38:00 2001 UTC vs.
Revision 1.9 by tdb, Wed Feb 5 14:27:59 2003 UTC

# Line 1 | Line 1
1 + /*
2 + * i-scream central monitoring system
3 + * http://www.i-scream.org.uk
4 + * Copyright (C) 2000-2002 i-scream
5 + *
6 + * This program is free software; you can redistribute it and/or
7 + * modify it under the terms of the GNU General Public License
8 + * as published by the Free Software Foundation; either version 2
9 + * of the License, or (at your option) any later version.
10 + *
11 + * This program is distributed in the hope that it will be useful,
12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 + * GNU General Public License for more details.
15 + *
16 + * You should have received a copy of the GNU General Public License
17 + * along with this program; if not, write to the Free Software
18 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 + */
20 +
21   //---PACKAGE DECLARATION---
22 < package uk.ac.ukc.iscream.util;
22 > package uk.org.iscream.cms.util;
23  
24   //---IMPORTS---
25   import java.util.*;
# Line 65 | Line 85 | public class StringUtils {
85          }
86          return -1;
87      }
88 +    
89 +    /**
90 +     * Checks if a given string matches a
91 +     * wildcard expression. For example:<br>
92 +     * <br>
93 +     * Given "testingstring"<br>
94 +     * And   "test*ing"<br>
95 +     * <br>
96 +     * This method would return true as there is a match.
97 +     *
98 +     * @param in the string to check
99 +     * @param expression the wildcard expression to match against
100 +     *
101 +     * @return if there is a match
102 +     */
103 +    public static boolean wildcardMatch(String in, String expression) {
104 +        // check actually has wildcards
105 +        if(expression.indexOf("*") != -1) {
106 +            StringTokenizer st = new StringTokenizer(expression, "*");
107 +            String part = "";
108 +            int index = 0;
109 +            // the first token will be everything at the start
110 +            // unless it starts with a wildcard
111 +            // -- if it starts with a wildcard the start
112 +            //    will always match
113 +            if(!expression.startsWith("*")) {
114 +                part = st.nextToken();
115 +                if (in.startsWith(part)) {
116 +                    index = part.length();
117 +                }
118 +                else {
119 +                    // doesn't start with the first part
120 +                    // can't be a match
121 +                    return false;
122 +                }
123 +            }
124 +            while (st.hasMoreTokens()) {
125 +                part = st.nextToken();
126 +                // if the next section can't be matched
127 +                // then this isn't in the input string
128 +                if(in.substring(index).indexOf(part) == -1) {
129 +                    // we don't want to look through any more of it
130 +                    // can't be a match if this part isn't there
131 +                    return false;
132 +                }
133 +                else {
134 +                    // move index pointer to start of the "rest"
135 +                    // where the rest is everything after what we just matched
136 +                    index += in.substring(index).indexOf(part) + part.length();
137 +                }
138 +            }
139 +            // if we reach here and we've found a match
140 +            // we want to check that the last part
141 +            // of the wildcard string is the last part
142 +            // of the input string, if it is, we break out
143 +            // and finish as we've found a match.
144 +            // if the end of the wildcard is a *, then
145 +            // we don't care
146 +            if (!expression.endsWith("*")) {
147 +                if ((in.endsWith(part))) {
148 +                    return true;
149 +                }
150 +                else {
151 +                    // doesn't end with the first part
152 +                    // can't be a match
153 +                    return false;
154 +                }
155 +            // ends with a *, so it matches the
156 +            // end regardless
157 +            }
158 +            else {
159 +                return true;
160 +            }
161 +        }
162 +        else if(in.equals(expression)) {
163 +            // might as well do a check if they're the same
164 +            // bit daft to call this whole method if they are though :)
165 +            return true;
166 +        }
167 +        else {
168 +            // obviously doesn't match if it's not a wildcard
169 +            // expression or isn't equal :)
170 +            return false;
171 +        }
172 +    }
173  
174   //---CONSTRUCTORS---
175  
# Line 74 | Line 179 | public class StringUtils {
179       * Overrides the {@link java.lang.Object#toString() Object.toString()}
180       * method to provide clean logging (every class should have this).
181       *
182 <     * This uses the uk.ac.ukc.iscream.util.FormatName class
182 >     * This uses the uk.org.iscream.cms.server.util.FormatName class
183       * to format the toString()
184       *
185       * @return the name of this class and its CVS revision

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines