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

cdc.applic.dictionaries.edit.checks.rules.EnPatternMustBeValid Maven / Gradle / Ivy

There is a newer version: 0.13.3
Show newest version
package cdc.applic.dictionaries.edit.checks.rules;

import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import cdc.applic.dictionaries.edit.EnPatternType;
import cdc.applic.dictionaries.edit.checks.utils.EnAbstractRuleChecker;
import cdc.applic.dictionaries.edit.checks.utils.EnIssueDescription;
import cdc.applic.dictionaries.edit.checks.utils.EnRuleUtils;
import cdc.issues.IssueSeverity;
import cdc.issues.checks.CheckContext;
import cdc.issues.checks.CheckResult;
import cdc.issues.checks.SnapshotManager;
import cdc.issues.locations.Location;
import cdc.issues.rules.Rule;

public class EnPatternMustBeValid extends EnAbstractRuleChecker {
    public static final IssueSeverity SEVERITY = IssueSeverity.CRITICAL;
    public static final String NAME = "PATTERN_MUST_BE_VALID";
    public static final Rule RULE =
            EnRuleUtils.define(NAME,
                               description -> description.text("Pattern types must have a valid pattern.")
                                                         .appliesTo("Pattern types"),
                               SEVERITY);

    public EnPatternMustBeValid(SnapshotManager manager) {
        super(manager,
              EnPatternType.class,
              RULE);
    }

    public static boolean isValid(String pattern) {
        try {
            Pattern.compile(pattern);
            return true;
        } catch (final PatternSyntaxException e) {
            return false;
        }
    }

    @Override
    public CheckResult check(CheckContext context,
                             EnPatternType object,
                             Location location) {
        final String pattern = object.getPattern();
        if (EnPatternIsMandatory.isDefined(pattern)
                && !isValid(pattern)) {
            final EnIssueDescription.Builder description = EnIssueDescription.builder();

            description.header(getHeader(object))
                       .violation("has an invalid pattern: '" + pattern + "'");

            add(issue().description(description)
                       .location(location)
                       .build());
            return CheckResult.FAILURE;

        } else {
            return CheckResult.SUCCESS;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy