/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.ui.jsp;

import java.io.IOException;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import com.silverwrist.venice.ui.*;
import com.silverwrist.venice.ui.helpers.HTMLRendering;

public class UtilImageTag extends VeniceBodyTagSupport
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private String src = null;
  private String fixup = null;
  private String alt = "";
  private String width = null;
  private String height = null;
  private String border = "0";
  private String other_alt = null;
  private String align = null;
  private String hspace = null;
  private String vspace = null;
  private String usemap = null;

  /*--------------------------------------------------------------------------------
   * Internal functions
   *--------------------------------------------------------------------------------
   */

  private static int convertParam(String param, String name) throws JspTagException
  {
    if (param==null)
      throw new JspTagException("<util:image/> " + name + "= attribute not specified");

    try
    { // convert the parameter to an integer
      int rc = Integer.parseInt(param);
      if (rc<0)
	throw new JspTagException("<util:image/> " + name + "= attribute out of range");
      return rc;

    } // end try
    catch (NumberFormatException nfe)
    { // there's a problem
      throw new JspTagException("<util:image/> " + name + "= attribute not a proper integer");

    } // end catch

  } // end convertParam

  /*--------------------------------------------------------------------------------
   * Overrides from class TagSupport
   *--------------------------------------------------------------------------------
   */

  public int doStartTag() throws JspException
  {
    if (src==null)
      throw new JspTagException("<util:image/> src= attribute not specified");

    return EVAL_BODY_TAG;;

  } // end doStartTag

  public int doAfterBody()
  {
    return SKIP_BODY;

  } // end doAfterBody

  public int doEndTag() throws JspException
  {
    int real_width = convertParam(width,"width");
    int real_height = convertParam(height,"height");
    int real_border = convertParam(border,"border");

    String real_src;
    if (fixup!=null)
    { // fix up the image path
      HTMLRendering html = (HTMLRendering)(getRequestOutput().queryService(HTMLRendering.class));
      real_src = html.getImagePath(src);

    } // end if
    else
      real_src = src;

    try
    { // write out what we came here to accomplish
      JspWriter out = pageContext.getOut();
      out.write("<IMG SRC=\"" + real_src + "\" ALT=\"");
      if (other_alt==null)
	out.write(alt);
      else
	out.write(other_alt);
      out.write("\" WIDTH=" + real_width + " HEIGHT=" + real_height + " BORDER=" + real_border);
      if (align!=null)
	out.write(" ALIGN=" + align);
      if (hspace!=null)
	out.write(" HSPACE=" + hspace);
      if (vspace!=null)
	out.write(" VSPACE=" + vspace);
      if (usemap!=null)
	out.write(" USEMAP=\"" + usemap + "\"");
      out.write(">");

    } // end try
    catch (IOException e)
    { // convert the I/O error into something the servlet engine can deal with
      throw new JspTagException("error writing <IMG> tag - " + e.getMessage());

    } // end catch

    return EVAL_PAGE;

  } // end doEndTag

  public void release()
  {
    super.release();
    src = null;
    fixup = null;
    alt = "";
    width = null;
    height = null;
    border = "0";
    other_alt = null;
    align = null;
    hspace = null;
    vspace = null;
    usemap = null;

  } // end release

  /*--------------------------------------------------------------------------------
   * Attribute set functions
   *--------------------------------------------------------------------------------
   */

  public void setSrc(String s)
  {
    src = s;

  } // end setSrc

  public void setFixup(String s)
  {
    fixup = s;

  } // end setFixup

  public void setAlt(String s)
  {
    alt = s;

  } // end setAlt

  public void setWidth(String s)
  {
    width = s;

  } // end setWidth

  public void setHeight(String s)
  {
    height = s;

  } // end setHeight

  public void setBorder(String s)
  {
    border = s;

  } // end setBorder

  public void setAlign(String s)
  {
    align = s;

  } // end setAlign

  public void setHspace(String s)
  {
    hspace = s;

  } // end setHspace

  public void setVspace(String s)
  {
    vspace = s;

  } // end setVspace

  public void setUsemap(String s)
  {
    usemap = s;

  } // end setUsemap

  /*--------------------------------------------------------------------------------
   * External operations
   *--------------------------------------------------------------------------------
   */

  final void setOtherAlt(String s)
  {
    other_alt = s;

  } // end setOtherAlt

} // end class UtilImageTag
