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" }) T schemaWriter = newWriter(out);
88        S generationState = newGenerationState(metaschema, schemaWriter, configuration);
89        generateSchema(generationState);
90        generationState.flushWriter();
91      } catch (SchemaGenerationException ex) { // NOPMD avoid nesting same exception
92        throw ex;
93      } catch (Exception ex) { // NOPMD need to catch close exception
94        throw new SchemaGenerationException(ex);
95      }
96    }
97  
98    /**
99     * Determine the collection of root definitions.
100    *
101    * @param generationState
102    *          the schema generation state used for context and writing
103    * @param handler
104    *          a callback to execute on each identified root definition
105    * @return the list of identified root definitions
106    */
107   protected List<IAssemblyDefinition> analyzeDefinitions(
108       @NonNull S generationState,
109       @Nullable BiConsumer<ModuleIndex.DefinitionEntry, IDefinition> handler) {
110     // TODO: use of handler here is confusing and introduces side effects. Consider
111     // refactoring this in
112     // the caller
113 
114     List<IAssemblyDefinition> rootAssemblyDefinitions = new LinkedList<>();
115     for (ModuleIndex.DefinitionEntry entry : generationState.getMetaschemaIndex().getDefinitions()) {
116 
117       IDefinition definition = ObjectUtils.notNull(entry.getDefinition());
118       if (definition instanceof IAssemblyDefinition && ((IAssemblyDefinition) definition).isRoot()) {
119         // found root definition
120         IAssemblyDefinition assemblyDefinition = (IAssemblyDefinition) definition;
121         rootAssemblyDefinitions.add(assemblyDefinition);
122       }
123 
124       boolean referenced = entry.isReferenced();
125       if (!referenced) {
126         // skip unreferenced definitions
127         continue;
128       }
129 
130       if (handler != null) {
131         handler.accept(entry, definition);
132       }
133     }
134     return rootAssemblyDefinitions;
135   }
136 
137 }