Skip to content

Commit

Permalink
Refactored validator implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
benfortuna committed Mar 6, 2022
1 parent 261375b commit 2858d19
Show file tree
Hide file tree
Showing 7 changed files with 1,126 additions and 463 deletions.
246 changes: 11 additions & 235 deletions src/main/java/net/fortuna/ical4j/validate/CalendarValidatorImpl.java
@@ -1,15 +1,12 @@
package net.fortuna.ical4j.validate;

import net.fortuna.ical4j.model.Calendar;
import net.fortuna.ical4j.model.Component;
import net.fortuna.ical4j.model.Property;
import net.fortuna.ical4j.model.PropertyContainer;
import net.fortuna.ical4j.model.component.CalendarComponent;
import net.fortuna.ical4j.model.property.*;
import net.fortuna.ical4j.util.CompatibilityHints;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

Expand All @@ -20,36 +17,32 @@ public class CalendarValidatorImpl implements Validator<Calendar> {

protected final List<Class<? extends Property>> calendarProperties = new ArrayList<>();

private final List<ValidationRule> rules;
private final PropertyContainerRuleSet<Calendar> rules;

public CalendarValidatorImpl(ValidationRule... rules) {
this.rules = Arrays.asList(rules);
this.rules = new PropertyContainerRuleSet<>(rules);

Collections.addAll(calendarProperties, CalScale.class, Method.class, ProdId.class, Version.class,
Uid.class, LastModified.class, Url.class, RefreshInterval.class, Source.class, Color.class,
Name.class, Description.class, Categories.class, Image.class);
}

@Override
public void validate(Calendar target) throws ValidationException {
ValidationResult result = new ValidationResult();

for (ValidationRule rule : rules) {
result.getEntries().addAll(apply(rule, Calendar.VCALENDAR, (PropertyContainer) target));
}
public ValidationResult validate(Calendar target) throws ValidationException {
ValidationResult result = new ValidationResult(rules.apply(Calendar.VCALENDAR, target));

if (!CompatibilityHints.isHintEnabled(CompatibilityHints.KEY_RELAXED_VALIDATION)) {
// require VERSION:2.0 for RFC2445..
if (!Version.VERSION_2_0.equals(target.getProperty(Property.VERSION))) {
result.getEntries().add(new ValidationEntry("Unsupported Version: " + target.getProperty(Property.VERSION).getValue(),
ValidationEntry.Level.ERROR, Calendar.VCALENDAR));
ValidationEntry.Severity.ERROR, Calendar.VCALENDAR));
}
}

// must contain at least one component
if (target.getComponents().isEmpty()) {
result.getEntries().add(new ValidationEntry("Calendar must contain at least one component",
ValidationEntry.Level.ERROR, Calendar.VCALENDAR));
ValidationEntry.Severity.ERROR, Calendar.VCALENDAR));
}

// validate properties..
Expand All @@ -58,41 +51,16 @@ public void validate(Calendar target) throws ValidationException {

if (!(property instanceof XProperty) && !isCalendarProperty) {
result.getEntries().add(new ValidationEntry("Invalid property: " + property.getName(),
ValidationEntry.Level.ERROR, Calendar.VCALENDAR));
ValidationEntry.Severity.ERROR, Calendar.VCALENDAR));
}
}

// if (!CompatibilityHints.isHintEnabled(CompatibilityHints.KEY_RELAXED_VALIDATION)) {
// validate method..
final Method method = target.getProperty(Property.METHOD);
if (Method.PUBLISH.equals(method)) {
new PublishValidator().validate(target);
}
else if (Method.REQUEST.equals(target.getProperty(Property.METHOD))) {
new RequestValidator().validate(target);
}
else if (Method.REPLY.equals(target.getProperty(Property.METHOD))) {
new ReplyValidator().validate(target);
}
else if (Method.ADD.equals(target.getProperty(Property.METHOD))) {
new AddValidator().validate(target);
}
else if (Method.CANCEL.equals(target.getProperty(Property.METHOD))) {
new CancelValidator().validate(target);
}
else if (Method.REFRESH.equals(target.getProperty(Property.METHOD))) {
new RefreshValidator().validate(target);
}
else if (Method.COUNTER.equals(target.getProperty(Property.METHOD))) {
new CounterValidator().validate(target);
}
else if (Method.DECLINE_COUNTER.equals(target.getProperty(Property.METHOD))) {
new DeclineCounterValidator().validate(target);
}
// }

