Search in sources :

Example 1 with ValidationWarning

use of ezvcard.ValidationWarning in project ez-vcard by mangstadt.

the class VCardParameters method validate.

/**
 * <p>
 * Checks the parameters for data consistency problems or deviations from
 * the specification.
 * </p>
 * <p>
 * These problems will not prevent the vCard from being written to a data
 * stream*, but may prevent it from being parsed correctly by the consuming
 * application.
 * </p>
 * <p>
 * *With a few exceptions: One thing this method does is check for illegal
 * characters. There are certain characters that will break the vCard syntax
 * if written (such as a newline character in a parameter name). If one of
 * these characters is present, it WILL prevent the vCard from being
 * written.
 * </p>
 * @param version the vCard version to validate against
 * @return a list of warnings or an empty list if no problems were found
 */
public List<ValidationWarning> validate(VCardVersion version) {
    List<ValidationWarning> warnings = new ArrayList<ValidationWarning>(0);
    /*
		 * Check for invalid characters in names and values.
		 */
    SyntaxStyle syntax = version.getSyntaxStyle();
    for (Map.Entry<String, List<String>> entry : this) {
        String name = entry.getKey();
        /*
			 * Don't check LABEL parameter for 2.1 and 3.0 because this
			 * parameter is converted to a property in those versions.
			 */
        if (version != VCardVersion.V4_0 && LABEL.equalsIgnoreCase(name)) {
            continue;
        }
        // check the parameter name
        if (!VObjectValidator.validateParameterName(name, syntax, true)) {
            if (syntax == SyntaxStyle.OLD) {
                AllowedCharacters notAllowed = VObjectValidator.allowedCharactersParameterName(syntax, true).flip();
                warnings.add(new ValidationWarning(30, name, notAllowed.toString(true)));
            } else {
                warnings.add(new ValidationWarning(26, name));
            }
        }
        // check the parameter value(s)
        List<String> values = entry.getValue();
        for (String value : values) {
            /*
				 * Newlines are allowed in LABEL parameters, but are not allowed
				 * by vobject, so remove them from the value before validating.
				 */
            if (LABEL.equalsIgnoreCase(name)) {
                value = value.replaceAll("\r\n|\r|\n", "");
            }
            if (!VObjectValidator.validateParameterValue(value, syntax, false, true)) {
                AllowedCharacters notAllowed = VObjectValidator.allowedCharactersParameterValue(syntax, false, true).flip();
                int code = (syntax == SyntaxStyle.OLD) ? 31 : 25;
                warnings.add(new ValidationWarning(code, name, value, notAllowed.toString(true)));
            }
        }
    }
    /*
		 * Check for invalid or unsupported values (e.g. "ENCODING=foo").
		 */
    {
        final int nonStandardValueCode = 3;
        final int unsupportedValueCode = 4;
        String value = first(CALSCALE);
        if (value != null && Calscale.find(value) == null) {
            warnings.add(new ValidationWarning(nonStandardValueCode, CALSCALE, value, Calscale.all()));
        }
        value = first(ENCODING);
        if (value != null) {
            Encoding encoding = Encoding.find(value);
            if (encoding == null) {
                warnings.add(new ValidationWarning(nonStandardValueCode, ENCODING, value, Encoding.all()));
            } else if (!encoding.isSupportedBy(version)) {
                warnings.add(new ValidationWarning(unsupportedValueCode, ENCODING, value));
            }
        }
        value = first(VALUE);
        if (value != null) {
            VCardDataType dataType = VCardDataType.find(value);
            if (dataType == null) {
                warnings.add(new ValidationWarning(nonStandardValueCode, VALUE, value, VCardDataType.all()));
            } else if (!dataType.isSupportedBy(version)) {
                warnings.add(new ValidationWarning(unsupportedValueCode, VALUE, value));
            }
        }
    }
    /*
		 * Check for parameters with malformed values.
		 */
    {
        final int malformedValueCode = 5;
        try {
            getGeo();
        } catch (IllegalStateException e) {
            warnings.add(new ValidationWarning(malformedValueCode, GEO, first(GEO)));
        }
        try {
            Integer index = getIndex();
            if (index != null && index <= 0) {
                warnings.add(new ValidationWarning(28, index));
            }
        } catch (IllegalStateException e) {
            warnings.add(new ValidationWarning(malformedValueCode, INDEX, first(INDEX)));
        }
        List<String> pids = get(PID);
        for (String pid : pids) {
            if (!isPidValid(pid)) {
                warnings.add(new ValidationWarning(27, pid));
            }
        }
        try {
            Integer pref = getPref();
            if (pref != null && (pref < 1 || pref > 100)) {
                warnings.add(new ValidationWarning(29, pref));
            }
        } catch (IllegalStateException e) {
            warnings.add(new ValidationWarning(malformedValueCode, PREF, first(PREF)));
        }
    }
    /*
		 * Check that each parameter is supported by the given vCard version.
		 */
    {
        final int paramNotSupportedCode = 6;
        for (Map.Entry<String, Set<VCardVersion>> entry : supportedVersions.entrySet()) {
            String name = entry.getKey();
            String value = first(name);
            if (value == null) {
                continue;
            }
            Set<VCardVersion> versions = entry.getValue();
            if (!versions.contains(version)) {
                warnings.add(new ValidationWarning(paramNotSupportedCode, name));
            }
        }
    }
    /*
		 * Check that the CHARSET parameter has a character set that is
		 * supported by this JVM.
		 */
    {
        final int invalidCharsetCode = 22;
        String charsetStr = getCharset();
        if (charsetStr != null) {
            try {
                Charset.forName(charsetStr);
            } catch (IllegalCharsetNameException e) {
                warnings.add(new ValidationWarning(invalidCharsetCode, charsetStr));
            } catch (UnsupportedCharsetException e) {
                warnings.add(new ValidationWarning(invalidCharsetCode, charsetStr));
            }
        }
    }
    return warnings;
}
Also used : EnumSet(java.util.EnumSet) Set(java.util.Set) ArrayList(java.util.ArrayList) VCardVersion(ezvcard.VCardVersion) ValidationWarning(ezvcard.ValidationWarning) SortString(ezvcard.property.SortString) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) AllowedCharacters(com.github.mangstadt.vinnie.validate.AllowedCharacters) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) SyntaxStyle(com.github.mangstadt.vinnie.SyntaxStyle) AbstractList(java.util.AbstractList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) ClientPidMap(ezvcard.property.ClientPidMap) Map(java.util.Map) VCardDataType(ezvcard.VCardDataType)

