import java.net.*;
import java.io.*;
import java.util.*;
import Html;

/**
 *  This type represents a title of a document or of something else.
 *
 *  @author http://bumble.sf.net
 *  @See Answer, FAQ, etc
 */
 
  
public class Title extends Object
{
  //--------------------------------------------
  private static String NEWLINE = System.getProperty("line.separator");
  //--------------------------------------------
  /** the text of the question */
  private String contents;  
  //--------------------------------------------
  public static String INDICATOR = "**";
  //--------------------------------------------
  public static String TERMINATOR = "**";
  //--------------------------------------------
  private boolean isGood;
  //--------------------------------------------
  

  //--------------------------------------------
  public Title()
  {
    this.contents = "";  
  }

  //--------------------------------------------
  /** construct with some text */
  public Title(String sText)
  {
    this();
    this.loadData(sText);
  } 


  //--------------------------------------------
  /** load data from text */
  public void loadData(String sText)
  {
    String sTextTrimmed;

    this.isGood = this.isTitle(sText);
    if (!this.isGood)
    {
      return;
    }

    sTextTrimmed = sText.trim();

    this.contents = sTextTrimmed.substring(
       this.INDICATOR.length(),
       sTextTrimmed.length() - this.TERMINATOR.length());

  } //-- method: loadData

  //--------------------------------------------
  public String getContents()
  {
    return this.contents;
  }

  //--------------------------------------------
  public void setContents(String sContents)
  {
    this.contents = sContents;
  }

  //--------------------------------------------
  /** checks if this looks like a Title */
  public static boolean isTitle(String sText)
  {
    if (sText.trim().length() == 0)
     { return false; }

    if (!sText.trim().startsWith(Title.INDICATOR))
      { return false; }

    //-- there must be more than just the start token
    //--
    if (sText.trim().equalsIgnoreCase(Title.INDICATOR))
      { return false; }

    if (!sText.trim().endsWith(Title.TERMINATOR))
      { return false; }

    return true;
  }

  //--------------------------------------------
  public String toString()
  {
    if (this.contents.trim().equals(""))
    {
      return "";
    }

    StringBuffer sbReturn = new StringBuffer("");
    sbReturn.append(Title.INDICATOR);
    sbReturn.append(this.contents);
    sbReturn.append(Title.TERMINATOR);
    return sbReturn.toString();
  }

  //--------------------------------------------
  public String printReport()
  {
    StringBuffer sbReturn = new StringBuffer("");
    sbReturn.append("Title Indicator  >");
    sbReturn.append(Title.INDICATOR);
    sbReturn.append(NEWLINE);
    sbReturn.append("Title Terminator >");
    sbReturn.append(Title.TERMINATOR);
    sbReturn.append(NEWLINE);
    sbReturn.append("contents         >");
    sbReturn.append(this.contents);
    sbReturn.append(NEWLINE);
    sbReturn.append("Last data was ok >");
    sbReturn.append(this.isGood);
    sbReturn.append(NEWLINE);
    return sbReturn.toString();
  }

  //--------------------------------------------
  public String printHtml()
  {
    if (this.contents.trim().equals(""))
    {
      return "";
    }

    StringBuffer sbReturn = new StringBuffer("");
    sbReturn.append("<h2>");
    sbReturn.append(Html.encode(this.contents.trim()));
    sbReturn.append("</h2>");
    return sbReturn.toString();
  }

  //--------------------------------------------
  public String print()
  {
    StringBuffer sbReturn = new StringBuffer("");
    return this.toString();
  }

  //--------------------------------------------
  /** a main method for testing */
  public static void main(String[] args) throws Exception
  {
    
    StringBuffer sbUsageMessage = new StringBuffer("");
    sbUsageMessage.append("test usage: java Title text");
    sbUsageMessage.append(NEWLINE);
    sbUsageMessage.append(" eg  java Title \""); 
    sbUsageMessage.append(Title.INDICATOR);
    sbUsageMessage.append(" Title Text \"");
    sbUsageMessage.append(Title.TERMINATOR);
    sbUsageMessage.append(NEWLINE);

    StringBuffer sbMessage = new StringBuffer("");


    if (args.length == 0)
    {	    
      System.out.println(sbUsageMessage);
      System.exit(-1);
    }

    Title rTest = new Title(args[0]);

    System.out.println(rTest.printHtml());
    System.out.println(NEWLINE);
    System.out.println(rTest.toString());
    System.out.println(NEWLINE);
    System.out.println(rTest.printReport());

 
  } //-- main()
  
} //-- Title class
