-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModel.java
186 lines (167 loc) · 7.54 KB
/
Model.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
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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Representation of a model class
*/
class Model {
public String tableName = null;
public List<Column> fields = new ArrayList<>();
/**
* Search by column name only
*/
public Column search(Column needle) {
for (Column col : fields) {
if (col.name != null && col.name.equals(needle.name)) {
return col;
}
}
return null;
}
/**
* Create a buffered reader on the file at the desired commit
* If the SHA1 commit ID is null, the current file is loaded
* If the SHA1 commit ID is not null, the file at that revision is loaded
*/
private static BufferedReader openFileAt(String file, String sha1) throws IOException {
ProcessBuilder builder;
if (sha1 == null) {
builder = new ProcessBuilder("cat", file);
} else {
builder = new ProcessBuilder("git", "--no-pager", "show", sha1 + ":" + file);
}
builder.redirectErrorStream(true);
Process p = builder.start();
return new BufferedReader(new InputStreamReader(p.getInputStream()));
}
/**
* Parse a model class file
*/
Model(String sha1, String modelFile, Map<String, String> types) throws IOException {
if (!modelFile.endsWith("/com/activeandroid/Model.java")
&& !(new File(modelFile).exists())) {
throw new IllegalArgumentException("Unable to open model file: " + modelFile);
}
// Various regexes used to parse the interesting sections of the file
Pattern column = Pattern.compile("(public)?\\s*([^ ]+)\\s*([^\\s]+)\\s*;");
Pattern table = Pattern.compile("@Table\\s*\\(\\s*name\\s*=\\s*([^\\)]+)\\)");
Pattern pkg = Pattern.compile("package\\s+([^\\s;]+)\\s*;");
Pattern imp = Pattern.compile("import\\s+([^\\s;]+)\\s*;");
Pattern ext = Pattern.compile("extends\\s+([^\\s\\{]+)\\s*\\{?");
Pattern keys = Pattern.compile("on(Delete|Update)\\s*=\\s*.*(SET_NULL|SET_DEFAULT|CASCADE|RESTRICT|NO_ACTION)");
boolean isModel = false;
String line, packagePath = "";
Map<String, String> imports = new HashMap<>();
BufferedReader r = openFileAt(modelFile, sha1);
while (true) {
line = r.readLine();
if (line == null) {
break;
}
// Load package name as a relative path
Matcher pkgMatcher = pkg.matcher(line);
if (pkgMatcher.find()) {
packagePath = modelFile.substring(0, modelFile.indexOf(pkgMatcher.group(1).replaceAll("\\.", "/")));
}
// Load imports
Matcher importMatcher = imp.matcher(line);
if (importMatcher.find()) {
imports.put(
importMatcher.group(1).substring(importMatcher.group(1).lastIndexOf(".") + 1), // class name (without the package name)
importMatcher.group(1).replaceAll("\\.", "/") // file path
);
}
// See if we subclass another model
Matcher subclass = ext.matcher(line);
if (subclass.find()) {
if (imports.containsKey(subclass.group(1))) {
// Add all the parent's fields
Model parent = new Model(sha1, packagePath + imports.get(subclass.group(1)) + ".java", types);
fields.addAll(parent.fields);
}
}
// See if the @Table annotation contains the table name
if (tableName == null && line.contains("@Table")) {
isModel = true;
Matcher tableMatcher = table.matcher(line);
if (tableMatcher.find()) {
tableName = tableMatcher.group(1);
}
}
// Ah, new column!
if (line.contains("@Column")) {
// See if it's a foreign key with onDelete and onUpdate instructions
Matcher keysMatcher = keys.matcher(line);
String upd = null;
String del = null;
while (keysMatcher.find()) {
if ("Update".equals(keysMatcher.group(1))) {
upd = keysMatcher.group(2).replaceAll("_", " ");
}
if ("Delete".equals(keysMatcher.group(1))) {
del = keysMatcher.group(2).replaceAll("_", " ");
}
}
// Parse the column declaration
do {
Matcher colMatcher = column.matcher(line);
if (colMatcher.find()) {
Column col = new Column(colMatcher.group(2), colMatcher.group(3));
if (del != null) {
col.delete = del;
}
if (upd != null) {
col.update = upd;
}
// Try to detect FK
switch (colMatcher.group(2)) {
case "Boolean": case "boolean":
case "Integer": case "int":
case "Double": case "double":
case "Float": case "float":
case "char": case "CharSequence": case "String":
// no-op
break;
default:
// See if this is a custom type provided by AA
if (types.containsKey(colMatcher.group(2))) {
// not a FK so ignore
}
// if the column type is imported and is a model, save the foreign key table name
else if (imports.containsKey(colMatcher.group(2))) {
col.fk = new Model(sha1, packagePath + imports.get(colMatcher.group(2)) + ".java", types).tableName;
}
// try package local foreign key class?
else if (col.fk == null) {
col.fk = new Model(sha1, packagePath + colMatcher.group(2) + ".java", types).tableName;
}
else {
System.out.println("Unhandled column type: " + colMatcher.group(2));
}
break;
}
fields.add(col);
break;
}
} while ((line = r.readLine()) != null);
}
}
// If the @Table annotation didn't set the table name, assume class name
if (tableName == null && isModel) {
String className = modelFile.substring(modelFile.lastIndexOf("/") + 1);
tableName = className.substring(0, className.indexOf("."));
}
// The table name will be null if there is no @Table annotation
if (tableName != null) {
tableName = tableName.replaceAll("\"", ""); // strip quotes
}
}
}