Commit 652e19a3 authored by Simon Barner's avatar Simon Barner
Browse files

- Add getModel(ResourceSet resourceSet, Class<T> clazz, EFactory factory,...

- Add getModel(ResourceSet resourceSet, Class<T> clazz, EFactory factory, String fileExtension, Class<R> referenceClazz), a method that Obtains a model of a given type from a {@code resourceSet}, and creates the model if it does not exist yet (and adds it to the given {@code resourceSet}) and that returns {@code null} if the model could not be created.
parent d2c42fb2
......@@ -27,12 +27,14 @@ import java.util.List;
import java.util.Map;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EFactory;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceFactoryImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl;
import org.eclipse.emf.ecore.xmi.impl.XMLResourceFactoryImpl;
import org.eclipse.emf.ecore.xml.type.AnyType;
import org.fortiss.tooling.kernel.extension.data.ITopLevelElement;
import org.fortiss.tooling.kernel.model.IIdLabeled;
......@@ -44,7 +46,7 @@ import org.fortiss.tooling.kernel.service.IPersistencyService;
* @author mou
* @author $Author$
* @version $Rev$
* @ConQAT.Rating YELLOW Hash: 888395C3876DA7D2840C28CB2FDFE45B
* @ConQAT.Rating YELLOW Hash: 2E2846E7ECCBF06753A9F08A9C8E74D5
*/
public final class ResourceUtils {
......@@ -218,7 +220,7 @@ public final class ResourceUtils {
* be found.
*/
@SuppressWarnings("unchecked")
public static <T> T getModel(ResourceSet resourceSet, Class<T> clazz) {
public static <T extends EObject> T getModel(ResourceSet resourceSet, Class<T> clazz) {
for(Resource resource : resourceSet.getResources()) {
for(EObject obj : resource.getContents()) {
if(clazz.isAssignableFrom(obj.getClass())) {
......@@ -229,4 +231,77 @@ public final class ResourceUtils {
return null;
}
/**
* Obtains a model of a given type from a {@code resourceSet}, and creates the model if it does
* not exist yet (and adds it to the given {@code resourceSet}). Returns {@code null} if the
* model could not be created.
*
* @param resourceSet
* {@link ResourceSet} from which model of given type should be determined
* @param clazz
* Type of model to be returned.
* @param factory
* {@link EFactory} to create root model element in case the model does not exist in
* the {@code resourceSet} yet.
* @param fileExtension
* File extension to be used in case a new resource has to be created and added to
* the given given {@code resourceSet}. In case this parameter is {@code null}, this
* method effectively behaves like {@link #getModel(ResourceSet, Class)}.
* @param referenceClazz
* Type of root model element whose {@link Resource} {@link URI} should be used to
* derive the base name in case a new {@link Resource} has to be created. In case
* this parameter is {@code null}, the {@link URI} of the first {@link Resource} in
* the given {@link ResourceSet} is used.
*
* @return Model of a given type, or {@code null} if it could not be created.
*/
@SuppressWarnings("unchecked")
public static <T extends EObject, R extends EObject> T getModel(ResourceSet resourceSet,
Class<T> clazz, EFactory factory, String fileExtension, Class<R> referenceClazz) {
T existingModel = getModel(resourceSet, clazz);
if(existingModel != null) {
return existingModel;
}
if(fileExtension != null) {
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
.put(fileExtension, new XMLResourceFactoryImpl());
}
URI uri = null;
EObject obj = null;
if(referenceClazz != null) {
obj = getModel(resourceSet, referenceClazz);
if(obj != null) {
uri = obj.eResource().getURI();
}
} else if(!resourceSet.getResources().isEmpty()) {
// "resourceSet" is guaranteed to be non-empty
Resource resource = resourceSet.getResources().get(0);
uri = resource.getURI();
// Resources that are in the resource set are expected to be non-empty
obj = resource.getContents().get(0);
}
if(factory != null && fileExtension != null && uri != null && obj != null) {
uri = uri.trimFileExtension().appendFileExtension(fileExtension);
Resource resource = resourceSet.createResource(uri);
final T newModel = (T)factory.create(EcoreUtils.getEClassForClass(clazz));
ITopLevelElement modelContext = IPersistencyService.INSTANCE.getTopLevelElementFor(obj);
modelContext.runAsCommand(new Runnable() {
@Override
public void run() {
resource.getContents().add(newModel);
}
});
return newModel;
}
return null;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment