/*
 * $Header: /home/cvsroot/main/com/hm/util/taglib/LinkParamTag.java,v 1.1 2001/06/08 08:10:57 efe Exp $
 * $Revision: 1.1 $
 * $Date: 2001/06/08 08:10:57 $
 *
 * ====================================================================
 */


package com.hm.util.taglib;

import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;

/**
 * Implements a custom tag to add parameter to a href request.<br/>
 * This tag is intended to be nested in a <code>hm:link</code> tag.
 *
 * Title:        BSquare
 * Description:  Bsquare Projects
 * Copyright:    Copyright (c) 2001
 * Company:      HubMethods
 * @author Eric Fesler
 * @version 1.0
 */

public class LinkParamTag extends BodyTagSupport {

    // ----------------------------------------------------- Instance variables
    /**
     * The name of the request parameter
     */
    private String name = null;

    /**
     * The value of the request parameter
     */
    private String value = null;


    // ----------------------------------------------------- Properties

    /**
     * Sets the request parameter tag name
     *
     * @param name the request parameter tag name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * Returns the request parameter name
     *
     * @return the request parameter name
     */
    public String getName() {
        return name;
    }

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

    /**
     * Returns the request parameter value
     *
     * @return the request parameter value
     */
    public String getValue() {
        return value;
    }


    // ------------------------------------------------------ Public Methods
    /**
     * Add the parameter and its value to the link tag
     */
    public int doAfterBody() throws JspException {
        // parent tag must be a LinkTag, gives access to methods in parent
	LinkTag myparent = (LinkTag)javax.servlet.jsp.tagext.TagSupport.findAncestorWithClass(this, LinkTag.class);

	if (myparent == null)
	    throw new JspException("linkparam tag not nested within link tag");
        else {
            BodyContent bodyContent = getBodyContent();
            if (bodyContent != null && !bodyContent.getString().equals("")) {
                setValue(bodyContent.getString());
            }
            else if (getValue() == null)
                throw new JspException("both value attribute and body are empty");
	    myparent.addRequestParameter(getName(), getValue());
	}
	return SKIP_BODY;
    }
}