foreachattributetag.java

来自「jakarta-taglibs」· Java 代码 · 共 146 行

JAVA
146
字号
/*
 * Copyright 1999,2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 

package org.apache.taglibs.jndi;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import javax.naming.*;
import javax.naming.directory.*;

import java.io.*;

/**
 *
 * @author  Danno Ferrin <shemnon@earthlink.net>
 * @version $Revision: 1.3 $
 */
public class ForEachAttributeTag extends BodyTagSupport {

    private String attributeRef;
    private Object attributeObject;
    private NamingEnumeration nameEnum;
    private Attribute currentAttribute;
    private int scope = PageContext.PAGE_SCOPE;

    /** Creates new ForEachAttributeTag */
    public ForEachAttributeTag() {
    }

    /**
     * Setter for property scope.
     * @param Scope New value of property scope.
     */
    public void setScope(String scope) {
        this.scope = decodeScope(scope);
    }

    /**
     * Getter for property object.
     * @return Value of property object.
     */
    public Object getObject() {
        return attributeObject;
    }

    /**
     * Setter for property object.
     * @param object New value of property object.
     */
    public void setObject(DirContext object) {
        attributeObject = object;
    }

    /**
     * Setter for property ref.
     * @param ref New value of property ref.
     */
    public void setRef(String ref) {
        this.attributeRef = ref;
    }
    
    public int doStartTag() throws JspException {
        // Initialize invocation variables
        nameEnum = null;

        if( attributeRef != null ) {
            attributeObject = pageContext.findAttribute(attributeRef);
        }
        try {
            if ((attributeObject instanceof DirContext) 
             && (attributeObject != null)) {
                attributeObject = ((DirContext)attributeObject).getAttributes("");
            }

            if ((attributeObject instanceof SearchResult) 
             && (attributeObject != null)) {
                attributeObject = ((SearchResult)attributeObject)
                        .getAttributes(); 
            }

            if ((attributeObject instanceof Attributes)
             && (attributeObject != null)) {
                nameEnum = ((Attributes)attributeObject).getAll();
            } 
        } catch (NamingException ne) {
        }
        
        if ((nameEnum != null) && nameEnum.hasMoreElements()) {
            return EVAL_BODY_TAG;
        } else {
            return SKIP_BODY;
        }
    }
    
    public void doInitBody() {
        currentAttribute = (Attribute) nameEnum.nextElement();
        pageContext.setAttribute(getId(), currentAttribute, scope);
    }

    public int doAfterBody() {
        if (!nameEnum.hasMoreElements()) {
            return SKIP_BODY;
        } else {
            currentAttribute = (Attribute) nameEnum.nextElement();
            pageContext.setAttribute(getId(), currentAttribute, scope);
            return EVAL_BODY_TAG;
        }                
    }
    
    public int doEndTag() throws JspException {
        try {
            if (bodyContent != null) {
                bodyContent.writeOut(pageContext.getOut());
            } 
        } catch (IOException ioe) {
            throw new JspException(ioe.toString());
        }
        return EVAL_PAGE;
    }
    
    public static int decodeScope(String scope) {
        if (scope.equalsIgnoreCase("request")) {
            return PageContext.REQUEST_SCOPE;
        } else if (scope.equalsIgnoreCase("session")) {
            return PageContext.SESSION_SCOPE;
        } else if (scope.equalsIgnoreCase("application")) {
            return PageContext.APPLICATION_SCOPE;
        } else {
            return PageContext.PAGE_SCOPE;
        }
    }
}

⌨️ 快捷键说明

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