1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 * <s:sort comparator="myComparator" source="myList">
59 * <s:iterator>
60 * <!-- do something with each sorted elements -->
61 * <s:property value="..." />
62 * </s:iterator>
63 * </s:sort>
64 *
65 * USAGE 2:
66 * <s:sort id="mySortedList" comparator="myComparator" source="myList" />
67 *
68 * <%
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 * %>
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
111 Object srcToSort;
112 if (sourceAttr == null) {
113 srcToSort = findValue("top");
114 } else {
115 srcToSort = findValue(sourceAttr);
116 }
117 if (! MakeIterator.isIterable(srcToSort)) {
118 throw new JspException("source ["+srcToSort+"] is not iteratable");
119 }
120
121
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
129 sortIteratorFilter = new SortIteratorFilter();
130 sortIteratorFilter.setComparator(c);
131 sortIteratorFilter.setSource(srcToSort);
132 sortIteratorFilter.execute();
133
134
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
147 getStack().pop();
148 sortIteratorFilter = null;
149
150 return returnVal;
151 }
152 }