Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Getting the root node from a resource
Getting the root node from a resource [message #1059808] Tue, 21 May 2013 13:11 Go to next message
Erick Fonseca is currently offline Erick Fonseca
Messages: 26
Registered: December 2011
Junior Member
Is there a specific way to get the root node from a Resource? Currently I'm doing

resource.getContents().get(0)


But I feel it's weird to get the whole content list if I just want the root.
Re: Getting the root node from a resource [message #1059878 is a reply to message #1059808] Wed, 22 May 2013 01:14 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik Lindberg
Messages: 2431
Registered: July 2009
Senior Member
On 2013-21-05 10:12, Erick Fonseca wrote:
> Is there a specific way to get the root node from a Resource? Currently
> I'm doing
>
> resource.getContents().get(0)
>
>
> But I feel it's weird to get the whole content list if I just want the
> root.

You are doing it right.
A Resource is just more general than the use case of containing a parse
result.

- henrik
Re: Getting the root node from a resource [message #1059882 is a reply to message #1059808] Wed, 22 May 2013 01:41 Go to previous messageGo to next message
Ed Willink is currently offline Ed Willink
Messages: 3191
Registered: July 2009
Senior Member
Hi

You're probably also doing

(MyRoot)resource.getContents().get(0)

which means that the user gets to see confusing stack traces for the
following errors
Resource is empty
Resource is multi-rooted
Resource doesn't contain MyRoot
usually resulting from a file name typo, but sometimes through
application defects.

I've taken to coding at least

for (EObject eObject : resource.getContents()) {
if (eObject instanceof MyRoot) {
doSomethingWith((MyRoot)eObject);
}
else {
some error
}
}

Regards

Ed Willink

On 21/05/2013 18:12, Erick Fonseca wrote:
> Is there a specific way to get the root node from a Resource?
> Currently I'm doing
>
> resource.getContents().get(0)
>
>
> But I feel it's weird to get the whole content list if I just want the
> root.
Re: Getting the root node from a resource [message #1060039 is a reply to message #1059882] Wed, 22 May 2013 11:20 Go to previous messageGo to next message
Hallvard Traetteberg is currently offline Hallvard Traetteberg
Messages: 539
Registered: July 2009
Location: Trondheim, Norway
Senior Member
On 21.05.13 22.41, Ed Willink wrote:
>
> (MyRoot)resource.getContents().get(0)
>
> which means that the user gets to see confusing stack traces for the
> following errors
> Resource is empty
> Resource is multi-rooted
> Resource doesn't contain MyRoot
> usually resulting from a file name typo, but sometimes through
> application defects.
>
> I've taken to coding at least
>
> for (EObject eObject : resource.getContents()) {
> if (eObject instanceof MyRoot) {
> doSomethingWith((MyRoot)eObject);
> }
> else {
> some error
> }
> }

Yes, this is in general a lot better, since with Xtext-based DSLs it is
common to derive other models, and your particular root may not always
be the first element in such a case. I wish EcoreUtil had a method
<T> T getRoot(Class<T> clazz, boolean throwError) that did this, so it
was easy for everybody to start using this coding style.

Hallvard
Re: Getting the root node from a resource [message #1060055 is a reply to message #1060039] Wed, 22 May 2013 13:49 Go to previous message
Erick Fonseca is currently offline Erick Fonseca
Messages: 26
Registered: December 2011
Junior Member
Thank you for the insights. I will perform this check.
Previous Topic:Write new resource description very slow
Next Topic:How to get ecore and genmodel?
Goto Forum:
  


Current Time: Tue May 28 17:15:03 EDT 2013

Powered by FUDForum. Page generated in 0.01542 seconds