Example 2 with ValidationWarning

use of ezvcard.ValidationWarning in project ez-vcard by mangstadt.

the class TestUtils method checkCodes.

public static boolean checkCodes(List<ValidationWarning> warnings, Integer... expectedCodes) {
    if (warnings.size() != expectedCodes.length) {
        return false;
    }
    /*
		 * Don't use a Set because there can be multiple warnings with the same
		 * code.
		 */
    List<Integer> expected = new ArrayList<Integer>(Arrays.asList(expectedCodes));
    for (ValidationWarning warning : warnings) {
        Integer code = warning.getCode();
        boolean removed = expected.remove((Object) code);
        if (!removed) {
            return false;
        }
    }
    return true;
}
Also used : ArrayList(java.util.ArrayList) ValidationWarning(ezvcard.ValidationWarning)

Example 3 with ValidationWarning

use of ezvcard.ValidationWarning in project ez-vcard by mangstadt.

the class Agent method _validate.

@Override
protected void _validate(List<ValidationWarning> warnings, VCardVersion version, VCard vcard) {
    if (url == null && this.vcard == null) {
        warnings.add(new ValidationWarning(8));
    }
    if (this.vcard != null) {
        NumberFormat nf = NumberFormat.getIntegerInstance();
        nf.setMinimumIntegerDigits(2);
        ValidationWarnings validationWarnings = this.vcard.validate(version);
        for (Map.Entry<VCardProperty, List<ValidationWarning>> entry : validationWarnings) {
            VCardProperty property = entry.getKey();
            List<ValidationWarning> propViolations = entry.getValue();
            for (ValidationWarning propViolation : propViolations) {
                String className = (property == null) ? "" : property.getClass().getSimpleName();
                int code = propViolation.getCode();
                String codeStr = (code >= 0) ? "W" + nf.format(code) : "";
                String message = propViolation.getMessage();
                warnings.add(new ValidationWarning(10, className, codeStr, message));
            }
        }
    }
}
Also used : ValidationWarnings(ezvcard.ValidationWarnings) List(java.util.List) ValidationWarning(ezvcard.ValidationWarning) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) NumberFormat(java.text.NumberFormat)

