KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > taglib > logic > EmptyTag


1 /*
2  * $Id: EmptyTag.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 1999-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.apache.struts.taglib.logic;
20
21 import java.util.Collection JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.lang.reflect.Array JavaDoc;
24
25 import javax.servlet.jsp.JspException JavaDoc;
26
27 import org.apache.struts.taglib.TagUtils;
28
29 /**
30  * Evalute the nested body content of this tag if the specified value
31  * is empty for this request.
32  *
33  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
34  * @since Struts 1.1
35  */

36 public class EmptyTag extends ConditionalTagBase {
37
38
39     // ------------------------------------------------------ Protected Methods
40

41
42     /**
43      * Evaluate the condition that is being tested by this particular tag,
44      * and return <code>true</code> if the nested body content of this tag
45      * should be evaluated, or <code>false</code> if it should be skipped.
46      * This method must be implemented by concrete subclasses.
47      *
48      * @exception JspException if a JSP exception occurs
49      */

50     protected boolean condition() throws JspException JavaDoc {
51
52         return (condition(true));
53
54     }
55
56
57     /**
58      * Evaluate the condition that is being tested by this particular tag,
59      * and return <code>true</code> if the nested body content of this tag
60      * should be evaluated, or <code>false</code> if it should be skipped.
61      * This method must be implemented by concrete subclasses.
62      *
63      * @param desired Desired outcome for a true result
64      *
65      * @exception JspException if a JSP exception occurs
66      */

67     protected boolean condition(boolean desired) throws JspException JavaDoc {
68         if (this.name == null) {
69             JspException JavaDoc e =
70                     new JspException JavaDoc(messages.getMessage("empty.noNameAttribute"));
71             TagUtils.getInstance().saveException(pageContext, e);
72             throw e;
73         }
74
75         Object JavaDoc value = null;
76         if (this.property == null) {
77             value = TagUtils.getInstance().lookup(pageContext, name, scope);
78         } else {
79             value = TagUtils.getInstance().lookup(pageContext, name, property, scope);
80         }
81
82         boolean empty = true;
83
84         if (value == null) {
85             empty = true;
86
87         } else if (value instanceof String JavaDoc) {
88             String JavaDoc strValue = (String JavaDoc) value;
89             empty = (strValue.length() < 1);
90
91         } else if (value instanceof Collection JavaDoc) {
92             Collection JavaDoc collValue = (Collection JavaDoc) value;
93             empty = collValue.isEmpty();
94
95         } else if (value instanceof Map JavaDoc) {
96             Map JavaDoc mapValue = (Map JavaDoc) value;
97             empty = mapValue.isEmpty();
98
99         } else if (value.getClass().isArray()) {
100             empty = Array.getLength(value) == 0;
101
102         } else {
103             empty = false;
104         }
105
106         return (empty == desired);
107     }
108
109 }
110
Popular Tags