ikxeno commented on code in PR #13276: URL: https://github.com/apache/maven/pull/13276#discussion_r4121722279
########## impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnval/JsonReporter.java: ########## @@ -0,0 +1,109 @@ +/* + * 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.maven.cling.invoker.mvnval; + +import java.util.List; +import java.util.function.Consumer; +import java.util.stream.Collectors; + +import org.apache.maven.api.annotations.Nullable; +import org.apache.maven.api.services.ModelProblem; + +/** + * Renders validation results as a JSON array, one object per validated file. Assembled by hand + * so the tool needs no dependency the distribution does not already carry. + */ +class JsonReporter implements Reporter { + + private static final char[] HEX = "0123456789abcdef".toCharArray(); + + private static final char FIRST_PRINTABLE_ASCII = 0x20; + private static final char LAST_PRINTABLE_ASCII = 0x7e; + + @Override + public void report(List<Report> reports, Consumer<String> out) { + out.accept(reports.stream().map(JsonReporter::toJson).collect(Collectors.joining(",", "[", "]"))); + } + + private static String toJson(Report report) { + StringBuilder sb = + new StringBuilder("{\"pom\":").append(quote(report.pom().toString())); + if (report.failure() != null) { + sb.append(",\"failure\":").append(quote(report.failure())); + } + return sb.append(",\"problems\":") + .append(report.problems().stream() + .map(problem -> toJson(problem, report)) + .collect(Collectors.joining(",", "[", "]"))) + .append('}') + .toString(); + } + + private static String toJson(ModelProblem problem, Report report) { + StringBuilder sb = new StringBuilder("{\"severity\":") + .append(quote(problem.getSeverity().name())) + .append(",\"message\":") + .append(quote(problem.getMessage())); + // Absent values are left out, not written as the -1 the model builder uses for "no location". + String source = report.sourceOf(problem); + if (source != null) { + sb.append(",\"source\":").append(quote(source)); + } + if (problem.getLineNumber() > 0) { + sb.append(",\"line\":").append(problem.getLineNumber()); + } + if (problem.getColumnNumber() > 0) { + sb.append(",\"column\":").append(problem.getColumnNumber()); + } + return sb.append('}').toString(); + } + + private static String quote(@Nullable String value) { Review Comment: Taken, thanks, addbec6. The builder only turns up once a character actually needs replacing now. -- 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]
