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
Revision: 1.7
Committed: Sat May 18 18:16:04 2002 UTC (21 years, 11 months ago) by tdb
Branch: MAIN
Changes since 1.6: +21 -2 lines
Log Message:
i-scream is now licensed under the GPL. I've added the GPL headers to every
source file, and put a full copy of the license in the appropriate places.
I think I've covered everything. This is going to be a mad commit ;)

File Contents

# User Rev Content
1 tdb 1.7 /*
2     * i-scream central monitoring system
3     * Copyright (C) 2000-2002 i-scream
4     *
5     * This program is free software; you can redistribute it and/or
6     * modify it under the terms of the GNU General Public License
7     * as published by the Free Software Foundation; either version 2
8     * of the License, or (at your option) any later version.
9     *
10     * This program is distributed in the hope that it will be useful,
11     * but WITHOUT ANY WARRANTY; without even the implied warranty of
12     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13     * GNU General Public License for more details.
14     *
15     * You should have received a copy of the GNU General Public License
16     * along with this program; if not, write to the Free Software
17     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18     */
19    
20 tdb 1.1 //---PACKAGE DECLARATION---
21 tdb 1.4 package uk.org.iscream.cms.server.util;
22 tdb 1.1
23     //---IMPORTS---
24     import java.util.*;
25    
26     /**
27     * A class containing useful methods for manipulating
28     * String objects.
29     *
30 tdb 1.6 * @author $Author: tdb $
31 tdb 1.7 * @version $Id: StringUtils.java,v 1.6 2001/12/11 17:23:54 tdb Exp $
32 tdb 1.1 */
33     public class StringUtils {
34    
35     //---FINAL ATTRIBUTES---
36    
37     /**
38     * The current CVS revision of this class
39     */
40 tdb 1.7 public static final String REVISION = "$Revision: 1.6 $";
41 tdb 1.1
42     //---STATIC METHODS---
43    
44     /**
45     * Searches a string and replaces all occurences
46     * of the given search text with the given replacement
47     * text.
48     *
49     * @param text the text to search and replace in
50     * @param search the string to look for and replace
51     * @param replace the text to replace with
52     * @return the updated version of text
53     */
54     public static String replaceText(String text, String search, String replace) {
55     StringBuffer textBuffer = new StringBuffer(text);
56     int currIndex = 0;
57     currIndex = text.indexOf(search, currIndex);
58     while(currIndex != -1) {
59     if (currIndex != -1) {
60     textBuffer.delete(currIndex, currIndex + search.length());
61     textBuffer.insert(currIndex, replace);
62     }
63     text = textBuffer.toString();
64     currIndex = text.indexOf(search, currIndex + search.length());
65     }
66     return new String(textBuffer);
67 ajm 1.2 }
68    
69     /**
70     * This method takes an array of String's and a
71     * String to look for and returns the position
72     * in the array that the string occurs.
73     *
74     * @param search the string to look for
75     * @param array the array to look in
76     *
77     * @return the position in the array
78     */
79     public static int getStringPos(String search, String[] array) {
80     for(int x = 0; x < array.length; x++) {
81     if (array[x].equals(search)) {
82     return x;
83     }
84     }
85     return -1;
86 tdb 1.5 }
87    
88     /**
89 tdb 1.6 * Checks if a given string matches a
90     * wildcard expression. For example:<br>
91 tdb 1.5 * <br>
92 tdb 1.6 * Given "testingstring"<br>
93     * And "test*ing"<br>
94 tdb 1.5 * <br>
95 tdb 1.6 * This method would return true as there is a match.
96 tdb 1.5 *
97 tdb 1.6 * @param in the string to check
98     * @param expression the wildcard expression to match against
99 tdb 1.5 *
100     * @return if there is a match
101     */
102 tdb 1.6 public static boolean wildcardMatch(String in, String expression) {
103     // check actually has wildcards
104     if(expression.indexOf("*") != -1) {
105     StringTokenizer st = new StringTokenizer(expression, "*");
106     String part = "";
107     int index = 0;
108     // the first token will be everything at the start
109     // unless it starts with a wildcard
110     // -- if it starts with a wildcard the start
111     // will always match
112     if(!expression.startsWith("*")) {
113     part = st.nextToken();
114     if (in.startsWith(part)) {
115     index = part.length();
116 tdb 1.5 }
117 tdb 1.6 else {
118     // doesn't start with the first part
119     // can't be a match
120     return false;
121     }
122     }
123     while (st.hasMoreTokens()) {
124     part = st.nextToken();
125     // if the next section can't be matched
126     // then this isn't in the input string
127     if(in.substring(index).indexOf(part) == -1) {
128     // we don't want to look through any more of it
129     // can't be a match if this part isn't there
130     return false;
131     }
132     else {
133     // move index pointer to start of the "rest"
134     // where the rest is everything after what we just matched
135     index += in.substring(index).indexOf(part) + part.length();
136     }
137     }
138     // if we reach here and we've found a match
139     // we want to check that the last part
140     // of the wildcard string is the last part
141     // of the input string, if it is, we break out
142     // and finish as we've found a match.
143     // if the end of the wildcard is a *, then
144     // we don't care
145     if (!expression.endsWith("*")) {
146     if ((in.endsWith(part))) {
147     return true;
148     }
149     else {
150     // doesn't end with the first part
151     // can't be a match
152     return false;
153     }
154     // ends with a *, so it matches the
155     // end regardless
156     }
157     else {
158     return true;
159 tdb 1.5 }
160     }
161 tdb 1.6 else if(in.equals(expression)) {
162     // might as well do a check if they're the same
163     // bit daft to call this whole method if they are though :)
164     return true;
165     }
166     else {
167     // obviously doesn't match if it's not a wildcard
168     // expression or isn't equal :)
169     return false;
170     }
171 tdb 1.1 }
172    
173     //---CONSTRUCTORS---
174    
175     //---PUBLIC METHODS---
176    
177     /**
178     * Overrides the {@link java.lang.Object#toString() Object.toString()}
179     * method to provide clean logging (every class should have this).
180     *
181 tdb 1.4 * This uses the uk.org.iscream.cms.server.util.FormatName class
182 tdb 1.1 * to format the toString()
183     *
184     * @return the name of this class and its CVS revision
185     */
186     public String toString() {
187     return FormatName.getName(
188     _name,
189     getClass().getName(),
190     REVISION);
191     }
192    
193     //---PRIVATE METHODS---
194    
195     //---ACCESSOR/MUTATOR METHODS---
196    
197     //---ATTRIBUTES---
198    
199     /**
200     * This is the friendly identifier of the
201     * component this class is running in.
202     * eg, a Filter may be called "filter1",
203     * If this class does not have an owning
204     * component, a name from the configuration
205     * can be placed here. This name could also
206     * be changed to null for utility classes.
207     */
208     private String _name = null;
209    
210     //---STATIC ATTRIBUTES---
211    
212     }