Whitespace + general cleanup
This commit is contained in:
@@ -18,10 +18,10 @@ import org.yaml.snakeyaml.representer.Representer;
|
||||
* a file and call its load() method. For specifying node paths in the
|
||||
* various get*() methods, they support SK's path notation, allowing you to
|
||||
* select child nodes by delimiting node names with periods.
|
||||
*
|
||||
*
|
||||
* <p>
|
||||
* For example, given the following configuration file:</p>
|
||||
*
|
||||
*
|
||||
* <pre>members:
|
||||
* - Hollie
|
||||
* - Jason
|
||||
@@ -36,38 +36,39 @@ import org.yaml.snakeyaml.representer.Representer;
|
||||
* cool: false
|
||||
* eats:
|
||||
* babies: true</pre>
|
||||
*
|
||||
*
|
||||
* <p>Calling code could access sturmeh's baby eating state by using
|
||||
* <code>getBoolean("sturmeh.eats.babies", false)</code>. For lists, there are
|
||||
* methods such as <code>getStringList</code> that will return a type safe list.
|
||||
*
|
||||
*
|
||||
* <p>This class is currently incomplete. It is not yet possible to get a node.
|
||||
* </p>
|
||||
*
|
||||
*
|
||||
* @author sk89q
|
||||
*/
|
||||
public class Configuration extends ConfigurationNode {
|
||||
private Yaml yaml;
|
||||
private File file;
|
||||
|
||||
|
||||
public Configuration(File file) {
|
||||
super(new HashMap<String, Object>());
|
||||
|
||||
|
||||
DumperOptions options = new DumperOptions();
|
||||
|
||||
options.setIndent(4);
|
||||
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
||||
|
||||
yaml = new Yaml(new SafeConstructor(), new Representer(), options);
|
||||
|
||||
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Loads the configuration file. All errors are thrown away.
|
||||
*/
|
||||
public void load() {
|
||||
public void load() {
|
||||
FileInputStream stream = null;
|
||||
|
||||
|
||||
try {
|
||||
stream = new FileInputStream(file);
|
||||
read(yaml.load(new UnicodeReader(stream)));
|
||||
@@ -80,56 +81,54 @@ public class Configuration extends ConfigurationNode {
|
||||
if (stream != null) {
|
||||
stream.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
}
|
||||
} catch (IOException e) {}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Saves the configuration to disk. All errors are clobbered.
|
||||
*
|
||||
* @return true if it was successful
|
||||
*
|
||||
* @return true if it was successful
|
||||
*/
|
||||
public boolean save() {
|
||||
FileOutputStream stream = null;
|
||||
|
||||
|
||||
File parent = file.getParentFile();
|
||||
|
||||
if (parent != null) {
|
||||
parent.mkdirs();
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
stream = new FileOutputStream(file);
|
||||
yaml.dump(root, new OutputStreamWriter(stream, "UTF-8"));
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
} finally {
|
||||
} catch (IOException e) {} finally {
|
||||
try {
|
||||
if (stream != null) {
|
||||
stream.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
}
|
||||
} catch (IOException e) {}
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void read(Object input) throws ConfigurationException {
|
||||
try {
|
||||
if ( null == input ) {
|
||||
if (null == input) {
|
||||
root = new HashMap<String, Object>();
|
||||
} else {
|
||||
root = (Map<String, Object>)input;
|
||||
root = (Map<String, Object>) input;
|
||||
}
|
||||
} catch (ClassCastException e) {
|
||||
throw new ConfigurationException("Root document must be an key-value structure");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This method returns an empty ConfigurationNode for using as a
|
||||
* This method returns an empty ConfigurationNode for using as a
|
||||
* default in methods that select a node from a node list.
|
||||
* @return
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user