package com.walker.security.util;
|
|
import java.util.ArrayDeque;
|
import java.util.ArrayList;
|
import java.util.Collection;
|
import java.util.Deque;
|
import java.util.Iterator;
|
import java.util.List;
|
|
public class StringUtils {
|
|
private static final String[] EMPTY_STRING_ARRAY = new String[0];
|
|
public static String cleanPath(String path) {
|
if (!hasLength(path)) {
|
return path;
|
} else {
|
String normalizedPath = replace(path, "\\", "/");
|
String pathToUse = normalizedPath;
|
if (normalizedPath.indexOf(46) == -1) {
|
return normalizedPath;
|
} else {
|
int prefixIndex = normalizedPath.indexOf(58);
|
String prefix = "";
|
if (prefixIndex != -1) {
|
prefix = normalizedPath.substring(0, prefixIndex + 1);
|
if (prefix.contains("/")) {
|
prefix = "";
|
} else {
|
pathToUse = normalizedPath.substring(prefixIndex + 1);
|
}
|
}
|
|
if (pathToUse.startsWith("/")) {
|
prefix = prefix + "/";
|
pathToUse = pathToUse.substring(1);
|
}
|
|
String[] pathArray = delimitedListToStringArray(pathToUse, "/");
|
Deque<String> pathElements = new ArrayDeque(pathArray.length);
|
int tops = 0;
|
|
int i;
|
for(i = pathArray.length - 1; i >= 0; --i) {
|
String element = pathArray[i];
|
if (!".".equals(element)) {
|
if ("..".equals(element)) {
|
++tops;
|
} else if (tops > 0) {
|
--tops;
|
} else {
|
pathElements.addFirst(element);
|
}
|
}
|
}
|
|
if (pathArray.length == pathElements.size()) {
|
return normalizedPath;
|
} else {
|
for(i = 0; i < tops; ++i) {
|
pathElements.addFirst("..");
|
}
|
|
if (pathElements.size() == 1 && ((String)pathElements.getLast()).isEmpty() && !prefix.endsWith("/")) {
|
pathElements.addFirst(".");
|
}
|
|
String joined = collectionToDelimitedString(pathElements, "/");
|
return prefix.isEmpty() ? joined : prefix + joined;
|
}
|
}
|
}
|
}
|
|
public static String collectionToDelimitedString(Collection<?> coll, String delim, String prefix, String suffix) {
|
if (coll == null || coll.size() == 0) {
|
return "";
|
} else {
|
int totalLength = coll.size() * (prefix.length() + suffix.length()) + (coll.size() - 1) * delim.length();
|
|
Object element;
|
for(Iterator var5 = coll.iterator(); var5.hasNext(); totalLength += String.valueOf(element).length()) {
|
element = var5.next();
|
}
|
|
StringBuilder sb = new StringBuilder(totalLength);
|
Iterator<?> it = coll.iterator();
|
|
while(it.hasNext()) {
|
sb.append(prefix).append(it.next()).append(suffix);
|
if (it.hasNext()) {
|
sb.append(delim);
|
}
|
}
|
|
return sb.toString();
|
}
|
}
|
|
public static String collectionToDelimitedString(Collection<?> coll, String delim) {
|
return collectionToDelimitedString(coll, delim, "", "");
|
}
|
|
public static String collectionToCommaDelimitedString(Collection<?> coll) {
|
return collectionToDelimitedString(coll, ",");
|
}
|
|
public static String[] delimitedListToStringArray(String str, String delimiter) {
|
return delimitedListToStringArray(str, delimiter, (String)null);
|
}
|
|
public static String[] delimitedListToStringArray(String str, String delimiter, String charsToDelete) {
|
if (str == null) {
|
return EMPTY_STRING_ARRAY;
|
} else if (delimiter == null) {
|
return new String[]{str};
|
} else {
|
List<String> result = new ArrayList();
|
int pos;
|
if (delimiter.isEmpty()) {
|
for(pos = 0; pos < str.length(); ++pos) {
|
result.add(deleteAny(str.substring(pos, pos + 1), charsToDelete));
|
}
|
} else {
|
int delPos;
|
for(pos = 0; (delPos = str.indexOf(delimiter, pos)) != -1; pos = delPos + delimiter.length()) {
|
result.add(deleteAny(str.substring(pos, delPos), charsToDelete));
|
}
|
|
if (str.length() > 0 && pos <= str.length()) {
|
result.add(deleteAny(str.substring(pos), charsToDelete));
|
}
|
}
|
|
return toStringArray((Collection)result);
|
}
|
}
|
|
public static String[] toStringArray(Collection<String> collection) {
|
// return !CollectionUtils.isEmpty(collection) ? (String[])collection.toArray(EMPTY_STRING_ARRAY) : EMPTY_STRING_ARRAY;
|
return collection != null && collection.size() > 0 ? (String[])collection.toArray(EMPTY_STRING_ARRAY) : EMPTY_STRING_ARRAY;
|
}
|
|
public static String deleteAny(String inString, String charsToDelete) {
|
if (hasLength(inString) && hasLength(charsToDelete)) {
|
int lastCharIndex = 0;
|
char[] result = new char[inString.length()];
|
|
for(int i = 0; i < inString.length(); ++i) {
|
char c = inString.charAt(i);
|
if (charsToDelete.indexOf(c) == -1) {
|
result[lastCharIndex++] = c;
|
}
|
}
|
|
if (lastCharIndex == inString.length()) {
|
return inString;
|
} else {
|
return new String(result, 0, lastCharIndex);
|
}
|
} else {
|
return inString;
|
}
|
}
|
|
|
public static String replace(String inString, String oldPattern, String newPattern) {
|
if (hasLength(inString) && hasLength(oldPattern) && newPattern != null) {
|
int index = inString.indexOf(oldPattern);
|
if (index == -1) {
|
return inString;
|
} else {
|
int capacity = inString.length();
|
if (newPattern.length() > oldPattern.length()) {
|
capacity += 16;
|
}
|
|
StringBuilder sb = new StringBuilder(capacity);
|
int pos = 0;
|
|
for(int patLen = oldPattern.length(); index >= 0; index = inString.indexOf(oldPattern, pos)) {
|
sb.append(inString, pos, index);
|
sb.append(newPattern);
|
pos = index + patLen;
|
}
|
|
sb.append(inString, pos, inString.length());
|
return sb.toString();
|
}
|
} else {
|
return inString;
|
}
|
}
|
|
public static boolean hasLength(String str) {
|
return str != null && !str.isEmpty();
|
}
|
}
|