cucumber.io.ClasspathIterable Maven / Gradle / Ivy
package cucumber.io;
import cucumber.runtime.CucumberException;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Enumeration;
import java.util.Iterator;
class ClasspathIterable implements Iterable {
private final ClassLoader cl;
private final String path;
private final String suffix;
public ClasspathIterable(ClassLoader cl, String path, String suffix) {
this.cl = cl;
this.path = path;
this.suffix = suffix;
}
@Override
public Iterator iterator() {
try {
FlatteningIterator iterator = new FlatteningIterator();
Enumeration resources = cl.getResources(path);
while (resources.hasMoreElements()) {
URL url = resources.nextElement();
if (url.getProtocol().equals("jar")) {
String jarPath = filePath(url);
iterator.push(new ZipResourceIterator(jarPath, path, suffix));
} else {
File file = new File(getPath(url));
File rootDir = new File(file.getAbsolutePath().substring(0, file.getAbsolutePath().length() - path.length()));
iterator.push(new FileResourceIterator(rootDir, file, suffix));
}
}
return iterator;
} catch (IOException e) {
throw new CucumberException(e);
}
}
static String filePath(URL jarUrl) throws UnsupportedEncodingException, MalformedURLException {
String path = new File(new URL(jarUrl.getFile()).getFile()).getAbsolutePath();
String pathToJar = path.substring(0, path.indexOf("!"));
return URLDecoder.decode(pathToJar, "UTF-8");
}
static boolean hasSuffix(String suffix, String name) {
return suffix == null || name.endsWith(suffix);
}
private static String getPath(URL url) {
try {
return URLDecoder.decode(url.getPath(), "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new CucumberException("Encoding problem", e);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy