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.9
Committed: Wed Feb 5 14:27:59 2003 UTC (21 years, 2 months ago) by tdb
Branch: MAIN
Changes since 1.8: +3 -3 lines
Log Message:
Util package has been pulled out of the server. Next step will be to modify
the server and conient (and anything else?) to use this instead. New
package name is uk.org.iscream.cms.util. All the java files were moved with
a repo copy, so they retain their history.

File Contents

# User Rev Content
1 tdb 1.7 /*
2     * i-scream central monitoring system
3 tdb 1.8 * http://www.i-scream.org.uk
4 tdb 1.7 * 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 tdb 1.1 //---PACKAGE DECLARATION---
22 tdb 1.9 package uk.org.iscream.cms.util;
23 tdb 1.1
24     //---IMPORTS---
25     import java.util.*;
26    
27     /**
28     * A class containing useful methods for manipulating
29     * String objects.
30     *
31 tdb 1.6 * @author $Author: tdb $
32 tdb 1.9 * @version $Id: StringUtils.java,v 1.8 2002/05/21 16:47:20 tdb Exp $
33 tdb 1.1 */
34     public class StringUtils {
35    
36     //---FINAL ATTRIBUTES---
37    
38     /**
39     * The current CVS revision of this class
40     */
41 tdb 1.9 public static final String REVISION = "$Revision: 1.8 $";
42 tdb 1.1
43     //---STATIC METHODS---
44    
45     /**
46     * Searches a string and replaces all occurences
47     * of the given search text with the given replacement
48     * text.
49     *
50     * @param text the text to search and replace in
51     * @param search the string to look for and replace
52     * @param replace the text to replace with
53     * @return the updated version of text
54     */
55     public static String replaceText(String text, String search, String replace) {
56     StringBuffer textBuffer = new StringBuffer(text);
57     int currIndex = 0;
58     currIndex = text.indexOf(search, currIndex);
59     while(currIndex != -1) {
60     if (currIndex != -1) {
61     textBuffer.delete(currIndex, currIndex + search.length());
62     textBuffer.insert(currIndex, replace);
63     }
64     text = textBuffer.toString();
65     currIndex = text.indexOf(search, currIndex + search.length());
66     }
67     return new String(textBuffer);
68 ajm 1.2 }
69    
70     /**
71     * This method takes an array of String's and a
72     * String to look for and returns the position
73     * in the array that the string occurs.
74     *
75     * @param search the string to look for
76     * @param array the array to look in
77     *
78     * @return the position in the array
79     */
80     public static int getStringPos(String search, String[] array) {
81     for(int x = 0; x < array.length; x++) {
82     if (array[x].equals(search)) {
83     return x;
84     }
85     }
86     return -1;
87 tdb 1.5 }
88    
89     /**
90 tdb 1.6 * Checks if a given string matches a
91     * wildcard expression. For example:<br>
92 tdb 1.5 * <br>
93 tdb 1.6 * Given "testingstring"<br>
94     * And "test*ing"<br>
95 tdb 1.5 * <br>
96 tdb 1.6 * This method would return true as there is a match.
97 tdb 1.5 *
98 tdb 1.6 * @param in the string to check
99     * @param expression the wildcard expression to match against
100 tdb 1.5 *
101     * @return if there is a match
102     */
103 tdb 1.6 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 tdb 1.5 }
118 tdb 1.6 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 tdb 1.5 }
161     }
162 tdb 1.6 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 tdb 1.1 }
173    
174     //---CONSTRUCTORS---
175    
176     //---PUBLIC METHODS---
177    
178     /**
179     * Overrides the {@link java.lang.Object#toString() Object.toString()}
180     * method to provide clean logging (every class should have this).
181     *
182 tdb 1.4 * This uses the uk.org.iscream.cms.server.util.FormatName class
183 tdb 1.1 * to format the toString()
184     *
185     * @return the name of this class and its CVS revision
186     */
187     public String toString() {
188     return FormatName.getName(
189     _name,
190     getClass().getName(),
191     REVISION);
192     }
193    
194     //---PRIVATE METHODS---
195    
196     //---ACCESSOR/MUTATOR METHODS---
197    
198     //---ATTRIBUTES---
199    
200     /**
201     * This is the friendly identifier of the
202     * component this class is running in.
203     * eg, a Filter may be called "filter1",
204     * If this class does not have an owning
205     * component, a name from the configuration
206     * can be placed here. This name could also
207     * be changed to null for utility classes.
208     */
209     private String _name = null;
210    
211     //---STATIC ATTRIBUTES---
212    
213     }