从IWorkspaceRoot和location String获取IFile

时间:2021-11-07 11:54:33

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:

我想获得一个对应于我所拥有的位置字符串的IFile:

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

I have the enclosing IWorkspace and IWorkspaceRoot. If I had the IPath corresponding to the location above, I could simply call IWorkspaceRoot.getFileForLocation(IPath).

我有封闭的IWorkspace和IWorkspaceRoot。如果我有与上面位置相对应的IPath,我可以简单地调用IWorkspaceRoot.getFileForLocation(IPath)。

How do I get the corresponding IPath from the location String? Or is there some other way to get the corresponding IFile?

如何从位置字符串中获取相应的IPath?或者是否有其他方法可以获得相应的IFile?

3 个解决方案

#1


2  

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:"

如果位置字符串不是“平台:”类型的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”文件夹中

#2


3  

Since IWorkspaceRoot is an IContainer, can't you just use workspaceRoot.findMember(String name) and cast the resulting IResource to IFile?

由于IWorkspaceRoot是一个IContainer,你不能只使用workspaceRoot.findMember(String name)并将生成的IResource转换为IFile吗?

#3


3  

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);

#1


2  

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:"

如果位置字符串不是“平台:”类型的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”文件夹中

#2


3  

Since IWorkspaceRoot is an IContainer, can't you just use workspaceRoot.findMember(String name) and cast the resulting IResource to IFile?

由于IWorkspaceRoot是一个IContainer,你不能只使用workspaceRoot.findMember(String name)并将生成的IResource转换为IFile吗?

#3


3  

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);