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.5
Committed: Mon Dec 10 22:49:19 2001 UTC (22 years, 4 months ago) by tdb
Branch: MAIN
Changes since 1.4: +79 -2 lines
Log Message:
Pulled out the wildcardCheck method because it's rather more generic than
it's use in the ConfigurationManagerServant. I still need to tidy some of
the comments and variable names to abstract it a bit more, and I might also
remove it taking a semi-colon seperated list - that's more behaviour of the
configuration system. It's a start, and I'll remember to do the rest when I
read this commit message tomorrow :)

File Contents

# User Rev Content
1 tdb 1.1 //---PACKAGE DECLARATION---
2 tdb 1.4 package uk.org.iscream.cms.server.util;
3 tdb 1.1
4     //---IMPORTS---
5     import java.util.*;
6    
7     /**
8     * A class containing useful methods for manipulating
9     * String objects.
10     *
11 tdb 1.4 * @author $Author: tdb1 $
12 tdb 1.5 * @version $Id: StringUtils.java,v 1.4 2001/05/29 17:02:35 tdb1 Exp $
13 tdb 1.1 */
14     public class StringUtils {
15    
16     //---FINAL ATTRIBUTES---
17    
18     /**
19     * The current CVS revision of this class
20     */
21 tdb 1.5 public static final String REVISION = "$Revision: 1.4 $";
22 tdb 1.1
23     //---STATIC METHODS---
24    
25     /**
26     * Searches a string and replaces all occurences
27     * of the given search text with the given replacement
28     * text.
29     *
30     * @param text the text to search and replace in
31     * @param search the string to look for and replace
32     * @param replace the text to replace with
33     * @return the updated version of text
34     */
35     public static String replaceText(String text, String search, String replace) {
36     StringBuffer textBuffer = new StringBuffer(text);
37     int currIndex = 0;
38     currIndex = text.indexOf(search, currIndex);
39     while(currIndex != -1) {
40     if (currIndex != -1) {
41     textBuffer.delete(currIndex, currIndex + search.length());
42     textBuffer.insert(currIndex, replace);
43     }
44     text = textBuffer.toString();
45     currIndex = text.indexOf(search, currIndex + search.length());
46     }
47     return new String(textBuffer);
48 ajm 1.2 }
49    
50     /**
51     * This method takes an array of String's and a
52     * String to look for and returns the position
53     * in the array that the string occurs.
54     *
55     * @param search the string to look for
56     * @param array the array to look in
57     *
58     * @return the position in the array
59     */
60     public static int getStringPos(String search, String[] array) {
61     for(int x = 0; x < array.length; x++) {
62     if (array[x].equals(search)) {
63     return x;
64     }
65     }
66     return -1;
67 tdb 1.5 }
68    
69     /**
70     * Checks that a given string is not matched within the
71     * given list of strings. For example:<br>
72     * <br>
73     * Given "stue5de.ukc.ac.uk"<br>
74     * And "raptor.ukc.ac.uk;stue*.ukc.ac.uk<br>
75     * <br>
76     * This method would return true as there is a match
77     *
78     * @param source the string to look for
79     * @param group the group to search for a wilcard entry
80     *
81     * @return if there is a match
82     */
83     public static boolean wildcardCheck(String source, String group) {
84     boolean foundMatch = false;
85     StringTokenizer st = new StringTokenizer(group, ";");
86     // go through all the hosts in the group
87     while (st.hasMoreTokens()) {
88     String host = st.nextToken();
89     // this host has wildcards
90     if(host.indexOf("*") != -1) {
91     StringTokenizer hostst = new StringTokenizer(host, "*");
92     String part = "";
93     int index = 0;
94     // the first token will be everything at the start
95     // unless it starts with a wildcard
96     if(!host.startsWith("*")) {
97     part = hostst.nextToken();
98     if (source.startsWith(part)) {
99     foundMatch = true;
100     index = part.length();
101     }
102     // all of the start of the string is matched
103     } else {
104     foundMatch = true;
105     }
106     // if the start matched, we want to check the rest...
107     if (foundMatch) {
108     while (hostst.hasMoreTokens()) {
109     part = hostst.nextToken();
110     // if the next section can't be matched
111     // then this isn't in the source
112     if(source.substring(index).indexOf(part) == -1) {
113     foundMatch = false;
114     // we don't want to look through any more of it
115     break;
116     } else {
117     foundMatch = true;
118     index += source.substring(index).indexOf(part) + part.length();
119     }
120     }
121     // if we reach here and we've found a match
122     // we want to check that the last part
123     // of the wildcard string is the last part
124     // of the source, if it is, we break out
125     // and finish as we've found a match.
126     // if the end of the wildcard is a *, then
127     // we don't care
128     if (!host.endsWith("*") && foundMatch) {
129     if ((source.endsWith(part))) {
130     //_logger.write(toString(), Logger.DEBUG, "wildcard match found for - " + source + " in - " + host);
131     break;
132     // if there is no match, say so so we go round again
133     } else {
134     foundMatch = false;
135     }
136     } else if (foundMatch) {
137     //_logger.write(toString(), Logger.DEBUG, "wildcard match found for - " + source + " in - " + host);
138     break;
139     }
140     }
141     }
142     }
143     return foundMatch;
144 tdb 1.1 }
145    
146     //---CONSTRUCTORS---
147    
148     //---PUBLIC METHODS---
149    
150     /**
151     * Overrides the {@link java.lang.Object#toString() Object.toString()}
152     * method to provide clean logging (every class should have this).
153     *
154 tdb 1.4 * This uses the uk.org.iscream.cms.server.util.FormatName class
155 tdb 1.1 * to format the toString()
156     *
157     * @return the name of this class and its CVS revision
158     */
159     public String toString() {
160     return FormatName.getName(
161     _name,
162     getClass().getName(),
163     REVISION);
164     }
165    
166     //---PRIVATE METHODS---
167    
168     //---ACCESSOR/MUTATOR METHODS---
169    
170     //---ATTRIBUTES---
171    
172     /**
173     * This is the friendly identifier of the
174     * component this class is running in.
175     * eg, a Filter may be called "filter1",
176     * If this class does not have an owning
177     * component, a name from the configuration
178     * can be placed here. This name could also
179     * be changed to null for utility classes.
180     */
181     private String _name = null;
182    
183     //---STATIC ATTRIBUTES---
184    
185     }