dion 2004/10/26 09:42:56 Modified: jelly/jelly-tags/util/src/java/org/apache/commons/jelly/tags/util UtilTagLibrary.java jelly/jelly-tags/util/xdocs changes.xml jelly/jelly-tags/util/src/test/org/apache/commons/jelly/tags/util suite.jelly Added: jelly/jelly-tags/util/src/java/org/apache/commons/jelly/tags/util SortTag.java jelly/jelly-tags/util/src/test/org/apache/commons/jelly/tags/util Customer.java Log: Jelly-157: Add a util:sort tag to sort lists. Revision Changes Path 1.6 +3 -2 jakarta-commons/jelly/jelly-tags/util/src/java/org/apache/commons/jelly/tags/util/UtilTagLibrary.java Index: UtilTagLibrary.java =================================================================== RCS file: /home/cvs/jakarta-commons/jelly/jelly-tags/util/src/java/org/apache/commons/jelly/tags/util/UtilTagLibrary.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- UtilTagLibrary.java 9 Sep 2004 12:22:43 -0000 1.5 +++ UtilTagLibrary.java 26 Oct 2004 16:42:56 -0000 1.6 @@ -30,7 +30,8 @@ registerTag("loadText", LoadTextTag.class); registerTag("properties", PropertiesTag.class); registerTag("replace", ReplaceTag.class); - registerTag("tokenize", TokenizeTag.class); registerTag("sleep", SleepTag.class); + registerTag("sort", SortTag.class); + registerTag("tokenize", TokenizeTag.class); } } 1.1 jakarta-commons/jelly/jelly-tags/util/src/java/org/apache/commons/jelly/tags/util/SortTag.java Index: SortTag.java =================================================================== /* * Copyright 2002,2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.jelly.tags.util; import java.util.ArrayList; import java.util.Collections; import java.util.List; import org.apache.commons.beanutils.BeanComparator; import org.apache.commons.jelly.JellyTagException; import org.apache.commons.jelly.MissingAttributeException; import org.apache.commons.jelly.TagSupport; import org.apache.commons.jelly.XMLOutput; public class SortTag extends TagSupport { /** things to sort */ private List items; /** the variable to store the result in */ private String var; /** property of the beans to sort on, if any */ private String property; // Tag interface //------------------------------------------------------------------------- public void doTag(final XMLOutput output) throws JellyTagException { if (var == null) { throw new MissingAttributeException("var"); } if (items == null) { throw new MissingAttributeException("items"); } List sorted = new ArrayList(items); if (property == null) { Collections.sort(sorted); } else { BeanComparator comparator = new BeanComparator(property); Collections.sort(sorted, comparator); } context.setVariable(var, sorted); } /** * Set the items to be sorted * @param newItems some collection */ public void setItems(List newItems) { items = newItems; } /** * The variable to hold the sorted collection. * @param newVar the name of the variable. */ public void setVar(String newVar) { var = newVar; } public void setProperty(String newProperty) { property = newProperty; } } 1.5 +1 -0 jakarta-commons/jelly/jelly-tags/util/xdocs/changes.xml Index: changes.xml =================================================================== RCS file: /home/cvs/jakarta-commons/jelly/jelly-tags/util/xdocs/changes.xml,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- changes.xml 12 Sep 2004 16:04:59 -0000 1.4 +++ changes.xml 26 Oct 2004 16:42:56 -0000 1.5 @@ -25,6 +25,7 @@ </properties> <body> <release version="1.1-SNAPSHOT" date="in CVS"> + <action dev="dion" type="add" issue="JELLY-157">Add util:sort to sort lists, including sorting by bean properties</action> <action dev="dion" type="update" issue="JELLY-57" due-to="Bill Keese">support encoding parameter for util:loadText</action> </release> <release version="1.0" date="2004-09-10"> 1.6 +37 -0 jakarta-commons/jelly/jelly-tags/util/src/test/org/apache/commons/jelly/tags/util/suite.jelly Index: suite.jelly =================================================================== RCS file: /home/cvs/jakarta-commons/jelly/jelly-tags/util/src/test/org/apache/commons/jelly/tags/util/suite.jelly,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- suite.jelly 31 Aug 2004 09:16:28 -0000 1.5 +++ suite.jelly 26 Oct 2004 16:42:56 -0000 1.6 @@ -150,5 +150,42 @@ var="suite" />
<test:assert test="${suite != null}" >The suite should exist</test:assert> </test:case> + + <test:case name="testSortBasic"> + <j:new var="testCollection" className="java.util.ArrayList"/> + <j:mute> + ${testCollection.add('Hello')} + ${testCollection.add('World')} + ${testCollection.add('Jelly')} + </j:mute> + <util:sort var="sorted" items="${testCollection}" /> + <test:assertEquals expected="Hello" actual="${sorted[0]}"/> + <test:assertEquals expected="Jelly" actual="${sorted[1]}"/> + <test:assertEquals expected="World" actual="${sorted[2]}"/> + </test:case> + + <test:case name="testSortBean"> + <j:new var="testCollection" className="java.util.ArrayList"/> + <j:useBean var="cust1" class="org.apache.commons.jelly.util.Customer" city="Sydney" location="Australia" name="Fred Nerk"/> + <j:useBean var="cust2" class="org.apache.commons.jelly.util.Customer" city="Melbourne" location="Australia" name="Joe Nerk"/> + <j:useBean var="cust3" class="org.apache.commons.jelly.util.Customer" city="San Francisco" location="U.S.A." name="Colette Cool"/> + <j:mute> + ${testCollection.add(cust1)} + ${testCollection.add(cust2)} + ${testCollection.add(cust3)} + </j:mute> + <!-- test sorting by the name property --> + <util:sort var="sortedByName" items="${testCollection}" property="name"/> + <test:assertEquals expected="Colette Cool" actual="${sortedByName[0].name}"/> + <test:assertEquals expected="Fred Nerk" actual="${sortedByName[1].name}"/> + <test:assertEquals expected="Joe Nerk" actual="${sortedByName[2].name}"/> + + <!-- test sorting by the location property --> + <util:sort var="sortedByLocation" items="${testCollection}" property="location"/> + <test:assertEquals expected="Australia" actual="${sortedByLocation[0].location}"/> + <test:assertEquals expected="Australia" actual="${sortedByLocation[1].location}"/> + <test:assertEquals expected="U.S.A." actual="${sortedByLocation[2].location}"/> + + </test:case> </test:suite> 1.1 jakarta-commons/jelly/jelly-tags/util/src/test/org/apache/commons/jelly/tags/util/Customer.java Index: Customer.java =================================================================== /* * Copyright 2002,2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.jelly.util; import java.util.ArrayList; import java.util.Iterator; import java.util.List; /** * A sample bean that we can construct via Jelly tags * * @author <a href="mailto:[EMAIL PROTECTED]">James Strachan</a> * @version $Revision: 1.1 $ */ public class Customer { private String name; private String city; private String location; public Customer() { } public Customer(String name) { setName(name); } public Customer(String name, String city) { setName(name); setCity(city); } public Customer(Customer cust) { setName(cust.getName()); setCity(cust.getCity()); setLocation(cust.getLocation()); } public String toString() { return super.toString() + "[name=" + name + ";city=" + city + "]"; } /** * Returns the city. * @return String */ public String getCity() { return city; } /** * Returns the location. * @return String */ public String getLocation() { return location; } /** * Returns the name. * @return String */ public String getName() { return name; } /** * Sets the city. * @param city The city to set */ public void setCity(String city) { this.city = city; } /** * Sets the location. * @param location The location to set */ public void setLocation(String location) { this.location = location; } /** * Sets the name. * @param name The name to set */ public void setName(String name) { this.name = name; } } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]