You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ofbiz.apache.org by Ashish Vijaywargiya <as...@hotwaxmedia.com> on 2009/04/07 06:26:20 UTC
Re: svn commit: r762610 - in /ofbiz/trunk/framework: base/src/org/ofbiz/base/util/GroovyUtil.java
base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java example/config/ExampleUiLabels.xml
example/widget/example/FormWidgetExampleForms.xml
Thanks Andrew.
--
Ashish
jaz@apache.org wrote:
> Author: jaz
> Date: Tue Apr 7 03:17:31 2009
> New Revision: 762610
>
> URL: http://svn.apache.org/viewvc?rev=762610&view=rev
> Log:
> Added Groovy support to the FlexibleStringExpander; updated example component to show usage
>
> 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/config/ExampleUiLabels.xml
> 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=762610&r1=762609&r2=762610&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 Tue Apr 7 03:17:31 2009
> @@ -33,6 +33,7 @@
> 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 @@
> 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<String, Object> 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<String, Object> context) throws GeneralException {
> try {
> Class scriptClass = parsedScripts.get(location);
>
> 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=762610&r1=762609&r2=762610&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 Tue Apr 7 03:17:31 2009
> @@ -26,13 +26,9 @@
> 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 @@
> // 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 @@
> }
> }
>
> + protected static class GroovyElem implements StrElem {
> + protected final String str;
> + protected GroovyElem(String script) {
> + this.str = 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));
> + 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;
>
> Modified: ofbiz/trunk/framework/example/config/ExampleUiLabels.xml
> URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/example/config/ExampleUiLabels.xml?rev=762610&r1=762609&r2=762610&view=diff
> ==============================================================================
> --- ofbiz/trunk/framework/example/config/ExampleUiLabels.xml (original)
> +++ ofbiz/trunk/framework/example/config/ExampleUiLabels.xml Tue Apr 7 03:17:31 2009
> @@ -148,9 +148,9 @@
> <value xml:lang="it">Campo9: campo di selezione con valore di default per data e ora</value>
> </property>
> <property key="ExampleDateField9Tooltip">
> - <value xml:lang="en">Same as above, uses the $ {bsh:...} notation to call an util method to get the now timestamp</value>
> - <value xml:lang="fr">La même chose mais utilise la notation $ {bsh:...} pour appeler un méthode utilitaire pour obtenir l'heure du moment</value>
> - <value xml:lang="it">Lo stesso di sopra, usare la notazione $ {bsh:...} per eseguire un metodo di utilità per ottenere un nuovo timestamp</value>
> + <value xml:lang="en">Same as above, uses the $ {groovy:...} notation to call an util method to get the now timestamp</value>
> + <value xml:lang="fr">La même chose mais utilise la notation $ {groovy:...} pour appeler un méthode utilitaire pour obtenir l'heure du moment</value>
> + <value xml:lang="it">Lo stesso di sopra, usare la notazione $ {groovy:...} per eseguire un metodo di utilità per ottenere un nuovo timestamp</value>
> </property>
> <property key="ExampleDateTimeFields">
> <value xml:lang="en">Date/Time fields</value>
>
> Modified: ofbiz/trunk/framework/example/widget/example/FormWidgetExampleForms.xml
> URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/example/widget/example/FormWidgetExampleForms.xml?rev=762610&r1=762609&r2=762610&view=diff
> ==============================================================================
> --- ofbiz/trunk/framework/example/widget/example/FormWidgetExampleForms.xml (original)
> +++ ofbiz/trunk/framework/example/widget/example/FormWidgetExampleForms.xml Tue Apr 7 03:17:31 2009
> @@ -41,7 +41,7 @@
> 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}"-->
> - <date-time default-value="${bsh:org.ofbiz.base.util.UtilDateTime.nowTimestamp()}"/>
> + <date-time default-value="${groovy:org.ofbiz.base.util.UtilDateTime.nowTimestamp()}"/>
> </field>
> <!-- ***************** -->
> <!-- *** field10 *** -->
> @@ -99,7 +99,7 @@
> entry-name="exampleDateField"
> title="${uiLabelMap.ExampleDateField7Title}"
> tooltip="${uiLabelMap.ExampleDateField7Tooltip}">
> - <display description="${bsh:org.ofbiz.base.util.UtilDateTime.toDateString(exampleDateField, "MM/dd/yyyy");}"/>
> + <display description="${groovy:org.ofbiz.base.util.UtilDateTime.toDateString(exampleDateField, "MM/dd/yyyy");}"/>
> </field>
> <!-- ***************** -->
> <!-- *** field8 *** -->
>
>
>
Re: svn commit: r762610 - in /ofbiz/trunk/framework: base/src/org/ofbiz/base/util/GroovyUtil.java base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java example/config/ExampleUiLabels.xml example/widget/example/FormWidgetExampleForms.xml
Posted by Jacques Le Roux <ja...@les7arts.com>.
Yes, it was https://issues.apache.org/jira/browse/OFBIZ-1940, just closed
Thanks for the reminder
Jacques
From: "Andrew Zeneski" <an...@hotwaxmedia.com>
No problem. :) If there is a JIRA issue for this, please let me know.
I couldn't find it.
Andrew
On Apr 7, 2009, at 4:12 AM, Jacques Le Roux wrote:
> Yes, thanks Andrew,
>
> I wished to do it but I'm pretty happier you did :o)
>
> Jacques
>
> From: "Ashish Vijaywargiya" <as...@hotwaxmedia.com>
> Thanks Andrew.
>
> --
> Ashish
>
> jaz@apache.org wrote:
>> Author: jaz
>> Date: Tue Apr 7 03:17:31 2009
>> New Revision: 762610
>>
>> URL: http://svn.apache.org/viewvc?rev=762610&view=rev
>> Log:
>> Added Groovy support to the FlexibleStringExpander; updated example component to show usage
>>
>> 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/config/ExampleUiLabels.xml
>> 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=762610&r1=762609&r2=762610&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 Tue Apr 7 03:17:31 2009
>> @@ -33,6 +33,7 @@
>> 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 @@
>> 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<String, Object> 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<String, Object> context) throws GeneralException {
>> try {
>> Class scriptClass = parsedScripts.get(location);
>>
>> 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=762610&r1=762609&r2=762610&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 Tue Apr 7 03:17:31 2009
>> @@ -26,13 +26,9 @@
>> 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 @@
>> // 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 @@
>> }
>> }
>> + protected static class GroovyElem implements StrElem {
>> + protected final String str;
>> + protected GroovyElem(String script) {
>> + this.str = 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));
>> + 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;
>>
>> Modified: ofbiz/trunk/framework/example/config/ExampleUiLabels.xml
>> URL:
>> http://svn.apache.org/viewvc/ofbiz/trunk/framework/example/config/ExampleUiLabels.xml?rev=762610&r1=762609&r2=762610&view=diff
>> = = = = = = = = = =====================================================================
>> --- ofbiz/trunk/framework/example/config/ExampleUiLabels.xml (original)
>> +++ ofbiz/trunk/framework/example/config/ExampleUiLabels.xml Tue Apr 7 03:17:31 2009
>> @@ -148,9 +148,9 @@
>> <value xml:lang="it">Campo9: campo di selezione con valore di default per data e ora</value>
>> </property>
>> <property key="ExampleDateField9Tooltip">
>> - <value xml:lang="en">Same as above, uses the $ {bsh:...} notation to call an util method to get the now
>> timestamp</value>
>> - <value xml:lang="fr">La mÃfªme chose mais utilise la notation $ {bsh:...} pour appeler un mÃf©thode utilitaire pour
>> obtenir l'heure du moment</value>
>> - <value xml:lang="it">Lo stesso di sopra, usare la notazione $ {bsh:...} per eseguire un metodo di utilitÃf per
>> ottenere un nuovo timestamp</value>
>> + <value xml:lang="en">Same as above, uses the $ {groovy:...} notation to call an util method to get the now
>> timestamp</value>
>> + <value xml:lang="fr">La mÃfªme chose mais utilise la notation $ {groovy:...} pour appeler un mÃf©thode utilitaire
>> pour obtenir l'heure du moment</value>
>> + <value xml:lang="it">Lo stesso di sopra, usare la notazione $ {groovy:...} per eseguire un metodo di utilitÃf per
>> ottenere un nuovo timestamp</value>
>> </property>
>> <property key="ExampleDateTimeFields">
>> <value xml:lang="en">Date/Time fields</value>
>>
>> Modified: ofbiz/trunk/framework/example/widget/example/ FormWidgetExampleForms.xml
>> URL:
>> http://svn.apache.org/viewvc/ofbiz/trunk/framework/example/widget/example/FormWidgetExampleForms.xml?rev=762610&r1=762609&r2=762610&view=diff
>> = = = = = = = = = =====================================================================
>> --- ofbiz/trunk/framework/example/widget/example/ FormWidgetExampleForms.xml (original)
>> +++ ofbiz/trunk/framework/example/widget/example/ FormWidgetExampleForms.xml Tue Apr 7 03:17:31 2009
>> @@ -41,7 +41,7 @@
>> 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}"-->
>> - <date-time default-value="$ {bsh:org.ofbiz.base.util.UtilDateTime.nowTimestamp()}"/>
>> + <date-time default-value="$ {groovy:org.ofbiz.base.util.UtilDateTime.nowTimestamp()}"/>
>> </field>
>> <!-- ***************** -->
>> <!-- *** field10 *** -->
>> @@ -99,7 +99,7 @@
>> entry-name="exampleDateField"
>> title="${uiLabelMap.ExampleDateField7Title}"
>> tooltip="${uiLabelMap.ExampleDateField7Tooltip}">
>> - <display description="$ {bsh:org .ofbiz.base.util.UtilDateTime.toDateString(exampleDateField,
>> "MM/dd/yyyy");}"/>
>> + <display description="$ {groovy:org .ofbiz.base.util.UtilDateTime.toDateString(exampleDateField,
>> "MM/dd/yyyy");}"/>
>> </field>
>> <!-- ***************** -->
>> <!-- *** field8 *** -->
>>
>>
>>
>
>
>
Re: svn commit: r762610 - in /ofbiz/trunk/framework: base/src/org/ofbiz/base/util/GroovyUtil.java base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java example/config/ExampleUiLabels.xml example/widget/example/FormWidgetExampleForms.xml
Posted by Andrew Zeneski <an...@hotwaxmedia.com>.
No problem. :) If there is a JIRA issue for this, please let me know.
I couldn't find it.
Andrew
On Apr 7, 2009, at 4:12 AM, Jacques Le Roux wrote:
> Yes, thanks Andrew,
>
> I wished to do it but I'm pretty happier you did :o)
>
> Jacques
>
> From: "Ashish Vijaywargiya" <as...@hotwaxmedia.com>
> Thanks Andrew.
>
> --
> Ashish
>
> jaz@apache.org wrote:
>> Author: jaz
>> Date: Tue Apr 7 03:17:31 2009
>> New Revision: 762610
>>
>> URL: http://svn.apache.org/viewvc?rev=762610&view=rev
>> Log:
>> Added Groovy support to the FlexibleStringExpander; updated example
>> component to show usage
>>
>> 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/config/ExampleUiLabels.xml
>> 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=762610&r1=762609&r2=762610&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 Tue Apr 7 03:17:31 2009
>> @@ -33,6 +33,7 @@
>> 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 @@
>> 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<String,
>> Object> 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<String, Object> context) throws GeneralException {
>> try {
>> Class scriptClass = parsedScripts.get(location);
>>
>> 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=762610&r1=762609&r2=762610&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 Tue Apr 7 03:17:31 2009
>> @@ -26,13 +26,9 @@
>> 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 @@
>> // 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 @@
>> }
>> }
>> + protected static class GroovyElem implements StrElem {
>> + protected final String str;
>> + protected GroovyElem(String script) {
>> + this.str = 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));
>> + 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;
>>
>> Modified: ofbiz/trunk/framework/example/config/ExampleUiLabels.xml
>> URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/example/config/ExampleUiLabels.xml?rev=762610&r1=762609&r2=762610&view=diff
>> =
>> =
>> =
>> =
>> =
>> =
>> =
>> =
>> =
>> =====================================================================
>> --- ofbiz/trunk/framework/example/config/ExampleUiLabels.xml
>> (original)
>> +++ ofbiz/trunk/framework/example/config/ExampleUiLabels.xml Tue
>> Apr 7 03:17:31 2009
>> @@ -148,9 +148,9 @@
>> <value xml:lang="it">Campo9: campo di selezione con valore
>> di default per data e ora</value>
>> </property>
>> <property key="ExampleDateField9Tooltip">
>> - <value xml:lang="en">Same as above, uses the $ {bsh:...}
>> notation to call an util method to get the now timestamp</value>
>> - <value xml:lang="fr">La mÃfªme chose mais utilise la
>> notation $ {bsh:...} pour appeler un mÃf©thode utilitaire pour
>> obtenir l'heure du moment</value>
>> - <value xml:lang="it">Lo stesso di sopra, usare la
>> notazione $ {bsh:...} per eseguire un metodo di utilitÃf per
>> ottenere un nuovo timestamp</value>
>> + <value xml:lang="en">Same as above, uses the $
>> {groovy:...} notation to call an util method to get the now
>> timestamp</value>
>> + <value xml:lang="fr">La mÃfªme chose mais utilise la
>> notation $ {groovy:...} pour appeler un mÃf©thode utilitaire pour
>> obtenir l'heure du moment</value>
>> + <value xml:lang="it">Lo stesso di sopra, usare la
>> notazione $ {groovy:...} per eseguire un metodo di utilitÃf per
>> ottenere un nuovo timestamp</value>
>> </property>
>> <property key="ExampleDateTimeFields">
>> <value xml:lang="en">Date/Time fields</value>
>>
>> Modified: ofbiz/trunk/framework/example/widget/example/
>> FormWidgetExampleForms.xml
>> URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/example/widget/example/FormWidgetExampleForms.xml?rev=762610&r1=762609&r2=762610&view=diff
>> =
>> =
>> =
>> =
>> =
>> =
>> =
>> =
>> =
>> =====================================================================
>> --- ofbiz/trunk/framework/example/widget/example/
>> FormWidgetExampleForms.xml (original)
>> +++ ofbiz/trunk/framework/example/widget/example/
>> FormWidgetExampleForms.xml Tue Apr 7 03:17:31 2009
>> @@ -41,7 +41,7 @@
>> 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}"-->
>> - <date-time default-value="$
>> {bsh:org.ofbiz.base.util.UtilDateTime.nowTimestamp()}"/>
>> + <date-time default-value="$
>> {groovy:org.ofbiz.base.util.UtilDateTime.nowTimestamp()}"/>
>> </field>
>> <!-- ***************** -->
>> <!-- *** field10 *** -->
>> @@ -99,7 +99,7 @@
>> entry-name="exampleDateField"
>> title="${uiLabelMap.ExampleDateField7Title}"
>> tooltip="${uiLabelMap.ExampleDateField7Tooltip}">
>> - <display description="$
>> {bsh:org
>> .ofbiz.base.util.UtilDateTime.toDateString(exampleDateField,
>> "MM/dd/yyyy");}"/>
>> + <display description="$
>> {groovy:org
>> .ofbiz.base.util.UtilDateTime.toDateString(exampleDateField,
>> "MM/dd/yyyy");}"/>
>> </field>
>> <!-- ***************** -->
>> <!-- *** field8 *** -->
>>
>>
>>
>
>
>
Re: svn commit: r762610 - in /ofbiz/trunk/framework: base/src/org/ofbiz/base/util/GroovyUtil.java base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java example/config/ExampleUiLabels.xml example/widget/example/FormWidgetExampleForms.xml
Posted by Jacques Le Roux <ja...@les7arts.com>.
Yes, thanks Andrew,
I wished to do it but I'm pretty happier you did :o)
Jacques
From: "Ashish Vijaywargiya" <as...@hotwaxmedia.com>
Thanks Andrew.
--
Ashish
jaz@apache.org wrote:
> Author: jaz
> Date: Tue Apr 7 03:17:31 2009
> New Revision: 762610
>
> URL: http://svn.apache.org/viewvc?rev=762610&view=rev
> Log:
> Added Groovy support to the FlexibleStringExpander; updated example component to show usage
>
> 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/config/ExampleUiLabels.xml
> 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=762610&r1=762609&r2=762610&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 Tue Apr 7 03:17:31 2009
> @@ -33,6 +33,7 @@
> 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 @@
> 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<String, Object> 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<String, Object> context) throws GeneralException {
> try {
> Class scriptClass = parsedScripts.get(location);
>
> 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=762610&r1=762609&r2=762610&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 Tue Apr 7 03:17:31 2009
> @@ -26,13 +26,9 @@
> 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 @@
> // 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 @@
> }
> }
> + protected static class GroovyElem implements StrElem {
> + protected final String str;
> + protected GroovyElem(String script) {
> + this.str = 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));
> + 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;
>
> Modified: ofbiz/trunk/framework/example/config/ExampleUiLabels.xml
> URL:
> http://svn.apache.org/viewvc/ofbiz/trunk/framework/example/config/ExampleUiLabels.xml?rev=762610&r1=762609&r2=762610&view=diff
> ==============================================================================
> --- ofbiz/trunk/framework/example/config/ExampleUiLabels.xml (original)
> +++ ofbiz/trunk/framework/example/config/ExampleUiLabels.xml Tue Apr 7 03:17:31 2009
> @@ -148,9 +148,9 @@
> <value xml:lang="it">Campo9: campo di selezione con valore di default per data e ora</value>
> </property>
> <property key="ExampleDateField9Tooltip">
> - <value xml:lang="en">Same as above, uses the $ {bsh:...} notation to call an util method to get the now timestamp</value>
> - <value xml:lang="fr">La mÃfªme chose mais utilise la notation $ {bsh:...} pour appeler un mÃf©thode utilitaire pour
> obtenir l'heure du moment</value>
> - <value xml:lang="it">Lo stesso di sopra, usare la notazione $ {bsh:...} per eseguire un metodo di utilitÃf per ottenere
> un nuovo timestamp</value>
> + <value xml:lang="en">Same as above, uses the $ {groovy:...} notation to call an util method to get the now
> timestamp</value>
> + <value xml:lang="fr">La mÃfªme chose mais utilise la notation $ {groovy:...} pour appeler un mÃf©thode utilitaire pour
> obtenir l'heure du moment</value>
> + <value xml:lang="it">Lo stesso di sopra, usare la notazione $ {groovy:...} per eseguire un metodo di utilitÃf per
> ottenere un nuovo timestamp</value>
> </property>
> <property key="ExampleDateTimeFields">
> <value xml:lang="en">Date/Time fields</value>
>
> Modified: ofbiz/trunk/framework/example/widget/example/FormWidgetExampleForms.xml
> URL:
> http://svn.apache.org/viewvc/ofbiz/trunk/framework/example/widget/example/FormWidgetExampleForms.xml?rev=762610&r1=762609&r2=762610&view=diff
> ==============================================================================
> --- ofbiz/trunk/framework/example/widget/example/FormWidgetExampleForms.xml (original)
> +++ ofbiz/trunk/framework/example/widget/example/FormWidgetExampleForms.xml Tue Apr 7 03:17:31 2009
> @@ -41,7 +41,7 @@
> 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}"-->
> - <date-time default-value="${bsh:org.ofbiz.base.util.UtilDateTime.nowTimestamp()}"/>
> + <date-time default-value="${groovy:org.ofbiz.base.util.UtilDateTime.nowTimestamp()}"/>
> </field>
> <!-- ***************** -->
> <!-- *** field10 *** -->
> @@ -99,7 +99,7 @@
> entry-name="exampleDateField"
> title="${uiLabelMap.ExampleDateField7Title}"
> tooltip="${uiLabelMap.ExampleDateField7Tooltip}">
> - <display description="${bsh:org.ofbiz.base.util.UtilDateTime.toDateString(exampleDateField,
> "MM/dd/yyyy");}"/>
> + <display description="${groovy:org.ofbiz.base.util.UtilDateTime.toDateString(exampleDateField,
> "MM/dd/yyyy");}"/>
> </field>
> <!-- ***************** -->
> <!-- *** field8 *** -->
>
>
>