Index: src/org/eclipse/gmf/runtime/emf/core/edit/IDemuxedMListener.java =================================================================== RCS file: /home/technology/org.eclipse.gmf/plugins/org.eclipse.gmf.runtime.emf.core/src/org/eclipse/gmf/runtime/emf/core/edit/IDemuxedMListener.java,v retrieving revision 1.2 diff -u -r1.2 IDemuxedMListener.java --- src/org/eclipse/gmf/runtime/emf/core/edit/IDemuxedMListener.java 12 Sep 2005 21:25:12 -0000 1.2 +++ src/org/eclipse/gmf/runtime/emf/core/edit/IDemuxedMListener.java 25 Nov 2005 21:45:17 -0000 @@ -61,6 +61,8 @@ /** * Notifies me of an event indicating that a resource has been loaded. + * It is my responsibility to determine whether the resouce was loaded + * with errors. Also, a resource with errors may have no contents. * * @param notification the raw notification * @param resource the resource that was loaded Index: src/org/eclipse/gmf/runtime/emf/core/edit/MEditingDomain.java =================================================================== RCS file: /home/technology/org.eclipse.gmf/plugins/org.eclipse.gmf.runtime.emf.core/src/org/eclipse/gmf/runtime/emf/core/edit/MEditingDomain.java,v retrieving revision 1.3 diff -u -r1.3 MEditingDomain.java --- src/org/eclipse/gmf/runtime/emf/core/edit/MEditingDomain.java 14 Nov 2005 21:10:12 -0000 1.3 +++ src/org/eclipse/gmf/runtime/emf/core/edit/MEditingDomain.java 25 Nov 2005 21:45:17 -0000 @@ -150,27 +150,35 @@ public abstract URI convertURI(URI uri); /** - * Creates a resource and object of type rootEClass that becomes the root of - * the resource. + * Produces a resource for a given file name URI. If the + * resource is already loaded then an exception is thrown. The + * resource will be in the unloaded state when returned. * * @param fileNameURI * Optional file name to be assigned to the resource. - * @return The new resource. + * @return The resource object. + * + * @throws IllegalStateException if the resource is already loaded. + * */ public abstract Resource createResource(String fileNameURI); /** - * Creates a resource and object of type rootEClass that becomes the root of - * the resource. + * Produces a resource for a given URI and with the provided + * options. Unless specified with the appropriate option + * {@link MResourceOption#URI}, the URI is assumed to be a file path. * - * @param fileNameURI - * Optional file name to be assigned to the resource. + * @param uri + * Optional uri to be assigned to the resource. * @param options * The create options. This is a bit mask of values from * MResourceOption. - * @return The new resource. + * + * @return The resource object. + * + * @throws IllegalStateException if the resource is already loaded. */ - public abstract Resource createResource(String fileNameURI, int options); + public abstract Resource createResource(String uri, int options); /** * Creates a resource and object of type rootEClass that becomes the root of @@ -181,6 +189,7 @@ * @param rootEClass * The root object's type (optional). * @return The new resource. + * @throws IllegalStateException if the resource is already loaded. */ public abstract Resource createResource(String fileNameURI, EClass rootEClass); @@ -194,6 +203,7 @@ * @param rootEClass * The root object's type (optional) * @return The new resource. + * @throws IllegalStateException if the resource is already loaded. */ public abstract Resource createResource(String fileNameURI, EClass rootEClass, int options); @@ -204,6 +214,9 @@ * @param fileNameURI * The resource's file path. * @return The loaded resource. + * @deprecated Use {@link #createResource(String)} or + * {@link findResource(String)} followed by + * {@link #loadResource(Resource)} to get the same results. */ public abstract Resource loadResource(String fileNameURI); @@ -216,9 +229,12 @@ * The load options. This is a bit mask of values from * MResourceOption. * @return The loaded resource. + * @deprecated Use {@link #createResource(String, int)} or + * {@link #findResource(String, int)} followed by + * {@link #loadResource(Resource, int)} to get the same results. */ public abstract Resource loadResource(String fileNameURI, int options); - + /** * Loads a resource with a given file path from the given input stream using * the given load options. @@ -231,20 +247,30 @@ * @param inputStream * The input stream from which to load the resource's contents. * @return The loaded resource. + * @deprecated Use {@link #createResource(String, int)} or + * {@link #findResource(String, int)} followed by + * {@link #loadResource(Resource, int, InputStream)} to get the same + * results. */ public abstract Resource loadResource(String fileNameURI, int options, InputStream inputStream); /** - * Loads an unloaded resource. + * Loads an unloaded resource. It is the responsibility of callers to catch + * any exceptions that will be thrown as a result of the resource being loaded + * with errors. Note that the state of the resource could be loaded (ie. it could + * be necessary to unload it) but the {@link Resource#getErrors()} could be non-empty. * * @param resource * The resource to load. */ public abstract void loadResource(Resource resource); - + /** - * Loads an unloaded resource. + * Loads an unloaded resource. It is the responsibility of callers to catch + * any exceptions that will be thrown as a result of the resource being loaded + * with errors. Note that the state of the resource could be loaded (ie. it could + * be necessary to unload it) but the {@link Resource#getErrors()} could be non-empty. * * @param resource * The resource to load. @@ -256,7 +282,11 @@ /** * Loads an unloaded resource from the given input stream using the given - * load options. + * load options. It is the responsibility of callers to catch + * any exceptions that will be thrown as a result of the resource being loaded + * with errors. Note that the state of the resource could be loaded + * (ie. it could be necessary to unload it) but the {@link Resource#getErrors()} + * could be non-empty. * * @param resource * The resource to load. @@ -274,6 +304,7 @@ * * @param resource * The resource to unload. + * @deprecated Use {@link Resource#unload()} instead. */ public abstract void unloadResource(Resource resource); @@ -285,6 +316,7 @@ * @param options * The unload options. This is a bit mask of values from * MResourceOption. + * @deprecated Use {@link Resource#unload()} instead. */ public abstract void unloadResource(Resource resource, int options); Index: src/org/eclipse/gmf/runtime/emf/core/internal/domain/MSLEditingDomain.java =================================================================== RCS file: /home/technology/org.eclipse.gmf/plugins/org.eclipse.gmf.runtime.emf.core/src/org/eclipse/gmf/runtime/emf/core/internal/domain/MSLEditingDomain.java,v retrieving revision 1.6 diff -u -r1.6 MSLEditingDomain.java --- src/org/eclipse/gmf/runtime/emf/core/internal/domain/MSLEditingDomain.java 21 Nov 2005 20:19:20 -0000 1.6 +++ src/org/eclipse/gmf/runtime/emf/core/internal/domain/MSLEditingDomain.java 25 Nov 2005 21:45:18 -0000 @@ -464,6 +464,8 @@ int options) { URI uri = null; + + boolean wasCreated = false; if ((fileNameURI == null) || (fileNameURI.equals(MSLConstants.EMPTY_STRING))) @@ -491,8 +493,10 @@ if ((resource == null) && (!convertedURI.equals(uri))) resource = resourceSet.getResource(convertedURI, false); - if (resource == null) + if (resource == null) { resource = resourceSet.createResource(convertedURI); + wasCreated = true; + } else if (resource.isLoaded()) { @@ -524,7 +528,8 @@ MSLUtil.postProcessResource(resource); } - eventBroker.addEvent(resource, EventTypes.CREATE); + if (wasCreated) + eventBroker.addEvent(resource, EventTypes.CREATE); return resource; } @@ -583,9 +588,17 @@ resource = resourceSet.createResource(convertedURI); if (resource != null) { - - loadResource(resource, options, inputStream); - + + try { + loadResource(resource, options, inputStream); + } catch (RuntimeException e) { + // If an exception is thrown then we will try to automatically unload the resource. + if (resource.isLoaded()) { + resource.unload(); + } + throw e; + } + return resource; } } @@ -614,9 +627,7 @@ */ public void loadResource(Resource resource, int options, InputStream inputStream) { - try { - Map loadOptions = null; if (resource instanceof XMLResource) { @@ -654,11 +665,11 @@ resource.load(inputStream, loadOptions); } catch (Exception e) { - + // TODO In the next iteration, we will no longer be automatically unloading a resource if there are errors. resource.unload(); - + eventBroker.clearEvents(); - + RuntimeException newE = null; if (e instanceof MSLRuntimeException) @@ -692,9 +703,8 @@ resource.unload(); } catch (Exception e) { - eventBroker.clearEvents(); - + RuntimeException newE = null; if (e instanceof MSLRuntimeException) Index: src/org/eclipse/gmf/runtime/emf/core/internal/notifications/MSLResourceListener.java =================================================================== RCS file: /home/technology/org.eclipse.gmf/plugins/org.eclipse.gmf.runtime.emf.core/src/org/eclipse/gmf/runtime/emf/core/internal/notifications/MSLResourceListener.java,v retrieving revision 1.2 diff -u -r1.2 MSLResourceListener.java --- src/org/eclipse/gmf/runtime/emf/core/internal/notifications/MSLResourceListener.java 12 Sep 2005 21:25:11 -0000 1.2 +++ src/org/eclipse/gmf/runtime/emf/core/internal/notifications/MSLResourceListener.java 25 Nov 2005 21:45:18 -0000 @@ -68,6 +68,22 @@ private Map unloadedResourcesRoot = new HashMap(); + // TODO Remove this tracking of resources with errors in the next iteration. + /* + * This map is here to keep track of which resources were + * loaded with errors. For now we are not going to be propagating + * any events associated with loading/unloaded these resources. + * In the next iteration we will propagate all of these events + * and listeners will be required to verify whether the resource + * loaded with errors or not. + * + * Look at MEditingDomain.loadResource, MEditingDomain.unloadResource + * and IDemuxedMListener.handleResourceLoadedEvent for more details. + * + * cmcgee + */ + private Map resourcesWithErrors = new WeakHashMap(); + /** * Constructor. */ @@ -143,7 +159,25 @@ loadedResources.put(notifier, Boolean.TRUE); MSLUtil.postProcessResource(notifier); + + // TODO Remove this check for errors in the next iteration. + // If the resource loaded with errors, place it into a special + // map indicating that it was loaded with errors so that we + // do not propagate any automated unload events. This is going + // to change in the next iteration where we will propagate all + // resource-level events. + // + // cmcgee + // + if (notifier.getErrors().size() > 0) { + resourcesWithErrors.put(notifier, Boolean.TRUE); + } else { + resourcesWithErrors.remove(notifier); + // forward event to broker. + domain.getEventBroker().addEvent(notification); + } + return; } else if (!newBooleanValue && oldBooleanValue) { loadedResources.remove(notifier); @@ -156,11 +190,25 @@ EObject root = null; if ( unloadedResourcesRoot.containsKey(notifier)) { root = (EObject)unloadedResourcesRoot.get(notifier); - } + } UnloadNotification unloadNotification = new UnloadNotification(root, notification); unloadedResourcesRoot.remove(notifier); - domain.getEventBroker().addEvent(unloadNotification); + + // TODO Remove this check for resources with errors in the next iteration. + // We will be checking whether this resource was one that + // loaded with errors in it. If this is the case then we + // do not propagate the event. This is going to change in the + // next iteration. + // + // cmcgee + // + if (!resourcesWithErrors.containsKey(notifier)) { + domain.getEventBroker().addEvent(unloadNotification); + } else { + resourcesWithErrors.remove(notifier); + } + return; } } @@ -172,7 +220,7 @@ // we received this event domain.getCommandGenerator().generateCommand(notification); } - + // forward event to broker. domain.getEventBroker().addEvent(notification); } Index: src/org/eclipse/gmf/runtime/emf/core/util/ResourceUtil.java =================================================================== RCS file: /home/technology/org.eclipse.gmf/plugins/org.eclipse.gmf.runtime.emf.core/src/org/eclipse/gmf/runtime/emf/core/util/ResourceUtil.java,v retrieving revision 1.2 diff -u -r1.2 ResourceUtil.java --- src/org/eclipse/gmf/runtime/emf/core/util/ResourceUtil.java 12 Sep 2005 21:25:12 -0000 1.2 +++ src/org/eclipse/gmf/runtime/emf/core/util/ResourceUtil.java 25 Nov 2005 21:45:19 -0000 @@ -208,10 +208,32 @@ * The load options. This is a bit mask of values from * MResourceOption. * @return The loaded resource. + * @deprecated Use {@link #create(String, int)} or {@link #findResource(String, int)} + * followed by {@link #load(Resource, int)} to produce the same result. */ public static Resource load(String path, int options) { return MEditingDomain.INSTANCE.loadResource(path, options); } + + /** + * Produces a resource for a given file path and with the provided + * options. Unless specified with the appropriate option + * {@link MResourceOption#URI}, the path is assumed to be a file path. + * + * @param uri + * Optional uri to be assigned to the resource. + * @param options + * The create options. This is a bit mask of values from + * MResourceOption. + * + * @return The resource object. + * + * @throws IllegalStateException if the resource is already loaded. + */ + public static Resource create(String path, int options) { + return MEditingDomain.INSTANCE + .createResource(path, options); + } /** * Loads a resource with a given file path from the given input stream using @@ -225,6 +247,10 @@ * @param inputStream * The input stream from which to load the resource's contents. * @return The loaded resource. + * + * @deprecated Use {@link #create(String, int)} or (@link #findResource(String)} + * followed by {@link #load(Resource, int, InputStream)} to produce the same + * result. */ public static Resource load(String path, int options, InputStream inputStream) { @@ -232,7 +258,12 @@ } /** - * Loads an unloaded resource. + * Loads an unloaded resource. It is the client's responsibility + * to catch any exceptions that are thrown because of the loading + * of the resource. Also, once a resource is loaded, it could have + * errors (@link Resource#getErrors()}. The resource may be in the + * loaded state after this method is called. The client must decide + * whether to unload the resource in this case. * * @param resource * The resource to load. @@ -246,7 +277,12 @@ /** * Loads an unloaded resource from the given input stream using the given - * load options. + * load options. It is the client's responsibility + * to catch any exceptions that are thrown because of the loading + * of the resource. Also, once a resource is loaded, it could have + * errors (@link Resource#getErrors()}. The resource may be in the + * loaded state after this method is called. The client must decide + * whether to unload the resource in this case. * * @param resource * The resource to load. @@ -269,6 +305,7 @@ * @param options * The unload options. This is a bit mask of values from * MResourceOption. + * @deprecated Use {@link Resource#unload()} instead. */ public static void unload(Resource resource, int options) { MEditingDomain.INSTANCE.unloadResource(resource, options); @@ -316,6 +353,22 @@ public static Resource create(String path, EClass eClass) { return MEditingDomain.INSTANCE.createResource(path, eClass); } + + /** + * Produces a resource for a given file path. If the + * resource is already loaded then an exception is thrown. The + * resource will be in the unloaded state when returned. + * + * @param path + * Optional file name to be assigned to the resource. + * + * @return The resource object. + * + * @throws IllegalStateException if the resource is already loaded. + */ + public static Resource create(String path) { + return MEditingDomain.INSTANCE.createResource(path); + } /** * Loads a resource from a given file. @@ -323,13 +376,20 @@ * @param path * The resource's file path. * @return The loaded resource. + * @deprecated Use {@link #create(String)} or {@link #findResource(String)} + * followed by {@link #load(Resource)} for the same result. */ public static Resource load(String path) { return MEditingDomain.INSTANCE.loadResource(path); } /** - * Loads an unloaded resource. + * Loads an unloaded resource. It is the client's responsibility + * to catch any exceptions that are thrown because of the loading + * of the resource. Also, once a resource is loaded, it could have + * errors (@link Resource#getErrors()}. The resource may be in the + * loaded state after this method is called. The client must decide + * whether to unload the resource in this case. * * @param resource * The resource to load. @@ -343,6 +403,7 @@ * * @param resource * The resource to unload. + * @deprecated Use {@link Resource#unload()} instead. */ public static void unload(Resource resource) { MEditingDomain.INSTANCE.unloadResource(resource);