Re: Using WebMacro outside of a WebApplication

Keats Kirsch <[email protected]> Tue, 20 Sep 2005 12:38:58 -0400
Newsgroups gmane.comp.java.webmacro.user
Message-ID <[email protected]>
I'm not clear on what your problem is.  It looks like you have the right 
idea.  You can evaluate a template to a stream or a String.  If you want 
to set other data types using WM, you can pull them out of the context 
after evaluating the template.

Something like:

            Context ctx = new Context();
            ctx.put("val1", 1);
            ctx.put("val2", 155);
            Macro macro = null;
            macro = new StringMacro("#set $val = $val1 + $val2");
            macro.evaluate( ctx);
            Integer val = (Integer)ctx.get("val");

Hope this helps.

Keats

Danni Miller wrote:

>I would like to use the expression handling of
>WebMacro outside of typical WebMacro. I have been able
>to use a StringMacro which evalutates out properly,
>but I would also like to evalute a full expression
>such as $val + $val1 When I evaluate such an
>expression I get String substitutions. Is there any
>way to do this with WebMacro 2.0. I am not sure what I
>should use to have this functionality, or if those
>classes are public. I have a sample below of what I am
>trying to accomplish. 
>
>By the way I have to say I love the ideas and
>implementation of WebMacro. Thanks
>
>import org.webmacro.engine.StringMacro;
>import org.webmacro.engine.MacroAdapter;
>import org.webmacro.engine.StringMacroAdapter;
>import org.webmacro.Context;
>import org.webmacro.Macro;
>
>
>public class Test {
>
>    public static void main(String[] args) {
>        try {
>
>
>
>            Context ctx = new Context();
>            ctx.put("name", "David");
>            ctx.put("val1", 1);
>            ctx.put("val2", 155);
>
>            Macro macro = null;
>
>            macro = new StringMacro("$name");
>            System.out.println( macro.evaluate( ctx));
>
>            macro = new
>StringMacro("$name.substring(0,2).length()");
>            System.out.println( macro.evaluate( ctx));
>
>            macro = new
>StringMacro("$name.substring(0,2).length()+5");
>            System.out.println( macro.evaluate( ctx));
>
>            macro = new StringMacro("$val1 + $val2");
>            System.out.println( macro.evaluate( ctx));
>
>            macro = new StringMacroAdapter("$val1 +
>156");
>            System.out.println( macro.evaluate( ctx));
>
>            macro = MacroAdapter.createMacro("$val1 +
>$val2");
>            System.out.println( macro.evaluate( ctx));
>
>
>
>
>
>        } catch(Throwable t){
>            t.printStackTrace();
>        }
>
>
>    }
>}
>
>  
>




-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php