/*
 * $Id$
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log$
 * Revision 1.1  2006/03/09 01:35:14  marbobo
 * + Initiale Version
 *
 */

package vkc.view.tag;

import java.util.Iterator;

import javax.servlet.jsp.JspException;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.ResponseUtils;

import vkc.util.VKCStringUtils;

public class ErrorsTag extends org.apache.struts.taglib.html.ErrorsTag {
    /**
     * Filter the rendered output for characters that are sensitive in HTML?
     */
    protected boolean filter = true;

    public boolean getFilter() {
        return filter;
    }

    public void setFilter(boolean filter) {
        this.filter = filter;
    }

    /**
     * Render the specified error messages if there are any.
     *
     * @exception JspException if a JSP exception has occurred
     */
    public int doStartTag() throws JspException {
        // Were any error messages specified?
        ActionErrors errors = null;
        
        try {
            errors = RequestUtils.getActionErrors(pageContext, name);
        } catch (JspException e) {
            RequestUtils.saveException(pageContext, e);
            throw e;
        }

        if ((errors == null) || errors.isEmpty()) {
            return EVAL_BODY_INCLUDE;
        }

        boolean headerPresent = RequestUtils.present(pageContext, bundle, locale, "errors.header");
        boolean footerPresent = RequestUtils.present(pageContext, bundle, locale, "errors.footer");
        boolean prefixPresent = RequestUtils.present(pageContext, bundle, locale, "errors.prefix");
        boolean suffixPresent = RequestUtils.present(pageContext, bundle, locale, "errors.suffix");

        // Render the error messages appropriately
        StringBuffer results = new StringBuffer();
        boolean headerDone = false;
        String message = null;
        Iterator reports = (property == null) ? errors.get() : errors.get(property);

        while (reports.hasNext()) {
            ActionError report = (ActionError) reports.next();
            
            if (!headerDone) {
                if (headerPresent) {
                    message = RequestUtils.message(pageContext, bundle, locale, "errors.header");
                    results.append(message);
                    results.append(lineEnd);
                }

                headerDone = true;
            }
            
            if (prefixPresent) {
                message = RequestUtils.message(pageContext, bundle, locale, "errors.prefix");
                results.append(message);
            }
            
            message = RequestUtils.message(pageContext, bundle, locale, report.getKey(), report.getValues());
                    
            if (message != null) {
                if (filter) {
                	message = VKCStringUtils.filterUnicodeChars(message);
                }
                
                results.append(message);
                results.append(lineEnd);
            }
            
            if (suffixPresent) {
                message = RequestUtils.message(pageContext, bundle, locale, "errors.suffix");
                results.append(message);
            }
        }
        
        if (headerDone && footerPresent) {
            message = RequestUtils.message(pageContext, bundle, locale, "errors.footer");
            results.append(message);
            results.append(lineEnd);
        }

        ResponseUtils.write(pageContext, results.toString());

        return EVAL_BODY_INCLUDE;
    }

    /**
     * Release any acquired resources.
     */
    public void release() {
        super.release();
        filter = true;
    }
}