svn commit: r724146 - in /ofbiz/trunk: ./ applications/product/script/org/ofbiz/product/inventory/ framework/base/lib/ framework/base/src/org/ofbiz/base/util/string/

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

svn commit: r724146 - in /ofbiz/trunk: ./ applications/product/script/org/ofbiz/product/inventory/ framework/base/lib/ framework/base/src/org/ofbiz/base/util/string/

adrianc
Author: adrianc
Date: Sun Dec  7 08:11:44 2008
New Revision: 724146

URL: http://svn.apache.org/viewvc?rev=724146&view=rev
Log:
Integrated the Unified Expression Language (JSR-245) into the FlexibleStringExpander class.

This commit also includes an example usage in InventoryServices.xml.

Added:
    ofbiz/trunk/framework/base/lib/juel-2.1.0.jar   (with props)
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/UelUtil.java   (with props)
Modified:
    ofbiz/trunk/.classpath
    ofbiz/trunk/LICENSE
    ofbiz/trunk/applications/product/script/org/ofbiz/product/inventory/InventoryServices.xml
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java

Modified: ofbiz/trunk/.classpath
URL: http://svn.apache.org/viewvc/ofbiz/trunk/.classpath?rev=724146&r1=724145&r2=724146&view=diff
==============================================================================
--- ofbiz/trunk/.classpath (original)
+++ ofbiz/trunk/.classpath Sun Dec  7 08:11:44 2008
@@ -98,6 +98,7 @@
  <classpathentry kind="lib" path="framework/base/lib/log4j-1.2.15.jar"/>
  <classpathentry kind="lib" path="framework/base/lib/junitperf.jar"/>
  <classpathentry kind="lib" path="framework/base/lib/junit.jar"/>
+    <classpathentry kind="lib" path="framework/base/lib/juel-2.1.0.jar"/>
  <classpathentry kind="lib" path="framework/base/lib/jdbm-1.0.jar"/>
  <classpathentry kind="lib" path="framework/base/lib/jakarta-regexp-1.5.jar"/>
  <classpathentry kind="lib" path="framework/base/lib/icu4j-3_6.jar"/>

Modified: ofbiz/trunk/LICENSE
URL: http://svn.apache.org/viewvc/ofbiz/trunk/LICENSE?rev=724146&r1=724145&r2=724146&view=diff
==============================================================================
--- ofbiz/trunk/LICENSE (original)
+++ ofbiz/trunk/LICENSE Sun Dec  7 08:11:44 2008
@@ -17,6 +17,7 @@
 ofbiz/trunk/framework/base/lib/avalon-util-exception-1.0.0.jar
 ofbiz/trunk/framework/base/lib/jakarta-regexp-1.5.jar
 ofbiz/trunk/framework/base/lib/jpim-0.1.jar
+ofbiz/trunk/framework/base/lib/juel-2.1.0.jar
 ofbiz/trunk/framework/base/lib/log4j-1.2.15.jar
 ofbiz/trunk/framework/base/lib/mx4j-3.0.1.jar
 ofbiz/trunk/framework/base/lib/mx4j-remote-3.0.1.jar
@@ -1929,6 +1930,7 @@
 =========================================================================
 The following library distributed with Apache OFBiz is licensed under the
 COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL):
+ofbiz/trunk/framework/base/lib/juel-2.1.0.jar (javax.el.* API)
 ofbiz/trunk/framework/base/lib/mail.jar
 =========================================================================
 COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0 1.

Modified: ofbiz/trunk/applications/product/script/org/ofbiz/product/inventory/InventoryServices.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/script/org/ofbiz/product/inventory/InventoryServices.xml?rev=724146&r1=724145&r2=724146&view=diff
==============================================================================
--- ofbiz/trunk/applications/product/script/org/ofbiz/product/inventory/InventoryServices.xml (original)
+++ ofbiz/trunk/applications/product/script/org/ofbiz/product/inventory/InventoryServices.xml Sun Dec  7 08:11:44 2008
@@ -775,7 +775,7 @@
                             <field-to-list field-name="item" list-name="reservations"/>
                         </else>
                     </if>
-                    <set field="reserveQtyAccum" value="${bsh:reserveQtyAccum + item.getDouble(&quot;quantity&quot;);}" type="Double"/>
+                    <set field="reserveQtyAccum" value="${reserveQtyAccum + item.quantity}" type="Double"/>
                 </if-empty>
                 </then>
             </if>

Added: ofbiz/trunk/framework/base/lib/juel-2.1.0.jar
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/lib/juel-2.1.0.jar?rev=724146&view=auto
==============================================================================
Binary file - no diff available.

Propchange: ofbiz/trunk/framework/base/lib/juel-2.1.0.jar
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

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=724146&r1=724145&r2=724146&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 Sun Dec  7 08:11:44 2008
@@ -36,20 +36,21 @@
 
 import bsh.EvalError;
 
