KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > application > InitParametersTag


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>initParameters</b>, used to get init parameters
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 available only within the
30  * body of the <b>initParameters</b> tag.
31  * <p>
32  * Loops through all the init parameters in the ServletContext.
33  * <p>
34  * JSP Tag Lib Descriptor
35  * <p><pre>
36  * &lt;name&gt;initParameters&lt;/name&gt;
37  * &lt;tagclass&gt;org.apache.taglibs.application.InitParametersTag&lt;/tagclass&gt;
38  * &lt;teiclass&gt;org.apache.taglibs.application.InitParametersTEI&lt;/teiclass&gt;
39  * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
40  * &lt;info&gt;Loop through all initParameters or get a single initParameter.&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 InitParametersTag extends BodyTagSupport
52 {
53     private String JavaDoc name = null;
54     private ServletContext app = null;
55     private Enumeration parameters = null;
56     private String JavaDoc paramName = null;
57
58     /**
59      * Loops through all the init parameters in the current ServletContext.
60      *
61      * @return SKIP_BODY if no init parameters are found, EVAL_BODY_TAG if an init parameter exists
62      */

63     public final int doStartTag() throws JspException
64     {
65     // Get the ServletContext
66
app = pageContext.getServletContext();
67         paramName = null;
68
69         if( name != null ) {
70             if( app.getInitParameter(name) != null ) {
71                 paramName = name;
72             }
73         } else {
74             parameters = app.getInitParameterNames();
75             if( parameters != null && parameters.hasMoreElements() ) {
76                 paramName = (String JavaDoc)parameters.nextElement();
77             }
78         }
79     if( paramName == null )
80         return SKIP_BODY;
81
82     pageContext.setAttribute(id,this);
83     return EVAL_BODY_TAG;
84     }
85
86     /**
87      * Method called at end of each initParmaeters tag.
88      *
89      * @return EVAL_BODY_TAG if there is another init parameter, or SKIP_BODY if there are no more init parameters
90      */

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

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

129     public final String JavaDoc getName()
130     {
131     return paramName;
132     }
133
134     /**
135      * Set the name of the init parameter to get.
136      * <p>
137      * @param String - init parameter name
138      */

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

151     public final String JavaDoc getValue()
152     {
153       String JavaDoc paramValue = app.getInitParameter(paramName);
154       if (paramValue == null) {
155         return "";
156       }
157       
158       return paramValue;
159     }
160
161 }
162
Popular Tags