1
2
3
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
25
26
27
28
29
30
31
32
33
34
35
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
73 }
74
75
76
77
78 enum SchemaFormat {
79
80
81
82 JSON(new JsonSchemaGenerator()),
83
84
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 }