localizedtextutil.java
来自「在Struts2中的jar包xwork的源代码.版本为2.0.7」· Java 代码 · 共 806 行 · 第 1/3 页
JAVA
806 行
* object. If so, repeat the entire process fromthe beginning with the object's class as * aClass and "address.state" as the message key.</li> * <li>If not found, look for the message in aClass' package hierarchy.</li> * <li>If still not found, look for the message in the default resource bundles.</li> * <li>Return defaultMessage</li> * </ol> * <p/> * When looking for the message, if the key indexes a collection (e.g. user.phone[0]) and a * message for that specific key cannot be found, the general form will also be looked up * (i.e. user.phone[*]). * <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. * * @param aClass the class whose name to use as the start point for the search * @param aTextName the key to find the text message for * @param locale the locale the message should be for * @param defaultMessage the message to be returned if no text message can be found in any * resource bundle * @return the localized text, or null if none can be found and no defaultMessage is provided */ public static String findText(Class aClass, String aTextName, Locale locale, String defaultMessage, Object[] args) { ValueStack valueStack = ActionContext.getContext().getValueStack(); return findText(aClass, aTextName, locale, defaultMessage, args, valueStack); } /** * Finds a localized text message for the given key, aTextName. Both the key and the message * itself is evaluated as required. The following algorithm is used to find the requested * message: * <p/> * <ol> * <li>Look for message in aClass' class hierarchy. * <ol> * <li>Look for the message in a resource bundle for aClass</li> * <li>If not found, look for the message in a resource bundle for any implemented interface</li> * <li>If not found, traverse up the Class' hierarchy and repeat from the first sub-step</li> * </ol></li> * <li>If not found and aClass is a {@link ModelDriven} Action, then look for message in * the model's class hierarchy (repeat sub-steps listed above).</li> * <li>If not found, look for message in child property. This is determined by evaluating * the message key as an OGNL expression. For example, if the key is * <i>user.address.state</i>, then it will attempt to see if "user" can be resolved into an * object. If so, repeat the entire process fromthe beginning with the object's class as * aClass and "address.state" as the message key.</li> * <li>If not found, look for the message in aClass' package hierarchy.</li> * <li>If still not found, look for the message in the default resource bundles.</li> * <li>Return defaultMessage</li> * </ol> * <p/> * When looking for the message, if the key indexes a collection (e.g. user.phone[0]) and a * message for that specific key cannot be found, the general form will also be looked up * (i.e. user.phone[*]). * <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 aClass the class whose name to use as the start point for the search * @param aTextName the key to find the text message for * @param locale the locale the message should be for * @param defaultMessage the message to be returned if no text message can be found in any * resource bundle * @param valueStack the value stack to use to evaluate expressions instead of the * one in the ActionContext ThreadLocal * @return the localized text, or null if none can be found and no defaultMessage is provided */ public static String findText(Class aClass, String aTextName, Locale locale, String defaultMessage, Object[] args, ValueStack valueStack) { String indexedTextName = null; if (aTextName == null) { LOG.warn("Trying to find text with null key!"); aTextName = ""; } // calculate indexedTextName (collection[*]) if applicable if (aTextName.indexOf("[") != -1) { int i = -1; indexedTextName = aTextName; while ((i = indexedTextName.indexOf("[", i + 1)) != -1) { int j = indexedTextName.indexOf("]", i); String a = indexedTextName.substring(0, i); String b = indexedTextName.substring(j); indexedTextName = a + "[*" + b; } } // search up class hierarchy String msg = findMessage(aClass, aTextName, indexedTextName, locale, args, null, valueStack); if (msg != null) { return msg; } if (ModelDriven.class.isAssignableFrom(aClass)) { ActionContext context = ActionContext.getContext(); // search up model's class hierarchy ActionInvocation actionInvocation = context.getActionInvocation(); // ActionInvocation may be null if we're being run from a Sitemesh filter, so we won't get model texts if this is null if (actionInvocation != null) { Object action = actionInvocation.getAction(); if (action instanceof ModelDriven) { Object model = ((ModelDriven) action).getModel(); if (model != null) { msg = findMessage(model.getClass(), aTextName, indexedTextName, locale, args, null, valueStack); if (msg != null) { return msg; } } } } } // nothing still? alright, search the package hierarchy now for (Class clazz = aClass; (clazz != null) && !clazz.equals(Object.class); clazz = clazz.getSuperclass()) { String basePackageName = clazz.getName(); while (basePackageName.lastIndexOf('.') != -1) { basePackageName = basePackageName.substring(0, basePackageName.lastIndexOf('.')); String packageName = basePackageName + ".package"; msg = getMessage(packageName, locale, aTextName, valueStack, args); if (msg != null) { return msg; } if (indexedTextName != null) { msg = getMessage(packageName, locale, indexedTextName, valueStack, args); if (msg != null) { return msg; } } } } // see if it's a child property int idx = aTextName.indexOf("."); if (idx != -1) { String newKey = null; String prop = null; if (aTextName.startsWith(XWorkConverter.CONVERSION_ERROR_PROPERTY_PREFIX)) { idx = aTextName.indexOf(".", XWorkConverter.CONVERSION_ERROR_PROPERTY_PREFIX.length()); if (idx != -1) { prop = aTextName.substring(XWorkConverter.CONVERSION_ERROR_PROPERTY_PREFIX.length(), idx); newKey = XWorkConverter.CONVERSION_ERROR_PROPERTY_PREFIX + aTextName.substring(idx + 1); } } else { prop = aTextName.substring(0, idx); newKey = aTextName.substring(idx + 1); } if (prop != null) { Object obj = valueStack.findValue(prop); try { Object actionObj = OgnlUtil.getRealTarget(prop, valueStack.getContext(), valueStack.getRoot()); if (actionObj != null) { PropertyDescriptor propertyDescriptor = OgnlRuntime.getPropertyDescriptor(actionObj.getClass(), prop); if (propertyDescriptor != null) { Class clazz=propertyDescriptor.getPropertyType(); if (clazz != null) { if (obj != null) valueStack.push(obj); msg = findText(clazz, newKey, locale, null, args); if (obj != null) valueStack.pop(); if (msg != null) { return msg; } } } } } catch(Exception e) { _log.debug("unable to find property "+prop, e); } } } // get default GetDefaultMessageReturnArg result = null; if (indexedTextName == null) { result = getDefaultMessage(aTextName, locale, valueStack, args, defaultMessage); } else { result = getDefaultMessage(aTextName, locale, valueStack, args, null); if (result.message != null) { return result.message; } result = getDefaultMessage(indexedTextName, locale, valueStack, args, defaultMessage); } // could we find the text, if not log a warn if (unableToFindTextForKey(result)) { String warn = "Unable to find text for key '" + aTextName + "' "; if (indexedTextName != null) { warn += " or indexed key '" + indexedTextName + "' "; } warn += "in class '" + aClass.getName() + "' and locale '" + locale + "'"; LOG.debug(warn); } return result != null ? result.message : null; } /** * Determines if we found the text in the bundles. * * @param result the result so far * @return <tt>true</tt> if we could <b>not</b> find the text, <tt>false</tt> if the text was found (=success). */ private static boolean unableToFindTextForKey(GetDefaultMessageReturnArg result) { if (result == null || result.message == null) { return true; } // did we find it in the bundle, then no problem? if (result.foundInBundle) { return false; } // not found in bundle return true; } /** * Finds a localized text message for the given key, aTextName, in the specified resource bundle * with aTextName as the default message. * <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. * * @see #findText(java.util.ResourceBundle, String, java.util.Locale, String, Object[]) */ public static String findText(ResourceBundle bundle, String aTextName, Locale locale) { return findText(bundle, aTextName, locale, aTextName, new Object[0]); } /** * 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. */ public static String findText(ResourceBundle bundle, String aTextName, Locale locale, String defaultMessage, Object[] args) { ValueStack valueStack = ActionContext.getContext().getValueStack(); return findText(bundle, aTextName, locale, defaultMessage, args, valueStack); } /**
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?