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

cdc.mf.checks.atts.text.AbstractTextMustBeUnicode Maven / Gradle / Ivy

The newest version!
package cdc.mf.checks.atts.text;

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;

public abstract class AbstractTextMustBeUnicode extends MfAbstractRuleChecker {
    private static final Pattern UNICODE = Pattern.compile("^[^\\ufffd]*$");
    public static final IssueSeverity SEVERITY = IssueSeverity.MINOR;
    protected static final String MUST_BE_UNICODE = " must be Unicode.";

    protected AbstractTextMustBeUnicode(SnapshotManager manager,
                                        Class objectClass,
                                        Rule rule) {
        super(manager,
              objectClass,
              rule);
    }

    protected abstract String getText(O object);

    @Override
    public CheckResult check(CheckContext context,
                             O object,
                             Location location) {
        final String text = getText(object);
        if (text != null && !UNICODE.matcher(text).matches()) {
            final IssueDescription.Builder description = IssueDescription.builder();

            description.header(getHeader(object))
                       .value(text)
                       .violation("contains non-unicode character(s) at (0-based):");
            for (int index = 0; index < text.length(); index++) {
                final char c = text.charAt(index);
                if (!UNICODE.matcher(Character.toString(c)).matches()) {
                    description.uItem(Integer.toString(index));
                }
            }

            add(issue().description(description)
                       .location(location)
                       .build());
            return CheckResult.FAILURE;
        } else {
            return CheckResult.SUCCESS;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy