public final class JsonRef extends Object
JSON Reference, currently a draft, is a way to define a path within a JSON document.
To quote the draft, "A JSON Reference is a JSON object, which contains a member named "$ref", which has a JSON string value." This string value must be a URI. Example:
{
"$ref": "http://example.com/example.json#/foo/bar"
}
This class is used in a more general way than the draft. It is also used as a backing class for schema identifiers.
The implementation is a wrapper over Java's URI, with the
following differences:
This class is thread safe and immutable.
| Modifier and Type | Field and Description |
|---|---|
private static JsonRef |
EMPTY
An empty JSON Reference
|
private static URI |
EMPTY_URI
Empty URI
|
private JsonFragment |
fragment
The fragment of this JSON Reference
|
private int |
hashCode |
private static URI |
HASHONLY_URI
URI with only an empty fragment part
|
private URI |
locator
The locator for this fragment
|
private URI |
uri
The URI, as provided by the input, with an appended empty fragment if
no fragment was provided
|
| Modifier | Constructor and Description |
|---|---|
private |
JsonRef(URI uri)
The main constructor, which is private by design
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
contains(JsonRef other)
Tell whether the current JSON Reference contains another
|
static JsonRef |
emptyRef()
Return an empty reference
|
boolean |
equals(Object obj) |
static JsonRef |
fromNode(JsonNode node)
Build a JSON Reference from a
JsonNode |
static JsonRef |
fromString(String s)
Build a JSON Reference from a string input
|
static JsonRef |
fromURI(URI uri)
Build a JSON Reference from a URI
|
JsonFragment |
getFragment()
Return this JSON Reference's fragment
|
URI |
getRootAsURI()
Return this JSON Reference's locator
|
int |
hashCode() |
boolean |
isAbsolute()
Tell whether this reference is an absolute reference
|
JsonRef |
resolve(JsonRef other)
Resolve this reference against another reference
|
private URI |
resolveJarURI(JsonRef other)
Resolve a relative reference against a
jar URI |
String |
toString() |
private static final URI HASHONLY_URI
private static final URI EMPTY_URI
private static final JsonRef EMPTY
private final URI uri
private final URI locator
private final JsonFragment fragment
private final int hashCode
private JsonRef(URI uri)
If the provided URI has no fragment, then an empty one is appended.
uri - Input URIpublic static JsonRef fromURI(URI uri)
uri - the provided URINullPointerException - the provided URI is nullJsonRef(URI)public static JsonRef fromString(String s) throws JsonSchemaException
s - the stringJsonSchemaException - string is not a valid URINullPointerException - provided string is nullpublic static JsonRef fromNode(JsonNode node) throws JsonSchemaException
JsonNode
If the node is not textual, this returns an empty reference.
Otherwise, it calls fromString(String) with this node's text
value.
node - the nodeJsonSchemaException - see fromString(String)NullPointerException - provided node is nullpublic static JsonRef emptyRef()
An empty reference is a reference which only has an empty fragment.
public boolean isAbsolute()
A JSON Reference is considered absolute iif the underlying URI is itself absolute and it has an empty, or no, fragment part.
public JsonRef resolve(JsonRef other)
Note: jar URIs need to be special cased, see resolveJarURI(JsonRef).
other - the reference to resolvepublic URI getRootAsURI()
The locator of a fragment is the URI with an empty fragment.
public JsonFragment getFragment()
public boolean contains(JsonRef other)
This is considered true iif both references have the same locator, in other words, if they differ only by their fragment part.
other - the other referenceprivate URI resolveJarURI(JsonRef other)
jar URI
Unfortunately, URI.resolve(URI) does not work for these,
because of the way JAR URIs are built. An example JAR URI is:
jar:file:/path/to/my.jar!/jar/entry.json#fragmentHere
As per URI rules, this URI has no host and no path, just a "scheme
specific part" containing everyting after the scheme. Which means that
using .resolve(x) on such a URI for whatever URI x will
return... Yes, x. This is how URI resolution is supposed to work.
We therefore handle these specially in this method:
! (we are
guaranteed that there is one: we cannot enter this whole class if the
URI did not resolve successfully in the first place), make a URI out
of it and use classic URI resolution rules to resolve that extracted
path against the other URI;This means that, for instance, resolving ../x.json# against
the URI above will lead to jar:file:/path/to/my.jar!/x.json#.
Sick. That's the price to pay for supporting the jar scheme in
the first place :(
other - the URI to resolve againstCopyright © 2012. All Rights Reserved.