Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » EAttribute serialized as Element not attribute (override?)
EAttribute serialized as Element not attribute (override?) [message #1758502] Wed, 29 March 2017 11:49 Go to next message
Thomas Axel is currently offline Thomas AxelFriend
Messages: 10
Registered: March 2017
Junior Member
Hi.

We have an attribute called "text" it's an EString. (Used by xtext)

When its serialized (XML) to the model it's saved as an attribute of the object.
Is it possible to make it serialize to an element instead?

The thing is that our models are persisted with git.
So if you look on Github it's not easy to see what "line" changed;
Because the attribute will format to one line.

Where if it were persisted as an element it easier to see because it would be able to be persisted as multiline.

(it works fine now, it more a matter of preference, and transparency when comparing models on Github )
Re: EAttribute serialized as Element not attribute (override?) [message #1758535 is a reply to message #1758502] Wed, 29 March 2017 17:15 Go to previous messageGo to next message
Ed Merks is currently online Ed MerksFriend
Messages: 29115
Registered: July 2009
Senior Member
Yes, with extended metadata annotations you could serialize them as elements, but I think better (certainly easier) would be use the save option org.eclipse.emf.ecore.xmi.XMLResource.OPTION_LINE_WIDTH set to a very small number like 10 so that each attribute is serialized on its own line (which is still more compact than as separate elements, and it doesn't otherwise change the serialization "schema").
Re: EAttribute serialized as Element not attribute (override?) [message #1758570 is a reply to message #1758535] Thu, 30 March 2017 08:16 Go to previous messageGo to next message
Thomas Axel is currently offline Thomas AxelFriend
Messages: 10
Registered: March 2017
Junior Member
Hi Ed
Thanks for your fast response

I tried with the second solution (XMLResource.OPTION_LINE_WIDTH, 10)

And yes that for sure do something with my savefile.

But not what I want, of how it looks with the second solution.
I attached a picture

But the text still gets very long.
Basically, you can say that everytime the output is 

It means its represent a newline.
(not sure it is xtext syntax or EMF)

So best case would be that everytime a "
" comes it should also span over more lines.
Re: EAttribute serialized as Element not attribute (override?) [message #1758576 is a reply to message #1758570] Thu, 30 March 2017 09:26 Go to previous messageGo to next message
Ed Merks is currently online Ed MerksFriend
Messages: 29115
Registered: July 2009
Senior Member
You'd need to use org.eclipse.emf.ecore.xmi.XMLResource.OPTION_EXTENDED_META_DATA and add an annotation just like the one added by org.eclipse.emf.ecore.util.BasicExtendedMetaData.setFeatureKind(EStructuralFeature, int) to the feature that you want to be an element.
Re: EAttribute serialized as Element not attribute (override?) [message #1758580 is a reply to message #1758570] Thu, 30 March 2017 09:27 Go to previous messageGo to next message
Thomas Axel is currently offline Thomas AxelFriend
Messages: 10
Registered: March 2017
Junior Member
I found this
https://dzone.com/articles/emf-tips-accessing-model-meta

And that seemed to do the trick for me.

Could still be cooler if you could just set it in the model insteed of in code however.


Added this
saveOptions.put(XMLResource.OPTION_EXTENDED_META_DATA, Boolean.TRUE);
XMLMapImpl map = new XMLMapImpl();
XMLInfoImpl x = new XMLInfoImpl();
x.setXMLRepresentation(XMLInfoImpl.ELEMENT);
map.add(IComponentPackage.Literals.CALCULATION__TEXT, x);
saveOptions.put(XMLResource.OPTION_XML_MAP, map);


Re: EAttribute serialized as Element not attribute (override?) [message #1758590 is a reply to message #1758580] Thu, 30 March 2017 11:40 Go to previous messageGo to next message
Ed Merks is currently online Ed MerksFriend
Messages: 29115
Registered: July 2009
Senior Member
Yes, you can do that an extended metadata annotation on the EStructuralFeature in the Ecore model.
      <eAnnotations source="http:///org/eclipse/emf/ecore/util/ExtendedMetaData">
        <details key="kind" value="element"/>
      </eAnnotations>
Re: EAttribute serialized as Element not attribute (override?) [message #1758600 is a reply to message #1758590] Thu, 30 March 2017 13:20 Go to previous messageGo to next message
Thomas Axel is currently offline Thomas AxelFriend
Messages: 10
Registered: March 2017
Junior Member
Hi Ed
again thanks.

That also works perfectly (and cleaner than only code)

The only thing I still had to do in code in my case was to add:
saveOptions.put(XMLResource.OPTION_EXTENDED_META_DATA, Boolean.TRUE);



Ed Merks wrote on Thu, 30 March 2017 11:40
Yes, you can do that an extended metadata annotation on the EStructuralFeature in the Ecore model.
      <eAnnotations source="http:///org/eclipse/emf/ecore/util/ExtendedMetaData">
        <details key="kind" value="element"/>
      </eAnnotations>

Re: EAttribute serialized as Element not attribute (override?) [message #1758613 is a reply to message #1758600] Thu, 30 March 2017 15:04 Go to previous message
Ed Merks is currently online Ed MerksFriend
Messages: 29115
Registered: July 2009
Senior Member
If you change the GenPackage's Resource Type to XML, the presence of extended metadata annotations will generate a resource factory with the right options.
Previous Topic:DSLTables generation problem when packages suffixes are customized
Next Topic:custom labels
Goto Forum:
  


Current Time: Wed Jan 17 05:43:00 GMT 2018

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

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