001: package org.andromda.schema2xmi;
002:
003: import java.util.Collection;
004:
005: import org.apache.commons.collections.CollectionUtils;
006: import org.apache.commons.collections.Predicate;
007: import org.apache.commons.lang.StringUtils;
008: import org.omg.uml.foundation.core.ModelElement;
009: import org.omg.uml.modelmanagement.Model;
010: import org.omg.uml.modelmanagement.UmlPackage;
011:
012: /**
013: * Finds model elements by their names.
014: *
015: * @author Chad Brandon
016: */
017: public class ModelElementFinder {
018: /**
019: * Finds the model element having the <code>fullyQualifiedName</code> in
020: * the <code>model</code>, returns <code>null</code> if not found.
021: *
022: * @param model The model to search
023: * @param fullyQualifiedName the fully qualified name to find.
024: * @return the found model element.
025: */
026: public static Object find(Model model, String fullyQualifiedName) {
027: Object modelElement = null;
028: if (model != null) {
029: String[] names = fullyQualifiedName
030: .split(Schema2XMIGlobals.PACKAGE_SEPERATOR);
031: if (names != null && names.length > 0) {
032: Object element = model;
033: for (int ctr = 0; ctr < names.length && element != null; ctr++) {
034: String name = names[ctr];
035: if (UmlPackage.class.isAssignableFrom(element
036: .getClass())) {
037: element = getElement(((UmlPackage) element)
038: .getOwnedElement(), name);
039: }
040: modelElement = element;
041: }
042: }
043: }
044: return modelElement;
045: }
046:
047: /**
048: * Finds and returns the first model element having the given
049: * <code>name</code> in the <code>modelPackage</code>, returns
050: * <code>null</code> if not found.
051: *
052: * @param modelPackage The modelPackage to search
053: * @param name the name to find.
054: * @return the found model element.
055: */
056: public static Object find(org.omg.uml.UmlPackage modelPackage,
057: final String name) {
058: return CollectionUtils.find(modelPackage.getCore()
059: .getModelElement().refAllOfType(), new Predicate() {
060: public boolean evaluate(Object object) {
061: return StringUtils.trimToEmpty(
062: ((ModelElement) object).getName()).equals(name);
063: }
064: });
065: }
066:
067: /**
068: * Finds and returns the first model element having the given
069: * <code>name</code> in the <code>umlPackage</code>, returns
070: * <code>null</code> if not found.
071: *
072: * @param umlPackage The modelPackage to search
073: * @param name the name to find.
074: * @return the found model element.
075: */
076: public static Object find(
077: org.omg.uml.modelmanagement.UmlPackage umlPackage,
078: final String name) {
079: return CollectionUtils.find(umlPackage.getOwnedElement(),
080: new Predicate() {
081: public boolean evaluate(Object object) {
082: return StringUtils.trimToEmpty(
083: ((ModelElement) object).getName())
084: .equals(name);
085: }
086: });
087: }
088:
089: /**
090: * Finds the model element having the <code>name</code> contained within
091: * the <code>elements</code>, returns null if it can't be found.
092: *
093: * @param elements the collection of model elements to search
094: * @param name the name of the model element.
095: * @return the found model element or null if not found.
096: */
097: private static Object getElement(Collection elements,
098: final String name) {
099: return CollectionUtils.find(elements, new Predicate() {
100: public boolean evaluate(Object object) {
101: return StringUtils.trimToEmpty(
102: ((ModelElement) object).getName()).equals(name);
103: }
104: });
105: }
106: }
|