001/** 002 * Copyright (C) 2006-2024 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 static java.util.Optional.ofNullable; 019 020import java.lang.reflect.AnnotatedElement; 021import java.util.ArrayList; 022import java.util.List; 023import java.util.stream.Stream; 024 025import org.apache.xbean.finder.AnnotationFinder; 026import org.talend.sdk.component.api.component.Icon; 027import org.talend.sdk.component.runtime.manager.reflect.IconFinder; 028import org.talend.sdk.component.tools.ComponentHelper; 029import org.talend.sdk.component.tools.validator.Validators.ValidatorHelper; 030 031public class FamilyValidator implements Validator { 032 033 private final Validators.ValidatorHelper helper; 034 035 private final IconFinder iconFinder = new IconFinder(); 036 037 public FamilyValidator(final ValidatorHelper helper) { 038 this.helper = helper; 039 } 040 041 @Override 042 public Stream<String> validate(final AnnotationFinder finder, final List<Class<?>> components) { 043 return components.stream().flatMap(this::validateComponentClass); 044 } 045 046 private Stream<String> validateComponentClass(final Class<?> component) { 047 List<String> errors = new ArrayList<>(); 048 try { 049 final Icon icon = ComponentHelper 050 .findPackageOrFail(component, (Class<?> clazz) -> clazz.isAnnotationPresent(Icon.class), 051 Icon.class.getName()) 052 .getAnnotation(Icon.class); 053 054 ofNullable(helper.validateIcon(icon, errors)).ifPresent(errors::add); 055 } catch (final IllegalArgumentException iae) { 056 try { 057 ComponentHelper.findPackageOrFail(component, this::isIndirectIconPresent, Icon.class.getName()); 058 } catch (final IllegalArgumentException iae2) { 059 errors.add(iae.getMessage()); 060 } 061 } 062 return errors.stream(); 063 } 064 065 private boolean isIndirectIconPresent(final AnnotatedElement ae) { 066 return iconFinder.findIndirectIcon(ae).isPresent(); 067 } 068}