Skip to content

Commit 3b7c123

Browse files
author
杨利兵
committed
添加csharp resx文件
1 parent 45fe159 commit 3b7c123

File tree

8 files changed

+195
-4
lines changed

8 files changed

+195
-4
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* Copyright 2015 yanglb.com
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.yanglb.utilitys.codegen.core.generator.impl;
17+
18+
19+
import java.util.ArrayList;
20+
import java.util.HashMap;
21+
import java.util.List;
22+
23+
import com.yanglb.utilitys.codegen.core.GenFactory;
24+
import com.yanglb.utilitys.codegen.core.generator.BaseGenerator;
25+
import com.yanglb.utilitys.codegen.core.model.TableModel;
26+
import com.yanglb.utilitys.codegen.core.model.WritableModel;
27+
import com.yanglb.utilitys.codegen.core.reader.ISettingReader;
28+
import com.yanglb.utilitys.codegen.core.reader.ITableReader;
29+
import com.yanglb.utilitys.codegen.core.translator.ITranslator;
30+
import com.yanglb.utilitys.codegen.core.writer.IWriter;
31+
import com.yanglb.utilitys.codegen.exceptions.CodeGenException;
32+
import com.yanglb.utilitys.codegen.support.SupportGen;
33+
import com.yanglb.utilitys.codegen.utility.MsgUtility;
34+
35+
public class MsgCSGeneratorImpl extends BaseGenerator {
36+
@Override
37+
protected void onGeneration() throws CodeGenException {
38+
super.onGeneration();
39+
40+
// 读取必要的配置数据
41+
ISettingReader settingReader = GenFactory.createByName(SupportGen.setting_reader);
42+
String genKey = String.format("%s_%s", paramaModel.getType().name(), paramaModel.getLang().name());
43+
HashMap<String, String> settingMap = settingReader.settingReader(genKey);
44+
45+
// 读取DB信息表
46+
ITableReader tableReader = GenFactory.createByName(SupportGen.table_reader);
47+
tableReader.setStartPoint(3, 2);
48+
List<TableModel> list = tableReader.reader(this.paramaModel.getIn(), this.paramaModel.getSheets());
49+
if(list.size() == 0) {
50+
throw new CodeGenException(MsgUtility.getString("E_003"));
51+
}
52+
53+
// 获取语言(每种语言翻译一次)
54+
List<String> langList = new ArrayList<String>();
55+
TableModel tableModel = list.get(0);
56+
for(String key : tableModel.getColumns()) {
57+
if(!"id".equals(key)) {
58+
langList.add(key);
59+
60+
settingMap.put("MsgLang", key);
61+
SupportGen supportTrans = SupportGen.msg_cs_translator;
62+
SupportGen supportWriter = SupportGen.utf8_writer;
63+
64+
// 转换为可写入的Model(单个文件)
65+
ITranslator<List<TableModel>> translator = GenFactory.createByName(supportTrans);
66+
WritableModel writableModel = translator.translate(settingMap, this.paramaModel, list);
67+
68+
// 写入到文件中
69+
IWriter writer = GenFactory.createByName(supportWriter);
70+
writer.writer(writableModel);
71+
}
72+
}
73+
}
74+
75+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ private void replaceFlags() throws CodeGenException {
142142

143143
// 如果有值则替换
144144
if(hasKey) {
145-
data = data.replaceAll(String.format("\\{%s\\}", key), value);
145+
String f = String.format("\\$\\{%s\\}", key);
146+
data = data.replaceAll(f, value);
146147
}
147148
}
148149
this.writableModel.setData(new StringBuilder(data));
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/**
2+
* Copyright 2015 yanglb.com
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.yanglb.utilitys.codegen.core.translator.impl;
17+
18+
import java.util.Map;
19+
20+
import com.yanglb.utilitys.codegen.core.model.TableModel;
21+
import com.yanglb.utilitys.codegen.core.translator.BaseMsgTranslator;
22+
import com.yanglb.utilitys.codegen.exceptions.CodeGenException;
23+
import com.yanglb.utilitys.codegen.utility.StringUtility;
24+
25+
public class MsgCSTranslatorImpl extends BaseMsgTranslator {
26+
protected String msgLang = "";
27+
28+
@Override
29+
protected void onBeforeTranslate() throws CodeGenException {
30+
super.onBeforeTranslate();
31+
32+
// 当前生成的国际化语言
33+
this.msgLang = this.settingMap.get("MsgLang");
34+
35+
// 文件名
36+
String fileName = getFileName();
37+
if (fileName.equals("")) {
38+
// 空文件名
39+
fileName = this.msgLang;
40+
} else {
41+
if(!this.msgLang.equals("default")) {
42+
fileName = fileName + "." + this.msgLang;
43+
}
44+
}
45+
46+
this.writableModel.setFileName(fileName);
47+
this.writableModel.setExtension("resx");
48+
this.writableModel.setFilePath("");
49+
}
50+
51+
@Override
52+
protected void onTranslate() throws CodeGenException {
53+
super.onTranslate();
54+
StringBuilder sb = this.writableModel.getData();
55+
sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" +
56+
// this.genHeader() +
57+
this.settingMap.get("head") +
58+
"<root>\r\n");
59+
60+
for(TableModel tblModel : this.model) {
61+
// 添加Sheet注释
62+
sb.append(String.format(" \r\n"));
63+
sb.append(String.format(" <!-- %s -->\r\n", tblModel.getSheetName()));
64+
65+
for(Map<String, String> itm : tblModel.toList()) {
66+
String id = itm.get("id");
67+
if(StringUtility.isNullOrEmpty(id)) continue;
68+
69+
// 对字符串进行转换
70+
String value = this.convert2CSCode(itm.get(this.msgLang));
71+
sb.append(String.format(" <data name=\"%s\">\r\n", id));
72+
sb.append(String.format(" <value>%s</value>\r\n", value));
73+
sb.append(String.format(" </data>\r\n"));
74+
}
75+
}
76+
77+
int idx = sb.lastIndexOf(",");
78+
if(idx != -1) {
79+
sb.deleteCharAt(idx);
80+
}
81+
sb.append("</root>\r\n");
82+
}
83+
84+
private String genHeader() {
85+
StringBuilder sb = new StringBuilder();
86+
sb.append(String.format("<!--\r\n"));
87+
sb.append(String.format(" %s\r\n", this.settingMap.get("generatorCopyright")));
88+
sb.append(String.format(" Auto generated by %s v%s\r\n", this.settingMap.get("generatorName"), this.settingMap.get("generatorVersion")));
89+
sb.append(String.format(" %s\r\n", this.model.get(0).getGenerationDate()));
90+
// sb.append(String.format(" {generationDate}\r\n"));
91+
sb.append(String.format(" %s\r\n", this.settingMap.get("generatorRepository")));
92+
sb.append(String.format("-->\r\n"));
93+
return sb.toString();
94+
}
95+
96+
private String convert2CSCode(String value) {
97+
if(value == null) return null;
98+
99+
// 先替换\r\n,防止有文档只有\r或\n 后面再替换一次
100+
value = value.replaceAll("\r\n", "\\\\r\\\\n");
101+
value = value.replaceAll("\r", "\\\\r\\\\n");
102+
value = value.replaceAll("\n", "\\\\r\\\\n");
103+
return value;
104+
}
105+
106+
// @Override
107+
// protected void onAfterTranslate() throws CodeGenException {
108+
//// super.onAfterTranslate();
109+
// // 不需要替换标记
110+
// }
111+
}

src/com/yanglb/utilitys/codegen/shell/CodeGenShell.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ public boolean invoke(String[] args) {
8181
* 显示帮助信息
8282
*/
8383
private void showHelp() {
84-
System.out.println("代码生成器 v2.3.0 使用说明");
85-
System.out.println("Copyright 2015-2019 yanglb.com All Rights Reserved.");
84+
System.out.println("代码生成器 v3.0.0 使用说明");
85+
System.out.println("Copyright 2015-2020 yanglb.com All Rights Reserved.");
8686
System.out.println();
8787
System.out.println("用法:");
8888
System.out.println("cg -type 生成类型 -lang 生成语言 -in 输入文件 [-sheets 要生成的Sheet名1[,2]] [-out 输出目录]");

src/com/yanglb/utilitys/codegen/support/SupportGen.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public enum SupportGen {
3131
msg_js_translator, // com.yanglb.utilitys.codegen.core.translator.impl.MsgJsTranslatorImpl
3232
msg_json_translator, // com.yanglb.utilitys.codegen.core.translator.impl.MsgJsonTranslatorImpl
3333
msg_java_translator, // com.yanglb.utilitys.codegen.core.translator.impl.MsgJavaTranslatorImpl
34+
msg_cs_translator,
3435

3536
// writer
3637
utf8_writer, // com.yanglb.utilitys.codegen.core.writer.impl.Utf8WriterImpl

src/com/yanglb/utilitys/codegen/support/SupportLang.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ public enum SupportLang {
2424
java,
2525
sql,
2626
js,
27-
json
27+
json,
28+
cs
2829
}

src/conf/CodeGenerator.xlsx

2.13 KB
Binary file not shown.

src/conf/conf.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ dml_sql=com.yanglb.utilitys.codegen.core.generator.impl.DmlGeneratorImpl
2626
msg_js=com.yanglb.utilitys.codegen.core.generator.impl.MsgGeneratorImpl
2727
msg_json=com.yanglb.utilitys.codegen.core.generator.impl.MsgGeneratorImpl
2828
msg_java=com.yanglb.utilitys.codegen.core.generator.impl.MsgGeneratorImpl
29+
msg_cs=com.yanglb.utilitys.codegen.core.generator.impl.MsgCSGeneratorImpl
2930

3031
# reader
3132
ddl_reader=com.yanglb.utilitys.codegen.core.reader.impl.DdlReaderImpl
@@ -42,6 +43,7 @@ dml_translator=com.yanglb.utilitys.codegen.core.translator.impl.DmlTranslatorImp
4243
msg_js_translator=com.yanglb.utilitys.codegen.core.translator.impl.MsgJsTranslatorImpl
4344
msg_json_translator=com.yanglb.utilitys.codegen.core.translator.impl.MsgJsonTranslatorImpl
4445
msg_java_translator=com.yanglb.utilitys.codegen.core.translator.impl.MsgJavaTranslatorImpl
46+
msg_cs_translator=com.yanglb.utilitys.codegen.core.translator.impl.MsgCSTranslatorImpl
4547
po_cs_translator=com.yanglb.utilitys.codegen.core.translator.impl.PoCSharpTranslatorImpl
4648

4749
# writer

0 commit comments

Comments
 (0)