import java.io.*;
import java.util.*;

//import BufferLoad;
//import TextLink;
//import PlainText;
//import EndText;
//import DocumentElement;
//import PlainList;


/**
 *  This class represents some text which contains linked
 *  text as well as ordinary text.
 *
 *  @see PlainText, TextLink
 *  @author http://bumble.sf.net
 */
 
  
public class MixedText extends Object
{
  //--------------------------------------------
  private static String NEWLINE = System.getProperty("line.separator");
  //--------------------------------------------
  private boolean isGood;
  //--------------------------------------------
  private String errors;
  //--------------------------------------------
  private Vector textElements;
  //--------------------------------------------

  public MixedText()
  {
    this.errors = "";
    this.errors = "";
    this.textElements = new Vector();
  } 

  //--------------------------------------------
  public MixedText(String sText)
  {
    this();
    this.loadData(sText);
  } 

  //--------------------------------------------
  public int countElements()
  {
    return this.textElements.size();
  }

  //--------------------------------------------
  public void loadData(String sText)
  {

    this.isGood = this.isMixedText(sText);
    if (!this.isGood)
    {
      return;
    }

    StringReader srText = new StringReader(sText);
    String sTextLinkStart = "";
    String sContent = "";
    String sToken = "";

    int iCurrentCharacter = 0;
    StringBuffer sbText = new StringBuffer("");
    
    while (iCurrentCharacter != -1)
    {
      sbText.append((char)iCurrentCharacter);
      //-- System.out.println("sbText=" + sbText);
      sTextLinkStart = TextLink.recognize(sbText.toString());

      if (!sTextLinkStart.equals(""))
      {
        //-- remove the link suffix
        sContent = EndText.removeSuffix(
          sbText.toString(), sTextLinkStart);
        this.textElements
          .addElement(new PlainText(sContent));

        sbText.setLength(0);
        sbText.append(sTextLinkStart);
      }


      if (TextLink.isTextLink(sbText.toString()))
      {
        //-- System.out.println("<found link text>");
        this.textElements
         .addElement(new TextLink(sbText.toString()));
        sbText.setLength(0);
      } 

      if (PlainList.recognize(sbText.toString()))
      {
        sToken = PlainList.returnToken(sbText.toString());

        sContent = EndText.removeSuffix(
          sbText.toString(), sToken);

        this.textElements
          .addElement(new PlainText(sContent));

        sbText.setLength(0);
        sbText.append(sToken);
      }

      if (PlainList.isPlainList(sbText.toString()))
      {
        //-- System.out.println("<found list>");
        this.textElements
          .addElement(new PlainList(sbText.toString()));
        sbText.setLength(0);
      }

      try
      {
        iCurrentCharacter = srText.read();
      }
      catch (IOException e)
      {
        this.errors =
          "An IO Exception occurred while reading " + e;
        return;
      }
    } //-- while more characters

    if (TextLink.isTextLink(sbText.toString()))
    {
      //-- System.out.println("<found link text>");
      this.textElements
       .addElement(new TextLink(sbText.toString()));
      sbText.setLength(0);
    }
    else if (PlainList.isPlainList(sbText.toString()))
    {
      //-- System.out.println("<found list>");
      this.textElements
       .addElement(new PlainList(sbText.toString()));
      sbText.setLength(0);
    }
    else
    {
      //-- System.out.println("<found plain text>");
      this.textElements
       .addElement(new PlainText(sbText.toString()));
      sbText.setLength(0);
    }

    srText.close();

  } //-- m: loadData()

  //--------------------------------------------
  /** mixed text does not really have any structure */
  public static boolean isMixedText(String sText)
  {
    return true;
  }

  //--------------------------------------------
  public String toString()
  {
    StringBuffer sbReturn = new StringBuffer("");
    DocumentElement deCurrent;
    Enumeration ee = this.textElements.elements();
    while (ee.hasMoreElements())
    {
      deCurrent = (DocumentElement)ee.nextElement();
      sbReturn.append(deCurrent.toString());
    }

    sbReturn.append(NEWLINE);

    return sbReturn.toString();
  }

  //--------------------------------------------
  public String printHtml()
  {

    StringBuffer sbReturn = new StringBuffer("");

    DocumentElement deCurrent;
    Enumeration ee = this.textElements.elements();
    while (ee.hasMoreElements())
    {
      deCurrent = (DocumentElement)ee.nextElement();
      sbReturn.append(deCurrent.printHtml());
    }

    return sbReturn.toString();
  }

  //--------------------------------------------
  public String printReport()
  {
    StringBuffer sbReturn = new StringBuffer("");
    sbReturn.append("");
    sbReturn.append(NEWLINE);
    sbReturn.append("Number of elements>");
    sbReturn.append(this.countElements());
    sbReturn.append(NEWLINE);
    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 MixedText text-file [show]");
    sbUsageMessage.append(NEWLINE);
    sbUsageMessage.append("");
    sbUsageMessage.append(NEWLINE);

    if (args.length == 0)
    {	    
      System.out.println(sbUsageMessage);
      System.exit(-1);
    }


    StringBuffer sbTest = new StringBuffer("");


    //-- show all data, not just the html.
    if (args.length == 2)
    {	    
      System.out.println("Using data");
      System.out.println(BufferLoad.loadBuffer(args[0]));
    }

    MixedText document = new MixedText(BufferLoad.loadBuffer(args[0]));

    if (args.length == 2)
    {	    
      System.out.println(".toString()");
      System.out.println(document.toString());
      System.out.println(".printReport()");
      System.out.println(document.printReport());
    }

    System.out.println(".printHtml()");
    System.out.println(document.printHtml());

  } //-- main()
  
} //-- FAQ class






