|
1
|
|
|
|
2
|
|
|
|
3
|
|
|
|
4
|
|
|
|
5
|
|
|
|
6
|
|
|
|
7
|
|
|
|
8
|
|
|
|
9
|
|
|
|
10
|
|
|
|
11
|
|
|
|
12
|
|
|
|
13
|
|
|
|
14
|
|
|
|
15
|
|
|
|
16
|
|
|
|
17
|
|
|
|
18
|
|
|
|
19
|
|
|
|
20
|
|
|
|
21
|
|
|
|
22
|
|
|
|
23
|
|
|
|
24
|
|
|
|
25
|
|
package org.huihoo.jfox.soaf.util.resource;
|
|
26
|
|
|
|
27
|
|
import java.util.Iterator;
|
|
28
|
|
import java.util.StringTokenizer;
|
|
29
|
|
|
|
30
|
|
|
|
31
|
|
|
|
32
|
|
|
|
33
|
|
|
|
34
|
|
|
|
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
|
0
|
public static String join(String seperator, String[] strings) {
|
|
56
|
0
|
int length = strings.length;
|
|
57
|
0
|
if (length == 0) {
|
|
58
|
0
|
return EMPTY_STRING;
|
|
59
|
|
}
|
|
60
|
0
|
StringBuffer buf = new StringBuffer(length * strings[0].length())
|
|
61
|
|
.append(strings[0]);
|
|
62
|
0
|
for (int i = 1; i < length; i++) {
|
|
63
|
0
|
buf.append(seperator).append(strings[i]);
|
|
64
|
|
}
|
|
65
|
0
|
return buf.toString();
|
|
66
|
|
}
|
|
67
|
|
|
|
68
|
0
|
public static String join(String seperator, Iterator objects) {
|
|
69
|
0
|
StringBuffer buf = new StringBuffer();
|
|
70
|
0
|
if (objects.hasNext()) {
|
|
71
|
0
|
buf.append(objects.next());
|
|
72
|
|
}
|
|
73
|
0
|
while (objects.hasNext()) {
|
|
74
|
0
|
buf.append(seperator).append(objects.next());
|
|
75
|
|
}
|
|
76
|
0
|
return buf.toString();
|
|
77
|
|
}
|
|
78
|
|
|
|
79
|
0
|
public static String[] add(String[] x, String sep, String[] y) {
|
|
80
|
0
|
String[] result = new String[x.length];
|
|
81
|
0
|
for (int i = 0; i < x.length; i++) {
|
|
82
|
0
|
result[i] = x[i] + sep + y[i];
|
|
83
|
|
}
|
|
84
|
0
|
return result;
|
|
85
|
|
}
|
|
86
|
|
|
|
87
|
0
|
public static String repeat(String string, int times) {
|
|
88
|
0
|
StringBuffer buf = new StringBuffer(string.length() * times);
|
|
89
|
0
|
for (int i = 0; i < times; i++) {
|
|
90
|
0
|
buf.append(string);
|
|
91
|
|
}
|
|
92
|
0
|
return buf.toString();
|
|
93
|
|
}
|
|
94
|
|
|
|
95
|
0
|
public static String replace(String template, String placeholder,
|
|
96
|
|
String replacement) {
|
|
97
|
0
|
return replace(template, placeholder, replacement, false);
|
|
98
|
|
}
|
|
99
|
|
|
|
100
|
0
|
public static String replace(String template, String placeholder,
|
|
101
|
|
String replacement, boolean wholeWords) {
|
|
102
|
0
|
int loc = template.indexOf(placeholder);
|
|
103
|
0
|
if (loc < 0) {
|
|
104
|
0
|
return template;
|
|
105
|
|
} else {
|
|
106
|
0
|
final boolean actuallyReplace = !wholeWords
|
|
107
|
|
|| loc + placeholder.length() == template.length()
|
|
108
|
|
|| !Character.isJavaIdentifierPart(template.charAt(loc
|
|
109
|
|
+ placeholder.length()));
|
|
110
|
0
|
String actualReplacement = actuallyReplace ? replacement
|
|
111
|
|
: placeholder;
|
|
112
|
0
|
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
|
0
|
public static String replaceOnce(String template, String placeholder,
|
|
120
|
|
String replacement) {
|
|
121
|
0
|
int loc = template.indexOf(placeholder);
|
|
122
|
0
|
if (loc < 0) {
|
|
123
|
0
|
return template;
|
|
124
|
|
} else {
|
|
125
|
0
|
return new StringBuffer(template.substring(0, loc)).append(
|
|
126
|
|
replacement).append(
|
|
127
|
|
template.substring(loc + placeholder.length())).toString();
|
|
128
|
|
}
|
|
129
|
|
}
|
|
130
|
|
|
|
131
|
0
|
public static String[] split(String seperators, String list) {
|
|
132
|
0
|
return split(seperators, list, false);
|
|
133
|
|
}
|
|
134
|
|
|
|
135
|
0
|
public static String[] split(String seperators, String list, boolean include) {
|
|
136
|
0
|
StringTokenizer tokens = new StringTokenizer(list, seperators, include);
|
|
137
|
0
|
String[] result = new String[tokens.countTokens()];
|
|
138
|
0
|
int i = 0;
|
|
139
|
0
|
while (tokens.hasMoreTokens()) {
|
|
140
|
0
|
result[i++] = tokens.nextToken();
|
|
141
|
|
}
|
|
142
|
0
|
return result;
|
|
143
|
|
}
|
|
144
|
|
|
|
145
|
0
|
public static String unqualify(String qualifiedName) {
|
|
146
|
0
|
return unqualify(qualifiedName, ".");
|
|
147
|
|
}
|
|
148
|
|
|
|
149
|
0
|
public static String unqualify(String qualifiedName, String seperator) {
|
|
150
|
0
|
return qualifiedName
|
|
151
|
|
.substring(qualifiedName.lastIndexOf(seperator) + 1);
|
|
152
|
|
}
|
|
153
|
|
|
|
154
|
0
|
public static String qualifier(String qualifiedName) {
|
|
155
|
0
|
int loc = qualifiedName.lastIndexOf(".");
|
|
156
|
0
|
if (loc < 0) {
|
|
157
|
0
|
return EMPTY_STRING;
|
|
158
|
|
} else {
|
|
159
|
0
|
return qualifiedName.substring(0, loc);
|
|
160
|
|
}
|
|
161
|
|
}
|
|
162
|
|
|
|
163
|
0
|
public static String[] suffix(String[] columns, String theSuffix) {
|
|
164
|
0
|
if (theSuffix == null) {
|
|
165
|
0
|
return columns;
|
|
166
|
|
}
|
|
167
|
0
|
String[] qualified = new String[columns.length];
|
|
168
|
0
|
for (int i = 0; i < columns.length; i++) {
|
|
169
|
0
|
qualified[i] = suffix(columns[i], theSuffix);
|
|
170
|
|
}
|
|
171
|
0
|
return qualified;
|
|
172
|
|
}
|
|
173
|
|
|
|
174
|
0
|
public static String suffix(String name, String theSuffix) {
|
|
175
|
0
|
return (theSuffix == null) ? name : name + theSuffix;
|
|
176
|
|
}
|
|
177
|
|
|
|
178
|
0
|
public static String[] prefix(String[] columns, String thePrefix) {
|
|
179
|
0
|
if (thePrefix == null) {
|
|
180
|
0
|
return columns;
|
|
181
|
|
}
|
|
182
|
0
|
String[] qualified = new String[columns.length];
|
|
183
|
0
|
for (int i = 0; i < columns.length; i++) {
|
|
184
|
0
|
qualified[i] = thePrefix + columns[i];
|
|
185
|
|
}
|
|
186
|
0
|
return qualified;
|
|
187
|
|
}
|
|
188
|
|
|
|
189
|
0
|
public static String root(String qualifiedName) {
|
|
190
|
0
|
int loc = qualifiedName.indexOf(".");
|
|
191
|
0
|
return (loc < 0) ? qualifiedName : qualifiedName.substring(0, loc);
|
|
192
|
|
}
|
|
193
|
|
|
|
194
|
0
|
public static boolean booleanValue(String tfString) {
|
|
195
|
0
|
String trimmed = tfString.trim().toLowerCase();
|
|
196
|
0
|
return trimmed.equals("true") || trimmed.equals("t");
|
|
197
|
|
}
|
|
198
|
|
|
|
199
|
0
|
public static String toString(Object[] array) {
|
|
200
|
0
|
int len = array.length;
|
|
201
|
0
|
if (len == 0) {
|
|
202
|
0
|
return StringHelper.EMPTY_STRING;
|
|
203
|
|
}
|
|
204
|
0
|
StringBuffer buf = new StringBuffer(len * 12);
|
|
205
|
0
|
for (int i = 0; i < len - 1; i++) {
|
|
206
|
0
|
buf.append(array[i]).append(StringHelper.COMMA_SPACE);
|
|
207
|
|
}
|
|
208
|
0
|
return buf.append(array[len - 1]).toString();
|
|
209
|
|
}
|
|
210
|
|
|
|
211
|
0
|
public static String[] multiply(String string, Iterator placeholders,
|
|
212
|
|
Iterator replacements) {
|
|
213
|
0
|
String[] result = new String[] { string };
|
|
214
|
0
|
while (placeholders.hasNext()) {
|
|
215
|
0
|
result = multiply(result, (String) placeholders.next(),
|
|
216
|
|
(String[]) replacements.next());
|
|
217
|
|
}
|
|
218
|
0
|
return result;
|
|
219
|
|
}
|
|
220
|
|
|
|
221
|
0
|
private static String[] multiply(String[] strings, String placeholder,
|
|
222
|
|
String[] replacements) {
|
|
223
|
0
|
String[] results = new String[replacements.length * strings.length];
|
|
224
|
0
|
int n = 0;
|
|
225
|
0
|
for (int i = 0; i < replacements.length; i++) {
|
|
226
|
0
|
for (int j = 0; j < strings.length; j++) {
|
|
227
|
0
|
results[n++] = replaceOnce(strings[j], placeholder,
|
|
228
|
|
replacements[i]);
|
|
229
|
|
}
|
|
230
|
|
}
|
|
231
|
0
|
return results;
|
|
232
|
|
}
|
|
233
|
|
|
|
234
|
|
|
|
235
|
|
|
|
236
|
|
|
|
237
|
|
|
|
238
|
|
|
|
239
|
|
|
|
240
|
|
|
|
241
|
|
|
|
242
|
|
|
|
243
|
|
|
|
244
|
0
|
public static int count(String string, char character) {
|
|
245
|
0
|
int n = 0;
|
|
246
|
0
|
for (int i = 0; i < string.length(); i++) {
|
|
247
|
0
|
if (string.charAt(i) == character) {
|
|
248
|
0
|
n++;
|
|
249
|
|
}
|
|
250
|
|
}
|
|
251
|
0
|
return n;
|
|
252
|
|
}
|
|
253
|
|
|
|
254
|
0
|
public static int countUnquoted(String string, char character) {
|
|
255
|
0
|
if (SINGLE_QUOTE == character) {
|
|
256
|
0
|
throw new IllegalArgumentException(
|
|
257
|
|
"Unquoted count of quotes is invalid");
|
|
258
|
|
}
|
|
259
|
|
|
|
260
|
|
|
|
261
|
|
|
|
262
|
0
|
int countNum = 0;
|
|
263
|
0
|
int stringLength = string == null ? 0 : string.length();
|
|
264
|
0
|
boolean inQuote = false;
|
|
265
|
0
|
for (int indx = 0; indx < stringLength; indx++) {
|
|
266
|
0
|
if (inQuote) {
|
|
267
|
0
|
if (SINGLE_QUOTE == string.charAt(indx)) {
|
|
268
|
0
|
inQuote = false;
|
|
269
|
|
}
|
|
270
|
0
|
} else if (SINGLE_QUOTE == string.charAt(indx)) {
|
|
271
|
0
|
inQuote = true;
|
|
272
|
0
|
} else if (string.charAt(indx) == character) {
|
|
273
|
0
|
countNum ++;
|
|
274
|
|
}
|
|
275
|
|
}
|
|
276
|
0
|
return countNum;
|
|
277
|
|
}
|
|
278
|
|
|
|
279
|
0
|
public static boolean isNotEmpty(String string) {
|
|
280
|
0
|
return string != null && string.length() > 0;
|
|
281
|
|
}
|
|
282
|
|
|
|
283
|
0
|
public static String qualify(String thePrefix, String name) {
|
|
284
|
0
|
char first = name.charAt(0);
|
|
285
|
0
|
if (first == SINGLE_QUOTE ||
|
|
286
|
|
Character.isDigit(first)
|
|
287
|
|
|
|
288
|
|
) {
|
|
289
|
0
|
return name;
|
|
290
|
|
} else {
|
|
291
|
0
|
return new StringBuffer(thePrefix.length() + name.length() + 1)
|
|
292
|
|
.append(thePrefix).append(DOT).append(name).toString();
|
|
293
|
|
}
|
|
294
|
|
}
|
|
295
|
|
|
|
296
|
0
|
public static String[] qualify(String thePrefix, String[] names) {
|
|
297
|
0
|
if (thePrefix == null) {
|
|
298
|
0
|
return names;
|
|
299
|
|
}
|
|
300
|
0
|
int len = names.length;
|
|
301
|
0
|
String[] qualified = new String[len];
|
|
302
|
0
|
for (int i = 0; i < len; i++) {
|
|
303
|
0
|
qualified[i] = qualify(thePrefix, names[i]);
|
|
304
|
|
}
|
|
305
|
0
|
return qualified;
|
|
306
|
|
}
|
|
307
|
|
|
|
308
|
0
|
public static int firstIndexOfChar(String sqlString, String string,
|
|
309
|
|
int startindex) {
|
|
310
|
0
|
int matchAt = -1;
|
|
311
|
0
|
for (int i = 0; i < string.length(); i++) {
|
|
312
|
0
|
int curMatch = sqlString.indexOf(string.charAt(i), startindex);
|
|
313
|
0
|
if (curMatch >= 0) {
|
|
314
|
0
|
if (matchAt == -1) {
|
|
315
|
0
|
matchAt = curMatch;
|
|
316
|
|
} else {
|
|
317
|
0
|
matchAt = Math.min(matchAt, curMatch);
|
|
318
|
|
}
|
|
319
|
|
}
|
|
320
|
|
}
|
|
321
|
0
|
return matchAt;
|
|
322
|
|
}
|
|
323
|
|
|
|
324
|
0
|
public static String truncate(String string, int length) {
|
|
325
|
0
|
if (string.length() <= length) {
|
|
326
|
0
|
return string;
|
|
327
|
|
} else {
|
|
328
|
0
|
return string.substring(0, length);
|
|
329
|
|
}
|
|
330
|
|
}
|
|
331
|
|
|
|
332
|
0
|
public static String toUpperCase(String str) {
|
|
333
|
0
|
return str == null ? null : str.toUpperCase();
|
|
334
|
|
}
|
|
335
|
|
|
|
336
|
0
|
public static String toLowerCase(String str) {
|
|
337
|
0
|
return str == null ? null : str.toLowerCase();
|
|
338
|
|
}
|
|
339
|
|
|
|
340
|
|
}
|
|
341
|
|
|
|
342
|
|
|