cdc.mf.checks.atts.name.AbstractNameMustNotMatchPattern Maven / Gradle / Ivy
The newest version!
package cdc.mf.checks.atts.name;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.regex.Pattern;
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;
import cdc.mf.checks.IssueDescription;
import cdc.mf.checks.MfAbstractRuleChecker;
import cdc.mf.model.MfNameItem;
import cdc.util.strings.StringUtils;
/**
* Check that if an {@link MfNameItem} has a name, it does not match a pattern.
*
* @author Damien Carbonne
* @param The {@link MfNameItem} type.
*/
public abstract class AbstractNameMustNotMatchPattern extends MfAbstractRuleChecker {
public static final IssueSeverity SEVERITY = IssueSeverity.MAJOR;
private final Pattern pattern;
private final Set exceptions;
protected AbstractNameMustNotMatchPattern(SnapshotManager manager,
Class objectClass,
Rule rule,
String regex,
Collection exceptions) {
super(manager,
objectClass,
rule);
this.pattern = Pattern.compile(regex);
this.exceptions = Set.copyOf(exceptions);
}
protected AbstractNameMustNotMatchPattern(SnapshotManager manager,
Class objectClass,
Rule rule,
String regex) {
this(manager,
objectClass,
rule,
regex,
Collections.emptySet());
}
@Override
protected final String getHeader(O object) {
return getTheNameOfHeader(object);
}
@Override
public CheckResult check(CheckContext context,
O object,
Location location) {
final String name = object.getName();
if (!StringUtils.isNullOrEmpty(name) && !exceptions.contains(name)) {
final boolean matches = pattern.matcher(name).matches();
if (matches) {
final IssueDescription.Builder description = IssueDescription.builder();
description.header(getHeader(object))
.value(name)
.violation("matches " + pattern.pattern());
add(issue().description(description)
.location(location)
.build());
return CheckResult.FAILURE;
} else {
return CheckResult.SUCCESS;
}
} else {
return CheckResult.SUCCESS;
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy