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  public static IProduction generate(
047      @NonNull IModule module,
048      @NonNull Path targetDir,
049      @NonNull IBindingConfiguration bindingConfiguration) throws IOException {
050    return generate(CollectionUtil.singletonList(module), targetDir, bindingConfiguration);
051  }
052
053  /**
054   * Generates Java classes for Module fields and flags.
055   *
056   * @param modules
057   *          the Metaschema modules to build classes for
058   * @param targetDirectory
059   *          the directory to generate classes in
060   * @param bindingConfiguration
061   *          binding customizations that can be used to set namespaces, class
062   *          names, and other aspects of generated classes
063   * @return information about all the produced classes
064   * @throws IOException
065   *           if a build error occurred while generating the class
066   */
067  @NonNull
068  public static IProduction generate(
069      @NonNull Collection<? extends IModule> modules,
070      @NonNull Path targetDirectory,
071      @NonNull IBindingConfiguration bindingConfiguration) throws IOException {
072    Objects.requireNonNull(modules, "metaschemas");
073    Objects.requireNonNull(targetDirectory, "generationTargetDirectory");
074    Objects.requireNonNull(bindingConfiguration, "bindingConfiguration");
075    if (LOGGER.isInfoEnabled()) {
076      LOGGER.info("Generating Java classes in: {}", targetDirectory);
077    }
078
079    return IProduction.of(modules, bindingConfiguration, targetDirectory);
080  }
081}