activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r523008 - in /activemq/camel/trunk/camel-core/src: main/java/org/apache/camel/builder/ test/java/org/apache/camel/builder/
Date Tue, 27 Mar 2007 17:33:51 GMT
Author: jstrachan
Date: Tue Mar 27 10:33:50 2007
New Revision: 523008

URL: http://svn.apache.org/viewvc?view=rev&rev=523008
Log:
added some regular expression expressions & predicates with test cases

Added:
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/ExpressionBuilderTest.java
  (with props)
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/PredicateBuilderTest.java
  (with props)
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java?view=diff&rev=523008&r1=523007&r2=523008
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
Tue Mar 27 10:33:50 2007
@@ -22,6 +22,8 @@
 import java.util.ArrayList;
 import java.util.List;
 import java.util.StringTokenizer;
+import java.util.Arrays;
+import java.util.regex.Pattern;
 
 /**
  * @version $Revision: $
@@ -144,8 +146,10 @@
     public static <E extends Exchange> Expression<E> tokenizeExpression(final
Expression<E> expression, final String token) {
         return new Expression<E>() {
             public Object evaluate(E exchange) {
-                Object value = expression.evaluate(exchange);
-                String text = exchange.getContext().getTypeConverter().convertTo(String.class,
value);
+                String text = evaluateStringExpression(expression, exchange);
+                if (text == null) {
+                    return null;
+                }
                 StringTokenizer iter = new StringTokenizer(text, token);
                 List<String> answer = new ArrayList<String>();
                 while (iter.hasMoreTokens()) {
@@ -159,5 +163,81 @@
                 return "tokenize(" + expression + ", " + token + ")";
             }
         };
+    }
+
+    /**
+     * Returns a tokenize expression which will tokenize the string with the given regex
+     */
+    public static <E extends Exchange> Expression<E> regexTokenize(final Expression<E>
expression, String regexTokenizer) {
+        final Pattern pattern = Pattern.compile(regexTokenizer);
+        return new Expression<E>() {
+            public Object evaluate(E exchange) {
+                String text = evaluateStringExpression(expression, exchange);
+                if (text == null) {
+                    return null;
+                }
+                return Arrays.asList(pattern.split(text));
+            }
+
+            @Override
+            public String toString() {
+                return "regexTokenize(" + expression + ", " + pattern.pattern() + ")";
+            }
+        };
+    }
+
+    /**
+     * Transforms the expression into a String then performs the regex replaceAll to transform
the String and return the result
+     */
+    public static <E extends Exchange> Expression<E> regexReplaceAll(final Expression<E>
expression, String regex, final String replacement) {
+        final Pattern pattern = Pattern.compile(regex);
+        return new Expression<E>() {
+            public Object evaluate(E exchange) {
+                String text = evaluateStringExpression(expression, exchange);
+                if (text == null) {
+                    return null;
+                }
+                return pattern.matcher(text).replaceAll(replacement);
+            }
+
+            @Override
+            public String toString() {
+                return "regexReplaceAll(" + expression + ", " + pattern.pattern() + ")";
+            }
+        };
+    }
+
+    /**
+     * Transforms the expression into a String then performs the regex replaceAll to transform
the String and return the result
+     */
+    public static <E extends Exchange> Expression<E> regexReplaceAll(final Expression<E>
expression, String regex, final Expression<E> replacementExpression) {
+        final Pattern pattern = Pattern.compile(regex);
+        return new Expression<E>() {
+            public Object evaluate(E exchange) {
+                String text = evaluateStringExpression(expression, exchange);
+                String replacement = evaluateStringExpression(replacementExpression, exchange);;
+                if (text == null || replacement == null) {
+                    return null;
+                }
+                return pattern.matcher(text).replaceAll(replacement);
+            }
+
+            @Override
+            public String toString() {
+                return "regexReplaceAll(" + expression + ", " + pattern.pattern() + ")";
+            }
+        };
+    }
+
+    /**
+     * Evaluates the expression on the given exchange and returns the String representation
+     *
+     * @param expression the expression to evaluate
+     * @param exchange the exchange to use to evaluate the expression
+     * @return the String representation of the expression or null if it could not be evaluated
+     */
+    public static <E extends Exchange> String evaluateStringExpression(Expression<E>
expression, E exchange) {
+        Object value = expression.evaluate(exchange);
+        return exchange.getContext().getTypeConverter().convertTo(String.class, value);
     }
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java?view=diff&rev=523008&r1=523007&r2=523008
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java
Tue Mar 27 10:33:50 2007
@@ -22,6 +22,9 @@
 import org.apache.camel.util.ObjectHelper;
 import static org.apache.camel.util.ObjectHelper.notNull;
 
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
 /**
  * A helper class for working with predicates
  *
@@ -238,4 +241,45 @@
             }
         };
     }
+
+
+    /**
+     * Returns a predicate which is true if the expression matches the given regular expression
+     *
+     * @param expression the expression to evaluate
+     * @param regex the regular expression to match against
+     * @return a new predicate
+     */
+    public static <E extends Exchange> Predicate<E> regex(final Expression<E>
expression, final String regex) {
+        return regex(expression, Pattern.compile(regex));
+    }
+
+    /**
+     * Returns a predicate which is true if the expression matches the given regular expression
+     *
+     * @param expression the expression to evaluate
+     * @param pattern the regular expression to match against
+     * @return a new predicate
+     */
+    public static <E extends Exchange> Predicate<E> regex(final Expression<E>
expression, final Pattern pattern) {
+        notNull(expression, "expression");
+        notNull(pattern, "pattern");
+
+        return new Predicate<E>() {
+            public boolean matches(E exchange) {
+                Object value = expression.evaluate(exchange);
+                if (value != null) {
+                    Matcher matcher = pattern.matcher(value.toString());
+                    return matcher.matches();
+                }
+                return false;
+            }
+
+            @Override
+            public String toString() {
+                return expression + ".matches(" + pattern + ")";
+            }
+        };
+    }
+
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java?view=diff&rev=523008&r1=523007&r2=523008
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java
Tue Mar 27 10:33:50 2007
@@ -78,6 +78,11 @@
     }
 
     @Fluent
