Escape result from FileLocator.resolve (url)

FileLocator.resolve(url) can use the FileLocator.resolve(url) method to translate the bundleentry://something/somewhere/x.txt to the correct file URL for /mnt/foo/somewhere/x.txt .

However, as also described in https://bugs.eclipse.org/bugs/show_bug.cgi?id=145096 , the URL is not escaped. As an example, if an Eclipse installation containing a reference package is in a directory containing a space, the URL returned by FileLocator.resolve still contains a space, and the url.toURI() call fails because of this.

  • How can I manually remove all the required characters in the URL?
  • How can I get a File object based on the path relative to the current bundle?

As a reference, here is the code that crashes when trying to find the dir directory inside my .jar plugin file if this file is in a directory containing a space:

  final IPath pathOfExampleProject = new Path("dir"); final Bundle bundle = Platform.getBundle(AproveIDs.PLUGIN_ID); final URL url = FileLocator.find(bundle, pathOfExampleProject, null); final URL url2 = FileLocator.toFileURL(url); url2.toURI(); // Illegal character in path at index [...] 
+8
eclipse eclipse-plugin
source share
2 answers

I just found this code:

http://code.google.com/p/dart/source/browse/branches/bleeding_edge/dart/editor/tools/plugins/com.google.dart.tools.core/src/com/google/dart/tools/ core / internal / model / BundledSystemLibrary.java? r = 2057

The relevant lines really help:

 // We need to use the 3-arg constructor of URI in order to properly escape file system chars. URI resolvedUri = new URI(resolvedUrl.getProtocol(), resolvedUrl.getPath(), null); 
+7
source share

Two additional notes:

  • FileLocator.resolve does resolve the URL, but it does not necessarily return the file: / URL. In the case when your package is packaged (in .jar), you should use FileLocator.toFileURL, which automatically extracts the resource to the cache if necessary.
  • since Eclipse 4.x now includes a common EMF API by default, you can simply avoid the URL of the API URI API as follows:

URI resolvedUri = URI.createFileURI(resolved.getPath());

To get the file name, call resolvedUri.toFileString();

+1
source share

All Articles