Skip to content

Commit

Permalink
Enhancement for the getString methods - handle compound messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Amedeo Paglione committed Sep 24, 2003
1 parent 0135549 commit 0f3d58b
Showing 1 changed file with 90 additions and 3 deletions.
93 changes: 90 additions & 3 deletions com/gallery/GalleryRemote/util/GRI18n.java
Expand Up @@ -12,6 +12,8 @@
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.MissingResourceException;
import java.io.*;
import java.text.MessageFormat;

public class GRI18n implements PreferenceNames {
private static final String RESNAME = "com.gallery.GalleryRemote.resources.GRResources";
Expand All @@ -20,6 +22,7 @@ public class GRI18n implements PreferenceNames {
private static GRI18n ourInstance;
private Locale grLocale;
private ResourceBundle grResBundle;
private MessageFormat grMsgFrmt;

public synchronized static GRI18n getInstance() {
if (ourInstance == null) {
Expand All @@ -40,6 +43,7 @@ private GRI18n() {
grLocale = new Locale(myLang, myCountry);
}

grMsgFrmt = new MessageFormat("");
setResBoundle();

}
Expand All @@ -50,22 +54,59 @@ public void setLocale(String language, String country) {
}

public String getString(String key) {
return getString(getCallerName(), key);
}

public String getString(Object caller, String key) {
return getString(caller.getClass().getName(), key);
}

public String getString(String key, Object [] params) {
return getString(getCallerName(), key, params);
}

public String getString(Object caller, String key, Object [] params) {
return getString(caller.getClass().getName(), key, params);
}

private String getString(String className, String key) {
String msg;
String extKey = className + "." + key;
try {
msg = grResBundle.getString(key);
msg = grResBundle.getString(extKey);
} catch (NullPointerException e) {
Log.log(Log.ERROR, MODULE, "Key null error");
Log.logException(Log.ERROR, MODULE, e);
msg = "[NULLKEY]";
} catch (MissingResourceException e) {
Log.log(Log.INFO, MODULE, "Key [" + key + "] not defined");
Log.log(Log.INFO, MODULE, "Key [" + extKey + "] not defined");
Log.logException(Log.INFO, MODULE, e);
msg = "["+key+"]";
msg = "["+extKey+"]";
}

return msg;

}

private String getString(String className, String key, Object [] params) {
String template, msg;
String extKey = className + "." + key;
try {
template = grResBundle.getString(extKey);
grMsgFrmt.applyPattern(template);
msg = grMsgFrmt.format(params);
} catch (NullPointerException e) {
Log.log(Log.ERROR, MODULE, "Key null error");
Log.logException(Log.ERROR, MODULE, e);
msg = "[NULLKEY]";
} catch (MissingResourceException e) {
Log.log(Log.INFO, MODULE, "Key [" + extKey + "] not defined");
Log.logException(Log.INFO, MODULE, e);
msg = "["+extKey+"]";
}

return msg;
}

private void setResBoundle() {
try {
Expand All @@ -74,8 +115,54 @@ private void setResBoundle() {
Log.log(Log.ERROR, MODULE, "Resource bundle error");
Log.logException(Log.ERROR, MODULE, e);
}

grMsgFrmt.setLocale(grLocale);

}

private static String getCallerName() {


StringWriter sw = new StringWriter();
new Throwable().printStackTrace(new PrintWriter(sw));
BufferedReader sr = new BufferedReader(new StringReader(sw.getBuffer().toString()));

String returnVal = "";

try {
int lineCount = 0;
String line;

while( (line = sr.readLine()) != null ) {
if(lineCount == 3) { // stack depth 1 --> getCallerName()
// 2 getCallerName's caller
// 3 actual caller we're interested in
int start = line.indexOf("at ") + 3,
end = line.indexOf("(");
String [] returnVals;
String retTemp = line.substring(start, end);
returnVals = retTemp.split("\\.");
returnVal = returnVals[returnVals.length - 2];

}
lineCount++;
}
}
catch(IOException e) {
Log.log(Log.ERROR, MODULE, "Error reading the call stack...");
Log.logException(Log.ERROR, MODULE, e);
}

int initBegin = returnVal.indexOf(".<init>"); // trim off ".<init>" for initializers
if (initBegin != -1)
returnVal = returnVal.substring(0, initBegin);

int dollarSignBegin = returnVal.indexOf("$");
if (dollarSignBegin != -1)
returnVal = returnVal.substring(0, dollarSignBegin);

return returnVal;
}

}

0 comments on commit 0f3d58b

Please sign in to comment.