geforkt von Mirrors/Paper
Line-endings!
By: Dinnerbone <dinnerbone@dinnerbone.com>
Dieser Commit ist enthalten in:
Ursprung
5ac485db46
Commit
8d8700833f
@ -1,136 +1,136 @@
|
|||||||
package org.bukkit.util.config;
|
package org.bukkit.util.config;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStreamWriter;
|
import java.io.OutputStreamWriter;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import org.yaml.snakeyaml.DumperOptions;
|
import org.yaml.snakeyaml.DumperOptions;
|
||||||
import org.yaml.snakeyaml.Yaml;
|
import org.yaml.snakeyaml.Yaml;
|
||||||
import org.yaml.snakeyaml.constructor.SafeConstructor;
|
import org.yaml.snakeyaml.constructor.SafeConstructor;
|
||||||
import org.yaml.snakeyaml.reader.UnicodeReader;
|
import org.yaml.snakeyaml.reader.UnicodeReader;
|
||||||
import org.yaml.snakeyaml.representer.Representer;
|
import org.yaml.snakeyaml.representer.Representer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* YAML configuration loader. To use this class, construct it with path to
|
* YAML configuration loader. To use this class, construct it with path to
|
||||||
* a file and call its load() method. For specifying node paths in the
|
* 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
|
* various get*() methods, they support SK's path notation, allowing you to
|
||||||
* select child nodes by delimiting node names with periods.
|
* select child nodes by delimiting node names with periods.
|
||||||
*
|
*
|
||||||
* <p>
|
* <p>
|
||||||
* For example, given the following configuration file:</p>
|
* For example, given the following configuration file:</p>
|
||||||
*
|
*
|
||||||
* <pre>members:
|
* <pre>members:
|
||||||
* - Hollie
|
* - Hollie
|
||||||
* - Jason
|
* - Jason
|
||||||
* - Bobo
|
* - Bobo
|
||||||
* - Aya
|
* - Aya
|
||||||
* - Tetsu
|
* - Tetsu
|
||||||
* worldguard:
|
* worldguard:
|
||||||
* fire:
|
* fire:
|
||||||
* spread: false
|
* spread: false
|
||||||
* blocks: [cloth, rock, glass]
|
* blocks: [cloth, rock, glass]
|
||||||
* sturmeh:
|
* sturmeh:
|
||||||
* cool: false
|
* cool: false
|
||||||
* eats:
|
* eats:
|
||||||
* babies: true</pre>
|
* babies: true</pre>
|
||||||
*
|
*
|
||||||
* <p>Calling code could access sturmeh's baby eating state by using
|
* <p>Calling code could access sturmeh's baby eating state by using
|
||||||
* <code>getBoolean("sturmeh.eats.babies", false)</code>. For lists, there are
|
* <code>getBoolean("sturmeh.eats.babies", false)</code>. For lists, there are
|
||||||
* methods such as <code>getStringList</code> that will return a type safe list.
|
* 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>This class is currently incomplete. It is not yet possible to get a node.
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @author sk89q
|
* @author sk89q
|
||||||
*/
|
*/
|
||||||
public class Configuration extends ConfigurationNode {
|
public class Configuration extends ConfigurationNode {
|
||||||
private Yaml yaml;
|
private Yaml yaml;
|
||||||
private File file;
|
private File file;
|
||||||
|
|
||||||
public Configuration(File file) {
|
public Configuration(File file) {
|
||||||
super(new HashMap<String, Object>());
|
super(new HashMap<String, Object>());
|
||||||
|
|
||||||
DumperOptions options = new DumperOptions();
|
DumperOptions options = new DumperOptions();
|
||||||
options.setIndent(4);
|
options.setIndent(4);
|
||||||
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
||||||
|
|
||||||
yaml = new Yaml(new SafeConstructor(), new Representer(), options);
|
yaml = new Yaml(new SafeConstructor(), new Representer(), options);
|
||||||
|
|
||||||
this.file = file;
|
this.file = file;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads the configuration file. All errors are thrown away.
|
* Loads the configuration file. All errors are thrown away.
|
||||||
*/
|
*/
|
||||||
public void load() {
|
public void load() {
|
||||||
FileInputStream stream = null;
|
FileInputStream stream = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
stream = new FileInputStream(file);
|
stream = new FileInputStream(file);
|
||||||
read(yaml.load(new UnicodeReader(stream)));
|
read(yaml.load(new UnicodeReader(stream)));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
root = new HashMap<String, Object>();
|
root = new HashMap<String, Object>();
|
||||||
} catch (ConfigurationException e) {
|
} catch (ConfigurationException e) {
|
||||||
root = new HashMap<String, Object>();
|
root = new HashMap<String, Object>();
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
if (stream != null) {
|
if (stream != null) {
|
||||||
stream.close();
|
stream.close();
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Saves the configuration to disk. All errors are clobbered.
|
* Saves the configuration to disk. All errors are clobbered.
|
||||||
*
|
*
|
||||||
* @return true if it was successful
|
* @return true if it was successful
|
||||||
*/
|
*/
|
||||||
public boolean save() {
|
public boolean save() {
|
||||||
FileOutputStream stream = null;
|
FileOutputStream stream = null;
|
||||||
|
|
||||||
file.getParentFile().mkdirs();
|
file.getParentFile().mkdirs();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
stream = new FileOutputStream(file);
|
stream = new FileOutputStream(file);
|
||||||
yaml.dump(root, new OutputStreamWriter(stream, "UTF-8"));
|
yaml.dump(root, new OutputStreamWriter(stream, "UTF-8"));
|
||||||
return true;
|
return true;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
if (stream != null) {
|
if (stream != null) {
|
||||||
stream.close();
|
stream.close();
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
private void read(Object input) throws ConfigurationException {
|
private void read(Object input) throws ConfigurationException {
|
||||||
try {
|
try {
|
||||||
if ( null == input ) {
|
if ( null == input ) {
|
||||||
root = new HashMap<String, Object>();
|
root = new HashMap<String, Object>();
|
||||||
} else {
|
} else {
|
||||||
root = (Map<String, Object>)input;
|
root = (Map<String, Object>)input;
|
||||||
}
|
}
|
||||||
} catch (ClassCastException e) {
|
} catch (ClassCastException e) {
|
||||||
throw new ConfigurationException("Root document must be an key-value structure");
|
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.
|
* default in methods that select a node from a node list.
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static ConfigurationNode getEmptyNode() {
|
public static ConfigurationNode getEmptyNode() {
|
||||||
return new ConfigurationNode(new HashMap<String, Object>());
|
return new ConfigurationNode(new HashMap<String, Object>());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Datei-Diff unterdrückt, da er zu groß ist
Diff laden
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren