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  @FunctionalInterface
23  public interface ISchemaGenerator {
24    /**
25     * Generate and write a schema for the provided {@code metaschema} to the
26     * {@link Writer} provided by {@code writer} using the provided
27     * {@code configuration}.
28     *
29     * @param metaschema
30     *          the Module to generate the schema for
31     * @param writer
32     *          the writer to use to write the schema
33     * @param configuration
34     *          the schema generation configuration
35     * @throws SchemaGenerationException
36     *           if an error occurred while writing the schema
37     */
38    void generateFromModule(
39        @NonNull IModule metaschema,
40        @NonNull Writer writer,
41        @NonNull IConfiguration<SchemaGenerationFeature<?>> configuration);
42  
43    static void generateSchema(
44        @NonNull IModule module,
45        @NonNull Path destination,
46        @NonNull SchemaFormat asFormat,
47        @NonNull IConfiguration<SchemaGenerationFeature<?>> configuration)
48        throws IOException {
49      ISchemaGenerator schemaGenerator = asFormat.getSchemaGenerator();
50  
51      try (Writer writer = Files.newBufferedWriter(
52          destination,
53          StandardCharsets.UTF_8,
54          StandardOpenOption.CREATE,
55          StandardOpenOption.WRITE,
56          StandardOpenOption.TRUNCATE_EXISTING)) {
57        assert writer != null;
58        schemaGenerator.generateFromModule(module, writer, configuration);
59        writer.flush();
60      }
61    }
62  
63    static void generateSchema(
64        @NonNull IModule module,
65        @NonNull Writer writer,
66        @NonNull SchemaFormat asFormat,
67        @NonNull IConfiguration<SchemaGenerationFeature<?>> configuration)
68        throws IOException {
69      ISchemaGenerator schemaGenerator = asFormat.getSchemaGenerator();
70  
71      schemaGenerator.generateFromModule(module, writer, configuration);
72      writer.flush();
73      // we don't want to close os, since we do not own it
74    }
75  
76    /**
77     * Identifies the supported schema generation formats.
78     */
79    enum SchemaFormat {
80      /**
81       * a JSON Schema.
82       */
83      JSON(new JsonSchemaGenerator()),
84      /**
85       * an XML Schema.
86       */
87      XML(new XmlSchemaGenerator());
88  
89      @NonNull
90      private final ISchemaGenerator schemaGenerator;
91  
92      SchemaFormat(@NonNull ISchemaGenerator schemaGenerator) {
93        this.schemaGenerator = schemaGenerator;
94      }
95  
96      @NonNull
97      public ISchemaGenerator getSchemaGenerator() {
98        return schemaGenerator;
99      }
100   }
101 }