简体   繁体   English

从IWorkspaceRoot和location String获取IFile

[英]Get IFile from IWorkspaceRoot and location String

This is an Eclipse question, and you can assume the Java package for all these Eclipse classes is org.eclipse.core.resources . 这是一个Eclipse问题,您可以假设所有这些Eclipse类的Java包都是org.eclipse.core.resources

I want to get an IFile corresponding to a location String I have: 我想获得一个对应于我所拥有的位置StringIFile

 "platform:/resource/Tracbility_All_Supported_lib/processes/gastuff/globalht/GlobalHTInterface.wsdl"

I have the enclosing IWorkspace and IWorkspaceRoot . 我有封闭的IWorkspaceIWorkspaceRoot If I had the IPath corresponding to the location above, I could simply call IWorkspaceRoot.getFileForLocation(IPath) . 如果我有与上面位置相对应的IPath ,我可以简单地调用IWorkspaceRoot.getFileForLocation(IPath)

How do I get the corresponding IPath from the location String ? 如何从位置String获取相应的IPath Or is there some other way to get the corresponding IFile ? 或者是否有其他方法可以获得相应的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 implements IPath. org.eclipse.core.runtime.Path实现IPath。

IPath p = new Path(locationString);
IWorkspaceRoot.getFileForLocation(p);

This would have worked had the location string not been a URL of type "platform:" 如果位置字符串不是“platform:”类型的URL,这将有效

For this particular case, notes in org.eclipse.core.runtime.Platform javadoc indicate that the "correct" solution is something like 对于这种特殊情况,org.eclipse.core.runtime.Platform javadoc中的注释表明“正确”的解决方案就像是

fileUrl = FileLocator.toFileURL(new URL(locationString)); 
IWorkspaceRoot.getFileForLocation(fileUrl.getPath());

@[Paul Reiners] your solution apparently assumes that the workspace root is going to be in the "resources" folder @ [Paul Reiners]您的解决方案显然假设工作区根目录将位于“resources”文件夹中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM