2013-06-08 21:31:19 +00:00
|
|
|
package com.earth2me.essentials.utils;
|
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Locale;
|
2013-06-08 21:31:19 +00:00
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
public class StringUtil {
|
|
|
|
private static final Pattern INVALIDFILECHARS = Pattern.compile("[^a-z0-9-]");
|
|
|
|
private static final Pattern STRICTINVALIDCHARS = Pattern.compile("[^a-z0-9]");
|
|
|
|
private static final Pattern INVALIDCHARS = Pattern.compile("[^\t\n\r\u0020-\u007E\u0085\u00A0-\uD7FF\uE000-\uFFFC]");
|
2013-06-08 21:31:19 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
//Used to clean file names before saving to disk
|
|
|
|
public static String sanitizeFileName(final String name) {
|
|
|
|
return INVALIDFILECHARS.matcher(name.toLowerCase(Locale.ENGLISH)).replaceAll("_");
|
|
|
|
}
|
2013-06-08 21:31:19 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
//Used to clean strings/names before saving as filenames/permissions
|
|
|
|
public static String safeString(final String string) {
|
|
|
|
return STRICTINVALIDCHARS.matcher(string.toLowerCase(Locale.ENGLISH)).replaceAll("_");
|
|
|
|
}
|
2013-06-08 21:31:19 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
//Less restrictive string sanitizing, when not used as perm or filename
|
|
|
|
public static String sanitizeString(final String string) {
|
|
|
|
return INVALIDCHARS.matcher(string).replaceAll("");
|
|
|
|
}
|
2013-06-08 21:31:19 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
public static String joinList(Object... list) {
|
|
|
|
return joinList(", ", list);
|
|
|
|
}
|
2013-06-08 21:31:19 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
public static String joinList(String seperator, Object... list) {
|
|
|
|
StringBuilder buf = new StringBuilder();
|
|
|
|
for (Object each : list) {
|
|
|
|
if (buf.length() > 0) {
|
|
|
|
buf.append(seperator);
|
|
|
|
}
|
2013-06-08 21:31:19 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
if (each instanceof Collection) {
|
|
|
|
buf.append(joinList(seperator, ((Collection) each).toArray()));
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
buf.append(each.toString());
|
|
|
|
} catch (Exception e) {
|
|
|
|
buf.append(each.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf.toString();
|
|
|
|
}
|
2014-04-18 04:57:47 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
public static String joinListSkip(String seperator, String skip, Object... list) {
|
|
|
|
StringBuilder buf = new StringBuilder();
|
|
|
|
for (Object each : list) {
|
|
|
|
if (each.toString().equalsIgnoreCase(skip)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf.length() > 0) {
|
|
|
|
buf.append(seperator);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (each instanceof Collection) {
|
|
|
|
buf.append(joinListSkip(seperator, skip, ((Collection) each).toArray()));
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
buf.append(each.toString());
|
|
|
|
} catch (Exception e) {
|
|
|
|
buf.append(each.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
private StringUtil() {
|
|
|
|
}
|
2013-06-08 21:31:19 +00:00
|
|
|
}
|