package www.file;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
/**
* 扫描layout以及java用到的color,并将其复制到color.xml下
*
* @author zhongwr 注:重命名既是源文件,先手动备份好源文件再重命名,以免出错找不到源文件
*/
public class ColorRefereceUtil {
/** 要删除资源名所在的源文件color.xml对应的所有行号集合 */
public static List<Integer> colorDelLineLists = new ArrayList<Integer>();
// test path:将原文件strings.xml拷贝到目录D:\\copystylewrite\\,然后在执行
public static String srcColorAbsoluteFilePath = "D:\\copycolorwrite\\color.xml";
public static String targetColorAbsoluteFilePath = "D:\\copycolorwriteTarget\\color.xml";
// public static String srcStyleAbsoluteFilePath =
// "D:\\soft\\studio_project\\PPTest\\app\\src\\main\\res\\values\\color.xml";
// public static String targetStyleAbsoluteFilePath =
// "D:\\soft\\studio_project\\PPTest\\lib_preg_home\\src\\main\\res\\values\\color.xml";
/**
* 查找并移动所有文件 zhongwr 下午3:02:50 void
*/
public static void moveAllFile(String fileDir) {
boolean isJavaDir = true;
if (fileDir.contains("\\res\\layout")) {
isJavaDir = false;
}
File f = new File(fileDir);
if (f != null) {
if (f.isDirectory()) {
File[] fileArray = f.listFiles();
if (fileArray != null) {
for (int i = 0; i < fileArray.length; i++) {
// 递归调用
moveAllFile(fileArray[i].getAbsolutePath());
}
}
} else {// 操作所有
if (isJavaDir) {
executeJavaFile(f);
} else {
executeLayoutFile(f);
}
}
}
}
/**
* 执行Java中图片 下午7:53:04 void
*/
private static void executeJavaFile(File f) {
List<String> colorNameList = getJavaColorName(f.getAbsolutePath());
if (null != colorNameList && !colorNameList.isEmpty()) {
// System.out.println("colorNameList = "+colorNameList);
int size = colorNameList.size();
for (int i = 0; i < size; i++) {
String colorName = colorNameList.get(i);
if (null != colorName) {
// 查找color
List<Integer> colorLineList = getSrcColorLines(srcColorAbsoluteFilePath, colorName.trim());
// System.out.println("colorLineList = " + colorLineList.toString());
if (null != colorLineList && !colorLineList.isEmpty()) {
if (!colorDelLineLists.contains(colorLineList.get(0))) {// 去重:如果曾经写进去,就不用再去写进去了
colorDelLineLists.addAll(colorLineList);
// 不管目标文件存不存在资源名都要删除源文件的,因为要都会写入
boolean isColorNameExist = isColorNameExist(colorName, targetColorAbsoluteFilePath);
System.out.println("isColorNameExist = " + isColorNameExist);
if (!isColorNameExist) {
writeColorFile(colorLineList, srcColorAbsoluteFilePath, targetColorAbsoluteFilePath);
}
}
}
}
}
}
}
/**
* 判断目标文件中是否已存在要写入的color
*
* 下午7:56:51 boolean
*
* @param targetColorName
* @param targetColorAbsoluteFilePath2
* 写入的文件绝对路径
*/
private static boolean isColorNameExist(String targetColorName, String targetColorAbsoluteFilePath2) {
File newfile = new File(targetColorAbsoluteFilePath2);
if (!newfile.exists()) {
return false;
}
FileInputStream fis = null;
InputStream is = null;
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
// 获取XmlPullParser实例
XmlPullParser pullParser = factory.newPullParser();
fis = new FileInputStream(newfile);
is = new BufferedInputStream(fis);
pullParser.setInput(is, "UTF-8");
// 开始
int eventType = pullParser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {// 文档没读取完
String nodeName = pullParser.getName();
switch (eventType) {
// 文档开始
case XmlPullParser.START_DOCUMENT:
break;
// 开始节点
case XmlPullParser.START_TAG:
if (null != nodeName && "color".equals(nodeName)) {
String attrname = pullParser.getAttributeValue(0);
if (targetColorName.equals(attrname)) {
System.out.println("exist attrname = " + attrname);
try {
if (is != null) {
is.close();
}
if (fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
}
break;
default:
break;
}
// 手动的触发下一个事件
eventType = pullParser.next();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
if (fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
/**
* 读取单个java文件内容,并获取R.color.xxx中的xxx文件名
*/
public static List<String> getJavaColorName(String javaFileName) {
List<String> colorNameList = new ArrayList<String>();
try {
BufferedReader reader = new BufferedReader(new FileReader(javaFileName));
String line;
String colorName = null;
while ((line = reader.readLine()) != null) {
if (line.contains("R.color.")) {
String[] fileArr = line.split("R.color.");
if (null != fileArr && fileArr.length >= 2) {
if (fileArr[1].contains(",")) {// 考虑到setTag(R.color.xxx,"")
colorName = fileArr[1].split("[,]")[0];
} else {// 剩下都是(R.color.xxx)
colorName = fileArr[1].split("[)]")[0];
}
//
if (!colorNameList.contains(colorName)) {// 去重
colorNameList.add(colorName);
}
}
}
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
}
return colorNameList;
}
/**
* 执行xml中图片 下午7:53:04 void
*/
private static void executeLayoutFile(File f) {
List<String> colorNameList = getLayoutColorName(f.getAbsolutePath());
// System.out.println("stringNameList = "+stringNameList);
if (null != colorNameList && !colorNameList.isEmpty()) {
int size = colorNameList.size();
for (int i = 0; i < size; i++) {
String colorName = colorNameList.get(i);
if (null != colorName) {
// 查找color
List<Integer> colorLineList = getSrcColorLines(srcColorAbsoluteFilePath, colorName.trim());
if (null != colorLineList && !colorLineList.isEmpty()) {
if (!colorDelLineLists.contains(colorLineList.get(0))) {// 去重:如果曾经写进去,就不用再去写进去了
colorDelLineLists.addAll(colorLineList);// 不管目标文件存不存在资源名都要删除源文件的,因为要都会写入
boolean isColorNameExist = isColorNameExist(colorName, targetColorAbsoluteFilePath);
System.out.println("isColorNameExist = " + isColorNameExist);
if (!isColorNameExist) {//
writeColorFile(colorLineList, srcColorAbsoluteFilePath, targetColorAbsoluteFilePath);
}
}
}
}
}
}
}
/**
* 获取xml中引用color名集合 如:
*
* @color/no_data_toast 的no_data_toast 下午3:05:04 List<String>
*/
public static List<String> getLayoutColorName(String xmlFileName) {
List<String> colorNameList = new ArrayList<String>();
try {
BufferedReader reader = new BufferedReader(new FileReader(xmlFileName));
String line;
String colorName = null;
没有合适的资源?快使用搜索试试~ 我知道了~
android之APP抽离Module及自动化移动全部资源
共24个文件
class:11个
java:10个
prefs:1个
需积分: 12 9 下载量 114 浏览量
2016-10-21
16:53:18
上传
评论
收藏 53KB RAR 举报
温馨提示
android之APP抽离Module及自动化移动全部资源,用于自动化移动java类所需要的全部资源如drawable/layout/string/style/color
资源推荐
资源详情
资源评论
收起资源包目录
MoveFiles.rar (24个子文件)
MoveFiles
bin
www
file
LayoutIncludeLayoutFileUtil.class 4KB
DrawableFileUtil.class 6KB
FileUtil.class 2KB
StyleLayoutRefereceUtil.class 10KB
StringRefereceUtil.class 11KB
SytleDrawFileUtil.class 3KB
XmlDrawFileUtil.class 3KB
LayoutJavaFileUtil.class 4KB
ColorRefereceUtil.class 11KB
main
FileMain.class 726B
FileMain$1.class 1KB
.settings
org.eclipse.jdt.core.prefs 598B
src
www
file
XmlDrawFileUtil.java 3KB
StringRefereceUtil.java 15KB
LayoutIncludeLayoutFileUtil.java 4KB
StyleLayoutRefereceUtil.java 13KB
ColorRefereceUtil.java 15KB
LayoutJavaFileUtil.java 4KB
FileUtil.java 1KB
DrawableFileUtil.java 7KB
SytleDrawFileUtil.java 3KB
main
FileMain.java 5KB
.project 385B
.classpath 380B
共 24 条
- 1
资源评论
小钟视野
- 粉丝: 45
- 资源: 50
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功