Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Serialization of EAttribute generating an attribute when an element was expected
Serialization of EAttribute generating an attribute when an element was expected [message #480242] Fri, 14 August 2009 14:20 Go to next message
Eclipse UserFriend
Originally posted by: ben.tenne.gmail.com

Hi,

If my Ecore specifies an EClass containing an EAttribute with
upperBound="-1" and defaultValueLiteral="0", then I observe unexpected
behaviour when I serialize an instance to XML.

My ecore was generated from an XSD using XSDEcoreBuilder.

Firstly, here's the XSD:

<xsd:schema xmlns="http://feb" xmlns:acv="http://feb"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://feb">
<xsd:complexType name="Class1" id="_MMN8sIjLEd6KGtBVr-6TfA">
<xsd:sequence>
<xsd:element name="attribute1" default="0"
id="_QpagwIjLEd6KGtBVr-6TfA" minOccurs="0" maxOccurs="unbounded">
<xsd:simpleType>
<xsd:restriction base="xsd:int">
<xsd:totalDigits value="10"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>

Note that attribute1 has default="0" defined. I don't fully understand
what sense this makes when maxOccurs>1, but as far as I'm aware, it's
legal.

Here's an excerpt from my .ecore file:

<eStructuralFeatures xsi:type="ecore:EAttribute" name="attribute1"
unique="false" upperBound="-1" eType="//Attribute1Type"
defaultValueLiteral="0">
<eAnnotations
source="http:///org/eclipse/emf/ecore/util/ExtendedMetaData">
<details key="kind" value="element"/>
<details key="name" value="attribute1"/>
</eAnnotations>
</eStructuralFeatures>

If I instantiate the containing class, and add a value to this attribute,
it serializes as follows. I've got XMLResource.OPTION_EXTENDED_META_DATA
and OPTION_KEEP_DEFAULT_CONTENT set.

<feb:Class1 xmlns:feb="http://feb">
<attribute1>123</attribute1>
</feb:Class1>

No problems so far. If I create a new instance, but I don't set a value
for attribute1, it serializes as:

<feb:Class1 xmlns:feb="http://feb" attribute1=""/>

Note that attribute1 has been serialized as an attribute with a value of
empty string. This is where the problem occurs. If I subsequently
attempt to deserialize this XML (also with
XMLResource.OPTION_EXTENDED_META_DATA set), and given that attribute1 is
expected to be represented as an element, rather than an attribute, it
fails with:

org.eclipse.emf.ecore.resource.Resource$IOWrappedException: Feature
'attribute1' not found. (, 2, 51)
at
org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.handleErrors(XMLL oadImpl.java:83)
at org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl. java:191)
at
org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLRes ourceImpl.java:180)
at
org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(Resour ceImpl.java:1445)

If I alter my ecore, removing the default value, then this problem does
not occur. Alternatively, if I avoid setting OPTION_KEEP_DEFAULT_CONTENT,
the problem does not occur.

Can anyone explain why I'm seeing this behaviour?

