Skip to content

Commit

Permalink
getReferenceAsString, getReferenceAsQN - from FaceQNP -> UddlQNP
Browse files Browse the repository at this point in the history
  • Loading branch information
steve-hickman-epistimis committed Mar 18, 2024
1 parent 3c30d94 commit 64e6398
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions com.epistimis.uddl/src/com/epistimis/uddl/UddlQNP.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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<INode> 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
Expand Down

0 comments on commit 64e6398

Please sign in to comment.