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

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

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

  private String title = null;
  private String stocktitle = null;
  private String subtitle = null;
  private String stocksubtitle = null;

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

  public int doStartTag()
  {
    return EVAL_BODY_TAG;

  } // end UtilHeaderTag

  public int doAfterBody()
  {
    return SKIP_BODY;

  } // end doAfterBody

  public int doEndTag() throws JspException
  {
    RequestOutput ro = getRequestOutput();
    HTMLRendering html = (HTMLRendering)(ro.queryService(HTMLRendering.class));

    // find the real title string
    String real_title = null;
    if (title!=null)
      real_title = title;
    else if (stocktitle!=null)
      real_title = html.getStockMessage(stocktitle);
    if (real_title==null)
      throw new JspTagException("<util:header/> contains no title= or stocktitle= attributes!");

    // find the real subtitle string
    String real_subtitle = null;
    if (subtitle!=null)
      real_subtitle = subtitle;
    else if (stocksubtitle!=null)
      real_subtitle = html.getStockMessage(stocksubtitle);

    try
    { // write out what we came here to accomplish
      JspWriter out = pageContext.getOut();
      ro.writeContentHeader(out,real_title,real_subtitle);

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

    } // end catch

    return EVAL_PAGE;

  } // end doEndTag

  public void release()
  {
    super.release();
    title = null;
    stocktitle = null;
    subtitle = null;
    stocksubtitle = null;

  } // end release

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

  public void setTitle(String s)
  {
    title = s.trim();

  } // end setTitle

  public void setStocktitle(String s)
  {
    stocktitle = s;

  } // end setStocktitle

  public void setSubtitle(String s)
  {
    subtitle = s.trim();

  } // end setSubtitle

  public void setStocksubtitle(String s)
  {
    stocksubtitle = s;

  } // end setStocksubtitle

} // end class UtilHeaderTag
