diff options
| author | Ben Culkin <scorpress@gmail.com> | 2020-04-13 18:45:06 -0400 |
|---|---|---|
| committer | Ben Culkin <scorpress@gmail.com> | 2020-04-13 18:45:06 -0400 |
| commit | 7bbc3b4c159eb50f6286ccbcc8da6af9f5a8fb06 (patch) | |
| tree | 24c628a479fc5ae790b2dbd292581bed6e149ed1 | |
| parent | 10b6688ef898285924026e695240a1de2f332bcb (diff) | |
Cleanup pass
Pass to do some cleanup
| -rw-r--r-- | src/main/java/bjc/everge/BadReplParse.java | 26 | ||||
| -rw-r--r-- | src/main/java/bjc/everge/ControlledString.java | 98 | ||||
| -rw-r--r-- | src/main/java/bjc/everge/Everge.java | 118 | ||||
| -rw-r--r-- | src/main/java/bjc/everge/IntHolder.java | 8 | ||||
| -rw-r--r-- | src/main/java/bjc/everge/LogStream.java | 37 | ||||
| -rw-r--r-- | src/main/java/bjc/everge/MirrorOutputStream.java | 5 | ||||
| -rw-r--r-- | src/main/java/bjc/everge/ReplError.java | 46 | ||||
| -rw-r--r-- | src/main/java/bjc/everge/ReplOpts.java | 32 | ||||
| -rw-r--r-- | src/main/java/bjc/everge/ReplPair.java | 477 | ||||
| -rw-r--r-- | src/main/java/bjc/everge/ReplSet.java | 20 | ||||
| -rw-r--r-- | src/main/java/bjc/everge/StageStatus.java | 2 | ||||
| -rw-r--r-- | src/main/java/bjc/everge/StringUtils.java | 60 | ||||
| -rw-r--r-- | src/test/java/bjc/everge/EvergeTest.java | 9 | ||||
| -rw-r--r-- | src/test/java/bjc/everge/ReplPairTest.java | 11 | ||||
| -rw-r--r-- | src/test/java/bjc/everge/StringUtilsTest.java | 10 | ||||
| -rw-r--r-- | src/test/java/bjc/everge/TestUtils.java | 50 |
16 files changed, 577 insertions, 432 deletions
diff --git a/src/main/java/bjc/everge/BadReplParse.java b/src/main/java/bjc/everge/BadReplParse.java index e3858af..0278e76 100644 --- a/src/main/java/bjc/everge/BadReplParse.java +++ b/src/main/java/bjc/everge/BadReplParse.java @@ -2,8 +2,10 @@ package bjc.everge; import java.util.ArrayList; import java.util.List; + /** * Exception thrown when ReplPair parsing fails + * * @author bjculkin * */ @@ -19,9 +21,9 @@ public class BadReplParse extends RuntimeException { /** * Create a new exception for ReplPair parsing failing. - * - * @param msg - * The message for the exception. + * + * @param msg + * The message for the exception. */ public BadReplParse(String msg) { this(msg, new ArrayList<>()); @@ -29,11 +31,11 @@ public class BadReplParse extends RuntimeException { /** * Create a new exception for ReplPair parsing failing. - * + * * @param msg - * The message for the exception. + * The message for the exception. * @param errs - * The list of errors encountered while parsing. + * The list of errors encountered while parsing. */ public BadReplParse(String msg, List<ReplError> errs) { super(msg); @@ -44,16 +46,18 @@ public class BadReplParse extends RuntimeException { @Override public String toString() { String errString; - if (errs.size() == 0) errString = "An error"; - else errString = "Errors"; + if (errs.size() == 0) + errString = "An error"; + else + errString = "Errors"; - return String.format("%s occured parsing replacement pairs: %s\n%s", - errString, getMessage(), errs); + return String.format("%s occured parsing replacement pairs: %s\n%s", errString, + getMessage(), errs); } /** * Convert the exception to a printable format. - * + * * @return The exception as a printable format. */ public String toPrintString() { diff --git a/src/main/java/bjc/everge/ControlledString.java b/src/main/java/bjc/everge/ControlledString.java index 691b792..aa1ea99 100644 --- a/src/main/java/bjc/everge/ControlledString.java +++ b/src/main/java/bjc/everge/ControlledString.java @@ -35,7 +35,7 @@ public class ControlledString { * Create a new argless control. * * @param nam - * The name of the control. + * The name of the control. */ public Control(String nam) { name = nam; @@ -45,9 +45,9 @@ public class ControlledString { * Create a new control. * * @param nam - * The name of the control. + * The name of the control. * @param ars - * The arguments of the control. + * The arguments of the control. */ public Control(String nam, String... ars) { name = nam; @@ -65,19 +65,22 @@ public class ControlledString { /** * Get an argument from the control. - * - * @param i The index of the argument to get. + * + * @param i + * The index of the argument to get. * @return The argument at that position. */ public String get(int i) { if (i < 0) { - String msg = String.format("Control argument index must be greater than 0 (was %d)", i); + String msg = String.format( + "Control argument index must be greater than 0 (was %d)", i); throw new IndexOutOfBoundsException(msg); } if (i > args.length) { - String msg = String.format("Control argument index must be less than %d (was %d)", + String msg = String.format( + "Control argument index must be less than %d (was %d)", args.length, i); throw new IndexOutOfBoundsException(msg); @@ -114,21 +117,35 @@ public class ControlledString { @Override public boolean equals(Object obj) { - if (this == obj) { return true; } - if (obj == null) { return false; } - if (getClass() != obj.getClass()) { return false; } + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } Control other = (Control) obj; if (name == null) { - if (other.name != null) { return false; } - } else if (!name.equals(other.name)) { return false; } + if (other.name != null) { + return false; + } + } else if (!name.equals(other.name)) { + return false; + } - boolean isArged = args != null && args.length > 0; + boolean isArged = args != null && args.length > 0; boolean oIsArged = other.args != null && other.args.length > 0; - if (isArged && !oIsArged) { return false; } - if (!isArged && oIsArged) { return false; } + if (isArged && !oIsArged) { + return false; + } + if (!isArged && oIsArged) { + return false; + } if (isArged && oIsArged) { return Arrays.equals(args, other.args); @@ -141,16 +158,16 @@ public class ControlledString { * Convenient static constructor for static imports. * * @param nam - * The name of the control. + * The name of the control. * @param ars - * The arguments to the control. + * The arguments to the control. * @return A control with the right parameters. */ public static Control C(String nam, String... ars) { return new Control(nam, ars); } } - + /** * Parameter class for defining how to parse a ControlledString. * @@ -181,15 +198,16 @@ public class ControlledString { * Create a new set of parse strings. * * @param contInd - * The control indicator. + * The control indicator. * @param contSep - * The control separator. + * The control separator. * @param contArg - * The argument separator. + * The argument separator. * @param contEsc - * The control escape. + * The control escape. */ - public ParseStrings(String contInd, String contSep, String contArg, String contEsc) { + public ParseStrings(String contInd, String contSep, String contArg, + String contEsc) { this.contInd = contInd; this.contSep = contSep; this.contArg = contArg; @@ -200,20 +218,21 @@ public class ControlledString { * Convenient static constructor. * * @param contInd - * The control indicator. + * The control indicator. * @param contSep - * The control separator. + * The control separator. * @param contArg - * The argument separator. + * The argument separator. * @param contEsc - * The control escape. + * The control escape. * @return A new set of control strings. */ - public static ParseStrings PS(String contInd, String contSep, String contArg, String contEsc) { + public static ParseStrings PS(String contInd, String contSep, String contArg, + String contEsc) { return new ParseStrings(contInd, contSep, contArg, contEsc); } } - + /** * The string the controls apply to. */ @@ -235,7 +254,7 @@ public class ControlledString { * Create a new controlled string without any controls. * * @param strung - * The string to use. + * The string to use. */ public ControlledString(String strung) { strang = strung; @@ -247,9 +266,9 @@ public class ControlledString { * Create a new controlled string. * * @param strung - * The string to use. + * The string to use. * @param controls - * The controls that apply to the string. + * The controls that apply to the string. */ public ControlledString(String strung, Control... controls) { strang = strung; @@ -281,13 +300,12 @@ public class ControlledString { * The controls must be parsed from the beginning of the string. * * @param lne - * The string to parse from. + * The string to parse from. * @param strangs - * The object to read the strings from + * The object to read the strings from * @return A parsed control string. */ - public static ControlledString parse(String lne, ParseStrings strangs) - { + public static ControlledString parse(String lne, ParseStrings strangs) { if (!lne.startsWith(strangs.contInd)) { return new ControlledString(lne); } @@ -299,10 +317,11 @@ public class ControlledString { msg = String.format(msg, strangs.contInd); throw new IllegalArgumentException(msg); - } + } ControlledString cs = new ControlledString(bits[0]); - if (bits.length > 2) cs.strang = bits[2]; + if (bits.length > 2) + cs.strang = bits[2]; bits = StringUtils.escapeSplit(strangs.contEsc, strangs.contSep, bits[1]); @@ -311,7 +330,8 @@ public class ControlledString { for (int i = 0; i < bits.length; i++) { String bit = bits[i]; - String[] bots = StringUtils.escapeSplit(strangs.contEsc, strangs.contArg, bit); + String[] bots + = StringUtils.escapeSplit(strangs.contEsc, strangs.contArg, bit); Control cont = new Control(bots[0]); diff --git a/src/main/java/bjc/everge/Everge.java b/src/main/java/bjc/everge/Everge.java index f061200..0070c22 100644 --- a/src/main/java/bjc/everge/Everge.java +++ b/src/main/java/bjc/everge/Everge.java @@ -70,7 +70,9 @@ public class Everge { /** * Set the output stream. - * @param out The output stream.. + * + * @param out + * The output stream.. */ public void setOutput(PrintStream out) { outStream = out; @@ -78,7 +80,9 @@ public class Everge { /** * Set the output stream. - * @param out The output stream.. + * + * @param out + * The output stream.. */ public void setOutput(OutputStream out) { outStream = new PrintStream(out); @@ -86,7 +90,9 @@ public class Everge { /** * Set the error stream. - * @param err The error stream. + * + * @param err + * The error stream. */ public void setError(PrintStream err) { errStream = new LogStream(err); @@ -94,7 +100,9 @@ public class Everge { /** * Set the error stream. - * @param err The error stream. + * + * @param err + * The error stream. */ public void setError(OutputStream err) { errStream = new LogStream(new PrintStream(err)); @@ -104,7 +112,7 @@ public class Everge { * Main method for front end, * * @param args - * The CLI arguments. + * The CLI arguments. */ public static void main(String[] args) { Everge evg = new Everge(); @@ -116,7 +124,7 @@ public class Everge { * Process one or more command line arguments. * * @param args - * The arguments to process. + * The arguments to process. * @return Whether we processed succesfully or not. */ public boolean processArgs(String... args) { @@ -147,9 +155,9 @@ public class Everge { * Process one or more command line arguments. * * @param args - * The arguments to process. + * The arguments to process. * @param errs - * The list to stash errors in. + * The list to stash errors in. * @return Whether we processed succesfully or not. */ public boolean processArgs(List<String> errs, String... args) { @@ -161,9 +169,9 @@ public class Everge { loadQueue(args); // Process CLI args - while(argQue.size() > 0) { + while (argQue.size() > 0) { String arg = argQue.pop(); - + retStat = processArg(errs, retStat, arg); } } finally { @@ -175,7 +183,7 @@ public class Everge { private boolean processArg(List<String> errs, boolean retStat, String arg) { boolean newRet = retStat; - + if (arg.equals("--")) { doingArgs = false; return newRet; @@ -186,7 +194,7 @@ public class Everge { String argName = arg; String argBody = ""; - // Process arguments to arguments + // Process arguments to arguments int idx = arg.indexOf("="); if (idx != -1) { argName = arg.substring(0, idx); @@ -227,8 +235,8 @@ public class Everge { errStream.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); + String msg = String.format( + "[ERROR] Invalid verbosity: '%s' is not an integer", argBody); errs.add(msg); newRet = false; } @@ -269,9 +277,10 @@ public class Everge { if (ferrs.size() > 0) { StringBuilder sb = new StringBuilder(); - + String errString = "an error"; - if (ferrs.size() > 1) errString = String.format("%d errors"); + if (ferrs.size() > 1) + errString = String.format("%d errors"); { String msg = String.format( @@ -290,12 +299,13 @@ public class Everge { replSet.addPairs(lrp); } catch (FileNotFoundException fnfex) { - String msg = String.format("[ERROR] Could not open data file '%s' for input", - argBody); + String msg = String.format( + "[ERROR] Could not open data file '%s' for input", argBody); errs.add(msg); newRet = false; } catch (IOException ioex) { - String msg = String.format("[ERROR] Unknown I/O error reading data file '%s': %s", + String msg = String.format( + "[ERROR] Unknown I/O error reading data file '%s': %s", argBody, ioex.getMessage()); errs.add(msg); newRet = false; @@ -315,19 +325,24 @@ public class Everge { while (scn.hasNextLine()) { String ln = scn.nextLine().trim(); - if (ln.equals("")) continue; - if (ln.startsWith("#")) continue; + if (ln.equals("")) + continue; + if (ln.startsWith("#")) + continue; sl.add(ln); } processArgs(sl.toArray(new String[0])); } catch (FileNotFoundException fnfex) { - String msg = String.format("[ERROR] Could not open argument file '%s' for input", argBody); + String msg = String.format( + "[ERROR] Could not open argument file '%s' for input", + argBody); errs.add(msg); newRet = false; } catch (IOException ioex) { - String msg = String.format("[ERROR] Unknown I/O error reading input file '%s': %s", + String msg = String.format( + "[ERROR] Unknown I/O error reading input file '%s': %s", argBody, ioex.getMessage()); errs.add(msg); newRet = false; @@ -343,25 +358,27 @@ public class Everge { try { inputStat = InputStatus.valueOf(argBody.toUpperCase()); } catch (IllegalArgumentException iaex) { - String msg = String.format("[ERROR] '%s' is not a valid input status", argBody); + String msg = String.format("[ERROR] '%s' is not a valid input status", + argBody); errs.add(msg); } break; - default: - { - String msg = String.format("[ERROR] Unrecognised CLI argument name '%s'\n", argName); - errs.add(msg); - newRet = false; - } + default: { + String msg = String + .format("[ERROR] Unrecognised CLI argument name '%s'\n", argName); + errs.add(msg); + newRet = false; + } } } else { String tmp = arg; // Strip off an escaped initial dash - if (tmp.startsWith("\\-")) tmp = tmp.substring(1); + if (tmp.startsWith("\\-")) + tmp = tmp.substring(1); processInputFile(tmp); } - + return newRet; } @@ -369,7 +386,7 @@ public class Everge { * Process a input file. * * @param fle - * Input file to process. + * Input file to process. * @return Whether we processed succesfully or not. */ public boolean processInputFile(String fle) { @@ -389,9 +406,9 @@ public class Everge { * Process a input file. * * @param fle - * Input file to process. + * Input file to process. * @param errs - * List to accumulate errors in. + * List to accumulate errors in. * @return Whether we processed succesfully or not. */ public boolean processInputFile(List<String> errs, String fle) { @@ -400,14 +417,16 @@ public class Everge { // Read in and do replacements on a file try { if (verbosity > 2) { - errStream.printf("[TRACE] Reading file (%s) in mode (%s)\n", fle, inputStat); + errStream.printf("[TRACE] Reading file (%s) in mode (%s)\n", fle, + inputStat); } if (inputStat == InputStatus.ALL) { Path pth = Paths.get(fle); if (!Files.isReadable(pth)) { - String msg = String.format("[ERROR] File '%s' is not readable\n", fle); + String msg + = String.format("[ERROR] File '%s' is not readable\n", fle); errs.add(msg); return false; } @@ -418,27 +437,31 @@ public class Everge { processString(strang); } else if (inputStat == InputStatus.LINE) { - try (FileInputStream fis = new FileInputStream(fle); Scanner scn = new Scanner(fis)) { - while(scn.hasNextLine()) { + try (FileInputStream fis = new FileInputStream(fle); + Scanner scn = new Scanner(fis)) { + while (scn.hasNextLine()) { processString(scn.nextLine()); } } } else if (inputStat == InputStatus.REGEX) { - try (FileInputStream fis = new FileInputStream(fle); Scanner scn = new Scanner(fis)) { + try (FileInputStream fis = new FileInputStream(fle); + Scanner scn = new Scanner(fis)) { scn.useDelimiter(pattern); - while(scn.hasNext()) { + while (scn.hasNext()) { processString(scn.next()); } } } else { - String msg = String.format("[INTERNAL-ERROR] Input status '%s' is not yet implemented\n", + String msg = String.format( + "[INTERNAL-ERROR] Input status '%s' is not yet implemented\n", inputStat); 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", + 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; @@ -453,7 +476,7 @@ public class Everge { * Process an input string. * * @param inp - * The input string to process. + * The input string to process. */ public void processString(String inp) { argLock.readLock().lock(); @@ -462,13 +485,16 @@ public class Everge { String strang = inp; if (verbosity >= 3) { - errStream.infof("[INFO] Processing replacements for string '%s' in mode \n", strang); + errStream.infof( + "[INFO] Processing replacements for string '%s' in mode \n", + strang); } strang = replSet.apply(inp); outStream.print(strang); - if (printNL) outStream.println(); + if (printNL) + outStream.println(); } finally { argLock.readLock().unlock(); } diff --git a/src/main/java/bjc/everge/IntHolder.java b/src/main/java/bjc/everge/IntHolder.java index b72e0e7..31c2578 100644 --- a/src/main/java/bjc/everge/IntHolder.java +++ b/src/main/java/bjc/everge/IntHolder.java @@ -22,7 +22,7 @@ public class IntHolder { * Create a new int-holder set to a value. * * @param i - * The value to set the int to. + * The value to set the int to. */ public IntHolder(int i) { val = i; @@ -41,7 +41,7 @@ public class IntHolder { * Increment the value by an amount and return it. * * @param i - * The amount to increment by. + * The amount to increment by. * * @return The value of the holder. */ @@ -62,9 +62,9 @@ public class IntHolder { /** * Set the value. - * + * * @param i - * The value to set it to. + * The value to set it to. */ public void set(int i) { val = i; diff --git a/src/main/java/bjc/everge/LogStream.java b/src/main/java/bjc/everge/LogStream.java index f4dd217..68bbf84 100644 --- a/src/main/java/bjc/everge/LogStream.java +++ b/src/main/java/bjc/everge/LogStream.java @@ -53,7 +53,7 @@ public class LogStream { * Defaults to printing only fatal errors. * * @param out - * The output stream to place things into. + * The output stream to place things into. */ public LogStream(PrintStream out) { output = out; @@ -64,9 +64,10 @@ public class LogStream { * Create a new log stream. * * @param out - * The output stream to place things into. + * The output stream to place things into. * @param level - * The verbosity level. Use the constants in this class for the values. + * The verbosity level. Use the constants in this class for the + * values. */ public LogStream(PrintStream out, int level) { output = out; @@ -75,6 +76,7 @@ public class LogStream { /** * Get the verbosity of the stream. + * * @return The verbosity of the stream. */ public int verbosity() { @@ -83,7 +85,9 @@ public class LogStream { /** * Set the verbosity of the stream. - * @param verb The verbosity of the stream. + * + * @param verb + * The verbosity of the stream. */ public void verbosity(int verb) { verbosity = verb; @@ -98,12 +102,14 @@ public class LogStream { /** * Increase the verbosity of the stream by an amount. - * @param amt The amount to increase the verbosity by. + * + * @param amt + * The amount to increase the verbosity by. */ public void louder(int amt) { verbosity += amt; } - + /** * Decrement the verbosity of the stream. */ @@ -113,7 +119,9 @@ public class LogStream { /** * Decrease the verbosity of the stream by an amount. - * @param amt The amount to decrease the verbosity by. + * + * @param amt + * The amount to decrease the verbosity by. */ public void quieter(int amt) { verbosity -= amt; @@ -123,7 +131,7 @@ public class LogStream { * Print a message that will always be visible. * * @param msg - * The message to print. + * The message to print. */ public void print(String msg) { output.print(msg); @@ -133,10 +141,10 @@ public class LogStream { * Print a formatted message that will always be visible. * * @param msg - * The format string for the message to print. + * The format string for the message to print. * * @param args - * The arguments to the format string. + * The arguments to the format string. */ public void printf(String msg, Object... args) { output.printf(msg, args); @@ -144,8 +152,11 @@ public class LogStream { /** * Print a message at a given verbosity level. - * @param lvl The verbosity level. - * @param msg The message to print. + * + * @param lvl + * The verbosity level. + * @param msg + * The message to print. */ public void message(int lvl, String msg) { if (verbosity >= lvl) { @@ -195,7 +206,7 @@ public class LogStream { message(DEBUG, msg); } - public void debugf(String msg, Object...args) { + public void debugf(String msg, Object... args) { messagef(DEBUG, msg, args); } diff --git a/src/main/java/bjc/everge/MirrorOutputStream.java b/src/main/java/bjc/everge/MirrorOutputStream.java index 6718bd7..8c99cc9 100644 --- a/src/main/java/bjc/everge/MirrorOutputStream.java +++ b/src/main/java/bjc/everge/MirrorOutputStream.java @@ -14,30 +14,35 @@ public class MirrorOutputStream extends OutputStream { } } + @Override public void close() throws IOException { for (OutputStream stream : streams) { stream.close(); } } + @Override public void flush() throws IOException { for (OutputStream stream : streams) { stream.flush(); } } + @Override public void write(byte[] ba) throws IOException { for (OutputStream stream : streams) { stream.write(ba); } } + @Override public void write(byte[] ba, int off, int len) throws IOException { for (OutputStream stream : streams) { stream.write(ba, off, len); } } + @Override public void write(int b) throws IOException { for (OutputStream stream : streams) { stream.write(b); diff --git a/src/main/java/bjc/everge/ReplError.java b/src/main/java/bjc/everge/ReplError.java index 2e1334c..27324ca 100644 --- a/src/main/java/bjc/everge/ReplError.java +++ b/src/main/java/bjc/everge/ReplError.java @@ -28,13 +28,13 @@ public class ReplError { * Create a new ReplPair parse error. * * @param lne - * The line the error occured on. + * The line the error occured on. * @param nPairs - * The number of pairs processed up to this point. + * The number of pairs processed up to this point. * @param msg - * The message detailing the error. + * The message detailing the error. * @param txt - * The text that caused the error. + * The text that caused the error. */ public ReplError(IntHolder lne, IntHolder nPairs, String msg, String txt) { this(lne.get(), nPairs.get(), msg, txt); @@ -44,13 +44,13 @@ public class ReplError { * Create a new ReplPair parse error. * * @param lne - * The line the error occured on. + * The line the error occured on. * @param nPairs - * The number of pairs processed up to this point. + * The number of pairs processed up to this point. * @param msg - * The message detailing the error. + * The message detailing the error. * @param txt - * The text that caused the error. + * The text that caused the error. */ public ReplError(int lne, int nPairs, String msg, String txt) { line = lne; @@ -63,16 +63,19 @@ public class ReplError { @Override public String toString() { String errString; - if (txt == null) errString = "No associated line"; - else if (txt.equals("")) errString = "Text of line was empty"; - else errString = "Text of line was: " + txt; + if (txt == null) + errString = "No associated line"; + else if (txt.equals("")) + errString = "Text of line was empty"; + else + errString = "Text of line was: " + txt; return String.format("line %d, pair %d:%s\n\t%s", line, numPairs, msg, errString); } /** * Convert the error to a printable string. - * + * * @return The error as a printable string. */ public String toPrintString() { @@ -81,19 +84,22 @@ public class ReplError { /** * Convert the error to a printable string, with a custom header. - * + * * @param hdr - * The text to include in the header. - * + * The text to include in the header. + * * @return The error as a printable string. */ public String toPrintString(String hdr) { String errString; - if (txt == null) errString = "No associated line"; - else if (txt.equals("")) errString = "Text of line was empty"; - else errString = "Text of line was: " + txt; + if (txt == null) + errString = "No associated line"; + else if (txt.equals("")) + errString = "Text of line was empty"; + else + errString = "Text of line was: " + txt; - return String.format("[ERROR] line %d, pair %d: %s\n%s\tContext: %s", - line, numPairs, msg, hdr, errString); + return String.format("[ERROR] line %d, pair %d: %s\n%s\tContext: %s", line, + numPairs, msg, hdr, errString); } } diff --git a/src/main/java/bjc/everge/ReplOpts.java b/src/main/java/bjc/everge/ReplOpts.java index 6086310..debb26e 100644 --- a/src/main/java/bjc/everge/ReplOpts.java +++ b/src/main/java/bjc/everge/ReplOpts.java @@ -51,6 +51,7 @@ public class ReplOpts { * The stream to print error output on. */ public PrintStream errStream = System.err; + @Override public int hashCode() { final int prime = 31; @@ -64,19 +65,30 @@ public class ReplOpts { result = prime * result + (isTrace ? 1231 : 1237); return result; } + @Override public boolean equals(Object obj) { - if (this == obj) return true; - if (obj == null) return false; - if (getClass() != obj.getClass()) return false; + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; ReplOpts other = (ReplOpts) obj; - if (defMulti != other.defMulti) return false; - if (defPrior != other.defPrior) return false; - if (defStage != other.defStage) return false; - if (defStatus != other.defStatus) return false; - if (isDebug != other.isDebug) return false; - if (isPerf != other.isPerf) return false; - if (isTrace != other.isTrace) return false; + if (defMulti != other.defMulti) + return false; + if (defPrior != other.defPrior) + return false; + if (defStage != other.defStage) + return false; + if (defStatus != other.defStatus) + return false; + if (isDebug != other.isDebug) + return false; + if (isPerf != other.isPerf) + return false; + if (isTrace != other.isTrace) + return false; return true; } } diff --git a/src/main/java/bjc/everge/ReplPair.java b/src/main/java/bjc/everge/ReplPair.java index 0d028a6..a8d1b0f 100644 --- a/src/main/java/bjc/everge/ReplPair.java +++ b/src/main/java/bjc/everge/ReplPair.java @@ -36,8 +36,8 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> { /** * The guard for this replacement. * - * The guard of the replacement is a regex that has to match before the pair will be considered. - * Defaults to being blank. + * The guard of the replacement is a regex that has to match before the pair + * will be considered. Defaults to being blank. */ public String guard; @@ -62,9 +62,9 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> { * Create a new replacement pair with a priority of 1. * * @param f - * The string to find. + * The string to find. * @param r - * The string to replace. + * The string to replace. */ public ReplPair(String f, String r) { this(f, r, 1); @@ -74,11 +74,11 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> { * Create a new named replacement pair with a priority of 1. * * @param f - * The string to find. + * The string to find. * @param r - * The string to replace. + * The string to replace. * @param n - * The name of the replacement pair. + * The name of the replacement pair. */ public ReplPair(String f, String r, String n) { this(f, r, 1, n); @@ -88,11 +88,11 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> { * Create a new replacement pair with a set priority. * * @param f - * The string to find. + * The string to find. * @param r - * The string to replace. + * The string to replace. * @param p - * The priority for the replacement. + * The priority for the replacement. */ public ReplPair(String f, String r, int p) { this(f, r, p, f); @@ -102,16 +102,16 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> { * Create a new replacement pair with a set priority and name. * * @param f - * The string to find. + * The string to find. * @param r - * The string to replace. + * The string to replace. * @param n - * The name of the replacement pair. + * The name of the replacement pair. * @param p - * The priority for the replacement. + * The priority for the replacement. */ public ReplPair(String f, String r, int p, String n) { - find = f; + find = f; replace = r; name = n; @@ -123,9 +123,8 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> { * Read a list of replacement pairs from an input source. * * @param scn - * The source to read the replacements from. - * @return - * The list of replacements. + * The source to read the replacements from. + * @return The list of replacements. */ public static List<ReplPair> readList(Scanner scn) { List<ReplPair> lst = new ArrayList<>(); @@ -134,15 +133,14 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> { } /** - * Read a list of replacement pairs from an input source, adding them to - * an existing list. + * Read a list of replacement pairs from an input source, adding them to an + * existing list. * * @param detals - * The list to add the replacements to. + * The list to add the replacements to. * @param scn - * The source to read the replacements from. - * @return - * The list of replacements. + * The source to read the replacements from. + * @return The list of replacements. */ public static List<ReplPair> readList(List<ReplPair> detals, Scanner scn) { List<ReplError> errList = new ArrayList<>(); @@ -157,41 +155,38 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> { } /** - * Read a list of replacement pairs from an input source, adding them to - * an existing list. + * Read a list of replacement pairs from an input source, adding them to an + * existing list. * * @param detals - * The list to add the replacements to. + * The list to add the replacements to. * @param scn - * The source to read the replacements from. + * The source to read the replacements from. * @param errs - * The list to stick errors in. - * @return - * The list of replacements. + * The list to stick errors in. + * @return The list of replacements. */ - public static List<ReplPair> readList(List<ReplPair> detals, Scanner scn, List<ReplError> errs) { + public static List<ReplPair> readList(List<ReplPair> detals, Scanner scn, + List<ReplError> errs) { return readList(detals, scn, errs, new ReplOpts()); } /** - * Read a list of replacement pairs from an input source, adding them to - * an existing list. + * Read a list of replacement pairs from an input source, adding them to an + * existing list. * * @param detals - * The list to add the replacements to. + * The list to add the replacements to. * @param scn - * The source to read the replacements from. + * The source to read the replacements from. * @param errs - * The list to stick errors in. + * The list to stick errors in. * @param ropts - * The options to use when reading the pairs. - * @return - * The list of replacements. + * The options to use when reading the pairs. + * @return The list of replacements. */ - public static List<ReplPair> readList( - List<ReplPair> detals, Scanner scn, - List<ReplError> errs, ReplOpts ropts) - { + public static List<ReplPair> readList(List<ReplPair> detals, Scanner scn, + List<ReplError> errs, ReplOpts ropts) { IntHolder lno = new IntHolder(); IntHolder pno = new IntHolder(); @@ -204,8 +199,10 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> { lno.incr(); // If its commented or blank, skip it - if (name.equals("")) continue; - if (name.startsWith("#")) continue; + if (name.equals("")) + continue; + if (name.startsWith("#")) + continue; // Global control. Process it. if (name.startsWith("|//")) { @@ -217,20 +214,22 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> { ReplPair rp = new ReplPair(); rp.priority = ropts.defPrior; - rp.stat = ropts.defStatus; - rp.lno = lno.get(); - rp.stage = ropts.defStage; + rp.stat = ropts.defStatus; + rp.lno = lno.get(); + rp.stage = ropts.defStage; boolean isMulti = ropts.defMulti; { String tmpName = readName(name, scn, errs, rp, ropts, lno, pno); - if (tmpName == null) continue; + if (tmpName == null) + continue; name = tmpName; } rp.find = name; - if (rp.name == null) rp.name = name; + if (rp.name == null) + rp.name = name; // We started to process the pair, mark it as being // started @@ -239,7 +238,8 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> { // Read in the next uncommented line do { - if (!scn.hasNextLine()) break; + if (!scn.hasNextLine()) + break; body = scn.nextLine().trim(); lno.incr(); @@ -247,14 +247,15 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> { if (body == null) { String msg = String.format( - "Ran out of input looking for replacement body for raw name '%s'", name); + "Ran out of input looking for replacement body for raw name '%s'", + name); errs.add(new ReplError(lno, pno, msg, null)); break; } isMulti = ropts.defMulti; - + ControlledString cs = getControls(body, errs, ropts, lno, pno, "body"); // Body has attached controls, process them. if (cs.hasControls()) { @@ -273,17 +274,18 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> { case "MULTI": case "M": if (cont.count() != 1) { - String errMsg = String.format("Expected one multi flag (got %d)", cont.count()); + String errMsg = String.format( + "Expected one multi flag (got %d)", cont.count()); errs.add(new ReplError(lno, pno, errMsg, body)); } else { isMulti = Boolean.parseBoolean(cont.get(0)); } break; - default: - { - String errMsg = String.format("Invalid control name '%s'", cont.name); - errs.add(new ReplError(lno, pno, errMsg, body)); - } + default: { + String errMsg + = String.format("Invalid control name '%s'", cont.name); + errs.add(new ReplError(lno, pno, errMsg, body)); + } break; } } @@ -293,7 +295,8 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> { if (isMulti) { String tmp = readMultiLine(body, scn, ropts, errs, "body", lno); - if (tmp == null) continue; + if (tmp == null) + continue; body = tmp; } @@ -326,11 +329,14 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> { // Special-case one-stage processing. if (stages.size() == 1) { - if (ropts.isTrace) ropts.errStream.printf("\t[DEBUG] Executing single-stage bypass\n"); + if (ropts.isTrace) + ropts.errStream.printf("\t[DEBUG] Executing single-stage bypass\n"); for (ReplPair rp : stages.iterator().next()) { if (rp.stat == StageStatus.INTERNAL) { - if (ropts.isTrace) ropts.errStream.printf("\t[DEBUG] Excluding internal RP %s\n", rp); + if (ropts.isTrace) + ropts.errStream.printf("\t[DEBUG] Excluding internal RP %s\n", + rp); continue; } @@ -347,15 +353,17 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> { List<ReplPair> tmpList = new ArrayList<>(); tmpList.addAll(detals); - if (ropts.isTrace) ropts.errStream.printf("\t[DEBUG] Stages: %s\n", stages); + if (ropts.isTrace) + ropts.errStream.printf("\t[DEBUG] Stages: %s\n", stages); int procStg = 0; for (List<ReplPair> stageList : stages) { procStg += 1; List<ReplPair> curStage = new ArrayList<>(); - if (ropts.isTrace) ropts.errStream.printf("\t[DEBUG] Staging stage %d of %d: %s\n", - procStg, stageList.size(), stageList); + if (ropts.isTrace) + ropts.errStream.printf("\t[DEBUG] Staging stage %d of %d: %s\n", procStg, + stageList.size(), stageList); for (ReplPair rp : stageList) { // Process through every pair in the previous @@ -365,7 +373,7 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> { if (ropts.isTrace && !rp.replace.equals(tmp)) { ropts.errStream.printf("\t[DEBUG] Staged '%s' -> '%s'\t%s\n", - rp.replace, tmp, curPar); + rp.replace, tmp, curPar); } rp.replace = tmp; @@ -374,21 +382,22 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> { // If we're external; add straight to the output if (rp.stat == StageStatus.EXTERNAL) { if (ropts.isTrace) { - ropts.errStream.printf("\t[DEBUG] Skipped external for staging: %s\n", - rp); + ropts.errStream.printf( + "\t[DEBUG] Skipped external for staging: %s\n", rp); } detals.add(rp); } else { if (ropts.isTrace) { - ropts.errStream.printf("\t[DEBUG] Added to stage %d: %s\n\t\tContents: %s\n", + ropts.errStream.printf( + "\t[DEBUG] Added to stage %d: %s\n\t\tContents: %s\n", procStg, rp, curStage); } curStage.add(rp); } } - + tmpList.addAll(curStage); tmpList.sort(null); } @@ -396,7 +405,8 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> { // Copy over to output, excluding internals for (ReplPair rp : tmpList) { if (rp.stat == StageStatus.INTERNAL) { - if (ropts.isTrace) ropts.errStream.printf("\t[DEBUG] Excluded internal: %s\n", rp); + if (ropts.isTrace) + ropts.errStream.printf("\t[DEBUG] Excluded internal: %s\n", rp); continue; } @@ -417,19 +427,22 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> { List<ReplError> errs, String typ, IntHolder lno) { String tmp = lead; - if (ropts.isTrace && tmp.endsWith("\\")) - ropts.errStream.printf("\t[TRACE] Starting multi-line parse for %s '%s'\n", typ, tmp); + if (ropts.isTrace && tmp.endsWith("\\")) + ropts.errStream.printf("\t[TRACE] Starting multi-line parse for %s '%s'\n", + typ, tmp); boolean didMulti = tmp.endsWith("\\"); while (tmp.endsWith("\\")) { boolean incNL = tmp.endsWith("|\\"); - if (!src.hasNextLine()) break; + if (!src.hasNextLine()) + break; String nxt = src.nextLine().trim(); lno.incr(); - if (nxt.startsWith("#")) continue; + if (nxt.startsWith("#")) + continue; String nlStr = incNL ? "\n" : ""; @@ -454,7 +467,8 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> { @Override public String apply(String inp) { if (guard != null) { - if (!inp.matches(guard)) return inp; + if (!inp.matches(guard)) + return inp; } String res = inp.replaceAll(find, replace); @@ -466,18 +480,20 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> { public String toString() { String nameStr = ""; - if (!find.equals(name)) nameStr = String.format("(%s)", name); + if (!find.equals(name)) + nameStr = String.format("(%s)", name); return String.format("%ss/(%s)/(%s)/p(%d)", nameStr, find, replace, priority); } @Override public int compareTo(ReplPair rp) { - if (this.priority == rp.priority) return this.lno - rp.lno; + if (this.priority == rp.priority) + return this.lno - rp.lno; return rp.priority - this.priority; } - + @Override public int hashCode() { final int prime = 31; @@ -492,21 +508,32 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> { @Override public boolean equals(Object obj) { - if (this == obj) return true; - if (obj == null) return false; - if (getClass() != obj.getClass()) return false; + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; ReplPair other = (ReplPair) obj; if (find == null) { - if (other.find != null) return false; - } else if (!find.equals(other.find)) return false; + if (other.find != null) + return false; + } else if (!find.equals(other.find)) + return false; if (name == null) { - if (other.name != null) return false; - } else if (!name.equals(other.name)) return false; - if (priority != other.priority) return false; + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + if (priority != other.priority) + return false; if (replace == null) { - if (other.replace != null) return false; - } else if (!replace.equals(other.replace)) return false; - if (stage != other.stage) return false; + if (other.replace != null) + return false; + } else if (!replace.equals(other.replace)) + return false; + if (stage != other.stage) + return false; return true; } @@ -521,129 +548,135 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> { if (cs.hasControls()) { for (Control cont : cs.controls) { switch (cont.name) { - case "NAME": - case "N": - if (cont.count() != 1) { - String errMsg = String.format("One name argument was expected (got %d)", - cont.count()); - - errs.add(new ReplError(lno, pno, errMsg, nam)); - } else { - rp.name = cont.get(0); - } - break; - case "GUARD": - case "G": - if (cont.count() != 1) { - String errMsg = String.format("One guard argument was expected (got %d)", - cont.count()); - - errs.add(new ReplError(lno, pno, errMsg, nam)); - } else { - String pat = cont.get(0); + case "NAME": + case "N": + if (cont.count() != 1) { + String errMsg = String.format( + "One name argument was expected (got %d)", cont.count()); - try { - Pattern.compile(pat); - } catch (PatternSyntaxException psex) { - String errMsg = String.format("Guard argument '%s' is not a valid regex (%s)", - pat, psex.getMessage()); + errs.add(new ReplError(lno, pno, errMsg, nam)); + } else { + rp.name = cont.get(0); + } + break; + case "GUARD": + case "G": + if (cont.count() != 1) { + String errMsg = String.format( + "One guard argument was expected (got %d)", cont.count()); - errs.add(new ReplError(lno, pno, errMsg, nam)); - } + errs.add(new ReplError(lno, pno, errMsg, nam)); + } else { + String pat = cont.get(0); - rp.guard = cont.get(0); - } - break; - case "PRIORITY": - case "PRIOR": - case "P": try { - if (cont.count() != 1) { - String errMsg = String.format("One priority argument was expected (got %d", - cont.count()); - - errs.add(new ReplError(lno, pno, errMsg, nam)); - } else { - rp.priority = Integer.parseInt(cont.get(0)); - } - } catch (NumberFormatException nfex) { - String errMsg = String.format("'%s' is not a valid priority (must be an integer)", - cont.get(0)); + Pattern.compile(pat); + } catch (PatternSyntaxException psex) { + String errMsg = String.format( + "Guard argument '%s' is not a valid regex (%s)", pat, + psex.getMessage()); errs.add(new ReplError(lno, pno, errMsg, nam)); } - break; - case "STAGE": - case "S": - try { - if (cont.count() != 1) { - String errMsg = String.format("One stage argument was expected (got %d", - cont.count()); - errs.add(new ReplError(lno, pno, errMsg, nam)); - } else { - int tmpStage = Integer.parseInt(cont.get(0)); - if (tmpStage < 0) { - String errMsg = String.format("'%s' is not a valid stage (must be a positive integer)", - cont.get(0)); - errs.add(new ReplError(lno, pno, errMsg, nam)); - - break; - } - rp.stage = tmpStage; - } - } catch (NumberFormatException nfex) { - String errMsg = String.format("'%s' is not a valid stage (must be a positive integer)", - cont.get(0)); + rp.guard = cont.get(0); + } + break; + case "PRIORITY": + case "PRIOR": + case "P": + try { + if (cont.count() != 1) { + String errMsg = String.format( + "One priority argument was expected (got %d", + cont.count()); errs.add(new ReplError(lno, pno, errMsg, nam)); + } else { + rp.priority = Integer.parseInt(cont.get(0)); } - break; - case "MULTITRUE": - case "MULTIT": - case "MT": - isMulti = true; - break; - case "MULTIFALSE": - case "MULTIF": - case "MF": - isMulti = false; - break; - case "MULTI": - case "M": + } catch (NumberFormatException nfex) { + String errMsg = String.format( + "'%s' is not a valid priority (must be an integer)", + cont.get(0)); + + errs.add(new ReplError(lno, pno, errMsg, nam)); + } + break; + case "STAGE": + case "S": + try { if (cont.count() != 1) { - String errMsg = String.format("One multi-flag argument was expected (got %d", + String errMsg = String.format( + "One stage argument was expected (got %d", cont.count()); errs.add(new ReplError(lno, pno, errMsg, nam)); } else { - isMulti = Boolean.parseBoolean(cont.get(0)); + int tmpStage = Integer.parseInt(cont.get(0)); + if (tmpStage < 0) { + String errMsg = String.format( + "'%s' is not a valid stage (must be a positive integer)", + cont.get(0)); + errs.add(new ReplError(lno, pno, errMsg, nam)); + + break; + } + rp.stage = tmpStage; } - break; - case "INTERNAL": - case "INT": - case "I": - rp.stat = StageStatus.INTERNAL; - break; - case "EXTERNAL": - case "EXT": - case "E": - rp.stat = StageStatus.EXTERNAL; - break; - case "BOTH": - case "B": - rp.stat = StageStatus.BOTH; - break; - default: - { - String errMsg = String.format("Unknown control name '%s' for name '%s'", - cont.name, nam); + } catch (NumberFormatException nfex) { + String errMsg = String.format( + "'%s' is not a valid stage (must be a positive integer)", + cont.get(0)); - ReplError erd = new ReplError(lno, pno, errMsg, nam); + errs.add(new ReplError(lno, pno, errMsg, nam)); + } + break; + case "MULTITRUE": + case "MULTIT": + case "MT": + isMulti = true; + break; + case "MULTIFALSE": + case "MULTIF": + case "MF": + isMulti = false; + break; + case "MULTI": + case "M": + if (cont.count() != 1) { + String errMsg = String.format( + "One multi-flag argument was expected (got %d", + cont.count()); - errs.add(erd); - } - break; + errs.add(new ReplError(lno, pno, errMsg, nam)); + } else { + isMulti = Boolean.parseBoolean(cont.get(0)); + } + break; + case "INTERNAL": + case "INT": + case "I": + rp.stat = StageStatus.INTERNAL; + break; + case "EXTERNAL": + case "EXT": + case "E": + rp.stat = StageStatus.EXTERNAL; + break; + case "BOTH": + case "B": + rp.stat = StageStatus.BOTH; + break; + default: { + String errMsg = String.format( + "Unknown control name '%s' for name '%s'", cont.name, nam); + + ReplError erd = new ReplError(lno, pno, errMsg, nam); + + errs.add(erd); + } + break; } } @@ -653,7 +686,8 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> { // Multi-line name with a trailer if (isMulti) { String tmp = readMultiLine(name, scn, ropts, errs, "name", lno); - if (tmp == null) return null; + if (tmp == null) + return null; name = tmp; } @@ -662,7 +696,8 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> { private static void readGlobal(String nam, Scanner scn, List<ReplError> errs, ReplOpts ropts, IntHolder lno, IntHolder pno) { - ControlledString cs = getControls(nam.substring(1), errs, ropts, lno, pno, "global"); + ControlledString cs + = getControls(nam.substring(1), errs, ropts, lno, pno, "global"); for (Control cont : cs.controls) { switch (cont.name) { @@ -671,8 +706,8 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> { case "P": try { if (cont.count() != 1) { - String errMsg = String.format("Must specify 1 priority (%d specified)", - cont.count()); + String errMsg = String.format( + "Must specify 1 priority (%d specified)", cont.count()); errs.add(new ReplError(lno, pno, errMsg, nam)); } else { @@ -680,7 +715,8 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> { ropts.defPrior = tmp; } } catch (NumberFormatException nfex) { - String errMsg = String.format("'%s' is not a valid priority (must be an integer)", + String errMsg = String.format( + "'%s' is not a valid priority (must be an integer)", cont.get(0)); errs.add(new ReplError(lno, pno, errMsg, nam)); @@ -690,15 +726,16 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> { case "S": try { if (cont.count() != 1) { - String errMsg = String.format("Must specify 1 stage (%d specified)", - cont.count()); + String errMsg = String.format( + "Must specify 1 stage (%d specified)", cont.count()); errs.add(new ReplError(lno, pno, errMsg, nam)); } else { int tmpStage = Integer.parseInt(cont.get(0)); if (tmpStage < 0) { - String errMsg = String.format("'%s' is not a valid stage (must be a positive integer)", + String errMsg = String.format( + "'%s' is not a valid stage (must be a positive integer)", cont.get(0)); errs.add(new ReplError(lno, pno, errMsg, nam)); @@ -708,7 +745,8 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> { ropts.defStage = tmpStage; } } catch (NumberFormatException nfex) { - String errMsg = String.format("'%s' is not a valid stage (must be a positive integer)", + String errMsg = String.format( + "'%s' is not a valid stage (must be a positive integer)", cont.get(0)); errs.add(new ReplError(lno, pno, errMsg, nam)); @@ -727,8 +765,8 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> { case "MULTI": case "M": if (cont.count() != 1) { - String errMsg = String.format("Must specify 1 multi-flag (%d specified)", - cont.count()); + String errMsg = String.format( + "Must specify 1 multi-flag (%d specified)", cont.count()); errs.add(new ReplError(lno, pno, errMsg, nam)); } else { @@ -762,8 +800,8 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> { case "DEBUG": case "D": if (cont.count() != 1) { - String errMsg = String.format("Must specify 1 debug flag (%d specified)", - cont.count()); + String errMsg = String.format( + "Must specify 1 debug flag (%d specified)", cont.count()); errs.add(new ReplError(lno, pno, errMsg, nam)); } else { @@ -783,8 +821,8 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> { case "TRACE": case "T": if (cont.count() != 1) { - String errMsg = String.format("Must specify 1 trace flag (%d specified)", - cont.count()); + String errMsg = String.format( + "Must specify 1 trace flag (%d specified)", cont.count()); errs.add(new ReplError(lno, pno, errMsg, nam)); } else { @@ -804,30 +842,29 @@ public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> { case "PERF": case "PRF": if (cont.count() != 1) { - String errMsg = String.format("Must specify 1 perf. flag (%d specified)", - cont.count()); + String errMsg = String.format( + "Must specify 1 perf. flag (%d specified)", cont.count()); errs.add(new ReplError(lno, pno, errMsg, nam)); } else { ropts.isPerf = Boolean.parseBoolean(cont.get(0)); } break; - default: - { - String msg = String.format("Invalid global control name '%s'", cont.name); - ReplError err = new ReplError(lno, pno, msg, nam); - errs.add(err); - } + default: { + String msg = String.format("Invalid global control name '%s'", cont.name); + ReplError err = new ReplError(lno, pno, msg, nam); + errs.add(err); + } break; } - if (ropts.isTrace) + if (ropts.isTrace) ropts.errStream.printf("\t[TRACE] Processed global control '%s'\n", cont); } return; } - + private static ControlledString getControls(String lne, List<ReplError> errs, ReplOpts ropts, IntHolder lno, IntHolder pno, String type) { try { diff --git a/src/main/java/bjc/everge/ReplSet.java b/src/main/java/bjc/everge/ReplSet.java index f759e8e..0920c6a 100644 --- a/src/main/java/bjc/everge/ReplSet.java +++ b/src/main/java/bjc/everge/ReplSet.java @@ -23,10 +23,11 @@ public class ReplSet { /** * Create a new set of pairs using an existing list of pairs. * - * Changes to the list of pairs will carry across to the ReplSet, so be careful about that. + * Changes to the list of pairs will carry across to the ReplSet, so be careful + * about that. * * @param lst - * The list of pairs to use. + * The list of pairs to use. */ public ReplSet(List<ReplPair> lst) { parList = lst; @@ -34,15 +35,18 @@ public class ReplSet { /** * Load a ReplSet from a file. + * * @param fName - * The file to load the ReplSet from. + * The file to load the ReplSet from. * @return A ReplSet, loaded from the file. - * @throws IOException if something goes badly reading it. + * @throws IOException + * if something goes badly reading it. */ public static ReplSet fromFile(String fName) throws IOException { ReplSet rs = new ReplSet(); - try (FileInputStream fis = new FileInputStream(fName); Scanner scn = new Scanner(fis)) { + try (FileInputStream fis = new FileInputStream(fName); + Scanner scn = new Scanner(fis)) { rs.parList = ReplPair.readList(scn); } @@ -53,7 +57,7 @@ public class ReplSet { * Adds more pairs to the ReplSet. * * @param pars - * The pairs to add to the ReplSet. + * The pairs to add to the ReplSet. */ public void addPairs(List<ReplPair> pars) { for (ReplPair par : pars) { @@ -68,7 +72,7 @@ public class ReplSet { * Adds more pairs to the ReplSet. * * @param pars - * The pairs to add to the ReplSet. + * The pairs to add to the ReplSet. */ public void addPairs(ReplPair... pars) { for (ReplPair par : pars) { @@ -83,7 +87,7 @@ public class ReplSet { * Apply the ReplSet to a string. * * @param val - * The string to apply the ReplSet to. + * The string to apply the ReplSet to. * * @return The result of applying the ReplSet. */ diff --git a/src/main/java/bjc/everge/StageStatus.java b/src/main/java/bjc/everge/StageStatus.java index 2a1fe63..69125fc 100644 --- a/src/main/java/bjc/everge/StageStatus.java +++ b/src/main/java/bjc/everge/StageStatus.java @@ -2,6 +2,7 @@ package bjc.everge; /** * Possible statuses of pairs with respect to exporting. + * * @author Ben Culkin */ public enum StageStatus { @@ -18,4 +19,3 @@ public enum StageStatus { */ BOTH; } - diff --git a/src/main/java/bjc/everge/StringUtils.java b/src/main/java/bjc/everge/StringUtils.java index 649e5f4..61c8973 100644 --- a/src/main/java/bjc/everge/StringUtils.java +++ b/src/main/java/bjc/everge/StringUtils.java @@ -19,12 +19,12 @@ public class StringUtils { * Split a string on every occurrence of a string not preceded by an escape. * * @param escape - * The escape that stops splitting. + * The escape that stops splitting. * @param splat - * The string to split on. If this starts with the escape sequence, things will work - * poorly. + * The string to split on. If this starts with the escape + * sequence, things will work poorly. * @param inp - * The string to split. + * The string to split. * @return The string split as specified above. */ public static String[] escapeSplit(String escape, String splat, String inp) { @@ -34,12 +34,16 @@ public class StringUtils { // No input if (inp == null || inp.equals("")) { - return new String[] {inp}; + return new String[] { + inp + }; } // Input does not contain any delimiters if (!inp.contains(splat)) { - return new String[] {inp}; + return new String[] { + inp + }; } // No escape, so we can just split normally @@ -62,19 +66,19 @@ public class StringUtils { int eidx = wrk.indexOf(escape); // Was the last thing we saw an escape? - // This is used to enable the handling of escaping escapes + // This is used to enable the handling of escaping escapes boolean hadEscape = false; // As long as there an occurrence of either the split/escape while (sidx != -1 || eidx != -1) { // If there is an escape before a split if (eidx > 0 && eidx < sidx) { - if (isDebug) System.err.printf("[TRACE] Considering escape\n"); + if (isDebug) + System.err.printf("[TRACE] Considering escape\n"); /* - * We potentially have an escaped sequence: - * - either an escaped split - * - or an escaped escape + * We potentially have an escaped sequence: - either an escaped split - or + * an escaped escape */ // Check for an escaped split @@ -86,12 +90,12 @@ public class StringUtils { wrk = sliceStringL(wrk, eidx, escape.length()); // Recalculate indexes - sidx = wrk.indexOf(splat, ofst); + sidx = wrk.indexOf(splat, ofst); eidx = wrk.indexOf(escape, ofst); if (isDebug) { - System.err.printf("[TRACE] After esc. split (%s) %d/%d\n", - wrk, sidx, eidx); + System.err.printf("[TRACE] After esc. split (%s) %d/%d\n", wrk, + sidx, eidx); } // No pending escape @@ -108,7 +112,7 @@ public class StringUtils { wrk = sliceStringL(wrk, eidx, escape.length()); // Recalculate indexes - sidx = wrk.indexOf(splat, ofst); + sidx = wrk.indexOf(splat, ofst); eidx = wrk.indexOf(escape, ofst); if (isDebug) { @@ -135,7 +139,8 @@ public class StringUtils { while (sidx != -1 && hasEscape) { int oidx = wrk.indexOf(splat, sidx + escape.length()); - if (oidx == -1) break; + if (oidx == -1) + break; wrk = sliceStringL(wrk, oidx, escape.length()); @@ -151,8 +156,8 @@ public class StringUtils { String tmp = wrk.substring(0, sidx); if (isDebug) { - System.err.printf("[TRACE] Adding (%s) to returned splits; (%s)\n", - tmp, wrk.substring(sidx)); + System.err.printf("[TRACE] Adding (%s) to returned splits; (%s)\n", tmp, + wrk.substring(sidx)); } ret.add(tmp); @@ -172,7 +177,8 @@ public class StringUtils { hadEscape = false; } - if (!wrk.equals("")) ret.add(wrk); + if (!wrk.equals("")) + ret.add(wrk); return ret.toArray(new String[0]); } @@ -181,16 +187,16 @@ public class StringUtils { * Slice a substring out of another string. * * @param strang - * The string to remove a substring from. + * The string to remove a substring from. * @param lft - * The left-side of the substring to remove. + * The left-side of the substring to remove. * @param rft - * The right-side of the substring to remove. + * The right-side of the substring to remove. * * @return The string, with the substring removed. */ public static String sliceString(String strang, int lft, int rft) { - String leftSide = strang.substring(0, lft); + String leftSide = strang.substring(0, lft); String rightSide = strang.substring(rft); return leftSide + rightSide; @@ -200,16 +206,16 @@ public class StringUtils { * Slice a substring out of another string. * * @param strang - * The string to remove a substring from. + * The string to remove a substring from. * @param lft - * The left-side of the substring to remove. + * The left-side of the substring to remove. * @param len - * The length of the substring to remove. + * The length of the substring to remove. * * @return The string, with the substring removed. */ public static String sliceStringL(String strang, int lft, int len) { - String leftSide = strang.substring(0, lft); + String leftSide = strang.substring(0, lft); String rightSide = strang.substring(lft + len); return leftSide + rightSide; diff --git a/src/test/java/bjc/everge/EvergeTest.java b/src/test/java/bjc/everge/EvergeTest.java index cfc9147..0ef9939 100644 --- a/src/test/java/bjc/everge/EvergeTest.java +++ b/src/test/java/bjc/everge/EvergeTest.java @@ -51,10 +51,10 @@ public class EvergeTest { evg.setOutput(normOut); evg.setError(normErr); - //evg.processArgs("--verbosity", "3"); + // evg.processArgs("--verbosity", "3"); List<String> errs = new ArrayList<>(); - boolean stat = evg.processArgs(errs, "--input-status", "line", "--file", "data/test/evg-test1.rp", - "data/test/evg-test1.inp"); + boolean stat = evg.processArgs(errs, "--input-status", "line", "--file", + "data/test/evg-test1.rp", "data/test/evg-test1.inp"); if (!stat) { System.err.println("[ERROR] Did not succesfully process args"); for (String err : errs) { @@ -62,7 +62,8 @@ public class EvergeTest { } System.err.println("[ERROR] Normal Output:\n--------------------"); System.err.println(baos.toString().trim()); - System.err.println("--------------------\n[ERROR] Error Output:\n------------------"); + System.err.println( + "--------------------\n[ERROR] Error Output:\n------------------"); System.err.println(errBaos.toString().trim()); System.err.println("--------------------"); diff --git a/src/test/java/bjc/everge/ReplPairTest.java b/src/test/java/bjc/everge/ReplPairTest.java index 8f0a3e8..c2a020c 100644 --- a/src/test/java/bjc/everge/ReplPairTest.java +++ b/src/test/java/bjc/everge/ReplPairTest.java @@ -23,7 +23,8 @@ public class ReplPairTest { List<ReplPair> lrp = null; String fName = "data/test/test1.rp"; - try (FileInputStream fis = new FileInputStream(fName); Scanner scn = new Scanner(fis)) { + try (FileInputStream fis = new FileInputStream(fName); + Scanner scn = new Scanner(fis)) { lrp = ReplPair.readList(scn); assertTrue(lrp.size() == 0); @@ -39,7 +40,8 @@ public class ReplPairTest { @Test public void testMultiReplace() { - assertMultiReplace("data/test/test3.rp", "A B C", "a b c", "A A B", "a a b", "AAB", "aab"); + assertMultiReplace("data/test/test3.rp", "A B C", "a b c", "A A B", "a a b", + "AAB", "aab"); } @Test @@ -59,13 +61,12 @@ public class ReplPairTest { @Test public void testErrorException() { - String msg = - "[ERROR] An error occured parsing replacement pairs:" + String msg = "[ERROR] An error occured parsing replacement pairs:" + "\n\t[ERROR] line 2, pair 1: Ran out of input looking for" + " replacement body for raw name 'a'" + "\n\t\tContext: No associated line"; - assertThrownMessage(msg, "data/test/test7.rp"); + assertThrownMessage(msg, "data/test/test7.rp"); } @Test diff --git a/src/test/java/bjc/everge/StringUtilsTest.java b/src/test/java/bjc/everge/StringUtilsTest.java index a1469ae..91af56e 100644 --- a/src/test/java/bjc/everge/StringUtilsTest.java +++ b/src/test/java/bjc/everge/StringUtilsTest.java @@ -14,16 +14,16 @@ import static org.junit.Assert.*; public class StringUtilsTest { @Test public void testNullSplit() { - assertSplitsTo("a", null, " ", "a"); - assertSplitsTo("a b", null, " ", "a", "b"); + assertSplitsTo("a", null, " ", "a"); + assertSplitsTo("a b", null, " ", "a", "b"); assertSplitsTo("a b cd", null, " ", "a", "b", "cd"); } @Test public void testNoEscapeSplit() { - assertSplitsTo("a", "/", " ", "a"); - assertSplitsTo("a b", "/", " ", "a", "b"); - assertSplitsTo("a b/c", "/", " ", "a", "b/c"); + assertSplitsTo("a", "/", " ", "a"); + assertSplitsTo("a b", "/", " ", "a", "b"); + assertSplitsTo("a b/c", "/", " ", "a", "b/c"); } @Test diff --git a/src/test/java/bjc/everge/TestUtils.java b/src/test/java/bjc/everge/TestUtils.java index 6c4b73b..49e962e 100644 --- a/src/test/java/bjc/everge/TestUtils.java +++ b/src/test/java/bjc/everge/TestUtils.java @@ -21,9 +21,9 @@ public class TestUtils { * Assert that a ReplParseException is thrown with a given message. * * @param msg - * The message. + * The message. * @param fle - * The file to load input from. + * The file to load input from. */ public static void assertThrownMessage(String msg, String fle) { assertThrownMessage(false, msg, fle); @@ -33,19 +33,21 @@ public class TestUtils { * Assert that a ReplParseException is thrown with a given message. * * @param logMsg - * Log the exception message. + * Log the exception message. * @param msg - * The message. + * The message. * @param fle - * The file to load input from. + * The file to load input from. */ public static void assertThrownMessage(boolean logMsg, String msg, String fle) { - try (FileInputStream fis = new FileInputStream(fle); Scanner scn = new Scanner(fis)) { + try (FileInputStream fis = new FileInputStream(fle); + Scanner scn = new Scanner(fis)) { ReplPair.readList(new ArrayList<>(), scn); assertTrue(false); } catch (BadReplParse rpex) { - if (logMsg) System.err.println(rpex.toPrintString()); + if (logMsg) + System.err.println(rpex.toPrintString()); assertEquals(msg, rpex.toPrintString()); } catch (Exception ex) { @@ -64,20 +66,23 @@ public class TestUtils { public static void assertMultiReplace(boolean logRep, String fle, String... inps) { if (inps.length < 2) { - throw new IllegalArgumentException("ERROR: Must provide at least two strings to assertMultiReplace"); + throw new IllegalArgumentException( + "ERROR: Must provide at least two strings to assertMultiReplace"); } if (inps.length % 2 != 0) { - throw new IllegalArgumentException("ERROR: Odd number of strings passed to assertMultiReplace"); + throw new IllegalArgumentException( + "ERROR: Odd number of strings passed to assertMultiReplace"); } List<ReplPair> lrp = null; - try (FileInputStream fis = new FileInputStream(fle); Scanner scn = new Scanner(fis)) { + try (FileInputStream fis = new FileInputStream(fle); + Scanner scn = new Scanner(fis)) { lrp = ReplPair.readList(scn); } catch (BadReplParse rpex) { System.err.println(rpex.toPrintString()); - + assertTrue(false); } catch (Exception ex) { System.err.println("EXCEPTION"); @@ -90,7 +95,7 @@ public class TestUtils { for (int i = 0; i < inps.length; i += 2) { String right = inps[i]; - String inp = inps[i + 1]; + String inp = inps[i + 1]; assertReplacesTo(logRep, right, lrp, inp); } @@ -104,7 +109,8 @@ public class TestUtils { assertReplacesTo(false, right, rps, inp); } - public static void assertReplacesTo(boolean logRep, String right, List<ReplPair> rps, String inp) { + public static void assertReplacesTo(boolean logRep, String right, List<ReplPair> rps, + String inp) { if (logRep) { System.err.printf("\t[LOG] Checking '%s' -> '%s'\n", inp, right); } @@ -124,13 +130,16 @@ public class TestUtils { assertEquals(right, tmp); } - public static void assertSplitsTo(String inp, String esc, String splat, String... right) { + public static void assertSplitsTo(String inp, String esc, String splat, + String... right) { assertSplitsTo(false, inp, esc, splat, right); } - public static void assertSplitsTo(boolean doLog, String inp, String esc, String splat, String... right) { + public static void assertSplitsTo(boolean doLog, String inp, String esc, String splat, + String... right) { try { - if (doLog) StringUtils.isDebug = true; + if (doLog) + StringUtils.isDebug = true; String[] lst = StringUtils.escapeSplit(esc, splat, inp); @@ -152,7 +161,8 @@ public class TestUtils { assertTrue(false); } finally { - if (doLog) StringUtils.isDebug = false; + if (doLog) + StringUtils.isDebug = false; } } @@ -160,8 +170,10 @@ public class TestUtils { assertIsControl(false, inp, strang, args); } - public static void assertIsControl(boolean doLog, String inp, String strang, Control... args) { - ControlledString cs = ControlledString.parse(inp, new ParseStrings("//", ";", "/", "|")); + public static void assertIsControl(boolean doLog, String inp, String strang, + Control... args) { + ControlledString cs + = ControlledString.parse(inp, new ParseStrings("//", ";", "/", "|")); if (doLog) { System.err.printf("[LOG] CS: %s\n", cs); |
