
com.araguacaima.commons.utils.JsonSchemaUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-utils Show documentation
Show all versions of commons-utils Show documentation
Common utilities is a set of java utilities for managing typical actions when working with enums,
files, exceptions, zip/jar files, classes (via Reflection), maps, numbers and so on.
Most of the utilities extend functionalities offered by amazing libraries such as:
* commons-beanutils (apache)
* commons-lang3 (apache)
* commons-io (apache)
* commons-math3 (apache)
* commons-collections4 (apache)
* jreversepro (akkumar)
package com.araguacaima.commons.utils;
import com.araguacaima.commons.utils.jsonschema.RuleFactory;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
import org.jsonschema2pojo.*;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.*;
@SuppressWarnings("UnusedReturnValue")
public class JsonSchemaUtils {
public static final String DEFINITIONS_ROOT = "definitions";
private static ReflectionUtils reflectionUtils = ReflectionUtils.getInstance();
private final JsonUtils jsonUtils = new JsonUtils();
private final MapUtils mapUtils = MapUtils.getInstance();
private CompilerUtils.FilesCompiler filesCompiler;
private static final NoopAnnotator noopAnnotator = new NoopAnnotator();
private static final SchemaStore schemaStore = new SchemaStore();
private static final org.jsonschema2pojo.SchemaGenerator schemaGenerator = new org.jsonschema2pojo.SchemaGenerator();
private static final GenerationConfig config = new DefaultGenerationConfig() {
@Override
public boolean isUsePrimitives() {
return true;
}
@Override
public boolean isUseLongIntegers() {
return true;
}
@Override
public AnnotationStyle getAnnotationStyle() {
return AnnotationStyle.NONE;
}
@Override
public InclusionLevel getInclusionLevel() {
return InclusionLevel.ALWAYS;
}
@Override
public boolean isUseOptionalForGetters() {
return false;
}
@Override
public boolean isRemoveOldOutput() {
return true;
}
@Override
public boolean isSerializable() {
return true;
}
@Override
public boolean isIncludeConstructors() {
return true;
}
@Override
public boolean isIncludeAdditionalProperties() {
return false;
}
@Override
public String getTargetVersion() {
return "1.8";
}
@Override
public Language getTargetLanguage() {
return Language.JAVA;
}
};
public JsonSchemaUtils(T classLoader) {
if (classLoader != null) {
filesCompiler = new CompilerUtils.FilesCompiler<>(classLoader);
}
}
public T processFile_(File file, String packageName, File sourceCodeDirectory, File compiledClassesDirectory) throws IOException, NoSuchFieldException, IllegalAccessException, URISyntaxException, InstantiationException {
processFile(file, packageName, sourceCodeDirectory, compiledClassesDirectory);
return filesCompiler.getClassLoader();
}
public T processFile_(String json, String packageName, File sourceCodeDirectory, File compiledClassesDirectory) throws IOException, NoSuchFieldException, IllegalAccessException, URISyntaxException, InstantiationException {
processFile(json, packageName, sourceCodeDirectory, compiledClassesDirectory);
return filesCompiler.getClassLoader();
}
public Set> processFile(File file, String packageName, File sourceCodeDirectory, File compiledClassesDirectory) throws IOException, NoSuchFieldException, IllegalAccessException, URISyntaxException, InstantiationException {
String json = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
return processFile(json, packageName, sourceCodeDirectory, compiledClassesDirectory);
}
@SuppressWarnings("unchecked")
public Set> processFile(String json, String packageName, File sourceCodeDirectory, File compiledClassesDirectory) throws IOException, NoSuchFieldException, IllegalAccessException, URISyntaxException, InstantiationException {
try {
FileUtils.cleanDirectory(sourceCodeDirectory);
Map jsonSchema = jsonUtils.fromJSON(json, Map.class);
String id = String.valueOf(jsonSchema.get("$id"));
String className_;
String packageName_;
if (id.contains(".")) {
className_ = id.substring(id.lastIndexOf('.') + 1);
packageName_ = id.substring(0, id.lastIndexOf('.'));
} else {
className_ = id;
packageName_ = packageName;
}
LinkedHashMap definitionMap = new LinkedHashMap<>();
Set ids = new LinkedHashSet<>();
buildDefinitions(packageName, ids, definitionMap, (Map) jsonSchema.get(DEFINITIONS_ROOT));
RuleFactory ruleFactory = new RuleFactory(config, noopAnnotator, schemaStore, DEFINITIONS_ROOT, definitionMap);
jsonUtils.jsonToSourceClassFile(json, className_, packageName_, sourceCodeDirectory, ruleFactory, schemaGenerator);
} catch (MismatchedInputException ignored) {
Collection
© 2015 - 2025 Weber Informatics LLC | Privacy Policy