001/*
002 * SPDX-FileCopyrightText: none
003 * SPDX-License-Identifier: CC0-1.0
004 */
005
006package dev.metaschema.databind.codegen;
007
008import org.apache.logging.log4j.LogManager;
009import org.apache.logging.log4j.Logger;
010
011import java.io.IOException;
012import java.nio.file.Path;
013import java.util.Collection;
014import java.util.Objects;
015
016import dev.metaschema.core.model.IModule;
017import dev.metaschema.core.util.CollectionUtil;
018import dev.metaschema.databind.codegen.config.IBindingConfiguration;
019import edu.umd.cs.findbugs.annotations.NonNull;
020
021/**
022 * Provides methods for generating Java classes based on a single or a
023 * collection of Metaschemas.
024 */
025public final class JavaGenerator {
026  private static final Logger LOGGER = LogManager.getLogger(JavaGenerator.class);
027
028  private JavaGenerator() {
029    // disable construction
030  }
031
032  /**
033   * Generate Java sources for the provided Metaschema module.
034   *
035   * @param module
036   *          the Metaschema module to generate Java sources for
037   * @param targetDir
038   *          the directory to generate sources in
039   * @param bindingConfiguration
040   *          the binding customizations to use when generating the Java classes
041   * @return information about all the produced classes
042   * @throws IOException
043   *           if an error occurred while generating the class
044   */
045  @NonNull
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}