2
* $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/bean/SizeTag.java,v 1.9 2004/03/14 06:23:45 sraeburn Exp $
4
* $Date: 2004/03/14 06:23:45 $
6
* Copyright 1999-2004 The Apache Software Foundation.
8
* Licensed under the Apache License, Version 2.0 (the "License");
9
* you may not use this file except in compliance with the License.
10
* You may obtain a copy of the License at
12
* http://www.apache.org/licenses/LICENSE-2.0
14
* Unless required by applicable law or agreed to in writing, software
15
* distributed under the License is distributed on an "AS IS" BASIS,
16
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
* See the License for the specific language governing permissions and
18
* limitations under the License.
22
package org.apache.struts.taglib.bean;
25
import java.lang.reflect.Array;
26
import java.util.Collection;
28
import javax.servlet.jsp.JspException;
29
import javax.servlet.jsp.PageContext;
30
import javax.servlet.jsp.tagext.TagSupport;
31
import org.apache.struts.util.MessageResources;
32
import org.apache.struts.taglib.TagUtils;
36
* Define a scripting variable that will contain the number of elements
37
* found in a specified array, Collection, or Map.
39
* @version $Revision: 1.9 $ $Date: 2004/03/14 06:23:45 $
42
public class SizeTag extends TagSupport {
45
// ------------------------------------------------------------- Properties
49
* The actual collection to be counted.
51
protected Object collection = null;
53
public Object getCollection() {
54
return (this.collection);
57
public void setCollection(Object collection) {
58
this.collection = collection;
63
* The name of the scripting variable that will be exposed as a page
66
protected String id = null;
68
public String getId() {
72
public void setId(String id) {
78
* The message resources for this package.
80
protected static MessageResources messages =
81
MessageResources.getMessageResources
82
("org.apache.struts.taglib.bean.LocalStrings");
87
* The name of the bean owning the property to be counted.
89
protected String name = null;
91
public String getName() {
95
public void setName(String name) {
101
* The name of the property to be retrieved.
103
protected String property = null;
105
public String getProperty() {
106
return (this.property);
109
public void setProperty(String property) {
110
this.property = property;
115
* The scope within which to search for the specified bean.
117
protected String scope = null;
119
public String getScope() {
123
public void setScope(String scope) {
128
// --------------------------------------------------------- Public Methods
132
* Retrieve the required property and expose it as a scripting variable.
134
* @exception JspException if a JSP exception has occurred
136
public int doStartTag() throws JspException {
138
// Retrieve the required property value
139
Object value = this.collection;
142
// Must specify either a collection attribute or a name
144
JspException e = new JspException
145
(messages.getMessage("size.noCollectionOrName"));
146
TagUtils.getInstance().saveException(pageContext, e);
150
value = TagUtils.getInstance().lookup(pageContext, name, property, scope);
153
// Identify the number of elements, based on the collection type
156
JspException e = new JspException
157
(messages.getMessage("size.collection"));
158
TagUtils.getInstance().saveException(pageContext, e);
160
} else if (value.getClass().isArray()) {
161
size = Array.getLength(value);
162
} else if (value instanceof Collection) {
163
size = ((Collection) value).size();
164
} else if (value instanceof Map) {
165
size = ((Map) value).size();
167
JspException e = new JspException
168
(messages.getMessage("size.collection"));
169
TagUtils.getInstance().saveException(pageContext, e);
173
// Expose this size as a scripting variable
174
pageContext.setAttribute(this.id, new Integer(size),
175
PageContext.PAGE_SCOPE);
182
* Release all allocated resources.
184
public void release() {