import java.io.*;
import java.util.*;


/**
 * A message in an instant messaging system
 *
 * Unlike a normal Message, an EnglishMessage does not
 * contain abbreviations -- they are all expanded
 *
 * @author  Prof. David Bernstein
 * @version 1.0
 */
public class EnglishMessage extends Message
{
    private Properties      abbreviations;


    /**
     * Explicit Value Constructor
     *
     * @param msg   The text of the message
     */
    public EnglishMessage(String text)
    {
	super(text);
	InputStream      is;


	abbreviations = new Properties();
	try {

	    is = new FileInputStream("abbreviations.txt");
	    abbreviations.load(is);

	} catch (IOException ioe) {

	    // There was a problem opening the file
	    // containing the abbreviations
	}
    }







    /**
     * Get the text of this Message with all of the
     * abbreviations expanded
     *
     * @return   The text
     */
    public String getText()
    {
	String       tempText;

	// Use the protected attribute in the parent
	tempText  = replaceAbbreviations(text);

	return tempText;
    }
    



    /**
     * Replace the abbreviations in a message
     *
     * @param msg   The original message
     */
    private String replaceAbbreviations(String msg)
    {
	String              replaced, token, word;
	StringTokenizer     tokenizer;

	replaced = "";
	
	// Use the protected attribute in the parent
	tokenizer = new StringTokenizer(msg, delimiters, true);
	while (tokenizer.hasMoreTokens()) {

	    token = tokenizer.nextToken();
	    word  = abbreviations.getProperty(token);

	    if (word == null) replaced += token;
	    else              replaced += word;
	}


	return replaced;
    }

}
