~ubuntu-branches/ubuntu/trusty/libstruts1.2-java/trusty-proposed

« back to all changes in this revision

Viewing changes to src/share/org/apache/struts/taglib/bean/SizeTag.java

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2004-11-19 15:35:25 UTC
  • Revision ID: james.westby@ubuntu.com-20041119153525-mdu08a76z4zo67xt
Tags: upstream-1.2.4
ImportĀ upstreamĀ versionĀ 1.2.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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 $
 
3
 * $Revision: 1.9 $
 
4
 * $Date: 2004/03/14 06:23:45 $
 
5
 *
 
6
 * Copyright 1999-2004 The Apache Software Foundation.
 
7
 * 
 
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
 
11
 * 
 
12
 *      http://www.apache.org/licenses/LICENSE-2.0
 
13
 * 
 
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.
 
19
 */
 
20
 
 
21
 
 
22
package org.apache.struts.taglib.bean;
 
23
 
 
24
 
 
25
import java.lang.reflect.Array;
 
26
import java.util.Collection;
 
27
import java.util.Map;
 
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;
 
33
 
 
34
 
 
35
/**
 
36
 * Define a scripting variable that will contain the number of elements
 
37
 * found in a specified array, Collection, or Map.
 
38
 *
 
39
 * @version $Revision: 1.9 $ $Date: 2004/03/14 06:23:45 $
 
40
 */
 
41
 
 
42
public class SizeTag extends TagSupport {
 
43
 
 
44
 
 
45
    // ------------------------------------------------------------- Properties
 
46
 
 
47
 
 
48
    /**
 
49
     * The actual collection to be counted.
 
50
     */
 
51
    protected Object collection = null;
 
52
 
 
53
    public Object getCollection() {
 
54
        return (this.collection);
 
55
    }
 
56
 
 
57
    public void setCollection(Object collection) {
 
58
        this.collection = collection;
 
59
    }
 
60
 
 
61
 
 
62
    /**
 
63
     * The name of the scripting variable that will be exposed as a page
 
64
     * scope attribute.
 
65
     */
 
66
    protected String id = null;
 
67
 
 
68
    public String getId() {
 
69
        return (this.id);
 
70
    }
 
71
 
 
72
    public void setId(String id) {
 
73
        this.id = id;
 
74
    }
 
75
 
 
76
 
 
77
    /**
 
78
     * The message resources for this package.
 
79
     */
 
80
    protected static MessageResources messages =
 
81
        MessageResources.getMessageResources
 
82
        ("org.apache.struts.taglib.bean.LocalStrings");
 
83
 
 
84
 
 
85
 
 
86
    /**
 
87
     * The name of the bean owning the property to be counted.
 
88
     */
 
89
    protected String name = null;
 
90
 
 
91
    public String getName() {
 
92
        return (this.name);
 
93
    }
 
94
 
 
95
    public void setName(String name) {
 
96
        this.name = name;
 
97
    }
 
98
 
 
99
 
 
100
    /**
 
101
     * The name of the property to be retrieved.
 
102
     */
 
103
    protected String property = null;
 
104
 
 
105
    public String getProperty() {
 
106
        return (this.property);
 
107
    }
 
108
 
 
109
    public void setProperty(String property) {
 
110
        this.property = property;
 
111
    }
 
112
 
 
113
 
 
114
    /**
 
115
     * The scope within which to search for the specified bean.
 
116
     */
 
117
    protected String scope = null;
 
118
 
 
119
    public String getScope() {
 
120
        return (this.scope);
 
121
    }
 
122
 
 
123
    public void setScope(String scope) {
 
124
        this.scope = scope;
 
125
    }
 
126
 
 
127
 
 
128
    // --------------------------------------------------------- Public Methods
 
129
 
 
130
 
 
131
    /**
 
132
     * Retrieve the required property and expose it as a scripting variable.
 
133
     *
 
134
     * @exception JspException if a JSP exception has occurred
 
135
     */
 
136
    public int doStartTag() throws JspException {
 
137
 
 
138
        // Retrieve the required property value
 
139
        Object value = this.collection;
 
140
        if (value == null) {
 
141
            if (name == null) {
 
142
                // Must specify either a collection attribute or a name
 
143
                // attribute.
 
144
                JspException e = new JspException
 
145
                    (messages.getMessage("size.noCollectionOrName"));
 
146
                TagUtils.getInstance().saveException(pageContext, e);
 
147
                throw e;
 
148
            }
 
149
            
 
150
            value = TagUtils.getInstance().lookup(pageContext, name, property, scope);
 
151
        }
 
152
 
 
153
        // Identify the number of elements, based on the collection type
 
154
        int size = 0;
 
155
        if (value == null) {
 
156
            JspException e = new JspException
 
157
                (messages.getMessage("size.collection"));
 
158
            TagUtils.getInstance().saveException(pageContext, e);
 
159
            throw 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();
 
166
        } else {
 
167
            JspException e = new JspException
 
168
                (messages.getMessage("size.collection"));
 
169
            TagUtils.getInstance().saveException(pageContext, e);
 
170
            throw e;
 
171
        }
 
172
 
 
173
        // Expose this size as a scripting variable
 
174
        pageContext.setAttribute(this.id, new Integer(size),
 
175
                                 PageContext.PAGE_SCOPE);
 
176
        return (SKIP_BODY);
 
177
 
 
178
    }
 
179
 
 
180
 
 
181
    /**
 
182
     * Release all allocated resources.
 
183
     */
 
184
    public void release() {
 
185
 
 
186
        super.release();
 
187
        collection = null;
 
188
        id = null;
 
189
        name = null;
 
190
        property = null;
 
191
        scope = null;
 
192
 
 
193
    }
 
194
 
 
195
 
 
196
}