2012-02-09 21 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

相關問題