com.relogiclabs.json.schema.tree.SchemaTree Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of relogiclabs-json-schema Show documentation
Show all versions of relogiclabs-json-schema Show documentation
The New JSON Schema prioritizes simplicity, conciseness, and readability, making
it user-friendly and accessible without the need for extensive prior knowledge.
It offers efficient read-write facilities, precise JSON document definition
through various data types and functions, and extensibility to meet modern web
service diverse requirements.
package com.relogiclabs.json.schema.tree;
import com.relogiclabs.json.schema.internal.antlr.SchemaLexer;
import com.relogiclabs.json.schema.internal.antlr.SchemaParser;
import com.relogiclabs.json.schema.internal.tree.SchemaTreeVisitor;
import com.relogiclabs.json.schema.internal.util.LexerErrorListener;
import com.relogiclabs.json.schema.internal.util.ParserErrorListener;
import com.relogiclabs.json.schema.type.JRoot;
import lombok.Getter;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import static com.relogiclabs.json.schema.tree.TreeType.SCHEMA_TREE;
@Getter
public final class SchemaTree implements DataTree {
private final RuntimeContext runtime;
private final JRoot root;
public SchemaTree(RuntimeContext runtime, String input) {
this.runtime = runtime;
var schemaLexer = new SchemaLexer(CharStreams.fromString(input));
schemaLexer.removeErrorListeners();
schemaLexer.addErrorListener(LexerErrorListener.SCHEMA);
var schemaParser = new SchemaParser(new CommonTokenStream(schemaLexer));
schemaParser.removeErrorListeners();
schemaParser.addErrorListener(ParserErrorListener.SCHEMA);
root = (JRoot) new SchemaTreeVisitor(runtime).visit(schemaParser.schema());
}
@Override
public boolean match(DataTree dataTree) {
var result = root.match(dataTree.getRoot());
result &= runtime.invokeValidators();
return result;
}
@Override
public TreeType getType() {
return SCHEMA_TREE;
}
}