Author: adrianc
Date: Mon Dec 8 10:00:22 2008 New Revision: 724428 URL: http://svn.apache.org/viewvc?rev=724428&view=rev Log: More work on the Unified Expression Language code: 1. Renamed some things in UelUtil.java to make them more understandable 2. Moved exception handling out of UelUtil.java and into calling methods 3. Added missing UEL implementation to FlexibleStringExpander.java Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/UelUtil.java Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java?rev=724428&r1=724427&r2=724428&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java Mon Dec 8 10:00:22 2008 @@ -29,7 +29,6 @@ import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.ObjectType; import org.ofbiz.base.util.cache.UtilCache; -import org.ofbiz.base.util.collections.FlexibleMapAccessor; import org.ofbiz.base.util.UtilDateTime; import org.ofbiz.base.util.UtilFormatOut; import org.ofbiz.base.util.UtilMisc; @@ -280,7 +279,7 @@ } protected static class BshElem implements StrElem { - String str; + protected String str; protected BshElem(String scriptlet) { this.str = scriptlet; } @@ -305,20 +304,23 @@ } protected static class CurrElem implements StrElem { - String str; + protected String valueStr; protected FlexibleStringExpander codeExpr = null; protected CurrElem(String original) { int currencyPos = original.indexOf("?currency("); - int closeBracket = original.indexOf(")", currencyPos+10); - this.codeExpr = FlexibleStringExpander.getInstance(original.substring(currencyPos+10, closeBracket)); - this.str = original.substring(0, currencyPos); + int closeParen = original.indexOf(")", currencyPos + 10); + this.codeExpr = FlexibleStringExpander.getInstance(original.substring(currencyPos + 10, closeParen)); + this.valueStr = openBracket + original.substring(0, currencyPos) + closeBracket; } public void append(StringBuilder buffer, Map<String, ? extends Object> context, TimeZone timeZone, Locale locale) { - FlexibleMapAccessor<Object> fma = new FlexibleMapAccessor<Object>(this.str); - Object obj = fma.get(context, locale); - if (obj != null) { - String currencyCode = this.codeExpr.expandString(context, timeZone, locale); - buffer.append(UtilFormatOut.formatCurrency(Double.valueOf(obj.toString()), currencyCode, locale)); + try { + Object obj = UelUtil.evaluate(context, this.valueStr); + if (obj != null) { + String currencyCode = this.codeExpr.expandString(context, timeZone, locale); + buffer.append(UtilFormatOut.formatCurrency(Double.valueOf(obj.toString()), currencyCode, locale)); + } + } catch (Exception e) { + Debug.logVerbose("Error evaluating expression: " + e, module); } } } @@ -345,35 +347,37 @@ this.hint = expr.length(); } } - Object obj = UelUtil.evaluate(context, openBracket + expr.toString() + closeBracket); - if (obj != null) { - try { - buffer.append((String) ObjectType.simpleTypeConvert(obj, "String", null, timeZone, locale, true)); - } catch (Exception e) { - buffer.append(obj); + try { + Object obj = UelUtil.evaluate(context, openBracket + expr.toString() + closeBracket); + if (obj != null) { + buffer.append((String) ObjectType.simpleTypeConvert(obj, "String", null, timeZone, locale, false)); } + } catch (Exception e) { + Debug.logVerbose("Error evaluating expression: " + e, module); } } } protected static class VarElem implements StrElem { protected String original = null; + protected String bracketedOriginal = null; protected VarElem(String original) { this.original = original; + this.bracketedOriginal = openBracket + original + closeBracket; } public void append(StringBuilder buffer, Map<String, ? extends Object> context, TimeZone timeZone, Locale locale) { - Object obj = UelUtil.evaluate(context, openBracket + this.original + closeBracket); - if (obj == null) { - if (this.original.startsWith("env.")) { - obj = System.getProperty(this.original.substring(4)); + try { + Object obj = UelUtil.evaluate(context, this.bracketedOriginal); + if (obj == null) { + if (this.original.startsWith("env.")) { + obj = System.getProperty(this.original.substring(4)); + } } - } - if (obj != null) { - try { - buffer.append((String) ObjectType.simpleTypeConvert(obj, "String", null, timeZone, locale, true)); - } catch (Exception e) { - buffer.append(obj); + if (obj != null) { + buffer.append((String) ObjectType.simpleTypeConvert(obj, "String", null, timeZone, locale, false)); } + } catch (Exception e) { + Debug.logVerbose("Error evaluating expression: " + e, module); } } } Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/UelUtil.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/UelUtil.java?rev=724428&r1=724427&r2=724428&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/UelUtil.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/UelUtil.java Mon Dec 8 10:00:22 2008 @@ -24,8 +24,6 @@ import javolution.util.FastMap; -import org.ofbiz.base.util.Debug; - /** Implements the Unified Expression Language (JSR-245). */ public class UelUtil { @@ -46,24 +44,18 @@ * @param context Evaluation context (variables) * @param expression UEL expression * @return Result object + * @throws Various <code>javax.el.*</code> exceptions */ public static Object evaluate(Map<String, ? extends Object> context, String expression) { - Object obj = null; - try { - ELContext elContext = new BasicContext(context); - ValueExpression ve = exprFactory.createValueExpression(elContext, expression, Object.class); - obj = ve.getValue(elContext); - } catch (Exception e) { - Debug.logVerbose("Error evaluating expression: " + e, module); - } - return obj; + ELContext elContext = new BasicContext(context); + ValueExpression ve = exprFactory.createValueExpression(elContext, expression, Object.class); + return ve.getValue(elContext); } protected static class BasicContext extends ELContext { - protected VariableMapper variables = null; - protected BasicContext() {} + protected final VariableMapper variableMapper; public BasicContext(Map<String, ? extends Object> context) { - this.variables = new Variables(context); + this.variableMapper = new BasicVariableMapper(context, this); } public ELResolver getELResolver() { return defaultResolver; @@ -72,22 +64,24 @@ return functionMapper; } public VariableMapper getVariableMapper() { - return this.variables; + return this.variableMapper; } - protected class Variables extends VariableMapper { - protected Map<String, Object> context = FastMap.newInstance(); - protected Variables(Map<String, ? extends Object> context) { - this.context.putAll(context); + protected class BasicVariableMapper extends VariableMapper { + protected ELContext elContext; + protected Map<String, Object> variables = FastMap.newInstance(); + protected BasicVariableMapper(Map<String, ? extends Object> context, ELContext parentContext) { + this.variables.putAll(context); + this.elContext = parentContext; } public ValueExpression resolveVariable(String variable) { - Object obj = this.context.get(variable); + Object obj = this.variables.get(variable); if (obj != null) { return new BasicValueExpression(obj); } return null; } public ValueExpression setVariable(String variable, ValueExpression expression) { - return new BasicValueExpression(this.context.put(variable, expression.getValue(null))); + return new BasicValueExpression(this.variables.put(variable, expression.getValue(this.elContext))); } } @SuppressWarnings("serial") |
Free forum by Nabble | Edit this page |