This is an automated email from the ASF dual-hosted git repository.
nmalin pushed a commit to branch release24.09
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git
The following commit(s) were added to refs/heads/release24.09 by this push:
new f979f0eb40 Improved: Update end bracket detection en groovy scriplet
(OFBIZ-13178)
f979f0eb40 is described below
commit f979f0eb406e85b8fa4aa9934edcc22c725d6c9e
Author: Nicolas Malin <[email protected]>
AuthorDate: Tue Nov 19 10:36:28 2024 +0100
Improved: Update end bracket detection en groovy scriplet (OFBIZ-13178)
When you use double brackets on groovy scriptlet, the end bracket isn't
correctly manage.
The follow simple code failed :
${groovy: if (true) {return 0}}
---
.../base/util/string/FlexibleStringExpander.java | 22 ++++++++++++++--------
.../util/string/FlexibleStringExpanderTests.java | 6 +++++-
2 files changed, 19 insertions(+), 9 deletions(-)
diff --git
a/framework/base/src/main/java/org/apache/ofbiz/base/util/string/FlexibleStringExpander.java
b/framework/base/src/main/java/org/apache/ofbiz/base/util/string/FlexibleStringExpander.java
index 6a60a166b6..dc4166806f 100644
---
a/framework/base/src/main/java/org/apache/ofbiz/base/util/string/FlexibleStringExpander.java
+++
b/framework/base/src/main/java/org/apache/ofbiz/base/util/string/FlexibleStringExpander.java
@@ -309,17 +309,11 @@ public abstract class FlexibleStringExpander implements
Serializable, IsEmpty {
}
if (expression.indexOf("groovy:", start + 2) == start + 2 &&
!escapedExpression) {
// checks to see if this starts with a "groovy:", if so treat
the rest of the expression as a groovy scriptlet
+ end = getMatchingClosingBracket(expression, start, origLen,
end);
strElems.add(new ScriptElem(chars, start, Math.min(end + 1,
start + length) - start, start + 9, end - start - 9));
} else {
// Scan for matching closing bracket
- int ptr = expression.indexOf("{", start + 2);
- while (ptr != -1 && end != -1 && ptr < end) {
- end = expression.indexOf(CLOSE_BRACKET, end + 1);
- ptr = expression.indexOf("{", ptr + 1);
- }
- if (end == -1) {
- end = origLen;
- }
+ end = getMatchingClosingBracket(expression, start, origLen,
end);
// Evaluation sequence is important - do not change it
if (escapedExpression) {
strElems.add(new ConstOffsetElem(chars, start, end + 1 -
start));
@@ -352,6 +346,18 @@ public abstract class FlexibleStringExpander implements
Serializable, IsEmpty {
return strElems.toArray(new FlexibleStringExpander[strElems.size()]);
}
+ private static int getMatchingClosingBracket(String expression, int start,
int origLen, int end) {
+ int ptr = expression.indexOf("{", start + 2);
+ while (ptr != -1 && end != -1 && ptr < end) {
+ end = expression.indexOf(CLOSE_BRACKET, end + 1);
+ ptr = expression.indexOf("{", ptr + 1);
+ }
+ if (end == -1) {
+ end = origLen;
+ }
+ return end;
+ }
+
// Note: a character array is used instead of a String to keep the memory
footprint small.
private final char[] chars;
private int hint = 20;
diff --git
a/framework/base/src/test/java/org/apache/ofbiz/base/util/string/FlexibleStringExpanderTests.java
b/framework/base/src/test/java/org/apache/ofbiz/base/util/string/FlexibleStringExpanderTests.java
index 44b5afac5d..81c39731ce 100644
---
a/framework/base/src/test/java/org/apache/ofbiz/base/util/string/FlexibleStringExpanderTests.java
+++
b/framework/base/src/test/java/org/apache/ofbiz/base/util/string/FlexibleStringExpanderTests.java
@@ -316,8 +316,12 @@ public class FlexibleStringExpanderTests {
fseTest("UEL integration: missing", "${noList[0]}", testMap, null,
null, "", null, false);
fseTest("Escaped expression", "This is an \\${escaped} expression",
testMap, "This is an ${escaped} expression", false);
fseTest("Escaped(groovy) expression", "This is an \\${groovy:escaped}
expression", testMap, "This is an ${groovy:escaped} expression", false);
+ fseTest("Bracket en groovy", "This is a groovy ${groovy: if (true)
{return 'bracket'}} expression", testMap,
+ "This is a groovy bracket expression", false);
+ fseTest("Bracket en groovy again", "This is a groovy ${groovy: if
(true) {if (true) {return 'with 2 brackets'}}} expression", testMap,
+ "This is a groovy with 2 brackets expression", false);
- // TODO: Find a better way to setup or handle the big decimal value.
If new ones are not instanciated in the test
+ // TODO: Find a better way to setup or handle the big decimal value.
If new ones are not instantiated in the test
// it fails because of the comparison between object pointers..
fseTest("nested UEL integration(return BigDecimal)", "${a${'moun'}t}",
testMap, null, LOCALE_TO_TEST,
"1,234,567.89", new BigDecimal("1234567.89"), false);