This commit is contained in:
jendib 2013-08-26 22:03:14 +02:00
parent 6e3d2ea972
commit ac3580fb4a

View File

@ -1,97 +1,102 @@
package com.sismics.util; package com.sismics.util;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import java.io.File; import java.io.File;
import java.io.FilenameFilter; import java.io.FilenameFilter;
import java.io.IOException; import java.io.IOException;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.*; import java.util.*;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
import java.util.jar.JarFile; import java.util.jar.JarFile;
/** /**
* Resource utilities. * Resource utilities.
* *
* @author jtremeaux * @author jtremeaux
*/ */
public class ResourceUtil { public class ResourceUtil {
/** /**
* List files inside a directory. The path can be a directory on the filesystem, or inside a JAR. * List files inside a directory. The path can be a directory on the filesystem, or inside a JAR.
* *
* @param clazz Class * @param clazz Class
* @param path Path * @param path Path
* @param filter Filter * @param filter Filter
* @return List of files * @return List of files
* @throws URISyntaxException * @throws URISyntaxException
* @throws IOException * @throws IOException
*/ */
public static List<String> list(Class<?> clazz, String path, FilenameFilter filter) throws URISyntaxException, IOException { public static List<String> list(Class<?> clazz, String path, FilenameFilter filter) throws URISyntaxException, IOException {
// Path is a directory on the filesystem // Path is a directory on the filesystem
URL dirUrl = clazz.getResource(path); URL dirUrl = clazz.getResource(path);
if (dirUrl != null && dirUrl.getProtocol().equals("file")) { if (dirUrl != null && dirUrl.getProtocol().equals("file")) {
return Arrays.asList(new File(dirUrl.toURI()).list(filter)); return Arrays.asList(new File(dirUrl.toURI()).list(filter));
} }
// Path is a directory inside the same JAR as clazz // Path is a directory inside the same JAR as clazz
if (dirUrl == null) { if (dirUrl == null) {
String className = clazz.getName().replace(".", "/") + ".class"; String className = clazz.getName().replace(".", "/") + ".class";
dirUrl = clazz.getClassLoader().getResource(className); dirUrl = clazz.getClassLoader().getResource(className);
} }
if (dirUrl.getProtocol().equals("jar")) { if (dirUrl.getProtocol().equals("jar")) {
if (path.startsWith("/")) { if (path.startsWith("/")) {
path = path.substring(1); path = path.substring(1);
} }
if (!path.endsWith("/")) { if (!path.endsWith("/")) {
path = path + "/"; path = path + "/";
} }
// Extract the JAR path // Extract the JAR path
String jarPath = dirUrl.getPath().substring(5, dirUrl.getPath().indexOf("!")); String jarPath = dirUrl.getPath().substring(5, dirUrl.getPath().indexOf("!"));
JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8")); JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
Set<String> fileSet = new HashSet<String>();
Enumeration<JarEntry> entries = jar.entries();
Set<String> fileSet = new HashSet<String>(); try {
while (entries.hasMoreElements()) { Enumeration<JarEntry> entries = jar.entries();
// Filter according to the path while (entries.hasMoreElements()) {
String entryName = entries.nextElement().getName(); // Filter according to the path
if (!entryName.startsWith(path)) { String entryName = entries.nextElement().getName();
continue; if (!entryName.startsWith(path)) {
} continue;
String name = entryName.substring(path.length()); }
if (!"".equals(name)) { String name = entryName.substring(path.length());
// If it is a subdirectory, just return the directory name if (!"".equals(name)) {
int checkSubdir = name.indexOf("/"); // If it is a subdirectory, just return the directory name
if (checkSubdir >= 0) { int checkSubdir = name.indexOf("/");
name = name.substring(0, checkSubdir); if (checkSubdir >= 0) {
} name = name.substring(0, checkSubdir);
}
if (filter == null || filter.accept(null, name)) {
fileSet.add(name); if (filter == null || filter.accept(null, name)) {
} fileSet.add(name);
} }
} }
return Lists.newArrayList(fileSet); }
} } finally {
jar.close();
throw new UnsupportedOperationException(MessageFormat.format("Cannot list files for URL {0}", dirUrl)); }
}
return Lists.newArrayList(fileSet);
/** }
* List files inside a directory. The path can be a directory on the filesystem, or inside a JAR.
* throw new UnsupportedOperationException(MessageFormat.format("Cannot list files for URL {0}", dirUrl));
* @param clazz Class }
* @param path Path
* @return List of files /**
* @throws URISyntaxException * List files inside a directory. The path can be a directory on the filesystem, or inside a JAR.
* @throws IOException *
*/ * @param clazz Class
public static List<String> list(Class<?> clazz, String path) throws URISyntaxException, IOException { * @param path Path
return list(clazz, path, null); * @return List of files
} * @throws URISyntaxException
} * @throws IOException
*/
public static List<String> list(Class<?> clazz, String path) throws URISyntaxException, IOException {
return list(clazz, path, null);
}
}