summaryrefslogtreecommitdiff
path: root/src/main/java/bjc/everge/Everge.java
blob: 96b20ff9f3bbdfcc4eceadf48caf96e54f3f11d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
package bjc.everge;

import java.io.*;
import java.nio.charset.*;
import java.nio.file.*;
import java.util.*;
import java.util.concurrent.locks.*;
import java.util.regex.*;

import bjc.utils.ioutils.*;

/**
 * Everge front-end application.
 *
 * @author Ben Culkin
 */
public class Everge {
	/**
	 * Details how we handle our input.
	 */
	public static enum InputStatus {
		/**
		 * Process the input as a single string.
		 */
		ALL,
		/**
		 * Process the input line-by-line.
		 */
		LINE,
		/**
		 * Process the input, splitting it around occurrences of a regular expression.
		 */
		REGEX;
	}

	/**
	 *  Options for doing repl-pairs
	 */
	private ReplPairOptions replOptions = new ReplPairOptions();

	/**
	 * Repository for ReplPairs
	 */
	private ReplPairSet replSet = new ReplPairSet();

	/**
	 *  Input status.
	 *  
	 *  How the input to run replacements on should be processed.
	 */
	private InputStatus inputStatus = InputStatus.ALL;

	/**
	 *  Are we processing CLI args? (haven't seen a -- yet)
	 */
	private boolean doingArgs = true;

	/**
	 *  Should an NL be printed after each replace?
	 */
	private boolean printNLAfterReplace = true;

	/**
	 *  Verbosity level
	 */
	private int verbosity;

	/**
	 *  The pattern to use for REGEX input mode
	 */
	private String regexPattern;

	/**
	 *  Used to prevent inter-mixing argument alterations with input processing.
	 */
	private ReadWriteLock argLock = new ReentrantReadWriteLock();

	// Input/output streams
	/**
	 * Stream to use for normal output.
	 */
	private PrintStream outputStream = System.out;
	/**
	 * Stream to use for error output.
	 */
	private LogStream errorStream = new LogStream(System.err);

	/**
	 * Set the output stream.
	 * 
	 * @param out
	 *            The output stream..
	 */
	public void setOutput(PrintStream out) {
		outputStream = out;
	}

	/**
	 * Set the output stream.
	 * 
	 * @param out
	 *            The output stream..
	 */
	public void setOutput(OutputStream out) {
		setOutput(new PrintStream(out));
	}

	/**
	 * Set the error stream.
	 * 
	 * @param err
	 *            The error stream.
	 */
	public void setError(PrintStream err) {
		errorStream = new LogStream(err);
	}

	/**
	 * Set the error stream.
	 * 
	 * @param err
	 *            The error stream.
	 */
	public void setError(OutputStream err) {
		setError(new PrintStream(err));
	}

	/**
	 * Main method for front end,
	 *
	 * @param args
	 *             The CLI arguments.
	 */
	public static void main(String[] args) {
		Everge evg = new Everge();

		evg.processArgs(args);
	}

	/**
	 * Process one or more command line arguments.
	 *
	 * @param args
	 *             The arguments to process.
	 * @return Whether we processed successfully or not.
	 */
	public boolean processArgs(String... args) {
		List<String> errs = new ArrayList<>();

		boolean stat = processArgs(errs, args);
		if (verbosity >= 2) {
			String argString = args.length > 0 ? "arguments" : "argument";

			errorStream.infof("[INFO] Processed %d %s\n", args.length, argString);
			int argCount = 0;
			if (verbosity >= 3) {
				String arg = args[argCount++];
				errorStream.tracef("[TRACE]\tArg %d: '%s\n", argCount, arg);
			}
		}

		if (!stat)
			for (String err : errs) errorStream.errorf("%s\n", err);

		return stat;
	}

	/**
	 * Process one or more command line arguments.
	 *
	 * @param args
	 *             The arguments to process.
	 * @param errs
	 *             The list to stash errors in.
	 * @return Whether we processed successfully or not.
	 */
	public boolean processArgs(List<String> errs, String... args) {
		argLock.writeLock().lock();

		boolean returnStatus = true;

		try {
			Deque<String> argQueue = loadQueue(args);

			// Process CLI arguments
			while (argQueue.size() > 0) {
				String arg = argQueue.pop();

				returnStatus = processArg(errs, returnStatus, arg, argQueue);
			}
		} finally {
			argLock.writeLock().unlock();
		}

		return returnStatus;
	}

