Source code: com/RuntimeCollective/webapps/tag/HasMembersTag.java
1 /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/webapps/tag/HasMembersTag.java,v 1.8 2003/09/30 15:13:18 joe Exp $
2 * $Revision: 1.8 $
3 * $Date: 2003/09/30 15:13:18 $
4 *
5 * ====================================================================
6 *
7 * Josephine : http://www.runtime-collective.com/josephine/index.html
8 *
9 * Copyright (C) 2003 Runtime Collective
10 *
11 * This product includes software developed by the
12 * Apache Software Foundation (http://www.apache.org/).
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 */
29
30 package com.RuntimeCollective.webapps.tag;
31
32 import java.util.Collection;
33 import java.util.Iterator;
34 import java.util.Enumeration;
35 import java.util.Map;
36 import javax.servlet.http.HttpServletRequest;
37 import javax.servlet.jsp.JspException;
38 import javax.servlet.jsp.PageContext;
39 import org.apache.commons.beanutils.PropertyUtils;
40 import org.apache.struts.util.RequestUtils;
41 import org.apache.struts.taglib.logic.ConditionalTagBase;
42
43 import com.RuntimeCollective.webapps.RuntimeParameters;
44
45 /**
46 * Evalute the nested body content of this tag if the specified value
47 * is present on this request, and is an array, map, iterator, enumeration or collection of length>0.
48 *
49 * This tag takes the following parameters:
50 * <ul>
51 * <li> <code>name</code> -- The name of the bean that holds the value. [Mandatory]</li>
52 * <li> <code>property</code> -- The property on the bean that holds the value. [Optional]</li>
53 * <li> <code>scope</code> -- The scope of the bean that holds the value. [Optional]</li>
54 * <li> <code>collection</code> -- A runtime expression that evaluates to a collection. If this is defined then name, property, and scope are ignored. [Optional]</li>
55 * </ul>
56 *
57 * @version $Id: HasMembersTag.java,v 1.8 2003/09/30 15:13:18 joe Exp $
58 */
59 public class HasMembersTag extends ConditionalTagBase {
60
61 /** A collection. */
62 protected Object collection = null;
63 /** Get a collection. */
64 public Object getCollection() { return this.collection; }
65 /** Set a collection. */
66 public void setCollection(Object collection) { this.collection = collection; }
67
68 /**
69 * Evaluate the condition that is being tested by this particular tag,
70 * and return <code>true</code> if the nested body content of this tag
71 * should be evaluated, or <code>false</code> if it should be skipped.
72 * This method must be implemented by concrete subclasses.
73 *
74 * @exception JspException if a JSP exception occurs
75 */
76 protected boolean condition() throws JspException {
77 return (condition(true));
78 }
79
80
81 /**
82 * Evaluate the condition that is being tested by this particular tag,
83 * and return <code>true</code> if the nested body content of this tag
84 * should be evaluated, or <code>false</code> if it should be skipped.
85 * This method must be implemented by concrete subclasses.
86 *
87 * @param desired Desired outcome for a true result
88 *
89 * @exception JspException if a JSP exception occurs
90 */
91 protected boolean condition(boolean desired) throws JspException {
92
93 // Evaluate the length of the specified value
94 boolean lengthOK = false;
95 Object value = null;
96 try {
97
98 // Get value from request
99 if ( collection!=null ) value = collection;
100 else value = RequestUtils.lookup(pageContext, name, property, scope);
101
102 if ( value != null ) {
103 // If value is a collection
104 if (value instanceof Collection) lengthOK = ( ( (Collection) value).size()>0);
105 // If value is an Iterator
106 else if (value instanceof Iterator) lengthOK = ( (Iterator) value).hasNext();
107 // If value is an Enumeration
108 else if (value instanceof Enumeration) lengthOK = ( (Enumeration) value).hasMoreElements();
109 // If value is a map
110 else if (value instanceof Map) lengthOK = ( ( (Map) value).size()>0);
111 // If value is an array
112 else if (value instanceof Object[]) lengthOK = ( ( (Object[]) value).length>0);
113 else RuntimeParameters.logError( this, "Cannot test length of array/collection/map for name="+name+" property="+property+" scope="+scope+". Runtime class was "+value.getClass().getName() );
114 }
115 } catch (Exception e) {
116 RuntimeParameters.logError( this, "Cannot test length of array/collection/map for name="+name+" property="+property+" scope="+scope,e);
117 throw new JspException(e.toString());
118 }
119
120 return (lengthOK == desired);
121
122 }
123
124 public void release() {
125 super.release();
126 collection=null;
127 }
128 }