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

xpertss.json.schema.core.load.uri.URITranslator Maven / Gradle / Ivy

The newest version!
package xpertss.json.schema.core.load.uri;

import xpertss.json.schema.core.load.SchemaLoader;
import xpertss.json.schema.core.ref.JsonRef;
import xpertss.json.schema.core.util.URIUtils;
import com.google.common.collect.ImmutableMap;

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

/**
 * URI translation
 *
 * 

When it is required that a URI be dereferenced (either by yourself, * using {@link SchemaLoader#get(URI)}, or when a JSON Reference is encountered * in a JSON Schema), this class is in charge of translating the * resolved URI into a more suitable URI for your environment.

* *

Translation is done in three steps:

* *
    *
  • resolving against the default namespace,
  • *
  • translating the path to another one (if applicable),
  • *
  • translating the full schema URI to another one (if applicable).
  • *
* *

By default, the namespace is empty and no path or schema translations are * defined.

* * @see URITranslatorConfiguration */ public final class URITranslator { private final URI namespace; private final Map pathRedirects; private final Map schemaRedirects; public URITranslator(URITranslatorConfiguration cfg) { namespace = cfg.namespace; pathRedirects = ImmutableMap.copyOf(cfg.pathRedirects); schemaRedirects = ImmutableMap.copyOf(cfg.schemaRedirects); } public URI translate(URI source) { URI uri = URIUtils.normalizeURI(namespace.resolve(source)); String fragment = uri.getFragment(); try { uri = new URI(uri.getScheme(), uri.getSchemeSpecificPart(), null); } catch (URISyntaxException e) { throw new IllegalStateException("How did I get there??", e); } for (final Map.Entry entry: pathRedirects.entrySet()) { final URI relative = entry.getKey().relativize(uri); if (!relative.equals(uri)) uri = entry.getValue().resolve(relative); } uri = JsonRef.fromURI(uri).getLocator(); if (schemaRedirects.containsKey(uri)) uri = schemaRedirects.get(uri); try { return new URI(uri.getScheme(), uri.getSchemeSpecificPart(), fragment); } catch (URISyntaxException e) { throw new IllegalStateException("How did I get there??", e); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy