From 64e6398b0a7ca1e58ca1121619689d0aa2e005f5 Mon Sep 17 00:00:00 2001 From: Steve Hickman Date: Mon, 18 Mar 2024 15:31:54 -0700 Subject: [PATCH] getReferenceAsString, getReferenceAsQN - from FaceQNP -> UddlQNP --- .../src/com/epistimis/uddl/UddlQNP.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/com.epistimis.uddl/src/com/epistimis/uddl/UddlQNP.java b/com.epistimis.uddl/src/com/epistimis/uddl/UddlQNP.java index e990383..f0c524e 100644 --- a/com.epistimis.uddl/src/com/epistimis/uddl/UddlQNP.java +++ b/com.epistimis.uddl/src/com/epistimis/uddl/UddlQNP.java @@ -5,6 +5,7 @@ import java.lang.invoke.MethodHandles; import java.util.Collection; import java.util.HashMap; +import java.util.List; import java.util.Map; import org.apache.log4j.Logger; @@ -19,6 +20,8 @@ import org.eclipse.xtext.naming.DefaultDeclarativeQualifiedNameProvider; import org.eclipse.xtext.naming.IQualifiedNameConverter; import org.eclipse.xtext.naming.QualifiedName; +import org.eclipse.xtext.nodemodel.INode; +import org.eclipse.xtext.nodemodel.util.NodeModelUtils; //import org.eclipse.xtext.xbase.scoping.XbaseQualifiedNameProvider; import org.eclipse.emf.ecore.util.EContentsEList; @@ -135,6 +138,37 @@ public QualifiedName minimalQualifiedName(EObject obj, String qnPrefix) { } + /** + * Per this (https://www.eclipse.org/forums/index.php?t=msg&th=1084491&goto=1776641&) + * we can't use cross references in a QNP. Instead, we can grab the actual text + * of the cross reference using NodeModelUtils (https://archive.eclipse.org/modeling/tmf/xtext/javadoc/2.3/org/eclipse/xtext/nodemodel/util/NodeModelUtils.html) + * (which is what we want anyway). + * + * Note this is likely to be a QN - and we need the entire thing to make sure we don't have name + * collisions. + * + * NOTE also: When using a QN as a node in a larger qualified name, we can't use it in its default + * format because it will include '.' separators that would make it look like multiple segments. + * That means we need to convert the entire thing into a string that uses a different separator. + * Ideally, the replacement separator should not be something that would otherwise be used in + * a single node (e.g. '_') because that could result in name collisions. + */ + public static String getReferenceAsString(EObject obj, String featureName) { + EStructuralFeature refFeature = obj.eClass().getEStructuralFeature(featureName); + // Should only be 1 node + List nodes = NodeModelUtils.findNodesForFeature(obj, refFeature); + INode node = nodes.get(0); + return node.getText().trim(); + } + + public QualifiedName getReferenceAsQN(EObject obj, String featureName) { + String qnString = getReferenceAsString(obj,featureName); + + // Since the string may itself be a qualified name, we need to parse it into segments + QualifiedName refQN = converter.toQualifiedName(qnString); + return refQN; + } + /** * Return the shortest QN that will work as a valid reference, taking * into account all the imported namespaces in the resource where the reference