KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > common > CommonPlugin


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: CommonPlugin.java,v 1.10 2005/06/12 13:24:00 emerks Exp $
16  */

17 package org.eclipse.emf.common;
18
19
20 import java.io.IOException JavaDoc;
21 import java.net.URL JavaDoc;
22
23 import org.eclipse.core.runtime.Platform;
24 import org.eclipse.emf.common.util.ResourceLocator;
25 import org.eclipse.emf.common.util.URI;
26
27
28 /**
29  * The <b>Plugin</b> for the model EMF.Common library.
30  * EMF must run
31  * within an Eclipse workbench,
32  * within a headless Eclipse workspace,
33  * or just stand-alone as part of some other application.
34  * To support this, all resource access should be directed to the resource locator,
35  * which can redirect the service as appopriate to the runtime.
36  * During stand-alone invocation no plugin initialization takes place.
37  * In this case, common.resources.jar must be on the CLASSPATH.
38  * @see #INSTANCE
39  */

40 public final class CommonPlugin extends EMFPlugin
41 {
42   /**
43    * The singleton instance of the plugin.
44    */

45   public static final CommonPlugin INSTANCE = new CommonPlugin();
46
47   /**
48    * The one instance of this class.
49    */

50   private static Implementation plugin;
51
52   /**
53    * Creates the singleton instance.
54    */

55   private CommonPlugin()
56   {
57     super(new ResourceLocator[] {});
58   }
59
60   /*
61    * Javadoc copied from base class.
62    */

63   public ResourceLocator getPluginResourceLocator()
64   {
65     return plugin;
66   }
67
68   /**
69    * Returns the singleton instance of the Eclipse plugin.
70    * @return the singleton instance.
71    */

72   public static Implementation getPlugin()
73   {
74     return plugin;
75   }
76
77   /**
78    * Use the platform, if available, to convert to a local URI.
79    */

80   public static URI asLocalURI(URI uri)
81   {
82     return plugin == null ? uri : Implementation.asLocalURI(uri);
83   }
84
85   /**
86    * Use the platform, if available, to resolve the URL.
87    */

88   public static URI resolve(URI uri)
89   {
90     return plugin == null ? uri : Implementation.resolve(uri);
91   }
92
93   /**
94    * Use the platform, if available, to load the named class using the right class loader.
95    */

96   public static Class JavaDoc loadClass(String JavaDoc pluginID, String JavaDoc className) throws ClassNotFoundException JavaDoc
97   {
98     return plugin == null ? Class.forName(className) : Implementation.loadClass(pluginID, className);
99   }
100
101   /**
102    * The actual implementation of the Eclipse <b>Plugin</b>.
103    */

104   public static class Implementation extends EclipsePlugin
105   {
106     /**
107      * Creates an instance.
108      */

109     public Implementation()
110     {
111       super();
112
113       // Remember the static instance.
114
//
115
plugin = this;
116     }
117
118     /**
119      * Use the platform to convert to a local URI.
120      */

121     protected static URI asLocalURI(URI uri)
122     {
123       try
124       {
125         String JavaDoc fragment = uri.fragment();
126         URL JavaDoc url = Platform.asLocalURL(new URL JavaDoc(uri.trimFragment().toString()));
127         return fix(url, fragment);
128       }
129       catch (IOException JavaDoc exception)
130       {
131       }
132       return uri;
133     }
134
135     /**
136      * Use the platform to convert to a local URI.
137      */

138     protected static URI resolve(URI uri)
139     {
140       String JavaDoc fragment = uri.fragment();
141       URI uriWithoutFragment = uri.trimFragment();
142       String JavaDoc uriWithoutFragmentToString = uriWithoutFragment.toString();
143       
144       URL JavaDoc url = null;
145       try
146       {
147         url = Platform.resolve(new URL JavaDoc(uriWithoutFragmentToString));
148       }
149       catch (IOException JavaDoc exception1)
150       {
151         // Platform.resolve() doesn't work if the project is encoded.
152
//
153
try
154         {
155           uriWithoutFragmentToString = URI.decode(uriWithoutFragmentToString);
156           url = Platform.resolve(new URL JavaDoc(uriWithoutFragmentToString));
157         }
158         catch (IOException JavaDoc exception2)
159         {
160         }
161       }
162       if (url != null)
163       {
164         try
165         {
166           return fix(url, fragment);
167         }
168         catch (IOException JavaDoc exception)
169         {
170         }
171       }
172       
173       return uri;
174     }
175
176     protected static URI fix(URL JavaDoc url, String JavaDoc fragment) throws IOException JavaDoc
177     {
178       // Only file-scheme URIs will be re-encoded. If a URI was decoded in the workaround
179
// above, and Platform.resolve() didn't return a file-scheme URI, then this will return
180
// an decoded URI.
181
//
182
URI result =
183         "file".equalsIgnoreCase(url.getProtocol()) ?
184           URI.createFileURI(URI.decode(url.getFile())) :
185           URI.createURI(url.toString());
186       if (fragment != null)
187       {
188         result = result.appendFragment(fragment);
189       }
190       return result;
191     }
192     
193     /**
194      * Use the platform to load the named class using the right class loader.
195      */

196     public static Class JavaDoc loadClass(String JavaDoc pluginID, String JavaDoc className) throws ClassNotFoundException JavaDoc
197     {
198       return Platform.getBundle(pluginID).loadClass(className);
199     }
200   }
201 }
202
Popular Tags