1 package org.andromda.translation.ocl;
2
3 import java.io.IOException;
4 import java.io.PushbackReader;
5 import java.io.StringReader;
6 import java.util.HashMap;
7 import java.util.Map;
8 import org.andromda.core.common.ExceptionUtils;
9 import org.andromda.core.translation.Expression;
10 import org.andromda.core.translation.TranslationUtils;
11 import org.andromda.core.translation.Translator;
12 import org.andromda.core.translation.TranslatorException;
13 import org.andromda.core.translation.library.LibraryTranslation;
14 import org.andromda.core.translation.library.LibraryTranslationFinder;
15 import org.andromda.translation.ocl.analysis.DepthFirstAdapter;
16 import org.andromda.translation.ocl.lexer.Lexer;
17 import org.andromda.translation.ocl.lexer.LexerException;
18 import org.andromda.translation.ocl.node.AClassifierContextDeclaration;
19 import org.andromda.translation.ocl.node.ADefClassifierExpressionBody;
20 import org.andromda.translation.ocl.node.AInvClassifierExpressionBody;
21 import org.andromda.translation.ocl.node.AOperationContextDeclaration;
22 import org.andromda.translation.ocl.node.AOperationExpressionBody;
23 import org.andromda.translation.ocl.node.Start;
24 import org.andromda.translation.ocl.parser.OclParser;
25 import org.andromda.translation.ocl.parser.OclParserException;
26 import org.andromda.translation.ocl.parser.ParserException;
27 import org.andromda.translation.ocl.syntax.ConcreteSyntaxUtils;
28 import org.andromda.translation.ocl.syntax.OperationDeclaration;
29 import org.apache.log4j.Logger;
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public abstract class BaseTranslator
44 extends DepthFirstAdapter
45 implements Translator
46 {
47
48
49
50 protected Logger logger = Logger.getLogger(this.getClass());
51
52
53
54
55
56 private Object contextElement = null;
57
58
59
60
61 private Expression translatedExpression = null;
62
63
64
65
66 private LibraryTranslation libraryTranslation = null;
67
68
69
70
71
72
73 private void setContextElement(Object contextElement)
74 {
75 this.contextElement = contextElement;
76 }
77
78
79
80
81
82
83 public Expression getExpression()
84 {
85 final String methodName = "BaseTranslator.getExpression";
86 if (this.translatedExpression == null)
87 {
88 throw new TranslatorException(methodName + " - translatedExpression can not be null");
89 }
90 return translatedExpression;
91 }
92
93
94
95
96
97
98 public Object getContextElement()
99 {
100 String methodName = "getContextElement";
101 if (this.contextElement == null)
102 {
103 throw new TranslatorException(methodName + " - the contextElement can not be null");
104 }
105 return this.contextElement;
106 }
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152 protected void handleTranslationFragment(Object node)
153 {
154 ExceptionUtils.checkNull("node", node);
155 if (this.libraryTranslation != null)
156 {
157 this.libraryTranslation.handleTranslationFragment(TranslationUtils.trimToEmpty(node), this.getExpression()
158 .getKind(), node);
159 }
160 }
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186 protected String getTranslationFragment(String fragmentName)
187 {
188 ExceptionUtils.checkEmpty("fragmentName", fragmentName);
189 String fragmentString = null;
190 if (this.libraryTranslation != null)
191 {
192 fragmentString = this.libraryTranslation.getTranslationFragment(fragmentName,
193 this.getExpression().getKind());
194 }
195 return fragmentString;
196 }
197
198
199
200
201
202 public void preProcess()
203 {
204 this.contextElement = null;
205 }
206
207
208
209
210
211
212
213
214
215
216
217
218
219 public Expression translate(String translationName, String expression, Object contextElement)
220 {
221 ExceptionUtils.checkEmpty("translationName", translationName);
222 ExceptionUtils.checkEmpty("expression", expression);
223 try
224 {
225
226 this.preProcess();
227
228
229 this.setContextElement(contextElement);
230 Map<String, Object> templateObjects = new HashMap<String, Object>();
231 this.libraryTranslation = LibraryTranslationFinder.findLibraryTranslation(translationName);
232 final String variable = this.libraryTranslation.getVariable();
233 if (variable != null)
234 {
235 templateObjects.put(variable, contextElement);
236 }
237 if (this.libraryTranslation != null)
238 {
239 libraryTranslation.getLibrary().initialize();
240 libraryTranslation.processTranslation(templateObjects);
241 this.process(expression);
242 libraryTranslation.getLibrary().shutdown();
243 }
244
245 this.postProcess();
246 }
247 catch (Exception ex)
248 {
249 String errMsg = "Error translating with translation '" + translationName + "'," + " contextElement '" +
250 contextElement +
251 "' and expression --> '" +
252 expression +
253 '\'' +
254 "\nMESSAGE --> '" +
255 ex.getMessage() +
256 '\'';
257 logger.error(errMsg);
258 throw new TranslatorException(errMsg, ex);
259 }
260 return translatedExpression;
261 }
262
263
264
265
266
267
268
269 protected void process(final String expression) throws IOException
270 {
271 ExceptionUtils.checkEmpty("expression", expression);
272 try
273 {
274 Lexer lexer = new Lexer(new PushbackReader(new StringReader(expression)));
275 OclParser parser = new OclParser(lexer);
276 Start startNode = parser.parse();
277 this.translatedExpression = new Expression(expression);
278 startNode.apply(this);
279 }
280 catch (ParserException ex)
281 {
282 throw new OclParserException(ex.getMessage());
283 }
284 catch (LexerException ex)
285 {
286 throw new OclParserException(ex.getMessage());
287 }
288 }
289
290
291
292
293
294 public abstract void postProcess();
295
296
297
298
299
300
301
302
303
304 public void inAInvClassifierExpressionBody(AInvClassifierExpressionBody expressionBody)
305 {
306 ExceptionUtils.checkNull("expressionBody", expressionBody);
307 if (this.translatedExpression != null)
308 {
309 this.translatedExpression.setName(TranslationUtils.trimToEmpty(expressionBody.getName()));
310 this.translatedExpression.setKind(ExpressionKinds.INV);
311 }
312 }
313
314
315
316
317
318
319
320 public void inADefClassifierExpressionBody(ADefClassifierExpressionBody expressionBody)
321 {
322 ExceptionUtils.checkNull("expressionBody", expressionBody);
323 if (this.translatedExpression != null)
324 {
325 this.translatedExpression.setName(TranslationUtils.trimToEmpty(expressionBody.getName()));
326 this.translatedExpression.setKind(ExpressionKinds.DEF);
327 }
328 }
329
330
331
332
333
334
335
336 public void inAOperationExpressionBody(AOperationExpressionBody operationExpressionBody)
337 {
338 ExceptionUtils.checkNull("operationExpressionBody", operationExpressionBody);
339
340 if (this.translatedExpression != null)
341 {
342
343 this.translatedExpression.setName(TranslationUtils.getPropertyAsString(operationExpressionBody, "name"));
344
345
346 this.translatedExpression.setKind(TranslationUtils.getPropertyAsString(operationExpressionBody,
347 "operationStereotype"));
348 }
349 }
350
351
352
353
354
355
356
357
358 public void inAOperationContextDeclaration(AOperationContextDeclaration declaration)
359 {
360 final String methodName = "BaseTranslator.inAOperationContextDeclaration";
361 if (logger.isDebugEnabled())
362 {
363 logger.debug("performing " + methodName + " with declaration --> " + declaration);
364 }
365 if (this.translatedExpression != null)
366 {
367 this.translatedExpression.setContextElement(ConcreteSyntaxUtils.getType(declaration.getName(),
368 declaration.getPathNameTail()));
369 }
370 this.operation = ConcreteSyntaxUtils.getOperationDeclaration(declaration.getOperation());
371 }
372
373
374
375
376 private OperationDeclaration operation;
377
378
379
380
381
382
383 protected OperationDeclaration getOperation()
384 {
385 return this.operation;
386 }
387
388
389
390
391
392
393
394
395 protected boolean isOperationArgument(final String argument)
396 {
397 return this.operation != null && ConcreteSyntaxUtils.getArgumentNames(operation.getArguments()).contains(
398 argument);
399 }
400
401
402
403
404
405
406
407
408 public void inAClassifierContextDeclaration(AClassifierContextDeclaration declaration)
409 {
410 final String methodName = "BaseTranslator.inAClassifierContextDeclaration";
411 if (logger.isDebugEnabled())
412 {
413 logger.debug("performing " + methodName + " with declaration --> " + declaration);
414 }
415 if (this.translatedExpression != null)
416 {
417 this.translatedExpression.setContextElement(ConcreteSyntaxUtils.getType(declaration.getName(),
418 declaration.getPathNameTail()));
419 }
420 }
421 }