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