diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-04-03 19:22:48 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-04-03 19:22:48 -0400 |
| commit | 1c8bc7132d980c1ff2dbd6b9af579c3b2fd8c63e (patch) | |
| tree | a29777f07ebd81fbef61b5ae02f13f1a9d8f65a2 /BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedConfigReader.java | |
| parent | a023de85aa08c8f2b8b2441c6b14064eabee2775 (diff) | |
General code refactoring and maintenance
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedConfigReader.java')
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedConfigReader.java | 110 |
1 files changed, 79 insertions, 31 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedConfigReader.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedConfigReader.java index 46f5f1d..b6162e5 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedConfigReader.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedConfigReader.java @@ -2,6 +2,7 @@ package bjc.utils.parserutils; import java.io.InputStream; import java.util.HashMap; +import java.util.InputMismatchException; import java.util.Map; import java.util.Scanner; import java.util.function.BiConsumer; @@ -58,6 +59,13 @@ public class RuleBasedConfigReader<E> { */ public void addPragma(String pragmaName, BiConsumer<FunctionalStringTokenizer, E> pragmaAction) { + if (pragmaName == null) { + throw new NullPointerException("Pragma name must not be null"); + } else if (pragmaAction == null) { + throw new NullPointerException( + "Pragma action must not be null"); + } + pragmas.put(pragmaName, pragmaAction); } @@ -71,42 +79,77 @@ public class RuleBasedConfigReader<E> { * @return The final state of the reader */ public E fromStream(InputStream inputStream, E initialState) { - Scanner inputSource = new Scanner(inputStream); - - E state = initialState; - - while (inputSource.hasNextLine()) { - String line = inputSource.nextLine(); - - if (line.equals("")) { - endRule.accept(state); - continue; - } else if (line.startsWith("\t")) { - continueRule.accept(new FunctionalStringTokenizer( - line.substring(1), " "), state); - } else { - FunctionalStringTokenizer tokenizer = - new FunctionalStringTokenizer(line, " "); - - String nextToken = tokenizer.nextToken(); - if (nextToken.equals("#")) { - // Do nothing, this is a comment - } else if (nextToken.equals("pragma")) { - String token = tokenizer.nextToken(); - - pragmas.getOrDefault(token, (tokenzer, stat) -> { - throw new UnknownPragmaException( - "Unknown pragma " + token); - }).accept(tokenizer, state); + if (inputStream == null) { + throw new NullPointerException( + "Input stream must not be null"); + } + + E state; + + try (Scanner inputSource = new Scanner(inputStream)) { + + state = initialState; + boolean ruleOpen = false; + while (inputSource.hasNextLine()) { + String line = inputSource.nextLine(); + + if (line.equals("")) { + if (ruleOpen == false) { + // Ignore blank line without an open rule + } + + if (endRule == null) { + // Nothing happens on rule end + } else { + endRule.accept(state); + } + + continue; + } else if (line.startsWith("\t")) { + if (ruleOpen == false) { + throw new InputMismatchException( + "Can't continue rule with no rule currently open"); + } + + if (continueRule == null) { + throw new InputMismatchException( + "Attempted to continue rule with rule continuation disabled." + + " Check for extraneous tabs"); + } + + continueRule.accept(new FunctionalStringTokenizer( + line.substring(1), " "), state); } else { - startRule.accept(tokenizer, - new Pair<>(nextToken, state)); + FunctionalStringTokenizer tokenizer = + new FunctionalStringTokenizer(line, " "); + + String nextToken = tokenizer.nextToken(); + + if (nextToken.equals("#") || nextToken.equals("//")) { + // Do nothing, this is a comment + } else if (nextToken.equals("pragma")) { + String token = tokenizer.nextToken(); + + pragmas.getOrDefault(token, (tokenzer, stat) -> { + throw new UnknownPragmaException( + "Unknown pragma " + token); + }).accept(tokenizer, state); + } else { + if (ruleOpen == true) { + throw new InputMismatchException( + "Attempted to open a" + + " rule with a rule already open. Make sure rules are" + + " seperated by blank lines"); + } + + startRule.accept(tokenizer, + new Pair<>(nextToken, state)); + ruleOpen = true; + } } } } - inputSource.close(); - return state; } @@ -139,6 +182,11 @@ public class RuleBasedConfigReader<E> { */ public void setStartRule( BiConsumer<FunctionalStringTokenizer, Pair<String, E>> startRule) { + if (startRule == null) { + throw new NullPointerException( + "Action on rule start must be non-null"); + } + this.startRule = startRule; } } |
