001: /*
002: * $Id: EmptyTag.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.logic;
022:
023: import org.apache.struts.taglib.TagUtils;
024:
025: import javax.servlet.jsp.JspException;
026:
027: import java.lang.reflect.Array;
028:
029: import java.util.Collection;
030: import java.util.Map;
031:
032: /**
033: * Evalute the nested body content of this tag if the specified value is empty
034: * for this request.
035: *
036: * @version $Rev: 471754 $ $Date: 2004-10-16 12:38:42 -0400 (Sat, 16 Oct 2004)
037: * $
038: * @since Struts 1.1
039: */
040: public class EmptyTag extends ConditionalTagBase {
041: // ------------------------------------------------------ Protected Methods
042:
043: /**
044: * Evaluate the condition that is being tested by this particular tag, and
045: * return <code>true</code> if the nested body content of this tag should
046: * be evaluated, or <code>false</code> if it should be skipped. This
047: * method must be implemented by concrete subclasses.
048: *
049: * @throws JspException if a JSP exception occurs
050: */
051: protected boolean condition() throws JspException {
052: return (condition(true));
053: }
054:
055: /**
056: * Evaluate the condition that is being tested by this particular tag, and
057: * return <code>true</code> if the nested body content of this tag should
058: * be evaluated, or <code>false</code> if it should be skipped. This
059: * method must be implemented by concrete subclasses.
060: *
061: * @param desired Desired outcome for a true result
062: * @throws JspException if a JSP exception occurs
063: */
064: protected boolean condition(boolean desired) throws JspException {
065: if (this .name == null) {
066: JspException e = new JspException(messages
067: .getMessage("empty.noNameAttribute"));
068:
069: TagUtils.getInstance().saveException(pageContext, e);
070: throw e;
071: }
072:
073: Object value = null;
074:
075: if (this .property == null) {
076: value = TagUtils.getInstance().lookup(pageContext, name,
077: scope);
078: } else {
079: value = TagUtils.getInstance().lookup(pageContext, name,
080: property, scope);
081: }
082:
083: boolean empty = true;
084:
085: if (value == null) {
086: empty = true;
087: } else if (value instanceof String) {
088: String strValue = (String) value;
089:
090: empty = (strValue.length() < 1);
091: } else if (value instanceof Collection) {
092: Collection collValue = (Collection) value;
093:
094: empty = collValue.isEmpty();
095: } else if (value instanceof Map) {
096: Map mapValue = (Map) value;
097:
098: empty = mapValue.isEmpty();
099: } else if (value.getClass().isArray()) {
100: empty = Array.getLength(value) == 0;
101: } else {
102: empty = false;
103: }
104:
105: return (empty == desired);
106: }
107: }
|