geforkt von Mirrors/Paper
Added the ability to set a header when saving configuration files.
By: sk89q <the.sk89q@gmail.com>
Dieser Commit ist enthalten in:
Ursprung
727890f5a8
Commit
bdd890d0ae
@ -56,6 +56,7 @@ import org.yaml.snakeyaml.representer.Representer;
|
||||
public class Configuration extends ConfigurationNode {
|
||||
private Yaml yaml;
|
||||
private File file;
|
||||
private String header = null;
|
||||
|
||||
public Configuration(File file) {
|
||||
super(new HashMap<String, Object>());
|
||||
@ -93,8 +94,49 @@ public class Configuration extends ConfigurationNode {
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the configuration to disk. All errors are clobbered.
|
||||
*
|
||||
* Set the header for the file as a series of lines that are terminated
|
||||
* by a new line sequence.
|
||||
*
|
||||
* @param headerLines header lines to prepend
|
||||
*/
|
||||
public void setHeader(String ... headerLines) {
|
||||
StringBuilder header = new StringBuilder();
|
||||
|
||||
for (String line : headerLines) {
|
||||
if (header.length() > 0) {
|
||||
header.append("\r\n");
|
||||
}
|
||||
header.append(line);
|
||||
}
|
||||
|
||||
setHeader(header.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the header for the file. A header can be provided to prepend the
|
||||
* YAML data output on configuration save. The header is
|
||||
* printed raw and so must be manually commented if used. A new line will
|
||||
* be appended after the header, however, if a header is provided.
|
||||
*
|
||||
* @param header header to prepend
|
||||
*/
|
||||
public void setHeader(String header) {
|
||||
this.header = header;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the set header.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getHeader() {
|
||||
return header;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the configuration to disk. All errors are clobbered.
|
||||
*
|
||||
* @param header header to prepend
|
||||
* @return true if it was successful
|
||||
*/
|
||||
public boolean save() {
|
||||
@ -108,7 +150,12 @@ public class Configuration extends ConfigurationNode {
|
||||
|
||||
try {
|
||||
stream = new FileOutputStream(file);
|
||||
yaml.dump(root, new OutputStreamWriter(stream, "UTF-8"));
|
||||
OutputStreamWriter writer = new OutputStreamWriter(stream, "UTF-8");
|
||||
if (header != null) {
|
||||
writer.append(header);
|
||||
writer.append("\r\n");
|
||||
}
|
||||
yaml.dump(root, writer);
|
||||
return true;
|
||||
} catch (IOException e) {} finally {
|
||||
try {
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren