gnodet-bot commented on code in PR #26379: URL: https://github.com/apache/camel/pull/26379#discussion_r4000659081
########## dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/BeanRefChecks.java: ########## @@ -0,0 +1,503 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.dsl.jbang.core.commands.ai; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.camel.catalog.CamelCatalog; +import org.apache.camel.dsl.jbang.core.commands.ai.SourceValidator.BeanDeclarations; +import org.apache.camel.tooling.model.EipModel; + +import static org.apache.camel.dsl.jbang.core.commands.ai.YamlLines.YAML_URI_PATTERN; +import static org.apache.camel.dsl.jbang.core.commands.ai.YamlLines.countLeadingSpaces; +import static org.apache.camel.dsl.jbang.core.commands.ai.YamlLines.unquote; + +/** + * The bean and resource reference checks of {@link SourceValidator}: every bean the YAML refers to is declared, + * implements what the option needs, and names a method when it has several; every stylesheet or template file exists. + */ +final class BeanRefChecks { + + private BeanRefChecks() { + } + + static final Pattern BEAN_NAME_PATTERN = Pattern.compile("^\\s*-?\\s*name:\\s*(\\S+)\\s*$"); + + static final Pattern BEAN_REF_PATTERN = Pattern.compile( + "^\\s*-?\\s*(ref|aggregationStrategy|strategyRef|processorRef|loadBalancerRef|executorServiceRef|onPrepareRef" + + "|onRedeliveryRef|aggregationRepositoryRef|comparatorRef|bean|processor)" + + ":\\s*(\\S+)\\s*$"); + + static final Pattern BEAN_TYPE_PATTERN = Pattern.compile("^\\s*type:\\s*[\"']?#class:([\\w.$]+)"); + + /** The beans declared under {@code beans:} with a {@code #class:} type, name to fully qualified class name. */ + static Map<String, String> declaredBeanTypes(String content) { + Map<String, String> types = new LinkedHashMap<>(); + if (content == null) { + return types; + } + String last = null; + for (String line : content.split("\n", -1)) { + Matcher nm = BEAN_NAME_PATTERN.matcher(line); + if (nm.find()) { + last = unquote(nm.group(1)); + continue; + } + Matcher tm = BEAN_TYPE_PATTERN.matcher(line); + if (tm.find() && last != null) { + types.put(last, tm.group(1)); + } + } + return types; + } + + /** The interface an option's bean must implement, for the options where a wrong class is a common mistake. */ Review Comment: 💡 **Nit:** Orphaned Javadoc — this old single-line comment is superseded by the new `/** Without a catalog: ... */` block immediately below it. Only the second block is picked up by the Javadoc tool; this one is dead text. ```suggestion ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
