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

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

commit 9b6a33703e6ab0f659d80095100c9f137d52673b
Author: Claus Ibsen <[email protected]>
AuthorDate: Sat Feb 3 10:24:59 2024 +0100

    CAMEL-20378: Languages should be thread-safe and be configured only via 
properties array, all in the same way.
---
 .../apache/camel/language/xpath/XPathLanguage.java | 19 +++++-------
 .../support/SingleInputTypedLanguageSupport.java   | 34 ++++++++++++++++++++++
 2 files changed, 41 insertions(+), 12 deletions(-)

diff --git 
a/components/camel-xpath/src/main/java/org/apache/camel/language/xpath/XPathLanguage.java
 
b/components/camel-xpath/src/main/java/org/apache/camel/language/xpath/XPathLanguage.java
index 028a445b53a..a0e158f5010 100644
--- 
a/components/camel-xpath/src/main/java/org/apache/camel/language/xpath/XPathLanguage.java
+++ 
b/components/camel-xpath/src/main/java/org/apache/camel/language/xpath/XPathLanguage.java
@@ -19,6 +19,7 @@ package org.apache.camel.language.xpath;
 import java.util.Map;
 
 import javax.xml.namespace.QName;
+import javax.xml.xpath.XPathConstants;
 import javax.xml.xpath.XPathFactory;
 
 import org.apache.camel.CamelContext;
@@ -26,7 +27,6 @@ import org.apache.camel.Expression;
 import org.apache.camel.Predicate;
 import org.apache.camel.spi.PropertyConfigurer;
 import org.apache.camel.spi.annotations.Language;
-import org.apache.camel.support.ExpressionToPredicateAdapter;
 import org.apache.camel.support.SingleInputTypedLanguageSupport;
 import org.apache.camel.support.component.PropertyConfigurerSupport;
 
@@ -45,18 +45,13 @@ public class XPathLanguage extends 
SingleInputTypedLanguageSupport implements Pr
     private Boolean preCompile;
 
     @Override
-    public Predicate createPredicate(String expression) {
-        return 
ExpressionToPredicateAdapter.toPredicate(createExpression(expression));
-    }
-
-    @Override
-    public Expression createExpression(String expression) {
-        return createExpression(expression, null);
-    }
+    public Predicate createPredicate(Expression source, String expression, 
Object[] properties) {
+        expression = loadResource(expression);
 
-    @Override
-    public Predicate createPredicate(String expression, Object[] properties) {
-        return 
ExpressionToPredicateAdapter.toPredicate(createExpression(expression, 
properties));
+        XPathBuilder builder = XPathBuilder.xpath(expression);
+        configureBuilder(builder, properties, source);
+        builder.setResultQName(XPathConstants.BOOLEAN); // use boolean for 
predicate mode
+        return builder;
     }
 
     @Override
diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/SingleInputTypedLanguageSupport.java
 
b/core/camel-support/src/main/java/org/apache/camel/support/SingleInputTypedLanguageSupport.java
index 19e00a97997..c57f2055344 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/SingleInputTypedLanguageSupport.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/SingleInputTypedLanguageSupport.java
@@ -17,14 +17,26 @@
 package org.apache.camel.support;
 
 import org.apache.camel.Expression;
+import org.apache.camel.Predicate;
 import org.apache.camel.spi.Language;
 import org.apache.camel.support.builder.ExpressionBuilder;
+import org.apache.camel.support.builder.PredicateBuilder;
 
 /**
  * Base class for {@link Language} implementations that support a result type 
and different sources of input data.
  */
 public abstract class SingleInputTypedLanguageSupport extends 
TypedLanguageSupport {
 
+    @Override
+    public Predicate createPredicate(String expression) {
+        return createPredicate(expression, null);
+    }
+
+    @Override
+    public Expression createExpression(String expression) {
+        return createExpression(expression, null);
+    }
+
     @Override
     public Expression createExpression(String expression, Object[] properties) 
{
         Class<?> type = property(Class.class, properties, 0, getResultType());
@@ -38,6 +50,16 @@ public abstract class SingleInputTypedLanguageSupport 
extends TypedLanguageSuppo
         return ExpressionBuilder.convertToExpression(createExpression(source, 
expression, properties), type);
     }
 
+    @Override
+    public Predicate createPredicate(String expression, Object[] properties) {
+        Class<?> type = property(Class.class, properties, 0, getResultType());
+        String variable = property(String.class, properties, 1, null);
+        String header = property(String.class, properties, 2, null);
+        String property = property(String.class, properties, 3, null);
+        Expression source = ExpressionBuilder.singleInputExpression(variable, 
header, property);
+        return createPredicate(source, expression, properties);
+    }
+
     /**
      * Creates an expression based on the input with properties.
      *
@@ -49,4 +71,16 @@ public abstract class SingleInputTypedLanguageSupport 
extends TypedLanguageSuppo
     public Expression createExpression(Expression source, String expression, 
Object[] properties) {
         throw new UnsupportedOperationException();
     }
+
+    /**
+     * Creates a predicate based on the input with properties.
+     *
+     * @param  source     the expression allowing to retrieve the input data 
of the main expression.
+     * @param  expression the main expression to evaluate as predicate.
+     * @param  properties configuration properties (optimized as object array 
with hardcoded positions for properties)
+     * @return            the created predicate
+     */
+    public Predicate createPredicate(Expression source, String expression, 
Object[] properties) {
+        return PredicateBuilder.toPredicate(createExpression(source, 
expression, properties));
+    }
 }

Reply via email to