KickJava   Java API By Example, From Geeks To Geeks.

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


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>equalsAttribute</b>, used to determine if a ServletContext
27  * attribute equals the value of the "match" tag attribute.
28  * <p>
29  * Includes the body of the tag if the attribute equals the value of the
30  * "match" tag attribute.
31  * <p>
32  * You can set the optional tag attribute <b>value</b> to <i>true</i> or
33  * <i>false</i>. The body of the tag is included if equalsAttribute matches
34  * the value.
35  * <p>
36  * You can set the optional tag attribute <b>ignoreCase</b> to <i>true</i> or
37  * <i>false</i>. If ignoreCase is set to true, then the comparison between the
38  * application attribute and the "match" tag attribute will <i>not</i> be
39  * case-sensitive.
40  * <p>
41  * JSP Tag Lib Descriptor
42  * <p><pre>
43  * &lt;name>equalsAttribute&lt;/name>
44  * &lt;tagclass>org.apache.taglibs.application.EqualsAttributeTag&lt;/tagclass>
45  * &lt;bodycontent>JSP&lt;/bodycontent>
46  * &lt;info>Includes the body of the tag if the application attribute equals the
47  * value of the "match" tag attribute.&lt;/info>
48  * &lt;attribute>
49  * &lt;name>name&lt;/name>
50  * &lt;required>true&lt;/required>
51  * &lt;rtexprvalue>false&lt;/rtexprvalue>
52  * &lt;/attribute>
53  * &lt;attribute>
54  * &lt;name>value&lt;/name>
55  * &lt;required>false&lt;/required>
56  * &lt;rtexprvalue>false&lt;/rtexprvalue>
57  * &lt;/attribute>
58  * &lt;attribute>
59  * &lt;name>match&lt;/name>
60  * &lt;required>true&lt;/required>
61  * &lt;rtexprvalue>true&lt;/rtexprvalue>
62  * &lt;/attribute>
63  * &lt;attribute>
64  * &lt;name>ignoreCase&lt;/name>
65  * &lt;required>false&lt;/required>
66  * &lt;rtexprvalue>false&lt;/rtexprvalue>
67  * &lt;/attribute>
68  * </pre>
69  *
70  * @author Morgan Delagrange
71  */

72
73 public class EqualsAttributeTag extends TagSupport
74 {
75     private String JavaDoc name = null;
76     private String JavaDoc match = null;
77     //default behaviour is don't ignore case and execute body when tag contents
78
//equal value set by setMatch
79
private boolean ignoreCase = false;
80     private boolean value = true;
81
82     /**
83      * Includes the body of the tag if the application attribute equals the value set in the
84      * 'match' attribute.
85      *
86      * @return SKIP_BODY if equalsAttribute body content does not equal the value of
87      * the match attribute, EVAL_BODY_include if it does
88      */

89     public final int doStartTag() throws JspException
90     {
91         //result is whether or not tag contents equal the match attribute
92
boolean result = false;
93         Object JavaDoc attribute = pageContext.getServletContext().getAttribute(name);
94       
95         if (attribute == null) {
96             result = false;
97         } else {
98             String JavaDoc attributeValue = attribute.toString();
99         
100             if (ignoreCase) {
101                 result = attributeValue.equalsIgnoreCase(match);
102             } else {
103                 result = attributeValue.equals(match);
104             }
105         }
106       
107         if( value == result )
108             return EVAL_BODY_INCLUDE;
109
110         return SKIP_BODY;
111
112     }
113
114     /**
115      * Set the required tag attribute <b>name</b>.
116      *
117      * @param String name of application attribute
118      */

119     public final void setName(String JavaDoc str)
120     {
121     name = str;
122     }
123
124     /**
125      * Set the String that will be compared to the application
126      * attribute.
127      *
128      * @param String value to match against the application attribute
129      */

130     public final void setMatch(String JavaDoc str)
131     {
132         match = str;
133     }
134     
135     /**
136      * If ignoreCase is set to true, then the comparison
137      * between the "match" attribute and the
138      * application attribute will <i>not</i> be case sensitive
139      *
140      * @param boolean true = ignore case<BR>false = case sensitive<BR>
141      * default value = false
142      */

143     public final void setIgnoreCase(boolean value)
144     {
145        this.ignoreCase = value;
146     }
147     
148     /**
149      * Set the optional tag attribute <b>value</b> to true or false.
150      *
151      * @param boolean true or false
152      */

153     public final void setValue(boolean value)
154     {
155     this.value = value;
156     }
157
158 }
159
Popular Tags