KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > views > jsp > IteratorTag


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.views.jsp;
6
7 import com.opensymphony.webwork.util.MakeIterator;
8 import com.opensymphony.xwork.util.OgnlValueStack;
9
10 import javax.servlet.jsp.JspException JavaDoc;
11 import javax.servlet.jsp.PageContext JavaDoc;
12 import java.util.Iterator JavaDoc;
13
14
15 /**
16  * <p>Iterator will iterate over a value. An iterable value can be either of: java.util.Collection,
17  * java.util.Iterator, java.util.Enumeration, java.util.Map, array.</p>
18  * <p/>
19  * <p>Example:</p>
20  * <pre>
21  * &lt;ww:iterator value="days"&gt;
22  * &lt;p&gt;day is: &lt;ww:property/&gt;&lt;/p&gt;
23  * &lt;/ww:iterator&gt;
24  * </pre>
25  * <p>The above example retrieves the value of the getDays() method of the current object on the value stack and uses
26  * it to iterate over. The &lt;ww:property/&gt; tag prints out the current value of the iterator.</p>
27  * <p>The following example uses a {@link BeanTag} and places it into the ActionContext. The iterator tag will
28  * retrieve that object from the ActionContext and then calls its getDays() method as above. The status attribute is also
29  * used to create a {@link IteratorStatus} object, which in this example, its odd() method is used to
30  * alternate row colours:</p>
31  * <pre>
32  * &lt;ww:bean name="'com.opensymphony.webwork.example.IteratorExample'" id="it"&gt;
33  * &lt;ww:param name="'day'" value="'foo'"/&gt;
34  * &lt;ww:param name="'day'" value="'bar'"/&gt;
35  * &lt;/ww:bean&gt;
36  * <p/>
37  * &lt;table border="0" cellspacing="0" cellpadding="1"&gt;
38  * &lt;tr&gt;
39  * &lt;th&gt;Days of the week&lt;/th&gt;
40  * &lt;/tr&gt;
41  * <p/>
42  * &lt;ww:iterator value="#it.days" status="rowstatus"&gt;
43  * &lt;tr&gt;
44  * &lt;ww:if test="#rowstatus.odd == true"&gt;
45  * &lt;td style="background: grey"&gt;&lt;ww:property/&gt;&lt;/td&gt;
46  * &lt;/ww:if&gt;
47  * &lt;ww:else&gt;
48  * &lt;td&gt;&lt;ww:property/&gt;&lt;/td&gt;
49  * &lt;/ww:else&gt;
50  * &lt;/tr&gt;
51  * &lt;/ww:iterator&gt;
52  * &lt;/table&gt;
53  * </pre>
54  *
55  * @author $Author: jcarreira $
56  * @author Rick Salsa (rsal@mb.sympatico.ca)
57  * @version $Revision: 1.14 $
58  */

59 public class IteratorTag extends WebWorkBodyTagSupport {
60     //~ Instance fields ////////////////////////////////////////////////////////
61

62     protected Iterator iterator;
63     protected IteratorStatus status;
64     protected Object JavaDoc oldStatus;
65     protected IteratorStatus.StatusState statusState;
66     protected String JavaDoc statusAttr;
67     protected String JavaDoc value;
68
69     //~ Methods ////////////////////////////////////////////////////////////////
70

71     public void setStatus(String JavaDoc name) {
72         this.statusAttr = name;
73     }
74
75     public void setValue(String JavaDoc value) {
76         this.value = value;
77     }
78
79     public int doAfterBody() throws JspException JavaDoc {
80         OgnlValueStack stack = getStack();
81         stack.pop();
82
83         if (iterator.hasNext()) {
84             Object JavaDoc currentValue = iterator.next();
85             stack.push(currentValue);
86
87             String JavaDoc id = getId();
88
89             if ((id != null) && (currentValue != null)) {
90                 pageContext.setAttribute(id, currentValue);
91                 pageContext.setAttribute(id, currentValue, PageContext.REQUEST_SCOPE);
92             }
93
94             // Update status
95
if (status != null) {
96                 statusState.next(); // Increase counter
97
statusState.setLast(!iterator.hasNext());
98             }
99
100             return EVAL_BODY_AGAIN;
101         } else {
102             // Reset status object in case someone else uses the same name in another iterator tag instance
103
if (status != null) {
104                 if (oldStatus == null) {
105                     stack.getContext().put(statusAttr, null);
106                 } else {
107                     stack.getContext().put(statusAttr, oldStatus);
108                 }
109             }
110
111             // Release objects
112
iterator = null;
113             status = null;
114
115             try {
116                 bodyContent.writeOut(bodyContent.getEnclosingWriter());
117             } catch (Exception JavaDoc e) {
118                 throw new JspException JavaDoc(e.getMessage());
119             }
120
121             return SKIP_BODY;
122         }
123     }
124
125     public int doStartTag() throws JspException JavaDoc {
126         //Create an iterator status if the status attribute was set.
127
if (statusAttr != null) {
128             statusState = new IteratorStatus.StatusState();
129             status = new IteratorStatus(statusState);
130         }
131
132         OgnlValueStack stack = getStack();
133
134         if (value == null) {
135             value = "top";
136         }
137
138         iterator = MakeIterator.convert(findValue(value));
139
140         // get the first
141
if ((iterator != null) && iterator.hasNext()) {
142             Object JavaDoc currentValue = iterator.next();
143             stack.push(currentValue);
144
145             String JavaDoc id = getId();
146
147             if ((id != null) && (currentValue != null)) {
148                 pageContext.setAttribute(id, currentValue);
149                 pageContext.setAttribute(id, currentValue, PageContext.REQUEST_SCOPE);
150             }
151
152             // Status object
153
if (statusAttr != null) {
154                 statusState.setLast(!iterator.hasNext());
155                 oldStatus = stack.getContext().get(statusAttr);
156                 stack.getContext().put(statusAttr, status);
157             }
158
159             return EVAL_BODY_AGAIN;
160         } else {
161             return SKIP_BODY;
162         }
163     }
164 }
165
Popular Tags