author | Stephen Talley <stephen.talley@sun.com> |
Thu, 24 Jul 2008 21:30:12 -0400 | |
changeset 13 | b48b988cc94d |
parent 0 | 62ac12e07fc0 |
child 17 | b82508ab9887 |
permissions | -rw-r--r-- |
0 | 1 |
/* |
2 |
* CDDL HEADER START |
|
3 |
* |
|
4 |
* The contents of this file are subject to the terms of the |
|
5 |
* Common Development and Distribution License (the "License"). |
|
6 |
* You may not use this file except in compliance with the License. |
|
7 |
* |
|
8 |
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 |
* or http://www.opensolaris.org/os/licensing. |
|
10 |
* See the License for the specific language governing permissions |
|
11 |
* and limitations under the License. |
|
12 |
* |
|
13 |
* When distributing Covered Code, include this CDDL HEADER in each |
|
14 |
* file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 |
* If applicable, add the following below this CDDL HEADER, with the |
|
16 |
* fields enclosed by brackets "[]" replaced with your own identifying |
|
17 |
* information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 |
* |
|
19 |
* CDDL HEADER END |
|
20 |
*/ |
|
21 |
||
22 |
/* |
|
23 |
* Copyright 2008 Sun Microsystems, Inc. All rights reserved. |
|
24 |
* Use is subject to license terms. |
|
25 |
* |
|
26 |
* ident "@(#)TextUtil.java 1.10 08/05/11 SMI" |
|
27 |
*/ |
|
28 |
||
29 |
package org.opensolaris.os.vp.util; |
|
30 |
||
31 |
import java.util.*; |
|
32 |
import java.util.regex.*; |
|
33 |
||
34 |
public class TextUtil { |
|
35 |
// |
|
36 |
// Static methods |
|
37 |
// |
|
38 |
||
39 |
/** |
|
40 |
* Returns the base portion of a fully-qualified class name. |
|
41 |
*/ |
|
42 |
public static String getClassBaseName(String className) { |
|
43 |
return className.replaceFirst(".*\\.", ""); |
|
44 |
} |
|
45 |
||
46 |
/** |
|
47 |
* Returns the base portion of a fully-qualified class name. |
|
48 |
*/ |
|
49 |
public static String getClassBaseName(Class clazz) { |
|
50 |
return getClassBaseName(clazz.getName()); |
|
51 |
} |
|
52 |
||
53 |
/** |
|
54 |
* Returns the package portion of a fully-qualified class name. |
|
55 |
*/ |
|
13
b48b988cc94d
2655 s/getParentPackageName/getPackageName/g
Stephen Talley <stephen.talley@sun.com>
parents:
0
diff
changeset
|
56 |
public static String getPackageName(String name) { |
0 | 57 |
return name.replaceFirst("\\.?\\.*[^.]*$", ""); |
58 |
} |
|
59 |
||
60 |
/** |
|
61 |
* Returns the package portion of a fully-qualified class name. |
|
62 |
*/ |
|
13
b48b988cc94d
2655 s/getParentPackageName/getPackageName/g
Stephen Talley <stephen.talley@sun.com>
parents:
0
diff
changeset
|
63 |
public static String getPackageName(Class clazz) { |
b48b988cc94d
2655 s/getParentPackageName/getPackageName/g
Stephen Talley <stephen.talley@sun.com>
parents:
0
diff
changeset
|
64 |
return getPackageName(clazz.getName()); |
0 | 65 |
} |
66 |
||
67 |
/** |
|
68 |
* Returns a random alpha {@code String} with length between {@code minLen} |
|
69 |
* and {@code maxLen}. |
|
70 |
*/ |
|
71 |
public static String getRandomAlphaString(int minLen, int maxLen) { |
|
72 |
StringBuffer buffer = new StringBuffer(); |
|
73 |
int length = (int)(Math.random() * (maxLen - minLen + 1)) + minLen; |
|
74 |
for (int j = 0; j < length; j++) { |
|
75 |
char c = (char)((Math.random() * 26) + 97); |
|
76 |
buffer.append(c); |
|
77 |
} |
|
78 |
return buffer.toString(); |
|
79 |
} |
|
80 |
||
81 |
/** |
|
82 |
* Replace characters with special meaning to HTML with their HTML entity |
|
83 |
* equivalents. |
|
84 |
*/ |
|
85 |
public static String escapeHTMLChars(String text) { |
|
86 |
StringBuffer buffer = new StringBuffer(); |
|
87 |
||
88 |
for (char c : text.toCharArray()) { |
|
89 |
switch (c) { |
|
90 |
case '<': |
|
91 |
buffer.append("<"); |
|
92 |
break; |
|
93 |
||
94 |
case '>': |
|
95 |
buffer.append(">"); |
|
96 |
break; |
|
97 |
||
98 |
case '\"': |
|
99 |
buffer.append("""); |
|
100 |
break; |
|
101 |
||
102 |
case '\'': |
|
103 |
buffer.append("'"); |
|
104 |
break; |
|
105 |
||
106 |
case '\\': |
|
107 |
buffer.append("\"); |
|
108 |
break; |
|
109 |
||
110 |
case '&': |
|
111 |
buffer.append("&"); |
|
112 |
break; |
|
113 |
||
114 |
case '\n': |
|
115 |
buffer.append("<br/>"); |
|
116 |
break; |
|
117 |
||
118 |
default: |
|
119 |
buffer.append(c); |
|
120 |
} |
|
121 |
} |
|
122 |
||
123 |
return buffer.toString(); |
|
124 |
} |
|
125 |
||
126 |
/** |
|
127 |
* Splits the given string into lines. |
|
128 |
* |
|
129 |
* @param text |
|
130 |
* the text to format |
|
131 |
* |
|
132 |
* @param width |
|
133 |
* the maximum width of each line |
|
134 |
* |
|
135 |
* @param forceBreak |
|
136 |
* whether to force a break in the middle of a word |
|
137 |
* if the length of the word is greater than |
|
138 |
* <code>width</code> |
|
139 |
* |
|
140 |
* @return a wrapped string separated by newlines |
|
141 |
*/ |
|
142 |
public static String format(String text, int width, boolean forceBreak) { |
|
143 |
if (text == null) { |
|
144 |
return text; |
|
145 |
} |
|
146 |
||
147 |
// Replace newlines |
|
148 |
text = text.replaceAll("\\s*[\\r\\n]\\s*", " "); |
|
149 |
||
150 |
StringBuffer buffer = new StringBuffer(); |
|
151 |
||
152 |
int w = width; |
|
153 |
if (w < 1) { |
|
154 |
w = 1; |
|
155 |
} |
|
156 |
||
157 |
for (int i = 0; text.length() > 0; i++) { |
|
158 |
String regex = "^\\s*(.{1," + w + "})($|\\s+.*)"; |
|
159 |
String[] groups = match(text, regex); |
|
160 |
||
161 |
if (groups == null) { |
|
162 |
// Can't break line on whitespace without exceeding width |
|
163 |
if (forceBreak) { |
|
164 |
// Break line in middle of word |
|
165 |
regex = "^\\s*(.{1," + w + "})(.*)"; |
|
166 |
} else { |
|
167 |
// Break line at next whitespace |
|
168 |
regex = "^\\s*(.+?)($|\\s+.*)"; |
|
169 |
} |
|
170 |
||
171 |
groups = match(text, regex); |
|
172 |
} |
|
173 |
||
174 |
if (i != 0) { |
|
175 |
buffer.append("\n"); |
|
176 |
} |
|
177 |
||
178 |
buffer.append(groups[1].trim()); |
|
179 |
text = groups[2]; |
|
180 |
||
181 |
if (i == 0) { |
|
182 |
w = width; |
|
183 |
if (w < 1) { |
|
184 |
w = 1; |
|
185 |
} |
|
186 |
} |
|
187 |
} |
|
188 |
||
189 |
return buffer.toString(); |
|
190 |
} |
|
191 |
||
192 |
public static boolean isPrintable(char c) { |
|
193 |
return c >= 32 && c <= 126; |
|
194 |
} |
|
195 |
||
196 |
public static String join(String delim, Object... list) { |
|
197 |
StringBuffer buffer = new StringBuffer(); |
|
198 |
||
199 |
for (int i = 0; i < list.length; i++) { |
|
200 |
if (i != 0 && delim != null) { |
|
201 |
buffer.append(delim); |
|
202 |
} |
|
203 |
buffer.append(list[i]); |
|
204 |
} |
|
205 |
||
206 |
return buffer.toString(); |
|
207 |
} |
|
208 |
||
209 |
public static String join(String delim, byte... list) { |
|
210 |
Byte[] array = new Byte[list.length]; |
|
211 |
for (int i = 0; i < array.length; i++) { |
|
212 |
array[i] = list[i]; |
|
213 |
} |
|
214 |
return join(delim, (Object[])array); |
|
215 |
} |
|
216 |
||
217 |
public static String join(String delim, char... list) { |
|
218 |
Character[] array = new Character[list.length]; |
|
219 |
for (int i = 0; i < array.length; i++) { |
|
220 |
array[i] = list[i]; |
|
221 |
} |
|
222 |
return join(delim, (Object[])array); |
|
223 |
} |
|
224 |
||
225 |
public static String join(String delim, double... list) { |
|
226 |
Double[] array = new Double[list.length]; |
|
227 |
for (int i = 0; i < array.length; i++) { |
|
228 |
array[i] = list[i]; |
|
229 |
} |
|
230 |
return join(delim, (Object[])array); |
|
231 |
} |
|
232 |
||
233 |
public static String join(String delim, float... list) { |
|
234 |
Float[] array = new Float[list.length]; |
|
235 |
for (int i = 0; i < array.length; i++) { |
|
236 |
array[i] = list[i]; |
|
237 |
} |
|
238 |
return join(delim, (Object[])array); |
|
239 |
} |
|
240 |
||
241 |
public static String join(String delim, int... list) { |
|
242 |
Integer[] array = new Integer[list.length]; |
|
243 |
for (int i = 0; i < array.length; i++) { |
|
244 |
array[i] = list[i]; |
|
245 |
} |
|
246 |
return join(delim, (Object[])array); |
|
247 |
} |
|
248 |
||
249 |
public static String join(String delim, long... list) { |
|
250 |
Long[] array = new Long[list.length]; |
|
251 |
for (int i = 0; i < array.length; i++) { |
|
252 |
array[i] = list[i]; |
|
253 |
} |
|
254 |
return join(delim, (Object[])array); |
|
255 |
} |
|
256 |
||
257 |
public static String join(String delim, short... list) { |
|
258 |
Short[] array = new Short[list.length]; |
|
259 |
for (int i = 0; i < array.length; i++) { |
|
260 |
array[i] = list[i]; |
|
261 |
} |
|
262 |
return join(delim, (Object[])array); |
|
263 |
} |
|
264 |
||
265 |
public static String join(String delim, Collection list) { |
|
266 |
return join(delim, list.toArray()); |
|
267 |
} |
|
268 |
||
269 |
/** |
|
270 |
* Matches the given string against the given regular expression, |
|
271 |
* returning an array of the matched regular expression groups. |
|
272 |
* |
|
273 |
* @param text |
|
274 |
* the String to which to apply the regular |
|
275 |
* expression |
|
276 |
* |
|
277 |
* @param regex |
|
278 |
* a regular expression containing groups |
|
279 |
* |
|
280 |
* @return an array of the matched regular expression groups |
|
281 |
* (group 0 is the entire pattern), if the given |
|
282 |
* pattern matched, or {@code null} if the |
|
283 |
* pattern did not match |
|
284 |
* |
|
285 |
* @exception PatternSyntaxException |
|
286 |
* if the given pattern was malformed |
|
287 |
*/ |
|
288 |
public static String[] match(String text, String regex) |
|
289 |
throws PatternSyntaxException { |
|
290 |
||
291 |
Matcher matcher = Pattern.compile(regex).matcher(text); |
|
292 |
if (!matcher.matches()) { |
|
293 |
return null; |
|
294 |
} |
|
295 |
||
296 |
String[] groups = new String[matcher.groupCount() + 1]; |
|
297 |
for (int i = 0; i < groups.length; i++) { |
|
298 |
groups[i] = matcher.group(i); |
|
299 |
} |
|
300 |
||
301 |
return groups; |
|
302 |
} |
|
303 |
||
304 |
/** |
|
305 |
* Splits the given text into fields. |
|
306 |
* |
|
307 |
* @param delim |
|
308 |
* the regular expression delimiter for each field |
|
309 |
* |
|
310 |
* @param text |
|
311 |
* the text to split |
|
312 |
* |
|
313 |
* @param quotes |
|
314 |
* the characters that indicate a grouping of text, |
|
315 |
* eg. single quotes ("<code>'</code>") or double |
|
316 |
* quotes ("<code>"</code>"), etc., or |
|
317 |
* <code>null</code> for no quotes |
|
318 |
* |
|
319 |
* @param quoteEsc |
|
320 |
* an expression that can precede a quote character |
|
321 |
* so that it is interpreted normally, rather than as |
|
322 |
* a quote character, or <code>null</code> if no such |
|
323 |
* escape character is used |
|
324 |
* |
|
325 |
* @return the resulting fields |
|
326 |
*/ |
|
327 |
public static String[] split( |
|
328 |
String delim, String text, String quotes, String quoteEsc) { |
|
329 |
||
330 |
// Create the matching regular expression |
|
331 |
int ngroups = 0; |
|
332 |
int quoteGroup = -1; |
|
333 |
int quoteEscGroup = -1; |
|
334 |
||
335 |
StringBuffer reBuffer = new StringBuffer("^(.*?)("); |
|
336 |
int fieldGroup = ++ngroups; |
|
337 |
ngroups++; |
|
338 |
||
339 |
if (quotes != null) { |
|
340 |
if (quoteEsc != null) { |
|
341 |
reBuffer.append("(\\Q").append(quoteEsc).append("\\E"). |
|
342 |
append("[").append(quotes).append("])|"); |
|
343 |
quoteEscGroup = ++ngroups; |
|
344 |
} |
|
345 |
reBuffer.append("([").append(quotes).append("])|"); |
|
346 |
quoteGroup = ++ngroups; |
|
347 |
} |
|
348 |
reBuffer.append("(").append(delim).append("))(.*)"); |
|
349 |
int delimGroup = ++ngroups; |
|
350 |
int textGroup = ++ngroups; |
|
351 |
||
352 |
String regex = reBuffer.toString(); |
|
353 |
||
354 |
List<String> fields = new ArrayList<String>(); |
|
355 |
||
356 |
// Build the field |
|
357 |
StringBuffer field = new StringBuffer(); |
|
358 |
||
359 |
boolean inQuote = false; |
|
360 |
String curQuote = null; |
|
361 |
while (text != null && text.length() > 0) { |
|
362 |
||
363 |
String[] groups = match(text, regex); |
|
364 |
||
365 |
if (groups == null) { |
|
366 |
field.append(text); |
|
367 |
text = null; |
|
368 |
} else { |
|
369 |
||
370 |
field.append(groups[fieldGroup]); |
|
371 |
||
372 |
// Remaining text |
|
373 |
text = groups[textGroup]; |
|
374 |
||
375 |
// Was a delimiter reached? |
|
376 |
if (groups[delimGroup] != null) { |
|
377 |
if (inQuote) { |
|
378 |
field.append(groups[delimGroup]); |
|
379 |
} else { |
|
380 |
fields.add(field.toString()); |
|
381 |
field.setLength(0); |
|
382 |
} |
|
383 |
} else |
|
384 |
||
385 |
try { |
|
386 |
String quote = groups[quoteGroup]; |
|
387 |
// Was a quote reached? |
|
388 |
if (quote != null) { |
|
389 |
if (inQuote) { |
|
390 |
if (quote.equals(curQuote)) { |
|
391 |
inQuote = false; |
|
392 |
} else { |
|
393 |
field.append(quote); |
|
394 |
} |
|
395 |
} else { |
|
396 |
inQuote = true; |
|
397 |
curQuote = quote; |
|
398 |
} |
|
399 |
} else |
|
400 |
||
401 |
// Was an escaped quote reached? |
|
402 |
if (groups[quoteEscGroup] != null) { |
|
403 |
field.append(quotes); |
|
404 |
} |
|
405 |
} |
|
406 |
catch (ArrayIndexOutOfBoundsException ignore) {} |
|
407 |
} |
|
408 |
} |
|
409 |
||
410 |
fields.add(field.toString()); |
|
411 |
||
412 |
return fields.toArray(new String[fields.size()]); |
|
413 |
} |
|
414 |
} |