-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathMain.java
More file actions
132 lines (117 loc) · 4.94 KB
/
Main.java
File metadata and controls
132 lines (117 loc) · 4.94 KB
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
package com.foxykeep.cpcodegenerator;
import com.foxykeep.cpcodegenerator.generator.DatabaseGenerator;
import com.foxykeep.cpcodegenerator.model.TableData;
import com.foxykeep.cpcodegenerator.util.PathUtils;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
public class Main {
public static void main(final String[] args) {
final String dir = System.getProperty("user.dir");
System.out.println("current dir = " + dir);
File fileInputDir;
if (args.length > 0) {
fileInputDir = new File(args[0]);
if (!fileInputDir.isAbsolute()) {
fileInputDir = new File(dir + File.separator + args[0]);
}
} else {
fileInputDir = new File(dir + File.separator + "example");
}
if (!fileInputDir.exists() || !fileInputDir.isDirectory()) {
return;
}
String columnMetadataText;
final StringBuilder sb = new StringBuilder();
BufferedReader br;
try {
br = new BufferedReader(new FileReader(new File("res/column_metadata.txt")));
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
columnMetadataText = sb.toString();
} catch (FileNotFoundException e) {
e.printStackTrace();
return;
} catch (IOException e) {
e.printStackTrace();
return;
}
// For each file in the input folder
for (File file : fileInputDir.listFiles()) {
final String fileName = file.getName();
if ("format.json".equals(fileName)) {
continue;
}
System.out.println("Generating code for " + fileName);
final char[] buffer = new char[2048];
sb.setLength(0);
final Reader in;
try {
in = new InputStreamReader(new FileInputStream(file), "UTF-8");
int read;
do {
read = in.read(buffer, 0, buffer.length);
if (read != -1) {
sb.append(buffer, 0, read);
}
} while (read >= 0);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return;
} catch (FileNotFoundException e) {
e.printStackTrace();
return;
} catch (IOException e) {
e.printStackTrace();
return;
}
String content = sb.toString();
content = content.substring(content.indexOf("{"));
if (content.length() == 0) {
System.out.println("file is empty.");
return;
}
try {
final JSONObject root = new JSONObject(content);
final JSONObject jsonDatabase = root.getJSONObject("database");
// Classes generation
String classPackage, classesPrefix, contentClassesPrefix, dbAuthorityPackage, providerFolder;
int dbVersion;
boolean hasProviderSubclasses;
classPackage = jsonDatabase.getString("package");
classesPrefix = jsonDatabase.getString("classes_prefix");
contentClassesPrefix = jsonDatabase.optString("content_classes_prefix", "");
dbAuthorityPackage = jsonDatabase.optString("authority_package", classPackage);
providerFolder = jsonDatabase.optString("provider_folder",
PathUtils.PROVIDER_DEFAULT);
dbVersion = jsonDatabase.getInt("version");
hasProviderSubclasses = jsonDatabase.optBoolean("has_subclasses");
ArrayList<TableData> classDataList = TableData.getClassesData(
root.getJSONArray("tables"), contentClassesPrefix, dbVersion);
// Database generation
DatabaseGenerator.generate(fileName, classPackage, dbVersion, dbAuthorityPackage,
classesPrefix, classDataList, providerFolder, hasProviderSubclasses);
FileCache.saveFile(
PathUtils.getAndroidFullPath(fileName, classPackage, providerFolder + "."
+ PathUtils.UTIL)
+ "ColumnMetadata.java",
String.format(columnMetadataText, classPackage, providerFolder + "."
+ PathUtils.UTIL));
} catch (JSONException e) {
e.printStackTrace();
return;
}
}
}
}