下面列出了怎么用java.util.ListResourceBundle的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Gets a resource bundle using the specified base name and locale, and the caller's class loader.
* @param bundle the base name of the resource bundle, a fully qualified class name
* @param locale the locale for which a resource bundle is desired
* @return a resource bundle for the given base name and locale
*/
public static ListResourceBundle getResourceBundle(final String bundle, final Locale locale) {
return AccessController.doPrivileged(new PrivilegedAction<ListResourceBundle>() {
public ListResourceBundle run() {
try {
return (ListResourceBundle)ResourceBundle.getBundle(bundle, locale);
} catch (MissingResourceException e) {
try {
return (ListResourceBundle)ResourceBundle.getBundle(bundle, new Locale("en", "US"));
} catch (MissingResourceException e2) {
throw new MissingResourceException(
"Could not load any resource bundle by " + bundle, bundle, "");
}
}
}
});
}
/**
* Gets a resource bundle using the specified base name and locale, and the
* caller's class loader.
*
* @param bundle the base name of the resource bundle, a fully qualified
* class name
* @param locale the locale for which a resource bundle is desired
* @return a resource bundle for the given base name and locale
*/
public static ListResourceBundle getResourceBundle(final String bundle, final Locale locale) {
return AccessController.doPrivileged(new PrivilegedAction<ListResourceBundle>() {
public ListResourceBundle run() {
try {
return (ListResourceBundle) ResourceBundle.getBundle(bundle, locale);
} catch (MissingResourceException e) {
try {
return (ListResourceBundle) ResourceBundle.getBundle(bundle, new Locale("en", "US"));
} catch (MissingResourceException e2) {
throw new MissingResourceException(
"Could not load any resource bundle by " + bundle, bundle, "");
}
}
}
});
}
/**
* Loads a resources using JRE and returns the names
*/
private static Set<String> loadClass(String clazz) throws Exception {
ListResourceBundle lrb =
(ListResourceBundle)Class.forName(clazz).newInstance();
Set<String> keys = lrb.keySet();
Map<String,String> newold = new HashMap<String,String>();
boolean dup = false;
// Check if normalize() creates dup entries. This is crucial.
for (String k: keys) {
String key = normalize(k);
if (newold.containsKey(key)) {
err("Dup found for " + key + ":");
err("["+newold.get(key)+"]");
err("["+k+"]");
dup = true;
}
newold.put(key, k);
}
if (dup) throw new Exception();
return keys;
}
/**
* Gets a resource bundle using the specified base name and locale, and the caller's class loader.
* @param bundle the base name of the resource bundle, a fully qualified class name
* @param locale the locale for which a resource bundle is desired
* @return a resource bundle for the given base name and locale
*/
public static ListResourceBundle getResourceBundle(final String bundle, final Locale locale) {
return AccessController.doPrivileged(new PrivilegedAction<ListResourceBundle>() {
public ListResourceBundle run() {
try {
return (ListResourceBundle)ResourceBundle.getBundle(bundle, locale);
} catch (MissingResourceException e) {
try {
return (ListResourceBundle)ResourceBundle.getBundle(bundle, new Locale("en", "US"));
} catch (MissingResourceException e2) {
throw new MissingResourceException(
"Could not load any resource bundle by " + bundle, bundle, "");
}
}
}
});
}
/**
* Gets a resource bundle using the specified base name and locale, and the
* caller's class loader.
*
* @param bundle the base name of the resource bundle, a fully qualified
* class name
* @param locale the locale for which a resource bundle is desired
* @return a resource bundle for the given base name and locale
*/
public static ListResourceBundle getResourceBundle(final String bundle, final Locale locale) {
return AccessController.doPrivileged(new PrivilegedAction<ListResourceBundle>() {
public ListResourceBundle run() {
try {
return (ListResourceBundle) ResourceBundle.getBundle(bundle, locale);
} catch (MissingResourceException e) {
try {
return (ListResourceBundle) ResourceBundle.getBundle(bundle, new Locale("en", "US"));
} catch (MissingResourceException e2) {
throw new MissingResourceException(
"Could not load any resource bundle by " + bundle, bundle, "");
}
}
}
});
}
/**
* Loads a resources using JRE and returns the names
*/
private static Set<String> loadClass(String clazz) throws Exception {
ListResourceBundle lrb =
(ListResourceBundle)Class.forName(clazz).newInstance();
Set<String> keys = lrb.keySet();
Map<String,String> newold = new HashMap<String,String>();
boolean dup = false;
// Check if normalize() creates dup entries. This is crucial.
for (String k: keys) {
String key = normalize(k);
if (newold.containsKey(key)) {
err("Dup found for " + key + ":");
err("["+newold.get(key)+"]");
err("["+k+"]");
dup = true;
}
newold.put(key, k);
}
if (dup) throw new Exception();
return keys;
}
/**
* Gets a resource bundle using the specified base name and locale, and the caller's class loader.
* @param bundle the base name of the resource bundle, a fully qualified class name
* @param locale the locale for which a resource bundle is desired
* @return a resource bundle for the given base name and locale
*/
public static ListResourceBundle getResourceBundle(final String bundle, final Locale locale) {
return AccessController.doPrivileged(new PrivilegedAction<ListResourceBundle>() {
public ListResourceBundle run() {
try {
return (ListResourceBundle)ResourceBundle.getBundle(bundle, locale);
} catch (MissingResourceException e) {
try {
return (ListResourceBundle)ResourceBundle.getBundle(bundle, new Locale("en", "US"));
} catch (MissingResourceException e2) {
throw new MissingResourceException(
"Could not load any resource bundle by " + bundle, bundle, "");
}
}
}
});
}
/**
* Gets a resource bundle using the specified base name and locale, and the
* caller's class loader.
*
* @param bundle the base name of the resource bundle, a fully qualified
* class name
* @param locale the locale for which a resource bundle is desired
* @return a resource bundle for the given base name and locale
*/
public static ListResourceBundle getResourceBundle(final String bundle, final Locale locale) {
return AccessController.doPrivileged(new PrivilegedAction<ListResourceBundle>() {
public ListResourceBundle run() {
try {
return (ListResourceBundle) ResourceBundle.getBundle(bundle, locale);
} catch (MissingResourceException e) {
try {
return (ListResourceBundle) ResourceBundle.getBundle(bundle, new Locale("en", "US"));
} catch (MissingResourceException e2) {
throw new MissingResourceException(
"Could not load any resource bundle by " + bundle, bundle, "");
}
}
}
});
}
/**
* Gets a resource bundle using the specified base name and locale, and the caller's class loader.
* @param bundle the base name of the resource bundle, a fully qualified class name
* @param locale the locale for which a resource bundle is desired
* @return a resource bundle for the given base name and locale
*/
public static ListResourceBundle getResourceBundle(final String bundle, final Locale locale) {
return AccessController.doPrivileged(new PrivilegedAction<ListResourceBundle>() {
public ListResourceBundle run() {
try {
return (ListResourceBundle)ResourceBundle.getBundle(bundle, locale);
} catch (MissingResourceException e) {
try {
return (ListResourceBundle)ResourceBundle.getBundle(bundle, new Locale("en", "US"));
} catch (MissingResourceException e2) {
throw new MissingResourceException(
"Could not load any resource bundle by " + bundle, bundle, "");
}
}
}
});
}
@Override
public void onCreateActions(ListResourceBundle resources, final BaseEditor baseEditor, IProgressMonitor monitor) {
IPyEdit edit = (IPyEdit) baseEditor;
edit.addOfflineActionListener("--internal-test-modules", new Action() {
@Override
public void run() {
List<IPythonNature> allPythonNatures = PythonNature.getAllPythonNatures();
StringBuffer buf = new StringBuffer();
try {
for (IPythonNature nature : allPythonNatures) {
buf.append(checkIntegrity(nature, new NullProgressMonitor(), true));
}
} catch (MisconfigurationException e) {
buf.append(e.getMessage());
}
PyDialogHelpers.showString(buf.toString());
}
}, "Used just for testing (do not use).", true);
}
/**
* Gets a resource bundle using the specified base name and locale, and the caller's class loader.
* @param bundle the base name of the resource bundle, a fully qualified class name
* @param locale the locale for which a resource bundle is desired
* @return a resource bundle for the given base name and locale
*/
public static ListResourceBundle getResourceBundle(final String bundle, final Locale locale) {
return AccessController.doPrivileged(new PrivilegedAction<ListResourceBundle>() {
public ListResourceBundle run() {
try {
return (ListResourceBundle)ResourceBundle.getBundle(bundle, locale);
} catch (MissingResourceException e) {
try {
return (ListResourceBundle)ResourceBundle.getBundle(bundle, new Locale("en", "US"));
} catch (MissingResourceException e2) {
throw new MissingResourceException(
"Could not load any resource bundle by " + bundle, bundle, "");
}
}
}
});
}
/**
* Gets a resource bundle using the specified base name and locale, and the
* caller's class loader.
*
* @param bundle the base name of the resource bundle, a fully qualified
* class name
* @param locale the locale for which a resource bundle is desired
* @return a resource bundle for the given base name and locale
*/
public static ListResourceBundle getResourceBundle(final String bundle, final Locale locale) {
return AccessController.doPrivileged(new PrivilegedAction<ListResourceBundle>() {
public ListResourceBundle run() {
try {
return (ListResourceBundle) ResourceBundle.getBundle(bundle, locale);
} catch (MissingResourceException e) {
try {
return (ListResourceBundle) ResourceBundle.getBundle(bundle, new Locale("en", "US"));
} catch (MissingResourceException e2) {
throw new MissingResourceException(
"Could not load any resource bundle by " + bundle, bundle, "");
}
}
}
});
}
/**
* Loads a resources using JRE and returns the names
*/
private static Set<String> loadClass(String clazz) throws Exception {
ListResourceBundle lrb =
(ListResourceBundle)Class.forName(clazz).newInstance();
Set<String> keys = lrb.keySet();
Map<String,String> newold = new HashMap<String,String>();
boolean dup = false;
// Check if normalize() creates dup entries. This is crucial.
for (String k: keys) {
String key = normalize(k);
if (newold.containsKey(key)) {
err("Dup found for " + key + ":");
err("["+newold.get(key)+"]");
err("["+k+"]");
dup = true;
}
newold.put(key, k);
}
if (dup) throw new Exception();
return keys;
}
/**
* Notifies listeners that the actions have just been created in the editor.
*/
public void notifyOnCreateActions(final ListResourceBundle resources) {
final BaseEditor edit = pyEdit.get();
if (edit == null) {
return;
}
INotifierRunnable runnable = new INotifierRunnable() {
@Override
public void run(final IProgressMonitor monitor) {
for (IPyEditListener listener : edit.getAllListeners()) {
try {
if (!monitor.isCanceled()) {
listener.onCreateActions(resources, edit, monitor);
}
} catch (Exception e) {
//must not fail
Log.log(e);
}
}
}
};
runIt(runnable);
}
/**
* Loads a resources using JRE and returns the names
*/
private static Set<String> loadClass(String clazz) throws Exception {
ListResourceBundle lrb =
(ListResourceBundle)Class.forName(clazz).newInstance();
Set<String> keys = lrb.keySet();
Map<String,String> newold = new HashMap<String,String>();
boolean dup = false;
// Check if normalize() creates dup entries. This is crucial.
for (String k: keys) {
String key = normalize(k);
if (newold.containsKey(key)) {
err("Dup found for " + key + ":");
err("["+newold.get(key)+"]");
err("["+k+"]");
dup = true;
}
newold.put(key, k);
}
if (dup) throw new Exception();
return keys;
}
/**
* Gets a resource bundle using the specified base name and locale, and the
* caller's class loader.
*
* @param bundle the base name of the resource bundle, a fully qualified
* class name
* @param locale the locale for which a resource bundle is desired
* @return a resource bundle for the given base name and locale
*/
public static ListResourceBundle getResourceBundle(final String bundle, final Locale locale) {
return AccessController.doPrivileged(new PrivilegedAction<ListResourceBundle>() {
public ListResourceBundle run() {
try {
return (ListResourceBundle) ResourceBundle.getBundle(bundle, locale);
} catch (MissingResourceException e) {
try {
return (ListResourceBundle) ResourceBundle.getBundle(bundle, new Locale("en", "US"));
} catch (MissingResourceException e2) {
throw new MissingResourceException(
"Could not load any resource bundle by " + bundle, bundle, "");
}
}
}
});
}
@Override
public void onCreateActions(ListResourceBundle resources, BaseEditor baseEditor, IProgressMonitor monitor) {
//Note: can be called multiple times (will only create handlers for the new actions).
Map<String, InteractiveCommandCustomHandler> commandIdToHandler = InteractiveConsoleCommand
.getCommandIdToHandler();
Set<Entry<String, InteractiveCommandCustomHandler>> entrySet = commandIdToHandler.entrySet();
for (Entry<String, InteractiveCommandCustomHandler> entry : entrySet) {
if (idToAction.containsKey(entry.getKey())) {
continue;
}
InteractiveConsoleUserCommandAction action = new InteractiveConsoleUserCommandAction(resources,
"Pyedit.InteractiveConsoleUserCommand", baseEditor, entry.getKey());
action.setActionDefinitionId(entry.getKey());
action.setId(entry.getKey());
baseEditor.setAction(FullRepIterable.getLastPart(entry.getKey()), action);
idToAction.put(entry.getKey(), action);
}
this.resources = resources;
this.weakEditor = new WeakReference<>(baseEditor);
InteractiveConsoleCommand.onCommandIdToHandlerChanged.registerListener(onCommandIdToHandlerChangedCallback);
}
/**
* Loads a resources using JRE and returns the names
*/
private static Set<String> loadClass(String clazz) throws Exception {
ListResourceBundle lrb =
(ListResourceBundle)Class.forName(clazz).newInstance();
Set<String> keys = lrb.keySet();
Map<String,String> newold = new HashMap<String,String>();
boolean dup = false;
// Check if normalize() creates dup entries. This is crucial.
for (String k: keys) {
String key = normalize(k);
if (newold.containsKey(key)) {
err("Dup found for " + key + ":");
err("["+newold.get(key)+"]");
err("["+k+"]");
dup = true;
}
newold.put(key, k);
}
if (dup) throw new Exception();
return keys;
}
/**
* Gets a resource bundle using the specified base name and locale, and the caller's class loader.
* @param bundle the base name of the resource bundle, a fully qualified class name
* @param locale the locale for which a resource bundle is desired
* @return a resource bundle for the given base name and locale
*/
public static ListResourceBundle getResourceBundle(final String bundle, final Locale locale) {
return AccessController.doPrivileged(new PrivilegedAction<ListResourceBundle>() {
public ListResourceBundle run() {
try {
return (ListResourceBundle)ResourceBundle.getBundle(bundle, locale);
} catch (MissingResourceException e) {
try {
return (ListResourceBundle)ResourceBundle.getBundle(bundle, new Locale("en", "US"));
} catch (MissingResourceException e2) {
throw new MissingResourceException(
"Could not load any resource bundle by " + bundle, bundle, "");
}
}
}
});
}
/**
* Gets a resource bundle using the specified base name and locale, and the
* caller's class loader.
*
* @param bundle the base name of the resource bundle, a fully qualified
* class name
* @param locale the locale for which a resource bundle is desired
* @return a resource bundle for the given base name and locale
*/
public static ListResourceBundle getResourceBundle(final String bundle, final Locale locale) {
return AccessController.doPrivileged(new PrivilegedAction<ListResourceBundle>() {
public ListResourceBundle run() {
try {
return (ListResourceBundle) ResourceBundle.getBundle(bundle, locale);
} catch (MissingResourceException e) {
try {
return (ListResourceBundle) ResourceBundle.getBundle(bundle, new Locale("en", "US"));
} catch (MissingResourceException e2) {
throw new MissingResourceException(
"Could not load any resource bundle by " + bundle, bundle, "");
}
}
}
});
}
/**
* Gets a resource bundle using the specified base name and locale, and the caller's class loader.
* @param bundle the base name of the resource bundle, a fully qualified class name
* @param locale the locale for which a resource bundle is desired
* @return a resource bundle for the given base name and locale
*/
public static ListResourceBundle getResourceBundle(final String bundle, final Locale locale) {
return AccessController.doPrivileged(new PrivilegedAction<ListResourceBundle>() {
public ListResourceBundle run() {
try {
return (ListResourceBundle)ResourceBundle.getBundle(bundle, locale);
} catch (MissingResourceException e) {
try {
return (ListResourceBundle)ResourceBundle.getBundle(bundle, new Locale("en", "US"));
} catch (MissingResourceException e2) {
throw new MissingResourceException(
"Could not load any resource bundle by " + bundle, bundle, "");
}
}
}
});
}
/**
* Gets a resource bundle using the specified base name and locale, and the
* caller's class loader.
*
* @param bundle the base name of the resource bundle, a fully qualified
* class name
* @param locale the locale for which a resource bundle is desired
* @return a resource bundle for the given base name and locale
*/
public static ListResourceBundle getResourceBundle(final String bundle, final Locale locale) {
return AccessController.doPrivileged(new PrivilegedAction<ListResourceBundle>() {
public ListResourceBundle run() {
try {
return (ListResourceBundle) ResourceBundle.getBundle(bundle, locale);
} catch (MissingResourceException e) {
try {
return (ListResourceBundle) ResourceBundle.getBundle(bundle, new Locale("en", "US"));
} catch (MissingResourceException e2) {
throw new MissingResourceException(
"Could not load any resource bundle by " + bundle, bundle, "");
}
}
}
});
}
@Before
public void setUp() {
request = mock(HttpServletRequest.class);
servletContext = mock(ServletContext.class);
session = mock(HttpSession.class);
localization = new JstlLocalization(request);
ResourceBundle bundle = new ListResourceBundle() {
@Override
protected Object[][] getContents() {
return new Object[][] { { "my.key", "abc" } };
}
};
LocalizationContext context = new LocalizationContext(bundle);
when(request.getAttribute(FMT_LOCALIZATION_CONTEXT + ".request")).thenReturn(context);
when(request.getSession(false)).thenReturn(session);
when(request.getServletContext()).thenReturn(servletContext);
}
protected ExpressionRuntime createRuntime() throws ReportProcessingException {
ResourceBundle b = new ListResourceBundle() {
protected Object[][] getContents() {
return new Object[][] { { "format", "$(date,date,yyyy-MM-dd'T'HH:mm:ss,SSSZZZ)" } };
}
};
ResourceBundleFactory f = Mockito.mock( ResourceBundleFactory.class );
Mockito.when( f.getLocale() ).thenReturn( Locale.US );
Mockito.when( f.getTimeZone() ).thenReturn( TimeZone.getTimeZone( "PST" ) );
Mockito.when( f.getResourceBundle( "test" ) ).thenReturn( b );
ProcessingContext pc = Mockito.mock( ProcessingContext.class );
Mockito.when( pc.getResourceBundleFactory() ).thenReturn( f );
DataRow r = Mockito.mock( DataRow.class );
Mockito.when( r.get( "number" ) ).thenReturn( new Double( 123456.78901 ) );
Mockito.when( r.get( "date" ) ).thenReturn( new Date( 1234567890123l ) );
return new DebugExpressionRuntime( r, new DefaultTableModel(), 0, pc );
}
/**
* This method associates Ctrl+new line with the evaluation of commands in the console.
*/
@Override
public void onCreateActions(ListResourceBundle resources, final BaseEditor baseEditor, IProgressMonitor monitor) {
final PyEdit edit = (PyEdit) baseEditor;
final EvaluateAction evaluateAction = new EvaluateAction(edit);
evaluateAction.setActionDefinitionId(IInteractiveConsoleConstants.EVALUATE_ACTION_ID);
evaluateAction.setId(IInteractiveConsoleConstants.EVALUATE_ACTION_ID);
Runnable runnable = new Runnable() {
@Override
public void run() {
if (!edit.isDisposed()) {
edit.setAction(IInteractiveConsoleConstants.EVALUATE_ACTION_ID, evaluateAction);
}
}
};
Display.getDefault().syncExec(runnable);
}
/**
* Loads a resources using JRE and returns the names
*/
private static Set<String> loadClass(String clazz) throws Exception {
ListResourceBundle lrb =
(ListResourceBundle)Class.forName(clazz).newInstance();
Set<String> keys = lrb.keySet();
Map<String,String> newold = new HashMap<String,String>();
boolean dup = false;
// Check if normalize() creates dup entries. This is crucial.
for (String k: keys) {
String key = normalize(k);
if (newold.containsKey(key)) {
err("Dup found for " + key + ":");
err("["+newold.get(key)+"]");
err("["+k+"]");
dup = true;
}
newold.put(key, k);
}
if (dup) throw new Exception();
return keys;
}
/**
* Loads a resources using JRE and returns the names
*/
private static Set<String> loadClass(String clazz) throws Exception {
ListResourceBundle lrb =
(ListResourceBundle)Class.forName(clazz).newInstance();
Set<String> keys = lrb.keySet();
Map<String,String> newold = new HashMap<String,String>();
boolean dup = false;
// Check if normalize() creates dup entries. This is crucial.
for (String k: keys) {
String key = normalize(k);
if (newold.containsKey(key)) {
err("Dup found for " + key + ":");
err("["+newold.get(key)+"]");
err("["+k+"]");
dup = true;
}
newold.put(key, k);
}
if (dup) throw new Exception();
return keys;
}
/**
* Loads a resources using JRE and returns the names
*/
private static Set<String> loadClass(String clazz) throws Exception {
ListResourceBundle lrb =
(ListResourceBundle)Class.forName(clazz).newInstance();
Set<String> keys = lrb.keySet();
Map<String,String> newold = new HashMap<String,String>();
boolean dup = false;
// Check if normalize() creates dup entries. This is crucial.
for (String k: keys) {
String key = normalize(k);
if (newold.containsKey(key)) {
err("Dup found for " + key + ":");
err("["+newold.get(key)+"]");
err("["+k+"]");
dup = true;
}
newold.put(key, k);
}
if (dup) throw new Exception();
return keys;
}
private static ResourceBundle findSystemResourceBundle(final Locale locale) {
// J2ObjC: inlined contents of sun/util/logging/resources/logging/logging.properties
return new ListResourceBundle() {
@Override
protected Object[][] getContents() {
return new Object[][] {
{ "ALL", "ALL" },
{ "SEVERE", "SEVERE" },
{ "WARNING", "WARNING" },
{ "INFO", "INFO" },
{ "CONFIG", "CONFIG" },
{ "FINE", "FINE" },
{ "FINER", "FINER" },
{ "FINEST", "FINEST" },
{ "OFF", "OFF" }
};
}
};
}
@Provides @Named(SetSecurityAnswersHandler.NAME_RESOURCE_BUNDLE)
public ResourceBundle stubSecurityQuestionBundle() {
return new ListResourceBundle()
{
@Override
protected Object[][] getContents()
{
return new Object[][] {
{ "questionA", "Mother's maiden name?" },
{ "questionB", "Father's middle name?" },
{ "questionC", "First car?" },
};
}
};
}