-
Notifications
You must be signed in to change notification settings - Fork 9
/
GenerateResourceName
313 lines (303 loc) · 12.7 KB
/
GenerateResourceName
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 根据指定的项目的R.java文件的ID,生成对应源文件目录的RR.java文件的名称
*/
public class GenerateResourceName
{
private final static String destFileName = "RR";
private final static String destProjectRootPath = "F:\\jin\\workspace\\GenerateResourceName\\";
private final static String srcName = destProjectRootPath
+ "gen\\com\\jinpang\\GenerateResourceName\\R.java";
private final static String destName = destProjectRootPath
+ "src\\com\\jinpang\\albumsdk\\" + destFileName + ".java";
private final static String tmpName = destProjectRootPath
+ "src\\com\\jinpang\\GenerateResourceName\\" + destFileName + ".tmp";
public final static String StringName = "String";
public final static String regex = "public static final (int) (.+)=(.+);";
public final static String regexClassName = "public final class (R) \\{";
public final static String regexInnerClassName = "public static final class (.+) \\{";
public final static String regexIntegerArrayName = "public static final (int)\\[\\] (.+)=(.+)";
public final static String regexStleableClass = "public static final class (styleable)";
public final static String regexPackage = "package com.(.+);";
public final static String newLine = "\n";
public final static String prefix = " ";
public final static String clause = ";";
public final static String methodPrefix1 = " ";
public final static String methodPrefix2 = " ";
public final static String methodPrefix3 = " ";
//如果导入的包
public final static String importContextClass = "import android.content.Context;";
public final static String id = "public static final String ID = \"id\"";
public final static String string = "public static final String STRING = \"string\"";
public final static String array = "public static final String ARRAY = \"array\"";
public final static String attr = "public static final String ATTR = \"attr\"";
public final static String color = "public static final String COLOR = \"color\"";
public final static String dimen = "public static final String DIMEN = \"dimen\"";
public final static String drawable = "public static final String DRAWABLE = \"drawable\"";
public final static String layout = "public static final String LAYOUT = \"layout\"";
public final static String raw = "public static final String RAW = \"raw\"";
public final static String style = "public static final String STYLE = \"style\"";
public final static String anim = "public static final String ANIM = \"anim\"";
public final static String bool = "public static final String BOOL = \"bool\"";
public final static String integer = "public static final String INTEGER = \"integer\"";
public final static String interpolator = "public static final String INTERPOLATOR = \"interpolator\"";
public final static String menu = "public static final String MENU = \"menu\"";
public final static String mipmap = "public static final String MIPMAP = \"mipmap\"";
public final static String plurals = "public static final String PLURALS = \"plurals\"";
public final static String styleable = "public static final String STYLEABLE = \"styleable\"";
public final static String xml = "public static final String XML = \"xml\"";
public final static String common[] =
{
id, string, array, attr, color, dimen, drawable, layout, raw, style, anim, bool,
integer, interpolator, menu, mipmap, plurals, styleable, xml
};
private static Map<String, String> mKeyValueMaps = new HashMap<String, String>();
public static void main(String[] args)
{
File src = new File(srcName);
File tmp = new File(tmpName);
File dest = new File(destName);
parse(src, tmp);
//处理styleable类型
parseStyleable(tmp, dest);
}
public static void parse(File src, File dest)
{
mKeyValueMaps.clear();
BufferedReader br = null;
BufferedWriter bw = null;
Pattern pattern = Pattern.compile(regex);
Pattern patternClassName = Pattern.compile(regexClassName);
Pattern patternInnerClassName = Pattern.compile(regexInnerClassName);
Pattern patternPackage = Pattern.compile(regexPackage);
Pattern patternStleableClass = Pattern.compile(regexStleableClass);
Matcher matcher;
Matcher matcherClassName;
Matcher matcherInnerClassName;
Matcher matcherPackage;
Matcher matcherStleableClass;
try
{
br = new BufferedReader(new FileReader(src));
bw = new BufferedWriter(new FileWriter(dest));
String line = "";
while ((line = br.readLine()) != null)
{
matcher = pattern.matcher(line);
matcherClassName = patternClassName.matcher(line);
matcherInnerClassName = patternInnerClassName.matcher(line);
matcherPackage = patternPackage.matcher(line);
matcherStleableClass = patternStleableClass.matcher(line);
if (matcherPackage.find())
{
bw.write(line + newLine);
bw.write(importContextClass + newLine + newLine);
}
else if (matcherClassName.find())
{
String g1 = matcherClassName.group(1).trim();
line = line.replace(g1, destFileName);
bw.write(line + newLine + newLine);
for (String s : common)
{
bw.write(prefix + s + clause + newLine);
}
bw.write(newLine);
}
else if (matcherInnerClassName.find())
{
String g1 = matcherInnerClassName.group(1).trim();
bw.write(line + newLine + newLine);
String method = methodPrefix1
+ "public static int getIdentifier(Context context, String resName)"
+ newLine + methodPrefix1 + "{" + newLine + methodPrefix2 +
"return context.getResources().getIdentifier("
+ newLine + methodPrefix3 +
"resName," + newLine + methodPrefix3 +
"\"" + g1 + "\"," + newLine + methodPrefix3 +
"context.getPackageName());" + newLine + methodPrefix1 +
"}" + newLine;
bw.write(method);
bw.write(newLine);
if (matcherStleableClass.find())
{
g1 = matcherStleableClass.group(1).trim();
String methodArray = methodPrefix1
+ "public static int[] getIdentifier(Context context, String[] attrNames)"
+ newLine + methodPrefix1 + "{" + newLine + methodPrefix2 +
"int length = attrNames.length;" + newLine + methodPrefix2 +
"int[] attrs = new int[length];" + newLine + methodPrefix2 +
"for (int i = 0; i < length; i++)" + newLine + methodPrefix2 +
"{" + newLine + methodPrefix3 +
"attrs[i] = getIdentifier(context, attrNames[i]);" + newLine
+ methodPrefix2 +
"}" + newLine + methodPrefix2 +
"return attrs;" + newLine + methodPrefix1 +
"}" + newLine;
bw.write(methodArray);
bw.write(newLine);
}
}
else
{
if (matcher.find())
{
String g1 = matcher.group(1).trim();
String g2 = matcher.group(2).trim();
String g3 = matcher.group(3).trim();
mKeyValueMaps.put(g3, g2);
line = line.replace(g1, StringName).replace(g3, "\"" + g2 + "\"");
}
bw.write(line + newLine);
}
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (br != null)
{
try
{
br.close();
br = null;
}
catch (IOException e)
{
e.printStackTrace();
}
}
if (bw != null)
{
try
{
bw.flush();
bw.close();
bw = null;
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
public static void parseStyleable(File src, File dest)
{
BufferedReader br = null;
BufferedWriter bw = null;
Pattern patternIntegerArrayName = Pattern.compile(regexIntegerArrayName);
Matcher matcherIntegerArrayName;
try
{
br = new BufferedReader(new FileReader(src));
bw = new BufferedWriter(new FileWriter(dest));
String line = "";
while ((line = br.readLine()) != null)
{
matcherIntegerArrayName = patternIntegerArrayName.matcher(line);
if (matcherIntegerArrayName.find())
{
String g1 = matcherIntegerArrayName.group(1).trim();
line = line.replace(g1, StringName);
bw.write(line + newLine);
}
else
{
Set<String> keySets = mKeyValueMaps.keySet();
boolean isNeed = false;
String[] tmpArray = line.split(",");
for (int i = 0; i < tmpArray.length; i++)
{
tmpArray[i] = tmpArray[i].trim();
if (keySets.contains(tmpArray[i]))
{
tmpArray[i] = mKeyValueMaps.get(tmpArray[i]);
isNeed = true;
}
}
String tmpStr = "";
if (isNeed)
{
if (tmpArray.length > 1)
{
tmpStr = tmpStr + methodPrefix2;
for (int j = 0; j < tmpArray.length - 1; j++)
{
tmpStr = tmpStr + "\"" + tmpArray[j] + "\", ";
}
tmpStr = tmpStr + "\"" + tmpArray[tmpArray.length - 1] + "\"";
}
else
{
tmpStr = methodPrefix2 + tmpStr + "\"" + tmpArray[0] + "\"";
}
bw.write(tmpStr + newLine);
}
else
{
bw.write(line + newLine);
}
}
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (br != null)
{
try
{
br.close();
br = null;
}
catch (IOException e)
{
e.printStackTrace();
}
}
if (bw != null)
{
try
{
bw.flush();
bw.close();
bw = null;
}
catch (IOException e)
{
e.printStackTrace();
}
}
if (src != null && src.exists())
{
src.delete();
}
}
}
}