KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > tag > el > core > ForEachTag


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.standard.tag.el.core;
18
19 import java.util.ArrayList JavaDoc;
20
21 import javax.servlet.jsp.JspException JavaDoc;
22 import javax.servlet.jsp.jstl.core.LoopTag;
23 import javax.servlet.jsp.tagext.IterationTag JavaDoc;
24
25 import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
26 import org.apache.taglibs.standard.tag.common.core.ForEachSupport;
27 import org.apache.taglibs.standard.tag.common.core.NullAttributeException;
28
29 /**
30  * <p>A handler for &lt;forEach&gt; that accepts attributes as Strings
31  * and evaluates them as expressions at runtime.</p>
32  *
33  * @author Shawn Bayern
34  */

35
36 public class ForEachTag
37     extends ForEachSupport
38     implements LoopTag, IterationTag JavaDoc
39 {
40
41     //*********************************************************************
42
// 'Private' state (implementation details)
43

44     private String JavaDoc begin_; // stores EL-based property
45
private String JavaDoc end_; // stores EL-based property
46
private String JavaDoc step_; // stores EL-based property
47
private String JavaDoc items_; // stores EL-based property
48

49
50     //*********************************************************************
51
// Constructor
52

53     public ForEachTag() {
54         super();
55         init();
56     }
57
58
59     //*********************************************************************
60
// Tag logic
61

62     /* Begins iterating by processing the first item. */
63     public int doStartTag() throws JspException JavaDoc {
64
65         // evaluate any expressions we were passed, once per invocation
66
evaluateExpressions();
67
68     // chain to the parent implementation
69
return super.doStartTag();
70     }
71
72
73     // Releases any resources we may have (or inherit)
74
public void release() {
75         super.release();
76         init();
77     }
78
79
80     //*********************************************************************
81
// Accessor methods
82

83     // for EL-based attribute
84
public void setBegin(String JavaDoc begin_) {
85         this.begin_ = begin_;
86         this.beginSpecified = true;
87     }
88
89     // for EL-based attribute
90
public void setEnd(String JavaDoc end_) {
91         this.end_ = end_;
92         this.endSpecified = true;
93     }
94
95     // for EL-based attribute
96
public void setStep(String JavaDoc step_) {
97         this.step_ = step_;
98         this.stepSpecified = true;
99     }
100
101     public void setItems(String JavaDoc items_) {
102         this.items_ = items_;
103     }
104
105     //*********************************************************************
106
// Private (utility) methods
107

108     // (re)initializes state (during release() or construction)
109
private void init() {
110         // defaults for interface with page author
111
begin_ = null; // (no expression)
112
end_ = null; // (no expression)
113
step_ = null; // (no expression)
114
items_ = null; // (no expression)
115
}
116
117     /* Evaluates expressions as necessary */
118     private void evaluateExpressions() throws JspException JavaDoc {
119         /*
120          * Note: we don't check for type mismatches here; we assume
121          * the expression evaluator will return the expected type
122          * (by virtue of knowledge we give it about what that type is).
123          * A ClassCastException here is truly unexpected, so we let it
124          * propagate up.
125          */

126
127         if (begin_ != null) {
128             Object JavaDoc r = ExpressionEvaluatorManager.evaluate(
129                 "begin", begin_, Integer JavaDoc.class, this, pageContext);
130         if (r == null)
131         throw new NullAttributeException("forEach", "begin");
132             begin = ((Integer JavaDoc) r).intValue();
133             validateBegin();
134         }
135
136         if (end_ != null) {
137             Object JavaDoc r = ExpressionEvaluatorManager.evaluate(
138                 "end", end_, Integer JavaDoc.class, this, pageContext);
139         if (r == null)
140         throw new NullAttributeException("forEach", "end");
141             end = ((Integer JavaDoc) r).intValue();
142             validateEnd();
143         }
144
145         if (step_ != null) {
146             Object JavaDoc r = ExpressionEvaluatorManager.evaluate(
147                 "step", step_, Integer JavaDoc.class, this, pageContext);
148         if (r == null)
149         throw new NullAttributeException("forEach", "step");
150             step = ((Integer JavaDoc) r).intValue();
151             validateStep();
152         }
153
154     if (items_ != null) {
155             rawItems = ExpressionEvaluatorManager.evaluate(
156                 "items", items_, Object JavaDoc.class, this, pageContext);
157         // use an empty list to indicate "no iteration", if relevant
158
if (rawItems == null)
159         rawItems = new ArrayList JavaDoc();
160         }
161     }
162 }
163
Popular Tags