Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBenoit Maggi2014-04-30 13:47:18 +0000
committerCamille Letavernier2014-05-13 15:48:13 +0000
commitdeaec2a96cd6f01c51c876a6160fc782cd1293b5 (patch)
tree266a23742f075c2dd23704b9cf77d15501724aae /plugins/infra/emf
parent36c7009a5ca5bc1aa0f408bf19b7e802640a714d (diff)
downloadorg.eclipse.papyrus-deaec2a96cd6f01c51c876a6160fc782cd1293b5.tar.gz
org.eclipse.papyrus-deaec2a96cd6f01c51c876a6160fc782cd1293b5.tar.xz
org.eclipse.papyrus-deaec2a96cd6f01c51c876a6160fc782cd1293b5.zip
Bug 413802 – Cannot copy & paste model using Project Explorer
Add copy/paste action for papyrus model in projectExplorer (Handler/Command don't seem to be able to override paste action) Change-Id: I5d2f5630d75c65e08fe8fb67cb3e383150591feb Signed-off-by: Benoit Maggi <benoit.maggi@cea.fr>
Diffstat (limited to 'plugins/infra/emf')
-rw-r--r--plugins/infra/emf/org.eclipse.papyrus.infra.emf/src/org/eclipse/papyrus/infra/emf/resource/DependencyManagementHelper.java95
-rw-r--r--plugins/infra/emf/org.eclipse.papyrus.infra.emf/src/org/eclipse/papyrus/infra/emf/resource/IURIReplacementStrategy.java28
-rw-r--r--plugins/infra/emf/org.eclipse.papyrus.infra.emf/src/org/eclipse/papyrus/infra/emf/resource/MoveFileURIReplacementStrategy.java144
-rw-r--r--plugins/infra/emf/org.eclipse.papyrus.infra.emf/src/org/eclipse/papyrus/infra/emf/resource/ReplacementImpl.java72
-rw-r--r--plugins/infra/emf/org.eclipse.papyrus.infra.emf/src/org/eclipse/papyrus/infra/emf/resource/RestoreDependencyHelper.java132
-rw-r--r--plugins/infra/emf/org.eclipse.papyrus.infra.emf/src/org/eclipse/papyrus/infra/emf/utils/ResourceUtils.java188
6 files changed, 503 insertions, 156 deletions
diff --git a/plugins/infra/emf/org.eclipse.papyrus.infra.emf/src/org/eclipse/papyrus/infra/emf/resource/DependencyManagementHelper.java b/plugins/infra/emf/org.eclipse.papyrus.infra.emf/src/org/eclipse/papyrus/infra/emf/resource/DependencyManagementHelper.java
index e97eb3d2477..65d9a2fe23f 100644
--- a/plugins/infra/emf/org.eclipse.papyrus.infra.emf/src/org/eclipse/papyrus/infra/emf/resource/DependencyManagementHelper.java
+++ b/plugins/infra/emf/org.eclipse.papyrus.infra.emf/src/org/eclipse/papyrus/infra/emf/resource/DependencyManagementHelper.java
@@ -1,6 +1,6 @@
/*****************************************************************************
* Copyright (c) 2013 CEA LIST.
- *
+ *
* 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
@@ -35,21 +35,21 @@ import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
/**
* A Helper to edit dependencies (URIs) between EMF Resources
- *
+ *
* It replaces all references from a Resource A to a Resource B with references from A to B'
- *
+ *
* B and B' must be "equivalent" (e.g. B is a copy of B' with or without modifications).
- *
+ *
* @author Camille Letavernier
- *
+ *
*/
public class DependencyManagementHelper {
/**
* Replaces all references (dependencies) in the "fromResources" to the "uriToReplace" URI, with the "targetURI"
- *
+ *
* Simplified pseudo code algorithm: fromResources.replaceAll(uriToReplace, targetURI)
- *
+ *
* @param uriToReplace
* The URI of the resource which initially contains the referenced elements. May or may not exist.
* Must not be null
@@ -62,7 +62,7 @@ public class DependencyManagementHelper {
* Must not be empty
* @param editingDomain
* The editing domain. May be null.
- *
+ *
* @return
* The collection of replacements
*/
@@ -90,9 +90,9 @@ public class DependencyManagementHelper {
/**
* Replaces all references (dependencies) in the "fromResources" to the "uriToReplace" URI, with the "targetURI"
- *
+ *
* Simplified pseudo code algorithm: fromResources.replaceAll(uriToReplace, targetURI)
- *
+ *
* @param uriToReplace
* The URI of the resource which initially contains the referenced elements. May or may not exist.
* Must not be null
@@ -197,7 +197,7 @@ public class DependencyManagementHelper {
/**
* Replaces the EObject (Which may be a proxy) by its equivalent in the given Resource's URI.
* Returns null if the "currentValueToReplace" doesn't belong to the resource represented by "uriToReplace".
- *
+ *
* @param currentValueToReplace
* The current value, to be replaced. May be a proxy
* @param uriToReplace
@@ -221,7 +221,7 @@ public class DependencyManagementHelper {
/**
* Replaces the EObject (Which may be a proxy) by its equivalent in the given Resource's URI.
- *
+ *
* @param currentValueToReplace
* The current value, to be replaced. May be a proxy
* @param targetURI
@@ -255,9 +255,9 @@ public class DependencyManagementHelper {
/**
* Replaces all references (dependencies) in the "resourceSet" to the "uriToReplace" URI, with the "targetURI"
- *
+ *
* Simplified pseudo code algorithm: resourceSet.getResources().replaceAll(uriToReplace, targetURI)
- *
+ *
* @param uriToReplace
* The URI of the resource which initially contains the referenced elements. May or may not exist.
* Must not be null
@@ -283,9 +283,9 @@ public class DependencyManagementHelper {
/**
* Replaces all references (dependencies) in the "fromResources" to the "uriToReplace" URI, with the "targetURI"
- *
+ *
* Simplified pseudo code algorithm: fromResources.replaceAll(uriToReplace, targetURI)
- *
+ *
* @param uriToReplace
* The URI of the resource which initially contains the referenced elements. May or may not exist.
* Must not be null
@@ -305,9 +305,9 @@ public class DependencyManagementHelper {
/**
* Replaces all references (dependencies) in the "fromResource" to the "uriToReplace" URI, with the "targetURI"
- *
+ *
* Simplified pseudo code algorithm: fromResource.replaceAll(uriToReplace, targetURI)
- *
+ *
* @param uriToReplace
* The URI of the resource which initially contains the referenced elements. May or may not exist.
* Must not be null
@@ -327,9 +327,9 @@ public class DependencyManagementHelper {
/**
* Replaces all references (dependencies) in the "resourceSet" to the "uriToReplace" URI, with the "targetURI"
- *
+ *
* Simplified pseudo code algorithm: resourceSet.getResources().replaceAll(uriToReplace, targetURI)
- *
+ *
* @param uriToReplace
* The URI of the resource which initially contains the referenced elements. May or may not exist.
* Must not be null
@@ -347,59 +347,4 @@ public class DependencyManagementHelper {
return updateDependencies(uriToReplace, targetURI, resourcesToEdit, null);
}
- private static class ReplacementImpl implements Replacement {
-
- private EObject parent;
-
- private EStructuralFeature property;
-
- private EObject oldValue;
-
- private EObject newValue;
-
- public ReplacementImpl(EObject parent, EStructuralFeature property, EObject oldValue, EObject newValue) {
- this.parent = parent;
- this.property = property;
- this.oldValue = oldValue;
- this.newValue = newValue;
- }
-
- public EObject getEObject() {
- return parent;
- }
-
- public EStructuralFeature getEStructuralFeature() {
- return property;
- }
-
- public EObject get(boolean resolve) {
- if(resolve && newValue != null && newValue.eIsProxy()) {
- newValue = EcoreUtil.resolve(newValue, parent);
- }
-
- return newValue;
- }
-
- public void set(Object newValue) {
- throw new UnsupportedOperationException();
- }
-
- public boolean isSet() {
- return newValue != null;
- }
-
- public void unset() {
- throw new UnsupportedOperationException();
- }
-
- public EObject getOldValue() {
- return oldValue;
- }
-
- @Override
- public String toString() {
- return String.format("%s replaced with %s", EcoreUtil.getURI(oldValue), EcoreUtil.getURI(newValue));
- }
- }
-
}
diff --git a/plugins/infra/emf/org.eclipse.papyrus.infra.emf/src/org/eclipse/papyrus/infra/emf/resource/IURIReplacementStrategy.java b/plugins/infra/emf/org.eclipse.papyrus.infra.emf/src/org/eclipse/papyrus/infra/emf/resource/IURIReplacementStrategy.java
new file mode 100644
index 00000000000..65e3d46d423
--- /dev/null
+++ b/plugins/infra/emf/org.eclipse.papyrus.infra.emf/src/org/eclipse/papyrus/infra/emf/resource/IURIReplacementStrategy.java
@@ -0,0 +1,28 @@
+/*****************************************************************************
+ * Copyright (c) 2014 CEA LIST.
+ *
+ *
+ * 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:
+ * Benoit Maggi benoit.maggi@cea.fr - Initial API and implementation
+ *
+ *****************************************************************************/
+
+package org.eclipse.papyrus.infra.emf.resource;
+
+import org.eclipse.emf.common.util.URI;
+
+
+/**
+ * Interface for different strategies to change URI of a Resource
+ *
+ */
+public interface IURIReplacementStrategy {
+
+ URI getReplacementCandidate(URI resourceURI);
+
+}
diff --git a/plugins/infra/emf/org.eclipse.papyrus.infra.emf/src/org/eclipse/papyrus/infra/emf/resource/MoveFileURIReplacementStrategy.java b/plugins/infra/emf/org.eclipse.papyrus.infra.emf/src/org/eclipse/papyrus/infra/emf/resource/MoveFileURIReplacementStrategy.java
new file mode 100644
index 00000000000..81f59eb4a81
--- /dev/null
+++ b/plugins/infra/emf/org.eclipse.papyrus.infra.emf/src/org/eclipse/papyrus/infra/emf/resource/MoveFileURIReplacementStrategy.java
@@ -0,0 +1,144 @@
+/*****************************************************************************
+ * Copyright (c) 2014 CEA LIST.
+ *
+ *
+ * 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:
+ * Benoit Maggi benoit.maggi@cea.fr - Initial API and implementation
+ *
+ *****************************************************************************/
+
+
+package org.eclipse.papyrus.infra.emf.resource;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IWorkspaceRoot;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.resource.URIConverter;
+import org.eclipse.papyrus.infra.core.resource.ModelSet;
+
+
+/**
+ * The Class MoveFileURIReplacementStrategy.
+ */
+public class MoveFileURIReplacementStrategy implements IURIReplacementStrategy {
+
+
+ /** The replacement uri mapping. */
+ protected Map<URI, URI> replacementURIMapping;
+
+ /** The source path. */
+ protected IPath sourcePath;
+
+ /** The target path. */
+ protected IPath targetPath;
+
+ /**
+ * Instantiates a new move file uri replacement strategy.
+ *
+ * @param replacementURIMapping
+ * the replacement uri mapping
+ * @param sourcePath
+ * the source path
+ * @param targetPath
+ * the target path
+ */
+ public MoveFileURIReplacementStrategy(Map<URI, URI> replacementURIMapping, IPath sourcePath, IPath targetPath) {
+ this.replacementURIMapping = replacementURIMapping;
+ this.sourcePath = sourcePath;
+ this.targetPath = targetPath;
+ }
+
+ /**
+ * Instantiates a new move file uri replacement strategy.
+ *
+ * @param sourcePath
+ * the source path
+ * @param targetPath
+ * the target path
+ */
+ public MoveFileURIReplacementStrategy(IPath sourcePath, IPath targetPath) {
+ this(new HashMap<URI, URI>(), sourcePath, targetPath);
+ }
+
+
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.papyrus.infra.emf.resource.IURIReplacementStrategy#getReplacementCandidate(org.eclipse.emf.common.util.URI)
+ */
+ public URI getReplacementCandidate(URI resourceURI) {
+ if(replacementURIMapping.containsKey(resourceURI)) {
+ return replacementURIMapping.get(resourceURI);
+ }
+ URI calculateUri = calculateNewUri(resourceURI);
+ replacementURIMapping.put(resourceURI, calculateUri);
+ return calculateUri;
+ }
+
+
+ /**
+ * Calculate uri.
+ *
+ * @param resourceURI
+ * the resource uri
+ * @return the uri
+ */
+ protected URI calculateNewUri(URI resourceURI) {
+ ModelSet modelSet = new ModelSet();
+ URIConverter uriConverter = modelSet.getURIConverter();
+ boolean exists = uriConverter.exists(resourceURI, Collections.EMPTY_MAP);
+ if(!exists) {
+ URI choseCorrectPath = trySourcePath(sourcePath, targetPath, resourceURI);
+ if(choseCorrectPath != null) {
+ return choseCorrectPath;
+ }
+ }
+ return resourceURI;
+ }
+
+
+
+
+ /**
+ * If the resource is not found in the target location then look in the source location
+ *
+ * @param sourcePath
+ * the source path
+ * @param targetPath
+ * the target path
+ * @param resourceURI
+ * the resource uri
+ * @return the uri
+ */
+ protected URI trySourcePath(IPath sourcePath, IPath targetPath, URI resourceURI) {
+ String uriPlatformString = resourceURI.toPlatformString(true);
+ if(uriPlatformString != null) {
+ Path path = new Path(uriPlatformString);
+ IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
+ IFile file = root.getFile(path);
+ if(!file.exists()) {// test file non existence from the target
+ // calculate a new path from source path
+ URI sourceURI = URI.createPlatformResourceURI(sourcePath.toString()+Path.SEPARATOR, true);
+ URI targetURI = URI.createPlatformResourceURI(targetPath.toString()+Path.SEPARATOR, true);
+ URI resourceDeresolved = resourceURI.deresolve(targetURI);
+ URI resourceResolved = resourceDeresolved.resolve(sourceURI);
+ return resourceResolved;
+ }
+ }
+ return null;
+ }
+
+}
diff --git a/plugins/infra/emf/org.eclipse.papyrus.infra.emf/src/org/eclipse/papyrus/infra/emf/resource/ReplacementImpl.java b/plugins/infra/emf/org.eclipse.papyrus.infra.emf/src/org/eclipse/papyrus/infra/emf/resource/ReplacementImpl.java
new file mode 100644
index 00000000000..6eafbfba7cc
--- /dev/null
+++ b/plugins/infra/emf/org.eclipse.papyrus.infra.emf/src/org/eclipse/papyrus/infra/emf/resource/ReplacementImpl.java
@@ -0,0 +1,72 @@
+/*****************************************************************************
+ * Copyright (c) 2013 CEA LIST.
+ *
+ * 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:
+ * Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
+ *****************************************************************************/
+package org.eclipse.papyrus.infra.emf.resource;
+
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.EStructuralFeature;
+import org.eclipse.emf.ecore.util.EcoreUtil;
+
+
+public class ReplacementImpl implements Replacement {
+
+ private EObject parent;
+
+ private EStructuralFeature property;
+
+ private EObject oldValue;
+
+ private EObject newValue;
+
+ public ReplacementImpl(EObject parent, EStructuralFeature property, EObject oldValue, EObject newValue) {
+ this.parent = parent;
+ this.property = property;
+ this.oldValue = oldValue;
+ this.newValue = newValue;
+ }
+
+ public EObject getEObject() {
+ return parent;
+ }
+
+ public EStructuralFeature getEStructuralFeature() {
+ return property;
+ }
+
+ public EObject get(boolean resolve) {
+ if(resolve && newValue != null && newValue.eIsProxy()) {
+ newValue = EcoreUtil.resolve(newValue, parent);
+ }
+
+ return newValue;
+ }
+
+ public void set(Object newValue) {
+ throw new UnsupportedOperationException();
+ }
+
+ public boolean isSet() {
+ return newValue != null;
+ }
+
+ public void unset() {
+ throw new UnsupportedOperationException();
+ }
+
+ public EObject getOldValue() {
+ return oldValue;
+ }
+
+ @Override
+ public String toString() {
+ return String.format("%s replaced with %s", EcoreUtil.getURI(oldValue), EcoreUtil.getURI(newValue)); //$NON-NLS-1$
+ }
+} \ No newline at end of file
diff --git a/plugins/infra/emf/org.eclipse.papyrus.infra.emf/src/org/eclipse/papyrus/infra/emf/resource/RestoreDependencyHelper.java b/plugins/infra/emf/org.eclipse.papyrus.infra.emf/src/org/eclipse/papyrus/infra/emf/resource/RestoreDependencyHelper.java
new file mode 100644
index 00000000000..f018ddb9c5e
--- /dev/null
+++ b/plugins/infra/emf/org.eclipse.papyrus.infra.emf/src/org/eclipse/papyrus/infra/emf/resource/RestoreDependencyHelper.java
@@ -0,0 +1,132 @@
+/*****************************************************************************
+ * Copyright (c) 2014 CEA LIST.
+ *
+ *
+ * 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:
+ * Benoit Maggi benoit.maggi@cea.fr - Initial API and implementation
+ *
+ *****************************************************************************/
+
+package org.eclipse.papyrus.infra.emf.resource;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.Map;
+
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.EReference;
+import org.eclipse.emf.ecore.EStructuralFeature;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.util.EcoreUtil;
+import org.eclipse.papyrus.infra.emf.Activator;
+
+/**
+ * The Class RestoreDependencyHelper is used to restore broken URI according to a replacement Strategy.
+ */
+public class RestoreDependencyHelper {
+
+
+ /** The uri replacement strategy. */
+ protected IURIReplacementStrategy iURIReplacementStrategy;
+
+ /**
+ * Instantiates a new restore dependency helper.
+ *
+ * @param iURIReplacementStrategy the chosen uriReplacementStrategy
+ */
+ public RestoreDependencyHelper(IURIReplacementStrategy iURIReplacementStrategy) {
+ this.iURIReplacementStrategy = iURIReplacementStrategy;
+ }
+
+ /**
+ * Restore dependencies, iterate on all contents to check and replace URIs.
+ *
+ * @param resource the resource
+ * @return the collection
+ */
+ public Collection<Replacement> restoreDependencies(Resource resource) {
+ Iterator<EObject> allContentsIterator = resource.getAllContents();
+ Collection<Replacement> replacements = new LinkedList<Replacement>();
+ while(allContentsIterator.hasNext()) {
+ EObject eObject = allContentsIterator.next();
+
+ for(EReference reference : eObject.eClass().getEAllReferences()) {
+ if(reference.isContainment()) {
+ continue;
+ }
+
+ if(!reference.isChangeable()) {
+ continue;
+ }
+
+ Object value = eObject.eGet(reference);
+ if(value instanceof EObject) {
+ EObject eObjectToReplace = (EObject)value;
+ EObject newEObject = checkAndReplace(eObjectToReplace);
+ if(newEObject == null) {
+ continue;
+ }
+
+ try {
+ eObject.eSet(reference, newEObject);
+ replacements.add(new ReplacementImpl(eObject, reference, eObjectToReplace, newEObject));
+ } catch (Exception ex) {
+ Activator.log.error(ex);
+ }
+
+ } else if(value instanceof Collection<?>) {
+ Map<EObject, EObject> previousToNewValue = new HashMap<EObject, EObject>();
+ Collection<?> collection = (Collection<?>)value;
+ for(Object collectionElement : (Collection<?>)value) {
+ if(collectionElement instanceof EObject) {
+ EObject eObjectToReplace = (EObject)collectionElement;
+ EObject newEObject = checkAndReplace(eObjectToReplace);
+ if(newEObject == null) {
+ continue;
+ }
+ previousToNewValue.put(eObjectToReplace, newEObject);
+ }
+ }
+ if(previousToNewValue.isEmpty()) {
+ continue;
+ }
+ if(collection instanceof EStructuralFeature.Setting) {
+ EStructuralFeature.Setting setting = (EStructuralFeature.Setting)collection;
+ for(Map.Entry<EObject, EObject> entry : previousToNewValue.entrySet()) {
+ EcoreUtil.replace(setting, entry.getKey(), entry.getValue());
+ replacements.add(new ReplacementImpl(eObject, reference, entry.getKey(), entry.getValue()));
+ }
+ }
+ }
+ }
+ }
+ return replacements;
+ }
+
+ /**
+ * Check and replace.
+ *
+ * @param eObject the e object to replace
+ * @return the e object
+ */
+ protected EObject checkAndReplace(EObject eObject) {
+ URI eObjectURIToReplace = EcoreUtil.getURI(eObject);
+ URI resourceURI = eObjectURIToReplace.trimFragment();
+ if(!resourceURI.isEmpty()) {
+ URI targetURI = iURIReplacementStrategy.getReplacementCandidate(resourceURI);
+ if(targetURI != null && !targetURI.equals(resourceURI)) {
+ return DependencyManagementHelper.replace(eObject, targetURI);
+ }
+ }
+ return null;
+ }
+
+}
diff --git a/plugins/infra/emf/org.eclipse.papyrus.infra.emf/src/org/eclipse/papyrus/infra/emf/utils/ResourceUtils.java b/plugins/infra/emf/org.eclipse.papyrus.infra.emf/src/org/eclipse/papyrus/infra/emf/utils/ResourceUtils.java
index 225e5b57eae..26e1818e66b 100644
--- a/plugins/infra/emf/org.eclipse.papyrus.infra.emf/src/org/eclipse/papyrus/infra/emf/utils/ResourceUtils.java
+++ b/plugins/infra/emf/org.eclipse.papyrus.infra.emf/src/org/eclipse/papyrus/infra/emf/utils/ResourceUtils.java
@@ -1,81 +1,107 @@
-/*****************************************************************************
- * Copyright (c) 2012 CEA LIST.
- *
- *
- * 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:
- * Vincent Lorenzo (CEA LIST) Vincent.Lorenzo@cea.fr - Initial API and implementation
- *
- *****************************************************************************/
-package org.eclipse.papyrus.infra.emf.utils;
-
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.Set;
-
-import org.eclipse.core.resources.IFile;
-import org.eclipse.core.resources.ResourcesPlugin;
-import org.eclipse.core.runtime.Assert;
-import org.eclipse.core.runtime.Path;
-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.xmi.XMIResource;
-
-/**
- *
- * This class provides methods for EMF Resource
- *
- */
-public class ResourceUtils {
-
- private ResourceUtils() {
- //to prevent instanciation
- }
-
- /**
- *
- * @param resource
- * an EMF resource
- * @return
- * the IFile corresponding to this resource, or <code>null</code> if not found
- */
- public static IFile getFile(final Resource resource) {
- if(resource != null) {
- URI uri = resource.getURI();
- uri = resource.getResourceSet().getURIConverter().normalize(uri);
- if(uri.isPlatformResource()) {
- String uriPlatformString = uri.toPlatformString(true) ;
- return ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(uriPlatformString));
- }
- }
- return null;
- }
-
- /**
- *
- *
- * @param resource
- * a resource
- * @return
- * a collection with all existing id in the resource
- *
- */
- public static Collection<String> getAllResourceIds(final XMIResource resource) {
- final Set<String> ids = new HashSet<String>();
- final Iterator<EObject> iterator = resource.getAllContents();
- while(iterator.hasNext()) {
- final EObject current = iterator.next();
- final String id = resource.getID(current);
- Assert.isNotNull(id);
- Assert.isTrue(!ids.contains(id));
- ids.add(id);
- }
- return ids;
- }
-}
+/*****************************************************************************
+ * Copyright (c) 2012 CEA LIST.
+ *
+ *
+ * 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:
+ * Vincent Lorenzo (CEA LIST) Vincent.Lorenzo@cea.fr - Initial API and implementation
+ *
+ *****************************************************************************/
+package org.eclipse.papyrus.infra.emf.utils;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.Assert;
+import org.eclipse.core.runtime.Path;
+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.xmi.XMIResource;
+
+/**
+ *
+ * This class provides methods for EMF Resource
+ *
+ */
+public class ResourceUtils {
+
+ private ResourceUtils() {
+ //to prevent instanciation
+ }
+
+ /**
+ *
+ * @param resource
+ * an EMF resource
+ * @return
+ * the IFile corresponding to this resource, or <code>null</code> if not found
+ */
+ public static IFile getFile(final Resource resource) {
+ if(resource != null) {
+ URI uri = resource.getURI();
+ uri = resource.getResourceSet().getURIConverter().normalize(uri);
+ if(uri.isPlatformResource()) {
+ String uriPlatformString = uri.toPlatformString(true) ;
+ return ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(uriPlatformString));
+ }
+ }
+ return null;
+ }
+
+ /**
+ *
+ *
+ * @param resource
+ * a resource
+ * @return
+ * a collection with all existing id in the resource
+ *
+ */
+ public static Collection<String> getAllResourceIds(final XMIResource resource) {
+ final Set<String> ids = new HashSet<String>();
+ final Iterator<EObject> iterator = resource.getAllContents();
+ while(iterator.hasNext()) {
+ final EObject current = iterator.next();
+ final String id = resource.getID(current);
+ Assert.isNotNull(id);
+ Assert.isTrue(!ids.contains(id));
+ ids.add(id);
+ }
+ return ids;
+ }
+
+
+ /**
+ * Default options to save emf files used in Papyrus
+ * @return
+ */
+ public static Map<String, Object> getSaveOptions() {
+ Map<String, Object> saveOptions = new HashMap<String, Object>();
+
+ // default save options.
+ saveOptions.put(XMIResource.OPTION_DECLARE_XML, Boolean.TRUE);
+ saveOptions.put(XMIResource.OPTION_PROCESS_DANGLING_HREF, XMIResource.OPTION_PROCESS_DANGLING_HREF_DISCARD);
+ saveOptions.put(XMIResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE);
+ saveOptions.put(XMIResource.OPTION_USE_XMI_TYPE, Boolean.TRUE);
+ saveOptions.put(XMIResource.OPTION_SAVE_TYPE_INFORMATION, Boolean.TRUE);
+ saveOptions.put(XMIResource.OPTION_SKIP_ESCAPE_URI, Boolean.FALSE);
+ saveOptions.put(XMIResource.OPTION_ENCODING, "UTF-8");
+
+ //see bug 397987: [Core][Save] The referenced plugin models are saved using relative path
+ saveOptions.put(XMIResource.OPTION_URI_HANDLER, new org.eclipse.emf.ecore.xmi.impl.URIHandlerImpl.PlatformSchemeAware());
+
+ return saveOptions;
+ }
+
+}

Back to the top