KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > tag > common > xml > 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.common.xml;
18
19 import java.util.List JavaDoc;
20
21 import javax.servlet.jsp.JspTagException JavaDoc;
22 import javax.servlet.jsp.jstl.core.LoopTagSupport;
23
24 import org.apache.taglibs.standard.resources.Resources;
25
26 /**
27  * <p>Support for the XML library's &lt;forEach&gt; tag.</p>
28  *
29  * @see javax.servlet.jsp.jstl.core.LoopTagSupport
30  * @author Shawn Bayern
31  */

32 public class ForEachTag extends LoopTagSupport {
33
34     //*********************************************************************
35
// Private state
36

37     private String JavaDoc select; // tag attribute
38
private List JavaDoc nodes; // XPath result
39
private int nodesIndex; // current index
40
private org.w3c.dom.Node JavaDoc current; // current node
41

42     //*********************************************************************
43
// Iteration control methods
44

45     // (We inherit semantics and Javadoc from LoopTagSupport.)
46

47     protected void prepare() throws JspTagException JavaDoc {
48         nodesIndex = 0;
49         XPathUtil xu = new XPathUtil(pageContext);
50         nodes = xu.selectNodes(XPathUtil.getContext(this), select);
51     }
52
53     protected boolean hasNext() throws JspTagException JavaDoc {
54         return (nodesIndex < nodes.size());
55     }
56
57     protected Object JavaDoc next() throws JspTagException JavaDoc {
58     Object JavaDoc o = nodes.get(nodesIndex++);
59     if (!(o instanceof org.w3c.dom.Node JavaDoc))
60         throw new JspTagException JavaDoc(
61         Resources.getMessage("FOREACH_NOT_NODESET"));
62     current = (org.w3c.dom.Node JavaDoc) o;
63         return current;
64     }
65
66
67     //*********************************************************************
68
// Tag logic and lifecycle management
69

70     // Releases any resources we may have (or inherit)
71
public void release() {
72     init();
73         super.release();
74     }
75
76
77     //*********************************************************************
78
// Attribute accessors
79

80     public void setSelect(String JavaDoc select) {
81     this.select = select;
82     }
83
84     public void setBegin(int begin) throws JspTagException JavaDoc {
85         this.beginSpecified = true;
86         this.begin = begin;
87         validateBegin();
88     }
89
90     public void setEnd(int end) throws JspTagException JavaDoc {
91         this.endSpecified = true;
92         this.end = end;
93         validateEnd();
94     }
95
96     public void setStep(int step) throws JspTagException JavaDoc {
97         this.stepSpecified = true;
98         this.step = step;
99         validateStep();
100     }
101     
102     //*********************************************************************
103
// Public methods for subtags
104

105     /* Retrieves the current context. */
106     public org.w3c.dom.Node JavaDoc getContext() throws JspTagException JavaDoc {
107     // expose the current node as the context
108
return current;
109     }
110
111
112     //*********************************************************************
113
// Private utility methods
114

115     private void init() {
116     select = null;
117     nodes = null;
118     nodesIndex = 0;
119     current = null;
120     }
121 }
122
123
Popular Tags