Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » From IResource to EObject and back
From IResource to EObject and back [message #821514] Thu, 15 March 2012 09:12 Go to next message
Vlad Dumitrescu is currently offline Vlad Dumitrescu
Messages: 313
Registered: July 2009
Location: Gothenburg
Senior Member
Hi!

I would need to be able to easily get from an IFile to the corresponding language top-level construct and back.

For the first link I came up with the following code:
public EObject getModelForResource(IFile file) {
	URI uri = URI.createURI("platform:/resource/"
			+ file.getProject().getName() + "/"
			+ file.getProjectRelativePath());
	ResourceSet set = resourceSetProvider.get(file.getProject());
	try {
		Resource res = set.getResource(uri, true);
		if (res != null) {
			return res.getContents().get(0);
		}
	} catch (Exception e) {
	}
	return null;
}

Which seems to work, but it also seems to parse the files from scratch again and again if they aren't open in an editor. Does a persistent cache exist, or do I have to implement one?

For the link from top-level EObject to IFile, the solution I found is
public IResource getResourceForModel(EObject obj) {
	URI uri = obj.eResource().getURI();
        // assuming platform://resource/project/path/to/file
	String project = uri.segment(1);
	IPath path = new Path(uri.path()).removeFirstSegments(2);
	return workspace.getRoot().getProject(project).findMember(path);
}


Is this a reasonable way to navigate?

best regards,
Vlad
Re: From IResource to EObject and back [message #824658 is a reply to message #821514] Mon, 19 March 2012 18:53 Go to previous messageGo to next message
Jan Kohnlein is currently offline Jan Kohnlein
Messages: 572
Registered: July 2009
Senior Member
Instead of manually dealing with platform:/reosurce URIs, you should use
the IStorageToUriMapper, which is also capable of handling resources in
ZIP files.

There is no persistent cache for the resources loaded in the editors. If
you really have to access their content, you could get an inspiration
how to do that from the EditorResourceAccess.


Am 15.03.12 14:12, schrieb Vlad Dumitrescu:
> Hi!
>
> I would need to be able to easily get from an IFile to the corresponding
> language top-level construct and back.
> For the first link I came up with the following code:
>
> public EObject getModelForResource(IFile file) {
> URI uri = URI.createURI("platform:/resource/"
> + file.getProject().getName() + "/"
> + file.getProjectRelativePath());
> ResourceSet set = resourceSetProvider.get(file.getProject());
> try {
> Resource res = set.getResource(uri, true);
> if (res != null) {
> return res.getContents().get(0);
> }
> } catch (Exception e) {
> }
> return null;
> }
>
> Which seems to work, but it also seems to parse the files from scratch
> again and again if they aren't open in an editor. Does a persistent
> cache exist, or do I have to implement one?
>
> For the link from top-level EObject to IFile, the solution I found is
> public IResource getResourceForModel(EObject obj) {
> URI uri = obj.eResource().getURI();
> // assuming platform://resource/project/path/to/file
> String project = uri.segment(1);
> IPath path = new Path(uri.path()).removeFirstSegments(2);
> return workspace.getRoot().getProject(project).findMember(path);
> }
>
>
> Is this a reasonable way to navigate?
>
> best regards,
> Vlad
>


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: From IResource to EObject and back [message #824882 is a reply to message #824658] Tue, 20 March 2012 03:27 Go to previous message
Vlad Dumitrescu is currently offline Vlad Dumitrescu
Messages: 313
Registered: July 2009
Location: Gothenburg
Senior Member
Thank you very much, Jan!

regards,
Vlad
Previous Topic:Exclude Directories from the builder
Next Topic:Advanced Formatting doubts
Goto Forum:
  


Current Time: Sun May 26 03:11:14 EDT 2013

Powered by FUDForum. Page generated in 0.06265 seconds