	private boolean processArg(List<String> errors, boolean retStat, String arg, Deque<String> argQueue) {
		boolean newReturnStatus = retStat;

		if (arg.equals("--")) {
			doingArgs = false;
			
			return newReturnStatus;
		}

		// Process an argument
		if (doingArgs && arg.startsWith("-")) {
			String argName = arg;
			String argBody = "";

			// Process 'joined' arguments (a=b)
			int idx = arg.indexOf("=");
			if (idx != -1) {
				argName = arg.substring(0, idx);
				argBody = arg.substring(idx + 1);
			}

			switch (argName) {
			case "-n":
			case "--newline":
				printNLAfterReplace = true;
				break;
			case "-N":
			case "--no-newline":
			case "--nonewline":
				printNLAfterReplace = false;
				break;
				
			case "-v":
			case "--verbose":
				verbosity += 1;
				errorStream.louder();
				//System.err.printf("[TRACE] Incremented verbosity\n");
				break;
			case "-q":
			case "--quiet":
				verbosity -= 1;
				errorStream.quieter();
				//System.err.printf("[TRACE] Decremented verbosity\n");
				break;
			case "--verbosity":
				if (argQueue.size() < 1) {
					errors.add("[ERROR] No parameter to --verbosity");
					newReturnStatus = false;
					break;
				}
				argBody = argQueue.pop();
			case "-V":
				try {
					verbosity = Integer.parseInt(argBody);
					errorStream.verbosity(verbosity);
					//System.err.printf("[TRACE] Set verbosity to %d\n", verbosity);
				} catch (NumberFormatException nfex) {
					String msg = String.format(
							"[ERROR] Invalid verbosity: '%s' is not an integer", argBody);
					errors.add(msg);
					newReturnStatus = false;
				}
				break;
				
			case "--pattern":
				if (argQueue.size() < 1) {
					errors.add("[ERROR] No parameter to --pattern");
					newReturnStatus = false;
					break;
				}
				argBody = argQueue.pop();
			case "-p":
				if (inputStatus != InputStatus.REGEX) 
					errorStream.warn("[WARN] Specified pattern will be ignored unless input mode is switched to REGEX");
				
				try {
					regexPattern = argBody;

					Pattern.compile(argBody);
				} catch (PatternSyntaxException psex) {
					String msg = String.format("[ERROR] Pattern '%s' is invalid: %s",
							regexPattern, psex.getMessage());
					errors.add(msg);
					newReturnStatus = false;
				}
				break;
				
			case "--file":
				if (argQueue.size() < 1) {
					errors.add("[ERROR] No argument to --file");
					newReturnStatus = false;
					break;
				}
				argBody = argQueue.pop();
			case "-f":
				try (FileInputStream fis = new FileInputStream(argBody);
						Scanner scn = new Scanner(fis)) {
					List<ReplPairError> ferrs = new ArrayList<>();

					List<ReplPair> pairList = new ArrayList<>();
					ReplPairParser parser = new ReplPairParser();
					pairList = parser.readList(pairList, scn, ferrs, replOptions);

					if (ferrs.size() > 0) {
						StringBuilder sb = new StringBuilder();

						String errString = "an error";
						if (ferrs.size() > 1)
							errString = String.format("%d errors", ferrs.size());

						
						String msg = String.format(
								"[ERROR] Encountered %s parsing data file'%s'\n",
								errString, argBody);
						sb.append(msg);
						
						for (ReplPairError err : ferrs)
							sb.append(String.format("\t%s\n", err));

						errors.add(sb.toString());
						newReturnStatus = false;
					}

					replSet.addPairs(pairList);
				} catch (FileNotFoundException fnfex) {
					String msg = String.format(
							"[ERROR] Could not open data file '%s' for input", argBody);
					errors.add(msg);
					newReturnStatus = false;
				} catch (IOException ioex) {
					String msg = String.format(
							"[ERROR] Unknown I/O error reading data file '%s': %s",
							argBody, ioex.getMessage());
					errors.add(msg);
					newReturnStatus = false;
				}
				break;
				
			case "--arg-file":
				if (argQueue.size() < 1) {
					errors.add("[ERROR] No argument to --arg-file");
					break;
				}
				argBody = argQueue.pop();
			case "-F":
				try (FileInputStream fis = new FileInputStream(argBody);
						Scanner scn = new Scanner(fis)) {
					List<String> sl = new ArrayList<>();

					while (scn.hasNextLine()) {
						String ln = scn.nextLine().trim();

						if (ln.equals(""))      continue;
						if (ln.startsWith("#")) continue;

						sl.add(ln);
					}

					// @FixMe :ArgFile
					// This won't work properly when using the 'non-inline' arguments
					// oops. It should. -- bculkin, Oct 31
					processArgs(sl.toArray(new String[0]));
				} catch (FileNotFoundException fnfex) {
					String msg = String.format(
							"[ERROR] Could not open argument file '%s' for input",
							argBody);
					errors.add(msg);
					newReturnStatus = false;
				} catch (IOException ioex) {
					String msg = String.format(
							"[ERROR] Unknown I/O error reading input file '%s': %s",
							argBody, ioex.getMessage());
					errors.add(msg);
					newReturnStatus = false;
				}
				break;
				
			case "--input-status":
				if (argQueue.size() < 1) {
					errors.add("[ERROR] No argument to --input-status");
					break;
				}
				argBody = argQueue.pop();
			case "-I":
				try {
					inputStatus = InputStatus.valueOf(argBody.toUpperCase());
				} catch (IllegalArgumentException iaex) {
					String msg = String.format("[ERROR] '%s' is not a valid input status",
							argBody);
					errors.add(msg);
				}
				break;
				
			default: {
				String msg = String.format(
						"[ERROR] Unrecognised CLI argument name '%s'\n", argName);
				errors.add(msg);
				newReturnStatus = false;
			}
			}
		} else {
			String tmp = arg;
			// Strip off an escaped initial dash
			if (tmp.startsWith("\\-")) tmp = tmp.substring(1);

			processInputFile(tmp);
		}

		return newReturnStatus;
	}

