Skip to content
Snippets Groups Projects
Commit a988edf0 authored by Simon Barner's avatar Simon Barner
Browse files

- Add computeFullyQualifiedName(INamedElement element, boolean...

- Add computeFullyQualifiedName(INamedElement element, boolean includeTopLevel) that allows to skip the top-level of a name (the FileProject in AF3)
- The behavior of computeFullyQualifiedName(INamedElement element) is preserved to include the name of the top-level element
- Change implementation of computeFullyQualifiedName() and computeRelativeName() to use the a StringBuilder and the prependName() helper method
parent 44cbea52
No related branches found
No related tags found
No related merge requests found
......@@ -39,7 +39,7 @@ import org.fortiss.tooling.kernel.service.IPersistencyService;
* @author hoelzl
* @author $Author$
* @version $Rev$
* @ConQAT.Rating YELLOW Hash: 7C95224EE38B0DF2821F3387980946F5
* @ConQAT.Rating YELLOW Hash: 81BE17E4DF7F66D299A6A155ADDE846D
*/
public final class KernelModelElementUtils {
......@@ -249,6 +249,21 @@ public final class KernelModelElementUtils {
return null;
}
/**
* Internal helper method for {@link #computeFullyQualifiedName(INamedElement, boolean)} and
* {@link #computeRelativeName(INamedElement, INamedElement)} that
* prepends the name of a single element to the current value of fully qualified name.
*/
private static void prependName(StringBuilder sb, EObject current) {
if(current instanceof INamedElement && !(current instanceof ILibraryElementReference)) {
if(sb.length() != 0) {
sb.insert(0, ".");
}
sb.insert(0, ((INamedElement)current).getName());
}
}
/**
* Computes the qualified name relative to a parent element.
*
......@@ -260,21 +275,18 @@ public final class KernelModelElementUtils {
* parent element is not a container for element.
*/
public static String computeRelativeName(INamedElement parent, INamedElement element) {
String qualifiedName = element.getName();
StringBuilder sb = new StringBuilder();
EObject current = element;
while(current != null && current != parent) {
prependName(sb, current);
current = current.eContainer();
if(current instanceof INamedElement && !(current instanceof ILibraryElementReference)) {
qualifiedName = ((INamedElement)current).getName() + "." + qualifiedName;
}
}
if(current == null) {
return null;
}
return qualifiedName;
return sb.toString();
}
/**
......@@ -286,17 +298,33 @@ public final class KernelModelElementUtils {
* @return the element's fully qualified name.
*/
public static String computeFullyQualifiedName(INamedElement element) {
String qualifiedName = element.getName();
return computeFullyQualifiedName(element, true);
}
/**
* Computes the fully qualified name of an element.
*
* @param element
* the element whose fully qualified name will be computed.
*
* @param includeTopLevel
* Flag whether to include the top-level of the model element hierarchy into the name
*
* @return the element's fully qualified name.
*/
public static String computeFullyQualifiedName(INamedElement element, boolean includeTopLevel) {
StringBuilder sb = new StringBuilder();
EObject current = element;
while(current != null) {
while(current.eContainer() != null) {
prependName(sb, current);
current = current.eContainer();
if(current instanceof INamedElement && !(current instanceof ILibraryElementReference)) {
qualifiedName = ((INamedElement)current).getName() + "." + qualifiedName;
}
}
if(includeTopLevel) {
prependName(sb, current);
}
return qualifiedName;
return sb.toString();
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment