cucumber.runtime.arquillian.locator.JarLocation Maven / Gradle / Ivy
package cucumber.runtime.arquillian.locator;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
public final class JarLocation {
private JarLocation() {
// no-op
}
public static File jarLocation(final Class> clazz) {
try {
final String classFileName = clazz.getName().replace(".", "/") + ".class";
final ClassLoader loader = clazz.getClassLoader();
URL url;
if (loader != null) {
url = loader.getResource(classFileName);
} else {
url = clazz.getResource(classFileName);
}
if (url == null) {
throw new IllegalStateException("classloader.getResource(classFileName) returned a null URL");
}
if ("jar".equals(url.getProtocol())) {
final String spec = url.getFile();
int separator = spec.indexOf('!');
if (separator == -1) {
throw new MalformedURLException("no ! found in jar url spec:" + spec);
}
url = new URL(spec.substring(0, separator));
return new File(decode(url.getFile()));
} else if ("file".equals(url.getProtocol())) {
return toFile(classFileName, url);
} else {
throw new IllegalArgumentException("Unsupported URL scheme: " + url.toExternalForm());
}
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
private static File toFile(final String classFileName, final URL url) {
String path = url.getFile();
path = path.substring(0, path.length() - classFileName.length());
return new File(decode(path));
}
private static String decode(final String fileName) {
if (fileName.indexOf('%') == -1) {
return fileName;
}
final StringBuilder result = new StringBuilder(fileName.length());
final ByteArrayOutputStream out = new ByteArrayOutputStream();
for (int i = 0; i < fileName.length();) {
final char c = fileName.charAt(i);
if (c == '%') {
out.reset();
do {
if (i + 2 >= fileName.length()) {
throw new IllegalArgumentException("Incomplete % sequence at: " + i);
}
final int d1 = Character.digit(fileName.charAt(i + 1), 16);
final int d2 = Character.digit(fileName.charAt(i + 2), 16);
if (d1 == -1 || d2 == -1) {
throw new IllegalArgumentException("Invalid % sequence (" + fileName.substring(i, i + 3) + ") at: " + String.valueOf(i));
}
out.write((byte) ((d1 << 4) + d2));
i += 3;
} while (i < fileName.length() && fileName.charAt(i) == '%');
result.append(out.toString());
continue;
} else {
result.append(c);
}
i++;
}
return result.toString();
}
}