@@ -30,41 +30,41 @@ public class EcoreUtilities {
30
30
public static XMLSave .XMLTypeInfo noTypeInfo ;
31
31
32
32
static {
33
- noTypeInfo = new XMLSave .XMLTypeInfo () {
33
+ EcoreUtilities . noTypeInfo = new XMLSave .XMLTypeInfo () {
34
34
35
35
@ Override
36
- public boolean shouldSaveType (EClass objectType , EClass featureType ,
37
- EStructuralFeature feature ) {
36
+ public boolean shouldSaveType (final EClass objectType , final EClass featureType ,
37
+ final EStructuralFeature feature ) {
38
38
return false ;
39
39
}
40
40
41
41
@ Override
42
- public boolean shouldSaveType (EClass objectType , EClassifier featureType ,
43
- EStructuralFeature feature ) {
42
+ public boolean shouldSaveType (final EClass objectType , final EClassifier featureType ,
43
+ final EStructuralFeature feature ) {
44
44
return false ;
45
45
}
46
46
};
47
47
}
48
48
49
49
/**
50
50
* Gets root EObject of given xmi file path
51
- *
51
+ *
52
52
* @param xmiFileFullPath file path of xmi file
53
53
* @return root @EObject
54
54
* @throws IOException
55
55
*/
56
56
public static EObject getRootObject (final String xmiFileFullPath ) throws IOException {
57
- return getRootObject (URI .createPlatformResourceURI (xmiFileFullPath , true ));
57
+ return EcoreUtilities . getRootObject (URI .createPlatformResourceURI (xmiFileFullPath , true ));
58
58
}
59
59
60
60
/**
61
61
* Gets root EObject of given xmi file path
62
- *
62
+ *
63
63
* @param xmiFileFullPath file path of xmi file
64
64
* @return root @EObject
65
65
* @throws IOException
66
66
*/
67
- public static EObject getRootObject (URI uri ) throws IOException {
67
+ public static EObject getRootObject (final URI uri ) throws IOException {
68
68
@ SuppressWarnings ("rawtypes" )
69
69
final ModelIO modelIO = new ModelIO <>();
70
70
@ SuppressWarnings ("rawtypes" )
@@ -81,20 +81,20 @@ public static EObject getRootObject(URI uri) throws IOException {
81
81
82
82
/**
83
83
* Gets root EObject of given xmi file path
84
- *
84
+ *
85
85
* @param xmiFileFullPath file path of xmi file
86
86
* @return root @EObject
87
87
* @throws IOException
88
88
*/
89
89
@ SuppressWarnings ({"rawtypes" , "unchecked" })
90
90
public static Resource loadInstanceRoot (final String xmiFileFullPath ) throws IOException {
91
- Map options = new HashMap ();
91
+ final Map options = new HashMap ();
92
92
options .put (XMLResource .OPTION_SCHEMA_LOCATION , Boolean .TRUE );
93
93
94
- ResourceSetImpl resourceSet = new ResourceSetImpl ();
94
+ final ResourceSetImpl resourceSet = new ResourceSetImpl ();
95
95
resourceSet .getResourceFactoryRegistry ().getExtensionToFactoryMap ()
96
96
.put (Resource .Factory .Registry .DEFAULT_EXTENSION , new XMIResourceFactoryImpl ());
97
- Resource resource =
97
+ final Resource resource =
98
98
resourceSet .getResource (URI .createPlatformResourceURI (xmiFileFullPath , true ), true );
99
99
resource .load (options );
100
100
if (resource .isLoaded ()) {
@@ -104,48 +104,50 @@ public static Resource loadInstanceRoot(final String xmiFileFullPath) throws IOE
104
104
}
105
105
106
106
/**
107
- *
107
+ *
108
108
* @param eObject possibly the package to find all @EClass's under it
109
109
* @return @List of @EClass names
110
110
*/
111
- public static List <String > getAllEClassNames (EObject eObject ) {
112
- List <EClass > classes = new ArrayList <>();
113
- recursiveGetEClasses (eObject , classes );
114
- List <String > classNames = new ArrayList <>();
115
- for (EClass eClass : classes ) {
111
+ public static List <String > getAllEClassNames (final EObject eObject ) {
112
+ final List <EClass > classes = new ArrayList <>();
113
+ EcoreUtilities . recursiveGetEClasses (eObject , classes );
114
+ final List <String > classNames = new ArrayList <>();
115
+ for (final EClass eClass : classes ) {
116
116
classNames .add (eClass .getName ());
117
117
}
118
118
return classNames ;
119
119
}
120
120
121
121
/**
122
- *
122
+ *
123
123
* @param eObject possibly the package to find all @EClass under it
124
124
* @return @List of all @EClass
125
125
*/
126
- public static List <EClass > getAllEClass (EObject eObject ) {
127
- List <EClass > classes = new ArrayList <>();
128
- recursiveGetEClasses (eObject , classes );
126
+ public static List <EClass > getAllEClass (final EObject eObject ) {
127
+ final List <EClass > classes = new ArrayList <>();
128
+ EcoreUtilities . recursiveGetEClasses (eObject , classes );
129
129
return classes ;
130
130
}
131
131
132
- private static void recursiveGetEClasses (EObject object , List <EClass > classes ) {
133
- for (EObject eObject : object .eContents ()) {
134
- if (eObject instanceof EClass )
132
+ private static void recursiveGetEClasses (final EObject object , final List <EClass > classes ) {
133
+ for (final EObject eObject : object .eContents ()) {
134
+ if (eObject instanceof EClass ) {
135
135
classes .add ((EClass ) eObject );
136
- else if (eObject instanceof EPackage )
137
- recursiveGetEClasses (eObject , classes );
136
+ } else if (eObject instanceof EPackage ) {
137
+ EcoreUtilities .recursiveGetEClasses (eObject , classes );
138
+ }
138
139
}
139
140
}
140
141
141
142
/**
142
- *
143
+ *
143
144
* @param @EObject object to be set
144
145
* @param name reference name
145
146
* @param newVal new value
146
147
*/
147
- public static void eSetAttributeByName (EObject eObject , String name , Object newVal ) {
148
- for (EAttribute eAttribute : eObject .eClass ().getEAllAttributes ()) {
148
+ public static void eSetAttributeByName (final EObject eObject , final String name ,
149
+ final Object newVal ) {
150
+ for (final EAttribute eAttribute : eObject .eClass ().getEAllAttributes ()) {
149
151
if (eAttribute .getName ().equals (name )) {
150
152
eObject .eSet (eAttribute , newVal );
151
153
break ;
@@ -154,14 +156,15 @@ public static void eSetAttributeByName(EObject eObject, String name, Object newV
154
156
}
155
157
156
158
/**
157
- *
159
+ *
158
160
* @param @EObject object to be set
159
161
* @param name reference name
160
162
* @param newVal new value
161
163
*/
162
164
@ SuppressWarnings ({"unchecked" , "rawtypes" })
163
- public static void eSetReferenceByName (EObject eObject , String name , Object newVal ) {
164
- for (EReference eReference : eObject .eClass ().getEAllReferences ()) {
165
+ public static void eSetReferenceByName (final EObject eObject , final String name ,
166
+ final Object newVal ) {
167
+ for (final EReference eReference : eObject .eClass ().getEAllReferences ()) {
165
168
if (eReference .getName ().equals (name )) {
166
169
if (eReference .isMany ()) {
167
170
((List ) eObject .eGet (eReference )).add (newVal );
@@ -174,94 +177,105 @@ public static void eSetReferenceByName(EObject eObject, String name, Object newV
174
177
}
175
178
176
179
/**
177
- *
180
+ *
178
181
* @param root @EObject
179
182
* @param className class name to be find
180
183
* @return
181
184
*/
182
- public static EClass findEClass (EObject root , String className ) {
183
- List <EClass > allEClass = getAllEClass (root );
185
+ public static EClass findEClass (final EObject root , final String className ) {
186
+ final List <EClass > allEClass = EcoreUtilities . getAllEClass (root );
184
187
return allEClass .stream ().filter (c -> c .getName ().equals (className )).findFirst ().orElse (null );
185
188
}
186
189
187
190
/**
188
- *
191
+ *
189
192
* @param container
190
193
* @param eObject
191
194
* @return
192
195
*/
193
- public static EReference getContainmentEReference (EObject container , EObject eObject ) {
194
- for (EReference eReference : container .eClass ().getEAllReferences ()) {
196
+ public static EReference getContainmentEReference (final EObject container ,
197
+ final EObject eObject ) {
198
+ for (final EReference eReference : container .eClass ().getEAllReferences ()) {
195
199
if ((eObject .eClass ().getName ().equals (eReference .getEReferenceType ().getName ())
196
200
|| eObject .eClass ().getEAllSuperTypes ().stream ()
197
201
.anyMatch (s -> s .getName ().equals (eReference .getEReferenceType ().getName ())))
198
- && eReference .isContainment ())
202
+ && eReference .isContainment ()) {
199
203
return eReference ;
204
+ }
200
205
}
201
206
return null ;
202
207
}
203
208
204
209
/**
205
210
* Puts given dynamic EObject to container
206
- *
211
+ *
207
212
* @param container
208
213
* @param eObject
209
214
*/
210
215
@ SuppressWarnings ({"unchecked" , "rawtypes" })
211
- public static void putIntoContainer (EObject container , EObject eObject ) {
212
- EReference eReference = getContainmentEReference (container , eObject );
216
+ public static void putIntoContainer (final EObject container , final EObject eObject ) {
217
+ final EReference eReference = EcoreUtilities . getContainmentEReference (container , eObject );
213
218
if (eReference != null ) {
214
- if (eReference .isMany ())
219
+ if (eReference .isMany ()) {
215
220
((List ) container .eGet (eReference )).add (eObject );
216
- else
221
+ } else {
217
222
container .eSet (eReference , eObject );
223
+ }
218
224
}
219
225
}
220
226
221
227
/**
222
228
* Saves given @EObject to given file path
223
- *
229
+ *
224
230
* @param root @EObject to be saved
225
231
* @param savePath file location
226
232
*/
227
233
@ SuppressWarnings ({"unchecked" , "rawtypes" })
228
- public static void saveResource (EObject root , String savePath ) {
229
- ResourceSet resourceSet = new ResourceSetImpl ();
234
+ public static void saveResource (final EObject root , final String savePath ) {
235
+ final ResourceSet resourceSet = new ResourceSetImpl ();
230
236
resourceSet .getResourceFactoryRegistry ().getExtensionToFactoryMap ().put ("*" ,
231
237
new XMLResourceFactoryImpl ());
232
- Resource resource = resourceSet .createResource (URI .createFileURI (savePath ));
238
+ final Resource resource = resourceSet .createResource (URI .createFileURI (savePath ));
233
239
resource .getContents ().add (root );
234
240
235
- Map options = new HashMap ();
241
+ final Map options = new HashMap ();
236
242
options .put (XMLResource .OPTION_SCHEMA_LOCATION , Boolean .TRUE );
237
243
// options.put(XMLResource.OPTION_SAVE_TYPE_INFORMATION, noTypeInfo);
238
244
try {
239
245
resource .save (options );
240
- } catch (IOException e ) {
246
+ } catch (final IOException e ) {
241
247
e .printStackTrace ();
242
248
}
243
249
}
244
250
245
251
246
252
/**
247
253
* Saves given @EObject to its resource.
248
- *
254
+ *
249
255
* @param root @EObject
250
256
*/
251
257
@ SuppressWarnings ({"rawtypes" , "unchecked" })
252
- public static void saveResource (EObject root ) {
253
- Map options = new HashMap ();
258
+ public static void saveResource (final EObject root ) {
259
+ final Map options = new HashMap ();
254
260
options .put (XMLResource .OPTION_SCHEMA_LOCATION , Boolean .TRUE );
255
261
// options.put(XMLResource.OPTION_SAVE_TYPE_INFORMATION, noTypeInfo);
256
262
257
263
try {
258
264
root .eResource ().save (options );
259
- } catch (IOException e ) {
265
+ } catch (final IOException e ) {
260
266
e .printStackTrace ();
261
267
}
262
268
}
263
269
264
- public static EClass findEClass (List <EClass > allEClasses , String className ) {
270
+ public static EClass findEClass (final List <EClass > allEClasses , final String className ) {
265
271
return allEClasses .stream ().filter (c -> c .getName ().equals (className )).findFirst ().orElse (null );
266
272
}
273
+
274
+ @ SuppressWarnings ("unchecked" )
275
+ public static void saveResource (final EObject root , final URI uri ) {
276
+ @ SuppressWarnings ("rawtypes" )
277
+ final ModelIO modelIO = new ModelIO <>();
278
+
279
+ modelIO .write (uri , root );
280
+ }
267
281
}
0 commit comments