All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.regnosys.rosetta.translate.GeneratorMain Maven / Gradle / Ivy

There is a newer version: 11.25.1
Show newest version
package com.regnosys.rosetta.translate;

import com.google.common.base.Joiner;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
import com.google.inject.Injector;
import com.regnosys.rosetta.RosettaStandaloneSetup;

import org.apache.log4j.Logger;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

/**
 * We think the original intention of this class is to make the builds faster i.e. generate once and use generated artefacts
 */
public class GeneratorMain {

    private static final Logger LOGGER = Logger.getLogger(GeneratorMain.class);

    public static void main(String[] args) throws IOException {
        CLI options = new CLI(args);
        Path javaOut = Paths.get(options.getOutputPath());

        Injector injector = new RosettaStandaloneSetup().createInjectorAndDoEMFRegistration();
        IngesterGenerator generator = injector.getInstance(IngesterGenerator.class);
        SynonymToEnumMapGenerator synonymToEnumMapGenerator = injector.getInstance(SynonymToEnumMapGenerator.class);

        new Translator(options, GeneratorMain.class.getClassLoader(), generator, synonymToEnumMapGenerator).generateJava(javaOut, true);
    }

    static class CLI implements TranslatorOptions {
        private final Multimap argsMap;

        public CLI(String[] args) {
            String[] argList = Joiner.on(" ").skipNulls().join(args).split("\\s+");
            LOGGER.info("Running with args: " + Joiner.on(' ').join(argList));

            List> partitionedArgs = Lists.partition(Lists.newArrayList(argList), 2);
            Multimap> indexed = Multimaps.index(partitionedArgs, l -> l.get(0));
            argsMap = Multimaps.transformValues(indexed, l -> Iterables.get(l, 1, null));
        }

        @Override
        public String getName() {
            return requiredSingleValue("name");
        }

        @Override
        public Optional getModelId() {
            return singleValue("modelId");
        }

        @Override
        public Collection getModelClasspath() {
            return multiValue("model-classpath");
        }

        @Override
        public String getModelFileDirIncludeRegex() {
            return singleValue("model-file-dir-include-regex").orElse(".*\\.rosetta");
        }

        @Override
        public Optional getModelFileDirExcludeRegex() {
            return singleValue("model-file-dir-exclude-regex");
        }

        @Override
        public String getXsdFilePath(String rosettaClassName) {
            return requiredSingleValue(rosettaClassName.toLowerCase() + "-xsd-file-path");
        }

        @Override
        public String getXmlExclusionsFilePath() {
            return singleValue("xml-exclusions-file-path").orElse(null);
        }

        @Override
        public Collection getRosettaClasses() {
            return requiredMultiValue("rosetta-class");
        }

        @Override
        public String getGeneratedPackage() {
            return requiredSingleValue("package");
        }

        public String getOutputPath() {
            return requiredSingleValue("output");
        }

        @Override
        public Collection getSynonymSources(String rosettaClassName) {
            return requiredMultiValue(rosettaClassName.toLowerCase() + "-synonym-source");
        }

        @Override
        public Collection getTopLevelTags(String rosettaClassName) {
            return multiValue(rosettaClassName.toLowerCase() + "-top-level-tags");
        }

        @Override
        public String getGeneratedFactoryName() {
            return singleValue("generated-factory-name").orElse("XmlHandlerFactoryImpl");
        }

        @Override
        public String getChildPackageName(String rosettaClass) {
            return requiredSingleValue("namespace");
        }

        @Override
        public String getCdmClassesDir() {
            return singleValue("cdm-classes-dir").orElse("");
        }

        @Override
        public Collection getIntegrationTestRoots(String rosettaClassName) {
            return multiValue(rosettaClassName.toLowerCase() + "integration-test-files-root-dir");
        }

        @Override
        public boolean isVerbose() {
            Optional verbose = singleValue("verbose");
            return verbose.map(Boolean::valueOf).orElse(false);
        }

        @Override
        public boolean clean() {
            return singleValue("clean").map(Boolean::valueOf).orElse(true);
        }

        @Override
        public boolean isJson() {
            return false;
        }

        private Optional singleValue(String key) {
            Collection value = argsMap.get("--" + key);
            if (value.isEmpty()) {
                return Optional.empty();
            }
            return Optional.of(value).map(Iterables::getOnlyElement);
        }

        private String requiredSingleValue(String key) {
            return singleValue(key).orElseThrow(() -> new RuntimeException(key + " is a required argument"));
        }

        private Collection multiValue(String key) {
            return argsMap.get("--" + key);
        }

        private Collection requiredMultiValue(String key) {
            Collection collection = argsMap.get("--" + key);
            if (collection.isEmpty()) {
                throw new RuntimeException(key + " is a required argument");
            }
            return collection;
        }

        @Override
        public boolean isDefault() {
            return true;
        }

        @Override
        public String getFullClassname(String rosettaClassName) {
            return requiredSingleValue("fullClassName");
        }

        @Override
        public boolean isCoreIgnored() {
            return true;
        }

        @Override
        public boolean preProcessJson() {
            return false;
        }

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy