import java.net.*;
import java.io.*;
import java.util.*;

/** This class is represents a value from an attribute or text data field.
 *  In an Html context the value is the text occuring after the = sign
 *  in a tag attribute. The value may be quoted or not, and in some contexts
 *  may be 'badly quoted', that is the quotes dont match.
 *  @author matth3wbishop<at>yahoo!<dot>com 
 *  @site bumble.sf.net
 *  @See Document, Form, Reference, Link
 */
 
  
public class Value extends Object
{
  //--------------------------------------------
  /** determines if quotes can be mismatching, eg 'hello" */
  public static boolean strictQuotes = true;
  //--------------------------------------------
  /** determines if the value must be quoted */
  public static boolean mustHaveQuotes = false;
  //--------------------------------------------
  private String content;
  //--------------------------------------------
  private char quoteCharacter;
  //--------------------------------------------
  private boolean isQuoted;
  //--------------------------------------------
  private boolean isGood;
  //--------------------------------------------
  /** set to true if the quote characters dont match */
  private boolean mismatchedQuotes;
  //--------------------------------------------
  public static Character STANDARD_QUOTE = new Character('\'');
  //--------------------------------------------
  private static String NEWLINE = System.getProperty("line.separator");
  

  //--------------------------------------------
  public Value()
  {
    this.content = "";
    //this.quoteCharacter = "";
  } //-- constr: ()

  //--------------------------------------------
  /** construct with a value from text */
  public Value(String sText)
  {
    this();
    
    this.loadData(sText);
  } //-- constr: (string)


  //--------------------------------------------
  /** load data from text */
  public void loadData(String sText)
  {
    //System.out.println("Value loadData:" + sText);
    this.isGood = this.isValue(sText);
    if (!this.isGood)
    {
      this.content = "";
      return;
    }

    char cSingleQuote = '\'';
    char cDoubleQuote = '"';
    String sTextTrimmed = sText.trim();


    char cFirst = sTextTrimmed.charAt(0);
    char cLast = sTextTrimmed.charAt(sTextTrimmed.length() - 1);

    if (sTextTrimmed.length() == 1)
    {
      if (cFirst == '>')
      {
        this.content = "";
        this.isQuoted = false;
        return;
      }
      else
      {
        this.content = sTextTrimmed;
        this.isQuoted = false;
        return;
      }
    }


    if (sTextTrimmed.length() == 2)
    {
      if (Value.isQuote(cFirst))
      {
        this.content = "";
        this.isQuoted = true;
        return;
      }
      else
      {
        this.content = sTextTrimmed;
        this.isQuoted = false;
        return;
      }
    } //-- if length 2


    if (Value.isQuote(cFirst))
    {
      this.isQuoted = true;
      this.quoteCharacter = cFirst;
      this.content = sTextTrimmed.substring(1, sTextTrimmed.length() - 1);
      return;
    }

    //-- if the value is not quoted

    //char cFirst = sText.charAt(0)
    //char cLast = sText.charAt(sText.length() - 1);

    this.isQuoted = false;

    if (cLast == '>')
    {
      this.content = sTextTrimmed.substring(0, sTextTrimmed.length() - 1);
      return;
    }
    else
    {
      this.content = sTextTrimmed;
      return;
    }



  } //-- method: loadData



  //--------------------------------------------
  public String getContent()
  {
    return this.content;
  }

  //--------------------------------------------
  public void setContent(String sContent)
  {
    this.content = sContent;
  }

  //--------------------------------------------
  /** checks if the text is in a good format  */
  public static boolean isValue(String sText)
  {

    char cSingleQuote = '\'';
    char cDoubleQuote = '"';
    String sTextTrimmed = sText.trim();

    if (sTextTrimmed.length() == 0)
     { return false; }

    char cFirst = sTextTrimmed.charAt(0);
    char cLast = sTextTrimmed.charAt(sTextTrimmed.length() - 1);

    if (sTextTrimmed.length() == 1)
    {
      if (Value.isQuote(cFirst))
       { return false; }
      else if (cFirst == '>')
       { return true; }
      else
       { return true; }
    }


    if (sTextTrimmed.length() == 2)
    {
      if (Value.isQuote(cFirst))
      {
        if (cFirst == cLast)
         { return true; }
        else
         { return false; }
      }
    }

    if (Value.isQuote(cFirst))
    {
      if (cFirst == cLast)
       { return true; }
      else
       { return false; }
    }

    //-- if the value is not quoted

    cFirst = sText.charAt(0);
    cLast = sText.charAt(sText.length() - 1);

    if (Character.isWhitespace(cLast) || (cLast == '>'))
     { return true; }
    else
     { return false; }

  } //-- method: isValue

  //--------------------------------------------
  /** determines if a character is a quote, should be in a new class */
  public static boolean isQuote(char cTest)
  {
    char cSingleQuote = '\'';
    char cDoubleQuote = '"';

    if ((cTest == cSingleQuote) || (cTest == cDoubleQuote))
     { return true; }
    else
     { return false; }
  }

  //--------------------------------------------
  public String toString()
  {
    StringBuffer sbReturn = new StringBuffer("");
    sbReturn.append(Value.STANDARD_QUOTE + this.content + Value.STANDARD_QUOTE);
    return sbReturn.toString();
  }

  //--------------------------------------------
  public String toStringNoQuotes()
  {
    StringBuffer sbReturn = new StringBuffer("");
    sbReturn.append(this.content);
    return sbReturn.toString();
  }

  //--------------------------------------------
  public String print()
  {
    StringBuffer sbReturn = new StringBuffer("");
    sbReturn.append(Value.STANDARD_QUOTE + this.content + Value.STANDARD_QUOTE);
    return sbReturn.toString();
  }

  //--------------------------------------------
  public String printReport()
  {
    StringBuffer sbReturn = new StringBuffer("");
    sbReturn.append("Content of the value:" + this.content);
    sbReturn.append(NEWLINE);
    sbReturn.append("Value is in good format:" + this.isGood);
    sbReturn.append(NEWLINE);
    sbReturn.append("The value is quoted:" + this.isQuoted);
    sbReturn.append(NEWLINE);
    sbReturn.append("The Quote character is:" + this.quoteCharacter);
    sbReturn.append(NEWLINE);
    sbReturn.append("The standard quote is:" + Value.STANDARD_QUOTE);
    sbReturn.append(NEWLINE);
    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 Value [Value text]");
    sbUsageMessage.append(NEWLINE);
    sbUsageMessage.append(" eg  java Value 'hello' ");
    sbUsageMessage.append(NEWLINE);


    if (args.length == 0)
    {	    
      System.out.println(sbUsageMessage);
      System.exit(-1);
    }

    String sTest;
    //sTest = "value>";
    Value rTest = new Value(args[0]);
    StringBuffer sbMessage = new StringBuffer("");
    sbMessage.append(rTest.print());
    sbMessage.append(NEWLINE);
    sbMessage.append(rTest.printReport());
  
 
    System.out.println(sbMessage);
  } //-- main()
  
} //-- Value class
