1 11 package org.eclipse.core.internal.resources; 12 13 import java.io.IOException ; 14 import java.net.*; 15 import org.eclipse.core.internal.boot.PlatformURLConnection; 16 import org.eclipse.core.internal.boot.PlatformURLHandler; 17 import org.eclipse.core.internal.utils.Messages; 18 import org.eclipse.core.resources.IProject; 19 import org.eclipse.core.resources.ResourcesPlugin; 20 import org.eclipse.core.runtime.IPath; 21 import org.eclipse.core.runtime.Path; 22 import org.eclipse.osgi.util.NLS; 23 24 28 public class PlatformURLResourceConnection extends PlatformURLConnection { 29 30 public static final String RESOURCE = "resource"; public static final String RESOURCE_URL_STRING = PlatformURLHandler.PROTOCOL + PlatformURLHandler.PROTOCOL_SEPARATOR + '/' + RESOURCE + '/'; 33 private static URL rootURL; 34 35 public PlatformURLResourceConnection(URL url) { 36 super(url); 37 } 38 39 protected boolean allowCaching() { 40 return false; } 42 43 protected URL resolve() throws IOException { 44 String filePath = url.getFile().trim(); 45 filePath = URLDecoder.decode(filePath, "UTF-8"); IPath spec = new Path(filePath).makeRelative(); 47 if (!spec.segment(0).equals(RESOURCE)) 48 throw new IOException (NLS.bind(Messages.url_badVariant, url)); 49 int count = spec.segmentCount(); 50 if (count == 1) 52 return rootURL; 53 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(spec.segment(1)); 55 if (!project.exists()) { 56 String message = NLS.bind(Messages.url_couldNotResolve, project.getName(), url.toExternalForm()); 57 throw new IOException (message); 58 } 59 IPath result = null; 60 if (count == 2) 61 result = project.getLocation(); 62 else { 63 spec = spec.removeFirstSegments(2); 64 result = project.getFile(spec).getLocation(); 65 } 66 return new URL("file", "", result.toString()); } 68 69 74 public static void startup(IPath root) { 75 if (rootURL != null) 77 return; 78 try { 79 rootURL = root.toFile().toURL(); 80 } catch (MalformedURLException e) { 81 return; 83 } 84 PlatformURLHandler.register(RESOURCE, PlatformURLResourceConnection.class); 85 } 86 } 87 | Popular Tags |