KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > application > 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.application;
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 ServletContext 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 ServletContext.
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 ServletContext app = null;
55     private Enumeration attributes = null;
56     private String JavaDoc attribute = null;
57
58     /**
59      * Loops through all the attributes that came within the ServletContext.
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 ServletContext
66
app = pageContext.getServletContext();
67         attribute = null;
68
69         if( name != null ) {
70             if( app.getAttribute(name) != null) {
71                 attribute = name;
72             }
73         } else {
74             attributes = app.getAttributeNames();
75             if( attributes == null || !attributes.hasMoreElements() )
76                 return SKIP_BODY;
77             attribute = (String JavaDoc)attributes.nextElement();
78         }
79     if( attribute == null )
80         return SKIP_BODY;
81
82     pageContext.setAttribute(id,this,PageContext.PAGE_SCOPE);
83     return EVAL_BODY_TAG;
84     }
85
86     /**
87      * Method called at end of each attributes tag.
88      *
89      * @return EVAL_BODY_TAG if there is another attribute, or SKIP_BODY if there are no more attributes
90      */

91     public final int doAfterBody() throws JspException
92     {
93     // See if this is the last attribute
94
if( name != null || !attributes.hasMoreElements() )
95         return SKIP_BODY;
96     // There is another attribute, so loop again
97
attribute = (String JavaDoc)attributes.nextElement();
98     if( attribute == null )
99         return SKIP_BODY;
100     return EVAL_BODY_TAG;
101     }
102
103     /**
104      * Method called at end of Tag
105      * @return EVAL_PAGE
106      */

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

128     public final String JavaDoc getName()
129     {
130     return attribute;
131     }
132
133     /**
134      * Set the name of the attribute to get
135      *
136      * @param String - attribute name
137      */

138     public final void setName(String JavaDoc name)
139     {
140         this.name = name;
141     }
142
143     /**
144      * Returns the value of the attribute.
145      * <p>
146      * &lt;jsp:getProperty name=<i>"id"</i> property="value"/&gt;
147      *
148      * @return String - value of the attribute
149      */

150     public final String JavaDoc getValue()
151     {
152     Object JavaDoc value = app.getAttribute(attribute);
153     if( value == null )
154         return "";
155     return "" + value.toString();
156     }
157
158 }
159
Popular Tags