You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by "Charles N. Harvey III" <ch...@alloy.com> on 2002/08/22 23:00:19 UTC

List of context objects

I would like to be able to obtain a list of all the objects in the
context so that I can display said list to template designer.  The
idea that I am working off of is from the XML/XSLT world.  With just
about any XSLT system you can show the XML stream instead of showing
the transformed result.  This is quite helpful because the template
designer can do like in Maverick and do /myfile.m?maxTransforms=0
and the user will be shown the entire XML "stream".

If my designers are using Velocity they have no such feature and it
becomes a maintenence issue for me to keep up the API of tools that
I have created, their names, and all their methods.

But with the java.lang.reflect package I can obtain a list of methods
from any class object.  As well as the return type and parameter types.
The example from Sun is only 29 lines, pretty easy stuff.  But it only
gets the methods for the Polygon class.

I would like to be able to feed it a list of the classes that the
VelocityContext is populated with and dump it to the screen.  If a user
types in /index.vm?showMethods=1 then the VelocityViewServlet could call
this method and then use a Velocity template to show the output in a nice
format - like a tree diagram or something.

Would I even need to write a class to do this or could I do most of this
processing from a template if the context is in the context?  If anyone
has any ideas I would love to hear them.

Thanks.

Charlie

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: List of context objects

Posted by Nathan Bubna <na...@esha.com>.
Charles said:
> Nathan,
> Ok, I have been trying for about 3 hours now and I am coming up completely
> empty.  I don't seem to be able to figure out how to _get_ the context
into
> a tool so I can then _put_ it into the context.  I'm pretty desperate so
I'm
> going to have to ask if you have some kind of example.

no prob.

ok, i'm going to assume you know how to create a tool and setup the
toolbox.xml.  if you don't i think Gabe's got some good examples, or there
may be something in the archives of this list or the dev one.  (i'd offer to
explain, but i leave in an hour for a week+ vacation :)

so, assuming that, create a tool like this and set it to be request scoped
in the toolbox.xml...

import org.apache.velocity.tools.view.context.ViewContext;
import org.apache.velocity.tools.view.tools.ViewTool;

public class MyContextHelperToolThing implements ViewTool
{
    public void init(Object obj)
    {
        ViewContext ctx = (ViewContext)obj;
         ctx.getRequest().setAttribute("context", ctx);
    }
}

make sense?  remember, the ViewContext (actually implemented by
ChainedContext) exposes the request attributes to the template just as if
they were put directly in the context.  now, i'll admit, this is pretty much
a hack, but it should work.  and since it's hack, it also probably wouldn't
hurt to just cast the object passed to init() as the
org.apache.velocity.tools.view.context.ChainedContext that we know it really
is and do a normal ctx.put(key, obj) on it like....

public void init(Object obj)
{
    ChainedContext ctx = (ChainedContext)obj;
     ctx.put("context", ctx);
}


hope this helps!

Nathan Bubna
nathan@esha.com


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: List of context objects

Posted by "Charles N. Harvey III" <ch...@alloy.com>.
Nathan,
Ok, I have been trying for about 3 hours now and I am coming up completely
empty.  I don't seem to be able to figure out how to _get_ the context into
a tool so I can then _put_ it into the context.  I'm pretty desperate so I'm
going to have to ask if you have some kind of example.

Should I be implementing ViewContext?  Or ServletToolboxManager?  I don't
feel comfortable enough to extend VVS.  Thanks a lot.

Charlie

-----Original Message-----
From: Nathan Bubna [mailto:nathan@esha.com]
Sent: Friday, August 23, 2002 10:36 AM
To: Velocity Users List
Subject: Re: List of context objects


Charles said:
> Denis,
> Thanks.  This is great.  One more question, does the context get put into
> the context in the VelocityViewServlet?  It doesn't seem like it.  I am
> digging around to find out where a good place to do that would be.  Should
> I create a tool to do just that?  Thanks again.

No, the VelocityViewServlet doesn't automatically put the context in the
context.  Adding it during the init method of a tool will probably be the
easiest way, apart from extending the VVS to do it for you (if you go that
route, i suggest overriding the createContext method).

Nathan Bubna
nathan@esha.com


--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: List of context objects

Posted by Nathan Bubna <na...@esha.com>.
Charles said:
> Denis,
> Thanks.  This is great.  One more question, does the context get put into
> the context in the VelocityViewServlet?  It doesn't seem like it.  I am
> digging around to find out where a good place to do that would be.  Should
> I create a tool to do just that?  Thanks again.

No, the VelocityViewServlet doesn't automatically put the context in the
context.  Adding it during the init method of a tool will probably be the
easiest way, apart from extending the VVS to do it for you (if you go that
route, i suggest overriding the createContext method).

Nathan Bubna
nathan@esha.com


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: List of context objects

Posted by "Charles N. Harvey III" <ch...@alloy.com>.
Denis,
Thanks.  This is great.  One more question, does the context get put into
the context in the VelocityViewServlet?  It doesn't seem like it.  I am
digging around to find out where a good place to do that would be.  Should
I create a tool to do just that?  Thanks again.

Charlie


-----Original Message-----
From: Denis [mailto:jido@respublica.fr]
Sent: Friday, August 23, 2002 5:33 AM
To: Velocity Users List
Subject: Re: List of context objects


Hi Charlie
On Thursday, August 22, 2002, at 10:00  pm, Charles N. Harvey III wrote:

> I would like to be able to obtain a list of all the objects in the
> context so that I can display said list to template designer.
<snip>
> Would I even need to write a class to do this or could I do most
> of this
> processing from a template if the context is in the context?  If anyone
> has any ideas I would love to hear them.

You can just put the context in the template. This gives you the
list of objects in the context:

#foreach ($key in $context.keys)
   $key: $context.get($key)
#end

Cheers,
-- Denis.

>
> Thanks.
>
> Charlie


--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: List of context objects

Posted by Denis <ji...@respublica.fr>.
Hi Charlie
On Thursday, August 22, 2002, at 10:00  pm, Charles N. Harvey III wrote:

> I would like to be able to obtain a list of all the objects in the
> context so that I can display said list to template designer.
<snip>
> Would I even need to write a class to do this or could I do most 
> of this
> processing from a template if the context is in the context?  If anyone
> has any ideas I would love to hear them.

You can just put the context in the template. This gives you the 
list of objects in the context:

#foreach ($key in $context.keys)
   $key: $context.get($key)
#end

Cheers,
-- Denis.

>
> Thanks.
>
> Charlie


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>