001: /*
002: * $Id: SizeTag.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.taglib.bean;
022:
023: import org.apache.struts.taglib.TagUtils;
024: import org.apache.struts.util.MessageResources;
025:
026: import javax.servlet.jsp.JspException;
027: import javax.servlet.jsp.PageContext;
028: import javax.servlet.jsp.tagext.TagSupport;
029:
030: import java.lang.reflect.Array;
031:
032: import java.util.Collection;
033: import java.util.Map;
034:
035: /**
036: * Define a scripting variable that will contain the number of elements found
037: * in a specified array, Collection, or Map.
038: *
039: * @version $Rev: 471754 $ $Date: 2004-10-16 12:38:42 -0400 (Sat, 16 Oct 2004)
040: * $
041: */
042: public class SizeTag extends TagSupport {
043: /**
044: * The message resources for this package.
045: */
046: protected static MessageResources messages = MessageResources
047: .getMessageResources("org.apache.struts.taglib.bean.LocalStrings");
048:
049: // ------------------------------------------------------------- Properties
050:
051: /**
052: * The actual collection to be counted.
053: */
054: protected Object collection = null;
055:
056: /**
057: * The name of the scripting variable that will be exposed as a page scope
058: * attribute.
059: */
060: protected String id = null;
061:
062: /**
063: * The name of the bean owning the property to be counted.
064: */
065: protected String name = null;
066:
067: /**
068: * The name of the property to be retrieved.
069: */
070: protected String property = null;
071:
072: /**
073: * The scope within which to search for the specified bean.
074: */
075: protected String scope = null;
076:
077: public Object getCollection() {
078: return (this .collection);
079: }
080:
081: public void setCollection(Object collection) {
082: this .collection = collection;
083: }
084:
085: public String getId() {
086: return (this .id);
087: }
088:
089: public void setId(String id) {
090: this .id = id;
091: }
092:
093: public String getName() {
094: return (this .name);
095: }
096:
097: public void setName(String name) {
098: this .name = name;
099: }
100:
101: public String getProperty() {
102: return (this .property);
103: }
104:
105: public void setProperty(String property) {
106: this .property = property;
107: }
108:
109: public String getScope() {
110: return (this .scope);
111: }
112:
113: public void setScope(String scope) {
114: this .scope = scope;
115: }
116:
117: // --------------------------------------------------------- Public Methods
118:
119: /**
120: * Retrieve the required property and expose it as a scripting variable.
121: *
122: * @throws JspException if a JSP exception has occurred
123: */
124: public int doStartTag() throws JspException {
125: // Retrieve the required property value
126: Object value = this .collection;
127:
128: if (value == null) {
129: if (name == null) {
130: // Must specify either a collection attribute or a name
131: // attribute.
132: JspException e = new JspException(messages
133: .getMessage("size.noCollectionOrName"));
134:
135: TagUtils.getInstance().saveException(pageContext, e);
136: throw e;
137: }
138:
139: value = TagUtils.getInstance().lookup(pageContext, name,
140: property, scope);
141: }
142:
143: // Identify the number of elements, based on the collection type
144: int size = 0;
145:
146: if (value == null) {
147: JspException e = new JspException(messages
148: .getMessage("size.collection"));
149:
150: TagUtils.getInstance().saveException(pageContext, e);
151: throw e;
152: } else if (value.getClass().isArray()) {
153: size = Array.getLength(value);
154: } else if (value instanceof Collection) {
155: size = ((Collection) value).size();
156: } else if (value instanceof Map) {
157: size = ((Map) value).size();
158: } else {
159: JspException e = new JspException(messages
160: .getMessage("size.collection"));
161:
162: TagUtils.getInstance().saveException(pageContext, e);
163: throw e;
164: }
165:
166: // Expose this size as a scripting variable
167: pageContext.setAttribute(this .id, new Integer(size),
168: PageContext.PAGE_SCOPE);
169:
170: return (SKIP_BODY);
171: }
172:
173: /**
174: * Release all allocated resources.
175: */
176: public void release() {
177: super.release();
178: collection = null;
179: id = null;
180: name = null;
181: property = null;
182: scope = null;
183: }
184: }
|