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
13
14
15
16 public class ModelElementFinder
17 {
18
19
20
21
22
23
24
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
55
56
57
58
59
60
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
79
80
81
82
83
84
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
103
104
105
106
107
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 }