Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 967b6b05

Přidáno uživatelem Michal Horký před asi 4 roky(ů)

re #7893

Refactoring defaultního a IO package + Javadoc.

Zobrazit rozdíly:

project/Deserializer/src/CLI.java
5 5
import io.FileWorker;
6 6
import javafx.application.Application;
7 7

  
8
/**
9
 * Command-line user interface.
10
 */
8 11
public class CLI implements IConversionResults {
9 12

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

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

  
23
		/**
24
		 * Text describing the activity for the option.
25
		 */
12 26
		String about;
13 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
		 */
14 34
		public Option(String abbr) {
15
			// Convert input argument to option.
16 35
			this.abbr = abbr;
17 36
			this.about = null;
18 37
		}
19
		
38

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

  
25 50
		@Override
26 51
		public String toString() {
27 52
			return abbr + "\t" + about;
......
48 73
		}
49 74
	}
50 75

  
51
	
52
	
76
	/**
77
	 * The list of options for running the application.
78
	 */
53 79
	private static HashMap<Option, Runnable> options;
80

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

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

  
91
	/**
92
	 * Initialization of static variables.
93
	 */
57 94
	static {
58 95
		options = new HashMap<Option, Runnable>();
59 96
		options.put(new Option("-?", "\tPrint this help message."), () -> {
......
68 105
		printJSON = false;
69 106
	}
70 107

  
108
	/**
109
	 * Prints help for running the application.
110
	 */
71 111
	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"
112
		System.out.println("\n" + "Usage:" + "\n" + "    java ... -jar JOU_Deserializer.jar [-options] [args...]" + "\n"
113
				+ "\n" + "...:" + "\n"
77 114
				+ "    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"
115
				+ "        --module-path libs\\javafx-sdk-11.0.2\\lib --add-modules=javafx.controls" + "\n" + "\n"
116
				+ "-options:" + "\n" + printOptions() + "\n" + "args...:" + "\n"
84 117
				+ "    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"
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"
90 120
				+ "    GUI: java -jar JOU_Deserializer.jar" + "\n");
91 121
	}
92
	
122

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

  
130
	/**
131
	 * @return string with all possible options for running the application.
132
	 */
97 133
	private static String printOptions() {
98 134
		String result = "";
99 135
		for (Option option : options.keySet()) {
......
102 138
		return result;
103 139
	}
104 140

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

  
154
			// Get options and input parameters.
113 155
			for (int i = 0; i < args.length; i++) {
114 156
				Option o = new Option(args[i]);
115 157
				if (options.containsKey(o)) {
......
128 170
					}
129 171
				}
130 172
			}
131
			
173

  
132 174
			if (input == null) {
133 175
				// Only options.
134 176
				return;
135 177
			}
136
			
178

  
179
			// Set input and output file.
137 180
			File inputFile = new File(input);
138 181
			if (!inputFile.exists()) {
139 182
				errorMessage("the input file does not exist");
140 183
				return;
141 184
			}
142 185
			outputFile = output == null ? null : new File(output);
143
			
186

  
187
			// Create and run converter.
144 188
			Converter thread = new Converter(new CLI());
145 189
			thread.start();
146 190
			thread.setInput(inputFile, null);
......
157 201
			}
158 202
		}
159 203
	}
160
	
204

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

  
167 214
	@Override
168 215
	public void loadingInputFileError() {
169 216
		errorMessage("there was an error loading the file");
......
177 224
		} else {
178 225
			errorMessage("an error occurred while deserializing");
179 226
			if (loaded != null) {
180
				System.out.println("The following actions (print, save) will work with the extracted content of the file.");
227
				System.out.println(
228
						"The following actions (print, save) will work with the extracted content of the file.");
181 229
				saveJson(loaded);
182 230
			} else {
183 231
				// This shouldn't happen, it's catched by the loadingInputFileError().
......
185 233
			}
186 234
		}
187 235
	}
188
	
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
	 */
189 242
	private void saveJson(String json) {
190 243
		if (outputFile != null) {
191 244
			try {
......
199 252
			System.out.println(json);
200 253
		}
201 254
	}
202
	
255

  
203 256
}

Také k dispozici: Unified diff