// perform ITIP validation on components..
if (method != null) {
result.getEntries().addAll(new ITIPValidator().validate(target).getEntries());

// perform ITIP validation on components..
for (CalendarComponent component : target.getComponents()) {
component.validate(method);
}
Expand All @@ -101,198 +69,6 @@ else if (Method.DECLINE_COUNTER.equals(target.getProperty(Property.METHOD))) {
if (result.hasErrors()) {
throw new ValidationException(result);
}
}

public static class PublishValidator implements Validator<Calendar> {

@Override
public void validate(Calendar target) throws ValidationException {
if (target.getComponent(Component.VEVENT) != null) {
ComponentValidator.assertNone(Component.VFREEBUSY, target.getComponents());
ComponentValidator.assertNone(Component.VJOURNAL, target.getComponents());

if (!CompatibilityHints.isHintEnabled(CompatibilityHints.KEY_RELAXED_VALIDATION)) {
ComponentValidator.assertNone(Component.VTODO, target.getComponents());
}
}
else if (target.getComponent(Component.VFREEBUSY) != null) {
ComponentValidator.assertNone(Component.VTODO, target.getComponents());
ComponentValidator.assertNone(Component.VJOURNAL, target.getComponents());
ComponentValidator.assertNone(Component.VTIMEZONE, target.getComponents());
ComponentValidator.assertNone(Component.VALARM, target.getComponents());
}
else if (target.getComponent(Component.VTODO) != null) {
// ComponentValidator.assertNone(Component.VFREEBUSY, target.getComponents());
// ComponentValidator.assertNone(Component.VEVENT, target.getComponents());
ComponentValidator.assertNone(Component.VJOURNAL, target.getComponents());
}
// else if (target.getComponent(Component.VJOURNAL) != null) {
// ComponentValidator.assertNone(Component.VFREEBUSY, target.getComponents());
// ComponentValidator.assertNone(Component.VEVENT, target.getComponents());
// ComponentValidator.assertNone(Component.VTODO, target.getComponents());
// }
}
}

public static class RequestValidator implements Validator<Calendar> {

@Override
public void validate(Calendar target) throws ValidationException {
if (target.getComponent(Component.VEVENT) != null) {
ComponentValidator.assertNone(Component.VFREEBUSY, target.getComponents());
ComponentValidator.assertNone(Component.VJOURNAL, target.getComponents());
ComponentValidator.assertNone(Component.VTODO, target.getComponents());
}
else if (target.getComponent(Component.VFREEBUSY) != null) {
ComponentValidator.assertNone(Component.VTODO, target.getComponents());
ComponentValidator.assertNone(Component.VJOURNAL, target.getComponents());
ComponentValidator.assertNone(Component.VTIMEZONE, target.getComponents());
ComponentValidator.assertNone(Component.VALARM, target.getComponents());
}
else if (target.getComponent(Component.VTODO) != null) {
// ComponentValidator.assertNone(Component.VFREEBUSY, target.getComponents());
// ComponentValidator.assertNone(Component.VEVENT, target.getComponents());
ComponentValidator.assertNone(Component.VJOURNAL, target.getComponents());
}
}
}

public static class ReplyValidator implements Validator<Calendar> {

@Override
public void validate(Calendar target) throws ValidationException {
if (target.getComponent(Component.VEVENT) != null) {
ComponentValidator.assertOneOrLess(Component.VTIMEZONE, target.getComponents());

ComponentValidator.assertNone(Component.VALARM, target.getComponents());
ComponentValidator.assertNone(Component.VFREEBUSY, target.getComponents());
ComponentValidator.assertNone(Component.VJOURNAL, target.getComponents());
ComponentValidator.assertNone(Component.VTODO, target.getComponents());
}
else if (target.getComponent(Component.VFREEBUSY) != null) {
ComponentValidator.assertNone(Component.VTODO, target.getComponents());
ComponentValidator.assertNone(Component.VJOURNAL, target.getComponents());
ComponentValidator.assertNone(Component.VTIMEZONE, target.getComponents());
ComponentValidator.assertNone(Component.VALARM, target.getComponents());
}
else if (target.getComponent(Component.VTODO) != null) {
ComponentValidator.assertOneOrLess(Component.VTIMEZONE, target.getComponents());

ComponentValidator.assertNone(Component.VALARM, target.getComponents());
// ComponentValidator.assertNone(Component.VFREEBUSY, target.getComponents());
// ComponentValidator.assertNone(Component.VEVENT, target.getComponents());
ComponentValidator.assertNone(Component.VJOURNAL, target.getComponents());
}
}
}

public static class AddValidator implements Validator<Calendar> {

@Override
public void validate(Calendar target) throws ValidationException {
if (target.getComponent(Component.VEVENT) != null) {
ComponentValidator.assertNone(Component.VFREEBUSY, target.getComponents());
ComponentValidator.assertNone(Component.VJOURNAL, target.getComponents());
ComponentValidator.assertNone(Component.VTODO, target.getComponents());
}
else if (target.getComponent(Component.VTODO) != null) {
ComponentValidator.assertNone(Component.VFREEBUSY, target.getComponents());
// ComponentValidator.assertNone(Component.VEVENT, target.getComponents());
ComponentValidator.assertNone(Component.VJOURNAL, target.getComponents());
}
else if (target.getComponent(Component.VJOURNAL) != null) {
ComponentValidator.assertOneOrLess(Component.VTIMEZONE, target.getComponents());

ComponentValidator.assertNone(Component.VFREEBUSY, target.getComponents());
// ComponentValidator.assertNone(Component.VEVENT, target.getComponents());
// ComponentValidator.assertNone(Component.VTODO, target.getComponents());
}
}
}

public static class CancelValidator implements Validator<Calendar> {

@Override
public void validate(Calendar target) throws ValidationException {
if (target.getComponent(Component.VEVENT) != null) {
ComponentValidator.assertNone(Component.VALARM, target.getComponents());
ComponentValidator.assertNone(Component.VFREEBUSY, target.getComponents());
ComponentValidator.assertNone(Component.VJOURNAL, target.getComponents());
ComponentValidator.assertNone(Component.VTODO, target.getComponents());
}
else if (target.getComponent(Component.VTODO) != null) {
ComponentValidator.assertOneOrLess(Component.VTIMEZONE, target.getComponents());

ComponentValidator.assertNone(Component.VALARM, target.getComponents());
ComponentValidator.assertNone(Component.VFREEBUSY, target.getComponents());
// ComponentValidator.assertNone(Component.VEVENT, target.getComponents());
ComponentValidator.assertNone(Component.VJOURNAL, target.getComponents());
}
else if (target.getComponent(Component.VJOURNAL) != null) {
ComponentValidator.assertNone(Component.VALARM, target.getComponents());
ComponentValidator.assertNone(Component.VFREEBUSY, target.getComponents());
// ComponentValidator.assertNone(Component.VEVENT, target.getComponents());
// ComponentValidator.assertNone(Component.VTODO, target.getComponents());
}
}
}

public static class RefreshValidator implements Validator<Calendar> {

@Override
public void validate(Calendar target) throws ValidationException {
if (target.getComponent(Component.VEVENT) != null) {
ComponentValidator.assertNone(Component.VALARM, target.getComponents());
ComponentValidator.assertNone(Component.VFREEBUSY, target.getComponents());
ComponentValidator.assertNone(Component.VJOURNAL, target.getComponents());
ComponentValidator.assertNone(Component.VTODO, target.getComponents());
}
else if (target.getComponent(Component.VTODO) != null) {
ComponentValidator.assertNone(Component.VALARM, target.getComponents());
ComponentValidator.assertNone(Component.VFREEBUSY, target.getComponents());
// ComponentValidator.assertNone(Component.VEVENT, target.getComponents());
ComponentValidator.assertNone(Component.VJOURNAL, target.getComponents());
ComponentValidator.assertNone(Component.VTIMEZONE, target.getComponents());
}
}
}

public static class CounterValidator implements Validator<Calendar> {

@Override
public void validate(Calendar target) throws ValidationException {
if (target.getComponent(Component.VEVENT) != null) {
ComponentValidator.assertNone(Component.VFREEBUSY, target.getComponents());
ComponentValidator.assertNone(Component.VJOURNAL, target.getComponents());
ComponentValidator.assertNone(Component.VTODO, target.getComponents());
}
else if (target.getComponent(Component.VTODO) != null) {
ComponentValidator.assertOneOrLess(Component.VTIMEZONE, target.getComponents());

ComponentValidator.assertNone(Component.VFREEBUSY, target.getComponents());
// ComponentValidator.assertNone(Component.VEVENT, target.getComponents());
ComponentValidator.assertNone(Component.VJOURNAL, target.getComponents());
}
}
}

public static class DeclineCounterValidator implements Validator<Calendar> {

@Override
public void validate(Calendar target) throws ValidationException {
if (target.getComponent(Component.VEVENT) != null) {
ComponentValidator.assertNone(Component.VFREEBUSY, target.getComponents());
ComponentValidator.assertNone(Component.VJOURNAL, target.getComponents());
ComponentValidator.assertNone(Component.VTODO, target.getComponents());
ComponentValidator.assertNone(Component.VTIMEZONE, target.getComponents());
ComponentValidator.assertNone(Component.VALARM, target.getComponents());
}
else if (target.getComponent(Component.VTODO) != null) {
ComponentValidator.assertNone(Component.VALARM, target.getComponents());
ComponentValidator.assertNone(Component.VFREEBUSY, target.getComponents());
// ComponentValidator.assertNone(Component.VEVENT, target.getComponents());
ComponentValidator.assertNone(Component.VJOURNAL, target.getComponents());
}
}
return result;
}
}

0 comments on commit 2858d19

Please sign in to comment.