-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
LocationUtil.java
92 lines (76 loc) · 2.89 KB
/
LocationUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package nallar.tickthreading.util;
import nallar.tickthreading.Log;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.regex.Pattern;
public enum LocationUtil {
;
private static final File currentDir = new File(".").getAbsoluteFile();
private static String normalize(String path, String separator) {
path = path.replace(separator + separator, separator);
path = path.replace(separator + separator, separator);
if (path.endsWith(separator)) {
path = path.substring(0, path.length() - 1);
}
return path;
}
// Derived from http://stackoverflow.com/a/3054692/250076
public static File getRelativeFile(File targetFile, File baseFile) {
try {
targetFile = targetFile.getCanonicalFile();
baseFile = baseFile.getCanonicalFile();
} catch (IOException ignored) {
}
String pathSeparator = File.separator;
String basePath = baseFile.getAbsolutePath();
String normalizedTargetPath = normalize(targetFile.getAbsolutePath(), pathSeparator);
String normalizedBasePath = normalize(basePath, pathSeparator);
String[] base = normalizedBasePath.split(Pattern.quote(pathSeparator));
String[] target = normalizedTargetPath.split(Pattern.quote(pathSeparator));
StringBuilder common = new StringBuilder();
int commonIndex = 0;
while (commonIndex < target.length && commonIndex < base.length && target[commonIndex].equals(base[commonIndex])) {
common.append(target[commonIndex]).append(pathSeparator);
commonIndex++;
}
if (commonIndex == 0) {
throw new Error("No common path element found for '" + normalizedTargetPath + "' and '" + normalizedBasePath + '\'');
}
boolean baseIsFile = true;
if (baseFile.exists()) {
baseIsFile = baseFile.isFile();
} else if (basePath.endsWith(pathSeparator)) {
baseIsFile = false;
}
StringBuilder relative = new StringBuilder();
if (base.length != commonIndex) {
int numDirsUp = baseIsFile ? base.length - commonIndex - 1 : base.length - commonIndex;
for (int i = 0; i < numDirsUp; i++) {
relative.append("..").append(pathSeparator);
}
}
relative.append(normalizedTargetPath.substring(common.length()));
return new File(relative.toString());
}
public static File directoryOf(Class clazz) {
File location = locationOf(clazz);
if (location.isDirectory()) {
return location;
}
return location.getParentFile();
}
public static File locationOf(Class clazz) {
String path = clazz.getProtectionDomain().getCodeSource().getLocation().getPath();
path = path.contains("!") ? path.substring(0, path.lastIndexOf('!')) : path;
if (!path.isEmpty() && path.charAt(0) == '/') {
path = "file:" + path;
}
try {
return getRelativeFile(new File(new URL(path).toURI()), currentDir);
} catch (Exception e) {
Log.severe("", e);
return getRelativeFile(new File(path), currentDir);
}
}
}