com.ksc.mission.base.codegen.Generation Maven / Gradle / Ivy
package com.ksc.mission.base.codegen;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.Version;
public class Generation {
protected Configuration config = null;
protected ModelView modelView;
protected TemplateType templateType;
public Generation(ModelView mv, TemplateType templateType) {
config = new Configuration(new Version("2.3.23"));
config.setClassForTemplateLoading(Generation.class, "/");
config.setDefaultEncoding("UTF-8");
this.modelView = mv;
this.templateType = templateType;
}
public void merge() {
Template template;
try {
template = config.getTemplate(templateType.getTemplateFileName());
Map templateData = new HashMap();
templateData.put("application", modelView.getModel().getApplication());
templateData.put("templateType", templateType);
templateData.put("model", modelView.getModel());
templateData.put("modelView", modelView);
templateData.put("primaryProperties", modelView.getModel().getPrimaryProperties());
templateData.put("allProperties", modelView.getModel().getProperties());
templateData.put("properties", modelView.getProperties());
try (FileWriter out = new FileWriter(templateType.getFullOutputFilePath(modelView))) {
template.process(templateData, out);
out.flush();
}
} catch (IOException | TemplateException e) {
e.printStackTrace();
}
}
}