package tutor; import java.io.*; import java.util.*; /** This class represents the translation of some piece * of text into some other language. The translation forms part * of a {@linkplain tutor.Record} and each record may contain * several translations, which are stored in a {@linkplain tutor.TranslationSet} * * * */ 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; //-------------------------------------------- /** determines if the translation is written in the native * characterset of the language (e.g. kanji for chinese) */ private boolean nativeCharacters; //-------------------------------------------- /** The text indicating a translation field in the data file */ public static String FIELD_START = "-"; //-------------------------------------------- /** the string which separates field names from field values */ public static String SEPARATOR = " "; //-------------------------------------------- private static String NEWLINE = System.getProperty("line.separator"); //-------------------------------------------- /** creates a Translation with no data */ public Translation() { this.translation = ""; this.languageCode = ""; this.isGood = false; this.nativeCharacters = false; } //-- constr: () //-------------------------------------------- /** creates a Translation with two strings */ public Translation(String sTranslation, String sLanguageCode) { this(); this.translation = sTranslation; this.languageCode = sLanguageCode; } //-- constr: (string, string) //-------------------------------------------- /** creates Translation object from text data */ public Translation(String sText) { this(); this.loadData(sText); } //-------------------------------------------- /** an alias for isGoodFormat */ public static boolean isTranslation(String sTextData) { return Translation.isGoodFormat(sTextData); } //-------------------------------------------- /** Determines if the translation (or transcription) is written * with the native character set for the language being studied. * Within the data file this is indicated by a {@literal .cc} * extension after the language code. For example * {@code -ja.cc } * */ public boolean isNativeCharacters() { return this.nativeCharacters; } //-------------------------------------------- /** checks if the transcription consists of a single word */ public boolean isSingleWord() { if (this.translation.trim().indexOf(' ') == -1) { return false; } return true; } //-------------------------------------------- /** checks whether the given string represents usable data * For example {@code "-en big" } */ 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 //-------------------------------------------- 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 sText) { if (!this.isGoodFormat(sText.trim())) { this.isGood = false; this.translation = sText; this.languageCode = "??"; return; } this.isGood = true; String sDataString = new String(sText.trim().substring(this.FIELD_START.length())); if (sDataString.indexOf(' ') == -1) { if (sDataString.endsWith(".cc")) { this.languageCode = sDataString.substring(0, sDataString.indexOf('.')); this.nativeCharacters = true; return; } this.languageCode = sDataString; this.translation = ""; return; } this.languageCode = sDataString.substring(0, sDataString.indexOf(' ')); if (this.languageCode.endsWith(".cc")) { this.languageCode = this.languageCode.substring(0, this.languageCode.indexOf('.')); this.nativeCharacters = true; } this.translation = sDataString.substring(sDataString.indexOf(' ')).trim(); } //-- loadData //-------------------------------------------- /** @return the translation 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; } //-------------------------------------------- /** attempts to return the language name using the * {@linkplain java.util.Locale} class */ public String getLanguageName() { return (new Locale(this.languageCode)).getDisplayLanguage(); } //-------------------------------------------- /** determines if the languageCode attribute is a valid * 2 letter code. However... warning... this result should not * be taken too seriously because java locales only support * 2 letter language codes NOT 3 letter codes. Some languages * only have a 3 letter code * (for example: the native american mayan language, tzotzil, * code: tzz) * */ public boolean isValidLanguageCode() { if (this.languageCode.length() > 3) { return false; } //-- see the comment above for the logic of the following //-- statement. Three letter codes such as "tzz" may be //-- valid, but not valid java language codes. if (this.languageCode.length() == 3) { return true; } String sLanguageName = (new Locale(this.languageCode)).getDisplayLanguage(); if (sLanguageName.equals(this.languageCode)) { return false; } return true; } //-------------------------------------------- /** 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("Language Code :" + this.languageCode + NEWLINE); sbReturn.append("Language Name :" + (new Locale(this.languageCode)).getDisplayLanguage() + NEWLINE); sbReturn.append("Translation Text :" + this.translation + NEWLINE); sbReturn.append("Native Characters:" + this.nativeCharacters + NEWLINE); sbReturn.append("Is Valid Language:" + this.isValidLanguageCode() + NEWLINE); return sbReturn.toString(); } //-------------------------------------------- /** provides a suitable data string for storing in a text file */ public String toString() { StringBuffer sbReturn = new StringBuffer(""); if (this.isGood) { sbReturn.append(this.FIELD_START); sbReturn.append(this.languageCode); if (this.nativeCharacters) { sbReturn.append(".cc"); } } sbReturn.append(this.SEPARATOR); 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_START); 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 \"-fr bonjour\" "); 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() " + NEWLINE); System.out.println(trTest.print()); System.out.println("method: toString() " + trTest.toString()); //System.out.println(trTest.printReport()); } //-- main() } //-- Translation class