package org.eclipse.osgi.internal.location;
import java.io.*;
import java.net.*;
import org.eclipse.osgi.internal.location.Locker.MockLocker;
public class LocationHelper {
public static final String PROP_OSGI_LOCKING = "osgi.locking";
public static final String LOCKING_NONE = "none";
public static final String LOCKING_IO = "java.io";
public static final String LOCKING_NIO = "java.nio";
@SuppressWarnings("deprecation")
public static URL buildURL(String spec, boolean trailingSlash) {
if (spec == null)
return null;
if (File.separatorChar == '\\')
spec = spec.trim();
boolean isFile = spec.startsWith("file:");
try {
if (isFile)
return adjustTrailingSlash(new File(spec.substring(5)).toURL(), trailingSlash);
return new URL(spec);
} catch (MalformedURLException e) {
if (isFile)
return null;
try {
return adjustTrailingSlash(new File(spec).toURL(), trailingSlash);
} catch (MalformedURLException e1) {
return null;
}
}
}
private static URL adjustTrailingSlash(URL url, boolean trailingSlash) throws MalformedURLException {
String file = url.getPath();
if (trailingSlash == (file.endsWith("/")))
return url;
file = trailingSlash ? file + "/" : file.substring(0, file.length() - 1);
return new URL(url.getProtocol(), url.getHost(), file);
}
public static Locker createLocker(File lock, String lockMode, boolean debug) {
if (lockMode == null) {
lockMode = System.getProperty(PROP_OSGI_LOCKING);
}
if (LOCKING_NONE.equals(lockMode)) {
return new MockLocker();
}
if (LOCKING_IO.equals(lockMode)) {
return new Locker_JavaIo(lock);
}
if (LOCKING_NIO.equals(lockMode)) {
return new Locker_JavaNio(lock, debug);
}
return new Locker_JavaNio(lock, debug);
}
public static InputStream getStream(URL location) throws IOException {
if ("file".equalsIgnoreCase(location.getProtocol())) {
File f = new File(location.getPath());
if (f.exists()) {
return new FileInputStream(f);
}
}
return location.openStream();
}
public static URLConnection getConnection(URL url) throws IOException {
if ("file".equalsIgnoreCase(url.getProtocol())) {
try {
return url.openConnection();
} catch (IllegalArgumentException e) {
File f = new File(url.getPath());
if (f.exists()) {
return f.toURI().toURL().openConnection();
}
}
}
return url.openConnection();
}
public static File decodePath(File file) {
if (!file.exists() && (file.getPath().indexOf('%') >= 0 || file.getPath().indexOf('+') >= 0)) {
String absolute = file.getAbsolutePath();
String decodePath = LocationHelper.decode(absolute, true);
File f = new File(decodePath);
if (f.exists()) {
return f;
}
decodePath = LocationHelper.decode(absolute, false);
f = new File(decodePath);
if (f.exists()) {
return f;
}
}
return file;
}
public static String decode(String urlString, boolean plusEncoded) {
if (plusEncoded && urlString.indexOf('+') >= 0) {
int len = urlString.length();
StringBuilder buf = new StringBuilder(len);
for (int i = 0; i < len; i++) {
char c = urlString.charAt(i);
if (c == '+')
buf.append("%2B");
else
buf.append(c);
}
urlString = buf.toString();
}
try {
return URLDecoder.decode(urlString, "UTF-8");
} catch (UnsupportedEncodingException | RuntimeException e) {
return urlString;
}
}
}