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.json.schema;
28  
29  import com.fasterxml.jackson.databind.node.ObjectNode;
30  
31  import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine;
32  import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline;
33  import gov.nist.secauto.metaschema.core.model.IDefinition;
34  import gov.nist.secauto.metaschema.schemagen.SchemaGenerationException;
35  import gov.nist.secauto.metaschema.schemagen.json.impl.JsonGenerationState;
36  
37  import java.io.IOException;
38  
39  import edu.umd.cs.findbugs.annotations.NonNull;
40  
41  public abstract class AbstractDefinitionJsonSchema<D extends IDefinition>
42      extends AbstractDefineableJsonSchema {
43    @NonNull
44    private final D definition;
45  
46    protected AbstractDefinitionJsonSchema(@NonNull D definition) {
47      this.definition = definition;
48    }
49  
50    @NonNull
51    protected D getDefinition() {
52      return definition;
53    }
54  
55    @Override
56    public boolean isInline(JsonGenerationState state) {
57      return state.isInline(getDefinition());
58    }
59  
60    @Override
61    protected String generateDefinitionName(JsonGenerationState state) {
62      return state.getTypeNameForDefinition(definition, null);
63    }
64  
65    protected abstract void generateBody(@NonNull JsonGenerationState state, @NonNull ObjectNode obj) throws IOException;
66  
67    @Override
68    public void generateSchema(JsonGenerationState state, ObjectNode obj) {
69      D definition = getDefinition();
70  
71      try {
72        generateTitle(definition, obj);
73        generateDescription(definition, obj);
74  
75        generateBody(state, obj);
76      } catch (IOException ex) {
77        throw new SchemaGenerationException(ex);
78      }
79    }
80  
81    public static void generateTitle(@NonNull IDefinition definition, @NonNull ObjectNode obj) {
82      String formalName = definition.getEffectiveFormalName();
83      if (formalName != null) {
84        obj.put("title", formalName);
85      }
86    }
87  
88    public static void generateDescription(@NonNull IDefinition definition, @NonNull ObjectNode obj) {
89      MarkupLine description = definition.getDescription();
90  
91      StringBuilder retval = null;
92      if (description != null) {
93        retval = new StringBuilder().append(description.toMarkdown());
94      }
95  
96      MarkupMultiline remarks = definition.getRemarks();
97      if (remarks != null) {
98        if (retval == null) {
99          retval = new StringBuilder();
100       } else {
101         retval.append("\n\n");
102       }
103       retval.append(remarks.toMarkdown());
104     }
105     if (retval != null) {
106       obj.put("description", retval.toString());
107     }
108   }
109 }