001/* 002 * SPDX-FileCopyrightText: none 003 * SPDX-License-Identifier: CC0-1.0 004 */ 005 006package gov.nist.secauto.metaschema.schemagen; 007 008import gov.nist.secauto.metaschema.core.configuration.IConfiguration; 009import gov.nist.secauto.metaschema.core.model.IModule; 010import gov.nist.secauto.metaschema.schemagen.json.JsonSchemaGenerator; 011import gov.nist.secauto.metaschema.schemagen.xml.XmlSchemaGenerator; 012 013import java.io.IOException; 014import java.io.Writer; 015import java.nio.charset.StandardCharsets; 016import java.nio.file.Files; 017import java.nio.file.Path; 018import java.nio.file.StandardOpenOption; 019 020import edu.umd.cs.findbugs.annotations.NonNull; 021 022public interface ISchemaGenerator { 023 /** 024 * Generate and write a schema for the provided {@code metaschema} to the 025 * {@link Writer} provided by {@code writer} using the provided 026 * {@code configuration}. 027 * 028 * @param metaschema 029 * the Module to generate the schema for 030 * @param writer 031 * the writer to use to write the schema 032 * @param configuration 033 * the schema generation configuration 034 * @throws SchemaGenerationException 035 * if an error occurred while writing the schema 036 */ 037 void generateFromModule( 038 @NonNull IModule metaschema, 039 @NonNull Writer writer, 040 @NonNull IConfiguration<SchemaGenerationFeature<?>> configuration); 041 042 static void generateSchema( 043 @NonNull IModule module, 044 @NonNull Path destination, 045 @NonNull SchemaFormat asFormat, 046 @NonNull IConfiguration<SchemaGenerationFeature<?>> configuration) 047 throws IOException { 048 ISchemaGenerator schemaGenerator = asFormat.getSchemaGenerator(); 049 050 try (Writer writer = Files.newBufferedWriter( 051 destination, 052 StandardCharsets.UTF_8, 053 StandardOpenOption.CREATE, 054 StandardOpenOption.WRITE, 055 StandardOpenOption.TRUNCATE_EXISTING)) { 056 assert writer != null; 057 schemaGenerator.generateFromModule(module, writer, configuration); 058 writer.flush(); 059 } 060 } 061 062 static void generateSchema( 063 @NonNull IModule module, 064 @NonNull Writer writer, 065 @NonNull SchemaFormat asFormat, 066 @NonNull IConfiguration<SchemaGenerationFeature<?>> configuration) 067 throws IOException { 068 ISchemaGenerator schemaGenerator = asFormat.getSchemaGenerator(); 069 070 schemaGenerator.generateFromModule(module, writer, configuration); 071 writer.flush(); 072 // we don't want to close os, since we do not own it 073 } 074 075 /** 076 * Identifies the supported schema generation formats. 077 */ 078 enum SchemaFormat { 079 /** 080 * a JSON Schema. 081 */ 082 JSON(new JsonSchemaGenerator()), 083 /** 084 * an XML Schema. 085 */ 086 XML(new XmlSchemaGenerator()); 087 088 @NonNull 089 private final ISchemaGenerator schemaGenerator; 090 091 SchemaFormat(@NonNull ISchemaGenerator schemaGenerator) { 092 this.schemaGenerator = schemaGenerator; 093 } 094 095 @NonNull 096 public ISchemaGenerator getSchemaGenerator() { 097 return schemaGenerator; 098 } 099 } 100}