FileLocator.resolve(url)的转义结果

C-O*_*tto 8 eclipse eclipse-plugin

该方法FileLocator.resolve(url)可用于将地址bundleentry://something/somewhere/x.txt转换为适当的文件URL /mnt/foo/somewhere/x.txt.

但是,这也记录在https://bugs.eclipse.org/bugs/show_bug.cgi?id=145096,URL不会被转义.例如,如果包含引用的bundle的Eclipse安装位于包含空格的目录中,则返回的URL FileLocator.resolve仍包含空格,因此调用url.toURI()失败.

  • 如何手动转义URL中的所有必要字符?
  • 如何File根据相对于当前包的路径获取对象?

作为参考,如果该文件位于包含空格的目录中,则尝试查找dir插件.jar文件中的目录时,代码会失败:

    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 [...]
Run Code Online (Sandbox Code Playgroud)

C-O*_*tto 7

我刚发现这段代码:

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/芯/内部/模型/ BundledSystemLibrary.java R = 2057

相关的行确实有帮助:

// 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);
Run Code Online (Sandbox Code Playgroud)