View Javadoc
1   /*
2    * Portions of this software was developed by employees of the National Institute
3    * of Standards and Technology (NIST), an agency of the Federal Government and is
4    * being made available as a public service. Pursuant to title 17 United States
5    * Code Section 105, works of NIST employees are not subject to copyright
6    * protection in the United States. This software may be subject to foreign
7    * copyright. Permission in the United States and in foreign countries, to the
8    * extent that NIST may hold copyright, to use, copy, modify, create derivative
9    * works, and distribute this software and its documentation without fee is hereby
10   * granted on a non-exclusive basis, provided that this notice and disclaimer
11   * of warranty appears in all copies.
12   *
13   * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
14   * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
15   * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
17   * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
18   * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
19   * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
20   * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
21   * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
22   * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
23   * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
24   * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
25   */
26  
27  package gov.nist.secauto.metaschema.schemagen;
28  
29  import gov.nist.secauto.metaschema.core.configuration.IConfiguration;
30  import gov.nist.secauto.metaschema.core.model.IModule;
31  import gov.nist.secauto.metaschema.schemagen.json.JsonSchemaGenerator;
32  import gov.nist.secauto.metaschema.schemagen.xml.XmlSchemaGenerator;
33  
34  import java.io.IOException;
35  import java.io.OutputStream;
36  import java.io.OutputStreamWriter;
37  import java.io.Writer;
38  import java.nio.charset.StandardCharsets;
39  import java.nio.file.Files;
40  import java.nio.file.Path;
41  import java.nio.file.StandardOpenOption;
42  
43  import edu.umd.cs.findbugs.annotations.NonNull;
44  
45  public interface ISchemaGenerator {
46    /**
47     * Generate and write a schema for the provided {@code metaschema} to the
48     * {@link Writer} provided by {@code writer} using the provided
49     * {@code configuration}.
50     *
51     * @param metaschema
52     *          the Module to generate the schema for
53     * @param writer
54     *          the writer to use to write the schema
55     * @param configuration
56     *          the schema generation configuration
57     * @throws SchemaGenerationException
58     *           if an error occurred while writing the schema
59     */
60    void generateFromModule(
61        @NonNull IModule metaschema,
62        @NonNull Writer writer,
63        @NonNull IConfiguration<SchemaGenerationFeature<?>> configuration);
64  
65    static void generateSchema(
66        @NonNull IModule module,
67        @NonNull Path destination,
68        @NonNull SchemaFormat asFormat,
69        @NonNull IConfiguration<SchemaGenerationFeature<?>> configuration)
70        throws IOException {
71      ISchemaGenerator schemaGenerator = asFormat.getSchemaGenerator();
72  
73      try (Writer writer = Files.newBufferedWriter(
74          destination,
75          StandardCharsets.UTF_8,
76          StandardOpenOption.CREATE,
77          StandardOpenOption.WRITE,
78          StandardOpenOption.TRUNCATE_EXISTING)) {
79        assert writer != null;
80        schemaGenerator.generateFromModule(module, writer, configuration);
81        writer.flush();
82      }
83    }
84  
85    static void generateSchema(
86        @NonNull IModule module,
87        @NonNull OutputStream os,
88        @NonNull SchemaFormat asFormat,
89        @NonNull IConfiguration<SchemaGenerationFeature<?>> configuration)
90        throws IOException {
91      ISchemaGenerator schemaGenerator = asFormat.getSchemaGenerator();
92  
93      Writer writer = new OutputStreamWriter(os, StandardCharsets.UTF_8);
94      schemaGenerator.generateFromModule(module, writer, configuration);
95      writer.flush();
96      // we don't want to close os, since we do not own it
97    }
98  
99    /**
100    * Identifies the supported schema generation formats.
101    */
102   enum SchemaFormat {
103     /**
104      * a JSON Schema.
105      */
106     JSON(new JsonSchemaGenerator()),
107     /**
108      * an XML Schema.
109      */
110     XML(new XmlSchemaGenerator());
111 
112     private final ISchemaGenerator schemaGenerator;
113 
114     SchemaFormat(@NonNull ISchemaGenerator schemaGenerator) {
115       this.schemaGenerator = schemaGenerator;
116     }
117 
118     public ISchemaGenerator getSchemaGenerator() {
119       return schemaGenerator;
120     }
121   }
122 }