Java Reference
In-Depth Information
Example 10•22: GUIResourceBundle.java (continued)
StringTokenizer t = new StringTokenizer(s, ", \t", false);
ArrayList list = new ArrayList();
while(t.hasMoreTokens()) list.add(t.nextToken());
return list;
}
/** Like above, but return a default instead of throwing an exception */
public java.util.List getStringList(String key,
java.util.List defaultValue) {
try { return getStringList(key); }
catch(MissingResourceException e) { return defaultValue; }
}
/** Look up the named resource and try to interpret it as a boolean. */
public boolean getBoolean(String key) throws MissingResourceException {
String s = bundle.getString(key);
s = s.toLowerCase();
if (s.equals("true")) return true;
else if (s.equals("false")) return false;
else if (s.equals("yes")) return true;
else if (s.equals("no")) return false;
else if (s.equals("on")) return true;
else if (s.equals("off")) return false;
else {
throw new MalformedResourceException("boolean", key);
}
}
/** As above, but return the default instead of throwing an exception */
public boolean getBoolean(String key, boolean defaultValue) {
try { return getBoolean(key); }
catch(MissingResourceException e) {
if (e instanceof MalformedResourceException)
System.err.println("WARNING: " + e.getMessage());
return defaultValue;
}
}
/** Like getBoolean(), but for integers */
public int getInt(String key) throws MissingResourceException {
String s = bundle.getString(key);
try {
// Use decode() instead of parseInt() so we support octal
// and hexadecimal numbers
return Integer.decode(s).intValue();
} catch (NumberFormatException e) {
throw new MalformedResourceException("int", key);
}
}
/** As above, but with a default value */
public int getInt(String key, int defaultValue) {
try { return getInt(key); }
catch(MissingResourceException e) {
if (e instanceof MalformedResourceException)
System.err.println("WARNING: " + e.getMessage());
return defaultValue;
Search WWH ::




Custom Search