Example 4 with ValidationWarning

use of ezvcard.ValidationWarning in project ez-vcard by mangstadt.

the class VCardProperty method validate.

/**
 * Checks the property for data consistency problems or deviations from the
 * spec. These problems will not prevent the property from being written to
 * a data stream, but may prevent it from being parsed correctly by the
 * consuming application. These problems can largely be avoided by reading
 * the Javadocs of the property class, or by being familiar with the vCard
 * standard.
 * @param version the version to check the property against (use 4.0 for
 * xCard and jCard)
 * @param vcard the vCard this property belongs to
 * @see VCard#validate
 * @return a list of warnings or an empty list if no problems were found
 */
public final List<ValidationWarning> validate(VCardVersion version, VCard vcard) {
    List<ValidationWarning> warnings = new ArrayList<ValidationWarning>(0);
    // check the supported versions
    if (!isSupportedBy(version)) {
        warnings.add(new ValidationWarning(2, Arrays.toString(getSupportedVersions())));
    }
    // check parameters
    warnings.addAll(parameters.validate(version));
    // check group
    if (group != null) {
        SyntaxStyle syntax = version.getSyntaxStyle();
        AllowedCharacters allowed = VObjectValidator.allowedCharactersGroup(syntax, true);
        if (!allowed.check(group)) {
            if (syntax == SyntaxStyle.OLD) {
                AllowedCharacters notAllowed = allowed.flip();
                warnings.add(new ValidationWarning(32, group, notAllowed.toString(true)));
            } else {
                warnings.add(new ValidationWarning(23, group));
            }
        }
    }
    _validate(warnings, version, vcard);
    return warnings;
}
Also used : AllowedCharacters(com.github.mangstadt.vinnie.validate.AllowedCharacters) ArrayList(java.util.ArrayList) SyntaxStyle(com.github.mangstadt.vinnie.SyntaxStyle) ValidationWarning(ezvcard.ValidationWarning)

Example 5 with ValidationWarning

use of ezvcard.ValidationWarning in project ez-vcard by mangstadt.

the class RawProperty method _validate.

@Override
protected void _validate(List<ValidationWarning> warnings, VCardVersion version, VCard vcard) {
    SyntaxStyle syntax = version.getSyntaxStyle();
    AllowedCharacters allowed = VObjectValidator.allowedCharactersParameterName(syntax, true);
    if (!allowed.check(propertyName)) {
        if (syntax == SyntaxStyle.OLD) {
            AllowedCharacters notAllowed = allowed.flip();
            warnings.add(new ValidationWarning(33, propertyName, notAllowed.toString(true)));
        } else {
            warnings.add(new ValidationWarning(24, propertyName));
        }
    }
}
Also used : AllowedCharacters(com.github.mangstadt.vinnie.validate.AllowedCharacters) SyntaxStyle(com.github.mangstadt.vinnie.SyntaxStyle) ValidationWarning(ezvcard.ValidationWarning)

Aggregations

ValidationWarning (ezvcard.ValidationWarning)5 SyntaxStyle (com.github.mangstadt.vinnie.SyntaxStyle)3 AllowedCharacters (com.github.mangstadt.vinnie.validate.AllowedCharacters)3 ArrayList (java.util.ArrayList)3 List (java.util.List)2 Map (java.util.Map)2 VCardDataType (ezvcard.VCardDataType)1 VCardVersion (ezvcard.VCardVersion)1 ValidationWarnings (ezvcard.ValidationWarnings)1 ClientPidMap (ezvcard.property.ClientPidMap)1 SortString (ezvcard.property.SortString)1 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)1 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)1 NumberFormat (java.text.NumberFormat)1 AbstractList (java.util.AbstractList)1 EnumSet (java.util.EnumSet)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Set (java.util.Set)1