tags:

views:

1269

answers:

3

This is an Eclipse question, and you can assume the Java package for all these Eclipse classes is org.eclipse.core.resources.

I want to get an IFile corresponding to a location String I have:

 "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).

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

A: 
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);
Paul Reiners
A: 

org.eclipse.core.runtime.Path implements IPath.

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

This would have worked had the location string not been a URL of type "platform:"

For this particular case, notes in org.eclipse.core.runtime.Platform javadoc indicate that the "correct" solution is something like

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

Tirno
No, actually that didn't work. I think it's because locationString has a format like this:"platform:/resource/Tracbility_All_Supported_lib/processes/gastuff/globalht/GlobalHTInterface.wsdl"but the Path constructor wants "a valid file system path on the local file system."
Paul Reiners
+1  A: 

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

Roel Spilker
Yes, that worked. Thanks!
Paul Reiners