1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts.taglib.logic;
19
20 import org.apache.struts.taglib.TagUtils;
21
22 import javax.servlet.jsp.JspException;
23
24 import java.lang.reflect.Array;
25
26 import java.util.Collection;
27 import java.util.Map;
28
29 /***
30 * Evalute the nested body content of this tag if the specified value is empty
31 * for this request.
32 *
33 * @version $Rev: 376842 $ $Date: 2004-10-16 12:38:42 -0400 (Sat, 16 Oct 2004)
34 * $
35 * @since Struts 1.1
36 */
37 public class EmptyTag extends ConditionalTagBase {
38
39
40 /***
41 * Evaluate the condition that is being tested by this particular tag, and
42 * return <code>true</code> if the nested body content of this tag should
43 * be evaluated, or <code>false</code> if it should be skipped. This
44 * method must be implemented by concrete subclasses.
45 *
46 * @throws JspException if a JSP exception occurs
47 */
48 protected boolean condition()
49 throws JspException {
50 return (condition(true));
51 }
52
53 /***
54 * Evaluate the condition that is being tested by this particular tag, and
55 * return <code>true</code> if the nested body content of this tag should
56 * be evaluated, or <code>false</code> if it should be skipped. This
57 * method must be implemented by concrete subclasses.
58 *
59 * @param desired Desired outcome for a true result
60 * @throws JspException if a JSP exception occurs
61 */
62 protected boolean condition(boolean desired)
63 throws JspException {
64 if (this.name == null) {
65 JspException e =
66 new JspException(messages.getMessage("empty.noNameAttribute"));
67
68 TagUtils.getInstance().saveException(pageContext, e);
69 throw e;
70 }
71
72 Object value = null;
73
74 if (this.property == null) {
75 value = TagUtils.getInstance().lookup(pageContext, name, scope);
76 } else {
77 value =
78 TagUtils.getInstance().lookup(pageContext, name, property, scope);
79 }
80
81 boolean empty = true;
82
83 if (value == null) {
84 empty = true;
85 } else if (value instanceof String) {
86 String strValue = (String) value;
87
88 empty = (strValue.length() < 1);
89 } else if (value instanceof Collection) {
90 Collection collValue = (Collection) value;
91
92 empty = collValue.isEmpty();
93 } else if (value instanceof Map) {
94 Map mapValue = (Map) value;
95
96 empty = mapValue.isEmpty();
97 } else if (value.getClass().isArray()) {
98 empty = Array.getLength(value) == 0;
99 } else {
100 empty = false;
101 }
102
103 return (empty == desired);
104 }
105 }