1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.schemagen.json.impl;
7   
8   import com.fasterxml.jackson.databind.node.ObjectNode;
9   
10  import gov.nist.secauto.metaschema.core.model.IInstance;
11  import gov.nist.secauto.metaschema.core.util.ObjectUtils;
12  import gov.nist.secauto.metaschema.schemagen.json.IJsonGenerationState;
13  
14  import edu.umd.cs.findbugs.annotations.NonNull;
15  
16  public abstract class AbstractJsonProperty<I extends IInstance>
17      implements IJsonProperty<I> {
18    @NonNull
19    private final I instance;
20  
21    protected AbstractJsonProperty(@NonNull I instance) {
22      this.instance = instance;
23    }
24  
25    @Override
26    @NonNull
27    public I getInstance() {
28      return instance;
29    }
30  
31    protected void generateMetadata(@NonNull ObjectNode obj) {
32      // do nothing by default
33    }
34  
35    // REFACTOR: rename to generate schema
36    protected abstract void generateBody(
37        @NonNull ObjectNode obj,
38        @NonNull IJsonGenerationState state);
39  
40    @Override
41    public void generateProperty(
42        PropertyCollection properties,
43        IJsonGenerationState state) {
44  
45      ObjectNode contextObj = ObjectUtils.notNull(state.getJsonNodeFactory().objectNode());
46  
47      generateMetadata(contextObj);
48  
49      generateBody(contextObj, state);
50  
51      String name = getName();
52      properties.addProperty(name, contextObj);
53      if (isRequired()) {
54        properties.addRequired(name);
55      }
56    }
57  }