summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/net/jesterpm/sermonuploader/config/Config.java92
1 files changed, 91 insertions, 1 deletions
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<String, String> mConfig;
+ private Map<String, String> 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<String, String> 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) {