import java.net.*;
import java.io.*;
import java.util.*;

//-- can only import packages in jdk 1.4
//import Html;
//import TextTool;
//import QuoteText;
//import EndText;
//import DocumentElement;

/**
 *
 *  This class is a piece of text which contains as one
 *  of its dimensions data which may be interpreted as
 *  a locator of some other resource. 
 *
 *  @author http://bumble.sf.net
 *  @See Answer, FAQ, etc
 */
 
  
public class TextLink extends Object implements DocumentElement
{
  //--------------------------------------------
  private static String NEWLINE = System.getProperty("line.separator");
  //--------------------------------------------
  /**  */
  private String text;  
  //--------------------------------------------
  private String pointer;  
  //--------------------------------------------
  public static String TERMINATOR = "";
  //--------------------------------------------
  public static String[] protocols =
      {"http://", "https://", "ftp://", "link://", "www."};
  //--------------------------------------------
  private boolean isGood;
  //--------------------------------------------
  private String returnToken;

  //--------------------------------------------
  public TextLink()
  {
    this.text = "";
    this.pointer = "";
    this.returnToken = "";

  }

  //--------------------------------------------
  /** construct with some text */
  public TextLink(String sText)
  {
    this();
    this.loadData(sText);
  } 


  //--------------------------------------------
  /** load data from text */
  public void loadData(String sText)
  {
    String sTextTrimmed;
    String sTarget;

    this.isGood = this.isTextLink(sText);
    if (!this.isGood)
    {
      return;
    }

    sTextTrimmed = sText.trim();

    String[] ssLink = TextTool.splitOnFirstSpace(sText);
    sTarget = ssLink[0];

    //--
    if (sTarget.toLowerCase().startsWith("www."))
    {
      this.pointer = "http://" + sTarget;
    }
    else if (sTarget.toLowerCase().startsWith("link://"))
    {
      this.pointer = EndText.removePrefix(sTarget, "link://");
    }
    else
    {
      this.pointer = sTarget;
    }


    if (ssLink.length == 1)
    {
      this.text = this.pointer;
    }
    else if (ssLink.length == 2)
    {
      this.text = QuoteText.popQuotes(ssLink[1]);
    }

  } //-- method: loadData

  //--------------------------------------------
  /** this method sees if the end of another string looks
   *  like the start of a TextLink. this might be called
   *  by a parsing routine */
  public static String recognize(String sText)
  {
    return EndText.getSuffix(sText, TextLink.protocols);
  }


  //--------------------------------------------
  public String getText()
  {
    return this.text;
  }

  //--------------------------------------------
  public void setText(String sText)
  {
    this.text = sText;
  }

  //--------------------------------------------
  /** checks if this looks like a */
  public static boolean isTextLink(String sText)
  {

    if (sText.trim().length() == 0)
     { return false; }

    if (!TextTool.startsWithIgnoreCase(sText.trim(), TextLink.protocols))
    {
      return false;
    }

    String[] ssSections = TextTool.splitOnFirstSpace(sText);

    if (ssSections.length == 1)
    {
      if (!sText.endsWith(" "))
      {
        return false;
      }
    }
    else if (ssSections.length == 2)
    {
      if (!QuoteText.isQuoted(ssSections[1]))
      {
        return false;
      }

    }

    return true;
  } //-- method: isTextLink


  //--------------------------------------------
  public String toString()
  {
    StringBuffer sbReturn = new StringBuffer("");
    sbReturn.append(this.pointer);
    sbReturn.append(" ");

    if (!this.pointer.equals(this.text))
    {
      sbReturn.append("'" + this.text + "' ");
    }
    return sbReturn.toString();
  }

  //--------------------------------------------
  public String printReport()
  {
    StringBuffer sbReturn = new StringBuffer("");
    sbReturn.append("Text of the link>");
    sbReturn.append(this.text);
    sbReturn.append(NEWLINE);
    sbReturn.append("Link points to  >");
    sbReturn.append(this.pointer);
    sbReturn.append(NEWLINE);
    sbReturn.append("Last data ok    >");
    sbReturn.append(this.isGood);
    sbReturn.append(NEWLINE);
    return sbReturn.toString();
  }

  //--------------------------------------------
  public String printHtml()
  {
    StringBuffer sbReturn = new StringBuffer("");
    sbReturn.append("<a href='");
    sbReturn.append(Html.encode(this.pointer.trim()));
    sbReturn.append("'>");
    sbReturn.append(Html.encode(this.text.trim()));
    sbReturn.append("</a> ");
    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 TextLink text");
    sbUsageMessage.append(NEWLINE);
    sbUsageMessage.append("eg: java TextLink \"http://bumble.sf.net 'here'\" ");
    sbUsageMessage.append(NEWLINE);

    StringBuffer sbMessage = new StringBuffer("");


    if (args.length == 0)
    {	    
      System.out.println(sbUsageMessage);
      System.exit(-1);
    }


    TextLink linkTest = new TextLink(args[0]);

    System.out.println(".printHtml()");
    System.out.println(linkTest.printHtml());
    System.out.println(".toString()");
    System.out.println(linkTest.toString());
    System.out.println(".printReport()");
    System.out.println(linkTest.printReport());

 
  } //-- main()
  
} //-- Question class
