package tutor; import java.io.*; import java.util.*; /** * This class represents a data record of information about * a language sound file. The record may contain a set of * {@linkplain tutor.Translation}s, a pointer to a language sound file * , an image file, comments about the record, as well as other information. * The class loads its data by parsing a text string in the format * which is illustrated below. * A record object will usually form part of a * data set called a {@linkplain tutor.RecordSet}. This recordset * is constructed by parsing a plain text data file. * * An example record * {@code * #-- about the weather * * *kazega.wav * -ja Kaze ga tsuyoi desu. * -ja.cc 風が強いです。 * -en It is windy. * -; } * */ public class Record extends Object { //-------------------------------------------- /** The sound file or url which contains some human language recording */ private String soundUrl; //-------------------------------------------- /** contains text comments which are associated with the data record */ private StringBuffer comments; //-------------------------------------------- /** A set of translations into various languages */ private TranslationSet translations; //-------------------------------------------- /** An image which illustrates the word or phrase of the sound file */ private String imageFile; //-------------------------------------------- /** A transcription of the language sound file into some writing * system, this object is not being used */ private Transcription transcription; //-------------------------------------------- /** information about cognates in other languages */ private String cognates; //-------------------------------------------- /** if the record contains some data set this to false; */ private boolean isEmpty = true; //-------------------------------------------- private boolean isGood; //-------------------------------------------- private StringBuffer error; //-------------------------------------------- private StringBuffer errorMessages; //-------------------------------------------- /** a cross platform linebreak */ public static String NEWLINE = System.getProperty("line.separator"); //-------------------------------------------- public static String SOUNDURL_MARKER = "*"; //-------------------------------------------- public static String IMAGE_FILE_MARKER = "-ii"; //-------------------------------------------- public static String RECORD_END_MARKER = "-;"; //-------------------------------------------- public static String COMMENT_MARKER = "#"; //-------------------------------------------- /** creates a record with no data. */ public Record() { this.isGood = false; this.error = new StringBuffer(""); this.errorMessages = new StringBuffer(""); this.translations = new TranslationSet(); this.transcription = new Transcription(); this.soundUrl = new String(""); this.imageFile = new String(""); this.comments = new StringBuffer(""); } //-- constr: () //-------------------------------------------- /** Create a new Record object by parsing a string containing * data. Internally Translation and Transcription objects * will be created by handing string segments to their constructors */ public Record(String sText) { this(); this.loadData(sText); } //-------------------------------------------- public boolean isGood() { return this.isGood; } //-------------------------------------------- /** populate internal data fields by parsing a string * containing information about a sound file and its * language content. * @param= sDataString string to parse * */ public void loadData(String sDataString) { BufferedReader brReader; brReader = new BufferedReader(new StringReader(sDataString)); String sCurrentLine = ""; while (sCurrentLine != null) { // System.out.println(sbCurrentLine); if (sCurrentLine.startsWith(Record.SOUNDURL_MARKER)) { this.soundUrl = sCurrentLine.substring(Record.SOUNDURL_MARKER.length()); } else if (sCurrentLine.startsWith(Record.IMAGE_FILE_MARKER)) { this.imageFile = sCurrentLine.substring(Record.IMAGE_FILE_MARKER.length()); } else if (sCurrentLine.startsWith(Record.COMMENT_MARKER)) { this.comments.append( sCurrentLine.substring(Record.COMMENT_MARKER.length()) + "\n"); } else if (sCurrentLine.startsWith(Record.RECORD_END_MARKER)) { break; } else if (sCurrentLine.startsWith("-")) { this.translations.addTranslation(new Translation(sCurrentLine)); } else { } try { sCurrentLine = brReader.readLine().trim(); } catch (IOException e) { this.isGood = false; this.error.append("Problem reading from text data"); this.errorMessages.append(e); return; } catch (NullPointerException ee) { sCurrentLine = null; } //-- try } //-- while more lines } //-- method: loadData //-------------------------------------------- /** checks whether there is any reference to a sound resource. */ public boolean hasSound() { if (this.soundUrl.equals("")) { return false; } else { return true; } } //-------------------------------------------- /** set the pointer to a sound resource. */ public void setSoundUrl(String sSoundUrl) { this.soundUrl = sSoundUrl; } //-------------------------------------------- /** returns the pointer to a sound resource */ public String getSoundUrl() { return this.soundUrl; } //-------------------------------------------- public String getImageFile() { return this.imageFile; } //-------------------------------------------- public void setImageFile(String sImageFile) { this.imageFile = sImageFile.trim(); } //-------------------------------------------- /** give access to the transcription of the sound file */ public Transcription getTranscription() { return this.transcription; } //-------------------------------------------- /** return comments associated with the record */ public String getComments() { return this.comments.toString(); } //-------------------------------------------- /** set comments for the record */ public void setComments(String sComment) { this.comments.setLength(0); this.comments.append(sComment); } //-------------------------------------------- /** adds a commment to the record */ public void addComments(String sComment) { this.comments.append(sComment + NEWLINE); } //-------------------------------------------- /** give access to the set of translations */ public TranslationSet getTranslationSet() { return this.translations; } //-------------------------------------------- /** returns a particular translation specified by the * the language code. The translation is extracted from * the {@linkplain tutor.TranslationSet}. If there is more than * one translation for a particular language code, this just returns * the first translation. It is possible * that the translationset class should not exist */ public Translation getTranslationByCode(String sLanguageCode) { return this.translations.getTranslationByCode(sLanguageCode); } //-------------------------------------------- /** returns a particular translation specified by the * the language code, written in the roman alphabet */ public Translation getRomanTranslationByCode(String sLanguageCode) { return this.translations.getRomanTranslationByCode(sLanguageCode); } //-------------------------------------------- /** returns a particular translation specified by the * the language code, written in the "native" character set * for the language of the sound file (eg japanese kana) */ public Translation getNativeTranslationByCode(String sLanguageCode) { return this.translations.getNativeTranslationByCode(sLanguageCode); } //-------------------------------------------- /** print a report about the record */ public String printReport() { StringBuffer sbOutput = new StringBuffer(""); sbOutput.append("Sound Resource :" + this.soundUrl); sbOutput.append(NEWLINE); sbOutput.append("Image File :" + this.imageFile); sbOutput.append(NEWLINE); sbOutput.append("Record has data :" + this.isEmpty); sbOutput.append(NEWLINE); sbOutput.append("Number of translations:" + this.translations.count()); sbOutput.append(NEWLINE); sbOutput.append("Image File Marker :" + Record.IMAGE_FILE_MARKER); sbOutput.append(NEWLINE); sbOutput.append("Sound File Marker :" + Record.SOUNDURL_MARKER); sbOutput.append(NEWLINE); sbOutput.append("Record End Marker :" + Record.RECORD_END_MARKER); sbOutput.append(NEWLINE); return sbOutput.toString(); } //-------------------------------------------- /** create some kind of textual representation of the object. * suitable for displaying to the console or plain text element */ public String print() { StringBuffer sbOutput = new StringBuffer(""); sbOutput.append("Sound url /" + this.soundUrl + "/ \n"); sbOutput.append("Image File /" + this.imageFile + "/ \n"); sbOutput.append("Comments /" + this.comments + "/ \n"); sbOutput.append(this.translations.print()); return sbOutput.toString(); } //-------------------------------------------- /** creates a textual representation of the object, probably * in a format suitable for saving in a text data file. */ public String toString() { StringBuffer sbOutput = new StringBuffer(""); if (this.comments.toString().trim().length() > 0) { sbOutput.append(Record.COMMENT_MARKER + this.comments.toString().replace("\n", "\n#")); sbOutput.append(NEWLINE); } sbOutput.append(Record.SOUNDURL_MARKER + this.soundUrl); sbOutput.append(NEWLINE); if (this.imageFile.trim().length() > 0) { sbOutput.append(Record.IMAGE_FILE_MARKER + this.imageFile); sbOutput.append(NEWLINE); } sbOutput.append(this.translations.toString()); sbOutput.append(Record.RECORD_END_MARKER); sbOutput.append(NEWLINE); return sbOutput.toString(); } //-- m: toString //-------------------------------------------- /** A main method to allow for testing of the class from the console. */ public static void main(String[] args) throws Exception { String NEWLINE = System.getProperty("line.separator"); String sTest = new String( " #------------------------ \n" + " #-- about the weather \n" + " \n" + " *kazega.wav \n" + " -ii wind.jpg \n" + " -ja Kaze ga tsuyoi desu. \n" + " -ja.cc 風が強いです。 \n" + " -en It is windy. \n" + " -; \n"); System.out.println("Using text data:"); System.out.println(sTest); Record rTestRecord = new Record(sTest.toString()); System.out.println("method: toString()"); System.out.println(rTestRecord.toString()); System.out.println("method: print()"); System.out.println(rTestRecord.print()); } //-- main() } //-- Record class