这是一个 Eclipse 问题,您可以假设所有这些 Eclipse 类的 Java 包是 org.eclipse.core.resources.

我想要一个 IFile 对应一个位置 String 我有:

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

我有附件 IWorkspaceIWorkspaceRoot. 。如果我有 IPath 对应于上面的位置,我可以简单地调用 IWorkspaceRoot.getFileForLocation(IPath).

如何获取对应的 IPath 从该位置 String?或者有其他方法可以得到对应的 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”文件夹中

其他提示

由于 IWorkspaceRoot 是一个 IContainer,你不能直接使用 workspaceRoot.findMember(String name) 并将生成的 IResource 转换为 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);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top