/** *CreatesanewStringManagerforagivenpackage.Thisisa *privatemethodandallaccesstoitisarbitratedbythe *staticgetManagermethodcallsothatonlyoneStringManager *perpackagewillbecreated. * *@parampackageNameNameofpackagetocreateStringManagerfor.
*/ private StringManager(String packageName) {
String bundleName = packageName + ".LocalStrings";
ResourceBundle tempBundle = null; try {
tempBundle = ResourceBundle.getBundle(bundleName, Locale.getDefault());
} catch( MissingResourceException ex ) { // Try from the current loader (that's the case for trusted apps) // Should only be required if using a TC5 style classloader structure // where common != shared != server
ClassLoader cl = Thread.currentThread().getContextClassLoader(); if( cl != null ) { try {
tempBundle = ResourceBundle.getBundle(
bundleName, Locale.getDefault(), cl);
} catch(MissingResourceException ex2) { // Ignore
}
}
} // Get the actual locale, which may be different from the requested one if (tempBundle != null) {
locale = tempBundle.getLocale();
} else {
locale = null;
}
bundle = tempBundle;
}
/** *Getastringfromtheunderlyingresourcebundleorreturn *nulliftheStringisnotfound. * *@paramkeytodesiredresourceString *@returnresourceStringmatching<i>key</i>fromunderlying *bundleornullifnotfound. *@throwsIllegalArgumentExceptionif<i>key</i>isnull.
*/ public String getString(String key) { if(key == null){
String msg = "key may not have a null value";
thrownew IllegalArgumentException(msg);
}
String str = null;
try { // Avoid NPE if bundle is null and treat it like an MRE if (bundle != null) {
str = bundle.getString(key);
}
} catch(MissingResourceException mre) { //bad: shouldn't mask an exception the following way: // str = "[cannot find message associated with key '" + key + "' due to " + mre + "]"; // because it hides the fact that the String was missing // from the calling code. //good: could just throw the exception (or wrap it in another) // but that would probably cause much havoc on existing // code. //better: consistent with container pattern to // simply return null. Calling code can then do // a null check.
str = null;
}
return str;
}
/** *Getastringfromtheunderlyingresourcebundleandformat *itwiththegivensetofarguments. * *@paramkeyThekeyfortherequiredmessage *@paramargsThevaluestoinsertintothemessage * *@returnTherequeststringformattedwiththeprovidedargumentsorthe *keyifthekeywasnotfound.
*/ public String getString(final String key, final Object... args) {
String value = getString(key); if (value == null) {
value = key;
}
MessageFormat mf = new MessageFormat(value);
mf.setLocale(locale); return mf.format(args, new StringBuffer(), null).toString();
}
// -------------------------------------------------------------- // STATIC SUPPORT METHODS // --------------------------------------------------------------
privatestaticfinal Map<String, StringManager> managers = new HashMap<>();
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.