Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEike Stepper2012-09-04 08:24:09 +0000
committerEike Stepper2012-09-04 08:24:09 +0000
commita41909b6e4e19df5d10fff8d6a53d7273b71c4e3 (patch)
treeb7c6c6af1f396719a7b0d6af243d20d9d92ebd5c /plugins
parent91202b47531461e35fe23d784e15bf6b62345f71 (diff)
downloadcdo-a41909b6e4e19df5d10fff8d6a53d7273b71c4e3.tar.gz
cdo-a41909b6e4e19df5d10fff8d6a53d7273b71c4e3.tar.xz
cdo-a41909b6e4e19df5d10fff8d6a53d7273b71c4e3.zip
[388723] Add EMFUtil.getPositionalURI(InternalEObject)
https://bugs.eclipse.org/bugs/show_bug.cgi?id=388723
Diffstat (limited to 'plugins')
-rw-r--r--plugins/org.eclipse.emf.cdo.common/src/org/eclipse/emf/cdo/common/model/EMFUtil.java67
1 files changed, 67 insertions, 0 deletions
diff --git a/plugins/org.eclipse.emf.cdo.common/src/org/eclipse/emf/cdo/common/model/EMFUtil.java b/plugins/org.eclipse.emf.cdo.common/src/org/eclipse/emf/cdo/common/model/EMFUtil.java
index 3ef71b898b..0a3baef061 100644
--- a/plugins/org.eclipse.emf.cdo.common/src/org/eclipse/emf/cdo/common/model/EMFUtil.java
+++ b/plugins/org.eclipse.emf.cdo.common/src/org/eclipse/emf/cdo/common/model/EMFUtil.java
@@ -36,6 +36,7 @@ import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
+import org.eclipse.emf.ecore.util.FeatureMap;
import org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl;
import java.io.ByteArrayInputStream;
@@ -80,6 +81,72 @@ public final class EMFUtil
{
}
+ /**
+ * @since 4.2
+ */
+ public static URI getPositionalURI(InternalEObject internalEObject)
+ {
+ List<String> uriFragmentPath = new ArrayList<String>();
+ Resource resource;
+ for (InternalEObject container = internalEObject.eInternalContainer(); (resource = internalEObject
+ .eDirectResource()) == null && container != null; container = internalEObject.eInternalContainer())
+ {
+ String segment = getPositionalURIFragmentSegment(container, internalEObject.eContainingFeature(), internalEObject);
+ uriFragmentPath.add(segment);
+ internalEObject = container;
+ }
+
+ StringBuilder builder = new StringBuilder("/");
+ builder.append(resource.getContents().indexOf(internalEObject));
+ for (int i = uriFragmentPath.size() - 1; i >= 0; --i)
+ {
+ builder.append('/');
+ builder.append(uriFragmentPath.get(i));
+ }
+
+ return resource.getURI().appendFragment(builder.toString());
+ }
+
+ /**
+ * @since 4.2
+ */
+ private static String getPositionalURIFragmentSegment(EObject container, EStructuralFeature eStructuralFeature,
+ InternalEObject eObject)
+ {
+ StringBuilder builder = new StringBuilder();
+ builder.append('@');
+ builder.append(eStructuralFeature.getName());
+
+ if (eStructuralFeature instanceof EAttribute)
+ {
+ FeatureMap featureMap = (FeatureMap)container.eGet(eStructuralFeature, false);
+ for (int i = 0, size = featureMap.size(); i < size; ++i)
+ {
+ if (featureMap.getValue(i) == eObject)
+ {
+ EStructuralFeature entryFeature = featureMap.getEStructuralFeature(i);
+ if (entryFeature instanceof EReference && ((EReference)entryFeature).isContainment())
+ {
+ builder.append('.');
+ builder.append(i);
+ return builder.toString();
+ }
+ }
+ }
+
+ builder.append(".-1");
+ }
+ else if (eStructuralFeature.isMany())
+ {
+ EList<?> eList = (EList<?>)container.eGet(eStructuralFeature, false);
+ int index = eList.indexOf(eObject);
+ builder.append('.');
+ builder.append(index);
+ }
+
+ return builder.toString();
+ }
+
public static EPackage getGeneratedEPackage(EPackage ePackage)
{
String packageURI = ePackage.getNsURI();

Back to the top