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.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
27
28
29
30
31
32
33
34
35
36
37
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
76 }
77
78
79
80
81 enum SchemaFormat {
82
83
84
85 JSON(new JsonSchemaGenerator()),
86
87
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 }