com.github.fge.jsonschema.processing.ref.SchemaLoader Maven / Gradle / Ivy
/*
* Copyright (c) 2013, Francis Galiegue
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Lesser GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Lesser GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package com.github.fge.jsonschema.processing.ref;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jsonschema.processing.ProcessingException;
import com.github.fge.jsonschema.ref.JsonRef;
import com.github.fge.jsonschema.report.ProcessingMessage;
import com.github.fge.jsonschema.schema.SchemaBundle;
import com.github.fge.jsonschema.tree.JsonSchemaTree;
import com.google.common.base.Preconditions;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import java.net.URI;
import java.util.concurrent.ExecutionException;
import static com.github.fge.jsonschema.messages.RefProcessingMessages.*;
/**
* A JSON Schema registry
*
* All schema registering and downloading is done through this class.
*
* Note that if the id of a schema is not absolute (that is, the URI itself
* is absolute and it has no fragment part, or an empty fragment), then the
* whole schema will be considered anonymous.
*
* This class is thread safe.
*/
public final class SchemaLoader
{
/**
* The default namespace
*/
private final JsonRef namespace;
/**
* Schema cache
*/
private final LoadingCache cache;
/**
* Our dereferencing mode
*/
private final Dereferencing dereferencing;
/**
* Constructor
*
* @param manager the URI manager to use
* @param namespace this registry's namespace
* @param dereferencing our {@link Dereferencing} mode
*/
public SchemaLoader(final URIManager manager, final URI namespace,
final Dereferencing dereferencing)
{
this.dereferencing = dereferencing;
this.namespace = JsonRef.fromURI(namespace);
cache = CacheBuilder.newBuilder().maximumSize(100L)
.build(new CacheLoader()
{
@Override
public JsonNode load(final URI key)
throws ProcessingException
{
return manager.getContent(key);
}
});
}
public JsonSchemaTree load(final JsonNode schema)
{
Preconditions.checkNotNull(schema, "cannot register null schema");
return dereferencing.newTree(schema);
}
/**
* Get a schema tree from the given URI
*
* Note that if the URI is relative, it will be resolved against this
* registry's namespace, if any.
*
* @param uri the URI
* @return a schema tree
*/
public JsonSchemaTree get(final URI uri)
throws ProcessingException
{
final JsonRef ref = namespace.resolve(JsonRef.fromURI(uri));
final ProcessingMessage msg = new ProcessingMessage()
.put("uri", ref);
if (!ref.isAbsolute())
throw new ProcessingException(msg.msg(URI_NOT_ABSOLUTE));
final URI realURI = ref.toURI();
try {
final JsonNode node = cache.get(realURI);
return dereferencing.newTree(ref, node);
} catch (ExecutionException e) {
throw (ProcessingException) e.getCause();
}
}
/**
* Load a schema bundle into this registry
*
* As a schema bundle is guaranteed to have well-formed locators, schemas
* from a bundle can be directly injected into the cache.
*/
public void addBundle(final SchemaBundle bundle)
{
cache.putAll(bundle.getSchemas());
}
@Override
public String toString()
{
return cache.stats().toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy