1 |
49fd8648
|
Michal Horký
|
import java.io.File;
|
2 |
|
|
import java.io.IOException;
|
3 |
|
|
import java.util.HashMap;
|
4 |
|
|
|
5 |
|
|
import io.FileWorker;
|
6 |
|
|
import javafx.application.Application;
|
7 |
|
|
|
8 |
|
|
public class CLI implements IConversionResults {
|
9 |
|
|
|
10 |
|
|
private static class Option {
|
11 |
|
|
String abbr;
|
12 |
|
|
String about;
|
13 |
|
|
|
14 |
|
|
public Option(String abbr) {
|
15 |
|
|
// Convert input argument to option.
|
16 |
|
|
this.abbr = abbr;
|
17 |
|
|
this.about = null;
|
18 |
|
|
}
|
19 |
|
|
|
20 |
|
|
public Option(String abbr, String about) {
|
21 |
|
|
this.abbr = abbr;
|
22 |
|
|
this.about = about;
|
23 |
|
|
}
|
24 |
|
|
|
25 |
|
|
@Override
|
26 |
|
|
public String toString() {
|
27 |
|
|
return abbr + "\t" + about;
|
28 |
|
|
}
|
29 |
|
|
|
30 |
|
|
@Override
|
31 |
|
|
public int hashCode() {
|
32 |
|
|
return abbr.hashCode();
|
33 |
|
|
}
|
34 |
|
|
|
35 |
|
|
@Override
|
36 |
|
|
public boolean equals(Object obj) {
|
37 |
|
|
if (this == obj) {
|
38 |
|
|
return true;
|
39 |
|
|
}
|
40 |
|
|
if (obj == null) {
|
41 |
|
|
return false;
|
42 |
|
|
}
|
43 |
|
|
if (this.getClass() != obj.getClass()) {
|
44 |
|
|
return false;
|
45 |
|
|
}
|
46 |
|
|
Option other = (Option) obj;
|
47 |
|
|
return this.abbr.equals(other.abbr);
|
48 |
|
|
}
|
49 |
|
|
}
|
50 |
|
|
|
51 |
a5d16f0a
|
horkmi
|
|
52 |
|
|
|
53 |
49fd8648
|
Michal Horký
|
private static HashMap<Option, Runnable> options;
|
54 |
|
|
private static boolean printJSON;
|
55 |
|
|
private static File outputFile;
|
56 |
|
|
|
57 |
|
|
static {
|
58 |
|
|
options = new HashMap<Option, Runnable>();
|
59 |
|
|
options.put(new Option("-?", "\tPrint this help message."), () -> {
|
60 |
|
|
help();
|
61 |
|
|
});
|
62 |
|
|
options.put(new Option("-help", "Print this help message."), () -> {
|
63 |
|
|
help();
|
64 |
|
|
});
|
65 |
a5d16f0a
|
horkmi
|
options.put(new Option("-print", "Prints the resulting json to the console."), () -> {
|
66 |
49fd8648
|
Michal Horký
|
setPrintJSON();
|
67 |
|
|
});
|
68 |
|
|
printJSON = false;
|
69 |
|
|
}
|
70 |
|
|
|
71 |
|
|
private static void help() {
|
72 |
|
|
System.out.println("\n"
|
73 |
|
|
+ "Usage:" + "\n"
|
74 |
|
|
+ " java ... -jar JOU_Deserializer.jar [-options] [args...]" + "\n"
|
75 |
|
|
+ "\n"
|
76 |
|
|
+ "...:" + "\n"
|
77 |
|
|
+ " When running the app in the JVM 11, the following VM arguments may be required:" + "\n"
|
78 |
|
|
+ " --module-path libs\\javafx-sdk-11.0.2\\lib --add-modules=javafx.controls" + "\n"
|
79 |
|
|
+ "\n"
|
80 |
|
|
+ "-options:" + "\n"
|
81 |
|
|
+ printOptions()
|
82 |
|
|
+ "\n"
|
83 |
|
|
+ "args...:" + "\n"
|
84 |
|
|
+ " Input and output file. Output file need not be specified, if options contains -print." + "\n"
|
85 |
|
|
+ "\n"
|
86 |
|
|
+ "Note: if no options and arguments are specified, the GUI is created." + "\n"
|
87 |
|
|
+ "\n"
|
88 |
|
|
+ "Example:" + "\n"
|
89 |
|
|
+ " CLI: java -jar JOU_Deserializer.jar -print input.data output.json" + "\n"
|
90 |
|
|
+ " GUI: java -jar JOU_Deserializer.jar" + "\n");
|
91 |
|
|
}
|
92 |
|
|
|
93 |
|
|
private static void setPrintJSON() {
|
94 |
|
|
printJSON = true;
|
95 |
|
|
}
|
96 |
a5d16f0a
|
horkmi
|
|
97 |
|
|
private static String printOptions() {
|
98 |
|
|
String result = "";
|
99 |
|
|
for (Option option : options.keySet()) {
|
100 |
|
|
result += " " + option + "\n";
|
101 |
|
|
}
|
102 |
|
|
return result;
|
103 |
|
|
}
|
104 |
49fd8648
|
Michal Horký
|
|
105 |
a5d16f0a
|
horkmi
|
|
106 |
|
|
|
107 |
49fd8648
|
Michal Horký
|
public static void main(String[] args) {
|
108 |
|
|
if (args.length == 0) {
|
109 |
d2be23a3
|
horkmi
|
Application.launch(Window.class, args);
|
110 |
49fd8648
|
Michal Horký
|
} else {
|
111 |
|
|
String input = null, output = null;
|
112 |
|
|
|
113 |
|
|
for (int i = 0; i < args.length; i++) {
|
114 |
|
|
Option o = new Option(args[i]);
|
115 |
|
|
if (options.containsKey(o)) {
|
116 |
|
|
options.get(o).run();
|
117 |
|
|
} else {
|
118 |
|
|
input = args[i];
|
119 |
|
|
if (i == args.length - 1) {
|
120 |
|
|
// If we are at the end of the list, check printJSON.
|
121 |
|
|
if (!printJSON) {
|
122 |
|
|
errorMessage("output not defined");
|
123 |
|
|
return;
|
124 |
|
|
}
|
125 |
|
|
} else {
|
126 |
|
|
output = args[i + 1];
|
127 |
|
|
break;
|
128 |
|
|
}
|
129 |
|
|
}
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
if (input == null) {
|
133 |
|
|
// Only options.
|
134 |
|
|
return;
|
135 |
|
|
}
|
136 |
|
|
|
137 |
|
|
File inputFile = new File(input);
|
138 |
|
|
if (!inputFile.exists()) {
|
139 |
|
|
errorMessage("the input file does not exist");
|
140 |
|
|
return;
|
141 |
|
|
}
|
142 |
|
|
outputFile = output == null ? null : new File(output);
|
143 |
|
|
|
144 |
|
|
Converter thread = new Converter(new CLI());
|
145 |
|
|
thread.start();
|
146 |
7e0b699a
|
horkmi
|
thread.setInput(inputFile, null);
|
147 |
a5d16f0a
|
horkmi
|
try {
|
148 |
|
|
Thread.sleep(1000);
|
149 |
|
|
} catch (InterruptedException e) {
|
150 |
|
|
errorMessage("interrupted sleep of the main thread");
|
151 |
|
|
}
|
152 |
49fd8648
|
Michal Horký
|
thread.end();
|
153 |
|
|
try {
|
154 |
|
|
thread.join();
|
155 |
|
|
} catch (InterruptedException e) {
|
156 |
|
|
errorMessage("while waiting for the converter thread");
|
157 |
|
|
}
|
158 |
|
|
}
|
159 |
|
|
}
|
160 |
|
|
|
161 |
|
|
private static void errorMessage(String message) {
|
162 |
|
|
System.out.println("\nERROR: " + message.toUpperCase() + "!\n");
|
163 |
|
|
}
|
164 |
|
|
|
165 |
a5d16f0a
|
horkmi
|
|
166 |
|
|
|
167 |
49fd8648
|
Michal Horký
|
@Override
|
168 |
|
|
public void loadingInputFileError() {
|
169 |
a5d16f0a
|
horkmi
|
errorMessage("there was an error loading the file");
|
170 |
49fd8648
|
Michal Horký
|
}
|
171 |
|
|
|
172 |
|
|
@Override
|
173 |
c670fa2f
|
@havlijan17
|
public void completed(String json, String loaded) {
|
174 |
e608f492
|
horkmi
|
if (json != null) {
|
175 |
49fd8648
|
Michal Horký
|
System.out.println("Deserialization completed.");
|
176 |
|
|
saveJson(json);
|
177 |
|
|
} else {
|
178 |
a5d16f0a
|
horkmi
|
errorMessage("an error occurred while deserializing");
|
179 |
e608f492
|
horkmi
|
if (loaded != null) {
|
180 |
|
|
System.out.println("The following actions (print, save) will work with the extracted content of the file.");
|
181 |
|
|
saveJson(loaded);
|
182 |
|
|
} else {
|
183 |
|
|
// This shouldn't happen, it's catched by the loadingInputFileError().
|
184 |
|
|
System.out.println("The content read from the file is null.");
|
185 |
|
|
}
|
186 |
49fd8648
|
Michal Horký
|
}
|
187 |
|
|
}
|
188 |
|
|
|
189 |
|
|
private void saveJson(String json) {
|
190 |
|
|
if (outputFile != null) {
|
191 |
|
|
try {
|
192 |
|
|
FileWorker.saveJson(outputFile, json);
|
193 |
|
|
System.out.println("The JSON file was saved correctly.");
|
194 |
|
|
} catch (IOException e) {
|
195 |
a5d16f0a
|
horkmi
|
errorMessage("there was an error saving the JSON file.");
|
196 |
49fd8648
|
Michal Horký
|
}
|
197 |
|
|
} else {
|
198 |
|
|
// Options contain -print.
|
199 |
|
|
System.out.println(json);
|
200 |
|
|
}
|
201 |
|
|
}
|
202 |
|
|
|
203 |
|
|
}
|