Line | Hits | Source |
---|---|---|
1 | /* | |
2 | Chrysalis Web Framework [http://chrysalis.sourceforge.net] | |
3 | Copyright (c) 2002, 2003, 2004, Paul Strack | |
4 | ||
5 | All rights reserved. | |
6 | ||
7 | Redistribution and use in source and binary forms, with or without | |
8 | modification, are permitted provided that the following conditions are met: | |
9 | ||
10 | 1. Redistributions of source code must retain the above copyright notice, this | |
11 | list of conditions and the following disclaimer. | |
12 | ||
13 | 2. Redistributions in binary form must reproduce the above copyright notice, | |
14 | this list of conditions and the following disclaimer in the documentation | |
15 | and/or other materials provided with the distribution. | |
16 | ||
17 | 3. Neither the name of the copyright holder nor the names of its contributors | |
18 | may be used to endorse or promote products derived from this software without | |
19 | specific prior written permission. | |
20 | ||
21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
22 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
23 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
24 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | |
25 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
26 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
27 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | |
28 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
29 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
30 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
31 | */ | |
32 | ||
33 | package org.chwf.taglib.jutil; | |
34 | ||
35 | import java.io.IOException; | |
36 | import java.util.Arrays; | |
37 | import java.util.Collection; | |
38 | import java.util.Collections; | |
39 | import java.util.Iterator; | |
40 | import java.util.Map; | |
41 | ||
42 | import javax.servlet.jsp.JspException; | |
43 | ||
44 | import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager; | |
45 | import org.chwf.servlet.ServletUtils; | |
46 | import org.chwf.taglib.base.LifeCycleBodyTagSupport; | |
47 | import org.chwf.taglib.base.ObjectTagSupport; | |
48 | import org.chwf.taglib.base.TagException; | |
49 | ||
50 | /** | |
51 | * <p>Tag handler for the <code><forEach></code> tag.</p> | |
52 | * | |
53 | * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a> | |
54 | */ | |
55 | 27 | public class ForEachTag extends LifeCycleBodyTagSupport { |
56 | ||
57 | /** The looping variable. */ | |
58 | private String var; | |
59 | ||
60 | /** The collection name. */ | |
61 | private String collection; | |
62 | ||
63 | /** The iterator for looping. */ | |
64 | private Iterator iterator; | |
65 | ||
66 | /** A counter. */ | |
67 | 13 | private int count = 0; |
68 | ||
69 | /** The object. */ | |
70 | private String el; | |
71 | ||
72 | /** | |
73 | * Clean up data for tag handler reuse and pooling. Should be overridden in | |
74 | * tag handler. Subclasses should always invoke their superclass's cleanup | |
75 | * method: <code>super.cleanup()</code> | |
76 | */ | |
77 | public void cleanup() { | |
78 | 1 | super.cleanup(); |
79 | 1 | this.var = null; |
80 | 1 | this.collection = null; |
81 | 1 | this.count = 0; |
82 | 1 | this.el = null; |
83 | 1 | setIterator(null); |
84 | 1 | } |
85 | ||
86 | /** | |
87 | * The var name holding each object for iteration. | |
88 | * | |
89 | * @param var The variable name. | |
90 | */ | |
91 | public void setVar(String var) { | |
92 | 8 | this.var = var; |
93 | 8 | } |
94 | ||
95 | /** | |
96 | * The var name holding each object for iteration. | |
97 | * | |
98 | * @return The variable name. | |
99 | */ | |
100 | public String getVar() { | |
101 | 14 | return this.var; |
102 | } | |
103 | ||
104 | /** | |
105 | * The collection name. | |
106 | * | |
107 | * @param collection The collection name. | |
108 | */ | |
109 | public void setCollection(String collection) { | |
110 | 5 | this.collection = collection; |
111 | 5 | } |
112 | ||
113 | /** | |
114 | * The collection name. | |
115 | * | |
116 | * @return The collection name. | |
117 | */ | |
118 | public String getCollection() { | |
119 | 7 | return this.collection; |
120 | } | |
121 | ||
122 | /** | |
123 | * The JSTL Expression Language statement. | |
124 | * | |
125 | * @param el The JSTL Expression Language statement. | |
126 | */ | |
127 | public void setEl(String el) { | |
128 | 3 | this.el = el; |
129 | 3 | } |
130 | ||
131 | /** | |
132 | * The JSTL Expression Language statement. | |
133 | * | |
134 | * @return The JSTL Expression Language statement. | |
135 | */ | |
136 | public String getEl() { | |
137 | 7 | return this.el; |
138 | } | |
139 | ||
140 | /** | |
141 | * Skips body if the collection is empty. | |
142 | * | |
143 | * @return SKIP_BODY if the collection is empty. | |
144 | * @throws JspException If the iterator cannot be initialized. | |
145 | */ | |
146 | public int doStart() throws JspException { | |
147 | 5 | initIterator(); |
148 | 5 | if (hasNext()) { |
149 | 4 | return ServletUtils.EVAL_BODY_TAG; |
150 | } else { | |
151 | 1 | return SKIP_BODY; |
152 | } | |
153 | } | |
154 | ||
155 | /** | |
156 | * Continues iteration for all values in the collection. | |
157 | * | |
158 | * @return EVAL_BODY_TAG if there are more elements in the colleciton. | |
159 | * @throws JspException for IO exceptions. | |
160 | */ | |
161 | public int doAfterBody() throws JspException { | |
162 | try { | |
163 | // See whether the iterator has more elements: | |
164 | 8 | if (hasNext()) { |
165 | 4 | return ServletUtils.EVAL_BODY_TAG; |
166 | } else { | |
167 | // If not, remove object and write body content: | |
168 | 4 | pageContext.removeAttribute(getVar()); |
169 | 4 | getPreviousOut().print(bodyContent.getString()); |
170 | } | |
171 | 4 | } catch (IOException ex) { |
172 | 0 | throw new TagException(ex); |
173 | } | |
174 | 4 | return SKIP_BODY; |
175 | } | |
176 | ||
177 | /** | |
178 | * True if there are more values. Stores each object in the page context. | |
179 | * | |
180 | * @return True if there are more values. | |
181 | */ | |
182 | private boolean hasNext() { | |
183 | 13 | if (getIterator().hasNext()) { |
184 | 8 | Object object = getIterator().next(); |
185 | 8 | ServletUtils.setPageAttribute(pageContext, getVar(), object); |
186 | 8 | ServletUtils.setPageAttribute( |
187 | pageContext, | |
188 | ObjectTagSupport.DEFAULT_OBJECT, | |
189 | object); | |
190 | 8 | this.count++; |
191 | 8 | ServletUtils.setPageAttribute( |
192 | pageContext, | |
193 | "counter", | |
194 | new Integer(count)); | |
195 | 8 | return true; // More objects, continue |
196 | } else { | |
197 | 5 | pageContext.removeAttribute("counter"); |
198 | 5 | return false; // Done with iteration |
199 | } | |
200 | } | |
201 | ||
202 | /** | |
203 | * Initializes the iterator for the collection. map or array. | |
204 | * | |
205 | * @throws JspException If the iterator cannot be initialized. | |
206 | */ | |
207 | private void initIterator() throws JspException { | |
208 | 5 | Object data = getTagResult(); |
209 | ||
210 | 5 | if (data == null) { |
211 | // If no collection found, use empty list: | |
212 | 1 | setIterator(Collections.EMPTY_LIST.iterator()); |
213 | 4 | } else if (data instanceof Collection) { |
214 | 2 | setIterator(((Collection) data).iterator()); |
215 | 2 | } else if (data instanceof Map) { |
216 | 1 | setIterator(((Map) data).values().iterator()); |
217 | 1 | } else if (data instanceof Object[]) { |
218 | 1 | setIterator(Arrays.asList((Object[]) data).iterator()); |
219 | } else { | |
220 | 0 | throw new TagException( |
221 | JutilMessages.MESSAGE_NOT_A_COLLECTION, | |
222 | new Object[] { collection }); | |
223 | } | |
224 | 5 | } |
225 | ||
226 | /** | |
227 | * Get tag result, using either the collection attribute or el. | |
228 | * | |
229 | * @return The tag result. | |
230 | * @throws JspException For EL errors. | |
231 | */ | |
232 | private Object getTagResult() throws JspException { | |
233 | 5 | String el = getEl(); |
234 | 5 | String collection = getCollection(); |
235 | 5 | if (el != null) { |
236 | 1 | return ExpressionEvaluatorManager.evaluate( |
237 | "el", | |
238 | el, | |
239 | Object.class, | |
240 | this, | |
241 | pageContext); | |
242 | 4 | } else if (collection != null) { |
243 | 4 | return pageContext.findAttribute(collection); |
244 | } else { | |
245 | 0 | return null; |
246 | } | |
247 | } | |
248 | ||
249 | /** | |
250 | * Self-encapsulate iterator. | |
251 | * | |
252 | * @param iterator the iterator. | |
253 | */ | |
254 | private void setIterator(Iterator iterator) { | |
255 | 6 | this.iterator = iterator; |
256 | 6 | } |
257 | ||
258 | /** | |
259 | * Self-encapsulate iterator. | |
260 | * | |
261 | * @return the iterator. | |
262 | */ | |
263 | private Iterator getIterator() { | |
264 | 21 | return this.iterator; |
265 | } | |
266 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |