diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-05-10 16:02:45 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-05-10 16:02:45 -0400 |
| commit | 61fd71f69e080790da722e0e03b71ecd7c2538a2 (patch) | |
| tree | e5c1150b27b84d550f807e44ac82688216451f00 /BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java | |
| parent | 87ae1dfc8d8cb7b51d7bda4750ce841bbe691cfc (diff) | |
General update
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java')
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java | 60 |
1 files changed, 17 insertions, 43 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java b/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java index be0a65b..b35c77b 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java +++ b/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java @@ -1,13 +1,11 @@ package bjc.utils.components; import java.io.InputStream; -import java.util.function.BiConsumer; -import bjc.utils.exceptions.PragmaFormatException; -import bjc.utils.funcdata.FunctionalStringTokenizer; -import bjc.utils.funcutils.ListUtils; import bjc.utils.parserutils.RuleBasedConfigReader; +import static bjc.utils.parserutils.RuleBasedReaderPragmas.*; + /** * Read a component description from a file * @@ -15,8 +13,10 @@ import bjc.utils.parserutils.RuleBasedConfigReader; * */ public class ComponentDescriptionFileParser { + // The reader used to read in component descriptions private static RuleBasedConfigReader<ComponentDescriptionState> reader; + // Initialize the reader and its pragmas static { // This reader works entirely off of pragmas, so no need to handle // rules @@ -31,19 +31,6 @@ public class ComponentDescriptionFileParser { setupReaderPragmas(); } - private static BiConsumer<FunctionalStringTokenizer, ComponentDescriptionState> buildStringCollapserPragma( - String pragmaName) { - return (tokenizer, state) -> { - if (!tokenizer.hasMoreTokens()) { - throw new PragmaFormatException("Pragma " + pragmaName - + " requires one string argument"); - } - - state.setName(ListUtils - .collapseTokens(tokenizer.toList((strang) -> strang))); - }; - } - /** * Parse a component description from a stream * @@ -51,42 +38,29 @@ public class ComponentDescriptionFileParser { * The stream to parse from * @return The description parsed from the stream */ - public static ComponentDescription fromStream( - InputStream inputSource) { + public static ComponentDescription + fromStream(InputStream inputSource) { ComponentDescriptionState readState = reader .fromStream(inputSource, new ComponentDescriptionState()); return readState.toDescription(); } + /* + * Create all the pragmas the reader needs to function + */ private static void setupReaderPragmas() { - reader.addPragma("name", buildStringCollapserPragma("name")); + reader.addPragma("name", buildStringCollapser("name", + (name, state) -> state.setName(name))); - reader.addPragma("author", buildStringCollapserPragma("author")); + reader.addPragma("author", buildStringCollapser("author", + (author, state) -> state.setAuthor(author))); reader.addPragma("description", - buildStringCollapserPragma("description")); + buildStringCollapser("description", (description, + state) -> state.setDescription(description))); - reader.addPragma("version", (tokenizer, state) -> { - if (!tokenizer.hasMoreTokens()) { - throw new PragmaFormatException( - "Pragma version requires one integer argument"); - } - - String token = tokenizer.nextToken(); - - try { - state.setVersion(Integer.parseInt(token)); - } catch (NumberFormatException nfex) { - PragmaFormatException pfex = new PragmaFormatException( - "Argument " + token - + " to version pragma isn't a valid integer. " - + "This pragma requires a integer argument"); - - pfex.initCause(nfex); - - throw pfex; - } - }); + reader.addPragma("version", buildInteger("version", + (version, state) -> state.setVersion(version))); } } |
