Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c5da99b

Browse files
committed
Use new structure to parse Android XML (Restructuring) v25
Support selector, but the code structure is bad
1 parent 6002e41 commit c5da99b

File tree

3 files changed

+70
-49
lines changed

3 files changed

+70
-49
lines changed

src/com/excelsecu/androidx2j/AX2JClassTranslator.java

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -207,16 +207,20 @@ public void translate(AX2JCodeBlock codeBlock, Attribute attr, int priority) {
207207
* @param attr the attribute to be translated
208208
* @return the value after translating
209209
*/
210-
protected String translateValue(AX2JCodeBlock codeBlock, AX2JAttribute attribute, AX2JMethod method) {
211-
String value = attribute.getValue().getValue();
212-
String attrName = attribute.getName().getQualifiedName();
213-
210+
private String translateValue(AX2JCodeBlock codeBlock, AX2JAttribute attribute, AX2JMethod method) {
214211
int argOrder = attribute.getTypeValue(AX2JAttribute.TYPE_ARGUMENTS_ORDER);
215212
if (argOrder == AX2JAttribute.TYPE_ARGUMENTS_ALL_THE_SAME) {
216213
argOrder = 1;
217214
}
218215
Class<?> argType = method.getArgType(argOrder);
219-
216+
217+
return translateValue(codeBlock, method, attribute.getValue(), argType);
218+
}
219+
220+
protected final String translateValue(AX2JCodeBlock codeBlock, AX2JMethod method, Attribute attribute, Class<?> argType) {
221+
String value = attribute.getValue();
222+
String name = attribute.getQualifiedName();
223+
220224
if (argType.equals(Integer.class)) {
221225
//dp, px, sp
222226
if (value.matches("[0-9.]+dp")) {
@@ -247,8 +251,8 @@ else if (value.contains("@string/")) {
247251
value = value.substring(value.indexOf('/') + 1);
248252
value = Config.R_CLASS + ".string." + value;
249253
value = Config.RESOURCES_NAME + ".getString(" + value + ")";
250-
} else if (attrName.equals("android:text") ||
251-
attrName.equals("android:hint")) {
254+
} else if (name.equals("android:text") ||
255+
name.equals("android:hint")) {
252256
value = "\"" + value + "\"";
253257
}
254258

@@ -289,19 +293,19 @@ else if (value.equals("vertical")) {
289293
}
290294

291295
//gravity
292-
else if (attrName.equals("android:gravity") ||
293-
attrName.equals("android:layout_gravity")) {
296+
else if (name.equals("android:gravity") ||
297+
name.equals("android:layout_gravity")) {
294298
value = Utils.prefixParams(value, "Gravity");
295299
codeBlock.addImport(Gravity.class.getName());
296300
}
297301

298302
//margin
299-
else if (attrName.matches("android:layout_margin(Left)|(Top)|(Right)|(Bottom)")) {
303+
else if (name.matches("android:layout_margin(Left)|(Top)|(Right)|(Bottom)")) {
300304
codeBlock.addImport(ViewGroup.class.getName());
301305
}
302306

303307
//text
304-
else if (attrName.equals("android:textAppearance")) {
308+
else if (name.equals("android:textAppearance")) {
305309
String style = AX2JStyle.getStyle(value).name;
306310
style = style.replace('.', '_');
307311
style = "android.R.style." + style;
@@ -310,7 +314,7 @@ else if (attrName.equals("android:textAppearance")) {
310314

311315
/** independent part **/
312316
//RelativeLayout rule
313-
if (method.getMethodName().equals("addRule")) {
317+
if (method != null && method.getMethodName().equals("addRule")) {
314318
if (value.equals("true")) {
315319
value = "RelativeLayout.TRUE";
316320
} else if (value.equals("false")) {
@@ -320,7 +324,7 @@ else if (attrName.equals("android:textAppearance")) {
320324
}
321325

322326
//divider
323-
if (attrName.equals("android:divider")) {
327+
if (name.equals("android:divider")) {
324328
codeBlock.addImport(ColorDrawable.class.getName());
325329
}
326330

@@ -351,8 +355,8 @@ else if (argType.equals(Drawable.class) || argType.equals(ColorStateList.class))
351355
if (value.startsWith("@drawable/")) {
352356
value = value.substring(value.indexOf('/') + 1);
353357
value = Config.R_CLASS + ".drawable." + value;
354-
if (attrName.contains("Color") ||
355-
attrName.contains("TintList")) {
358+
if (name.contains("Color") ||
359+
name.contains("TintList")) {
356360
value = "resources.getColorStateList(" + value + ")";
357361
} else {
358362
value = "resources.getDrawable(" + value + ")";
@@ -362,21 +366,21 @@ else if (argType.equals(Drawable.class) || argType.equals(ColorStateList.class))
362366

363367
else if (argType.equals(TransformationMethod.class)) {
364368
//text
365-
if (attrName.equals("android:password")) {
369+
if (name.equals("android:password")) {
366370
value = "new PasswordTransformationMethod()";
367371
codeBlock.addImport(PasswordTransformationMethod.class.getName());
368-
} else if (attrName.equals("android:singleLine")) {
372+
} else if (name.equals("android:singleLine")) {
369373
value = "new SingleLineTransformationMethod()";
370374
codeBlock.addImport(SingleLineTransformationMethod.class.getName());
371-
} else if (attrName.equals("android:inputType")) {
375+
} else if (name.equals("android:inputType")) {
372376
String error = value;
373377
value = Config.INPUT_TYPE_MAP.get(value);
374378
if (value == null) {
375379
throw new AX2JException(AX2JException.ATTRIBUTE_VALUE_ERROR, error);
376380
}
377381
value = Utils.prefixParams(value, "InputType");
378382
codeBlock.addImport(InputType.class.getName());
379-
} else if (attrName.equals("android:ellipsize")) {
383+
} else if (name.equals("android:ellipsize")) {
380384
value = value.toUpperCase();
381385
value = "TextUtils.TruncateAt." + value;
382386
codeBlock.addImport(TextUtils.class.getName());

src/com/excelsecu/androidx2j/BaseTranslator.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,28 @@ protected void translateAttribute(AX2JCodeBlock codeBlock, Attribute attribute,
167167
}
168168
}
169169

170+
protected final String translateValue(AX2JCodeBlock codeBlock, Attribute attribute, Class<?> argType) {
171+
String value = attribute.getValue();
172+
Class<?> type = codeBlock.getType();
173+
174+
while (true) {
175+
AX2JClassTranslator translator = map.get(type);
176+
if (translator == null) {
177+
codeBlock.add("//" + attribute.asXML() + "\t//not support\n");
178+
break;
179+
} else {
180+
try {
181+
value = translator.translateValue(codeBlock, null, attribute, argType);
182+
break;
183+
} catch(AX2JException e) {
184+
type = type.getSuperclass();
185+
}
186+
}
187+
}
188+
189+
return value;
190+
}
191+
170192
/**
171193
* Add the class to the import list. If already exists, ignore.
172194
* @param className the class try to be added in import list

src/com/excelsecu/androidx2j/SelectorTranslator.java

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,30 @@
33

44
import android.content.Context;
55
import android.content.res.ColorStateList;
6+
import android.graphics.drawable.Drawable;
67
import android.graphics.drawable.StateListDrawable;
78

89
public class SelectorTranslator extends BaseTranslator {
910

10-
public static void main(String[] argv) {
11-
System.out.println(new SelectorTranslator(new AX2JParser("res/drawable/color_selector.xml").parse()).translate());
12-
}
13-
1411
public SelectorTranslator(AX2JNode node) {
1512
super(node);
1613
AX2JNode.resetOrder();
1714
}
1815

1916
public String translate() {
2017
if (getType() == ColorStateList.class) {
21-
String javaBlock = translateToColorStateList();
22-
return javaBlock;
18+
return translateToColorStateList().toString();
2319
} else if (getType() == StateListDrawable.class) {
24-
String javaBlock = translateToStateListDrawable();
25-
return javaBlock;
20+
return translateToStateListDrawable().toString();
2621
} else {
2722
throw new AX2JException(AX2JException.AXML_PARSE_ERROR, "not a selector type");
2823
}
2924
}
3025

31-
private String translateToColorStateList() {
26+
private AX2JCodeBlock translateToColorStateList() {
3227
addImport(Context.class.getName());
3328
addImport(ColorStateList.class.getName());
34-
String javaBlock = "";
29+
AX2JCodeBlock codeBlock = new AX2JCodeBlock(ColorStateList.class, getRoot().getObjectName());
3530
String stateSetList = "";
3631
String colorList = "";
3732
for (AX2JNode n : getRoot().getChildren()) {
@@ -43,7 +38,7 @@ private String translateToColorStateList() {
4338
for (Attribute a : n.getAttributes()) {
4439
String attrName = a.getQualifiedName();
4540
if (attrName.equals("android:color")) {
46-
//color = translateValue(a);
41+
color = translateValue(codeBlock, a, Integer.class);
4742
} else {
4843
String state = "android.R.attr." + a.getName();
4944
if (a.getValue().equals("false")) {
@@ -55,7 +50,6 @@ private String translateToColorStateList() {
5550
stateSet += ", " + state;
5651
}
5752
}
58-
//extraHandle(getRoot(), a);
5953
}
6054
if (colorList.equals("")) {
6155
colorList = color;
@@ -67,31 +61,32 @@ private String translateToColorStateList() {
6761
//remove comma
6862
stateSetList = stateSetList.substring(0, stateSetList.length() - 1);
6963

70-
javaBlock += "int[][] stateSet" + " = new int[][] {" + stateSetList + "};\n";
71-
javaBlock += "int[] colorSet" + " = new int[] {" + colorList + "};\n";
72-
javaBlock += "ColorStateList colorStateList = new ColorStateList(stateSet, colorSet);\n";
73-
return javaBlock;
64+
codeBlock.add("int[][] stateSet" + " = new int[][] {" + stateSetList + "};\n");
65+
codeBlock.add("int[] colorSet" + " = new int[] {" + colorList + "};\n");
66+
codeBlock.add("ColorStateList colorStateList = new ColorStateList(stateSet, colorSet);\n");
67+
return codeBlock;
7468
}
7569

76-
private String translateToStateListDrawable() {
70+
private AX2JCodeBlock translateToStateListDrawable() {
7771
addImport(Context.class.getName());
7872
addImport(StateListDrawable.class.getName());
73+
AX2JCodeBlock codeBlock = new AX2JCodeBlock(ColorStateList.class, getRoot().getObjectName());
7974
int num = 0;
80-
String javaBlock = "";
81-
javaBlock += "StateListDrawable stateListDrawable = new StateListDrawable();\n";
82-
for (AX2JNode n : getRoot().getChildren()) {
83-
if (!n.getLabelName().equals("item")) {
75+
76+
codeBlock.add("StateListDrawable stateListDrawable = new StateListDrawable();\n");
77+
for (AX2JNode node : getRoot().getChildren()) {
78+
if (!node.getLabelName().equals("item")) {
8479
continue;
8580
}
8681
String stateSet = "";
8782
String drawable = "";
88-
for (Attribute a : n.getAttributes()) {
89-
String attrName = a.getQualifiedName();
83+
for (Attribute attribute : node.getAttributes()) {
84+
String attrName = attribute.getQualifiedName();
9085
if (attrName.equals("android:drawable")) {
91-
//drawable = translateValue(a);
86+
drawable = translateValue(codeBlock, attribute, Drawable.class);
9287
} else {
93-
String state = "android.R.attr." + a.getName();
94-
if (a.getValue().equals("false")) {
88+
String state = "android.R.attr." + attribute.getName();
89+
if (attribute.getValue().equals("false")) {
9590
state = "-" + state;
9691
}
9792
if (stateSet.equals("")) {
@@ -100,14 +95,14 @@ private String translateToStateListDrawable() {
10095
stateSet += ", " + state;
10196
}
10297
}
103-
//extraHandle(getRoot(), a);
10498
}
10599

106100
String setName = "stateSet" + num;
107-
javaBlock += "int[] " + setName + " = new int[] {" + stateSet + "};\n";
108-
javaBlock += "stateListDrawable.addState(" + setName + ", " + drawable + ");\n";
101+
codeBlock.add("int[] " + setName + " = new int[] {" + stateSet + "};\n");
102+
codeBlock.add("stateListDrawable.addState(" + setName + ", " + drawable + ");\n");
109103
num++;
110104
}
111-
return javaBlock;
105+
106+
return codeBlock;
112107
}
113108
}

0 commit comments

Comments
 (0)