1 package org.andromda.core.translation.library;
2
3 import java.util.LinkedHashMap;
4 import java.util.Map;
5 import org.andromda.core.common.ExceptionUtils;
6 import org.apache.commons.lang.StringUtils;
7 import org.apache.commons.lang.builder.ToStringBuilder;
8
9 /**
10 * A Translation "fragment" of a translation file. This fragment belongs to a Translation object.
11 *
12 * @author Chad Brandon
13 * @see org.andromda.core.translation.library.Translation
14 */
15 public class Fragment
16 {
17 private String name;
18 private String handlerMethod;
19
20 /**
21 * The Translation to which this Fragment belongs.
22 */
23 private Translation translation;
24
25 /**
26 * The possible kinds available to this Fragment
27 */
28 private Map<String, String> kinds = new LinkedHashMap<String, String>();
29
30 /**
31 * It doesn't make any sense to instatiate this object explicitly. It intended to be instantiated as part of a
32 * Translation.
33 *
34 * @see org.andromda.core.translation.library.Translation
35 */
36 public Fragment()
37 {
38 // Here for documentation purposes
39 }
40
41 /**
42 * Gets the name of this fragment
43 *
44 * @return the name of this fragment.
45 */
46 public String getName()
47 {
48 return name;
49 }
50
51 /**
52 * Sets the name of this fragment.
53 *
54 * @param name the name to set.
55 */
56 public void setName(final String name)
57 {
58 this.name = StringUtils.trimToEmpty(name);
59 }
60
61 /**
62 * Returns the kinds contained within this translation fragment
63 *
64 * @return Map the Kinds keyed by name.
65 */
66 public Map<String, String> getKinds()
67 {
68 return this.kinds;
69 }
70
71 /**
72 * Returns the body for the fragment kind with the specified name.
73 *
74 * @param name the name of the kind to get.
75 * @return FragmentKind
76 */
77 public String getKind(String name)
78 {
79 // clean the name first
80 name = StringUtils.trimToEmpty(name);
81 ExceptionUtils.checkEmpty("name", name);
82 String kind = StringUtils.trimToEmpty(kinds.get(name));
83 if (kind == null)
84 {
85 throw new LibraryException(
86 "No kind '" + name + "' could be found for the translation fragment '" +
87 this.getName() + "' check the fragment '" + this.getName() + "' in translation template --> '" +
88 getTranslation().getLibraryTranslation().getTemplate() + '\'');
89 }
90 return kind;
91 }
92
93 /**
94 * Adds the specified kind having the specified name and body to the Fragment.
95 *
96 * @param name the name of the kind of expression.
97 * @param body the body of the kind of expression.
98 */
99 public void addKind(
100 final String name,
101 final String body)
102 {
103 kinds.put(
104 StringUtils.trimToEmpty(name),
105 body);
106 }
107
108 /**
109 * Returns the name of the handler method.
110 *
111 * @return Returns the handlerMethod.
112 */
113 public String getHandlerMethod()
114 {
115 return this.handlerMethod;
116 }
117
118 /**
119 * Sets the name of the handler method. This method is the method within the Translator that handles the processing
120 * of the fragment.
121 *
122 * @param handlerMethod The handlerMethod to set.
123 * @see org.andromda.core.translation.Translator
124 */
125 public void setHandlerMethod(final String handlerMethod)
126 {
127 this.handlerMethod = handlerMethod;
128 }
129
130 /**
131 * Gets the Translation to which this Fragment belongs.
132 *
133 * @return Translation
134 */
135 public Translation getTranslation()
136 {
137 final String methodName = "Fragment.getTranslation";
138
139 // should never happen, but it doesn't hurt to be safe
140 if (this.translation == null)
141 {
142 throw new LibraryException(methodName + " - translation can not be null");
143 }
144 return translation;
145 }
146
147 /**
148 * Sets the Translation to which this Fragment belongs.
149 *
150 * @param translation
151 */
152 public void setTranslation(final Translation translation)
153 {
154 this.translation = translation;
155 }
156
157 /**
158 * @see Object#toString()
159 */
160 public String toString()
161 {
162 return ToStringBuilder.reflectionToString(this);
163 }
164 }