001/**
002 * Copyright (C) 2006-2025 Talend Inc. - www.talend.com
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.talend.sdk.component.tools.validator;
017
018import java.util.List;
019import java.util.Objects;
020import java.util.stream.Stream;
021
022import org.apache.xbean.finder.AnnotationFinder;
023import org.talend.sdk.component.runtime.manager.ParameterMeta;
024import org.talend.sdk.component.tools.spi.ValidationExtension;
025import org.talend.sdk.component.tools.spi.ValidationExtension.ValidationResult;
026import org.talend.sdk.component.tools.validator.Validators.ValidatorHelper;
027
028public class ExtensionValidator implements Validator {
029
030    private final ValidationExtension extension;
031
032    private final Validators.ValidatorHelper helper;
033
034    public ExtensionValidator(final ValidationExtension extension, final ValidatorHelper helper) {
035        this.extension = extension;
036        this.helper = helper;
037    }
038
039    @Override
040    public Stream<String> validate(final AnnotationFinder finder, final List<Class<?>> components) {
041        final ValidationExtension.ValidationContext context = new ValidationExtension.ValidationContext() {
042
043            @Override
044            public AnnotationFinder finder() {
045                return finder;
046            }
047
048            @Override
049            public List<Class<?>> components() {
050                return components;
051            }
052
053            @Override
054            public List<ParameterMeta> parameters(final Class<?> component) {
055                return helper.buildOrGetParameters(component);
056            }
057        };
058        final ValidationResult result = this.extension.validate(context);
059
060        final Stream<String> errors;
061        if (result == null || result.getErrors() == null) {
062            errors = Stream.empty();
063        } else {
064            errors = result.getErrors().stream();
065        }
066        return errors.filter(Objects::nonNull);
067    }
068}