summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/cli/objects
diff options
context:
space:
mode:
authorBenjamin J. Culkin <bjculkin@mix.wvu.edu>2017-09-09 21:46:16 -0300
committerBenjamin J. Culkin <bjculkin@mix.wvu.edu>2017-09-09 21:47:34 -0300
commitd766896972c9e9be4a9e0021ec5f4f0665901865 (patch)
tree1f6473300ef86e0697d682360bea0d28fc144baa /BJC-Utils2/src/main/java/bjc/utils/cli/objects
parent40f3a28569366c4357fbda11d2fff3b77686d84f (diff)
Update
Most of it is documentation changes. The rest is more work on BlockReaders, as well as a simple command language for configuring them.
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/cli/objects')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/cli/objects/BlockReaderCLI.java325
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/cli/objects/Command.java65
2 files changed, 390 insertions, 0 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/objects/BlockReaderCLI.java b/BJC-Utils2/src/main/java/bjc/utils/cli/objects/BlockReaderCLI.java
new file mode 100644
index 0000000..115f782
--- /dev/null
+++ b/BJC-Utils2/src/main/java/bjc/utils/cli/objects/BlockReaderCLI.java
@@ -0,0 +1,325 @@
+package bjc.utils.cli.objects;
+
+import java.io.InputStreamReader;
+import java.io.Reader;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Scanner;
+import java.util.function.Predicate;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.regex.PatternSyntaxException;
+
+import bjc.utils.ioutils.Prompter;
+import bjc.utils.ioutils.blocks.*;
+
+public class BlockReaderCLI {
+ /*
+ * All of the block readers.
+ */
+ private Map<String, BlockReader> readers;
+
+ /*
+ * All of the I/O sources.
+ */
+ private Map<String, Reader> sources;
+
+ /**
+ * Create a new CLI for configuring BlockReaders.
+ *
+ * @param srcs
+ * The container of initial I/O sources.
+ */
+ public BlockReaderCLI(Map<String, Reader> srcs) {
+ readers = new HashMap();
+
+ sources = srcs;
+ }
+
+ public static void main(String[] args) {
+ /*
+ * Create/configure I/O sources.
+ */
+ Map<String, Reader> sources = new HashMap<>();
+ sources.put("stdio", new InputStreamReader(System.in));
+
+ BlockReaderCLI reader = new BlockReaderCLI(sources);
+
+ reader.run(new Scanner(System.in), "console");
+ }
+
+ /**
+ * Run the CLI on an input source.
+ *
+ * @param input
+ * The place to read input from.
+ * @param ioSource
+ * The name of the place to read input from.
+ */
+ public void run(Scanner input, String ioSource) {
+ int lno = 0;
+ while(input.hasNextLine()) {
+ System.out.printf("reader-conf(%d)>", lno);
+ String ln = input.nextLine();
+
+ lno += 1;
+
+ Command com = Command.fromString(ln, lno, ioSource);
+ if(com == null) continue;
+
+ handleCommand(com);
+ }
+
+ input.close();
+ }
+
+ /*
+ * Handle a command.
+ */
+ public void handleCommand(Command com) {
+ switch(com.nameCommand) {
+ case "def-filtered":
+ defFiltered(com);
+ break;
+ case "def-layered":
+ defLayered(com);
+ break;
+ case "def-pushback":
+ defPushback(com);
+ break;
+ case "def-simple":
+ defSimple(com);
+ break;
+ case "def-serial":
+ defSerial(com);
+ break;
+ case "exit":
+ case "quit":
+ System.out.printf("Exiting reader-conf, %d readers configured in %d commands\n",
+ readers.size(), com.lineNo);
+ break;
+ default:
+ System.err.print(com.error("Unknown command '%s'\n", com.nameCommand));
+ break;
+ }
+ }
+
+ private void defFiltered(Command com) {
+ String remn = com.remnCommand;
+
+ /*
+ * Get the block name.
+ */
+ int idx = remn.indexOf(' ');
+ if(idx == -1) {
+ System.err.print(com.error("No name argument for def-filtered.\n"));
+ }
+ String blockName = remn.substring(0, idx).trim();
+ remn = remn.substring(idx).trim();
+
+ /*
+ * Check there isn't a reader already bound to this name.
+ */
+ if(readers.containsKey(blockName)) {
+ System.err.print(com.warn("Shadowing existing reader named %s\n", blockName));
+ }
+
+ /*
+ * Get the reader name.
+ */
+ idx = remn.indexOf(' ');
+ if(idx == -1) {
+ System.err.print("No reader-name argument for def-filtered.\n");
+ }
+ String readerName = remn.substring(0, idx).trim();
+ remn = remn.substring(idx).trim();
+
+ /*
+ * Check there is a reader bound to that name.
+ */
+ if(!readers.containsKey(readerName)) {
+ System.err.print(com.error("No source named %s\n", readerName));
+ return;
+ }
+
+ /*
+ * Get the pattern.
+ */
+ if(remn.equals("")) {
+ System.err.print("No filter argument for def-filtered\n");
+ }
+
+ String filter = remn;
+
+ try {
+ Pattern pat = Pattern.compile(filter);
+
+ Predicate<Block> pred = (block) -> {
+ Matcher mat = pat.matcher(block.contents);
+
+ return mat.matches();
+ };
+
+ BlockReader reader = new FilteredBlockReader(readers.get(readerName), pred);
+
+ readers.put(blockName, reader);
+ } catch (PatternSyntaxException psex) {
+ System.err.print(com.error("Invalid regular expression '%s' for filter. (%s)\n", filter, psex.getMessage()));
+ }
+ }
+
+ private void defPushback(Command com) {
+ String[] parts = com.remnCommand.split(" ");
+
+ if(parts.length != 2) {
+ System.err.print(com.error("Incorrect number of arguments to def-pushback. Requires a block name and a reader name\n"));
+ return;
+ }
+
+ String blockName = parts[0];
+ if(readers.containsKey(blockName)) {
+ System.err.print(com.warn("Shadowing existing reader %s\n", blockName));
+ }
+
+ String readerName = parts[1];
+ if(!readers.containsKey(readerName)) {
+ System.err.print(com.error("No reader named %s\n", readerName));
+ return;
+ }
+
+ BlockReader reader = new PushbackBlockReader(readers.get(readerName));
+ readers.put(blockName, reader);
+ }
+
+ private void defLayered(Command com) {
+ String[] parts = com.remnCommand.split(" ");
+
+ if(parts.length != 3) {
+ System.err.print(com.error("Incorrect number of arguments to def-layered. Requires a block name and two reader names\n"));
+ return;
+ }
+
+ /*
+ * Get the block name.
+ */
+ String blockName = parts[0];
+ if(readers.containsKey(blockName)) {
+ System.err.print(com.warn("Shadowing existing reader named %s\n", blockName));
+ }
+
+ /*
+ * Make sure the component readers exist.
+ */
+ if(!readers.containsKey(parts[1])) {
+ System.err.print(com.error("No reader named %s\n", parts[1]));
+ return;
+ }
+
+ if(!readers.containsKey(parts[2])) {
+ System.err.print(com.error("No reader named %s\n", parts[2]));
+ return;
+ }
+
+ BlockReader reader = new LayeredBlockReader(readers.get(parts[1]), readers.get(parts[2]));
+ readers.put(blockName, reader);
+ }
+
+ private void defSerial(Command com) {
+ String[] parts = com.remnCommand.split(" ");
+
+ if(parts.length < 2) {
+ System.err.print(com.error("Not enough arguments to def-serial. Requires at least a block name and at least one reader name\n"));
+ return;
+ }
+
+ /*
+ * Get the name for this BlockReader.
+ */
+ String blockName = parts[0];
+ /*
+ * Check there isn't a reader already bound to this name.
+ */
+ if(readers.containsKey(blockName)) {
+ System.err.print(com.warn("Shadowing existing reader named %s\n", blockName));
+ }
+
+ /*
+ * Get all of the component readers.
+ */
+ BlockReader[] readerArr = new BlockReader[parts.length - 1];
+ for(int i = 1; i < parts.length; i++) {
+ String readerName = parts[i];
+
+ /*
+ * Check there is a source bound to that name.
+ */
+ if(!readers.containsKey(readerName)) {
+ System.err.print(com.error("No reader named %s\n", readerName));
+ return;
+ }
+
+ readerArr[i] = readers.get(readerName);
+ }
+
+ BlockReader reader = new SerialBlockReader(readerArr);
+
+ readers.put(blockName, reader);
+ }
+
+ private void defSimple(Command com) {
+ String remn = com.remnCommand;
+
+ /*
+ * Get the block name.
+ */
+ int idx = remn.indexOf(' ');
+ if(idx == -1) {
+ System.err.print(com.error("No name argument for def-simple.\n"));
+ }
+ String blockName = remn.substring(0, idx).trim();
+ remn = remn.substring(idx).trim();
+
+ /*
+ * Check there isn't a reader already bound to this name.
+ */
+ if(readers.containsKey(blockName)) {
+ System.err.print(com.warn("Shadowing existing reader named %s\n", blockName));
+ }
+
+ /*
+ * Get the source name.
+ */
+ idx = remn.indexOf(' ');
+ if(idx == -1) {
+ System.err.print("No source-name argument for def-simple.\n");
+ }
+ String sourceName = remn.substring(0, idx).trim();
+ remn = remn.substring(idx).trim();
+
+ /*
+ * Check there is a source bound to that name.
+ */
+ if(!sources.containsKey(sourceName)) {
+ System.err.print(com.error("No source named %s\n", sourceName));
+ return;
+ }
+
+ /*
+ * Get the pattern.
+ */
+ if(remn.equals("")) {
+ System.err.print("No delimiter argument for def-simple\n");
+ }
+
+ String delim = remn;
+
+ try {
+ BlockReader reader = new SimpleBlockReader(delim, sources.get(sourceName));
+
+ readers.put(blockName, reader);
+ } catch (PatternSyntaxException psex) {
+ System.err.print(com.error("Invalid regular expression '%s' for delimiter. (%s)\n", delim, psex.getMessage()));
+ }
+ }
+}
diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/objects/Command.java b/BJC-Utils2/src/main/java/bjc/utils/cli/objects/Command.java
new file mode 100644
index 0000000..a0d4493
--- /dev/null
+++ b/BJC-Utils2/src/main/java/bjc/utils/cli/objects/Command.java
@@ -0,0 +1,65 @@
+package bjc.utils.cli.objects;
+
+public class Command {
+ public final int lineNo;
+
+ public final String fullCommand;
+ public final String remnCommand;
+ public final String nameCommand;
+
+ public final String ioSource;
+
+ /**
+ * Create a new command.
+ *
+ * @param ln
+ * The line to get the command from.
+ * @param lno
+ * The number of the line the command came from.
+ * @param ioSrc
+ * The name of where the I/O came from.
+ */
+ public Command(String ln, int lno, String ioSrc) {
+ int idx = ln.indexOf(' ');
+
+ if(idx == -1) idx = ln.length();
+
+ fullCommand = ln;
+ nameCommand = ln.substring(0, idx).trim();
+ remnCommand = ln.substring(idx).trim();
+
+ lineNo = lno;
+
+ ioSource = ioSrc;
+ }
+
+ public static Command fromString(String ln, int lno, String ioSource) {
+ /*
+ * Ignore blank lines and comments.
+ */
+ if(ln.equals("")) return null;
+ if(ln.startsWith("#")) return null;
+
+ /*
+ * Trim off comments part-way through the line.
+ */
+ int idxHash = ln.indexOf('#');
+ if(idxHash != -1) {
+ ln = ln.substring(0, idxHash).trim();
+ }
+
+ return new Command(ln, lno, ioSource);
+ }
+
+ public String warn(String warning, Object... parms) {
+ String msg = String.format(warning, parms);
+
+ return String.format("WARNING (%s:%d): %s", ioSource, lineNo, msg);
+ }
+
+ public String error(String err, Object... parms) {
+ String msg = String.format(err, parms);
+
+ return String.format("ERROR (%s:%d): %s", ioSource, lineNo, msg);
+ }
+}