From b21a2b6ba801d5ff69e7f87d74a91c09c2683800 Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Sat, 13 Oct 2012 20:14:49 -0700 Subject: Added config parsing. Should add a unit test for the parser later. --- src/net/jesterpm/sermonuploader/config/Config.java | 92 +++++++++++++++++++++- 1 file changed, 91 insertions(+), 1 deletion(-) diff --git a/src/net/jesterpm/sermonuploader/config/Config.java b/src/net/jesterpm/sermonuploader/config/Config.java index ed239c5..9c9cc20 100644 --- a/src/net/jesterpm/sermonuploader/config/Config.java +++ b/src/net/jesterpm/sermonuploader/config/Config.java @@ -4,6 +4,14 @@ package net.jesterpm.sermonuploader.config; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.File; + +import java.text.ParseException; + import java.util.Map; import java.util.HashMap; @@ -14,7 +22,14 @@ import java.util.HashMap; */ public class Config { private final String mFilename; - private final Map mConfig; + private Map mConfig; + + /** + * States the parser can be in. + */ + private enum TokenizerState { + OUT_OF_ENTRY, IN_ENTRY; + } /** * Create a new Config object based on the given file. @@ -26,11 +41,86 @@ public class Config { } private void parse() { + try { + final File configfile = new File(mFilename); + if (configfile.isFile()) { + BufferedReader in = new BufferedReader(new FileReader(mFilename)); + + int lineno = 0; + String line; + String key = ""; + String value = ""; + TokenizerState state = TokenizerState.OUT_OF_ENTRY; + while ((line = in.readLine()) != null) { + lineno++; + if (line.length() == 0) { + continue; + } + switch (state) { + case IN_ENTRY: + if (Character.isWhitespace(line.charAt(0))) { + value += "\n" + line.trim(); + break; + + } else { + // Beginning new entry. Save old and pass through. + mConfig.put(key, value); + key = value = ""; + state = TokenizerState.OUT_OF_ENTRY; + // NB Intentionally falling through... + } + + case OUT_OF_ENTRY: + if (line.charAt(0) == '#') { + continue; + } + + final int pos = line.indexOf(':'); + if (pos == -1) { + throw new ParseException("Missing : at line " + lineno, lineno); + } + key = line.substring(0, pos).trim().toLowerCase(); + if (key.length() == 0) { + throw new ParseException("Zero-length key on line " + lineno, + lineno); + } + value = line.substring(pos + 1).trim(); + state = TokenizerState.IN_ENTRY; + break; + } + } + + // Catch last key/value + if (state == TokenizerState.IN_ENTRY) { + mConfig.put(key, value); + } + + in.close(); + } + + } catch (final Exception e) { + System.err.println("[ERROR] Failed to load config from " + mFilename + ". " + + e.getMessage()); + } } public void save() { + try { + BufferedWriter out = new BufferedWriter(new FileWriter(mFilename)); + + for (Map.Entry entry : mConfig.entrySet()) { + out.write(entry.getKey()); + out.write(": "); + out.write(entry.getValue()); + out.newLine(); + } + + out.close(); + } catch (final Exception e) { + System.err.println("[ERROR] Failed to save configuration: " + e.getMessage()); + } } public String get(final String key) { -- cgit v1.2.3