com.smartnews.jpa_entity_generator.CodeRenderer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jpa-entity-generator-enhance Show documentation
Show all versions of jpa-entity-generator-enhance Show documentation
Lombok-wired JPA entity source code generator, enchanced version.
package com.smartnews.jpa_entity_generator;
import com.smartnews.jpa_entity_generator.metadata.IndexInfo;
import com.smartnews.jpa_entity_generator.rule.Annotation;
import com.smartnews.jpa_entity_generator.rule.ClassAnnotationRule;
import com.smartnews.jpa_entity_generator.rule.ImportRule;
import com.smartnews.jpa_entity_generator.util.ResourceReader;
import freemarker.cache.StringTemplateLoader;
import freemarker.ext.beans.BeansWrapper;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import lombok.Data;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* Code renderer.
*/
public class CodeRenderer {
/**
* Renders source code by using Freemarker template engine.
*/
public static String render(String templatePath, RenderingData data) throws IOException, TemplateException {
Configuration config = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
StringTemplateLoader templateLoader = new StringTemplateLoader();
String source;
try (InputStream is = ResourceReader.getResourceAsStream(templatePath);
BufferedReader buffer = new BufferedReader(new InputStreamReader(is))) {
source = buffer.lines().collect(Collectors.joining("\n"));
}
templateLoader.putTemplate("template", source);
config.setTemplateLoader(templateLoader);
config.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
config.setObjectWrapper(new BeansWrapper(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS));
config.setWhitespaceStripping(true);
try (Writer writer = new java.io.StringWriter()) {
Template template = config.getTemplate("template");
template.process(data, writer);
return writer.toString();
}
}
/**
* Data used when rendering source code.
*/
@Data
public static class RenderingData {
private String packageName;
private String tableName;
private String className;
private String classComment;
private boolean jpa1Compatible = false;
private boolean requireJSR305 = false;
private List topAdditionalCodeList = new ArrayList<>();
private List bottomAdditionalCodeList = new ArrayList<>();
private List importRules = new ArrayList<>();
private List classAnnotationRules = new ArrayList<>();
private List interfaceNames = new ArrayList<>();
private List fields = new ArrayList<>();
private List primaryKeyFields = new ArrayList<>();
//add by zhengyi,索引信息
private List indexInfoList = new ArrayList<>();
@Data
public static class Field {
private String name;
private String columnName;
//add by zhengyi,字段长度
private int length;
//add by zhengyi,decimal精度
private int precision;
//add by zhengyi,decimal小数位数
private int digits;
private boolean nullable;
private String type;
private String comment;
private String defaultValue;
private boolean primaryKey;
private boolean autoIncrement;
private boolean primitive;
private String generatedValueStrategy;
private List annotations = new ArrayList<>();
}
}
}