Skip to content

Commit

Permalink
Support for warning only when relaxed validation enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
benfortuna committed Sep 24, 2019
1 parent d1d24e8 commit 19da319
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 70 deletions.
34 changes: 16 additions & 18 deletions src/main/java/net/fortuna/ical4j/validate/ComponentValidator.java
Expand Up @@ -37,6 +37,8 @@

import java.util.List;

import static net.fortuna.ical4j.validate.Validator.assertFalse;

/**
* @author Ben
*
Expand All @@ -56,27 +58,25 @@ public ComponentValidator(List<ValidationRule> rules) {
@Override
public void validate(T target) throws ValidationException {
for (ValidationRule rule : rules) {
if (CompatibilityHints.isHintEnabled(CompatibilityHints.KEY_RELAXED_VALIDATION)
&& rule.isRelaxedModeSupported()) {
continue;
}
boolean warnOnly = CompatibilityHints.isHintEnabled(CompatibilityHints.KEY_RELAXED_VALIDATION)
&& rule.isRelaxedModeSupported();

switch (rule.getType()) {
case None:
rule.getInstances().forEach(s -> PropertyValidator.assertNone(s,
target.getProperties()));
rule.getInstances().forEach(s -> assertFalse(input -> input.getProperty(s) != null,
PropertyValidator.ASSERT_NONE_MESSAGE, warnOnly, target.getProperties(), s));
break;
case One:
rule.getInstances().forEach(s -> PropertyValidator.assertOne(s,
target.getProperties()));
rule.getInstances().forEach(s -> assertFalse(input -> input.getProperties(s).size() != 1,
PropertyValidator.ASSERT_ONE_MESSAGE, warnOnly, target.getProperties(), s));
break;
case OneOrLess:
rule.getInstances().forEach(s -> PropertyValidator.assertOneOrLess(s,
target.getProperties()));
rule.getInstances().forEach(s -> assertFalse(input -> input.getProperties(s).size() > 1,
PropertyValidator.ASSERT_ONE_OR_LESS_MESSAGE, warnOnly, target.getProperties(), s));
break;
case OneOrMore:
rule.getInstances().forEach(s -> PropertyValidator.assertOneOrMore(s,
target.getProperties()));
rule.getInstances().forEach(s -> assertFalse(input -> input.getProperties(s).size() < 1,
PropertyValidator.ASSERT_ONE_OR_MORE_MESSAGE, warnOnly, target.getProperties(), s));
break;
}
}
Expand All @@ -88,9 +88,8 @@ public void validate(T target) throws ValidationException {
* @throws ValidationException where the assertion fails
*/
public static void assertNone(String componentName, ComponentList<?> components) throws ValidationException {
if (components.getComponent(componentName) != null) {
throw new ValidationException(ASSERT_NONE_MESSAGE, new Object[] {componentName});
}
assertFalse(input -> input.getComponent(componentName) != null, ASSERT_NONE_MESSAGE, false,
components, componentName);
}

/**
Expand All @@ -99,8 +98,7 @@ public static void assertNone(String componentName, ComponentList<?> components)
* @throws ValidationException where the assertion fails
*/
public static void assertOneOrLess(String componentName, ComponentList<?> components) throws ValidationException {
if (components.getComponents(componentName).size() > 1) {
throw new ValidationException(ASSERT_ONE_OR_LESS_MESSAGE, new Object[] {componentName});
}
assertFalse(input -> input.getComponents(componentName).size() > 1, ASSERT_ONE_OR_LESS_MESSAGE, false,
components, componentName);
}
}
31 changes: 14 additions & 17 deletions src/main/java/net/fortuna/ical4j/validate/ParameterValidator.java
Expand Up @@ -34,6 +34,8 @@
import net.fortuna.ical4j.model.Parameter;
import net.fortuna.ical4j.model.ParameterList;

import static net.fortuna.ical4j.validate.Validator.assertFalse;

