Skip to content

Commit 32e3509

Browse files
committed
ST6RI-684 Implemented Namespace::qualificationOf and unqualifiedNameOf.
1 parent 6a1e002 commit 32e3509

4 files changed

Lines changed: 61 additions & 50 deletions

File tree

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

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -42,52 +42,14 @@ class KerMLQualifiedNameConverter implements IQualifiedNameConverter {
4242

4343
Preconditions.checkArgument(!qualifiedNameAsText.empty, "Qualified name cannot be empty")
4444

45-
val segments = newArrayList
46-
var i = 0
47-
var j = 0;
48-
var n = qualifiedNameAsText.length()
49-
var isDelimitable = true
50-
51-
while (j < n) {
52-
val c = qualifiedNameAsText.charAt(j)
53-
val delim = "\'\\:".indexOf(c)
54-
if (isDelimitable && delim > 1) {
55-
if (j + 1 < n && ":". indexOf(qualifiedNameAsText.charAt(j + 1)) == 0) {
56-
segments.add(ElementUtil.unescapeString(qualifiedNameAsText.substring(i, j)));
57-
i = j + 2;
58-
j = i - 1;
59-
}
60-
} else if (delim == 0) {
61-
isDelimitable = !isDelimitable
62-
} else if (delim == 1) {
63-
j++
64-
}
65-
j++
66-
}
67-
if (i < n && j <= n) {
68-
segments.add(ElementUtil.unescapeString(qualifiedNameAsText.substring(i, j)));
69-
}
70-
45+
val segments = ElementUtil.parseQualifiedName(qualifiedNameAsText)
7146
QualifiedName.create(segments)
7247
}
7348

7449
override toString(QualifiedName name) {
75-
if (name === null)
76-
throw new IllegalArgumentException("Qualified name cannot be null")
77-
val segmentCount = name.getSegmentCount
78-
switch (segmentCount) {
79-
case 0: return ""
80-
case 1: return ElementUtil.escapeName(name.getFirstSegment)
81-
default: {
82-
val builder = new StringBuilder;
83-
builder.append(ElementUtil.escapeName(name.getFirstSegment))
84-
for (var i = 1; i < segmentCount; i++) {
85-
builder.append("::")
86-
builder.append(ElementUtil.escapeName(name.getSegment(i)))
87-
}
88-
return builder.toString()
89-
}
90-
}
50+
Preconditions.checkArgument(name !== null, "Qualified name cannot be null")
51+
52+
ElementUtil.toQualifiedNameString(name.segments)
9153
}
9254

9355
}

org.omg.sysml/src/org/omg/sysml/delegate/invocation/Namespace_qualificationOf_InvocationDelegate.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@
2222
package org.omg.sysml.delegate.invocation;
2323

2424
import java.lang.reflect.InvocationTargetException;
25+
import java.util.List;
2526

2627
import org.eclipse.emf.common.util.EList;
2728
import org.eclipse.emf.ecore.EOperation;
2829
import org.eclipse.emf.ecore.InternalEObject;
2930
import org.eclipse.emf.ecore.util.BasicInvocationDelegate;
30-
import org.omg.sysml.lang.sysml.Namespace;
31+
import org.omg.sysml.util.ElementUtil;
3132

3233
public class Namespace_qualificationOf_InvocationDelegate extends BasicInvocationDelegate {
3334

@@ -37,11 +38,11 @@ public Namespace_qualificationOf_InvocationDelegate(EOperation operation) {
3738

3839
@Override
3940
public Object dynamicInvoke(InternalEObject target, EList<?> arguments) throws InvocationTargetException {
40-
Namespace self = (Namespace) target;
4141
String qualifiedName = (String) arguments.get(0);
4242

43-
// TODO: implement this method
44-
throw new UnsupportedOperationException();
43+
List<String> segments = ElementUtil.parseQualifiedName(qualifiedName);
44+
int n = segments.size();
45+
return n < 2? null: ElementUtil.toQualifiedNameString(segments.subList(0, n - 1));
4546
}
4647

4748
}

org.omg.sysml/src/org/omg/sysml/delegate/invocation/Namespace_unqualifiedNameOf_InvocationDelegate.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@
2222
package org.omg.sysml.delegate.invocation;
2323

2424
import java.lang.reflect.InvocationTargetException;
25+
import java.util.List;
2526

2627
import org.eclipse.emf.common.util.EList;
2728
import org.eclipse.emf.ecore.EOperation;
2829
import org.eclipse.emf.ecore.InternalEObject;
2930
import org.eclipse.emf.ecore.util.BasicInvocationDelegate;
30-
import org.omg.sysml.lang.sysml.Namespace;
31+
import org.omg.sysml.util.ElementUtil;
3132

3233
public class Namespace_unqualifiedNameOf_InvocationDelegate extends BasicInvocationDelegate {
3334

@@ -37,11 +38,11 @@ public Namespace_unqualifiedNameOf_InvocationDelegate(EOperation operation) {
3738

3839
@Override
3940
public Object dynamicInvoke(InternalEObject target, EList<?> arguments) throws InvocationTargetException {
40-
Namespace self = (Namespace) target;
4141
String qualifiedName = (String) arguments.get(0);
4242

43-
// TODO: implement this method
44-
throw new UnsupportedOperationException();
43+
List<String> segments = ElementUtil.parseQualifiedName(qualifiedName);
44+
int n = segments.size();
45+
return n < 1? null: segments.get(n - 1);
4546
}
4647

4748
}

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,53 @@ public static String escapeName(String name) {
122122
public static boolean isIdentifier(String name) {
123123
return name.matches("[a-zA-Z_]\\w*");
124124
}
125+
126+
public static List<String> parseQualifiedName(String qualifiedNameText) {
127+
List<String> segments = new ArrayList<>();
128+
int i = 0;
129+
int j = 0;
130+
int n = qualifiedNameText.length();
131+
boolean isDelimitable = true;
132+
133+
while (j < n) {
134+
char c = qualifiedNameText.charAt(j);
135+
int delim = "\'\\:".indexOf(c);
136+
if (isDelimitable && delim > 1) {
137+
if (j + 1 < n && ":". indexOf(qualifiedNameText.charAt(j + 1)) == 0) {
138+
segments.add(ElementUtil.unescapeString(qualifiedNameText.substring(i, j)));
139+
i = j + 2;
140+
j = i - 1;
141+
}
142+
} else if (delim == 0) {
143+
isDelimitable = !isDelimitable;
144+
} else if (delim == 1) {
145+
j++;
146+
}
147+
j++;
148+
}
149+
if (i < n && j <= n) {
150+
segments.add(ElementUtil.unescapeString(qualifiedNameText.substring(i, j)));
151+
}
152+
153+
return segments;
154+
}
155+
156+
public static String toQualifiedNameString(List<String> segments) {
157+
int segmentCount = segments.size();
158+
if (segmentCount == 0) {
159+
return "";
160+
} else if (segmentCount == 1)
161+
return escapeName(segments.get(0));
162+
else {
163+
StringBuilder builder = new StringBuilder();
164+
builder.append(ElementUtil.escapeName(segments.get(0)));
165+
for (var i = 1; i < segmentCount; i++) {
166+
builder.append("::");
167+
builder.append(ElementUtil.escapeName(segments.get(i)));
168+
}
169+
return builder.toString();
170+
}
171+
}
125172

126173
/**
127174
* Get documentation text for this element, as given by the body of the first documentation comment

0 commit comments

Comments
 (0)