Multiple strings.xml files cause order and incomplete problems in android
2016-04-19 12:17
591 查看
there is a boring task, boss let me check multiple strings.xml files to find the differences compare to res/values/strings.xml. Jesus christ! It's 33 kinds of languages in android. I do not want work like a
idiot. so, I wrote a program to solve that stupid task.
here is my code.
I'm fish, I'm on.
idiot. so, I wrote a program to solve that stupid task.
here is my code.
package com.jack.filter; import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.FileVisitResult; import java.nio.file.FileVisitor; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /*********************************V_I_2016-4-19 12:06:35************************************************************ * 不能在安卓项目中使用!!!! * 思路: * 给定一个res路径,遍历读取以values-开头的目录存到集合中 * 读取目录下的strings.xml文件,将数据转为键值对HashMap<String,String> source---("XXX","<string name="XXX">ZZZZ</string>"); * 遍历values/strings.xml默认文件,逐行读取String readLine,同时声明一个List<String> tem; * 如果该行为<string name="XXX">ZZZZ</string>则提取键XXX,如果匹配到source则tem.add(source.get(key)); * 如果该行没有key(注释)或者XML文件头,或者没有匹配的则直接tem.add(readLine); * * 注意事项: * 不能在安卓项目中使用! 需要新建一个Java project运行,导入的包全为java.nio.* 下的子类 * 如果报错则添加JRE Library * *****************************V_II_2016-5-10 9:45:04**************************************************************** * * V_II : 修改BUG(1.string有多行) 新加非匹配型(有些其他语言不需要,只使用一个默认即可) * * * ****************************V_III_2016-8-9 10:26:04**************************************************************** * V_III : 修改BUG(1.注释的顺序放错了) * * @author jack * last-modify : 2016-8-9 10:26:04 */ public class StringsFileUtil { /** * @param resFileDir = E:\\workspace\\TimeSheet\\res * @param defaultFilePath = E:\\workspace\\TimeSheet\\res\\values\\strings.xml * @param unchangeKey 不需要多语言的项 * @throws IOException */ public void tidyFiles(String resFileDir, String defaultFilePath, final List<String> unchangeKey) throws IOException { final Path path = Paths.get(resFileDir); final Path dataPath = Paths.get(defaultFilePath); final List<String> sequence = new ArrayList<>(); final Map<String, Item> data = prepareData(dataPath, sequence); Path absPath = path.toAbsolutePath(); Files.walkFileTree(absPath, new FileVisitor<Path>() { @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { return FileVisitResult.CONTINUE; } @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { if (dir.getName(dir.getNameCount() - 1).toString().startsWith("values-")) { return FileVisitResult.CONTINUE; } else if (dir.equals(path)) { return FileVisitResult.CONTINUE; } return FileVisitResult.SKIP_SUBTREE; } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (file.toString().endsWith("strings.xml")) { new TransferThread(sequence, data, file, unchangeKey).start(); } return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { return FileVisitResult.CONTINUE; } }); } public Map<String, Item> prepareData(Path path, List<String> sequence) throws IOException { List<String> lines = Files.readAllLines(path, Charset.forName("utf-8")); String key = null; Item value = null; int i = 1; List<String> values = null; Map<String, Item> result = new HashMap<>(); for (String line : lines) { boolean isStart = line.contains("<string name="); boolean isEnd = line.contains("</string>"); if (!isStart && !isEnd) { if (values != null) { values.add(line); continue; } else { key = "jack" + i++; value = new Item(); value.value = line; } } else if (isStart && isEnd) { key = line.split("\"")[1]; value = new Item(); value.value = line; } else if (isStart && !isEnd) { key = line.split("\"")[1]; value = new Item(); values = new ArrayList<>(); values.add(line); continue; } else if (!isStart && isEnd) { if (value != null && key != null) { values.add(line); value.values = values; } } if(sequence!=null) sequence.add(key); result.put(key, value); key = null; value = null; values = null; } return result; } class Item { String value; //single line List<String> values; //multiply lines public Item() { value = null; values = null; } } class TransferThread extends Thread { private List<String> sequence; private Map<String, Item> data; private Path path; private List<String> unchangeKey; public TransferThread(List<String> sequence, Map<String, Item> data, Path path, List<String> unchangeKey) { this.sequence = sequence; this.data = data; this.path = path; this.unchangeKey = unchangeKey; } @Override public void run() { Map<String, Item> source; try { source = prepareData(path, null); List<String> tem = new ArrayList<>(); for (int i=0;i<sequence.size();i++){ String key = sequence.get(i); if(unchangeKey!=null && unchangeKey.contains(key)) continue; if(key.startsWith("jack")) addToList(tem, data.get(key)); else addToList(tem, source.get(key)); } Files.write(path, tem, Charset.forName("utf-8")); } catch (IOException e) { e.printStackTrace(); } System.out.println("------finish-----" + path); } private void addToList(List<String> tem, Item item) { if (item.value != null ) { tem.add(item.value); } else if(item.values!=null){ tem.addAll(item.values); } } } public static void main(String[] args) throws IOException { String resFileDir = "E:\\workspace_temp\\WnOPOS_V1\\app\\src\\main\\res"; String defaultFilePath = "E:\\workspace_temp\\WnOPOS_V1\\app\\src\\main\\res\\values\\strings.xml"; List<String> unchangeKey = new ArrayList<>(); unchangeKey.add("app_name"); unchangeKey.add("admin"); new StringsFileUtil().tidyFiles(resFileDir, defaultFilePath,unchangeKey); } }
I'm fish, I'm on.
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories