Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7e459d4729165005b46d63742040a51fe94dddb5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<div id="maincontent">
<div id="midcolumn">
<h1>Handling Custom EDataTypes</h1>
<p>
Edapt is a tool to migrate previously saved XMI-Files created using EMF (Eclipse Modeling Framework). 
A migration is required if the metamodel (Ecore) of the persisted entities (EObjects) in the file has changed due to model evolution. 
Edapt keeps a history of changes of an Ecore. This history is used to migrate models in XMI files. 
Edapt uses Dynamic EMF to load models from those files and migrates them to conform to the current metamodel version.
</p><p>
EMF offers EDataTypes for the most common Java types (String, Integer, ...) which can be used to type EAttributes. 
Moreover it is possible to specify custom EDataTypes which use a custom instance-class. 
When such a data type is specified and used in an EClass, EMF generates additional (de-)serialization code in the EFactory for the EPackage. 
To load and save XMI files with a model using those data type, custom code is required for (de-)serialization.
</p><p>
The alerted reader might have noticed that the usage of Dynamic EMF and custom datatypes don’t get along very well. 
Dynamic EMF always falls back to existing, generic EMF-classes, since no actual compiled classes are available for the model. 
So during (de)serialization Edapt uses EFactoryImpl which lacks all generated (and maybe even overridden and marked with @generated NOT) code. 
</p><p>
In order to overcome this issue, Edapt offers the org.eclipse.emf.edapt.factories extension point, which allows to register a custom EFactory implementation for a nsURI, which is then used to (de-)serialize files which conform to the nsURI.
</p><p>
<hr/>
<a>Listing 1</a> - Extension Point Registration
<pre class="codebox">
   &lt;extension
         point="org.eclipse.emf.edapt.factories"&gt;
      &lt;factory
            class="library.edapt.LibraryFactoryImpl"
            nsURI="http://library/v3"&gt;
      &lt;/factory&gt;
   &lt;/extension&gt;
</pre>
<hr/>
</p><p>
The easiest way to create such a custom factory for usage with Edapt is to subclass EFactoryImpl and to override createFromString(EDataType, String) and convertToString(EDataType, Object) respectively. 
Please note that eDataType.getClassifierID() does not necessarily give the same ids in a dynamic instance. 
The ids are generated and might vary. So it is not possible to simply copy the existing EFactory-code, but you have to adjust the switch() statements accordingly, e.g by identifying datatypes via the name attribute.
</p><p>
<hr/>
<a>Listing 2</a> - Example Custom Factory
<pre class="codebox">
public class LibraryFactoryImpl extends EFactoryImpl {

	@Override
	public Object createFromString(EDataType eDataType, String initialValue) {
		switch (eDataType.getName()) {
		case "MyCustomDataType":
			return createMyCustomDataTypeFromString(eDataType, initialValue);
		default:
			return super.createFromString(eDataType, initialValue);
		}
	}

	@Override
	public String convertToString(EDataType eDataType, Object instanceValue) {
		switch (eDataType.getName()) {
		case "MyCustomDataType":
			return convertMyCustomDataTypeToString(eDataType, instanceValue);
		default:
			return super.convertToString(eDataType, instanceValue);
		}
	}

	private String convertMyCustomDataTypeToString(EDataType eDataType, Object instanceValue) {/* TODO */return null;}
	private Object createMyCustomDataTypeFromString(EDataType eDataType, String initialValue) {/* TODO */return null;}
}
</pre>
<hr/>
</p>
</div>
</div>

Back to the top