01: package org.andromda.metafacades.uml;
02:
03: import java.util.regex.Matcher;
04: import java.util.regex.Pattern;
05:
06: import org.apache.commons.collections.CollectionUtils;
07: import org.apache.commons.collections.Predicate;
08: import org.apache.commons.lang.StringUtils;
09:
10: /**
11: * Contains utilities that are common to the UML metafacades.
12: *
13: * @author Chad Brandon
14: */
15: public class UMLMetafacadeUtils {
16: /**
17: * Returns true or false depending on whether or not this Classifier or any of its specializations is of the given
18: * type having the specified <code>typeName</code>
19: *
20: * @param typeName the name of the type (i.e. datatype::Collection)
21: * @return true/false
22: */
23: public static boolean isType(ClassifierFacade classifier,
24: String typeName) {
25: boolean isType = false;
26: if (classifier != null && typeName != null) {
27: final String type = StringUtils.trimToEmpty(typeName);
28: String name = StringUtils.trimToEmpty(classifier
29: .getFullyQualifiedName(true));
30: isType = name.equals(type);
31: // if this isn't a type defined by typeName, see if we can find any
32: // types that inherit from the type.
33: if (!isType) {
34: isType = CollectionUtils.find(classifier
35: .getAllGeneralizations(), new Predicate() {
36: public boolean evaluate(Object object) {
37: String name = StringUtils
38: .trimToEmpty(((ModelElementFacade) object)
39: .getFullyQualifiedName(true));
40: return name.equals(type);
41: }
42: }) != null;
43: }
44: }
45: return isType;
46: }
47:
48: /**
49: * Gets the getter prefix for a getter operation given the <code>type</code>.
50: *
51: * @param type the type from which to determine the prefix.
52: * @return the gettern prefix.
53: */
54: public static String getGetterPrefix(final ClassifierFacade type) {
55: return type != null && type.isBooleanType()
56: && type.isPrimitive() ? "is" : "get";
57: }
58:
59: /**
60: * Returns true if the passed in constraint <code>expression</code> is of type <code>kind</code>, false otherwise.
61: *
62: * @param expression the expression to check.
63: * @param kind the constraint kind (i.e. <em>inv</em>,<em>pre</em>, <em>body</em>, etc).
64: * @return true/false
65: */
66: public static boolean isConstraintKind(String expression,
67: String kind) {
68: Pattern pattern = Pattern.compile(".*\\s*"
69: + StringUtils.trimToEmpty(kind) + "\\s*\\w*\\s*:.*",
70: Pattern.DOTALL);
71: Matcher matcher = pattern.matcher(StringUtils
72: .trimToEmpty(expression));
73: return matcher.matches();
74: }
75: }
|