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

import java.io.*;
import java.util.*;
import com.silverwrist.util.*;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.*;
import com.silverwrist.venice.frame.FramedContent;

class TextPageData extends PageData implements FramedContent
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private String m_title;    // title string
  private String m_body;     // body text
  private String m_qid;      // page QID

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  TextPageData(File file, String request_path) throws IOException
  {
    super(file);

    // Read in the file data.
    StringBuffer raw = IOUtils.loadText(file);

    // make the upper-case search page and use that to locate the page title and body
    String search = raw.toString().toUpperCase();
    m_title = searchBetweenTags(raw,search,"TITLE");
    m_body = searchBetweenTags(raw,search,"BODY");

    if (m_body==null)
    { // default the body if it doesn't exist
      ResourceBundle b = ResourceBundle.getBundle("com.silverwrist.venice.servlet.FrameServletMessages",
						  Locale.getDefault());
      m_body = b.getString("frame.noContent");

    } // end if

    if (m_title==null)
    { // default the title as well
      int p = request_path.lastIndexOf('/');
      if (p<0)
	m_title = request_path;
      else
	m_title = request_path.substring(p+1);

    } // end if

    // Determine the page QID.
    if (request_path.startsWith("/"))
      m_qid = request_path.substring(1);
    else
      m_qid = request_path;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Internal operations
   *--------------------------------------------------------------------------------
   */

  private static final String searchBetweenTags(StringBuffer data, String search, String tagname)
  {
    tagname = tagname.toUpperCase();
    String start = "<" + tagname;
    String end = "</" + tagname + ">";

    int startpos = search.indexOf(start);
    if (startpos<0)
      return null;
    startpos += start.length();

    int bkt_pos = search.indexOf('>',startpos);
    if (bkt_pos<0)
      return null;

    int end_pos = search.indexOf(end,++bkt_pos);
    if (end_pos<0)
      return data.substring(bkt_pos);
    else
      return data.substring(bkt_pos,end_pos);

  } // end searchBetweenTags

  /*--------------------------------------------------------------------------------
   * Abstract implementations from class PageData
   *--------------------------------------------------------------------------------
   */

  public void render(SelfRenderControl control) throws IOException, RenderingException
  {
    PrintWriter wr = control.getTextRender().getWriter();
    wr.write(m_body);
    wr.flush();

  } // end render

  /*--------------------------------------------------------------------------------
   * Implementations from interface FramedContent
   *--------------------------------------------------------------------------------
   */

  public String getMenuSelector()
  {
    return null;

  } // end getMenuSelector

  public String getPageTitle()
  {
    return m_title;

  } // end getPageTitle

  public String getPageQID()
  {
    return m_qid;

  } // end getPageQID

} // end class TextPageData
