-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
AbstractPathResourceAccessor.java
128 lines (101 loc) · 4.2 KB
/
AbstractPathResourceAccessor.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
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
package liquibase.resource;
import liquibase.Scope;
import liquibase.logging.Logger;
import liquibase.util.FileUtil;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;
public abstract class AbstractPathResourceAccessor extends AbstractResourceAccessor {
abstract protected Path getRootPath();
@Override
public String toString() {
return getClass().getName() + " (" + getRootPath() + ")";
}
@Override
public List<String> describeLocations() {
return Collections.singletonList(getRootPath().toString());
}
@Override
public List<Resource> getAll(String path) throws IOException {
if (path == null) {
throw new IllegalArgumentException("Path must not be null");
}
final List<Resource> returnList = new ArrayList<>();
Logger log = Scope.getCurrentScope().getLog(getClass());
path = standardizePath(path);
if (path == null) {
return returnList;
}
Path finalPath = getRootPath().resolve(path);
if (Files.exists(finalPath)) {
returnList.add(createResource(finalPath, path));
} else {
log.fine("Path " + path + " in " + getRootPath() + " does not exist");
}
return returnList;
}
private String standardizePath(String path) {
try {
//
// Flip the separators to Linux-style and replace the first separator
// If then root path is the absolute path for Linux or Windows then return that result
//
String rootPath = getRootPath().toString();
String result = new File(path).toPath().normalize().toString().replace("\\", "/").replaceFirst("^/", "");
if (rootPath.equals("/") || rootPath.equals("\\")) {
return result;
}
//
// Strip off any Windows drive prefix and return the result
//
return result.replaceFirst("^\\w:/","");
} catch (InvalidPathException e) {
Scope.getCurrentScope().getLog(getClass()).warning("Failed to standardize path " + path, e);
return path;
}
}
@Override
public List<Resource> search(String startPath, boolean recursive) throws IOException {
if (startPath == null) {
throw new IllegalArgumentException("Path must not be null");
}
startPath = startPath
.replaceFirst("^file:/+", "")
.replaceFirst("^/", "");
Logger log = Scope.getCurrentScope().getLog(getClass());
Path rootPath = getRootPath();
Path basePath = rootPath.resolve(startPath);
final List<Resource> returnSet = new ArrayList<>();
if (!Files.exists(basePath)) {
log.fine("Path " + startPath + " in " + rootPath + " does not exist");
return returnSet;
}
if (!Files.isDirectory(basePath)) {
throw new IOException("'" + startPath + "' is a file, not a directory");
}
SimpleFileVisitor<Path> fileVisitor = new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (attrs.isRegularFile()) {
addToReturnList(file);
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
private void addToReturnList(Path file) {
String pathToAdd = rootPath.relativize(file).normalize().toString().replace("\\", "/");
pathToAdd = pathToAdd.replaceFirst("/$", "");
returnSet.add(createResource(file, pathToAdd));
}
};
int maxDepth = recursive ? Integer.MAX_VALUE : 1;
Files.walkFileTree(basePath, Collections.singleton(FileVisitOption.FOLLOW_LINKS), maxDepth, fileVisitor);
return returnSet;
}
protected abstract Resource createResource(Path file, String pathToAdd);
}