Skip to content

Commit 5e536c1

Browse files
author
杨利兵
committed
添加json格式多语言输出
1 parent 7399f0f commit 5e536c1

File tree

6 files changed

+132
-3
lines changed

6 files changed

+132
-3
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ protected void onGeneration() throws CodeGenException {
6161
SupportGen supportTrans = SupportGen.msg_js_translator;
6262
SupportGen supportWriter = SupportGen.utf8_writer;
6363
if(this.paramaModel.getLang() == SupportLang.java) {
64-
// TODO: 暂时使用UTF-8写入器
65-
// supportWriter = SupportGen.ascii_writer;
6664
supportTrans = SupportGen.msg_java_translator;
65+
} else if (this.paramaModel.getLang() == SupportLang.json) {
66+
supportTrans = SupportGen.msg_json_translator;
6767
}
6868

6969
// 转换为可写入的Model(单个文件)
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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.List;
19+
import java.util.Map;
20+
21+
import com.yanglb.utilitys.codegen.core.model.TableModel;
22+
import com.yanglb.utilitys.codegen.core.translator.BaseTranslator;
23+
import com.yanglb.utilitys.codegen.exceptions.CodeGenException;
24+
import com.yanglb.utilitys.codegen.utility.StringUtility;
25+
26+
public class MsgJsonTranslatorImpl extends BaseTranslator<List<TableModel>> {
27+
protected String msgLang = "";
28+
29+
@Override
30+
protected void onBeforeTranslate() throws CodeGenException {
31+
super.onBeforeTranslate();
32+
33+
// 当前生成的国际化语言
34+
this.msgLang = this.settingMap.get("MsgLang");
35+
36+
// 可指定文件名
37+
String fileName = this.paramaModel.getOptions().get("fn");
38+
if(fileName == null || fileName.equals("")){
39+
fileName = "message";
40+
}
41+
this.writableModel.setExtension("json");
42+
this.writableModel.setFileName(fileName);
43+
if(!this.msgLang.equals("default")) {
44+
this.writableModel.setFileName(fileName + "." + this.msgLang);
45+
}
46+
this.writableModel.setFilePath("msg/json");
47+
}
48+
49+
@Override
50+
protected void onTranslate() throws CodeGenException {
51+
super.onTranslate();
52+
StringBuilder sb = this.writableModel.getData();
53+
sb.append("{\r\n");
54+
55+
// 分组输出
56+
if(this.paramaModel.getOptions().get("group") != null) {
57+
for(TableModel tblModel : this.model) {
58+
// 如果没有Sheet名或Sheet名被#注释则添加到Root中
59+
String sheetName = tblModel.getSheetName();
60+
if(sheetName==null || sheetName.equals("") || sheetName.startsWith("@")) {
61+
for(Map<String, String> itm : tblModel.toList()) {
62+
String id = itm.get("id");
63+
if(StringUtility.isNullOrEmpty(id)) continue;
64+
// 对字符串进行转换
65+
String value = this.convert2JsCode(itm.get(this.msgLang));
66+
sb.append(String.format(" \"%\"s: \"%s\",\r\n", id, value));
67+
}
68+
} else {
69+
sb.append(String.format(" \"%s\": {\r\n", tblModel.getSheetName()));
70+
for(Map<String, String> itm : tblModel.toList()) {
71+
String id = itm.get("id");
72+
if(StringUtility.isNullOrEmpty(id)) continue;
73+
// 对字符串进行转换
74+
String value = this.convert2JsCode(itm.get(this.msgLang));
75+
sb.append(String.format(" \"%s\": \"%s\",\r\n", id, value));
76+
}
77+
int idx = sb.lastIndexOf(",");
78+
if(idx != -1) {
79+
sb.deleteCharAt(idx);
80+
}
81+
82+
sb.append(" },\r\n");
83+
}
84+
}
85+
86+
int idx = sb.lastIndexOf(",");
87+
if(idx != -1) {
88+
sb.deleteCharAt(idx);
89+
}
90+
} else {
91+
// 合并输出
92+
for(TableModel tblModel : this.model) {
93+
for(Map<String, String> itm : tblModel.toList()) {
94+
String id = itm.get("id");
95+
if(StringUtility.isNullOrEmpty(id)) continue;
96+
// 对字符串进行转换
97+
String value = this.convert2JsCode(itm.get(this.msgLang));
98+
sb.append(String.format(" \"%s\": \"%s\",\r\n", id, value));
99+
}
100+
}
101+
102+
int idx = sb.lastIndexOf(",");
103+
if(idx != -1) {
104+
sb.deleteCharAt(idx);
105+
}
106+
}
107+
sb.append("}");
108+
}
109+
110+
private String convert2JsCode(String value) {
111+
if(value == null) return null;
112+
113+
// 先替换\r\n,防止有文档只有\r或\n 后面再替换一次
114+
value = value.replaceAll("\r\n", "\\\\r\\\\n");
115+
value = value.replaceAll("\r", "\\\\r\\\\n");
116+
value = value.replaceAll("\n", "\\\\r\\\\n");
117+
return value;
118+
}
119+
120+
@Override
121+
protected void onAfterTranslate() throws CodeGenException {
122+
// super.onAfterTranslate();
123+
// 不需要替换标记
124+
}
125+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public boolean invoke(String[] args) {
8181
* 显示帮助信息
8282
*/
8383
private void showHelp() {
84-
System.out.println("代码生成器 v2.0.3 使用说明");
84+
System.out.println("代码生成器 v2.1.0 使用说明");
8585
System.out.println("Copyright 2015-2019 yanglb.com All Rights Reserved.");
8686
System.out.println();
8787
System.out.println("用法:");

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public enum SupportGen {
2929
ddl_sqlserver_translator, // com.yanglb.utilitys.codegen.core.translator.impl.DdlSqlServerTranslatorImpl
3030
dml_translator, // com.yanglb.utilitys.codegen.core.translator.impl.DmlTranslatorImpl
3131
msg_js_translator, // com.yanglb.utilitys.codegen.core.translator.impl.MsgJsTranslatorImpl
32+
msg_json_translator, // com.yanglb.utilitys.codegen.core.translator.impl.MsgJsonTranslatorImpl
3233
msg_java_translator, // com.yanglb.utilitys.codegen.core.translator.impl.MsgJavaTranslatorImpl
3334
po_cs_translator, // com.yanglb.utilitys.codegen.core.translator.impl.PoCSharpTranslatorImpl
3435

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ public enum SupportLang {
2525
cs,
2626
sql,
2727
js,
28+
json
2829
}

src/conf/conf.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ po_cs=com.yanglb.utilitys.codegen.core.generator.impl.PoCSharpGeneratorImpl
2929

3030
# message generator
3131
msg_js=com.yanglb.utilitys.codegen.core.generator.impl.MsgGeneratorImpl
32+
msg_json=com.yanglb.utilitys.codegen.core.generator.impl.MsgGeneratorImpl
3233
msg_java=com.yanglb.utilitys.codegen.core.generator.impl.MsgGeneratorImpl
3334

3435

@@ -45,6 +46,7 @@ ddl_sqlite_translator=com.yanglb.utilitys.codegen.core.translator.impl.DdlSqlite
4546
ddl_sqlserver_translator=com.yanglb.utilitys.codegen.core.translator.impl.DdlSqlServerTranslatorImpl
4647
dml_translator=com.yanglb.utilitys.codegen.core.translator.impl.DmlTranslatorImpl
4748
msg_js_translator=com.yanglb.utilitys.codegen.core.translator.impl.MsgJsTranslatorImpl
49+
msg_json_translator=com.yanglb.utilitys.codegen.core.translator.impl.MsgJsonTranslatorImpl
4850
msg_java_translator=com.yanglb.utilitys.codegen.core.translator.impl.MsgJavaTranslatorImpl
4951
po_cs_translator=com.yanglb.utilitys.codegen.core.translator.impl.PoCSharpTranslatorImpl
5052

0 commit comments

Comments
 (0)