View Javadoc

1   /*
2    * $Id: SortIteratorTag.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.struts2.views.jsp.iterator;
23  
24  import java.util.Comparator;
25  
26  import javax.servlet.jsp.JspException;
27  
28  import org.apache.struts2.views.annotations.StrutsTag;
29  import org.apache.struts2.views.annotations.StrutsTagAttribute;
30  import org.apache.struts2.util.MakeIterator;
31  import org.apache.struts2.util.SortIteratorFilter;
32  import org.apache.struts2.views.jsp.StrutsBodyTagSupport;
33  
34  
35  /***
36   * <!-- START SNIPPET: javadoc -->
37   *
38   * <b>NOTE: JSP-TAG</b>
39   *
40   * <p>A Tag that sorts a List using a Comparator both passed in as the tag attribute.
41   * If 'var' attribute is specified, the sorted list will be placed into the PageContext
42   * attribute using the key specified by 'var'. The sorted list will ALWAYS be
43   * pushed into the stack and poped at the end of this tag.</p>
44   *
45   * <!-- END SNIPPET: javadoc -->
46   *
47   *
48   * <!-- START SNIPPET: params -->
49   *
50   * <ul>
51   *      <li>id (String) - if specified, the sorted iterator will be place with this id under page context</li>
52   *      <li>source (Object) - the source for the sort to take place (should be iteratable) else JspException will be thrown</li>
53   *      <li>comparator* (Object) - the comparator used to do sorting (should be a type of Comparator or its decendent) else JspException will be thrown</li>
54   * </ul>
55   *
56   * <!-- END SNIPPET: params -->
57   *
58   *
59   *
60   * <pre>
61   * <!-- START SNIPPET: example -->
62   *
63   * USAGE 1:
64   * &lt;s:sort comparator="myComparator" source="myList"&gt;
65   *      &lt;s:iterator&gt;
66   *      &lt;!-- do something with each sorted elements --&gt;
67   *      &lt;s:property value="..." /&gt;
68   *      &lt;/s:iterator&gt;
69   * &lt;/s:sort&gt;
70   *
71   * USAGE 2:
72   * &lt;s:sort var="mySortedList" comparator="myComparator" source="myList" /&gt;
73   *
74   * &lt;%
75   *    Iterator sortedIterator = (Iterator) pageContext.getAttribute("mySortedList");
76   *    for (Iterator i = sortedIterator; i.hasNext(); ) {
77   *      // do something with each of the sorted elements
78   *    }
79   * %&gt;
80   *
81   * <!-- END SNIPPET: example -->
82   * </pre>
83   *
84   *
85   * @see org.apache.struts2.util.SortIteratorFilter
86   *
87   * @s.tag name="sort" tld-body-content="JSP"
88   * description="Sort a List using a Comparator both passed in as the tag attribute."
89   */
90  @StrutsTag(name="sort", tldTagClass="org.apache.struts2.views.jsp.iterator.SortIteratorTag", 
91          description="Sort a List using a Comparator both passed in as the tag attribute.")
92  public class SortIteratorTag extends StrutsBodyTagSupport {
93  
94      private static final long serialVersionUID = -7835719609764092235L;
95  
96      String comparatorAttr;
97      String sourceAttr;
98      String var;
99  
100     SortIteratorFilter sortIteratorFilter = null;
101 
102     @StrutsTagAttribute(required=true,type="java.util.Comparator", description="The comparator to use")
103     public void setComparator(String comparator) {
104         comparatorAttr = comparator;
105     }
106 
107     @StrutsTagAttribute(description="The iterable source to sort")
108     public void setSource(String source) {
109         sourceAttr = source;
110     }
111     
112     @StrutsTagAttribute(description="Deprecated. Use 'var' instead")
113     public void setId(String string) {
114         setVar(string);
115     }
116     
117     @StrutsTagAttribute(description="The name to store the resultant iterator into page context, if such name is supplied")
118     public void setVar(String var) {
119         this.var = var;
120     }
121 
122     public int doStartTag() throws JspException {
123         // Source
124         Object srcToSort;
125         if (sourceAttr == null) {
126             srcToSort = findValue("top");
127         } else {
128             srcToSort = findValue(sourceAttr);
129         }
130         if (! MakeIterator.isIterable(srcToSort)) { // see if source is Iteratable
131             throw new JspException("source ["+srcToSort+"] is not iteratable");
132         }
133 
134         // Comparator
135         Object comparatorObj = findValue(comparatorAttr);
136         if (! (comparatorObj instanceof Comparator)) {
137             throw new JspException("comparator ["+comparatorObj+"] does not implements Comparator interface");
138         }
139         Comparator c = (Comparator) findValue(comparatorAttr);
140 
141         // SortIteratorFilter
142         sortIteratorFilter = new SortIteratorFilter();
143         sortIteratorFilter.setComparator(c);
144         sortIteratorFilter.setSource(srcToSort);
145         sortIteratorFilter.execute();
146 
147         // push sorted iterator into stack, so nexted tag have access to it
148         getStack().push(sortIteratorFilter);
149         if (var != null && var.length() > 0) {
150             pageContext.setAttribute(var, sortIteratorFilter);
151         }
152 
153         return EVAL_BODY_INCLUDE;
154     }
155 
156     public int doEndTag() throws JspException {
157         int returnVal =  super.doEndTag();
158 
159         // pop sorted list from stack at the end of tag
160         getStack().pop();
161         sortIteratorFilter = null;
162 
163         return returnVal;
164     }
165 }