Projekt

Obecné

Profil

Stáhnout (4.5 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
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
	
52
	
53
	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
		options.put(new Option("-print", "Prints the resulting json to the console."), () -> {
66
			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
	
97
	private static String printOptions() {
98
		String result = "";
99
		for (Option option : options.keySet()) {
100
			result += "    " + option + "\n";
101
		}
102
		return result;
103
	}
104

    
105
	
106
	
107
	public static void main(String[] args) {
108
		if (args.length == 0) {
109
			Application.launch(Deserializer.class, args);
110
		} 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
			thread.setInput(inputFile);
147
			try {
148
				Thread.sleep(1000);
149
			} catch (InterruptedException e) {
150
				errorMessage("interrupted sleep of the main thread");
151
			}
152
			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
	
166
	
167
	@Override
168
	public void loadingInputFileError() {
169
		errorMessage("there was an error loading the file");
170
	}
171

    
172
	@Override
173
	public void completed(String html, String json, String loaded) {
174
		if (json != null && json.length() > 0) {
175
			System.out.println("Deserialization completed.");
176
			saveJson(json);
177
		} else {
178
			errorMessage("an error occurred while deserializing");
179
		}
180
	}
181
	
182
	private void saveJson(String json) {
183
		if (outputFile != null) {
184
			try {
185
				FileWorker.saveJson(outputFile, json);
186
				System.out.println("The JSON file was saved correctly.");
187
			} catch (IOException e) {
188
				errorMessage("there was an error saving the JSON file.");
189
			}
190
		} else {
191
			// Options contain -print.
192
			System.out.println(json);
193
		}
194
	}
195
	
196
}
(1-1/6)