mirror of
https://github.com/apache/struts.git
synced 2026-08-11 01:27:14 +00:00
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
This commit is contained in:
@@ -11,17 +11,16 @@ public class OgnlTextParser implements TextParser {
|
||||
// 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);
|
||||
}
|
||||
|
||||
@@ -97,6 +97,24 @@ public class TextParseUtilTest extends XWorkTestCase {
|
||||
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<String, Object>() {{ 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<String, Object>() {{ 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<String>(Arrays.asList("foo", "bar", "tee")),
|
||||
@@ -132,10 +150,13 @@ public class TextParseUtilTest extends XWorkTestCase {
|
||||
|
||||
public void testTranslateVariablesRecursive() {
|
||||
ValueStack stack = ActionContext.getContext().getValueStack();
|
||||
stack.push(new HashMap<String, Object>() {{ put("foo", "${1+1}"); }});
|
||||
stack.push(new HashMap<String, Object>() {{ 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() {
|
||||
|
||||
Reference in New Issue
Block a user