svn commit: r770973 - in /ofbiz/trunk/framework/base/src/org/ofbiz/base/util: collections/FlexibleMapAccessor.java string/FlexibleStringExpander.java string/UelUtil.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r770973 - in /ofbiz/trunk/framework/base/src/org/ofbiz/base/util: collections/FlexibleMapAccessor.java string/FlexibleStringExpander.java string/UelUtil.java

adrianc
Author: adrianc
Date: Sat May  2 18:04:21 2009
New Revision: 770973

URL: http://svn.apache.org/viewvc?rev=770973&view=rev
Log:
UEL improvements:

1. Implemented operator substitution suggested by Jacopo and Scott. It is a subset of the beanshell operator substitution - http://www.beanshell.org/manual/syntax.html#Document_Friendly_Entities.

2. Added an extension to UEL that permits using variables that might not exist in expressions. Example: The expression ${foo + bar} will throw an exception if either foo or bar don't exist. Assuming foo and bar are both intended to be integers, the expression ${foo$integer + bar$integer} will not throw an exception. Instead, Integer instances (initialized to zero) will be created for the missing variables.

Modified:
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/FlexibleMapAccessor.java
    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/collections/FlexibleMapAccessor.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/FlexibleMapAccessor.java?rev=770973&r1=770972&r2=770973&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/FlexibleMapAccessor.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/FlexibleMapAccessor.java Sat May  2 18:04:21 2009
@@ -138,7 +138,7 @@
                 Debug.logVerbose("UEL exception while getting value: " + e + ", original = " + this.original, module);
             }
         } catch (Exception e) {
-            Debug.logInfo("UEL exception while getting value: " + e + ", original = " + this.original, module);
+            Debug.logError("UEL exception while getting value: " + e + ", original = " + this.original, module);
         }
         return UtilGenerics.<T>cast(obj);
     }
@@ -161,7 +161,7 @@
         try {
             UelUtil.setValue(base, getExpression(base), value == null ? Object.class : value.getClass(), value);
         } catch (Exception e) {
-            Debug.logInfo("UEL exception while setting value: " + e + ", original = " + this.original, module);
+            Debug.logError("UEL exception while setting value: " + e + ", original = " + this.original, module);
         }
     }
 
@@ -181,7 +181,7 @@
             Map<String, Object> writableMap = UtilGenerics.cast(base);
             UelUtil.removeValue(writableMap, getExpression(base));
         } catch (Exception e) {
-            Debug.logInfo("UEL exception while removing value: " + e + ", original = " + this.original, module);
+            Debug.logError("UEL exception while removing value: " + e + ", original = " + this.original, module);
         }
         return object;
     }

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=770973&r1=770972&r2=770973&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 Sat May  2 18:04:21 2009
@@ -25,6 +25,7 @@
 import java.util.Locale;
 import java.util.Map;
 import java.util.TimeZone;
+import javax.el.*;
 
 import org.ofbiz.base.util.cache.UtilCache;
 import org.ofbiz.base.util.*;
@@ -356,8 +357,12 @@
                     String currencyCode = this.codeExpr.expandString(context, timeZone, locale);
                     buffer.append(UtilFormatOut.formatCurrency(new BigDecimal(obj.toString()), currencyCode, locale));
                 }
+            } catch (PropertyNotFoundException e) {
+                if (Debug.verboseOn()) {
+                    Debug.logVerbose("Error evaluating expression: " + e, module);
+                }
             } catch (Exception e) {
-                Debug.logVerbose("Error evaluating expression: " + e, module);
+                Debug.logError("Error evaluating expression: " + e, module);
             }
         }
     }
@@ -389,8 +394,12 @@
                 if (obj != null) {
                     buffer.append((String) ObjectType.simpleTypeConvert(obj, "String", null, timeZone, locale, false));
                 }
+            } catch (PropertyNotFoundException e) {
+                if (Debug.verboseOn()) {
+                    Debug.logVerbose("Error evaluating expression: " + e, module);
+                }
             } catch (Exception e) {
-                Debug.logVerbose("Error evaluating expression: " + e, module);
+                Debug.logError("Error evaluating expression: " + e, module);
             }
         }
     }
@@ -400,14 +409,18 @@
         protected final String bracketedOriginal;
         protected VarElem(String original) {
             this.original = original;
-            this.bracketedOriginal = openBracket + original + closeBracket;
+            this.bracketedOriginal = openBracket + UelUtil.prepareExpression(original) + closeBracket;
         }
         public void append(StringBuilder buffer, Map<String, ? extends Object> context, TimeZone timeZone, Locale locale) {
             Object obj = null;
             try {
                 obj = UelUtil.evaluate(context, this.bracketedOriginal);
+            } catch (PropertyNotFoundException e) {
+                if (Debug.verboseOn()) {
+                    Debug.logVerbose("Error evaluating expression " + this.original + ": " + e, module);
+                }
             } catch (Exception e) {
-                Debug.logVerbose("Error evaluating expression: " + e, module);
+                Debug.logError("Error evaluating expression " + this.original + ": " + e, module);
             }
             if (obj == null) {
                 if (this.original.startsWith("env.")) {

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=770973&r1=770972&r2=770973&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 Sat May  2 18:04:21 2009
@@ -135,20 +135,52 @@
          */
         public ValueExpression resolveVariable(String variable) {
             Object obj = null;
-            //Object obj = this.variables.get(variable);
+            String createObjectType = null;
+            String name = variable;
+            if (variable.contains("$")) {
+                if (variable.endsWith("$string")) {
+                    name = variable.substring(0, variable.length() - 7);
+                    createObjectType = "string";
+                } else if (variable.endsWith("$boolean")) {
+                    name = variable.substring(0, variable.length() - 8);
+                    createObjectType = "boolean";
+                } else if (variable.endsWith("$integer")) {
+                    name = variable.substring(0, variable.length() - 8);
+                    createObjectType = "integer";
+                } else if (variable.endsWith("$long")) {
+                    name = variable.substring(0, variable.length() - 5);
+                    createObjectType = "long";
+                } else if (variable.endsWith("$double")) {
+                    name = variable.substring(0, variable.length() - 7);
+                    createObjectType = "double";
+                }
+            }
             if (this.variables instanceof LocalizedMap) {
                 Locale locale = UtilMisc.ensureLocale(this.variables.get("locale"));
-                Object localizedObj = ((LocalizedMap) this.variables).get(variable, locale);
+                Object localizedObj = ((LocalizedMap<?>) this.variables).get(name, locale);
                 if (localizedObj == null) {
-                    localizedObj = this.variables.get(variable);
+                    localizedObj = this.variables.get(name);
                 }
                 obj = localizedObj;
             } else {
-                obj = this.variables.get(variable);
+                obj = this.variables.get(name);
             }
             if (obj != null) {
                 return new BasicValueExpression(obj);
             }
+            if (createObjectType != null) {
+                if ("string".equals(createObjectType)) {
+                    return new BasicValueExpression("");
+                } else if ("boolean".equals(createObjectType)) {
+                    return new BasicValueExpression(Boolean.FALSE);
+                } else if ("integer".equals(createObjectType)) {
+                    return new BasicValueExpression(Integer.valueOf(0));
+                } else if ("long".equals(createObjectType)) {
+                    return new BasicValueExpression(Long.valueOf(0));
+                } else if ("double".equals(createObjectType)) {
+                    return new BasicValueExpression(Double.valueOf(0));
+                }
+            }
             return null;
         }
         public ValueExpression setVariable(String variable, ValueExpression expression) {
@@ -334,23 +366,41 @@
         }
     }
 
-    /** Prepares an expression for evaluation by UEL. The OFBiz syntax is
+    /** Prepares an expression for evaluation by UEL.<p>The OFBiz syntax is
      * converted to UEL-compatible syntax and the resulting expression is
-     * returned.
+     * returned. OFBiz syntax provides special forms of common operators to make
+     * it easier to embed UEL expressions in XML:
+     * <table border="1" cellpadding="2">
+     * <tr><td><strong>@gt</strong></td><td>&gt;</td></tr>
+     * <tr><td><strong>@lt</strong></td><td>&lt;</td></tr>
+     * <tr><td><strong>@lteq</strong></td><td>&lt;=</td></tr>
+     * <tr><td><strong>@gteq</strong></td><td>&gt;=</td></tr>
+     * <tr><td><strong>@or</strong></td><td>||</td></tr>
+     * <tr><td><strong>@and</strong></td><td>&amp;&amp;</td></tr>
+     * </table></p>
      * @param expression Expression to be converted
      * @return Converted expression
      */
     public static String prepareExpression(String expression) {
         String result = expression;
-        int openBrace = expression.indexOf("[+");
-        int closeBrace = (openBrace == -1 ? -1 : expression.indexOf(']', openBrace));
+        result = result.replace("[]", "['add']");
+        if (result.contains("@")) {
+            // TODO: create a static Pattern instance and use a Matcher
+            result = result.replace("@or", "||");
+            result = result.replace("@and", "&&");
+            result = result.replace("@lteq", "<=");
+            result = result.replace("@gteq", ">=");
+            result = result.replace("@lt", "<");
+            result = result.replace("@gt", ">");
+        }
+        int openBrace = result.indexOf("[+");
+        int closeBrace = (openBrace == -1 ? -1 : result.indexOf(']', openBrace));
         if (closeBrace != -1) {
-            String base = expression.substring(0, openBrace);
-            String property = expression.substring(openBrace+2, closeBrace).trim();
-            String end = expression.substring(closeBrace + 1);
+            String base = result.substring(0, openBrace);
+            String property = result.substring(openBrace+2, closeBrace).trim();
+            String end = result.substring(closeBrace + 1);
             result = base + "['insert@" + property + "']" + end;
         }
-        result = result.replace("[]", "['add']");
         return result;
     }
 }