Skip to content

Commit a54c44a

Browse files
author
杨利兵
committed
完成msg.cs生成功能
1 parent f08052d commit a54c44a

17 files changed

+92
-204
lines changed

src/com/yanglb/utilitys/codegen/core/generator/BaseGenerator.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import java.lang.reflect.Field;
1919
import java.util.ArrayList;
2020
import java.util.Arrays;
21+
import java.util.Date;
22+
import java.util.HashMap;
2123
import java.util.List;
2224

2325
import com.yanglb.utilitys.codegen.core.model.OptionModel;
@@ -28,6 +30,7 @@
2830
public class BaseGenerator implements IGenerator{
2931
protected ParamaModel paramaModel;
3032
protected List<OptionModel> supportOptions;
33+
protected HashMap<String, String> settingMap;
3134

3235
protected void printInfo() {
3336
System.out.println("生成信息:");
@@ -68,6 +71,9 @@ protected void printInfo() {
6871
public void init(ParamaModel paramaModel) {
6972
this.paramaModel = paramaModel;
7073
this.supportOptions = new ArrayList<OptionModel>();
74+
this.settingMap = new HashMap<String, String>();
75+
76+
this.settingMap.put("generationDate", new Date().toString());
7177
}
7278

7379
@Override

src/com/yanglb/utilitys/codegen/core/generator/impl/MsgCSGeneratorImpl.java

Lines changed: 0 additions & 75 deletions
This file was deleted.

src/com/yanglb/utilitys/codegen/core/generator/impl/MsgGeneratorImpl.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ protected void onGeneration() throws CodeGenException {
4040

4141
// 读取必要的配置数据
4242
ISettingReader settingReader = GenFactory.createByName(SupportGen.setting_reader);
43-
HashMap<String, String> settingMap = settingReader.settingReader();
43+
String genKey = String.format("%s_%s", paramaModel.getType().name(), paramaModel.getLang().name());
44+
HashMap<String, String> map = settingReader.settingReader(genKey);
45+
settingMap.putAll(map);
4446

4547
// 读取DB信息表
4648
ITableReader tableReader = GenFactory.createByName(SupportGen.table_reader);
@@ -64,6 +66,8 @@ protected void onGeneration() throws CodeGenException {
6466
supportTrans = SupportGen.msg_java_translator;
6567
} else if (this.paramaModel.getLang() == SupportLang.json) {
6668
supportTrans = SupportGen.msg_json_translator;
69+
} else if (this.paramaModel.getLang() == SupportLang.cs) {
70+
supportTrans = SupportGen.msg_cs_translator;
6771
}
6872

6973
// 转换为可写入的Model(单个文件)

src/com/yanglb/utilitys/codegen/core/translator/BaseDdlTranslator.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import com.yanglb.utilitys.codegen.support.SupportLang;
2828
import com.yanglb.utilitys.codegen.utility.StringUtility;
2929

30-
public class BaseDdlTranslator extends BaseSqlTranslator<List<DdlModel>> {
30+
public class BaseDdlTranslator extends BaseSqlTranslator<DdlModel> {
3131
// 外键关系列表
3232
protected List<ForeignModel> foreignKeyList = new ArrayList<ForeignModel>();
3333

@@ -193,17 +193,6 @@ protected ForeignDetailModel getForeignByDdlDetail(DdlModel model, DdlDetail det
193193
return null;
194194
}
195195

196-
/**
197-
* 取得用于替换的Model
198-
*/
199-
@Override
200-
protected Object getReplaceModel() {
201-
if(this.model == null || this.model.size() <= 0) return null;
202-
203-
// 只使用第一个Sheet替换
204-
return this.model.get(0);
205-
}
206-
207196
/**
208197
* 根据前缀查找全表名
209198
* @param prefix 前缀

src/com/yanglb/utilitys/codegen/core/translator/BaseMsgTranslator.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,31 @@
1919
import java.util.List;
2020

2121
import com.yanglb.utilitys.codegen.core.model.TableModel;
22+
import com.yanglb.utilitys.codegen.exceptions.CodeGenException;
2223

2324
public class BaseMsgTranslator extends BaseTranslator<List<TableModel>> {
25+
protected String msgLang = "";
26+
27+
@Override
28+
protected void onBeforeTranslate() throws CodeGenException {
29+
super.onBeforeTranslate();
30+
31+
// 当前生成的国际化语言
32+
this.msgLang = this.settingMap.get("MsgLang");
33+
34+
// 文件名
35+
String fileName = getFileName();
36+
if (fileName.equals("")) {
37+
// 空文件名
38+
fileName = this.msgLang;
39+
} else {
40+
if(!this.isDefaultLanguage()) {
41+
fileName = fileName + "." + this.msgLang;
42+
}
43+
}
44+
45+
this.writableModel.setFileName(fileName);
46+
}
2447

2548
/**
2649
* 文件名,优先使用--fn参数指定的文件名,如不指定使用excel名称
@@ -43,4 +66,12 @@ protected String getFileName() {
4366

4467
return fileName;
4568
}
69+
70+
/**
71+
* 获取当前语言是否为默认语言
72+
* @return
73+
*/
74+
protected boolean isDefaultLanguage() {
75+
return this.msgLang.equals("default");
76+
}
4677
}

src/com/yanglb/utilitys/codegen/core/translator/BaseSqlTranslator.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,32 @@
1515
*/
1616
package com.yanglb.utilitys.codegen.core.translator;
1717

18-
public class BaseSqlTranslator<T> extends BaseTranslator<T> {
18+
import java.util.List;
19+
20+
import com.yanglb.utilitys.codegen.exceptions.CodeGenException;
21+
22+
public class BaseSqlTranslator<T> extends BaseTranslator<List<T>> {
1923
// 列引号字符,如:SQL Server为[], MySQL为`等
2024
protected String sqlColumnStart = "\"";
2125
protected String sqlColumnEnd = "\"";
26+
27+
/**
28+
* 取得用于替换的Model
29+
*/
30+
private Object getReplaceModel() {
31+
if(this.model == null || this.model.size() <= 0) return null;
32+
33+
// 只使用第一个Sheet替换
34+
return this.model.get(0);
35+
}
36+
37+
@Override
38+
protected void onAfterTranslate() throws CodeGenException {
39+
super.onAfterTranslate();
40+
41+
// 替换Flag
42+
String data = this.writableModel.getData().toString();
43+
data = this.replaceFlags(data, getReplaceModel());
44+
this.writableModel.setData(new StringBuilder(data));
45+
}
2246
}

src/com/yanglb/utilitys/codegen/core/translator/BaseTranslator.java

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,6 @@ protected void onTranslate() throws CodeGenException{
8585
*/
8686
protected void onAfterTranslate() throws CodeGenException {
8787
// 编码转换在写入器完成
88-
// 标记替换
89-
this.replaceFlags();
90-
91-
// TODO: 代码格式调整(后期完善)
9288
}
9389

9490
// 翻译操作
@@ -99,14 +95,15 @@ private void doTranslate() throws CodeGenException{
9995
}
10096

10197
/**
102-
* 替换{标记}
103-
* 如果
104-
* @throws CodeGenException
98+
* 模板替换
99+
* @param template 模板内容
100+
* @param replaceModel
101+
* @return
102+
* @throws CodeGenException
105103
*/
106-
private void replaceFlags() throws CodeGenException {
107-
Object replaceModel = this.getReplaceModel();
108-
List<String> flags = StringUtility.findFlags(this.writableModel.getData());
109-
String data = this.writableModel.getData().toString();
104+
protected String replaceFlags(String template, Object replaceModel) throws CodeGenException {
105+
List<String> flags = StringUtility.findFlags(template);
106+
String data = new String(template);
110107
for(String key:flags) {
111108
// Map元素
112109
String value = null;
@@ -142,14 +139,6 @@ private void replaceFlags() throws CodeGenException {
142139
data = data.replaceAll(f, value);
143140
}
144141
}
145-
this.writableModel.setData(new StringBuilder(data));
146-
}
147-
148-
/**
149-
* 取得用于替换的Model
150-
* @return
151-
*/
152-
protected Object getReplaceModel() {
153-
return this.model;
142+
return data;
154143
}
155144
}

src/com/yanglb/utilitys/codegen/core/translator/impl/DdlMysqlTranslatorImpl.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,6 @@ protected void onTranslate() throws CodeGenException {
7777
sb.append(this.genForeignKey());
7878
}
7979

80-
/**
81-
* 取得用于替换的Model
82-
*/
83-
@Override
84-
protected Object getReplaceModel() {
85-
if (this.model == null || this.model.size() <= 0)
86-
return null;
87-
88-
// 只使用第一个Sheet替换
89-
return this.model.get(0);
90-
}
91-
9280
/**
9381
* 生成MySQL的DDL
9482
*

src/com/yanglb/utilitys/codegen/core/translator/impl/DdlSqliteTranslatorImpl.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,6 @@ protected void onTranslate() throws CodeGenException {
6060
sb.append(this.genDdl(itm));
6161
}
6262
}
63-
64-
/**
65-
* 取得用于替换的Model
66-
*/
67-
@Override
68-
protected Object getReplaceModel() {
69-
if(this.model == null || this.model.size() <= 0) return null;
70-
71-
// 只使用第一个Sheet替换
72-
return this.model.get(0);
73-
}
7463

7564
/**
7665
* 生成MySQL的DDL

src/com/yanglb/utilitys/codegen/core/translator/impl/DmlTranslatorImpl.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import com.yanglb.utilitys.codegen.support.SupportLang;
2727
import com.yanglb.utilitys.codegen.utility.StringUtility;
2828

29-
public class DmlTranslatorImpl extends BaseSqlTranslator<List<DmlModel>> {
29+
public class DmlTranslatorImpl extends BaseSqlTranslator<DmlModel> {
3030
@Override
3131
protected void onBeforeTranslate() throws CodeGenException {
3232
super.onBeforeTranslate();
@@ -68,17 +68,6 @@ protected void onTranslate() throws CodeGenException {
6868
}
6969
}
7070

71-
/**
72-
* 取得用于替换的Model
73-
*/
74-
@Override
75-
protected Object getReplaceModel() {
76-
if(this.model == null || this.model.size() <= 0) return null;
77-
78-
// 只使用第一个Sheet替换
79-
return this.model.get(0);
80-
}
81-
8271
private String genDml(DmlModel model) {
8372
StringBuilder sb = new StringBuilder();
8473

0 commit comments

Comments
 (0)