	/**
	 * Process a input file.
	 *
	 * @param fle
	 *            Input file to process.
	 * @return Whether we processed succesfully or not.
	 */
	public boolean processInputFile(String fle) {
		List<String> errs = new ArrayList<>();

		boolean stat = processInputFile(errs, fle);
		if (!stat) for (String err : errs) errorStream.errorf("%s\n", err);

		return stat;
	}

	/**
	 * Process a input file.
	 *
	 * @param fle
	 *             Input file to process.
	 * @param errs
	 *             List to accumulate errors in.
	 * @return Whether we processed succesfully or not.
	 */
	public boolean processInputFile(List<String> errs, String fle) {
		argLock.readLock().lock();

		// Read in and do replacements on a file
		try {
			if (verbosity > 2) {
				errorStream.printf("[TRACE] Reading file (%s) in mode (%s)\n", fle,
						inputStatus);
			}

			if (inputStatus == InputStatus.ALL) {
				Path pth = Paths.get(fle);

				if (!Files.isReadable(pth)) {
					String msg = String.format("[ERROR] File '%s' is not readable\n", fle);
					errs.add(msg);
					return false;
				}

				byte[] inp = Files.readAllBytes(pth);

				String strang = new String(inp, Charset.forName("UTF-8"));

				processString(strang);
			} else if (inputStatus == InputStatus.LINE) {
				try (
						FileInputStream fis = new FileInputStream(fle);
						Scanner scn = new Scanner(fis)) {
					while (scn.hasNextLine()) processString(scn.nextLine());
				}
			} else if (inputStatus == InputStatus.REGEX) {
				try (FileInputStream fis = new FileInputStream(fle);
						Scanner scn = new Scanner(fis)) {
					scn.useDelimiter(regexPattern);

					while (scn.hasNext()) processString(scn.next());
				}
			} else {
				String msg = String.format(
						"[INTERNAL-ERROR] Input status '%s' is not yet implemented\n",
						inputStatus);
				errs.add(msg);
				return false;
			}
		} catch (IOException ioex) {
			String msg = String.format(
					"[ERROR] Unknown I/O related error for file '%s'\n\tError was %s",
					fle, ioex.getMessage());
			errs.add(msg);
			return false;
		} finally {
			argLock.readLock().unlock();
		}

		return true;
	}

	/**
	 * Process an input string.
	 *
	 * @param inp
	 *            The input string to process.
	 */
	public void processString(String inp) {
		argLock.readLock().lock();

		try {
			String strang = inp;

			if (verbosity >= 3) {
				errorStream.infof(
						"[INFO] Processing replacements for string '%s' in mode %s\n",
						strang, inputStatus);
				
				if (!inp.equals(inp.trim())) {
					errorStream.infof("[INFO] String '%s' has trailing spaces on it\n", inp);
				}
			}

			strang = replSet.apply(inp);

			outputStream.print(strang);
			if (printNLAfterReplace) outputStream.println();
		} finally {
			argLock.readLock().unlock();
		}
	}

	// Load arguments into the argument queue.
	private Deque<String> loadQueue(String... args) {
		Deque<String> argQueue = new ArrayDeque<>(args.length);
		
		boolean doArgs = true;
		for (String arg : args) {
			if (arg.equals("--")) {
				doArgs = false;
				continue;
			}

			if (doArgs) {
				if (arg.startsWith("-") && !arg.startsWith("--")) {
					// Handle things like -nNv correctly
					char[] car = arg.substring(1).toCharArray();

					if (verbosity >= 3) {
						errorStream.infof("[INFO] Adding collection of single-char arguments: %s", car);
					}

					for (char c : car) {
						String argstr = String.format("-%c", c);
						argQueue.add(argstr);
					}
				} else {
					argQueue.add(arg);
				}
			} else {
				argQueue.add(arg);
			}
		}
		
		return argQueue;
	}
}