Skip to content

Commit 4b83fac

Browse files
author
杨利兵
committed
添加Android 资源文件生成功能
1 parent 92709bd commit 4b83fac

File tree

6 files changed

+106
-1
lines changed

6 files changed

+106
-1
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ protected void onGeneration() throws CodeGenException {
7070
supportTrans = SupportGen.msg_cs_translator;
7171
} else if (this.paramaModel.getLang() == SupportLang.ios) {
7272
supportTrans = SupportGen.msg_ios_translator;
73+
} else if (this.paramaModel.getLang() == SupportLang.android) {
74+
supportTrans = SupportGen.msg_android_translator;
7375
}
7476

7577
// 转换为可写入的Model(单个文件)
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* Copyright 2020 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.ArrayList;
19+
import java.util.HashMap;
20+
import java.util.List;
21+
import java.util.Map;
22+
23+
import com.yanglb.utilitys.codegen.core.model.TableModel;
24+
import com.yanglb.utilitys.codegen.core.translator.BaseMsgTranslator;
25+
import com.yanglb.utilitys.codegen.exceptions.CodeGenException;
26+
import com.yanglb.utilitys.codegen.utility.MsgUtility;
27+
import com.yanglb.utilitys.codegen.utility.StringUtility;
28+
29+
public class MsgAndroidTranslatorImpl extends BaseMsgTranslator {
30+
@Override
31+
protected void onBeforeTranslate() throws CodeGenException {
32+
super.onBeforeTranslate();
33+
34+
this.writableModel.setExtension("xml");
35+
String path = (this.isDefaultLanguage() ? "" : this.getSplitString() + this.msgLang);
36+
this.writableModel.setFilePath("msg/android/values" + path);
37+
38+
// 文件名
39+
this.writableModel.setFileName("strings");
40+
}
41+
42+
@Override
43+
protected void onTranslate() throws CodeGenException {
44+
super.onTranslate();
45+
StringBuilder sb = this.writableModel.getData();
46+
sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" +
47+
this.settingMap.get("head") +
48+
"<resources>\r\n");
49+
50+
// 替换标记
51+
String s = this.replaceFlags(sb.toString(), null);
52+
sb = new StringBuilder(s);
53+
54+
// 用于检查相同的key
55+
Map<String, Boolean> keys = new HashMap<String, Boolean>();
56+
Map<String, List<String>> arrays = new HashMap<String, List<String>>();
57+
for(TableModel tblModel : this.model) {
58+
for(Map<String, String> itm : tblModel.toList()) {
59+
String id = itm.get("id");
60+
if(StringUtility.isNullOrEmpty(id)) continue;
61+
if (!arrays.containsKey(id)) {
62+
arrays.put(id, new ArrayList<String>());
63+
}
64+
65+
// 对字符串进行转换
66+
String value = this.convert2CSCode(itm.get(this.msgLang));
67+
List<String> items = arrays.get(id);
68+
items.add(value);
69+
}
70+
}
71+
72+
for(String key:arrays.keySet()) {
73+
List<String> items = arrays.get(key);
74+
if (items.size() > 1) {
75+
// list
76+
sb.append(String.format(" <string-array name=\"%s\">\r\n", key));
77+
for(String value:items) {
78+
sb.append(String.format(" <item>%s</item>\r\n", value));
79+
}
80+
sb.append(" </string-array>\r\n");
81+
} else {
82+
sb.append(String.format(" <string name=\"%s\">%s</string>\r\n", key, items.get(0)));
83+
}
84+
}
85+
86+
sb.append("</resources>\r\n");
87+
88+
this.writableModel.setData(sb);
89+
}
90+
91+
private String convert2CSCode(String value) {
92+
if(value == null) return null;
93+
94+
// 先替换\r\n,防止有文档只有\r或\n 后面再替换一次
95+
value = value.replaceAll("\r\n", "\\\\r\\\\n");
96+
value = value.replaceAll("\r", "\\\\r\\\\n");
97+
value = value.replaceAll("\n", "\\\\r\\\\n");
98+
return value;
99+
}
100+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public enum SupportGen {
3333
msg_java_translator,
3434
msg_cs_translator,
3535
msg_ios_translator,
36+
msg_android_translator,
3637

3738
// writer
3839
utf8_writer,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ public enum SupportLang {
2626
js,
2727
json,
2828
cs,
29-
ios
29+
ios,
30+
android
3031
}

src/conf/CodeGenerator.xlsx

1.23 KB
Binary file not shown.

src/conf/conf.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ msg_json_translator=com.yanglb.utilitys.codegen.core.translator.impl.MsgJsonTran
4747
msg_java_translator=com.yanglb.utilitys.codegen.core.translator.impl.MsgJavaTranslatorImpl
4848
msg_cs_translator=com.yanglb.utilitys.codegen.core.translator.impl.MsgCSTranslatorImpl
4949
msg_ios_translator=com.yanglb.utilitys.codegen.core.translator.impl.MsgIOSTranslatorImpl
50+
msg_android_translator=com.yanglb.utilitys.codegen.core.translator.impl.MsgAndroidTranslatorImpl
5051
po_cs_translator=com.yanglb.utilitys.codegen.core.translator.impl.PoCSharpTranslatorImpl
5152

5253
# writer

0 commit comments

Comments
 (0)