Commits

Anonymous committed 56364af

XW-721 Ability to convert Struts2 field error messages that are arrays using global-messages.properties

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@2058e221344d-f017-0410-9bd5-d282ab1896d7

Comments (0)

Files changed (1)

core/src/main/java/com/opensymphony/xwork2/conversion/impl/XWorkConverter.java

 import java.lang.reflect.Method;
 import java.net.URL;
 import java.util.*;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.text.MessageFormat;
 
 
 /**
 
     public static final String LAST_BEAN_CLASS_ACCESSED = "last.bean.accessed";
     public static final String LAST_BEAN_PROPERTY_ACCESSED = "last.property.accessed";
+    public static final String MESSAGE_INDEX_PATTERN = "\\[\\d+\\]\\.";
+    public static final String MESSAGE_INDEX_BRACKET_PATTERN = "[\\[\\]\\.]";
+    public static final String PERIOD = ".";
+    public static final Pattern messageIndexPattern = Pattern.compile(MESSAGE_INDEX_PATTERN); 
 
     /**
      * Target class conversion Mappings.
                 new Object[]{
                         propertyName
                 });
+
+        List<String> indexValues = getIndexValues(propertyName);
+
+        propertyName = removeAllIndexesInProperyName(propertyName);
+
         String getTextExpression = "getText('" + CONVERSION_ERROR_PROPERTY_PREFIX + propertyName + "','" + defaultMessage + "')";
         String message = (String) stack.findValue(getTextExpression);
 
         if (message == null) {
             message = defaultMessage;
+        } else {
+            message = MessageFormat.format(message, indexValues.toArray());
         }
 
         return message;
     }
 
+    private static String removeAllIndexesInProperyName(String propertyName) {
+        return propertyName.replaceAll(MESSAGE_INDEX_PATTERN, PERIOD);
+    }
+
+    private static List<String> getIndexValues(String propertyName) {
+        Matcher matcher = messageIndexPattern.matcher(propertyName);
+        List<String> indexes = new ArrayList<String>();
+        while (matcher.find()) {
+            Integer index = new Integer(matcher.group().replaceAll(MESSAGE_INDEX_BRACKET_PATTERN, "")) + 1;
+            indexes.add(Integer.toString(index));
+        }
+        return indexes;
+    }
 
     public static String buildConverterFilename(Class clazz) {
         String className = clazz.getName();