KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > page > AttributesTag


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.page;
18
19 import java.util.*;
20 import javax.servlet.*;
21 import javax.servlet.http.*;
22 import javax.servlet.jsp.*;
23 import javax.servlet.jsp.tagext.*;
24
25 /**
26  * JSP Tag <b>attributes</b>, used to get PageContext attribute
27  * information using the standard JSP 1.2 &lt;jsp:getProperty&gt; tag.
28  * <p>
29  * The script variable of name <b>id</b> is availble only within the
30  * body of the <b>attributes</b> tag.
31  * <p>
32  * Loops through all the attributes in the PageContext.
33  * <p>
34  * JSP Tag Lib Descriptor
35  * <p><pre>
36  * &lt;name&gt;attributes&lt;/name&gt;
37  * &lt;tagclass&gt;org.apache.taglibs.application.AttributesTag&lt;/tagclass&gt;
38  * &lt;teiclass&gt;org.apache.taglibs.application.AttributesTEI&lt;/teiclass&gt;
39  * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
40  * &lt;info&gt;Loop through all attributes or get a single attribute.&lt;/info&gt;
41  * &lt;attribute&gt;
42  * &lt;name&gt;id&lt;/name&gt;
43  * &lt;required&gt;true&lt;/required&gt;
44  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
45  * &lt;/attribute&gt;
46  * </pre>
47  *
48  * @author Morgan Delagrange
49  */

50
51 public class AttributesTag extends BodyTagSupport
52 {
53     private String JavaDoc name = null;
54     private Enumeration attributes = null;
55     private String JavaDoc attribute = null;
56
57     /**
58      * Loops through all the attributes that came with the PageContext.
59      *
60      * @return SKIP_BODY if no attributes are found, EVAL_BODY_TAG if an attribute exists
61      */

62     public final int doStartTag() throws JspException
63     {
64
65     attributes = pageContext.getAttributeNamesInScope(PageContext.PAGE_SCOPE);
66     if( attributes == null || !attributes.hasMoreElements() )
67         return SKIP_BODY;
68     attribute = (String JavaDoc)attributes.nextElement();
69     if( attribute == null )
70         return SKIP_BODY;
71
72     pageContext.setAttribute(id,this);
73     return EVAL_BODY_TAG;
74     }
75
76     /**
77      * Method called at end of each attributes tag.
78      *
79      * @return EVAL_BODY_TAG if there is another attribute, or SKIP_BODY if there are no more attributes
80      */

81     public final int doAfterBody() throws JspException
82     {
83     // See if this is the last attribute
84
if( !attributes.hasMoreElements() )
85         return SKIP_BODY;
86     // There is another attribute, so loop again
87
attribute = (String JavaDoc)attributes.nextElement();
88     if( attribute == null )
89         return SKIP_BODY;
90     return EVAL_BODY_TAG;
91     }
92
93     /**
94      * Method called at end of Tag
95      * @return EVAL_PAGE
96      */

97     public final int doEndTag() throws JspException
98     {
99         pageContext.removeAttribute(id,PageContext.PAGE_SCOPE);
100     try
101     {
102         if(bodyContent != null) {
103             bodyContent.writeOut(bodyContent.getEnclosingWriter());
104             }
105     } catch(java.io.IOException JavaDoc e)
106     {
107         throw new JspException("IO Error: " + e.getMessage());
108     }
109     return EVAL_PAGE;
110     }
111
112     /**
113      * Returns the name of the attribute.
114      * <p>
115      * &lt;jsp:getProperty name=<i>"id"</i> property="name"/&gt;
116      *
117      * @return String - attribute name
118      */

119     public final String JavaDoc getName()
120     {
121     return attribute;
122     }
123
124     /**
125      * Returns the value of the attribute.
126      * <p>
127      * &lt;jsp:getProperty name=<i>"id"</i> property="value"/&gt;
128      *
129      * @return String - value of the attribute
130      */

131     public final String JavaDoc getValue()
132     {
133     Object JavaDoc value = pageContext.getAttribute(attribute);
134     if( value == null )
135         return "";
136     return "" + value.toString();
137     }
138
139 }
140
Popular Tags