KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > taglib > bean > SizeTag


1 /*
2  * $Id: SizeTag.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
20 package org.apache.struts.taglib.bean;
21
22
23 import java.lang.reflect.Array JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Map JavaDoc;
26 import javax.servlet.jsp.JspException JavaDoc;
27 import javax.servlet.jsp.PageContext JavaDoc;
28 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
29 import org.apache.struts.util.MessageResources;
30 import org.apache.struts.taglib.TagUtils;
31
32
33 /**
34  * Define a scripting variable that will contain the number of elements
35  * found in a specified array, Collection, or Map.
36  *
37  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
38  */

39
40 public class SizeTag extends TagSupport JavaDoc {
41
42
43     // ------------------------------------------------------------- Properties
44

45
46     /**
47      * The actual collection to be counted.
48      */

49     protected Object JavaDoc collection = null;
50
51     public Object JavaDoc getCollection() {
52         return (this.collection);
53     }
54
55     public void setCollection(Object JavaDoc collection) {
56         this.collection = collection;
57     }
58
59
60     /**
61      * The name of the scripting variable that will be exposed as a page
62      * scope attribute.
63      */

64     protected String JavaDoc id = null;
65
66     public String JavaDoc getId() {
67         return (this.id);
68     }
69
70     public void setId(String JavaDoc id) {
71         this.id = id;
72     }
73
74
75     /**
76      * The message resources for this package.
77      */

78     protected static MessageResources messages =
79     MessageResources.getMessageResources
80     ("org.apache.struts.taglib.bean.LocalStrings");
81
82
83
84     /**
85      * The name of the bean owning the property to be counted.
86      */

87     protected String JavaDoc name = null;
88
89     public String JavaDoc getName() {
90         return (this.name);
91     }
92
93     public void setName(String JavaDoc name) {
94         this.name = name;
95     }
96
97
98     /**
99      * The name of the property to be retrieved.
100      */

101     protected String JavaDoc property = null;
102
103     public String JavaDoc getProperty() {
104         return (this.property);
105     }
106
107     public void setProperty(String JavaDoc property) {
108         this.property = property;
109     }
110
111
112     /**
113      * The scope within which to search for the specified bean.
114      */

115     protected String JavaDoc scope = null;
116
117     public String JavaDoc getScope() {
118         return (this.scope);
119     }
120
121     public void setScope(String JavaDoc scope) {
122         this.scope = scope;
123     }
124
125
126     // --------------------------------------------------------- Public Methods
127

128
129     /**
130      * Retrieve the required property and expose it as a scripting variable.
131      *
132      * @exception JspException if a JSP exception has occurred
133      */

134     public int doStartTag() throws JspException JavaDoc {
135
136         // Retrieve the required property value
137
Object JavaDoc value = this.collection;
138         if (value == null) {
139             if (name == null) {
140                 // Must specify either a collection attribute or a name
141
// attribute.
142
JspException JavaDoc e = new JspException JavaDoc
143                     (messages.getMessage("size.noCollectionOrName"));
144                 TagUtils.getInstance().saveException(pageContext, e);
145                 throw e;
146             }
147             
148             value = TagUtils.getInstance().lookup(pageContext, name, property, scope);
149         }
150
151         // Identify the number of elements, based on the collection type
152
int size = 0;
153         if (value == null) {
154             JspException JavaDoc e = new JspException JavaDoc
155                 (messages.getMessage("size.collection"));
156             TagUtils.getInstance().saveException(pageContext, e);
157             throw e;
158         } else if (value.getClass().isArray()) {
159             size = Array.getLength(value);
160         } else if (value instanceof Collection JavaDoc) {
161             size = ((Collection JavaDoc) value).size();
162         } else if (value instanceof Map JavaDoc) {
163             size = ((Map JavaDoc) value).size();
164         } else {
165             JspException JavaDoc e = new JspException JavaDoc
166                 (messages.getMessage("size.collection"));
167             TagUtils.getInstance().saveException(pageContext, e);
168             throw e;
169         }
170
171         // Expose this size as a scripting variable
172
pageContext.setAttribute(this.id, new Integer JavaDoc(size),
173                                  PageContext.PAGE_SCOPE);
174         return (SKIP_BODY);
175
176     }
177
178
179     /**
180      * Release all allocated resources.
181      */

182     public void release() {
183
184         super.release();
185         collection = null;
186         id = null;
187         name = null;
188         property = null;
189         scope = null;
190
191     }
192
193
194 }
195
Popular Tags