ResourceException when creating IMarker on IFile (linked resource) - eclipse

I have some problems updating an "old" Eclipse plugin. Here is what I would like to do and what the original plugin did:
(parse compiler output on console with file name and error information --> still works)
--> set link to the location within the file
--> set marker to location in the file
What I did in the past was to get the IFile from the path String of the file and generated link and marker from it:
IFile ifile;
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IPath path = new Path(fileName);
IFiles[] files = workspace.getRoot().findFilesForLocation(path);
...
ifile = iFiles[0];
Map attributes = new HashMap();
attributes.put(IMarker.SEVERITY, new Integer (severity));
MarkerUtilities.setLineNumber(attributes, lineNumber);
MarkerUtilities.setMessage(attributes, message);
MarkerUtilities.createMarker(ifile, attributes,
IMarker
Since findFilesForLocation is deprecated, I tried to find another way but I am not succeeding whatsoever. Using the changed code to get the IFile always results in a exception: org.eclipse.core.internal.resources.ResourceException: Resource '/path/to/file.c' does not exist.
Is it possible that this relates to the fact that the source file is only linked into the project, and not physically within the project?
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IPath location = new Path(fileName);
IFile ifile = workspace.getRoot().getFile(location);
Can anyone help?
Thank you!

I am guessing that fileName is the fully qualified path to the file you want to get. I'm also guessing that the file that you are looking for is already in the workspace, even if it is linked (if not, then this won't work. You will first need to add the file to a project before getting the IFile for it).
You need to do something like this:
IFiles[] files = workspace.getRoot().findFilesForLocationURI("file:" + fileName);
Then this will find all files in the workspace that correspond to the file in the file system.
The reason why your attempt above is giving you a ResourceException is that you are trying to pass in a file system path to get an IFile object from the workspace. The Eclipse workspace is an abstraction over the underlying filesystem and cannot directly work with absolute paths.

For the Resources APIs, Paths usually means a path in the workspace and Location usually refers to a place in the filesystem or outside the workspace. If you already have a workspace path to start with, just ask the IWorkspaceRoot for the IFile and get on with what you're doing.

Related

Clearing the contents of a resource file (IFile)

In the workspace we have a set of resource files which contain contents in it.
Problem: When I select the project we can get the resource file and now we want to clear the contents of the file. So we tried to get the resource file as IFile and if we try to use the IFile::delete() method it is deleting the resource file from workspace instead of clearing the contents.
How can we clear the contents of the IFile present in the workspace without deleting the IFile form the workspace?
Use IFile::setContents() to change the contents of a file.
To clear the content of the file, use an empty input stream:
InputStream newContents = new ByteArrayInputStream( new byte[ 0 ] );
IProgressmonitor monitor = ...
file.setContents( newContents, IResource.KEEP_HISTORY, monitor );
The KEEP_HISTORY flag keeps the previous contents in the history so that user could go back to earlier versions with Replace With > Local History. If that's not desired, use IResource.NONE.

Read AndroidManifest.xml in eclipse plugin code without opening it in IEditor

First I tried to open it in eclipse editor and then read it using org.eclipse.jface.text.IDocument API. But it is quite lengthy, so I tried to figure out way to directly read the file. Here are the pieces that I have found in this regard.
IProject project = (IProject)((IAdaptable)firstElement).getAdapter(IProject.class);
IPath path = project.getFullPath();
String strr=path.toString();
IFile manifest = project.getFile("AndroidManifest.xml");
IPath file_path = manifest.getFullPath().makeAbsolute();
Now, I can use getContents() method of IFile interface. But how to read the contents as it appears in editor(Eclipse editor or IEditor) as I have no idea how AndroidManifest.xml is encoded.

Eclipse PDE: given a relative path like /ProjectName/lib/something.jar, how do you get a full filesystem path?

