Discussion:
[mule-scm] [mule][24064] branches/mule-3.x/core/src: MULE-6126 Provide a regex() function that can be used instead of RegexExpressionEvaluator
Pablo Kraan
2012-03-15 12:35:33 UTC
Permalink
There are a couple of methods (verifyRegex, vefiryText) that are basically
the same. There should be a common verifyStringParam(name, value) to do
that. Same for the verifyFlags.
Validation is done in the call method instead of in the validateParams
method. Looks like validateParams should not be part of the
ExpressionLanguageFunction interface
Regarding tests, I would add a note about why is needed to double escape
the "/" in the regexs (that should be included in the docs too)

Pablo
**
Revision 24064 <http://fisheye.codehaus.org/changelog/mule/?cs=24064>
Author dfeist Date 2012-03-14 21:54:59 -0500 (Wed, 14 Mar 2012) Log
Message
MULE-6126 Provide a regex() function that can be used instead of RegexExpressionEvaluator
Modified Paths
-
branches/mule-3.x/core/src/main/java/org/mule/el/mvel/MVELExpressionLanguage.java<#1361447a858b7f7e_branchesmule3xcoresrcmainjavaorgmuleelmvelMVELExpressionLanguagejava>
-
branches/mule-3.x/core/src/test/java/org/mule/el/mvel/MVELExpressionLanguageTestCase.java<#1361447a858b7f7e_branchesmule3xcoresrctestjavaorgmuleelmvelMVELExpressionLanguageTestCasejava>
Added Paths
- branches/mule-3.x/core/src/main/java/org/mule/el/function/
-
branches/mule-3.x/core/src/main/java/org/mule/el/function/RegexExpressionLanguageFuntion.java<#1361447a858b7f7e_branchesmule3xcoresrcmainjavaorgmuleelfunctionRegexExpressionLanguageFuntionjava>
- branches/mule-3.x/core/src/test/java/org/mule/el/function/
-
branches/mule-3.x/core/src/test/java/org/mule/el/function/RegexExpressionLanguageFunctionTestCase.java<#1361447a858b7f7e_branchesmule3xcoresrctestjavaorgmuleelfunctionRegexExpressionLanguageFunctionTestCasejava>
Diff
branches/mule-3.x/core/src/main/java/org/mule/el/function/RegexExpressionLanguageFuntion.java
(0 => 24064)
--- branches/mule-3.x/core/src/main/java/org/mule/el/function/RegexExpressionLanguageFuntion.java (rev 0)
+ * $Id$
+ * --------------------------------------------------------------------------------------
+ * Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com
+ *
+ * The software in this package is published under the terms of the CPAL v1.0
+ * license, a copy of which has been included with this distribution in the
+ * LICENSE.txt file.
+ */
+
+package org.mule.el.function;
+
+import org.mule.api.el.ExpressionLanguageContext;
+import org.mule.api.el.ExpressionLanguageFunction;
+import org.mule.api.transformer.TransformerException;
+import org.mule.el.context.MessageContext;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.commons.collections.map.LRUMap;
+
+public class RegexExpressionLanguageFuntion implements ExpressionLanguageFunction
+{
+ private static final int SINGLE_CAPTURE_GROUP = 1;
+ private static final int NO_CAPTURE_GROUP = 0;
+
+ // Caches compiled patterns to improve performance
+ private Map<String, Pattern> patterns = Collections.synchronizedMap(new LRUMap(256));
+
+ public Object call(Object[] params, ExpressionLanguageContext context)
+ {
+ int numParams = params.length;
+ if (numParams < 1 || numParams > 3)
+ {
+ throw new IllegalArgumentException("invalid number of arguments");
+ }
+
+ String regex = verifyRegex(params[0]);
+ Object result = null;
+
+ if (numParams == 1)
+ {
+ try
+ {
+ result = getMatches(regex,
+ context.getVariable("message", MessageContext.class).payloadAs(String.class), 0, context);
+ }
+ catch (TransformerException e)
+ {
+ throw new RuntimeException("Unable to convert payload to string");
+ }
+ }
+ else
+ {
+ String text = verifyText(params[1]);
+ if (numParams == 2)
+ {
+ result = getMatches(regex, text, 0, context);
+
+ }
+ else if (numParams == 3)
+ {
+ result = getMatches(regex, text, verifyFlags(params[2]), context);
+ }
+ }
+ return result;
+ }
+
+ protected String verifyRegex(Object regex)
+ {
+ if (regex == null)
+ {
+ throw new IllegalArgumentException("regular expression is null");
+ }
+ else if (!(regex instanceof String))
+ {
+ throw new IllegalArgumentException("regular expression is not a string");
+ }
+ return (String) regex;
+ }
+
+ protected String verifyText(Object text)
+ {
+ if (text == null)
+ {
+ throw new IllegalArgumentException("text is null");
+ }
+ else if (!(text instanceof String))
+ {
+ throw new IllegalArgumentException("text is not a string");
+ }
+ return (String) text;
+ }
+
+ protected int verifyFlags(Object flags)
+ {
+ if (flags == null)
+ {
+ return 0;
+ }
+ else if (!(flags instanceof Integer))
+ {
+ throw new IllegalArgumentException("regular expression flags is not an integer");
+ }
+ return (Integer) flags;
+ }
+
+ protected Object getMatches(String regex, String text, int flags, ExpressionLanguageContext context)
+ {
+ Matcher matcher = createMatcher(regex, text, flags);
+ Object result = null;
+
+ if (matcher.matches())
+ {
+ if (matcher.groupCount() == NO_CAPTURE_GROUP || matcher.groupCount() == SINGLE_CAPTURE_GROUP)
+ {
+ result = matcher.group(matcher.groupCount());
+ }
+ else
+ {
+ String[] matchedValues = new String[matcher.groupCount()];
+
+ for (int i = 1; i <= matcher.groupCount(); i++)
+ {
+ matchedValues[i - 1] = matcher.group(i);
+ }
+
+ result = matchedValues;
+ }
+ }
+ return result;
+ }
+
+ public void validateParams(Object[] params)
+ {
+
+ }
+
+ private Matcher createMatcher(String expression, String string, int flags)
+ {
+ Pattern pattern = patterns.get(expression + flags);
+
+ if (pattern == null)
+ {
+ pattern = Pattern.compile(expression, flags);
+ patterns.put(expression + flags, pattern);
+ }
+
+ return pattern.matcher(string);
+ }
+
+}Property changes on: branches/mule-3.x/core/src/main/java/org/mule/el/function/RegexExpressionLanguageFuntion.java___________________________________________________________________
Added: svn:keywords
Added: svn:eol-style
branches/mule-3.x/core/src/main/java/org/mule/el/mvel/MVELExpressionLanguage.java
(24063 => 24064)
--- branches/mule-3.x/core/src/main/java/org/mule/el/mvel/MVELExpressionLanguage.java 2012-03-14 21:41:23 UTC (rev 24063)
branches/mule-3.x/core/src/test/java/org/mule/el/function/RegexExpressionLanguageFunctionTestCase.java
(0 => 24064)
--- branches/mule-3.x/core/src/test/java/org/mule/el/function/RegexExpressionLanguageFunctionTestCase.java (rev 0)
+ * $Id$
+ * --------------------------------------------------------------------------------------
+ * Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com
+ *
+ * The software in this package is published under the terms of the CPAL v1.0
+ * license, a copy of which has been included with this distribution in the
+ * LICENSE.txt file.
+ */
+
+package org.mule.el.function;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import org.mule.api.MuleContext;
+import org.mule.api.MuleMessage;
+import org.mule.api.el.ExpressionExecutor;
+import org.mule.api.lifecycle.InitialisationException;
+import org.mule.api.transformer.TransformerException;
+import org.mule.el.context.MessageContext;
+import org.mule.el.function.RegexExpressionLanguageFuntion;
+import org.mule.el.mvel.MVELExpressionExecutor;
+import org.mule.el.mvel.MVELExpressionLanguageContext;
+import org.mule.tck.junit4.AbstractMuleTestCase;
+import org.mule.tck.size.SmallTest;
+
+import java.util.Date;
+import java.util.regex.Pattern;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.mvel2.CompileException;
+import org.mvel2.ParserContext;
+
+public class RegexExpressionLanguageFunctionTestCase extends AbstractMuleTestCase
+{
+
+ protected ExpressionExecutor<MVELExpressionLanguageContext> expressionExecutor;
+ protected MVELExpressionLanguageContext context;
+ protected RegexExpressionLanguageFuntion regexFuntion;
+
+ public void setup() throws InitialisationException
+ {
+ ParserContext parserContext = new ParserContext();
+ expressionExecutor = new MVELExpressionExecutor(parserContext);
+ context = new MVELExpressionLanguageContext(parserContext, Mockito.mock(MuleContext.class));
+ regexFuntion = new RegexExpressionLanguageFuntion();
+ context.declareFunction("regex", regexFuntion);
+ }
+
+ public void testReturnNullWhenDoesNotMatches() throws Exception
+ {
+ addMessageToContextWithPayload("TEST");
+ Object result = regexFuntion.call(new Object[]{"'TESTw+TEST'"}, context);
+ assertNull(result);
+ }
+
+ public void testReturnNullWhenDoesNotMatchesMVEL() throws Exception
+ {
+ addMessageToContextWithPayload("TEST");
+ Object result = expressionExecutor.execute("regex('TESTw+TEST')", context);
+ assertNull(result);
+ }
+
+ public void testReturnsPayloadWhenMatchesIfNoCaptureGroupDefined() throws Exception
+ {
+ addMessageToContextWithPayload("TESTfooTEST");
+ Object result = regexFuntion.call(new Object[]{"TEST\\w+TEST"}, context);
+ assertEquals("TESTfooTEST", result);
+ }
+
+ public void testReturnsPayloadWhenMatchesIfNoCaptureGroupDefinedMVEL() throws Exception
+ {
+ addMessageToContextWithPayload("TESTfooTEST");
+ Object result = expressionExecutor.execute("regex('TEST\\\\w+TEST')", context);
+ assertEquals("TESTfooTEST", result);
+ }
+
+ public void testReturnsMatchedValueIfCaptureGroupDefined() throws Exception
+ {
+ addMessageToContextWithPayload("TESTfooTEST");
+ Object result = regexFuntion.call(new Object[]{"TEST(\\w+)TEST"}, context);
+ assertEquals("foo", result);
+ }
+
+ public void testReturnsMatchedValueIfCaptureGroupDefinedMVEL() throws Exception
+ {
+ addMessageToContextWithPayload("TESTfooTEST");
+ Object result = expressionExecutor.execute("regex('TEST(\\\\w+)TEST')", context);
+ assertEquals("foo", result);
+ }
+
+ public void testReturnsMultipleValuesIfMultipleCaptureGroupDefine() throws Exception
+ {
+ addMessageToContextWithPayload("TESTfooTESTbar");
+ Object result = regexFuntion.call(new Object[]{"TEST(\\w+)TEST(\\w+)"}, context);
+
+ assertTrue(result instanceof String[]);
+ String[] values = (String[]) result;
+ assertEquals(2, values.length);
+ assertEquals("foo", values[0]);
+ assertEquals("bar", values[1]);
+ }
+
+ public void testReturnsMultipleValuesIfMultipleCaptureGroupDefineMVEL() throws Exception
+ {
+ addMessageToContextWithPayload("TESTfooTESTbar");
+ Object result = expressionExecutor.execute("regex('TEST(\\\\w+)TEST(\\\\w+)')", context);
+
+ assertTrue(result instanceof String[]);
+ String[] values = (String[]) result;
+ assertEquals(2, values.length);
+ assertEquals("foo", values[0]);
+ assertEquals("bar", values[1]);
+ }
+
+ public void testReturnsPayloadWhenMatchesIfNoCaptureGroupDefinedTextArgument() throws Exception
+ {
+ Object result = regexFuntion.call(new Object[]{"TEST\\w+TEST", "TESTfooTEST"}, context);
+ assertEquals("TESTfooTEST", result);
+ }
+
+ public void testReturnsPayloadWhenMatchesIfNoCaptureGroupDefinedTextArgumentMVEL() throws Exception
+ {
+ Object result = expressionExecutor.execute("regex('TEST\\\\w+TEST','TESTfooTEST')", context);
+ assertEquals("TESTfooTEST", result);
+ }
+
+ public void testReturnsPayloadWhenMatchesIfNoCaptureGroupDefinedTextAndFlagsArgument() throws Exception
+ {
+ Object result = regexFuntion.call(new Object[]{"test\\w+test", "TESTfooTEST",
+ Pattern.CASE_INSENSITIVE}, context);
+ assertEquals("TESTfooTEST", result);
+ }
+
+ public void testReturnsPayloadWhenMatchesIfNoCaptureGroupDefinedTextAndFlagsArgumentMVEL()
+ throws Exception
+ {
+ Object result = expressionExecutor.execute(
+ "regex('test\\\\w+test','TESTfooTEST', java.util.regex.Pattern.CASE_INSENSITIVE)", context);
+ assertEquals("TESTfooTEST", result);
+ }
+
+ public void testInvalidNullRegex() throws Exception
+ {
+ regexFuntion.call(new Object[]{null}, context);
+ }
+
+ public void testInvalidNullRegexMVEL() throws Exception
+ {
+ expressionExecutor.execute("regex(null)", context);
+ }
+
+ public void testInvalidNonStringRegex() throws Exception
+ {
+ regexFuntion.call(new Object[]{new Date()}, context);
+ }
+
+ public void testInvalidNonStringRegexMVEL() throws Exception
+ {
+ expressionExecutor.execute("regex(new Date())", context);
+ }
+
+ public void testInvalidNullText() throws Exception
+ {
+ regexFuntion.call(new Object[]{"TESTw+TEST", null}, context);
+ }
+
+ public void testInvalidNullTextMVEL() throws Exception
+ {
+ expressionExecutor.execute("regex('TESTw+TEST',null)", context);
+ }
+
+ public void testInvalidNonStringText() throws Exception
+ {
+ regexFuntion.call(new Object[]{"TESTw+TEST", new Date()}, context);
+ }
+
+ public void testInvalidNonStringTextMVEL() throws Exception
+ {
+ expressionExecutor.execute("regex('TESTw+TEST',new Date())", context);
+ }
+
+ public void testInvalidNonIntFlags() throws Exception
+ {
+ regexFuntion.call(new Object[]{"TESTw+TEST", "text", "foo"}, context);
+ }
+
+ public void testInvalidNonIntFlagsMVEL() throws Exception
+ {
+ expressionExecutor.execute("regex('TESTw+TEST','text','foo')", context);
+ }
+
+ protected void addMessageToContextWithPayload(String payload) throws TransformerException
+ {
+ MuleMessage message = Mockito.mock(MuleMessage.class);
+ Mockito.when(message.getPayload(Mockito.any(Class.class))).thenReturn(payload);
+ context.addFinalVariable("message", new MessageContext(message));
+ }
+
+}Property changes on: branches/mule-3.x/core/src/test/java/org/mule/el/function/RegexExpressionLanguageFunctionTestCase.java___________________________________________________________________
Added: svn:keywords
Added: svn:eol-style
branches/mule-3.x/core/src/test/java/org/mule/el/mvel/MVELExpressionLanguageTestCase.java
(24063 => 24064)
--- branches/mule-3.x/core/src/test/java/org/mule/el/mvel/MVELExpressionLanguageTestCase.java 2012-03-14 21:41:23 UTC (rev 24063)
+ public void regexFunction()
+ {
+ assertEquals("foo",
+ evaluate("regex('TEST(\\\\w+)TEST')", new DefaultMuleMessage("TESTfooTEST", muleContext)));
+ }
+ protected Object evaluate(String expression) { if (variant.equals(Variant.EXPRESSION_WITH_DELIMITER))
------------------------------
http://xircles.codehaus.org/manage_email
Loading...