Evaluating a GString

View: New views
6 Messages — Rating Filter:   Alert me  

Evaluating a GString

by melix :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I'm trying to find out if there's any shorter way to evaluate a GString from Java code. The string to be evaluated comes from an XML configuration file and includes placeholders. My solution seems rather complex for such a basic need, and I'm quite sure there are situations where it doesn't work properly :

Binding binding = new Binding();
...
String evaluatedRegex = "\""+ org.apache.commons.lang.StringEscapeUtils.escapeJava(regex)+"\"";
evaluatedRegex = new GroovyShell(binding).evaluate(evaluatedRegex).toString();
I'd like to be able to write something like :
String evaluatedRegex = new GString(regex, binding).toString();
Or (cleaner) :
String evaluatedRegex = GStringFactory.evaluate(regex, binding);
Thanks

Re: Evaluating a GString

by glaforge :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Have you looked at the Eval class?
http://groovy.codehaus.org/api/groovy/util/Eval.html

On Mon, Oct 27, 2008 at 12:19 PM, melix <cedric.champeau@...> wrote:

> Hi,
>
> I'm trying to find out if there's any shorter way to evaluate a GString from
> Java code. The string to be evaluated comes from an XML configuration file
> and includes placeholders. My solution seems rather complex for such a basic
> need, and I'm quite sure there are situations where it doesn't work properly
> :
>
> Binding binding = new Binding();
> ...
> String evaluatedRegex = "\""+
> org.apache.commons.lang.StringEscapeUtils.escapeJava(regex)+"\"";
> evaluatedRegex = new
> GroovyShell(binding).evaluate(evaluatedRegex).toString();
>
> I'd like to be able to write something like :
>
> String evaluatedRegex = new GString(regex, binding).toString();
>
> Or (cleaner) :
>
> String evaluatedRegex = GStringFactory.evaluate(regex, binding);
>
> Thanks
> ________________________________
> View this message in context: Evaluating a GString
> Sent from the groovy - user mailing list archive at Nabble.com.
>



--
Guillaume Laforge
Groovy Project Manager
G2One, Inc. Vice-President Technology
http://www.g2one.com

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Evaluating a GString

by melix :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Salut Guillaume,

As far as I understand, the Eval class does not take a binding as a parameter, and the expression has to be valid Groovy code (that's why I'm doing that nasty escapeJava things). I'd like to avoid escaping as much as possible.

How would you do that ?

glaforge wrote:
Have you looked at the Eval class?
http://groovy.codehaus.org/api/groovy/util/Eval.html

On Mon, Oct 27, 2008 at 12:19 PM, melix <cedric.champeau@lingway.com> wrote:
> Hi,
>
> I'm trying to find out if there's any shorter way to evaluate a GString from
> Java code. The string to be evaluated comes from an XML configuration file
> and includes placeholders. My solution seems rather complex for such a basic
> need, and I'm quite sure there are situations where it doesn't work properly
> :
>
> Binding binding = new Binding();
> ...
> String evaluatedRegex = "\""+
> org.apache.commons.lang.StringEscapeUtils.escapeJava(regex)+"\"";
> evaluatedRegex = new
> GroovyShell(binding).evaluate(evaluatedRegex).toString();
>
> I'd like to be able to write something like :
>
> String evaluatedRegex = new GString(regex, binding).toString();
>
> Or (cleaner) :
>
> String evaluatedRegex = GStringFactory.evaluate(regex, binding);
>
> Thanks
> ________________________________
> View this message in context: Evaluating a GString
> Sent from the groovy - user mailing list archive at Nabble.com.
>



--
Guillaume Laforge
Groovy Project Manager
G2One, Inc. Vice-President Technology
http://www.g2one.com

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Re: Evaluating a GString

by glaforge :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Well, perhaps in that case Eval is too simplistic.
But at this point, I guess your best bet is simply to do a nice
utility method wrapping GroovyShell, just as you did, because there's
no simpler way as of now to do what you're trying to achieve.

On Mon, Oct 27, 2008 at 12:32 PM, melix <cedric.champeau@...> wrote:

