⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 containstag.java

📁 struts+spring+hibernate自创框架
💻 JAVA
字号:
package com.pegasus.framework.component.taglib.logic;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.collections.IteratorUtils;
import org.apache.struts.taglib.TagUtils;
import org.apache.struts.util.MessageResources;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;


public class ContainsTag extends TagSupport {
    protected static MessageResources messages = MessageResources
            .getMessageResources("org.apache.struts.taglib.logic.LocalStrings");

    protected String name;

    protected String property;

    protected String value;

    protected String collectionName;

    protected String collectionProperty;

    protected String collectionValue;

    /**
     *
     * @return  .
     */
    public String getName() {
        return name;
    }

    /**
     *
     * @param name   .
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     *
     * @return     .
     */
    public String getProperty() {
        return property;
    }

    /**
     *
     * @param property    .
     */
    public void setProperty(String property) {
        this.property = property;
    }

    /**
     *
     * @return      .
     */
    public String getValue() {
        return value;
    }

    /**
     *
     * @param value      .
     */
    public void setValue(String value) {
        this.value = value;
    }

    /**
     *
     * @return        .
     */
    public String getCollectionName() {
        return collectionName;
    }

    /**
     *
     * @param collectionName      .
     */
    public void setCollectionName(String collectionName) {
        this.collectionName = collectionName;
    }

    /**
     *
     * @return      .
     */
    public String getCollectionProperty() {
        return collectionProperty;
    }

    /**
     *
     * @param collectionProperty    .
     */
    public void setCollectionProperty(String collectionProperty) {
        this.collectionProperty = collectionProperty;
    }

    /**
     *
     * @return   .
     */
    public String getCollectionValue() {
        return collectionValue;
    }

    /**
     *
     * @param collectionValue   .
     */
    public void setCollectionValue(String collectionValue) {
        this.collectionValue = collectionValue;
    }


    /**
     *
     * @return           .
     * @throws JspException   .
     */
    public int doStartTag() throws JspException {
        return !condition() ? SKIP_BODY : EVAL_BODY_INCLUDE;
    }

    /**
     *
     * @return  .
     * @throws JspException  .
     */
    public int doEndTag() throws JspException {
        return EVAL_PAGE;
    }

    /**
     *
     * @return     .
     * @throws JspException  .
     */
    protected boolean condition() throws JspException {
        if (value == null) {
            Object obj = TagUtils.getInstance().lookup(pageContext, name, property, null);
            value = formatValue(obj);
        }

        Object set_values = TagUtils.getInstance().lookup(pageContext, collectionName, collectionProperty, null);
        if (set_values == null) {
            JspException e = new JspException(messages.getMessage("containsTag.set"));
            TagUtils.getInstance().saveException(pageContext, e);
            throw e;
        }
        Set set ;
        if (set_values instanceof Set)
            set = (Set) set_values;
        else
            set = getSet(set_values);
        return set.contains(value);
    }

    /**
     *
     * @param value     .
     * @return         .
     * @throws JspException   .
     */
    protected String formatValue(Object value) throws JspException {
        if (value == null)
            return "";
        else
            return TagUtils.getInstance().filter(value.toString());
    }

    /**
     *
     * @param collection    .
     * @return          .
     * @throws JspException .
     */
    protected Iterator getIterator(Object collection) throws JspException {
        if (collection.getClass().isArray())
            collection = Arrays.asList((Object[]) collection);
        if (collection instanceof Collection)
            return ((Collection) collection).iterator();
        if (collection instanceof Iterator)
            return (Iterator) collection;
        if (collection instanceof Map)
            return ((Map) collection).values().iterator();
        if (collection instanceof Enumeration)
            return IteratorUtils.asIterator((Enumeration) collection);
        else
            throw new JspException(messages.getMessage("radioCollectionTag.iterator", collection.toString()));
    }

    /**
     *
     * @param iterator       .
     * @return             .
     * @throws JspException  .
     */
    protected Set initSet(Iterator iterator) throws JspException {
        Set set = new HashSet();
        while (iterator.hasNext()) {
            Object bean = iterator.next();
            if (collectionValue == null || "".equals(collectionValue))
                set.add(bean.toString());
            else {
                Object beanValue ;
                try {
                    beanValue = PropertyUtils.getProperty(bean, collectionValue);
                    if (beanValue == null)
                        beanValue = "";
                }
                catch (IllegalAccessException e) {
                    JspException jspe = new JspException(messages.getMessage("getter.access", collectionValue, bean));
                    TagUtils.getInstance().saveException(pageContext, jspe);
                    throw jspe;
                }
                catch (InvocationTargetException e) {
                    Throwable t = e.getTargetException();
                    JspException jspe = new JspException(messages.getMessage("getter.result", collectionValue, t
                            .toString()));
                    TagUtils.getInstance().saveException(pageContext, jspe);
                    throw jspe;
                }
                catch (NoSuchMethodException e) {
                    JspException jspe = new JspException(messages.getMessage("getter.method", collectionValue, bean));
                    TagUtils.getInstance().saveException(pageContext, jspe);
                    throw jspe;
                }
                set.add(beanValue.toString());
            }
        }
        return set;
    }

    /**
     *
     * @param collection   ;
     * @return            .
     * @throws JspException .
     */
    protected Set getSet(Object collection) throws JspException {
        Iterator iter = getIterator(collection);
        return initSet(iter);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -