Skip to main content



      Home
Home » Modeling » EMF » Two different XML custom serializations for the same model
Two different XML custom serializations for the same model [message #1835443] Thu, 03 December 2020 08:58 Go to next message
Eclipse UserFriend
Hello

I have an ecore model, and I need to have two additional XML serializations (for exchange with third party tools). The two formats are very similar, and I think I can achieve both serialization using extended metadata.

I have created two different resource factories, using in each case a different BasicExtendedMetaData instance, with the appropriate settings, in the option OPTION_EXTENDED_META_DATA of the resource's default load and save options.

Things seems to work, but I have noticed that sometimes there are interferences, and I end up with the wrong format. After some debugging, I noticed that BasicExtendedMetaData directly modifies the loaded EPackage and EClasses (adding annotations and modifying the metadata field). So, after the first resource is loaded, the classes are modified and then, even if there are two instances of BasicExtendedMetaData , it uses only the settings of the first one used.

The question is how I can use two different extended metadata instances, only within each resource, without modifying the loaded EPackage as a side effect?

A related question concerns the XML namespaces. So far, I test using the same XML namespace URI in both serializations (with different file extensions) and it matches the EPackage nsURI. But at the end, I will need to support two different XML namespaces URIs. In my first tries to have different URIs, I have managed to make it work for saving, overriding ExtendedMetadat.getNamespace(EPackage), but on loading it fails finding the package in the Registry. Is there something that I am missing?

A last question, for one of the serializations I need to be sure that the XML root object is unqualified
<root xmlns="http://the/namespace/uri">
is there an option to force this serialization?

Thanks for the help


Re: Two different XML custom serializations for the same model [message #1835453 is a reply to message #1835443] Thu, 03 December 2020 11:49 Go to previous messageGo to next message
Eclipse UserFriend
Look closely at this implementation of this constructor org.eclipse.emf.ecore.util.BasicExtendedMetaData.BasicExtendedMetaData(String, Registry, Map<EModelElement, EAnnotation>). It gives the possibility to use your own URI for looking up annotations, your own package registry, your own "external" map of annotations" and an annotation cache/map to use (instead of directly stored information on the Ecore model).

In the end, lookup of a namespace to map it to a package is done via org.eclipse.emf.ecore.util.BasicExtendedMetaData.getPackage(String), so you can specialize that (or the it uses registry).

The "unqualifed" root element seems tricky because this is not really unqualified, it is implicitly qualified and all nested elements will then also be implicitly qualified. I think you'll have to mess with the DocumentRoot's getXMLNSPrefixMap() for this purpose, i.e., a mapping from the "" prefix to the namespace.
Re: Two different XML custom serializations for the same model [message #1835461 is a reply to message #1835453] Thu, 03 December 2020 13:38 Go to previous messageGo to next message
Eclipse UserFriend
Hello Ed,

Quote:
Look closely at this implementation of this constructor org.eclipse.emf.ecore.util.BasicExtendedMetaData.BasicExtendedMetaData(String, Registry, Map<EModelElement, EAnnotation>)

In the end, lookup of a namespace to map it to a package is done via org.eclipse.emf.ecore.util.BasicExtendedMetaData.getPackage(String), so you can specialize that (or the it uses registry).


Ok, this solved my issues. Thanks. But a question popped into my head. I want to be sure to be using the ResourcerSet's local package registry , but I do not see a way to get it from the resource factory that instantiate the ExtendedMetadata. is there some API to get that, or do I have to do it in some application specific way?

Quote:
I think you'll have to mess with the DocumentRoot's getXMLNSPrefixMap() for this purpose, i.e., a mapping from the "" prefix to the namespace.


ok, I will take a look, but I have never worked with the DocumentRoot, any hint where to start looking at?

Thanks for your prompt help
Re: Two different XML custom serializations for the same model [message #1835479 is a reply to message #1835461] Fri, 04 December 2020 02:54 Go to previous messageGo to next message
Eclipse UserFriend
Yes, the resource factory doesn't know in which resource set the resource will be placed, so if you create a BasicExtendedMetadata instance rather than just use Boolean.TRUE, you won't know that context information.

Does your model even have a DocumentRoot? Is it a model derived from an XML Schema?
Re: Two different XML custom serializations for the same model [message #1835482 is a reply to message #1835479] Fri, 04 December 2020 03:54 Go to previous message
Eclipse UserFriend
Hi Ed,

Ok, understood for the BasicExtendedMetadata

Quote:
Does your model even have a DocumentRoot? Is it a model derived from an XML Schema?


No, my model was created from scratch in ecore, without XML in mind. I was using the standard XMI serialization. Now, I am trying to serialize/deserialize instances to/from some XML tool exchange formats (the model is about railway information, and there are several XML "standard" exchange formats).

At first, I thought about doing a bi-directional model transformation (from my model to a model generated from the XML schema of the standards) but I noticed the differences were not very big (mainly different tag names, and attributes represented as nested elements instead). So, I imagined it was feasible with the extended metadata, and I embarked on this resource factory approach.

The big advantage is that people can use our tools directly on files coming from the third-party tools, without any translation. But if if gets too complicated, I may simply fall back to make the transformation as a big switch visitor.

Thanks for your time anywway

[Updated on: Fri, 04 December 2020 03:56] by Moderator

Previous Topic:Feature IDs in generated code
Next Topic:[Xcore] Standalone app generates EJavaObject types
Goto Forum:
  


Current Time: Sat Nov 08 02:44:48 EST 2025

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

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

Back to the top