根据配置文件自动生成java文件

back_recharge
wangyuan 2019-01-18 12:12:58 +08:00
parent cbd48c5097
commit 6a096f78e9
1 changed files with 110 additions and 6 deletions

View File

@ -3,18 +3,34 @@ import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
public class ExcelUtils {
private static boolean isWrite = true;
private static String excelPath ="tmp/";
private static String path = "C:/ljsd/jieling_conf/";
private static String excelPath ="D:/tmp/"; //excel 文件
private static String path = "conf/server/";
private static Set<String> oldFileNames = new HashSet<>();
private static String javaFilePath = "serverlogic/src/main/java/com/ljsd/jieling/config/";
public static void main(String[] args) throws IOException {
File oldfile = new File(path);
String[] list = oldfile.list();
oldFileNames.addAll(Arrays.asList(list));
readExcelData();
//只加载新文件
Set<String> newFileNames = new HashSet<>();
File newfile = new File(path);
String[] newList = newfile.list();
newFileNames.addAll(Arrays.asList(newList));
newFileNames.removeAll(oldFileNames);
if(!newFileNames.isEmpty()){
for(String newFileName : newFileNames){
String finalNewFileName = newFileName.replaceAll(".txt", "");
genJavaFile(finalNewFileName);
}
}
}
public static void readExcelData() throws IOException {
@ -40,6 +56,94 @@ public class ExcelUtils {
}
}
public static void genJavaFile(String fileName){
Map<String,String> fieldInfo = new HashMap<>();
File file = new File("conf/server/"+fileName +".txt");
StringBuffer stringBuffer = new StringBuffer("package com.ljsd.jieling.config;\n" +
"\n" +
"import com.ljsd.jieling.logic.STableManager;\n" +
"import com.ljsd.jieling.logic.Table;\n" +
"\n" +
"import java.util.Map;\n" +
"\n" +
"@Table(name =\"").append(fileName).append("\")\r\n");
stringBuffer.append("public class S").append(fileName).append(" implements BaseConfig {\n\n");
StringBuffer getInfos = new StringBuffer();
if(file.exists()){
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String firstLine = bufferedReader.readLine();
String secondLine = bufferedReader.readLine();
String[] s = firstLine.split("\t");
String[] s1 = secondLine.split("\t");
for(int i=0;i<s.length;i++){
String fieldType = typeInfo(s1[i]);
String fieldName = s[i];
String firstChart = fieldName.substring(0, 1).toLowerCase();
StringBuffer finalFieldName = new StringBuffer().append(firstChart).append(fieldName.substring(1));
getInfos.append("\tpublic ").append(fieldType).append(" ").append("get").append(fieldName).append("() {\n" +
" return " ).append(finalFieldName).append(";\r\n").append(
" }\r\n\n");
stringBuffer.append("\tprivate ").append(fieldType).append(" ").append(finalFieldName).append(";").
append("\r\n\n");
}
System.out.println(firstLine);
System.out.println(secondLine);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
getInfos.append("\r\n}");
stringBuffer.append(" \n\t@Override\n" +
" public void init() throws Exception {\n" +
" \n" +
" }\n\n\n");
File targetJavaFile = new File(javaFilePath + "S" + fileName + ".java");
try(PrintWriter printWriter = new PrintWriter(new FileOutputStream(targetJavaFile));) {
printWriter.print(stringBuffer);
printWriter.print(getInfos);
printWriter.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println(stringBuffer);
System.out.println(getInfos);
}
private static String typeInfo(String fieldInfo){
//mut,int#int,2
if(fieldInfo.contains("mut")){
String[] split = fieldInfo.split("#");
String[] typeInfos = split[1].split(",");
int nums = Integer.parseInt(typeInfos[1]);
StringBuffer sb = new StringBuffer();
if("string".equals(typeInfos[0])){
sb.append("String");
}else{
sb.append(typeInfos[0]);
}
for(int i=0;i<nums;i++){
sb.append("[]");
}
return sb.toString();
}
if("string".equals(fieldInfo)){
return "String";
}
return fieldInfo;
}
private static void buildDataInfo(Workbook wb) throws IOException {
Sheet sheet;
Row row;