/**
* $Id$ [15-May-2004]
*
Expand All @@ -44,11 +46,11 @@
*/
public final class ParameterValidator {

private static final String ASSERT_NONE_MESSAGE = "Parameter [{0}] is not applicable";
public static final String ASSERT_NONE_MESSAGE = "Parameter [{0}] is not applicable";

private static final String ASSERT_ONE_OR_LESS_MESSAGE = "Parameter [{0}] must only be specified once";
public static final String ASSERT_ONE_OR_LESS_MESSAGE = "Parameter [{0}] must only be specified once";

private static final String ASSERT_ONE_MESSAGE = "Parameter [{0}] must be specified once";
public static final String ASSERT_ONE_MESSAGE = "Parameter [{0}] must be specified once";

private static final String ASSERT_NULL_OR_EQUAL_MESSAGE = "Parameter [{0}] is invalid";

Expand All @@ -68,12 +70,11 @@ private ParameterValidator() {
* @throws ValidationException
* when the specified parameter occurs more than once
*/
public static void assertOneOrLess(final String paramName,
final ParameterList parameters) throws ValidationException {
public static void assertOneOrLess(final String paramName, final ParameterList parameters)
throws ValidationException {

if (parameters.getParameters(paramName).size() > 1) {
throw new ValidationException(ASSERT_ONE_OR_LESS_MESSAGE, new Object[] {paramName});
}
assertFalse(parameters1 -> parameters1.getParameters(paramName).size() > 1, ASSERT_ONE_OR_LESS_MESSAGE, false,
parameters, paramName);
}

/**
Expand All @@ -86,12 +87,9 @@ public static void assertOneOrLess(final String paramName,
* @throws ValidationException
* when the specified parameter does not occur once
*/
public static void assertOne(final String paramName,
final ParameterList parameters) throws ValidationException {

if (parameters.getParameters(paramName).size() != 1) {
throw new ValidationException(ASSERT_ONE_MESSAGE, new Object[] {paramName});
}
public static void assertOne(final String paramName, final ParameterList parameters) throws ValidationException {
assertFalse(parameters1 -> parameters1.getParameters(paramName).size() != 1, ASSERT_ONE_MESSAGE, false,
parameters, paramName);
}

/**
Expand All @@ -102,9 +100,8 @@ public static void assertOne(final String paramName,
* is found in the list of properties
*/
public static void assertNone(final String paramName, final ParameterList parameters) throws ValidationException {
if (parameters.getParameter(paramName) != null) {
throw new ValidationException(ASSERT_NONE_MESSAGE, new Object[] {paramName});
}
assertFalse(parameters1 -> parameters1.getParameter(paramName) != null, ASSERT_NONE_MESSAGE, false,
parameters, paramName);
}

/**
Expand Down
60 changes: 25 additions & 35 deletions src/main/java/net/fortuna/ical4j/validate/PropertyValidator.java
Expand Up @@ -37,6 +37,8 @@

import java.util.List;

import static net.fortuna.ical4j.validate.Validator.assertFalse;

/**
* $Id$ [15-May-2004]
*
Expand All @@ -46,13 +48,13 @@
*/
public final class PropertyValidator implements Validator<Property> {

private static final String ASSERT_NONE_MESSAGE = "Property [{0}] is not applicable";
public static final String ASSERT_NONE_MESSAGE = "Property [{0}] is not applicable";

private static final String ASSERT_ONE_OR_LESS_MESSAGE = "Property [{0}] must only be specified once";
public static final String ASSERT_ONE_OR_LESS_MESSAGE = "Property [{0}] must only be specified once";

private static final String ASSERT_ONE_MESSAGE = "Property [{0}] must be specified once";
public static final String ASSERT_ONE_MESSAGE = "Property [{0}] must be specified once";

private static final String ASSERT_ONE_OR_MORE_MESSAGE = "Property [{0}] must be specified at least once";
public static final String ASSERT_ONE_OR_MORE_MESSAGE = "Property [{0}] must be specified at least once";

private final List<ValidationRule> rules;

Expand All @@ -63,23 +65,21 @@ public PropertyValidator(List<ValidationRule> rules) {
@Override
public void validate(Property target) throws ValidationException {
for (ValidationRule rule : rules) {
if (CompatibilityHints.isHintEnabled(CompatibilityHints.KEY_RELAXED_VALIDATION)
&& rule.isRelaxedModeSupported()) {
continue;
}
boolean warnOnly = CompatibilityHints.isHintEnabled(CompatibilityHints.KEY_RELAXED_VALIDATION)
&& rule.isRelaxedModeSupported();

switch (rule.getType()) {
case None:
rule.getInstances().forEach(s -> ParameterValidator.assertNone(s,
target.getParameters()));
rule.getInstances().forEach(s -> assertFalse(input -> input.getParameter(s) != null,
ParameterValidator.ASSERT_NONE_MESSAGE, warnOnly, target.getParameters(), s));
break;
case One:
rule.getInstances().forEach(s -> ParameterValidator.assertOne(s,
target.getParameters()));
rule.getInstances().forEach(s -> assertFalse(input -> input.getParameters(s).size() != 1,
ParameterValidator.ASSERT_ONE_MESSAGE, warnOnly, target.getParameters(), s));
break;
case OneOrLess:
rule.getInstances().forEach(s -> ParameterValidator.assertOneOrLess(s,
target.getParameters()));
rule.getInstances().forEach(s -> assertFalse(input -> input.getParameters(s).size() > 1,
ParameterValidator.ASSERT_ONE_OR_LESS_MESSAGE, warnOnly, target.getParameters(), s));
break;
}
}
Expand All @@ -95,12 +95,9 @@ public void validate(Property target) throws ValidationException {
* @throws ValidationException
* when the specified property occurs more than once
*/
public static void assertOneOrLess(final String propertyName,
final PropertyList properties) throws ValidationException {

if (properties.getProperties(propertyName).size() > 1) {
throw new ValidationException(ASSERT_ONE_OR_LESS_MESSAGE, new Object[] {propertyName});
}
public static void assertOneOrLess(final String propertyName, final PropertyList properties) throws ValidationException {
assertFalse(input -> input.getProperties(propertyName).size() > 1, ASSERT_ONE_OR_LESS_MESSAGE, false,
properties, propertyName);
}

/**
Expand All @@ -113,12 +110,9 @@ public static void assertOneOrLess(final String propertyName,
* @throws ValidationException
* when the specified property occurs more than once
*/
public static void assertOneOrMore(final String propertyName,
final PropertyList properties) throws ValidationException {

if (properties.getProperties(propertyName).size() < 1) {
throw new ValidationException(ASSERT_ONE_OR_MORE_MESSAGE, new Object[] {propertyName});
}
public static void assertOneOrMore(final String propertyName, final PropertyList properties) throws ValidationException {
assertFalse(input -> input.getProperties(propertyName).size() < 1, ASSERT_ONE_OR_MORE_MESSAGE, false,
properties, propertyName);
}

/**
Expand All @@ -131,12 +125,9 @@ public static void assertOneOrMore(final String propertyName,
* @throws ValidationException
* when the specified property does not occur once
*/
public static void assertOne(final String propertyName,
final PropertyList properties) throws ValidationException {

if (properties.getProperties(propertyName).size() != 1) {
throw new ValidationException(ASSERT_ONE_MESSAGE, new Object[] {propertyName});
}
public static void assertOne(final String propertyName, final PropertyList properties) throws ValidationException {
assertFalse(input -> input.getProperties(propertyName).size() != 1, ASSERT_ONE_MESSAGE, false,
properties, propertyName);
}

/**
Expand All @@ -147,8 +138,7 @@ public static void assertOne(final String propertyName,
* is found in the list of properties
*/
public static void assertNone(final String propertyName, final PropertyList properties) throws ValidationException {
if (properties.getProperty(propertyName) != null) {
throw new ValidationException(ASSERT_NONE_MESSAGE, new Object[] {propertyName});
}
assertFalse(input -> input.getProperty(propertyName) != null, ASSERT_NONE_MESSAGE, false,
properties, propertyName);
}
}
16 changes: 16 additions & 0 deletions src/main/java/net/fortuna/ical4j/validate/Validator.java
Expand Up @@ -31,14 +31,30 @@
*/
package net.fortuna.ical4j.validate;

import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.text.MessageFormat;
import java.util.function.Predicate;

/**
* @author fortuna
*
*/
public interface Validator<T> extends Serializable {

static <T> void assertFalse(Predicate<T> predicate, String message, boolean warn, T components,
Object...messageParams) {

if (predicate.test(components)) {
if (warn) {
LoggerFactory.getLogger(Validator.class).warn(MessageFormat.format(message, messageParams));
} else {
throw new ValidationException(message, messageParams);
}
}
}

/**
* Validates the associated model against an applicable standard.
* @throws ValidationException where the model does not confirm to the applicable standard
Expand Down

0 comments on commit 19da319

Please sign in to comment.