1 package org.andromda.core.translation;
2
3 import org.andromda.core.common.AndroMDALogger;
4 import org.andromda.core.common.ExceptionUtils;
5 import org.andromda.core.namespace.NamespaceComponents;
6 import org.andromda.core.translation.library.LibraryTranslation;
7 import org.andromda.core.translation.library.LibraryTranslationFinder;
8 import org.apache.log4j.Logger;
9
10 /**
11 * The <strong>expression </strong> translator class that all translations are performed through. This is the entry
12 * point to expression (OCL, etc) translation.
13 *
14 * @author Chad Brandon
15 * @author Bob Fields
16 */
17 public class ExpressionTranslator
18 {
19 private static final Logger logger = Logger.getLogger(ExpressionTranslator.class);
20 private static final ExpressionTranslator translator = new ExpressionTranslator();
21
22 /**
23 * Gets the shared ExpressionTranslator instance.
24 *
25 * @return ExpressionTranslator.
26 */
27 public static ExpressionTranslator instance()
28 {
29 return translator;
30 }
31
32 /**
33 * Initializes the ExpressionTranslator. This <strong>MUST </strong> be called to find and loal all available
34 * translation-libraries.
35 */
36 public void initialize()
37 {
38 // configure the logger
39 AndroMDALogger.initialize();
40
41 // discover plugins
42 NamespaceComponents.instance().discover();
43 }
44
45 /**
46 * Performs translation of the <code>expression</code> by looking up the
47 * <code>translationName</code> from the available Translation-Libraries
48 * found on the classpath.
49 *
50 * @param translationName the name of the translation to use for translating
51 * (i.e. a translationName like 'query.EJB-QL' would mean use the
52 * <code>EJB-QL</code> translation from the <code>query</code>
53 * library.
54 * @param expression the actual expression to translate.
55 * @param contextElement the element which provides the context of this
56 * expression. This is passed from the model. This can be null.
57 * @return Expression the resulting expression instance which contains the
58 * translated expression as well as additional information about the
59 * expression.
60 */
61 public Expression translate(
62 final String translationName,
63 final String expression,
64 final Object contextElement)
65 {
66 ExceptionUtils.checkEmpty("translationName", translationName);
67 ExceptionUtils.checkEmpty("expression", expression);
68
69 Expression translatedExpression = null;
70 try
71 {
72 final LibraryTranslation libraryTranslation =
73 LibraryTranslationFinder.findLibraryTranslation(translationName);
74
75 if (libraryTranslation != null)
76 {
77 final Translator translator = libraryTranslation.getTranslator();
78 translatedExpression = translator.translate(translationName, expression, contextElement);
79 }
80 else
81 {
82 logger.error("ERROR! No translation found with name --> '" + translationName + '\'');
83 }
84 }
85 catch (final Throwable throwable)
86 {
87 throw new TranslatorException(throwable);
88 }
89 return translatedExpression;
90 }
91 }