1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.schemagen.json;
7   
8   import com.fasterxml.jackson.core.JsonFactory;
9   import com.fasterxml.jackson.core.JsonGenerator;
10  import com.fasterxml.jackson.core.JsonGenerator.Feature;
11  import com.fasterxml.jackson.databind.ObjectMapper;
12  import com.fasterxml.jackson.databind.node.ObjectNode;
13  
14  import gov.nist.secauto.metaschema.core.configuration.IConfiguration;
15  import gov.nist.secauto.metaschema.core.model.IModule;
16  import gov.nist.secauto.metaschema.core.util.ObjectUtils;
17  import gov.nist.secauto.metaschema.schemagen.AbstractSchemaGenerator;
18  import gov.nist.secauto.metaschema.schemagen.SchemaGenerationException;
19  import gov.nist.secauto.metaschema.schemagen.SchemaGenerationFeature;
20  import gov.nist.secauto.metaschema.schemagen.json.impl.IJsonSchema;
21  import gov.nist.secauto.metaschema.schemagen.json.impl.JsonDatatypeManager;
22  import gov.nist.secauto.metaschema.schemagen.json.impl.JsonGenerationState;
23  import gov.nist.secauto.metaschema.schemagen.json.impl.JsonSchemaModule;
24  
25  import java.io.IOException;
26  import java.io.Writer;
27  
28  import edu.umd.cs.findbugs.annotations.NonNull;
29  
30  public class JsonSchemaGenerator
31      extends AbstractSchemaGenerator<JsonGenerator, JsonDatatypeManager, JsonGenerationState> {
32    @NonNull
33    private final JsonFactory jsonFactory;
34  
35    public JsonSchemaGenerator() {
36      this(new JsonFactory());
37    }
38  
39    public JsonSchemaGenerator(@NonNull JsonFactory jsonFactory) {
40      this.jsonFactory = jsonFactory;
41    }
42  
43    @NonNull
44    public JsonFactory getJsonFactory() {
45      return jsonFactory;
46    }
47  
48    @SuppressWarnings("resource")
49    @Override
50    protected JsonGenerator newWriter(Writer out) {
51      try {
52        return ObjectUtils.notNull(getJsonFactory().createGenerator(out)
53            .setCodec(new ObjectMapper())
54            .useDefaultPrettyPrinter()
55            .disable(Feature.AUTO_CLOSE_TARGET));
56      } catch (IOException ex) {
57        throw new SchemaGenerationException(ex);
58      }
59    }
60  
61    @Override
62    protected JsonGenerationState newGenerationState(
63        IModule module,
64        JsonGenerator schemaWriter,
65        IConfiguration<SchemaGenerationFeature<?>> configuration) {
66      return new JsonGenerationState(module, schemaWriter, configuration);
67    }
68  
69    @Override
70    protected void generateSchema(JsonGenerationState state) {
71      IModule module = state.getModule();
72  
73      IJsonSchema moduleSchema = new JsonSchemaModule(module, state);
74      ObjectNode schemaNode = ObjectUtils.notNull(state.getJsonNodeFactory().objectNode());
75      moduleSchema.generateInlineJsonSchema(schemaNode, state);
76  
77      try {
78        state.writeObject(schemaNode);
79      } catch (IOException ex) {
80        throw new SchemaGenerationException(ex);
81      }
82    }
83  }