1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts.taglib.bean;
22
23 import org.apache.struts.taglib.TagUtils;
24 import org.apache.struts.util.MessageResources;
25
26 import javax.servlet.jsp.JspException;
27 import javax.servlet.jsp.PageContext;
28 import javax.servlet.jsp.tagext.TagSupport;
29
30 import java.lang.reflect.Array;
31
32 import java.util.Collection;
33 import java.util.Map;
34
35
36
37
38
39
40
41
42 public class SizeTag extends TagSupport {
43
44
45
46 protected static MessageResources messages =
47 MessageResources.getMessageResources(
48 "org.apache.struts.taglib.bean.LocalStrings");
49
50
51
52
53
54
55 protected Object collection = null;
56
57
58
59
60
61 protected String id = null;
62
63
64
65
66 protected String name = null;
67
68
69
70
71 protected String property = null;
72
73
74
75
76 protected String scope = null;
77
78 public Object getCollection() {
79 return (this.collection);
80 }
81
82 public void setCollection(Object collection) {
83 this.collection = collection;
84 }
85
86 public String getId() {
87 return (this.id);
88 }
89
90 public void setId(String id) {
91 this.id = id;
92 }
93
94 public String getName() {
95 return (this.name);
96 }
97
98 public void setName(String name) {
99 this.name = name;
100 }
101
102 public String getProperty() {
103 return (this.property);
104 }
105
106 public void setProperty(String property) {
107 this.property = property;
108 }
109
110 public String getScope() {
111 return (this.scope);
112 }
113
114 public void setScope(String scope) {
115 this.scope = scope;
116 }
117
118
119
120
121
122
123
124
125 public int doStartTag() throws JspException {
126
127 Object value = this.collection;
128
129 if (value == null) {
130 if (name == null) {
131
132
133 JspException e =
134 new JspException(messages.getMessage(
135 "size.noCollectionOrName"));
136
137 TagUtils.getInstance().saveException(pageContext, e);
138 throw e;
139 }
140
141 value =
142 TagUtils.getInstance().lookup(pageContext, name, property, scope);
143 }
144
145
146 int size = 0;
147
148 if (value == null) {
149 JspException e =
150 new JspException(messages.getMessage("size.collection"));
151
152 TagUtils.getInstance().saveException(pageContext, e);
153 throw e;
154 } else if (value.getClass().isArray()) {
155 size = Array.getLength(value);
156 } else if (value instanceof Collection) {
157 size = ((Collection) value).size();
158 } else if (value instanceof Map) {
159 size = ((Map) value).size();
160 } else {
161 JspException e =
162 new JspException(messages.getMessage("size.collection"));
163
164 TagUtils.getInstance().saveException(pageContext, e);
165 throw e;
166 }
167
168
169 pageContext.setAttribute(this.id, new Integer(size),
170 PageContext.PAGE_SCOPE);
171
172 return (SKIP_BODY);
173 }
174
175
176
177
178 public void release() {
179 super.release();
180 collection = null;
181 id = null;
182 name = null;
183 property = null;
184 scope = null;
185 }
186 }