+    public Predicate<E> matchesRegex(@FluentArg("regex") String regex) {
+        return PredicateBuilder.regex(expression, regex);
+    }
+
+    @Fluent
     public Predicate<E> isNull() {
         return PredicateBuilder.isNull(expression);
     }

Added: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/ExpressionBuilderTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/ExpressionBuilderTest.java?view=auto&rev=523008
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/ExpressionBuilderTest.java
(added)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/ExpressionBuilderTest.java
Tue Mar 27 10:33:50 2007
@@ -0,0 +1,65 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.builder;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Expression;
+import org.apache.camel.Message;
+import org.apache.camel.Predicate;
+import org.apache.camel.TestSupport;
+import static org.apache.camel.builder.ExpressionBuilder.*;
+import static org.apache.camel.builder.PredicateBuilder.contains;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.impl.DefaultExchange;
+
+import java.util.Arrays;
+
+/**
+ * @version $Revision$
+ */
+public class ExpressionBuilderTest extends TestSupport {
+    protected Exchange exchange = new DefaultExchange(new DefaultCamelContext());
+
+    public void testRegexTokenize() throws Exception {
+        Expression<Exchange> expression = regexTokenize(headerExpression("location"),
",");
+        assertExpression(expression, exchange, Arrays.asList(new String[]{"Islington", "London",
"UK"}));
+
+        Predicate<Exchange> predicate = contains(regexTokenize(headerExpression("location"),
","), constantExpression("London"));
+        assertPredicate(predicate, exchange, true);
+
+        predicate = contains(regexTokenize(headerExpression("location"), ","), constantExpression("Manchester"));
+        assertPredicate(predicate, exchange, false);
+    }
+
+    public void testRegexReplaceAll() throws Exception {
+        Expression<Exchange> expression = regexReplaceAll(headerExpression("location"),
"London", "Westminster");
+        assertExpression(expression, exchange, "Islington,Westminster,UK");
+
+        expression = regexReplaceAll(headerExpression("location"), "London", headerExpression("name"));
+        assertExpression(expression, exchange, "Islington,James,UK");
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        Message in = exchange.getIn();
+        in.setBody("Hello there!");
+        in.setHeader("name", "James");
+        in.setHeader("location", "Islington,London,UK");
+    }
+}

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/ExpressionBuilderTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/ExpressionBuilderTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/ExpressionBuilderTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/PredicateBuilderTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/PredicateBuilderTest.java?view=auto&rev=523008
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/PredicateBuilderTest.java
(added)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/PredicateBuilderTest.java
Tue Mar 27 10:33:50 2007
@@ -0,0 +1,55 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.builder;
+
+import org.apache.camel.TestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Expression;
+import org.apache.camel.Predicate;
+import org.apache.camel.Message;
+import static org.apache.camel.builder.PredicateBuilder.*;
+import org.apache.camel.impl.DefaultExchange;
+import org.apache.camel.impl.DefaultCamelContext;
+
+import java.util.Arrays;
+
+/**
+ * @version $Revision$
+ */
+public class PredicateBuilderTest extends TestSupport {
+    protected Exchange exchange = new DefaultExchange(new DefaultCamelContext());
+
+    public void testRegexTokenize() throws Exception {
+        Expression<Exchange> locationHeader = ExpressionBuilder.headerExpression("location");
+
+        Predicate<Exchange> predicate = regex(locationHeader, "[a-zA-Z]+,London,UK");
+        assertPredicate(predicate, exchange, true);
+
+        predicate = regex(locationHeader, "[a-zA-Z]+,Westminster,[a-zA-Z]+");
+        assertPredicate(predicate, exchange, false);
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        Message in = exchange.getIn();
+        in.setBody("Hello there!");
+        in.setHeader("name", "James");
+        in.setHeader("location", "Islington,London,UK");
+    }
+}

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/PredicateBuilderTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/PredicateBuilderTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/PredicateBuilderTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message