Commit 4f736ffc authored by Alexander Diewald's avatar Alexander Diewald

EMFResourceUtils: Cleanup & API consolidation

Issue-Ref: 3993
Issue-Url: https://af3-developer.fortiss.org/issues/3993Signed-off-by: Alexander Diewald's avatarAlexander Diewald <diewald@fortiss.org>
parent bccc177f
CompositionUtils.java 34c0a191bd0fb4176c94b4d61abb5c88a679d5e8 GREEN
EMFResourceUtils.java cce1077c902e76d701412c6538dd15d6cafb4763 YELLOW
EMFResourceUtils.java 8d878ffbe17ad916699bf6c2ac64fd4a1498c215 YELLOW
EcoreSerializerBase.java 0a0c2969d793d2e68094c55c8f7b0a662ef6e5d5 GREEN
EcoreUtils.java 18416b5c214410a02eb35596fd807a1cc27d6b35 GREEN
ExtensionPointUtils.java 7ce63242b49eb9a7cd4eaadd223f5ebce1dfd75b GREEN
......
......@@ -39,6 +39,7 @@ import org.fortiss.tooling.kernel.ToolingKernelActivator;
* Utility methods used for Eclipse resource storage.
*
* @author hoelzl
* @author diewald
*/
public final class EMFResourceUtils {
/**
......@@ -57,6 +58,34 @@ public final class EMFResourceUtils {
r.save(buildOptionsMap());
}
/**
* Loads the root model element from the given {@link URI}. Logs errors to the console and
* returns <code>null</code>.
*
* @param uri
* the resource {@link URI}.
* @return the root {@link EObject}
*/
public static EObject loadModelFromFile(URI uri) {
try {
return loadModelFromFileWithUnknownFeatures(uri).getFirst();
} catch(Exception ex) {
error(ToolingKernelActivator.getDefault(),
"Failed to load model from " + uri.toString());
}
return null;
}
/** Refresh the workspace. */
public static void refreshWorkspace() {
try {
// refresh the workspace
getWorkspace().getRoot().refreshLocal(IResource.DEPTH_INFINITE, null);
} catch(Exception e) {
error(ToolingKernelActivator.getDefault(), "Cannot refresh workspace!", e);
}
}
/**
* Build the map of options used for storage of EMF models.
*
......@@ -85,45 +114,9 @@ public final class EMFResourceUtils {
* the resource {@link URI}.
* @return the root {@link EObject}.
*/
public static Pair<EObject, Map<EObject, AnyType>>
private static Pair<EObject, Map<EObject, AnyType>>
loadModelFromFileWithUnknownFeatures(URI uri) {
return loadModelFromFileWithUnknownFeatures(uri, true);
}
/**
* Loads the root model element from the given {@link URI}. Returns {@code null} in case of
* errors, and optionally logs errors to the console.
*
* @param uri
* the resource {@link URI}.
* @param logError
* Whether to log errors.
* @return the root {@link EObject}.
*/
public static Pair<EObject, Map<EObject, AnyType>> loadModelFromFileWithUnknownFeatures(URI uri,
boolean logError) {
try {
ResourceSet rset = new ResourceSetImpl();
Resource r;
r = rset.createResource(uri);
r.load(EMFResourceUtils.buildOptionsMap());
Map<EObject, AnyType> unknownFeatures = r instanceof XMIResource
? ((XMIResource)r).getEObjectToExtensionMap() : new HashMap<EObject, AnyType>();
if(!r.getContents().isEmpty()) {
// get(0) because the list is not empty by the check above and because we do not
// know which element is relevant, so we just take the first one.
return new Pair<EObject, Map<EObject, AnyType>>(r.getContents().get(0),
unknownFeatures);
}
} catch(IOException ex) {
if(logError) {
error(ToolingKernelActivator.getDefault(),
"Failed to load model from " + uri.toString(), ex);
}
} catch(Throwable t) {
return null;
}
return null;
return loadModelFromFileWithUnknownFeatures(uri, EObject.class);
}
/**
......@@ -137,8 +130,8 @@ public final class EMFResourceUtils {
* @return the root {@link EObject} of class type topLevelClass (first root which is found of
* this type is returned)
*/
public static Pair<EObject, Map<EObject, AnyType>> loadModelFromFileWithUnknownFeatures(URI uri,
Class<?> topLevelClass) {
private static Pair<EObject, Map<EObject, AnyType>>
loadModelFromFileWithUnknownFeatures(URI uri, Class<?> topLevelClass) {
try {
ResourceSet rset = new ResourceSetImpl();
Resource r;
......@@ -147,10 +140,10 @@ public final class EMFResourceUtils {
Map<EObject, AnyType> unknownFeatures = r instanceof XMIResource
? ((XMIResource)r).getEObjectToExtensionMap() : new HashMap<EObject, AnyType>();
if(!r.getContents().isEmpty()) {
for(EObject content : r.getContents()) {
if(topLevelClass.isAssignableFrom(content.getClass()))
if(topLevelClass.isAssignableFrom(content.getClass())) {
return new Pair<EObject, Map<EObject, AnyType>>(content, unknownFeatures);
}
}
}
} catch(IOException ex) {
......@@ -159,32 +152,4 @@ public final class EMFResourceUtils {
}
return null;
}
/**
* Loads the root model element from the given {@link URI}. Logs errors to the console and
* returns <code>null</code>.
*
* @param uri
* the resource {@link URI}.
* @return the root {@link EObject}
*/
public static EObject loadModelFromFile(URI uri) {
try {
return loadModelFromFileWithUnknownFeatures(uri).getFirst();
} catch(Exception ex) {
error(ToolingKernelActivator.getDefault(),
"Failed to load model from " + uri.toString());
}
return null;
}
/** Refresh the workspace. */
public static void refreshWorkspace() {
try {
// refresh the workspace
getWorkspace().getRoot().refreshLocal(IResource.DEPTH_INFINITE, null);
} catch(Exception e) {
error(ToolingKernelActivator.getDefault(), "Cannot refresh workspace!", e);
}
}
}
Markdown is supported
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