You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Travis P <al...@gmail.com> on 2013/05/06 21:15:18 UTC

In VTL, simply reverse a list with java.util.Collection static method

Via web searching, I've found quite a few people reversing lists by writing
out loops.

Seems to me, we should be able to something like:

#set ($lst = ["a","b","c"])
#set ($revlst = $lst)
$Collection.reverse($revlst)

where $Collection.reverse refers to the java.util.Collections static method
static void*reverse<http://w3.hursley.ibm.com/java/docs/jdk5.0/api/java/util/Collections.html#reverse(java.util.List)>
*(List <http://w3.hursley.ibm.com/java/docs/jdk5.0/api/java/util/List.html>
<?> list)
          Reverses the order of the elements in the specified list.

but I don't see any way to do that without writing a custom tool.  Which
seems heavy-handed for such a basic thing.  Am I missing something?

(I'm using velocity-1.7, velocity-tools-view-2.0, running on tomcat-7.0.39)

-Travis

Re: In VTL, simply reverse a list with java.util.Collection static method

Posted by Travis P <al...@gmail.com>.
On Mon, May 6, 2013 at 2:30 PM, Sergiu Dumitriu wrote:

> On 05/06/2013 03:15 PM, Travis P wrote (with minor fix):
> > Via web searching, I've found quite a few people reversing lists by
> writing
> > out loops.
> >
> > Seems to me, we should be able to something like:
> >
> > #set ($lst = ["a","b","c"])
> > #set ($revlst = $lst.clone() )
> > $Collection.reverse($revlst)
> > ... <snip> ...
>
> You should use the existing SortTool tool.
>
>
> http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/generic/SortTool.html
>
> For security reasons, Velocity doesn't allow accessing raw classes or
> instantiating new objects. You must always start from a valid variable
> placed in the context.
>
> See http://velocity.apache.org/tools/releases/2.0/ for more details
> about tools and how to configure them.
>

Thanks Sergiu.

I have written my own Tools.  I was just wondering if I could use the VTL
for more rather than diving down into my own tools for such basic stuff
(and given that I found several other folks also asking this question, I'm
not alone in that desire).

I don't think the sort tool can be used for this, unless the list elements
have a property which is the their own index (and I don't see that).  I
don't want to sort based on object values.  I want to reverse the order of
a list, independent of what any values may be.

-Travis

Re: In VTL, simply reverse a list with java.util.Collection static method

Posted by Sergiu Dumitriu <se...@gmail.com>.
On 05/06/2013 03:15 PM, Travis P wrote:
> Via web searching, I've found quite a few people reversing lists by writing
> out loops.
> 
> Seems to me, we should be able to something like:
> 
> #set ($lst = ["a","b","c"])
> #set ($revlst = $lst)
> $Collection.reverse($revlst)
> 
> where $Collection.reverse refers to the java.util.Collections static method
> static void*reverse<http://w3.hursley.ibm.com/java/docs/jdk5.0/api/java/util/Collections.html#reverse(java.util.List)>
> *(List <http://w3.hursley.ibm.com/java/docs/jdk5.0/api/java/util/List.html>
> <?> list)
>           Reverses the order of the elements in the specified list.
> 
> but I don't see any way to do that without writing a custom tool.  Which
> seems heavy-handed for such a basic thing.  Am I missing something?
> 
> (I'm using velocity-1.7, velocity-tools-view-2.0, running on tomcat-7.0.39)
> 
> -Travis
> 

You should use the existing SortTool tool.

http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/generic/SortTool.html

For security reasons, Velocity doesn't allow accessing raw classes or
instantiating new objects. You must always start from a valid variable
placed in the context.

See http://velocity.apache.org/tools/releases/2.0/ for more details
about tools and how to configure them.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: In VTL, simply reverse a list with java.util.Collection static method

Posted by Nathan Bubna <nb...@gmail.com>.
Now that is a good question.  Have you tried:

<tool class="java.util.Collections"/> ??

I kind of doubt that's instantiable, but it might be worth a try.  If
it's not, you might do a hack kind of like:

<tool class="com.hack.CollectionsTool"/>

public class CollectionsTool {

  public void setVelocityContext(Context context) {
     context.put("Collections", java.util.Collections.class);
  }
}

I've never written a self-replacing tool like this, but if you
configure the VelocityViewServlet to let the "user" overwrite tools,
by giving
org.apache.velocity.tools.userCanOverwriteTools a value of true in
your init-params, it should work.

Or a similar tool that just has a:

public List reverse(List list) {
  java.util.Collections.reverse(list);
  return list;
}

method.

I wish i had a better answer.  It would be cool to patch VelocityTools
to support:

<tool static="true" class="java.util.Collections"/>

But i haven't the time to do it.  Perhaps you could open a feature
request in our JIRA instance.





On Mon, May 6, 2013 at 12:57 PM, Travis P <al...@gmail.com> wrote:
> On Mon, May 6, 2013 at 2:42 PM, Nathan Bubna <nb...@gmail.com> wrote:
>
>>
>> http://velocity.apache.org/engine/devel/developer-guide.html#supportforstaticclasses
>
>
> Yes, I'd found that.  I guess how to use that for adding a way to access
>    java.util.Collection as $Collection
> within a VTL when using the VelocityViewServlet which supplies me a context
> makes it not obvious how to take that short documentation and translate it
> into my goal.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: In VTL, simply reverse a list with java.util.Collection static method

Posted by Travis P <al...@gmail.com>.
On Mon, May 6, 2013 at 2:42 PM, Nathan Bubna <nb...@gmail.com> wrote:

>
> http://velocity.apache.org/engine/devel/developer-guide.html#supportforstaticclasses


Yes, I'd found that.  I guess how to use that for adding a way to access
   java.util.Collection as $Collection
within a VTL when using the VelocityViewServlet which supplies me a context
makes it not obvious how to take that short documentation and translate it
into my goal.

Re: In VTL, simply reverse a list with java.util.Collection static method

Posted by Nathan Bubna <nb...@gmail.com>.
http://velocity.apache.org/engine/devel/developer-guide.html#supportforstaticclasses

On Mon, May 6, 2013 at 12:15 PM, Travis P <al...@gmail.com> wrote:
> Via web searching, I've found quite a few people reversing lists by writing
> out loops.
>
> Seems to me, we should be able to something like:
>
> #set ($lst = ["a","b","c"])
> #set ($revlst = $lst)
> $Collection.reverse($revlst)
>
> where $Collection.reverse refers to the java.util.Collections static method
> static void*reverse<http://w3.hursley.ibm.com/java/docs/jdk5.0/api/java/util/Collections.html#reverse(java.util.List)>
> *(List <http://w3.hursley.ibm.com/java/docs/jdk5.0/api/java/util/List.html>
> <?> list)
>           Reverses the order of the elements in the specified list.
>
> but I don't see any way to do that without writing a custom tool.  Which
> seems heavy-handed for such a basic thing.  Am I missing something?
>
> (I'm using velocity-1.7, velocity-tools-view-2.0, running on tomcat-7.0.39)
>
> -Travis

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org