();
+ env.put("pattern", pattern);
+ env.put("target", filtered.toLowerCase());
+ return (Boolean) exp.execute(env);
+ }
+
+ /**
+ * 修复正则表达式匹配的问题,因为使用了 oro 的 matches,会出现:
+ *
+ *
+ * foo|foot 匹配 foot 出错,原因是 foot 匹配了 foo 之后,会返回 foo,但是 foo 的长度和 foot 的长度不一样
+ *
+ *
+ * 因此此类对正则表达式进行了从长到短的排序
+ *
+ * @author zebin.xuzb 2012-10-22 下午2:02:26
+ * @version 1.0.0
+ */
+ private static class StringComparator implements Comparator {
+
+ @Override
+ public int compare(String str1, String str2) {
+ if (str1.length() > str2.length()) {
+ return -1;
+ } else if (str1.length() < str2.length()) {
+ return 1;
+ } else {
+ return 0;
+ }
+ }
+ }
+
+ /**
+ * 修复正则表达式匹配的问题,即使按照长度递减排序,还是会出现以下问题:
+ *
+ *
+ * foooo|f.*t 匹配 fooooot 出错,原因是 fooooot 匹配了 foooo 之后,会将 fooo 和数据进行匹配,但是 foooo 的长度和 fooooot 的长度不一样
+ *
+ *
+ * 因此此类对正则表达式进行头尾完全匹配
+ *
+ * @author simon
+ * @version 1.0.0
+ */
+
+ private List completionPattern(List patterns) {
+ List result = new ArrayList();
+ for (String pattern : patterns) {
+ StringBuffer stringBuffer = new StringBuffer();
+ stringBuffer.append("^");
+ stringBuffer.append(pattern);
+ stringBuffer.append("$");
+ result.add(stringBuffer.toString());
+ }
+ return result;
+ }
+
+ @Override
+ public String toString() {
+ return pattern;
+ }
+}
diff --git a/protocol/src/main/java/com/alibaba/otter/canal/protocol/aviater/PatternUtils.java b/protocol/src/main/java/com/alibaba/otter/canal/protocol/aviater/PatternUtils.java
new file mode 100644
index 00000000..e04f0c4b
--- /dev/null
+++ b/protocol/src/main/java/com/alibaba/otter/canal/protocol/aviater/PatternUtils.java
@@ -0,0 +1,38 @@
+package com.alibaba.otter.canal.protocol.aviater;
+
+import java.util.Map;
+
+import org.apache.oro.text.regex.MalformedPatternException;
+import org.apache.oro.text.regex.Pattern;
+import org.apache.oro.text.regex.PatternCompiler;
+import org.apache.oro.text.regex.Perl5Compiler;
+
+import com.google.common.base.Function;
+import com.google.common.collect.MapMaker;
+import com.google.common.collect.MigrateMap;
+
+public class PatternUtils {
+ @SuppressWarnings("deprecation")
+ private static Map patterns = MigrateMap.makeComputingMap(new MapMaker().softValues(),
+ new Function() {
+
+ public Pattern apply(String pattern) {
+ try {
+ PatternCompiler pc = new Perl5Compiler();
+ return pc.compile(pattern,
+ Perl5Compiler.CASE_INSENSITIVE_MASK | Perl5Compiler.READ_ONLY_MASK
+ | Perl5Compiler.SINGLELINE_MASK);
+ } catch (MalformedPatternException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ });
+
+ public static Pattern getPattern(String pattern) {
+ return patterns.get(pattern);
+ }
+
+ public static void clear() {
+ patterns.clear();
+ }
+}
diff --git a/protocol/src/main/java/com/alibaba/otter/canal/protocol/aviater/RegexFunction.java b/protocol/src/main/java/com/alibaba/otter/canal/protocol/aviater/RegexFunction.java
new file mode 100644
index 00000000..be084c25
--- /dev/null
+++ b/protocol/src/main/java/com/alibaba/otter/canal/protocol/aviater/RegexFunction.java
@@ -0,0 +1,31 @@
+package com.alibaba.otter.canal.protocol.aviater;
+
+import java.util.Map;
+
+import org.apache.oro.text.regex.Perl5Matcher;
+
+import com.googlecode.aviator.runtime.function.AbstractFunction;
+import com.googlecode.aviator.runtime.function.FunctionUtils;
+import com.googlecode.aviator.runtime.type.AviatorBoolean;
+import com.googlecode.aviator.runtime.type.AviatorObject;
+
+/**
+ * 提供aviator regex的代码扩展
+ *
+ * @author jianghang 2012-7-23 上午10:29:23
+ */
+public class RegexFunction extends AbstractFunction {
+
+ public AviatorObject call(Map env, AviatorObject arg1, AviatorObject arg2) {
+ String pattern = FunctionUtils.getStringValue(arg1, env);
+ String text = FunctionUtils.getStringValue(arg2, env);
+ Perl5Matcher matcher = new Perl5Matcher();
+ boolean isMatch = matcher.matches(text, PatternUtils.getPattern(pattern));
+ return AviatorBoolean.valueOf(isMatch);
+ }
+
+ public String getName() {
+ return "regex";
+ }
+
+}