1 /***
2 * @(#)StringHelper.java
3 *
4 * JFoxSOAF, Service-Oriented Application Framework
5 *
6 * Copyright(c) JFoxSOAF Team
7 *
8 * Licensed under the GNU LGPL, Version 2.1 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.gnu.org/copyleft/lesser.html
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 * For more information, please visit:
21 * http://www.jfox.cn/confluence/display/JFoxSOAF/Home
22 * http://www.huihoo.org/jfox/jfoxsoaf
23 */
24
25 package org.huihoo.jfox.soaf.util.resource;
26
27 import java.util.Iterator;
28 import java.util.StringTokenizer;
29
30 /***
31 * String helper.
32 *
33 * @version $Revision: 1.2 $ $Date: 2006/02/15 08:45:46 $
34 * @version Revision: 1.0
35 */
36
37 public final class StringHelper {
38
39 public static final String EMPTY_STRING = "";
40
41 public static final char DOT = '.';
42
43 public static final char UNDERSCORE = '_';
44
45 public static final String COMMA_SPACE = ", ";
46
47 public static final String COMMA = ",";
48
49 public static final String OPEN_PAREN = "(";
50
51 public static final String CLOSE_PAREN = ")";
52
53 public static final char SINGLE_QUOTE = '\'';
54
55 public static String join(String seperator, String[] strings) {
56 int length = strings.length;
57 if (length == 0) {
58 return EMPTY_STRING;
59 }
60 StringBuffer buf = new StringBuffer(length * strings[0].length())
61 .append(strings[0]);
62 for (int i = 1; i < length; i++) {
63 buf.append(seperator).append(strings[i]);
64 }
65 return buf.toString();
66 }
67
68 public static String join(String seperator, Iterator objects) {
69 StringBuffer buf = new StringBuffer();
70 if (objects.hasNext()) {
71 buf.append(objects.next());
72 }
73 while (objects.hasNext()) {
74 buf.append(seperator).append(objects.next());
75 }
76 return buf.toString();
77 }
78
79 public static String[] add(String[] x, String sep, String[] y) {
80 String[] result = new String[x.length];
81 for (int i = 0; i < x.length; i++) {
82 result[i] = x[i] + sep + y[i];
83 }
84 return result;
85 }
86
87 public static String repeat(String string, int times) {
88 StringBuffer buf = new StringBuffer(string.length() * times);
89 for (int i = 0; i < times; i++) {
90 buf.append(string);
91 }
92 return buf.toString();
93 }
94
95 public static String replace(String template, String placeholder,
96 String replacement) {
97 return replace(template, placeholder, replacement, false);
98 }
99
100 public static String replace(String template, String placeholder,
101 String replacement, boolean wholeWords) {
102 int loc = template.indexOf(placeholder);
103 if (loc < 0) {
104 return template;
105 } else {
106 final boolean actuallyReplace = !wholeWords
107 || loc + placeholder.length() == template.length()
108 || !Character.isJavaIdentifierPart(template.charAt(loc
109 + placeholder.length()));
110 String actualReplacement = actuallyReplace ? replacement
111 : placeholder;
112 return new StringBuffer(template.substring(0, loc)).append(
113 actualReplacement).append(
114 replace(template.substring(loc + placeholder.length()),
115 placeholder, replacement, wholeWords)).toString();
116 }
117 }
118
119 public static String replaceOnce(String template, String placeholder,
120 String replacement) {
121 int loc = template.indexOf(placeholder);
122 if (loc < 0) {
123 return template;
124 } else {
125 return new StringBuffer(template.substring(0, loc)).append(
126 replacement).append(
127 template.substring(loc + placeholder.length())).toString();
128 }
129 }
130
131 public static String[] split(String seperators, String list) {
132 return split(seperators, list, false);
133 }
134
135 public static String[] split(String seperators, String list, boolean include) {
136 StringTokenizer tokens = new StringTokenizer(list, seperators, include);
137 String[] result = new String[tokens.countTokens()];
138 int i = 0;
139 while (tokens.hasMoreTokens()) {
140 result[i++] = tokens.nextToken();
141 }
142 return result;
143 }
144
145 public static String unqualify(String qualifiedName) {
146 return unqualify(qualifiedName, ".");
147 }
148
149 public static String unqualify(String qualifiedName, String seperator) {
150 return qualifiedName
151 .substring(qualifiedName.lastIndexOf(seperator) + 1);
152 }
153
154 public static String qualifier(String qualifiedName) {
155 int loc = qualifiedName.lastIndexOf(".");
156 if (loc < 0) {
157 return EMPTY_STRING;
158 } else {
159 return qualifiedName.substring(0, loc);
160 }
161 }
162
163 public static String[] suffix(String[] columns, String theSuffix) {
164 if (theSuffix == null) {
165 return columns;
166 }
167 String[] qualified = new String[columns.length];
168 for (int i = 0; i < columns.length; i++) {
169 qualified[i] = suffix(columns[i], theSuffix);
170 }
171 return qualified;
172 }
173
174 public static String suffix(String name, String theSuffix) {
175 return (theSuffix == null) ? name : name + theSuffix;
176 }
177
178 public static String[] prefix(String[] columns, String thePrefix) {
179 if (thePrefix == null) {
180 return columns;
181 }
182 String[] qualified = new String[columns.length];
183 for (int i = 0; i < columns.length; i++) {
184 qualified[i] = thePrefix + columns[i];
185 }
186 return qualified;
187 }
188
189 public static String root(String qualifiedName) {
190 int loc = qualifiedName.indexOf(".");
191 return (loc < 0) ? qualifiedName : qualifiedName.substring(0, loc);
192 }
193
194 public static boolean booleanValue(String tfString) {
195 String trimmed = tfString.trim().toLowerCase();
196 return trimmed.equals("true") || trimmed.equals("t");
197 }
198
199 public static String toString(Object[] array) {
200 int len = array.length;
201 if (len == 0) {
202 return StringHelper.EMPTY_STRING;
203 }
204 StringBuffer buf = new StringBuffer(len * 12);
205 for (int i = 0; i < len - 1; i++) {
206 buf.append(array[i]).append(StringHelper.COMMA_SPACE);
207 }
208 return buf.append(array[len - 1]).toString();
209 }
210
211 public static String[] multiply(String string, Iterator placeholders,
212 Iterator replacements) {
213 String[] result = new String[] { string };
214 while (placeholders.hasNext()) {
215 result = multiply(result, (String) placeholders.next(),
216 (String[]) replacements.next());
217 }
218 return result;
219 }
220
221 private static String[] multiply(String[] strings, String placeholder,
222 String[] replacements) {
223 String[] results = new String[replacements.length * strings.length];
224 int n = 0;
225 for (int i = 0; i < replacements.length; i++) {
226 for (int j = 0; j < strings.length; j++) {
227 results[n++] = replaceOnce(strings[j], placeholder,
228 replacements[i]);
229 }
230 }
231 return results;
232 }
233
234
235
236
237
238
239
240
241
242
243
244 public static int count(String string, char character) {
245 int n = 0;
246 for (int i = 0; i < string.length(); i++) {
247 if (string.charAt(i) == character) {
248 n++;
249 }
250 }
251 return n;
252 }
253
254 public static int countUnquoted(String string, char character) {
255 if (SINGLE_QUOTE == character) {
256 throw new IllegalArgumentException(
257 "Unquoted count of quotes is invalid");
258 }
259
260
261
262 int countNum = 0;
263 int stringLength = string == null ? 0 : string.length();
264 boolean inQuote = false;
265 for (int indx = 0; indx < stringLength; indx++) {
266 if (inQuote) {
267 if (SINGLE_QUOTE == string.charAt(indx)) {
268 inQuote = false;
269 }
270 } else if (SINGLE_QUOTE == string.charAt(indx)) {
271 inQuote = true;
272 } else if (string.charAt(indx) == character) {
273 countNum ++;
274 }
275 }
276 return countNum;
277 }
278
279 public static boolean isNotEmpty(String string) {
280 return string != null && string.length() > 0;
281 }
282
283 public static String qualify(String thePrefix, String name) {
284 char first = name.charAt(0);
285 if (first == SINGLE_QUOTE ||
286 Character.isDigit(first)
287
288 ) {
289 return name;
290 } else {
291 return new StringBuffer(thePrefix.length() + name.length() + 1)
292 .append(thePrefix).append(DOT).append(name).toString();
293 }
294 }
295
296 public static String[] qualify(String thePrefix, String[] names) {
297 if (thePrefix == null) {
298 return names;
299 }
300 int len = names.length;
301 String[] qualified = new String[len];
302 for (int i = 0; i < len; i++) {
303 qualified[i] = qualify(thePrefix, names[i]);
304 }
305 return qualified;
306 }
307
308 public static int firstIndexOfChar(String sqlString, String string,
309 int startindex) {
310 int matchAt = -1;
311 for (int i = 0; i < string.length(); i++) {
312 int curMatch = sqlString.indexOf(string.charAt(i), startindex);
313 if (curMatch >= 0) {
314 if (matchAt == -1) {
315 matchAt = curMatch;
316 } else {
317 matchAt = Math.min(matchAt, curMatch);
318 }
319 }
320 }
321 return matchAt;
322 }
323
324 public static String truncate(String string, int length) {
325 if (string.length() <= length) {
326 return string;
327 } else {
328 return string.substring(0, length);
329 }
330 }
331
332 public static String toUpperCase(String str) {
333 return str == null ? null : str.toUpperCase();
334 }
335
336 public static String toLowerCase(String str) {
337 return str == null ? null : str.toLowerCase();
338 }
339
340 }
341