本文主要是介绍android的strings整理脚本,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
统一对String整理的工具,结构如下
代码
package com.owant.toollib;import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;/*** Created by owant on 8/16/16.* 对Activity,layout的文件应用String资源的整理工具* 使得String统一,而且结构清晰*/
public class ValuesStringsUtil {public static String activityPath = "F:\\MyWorkAS\\NewHome\\app\\src\\main\\java\\com\\xinlianfeng\\android\\livehome\\activity\\DehumidifierSettingActivity.java";public final static String layoutParentPath = "F:\\MyWorkAS\\NewHome\\app\\src\\main\\res\\layout\\";public final static String valuesStringsPath = "F:\\MyWorkAS\\NewHome\\app\\src\\main\\res\\values\\strings.xml";public static List<String> stringIds = new ArrayList<>();public static TreeSet<String> stringSet = new TreeSet<>();public static void main(String[] arg) {businessOnActivity(activityPath);}private static void businessOnActivity(String activityPath) {stringIds.clear();stringSet.clear();//获取Activity的StringString fileContext = getFileContext(activityPath);getActivityStrings(fileContext);//获取Activity的Layout文件List<String> layoutFileNames = getLayoutFileNames(fileContext);if (layoutFileNames.size() > 0) {for (String lf : layoutFileNames) {String layoutFilePath = layoutParentPath + lf + ".xml";
// System.out.println("lf:" + lf);System.out.println("layout_file_path:\t" + layoutFilePath);getLayoutStrings(getFileContext(layoutFilePath));}}String stringContext = getFileContext(valuesStringsPath);for (String key : stringIds) {String stringValue = getStringValue(stringContext, key);if (stringSet.add(stringValue)) {System.out.println(stringValue);}}}private static String getFileContext(String path) {StringBuffer context = new StringBuffer();try {BufferedReader reader = new BufferedReader(new FileReader(new File(path)));byte[] buffer = new byte[1024];String value;while ((value = reader.readLine()) != null) {context.append(value);}} catch (Exception e) {e.printStackTrace();}return context.toString();}private static void getActivityStrings(String context) {//查找activity中的String//getString(R.string.app_name);String pattern = "\\(R\\.string\\.(.+?)\\)";Pattern r = Pattern.compile(pattern);Matcher m = r.matcher(context);while (m.find()) {stringIds.add(m.group(1));}getLayoutFileNames(context);}private static List<String> getLayoutFileNames(String context) {List<String> layoutFileNames = new ArrayList<>();//查找activity中包括的Layout文件// setContentView(R.layout.activity_test_main);//inflate(R.layout.purify_error_dialog_layout, null);
// String formatLayout = "\\(R\\.layout\\.(.+?)\\)";String formatLayout = "R\\.layout\\.(.+?)[;,)]";Pattern layoutPattern = Pattern.compile(formatLayout);Matcher layoutMatcher = layoutPattern.matcher(context);while (layoutMatcher.find()) {String groupText = layoutMatcher.group(1);
// if (groupText.indexOf(",") > 0) {
// groupText = groupText.substring(0, groupText.indexOf(","));
// }layoutFileNames.add(groupText);}return layoutFileNames;}private static void getLayoutStrings(String context) {//"@string/tab_contorl"String pattern = "\"@string/(.+?)\"";Pattern r = Pattern.compile(pattern);Matcher m = r.matcher(context);while (m.find()) {stringIds.add(m.group(1));}}private static String getStringValue(String context, String key) {// <string name="app_name">Base</string>String value = "";String format = "<string name=\"{0}\">(.+?)</string>";format = format.replace("{0}", key);Pattern pattern = Pattern.compile(format);Matcher matcher = pattern.matcher(context);while (matcher.find()) {value = matcher.group(0);}return value;}}
运行后得到
这篇关于android的strings整理脚本的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!