Skip to content

Commit 09dc08c

Browse files
author
emre.kirmizi
committed
minor updates.
1 parent c307b0d commit 09dc08c

File tree

1 file changed

+71
-57
lines changed
  • Source/eu.modelwriter.configuration/src/eu/modelwriter/configuration/internal

1 file changed

+71
-57
lines changed

Source/eu.modelwriter.configuration/src/eu/modelwriter/configuration/internal/EcoreUtilities.java

Lines changed: 71 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -30,41 +30,41 @@ public class EcoreUtilities {
3030
public static XMLSave.XMLTypeInfo noTypeInfo;
3131

3232
static {
33-
noTypeInfo = new XMLSave.XMLTypeInfo() {
33+
EcoreUtilities.noTypeInfo = new XMLSave.XMLTypeInfo() {
3434

3535
@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) {
3838
return false;
3939
}
4040

4141
@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) {
4444
return false;
4545
}
4646
};
4747
}
4848

4949
/**
5050
* Gets root EObject of given xmi file path
51-
*
51+
*
5252
* @param xmiFileFullPath file path of xmi file
5353
* @return root @EObject
5454
* @throws IOException
5555
*/
5656
public static EObject getRootObject(final String xmiFileFullPath) throws IOException {
57-
return getRootObject(URI.createPlatformResourceURI(xmiFileFullPath, true));
57+
return EcoreUtilities.getRootObject(URI.createPlatformResourceURI(xmiFileFullPath, true));
5858
}
5959

6060
/**
6161
* Gets root EObject of given xmi file path
62-
*
62+
*
6363
* @param xmiFileFullPath file path of xmi file
6464
* @return root @EObject
6565
* @throws IOException
6666
*/
67-
public static EObject getRootObject(URI uri) throws IOException {
67+
public static EObject getRootObject(final URI uri) throws IOException {
6868
@SuppressWarnings("rawtypes")
6969
final ModelIO modelIO = new ModelIO<>();
7070
@SuppressWarnings("rawtypes")
@@ -81,20 +81,20 @@ public static EObject getRootObject(URI uri) throws IOException {
8181

8282
/**
8383
* Gets root EObject of given xmi file path
84-
*
84+
*
8585
* @param xmiFileFullPath file path of xmi file
8686
* @return root @EObject
8787
* @throws IOException
8888
*/
8989
@SuppressWarnings({"rawtypes", "unchecked"})
9090
public static Resource loadInstanceRoot(final String xmiFileFullPath) throws IOException {
91-
Map options = new HashMap();
91+
final Map options = new HashMap();
9292
options.put(XMLResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE);
9393

94-
ResourceSetImpl resourceSet = new ResourceSetImpl();
94+
final ResourceSetImpl resourceSet = new ResourceSetImpl();
9595
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
9696
.put(Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl());
97-
Resource resource =
97+
final Resource resource =
9898
resourceSet.getResource(URI.createPlatformResourceURI(xmiFileFullPath, true), true);
9999
resource.load(options);
100100
if (resource.isLoaded()) {
@@ -104,48 +104,50 @@ public static Resource loadInstanceRoot(final String xmiFileFullPath) throws IOE
104104
}
105105

106106
/**
107-
*
107+
*
108108
* @param eObject possibly the package to find all @EClass's under it
109109
* @return @List of @EClass names
110110
*/
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) {
116116
classNames.add(eClass.getName());
117117
}
118118
return classNames;
119119
}
120120

121121
/**
122-
*
122+
*
123123
* @param eObject possibly the package to find all @EClass under it
124124
* @return @List of all @EClass
125125
*/
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);
129129
return classes;
130130
}
131131

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) {
135135
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+
}
138139
}
139140
}
140141

141142
/**
142-
*
143+
*
143144
* @param @EObject object to be set
144145
* @param name reference name
145146
* @param newVal new value
146147
*/
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()) {
149151
if (eAttribute.getName().equals(name)) {
150152
eObject.eSet(eAttribute, newVal);
151153
break;
@@ -154,14 +156,15 @@ public static void eSetAttributeByName(EObject eObject, String name, Object newV
154156
}
155157

156158
/**
157-
*
159+
*
158160
* @param @EObject object to be set
159161
* @param name reference name
160162
* @param newVal new value
161163
*/
162164
@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()) {
165168
if (eReference.getName().equals(name)) {
166169
if (eReference.isMany()) {
167170
((List) eObject.eGet(eReference)).add(newVal);
@@ -174,94 +177,105 @@ public static void eSetReferenceByName(EObject eObject, String name, Object newV
174177
}
175178

176179
/**
177-
*
180+
*
178181
* @param root @EObject
179182
* @param className class name to be find
180183
* @return
181184
*/
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);
184187
return allEClass.stream().filter(c -> c.getName().equals(className)).findFirst().orElse(null);
185188
}
186189

187190
/**
188-
*
191+
*
189192
* @param container
190193
* @param eObject
191194
* @return
192195
*/
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()) {
195199
if ((eObject.eClass().getName().equals(eReference.getEReferenceType().getName())
196200
|| eObject.eClass().getEAllSuperTypes().stream()
197201
.anyMatch(s -> s.getName().equals(eReference.getEReferenceType().getName())))
198-
&& eReference.isContainment())
202+
&& eReference.isContainment()) {
199203
return eReference;
204+
}
200205
}
201206
return null;
202207
}
203208

204209
/**
205210
* Puts given dynamic EObject to container
206-
*
211+
*
207212
* @param container
208213
* @param eObject
209214
*/
210215
@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);
213218
if (eReference != null) {
214-
if (eReference.isMany())
219+
if (eReference.isMany()) {
215220
((List) container.eGet(eReference)).add(eObject);
216-
else
221+
} else {
217222
container.eSet(eReference, eObject);
223+
}
218224
}
219225
}
220226

221227
/**
222228
* Saves given @EObject to given file path
223-
*
229+
*
224230
* @param root @EObject to be saved
225231
* @param savePath file location
226232
*/
227233
@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();
230236
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*",
231237
new XMLResourceFactoryImpl());
232-
Resource resource = resourceSet.createResource(URI.createFileURI(savePath));
238+
final Resource resource = resourceSet.createResource(URI.createFileURI(savePath));
233239
resource.getContents().add(root);
234240

235-
Map options = new HashMap();
241+
final Map options = new HashMap();
236242
options.put(XMLResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE);
237243
// options.put(XMLResource.OPTION_SAVE_TYPE_INFORMATION, noTypeInfo);
238244
try {
239245
resource.save(options);
240-
} catch (IOException e) {
246+
} catch (final IOException e) {
241247
e.printStackTrace();
242248
}
243249
}
244250

245251

246252
/**
247253
* Saves given @EObject to its resource.
248-
*
254+
*
249255
* @param root @EObject
250256
*/
251257
@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();
254260
options.put(XMLResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE);
255261
// options.put(XMLResource.OPTION_SAVE_TYPE_INFORMATION, noTypeInfo);
256262

257263
try {
258264
root.eResource().save(options);
259-
} catch (IOException e) {
265+
} catch (final IOException e) {
260266
e.printStackTrace();
261267
}
262268
}
263269

264-
public static EClass findEClass(List<EClass> allEClasses, String className) {
270+
public static EClass findEClass(final List<EClass> allEClasses, final String className) {
265271
return allEClasses.stream().filter(c -> c.getName().equals(className)).findFirst().orElse(null);
266272
}
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+
}
267281
}

0 commit comments

Comments
 (0)