1 package org.andromda.metafacades.uml;
2
3 import org.andromda.core.mapping.Mapping;
4 import org.andromda.core.mapping.Mappings;
5 import org.apache.commons.lang.StringUtils;
6
7 /**
8 * <p/>
9 * An object responsible for mapping types in the object model to other language identifiers/types. (For example, Java,
10 * SQL, Jdbc, etc). Basically just wraps the core {@link Mappings} instance in order to provide type specific mapping
11 * functionality. </p>
12 *
13 * @author Chad Brandon
14 * @author Bob Fields
15 */
16 public final class TypeMappings
17 {
18 /**
19 * The contained mappings instance.
20 */
21 private Mappings mappings = null;
22
23 /**
24 * The constructor that takes the {@link Mappings} instance.
25 *
26 * @param mappings the Mapping instance from which to construct this TypeMappings instance.
27 */
28 private TypeMappings(Mappings mappings)
29 {
30 this.mappings = mappings;
31 }
32
33 /**
34 * Sets the optional suffix used for array types (if
35 *
36 * @param arraySuffix the suffix used for arrays within this mapping.
37 */
38 public void setArraySuffix(String arraySuffix)
39 {
40 this.arraySuffix = arraySuffix;
41 }
42
43 /**
44 * Returns a new configured instance of this TypeMappings configured from the mappings instance.
45 *
46 * @param mappings the Mappings instance.
47 * @return TypeMappings the configured TypeMappings instance.
48 */
49 public static TypeMappings getInstance(Mappings mappings)
50 {
51 return new TypeMappings(mappings);
52 }
53
54 /**
55 * Returns a new configured instance of this TypeMappings configured from the mappings instance.
56 *
57 * @param mappingsUri the URI to configure the underlying {@link Mappings} instance.
58 * @return TypeMappings the configured TypeMappings instance.
59 */
60 public static TypeMappings getInstance(String mappingsUri)
61 {
62 return TypeMappings.getInstance(Mappings.getInstance(mappingsUri));
63 }
64
65 /**
66 * The suffix appended to array types is set from {@link #setArraySuffix(String)}.
67 */
68 private String arraySuffix = null;
69
70 /**
71 * Returns the <code>to</code> mapping from a given <code>from</code> mapping.
72 *
73 * @param from the <code>from</code> mapping, this is the type/identifier that is in the model.
74 * @return String to the <code>to</code> mapping (this is the mapping that can be retrieved if a corresponding
75 * 'from' is found.
76 */
77 public String getTo(String from)
78 {
79 from = StringUtils.trimToEmpty(from);
80 String initialFrom = from;
81
82 String to = null;
83
84 // first we check to see if there's an array
85 // type mapping directly defined in the mappings
86 Mapping mapping = this.mappings.getMapping(from);
87 if (mapping == null && arraySuffix != null)
88 {
89 // if there is no mapping, remove the array suffix and
90 // check for the mapping without the suffix.
91 // if the from has an array suffix, then strip the array off
92 // so we can find the mapping
93 boolean isArray = from.endsWith(arraySuffix);
94 if (isArray)
95 {
96 from = StringUtils.replace(from, arraySuffix, "");
97 }
98 mapping = this.mappings.getMapping(from);
99 if (mapping != null)
100 {
101 StringBuilder toBuffer = new StringBuilder(mapping.getTo());
102 if (isArray)
103 {
104 // append the suffix back to the return value;
105 toBuffer.append(arraySuffix);
106 }
107 to = toBuffer.toString();
108 }
109 }
110 else if (mapping != null)
111 {
112 to = mapping.getTo();
113 }
114
115 if (to == null)
116 {
117 to = initialFrom;
118 }
119 return StringUtils.trimToEmpty(to);
120 }
121
122 /**
123 * Returns the Mappings instance which this TypeMapping wraps.
124 *
125 * @return URL
126 */
127 public Mappings getMappings()
128 {
129 return this.mappings;
130 }
131 }