localizedtextutil.java
来自「在Struts2中的jar包xwork的源代码.版本为2.0.7」· Java 代码 · 共 806 行 · 第 1/3 页
JAVA
806 行
* Finds a localized text message for the given key, aTextName, in the specified resource * bundle. * <p/> * If a message is found, it will also be interpolated. Anything within <code>${...}</code> * will be treated as an OGNL expression and evaluated as such. * <p/> * If a message is <b>not</b> found a WARN log will be logged. * * @param bundle the bundle * @param aTextName the key * @param locale the locale * @param defaultMessage the default message to use if no message was found in the bundle * @param args arguments for the message formatter. * @param valueStack the OGNL value stack. */ public static String findText(ResourceBundle bundle, String aTextName, Locale locale, String defaultMessage, Object[] args, ValueStack valueStack) { try { reloadBundles(); String message = TextParseUtil.translateVariables(bundle.getString(aTextName), valueStack); MessageFormat mf = buildMessageFormat(message, locale); return mf.format(args); } catch (MissingResourceException ex) { // ignore } GetDefaultMessageReturnArg result = getDefaultMessage(aTextName, locale, valueStack, args, defaultMessage); if (unableToFindTextForKey(result)) { LOG.warn("Unable to find text for key '" + aTextName + "' in ResourceBundles for locale '" + locale + "'"); } return result.message; } /** * Gets the default message. */ private static GetDefaultMessageReturnArg getDefaultMessage(String key, Locale locale, ValueStack valueStack, Object[] args, String defaultMessage) { GetDefaultMessageReturnArg result = null; boolean found = true; if (key != null) { String message = findDefaultText(key, locale); if (message == null) { message = defaultMessage; found = false; // not found in bundles } // defaultMessage may be null if (message != null) { MessageFormat mf = buildMessageFormat(TextParseUtil.translateVariables(message, valueStack), locale); String msg = mf.format(args); result = new GetDefaultMessageReturnArg(msg, found); } } return result; } /** * Gets the message from the named resource bundle. */ private static String getMessage(String bundleName, Locale locale, String key, ValueStack valueStack, Object[] args) { ResourceBundle bundle = findResourceBundle(bundleName, locale); if (bundle == null) { return null; } reloadBundles(); try { String message = TextParseUtil.translateVariables(bundle.getString(key), valueStack); MessageFormat mf = buildMessageFormat(message, locale); return mf.format(args); } catch (MissingResourceException e) { return null; } } private static MessageFormat buildMessageFormat(String pattern, Locale locale) { MessageFormatKey key = new MessageFormatKey(pattern, locale); MessageFormat format = (MessageFormat) messageFormats.get(key); if (format == null) { format = new MessageFormat(pattern); format.setLocale(locale); format.applyPattern(pattern); messageFormats.put(key, format); } return format; } /** * Traverse up class hierarchy looking for message. Looks at class, then implemented interface, * before going up hierarchy. */ private static String findMessage(Class clazz, String key, String indexedKey, Locale locale, Object[] args, Set checked, ValueStack valueStack) { if (checked == null) { checked = new TreeSet(); } else if (checked.contains(clazz.getName())) { return null; } // look in properties of this class String msg = getMessage(clazz.getName(), locale, key, valueStack, args); if (msg != null) { return msg; } if (indexedKey != null) { msg = getMessage(clazz.getName(), locale, indexedKey, valueStack, args); if (msg != null) { return msg; } } // look in properties of implemented interfaces Class[] interfaces = clazz.getInterfaces(); for (int x = 0; x < interfaces.length; x++) { msg = getMessage(interfaces[x].getName(), locale, key, valueStack, args); if (msg != null) { return msg; } if (indexedKey != null) { msg = getMessage(interfaces[x].getName(), locale, indexedKey, valueStack, args); if (msg != null) { return msg; } } } // traverse up hierarchy if (clazz.isInterface()) { interfaces = clazz.getInterfaces(); for (int x = 0; x < interfaces.length; x++) { msg = findMessage(interfaces[x], key, indexedKey, locale, args, checked, valueStack); if (msg != null) { return msg; } } } else { if (!clazz.equals(Object.class) && !clazz.isPrimitive()) { return findMessage(clazz.getSuperclass(), key, indexedKey, locale, args, checked, valueStack); } } return null; } private static void reloadBundles() { if (reloadBundles) { try { clearMap(ResourceBundle.class, null, "cacheList"); // now, for the true and utter hack, if we're running in tomcat, clear // it's class loader resource cache as well. clearTomcatCache(); } catch (Exception e) { LOG.error("Could not reload resource bundles", e); } } } private static void clearTomcatCache() { ClassLoader loader = Thread.currentThread().getContextClassLoader(); // no need for compilation here. Class cl = loader.getClass(); try { if ("org.apache.catalina.loader.WebappClassLoader".equals(cl.getName())) { clearMap(cl, loader, "resourceEntries"); } else { if (LOG.isDebugEnabled()) { LOG.debug("class loader " + cl.getName() + " is not tomcat loader."); } } } catch (Exception e) { LOG.warn("couldn't clear tomcat cache", e); } } private static void clearMap(Class cl, Object obj, String name) throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { Field field = cl.getDeclaredField(name); field.setAccessible(true); Object cache = field.get(obj); synchronized (cache) { Class ccl = cache.getClass(); Method clearMethod = ccl.getMethod("clear"); clearMethod.invoke(cache); } } /** * Clears all the internal lists. */ public static void reset() { clearDefaultResourceBundles(); synchronized (misses) { misses.clear(); } synchronized (messageFormats) { messageFormats.clear(); } } static class MessageFormatKey { String pattern; Locale locale; MessageFormatKey(String pattern, Locale locale) { this.pattern = pattern; this.locale = locale; } public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof MessageFormatKey)) return false; final MessageFormatKey messageFormatKey = (MessageFormatKey) o; if (locale != null ? !locale.equals(messageFormatKey.locale) : messageFormatKey.locale != null) return false; if (pattern != null ? !pattern.equals(messageFormatKey.pattern) : messageFormatKey.pattern != null) return false; return true; } public int hashCode() { int result; result = (pattern != null ? pattern.hashCode() : 0); result = 29 * result + (locale != null ? locale.hashCode() : 0); return result; } } static class GetDefaultMessageReturnArg { String message; boolean foundInBundle; public GetDefaultMessageReturnArg(String message, boolean foundInBundle) { this.message = message; this.foundInBundle = foundInBundle; } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?