下面列出了怎么用java.util.MissingResourceException的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Obtain the static-catalog setting from the properties.
*
* <p>In the properties, a value of 'yes', 'true', or '1' is considered
* true, anything else is false.</p>
*
* @return The static-catalog setting from the propertyFile or the
* defaultUseStaticCatalog.
*/
private boolean queryUseStaticCatalog () {
String staticCatalog = SecuritySupport.getSystemProperty(pStatic);
if (staticCatalog == null) {
if (resources==null) readProperties();
if (resources==null) return defaultUseStaticCatalog;
try {
staticCatalog = resources.getString("static-catalog");
} catch (MissingResourceException e) {
return defaultUseStaticCatalog;
}
}
if (staticCatalog == null) {
return defaultUseStaticCatalog;
}
return (staticCatalog.equalsIgnoreCase("true")
|| staticCatalog.equalsIgnoreCase("yes")
|| staticCatalog.equalsIgnoreCase("1"));
}
/**
* Initializes all non-final public static fields in the given class with
* messages from a {@link ResourceBundle}.
*
* @param clazz the class containing the fields
*/
public static void initializeMessages(Class<?> clazz, String rbName) {
ResourceBundle rb = null;
try {
rb = ResourceBundle.getBundle(rbName);
} catch (MissingResourceException mre) {
// fall through, handled later
}
for (Field field : clazz.getFields()) {
if (isWritableField(field)) {
String key = field.getName();
String message = getMessage(rb, key);
int mnemonicInt = findMnemonicInt(message);
message = removeMnemonicAmpersand(message);
message = replaceWithPlatformLineFeed(message);
setFieldValue(field, message);
MNEMONIC_LOOKUP.put(message, mnemonicInt);
}
}
}
@Test
public void testNamesAndDescriptions() throws Exception {
PreDownloadPolicy policy = lookupPolicy();
assertEquals("Release Artifact Update Policy", policy.getName());
assertTrue(policy.getDescription(Locale.US).contains("when a release artifact will be updated"));
assertEquals("Update always", policy.getOptionName(Locale.US, UpdateOption.ALWAYS));
assertEquals("Do not download from remote", policy.getOptionName(Locale.US, UpdateOption.NEVER));
assertEquals("Update, if older than a day", policy.getOptionName(Locale.US, UpdateOption.DAILY));
assertEquals("Update, if older than a hour", policy.getOptionName(Locale.US, UpdateOption.HOURLY));
assertEquals("Download only once", policy.getOptionName(Locale.US, UpdateOption.ONCE));
assertTrue(policy.getOptionDescription(Locale.US, UpdateOption.ALWAYS).contains("each download"));
assertTrue(policy.getOptionDescription(Locale.US, UpdateOption.NEVER).contains("never from the remote"));
assertTrue(policy.getOptionDescription(Locale.US, UpdateOption.DAILY).contains("older than one day"));
assertTrue(policy.getOptionDescription(Locale.US, UpdateOption.HOURLY).contains("older than one hour"));
assertTrue(policy.getOptionDescription(Locale.US, UpdateOption.ONCE).contains("if it does not exist"));
try {
policy.getOptionName(Locale.US, StandardOption.NOOP);
// Exception should be thrown
assertTrue(false);
} catch (MissingResourceException e) {
//
}
}
public void processEvent(final SystemEvent event) {
try {
ResourceBundle adminBundle = ResourceBundle.getBundle("admin");
ResourceBundle adminPersistenceBundle = null;
try {
adminPersistenceBundle = ResourceBundle.getBundle("admin-persistence");
} catch (MissingResourceException mre) {
//intentional
}
boolean isLegacyTemplate = has(adminBundle.getString("admin.legacy")) && adminBundle.getString("admin.legacy").equals("true");
StringBuilder sb = new StringBuilder("Using Admin Template ")
.append(adminBundle.getString("admin.version"))
.append(isLegacyTemplate ? " (legacy)" : "");
if (has(adminPersistenceBundle)) {
sb.append(", Admin Persistence ").append(adminPersistenceBundle.getString("admin-persistence.version"));
}
sb.append(" and Admin Theme ").append(ResourceBundle.getBundle("admin-theme").getString("theme.version"));
log.log(Level.INFO, sb.toString());
} catch (Exception e) {
log.log(Level.WARNING, "Could not get AdminFaces version.", e);
}
}
/**
* Returns the localized message for the given message key
*
* @param key
* the message key
* @return The localized message for the key
*/
public static String getString(String key) {
if (RESOURCE_BUNDLE == null) {
throw new RuntimeException("Localized messages from resource bundle '" + BUNDLE_NAME + "' not loaded during initialization of driver.");
}
try {
if (key == null) {
throw new IllegalArgumentException("Message key can not be null");
}
String message = RESOURCE_BUNDLE.getString(key);
if (message == null) {
message = "Missing error message for key '" + key + "'";
}
return message;
} catch (MissingResourceException e) {
return '!' + key + '!';
}
}
/**
* Return the string value of a named resource in the rmic.properties
* resource bundle. If the resource is not found, null is returned.
*/
public static String getString(String key) {
if (!resourcesInitialized) {
initResources();
}
// To enable extensions, search the 'resourcesExt'
// bundle first, followed by the 'resources' bundle...
if (resourcesExt != null) {
try {
return resourcesExt.getString(key);
} catch (MissingResourceException e) {}
}
try {
return resources.getString(key);
} catch (MissingResourceException ignore) {
}
return null;
}
/**
* Overrides this method to get the formatted&localized error message.
*
* REVISIT: the system locale is used to load the property file.
* do we want to allow the appilcation to specify a
* different locale?
*/
public String getMessage() {
ResourceBundle resourceBundle = null;
resourceBundle = SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.XMLSchemaMessages");
if (resourceBundle == null)
throw new MissingResourceException("Property file not found!", "com.sun.org.apache.xerces.internal.impl.msg.XMLSchemaMessages", key);
String msg = resourceBundle.getString(key);
if (msg == null) {
msg = resourceBundle.getString("BadMessageKey");
throw new MissingResourceException(msg, "com.sun.org.apache.xerces.internal.impl.msg.XMLSchemaMessages", key);
}
if (args != null) {
try {
msg = java.text.MessageFormat.format(msg, args);
} catch (Exception e) {
msg = resourceBundle.getString("FormatFailed");
msg += " " + resourceBundle.getString(key);
}
}
return msg;
}
/**
* Returns the localized message for the given message key
*
* @param key
* the message key
* @return The localized message for the key
*/
public static String getString(String key) {
if (RESOURCE_BUNDLE == null) {
throw new RuntimeException("Localized messages from resource bundle '" + BUNDLE_NAME + "' not loaded during initialization of driver.");
}
try {
if (key == null) {
throw new IllegalArgumentException("Message key can not be null");
}
String message = RESOURCE_BUNDLE.getString(key);
if (message == null) {
message = "Missing error message for key '" + key + "'";
}
return message;
} catch (MissingResourceException e) {
return '!' + key + '!';
}
}
/**
* Obtain the preferPublic setting from the properties.
*
* <p>In the properties, a value of 'public' is true,
* anything else is false.</p>
*
* @return True if prefer is public or the
* defaultPreferSetting.
*/
private boolean queryPreferPublic () {
String prefer = SecuritySupport.getSystemProperty(pPrefer);
if (prefer == null) {
if (resources==null) readProperties();
if (resources==null) return defaultPreferPublic;
try {
prefer = resources.getString("prefer");
} catch (MissingResourceException e) {
return defaultPreferPublic;
}
}
if (prefer == null) {
return defaultPreferPublic;
}
return (prefer.equalsIgnoreCase("public"));
}
/**
* Return an internationalized text message for this diagnostic. The generated
* messages are backed by a <code>PropertyResourceBundle</code> with parameters
* substituted into appropriate points in the message. See the package level
* documentation for further information.
*
* @return diagnostic message
*/
public String getMessage() {
final String key = getType().toString();
try {
String value = RESOURCE.getString(key);
//
//
//
// Substitute parameters into value string
final String[] params = getParams();
for (int k = 0; k < params.length; ++k) {
final int position = value.indexOf("%" + (k + 1));
// In theory should always use all parameters, but just in case someone does
// write an error message that does not use all the parameters, we make an
// extra check here.
if (position != -1) {
value = value.substring(0, position) + params[k] + value.substring(position + 2);
}
}
return mType.getMessagePrefix() + value;
} catch (final MissingResourceException e) {
Diagnostic.userLog("Missing resource information for diagnostic: " + key);
return mType.getMessagePrefix() + key;
}
}
@Override
protected OlsonTimeZone createInstance(String key, String data) {
OlsonTimeZone tz = null;
try {
UResourceBundle top = UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME,
ZONEINFORESNAME, ICUResourceBundle.ICU_DATA_CLASS_LOADER);
UResourceBundle res = openOlsonResource(top, data);
if (res != null) {
tz = new OlsonTimeZone(top, res, data);
tz.freeze();
}
} catch (MissingResourceException e) {
// do nothing
}
return tz;
}
private void loadResourceBundle(String resourceBundleName,
Locale locale) {
if (! table.contains(locale)) {
try {
Hashtable resourceTable = new Hashtable();
ResourceBundle bundle = ResourceBundle.getBundle(resourceBundleName, locale);
Enumeration iter = bundle.getKeys();
while(iter.hasMoreElements()) {
String key = (String)iter.nextElement();
resourceTable.put(key, bundle.getObject(key));
}
table.put(locale, resourceTable);
}
catch (MissingResourceException e) {
System.err.println("loadResourceBundle: " + e);
// Just return so toDisplayString() returns the
// non-localized key.
return;
}
}
}
/**
* Queries the engine for the set of features it supports for a given locale.
* Features can either be framework defined, e.g.
* {@link TextToSpeech.Engine#KEY_FEATURE_NETWORK_SYNTHESIS} or engine specific.
* Engine specific keys must be prefixed by the name of the engine they
* are intended for. These keys can be used as parameters to
* {@link TextToSpeech#speak(String, int, java.util.HashMap)} and
* {@link TextToSpeech#synthesizeToFile(String, java.util.HashMap, String)}.
*
* Features values are strings and their values must meet restrictions described in their
* documentation.
*
* @param locale The locale to query features for.
* @return Set instance. May return {@code null} on error.
* @deprecated As of API level 21, please use voices. In order to query features of the voice,
* call {@link #getVoices()} to retrieve the list of available voices and
* {@link Voice#getFeatures()} to retrieve the set of features.
*/
@Deprecated
public Set<String> getFeatures(final Locale locale) {
return runAction(new Action<Set<String>>() {
@Override
public Set<String> run(ITextToSpeechService service) throws RemoteException {
String[] features = null;
try {
features = service.getFeaturesForLanguage(
locale.getISO3Language(), locale.getISO3Country(), locale.getVariant());
} catch(MissingResourceException e) {
Log.w(TAG, "Couldn't retrieve 3 letter ISO 639-2/T language and/or ISO 3166 " +
"country code for locale: " + locale, e);
return null;
}
if (features != null) {
final Set<String> featureSet = new HashSet<String>();
Collections.addAll(featureSet, features);
return featureSet;
}
return null;
}
}, null, "getFeatures");
}
/**
* Obtain the list of catalog files from the properties.
*
* @return A semicolon delimited list of catlog file URIs
*/
private String queryCatalogFiles () {
String catalogList = SecuritySupport.getSystemProperty(pFiles);
fromPropertiesFile = false;
if (catalogList == null) {
if (resources == null) readProperties();
if (resources != null) {
try {
catalogList = resources.getString("catalogs");
fromPropertiesFile = true;
} catch (MissingResourceException e) {
System.err.println(propertyFile + ": catalogs not found.");
catalogList = null;
}
}
}
if (catalogList == null) {
catalogList = defaultCatalogFiles;
}
return catalogList;
}
private void readTips(ResourceBundle tips, String tipPrefix) {
ArrayList<String> tipList = new ArrayList<>();
// Look for all properties "<tip prefix>x" where x is a sequential 0-index
try {
for (int i = 0; ; i++) {
String tip = tips.getString(tipPrefix + i);
tipList.add(tip);
}
} catch (MissingResourceException ex) {
// no more tips
}
tipsText = tipList.toArray(new String[tipList.size()]);
if (tipsText.length == 0) {
throw new IllegalArgumentException(res.getString("NoTipsOfTheDaySupplied.exception.message"));
}
}
private boolean lookupBundle(String rbName) {
// See if Logger.getLogger can find the resource in this directory
try {
Logger aLogger = Logger.getLogger("NestedLogger2", rbName);
} catch (MissingResourceException re) {
if (DEBUG) {
System.out.println(
"As expected, LoadItUp2.lookupBundle() did not find the bundle "
+ rbName);
}
return false;
}
System.out.println("FAILED: LoadItUp2.lookupBundle() found the bundle "
+ rbName + " using a stack search.");
return true;
}
/**
* Returns the plugin's resource bundle.
*
* @return the ResourceBundle or null if missing
*/
public ResourceBundle getResourceBundle() {
try {
if (mResourceBundle == null) {
mResourceBundle = ResourceBundle.getBundle("Annotator.AnnotatorPluginResources");
}
} catch (MissingResourceException x) {
mResourceBundle = null;
}
return mResourceBundle;
}
/** return the specified resource or null if not found */
public ResourceBundle getResourceBundle(String resource, Locale locale) {
try {
return ResourceBundle.getBundle(resource, locale);
} catch (MissingResourceException e) {
return null;
}
}
public static String getString(String key) {
try {
return RESOURCE_BUNDLE.getString(key);
} catch (MissingResourceException e) {
return '!' + key + '!';
}
}
/**
* @param key
* @param lcl
*/
public static String getString( String key, ULocale lcl )
{
try
{
return getResourceBundle( lcl ).getString( key );
}
catch ( MissingResourceException e )
{
return '!' + key + '!';
}
}
public void setLocale(Locale locale) {
try {
if (locale != null) {
this.resources = SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.xpath.regex.message", locale);
}
else {
this.resources = SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.xpath.regex.message");
}
}
catch (MissingResourceException mre) {
throw new RuntimeException("Installation Problem??? Couldn't load messages: "
+ mre.getMessage());
}
}
public boolean testGetBundleFromSystemClassLoader(String bundleName) {
// this should succeed if the bundle is on the system classpath.
try {
Logger aLogger = Logger.getLogger(ResourceBundleSearchTest.newLoggerName(),
bundleName);
} catch (MissingResourceException re) {
msgs.add("INFO: testGetBundleFromSystemClassLoader() did not find bundle "
+ bundleName);
return false;
}
msgs.add("INFO: testGetBundleFromSystemClassLoader() found the bundle "
+ bundleName);
return true;
}
public static String getString(String key)
{
try
{
return RESOURCE_BUNDLE.getString(key);
}
catch (MissingResourceException e)
{
return '!' + key + '!';
}
}
/**
* Returns the label for the specified type name.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
*
* @generated
*/
protected String getLabel ( final String typeName )
{
try
{
return ChartEditPlugin.INSTANCE
.getString ( "_UI_" + typeName + "_type" ); //$NON-NLS-1$ //$NON-NLS-2$
}
catch ( final MissingResourceException mre )
{
ChartEditorPlugin.INSTANCE.log ( mre );
}
return typeName;
}
/**
* Returns a String array containing time zone names. The String array has
* at most size elements.
*
* @param key the time zone ID for which names are obtained
* @param size the requested size of array for names
* @return a String array containing names
*/
public String[] getStringArray(String key, int size) {
String[] names = handleGetObject(key, size);
if ((names == null || names.length != size) && parent != null) {
names = ((TimeZoneNamesBundle)parent).getStringArray(key, size);
}
if (names == null) {
throw new MissingResourceException("no time zone names", getClass().getName(), key);
}
return names;
}
public static String getString(String key) {
try {
return BUNDLE.getString(key);
} catch (MissingResourceException e) {
return key;
}
}
public static boolean isOnClassPath(String baseName, ClassLoader cl) {
ResourceBundle rb = null;
try {
rb = ResourceBundle.getBundle(baseName, Locale.getDefault(), cl);
System.out.println("INFO: Found bundle " + baseName + " on " + cl);
} catch (MissingResourceException e) {
System.out.println("INFO: Could not find bundle " + baseName + " on " + cl);
return false;
}
return (rb != null);
}
/** return the specified resource or null if not found */
public ResourceBundle getResourceBundle(String resource) {
try {
return ResourceBundle.getBundle(resource);
} catch (MissingResourceException e) {
return null;
}
}
/** return the specified resource or null if not found */
public ResourceBundle getResourceBundle(String resource, Locale locale) {
try {
return ResourceBundle.getBundle(resource, locale);
} catch (MissingResourceException e) {
return null;
}
}
private static ResourceBundle findSystemResourceBundle(final Locale locale) {
// the resource bundle is in a restricted package
return AccessController.doPrivileged(new PrivilegedAction<ResourceBundle>() {
@Override
public ResourceBundle run() {
try {
return ResourceBundle.getBundle(SYSTEM_LOGGER_RB_NAME,
locale,
ClassLoader.getSystemClassLoader());
} catch (MissingResourceException e) {
throw new InternalError(e.toString());
}
}
});
}