import java.net.*;
import java.io.*;
import java.util.*;

/**
 *  This class represents a transcription of the contents of
 *  a language sound file. The transcription may be in any 
 *  character set. A Transcription is on of the data fields 
 *  of a Record of information about a language sound file. Other
 *  data fields in this Record are the set of Translations and the 
 *  Cognates
 *
 *  tested under jvm1.1 and jview
 *
 *  @author matth3wbishop<at>yahoo!<dot>com 
 */
 
  
public class Transcription extends Object
{
  //--------------------------------------------
  /** A set of words representing the textual equivalent 
   *  of some segment of sound */
  private String transcription;
  //--------------------------------------------
  /** The character set in which the transcription is written */
  private String characterSet;
  //--------------------------------------------
  private boolean isGood;
  //--------------------------------------------
  /** The text which indicates that this field is a transcription */
  public static String FIELD_NAME = "ts";
  //--------------------------------------------
  /** The string which separates the field name from the value. */
  public static String SEPARATOR = "="; 
  //--------------------------------------------
  public static String NEWLINE = System.getProperty("line.separator");
  //--------------------------------------------

 
  public Transcription()
  {
    this.transcription = "";
    this.characterSet = "";    
    this.isGood = true;

  } //-- constr: ()

  //--------------------------------------------
  /** create an object by parsing text data */
  public Transcription(String sDataString)
  {
    this();
    this.loadData(sDataString);

  } //-- constr: (string)

  //--------------------------------------------
  public void loadData(String sDataString)
  {

    if (!this.isGoodFormat(sDataString.trim()))
    {
      this.transcription = "";
      this.isGood = false;
      return;
    }
    else
    {
      this.isGood = true;
    }

    StringBuffer sbText = new StringBuffer(sDataString.trim());

    this.transcription = sDataString
     .trim().substring(this.FIELD_NAME.length())
     .trim().substring(this.SEPARATOR.length());


  } //-- loadData


  //--------------------------------------------
  /** an alias for isGoodFormat */
  public static boolean isTranscription(String sTextData)
  {
    return Transcription.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(Transcription.FIELD_NAME);

    if (!bHasFieldName)
     { return false; }

    boolean bHasSeparator = sTextData.toLowerCase()
     .trim().substring(Transcription.FIELD_NAME.length())
     .trim().startsWith(Transcription.SEPARATOR);

    if (!bHasSeparator)
     { return false; }

    String sValue = sTextData.toLowerCase()
     .trim().substring(Transcription.FIELD_NAME.length())
     .trim().substring(Transcription.SEPARATOR.length())
     .trim();

    if (sValue.equals(""))
     { return false; }
    else
     { return true; }

  }


  //--------------------------------------------
  /** return the text of the transcription */
  public String getTranscription()
  {
    return this.transcription; 
  }

  //--------------------------------------------
  /** return the character set in which the transcription is 
   *  written */
  public String getCharacterSet()
  { 
    return this.characterSet; 
  }

  //--------------------------------------------
  /** provide a textual representation of the object suitable for
   *  displaying at the console. */ 
  public String print()
  { 

    StringBuffer sbReturn = new StringBuffer("");
    sbReturn.append("Transcription:" + this.transcription); 
    sbReturn.append(NEWLINE); 
    sbReturn.append("Transcription character set:" + this.characterSet); 
    sbReturn.append(NEWLINE); 
    return sbReturn.toString();

  }
  
  //--------------------------------------------
  /** print the data as text */
  public String toString()
  {
    StringBuffer sbReturn = new StringBuffer("");
    sbReturn.append(Transcription.FIELD_NAME + Transcription.SEPARATOR); 
    sbReturn.append(this.transcription); 
    return sbReturn.toString();
  }
  
  //--------------------------------------------
  /**  */
  public String debug()
  {
    String sReturn =
     "Transcription.transcription=[" + this.transcription + "] \n" +
     "Transcription.characterSet=[" + this.characterSet + "] \n";
    return sReturn;
  }

  //--------------------------------------------
  /** print some information about the object */
  public String printReport()
  {
    StringBuffer sbReturn = new StringBuffer("");
    sbReturn.append("Field Name:" + Transcription.FIELD_NAME);
    sbReturn.append(NEWLINE); 
    sbReturn.append("Separator:" + Transcription.SEPARATOR); 
    sbReturn.append(NEWLINE); 
    sbReturn.append("The data is ok:");
    sbReturn.append(this.isGood);
    sbReturn.append(NEWLINE); 
    return sbReturn.toString();
 
  } //-- method: printReport

  //--------------------------------------------
  /** provide a method for testing from the console */
  public static void main(String[] args) throws Exception
  {
    StringBuffer sbUsageMessage = new StringBuffer("");
    sbUsageMessage.append(
      "test usage: java Transcription [transcription text]");
    sbUsageMessage.append(NEWLINE); 
    sbUsageMessage.append(
      "eg  java Transcription ts=welcome ");

    if (args.length == 0)
    {	    
      System.out.println(sbUsageMessage);
      System.exit(-1);
    }

    StringBuffer sbOutput = new StringBuffer("");

    Transcription trTest = new Transcription(args[0]);
    System.out.println(trTest.print());
    System.out.println(trTest.printReport());

  } //-- main()
} //-- Transcription class
