001/*
002 * SPDX-FileCopyrightText: none
003 * SPDX-License-Identifier: CC0-1.0
004 */
005
006package gov.nist.secauto.metaschema.databind.codegen;
007
008import gov.nist.secauto.metaschema.core.model.IModule;
009import gov.nist.secauto.metaschema.core.util.CollectionUtil;
010import gov.nist.secauto.metaschema.databind.codegen.config.IBindingConfiguration;
011
012import org.apache.logging.log4j.LogManager;
013import org.apache.logging.log4j.Logger;
014
015import java.io.IOException;
016import java.nio.file.Path;
017import java.util.Collection;
018import java.util.Objects;
019
020import edu.umd.cs.findbugs.annotations.NonNull;
021
022/**
023 * Provides methods for generating Java classes based on a single or a
024 * collection of Metaschemas.
025 */
026public final class JavaGenerator {
027  private static final Logger LOGGER = LogManager.getLogger(JavaGenerator.class);
028
029  private JavaGenerator() {
030    // disable construction
031  }
032
033  /**
034   * Generate Java sources for the provided Metaschema module.
035   *
036   * @param module
037   *          the Metaschema module to generate Java sources for
038   * @param targetDir
039   *          the directory to generate sources in
040   * @param bindingConfiguration
041   *          the binding customizations to use when generating the Java classes
042   * @return information about all the produced classes
043   * @throws IOException
044   *           if an error occurred while generating the class
045   */
046  @NonNull
047  public static IProduction generate(
048      @NonNull IModule module,
049      @NonNull Path targetDir,
050      @NonNull IBindingConfiguration bindingConfiguration) throws IOException {
051    return generate(CollectionUtil.singletonList(module), targetDir, bindingConfiguration);
052  }
053
054  /**
055   * Generates Java classes for Module fields and flags.
056   *
057   * @param modules
058   *          the Metaschema modules to build classes for
059   * @param targetDirectory
060   *          the directory to generate classes in
061   * @param bindingConfiguration
062   *          binding customizations that can be used to set namespaces, class
063   *          names, and other aspects of generated classes
064   * @return information about all the produced classes
065   * @throws IOException
066   *           if a build error occurred while generating the class
067   */
068  @NonNull
069  public static IProduction generate(
070      @NonNull Collection<? extends IModule> modules,
071      @NonNull Path targetDirectory,
072      @NonNull IBindingConfiguration bindingConfiguration) throws IOException {
073    Objects.requireNonNull(modules, "metaschemas");
074    Objects.requireNonNull(targetDirectory, "generationTargetDirectory");
075    Objects.requireNonNull(bindingConfiguration, "bindingConfiguration");
076    if (LOGGER.isInfoEnabled()) {
077      LOGGER.info("Generating Java classes in: {}", targetDirectory);
078    }
079
080    return IProduction.of(modules, bindingConfiguration, targetDirectory);
081  }
082}