/*
 * $Id: ImgTag.java,v 1.4 2006/10/04 03:15:09 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: ImgTag.java,v $
 * Revision 1.4  2006/10/04 03:15:09  marbobo
 * + Vollständige Unterstützung für Templates implementiert
 * + Alles von Virtoweb auf ProCoBa umgestellt
 *
 * Revision 1.3  2006/03/09 01:37:48  marbobo
 * + Unicode-Unterstützung für Html implementiert
 *
 */

package vkc.view.tag;

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

import org.apache.struts.util.RequestUtils;

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

/**
 * @see <code>org.apache.struts.taglib.html.ImgTag</code>. SessionID
 * wird nicht mehr in den Pfad des Bildes kodiert. Auslieferung
 * unter per Apache Webserver ist dann einfacher.
 * <p>
 * @author bigmac
 */
public class ImgTag extends org.apache.struts.taglib.html.ImgTag {
    /**
     * 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;
    }

    /**
     * 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;
 	}

	/**
     * Render the end of the IMG tag.
     *
     * @exception JspException if a JSP exception has occurred
     */
    public int doEndTag() throws JspException {
        // Generate the name definition or image element
        StringBuffer results = new StringBuffer("<img");
        String tmp = src();
        VKCUser user = ServletUtils.getUser((HttpServletRequest)pageContext.getRequest());
        String srcurl = url(ServletUtils.replacePathVars(tmp, user));
        
        if (srcurl != null) {
            results.append(" src=\"");
            results.append( srcurl );
            results.append("\"");
        }
        
        if (imageName != null) {
            results.append(" name=\"");
            results.append(imageName);
            results.append("\"");
        }
        
        if (height != null) {
            results.append(" height=\"");
            results.append(height);
            results.append("\"");
        }
        
        if (width != null) {
            results.append(" width=\"");
            results.append(width);
            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 (hspace != null) {
            results.append(" hspace=\"");
            results.append(hspace);
            results.append("\"");
        }
        
        if (vspace != null) {
            results.append(" vspace=\"");
            results.append(vspace);
            results.append("\"");
        }
        
        if (ismap != null) {
            results.append(" ismap=\"");
            results.append(ismap);
            results.append("\"");
        }
        
        if (usemap != null) {
            results.append(" usemap=\"");
            results.append(usemap);
            results.append("\"");
        }
        
        results.append(prepareStyles());
        results.append(prepareEventHandlers());
        results.append(getElementClose());

        try {
	        JspWriter writer = pageContext.getOut();
	        writer.write(results.toString());
        } catch (Exception e) {
        	throw new JspException(e);
		}

        return EVAL_PAGE;
    }

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