1 /* *************************************************************************** 2 * Copyright (c) 2008 Brabenetz Harald, Austria. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 *****************************************************************************/ 17 18 package org.settings4j.util; 19 20 import java.util.Map; 21 22 import org.apache.taglibs.standard.lang.jstl.ELEvaluator; 23 import org.apache.taglibs.standard.lang.jstl.ELException; 24 import org.apache.taglibs.standard.lang.jstl.VariableResolver; 25 26 27 /** 28 * A Simple Variable Resolver for ExpressionLanguage. With the JSTLVariableResolver you need a PageContext-Object with 29 * HttpRequest etc.... This class is a Simple Variant of the JSTLVariableResolver who accept a Simple Map as 30 * PageContext. Example: 1. create a Map with Values(key="name", value="Herr Mustermann") 2. parse the String 31 * "Hello, ${name}" with the given Map 3. result: "Hello, Herr Mustermann". 32 * 33 * @see org.apache.taglibs.standard.lang.jstl.JSTLVariableResolver 34 * @author Harald.Brabenetz 35 */ 36 public final class ExpressionLanguageUtil { 37 38 /** Hide Constructor (Utility Pattern).*/ 39 private ExpressionLanguageUtil() { 40 super(); 41 } 42 /** 43 * The VariableResolver for a <code>java.util.Map</code> as Context-Object. 44 */ 45 private static final VariableResolver SIMPLE_VARIABLE_RESOLVER = new VariableResolver() { 46 47 /** 48 * Resolves the specified variable within the given context. Returns null if the variable is not found. 49 **/ 50 public Object resolveVariable(final String pName, final Object simpleMap) { 51 final Map ctx = (Map) simpleMap; 52 return ctx.get(pName); 53 } 54 }; 55 56 private static final ELEvaluator SIMPLE_EVALUATOR = new ELEvaluator(SIMPLE_VARIABLE_RESOLVER); 57 58 /** 59 * Helper-Function to evaluate a Expression to a String. 60 * 61 * @param expression The Expression to be evaluated 62 * @param context The context where the expression has Access 63 * @return The Result as String 64 * @throws ELException Throw an Exception if the expression is not valid. 65 */ 66 public static String evaluateExpressionLanguage(final String expression, final Map context) throws ELException { 67 return (String) evaluateExpressionLanguage(expression, context, String.class); 68 } 69 70 /** 71 * Helper-Function to evaluate a Expression to a given Class. 72 * 73 * @param expression The Expression to be evaluated 74 * @param context The context where the expression has Access 75 * @param returnType the Class-Object who will be returned 76 * @return The Result as the given return-Type 77 * @throws ELException Throw an Exception if the expression is not valid. 78 */ 79 public static Object evaluateExpressionLanguage(final String expression, final Map context, final Class returnType) 80 throws ELException { 81 return SIMPLE_EVALUATOR.evaluate(expression, context, returnType, null, null); 82 } 83 84 }