/*
 * $Id: FileTag.java,v 1.1 2005/02/16 00:23:17 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: FileTag.java,v $
 * Revision 1.1  2005/02/16 00:23:17  marbobo
 * + Initiale Version
 *
 */

package vkc.view.tag;

import javax.servlet.jsp.JspException;

import org.apache.struts.taglib.html.BaseFieldTag;
import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.ResponseUtils;

import vkc.util.VKCStringUtils;

public class FileTag extends BaseFieldTag {
    /**
     * Construct a new instance of this tag.
     */
    public FileTag() {
    	super();
    	this.type = "file";
    }

    // ------------------------------------------------------------- Properties
    /**
     * Generate the required input tag, followed by the optional rendered text.
     * Support for <code>write</code> property since Struts 1.1.
     *
     * @exception JspException if a JSP exception has occurred
     */
    public int doStartTag() throws JspException {
        ResponseUtils.write( this.pageContext, this.renderInputElement() );
        
        return EVAL_PAGE;
    }

    /**
     * Renders a fully formed &lt;input&gt; element.
     * @throws JspException
     * @since Struts 1.2
     */
    protected String renderInputElement() throws JspException {
        StringBuffer results = new StringBuffer( "<input type=\"" );
        results.append( this.type );
        results.append( "\" name=\"" );

        if ( indexed ) {
            this.prepareIndex( results, name );
        }

        results.append( property );
        results.append( "\"" );
        
        if ( accesskey != null ) {
            results.append( " accesskey=\"" );
            results.append( accesskey );
            results.append( "\"" );
        }

        if ( accept != null ) {
            results.append( " accept=\"" );
            results.append( accept );
            results.append( "\"" );
        }

        if ( maxlength != null ) {
            results.append( " maxlength=\"" );
            results.append( maxlength );
            results.append( "\"" );
        }

        if ( cols != null ) {
            results.append( " size=\"" );
            results.append( cols );
            results.append( "\"" );
        }

        if ( tabindex != null ) {
            results.append( " tabindex=\"" );
            results.append( tabindex );
            results.append( "\"" );
        }

        results.append(" value=\"");
        
        if ( value != null ) {
            results.append( this.formatValue( value ) );

        } else if ( redisplay || !"password".equals( type ) ) {
            Object value =
                RequestUtils.lookup( pageContext, name, property, null );

            results.append( this.formatValue( value ) );
        }

        results.append( '"' );
        results.append( this.prepareEventHandlers() );
        results.append( this.prepareStyles() );
        results.append( this.getElementClose() );

        return results.toString();
    }
    

    /**
     * Return the given value as a formatted <code>String</code>.  This 
     * implementation escapes potentially harmful HTML characters.
     *
     * @param value The value to be formatted. <code>null</code> values will
     * be returned as the empty String "".
     * 
     * @throws JspException if a JSP exception has occurred
     * 
     * @since Struts 1.2
     */
    protected String formatValue( Object value ) {
        if ( value == null ) {
            return "";
        }

        return VKCStringUtils.filterUnicodeChars( value.toString() );
    }
}