Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2012, Francis Galiegue <fgaliegue@gmail.com>
   *
   * 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 <http://www.gnu.org/licenses/>.
  */
 
 package com.github.fge.jsonschema.ref;
 
 
 import java.net.URI;
Representation of a JSON Reference

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 differs from the JSON Reference draft in one important way: normally, the fragment part of a JSON Reference must be a JSON Pointer; however, this means you cannot use a reference for addressing inner schemas when inline addressing mode is used. We therefore choose to accept all possible fragment parts.

The implementation is a wrapper over Java's java.net.URI, with the following differences:

  • all URIs are normalized from the get go;
  • an empty fragment is equivalent to no fragment at all, and stands for a root JSON Pointer;
  • a reference is taken to be absolute if the underlying URI is absolute and it has no fragment, or an empty fragment.

It also special cases the following:

  • an empty reference (for instance, used in anonymouns schemas);
  • URIs with the jar scheme (the resolving algorithm differs -- please note that this breaks URI resolution rules).

This class is thread safe and immutable.

 
 
 public abstract class JsonRef
 {
     private static final URI EMPTY_URI = URI.create("");
 
     protected static final URI HASHONLY_URI = URI.create("#");

    
The URI, as provided by the input, with an appended empty fragment if no fragment was provided
 
     protected final URI uri;

    
The locator of this reference. This is the URI with an empty fragment part.
 
     protected final URI locator;

    
The fragment of this reference.

See also:
JsonFragment
    protected final JsonFragment fragment;

    
String representation
    private final String asString;

    
Hashcode
    private final int hashCode;

    
Main constructor, protected by design

Parameters:
uri the URI to build that reference
    protected JsonRef(final URI uri)
    {
        final String scheme = uri.getScheme();
        final String ssp = uri.getSchemeSpecificPart();
        final String uriFragment = uri.getFragment();
        final String realFragment = uriFragment == null ? "" : uriFragment;
        try {
            this. = new URI(schemessprealFragment);
             = new URI(schemessp"");
             = JsonFragment.fromFragment(realFragment);
             = this..toString();
             = .hashCode();
        } catch (URISyntaxException e) {
            throw new RuntimeException("WTF??"e);
        }
    }
    
Build a JSON Reference from a URI

Parameters:
uri the provided URI
Returns:
the JSON Reference
Throws:
java.lang.NullPointerException the provided URI is null
    public static JsonRef fromURI(final URI uri)
    {
        Preconditions.checkNotNull(uri"URI must not be null");
        final URI normalized = uri.normalize();
        if (.equals(normalized) || .equals(normalized))
            return EmptyJsonRef.getInstance();
        return "jar".equals(normalized.getScheme())
            ? new JarJsonRef(normalized)
            : new HierarchicalJsonRef(normalized);
    }

    
Build a JSON Reference from a string input

Parameters:
s the string
Returns:
the reference
Throws:
com.github.fge.jsonschema.main.JsonSchemaException string is not a valid URI
java.lang.NullPointerException provided string is null
    public static JsonRef fromString(final String s)
        throws JsonSchemaException
    {
        Preconditions.checkNotNull(s"string must not be null");
        try {
            return fromURI(new URI(s));
        } catch (URISyntaxException e) {
            final Message.Builder msg = ..newMessage()
                .setKeyword("N/A").addInfo("uri"s).setMessage("invalid URI");
            throw JsonSchemaException.wrap(msg.build(), e);
        }
    }

    
Return an empty reference

An empty reference is a reference which only has an empty fragment.

Returns:
see above
    public static JsonRef emptyRef()
    {
        return EmptyJsonRef.getInstance();
    }

    
Return the underlying URI for this JSON Reference

Returns:
the URI
    public final URI toURI()
    {
        return ;
    }

    
Tell whether this reference is an absolute reference

A JSON Reference is considered absolute iif the underlying URI is itself absolute and it has an empty, or no, fragment part.

Returns:
see above
    public abstract boolean isAbsolute();

    
Resolve this reference against another reference

Parameters:
other the reference to resolve
Returns:
the resolved reference
    public abstract JsonRef resolve(final JsonRef other);

    
Return this JSON Reference's locator

This returns the reference with an empty fragment.

Returns:
an URI
    public final URI getLocator()
    {
        return ;
    }

    
Return this JSON Reference's fragment

Returns:
the fragment
    public final JsonFragment getFragment()
    {
        return ;
    }

    
Tell whether the current JSON Reference "contains" another

This is considered true iif both references have the same locator, in other words, if they differ only by their fragment part.

Parameters:
other the other reference
Returns:
see above
    public final boolean contains(final JsonRef other)
    {
        return .equals(other.locator);
    }
    @Override
    public final int hashCode()
    {
        return ;
    }
    @Override
    public final boolean equals(final Object obj)
    {
        if (obj == null)
            return false;
        if (this == obj)
            return true;
        if (!(obj instanceof JsonRef))
            return false;
        final JsonRef that = (JsonRefobj;
        return .equals(that.asString);
    }
    @Override
    public final String toString()
    {
        return ;
    }
New to GrepCode? Check out our FAQ X