Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2016 Network New Technologies Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.networknt.schema;
import com.fasterxml.jackson.databind.JsonNode;
import com.networknt.schema.SpecVersion.VersionFlag;
import com.networknt.schema.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.function.Consumer;
/**
* Represents a meta-schema which is uniquely identified by its IRI.
*/
public class JsonMetaSchema {
private static final Logger logger = LoggerFactory.getLogger(JsonMetaSchema.class);
/**
* Factory for creating a format keyword.
*/
public interface FormatKeywordFactory {
/**
* Creates a format keyword.
*
* @param formats the formats
* @return the format keyword
*/
FormatKeyword newInstance(Map formats);
}
/**
* Builder for {@link JsonMetaSchema}.
*/
public static class Builder {
private String iri;
private String idKeyword = "$id";
private VersionFlag specification = null;
private final Map keywords = new HashMap<>();
private final Map formats = new HashMap<>();
private final Map vocabularies = new HashMap<>();
private FormatKeywordFactory formatKeywordFactory = null;
private VocabularyFactory vocabularyFactory = null;
private KeywordFactory unknownKeywordFactory = null;
public Builder(String iri) {
this.iri = iri;
}
private Map createKeywordsMap(Map kwords, Map formats) {
boolean formatKeywordPresent = false;
Map map = new HashMap<>();
for (Map.Entry type : kwords.entrySet()) {
String keywordName = type.getKey();
Keyword keyword = type.getValue();
if (ValidatorTypeCode.FORMAT.getValue().equals(keywordName)) {
if (!(keyword instanceof FormatKeyword) && !ValidatorTypeCode.FORMAT.equals(keyword)) {
throw new IllegalArgumentException("Overriding the keyword 'format' is not supported. Use the formatKeywordFactory and extend the FormatKeyword.");
}
// Indicate that the format keyword needs to be created
formatKeywordPresent = true;
} else {
map.put(keyword.getValue(), keyword);
}
}
if (formatKeywordPresent) {
final FormatKeyword formatKeyword = formatKeywordFactory != null ? formatKeywordFactory.newInstance(formats)
: new FormatKeyword(formats);
map.put(formatKeyword.getValue(), formatKeyword);
}
return map;
}
/**
* Sets the format keyword factory.
*
* @param formatKeywordFactory the format keyword factory
* @return the builder
*/
public Builder formatKeywordFactory(FormatKeywordFactory formatKeywordFactory) {
this.formatKeywordFactory = formatKeywordFactory;
return this;
}
/**
* Sets the vocabulary factory for handling custom vocabularies.
*
* @param vocabularyFactory the factory
* @return the builder
*/
public Builder vocabularyFactory(VocabularyFactory vocabularyFactory) {
this.vocabularyFactory = vocabularyFactory;
return this;
}
/**
* Sets the keyword factory for handling unknown keywords.
*
* @param unknownKeywordFactory the factory
* @return the builder
*/
public Builder unknownKeywordFactory(KeywordFactory unknownKeywordFactory) {
this.unknownKeywordFactory = unknownKeywordFactory;
return this;
}
/**
* Customize the formats.
*
* @param customizer the customizer
* @return the builder
*/
public Builder formats(Consumer