KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > taglib > jxpath > 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.cocoon.taglib.jxpath.core;
18
19 import org.apache.cocoon.taglib.IterationTag;
20 import org.apache.cocoon.taglib.core.ForEachSupport;
21 import org.apache.cocoon.taglib.core.LoopTag;
22
23 import org.xml.sax.Attributes JavaDoc;
24 import org.xml.sax.SAXException JavaDoc;
25
26 /**
27  * <p>A handler for &lt;forEach&gt; that accepts attributes as Strings
28  * and evaluates them as expressions at runtime.</p>
29  *
30  * Migration from JSTL1.0
31  * @see org.apache.taglibs.standard.tag.el.core.ForEachTag
32  *
33  * @author <a HREF="mailto:volker.schmitt@basf-it-services.com">Volker Schmitt</a>
34  * @version CVS $Id: ForEachTag.java 30932 2004-07-29 17:35:38Z vgritsenko $
35  */

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

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

46     //*********************************************************************
47
// Constructor
48

49     public ForEachTag() {
50         super();
51         init();
52     }
53
54     //*********************************************************************
55
// Tag logic
56

57     /* Begins iterating by processing the first item. */
58     public int doStartTag(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts) throws SAXException JavaDoc {
59
60         // evaluate any expressions we were passed, once per invocation
61
evaluateExpressions();
62
63         // chain to the parent implementation
64
return super.doStartTag(namespaceURI, localName, qName, atts);
65     }
66
67     // Releases any resources we may have (or inherit)
68
public void recycle() {
69         init();
70         super.recycle();
71    
72     }
73
74     //*********************************************************************
75
// Accessor methods
76

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

102     // (re)initializes state (during release() or construction)
103
private void init() {
104         // defaults for interface with page author
105
begin_ = null; // (no expression)
106
end_ = null; // (no expression)
107
step_ = null; // (no expression)
108
items_ = null; // (no expression)
109
}
110
111     /* Evaluates expressions as necessary */
112     private void evaluateExpressions() throws SAXException JavaDoc {
113
114         if (begin_ != null) {
115             begin = Integer.parseInt(begin_);
116         }
117         if (end_ != null) {
118             end = Integer.parseInt(end_);
119         }
120         if (step_ != null) {
121             step = Integer.parseInt(step_);
122         }
123         if (items_ != null) {
124             rawItems = getVariable(items_);
125         }
126
127         /*
128          * Note: we don't check for type mismatches here; we assume
129          * the expression evaluator will return the expected type
130          * (by virtue of knowledge we give it about what that type is).
131          * A ClassCastException here is truly unexpected, so we let it
132          * propagate up.
133          */

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

166     }
167 }
168
Popular Tags