PathUtil.java
4.63 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Node
* javax.jcr.RepositoryException
*/
package com.day.jcr.vault.util;
import com.day.jcr.vault.util.Constants;
import com.day.jcr.vault.util.Text;
import java.io.File;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
public class PathUtil {
public static String[] makePath(String[] parent, String path) {
if (path == null || path.equals("") || path.equals(".")) {
return parent;
}
boolean isAbsolute = false;
String[] composed = Text.explode(path, 47);
if (path.charAt(0) == '/') {
isAbsolute = true;
} else if (parent != null && parent.length > 0) {
int off = 0;
if (parent[0].equals("/")) {
isAbsolute = true;
off = 1;
}
String[] c = new String[parent.length - off + composed.length];
System.arraycopy(parent, off, c, 0, parent.length - off);
System.arraycopy(composed, 0, c, parent.length - off, composed.length);
composed = c;
}
int dst = 0;
boolean startsWithParent = false;
for (int i = 0; i < composed.length; ++i) {
String element = composed[i];
if (element.equals(".")) continue;
if (element.equals("..")) {
if (dst == 0) {
startsWithParent = true;
}
if (startsWithParent) {
composed[dst++] = element;
continue;
}
--dst;
continue;
}
composed[dst++] = element;
}
if (isAbsolute) {
String[] ret = new String[dst + 1];
System.arraycopy(composed, 0, ret, 1, dst);
ret[0] = "/";
return ret;
}
if (dst == composed.length) {
return composed;
}
String[] ret = new String[dst];
System.arraycopy(composed, 0, ret, 0, dst);
return ret;
}
public static String makePath(String parent, String relPath) {
String[] ret = PathUtil.makePath(Text.explode(parent, 47), relPath);
return "/" + Text.implode(ret, "/");
}
public static File getRelativeFile(File parent, File file) {
return new File(PathUtil.getRelativeFilePath(parent.getPath(), file.getPath()));
}
public static String getRelativePath(String parent, String path) {
return PathUtil.getRelativeFilePath(parent, path, "/");
}
public static String getRelativeFilePath(String cwd, String path) {
return PathUtil.getRelativeFilePath(cwd, path, Constants.FS_NATIVE);
}
public static String getRelativeFilePath(String cwd, String path, String separator) {
int i;
if (cwd.equals(path)) {
return ".";
}
if (!(path.startsWith(separator) || path.length() >= 2 && path.charAt(1) == ':' && path.charAt(2) == '\\')) {
return path;
}
String[] p1 = Text.explode(cwd, separator.charAt(0));
String[] p2 = Text.explode(path, separator.charAt(0));
for (i = 0; i < p1.length && i < p2.length && p1[i].equals(p2[i]); ++i) {
}
StringBuffer buf = new StringBuffer();
String delim = "";
for (int j = i; j < p1.length; ++j) {
buf.append(delim).append("..");
delim = separator;
}
while (i < p2.length) {
buf.append(delim).append(p2[i++]);
delim = separator;
}
return buf.toString();
}
public static String append(String parent, String relPath) {
if (relPath == null || relPath.length() == 0) {
return parent == null ? "" : parent;
}
StringBuffer ret = new StringBuffer();
if (parent != null) {
ret.append(parent);
}
if (ret.length() > 0 && ret.charAt(ret.length() - 1) != '/') {
ret.append('/');
}
ret.append(relPath);
return ret.toString();
}
public static int getDepth(String path) {
int len = path.length();
if (len <= 1) {
return 0;
}
int depth = 1;
for (int i = 1; i < len; ++i) {
if (path.charAt(i) != '/') continue;
++depth;
}
return depth;
}
public static String getPath(Node parent, String relPath) throws RepositoryException {
String path = parent.getPath();
if (relPath.length() > 0) {
path = path.endsWith("/") ? path + relPath : path + "/" + relPath;
}
return path;
}
}