I've attempted to keep my description as short and to the point as
possible. If, in doing so, I've given too little information, then please
let me know and I'll give a more verbose description.
Re: Serialization of EAttribute generating an attribute when an element was expected [message #480442 is a reply to message #480242] Mon, 17 August 2009 08:02 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Ben,

Comments below.


Ben Tenne schrieb:
> Hi,
>
> If my Ecore specifies an EClass containing an EAttribute with
> upperBound="-1" and defaultValueLiteral="0", then I observe unexpected
> behaviour when I serialize an instance to XML.
Multi-valued features don't support default values.
>
> My ecore was generated from an XSD using XSDEcoreBuilder.
> Firstly, here's the XSD:
>
> <xsd:schema xmlns="http://feb" xmlns:acv="http://feb"
> xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://feb">
> <xsd:complexType name="Class1" id="_MMN8sIjLEd6KGtBVr-6TfA">
> <xsd:sequence>
> <xsd:element name="attribute1" default="0"
> id="_QpagwIjLEd6KGtBVr-6TfA" minOccurs="0" maxOccurs="unbounded">
> <xsd:simpleType>
> <xsd:restriction base="xsd:int">
> <xsd:totalDigits value="10"/>
> </xsd:restriction>
> </xsd:simpleType>
> </xsd:element>
> </xsd:sequence>
> </xsd:complexType>
> </xsd:schema>
>
> Note that attribute1 has default="0" defined. I don't fully understand
> what sense this makes when maxOccurs>1, but as far as I'm aware, it's
> legal.
It's legal in a schema, but EMF can't support it.
>
> Here's an excerpt from my .ecore file:
>
> <eStructuralFeatures xsi:type="ecore:EAttribute" name="attribute1"
> unique="false" upperBound="-1" eType="//Attribute1Type"
> defaultValueLiteral="0">
> <eAnnotations
> source="http:///org/eclipse/emf/ecore/util/ExtendedMetaData">
> <details key="kind" value="element"/>
> <details key="name" value="attribute1"/>
> </eAnnotations>
> </eStructuralFeatures>
>
> If I instantiate the containing class, and add a value to this
> attribute, it serializes as follows. I've got
> XMLResource.OPTION_EXTENDED_META_DATA and OPTION_KEEP_DEFAULT_CONTENT set.
>
> <feb:Class1 xmlns:feb="http://feb">
> <attribute1>123</attribute1>
> </feb:Class1>
>
> No problems so far. If I create a new instance, but I don't set a value
> for attribute1, it serializes as:
>
> <feb:Class1 xmlns:feb="http://feb" attribute1=""/>
The serializer will try to serialize attributes with an explicit
default. But given that a default isn't actually supported in the model,
the emptz list is serialiyed.
>
> Note that attribute1 has been serialized as an attribute with a value of
> empty string. This is where the problem occurs. If I subsequently
> attempt to deserialize this XML (also with
> XMLResource.OPTION_EXTENDED_META_DATA set), and given that attribute1 is
> expected to be represented as an element, rather than an attribute, it
> fails with:
>
> org.eclipse.emf.ecore.resource.Resource$IOWrappedException: Feature
> 'attribute1' not found. (, 2, 51)
> at
> org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.handleErrors(XMLL oadImpl.java:83)
>
> at
> org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl. java:191)
> at
> org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLRes ourceImpl.java:180)
>
> at
> org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(Resour ceImpl.java:1445)
>
>
> If I alter my ecore, removing the default value, then this problem does
> not occur. Alternatively, if I avoid setting
> OPTION_KEEP_DEFAULT_CONTENT, the problem does not occur.
>
> Can anyone explain why I'm seeing this behaviour?
You need to find a way to avoid this because EMF doesn't support
defaults for multi-valued features. Best to avoid the option to
serialize defaults.
>
> I've attempted to keep my description as short and to the point as
> possible. If, in doing so, I've given too little information, then
> please let me know and I'll give a more verbose description.
>
>
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Serialization of EAttribute generating an attribute when an element was expected [message #480521 is a reply to message #480442] Mon, 17 August 2009 13:47 Go to previous message
Eclipse UserFriend
Originally posted by: ben.tenne.gmail.com

Thanks for the reply, Ed.

I agree that a default value in this context makes no sense, so it seems
perfectly reasonable for EMF to not support it, despite the fact that XSD
allows it.

I'm going to avoid hitting this problem by post-processing the Ecore from
XSDEcoreBuilder, walking the tree to remove defaults from such features.

Regards,
Ben.
Previous Topic:CopyToClipboardCommand and DragAndDropCommand behavior questions
Next Topic:[CDO] queryHandlerFactories[sql]
Goto Forum:
  


Current Time: Sat Apr 27 04:20:28 GMT 2024

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

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

Back to the top