KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > request > 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.request;
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 HttpServletRequest attribute
27  * information using the standard JSP &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 HttpServletRequest.
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.request.AttributesTag&lt;/tagclass&gt;
38  * &lt;teiclass&gt;org.apache.taglibs.request.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 Glenn Nielsen
49  */

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

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

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

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

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

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