73 lines
2.6 KiB
Java
73 lines
2.6 KiB
Java
package eu.dnetlib.dhp.schema.dump;
|
|
|
|
import java.io.*;
|
|
import java.lang.reflect.Type;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Paths;
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
|
import com.github.imifou.jsonschema.module.addon.AddonModule;
|
|
import com.github.victools.jsonschema.generator.*;
|
|
|
|
import eu.dnetlib.dhp.schema.dump.oaf.community.CommunityResult;
|
|
import eu.dnetlib.dhp.schema.dump.oaf.graph.*;
|
|
|
|
public class ExecCreateSchemas {
|
|
|
|
SchemaGenerator generator;
|
|
|
|
private void init(){
|
|
|
|
AddonModule module = new AddonModule();
|
|
SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(
|
|
new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT),
|
|
SchemaVersion.DRAFT_7,
|
|
OptionPreset.PLAIN_JSON)
|
|
.with(module)
|
|
.with(Option.SCHEMA_VERSION_INDICATOR)
|
|
.without(Option.NONPUBLIC_NONSTATIC_FIELDS_WITHOUT_GETTERS);
|
|
SchemaGeneratorConfig config = configBuilder.build();
|
|
generator = new SchemaGenerator(config);
|
|
}
|
|
|
|
private void generate(Type targetType, String directory, String filename) throws IOException {
|
|
JsonNode jsonSchema = generator.generateSchema(targetType);
|
|
|
|
if(!Files.exists(Paths.get(directory))){
|
|
Files.createDirectories(Paths.get(directory));
|
|
}
|
|
|
|
if(!Files.exists(Paths.get(directory + filename))) {
|
|
Files.createFile(Paths.get(directory + filename));
|
|
}
|
|
|
|
File f = new File(directory + filename);
|
|
|
|
try(PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(f)))) {
|
|
writer.println(new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema));
|
|
}
|
|
|
|
|
|
}
|
|
public static void main(String[] args) throws IOException {
|
|
|
|
ExecCreateSchemas ecs = new ExecCreateSchemas();
|
|
ecs.init();
|
|
ecs.generate(GraphResult.class, "/tmp/jsonschemas/" , "result_schema.json");
|
|
ecs.generate(ResearchCommunity.class, "/tmp/jsonschemas/" , "community_infrastructure_schema.json");
|
|
ecs.generate(Datasource.class, "/tmp/jsonschemas/" , "datasource_schema.json");
|
|
ecs.generate(Project.class, "/tmp/jsonschemas/" , "project_schema.json");
|
|
ecs.generate(Relation.class, "/tmp/jsonschemas/" , "relation_schema.json");
|
|
ecs.generate(Organization.class, "/tmp/jsonschemas/" , "organization_schema.json");
|
|
|
|
ecs.generate(CommunityResult.class, "/tmp/jsonschemas/" , "community_result_schema.json");
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|