View Javadoc

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