1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.schemagen;
7   
8   import gov.nist.secauto.metaschema.core.configuration.IConfiguration;
9   import gov.nist.secauto.metaschema.core.model.IModule;
10  import gov.nist.secauto.metaschema.schemagen.json.JsonSchemaGenerator;
11  import gov.nist.secauto.metaschema.schemagen.xml.XmlSchemaGenerator;
12  
13  import java.io.IOException;
14  import java.io.Writer;
15  import java.nio.charset.StandardCharsets;
16  import java.nio.file.Files;
17  import java.nio.file.Path;
18  import java.nio.file.StandardOpenOption;
19  
20  import edu.umd.cs.findbugs.annotations.NonNull;
21  
22  public interface ISchemaGenerator {
23    /**
24     * Generate and write a schema for the provided {@code metaschema} to the
25     * {@link Writer} provided by {@code writer} using the provided
26     * {@code configuration}.
27     *
28     * @param metaschema
29     *          the Module to generate the schema for
30     * @param writer
31     *          the writer to use to write the schema
32     * @param configuration
33     *          the schema generation configuration
34     * @throws SchemaGenerationException
35     *           if an error occurred while writing the schema
36     */
37    void generateFromModule(
38        @NonNull IModule metaschema,
39        @NonNull Writer writer,
40        @NonNull IConfiguration<SchemaGenerationFeature<?>> configuration);
41  
42    static void generateSchema(
43        @NonNull IModule module,
44        @NonNull Path destination,
45        @NonNull SchemaFormat asFormat,
46        @NonNull IConfiguration<SchemaGenerationFeature<?>> configuration)
47        throws IOException {
48      ISchemaGenerator schemaGenerator = asFormat.getSchemaGenerator();
49  
50      try (Writer writer = Files.newBufferedWriter(
51          destination,
52          StandardCharsets.UTF_8,
53          StandardOpenOption.CREATE,
54          StandardOpenOption.WRITE,
55          StandardOpenOption.TRUNCATE_EXISTING)) {
56        assert writer != null;
57        schemaGenerator.generateFromModule(module, writer, configuration);
58        writer.flush();
59      }
60    }
61  
62    static void generateSchema(
63        @NonNull IModule module,
64        @NonNull Writer writer,
65        @NonNull SchemaFormat asFormat,
66        @NonNull IConfiguration<SchemaGenerationFeature<?>> configuration)
67        throws IOException {
68      ISchemaGenerator schemaGenerator = asFormat.getSchemaGenerator();
69  
70      schemaGenerator.generateFromModule(module, writer, configuration);
71      writer.flush();
72      // we don't want to close os, since we do not own it
73    }
74  
75    /**
76     * Identifies the supported schema generation formats.
77     */
78    enum SchemaFormat {
79      /**
80       * a JSON Schema.
81       */
82      JSON(new JsonSchemaGenerator()),
83      /**
84       * an XML Schema.
85       */
86      XML(new XmlSchemaGenerator());
87  
88      @NonNull
89      private final ISchemaGenerator schemaGenerator;
90  
91      SchemaFormat(@NonNull ISchemaGenerator schemaGenerator) {
92        this.schemaGenerator = schemaGenerator;
93      }
94  
95      @NonNull
96      public ISchemaGenerator getSchemaGenerator() {
97        return schemaGenerator;
98      }
99    }
100 }