/*
 * $Id: DefineTag.java,v 1.1 2004/11/08 12:32:13 koppy Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 * 
 * Created on 05.11.2004
 *
 * $Log: DefineTag.java,v $
 * Revision 1.1  2004/11/08 12:32:13  koppy
 * initiale version
 *
 */
package vkc.view.tag;

import java.util.Enumeration;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;

import org.apache.struts.util.RequestUtils;

/**
 * Define a scripting variable based on the value(s) of the specified
 * bean property.
 *
 * @author Craig R. McClanahan
 * @version $Revision: 1.1 $ $Date: 2004/11/08 12:32:13 $
 */

public class DefineTag extends org.apache.struts.taglib.bean.DefineTag{

    

    /**
     * The name of the bean owning the property to be exposed.
     */
    protected String nameSuffix = null;

    public String getNameSuffix() {
        return (this.nameSuffix);
    }

    public void setNameSuffix(String name) {
        this.nameSuffix = name;
    }


    
    /**
     * Retrieve the required property and expose it as a scripting variable.
     *
     * @exception JspException if a JSP exception has occurred
     */
    public int doEndTag() throws JspException {

        // Enforce restriction on ways to declare the new value
        int n = 0;
        if (this.body != null) {
            n++;
        }
        if (this.nameSuffix != null) {
            n++;
        }
        if (this.value != null) {
            n++;
        }
        if (n != 1) {
            JspException e =
                new JspException(messages.getMessage("define.value"));
            RequestUtils.saveException(pageContext, e);
            throw e;
        }

        // Retrieve the required property value
        Object value = this.value;
        if ((value == null) && (nameSuffix != null)) {
            
            Enumeration e = pageContext.getAttributeNamesInScope( RequestUtils.getScope(scope) );
            while( e.hasMoreElements()){
                String name = (String)e.nextElement();
                if(name.endsWith( nameSuffix)){
                    value = RequestUtils.lookup(pageContext, name, property, scope);
                    break;
                }
            }
        }
        if ((value == null) && (body != null)) {
            value = body;
        }
        if (value == null) {
            JspException e =
                new JspException(messages.getMessage("define.null"));
            RequestUtils.saveException(pageContext, e);
            throw e;
        }

        // Expose this value as a scripting variable
        int inScope = PageContext.PAGE_SCOPE;
        try {
			if (toScope != null) {
				inScope = RequestUtils.getScope(toScope);
			}
		} catch (JspException e) {
			//  toScope was invalid name so we default to PAGE_SCOPE
		}

        pageContext.setAttribute(id, value, inScope);

        // Continue processing this page
        return (EVAL_PAGE);

    }

    /**
     * Release all allocated resources.
     */
    public void release() {

        super.release();
        nameSuffix = null;
    }


}
