diff --git a/src/main/java/net/fortuna/ical4j/validate/ComponentValidator.java b/src/main/java/net/fortuna/ical4j/validate/ComponentValidator.java index 683577566..88e6c1afa 100644 --- a/src/main/java/net/fortuna/ical4j/validate/ComponentValidator.java +++ b/src/main/java/net/fortuna/ical4j/validate/ComponentValidator.java @@ -37,6 +37,8 @@ import java.util.List; +import static net.fortuna.ical4j.validate.Validator.assertFalse; + /** * @author Ben * @@ -56,27 +58,25 @@ public ComponentValidator(List 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; } } @@ -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); } /** @@ -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); } } diff --git a/src/main/java/net/fortuna/ical4j/validate/ParameterValidator.java b/src/main/java/net/fortuna/ical4j/validate/ParameterValidator.java index 6fd7bff13..bf036eda9 100644 --- a/src/main/java/net/fortuna/ical4j/validate/ParameterValidator.java +++ b/src/main/java/net/fortuna/ical4j/validate/ParameterValidator.java @@ -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] * @@ -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"; @@ -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); } /** @@ -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); } /** @@ -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); } /** diff --git a/src/main/java/net/fortuna/ical4j/validate/PropertyValidator.java b/src/main/java/net/fortuna/ical4j/validate/PropertyValidator.java index 15cba1523..d95241326 100644 --- a/src/main/java/net/fortuna/ical4j/validate/PropertyValidator.java +++ b/src/main/java/net/fortuna/ical4j/validate/PropertyValidator.java @@ -37,6 +37,8 @@ import java.util.List; +import static net.fortuna.ical4j.validate.Validator.assertFalse; + /** * $Id$ [15-May-2004] * @@ -46,13 +48,13 @@ */ public final class PropertyValidator implements Validator { - 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 rules; @@ -63,23 +65,21 @@ public PropertyValidator(List 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; } } @@ -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); } /** @@ -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); } /** @@ -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); } /** @@ -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); } } diff --git a/src/main/java/net/fortuna/ical4j/validate/Validator.java b/src/main/java/net/fortuna/ical4j/validate/Validator.java index 3497ee8e6..d35ee27aa 100644 --- a/src/main/java/net/fortuna/ical4j/validate/Validator.java +++ b/src/main/java/net/fortuna/ical4j/validate/Validator.java @@ -31,7 +31,11 @@ */ package net.fortuna.ical4j.validate; +import org.slf4j.LoggerFactory; + import java.io.Serializable; +import java.text.MessageFormat; +import java.util.function.Predicate; /** * @author fortuna @@ -39,6 +43,18 @@ */ public interface Validator extends Serializable { + static void assertFalse(Predicate 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