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

com.github.fge.jsonschema.processors.digest.SchemaDigester Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2014, Francis Galiegue ([email protected])
 *
 * This software is dual-licensed under:
 *
 * - the Lesser General Public License (LGPL) version 3.0 or, at your option, any
 *   later version;
 * - the Apache Software License (ASL) version 2.0.
 *
 * The text of this file and of both licenses is available at the root of this
 * project or, if you have the jar distribution, in directory META-INF/, under
 * the names LGPL-3.0.txt and ASL-2.0.txt respectively.
 *
 * Direct link to the sources:
 *
 * - LGPL 3.0: https://www.gnu.org/licenses/lgpl-3.0.txt
 * - ASL 2.0: http://www.apache.org/licenses/LICENSE-2.0.txt
 */

package com.github.fge.jsonschema.processors.digest;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.NodeType;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.core.processing.Processor;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.core.util.Dictionary;
import com.github.fge.jsonschema.keyword.digest.Digester;
import com.github.fge.jsonschema.library.Library;
import com.github.fge.jsonschema.processors.data.SchemaContext;
import com.github.fge.jsonschema.processors.data.SchemaDigest;
import com.github.fge.jsonschema.processors.validation.ValidationChain;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

import java.util.Map;

/**
 * The schema digester
 *
 * 

This processor is called by a {@link ValidationChain} after it has made * sure that the schema is syntactically valid.

*/ public final class SchemaDigester implements Processor { private final ListMultimap typeMap = ArrayListMultimap.create(); private final Map digesterMap = Maps.newHashMap(); public SchemaDigester(final Library library) { this(library.getDigesters()); } public SchemaDigester(final Dictionary dict) { String keyword; Digester digester; final Map map = dict.entries(); for (final Map.Entry entry: map.entrySet()) { keyword = entry.getKey(); digester = entry.getValue(); digesterMap.put(keyword, digester); for (final NodeType type: digester.supportedTypes()) typeMap.put(type, keyword); } } @Override public SchemaDigest process(final ProcessingReport report, final SchemaContext input) throws ProcessingException { final JsonNode schema = input.getSchema().getNode(); final NodeType type = input.getInstanceType(); final Map map = Maps.newHashMap(buildDigests(schema)); map.keySet().retainAll(typeMap.get(type)); return new SchemaDigest(input, map); } private Map buildDigests(final JsonNode schema) { final ImmutableMap.Builder builder = ImmutableMap.builder(); final Map map = Maps.newHashMap(digesterMap); map.keySet().retainAll(Sets.newHashSet(schema.fieldNames())); for (final Map.Entry entry: map.entrySet()) builder.put(entry.getKey(), entry.getValue().digest(schema)); return builder.build(); } @Override public String toString() { return "schema digester"; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy