View Javadoc

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