package de.greenrobot.daogenerator;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Once you have your model created, use this class to generate entities only
*
* @author Markus
* @author Charles
*/
public class EntityGenerator {
private Pattern patternKeepIncludes;
private Pattern patternKeepFields;
private Pattern patternKeepMethods;
private Template templateEntity;
public EntityGenerator() throws IOException {
System.out.println("EntityGenerator");
System.out.println("Copyright 2011-2014 Markus Junginger, greenrobot.de. Licensed under GPL V3.");
patternKeepIncludes = compilePattern("INCLUDES");
patternKeepFields = compilePattern("FIELDS");
patternKeepMethods = compilePattern("METHODS");
Configuration config = new Configuration();
config.setClassForTemplateLoading(this.getClass(), "/");
config.setObjectWrapper(new DefaultObjectWrapper());
templateEntity = config.getTemplate("entity-no-mapping.ftl");
}
private Pattern compilePattern(String sectionName) {
int flags = Pattern.DOTALL | Pattern.MULTILINE;
return Pattern.compile(".*^\\s*?//\\s*?KEEP " + sectionName + ".*?\n(.*?)^\\s*// KEEP " + sectionName
+ " END.*?\n", flags);
}
/** Generates all entities and DAOs for the given schema. */
public void generateAll(Schema schema, String outDir) throws Exception {
generateAll(schema, outDir, null);
}
/** Generates all entities and DAOs for the given schema. */
public void generateAll(Schema schema, String outDir, String outDirTest) throws Exception {
long start = System.currentTimeMillis();
File outDirFile = toFileForceExists(outDir);
schema.init2ndPass();
//schema.init3ndPass();
System.out.println("Processing schema version " + schema.getVersion() + "...");
List<Entity> entities = schema.getEntities();
for (Entity entity : entities) {
if (!entity.isProtobuf() && !entity.isSkipGeneration()) {
generate(templateEntity, outDirFile, entity.getJavaPackage(), entity.getClassName(), schema, entity);
}
}
long time = System.currentTimeMillis() - start;
System.out.println("Processed " + entities.size() + " entities in " + time + "ms");
}
protected File toFileForceExists(String filename) throws IOException {
File file = new File(filename);
if (!file.exists()) {
throw new IOException(filename
+ " does not exist. This check is to prevent accidental file generation into a wrong path.");
}
return file;
}
private void generate(Template template, File outDirFile, String javaPackage, String javaClassName, Schema schema,
Entity entity) throws Exception {
generate(template, outDirFile, javaPackage, javaClassName, schema, entity, null);
}
private void generate(Template template, File outDirFile, String javaPackage, String javaClassName, Schema schema,
Entity entity, Map<String, Object> additionalObjectsForTemplate) throws Exception {
Map<String, Object> root = new HashMap<String, Object>();
root.put("schema", schema);
root.put("entity", entity);
if (additionalObjectsForTemplate != null) {
root.putAll(additionalObjectsForTemplate);
}
try {
File file = toJavaFilename(outDirFile, javaPackage, javaClassName);
file.getParentFile().mkdirs();
if (entity != null && entity.getHasKeepSections()) {
checkKeepSections(file, root);
}
Writer writer = new FileWriter(file);
try {
template.process(root, writer);
writer.flush();
System.out.println("Written " + file.getCanonicalPath());
} finally {
writer.close();
}
} catch (Exception ex) {
System.err.println("Data map for template: " + root);
System.err.println("Error while generating " + javaPackage + "." + javaClassName + " ("
+ outDirFile.getCanonicalPath() + ")");
throw ex;
}
}
private void checkKeepSections(File file, Map<String, Object> root) {
if (file.exists()) {
try {
String contents = new String(DaoUtil.readAllBytes(file));
Matcher matcher;
matcher = patternKeepIncludes.matcher(contents);
if (matcher.matches()) {
root.put("keepIncludes", matcher.group(1));
}
matcher = patternKeepFields.matcher(contents);
if (matcher.matches()) {
root.put("keepFields", matcher.group(1));
}
matcher = patternKeepMethods.matcher(contents);
if (matcher.matches()) {
root.put("keepMethods", matcher.group(1));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
protected File toJavaFilename(File outDirFile, String javaPackage, String javaClassName) {
String packageSubPath = javaPackage.replace('.', '/');
File packagePath = new File(outDirFile, packageSubPath);
File file = new File(packagePath, javaClassName + ".java");
return file;
}
}