1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.org.iscream.cms.server.util; |
3 |
|
4 |
//---IMPORTS--- |
5 |
import java.util.*; |
6 |
|
7 |
/** |
8 |
* A class containing useful methods for manipulating |
9 |
* String objects. |
10 |
* |
11 |
* @author $Author: tdb $ |
12 |
* @version $Id: StringUtils.java,v 1.5 2001/12/10 22:49:19 tdb Exp $ |
13 |
*/ |
14 |
public class StringUtils { |
15 |
|
16 |
//---FINAL ATTRIBUTES--- |
17 |
|
18 |
/** |
19 |
* The current CVS revision of this class |
20 |
*/ |
21 |
public static final String REVISION = "$Revision: 1.5 $"; |
22 |
|
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 |
} |
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 |
} |
68 |
|
69 |
/** |
70 |
* Checks if a given string matches a |
71 |
* wildcard expression. For example:<br> |
72 |
* <br> |
73 |
* Given "testingstring"<br> |
74 |
* And "test*ing"<br> |
75 |
* <br> |
76 |
* This method would return true as there is a match. |
77 |
* |
78 |
* @param in the string to check |
79 |
* @param expression the wildcard expression to match against |
80 |
* |
81 |
* @return if there is a match |
82 |
*/ |
83 |
public static boolean wildcardMatch(String in, String expression) { |
84 |
// check actually has wildcards |
85 |
if(expression.indexOf("*") != -1) { |
86 |
StringTokenizer st = new StringTokenizer(expression, "*"); |
87 |
String part = ""; |
88 |
int index = 0; |
89 |
// the first token will be everything at the start |
90 |
// unless it starts with a wildcard |
91 |
// -- if it starts with a wildcard the start |
92 |
// will always match |
93 |
if(!expression.startsWith("*")) { |
94 |
part = st.nextToken(); |
95 |
if (in.startsWith(part)) { |
96 |
index = part.length(); |
97 |
} |
98 |
else { |
99 |
// doesn't start with the first part |
100 |
// can't be a match |
101 |
return false; |
102 |
} |
103 |
} |
104 |
while (st.hasMoreTokens()) { |
105 |
part = st.nextToken(); |
106 |
// if the next section can't be matched |
107 |
// then this isn't in the input string |
108 |
if(in.substring(index).indexOf(part) == -1) { |
109 |
// we don't want to look through any more of it |
110 |
// can't be a match if this part isn't there |
111 |
return false; |
112 |
} |
113 |
else { |
114 |
// move index pointer to start of the "rest" |
115 |
// where the rest is everything after what we just matched |
116 |
index += in.substring(index).indexOf(part) + part.length(); |
117 |
} |
118 |
} |
119 |
// if we reach here and we've found a match |
120 |
// we want to check that the last part |
121 |
// of the wildcard string is the last part |
122 |
// of the input string, if it is, we break out |
123 |
// and finish as we've found a match. |
124 |
// if the end of the wildcard is a *, then |
125 |
// we don't care |
126 |
if (!expression.endsWith("*")) { |
127 |
if ((in.endsWith(part))) { |
128 |
return true; |
129 |
} |
130 |
else { |
131 |
// doesn't end with the first part |
132 |
// can't be a match |
133 |
return false; |
134 |
} |
135 |
// ends with a *, so it matches the |
136 |
// end regardless |
137 |
} |
138 |
else { |
139 |
return true; |
140 |
} |
141 |
} |
142 |
else if(in.equals(expression)) { |
143 |
// might as well do a check if they're the same |
144 |
// bit daft to call this whole method if they are though :) |
145 |
return true; |
146 |
} |
147 |
else { |
148 |
// obviously doesn't match if it's not a wildcard |
149 |
// expression or isn't equal :) |
150 |
return false; |
151 |
} |
152 |
} |
153 |
|
154 |
//---CONSTRUCTORS--- |
155 |
|
156 |
//---PUBLIC METHODS--- |
157 |
|
158 |
/** |
159 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
160 |
* method to provide clean logging (every class should have this). |
161 |
* |
162 |
* This uses the uk.org.iscream.cms.server.util.FormatName class |
163 |
* to format the toString() |
164 |
* |
165 |
* @return the name of this class and its CVS revision |
166 |
*/ |
167 |
public String toString() { |
168 |
return FormatName.getName( |
169 |
_name, |
170 |
getClass().getName(), |
171 |
REVISION); |
172 |
} |
173 |
|
174 |
//---PRIVATE METHODS--- |
175 |
|
176 |
//---ACCESSOR/MUTATOR METHODS--- |
177 |
|
178 |
//---ATTRIBUTES--- |
179 |
|
180 |
/** |
181 |
* This is the friendly identifier of the |
182 |
* component this class is running in. |
183 |
* eg, a Filter may be called "filter1", |
184 |
* If this class does not have an owning |
185 |
* component, a name from the configuration |
186 |
* can be placed here. This name could also |
187 |
* be changed to null for utility classes. |
188 |
*/ |
189 |
private String _name = null; |
190 |
|
191 |
//---STATIC ATTRIBUTES--- |
192 |
|
193 |
} |