From 54e5c912ebd9a1599bfcf7a719da17c28127bbe3 Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Mon, 3 Jun 2013 08:47:16 +0000 Subject: [PATCH] WW-4090 Removes double evaluation of parsed expression git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/branches/STRUTS_2_3_14_2_X@1488897 13f79535-47bb-0310-9956-ffa450edef68 --- .../xwork2/util/OgnlTextParser.java | 5 ++-- .../xwork2/util/TextParseUtilTest.java | 23 ++++++++++++++++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/xwork-core/src/main/java/com/opensymphony/xwork2/util/OgnlTextParser.java b/xwork-core/src/main/java/com/opensymphony/xwork2/util/OgnlTextParser.java index c25298adb8..fa390bf80f 100644 --- a/xwork-core/src/main/java/com/opensymphony/xwork2/util/OgnlTextParser.java +++ b/xwork-core/src/main/java/com/opensymphony/xwork2/util/OgnlTextParser.java @@ -11,17 +11,16 @@ public Object evaluate(char[] openChars, String expression, TextParseUtil.Parsed // deal with the "pure" expressions first! //expression = expression.trim(); Object result = expression; + int pos = 0; + for (char open : openChars) { int loopCount = 1; - int pos = 0; - //this creates an implicit StringBuffer and shouldn't be used in the inner loop final String lookupChars = open + "{"; while (true) { int start = expression.indexOf(lookupChars, pos); if (start == -1) { - pos = 0; loopCount++; start = expression.indexOf(lookupChars); } diff --git a/xwork-core/src/test/java/com/opensymphony/xwork2/util/TextParseUtilTest.java b/xwork-core/src/test/java/com/opensymphony/xwork2/util/TextParseUtilTest.java index e18fb0f77a..45e79709fc 100644 --- a/xwork-core/src/test/java/com/opensymphony/xwork2/util/TextParseUtilTest.java +++ b/xwork-core/src/test/java/com/opensymphony/xwork2/util/TextParseUtilTest.java @@ -97,6 +97,24 @@ public void testTranslateVariables() { assertEquals("count must be between 123 and 456, current value is 98765.", s); } + public void testNestedExpression() throws Exception { + ValueStack stack = ActionContext.getContext().getValueStack(); + stack.push(new HashMap() {{ put("foo", "${%{1+1}}"); }}); + String s = TextParseUtil.translateVariables("${foo}", stack); + assertEquals("${%{1+1}}", s); + stack.pop(); + } + + public void testMixedOpenChars() throws Exception { + ValueStack stack = ActionContext.getContext().getValueStack(); + stack.push(new HashMap() {{ put("foo", "bar"); }}); + String s = TextParseUtil.translateVariables("${foo}-%{foo}", stack); + assertEquals("bar-bar", s); + s = TextParseUtil.translateVariables("%{foo}-${foo}", stack); + assertEquals("%{foo}-bar", s); // this is bad, but it is the only way not to double evaluate passed expression + stack.pop(); + } + public void testCommaDelimitedStringToSet() { assertEquals(0, TextParseUtil.commaDelimitedStringToSet("").size()); assertEquals(new HashSet(Arrays.asList("foo", "bar", "tee")), @@ -132,10 +150,13 @@ public void testTranslateVariablesNoRecursive() { public void testTranslateVariablesRecursive() { ValueStack stack = ActionContext.getContext().getValueStack(); - stack.push(new HashMap() {{ put("foo", "${1+1}"); }}); + stack.push(new HashMap() {{ put("foo", "${1+1}"); put("bar", "${${1+2}}"); }}); Object s = TextParseUtil.translateVariables('$', "foo: ${foo}", stack, String.class, null, 2); assertEquals("foo: 2", s); + + s = TextParseUtil.translateVariables('$', "foo: ${bar}", stack, String.class, null, 1); + assertEquals("foo: ${${1+2}}", s); } public void testTranslateVariablesWithNull() {