Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Customizing serialization and de-serialization.
Customizing serialization and de-serialization. [message #423927] Fri, 10 October 2008 03:06 Go to next message
Eclipse UserFriend
Originally posted by: minnist.gmx.net

Hi!

I am trying to customize the serialization and de-serialization of a given
meta model in order to create XML code that adheres to a provided DTD.

Using annotations and customizing XMLSave I was able to generate the XML I
need. The problem I am stuck with now is that I don't seem to be able
properly re-load the XML.

I have a class System, itself contained by another class, which has an
attribute #definition.

The XML code I need to generate looks like:
<...>
<...></...>
<system>TEXT</system>
</...>

Thus Because I don't want #definition to appear as tag <definition> in the
XML code I overwrote XMLSave.saveElement():

protected void saveElement(EObject obj, EStructuralFeature structFeat) {
EClass eClass = obj.eClass();
EClassifier eType = structFeat.getEType();

if (eClass.equals(SomePackage.eINSTANCE.getSystem())) {
String str = extendedMetaData.getName(eClass);
this.doc.startElement("system");
this.doc.endContentElement(((System)obj).getDefinition());
}
else {
super.saveElement(obj, structFeat);
}
}


However when the XML code is de-serialized, the content between the
<system></system> tags is ignored and #definition is not set. (Which, I
guess, makes sense)


Thus my question is, what would be the best way to achieve the behaviour I
need.

At the beginning I was hoping I could achieve everything I need to do
using annotations. Then I was hoping I just need to modify
createObject(EFactory factory, EClassifier type) . However I am not sure
how/where to get the TEXT to set the #definition attribute.

Furthermore:
Is overwriting saveElement() actually the best way to generate the needed
XML code. Can this be achieved using annotations as well? Or can at least
system from <this.doc.startElement("system");> somehow be retrieved from
the annotations? Every time I look at the xmlMap it is actually empty

Is there more information available about using annotations and
XMLSave/XMLLoad and XMLHelper and so on to customize serialization and
de-serialization.

I would appreciate any help
Lothar
Re: Customizing serialization and de-serialization. [message #423939 is a reply to message #423927] Fri, 10 October 2008 11:12 Go to previous message
Ed Merks is currently online Ed MerksFriend
Messages: 33141
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------050503080606090007080909
Content-Type: text/plain; charset=ISO-2022-JP
Content-Transfer-Encoding: 7bit

Lothar,

Comments below.

minnist wrote:
> Hi!
>
> I am trying to customize the serialization and de-serialization of a
> given meta model in order to create XML code that adheres to a
> provided DTD.
DTDs are so last century! :-P
>
> Using annotations and customizing XMLSave I was able to generate the
> XML I need. The problem I am stuck with now is that I don't seem to be
> able properly re-load the XML.
Wasn't it possible to express your serialized form as an XML Schema and
then generate a model from that? Or did that produce a gross model?
>
> I have a class System, itself contained by another class, which has an
> attribute #definition.
>
> The XML code I need to generate looks like:
> <...>
> <...></...>
> <system>TEXT</system>
> </...>
This would be like a complex type with simple content with "TEXT" being
the value of that simple content feature.
>
> Thus Because I don't want #definition to appear as tag <definition> in
> the XML code I overwrote XMLSave.saveElement():
>
> protected void saveElement(EObject obj, EStructuralFeature structFeat) {
> EClass eClass = obj.eClass();
> EClassifier eType = structFeat.getEType();
>
> if (eClass.equals(SomePackage.eINSTANCE.getSystem())) {
I'd use eClass == SomePackage.Literals.SYSTEM as a much faster way...
> String str = extendedMetaData.getName(eClass);
> this.doc.startElement("system");
> this.doc.endContentElement(((System)obj).getDefinition());
If the System class was marked as having simple content kind and the
definition was marked as having feature kind simple, then you'd get this
form without all this code...
> }
> else {
> super.saveElement(obj, structFeat);
> }
> }
>
>
> However when the XML code is de-serialized, the content between the
> <system></system> tags is ignored and #definition is not set. (Which,
> I guess, makes sense)
While if you have it annotated such that the definition feature is the
simple content feature, it would know to take the element content and
stuff it in there.
>
>
> Thus my question is, what would be the best way to achieve the
> behaviour I need.
It would really be best to try to express your syntax as an XML Schema
and then produce your model from that or try to annotate your model to
match that. Your example here is a complex type with simple content.
>
> At the beginning I was hoping I could achieve everything I need to do
> using annotations. Then I was hoping I just need to modify
> createObject(EFactory factory, EClassifier type) . However I am not
> sure how/where to get the TEXT to set the #definition attribute.
>
> Furthermore:
> Is overwriting saveElement() actually the best way to generate the
> needed XML code. Can this be achieved using annotations as well? Or
> can at least system from <this.doc.startElement("system");> somehow be
> retrieved from the annotations? Every time I look at the xmlMap it is
> actually empty
XML Map predates extended metadata. The latter is far more powerful and
capable to producing pretty much anything you can expression as an XML
Schema.
>
> Is there more information available about using annotations and
> XMLSave/XMLLoad and XMLHelper and so on to customize serialization and
> de-serialization.
Specializing those is pretty much a last resort. I'm pretty sure that
the annotations described here should suffice:

XML Schema to Ecore Mapping
< http://www.eclipse.org/modeling/emf/docs/overviews/XMLSchema ToEcoreMapping.pdf>

>
> I would appreciate any help
> Lothar

