View Javadoc

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