|
| 1 | +package jce.util |
| 2 | + |
| 3 | +import java.util.List |
| 4 | +import org.eclipse.emf.ecore.EClass |
| 5 | +import org.eclipse.emf.ecore.EClassifier |
| 6 | +import org.eclipse.emf.ecore.ENamedElement |
| 7 | +import org.eclipse.emf.ecore.EPackage |
| 8 | +import org.eclipse.emf.ecore.EStructuralFeature |
| 9 | + |
| 10 | +/** |
| 11 | + * A utility class for searching elements in Ecore metamodels. |
| 12 | + * @author Heiko Klare, Timur Saglam |
| 13 | + */ |
| 14 | +final class EcoreUtil { |
| 15 | + static final PathHelper PATH = new PathHelper(Character.valueOf('.').charValue) |
| 16 | + |
| 17 | + private new() { |
| 18 | + throw new AssertionError("Suppress default constructor for noninstantiability") |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * Finds a specific {@link EClass} in a {@link EPackage} and its subpackages. |
| 23 | + * @param fullName is the fully qualified name of the desired {@link EClass}. |
| 24 | + * @param ePackage is the {@link EPackage} which contains the {@link EClass}. |
| 25 | + * @return the {@link EClass} or null if there is none with the specified name. |
| 26 | + */ |
| 27 | + def static EClass findEClass(String fullName, EPackage ePackage) { |
| 28 | + var String eClassName = PATH.getLastSegment(fullName) // get EClass name |
| 29 | + var EPackage parent = findESubpackage(PATH.cutLastSegment(fullName), ePackage) // search EPackage. |
| 30 | + if (parent !== null) { // if EPackage was found. |
| 31 | + for (EClassifier classifier : parent.getEClassifiers()) { // for every EClassifier: |
| 32 | + if (classifier instanceof EClass && isSame(classifier, eClassName)) { |
| 33 | + return (classifier as EClass) // search for the right EClass. |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + return null |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Finds a specific {@link EPackage} which is directly or indirectly a subpackage of a given {@link EPackage}. |
| 42 | + * @param fullName is the fully qualified name of the desired {@link EPackage}. |
| 43 | + * @param ePackage is the {@link EPackage} which contains the subpackage. |
| 44 | + * @return the subpackage or null if there is none with the specified name. |
| 45 | + */ |
| 46 | + def static EPackage findESubpackage(String fullName, EPackage ePackage) { |
| 47 | + for (EPackage subpackage : ePackage.getESubpackages()) { // for every subpackage |
| 48 | + if (isSame(subpackage, PATH.getFirstSegment(fullName))) { // if is the right subpackage |
| 49 | + if (!PATH.hasMultipleSegments(fullName)) { |
| 50 | + return subpackage // return package if the are no more segments in the path |
| 51 | + } else { |
| 52 | + return findESubpackage(PATH.cutFirstSegment(fullName), subpackage) // search further for every segment |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + return null |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Returns the package with the given fully qualified name, starting from the given root {@link EPackage}. |
| 61 | + * @param rootPackage the root {@link EPackage} of a metamodel to start from |
| 62 | + * @param fullyQualifiedName the fully qualified name of the package to search for |
| 63 | + * @return the resolved {@link EPackage} or <code>null</code> if none was found |
| 64 | + */ |
| 65 | + def static EPackage findEPackage(EPackage ePackage, String fullName) { |
| 66 | + if (isSame(ePackage, fullName)) { |
| 67 | + return ePackage |
| 68 | + } |
| 69 | + return findESubpackage(PATH.cutFirstSegment(fullName), ePackage) |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Finds a specific {@link EStructuralFeature} in an {@link EClass}. |
| 74 | + * @param fullName is the fully qualified name of the desired {@link EStructuralFeature}. |
| 75 | + * @param eClass is the {@link EClass} which contains the {@link EStructuralFeature}. |
| 76 | + * @return the {@link EStructuralFeature} or null if there is none with the specified name. |
| 77 | + */ |
| 78 | + def static EStructuralFeature findEStructuralFeature(String fullName, EClass eClass) { |
| 79 | + for (EStructuralFeature feature : eClass.getEStructuralFeatures()) { |
| 80 | + if (isSame(feature, fullName)) { |
| 81 | + return feature |
| 82 | + } |
| 83 | + } |
| 84 | + return null |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Finds a specific {@link EStructuralFeature} in an {@link EPackage}. |
| 89 | + * @param fullName is the fully qualified name of the desired {@link EStructuralFeature}. |
| 90 | + * @param eClassName is the fully qualified name of the {@link EClass} that contains the {@link EStructuralFeature}. |
| 91 | + * @param ePackage ePackage is the {@link EPackage} which contains the {@link EClass} that contains the{@link EStructuralFeature}. |
| 92 | + * @return the {@link EStructuralFeature} or null if there is none with the specified name. |
| 93 | + */ |
| 94 | + def static EStructuralFeature findEStructuralFeature(String fullName, String eClassName, EPackage ePackage) { |
| 95 | + var EClass eClass = findEClass(eClassName, ePackage) |
| 96 | + if (eClass !== null) { // if class was found |
| 97 | + return findEStructuralFeature(fullName, eClass) // search for feature |
| 98 | + } |
| 99 | + return null |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Returns the list of names of all non-abstract {@link EClass}es in an {@link EPackage}. |
| 104 | + */ |
| 105 | + def static List<String> getClassNames(EPackage ePackage) { |
| 106 | + return ePackage.EClassifiers.filter(EClass).filter[!interface && !abstract].map[name].toList |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Compares the name of an ENamedElement with a String. |
| 111 | + */ |
| 112 | + def private static boolean isSame(ENamedElement element, String elementName) { |
| 113 | + return element.getName().equals(elementName) |
| 114 | + } |
| 115 | +} |
0 commit comments