I'm trying to find a path to a jar file that's in the raw classpath. getRawClasspath returns a collection of IClasspathEntry objects. I can call getPath on those.
But getPath returns something weird: an IPath that starts with the project name, like:
/ProjectName/lib/something.jar
What's the right way to turn this relative path into a full-qualified OS path? At first I thought I could just add the path to the workspace root, but that doesn't work since there are often intermediate directories between the workspace and the project.
And more generally, how do I know what to do with an IPath returned by a method? It seems like I never know what that IPath is; relative to the project, relative to the workspace, relative to the project but with the project name as the first element, relative to the phase of the moon... It's all baffling, and the documentation is never helpful - or at least I don't know where to look.
UPDATE
I'm even more confused now. The problem still is that, when you have an IClasspathEntry, it's still unclear to me how to resolve it to a filesystem path.
The answer that says "if a path starts with a / it's an absolute path (relative to the workspace) isn't correct. The problem is that the getPath method on an IClasspath returns one of two things: a path starting with a slash that's relative to the workspace, or an IPath starting with a / that's an actual filesystem path. Yes, two completely different things are shoved into one type. You get the filesystem variant when the jar is outside the workspace, and you get the "absolute" variant when it's in the workspace.
I think part of the answer is that an IPath, by itself, is only a fancy string. You have to know where it came from to make sense out of it. It doesn't carry the right sorts of information to be useful on its own.
So what's the right way to deal with this?
try this:
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IResource res = root.findMember("/ProjectName/lib/something.jar");
System.out.println(res.getLocation().toString());
Paths in Eclipse are complicated, and there are a few other situations that you haven't mentioned, like classpath containers (JRE is an example), linked resources outside of the workspace and classpath variables.
To simplify, I suggest you use getResolvedClasspath instead, that returns only 'simple' classpath entires (no variables, no containers). According to its Javadoc, it returns absolute paths, and their interpretation depends on the kind of the entry:
CPE_LIBRARY - if it's an external library, it's a filesystem path (and it has no associated resource, meaning you can't find it with findMember). Otherwise, it's a path based on the workspace root
CPE_PROJECT - absolute path to the project
CPE_SOURCE - absolute path to the source folder
All absolute path are interpreted in the workspace. If you need the file-system path, you need to go through getLocation.
As a side-note, there is no 1-to-1 mapping between file-system entities and workspace resources. Because of workspace links, you may have several workspace paths pointing to the same (file-system) location.
This is the best I could come up with, and it totally is an ugly hack, yes:
private IPath getCorrectAbsolutePath(IJavaProject project, IPath path) throws IllegalArgumentException {
final String projectName = project.getProject().getName();
if (path.segmentCount() > 1 && path.segment(0).equals(projectName)) {
IPath projectAbsolutePath = project.getProject().getLocation();
IPath relativePath = path.removeFirstSegments(1);
return projectAbsolutePath.append(relativePath);
} else {
if (!path.isAbsolute())
path = path.makeAbsolute();
if (!path.isAbsolute())
throw new IllegalArgument("Cannot make IPath absolute: " + path.toString());
return path;
}
}
I have a suggestion, look picture attachment, find "location" property from properties of any plugin of eclipse , maybe can find it where is from. but sorry I have no 10 reputation, you can see picture from picture url

Eclipse Plugin: Obtaining an IFile from String

I am doing some serialization which also contains an IFile path that needs to be stored as string.
I am using this IFile in a plugin project. For debugging or running Eclipse starts a new workspace. This testing-workspace has its root somewhere relatively to the plugin folder. My problem is, when I turn my IFile to an absolute path, my Eclipse testing-workspace considers the file as outside the workspace and throws exceptions.
If I use the project relative path, the IFile creation from string fails and IFile is null.
I truly want to believe that it works the way I need it, but I really would like to see it. Is there a way to reconstruct a valid IFile from a project relative path?
Currently, I am doing the reconstruction from String->IFile like this:
//name is a string with the absolute path
IPath location = new Path(name);
IFile file = project.getFile(location.lastSegment());
But, like already mentioned, works only with an absolute path, which doesn't work in the eclipse testing-workspace.
Thanks for a hint
You are very close. You want:
IPath path = new Path(name);
IFile file = project.getFile(path);
The name should be an absolute (to the workspace) or project-relative path. This interface is defined in IContainer.getFile(IPath). I changed the variable from "location" because location is usually meant as the actual (local) OS file path. To get the path use:
IPath path = file.getProjectRelativePath();

eclipse: Find Resource with a locationURI

How can i find a Resource with a locationURI?
Path path = new Path('/home/foo/eclipse/runtime-EclipseApplication/someproj/B.txt');
IResource res = ResourcesPlugin.getWorkspace().getRoot().findMember(path);
I know there is a way by using the locationURI from current project to trim the "needless" part, but is there a "better" way to slove this issue.
You can use the following construction:
ResourcesPlugin.getWorkspace().getRoot().findFilesForLocationURI(URIUtil.toURI(fileName));
where fileName - absolute path of File.getAbsolutePath()
and URIUtil from org.eclipse.core.filesystem
You can use a FileLocator to find the file within your bundle, if that is what you want (instead of finding a file in the runtime workspace), see Retrieve a file from within a folder in the workspace