-/** Expands string values within a Map context supporting the ${} syntax for
- * variable placeholders and the "." (dot) and "[]" (square-brace) syntax
- * elements for accessing Map entries and List elements in the context.
- * It Also supports the execution of bsh files by using the 'bsh:' prefix.
+/** Expands String values that contain Unified Expression Language syntax.
+ * Also supports the execution of bsh scripts by using the 'bsh:' prefix.
  * Further it is possible to control the output by specifying the suffix
  * '?currency(XXX)' to format the output according the current locale
- * and specified (XXX) currency
+ * and specified (XXX) currency.<p>This class extends the UEL by allowing
+ * nested expressions.</p>
  */
 @SuppressWarnings("serial")
 public class FlexibleStringExpander implements Serializable {
     
     public static final String module = FlexibleStringExpander.class.getName();
-    protected static UtilCache<String, FlexibleStringExpander> exprCache = new UtilCache<String, FlexibleStringExpander>("flexibleStringExpander.ExpressionCache");
-    protected static FlexibleStringExpander nullExpr = new FlexibleStringExpander(null);
+    public static final String openBracket = "${";
+    public static final String closeBracket = "}";
+    protected static final UtilCache<String, FlexibleStringExpander> exprCache = new UtilCache<String, FlexibleStringExpander>("flexibleStringExpander.ExpressionCache");
+    protected static final FlexibleStringExpander nullExpr = new FlexibleStringExpander(null);
     protected String orig;
     protected List<StrElem> strElems = null;
     protected int hint = 20;
@@ -61,7 +62,7 @@
     public FlexibleStringExpander(String original) {
         // TODO: Change this to protected, remove @deprecated javadoc comment
         this.orig = original;
-        if (original != null && original.contains("${")) {
+        if (original != null && original.contains(openBracket)) {
             this.strElems = getStrElems(original);
             if (original.length() > this.hint) {
                 this.hint = original.length();
@@ -146,7 +147,7 @@
             return nullExpr;
         }
         // Remove the next three lines to cache all expressions
-        if (!original.contains("${")) {
+        if (!original.contains(openBracket)) {
             return new FlexibleStringExpander(original);
         }
         FlexibleStringExpander fse = exprCache.get(original);
@@ -192,7 +193,7 @@
      * @return The original String expanded by replacing varaible place holders.
      */
     public static String expandString(String original, Map<String, ? extends Object> context, TimeZone timeZone, Locale locale) {
-        if (context == null || original == null || !original.contains("${")) {
+        if (context == null || original == null || !original.contains(openBracket)) {
             return original;
         }
         FlexibleStringExpander fse = FlexibleStringExpander.getInstance(original);
@@ -209,7 +210,7 @@
         }
         int origLen = original.length();
         ArrayList<StrElem> strElems = new ArrayList<StrElem>();
-        int start = original.indexOf("${");
+        int start = original.indexOf(openBracket);
         if (start == -1) {
             strElems.add(new ConstElem(original));
             strElems.trimToSize();
@@ -218,7 +219,7 @@
         int currentInd = 0;
         int end = -1;
         while (start != -1) {
-            end = original.indexOf("}", start);
+            end = original.indexOf(closeBracket, start);
             if (end == -1) {
                 Debug.logWarning("Found a ${ without a closing } (curly-brace) in the String: " + original, module);
                 break;
@@ -231,10 +232,10 @@
             if (original.indexOf("bsh:", start + 2) == start + 2) {
                 strElems.add(new BshElem(original.substring(start + 6, end)));
             } else {
-                int ptr = original.indexOf("${", start + 2);
+                int ptr = original.indexOf(openBracket, start + 2);
                 while (ptr != -1 && end != -1 && ptr < end) {
-                    end = original.indexOf("}", end + 1);
-                    ptr = original.indexOf("${", ptr + 2);
+                    end = original.indexOf(closeBracket, end + 1);
+                    ptr = original.indexOf(openBracket, ptr + 2);
                 }
                 if (end == -1) {
                     end = origLen;
@@ -243,7 +244,7 @@
                 // Evaluation sequence is important - do not change it
                 if (expression.contains("?currency(")) {
                     strElems.add(new CurrElem(expression));
-                } else if (expression.contains("${")){
+                } else if (expression.contains(openBracket)){
                     strElems.add(new NestedVarElem(expression));
                 } else {
                     strElems.add(new VarElem(expression));
@@ -254,7 +255,7 @@
             if (currentInd > origLen) {
                 currentInd = origLen;
             }
-            start = original.indexOf("${", currentInd);
+            start = original.indexOf(openBracket, currentInd);
         }
         // append the rest of the original string, ie after the last variable
         if (currentInd < origLen) {
@@ -344,8 +345,7 @@
                     this.hint = expr.length();
                 }
             }
-            FlexibleMapAccessor<Object> fma = new FlexibleMapAccessor<Object>(expr.toString());
-            Object obj = fma.get(context, locale);
+            Object obj = UelUtil.evaluate(context, openBracket + expr.toString() + closeBracket);
             if (obj != null) {
                 try {
                     buffer.append((String) ObjectType.simpleTypeConvert(obj, "String", null, timeZone, locale, true));
@@ -357,16 +357,15 @@
     }
 
     protected static class VarElem implements StrElem {
-        protected FlexibleMapAccessor<Object> fma = null;
+        protected String original = null;
         protected VarElem(String original) {
-            this.fma = new FlexibleMapAccessor<Object>(original);
+            this.original = original;
         }
         public void append(StringBuilder buffer, Map<String, ? extends Object> context, TimeZone timeZone, Locale locale) {
-            Object obj = this.fma.get(context, locale);
+            Object obj = UelUtil.evaluate(context, openBracket + this.original + closeBracket);
             if (obj == null) {
-                String key = fma.getOriginalName();
-                if (key.startsWith("env.")) {
-                    obj = System.getProperty(key.substring(4));
+                if (this.original.startsWith("env.")) {
+                    obj = System.getProperty(this.original.substring(4));
                 }
             }
             if (obj != null) {

Added: 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=724146&view=auto
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/UelUtil.java (added)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/UelUtil.java Sun Dec  7 08:11:44 2008
@@ -0,0 +1,147 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *******************************************************************************/
+package org.ofbiz.base.util.string;
+
+import java.lang.reflect.Method;
+import java.util.Map;
+import javax.el.*;
+
+import javolution.util.FastMap;
+
+/** Implements the Unified Expression Language (JSR-245). */
+public class UelUtil {
+    
+    public static final FunctionMapper functionMapper = new Functions();
+    protected static final ExpressionFactory exprFactory = new de.odysseus.el.ExpressionFactoryImpl();
+    protected static final ELResolver defaultResolver = new CompositeELResolver() {
+        {
+            add(new ArrayELResolver(false));
+            add(new ListELResolver(false));
+            add(new MapELResolver(false));
+            add(new ResourceBundleELResolver());
+            add(new BeanELResolver(false));
+        }
+    };
+
+    /** Evaluates a Unified Expression Language expression and returns the result.
+     * @param context Evaluation context (variables)
+     * @param expression UEL expression
+     * @return Result object
+     */
+    public static Object evaluate(Map<String, ? extends Object> context, String expression) {
+        ELContext elContext = new BasicContext(context);
+        ValueExpression ve = exprFactory.createValueExpression(elContext, expression, Object.class);
+        Object obj = null;
+        try {
+            obj = ve.getValue(elContext);
+        } catch (Exception e) {}
+        return obj;
+    }
+
+    protected static class BasicContext extends ELContext {
+        protected VariableMapper variables = null;
+        protected BasicContext() {}
+        public BasicContext(Map<String, ? extends Object> context) {
+            this.variables = new Variables(context);
+        }
+        public ELResolver getELResolver() {
+            return defaultResolver;
+        }
+        public FunctionMapper getFunctionMapper() {
+            return functionMapper;
+        }
+        public VariableMapper getVariableMapper() {
+            return this.variables;
+        }
+        protected class Variables extends VariableMapper {
+            protected Map<String, Object> context = FastMap.newInstance();
+            protected Variables(Map<String, ? extends Object> context) {
+                this.context.putAll(context);
+            }
+            public ValueExpression resolveVariable(String variable) {
+                Object obj = this.context.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)));
+            }
+        }
+        @SuppressWarnings("serial")
+        protected class BasicValueExpression extends ValueExpression {
+            protected Object object;
+            public BasicValueExpression(Object object) {
+                super();
+                this.object = object;
+            }
+            public boolean equals(Object obj) {
+                if (this == obj) {
+                    return true;
+                }
+                try {
+                    BasicValueExpression other = (BasicValueExpression) obj;
+                    return this.object.equals(other.object);
+                } catch (Exception e) {}
+                return false;
+            }
+            public int hashCode() {
+                return this.object == null ? 0 : this.object.hashCode();
+            }
+            public Object getValue(ELContext context) {
+                return this.object;
+            }
+            public String getExpressionString() {
+                return null;
+            }
+            public boolean isLiteralText() {
+                return false;
+            }
+            public Class<?> getType(ELContext context) {
+                return this.object == null ? null : this.object.getClass();
+            }
+            public boolean isReadOnly(ELContext context) {
+                return false;
+            }
+            public void setValue(ELContext context, Object value) {
+                this.object = value;
+            }
+            public String toString() {
+                return "ValueExpression(" + this.object + ")";
+            }
+            public Class<?> getExpectedType() {
+                return this.object == null ? null : this.object.getClass();
+            }
+        }
+    }
+
+    protected static class Functions extends FunctionMapper {
+        protected Map<String, Method> functionMap = FastMap.newInstance();
+        public void setFunction(String prefix, String localName, Method method) {
+            synchronized(this) {
+                functionMap.put(prefix + ":" + localName, method);
+            }
+        }
+        public Method resolveFunction(String prefix, String localName) {
+            return functionMap.get(prefix + ":" + localName);
+        }
+    }
+
+}

Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/UelUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/UelUtil.java
------------------------------------------------------------------------------
    svn:keywords = "Date Rev Author URL Id"

Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/UelUtil.java
------------------------------------------------------------------------------
    svn:mime-type = text/xml