View Javadoc

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