Projekt

Obecné

Profil

Stáhnout (6.05 KB) Statistiky
| Větev: | Tag: | Revize:
1
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
/**
9
 * Command-line user interface.
10
 */
11
public class CLI implements IConversionResults {
12

    
13
	/**
14
	 * Instances of this class define options for running the application.
15
	 */
16
	private static class Option {
17

    
18
		/**
19
		 * Abbreviation for the option name.
20
		 */
21
		String abbr;
22

    
23
		/**
24
		 * Text describing the activity for the option.
25
		 */
26
		String about;
27

    
28
		/**
29
		 * Converts an input argument to the option for comparison with options in the
30
		 * list.
31
		 * 
32
		 * @param abbr input argument.
33
		 */
34
		public Option(String abbr) {
35
			this.abbr = abbr;
36
			this.about = null;
37
		}
38

    
39
		/**
40
		 * Creates an option to the option list.
41
		 * 
42
		 * @param abbr  option abbreviation.
43
		 * @param about option description.
44
		 */
45
		public Option(String abbr, String about) {
46
			this.abbr = abbr;
47
			this.about = about;
48
		}
49

    
50
		@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
	/**
77
	 * The list of options for running the application.
78
	 */
79
	private static HashMap<Option, Runnable> options;
80

    
81
	/**
82
	 * Whether the resulting JSON should be written to the command line.
83
	 */
84
	private static boolean printJSON;
85

    
86
	/**
87
	 * Where to save the resulting JSON.
88
	 */
89
	private static File outputFile;
90

    
91
	/**
92
	 * Initialization of static variables.
93
	 */
94
	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
		options.put(new Option("-print", "Prints the resulting json to the console."), () -> {
103
			setPrintJSON();
104
		});
105
		printJSON = false;
106
	}
107

    
108
	/**
109
	 * Prints help for running the application.
110
	 */
111
	private static void help() {
112
		System.out.println("\n" + "Usage:" + "\n" + "    java ... -jar JOU_Deserializer.jar [-options] [args...]" + "\n"
113
				+ "\n" + "...:" + "\n"
114
				+ "    When running the app in the JVM 11, the following VM arguments may be required:" + "\n"
115
				+ "        --module-path libs\\javafx-sdk-11.0.2\\lib --add-modules=javafx.controls" + "\n" + "\n"
116
				+ "-options:" + "\n" + printOptions() + "\n" + "args...:" + "\n"
117
				+ "    Input and output file. Output file need not be specified, if options contains -print." + "\n"
118
				+ "\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
				+ "    GUI: java -jar JOU_Deserializer.jar" + "\n");
121
	}
122

    
123
	/**
124
	 * Sets printJSON to true.
125
	 */
126
	private static void setPrintJSON() {
127
		printJSON = true;
128
	}
129

    
130
	/**
131
	 * @return string with all possible options for running the application.
132
	 */
133
	private static String printOptions() {
134
		String result = "";
135
		for (Option option : options.keySet()) {
136
			result += "    " + option + "\n";
137
		}
138
		return result;
139
	}
140

    
141
	/**
142
	 * Main method.
143
	 * 
144
	 * @param args input arguments.
145
	 */
146
	public static void main(String[] args) {
147
		if (args.length == 0) {
148
			// If no arguments exist, launch the GUI.
149
			Application.launch(Window.class, args);
150
		} else {
151
			// Command-line user interface only.
152
			String input = null, output = null;
153

    
154
			// Get options and input parameters.
155
			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

    
174
			if (input == null) {
175
				// Only options.
176
				return;
177
			}
178

    
179
			// Set input and output file.
180
			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

    
187
			// Create and run converter.
188
			Converter thread = new Converter(new CLI());
189
			thread.start();
190
			thread.setInput(inputFile, null);
191
			try {
192
				Thread.sleep(1000);
193
			} catch (InterruptedException e) {
194
				errorMessage("interrupted sleep of the main thread");
195
			}
196
			thread.end();
197
			try {
198
				thread.join();
199
			} catch (InterruptedException e) {
200
				errorMessage("while waiting for the converter thread");
201
			}
202
		}
203
	}
204

    
205
	/**
206
	 * Prints an error message.
207
	 * 
208
	 * @param message error message.
209
	 */
210
	private static void errorMessage(String message) {
211
		System.out.println("\nERROR: " + message.toUpperCase() + "!\n");
212
	}
213

    
214
	@Override
215
	public void loadingInputFileError() {
216
		errorMessage("there was an error loading the file");
217
	}
218

    
219
	@Override
220
	public void completed(String json, String loaded) {
221
		if (json != null) {
222
			System.out.println("Deserialization completed.");
223
			saveJson(json);
224
		} else {
225
			errorMessage("an error occurred while deserializing");
226
			if (loaded != null) {
227
				System.out.println(
228
						"The following actions (print, save) will work with the extracted content of the file.");
229
				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
		}
235
	}
236

    
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
	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
				errorMessage("there was an error saving the JSON file.");
249
			}
250
		} else {
251
			// Options contain -print.
252
			System.out.println(json);
253
		}
254
	}
255

    
256
}
(1-1/6)