EMF XML Serialization - change xsi:type to another attribute [message #1847166] |
Sun, 17 October 2021 16:27  |
Eclipse User |
|
|
|
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 #1847219 is a reply to message #1847171] |
Tue, 19 October 2021 11:36  |
Eclipse User |
|
|
|
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
|
|
|
Powered by
FUDForum. Page generated in 0.03874 seconds