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 |
|
|
private static HashMap<Option, Runnable> options;
|
52 |
|
|
private static boolean printJSON;
|
53 |
|
|
private static File outputFile;
|
54 |
|
|
|
55 |
|
|
static {
|
56 |
|
|
options = new HashMap<Option, Runnable>();
|
57 |
|
|
options.put(new Option("-?", "\tPrint this help message."), () -> {
|
58 |
|
|
help();
|
59 |
|
|
});
|
60 |
|
|
options.put(new Option("-help", "Print this help message."), () -> {
|
61 |
|
|
help();
|
62 |
|
|
});
|
63 |
|
|
options.put(new Option("-print", "Text..."), () -> {
|
64 |
|
|
setPrintJSON();
|
65 |
|
|
});
|
66 |
|
|
printJSON = false;
|
67 |
|
|
}
|
68 |
|
|
|
69 |
|
|
private static String printOptions() {
|
70 |
|
|
String result = "";
|
71 |
|
|
for (Option option : options.keySet()) {
|
72 |
|
|
result += " " + option + "\n";
|
73 |
|
|
}
|
74 |
|
|
return result;
|
75 |
|
|
}
|
76 |
|
|
|
77 |
|
|
private static void help() {
|
78 |
|
|
System.out.println("\n"
|
79 |
|
|
+ "Usage:" + "\n"
|
80 |
|
|
+ " java ... -jar JOU_Deserializer.jar [-options] [args...]" + "\n"
|
81 |
|
|
+ "\n"
|
82 |
|
|
+ "...:" + "\n"
|
83 |
|
|
+ " When running the app in the JVM 11, the following VM arguments may be required:" + "\n"
|
84 |
|
|
+ " --module-path libs\\javafx-sdk-11.0.2\\lib --add-modules=javafx.controls" + "\n"
|
85 |
|
|
+ "\n"
|
86 |
|
|
+ "-options:" + "\n"
|
87 |
|
|
+ printOptions()
|
88 |
|
|
+ "\n"
|
89 |
|
|
+ "args...:" + "\n"
|
90 |
|
|
+ " Input and output file. Output file need not be specified, if options contains -print." + "\n"
|
91 |
|
|
+ "\n"
|
92 |
|
|
+ "Note: if no options and arguments are specified, the GUI is created." + "\n"
|
93 |
|
|
+ "\n"
|
94 |
|
|
+ "Example:" + "\n"
|
95 |
|
|
+ " CLI: java -jar JOU_Deserializer.jar -print input.data output.json" + "\n"
|
96 |
|
|
+ " GUI: java -jar JOU_Deserializer.jar" + "\n");
|
97 |
|
|
}
|
98 |
|
|
|
99 |
|
|
private static void setPrintJSON() {
|
100 |
|
|
printJSON = true;
|
101 |
|
|
}
|
102 |
|
|
|
103 |
|
|
public static void main(String[] args) {
|
104 |
|
|
if (args.length == 0) {
|
105 |
|
|
Application.launch(Deserializer.class, args);
|
106 |
|
|
} else {
|
107 |
|
|
String input = null, output = null;
|
108 |
|
|
|
109 |
|
|
for (int i = 0; i < args.length; i++) {
|
110 |
|
|
Option o = new Option(args[i]);
|
111 |
|
|
if (options.containsKey(o)) {
|
112 |
|
|
options.get(o).run();
|
113 |
|
|
} else {
|
114 |
|
|
input = args[i];
|
115 |
|
|
if (i == args.length - 1) {
|
116 |
|
|
// If we are at the end of the list, check printJSON.
|
117 |
|
|
if (!printJSON) {
|
118 |
|
|
errorMessage("output not defined");
|
119 |
|
|
return;
|
120 |
|
|
}
|
121 |
|
|
} else {
|
122 |
|
|
output = args[i + 1];
|
123 |
|
|
break;
|
124 |
|
|
}
|
125 |
|
|
}
|
126 |
|
|
}
|
127 |
|
|
|
128 |
|
|
if (input == null) {
|
129 |
|
|
// Only options.
|
130 |
|
|
return;
|
131 |
|
|
}
|
132 |
|
|
|
133 |
|
|
File inputFile = new File(input);
|
134 |
|
|
if (!inputFile.exists()) {
|
135 |
|
|
errorMessage("the input file does not exist");
|
136 |
|
|
return;
|
137 |
|
|
}
|
138 |
|
|
outputFile = output == null ? null : new File(output);
|
139 |
|
|
|
140 |
|
|
Converter thread = new Converter(new CLI());
|
141 |
|
|
thread.start();
|
142 |
|
|
thread.setInput(inputFile);
|
143 |
|
|
thread.end();
|
144 |
|
|
try {
|
145 |
|
|
thread.join();
|
146 |
|
|
} catch (InterruptedException e) {
|
147 |
|
|
errorMessage("while waiting for the converter thread");
|
148 |
|
|
}
|
149 |
|
|
}
|
150 |
|
|
}
|
151 |
|
|
|
152 |
|
|
private static void errorMessage(String message) {
|
153 |
|
|
System.out.println("\nERROR: " + message.toUpperCase() + "!\n");
|
154 |
|
|
}
|
155 |
|
|
|
156 |
|
|
@Override
|
157 |
|
|
public void loadingInputFileError() {
|
158 |
|
|
System.out.println("There was an error loading the file.");
|
159 |
|
|
}
|
160 |
|
|
|
161 |
|
|
@Override
|
162 |
|
|
public void completed(String json) {
|
163 |
|
|
if (json != null) {
|
164 |
|
|
System.out.println("Deserialization completed.");
|
165 |
|
|
saveJson(json);
|
166 |
|
|
} else {
|
167 |
|
|
System.out.println("An error occurred while deserializing!");
|
168 |
|
|
}
|
169 |
|
|
}
|
170 |
|
|
|
171 |
|
|
private void saveJson(String json) {
|
172 |
|
|
if (outputFile != null) {
|
173 |
|
|
try {
|
174 |
|
|
FileWorker.saveJson(outputFile, json);
|
175 |
|
|
System.out.println("The JSON file was saved correctly.");
|
176 |
|
|
} catch (IOException e) {
|
177 |
|
|
System.out.println("There was an error saving the JSON file.");
|
178 |
|
|
}
|
179 |
|
|
} else {
|
180 |
|
|
// Options contain -print.
|
181 |
|
|
System.out.println(json);
|
182 |
|
|
}
|
183 |
|
|
}
|
184 |
|
|
|
185 |
|
|
}
|