author | Stephen Talley <stephen.talley@sun.com> |
Fri, 03 Apr 2009 12:57:16 -0400 | |
changeset 253 | f219d4576960 |
parent 243 | 979bb698977b |
child 304 | 6630c8b85735 |
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 |
|
253
f219d4576960
7937 conversion between String and String[] should be formalized in StringArrayConverter
Stephen Talley <stephen.talley@sun.com>
parents:
243
diff
changeset
|
29 |
import java.util.Collection; |
0 | 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 |
||
17
b82508ab9887
2522 hard to differentiate group and share edit views
Stephen Talley <stephen.talley@sun.com>
parents:
13
diff
changeset
|
311 |
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
|
312 |
StringBuffer buffer = new StringBuffer(); |
b82508ab9887
2522 hard to differentiate group and share edit views
Stephen Talley <stephen.talley@sun.com>
parents:
13
diff
changeset
|
313 |
for (String word : words) { |
b82508ab9887
2522 hard to differentiate group and share edit views
Stephen Talley <stephen.talley@sun.com>
parents:
13
diff
changeset
|
314 |
try { |
b82508ab9887
2522 hard to differentiate group and share edit views
Stephen Talley <stephen.talley@sun.com>
parents:
13
diff
changeset
|
315 |
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
|
316 |
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
|
317 |
} catch (IndexOutOfBoundsException ignore) { |
b82508ab9887
2522 hard to differentiate group and share edit views
Stephen Talley <stephen.talley@sun.com>
parents:
13
diff
changeset
|
318 |
} |
b82508ab9887
2522 hard to differentiate group and share edit views
Stephen Talley <stephen.talley@sun.com>
parents:
13
diff
changeset
|
319 |
} |
b82508ab9887
2522 hard to differentiate group and share edit views
Stephen Talley <stephen.talley@sun.com>
parents:
13
diff
changeset
|
320 |
return buffer.toString(); |
b82508ab9887
2522 hard to differentiate group and share edit views
Stephen Talley <stephen.talley@sun.com>
parents:
13
diff
changeset
|
321 |
} |
0 | 322 |
} |