
import java.util.*;


//import Html;
//import MixedText;
//import BlankText;

/**
 *  This is one item from a list. The list does not have to
 *  be about any particular subject
 *
 *  @author http://bumble.sf.net
 *  @See 
 */
 
  
public class ListItem 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 = INDICATOR;
  //--------------------------------------------
  private boolean isGood;
  //--------------------------------------------
  

  //--------------------------------------------
  public ListItem()
  {
    this.contents = "";  
  }

  //--------------------------------------------
  public ListItem(String sText)
  {
    this();
    this.loadData(sText);
  } 


  //--------------------------------------------
  /** load data from text */
  public void loadData(String sText)
  {

    this.isGood = this.isListItem(sText);
    if (!this.isGood)
    {
      return;
    }

    this.contents = sText.substring(
       this.INDICATOR.length(),
       sText.length() - this.INDICATOR.length()).trim();

  } //-- method: loadData

  //--------------------------------------------
  public String getContents()
  {
    return this.contents;
  }

  //--------------------------------------------
  public void setContents(String sContents)
  {
    this.contents = sContents;
  }

  //--------------------------------------------
  /** recognizes the beginning of a ListItem at the end
   *  of another string. This needs to be static because
   *  it may be called by a parser before any object is created
   **/
  public static boolean recognize(String sContents)
  {
    if (sContents.endsWith(ListItem.INDICATOR))
     { return true; }

    return false;
  }

  //--------------------------------------------
  /** since the list item uses the start token of the
   *  next item as its end token, it needs to return this
   *  to the parser. This should not be a static method */
  public static String restoreToken()
  {
    //-- at the end of the list this will be different
    //--
    return ListItem.INDICATOR;    
  }

  //--------------------------------------------
  /** checks if this looks like a list item */
  public static boolean isListItem(String sText)
  {
    if (sText.trim().length() == 0)
     { return false; }

    if (!sText.startsWith(ListItem.INDICATOR))
      { return false; }

    if (sText.equals(ListItem.INDICATOR))
      { return false; }

    if (!sText.endsWith(ListItem.TERMINATOR) &&
        !BlankText.lastLineIsBlank(sText))
      { return false; }

    return true;
  }

  //--------------------------------------------
  /** returns the list item to a canonical text form */
  public String toString()
  {
    StringBuffer sbReturn = new StringBuffer("");
    sbReturn.append(ListItem.INDICATOR);
    sbReturn.append(this.contents);
    return sbReturn.toString();
  }

  //--------------------------------------------
  public String printReport()
  {
    StringBuffer sbReturn = new StringBuffer("");
    sbReturn.append("ListItem Indicator >");
    sbReturn.append(ListItem.INDICATOR);
    sbReturn.append(NEWLINE);
    sbReturn.append("ListItem Terminator>");
    sbReturn.append(ListItem.TERMINATOR);
    sbReturn.append(NEWLINE);
    sbReturn.append("ListItem 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()
  {
    StringBuffer sbReturn = new StringBuffer("");
    sbReturn.append("<li>");
    sbReturn.append(Html.encode(this.contents.trim()));
    sbReturn.append("</li>");
    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 ListItem text");
    sbUsageMessage.append(NEWLINE);
    sbUsageMessage.append(" eg  java ListItem ");
    sbUsageMessage.append("\"");
    sbUsageMessage.append(ListItem.INDICATOR);
    sbUsageMessage.append("go to shops");
    sbUsageMessage.append(ListItem.TERMINATOR);
    sbUsageMessage.append("\"");
    sbUsageMessage.append(NEWLINE);

    StringBuffer sbMessage = new StringBuffer("");


    if (args.length == 0)
    {	    
      System.out.println(sbUsageMessage);
      System.exit(-1);
    }

    String sTestItem =
     " - point one " + NEWLINE +
     " - ";

    ListItem item = new ListItem(args[0]);

    //System.out.println("Using data");
    System.out.println(".printHtml()");
    System.out.println(item.printHtml());
    System.out.println(".toString()");
    System.out.println(item.toString());
    System.out.println(".printReport()");
    System.out.println(item.printReport());

 
  } //-- main()
  
} //-- ListItem class
