001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.views.jsp.iterator;
006:
007: import com.opensymphony.webwork.util.SubsetIteratorFilter;
008: import com.opensymphony.webwork.util.SubsetIteratorFilter.Decider;
009: import com.opensymphony.webwork.views.jsp.WebWorkBodyTagSupport;
010:
011: import javax.servlet.jsp.JspException;
012:
013: import org.apache.commons.logging.Log;
014: import org.apache.commons.logging.LogFactory;
015:
016: /**
017: * <!-- START SNIPPET: javadoc -->
018: * <b>NOTE: JSP-TAG</b>
019: *
020: * <p>A tag that takes an iterator and outputs a subset of it. It delegates to
021: * {@link com.opensymphony.webwork.util.SubsetIteratorFilter} internally to
022: * perform the subset functionality.</p>
023: * <!-- END SNIPPET: javadoc -->
024: *
025: * <!-- START SNIPPET: params -->
026: * <ul>
027: * <li>count (Object) - Indicate the number of entries to be in the resulting subset iterator</li>
028: * <li>source* (Object) - Indicate the source of which the resulting subset iterator is to be derived base on</li>
029: * <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>
030: * <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>
031: * <li>id (String) - Indicate the pageContext attribute id to store the resultant subset iterator in</li>
032: * </ul>
033: * <!-- END SNIPPET: params -->
034: *
035: *
036: * <pre>
037: * <!-- START SNIPPET: action -->
038: * public class MySubsetTagAction extends ActionSupport {
039: * public String execute() throws Exception {
040: * l = new ArrayList();
041: * l.add(new Integer(1));
042: * l.add(new Integer(2));
043: * l.add(new Integer(3));
044: * l.add(new Integer(4));
045: * l.add(new Integer(5));
046: * return "done";
047: * }
048: *
049: *
050: * public Integer[] getMyArray() {
051: * return a;
052: * }
053: *
054: * public List getMyList() {
055: * return l;
056: * }
057: *
058: * public Decider getMyDecider() {
059: * return new Decider() {
060: * public boolean decide(Object element) throws Exception {
061: * int i = ((Integer)element).intValue();
062: * return (((i % 2) == 0)?true:false);
063: * }
064: * };
065: * }
066: * }
067: * <!-- END SNIPPET: action -->
068: * </pre>
069: *
070: *
071: * <pre>
072: * <!-- START SNIPPET: example1 -->
073: * <!-- A: List basic -->
074: * <ww:subset source="myList">
075: * <ww:iterator>
076: * <ww:property />
077: * </ww:iterator>
078: * </ww:subset>
079: * <!-- END SNIPPET: example1 -->
080: * </pre>
081: *
082: * <pre>
083: * <!-- START SNIPPET: example2 -->
084: * <!-- B: List with count -->
085: * <ww:subset source="myList" count="3">
086: * <ww:iterator>
087: * <ww:property />
088: * </ww:iterator>
089: * </ww:subset>
090: * <!-- END SNIPPET: example2 -->
091: * </pre>
092: *
093: * <pre>
094: * <!-- START SNIPPET: example3 -->
095: * <!-- C: List with start -->
096: * <ww:subset source="myList" count="13" start="3">
097: * <ww:iterator>
098: * <ww:property />
099: * </ww:iterator>
100: * </ww:subset>
101: * <!-- END SNIPPET: example3 -->
102: * </pre>
103: *
104: * <pre>
105: * <!-- START SNIPPET: example4 -->
106: * <!-- D: List with id -->
107: * <ww:subset id="mySubset" source="myList" count="13" start="3" />
108: * <%
109: * Iterator i = (Iterator) pageContext.getAttribute("mySubset");
110: * while(i.hasNext()) {
111: * %>
112: * <%=i.next() %>
113: * <% } %>
114: * <!-- END SNIPPET: example4 -->
115: * </pre>
116: *
117: * <pre>
118: * <!-- START SNIPPET: example5 -->
119: * <!-- D: List with Decider -->
120: * <ww:subset source="myList" decider="myDecider">
121: * <ww:iterator>
122: * <ww:property />
123: * </ww:iterator>
124: * </ww:subset>
125: * <!-- END SNIPPET: example5 -->
126: * </pre>
127: *
128: *
129: * @author Rickard �berg (rickard@dreambean.com)
130: * @author tm_jee ( tm_jee(at)yahoo.co.uk )
131: * @ww.tag name="subset" tld-body-content="JSP"
132: * description="Takes an iterator and outputs a subset of it"
133: */
134: public class SubsetIteratorTag extends WebWorkBodyTagSupport {
135:
136: private static final long serialVersionUID = -6252696081713080102L;
137:
138: private static final Log _log = LogFactory
139: .getLog(SubsetIteratorTag.class);
140:
141: String countAttr;
142: String sourceAttr;
143: String startAttr;
144: String deciderAttr;
145:
146: SubsetIteratorFilter subsetIteratorFilter = null;
147:
148: /**
149: * @ww.tagattribute required="false" type="Integer"
150: * description="Indicate the number of entries to be in the resulting subset iterator"
151: */
152: public void setCount(String count) {
153: countAttr = count;
154: }
155:
156: /**
157: * @ww.tagattribute required="false"
158: * description="Indicate the source of which the resulting subset iterator is to be derived base on"
159: */
160: public void setSource(String source) {
161: sourceAttr = source;
162: }
163:
164: /**
165: * @ww.tagattribute required="false" type="Integer"
166: * 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"
167: */
168: public void setStart(String start) {
169: startAttr = start;
170: }
171:
172: /**
173: * @ww.tagattribute required="false" type="com.opensymphony.webwork.util.SubsetIteratorFilter.Decider"
174: * description="Extension to plug-in a decider to determine if that particular entry is to be included in the resulting subset iterator"
175: */
176: public void setDecider(String decider) {
177: deciderAttr = decider;
178: }
179:
180: public int doStartTag() throws JspException {
181:
182: // source
183: Object source = null;
184: if (sourceAttr == null && sourceAttr.length() <= 0) {
185: source = findValue("top");
186: } else {
187: source = findValue(sourceAttr);
188: }
189:
190: // count
191: int count = -1;
192: if (countAttr != null && countAttr.length() > 0) {
193: Object countObj = findValue(countAttr);
194: if (countObj instanceof Integer) {
195: count = ((Integer) countObj).intValue();
196: } else if (countObj instanceof Float) {
197: count = ((Float) countObj).intValue();
198: } else if (countObj instanceof Long) {
199: count = ((Long) countObj).intValue();
200: } else if (countObj instanceof Double) {
201: count = ((Long) countObj).intValue();
202: } else if (countObj instanceof String) {
203: try {
204: count = Integer.parseInt((String) countObj);
205: } catch (NumberFormatException e) {
206: _log.warn("unable to convert count attribute ["
207: + countObj
208: + "] to number, ignore count attribute", e);
209: }
210: }
211: }
212:
213: // start
214: int start = 0;
215: if (startAttr != null && startAttr.length() > 0) {
216: Object startObj = findValue(startAttr);
217: if (startObj instanceof Integer) {
218: start = ((Integer) startObj).intValue();
219: } else if (startObj instanceof Float) {
220: start = ((Float) startObj).intValue();
221: } else if (startObj instanceof Long) {
222: start = ((Long) startObj).intValue();
223: } else if (startObj instanceof Double) {
224: start = ((Long) startObj).intValue();
225: } else if (startObj instanceof String) {
226: try {
227: start = Integer.parseInt((String) startObj);
228: } catch (NumberFormatException e) {
229: _log.warn("unable to convert count attribute ["
230: + startObj
231: + "] to number, ignore count attribute", e);
232: }
233: }
234: }
235:
236: // decider
237: Decider decider = null;
238: if (deciderAttr != null && deciderAttr.length() > 0) {
239: Object deciderObj = findValue(deciderAttr);
240: if (!(deciderObj instanceof Decider)) {
241: throw new JspException("decider found from stack ["
242: + deciderObj + "] does not implement "
243: + Decider.class);
244: }
245: decider = (Decider) deciderObj;
246: }
247:
248: subsetIteratorFilter = new SubsetIteratorFilter();
249: subsetIteratorFilter.setCount(count);
250: subsetIteratorFilter.setDecider(decider);
251: subsetIteratorFilter.setSource(source);
252: subsetIteratorFilter.setStart(start);
253: subsetIteratorFilter.execute();
254:
255: getStack().push(subsetIteratorFilter);
256: if (getId() != null) {
257: pageContext.setAttribute(getId(), subsetIteratorFilter);
258: }
259:
260: return EVAL_BODY_INCLUDE;
261: }
262:
263: public int doEndTag() throws JspException {
264:
265: getStack().pop();
266:
267: subsetIteratorFilter = null;
268:
269: return EVAL_PAGE;
270: }
271: }
|