FileLocator.resolve(url)的转义结果 [英] Escape result of FileLocator.resolve(url)
问题描述
方法 FileLocator.resolve(url)
可用于翻译地址 bundentry://something/somewhere/x.txt
到 /mnt/foo/somewhere/x.txt
的正确文件URL。
但是, https://bugs.eclipse.org/bugs/ show_bug.cgi?id = 145096 ,该URL不会转义。例如,如果包含引用的包的Eclipse安装在包含空格的目录中,则 FileLocator.resolve
返回的URL仍然包含空格并调用 url.toURI()
因此失败。
- 如何手动退出所有 URL中必需的字符?
- 如何根据相对于当前$的路径获取
文件
对象b $ b bundle?
作为参考,这里是尝试找到目录 dir时失败的代码如果该文件在包含空格的目录中,我的插件的
.jar
文件中
< pre class =lang-java prettyprint-override>
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(); //索引[...]路径中的非法字符
p>我刚找到这个代码:
相关行确实有帮助:
code> //我们需要使用URI的3-arg构造函数来正确转义文件系统字符。
URI resolvedUri = new URI(resolvedUrl.getProtocol(),resolvedUrl.getPath(),null);
The method FileLocator.resolve(url)
can be used to translate an address bundleentry://something/somewhere/x.txt
to a proper file URL for /mnt/foo/somewhere/x.txt
.
However, which is also documented at https://bugs.eclipse.org/bugs/show_bug.cgi?id=145096, the URL is not escaped. As an example, if the Eclipse installation containing the referenced bundle is in a directory containing a space, the URL returned by FileLocator.resolve
still contains the space and calling url.toURI()
fails because of that.
- How can I manually escape all necessary characters in the URL?
- How can I get a
File
object based on a path relative to the current bundle?
As reference, here is the code that fails when trying to find the directory dir
inside my plugin's .jar
file if that 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 [...]
I just found this code:
The relevant lines indeed 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);
这篇关于FileLocator.resolve(url)的转义结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!