All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alogic.matcher.MatcherFactory Maven / Gradle / Ivy

There is a newer version: 1.6.17
Show newest version
package com.alogic.matcher;

import com.alogic.matcher.impl.Exact;
import com.alogic.matcher.impl.IpSegment;
import com.alogic.matcher.impl.Re2;
import com.alogic.matcher.impl.Wildcard;
import com.anysoft.util.Properties;
import com.google.re2j.Matcher;
import com.google.re2j.Pattern;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;

/**
 * 工厂类
 *
 * @since 1.6.12.57 [20200103]
 */
public class MatcherFactory {

    /**
     * a slf4j logger
     */
    protected static Logger LOG = LoggerFactory.getLogger(CommonMatcher.class);

    /**
     * 类名映射
     */
    protected static Map> classMappings
            = new HashMap>();

    /**
     * 分离匹配器和表达式
     */
    protected static Pattern matcherAndExpr = Pattern.compile("^\\((.*)\\)(.*)$");

    protected static String DFT_MATCHER = "re2";

    /**
     * 注册匹配器
     * @param name 名称
     * @param clazz 实现类
     */
    public static void register(String name,Class clazz){
        if (StringUtils.isNotEmpty(name) && clazz != null){
            classMappings.put(name,clazz);
        }
    }

    /**
     * 注销匹配器
     * @param name 匹配器名称
     */
    public static void unregister(String name){
        if (StringUtils.isNotEmpty(name)) {
            classMappings.remove(name);
        }
    }

    public static CommonMatcher getMatcher(String pattern, Properties p){
        String matcherId = DFT_MATCHER;
        String regex = pattern;

        Matcher matcher = matcherAndExpr.matcher(pattern);
        if (matcher.find()){
            matcherId = matcher.group(1);
            regex = matcher.group(2);
        }

        if (StringUtils.isEmpty(matcherId)){
            matcherId = DFT_MATCHER;
        }
        matcherId = matcherId.toLowerCase();


        Class clazz = getMatcherClass(matcherId);
        try{
            Constructor constructor = clazz.getConstructor(
                    new Class[]{String.class,Properties.class}
            );
            return constructor.newInstance(new Object[]{regex,p});
        }catch (Exception ex){
            LOG.warn("Can not create matcher instance:" + matcherId);
            return new Wildcard(regex,p);
        }
    }

    protected static Class getMatcherClass(String matcherId){
        Class found = classMappings.get(matcherId);
        return found == null?Wildcard.class:found;
    }

    static {
        register("re2", Re2.class);
        register("regex", Re2.class);
        register("wildcard", Wildcard.class);
        register("exact", Exact.class);
        register("cidr", IpSegment.class);
        register("ip", IpSegment.class);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy