KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > resources > PlatformURLResourceConnection


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.resources;
12
13 import java.io.IOException JavaDoc;
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 /**
25  * Platform URL support
26  * platform:/resource/<path>/<resource> maps to resource in current workspace
27  */

28 public class PlatformURLResourceConnection extends PlatformURLConnection {
29
30     // resource/ protocol
31
public static final String JavaDoc RESOURCE = "resource"; //$NON-NLS-1$
32
public static final String JavaDoc 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; // don't cache, workspace is local
41
}
42
43     protected URL resolve() throws IOException JavaDoc {
44         String JavaDoc filePath = url.getFile().trim();
45         filePath = URLDecoder.decode(filePath, "UTF-8"); //$NON-NLS-1$
46
IPath spec = new Path(filePath).makeRelative();
47         if (!spec.segment(0).equals(RESOURCE))
48             throw new IOException JavaDoc(NLS.bind(Messages.url_badVariant, url));
49         int count = spec.segmentCount();
50         // if there is only one segment then we are talking about the workspace root.
51
if (count == 1)
52             return rootURL;
53         // if there are two segments then the second is a project name.
54
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(spec.segment(1));
55         if (!project.exists()) {
56             String JavaDoc message = NLS.bind(Messages.url_couldNotResolve, project.getName(), url.toExternalForm());
57             throw new IOException JavaDoc(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()); //$NON-NLS-1$ //$NON-NLS-2$
67
}
68
69     /**
70      * This method is called during resource plugin startup() initialization.
71      *
72      * @param root URL to the root of the current workspace.
73      */

74     public static void startup(IPath root) {
75         // register connection type for platform:/resource/ handling
76
if (rootURL != null)
77             return;
78         try {
79             rootURL = root.toFile().toURL();
80         } catch (MalformedURLException e) {
81             // should never happen but if it does, the resource URL cannot be supported.
82
return;
83         }
84         PlatformURLHandler.register(RESOURCE, PlatformURLResourceConnection.class);
85     }
86 }
87
Popular Tags