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.ArrayList;
019import java.util.List;
020import java.util.Objects;
021import java.util.stream.Stream;
022
023import org.apache.xbean.finder.AnnotationFinder;
024import org.talend.sdk.component.api.component.Icon;
025import org.talend.sdk.component.api.component.Version;
026import org.talend.sdk.component.runtime.manager.reflect.IconFinder;
027import org.talend.sdk.component.tools.validator.Validators.ValidatorHelper;
028
029public class MetadataValidator implements Validator {
030
031    private final Validators.ValidatorHelper helper;
032
033    public MetadataValidator(final ValidatorHelper helper) {
034        this.helper = helper;
035    }
036
037    @Override
038    public Stream<String> validate(final AnnotationFinder finder, final List<Class<?>> components) {
039        return components.stream().flatMap(component -> {
040            final Stream<String> iconErrors = this.findIconsError(component);
041
042            final Stream<String> versionErrors = this.findVersionsError(component);
043
044            return Stream.concat(iconErrors, versionErrors);
045        }).filter(Objects::nonNull).sorted();
046    }
047
048    private Stream<String> findIconsError(final Class<?> component) {
049        final IconFinder iconFinder = new IconFinder();
050        if (iconFinder.findDirectIcon(component).isPresent()) {
051            final Icon icon = component.getAnnotation(Icon.class);
052            final List<String> messages = new ArrayList<>();
053            messages.add(helper.validateIcon(icon, messages));
054            return messages.stream();
055        } else if (!iconFinder.findIndirectIcon(component).isPresent()) {
056            return Stream.of("No @Icon on " + component);
057        }
058        return Stream.empty();
059    }
060
061    private Stream<String> findVersionsError(final Class<?> component) {
062        if (!component.isAnnotationPresent(Version.class)) {
063            return Stream.of("Component " + component + " should use @Icon and @Version");
064        }
065        return Stream.empty();
066    }
067}