Commits

Joe Xie  committed 04eb4aa

CONF-30205 : fix up the bug where replacements are not properly replaced.

  • Participants
  • Parent commits e69ef30
  • Branches xwork_1-0-3, xwork_1-0-3_branch
  • Tags xwork_1-0-3-6

Comments (0)

Files changed (2)

File src/java/com/opensymphony/xwork/util/TextParseUtil.java

 package com.opensymphony.xwork.util;
 
 
-import java.util.HashMap;
-import java.util.Map;
-import java.util.concurrent.Callable;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 /**
  * Utility class for text parsing.
      * @return the parsed expression
      */
     public static String translateVariables(String expression, final OgnlValueStack stack) {
-        Map<String, Callable<String>> variables = extractOgnlVariables(expression, stack);
-        for (Map.Entry<String, Callable<String>> variableEntry : variables.entrySet()) {
-            String result;
+        StringBuilder sb = new StringBuilder();
+        Pattern p = Pattern.compile("\\$\\{([^}]*)\\}");
+        Matcher m = p.matcher(expression);
+        int previous = 0;
+        while (m.find()) {
+            String g = m.group(1);
+            int start = m.start();
+            String value;
             try {
-                result = variableEntry.getValue().call();
-                expression = expression.replace("${" + variableEntry.getKey() +"}", result);
-            } catch (Exception e) {
-                //
-                continue;
+                Object o = stack.findValue(g);
+                value = o == null ? "" : o.toString();
+            } catch (Exception ignored) {
+                value = "";
             }
+            sb.append(expression.substring(previous, start)).append(value);
+            previous = m.end();
         }
-
-        return expression;
-    }
-
-    private static Map<String, Callable<String>> extractOgnlVariables(String expression, final OgnlValueStack stack) {
-        Map<String, Callable<String>> variables = new HashMap<String, Callable<String>>();
-        while (true) {
-            int x = expression.indexOf("${");
-            int y = expression.indexOf("}", x);
-
-            if ((x != -1) && (y != -1)) {
-                final String var = expression.substring(x + 2, y);
-                variables.put(var, new Callable<String>() {
-                    public String call() throws Exception {
-                        Object o = stack.findValue(var);
-                        return o == null ? "" : o.toString();
-                    }
-                });
-
-                expression = expression.substring(y, expression.length());
-            } else {
-                break;
-            }
+        if (previous < expression.length() ) {
+            sb.append(expression.substring(previous));
         }
-        return variables;
+        return sb.toString();
     }
 }

File src/test/com/opensymphony/xwork/util/TextParseUtilTest.java

         final Dog dog = new Dog();
         final Cat cat = new Cat();
         cat.setName("${name}");
-        dog.setName("${age}");
+        dog.setName("${age}\\0");
         dog.setAge(10);
         dog.setHates(cat);
         stack.push(dog);
-        String result = TextParseUtil.translateVariables("a${name}b${hates.name}${}${fish}${age}", stack);
-        assertEquals("a${age}b${name}10", result);
+        String result = TextParseUtil.translateVariables("a${name}b${hates.name}${}${fish}${age}${age}${a", stack);
+        assertEquals("a${age}\\0b${name}1010${a", result);
     }
 }