1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 * <s:sort comparator="myComparator" source="myList">
65 * <s:iterator>
66 * <!-- do something with each sorted elements -->
67 * <s:property value="..." />
68 * </s:iterator>
69 * </s:sort>
70 *
71 * USAGE 2:
72 * <s:sort var="mySortedList" comparator="myComparator" source="myList" />
73 *
74 * <%
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 * %>
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
124 Object srcToSort;
125 if (sourceAttr == null) {
126 srcToSort = findValue("top");
127 } else {
128 srcToSort = findValue(sourceAttr);
129 }
130 if (! MakeIterator.isIterable(srcToSort)) {
131 throw new JspException("source ["+srcToSort+"] is not iteratable");
132 }
133
134
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
142 sortIteratorFilter = new SortIteratorFilter();
143 sortIteratorFilter.setComparator(c);
144 sortIteratorFilter.setSource(srcToSort);
145 sortIteratorFilter.execute();
146
147
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
160 getStack().pop();
161 sortIteratorFilter = null;
162
163 return returnVal;
164 }
165 }