Subversion Repositories general

Rev

Rev 961 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/**
 * Extends and based on Apache Struts source code.
 *
 * Copyleft Anatoli Klassen (anatoli@aksoft.net)
 */
package ak.strutsx.taglib;

import java.util.Iterator;
import java.util.Collection;
import java.util.Map;
import javax.servlet.jsp.JspException;
import org.apache.struts.util.RequestUtils;
import org.apache.struts.taglib.logic.ConditionalTagBase;

public class EmptyTag
        extends ConditionalTagBase
{
    protected boolean condition()
        throws JspException
    {
        return condition(true);
    }

    protected boolean condition(boolean desired)
        throws JspException
    {
                if(this.name == null) {
                        JspException e = new JspException(messages.getMessage("empty.noNameAttribute"));
                        RequestUtils.saveException(pageContext, e);
                        throw e;
                }

                boolean empty = true;

                Object value = null;
                if(this.property == null)
                        value = RequestUtils.lookup(pageContext, name, scope);
                else
                        value = RequestUtils.lookup(pageContext, name, property, scope);

                if(value != null) {
                        if(value instanceof String)
                                empty = (((String)value).length() < 1);
                        else if(value instanceof Collection)
                                empty = ((Collection)value).isEmpty();
                        else if(value instanceof Map)
                                empty = ((Map)value).isEmpty();
                        else if(value instanceof Iterator)
                                empty = !((Iterator)value).hasNext();
                        else
                                empty = false;
                }

                return (empty == desired);
    }
}