Platform URLs in Eclipse
Frequently people wonder how to "locate" files in Eclipse. In 80% of the cases, the
conversation is about things like
IFile
and
IWorkspace
. Sometimes it digresses
to files available inside bundles and, rarely, it involves the "state location" of a bundle
(see the
Plugin.getStateLocation()
javadoc for more details). Obviously you can use
IResource
or java.io.File
to work with files in these places. There is another way, though: platform URI.
There are a few ways to work with the "platform" scheme:
platform:/resource |
It is used to identify a resource located in the workspace. The next path
segment after "resource" should be the name of a project, which can be
followed by the folder and/or file we want to locate. |
platform:/plugin |
It
is used to locate a resource available in a plug-in/bundle. One really cool thing about this one is that it doesn't really
matter if this resource is available in a directory or in a jar file.
It also doesn't matter if the bundle is installed in a link folder or in the default directory.
The path segment after "plugin" should be the identifier of the bundle,
which can be followed by the path of the resource in the bundle.
|
platform:/fragment |
This
one is quite similar to "platform:/plugin", being used to locate
fragment resources instead of bundle resources. As you are probably
guessing, the segment after "fragment" should be the fragment's
identifier. |
platform:/meta |
We can
use this to access a bundle's stage location. The path segment after
"meta" should be the bundle's identifier, followed by the path of the
resource we want to refer to. |
platform:/config |
The "config" segment causes the
platform URI to refer to the configuration area of the running Eclipse (usually the eclipse/configuration directory). This can
be useful to read the config.ini file, for example. |
platform:/base |
This always
refers to the directory of the Eclipse being executed.
It is interesting to note that, for example,
platform:/base/plugins/org.eclipse.mybundle/plugin.xml and platform:/plugin/org.eclipse.mybundle/plugin.xml
don't necessarily refer to the same resource. The former is a "pointer" to a plugin.xml file located in a directory
plugins/org.eclipse.mybundle under the directory that Eclipse is installed. The latter points to the plugin.xml of the
"org.eclipse.mybundle" bundle regardless of where it is installed and whether it is jarred or not. |
So what can we do with platform URIs? For one, read the contents of the
resources pointed by them. We may also be able to write to such
resources or even delete or create them.
URIs can be a good fit for APIs that would normally use "plain" paths. Take as
an example the icon
attribute of the extension point below. Because its value is handled as
a URI, we are allowed to refer to an image located in a different
bundle.
<extension point="org.eclipse.ui.editorActions">
<editorContribution ...>
<action
icon="platform:/plugin/com.myplugin/icons/me.gif"
...
/>
</editorContribution>
</extension>