Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'sandbox/PapyrusEMFCompareV1/deprecated/org.eclipse.papyrus.emf.compare.transactional.editor/src/org/eclipse/papyrus/emf/compare/transactional/editor/sphinx')
-rw-r--r--sandbox/PapyrusEMFCompareV1/deprecated/org.eclipse.papyrus.emf.compare.transactional.editor/src/org/eclipse/papyrus/emf/compare/transactional/editor/sphinx/EcorePlatformUtil.java1982
-rw-r--r--sandbox/PapyrusEMFCompareV1/deprecated/org.eclipse.papyrus.emf.compare.transactional.editor/src/org/eclipse/papyrus/emf/compare/transactional/editor/sphinx/EcoreResourceUtil.java1148
-rw-r--r--sandbox/PapyrusEMFCompareV1/deprecated/org.eclipse.papyrus.emf.compare.transactional.editor/src/org/eclipse/papyrus/emf/compare/transactional/editor/sphinx/WorkspaceEditingDomainUtil.java321
-rw-r--r--sandbox/PapyrusEMFCompareV1/deprecated/org.eclipse.papyrus.emf.compare.transactional.editor/src/org/eclipse/papyrus/emf/compare/transactional/editor/sphinx/WorkspaceTransactionUtil.java198
4 files changed, 0 insertions, 3649 deletions
diff --git a/sandbox/PapyrusEMFCompareV1/deprecated/org.eclipse.papyrus.emf.compare.transactional.editor/src/org/eclipse/papyrus/emf/compare/transactional/editor/sphinx/EcorePlatformUtil.java b/sandbox/PapyrusEMFCompareV1/deprecated/org.eclipse.papyrus.emf.compare.transactional.editor/src/org/eclipse/papyrus/emf/compare/transactional/editor/sphinx/EcorePlatformUtil.java
deleted file mode 100644
index 67c1d923fb3..00000000000
--- a/sandbox/PapyrusEMFCompareV1/deprecated/org.eclipse.papyrus.emf.compare.transactional.editor/src/org/eclipse/papyrus/emf/compare/transactional/editor/sphinx/EcorePlatformUtil.java
+++ /dev/null
@@ -1,1982 +0,0 @@
-/**
- * <copyright>
- *
- * Copyright (c) 2008-2011 See4sys, itemis and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * See4sys - Initial API and implementation
- * itemis - [346715] IMetaModelDescriptor methods of MetaModelDescriptorRegistry taking EObject or Resource arguments should not start new EMF transactions
- *
- * </copyright>
- */
-package org.eclipse.papyrus.emf.compare.transactional.editor.sphinx;
-
-import java.io.IOException;
-import java.net.URL;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import org.eclipse.core.commands.ExecutionException;
-import org.eclipse.core.commands.operations.IOperationHistory;
-import org.eclipse.core.commands.operations.IUndoableOperation;
-import org.eclipse.core.resources.IContainer;
-import org.eclipse.core.resources.IFile;
-import org.eclipse.core.resources.IProject;
-import org.eclipse.core.resources.IWorkspace;
-import org.eclipse.core.resources.IWorkspaceRunnable;
-import org.eclipse.core.resources.ResourcesPlugin;
-import org.eclipse.core.runtime.Assert;
-import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.IAdaptable;
-import org.eclipse.core.runtime.IPath;
-import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.IStatus;
-import org.eclipse.core.runtime.OperationCanceledException;
-import org.eclipse.core.runtime.Path;
-import org.eclipse.core.runtime.Platform;
-import org.eclipse.core.runtime.Status;
-import org.eclipse.core.runtime.SubMonitor;
-import org.eclipse.core.runtime.jobs.ISchedulingRule;
-import org.eclipse.core.runtime.jobs.Job;
-import org.eclipse.core.runtime.jobs.MultiRule;
-import org.eclipse.emf.common.command.BasicCommandStack;
-import org.eclipse.emf.common.util.URI;
-import org.eclipse.emf.ecore.EObject;
-import org.eclipse.emf.ecore.resource.Resource;
-import org.eclipse.emf.ecore.resource.ResourceSet;
-import org.eclipse.emf.ecore.util.FeatureMap;
-import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
-import org.eclipse.emf.edit.provider.IWrapperItemProvider;
-import org.eclipse.emf.transaction.RunnableWithResult;
-import org.eclipse.emf.transaction.TransactionalEditingDomain;
-import org.eclipse.emf.transaction.util.TransactionUtil;
-import org.eclipse.emf.workspace.AbstractEMFOperation;
-import org.eclipse.emf.workspace.util.WorkspaceSynchronizer;
-import org.eclipse.osgi.util.NLS;
-import org.eclipse.sphinx.emf.Activator;
-import org.eclipse.sphinx.emf.edit.TransientItemProvider;
-import org.eclipse.sphinx.emf.internal.messages.Messages;
-import org.eclipse.sphinx.emf.metamodel.IMetaModelDescriptor;
-import org.eclipse.sphinx.emf.model.IModelDescriptor;
-import org.eclipse.sphinx.emf.model.ModelDescriptorRegistry;
-import org.eclipse.sphinx.emf.resource.ModelResourceDescriptor;
-import org.eclipse.sphinx.emf.saving.SaveIndicatorUtil;
-import org.eclipse.sphinx.emf.scoping.IResourceScope;
-import org.eclipse.sphinx.platform.util.ExtendedPlatform;
-import org.eclipse.sphinx.platform.util.PlatformLogUtil;
-import org.eclipse.sphinx.platform.util.ReflectUtil;
-import org.eclipse.sphinx.platform.util.StatusUtil;
-import org.xml.sax.SAXException;
-
-/**
- * Eclipse platform utility class.
- * <p>
- * Provides a set of methods allowing to handle resources, to load or save models, to retrieve a file from a resource
- * (and vice-versa), etc.
- */
-public final class EcorePlatformUtil {
-
- // Prevent from instantiation
- private EcorePlatformUtil() {
- }
-
- /**
- * Converts given {@link IPath path} into a workspace-relative platform resource {@link URI} if possible or an
- * absolute file {@link URI} otherwise. Returns given {@link IPath} as indeterminate (scheme-less) {@link URI} if
- * none of both is possible.
- *
- * @param path
- * The {@link IPath path} for which to create the {@link URI}; must not be <code>null</code>.
- * @return Workspace-relative platform resource {@link URI} or absolute file {@link URI} for given {@link IPath
- * path} or given {@link IPath} as indeterminate (scheme-less) {@link URI} otherwise.
- */
- public static URI createURI(IPath path) {
- Assert.isNotNull(path);
-
- URI uri = URI.createURI(path.toString(), true);
- URI convertedURI = EcoreResourceUtil.convertToPlatformResourceURI(uri);
- if (!convertedURI.isPlatformResource()) {
- convertedURI = EcoreResourceUtil.convertToAbsoluteFileURI(convertedURI);
- }
- return convertedURI;
- }
-
- public static IPath createPath(URI uri) {
- Assert.isNotNull(uri);
-
- if (uri.isPlatform()) {
- return new Path(uri.toPlatformString(true)).removeTrailingSeparator();
- } else if (uri.isFile()) {
- return new Path(uri.toFileString()).removeTrailingSeparator();
- } else {
- return new Path(uri.toString()).removeTrailingSeparator();
- }
- }
-
- /**
- * Converts the given {@link IPath path} into an absolute file {@linkplain URI}.
- *
- * @param path
- * The {@link Path path} for which an {@linkplain URI} is to be created.
- * @return The absolute file {@linkplain URI} for the given {@link IPath path} or an {@link URI} corresponding to
- * given {@link IPath path} as is if no conversion is possible.
- */
- public static URI createAbsoluteFileURI(IPath path) {
- URI uri = createURI(path);
- return EcoreResourceUtil.convertToAbsoluteFileURI(uri);
- }
-
- /**
- * Converts the given {@link URI uri} into an absolute file location, <em>i.e.</em> into a {@linkplain IPath path}.
- *
- * @param uri
- * The file {@linkplain URI} for which an absolute file {@linkplain IPath location} must be created.
- * @return The absolute file {@linkplain IPath location} for the given file {@link URI uri}.
- */
- public static IPath createAbsoluteFileLocation(URI uri) {
- uri = EcoreResourceUtil.convertToAbsoluteFileURI(uri);
- String uriString;
- if (uri.isFile()) {
- uriString = uri.toFileString();
- } else {
- uriString = uri.toString();
- }
- return new Path(uriString).removeTrailingSeparator();
- }
-
- /**
- * Converts the given {@link IPath path} into an absolute file {@linkplain IPath location}.
- *
- * @param path
- * The file path for which an absolute file {@linkplain IPath location} must be created.
- * @return The absolute file {@linkplain IPath location} for the given file {@link IPath path}.
- */
- public static IPath convertToAbsoluteFileLocation(IPath path) {
- URI uri = createAbsoluteFileURI(path);
- String uriString;
- if (uri.isFile()) {
- uriString = uri.toFileString();
- } else {
- uriString = uri.toString();
- }
- return new Path(uriString).removeTrailingSeparator();
- }
-
- /**
- * Reads the model namespace (i.e. XML namespace) of given {@link IFile file}. Returns a meaningful result only if
- * the {@link IFile file} is an XML document.
- *
- * @param file
- * The {@link IFile file} to investigate.
- * @return The model namespace denoted in the specified {@link IFile file} or <code>null</code> if the file is
- * either a non-XML file or an XML file which is not well-formed or has no model namespace.
- */
- public static String readModelNamespace(IFile file) {
- if (file != null && file.isAccessible()) {
- URI uri = createURI(file.getFullPath());
- return EcoreResourceUtil.readModelNamespace(null, uri);
- }
- return null;
- }
-
- /**
- * Reads the target namespace of given {@link IFile file}. Returns a meaningful result only if given {@link IFile
- * file} is an XML document.
- *
- * @param file
- * The {@link IFile file} to investigate.
- * @return The target namespace denoted in given {@link IFile file} or <code>null</code> if the {@link IFile file}
- * is either a not an XML file or an XML file which is not well-formed or has no target namespace.
- */
- public static String readTargetNamespace(IFile file) {
- return readTargetNamespace(file, (String[]) null);
- }
-
- /**
- * @param file
- * @param targetNamespaceExcludePatterns
- * @return
- */
- public static String readTargetNamespace(IFile file, String... targetNamespaceExcludePatterns) {
- if (file != null && file.isAccessible()) {
- URI uri = createURI(file.getFullPath());
- return EcoreResourceUtil.readTargetNamespace(null, uri, targetNamespaceExcludePatterns);
- }
- return null;
- }
-
- /**
- * Retrieves the XML comments located above the root element in given {@link IFile file}. Returns a meaningful
- * result only if given {@link IFile file} is an XML document.
- *
- * @param file
- * The {@link IFile file} to investigate.
- * @return Collection of strings representing the retrieved XML comments or empty collection if no such could be
- * found.
- */
- public static Collection<String> readRootElementComments(IFile file) {
- if (file != null && file.isAccessible()) {
- URI uri = createURI(file.getFullPath());
- return EcoreResourceUtil.readRootElementComments(null, uri);
- }
- return Collections.emptyList();
- }
-
- /**
- * Retrieves the {@link EObject root object} of the model contained in given {@link IFile file}. Returns
- * <code>null</code> if the {@link IFile file} has not been loaded yet.
- *
- * @param file
- * The {@link IFile file} containing the model.
- * @return The {@link EObject root object} of the model in given {@link IFile file} or <tt>null</tt> if the
- * {@link IFile file} has not been loaded yet or is empty.
- * @see #getModelRoot(TransactionalEditingDomain, IFile)
- */
- public static EObject getModelRoot(IFile file) {
- TransactionalEditingDomain editingDomain = WorkspaceEditingDomainUtil.getEditingDomain(file);
- if (editingDomain != null) {
- return getModelRoot(editingDomain, file);
- }
- return null;
- }
-
- /**
- * Retrieves the {@link EObject root object} of the model contained in given {@link IFile file} using given
- * {@link TransactionalEditingDomain editing domain}. Returns <tt>null</tt> if the {@link IFile file} has not been
- * loaded into the {@link TransactionalEditingDomain editing domain}'s {@link ResourceSet resource set} yet or is
- * empty.
- *
- * @param editingDomain
- * The {@linkplain TransactionalEditingDomain editing domain} the specified {@link IFile file} belongs
- * to; must not be <code>null</code>.
- * @param file
- * The {@linkplain IFile file} containing the model; must not be <code>null</code>.
- * @return The {@linkplain EObject root object} of the model in given {@link IFile file} or <tt>null</tt> if that
- * {@linkplain IFile file} has not been loaded into the specified {@linkplain TransactionalEditingDomain
- * editing domain}'s {@linkplain ResourceSet resource set} yet.
- */
- public static EObject getModelRoot(final TransactionalEditingDomain editingDomain, final IFile file) {
- if (editingDomain != null && file != null) {
- try {
- return TransactionUtil.runExclusive(editingDomain, new RunnableWithResult.Impl<EObject>() {
- public void run() {
- URI uri = createURI(file.getFullPath());
- setResult(EcoreResourceUtil.getModelRoot(editingDomain.getResourceSet(), uri));
- }
- });
- } catch (InterruptedException ex) {
- PlatformLogUtil.logAsError(Activator.getPlugin(), ex);
- }
- }
- return null;
- }
-
- /**
- * Loads the model contained in given {@link IFile file} using {@link EcoreResourceUtil#getDefaultLoadOptions()
- * default load options} and returns its {@link EObject root object}. Returns <tt>null</tt> if the {@link IFile
- * file} is empty.
- * <p>
- * Note: Calling this method involves retrieving the {@link TransactionalEditingDomain editing domain} behind the
- * given file. In case that the {@link IFile file} has not been loaded yet this is done by analyzing the its content
- * type. However, if it happens that the {@link IFile file}'s content type is retrieved for the very first time this
- * operation is somewhat costly in terms of runtime performance. Therefore, when the
- * {@link TransactionalEditingDomain editing domain} is already available prior to calling this method it is
- * recommended to use {@link #loadModelRoot(TransactionalEditingDomain, IFile)} instead.
- *
- * @param file
- * The {@link IFile file} containing the model.
- * @return The {@link EObject root object} of the model in given {@link IFile file} or <tt>null</tt> if the
- * {@link IFile file} is empty.
- * @see EcoreResourceUtil#getDefaultLoadOptions()
- * @see #loadModelRoot(TransactionalEditingDomain, IFile)
- */
- public static EObject loadModelRoot(IFile file) {
- return loadModelRoot(file, EcoreResourceUtil.getDefaultLoadOptions());
- }
-
- /**
- * Loads the model contained in given {@link IFile file} using given load options and returns its {@link EObject
- * root object}. Returns <tt>null</tt> if the {@link IFile file} is empty.
- * <p>
- * Note: Calling this method involves retrieving the {@link TransactionalEditingDomain editing domain} behind the
- * given file. In case that the {@link IFile file} has not been loaded yet this is done by analyzing the its content
- * type. However, if it happens that the {@link IFile file}'s content type is retrieved for the very first time this
- * operation is somewhat costly in terms of runtime performance. Therefore, when the
- * {@link TransactionalEditingDomain editing domain} is already available prior to calling this method it is
- * recommended to use {@link #loadModelRoot(TransactionalEditingDomain, IFile, Map)} instead.
- *
- * @param file
- * The {@link IFile file} containing the model.
- * @param options
- * The options to be used for loading the model.
- * @return The {@link EObject root object} of the model in given {@link IFile file} or <tt>null</tt> if the
- * {@link IFile file} is empty.
- * @see #loadModelRoot(TransactionalEditingDomain, IFile, Map)
- */
- public static EObject loadModelRoot(IFile file, Map<?, ?> options) {
- TransactionalEditingDomain editingDomain = WorkspaceEditingDomainUtil.getEditingDomain(file);
- if (editingDomain != null) {
- return loadModelRoot(editingDomain, file, options);
- }
- return null;
- }
-
- /**
- * Loads the model contained in given {@link IFile file} into given {@link TransactionalEditingDomain editing
- * domain} using {@link EcoreResourceUtil#getDefaultLoadOptions() default load options} and returns its
- * {@link EObject root object}. Returns <tt>null</tt> if the {@link IFile file} is empty.
- *
- * @param file
- * The {@link IFile file} containing the model.
- * @param editingDomain
- * The {@link TransactionalEditingDomain editing domain} the {@link IFile file} belongs to.
- * @param options
- * The options to be used for loading the model.
- * @return The {@link EObject root object} of the model in given {@link IFile file} or <tt>null</tt> if the
- * {@link IFile file} is empty.
- * @see EcoreResourceUtil#getDefaultLoadOptions()
- */
- public static EObject loadModelRoot(TransactionalEditingDomain editingDomain, IFile file) {
- return loadModelRoot(editingDomain, file, EcoreResourceUtil.getDefaultLoadOptions());
- }
-
- /**
- * Loads the model contained in given {@link IFile file} into given {@link TransactionalEditingDomain editing
- * domain} using given load options and returns its {@link EObject root object}. Returns <tt>null</tt> if the
- * {@link IFile file} is empty.
- *
- * @param file
- * The {@link IFile file} containing the model; must not be <code>null</code>.
- * @param editingDomain
- * The {@link TransactionalEditingDomain editing domain} the {@link IFile file} belongs to; must not be
- * <code>null</code>.
- * @param options
- * The options to be used for loading the model.
- * @return The {@link EObject root object} of the model in given {@link IFile file} or <tt>null</tt> if the
- * {@link IFile file} is empty.
- */
- public static EObject loadModelRoot(final TransactionalEditingDomain editingDomain, final IFile file, final Map<?, ?> options) {
- if (editingDomain != null && file != null) {
- try {
- return TransactionUtil.runExclusive(editingDomain, new RunnableWithResult.Impl<EObject>() {
- public void run() {
- URI uri = createURI(file.getFullPath());
- setResult(EcoreResourceUtil.loadModelRoot(editingDomain.getResourceSet(), uri, options));
- }
- });
- } catch (InterruptedException ex) {
- PlatformLogUtil.logAsError(Activator.getPlugin(), ex);
- }
- }
- return null;
- }
-
- /**
- * Tests if the given {@link Resource resource} is loaded in the {@link ResourceSet resource set} of given
- * {@link TransactionalEditingDomain editingDomain}.
- *
- * @param editingDomain
- * The {@link TransactionalEditingDomain editing domain} with the {@link ResourceSet resource set} to be
- * investigated.
- * @param resource
- * The {@link Resource resource} that may or not be loaded.
- * @return <code>true</code> if specified {@link Resource resource} is loaded in {@link ResourceSet resource set} of
- * given {@link TransactionalEditingDomain editingDomain}; <code>false</code> otherwise.
- */
- public static boolean isResourceLoaded(final TransactionalEditingDomain editingDomain, final Resource resource) {
- if (editingDomain != null && resource != null) {
- try {
- return TransactionUtil.runExclusive(editingDomain, new RunnableWithResult.Impl<Boolean>() {
- public void run() {
- setResult(EcoreResourceUtil.isResourceLoaded(editingDomain.getResourceSet(), resource.getURI()));
- }
- });
- } catch (InterruptedException ex) {
- PlatformLogUtil.logAsError(Activator.getPlugin(), ex);
- }
- }
- return false;
- }
-
- /**
- * Tests if the given {@link IFile file} is loaded in the {@link ResourceSet resource set} of some
- * {@link TransactionalEditingDomain editingDomain}.
- *
- * @param file
- * The {@link IFile file} that may or not be loaded.
- * @return <code>true</code> if specified {@link IFile file} is loaded in {@link ResourceSet resource set} of some
- * {@link TransactionalEditingDomain editingDomain}; <code>false</code> otherwise.
- */
- public static boolean isFileLoaded(IFile file) {
- return WorkspaceEditingDomainUtil.getCurrentEditingDomain(file) != null;
- }
-
- /**
- * Tests if the given {@link IFile file} is loaded in the {@link ResourceSet resource set} of the given
- * {@link TransactionalEditingDomain editingDomain}.
- *
- * @param editingDomain
- * The {@link TransactionalEditingDomain editing domain} with the {@link ResourceSet resource set} to be
- * investigated.
- * @param file
- * The {@link IFile file} that may or not be loaded.
- * @return <code>true</code> if specified {@link IFile file} is loaded in {@link ResourceSet resource set} of given
- * {@link TransactionalEditingDomain editingDomain}; <code>false</code> otherwise.
- */
- public static boolean isFileLoaded(final TransactionalEditingDomain editingDomain, final IFile file) {
- if (editingDomain != null && file != null) {
- try {
- return TransactionUtil.runExclusive(editingDomain, new RunnableWithResult.Impl<Boolean>() {
- public void run() {
- URI uri = createURI(file.getFullPath());
- setResult(EcoreResourceUtil.isResourceLoaded(editingDomain.getResourceSet(), uri));
- }
- });
- } catch (InterruptedException ex) {
- PlatformLogUtil.logAsError(Activator.getPlugin(), ex);
- }
- }
- return false;
- }
-
- /**
- * Returns the {@linkplain IFile file} corresponding to the specified {@linkplain Object object}.
- * <p>
- * The supported object types are:
- * <ul>
- * <li>{@linkplain org.eclipse.core.resources.IFile}</li>
- * <li>{@linkplain org.eclipse.emf.common.util.URI}</li>
- * <li>{@linkplain org.eclipse.emf.ecore.resource.Resource}</li>
- * <li>{@linkplain org.eclipse.emf.ecore.EObject}</li>
- * <li>{@linkplain org.eclipse.emf.edit.provider.IWrapperItemProvider}</li>
- * <li>{@linkplain org.eclipse.emf.ecore.util.FeatureMap.Entry}</li>
- * </ul>
- *
- * @param object
- * The object from which a file must be returned.
- * @return The underlying file from the given object.
- */
- public static IFile getFile(Object object) {
- if (object instanceof IFile) {
- return (IFile) object;
- } else if (object instanceof URI) {
- return getFile((URI) object);
- } else if (object instanceof Resource) {
- return getFile((Resource) object);
- } else if (object instanceof EObject) {
- return getFile((EObject) object);
- } else if (object instanceof IWrapperItemProvider) {
- return getFile((IWrapperItemProvider) object);
- } else if (object instanceof FeatureMap.Entry) {
- return getFile((FeatureMap.Entry) object);
- } else if (object instanceof TransientItemProvider) {
- return getFile((TransientItemProvider) object);
- }
- return null;
- }
-
- /**
- * Retrieves the {@linkplain IFile file} corresponding to the given {@linkplain org.eclipse.emf.common.util.URI}.
- *
- * @param uri
- * The {@linkplain URI} of the file to return.
- * @return The file corresponding to the specified {@link URI uri}.
- */
- public static IFile getFile(URI uri) {
- if (uri != null && Platform.isRunning()) {
- try {
- return (IFile) ReflectUtil.invokeInvisibleMethod(WorkspaceSynchronizer.class, "getFile", new Object[] { uri, //$NON-NLS-1$
- EcoreResourceUtil.getURIConverter(), false });
- } catch (Exception ex) {
- // Ignore exception
- }
- }
- return null;
- }
-
- /**
- * Retrieves the {@link IFile file} corresponding to the given <code>resource</code>.
- *
- * @param resource
- * The {@link Resource resource} for which the file is to be returned.
- * @return The file corresponding to the specified <code>resource</code>.
- */
- public static IFile getFile(final Resource resource) {
- if (resource != null && Platform.isRunning()) {
- return WorkspaceSynchronizer.getFile(resource);
- }
- return null;
- }
-
- /**
- * Retrieves the {@linkplain IFile file} owning to the given {@linkplain org.eclipse.emf.ecore.EObject}.
- *
- * @param eObject
- * The {@linkplain EObject} whose file must be returned.
- * @return The file containing the specified {@link EObject eObject}.
- */
- public static IFile getFile(final EObject eObject) {
- if (eObject != null) {
- return getFile(eObject.eResource());
- }
- return null;
- }
-
- /**
- * Retrieves the {@linkplain IFile file} owning the given
- * {@linkplain org.eclipse.emf.edit.provider.IWrapperItemProvider}.
- * <p>
- * First retrieves the owner of the {@link IWrapperItemProvider provider}; then, if owner is an {@linkplain EObject}
- * delegates to {@linkplain #getFile(EObject)} else delegates to {@linkplain #getFile(Object)}.
- *
- * @param provider
- * The {@linkplain IWrapperItemProvider} whose file must be returned.
- * @return The file containing the specified {@link IWrapperItemProvider provider}; <code>null</code> if that
- * provider is <code>null</code>.
- */
- public static IFile getFile(final IWrapperItemProvider provider) {
- if (provider != null) {
- Object owner = provider.getOwner();
- if (owner instanceof EObject) {
- return getFile((EObject) owner);
- } else {
- Object unwrapped = AdapterFactoryEditingDomain.unwrap(provider);
- return getFile(unwrapped);
- }
- }
- return null;
- }
-
- /**
- * Retrieves the {@linkplain IFile file} matching the given {@link FeatureMap.Entry entry}.
- * <p>
- * First unwraps the {@link FeatureMap.Entry entry}; then, delegates to {@linkplain #getFile(Object)}.
- *
- * @param entry
- * The {@linkplain FeatureMap.Entry} whose underlying file must be returned.
- * @return The file under the specified {@link FeatureMap.Entry entry}.
- */
- public static IFile getFile(FeatureMap.Entry entry) {
- Object unwrapped = AdapterFactoryEditingDomain.unwrap(entry);
- return getFile(unwrapped);
- }
-
- /**
- * Retrieves the {@linkplain IFile file} owning the given {@linkplain TransientItemProvider}.
- * <p>
- * First retrieves the owner of the {@link TransientItemProvider provider}; then, if owner is an
- * {@linkplain EObject} delegates to {@linkplain #getFile(EObject)} else delegates to {@linkplain #getFile(Object)}.
- *
- * @param provider
- * The {@linkplain TransientItemProvider} whose file must be returned.
- * @return The file containing the specified {@link TransientItemProvider provider}; <code>null</code> if that
- * provider is <code>null</code>.
- */
- public static IFile getFile(final TransientItemProvider provider) {
- if (provider != null) {
- Object target = provider.getTarget();
- if (target instanceof EObject) {
- return getFile((EObject) target);
- }
- }
- return null;
- }
-
- /**
- * Returns the {@linkplain Resource resource} corresponding to the specified {@linkplain Object object}.
- * <p>
- * The supported object types are:
- * <ul>
- * <li>{@linkplain org.eclipse.core.resources.IFile}</li>
- * <li>{@linkplain org.eclipse.emf.common.util.URI}</li>
- * <li>{@linkplain org.eclipse.emf.ecore.resource.Resource}</li>
- * <li>{@linkplain org.eclipse.emf.ecore.EObject}</li>
- * <li>{@linkplain org.eclipse.emf.ecore.util.FeatureMap.Entry}</li>
- * <li>{@linkplain org.eclipse.emf.edit.provider.IWrapperItemProvider}</li>
- * </ul>
- * <p>
- * If the type of the specified object does not belongs to that list of supported types, <code>null</code> is
- * returned.
- *
- * @param object
- * The object from which a resource must be returned.
- * @return The underlying resource from the given object.
- */
- public static Resource getResource(Object object) {
- if (object instanceof IFile) {
- return getResource((IFile) object);
- } else if (object instanceof URI) {
- return getResource((URI) object);
- } else if (object instanceof Resource) {
- return (Resource) object;
- } else if (object instanceof EObject) {
- return getResource((EObject) object);
- } else if (object instanceof IWrapperItemProvider) {
- return getResource((IWrapperItemProvider) object);
- } else if (object instanceof FeatureMap.Entry) {
- return getResource((FeatureMap.Entry) object);
- } else if (object instanceof TransientItemProvider) {
- return getResource((TransientItemProvider) object);
- }
- return null;
- }
-
- /**
- * Retrieves the {@linkplain Resource resource} corresponding to the given {@link IFile file}.
- *
- * @param file
- * The {@linkplain IFile file} whose {@link Resource resource} is to be returned.
- * @return The resource corresponding to the specified {@link IFile file}.
- */
- public static Resource getResource(final IFile file) {
- final TransactionalEditingDomain editingDomain = WorkspaceEditingDomainUtil.getCurrentEditingDomain(file);
- if (editingDomain != null) {
- try {
- return TransactionUtil.runExclusive(editingDomain, new RunnableWithResult.Impl<Resource>() {
- public void run() {
- URI uri = createURI(file.getFullPath());
- setResult(editingDomain.getResourceSet().getResource(uri, false));
- }
- });
- } catch (InterruptedException ex) {
- PlatformLogUtil.logAsError(Activator.getPlugin(), ex);
- }
- }
- return null;
- }
-
- /**
- * Returns the {@linkplain Resource resource} corresponding to the specified {@link URI uri}.
- *
- * @param uri
- * The {@linkplain URI} of the resource to return.
- * @return The resource corresponding to the specified {@link URI uri}.
- */
- public static Resource getResource(final URI uri) {
- IFile file = getFile(uri);
- final TransactionalEditingDomain editingDomain = WorkspaceEditingDomainUtil.getCurrentEditingDomain(file);
- if (editingDomain != null) {
- try {
- return TransactionUtil.runExclusive(editingDomain, new RunnableWithResult.Impl<Resource>() {
- public void run() {
- setResult(editingDomain.getResourceSet().getResource(uri.trimFragment(), false));
- }
- });
- } catch (InterruptedException ex) {
- PlatformLogUtil.logAsError(Activator.getPlugin(), ex);
- }
- }
- return null;
- }
-
- /**
- * Retrieves the {@linkplain Resource resource} corresponding to the given {@link EObject object}.
- *
- * @param eObject
- * The {@linkplain EObject object} whose {@link Resource resource} is to be returned.
- * @return The resource corresponding to the specified {@link EObject object}.
- */
- public static Resource getResource(final EObject eObject) {
- return EcoreResourceUtil.getResource(eObject);
- }
-
- /**
- * Retrieves the {@linkplain Resource resource} owning the given {@link IWrapperItemProvider provider}.
- * <p>
- * First retrieves the owner of the {@link IWrapperItemProvider provider}; then, if owner is an {@linkplain EObject}
- * returns its resource, else delegates to {@linkplain #getResource(Object)}.
- *
- * @param provider
- * The {@linkplain IWrapperItemProvider} whose resource must be returned.
- * @return The resource containing the specified {@link IWrapperItemProvider provider}; <code>null</code> if that
- * provider is <code>null</code>.
- */
- public static Resource getResource(final IWrapperItemProvider provider) {
- return EcoreResourceUtil.getResource(provider);
- }
-
- /**
- * Retrieves the {@linkplain Resource resource} matching the given {@link FeatureMap.Entry entry}.
- * <p>
- * First unwraps the {@link FeatureMap.Entry entry}; then, delegates to {@linkplain #getResource(Object)}.
- *
- * @param entry
- * The {@linkplain FeatureMap.Entry} whose underlying resource must be returned.
- * @return The resource under the specified {@link FeatureMap.Entry entry}.
- */
- public static Resource getResource(FeatureMap.Entry entry) {
- return EcoreResourceUtil.getResource(entry);
- }
-
- /**
- * Retrieves the {@linkplain Resource resource} owning the given {@link TransientItemProvider provider}.
- * <p>
- * First retrieves the owner of the {@link TransientItemProvider provider}; then, if owner is an
- * {@linkplain EObject} returns its resource.
- *
- * @param provider
- * The {@linkplain TransientItemProvider} whose resource must be returned.
- * @return The resource containing the specified {@link TransientItemProvider provider}; <code>null</code> if that
- * provider is <code>null</code>.
- */
- public static Resource getResource(final TransientItemProvider provider) {
- return EcoreResourceUtil.getResource(provider);
- }
-
- /**
- * Returns all resources owned by the {@link IModelDescriptor model descriptor} of the contextObject.
- *
- * @param contextObject
- * The Object used to research resources in the model.
- * @param includeReferencedModels
- * Determines if the {@link IModelDescriptor model descriptors} referenced by the context object's
- * {@link IModelDescriptor model descriptor} must be considered for the research.
- * @return The resources in the context object's model.
- * @since 0.7.0
- */
- public static Collection<Resource> getResourcesInModel(Object contextObject, boolean includeReferencedModels) {
- if (contextObject instanceof IModelDescriptor) {
- return getResourcesInModel((IModelDescriptor) contextObject, includeReferencedModels);
- } else if (contextObject instanceof IFile) {
- return getResourcesInModel((IFile) contextObject, includeReferencedModels);
- } else if (contextObject instanceof URI) {
- return getResourcesInModel((URI) contextObject, includeReferencedModels);
- } else if (contextObject instanceof Resource) {
- return getResourcesInModel((Resource) contextObject, includeReferencedModels);
- } else if (contextObject instanceof EObject) {
- return getResourcesInModel((EObject) contextObject, includeReferencedModels);
- } else if (contextObject instanceof IWrapperItemProvider) {
- return getResourcesInModel((IWrapperItemProvider) contextObject, includeReferencedModels);
- } else if (contextObject instanceof FeatureMap.Entry) {
- return getResourcesInModel((FeatureMap.Entry) contextObject, includeReferencedModels);
- } else if (contextObject instanceof TransientItemProvider) {
- return getResourcesInModel((TransientItemProvider) contextObject, includeReferencedModels);
- }
- return Collections.emptyList();
- }
-
- /**
- * Returns all resources owned by the {@link IModelDescriptor model descriptor} provided in argument .
- *
- * @param modelDescriptor
- * The {@link IModelDescriptor model descriptor} used as context object for investigation.
- * @param includeReferencedModels
- * Determines if the {@link IModelDescriptor model descriptors} referenced by the context
- * {@link IModelDescriptor model descriptor} must be considered for the research.
- * @return The {@link Resource resource}s owned by the {@link IModelDescriptor model descriptor}.
- * @since 0.7.0
- */
- public static Collection<Resource> getResourcesInModel(IModelDescriptor modelDescriptor, boolean includeReferencedModels) {
- if (modelDescriptor != null) {
- return modelDescriptor.getLoadedResources(includeReferencedModels);
- }
- return Collections.emptySet();
- }
-
- /**
- * Returns all resources owned by the {@link IModelDescriptor model descriptor} of the context {@link IFile file}.
- * If the given {@link IFile file} doesn't belong to any {@link IModelDescriptor model descriptor}, resources in
- * context{@link ResourceSet} will be returned
- *
- * @param contextFile
- * The {@link IFile file} used as context object for investigation.
- * @param includeReferencedModels
- * Determines if the {@link IModelDescriptor model descriptors} referenced by the context {@link IFile
- * file}'s {@link IModelDescriptor model descriptor} must be considered for the research.
- * @return The resources in the context {@link IFile file}'s model.
- * @since 0.7.0
- */
- public static Collection<Resource> getResourcesInModel(IFile contextFile, boolean includeReferencedModels) {
- IModelDescriptor modelDescriptor = ModelDescriptorRegistry.INSTANCE.getModel(contextFile);
- if (modelDescriptor != null) {
- return modelDescriptor.getLoadedResources(includeReferencedModels);
- }
- return getResourcesInContext(contextFile);
- }
-
- /**
- * Returns all resources owned by the {@link IModelDescriptor model descriptor} of the context {@link URI uri}.
- *
- * @param contextURI
- * The {@link URI uri} used as context object for investigation.
- * @param includeReferencedModels
- * Determines if the {@link IModelDescriptor model descriptors} referenced by the context {@link URI uri}
- * 's {@link IModelDescriptor model descriptor} must be considered for the research.
- * @return The resources in the context {@link URI uri}'s model.
- * @since 0.7.0
- */
- public static Collection<Resource> getResourcesInModel(URI contextURI, boolean includeReferencedModels) {
- Resource contextResource = getResource(contextURI);
- return getResourcesInModel(contextResource, includeReferencedModels);
- }
-
- /**
- * Returns all resources owned by the {@link IModelDescriptor model descriptor} of the context {@link Resource
- * resource}.If the given {@link Resource contextResource} doesn't belong to any {@link IModelDescriptor model
- * descriptor}, resources in context{@link ResourceSet} will be returned
- *
- * @param contextResource
- * The {@link Resource resource} used as context object for investigation.
- * @param includeReferencedModels
- * Determines if the {@link IModelDescriptor model descriptors} referenced by the context
- * {@link Resource resource} 's {@link IModelDescriptor model descriptor} must be considered for the
- * research.
- * @return The resources in the context {@link Resource resource}'s model.
- * @since 0.7.0
- */
- public static Collection<Resource> getResourcesInModel(Resource contextResource, boolean includeReferencedModels) {
- IModelDescriptor modelDescriptor = ModelDescriptorRegistry.INSTANCE.getModel(contextResource);
- if (modelDescriptor != null) {
- return modelDescriptor.getLoadedResources(includeReferencedModels);
- }
- return getResourcesInContext(contextResource);
- }
-
- /**
- * Returns all resources owned by the {@link IModelDescriptor model descriptor} of the context {@link EObject
- * eobject}.
- *
- * @param contextEObject
- * The {@link EObject eobject} used as context object for investigation.
- * @param includeReferencedModels
- * Determines if the {@link IModelDescriptor model descriptors} referenced by the context {@link EObject
- * eobject}'s {@link IModelDescriptor model descriptor} must be considered for the research.
- * @return The resources in the context {@link EObject eobject}'s model.
- * @since 0.7.0
- */
- public static Collection<Resource> getResourcesInModel(EObject contextEObject, boolean includeReferencedModels) {
- Resource contextResource = contextEObject.eResource();
- return getResourcesInModel(contextResource, includeReferencedModels);
- }
-
- /**
- * Returns all resources owned by the {@link IModelDescriptor model descriptor} of the context
- * {@link IWrapperItemProvider provider}.
- *
- * @param contextProvider
- * The {@link IWrapperItemProvider provider} used as context object for investigation.
- * @param includeReferencedModels
- * Determines if the {@link IModelDescriptor model descriptors} referenced by the context
- * {@link IWrapperItemProvider provider}'s {@link IModelDescriptor model descriptor} must be considered
- * for the research.
- * @return The resources in the context {@link IWrapperItemProvider provider}'s model.
- * @since 0.7.0
- */
- public static Collection<Resource> getResourcesInModel(IWrapperItemProvider contextProvider, boolean includeReferencedModels) {
- Resource contextResource = getResource(contextProvider);
- return getResourcesInModel(contextResource, includeReferencedModels);
- }
-
- /**
- * Returns all resources owned by the {@link IModelDescriptor model descriptor} of the context
- * {@link FeatureMap.Entry feature map entry}.
- *
- * @param contextEntry
- * The {@link FeatureMap.Entry feature map entry} used as context object for investigation.
- * @param includeReferencedModels
- * Determines if the {@link IModelDescriptor model descriptors} referenced by the context
- * {@link FeatureMap.Entry feature map entry}'s {@link IModelDescriptor model descriptor} must be
- * considered for the research.
- * @return The resources in the context {@link FeatureMap.Entry feature map entry}'s model.
- * @since 0.7.0
- */
- public static Collection<Resource> getResourcesInModel(FeatureMap.Entry contextEntry, boolean includeReferencedModels) {
- Resource contextResource = getResource(contextEntry);
- return getResourcesInModel(contextResource, includeReferencedModels);
- }
-
- /**
- * Returns all resources owned by the {@link IModelDescriptor model descriptor} of the context
- * {@link TransientItemProvider provider}.
- *
- * @param contextProvider
- * The {@link TransientItemProvider provider} used as context object for investigation.
- * @param includeReferencedModels
- * Determines if the {@link IModelDescriptor model descriptors} referenced by the context
- * {@link TransientItemProvider provider}'s {@link IModelDescriptor model descriptor} must be considered
- * for the research.
- * @return The resources in the context {@link TransientItemProvider provider}'s model.
- * @since 2.1
- */
- public static Collection<Resource> getResourcesInModel(TransientItemProvider contextProvider, boolean includeReferencedModels) {
- Resource contextResource = getResource(contextProvider);
- return getResourcesInModel(contextResource, includeReferencedModels);
- }
-
- /**
- * Returns all resources owned by the {@link IModelDescriptor model}s of given {@link IMetaModelDescriptor
- * metamodel} in the context {@link IContainer container}.
- *
- * @param contextContainer
- * The {@link IContainer container} used as context object for investigation.
- * @param includeReferencedModels
- * Determines if the {@link IModelDescriptor model descriptors} referenced by the context
- * {@link IContainer container}'s {@link IModelDescriptor model descriptor} must be considered for the
- * research.
- * @return The resources in the context {@link IContainer container}'s models.
- * @since 0.7.0
- */
- public static Collection<Resource> getResourcesInModels(IContainer contextContainer, IMetaModelDescriptor mmDescriptor,
- boolean includeReferencedModels) {
- Collection<Resource> resourcesInModels = new HashSet<Resource>();
- Collection<IModelDescriptor> modelDescriptors = ModelDescriptorRegistry.INSTANCE.getModels(contextContainer, mmDescriptor);
- for (IModelDescriptor modelDescriptor : modelDescriptors) {
- resourcesInModels.addAll(modelDescriptor.getLoadedResources(includeReferencedModels));
- }
- return resourcesInModels;
- }
-
- /**
- * Returns all resources owned by the {@link IResourceScope model resource scope} of the contextObject.
- *
- * @param contextObject
- * The Object used to research resources in the model.
- * @param includeReferencedScopes
- * Determines if the {@link IResourceScope model resource scope} referenced by the context object's
- * {@link IResourceScope model resource scope} must be considered for the research.
- * @return The resources in the context object's {@link IResourceScope model resource scope}.
- * @since 0.7.0
- */
- public static Collection<Resource> getResourcesInScope(Object contextObject, boolean includeReferencedScopes) {
- if (contextObject instanceof IModelDescriptor) {
- return getResourcesInScope((IModelDescriptor) contextObject, includeReferencedScopes);
- } else if (contextObject instanceof IFile) {
- return getResourcesInScope((IFile) contextObject, includeReferencedScopes);
- } else if (contextObject instanceof URI) {
- return getResourcesInScope((URI) contextObject, includeReferencedScopes);
- } else if (contextObject instanceof Resource) {
- return getResourcesInScope((Resource) contextObject, includeReferencedScopes);
- } else if (contextObject instanceof EObject) {
- return getResourcesInScope((EObject) contextObject, includeReferencedScopes);
- } else if (contextObject instanceof IWrapperItemProvider) {
- return getResourcesInScope((IWrapperItemProvider) contextObject, includeReferencedScopes);
- } else if (contextObject instanceof FeatureMap.Entry) {
- return getResourcesInScope((FeatureMap.Entry) contextObject, includeReferencedScopes);
- } else if (contextObject instanceof TransientItemProvider) {
- return getResourcesInScope((TransientItemProvider) contextObject, includeReferencedScopes);
- }
- return Collections.emptyList();
- }
-
- /**
- * Returns all resources owned by the {@link IResourceScope model resource scope} of the {@link IModelDescriptor
- * model descriptor} provided in argument .
- *
- * @param modelDescriptor
- * The {@link IModelDescriptor model descriptor} used as context object for investigation.
- * @param includeReferencedModels
- * Determines if the {@link IResourceScope model resource scopes} referenced by the context
- * {@link IResourceScope model resource scope} must be considered for the research.
- * @return The {@link Resource resource}s owned by the {@link IResourceScope model resource scope}.
- * @since 0.7.0
- */
- public static Collection<Resource> getResourcesInScope(IModelDescriptor modelDescriptor, boolean includeReferencedScopes) {
- if (modelDescriptor != null) {
- TransactionalEditingDomain editingDomain = modelDescriptor.getEditingDomain();
- return modelDescriptor.getScope().getLoadedResources(editingDomain, includeReferencedScopes);
- }
- return Collections.emptySet();
- }
-
- /**
- * Returns all resources owned by the {@link IResourceScope model resource scope} of the {@link IModelDescriptor
- * model descriptor} of the context {@link IFile file}. If the given {@link IFile file} does not belong to any
- * {@link IModelDescriptor model descriptor}, resources in context {@link ResourceSet} will be returned.
- *
- * @param contextFile
- * The {@link IFile file} used as context object for investigation.
- * @param includeReferencedModels
- * Determines if the {@link IResourceScope model resource scopes} referenced by the context {@link IFile
- * file}'s {@link IResourceScope model resource scope} must be considered for the research.
- * @return The resources in the context {@link IFile file}'s {@link IResourceScope model resource scope}.
- * @since 0.7.0
- */
- public static Collection<Resource> getResourcesInScope(IFile contextFile, boolean includeReferencedScopes) {
- IModelDescriptor modelDescriptor = ModelDescriptorRegistry.INSTANCE.getModel(contextFile);
- if (modelDescriptor != null) {
- return getResourcesInScope(modelDescriptor, includeReferencedScopes);
- }
- return getResourcesInContext(contextFile);
- }
-
- /**
- * Returns all resources owned by the {@link IResourceScope model resource scope} of the {@link IModelDescriptor
- * model descriptor} of the context {@link URI uri}.
- *
- * @param contextURI
- * The {@link URI uri} used as context object for investigation.
- * @param includeReferencedModels
- * Determines if the {@link IResourceScope model resource scopes} referenced by the context {@link URI
- * uri}'s {@link IResourceScope model resource scope} must be considered for the research.
- * @return The resources in the context {@link URI uri}'s {@link IResourceScope model resource scope}.
- * @since 0.7.0
- */
- public static Collection<Resource> getResourcesInScope(URI contextURI, boolean includeReferencedScopes) {
- Resource contextResource = getResource(contextURI);
- return getResourcesInScope(contextResource, includeReferencedScopes);
- }
-
- /**
- * Returns all resources owned by the {@link IResourceScope model resource scope} of the {@link IModelDescriptor
- * model descriptor} of the context {@link Resource resource}.If the given {@link Resource contextResource} does not
- * belong to any {@link IModelDescriptor model descriptor}, resources in context {@link ResourceSet} will be
- * returned.
- *
- * @param contextResource
- * The {@link Resource resource} used as context object for investigation.
- * @param includeReferencedModels
- * Determines if the {@link IResourceScope model resource scopes} referenced by the context
- * {@link Resource resource}'s {@link IResourceScope model resource scope} must be considered for the
- * research.
- * @return The resources in the context {@link Resource resource}'s {@link IResourceScope model resource scope}.
- * @since 0.7.0
- */
- public static Collection<Resource> getResourcesInScope(Resource contextResource, boolean includeReferencedScopes) {
- IModelDescriptor modelDescriptor = ModelDescriptorRegistry.INSTANCE.getModel(contextResource);
- if (modelDescriptor != null) {
- return getResourcesInScope(modelDescriptor, includeReferencedScopes);
- }
- return getResourcesInContext(contextResource);
- }
-
- /**
- * Returns all resources owned by the {@link IResourceScope model resource scope} of the {@link IModelDescriptor
- * model descriptor} of the context {@link EObject eObject}.
- *
- * @param contextEObject
- * The {@link EObject eObject} used as context object for investigation.
- * @param includeReferencedModels
- * Determines if the {@link IResourceScope model resource scopes} referenced by the context
- * {@link EObject eObject}'s {@link IResourceScope model resource scope} must be considered for the
- * research.
- * @return The resources in the context {@link EObject eObject}'s {@link IResourceScope model resource scope}.
- * @since 0.7.0
- */
- public static Collection<Resource> getResourcesInScope(EObject contextEObject, boolean includeReferencedScopes) {
- Resource contextResource = contextEObject.eResource();
- return getResourcesInScope(contextResource, includeReferencedScopes);
- }
-
- /**
- * Returns all resources owned by the {@link IResourceScope model resource scope} of the {@link IModelDescriptor
- * model descriptor} of the context {@link IWrapperItemProvider provider}.
- *
- * @param contextProvider
- * The {@link IWrapperItemProvider provider} used as context object for investigation.
- * @param includeReferencedModels
- * Determines if the {@link IResourceScope model resource scopes} referenced by the context
- * {@link IWrapperItemProvider provider}'s {@link IResourceScope model resource scope} must be considered
- * for the research.
- * @return The resources in the context {@link IWrapperItemProvider provider}'s {@link IResourceScope model resource
- * scope}.
- * @since 0.7.0
- */
- public static Collection<Resource> getResourcesInScope(IWrapperItemProvider contextProvider, boolean includeReferencedScopes) {
- Resource contextResource = getResource(contextProvider);
- return getResourcesInScope(contextResource, includeReferencedScopes);
- }
-
- /**
- * Returns all resources owned by the {@link IResourceScope model resource scope} of the {@link IModelDescriptor
- * model descriptor} of the context {@link FeatureMap.Entry feature map entry}.
- *
- * @param contextEntry
- * The {@link FeatureMap.Entry feature map entry} used as context object for investigation.
- * @param includeReferencedModels
- * Determines if the {@link IResourceScope model resource scopes} referenced by the context
- * {@link FeatureMap.Entry feature map entry}'s {@link IResourceScope model resource scope} must be
- * considered for the research.
- * @return The resources in the context {@link FeatureMap.Entry feature map entry}'s {@link IResourceScope model
- * resource scope}.
- * @since 0.7.0
- */
- public static Collection<Resource> getResourcesInScope(FeatureMap.Entry contextEntry, boolean includeReferencedScopes) {
- Resource contextResource = getResource(contextEntry);
- return getResourcesInScope(contextResource, includeReferencedScopes);
- }
-
- /**
- * Returns all resources owned by the {@link IResourceScope model resource scope} of the {@link IModelDescriptor
- * model descriptor} of the context {@link TransientItemProvider provider}.
- *
- * @param contextProvider
- * The {@link TransientItemProvider provider} used as context object for investigation.
- * @param includeReferencedModels
- * Determines if the {@link IResourceScope model resource scopes} referenced by the context
- * {@link TransientItemProvider provider}'s {@link IResourceScope model resource scope} must be
- * considered for the research.
- * @return The resources in the context {@link TransientItemProvider provider}'s {@link IResourceScope model
- * resource scope}.
- * @since 2.1
- */
- public static Collection<Resource> getResourcesInScope(TransientItemProvider contextProvider, boolean includeReferencedScopes) {
- Resource contextResource = getResource(contextProvider);
- return getResourcesInScope(contextResource, includeReferencedScopes);
- }
-
- /**
- * Returns all resources owned by the {@link IResourceScope model resource scope}s of the {@link IModelDescriptor
- * model descriptor}s of the context {@link IContainer container}.
- *
- * @param contextContainer
- * The {@link IContainer container} used as context object for investigation.
- * @param includeReferencedModels
- * Determines if the {@link IResourceScope model resource scopes} referenced by the context
- * {@link IContainer container}'s {@link IResourceScope model resource scope} must be considered for the
- * research.
- * @return The resources in the context {@link IContainer container}'s {@link IResourceScope model resource scope}.
- * @since 0.7.0
- */
- public static Collection<Resource> getResourcesInScopes(IContainer contextContainer, boolean includeReferencedScopes) {
- Collection<Resource> resourcesInScopes = new HashSet<Resource>();
- Collection<IModelDescriptor> modelDescriptors = ModelDescriptorRegistry.INSTANCE.getModels(contextContainer);
- for (IModelDescriptor modelDescriptor : modelDescriptors) {
- resourcesInScopes.addAll(getResourcesInScope(modelDescriptor, includeReferencedScopes));
- }
- return resourcesInScopes;
- }
-
- private static Collection<Resource> getResourcesInContext(Object contextObject) {
- Resource resource = EcoreResourceUtil.getResource(contextObject);
- if (resource != null) {
- ResourceSet resourceSet = resource.getResourceSet();
- if (resourceSet != null) {
- return resourceSet.getResources();
- }
- return Collections.singletonList(resource);
- }
- return Collections.emptySet();
- }
-
- public static ISchedulingRule createSaveNewSchedulingRule(Collection<ModelResourceDescriptor> modelResourceDescriptors) {
- if (modelResourceDescriptors != null) {
- Set<IPath> paths = new HashSet<IPath>();
- for (ModelResourceDescriptor descriptor : modelResourceDescriptors) {
- paths.add(descriptor.getPath());
- }
- return ExtendedPlatform.createSaveNewSchedulingRule(paths);
- }
- return null;
- }
-
- /**
- * Creates the {@linkplain ISchedulingRule scheduling rule} that is required for saving the specified
- * {@link Resource resource}.
- *
- * @param resource
- * The {@linkplain Resource resource} to be saved.
- * @return The {@linkplain ISchedulingRule scheduling rule} required for saving the given {@link Resource resource}.
- */
- public static ISchedulingRule createSaveSchedulingRule(Resource resource) {
- if (resource != null) {
- IFile modelFile = getFile(resource);
- return ExtendedPlatform.createSaveSchedulingRule(modelFile);
- }
- return null;
- }
-
- /**
- * Creates the {@linkplain ISchedulingRule scheduling rule} that are required for saving the specified
- * {@link Resource resource}s.
- *
- * @param resources
- * The {@linkplain Resource resource}s to be saved (for which scheduling rules must be created.
- * @return The scheduling {@linkplain MultiRule rule}s required for saving the given {@link Resource resources}.
- */
- public static ISchedulingRule createSaveSchedulingRule(Collection<Resource> resources) {
- if (resources != null) {
- /*
- * Performance optimization: Create a scheduling rule on a per resource basis only if number of resources is
- * reasonably low.
- */
- if (resources.size() < ExtendedPlatform.LIMIT_INDIVIDUAL_RESOURCES_SCHEDULING_RULE) {
- Set<ISchedulingRule> rules = new HashSet<ISchedulingRule>();
- for (Resource resource : resources) {
- ISchedulingRule schedulingRule = createSaveSchedulingRule(resource);
- if (schedulingRule != null) {
- rules.add(schedulingRule);
- }
- }
- return MultiRule.combine(rules.toArray(new ISchedulingRule[rules.size()]));
- } else {
- // Return workspace root as scheduling rule otherwise
- return ResourcesPlugin.getWorkspace().getRoot();
- }
- }
- return null;
- }
-
- private static ISchedulingRule createSaveSchedulingRule(Map<TransactionalEditingDomain, Collection<Resource>> resources) {
- if (resources != null) {
- Collection<Resource> allResources = new HashSet<Resource>();
- for (Collection<Resource> resourcesInEditingDomain : resources.values()) {
- allResources.addAll(resourcesInEditingDomain);
- }
- return createSaveSchedulingRule(allResources);
- }
- return null;
- }
-
- /**
- * Add a new model {@link Resource} to the provided {@link TransactionalEditingDomain} , created by using
- * {@link EObject} modelRoot as based for the containing model.
- *
- * @param editingDomain
- * The {@link TransactionalEditingDomain} where to add new resource.
- * @param path
- * The relative {@link IPath} of the new {@link Resource}.
- * @param contentTypeId
- * The contenType ID of the contained model.
- * @param modelRoot
- * The root element of the model to include in the new {@link Resource}.
- * @param async
- * Boolean parameter to determine if the execution must be synchronous or asynchronous.
- * @param monitor
- * The {@link IProgressMonitor}.
- */
- public static void addNewModelResource(TransactionalEditingDomain editingDomain, IPath path, final String contentTypeId, EObject modelRoot,
- boolean async, IProgressMonitor monitor) {
- addNewModelResources(editingDomain, Collections.singletonList(new ModelResourceDescriptor(modelRoot, path, contentTypeId)), async, monitor);
- }
-
- /**
- * * Add a new {@link Resource resource}s described by modelResourceDescriptors to the provided
- * {@link TransactionalEditingDomain editingDomain} .
- *
- * @param editingDomain
- * The {@link TransactionalEditingDomain} where to add new resource.
- * @param modelResourceDescriptors
- * The list of {@link ModelResourceDescriptor} describing new resources to add.
- * @param async
- * Boolean parameter to determine if the execution must be synchronous or asynchronous.
- * @param monitor
- * The {@link IProgressMonitor}.
- */
- public static void addNewModelResources(final TransactionalEditingDomain editingDomain,
- final Collection<ModelResourceDescriptor> modelResourceDescriptors, boolean async, final IProgressMonitor monitor) {
- if (modelResourceDescriptors != null && editingDomain != null && modelResourceDescriptors.size() > 0) {
- ISchedulingRule rule = createSaveNewSchedulingRule(modelResourceDescriptors);
- if (async) {
- Job job = new Job(modelResourceDescriptors.size() == 1 ? Messages.job_addingNewModelResource : Messages.job_addingNewModelResources) {
- @Override
- public IStatus run(IProgressMonitor monitor) {
- try {
- runAddNewModelResources(editingDomain, modelResourceDescriptors, monitor);
- return Status.OK_STATUS;
- } catch (CoreException ex) {
- return ex.getStatus();
- }
- }
- };
- job.setRule(rule);
- job.setPriority(Job.BUILD);
- job.schedule();
- } else {
- IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
- public void run(IProgressMonitor monitor) throws CoreException {
- runAddNewModelResources(editingDomain, modelResourceDescriptors, monitor);
- }
- };
- try {
- ResourcesPlugin.getWorkspace().run(runnable, rule, 0, monitor);
- } catch (CoreException ex) {
- PlatformLogUtil.logAsError(Activator.getDefault(), ex);
- }
- }
- }
-
- }
-
- private static void runAddNewModelResources(final TransactionalEditingDomain editingDomain,
- final Collection<ModelResourceDescriptor> modelResourceDescriptors, IProgressMonitor monitor) throws CoreException {
- Assert.isNotNull(editingDomain);
- Assert.isNotNull(modelResourceDescriptors);
- SubMonitor progress = SubMonitor.convert(monitor, modelResourceDescriptors.size() == 1 ? Messages.task_addingNewModelResource
- : Messages.task_addingNewModelResources, 1);
-
- Map<String, Object> transactionOptions = WorkspaceTransactionUtil.getDefaultSaveNewTransactionOptions();
- String label = modelResourceDescriptors.size() == 1 ? Messages.operation_addingNewModelResource : Messages.operation_addingNewModelResources;
- final IUndoableOperation operation = new AbstractEMFOperation(editingDomain, label, transactionOptions) {
- @Override
- protected IStatus doExecute(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
- IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
- public void run(IProgressMonitor monitor) throws CoreException {
- SubMonitor progress = SubMonitor.convert(monitor, modelResourceDescriptors.size());
- for (ModelResourceDescriptor descriptor : modelResourceDescriptors) {
- progress.subTask(NLS.bind(Messages.subtask_addingResource, descriptor.getPath().toString()));
-
- // Convert path to URI
- URI uri = URI.createPlatformResourceURI(descriptor.getPath().toString(), true);
-
- // add new resource
- EcoreResourceUtil.addNewModelResource(editingDomain.getResourceSet(), uri, descriptor.getContentTypeId(),
- descriptor.getModelRoot());
-
- progress.worked(1);
- }
- }
- };
-
- try {
- // Execute save operation as IWorkspaceRunnable on workspace in order to avoid resource change
- // notifications during transaction execution
- /*
- * !! Important Note !! Only set IWorkspace.AVOID_UPDATE flag but don't define any scheduling
- * restrictions for the save operation right here (this must only be done on outer workspace jobs or
- * workspace runnables from which this method is called). Otherwise it would be likely to end up in
- * deadlocks with operations which already have acquired exclusive access to the workspace but are
- * waiting for exclusive access to the model (i.e. for the transaction).
- */
- ResourcesPlugin.getWorkspace().run(runnable, null, IWorkspace.AVOID_UPDATE, monitor);
- return Status.OK_STATUS;
- } catch (CoreException ex) {
- return ex.getStatus();
- }
-
- }
-
- @Override
- public boolean canUndo() {
- // Avoid the save operation to appear in the undo menu
- return false;
- }
- };
- IOperationHistory history = WorkspaceTransactionUtil.getOperationHistory(editingDomain);
- try {
- history.execute(operation, progress.newChild(1), null);
- } catch (ExecutionException ex) {
- IStatus status = StatusUtil.createErrorStatus(Activator.getPlugin(), ex);
- throw new CoreException(status);
- }
- }
-
- public static void saveNewModelResource(TransactionalEditingDomain editingDomain, IPath path, final String contentTypeId, EObject modelRoot,
- boolean async, IProgressMonitor monitor) {
- saveNewModelResources(editingDomain, Collections.singletonList(new ModelResourceDescriptor(modelRoot, path, contentTypeId)),
- EcoreResourceUtil.getDefaultSaveOptions(), async, monitor);
- }
-
- public static void saveNewModelResources(TransactionalEditingDomain editingDomain, Collection<ModelResourceDescriptor> modelResourceDescriptors,
- boolean async, IProgressMonitor monitor) {
- saveNewModelResources(editingDomain, modelResourceDescriptors, EcoreResourceUtil.getDefaultSaveOptions(), async, monitor);
- }
-
- public static void saveNewModelResource(TransactionalEditingDomain editingDomain, IPath path, final String contentTypeId, EObject modelRoot,
- Map<?, ?> options, boolean async, IProgressMonitor monitor) {
- saveNewModelResources(editingDomain, Collections.singletonList(new ModelResourceDescriptor(modelRoot, path, contentTypeId)), options, async,
- monitor);
- }
-
- public static void saveNewModelResources(final TransactionalEditingDomain editingDomain,
- final Collection<ModelResourceDescriptor> modelResourceDescriptors, final Map<?, ?> options, boolean async, IProgressMonitor monitor) {
-
- if (modelResourceDescriptors != null && editingDomain != null && modelResourceDescriptors.size() > 0) {
- ISchedulingRule rule = createSaveNewSchedulingRule(modelResourceDescriptors);
- if (async) {
- Job job = new Job(modelResourceDescriptors.size() == 1 ? Messages.job_savingNewModelResource : Messages.job_savingNewModelResources) {
- @Override
- public IStatus run(IProgressMonitor monitor) {
- try {
- runSaveNewModelResources(editingDomain, modelResourceDescriptors, options, monitor);
- return Status.OK_STATUS;
- } catch (CoreException ex) {
- return ex.getStatus();
- }
- }
- };
- job.setRule(rule);
- job.setPriority(Job.BUILD);
- job.schedule();
- } else {
- IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
- public void run(IProgressMonitor monitor) throws CoreException {
- runSaveNewModelResources(editingDomain, modelResourceDescriptors, options, monitor);
- }
- };
- try {
- ResourcesPlugin.getWorkspace().run(runnable, rule, 0, monitor);
- } catch (CoreException ex) {
- PlatformLogUtil.logAsError(Activator.getDefault(), ex);
- }
- }
- }
- }
-
- /**
- * @param editingDomain
- * The {@link TransactionalEditingDomain editing domain} onto which a write-transaction must be created
- * in order to safely perform the saving; must not be <code>null</code>.
- * @param path
- * The {@link IPath path} of the new file to save; must not be <code>null</code>.
- * @param contentType
- * @param modelRoot
- * @param options
- * @param monitor
- */
- private static void runSaveNewModelResources(final TransactionalEditingDomain editingDomain,
- final Collection<ModelResourceDescriptor> modelResourceDescriptors, final Map<?, ?> options, IProgressMonitor monitor)
- throws CoreException {
- Assert.isNotNull(editingDomain);
- Assert.isNotNull(modelResourceDescriptors);
- SubMonitor progress = SubMonitor.convert(monitor, modelResourceDescriptors.size() == 1 ? Messages.task_savingNewModelResource
- : Messages.task_savingNewModelResources, 1);
-
- Map<String, Object> transactionOptions = WorkspaceTransactionUtil.getDefaultSaveNewTransactionOptions();
- String label = modelResourceDescriptors.size() == 1 ? Messages.operation_savingNewModelResource : Messages.operation_savingNewModelResources;
- final IUndoableOperation operation = new AbstractEMFOperation(editingDomain, label, transactionOptions) {
- @Override
- protected IStatus doExecute(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
- IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
- public void run(IProgressMonitor monitor) throws CoreException {
- SubMonitor progress = SubMonitor.convert(monitor, modelResourceDescriptors.size());
- for (ModelResourceDescriptor descriptor : modelResourceDescriptors) {
- progress.subTask(NLS.bind(Messages.subtask_savingResource, descriptor.getPath().toString()));
-
- // Convert path to URI
- URI uri = URI.createPlatformResourceURI(descriptor.getPath().toString(), true);
-
- try {
- // Save new resource
- /*
- * !! Important Note !! Resource must be saved before marking it as freshly saved
- * because otherwise the resource would loose its dirty state and consequently not be
- * saved at all.
- */
- EcoreResourceUtil.saveNewModelResource(editingDomain.getResourceSet(), uri, descriptor.getContentTypeId(),
- descriptor.getModelRoot(), options);
-
- // Mark resource as freshly saved in order to avoid that it gets automatically reloaded
- SaveIndicatorUtil.setSaved(editingDomain, descriptor.getModelRoot().eResource());
- } catch (Exception ex) {
- // Log exception in Error Log
- /*
- * !! Important Note !! The exception has already been recorded as error on the resource
- * and is principally subject to being converted to a problem marker later on (see
- * org.eclipse.sphinx.emf.util.EcoreResourceUtil.saveModelResource(Resource, Map<?,?>)
- * and org.eclipse.sphinx.emf.internal.resource.ResourceProblemHandler for details).
- * However, this is a new resource which had never saved before and does not yet exist
- * in the file system. As a consequence, there is no target to which the problem marker
- * could be attached and the problem behind this exception would remain unperceiveable
- * if we wouldn't log anything at this point.
- */
- PlatformLogUtil.logAsError(Activator.getPlugin(), ex);
- }
-
- progress.worked(1);
- }
- }
- };
-
- try {
- // Execute save operation as IWorkspaceRunnable on workspace in order to avoid resource change
- // notifications during transaction execution
- /*
- * !! Important Note !! Only set IWorkspace.AVOID_UPDATE flag but don't define any scheduling
- * restrictions for the save operation right here (this must only be done on outer workspace jobs or
- * workspace runnables from which this method is called). Otherwise it would be likely to end up in
- * deadlocks with operations which already have acquired exclusive access to the workspace but are
- * waiting for exclusive access to the model (i.e. for the transaction).
- */
- ResourcesPlugin.getWorkspace().run(runnable, null, IWorkspace.AVOID_UPDATE, monitor);
- return Status.OK_STATUS;
- } catch (CoreException ex) {
- return ex.getStatus();
- }
- }
-
- @Override
- public boolean canUndo() {
- // Avoid the save operation to appear in the undo menu
- return false;
- }
- };
- IOperationHistory history = WorkspaceTransactionUtil.getOperationHistory(editingDomain);
- try {
- history.execute(operation, progress.newChild(1), null);
- } catch (ExecutionException ex) {
- IStatus status = StatusUtil.createErrorStatus(Activator.getPlugin(), ex);
- throw new CoreException(status);
- }
- }
-
- /**
- * Saves all modified resources of the model behind the specified object (<em>i.e.</em> all resources in the context
- * of the given object).
- *
- * @param contextResource
- * The object resource identifying the model to save.
- * @param async
- * If <code>true</code>, model will be saved within a workspace job.
- * @param monitor
- * The progress monitor to use for showing save process progress.
- */
- public static void saveModel(Object contextObject, boolean async, IProgressMonitor monitor) {
- saveModel(contextObject, EcoreResourceUtil.getDefaultSaveOptions(), async, monitor);
- }
-
- /**
- * Saves all modified, writable models from a given context object.
- *
- * @param contextObject
- * The object context identifying the models to save.
- * @param options
- * The save options.
- * @param async
- * When passing <code>true</code> the model will be saved within a workspace job.
- * @param monitor
- * The progress monitor to use for showing save process progress.
- */
- // TODO Make sure that this method can also be used for contextObjects in resources which are located outside the
- // Eclipse workspace (only ISchedulingRule creation and IWorkspaceRunnable usages need to be adapted)
- public static void saveModel(Object contextObject, final Map<?, ?> options, boolean async, IProgressMonitor monitor) {
- if (contextObject instanceof IProject) {
- saveProject((IProject) contextObject, options, async, monitor);
- return;
- }
-
- SubMonitor progress = SubMonitor.convert(monitor, 100);
- Collection<Resource> resourcesInModel = new ArrayList<Resource>(getResourcesInModel(contextObject, true));
- final Map<TransactionalEditingDomain, Collection<Resource>> resourcesToSave = detectResourcesToSave(resourcesInModel,
- progress.newChild(async ? 100 : 5));
- if (resourcesToSave.size() > 0) {
- ISchedulingRule rule = createSaveSchedulingRule(resourcesToSave);
- if (async) {
- Job job = new Job(Messages.job_savingModel) {
- @Override
- public IStatus run(IProgressMonitor monitor) {
- try {
- runSaveModelResources(resourcesToSave, options, monitor);
- return Status.OK_STATUS;
- } catch (CoreException ex) {
- return ex.getStatus();
- }
- }
- };
-
- job.setRule(rule);
- job.setPriority(Job.BUILD);
- job.schedule();
- } else {
- IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
- public void run(IProgressMonitor monitor) throws CoreException {
- runSaveModelResources(resourcesToSave, options, monitor);
- }
- };
- try {
- ResourcesPlugin.getWorkspace().run(runnable, rule, 0, progress.newChild(95));
- } catch (CoreException ex) {
- PlatformLogUtil.logAsError(Activator.getDefault(), ex);
- }
- }
- }
- }
-
- /**
- * Saves all modified {@link Resource resource}s of all models behind the specified {@link IProject project}
- * including all referenced projects ( <em>i.e.</em> all resources in the context of the given project).
- *
- * @param project
- * The {@link IProject project} identifying the models to save.
- * @param async
- * If <code>true</code>, models will be saved within a workspace job.
- * @param monitor
- * The progress monitor to use for showing save process progress.
- */
- public static void saveProject(IProject project, boolean async, IProgressMonitor monitor) {
- saveProject(project, EcoreResourceUtil.getDefaultSaveOptions(), async, monitor);
- }
-
- /**
- * Saves all modified {@link Resource}s of all models behind the specified project including all referenced
- * {@link IProject}s ( <em>i.e.</em> all resources in the context of the given project).
- *
- * @param project
- * The {@link IProject project} identifying the models to save.
- * @param options
- * The save options.
- * @param async
- * If <code>true</code>, models will be saved within a workspace job.
- * @param monitor
- * The progress monitor to use for showing save process progress.
- */
- public static void saveProject(IProject project, final Map<?, ?> options, boolean async, IProgressMonitor monitor) {
- SubMonitor progress = SubMonitor.convert(monitor, 100);
- Collection<Resource> resourcesInProject = new ArrayList<Resource>(getResourcesInScopes(project, true));
- final Map<TransactionalEditingDomain, Collection<Resource>> resourcesToSave = detectResourcesToSave(resourcesInProject,
- progress.newChild(async ? 100 : 5));
- if (resourcesToSave.size() > 0) {
- ISchedulingRule rule = createSaveSchedulingRule(resourcesToSave);
- if (async) {
- Job job = new Job(resourcesToSave.size() == 1 ? Messages.job_savingModel : Messages.job_savingModels) {
- @Override
- public IStatus run(IProgressMonitor monitor) {
- try {
- runSaveModelResources(resourcesToSave, options, monitor);
- return Status.OK_STATUS;
- } catch (CoreException ex) {
- return ex.getStatus();
- }
- }
- };
- job.setRule(rule);
- job.setPriority(Job.BUILD);
- job.schedule();
- } else {
- IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
- public void run(IProgressMonitor monitor) throws CoreException {
- runSaveModelResources(resourcesToSave, options, monitor);
- }
- };
- try {
- ResourcesPlugin.getWorkspace().run(runnable, rule, 0, progress.newChild(95));
- } catch (CoreException ex) {
- PlatformLogUtil.logAsError(Activator.getDefault(), ex);
- }
- }
- }
- }
-
- private static Map<TransactionalEditingDomain, Collection<Resource>> detectResourcesToSave(Collection<Resource> resources,
- IProgressMonitor monitor) {
- SubMonitor progress = SubMonitor.convert(monitor, resources.size());
-
- Map<TransactionalEditingDomain, Collection<Resource>> resourcesToSave = new HashMap<TransactionalEditingDomain, Collection<Resource>>();
- for (Resource resource : resources) {
- TransactionalEditingDomain editingDomain = TransactionUtil.getEditingDomain(resource);
- if (editingDomain != null) {
- if (isResourceToSave(editingDomain, resource)) {
- Collection<Resource> resourcesToSaveInEditingDomain = resourcesToSave.get(editingDomain);
- if (resourcesToSaveInEditingDomain == null) {
- resourcesToSaveInEditingDomain = new HashSet<Resource>();
- resourcesToSave.put(editingDomain, resourcesToSaveInEditingDomain);
- }
- resourcesToSaveInEditingDomain.add(resource);
- }
- }
- progress.worked(1);
- }
- return resourcesToSave;
- }
-
- private static boolean isResourceToSave(TransactionalEditingDomain editingDomain, Resource contextResource) {
- Assert.isNotNull(editingDomain);
- return !editingDomain.isReadOnly(contextResource) && SaveIndicatorUtil.isDirty(editingDomain, contextResource);
- }
-
- /**
- * Saves model {@link Resource resource}s.
- *
- * @param resourcesToSave
- * The {@link TransactionalEditingDomain editing domain}s with the corresponding resources to save; must
- * not be <code>null</code>.
- * @param options
- * The save options.
- * @param monitor
- * The progress monitor to use (can be <code>null</code>).
- */
- private static void runSaveModelResources(final Map<TransactionalEditingDomain, Collection<Resource>> resourcesToSave, final Map<?, ?> options,
- IProgressMonitor monitor) throws CoreException {
- Assert.isNotNull(resourcesToSave);
- final SubMonitor progress = SubMonitor.convert(monitor, resourcesToSave.size());
-
- for (final TransactionalEditingDomain editingDomain : resourcesToSave.keySet()) {
- progress.setTaskName(resourcesToSave.get(editingDomain).size() == 1 ? Messages.task_savingModelResource
- : Messages.task_savingModelResources);
-
- /*
- * !! Important Note !! The saving of model resources as such does not imply any modifications of the
- * resource set and therefore could be done in a read transaction. However, it is possible that the
- * resources to be saved have a org.eclipse.emf.ecore.xmi.ResoureHandler registered upon them and that the
- * ResourceHandler#preSave() method performs some resource set modifying operations. In order to avoid
- * IllegalStateExceptions telling that the resource set cannot be modified without a write transaction in
- * such cases the whole save operation must be executed in a write transaction.
- */
- Map<String, Object> transactionOptions = WorkspaceTransactionUtil.getDefaultSaveTransactionOptions();
- String label = resourcesToSave.get(editingDomain).size() == 1 ? Messages.operation_savingModelResource
- : Messages.operation_savingModelResources;
- final IUndoableOperation operation = new AbstractEMFOperation(editingDomain, label, transactionOptions) {
- @Override
- protected IStatus doExecute(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
- IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
- public void run(IProgressMonitor monitor) throws CoreException {
- SubMonitor progress = SubMonitor.convert(monitor, resourcesToSave.get(editingDomain).size());
- for (Resource resource : resourcesToSave.get(editingDomain)) {
- progress.subTask(NLS.bind(Messages.subtask_savingResource, resource.getURI().toString()));
-
- try {
- // Save resource
- /*
- * !! Important Note !! Resource must be saved before marking it as freshly saved
- * because otherwise the resource would loose its dirty state and consequently not
- * be saved at all.
- */
- EcoreResourceUtil.saveModelResource(resource, options);
-
- // Mark resource as freshly saved in order to avoid that it gets automatically
- // reloaded
- SaveIndicatorUtil.setSaved(editingDomain, resource);
- } catch (Exception ex) {
- // Ignore exception, it has already been recorded as error on resource and will be
- // converted to a problem marker later on (see
- // org.eclipse.sphinx.emf.util.EcoreResourceUtil.saveModelResource(Resource,
- // Map<?,?>) and org.eclipse.sphinx.emf.internal.resource.ResourceProblemHandler for
- // details)
- }
-
- progress.worked(1);
- }
-
- // Refresh command stack state of associated editing domain
- ((BasicCommandStack) editingDomain.getCommandStack()).saveIsDone();
- }
- };
-
- try {
- // Execute save operation as IWorkspaceRunnable on workspace in order to avoid resource
- // change notifications during transaction execution
- /*
- * !! Important Note !! Only set IWorkspace.AVOID_UPDATE flag but don't define any scheduling
- * restrictions for the save operation right here (this must only be done on outer workspace
- * jobs or workspace runnables from which this method is called). Otherwise it would be likely
- * to end up in deadlocks with operations which already have acquired exclusive access to the
- * workspace but are waiting for exclusive access to the model (i.e. for the transaction).
- */
- ResourcesPlugin.getWorkspace().run(runnable, null, IWorkspace.AVOID_UPDATE, progress.newChild(1));
- return Status.OK_STATUS;
- } catch (CoreException ex) {
- return ex.getStatus();
- }
- }
-
- @Override
- public boolean canUndo() {
- // Avoid that save operation appears in the undo menu
- return false;
- }
- };
-
- IOperationHistory history = WorkspaceTransactionUtil.getOperationHistory(editingDomain);
- try {
- history.execute(operation, progress.newChild(1), null);
- } catch (ExecutionException ex) {
- IStatus status = StatusUtil.createErrorStatus(Activator.getPlugin(), ex);
- throw new CoreException(status);
- }
- }
- }
-
- /**
- * Unloads the model contained in given {@link IFile file}.
- *
- * @param file
- * The {@link IFile file} containing the model.
- * @see #unloadFile(TransactionalEditingDomain, IFile)
- */
- public static void unloadFile(IFile file) {
- /*
- * !! Important Note !! For the sake of robustness, it is necessary to consider all editing domains but not only
- * the one which would be returned by WorkspaceEditingDomainUtil#getCurrentEditingDomain(IFile). Although not
- * really intended by Sphinx workspace management it might anyway happen that the same file gets loaded into
- * multiple editing domains. Typical reasons for this are e.g. lazy loading of one file from multiple other
- * files which are in different editing domains or programatic action by some application. We then have to make
- * sure that the given file gets unloaded from all editing domains it is in.
- */
- Collection<TransactionalEditingDomain> editingDomains = WorkspaceEditingDomainUtil.getAllEditingDomains();
- for (TransactionalEditingDomain editingDomain : editingDomains) {
- if (EcorePlatformUtil.isFileLoaded(editingDomain, file)) {
- unloadFile(editingDomain, file);
- }
- }
- }
-
- /**
- * Unloads the model contained in given {@linkplain IFile file} from given {@linkplain TransactionalEditingDomain
- * editing domain}.
- *
- * @param file
- * The {@linkplain IFile file} containing the model.
- * @param editingDomain
- * The {@linkplain TransactionalEditingDomain editing domain} the <code>file</code> belongs to.
- */
- public static void unloadFile(final TransactionalEditingDomain editingDomain, final IFile file) {
- if (editingDomain != null && file != null) {
- try {
- editingDomain.runExclusive(new Runnable() {
- public void run() {
- try {
- URI uri = createURI(file.getFullPath());
- EcoreResourceUtil.unloadResource(editingDomain.getResourceSet(), uri);
- } catch (RuntimeException ex) {
- PlatformLogUtil.logAsError(Activator.getPlugin(), ex);
- }
- }
- });
- } catch (InterruptedException ex) {
- PlatformLogUtil.logAsError(Activator.getPlugin(), ex);
- }
- }
- }
-
- /**
- * Unloads the models contained in given {@linkplain IFile file}s from given {@linkplain TransactionalEditingDomain
- * editing domain} inside a read-only transaction.
- *
- * @param files
- * The {@link IFile file}s containing the models.
- * @param editingDomain
- * The {@link TransactionalEditingDomain editing domain} the {@link IFile file}s belong to.
- * @since 0.7.0
- */
- public static void unloadFiles(final TransactionalEditingDomain editingDomain, final Collection<IFile> files, final boolean memoryOptimized,
- IProgressMonitor monitor) throws OperationCanceledException {
- if (editingDomain != null && files != null && files.size() > 0) {
- final SubMonitor progress = SubMonitor.convert(monitor, Messages.task_unloadingModelFiles, files.size());
- if (progress.isCanceled()) {
- throw new OperationCanceledException();
- }
-
- try {
- editingDomain.runExclusive(new Runnable() {
- public void run() {
- for (IFile file : files) {
- progress.subTask(NLS.bind(Messages.subtask_unloadingModelFile, file.getFullPath().toString()));
-
- try {
- URI uri = createURI(file.getFullPath());
- EcoreResourceUtil.unloadResource(editingDomain.getResourceSet(), uri, memoryOptimized);
- } catch (RuntimeException ex) {
- PlatformLogUtil.logAsError(Activator.getPlugin(), ex);
- }
-
- progress.worked(1);
- if (progress.isCanceled()) {
- throw new OperationCanceledException();
- }
- editingDomain.yield();
- }
- }
- });
- } catch (InterruptedException ex) {
- PlatformLogUtil.logAsError(Activator.getPlugin(), ex);
- }
- }
- }
-
- /**
- * Unloads the specified {@linkplain Resource resource}s from given {@linkplain TransactionalEditingDomain editing
- * domain} inside a read-only transaction.
- *
- * @param memoryOptimized
- * @param resources
- * The resources to unload.
- * @param editingDomain
- * The editing domain owning {@link Resource resource}s.
- * @since 0.7.0
- */
- public static void unloadResources(final TransactionalEditingDomain editingDomain, final Collection<Resource> resources,
- final boolean memoryOptimized, IProgressMonitor monitor) throws OperationCanceledException {
- if (editingDomain != null && resources != null && resources.size() > 0) {
- final SubMonitor progress = SubMonitor.convert(monitor, Messages.task_unloadingModelResources, resources.size());
- if (progress.isCanceled()) {
- throw new OperationCanceledException();
- }
-
- try {
- editingDomain.runExclusive(new Runnable() {
- public void run() {
- List<Resource> safeResources = new ArrayList<Resource>(resources);
- for (Resource resource : safeResources) {
- progress.subTask(NLS.bind(Messages.subtask_unloadingModelResource, resource.getURI().toString()));
-
- try {
- if (editingDomain.getResourceSet().getResources().contains(resource)) {
- EcoreResourceUtil.unloadResource(resource, memoryOptimized);
- }
- } catch (RuntimeException ex) {
- PlatformLogUtil.logAsError(Activator.getPlugin(), ex);
- }
-
- progress.worked(1);
- if (progress.isCanceled()) {
- throw new OperationCanceledException();
- }
- editingDomain.yield();
- }
- }
- });
- } catch (InterruptedException ex) {
- PlatformLogUtil.logAsError(Activator.getPlugin(), ex);
- }
- }
- }
-
- /**
- * Unloads all {@linkplain Resource resource}s from given {@linkplain TransactionalEditingDomain editing domain}
- * inside a read-only transaction.
- *
- * @param editingDomain
- * The editing domain owning {@link Resource resource}s.
- */
- public static void unloadAllResources(final TransactionalEditingDomain editingDomain, IProgressMonitor monitor) throws OperationCanceledException {
- if (editingDomain != null) {
- final SubMonitor progress = SubMonitor.convert(monitor, Messages.task_unloadingModelResources, editingDomain.getResourceSet()
- .getResources().size());
- if (progress.isCanceled()) {
- throw new OperationCanceledException();
- }
-
- try {
- editingDomain.runExclusive(new Runnable() {
- public void run() {
- List<Resource> safeResources = new ArrayList<Resource>(editingDomain.getResourceSet().getResources());
- for (Resource resource : safeResources) {
- progress.subTask(NLS.bind(Messages.subtask_unloadingModelResource, resource.getURI().toString()));
-
- try {
- EcoreResourceUtil.unloadResource(resource, true);
- } catch (RuntimeException ex) {
- PlatformLogUtil.logAsError(Activator.getPlugin(), ex);
- }
-
- progress.worked(1);
- if (progress.isCanceled()) {
- throw new OperationCanceledException();
- }
- editingDomain.yield();
- }
- }
- });
- } catch (InterruptedException ex) {
- PlatformLogUtil.logAsError(Activator.getPlugin(), ex);
- }
- }
- }
-
- /**
- * Parses given {@link IFile file} and validates it against XSD schema with specified {@link URL url}. Raises an
- * exception if the {@link IFile file}'s content is not compliant with respect to XSD schema.
- *
- * @param file
- * The {@link IFile file} to be validated.
- * @param schemaURL
- * The {@link URL url} of the XSD schema to be used for validation.
- * @throws SAXException
- * @throws IOException
- */
- public static void validate(IFile file, URL schemaURL) throws SAXException, IOException {
- Assert.isNotNull(file);
-
- URI uri = createURI(file.getFullPath());
- EcoreResourceUtil.validate(uri, schemaURL);
- }
-}
diff --git a/sandbox/PapyrusEMFCompareV1/deprecated/org.eclipse.papyrus.emf.compare.transactional.editor/src/org/eclipse/papyrus/emf/compare/transactional/editor/sphinx/EcoreResourceUtil.java b/sandbox/PapyrusEMFCompareV1/deprecated/org.eclipse.papyrus.emf.compare.transactional.editor/src/org/eclipse/papyrus/emf/compare/transactional/editor/sphinx/EcoreResourceUtil.java
deleted file mode 100644
index fb8358afe32..00000000000
--- a/sandbox/PapyrusEMFCompareV1/deprecated/org.eclipse.papyrus.emf.compare.transactional.editor/src/org/eclipse/papyrus/emf/compare/transactional/editor/sphinx/EcoreResourceUtil.java
+++ /dev/null
@@ -1,1148 +0,0 @@
-/**
- * <copyright>
- *
- * Copyright (c) 2008-2011 See4sys, BMW Car IT, itemis and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * See4sys - Initial API and implementation
- * BMW Car IT - Added/Updated javadoc
- * itemis - [346715] IMetaModelDescriptor methods of MetaModelDescriptorRegistry taking EObject or Resource arguments should not start new EMF transactions
- * itemis - [357962] Make sure that problems occurring when saving model elements in a new resource are not recorded as errors/warnings on resource
- * </copyright>
- */
-package org.eclipse.papyrus.emf.compare.transactional.editor.sphinx;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.URL;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import javax.xml.XMLConstants;
-import javax.xml.transform.Source;
-import javax.xml.transform.stream.StreamSource;
-import javax.xml.validation.Schema;
-import javax.xml.validation.SchemaFactory;
-import javax.xml.validation.Validator;
-
-import org.eclipse.core.resources.ResourcesPlugin;
-import org.eclipse.core.runtime.Assert;
-import org.eclipse.core.runtime.IPath;
-import org.eclipse.core.runtime.Platform;
-import org.eclipse.emf.common.CommonPlugin;
-import org.eclipse.emf.common.notify.Notifier;
-import org.eclipse.emf.common.util.BasicEList;
-import org.eclipse.emf.common.util.EList;
-import org.eclipse.emf.common.util.URI;
-import org.eclipse.emf.common.util.WrappedException;
-import org.eclipse.emf.ecore.EObject;
-import org.eclipse.emf.ecore.resource.ContentHandler;
-import org.eclipse.emf.ecore.resource.Resource;
-import org.eclipse.emf.ecore.resource.Resource.Diagnostic;
-import org.eclipse.emf.ecore.resource.ResourceSet;
-import org.eclipse.emf.ecore.resource.URIConverter;
-import org.eclipse.emf.ecore.resource.impl.ExtensibleURIConverterImpl;
-import org.eclipse.emf.ecore.util.FeatureMap;
-import org.eclipse.emf.ecore.xmi.XMIException;
-import org.eclipse.emf.ecore.xmi.XMLResource;
-import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
-import org.eclipse.emf.edit.provider.IWrapperItemProvider;
-import org.eclipse.osgi.util.NLS;
-import org.eclipse.sphinx.emf.Activator;
-import org.eclipse.sphinx.emf.edit.TransientItemProvider;
-import org.eclipse.sphinx.emf.internal.messages.Messages;
-import org.eclipse.sphinx.emf.resource.ExtendedResource;
-import org.eclipse.sphinx.emf.resource.ExtendedResourceAdapterFactory;
-import org.eclipse.sphinx.emf.resource.ScopingResourceSetImpl;
-import org.eclipse.sphinx.platform.util.ExtendedPlatform;
-import org.eclipse.sphinx.platform.util.PlatformLogUtil;
-import org.eclipse.sphinx.platform.util.XMLRootElementHandler;
-import org.xml.sax.SAXException;
-
-/**
- * Utility class for resource management.
- */
-public final class EcoreResourceUtil {
-
- // Prevent from instantiation
- private EcoreResourceUtil() {
- }
-
- /**
- * Returns an instance of {@link ExtensibleURIConverterImpl} where the URI mappings are initialized in such a way
- * that normalization of non-platform:/resource {@link URI}s which reference resources inside the workspace yields
- * the corresponding platform:/resource {@link URI}s.
- *
- * @return An instance of {@link ExtensibleURIConverterImpl} containing URI mappings for normalizing
- * non-platform:/resource {@link URI}s referencing workspace resources to corresponding platform:/resource
- * {@link URI}s.
- */
- public static URIConverter getURIConverter() {
- return getURIConverter(null);
- }
-
- /**
- * Returns the {@link URIConverter URI converter} of given {@link ResourceSet resource set}. If no
- * {@link ResourceSet resource set} is provided an instance of {@link ExtensibleURIConverterImpl} is returned
- * instead. In both cases, the {@link URIConverter URI converter}'s URI mappings are initialized in such a way that
- * normalization of non-platform:/resource {@link URI}s which reference resources inside the workspace yields the
- * corresponding platform:/resource {@link URI}s.
- *
- * @param resourceSet
- * The {@link ResourceSet resource set} whose {@link URIConverter URI converter} is to be retrieved.
- * @return The {@link URIConverter URI converter} of given {@link ResourceSet resource set}, or an instance of
- * {@link ExtensibleURIConverterImpl} if no such is provided, containing URI mappings for normalizing
- * non-platform:/resource {@link URI}s referencing workspace resources to corresponding platform:/resource
- * {@link URI}s.
- */
- public static URIConverter getURIConverter(ResourceSet resourceSet) {
- // Retrieve or create URI converter
- URIConverter uriConverter;
- if (resourceSet != null) {
- uriConverter = resourceSet.getURIConverter();
- } else {
- uriConverter = new ExtensibleURIConverterImpl();
- }
- if (Platform.isRunning()) {
- // Initialize URI mappings
- IPath workspaceRootPath = ResourcesPlugin.getWorkspace().getRoot().getFullPath().addTrailingSeparator();
- URI workspaceRootURI = URI.createPlatformResourceURI(workspaceRootPath.toString(), true);
-
- IPath workspaceRootLocation = ResourcesPlugin.getWorkspace().getRoot().getLocation().addTrailingSeparator();
- URI workspaceRootLocationURI = URI.createURI(workspaceRootLocation.toString(), true);
- URI workspaceRootLocationFileURI = URI.createFileURI(workspaceRootLocation.toString());
-
- uriConverter.getURIMap().put(workspaceRootLocationURI, workspaceRootURI);
- uriConverter.getURIMap().put(workspaceRootLocationFileURI, workspaceRootURI);
- }
- return uriConverter;
- }
-
- /**
- * Converts given {@link URI} into an absolute file {@link URI}.
- *
- * @param uri
- * The {@link URI} to be converted.
- * @return Absolute file {@link URI} for the given {@link URI} or given {@link URI} if no conversion is possible.
- */
- public static URI convertToAbsoluteFileURI(URI uri) {
- Assert.isNotNull(uri);
-
- // FIXME File bug to EMF: CommonPlugin.asLocalURI() is unable to convert URIs that start with a driver letter
- // (IOException raised and caught internally)
- if (!uri.isRelative() && uri.scheme().matches("[A-Z]")) { //$NON-NLS-1$
- uri = URI.createFileURI(uri.toString());
- }
-
- // Try to convert given URI to absolute file URI right away
- URI convertedURI = CommonPlugin.asLocalURI(uri);
-
- // Resulting URI still relative?
- if (convertedURI.isRelative()) {
- // Normalize given URI and try to convert it again
- uri = getURIConverter().normalize(uri);
- convertedURI = CommonPlugin.asLocalURI(uri);
- }
-
- return convertedURI;
- }
-
- /**
- * Converts given URI into a platform resource URI.
- *
- * @param uri
- * The {@link URI} to be converted.
- * @return platform resource URI for the given URI or given URI if it references a location outside the workspace or
- * platform is not available.
- */
- public static URI convertToPlatformResourceURI(URI uri) {
- Assert.isNotNull(uri);
-
- // Already a platform resource URI?
- if (uri.isPlatformResource()) {
- return uri;
- } else {
- // Try to convert given URI to platform resource URI
- return getURIConverter().normalize(uri);
- }
- }
-
- /**
- * Proves if resource specified by an URI exists.
- *
- * @param uri
- * The URI to prove Returns <b>true</b> only if the URI represents a file and if this file exists.
- */
- public static boolean exists(URI uri) {
- if (uri != null) {
- return getURIConverter().exists(uri, null);
- }
- return false;
- }
-
- /**
- * Returns the id of the content type of the file behind given {@link URI}.
- *
- * @param uri
- * The {@link URI} whose content type id is to be established.
- * @return The id of the content type of the file behind given {@link URI}, or <code>null</code> if given
- * {@link URI} references a resource which is no file (e.g., a folder or project), or a file that does not
- * exist or has no content type.
- */
- public static String getContentTypeId(URI uri) {
- if (uri != null) {
- try {
- Map<String, ?> contentDescription = getURIConverter().contentDescription(uri, null);
- return (String) contentDescription.get(ContentHandler.CONTENT_TYPE_PROPERTY);
- } catch (Exception ex) {
- PlatformLogUtil.logAsError(Activator.getPlugin(), ex);
- }
- }
- return null;
- }
-
- /**
- * Determines if {@link Resource resource} behind specified {@link URI} is read-only. Returns <code>false</code> if
- * this resource does not exist.
- *
- * @param uri
- * The {@link URI} identifying the {@link Resource resource} to be investigated.
- * @return <code>true</code> if {@link Resource resource} behind specified {@link URI} is read-only, and
- * <code>false</code> otherwise.
- */
- public static boolean isReadOnly(URI uri) {
- if (uri != null) {
- Map<String, ?> attributes = getURIConverter().getAttributes(uri, null);
- Object readOnly = attributes.get(URIConverter.ATTRIBUTE_READ_ONLY);
- return readOnly instanceof Boolean && (Boolean) readOnly;
- }
- return false;
- }
-
- /**
- * Reads the model namespace (i.e. XML namespace) of given {@link Resource resource}. Returns a meaningful result
- * only if given {@link Resource resource} is an XML document.
- *
- * @param resource
- * The {@link Resource resource} to investigate.
- * @return The model namespace denoted in given {@link Resource resource} or <code>null</code> if the
- * {@link Resource resource} is either a non-XML file or an XML file which is not well-formed or has no
- * model namespace.
- */
- public static String readModelNamespace(Resource resource) {
- if (resource != null) {
- return readModelNamespace(EcoreResourceUtil.getURIConverter(resource.getResourceSet()), resource.getURI());
- }
- return null;
- }
-
- /**
- * Reads the model namespace (i.e. XML namespace) of resource behind given {@link URI}. Returns a meaningful result
- * only if the resource in question is an XML document.
- *
- * @param uriConverter
- * The {@link URIConverter uriConverter} used to create {@link InputStream inputstream} . May be
- * <code>null</code>.
- * @param uri
- * The {@link URI uri} of the resource to investigate.
- * @return The model namespace denoted in resource behind given {@link URI} or <code>null</code> if the resource in
- * question is either a non-XML file or an XML file which is not well-formed or has no model namespace.
- */
- public static String readModelNamespace(URIConverter uriConverter, URI uri) {
- String namespace = null;
- if (exists(uri)) {
- InputStream inputStream = null;
- try {
- uriConverter = uriConverter != null ? uriConverter : new ExtensibleURIConverterImpl();
- inputStream = uriConverter.createInputStream(uri);
- XMLRootElementHandler handler = new XMLRootElementHandler();
- handler.parseContents(inputStream);
- namespace = handler.getRootElementNamespace();
- } catch (SAXException ex) {
- // Ignore parse exceptions because we might be face to non-XML files or XML files
- // which are not well-formed - that's o.k. simply return null
- } catch (IOException ex) {
- // Ignore I/O exceptions because we might be face to non-XML files or XML files
- // which are not well-formed - that's o.k. simply return null
- } catch (Exception ex) {
- PlatformLogUtil.logAsWarning(Activator.getPlugin(), ex);
- } finally {
- ExtendedPlatform.safeClose(inputStream);
- }
- }
- return namespace;
- }
-
- /**
- * Reads the target namespace of given {@link Resource resource}. Returns a meaningful result only if given
- * {@link Resource resource} is an XML document.
- *
- * @param resource
- * The {@link Resource resource} to investigate.
- * @return The target namespace denoted in given {@link Resource resource} or <code>null</code> if the
- * {@link Resource resource} is either a not an XML file or an XML file which is not well-formed or has no
- * target namespace.
- */
- public static String readTargetNamespace(Resource resource) {
- return readTargetNamespace(resource, (String[]) null);
- }
-
- public static String readTargetNamespace(Resource resource, String... targetNamespaceExcludePatterns) {
- if (resource != null) {
- return readTargetNamespace(getURIConverter(resource.getResourceSet()), resource.getURI(), targetNamespaceExcludePatterns);
- }
- return null;
- }
-
- /**
- * Reads the target namespace of the resource behind given {@link URI}. Returns a meaningful result only if the
- * resource in question is an XML document.
- *
- * @param uriConverter
- * The {@link URIConverter uriConverter} used to create {@link InputStream input stream} . May be
- * <code>null</code>.
- * @param uri
- * The {@link URI uri} of the resource to investigate.
- * @return The target namespace denoted in resource behind given {@link URI} or <code>null</code> if the resource in
- * question is either a not an XML file or an XML file which is not well-formed or has no target namespace.
- */
- public static String readTargetNamespace(URIConverter uriConverter, URI uri) {
- return readTargetNamespace(uriConverter, uri, (String[]) null);
- }
-
- public static String readTargetNamespace(URIConverter uriConverter, URI uri, String... targetNamespaceExcludePatterns) {
- String targetNamespace = null;
- if (exists(uri)) {
- InputStream inputStream = null;
- try {
- uriConverter = uriConverter != null ? uriConverter : new ExtensibleURIConverterImpl();
- inputStream = uriConverter.createInputStream(uri);
- XMLRootElementHandler handler = new XMLRootElementHandler();
- if (targetNamespaceExcludePatterns != null) {
- handler.seTargetNamespaceExcludePatterns(targetNamespaceExcludePatterns);
- }
- handler.parseContents(inputStream);
- targetNamespace = handler.getTargetNamespace();
- } catch (SAXException ex) {
- // Ignore parse exceptions because we might be face to non-XML files or XML files
- // which are not well-formed - that's o.k. simply return null
- } catch (IOException ex) {
- // Ignore I/O exceptions because we might be face to non-XML files or XML files
- // which are not well-formed - that's o.k. simply return null
- } catch (Exception ex) {
- PlatformLogUtil.logAsWarning(Activator.getPlugin(), ex);
- } finally {
- ExtendedPlatform.safeClose(inputStream);
- }
- }
- return targetNamespace;
- }
-
- /**
- * Retrieves the XML comments located above the root element in given {@link Resource resource}. Returns a
- * meaningful result only if given {@link Resource resource} is an XML document.
- *
- * @param resource
- * The {@link Resource resource} to investigate.
- * @return Collection of strings representing the retrieved XML comments or empty collection if no such could be
- * found.
- */
- public static Collection<String> readRootElementComments(Resource resource) {
- if (resource != null) {
- return readRootElementComments(getURIConverter(resource.getResourceSet()), resource.getURI());
- }
- return Collections.emptyList();
- }
-
- /**
- * Retrieves the XML comments located above the root element in resource behind given {@link URI}. Returns a
- * meaningful result only if the resource in question is an XML document.
- *
- * @param uriConverter
- * The {@link URIConverter uriConverter} used to create {@link InputStream input stream} . May be
- * <code>null</code>.
- * @param uri
- * The {@link URI uri} of the resource to investigate.
- * @return Collection of strings representing the retrieved XML comments or empty collection if no such could be
- * found.
- */
- public static Collection<String> readRootElementComments(URIConverter uriConverter, URI uri) {
- Collection<String> comments = new ArrayList<String>();
-
- if (exists(uri)) {
- InputStream inputStream = null;
- try {
- uriConverter = uriConverter != null ? uriConverter : new ExtensibleURIConverterImpl();
- inputStream = uriConverter.createInputStream(uri);
- XMLRootElementHandler handler = new XMLRootElementHandler();
- // Parse and retrieve comments above root element
- handler.parseContents(inputStream, true);
- comments = handler.getRootElementComments();
- } catch (SAXException ex) {
- // Ignore parse exceptions because we might be face to non-XML files or XML files
- // which are not well-formed - that's o.k. simply return null
- } catch (IOException ex) {
- // Ignore I/O exceptions because we might be face to non-XML files or XML files
- // which are not well-formed - that's o.k. simply return null
- } catch (Exception ex) {
- PlatformLogUtil.logAsWarning(Activator.getPlugin(), ex);
- } finally {
- ExtendedPlatform.safeClose(inputStream);
- }
- }
- return comments;
- }
-
- /**
- * Reads the XSI schema location of given {@link Resource resource} and extracts pairs of namespace and schema URIs
- * from it (see http://www.w3.org/TR/xmlschema-0/#schemaLocation for details). Returns a meaningful result only if
- * given {@link Resource resource} is an XML document.
- *
- * @param resource
- * The {@link Resource resource} to investigate.
- * @return Pairs of namespace and schema URIs in given {@link Resource resource} or an empty map if the
- * {@link Resource resource} is either a non-XML file or an XML file which is not well-formed or has no XSI
- * schema location.
- */
- public static Map<String, String> readSchemaLocationEntries(Resource resource) {
- if (resource != null) {
- return readSchemaLocationEntries(getURIConverter(resource.getResourceSet()), resource.getURI());
- }
- return Collections.emptyMap();
- }
-
- /**
- * Reads the XSI schema location of the resource behind given {@link URI} and extracts pairs of namespace and schema
- * URIs from it (see http://www.w3.org/TR/xmlschema-0/#schemaLocation for details). Returns a meaningful result only
- * if the resource in question is an XML document.
- *
- * @param uriConverter
- * The {@link URIConverter uriConverter} used to create {@link InputStream input stream} . May be
- * <code>null</code>.
- * @param uri
- * The {@link URI uri} of the {@link Resource resource} to investigate.
- * @return Pairs of namespace and schema URIs in resource behind given {@link URI} or an empty map if the resource
- * in question is either a non-XML file or an XML file which is not well-formed or has no XSI schema
- * location.
- */
- public static Map<String, String> readSchemaLocationEntries(URIConverter uriConverter, URI uri) {
- String schemaLocation = null;
- if (exists(uri)) {
- InputStream inputStream = null;
- try {
- uriConverter = uriConverter != null ? uriConverter : new ExtensibleURIConverterImpl();
- inputStream = uriConverter.createInputStream(uri);
- XMLRootElementHandler handler = new XMLRootElementHandler();
- handler.parseContents(inputStream);
- schemaLocation = handler.getSchemaLocation();
- } catch (SAXException ex) {
- // Ignore parse exceptions because we might be face to non-XML files or XML files
- // which are not well-formed - that's o.k. simply return null
- } catch (IOException ex) {
- // Ignore I/O exceptions because we might be face to non-XML files or XML files
- // which are not well-formed - that's o.k. simply return null
- } catch (Exception ex) {
- PlatformLogUtil.logAsWarning(Activator.getPlugin(), ex);
- } finally {
- ExtendedPlatform.safeClose(inputStream);
- }
- }
- Map<String, String> schemaLocationEntries = new HashMap<String, String>();
- if (schemaLocation != null) {
- String[] schemaLocationTokens = schemaLocation.split(" "); //$NON-NLS-1$
- for (int i = 0; i + 1 < schemaLocationTokens.length; i = i + 2) {
- schemaLocationEntries.put(schemaLocationTokens[i], schemaLocationTokens[i + 1]);
- }
- }
- return schemaLocationEntries;
- }
-
- /**
- * Returns a set of default options which can be used for loading a Resource.
- *
- * @return A set of default options for loading a Resource.
- */
- public static Map<?, ?> getDefaultLoadOptions() {
- HashMap<Object, Object> options = new HashMap<Object, Object>(1);
-
- // Be fault-tolerant and enable files which are partially broken to be loaded
- options.put(XMLResource.OPTION_RECORD_UNKNOWN_FEATURE, Boolean.TRUE);
-
- return options;
- }
-
- /**
- * Returns a map with the default options for resource saving.
- *
- * @return This method will return an empty map.
- */
- public static Map<?, ?> getDefaultSaveOptions() {
- return Collections.emptyMap();
- }
-
- /**
- * Retrieves the top level EObject contained within a resource.
- *
- * @param resource
- * Some model resource
- * @return The first EObject from the resource's content. Returns <code>null</code> if:
- * <ul>
- * <li><code>resource</code> is <code>null</code>;</li>
- * <li><code>resource</code> has no content;</li>
- * <li>content does not contain an EObject as its first element.</li>
- * </ul>
- */
- public static EObject getModelRoot(Resource resource) {
- if (resource != null) {
- EList<EObject> contents = resource.getContents();
- if (contents.size() > 0) {
- return resource.getContents().get(0);
- }
- }
- return null;
- }
-
- /**
- * Returns the root element of the model owned by the resource specified by the given URI. Does not explicitly ask
- * the loading of the resource if it has not already been loaded in resource set.
- *
- * @param resourceSet
- * The resource set into which model resource must be loaded.
- * @param uri
- * The URI to resolve; i.e. the URI of the model resource to load.
- * @return The root of the loaded model or <code>null</code> if underlying resource has not been loaded.
- */
- public static EObject getModelRoot(ResourceSet resourceSet, URI uri) {
- return loadModelRoot(resourceSet, uri, null, false);
- }
-
- /**
- * Returns the element of the model owned by the resource specified by the given URI and pointed by the given
- * fragment. Does not explicitly ask the loading of the resource if it has not already been loaded in resource set.
- *
- * @param resourceSet
- * The resource set into which model resource must be loaded.
- * @param uri
- * The URI to resolve; i.e. the URI of the model resource to load.
- * @return The element of the loaded model pointed by the fragment of the uri or <code>null</code> if underlying
- * resource has not been loaded.
- */
- public static EObject getModelFragment(ResourceSet resourceSet, URI uri) {
- return loadModelFragment(resourceSet, uri, false);
- }
-
- /**
- * Returns the element of the model pointed by the fragment owned by the resource specified by the given {@link URI
- * uri}. Asks the loading of the resource if it has not already been loaded in resource set.
- *
- * @param resourceSet
- * The resource set into which model resource must be loaded.
- * @param uri
- * The {@link URI uri} containing a fragment to resolve; i.e. the URI of the model resource to load
- * append with the reference to the model object.
- * @return The object of the loaded model referenced by the fragment, or <code>null</code> if the URI cannot be
- * resolved to an existing file.
- */
- public static EObject loadModelFragment(ResourceSet resourceSet, URI uri) {
- return loadModelFragment(resourceSet, uri, true);
- }
-
- private static EObject loadModelFragment(ResourceSet resourceSet, URI uri, boolean loadOnDemand) {
- Assert.isNotNull(resourceSet);
- Assert.isNotNull(uri);
-
- if (uri.hasFragment()) {
- // Try to convert given URI to platform:/resource URI if not yet so
- /*
- * !! Important Note !! This is necessary in order to avoid that resources which are located inside the
- * workspace get loaded multiple times just because they are referenced by URIs with different schemes. If
- * given resource set were an instance of ResourceSetImpl this extra conversion wouldn't be necessary.
- * org.eclipse.emf.ecore.resource.ResourceSet.getResource(URI, boolean) normalizes and compares given URI
- * and to normalized copies of URIs of already present resources and thereby avoids multiple loading of same
- * resources on its own. This is however not true when ExtendedResourceSetImpl or a subclass of it is used.
- * Herein, URI normalization and comparison has been removed from
- * org.eclipse.sphinx.emf.resource.ExtendedResourceSetImpl.getResource(URI, boolean) in order to increase
- * runtime performance.
- */
- if (!uri.isPlatform()) {
- uri = convertToPlatformResourceURI(uri);
- }
-
- return resourceSet.getEObject(uri, loadOnDemand);
- }
- return null;
- }
-
- /**
- * Returns the root element of the model owned by the resource specified by the given URI. Asks the loading of the
- * resource if it has not already been loaded in resource set.
- *
- * @param resourceSet
- * The resource set into which model resource must be loaded.
- * @param uri
- * The URI to resolve; i.e. the URI of the model resource to load.
- * @param options
- * The load options. If <code>null</code>, default loading options are used.
- * @return The root of the loaded model.
- */
- public static EObject loadModelRoot(ResourceSet resourceSet, URI uri, Map<?, ?> options) {
- return loadModelRoot(resourceSet, uri, options, true);
- }
-
- /**
- * Loads a model from a {@link java.io.File File} in a given {@link ResourceSet}.
- * <p>
- * This will return the first root of the loaded model, other roots can be accessed via the resource's content.
- * </p>
- *
- * @param resourceSet
- * The {@link ResourceSet} to load the model in.
- * @param file
- * {@link java.io.File File} containing the model to be loaded.
- * @param options
- * The load options. If <code>null</code>, default loading options are used.
- * @return The model loaded from the file.
- * @throws IOException
- * If the given file does not exist.
- */
- public static EObject loadModelRoot(ResourceSet resourceSet, File file, Map<?, ?> options) throws IOException {
- Assert.isNotNull(file);
- return loadModelRoot(resourceSet, URI.createFileURI(file.getPath()), options);
- }
-
- /**
- * Returns the {@linkplain Resource resource} corresponding to the specified {@linkplain Object object}.
- * <p>
- * The supported object types are:
- * <ul>
- * <li>{@linkplain org.eclipse.emf.ecore.resource.Resource}</li>
- * <li>{@linkplain org.eclipse.emf.ecore.EObject}</li>
- * <li>{@linkplain org.eclipse.emf.ecore.util.FeatureMap.Entry}</li>
- * <li>{@linkplain org.eclipse.emf.edit.provider.IWrapperItemProvider}</li>
- * </ul>
- * <p>
- * If the type of the specified object does not belongs to that list of supported types, <code>null</code> is
- * returned.
- *
- * @param object
- * The object from which a resource must be returned.
- * @return The underlying resource from the given object.
- */
- public static Resource getResource(Object object) {
- if (object instanceof Resource) {
- return (Resource) object;
- } else if (object instanceof EObject) {
- return getResource((EObject) object);
- } else if (object instanceof IWrapperItemProvider) {
- return getResource((IWrapperItemProvider) object);
- } else if (object instanceof FeatureMap.Entry) {
- return getResource((FeatureMap.Entry) object);
- } else if (object instanceof TransientItemProvider) {
- return getResource((TransientItemProvider) object);
- }
- return null;
- }
-
- /**
- * Retrieves the {@linkplain Resource resource} corresponding to the given {@link EObject object}.
- *
- * @param eObject
- * The {@linkplain EObject object} whose {@link Resource resource} is to be returned.
- * @return The resource corresponding to the specified {@link EObject object}.
- */
- public static Resource getResource(final EObject eObject) {
- if (eObject != null) {
- return eObject.eResource();
- }
- return null;
- }
-
- /**
- * Retrieves the {@linkplain Resource resource} owning the given {@link IWrapperItemProvider provider}.
- * <p>
- * First retrieves the owner of the {@link IWrapperItemProvider provider}; then, if owner is an {@linkplain EObject}
- * returns its resource, else delegates to {@linkplain #getResource(Object)}.
- *
- * @param provider
- * The {@linkplain IWrapperItemProvider} whose resource must be returned.
- * @return The resource containing the specified {@link IWrapperItemProvider provider}; <code>null</code> if that
- * provider is <code>null</code>.
- */
- public static Resource getResource(final IWrapperItemProvider provider) {
- if (provider != null) {
- Object owner = provider.getOwner();
- if (owner instanceof EObject) {
- return ((EObject) owner).eResource();
- } else {
- Object unwrapped = AdapterFactoryEditingDomain.unwrap(provider);
- return getResource(unwrapped);
- }
- }
- return null;
- }
-
- /**
- * Retrieves the {@linkplain Resource resource} matching the given {@link FeatureMap.Entry entry}.
- * <p>
- * First unwraps the {@link FeatureMap.Entry entry}; then, delegates to {@linkplain #getResource(Object)}.
- *
- * @param entry
- * The {@linkplain FeatureMap.Entry} whose underlying resource must be returned.
- * @return The resource under the specified {@link FeatureMap.Entry entry}.
- */
- public static Resource getResource(FeatureMap.Entry entry) {
- Object unwrapped = AdapterFactoryEditingDomain.unwrap(entry);
- return getResource(unwrapped);
- }
-
- /**
- * Retrieves the {@linkplain Resource resource} owning the given {@link TransientItemProvider provider}.
- * <p>
- * First retrieves the owner of the {@link TransientItemProvider provider}; then, if owner is an
- * {@linkplain EObject} returns its resource, else delegates to {@linkplain #getResource(Object)}.
- *
- * @param provider
- * The {@linkplain TransientItemProvider} whose resource must be returned.
- * @return The resource containing the specified {@link IWrapperItemProvider provider}; <code>null</code> if that
- * provider is <code>null</code>.
- */
- public static Resource getResource(TransientItemProvider provider) {
- if (provider != null) {
- Notifier target = provider.getTarget();
- if (target instanceof EObject) {
- return ((EObject) target).eResource();
- }
- }
- return null;
- }
-
- /**
- * Returns the contents of given Resource. If the provided Resource is
- * <tt>null<tt> the method will return an empty list.
- *
- * @param resource
- * @return The content of the given <code>resource</code> or an empty list if no Resource is provided.
- */
- public static EList<EObject> getResourceContents(Resource resource) {
- if (resource != null) {
- return resource.getContents();
- }
- return new BasicEList<EObject>(0);
- }
-
- /**
- * Returns the name of the model for the provided model root.
- *
- * @param modelRoot
- * Can either be an EObject or a Resource.
- * @return The name of the model specified by the given <code>modelRoot</code>.
- */
- public static String getModelName(Notifier modelRoot) {
- EObject modelContent = null;
- if (modelRoot instanceof EObject) {
- modelContent = (EObject) modelRoot;
- }
- if (modelRoot instanceof Resource) {
- modelContent = EcoreResourceUtil.getModelRoot((Resource) modelRoot);
- }
- if (modelContent != null) {
- String modelPackageName = modelContent.eClass().getEPackage().getName();
- return modelPackageName.substring(0, 1).toUpperCase() + modelPackageName.substring(1);
- }
- return ""; //$NON-NLS-1$
- }
-
- /**
- * Proves if the model with the specified {@link URI uri} is already loaded into the given {@link ResourceSet
- * resourceSet}.
- *
- * @param resourceSet
- * The resource set to search resource in.
- * @param uri
- * The URI of the concerned resource.
- * @return <ul>
- * <li><tt><b>true</b>&nbsp;&nbsp;</tt> if resource set contains the model with specified the URI;</li>
- * <li><tt><b>false</b>&nbsp;</tt> otherwise.</li>
- * </ul>
- */
- public static boolean isResourceLoaded(ResourceSet resourceSet, URI uri) {
- if (resourceSet != null && uri != null) {
- Resource resource = resourceSet.getResource(uri, false);
- return resource != null && resource.isLoaded();
- }
- return false;
- }
-
- /**
- * Create the new model given by the {@link EObject modelRoot} parameter. The method will create a new Resource
- * specified by a given URI and content type id. The new created resource will be added to a given ResourceSet.
- *
- * @param resourceSet
- * The ResourceSet to which the new Resource is added.
- * @param uri
- * The URI specifying the location to which the Resource is to be saved to.
- * @param contentTypeId
- * The id of the content type of which new created Resource shall be of.
- * @param modelRoot
- * Can either be an EObject or a Resource.
- * @since 0.7.0
- */
- public static Resource addNewModelResource(ResourceSet resourceSet, URI uri, String contentTypeId, EObject modelRoot) {
- if (uri != null && modelRoot != null) {
- try {
- // Create new ResourceSet if none has been provided
- if (resourceSet == null) {
- resourceSet = new ScopingResourceSetImpl();
- }
-
- // Unload and remove model resource if it is already loaded
- Resource resource = resourceSet.getResource(uri, false);
- if (resource != null) {
- try {
- unloadResource(resource);
- } catch (Exception ex) {
- PlatformLogUtil.logAsError(Activator.getPlugin(), ex);
- }
- }
-
- // Create and add new model resource to the resourceSet
- resource = resourceSet.createResource(uri, contentTypeId);
- if (resource != null) {
- resource.getContents().add(modelRoot);
- }
- return resource;
- } catch (Exception ex) {
- throw new WrappedException(ex);
- }
- }
- return null;
- }
-
- /**
- * The {@link Resource} provided as argument will be added to the given ResourceSet if it is not already inside.
- *
- * @param resourceSet
- * The resourceSet where to add resources
- * @param resource
- * The resource to add
- * @since 0.7.0
- */
- public static void addModelResource(ResourceSet resourceSet, Resource resource) {
- if (resource != null) {
- // Create new ResourceSet if none has been provided
- if (resourceSet == null) {
- resourceSet = new ScopingResourceSetImpl();
- }
-
- // Add resource to resourceSet if not already present
- if (resourceSet.getResource(resource.getURI(), false) == null) {
- resourceSet.getResources().add(resource);
- }
- }
- }
-
- /**
- * Saves the new model given by the {@link EObject modelRoot} parameter. The method will create a new Resource
- * specified by a given URI and content type id. The new created resource will be added to a given ResourceSet and
- * saved.
- *
- * @param resourceSet
- * The ResourceSet to which the new Resource is added.
- * @param uri
- * The URI specifying the location to which the Resource is to be saved to.
- * @param contentTypeId
- * The id of the content type of which new created Resource shall be of.
- * @param modelRoot
- * Can either be an EObject or a Resource.
- * @param options
- * The save options.
- * @see #getDefaultSaveOptions()
- */
- public static void saveNewModelResource(ResourceSet resourceSet, URI uri, String contentTypeId, EObject modelRoot, Map<?, ?> options) {
- // Create new model resource and add it to the provided ResourceSet
- Resource resource = addNewModelResource(resourceSet, uri, contentTypeId, modelRoot);
-
- // Save the newly created resource
- saveModelResource(resource, options);
- }
-
- /**
- * Saves the specified <code>resource</code>.
- *
- * @param resource
- * The {@link Resource resource} to be saved.
- * @param options
- * The save options.
- * @see #getDefaultSaveOptions()
- */
- public static void saveModelResource(Resource resource, Map<?, ?> options) {
- if (resource != null) {
- try {
- resource.save(options);
- } catch (IOException ex) {
- // Record exception as error on resource
- Throwable cause = ex.getCause();
- Exception exception = cause instanceof Exception ? (Exception) cause : ex;
- URI uri = resource.getURI();
- resource.getErrors()
- .add(new XMIException(NLS.bind(Messages.error_problemOccurredWhenSavingResource, uri.toString()), exception, uri.toString(),
- 1, 1));
-
- // Re-throw exception
- throw new WrappedException(ex);
- }
- }
- }
-
- /**
- * Unloads given {@link Resource resource} and removes it from underlying {@link ResourceSet resourceSet}.
- *
- * @param resource
- * The resource to be unloaded.
- */
- public static void unloadResource(Resource resource) {
- unloadResource(resource, false);
- }
-
- /**
- * Unloads given {@link Resource resource} and removes it from underlying {@link ResourceSet resourceSet}.
- *
- * @param resource
- * The resource to be unloaded.
- * @param memoryOptimized
- * Will activate the memory optimization option for unloading the resource. This is only available if the
- * resource is an XMLResource.
- */
- public static void unloadResource(Resource resource, boolean memoryOptimized) {
- if (resource != null) {
- try {
- // Perform resource unload, either memory optimized or normally
- if (memoryOptimized) {
- ExtendedResource extendedResource = ExtendedResourceAdapterFactory.INSTANCE.adapt(resource);
- if (extendedResource != null) {
- Map<Object, Object> defaultLoadOptions = extendedResource.getDefaultLoadOptions();
- defaultLoadOptions.put(ExtendedResource.OPTION_UNLOAD_MEMORY_OPTIMIZED, Boolean.TRUE);
- }
- }
- resource.unload();
- } catch (Exception ex) {
- throw new WrappedException(ex);
- } finally {
- // Remove resource from ResourceSet
- ResourceSet resourceSet = resource.getResourceSet();
- if (resourceSet != null) {
- resourceSet.getResources().remove(resource);
- }
- }
- }
- }
-
- /**
- * Unloads the resource with the specified URI from the given resource set.
- *
- * @param resourceSet
- * A resource set from which the model's resource should be unloaded.
- * @param uri
- * The URI of the resource to unload.
- */
- public static void unloadResource(ResourceSet resourceSet, URI uri) {
- unloadResource(resourceSet, uri, false);
- }
-
- /**
- * Unloads the resource with the specified URI from the given resource set.
- * <p>
- * It is recommended to call this method inside a write-transaction (see
- * {@link EcorePlatformUtil#unloadFile(ResourceSet, IPath)}).
- *
- * @param resourceSet
- * A resource set from which the model's resource should be unloaded.
- * @param uri
- * The URI of the resource to unload.
- * @param memoryOptimized
- * Will activate the memory optimization option for unloading the resource. This is only available if the
- * resource is an XMLResource.
- */
- public static void unloadResource(ResourceSet resourceSet, URI uri, boolean memoryOptimized) {
- if (resourceSet != null && uri != null) {
- // Get resource and unload it
- Resource resource = resourceSet.getResource(uri, false);
- unloadResource(resource, memoryOptimized);
- }
- }
-
- /**
- * Parses {@link Resource resource} with given {@link URI uri} and validates it against XSD schema with specified
- * {@link URL url}. Raises an exception if the {@link Resource resource}'s content is not compliant with respect to
- * XSD schema.
- *
- * @param uri
- * The {@link URI uri} of the {@link Resource resource} to be validated.
- * @param schemaURL
- * The {@link URL url} of the XSD schema to be used for validation.
- * @throws SAXException
- * @throws IOException
- */
- public static void validate(URI uri, URL schemaURL) throws SAXException, IOException {
- Assert.isNotNull(uri);
-
- // 1. Lookup a factory for the W3C XML Schema language
- SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
-
- // 2. Compile the schema
- Schema schema = factory.newSchema(schemaURL);
-
- // 3. Get a validator from the schema
- Validator validator = schema.newValidator();
-
- // 4. Load the resource and validate it
- InputStream stream = null;
- try {
- stream = URIConverter.INSTANCE.createInputStream(uri);
- Source source = new StreamSource(stream);
- validator.validate(source);
- } finally {
- ExtendedPlatform.safeClose(stream);
- }
- }
-
- /**
- * Loads from resource set the resource specified by the given URI, or try to load the URI if not in resource set.
- *
- * @param resourceSet
- * The resource set into which model resource must be loaded.
- * @param uri
- * The URI to resolve; <em>i.e.</em> the URI of the model resource to load.
- * @param options
- * The loading options.
- * @param loadOnDemand
- * If <code>true</code>, creates and loads the resource if it does not already exist.
- * @return The resource resolved by the specified URI, or <code>null</code> if there is not one and it is not being
- * demand loaded.
- */
- private static Resource loadModelResource(ResourceSet resourceSet, URI uri, Map<?, ?> options, boolean loadOnDemand) {
- Assert.isNotNull(uri);
-
- // Create new ResourceSet if none has been provided
- if (resourceSet == null) {
- resourceSet = new ScopingResourceSetImpl();
- }
-
- // Try to convert given URI to platform:/resource URI if not yet so
- /*
- * !! Important Note !! This is necessary in order to avoid that resources which are located inside the
- * workspace get loaded multiple times just because they are referenced by URIs with different schemes. If given
- * resource set were an instance of ResourceSetImpl this extra conversion wouldn't be necessary.
- * org.eclipse.emf.ecore.resource.ResourceSet.getResource(URI, boolean) normalizes and compares given URI and to
- * normalized copies of URIs of already present resources and thereby avoids multiple loading of same resources
- * on its own. This is however not true when ExtendedResourceSetImpl or a subclass of it is used. Herein, URI
- * normalization and comparison has been removed from
- * org.eclipse.sphinx.emf.resource.ExtendedResourceSetImpl.getResource(URI, boolean) in order to increase
- * runtime performance.
- */
- if (!uri.isPlatform()) {
- uri = convertToPlatformResourceURI(uri);
- }
-
- // Just get model resource if it is already loaded
- Resource resource = resourceSet.getResource(uri, false);
-
- // Load it using specified options if not done so yet and a demand load has been requested
- if ((resource == null || !resource.isLoaded()) && loadOnDemand) {
- if (exists(uri)) {
- if (resource == null) {
- String contentType = getContentTypeId(uri);
- resource = resourceSet.createResource(uri, contentType);
- }
- if (resource != null) {
- try {
- // Capture errors and warnings encountered during resource creation
- /*
- * !! Important note !! This is necessary because the resource's errors and warnings are
- * automatically cleared when the loading begins. Therefore, if we don't retrieve them at this
- * point all previously encountered errors and warnings would be lost (see
- * org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(InputStream, Map<?, ?>) for details)
- */
- List<Diagnostic> creationErrors = new ArrayList<Diagnostic>(resource.getErrors());
- List<Diagnostic> creationWarnings = new ArrayList<Diagnostic>(resource.getWarnings());
-
- // Load resource
- resource.load(options);
-
- // Make sure that no empty resources are kept in resource set
- if (resource.getContents().isEmpty()) {
- unloadResource(resource, true);
- }
-
- // Restore creation time errors and warnings
- resource.getErrors().addAll(creationErrors);
- resource.getWarnings().addAll(creationWarnings);
- } catch (IOException ex) {
- // Capture errors and warnings encountered during resource load attempt
- /*
- * !! Important note !! This is necessary because the resource's errors and warnings are
- * automatically cleared when it gets unloaded. Therefore, if we don't retrieve them at this
- * point all encountered errors and warnings encountered during loading would be lost (see
- * org.eclipse.emf.ecore.resource.impl.ResourceImpl.doUnload() for details)
- */
- List<Diagnostic> loadErrors = new ArrayList<Diagnostic>(resource.getErrors());
- List<Diagnostic> loadWarnings = new ArrayList<Diagnostic>(resource.getWarnings());
-
- // Make sure that resource gets unloaded and removed from resource set again
- try {
- unloadResource(resource, true);
- } catch (Exception e) {
- // Log unload problem in Error Log but don't let it go along as runtime exception. It is
- // most likely just a consequence of the load problems encountered before and therefore
- // should not prevent those from being restored as errors and warnings on resource.
- PlatformLogUtil.logAsError(Activator.getPlugin(), e);
- }
-
- // Restore load time errors and warnings on resource
- resource.getErrors().addAll(loadErrors);
- resource.getWarnings().addAll(loadWarnings);
-
- // Re-throw exception
- throw new WrappedException(ex);
- }
- }
- }
- }
- return resource;
- }
-
- /**
- * Loads the model from the resource specified by the given URI and returns its root element.
- *
- * @param resourceSet
- * The resource set into which model resource must be loaded.
- * @param uri
- * The URI to resolve; <em>i.e.</em> the URI of the model resource to load.
- * @param options
- * The load options. If <code>null</code>, default loading options are used.
- * @param loadOnDemand
- * If <code>true</code>, creates and loads the resource if it does not already exist.
- * @return The root of the loaded model; can be <code>null</code> if resource is not loaded resource set.
- */
- private static EObject loadModelRoot(ResourceSet resourceSet, URI uri, Map<?, ?> options, boolean loadOnDemand) {
- // Use default loading options when no specified
- if (options == null) {
- options = getDefaultLoadOptions();
- }
-
- // Load resource from resource set
- Resource resource = loadModelResource(resourceSet, uri, options, loadOnDemand);
-
- // Obtain and return resource content
- return getModelRoot(resource);
- }
-}
diff --git a/sandbox/PapyrusEMFCompareV1/deprecated/org.eclipse.papyrus.emf.compare.transactional.editor/src/org/eclipse/papyrus/emf/compare/transactional/editor/sphinx/WorkspaceEditingDomainUtil.java b/sandbox/PapyrusEMFCompareV1/deprecated/org.eclipse.papyrus.emf.compare.transactional.editor/src/org/eclipse/papyrus/emf/compare/transactional/editor/sphinx/WorkspaceEditingDomainUtil.java
deleted file mode 100644
index 0cdfaa2404d..00000000000
--- a/sandbox/PapyrusEMFCompareV1/deprecated/org.eclipse.papyrus.emf.compare.transactional.editor/src/org/eclipse/papyrus/emf/compare/transactional/editor/sphinx/WorkspaceEditingDomainUtil.java
+++ /dev/null
@@ -1,321 +0,0 @@
-/**
- * <copyright>
- *
- * Copyright (c) 2008-2010 See4sys, BMW Car IT and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * See4sys - Initial API and implementation
- * BMW Car IT - Added/Updated javadoc
- *
- * </copyright>
- */
-package org.eclipse.papyrus.emf.compare.transactional.editor.sphinx;
-
-import java.util.Collection;
-import java.util.Collections;
-
-import org.eclipse.core.resources.IContainer;
-import org.eclipse.core.resources.IFile;
-import org.eclipse.core.resources.IFolder;
-import org.eclipse.core.resources.IProject;
-import org.eclipse.core.resources.IWorkspaceRoot;
-import org.eclipse.core.resources.ResourcesPlugin;
-import org.eclipse.core.runtime.Platform;
-import org.eclipse.emf.common.util.URI;
-import org.eclipse.emf.ecore.EObject;
-import org.eclipse.emf.ecore.resource.Resource;
-import org.eclipse.emf.ecore.resource.ResourceSet;
-import org.eclipse.emf.ecore.util.FeatureMap;
-import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
-import org.eclipse.emf.edit.domain.EditingDomain;
-import org.eclipse.emf.edit.domain.IEditingDomainProvider;
-import org.eclipse.emf.edit.provider.IWrapperItemProvider;
-import org.eclipse.emf.transaction.Transaction;
-import org.eclipse.emf.transaction.TransactionalEditingDomain;
-import org.eclipse.emf.transaction.util.TransactionUtil;
-import org.eclipse.sphinx.emf.domain.IContainerEditingDomainProvider;
-import org.eclipse.sphinx.emf.edit.TransientItemProvider;
-import org.eclipse.sphinx.emf.metamodel.IMetaModelDescriptor;
-import org.eclipse.sphinx.emf.model.IModelDescriptor;
-
-/**
- * This utility class is intended to retrieve an {@linkplain EditingDomain} in various ways.
- */
-public final class WorkspaceEditingDomainUtil {
-
- // Prevent from instantiation
- private WorkspaceEditingDomainUtil() {
- }
-
- /**
- * Returns the {@link TransactionalEditingDomain editing domain} which currently contains the given {@link IFile
- * file} in its {@link ResourceSet resource set}.
- *
- * @param file
- * The {@link IFile file} whose current {@link TransactionalEditingDomain editing domain} is to be
- * retrieved.
- * @return The {@link TransactionalEditingDomain editing domain} which contains the given {@link IFile file}, or
- * <code>null</code> if given {@link IFile file} isn't contained in any {@link TransactionalEditingDomain
- * editing domain}.
- */
- public static TransactionalEditingDomain getCurrentEditingDomain(IFile file) {
- for (TransactionalEditingDomain editingDomain : getEditingDomains(ResourcesPlugin.getWorkspace().getRoot())) {
- if (EcorePlatformUtil.isFileLoaded(editingDomain, file)) {
- return editingDomain;
- }
- }
- return null;
- }
-
- /**
- * Returns the {@link TransactionalEditingDomain editing domain} to which the given {@link IFile file} is mapped.
- * The mapped {@link TransactionalEditingDomain editing domain} is the {@link TransactionalEditingDomain editing
- * domain} which is used as target when loading the {@link IFile file} from the workspace.
- * <p>
- * Even though the mapped {@link TransactionalEditingDomain editing domain} represents the primary
- * {@link TransactionalEditingDomain editing domain} for the given {@link IFile file} it still may happen that the
- * {@link IFile file} gets loaded in another or "foreign" {@link TransactionalEditingDomain editing domain}. Typical
- * reasons for this are e.g. lazy loading of one file from multiple other files which are in different
- * {@link TransactionalEditingDomain editing domain}s or programatic action by some application.
- * <p>
- *
- * @param file
- * The {@link IFile file} whose mapped {@link TransactionalEditingDomain editing domain} is to be
- * retrieved.
- * @return The {@link TransactionalEditingDomain editing domain} to which the given {@link IFile file} is mapped, or
- * <code>null</code> if given {@link IFile file} isn't mapped to any {@link TransactionalEditingDomain
- * editing domain}.
- */
- public static TransactionalEditingDomain getMappedEditingDomain(IFile file) {
- if (file != null) {
- IEditingDomainProvider provider = (IEditingDomainProvider) Platform.getAdapterManager().loadAdapter(file,
- IEditingDomainProvider.class.getName());
- if (provider != null) {
- EditingDomain editingDomain = provider.getEditingDomain();
- if (editingDomain instanceof TransactionalEditingDomain) {
- return (TransactionalEditingDomain) editingDomain;
- }
- }
- }
- return null;
- }
-
- /**
- * Retrieves the {@link TransactionalEditingDomain editing domain} which the given {@link Object object} belongs to.
- * The following types of {@link Object object}s are supported:
- * <p>
- * <ul>
- * <li>{@linkplain EObject}</li>
- * <li>{@linkplain FeatureMap.Entry}</li>
- * <li>{@linkplain IEditingDomainProvider}</li>
- * <li>{@linkplain IFile}</li>
- * <li>{@linkplain IModelDescriptor}</li>
- * <li>{@linkplain IWrapperProviderItem}</li>
- * <li>{@linkplain Resource}</li>
- * <li>{@linkplain ResourceSet}</li>
- * <li>{@linkplain Transaction}</li>
- * <li>{@linkplain TransactionalEditingDomain}</li>
- * <li>{@linkplain URI}</li>
- * </ul>
- * <p>
- * Note that it is not possible to obtain a single {@link TransactionalEditingDomain editing domain} from a
- * {@linkplain IContainer}. You can however retrieve the collection of {@link TransactionalEditingDomain editing
- * domain}s available on a {@linkplain IContainer} by calling {@linkplain #getEditingDomains(IContainer)}.
- *
- * @param object
- * The object whose corresponding {@link TransactionalEditingDomain editing domain} must be returned.
- * @return The EditingDomain the <code>object</code> corresponds with or <code>null</code> if the
- * <code>object</code> cannot be mapped to an {@link TransactionalEditingDomain editing domain}.
- * @see org.eclipse.sphinx.emf.workspace.domain.WorkspaceEditingDomainManager#getEditingDomainMapping()
- * @see #getEditingDomains(IContainer)
- */
- public static TransactionalEditingDomain getEditingDomain(Object object) {
- if (object instanceof IModelDescriptor) {
- return getEditingDomain((IModelDescriptor) object);
- } else if (object instanceof IFile) {
- return getEditingDomain((IFile) object);
- } else if (object instanceof URI) {
- return getEditingDomain((URI) object);
- } else if (object instanceof Resource) {
- // If resource is not yet or not anymore in a resource set, then try to figure out which editing domain it
- // would belong to if it were in a resource set
- Resource resource = (Resource) object;
- if (resource.getResourceSet() == null) {
- return getEditingDomain(EcorePlatformUtil.getFile(resource));
- }
- } else if (object instanceof IWrapperItemProvider) {
- return getEditingDomain((IWrapperItemProvider) object);
- } else if (object instanceof FeatureMap.Entry) {
- return getEditingDomain((FeatureMap.Entry) object);
- } else if (object instanceof TransientItemProvider) {
- return getEditingDomain((TransientItemProvider) object);
- }
-
- return object != null ? TransactionUtil.getEditingDomain(object) : null;
- }
-
- /**
- * Retrieves the {@link TransactionalEditingDomain editing domain} behind given {@link IModelDescriptor model
- * descriptor}.
- *
- * @param modelDescriptor
- * The {@link IModelDescriptor model descriptor} whose {@link TransactionalEditingDomain editing domain}
- * is to be retrieved.
- * @return The {@link TransactionalEditingDomain editing domain} behind given {@link IModelDescriptor model
- * descriptor} or <code>null</code> if no such could be retrieved.
- */
- public static TransactionalEditingDomain getEditingDomain(IModelDescriptor modelDescriptor) {
- if (modelDescriptor != null) {
- return modelDescriptor.getEditingDomain();
- }
- return null;
- }
-
- /**
- * Retrieves the {@link TransactionalEditingDomain editing domain} corresponding to the given {@link IFile file}.
- * Tries at first to determine the {@link TransactionalEditingDomain editing domain} which currently contains the
- * given {@link IFile file} in its {@link ResourceSet resource set} and returns it if successful. Otherwise, the
- * {@link TransactionalEditingDomain editing domain} to which the given {@link IFile file} is mapped is returned.
- *
- * @param file
- * The {@link IFile file} whose {@link TransactionalEditingDomain is to be retrieved}.
- * @return The {@link TransactionalEditingDomain editing domain} the given file belongs to, or <code>null</code> if
- * given {@link IFile file} doesn't belong to any {@link TransactionalEditingDomain editing domain}.
- * @see #getCurrentEditingDomain(IFile)
- * @see #getMappedEditingDomain(IFile)
- */
- public static TransactionalEditingDomain getEditingDomain(IFile file) {
- TransactionalEditingDomain editingDomain = getCurrentEditingDomain(file);
- if (editingDomain != null) {
- return editingDomain;
- }
- return getMappedEditingDomain(file);
- }
-
- /**
- * Retrieves the EditingDomain by a URI. Consults the active mapping to map a resource to its
- * {@link TransactionalEditingDomain editing domain}.
- *
- * @param uri
- * An URI (file, EMF Object's URI)
- * @return The {@link TransactionalEditingDomain editing domain} associated with the given URI. Returns
- * <code>null</code> if the URI cannot be resolved to workspace file or no
- * {@link TransactionalEditingDomain editing domain} can be mapped through the active mapping.
- */
- public static TransactionalEditingDomain getEditingDomain(URI uri) {
- IFile file = EcorePlatformUtil.getFile(uri);
- return getEditingDomain(file);
- }
-
- /**
- * Retrieves the {@link TransactionalEditingDomain editing domain} corresponding to the object wrapped in the given
- * IWrapperItemProvider.
- *
- * @param wrapperItemProvider
- * The IWrapperItemProvider wrapping the Object for which the {@link TransactionalEditingDomain editing
- * domain} is to be retrieved.
- * @return The {@link TransactionalEditingDomain editing domain} the wrapped Object is associated with or
- * <code>null</code> if the Object cannot be mapped to an {@link TransactionalEditingDomain editing domain}.
- */
- public static TransactionalEditingDomain getEditingDomain(IWrapperItemProvider wrapperItemProvider) {
- if (wrapperItemProvider != null) {
- Object unwrapped = AdapterFactoryEditingDomain.unwrap(wrapperItemProvider);
- TransactionalEditingDomain editingDomain = getEditingDomain(unwrapped);
- if (editingDomain != null) {
- return editingDomain;
- }
- return getEditingDomain(wrapperItemProvider.getOwner());
- }
- return null;
- }
-
- /**
- * Returns the {@link TransactionalEditingDomain editing domain} for the value Object of the given FeatureMap.Entry.
- *
- * @param entry
- * A FeatureMap.Entry holding the Object for which the {@link TransactionalEditingDomain editing domain}
- * is to be retrieved.
- * @return The EditingDomain the Object held by the given <code>entry</entry> corresponds with or <code>null</code>
- * if the Object cannot be mapped to an {@link TransactionalEditingDomain editing domain}.
- */
- public static TransactionalEditingDomain getEditingDomain(FeatureMap.Entry entry) {
- Object unwrapped = AdapterFactoryEditingDomain.unwrap(entry);
- return getEditingDomain(unwrapped);
- }
-
- /**
- * Returns the {@link TransactionalEditingDomain editing domain} for the value Object of the given
- * ItemProviderAdapter.
- *
- * @param provider
- * An ItemProviderAdapter holding the Object for which the {@link TransactionalEditingDomain editing
- * domain} is to be retrieved.
- * @return The EditingDomain the Target held by the given
- * <code>provider</entry> corresponds with or <code>null</code> if the Object cannot be mapped to an
- * {@link TransactionalEditingDomain editing domain}.
- */
- public static TransactionalEditingDomain getEditingDomain(TransientItemProvider provider) {
- Object target = provider.getTarget();
- return getEditingDomain(target);
- }
-
- /**
- * Retrieves the {@link TransactionalEditingDomain editing domain} corresponding to the specified meta-model
- * descriptor for the given context container.
- *
- * @param container
- * The container to consider as context object in order to retrieve the right
- * {@link TransactionalEditingDomain editing domain}.
- * @param mmDescriptor
- * A meta-model descriptor.
- * @return The {@link TransactionalEditingDomain editing domain} for the specified meta-model descriptor and the
- * specified context container or <code>null</code> if it cannot be mapped to any
- * {@link TransactionalEditingDomain editing domain}.
- */
- public static TransactionalEditingDomain getEditingDomain(IContainer container, IMetaModelDescriptor mmDescriptor) {
- if (container != null) {
- IContainerEditingDomainProvider provider = (IContainerEditingDomainProvider) Platform.getAdapterManager().loadAdapter(container,
- IContainerEditingDomainProvider.class.getName());
- if (provider != null) {
- return provider.getEditingDomain(mmDescriptor);
- }
- }
- return null;
- }
-
- /**
- * Returns all {@link TransactionalEditingDomain editing domain}s in the given {@link IContainer container} (i.e.
- * {@link IWorkspaceRoot workspace root}, {@link IProject project} or {@link IFolder folder}).
- *
- * @return The collection of {@link TransactionalEditingDomain editing domain}s in given {@link IContainer
- * container}.
- */
- public static Collection<TransactionalEditingDomain> getEditingDomains(IContainer container) {
- IContainerEditingDomainProvider provider = (IContainerEditingDomainProvider) Platform.getAdapterManager().loadAdapter(container,
- IContainerEditingDomainProvider.class.getName());
- if (provider != null) {
- return provider.getEditingDomains();
- }
- return Collections.emptyList();
- }
-
- /**
- * Returns all {@link TransactionalEditingDomain editing domain}s that exist in the workspace.
- * <p>
- * This is a convenience method, fully equivalent to:
- *
- * <pre>
- * WorkspaceEditingDomainUtil.getEditingDomains(ResourcesPlugin.getWorkspace().getRoot());
- * </pre>
- *
- * </p>
- * * @return The collection of {@link TransactionalEditingDomain editing domain}s in the workspace.
- */
- public static Collection<TransactionalEditingDomain> getAllEditingDomains() {
- return getEditingDomains(ResourcesPlugin.getWorkspace().getRoot());
- }
-}
diff --git a/sandbox/PapyrusEMFCompareV1/deprecated/org.eclipse.papyrus.emf.compare.transactional.editor/src/org/eclipse/papyrus/emf/compare/transactional/editor/sphinx/WorkspaceTransactionUtil.java b/sandbox/PapyrusEMFCompareV1/deprecated/org.eclipse.papyrus.emf.compare.transactional.editor/src/org/eclipse/papyrus/emf/compare/transactional/editor/sphinx/WorkspaceTransactionUtil.java
deleted file mode 100644
index df0d6dba57e..00000000000
--- a/sandbox/PapyrusEMFCompareV1/deprecated/org.eclipse.papyrus.emf.compare.transactional.editor/src/org/eclipse/papyrus/emf/compare/transactional/editor/sphinx/WorkspaceTransactionUtil.java
+++ /dev/null
@@ -1,198 +0,0 @@
-/**
- * <copyright>
- *
- * Copyright (c) 2008-2010 See4sys, BMW Car IT and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * See4sys - Initial API and implementation
- * BMW Car IT - Added/Updated javadoc
- *
- * </copyright>
- */
-package org.eclipse.papyrus.emf.compare.transactional.editor.sphinx;
-
-import java.util.HashMap;
-import java.util.Map;
-
-import org.eclipse.core.commands.ExecutionException;
-import org.eclipse.core.commands.operations.IOperationHistory;
-import org.eclipse.core.commands.operations.IUndoContext;
-import org.eclipse.core.commands.operations.IUndoableOperation;
-import org.eclipse.core.commands.operations.OperationHistoryFactory;
-import org.eclipse.core.resources.ResourcesPlugin;
-import org.eclipse.core.runtime.Assert;
-import org.eclipse.core.runtime.IAdaptable;
-import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.IStatus;
-import org.eclipse.core.runtime.OperationCanceledException;
-import org.eclipse.core.runtime.Status;
-import org.eclipse.emf.common.command.CommandStack;
-import org.eclipse.emf.transaction.Transaction;
-import org.eclipse.emf.transaction.TransactionalEditingDomain;
-import org.eclipse.emf.transaction.impl.TransactionImpl;
-import org.eclipse.emf.workspace.AbstractEMFOperation;
-import org.eclipse.emf.workspace.IWorkspaceCommandStack;
-import org.eclipse.emf.workspace.impl.WorkspaceCommandStackImpl;
-import org.eclipse.osgi.util.NLS;
-
-/**
- * An utility class providing helper methods for handling and executing transactions on an EMF model.
- */
-public final class WorkspaceTransactionUtil {
-
- // Prevent from instantiation
- private WorkspaceTransactionUtil() {
- }
-
- /**
- * Returns the IOperationHistory for the given EditingDomain.
- *
- * @param editingDomain
- * The EditingDomain for which the IOperationHistory is to be retrieved.
- * @return The IOperationHistory of the given <code>edingDomain</code>.
- */
- public static IOperationHistory getOperationHistory(TransactionalEditingDomain editingDomain) {
- if (editingDomain != null) {
- CommandStack commandStack = editingDomain.getCommandStack();
- if (commandStack instanceof IWorkspaceCommandStack) {
- return ((IWorkspaceCommandStack) commandStack).getOperationHistory();
- }
- }
- return OperationHistoryFactory.getOperationHistory();
- }
-
- /**
- * Returns the IUndoContext for the given EditingDomain.
- *
- * @param editingDomain
- * The EditingDomain for which the IUndoContext is to be retrieved.
- * @return The IUndoContext of the given <code>edingDomain</code>.
- */
- public static IUndoContext getUndoContext(TransactionalEditingDomain editingDomain) {
- if (editingDomain != null) {
- CommandStack commandStack = editingDomain.getCommandStack();
- if (commandStack instanceof WorkspaceCommandStackImpl) {
- return IOperationHistory.GLOBAL_UNDO_CONTEXT;
- }
- }
- return (IUndoContext) ResourcesPlugin.getWorkspace().getAdapter(IUndoContext.class);
- }
-
- /**
- * Returns a default set of options which can be used for executing an operation within a transaction.
- *
- * @return The default options for executing an operation in a transaction.
- */
- public static Map<String, Object> getDefaultTransactionOptions() {
- Map<String, Object> options = new HashMap<String, Object>();
- options.put(TransactionImpl.BLOCK_CHANGE_PROPAGATION, Boolean.TRUE);
- options.put(Transaction.OPTION_NO_VALIDATION, Boolean.TRUE);
- return options;
- }
-
- /**
- * Returns a default set of options which can be used for executing an operation saving a new model within a write
- * transaction.
- *
- * @return The default options for executing a save new operation in a transaction.
- */
- public static Map<String, Object> getDefaultSaveNewTransactionOptions() {
- Map<String, Object> options = WorkspaceTransactionUtil.getDefaultTransactionOptions();
- options.put(Transaction.OPTION_NO_UNDO, Boolean.TRUE);
- return options;
- }
-
- /**
- * Returns a default set of options which can be used for executing an operation saving an existing model within a
- * write transaction.
- *
- * @return The default options for executing a save operation in a transaction.
- */
- public static Map<String, Object> getDefaultSaveTransactionOptions() {
- Map<String, Object> options = WorkspaceTransactionUtil.getDefaultTransactionOptions();
- options.put(Transaction.OPTION_NO_UNDO, Boolean.TRUE);
- options.put(Transaction.OPTION_NO_NOTIFICATIONS, Boolean.TRUE);
- options.put(Transaction.OPTION_NO_TRIGGERS, Boolean.TRUE);
- return options;
- }
-
- /**
- * Executes a write operation in a write transaction .
- *
- * @param editingDomain
- * The Transactional Editing domain receiving the transaction .
- * @param runnable
- * The runnable defining the run method .
- * @param operationLabel
- * The label of the operation to execute .
- * @throws OperationCanceledException
- * Thrown when the transaction is cancelled by the user.
- * @throws ExecutionException
- * Thrown when the transaction could not be completed to an Exception.
- **/
- public static void executeInWriteTransaction(TransactionalEditingDomain editingDomain, Runnable runnable, String operationLabel)
- throws OperationCanceledException, ExecutionException {
- executeInWriteTransaction(editingDomain, runnable, operationLabel, getOperationHistory(editingDomain), getDefaultTransactionOptions(), null);
- }
-
- /**
- * Execute a write operation in a write transaction.
- *
- * @param editingDomain
- * The {@linkplain TransactionalEditingDomain editing domain} receiving the transaction.
- * @param runnable
- * The {@linkplain Runnable runnable} defining the run method.
- * @param operationLabel
- * The label of the operation to execute.
- * @param operationHistory
- * The {@linkplain IOperationHistory operation history} to store the executed operation.
- * @param transactionOptions
- * The options to set the transaction.
- * @param monitor
- * The {@linkplain IProgressMonitor progress monitor} to use during operation execution.
- * @throws OperationCanceledException
- * Thrown when the transaction is canceled by the user.
- * @throws ExecutionException
- * Thrown when the transaction could not be completed to an Exception.
- * @since 0.7.0
- **/
- public static void executeInWriteTransaction(TransactionalEditingDomain editingDomain, final Runnable runnable, final String operationLabel,
- IOperationHistory operationHistory, final Map<String, Object> transactionOptions, IProgressMonitor monitor)
- throws OperationCanceledException, ExecutionException {
- Assert.isNotNull(editingDomain);
- Assert.isNotNull(runnable);
-
- String safeLabel = operationLabel == null ? "Unnamed operation" : operationLabel; //$NON-NLS-1$
- IUndoableOperation operation = new AbstractEMFOperation(editingDomain, safeLabel, transactionOptions) {
- @Override
- protected IStatus doExecute(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
- try {
- runnable.run();
- return Status.OK_STATUS;
- } catch (RuntimeException ex) {
- if (ex instanceof OperationCanceledException) {
- throw (OperationCanceledException) ex;
- } else {
- throw new ExecutionException(NLS.bind(" {0} transaction failed", operationLabel), ex);
- //throw new ExecutionException(NLS.bind(Messages.problem_transactionFailed, operationLabel), ex);
- }
- }
- }
-
- @Override
- public boolean canUndo() {
- return transactionOptions.get(Transaction.OPTION_NO_UNDO) != Boolean.TRUE;
- }
- };
- // Perform the execution of the transaction.
- IStatus status = operationHistory.execute(operation, monitor, null);
-
- if (status.getSeverity() == IStatus.CANCEL) {
- throw new OperationCanceledException();
- }
- }
-}

Back to the top