gnodet-bot commented on code in PR #28586: URL: https://github.com/apache/camel/pull/28586#discussion_r4399709784
########## dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/RouteDiagramProblems.java: ########## @@ -0,0 +1,172 @@ +/* + * 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.action; + +import java.io.IOException; +import java.nio.charset.CharacterCodingException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.camel.catalog.CamelCatalog; +import org.apache.camel.catalog.DefaultCamelCatalog; +import org.apache.camel.diagram.RouteDiagramLayoutEngine.NodeInfo; +import org.apache.camel.diagram.RouteDiagramLayoutEngine.RouteInfo; +import org.apache.camel.dsl.jbang.core.commands.ai.RouteAssist; +import org.apache.camel.dsl.jbang.core.commands.ai.SourceValidator; + +/** + * The problems of the source files of a route diagram, on the steps they are of: the checks of {@code camel validate} + * for a YAML route, and for Java and XML the checks that read the source without compiling it ({@link RouteAssist}), as + * the route diagram reads Java routes without compiling them. + */ +final class RouteDiagramProblems { + + /** A problem of a line of the source; line 0 for a problem of the file. */ + record Problem(int line, String text) { + } + + /** The checks of a source file: {@link #problems}. */ + @FunctionalInterface + interface Checks { + List<Problem> problems(String name, String content, CamelCatalog catalog, Path directory); + } + + private static final Pattern MESSAGE_LINE = Pattern.compile("^Line (\\d+): (.*)$", Pattern.DOTALL); + // the step and route a message names first, which the diagram shows already: "setBody: route orders: " + private static final Pattern STEP_PREFIX = Pattern.compile("^[A-Za-z][\\w-]*: route [^:\\s]+: "); + + private RouteDiagramProblems() { + } + + /** + * Puts the problems of the source files on the steps of their routes ({@link NodeInfo#problems}): a problem on the + * step whose line is the last one before it (the line of the option or expression that is wrong is inside its + * step), a problem of the file on the first step: also that the file cannot be read or checked. + */ + static void attach(List<RouteInfo> routes, List<String> files) { + attach(routes, files, RouteDiagramProblems::problems); + } + + static void attach(List<RouteInfo> routes, List<String> files, Checks checks) { + CamelCatalog catalog = null; Review Comment: 💡 **Design note (non-blocking):** `Checks` is a package-private `@FunctionalInterface` accepted by the package-private `attach(routes, files, Checks)` overload. This is a clean testability seam — callers in tests can inject a lambda instead of the real checker. Worth noting that since both the interface and the overload are package-private, test classes in the same package can use them directly without any reflection or mocking framework. No action required; just confirming the design intent is sound. -- 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]
