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
|
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>Everge.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">everge</a> > <a href="index.source.html" class="el_package">bjc.everge</a> > <span class="el_source">Everge.java</span></div><h1>Everge.java</h1><pre class="source lang-java linenums">package bjc.everge;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.*;
import java.util.*;
import java.util.concurrent.locks.*;
import java.util.regex.*;
/**
* Everge front-end application.
*
* @author Ben Culkin
*/
<span class="fc" id="L20">public class Everge {</span>
/**
* Details how we handle our input.
*/
<span class="fc" id="L24"> public static enum InputStatus {</span>
/**
* Process the input as a single string.
*/
<span class="fc" id="L28"> ALL,</span>
/**
* Process the input line-by-line.
*/
<span class="fc" id="L32"> LINE,</span>
/**
* Process the input, splitting it around occurances of a regex.
*/
<span class="fc" id="L36"> REGEX;</span>
}
// Options for doing repl-pairs
<span class="fc" id="L40"> private ReplOpts ropts = new ReplOpts();</span>
// Loaded repl-pairs
<span class="fc" id="L43"> private List<ReplPair> lrp = new ArrayList<>();</span>
// Input status
<span class="fc" id="L46"> private InputStatus inputStat = InputStatus.ALL;</span>
// Are we processing CLI args? (haven't seen a -- yet)
<span class="fc" id="L49"> private boolean doingArgs = true;</span>
// Should an NL be printed after each replace?
<span class="fc" id="L52"> private boolean printNL = true;</span>
// Verbosity level
<span class="fc" id="L55"> private int verbosity = 0;</span>
// The pattern to use for REGEX input mode
private String pattern;
// The queue of arguments to process
<span class="fc" id="L61"> private Deque<String> argQue = new LinkedList<>();</span>
// Used to prevent inter-mixing argument alterations with input processing.
<span class="fc" id="L64"> private ReadWriteLock argLock = new ReentrantReadWriteLock();</span>
// Input/output streams
/**
* Stream to use for normal output.
*/
<span class="fc" id="L70"> public PrintStream outStream = System.out;</span>
/**
* Stream to use for error output.
*/
<span class="fc" id="L74"> public PrintStream errStream = System.err;</span>
/**
* Main method for front end,
*
* @param args
* The CLI arguments.
*/
public static void main(String[] args) {
<span class="nc" id="L83"> Everge evg = new Everge();</span>
<span class="nc" id="L85"> evg.processArgs(args);</span>
<span class="nc" id="L86"> }</span>
/**
* Process one or more command line arguments.
*
* @param args
* The arguments to process.
* @return Whether we processed succesfully or not.
*/
public boolean processArgs(String... args) {
<span class="nc" id="L96"> List<String> errs = new ArrayList<>();</span>
<span class="nc" id="L98"> boolean stat = processArgs(errs, args);</span>
<span class="nc bnc" id="L99" title="All 2 branches missed."> if (!stat) {</span>
<span class="nc bnc" id="L100" title="All 2 branches missed."> for (String err : errs) {</span>
<span class="nc" id="L101"> errStream.println(err);</span>
<span class="nc" id="L102"> }</span>
}
<span class="nc" id="L105"> return stat;</span>
}
/**
* 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 succesfully or not.
*/
public boolean processArgs(List<String> errs, String... args) {
<span class="fc" id="L118"> argLock.writeLock().lock();</span>
<span class="fc" id="L120"> boolean retStat = true;</span>
try {
<span class="fc" id="L123"> loadQueue(args);</span>
// Process CLI args
<span class="fc bfc" id="L126" title="All 2 branches covered."> while(argQue.size() > 0) {</span>
<span class="fc" id="L127"> String arg = argQue.pop();</span>
<span class="pc bpc" id="L129" title="1 of 2 branches missed."> if (arg.equals("--")) {</span>
<span class="nc" id="L130"> doingArgs = false;</span>
<span class="nc" id="L131"> continue;</span>
}
// Process an argument
<span class="pc bpc" id="L135" title="1 of 4 branches missed."> if (doingArgs && arg.startsWith("-")) {</span>
<span class="fc" id="L136"> String argName = arg;</span>
<span class="fc" id="L137"> String argBody = "";</span>
// Process arguments to arguments
<span class="fc" id="L140"> int idx = arg.indexOf("=");</span>
<span class="pc bpc" id="L141" title="1 of 2 branches missed."> if (idx != -1) {</span>
<span class="nc" id="L142"> argName = arg.substring(0, idx);</span>
<span class="nc" id="L143"> argBody = arg.substring(idx + 1);</span>
}
<span class="pc bpc" id="L146" title="11 of 13 branches missed."> switch (argName) {</span>
case "-n":
case "--newline":
<span class="nc" id="L149"> printNL = true;</span>
<span class="nc" id="L150"> break;</span>
case "-N":
case "--no-newline":
<span class="nc" id="L153"> printNL = false;</span>
<span class="nc" id="L154"> break;</span>
case "-v":
case "--verbose":
<span class="fc" id="L157"> verbosity += 1;</span>
<span class="fc" id="L158"> break;</span>
case "-q":
case "--quiet":
<span class="nc" id="L161"> verbosity -= 1;</span>
<span class="nc" id="L162"> break;</span>
case "--verbosity":
<span class="nc bnc" id="L164" title="All 2 branches missed."> if (argQue.size() < 1) {</span>
<span class="nc" id="L165"> errs.add("[ERROR] No parameter to --verbosity");</span>
<span class="nc" id="L166"> retStat = false;</span>
<span class="nc" id="L167"> break;</span>
}
<span class="nc" id="L169"> argBody = argQue.pop();</span>
<span class="nc" id="L170"> break;</span>
case "-V":
try {
<span class="nc" id="L173"> verbosity = Integer.parseInt(argBody);</span>
<span class="nc" id="L174"> } catch (NumberFormatException nfex) {</span>
<span class="nc" id="L175"> String msg = String.format("[ERROR] Invalid verbosity: '%s' is not an integer",</span>
argBody);
<span class="nc" id="L177"> errs.add(msg);</span>
<span class="nc" id="L178"> retStat = false;</span>
<span class="nc" id="L179"> }</span>
<span class="nc" id="L180"> break;</span>
case "--pattern":
<span class="nc bnc" id="L182" title="All 2 branches missed."> if (argQue.size() < 1) {</span>
<span class="nc" id="L183"> errs.add("[ERROR] No parameter to --pattern");</span>
<span class="nc" id="L184"> retStat = false;</span>
<span class="nc" id="L185"> break;</span>
}
<span class="nc" id="L187"> argBody = argQue.pop();</span>
case "-p":
try {
<span class="nc" id="L190"> pattern = argBody;</span>
<span class="nc" id="L192"> Pattern.compile(argBody);</span>
<span class="nc" id="L193"> } catch (PatternSyntaxException psex) {</span>
<span class="nc" id="L194"> String msg = String.format("[ERROR] Pattern '%s' is invalid: %s",</span>
<span class="nc" id="L195"> pattern, psex.getMessage());</span>
<span class="nc" id="L196"> errs.add(msg);</span>
<span class="nc" id="L197"> retStat = false;</span>
<span class="nc" id="L198"> }</span>
<span class="nc" id="L199"> break;</span>
case "--file":
<span class="pc bpc" id="L201" title="1 of 2 branches missed."> if (argQue.size() < 1) {</span>
<span class="nc" id="L202"> errs.add("[ERROR] No argument to --file");</span>
<span class="nc" id="L203"> retStat = false;</span>
<span class="nc" id="L204"> break;</span>
}
<span class="fc" id="L206"> argBody = argQue.pop();</span>
case "-f":
<span class="fc" id="L208"> try (FileInputStream fis = new FileInputStream(argBody);</span>
<span class="fc" id="L209"> Scanner scn = new Scanner(fis)) {</span>
<span class="fc" id="L210"> List<ReplError> ferrs = new ArrayList<>();</span>
<span class="fc" id="L212"> lrp = ReplPair.readList(lrp, scn, ferrs, ropts);</span>
<span class="pc bpc" id="L214" title="1 of 2 branches missed."> if (ferrs.size() > 0) {</span>
<span class="nc" id="L215"> StringBuilder sb = new StringBuilder();</span>
<span class="nc" id="L217"> String errString = "an error";</span>
<span class="nc bnc" id="L218" title="All 2 branches missed."> if (ferrs.size() > 1) errString = String.format("%d errors");</span>
{
<span class="nc" id="L221"> String msg = String.format(</span>
"[ERROR] Encountered %s parsing data file'%s'\n",
errString, argBody);
<span class="nc" id="L224"> sb.append(msg);</span>
}
<span class="nc bnc" id="L227" title="All 2 branches missed."> for (ReplError err : ferrs) {</span>
<span class="nc" id="L228"> sb.append(String.format("\t%s\n", err));</span>
<span class="nc" id="L229"> }</span>
<span class="nc" id="L231"> errs.add(sb.toString());</span>
<span class="nc" id="L232"> retStat = false;</span>
}
<span class="nc" id="L234"> } catch (FileNotFoundException fnfex) {</span>
<span class="nc" id="L235"> String msg = String.format("[ERROR] Could not open data file '%s' for input",</span>
argBody);
<span class="nc" id="L237"> errs.add(msg);</span>
<span class="nc" id="L238"> retStat = false;</span>
<span class="nc" id="L239"> } catch (IOException ioex) {</span>
<span class="nc" id="L240"> String msg = String.format("[ERROR] Unknown I/O error reading data file '%s': %s",</span>
<span class="nc" id="L241"> argBody, ioex.getMessage());</span>
<span class="nc" id="L242"> errs.add(msg);</span>
<span class="nc" id="L243"> retStat = false;</span>
<span class="pc" id="L244"> }</span>
<span class="nc" id="L245"> break;</span>
case "--arg-file":
<span class="nc bnc" id="L247" title="All 2 branches missed."> if (argQue.size() < 1) {</span>
<span class="nc" id="L248"> errs.add("[ERROR] No argument to --arg-file");</span>
<span class="nc" id="L249"> break;</span>
}
<span class="nc" id="L251"> argBody = argQue.pop();</span>
case "-F":
<span class="nc" id="L253"> try (FileInputStream fis = new FileInputStream(argBody);</span>
<span class="nc" id="L254"> Scanner scn = new Scanner(fis)) {</span>
<span class="nc" id="L255"> List<String> sl = new ArrayList<>();</span>
<span class="nc bnc" id="L257" title="All 2 branches missed."> while (scn.hasNextLine()) {</span>
<span class="nc" id="L258"> String ln = scn.nextLine().trim();</span>
<span class="nc bnc" id="L260" title="All 2 branches missed."> if (ln.equals("")) continue;</span>
<span class="nc bnc" id="L261" title="All 2 branches missed."> if (ln.startsWith("#")) continue;</span>
<span class="nc" id="L263"> sl.add(ln);</span>
<span class="nc" id="L264"> }</span>
<span class="nc" id="L266"> processArgs(sl.toArray(new String[0]));</span>
<span class="nc" id="L267"> } catch (FileNotFoundException fnfex) {</span>
<span class="nc" id="L268"> String msg = String.format("[ERROR] Could not open argument file '%s' for input", argBody);</span>
<span class="nc" id="L269"> errs.add(msg);</span>
<span class="nc" id="L270"> retStat = false;</span>
<span class="nc" id="L271"> } catch (IOException ioex) {</span>
<span class="nc" id="L272"> String msg = String.format("[ERROR] Unknown I/O error reading input file '%s': %s",</span>
<span class="nc" id="L273"> argBody, ioex.getMessage());</span>
<span class="nc" id="L274"> errs.add(msg);</span>
<span class="nc" id="L275"> retStat = false;</span>
<span class="nc" id="L276"> }</span>
<span class="nc" id="L277"> break;</span>
default:
{
<span class="nc" id="L280"> String msg = String.format("[ERROR] Unrecognised CLI argument name '%s'\n", argName);</span>
<span class="nc" id="L281"> errs.add(msg);</span>
<span class="nc" id="L282"> retStat = false;</span>
}
}
<span class="fc" id="L285"> } else {</span>
// Strip off an escaped initial dash
<span class="pc bpc" id="L287" title="1 of 2 branches missed."> if (arg.startsWith("\\-")) arg = arg.substring(1);</span>
<span class="fc" id="L289"> processInputFile(arg);</span>
}
<span class="fc" id="L291"> }</span>
} finally {
<span class="fc" id="L293"> argLock.writeLock().unlock();</span>
}
<span class="fc" id="L296"> return retStat;</span>
}
/**
* Process a input file.
*
* @param fle
* Input file to process.
* @return Whether we processed succesfully or not.
*/
public boolean processInputFile(String fle) {
<span class="fc" id="L307"> List<String> errs = new ArrayList<>();</span>
<span class="fc" id="L309"> boolean stat = processInputFile(errs, fle);</span>
<span class="pc bpc" id="L310" title="1 of 2 branches missed."> if (!stat) {</span>
<span class="nc bnc" id="L311" title="All 2 branches missed."> for (String err : errs) {</span>
<span class="nc" id="L312"> errStream.println(err);</span>
<span class="nc" id="L313"> }</span>
}
<span class="fc" id="L316"> return stat;</span>
}
/**
* 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) {
<span class="fc" id="L329"> argLock.readLock().lock();</span>
// Read in and do replacements on a file
try {
<span class="pc bpc" id="L333" title="1 of 2 branches missed."> if (verbosity > 2) {</span>
<span class="nc" id="L334"> errStream.printf("[TRACE] Reading file (%s) in mode (%s)\n", fle, inputStat);</span>
}
<span class="pc bpc" id="L337" title="1 of 2 branches missed."> if (inputStat == InputStatus.ALL) {</span>
<span class="fc" id="L338"> Path pth = Paths.get(fle);</span>
<span class="pc bpc" id="L340" title="1 of 2 branches missed."> if (!Files.isReadable(pth)) {</span>
<span class="nc" id="L341"> String msg = String.format("[ERROR] File '%s' is not readable\n", fle);</span>
<span class="nc" id="L342"> errs.add(msg);</span>
<span class="nc" id="L343"> return false;</span>
}
<span class="fc" id="L346"> byte[] inp = Files.readAllBytes(pth);</span>
<span class="fc" id="L348"> String strang = new String(inp, Charset.forName("UTF-8"));</span>
<span class="fc" id="L350"> processString(strang);</span>
<span class="pc bnc" id="L351" title="All 2 branches missed."> } else if (inputStat == InputStatus.LINE) {</span>
<span class="nc" id="L352"> try (FileInputStream fis = new FileInputStream(fle); Scanner scn = new Scanner(fis)) {</span>
<span class="nc bnc" id="L353" title="All 2 branches missed."> while(scn.hasNextLine()) {</span>
<span class="nc" id="L354"> processString(scn.nextLine());</span>
}
<span class="nc" id="L356"> }</span>
<span class="nc bnc" id="L357" title="All 2 branches missed."> } else if (inputStat == InputStatus.REGEX) {</span>
<span class="nc" id="L358"> try (FileInputStream fis = new FileInputStream(fle); Scanner scn = new Scanner(fis)) {</span>
<span class="nc" id="L359"> scn.useDelimiter(pattern);</span>
<span class="nc bnc" id="L361" title="All 2 branches missed."> while(scn.hasNext()) {</span>
<span class="nc" id="L362"> processString(scn.next());</span>
}
<span class="nc" id="L364"> }</span>
} else {
<span class="nc" id="L366"> String msg = String.format("[INTERNAL-ERROR] Input status '%s' is not yet implemented\n",</span>
inputStat);
<span class="nc" id="L368"> errs.add(msg);</span>
<span class="nc" id="L369"> return false;</span>
}
<span class="nc" id="L371"> } catch (IOException ioex) {</span>
<span class="nc" id="L372"> String msg = String.format("[ERROR] Unknown I/O related error for file '%s'\n\tError was %s",</span>
<span class="nc" id="L373"> fle, ioex.getMessage());</span>
<span class="nc" id="L374"> errs.add(msg);</span>
<span class="nc" id="L375"> return false;</span>
} finally {
<span class="fc" id="L377"> argLock.readLock().unlock();</span>
}
<span class="fc" id="L380"> return true;</span>
}
/**
* Process an input string.
*
* @param inp
* The input string to process.
*/
public void processString(String inp) {
<span class="fc" id="L390"> argLock.readLock().lock();</span>
try {
<span class="fc" id="L393"> String strang = inp;</span>
<span class="fc bfc" id="L395" title="All 2 branches covered."> for (ReplPair rp : lrp) {</span>
<span class="fc" id="L396"> strang = rp.apply(strang);</span>
<span class="fc" id="L397"> }</span>
<span class="fc" id="L399"> outStream.print(strang);</span>
<span class="pc bpc" id="L400" title="1 of 2 branches missed."> if (printNL) outStream.println();</span>
} finally {
<span class="fc" id="L402"> argLock.readLock().unlock();</span>
}
<span class="fc" id="L404"> }</span>
// Load arguments into the argument queue.
private void loadQueue(String... args) {
<span class="fc" id="L408"> boolean doArgs = true;</span>
<span class="fc bfc" id="L409" title="All 2 branches covered."> for (String arg : args) {</span>
<span class="pc bpc" id="L410" title="1 of 2 branches missed."> if (arg.equals("--")) doArgs = false;</span>
// Handle things like -nNv correctly
<span class="pc bpc" id="L413" title="1 of 2 branches missed."> if (doArgs) {</span>
<span class="fc bfc" id="L414" title="All 4 branches covered."> if (arg.startsWith("-") && !arg.startsWith("--")) {</span>
<span class="fc" id="L415"> char[] car = arg.substring(1).toCharArray();</span>
<span class="fc bfc" id="L416" title="All 2 branches covered."> for (char c : car) {</span>
<span class="fc" id="L417"> String argstr = String.format("-%c", c);</span>
<span class="fc" id="L418"> argQue.add(argstr);</span>
}
<span class="fc" id="L420"> } else {</span>
<span class="fc" id="L421"> argQue.add(arg);</span>
}
} else {
<span class="nc" id="L424"> argQue.add(arg);</span>
}
}
<span class="fc" id="L427"> }</span>
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.2.201808211720</span></div></body></html>
|