Commits

Anonymous committed 8de2aaa

Issue number: WW-1315
Obtained from:
Submitted by:
Reviewed by:

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

  • Participants
  • Parent commits d562d19

Comments (0)

Files changed (2)

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

  *
  * @author Jason Carreira
  * @author Rainer Hermanns
+ * @author tm_jee
+ * 
+ * @version $Date$ $Id$
  */
 public class TextParseUtil {
 
      * @return the parsed expression
      */
     public static String translateVariables(String expression, OgnlValueStack stack) {
-        return translateVariables('$', expression, stack, String.class).toString();
+        return translateVariables('$', expression, stack, String.class, null).toString();
+    }
+    
+    
+    /**
+     * Function similarly as {@link #translateVariables(char, String, OgnlValueStack)} 
+     * except for the introduction of an additional <code>evaluator</code> that allows
+     * the parsed value to be evaluated by the <code>evaluator</code>. The <code>evaluator</code>
+     * could be null, if it is it will just be skipped as if it is just calling 
+     * {@link #translateVariables(char, String, OgnlValueStack)}.
+     * 
+     * <p/>
+     * 
+     * A typical use-case would be when we need to URL Encode the parsed value. To do so 
+     * we could just supply a URLEncodingEvaluator for example. 
+     * 
+     * @see {@link TextParseUtil.ParsedValueEvaluator}
+     * @param expression
+     * @param stack
+     * @param evaluator The parsed Value evaluator (could be null).
+     * @return the parsed (and possibly evaluated) variable String.
+     */
+    public static String translateVariables(String expression, OgnlValueStack stack, ParsedValueEvaluator evaluator) {
+    	return translateVariables('$', expression, stack, String.class, evaluator).toString();
     }
 
     /**
      * @return Translated variable String
      */
     public static String translateVariables(char open, String expression, OgnlValueStack stack) {
-        return translateVariables(open, expression, stack, String.class).toString();
+        return translateVariables(open, expression, stack, String.class, null).toString();
     }
 
     /**
      * @return Converted object from variable translation.
      */
     public static Object translateVariables(char open, String expression, OgnlValueStack stack, Class asType) {
+    	return translateVariables(open, expression, stack, asType, null);
+    }
+    
+    /**
+     * Converted object from variable translation.
+     *
+     * @param open
+     * @param expression
+     * @param stack
+     * @param asType
+     * @param evaluator
+     * @return Converted object from variable translation.
+     */
+    public static Object translateVariables(char open, String expression, OgnlValueStack stack, Class asType, ParsedValueEvaluator evaluator) {
         // deal with the "pure" expressions first!
         //expression = expression.trim();
         Object result = expression;
                 String var = expression.substring(start + 2, end);
 
                 Object o = stack.findValue(var, asType);
+                if (evaluator != null) {
+                	o = evaluator.evaluate(o);
+                }
+                
 
                 String left = expression.substring(0, start);
                 String right = expression.substring(end + 1);
         }
         return set;
     }
+    
+    
+    /**
+     * A parsed value evaluator for {@link TextParseUtil}. It could be supplied by 
+     * calling {@link TextParseUtil#translateVariables(char, String, OgnlValueStack, Class, ParsedValueEvaluator)}.
+     * 
+     * <p/>
+     * 
+     * By supplying this <code>ParsedValueEvaluator</code>, the parsed value will be
+     * given to <code>ParsedValueEvaluator</code> to be evaluated before the 
+     * translateVariable process goes on. 
+     * 
+     * <p/>
+     * 
+     * A typical use-case would be to have a custom <code>ParseValueEvaluator</code>
+     * to URL Encode the parsed value.
+     * 
+     * @author tm_jee
+     * 
+     * @version $Date$ $Id$
+     */
+    public static interface ParsedValueEvaluator {
+    	Object evaluate(Object parsedValue);
+    }
 }

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

 import java.util.List;
 import java.util.HashSet;
 import java.util.Arrays;
+import java.util.Map;
 
 /**
  * Unit test of {@link TextParseUtil}.
  *
  * @author plightbo
+ * @author tm_jee
+ * 
+ * @version $Date$ $Id$
  */
 public class TextParseUtilTest extends XWorkTestCase {
+	
+	
+	public void testTranslateVariablesWithEvaluator() throws Exception {
+		OgnlValueStack stack = new OgnlValueStack();
+		stack.push(new Object() {
+			public String getMyVariable() {
+				return "My Variable ";
+			}
+		});
+		
+		TextParseUtil.ParsedValueEvaluator evaluator = new TextParseUtil.ParsedValueEvaluator() {
+			public Object evaluate(Object parsedValue) {
+				return parsedValue.toString()+"Something";
+			}
+		};
+		
+		String result = TextParseUtil.translateVariables("Hello ${myVariable}", stack, evaluator);
+		
+		assertEquals(result, "Hello My Variable Something");
+	}
 
     public void testTranslateVariables() {
         OgnlValueStack stack = new OgnlValueStack();
         assertEquals("foo: ${1", s);
 
         s =  TextParseUtil.translateVariables('$', "${{1, 2, 3}}", stack, Object.class);
+        assertNotNull(s);
         assertTrue("List not returned when parsing a 'pure' list", s instanceof List);
+        assertEquals(((List)s).size(), 3);
+        
+        s = TextParseUtil.translateVariables('$', "${#{'key1':'value1','key2':'value2','key3':'value3'}}", stack, Object.class);
+        assertNotNull(s);
+        assertTrue("Map not returned when parsing a 'pure' map", s instanceof Map);
+        assertEquals(((Map)s).size(), 3);
 
         s =  TextParseUtil.translateVariables('$', "${1} two ${3}", stack, Object.class);
         assertEquals("1 two 3", s);