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.IDefinition;
11  import gov.nist.secauto.metaschema.schemagen.SchemaGenerationException;
12  import gov.nist.secauto.metaschema.schemagen.json.IDefinitionJsonSchema;
13  import gov.nist.secauto.metaschema.schemagen.json.IJsonGenerationState;
14  
15  import java.io.IOException;
16  import java.util.Map;
17  import java.util.Objects;
18  
19  import edu.umd.cs.findbugs.annotations.NonNull;
20  
21  public abstract class AbstractDefinitionJsonSchema<D extends IDefinition>
22      extends AbstractDefineableJsonSchema
23      implements IDefinitionJsonSchema<D> {
24    @NonNull
25    private final D definition;
26  
27    @Override
28    public D getDefinition() {
29      return definition;
30    }
31  
32    protected AbstractDefinitionJsonSchema(
33        @NonNull D definition) {
34      this.definition = definition;
35    }
36  
37    @Override
38    public boolean isInline(IJsonGenerationState state) {
39      return state.isInline(getDefinition());
40    }
41  
42    protected abstract void generateBody(
43        @NonNull IJsonGenerationState state,
44        @NonNull ObjectNode obj) throws IOException;
45  
46    @Override
47    public void generateInlineSchema(ObjectNode obj, IJsonGenerationState state) {
48      D definition = getDefinition();
49  
50      try {
51        generateTitle(definition, obj);
52        generateDescription(definition, obj);
53  
54        generateBody(state, obj);
55      } catch (IOException ex) {
56        throw new SchemaGenerationException(ex);
57      }
58    }
59  
60    public static void generateTitle(@NonNull IDefinition definition, @NonNull ObjectNode obj) {
61      MetadataUtils.generateTitle(definition, obj);
62    }
63  
64    public static void generateDescription(@NonNull IDefinition definition, @NonNull ObjectNode obj) {
65      MetadataUtils.generateDescription(definition, obj);
66    }
67  
68    @Override
69    public void gatherDefinitions(
70        @NonNull Map<IKey, IDefinitionJsonSchema<?>> gatheredDefinitions,
71        @NonNull IJsonGenerationState state) {
72      gatheredDefinitions.put(getKey(), this);
73    }
74  
75    public static class SimpleKey implements IKey {
76      @NonNull
77      private final IDefinition definition;
78  
79      public SimpleKey(@NonNull IDefinition definition) {
80        this.definition = definition;
81      }
82  
83      @Override
84      public IDefinition getDefinition() {
85        return definition;
86      }
87  
88      @Override
89      public String getJsonKeyFlagName() {
90        return null;
91      }
92  
93      @Override
94      public String getDiscriminatorProperty() {
95        return null;
96      }
97  
98      @Override
99      public String getDiscriminatorValue() {
100       return null;
101     }
102 
103     @Override
104     public int hashCode() {
105       return Objects.hash(definition, null, null, null);
106     }
107 
108     @SuppressWarnings("PMD.OnlyOneReturn")
109     @Override
110     public boolean equals(Object obj) {
111       if (this == obj) {
112         return true;
113       }
114       if (!(obj instanceof IKey)) {
115         return false;
116       }
117       IKey other = (IKey) obj;
118       return Objects.equals(definition, other.getDefinition())
119           && Objects.equals(null, other.getJsonKeyFlagName())
120           && Objects.equals(null, other.getDiscriminatorProperty())
121           && Objects.equals(null, other.getDiscriminatorValue());
122     }
123   }
124 
125 }