01: package org.andromda.core.translation;
02:
03: import org.andromda.core.common.AndroMDALogger;
04: import org.andromda.core.common.ExceptionUtils;
05: import org.andromda.core.namespace.NamespaceComponents;
06: import org.andromda.core.translation.library.LibraryTranslation;
07: import org.andromda.core.translation.library.LibraryTranslationFinder;
08: import org.apache.log4j.Logger;
09:
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: */
16: public class ExpressionTranslator {
17: private static Logger logger = Logger
18: .getLogger(ExpressionTranslator.class);
19: private static ExpressionTranslator translator = new ExpressionTranslator();
20:
21: /**
22: * Gets the shared ExpressionTranslator instance.
23: *
24: * @return ExpressionTranslator.
25: */
26: public static ExpressionTranslator instance() {
27: return translator;
28: }
29:
30: /**
31: * Initializes the ExpressionTranslator. This <strong>MUST </strong> be called to find and loal all available
32: * translation-libraries.
33: */
34: public void initialize() {
35: // configure the logger
36: AndroMDALogger.initialize();
37:
38: // discover plugins
39: NamespaceComponents.instance().discover();
40: }
41:
42: /**
43: * Performs translation of the <code>expression</code> by looking up the
44: * <code>translationName</code> from the available Translation-Libraries
45: * found on the classpath.
46: *
47: * @param translationName the name of the translation to use for translating
48: * (i.e. a translationName like 'query.EJB-QL' would mean use the
49: * <code>EJB-QL</code> translation from the <code>query</code>
50: * library.
51: * @param expression the actual expression to translate.
52: * @param contextElement the element which provides the context of this
53: * expression. This is passed from the model. This can be null.
54: * @return Expression the resulting expression instance which contains the
55: * translated expression as well as additional information about the
56: * expression.
57: */
58: public Expression translate(final String translationName,
59: final String expression, final Object contextElement) {
60: ExceptionUtils.checkEmpty("translationName", translationName);
61: ExceptionUtils.checkEmpty("expression", expression);
62:
63: Expression translatedExpression = null;
64: try {
65: final LibraryTranslation libraryTranslation = LibraryTranslationFinder
66: .findLibraryTranslation(translationName);
67:
68: if (libraryTranslation != null) {
69: final Translator translator = libraryTranslation
70: .getTranslator();
71: translatedExpression = translator.translate(
72: translationName, expression, contextElement);
73: } else {
74: logger
75: .error("ERROR! No translation found with name --> '"
76: + translationName + "'");
77: }
78: } catch (final Throwable throwable) {
79: throw new TranslatorException(throwable);
80: }
81: return translatedExpression;
82: }
83: }
|