View Javadoc
1   package org.andromda.schema2xmi;
2   
3   import java.util.Collection;
4   import org.apache.commons.collections.CollectionUtils;
5   import org.apache.commons.collections.Predicate;
6   import org.apache.commons.lang.StringUtils;
7   import org.omg.uml.foundation.core.ModelElement;
8   import org.omg.uml.modelmanagement.Model;
9   import org.omg.uml.modelmanagement.UmlPackage;
10  
11  /**
12   * Finds model elements by their names.
13   *
14   * @author Chad Brandon
15   */
16  public class ModelElementFinder
17  {
18      /**
19       * Finds the model element having the <code>fullyQualifiedName</code> in
20       * the <code>model</code>, returns <code>null</code> if not found.
21       *
22       * @param model The model to search
23       * @param fullyQualifiedName the fully qualified name to find.
24       * @return the found model element.
25       */
26      public static Object find(
27          Model model,
28          String fullyQualifiedName)
29      {
30          Object modelElement = null;
31          if (model != null)
32          {
33              String[] names = fullyQualifiedName.split(Schema2XMIGlobals.PACKAGE_SEPARATOR);
34              if (names != null && names.length > 0)
35              {
36                  Object element = model;
37                  for (int ctr = 0; ctr < names.length && element != null; ctr++)
38                  {
39                      String name = names[ctr];
40                      if (UmlPackage.class.isAssignableFrom(element.getClass()))
41                      {
42                          element = getElement(
43                                  ((UmlPackage)element).getOwnedElement(),
44                                  name);
45                      }
46                      modelElement = element;
47                  }
48              }
49          }
50          return modelElement;
51      }
52  
53      /**
54       * Finds and returns the first model element having the given
55       * <code>name</code> in the <code>modelPackage</code>, returns
56       * <code>null</code> if not found.
57       *
58       * @param modelPackage The modelPackage to search
59       * @param name the name to find.
60       * @return the found model element.
61       */
62      public static Object find(
63          org.omg.uml.UmlPackage modelPackage,
64          final String name)
65      {
66          return CollectionUtils.find(
67              modelPackage.getCore().getModelElement().refAllOfType(),
68              new Predicate()
69              {
70                  public boolean evaluate(Object object)
71                  {
72                      return StringUtils.trimToEmpty(((ModelElement)object).getName()).equals(name);
73                  }
74              });
75      }
76  
77      /**
78       * Finds and returns the first model element having the given
79       * <code>name</code> in the <code>umlPackage</code>, returns
80       * <code>null</code> if not found.
81       *
82       * @param umlPackage The modelPackage to search
83       * @param name the name to find.
84       * @return the found model element.
85       */
86      public static Object find(
87          org.omg.uml.modelmanagement.UmlPackage umlPackage,
88          final String name)
89      {
90          return CollectionUtils.find(
91              umlPackage.getOwnedElement(),
92              new Predicate()
93              {
94                  public boolean evaluate(Object object)
95                  {
96                      return StringUtils.trimToEmpty(((ModelElement)object).getName()).equals(name);
97                  }
98              });
99      }
100 
101     /**
102      * Finds the model element having the <code>name</code> contained within
103      * the <code>elements</code>, returns null if it can't be found.
104      *
105      * @param elements the collection of model elements to search
106      * @param name the name of the model element.
107      * @return the found model element or null if not found.
108      */
109     private static Object getElement(
110         Collection elements,
111         final String name)
112     {
113         return CollectionUtils.find(
114             elements,
115             new Predicate()
116             {
117                 public boolean evaluate(Object object)
118                 {
119                     return StringUtils.trimToEmpty(((ModelElement)object).getName()).equals(name);
120                 }
121             });
122     }
123 }