
org.everit.json.schema.loader.ClassPathAwareSchemaClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of org.everit.json.schema Show documentation
Show all versions of org.everit.json.schema Show documentation
This is a fork of the implementation of the JSON Schema Core Draft v4 specification built with the org.json API which also supports internationalization
The newest version!
package org.everit.json.schema.loader;
import static java.util.Arrays.asList;
import static java.util.Collections.unmodifiableList;
import static java.util.Objects.requireNonNull;
import java.io.InputStream;
import java.util.List;
import java.util.Optional;
class ClassPathAwareSchemaClient implements SchemaClient {
private static final List HANDLED_PREFIXES = unmodifiableList(asList("classpath://", "classpath:/", "classpath:"));
private final SchemaClient fallbackClient;
ClassPathAwareSchemaClient(SchemaClient fallbackClient) {
this.fallbackClient = requireNonNull(fallbackClient, "fallbackClient cannot be null");
}
@Override public InputStream get(String url) {
return handleProtocol(url)
.map(this::loadFromClasspath)
.orElseGet(() -> fallbackClient.get(url));
}
private InputStream loadFromClasspath(String str) {
return getClass().getResourceAsStream(str);
}
private Optional handleProtocol(String url) {
return HANDLED_PREFIXES.stream().filter(url::startsWith)
.map(prefix -> "/" + url.substring(prefix.length()))
.findFirst();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy