package org.eclipse.core.internal.runtime;
import java.io.IOException;
import java.net.URL;
import org.eclipse.core.internal.boot.PlatformURLConnection;
import org.eclipse.core.internal.boot.PlatformURLHandler;
import org.eclipse.osgi.util.NLS;
import org.osgi.framework.Bundle;
public class PlatformURLFragmentConnection extends PlatformURLConnection {
private Bundle target = null;
private static boolean isRegistered = false;
public static final String FRAGMENT = "fragment";
public PlatformURLFragmentConnection(URL url) {
super(url);
}
@Override
protected boolean allowCaching() {
return true;
}
@Override
protected URL resolve() throws IOException {
String spec = url.getFile().trim();
if (spec.startsWith("/"))
spec = spec.substring(1);
if (!spec.startsWith(FRAGMENT))
throw new IOException(NLS.bind(CommonMessages.url_badVariant, url));
int ix = spec.indexOf('/', FRAGMENT.length() + 1);
String ref = ix == -1 ? spec.substring(FRAGMENT.length() + 1) : spec.substring(FRAGMENT.length() + 1, ix);
String id = getId(ref);
Activator activator = Activator.getDefault();
if (activator == null)
throw new IOException(CommonMessages.activator_not_available);
target = activator.getBundle(id);
if (target == null)
throw new IOException(NLS.bind(CommonMessages.url_resolveFragment, url));
URL result = target.getEntry("/");
if (ix == -1 || (ix + 1) >= spec.length())
return result;
return new URL(result, spec.substring(ix + 1));
}
public static void startup() {
if (isRegistered)
return;
PlatformURLHandler.register(FRAGMENT, PlatformURLFragmentConnection.class);
isRegistered = true;
}
}