在Eclipse插件中读取静态文件 [英] Read static file in a Eclipse plugin

查看:47
本文介绍了在Eclipse插件中读取静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Eclipse插件,该插件必须读取XML文件才能进行XSLT转换.XML文件位于XSLT/目录中.我以这种方式获得路径:

I'm writing a Eclipse plugin that must read an XML file for a XSLT transformation. The XML file is located inside XSLT/ directory. I get the path in this way:

String fileXSL = "XSLT/file.xslt";

程序找不到文件.同样,这种方式也不起作用:

the program don't find the file. Also in this way it doesn't work:

this.getClass().getClassLoader().getResource("XSLT/file.xslt");

我已将此目录插入build.properties中,但问题仍然存在.有什么主意吗?

I've inserted this dir inside build.properties but the problem remains. Any idea?

推荐答案

使用 org.eclipse.core.runtime.FileLocator .

首先获取您的捆绑包:

Bundle bundle = Platform.getBundle("your plugin id");

捆绑包还可以在传递给激活器 start 方法的 BundleContext 中使用.

The Bundle is also available in the BundleContext passed to the activator start method.

您也可以使用

Bundle bundle = FrameworkUtil.getBundle(getClass());

获取当前课程的捆绑包.

to get the bundle for the current class.

然后您可以使用:

InputStream is = FileLocator.openStream(bundle, new Path("XSLT/file.xslt"), false);

URL eclipseURL = FileLocator.find(bundle, new Path("XSLT/file.xslt"), null);

URL fileURL = FileLocator.toFileURL(eclipseURL);

注意: FileLocator.find 中的URL可能使用内部Eclipse协议,因此需要由 FileLocator.toFileURL 进行转换.这可能会导致插件jar在一个临时位置被解压缩,从而可以返回文件" URL.

Note: The URL from FileLocator.find may use an internal Eclipse protocol so needs to be converted by FileLocator.toFileURL. This may cause the plugin jar to be unpacked in a temporary location so that a 'file' URL can be returned.

这篇关于在Eclipse插件中读取静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