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