import java.io.*;
import java.util.*;

//import Question;
//import Answer;

/**
 *  This class represents one question and answer pair
 *  from a set of questions and answers. Possibly there
 *  will not actually be an answer.
 *
 *  This appears to be working under jvm1.1 
 *  
 *  @author matth3wbishop<at>yahoo!<dot>com
 *  @see FAQ, FaqDocument, Answer ...
 */
 
  
public class FaqItem extends Object
{
  //--------------------------------------------
  private static String NEWLINE = System.getProperty("line.separator");
  //--------------------------------------------
  private boolean isGood;
  //--------------------------------------------
  private String errors;
  //--------------------------------------------
  private Answer answer;
  //--------------------------------------------
  private Question question;
  //--------------------------------------------
  public static String INDICATOR = Question.INDICATOR;
  //--------------------------------------------
  public static String TERMINATOR = Question.INDICATOR;

  //--------------------------------------------
  public FaqItem()
  {
    this.errors = "";
    this.question = new Question();
    this.answer = new Answer();
  } 

  //--------------------------------------------
  public FaqItem(String sText)
  {
    this();
    this.loadData(sText);
  } 

  //--------------------------------------------
  public Question getQuestion()
  {
    return this.question;
  }

  //--------------------------------------------
  public Answer getAnswer()
  {
    return this.answer;
  }

  //--------------------------------------------
  public void setAnswer(String sAnswerText)
  {
    this.answer = new Answer(sAnswerText);
  }

  //--------------------------------------------
  public void loadData(String sText)
  {
    StringReader srText = new StringReader(sText);

    this.isGood = this.isFaqItem(sText);
    if (!this.isGood)
    {
      return;
    }

    int iCurrentCharacter = 0;
    StringBuffer sbText = new StringBuffer("");
    
    while (iCurrentCharacter != -1)
    {
      sbText.append((char)iCurrentCharacter);
      //System.out.println("sbText=" + sbText);

      if (Question.isQuestion(sbText.toString()))
      {
        this.question = new Question(sbText.toString());
        if (sText.trim().endsWith(Question.INDICATOR))
        {
          this.answer = new Answer(sText.trim().substring(
           sbText.toString().length(),
           sText.trim().length() - Question.INDICATOR.length()));
        }
        else
        {
          this.answer = new Answer(sText.trim().substring(
           sbText.toString().length(),
           sText.trim().length() - FAQ.TERMINATOR.length()));
        }

        sbText.setLength(0);
        return;
      }

      try
      {
        iCurrentCharacter = srText.read();
      }
      catch (IOException e)
      {
        this.errors =
          "An IO Exception occurred while reading " + e;
        return;
      }
    } //-- while

    srText.close();
  } //-- m: loadData()

  //--------------------------------------------
  /** checks if this looks like an faqitem */
  public static boolean isFaqItem(String sText)
  {
    if (sText.trim().length() == 0)
     { return false; }

    if (!sText.trim().startsWith(Question.INDICATOR))
      { return false; }

    if (sText.trim().equals(Question.INDICATOR))
      { return false; }

    if (!sText.trim().endsWith(Question.INDICATOR) &&
        !sText.trim().endsWith(FAQ.TERMINATOR))
      { return false; }

    return true;
  }
  
  //--------------------------------------------
  /** checks if there is an anwer */
  public boolean hasAnswer()
  {
    if (this.answer == null)
     { return false; }

    /*
    if (this.answer.getContents().trim().length() == 0)
     { return false; }
    */
    return true;
  }

  //--------------------------------------------
  public String toString()
  {
    StringBuffer sbReturn = new StringBuffer("");
    sbReturn.append(this.question.toString());
    sbReturn.append(NEWLINE);
    sbReturn.append(this.answer.toString());
    sbReturn.append(NEWLINE);
    return sbReturn.toString();
  }

  //--------------------------------------------
  public String printHtml()
  {
    StringBuffer sbReturn = new StringBuffer("");
    sbReturn.append(this.question.printHtml());
    sbReturn.append(NEWLINE);
    sbReturn.append(this.answer.printHtml());
    return sbReturn.toString();
  }
  //--------------------------------------------
  public String printReport()
  {
    StringBuffer sbReturn = new StringBuffer("");
    sbReturn.append("");
    sbReturn.append(NEWLINE);
    sbReturn.append("Question :");
    sbReturn.append(this.question.getContents());
    sbReturn.append(NEWLINE);
    sbReturn.append("Answer :");
    sbReturn.append(this.answer.getContents());
    sbReturn.append(NEWLINE);
    sbReturn.append("start token :");
    sbReturn.append(this.INDICATOR);
    sbReturn.append(NEWLINE);
    sbReturn.append("end token   :");
    sbReturn.append(this.TERMINATOR);
    sbReturn.append(NEWLINE);
    sbReturn.append("Errors :");
    sbReturn.append(this.errors);
    sbReturn.append(NEWLINE);
    return sbReturn.toString();
  }

  //--------------------------------------------
  public String print()
  {
    StringBuffer sbReturn = new StringBuffer("");
    sbReturn.append("");
    return sbReturn.toString();
  }


  //--------------------------------------------
  /** a main method for testing */
  public static void main(String[] args) throws Exception
  {

    StringBuffer sbUsageMessage = new StringBuffer("");
    sbUsageMessage.append("test usage: java FaqItem .");
    sbUsageMessage.append(NEWLINE);
    sbUsageMessage.append(" tests from standard in");
    sbUsageMessage.append(NEWLINE);

    if (args.length == 0)
    {	    
      System.out.println(sbUsageMessage);
      System.exit(-1);
    }

    StringBuffer sbTest = new StringBuffer("");

    sbTest.append(" q: what is an interface ?");
    sbTest.append(NEWLINE);
    sbTest.append(" an interface is a type");
    sbTest.append(NEWLINE);
    sbTest.append(" q:");
    sbTest.append(NEWLINE);

    System.out.println("Using data:");
    System.out.println(NEWLINE);
    System.out.println(sbTest);

    FaqItem rTest = new FaqItem(sbTest.toString());


    System.out.println(rTest.printHtml());
    System.out.println(NEWLINE);
    System.out.println(rTest.toString());
    System.out.println(NEWLINE);
    System.out.println(rTest.printReport());

  } //-- main()
  
} //-- FaqItem class






