com.github.fge.jsonschema.processors.digest.SchemaDigester Maven / Gradle / Ivy
/*
* Copyright (c) 2013, Francis Galiegue
*
* 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 .
*/
package com.github.fge.jsonschema.processors.digest;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jsonschema.exceptions.ProcessingException;
import com.github.fge.jsonschema.library.Dictionary;
import com.github.fge.jsonschema.processing.Processor;
import com.github.fge.jsonschema.processors.data.ValidationContext;
import com.github.fge.jsonschema.processors.data.ValidationDigest;
import com.github.fge.jsonschema.report.ProcessingReport;
import com.github.fge.jsonschema.util.Digester;
import com.github.fge.jsonschema.util.NodeType;
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;
public final class SchemaDigester
implements Processor
{
private final ListMultimap typeMap
= ArrayListMultimap.create();
private final Map digesterMap
= Maps.newHashMap();
public SchemaDigester(final Dictionary dict)
{
String keyword;
Digester digester;
for (final Map.Entry entry: dict.entries()) {
keyword = entry.getKey();
digester = entry.getValue();
digesterMap.put(keyword, digester);
for (final NodeType type: digester.supportedTypes())
typeMap.put(type, keyword);
}
}
@Override
public ValidationDigest process(final ProcessingReport report,
final ValidationContext 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 ValidationDigest(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";
}
}