This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 44ab6ba  CAMEL-17591: camel-core - OgnlHelper fixed not couting 
parenthesis in single/double quoted text, when splitting into methods
44ab6ba is described below

commit 44ab6ba00ef285a8c60e72d200759c012ad95143
Author: Claus Ibsen <[email protected]>
AuthorDate: Sun Feb 6 11:13:39 2022 +0100

    CAMEL-17591: camel-core - OgnlHelper fixed not couting parenthesis in 
single/double quoted text, when splitting into methods
---
 .../apache/camel/language/simple/SimpleTest.java   |  8 ++++++-
 .../java/org/apache/camel/util/OgnlHelper.java     | 15 +++++++++++++
 .../java/org/apache/camel/util/OgnlHelperTest.java | 25 ++++++++++++++++++++++
 3 files changed, 47 insertions(+), 1 deletion(-)

diff --git 
a/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
index 5c01c5a..4026f6c 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
@@ -1958,11 +1958,17 @@ public class SimpleTest extends LanguageTestSupport {
     }
 
     @Test
-    public void testReplaceAll() throws Exception {
+    public void testParenthesisReplaceAll() throws Exception {
         exchange.getIn().setBody("Bik (Ru)");
         assertExpression("${body.replaceAll(\"Bik 
\\(Ru\\)\",\"bik_ru\").replaceAll(\"b\",\"c\")}", "cik_ru");
     }
 
+    @Test
+    public void testParenthesisReplace() throws Exception {
+        exchange.getIn().setBody("Hello (( World (((( Again");
+        assertExpression("${body.replace(\"((\", \"--\").replace(\"((((\", 
\"----\")}", "Hello -- World ---- Again");
+    }
+
     @Override
     protected String getLanguageName() {
         return "simple";
diff --git 
a/core/camel-util/src/main/java/org/apache/camel/util/OgnlHelper.java 
b/core/camel-util/src/main/java/org/apache/camel/util/OgnlHelper.java
index e7eef1d..417a625 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/OgnlHelper.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/OgnlHelper.java
@@ -192,13 +192,28 @@ public final class OgnlHelper {
         int j = 0; // j is used as counter per method
         int squareBracketCnt = 0; // special to keep track if and how deep we 
are inside a square bracket block, eg: [foo]
         int parenthesisBracketCnt = 0; // special to keep track if and how 
deep we are inside a parenthesis block, eg: bar(${body}, ${header.foo})
+        boolean singleQuoted = false;
+        boolean doubleQuoted = false;
 
         for (int i = 0; i < ognl.length(); i++) {
             char ch = ognl.charAt(i);
+
+            if (!doubleQuoted && ch == '\'') {
+                singleQuoted = !singleQuoted;
+            } else if (!singleQuoted && ch == '\"') {
+                doubleQuoted = !doubleQuoted;
+            }
+            if (singleQuoted || doubleQuoted) {
+                // quoted text so append as literal text
+                sb.append(ch);
+                continue;
+            }
+
             // special for starting a new method
             if (j == 0 || j == 1 && ognl.charAt(i - 1) == '?'
                     || ch != '.' && ch != '?' && ch != ']') {
                 sb.append(ch);
+
                 // special if we are doing square bracket
                 if (ch == '[' && parenthesisBracketCnt == 0) {
                     squareBracketCnt++;
diff --git 
a/core/camel-util/src/test/java/org/apache/camel/util/OgnlHelperTest.java 
b/core/camel-util/src/test/java/org/apache/camel/util/OgnlHelperTest.java
index 5727a50..1c09a3e 100644
--- a/core/camel-util/src/test/java/org/apache/camel/util/OgnlHelperTest.java
+++ b/core/camel-util/src/test/java/org/apache/camel/util/OgnlHelperTest.java
@@ -42,4 +42,29 @@ public class OgnlHelperTest {
         assertEquals(".replaceFirst(\".*;?iwanttoknow=([^;]+);?.*\", \"$1\")", 
strings.get(2));
     }
 
+    @Test
+    public void splitOgnlWithParenthesisInQuotes() {
+        String ognl = "body.replace(\"((\", \"--\")";
+        assertFalse(OgnlHelper.isInvalidValidOgnlExpression(ognl));
+        assertTrue(OgnlHelper.isValidOgnlExpression(ognl));
+
+        List<String> strings = OgnlHelper.splitOgnl(ognl);
+        assertEquals(2, strings.size());
+        assertEquals("body", strings.get(0));
+        assertEquals(".replace(\"((\", \"--\")", strings.get(1));
+    }
+
+    @Test
+    public void splitOgnlWithParenthesisInQuotesTwo() {
+        String ognl = "body.replace(\"((\", \"--\").replace(\"((((\", 
\"----\")";
+        assertFalse(OgnlHelper.isInvalidValidOgnlExpression(ognl));
+        assertTrue(OgnlHelper.isValidOgnlExpression(ognl));
+
+        List<String> strings = OgnlHelper.splitOgnl(ognl);
+        assertEquals(3, strings.size());
+        assertEquals("body", strings.get(0));
+        assertEquals(".replace(\"((\", \"--\")", strings.get(1));
+        assertEquals(".replace(\"((((\", \"----\")", strings.get(2));
+    }
+
 }

Reply via email to