View Javadoc

1   /*
2    * $Id: SortIteratorTag.java 497654 2007-01-19 00:21:57Z rgielen $
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  package org.apache.struts2.views.jsp.iterator;
22  
23  import java.util.Comparator;
24  
25  import javax.servlet.jsp.JspException;
26  
27  import org.apache.struts2.views.annotations.StrutsTag;
28  import org.apache.struts2.views.annotations.StrutsTagAttribute;
29  import org.apache.struts2.util.MakeIterator;
30  import org.apache.struts2.util.SortIteratorFilter;
31  import org.apache.struts2.views.jsp.StrutsBodyTagSupport;
32  
33  
34  /***
35   * <!-- START SNIPPET: javadoc -->
36   *
37   * <b>NOTE: JSP-TAG</b>
38   *
39   * <p>A Tag that sorts a List using a Comparator both passed in as the tag attribute.
40   * If 'id' attribute is specified, the sorted list will be placed into the PageContext
41   * attribute using the key specified by 'id'. The sorted list will ALWAYS be
42   * pushed into the stack and poped at the end of this tag.</p>
43   *
44   * <!-- END SNIPPET: javadoc -->
45   *
46   *
47   * <!-- START SNIPPET: params -->
48   *
49   * <ul>
50   *      <li>id (String) - if specified, the sorted iterator will be place with this id under page context</li>
51   *      <li>source (Object) - the source for the sort to take place (should be iteratable) else JspException will be thrown</li>
52   *      <li>comparator* (Object) - the comparator used to do sorting (should be a type of Comparator or its decendent) else JspException will be thrown</li>
53   * </ul>
54   *
55   * <!-- END SNIPPET: params -->
56   *
57   *
58   *
59   * <pre>
60   * <!-- START SNIPPET: example -->
61   *
62   * USAGE 1:
63   * &lt;s:sort comparator="myComparator" source="myList"&gt;
64   *      &lt;s:iterator&gt;
65   *      &lt;!-- do something with each sorted elements --&gt;
66   *      &lt;s:property value="..." /&gt;
67   *      &lt;/s:iterator&gt;
68   * &lt;/s:sort&gt;
69   *
70   * USAGE 2:
71   * &lt;s:sort id="mySortedList" comparator="myComparator" source="myList" /&gt;
72   *
73   * &lt;%
74   *    Iterator sortedIterator = (Iterator) pageContext.getAttribute("mySortedList");
75   *    for (Iterator i = sortedIterator; i.hasNext(); ) {
76   *      // do something with each of the sorted elements
77   *    }
78   * %&gt;
79   *
80   * <!-- END SNIPPET: example -->
81   * </pre>
82   *
83   *
84   * @see org.apache.struts2.util.SortIteratorFilter
85   *
86   * @s.tag name="sort" tld-body-content="JSP"
87   * description="Sort a List using a Comparator both passed in as the tag attribute."
88   */
89  @StrutsTag(name="sort", tldTagClass="org.apache.struts2.views.jsp.iterator.SortIteratorTag", 
90          description="Sort a List using a Comparator both passed in as the tag attribute.")
91  public class SortIteratorTag extends StrutsBodyTagSupport {
92  
93      private static final long serialVersionUID = -7835719609764092235L;
94  
95      String comparatorAttr;
96      String sourceAttr;
97  
98      SortIteratorFilter sortIteratorFilter = null;
99  
100     @StrutsTagAttribute(required=true,type="java.util.Comparator", description="The comparator to use")
101     public void setComparator(String comparator) {
102         comparatorAttr = comparator;
103     }
104 
105     @StrutsTagAttribute(description="The iterable source to sort")
106     public void setSource(String source) {
107         sourceAttr = source;
108     }
109 
110     public int doStartTag() throws JspException {
111         // Source
112         Object srcToSort;
113         if (sourceAttr == null) {
114             srcToSort = findValue("top");
115         } else {
116             srcToSort = findValue(sourceAttr);
117         }
118         if (! MakeIterator.isIterable(srcToSort)) { // see if source is Iteratable
119             throw new JspException("source ["+srcToSort+"] is not iteratable");
120         }
121 
122         // Comparator
123         Object comparatorObj = findValue(comparatorAttr);
124         if (! (comparatorObj instanceof Comparator)) {
125             throw new JspException("comparator ["+comparatorObj+"] does not implements Comparator interface");
126         }
127         Comparator c = (Comparator) findValue(comparatorAttr);
128 
129         // SortIteratorFilter
130         sortIteratorFilter = new SortIteratorFilter();
131         sortIteratorFilter.setComparator(c);
132         sortIteratorFilter.setSource(srcToSort);
133         sortIteratorFilter.execute();
134 
135         // push sorted iterator into stack, so nexted tag have access to it
136         getStack().push(sortIteratorFilter);
137         if (getId() != null && getId().length() > 0) {
138             pageContext.setAttribute(getId(), sortIteratorFilter);
139         }
140 
141         return EVAL_BODY_INCLUDE;
142     }
143 
144     public int doEndTag() throws JspException {
145         int returnVal =  super.doEndTag();
146 
147         // pop sorted list from stack at the end of tag
148         getStack().pop();
149         sortIteratorFilter = null;
150 
151         return returnVal;
152     }
153 }