KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.servlet.jsp.JspException JavaDoc;
20 import javax.servlet.jsp.jstl.core.LoopTag;
21 import javax.servlet.jsp.tagext.IterationTag JavaDoc;
22
23 import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
24 import org.apache.taglibs.standard.tag.common.core.ForTokensSupport;
25 import org.apache.taglibs.standard.tag.common.core.NullAttributeException;
26
27 /**
28  * <p>A handler for &lt;forTokens&gt; that accepts attributes as Strings
29  * and evaluates them as expressions at runtime.
30  *
31  * @author Shawn Bayern
32  */

33 public class ForTokensTag
34     extends ForTokensSupport
35     implements LoopTag, IterationTag JavaDoc
36 {
37
38     //*********************************************************************
39
// 'Private' state (implementation details)
40

41     private String JavaDoc begin_; // raw EL-based property
42
private String JavaDoc end_; // raw EL-based property
43
private String JavaDoc step_; // raw EL-based property
44
private String JavaDoc items_; // raw EL-based property
45
private String JavaDoc delims_; // raw EL-based property
46

47
48     //*********************************************************************
49
// Constructor
50

51     public ForTokensTag() {
52         super();
53         init();
54     }
55
56
57     //*********************************************************************
58
// Tag logic
59

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

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

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

132
133        if (begin_ != null) {
134             Object JavaDoc r = ExpressionEvaluatorManager.evaluate(
135                 "begin", begin_, Integer JavaDoc.class, this, pageContext);
136             if (r == null)
137                 throw new NullAttributeException("forTokens", "begin");
138             begin = ((Integer JavaDoc) r).intValue();
139             validateBegin();
140         }
141
142         if (end_ != null) {
143             Object JavaDoc r = ExpressionEvaluatorManager.evaluate(
144                 "end", end_, Integer JavaDoc.class, this, pageContext);
145             if (r == null)
146                 throw new NullAttributeException("forTokens", "end");
147             end = ((Integer JavaDoc) r).intValue();
148             validateEnd();
149         }
150
151         if (step_ != null) {
152             Object JavaDoc r = ExpressionEvaluatorManager.evaluate(
153                 "step", step_, Integer JavaDoc.class, this, pageContext);
154             if (r == null)
155                 throw new NullAttributeException("forTokens", "step");
156             step = ((Integer JavaDoc) r).intValue();
157             validateStep();
158         }
159
160         if (items_ != null) {
161             items = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
162                 "items", items_, String JavaDoc.class, this, pageContext);
163         // use the empty string to indicate "no iteration"
164
if (items == null)
165         items = "";
166     }
167
168         if (delims_ != null) {
169             delims = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
170                 "delims", delims_, String JavaDoc.class, this, pageContext);
171         // use the empty string to cause monolithic tokenization
172
if (delims == null)
173         delims = "";
174     }
175     }
176 }
177
Popular Tags