package org.eclipse.core.internal.resources;
import java.io.IOException;
import java.net.*;
import org.eclipse.core.internal.boot.PlatformURLConnection;
import org.eclipse.core.internal.boot.PlatformURLHandler;
import org.eclipse.core.internal.utils.Messages;
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.osgi.util.NLS;
public class PlatformURLResourceConnection extends PlatformURLConnection {
public static final String RESOURCE = "resource";
public static final String RESOURCE_URL_STRING = PlatformURLHandler.PROTOCOL + PlatformURLHandler.PROTOCOL_SEPARATOR + '/' + RESOURCE + '/';
private static URL rootURL;
public PlatformURLResourceConnection(URL url) {
super(url);
}
@Override
protected boolean allowCaching() {
return false;
}
@Override
protected URL resolve() throws IOException {
String filePath = url.getFile().trim();
filePath = URLDecoder.decode(filePath, "UTF-8");
IPath spec = new Path(filePath).makeRelative();
if (!spec.segment(0).equals(RESOURCE))
throw new IOException(NLS.bind(Messages.url_badVariant, url));
int count = spec.segmentCount();
if (count == 1)
return rootURL;
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(spec.segment(1));
if (!project.exists()) {
String message = NLS.bind(Messages.url_couldNotResolve_projectDoesNotExist, project.getName(), url.toExternalForm());
throw new IOException(message);
}
IResource resource = null;
IPath result = null;
if (count == 2) {
resource = project;
} else {
spec = spec.removeFirstSegments(2);
resource = project.getFile(spec);
}
result = resource.getLocation();
if (result == null) {
URI uri = resource.getLocationURI();
if (uri != null) {
try {
URL url2 = uri.toURL();
if (url2.getProtocol().equals("file"))
return url2;
} catch (MalformedURLException e) {
String message = NLS.bind(Messages.url_couldNotResolve_URLProtocolHandlerCanNotResolveURL, uri.toString(), url.toExternalForm());
throw new IOException(message);
}
}
String message = NLS.bind(Messages.url_couldNotResolve_resourceLocationCanNotBeDetermined, resource.getFullPath(), url.toExternalForm());
throw new IOException(message);
}
return new URL("file", "", result.toString());
}
public static void startup(IPath root) {
if (rootURL != null)
return;
try {
rootURL = root.toFile().toURL();
} catch (MalformedURLException e) {
return;
}
PlatformURLHandler.register(RESOURCE, PlatformURLResourceConnection.class);
}
}