KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > runtime > PlatformURLFragmentConnection


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.runtime;
12
13 import java.io.IOException JavaDoc;
14 import java.net.URL JavaDoc;
15 import org.eclipse.core.internal.boot.PlatformURLConnection;
16 import org.eclipse.core.internal.boot.PlatformURLHandler;
17 import org.eclipse.osgi.util.NLS;
18 import org.osgi.framework.Bundle;
19
20 /**
21  * Platform URL support
22  * platform:/fragment/fragmentId/ maps to fragmentDescriptor.getInstallURLInternal()
23  */

24 public class PlatformURLFragmentConnection extends PlatformURLConnection {
25
26     private Bundle target = null;
27     private static boolean isRegistered = false;
28     public static final String JavaDoc FRAGMENT = "fragment"; //$NON-NLS-1$
29

30     /*
31      * Constructor for the class.
32      */

33     public PlatformURLFragmentConnection(URL JavaDoc url) {
34         super(url);
35     }
36
37     /* (non-Javadoc)
38      * @see org.eclipse.equinox.internal.url.PlatformURLConnection#allowCaching()
39      */

40     protected boolean allowCaching() {
41         return true;
42     }
43
44     /* (non-Javadoc)
45      * @see org.eclipse.equinox.internal.url.PlatformURLConnection#resolve()
46      */

47     protected URL JavaDoc resolve() throws IOException JavaDoc {
48         String JavaDoc spec = url.getFile().trim();
49         if (spec.startsWith("/")) //$NON-NLS-1$
50
spec = spec.substring(1);
51         if (!spec.startsWith(FRAGMENT))
52             throw new IOException JavaDoc(NLS.bind(CommonMessages.url_badVariant, url));
53         int ix = spec.indexOf("/", FRAGMENT.length() + 1); //$NON-NLS-1$
54
String JavaDoc ref = ix == -1 ? spec.substring(FRAGMENT.length() + 1) : spec.substring(FRAGMENT.length() + 1, ix);
55         String JavaDoc id = getId(ref);
56         Activator activator = Activator.getDefault();
57         if (activator == null)
58             throw new IOException JavaDoc(CommonMessages.activator_not_available);
59         target = activator.getBundle(id);
60         if (target == null)
61             throw new IOException JavaDoc(NLS.bind(CommonMessages.url_resolveFragment, url));
62         URL JavaDoc result = target.getEntry("/"); //$NON-NLS-1$
63
if (ix == -1 || (ix + 1) >= spec.length())
64             return result;
65         return new URL JavaDoc(result, spec.substring(ix + 1));
66     }
67
68     public static void startup() {
69         // register connection type for platform:/fragment handling
70
if (isRegistered)
71             return;
72         PlatformURLHandler.register(FRAGMENT, PlatformURLFragmentConnection.class);
73         isRegistered = true;
74     }
75 }
76
Popular Tags