Projekt

Obecné

Profil

Stáhnout (6.05 KB) Statistiky
| Větev: | Tag: | Revize:
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 967b6b05 horkmi
/**
9
 * Command-line user interface.
10
 */
11 49fd8648 Michal Horký
public class CLI implements IConversionResults {
12
13 967b6b05 horkmi
	/**
14
	 * Instances of this class define options for running the application.
15
	 */
16 49fd8648 Michal Horký
	private static class Option {
17 967b6b05 horkmi
18
		/**
19
		 * Abbreviation for the option name.
20
		 */
21 49fd8648 Michal Horký
		String abbr;
22 967b6b05 horkmi
23
		/**
24
		 * Text describing the activity for the option.
25
		 */
26 49fd8648 Michal Horký
		String about;
27
28 967b6b05 horkmi
		/**
29
		 * Converts an input argument to the option for comparison with options in the
30
		 * list.
31
		 * 
32
		 * @param abbr input argument.
33
		 */
34 49fd8648 Michal Horký
		public Option(String abbr) {
35
			this.abbr = abbr;
36
			this.about = null;
37
		}
38 967b6b05 horkmi
39
		/**
40
		 * Creates an option to the option list.
41
		 * 
42
		 * @param abbr  option abbreviation.
43
		 * @param about option description.
44
		 */
45 49fd8648 Michal Horký
		public Option(String abbr, String about) {
46
			this.abbr = abbr;
47
			this.about = about;
48
		}
49 967b6b05 horkmi
50 49fd8648 Michal Horký
		@Override
51
		public String toString() {
52
			return abbr + "\t" + about;
53
		}
54
55
		@Override
56
		public int hashCode() {
57
			return abbr.hashCode();
58
		}
59
60
		@Override
61
		public boolean equals(Object obj) {
62
			if (this == obj) {
63
				return true;
64
			}
65
			if (obj == null) {
66
				return false;
67
			}
68
			if (this.getClass() != obj.getClass()) {
69
				return false;
70
			}
71
			Option other = (Option) obj;
72
			return this.abbr.equals(other.abbr);
73
		}
74
	}
75
76 967b6b05 horkmi
	/**
77
	 * The list of options for running the application.
78
	 */
79 49fd8648 Michal Horký
	private static HashMap<Option, Runnable> options;
80 967b6b05 horkmi
81
	/**
82
	 * Whether the resulting JSON should be written to the command line.
83
	 */
84 49fd8648 Michal Horký
	private static boolean printJSON;
85 967b6b05 horkmi
86
	/**
87
	 * Where to save the resulting JSON.
88
	 */
89 49fd8648 Michal Horký
	private static File outputFile;
90
91 967b6b05 horkmi
	/**
92
	 * Initialization of static variables.
93
	 */
94 49fd8648 Michal Horký
	static {
95
		options = new HashMap<Option, Runnable>();
96
		options.put(new Option("-?", "\tPrint this help message."), () -> {
97
			help();
98
		});
99
		options.put(new Option("-help", "Print this help message."), () -> {
100
			help();
101
		});
102 a5d16f0a horkmi
		options.put(new Option("-print", "Prints the resulting json to the console."), () -> {
103 49fd8648 Michal Horký
			setPrintJSON();
104
		});
105
		printJSON = false;
106
	}
107
108 967b6b05 horkmi
	/**
109
	 * Prints help for running the application.
110
	 */
111 49fd8648 Michal Horký
	private static void help() {
112 967b6b05 horkmi
		System.out.println("\n" + "Usage:" + "\n" + "    java ... -jar JOU_Deserializer.jar [-options] [args...]" + "\n"
113
				+ "\n" + "...:" + "\n"
114 49fd8648 Michal Horký
				+ "    When running the app in the JVM 11, the following VM arguments may be required:" + "\n"
115 967b6b05 horkmi
				+ "        --module-path libs\\javafx-sdk-11.0.2\\lib --add-modules=javafx.controls" + "\n" + "\n"
116
				+ "-options:" + "\n" + printOptions() + "\n" + "args...:" + "\n"
117 49fd8648 Michal Horký
				+ "    Input and output file. Output file need not be specified, if options contains -print." + "\n"
118 967b6b05 horkmi
				+ "\n" + "Note: if no options and arguments are specified, the GUI is created." + "\n" + "\n"
119
				+ "Example:" + "\n" + "    CLI: java -jar JOU_Deserializer.jar -print input.data output.json" + "\n"
120 49fd8648 Michal Horký
				+ "    GUI: java -jar JOU_Deserializer.jar" + "\n");
121
	}
122 967b6b05 horkmi
123
	/**
124
	 * Sets printJSON to true.
125
	 */
126 49fd8648 Michal Horký
	private static void setPrintJSON() {
127
		printJSON = true;
128
	}
129 967b6b05 horkmi
130
	/**
131
	 * @return string with all possible options for running the application.
132
	 */
133 a5d16f0a horkmi
	private static String printOptions() {
134
		String result = "";
135
		for (Option option : options.keySet()) {
136
			result += "    " + option + "\n";
137
		}
138
		return result;
139
	}
140 49fd8648 Michal Horký
141 967b6b05 horkmi
	/**
142
	 * Main method.
143
	 * 
144
	 * @param args input arguments.
145
	 */
146 49fd8648 Michal Horký
	public static void main(String[] args) {
147
		if (args.length == 0) {
148 967b6b05 horkmi
			// If no arguments exist, launch the GUI.
149 d2be23a3 horkmi
			Application.launch(Window.class, args);
150 49fd8648 Michal Horký
		} else {
151 967b6b05 horkmi
			// Command-line user interface only.
152 49fd8648 Michal Horký
			String input = null, output = null;
153 967b6b05 horkmi
154
			// Get options and input parameters.
155 49fd8648 Michal Horký
			for (int i = 0; i < args.length; i++) {
156
				Option o = new Option(args[i]);
157
				if (options.containsKey(o)) {
158
					options.get(o).run();
159
				} else {
160
					input = args[i];
161
					if (i == args.length - 1) {
162
						// If we are at the end of the list, check printJSON.
163
						if (!printJSON) {
164
							errorMessage("output not defined");
165
							return;
166
						}
167
					} else {
168
						output = args[i + 1];
169
						break;
170
					}
171
				}
172
			}
173 967b6b05 horkmi
174 49fd8648 Michal Horký
			if (input == null) {
175
				// Only options.
176
				return;
177
			}
178 967b6b05 horkmi
179
			// Set input and output file.
180 49fd8648 Michal Horký
			File inputFile = new File(input);
181
			if (!inputFile.exists()) {
182
				errorMessage("the input file does not exist");
183
				return;
184
			}
185
			outputFile = output == null ? null : new File(output);
186 967b6b05 horkmi
187
			// Create and run converter.
188 49fd8648 Michal Horký
			Converter thread = new Converter(new CLI());
189
			thread.start();
190 7e0b699a horkmi
			thread.setInput(inputFile, null);
191 a5d16f0a horkmi
			try {
192
				Thread.sleep(1000);
193
			} catch (InterruptedException e) {
194
				errorMessage("interrupted sleep of the main thread");
195
			}
196 49fd8648 Michal Horký
			thread.end();
197
			try {
198
				thread.join();
199
			} catch (InterruptedException e) {
200
				errorMessage("while waiting for the converter thread");
201
			}
202
		}
203
	}
204 967b6b05 horkmi
205
	/**
206
	 * Prints an error message.
207
	 * 
208
	 * @param message error message.
209
	 */
210 49fd8648 Michal Horký
	private static void errorMessage(String message) {
211
		System.out.println("\nERROR: " + message.toUpperCase() + "!\n");
212
	}
213 967b6b05 horkmi
214 49fd8648 Michal Horký
	@Override
215
	public void loadingInputFileError() {
216 a5d16f0a horkmi
		errorMessage("there was an error loading the file");
217 49fd8648 Michal Horký
	}
218
219
	@Override
220 c670fa2f @havlijan17
	public void completed(String json, String loaded) {
221 e608f492 horkmi
		if (json != null) {
222 49fd8648 Michal Horký
			System.out.println("Deserialization completed.");
223
			saveJson(json);
224
		} else {
225 a5d16f0a horkmi
			errorMessage("an error occurred while deserializing");
226 e608f492 horkmi
			if (loaded != null) {
227 967b6b05 horkmi
				System.out.println(
228
						"The following actions (print, save) will work with the extracted content of the file.");
229 e608f492 horkmi
				saveJson(loaded);
230
			} else {
231
				// This shouldn't happen, it's catched by the loadingInputFileError().
232
				System.out.println("The content read from the file is null.");
233
			}
234 49fd8648 Michal Horký
		}
235
	}
236 967b6b05 horkmi
237
	/**
238
	 * Saves the resulting text (JSON) to the file or prints it to the command line.
239
	 * 
240
	 * @param json the resulting text in JSON format.
241
	 */
242 49fd8648 Michal Horký
	private void saveJson(String json) {
243
		if (outputFile != null) {
244
			try {
245
				FileWorker.saveJson(outputFile, json);
246
				System.out.println("The JSON file was saved correctly.");
247
			} catch (IOException e) {
248 a5d16f0a horkmi
				errorMessage("there was an error saving the JSON file.");
249 49fd8648 Michal Horký
			}
250
		} else {
251
			// Options contain -print.
252
			System.out.println(json);
253
		}
254
	}
255 967b6b05 horkmi
256 49fd8648 Michal Horký
}