Coverage details for org.chwf.taglib.jutil.ForEachTag

LineHitsSource
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>&lt;forEach&gt;</code> tag.</p>
52  *
53  * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a>
54  */
5527public 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. */
6713  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() {
781    super.cleanup();
791    this.var = null;
801    this.collection = null;
811    this.count = 0;
821    this.el = null;
831    setIterator(null);
841  }
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) {
928    this.var = var;
938  }
94  
95   /**
96    * The var name holding each object for iteration.
97    *
98    * @return The variable name.
99    */
100   public String getVar() {
10114    return this.var;
102   }
103  
104   /**
105    * The collection name.
106    *
107    * @param collection The collection name.
108    */
109   public void setCollection(String collection) {
1105    this.collection = collection;
1115  }
112  
113   /**
114    * The collection name.
115    *
116    * @return The collection name.
117    */
118   public String getCollection() {
1197    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) {
1283    this.el = el;
1293  }
130  
131   /**
132    * The JSTL Expression Language statement.
133    *
134    * @return The JSTL Expression Language statement.
135    */
136   public String getEl() {
1377    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 {
1475    initIterator();
1485    if (hasNext()) {
1494      return ServletUtils.EVAL_BODY_TAG;
150     } else {
1511      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:
1648      if (hasNext()) {
1654        return ServletUtils.EVAL_BODY_TAG;
166       } else {
167         // If not, remove object and write body content:
1684        pageContext.removeAttribute(getVar());
1694        getPreviousOut().print(bodyContent.getString());
170       }
1714    } catch (IOException ex) {
1720      throw new TagException(ex);
173     }
1744    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() {
18313    if (getIterator().hasNext()) {
1848      Object object = getIterator().next();
1858      ServletUtils.setPageAttribute(pageContext, getVar(), object);
1868      ServletUtils.setPageAttribute(
187         pageContext,
188         ObjectTagSupport.DEFAULT_OBJECT,
189         object);
1908      this.count++;
1918      ServletUtils.setPageAttribute(
192         pageContext,
193         "counter",
194         new Integer(count));
1958      return true; // More objects, continue
196     } else {
1975      pageContext.removeAttribute("counter");
1985      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 {
2085    Object data = getTagResult();
209  
2105    if (data == null) {
211       // If no collection found, use empty list:
2121      setIterator(Collections.EMPTY_LIST.iterator());
2134    } else if (data instanceof Collection) {
2142      setIterator(((Collection) data).iterator());
2152    } else if (data instanceof Map) {
2161      setIterator(((Map) data).values().iterator());
2171    } else if (data instanceof Object[]) {
2181      setIterator(Arrays.asList((Object[]) data).iterator());
219     } else {
2200      throw new TagException(
221         JutilMessages.MESSAGE_NOT_A_COLLECTION,
222         new Object[] { collection });
223     }
2245  }
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 {
2335    String el = getEl();
2345    String collection = getCollection();
2355    if (el != null) {
2361      return ExpressionEvaluatorManager.evaluate(
237         "el",
238         el,
239         Object.class,
240         this,
241         pageContext);
2424    } else if (collection != null) {
2434      return pageContext.findAttribute(collection);
244     } else {
2450      return null;
246     }
247   }
248  
249   /**
250    * Self-encapsulate iterator.
251    *
252    * @param iterator the iterator.
253    */
254   private void setIterator(Iterator iterator) {
2556    this.iterator = iterator;
2566  }
257  
258   /**
259    * Self-encapsulate iterator.
260    *
261    * @return the iterator.
262    */
263   private Iterator getIterator() {
26421    return this.iterator;
265   }
266 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.