<commit_before>/**
 * This file is part of the Harmony package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
package com.tactfactory.harmony.template;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Map;

import com.tactfactory.harmony.Harmony;
import com.tactfactory.harmony.meta.ApplicationMetadata;
import com.tactfactory.harmony.plateforme.BaseAdapter;
import com.tactfactory.harmony.utils.ConsoleUtils;
import com.tactfactory.harmony.utils.TactFileUtils;

import freemarker.cache.FileTemplateLoader;
import freemarker.cache.MultiTemplateLoader;
import freemarker.cache.TemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

/**
 * Base Generator.
 */
public abstract class BaseGenerator {
	// Meta-models
	/** The application metadata. */
	private ApplicationMetadata appMetas;

	// Platform adapter
	/** The used adapter. */
	private BaseAdapter adapter;
	/** The datamodel. */
	private Map<String, Object> datamodel;

	// Config
	/** The freemarker configuration. */
	private Configuration cfg = new Configuration();

	/**
	 * @return the appMetas
	 */
	public final ApplicationMetadata getAppMetas() {
		return appMetas;
	}

	/**
	 * @param appMetas the appMetas to set
	 */
	public final void setAppMetas(final ApplicationMetadata appMetas) {
		this.appMetas = appMetas;
	}

	/**
	 * @return the adapter
	 */
	public final BaseAdapter getAdapter() {
		return adapter;
	}

	/**
	 * @param adapter the adapter to set
	 */
	public final void setAdapter(final BaseAdapter adapter) {
		this.adapter = adapter;
	}

	/**
	 * @return the datamodel
	 */
	public final Map<String, Object> getDatamodel() {
		return datamodel;
	}

	/**
	 * @param datamodel the datamodel to set
	 */
	public final void setDatamodel(final Map<String, Object> datamodel) {
		this.datamodel = datamodel;
	}

	/**
	 * @return the cfg
	 */
	public final Configuration getCfg() {
		return cfg;
	}

	/**
	 * @param cfg the cfg to set
	 */
	public final void setCfg(final Configuration cfg) {
		this.cfg = cfg;
	}

	/**
	 * Constructor.
	 * @param adapt The adapter to use
	 * @throws Exception if adapter is null
	 */
	public BaseGenerator(final BaseAdapter adapt) throws Exception {
		if (adapt == null) {
			throw new Exception("No adapter define.");
		}

		// FIXME Clone object tree
		this.appMetas	= ApplicationMetadata.INSTANCE;
		this.adapter	= adapt;

		//this.cfg.setDirectoryForTemplateLoading(
		//		new File(Harmony.getRootPath() + "/vendor/tact-core"));

		final  Object[] files = Harmony.getTemplateFolders().values().toArray();
		final  TemplateLoader[] loaders = new TemplateLoader[files.length + 1];
		for (int i = 0; i < files.length; i++) {
			final FileTemplateLoader ftl =
					new FileTemplateLoader((File) files[i]);
			loaders[i] = ftl;
		}
		loaders[files.length] = new FileTemplateLoader(
				new File(Harmony.getRootPath() + "/vendor/tact-core"));
		final MultiTemplateLoader mtl = new MultiTemplateLoader(loaders);

		cfg.setTemplateLoader(mtl);
	}

	/**
	 * Make Java Source Code.
	 *
	 * @param templatePath Template path file.
	 * 		For list activity is "TemplateListActivity.java"
	 * @param generatePath The destination file path<commit_msg>Fix #3050<commit_after>/**
 * This file is part of the Harmony package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
package com.tactfactory.harmony.template;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Map;

import com.tactfactory.harmony.Harmony;
import com.tactfactory.harmony.meta.ApplicationMetadata;
import com.tactfactory.harmony.plateforme.BaseAdapter;
import com.tactfactory.harmony.utils.ConsoleUtils;
import com.tactfactory.harmony.utils.TactFileUtils;

import freemarker.cache.FileTemplateLoader;
import freemarker.cache.MultiTemplateLoader;
import freemarker.cache.TemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

/**
 * Base Generator.
 */
public abstract class BaseGenerator {
	// Meta-models
	/** The application metadata. */
	private ApplicationMetadata appMetas;

	// Platform adapter
	/** The used adapter. */
	private BaseAdapter adapter;
	/** The datamodel. */
	private Map<String, Object> datamodel;

	// Config
	/** The freemarker configuration. */
	private Configuration cfg = new Configuration();

	/**
	 * @return the appMetas
	 */
	public final ApplicationMetadata getAppMetas() {
		return appMetas;
	}

	/**
	 * @param appMetas the appMetas to set
	 */
	public final void setAppMetas(final ApplicationMetadata appMetas) {
		this.appMetas = appMetas;
	}

	/**
	 * @return the adapter
	 */
	public final BaseAdapter getAdapter() {
		return adapter;
	}

	/**
	 * @param adapter the adapter to set
	 */
	public final void setAdapter(final BaseAdapter adapter) {
		this.adapter = adapter;
	}

	/**
	 * @return the datamodel
	 */
	public final Map<String, Object> getDatamodel() {
		return datamodel;
	}

	/**
	 * @param datamodel the datamodel to set
	 */
	public final void setDatamodel(final Map<String, Object> datamodel) {
		this.datamodel = datamodel;
	}

	/**
	 * @return the cfg
	 */
	public final Configuration getCfg() {
		return cfg;
	}

	/**
	 * @param cfg the cfg to set
	 */
	public final void setCfg(final Configuration cfg) {
		this.cfg = cfg;
	}

	/**
	 * Constructor.
	 * @param adapt The adapter to use
	 * @throws Exception if adapter is null
	 */
	public BaseGenerator(final BaseAdapter adapt) {
		if (adapt == null) {
			throw new RuntimeException("No adapter define.");
		}

		try {
			// FIXME Clone object tree
			this.appMetas	= ApplicationMetadata.INSTANCE;
			this.adapter	= adapt;
	
			//this.cfg.setDirectoryForTemplateLoading(
			//		new File(Harmony.getRootPath() + "/vendor/tact-core"));
	
			final  Object[] files = 
					Harmony.getTemplateFolders().values().toArray();
			final  TemplateLoader[] loaders = 
					new TemplateLoader[files.length + 1];
			for (int i = 0; i < files.length; i++) {
				final FileTemplateLoader ftl =
						new FileTemplateLoader((File) files[i]);
				loaders[i] = ftl;
			}
				loaders[files.length] = new FileTemplateLoader(
						new File(Harmony.getRootPath() + "/vendor/tact-core"));
			final MultiTemplateLoader mtl = new MultiTemplateLoader(loaders);
	
			this.cfg.setTemplateLoader(mtl);

		} catch (IOException e) {
			throw new RuntimeException("Error with template loading : " 
						+ e.getMessage());
		}
	}

	/**
	 * Make Java Source Code.
	 *
	 * @param templatePath Template path file.
	 * 		For list activity is "TemplateListActivity.java"
	 * @param generatePath The destination file path
{'fraction_non_alphanumeric': 0.112375533428165, 'fraction_numerical': 0.0011379800853485065, 'mean_word_length': 3.7895095367847413, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7586218', 'n_tokens_mistral': 2345, 'n_tokens_neox': 2126, 'n_words': 591}