1 package org.andromda.core.translation.library;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.LinkedHashMap;
6 import java.util.Map;
7 import org.andromda.core.common.ExceptionUtils;
8 import org.andromda.core.translation.TranslationUtils;
9 import org.apache.commons.lang.StringUtils;
10 import org.apache.commons.lang.builder.ToStringBuilder;
11
12
13
14
15
16
17 public class Translation
18 {
19 private String name;
20 private final Map<String, Fragment> fragments = new LinkedHashMap<String, Fragment>();
21 private final Collection<String> ignorePatterns = new ArrayList<String>();
22
23
24
25
26 private LibraryTranslation libraryTranslation;
27
28
29
30
31
32
33 protected LibraryTranslation getLibraryTranslation()
34 {
35
36 if (this.libraryTranslation == null)
37 {
38 throw new LibraryException("Translation.getLibraryTranslation - libraryTranslation can not be null");
39 }
40 return libraryTranslation;
41 }
42
43
44
45
46
47
48 protected void setLibraryTranslation(final LibraryTranslation translation)
49 {
50 libraryTranslation = translation;
51 }
52
53
54
55
56
57
58
59 protected Fragment getFragment(final String name)
60 {
61 Fragment fragment = null;
62
63
64 for (String nextName : fragments.keySet())
65 {
66 if (name.matches(nextName))
67 {
68 fragment = fragments.get(nextName);
69 }
70 }
71
72
73
74 if (fragment == null && !this.isIgnorePattern(name))
75 {
76
77
78
79
80
81
82
83
84 }
85 return fragment;
86 }
87
88
89
90
91
92
93 public void addFragment(final Fragment fragment)
94 {
95 ExceptionUtils.checkNull(
96 "fragment",
97 fragment);
98 fragment.setTranslation(this);
99 this.fragments.put(
100 fragment.getName(),
101 fragment);
102 }
103
104
105
106
107
108
109 protected String getName()
110 {
111 return name;
112 }
113
114
115
116
117 protected void setName(final String name)
118 {
119 this.name = name;
120 }
121
122
123
124
125
126
127 public void addIgnorePattern(final String ignorePattern)
128 {
129 this.ignorePatterns.add(StringUtils.trimToEmpty(ignorePattern));
130 }
131
132
133
134
135
136
137
138
139
140 public boolean isIgnorePattern(String pattern)
141 {
142 boolean isIgnorePattern = false;
143 pattern = StringUtils.trimToEmpty(pattern);
144
145
146 for (String nextIgnorePattern : this.ignorePatterns)
147 {
148 isIgnorePattern = pattern.matches(StringUtils.trimToEmpty(nextIgnorePattern));
149 if (isIgnorePattern)
150 {
151 break;
152 }
153 }
154 return isIgnorePattern;
155 }
156
157
158
159
160
161
162
163
164
165 protected String getTranslated(
166 String name,
167 String kind)
168 {
169
170 name = StringUtils.trimToEmpty(name);
171 kind = StringUtils.trimToEmpty(kind);
172
173 ExceptionUtils.checkEmpty(
174 "name",
175 name);
176
177 Fragment fragment = this.getFragment(name);
178 String translated = "";
179 if (fragment != null)
180 {
181 translated = fragment.getKind(kind);
182 String begin = "fragment{";
183 int beginLength = begin.length();
184 String end = "}";
185 for (int beginIndex = translated.indexOf(begin); beginIndex != -1;
186 beginIndex = translated.indexOf(begin))
187 {
188 String fragmentName = translated.substring(
189 beginIndex + beginLength,
190 translated.length());
191 int endIndex = fragmentName.indexOf(end);
192 if (endIndex != -1)
193 {
194 fragmentName = fragmentName.substring(
195 0,
196 endIndex);
197 }
198 StringBuilder toReplace = new StringBuilder(begin);
199 toReplace.append(fragmentName);
200 toReplace.append(end);
201 translated =
202 StringUtils.replace(
203 translated,
204 toReplace.toString(),
205 this.getTranslated(
206 fragmentName,
207 kind));
208 }
209 }
210 return TranslationUtils.removeExtraWhitespace(translated);
211 }
212
213
214
215
216 public String toString()
217 {
218 return ToStringBuilder.reflectionToString(this);
219 }
220 }