/*
 * 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-2004 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.ui.jsp;

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

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

  private String type = null;
  private int real_type = -1;

  /*--------------------------------------------------------------------------------
   * Overrides from class BodyTagSupport
   *--------------------------------------------------------------------------------
   */

  public int doStartTag() throws JspException
  {
    if (type==null)
      throw new JspTagException("<util:href/> type= attribute not specified");
    HTMLRendering html = (HTMLRendering)(getRequestInput().queryService(HTMLRendering.class));
    real_type = html.convertLinkType(type);
    if (real_type<0)
      throw new JspTagException("<util:href/> type= attribute not a valid link type");
    return EVAL_BODY_BUFFERED;

  } // end doStartTag

  public int doAfterBody() throws JspException
  {
    UtilMenuHeaderItemTag menu_hdr =
        (UtilMenuHeaderItemTag)findAncestorWithClass(this,UtilMenuHeaderItemTag.class);
    if (menu_hdr!=null)
    { // set the link data
      menu_hdr.setLink(getBodyContent().getString().trim(),real_type);
      return SKIP_BODY;

    } // end if

    UtilXLinkTag xlink = (UtilXLinkTag)findAncestorWithClass(this,UtilXLinkTag.class);
    if (xlink!=null)
    { // set the link data
      xlink.setLink(getBodyContent().getString().trim(),real_type);
      return SKIP_BODY;

    } // end if

    throw new JspTagException("<util:href/> tag not nested inside a compatible tag!");

  } // end doAfterBody

  public void release()
  {
    super.release();
    type = null;
    real_type = -1;

  } // end release

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

  public void setType(String s)
  {
    this.type = s;

  } // end setType

} // end class UtilHrefTag
