diff --git a/framework/base/src/org/ofbiz/base/util/GroovyUtil.java b/framework/base/src/org/ofbiz/base/util/GroovyUtil.java index 247e560cdc..44102f3440 100644 --- a/framework/base/src/org/ofbiz/base/util/GroovyUtil.java +++ b/framework/base/src/org/ofbiz/base/util/GroovyUtil.java @@ -33,6 +33,7 @@ import groovy.lang.GroovyShell; import groovy.lang.Script; import org.codehaus.groovy.control.CompilationFailedException; import org.codehaus.groovy.runtime.InvokerHelper; +import bsh.EvalError; /** * GroovyUtil - Groovy Utilities @@ -61,6 +62,44 @@ public class GroovyUtil { return binding; } + /** + * Evaluate a Groovy condition or expression + * @param expression The expression to evaluate + * @param context The context to use in evaluation (re-written) + * @return Object The result of the evaluation + * @throws CompilationFailedException + */ + public static Object eval(String expression, Map context) throws CompilationFailedException { + Object o; + if (expression == null || expression.equals("")) { + Debug.logError("Groovy Evaluation error. Empty expression", module); + return null; + } + + if (Debug.verboseOn()) + Debug.logVerbose("Evaluating -- " + expression, module); + if (Debug.verboseOn()) + Debug.logVerbose("Using Context -- " + context, module); + + try { + GroovyShell shell = new GroovyShell(getBinding(context)); + o = shell.evaluate(expression); + + if (Debug.verboseOn()) + Debug.logVerbose("Evaluated to -- " + o, module); + + // read back the context info + Binding binding = shell.getContext(); + context.putAll(binding.getVariables()); + + } catch (CompilationFailedException e) { + Debug.logError(e, "Groovy Evaluation error.", module); + throw e; + } + + return o; + } + public static Object runScriptAtLocation(String location, Map context) throws GeneralException { try { Class scriptClass = parsedScripts.get(location); 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 0663ecfe2e..f41a76ca75 100644 --- a/framework/base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java +++ b/framework/base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java @@ -26,13 +26,9 @@ import java.util.Locale; import java.util.Map; import java.util.TimeZone; -import org.ofbiz.base.util.BshUtil; -import org.ofbiz.base.util.Debug; -import org.ofbiz.base.util.ObjectType; import org.ofbiz.base.util.cache.UtilCache; -import org.ofbiz.base.util.UtilDateTime; -import org.ofbiz.base.util.UtilFormatOut; -import org.ofbiz.base.util.UtilMisc; +import org.ofbiz.base.util.*; +import org.codehaus.groovy.control.CompilationFailedException; import bsh.EvalError; @@ -230,9 +226,13 @@ public class FlexibleStringExpander implements Serializable { // append everything from the current index to the start of the var strElems.add(new ConstElem(original.substring(currentInd, start))); } - // check to see if this starts with a "bsh:", if so treat the rest of the string as a bsh scriptlet + if (original.indexOf("bsh:", start + 2) == start + 2) { + // checks to see if this starts with a "bsh:", if so treat the rest of the string as a bsh scriptlet strElems.add(new BshElem(original.substring(start + 6, end))); + } else if (original.indexOf("groovy:", start + 2) == start + 2) { + // checks to see if this starts with a "groovy:", if so treat the rest of the string as a groovy scriptlet + strElems.add(new GroovyElem(original.substring(start + 9, end))); } else { int ptr = original.indexOf(openBracket, start + 2); while (ptr != -1 && end != -1 && ptr < end) { @@ -306,6 +306,31 @@ public class FlexibleStringExpander implements Serializable { } } + protected static class GroovyElem implements StrElem { + protected final String str; + protected GroovyElem(String script) { + this.str = script; + } + public void append(StringBuilder buffer, Map context, TimeZone timeZone, Locale locale) { + try { + Object obj = GroovyUtil.eval(this.str, UtilMisc.makeMapWritable(context)); + if (obj != null) { + try { + buffer.append(ObjectType.simpleTypeConvert(obj, "String", null, timeZone, locale, true)); + } catch (Exception e) { + buffer.append(obj); + } + } else { + if (Debug.verboseOn()) { + Debug.logVerbose("Groovy scriptlet evaluated to null [" + this.str + "], 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); + } + } + } + protected static class CurrElem implements StrElem { protected final String valueStr; protected final FlexibleStringExpander codeExpr; diff --git a/framework/example/config/ExampleUiLabels.xml b/framework/example/config/ExampleUiLabels.xml index 365e52c35b..27db2f153f 100644 --- a/framework/example/config/ExampleUiLabels.xml +++ b/framework/example/config/ExampleUiLabels.xml @@ -148,9 +148,9 @@ Campo9: campo di selezione con valore di default per data e ora - Same as above, uses the $ {bsh:...} notation to call an util method to get the now timestamp - La même chose mais utilise la notation $ {bsh:...} pour appeler un méthode utilitaire pour obtenir l'heure du moment - Lo stesso di sopra, usare la notazione $ {bsh:...} per eseguire un metodo di utilità per ottenere un nuovo timestamp + Same as above, uses the $ {groovy:...} notation to call an util method to get the now timestamp + La même chose mais utilise la notation $ {groovy:...} pour appeler un méthode utilitaire pour obtenir l'heure du moment + Lo stesso di sopra, usare la notazione $ {groovy:...} per eseguire un metodo di utilità per ottenere un nuovo timestamp Date/Time fields diff --git a/framework/example/widget/example/FormWidgetExampleForms.xml b/framework/example/widget/example/FormWidgetExampleForms.xml index edc197f8d7..3aff3b78ef 100644 --- a/framework/example/widget/example/FormWidgetExampleForms.xml +++ b/framework/example/widget/example/FormWidgetExampleForms.xml @@ -41,7 +41,7 @@ under the License. tooltip="${uiLabelMap.ExampleDateField9Tooltip}"> - + @@ -99,7 +99,7 @@ under the License. entry-name="exampleDateField" title="${uiLabelMap.ExampleDateField7Title}" tooltip="${uiLabelMap.ExampleDateField7Tooltip}"> - +