From e937dcb4bb5ebf74eaf4eb14a84c160714a8246e Mon Sep 17 00:00:00 2001 From: md_5 Date: Thu, 7 Mar 2013 18:17:49 +1100 Subject: [PATCH] Add uncommited config stuffs, just to get a clean working tree. --- config/pom.xml | 14 +++++ .../net/md_5/bungee/config/Configuration.java | 14 +++-- .../bungee/config/ConfigurationProvider.java | 29 ++++++++++ .../md_5/bungee/config/YamlConfiguration.java | 53 +++++++++++++++++++ 4 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 config/src/main/java/net/md_5/bungee/config/ConfigurationProvider.java create mode 100644 config/src/main/java/net/md_5/bungee/config/YamlConfiguration.java diff --git a/config/pom.xml b/config/pom.xml index 9df70007..ad8761be 100644 --- a/config/pom.xml +++ b/config/pom.xml @@ -17,4 +17,18 @@ BungeeCord-Config Generic java configuration API intended for use with BungeeCord + + + + junit + junit + 4.11 + test + + + org.yaml + snakeyaml + 1.11 + + diff --git a/config/src/main/java/net/md_5/bungee/config/Configuration.java b/config/src/main/java/net/md_5/bungee/config/Configuration.java index f9d32c82..43af0fff 100644 --- a/config/src/main/java/net/md_5/bungee/config/Configuration.java +++ b/config/src/main/java/net/md_5/bungee/config/Configuration.java @@ -2,6 +2,7 @@ package net.md_5.bungee.config; import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; import lombok.AccessLevel; @@ -13,8 +14,8 @@ public final class Configuration private static final char SEPARATOR = '.'; private final Map self; - private final Map comments; - private final Map defaults; + private Map comments = new HashMap<>(); + private final Configuration defaults; private Map getHolder(String path, Map parent, boolean create) { @@ -41,12 +42,17 @@ public final class Configuration public T get(String path, T def) { Object val = get( path, self ); - return ( val != null && val.getClass().isInstance( def ) ) ? (T) val : (T) get( path, defaults ); + return ( val != null && val.getClass().isInstance( def ) ) ? (T) val : (T) defaults.get( path ); + } + + public Object get(String path) + { + return get( path, null ); } public Object getDefault(String path) { - return get( path, defaults ); + return defaults.get( path ); } public void set(String path, Object value, String comment) diff --git a/config/src/main/java/net/md_5/bungee/config/ConfigurationProvider.java b/config/src/main/java/net/md_5/bungee/config/ConfigurationProvider.java new file mode 100644 index 00000000..7d1b5327 --- /dev/null +++ b/config/src/main/java/net/md_5/bungee/config/ConfigurationProvider.java @@ -0,0 +1,29 @@ +package net.md_5.bungee.config; + +import java.io.File; +import java.io.Reader; +import java.util.HashMap; +import java.util.Map; + +public abstract class ConfigurationProvider +{ + + private static final Map, ConfigurationProvider> providers = new HashMap<>(); + + static + { + providers.put( YamlConfiguration.class, new YamlConfiguration() ); + } + + public ConfigurationProvider getProvider(Class provider) + { + return providers.get( provider ); + } + /*------------------------------------------------------------------------*/ + + public abstract Configuration load(File file); + + public abstract Configuration load(Reader reader); + + public abstract Configuration load(String string); +} diff --git a/config/src/main/java/net/md_5/bungee/config/YamlConfiguration.java b/config/src/main/java/net/md_5/bungee/config/YamlConfiguration.java new file mode 100644 index 00000000..74d84ab8 --- /dev/null +++ b/config/src/main/java/net/md_5/bungee/config/YamlConfiguration.java @@ -0,0 +1,53 @@ +package net.md_5.bungee.config; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.io.Reader; +import java.util.Map; +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.Yaml; + +public class YamlConfiguration extends ConfigurationProvider +{ + + private final ThreadLocal yaml = new ThreadLocal() + { + @Override + protected Yaml initialValue() + { + DumperOptions options = new DumperOptions(); + options.setDefaultFlowStyle( DumperOptions.FlowStyle.BLOCK ); + return new Yaml( options ); + } + }; + + @Override + public Configuration load(File file) + { + try ( FileReader reader = new FileReader( file ) ) + { + return load( reader ); + } catch ( IOException ex ) + { + return null; + } + } + + @Override + @SuppressWarnings("unchecked") + public Configuration load(Reader reader) + { + Configuration conf = new Configuration( (Map) yaml.get().loadAs( reader, Map.class ), null ); + return conf; + } + + @Override + @SuppressWarnings("unchecked") + public Configuration load(String string) + { + Configuration conf = new Configuration( (Map) yaml.get().loadAs( string, Map.class ), null ); + return conf; + } +}