From f4c758dc79b704865ac5ae96c6e48aac6ab1d097 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Mon, 28 Mar 2016 19:59:48 -0400 Subject: Added parser for component descriptions --- .../components/ComponentDescriptionFileParser.java | 54 ++++++++++++++++++ .../components/ComponentDescriptionState.java | 66 ++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java create mode 100644 BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionState.java (limited to 'BJC-Utils2/src/main/java/bjc') diff --git a/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java b/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java new file mode 100644 index 0000000..bf0f0bd --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java @@ -0,0 +1,54 @@ +package bjc.utils.components; + +import java.io.InputStream; + +import bjc.utils.funcutils.ListUtils; +import bjc.utils.parserutils.RuleBasedConfigReader; + +/** + * Read a component description from a file + * + * @author ben + * + */ +public class ComponentDescriptionFileParser { + private static RuleBasedConfigReader reader; + + static { + // This reader works entirely off of pragmas, so no need to handle + // rules + reader = new RuleBasedConfigReader<>((fst, par) -> { + }, (fst, stat) -> { + }, (stat) -> { + }); + + reader.addPragma("name", (fst, stat) -> { + stat.setName(ListUtils.collapseTokens(fst.toList((s) -> s))); + }); + + reader.addPragma("author", (fst, stat) -> { + stat.setAuthor(ListUtils.collapseTokens(fst.toList((s) -> s))); + }); + + reader.addPragma("description", (fst, stat) -> { + stat.setDescription( + ListUtils.collapseTokens(fst.toList((s) -> s))); + }); + + reader.addPragma("version", (fst, stat) -> { + stat.setVersion(Integer.parseInt(fst.nextToken())); + }); + } + + /** + * Parse a component description from a stream + * + * @param is + * The stream to parse from + * @return The description parsed from the stream + */ + public ComponentDescription fromStream(InputStream is) { + return reader.fromStream(is, new ComponentDescriptionState()) + .toDescription(); + } +} diff --git a/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionState.java b/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionState.java new file mode 100644 index 0000000..6c374be --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionState.java @@ -0,0 +1,66 @@ +package bjc.utils.components; + +/** + * Internal state of component description parser + * + * @author ben + * + */ +public class ComponentDescriptionState { + private String name; + private String description; + + private String author; + + private int version; + + /** + * Convert this state into the description it represents + * + * @return The description represented by this state + */ + public ComponentDescription toDescription() { + return new ComponentDescription(name, author, description, + version); + } + + /** + * Set the name of this component + * + * @param name + * The name of this component + */ + public void setName(String name) { + this.name = name; + } + + /** + * Set the description of this component + * + * @param description + * The description of this component + */ + public void setDescription(String description) { + this.description = description; + } + + /** + * Set the author of this component + * + * @param author + * The author of this component + */ + public void setAuthor(String author) { + this.author = author; + } + + /** + * Set the version of this component + * + * @param version + * The version of this component + */ + public void setVersion(int version) { + this.version = version; + } +} -- cgit v1.2.3