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

com.dimajix.shaded.everit.schema.ConditionalSchemaValidatingVisitor Maven / Gradle / Ivy

There is a newer version: 1.2.0-synapse3.3-spark3.3-hadoop3.3
Show newest version
package com.dimajix.shaded.everit.schema;

import static java.util.Arrays.asList;
import static java.util.Objects.requireNonNull;
import static com.dimajix.shaded.everit.schema.event.ConditionalSchemaValidationEvent.Keyword.ELSE;
import static com.dimajix.shaded.everit.schema.event.ConditionalSchemaValidationEvent.Keyword.IF;
import static com.dimajix.shaded.everit.schema.event.ConditionalSchemaValidationEvent.Keyword.THEN;

import com.dimajix.shaded.everit.schema.event.ConditionalSchemaMatchEvent;
import com.dimajix.shaded.everit.schema.event.ConditionalSchemaMismatchEvent;
import com.dimajix.shaded.everit.schema.event.ConditionalSchemaValidationEvent;

class ConditionalSchemaValidatingVisitor extends Visitor {

    private final Object subject;

    private final ValidatingVisitor owner;

    private ConditionalSchema conditionalSchema;

    private ValidationException ifSchemaException;

    ConditionalSchemaValidatingVisitor(Object subject, ValidatingVisitor owner) {
        this.subject = subject;
        this.owner = requireNonNull(owner, "owner cannot be null");
    }

    @Override
    void visitConditionalSchema(ConditionalSchema conditionalSchema) {
        this.conditionalSchema = conditionalSchema;
        if (!conditionalSchema.getIfSchema().isPresent() ||
                (!conditionalSchema.getThenSchema().isPresent() && !conditionalSchema.getElseSchema().isPresent())) {
            return;
        }
        super.visitConditionalSchema(conditionalSchema);
    }

    @Override
    void visitIfSchema(Schema ifSchema) {
        if (conditionalSchema.getIfSchema().isPresent()) {
            ifSchemaException = owner.getFailureOfSchema(ifSchema, subject);
            if (ifSchemaException == null) {
                owner.validationListener.ifSchemaMatch(createMatchEvent(IF));
            } else {
                owner.validationListener.ifSchemaMismatch(createMismatchEvent(IF, ifSchemaException));
            }
        }
    }

    @Override
    void visitThenSchema(Schema thenSchema) {
        if (ifSchemaException == null) {
            ValidationException thenSchemaException = owner.getFailureOfSchema(thenSchema, subject);
            if (thenSchemaException != null) {
                ValidationException failure = new InternalValidationException(conditionalSchema,
                        new StringBuilder(new StringBuilder("#")),
                        "input is invalid against the \"then\" schema",
                        asList(thenSchemaException),
                        "then",
                        conditionalSchema.getSchemaLocation());

                owner.validationListener.thenSchemaMismatch(createMismatchEvent(THEN, thenSchemaException));
                owner.failure(failure);
            } else {
                owner.validationListener.thenSchemaMatch(createMatchEvent(THEN));
            }
        }
    }

    @Override
    void visitElseSchema(Schema elseSchema) {
        if (ifSchemaException != null) {
            ValidationException elseSchemaException = owner.getFailureOfSchema(elseSchema, subject);
            if (elseSchemaException != null) {
                ValidationException failure = new InternalValidationException(conditionalSchema,
                        new StringBuilder(new StringBuilder("#")),
                        "input is invalid against both the \"if\" and \"else\" schema",
                        asList(ifSchemaException, elseSchemaException),
                        "else",
                        conditionalSchema.getSchemaLocation());
                owner.validationListener.elseSchemaMismatch(createMismatchEvent(ELSE, elseSchemaException));
                owner.failure(failure);
            } else {
                owner.validationListener.elseSchemaMatch(createMatchEvent(ELSE));
            }
        }
    }

    private ConditionalSchemaMatchEvent createMatchEvent(ConditionalSchemaValidationEvent.Keyword keyword) {
        return new ConditionalSchemaMatchEvent(conditionalSchema, subject, keyword);
    }

    private ConditionalSchemaMismatchEvent createMismatchEvent(ConditionalSchemaValidationEvent.Keyword keyword,
            ValidationException failure) {
        return new ConditionalSchemaMismatchEvent(conditionalSchema, subject, keyword, failure);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy