2012-02-09 70 views
1

我在正在运行的eclipse中安装了一个bundle(org.osgi.framework.Bundle)。这个包中有一个文件。我有文件的路径,我可以用URL(java.net.URL)使用URL url = bundle.getEntry("/folder/file")来表示这个文件。将位于OSGi Bundle的文件转换为IFile

有没有办法得到这个文件的类型IFile(org.eclipse.core.resources.IFile)?

我需要位于安装的类型为IFile的osgi包中的文件的引用。但我不要想要在我的本地磁盘上临时复制文件(如工作区)。

在此先感谢!

回答

2

这很困难。 IFile表示实际文件,而不是归档中的条目。您需要为存档构建一个Eclipse文件系统(EFS)表示,但这可能需要很多工作。

你想达到什么目的?有可能你做的事情要简单得多。

+0

嗨,谢谢你的回答。更加详细:我编写的工具的用户可以在编辑器中打开特殊文件。这些文件可以位于工作区,也可以通过以IDE(新功能)开始的捆绑包提供。但是旧的实现需要在编辑器中显示一个IFile。我不知道如何在编辑器中打开由已启动的软件包提供的文件。对我来说最糟糕的情况是重写编辑器期望不是IFile,而是其他东西(比如新模型等)。但我希望我不需要重写这么多;) – mosk 2012-02-09 20:18:51

+0

感谢您的详细信息。编辑器可能假定一个“IFileEditorInput”作为它的输入对象......这是不好的做法,但我想这对你没有多大帮助。你可以试着看看它是否会接受一个'IStorageEditorInput',这对于非文件输入来说更容易实现。 – 2012-02-09 20:31:57

+0

就是这样,你把我带到了正确的道路上。编辑器实际上假设我能够自己实现的IEditorInput。现在它工作正常。谢谢! – mosk 2012-02-10 16:53:57

2

如果你有一个Eclipse插件/编辑器或类似这样的尝试:

//get the workspace 
IWorkspace workspace= ResourcesPlugin.getWorkspace(); 

//create the path to the file 
IPath location= new Path(yourURL.getPath()); 

//try to get the IFile (returns null if it could not be found in the workspace) 
IFile file= workspace.getRoot().getFileForLocation(location); 

if (file == null) { 
    //not found in the workspace, get the IFileStore (external files) 
    IFileStore fileStore = EFS.getLocalFileSystem().getStore(location); 
    return fileStore; 
} else { 
    // file found, return it 
    return file; 
} 

有益的可能是也:

url = FileLocator.toFileURL(yourURL); 

和/或

URL resolvedUrl = FileLocator.resolve(url); 

此之后,你可以为您的编辑器创建输入(我认为您想在那里使用它)?

Object file = myGetFile(); 
IEditorInput input; 
if (file instanceof IFile) { 
    input = new FileEditorInput((IFile)file); 
else { 
    if (file instanceof IFileStore) { 
     input = new FileStoreEditorInput((IFileStore)file); 
    } else { 
     throw new MyException("file is null, not found"); 
    } 
} 

我希望这会帮助你。

Greetz, Adreamus

+0

感谢您的回答。但'workspace.getRoot()。getFileForLocation(location);'只能在工作区内找到文件。即使FileLocator或其他可以访问文件系统上的文件的类也不起作用,因为我需要OSGi框架的接口。 – mosk 2012-02-10 16:55:49

+0

嗨,我包含的工作空间仅用于创建IFile,它们被加载到工作区中。所有外部文件都使用EFS.getLocalFileSystem()。getStore(IPath)加载。它也适用于捆绑中的文件! (看看http://lotuseclipsecorner.blogspot.com/2009/03/getting-access-to-files-in-eclipse-rcp.html)。但是存在更多的可能性,并且您选择了另一个,就像我可以在其他评论中看到的那样;-) – Adreamus 2012-02-11 10:20:17