Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » EMF XML Serialization - change xsi:type to another attribute
EMF XML Serialization - change xsi:type to another attribute [message #1847166] Sun, 17 October 2021 20:27 Go to next message
Daniel Winkels is currently offline Daniel WinkelsFriend
Messages: 3
Registered: September 2021
Junior Member
Hello,

this is my problem: i have a model with an abstract base class (DialogElement) and derived classes (Textbox, Textarea, etc..).
In the class DialogElement is a derived attribute "type" (EString) which is implemented to have the value of the class name of the instance. So i get

<element xsi:type="Textbox" id="firstID" type="Textbox" length="40"/>
<element xsi:type="Textarea" id="secondTextArea" type="Textarea"/>

Background: i am creating an editor for a 3rd party application without xsd schema. To be compatible, "xsi:type" needs to be
a) gone
b) renamed to "type"

I would like to change the implementation by extending an implementation class of the emf framework (i already did this for XMLHelper) but i just cannot find the class which needs to be changed.

Can somebody give me a hint?

Thank you very much.

Greetings from Cologne.

Daniel
Re: EMF XML Serialization - change xsi:type to another attribute [message #1847171 is a reply to message #1847166] Mon, 18 October 2021 06:46 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
I'm a little bit doubtful that a schema can represent metadata such as xsi:type type as data. I suppose you could override org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.saveTypeAttribute(EClass) to save it differently but next you'll want to load it as well, in which case probably an org.eclipse.emf.ecore.xmi.impl.SAXXMLHandler.getXSIType() override would do the trick.

Ed Merks
Professional Support: https://www.macromodeling.com/
Re: EMF XML Serialization - change xsi:type to another attribute [message #1847172 is a reply to message #1847171] Mon, 18 October 2021 07:01 Go to previous messageGo to next message
Daniel Winkels is currently offline Daniel WinkelsFriend
Messages: 3
Registered: September 2021
Junior Member
Hello Ed,

i think, this is the hint / answer i needed :)

I will try it and post the result.

Thank you.
Re: EMF XML Serialization - change xsi:type to another attribute [message #1847219 is a reply to message #1847171] Tue, 19 October 2021 15:36 Go to previous message
Daniel Winkels is currently offline Daniel WinkelsFriend
Messages: 3
Registered: September 2021
Junior Member
Hello Ed,

the problem has become simpler.

Case 1: xsi:type is present
Nothing to be done. The 3rd party software ignores the attribute. So there is no need to change the serialization.
<element xsi:type="Textbox" id="textbox6" name="textbox6" type="Textbox" page="page2" section="section3" row="row14" column="column11" label="Text1"/>


Case 2: xsi:type not present (i want to load a model from the 3rd party application)
<element id="textbox6" name="textbox6" type="Textbox" page="page2" section="section3" row="row14" column="column11" label="Text1"/>


Here I extended SAXXMLHandler with my custom implementation. If the attribute "xsi:type" is missing read the value of "type", which has to contain the correct value by design.

public class DialogSAXXMLHandler extends SAXXMLHandler {

	public DialogSAXXMLHandler(XMLResource xmiResource, XMLHelper helper, Map<?, ?> options) {
		super(xmiResource, helper, options);
	}

	@Override
	protected String getXSIType() {
		String xsiValue = attribs.getValue(ExtendedMetaData.XSI_URI, XMLResource.TYPE);
		if (xsiValue == null || xsiValue.isEmpty()) {
			return attribs.getValue("type");
		}
		return isNamespaceAware ? attribs.getValue(ExtendedMetaData.XSI_URI, XMLResource.TYPE)
				: attribs.getValue(TYPE_ATTRIB);
	}
}




For those who are as clueless as i was:
I needed to extend XMLLoadImpl to get an instance of my DialogSAXXMLHandler.

public class DialogXMLLoadImpl extends XMLLoadImpl {

	public DialogXMLLoadImpl(XMLHelper helper) {
		super(helper);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected DefaultHandler makeDefaultHandler() {
		return new DialogSAXXMLHandler(resource, helper, options);
	}

}


The DialogXMLLoadImpl instance is created in my implementation of the Resource.

public class DialogResourceImpl extends XMLResourceImpl {
	/**
	 * Creates an instance of the resource.
	 * <!-- begin-user-doc --> <!--
	 * end-user-doc -->
	 * @param uri the URI of the new resource.
	 * @generated
	 */
	public DialogResourceImpl(URI uri) {
		super(uri);
	}

	@Override
	protected XMLHelper createXMLHelper() {
		return new DialogResourceXMLHelper();
	}

	@Override
	protected XMLLoad createXMLLoad() {
		return new DialogXMLLoadImpl(createXMLHelper());
	}

	/**
	 * @since 2.7
	 */
	@Override
	protected XMLLoad createXMLLoad(Map<?, ?> options) {
		return createXMLLoad();
	}

	@Override
	protected XMLSave createXMLSave() {
		return new DialogXMLSaveImpl(createXMLHelper());
	}

	/**
	 * @since 2.7
	 */
	@Override
	protected XMLSave createXMLSave(Map<?, ?> options) {
		return createXMLSave();
	}

} // DialogResourceImpl


Previous Topic:XCore: Bundle 'org.eclipse.emf.ecore.xcore.lib' cannot be resolved
Next Topic:Show diagrams in a graphical manner
Goto Forum:
  


Current Time: Fri Apr 26 12:47:36 GMT 2024

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

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

Back to the top