Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » How to save an object from one package to an anyType feature of the object from another package with
How to save an object from one package to an anyType feature of the object from another package with [message #428137] Wed, 11 March 2009 15:59 Go to next message
koloale is currently offline koloaleFriend
Messages: 41
Registered: July 2009
Member
Hello!

I have two EPackage's P1 and P2 both created form XSD. I deserialize the
xml document: <p2:node xmlns:p2="NS_URI2">1</p2:node> into EObject obj
from the P1 package and want to save this object to AnyType feature of a
'Container' object from P2:
class Container {
/* @model containment="true"
* extendedMetaData="kind='element' name='config'"
*/
EObject getConfig();
void setConfig(EObject value);
}

I save deserialized object obj as follows:

String featureName = obj.eContainingFeature().getName();
String namespace = obj.eClass().getEPackage().getNsURI();

EStructuralFeature feature =
ExtendedMetaData.INSTANCE.demandFeature(namespace, featureName, false);
AnyType anyType = XMLTypeFactory.eINSTANCE.createAnyType();
anyType.getMixed().add(feature, configNode.getEObject());

Container c = ..
c.setConfig(anyType);

But when I serialize c object I get an xml with xsi:type:
<p1:container xmlns:p1="NS_URI1" xmlns:p2="NS_URI2">
<p1:config>
<p2:node xsi:type="p2:NodeType">1</p2:node>
<p1:config>
<p1:container>
and this xml fails the validation.


How do I get rid of xsi:type attribute?

Thanks in advance,
Alexey
Re: How to save an object from one package to an anyType feature of the object from another package [message #428145 is a reply to message #428137] Wed, 11 March 2009 17:05 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Alexey,

Comments below.


koloale wrote:
> Hello!
>
> I have two EPackage's P1 and P2 both created form XSD. I deserialize
> the xml document: <p2:node xmlns:p2="NS_URI2">1</p2:node> into EObject
> obj from the P1 package and want to save this object to AnyType
> feature of a 'Container' object from P2:
> class Container {
> /* @model containment="true"
> * extendedMetaData="kind='element' name='config'"
> */
> EObject getConfig();
> void setConfig(EObject value);
> }
>
> I save deserialized object obj as follows:
>
> String featureName = obj.eContainingFeature().getName();
> String namespace = obj.eClass().getEPackage().getNsURI();
>
> EStructuralFeature feature =
> ExtendedMetaData.INSTANCE.demandFeature(namespace, featureName, false);
> AnyType anyType = XMLTypeFactory.eINSTANCE.createAnyType();
> anyType.getMixed().add(feature, configNode.getEObject());
>
> Container c = ..
> c.setConfig(anyType);
>
> But when I serialize c object I get an xml with xsi:type:
> <p1:container xmlns:p1="NS_URI1" xmlns:p2="NS_URI2">
> <p1:config>
> <p2:node xsi:type="p2:NodeType">1</p2:node>
> <p1:config>
> <p1:container>
> and this xml fails the validation.
>
>
> How do I get rid of xsi:type attribute?
You can't. Without that information, the deserializer wouldn't know to
create NodeType instance from the P2 package.
>
> Thanks in advance,
> Alexey


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: How to save an object from one package to an anyType feature of the object from another package [message #428150 is a reply to message #428145] Wed, 11 March 2009 18:18 Go to previous messageGo to next message
koloale is currently offline koloaleFriend
Messages: 41
Registered: July 2009
Member
Ed Merks wrote:
> Alexey,
>
> Comments below.
>
>
> koloale wrote:
>> Hello!
>>
>> I have two EPackage's P1 and P2 both created form XSD. I deserialize
>> the xml document: <p2:node xmlns:p2="NS_URI2">1</p2:node> into EObject
>> obj from the P1 package and want to save this object to AnyType
>> feature of a 'Container' object from P2:
>> class Container {
>> /* @model containment="true"
>> * extendedMetaData="kind='element' name='config'"
>> */
>> EObject getConfig();
>> void setConfig(EObject value);
>> }
>>
>> I save deserialized object obj as follows:
>>
>> String featureName = obj.eContainingFeature().getName();
>> String namespace = obj.eClass().getEPackage().getNsURI();
>>
>> EStructuralFeature feature =
>> ExtendedMetaData.INSTANCE.demandFeature(namespace, featureName, false);
>> AnyType anyType = XMLTypeFactory.eINSTANCE.createAnyType();
>> anyType.getMixed().add(feature, configNode.getEObject());
>>
>> Container c = ..
>> c.setConfig(anyType);
>>
>> But when I serialize c object I get an xml with xsi:type:
>> <p1:container xmlns:p1="NS_URI1" xmlns:p2="NS_URI2">
>> <p1:config>
>> <p2:node xsi:type="p2:NodeType">1</p2:node>
>> <p1:config>
>> <p1:container>
>> and this xml fails the validation.
>>
>>
>> How do I get rid of xsi:type attribute?
> You can't. Without that information, the deserializer wouldn't know to
> create NodeType instance from the P2 package.
Ed, thanks for your answer!

At last I managed to save xml without xsi:type if I don't demand new
feature for anyType but use obj.eContainmentFeature(), i.e.

AnyType anyType = XMLTypeFactory.eINSTANCE.createAnyType();
anyType.getMixed().add(obj.eContainmentFeature(), obj);

It seems like a hack, but I realy wonder why deserializer wouldn't know
to create NodeType from P2 package as it knows that node element from p2
namespace therefore it should create object from P2 corresponding to
that element, the same as when p2:node is the root, e.g. through
DocumentRoot for P2 package.

Alexey
Re: How to save an object from one package to an anyType feature of the object from another package [message #428153 is a reply to message #428150] Wed, 11 March 2009 20:47 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Alexey,

Comments below.

koloale wrote:
> Ed Merks wrote:
>> Alexey,
>>
>> Comments below.
>>
>>
>> koloale wrote:
>>> Hello!
>>>
>>> I have two EPackage's P1 and P2 both created form XSD. I deserialize
>>> the xml document: <p2:node xmlns:p2="NS_URI2">1</p2:node> into
>>> EObject obj from the P1 package and want to save this object to
>>> AnyType feature of a 'Container' object from P2:
>>> class Container {
>>> /* @model containment="true"
>>> * extendedMetaData="kind='element' name='config'"
>>> */
>>> EObject getConfig();
>>> void setConfig(EObject value);
>>> }
>>>
>>> I save deserialized object obj as follows:
>>>
>>> String featureName = obj.eContainingFeature().getName();
>>> String namespace = obj.eClass().getEPackage().getNsURI();
>>>
>>> EStructuralFeature feature =
>>> ExtendedMetaData.INSTANCE.demandFeature(namespace, featureName, false);
>>> AnyType anyType = XMLTypeFactory.eINSTANCE.createAnyType();
>>> anyType.getMixed().add(feature, configNode.getEObject());
>>>
>>> Container c = ..
>>> c.setConfig(anyType);
>>>
>>> But when I serialize c object I get an xml with xsi:type:
>>> <p1:container xmlns:p1="NS_URI1" xmlns:p2="NS_URI2">
>>> <p1:config>
>>> <p2:node xsi:type="p2:NodeType">1</p2:node>
>>> <p1:config>
>>> <p1:container>
>>> and this xml fails the validation.
>>>
>>>
>>> How do I get rid of xsi:type attribute?
>> You can't. Without that information, the deserializer wouldn't know
>> to create NodeType instance from the P2 package.
> Ed, thanks for your answer!
>
> At last I managed to save xml without xsi:type if I don't demand new
> feature for anyType but use obj.eContainmentFeature(), i.e.
>
> AnyType anyType = XMLTypeFactory.eINSTANCE.createAnyType();
> anyType.getMixed().add(obj.eContainmentFeature(), obj);
>
> It seems like a hack, but I realy wonder why deserializer wouldn't
> know to create NodeType from P2 package as it knows that node element
> from p2 namespace therefore it should create object from P2
> corresponding to that element, the same as when p2:node is the root,
> e.g. through DocumentRoot for P2 package.
It sounds like you should have used
P2Package.Literals.DOCUMENT_ROOT__NODE as the feature above, rather than
demand creating one with that same name and namespace.
>
> Alexey


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:[CDO] Resources
Next Topic:Children or real containment
Goto Forum:
  


Current Time: Fri Apr 26 07:16:35 GMT 2024

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

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

Back to the top