diff options
| author | EVE <EVE@EVE-PC> | 2017-03-14 12:07:14 -0400 |
|---|---|---|
| committer | EVE <EVE@EVE-PC> | 2017-03-14 12:07:14 -0400 |
| commit | 504ca816530efdff06bc202e0432ebd354aec304 (patch) | |
| tree | 4836932fb81d1d625470502c78c94d202c9a7420 /BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedConfigReader.java | |
| parent | 5c1163df17c46f7d3e15b6c7949c38843ec56146 (diff) | |
Cleanup
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 | 66 |
1 files changed, 29 insertions, 37 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 2da4f7e..b26030c 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedConfigReader.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedConfigReader.java @@ -1,11 +1,5 @@ package bjc.utils.parserutils; -import java.io.InputStream; -import java.util.InputMismatchException; -import java.util.Scanner; -import java.util.function.BiConsumer; -import java.util.function.Consumer; - import bjc.utils.data.IHolder; import bjc.utils.data.IPair; import bjc.utils.data.Identity; @@ -15,15 +9,21 @@ import bjc.utils.funcdata.FunctionalMap; import bjc.utils.funcdata.FunctionalStringTokenizer; import bjc.utils.funcdata.IMap; +import java.io.InputStream; +import java.util.InputMismatchException; +import java.util.Scanner; +import java.util.function.BiConsumer; +import java.util.function.Consumer; + /** * This class parses a rules based config file, and uses it to drive a provided * set of actions - * + * * @author ben * * @param <E> * The type of the state object to use - * + * */ public class RuleBasedConfigReader<E> { // Function to execute when starting a rule @@ -45,7 +45,7 @@ public class RuleBasedConfigReader<E> { /** * Create a new rule-based config reader - * + * * @param start * The action to fire when starting a rule * @param continueRule @@ -64,29 +64,26 @@ public class RuleBasedConfigReader<E> { /** * Add a pragma to this reader - * + * * @param name * The name of the pragma to add * @param action * The function to execute when this pragma is read */ public void addPragma(String name, BiConsumer<FunctionalStringTokenizer, E> action) { - if (name == null) { + if(name == null) throw new NullPointerException("Pragma name must not be null"); - } else if (action == null) { - throw new NullPointerException("Pragma action must not be null"); - } + else if(action == null) throw new NullPointerException("Pragma action must not be null"); pragmas.put(name, action); } private void continueRule(E state, boolean isRuleOpen, String line) { // Make sure our input is correct - if (isRuleOpen == false) { + if(isRuleOpen == false) throw new InputMismatchException("Cannot continue rule with no rule open"); - } else if (continueRule == null) { + else if(continueRule == null) throw new InputMismatchException("Rule continuation not supported for current grammar"); - } // Accept the rule continueRule.accept(new FunctionalStringTokenizer(line.substring(1), " "), state); @@ -94,12 +91,12 @@ public class RuleBasedConfigReader<E> { private boolean endRule(E state, boolean isRuleOpen) { // Ignore blank line without an open rule - if (isRuleOpen == false) { + if(isRuleOpen == false) // Do nothing return false; - } else { + else { // Nothing happens on rule end - if (end != null) { + if(end != null) { // Process the rule ending end.accept(state); } @@ -111,7 +108,7 @@ public class RuleBasedConfigReader<E> { /** * Run a stream through this reader - * + * * @param input * The stream to get input * @param initialState @@ -119,15 +116,13 @@ public class RuleBasedConfigReader<E> { * @return The final state of the reader */ public E fromStream(InputStream input, E initialState) { - if (input == null) { - throw new NullPointerException("Input stream must not be null"); - } + if(input == null) throw new NullPointerException("Input stream must not be null"); // Application state: We're giving this back later E state = initialState; // Prepare our input source - try (Scanner source = new Scanner(input)) { + try(Scanner source = new Scanner(input)) { source.useDelimiter("\n"); // This is true when a rule's open IHolder<Boolean> isRuleOpen = new Identity<>(false); @@ -135,13 +130,13 @@ public class RuleBasedConfigReader<E> { // Do something for every line of the file source.forEachRemaining((line) -> { // Skip comment lines - if (line.startsWith("#") || line.startsWith("//")) { + if(line.startsWith("#") || line.startsWith("//")) // It's a comment return; - } else if (line.equals("")) { + else if(line.equals("")) { // End the rule isRuleOpen.replace(endRule(state, isRuleOpen.getValue())); - } else if (line.startsWith("\t")) { + } else if(line.startsWith("\t")) { // Continue the rule continueRule(state, isRuleOpen.getValue(), line); } else { @@ -158,7 +153,7 @@ public class RuleBasedConfigReader<E> { /** * Set the action to execute when continuing a rule - * + * * @param continueRule * The action to execute on continuation of a rule */ @@ -168,7 +163,7 @@ public class RuleBasedConfigReader<E> { /** * Set the action to execute when ending a rule - * + * * @param end * The action to execute on ending of a rule */ @@ -178,14 +173,12 @@ public class RuleBasedConfigReader<E> { /** * Set the action to execute when starting a rule - * + * * @param start * The action to execute on starting of a rule */ public void setStartRule(BiConsumer<FunctionalStringTokenizer, IPair<String, E>> start) { - if (start == null) { - throw new NullPointerException("Action on rule start must be non-null"); - } + if(start == null) throw new NullPointerException("Action on rule start must be non-null"); this.start = start; } @@ -198,7 +191,7 @@ public class RuleBasedConfigReader<E> { String nextToken = tokenizer.nextToken(); // Handle pragmas - if (nextToken.equals("pragma")) { + if(nextToken.equals("pragma")) { // Get the pragma name String token = tokenizer.nextToken(); @@ -208,9 +201,8 @@ public class RuleBasedConfigReader<E> { }).accept(tokenizer, state); } else { // Make sure input is correct - if (isRuleOpen == true) { + if(isRuleOpen == true) throw new InputMismatchException("Nested rules are currently not supported"); - } // Start a rule start.accept(tokenizer, new Pair<>(nextToken, state)); |
