private FullData buildData(final JsonNode schema, final JsonNode instance, final boolean deepCheck) { BUNDLE.checkNotNull(schema, "nullSchema"); BUNDLE.checkNotNull(instance, "nullInstance"); final SchemaTree schemaTree = loader.load(schema); final JsonTree tree = new SimpleJsonTree(instance); return new FullData(schemaTree, tree, deepCheck); } }
/** * Package private constructor to build a factory out of a builder * * @param builder the builder * @see JsonSchemaFactoryBuilder#freeze() */ JsonSchemaFactory(final JsonSchemaFactoryBuilder builder) { reportProvider = builder.reportProvider; loadingCfg = builder.loadingCfg; validationCfg = builder.validationCfg; loader = new SchemaLoader(loadingCfg); final Processor<SchemaContext, ValidatorList> processor = buildProcessor(); validator = new JsonValidator(loader, new ValidationProcessor(validationCfg, processor), reportProvider); syntaxValidator = new SyntaxValidator(validationCfg); }
private Processor<SchemaContext, ValidatorList> buildProcessor() { final RefResolver resolver = new RefResolver(loader); final Map<JsonRef, Library> libraries = validationCfg.getLibraries(); final Library defaultLibrary = validationCfg.getDefaultLibrary(); final ValidationChain defaultChain = new ValidationChain(resolver, defaultLibrary, validationCfg); final ProcessorMap<JsonRef, SchemaContext, ValidatorList> map = new ProcessorMap<JsonRef, SchemaContext, ValidatorList>(FUNCTION); map.setDefaultProcessor(defaultChain); JsonRef ref; ValidationChain chain; for (final Map.Entry<JsonRef, Library> entry: libraries.entrySet()) { ref = entry.getKey(); chain = new ValidationChain(resolver, entry.getValue(), validationCfg); map.addEntry(ref, chain); } final Processor<SchemaContext, ValidatorList> processor = map.getProcessor(); return new CachingProcessor<SchemaContext, ValidatorList>(processor, SchemaContextEquivalence.getInstance(), validationCfg.getCacheSize()); } }
/** * Create a new schema loader with a given loading configuration * * @param cfg the configuration * @see LoadingConfiguration * @see LoadingConfigurationBuilder */ public SchemaLoader(final LoadingConfiguration cfg) { translator = new URITranslator(cfg.getTranslatorConfiguration()); dereferencing = cfg.getDereferencing(); manager = new URIManager(cfg); preloadedSchemas = ImmutableMap.copyOf(cfg.getPreloadedSchemas()); CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder(); if (cfg.getCacheSize() != -1) { builder.maximumSize(cfg.getCacheSize()); } cache = builder.build(new CacheLoader<URI, JsonNode>() { @Nonnull @Override public JsonNode load(@Nonnull final URI key) throws ProcessingException { return manager.getContent(key); } }); }
/** * Create a new schema tree with a given loading URI and JSON Schema * * @param ref the location * @param node the schema * @return a new tree */ public SchemaTree newTree(final JsonRef ref, final JsonNode node) { return newTree(SchemaKey.forJsonRef(ref), node); }
@Test public void NonAbsoluteURIsAreRefused() { final SchemaLoader loader = new SchemaLoader(); final URI target = URI.create("moo#"); try { loader.get(target); fail("No exception thrown!"); } catch (ProcessingException e) { assertMessage(e.getProcessingMessage()) .hasMessage(BUNDLE.printf("refProcessing.uriNotAbsolute", target)) .hasLevel(LogLevel.FATAL).hasField("uri", target); } }
@Test public void unhandledSchemeShouldBeReportedAsSuch() { final URI uri = URI.create("bar://baz"); final URIManager manager = new URIManager(); try { manager.getContent(uri); } catch (ProcessingException e) { assertMessage(e.getProcessingMessage()) .hasMessage(BUNDLE.printf("refProcessing.unhandledScheme", "bar", uri)) .hasField("scheme", "bar").hasField("uri", uri) .hasLevel(LogLevel.FATAL); } }
@Nonnull @Override public JsonNode load(@Nonnull final URI key) throws ProcessingException { return manager.getContent(key); } });
/** * Build a {@link JsonSchema} instance * * @param uri the URI to load the schema from * @return a {@link JsonSchema} * @throws ProcessingException invalid URI, or URI did not resolve to a * JSON Schema * @throws NullPointerException URI is null */ JsonSchema buildJsonSchema(final String uri) throws ProcessingException { final JsonRef ref = JsonRef.fromString(uri); if (!ref.isLegal()) throw new JsonReferenceException(new ProcessingMessage() .setMessage(BUNDLE.getMessage("illegalJsonRef"))); final SchemaTree tree = loader.get(ref.getLocator()).setPointer(ref.getPointer()); if (tree.getNode().isMissingNode()) throw new JsonReferenceException(new ProcessingMessage() .setMessage(BUNDLE.getMessage("danglingRef"))); return new JsonSchemaImpl(processor, tree, reportProvider); }
/** * Create a new schema tree with a given loading URI and JSON Schema * * @param ref the location * @param node the schema * @return a new tree */ public SchemaTree newTree(final JsonRef ref, final JsonNode node) { return newTree(SchemaKey.forJsonRef(ref), node); }
@Nonnull @Override public JsonNode load(@Nonnull final URI key) throws ProcessingException { return manager.getContent(key); } });
/** * Build a {@link JsonSchema} instance * * @param schema the schema * @param pointer the pointer into the schema * @return a new {@link JsonSchema} * @throws ProcessingException resolving the pointer against the schema * leads to a {@link MissingNode} * @throws NullPointerException the schema or pointer is null */ JsonSchema buildJsonSchema(final JsonNode schema, final JsonPointer pointer) throws ProcessingException { final SchemaTree tree = loader.load(schema).setPointer(pointer); if (tree.getNode().isMissingNode()) throw new JsonReferenceException(new ProcessingMessage() .setMessage(BUNDLE.getMessage("danglingRef"))); return new JsonSchemaImpl(processor, tree, reportProvider); }
/** * Create a new schema tree with a given loading URI and JSON Schema * * @param ref the location * @param node the schema * @return a new tree */ public SchemaTree newTree(final JsonRef ref, final JsonNode node) { return newTree(SchemaKey.forJsonRef(ref), node); }
@Nonnull @Override public JsonNode load(@Nonnull final URI key) throws ProcessingException { return manager.getContent(key); } });
protected abstract SchemaTree newTree(SchemaKey key, JsonNode node);
protected abstract SchemaTree newTree(SchemaKey key, JsonNode node);
/** * Create a new tree from a schema * * <p>Note that it will always create an "anonymous" tree, that is a tree * with an empty loading URI.</p> * * @param schema the schema * @return a new tree * @see Dereferencing#newTree(JsonNode) * @throws NullPointerException schema is null */ public SchemaTree load(final JsonNode schema) { BUNDLE.checkNotNull(schema, "loadingCfg.nullSchema"); return dereferencing.newTree(schema); }
protected abstract SchemaTree newTree(SchemaKey key, JsonNode node);
/** * Create a new tree from a schema * * <p>Note that it will always create an "anonymous" tree, that is a tree * with an empty loading URI.</p> * * @param schema the schema * @return a new tree * @see Dereferencing#newTree(JsonNode) * @throws NullPointerException schema is null */ public SchemaTree load(final JsonNode schema) { BUNDLE.checkNotNull(schema, "loadingCfg.nullSchema"); return dereferencing.newTree(schema); }
/** * Create a new tree from a schema * * <p>Note that it will always create an "anonymous" tree, that is a tree * with an empty loading URI.</p> * * @param schema the schema * @return a new tree * @see Dereferencing#newTree(JsonNode) * @throws NullPointerException schema is null */ public SchemaTree load(final JsonNode schema) { BUNDLE.checkNotNull(schema, "loadingCfg.nullSchema"); return dereferencing.newTree(schema); }