-
Notifications
You must be signed in to change notification settings - Fork 12
/
Element.java
executable file
·145 lines (118 loc) · 3.65 KB
/
Element.java
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
package com.avast.android.butterknifezelezny.model;
import com.avast.android.butterknifezelezny.common.Utils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Element {
// constants
private static final Pattern sIdPattern = Pattern.compile("@\\+?(android:)?id/([^$]+)$", Pattern.CASE_INSENSITIVE);
private static final Pattern sValidityPattern = Pattern.compile("^([a-zA-Z_\\$][\\w\\$]*)$", Pattern.CASE_INSENSITIVE);
public String id;
public boolean isAndroidNS = false;
public String nameFull; // element name with package
public String name; // element name
public String fieldName; // name of variable
public boolean isValid = false;
public boolean used = true;
public boolean isClick = true;
public boolean useInLib = false;
public Element(String name, String id) {
// id
final Matcher matcher = sIdPattern.matcher(id);
if (matcher.find() && matcher.groupCount() > 0) {
this.id = matcher.group(2);
String androidNS = matcher.group(1);
this.isAndroidNS = !(androidNS == null || androidNS.length() == 0);
}
// name
String[] packages = name.split("\\.");
if (packages.length > 1) {
this.nameFull = name;
this.name = packages[packages.length - 1];
} else {
this.nameFull = null;
this.name = name;
}
this.fieldName = getFieldName();
}
/**
* Create full ID for using in layout XML files
*
* @return
*/
public String getFullIDByUseInLib() {
StringBuilder fullID = new StringBuilder();
String rPrefix;
String R = useInLib ? "R2" : "R";
if (isAndroidNS) {
rPrefix = "android." + R + ".id.";
} else {
rPrefix = R + ".id.";
}
fullID.append(rPrefix);
fullID.append(id);
return fullID.toString();
}
/**
* Create full ID for using in layout XML files
*
* @return
*/
public String getFullIDWithR() {
StringBuilder fullID = new StringBuilder();
String rPrefix;
if (isAndroidNS) {
rPrefix = "android.R.id.";
} else {
rPrefix = "R.id.";
}
fullID.append(rPrefix);
fullID.append(id);
return fullID.toString();
}
/**
* Create full ID for using in layout XML files
*
* @return
*/
public String getFullIDWithR2() {
StringBuilder fullID = new StringBuilder();
String rPrefix;
if (isAndroidNS) {
rPrefix = "android.R2.id.";
} else {
rPrefix = "R2.id.";
}
fullID.append(rPrefix);
fullID.append(id);
return fullID.toString();
}
/**
* Generate field name if it's not done yet
*
* @return
*/
private String getFieldName() {
String[] words = this.id.split("_");
StringBuilder sb = new StringBuilder();
sb.append(Utils.getPrefix());
for (int i = 0; i < words.length; i++) {
String[] idTokens = words[i].split("\\.");
char[] chars = idTokens[idTokens.length - 1].toCharArray();
if (i > 0 || !Utils.isEmptyString(Utils.getPrefix())) {
chars[0] = Character.toUpperCase(chars[0]);
}
sb.append(chars);
}
return sb.toString();
}
/**
* Check validity of field name
*
* @return
*/
public boolean checkValidity() {
Matcher matcher = sValidityPattern.matcher(fieldName);
isValid = matcher.find();
return isValid;
}
}