>
> Salut Guillaume,
>
> As far as I understand, the Eval class does not take a binding as a
> parameter, and the expression has to be valid Groovy code (that's why I'm
> doing that nasty escapeJava things). I'd like to avoid escaping as much as
> possible.
>
> How would you do that ?
>
>
> glaforge wrote:
>>
>> Have you looked at the Eval class?
>> http://groovy.codehaus.org/api/groovy/util/Eval.html
>>
>> On Mon, Oct 27, 2008 at 12:19 PM, melix <cedric.champeau@...>
>> wrote:
>>> Hi,
>>>
>>> I'm trying to find out if there's any shorter way to evaluate a GString
>>> from
>>> Java code. The string to be evaluated comes from an XML configuration
>>> file
>>> and includes placeholders. My solution seems rather complex for such a
>>> basic
>>> need, and I'm quite sure there are situations where it doesn't work
>>> properly
>>> :
>>>
>>> Binding binding = new Binding();
>>> ...
>>> String evaluatedRegex = "\""+
>>> org.apache.commons.lang.StringEscapeUtils.escapeJava(regex)+"\"";
>>> evaluatedRegex = new
>>> GroovyShell(binding).evaluate(evaluatedRegex).toString();
>>>
>>> I'd like to be able to write something like :
>>>
>>> String evaluatedRegex = new GString(regex, binding).toString();
>>>
>>> Or (cleaner) :
>>>
>>> String evaluatedRegex = GStringFactory.evaluate(regex, binding);
>>>
>>> Thanks
>>> ________________________________
>>> View this message in context: Evaluating a GString
>>> Sent from the groovy - user mailing list archive at Nabble.com.
>>>
>>
>>
>>
>> --
>> Guillaume Laforge
>> Groovy Project Manager
>> G2One, Inc. Vice-President Technology
>> http://www.g2one.com
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>     http://xircles.codehaus.org/manage_email
>>
>>
>>
>>
>
> --
> View this message in context: http://www.nabble.com/Evaluating-a-GString-tp20184911p20185099.html
> Sent from the groovy - user mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>



--
Guillaume Laforge
Groovy Project Manager
G2One, Inc. Vice-President Technology
http://www.g2one.com

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Evaluating a GString

by Jim White :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm not totally clear on exactly what functionality you need, but
usually folks who have this problem are good candidates for a template
engine.  I've not used them myself and so don't know all the trade-offs,
but groovy.text.GStringTemplateEngine may be appropriate.

Jim

melix wrote:

> Hi,
>
> I'm trying to find out if there's any shorter way to evaluate a GString
> from Java code. The string to be evaluated comes from an XML
> configuration file and includes placeholders. My solution seems rather
> complex for such a basic need, and I'm quite sure there are situations
> where it doesn't work properly :
>
> Binding binding = new Binding();
> ...
> String evaluatedRegex = "\""+ org.apache.commons.lang.StringEscapeUtils.escapeJava(regex)+"\"";
> evaluatedRegex = new GroovyShell(binding).evaluate(evaluatedRegex).toString();
>
> I'd like to be able to write something like :
>
> String evaluatedRegex = new GString(regex, binding).toString();
>
> Or (cleaner) :
>
> String evaluatedRegex = GStringFactory.evaluate(regex, binding);
>
> Thanks
> View this message in context: Evaluating a GString
> <http://www.nabble.com/Evaluating-a-GString-tp20184911p20184911.html>
> Sent from the groovy - user mailing list archive
> <http://www.nabble.com/groovy---user-f11867.html> at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Evaluating a GString

by melix :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I guess you're right : although using the term "template engine" is overkill in my case, this is the closest way to do what I want.

Jim White wrote:
I'm not totally clear on exactly what functionality you need, but
usually folks who have this problem are good candidates for a template
engine.  I've not used them myself and so don't know all the trade-offs,
but groovy.text.GStringTemplateEngine may be appropriate.

Jim

melix wrote:

> Hi,
>
> I'm trying to find out if there's any shorter way to evaluate a GString
> from Java code. The string to be evaluated comes from an XML
> configuration file and includes placeholders. My solution seems rather
> complex for such a basic need, and I'm quite sure there are situations
> where it doesn't work properly :
>
> Binding binding = new Binding();
> ...
> String evaluatedRegex = "\""+ org.apache.commons.lang.StringEscapeUtils.escapeJava(regex)+"\"";
> evaluatedRegex = new GroovyShell(binding).evaluate(evaluatedRegex).toString();
>
> I'd like to be able to write something like :
>
> String evaluatedRegex = new GString(regex, binding).toString();
>
> Or (cleaner) :
>
> String evaluatedRegex = GStringFactory.evaluate(regex, binding);
>
> Thanks
> View this message in context: Evaluating a GString
> <http://www.nabble.com/Evaluating-a-GString-tp20184911p20184911.html>
> Sent from the groovy - user mailing list archive
> <http://www.nabble.com/groovy---user-f11867.html> at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email