/*
 * $Id$
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log$
 * Revision 1.2  2006/10/04 03:15:09  marbobo
 * + Vollständige Unterstützung für Templates implementiert
 * + Alles von Virtoweb auf ProCoBa umgestellt
 *
 * Revision 1.1  2006/03/09 01:35:13  marbobo
 * + Initiale Version
 *
 */

package vkc.view.tag;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;

import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.ResponseUtils;

import vkc.db.entities.VKCUser;
import vkc.util.ServletUtils;
import vkc.util.VKCStringUtils;

public class ImageTag extends org.apache.struts.taglib.html.ImageTag {
    /**
     * 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;
    }

    /**
     * Process the end of this tag.
     * [Indexed property since Struts 1.1]
     *
     * @exception JspException if a JSP exception has occurred
     */
    public int doEndTag() throws JspException {
        // Generate an HTML <input type="image"> element
        HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
        VKCUser user = ServletUtils.getUser((HttpServletRequest)pageContext.getRequest());
        String tmp = null;
        StringBuffer results = new StringBuffer();
        results.append("<input type=\"image\"");
        
        if (property != null) {
            results.append(" name=\"");
            results.append(property);
            // @since Struts 1.1
            if (indexed) {
                prepareIndex(results, null);
            }
            
            results.append("\"");
        }
        
        tmp = src();
        
        if (tmp != null) {
            results.append(" src=\"");
            results.append(response.encodeURL(ServletUtils.replacePathVars(tmp, user)));
            results.append("\"");
        }
        
        if (align != null) {
            results.append(" align=\"");
            results.append(align);
            results.append("\"");
        }
        
        if (border != null) {
            results.append(" border=\"");
            results.append(border);
            results.append("\"");
        }
        
        if (value != null) {
            results.append(" value=\"");
            results.append(value);
            results.append("\"");
        }
        
        if (accesskey != null) {
            results.append(" accesskey=\"");
            results.append(accesskey);
            results.append("\"");
        }
        
        if (tabindex != null) {
            results.append(" tabindex=\"");
            results.append(tabindex);
            results.append("\"");
        }
        
        results.append(prepareEventHandlers());
        results.append(prepareStyles());
        results.append(getElementClose());

        // Render this element to our writer
        ResponseUtils.write(pageContext, results.toString());

        // Evaluate the remainder of this page
        return EVAL_PAGE;
    }

    /**
    * Return the text specified by the literal value or the message resources
    * key, if any; otherwise return <code>null</code>.
    *
    * @param literal Literal text value or <code>null</code>
    * @param key Message resources key or <code>null</code>
    *
    * @exception JspException if both arguments are non-null
    */
	protected String message(String literal, String key) throws JspException {
	   if (literal != null) {
	       if (key != null) {
	           JspException e = new JspException(messages.getMessage("common.both"));
	           RequestUtils.saveException(pageContext, e);
	           throw e;
	       }

	       if (filter) {
	    	   return VKCStringUtils.filterUnicodeChars(literal);
	       }
	       
	       return literal;
	   } 

	   if (key != null) {
           if (filter) {
    		   return VKCStringUtils.filterUnicodeChars(RequestUtils.message(pageContext, getBundle(), getLocale(), key));
           }
           
		   return RequestUtils.message(pageContext, getBundle(), getLocale(), key);
       } 

       return null;
	}

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