Created
August 21, 2012 21:49
-
-
Save EntityReborn/3419719 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * To change this template, choose Tools | Templates | |
| * and open the template in the editor. | |
| */ | |
| package com.gmail.entityreborn.launchii.config; | |
| import java.util.HashMap; | |
| import java.util.List; | |
| import java.util.Map; | |
| import java.util.Set; | |
| /** | |
| * | |
| * @author __import__ | |
| */ | |
| public class ConfigSection { | |
| private Map<String, Object> children; | |
| private ConfigurationFile manager; | |
| private ConfigSection parent; | |
| private String name; | |
| /** | |
| * | |
| * @param d Incoming data for this section | |
| * @param man Controlling manager for this instance | |
| */ | |
| public ConfigSection(Map<String, ?> d, ConfigurationFile man, ConfigSection p, String n) { | |
| parent = p; | |
| name = n; | |
| manager = man; | |
| children = new HashMap<String, Object>(); | |
| parseChildren(d); | |
| } | |
| public ConfigSection getParent() { | |
| return parent; | |
| } | |
| public String getSectionID() { | |
| return name; | |
| } | |
| /** | |
| * Clone the current section. Will probably orphan the new instance. | |
| * @return | |
| */ | |
| @Override | |
| public ConfigSection clone() { | |
| return new ConfigSection(asMap(), manager, parent, name); | |
| } | |
| /** | |
| * Clone the current section, and set a new manager. | |
| * @param man | |
| * @return | |
| */ | |
| public ConfigSection clone(ConfigurationFile man) { | |
| return new ConfigSection(asMap(), man, parent, name); | |
| } | |
| /** | |
| * Merge everything from map into the current section | |
| * @param map Incoming data | |
| */ | |
| public void merge(Map<String, Object> map) { | |
| for (String key : map.keySet()) { | |
| Object o = map.get(key); | |
| if (o instanceof Map) { | |
| ConfigSection c = getChildSection(key, true); | |
| if (c == null) { | |
| continue; | |
| } | |
| c.merge((Map<String, Object>)o); | |
| } else { | |
| set(key, o); | |
| } | |
| } | |
| } | |
| /** | |
| * | |
| * @param sect | |
| */ | |
| public void merge(ConfigSection sect) { | |
| merge(sect.asMap()); | |
| } | |
| public ConfigSection getRootSection() { | |
| return manager.rootSection(); | |
| } | |
| public ConfigurationFile getManager() { | |
| return manager; | |
| } | |
| public boolean has(String key){ | |
| return children.containsKey(key); | |
| } | |
| public Object get(String key) { | |
| return children.get(key); | |
| } | |
| public Object getRecursive(String key) { | |
| if (has(key)) { | |
| if (key.contains(".")) { | |
| int lastdot = key.lastIndexOf("."); | |
| String nodes = key.substring(0, lastdot); | |
| String newkey = key.substring(lastdot + 1, key.length()); | |
| ConfigSection sect = getChildSection(nodes, false); | |
| return sect.getRecursive(newkey); | |
| } else { | |
| return children.get(key); | |
| } | |
| } | |
| return null; | |
| } | |
| public void set(String key, Object value) { | |
| children.put(key, value); | |
| } | |
| public void setRecursive(String key, Object value) { | |
| if (key.contains(".")) { | |
| int lastdot = key.lastIndexOf("."); | |
| String nodes = key.substring(0, lastdot); | |
| String newkey = key.substring(lastdot + 1, key.length()); | |
| ConfigSection sect = getChildSection(nodes, true); | |
| sect.set(newkey, value); | |
| } else { | |
| children.put(key, value); | |
| } | |
| } | |
| public ConfigSection getConfigSection(String key) { | |
| return getChildSection(key, false); | |
| } | |
| protected ConfigSection getChildSection(String key, boolean create) { | |
| String[] nodes = key.split("\\.", 2); | |
| if (nodes.length == 2) { | |
| if (create && !has(nodes[0])) { | |
| setMap(nodes[0], new HashMap<String, Object>()); | |
| } | |
| return ((ConfigSection)getRecursive(nodes[0])).getChildSection(nodes[1], create); | |
| } else { | |
| if (create && (!has(nodes[0]) || !(get(nodes[0]) instanceof ConfigSection))) { | |
| setMap(nodes[0], new HashMap<String, Object>()); | |
| } | |
| return (ConfigSection)get(nodes[0]); | |
| } | |
| } | |
| protected final void parseChildren(Map<String, ?> data){ | |
| if (data == null) { | |
| data = new HashMap<String, Object>(); | |
| } | |
| for (String key: data.keySet()) { | |
| Object o = data.get(key); | |
| if (o instanceof Map) { | |
| ConfigSection conf = new ConfigSection( | |
| ((Map<String, ?>)o), manager, | |
| parent, name); | |
| set(key, conf); | |
| } else { | |
| set(key, o); | |
| } | |
| } | |
| } | |
| public void delete(String key) { | |
| children.remove(key); | |
| } | |
| public Map<String, Object> asMap() { | |
| Map<String, Object> map = new HashMap<String, Object>(); | |
| for (String key: children.keySet()) { | |
| Object x = children.get(key); | |
| if (x instanceof ConfigSection) { | |
| map.put(key, ((ConfigSection)x).asMap()); | |
| } else { | |
| map.put(key, children.get(key)); | |
| } | |
| } | |
| return map; | |
| } | |
| public void setMap(String key, Map<String, Object> map) { | |
| ConfigSection conf = new ConfigSection( | |
| map, manager, parent, key); | |
| set(key, conf); | |
| } | |
| public Map<String, Object> getMap(String key, Map def) { | |
| if (!has(key)) { | |
| return def; | |
| } | |
| try { | |
| return ((ConfigSection)(get(key))).asMap(); | |
| } catch (NullPointerException e) { | |
| return def; | |
| } | |
| } | |
| public boolean getBool(String key, boolean def) { | |
| Boolean b; | |
| if (!has(key)) { | |
| return def; | |
| } | |
| try { | |
| b = (Boolean)get(key); | |
| } catch (NullPointerException e) { | |
| return def; | |
| } | |
| return b.booleanValue(); | |
| } | |
| public void setBool(String key, boolean bool) { | |
| Boolean b = Boolean.valueOf(bool); | |
| set(key, b); | |
| } | |
| public int getInt(String key, int def) { | |
| Integer i; | |
| if (!has(key)) { | |
| return def; | |
| } | |
| try { | |
| i = (Integer)get(key); | |
| } catch (NullPointerException e) { | |
| return def; | |
| } | |
| return i.intValue(); | |
| } | |
| public void setInt(String key, int val) { | |
| Integer i = Integer.valueOf(val); | |
| set(key, i); | |
| } | |
| public float getFloat(String key, float def) { | |
| Float f; | |
| if (!has(key)) { | |
| return def; | |
| } | |
| try { | |
| f = (Float)get(key); | |
| } catch (NullPointerException e) { | |
| return def; | |
| } | |
| return f.floatValue(); | |
| } | |
| public void setFloat(String key, float val) { | |
| Float f = Float.valueOf(val); | |
| set(key, f); | |
| } | |
| public String getString(String key, String def) { | |
| if (!has(key)) { | |
| return def; | |
| } | |
| try { | |
| return (String)get(key); | |
| } catch (NullPointerException e) { | |
| return def; | |
| } | |
| } | |
| public void setString(String key, String val) { | |
| set(key, val); | |
| } | |
| public List getList(String key, List def) { | |
| List<?> l; | |
| if (!has(key)) { | |
| return def; | |
| } | |
| try { | |
| l = (List<?>)get(key); | |
| } catch (NullPointerException e) { | |
| return def; | |
| } | |
| return l; | |
| } | |
| public void setList(String key, List<?> l) { | |
| set(key, l); | |
| } | |
| public Set<String> getKeys() { | |
| return children.keySet(); | |
| } | |
| public Set<String> getKeys(String key) { | |
| return ((ConfigSection)(get(key))).getKeys(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * To change this template, choose Tools | Templates | |
| * and open the template in the editor. | |
| */ | |
| package com.gmail.entityreborn.launchii.config; | |
| import java.io.*; | |
| import java.util.Map; | |
| import java.util.logging.Level; | |
| import java.util.logging.Logger; | |
| import org.yaml.snakeyaml.DumperOptions; | |
| import org.yaml.snakeyaml.Yaml; | |
| /** | |
| * | |
| * @author __import__ | |
| */ | |
| public class ConfigurationFile { | |
| private String filename; | |
| private ConfigSection root; | |
| /** | |
| * | |
| * @param fname | |
| * @throws FileNotFoundException | |
| */ | |
| public ConfigurationFile(String fname) throws FileNotFoundException { | |
| filename = fname; | |
| reload(); | |
| } | |
| /** | |
| * | |
| * @param stream | |
| * @throws FileNotFoundException | |
| */ | |
| public ConfigurationFile(InputStream stream) throws FileNotFoundException { | |
| reload(stream); | |
| } | |
| /** | |
| * | |
| * @param file | |
| * @throws IOException | |
| */ | |
| public ConfigurationFile(File file) throws IOException { | |
| reload(file); | |
| } | |
| /** | |
| * | |
| * @param sect | |
| */ | |
| public void setDefaults(ConfigSection sect) { | |
| sect.merge(root); | |
| root = sect.clone(this); | |
| } | |
| /** | |
| * | |
| * @param stream | |
| */ | |
| public final void reload(InputStream stream) { | |
| Yaml yaml = new Yaml(); | |
| Map<String, Object> data = (Map<String, Object>) yaml.load(stream); | |
| root = new ConfigSection(data, this, null, ""); | |
| } | |
| /** | |
| * | |
| * @param file | |
| * @throws IOException | |
| */ | |
| public final void reload(File file) throws IOException { | |
| if (!file.exists()) { | |
| file.createNewFile(); | |
| } | |
| reload(new FileInputStream(file)); | |
| filename = file.getPath(); | |
| } | |
| /** | |
| * | |
| * @throws FileNotFoundException | |
| */ | |
| public final void reload() throws FileNotFoundException { | |
| FileInputStream f = new FileInputStream(filename); | |
| reload(f); | |
| } | |
| /** | |
| * | |
| * @param stream | |
| */ | |
| public void save(OutputStreamWriter stream) { | |
| DumperOptions options = new DumperOptions(); | |
| options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); | |
| Yaml yaml = new Yaml(options); | |
| yaml.dump(root.asMap(), stream); | |
| } | |
| /** | |
| * | |
| * @param file | |
| */ | |
| public void save(File file) { | |
| OutputStreamWriter stream = null; | |
| try { | |
| stream = new FileWriter(file); | |
| } catch (IOException ex) { | |
| Logger.getLogger(ConfigurationFile.class.getName()).log(Level.SEVERE, ex.getMessage(), ex); | |
| } | |
| if (stream != null) | |
| save(stream); | |
| } | |
| /** | |
| * | |
| */ | |
| public void save() { | |
| FileWriter f = null; | |
| try { | |
| f = new FileWriter(filename); | |
| } catch (IOException ex) { | |
| Logger.getLogger(ConfigurationFile.class.getName()).log(Level.SEVERE, ex.getMessage(), ex); | |
| } | |
| if (f != null) | |
| save(f); | |
| } | |
| /** | |
| * | |
| * @return | |
| */ | |
| public ConfigSection rootSection() { | |
| return root; | |
| } | |
| /** | |
| * | |
| * @param node | |
| * @return | |
| */ | |
| public ConfigSection getChildSection(String node) { | |
| String nodes = node; | |
| ConfigSection current = root; | |
| while (!nodes.isEmpty()) { | |
| String[] splitnodes = nodes.split("\\.", 2); | |
| current = current.getChildSection(splitnodes[0], false); | |
| if (splitnodes.length == 2) { | |
| nodes = splitnodes[1]; | |
| } else { | |
| break; | |
| } | |
| } | |
| return current; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| File launcherDir = Paths.getLauncherDataDir(); | |
| launcherDir.mkdirs(); | |
| InputStream defaultsFile = Launchii.class.getResourceAsStream("/resources/defaults.yml"); | |
| File optionsFile = new File(launcherDir, "config.yml"); | |
| ConfigurationFile defaults = new ConfigurationFile(defaultsFile); | |
| try { | |
| config = new ConfigurationFile(optionsFile); | |
| } catch (IOException ex) { | |
| Logger.getLogger(Launchii.class.getName()).log(Level.SEVERE, "Could not load config file! (" + new File("config.yml").getAbsolutePath(), ex); | |
| } | |
| config.setDefaults(defaults.rootSection()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment