commit | author | age
|
f13c4c
|
1 |
/*
|
JM |
2 |
* Copyright 2011 gitblit.com.
|
|
3 |
*
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5 |
* you may not use this file except in compliance with the License.
|
|
6 |
* You may obtain a copy of the License at
|
|
7 |
*
|
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9 |
*
|
|
10 |
* Unless required by applicable law or agreed to in writing, software
|
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13 |
* See the License for the specific language governing permissions and
|
|
14 |
* limitations under the License.
|
|
15 |
*/
|
87cc1e
|
16 |
package com.gitblit.utils;
|
JM |
17 |
|
b34048
|
18 |
import java.io.ByteArrayOutputStream;
|
87cc1e
|
19 |
import java.io.UnsupportedEncodingException;
|
ae9e15
|
20 |
import java.nio.ByteBuffer;
|
JM |
21 |
import java.nio.CharBuffer;
|
|
22 |
import java.nio.charset.CharacterCodingException;
|
|
23 |
import java.nio.charset.Charset;
|
|
24 |
import java.nio.charset.CharsetDecoder;
|
|
25 |
import java.nio.charset.IllegalCharsetNameException;
|
|
26 |
import java.nio.charset.UnsupportedCharsetException;
|
87cc1e
|
27 |
import java.security.MessageDigest;
|
JM |
28 |
import java.security.NoSuchAlgorithmException;
|
f339f5
|
29 |
import java.util.ArrayList;
|
ae9e15
|
30 |
import java.util.Arrays;
|
0b9119
|
31 |
import java.util.Collection;
|
94750e
|
32 |
import java.util.Collections;
|
JM |
33 |
import java.util.Comparator;
|
ae9e15
|
34 |
import java.util.LinkedHashSet;
|
87cc1e
|
35 |
import java.util.List;
|
ae9e15
|
36 |
import java.util.Set;
|
eb870f
|
37 |
import java.util.regex.Matcher;
|
JM |
38 |
import java.util.regex.Pattern;
|
f339f5
|
39 |
import java.util.regex.PatternSyntaxException;
|
8c9a20
|
40 |
|
d9f687
|
41 |
/**
|
JM |
42 |
* Utility class of string functions.
|
699e71
|
43 |
*
|
d9f687
|
44 |
* @author James Moger
|
699e71
|
45 |
*
|
d9f687
|
46 |
*/
|
87cc1e
|
47 |
public class StringUtils {
|
8c9a20
|
48 |
|
JM |
49 |
public static final String MD5_TYPE = "MD5:";
|
309c55
|
50 |
|
d5623a
|
51 |
public static final String COMBINED_MD5_TYPE = "CMD5:";
|
3e087a
|
52 |
|
d9f687
|
53 |
/**
|
JM |
54 |
* Returns true if the string is null or empty.
|
699e71
|
55 |
*
|
d9f687
|
56 |
* @param value
|
JM |
57 |
* @return true if string is null or empty
|
|
58 |
*/
|
87cc1e
|
59 |
public static boolean isEmpty(String value) {
|
JM |
60 |
return value == null || value.trim().length() == 0;
|
|
61 |
}
|
|
62 |
|
d9f687
|
63 |
/**
|
JM |
64 |
* Replaces carriage returns and line feeds with html line breaks.
|
699e71
|
65 |
*
|
d9f687
|
66 |
* @param string
|
JM |
67 |
* @return plain text with html line breaks
|
|
68 |
*/
|
87cc1e
|
69 |
public static String breakLinesForHtml(String string) {
|
JM |
70 |
return string.replace("\r\n", "<br/>").replace("\r", "<br/>").replace("\n", "<br/>");
|
|
71 |
}
|
|
72 |
|
d9f687
|
73 |
/**
|
JM |
74 |
* Prepare text for html presentation. Replace sensitive characters with
|
|
75 |
* html entities.
|
699e71
|
76 |
*
|
d9f687
|
77 |
* @param inStr
|
JM |
78 |
* @param changeSpace
|
|
79 |
* @return plain text escaped for html
|
|
80 |
*/
|
87cc1e
|
81 |
public static String escapeForHtml(String inStr, boolean changeSpace) {
|
310a80
|
82 |
return escapeForHtml(inStr, changeSpace, 4);
|
JM |
83 |
}
|
|
84 |
|
|
85 |
/**
|
|
86 |
* Prepare text for html presentation. Replace sensitive characters with
|
|
87 |
* html entities.
|
|
88 |
*
|
|
89 |
* @param inStr
|
|
90 |
* @param changeSpace
|
|
91 |
* @param tabLength
|
|
92 |
* @return plain text escaped for html
|
|
93 |
*/
|
|
94 |
public static String escapeForHtml(String inStr, boolean changeSpace, int tabLength) {
|
8a53c0
|
95 |
StringBuilder retStr = new StringBuilder();
|
87cc1e
|
96 |
int i = 0;
|
JM |
97 |
while (i < inStr.length()) {
|
|
98 |
if (inStr.charAt(i) == '&') {
|
|
99 |
retStr.append("&");
|
|
100 |
} else if (inStr.charAt(i) == '<') {
|
|
101 |
retStr.append("<");
|
|
102 |
} else if (inStr.charAt(i) == '>') {
|
|
103 |
retStr.append(">");
|
|
104 |
} else if (inStr.charAt(i) == '\"') {
|
|
105 |
retStr.append(""");
|
|
106 |
} else if (changeSpace && inStr.charAt(i) == ' ') {
|
|
107 |
retStr.append(" ");
|
|
108 |
} else if (changeSpace && inStr.charAt(i) == '\t') {
|
310a80
|
109 |
for (int j = 0; j < tabLength; j++) {
|
JM |
110 |
retStr.append(" ");
|
|
111 |
}
|
8c9a20
|
112 |
} else {
|
JM |
113 |
retStr.append(inStr.charAt(i));
|
|
114 |
}
|
|
115 |
i++;
|
|
116 |
}
|
|
117 |
return retStr.toString();
|
|
118 |
}
|
|
119 |
|
d9f687
|
120 |
/**
|
JM |
121 |
* Decode html entities back into plain text characters.
|
699e71
|
122 |
*
|
d9f687
|
123 |
* @param inStr
|
JM |
124 |
* @return returns plain text from html
|
|
125 |
*/
|
85c2e6
|
126 |
public static String decodeFromHtml(String inStr) {
|
JM |
127 |
return inStr.replace("&", "&").replace("<", "<").replace(">", ">")
|
|
128 |
.replace(""", "\"").replace(" ", " ");
|
|
129 |
}
|
|
130 |
|
d9f687
|
131 |
/**
|
JM |
132 |
* Encodes a url parameter by escaping troublesome characters.
|
699e71
|
133 |
*
|
d9f687
|
134 |
* @param inStr
|
JM |
135 |
* @return properly escaped url
|
|
136 |
*/
|
8c9a20
|
137 |
public static String encodeURL(String inStr) {
|
8a53c0
|
138 |
StringBuilder retStr = new StringBuilder();
|
8c9a20
|
139 |
int i = 0;
|
JM |
140 |
while (i < inStr.length()) {
|
|
141 |
if (inStr.charAt(i) == '/') {
|
|
142 |
retStr.append("%2F");
|
|
143 |
} else if (inStr.charAt(i) == ' ') {
|
|
144 |
retStr.append("%20");
|
16aa5a
|
145 |
} else if (inStr.charAt(i) == '&') {
|
JM |
146 |
retStr.append("%26");
|
46bfcc
|
147 |
} else if (inStr.charAt(i) == '+') {
|
JM |
148 |
retStr.append("%2B");
|
2a7306
|
149 |
} else {
|
87cc1e
|
150 |
retStr.append(inStr.charAt(i));
|
2a7306
|
151 |
}
|
87cc1e
|
152 |
i++;
|
JM |
153 |
}
|
|
154 |
return retStr.toString();
|
|
155 |
}
|
|
156 |
|
d9f687
|
157 |
/**
|
4bdd39
|
158 |
* Flatten the list of strings into a single string with the specified
|
JM |
159 |
* separator.
|
|
160 |
*
|
|
161 |
* @param values
|
|
162 |
* @param separator
|
|
163 |
* @return flattened list
|
|
164 |
*/
|
|
165 |
public static String flattenStrings(String[] values, String separator) {
|
|
166 |
return flattenStrings(Arrays.asList(values), separator);
|
|
167 |
}
|
|
168 |
|
|
169 |
/**
|
d9f687
|
170 |
* Flatten the list of strings into a single string with a space separator.
|
699e71
|
171 |
*
|
d9f687
|
172 |
* @param values
|
JM |
173 |
* @return flattened list
|
|
174 |
*/
|
0b9119
|
175 |
public static String flattenStrings(Collection<String> values) {
|
8a2e9c
|
176 |
return flattenStrings(values, " ");
|
JM |
177 |
}
|
|
178 |
|
d9f687
|
179 |
/**
|
JM |
180 |
* Flatten the list of strings into a single string with the specified
|
|
181 |
* separator.
|
699e71
|
182 |
*
|
d9f687
|
183 |
* @param values
|
JM |
184 |
* @param separator
|
|
185 |
* @return flattened list
|
|
186 |
*/
|
0b9119
|
187 |
public static String flattenStrings(Collection<String> values, String separator) {
|
87cc1e
|
188 |
StringBuilder sb = new StringBuilder();
|
JM |
189 |
for (String value : values) {
|
8a2e9c
|
190 |
sb.append(value).append(separator);
|
87cc1e
|
191 |
}
|
6e666f
|
192 |
if (sb.length() > 0) {
|
JM |
193 |
// truncate trailing separator
|
|
194 |
sb.setLength(sb.length() - separator.length());
|
|
195 |
}
|
87cc1e
|
196 |
return sb.toString().trim();
|
JM |
197 |
}
|
|
198 |
|
d9f687
|
199 |
/**
|
JM |
200 |
* Returns a string trimmed to a maximum length with trailing ellipses. If
|
|
201 |
* the string length is shorter than the max, the original string is
|
|
202 |
* returned.
|
699e71
|
203 |
*
|
d9f687
|
204 |
* @param value
|
JM |
205 |
* @param max
|
|
206 |
* @return trimmed string
|
|
207 |
*/
|
87cc1e
|
208 |
public static String trimString(String value, int max) {
|
JM |
209 |
if (value.length() <= max) {
|
|
210 |
return value;
|
|
211 |
}
|
|
212 |
return value.substring(0, max - 3) + "...";
|
|
213 |
}
|
|
214 |
|
d9f687
|
215 |
/**
|
JM |
216 |
* Left pad a string with the specified character, if the string length is
|
|
217 |
* less than the specified length.
|
699e71
|
218 |
*
|
d9f687
|
219 |
* @param input
|
JM |
220 |
* @param length
|
|
221 |
* @param pad
|
|
222 |
* @return left-padded string
|
|
223 |
*/
|
87cc1e
|
224 |
public static String leftPad(String input, int length, char pad) {
|
JM |
225 |
if (input.length() < length) {
|
|
226 |
StringBuilder sb = new StringBuilder();
|
|
227 |
for (int i = 0, len = length - input.length(); i < len; i++) {
|
|
228 |
sb.append(pad);
|
|
229 |
}
|
|
230 |
sb.append(input);
|
|
231 |
return sb.toString();
|
|
232 |
}
|
|
233 |
return input;
|
|
234 |
}
|
|
235 |
|
d9f687
|
236 |
/**
|
JM |
237 |
* Right pad a string with the specified character, if the string length is
|
|
238 |
* less then the specified length.
|
699e71
|
239 |
*
|
d9f687
|
240 |
* @param input
|
JM |
241 |
* @param length
|
|
242 |
* @param pad
|
|
243 |
* @return right-padded string
|
|
244 |
*/
|
87cc1e
|
245 |
public static String rightPad(String input, int length, char pad) {
|
JM |
246 |
if (input.length() < length) {
|
|
247 |
StringBuilder sb = new StringBuilder();
|
|
248 |
sb.append(input);
|
|
249 |
for (int i = 0, len = length - input.length(); i < len; i++) {
|
|
250 |
sb.append(pad);
|
|
251 |
}
|
|
252 |
return sb.toString();
|
|
253 |
}
|
|
254 |
return input;
|
|
255 |
}
|
|
256 |
|
d9f687
|
257 |
/**
|
JM |
258 |
* Calculates the SHA1 of the string.
|
699e71
|
259 |
*
|
d9f687
|
260 |
* @param text
|
JM |
261 |
* @return sha1 of the string
|
|
262 |
*/
|
87cc1e
|
263 |
public static String getSHA1(String text) {
|
JM |
264 |
try {
|
|
265 |
byte[] bytes = text.getBytes("iso-8859-1");
|
|
266 |
return getSHA1(bytes);
|
|
267 |
} catch (UnsupportedEncodingException u) {
|
|
268 |
throw new RuntimeException(u);
|
|
269 |
}
|
|
270 |
}
|
|
271 |
|
d9f687
|
272 |
/**
|
JM |
273 |
* Calculates the SHA1 of the byte array.
|
699e71
|
274 |
*
|
d9f687
|
275 |
* @param bytes
|
JM |
276 |
* @return sha1 of the byte array
|
|
277 |
*/
|
87cc1e
|
278 |
public static String getSHA1(byte[] bytes) {
|
JM |
279 |
try {
|
|
280 |
MessageDigest md = MessageDigest.getInstance("SHA-1");
|
|
281 |
md.update(bytes, 0, bytes.length);
|
8c9a20
|
282 |
byte[] digest = md.digest();
|
JM |
283 |
return toHex(digest);
|
87cc1e
|
284 |
} catch (NoSuchAlgorithmException t) {
|
JM |
285 |
throw new RuntimeException(t);
|
f97bf0
|
286 |
}
|
8c9a20
|
287 |
}
|
JM |
288 |
|
d9f687
|
289 |
/**
|
JM |
290 |
* Calculates the MD5 of the string.
|
699e71
|
291 |
*
|
d9f687
|
292 |
* @param string
|
JM |
293 |
* @return md5 of the string
|
|
294 |
*/
|
8c9a20
|
295 |
public static String getMD5(String string) {
|
JM |
296 |
try {
|
4ad1eb
|
297 |
return getMD5(string.getBytes("iso-8859-1"));
|
5450d0
|
298 |
} catch (UnsupportedEncodingException u) {
|
JM |
299 |
throw new RuntimeException(u);
|
4ad1eb
|
300 |
}
|
JM |
301 |
}
|
699e71
|
302 |
|
4ad1eb
|
303 |
/**
|
JM |
304 |
* Calculates the MD5 of the string.
|
699e71
|
305 |
*
|
4ad1eb
|
306 |
* @param string
|
JM |
307 |
* @return md5 of the string
|
|
308 |
*/
|
|
309 |
public static String getMD5(byte [] bytes) {
|
|
310 |
try {
|
|
311 |
MessageDigest md = MessageDigest.getInstance("MD5");
|
|
312 |
md.reset();
|
|
313 |
md.update(bytes);
|
|
314 |
byte[] digest = md.digest();
|
|
315 |
return toHex(digest);
|
5450d0
|
316 |
} catch (NoSuchAlgorithmException t) {
|
JM |
317 |
throw new RuntimeException(t);
|
8c9a20
|
318 |
}
|
JM |
319 |
}
|
|
320 |
|
d9f687
|
321 |
/**
|
JM |
322 |
* Returns the hex representation of the byte array.
|
699e71
|
323 |
*
|
d9f687
|
324 |
* @param bytes
|
JM |
325 |
* @return byte array as hex string
|
|
326 |
*/
|
b2fec2
|
327 |
public static String toHex(byte[] bytes) {
|
8c9a20
|
328 |
StringBuilder sb = new StringBuilder(bytes.length * 2);
|
JM |
329 |
for (int i = 0; i < bytes.length; i++) {
|
699e71
|
330 |
if ((bytes[i] & 0xff) < 0x10) {
|
8c9a20
|
331 |
sb.append('0');
|
JM |
332 |
}
|
699e71
|
333 |
sb.append(Long.toString(bytes[i] & 0xff, 16));
|
8c9a20
|
334 |
}
|
JM |
335 |
return sb.toString();
|
|
336 |
}
|
85c2e6
|
337 |
|
d9f687
|
338 |
/**
|
JM |
339 |
* Returns the root path of the specified path. Returns a blank string if
|
|
340 |
* there is no root path.
|
699e71
|
341 |
*
|
d9f687
|
342 |
* @param path
|
JM |
343 |
* @return root path or blank
|
|
344 |
*/
|
00afd7
|
345 |
public static String getRootPath(String path) {
|
JM |
346 |
if (path.indexOf('/') > -1) {
|
ec97f7
|
347 |
return path.substring(0, path.lastIndexOf('/'));
|
00afd7
|
348 |
}
|
JM |
349 |
return "";
|
|
350 |
}
|
008322
|
351 |
|
d9f687
|
352 |
/**
|
JM |
353 |
* Returns the path remainder after subtracting the basePath from the
|
|
354 |
* fullPath.
|
699e71
|
355 |
*
|
d9f687
|
356 |
* @param basePath
|
JM |
357 |
* @param fullPath
|
|
358 |
* @return the relative path
|
|
359 |
*/
|
008322
|
360 |
public static String getRelativePath(String basePath, String fullPath) {
|
ffbd6e
|
361 |
String bp = basePath.replace('\\', '/').toLowerCase();
|
JM |
362 |
String fp = fullPath.replace('\\', '/').toLowerCase();
|
|
363 |
if (fp.startsWith(bp)) {
|
|
364 |
String relativePath = fullPath.substring(basePath.length()).replace('\\', '/');
|
daaf89
|
365 |
if (relativePath.length() > 0 && relativePath.charAt(0) == '/') {
|
ffbd6e
|
366 |
relativePath = relativePath.substring(1);
|
JM |
367 |
}
|
|
368 |
return relativePath;
|
a125cf
|
369 |
}
|
ffbd6e
|
370 |
return fullPath;
|
a125cf
|
371 |
}
|
8c9a20
|
372 |
|
d9f687
|
373 |
/**
|
JM |
374 |
* Splits the space-separated string into a list of strings.
|
699e71
|
375 |
*
|
d9f687
|
376 |
* @param value
|
JM |
377 |
* @return list of strings
|
|
378 |
*/
|
f339f5
|
379 |
public static List<String> getStringsFromValue(String value) {
|
JM |
380 |
return getStringsFromValue(value, " ");
|
|
381 |
}
|
8c9a20
|
382 |
|
d9f687
|
383 |
/**
|
JM |
384 |
* Splits the string into a list of string by the specified separator.
|
699e71
|
385 |
*
|
d9f687
|
386 |
* @param value
|
JM |
387 |
* @param separator
|
|
388 |
* @return list of strings
|
|
389 |
*/
|
f339f5
|
390 |
public static List<String> getStringsFromValue(String value, String separator) {
|
3d699c
|
391 |
List<String> strings = new ArrayList<String>();
|
U |
392 |
try {
|
699e71
|
393 |
String[] chunks = value.split(separator + "(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
|
3d699c
|
394 |
for (String chunk : chunks) {
|
U |
395 |
chunk = chunk.trim();
|
|
396 |
if (chunk.length() > 0) {
|
|
397 |
if (chunk.charAt(0) == '"' && chunk.charAt(chunk.length() - 1) == '"') {
|
|
398 |
// strip double quotes
|
|
399 |
chunk = chunk.substring(1, chunk.length() - 1).trim();
|
|
400 |
}
|
|
401 |
strings.add(chunk);
|
|
402 |
}
|
|
403 |
}
|
|
404 |
} catch (PatternSyntaxException e) {
|
|
405 |
throw new RuntimeException(e);
|
|
406 |
}
|
|
407 |
return strings;
|
|
408 |
}
|
831469
|
409 |
|
JM |
410 |
/**
|
|
411 |
* Validates that a name is composed of letters, digits, or limited other
|
|
412 |
* characters.
|
699e71
|
413 |
*
|
831469
|
414 |
* @param name
|
JM |
415 |
* @return the first invalid character found or null if string is acceptable
|
|
416 |
*/
|
|
417 |
public static Character findInvalidCharacter(String name) {
|
46bfcc
|
418 |
char[] validChars = { '/', '.', '_', '-', '~', '+' };
|
831469
|
419 |
for (char c : name.toCharArray()) {
|
JM |
420 |
if (!Character.isLetterOrDigit(c)) {
|
|
421 |
boolean ok = false;
|
|
422 |
for (char vc : validChars) {
|
|
423 |
ok |= c == vc;
|
|
424 |
}
|
|
425 |
if (!ok) {
|
|
426 |
return c;
|
|
427 |
}
|
|
428 |
}
|
|
429 |
}
|
|
430 |
return null;
|
|
431 |
}
|
|
432 |
|
|
433 |
/**
|
|
434 |
* Simple fuzzy string comparison. This is a case-insensitive check. A
|
|
435 |
* single wildcard * value is supported.
|
699e71
|
436 |
*
|
831469
|
437 |
* @param value
|
JM |
438 |
* @param pattern
|
|
439 |
* @return true if the value matches the pattern
|
|
440 |
*/
|
|
441 |
public static boolean fuzzyMatch(String value, String pattern) {
|
|
442 |
if (value.equalsIgnoreCase(pattern)) {
|
|
443 |
return true;
|
|
444 |
}
|
|
445 |
if (pattern.contains("*")) {
|
|
446 |
boolean prefixMatches = false;
|
|
447 |
boolean suffixMatches = false;
|
|
448 |
|
|
449 |
int wildcard = pattern.indexOf('*');
|
|
450 |
String prefix = pattern.substring(0, wildcard).toLowerCase();
|
|
451 |
prefixMatches = value.toLowerCase().startsWith(prefix);
|
|
452 |
|
|
453 |
if (pattern.length() > (wildcard + 1)) {
|
|
454 |
String suffix = pattern.substring(wildcard + 1).toLowerCase();
|
|
455 |
suffixMatches = value.toLowerCase().endsWith(suffix);
|
|
456 |
return prefixMatches && suffixMatches;
|
|
457 |
}
|
|
458 |
return prefixMatches || suffixMatches;
|
|
459 |
}
|
|
460 |
return false;
|
|
461 |
}
|
94750e
|
462 |
|
JM |
463 |
/**
|
|
464 |
* Compare two repository names for proper group sorting.
|
699e71
|
465 |
*
|
94750e
|
466 |
* @param r1
|
JM |
467 |
* @param r2
|
|
468 |
* @return
|
|
469 |
*/
|
|
470 |
public static int compareRepositoryNames(String r1, String r2) {
|
|
471 |
// sort root repositories first, alphabetically
|
|
472 |
// then sort grouped repositories, alphabetically
|
62f435
|
473 |
r1 = r1.toLowerCase();
|
JM |
474 |
r2 = r2.toLowerCase();
|
94750e
|
475 |
int s1 = r1.indexOf('/');
|
JM |
476 |
int s2 = r2.indexOf('/');
|
|
477 |
if (s1 == -1 && s2 == -1) {
|
|
478 |
// neither grouped
|
|
479 |
return r1.compareTo(r2);
|
|
480 |
} else if (s1 > -1 && s2 > -1) {
|
|
481 |
// both grouped
|
|
482 |
return r1.compareTo(r2);
|
|
483 |
} else if (s1 == -1) {
|
|
484 |
return -1;
|
|
485 |
} else if (s2 == -1) {
|
|
486 |
return 1;
|
|
487 |
}
|
|
488 |
return 0;
|
|
489 |
}
|
|
490 |
|
|
491 |
/**
|
|
492 |
* Sort grouped repository names.
|
699e71
|
493 |
*
|
94750e
|
494 |
* @param list
|
JM |
495 |
*/
|
|
496 |
public static void sortRepositorynames(List<String> list) {
|
|
497 |
Collections.sort(list, new Comparator<String>() {
|
|
498 |
@Override
|
|
499 |
public int compare(String o1, String o2) {
|
|
500 |
return compareRepositoryNames(o1, o2);
|
|
501 |
}
|
|
502 |
});
|
|
503 |
}
|
309c55
|
504 |
|
JM |
505 |
public static String getColor(String value) {
|
|
506 |
int cs = 0;
|
|
507 |
for (char c : getMD5(value.toLowerCase()).toCharArray()) {
|
|
508 |
cs += c;
|
|
509 |
}
|
699e71
|
510 |
int n = (cs % 360);
|
309c55
|
511 |
float hue = ((float) n) / 360;
|
JM |
512 |
return hsvToRgb(hue, 0.90f, 0.65f);
|
|
513 |
}
|
|
514 |
|
|
515 |
public static String hsvToRgb(float hue, float saturation, float value) {
|
|
516 |
int h = (int) (hue * 6);
|
|
517 |
float f = hue * 6 - h;
|
|
518 |
float p = value * (1 - saturation);
|
|
519 |
float q = value * (1 - f * saturation);
|
|
520 |
float t = value * (1 - (1 - f) * saturation);
|
|
521 |
|
|
522 |
switch (h) {
|
|
523 |
case 0:
|
|
524 |
return rgbToString(value, t, p);
|
|
525 |
case 1:
|
|
526 |
return rgbToString(q, value, p);
|
|
527 |
case 2:
|
|
528 |
return rgbToString(p, value, t);
|
|
529 |
case 3:
|
|
530 |
return rgbToString(p, q, value);
|
|
531 |
case 4:
|
|
532 |
return rgbToString(t, p, value);
|
|
533 |
case 5:
|
|
534 |
return rgbToString(value, p, q);
|
|
535 |
default:
|
|
536 |
throw new RuntimeException(
|
|
537 |
"Something went wrong when converting from HSV to RGB. Input was " + hue + ", "
|
|
538 |
+ saturation + ", " + value);
|
|
539 |
}
|
|
540 |
}
|
|
541 |
|
|
542 |
public static String rgbToString(float r, float g, float b) {
|
|
543 |
String rs = Integer.toHexString((int) (r * 256));
|
|
544 |
String gs = Integer.toHexString((int) (g * 256));
|
|
545 |
String bs = Integer.toHexString((int) (b * 256));
|
|
546 |
return "#" + rs + gs + bs;
|
|
547 |
}
|
699e71
|
548 |
|
12c31e
|
549 |
/**
|
JM |
550 |
* Strips a trailing ".git" from the value.
|
699e71
|
551 |
*
|
12c31e
|
552 |
* @param value
|
JM |
553 |
* @return a stripped value or the original value if .git is not found
|
|
554 |
*/
|
6c6fbf
|
555 |
public static String stripDotGit(String value) {
|
JM |
556 |
if (value.toLowerCase().endsWith(".git")) {
|
|
557 |
return value.substring(0, value.length() - 4);
|
|
558 |
}
|
|
559 |
return value;
|
|
560 |
}
|
699e71
|
561 |
|
12c31e
|
562 |
/**
|
JM |
563 |
* Count the number of lines in a string.
|
699e71
|
564 |
*
|
12c31e
|
565 |
* @param value
|
JM |
566 |
* @return the line count
|
|
567 |
*/
|
672296
|
568 |
public static int countLines(String value) {
|
JM |
569 |
if (isEmpty(value)) {
|
|
570 |
return 0;
|
|
571 |
}
|
|
572 |
return value.split("\n").length;
|
|
573 |
}
|
699e71
|
574 |
|
12c31e
|
575 |
/**
|
JM |
576 |
* Returns the file extension of a path.
|
699e71
|
577 |
*
|
12c31e
|
578 |
* @param path
|
JM |
579 |
* @return a blank string or a file extension
|
|
580 |
*/
|
|
581 |
public static String getFileExtension(String path) {
|
|
582 |
int lastDot = path.lastIndexOf('.');
|
|
583 |
if (lastDot > -1) {
|
|
584 |
return path.substring(lastDot + 1);
|
|
585 |
}
|
|
586 |
return "";
|
|
587 |
}
|
699e71
|
588 |
|
f3b625
|
589 |
/**
|
dc7c2f
|
590 |
* Returns the file extension of a path.
|
JM |
591 |
*
|
|
592 |
* @param path
|
|
593 |
* @return a blank string or a file extension
|
|
594 |
*/
|
|
595 |
public static String stripFileExtension(String path) {
|
|
596 |
int lastDot = path.lastIndexOf('.');
|
|
597 |
if (lastDot > -1) {
|
|
598 |
return path.substring(0, lastDot);
|
|
599 |
}
|
|
600 |
return path;
|
|
601 |
}
|
|
602 |
|
|
603 |
/**
|
f3b625
|
604 |
* Replace all occurences of a substring within a string with
|
JC |
605 |
* another string.
|
699e71
|
606 |
*
|
f3b625
|
607 |
* From Spring StringUtils.
|
699e71
|
608 |
*
|
f3b625
|
609 |
* @param inString String to examine
|
JC |
610 |
* @param oldPattern String to replace
|
|
611 |
* @param newPattern String to insert
|
|
612 |
* @return a String with the replacements
|
|
613 |
*/
|
|
614 |
public static String replace(String inString, String oldPattern, String newPattern) {
|
|
615 |
StringBuilder sb = new StringBuilder();
|
|
616 |
int pos = 0; // our position in the old string
|
|
617 |
int index = inString.indexOf(oldPattern);
|
|
618 |
// the index of an occurrence we've found, or -1
|
|
619 |
int patLen = oldPattern.length();
|
|
620 |
while (index >= 0) {
|
|
621 |
sb.append(inString.substring(pos, index));
|
|
622 |
sb.append(newPattern);
|
|
623 |
pos = index + patLen;
|
|
624 |
index = inString.indexOf(oldPattern, pos);
|
|
625 |
}
|
|
626 |
sb.append(inString.substring(pos));
|
|
627 |
// remember to append any characters to the right of a match
|
|
628 |
return sb.toString();
|
|
629 |
}
|
699e71
|
630 |
|
ae9e15
|
631 |
/**
|
JM |
632 |
* Decodes a string by trying several charsets until one does not throw a
|
|
633 |
* coding exception. Last resort is to interpret as UTF-8 with illegal
|
|
634 |
* character substitution.
|
699e71
|
635 |
*
|
ae9e15
|
636 |
* @param content
|
JM |
637 |
* @param charsets optional
|
|
638 |
* @return a string
|
|
639 |
*/
|
|
640 |
public static String decodeString(byte [] content, String... charsets) {
|
|
641 |
Set<String> sets = new LinkedHashSet<String>();
|
|
642 |
if (!ArrayUtils.isEmpty(charsets)) {
|
|
643 |
sets.addAll(Arrays.asList(charsets));
|
|
644 |
}
|
086c04
|
645 |
String value = null;
|
ae9e15
|
646 |
sets.addAll(Arrays.asList("UTF-8", "ISO-8859-1", Charset.defaultCharset().name()));
|
JM |
647 |
for (String charset : sets) {
|
|
648 |
try {
|
|
649 |
Charset cs = Charset.forName(charset);
|
|
650 |
CharsetDecoder decoder = cs.newDecoder();
|
|
651 |
CharBuffer buffer = decoder.decode(ByteBuffer.wrap(content));
|
086c04
|
652 |
value = buffer.toString();
|
JM |
653 |
break;
|
ae9e15
|
654 |
} catch (CharacterCodingException e) {
|
JM |
655 |
// ignore and advance to the next charset
|
|
656 |
} catch (IllegalCharsetNameException e) {
|
|
657 |
// ignore illegal charset names
|
|
658 |
} catch (UnsupportedCharsetException e) {
|
|
659 |
// ignore unsupported charsets
|
|
660 |
}
|
|
661 |
}
|
872462
|
662 |
if (value != null && value.startsWith("\uFEFF")) {
|
086c04
|
663 |
// strip UTF-8 BOM
|
JM |
664 |
return value.substring(1);
|
|
665 |
}
|
|
666 |
return value;
|
ae9e15
|
667 |
}
|
699e71
|
668 |
|
eb870f
|
669 |
/**
|
JM |
670 |
* Attempt to extract a repository name from a given url using regular
|
|
671 |
* expressions. If no match is made, then return whatever trails after
|
|
672 |
* the final / character.
|
699e71
|
673 |
*
|
eb870f
|
674 |
* @param regexUrls
|
JM |
675 |
* @return a repository path
|
|
676 |
*/
|
|
677 |
public static String extractRepositoryPath(String url, String... urlpatterns) {
|
|
678 |
for (String urlPattern : urlpatterns) {
|
|
679 |
Pattern p = Pattern.compile(urlPattern);
|
|
680 |
Matcher m = p.matcher(url);
|
|
681 |
while (m.find()) {
|
|
682 |
String repositoryPath = m.group(1);
|
|
683 |
return repositoryPath;
|
|
684 |
}
|
|
685 |
}
|
|
686 |
// last resort
|
|
687 |
if (url.lastIndexOf('/') > -1) {
|
|
688 |
return url.substring(url.lastIndexOf('/') + 1);
|
|
689 |
}
|
|
690 |
return url;
|
|
691 |
}
|
699e71
|
692 |
|
b34048
|
693 |
/**
|
JM |
694 |
* Converts a string with \nnn sequences into a UTF-8 encoded string.
|
|
695 |
* @param input
|
|
696 |
* @return
|
|
697 |
*/
|
|
698 |
public static String convertOctal(String input) {
|
|
699 |
try {
|
|
700 |
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
|
701 |
Pattern p = Pattern.compile("(\\\\\\d{3})");
|
|
702 |
Matcher m = p.matcher(input);
|
|
703 |
int i = 0;
|
|
704 |
while (m.find()) {
|
|
705 |
bytes.write(input.substring(i, m.start()).getBytes("UTF-8"));
|
|
706 |
// replace octal encoded value
|
|
707 |
// strip leading \ character
|
|
708 |
String oct = m.group().substring(1);
|
|
709 |
bytes.write(Integer.parseInt(oct, 8));
|
699e71
|
710 |
i = m.end();
|
b34048
|
711 |
}
|
JM |
712 |
if (bytes.size() == 0) {
|
|
713 |
// no octal matches
|
|
714 |
return input;
|
|
715 |
} else {
|
|
716 |
if (i < input.length()) {
|
|
717 |
// add remainder of string
|
|
718 |
bytes.write(input.substring(i).getBytes("UTF-8"));
|
|
719 |
}
|
|
720 |
}
|
|
721 |
return bytes.toString("UTF-8");
|
|
722 |
} catch (Exception e) {
|
|
723 |
e.printStackTrace();
|
|
724 |
}
|
|
725 |
return input;
|
|
726 |
}
|
699e71
|
727 |
|
1e1b85
|
728 |
/**
|
JM |
729 |
* Returns the first path element of a path string. If no path separator is
|
699e71
|
730 |
* found in the path, an empty string is returned.
|
JM |
731 |
*
|
1e1b85
|
732 |
* @param path
|
JM |
733 |
* @return the first element in the path
|
|
734 |
*/
|
|
735 |
public static String getFirstPathElement(String path) {
|
|
736 |
if (path.indexOf('/') > -1) {
|
|
737 |
return path.substring(0, path.indexOf('/')).trim();
|
|
738 |
}
|
|
739 |
return "";
|
|
740 |
}
|
699e71
|
741 |
|
1e1b85
|
742 |
/**
|
JM |
743 |
* Returns the last path element of a path string
|
699e71
|
744 |
*
|
1e1b85
|
745 |
* @param path
|
JM |
746 |
* @return the last element in the path
|
|
747 |
*/
|
|
748 |
public static String getLastPathElement(String path) {
|
|
749 |
if (path.indexOf('/') > -1) {
|
|
750 |
return path.substring(path.lastIndexOf('/') + 1);
|
|
751 |
}
|
|
752 |
return path;
|
|
753 |
}
|
699e71
|
754 |
|
e5aaa5
|
755 |
/**
|
JM |
756 |
* Variation of String.matches() which disregards case issues.
|
699e71
|
757 |
*
|
e5aaa5
|
758 |
* @param regex
|
JM |
759 |
* @param input
|
|
760 |
* @return true if the pattern matches
|
|
761 |
*/
|
|
762 |
public static boolean matchesIgnoreCase(String input, String regex) {
|
|
763 |
Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
|
|
764 |
Matcher m = p.matcher(input);
|
|
765 |
return m.matches();
|
|
766 |
}
|
699e71
|
767 |
|
ac7e9a
|
768 |
/**
|
JM |
769 |
* Removes new line and carriage return chars from a string.
|
|
770 |
* If input value is null an empty string is returned.
|
699e71
|
771 |
*
|
ac7e9a
|
772 |
* @param input
|
JM |
773 |
* @return a sanitized or empty string
|
|
774 |
*/
|
|
775 |
public static String removeNewlines(String input) {
|
|
776 |
if (input == null) {
|
|
777 |
return "";
|
|
778 |
}
|
|
779 |
return input.replace('\n',' ').replace('\r', ' ').trim();
|
|
780 |
}
|
8f1c9f
|
781 |
|
JM |
782 |
|
|
783 |
/**
|
|
784 |
* Encode the username for user in an url.
|
|
785 |
*
|
|
786 |
* @param name
|
|
787 |
* @return the encoded name
|
|
788 |
*/
|
|
789 |
public static String encodeUsername(String name) {
|
|
790 |
return name.replace("@", "%40").replace(" ", "%20").replace("\\", "%5C");
|
|
791 |
}
|
|
792 |
|
|
793 |
/**
|
|
794 |
* Decode a username from an encoded url.
|
|
795 |
*
|
|
796 |
* @param name
|
|
797 |
* @return the decoded name
|
|
798 |
*/
|
|
799 |
public static String decodeUsername(String name) {
|
|
800 |
return name.replace("%40", "@").replace("%20", " ").replace("%5C", "\\");
|
|
801 |
}
|
309c55
|
802 |
} |