summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/configuration/ConfigFile.java
diff options
context:
space:
mode:
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/configuration/ConfigFile.java')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/configuration/ConfigFile.java147
1 files changed, 147 insertions, 0 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/configuration/ConfigFile.java b/BJC-Utils2/src/main/java/bjc/utils/configuration/ConfigFile.java
new file mode 100644
index 0000000..056f775
--- /dev/null
+++ b/BJC-Utils2/src/main/java/bjc/utils/configuration/ConfigFile.java
@@ -0,0 +1,147 @@
+package bjc.utils.configuration;
+
+import java.io.InputStream;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Scanner;
+
+import javax.lang.model.SourceVersion;
+
+/**
+ * A config file with support for categories, comments and other thins
+ *
+ * @author ben
+ *
+ */
+public class ConfigFile {
+ private static Map<String, ConfigCategory> topLevelCategories;
+
+ /**
+ * A category in a configuration file
+ * @author ben
+ *
+ */
+ public static class ConfigCategory {
+ private Map<String, ConfigCategory> children;
+
+ /**
+ * Create a new config category
+ */
+ public ConfigCategory() {
+ children = new HashMap<>();
+ }
+
+ public void addChild(String childName, ConfigCategory child) {
+ children.put(childName, child);
+ }
+ }
+
+ /**
+ * Parse the values in a config file from a stream
+ *
+ * @param source
+ * The stream to parse values from
+ * @return A config file with values parsed from the stream
+ */
+ public static ConfigFile parse(InputStream source) {
+ Scanner scn = new Scanner(source);
+
+ ConfigFile returnedFile = new ConfigFile();
+
+ while (scn.hasNextLine()) {
+ String currentLine = scn.nextLine();
+
+ // Ignore blank lines
+ if (currentLine.equals("")) {
+ continue;
+ }
+
+ String[] currentTokens = currentLine.split(" ");
+
+ // Ignore lines that start with a comment marker
+ if (isCommentMarker(currentTokens[0])) {
+ continue;
+ } else if (SourceVersion.isName(currentTokens[0])
+ && currentTokens[1].equals("{")) {
+ topLevelCategories.put(currentTokens[0], parseCategory(currentTokens[0], scn));
+ }
+ }
+
+ return returnedFile;
+ }
+
+ private static ConfigCategory parseCategory(String categoryName,
+ Scanner inputSource) {
+ ConfigCategory category = new ConfigCategory();
+
+ String currentLine = inputSource.nextLine();
+
+ String[] tokens = currentLine.split("\\s+ ");
+
+ // Parse contents of category
+ while (!tokens[0].equals("}")) {
+ // Ignore lines starting with comment marker
+ if (isCommentMarker(tokens[0])) {
+ continue;
+ }
+
+ int initialCommandToken = 0;
+
+ // Skip over blank tokens from lots of spacing
+ for (int i = 0; i < tokens.length; i++) {
+ String token = tokens[i];
+
+ if (token.equals("")) {
+ continue;
+ } else {
+ initialCommandToken = i;
+ break;
+ }
+ }
+
+ String[] relevantTokens = Arrays.copyOfRange(tokens,
+ initialCommandToken, tokens.length);
+
+ // Parse child subcategories
+ if (SourceVersion.isName(relevantTokens[0])
+ && relevantTokens[1].equals("{")) {
+ parseCategory(relevantTokens[0], inputSource);
+ } else {
+ // Parse config fields
+ parseEntry(category, relevantTokens);
+ }
+ currentLine = inputSource.nextLine();
+
+ tokens = currentLine.split("\\s+ ");
+ }
+
+ return category;
+ }
+
+ private static void parseEntry(ConfigCategory category,
+ String[] entryParts) {
+ String entry = String.join("", entryParts);
+
+ String[] expParts = entry.split("=");
+
+ String[] expSpecifiers = expParts[0].split(":");
+
+ String expType = expSpecifiers[0];
+ String expName = expSpecifiers[1];
+
+ switch (expType) {
+
+ }
+ }
+
+ private static boolean isCommentMarker(String token) {
+ switch (token) {
+ case "#":
+ case "//":
+ return true;
+ default:
+ return false;
+ }
+ }
+}