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