capint
8/3/2016 - 12:59 PM

Eclipse >> Workspace file <-> native file

Eclipse >> Workspace file <-> native file

import org.eclipse.core.filesystem.EFS;
/**
 * Convert an Eclipse workspace file to a native file
 * @param wsFile Eclipse workspace file
 * @return native file
 */
private File convertToNativeFile(IFile wsFile)
{
	// gets URI for EFS.
	java.net.URI uri = wsFile.getLocationURI();

	// If the file is a link, resolve it.
	if(wsFile.isLinked()){
	   uri = wsFile.getRawLocationURI();
	}

	// Gets native File using Eclipse File System
	File nativeFile = null;
	try {
		nativeFile = EFS.getStore(uri).toLocalFile(0, new NullProgressMonitor());
	} catch (CoreException e) {
		e.printStackTrace();
	}
	return nativeFile;
/**
 * Convert an Eclipse workspace file to a native file
 * @param wsFile Eclipse workspace file
 * @return native file
 */
private File convertToNativeFile(IFile wsFile)
{
	// gets URI for EFS.
	IPath location = wsFile.getLocation();

	// If the file is a link, resolve it.
	if(wsFile.isLinked()){
	   location = wsFile.getRawLocation();
	}

	// Gets native File using Eclipse File System
	File nativeFile = null;
	if (location != null)
		nativeFile = location.toFile();
	return nativeFile;
}

/**
 * Convert a native file to an Eclipse workspace file 
 * 
 * @param nativeFile
 * @return Eclipse workspace file
 */
private IFile convertToWorkspaceFile(File nativeFile) {
	IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
	IPath location = Path.fromOSString(nativeFile.getAbsolutePath());
	IFile file = root.findFilesForLocationURI(URIUtil.toURI(location))[0];
	return file;
}