svn commit: r763105 - in /ofbiz/trunk/framework: base/src/org/ofbiz/base/util/GroovyUtil.java base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java example/widget/example/FormWidgetExampleForms.xml

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

svn commit: r763105 - in /ofbiz/trunk/framework: base/src/org/ofbiz/base/util/GroovyUtil.java base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java example/widget/example/FormWidgetExampleForms.xml

jonesde
Author: jonesde
Date: Wed Apr  8 05:14:23 2009
New Revision: 763105

URL: http://svn.apache.org/viewvc?rev=763105&view=rev
Log:
Small change to groovy: stuff in FlexibleStringExpander to only interpret the expression/script on the initial parse and not each time it is run; tested with the stuff in the example component

Modified:
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/GroovyUtil.java
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java
    ofbiz/trunk/framework/example/widget/example/FormWidgetExampleForms.xml

Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/GroovyUtil.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/GroovyUtil.java?rev=763105&r1=763104&r2=763105&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/GroovyUtil.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/GroovyUtil.java Wed Apr  8 05:14:23 2009
@@ -47,7 +47,7 @@
 
     public static GroovyClassLoader groovyClassLoader = new GroovyClassLoader();
 
-    private static Binding getBinding(Map<String, Object> context) {
+    public static Binding getBinding(Map<String, ? extends Object> context) {
         Binding binding = new Binding();
         if (context != null) {
             Set<String> keySet = context.keySet();

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=763105&r1=763104&r2=763105&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 Wed Apr  8 05:14:23 2009
@@ -29,6 +29,7 @@
 import org.ofbiz.base.util.cache.UtilCache;
 import org.ofbiz.base.util.*;
 import org.codehaus.groovy.control.CompilationFailedException;
+import org.codehaus.groovy.runtime.InvokerHelper;
 
 import bsh.EvalError;
 
@@ -307,13 +308,16 @@
     }
 
     protected static class GroovyElem implements StrElem {
-        protected final String str;
+        protected final String originalString;
+        protected final Class parsedScript;
         protected GroovyElem(String script) {
-            this.str = script;
+            this.originalString = script;
+            this.parsedScript = GroovyUtil.groovyClassLoader.parseClass(script);
         }
         public void append(StringBuilder buffer, Map<String, ? extends Object> context, TimeZone timeZone, Locale locale) {
             try {
-                Object obj = GroovyUtil.eval(this.str, UtilMisc.makeMapWritable(context));
+                // this approach will re-parse the script each time: Object obj = GroovyUtil.eval(this.str, UtilMisc.makeMapWritable(context));
+                Object obj = InvokerHelper.createScript(this.parsedScript, GroovyUtil.getBinding(context)).run();
                 if (obj != null) {
                     try {
                         buffer.append(ObjectType.simpleTypeConvert(obj, "String", null, timeZone, locale, true));
@@ -322,11 +326,14 @@
                     }
                 } else {
                     if (Debug.verboseOn()) {
-                        Debug.logVerbose("Groovy scriptlet evaluated to null [" + this.str + "], got no return so inserting nothing.", module);
+                        Debug.logVerbose("Groovy scriptlet evaluated to null [" + this.originalString + "], got no return so inserting nothing.", module);
                     }
                 }
             } catch (CompilationFailedException e) {
-                Debug.logWarning(e, "Error evaluating Groovy scriptlet [" + this.str + "], inserting nothing; error was: " + e, module);
+                Debug.logWarning(e, "Error evaluating Groovy scriptlet [" + this.originalString + "], inserting nothing; error was: " + e, module);
+            } catch (Exception e) {
+                // handle other things, like the groovy.lang.MissingPropertyException
+                Debug.logWarning(e, "Error evaluating Groovy scriptlet [" + this.originalString + "], inserting nothing; error was: " + e, module);
             }
         }
     }

Modified: ofbiz/trunk/framework/example/widget/example/FormWidgetExampleForms.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/example/widget/example/FormWidgetExampleForms.xml?rev=763105&r1=763104&r2=763105&view=diff
==============================================================================
--- ofbiz/trunk/framework/example/widget/example/FormWidgetExampleForms.xml (original)
+++ ofbiz/trunk/framework/example/widget/example/FormWidgetExampleForms.xml Wed Apr  8 05:14:23 2009
@@ -39,8 +39,8 @@
         <field name="field9"
                title="${uiLabelMap.ExampleDateField9Title}"
                tooltip="${uiLabelMap.ExampleDateField9Tooltip}">
-            <!-- tooltip="Same as above, uses the ${'${'bsh:...} notation to call an util method to get the now timestamp}"-->
-            <!-- tooltip="Same as above, uses the \${bsh:...} notation to call an util method to get the now timestamp}"-->
+            <!-- tooltip="Same as above, uses the ${'${'groovy:...} notation to call an util method to get the now timestamp}"-->
+            <!-- tooltip="Same as above, uses the \${groovy:...} notation to call an util method to get the now timestamp}"-->
             <date-time default-value="${groovy:org.ofbiz.base.util.UtilDateTime.nowTimestamp()}"/>
         </field>
         <!-- ***************** -->
@@ -108,7 +108,7 @@
                entry-name="exampleDateField"
                title="${uiLabelMap.ExampleDateField8Title}"
                tooltip="${uiLabelMap.ExampleDateField8Tooltip}">
-            <display description="${bsh:org.ofbiz.base.util.UtilDateTime.toDateString(exampleDateField, &quot;MMMM, dd, yyyy&quot;);}"/>
+            <display description="${bsh:org.ofbiz.base.util.UtilDateTime.toDateString(exampleDateField, &quot;MMMM,dd,yyyy&quot;);}"/>
         </field>
     </form>