Skip to content

Commit c042d65

Browse files
committed
ST6RI-830 Revised QualifiedNameUtil to abstract global scoping impl.
1 parent 0b84200 commit c042d65

5 files changed

Lines changed: 45 additions & 23 deletions

File tree

org.omg.kerml.xtext/src/org/omg/kerml/xtext/naming/KerMLQualifiedNameConverter.xtend

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,12 @@ class KerMLQualifiedNameConverter implements IQualifiedNameConverter {
4343
Preconditions.checkArgument(!qualifiedNameAsText.empty, "Qualified name cannot be empty")
4444

4545
val segments = ElementUtil.parseQualifiedName(qualifiedNameAsText)
46-
QualifiedName.create(segments)
46+
QualifiedNameUtil.createQualifiedName(segments)
4747
}
4848

4949
override toString(QualifiedName name) {
50-
Preconditions.checkArgument(name !== null, "Qualified name cannot be null")
51-
52-
ElementUtil.toQualifiedNameString(name.segments)
50+
Preconditions.checkArgument(name !== null, "Qualified name cannot be null")
51+
QualifiedNameUtil.toQualifiedNameString(name)
5352
}
5453

5554
}

org.omg.kerml.xtext/src/org/omg/kerml/xtext/naming/QualifiedNameUtil.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,34 @@
2323
*/
2424
package org.omg.kerml.xtext.naming;
2525

26+
import java.util.List;
27+
2628
import org.eclipse.xtext.naming.QualifiedName;
2729
import org.omg.sysml.util.ElementUtil;
2830

31+
/**
32+
* This class provides utility methods for qualified names that abstracts the representation of
33+
* globally scoped qualified names. The current implementation presumes that globally scoped
34+
* qualified names are represented as Xtext QualifiedNames whose first symbol is specific
35+
* String identified by ElementUtil.isGlobalScopeSymbol.
36+
*/
2937
public class QualifiedNameUtil {
3038

31-
public static boolean isGlobalNameQualification(QualifiedName qualifiedName) {
39+
public static QualifiedName createQualifiedName(List<String> segments) {
40+
return QualifiedName.create(segments);
41+
}
42+
43+
public static boolean isGlobalScopeQualification(QualifiedName qualifiedName) {
3244
return qualifiedName != null &&
3345
qualifiedName.getSegmentCount() > 0 &&
3446
ElementUtil.isGlobalScopeSymbol(qualifiedName.getFirstSegment());
3547
}
3648

49+
public static QualifiedName getNonGlobalQualifiedName(QualifiedName qualifiedName) {
50+
return isGlobalScopeQualification(qualifiedName)? qualifiedName.skipFirst(1): qualifiedName;
51+
}
52+
53+
public static String toQualifiedNameString(QualifiedName qualifiedName) {
54+
return ElementUtil.toQualifiedNameString(qualifiedName.getSegments());
55+
}
3756
}

org.omg.kerml.xtext/src/org/omg/kerml/xtext/scoping/KerMLGlobalScope.xtend

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,11 @@ class KerMLGlobalScope extends AbstractScope {
7979
}
8080

8181
override getSingleElement(QualifiedName name) {
82-
val isGlobalQualification = QualifiedNameUtil.isGlobalNameQualification(name)
83-
val qualifiedName = isGlobalQualification? name.skipFirst(1) : name
84-
82+
val qualifiedName = QualifiedNameUtil.getNonGlobalQualifiedName(name)
8583
var IEObjectDescription result = null
8684

8785
if (qualifiedName.segmentCount > 0) {
88-
if (isGlobalQualification) {
86+
if (QualifiedNameUtil.isGlobalScopeQualification(name)) {
8987
//search the context resource first
9088
val rootNS = resource.contents.head as Namespace
9189
result = scopeFor(rootNS).getSingleElement(qualifiedName)

org.omg.kerml.xtext/src/org/omg/kerml/xtext/scoping/KerMLScope.xtend

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ class KerMLScope extends AbstractScope implements ISysMLScope {
168168
}
169169

170170
override getSingleElement(QualifiedName name) {
171-
if (QualifiedNameUtil.isGlobalNameQualification(name)){
171+
if (QualifiedNameUtil.isGlobalScopeQualification(name)){
172172
parent.getSingleElement(name)
173173
} else {
174174
val result = resolveInScope(name, true);

org.omg.sysml/src/org/omg/sysml/util/ElementUtil.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public static String unescapeString(String literal) {
9696
}
9797

9898
/**
99-
* Return a string that is the same as the input string, but with escapable characters
99+
* Return a string that is the same as the input string, but with escapable characters
100100
* replaced by appropriate escape sequences.
101101
*/
102102
public static String escapeString(String str) {
@@ -114,15 +114,19 @@ public static String escapeString(String str) {
114114
}
115115

116116
public static String escapeName(String name) {
117-
return (name == null || name.isEmpty() || isIdentifier(name))? name:
117+
return (name == null || name.isEmpty() || isIdentifier(name) || isGlobalScopeSymbol(name))? name:
118118
"'" + escapeString(name) + "'";
119119
}
120120

121121
public static boolean isIdentifier(String name) {
122122
return name.matches("[a-zA-Z_]\\w*");
123123
}
124124

125-
// Specific string used to identify a segment as a global scope qualifier
125+
// Qualified Names
126+
127+
/**
128+
* Specific string used to identify a segment as a global scope qualifier
129+
*/
126130
public static final String GLOBAL_SCOPE_SYMBOL = "$";
127131

128132
public static boolean isGlobalScopeSymbol(String segment) {
@@ -184,17 +188,9 @@ public static String toQualifiedNameString(List<String> segments) {
184188
return builder.toString();
185189
}
186190
}
187-
188-
/**
189-
* Get documentation text for this element, as given by the body of the first documentation comment
190-
* annotating the element (if any).
191-
*/
192-
public static String getDocumentationTextFor(Element element) {
193-
return element.getDocumentation().stream().
194-
map(Comment::getBody).
195-
findFirst().orElse(null);
196-
}
197191

192+
// Library Elements
193+
198194
public static boolean isStandardLibraryElement(Element element) {
199195
Namespace libraryNamespace = element.libraryNamespace();
200196
return libraryNamespace instanceof LibraryPackage &&
@@ -251,6 +247,16 @@ public static List<MetadataFeature> getAllMetadataFeaturesOf(Element element) {
251247
return metadataFeatures;
252248
}
253249

250+
/**
251+
* Get documentation text for this element, as given by the body of the first documentation comment
252+
* annotating the element (if any).
253+
*/
254+
public static String getDocumentationTextFor(Element element) {
255+
return element.getDocumentation().stream().
256+
map(Comment::getBody).
257+
findFirst().orElse(null);
258+
}
259+
254260
public static String processCommentBody(String body) {
255261
if (body != null) {
256262
body = body.replaceFirst("/\\*", "").replaceFirst("^\\s*", "");

0 commit comments

Comments
 (0)