package org.apache.struts.taglib.errors;

import java.util.Iterator;
import java.util.Locale;
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;
import org.apache.struts.util.BeanUtils;
import org.apache.struts.util.ErrorMessages;
import org.apache.struts.util.MessageResources;

public class BaseErrorsTag extends TagSupport {

    // --------------------------------------------------- Static Variables

    protected static String ERRORS_HEADER_KEY =
     "org.apache.struts.errors.header";

    protected static String ERRORS_FOOTER_KEY = 
     "org.apache.struts.errors.footer";

    protected static String ERRORS_PROP_HEADER_KEY = 
     "org.apache.struts.errors.prop.header";

    protected static String ERRORS_PROP_FOOTER_KEY = 
     "org.apache.struts.errors.prop.footer";

    protected static String ERRORS_PROP_NAME_HEADER_KEY = 
     "org.apache.struts.errors.prop.name.header";

    protected static String ERRORS_PROP_NAME_FOOTER_KEY = 
     "org.apache.struts.errors.prop.name.footer";

    protected static String ERRORS_PROP_MESSAGES_HEADER_KEY =
     "org.apache.struts.errors.prop.messages.header";

    protected static String ERRORS_PROP_MESSAGES_FOOTER_KEY =
     "org.apache.struts.errors.prop.messages.footer";

    protected static String ERRORS_PROP_MESSAGE_HEADER_KEY = 
     "org.apache.struts.errors.prop.message.header";

    protected static String ERRORS_PROP_MESSAGE_FOOTER_KEY = 
     "org.apache.struts.errors.prop.message.footer";

    // --------------------------------------------------- Instance Variables


    /**
     * The default locale on our server.
     */
    private static Locale defaultLocale = Locale.getDefault();


    /**
     * Name of the request scope attribute containing our error messages,
     * if any.
     */
    private String name = Action.ERROR_KEY;


    /**
     * Display key if message not exist.
     */
    private String flexible = null;

    // --------------------------------------------------- Constructors

    public BaseErrorsTag() {
     super();
    }

    // ---------------------------------------------------- Protected Methods

    protected ActionErrorsExt storedErrors() {
        ActionErrorsExt errors = new ActionErrorsExt();
        try {
            Object value = pageContext.getAttribute
                (name, PageContext.REQUEST_SCOPE);
            if (value == null) {
                ;
            } else if (value instanceof String) {
                errors.add(ActionErrors.GLOBAL_ERROR,
                           new ActionError((String) value));
            } else if (value instanceof String[]) {
                String keys[] = (String[]) value;
                for (int i = 0; i < keys.length; i++)
                    errors.add(ActionErrors.GLOBAL_ERROR,
                               new ActionError(keys[i]));
            } else if (value instanceof ErrorMessages) {
                String keys[] = ((ErrorMessages) value).getErrors();
                if (keys == null)
                    keys = new String[0];
                for (int i = 0; i < keys.length; i++)
                    errors.add(ActionErrors.GLOBAL_ERROR,
                               new ActionError(keys[i]));
            } else if (value instanceof ActionErrors) {
                errors = (ActionErrorsExt) value;
            }
        } catch (Exception e) {
            ;
        }
        return errors;
    }


    protected Locale getLocale() {
        Locale locale = null;
        try {
            locale = (Locale) pageContext.getAttribute
                (Action.LOCALE_KEY, PageContext.SESSION_SCOPE);
        } catch (IllegalStateException e) {     // Invalidated session
            locale = null;
        }
        if (locale == null)
            locale = defaultLocale;
        return locale;
    }


    // ----------------------------------------------------------- Properties

    /**
     * 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) {

        this.name = name;

    }


    /**
     * Return the errors attribute flexible.
     */
    public String getFlexible() {

        return (this.flexible);

    }

    /**
     * Set the errors attribute flexible.
     *
     * @param flexible The new errors attribute flexible
     */
    public void setFlexible(String flexible) {

        this.flexible = flexible;

    }

    // ------------------------------------------------------- Public Methods

    /**
     * Release any acquired resources.
     */
    public void release() {

        super.release();
        name = Action.ERROR_KEY;
        flexible = null;

    }

}