author | Stephen Talley <stephen.talley@oracle.com> |
Mon, 28 Mar 2011 10:53:34 -0400 | |
changeset 685 | 767674b0a2fb |
parent 648 | 05d2cd78123d |
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 |
/* |
|
638
91cbfdc9db0d
17737 vp -j/--newjvm needlessly identifies itself as Java
Stephen Talley <stephen.talley@oracle.com>
parents:
363
diff
changeset
|
23 |
* Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved. |
0 | 24 |
*/ |
25 |
||
219
57841c113efe
6788 package names should share o.o.o.vp prefix
Stephen Talley <stephen.talley@sun.com>
parents:
78
diff
changeset
|
26 |
package org.opensolaris.os.vp.util.misc; |
0 | 27 |
|
253
f219d4576960
7937 conversion between String and String[] should be formalized in StringArrayConverter
Stephen Talley <stephen.talley@sun.com>
parents:
243
diff
changeset
|
28 |
import java.util.Collection; |
0 | 29 |
import java.util.regex.*; |
30 |
||
31 |
public class TextUtil { |
|
32 |
// |
|
33 |
// Static methods |
|
34 |
// |
|
35 |
||
36 |
/** |
|
37 |
* Returns the base portion of a fully-qualified class name. |
|
38 |
*/ |
|
39 |
public static String getClassBaseName(String className) { |
|
40 |
return className.replaceFirst(".*\\.", ""); |
|
41 |
} |
|
42 |
||
43 |
/** |
|
307
a6e21a273a47
9370 Repository provides wrong class name to webserver
David Powell <David.Powell@sun.com>
parents:
304
diff
changeset
|
44 |
* Returns the base portion of the name of the given class. In |
a6e21a273a47
9370 Repository provides wrong class name to webserver
David Powell <David.Powell@sun.com>
parents:
304
diff
changeset
|
45 |
* constrast with the simple name, which is empty for anonymous |
a6e21a273a47
9370 Repository provides wrong class name to webserver
David Powell <David.Powell@sun.com>
parents:
304
diff
changeset
|
46 |
* classes and the Java identifier for local classes, this is the |
a6e21a273a47
9370 Repository provides wrong class name to webserver
David Powell <David.Powell@sun.com>
parents:
304
diff
changeset
|
47 |
* last component of the specified class's binary name. |
304
6630c8b85735
9343 Class.getSimpleName is insufficient for most uses
Stephen Talley <stephen.talley@sun.com>
parents:
253
diff
changeset
|
48 |
*/ |
6630c8b85735
9343 Class.getSimpleName is insufficient for most uses
Stephen Talley <stephen.talley@sun.com>
parents:
253
diff
changeset
|
49 |
public static String getBaseName(Class clazz) { |
6630c8b85735
9343 Class.getSimpleName is insufficient for most uses
Stephen Talley <stephen.talley@sun.com>
parents:
253
diff
changeset
|
50 |
return getClassBaseName(clazz.getName()); |
6630c8b85735
9343 Class.getSimpleName is insufficient for most uses
Stephen Talley <stephen.talley@sun.com>
parents:
253
diff
changeset
|
51 |
} |
6630c8b85735
9343 Class.getSimpleName is insufficient for most uses
Stephen Talley <stephen.talley@sun.com>
parents:
253
diff
changeset
|
52 |
|
6630c8b85735
9343 Class.getSimpleName is insufficient for most uses
Stephen Talley <stephen.talley@sun.com>
parents:
253
diff
changeset
|
53 |
/** |
0 | 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 a random alpha {@code String} with length between {@code minLen} |
|
62 |
* and {@code maxLen}. |
|
63 |
*/ |
|
64 |
public static String getRandomAlphaString(int minLen, int maxLen) { |
|
685
767674b0a2fb
18094 s/StringBuffer/StringBuilder/g
Stephen Talley <stephen.talley@oracle.com>
parents:
648
diff
changeset
|
65 |
StringBuilder buffer = new StringBuilder(); |
0 | 66 |
int length = (int)(Math.random() * (maxLen - minLen + 1)) + minLen; |
67 |
for (int j = 0; j < length; j++) { |
|
68 |
char c = (char)((Math.random() * 26) + 97); |
|
69 |
buffer.append(c); |
|
70 |
} |
|
71 |
return buffer.toString(); |
|
72 |
} |
|
73 |
||
74 |
/** |
|
75 |
* Replace characters with special meaning to HTML with their HTML entity |
|
76 |
* equivalents. |
|
77 |
*/ |
|
78 |
public static String escapeHTMLChars(String text) { |
|
685
767674b0a2fb
18094 s/StringBuffer/StringBuilder/g
Stephen Talley <stephen.talley@oracle.com>
parents:
648
diff
changeset
|
79 |
StringBuilder buffer = new StringBuilder(); |
0 | 80 |
|
81 |
for (char c : text.toCharArray()) { |
|
82 |
switch (c) { |
|
83 |
case '<': |
|
84 |
buffer.append("<"); |
|
85 |
break; |
|
86 |
||
87 |
case '>': |
|
88 |
buffer.append(">"); |
|
89 |
break; |
|
90 |
||
91 |
case '\"': |
|
92 |
buffer.append("""); |
|
93 |
break; |
|
94 |
||
95 |
case '\'': |
|
96 |
buffer.append("'"); |
|
97 |
break; |
|
98 |
||
99 |
case '\\': |
|
100 |
buffer.append("\"); |
|
101 |
break; |
|
102 |
||
103 |
case '&': |
|
104 |
buffer.append("&"); |
|
105 |
break; |
|
106 |
||
107 |
case '\n': |
|
108 |
buffer.append("<br/>"); |
|
109 |
break; |
|
110 |
||
111 |
default: |
|
112 |
buffer.append(c); |
|
113 |
} |
|
114 |
} |
|
115 |
||
116 |
return buffer.toString(); |
|
117 |
} |
|
118 |
||
119 |
/** |
|
120 |
* Splits the given string into lines. |
|
121 |
* |
|
122 |
* @param text |
|
123 |
* the text to format |
|
124 |
* |
|
125 |
* @param width |
|
126 |
* the maximum width of each line |
|
127 |
* |
|
78
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
17
diff
changeset
|
128 |
* @param indent1 |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
17
diff
changeset
|
129 |
* 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
|
130 |
* |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
17
diff
changeset
|
131 |
* @param indent2 |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
17
diff
changeset
|
132 |
* 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
|
133 |
* |
0 | 134 |
* @param forceBreak |
135 |
* whether to force a break in the middle of a word |
|
136 |
* if the length of the word is greater than |
|
137 |
* <code>width</code> |
|
138 |
* |
|
139 |
* @return a wrapped string separated by newlines |
|
140 |
*/ |
|
78
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
17
diff
changeset
|
141 |
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
|
142 |
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
|
143 |
|
0 | 144 |
if (text == null) { |
145 |
return text; |
|
146 |
} |
|
147 |
||
148 |
// Replace newlines |
|
149 |
text = text.replaceAll("\\s*[\\r\\n]\\s*", " "); |
|
150 |
||
685
767674b0a2fb
18094 s/StringBuffer/StringBuilder/g
Stephen Talley <stephen.talley@oracle.com>
parents:
648
diff
changeset
|
151 |
StringBuilder buffer = new StringBuilder(); |
0 | 152 |
|
78
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
17
diff
changeset
|
153 |
String indent = indent1; |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
17
diff
changeset
|
154 |
int w = width - indent.length(); |
0 | 155 |
if (w < 1) { |
156 |
w = 1; |
|
157 |
} |
|
158 |
||
159 |
for (int i = 0; text.length() > 0; i++) { |
|
160 |
String regex = "^\\s*(.{1," + w + "})($|\\s+.*)"; |
|
161 |
String[] groups = match(text, regex); |
|
162 |
||
163 |
if (groups == null) { |
|
164 |
// Can't break line on whitespace without exceeding width |
|
165 |
if (forceBreak) { |
|
166 |
// Break line in middle of word |
|
167 |
regex = "^\\s*(.{1," + w + "})(.*)"; |
|
168 |
} else { |
|
169 |
// Break line at next whitespace |
|
170 |
regex = "^\\s*(.+?)($|\\s+.*)"; |
|
171 |
} |
|
172 |
||
173 |
groups = match(text, regex); |
|
174 |
} |
|
175 |
||
176 |
if (i != 0) { |
|
177 |
buffer.append("\n"); |
|
178 |
} |
|
179 |
||
78
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
17
diff
changeset
|
180 |
buffer.append(indent).append(groups[1].trim()); |
0 | 181 |
text = groups[2]; |
182 |
||
183 |
if (i == 0) { |
|
78
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
17
diff
changeset
|
184 |
indent = indent2; |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
17
diff
changeset
|
185 |
w = width - indent.length(); |
0 | 186 |
if (w < 1) { |
187 |
w = 1; |
|
188 |
} |
|
189 |
} |
|
190 |
} |
|
191 |
||
192 |
return buffer.toString(); |
|
193 |
} |
|
194 |
||
78
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
17
diff
changeset
|
195 |
/** |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
17
diff
changeset
|
196 |
* Shortcut for: |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
17
diff
changeset
|
197 |
* <p> |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
17
diff
changeset
|
198 |
* <code> |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
17
diff
changeset
|
199 |
* {@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
|
200 |
* (text, width, "", "", forceBreak); |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
17
diff
changeset
|
201 |
* </code> |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
17
diff
changeset
|
202 |
* </p> |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
17
diff
changeset
|
203 |
*/ |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
17
diff
changeset
|
204 |
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
|
205 |
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
|
206 |
} |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
17
diff
changeset
|
207 |
|
0 | 208 |
public static boolean isPrintable(char c) { |
209 |
return c >= 32 && c <= 126; |
|
210 |
} |
|
211 |
||
212 |
public static String join(String delim, Object... list) { |
|
685
767674b0a2fb
18094 s/StringBuffer/StringBuilder/g
Stephen Talley <stephen.talley@oracle.com>
parents:
648
diff
changeset
|
213 |
StringBuilder buffer = new StringBuilder(); |
0 | 214 |
|
215 |
for (int i = 0; i < list.length; i++) { |
|
216 |
if (i != 0 && delim != null) { |
|
217 |
buffer.append(delim); |
|
218 |
} |
|
648
05d2cd78123d
17820 TextUtil.join should treat null values as empty strings
Stephen Talley <stephen.talley@oracle.com>
parents:
638
diff
changeset
|
219 |
if (list[i] != null) { |
05d2cd78123d
17820 TextUtil.join should treat null values as empty strings
Stephen Talley <stephen.talley@oracle.com>
parents:
638
diff
changeset
|
220 |
buffer.append(list[i]); |
05d2cd78123d
17820 TextUtil.join should treat null values as empty strings
Stephen Talley <stephen.talley@oracle.com>
parents:
638
diff
changeset
|
221 |
} |
0 | 222 |
} |
223 |
||
224 |
return buffer.toString(); |
|
225 |
} |
|
226 |
||
227 |
public static String join(String delim, byte... list) { |
|
228 |
Byte[] array = new Byte[list.length]; |
|
229 |
for (int i = 0; i < array.length; i++) { |
|
230 |
array[i] = list[i]; |
|
231 |
} |
|
232 |
return join(delim, (Object[])array); |
|
233 |
} |
|
234 |
||
235 |
public static String join(String delim, char... list) { |
|
236 |
Character[] array = new Character[list.length]; |
|
237 |
for (int i = 0; i < array.length; i++) { |
|
238 |
array[i] = list[i]; |
|
239 |
} |
|
240 |
return join(delim, (Object[])array); |
|
241 |
} |
|
242 |
||
243 |
public static String join(String delim, double... list) { |
|
244 |
Double[] array = new Double[list.length]; |
|
245 |
for (int i = 0; i < array.length; i++) { |
|
246 |
array[i] = list[i]; |
|
247 |
} |
|
248 |
return join(delim, (Object[])array); |
|
249 |
} |
|
250 |
||
251 |
public static String join(String delim, float... list) { |
|
252 |
Float[] array = new Float[list.length]; |
|
253 |
for (int i = 0; i < array.length; i++) { |
|
254 |
array[i] = list[i]; |
|
255 |
} |
|
256 |
return join(delim, (Object[])array); |
|
257 |
} |
|
258 |
||
259 |
public static String join(String delim, int... list) { |
|
260 |
Integer[] array = new Integer[list.length]; |
|
261 |
for (int i = 0; i < array.length; i++) { |
|
262 |
array[i] = list[i]; |
|
263 |
} |
|
264 |
return join(delim, (Object[])array); |
|
265 |
} |
|
266 |
||
267 |
public static String join(String delim, long... list) { |
|
268 |
Long[] array = new Long[list.length]; |
|
269 |
for (int i = 0; i < array.length; i++) { |
|
270 |
array[i] = list[i]; |
|
271 |
} |
|
272 |
return join(delim, (Object[])array); |
|
273 |
} |
|
274 |
||
275 |
public static String join(String delim, short... list) { |
|
276 |
Short[] array = new Short[list.length]; |
|
277 |
for (int i = 0; i < array.length; i++) { |
|
278 |
array[i] = list[i]; |
|
279 |
} |
|
280 |
return join(delim, (Object[])array); |
|
281 |
} |
|
282 |
||
283 |
public static String join(String delim, Collection list) { |
|
284 |
return join(delim, list.toArray()); |
|
285 |
} |
|
286 |
||
287 |
/** |
|
288 |
* Matches the given string against the given regular expression, |
|
289 |
* returning an array of the matched regular expression groups. |
|
290 |
* |
|
291 |
* @param text |
|
292 |
* the String to which to apply the regular |
|
293 |
* expression |
|
294 |
* |
|
295 |
* @param regex |
|
296 |
* a regular expression containing groups |
|
297 |
* |
|
298 |
* @return an array of the matched regular expression groups |
|
299 |
* (group 0 is the entire pattern), if the given |
|
300 |
* pattern matched, or {@code null} if the |
|
301 |
* pattern did not match |
|
302 |
*/ |
|
363
04bad26dceb9
11761 TextUtil should provide a match() polymorph that doesn't require recompilation of the regex
Stephen Talley <stephen.talley@sun.com>
parents:
307
diff
changeset
|
303 |
public static String[] match(String text, Pattern regex) { |
04bad26dceb9
11761 TextUtil should provide a match() polymorph that doesn't require recompilation of the regex
Stephen Talley <stephen.talley@sun.com>
parents:
307
diff
changeset
|
304 |
Matcher matcher = regex.matcher(text); |
0 | 305 |
if (!matcher.matches()) { |
306 |
return null; |
|
307 |
} |
|
308 |
||
309 |
String[] groups = new String[matcher.groupCount() + 1]; |
|
310 |
for (int i = 0; i < groups.length; i++) { |
|
311 |
groups[i] = matcher.group(i); |
|
312 |
} |
|
313 |
||
314 |
return groups; |
|
315 |
} |
|
316 |
||
363
04bad26dceb9
11761 TextUtil should provide a match() polymorph that doesn't require recompilation of the regex
Stephen Talley <stephen.talley@sun.com>
parents:
307
diff
changeset
|
317 |
/** |
04bad26dceb9
11761 TextUtil should provide a match() polymorph that doesn't require recompilation of the regex
Stephen Talley <stephen.talley@sun.com>
parents:
307
diff
changeset
|
318 |
* Compiles the given regular expression and calls {@link |
04bad26dceb9
11761 TextUtil should provide a match() polymorph that doesn't require recompilation of the regex
Stephen Talley <stephen.talley@sun.com>
parents:
307
diff
changeset
|
319 |
* #match(String,Pattern)}. |
04bad26dceb9
11761 TextUtil should provide a match() polymorph that doesn't require recompilation of the regex
Stephen Talley <stephen.talley@sun.com>
parents:
307
diff
changeset
|
320 |
* |
04bad26dceb9
11761 TextUtil should provide a match() polymorph that doesn't require recompilation of the regex
Stephen Talley <stephen.talley@sun.com>
parents:
307
diff
changeset
|
321 |
* @exception PatternSyntaxException |
04bad26dceb9
11761 TextUtil should provide a match() polymorph that doesn't require recompilation of the regex
Stephen Talley <stephen.talley@sun.com>
parents:
307
diff
changeset
|
322 |
* if the given pattern was malformed |
04bad26dceb9
11761 TextUtil should provide a match() polymorph that doesn't require recompilation of the regex
Stephen Talley <stephen.talley@sun.com>
parents:
307
diff
changeset
|
323 |
*/ |
04bad26dceb9
11761 TextUtil should provide a match() polymorph that doesn't require recompilation of the regex
Stephen Talley <stephen.talley@sun.com>
parents:
307
diff
changeset
|
324 |
public static String[] match(String text, String regex) |
04bad26dceb9
11761 TextUtil should provide a match() polymorph that doesn't require recompilation of the regex
Stephen Talley <stephen.talley@sun.com>
parents:
307
diff
changeset
|
325 |
throws PatternSyntaxException { |
04bad26dceb9
11761 TextUtil should provide a match() polymorph that doesn't require recompilation of the regex
Stephen Talley <stephen.talley@sun.com>
parents:
307
diff
changeset
|
326 |
|
04bad26dceb9
11761 TextUtil should provide a match() polymorph that doesn't require recompilation of the regex
Stephen Talley <stephen.talley@sun.com>
parents:
307
diff
changeset
|
327 |
return match(text, Pattern.compile(regex)); |
04bad26dceb9
11761 TextUtil should provide a match() polymorph that doesn't require recompilation of the regex
Stephen Talley <stephen.talley@sun.com>
parents:
307
diff
changeset
|
328 |
} |
04bad26dceb9
11761 TextUtil should provide a match() polymorph that doesn't require recompilation of the regex
Stephen Talley <stephen.talley@sun.com>
parents:
307
diff
changeset
|
329 |
|
638
91cbfdc9db0d
17737 vp -j/--newjvm needlessly identifies itself as Java
Stephen Talley <stephen.talley@oracle.com>
parents:
363
diff
changeset
|
330 |
/** |
91cbfdc9db0d
17737 vp -j/--newjvm needlessly identifies itself as Java
Stephen Talley <stephen.talley@oracle.com>
parents:
363
diff
changeset
|
331 |
* Converts {@code str} to a Java identifier by replacing any illegal |
91cbfdc9db0d
17737 vp -j/--newjvm needlessly identifies itself as Java
Stephen Talley <stephen.talley@oracle.com>
parents:
363
diff
changeset
|
332 |
* characters with {@code repl}. |
91cbfdc9db0d
17737 vp -j/--newjvm needlessly identifies itself as Java
Stephen Talley <stephen.talley@oracle.com>
parents:
363
diff
changeset
|
333 |
*/ |
91cbfdc9db0d
17737 vp -j/--newjvm needlessly identifies itself as Java
Stephen Talley <stephen.talley@oracle.com>
parents:
363
diff
changeset
|
334 |
public static String toJavaIdentifier(String str, String repl) { |
685
767674b0a2fb
18094 s/StringBuffer/StringBuilder/g
Stephen Talley <stephen.talley@oracle.com>
parents:
648
diff
changeset
|
335 |
StringBuilder buffer = new StringBuilder(); |
638
91cbfdc9db0d
17737 vp -j/--newjvm needlessly identifies itself as Java
Stephen Talley <stephen.talley@oracle.com>
parents:
363
diff
changeset
|
336 |
for (char c : str.toCharArray()) { |
91cbfdc9db0d
17737 vp -j/--newjvm needlessly identifies itself as Java
Stephen Talley <stephen.talley@oracle.com>
parents:
363
diff
changeset
|
337 |
boolean empty = buffer.length() == 0; |
91cbfdc9db0d
17737 vp -j/--newjvm needlessly identifies itself as Java
Stephen Talley <stephen.talley@oracle.com>
parents:
363
diff
changeset
|
338 |
if ((empty && Character.isJavaIdentifierStart(c)) || |
91cbfdc9db0d
17737 vp -j/--newjvm needlessly identifies itself as Java
Stephen Talley <stephen.talley@oracle.com>
parents:
363
diff
changeset
|
339 |
(!empty && Character.isJavaIdentifierPart(c))) { |
91cbfdc9db0d
17737 vp -j/--newjvm needlessly identifies itself as Java
Stephen Talley <stephen.talley@oracle.com>
parents:
363
diff
changeset
|
340 |
buffer.append(c); |
91cbfdc9db0d
17737 vp -j/--newjvm needlessly identifies itself as Java
Stephen Talley <stephen.talley@oracle.com>
parents:
363
diff
changeset
|
341 |
} else { |
91cbfdc9db0d
17737 vp -j/--newjvm needlessly identifies itself as Java
Stephen Talley <stephen.talley@oracle.com>
parents:
363
diff
changeset
|
342 |
buffer.append(repl); |
91cbfdc9db0d
17737 vp -j/--newjvm needlessly identifies itself as Java
Stephen Talley <stephen.talley@oracle.com>
parents:
363
diff
changeset
|
343 |
} |
91cbfdc9db0d
17737 vp -j/--newjvm needlessly identifies itself as Java
Stephen Talley <stephen.talley@oracle.com>
parents:
363
diff
changeset
|
344 |
} |
91cbfdc9db0d
17737 vp -j/--newjvm needlessly identifies itself as Java
Stephen Talley <stephen.talley@oracle.com>
parents:
363
diff
changeset
|
345 |
return buffer.toString(); |
91cbfdc9db0d
17737 vp -j/--newjvm needlessly identifies itself as Java
Stephen Talley <stephen.talley@oracle.com>
parents:
363
diff
changeset
|
346 |
} |
91cbfdc9db0d
17737 vp -j/--newjvm needlessly identifies itself as Java
Stephen Talley <stephen.talley@oracle.com>
parents:
363
diff
changeset
|
347 |
|
17
b82508ab9887
2522 hard to differentiate group and share edit views
Stephen Talley <stephen.talley@sun.com>
parents:
13
diff
changeset
|
348 |
public static String toJavaMethodName(String... words) { |
685
767674b0a2fb
18094 s/StringBuffer/StringBuilder/g
Stephen Talley <stephen.talley@oracle.com>
parents:
648
diff
changeset
|
349 |
StringBuilder buffer = new StringBuilder(); |
17
b82508ab9887
2522 hard to differentiate group and share edit views
Stephen Talley <stephen.talley@sun.com>
parents:
13
diff
changeset
|
350 |
for (String word : words) { |
b82508ab9887
2522 hard to differentiate group and share edit views
Stephen Talley <stephen.talley@sun.com>
parents:
13
diff
changeset
|
351 |
try { |
b82508ab9887
2522 hard to differentiate group and share edit views
Stephen Talley <stephen.talley@sun.com>
parents:
13
diff
changeset
|
352 |
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
|
353 |
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
|
354 |
} catch (IndexOutOfBoundsException ignore) { |
b82508ab9887
2522 hard to differentiate group and share edit views
Stephen Talley <stephen.talley@sun.com>
parents:
13
diff
changeset
|
355 |
} |
b82508ab9887
2522 hard to differentiate group and share edit views
Stephen Talley <stephen.talley@sun.com>
parents:
13
diff
changeset
|
356 |
} |
b82508ab9887
2522 hard to differentiate group and share edit views
Stephen Talley <stephen.talley@sun.com>
parents:
13
diff
changeset
|
357 |
return buffer.toString(); |
b82508ab9887
2522 hard to differentiate group and share edit views
Stephen Talley <stephen.talley@sun.com>
parents:
13
diff
changeset
|
358 |
} |
0 | 359 |
} |