001package org.andromda.core.translation; 002 003import org.andromda.core.common.AndroMDALogger; 004import org.andromda.core.common.ExceptionUtils; 005import org.andromda.core.namespace.NamespaceComponents; 006import org.andromda.core.translation.library.LibraryTranslation; 007import org.andromda.core.translation.library.LibraryTranslationFinder; 008import org.apache.log4j.Logger; 009 010/** 011 * The <strong>expression </strong> translator class that all translations are performed through. This is the entry 012 * point to expression (OCL, etc) translation. 013 * 014 * @author Chad Brandon 015 * @author Bob Fields 016 */ 017public class ExpressionTranslator 018{ 019 private static final Logger logger = Logger.getLogger(ExpressionTranslator.class); 020 private static final ExpressionTranslator translator = new ExpressionTranslator(); 021 022 /** 023 * Gets the shared ExpressionTranslator instance. 024 * 025 * @return ExpressionTranslator. 026 */ 027 public static ExpressionTranslator instance() 028 { 029 return translator; 030 } 031 032 /** 033 * Initializes the ExpressionTranslator. This <strong>MUST </strong> be called to find and loal all available 034 * translation-libraries. 035 */ 036 public void initialize() 037 { 038 // configure the logger 039 AndroMDALogger.initialize(); 040 041 // discover plugins 042 NamespaceComponents.instance().discover(); 043 } 044 045 /** 046 * Performs translation of the <code>expression</code> by looking up the 047 * <code>translationName</code> from the available Translation-Libraries 048 * found on the classpath. 049 * 050 * @param translationName the name of the translation to use for translating 051 * (i.e. a translationName like 'query.EJB-QL' would mean use the 052 * <code>EJB-QL</code> translation from the <code>query</code> 053 * library. 054 * @param expression the actual expression to translate. 055 * @param contextElement the element which provides the context of this 056 * expression. This is passed from the model. This can be null. 057 * @return Expression the resulting expression instance which contains the 058 * translated expression as well as additional information about the 059 * expression. 060 */ 061 public Expression translate( 062 final String translationName, 063 final String expression, 064 final Object contextElement) 065 { 066 ExceptionUtils.checkEmpty("translationName", translationName); 067 ExceptionUtils.checkEmpty("expression", expression); 068 069 Expression translatedExpression = null; 070 try 071 { 072 final LibraryTranslation libraryTranslation = 073 LibraryTranslationFinder.findLibraryTranslation(translationName); 074 075 if (libraryTranslation != null) 076 { 077 final Translator translator = libraryTranslation.getTranslator(); 078 translatedExpression = translator.translate(translationName, expression, contextElement); 079 } 080 else 081 { 082 logger.error("ERROR! No translation found with name --> '" + translationName + '\''); 083 } 084 } 085 catch (final Throwable throwable) 086 { 087 throw new TranslatorException(throwable); 088 } 089 return translatedExpression; 090 } 091}