import java.net.*;
import java.io.*;
import java.util.*;
import Html;

/**
 *  This type represents a question from an faq style chunk of
 *  text. The question does not include the answer
 *
 *  @author http://bumble.sf.net
 *  @See Answer, FAQ, etc
 */
 
  
public class Question extends Object
{
  //--------------------------------------------
  private static String NEWLINE = System.getProperty("line.separator");
  //--------------------------------------------
  /** the text of the question */
  private String contents;  
  //--------------------------------------------
  public static String TERMINATOR = "?";
  //--------------------------------------------
  public static String INDICATOR = "q:";
  //--------------------------------------------
  private boolean isGood;
  //--------------------------------------------
  

  //--------------------------------------------
  public Question()
  {
    this.contents = "";  
  }

  //--------------------------------------------
  /** construct with some text */
  public Question(String sText)
  {
    this();
    this.loadData(sText);
  } 


  //--------------------------------------------
  /** load data from text */
  public void loadData(String sText)
  {
    String sTextTrimmed;

    this.isGood = this.isQuestion(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 Question */
  public static boolean isQuestion(String sText)
  {
    if (sText.trim().length() == 0)
     { return false; }

    if (!sText.trim().startsWith(Question.INDICATOR))
      { return false; }

    if (!sText.trim().endsWith(Question.TERMINATOR))
      { return false; }

    return true;
  }

  //--------------------------------------------
  public String toString()
  {
    StringBuffer sbReturn = new StringBuffer("");
    sbReturn.append(Question.INDICATOR);
    sbReturn.append(this.contents);
    sbReturn.append(Question.TERMINATOR);
    return sbReturn.toString();
  }

  //--------------------------------------------
  public String printReport()
  {
    StringBuffer sbReturn = new StringBuffer("");
    sbReturn.append("Question Indicator :");
    sbReturn.append(Question.INDICATOR);
    sbReturn.append(NEWLINE);
    sbReturn.append("Question Terminator:");
    sbReturn.append(Question.TERMINATOR);
    sbReturn.append(NEWLINE);
    sbReturn.append("Question contents:");
    sbReturn.append(this.contents);
    sbReturn.append(NEWLINE);
    return sbReturn.toString();
  }

  //--------------------------------------------
  public String printHtml()
  {
    StringBuffer sbReturn = new StringBuffer("");
    sbReturn.append("<dt class='faq-question'><em>");
    //sbReturn.append(Question.INDICATOR);
    sbReturn.append(Html.encode(this.contents.trim()));
    sbReturn.append(Question.TERMINATOR);
    sbReturn.append("</em></dt>");
    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 Question text");
    sbUsageMessage.append(NEWLINE);
    sbUsageMessage.append(" eg  java Question \"q: why java ?\" ");
    sbUsageMessage.append(NEWLINE);

    StringBuffer sbMessage = new StringBuffer("");


    if (args.length == 0)
    {	    
      System.out.println(sbUsageMessage);
      System.exit(-1);
    }

    /* -------------------
    BufferedReader brInput =
      new BufferedReader(new InputStreamReader(System.in));
    int iInput = 0;
    char cInput = ' ';

    StringBuffer sbInput = new StringBuffer("");

    System.out.println("Testing the Question Class");
    System.out.println("Type * to finish");
    while (cInput != '*')
    {
      sbInput.append(cInput);
      cInput = (char)brInput.read();
    }

    Question rTest = new Question(sbInput.toString());
    ----------------------- */

    Question rTest = new Question(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()
  
} //-- Question class
