From 0220a17f18899715664812540949196e91ef8d10 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sun, 26 Mar 2017 22:55:16 -0400 Subject: Make BlockReader abstract, with varying impls. This allows cool things to be accomplished through chaining BlockReaders together without having to handle the block plumbing yourself. The current set of implementations are a simple one that reads blocks from a scanner delimited by a pattern, one that has a queue of blocks it will attempt to pull from before reading, and one that triggers an action before a block is read. As an example use, for FDS, a combo of simple -> triggered -> pushback is used where the triggered is used for prompting the user, and the pushback supports macros. --- .../java/bjc/utils/ioutils/SimpleBlockReader.java | 105 +++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 BJC-Utils2/src/main/java/bjc/utils/ioutils/SimpleBlockReader.java (limited to 'BJC-Utils2/src/main/java/bjc/utils/ioutils/SimpleBlockReader.java') diff --git a/BJC-Utils2/src/main/java/bjc/utils/ioutils/SimpleBlockReader.java b/BJC-Utils2/src/main/java/bjc/utils/ioutils/SimpleBlockReader.java new file mode 100644 index 0000000..329effc --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/ioutils/SimpleBlockReader.java @@ -0,0 +1,105 @@ +package bjc.utils.ioutils; + +import java.io.IOException; +import java.io.LineNumberReader; +import java.io.Reader; +import java.util.NoSuchElementException; +import java.util.Scanner; +import java.util.regex.Pattern; + +/** + * Simple implementation of {@link BlockReader} + * + * NOTE: The EOF marker is always treated as a delimiter. You are expected to + * handle blocks that may be shorter than you expect. + * + * @author EVE + * + */ +public class SimpleBlockReader implements BlockReader { + /* + * I/O source for blocks. + */ + private LineNumberReader lnReader; + private Scanner blockReader; + + /* + * The current block. + */ + private Block currBlock; + private int blockNo; + + /** + * Create a new block reader. + * + * @param blockDelim + * The pattern that separates blocks. Note that the end + * of file is always considered to end a block. + * + * @param source + * The source to read blocks from. + */ + public SimpleBlockReader(String blockDelim, Reader source) { + lnReader = new LineNumberReader(source); + + blockReader = new Scanner(lnReader); + + String pattern = String.format("(?:%s)|\\Z", blockDelim); + Pattern pt = Pattern.compile(pattern, Pattern.MULTILINE); + + blockReader.useDelimiter(pt); + } + + @Override + public boolean hasNextBlock() { + return blockReader.hasNext(); + } + + @Override + public Block getBlock() { + return currBlock; + } + + @Override + public boolean nextBlock() { + try { + int blockStartLine = lnReader.getLineNumber(); + String blockContents = blockReader.next(); + int blockEndLine = lnReader.getLineNumber(); + blockNo += 1; + + currBlock = new Block(blockNo, blockContents, blockStartLine, blockEndLine); + + return true; + } catch (NoSuchElementException nseex) { + return false; + } + } + + @Override + public int getBlockCount() { + return blockNo; + } + + @Override + public void close() throws IOException { + blockReader.close(); + + lnReader.close(); + } + + /** + * Set the delimiter used to separate blocks. + * + * @param delim + * The delimiter used to separate blocks. + */ + public void setDelimiter(String delim) { + blockReader.useDelimiter(delim); + } + + @Override + public String toString() { + return String.format("SimpleBlockReader [currBlock=%s, blockNo=%s]", currBlock, blockNo); + } +} -- cgit v1.2.3