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