|
From: Lane S. <la...@op...> - 2005-09-25 14:50:49
|
<html><body><font style=3D"font-family: arial,helvetica,sans-serif;" size=
=3D"2"><br> <br>I think it would be better to actually make the code availa=
ble <br>somewhere rather than hide it away on Wiki. Also, there's a few =
<br>things that should be done to that little function. Namely, caching =
<br>of StringTemplates. <br> <br>> Also, how do you use the Visitor to=
get a random value in the context? <br> <br>You don't. It's a TemplateVis=
itor, not a ContextVisitor. <br>
<br>
Eric: I will do this but I am not familiar with the TemplateVisitor usage. =
Is there an existing test case I can look at?<br>
<br>
-Lane<br> <br>eric <br> <br>> <br>> -Lane <br>> <br>> <br>> =
--- "Eric B. Ridge" <eb...@tc...> wrote: <br>> <br>> From: "Er=
ic B. Ridge" <eb...@tc...> <br>> Date: Thu, 22 Sep 2005 17:35:02 =
-0400 <br>> To: web...@li... <br>> Subject: Re=
: [WebMacro-user] Expression Handling Again <br>> <br>> On Sep 22, 20=
05, at 5:27 PM, Keats Kirsch wrote: <br>> <br>> > Eric's method is=
nice. <br>> <br>> It's also really fast. On the order of 20% faster =
than communicating <br>> the value through the Context. Which is importa=
nt if you're doing <br>> this sort of thing over and over in web-request=
s. <br>> <br>> eric <br>> <br>> > I do something similar (th=
ough less elegant) in the <br>> > PropertiesDirective. However for XM=
L files there is a much simpler <br>> > method that I sometimes use: =
just parse the XML file as a template <br>> > before you parse the XM=
L. Works great. <br>> > <br>> > You can use doubles in WMScript=
, but not natively with the built-in <br>> > operators. You can use t=
he $Math tool and the casting functions, <br>> > like $toDouble. <br>=
> > <br>> > Keats <br>> > <br>> > Dan Miller wrote:=
<br>> > <br>> > <br>> >> That method works great to g=
et the result. One last <br>> >> thing, how come webmacro expressi=
ons do not return <br>> >> double. Is that historical? Now that we=
use a grammar <br>> >> and parser for WM can is it possible to ex=
pand <br>> >> expressions? <br>> >> Thanks <br>> >&=
gt; <br>> >> --- "Eric B. Ridge" <eb...@tc...> wrote: <br>&=
gt; >> <br>> >> <br>> >> <br>> >>> Rath=
er than creating a StringMacro, create a <br>> >>> StringTempla=
te, with a mini-template of: <br>> >>> #set $foo =3D $x + 1 <br=
>> >>> <br>> >>> Then use a custom TemplateVisitor =
to pull out the <br>> >>> "SetValue" argument of the #set direc=
tive: <br>> >>> <br>> >>> static class MyTemplateVi=
sitor extends <br>> >>> TemplateVisitor { <br>> >>>=
Object value; <br>> >>> <br>> >>> public Object ge=
tValue() { <br>> >>> return value; <br>> >>> } <br>=
> >>> <br>> >>> public void visitDirectiveArg(Strin=
g <br>> >>> string, Object object) { <br>> >>> // t=
he #set directive has 2 arguments: "SetTarget" (LHS) <br>> >>> =
// and "SetValue" (RHS). We're only <br>> >>> interested in "Se=
tValue" <br>> >>> if (string.equals("SetValue")) { <br>> >=
;>> value =3D object; <br>> >>> } <br>> >>> }=
<br>> >>> } <br>> >>> <br>> >>> So, yo=
u're code will be something like: <br>> >>> <br>> >>&g=
t; public Object evalExpression (Context context, <br>> >>> Str=
ing expression) { <br>> >>> StringTemplate tmpl =3D new StringT=
emplate <br>> >>> (context.getBroker(), "#set $foo =3D " + expr=
ession); <br>> >>> MyTemplateVisitor visitor =3D new <br>> &=
gt;>> MyTemplateVisitor(); <br>> >>> <br>> >>>=
; template.parse(); <br>> >>> template.accept(visitor); <br>>=
; >>> <br>> >>> Object value =3D visitor.getValue(); <=
br>> >>> if (value instanceof Maco) { <br>> >>> ret=
urn ((Macro) value).evaluate(_context); <br>> >>> } else { <br>=
> >>> return value; <br>> >>> } <br>> >>&g=
t; } <br>> >>> <br>> >>> Hope this helps. <br>> =
>>> <br>> >>> eric <br>> >>> <br>> >=
>> <br>> >>> <br>> >>> <br>> >>> =
<br>> >>> On Sep 22, 2005, at 12:22 PM, Dan Miller wrote: <br>&=
gt; >>> <br>> >>> <br>> >>> <br>> >&=
gt;>> I guess what I was wondering about was using the <br>> >&=
gt;>> <br>> >>>> <br>> >>> api <br>> &g=
t;>> <br>> >>> <br>> >>>> directly steppin=
g in before the #set places the <br>> >>>> <br>> >>=
>> <br>> >>> result <br>> >>> <br>> >&g=
t;> <br>> >>>> into the context. I was looking into the s=
rc for <br>> >>>> PropertyMethod and trying to figure out ho=
w to get <br>> >>>> <br>> >>>> <br>> >&=
gt;> the <br>> >>> <br>> >>> <br>> >>&g=
t;> result. <br>> >>>> <br>> >>>> I am usi=
ng webmacro throughout a webproject, and <br>> >>>> <br>>=
>>>> <br>> >>> would <br>> >>> <br>>=
; >>> <br>> >>>> like to use similiar webmacro expr=
ession in xml <br>> >>>> <br>> >>>> <br>> =
>>> files <br>> >>> <br>> >>> <br>> >=
;>>> which are read and processed. Thanks <br>> >>>>=
; <br>> >>>> --- Keats Kirsch <ke...@xa...> wrote=
: <br>> >>>> <br>> >>>> <br>> >>>=
> <br>> >>>> <br>> >>>>> AFAIK there ar=
e only two ways to get something <br>> >>>>> <br>> >=
;>>>> <br>> >>> out <br>> >>> <br>> =
>>> <br>> >>>>> of a WM template <br>> >&g=
t;>>> expansion -- capture the result of the expansion, <br>> &=
gt;>>>> <br>> >>>>> <br>> >>> or =
<br>> >>> <br>> >>> <br>> >>>>> p=
ull something from <br>> >>>>> the context. Actually I su=
ppose you could use <br>> >>>>> <br>> >>>>=
> <br>> >>> the <br>> >>> <br>> >>> =
<br>> >>>>> #bean directive to <br>> >>>>&=
gt; create a "global" variable and then get it from <br>> >>>&g=
t;> <br>> >>>>> <br>> >>> the <br>> >=
;>> <br>> >>> <br>> >>>>> Broker. <br>&=
gt; >>>>> <br>> >>>>> Maybe if you can exp=
lain your requirements we <br>> >>>>> <br>> >>&g=
t;>> <br>> >>> could <br>> >>> <br>> >&=
gt;> <br>> >>>>> think of a way of <br>> >>&g=
t;>> doing what you want. <br>> >>>>> <br>> >=
>>>> Keats <br>> >>>>> <br>> >>>&=
gt;> Dan Miller wrote: <br>> >>>>> <br>> >>&g=
t;>> <br>> >>>>> <br>> >>>>> <br>=
> >>>>>> The usage of the StringMacro to use the #set =
<br>> >>>>>> <br>> >>>>>> <br>>=
; >>>>>> <br>> >>>>> directive <br>>=
>>>>> <br>> >>>>> <br>> >>>&g=
t;> <br>> >>>>>> works great. I was wondering if th=
ere is <br>> >>>>>> <br>> >>>>>> =
<br>> >>> anything <br>> >>> <br>> >>> =
<br>> >>>>>> exposed to evaluate the expression withou=
t <br>> >>>>>> <br>> >>>>>> <br>&=
gt; >>>>>> <br>> >>>>> modifying <br>&g=
t; >>>>> <br>> >>>>> <br>> >>>=
>> <br>> >>>>>> the context. <br>> >>&g=
t;>>> <br>> >>>>>> This works great, but I wo=
uld like to leave the <br>> >>>>>> context as is, not =
adding new temp variables to <br>> >>>>>> <br>> >=
;>>>>> <br>> >>> the <br>> >>> <br>&=
gt; >>> <br>> >>>>>> context. <br>> >&g=
t;>>>> <br>> >>>>>> Context ctx =3D new Co=
ntext(); <br>> >>>>>> ctx.put("val1", 1); <br>> >=
;>>>>> ctx.put("val2", 155); <br>> >>>>>&g=
t; Macro macro =3D null; <br>> >>>>>> macro =3D new St=
ringMacro("#set $val =3D $val1 + <br>> >>>>>> <br>>=
>>>>>> <br>> >>>>>> <br>> >&g=
t;>>> $val2"); <br>> >>>>> <br>> >>>=
>> <br>> >>>>> <br>> >>>>>> ma=
cro.evaluate( ctx); <br>> >>>>>> Integer val =3D (Inte=
ger)ctx.get("val"); <br>> >>>>>> <br>> >>>=
>>> <br>> >>>>>> I am looking forward to the =
2.0 release. <br>> >>>>>> <br>> >>>>>=
;> <br>> >>>>>> <br>> >>>>>> <=
br>> >>>>>> <br>> >>>>> <br>> >=
;>>>> <br>> >>>>> <br>> >>>>&g=
t; <br>> >> <br>> >> =3D=3D=3D message truncated =3D=3D=
=3D <br>> >> <br>> >> <br>> > <br>> > <br>>=
; > <br>> > <br>> > ----------------------------------------=
--------------- <br>> > SF.Net email is sponsored by: <br>> > T=
ame your development challenges with Apache's Geronimo App Server. <br>>=
> Download <br>> > it for free - -and be entered to win a 42" pla=
sma tv or your very <br>> own <br>> > Sony(tm)PSP. Click here to=
play: http://sourceforge.net/geronimo.php <br>> > __________________=
_____________________________ <br>> > Webmacro-user mailing list <br>=
> > Web...@li... <br>> > https://lists.s=
ourceforge.net/lists/listinfo/webmacro-user <br>> > <br>> <br>>=
<br>> <br>> ------------------------------------------------------- =
<br>> SF.Net email is sponsored by: <br>> Tame your development chall=
enges with Apache's Geronimo App Server. <br>> Download <br>> it fo=
r free - -and be entered to win a 42" plasma tv or your very own <br>> S=
ony(tm)PSP. Click here to play: http://sourceforge.net/geronimo.php <br>>=
; _______________________________________________ <br>> Webmacro-user ma=
iling list <br>> Web...@li... <br>> https://li=
sts.sourceforge.net/lists/listinfo/webmacro-user <br>> -----------------=
-------------------------------------- SF.Net <br>> email is sponsored=
by: Tame your development challenges with <br>> Apache's Geronimo App=
Server. Download it for free - -and be <br>> entered to win a 42" pla=
sma tv or your very own Sony(tm)PSP. Click <br>> here to play: http://=
sourceforge.net/geronimo.php <br>> ___________________________________=
____________ Webmacro-user <br>> mailing list Web...@li...=
ceforge.net https:// <br>> lists.sourceforge.net/lists/listinfo/webmacr=
o-user <br> <br> <br> <br>-------------------------------------------------=
------ <br>SF.Net email is sponsored by: <br>Tame your development challeng=
es with Apache's Geronimo App Server. Download <br>it for free - -and be en=
tered to win a 42" plasma tv or your very own <br>Sony(tm)PSP. Click here =
to play: http://sourceforge.net/geronimo.php <br>__________________________=
_____________________ <br>Webmacro-user mailing list <br>Webmacro-user@list=
s.sourceforge.net <br>https://lists.sourceforge.net/lists/listinfo/webmacro=
-user<br></font></body></html>
|