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

dev.harrel.jsonschema.SchemaParsingContext Maven / Gradle / Ivy

package dev.harrel.jsonschema;

import java.net.URI;
import java.util.Map;

import static java.util.Collections.unmodifiableMap;

/**
 * {@code SchemaParsingContext} class represents state of current schema parsing process.
 * {@link EvaluatorFactory} can use this class for evaluator creation process.
 *
 * @see EvaluatorFactory
 */
public final class SchemaParsingContext {
    private final MetaSchemaData metaSchemaData;
    private final URI baseUri;
    private final URI parentUri;
    private final SchemaRegistry schemaRegistry;
    private final Map currentSchemaObject;

    private SchemaParsingContext(MetaSchemaData metaSchemaData, URI baseUri, URI parentUri, SchemaRegistry schemaRegistry, Map currentSchemaObject) {
        this.metaSchemaData = metaSchemaData;
        this.baseUri = baseUri;
        this.parentUri = parentUri;
        this.schemaRegistry = schemaRegistry;
        this.currentSchemaObject = currentSchemaObject;
    }

    SchemaParsingContext(MetaSchemaData metaSchemaData, SchemaRegistry schemaRegistry, URI baseUri, Map currentSchemaObject) {
        this(metaSchemaData, baseUri, baseUri, schemaRegistry, currentSchemaObject);
    }

    SchemaParsingContext forChild(MetaSchemaData metaSchemaData, Map currentSchemaObject, URI parentUri) {
        return new SchemaParsingContext(metaSchemaData, baseUri, parentUri, schemaRegistry, currentSchemaObject);
    }

    SchemaParsingContext forChild(Map currentSchemaObject) {
        return forChild(metaSchemaData, currentSchemaObject, parentUri);
    }

    MetaSchemaData getMetaValidationData() {
        return metaSchemaData;
    }

    SpecificationVersion getSpecificationVersion() {
        return metaSchemaData.dialect.getSpecificationVersion();
    }

    URI getBaseUri() {
        return baseUri;
    }

    /**
     * Returns URI of the closest parent schema that contains $id keyword.
     * If there is no such parent, then the URI of root schema is returned.
     */
    public URI getParentUri() {
        return parentUri;
    }

    /**
     * Calculates absolute URI to the provided {@code JsonNode}.
     *
     * @see SchemaParsingContext#getAbsoluteUri(String)
     */
    public String getAbsoluteUri(JsonNode node) {
        return getAbsoluteUri(node.getJsonPointer());
    }

    /**
     * Calculates absolute URI for the corresponding JSON pointer.
     *
     * @param jsonPointer JSON pointer string
     * @return absolute URI
     */
    public String getAbsoluteUri(String jsonPointer) {
        return baseUri + "#" + jsonPointer;
    }

    /**
     * Returns JSON object which is currently being parsed in form of map.
     *
     * @return unmodifiable map representing schema object
     */
    public Map getCurrentSchemaObject() {
        return unmodifiableMap(currentSchemaObject);
    }

    CompoundUri getCompoundUri(JsonNode node) {
        return new CompoundUri(baseUri, node.getJsonPointer());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy