import java.net.*;
import java.io.*;
import java.util.*;

/** This class represents the translation of some piece
 *  of text into some other language. But the class does
 *  not hold any copy of the original text. So its name is
 *  a little misleading. It should be called something like
 *  TextFragmentInLanguage .
 *
 *  Appears to be working with jvm1.1 and jview
 *
 *  @todo=   handle bad data strings
 *   */


public class Translation extends Object
{
  //--------------------------------------------
  private String translation;
  //--------------------------------------------
  /** An RFC defined 2 letter language code. Technically
   *  this probably should be an enumerated type or a Locale class */
  private String languageCode;
  //--------------------------------------------
  private boolean isGood;
  //--------------------------------------------
  /** The text indicating a translation field in the data file */
  public static String FIELD_NAME = "tr";
  //--------------------------------------------
  /** the string which separates field names from field values */
  public static String SEPARATOR = "=";
  //--------------------------------------------
  /** the string which delimites the start of the language code */
  public static String LANG_CODE_START = "[";
  //--------------------------------------------
  /** the string which delimites the end of the language code */
  public static String LANG_CODE_END = "]";
  //--------------------------------------------
  private static String NEWLINE = System.getProperty("line.separator");

  //--------------------------------------------
  /** creates a Translation with no data */
  public Translation()
  {
    this.translation = "";
    this.languageCode = "";

  } //-- constr: ()

  //--------------------------------------------
  /** creates a Translation with data provided */
  public Translation(String sTranslation, String sLanguageCode)
  {
    
    this.translation = sTranslation;
    this.languageCode = sLanguageCode;

  } //-- constr: (string, string)

  //--------------------------------------------
  /** creates Translation object from text data */
  public Translation(String sText)
  {
    this();
    this.loadData(sText);

  } //-- constr: (string)


  //--------------------------------------------
  /** an alias for isGoodFormat */
  public static boolean isTranslation(String sTextData)
  {
    return Translation.isGoodFormat(sTextData);
  } 

  //--------------------------------------------
  /** checks whether the given string represents usable data */
  public static boolean isGoodFormat(String sTextData)
  {
    if (sTextData.trim().equals(""))
     { return false; }

    boolean bHasFieldName = sTextData
     .toLowerCase().trim().startsWith(Translation.FIELD_NAME);

    if (!bHasFieldName)
     { return false; }

    boolean bHasSeparator = sTextData.toLowerCase()
     .trim().substring(Translation.FIELD_NAME.length())
     .trim().startsWith(Translation.SEPARATOR);

    if (!bHasSeparator)
     { return false; }

    String sValue = sTextData.toLowerCase()
     .trim().substring(Translation.FIELD_NAME.length())
     .trim().substring(Translation.SEPARATOR.length())
     .trim();

    if (sValue.equals(""))
     { return false; }
    else
     { return true; }

  } //-- m: isGoodFormat

  //--------------------------------------------
  public boolean isGood()
  {
    //if (this.isGood == null)
    // { return false; }

    return this.isGood;   
  }

  //--------------------------------------------
  /** Populate internal data by extracting data from a string. 
   *  @param sDataString the data which needs to be parsed 
   *  */
  public void loadData(String sDataString)
  {

    if (!this.isGoodFormat(sDataString.trim()))
    {
      this.isGood = false;
      this.translation = "";
      this.languageCode = "";
      return;
    }
    else
    {
      this.isGood = true;
    }
   
    StringBuffer sbText =
      new StringBuffer(sDataString
        .trim()
        .substring(this.FIELD_NAME.length())
        .trim()
        .substring(this.SEPARATOR.length()));

    //-- StringBuffer.delete() is java 1.2
    //--
    //-- sbText.delete(0, this.FIELD_NAME.length());
    //-- sbText.delete(0, this.SEPARATOR.length());
    //-- check if the text contains the language code delimiters and
    //-- in the correct order.

    int iLangDelimStart =
      sbText.toString().indexOf(this.LANG_CODE_START);
    int iLangDelimEnd =
      sbText.toString().indexOf(this.LANG_CODE_END, iLangDelimStart);

    if ((iLangDelimStart == -1) || (iLangDelimEnd == -1))
    {
      this.languageCode = "";
      this.translation = sbText.toString();
    }
    else
    {
      this.languageCode =
        sbText.toString().substring(iLangDelimStart + 1, iLangDelimEnd);
      this.translation =
       sbText.toString().substring(0, iLangDelimStart) +
       sbText.toString().substring(iLangDelimEnd + 1);
    }


  } //-- parseDataString

  //--------------------------------------------

  //--------------------------------------------
  /** @return some text */
  public String getTranslation()
  {
    return this.translation; 
  }

  //--------------------------------------------
  /** returns a two letter code which represents the human
   *  natural language in which this translation is written.
   *  This 2 letter code should conform to the relevant
   *  rfc specification and may be checked by the java 
   *  Locale class. */
  public String getLanguageCode()
  { 
    return this.languageCode; 
  }
  
  //--------------------------------------------
  /** sets the translation text */
  public void setTranslation(String sNewTranslation)
  { 
    this.translation = sNewTranslation; 
  }
  //--------------------------------------------


   //--------------------------------------------
  /** provide a textual representation of the data contained
   *  by the Translation object. */ 
  public String print()
  { 
    StringBuffer sbReturn = new StringBuffer("");
    sbReturn.append("Translation:" + this.translation); 
    sbReturn.append(NEWLINE); 
    sbReturn.append("Translation language:" + this.languageCode); 
    sbReturn.append(NEWLINE); 
    return sbReturn.toString();
  }
  
  //--------------------------------------------
  /** provides a suitable data string for storing in a text file */
  public String toString()
  {
    StringBuffer sbReturn = new StringBuffer("");
    sbReturn.append(this.FIELD_NAME + this.SEPARATOR); 
    sbReturn.append(this.LANG_CODE_START); 
    sbReturn.append(this.languageCode); 
    sbReturn.append(this.LANG_CODE_END); 
    sbReturn.append(this.translation); 
    return sbReturn.toString();
  }
  
  //--------------------------------------------
  public String debug()
  {
    return "";
  }

  //--------------------------------------------
  /** print some information about the object */
  public String printReport()
  {
    StringBuffer sbReturn = new StringBuffer("");
    sbReturn.append("Field Name:" + Translation.FIELD_NAME);
    sbReturn.append(NEWLINE); 
    sbReturn.append("Separator:" + Translation.SEPARATOR); 
    sbReturn.append(NEWLINE); 
    sbReturn.append("The data is ok:");
    sbReturn.append(this.isGood);
    sbReturn.append(NEWLINE); 
    return sbReturn.toString();
 
  } //-- method: printReport

  //--------------------------------------------
  /** A main method to do some testing */
  public static void main(String[] args) throws Exception
  {
    StringBuffer sbUsageMessage = new StringBuffer("");
    sbUsageMessage.append("usage: java Translation [Translation text]");
    sbUsageMessage.append(NEWLINE); 
    sbUsageMessage.append("eg  java Translation \"tr=welcome back\" ");

    if (args.length == 0)
    {	    
      System.out.println(sbUsageMessage);
      System.exit(-1);
    }

    StringBuffer sbOutput = new StringBuffer("");

    Translation trTest = new Translation(args[0]);
    System.out.println("method: print() ");
    System.out.println(NEWLINE);
    System.out.println(trTest.print());

    System.out.println("method: toString() " + trTest.toString());
    System.out.println(trTest.printReport());


  } //-- main()
} //-- Translation class
