All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openrewrite.json.JsonParser Maven / Gradle / Ivy

There is a newer version: 8.33.4
Show newest version
/*
 * Copyright 2021 the original author or authors.
 * 

* Licensed 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 *

* https://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.openrewrite.json; import io.micrometer.core.instrument.Metrics; import io.micrometer.core.instrument.Timer; import org.antlr.v4.runtime.*; import org.intellij.lang.annotations.Language; import org.openrewrite.ExecutionContext; import org.openrewrite.InMemoryExecutionContext; import org.openrewrite.Parser; import org.openrewrite.internal.MetricsHelper; import org.openrewrite.internal.StringUtils; import org.openrewrite.internal.lang.Nullable; import org.openrewrite.json.internal.JsonParserVisitor; import org.openrewrite.json.internal.grammar.JSON5Lexer; import org.openrewrite.json.internal.grammar.JSON5Parser; import org.openrewrite.json.tree.Json; import java.nio.file.Path; import java.util.List; import java.util.Objects; import static java.util.stream.Collectors.toList; public class JsonParser implements Parser { @Override public List parseInputs(Iterable sourceFiles, @Nullable Path relativeTo, ExecutionContext ctx) { return acceptedInputs(sourceFiles).stream() .map(sourceFile -> { Timer.Builder timer = Timer.builder("rewrite.parse") .description("The time spent parsing an Json file") .tag("file.type", "Json"); Timer.Sample sample = Timer.start(); try { JSON5Parser parser = new JSON5Parser(new CommonTokenStream(new JSON5Lexer( CharStreams.fromStream(sourceFile.getSource())))); parser.removeErrorListeners(); parser.addErrorListener(new ForwardingErrorListener(sourceFile.getPath(), ctx)); Json.Document document = new JsonParserVisitor( sourceFile.getRelativePath(relativeTo), StringUtils.readFully(sourceFile.getSource()) ).visitJson5(parser.json5()); sample.stop(MetricsHelper.successTags(timer).register(Metrics.globalRegistry)); return document; } catch (Throwable t) { sample.stop(MetricsHelper.errorTags(timer, t).register(Metrics.globalRegistry)); ctx.getOnError().accept(new IllegalStateException(sourceFile.getPath() + " " + t.getMessage(), t)); return null; } }) .filter(Objects::nonNull) .collect(toList()); } @Override public List parse(@Language("Json") String... sources) { return parse(new InMemoryExecutionContext(), sources); } @Override public boolean accept(Path path) { return path.toString().endsWith(".json"); } private static class ForwardingErrorListener extends BaseErrorListener { private final Path sourcePath; private final ExecutionContext ctx; private ForwardingErrorListener(Path sourcePath, ExecutionContext ctx) { this.sourcePath = sourcePath; this.ctx = ctx; } @Override public void syntaxError(Recognizer recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { ctx.getOnError().accept(new JsonParsingException(sourcePath, String.format("Syntax error in %s at line %d:%d %s.", sourcePath, line, charPositionInLine, msg), e)); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy