package tutor; import java.util.*; /** * This class represents a transcription of a sound in a * {@linkplain tutor.Record} contained in a language {@linkplain tutor.DataFile}. * A transcription may be constructed from a {@linkplain tutor.Translation} * object. * @author http://bumble.sf.net */ public class Transcription extends Object { //-------------------------------------------- /** A set of words representing the textual equivalent * of some segment of sound */ private String transcription; //-------------------------------------------- /** A language code */ private String languageCode; //-------------------------------------------- /** 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_START = "-"; //-------------------------------------------- /** 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.languageCode = ""; this.characterSet = ""; this.isGood = false; } //-- constr: () //-------------------------------------------- /** create an object by parsing text data */ public Transcription(String sDataString) { this(); this.loadData(sDataString); } //-- constr: (string) //-------------------------------------------- /** create the object from a {@linkplain tutor.Translation} object */ public Transcription(Translation translation) { this(); this.languageCode = translation.getLanguageCode(); this.transcription = translation.getTranslation(); } //-------------------------------------------- /** checks if the transcription consists of a single word */ public boolean isSingleWord() { if (this.transcription.trim().indexOf(' ') == -1) { return false; } return true; } //-------------------------------------------- public void loadData(String sText) { if (!this.isGoodFormat(sText.trim())) { this.isGood = false; this.transcription = sText; this.languageCode = "??"; return; } this.isGood = true; String sDataString = new String(sText.trim().substring(this.FIELD_START.length())); if (sDataString.indexOf(' ') == -1) { this.languageCode = sDataString; this.transcription = ""; return; } this.languageCode = sDataString.substring(0, sDataString.indexOf(' ')); this.transcription = sDataString.substring(sDataString.indexOf(' ')).trim(); } //-- 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().length() < 3) { return false; } boolean bHasFieldName = sTextData .trim().startsWith(Translation.FIELD_START); if (!bHasFieldName) { return false; } if (!Character.isLetter(sTextData.trim().charAt(1))) { return false; } if (!Character.isLetter(sTextData.trim().charAt(2))) { return false; } return true; } //-- m: isGoodFormat //-------------------------------------------- /** 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("Language Code :" + this.languageCode); sbReturn.append(NEWLINE); sbReturn.append("Transcription Text:" + this.transcription); sbReturn.append(NEWLINE); sbReturn.append("Character set :" + this.characterSet); sbReturn.append(NEWLINE); sbReturn.append("Data Ok? :" + this.isGood); sbReturn.append(NEWLINE); return sbReturn.toString(); } //-------------------------------------------- /** print the data as text */ public String toString() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append(Transcription.FIELD_START + this.languageCode); sbReturn.append(Transcription.SEPARATOR + this.transcription); return sbReturn.toString(); } //-------------------------------------------- /** 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] \n" + "eg: java Transcription -es una cucaracha \n"); 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.toString()); } //-- main() } //-- Transcription class