001: /*
002: * $Id: SubsetIteratorTag.java 540569 2007-05-22 12:54:52Z mrdon $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.views.jsp.iterator;
022:
023: import javax.servlet.jsp.JspException;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.apache.struts2.views.annotations.StrutsTag;
028: import org.apache.struts2.views.annotations.StrutsTagAttribute;
029: import org.apache.struts2.util.SubsetIteratorFilter;
030: import org.apache.struts2.util.SubsetIteratorFilter.Decider;
031: import org.apache.struts2.views.jsp.StrutsBodyTagSupport;
032:
033: /**
034: * <!-- START SNIPPET: javadoc -->
035: * <b>NOTE: JSP-TAG</b>
036: *
037: * <p>A tag that takes an iterator and outputs a subset of it. It delegates to
038: * {@link org.apache.struts2.util.SubsetIteratorFilter} internally to
039: * perform the subset functionality.</p>
040: * <!-- END SNIPPET: javadoc -->
041: *
042: * <!-- START SNIPPET: params -->
043: * <ul>
044: * <li>count (Object) - Indicate the number of entries to be in the resulting subset iterator</li>
045: * <li>source* (Object) - Indicate the source of which the resulting subset iterator is to be derived base on</li>
046: * <li>start (Object) - Indicate the starting index (eg. first entry is 0) of entries in the source to be available as the first entry in the resulting subset iterator</li>
047: * <li>decider (Object) - Extension to plug-in a decider to determine if that particular entry is to be included in the resulting subset iterator</li>
048: * <li>id (String) - Indicate the pageContext attribute id to store the resultant subset iterator in</li>
049: * </ul>
050: * <!-- END SNIPPET: params -->
051: *
052: *
053: * <pre>
054: * <!-- START SNIPPET: action -->
055: * public class MySubsetTagAction extends ActionSupport {
056: * public String execute() throws Exception {
057: * l = new ArrayList();
058: * l.add(new Integer(1));
059: * l.add(new Integer(2));
060: * l.add(new Integer(3));
061: * l.add(new Integer(4));
062: * l.add(new Integer(5));
063: * return "done";
064: * }
065: *
066: *
067: * public Integer[] getMyArray() {
068: * return a;
069: * }
070: *
071: * public List getMyList() {
072: * return l;
073: * }
074: *
075: * public Decider getMyDecider() {
076: * return new Decider() {
077: * public boolean decide(Object element) throws Exception {
078: * int i = ((Integer)element).intValue();
079: * return (((i % 2) == 0)?true:false);
080: * }
081: * };
082: * }
083: * }
084: * <!-- END SNIPPET: action -->
085: * </pre>
086: *
087: *
088: * <pre>
089: * <!-- START SNIPPET: example1 -->
090: * <!-- s: List basic -->
091: * <s:subset source="myList">
092: * <s:iterator>
093: * <s:property />
094: * </s:iterator>
095: * </s:subset>
096: * <!-- END SNIPPET: example1 -->
097: * </pre>
098: *
099: * <pre>
100: * <!-- START SNIPPET: example2 -->
101: * <!-- B: List with count -->
102: * <s:subset source="myList" count="3">
103: * <s:iterator>
104: * <s:property />
105: * </s:iterator>
106: * </s:subset>
107: * <!-- END SNIPPET: example2 -->
108: * </pre>
109: *
110: * <pre>
111: * <!-- START SNIPPET: example3 -->
112: * <!-- C: List with start -->
113: * <s:subset source="myList" count="13" start="3">
114: * <s:iterator>
115: * <s:property />
116: * </s:iterator>
117: * </s:subset>
118: * <!-- END SNIPPET: example3 -->
119: * </pre>
120: *
121: * <pre>
122: * <!-- START SNIPPET: example4 -->
123: * <!-- D: List with id -->
124: * <s:subset id="mySubset" source="myList" count="13" start="3" />
125: * <%
126: * Iterator i = (Iterator) pageContext.getAttribute("mySubset");
127: * while(i.hasNext()) {
128: * %>
129: * <%=i.next() %>
130: * <% } %>
131: * <!-- END SNIPPET: example4 -->
132: * </pre>
133: *
134: * <pre>
135: * <!-- START SNIPPET: example5 -->
136: * <!-- D: List with Decider -->
137: * <s:subset source="myList" decider="myDecider">
138: * <s:iterator>
139: * <s:property />
140: * </s:iterator>
141: * </s:subset>
142: * <!-- END SNIPPET: example5 -->
143: * </pre>
144: *
145: *
146: * @s.tag name="subset" tld-body-content="JSP"
147: * description="Takes an iterator and outputs a subset of it"
148: */
149: @StrutsTag(name="subset",tldTagClass="org.apache.struts2.views.jsp.iterator.SubsetIteratorTag",description="Takes an iterator and outputs a subset of it.")
150: public class SubsetIteratorTag extends StrutsBodyTagSupport {
151:
152: private static final long serialVersionUID = -6252696081713080102L;
153:
154: private static final Log _log = LogFactory
155: .getLog(SubsetIteratorTag.class);
156:
157: String countAttr;
158: String sourceAttr;
159: String startAttr;
160: String deciderAttr;
161:
162: SubsetIteratorFilter subsetIteratorFilter = null;
163:
164: @StrutsTagAttribute(type="Integer",description="Indicate the number of entries to be in the resulting subset iterator")
165: public void setCount(String count) {
166: countAttr = count;
167: }
168:
169: @StrutsTagAttribute(description="Indicate the source of which the resulting subset iterator is to be derived base on")
170: public void setSource(String source) {
171: sourceAttr = source;
172: }
173:
174: /**
175: * @s.tagattribute required="false" type="Integer"
176: * description="Indicate the starting index (eg. first entry is 0) of entries in the source to be available as the first entry in the resulting subset iterator"
177: */
178: @StrutsTagAttribute(type="Integer",description="Indicate the starting index (eg. first entry is 0) of entries in the source to be available as the first entry in the resulting subset iterator")
179: public void setStart(String start) {
180: startAttr = start;
181: }
182:
183: @StrutsTagAttribute(type="org.apache.struts2.util.SubsetIteratorFilter.Decider",description="Extension to plug-in a decider to determine if that particular entry is to be included in the resulting subset iterator")
184: public void setDecider(String decider) {
185: deciderAttr = decider;
186: }
187:
188: public int doStartTag() throws JspException {
189:
190: // source
191: Object source = null;
192: if (sourceAttr == null || sourceAttr.length() == 0) {
193: source = findValue("top");
194: } else {
195: source = findValue(sourceAttr);
196: }
197:
198: // count
199: int count = -1;
200: if (countAttr != null && countAttr.length() > 0) {
201: Object countObj = findValue(countAttr);
202: if (countObj instanceof Integer) {
203: count = ((Integer) countObj).intValue();
204: } else if (countObj instanceof Float) {
205: count = ((Float) countObj).intValue();
206: } else if (countObj instanceof Long) {
207: count = ((Long) countObj).intValue();
208: } else if (countObj instanceof Double) {
209: count = ((Long) countObj).intValue();
210: } else if (countObj instanceof String) {
211: try {
212: count = Integer.parseInt((String) countObj);
213: } catch (NumberFormatException e) {
214: _log.warn("unable to convert count attribute ["
215: + countObj
216: + "] to number, ignore count attribute", e);
217: }
218: }
219: }
220:
221: // start
222: int start = 0;
223: if (startAttr != null && startAttr.length() > 0) {
224: Object startObj = findValue(startAttr);
225: if (startObj instanceof Integer) {
226: start = ((Integer) startObj).intValue();
227: } else if (startObj instanceof Float) {
228: start = ((Float) startObj).intValue();
229: } else if (startObj instanceof Long) {
230: start = ((Long) startObj).intValue();
231: } else if (startObj instanceof Double) {
232: start = ((Long) startObj).intValue();
233: } else if (startObj instanceof String) {
234: try {
235: start = Integer.parseInt((String) startObj);
236: } catch (NumberFormatException e) {
237: _log.warn("unable to convert count attribute ["
238: + startObj
239: + "] to number, ignore count attribute", e);
240: }
241: }
242: }
243:
244: // decider
245: Decider decider = null;
246: if (deciderAttr != null && deciderAttr.length() > 0) {
247: Object deciderObj = findValue(deciderAttr);
248: if (!(deciderObj instanceof Decider)) {
249: throw new JspException("decider found from stack ["
250: + deciderObj + "] does not implement "
251: + Decider.class);
252: }
253: decider = (Decider) deciderObj;
254: }
255:
256: subsetIteratorFilter = new SubsetIteratorFilter();
257: subsetIteratorFilter.setCount(count);
258: subsetIteratorFilter.setDecider(decider);
259: subsetIteratorFilter.setSource(source);
260: subsetIteratorFilter.setStart(start);
261: subsetIteratorFilter.execute();
262:
263: getStack().push(subsetIteratorFilter);
264: if (getId() != null) {
265: pageContext.setAttribute(getId(), subsetIteratorFilter);
266: }
267:
268: return EVAL_BODY_INCLUDE;
269: }
270:
271: public int doEndTag() throws JspException {
272:
273: getStack().pop();
274:
275: subsetIteratorFilter = null;
276:
277: return EVAL_PAGE;
278: }
279: }
|