-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathResourceUtil.java
More file actions
87 lines (73 loc) · 3.11 KB
/
ResourceUtil.java
File metadata and controls
87 lines (73 loc) · 3.11 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
package org.utplsql.api;
import com.sun.nio.zipfs.ZipPath;
import org.utplsql.api.reporter.CoverageHTMLReporter;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
* Helper class for dealing with Resources
*
* @author pesse
*/
public class ResourceUtil {
private ResourceUtil() {
}
/**
* Returns the Path to a resource so it is walkable no matter if it's inside a jar or on the file system
*
* @param resourceName The name of the resource
* @return Path to the resource, either in JAR or on file system
* @throws IOException
* @throws URISyntaxException
*/
public static Path getPathToResource(String resourceName) throws IOException, URISyntaxException {
URI uri = CoverageHTMLReporter.class.getResource(resourceName).toURI();
Path myPath;
if (uri.getScheme().equalsIgnoreCase("jar")) {
FileSystem fileSystem = FileSystems.newFileSystem(uri, Collections.emptyMap());
myPath = fileSystem.getPath(resourceName);
} else {
myPath = Paths.get(uri);
}
return myPath;
}
/**
* Returns the relative paths of all children of the given resource. Relative path begins from the first atom of the given path.
*
* @param resourceAsPath The resource to get children from
* @param filesOnly If set to true it will only return files, not directories
* @return List of relative Paths to the children
* @throws IOException
* @throws URISyntaxException
*/
public static List<Path> getListOfChildren(Path resourceAsPath, boolean filesOnly) throws IOException, URISyntaxException {
Path resourcePath = getPathToResource("/" + resourceAsPath.toString());
int relativeStartIndex = resourcePath.getNameCount() - resourceAsPath.getNameCount();
final List<Path> result = new ArrayList<>();
if (resourcePath instanceof ZipPath) {
try (ZipFile zf = new ZipFile(resourcePath.getFileSystem().toString())) {
for (Enumeration list = zf.entries(); list.hasMoreElements(); ) {
ZipEntry entry = (ZipEntry) list.nextElement();
// Get entry-path with root element so we can compare it
Path entryPath = resourcePath.getRoot().resolve(resourcePath.getFileSystem().getPath(entry.toString()));
if (entryPath.startsWith(resourcePath) && (!filesOnly || !entry.isDirectory())) {
result.add(entryPath.subpath(relativeStartIndex, entryPath.getNameCount()));
}
}
}
resourcePath.getFileSystem().close();
} else {
Files.walk(resourcePath)
.filter(p -> !filesOnly || p.toFile().isFile())
.forEach(p -> result.add(p.subpath(relativeStartIndex, p.getNameCount())));
}
return result;
}
}