package test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import javax.sound.sampled.AudioFormat.Encoding;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.aspectj.weaver.NewConstructorTypeMunger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;
/**
* 代码生成器
* @author Sirius
*
*/
public class AutoCreateCodeUtils {
private static final String ENCODING = "utf-8";
// 声明需要创建的 “实体” 数组
private String[] entityArr = {"Customer"};
private String[] entityNameArr = {"客户跟进"};
// 声明自动生成路径常量
public final String JAVA_DIR ="src/";
public final String RESOURCE_DIR ="resources/";
public final String TEST_DIR ="test/test/";
public final String TEMPLATE_DIR ="templates/";
public final String PACKAGE ="cn/itsource/crm/";
public final String PAGE ="WebContent/WEB-INF/views/";
// 声明使用模板数组
private String[] templateArr = {"Query.java",
"Service.java",
"ServiceImpl.java",
"Action.java",
"action.xml",
"service.xml",
"ServiceTestDemo.java",
"list.jsp"};
// 声明模板解析后的输出路径
private String[] outPathArr ={
JAVA_DIR+PACKAGE+"query/",
JAVA_DIR+PACKAGE+"service/",
JAVA_DIR+PACKAGE+"service/impl/",
JAVA_DIR+PACKAGE+"web/action/",
RESOURCE_DIR,
RESOURCE_DIR,
TEST_DIR,
PAGE
};
@Test
public void test() throws Exception {
// 循环待添加的实体列表
for (int n = 0; n < entityArr.length; n++) {
String entity = entityArr[n];
String entityName = entityNameArr[n];
// 创建模板上下文对象
VelocityContext context = new VelocityContext();
context.put("entity", entity);
// 设置变量
// Employee >> employee or SystemRole >> systemRole
String lowercaseEntity = entity.substring(0, 1).toLowerCase()+entity.substring(1);
context.put("lowercaseEntity", lowercaseEntity);
// 实体名称
context.put("entityName", entityName);
// 遍历每一个模板名称
for (int i = 0; i < templateArr.length; i++) {
String tempName = templateArr[i];
// XML配置暂时不处理
// if(tempName.contains(".xml")){
// continue;
// }
String outPath = outPathArr[i];
// 获取输出文件
/*
* ...query/ + Department + Query.java
* ...service/+ 'I' + Department + Service.java
* ...service/impl/+ Department + ServiceImpl.java
*
* ...resource/service.xml
*/
String outFilePath = outPath +entity+ tempName;
if("Service.java".equals(tempName)){// 如果是处理接口
// ...service/+ 'I' + Department + Service.java
outFilePath = outPath+"I" +entity+ tempName;
}else if("list.jsp".equals(tempName)){// 如果是处理页面
// ...page/department/list.jsp
outFilePath = outPath +lowercaseEntity+"/" + tempName;
System.out.println("list:" + outFilePath);
}
//输出文件
File outFile = new File(outFilePath);
//判断父文件夹
if(!outFile.getParentFile().exists()){
outFile.getParentFile().mkdirs();
}
// 使用velocity加载每一个模板
// Action.java
Template template = Velocity.getTemplate(TEMPLATE_DIR + tempName,ENCODING);
// 合并
if("service.xml".equals(tempName)){// 添加service配置
// 获取模板合成的内容
StringWriter writer = new StringWriter();
template.merge(context, writer );
Document newDoc = DocumentHelper.parseText(writer.toString());
System.out.println("newDoc:" + newDoc);
// 获取输出文件的内容
SAXReader saxReader = new SAXReader();
Document baseXml = saxReader.read(new File(RESOURCE_DIR + "applicationContext-service.xml"));
// System.out.println(baseXml.asXML());
// 把newDoc的根节点内容追加到baseXml根节点下
baseXml.getRootElement().add(newDoc.getRootElement());
// 把新内容会写回去
XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(new File(RESOURCE_DIR +"applicationContext-service.xml")));
xmlWriter.write(baseXml);
xmlWriter.close();
}else if("action.xml".equals(tempName)){// 添加action配置
// 获取模板合成的内容
StringWriter writer = new StringWriter();
template.merge(context, writer );
Document newDoc = DocumentHelper.parseText(writer.toString());
// 获取输出文件的内容
SAXReader saxReader = new SAXReader();
Document baseXml = saxReader.read(new File(RESOURCE_DIR + "applicationContext-action.xml"));
// System.out.println(baseXml.asXML());
// 把newDoc的根节点内容追加到baseXml根节点下
baseXml.getRootElement().add(newDoc.getRootElement());
// 把新内容会写回去
XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(new File(RESOURCE_DIR +"applicationContext-action.xml")));
xmlWriter.write(baseXml);
xmlWriter.close();
}else{// 添加其他代码
FileWriter writer = new FileWriter(outFile);
template.merge(context, writer);
writer.close();
}
}
//输出
System.err.println("代码生成完毕,请刷新工程。。。");
}
}
}