--------------050503080606090007080909
Content-Type: text/html; charset=ISO-2022-JP
Content-Transfer-Encoding: 7bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-2022-JP"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Lothar,<br>
<br>
Comments below.<br>
<br>
minnist wrote:
<blockquote cite="mid:op.uisf9oufqek0uu@isezaki.ikv-intern.local"
type="cite">Hi!
<br>
<br>
I am trying to customize the serialization and de-serialization of a
given meta model in order to create XML code that adheres to a provided
DTD.
<br>
</blockquote>
DTDs are so last century! :-P<br>
<blockquote cite="mid:op.uisf9oufqek0uu@isezaki.ikv-intern.local"
type="cite"><br>
Using annotations and customizing XMLSave I was able to generate the
XML I need. The problem I am stuck with now is that I don't seem to be
able properly re-load the XML.
<br>
</blockquote>
Wasn't it possible to express your serialized form as an XML Schema and
then generate a model from that?&nbsp; Or did that produce a gross model?<br>
<blockquote cite="mid:op.uisf9oufqek0uu@isezaki.ikv-intern.local"
type="cite"><br>
I have a class System, itself contained by another class, which has an
attribute #definition.
<br>
<br>
The XML code I need to generate looks like:
<br>
&lt;...&gt;
<br>
&nbsp; &lt;...&gt;&lt;/...&gt;
<br>
&nbsp; &lt;system&gt;TEXT&lt;/system&gt;
<br>
&lt;/...&gt;
<br>
</blockquote>
This would be like a complex type with simple content with "TEXT" being
the value of that simple content feature.<br>
<blockquote cite="mid:op.uisf9oufqek0uu@isezaki.ikv-intern.local"
type="cite"><br>
Thus Because I don't want #definition to appear as tag
&lt;definition&gt; in the XML code I overwrote XMLSave.saveElement():
<br>
<br>
protected void saveElement(EObject obj, EStructuralFeature structFeat)
{
<br>
&nbsp;&nbsp;&nbsp;&nbsp;EClass eClass = obj.eClass();
<br>
&nbsp;&nbsp;&nbsp;&nbsp;EClassifier eType = structFeat.getEType();
<br>
&nbsp;&nbsp;&nbsp;&nbsp;
<br>
&nbsp;&nbsp;&nbsp;&nbsp;if (eClass.equals(SomePackage.eINSTANCE.getSystem())) {
<br>
</blockquote>
I'd use eClass == SomePackage.Literals.SYSTEM as a much faster way...<br>
<blockquote cite="mid:op.uisf9oufqek0uu@isezaki.ikv-intern.local"
type="cite"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; String str = extendedMetaData.getName(eClass);
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; this.doc.startElement("system");
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; this.doc.endContentElement(((System)obj).getDefinition());
<br>
</blockquote>
If the System class was marked as having simple content kind and the
definition was marked as having feature kind simple, then you'd get
this form without all this code...<br>
<blockquote cite="mid:op.uisf9oufqek0uu@isezaki.ikv-intern.local"
type="cite">&nbsp;&nbsp;&nbsp;&nbsp;}
<br>
&nbsp;&nbsp;&nbsp;&nbsp;else {
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; super.saveElement(obj, structFeat);
<br>
&nbsp;&nbsp;&nbsp;&nbsp;}
<br>
}
<br>
<br>
<br>
However when the XML code is de-serialized, the content between the
&lt;system&gt;&lt;/system&gt; tags is ignored and #definition is not
set. (Which, I guess, makes sense)
<br>
</blockquote>
While if you have it annotated such that the definition feature is the
simple content feature, it would know to take the element content and
stuff it in there.<br>
<blockquote cite="mid:op.uisf9oufqek0uu@isezaki.ikv-intern.local"
type="cite"><br>
<br>
Thus my question is, what would be the best way to achieve the
behaviour I need.
<br>
</blockquote>
It would really be best to try to express your syntax as an XML Schema
and then produce your model from that or try to annotate your model to
match that.&nbsp; Your example here is a complex type with simple content.<br>
<blockquote cite="mid:op.uisf9oufqek0uu@isezaki.ikv-intern.local"
type="cite"><br>
At the beginning I was hoping I could achieve everything I need to do
using annotations. Then I was hoping I just need to modify
createObject(EFactory factory, EClassifier type) . However I am not
sure how/where to get the TEXT to set the #definition attribute.
<br>
<br>
Furthermore:
<br>
Is overwriting saveElement() actually the best way to generate the
needed XML code. Can this be achieved using annotations as well? Or can
at least system from &lt;this.doc.startElement("system");&gt; somehow
be retrieved from the annotations? Every time I look at the xmlMap it
is actually empty
<br>
</blockquote>
XML Map predates extended metadata.&nbsp; The latter is far more powerful
and capable to producing pretty much anything you can expression as an
XML Schema.<br>
<blockquote cite="mid:op.uisf9oufqek0uu@isezaki.ikv-intern.local"
type="cite"><br>
Is there more information available about using annotations and
XMLSave/XMLLoad and XMLHelper and so on to customize serialization and
de-serialization.
<br>
</blockquote>
Specializing those is pretty much a last resort.&nbsp; I'm pretty sure that
the annotations described here should suffice:<br>
<blockquote><a
href=" http://www.eclipse.org/modeling/emf/docs/overviews/XMLSchema ToEcoreMapping.pdf">XML
Schema to Ecore Mapping</a></blockquote>
<blockquote cite="mid:op.uisf9oufqek0uu@isezaki.ikv-intern.local"
type="cite"><br>
I would appreciate any help
<br>
Lothar
<br>
</blockquote>
</body>
</html>

--------------050503080606090007080909--


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:How to change the EObjectImpl used for dynamical loaded models (rootExtendsClass for dynamic models)
Next Topic:Eclipse modeling trainings
Goto Forum:
  


Current Time: Fri Apr 26 06:06:44 GMT 2024

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

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

Back to the top