1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.schemagen;
7   
8   import gov.nist.secauto.metaschema.core.configuration.IConfiguration;
9   import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition;
10  import gov.nist.secauto.metaschema.core.model.IDefinition;
11  import gov.nist.secauto.metaschema.core.model.IModule;
12  import gov.nist.secauto.metaschema.core.util.ObjectUtils;
13  import gov.nist.secauto.metaschema.schemagen.datatype.IDatatypeManager;
14  
15  import java.io.Writer;
16  import java.util.LinkedList;
17  import java.util.List;
18  import java.util.function.BiConsumer;
19  
20  import edu.umd.cs.findbugs.annotations.NonNull;
21  import edu.umd.cs.findbugs.annotations.Nullable;
22  
23  /**
24   * Thsi abstract class provides a common implementation shared by all schema
25   * generators.
26   *
27   * @param <T>
28   *          the writer type
29   * @param <D>
30   *          the {@link IDatatypeManager} type
31   * @param <S>
32   *          the {@link IGenerationState} type
33   */
34  public abstract class AbstractSchemaGenerator<
35      T extends AutoCloseable,
36      D extends IDatatypeManager,
37      S extends AbstractGenerationState<
38          T, D>>
39      implements ISchemaGenerator {
40  
41    /**
42     * Create a new writer to use to write the schema.
43     *
44     * @param out
45     *          the {@link Writer} to write the schema content to
46     * @return the schema writer
47     * @throws SchemaGenerationException
48     *           if an error occurred while creating the writer
49     */
50    @NonNull
51    protected abstract T newWriter(@NonNull Writer out);
52  
53    /**
54     * Create a new schema generation state object.
55     *
56     * @param module
57     *          the Metaschema module to generate the schema for
58     * @param schemaWriter
59     *          the writer to use to write the schema
60     * @param configuration
61     *          the generation configuration
62     * @return the schema generation state used for context and writing
63     * @throws SchemaGenerationException
64     *           if an error occurred while creating the generation state object
65     */
66    @NonNull
67    protected abstract S newGenerationState(
68        @NonNull IModule module,
69        @NonNull T schemaWriter,
70        @NonNull IConfiguration<SchemaGenerationFeature<?>> configuration);
71  
72    /**
73     * Called to generate the actual schema content.
74     *
75     * @param generationState
76     *          the generation state object
77     */
78    protected abstract void generateSchema(@NonNull S generationState);
79  
80    @Override
81    public void generateFromModule(
82        IModule metaschema,
83        Writer out,
84        IConfiguration<SchemaGenerationFeature<?>> configuration) {
85      try {
86        // avoid automatically closing streams not owned by the generator
87        @SuppressWarnings({ "PMD.CloseResource", "resource" })
88        T schemaWriter = newWriter(out);
89        S generationState = newGenerationState(metaschema, schemaWriter, configuration);
90        generateSchema(generationState);
91        generationState.flushWriter();
92      } catch (SchemaGenerationException ex) { // NOPMD avoid nesting same exception
93        throw ex;
94      } catch (Exception ex) { // NOPMD need to catch close exception
95        throw new SchemaGenerationException(ex);
96      }
97    }
98  
99    /**
100    * Determine the collection of root definitions.
101    *
102    * @param generationState
103    *          the schema generation state used for context and writing
104    * @param handler
105    *          a callback to execute on each identified root definition
106    * @return the list of identified root definitions
107    */
108   protected List<IAssemblyDefinition> analyzeDefinitions(
109       @NonNull S generationState,
110       @Nullable BiConsumer<ModuleIndex.DefinitionEntry, IDefinition> handler) {
111     // TODO: use of handler here is confusing and introduces side effects. Consider
112     // refactoring this in
113     // the caller
114 
115     List<IAssemblyDefinition> rootAssemblyDefinitions = new LinkedList<>();
116     for (ModuleIndex.DefinitionEntry entry : generationState.getMetaschemaIndex().getDefinitions()) {
117 
118       IDefinition definition = ObjectUtils.notNull(entry.getDefinition());
119       if (definition instanceof IAssemblyDefinition && ((IAssemblyDefinition) definition).isRoot()) {
120         // found root definition
121         IAssemblyDefinition assemblyDefinition = (IAssemblyDefinition) definition;
122         rootAssemblyDefinitions.add(assemblyDefinition);
123       }
124 
125       boolean referenced = entry.isReferenced();
126       if (!referenced) {
127         // skip unreferenced definitions
128         continue;
129       }
130 
131       if (handler != null) {
132         handler.accept(entry, definition);
133       }
134     }
135     return rootAssemblyDefinitions;
136   }
137 
138 }