一、前言
关于hotswap的hotswap-agent-1.1.0-SNAPSHOT.jar源码包中org.hotswap.agent.util.spring.util.PatternMatchUtils正则匹配工具类,对一般规则匹配regexMatch、简单规则匹配simpleMatch等处理。
二、源码说明
package org.hotswap.agent.util.spring.util;@b@@b@import java.util.HashMap;@b@import java.util.Map;@b@import java.util.regex.Matcher;@b@import java.util.regex.Pattern;@b@@b@public abstract class PatternMatchUtils@b@{@b@ private static final Map<String, Pattern> patterns = new HashMap();@b@@b@ public static boolean regexMatch(String pattern, String str)@b@ {@b@ if (StringUtils.isEmpty(pattern)) {@b@ return true;@b@ }@b@@b@ Pattern p = (Pattern)patterns.get(pattern);@b@ if (p == null) {@b@ p = Pattern.compile(pattern);@b@ patterns.put(pattern, p);@b@ }@b@ boolean matched = p.matcher(str).matches();@b@ return matched;@b@ }@b@@b@ public static boolean simpleMatch(String pattern, String str)@b@ {@b@ if ((pattern == null) || (str == null))@b@ return false;@b@@b@ int firstIndex = pattern.indexOf(42);@b@ if (firstIndex == -1)@b@ return pattern.equals(str);@b@@b@ if (firstIndex == 0) {@b@ if (pattern.length() == 1)@b@ return true;@b@@b@ int nextIndex = pattern.indexOf(42, firstIndex + 1);@b@ if (nextIndex == -1)@b@ return str.endsWith(pattern.substring(1));@b@@b@ String part = pattern.substring(1, nextIndex);@b@ if ("".equals(part))@b@ return simpleMatch(pattern.substring(nextIndex), str);@b@@b@ int partIndex = str.indexOf(part);@b@ while (partIndex != -1) {@b@ if (simpleMatch(pattern.substring(nextIndex), str.substring(partIndex + part.length())))@b@ return true;@b@@b@ partIndex = str.indexOf(part, partIndex + 1);@b@ }@b@ return false;@b@ }@b@ return ((str.length() >= firstIndex) && (pattern.substring(0, firstIndex).equals(str.substring(0, firstIndex))) && (simpleMatch(pattern.substring(firstIndex), str.substring(firstIndex))));@b@ }@b@@b@ public static boolean simpleMatch(String[] patterns, String str)@b@ {@b@ String[] arr$;@b@ int i$;@b@ if (patterns != null) {@b@ arr$ = patterns; int len$ = arr$.length; for (i$ = 0; i$ < len$; ++i$) { String pattern = arr$[i$];@b@ if (simpleMatch(pattern, str))@b@ return true;@b@ }@b@ }@b@@b@ return false;@b@ }@b@}