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.main;
 
 
 import java.net.URI;
 import java.util.Map;

Factory to build JSON Schema validating instances

You can create a factory with all default settings using defaultFactory(). This is what you will do in the vast majority of cases.

If you want to customize it, you need to go through JsonSchemaFactory.Builder.

This class is thread safe and immutable.

See also:
JsonSchema
com.github.fge.jsonschema.examples
 
 public final class JsonSchemaFactory
 {
    
Schema registry
 
     private final SchemaRegistry registry;

    
Default schema URI
 
     private final JsonRef defaultSchemaURI;

    
Map of schema URIs and validator caches
 
     private final Map<JsonRefJsonValidatorCachevalidatorCaches;

    
Build a factory with all default settings

Returns:
a schema factory instance
 
     public static JsonSchemaFactory defaultFactory()
     {
         return new Builder().build();
     }
 
     public static Builder builder()
     {
         return new Builder();
     }

    
Constructor, private by design

Parameters:
builder the builder
See also:
JsonSchemaFactory.Builder
 
     private JsonSchemaFactory(final Builder builder)
     {
          = new SchemaRegistry(builder.uriManagerbuilder.namespace,
             builder.addressingMode);
        .addBundle(builder.bundle);
         = builder.defaultSchemaURI;
        final ImmutableMap.Builder<JsonRefJsonValidatorCachecacheBuilder
            = ImmutableMap.builder();
        final Map<JsonRefMetaSchemamap = builder.metaSchemas;
        JsonRef ref;
        JsonValidatorCache validatorCache;
        for (final Map.Entry<JsonRefMetaSchemaentrymap.entrySet()) {
            ref = entry.getKey();
            validatorCache = new JsonValidatorCache(entry.getValue(), );
            cacheBuilder.put(refvalidatorCache);
        }
         = cacheBuilder.build();
    }

    
Create a schema instance from a JSON Schema, at a certain path

For instance, if you submit this schema:

     {
         "schema1": { ... },
         "schema2": { ... }
     }
 

then you can create a validator for schema1 using:

     final JsonSchema schema = factory.create(schema, "#/schema1");
 

The path can be a com.github.fge.jsonschema.ref.JsonPointer as above, but also an id reference.

Parameters:
schema the schema
path the pointer/id reference into the schema
Returns:
a JsonSchema instance
See also:
com.github.fge.jsonschema.ref.JsonFragment
    public JsonSchema fromSchema(final JsonNode schemafinal String path)
    {
        final SchemaContext schemaContext = .register(schema);
        final JsonNode subSchema = JsonFragment.fromFragment(path)
            .resolve(schemaContext.getSchema());
        return createSchema(schemaContextsubSchema);
    }

    
Create a schema instance from a JSON Schema

This calls fromSchema(com.fasterxml.jackson.databind.JsonNode,java.lang.String) with "" as a path.

Parameters:
schema the schema
Returns:
a JsonSchema instance
    public JsonSchema fromSchema(final JsonNode schema)
    {
        return fromSchema(schema"");
    }

    
Create a schema instance from a JSON Schema located at a given URI, and at a given path

This allows you, for instance, to load a schema using HTTP. Or, in fact, any other URI scheme that is supported.

Parameters:
uri the URI
path the JSON Pointer/id reference into the downloaded schema
Returns:
a JsonSchema instance
Throws:
JsonSchemaException unable to get content from that URI
See also:
com.github.fge.jsonschema.uri.URIManager
com.github.fge.jsonschema.schema.SchemaRegistry
    public JsonSchema fromURI(final URI urifinal String path)
        throws JsonSchemaException
    {
        final SchemaContext schemaContext = .get(uri);
        final JsonNode subSchema = JsonFragment.fromFragment(path)
            .resolve(schemaContext.getSchema());
        return createSchema(schemaContextsubSchema);
    }

    
Create a schema instance from a JSON Schema located at a given URI

Parameters:
uri the URI
Returns:
a JsonSchema instance
Throws:
JsonSchemaException unable to get content from that URI
See also:
fromSchema(com.fasterxml.jackson.databind.JsonNode,java.lang.String)
    public JsonSchema fromURI(final URI uri)
        throws JsonSchemaException
    {
        return fromURI(uri"");
    }

    
Create a schema instance from a JSON Schema located at a given URI

Parameters:
str the URI as a string
Returns:
a JsonSchema instance
Throws:
JsonSchemaException unable to get content from that URI
java.lang.IllegalArgumentException URI is invalid
See also:
java.net.URI.create(java.lang.String)
fromURI(java.net.URI,java.lang.String)
    public JsonSchema fromURI(final String str)
        throws JsonSchemaException
    {
        return fromURI(URI.create(str), "");
    }

    
Create a schema instance from a JSON Schema located at a given URI and at a given path

Parameters:
str the URI as a string
path the JSON Pointer/id reference into the downloaded schema
Returns:
a JsonSchema instance
Throws:
JsonSchemaException unable to get content from that URI
java.lang.IllegalArgumentException URI is invalid
See also:
java.net.URI.create(java.lang.String)
fromURI(java.net.URI,java.lang.String)
    public JsonSchema fromURI(final String strfinal String path)
        throws JsonSchemaException
    {
        return fromURI(URI.create(str), path);
    }

    
Create a JsonSchema instance

Parameters:
schemaContext the schema container
schema the subschema
Returns:
a JsonSchema instance
    private JsonSchema createSchema(final SchemaContext schemaContext,
        final JsonNode schema)
    {
        final SchemaNode schemaNode = new SchemaNode(schemaContextschema);
        final JsonValidatorCache cache
            = getValidatorCache(schemaContext.getSchema());
        return new JsonSchema(cacheschemaNode);
    }
    private JsonValidatorCache getValidatorCache(final JsonNode schema)
    {
        final JsonNode node = schema.path("$schema");
        if (!node.isTextual())
            return .get();
        JsonRef ref;
        try {
            ref = JsonRef.fromString(node.textValue());
            if (!.containsKey(ref))
                ref = ;
        } catch (JsonSchemaException ignored) {
            ref = ;
        }
        return .get(ref);
    }

    
Builder class for a JsonSchemaFactory
    public static final class Builder
    {
        
Addressing mode
        private AddressingMode addressingMode = .;

        
Default schema URI
        private JsonRef defaultSchemaURI = SchemaURIs.draftV3HyperSchema();

        
Keyword registries
        private final Map<JsonRefMetaSchemametaSchemas;

        
The URI manager
        private final URIManager uriManager = new URIManager();

        
The namespace
        private URI namespace = URI.create("");

        
Schema bundle
        private final SchemaBundle bundle = new SchemaBundle();

        
Constructor
        public Builder()
        {
            // Build keyword registries
             = Maps.newHashMap();
            JsonRef ref;
            MetaSchema metaSchema;
            for (final BuiltinSchemas builtin: BuiltinSchemas.values()) {
                ref = JsonRef.fromURI(builtin.getURI());
                metaSchema = MetaSchema.copyOf(builtin);
                .put(refmetaSchema);
            }
        }

        
Register a com.github.fge.jsonschema.uri.URIDownloader for a given scheme

Parameters:
scheme the URI scheme
downloader the downloader
Returns:
the builder
Throws:
java.lang.NullPointerException scheme is null
java.lang.IllegalArgumentException illegal scheme
        public Builder registerScheme(final String scheme,
            final URIDownloader downloader)
        {
            .registerScheme(schemedownloader);
            return this;
        }

        
Unregister a scheme

Parameters:
scheme the scheme to desupport
Returns:
the builder
        public Builder unregisterScheme(final String scheme)
        {
            .unregisterScheme(scheme);
            return this;
        }

        
Sets the addressing mode for this factory

Parameters:
addressingMode the addressing mode
Returns:
the builder
        public Builder addressingMode(final AddressingMode addressingMode)
        {
            this. = addressingMode;
            return this;
        }

        
Set the schema registry's namespace

Parameters:
namespace the namespace, as a string
Returns:
the builder
Throws:
java.lang.IllegalArgumentException invalid URI (see java.net.URI.create(java.lang.String))
See also:
com.github.fge.jsonschema.schema.SchemaRegistry
        public Builder setNamespace(final String namespace)
        {
            this. = URI.create(namespace);
            return this;
        }

        
Add an URI redirection

This allows you to add an alias for a schema location so that it point to another of your choice. It may be useful if you have to resolve absolute JSON References normally unreachable, but you have a copy of this schema locally.

Note that both URIs must be absolute.

Parameters:
from the source URI, as a string
to the target URI, as a string
Returns:
the builder
Throws:
java.lang.IllegalArgumentException either from or to is an invalid URI, or it is not an absolute JSON Reference
See also:
com.github.fge.jsonschema.ref.JsonRef
        public Builder addRedirection(final String fromfinal String to)
        {
            .addRedirection(fromto);
            return this;
        }

        
Add a keyword registry to the factory

Deprecated:
Parameters:
schemaURI the URI for the new registry
keywordRegistry the keyword registry
byDefault whether this registry will be the default
Returns:
the builder
Throws:
java.lang.NullPointerException the URI or registry are null
        @Deprecated
        public Builder addKeywordRegistry(final JsonRef schemaURI,
            final KeywordRegistry keywordRegistryfinal boolean byDefault)
        {
            Preconditions.checkNotNull(schemaURI"schema URI cannot be null");
            Preconditions.checkNotNull(keywordRegistry,
                "keyword registry cannot be null");
            final MetaSchema metaSchema = MetaSchema.builder()
                .withURI(schemaURI.toString())
                .addKeywordRegistry(keywordRegistry).build();
            .put(schemaURImetaSchema);
            if (byDefault)
                 = schemaURI;
            return this;
        }
        public Builder addMetaSchema(final MetaSchema metaSchema,
            final boolean byDefault)
        {
            final JsonRef dollarSchema = metaSchema.getDollarSchema();
            .put(dollarSchemametaSchema);
            if (byDefault)
                 = dollarSchema;
            return this;
        }

        
Register a schema

Parameters:
uri the URI of this schema
schema the schema
Returns:
the builder
See also:
com.github.fge.jsonschema.schema.SchemaBundle.addSchema(java.net.URI,com.fasterxml.jackson.databind.JsonNode)
        public Builder addSchema(final URI urifinal JsonNode schema)
        {
            .addSchema(urischema);
            return this;
        }

        
Register a schema

Parameters:
uri the URI of this schema
schema the schema
Returns:
the builder
See also:
com.github.fge.jsonschema.schema.SchemaBundle.addSchema(java.lang.String,com.fasterxml.jackson.databind.JsonNode)
        public Builder addSchema(final String urifinal JsonNode schema)
        {
            .addSchema(urischema);
            return this;
        }

        
Build the factory

Returns:
the factory
        public JsonSchemaFactory build()
        {
            return new JsonSchemaFactory(this);
        }
    }
New to GrepCode? Check out our FAQ X