package org.apache.struts.taglib.errors;

import java.util.Iterator;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionErrorsExt;

public class ErrorTag extends TagSupport {

    // --------------------------------------------------- Instance Variables


    /**
     * The default locale on our server.
     */
    protected static Locale defaultLocale = Locale.getDefault();

    // --------------------------------------------------- Constructors

    public ErrorTag() {
     super();
    }

    // ---------------------------------------------------- Protected Methods



    // ----------------------------------------------------------- Properties

    /**
     * Name of the request scope attribute containing our error messages,
     * if any.
     */
    protected String name = Action.ERROR_KEY;

    /**
     * Return the errors attribute name.
     */
    public String getName() {

        return (this.name);

    }

    /**
     * Set the errors attribute name.
     *
     * @param name The new errors attribute name
     */
    public void setName(String name) {

       if( name!=null )
        this.name = name;

    }

    /**
     * Name of the property - source of error.
     */
    protected String property = ActionErrors.GLOBAL_ERROR;

    /**
     * Return the errors attribute property.
     */
    public String getProperty() {

        return (this.property);

    }

    /**
     * Set the errors attribute property.
     *
     * @param property The new errors attribute property
     */
    public void setProperty(String property) {

       if( property!=null )
        this.property = property;

    }

    /**
     * Key of the property - key of error message.
     */
    protected String key = null;

    /**
     * Return the errors attribute key.
     */
    public String getKey() {

        return (this.key);

    }

    /**
     * Set the errors attribute key.
     *
     * @param key The new errors attribute key
     */
    public void setKey(String key) {

        this.key = key;

    }

    /**
     * The first optional argument.
     */
    protected String arg0 = null;

    public String getArg0() {
        return (this.arg0);
    }

    public void setArg0(String arg0) {
        this.arg0 = arg0;
    }


    /**
     * The second optional argument.
     */
    protected String arg1 = null;

    public String getArg1() {
        return (this.arg1);
    }

    public void setArg1(String arg1) {
        this.arg1 = arg1;
    }


    /**
     * The third optional argument.
     */
    protected String arg2 = null;

    public String getArg2() {
        return (this.arg2);
    }

    public void setArg2(String arg2) {
        this.arg2 = arg2;
    }


    /**
     * The fourth optional argument.
     */
    protected String arg3 = null;

    public String getArg3() {
        return (this.arg3);
    }

    public void setArg3(String arg3) {
        this.arg3 = arg3;
    }

    // ------------------------------------------------------- Public Methods

    /**
     * Save the specified error messages.
     *
     * @exception JspException if a JSP exception has occurred
     */
    public int doStartTag() throws JspException {
     HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
     ActionErrorsExt errors = (ActionErrorsExt)request.getAttribute( name );
     if( errors==null ) errors = new ActionErrorsExt();
     errors.add( property, new ActionError( key, arg0, arg1, arg2, arg3 ) );
     request.setAttribute( name, errors );
     return SKIP_BODY;
    }

    /**
     * Release any acquired resources.
     */
    public void release() {

        super.release();
        name = Action.ERROR_KEY;
        key = null;
        property = ActionErrors.GLOBAL_ERROR;

    }

}