From 89ba7ff311fe1e19b5f55eab0b4fe6dc3eda7cf1 Mon Sep 17 00:00:00 2001 From: "David E. Jones" Date: Wed, 8 Apr 2009 05:14:23 +0000 Subject: [PATCH] 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 git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@763105 13f79535-47bb-0310-9956-ffa450edef68 --- .../src/org/ofbiz/base/util/GroovyUtil.java | 2 +- .../util/string/FlexibleStringExpander.java | 17 ++++++++++++----- .../widget/example/FormWidgetExampleForms.xml | 6 +++--- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/framework/base/src/org/ofbiz/base/util/GroovyUtil.java b/framework/base/src/org/ofbiz/base/util/GroovyUtil.java index 44102f3440..9e3b69a425 100644 --- a/framework/base/src/org/ofbiz/base/util/GroovyUtil.java +++ b/framework/base/src/org/ofbiz/base/util/GroovyUtil.java @@ -47,7 +47,7 @@ public class GroovyUtil { public static GroovyClassLoader groovyClassLoader = new GroovyClassLoader(); - private static Binding getBinding(Map context) { + public static Binding getBinding(Map context) { Binding binding = new Binding(); if (context != null) { Set keySet = context.keySet(); diff --git a/framework/base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java b/framework/base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java index f41a76ca75..5ec5577d82 100644 --- a/framework/base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java +++ b/framework/base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java @@ -29,6 +29,7 @@ import java.util.TimeZone; 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 @@ public class FlexibleStringExpander implements Serializable { } 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 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 @@ public class FlexibleStringExpander implements Serializable { } } 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); } } } diff --git a/framework/example/widget/example/FormWidgetExampleForms.xml b/framework/example/widget/example/FormWidgetExampleForms.xml index 3aff3b78ef..81a588d0d8 100644 --- a/framework/example/widget/example/FormWidgetExampleForms.xml +++ b/framework/example/widget/example/FormWidgetExampleForms.xml @@ -39,8 +39,8 @@ under the License. - - + + @@ -108,7 +108,7 @@ under the License. entry-name="exampleDateField" title="${uiLabelMap.ExampleDateField8Title}" tooltip="${uiLabelMap.ExampleDateField8Tooltip}"> - +