|
Re: From IResource to EObject and back [message #824658 is a reply to message #821514] |
Mon, 19 March 2012 22:53 |
Jan Koehnlein 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
|
|
|
|
Powered by
FUDForum. Page generated in 0.03258 seconds