[英]Get IFile from IWorkspaceRoot and location String
這是一個Eclipse問題,您可以假設所有這些Eclipse類的Java包都是org.eclipse.core.resources
。
我想獲得一個對應於我所擁有的位置String
的IFile
:
"platform:/resource/Tracbility_All_Supported_lib/processes/gastuff/globalht/GlobalHTInterface.wsdl"
我有封閉的IWorkspace
和IWorkspaceRoot
。 如果我有與上面位置相對應的IPath
,我可以簡單地調用IWorkspaceRoot.getFileForLocation(IPath)
。
如何從位置String
獲取相應的IPath
? 或者是否有其他方法可以獲得相應的IFile
?
String platformLocationString = portTypeContainer
.getLocation();
String locationString = platformLocationString
.substring("platform:/resource/".length());
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IWorkspaceRoot workspaceRoot = workspace.getRoot();
IFile wSDLFile = (IFile) workspaceRoot
.findMember(locationString);
由於IWorkspaceRoot是一個IContainer,你不能只使用workspaceRoot.findMember(String name)
並將生成的IResource轉換為IFile嗎?
org.eclipse.core.runtime.Path實現IPath。
IPath p = new Path(locationString);
IWorkspaceRoot.getFileForLocation(p);
如果位置字符串不是“platform:”類型的URL,這將有效
對於這種特殊情況,org.eclipse.core.runtime.Platform javadoc中的注釋表明“正確”的解決方案就像是
fileUrl = FileLocator.toFileURL(new URL(locationString));
IWorkspaceRoot.getFileForLocation(fileUrl.getPath());
@ [Paul Reiners]您的解決方案顯然假設工作區根目錄將位於“resources”文件夾中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.