Search in sources :

Example 1 with AutoProcessor

use of com.virjar.vscrawler.core.processor.configurableprocessor.annotiondriven.annotation.AutoProcessor in project vscrawler by virjar.

the class AnnotationProcessorBuilder method judgeMatchStrategy.

private AnnotationSeedProcessor.MatchStrategy judgeMatchStrategy(Class<? extends AbstractAutoProcessModel> aClass) {
    final AutoProcessor autoProcessor = aClass.getAnnotation(AutoProcessor.class);
    if (autoProcessor == null) {
        return null;
    }
    String seedPattern = autoProcessor.seedPattern();
    if (StringUtils.isNotBlank(seedPattern)) {
        try {
            final Pattern pattern = Pattern.compile(seedPattern);
            return new AnnotationSeedProcessor.MatchStrategy() {

                @Override
                public boolean matchSeed(Seed seed) {
                    return pattern.matcher(seed.getData()).matches();
                }

                @Override
                public int priority() {
                    return autoProcessor.priority();
                }
            };
        } catch (PatternSyntaxException e) {
            throw new IllegalStateException("error when register processor for class" + aClass.getName() + " regex error for seedPattern", e);
        }
    }
    Method[] methods = aClass.getMethods();
    for (final Method method : methods) {
        if (method.getAnnotation(MatchSeed.class) == null) {
            continue;
        }
        Preconditions.checkArgument(Boolean.class.isAssignableFrom(method.getReturnType()));
        Preconditions.checkArgument(Modifier.isStatic(method.getModifiers()));
        return new AnnotationSeedProcessor.MatchStrategy() {

            @Override
            public boolean matchSeed(Seed seed) {
                try {
                    return (Boolean) method.invoke(null, seed);
                } catch (Exception e) {
                    throw new IllegalStateException("can not jude seed match method", e);
                }
            }

            @Override
            public int priority() {
                return autoProcessor.priority();
            }
        };
    }
    return null;
}
Also used : Pattern(java.util.regex.Pattern) MatchSeed(com.virjar.vscrawler.core.processor.configurableprocessor.annotiondriven.annotation.MatchSeed) Seed(com.virjar.vscrawler.core.seed.Seed) Method(java.lang.reflect.Method) AutoProcessor(com.virjar.vscrawler.core.processor.configurableprocessor.annotiondriven.annotation.AutoProcessor) MatchSeed(com.virjar.vscrawler.core.processor.configurableprocessor.annotiondriven.annotation.MatchSeed) PatternSyntaxException(java.util.regex.PatternSyntaxException) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Aggregations

AutoProcessor (com.virjar.vscrawler.core.processor.configurableprocessor.annotiondriven.annotation.AutoProcessor)1 MatchSeed (com.virjar.vscrawler.core.processor.configurableprocessor.annotiondriven.annotation.MatchSeed)1 Seed (com.virjar.vscrawler.core.seed.Seed)1 Method (java.lang.reflect.Method)1 Pattern (java.util.regex.Pattern)1 PatternSyntaxException (java.util.regex.PatternSyntaxException)1