You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by ad...@apache.org on 2008/12/28 19:15:20 UTC

svn commit: r729759 - in /ofbiz/trunk/framework: base/src/org/ofbiz/base/util/collections/FlexibleMapAccessor.java minilang/src/org/ofbiz/minilang/method/ContextAccessor.java

Author: adrianc
Date: Sun Dec 28 10:15:20 2008
New Revision: 729759

URL: http://svn.apache.org/viewvc?rev=729759&view=rev
Log:
Final work on Unified Expression Language integration. L-values can contain nested expressions.

Modified:
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/FlexibleMapAccessor.java
    ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ContextAccessor.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=729759&r1=729758&r2=729759&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 Sun Dec 28 10:15:20 2008
@@ -21,10 +21,12 @@
 import java.io.Serializable;
 import java.util.Locale;
 import java.util.Map;
+import javax.el.*;
 
 import org.ofbiz.base.util.Debug;
 import org.ofbiz.base.util.UtilGenerics;
 import org.ofbiz.base.util.cache.UtilCache;
+import org.ofbiz.base.util.string.FlexibleStringExpander;
 import org.ofbiz.base.util.string.UelUtil;
 
 /**
@@ -35,18 +37,19 @@
 @SuppressWarnings("serial")
 public class FlexibleMapAccessor<T> implements Serializable {
     public static final String module = FlexibleMapAccessor.class.getName();
-    protected static final String openBracket = "${";
-    protected static final String closeBracket = "}";
     protected static final UtilCache<String, FlexibleMapAccessor<?>> fmaCache = new UtilCache<String, FlexibleMapAccessor<?>>("flexibleMapAccessor.ExpressionCache");
     @SuppressWarnings("unchecked")
     protected static final FlexibleMapAccessor nullFma = new FlexibleMapAccessor(null);
 
     protected final String original;
-    protected String bracketedOriginal;
+    protected final String bracketedOriginal;
+    protected final FlexibleStringExpander fse;
     protected boolean isAscending = true;
 
     protected FlexibleMapAccessor(String name) {
         this.original = name;
+        FlexibleStringExpander fse = null;
+        String bracketedOriginal = null;
         if (name != null && name.length() > 0) {
             if (name.charAt(0) == '-') {
                 this.isAscending = false;
@@ -55,8 +58,14 @@
                 this.isAscending = true;
                 name = name.substring(1);
             }
-            this.bracketedOriginal = openBracket + UelUtil.prepareExpression(name) + closeBracket;
+            if (name.contains(FlexibleStringExpander.openBracket)) {
+                fse = FlexibleStringExpander.getInstance(UelUtil.prepareExpression(name));
+            } else {
+                bracketedOriginal = FlexibleStringExpander.openBracket.concat(UelUtil.prepareExpression(name).concat(FlexibleStringExpander.closeBracket));
+            }
         }
+        this.bracketedOriginal = bracketedOriginal;
+        this.fse = fse;
         if (Debug.verboseOn()) {
             Debug.logVerbose("FlexibleMapAccessor created, original = " + this.original, module);
         }
@@ -122,12 +131,14 @@
         }
         Object obj = null;
         try {
-            obj = UelUtil.evaluate(base, this.bracketedOriginal);
-        } catch (Exception e) {
-            // PropertyNotFound exceptions are common, so logging verbose
+            obj = UelUtil.evaluate(base, getExpression(base));
+        } catch (PropertyNotFoundException e) {
+            // PropertyNotFound exceptions are common, so log verbose
             if (Debug.verboseOn()) {
                 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);
         }
         return UtilGenerics.<T>cast(obj);
     }
@@ -148,9 +159,9 @@
             throw new IllegalArgumentException("Cannot put a value in a null base Map");
         }
         try {
-            UelUtil.setValue(base, this.bracketedOriginal, value == null ? Object.class : value.getClass(), value);
+            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 + ", value = " + value, module);
+            Debug.logInfo("UEL exception while setting value: " + e + ", original = " + this.original, module);
         }
     }
     
@@ -168,13 +179,23 @@
         }
         try {
             Map<String, Object> writableMap = UtilGenerics.cast(base);
-            UelUtil.removeValue(writableMap, this.bracketedOriginal);
+            UelUtil.removeValue(writableMap, getExpression(base));
         } catch (Exception e) {
             Debug.logInfo("UEL exception while removing value: " + e + ", original = " + this.original, module);
         }
         return object;
     }
     
+    protected String getExpression(Map<String, ? extends Object> base) {
+        String expression = null;
+        if (this.fse != null) {
+            expression = FlexibleStringExpander.openBracket.concat(this.fse.expandString(base).concat(FlexibleStringExpander.closeBracket));
+        } else {
+            expression = this.bracketedOriginal;
+        }
+        return expression;
+    }
+
     public String toString() {
         if (this.isEmpty()) {
             return super.toString();

Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ContextAccessor.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ContextAccessor.java?rev=729759&r1=729758&r2=729759&view=diff
==============================================================================
--- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ContextAccessor.java (original)
+++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ContextAccessor.java Sun Dec 28 10:15:20 2008
@@ -32,8 +32,6 @@
 
     protected String name;
     protected FlexibleMapAccessor<T> fma;
-    protected boolean needsExpand;
-    protected boolean empty;
 
     public ContextAccessor(String name) {
         init(name);
@@ -49,34 +47,16 @@
     
     protected void init(String name) {
         this.name = name;
-        if (name == null || name.length() == 0) {
-            empty = true;
-            needsExpand = false;
-            fma = FlexibleMapAccessor.getInstance(name);
-        } else {
-            empty = false;
-            int openPos = name.indexOf("${");
-            if (openPos != -1 && name.indexOf("}", openPos) != -1) {
-                fma = null;
-                needsExpand = true;
-            } else {
-                fma = FlexibleMapAccessor.getInstance(name);
-                needsExpand = false;
-            }
-        }
+        this.fma = FlexibleMapAccessor.getInstance(name);
     }
     
     public boolean isEmpty() {
-        return this.empty;
+        return this.fma.isEmpty();
     }
     
     /** Based on name get from Map or from List in Map */
     public T get(MethodContext methodContext) {
-        if (this.needsExpand) {
-            return UtilGenerics.<T>cast(methodContext.getEnv(name));
-        } else {
-            return UtilGenerics.<T>cast(methodContext.getEnv(fma));
-        }
+        return UtilGenerics.<T>cast(methodContext.getEnv(fma));
     }
     
     /** Based on name put in Map or from List in Map;
@@ -86,30 +66,17 @@
      * number the value will inserted/added at that point instead of set at the point.
      */
     public void put(MethodContext methodContext, T value) {
-        if (this.needsExpand) {
-            methodContext.putEnv(name, value);
-        } else {
-            methodContext.putEnv(fma, value);
-        }
+        methodContext.putEnv(fma, value);
     }
     
     /** Based on name remove from Map or from List in Map */
     public T remove(MethodContext methodContext) {
-        if (this.needsExpand) {
-            return UtilGenerics.<T>cast(methodContext.removeEnv(name));
-        } else {
-            return UtilGenerics.<T>cast(methodContext.removeEnv(fma));
-        }
+        return UtilGenerics.<T>cast(methodContext.removeEnv(fma));
     }
     
     /** Based on name get from Map or from List in Map */
     public T get(Map<String, ? extends Object> theMap, MethodContext methodContext) {
-        if (this.needsExpand) {
-            FlexibleMapAccessor<T> fma = FlexibleMapAccessor.getInstance(methodContext.expandString(name));
-            return fma.get(theMap);
-        } else {
-            return fma.get(theMap);
-        }
+        return fma.get(theMap);
     }
     
     /** Based on name put in Map or from List in Map;
@@ -119,22 +86,12 @@
      * number the value will inserted/added at that point instead of set at the point.
      */
     public void put(Map<String, Object> theMap, T value, MethodContext methodContext) {
-        if (this.needsExpand) {
-            FlexibleMapAccessor<T> fma = FlexibleMapAccessor.getInstance(methodContext.expandString(name));
-            fma.put(theMap, value);
-        } else {
-            fma.put(theMap, value);
-        }
+        fma.put(theMap, value);
     }
     
     /** Based on name remove from Map or from List in Map */
     public T remove(Map<String, ? extends Object> theMap, MethodContext methodContext) {
-        if (this.needsExpand) {
-            FlexibleMapAccessor<T> fma = FlexibleMapAccessor.getInstance(methodContext.expandString(name));
-            return fma.remove(theMap);
-        } else {
-            return fma.remove(theMap);
-        }
+        return fma.remove(theMap);
     }
     
     /** The equals and hashCode methods are imnplemented just case this object is ever accidently used as a Map key */    
@@ -145,7 +102,7 @@
     /** The equals and hashCode methods are imnplemented just case this object is ever accidently used as a Map key */    
     public boolean equals(Object obj) {
         if (obj instanceof ContextAccessor) {
-            ContextAccessor contextAccessor = (ContextAccessor) obj;
+            ContextAccessor<?> contextAccessor = (ContextAccessor<?>) obj;
             if (this.name == null) {
                 return contextAccessor.name == null;
             }