Skip to main content


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 13:12 Go to next message
Vlad Dumitrescu is currently offline Vlad DumitrescuFriend
Messages: 431
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 22:53 Go to previous messageGo to next message
Jan Koehnlein is currently offline Jan KoehnleinFriend
Messages: 760
Registered: July 2009
Location: Hamburg
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


---
Get professional support from the Xtext committers at www.typefox.io
Re: From IResource to EObject and back [message #824882 is a reply to message #824658] Tue, 20 March 2012 07:27 Go to previous message
Vlad Dumitrescu is currently offline Vlad DumitrescuFriend
Messages: 431
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: Tue Apr 23 17:04:35 GMT 2024

Powered by FUDForum. Page generated in 0.03932 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top