<commit_before>// Copyright (c) 2013-2014, Webit Team. All Rights Reserved.
package webit.generator;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import webit.generator.components.GeneratorProcesser;
import webit.generator.components.TableFactory;
import webit.generator.model.Table;
import webit.generator.saver.FileEntry;
import webit.generator.saver.FileSaver;
import webit.generator.saver.FolderEntry;
import webit.generator.util.FileUtil;
import webit.generator.util.Logger;
import webit.generator.util.ResourceUtil;
import webit.script.Engine;
import webit.script.exceptions.ParseException;
import webit.script.global.GlobalManager;
import webit.script.io.Out;
import webit.script.io.impl.DiscardOut;
import webit.script.lang.KeyValues;
import webit.script.util.KeyValuesUtil;

/**
 *
 * @author ZQQ
 */
public class Generator {

    private String outroot;
    private GlobalManager globalManager;
    private Engine templateEngine;
    private List<Table> tableList;
    private List<Table> whiteTables;
    private GeneratorProcesser[] processers;
    private Map<Object, FileSaver> fileSaverMap;

    private void initTemplateEngine() {
        final Engine engine = Engine.create(Config.getString("engine.props"));
        this.globalManager = engine.getGlobalManager();
        this.templateEngine = engine;
    }

    public void process() {
        try {

            outroot = FileUtil.concat(Config.getWorkPath(), Config.getRequiredString("outroot"));
            Logger.info("outroot: " + outroot);

            String[] processersClass = Config.getArrayWithoutComment("processers");
            processers = new GeneratorProcesser[processersClass.length];
            {
                int i = 0;
                for (String string : processersClass) {
                    Logger.info("Running processer: " + string);
                    GeneratorProcesser processer = processers[i++] = (GeneratorProcesser) ResourceUtil.loadClass(string).newInstance();
                    processer.init(this);
                }
            }

            initTemplateEngine();
            initRoot();
            for (GeneratorProcesser processer : processers) {
                processer.afterInitRoot();
            }
            initFileSaver();

            //create_folders
            {
                for (String folder : Config.getCreateFolders()) {
                    final int i = folder.indexOf('/');
                    getFileSaver(folder.substring(0, i)).createFolder(folder.substring(i + 1));
                }
            }

            //copys
            {
                for (String copy : Config.getCopys()) {
                    final int i = copy.indexOf('/');
                    String fileSaverName = copy.substring(0, i);
                    FileSaver fileSaver = getFileSaver(fileSaverName);
                    if (fileSaver == null) {
                        Logger.error("Not found FileSaver:" + fileSaverName);
                        throw new RuntimeException("Not found FileSaver:" + fileSaverName);
                    }
                    fileSaver.copyFile(copy.substring(i + 1), copy);
                }
            }

            //init Templates
            {
                final String[] initTemplates = Config.getInitTemplates();
                if (initTemplates.length != 0) {
                    final GlobalManager globalManager = this.templateEngine.getGlobalManager();
                    final Out out = new DiscardOut();
                    final KeyValues params = KeyValuesUtil.wrap(new String[]{
                        "GLOBAL",
                        "CONST"
                    }, new Object[]{
                        globalManager.getGlobalBag(),
                        globalManager.getConstBag()
                    });

                    for (String templateName : initTemplates) {
                        if (templateName != null
                                && (templateName = templateName.trim()).length() != 0) {
                            this.templateEngine.getTemplate(templateName)
                                    .merge(params, out);
                            //Commit Global
                            globalManager.commit();
                        }
                    }
                }
            }
            //TODO: log table summary
            margeTemplates();
        } catch (Exception ex) {
            Logger.error(ex.getMessage(), ex);<commit_msg>log init-templates<commit_after>// Copyright (c) 2013-2014, Webit Team. All Rights Reserved.
package webit.generator;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import webit.generator.components.GeneratorProcesser;
import webit.generator.components.TableFactory;
import webit.generator.model.Table;
import webit.generator.saver.FileEntry;
import webit.generator.saver.FileSaver;
import webit.generator.saver.FolderEntry;
import webit.generator.util.FileUtil;
import webit.generator.util.Logger;
import webit.generator.util.ResourceUtil;
import webit.script.Engine;
import webit.script.exceptions.ParseException;
import webit.script.global.GlobalManager;
import webit.script.io.Out;
import webit.script.io.impl.DiscardOut;
import webit.script.lang.KeyValues;
import webit.script.util.KeyValuesUtil;

/**
 *
 * @author ZQQ
 */
public class Generator {

    private String outroot;
    private GlobalManager globalManager;
    private Engine templateEngine;
    private List<Table> tableList;
    private List<Table> whiteTables;
    private GeneratorProcesser[] processers;
    private Map<Object, FileSaver> fileSaverMap;

    private void initTemplateEngine() {
        final Engine engine = Engine.create(Config.getString("engine.props"));
        this.globalManager = engine.getGlobalManager();
        this.templateEngine = engine;
    }

    public void process() {
        try {

            outroot = FileUtil.concat(Config.getWorkPath(), Config.getRequiredString("outroot"));
            Logger.info("outroot: " + outroot);

            String[] processersClass = Config.getArrayWithoutComment("processers");
            processers = new GeneratorProcesser[processersClass.length];
            {
                int i = 0;
                for (String string : processersClass) {
                    Logger.info("Running processer: " + string);
                    GeneratorProcesser processer = processers[i++] = (GeneratorProcesser) ResourceUtil.loadClass(string).newInstance();
                    processer.init(this);
                }
            }

            initTemplateEngine();
            initRoot();
            for (GeneratorProcesser processer : processers) {
                processer.afterInitRoot();
            }
            initFileSaver();

            //create_folders
            {
                for (String folder : Config.getCreateFolders()) {
                    final int i = folder.indexOf('/');
                    getFileSaver(folder.substring(0, i)).createFolder(folder.substring(i + 1));
                }
            }

            //copys
            {
                for (String copy : Config.getCopys()) {
                    final int i = copy.indexOf('/');
                    String fileSaverName = copy.substring(0, i);
                    FileSaver fileSaver = getFileSaver(fileSaverName);
                    if (fileSaver == null) {
                        Logger.error("Not found FileSaver:" + fileSaverName);
                        throw new RuntimeException("Not found FileSaver:" + fileSaverName);
                    }
                    fileSaver.copyFile(copy.substring(i + 1), copy);
                }
            }

            //init Templates
            {
                final String[] initTemplates = Config.getInitTemplates();
                if (initTemplates.length != 0) {
                    final GlobalManager globalManager = this.templateEngine.getGlobalManager();
                    final Out out = new DiscardOut();
                    final KeyValues params = KeyValuesUtil.wrap(new String[]{
                        "GLOBAL",
                        "CONST"
                    }, new Object[]{
                        globalManager.getGlobalBag(),
                        globalManager.getConstBag()
                    });

                    for (String templateName : initTemplates) {
                        if (templateName != null
                                && (templateName = templateName.trim()).length() != 0) {
                            if (Logger.isDebugEnabled()) {
                                Logger.debug("Run init: " + templateName);
                            }
                            this.templateEngine.getTemplate(templateName)
                                    .merge(params, out);
                            //Commit Global
                            globalManager.commit();
                        }
                    }
                }
            }
            //TODO: log table summary
            margeTemplates();
        } catch (Exception ex) {
            Logger.error(ex.getMessage(), ex);
{'fraction_non_alphanumeric': 0.09679161715458572, 'fraction_numerical': 0.003240790752943718, 'mean_word_length': 1.6850348027842228, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4246811', 'n_tokens_mistral': 2318, 'n_tokens_neox': 2198, 'n_words': 463}